diff --git a/DESIGN.md b/DESIGN.md index 466f388f2..afcec2f33 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -446,6 +446,11 @@ 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. + - 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. + - `outerS3` at the witness (`P3/VmWitOuter`, `outer_schedControl`, `outer_untyped`): every platform argument of `VEAnyX.outerS3` is instantiated at `warV` / `wAR` / `ipiMR` (`ARIface`, `ZvVirtAgree`, `ZvSyncKeep`, `CapAgree`, `PortInValMR`, `MRKeepD`, `MachPlat`, `MRKeep`, `PPPlatA`, `IrqPlatA`, `TimeAgree`, `ArchRefines`, the budget and timer queries, `StoreExt`), so the theorem's conclusion holds at the witness for both entries. Left as [P] premises, all about opaque `Fl4Spec` constants and none about the arch: `OpaqueWK` (one domain, `maxTicksToUs`, kernel WCET and timer unit, the period bound, `setDeadlineImpl` determinism, a port-read value), `virtKind`, `retypeFanOutLimit = 256`, `resetChunkBits = 16`, `FpuImplDet`, `FpuWriteDet`. + - Stage 3 does not prove: that the post re-establishes `S3Pre` (no multi-entry run; `SInvXV` has no preservation theorem), a `VEAnyX` state from boot, or non-vacuity of the other events' `S3Pre`. - Hypotheses still to discharge are named in each file. The main ones: the state facts after the TCB fault-handler install, finalisation of a final Endpoint, Notification, Reply, Thread or SchedContext cap on the spec side (`FinWorkHyp`; FL4's side and the VSpace cases are proved), the reply and fault paths, and the placement link between P3's typed heap and P1's object sets. - The proofs so far found no disagreement between FL4 and the spec. diff --git a/README.md b/README.md index 4b3d2f364..c647538c2 100644 --- a/README.md +++ b/README.md @@ -242,13 +242,27 @@ cap's owner, revoke and delete, capability delete of every cap (`VmEptState.capDelete`), and every CNode invocation (labels 25–32: Revoke, Delete, CancelBadgedSends, Copy, Mint, Move, Mutate, Rotate) at that state through FL4's dispatcher (`VmEptState.cnodeDecodeAll`). -Stage 2 proves the native x86 syscall entry and VM-aware scheduler -finish. The entry discharges the VM-specific capability routes and -takes the native-capability dispatcher cases as a premise. Separate -refinements cover VMEnter on VMX and SVM, ordinary unknown and trapped -syscalls, host interrupts, user exceptions, VM faults, and stalled and -unstalled VCPU exits. The proofs start from the joint VM state under -the entry and platform premises in [DESIGN.md §9](DESIGN.md#9-proof-plan). +Stage 3 proves one theorem over the nine x86 core entries +(`VEAnyX.outerS3`): native syscall, VMEnter on VMX and SVM, ordinary +unknown and trapped syscalls, host interrupts, user exceptions, VM +faults, and stalled and unstalled VCPU exits. The syscall case +(`VmEptState.kernel_entryS3`) covers the native-capability dispatcher +cases, the invocation, receive and yield arms and the finish, which +Stage 2 (`VmEptState.kernel_entry`) took as premises. Every entry starts +from the joint VM state (`VEAnyX`) and the premises of that event +(`S3Pre`), and returns a related extended-spec outcome at `VEAnyX`. The +post does not re-establish `S3Pre`, so the next entry must be given it +again, and no run of several entries is proved. Producing the first +`VEAnyX` state from boot is open. `VEAnyX` and `S3Pre` are not vacuous: +a constructed concrete state with a running guest and its VCPU satisfies +both for a syscall invoking a SchedControl cap and for one invoking an +Untyped cap (`JBootQ.VWit.witness_schedControl`, `witness_untyped`; the +state is not shown to be reached from boot). At that state a dispatch +pair exists and the invoked cap's core restriction is exercised +(`w_core_exercised`), and `outerS3` applies with every platform argument +instantiated except premises on opaque spec constants (`outer_schedControl`, +`outer_untyped`). The premises are listed in +[DESIGN.md §9](DESIGN.md#9-proof-plan). The [S] scope is serial core entry after the gate, with one scheduling domain and the single-core IRQ view. Assembly, hardware guest execution @@ -256,11 +270,13 @@ and the capture of the VCPU image and vector lie at the trusted boundary. Both fresh host and saved guest interrupt vectors exclude cross-core IPIs 126 and 127. The saved path covers no vector, active and inactive device lines, and non-IPI vectors above the device range. Native syscall numbers -are −11 through −1 with the trap flag clear. VMEnter (−23) also requires -an agreed enabled backend and a bound VCPU. Ordinary unknown syscalls use -`UnkSys`, exclude debug/TLS calls, and require the spec to select the -ordinary fault case. Software-trap vectors are below 256 and use FL4's -zero-extended payload. +are −11 through −1 with the trap flag clear. For the capability the +syscall invokes, a SchedControl cap must name the executing core and an +Untyped cap is covered only on core 0. VMEnter (−23) also requires a +clear trap flag, an agreed enabled backend and a bound VCPU. Ordinary +unknown syscalls use `UnkSys`, exclude debug/TLS calls, and require the +spec to select the ordinary fault case. Software-trap vectors are below +256 and use FL4's zero-extended payload. ## x86 virtualization diff --git a/proofs/Fl4Proofs.lean b/proofs/Fl4Proofs.lean index 245e8d981..88aaff382 100644 --- a/proofs/Fl4Proofs.lean +++ b/proofs/Fl4Proofs.lean @@ -449,6 +449,7 @@ import Fl4Proofs.P3.SpecNKO import Fl4Proofs.P3.IpcGrant import Fl4Proofs.P3.IpcGrantX import Fl4Proofs.P3.IpcGrantTr +import Fl4Proofs.P3.IpcGrantTrV import Fl4Proofs.P3.IpcEpGrant import Fl4Proofs.P3.IpcLeaves import Fl4Proofs.P3.EpRecvX @@ -2382,6 +2383,9 @@ import Fl4Proofs.P3.VirtTyK6 import Fl4Proofs.P3.VirtAwaken import Fl4Proofs.P3.VirtNvTcb import Fl4Proofs.P3.VirtR7e +import Fl4Proofs.P3.VirtFrameBatch +import Fl4Proofs.P3.VirtFrameBatchConservative +import Fl4Proofs.P3.VmFrameBatchRestart import Fl4Proofs.P3.VirtR7eK import Fl4Proofs.P3.VirtNvcW import Fl4Proofs.P3.VirtR7eK2 @@ -2814,7 +2818,13 @@ import Fl4Proofs.P3.VmDelHyps import Fl4Proofs.P3.VmDelCap import Fl4Proofs.P3.VmCnodeRD import Fl4Proofs.P3.VmCnodeVtxSpec +import Fl4Proofs.P3.DeriveVt import Fl4Proofs.P3.VmCnodeVtx +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.VmEntryS3Xfer +import Fl4Proofs.P3.P3JSendStF +import Fl4Proofs.P3.VmEntryS3SendF import Fl4Proofs.P3.VmCnodeCp import Fl4Proofs.P3.VmCnodeAll import Fl4Proofs.P3.VmDisp @@ -2831,6 +2841,77 @@ import Fl4Proofs.P3.VmFinish import Fl4Proofs.P3.VmHiErr import Fl4Proofs.P3.VmInvAfter import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmEntryS3Budget +import Fl4Proofs.P3.VmEntryS3Yield +import Fl4Proofs.P3.VmEntryS3Recv +import Fl4Proofs.P3.VmEntryS3Finish +import Fl4Proofs.P3.VmEntryS3Oracle +import Fl4Proofs.P3.VmEntryS3Fault +import Fl4Proofs.P3.VmEntryS3Pool +import Fl4Proofs.P3.VmEntryS3ScCtl +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.VmEntryS3Untyped +import Fl4Proofs.P3.VmEntryS3Dispatch +import Fl4Proofs.P3.VmEntryS3Tdx +import Fl4Proofs.P3.VmEntryS3Pml4 +import Fl4Proofs.P3.VmEntryS3Thread +import Fl4Proofs.P3.VmEntryS3Sc +import Fl4Proofs.P3.VmEntryS3Domain +import Fl4Proofs.P3.VmEntryS3IoControl +import Fl4Proofs.P3.VmEntryS3Send +import Fl4Proofs.P3.VmEntryS3ReplyNormal +import Fl4Proofs.P3.JAtZBUpdConsumed +import Fl4Proofs.P3.VmEntryS3XferAll +import Fl4Proofs.P3.VmEntryS3ReplyXfer +import Fl4Proofs.P3.JAtZBRstPost +import Fl4Proofs.P3.JAtZBRunTmo +import Fl4Proofs.P3.VmEntryS3ReplyFault +import Fl4Proofs.P3.VmEntryS3ReplyTmo +import Fl4Proofs.P3.JAtZBTmoRecv +import Fl4Proofs.P3.VmEntryS3ReplyRecv +import Fl4Proofs.P3.VmEntryS3Irq +import Fl4Proofs.P3.IrqCtlTrace +import Fl4Proofs.P3.VmEntryS3IrqCtl +import Fl4Proofs.P3.JAtZBMakePool +import Fl4Proofs.P3.VmEptMakePool +import Fl4Proofs.P3.VmEntryS3AsidControl +import Fl4Proofs.P3.JAtZBThrSched +import Fl4Proofs.P3.JAtZBRestartN +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.VmEntryS3ThrAdapt +import Fl4Proofs.P3.JAtZBThrResume +import Fl4Proofs.P3.JAtZBThrMcp +import Fl4Proofs.P3.JAtZBThrUnbind +import Fl4Proofs.P3.JAtZBThrBind +import Fl4Proofs.P3.JAtZBThrBindTop +import Fl4Proofs.P3.JAtZBThrPrio +import Fl4Proofs.P3.JAtZBThrSetPrio +import Fl4Proofs.P3.JAtZBThrTls +import Fl4Proofs.P3.JAtZBThrWrite +import Fl4Proofs.P3.JAtZBThrWriteTop +import Fl4Proofs.P3.JAtZBThrCopy +import Fl4Proofs.P3.JAtZBThrCopyTop +import Fl4Proofs.P3.JAtZBThrRead +import Fl4Proofs.P3.JAtZBThrReadTop +import Fl4Proofs.P3.JAtZBThrPostF +import Fl4Proofs.P3.JAtZBThrExtAux +import Fl4Proofs.P3.JAtZBThrFlags +import Fl4Proofs.P3.JAtZBThrSingleStep +import Fl4Proofs.P3.JAtZBThrBp +import Fl4Proofs.P3.JAtZBThrGetBp +import Fl4Proofs.P3.JAtZBThrFpuAux +import Fl4Proofs.P3.JAtZBThrFpu +import Fl4Proofs.P3.JAtZBThrTimer +import Fl4Proofs.P3.VmEntryS3InsTcb +import Fl4Proofs.P3.JAtZBTcbBuf +import Fl4Proofs.P3.VmEntryS3TcbInst +import Fl4Proofs.P3.VmEntryS3ReplyArm +import Fl4Proofs.P3.VmEntryS3FrameBatch +import Fl4Proofs.P3.VmEntryS3FrameMap +import Fl4Proofs.P3.VmEntryS3Frame +import Fl4Proofs.P3.VmEntryS3Tables +import Fl4Proofs.P3.VmEntryS3Selected +import Fl4Proofs.P3.VmEntryS3RecvEndpoint import Fl4Proofs.P3.NatFoot import Fl4Proofs.P3.EptSetRoot import Fl4Proofs.P3.EptSetRootDec @@ -2944,3 +3025,116 @@ import Fl4P2Base.SerialW import Fl4P2Base.GateImpl import Fl4P2Base.GateLock import Fl4P2Base.SafeW +import Fl4Proofs.P3.VmEntryS3TcbSsp +import Fl4Proofs.P3.VmEntryS3ThrDisp +import Fl4Proofs.P3.VmEntryS3Coverage +import Fl4Proofs.P3.VmFinFamTac +import Fl4Proofs.P3.VmFinFamA +import Fl4Proofs.P3.VmFinFamFpu +import Fl4Proofs.P3.VmFinFamFn +import Fl4Proofs.P3.VmFinFamDl +import Fl4Proofs.P3.VmFinFamHook +import Fl4Proofs.P3.VmFinGenBase +import Fl4Proofs.P3.VmFinGenLRd +import Fl4Proofs.P3.VmFinGenLIo +import Fl4Proofs.P3.VmFinGenLSi +import Fl4Proofs.P3.VmFinGenLSg +import Fl4Proofs.P3.VmFinGenLLd +import Fl4Proofs.P3.VmFinGenLFo +import Fl4Proofs.P3.VmFinGenVF +import Fl4Proofs.P3.VmFinVF +import Fl4Proofs.P3.VmFinGenUi +import Fl4Proofs.P3.VmFinGenIdk +import Fl4Proofs.P3.VmFinGenNk +import Fl4Proofs.P3.VmFinGenYk +import Fl4Proofs.P3.VmFinGenHu +import Fl4Proofs.P3.VmFinGenNq +import Fl4Proofs.P3.VmFinGenCt +import Fl4Proofs.P3.VmFinGenPj +import Fl4Proofs.P3.VmFinGenB +import Fl4Proofs.P3.VmFinFamB +import Fl4Proofs.P3.VmFinVF2 +import Fl4Proofs.P3.VmFinArmB +import Fl4Proofs.P3.VmFinGenDl +import Fl4Proofs.P3.VmFinArmDl +import Fl4Proofs.P3.VmFinArmNq +import Fl4Proofs.P3.VmFinDecode +import Fl4Proofs.P3.VmFinArmIlVm +import Fl4Proofs.P3.VmFinArmIl +import Fl4Proofs.P3.VmFinFbV +import Fl4Proofs.P3.VmFinArmIlTop +import Fl4Proofs.P3.VmFinArmFo +import Fl4Proofs.P3.VmFinArmFoTop +import Fl4Proofs.P3.VmFinVgFb +import Fl4Proofs.P3.VmFinArmVgA +import Fl4Proofs.P3.VmFinArmVgB +import Fl4Proofs.P3.VmFinArmVgC +import Fl4Proofs.P3.VmFinVgSlot5 +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.VmRecvFoot1 +import Fl4Proofs.P3.VmRecvFoot2a +import Fl4Proofs.P3.VmRecvFoot2 +import Fl4Proofs.P3.VmRecvFootB +import Fl4Proofs.P3.VmRecvFootC +import Fl4Proofs.P3.VmRecvFootD +import Fl4Proofs.P3.VmRecvFootX +import Fl4Proofs.P3.VmRecvFootXT +import Fl4Proofs.P3.VmRecvLeaves +import Fl4Proofs.P3.VmMidBlock +import Fl4Proofs.P3.VmRecvMid +import Fl4Proofs.P3.VmRecvMidSig +import Fl4Proofs.P3.VmRecvMidNtfn +import Fl4Proofs.P3.VmRecvMidEp +import Fl4Proofs.P3.VmRecvMidEp2 +import Fl4Proofs.P3.VmRecvMidAsm +import Fl4Proofs.P3.VmRecvMidDon +import Fl4Proofs.P3.VmRecvMidDonV +import Fl4Proofs.P3.VmFaultMid +import Fl4Proofs.P3.VmFaultSw +import Fl4Proofs.P3.VmRecvS +import Fl4Proofs.P3.VmRecvMidTop +import Fl4Proofs.P3.VmJAtZRB +import Fl4Proofs.P3.VmSwitchR +import Fl4Proofs.P3.VmSwitchRTop +import Fl4Proofs.P3.VmJAtZRBRet +import Fl4Proofs.P3.VmJAtZRBFin +import Fl4Proofs.P3.VmFinishR +import Fl4Proofs.P3.VmMidQ +import Fl4Proofs.P3.VmMidFb +import Fl4Proofs.P3.VmMidRunQ +import Fl4Proofs.P3.VmMidB +import Fl4Proofs.P3.VmMidVfh +import Fl4Proofs.P3.VmMidProd +import Fl4Proofs.P3.VmRecvFin +import Fl4Proofs.P3.VmEntryS3TopG +import Fl4Proofs.P3.VmEntryS3TopR +import Fl4Proofs.P3.VmEntryS3Fast +import Fl4Proofs.P3.VmHiRvm +import Fl4Proofs.P3.VmEntryS3Orc +import Fl4Proofs.P3.VmFinFrontS3 +import Fl4Proofs.P3.VmEntryS3Start +import Fl4Proofs.P3.SInvXV +import Fl4Proofs.P3.SInvXVWit +import Fl4Proofs.P3.VmWitPlatform +import Fl4Proofs.P3.VmWitSpec +import Fl4Proofs.P3.VmWitMem +import Fl4Proofs.P3.VmWitZ +import Fl4Proofs.P3.VmWitCs +import Fl4Proofs.P3.VmWitBlk +import Fl4Proofs.P3.VmWitLay +import Fl4Proofs.P3.VmWitSpecB +import Fl4Proofs.P3.VmWitJAt +import Fl4Proofs.P3.VmWitVcpu +import Fl4Proofs.P3.VmWitS3 +import Fl4Proofs.P3.VmWitOuter +import Fl4Proofs.P3.VmEntryS3TopS +import Fl4Proofs.P3.VmEntryS3Mid +import Fl4Proofs.P3.VmEntryS3Disp +import Fl4Proofs.P3.VmEntryS3Entry +import Fl4Proofs.P3.VmEntryS3Outer +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmVxLow +import Fl4Proofs.P3.VmKEntryX +import Fl4Proofs.P3.VmHandleX +import Fl4Proofs.P3.VmHandleTopX +import Fl4Proofs.P3.VmInvAfterX diff --git a/proofs/Fl4Proofs/P3/DeriveVt.lean b/proofs/Fl4Proofs/P3/DeriveVt.lean new file mode 100644 index 000000000..ff2892781 --- /dev/null +++ b/proofs/Fl4Proofs/P3/DeriveVt.lean @@ -0,0 +1,57 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **the VT-x view of a derived cap**. + +`vkeyF` and `NewOK` are moved here from P3/VmCnodeVtx (definitions unchanged) so the IPC grant's insert record +(P3/IpcGrantTr's `InsRec`) can carry **`VtOK`**: FL4's `derive_cap` (objects.rs l. 208-239) gives a cap with no VT-x +view, or the source's view and `NewOK` (an unmapped table cap is rejected, a frame comes out unmapped and non-EPT). +**`derive_vt`** proves it for `objects.derive_cap`. No named premise. +-/ +import Fl4Proofs.P3.CnodeVsW +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical + +noncomputable section + +/-- **The VT-x / thread view of an FL4 cap** (module docstring). -/ +def vkeyF : capability.Cap → Option (Nat × U64 × U64 × Option (U64 × U64)) + | .Thread t => some (0, t, 0#u64, none) + | .Vcpu w => some (1, w, 0#u64, none) + | .Ept p l m a v => some (2, p, l, if m then some (a, v) else none) + | .Frame _ _ _ _ true true a v => some (3, 0#u64, 0#u64, some (a, v)) + | _ => none + +/-- **A slot's cap made by a copy** (module docstring): neither an unmapped `Ept` cap nor an EPT-mapped frame cap. -/ +def NewOK (c : capability.Cap) : Prop := + (∀ p l a v, c ≠ .Ept p l false a v) ∧ ∀ p sz r d a v, c ≠ .Frame p sz r d true true a v + +/-- **A copy's cap** (`derive_cap`, `copy`): no VT-x view, or the source's view and `NewOK`. -/ +def VtOK (cv c' : capability.Cap) : Prop := vkeyF c' = none ∨ (vkeyF c' = vkeyF cv ∧ NewOK c') + +theorem derive_vt {S : Type} {st : machine.Store S} {s : S} {slot : obj.Slot} {c c' : capability.Cap} + (h : objects.derive_cap st s slot c = ok (.Ok c')) : VtOK c c' := by + unfold VtOK + cases c + case Untyped p sb fi dev => + unfold objects.derive_cap at h + revert h; generalize cdt.ensure_no_children st s slot = r + cases r with + | ret v => + cases v <;> simp [core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual] + intro h; subst h; exact Or.inl rfl + | _ => simp + case Frame p sz r dv m ept a va => + simp only [objects.derive_cap] at h; simp at h; subst h + exact Or.inl rfl + case Ept p l m a v => + cases m <;> simp [objects.derive_cap] at h + subst h + exact Or.inr ⟨rfl, fun p' l' a' v' e => by simp at e, fun p' sz r d a' v' e => by simp at e⟩ + all_goals simp only [objects.derive_cap] at h + all_goals (repeat' split at h) <;> (simp at h; try subst h; first | exact Or.inl rfl | exact Or.inr ⟨rfl, fun p' l' a' v' e => by simp at e, fun p' sz r d a' v' e => by simp at e⟩) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/DomDecode.lean b/proofs/Fl4Proofs/P3/DomDecode.lean index 447320152..f0936e468 100644 --- a/proofs/Fl4Proofs/P3/DomDecode.lean +++ b/proofs/Fl4Proofs/P3/DomDecode.lean @@ -270,6 +270,101 @@ def startSt (σ : Fl4Spec.State Unit) (n : Nat) : Fl4Spec.State Unit := def startS (s1 : S) (kb index : U64) : S := L.wr (L.wr (L.wr s1 (ksW kb 32#u64) index) (ksW kb 16#u64) 0#u64) (ksW kb 24#u64) 0#u64 +/-- The steps made by a selected Domain invocation. The only non-scheduler +step is the caller's restart, paired with the exact concrete/spec writes. -/ +inductive DomainTrace (th : TypedHeap) : S → Fl4Spec.State Unit → S → Fl4Spec.State Unit → Prop where + | restart {s s1 : S} {σ σ1 : Fl4Spec.State Unit} {ct : U64} {x : Fl4Spec.Tcb} : + σ.curThread = ct.bv → σ.kheap ct.bv = some (.tcb x) → th ct = some .tcb → + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + CurRun σ → DomainTrace th s σ s1 σ1 + | sched {s s' σ σ'} : + SchedStep (L := L) th s σ s' σ' → DomainTrace th s σ s' σ' + | trans {s s1 s2 σ σ1 σ2} : + DomainTrace th s σ s1 σ1 → DomainTrace th s1 σ1 s2 σ2 → DomainTrace th s σ s2 σ2 + +/-- Native clients can forget the selected trace and use their original +primitive-step relation. -/ +theorem DomainTrace.toSteps {th : TypedHeap} {s s' : S} {σ σ' : Fl4Spec.State Unit} + (tr : DomainTrace (L := L) th s σ s' σ') (arI : machine.Arch A) (a : A) : + Steps (L := L) arI th s a σ s' a σ' := by + induction tr with + | restart hc hk ht hs hm hr => exact Steps.restart arI hc hk ht hs hm hr + | sched h => exact Steps.sched arI h + | trans _ _ hi hj => exact hi.trans arI hj + +/-- A relation between the paired states of a Domain step. -/ +abbrev DomStepRel (S : Type) := S → Fl4Spec.State Unit → S → Fl4Spec.State Unit → Prop + +/-- The decoder uses only a restart, scheduler steps, and their composition. -/ +structure DomStepOps (th : TypedHeap) (trace : DomStepRel S) : Prop where + restart {s s1 : S} {σ σ1 : Fl4Spec.State Unit} {ct : U64} {x : Fl4Spec.Tcb} : + σ.curThread = ct.bv → σ.kheap ct.bv = some (.tcb x) → th ct = some .tcb → + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + CurRun σ → trace s σ s1 σ1 + sched {s s' : S} {σ σ' : Fl4Spec.State Unit} : + SchedStep (L := L) th s σ s' σ' → trace s σ s' σ' + trans {s s1 s2 : S} {σ σ1 σ2 : Fl4Spec.State Unit} : + trace s σ s1 σ1 → trace s1 σ1 s2 σ2 → trace s σ s2 σ2 + +/-- The existing native Domain interface records a primitive-step witness +at any architecture state. -/ +def DomNativeSteps (th : TypedHeap) : DomStepRel S := + fun s σ s' σ' => ∀ (A : Type) (arI : machine.Arch A) (a : A), + Steps (L := L) arI th s a σ s' a σ' + +theorem DomStepOps.native (th : TypedHeap) : DomStepOps (L := L) th (DomNativeSteps (L := L) th) where + restart hcur hk ht hs he hC _ arI a := Steps.restart arI hcur hk ht hs he hC + sched h _ arI a := Steps.sched arI h + trans f g A arI a := (f A arI a).trans arI (g A arI a) + +theorem DomStepOps.restricted (th : TypedHeap) : DomStepOps (L := L) th (DomainTrace (L := L) th) where + restart := DomainTrace.restart + sched := DomainTrace.sched + trans := DomainTrace.trans + + +def DomSetHypFor (th : TypedHeap) (trace : DomStepRel S) (s : S) (σ : Fl4Spec.State Unit) (ct : U64) : Prop := + ∀ s1 σ1 (t d : U64), sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ1)}, False) → + th t = some .tcb → d.val < 1 → + sched.set_domain st s1 t d ⦃ s2 => SlotsSame th (L.mem s1) (L.mem s2) ∧ L.ksb s2 = L.ksb s1 ∧ + DomSame (L.mem s1) (L.mem s2) (L.ksb s1) ∧ + ∃ y ∈ (Fl4Spec.invokeSetDomain (Z := Unit) t.bv (Fl4Spec.ucast Fl4Spec.domainLen d.bv) σ1).1, + KSub σ1 y.2 ∧ (∀ m, StateRel th m σ1 → StateRel th m y.2) ∧ y.2.cdt = σ1.cdt ∧ + y.2.isOriginalCap = σ1.isOriginalCap ∧ y.2.domainList = σ1.domainList ∧ + y.2.domainStartIndex = σ1.domainStartIndex ∧ + trace s1 σ1 s2 y.2 ⦄ + +def DomResHypFor (th : TypedHeap) (trace : DomStepRel S) (s : S) (σ : Fl4Spec.State Unit) (ct : U64) : Prop := + ∀ s1 σ1 (index : U64), sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ1)}, False) → index.val < 2 → + sched.reschedule_required st (startS (L := L) s1 (L.ksb s) index) ⦃ s5 => + SlotsSame th (L.mem (startS (L := L) s1 (L.ksb s) index)) (L.mem s5) ∧ L.ksb s5 = L.ksb s ∧ + DomSame (L.mem (startS (L := L) s1 (L.ksb s) index)) (L.mem s5) (L.ksb s) ∧ + ∃ y ∈ (Fl4Spec.rescheduleRequired (Z := Unit) (startSt σ1 index.val)).1, + KSub (startSt σ1 index.val) y.2 ∧ (∀ m, StateRel th m (startSt σ1 index.val) → StateRel th m y.2) ∧ + y.2.cdt = σ1.cdt ∧ y.2.isOriginalCap = σ1.isOriginalCap ∧ y.2.domainList = σ1.domainList ∧ + y.2.domainStartIndex = index.val ∧ + trace (startS (L := L) s1 (L.ksb s) index) (startSt σ1 index.val) s5 y.2 ⦄ + +def DomPostFor (th : TypedHeap) (trace : DomStepRel S) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation) (s : S) (res : core.result.Result Unit error.Exc × S) : + Prop := + (∃ e, res.1 = .Err e ∧ res.2 = s ∧ ∃ e', m.run σ = ({(.error e', σ)}, False) ∧ SysErrRel e e') ∨ + (res.1 = .Ok () ∧ ∃ inv, m.run σ = ({(.ok inv, σ)}, False) ∧ ∃ σ1, + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ y ∈ (Fl4Spec.invokeDomain (Z := Unit) inv σ1).1, SlotsSame th (L.mem s) (L.mem res.2) ∧ + CRel th (L.mem res.2) y.2 G ∧ FinInv th (L.mem res.2) y.2 G ∧ L.ksb res.2 = L.ksb s ∧ + DomRel (L.mem res.2) (L.ksb res.2) y.2 ∧ + (CurRun σ → trace s σ res.2 y.2)) + +def DomPostAFor (th : TypedHeap) (trace : DomStepRel S) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation) (s : S) (a : A) : + core.result.Result Unit error.Exc × S × A → Prop + | (r, s', a') => a' = a ∧ DomPostFor (L := L) th trace σ G m s (r, s') + /-- **The scheduler step of DomainSetSet** (for the scheduler's proofs): from the state after `set_thread_state Restart`, FL4's `set_domain` of a TCB to a domain below `NUM_DOMAINS` keeps the slots and the domain schedule, and @@ -305,6 +400,14 @@ def DomResHyp (th : TypedHeap) (s : S) (σ : Fl4Spec.State Unit) (ct : U64) : Pr ∀ (A : Type) (arI : machine.Arch A) (a : A), Steps (L := L) arI th (startS (L := L) s1 (L.ksb s) index) a (startSt σ1 index.val) s5 a y.2 ⦄ +/-- DomainSetSet scheduler receipt restricted to the reached native writes. -/ +abbrev DomSetTraceHyp (th : TypedHeap) (s : S) (σ : Fl4Spec.State Unit) (ct : U64) : Prop := + DomSetHypFor (L := L) th (DomainTrace (L := L) th) s σ ct + +/-- DomainScheduleSetStart scheduler receipt with its reached scheduler step. -/ +abbrev DomResTraceHyp (th : TypedHeap) (s : S) (σ : Fl4Spec.State Unit) (ct : U64) : Prop := + DomResHypFor (L := L) th (DomainTrace (L := L) th) s σ ct + /-- **The result of `domain::decode`**: the spec's error with the state unchanged, or the spec's decode, `set_thread_state Restart` and an outcome of `invoke_domain`, with the CSpace relation, `FinInv` and `DomRel`; the run @@ -320,14 +423,18 @@ def DomPost (th : TypedHeap) (σ : Fl4Spec.State Unit) (G : MdbGhost) DomRel (L.mem res.2) (L.ksb res.2) y.2 ∧ (CurRun σ → ∀ (A : Type) (arI : machine.Arch A) (a : A), Steps (L := L) arI th s a σ res.2 a y.2)) -/-- `DomPost` of `domain::decode`'s result with the arch state: the arch -state `a` given back (the DomainSetSet target's stall is a no-op, -`DomStallHyp`). -/ +/-- The native decoder post, with the original architecture state returned. -/ def DomPostA (th : TypedHeap) (σ : Fl4Spec.State Unit) (G : MdbGhost) (m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation) (s : S) (a : A) : core.result.Result Unit error.Exc × S × A → Prop | (r, s', a') => a' = a ∧ DomPost (L := L) th σ G m s (r, s') +/-- The actual paired Domain outcome with its restricted trace. -/ +abbrev DomTracePostA (th : TypedHeap) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation) (s : S) (a : A) : + core.result.Result Unit error.Exc × S × A → Prop := + DomPostAFor (L := L) th (DomainTrace (L := L) th) σ G m s a + /-- **DomainSetSet's stall of its target is a no-op** (`remote_tcb_stall_id`: on one core every thread has the current core's affinity). -/ def DomStallHyp (arI : machine.Arch A) (th : TypedHeap) (s : S) (a : A) : Prop := @@ -337,13 +444,21 @@ def DomStallHyp (arI : machine.Arch A) (th : TypedHeap) (s : S) (a : A) : Prop : theorem dom_let_pair {α β γ : 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 -theorem dom_err {th : TypedHeap} {σ : Fl4Spec.State Unit} {G : MdbGhost} +set_option maxHeartbeats 16000000 in +private theorem dom_err_for {trace : DomStepRel S} {th : TypedHeap} {σ : Fl4Spec.State Unit} {G : MdbGhost} {m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation} {s : S} {e : error.Exc} {e' : Fl4Spec.SyscallError} {a : A} (h : m.run σ = ({(.error e', σ)}, False)) (he : SysErrRel e e') : (ok ((.Err e, s, a) : core.result.Result Unit error.Exc × S × A)) ⦃ res => - DomPostA (L := L) th σ G m s a res ⦄ := by + DomPostAFor (L := L) th trace σ G m s a res ⦄ := by rw [WP.spec_ok]; exact ⟨rfl, Or.inl ⟨e, rfl, rfl, e', h, he⟩⟩ +theorem dom_err {th : TypedHeap} {σ : Fl4Spec.State Unit} {G : MdbGhost} + {m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation} {s : S} {e : error.Exc} {e' : Fl4Spec.SyscallError} + {a : A} (h : m.run σ = ({(.error e', σ)}, False)) (he : SysErrRel e e') : + (ok ((.Err e, s, a) : core.result.Result Unit error.Exc × S × A)) ⦃ res => + DomPostA (L := L) th σ G m s a res ⦄ := + dom_err_for (trace := DomNativeSteps (L := L) th) h he + /-- The CSpace parts and `FinInv` carried by a step that keeps the slots. -/ theorem crel_step {th : TypedHeap} {m m' : U64 → U64} {σ σ' : Fl4Spec.State Unit} {G : MdbGhost} (hCR : CRel th m σ G) (hFI : FinInv th m σ G) (hS : SlotsSame th m m') (hK : KSub σ σ') @@ -358,9 +473,8 @@ theorem ksW_al {kb o : U64} (hkf : kb.val + 13568 ≤ 2 ^ 64) (hka : kb.val % 8 (h8 : o.val % 8 = 0) : P1.Aligned (ksW kb o) := by unfold P1.Aligned; rw [ksW_val (by omega)]; omega -/-- **`set_thread_state Restart`**, with the CSpace parts, `FinInv` and the -domain schedule. -/ -theorem dom_restart (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.State Unit) (G : MdbGhost) +set_option maxHeartbeats 16000000 in +private theorem dom_restart_for {trace : DomStepRel S} (th : TypedHeap) (steps : DomStepOps (L := L) th trace) (hP : Placed th) (s : S) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) (ct : U64) (_hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) @@ -371,7 +485,7 @@ theorem dom_restart (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.Stat L.ksb s1 = L.ksb s ∧ ∃ σ1, Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ1)}, False) ∧ CRel th (L.mem s1) σ1 G ∧ FinInv th (L.mem s1) σ1 G ∧ DomRel (L.mem s1) (L.ksb s) σ1 ∧ σ1.domainList = σ.domainList ∧ - (CurRun σ → ∀ (A : Type) (arI : machine.Arch A) (a : A), Steps (L := L) arI th s a σ s1 a σ1) := by + (CurRun σ → trace s σ s1 σ1) := by have htf := hP.fits ct _ htcb have hta := hP.align ct _ htcb have hstv : (stA ct).val = ct.val + 3328 := by @@ -404,8 +518,24 @@ theorem dom_restart (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.Stat refine ⟨s1, hs1, hS, hkb, σ1, hst1, ?_⟩ have hK := ksub_of_eq (setThreadState_nks _ _) hst1 obtain ⟨hCR1, hFI1⟩ := crel_step ⟨hR, hV, hM, hC, hI⟩ hFI hS hK hR1 hcdt1 hio1 - exact ⟨hCR1, hFI1, hDR.of_same hDS hdl.1 hdl.2, hdl.1, fun hC A arI a => - Steps.restart arI _hcur hkt htcb hs1 (by rw [_hcur]; exact hst1) hC⟩ + exact ⟨hCR1, hFI1, hDR.of_same hDS hdl.1 hdl.2, hdl.1, fun hC => + steps.restart _hcur hkt htcb hs1 (by rw [_hcur]; exact hst1) hC⟩ + +/-- **`set_thread_state Restart`**, with the CSpace parts, `FinInv` and the +domain schedule. -/ +theorem dom_restart (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (_hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) : + ∃ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 ∧ SlotsSame th (L.mem s) (L.mem s1) ∧ + L.ksb s1 = L.ksb s ∧ ∃ σ1, Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ1)}, False) ∧ + CRel th (L.mem s1) σ1 G ∧ FinInv th (L.mem s1) σ1 G ∧ DomRel (L.mem s1) (L.ksb s) σ1 ∧ + σ1.domainList = σ.domainList ∧ + (CurRun σ → ∀ (A : Type) (arI : machine.Arch A) (a : A), Steps (L := L) arI th s a σ s1 a σ1) := + dom_restart_for th (DomStepOps.native (L := L) th) hP s σ G hCR hFI hDR ct _hcur htcb hact hkf hka hksO hactK /-! ## FL4's steps -/ @@ -618,8 +748,8 @@ theorem schedStep_start (th : TypedHeap) (s1 : S) (σ1 : Fl4Spec.State Unit) {kb rw [hw4 _ b1 b2 b5 b6 b7, hw4 _ b3 b4 b8 b9 b10] exact hent1 i hi -/-- **DomainScheduleSetStart.** -/ -theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) +set_option maxHeartbeats 16000000 in +private theorem dom_start_for {trace : DomStepRel S} (arI : machine.Arch A) (th : TypedHeap) (steps : DomStepOps (L := L) th trace) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) @@ -630,9 +760,9 @@ theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed t (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 ct buf i = ok x) (extra : Aeneas.Std.Array obj.Slot 3#usize) (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) - (hRes : DomResHyp (L := L) th s σ ct) : + (hRes : DomResHypFor (L := L) th trace s σ ct) : domain.decode st arI s a fl4_abi.label.DomainScheduleSetStart length buf extra ⦃ res => - DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by + DomPostAFor (L := L) th trace σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by set kb := L.ksb s with hkbdef unfold domain.decode have n1 : ¬ fl4_abi.label.DomainScheduleSetStart = fl4_abi.label.DomainSetSet := by @@ -644,7 +774,7 @@ theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed t · rw [if_pos h1]; simp only [error.truncated, bind_ok] have : args = [] := List.eq_nil_of_length_eq_zero (by rw [← hlen]; scalar_tac) subst this - exact dom_err (dec_start_nil hg _) trivial + exact dom_err_for (trace := trace) (dec_start_nil hg _) trivial rw [if_neg h1] obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by match args with @@ -659,7 +789,7 @@ theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed t by_cases h2 : y0 ≥ 2#u64 · rw [if_pos h2] simp only [show (2#u64 - 1#u64 : Result U64) = ok 1#u64 from rfl, error.range, bind_ok] - refine dom_err (e' := .rangeError 0 (BitVec.ofNat 64 (2 - 1))) ?_ ⟨rfl, rfl⟩ + refine dom_err_for (trace := trace) (e' := .rangeError 0 (BitVec.ofNat 64 (2 - 1))) ?_ ⟨rfl, rfl⟩ rw [hmap, hspec, if_pos (by scalar_tac)] rw [if_neg h2] have hy : y0.val < 2 := by scalar_tac @@ -667,7 +797,7 @@ theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed t obtain ⟨hlist, hdlt, hult, hz⟩ := hent y0 hy by_cases h3 : L.mem s (ksW kb (durOff y0)) = 0#u64 · rw [if_pos h3]; simp only [error.invalid_arg, bind_ok] - refine dom_err (e' := .invalidArgument 0) ?_ rfl + refine dom_err_for (trace := trace) (e' := .invalidArgument 0) ?_ rfl rw [hmap, hspec, if_neg (by omega), if_pos ?_] rw [hlist, hz h3, h3]; rfl rw [if_neg h3] @@ -680,7 +810,7 @@ theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed t exact h3 (UScalar.eq_of_val_eq (e2.trans rfl)) -- `set_thread_state Restart` obtain ⟨s1, hs1, hS1, hkb1, σ1, hst1, hCR1, hFI1, hDR1, hdl1, hstR⟩ := - dom_restart th hP s σ G hCR hFI ⟨hl, hent, hstart, h24D⟩ ct hcur htcb hact hkf hka hksO hactK + dom_restart_for th steps hP s σ G hCR hFI ⟨hl, hent, hstart, h24D⟩ ct hcur htcb hact hkf hka hksO hactK rw [hs1] simp only [bind_ok, ks_set_eq, hkb1, L.ksb_wr, show (2#u64 - 2#u64 : Result U64) = ok 0#u64 from rfl, show layout.ks.DOM_SCHED_START = 32#u64 by unfold layout.ks.DOM_SCHED_START; rfl, @@ -721,12 +851,30 @@ theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed t have hR5 : StateRel th (L.mem s5) y.2 := (hSR _ hR4).of_caps (fun x hx => hS5.capAt hx) obtain ⟨hCR5, hFI5⟩ := crel_step hCR1 hFI1 (hS4.trans hS5) ((KSub.of_kheap rfl).trans hK) hR5 hcdt hio refine Or.inr ⟨rfl, .invokeDomainScheduleSetStart y0.val, by rw [hmap, hspec, if_neg (by omega), if_neg hne], σ1, by rw [hcur]; exact hst1, - y, mem_setStart hy5, hS1.trans (hS4.trans hS5), hCR5, hFI5, hkb5, ?_, fun hC A arI a => - ((hstR hC A arI a).trans arI (Steps.sched arI (schedStep_start th s1 σ1 y0 hkb1 hkf hka))).trans arI - (hss A arI a)⟩ + y, mem_setStart hy5, hS1.trans (hS4.trans hS5), hCR5, hFI5, hkb5, ?_, fun hC => + steps.trans (steps.trans (hstR hC) (steps.sched (schedStep_start th s1 σ1 y0 hkb1 hkf hka))) + hss⟩ rw [hkb5] exact hDR4.of_same hDS5 hdl hst5 +/-- **DomainScheduleSetStart.** -/ +theorem dom_start_refines (arI : machine.Arch A) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) + (slbl : Fl4Spec.Data) (hg : Fl4Spec.genInvocationType slbl = .domainScheduleSetStart) + (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 ct buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hRes : DomResHyp (L := L) th s σ ct) : + domain.decode st arI s a fl4_abi.label.DomainScheduleSetStart length buf extra ⦃ res => + DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := + dom_start_for arI th (DomStepOps.native (L := L) th) hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK + slbl hg length buf args hlen hargs extra excs hRes + theorem ucast_ofNat (w : Nat) (x : U64) : Fl4Spec.ucast w x.bv = BitVec.ofNat w x.val := by apply BitVec.eq_of_toNat_eq; simp [Fl4Spec.ucast, BitVec.toNat_setWidth] @@ -794,9 +942,8 @@ theorem schedStep_conf (th : TypedHeap) (s1 : S) (σ1 : Fl4Spec.State Unit) {kb mem_ks_ne _ _ hkf hka (by decide) (by decide) (by decide) (by decide) (by decide)] exact hs.domTime -/-- ScheduleConfigure's invocation: the entry written, with the CSpace parts, -`FinInv` and `DomRel`. -/ -theorem dom_conf_ok (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) +set_option maxHeartbeats 16000000 in +private theorem dom_conf_ok_for {trace : DomStepRel S} (th : TypedHeap) (steps : DomStepOps (L := L) th trace) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) (ct : U64) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) @@ -811,10 +958,10 @@ theorem dom_conf_ok (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4S let s1 ← sched.set_thread_state st s ct sched.TS_RESTART let s2 ← sched.set_dom_sched st s1 y0 y1 y2 ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a)) ⦃ res => - DomPostA (L := L) th σ G m s a res ⦄ := by + DomPostAFor (L := L) th trace σ G m s a res ⦄ := by set kb := L.ksb s with hkbdef obtain ⟨s1, hs1, hS1, hkb1, σ1, hst1, hCR1, hFI1, hDR1, -, hstR⟩ := - dom_restart th hP s σ G hCR hFI hDR ct hcur htcb hact hkf hka hksO hactK + dom_restart_for th steps hP s σ G hCR hFI hDR ct hcur htcb hact hkf hka hksO hactK rw [hs1]; simp only [bind_ok, set_dom_sched_eq, hkb1] rw [WP.spec_ok] refine ⟨rfl, ?_⟩ @@ -836,7 +983,28 @@ theorem dom_conf_ok (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4S by rw [hs2, L.ksb_wr, L.ksb_wr, hkb1], by show DomRel (L.mem s2) (L.ksb s2) σ2 rw [hs2, L.ksb_wr, L.ksb_wr, hkb1]; exact hDR2, - fun hC A arI a => (hstR hC A arI a).trans arI (Steps.sched arI (schedStep_conf th s1 σ1 y1 y2 hkb1 hkf hka h1 h2))⟩ + fun hC => steps.trans (hstR hC) (steps.sched (schedStep_conf th s1 σ1 y1 y2 hkb1 hkf hka h1 h2))⟩ + +/-- ScheduleConfigure's invocation: the entry written, with the CSpace parts, +`FinInv` and `DomRel`. -/ +theorem dom_conf_ok (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) + (m : Fl4Spec.SEMonad Unit Fl4Spec.DomainInvocation) (y0 y1 y2 : U64) (h0 : y0.val = 0) (h1 : y1.val = 0) + (h2 : y2.val < 2 ^ 56) + (hm : m.run σ = ({(.ok (.invokeDomainScheduleConfigure (Fl4Spec.unat y0.bv) + (Fl4Spec.ucast Fl4Spec.domainLen y1.bv) (Fl4Spec.ucast Fl4Spec.domainDurationLen y2.bv)), σ)}, False)) : + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let s2 ← sched.set_dom_sched st s1 y0 y1 y2 + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a)) ⦃ res => + DomPostA (L := L) th σ G m s a res ⦄ := + dom_conf_ok_for th (DomStepOps.native (L := L) th) hP s a σ G hCR hFI hDR ct hcur htcb hact hkf hka hksO hactK + m y0 y1 y2 h0 h1 h2 hm theorem maxdur_eq : domain.MAX_DURATION = ok ⟨BitVec.ofNat 64 (2 ^ 56 - 1)⟩ := by unfold domain.MAX_DURATION @@ -846,8 +1014,8 @@ theorem maxdur_eq : domain.MAX_DURATION = ok ⟨BitVec.ofNat 64 (2 ^ 56 - 1)⟩ rw [this] rfl -/-- **DomainScheduleConfigure.** -/ -theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) +set_option maxHeartbeats 16000000 in +private theorem dom_conf_for {trace : DomStepRel S} (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (steps : DomStepOps (L := L) th trace) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) @@ -860,7 +1028,7 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe (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) (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : domain.decode st arI s a fl4_abi.label.DomainScheduleConfigure length buf extra ⦃ res => - DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by + DomPostAFor (L := L) th trace σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by set kb := L.ksb s with hkbdef unfold domain.decode have n1 : ¬ fl4_abi.label.DomainScheduleConfigure = fl4_abi.label.DomainSetSet := by @@ -868,7 +1036,7 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe rw [hct]; simp only [bind_ok, n1, ↓reduceIte] by_cases h1 : length < 3#u64 · rw [if_pos h1]; simp only [error.truncated, bind_ok] - exact dom_err (dec_conf_short hg _ (by rw [List.length_map, ← hlen]; scalar_tac) _) trivial + exact dom_err_for (trace := trace) (dec_conf_short hg _ (by rw [List.length_map, ← hlen]; scalar_tac) _) trivial rw [if_neg h1] obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, args = y0 :: y1 :: y2 :: rest := by match args with @@ -887,14 +1055,14 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe by_cases c1 : y0 ≥ 1#u64 · rw [if_pos c1] simp only [show (2#u64 - 2#u64 : Result U64) = ok 0#u64 from rfl, error.range, bind_ok] - refine dom_err (e' := .rangeError 0 (BitVec.ofNat 64 (2 - 2))) ?_ ⟨rfl, rfl⟩ + refine dom_err_for (trace := trace) (e' := .rangeError 0 (BitVec.ofNat 64 (2 - 2))) ?_ ⟨rfl, rfl⟩ rw [hmap, hspec, if_pos (by scalar_tac)] rw [if_neg c1] have hy0 : y0.val = 0 := by scalar_tac by_cases c2 : y1 ≥ 1#u64 · rw [if_pos c2] simp only [show (1#u64 - 1#u64 : Result U64) = ok 0#u64 from rfl, error.range, bind_ok] - refine dom_err (e' := .rangeError 0 (BitVec.ofNat 64 (1 - 1))) ?_ ⟨rfl, rfl⟩ + refine dom_err_for (trace := trace) (e' := .rangeError 0 (BitVec.ofNat 64 (1 - 1))) ?_ ⟨rfl, rfl⟩ rw [hmap, hspec, if_neg (by omega), if_pos (by scalar_tac)] rw [if_neg c2] have hy1 : y1.val = 0 := by scalar_tac @@ -905,7 +1073,7 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe exact bv_gt_iff y2 _ (by decide) by_cases c3 : y2 > (⟨BitVec.ofNat 64 (2 ^ 56 - 1)⟩ : U64) · rw [if_pos c3]; simp only [error.invalid_arg, bind_ok] - refine dom_err (e' := .invalidArgument 2) ?_ rfl + refine dom_err_for (trace := trace) (e' := .invalidArgument 2) ?_ rfl have : 2 ^ 56 - 1 < y2.val := by have : (⟨BitVec.ofNat 64 (2 ^ 56 - 1)⟩ : U64).val = 2 ^ 56 - 1 := by show (BitVec.ofNat 64 (2 ^ 56 - 1)).toNat = _; simp @@ -923,9 +1091,9 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe let s1 ← sched.set_thread_state st s ct sched.TS_RESTART let s2 ← sched.set_dom_sched st s1 y0 y1 y2 ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a)) ⦃ res => - DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (List.map UScalar.bv (y0 :: y1 :: y2 :: rest)) + DomPostAFor (L := L) th trace σ G (Fl4Spec.decodeDomainInvocation slbl (List.map UScalar.bv (y0 :: y1 :: y2 :: rest)) excs) s a res ⦄ := fun hc5 => - dom_conf_ok th hP s a σ G hCR hFI ⟨hl, hent, hstart, h24D⟩ ct hcur htcb hact hkf hka hksO hactK _ y0 y1 y2 hy0 hy1 hy2 + dom_conf_ok_for th steps hP s a σ G hCR hFI ⟨hl, hent, hstart, h24D⟩ ct hcur htcb hact hkf hka hksO hactK _ y0 y1 y2 hy0 hy1 hy2 (by rw [hmap, hspec, if_neg (by omega), if_neg (by omega), if_neg nc3, if_neg nc4, if_neg hc5]; rfl) have hk2 : obj.ks_get st s layout.ks.DOM_SCHED_START = ok (L.mem s (ksW kb 32#u64)) := by rw [ks_get_eq, show layout.ks.DOM_SCHED_START = 32#u64 by unfold layout.ks.DOM_SCHED_START; rfl] @@ -938,7 +1106,7 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe have hz : y2.bv = 0 := by rw [c4]; rfl by_cases c5 : y0 = L.mem s (ksW kb 32#u64) · rw [if_pos c5, if_pos c4]; simp only [error.invalid_arg, bind_ok] - refine dom_err (e' := .invalidArgument 2) ?_ rfl + refine dom_err_for (trace := trace) (e' := .invalidArgument 2) ?_ rfl rw [hmap, hspec, if_neg (by omega), if_neg (by omega), if_neg nc3, if_neg nc4, if_pos ⟨hst.1 c5, hz⟩] · rw [if_neg c5]; exact hok (fun h => c5 (hst.2 h.1)) · rw [if_neg c4]; simp only [hk2, bind_ok] @@ -948,8 +1116,26 @@ theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHe · rw [if_pos c5, if_neg c4]; exact hok (fun h => hz h.2) · rw [if_neg c5]; exact hok (fun h => c5 (hst.2 h.1)) -/-- **DomainSetSet.** -/ -theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) +/-- **DomainScheduleConfigure.** -/ +theorem dom_conf_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) + (slbl : Fl4Spec.Data) (hg : Fl4Spec.genInvocationType slbl = .domainScheduleConfigure) + (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 ct buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + domain.decode st arI s a fl4_abi.label.DomainScheduleConfigure length buf extra ⦃ res => + DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := + dom_conf_for arI hcfg th (DomStepOps.native (L := L) th) hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK + slbl hg length buf args hlen hargs extra excs + +set_option maxHeartbeats 16000000 in +private theorem dom_set_for {trace : DomStepRel S} (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (steps : DomStepOps (L := L) th trace) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) @@ -962,9 +1148,9 @@ theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHea (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) (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraSRel th (L.mem s) x0 excs) - (hSet : DomSetHyp (L := L) th s σ ct) (hstall : DomStallHyp (st := st) arI th s a) : + (hSet : DomSetHypFor (L := L) th trace s σ ct) (hstall : DomStallHyp (st := st) arI th s a) : domain.decode st arI s a fl4_abi.label.DomainSetSet length buf extra ⦃ res => - DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by + DomPostAFor (L := L) th trace σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by set kb := L.ksb s with hkbdef unfold domain.decode rw [hct]; simp only [bind_ok, ↓reduceIte] @@ -972,7 +1158,7 @@ theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHea · rw [if_pos h1]; simp only [error.truncated, bind_ok] have : args = [] := List.eq_nil_of_length_eq_zero (by rw [← hlen, h1]; rfl) subst this - exact dom_err (dec_set_nil hg _) trivial + exact dom_err_for (trace := trace) (dec_set_nil hg _) trivial rw [if_neg h1] obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by match args with @@ -984,14 +1170,14 @@ theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHea rw [hmap] by_cases c1 : y0 ≥ 1#u64 · rw [if_pos c1]; simp only [error.invalid_arg, bind_ok] - exact dom_err (e' := .invalidArgument 0) (dec_set_big hg _ _ (by rw [hu0, hcfg.numDomains]; scalar_tac) _) rfl + exact dom_err_for (trace := trace) (e' := .invalidArgument 0) (dec_set_big hg _ _ (by rw [hu0, hcfg.numDomains]; scalar_tac) _) rfl rw [if_neg c1] have nc1 : ¬ Fl4Spec.unat y0.bv ≥ Fl4Spec.numDomains := by rw [hu0, hcfg.numDomains]; scalar_tac have hy0 : y0.val < 1 := by scalar_tac rw [hx0]; simp only [bind_ok, is_null_eq] rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, ps, rs, idxs, rest', hps, hls, hxe, rfl⟩ · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok] - exact dom_err (dec_set_noex hg _ _ nc1) trivial + exact dom_err_for (trace := trace) (dec_set_noex hg _ _ nc1) trivial simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, slot_cap_eq, capAt_capV, bind_ok] have hAs : AbsSlot th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ have hcv : CapValid th (capV (L.mem s) x0) := hCR.2.1.abs hAs (by rw [← hxe]; exact capAt_capV _ _) @@ -999,10 +1185,10 @@ theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHea have hother : (∀ p, c ≠ .Thread p) → (ok ((core.result.Result.Err (error.Exc.Sys (error.SysError.InvalidArgument 1#u64))), s, a) : Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => - DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (y0.bv :: rest.map UScalar.bv) + DomPostAFor (L := L) th trace σ G (Fl4Spec.decodeDomainInvocation slbl (y0.bv :: rest.map UScalar.bv) ((absCap c, (ps.bv, idxs)) :: rest')) s a res ⦄ := by intro hc - refine dom_err (dec_set_other hg _ _ nc1 _ (fun p e => ?_) _ _) rfl + refine dom_err_for (trace := trace) (dec_set_other hg _ _ nc1 _ (fun p e => ?_) _ _) rfl cases c <;> simp [absCap] at e exact hc _ rfl cases c with @@ -1014,7 +1200,7 @@ theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHea let s4 ← sched.set_domain st s3 ptr y0 ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s4, a1)) _ ?_ obtain ⟨s1, hs1, hS1, hkb1, σ1, hst1, hCR1, hFI1, hDR1, -, hstR⟩ := - dom_restart th hP s σ G hCR hFI hDR ct hcur htcb hact hkf hka hksO hactK + dom_restart_for th steps hP s σ G hCR hFI hDR ct hcur htcb hact hkf hka hksO hactK rw [hs1]; simp only [bind_ok] refine WP.spec_bind (hSet s1 σ1 ptr y0 hs1 hst1 hcv hy0) ?_ rintro s2 ⟨hS2, hkb2, hDS, y, hy, hK, hSR, hcdt, hio, hdl, hst, hss⟩ @@ -1024,24 +1210,40 @@ theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHea obtain ⟨hCR2, hFI2⟩ := crel_step hCR1 hFI1 hS2 hK hR2 hcdt hio refine Or.inr ⟨rfl, .invokeDomainSet ptr.bv (Fl4Spec.ucast Fl4Spec.domainLen y0.bv), dec_set_thread hg _ _ nc1 _ _ _, σ1, by rw [hcur]; exact hst1, y, hy, hS1.trans hS2, hCR2, hFI2, hkb2.trans hkb1, ?_, - fun hC A arI a => (hstR hC A arI a).trans arI (hss A arI a)⟩ + fun hC => steps.trans (hstR hC) hss⟩ show DomRel (L.mem s2) (L.ksb s2) y.2 rw [hkb2, hkb1] rw [hkb1] at hDS exact hDR1.of_same hDS hdl hst | _ => simp only [error.invalid_arg, bind_ok]; exact hother (by simp) +/-- **DomainSetSet.** -/ +theorem dom_set_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) + (slbl : Fl4Spec.Data) (hg : Fl4Spec.genInvocationType slbl = .domainSetSet) + (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 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) + (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraSRel th (L.mem s) x0 excs) + (hSet : DomSetHyp (L := L) th s σ ct) (hstall : DomStallHyp (st := st) arI th s a) : + domain.decode st arI s a fl4_abi.label.DomainSetSet length buf extra ⦃ res => + DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := + dom_set_for arI hcfg th (DomStepOps.native (L := L) th) hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK + slbl hg length buf args hlen hargs extra x0 hx0 excs hex hSet hstall + /-- The spec's domain label of an FL4 label. -/ def domLabelOf (lbl : U64) : Fl4Spec.GenInvocationLabels := if lbl.val = 37 then .domainSetSet else if lbl.val = 38 then .domainScheduleConfigure else if lbl.val = 39 then .domainScheduleSetStart else .invalidInvocation -/-- **FL4's `domain::decode` refines l4v's domain invocations, every -label**: `decode_domain_invocation`, `set_thread_state Restart` and -`invoke_domain`, with the CSpace relation, `FinInv` and `DomRel` after them. -The scheduler steps are `DomSetHyp` and `DomResHyp`; DomainSetSet's stall of -its target is `DomStallHyp`. -/ -theorem domain_decode_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) +set_option maxHeartbeats 16000000 in +private theorem domain_decode_for {trace : DomStepRel S} (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (steps : DomStepOps (L := L) th trace) (hP : Placed th) (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) @@ -1054,26 +1256,26 @@ theorem domain_decode_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : Ty (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) (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraSRel th (L.mem s) x0 excs) - (hSet : DomSetHyp (L := L) th s σ ct) (hRes : DomResHyp (L := L) th s σ ct) + (hSet : DomSetHypFor (L := L) th trace s σ ct) (hRes : DomResHypFor (L := L) th trace s σ ct) (hstall : DomStallHyp (st := st) arI th s a) : domain.decode st arI s a lbl length buf extra ⦃ res => - DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by + DomPostAFor (L := L) th trace σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := by by_cases h37 : lbl.val = 37 · have e : lbl = fl4_abi.label.DomainSetSet := UScalar.eq_of_val_eq (by rw [h37]; simp [fl4_abi.label.DomainSetSet]) subst e - exact dom_set_refines arI hcfg th hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK slbl + exact dom_set_for arI hcfg th steps hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK slbl (by rw [hlbl]; simp [domLabelOf, h37]) length buf args hlen hargs extra x0 hx0 excs hex hSet hstall by_cases h38 : lbl.val = 38 · have e : lbl = fl4_abi.label.DomainScheduleConfigure := UScalar.eq_of_val_eq (by rw [h38]; simp [fl4_abi.label.DomainScheduleConfigure]) subst e - exact dom_conf_refines arI hcfg th hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK slbl + exact dom_conf_for arI hcfg th steps hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK slbl (by rw [hlbl]; simp [domLabelOf, h38]) length buf args hlen hargs extra excs by_cases h39 : lbl.val = 39 · have e : lbl = fl4_abi.label.DomainScheduleSetStart := UScalar.eq_of_val_eq (by rw [h39]; simp [fl4_abi.label.DomainScheduleSetStart]) subst e - exact dom_start_refines arI th hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK slbl + exact dom_start_for arI th steps hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK slbl (by rw [hlbl]; simp [domLabelOf, h39]) length buf args hlen hargs extra excs hRes have hg : Fl4Spec.genInvocationType slbl = .invalidInvocation := by rw [hlbl]; simp [domLabelOf, h37, h38, h39] unfold domain.decode @@ -1083,7 +1285,54 @@ theorem domain_decode_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : Ty have n3 : ¬ lbl = fl4_abi.label.DomainScheduleSetStart := by rw [u64_eq_iff]; simpa [fl4_abi.label.DomainScheduleSetStart] using h39 rw [hct]; simp only [bind_ok, n1, n2, n3, ↓reduceIte, error.illegal] - exact dom_err (dec_dom_other _ _ (by rw [hg]; decide) (by rw [hg]; decide) (by rw [hg]; decide)) trivial + exact dom_err_for (trace := trace) (dec_dom_other _ _ (by rw [hg]; decide) (by rw [hg]; decide) (by rw [hg]; decide)) trivial + +/-- **FL4's `domain::decode` refines l4v's domain invocations, every +label**: `decode_domain_invocation`, `set_thread_state Restart` and +`invoke_domain`, with the CSpace relation, `FinInv` and `DomRel` after them. +The scheduler steps are `DomSetHyp` and `DomResHyp`; DomainSetSet's stall of +its target is `DomStallHyp`. -/ +theorem domain_decode_refines (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) + (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) + (lbl : U64) (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = domLabelOf 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 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) + (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraSRel th (L.mem s) x0 excs) + (hSet : DomSetHyp (L := L) th s σ ct) (hRes : DomResHyp (L := L) th s σ ct) + (hstall : DomStallHyp (st := st) arI th s a) : + domain.decode st arI s a lbl length buf extra ⦃ res => + DomPostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := + domain_decode_for arI hcfg th (DomStepOps.native (L := L) th) hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK + lbl slbl hlbl length buf args hlen hargs extra x0 hx0 excs hex hSet hRes hstall + + +/-- The Domain decoder with the reached restart and scheduler trace. -/ +theorem domain_decode_trace (arI : machine.Arch A) (hcfg : DomConfig) (th : TypedHeap) (hP : Placed th) (s : S) (a : A) + (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) (hDR : DomRel (L.mem s) (L.ksb s) σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64) (hka : (L.ksb s).val % 8 = 0) + (hksO : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects th (ksW (L.ksb s) o)) + (hactK : ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val) + (lbl : U64) (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = domLabelOf 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 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) + (excs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraSRel th (L.mem s) x0 excs) + (hSet : DomSetTraceHyp (L := L) th s σ ct) (hRes : DomResTraceHyp (L := L) th s σ ct) + (hstall : DomStallHyp (st := st) arI th s a) : + domain.decode st arI s a lbl length buf extra ⦃ res => + DomTracePostA (L := L) th σ G (Fl4Spec.decodeDomainInvocation slbl (args.map UScalar.bv) excs) s a res ⦄ := + domain_decode_for arI hcfg th (DomStepOps.restricted (L := L) th) hP s a σ G hCR hFI hDR ct hct hcur htcb hact hkf hka hksO hactK + lbl slbl hlbl length buf args hlen hargs extra x0 hx0 excs hex hSet hRes hstall end diff --git a/proofs/Fl4Proofs/P3/DomFb.lean b/proofs/Fl4Proofs/P3/DomFb.lean index b11baaa98..c7cef8780 100644 --- a/proofs/Fl4Proofs/P3/DomFb.lean +++ b/proofs/Fl4Proofs/P3/DomFb.lean @@ -114,12 +114,12 @@ theorem dke_fbCode {α : Type} {m : SEMonad Unit α} (h : DKE m) : DKE (fbCode ( macro_rules | `(tactic| dk_leaf) => `(tactic| with_reducible refine dke_fbCode ?_) theorem dke_fbDelete (sl : CslotPtr) : DKE (fbDelete (Z := Unit) sl) := by - unfold fbDelete; exact dke_handleE (dk_capDelete sl) fun _ => dke_throw _ + unfold fbDelete fbDeleteWith; exact dke_handleE (dk_capDelete sl) fun _ => dke_throw _ macro_rules | `(tactic| dk_leaf) => `(tactic| with_reducible exact dke_fbDelete _) /-- One batch entry. -/ theorem dk_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : DKE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; dk_auto + unfold fbEntry fbEntryWith fbDeferDelete; dk_auto /-- The batch. -/ theorem dk_performFrameBatch (b : Fl4FrameBatchInvocation) : DK (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/DomSched.lean b/proofs/Fl4Proofs/P3/DomSched.lean index a6fe73ca7..4308d4cab 100644 --- a/proofs/Fl4Proofs/P3/DomSched.lean +++ b/proofs/Fl4Proofs/P3/DomSched.lean @@ -17,6 +17,7 @@ P3 (DESIGN.md §9), domains: **the domain cap at the composite relation** arch hook a no-op), given `ActReady`. * `decode_invocation_domainXT`: `DomXHyp` with both discharged. -/ +import Fl4Proofs.P3.DomDecode import Fl4Proofs.P3.XTopCaps import Fl4Proofs.P3.IpcSchedLift import Fl4Proofs.P1.QueueSlots @@ -344,10 +345,10 @@ set_option maxHeartbeats 4000000 in /-- **`DomResHyp` discharged** at `XTopAt`: `reschedule_required` after the start's writes refines the spec's (`reschedule_required_refines`, given `ActReady`), as scheduler steps. -/ -theorem domResHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : A} {σ : Fl4Spec.State Unit} +theorem domResTraceHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : A} {σ : Fl4Spec.State Unit} (hX : XTopAt (L := L) AR Z s ar σ) (hKA : KsApart Z.N Z.T Z.E Z.R Z.B (L.ksb s)) (hTh : ∀ t, Z.th t = some .tcb → t ∈ Z.T) (hC : CurRun σ) (hA : Notif.ActReady σ) {ct : U64} - (hcur : σ.curThread = ct.bv) : DomResHyp (L := L) Z.th s σ ct := by + (hcur : σ.curThread = ct.bv) : DomResTraceHyp (L := L) Z.th s σ ct := by intro s1 σ1 index hs1 hst hidx obtain ⟨-, x, hk⟩ := hX.curTcb hcur have hrun : Fl4Spec.runnable x.tcbState = true := hC x (by rw [hcur]; exact hk) @@ -372,10 +373,23 @@ theorem domResHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : subst e refine ⟨hX4.slotsSame_of hw fun b hb => sfootI_oobj (hS4.lists.choose_spec.choose_spec.1.tcbs) hb, hw.1.trans hk4, ?_, ((), σ'), by rw [hres]; rfl, KSub.of_kheap hK'.kh, fun m h => h.of_kheap hK'.kh, - hK'.cdt, hK'.orig, hdl', hds', fun A arI a => Steps.sched arI (schedStep_resched hA4 hrun5 hres hK' hdl' hds')⟩ + hK'.cdt, hK'.orig, hdl', hds', DomainTrace.sched (schedStep_resched hA4 hrun5 hres hK' hdl' hds')⟩ have := domSame_of_sfootI hS4 hw rwa [hk4] at this +theorem DomResTraceHyp.toDomResHyp {th : TypedHeap} {s : S} {σ : Fl4Spec.State Unit} {ct : U64} + (h : DomResTraceHyp (L := L) th s σ ct) : DomResHyp (L := L) th s σ ct := by + intro s1 σ1 index hr hs hi + refine WP.spec_mono (h s1 σ1 index hr hs hi) ?_ + rintro s2 ⟨hslots, hkb, hds, y, hy, hk, hsr, hcdt, hio, hdl, hsi, tr⟩ + exact ⟨hslots, hkb, hds, y, hy, hk, hsr, hcdt, hio, hdl, hsi, fun _ arI a => tr.toSteps arI a⟩ + +theorem domResHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (hX : XTopAt (L := L) AR Z s ar σ) (hKA : KsApart Z.N Z.T Z.E Z.R Z.B (L.ksb s)) + (hTh : ∀ t, Z.th t = some .tcb → t ∈ Z.T) (hC : CurRun σ) (hA : Notif.ActReady σ) {ct : U64} + (hcur : σ.curThread = ct.bv) : DomResHyp (L := L) Z.th s σ ct := + (domResTraceHyp_of hX hKA hTh hC hA hcur).toDomResHyp + end section @@ -523,9 +537,9 @@ the domain word, `enqueue` when schedulable, `reschedule_required` for the current thread) refines the spec's `invoke_set_domain` (the arch hook a no-op, the domain already 0: one domain) as scheduler steps, given `ActReady`. -/ -theorem domSetHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : A} {σ : Fl4Spec.State Unit} +theorem domSetTraceHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : A} {σ : Fl4Spec.State Unit} (hX : XTopAt (L := L) AR Z s ar σ) (hTh : ∀ t, Z.th t = some .tcb → t ∈ Z.T) (hC : CurRun σ) - (hA : Notif.ActReady σ) {ct : U64} (hcur : σ.curThread = ct.bv) : DomSetHyp (L := L) Z.th s σ ct := by + (hA : Notif.ActReady σ) {ct : U64} (hcur : σ.curThread = ct.bv) : DomSetTraceHyp (L := L) Z.th s σ ct := by intro s1 σ1 t d hs1 hst htcb hd have hd0 : d = 0#u64 := UScalar.eq_of_val_eq (by have : (0#u64 : U64).val = 0 := rfl; omega) subst hd0 @@ -605,15 +619,14 @@ theorem domSetHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : KSub σ1 y.2 ∧ (∀ m, StateRel Z.th m σ1 → StateRel Z.th m y.2) ∧ y.2.cdt = σ1.cdt ∧ y.2.isOriginalCap = σ1.isOriginalCap ∧ y.2.domainList = σ1.domainList ∧ y.2.domainStartIndex = σ1.domainStartIndex ∧ - ∀ (A : Type) (arI : machine.Arch A) (a : A), Steps (L := L) arI Z.th s1 a σ1 s2 a y.2 := by + DomainTrace (L := L) Z.th s1 σ1 s2 y.2 := by intro s2 σ2 hw2 hst2 hK2 hdl2 hds2 hm2 have hwall : P1.WritesIn st s1 s2 (Sched.SFootI st Z.T Z.c s1) := Sched.WritesIn.sfootI_trans hwsc hw2 have hK12 := hKac.trans hK2 refine ⟨hX1.slotsSame_of hwall fun b hb => sfootI_oobj hT hb, hwall.1, domSame_of_sfootI hS1 hwall, ((), σ2), ?_, KSub.of_kheap hK12.kh, fun m h => h.of_kheap hK12.kh, hK12.cdt, hK12.orig, - by rw [hdl2, hdlc]; rfl, by rw [hds2, hdsc]; rfl, fun A arI a => - (((Steps.sched arI hsta).trans arI (Steps.sched arI (same_schedStep hsame))).trans arI - (Steps.sched arI hstc)).trans arI (Steps.sched arI hst2)⟩ + by rw [hdl2, hdlc]; rfl, by rw [hds2, hdsc]; rfl, (((DomainTrace.sched hsta).trans (DomainTrace.sched (same_schedStep hsame))).trans + (DomainTrace.sched hstc)).trans (DomainTrace.sched hst2)⟩ unfold Fl4Spec.invokeSetDomain Fl4Spec.archPrepareSetDomain Fl4Spec.setDomain refine mem_bind_nd (x := ((), σ1)) (mem_bind_nd (x := (σ1.curDomain, σ1)) rfl (mem_whenP (fun h => ?_) (fun _ => rfl))) ?_ @@ -639,6 +652,18 @@ theorem domSetHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : rw [hct1] at h exact absurd (P1.eq_of_bv h) heq) (fun _ => rfl)) +theorem DomSetTraceHyp.toDomSetHyp {th : TypedHeap} {s : S} {σ : Fl4Spec.State Unit} {ct : U64} + (h : DomSetTraceHyp (L := L) th s σ ct) : DomSetHyp (L := L) th s σ ct := by + intro s1 σ1 t d hr hs ht hd + refine WP.spec_mono (h s1 σ1 t d hr hs ht hd) ?_ + rintro s2 ⟨hslots, hkb, hds, y, hy, hk, hsr, hcdt, hio, hdl, hsi, tr⟩ + exact ⟨hslots, hkb, hds, y, hy, hk, hsr, hcdt, hio, hdl, hsi, fun _ arI a => tr.toSteps arI a⟩ + +theorem domSetHyp_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (hX : XTopAt (L := L) AR Z s ar σ) (hTh : ∀ t, Z.th t = some .tcb → t ∈ Z.T) (hC : CurRun σ) + (hA : Notif.ActReady σ) {ct : U64} (hcur : σ.curThread = ct.bv) : DomSetHyp (L := L) Z.th s σ ct := + (domSetTraceHyp_of hX hTh hC hA hcur).toDomSetHyp + /-- **`DomXHyp` for the domain cap, discharged**: `decode_invocation_domainX` with `DomSetHyp` and `DomResHyp` from `XTopAt` (`domSetHyp_of`, `domResHyp_of`), at the CSpace relation of `XTopAt`; beyond the decode's diff --git a/proofs/Fl4Proofs/P3/HandleTopG.lean b/proofs/Fl4Proofs/P3/HandleTopG.lean index ad9161d19..843f83750 100644 --- a/proofs/Fl4Proofs/P3/HandleTopG.lean +++ b/proofs/Fl4Proofs/P3/HandleTopG.lean @@ -285,9 +285,8 @@ theorem ifP_X {X XP : S → A → Fl4Spec.State Unit → Prop} {r : core.result. /-! ## The composition -/ -/-- **`handle_invocation` refines `hiG dec perf rep`, keeping `X` (`XP` if preempted)**: XTopJointEntry's -`handle_invocation_refinesMXP` over the spec's decoder and dispatcher (module docstring). -/ -theorem handle_invocation_refinesMXPG +/-- The decoder receives the successful lookup equation for the invoked cap and slot. -/ +theorem handle_invocation_refinesMXPG_atLookup (dec : Bool → Fl4Spec.Data → List Fl4Spec.Data → Fl4Spec.CapRef → Fl4Spec.CslotPtr → Fl4Spec.Cap → List (Fl4Spec.Cap × Fl4Spec.CslotPtr) → Option Fl4Spec.ObjRef → Fl4Spec.SEMonad Unit ι) (perf : Bool → Bool → Bool → ι → Fl4Spec.PMonad Unit (List Fl4Spec.Data)) @@ -337,7 +336,9 @@ theorem handle_invocation_refinesMXPG (hDec : ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ th p = some (.cnode sl.2.length)) → capAt (L.mem s) (cslotAddr sl) = ok c → - Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → r2 = .Ok e → er = .ok exs → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → + (Fl4Spec.lookupCapAndSlot (Z := Unit) ct.bv (Fl4Spec.toBl cptr.bv)).run σ = + ({(.ok (absCap c, sl), σ)}, False) → r2 = .Ok e → er = .ok exs → XCapPG (L := L) arI dec perf X XP s ar σ info.label (msgLen info buf) (cslotAddr sl) c blocking is_call can_donate first_phase buf e mi.miLabel args (Fl4Spec.toBl cptr.bv) sl exs buffer ct) : syscall.handle_invocation st arI s ar is_call blocking can_donate first_phase cptr ⦃ res => @@ -393,7 +394,7 @@ theorem handle_invocation_refinesMXPG have hmF := hiMF_ok (mi := mi) hl hbuffer her have hmE := hiMEG_run (dec := dec) (cptr := Fl4Spec.toBl cptr.bv) (fp := first_phase) (a := absCap c) (sl := sl) (exs := exs) hmrs - rcases hDec sl c ex exs ⟨p, hp, hth⟩ hc hget rfl rfl with hD | hD + rcases hDec sl c ex exs ⟨p, hp, hth⟩ hc hget hl rfl rfl with hD | hD swap · refine WP.spec_bind hD ?_ rintro ⟨r3, s1, a1⟩ (⟨e3, he3, hs1, ⟨e', hdec, herr⟩, hXe⟩ | ⟨hok3, hne, inv, hdec, σ1, hst, rr, σy, hy, z, hz, hRT, hXz⟩) @@ -461,6 +462,68 @@ theorem handle_invocation_refinesMXPG exact ⟨(.ok z.1, z.2), hi_memG dec perf rep hcur hmi (mem_syscall_fault hmF (mem_when (fun _ => hz) (fun h => by cases h))), Or.inl ⟨rfl, rfl⟩, hRT, ifP_ok rfl hXz⟩ +/-- **`handle_invocation` refines `hiG dec perf rep`, keeping `X` (`XP` if preempted)**: XTopJointEntry's +`handle_invocation_refinesMXP` over the spec's decoder and dispatcher (module docstring). -/ +theorem handle_invocation_refinesMXPG + (dec : Bool → Fl4Spec.Data → List Fl4Spec.Data → Fl4Spec.CapRef → Fl4Spec.CslotPtr → Fl4Spec.Cap → + List (Fl4Spec.Cap × Fl4Spec.CslotPtr) → Option Fl4Spec.ObjRef → Fl4Spec.SEMonad Unit ι) + (perf : Bool → Bool → Bool → ι → Fl4Spec.PMonad Unit (List Fl4Spec.Data)) + (X XP : S → A → Fl4Spec.State Unit → Prop) (rep : Bool) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (th : TypedHeap) (G : MdbGhost) + (hP : Placed th) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) + (hDR : DomRel (L.mem s) (L.ksb s) σ) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (hX0 : X s ar σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) + -- the front end's reads (`HIRead`) + (info : msginfo.MessageInfo) (hinfo : msg.msg_info st s ct = ok info) + (mi : Fl4Spec.MessageInfo) (hmi : Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False)) + (buf : U64) (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) + (buffer : Option Fl4Spec.ObjRef) + (hbuffer : Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False)) + (r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault) + (hr2 : ipc.lookup_extra_caps st s ct buf info = ok r2) + (er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))) + (her : (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False)) + (hrr : (∃ e exs, r2 = .Ok e ∧ er = .ok exs) ∨ + (∃ f f', r2 = .Err f ∧ er = .error f' ∧ (blocking = true → faults.handle_fault st arI s ar ct f ⦃ r => + ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, RelTopM (L := L) r.1 z.2 ∧ X r.1 r.2 z.2 ⦄))) + (args : List Fl4Spec.Data) (hmrs : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False)) + -- the fault of a failed lookup + (hFL : blocking = true → ∀ lf lf', LookupFaultRel lf lf' → + faults.handle_fault st arI s ar ct (.Cap cptr false lf) ⦃ r => + ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv (.capFault (Fl4Spec.ofBl (Fl4Spec.toBl cptr.bv)) false lf') σ).1, + RelTopM (L := L) r.1 z.2 ∧ X r.1 r.2 z.2 ⦄) + -- the success tail (from `X` with the invoker still current) and the error reply (at the entry state) + (hRepS : ∀ (s1 : S) (a1 : A) (σ' : Fl4Spec.State Unit), RelTopM (L := L) s1 σ' → X s1 a1 σ' → + σ'.curThread = ct.bv → flTail (st := st) ct is_call s1 a1 ⦃ res => res.1 = .Ok () ∧ + ∃ z ∈ (specTail is_call ct.bv [] σ').1, RelTopM (L := L) res.2.1 z.2 ∧ X res.2.1 res.2.2 z.2 ⦄) + (hRepF : ∀ (a1 : A) (e : error.SysError) (e' : Fl4Spec.SyscallError), X s a1 σ → ErrRel (.Sys e) e' → + ((if is_call then do + let s2 ← syscall.reply_error st s ct e + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a1) + else ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s, a1)) : + Result ((core.result.Result Unit error.Exc) × S × A)) ⦃ res => + res.1 = .Ok () ∧ ∃ z ∈ ((Fl4Spec.NonDet.when is_call <| + Fl4Spec.fl4ReplyFromKernelError rep ct.bv (Fl4Spec.msgFromSyscallError e')) σ).1, + RelTopM (L := L) res.2.1 z.2 ∧ X res.2.1 res.2.2 z.2 ⦄) + -- the invocation keeps the current thread + (hPK : ∀ inv σ1 y, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + y ∈ ((perf blocking is_call can_donate inv).run σ1).1 → + y.2.curThread = ct.bv) + -- the decode and invocation of the cap + (hDec : ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ th p = some (.cnode sl.2.length)) → + capAt (L.mem s) (cslotAddr sl) = ok c → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → r2 = .Ok e → er = .ok exs → + XCapPG (L := L) arI dec perf X XP s ar σ info.label (msgLen info buf) (cslotAddr sl) c blocking is_call + can_donate first_phase buf e mi.miLabel args (Fl4Spec.toBl cptr.bv) sl exs buffer ct) : + syscall.handle_invocation st arI s ar is_call blocking can_donate first_phase cptr ⦃ res => + HIPostMXPG (L := L) dec perf X XP rep σ is_call blocking can_donate first_phase (Fl4Spec.toBl cptr.bv) res ⦄ := + handle_invocation_refinesMXPG_atLookup arI dec perf X XP rep s ar σ th G hP hCR hFI hDR hFR hX0 + ct hct hcur htcb is_call blocking can_donate first_phase cptr info hinfo mi hmi buf hbuf buffer + hbuffer r2 hr2 er her hrr args hmrs hFL hRepS hRepF hPK + (fun sl c e exs hsl hc hget _ he hexs => hDec sl c e exs hsl hc hget he hexs) + end end diff --git a/proofs/Fl4Proofs/P3/IoControl.lean b/proofs/Fl4Proofs/P3/IoControl.lean index 6a61db4fe..fb5dfd2e8 100644 --- a/proofs/Fl4Proofs/P3/IoControl.lean +++ b/proofs/Fl4Proofs/P3/IoControl.lean @@ -189,6 +189,55 @@ def IoCPost (th : TypedHeap) (kb : U64) (σ : Fl4Spec.State Unit) (m : Fl4Spec.S IoPortsAlloc (L.mem s') kb G' ∧ MdbOKV kb (L.mem s') G' ∧ (DomPlaced (L := L) th s → DK4 (L := L) s s') ∧ (CurRun σ → Steps (L := L) arI th s ar σ s' a' σ') + +/-- The actual restart, port bitmap update, and fresh capability insertion +of a successful IOPortControl invocation. -/ +def IoIssueAt (th : TypedHeap) (G : MdbGhost) (s : S) (a : A) (σ : Fl4Spec.State Unit) + (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) (G' : MdbGhost) : Prop := + a' = a ∧ ∃ (ct : U64) (s1 : S) (σ1 : Fl4Spec.State Unit) (low high : U64) (s2 : S) + (ps pd : U64) (rs rd : Nat) (idxs idxd : List Bool), + sched.cur_thread st s = ok ct ∧ + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 ∧ + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + low.val ≤ high.val ∧ high.val < 65536 ∧ + MaskPost (L := L) (L.ksb s1) low high true s1 s2 ∧ + let σ2 := ioMaskSt (BitVec.ofNat 16 low.val) (BitVec.ofNat 16 high.val) true σ1 + let c : capability.Cap := .IoPort low high 0#u64 + th ps = some (.cnode rs) ∧ th pd = some (.cnode rd) ∧ idxd.length = rd ∧ + Fl4Spec.capInsert (Z := Unit) (absCap c) (ps.bv, idxs) (pd.bv, idxd) σ2 = ({((), σ')}, False) ∧ + (∀ slot, IsSlot th slot → capV (L.mem s') slot = + if slot = cslotAddr (pd.bv, idxd) then c else capV (L.mem s2) slot) ∧ + capAt (L.mem s2) (cslotAddr (pd.bv, idxd)) = ok .Null ∧ + Vs.SlotFrame2 (L := L) th s2 s' ∧ + DestLive th (L.mem s2) (L.ksb s2) pd ∧ + (DelInv th (L.mem s2) → DelInv th (L.mem s')) ∧ + MdbStep (L.mem s2) G (L.mem s') G' + +def IoCPostTrace (th : TypedHeap) (G : MdbGhost) (kb : U64) (σ : Fl4Spec.State Unit) (m : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation) + (s : S) (ar : A) : core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ (∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∧ a' = ar + | (.Ok (), s', a') => ∃ inv, m.run σ = ({(.ok inv, σ)}, False) ∧ ∃ σ1, + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ' G', (.ok [], σ') ∈ ((Fl4Spec.archPerformInvocation (Z := Unit) inv).run σ1).1 ∧ + CRel th (L.mem s') σ' G' ∧ IoRel (L.mem s') kb σ' ∧ FinInv th (L.mem s') σ' G' ∧ + IoPortsAlloc (L.mem s') kb G' ∧ MdbOKV kb (L.mem s') G' ∧ (DomPlaced (L := L) th s → DK4 (L := L) s s') ∧ + (CurRun σ → Steps (L := L) arI th s ar σ s' a' σ') ∧ IoIssueAt (L := L) th G s ar σ s' a' σ' G' + + +variable {arI} in +theorem IoCPostTrace.toIoCPost {th : TypedHeap} {G : MdbGhost} {kb : U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {res : core.result.Result Unit error.Exc × S × A} {m : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} + (h : IoCPostTrace (L := L) arI th G kb σ m s a res) : + IoCPost (L := L) arI th kb σ m s a res := by + rcases res with ⟨res, s', a'⟩ + cases res with + | Err e => exact h + | Ok u => + cases u + obtain ⟨i, hd, σ1, hs, σ', G', hp, hc, hi, hf, ha, hm, hdk, hstep, _⟩ := h + exact ⟨i, hd, σ1, hs, σ', G', hp, hc, hi, hf, ha, hm, hdk, hstep⟩ + + variable {arI} in theorem IoCPost.of_run {th : TypedHeap} {kb : U64} {σ : Fl4Spec.State Unit} {m m' : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} (h : m.run σ = m'.run σ) {s : S} {ar : A} @@ -283,6 +332,101 @@ theorem io_target_tail (th : TypedHeap) (kb : U64) refine IoCPost.congr (run_bind_ok hee _) ?_ exact hK dsl pd hpd1 hpd hpdr hcd + +variable {arI} in +theorem IoCPostTrace.of_run {th : TypedHeap} {G : MdbGhost} {kb : U64} {σ : Fl4Spec.State Unit} + {m m' : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} (h : m.run σ = m'.run σ) {s : S} {ar : A} + {res : core.result.Result Unit error.Exc × S × A} (hq : IoCPostTrace (L := L) arI th G kb σ m' s ar res) : + IoCPostTrace (L := L) arI th G kb σ m s ar res := by + obtain ⟨r, s', a'⟩ := res + cases r with + | Err e => obtain ⟨h1, ⟨e', h2, h3⟩, h4⟩ := hq; exact ⟨h1, ⟨e', h.trans h2, h3⟩, h4⟩ + | Ok u => obtain ⟨inv, h2, h3⟩ := hq; exact ⟨inv, h.trans h2, h3⟩ + +variable {arI} in +theorem IoCPostTrace.congr {th : TypedHeap} {G : MdbGhost} {kb : U64} {σ : Fl4Spec.State Unit} + {m m' : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} (h : m.run σ = m'.run σ) {s : S} {ar : A} + {prog : Result (core.result.Result Unit error.Exc × S × A)} + (hp : prog ⦃ res => IoCPostTrace (L := L) arI th G kb σ m' s ar res ⦄) : prog ⦃ res => IoCPostTrace (L := L) arI th G kb σ m s ar res ⦄ := + WP.spec_mono hp (fun _ hq => IoCPostTrace.of_run h hq) + +variable {arI} in +theorem IoCPostTrace.err {th : TypedHeap} {G : MdbGhost} {kb : U64} {σ : Fl4Spec.State Unit} + {m : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} {s : S} {ar : A} {e : error.Exc} {e' : Fl4Spec.SyscallError} + (h : m.run σ = ({(.error e', σ)}, False)) (he : ErrRel e e') : + (ok ((.Err e, s, ar) : core.result.Result Unit error.Exc × S × A)) ⦃ res => + IoCPostTrace (L := L) arI th G kb σ m s ar res ⦄ := by + rw [WP.spec_ok]; exact ⟨rfl, ⟨e', h, he⟩, rfl⟩ + +/-- `target_tail` for `IoCPostTrace`. -/ +theorem io_target_tail_trace (th : TypedHeap) (G : MdbGhost) (kb : U64) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) (hR : StateRel th (L.mem s) σ) (hV : SlotsValid th (L.mem s)) + (c : capability.Cap) (ac : Fl4Spec.Cap) (hca : CapRel c ac) (hcv : CapValid th c) (idx dep : U64) + (ks : Fl4Spec.CslotPtr → Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation) + (K : obj.Slot → Result (core.result.Result Unit error.Exc × S × A)) + (hK : ∀ dsl pd, dsl.1 = pd.bv → th pd = some (.cnode dsl.2.length) → CnRef th (L.mem s) c pd → + capAt (L.mem s) (cslotAddr dsl) = ok .Null → + K (cslotAddr dsl) ⦃ res => IoCPostTrace (L := L) arI th G kb σ (ks dsl) s ar res ⦄) : + (do + let r ← cspace.lookup_target_slot st s c idx dep + 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, ar) + | 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, ar)) ⦃ res => + IoCPostTrace (L := L) arI th G kb σ (Fl4Spec.lookupTargetSlot ac (Fl4Spec.dataToCptr idx.bv) (Fl4Spec.unat dep.bv) >>= + fun d => Fl4Spec.ensureEmpty d >>= fun _ => ks d) s ar res ⦄ := by + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_exists + (lookup_slot_for_cnode_op_refines (Z := Unit) th s σ hR hV false c ac hca hcv idx dep) + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + have hlt : (Fl4Spec.lookupTargetSlot (Z := Unit) ac (Fl4Spec.dataToCptr idx.bv) (Fl4Spec.unat dep.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_traitFromResidualResult.from_residual, + core.convert.FromSame, core.convert.FromSame.from, bind_ok, bind_tc_ok] + exact IoCPostTrace.err (run_bind_error hlt _) (Or.inl hrel1) + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hpdr⟩ := hrel1 + obtain ⟨cd, ad, hcd, hcad, hgd⟩ := hR.slot_cap (Z := Unit) hpd1 hpd + unfold cdt.ensure_empty_slot + simp only [slot_cap_eq, hcd, bind_ok] + have hee := ensureEmpty_run dsl σ hgd + refine IoCPostTrace.congr (run_bind_ok hlt _) ?_ + by_cases hdn : cd = .Null + swap + · rw [cap_is_null_ne 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 : ad ≠ .nullCap := fun h => hdn ((hcad.null_iff).1 h) + simp only [this, ↓reduceIte] at hee + exact IoCPostTrace.err (run_bind_error hee _) (Or.inl trivial) + subst hdn + have hadn : ad = .nullCap := (hcad.null_iff).2 rfl + simp only [hadn, ↓reduceIte] at hee + rw [cap_is_null_null] + simp only [bind_ok, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch] + refine IoCPostTrace.congr (run_bind_ok hee _) ?_ + exact hK dsl pd hpd1 hpd hpdr hcd + theorem MaskPost.slotsSame {th : TypedHeap} (hP : Placed th) {kb : U64} (hL : IoLayout th kb) {s s' : S} {low high : U64} {set : Bool} (h : MaskPost (L := L) kb low high set s s') : SlotsSame th (L.mem s) (L.mem s') := io_frame_slotsSame hP hL h.2.2.1 @@ -329,7 +473,7 @@ theorem restart_io (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.State simp [fl4_abi.label.X86IOPortControlIssue] /-- **IOPortControl `Issue`** (label 63). -/ -theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) +theorem ioport_control_refines_trace (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hL : IoLayout th (L.ksb s)) (hIo : IoRel (L.mem s) (L.ksb s) σ) (hPA : IoPortsAlloc (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.invocationType slbl = .archInvocationLabel .x64IOPortControlIssue) @@ -345,7 +489,7 @@ theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IoPortControl) (ac : Fl4Spec.ArchCap) : ioport.decode_control st arI s ar fl4_abi.label.X86IOPortControlIssue length (cslotAddr (ps.bv, idxs)) buf extra ⦃ res => - IoCPost (L := L) arI th (L.ksb s) σ + IoCPostTrace (L := L) arI th G (L.ksb s) σ (Fl4Spec.decodeIoportControlInvocation slbl (args.map UScalar.bv) (ps.bv, idxs) ac excaps) s ar res ⦄ := by obtain ⟨hR, hV, hM, hC, hI⟩ := hCR unfold ioport.decode_control @@ -355,13 +499,13 @@ theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A by_cases h4 : length < 4#u64 · rw [if_pos h4]; simp only [error.truncated, bind_ok] rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 4 ∧ excaps.length ≥ 1) by simp; scalar_tac)] - exact IoCPost.err (e := .Sys .TruncatedMessage) (e' := .truncatedMessage) rfl (Or.inl trivial) + exact IoCPostTrace.err (e := .Sys .TruncatedMessage) (e' := .truncatedMessage) rfl (Or.inl trivial) rw [if_neg h4] rw [hx0]; simp only [bind_ok, is_null_eq] rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, c0, ac0, erest, hc0, hca0, hcv0, rfl⟩ · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok] rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 4 ∧ ([] : List Fl4Spec.Cap).length ≥ 1) by simp)] - exact IoCPost.err (e := .Sys .TruncatedMessage) (e' := .truncatedMessage) rfl (Or.inl trivial) + exact IoCPostTrace.err (e := .Sys .TruncatedMessage) (e' := .truncatedMessage) rfl (Or.inl trivial) simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte] obtain ⟨y0, y1, y2, y3, rest, rfl⟩ : ∃ y0 y1 y2 y3 rest, args = y0 :: y1 :: y2 :: y3 :: rest := by match args with @@ -396,18 +540,18 @@ theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A · rw [if_pos hlt]; simp only [error.invalid_arg, bind_ok] rw [whenE_pos (by rw [GT.gt, BitVec.lt_def, BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hfv, Nat.mod_eq_of_lt hlv]; scalar_tac)] - exact IoCPost.err (e := .Sys (.InvalidArgument 1#u64)) (e' := .invalidArgument 1) + exact IoCPostTrace.err (e := .Sys (.InvalidArgument 1#u64)) (e' := .invalidArgument 1) (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidArgument 1) : Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl rfl) rw [if_neg hlt] have hfl : first.val ≤ last.val := by scalar_tac rw [whenE_neg (by rw [GT.gt, BitVec.lt_def, BitVec.toNat_ofNat, BitVec.toNat_ofNat, Nat.mod_eq_of_lt hfv, Nat.mod_eq_of_lt hlv]; scalar_tac)] - refine IoCPost.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + refine IoCPostTrace.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ -- the range must be free refine WP.spec_bind (range_free_eq s first last hfl hlv) ?_ intro b hb - refine IoCPost.congr (run_bind_ok (run_liftE (isIoportRangeFree_run _ _ σ)) _) ?_ + refine IoCPostTrace.congr (run_bind_ok (run_liftE (isIoportRangeFree_run _ _ σ)) _) ?_ have hfree : decide ({x : Fl4Spec.IoPort | BitVec.ofNat 16 first.val ≤ x ∧ x ≤ BitVec.ofNat 16 last.val} ∩ {x | σ.archState.x64AllocatedIoPorts x = true} = ∅) = b := by rw [hb]; exact decide_eq_decide.2 (rangeFree_iff hIo hlv hfv) @@ -416,16 +560,16 @@ theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A | false => simp only [Bool.false_eq_true, ↓reduceIte] rw [whenE_pos (by simp)] - exact IoCPost.err (e := .Sys .RevokeFirst) (e' := .revokeFirst) + exact IoCPostTrace.err (e := .Sys .RevokeFirst) (e' := .revokeFirst) (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.revokeFirst : Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl trivial) | true => simp only [↓reduceIte] rw [whenE_neg (by simp)] - refine IoCPost.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + refine IoCPostTrace.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ have hfree' : ∀ p, first.val ≤ p → p ≤ last.val → bitOf (L.mem s) (L.ksb s) p = false := by simpa using hb.symm - refine io_target_tail arI th (L.ksb s) s ar σ hR hV c0 ac0 hca0 hcv0 y2 y3 _ _ ?_ + refine io_target_tail_trace arI th G (L.ksb s) s ar σ hR hV c0 ac0 hca0 hcv0 y2 y3 _ _ ?_ intro dsl pd hpd1 hpd hpdr hdst obtain ⟨da, db⟩ := dsl simp only at hpd1 hpd; subst hpd1 @@ -500,7 +644,10 @@ theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A rw [hF4.out (hL.out _ (by rw [wI_val hp]; omega))] refine ⟨.invokeIOPortControl (.ioPortControlInvocation (BitVec.ofNat 16 first.val) (BitVec.ofNat 16 last.val) (pd.bv, db) (ps.bv, idxs)), rfl, σ1, hst, σ', G', ?_, ⟨hR', hV', hM', hC', hI'⟩, hIo4, hFI', ?_, ?_, - fun hD => ?_, hstp⟩ + fun hD => ?_, hstp, + rfl, ct, s2, σ1, first, last, s3, ps, pd, rs, db.length, idxs, db, + hct, hs2, hst, hfl, hlv, hM3, hps, hpd, rfl, hci, hcap4, hdst3, hF4', + (DestLive.of_cnRef hpdr ⟨x0, hx0s, capV_of_capAt hc0⟩).same hS23 hk3, hdi, hSt4⟩ rotate_right · have hK2 : KsbOK (L := L) th s2 := hD.toK.same (hdk1 hD).1 have hdk3 : DK4 (L := L) s2 s3 := DK4.of_mask hK2.fits hK2.align hM3 @@ -543,6 +690,45 @@ theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A · -- the MDB invariants: a fresh IO port cap, naming no memory exact (MdbStep.same_left hM hS23 hSt4).okv hFR +/-- **Any other IOPortControl label**: `IllegalOperation`. -/ +theorem ioport_control_other_trace (th : TypedHeap) (G : MdbGhost) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (lbl : U64) + (hl : lbl.val ≠ 63) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64IOPortControlIssue) + (length slot buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4Spec.Data) + (ssl : Fl4Spec.CslotPtr) (ac : Fl4Spec.ArchCap) (excaps : List Fl4Spec.Cap) : + ioport.decode_control st arI s ar lbl length slot buf extra ⦃ res => + IoCPostTrace (L := L) arI th G (L.ksb s) σ (Fl4Spec.decodeIoportControlInvocation slbl args ssl ac excaps) s ar res ⦄ := by + unfold ioport.decode_control + rw [if_pos (by rw [bne_iff_ne, ne_eq, u64_eq_iff]; simpa using hl)] + simp only [error.illegal, bind_ok] + unfold Fl4Spec.decodeIoportControlInvocation + rw [if_neg hlbl] + exact IoCPostTrace.err (e := .Sys .IllegalOperation) (e' := .illegalOperation) rfl (Or.inl trivial) + + +/-- **IOPortControl `Issue`** (label 63). -/ +theorem ioport_control_refines (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hL : IoLayout th (L.ksb s)) (hIo : IoRel (L.mem s) (L.ksb s) σ) + (hPA : IoPortsAlloc (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.invocationType slbl = .archInvocationLabel .x64IOPortControlIssue) + (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) + (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact3 : ∀ w : U64, w.val < 1024 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ ioA (L.ksb s) w) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel th (L.mem s) x0 excaps) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IoPortControl) (ac : Fl4Spec.ArchCap) : + ioport.decode_control st arI s ar fl4_abi.label.X86IOPortControlIssue length (cslotAddr (ps.bv, idxs)) buf + extra ⦃ res => + IoCPost (L := L) arI th (L.ksb s) σ + (Fl4Spec.decodeIoportControlInvocation slbl (args.map UScalar.bv) (ps.bv, idxs) ac excaps) s ar res ⦄ := + WP.spec_mono (ioport_control_refines_trace arI th hP s ar σ G hCR hL hIo hPA hFI hFR slbl hlbl + length buf ct hct args hlen hargs hcur htcb hact hact3 extra x0 hx0 excaps hex hps hls hsrcC ac) + (fun _ h => h.toIoCPost) + /-- **Any other IOPortControl label**: `IllegalOperation`. -/ theorem ioport_control_other (th : TypedHeap) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (lbl : U64) (hl : lbl.val ≠ 63) (slbl : Fl4Spec.Data) @@ -558,6 +744,7 @@ theorem ioport_control_other (th : TypedHeap) (s : S) (ar : A) (σ : Fl4Spec.Sta rw [if_neg hlbl] exact IoCPost.err (e := .Sys .IllegalOperation) (e' := .illegalOperation) rfl (Or.inl trivial) + end end diff --git a/proofs/Fl4Proofs/P3/IpcGrantTrV.lean b/proofs/Fl4Proofs/P3/IpcGrantTrV.lean new file mode 100644 index 000000000..379c95121 --- /dev/null +++ b/proofs/Fl4Proofs/P3/IpcGrantTrV.lean @@ -0,0 +1,519 @@ +import Fl4Proofs.P3.IpcGrantTr +import Fl4Proofs.P3.DeriveVt +open Aeneas Aeneas.Std Result + +/-! +P3 (line mmuAD, Stage 3): **the grant transfer loop whose insert record also carries `VtOK`** (FL4's `derive_cap` gives +no VT-x view, or the source's view and `NewOK`): `InsRecV`, `TrIV` and the loop, siblings of P3/IpcGrantTr's (whose +statements are unchanged), generated by `handoff/tools/restore_s43.py`. No named premise. +-/ + +namespace Fl4Proofs.P3.Ipc +open fl4_core Classical Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Sched + +noncomputable section + +/-! ## The transfer loop with its insert -/ + +section LoopG +variable {S A : Type} {st : machine.Store S} {ar : machine.Arch A} [L : LawfulStore st] +variable {th : TypedHeap} {cr : Fl4Spec.Cr3} {N T C E R : Set U64} {B : Set (U64 × Fl4Spec.Word)} {c : U64} + +/-- **The transfer loop's insert**, recorded against the loop's start +`(s0, σ0)`: FL4's `cdt::insert` of a derived cap `c'` (`CapRel c' a'`) from a +source slot of the extra caps into the receive slot ran at a state `sk` with +`s0`'s slot words (`InsWords` to `sk'`, writing slot words only), the spec's +`cap_insert` at a state `σk` that is `σ0` up to user memory (ending at +`σc`), with the facts `JAtZ.insDJ` takes, at `s0` and `σ0`. -/ +def InsRecV (th : TypedHeap) (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (slots0 : List Fl4Spec.CslotPtr) + (s0 : S) (σ0 : Fl4Spec.State Unit) (sk' : S) (σc : Fl4Spec.State Unit) : Prop := + ∃ (cv c' : capability.Cap) (a' : Fl4Spec.Cap) (ps pd : U64) (rs rd : Nat) (idx idxd : List Bool) (sk : S) + (σk : Fl4Spec.State Unit), + th ps = some (.cnode rs) ∧ th pd = some (.cnode rd) ∧ idx.length = rs ∧ idxd.length = rd ∧ + (∃ j, ∃ hj : j < caps.length, (caps[j]).2 = (ps.bv, idx)) ∧ slots0 = [(pd.bv, idxd)] ∧ + capAt (L.mem s0) (cslotAddr (ps.bv, idx)) = ok cv ∧ cv ≠ .Null ∧ + capAt (L.mem s0) (cslotAddr (pd.bv, idxd)) = ok .Null ∧ + CapRel c' a' ∧ CapValid th c' ∧ EncWf c' ∧ c' ≠ .Null ∧ shape c' = shape cv ∧ badgeF c' = badgeF cv ∧ + (isUt cv = true → ¬ ∃ x, σ0.cdt x = some (ps.bv, idx)) ∧ isZb cv = false ∧ okey c' = okey cv ∧ + (∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 cv → Vs.VCapOK P L4 L3 L2 L1 c') ∧ + (Vs.TabUn c' → Vs.TabUn cv) ∧ (Vs.Keyed2 c' cv → c' = cv) ∧ ¬ Vs.TabUn c' ∧ (isUt cv = true → c' = cv) ∧ (Vs.NativeCap cv → Vs.NativeCap c') ∧ VtOK cv c' ∧ + SlotsSame th (L.mem s0) (L.mem sk) ∧ + InsWords (L := L) th sk sk' c' cv (cslotAddr (ps.bv, idx)) (cslotAddr (pd.bv, idxd)) ∧ + WritesIn st sk sk' (Sched.SlotWd th) ∧ + (∃ um, σk = setUm σ0 um) ∧ + Fl4Spec.capInsert (Z := Unit) a' (ps.bv, idx) (pd.bv, idxd) σk = ({((), σc)}, False) + +/-- One insert so far (`InsRecV`): the spec state is the insert's outcome up +to user memory, the slot words are those right after FL4's insert. -/ +def TrIV (th : TypedHeap) (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (slots0 : List Fl4Spec.CslotPtr) + (s0 : S) (σ0 : Fl4Spec.State Unit) (s : S) (σ : Fl4Spec.State Unit) : Prop := + ∃ sk' σc, InsRecV (L := L) th caps slots0 s0 σ0 sk' σc ∧ (∃ um, σ = setUm σc um) ∧ + SlotsSame th (L.mem sk') (L.mem s) + +theorem trI_umV {th : TypedHeap} {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {slots0 : List Fl4Spec.CslotPtr} + {s0 s s' : S} {σ0 σ : Fl4Spec.State Unit} (h : TrIV (L := L) th caps slots0 s0 σ0 s σ) + (hS : SlotsSame th (L.mem s) (L.mem s')) (v : Fl4Spec.Word → Fl4Spec.Word8) : + TrIV (L := L) th caps slots0 s0 σ0 s' (setUm σ v) := by + obtain ⟨sk', σc, hI, ⟨um, rfl⟩, h1⟩ := h + exact ⟨sk', σc, hI, ⟨v, rfl⟩, SlotsSame.trans' h1 hS⟩ + +/-- `TInv` with the insert trace and the writes in the receiver's buffer. -/ +def TInvGV (th : TypedHeap) (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (ep : U64) (rb : Fl4Spec.Word) (rbw : U64) (s0 : S) (σ0 : Fl4Spec.State Unit) (dest0 : U64) + (slots0 : List Fl4Spec.CslotPtr) (mi0 : Fl4Spec.MessageInfo) (x : S × msginfo.MessageInfo × obj.Slot × U64) : + Prop := + ∃ σ mi slots, x.2.2.2.val ≤ caps.length ∧ MiRel x.2.1 mi ∧ mi.miLength = mi0.miLength ∧ + mi.miLabel = mi0.miLabel ∧ + (∀ r σf, Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb x.2.2.2.val (caps.drop x.2.2.2.val) slots mi σ = + ({(r, σf)}, False) → + Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb 0 caps slots0 mi0 σ0 = ({(r, σf)}, False)) ∧ + IpcRel N T E R B (L.mem x.1) σ ∧ SRE st N T C E R B c x.1 σ ∧ (∃ G, RelTopB (L := L) th G N T C E R B c x.1 σ) ∧ + (∀ k, k < 128 → Fl4Spec.inUserFrame (bufAddr rb k) σ) ∧ + (∀ j (hj : j < caps.length), x.2.2.2.val ≤ j → + ∃ cv, capAt (L.mem x.1) (exAt extra j) = ok cv ∧ UwEq ep cv (caps[j]).1) ∧ + ((x.2.2.1 = 0#u64 ∧ slots = []) ∨ + (x.2.2.1 = dest0 ∧ slots = slots0 ∧ dest0 ≠ 0#u64 ∧ SlotsSame th (L.mem s0) (L.mem x.1) ∧ σ.cdt = σ0.cdt)) ∧ + KeepCV σ0 σ ∧ WritesIn st s0 x.1 (FW th B) ∧ x.2.1.caps_unwrapped.val < 8 ∧ + (TrN (L := L) th s0 σ0 x.1 σ ∨ (x.2.2.1 = 0#u64 ∧ TrIV (L := L) th caps slots0 s0 σ0 x.1 σ)) ∧ + WritesIn st s0 x.1 (RFoot th rbw) + +/-- `TPost` with the insert trace. -/ +def TPostGV (th : TypedHeap) (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (ep : U64) (rb : Fl4Spec.Word) + (rbw : U64) (s0 : S) (σ0 : Fl4Spec.State Unit) (slots0 : List Fl4Spec.CslotPtr) (mi0 : Fl4Spec.MessageInfo) + (y : S × msginfo.MessageInfo × U64) : Prop := + ∃ σ' mf, Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb 0 caps slots0 mi0 σ0 = ({(mf, σ')}, False) ∧ + MiRel y.2.1 mf ∧ mf.miExtraCaps = BitVec.ofNat 64 y.2.2.val ∧ + y.2.1.caps_unwrapped.val < 8 ∧ y.2.2.val ≤ 3 ∧ mf.miLabel = mi0.miLabel ∧ + IpcRel N T E R B (L.mem y.1) σ' ∧ SRE st N T C E R B c y.1 σ' ∧ (KeepCV σ0 σ' ∧ WritesIn st s0 y.1 (FW th B)) ∧ + (∃ G, RelTopB (L := L) th G N T C E R B c y.1 σ') ∧ + (TrN (L := L) th s0 σ0 y.1 σ' ∨ TrIV (L := L) th caps slots0 s0 σ0 y.1 σ') ∧ + WritesIn st s0 y.1 (RFoot th rbw) + +theorem tpostG_ofV {th : TypedHeap} {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {ep : U64} {rb : Fl4Spec.Word} + {σ0 σ : Fl4Spec.State Unit} {slots0 slots : List Fl4Spec.CslotPtr} {mi0 mi : Fl4Spec.MessageInfo} {s : S} + {out : msginfo.MessageInfo} {i : U64} + (hmi : MiRel out mi) + (hch : ∀ r σf, Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb i.val (caps.drop i.val) slots mi σ = + ({(r, σf)}, False) → + Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb 0 caps slots0 mi0 σ0 = ({(r, σf)}, False)) + (hend : Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb i.val (caps.drop i.val) slots mi σ = + ({(miAt mi i.val, σ)}, False)) + (hcu : out.caps_unwrapped.val < 8) (hi3 : i.val ≤ 3) (hlb : mi.miLabel = mi0.miLabel) + (hR : IpcRel N T E R B (L.mem s) σ) (hS : SRE st N T C E R B c s σ) {s0 : S} (hK : KeepCV σ0 σ) + (hW : WritesIn st s0 s (FW th B)) + (hB : ∃ G, RelTopB (L := L) th G N T C E R B c s σ) + (htr : TrN (L := L) th s0 σ0 s σ ∨ TrIV (L := L) th caps slots0 s0 σ0 s σ) {rbw : U64} + (hWr : WritesIn st s0 s (RFoot th rbw)) : + TPostGV (L := L) (st := st) (N := N) (T := T) (C := C) (E := E) (R := R) (B := B) (c := c) th caps ep rb rbw s0 σ0 + slots0 mi0 (s, out, i) := + ⟨σ, miAt mi i.val, hch _ _ hend, hmi, rfl, hcu, hi3, hlb, hR, hS, ⟨hK, hW⟩, hB, htr, hWr⟩ + +set_option maxHeartbeats 8000000 in +/-- **One step of the transfer loop**, with the insert trace (IpcGrant's `tc_body`). -/ +theorem tc_bodyGV (hP0 : IPlaced N T E R B) {th : TypedHeap} {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {ep rbw : U64} {rb : Fl4Spec.Word} {s0 : S} + {σ0 : Fl4Spec.State Unit} {dest0 : U64} {slots0 : List Fl4Spec.CslotPtr} {mi0 : Fl4Spec.MessageInfo} + (hlen : caps.length ≤ 3) + (hsrc : ∀ j (hj : j < caps.length), SrcOK th (L.mem s0) (exAt extra j) caps[j]) + (hnull : ∀ j, caps.length ≤ j → j < 3 → exAt extra j = 0#u64) + (hdest : dest0 ≠ 0#u64 → ∃ d, slots0 = [d] ∧ DestOK th (L.mem s0) dest0 d) + (hm : BufMap B rbw rb) (x : S × msginfo.MessageInfo × obj.Slot × U64) + (hx : TInvGV (L := L) (st := st) (N := N) (T := T) (C := C) (E := E) (R := R) (B := B) (c := c) th caps extra ep rb + rbw s0 σ0 dest0 slots0 mi0 x) : + ipc.transfer_caps_loop.body st extra ep rbw x.1 x.2.1 x.2.2.1 x.2.2.2 ⦃ r => match r with + | .done y => TPostGV (L := L) (st := st) (N := N) (T := T) (C := C) (E := E) (R := R) (B := B) (c := c) th caps ep + rb rbw s0 σ0 slots0 mi0 y + | .cont x' => TInvGV (L := L) (st := st) (N := N) (T := T) (C := C) (E := E) (R := R) (B := B) (c := c) th caps + extra ep rb rbw s0 σ0 dest0 slots0 mi0 x' ∧ 3 - x'.2.2.2.val < 3 - x.2.2.2.val ⦄ := by + obtain ⟨s, out, dest, i⟩ := x + obtain ⟨σ, mi, slots, hi, hmi, hml, hmlb, hch, hR, hS, ⟨G, hB⟩, hu, hpend, hdst, hK0, hW0, hcu, hTr, hWr⟩ := hx + dsimp only at hi hmi hch hR hS hB hpend hdst hTr ⊢ + have hPl : Placed th := hB.1 + unfold ipc.transfer_caps_loop.body + by_cases h3 : i < 3#u64 + · have hi3 : i.val < 3 := by scalar_tac + rw [if_pos h3] + simp only [lift, bind_ok] + rw [index_exAt extra _ (by rw [cast_us_val i hi3]; exact hi3), cast_us_val i hi3] + simp only [bind_ok, obj.Slot.is_null] + by_cases hlt : i.val < caps.length + · obtain ⟨ps, rs, idx, cv0, hps, hls, hsl, hxa, hcv0, habs0, hw0, he0, hcvv0⟩ := hsrc i.val hlt + have hsI : IsSlot th (exAt extra i.val) := by + rw [hxa]; exact AbsSlot.isSlot (⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ : AbsSlot th (ps.bv, idx)) + have hne : exAt extra i.val ≠ 0#u64 := hsI.ne_zero hPl + simp only [hne, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨cv, hcv, huw⟩ := hpend i.val hlt le_rfl + rw [slot_cap_eq, hcv] + simp only [bind_ok] + rw [unwrap_eq] + simp only [bind_ok] + have hdrop : caps.drop i.val = caps[i.val] :: caps.drop (i.val + 1) := List.drop_eq_getElem_cons hlt + by_cases hu' : UnwrapP ep (absCap cv) + · rw [if_pos hu'] + simp only [buf_caps_eq, bind_ok] + have hk : (core.num.U64.wrapping_add 122#u64 i).val = 122 + i.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + refine WP.spec_bind (buf_set_top hP0 hB hR hS hm (k := core.num.U64.wrapping_add 122#u64 i) + (by rw [hk]; omega) (epBadgeW cv) (hu _ (by rw [hk]; omega))) ?_ + rintro s2 ⟨rfl, hst, hR2, hS2, hB2⟩ + try simp only [lift, bind_ok] + have hand : (i &&& 63#u64).val = i.val := by + rw [UScalar.val_and] + have : i.val = 0 ∨ i.val = 1 ∨ i.val = 2 := by omega + rcases this with h | h | h <;> rw [h] <;> rfl + obtain ⟨i6, h6e, h6v, h6⟩ := spec_exists (U64.ShiftLeft_spec 1#u64 (i &&& 63#u64) (by rw [hand]; omega)) + have hi6 : i6.val < 8 := by + rw [h6v, hand] + rcases (show i.val = 0 ∨ i.val = 1 ∨ i.val = 2 by omega) with h | h | h <;> rw [h] <;> + simp [U64.size, U64.numBits] + rw [h6e] + simp only [bind_ok] + obtain ⟨i8, h8e, h8⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h8e, bind_ok, WP.spec_ok] + have hi8 : i8.val = i.val + 1 := by simpa using h8 + set σ1 := setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr rb (core.num.U64.wrapping_add 122#u64 i).val) + (epBadgeW cv).bv) with hσ1 + have hwp := hm _ (by rw [hk]; omega : (core.num.U64.wrapping_add 122#u64 i).val < 128) + have hal := (hP0.e.balign _ hwp).1 + have hSS : SlotsSame th (L.mem s) (L.mem (L.wr s (fl4BufA rbw (core.num.U64.wrapping_add 122#u64 i)) + (epBadgeW cv))) := + slotsSame_wr hPl s _ hal fun a ha h => hB.2.2.2.2.2.1.slot _ + (Or.inr (Or.inr (Or.inr (Or.inr ⟨_, hwp, le_refl _, by omega⟩)))) a ha h + refine ⟨⟨σ1, miUnwrap mi i.val, slots, by rw [hi8]; omega, ⟨hmi.1, ?_, hmi.2.2⟩, hml, hmlb, ?_, hR2, hS2, + ⟨G, hB2⟩, fun k hk => (inUserFrame_setUm _ _ _).2 (hu k hk), fun j hj hij => ?_, ?_, ⟨fun p => hK0.1 p, hK0.2⟩, + (hW0.trans (writesIn_wr hal)).mono fun b h => h.elim id (fun e => Or.inr ⟨_, hwp, e⟩), + by show (out.caps_unwrapped ||| i6).val < 2 ^ 3; rw [UScalar.val_or]; exact Nat.or_lt_two_pow hcu hi6, + hTr.imp (fun h => trN_um h hSS _) (fun h => ⟨h.1, trI_umV h.2 hSS _⟩), + (hWr.trans (writesIn_wr hal)).mono fun b h => h.elim id (fun e => Or.inr ⟨_, by rw [hk]; omega, e⟩)⟩, + by rw [hi8]; omega⟩ + · show (out.caps_unwrapped ||| i6).bv = mi.miCapsUnwrapped ||| ((1 : Fl4Spec.Data) <<< i.val) + rw [UScalar.bv_or, h6, hand]; exact congrArg₂ (· ||| ·) hmi.2.1 rfl + · intro r σf hr + refine hch r σf ?_ + rw [hdrop] + refine tcl_badge (hc := huw.1.mp hu') ?_ + (by rw [hi8] at hr; exact hr) + unfold Fl4Spec.setExtraBadge + show Fl4Spec.storeWordOffs rb (Fl4Spec.bufferCptrIndex + i.val) (Fl4Spec.capEpBadge (caps[i.val]).1) σ = _ + rw [← huw.2 (huw.1.mp hu')] + rw [show Fl4Spec.bufferCptrIndex + i.val = (core.num.U64.wrapping_add 122#u64 i).val by + rw [hk]; rfl] + exact hst + · obtain ⟨cv', h1, h2⟩ := hpend j hj (by rw [hi8] at hij; omega) + exact ⟨cv', by rw [hSS.capAt (by + obtain ⟨ps', rs', idx', cv'', hps', hls', -, hxa', -⟩ := hsrc j hj + rw [hxa']; exact AbsSlot.isSlot (⟨ps', rfl, Or.inl (by simpa [hls'] using hps')⟩ : + AbsSlot th (ps'.bv, idx')))]; exact h1, h2⟩ + · rcases hdst with h | ⟨h1, h2, h3', h4, h5⟩ + · exact Or.inl h + · exact Or.inr ⟨h1, h2, h3', SlotsSame.trans' h4 hSS, h5⟩ + · rw [if_neg hu'] + simp only [] + rcases hdst with ⟨rfl, rfl⟩ | ⟨rfl, rfl, hd0, hSS, hcdt⟩ + · simp only [decide_true, ↓reduceIte, WP.spec_ok] + refine tpostG_ofV hmi hch ?_ hcu (by omega) hmlb hR hS hK0 hW0 ⟨G, hB⟩ (hTr.imp_right And.right) hWr + rw [hdrop] + exact tcl_noslot (fun h => hu' (huw.1.mpr h)) + · obtain ⟨d, hsl0, ⟨pd, rd, idxd, hpd, hld, rfl, hdA, hdN⟩⟩ := hdest hd0 + subst hsl0 + simp only [hd0, decide_false, Bool.false_eq_true, ↓reduceIte] + have hcvs : capAt (L.mem s) (exAt extra i.val) = ok cv0 := by rw [hSS.capAt hsI, hcv0] + rw [hcv] at hcvs + have hcvs' : cv = cv0 := by simpa using hcvs + subst hcvs' + have hsl' : (caps[i.val]).2 = (ps.bv, idx) := hsl + have hnot : ¬ UnwrapP ep (caps[i.val]).1 := fun h => hu' (huw.1.mpr h) + have hAs : AbsSlot th (ps.bv, idx) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hdI : IsSlot th dest := by rw [hdA]; exact hAd.isSlot + have hncs : cv ≠ .Null → NoChildrenRel (st := st) s σ (exAt extra i.val) (caps[i.val]).2 := by + intro hut + obtain ⟨-, ⟨-, -, hM, hC, hI⟩, -⟩ := hB + rw [hsl', hxa] + refine noChildren_of_adj th hPl s σ G hM hC (hI.parentAdj (th := th)) _ hAs + ((hM.members _).2 ⟨hAs.isSlot, cv, by rw [← hxa]; exact hcv, ?_⟩) + exact hut + obtain ⟨r, hr, r', hr', hdr⟩ := derive_rel (Z := Unit) s σ (exAt extra i.val) (caps[i.val]).2 cv hw0 he0 + (fun h => hncs (by rintro rfl; exact h)) + rw [← habs0] at hr' + rw [hr] + simp only [bind_ok] + cases r with + | Err e => + cases r' with + | ok _ => exact hdr.elim + | error e' => + simp only [WP.spec_ok] + refine tpostG_ofV hmi hch ?_ hcu (by omega) hmlb hR hS hK0 hW0 ⟨G, hB⟩ (hTr.imp_right And.right) hWr + rw [hdrop] + exact tcl_derr hnot hr' + | Ok c' => + cases r' with + | error _ => exact hdr.elim + | ok a' => + obtain ⟨hw', ha', he', hv', hk'⟩ := hdr + simp only [] + by_cases hn : c' = .Null + · subst hn + rw [cap_is_null_null] + simp only [bind_ok, ↓reduceIte, WP.spec_ok] + refine tpostG_ofV hmi hch ?_ hcu (by omega) hmlb hR hS hK0 hW0 ⟨G, hB⟩ (hTr.imp_right And.right) hWr + rw [hdrop] + rw [← ha'] at hr' + exact tcl_dnull hnot hr' + · rw [cap_is_null_ne hn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hcvn : cv ≠ .Null := by + rintro rfl; unfold objects.derive_cap at hr; simp at hr; exact hn hr.symm + obtain ⟨hsh, hbd, hzb, hnu⟩ := derive_shape hr hn (hncs hcvn) + have hsnn : cv ≠ .Null := fun h => hn ((Vs.shape_kind hsh).2.2.2.2.2 h) + have hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idx)) = ok cv := by rw [← hxa]; exact hcv + have hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null := by + rw [← hdA, hSS.capAt hdI]; exact hdN + have hut : isUt cv = true → ∀ y, ¬ Mdb.Desc G.par y (cslotAddr (ps.bv, idx)) := by + intro hu0 y hy + obtain ⟨z, hz⟩ := desc_tail_child hy + exact hnu hu0 (by rw [hsl']; exact (hB.2.1.2.2.2.1.has_child_iff hPl hAs).2 ⟨z, hz⟩) + have hsI' : IsSlot th (cslotAddr (ps.bv, idx)) := hAs.isSlot + have hnx : nextOf (L.mem s) (cslotAddr (ps.bv, idx)) = 0#u64 ∨ + IsSlot th (nextOf (L.mem s) (cslotAddr (ps.bv, idx))) := by + have hm := (hB.2.1.2.2.1.members _).2 ⟨hsI', cv, hsrcC, hsnn⟩ + rcases hB.2.1.2.2.1.next_mem hm with h | ⟨h, -⟩ + · exact Or.inl h + · exact Or.inr ((hB.2.1.2.2.1.members _).1 h).1 + have hdnm : cslotAddr (pd.bv, idxd) ∉ G.chains.flatten := fun h => by + obtain ⟨-, c0, h0, hn0⟩ := (hB.2.1.2.2.1.members _).1 h + rw [hdst] at h0; exact hn0 (eq_of_ok_eq h0).symm + have hsd : cslotAddr (ps.bv, idx) ≠ cslotAddr (pd.bv, idxd) := fun e => by + rw [e, hdst] at hsrcC; exact hsnn (eq_of_ok_eq hsrcC).symm + have hnxS : nextOf (L.mem s) (cslotAddr (ps.bv, idx)) = 0#u64 ∨ + (IsSlot th (nextOf (L.mem s) (cslotAddr (ps.bv, idx))) ∧ + nextOf (L.mem s) (cslotAddr (ps.bv, idx)) ≠ cslotAddr (pd.bv, idxd) ∧ + nextOf (L.mem s) (cslotAddr (ps.bv, idx)) ≠ cslotAddr (ps.bv, idx)) := by + have hm := (hB.2.1.2.2.1.members _).2 ⟨hsI', cv, hsrcC, hsnn⟩ + rcases hB.2.1.2.2.1.next_mem hm with h | ⟨h1, h2⟩ + · exact Or.inl h + · exact Or.inr ⟨((hB.2.1.2.2.1.members _).1 h1).1, fun e => hdnm (e ▸ h1), h2⟩ + have hins0 := WP.spec_and (insert_top hP0 hB hR hS hps hpd hls hld hsrcC hsnn hdst c' a' ⟨hw', ha'⟩ + (hv' th hcvv0) he' hn hsh (Or.inl hbd) hut hzb (derive_okey hr hn) + (fun P L4 L3 L2 L1 => derive_vcap hr P L4 L3 L2 L1) (derive_ut hr)) + (insert_capfr2 hPl s c' _ _ hsI' hAd.isSlot cv hsrcC he0 he' hnx) + have hins := WP.spec_and hins0 (insert_mem hPl s c' _ _ hsI' hAd.isSlot hsd cv hsrcC hnxS) + rw [← hxa, ← hdA] at hins + refine WP.spec_bind hins ?_ + rintro s2 ⟨⟨⟨σ2, hci, hR2, hS2, ⟨hW2, hK2⟩, G2, hB2⟩, hfr⟩, hiw⟩ + rw [hxa, hdA] at hiw + obtain ⟨i3, h3e, h3v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h3e, bind_ok, WP.spec_ok] + have hi3v : i3.val = i.val + 1 := by simpa using h3v + have hmem : ((), σ2) ∈ (Fl4Spec.capInsert (Z := Unit) a' (ps.bv, idx) (pd.bv, idxd) σ).1 := by + rw [hci]; rfl + have hK := capInsert_nkn a' _ _ σ _ hmem + have hrec : InsRecV (L := L) th caps [(pd.bv, idxd)] s0 σ0 s2 σ2 := by + obtain ⟨⟨um, hum⟩, -⟩ := hTr.resolve_right fun h => hd0 h.1 + exact ⟨cv, c', a', ps, pd, rs, rd, idx, idxd, s, σ, hps, hpd, hls, hld, ⟨i.val, hlt, hsl'⟩, rfl, + by rw [← hxa]; exact hcv0, hsnn, by rw [← hdA]; exact hdN, ⟨hw', ha'⟩, hv' th hcvv0, he', hn, + hsh, hbd, fun hu0 hx => hnu hu0 (by rw [hcdt, hsl']; exact hx), hzb, derive_okey hr hn, + fun P L4 L3 L2 L1 => derive_vcap hr P L4 L3 L2 L1, fun h => (derive_tabun hr h).elim, fun hk => Vs.vkey_derive hr hk, fun h => derive_tabun hr h, + derive_ut hr, derive_nat hr, derive_vt hr, hSS, + hiw, hW2, ⟨um, hum⟩, hci⟩ + refine ⟨⟨σ2, mi, [], by rw [hi3v]; omega, hmi, hml, hmlb, fun r σf hr0 => ?_, hR2, hS2, ⟨G2, hB2⟩, + fun k hk => hK.inUserFrame (hu k hk), fun j hj hij => ?_, Or.inl ⟨rfl, rfl⟩, hK0.trans hK2, + (hW0.trans hW2).mono fun b h => h.elim id Or.inl, hcu, Or.inr ⟨rfl, s2, σ2, hrec, + ⟨σ2.machineState.underlyingMemory, rfl⟩, SlotsSame.refl _ _⟩, + (hWr.trans hW2).mono fun b h => h.elim id Or.inl⟩, by rw [hi3v]; omega⟩ + · refine hch r σf ?_ + rw [hdrop] + refine tcl_insert hnot hr' (fun h => hn ((CapRel.null_iff ⟨hw', ha'⟩).1 h)) ?_ + (by rw [hi3v] at hr0; exact hr0) + show Fl4Spec.capInsert a' (caps[i.val]).2 (pd.bv, idxd) σ = _ + rw [hsl']; exact hci + · obtain ⟨cv', h1, h2⟩ := hpend j hj (by rw [hi3v] at hij; omega) + obtain ⟨ps', rs', idx', cv'', hps', hls', -, hxa', -⟩ := hsrc j hj + have hjI : IsSlot th (exAt extra j) := by + rw [hxa']; exact AbsSlot.isSlot (⟨ps', rfl, Or.inl (by simpa [hls'] using hps')⟩ : + AbsSlot th (ps'.bv, idx')) + obtain ⟨-, -, -, cvj, -, -, -, -, hcvj, habsj, -⟩ := hsrc j hj + have hsrcfull : ∀ c'', fullF cv c' = some c'' → ¬ UnwrapP ep (absCap cv) := fun c'' hf => + not_unwrap_of_ut (by + cases cv <;> simp [fullF] at hf + exact ⟨_, _, _, _, rfl⟩) + by_cases ejd : exAt extra j = dest + · refine ⟨c', by rw [ejd]; exact hfr.2.1, ?_, fun h => ?_⟩ + · rw [ejd, hdN] at hcvj + have : cvj = .Null := by simpa using hcvj.symm + rw [habsj, this] + refine ⟨fun h => absurd ((unwrap_shape ep cv).2 (hsh ▸ (unwrap_shape ep c').1 h)) hu', + fun h => absurd h not_unwrap_of_null⟩ + · rw [ejd, hdN] at hcvj + have : cvj = .Null := by simpa using hcvj.symm + rw [habsj, this] at h; exact absurd h not_unwrap_of_null + · rcases hfr.2.2 _ hjI ejd with heq | ⟨ejs, c'', hfc, hc''⟩ + · exact ⟨cv', by rw [heq]; exact h1, h2⟩ + · rw [ejs, hcv0] at hcvj + have : cvj = cv := by simpa using hcvj.symm + have hnu := hsrcfull c'' hfc + have hnu'' := not_unwrap_of_ut (ep := ep) (fullF_ut hfc) + refine ⟨c'', hc'', ⟨fun h => absurd h hnu'', fun h => ?_⟩, fun h => ?_⟩ + · rw [habsj, this] at h; exact absurd h hnu + · rw [habsj, this] at h; exact absurd h hnu + · have hieq : i.val = caps.length := by omega + rw [hnull i.val (by omega) hi3] + simp only [decide_true, ↓reduceIte, WP.spec_ok] + refine tpostG_ofV hmi hch ?_ hcu (by omega) hmlb hR hS hK0 hW0 ⟨G, hB⟩ (hTr.imp_right And.right) hWr + rw [hieq, List.drop_length, ← hieq] + exact tcl_nil _ _ _ + · rw [if_neg h3] + simp only [WP.spec_ok] + have hieq : i.val = caps.length := by + have : 3 ≤ i.val := by scalar_tac + omega + refine tpostG_ofV hmi hch ?_ hcu (by omega) hmlb hR hS hK0 hW0 ⟨G, hB⟩ (hTr.imp_right And.right) hWr + rw [hieq, List.drop_length, ← hieq] + exact tcl_nil _ _ _ + +/-- IpcGrant's `transfer_loop_top` with the insert trace. -/ +theorem transfer_loop_topGV (hP0 : IPlaced N T E R B) {th : TypedHeap} {G : MdbGhost} + {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {extra : Aeneas.Std.Array obj.Slot 3#usize} {ep rbw : U64} + {rb : Fl4Spec.Word} {s0 : S} {σ0 : Fl4Spec.State Unit} {dest0 : U64} {slots0 : List Fl4Spec.CslotPtr} + {mi0 : Fl4Spec.MessageInfo} (out0 : msginfo.MessageInfo) (hmi0 : MiRel out0 mi0) + (hcu0 : out0.caps_unwrapped.val < 8) (hB0 : RelTopB (L := L) th G N T C E R B c s0 σ0) (hR0 : IpcRel N T E R B (L.mem s0) σ0) + (hS0 : SRE st N T C E R B c s0 σ0) (hlen : caps.length ≤ 3) + (hsrc : ∀ j (hj : j < caps.length), SrcOK th (L.mem s0) (exAt extra j) caps[j]) + (hnull : ∀ j, caps.length ≤ j → j < 3 → exAt extra j = 0#u64) + (hD : DestConf th (L.mem s0) caps extra dest0 slots0) + (hm : BufMap B rbw rb) (hu : ∀ k, k < 128 → Fl4Spec.inUserFrame (bufAddr rb k) σ0) : + ipc.transfer_caps_loop st s0 extra ep rbw out0 dest0 0#u64 ⦃ y => + TPostGV (L := L) (st := st) (N := N) (T := T) (C := C) (E := E) (R := R) (B := B) (c := c) th caps ep rb rbw + s0 σ0 slots0 mi0 y ⦄ := by + have hdest : dest0 ≠ 0#u64 → ∃ d, slots0 = [d] ∧ DestOK th (L.mem s0) dest0 d := fun h => by + rcases hD with ⟨h0, -⟩ | ⟨-, hd⟩ + · exact absurd h0 h + · exact hd + unfold ipc.transfer_caps_loop + refine loop.spec_decr_nat (fun x => 3 - x.2.2.2.val) + (TInvGV (L := L) (st := st) (N := N) (T := T) (C := C) (E := E) (R := R) (B := B) (c := c) th caps extra ep rb + rbw s0 σ0 dest0 slots0 mi0) _ _ _ (fun x hx => ?_) ?_ + · obtain ⟨s, o, d, i⟩ := x + have h := tc_bodyGV hP0 hlen hsrc hnull hdest hm (s, o, d, i) hx + refine WP.spec_mono h ?_ + intro r hr + cases r with + | done y => exact hr + | cont x' => exact hr + · refine ⟨σ0, mi0, slots0, by simp, hmi0, rfl, rfl, fun r σf h => by simpa using h, hR0, hS0, ⟨G, hB0⟩, hu, + fun j hj _ => ?_, ?_, KeepCV.refl σ0, WritesIn.refl s0 _, hcu0, + Or.inl ⟨⟨σ0.machineState.underlyingMemory, rfl⟩, SlotsSame.refl _ _⟩, WritesIn.refl s0 _⟩ + · obtain ⟨ps, rs, idx, cv, -, -, -, -, hcv, habs, -⟩ := hsrc j hj + exact ⟨cv, hcv, UwEq.of_abs habs.symm⟩ + · rcases hD with ⟨h0, h1⟩ | ⟨h0, -⟩ + · exact Or.inl ⟨h0, h1⟩ + · exact Or.inr ⟨rfl, rfl, h0, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, rfl⟩ + +set_option maxHeartbeats 4000000 in +/-- IpcGrant's `transfer_caps_top` with the insert trace. -/ +theorem transfer_caps_topGV (hP0 : IPlaced N T E R B) {th : TypedHeap} {G : MdbGhost} {s : S} + {σ : Fl4Spec.State Unit} (hB : RelTopB (L := L) th G N T C E R B c s σ) (hR : IpcRel N T E R B (L.mem s) σ) + (hS : SRE st N T C E R B c s σ) (info : msginfo.MessageInfo) (mi : Fl4Spec.MessageInfo) + (hil : info.length.bv = mi.miLength) (hlb : info.label.bv = mi.miLabel) + {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hlen : caps.length ≤ 3) + (hsrc : ∀ j (hj : j < caps.length), SrcOK th (L.mem s) (exAt extra j) caps[j]) + (hnull : ∀ j, caps.length ≤ j → j < 3 → exAt extra j = 0#u64) + (ep : U64) (receiver : obj.Tcb) (rbw : U64) (rbo : Option Fl4Spec.Word) (hrb : rbw = 0#u64 ↔ rbo = none) + (hm : ∀ rb, rbo = some rb → BufMap B rbw rb ∧ ∀ k, k < 128 → Fl4Spec.inUserFrame (bufAddr rb k) σ) + (hGRs : ∀ rb, rbo = some rb → ∃ sl, Fl4Spec.getReceiveSlots (Z := Unit) receiver.bv (some rb) σ = ({(sl, σ)}, False)) + (hG1 : ∀ rb, rbo = some rb → ipc.get_receive_slot st s receiver rbw ⦃ dest => ∃ slots, + Fl4Spec.getReceiveSlots (Z := Unit) receiver.bv (some rb) σ = ({(slots, σ)}, False) ∧ + DestConf th (L.mem s) caps extra dest slots ⦄) + (hGL : ∀ rb, rbo = some rb → ipc.get_receive_slot st s receiver rbw ⦃ dest => dest ≠ 0#u64 → ∃ pd idx, + dest = cslotAddr (pd.bv, idx) ∧ th pd = some (.cnode idx.length) ∧ DestCap th (L.mem s) pd ⦄) : + ipc.transfer_caps st s info extra ep receiver rbw ⦃ r => ∃ σ' mf, + Fl4Spec.transferCaps (Z := Unit) mi caps (some ep.bv) receiver.bv rbo σ = ({(mf, σ')}, False) ∧ + MiRel r.1 mf ∧ r.1.extra_caps.bv = mf.miExtraCaps ∧ IpcRel N T E R B (L.mem r.2) σ' ∧ + r.1.caps_unwrapped.val < 8 ∧ r.1.extra_caps.val ≤ 3 ∧ mf.miLabel = mi.miLabel ∧ + SRE st N T C E R B c r.2 σ' ∧ (KeepCV σ σ' ∧ WritesIn st s r.2 (FW th B)) ∧ + (∃ G', RelTopB (L := L) th G' N T C E R B c r.2 σ') ∧ + (TrN (L := L) th s σ r.2 σ' ∨ ∃ rb slots, rbo = some rb ∧ + Fl4Spec.getReceiveSlots (Z := Unit) receiver.bv (some rb) σ = ({(slots, σ)}, False) ∧ + TrIV (L := L) th caps slots s σ r.2 σ' ∧ + ∀ d ∈ slots, ∃ pd, d.1 = pd.bv ∧ DestCap th (L.mem s) pd) ∧ + WritesIn st s r.2 (RFoot th rbw) ⦄ := by + have hmi0 : MiRel { label := info.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := info.length } + ⟨mi.miLength, 0, 0, mi.miLabel⟩ := ⟨hil, rfl, hlb⟩ + -- the spec without caps or without a receive buffer + have hnone : rbo = none → Fl4Spec.transferCaps (Z := Unit) mi caps (some ep.bv) receiver.bv rbo σ = + ({(⟨mi.miLength, 0, 0, mi.miLabel⟩, σ)}, False) := by + rintro rfl + unfold Fl4Spec.transferCaps + rw [nd_bind_one (show Fl4Spec.getReceiveSlots (Z := Unit) receiver.bv none σ = ({([], σ)}, False) from rfl), + nd_pure_bind'] + rfl + have hnil : caps = [] → ∀ rb, rbo = some rb → Fl4Spec.transferCaps (Z := Unit) mi caps (some ep.bv) receiver.bv rbo σ = + ({(⟨mi.miLength, 0, 0, mi.miLabel⟩, σ)}, False) := by + rintro rfl rb rfl + obtain ⟨sl, hsl⟩ := hGRs rb rfl + unfold Fl4Spec.transferCaps + rw [nd_bind_one hsl, nd_pure_bind'] + exact tcl_nil _ _ _ + unfold ipc.transfer_caps msginfo.MessageInfo.new + simp only [bind_ok] + rw [index_exAt extra 0#usize (by decide), show ((0#usize : Usize).val) = 0 from rfl] + simp only [bind_ok, obj.Slot.is_null] + by_cases h0 : exAt extra 0 = 0#u64 + · have hcn : caps = [] := by + cases caps with + | nil => rfl + | cons y ys => + exfalso + obtain ⟨ps, rs, idx, cv, hps, hls, -, hxa, -⟩ := hsrc 0 (by simp) + have := (AbsSlot.isSlot (⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ : AbsSlot th (ps.bv, idx))).ne_zero hB.1 + rw [← hxa] at this; exact this h0 + rw [decide_eq_true h0, if_pos rfl, WP.spec_ok] + refine ⟨σ, ⟨mi.miLength, 0, 0, mi.miLabel⟩, ?_, hmi0, rfl, hR, by simp, by simp, rfl, hS, + ⟨KeepCV.refl σ, WritesIn.refl s _⟩, ⟨G, hB⟩, Or.inl trN_refl, WritesIn.refl s _⟩ + cases e : rbo with + | none => rw [← e]; exact hnone e + | some rb => rw [← e]; exact hnil hcn rb e + · rw [decide_eq_false h0, if_neg Bool.false_ne_true] + by_cases hr0 : rbw = 0#u64 + · rw [if_pos hr0, WP.spec_ok] + exact ⟨σ, _, hnone (hrb.1 hr0), hmi0, rfl, hR, by simp, by simp, rfl, hS, ⟨KeepCV.refl σ, WritesIn.refl s _⟩, + ⟨G, hB⟩, Or.inl trN_refl, WritesIn.refl s _⟩ + · rw [if_neg hr0] + obtain ⟨rb, hrbo⟩ : ∃ rb, rbo = some rb := by + cases e : rbo with + | none => exact absurd (hrb.2 e) hr0 + | some rb => exact ⟨rb, rfl⟩ + obtain ⟨hmb, hu⟩ := hm rb hrbo + refine WP.spec_bind (WP.spec_and (hG1 rb hrbo) (hGL rb hrbo)) ?_ + rintro dest ⟨⟨slots, hsl, hD⟩, hL⟩ + have hDL : ∀ d ∈ slots, ∃ pd, d.1 = pd.bv ∧ DestCap th (L.mem s) pd := by + intro d hd + rcases hD with ⟨-, rfl⟩ | ⟨h0, d', rfl, hok⟩ + · simp at hd + obtain rfl := List.mem_singleton.1 hd + obtain ⟨pd, idx, he, hpd, hL'⟩ := hL h0 + obtain ⟨pd0, rd0, idx0, hpd0, hl0, rfl, he0, -⟩ := hok + have e := cslotAddr_inj hB.1 (⟨pd0, rfl, Or.inl (by rw [hl0]; exact hpd0)⟩ : AbsSlot th (pd0.bv, idx0)) + (⟨pd, rfl, Or.inl hpd⟩ : AbsSlot th (pd.bv, idx)) (he0.symm.trans he) + exact ⟨pd, congrArg Prod.fst e, hL'⟩ + refine WP.spec_bind (transfer_loop_topGV hP0 _ hmi0 (by simp) hB hR hS hlen hsrc hnull hD hmb hu) ?_ + rintro ⟨s2, out1, i⟩ ⟨σ', mf, hloop, hmi1, hex, hcu1, hi31, hlb1, hR', hS', hF', hB', htr, hWr⟩ + refine (WP.spec_ok _).2 ⟨σ', mf, ?_, hmi1, ?_, hR', hcu1, hi31, hlb1, hS', hF', hB', + htr.imp_right fun h => ⟨rb, slots, hrbo, hsl, h, hDL⟩, hWr⟩ + swap + · show i.bv = mf.miExtraCaps + rw [hex]; apply BitVec.eq_of_toNat_eq + show i.val = (BitVec.ofNat 64 i.val).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by scalar_tac)] + subst hrbo + unfold Fl4Spec.transferCaps + rw [nd_bind_one hsl, nd_pure_bind'] + exact hloop + +end LoopG + +end + +end Fl4Proofs.P3.Ipc diff --git a/proofs/Fl4Proofs/P3/IrqCtlTrace.lean b/proofs/Fl4Proofs/P3/IrqCtlTrace.lean new file mode 100644 index 000000000..9592b4b94 --- /dev/null +++ b/proofs/Fl4Proofs/P3/IrqCtlTrace.lean @@ -0,0 +1,1243 @@ +/- +P3 (line mmuAD, Stage 3, IrqControl arm): **the IRQ control decode, every label, with the success outcome's trace**. + +* `IrqInvTr`: what `invoke_irq_control` did from `(s, a, σ)`: the IRQ's state word written `Signal`, the mask + (`mask_irq`), and the issued handler cap inserted into an empty CNode slot (the spec's `capInsert` outcome after + `setIrqSt Signal`, the slots' new values, the insert's frame, the destination's liveness, `DelInv` and the MDB + step). +* `IrqIssueAt`: a successful IRQ control invocation from `(s, a, σ)`: the current thread's restart, one arch step + (`ArchIrqStep`: none, or the IOAPIC programming), one spec-only kernel-state step (`KsStep`: the arch IRQ state and + the machine's IOAPIC), then `IrqInvTr`. +* `CPostT`: IrqControl's `CPost` with `IrqIssueAt` at the success outcome. +* `irq_control_decode_refinesT` (every label) and its parts: the proofs of IrqControl's `invoke_ctl_rel`, IrqIssue's + `target_tail` / `issue_tail` / `irq_issue_refines`, IrqArch's `ctl_finish` / `irq_msi_refines` / + `irq_ioapic_refines` / `irq_remap_refines` / `irq_ctl_other_refines` and IrqDecode's `irq_control_decode_refines`, + each success point extended by its trace (generated by `handoff/tools/gen_irqctl.py`). + +What a VM-aware caller needs to frame the VM/EPT state step by step (as IoControl's `IoIssueAt`). New file; no +existing statement changed; no new premise (the hypotheses are those of `irq_control_decode_refines`). +-/ +import Fl4Proofs.P3.IrqDecode +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **What `invoke_irq_control` did** (module docstring). -/ +def IrqInvTr (th : TypedHeap) (G : MdbGhost) (s : S) (a : A) (σ : Fl4Spec.State Unit) + (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) (G' : MdbGhost) : Prop := + ∃ (irq ps pd : U64) (rs rd : Nat) (idxs idxd : List Bool) (b : Bool), + irq.val < 128 ∧ arI.mask_irq a irq b = ok a' ∧ + th ps = some (.cnode rs) ∧ th pd = some (.cnode rd) ∧ idxd.length = rd ∧ + Fl4Spec.capInsert (Z := Unit) (absCap (.IrqHandler irq)) (ps.bv, idxs) (pd.bv, idxd) + (setIrqSt .irqSignal (BitVec.ofNat 8 irq.val) σ) = ({((), σ')}, False) ∧ + (∀ slot, IsSlot th slot → capV (L.mem s') slot = + if slot = cslotAddr (pd.bv, idxd) then .IrqHandler irq + else capV (L.mem (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) slot) ∧ + capAt (L.mem (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) (cslotAddr (pd.bv, idxd)) = ok .Null ∧ + Vs.SlotFrame2 (L := L) th (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL) s' ∧ + DestLive th (L.mem (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) + (L.ksb (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) pd ∧ + (DelInv th (L.mem (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) → DelInv th (L.mem s')) ∧ + MdbStep (L.mem (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL)) G (L.mem s') G' + +/-- **A successful IRQ control invocation's trace** (module docstring). -/ +def IrqIssueAt (th : TypedHeap) (G : MdbGhost) (s : S) (a : A) (σ : Fl4Spec.State Unit) + (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) (G' : MdbGhost) : Prop := + ∃ (ct : U64) (s1 : S) (σ1 : Fl4Spec.State Unit) (a1 : A) (σb : Fl4Spec.State Unit), + σ.curThread = ct.bv ∧ sched.set_thread_state st s ct sched.TS_RESTART = ok s1 ∧ + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ArchIrqStep arI a a1 ∧ KsStep σ1 σb ∧ + σb.domainList = σ1.domainList ∧ σb.domainStartIndex = σ1.domainStartIndex ∧ + (∀ m kb, IrqRel m kb σ1 → IrqRel m kb σb) ∧ (∀ m kb, IoRel m kb σ1 → IoRel m kb σb) ∧ + IrqInvTr (L := L) arI th G s1 a1 σb s' a' σ' G' + +/-- **IRQ control's `CPost` with the trace** (module docstring). -/ +def CPostT {X : Type} (invk : X → Fl4Spec.PMonad Unit Unit) (th : TypedHeap) (G : MdbGhost) (kb : U64) + (σ : Fl4Spec.State Unit) (m : Fl4Spec.SEMonad Unit X) (s : S) (ar : A) : + core.result.Result Unit error.Exc × S × A → Prop + | (.Err e, s', a') => s' = s ∧ (∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∧ a' = ar + | (.Ok (), s', a') => ∃ inv, m.run σ = ({(.ok inv, σ)}, False) ∧ ∃ σ1, + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ' G', (.ok (), σ') ∈ ((invk inv).run σ1).1 ∧ CRel th (L.mem s') σ' G' ∧ IrqRel (L.mem s') kb σ' ∧ + FinInv th (L.mem s') σ' G' ∧ HandlersActive (L.mem s') kb G' ∧ MdbOKV kb (L.mem s') G' ∧ + (DomPlaced (L := L) th s → DK4 (L := L) s s') ∧ (CurRun σ → Steps (L := L) arI th s ar σ s' a' σ') ∧ + IrqIssueAt (L := L) arI th G s ar σ s' a' σ' G' + +variable {arI} in +theorem CPostT.of_run {X : Type} {invk : X → Fl4Spec.PMonad Unit Unit} {th : TypedHeap} {G : MdbGhost} {kb : U64} + {σ : Fl4Spec.State Unit} {m m' : Fl4Spec.SEMonad Unit X} (h : m.run σ = m'.run σ) {s : S} {ar : A} + {res : core.result.Result Unit error.Exc × S × A} (hq : CPostT (L := L) arI invk th G kb σ m' s ar res) : + CPostT (L := L) arI invk th G kb σ m s ar res := by + obtain ⟨r, s', a'⟩ := res + cases r with + | Err e => obtain ⟨h1, ⟨e', h2, h3⟩, h4⟩ := hq; exact ⟨h1, ⟨e', h.trans h2, h3⟩, h4⟩ + | Ok u => obtain ⟨inv, h2, h3⟩ := hq; exact ⟨inv, h.trans h2, h3⟩ + +variable {arI} in +theorem CPostT.congr {X : Type} {invk : X → Fl4Spec.PMonad Unit Unit} {th : TypedHeap} {G : MdbGhost} {kb : U64} + {σ : Fl4Spec.State Unit} {m m' : Fl4Spec.SEMonad Unit X} (h : m.run σ = m'.run σ) {s : S} {ar : A} + {prog : Result (core.result.Result Unit error.Exc × S × A)} + (hp : prog ⦃ res => CPostT (L := L) arI invk th G kb σ m' s ar res ⦄) : + prog ⦃ res => CPostT (L := L) arI invk th G kb σ m s ar res ⦄ := + WP.spec_mono hp (fun _ hq => CPostT.of_run h hq) + +variable {arI} in +theorem CPostT.err {X : Type} {invk : X → Fl4Spec.PMonad Unit Unit} {th : TypedHeap} {G : MdbGhost} {kb : U64} + {σ : Fl4Spec.State Unit} {m : Fl4Spec.SEMonad Unit X} {s : S} {ar : A} {e : error.Exc} {e' : Fl4Spec.SyscallError} + (h : m.run σ = ({(.error e', σ)}, False)) (he : ErrRel e e') : + (ok ((.Err e, s, ar) : core.result.Result Unit error.Exc × S × A)) ⦃ res => + CPostT (L := L) arI invk th G kb σ m s ar res ⦄ := by + rw [WP.spec_ok]; exact ⟨rfl, ⟨e', h, he⟩, rfl⟩ + +/-- **`invoke_irq_control` against the spec's `set_irq_state Signal` and +`cap_insert`**, for an inactive IRQ, with the IRQ control cap at the source +and the destination empty (slots of CNodes). -/ +theorem invoke_ctl_relT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a') + (irq : U64) (hirq : irq.val < 128) (hin : L.mem s (irqStA (L.ksb s) irq) = 0#u64) + {ps pd : U64} {rs rd : Nat} (hps : th ps = some (.cnode rs)) (hpd : th pd = some (.cnode rd)) + {idxs idxd : List Bool} (hls : idxs.length = rs) (hld : idxd.length = rd) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) (hDL : DestLive th (L.mem s) (L.ksb s) pd) : + interrupt.invoke_irq_control st arI s ar irq (cslotAddr (pd.bv, idxd)) (cslotAddr (ps.bv, idxs)) ⦃ r => + ∃ σ' G', ((), σ') ∈ ((Fl4Spec.setIrqState (Z := Unit) .irqSignal (BitVec.ofNat 8 irq.val) >>= fun _ => + Fl4Spec.capInsert (Z := Unit) (.irqHandlerCap (BitVec.ofNat 8 irq.val)) (ps.bv, idxs) (pd.bv, idxd)) σ).1 ∧ + CRel th (L.mem r.1) σ' G' ∧ IrqRel (L.mem r.1) (L.ksb s) σ' ∧ L.ksb r.1 = L.ksb s ∧ + FinInv th (L.mem r.1) σ' G' ∧ HandlersActive (L.mem r.1) (L.ksb s) G' ∧ MdbOKV (L.ksb s) (L.mem r.1) G' ∧ + Steps (L := L) arI th s ar σ r.1 r.2 σ' ∧ IrqInvTr (L := L) arI th G s ar σ r.1 r.2 σ' G' ⦄ := by + unfold interrupt.invoke_irq_control + obtain ⟨a1, ha1⟩ := hmask ar irq (decide (interrupt.IRQ_SIGNAL = interrupt.IRQ_INACTIVE)) + rw [set_irq_state_eq arI s ar _ irq ha1] + simp only [bind_ok] + set s1 := L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL with hs1 + have hS := IntLayout.slotsSame (L := L) hP hL s hirq interrupt.IRQ_SIGNAL + rw [← hs1] at hS + set σ1 := setIrqSt .irqSignal (BitVec.ofNat 8 irq.val) σ with hσ1 + have hCR1 : CRel th (L.mem s1) σ1 G := crel_same hCR hS rfl rfl rfl + have hsig : irqSt interrupt.IRQ_SIGNAL = .irqSignal := by simp [irqSt, interrupt.IRQ_SIGNAL] + have hIR1 : IrqRel (L.mem s1) (L.ksb s) σ1 := + IrqRel.wr hL hIR hirq _ σ1 (by rw [hsig]; rfl) rfl + have hM := hCR.2.2.1 + obtain ⟨hR1, hV1, hM1, hC1, hI1⟩ := hCR1 + have hfr := fresh_of_inactive hHA hin + have hfr1 : ∀ y ∈ G.chains.flatten, srB (.IrqHandler irq) (capV (L.mem s1) y) = false := by + intro y hy; rw [hS.capV ((hM.members y).1 hy).1]; exact hfr y hy + have hAs : AbsSlot th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hsrc1 : capAt (L.mem s1) (cslotAddr (ps.bv, idxs)) = ok .IrqControl := by rw [hS.capAt hAs.isSlot, hsrcC] + have hdst1 : capAt (L.mem s1) (cslotAddr (pd.bv, idxd)) = ok .Null := by rw [hS.capAt hAd.isSlot, hdst] + have hFI1 : FinInv th (L.mem s1) σ1 G := hFI.of_same hM hS (KSub.of_kheap rfl) + have hw1 : ∀ i : U64, i.val < 128 → L.mem s1 (irqStA (L.ksb s) i) = + if i = irq then interrupt.IRQ_SIGNAL else L.mem s (irqStA (L.ksb s) i) := by + intro i hi + rw [hs1, L.mem_wr _ _ _ _ (hL.st_out irq hirq).1 (hL.st_out i hi).1] + by_cases e : i = irq + · subst e; simp + · rw [if_neg (fun h => e (irqStA_inj hL.fit hirq hi h).symm), if_neg e] + have hHA1 : HandlersActive (L.mem s1) (L.ksb s) G := by + intro y hy i hi + rw [hS.capV ((hM.members y).1 hy).1] at hi + obtain ⟨h1, h2⟩ := hHA y hy i hi + refine ⟨h1, ?_⟩ + rw [hw1 i h1] + split_ifs + · simp [interrupt.IRQ_SIGNAL] + · exact h2 + have hin1 : ∀ y ∈ G.chains.flatten, ¬ Ovl (absCap (.IrqHandler irq)) (absCap (capV (L.mem s1) y)) := by + intro y hy; rw [hS.capV ((hM.members y).1 hy).1]; exact ovl_fresh_irq hHA hirq hin y hy + have hins := insert_ctl_full_fin th s1 σ1 G hR1 hV1 hP hM1 hC1 hI1 hFI1 hps hpd hls hld hsrc1 hdst1 + (.IrqHandler irq) (show irq.val < 128 by omega) trivial (by simp [EncWf]) trivial hfr1 hin1 + have hnx : nextOf (L.mem s1) (cslotAddr (ps.bv, idxs)) = 0#u64 ∨ + IsSlot th (nextOf (L.mem s1) (cslotAddr (ps.bv, idxs))) := by + have hsm : cslotAddr (ps.bv, idxs) ∈ G.chains.flatten := (hM1.members _).2 ⟨hAs.isSlot, _, hsrc1, by simp⟩ + rcases hM1.next_mem hsm with h | ⟨h, -⟩ + · exact Or.inl h + · exact Or.inr ((hM1.members _).1 h).1 + have hfrm := insert_frame hP s1 (.IrqHandler irq) _ _ hAs.isSlot hAd.isSlot _ hsrc1 hnx + refine WP.spec_bind (WP.spec_and (WP.spec_and (WP.spec_and hins hfrm) + (Vs.insert_frame2 hP s1 (.IrqHandler irq) _ _ hAs.isSlot hAd.isSlot _ hsrc1 hnx)) + (insert_delinv_imp hP s1 G hM1 ((hM1.members _).2 ⟨hAs.isSlot, _, hsrc1, by simp⟩) hAd.isSlot + (capV_null_not_mem hM1 (capV_of_capAt hdst1)) hsrc1 (.IrqHandler irq) (by simp [EncWf]) (by simp) rfl)) ?_ + rintro s2 ⟨⟨⟨⟨σ2, G2, hci, hR2, hV2, hM2, hC2, hI2, hFI2, hcap2, ⟨bG2, hG2e⟩, hSt2⟩, hF2⟩, hF2'⟩, hdi⟩ + rw [WP.spec_ok] + have hkeep := capInsert_keeps (absCap (.IrqHandler irq)) (ps.bv, idxs) (pd.bv, idxd) σ1 ((), σ2) + (by rw [hci]; rfl) + have hkb2 : L.ksb s2 = L.ksb s := by rw [hF2.1]; simp [s1] + have hFRG : MdbOKV (L.ksb s) (L.mem s2) G2 := (MdbStep.same_left hM hS hSt2).okv hFR + have hk1 : L.ksb s1 = L.ksb s := by simp [s1] + have hstp : Steps (L := L) arI th s ar σ s2 a1 σ2 := + Steps.trans arI (Steps.irqWr arI hL hirq interrupt.IRQ_SIGNAL ha1 .irqSignal hsig) + (Steps.insert arI (hnk := by rw [hcap2 _ hAd.isSlot, if_pos rfl]; rfl) hP hps hpd hld hci hF2' ⟨hR2, hV2, hM2, hC2, hI2⟩ hFI2 (by rw [hk1]; exact hFRG) + (fun y hy hne => by rw [hcap2 y hy, if_neg hne]) hdst1 + (fun p sb fi d h => by rw [hcap2 _ hAd.isSlot, if_pos rfl] at h; cases h) + (typedKept_set hcap2 (Or.inl rfl)) (hDL.same hS hk1) hdi + (fun _ _ _ _ _ _ => by rw [hcap2 _ hAd.isSlot, if_pos rfl]; trivial) + (fun _ _ => by rw [hcap2 _ hAd.isSlot, if_pos rfl]; trivial) + (fun _ => by rw [hcap2 _ hAd.isSlot, if_pos rfl]; trivial) + (fun _ _ => by rw [hcap2 _ hAd.isSlot, if_pos rfl]; trivial) + (by rw [hcap2 _ hAd.isSlot, if_pos rfl]; exact fun h => by cases h) + (fun _ _ _ h => by rw [hcap2 _ hAd.isSlot, if_pos rfl] at h; cases h)) + refine ⟨σ2, G2, ?_, ⟨hR2, hV2, hM2, hC2, hI2⟩, ⟨fun i hi => ?_, fun i hi => ?_⟩, hkb2, hFI2, ?_, hFRG, hstp, + ⟨irq, ps, pd, rs, rd, idxs, idxd, _, hirq, ha1, hps, hpd, hld, hci, hcap2, hdst1, hF2', hDL.same hS hk1, hdi, hSt2⟩⟩ + rotate_right + · intro y hy i hi + have hys := ((hM2.members y).1 hy).1 + rw [hcap2 y hys] at hi + rw [hF2.out (hL.st_out i ?_)] + · split_ifs at hi with hyd + · cases hi; refine ⟨hirq, ?_⟩; rw [hw1 irq hirq, if_pos rfl]; simp [interrupt.IRQ_SIGNAL] + · have hyn : capV (L.mem s1) y ≠ .Null := by rw [hi]; simp + have hym : y ∈ G.chains.flatten := (hM1.members y).2 ⟨hys, _, capAt_capV _ _, hyn⟩ + exact hHA1 y hym i hi + · split_ifs at hi with hyd + · cases hi; exact hirq + · have hyn : capV (L.mem s1) y ≠ .Null := by rw [hi]; simp + have hym : y ∈ G.chains.flatten := (hM1.members y).2 ⟨hys, _, capAt_capV _ _, hyn⟩ + exact (hHA1 y hym i hi).1 + · refine mem_bind_nd (x := ((), σ1)) (setIrqState_mem' _ _ _) ?_ + show ((), σ2) ∈ (Fl4Spec.capInsert (absCap (.IrqHandler irq)) (ps.bv, idxs) (pd.bv, idxd) σ1).1 + rw [hci]; rfl + · rw [hkeep.1, hF2.out (hL.st_out i hi)]; exact hIR1.st i hi + · rw [hkeep.2.1]; exact hIR1.node i hi + +/-- **The target slot**: `lookup_target_slot` then `ensure_empty_slot` +against the spec's `lookup_target_slot` and `ensure_empty`, for any +continuation. -/ +theorem target_tailT {X : Type} (invk : X → Fl4Spec.PMonad Unit Unit) (th : TypedHeap) {G : MdbGhost} (kb : U64) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) (hR : StateRel th (L.mem s) σ) (hV : SlotsValid th (L.mem s)) + (c : capability.Cap) (ac : Fl4Spec.Cap) (hca : CapRel c ac) (hcv : CapValid th c) (idx dep : U64) + (ks : Fl4Spec.CslotPtr → Fl4Spec.SEMonad Unit X) + (K : obj.Slot → Result (core.result.Result Unit error.Exc × S × A)) + (hK : ∀ dsl pd, dsl.1 = pd.bv → th pd = some (.cnode dsl.2.length) → CnRef th (L.mem s) c pd → + capAt (L.mem s) (cslotAddr dsl) = ok .Null → + K (cslotAddr dsl) ⦃ res => CPostT (L := L) arI invk th G kb σ (ks dsl) s ar res ⦄) : + (do + let r ← cspace.lookup_target_slot st s c idx dep + 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, ar) + | 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, ar)) ⦃ res => + CPostT (L := L) arI invk th G kb σ (Fl4Spec.lookupTargetSlot ac (Fl4Spec.dataToCptr idx.bv) (Fl4Spec.unat dep.bv) >>= + fun d => Fl4Spec.ensureEmpty d >>= fun _ => ks d) s ar res ⦄ := by + unfold cspace.lookup_target_slot + obtain ⟨r1, hr1, e1, he1, hrel1⟩ := spec_exists + (lookup_slot_for_cnode_op_refines (Z := Unit) th s σ hR hV false c ac hca hcv idx dep) + rw [hr1]; simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + have hlt : (Fl4Spec.lookupTargetSlot (Z := Unit) ac (Fl4Spec.dataToCptr idx.bv) (Fl4Spec.unat dep.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_traitFromResidualResult.from_residual, + core.convert.FromSame, core.convert.FromSame.from, bind_ok, bind_tc_ok] + exact CPostT.err (run_bind_error hlt _) (Or.inl hrel1) + | Ok dest => + cases e1 with + | error _ => exact hrel1.elim + | ok dsl => + obtain ⟨rfl, pd, hpd1, hpd, hpdr⟩ := hrel1 + obtain ⟨cd, ad, hcd, hcad, hgd⟩ := hR.slot_cap (Z := Unit) hpd1 hpd + unfold cdt.ensure_empty_slot + simp only [slot_cap_eq, hcd, bind_ok] + have hee := ensureEmpty_run dsl σ hgd + refine CPostT.congr (run_bind_ok hlt _) ?_ + by_cases hdn : cd = .Null + swap + · rw [cap_is_null_ne 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 : ad ≠ .nullCap := fun h => hdn ((hcad.null_iff).1 h) + simp only [this, ↓reduceIte] at hee + exact CPostT.err (run_bind_error hee _) (Or.inl trivial) + subst hdn + have hadn : ad = .nullCap := (hcad.null_iff).2 rfl + simp only [hadn, ↓reduceIte] at hee + rw [cap_is_null_null] + simp only [bind_ok, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch] + refine CPostT.congr (run_bind_ok hee _) ?_ + exact hK dsl pd hpd1 hpd hpdr hcd + +variable {arI} in +theorem CPostT.liftME {X Y : Type} {f : X → Y} {invk : Y → Fl4Spec.PMonad Unit Unit} {th : TypedHeap} {G : MdbGhost} {kb : U64} + {σ : Fl4Spec.State Unit} {m : Fl4Spec.SEMonad Unit X} {s : S} {ar : A} {res : core.result.Result Unit error.Exc × S × A} + (hq : CPostT (L := L) arI (invk ∘ f) th G kb σ m s ar res) : + CPostT (L := L) arI invk th G kb σ (Fl4Spec.NonDetE.liftME f m) s ar res := by + obtain ⟨r, s', a'⟩ := res + cases r with + | Err e => obtain ⟨h1, ⟨e', h2, h3⟩, h4⟩ := hq; exact ⟨h1, ⟨e', run_liftME_err h2, h3⟩, h4⟩ + | Ok u => obtain ⟨inv, h2, h3⟩ := hq; exact ⟨f inv, run_liftME_ok h2, h3⟩ + +variable {arI} in +theorem CPostT.liftME' {X Y : Type} {f : X → Y} {invk : Y → Fl4Spec.PMonad Unit Unit} {th : TypedHeap} {G : MdbGhost} {kb : U64} + {σ : Fl4Spec.State Unit} {m : Fl4Spec.SEMonad Unit X} {s : S} {ar : A} + {prog : Result (core.result.Result Unit error.Exc × S × A)} + (hp : prog ⦃ res => CPostT (L := L) arI (invk ∘ f) th G kb σ m s ar res ⦄) : + prog ⦃ res => CPostT (L := L) arI invk th G kb σ (Fl4Spec.NonDetE.liftME f m) s ar res ⦄ := + WP.spec_mono hp (fun _ hq => CPostT.liftME hq) + +/-- After a successful decode: `set_thread_state Restart` then +`invoke_irq_control`, against the spec's (`invoke_irq_control` of +`IRQControl`), for an inactive IRQ. -/ +theorem issue_tailT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a') + (ct : U64) (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i) + (irq : U64) (hirq : irq.val < 128) (hin : L.mem s (irqStA (L.ksb s) irq) = 0#u64) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (dsl : Fl4Spec.CslotPtr) (pd : U64) (hpd1 : dsl.1 = pd.bv) (hpd : th pd = some (.cnode dsl.2.length)) + (hdst : capAt (L.mem s) (cslotAddr dsl) = ok .Null) (hDL : DestLive th (L.mem s) (L.ksb s) pd) : + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let (s2, a1) ← interrupt.invoke_irq_control st arI s1 ar irq (cslotAddr dsl) (cslotAddr (ps.bv, idxs)) + ok (core.result.Result.Ok (), s2, a1)) ⦃ res => + CPostT (L := L) arI (Fl4Spec.invokeFl4IrqControl ∘ Fl4Spec.Fl4IrqControlInvocation.spec) th G (L.ksb s) σ + (Fl4Spec.NonDetE.returnOk (.irqControl (BitVec.ofNat 8 irq.val) dsl (ps.bv, idxs))) s ar res ⦄ := by + obtain ⟨da, db⟩ := dsl + simp only at hpd1 hpd; subst hpd1 + have hrs := restart_irq th hP s σ G hCR hL hIR ct hcur htcb hact hact2 + refine WP.spec_bind (WP.spec_and (WP.spec_and hrs (sts_dk4c hP s htcb sched.TS_RESTART)) (spec_self hrs)) ?_ + rintro s1 ⟨⟨⟨hkb, hS, σ1, hst, hCR1, hIR1, -, -, hw⟩, hdk1⟩, hs1⟩ + obtain ⟨xct, hkct, -⟩ := hCR.1.tcb ct htcb + have hstp1 : CurRun σ → Steps (L := L) arI th s ar σ s1 ar σ1 := Steps.restart arI hcur hkct htcb hs1 hst + have hL1 : IntLayout th (L.ksb s1) := by rw [hkb]; exact hL + have hIR1' : IrqRel (L.mem s1) (L.ksb s1) σ1 := by rw [hkb]; exact hIR1 + have hHA1 : HandlersActive (L.mem s1) (L.ksb s1) G := by + intro y hy i hi + rw [hS.capV ((hCR.2.2.1.members y).1 hy).1] at hi + obtain ⟨h1, h2⟩ := hHA y hy i hi + rw [hkb, hw i h1]; exact ⟨h1, h2⟩ + have hin1 : L.mem s1 (irqStA (L.ksb s1) irq) = 0#u64 := by rw [hkb, hw irq hirq, hin] + have hFI1 : FinInv th (L.mem s1) σ1 G := hFI.of_same hCR.2.2.1 hS (ksub_of_eq (setThreadState_nks _ _) hst) + have hAs : AbsSlot th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot th (pd.bv, db) := ⟨pd, rfl, Or.inl hpd⟩ + have hsrc1 : capAt (L.mem s1) (cslotAddr (ps.bv, idxs)) = ok .IrqControl := by rw [hS.capAt hAs.isSlot, hsrcC] + have hdst1 : capAt (L.mem s1) (cslotAddr (pd.bv, db)) = ok .Null := by rw [hS.capAt hAd.isSlot, hdst] + have hFR1 : MdbOKV (L.ksb s1) (L.mem s1) G := by rw [hkb]; exact MdbOKV.of_same hCR.2.2.1 hS hFR + refine WP.spec_bind (WP.spec_and (invoke_ctl_relT arI th hP s1 ar σ1 G hCR1 hL1 hIR1' hHA1 hFI1 hFR1 hmask irq hirq + hin1 hps hpd hls rfl hsrc1 hdst1 (hDL.same hS hkb)) + (invoke_ctl_dk4c arI hP s1 ar hL1 hCR1.2.2.1 hmask irq hirq hAs.isSlot hAd.isSlot hsrc1)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨σ', G', hmem, hCR', hIR', -, hFI', hHA', hFR', hstp2, htr⟩, hdk2⟩ + apply (WP.spec_ok (core.result.Result.Ok (), s2, a2)).mpr + refine ⟨.irqControl (BitVec.ofNat 8 irq.val) (pd.bv, db) (ps.bv, idxs), rfl, σ1, hst, σ', G', ?_, hCR', ?_, + hFI', ?_, by rw [← hkb]; exact hFR', fun hD => (hdk1 hD).trans (hdk2 (hD.toK.same (hdk1 hD).1)), + fun hC => (hstp1 hC).trans arI hstp2, + ⟨ct, s1, σ1, ar, σ1, hcur, hs1, hst, Or.inl rfl, KsStep.refl _, rfl, rfl, fun _ _ h => h, fun _ _ h => h, htr⟩⟩ + · exact mem_liftE (x := ((), σ')) hmem + · rw [← hkb]; exact hIR' + · rw [← hkb]; exact hHA' + +/-- **`IRQIssueIRQHandler`** (label 33). -/ +theorem irq_issue_refinesT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .irqIssueIRQHandler) + (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) + (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel th (L.mem s) x0 excaps) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (nA : U64) (hnum : arI.num_ioapics ar = ok nA) (hnA : nA.bv = σ.archState.x64NumIoapics) + (hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a') : + interrupt.decode_irq_control st arI s ar fl4_abi.label.IRQIssueIRQHandler length (cslotAddr (ps.bv, idxs)) + buf extra ⦃ res => + CPostT (L := L) arI Fl4Spec.invokeFl4IrqControl th G (L.ksb s) σ + (Fl4Spec.decodeFl4IrqControl false slbl (args.map UScalar.bv) (ps.bv, idxs) excaps) s ar res ⦄ := by + obtain ⟨hR, hV, hM, hC, hI⟩ := hCR + unfold interrupt.decode_irq_control + rw [if_pos rfl] + unfold interrupt.decode_issue_isa interrupt.extra0 + rw [hx0]; simp only [bind_ok, is_null_eq] + unfold Fl4Spec.decodeFl4IrqControl + simp only [Bool.false_eq_true, ↓reduceIte] + refine CPostT.liftME' ?_ + unfold Fl4Spec.decodeIrqControlInvocationWith + rw [if_pos hlbl] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, c0, ac0, erest, hc0, hca0, hcv0, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 3 ∧ ([] : List Fl4Spec.Cap).length ≥ 1) by simp)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, slot_cap_eq, hc0, bind_ok] + by_cases h3 : length >= 3#u64 + swap + · rw [if_neg h3]; simp only [error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 3 ∧ (ac0 :: erest).length ≥ 1) by simp; scalar_tac)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + rw [if_pos h3] + obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, args = y0 :: y1 :: y2 :: rest := by + match args with + | [] | [_] | [_, _] => exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: y2 :: rest => exact ⟨y0, y1, y2, rest, rfl⟩ + rw [if_pos (show ((y0 :: y1 :: y2 :: rest).map UScalar.bv).length ≥ 3 ∧ (ac0 :: erest).length ≥ 1 by simp)] + simp only [hct, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), hargs 2#u64 y2 (by simp), hnum, bind_ok, + List.map_cons] + simp only [Fl4Spec.the, List.getElem?_cons_zero, List.getElem?_cons_succ] + -- `arch_check_irq` + have hck : ∀ (k : Unit → Fl4Spec.SEMonad Unit Fl4Spec.IrqControlInvocation), + (Fl4Spec.fl4ArchCheckIrq (Z := Unit) y0.bv >>= k).run σ = + ((if nA.bv = 0 then Fl4Spec.NonDetE.whenE (y0.bv > 15) (Fl4Spec.NonDetE.throwError (.rangeError 0 15)) + else Fl4Spec.NonDetE.throwError .illegalOperation) >>= k).run σ := by + intro k + unfold Fl4Spec.fl4ArchCheckIrq + rw [rbind_assoc, run_bind_ok (run_liftE + (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.archState.x64NumIoapics) rfl), hnA] + refine CPostT.congr (hck _) ?_ + by_cases hn0 : nA = 0#u64 + swap + · have : (nA != 0#u64) = true := by simpa using hn0 + simp only [this, ↓reduceIte, error.illegal, bind_ok] + have hn0' : ¬ nA.bv = 0 := fun h => hn0 (by rw [UScalar.eq_equiv_bv_eq]; exact h) + rw [if_neg hn0'] + exact CPostT.err (e' := .illegalOperation) (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : Fl4Spec.SEMonad Unit Unit)) rfl _) + (Or.inl trivial) + subst hn0 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [if_pos (show (0#u64 : U64).bv = 0 from rfl)] + have hISA : layout.irq.ISA_MAX = 15#u64 := by unfold layout.irq.ISA_MAX; rfl + have hISA0 : layout.irq.ISA_MIN = 0#u64 := by unfold layout.irq.ISA_MIN; rfl + by_cases hr : y0 > layout.irq.ISA_MAX + · rw [if_pos hr]; simp only [error.range, bind_ok] + have : y0.bv > 15 := (bv_gt_iff y0 15 (by norm_num)).2 (by rw [hISA] at hr; scalar_tac) + rw [whenE_pos this] + exact CPostT.err (e' := .rangeError 0 15) (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 15) : Fl4Spec.SEMonad Unit Unit)) rfl _) + (Or.inl ⟨by rw [hISA0]; rfl, by rw [hISA]; rfl⟩) + rw [if_neg hr] + have hy15 : y0.val ≤ 15 := by rw [hISA] at hr; scalar_tac + have : ¬ y0.bv > 15 := fun h => by have := (bv_gt_iff y0 15 (by norm_num)).1 h; omega + rw [whenE_neg this] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IRQ must be inactive + rw [is_irq_active_eq]; simp only [bind_ok] + rw [ucast8_ofNat] + refine CPostT.congr (run_bind_ok (run_liftE (isIrqActive_run hIR (by omega))) _) ?_ + by_cases hact0 : L.mem s (irqStA (L.ksb s) y0) = 0#u64 + swap + · simp only [hact0, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + rw [whenE_pos (by simpa using hact0)] + exact CPostT.err (e' := .revokeFirst) (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.revokeFirst : Fl4Spec.SEMonad Unit Unit)) rfl _) + (Or.inl trivial) + simp only [hact0, ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [whenE_neg (by simp [hact0])] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + refine target_tailT arI (Fl4Spec.invokeFl4IrqControl ∘ Fl4Spec.Fl4IrqControlInvocation.spec) th (L.ksb s) s ar σ + hR hV c0 ac0 hca0 hcv0 y1 y2 + (fun d => Fl4Spec.NonDetE.returnOk (.irqControl (BitVec.ofNat 8 y0.val) d (ps.bv, idxs))) _ ?_ + intro dsl pd hpd1 hpd hpdr hdst + exact issue_tailT arI th hP s ar σ G ⟨hR, hV, hM, hC, hI⟩ hL hIR hHA hFI hFR hmask ct hcur htcb hact hact2 y0 (by omega) + hact0 hps hls hsrcC dsl pd hpd1 hpd hdst (DestLive.of_cnRef hpdr ⟨x0, hx0s, capV_of_capAt hc0⟩) + +/-- After `set_thread_state Restart` (`restart_irq`) and spec steps outside +the CSpace and the interrupt state: `invoke_irq_control`. -/ +theorem ctl_finishT (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hHA : HandlersActive (L.mem s) (L.ksb s) G) + (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a') + (irq : U64) (hirq : irq.val < 128) (hin : L.mem s (irqStA (L.ksb s) irq) = 0#u64) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (dsl : Fl4Spec.CslotPtr) (pd : U64) (hpd1 : dsl.1 = pd.bv) (hpd : th pd = some (.cnode dsl.2.length)) + (hdst : capAt (L.mem s) (cslotAddr dsl) = ok .Null) (hDL : DestLive th (L.mem s) (L.ksb s) pd) + (s1 : S) (σ1 : Fl4Spec.State Unit) (hkb : L.ksb s1 = L.ksb s) (hS : SlotsSame th (L.mem s) (L.mem s1)) + (hCR1 : CRel th (L.mem s1) σ1 G) (hIR1 : IrqRel (L.mem s1) (L.ksb s) σ1) + (hw : ∀ i : U64, i.val < 128 → L.mem s1 (irqStA (L.ksb s) i) = L.mem s (irqStA (L.ksb s) i)) + (hK1 : KSub σ σ1) (a1 : A) (σ1' : Fl4Spec.State Unit) (hk : σ1'.kheap = σ1.kheap) (hc : σ1'.cdt = σ1.cdt) + (ho : σ1'.isOriginalCap = σ1.isOriginalCap) (hs : σ1'.interruptStates = σ1.interruptStates) + (hn : σ1'.interruptIrqNode = σ1.interruptIrqNode) : + interrupt.invoke_irq_control st arI s1 a1 irq (cslotAddr dsl) (cslotAddr (ps.bv, idxs)) ⦃ r => + ∃ σ' G', ((), σ') ∈ ((Fl4Spec.setIrqState (Z := Unit) .irqSignal (BitVec.ofNat 8 irq.val) >>= fun _ => + Fl4Spec.capInsert (Z := Unit) (.irqHandlerCap (BitVec.ofNat 8 irq.val)) (ps.bv, idxs) dsl) σ1').1 ∧ + CRel th (L.mem r.1) σ' G' ∧ IrqRel (L.mem r.1) (L.ksb s) σ' ∧ + FinInv th (L.mem r.1) σ' G' ∧ HandlersActive (L.mem r.1) (L.ksb s) G' ∧ MdbOKV (L.ksb s) (L.mem r.1) G' ∧ + Steps (L := L) arI th s1 a1 σ1' r.1 r.2 σ' ∧ IrqInvTr (L := L) arI th G s1 a1 σ1' r.1 r.2 σ' G' ⦄ := by + obtain ⟨da, db⟩ := dsl + simp only at hpd1 hpd; subst hpd1 + obtain ⟨hCR1', hIR1'⟩ := crel_irq_of hCR1 hIR1 hk hc ho hs hn + have hL1 : IntLayout th (L.ksb s1) := by rw [hkb]; exact hL + have hIR2 : IrqRel (L.mem s1) (L.ksb s1) σ1' := by rw [hkb]; exact hIR1' + have hHA1 : HandlersActive (L.mem s1) (L.ksb s1) G := by + intro y hy i hi + rw [hS.capV ((hCR.2.2.1.members y).1 hy).1] at hi + obtain ⟨h1, h2⟩ := hHA y hy i hi + rw [hkb, hw i h1]; exact ⟨h1, h2⟩ + have hin1 : L.mem s1 (irqStA (L.ksb s1) irq) = 0#u64 := by rw [hkb, hw irq hirq, hin] + have hAs : AbsSlot th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot th (pd.bv, db) := ⟨pd, rfl, Or.inl hpd⟩ + have hsrc1 : capAt (L.mem s1) (cslotAddr (ps.bv, idxs)) = ok .IrqControl := by rw [hS.capAt hAs.isSlot, hsrcC] + have hdst1 : capAt (L.mem s1) (cslotAddr (pd.bv, db)) = ok .Null := by rw [hS.capAt hAd.isSlot, hdst] + have hFI1 : FinInv th (L.mem s1) σ1' G := hFI.of_same hCR.2.2.1 hS (hK1.trans (KSub.of_kheap hk)) + have hFR1 : MdbOKV (L.ksb s1) (L.mem s1) G := by rw [hkb]; exact MdbOKV.of_same hCR.2.2.1 hS hFR + refine WP.spec_mono (invoke_ctl_relT arI th hP s1 a1 σ1' G hCR1' hL1 hIR2 hHA1 hFI1 hFR1 hmask irq hirq hin1 hps hpd + hls rfl hsrc1 hdst1 (hDL.same hS hkb)) ?_ + rintro ⟨s2, a2⟩ ⟨σ', G', hmem, hCR', hIR', -, hFI', hHA', hFR', hstp, htr⟩ + exact ⟨σ', G', hmem, hCR', by rw [← hkb]; exact hIR', hFI', by rw [← hkb]; exact hHA', by rw [← hkb]; exact hFR', + hstp, htr⟩ + +/-- **`X86IRQIssueIRQHandlerMSI`** (label 71). -/ +theorem irq_msi_refinesT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.invocationType slbl = .archInvocationLabel .x64IRQIssueIRQHandlerMSI) + (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) + (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel th (L.mem s) x0 excaps) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a') : + interrupt.decode_irq_control st arI s ar fl4_abi.label.X86IRQIssueIRQHandlerMSI length + (cslotAddr (ps.bv, idxs)) buf extra ⦃ res => + CPostT (L := L) arI Fl4Spec.invokeFl4IrqControl th G (L.ksb s) σ + (Fl4Spec.decodeFl4IrqControl false slbl (args.map UScalar.bv) (ps.bv, idxs) excaps) s ar res ⦄ := by + obtain ⟨hR, hV, hM, hC, hI⟩ := hCR + unfold interrupt.decode_irq_control + have n1 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerMSI = fl4_abi.label.IRQIssueIRQHandler := by + rw [u64_eq_iff]; simp + have n2 : (fl4_abi.label.X86IRQIssueIRQHandlerMSI != fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC) = true := by + rw [bne_iff_ne, ne_eq, u64_eq_iff]; simp + have n3 : ¬ ((fl4_abi.label.X86IRQIssueIRQHandlerMSI != fl4_abi.label.X86IRQIssueIRQHandlerMSI) = true) := by + simp + have n4 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerMSI = fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped := by + rw [u64_eq_iff]; simp + rw [if_neg n1, if_pos n2, if_neg n3] + simp only [n4, ↓reduceIte, bind_ok] + unfold interrupt.extra0; rw [hx0]; simp only [bind_ok, is_null_eq] + -- the spec's decode + unfold Fl4Spec.decodeFl4IrqControl + simp only [Bool.false_eq_true, ↓reduceIte] + refine CPostT.liftME' ?_ + unfold Fl4Spec.decodeIrqControlInvocationWith + rw [if_neg (show ¬ Fl4Spec.genInvocationType slbl = .irqIssueIRQHandler by rw [invocationType_gen hlbl]; decide)] + refine CPostT.liftME' ?_ + unfold Fl4Spec.archDecodeIrqControlInvocation + rw [if_neg (show ¬ Fl4Spec.invocationType slbl = .archInvocationLabel .x64IRQIssueIRQHandlerIOAPIC by + rw [hlbl]; decide), if_pos hlbl] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, c0, ac0, erest, hc0, hca0, hcv0, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 7 ∧ ([] : List Fl4Spec.Cap).length ≥ 1) by simp)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, slot_cap_eq, hc0, bind_ok] + by_cases h7 : length >= 7#u64 + swap + · rw [if_neg h7]; simp only [error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 7 ∧ (ac0 :: erest).length ≥ 1) by simp; scalar_tac)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + rw [if_pos h7] + obtain ⟨y0, y1, y2, y3, y4, y5, y6, rest, rfl⟩ : + ∃ y0 y1 y2 y3 y4 y5 y6 rest, args = y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest := by + match args with + | [] | [_] | [_, _] | [_, _, _] | [_, _, _, _] | [_, _, _, _, _] | [_, _, _, _, _, _] => + exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest => exact ⟨y0, y1, y2, y3, y4, y5, y6, rest, rfl⟩ + rw [if_pos (show ((y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest).map UScalar.bv).length ≥ 7 ∧ + (ac0 :: erest).length ≥ 1 by simp)] + simp only [hct, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), hargs 6#u64 y6 (by simp), bind_ok, + user_max_eq, user_min_eq, sub_107] + have hmap : List.map UScalar.bv (y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest) = + y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv := rfl + rw [hmap] + have g2 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[2]? = some y2.bv := rfl + have g3 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[3]? = some y3.bv := rfl + have g4 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[4]? = some y4.bv := rfl + have g6 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[6]? = some y6.bv := rfl + have g0 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[0]? = some y0.bv := rfl + have g1 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[1]? = some y1.bv := rfl + have g5 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[5]? = some y5.bv := rfl + have ge : (ac0 :: erest)[0]? = some ac0 := rfl + rw [g0, g1, g2, g3, g4, g5, g6, ge] + repeat rw [the_some] + rw [maxmin_eq] + by_cases hr : y6 > 107#u64 + · rw [if_pos hr]; simp only [error.range, bind_ok] + rw [whenE_pos ((bv_gt_iff y6 107 (by norm_num)).2 (by scalar_tac))] + exact CPostT.err (e' := .rangeError 0 (BitVec.ofNat 64 107)) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 (BitVec.ofNat 64 107)) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hr] + have hy6 : y6.val ≤ 107 := by scalar_tac + rw [whenE_neg (fun h => by have := (bv_gt_iff y6 107 (by norm_num)).1 h; omega)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IRQ + obtain ⟨z, hz, hzv⟩ := spec_exists (U64.add_spec (x := y6) (y := 16#u64) (by scalar_tac)) + rw [hz]; simp only [bind_ok] + rw [irqv_eq y6 hy6, show y6.val + 16 = z.val by rw [hzv]; rfl] + have hz128 : z.val < 128 := by rw [hzv]; simp; omega + rw [is_irq_active_eq]; simp only [bind_ok] + refine CPostT.congr (run_bind_ok (run_liftE (isIrqActive_run hIR hz128)) _) ?_ + by_cases hact0 : L.mem s (irqStA (L.ksb s) z) = 0#u64 + swap + · simp only [hact0, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + rw [whenE_pos (by simpa using hact0)] + exact CPostT.err (e' := .revokeFirst) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.revokeFirst : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl trivial) + simp only [hact0, ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [whenE_neg (by simp [hact0])] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + refine target_tailT arI _ th (L.ksb s) s ar σ hR hV c0 ac0 hca0 hcv0 y0 y1 _ _ ?_ + intro dsl pd hpd1 hpd hpdr hdst + try rw [if_pos rfl] + unfold interrupt.check_msi + simp only [hct, hargs 2#u64 y2 (by simp), hargs 3#u64 y3 (by simp), hargs 4#u64 y4 (by simp), bind_ok] + have hB : interrupt.PCI_BUS_MAX = 255#u64 := by unfold interrupt.PCI_BUS_MAX; rfl + have hD : interrupt.PCI_DEV_MAX = 31#u64 := by unfold interrupt.PCI_DEV_MAX; rfl + have hF : interrupt.PCI_FUNC_MAX = 7#u64 := by unfold interrupt.PCI_FUNC_MAX; rfl + have mB : Fl4Spec.maxPCIBus = BitVec.ofNat 64 255 := rfl + have mD : Fl4Spec.maxPCIDev = BitVec.ofNat 64 31 := rfl + have mF : Fl4Spec.maxPCIFunc = BitVec.ofNat 64 7 := rfl + rw [hB, hD, hF] + by_cases e2 : y2 > 255#u64 + · rw [if_pos e2] + simp only [error.range, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok, uncurry_apply_pair] + rw [whenE_pos (by rw [mB]; exact (bv_gt_iff y2 255 (by norm_num)).2 (by scalar_tac))] + exact CPostT.err (e' := .rangeError 0 Fl4Spec.maxPCIBus) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 Fl4Spec.maxPCIBus) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg e2, whenE_neg (by rw [mB]; exact fun h => by have := (bv_gt_iff y2 255 (by norm_num)).1 h; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + by_cases e3 : y3 > 31#u64 + · rw [if_pos e3] + simp only [error.range, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok, uncurry_apply_pair] + rw [whenE_pos (by rw [mD]; exact (bv_gt_iff y3 31 (by norm_num)).2 (by scalar_tac))] + exact CPostT.err (e' := .rangeError 0 Fl4Spec.maxPCIDev) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 Fl4Spec.maxPCIDev) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg e3, whenE_neg (by rw [mD]; exact fun h => by have := (bv_gt_iff y3 31 (by norm_num)).1 h; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + by_cases e4 : y4 > 7#u64 + · rw [if_pos e4] + simp only [error.range, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok, uncurry_apply_pair] + rw [whenE_pos (by rw [mF]; exact (bv_gt_iff y4 7 (by norm_num)).2 (by scalar_tac))] + exact CPostT.err (e' := .rangeError 0 Fl4Spec.maxPCIFunc) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 Fl4Spec.maxPCIFunc) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg e4, whenE_neg (by rw [mF]; exact fun h => by have := (bv_gt_iff y4 7 (by norm_num)).1 h; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- `set_thread_state Restart`, then `invoke_irq_control` + simp only [Std.bind_assoc, bind_ok] + have hrs := restart_irq th hP s σ G ⟨hR, hV, hM, hC, hI⟩ hL hIR ct hcur htcb hact hact2 + refine WP.spec_bind (WP.spec_and (WP.spec_and hrs (sts_dk4c hP s htcb sched.TS_RESTART)) (spec_self hrs)) ?_ + rintro s1 ⟨⟨⟨hkb, hS, σ1, hst, hCR1, hIR1, -, -, hw⟩, hdk1⟩, hs1⟩ + obtain ⟨xct, hkct, -⟩ := hR.tcb ct htcb + have hstp1 : CurRun σ → Steps (L := L) arI th s ar σ s1 ar σ1 := Steps.restart arI hcur hkct htcb hs1 hst + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, uncurry_apply_pair] + let irqSt : Fl4Spec.X64IRQState := .irqMSI (y2.bv &&& Fl4Spec.mask 8) (y3.bv &&& Fl4Spec.mask 5) + (y4.bv &&& Fl4Spec.mask 3) (y5.bv &&& Fl4Spec.mask 32) + let σb : Fl4Spec.State Unit := { σ1 with archState := { σ1.archState with + x64IrqState := Function.update σ1.archState.x64IrqState (BitVec.ofNat 8 z.val) irqSt } } + refine WP.spec_bind (WP.spec_and (ctl_finishT arI th hP s σ G ⟨hR, hV, hM, hC, hI⟩ hL hHA hFI hFR hmask z hz128 hact0 hps hls hsrcC + dsl pd hpd1 hpd hdst (DestLive.of_cnRef hpdr ⟨x0, hx0s, capV_of_capAt hc0⟩) s1 σ1 hkb hS hCR1 hIR1 hw (ksub_of_eq (setThreadState_nks _ _) hst) ar σb rfl rfl rfl rfl rfl) + (ctl_dk4_at arI th hP s s1 hkb hS hL hCR1.2.2.1 hmask z hz128 hps hls hsrcC dsl pd hpd1 hpd ar)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨σ', G', hmem, hCR', hIR', hFI', hHA', hFR', hstp2, htr⟩, hdk2⟩ + apply (WP.spec_ok (core.result.Result.Ok (), s2, a2)).mpr + refine ⟨.issueIRQHandlerMSI (BitVec.ofNat 8 z.val) dsl (ps.bv, idxs) y2.bv y3.bv y4.bv y5.bv, rfl, σ1, hst, + σ', G', ?_, hCR', hIR', hFI', hHA', hFR', + fun hD => (hdk1 hD).trans (hdk2 (hD.toK.same (hdk1 hD).1)), + fun hC => ((hstp1 hC).trans arI (Steps.ksSpec arI (s := s1) (a := ar) (σ' := σb) (by constructor <;> rfl) rfl rfl (fun h => ⟨h.st, h.node⟩) id)).trans arI hstp2, + ⟨ct, s1, σ1, ar, σb, hcur, hs1, hst, Or.inl rfl, by constructor <;> rfl, rfl, rfl, fun _ _ h => ⟨h.st, h.node⟩, fun _ _ h => h, htr⟩⟩ + refine mem_liftE (x := ((), σ')) ?_ + refine mem_bind_nd (x := (irqSt, σ1)) rfl ?_ + refine mem_bind_nd (x := ((), σb)) (updateIrqState_mem _ _ _) ?_ + exact hmem + +/-- **`X86IRQIssueIRQHandlerIOAPIC`** (label 70). -/ +theorem irq_ioapic_refinesT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.invocationType slbl = .archInvocationLabel .x64IRQIssueIRQHandlerIOAPIC) + (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) + (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel th (L.mem s) x0 excaps) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (hAR : ArchIrqRel arI ar σ) : + interrupt.decode_irq_control st arI s ar fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC length + (cslotAddr (ps.bv, idxs)) buf extra ⦃ res => + CPostT (L := L) arI Fl4Spec.invokeFl4IrqControl th G (L.ksb s) σ + (Fl4Spec.decodeFl4IrqControl false slbl (args.map UScalar.bv) (ps.bv, idxs) excaps) s ar res ⦄ := by + obtain ⟨hR, hV, hM, hC, hI⟩ := hCR + unfold interrupt.decode_irq_control + have n1 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC = fl4_abi.label.IRQIssueIRQHandler := by + rw [u64_eq_iff]; simp + have n2 : ¬ ((fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC != fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC) = true) := by + simp + have n4 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC = fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped := by + rw [u64_eq_iff]; simp + have n5 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC = fl4_abi.label.X86IRQIssueIRQHandlerMSI := by + rw [u64_eq_iff]; simp + rw [if_neg n1, if_neg n2] + simp only [n4, n5, ↓reduceIte, bind_ok] + obtain ⟨nA, hnum, hnA⟩ := hAR.num + unfold interrupt.extra0; rw [hx0]; simp only [bind_ok, is_null_eq] + -- the spec's decode + unfold Fl4Spec.decodeFl4IrqControl + simp only [Bool.false_eq_true, ↓reduceIte] + refine CPostT.liftME' ?_ + unfold Fl4Spec.decodeIrqControlInvocationWith + rw [if_neg (show ¬ Fl4Spec.genInvocationType slbl = .irqIssueIRQHandler by rw [invocationType_gen hlbl]; decide)] + refine CPostT.liftME' ?_ + unfold Fl4Spec.archDecodeIrqControlInvocation + rw [if_pos hlbl] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, c0, ac0, erest, hc0, hca0, hcv0, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 7 ∧ ([] : List Fl4Spec.Cap).length ≥ 1) by simp)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, slot_cap_eq, hc0, bind_ok] + by_cases h7 : length >= 7#u64 + swap + · rw [if_neg h7]; simp only [error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 7 ∧ (ac0 :: erest).length ≥ 1) by simp; scalar_tac)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + rw [if_pos h7] + obtain ⟨y0, y1, y2, y3, y4, y5, y6, rest, rfl⟩ : + ∃ y0 y1 y2 y3 y4 y5 y6 rest, args = y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest := by + match args with + | [] | [_] | [_, _] | [_, _, _] | [_, _, _, _] | [_, _, _, _, _] | [_, _, _, _, _, _] => + exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest => exact ⟨y0, y1, y2, y3, y4, y5, y6, rest, rfl⟩ + rw [if_pos (show ((y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest).map UScalar.bv).length ≥ 7 ∧ + (ac0 :: erest).length ≥ 1 by simp)] + simp only [hct, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), hargs 6#u64 y6 (by simp), bind_ok, + user_max_eq, user_min_eq, sub_107] + have hmap : List.map UScalar.bv (y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: rest) = + y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv := rfl + rw [hmap] + have g2 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[2]? = some y2.bv := rfl + have g3 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[3]? = some y3.bv := rfl + have g4 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[4]? = some y4.bv := rfl + have g6 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[6]? = some y6.bv := rfl + have g0 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[0]? = some y0.bv := rfl + have g1 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[1]? = some y1.bv := rfl + have g5 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: rest.map UScalar.bv)[5]? = some y5.bv := rfl + have ge : (ac0 :: erest)[0]? = some ac0 := rfl + rw [g0, g1, g2, g3, g4, g5, g6, ge] + repeat rw [the_some] + rw [maxmin_eq] + by_cases hr : y6 > 107#u64 + · rw [if_pos hr]; simp only [error.range, bind_ok] + rw [whenE_pos ((bv_gt_iff y6 107 (by norm_num)).2 (by scalar_tac))] + exact CPostT.err (e' := .rangeError 0 (BitVec.ofNat 64 107)) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 (BitVec.ofNat 64 107)) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hr] + have hy6 : y6.val ≤ 107 := by scalar_tac + rw [whenE_neg (fun h => by have := (bv_gt_iff y6 107 (by norm_num)).1 h; omega)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IRQ + obtain ⟨z, hz, hzv⟩ := spec_exists (U64.add_spec (x := y6) (y := 16#u64) (by scalar_tac)) + rw [hz]; simp only [bind_ok] + rw [irqv_eq y6 hy6, show y6.val + 16 = z.val by rw [hzv]; rfl] + have hz128 : z.val < 128 := by rw [hzv]; simp; omega + rw [is_irq_active_eq]; simp only [bind_ok] + refine CPostT.congr (run_bind_ok (run_liftE (isIrqActive_run hIR hz128)) _) ?_ + by_cases hact0 : L.mem s (irqStA (L.ksb s) z) = 0#u64 + swap + · simp only [hact0, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + rw [whenE_pos (by simpa using hact0)] + exact CPostT.err (e' := .revokeFirst) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.revokeFirst : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl trivial) + simp only [hact0, ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [whenE_neg (by simp [hact0])] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + refine target_tailT arI _ th (L.ksb s) s ar σ hR hV c0 ac0 hca0 hcv0 y0 y1 _ _ ?_ + intro dsl pd hpd1 hpd hpdr hdst + -- the vector + have hio : layout.irq.INT_OFFSET = 32#u64 := by unfold layout.irq.INT_OFFSET; rfl + rw [hio] + obtain ⟨z2, hz2, hz2v⟩ := spec_exists (U64.add_spec (x := z) (y := 32#u64) (by scalar_tac)) + rw [hz2]; simp only [bind_ok] + unfold interrupt.map_ioapic + simp only [hct, hargs 2#u64 y2 (by simp), hargs 3#u64 y3 (by simp), hargs 4#u64 y4 (by simp), + hargs 5#u64 y5 (by simp), hnum, bind_ok, decide_false, Bool.false_eq_true, ↓reduceIte, shl16] + -- the spec reads the IOAPIC count and pins + refine CPostT.congr (run_bind_ok (run_liftE + (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.archState.x64NumIoapics) rfl) _) ?_ + refine CPostT.congr (run_bind_ok (run_liftE + (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.archState.x64IoapicNirqs) rfl) _) ?_ + beta_reduce + rw [← hnA] + -- no IOAPIC + by_cases hn0 : nA = 0#u64 + · rw [if_pos hn0] + simp only [error.illegal, bind_ok, uncurry_apply_pair, 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 [whenE_pos (show nA.bv = 0 by rw [hn0]; rfl)] + exact CPostT.err (e := .Sys .IllegalOperation) (e' := .illegalOperation) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl trivial) + have hn0' : nA.val ≠ 0 := fun h => hn0 (UScalar.eq_of_val_eq h) + rw [if_neg hn0, whenE_neg (fun h => hn0 (by rw [UScalar.eq_equiv_bv_eq]; exact h))] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IOAPIC + by_cases hia : y2 >= nA + · rw [if_pos hia] + obtain ⟨m1, hm1, hm1v⟩ := spec_exists (U64.sub_spec (x := nA) (y := 1#u64) (by scalar_tac)) + rw [hm1]; simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos (by rw [bv_gt_iff', bv_sub1_toNat nA hn0']; scalar_tac)] + refine CPostT.err (e := .Sys (.RangeError 0#u64 m1)) (e' := .rangeError 0 (nA.bv - 1)) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 (nA.bv - 1)) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, ?_⟩) + apply BitVec.eq_of_toNat_eq + have : m1.val = nA.val - 1 := by rw [hm1v.1]; simp + rw [bv_sub1_toNat nA hn0']; exact this.symm + rw [if_neg hia, whenE_neg (by rw [bv_gt_iff', bv_sub1_toNat nA hn0']; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the pin + obtain ⟨p, hp, hpv, hp0⟩ := hAR.pins nA hnum y2 (by scalar_tac) + rw [hp]; simp only [bind_ok] + by_cases hpin : y3 >= p + · rw [if_pos hpin] + simp only [lift, bind_ok, error.range, uncurry_apply_pair, 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 [whenE_pos (by rw [bv_gt_iff', nirq_toNat _ (by rw [← hpv]; exact hp0), ← hpv]; scalar_tac)] + refine CPostT.err (e := .Sys (.RangeError 0#u64 (core.num.U64.wrapping_sub p 1#u64))) + (e' := .rangeError 0 (Fl4Spec.ucast 64 (σ.archState.x64IoapicNirqs y2.bv - 1))) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 + (Fl4Spec.ucast 64 (σ.archState.x64IoapicNirqs y2.bv - 1))) : Fl4Spec.SEMonad Unit Unit)) rfl _) + (Or.inl ⟨rfl, ?_⟩) + apply BitVec.eq_of_toNat_eq + rw [nirq_toNat _ (by rw [← hpv]; exact hp0), ← hpv] + rw [show (core.num.U64.wrapping_sub p 1#u64).bv.toNat = (core.num.U64.wrapping_sub p 1#u64).val from rfl, + core.num.U64.wrapping_sub_val_eq] + simp [U64.size, U64.numBits]; omega + rw [if_neg hpin, whenE_neg (by rw [bv_gt_iff', nirq_toNat _ (by rw [← hpv]; exact hp0), ← hpv]; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- level and polarity + by_cases hlv : y4 > 1#u64 + · rw [if_pos hlv] + simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos ((bv_gt_iff' y4 1).2 (by simp; scalar_tac))] + exact CPostT.err (e := .Sys (.RangeError 0#u64 1#u64)) (e' := .rangeError 0 1) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 1) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hlv, whenE_neg (fun h => by have := (bv_gt_iff' y4 1).1 h; simp at this; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + by_cases hpo : y5 > 1#u64 + · rw [if_pos hpo] + simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos ((bv_gt_iff' y5 1).2 (by simp; scalar_tac))] + exact CPostT.err (e := .Sys (.RangeError 0#u64 1#u64)) (e' := .rangeError 0 1) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 1) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hpo, whenE_neg (fun h => by have := (bv_gt_iff' y5 1).1 h; simp at this; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the handle is 0 + rw [if_neg (by scalar_tac)] + -- the IOAPIC (#42: routed, `ArchIrqRel.map`), `set_thread_state Restart`, then `invoke_irq_control` + obtain ⟨a1, hmap⟩ := hAR.map ar nA y2 y3 y4 y5 z2 p hnum hia hp hpin hlv hpo + simp only [Std.bind_assoc, bind_ok, hmap, uncurry_apply_pair, Bool.false_eq_true, ↓reduceIte] + have hrs := restart_irq th hP s σ G ⟨hR, hV, hM, hC, hI⟩ hL hIR ct hcur htcb hact hact2 + refine WP.spec_bind (WP.spec_and (WP.spec_and hrs (sts_dk4c hP s htcb sched.TS_RESTART)) (spec_self hrs)) ?_ + rintro s1 ⟨⟨⟨hkb, hS, σ1, hst, hCR1, hIR1, ha1, -, hw⟩, hdk1⟩, hs1⟩ + obtain ⟨xct, hkct, -⟩ := hR.tcb ct htcb + have hstp1 : CurRun σ → Steps (L := L) arI th s a1 σ s1 a1 σ1 := Steps.restart arI hcur hkct htcb hs1 hst + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, uncurry_apply_pair] + obtain ⟨ms', hums, hms'⟩ := doMachineOp_lift_ex + (Fl4Spec.ioapicMapPinToVectorImpl y2.bv y3.bv y4.bv y5.bv (Fl4Spec.ucast 64 (BitVec.ofNat 8 z.val) + Fl4Spec.irqIntOffset)) σ1 + let σa : Fl4Spec.State Unit := { σ1 with machineState := ms' } + let irqSt : Fl4Spec.X64IRQState := .irqIOAPIC (y2.bv &&& Fl4Spec.mask 5) (y3.bv &&& Fl4Spec.mask 5) + (y4.bv &&& 1) (y5.bv &&& 1) true + let σb : Fl4Spec.State Unit := { σa with archState := { σa.archState with + x64IrqState := Function.update σa.archState.x64IrqState (BitVec.ofNat 8 z.val) irqSt } } + refine WP.spec_bind (WP.spec_and (ctl_finishT arI th hP s σ G ⟨hR, hV, hM, hC, hI⟩ hL hHA hFI hFR hAR.mask z hz128 hact0 hps hls hsrcC + dsl pd hpd1 hpd hdst (DestLive.of_cnRef hpdr ⟨x0, hx0s, capV_of_capAt hc0⟩) s1 σ1 hkb hS hCR1 hIR1 hw (ksub_of_eq (setThreadState_nks _ _) hst) a1 σb rfl rfl rfl rfl rfl) + (ctl_dk4_at arI th hP s s1 hkb hS hL hCR1.2.2.1 hAR.mask z hz128 hps hls hsrcC dsl pd hpd1 hpd a1)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨σ', G', hmem, hCR', hIR', hFI', hHA', hFR', hstp2, htr⟩, hdk2⟩ + apply (WP.spec_ok (core.result.Result.Ok (), s2, a2)).mpr + refine ⟨.issueIRQHandlerIOAPIC (BitVec.ofNat 8 z.val) dsl (ps.bv, idxs) y2.bv y3.bv y4.bv y5.bv + (Fl4Spec.ucast 64 (BitVec.ofNat 8 z.val) + Fl4Spec.irqIntOffset), rfl, σ1, hst, σ', G', ?_, hCR', hIR', hFI', hHA', hFR', + fun hD => (hdk1 hD).trans (hdk2 (hD.toK.same (hdk1 hD).1)), + fun hC => ((((Steps.arch arI (s := s) (σ := σ) (Or.inr (Or.inr (Or.inl ⟨_, _, _, _, _, _, hmap⟩)))).trans arI (hstp1 hC)).trans arI + (Steps.ksSpec arI (s := s1) (a := a1) (⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, hums, rfl, rfl, rfl, rfl⟩ : KsStep σ1 σa) rfl rfl (fun h => ⟨h.st, h.node⟩) id)).trans arI + (Steps.ksSpec arI (s := s1) (a := a1) (σ := σa) (σ' := σb) (by constructor <;> rfl) rfl rfl (fun h => ⟨h.st, h.node⟩) id)).trans arI hstp2, + ⟨ct, s1, σ1, a1, σb, hcur, hs1, hst, Or.inr (Or.inr (Or.inl ⟨_, _, _, _, _, _, hmap⟩)), ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, hums, rfl, rfl, rfl, rfl⟩, rfl, rfl, fun _ _ h => ⟨h.st, h.node⟩, fun _ _ h => h, htr⟩⟩ + refine mem_liftE (x := ((), σ')) ?_ + refine mem_bind_nd (x := ((), σa)) hms' ?_ + refine mem_bind_nd (x := (irqSt, σa)) rfl ?_ + refine mem_bind_nd (x := ((), σb)) (updateIrqState_mem _ _ _) ?_ + exact hmem + +/-- **l4h `X86IRQIssueIRQHandlerIOAPICRemapped`** (label 72), against FL4's copy of the IOAPIC decode with +the remapping-table handle. -/ +theorem irq_remap_refinesT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (slbl : Fl4Spec.Data) + (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) + (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel th (L.mem s) x0 excaps) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (hAR : ArchIrqRel arI ar σ) : + interrupt.decode_irq_control st arI s ar fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped length + (cslotAddr (ps.bv, idxs)) buf extra ⦃ res => + CPostT (L := L) arI Fl4Spec.invokeFl4IrqControl th G (L.ksb s) σ + (Fl4Spec.decodeFl4IrqControl true slbl (args.map UScalar.bv) (ps.bv, idxs) excaps) s ar res ⦄ := by + obtain ⟨hR, hV, hM, hC, hI⟩ := hCR + unfold interrupt.decode_irq_control + have n1 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped = fl4_abi.label.IRQIssueIRQHandler := by + rw [u64_eq_iff]; simp + have n2 : (fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped != fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC) = true := by + rw [bne_iff_ne, ne_eq, u64_eq_iff]; simp + have n3 : (fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped != fl4_abi.label.X86IRQIssueIRQHandlerMSI) = true := by + rw [bne_iff_ne, ne_eq, u64_eq_iff]; simp + have n5 : ¬ fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped = fl4_abi.label.X86IRQIssueIRQHandlerMSI := by + rw [u64_eq_iff]; simp + rw [if_neg n1, if_pos n2, if_pos n3, if_pos rfl] + simp only [n5, eq_self_iff_true, ↓reduceIte, bind_ok] + obtain ⟨nA, hnum, hnA⟩ := hAR.num + unfold interrupt.extra0; rw [hx0]; simp only [bind_ok, is_null_eq] + -- the spec's decode + unfold Fl4Spec.decodeFl4IrqControl + simp only [↓reduceIte] + unfold Fl4Spec.decodeIrqControlRemapped + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, hx0s, c0, ac0, erest, hc0, hca0, hcv0, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 8 ∧ ([] : List Fl4Spec.Cap).length ≥ 1) by simp)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, slot_cap_eq, hc0, bind_ok] + by_cases h7 : length >= 8#u64 + swap + · rw [if_neg h7]; simp only [error.truncated, bind_ok] + rw [if_neg (show ¬ ((args.map UScalar.bv).length ≥ 8 ∧ (ac0 :: erest).length ≥ 1) by simp; scalar_tac)] + exact CPostT.err (e' := .truncatedMessage) rfl (Or.inl trivial) + rw [if_pos h7] + obtain ⟨y0, y1, y2, y3, y4, y5, y6, y7, rest, rfl⟩ : + ∃ y0 y1 y2 y3 y4 y5 y6 y7 rest, args = y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: y7 :: rest := by + match args with + | [] | [_] | [_, _] | [_, _, _] | [_, _, _, _] | [_, _, _, _, _] | [_, _, _, _, _, _] + | [_, _, _, _, _, _, _] => + exfalso; simp at hlen; scalar_tac + | y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: y7 :: rest => exact ⟨y0, y1, y2, y3, y4, y5, y6, y7, rest, rfl⟩ + rw [if_pos (show ((y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: y7 :: rest).map UScalar.bv).length ≥ 8 ∧ + (ac0 :: erest).length ≥ 1 by simp)] + simp only [hct, hargs 0#u64 y0 (by simp), hargs 1#u64 y1 (by simp), hargs 6#u64 y6 (by simp), bind_ok, + user_max_eq, user_min_eq, sub_107] + have hmap : List.map UScalar.bv (y0 :: y1 :: y2 :: y3 :: y4 :: y5 :: y6 :: y7 :: rest) = + y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv := rfl + rw [hmap] + have g2 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[2]? = some y2.bv := rfl + have g3 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[3]? = some y3.bv := rfl + have g4 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[4]? = some y4.bv := rfl + have g6 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[6]? = some y6.bv := rfl + have g0 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[0]? = some y0.bv := rfl + have g1 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[1]? = some y1.bv := rfl + have g5 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[5]? = some y5.bv := rfl + have ge : (ac0 :: erest)[0]? = some ac0 := rfl + have g7 : (y0.bv :: y1.bv :: y2.bv :: y3.bv :: y4.bv :: y5.bv :: y6.bv :: y7.bv :: rest.map UScalar.bv)[7]? = + some y7.bv := rfl + rw [g0, g1, g2, g3, g4, g5, g6, g7, ge] + repeat rw [the_some] + rw [maxmin_eq] + by_cases hr : y6 > 107#u64 + · rw [if_pos hr]; simp only [error.range, bind_ok] + rw [whenE_pos ((bv_gt_iff y6 107 (by norm_num)).2 (by scalar_tac))] + exact CPostT.err (e' := .rangeError 0 (BitVec.ofNat 64 107)) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 (BitVec.ofNat 64 107)) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hr] + have hy6 : y6.val ≤ 107 := by scalar_tac + rw [whenE_neg (fun h => by have := (bv_gt_iff y6 107 (by norm_num)).1 h; omega)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IRQ + obtain ⟨z, hz, hzv⟩ := spec_exists (U64.add_spec (x := y6) (y := 16#u64) (by scalar_tac)) + rw [hz]; simp only [bind_ok] + rw [irqv_eq y6 hy6, show y6.val + 16 = z.val by rw [hzv]; rfl] + have hz128 : z.val < 128 := by rw [hzv]; simp; omega + rw [is_irq_active_eq]; simp only [bind_ok] + refine CPostT.congr (run_bind_ok (run_liftE (isIrqActive_run hIR hz128)) _) ?_ + by_cases hact0 : L.mem s (irqStA (L.ksb s) z) = 0#u64 + swap + · simp only [hact0, ne_eq, not_false_eq_true, decide_true, ↓reduceIte] + rw [whenE_pos (by simpa using hact0)] + exact CPostT.err (e' := .revokeFirst) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.revokeFirst : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl trivial) + simp only [hact0, ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte] + rw [whenE_neg (by simp [hact0])] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + refine target_tailT arI _ th (L.ksb s) s ar σ hR hV c0 ac0 hca0 hcv0 y0 y1 _ _ ?_ + intro dsl pd hpd1 hpd hpdr hdst + -- the vector + have hio : layout.irq.INT_OFFSET = 32#u64 := by unfold layout.irq.INT_OFFSET; rfl + rw [hio] + obtain ⟨z2, hz2, hz2v⟩ := spec_exists (U64.add_spec (x := z) (y := 32#u64) (by scalar_tac)) + rw [hz2]; simp only [bind_ok] + unfold interrupt.map_ioapic + simp only [hct, hargs 2#u64 y2 (by simp), hargs 3#u64 y3 (by simp), hargs 4#u64 y4 (by simp), + hargs 5#u64 y5 (by simp), hargs 7#u64 y7 (by simp), hnum, bind_ok, decide_true, ↓reduceIte, shl16] + -- the spec reads the IOAPIC count and pins + refine CPostT.congr (run_bind_ok (run_liftE + (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.archState.x64NumIoapics) rfl) _) ?_ + refine CPostT.congr (run_bind_ok (run_liftE + (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.archState.x64IoapicNirqs) rfl) _) ?_ + beta_reduce + rw [← hnA] + -- no IOAPIC + by_cases hn0 : nA = 0#u64 + · rw [if_pos hn0] + simp only [error.illegal, bind_ok, uncurry_apply_pair, 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 [whenE_pos (show nA.bv = 0 by rw [hn0]; rfl)] + exact CPostT.err (e := .Sys .IllegalOperation) (e' := .illegalOperation) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl trivial) + have hn0' : nA.val ≠ 0 := fun h => hn0 (UScalar.eq_of_val_eq h) + rw [if_neg hn0, whenE_neg (fun h => hn0 (by rw [UScalar.eq_equiv_bv_eq]; exact h))] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IOAPIC + by_cases hia : y2 >= nA + · rw [if_pos hia] + obtain ⟨m1, hm1, hm1v⟩ := spec_exists (U64.sub_spec (x := nA) (y := 1#u64) (by scalar_tac)) + rw [hm1]; simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos (by rw [bv_gt_iff', bv_sub1_toNat nA hn0']; scalar_tac)] + refine CPostT.err (e := .Sys (.RangeError 0#u64 m1)) (e' := .rangeError 0 (nA.bv - 1)) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 (nA.bv - 1)) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, ?_⟩) + apply BitVec.eq_of_toNat_eq + have : m1.val = nA.val - 1 := by rw [hm1v.1]; simp + rw [bv_sub1_toNat nA hn0']; exact this.symm + rw [if_neg hia, whenE_neg (by rw [bv_gt_iff', bv_sub1_toNat nA hn0']; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the pin + obtain ⟨p, hp, hpv, hp0⟩ := hAR.pins nA hnum y2 (by scalar_tac) + rw [hp]; simp only [bind_ok] + by_cases hpin : y3 >= p + · rw [if_pos hpin] + simp only [lift, bind_ok, error.range, uncurry_apply_pair, 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 [whenE_pos (by rw [bv_gt_iff', nirq_toNat _ (by rw [← hpv]; exact hp0), ← hpv]; scalar_tac)] + refine CPostT.err (e := .Sys (.RangeError 0#u64 (core.num.U64.wrapping_sub p 1#u64))) + (e' := .rangeError 0 (Fl4Spec.ucast 64 (σ.archState.x64IoapicNirqs y2.bv - 1))) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 + (Fl4Spec.ucast 64 (σ.archState.x64IoapicNirqs y2.bv - 1))) : Fl4Spec.SEMonad Unit Unit)) rfl _) + (Or.inl ⟨rfl, ?_⟩) + apply BitVec.eq_of_toNat_eq + rw [nirq_toNat _ (by rw [← hpv]; exact hp0), ← hpv] + rw [show (core.num.U64.wrapping_sub p 1#u64).bv.toNat = (core.num.U64.wrapping_sub p 1#u64).val from rfl, + core.num.U64.wrapping_sub_val_eq] + simp [U64.size, U64.numBits]; omega + rw [if_neg hpin, whenE_neg (by rw [bv_gt_iff', nirq_toNat _ (by rw [← hpv]; exact hp0), ← hpv]; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- level and polarity + by_cases hlv : y4 > 1#u64 + · rw [if_pos hlv] + simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos ((bv_gt_iff' y4 1).2 (by simp; scalar_tac))] + exact CPostT.err (e := .Sys (.RangeError 0#u64 1#u64)) (e' := .rangeError 0 1) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 1) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hlv, whenE_neg (fun h => by have := (bv_gt_iff' y4 1).1 h; simp at this; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + by_cases hpo : y5 > 1#u64 + · rw [if_pos hpo] + simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos ((bv_gt_iff' y5 1).2 (by simp; scalar_tac))] + exact CPostT.err (e := .Sys (.RangeError 0#u64 1#u64)) (e' := .rangeError 0 1) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 1) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hpo, whenE_neg (fun h => by have := (bv_gt_iff' y5 1).1 h; simp at this; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the handle + by_cases hh : y7 >= 65536#u64 + · rw [if_pos hh] + simp only [error.range, bind_ok, uncurry_apply_pair, 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 [whenE_pos (by rw [GE.ge, BitVec.le_def]; show (65536 : BitVec 64).toNat ≤ y7.val; simp; scalar_tac)] + exact CPostT.err (e := .Sys (.RangeError 0#u64 65535#u64)) (e' := .rangeError 0 0xffff) + (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 0xffff) : + Fl4Spec.SEMonad Unit Unit)) rfl _) (Or.inl ⟨rfl, rfl⟩) + rw [if_neg hh, whenE_neg (by rw [GE.ge, BitVec.le_def]; show ¬ (65536 : BitVec 64).toNat ≤ y7.val; simp; scalar_tac)] + refine CPostT.congr (run_bind_ok (m := (Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit)) rfl _) ?_ + -- the IOAPIC (#42: routed, `ArchIrqRel.mapr`), `set_thread_state Restart`, then `invoke_irq_control` + obtain ⟨a1, hmap⟩ := hAR.mapr ar nA y2 y3 y4 y5 z2 y7 p hnum hia hp hpin hlv hpo hh + simp only [Std.bind_assoc, bind_ok, hmap, uncurry_apply_pair, Bool.false_eq_true, ↓reduceIte] + have hrs := restart_irq th hP s σ G ⟨hR, hV, hM, hC, hI⟩ hL hIR ct hcur htcb hact hact2 + refine WP.spec_bind (WP.spec_and (WP.spec_and hrs (sts_dk4c hP s htcb sched.TS_RESTART)) (spec_self hrs)) ?_ + rintro s1 ⟨⟨⟨hkb, hS, σ1, hst, hCR1, hIR1, ha1, -, hw⟩, hdk1⟩, hs1⟩ + obtain ⟨xct, hkct, -⟩ := hR.tcb ct htcb + have hstp1 : CurRun σ → Steps (L := L) arI th s a1 σ s1 a1 σ1 := Steps.restart arI hcur hkct htcb hs1 hst + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, uncurry_apply_pair] + obtain ⟨ms', hums, hms'⟩ := doMachineOp_lift_ex + (Fl4Spec.ioapicMapPinToVectorRemappedImpl y2.bv y3.bv y4.bv y5.bv + (Fl4Spec.ucast 64 (BitVec.ofNat 8 z.val) + Fl4Spec.irqIntOffset) y7.bv) σ1 + let σa : Fl4Spec.State Unit := { σ1 with machineState := ms' } + let irqSt : Fl4Spec.X64IRQState := .irqIOAPIC (y2.bv &&& Fl4Spec.mask 5) (y3.bv &&& Fl4Spec.mask 5) + (y4.bv &&& 1) (y5.bv &&& 1) true + let σb : Fl4Spec.State Unit := { σa with archState := { σa.archState with + x64IrqState := Function.update σa.archState.x64IrqState (BitVec.ofNat 8 z.val) irqSt } } + refine WP.spec_bind (WP.spec_and (ctl_finishT arI th hP s σ G ⟨hR, hV, hM, hC, hI⟩ hL hHA hFI hFR hAR.mask z hz128 hact0 hps hls hsrcC + dsl pd hpd1 hpd hdst (DestLive.of_cnRef hpdr ⟨x0, hx0s, capV_of_capAt hc0⟩) s1 σ1 hkb hS hCR1 hIR1 hw (ksub_of_eq (setThreadState_nks _ _) hst) a1 σb rfl rfl rfl rfl rfl) + (ctl_dk4_at arI th hP s s1 hkb hS hL hCR1.2.2.1 hAR.mask z hz128 hps hls hsrcC dsl pd hpd1 hpd a1)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨σ', G', hmem, hCR', hIR', hFI', hHA', hFR', hstp2, htr⟩, hdk2⟩ + apply (WP.spec_ok (core.result.Result.Ok (), s2, a2)).mpr + refine ⟨.ioapicRemapped (BitVec.ofNat 8 z.val) dsl (ps.bv, idxs) y2.bv y3.bv y4.bv y5.bv + (Fl4Spec.ucast 64 (BitVec.ofNat 8 z.val) + Fl4Spec.irqIntOffset) y7.bv, rfl, σ1, hst, σ', G', ?_, hCR', hIR', hFI', hHA', hFR', + fun hD => (hdk1 hD).trans (hdk2 (hD.toK.same (hdk1 hD).1)), + fun hC => ((((Steps.arch arI (s := s) (σ := σ) (Or.inr (Or.inr (Or.inr (Or.inl ⟨_, _, _, _, _, _, _, hmap⟩))))).trans arI (hstp1 hC)).trans arI + (Steps.ksSpec arI (s := s1) (a := a1) (⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, hums, rfl, rfl, rfl, rfl⟩ : KsStep σ1 σa) rfl rfl (fun h => ⟨h.st, h.node⟩) id)).trans arI + (Steps.ksSpec arI (s := s1) (a := a1) (σ := σa) (σ' := σb) (by constructor <;> rfl) rfl rfl (fun h => ⟨h.st, h.node⟩) id)).trans arI hstp2, + ⟨ct, s1, σ1, a1, σb, hcur, hs1, hst, Or.inr (Or.inr (Or.inr (Or.inl ⟨_, _, _, _, _, _, _, hmap⟩))), ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, hums, rfl, rfl, rfl, rfl⟩, rfl, rfl, fun _ _ h => ⟨h.st, h.node⟩, fun _ _ h => h, htr⟩⟩ + refine mem_liftE (x := ((), σ')) ?_ + refine mem_bind_nd (x := ((), σa)) hms' ?_ + refine mem_bind_nd (x := (irqSt, σa)) rfl ?_ + refine mem_bind_nd (x := ((), σb)) (updateIrqState_mem _ _ _) ?_ + exact hmem + +/-- **Any other IRQControl label**: `IllegalOperation`. -/ +theorem irq_ctl_other_refinesT (th : TypedHeap) (G : MdbGhost) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (lbl : U64) (h1 : lbl.val ≠ 33) (h2 : lbl.val ≠ 70) (h3 : lbl.val ≠ 71) (h4 : lbl.val ≠ 72) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl ≠ .irqIssueIRQHandler ∧ + Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64IRQIssueIRQHandlerIOAPIC ∧ + Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64IRQIssueIRQHandlerMSI) + (length src buf : U64) (extra : Aeneas.Std.Array obj.Slot 3#usize) (args : List Fl4Spec.Data) + (ssl : Fl4Spec.CslotPtr) (excaps : List Fl4Spec.Cap) : + interrupt.decode_irq_control st arI s ar lbl length src buf extra ⦃ res => + CPostT (L := L) arI Fl4Spec.invokeFl4IrqControl th G (L.ksb s) σ + (Fl4Spec.decodeFl4IrqControl false slbl args ssl excaps) s ar res ⦄ := by + unfold interrupt.decode_irq_control + have n1 : ¬ lbl = fl4_abi.label.IRQIssueIRQHandler := by rw [u64_eq_iff]; simpa using h1 + have n2 : (lbl != fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC) = true := by + rw [bne_iff_ne, ne_eq, u64_eq_iff]; simpa using h2 + have n3 : (lbl != fl4_abi.label.X86IRQIssueIRQHandlerMSI) = true := by + rw [bne_iff_ne, ne_eq, u64_eq_iff]; simpa using h3 + have n4 : ¬ lbl = fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped := by rw [u64_eq_iff]; simpa using h4 + rw [if_neg n1, if_pos n2, if_pos n3, if_neg n4] + simp only [error.illegal, bind_ok] + unfold Fl4Spec.decodeFl4IrqControl + simp only [Bool.false_eq_true, ↓reduceIte] + refine CPostT.liftME' ?_ + unfold Fl4Spec.decodeIrqControlInvocationWith + rw [if_neg hlbl.1] + refine CPostT.liftME' ?_ + unfold Fl4Spec.archDecodeIrqControlInvocation + rw [if_neg hlbl.2.1, if_neg hlbl.2.2] + exact CPostT.err (e := .Sys .IllegalOperation) (e' := .illegalOperation) rfl (Or.inl trivial) + +/-- **`decode_irq_control`, every label.** -/ +theorem irq_control_decode_refinesT (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) + (hCR : CRel th (L.mem s) σ G) (hL : IntLayout th (L.ksb s)) (hIR : IrqRel (L.mem s) (L.ksb s) σ) + (hHA : HandlersActive (L.mem s) (L.ksb s) G) (hFI : FinInv th (L.mem s) σ G) (hFR : MdbOKV (L.ksb s) (L.mem s) G) + (lbl : U64) (slbl : Fl4Spec.Data) (hlbl : IrqCLabel lbl slbl) + (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) + (hcur : σ.curThread = ct.bv) (htcb : th ct = some .tcb) + (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel th (L.mem s) x0 excaps) + {ps : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok .IrqControl) + (nA : U64) (hnum : arI.num_ioapics ar = ok nA) (hnA : nA.bv = σ.archState.x64NumIoapics) + (hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a') + (hAR : ArchIrqRel arI ar σ) : + interrupt.decode_irq_control st arI s ar lbl length (cslotAddr (ps.bv, idxs)) buf extra ⦃ res => + CPostT (L := L) arI Fl4Spec.invokeFl4IrqControl th G (L.ksb s) σ + (Fl4Spec.decodeFl4IrqControl (decide (lbl.val = 72)) slbl (args.map UScalar.bv) (ps.bv, idxs) excaps) + s ar res ⦄ := by + obtain ⟨l33, l67, l68, lo⟩ := hlbl + by_cases h33 : lbl.val = 33 + · have e : lbl = fl4_abi.label.IRQIssueIRQHandler := + UScalar.eq_of_val_eq (by rw [h33]; simp [fl4_abi.label.IRQIssueIRQHandler]) + subst e + simp only [h33] + exact irq_issue_refinesT arI th hP s ar σ G hCR hL hIR hHA hFI hFR slbl (l33 h33) length buf ct hct args hlen hargs + hcur htcb hact hact2 extra x0 hx0 excaps hex hps hls hsrcC nA hnum hnA hmask + by_cases h70 : lbl.val = 70 + · have e : lbl = fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC := + UScalar.eq_of_val_eq (by rw [h70]; simp [fl4_abi.label.X86IRQIssueIRQHandlerIOAPIC]) + subst e + simp only [h70] + exact irq_ioapic_refinesT arI th hP s ar σ G hCR hL hIR hHA hFI hFR slbl (l67 h70) length buf ct hct args hlen hargs + hcur htcb hact hact2 extra x0 hx0 excaps hex hps hls hsrcC hAR + by_cases h71 : lbl.val = 71 + · have e : lbl = fl4_abi.label.X86IRQIssueIRQHandlerMSI := + UScalar.eq_of_val_eq (by rw [h71]; simp [fl4_abi.label.X86IRQIssueIRQHandlerMSI]) + subst e + simp only [h71] + exact irq_msi_refinesT arI th hP s ar σ G hCR hL hIR hHA hFI hFR slbl (l68 h71) length buf ct hct args hlen hargs + hcur htcb hact hact2 extra x0 hx0 excaps hex hps hls hsrcC hmask + by_cases h72 : lbl.val = 72 + · have e : lbl = fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped := + UScalar.eq_of_val_eq (by rw [h72]; simp [fl4_abi.label.X86IRQIssueIRQHandlerIOAPICRemapped]) + subst e + simp only [h72] + exact irq_remap_refinesT arI th hP s ar σ G hCR hL hIR hHA hFI hFR slbl length buf ct hct args hlen hargs + hcur htcb hact hact2 extra x0 hx0 excaps hex hps hls hsrcC hAR + simp only [h72] + exact irq_ctl_other_refinesT arI th G s ar σ lbl h33 h70 h71 h72 slbl (lo h33 h70 h71 h72) length _ buf extra _ _ _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBMakePool.lean b/proofs/Fl4Proofs/P3/JAtZBMakePool.lean new file mode 100644 index 000000000..40db6fcdd --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBMakePool.lean @@ -0,0 +1,1030 @@ +import Fl4Proofs.P3.MpDirtyJ + +/-! +P3 (line mmuAD, Stage 3, ASIDControl MakePool): P3/XTopJointMakePool's and P3/MpDirtyJ's MakePool step +lemmas (`mp_objK`, `mp_keeps`, `mpBlkAfter`, `mp_utc`, `mp_sfree`, `mpStepJ`, `mpOObjAt`, `mpPlacedCAt`, +`mpRegionCAt`, `mpAddOKAt`, `destOff`) over `JAtZB` (the base of `VmEptState`), proofs as the native ones. +New file; no existing statement changed; no new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} +variable {Lst : U64 → List U64} {D : Set U64} + +/-- **The ASID-table word lies in no object of `H` but the `other` ones** +(`JAtZ.vsObjW`). -/ +theorem JAtZB.atab_obj {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {hi : U64} (hh : hi.val < 2048) {q : U64} {k : P1.HKind} (hq : H q = some k) (hk : ∀ z, k ≠ .other z) : + (atA (L.ksb s) hi).val < q.val ∨ q.val + 2 ^ k.bits ≤ (atA (L.ksb s) hi).val := + J.vsObjW ⟨J.scOut⟩ (Or.inr (Or.inl ⟨hi, hh, rfl⟩)) hq hk + +theorem JAtZB.atab_al {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {hi : U64} (hh : hi.val < 2048) : P1.Aligned (atA (L.ksb s) hi) := + J.vsW_al (Or.inr (Or.inl ⟨hi, hh, rfl⟩)) + +/-- **A slot lies in a TCB or an `other` object of `H`** (`JAtZ.slot_in_H` +with the kinds). -/ +theorem JAtZB.slot_in_H' {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {x : U64} (hx : IsSlot Z.th x) : + ∃ p k, H p = some k ∧ (k = .tcb ∨ ∃ z, k = .other z) ∧ p.val ≤ x.val ∧ x.val + 32 ≤ p.val + 2 ^ k.bits := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + obtain ⟨p, i, hi, rfl⟩ := hx + have hv := hi.val hJ.pl + rcases hi with ⟨r, hp, hlt⟩ | ⟨hp, hlt⟩ + · refine ⟨p, .other (r + 5), hJ.cnode p r hp, Or.inr ⟨_, rfl⟩, by omega, ?_⟩ + simp only [P1.HKind.bits] + have : 32 * (i + 1) ≤ 32 * 2 ^ r := by + have : i + 1 ≤ 2 ^ r := hlt; omega + rw [pow_add]; omega + · refine ⟨p, .tcb, (hJ.tcb p).1 hp, Or.inl rfl, by omega, ?_⟩ + simp only [P1.HKind.bits] + have : i < tcbSlots := hlt + simp [tcbSlots] at this; omega + +/-- **A word of an object of `H` other than a TCB or an `other` object is +kept by `MakePool`** (slots lie in TCBs and CNodes, `JAtZ.slot_in_H`; the +region holds no object of `H`; the table word by `JAtZ.atab_obj`). -/ +theorem JAtZB.mp_objK {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {p hi : U64} (hh : hi.val < 2048) + (hw : ∀ b, P1.Aligned b → ¬ MpW Z.th p (L.ksb s) hi b → L.mem s' b = L.mem s b) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + {q : U64} {k : P1.HKind} (hq : H q = some k) (hko : ∀ z, k ≠ .other z) (hkt : k ≠ .tcb) + {w : U64} (hal : P1.Aligned w) (g1 : q.val ≤ w.val) (g2 : w.val < q.val + 2 ^ k.bits) : + L.mem s' w = L.mem s w := by + have hP := J.xtop.rel.1 + refine mp_keep hP hw hal (fun x hx => ?_) ?_ ?_ + · obtain ⟨p', k', hp', hk', h1, h2⟩ := J.slot_in_H' hx + have hne : q ≠ p' := by + intro e; subst e; rw [hq] at hp'; cases hp' + rcases hk' with e | ⟨z, e⟩ + · exact hkt e + · exact hko z e + have := J.kern.heap.disj q _ p' _ hq hp' hne + omega + · have := hHo q k hq; omega + · intro e + have := J.atab_obj hh hq hko + rw [← e] at this; omega + +/-- **The scheduler's words `MakePool` keeps**: the TCBs' `MCP`, `YIELD_TO` +and IPC-buffer words (off their slots, `SlotsApart`), the SCs' `REPLY` +words and every SC word below 128, the replies' links (`JAtZ.mp_objK`). -/ +theorem JAtZB.mp_keeps {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {p hi : U64} (hh : hi.val < 2048) + (hw : ∀ b, P1.Aligned b → ¬ MpW Z.th p (L.ksb s) hi b → L.mem s' b = L.mem s b) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) : + (∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t)) ∧ (∀ t ∈ Z.T, L.mem s' (P1.yieldA t) = L.mem s (P1.yieldA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (tIpcBufA t) = L.mem s (tIpcBufA t)) ∧ + (∀ q ∈ Z.C, L.mem s' (P1.scReplyA q) = L.mem s (P1.scReplyA q)) ∧ + (∀ r ∈ Z.R, L.mem s' (P1.rPrevA r) = L.mem s (P1.rPrevA r) ∧ L.mem s' (P1.rNextA r) = L.mem s (P1.rNextA r)) ∧ + (∀ q z, H q = some (.sc z) → ∀ K : U64, K.val % 8 = 0 → K.val < 128 → + L.mem s' (scW q K) = L.mem s (scW q K)) := by + have hP := J.xtop.rel.1 + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hT := hc.tcbs + have hA := J.apart + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have tw : ∀ t ∈ Z.T, ∀ b : U64, P1.Aligned b → t.val + 256 ≤ b.val → b.val < t.val + 4096 → + L.mem s' b = L.mem s b := by + intro t ht b hb h1 h2 + have hHt : H t = some .tcb := J.zH.1 t ht + refine mp_keep hP hw hb (fun x hx => by rcases (hA x hx).1 t ht with h | h <;> omega) ?_ ?_ + · have := hHo t _ hHt; simp only [P1.HKind.bits] at this; omega + · intro e + have := J.atab_obj hh hHt (fun _ e => by cases e) + rw [← e] at this; simp only [P1.HKind.bits] at this; omega + have scw : ∀ q z, H q = some (.sc z) → ∀ w : U64, P1.Aligned w → q.val ≤ w.val → w.val < q.val + 2 ^ z → + L.mem s' w = L.mem s w := fun q z hq w hal g1 g2 => + J.mp_objK hh hw hHo hq (fun _ e => by cases e) (fun e => by cases e) hal g1 (by simpa only [P1.HKind.bits] using g2) + refine ⟨fun t ht => ⟨tw t ht _ (mcpA_aligned hT ht) (by rw [mcpA_val hT ht]; omega) (by rw [mcpA_val hT ht]; omega), + tw t ht _ (flagsA_aligned hT ht) (by rw [flagsA_val hT ht]; omega) (by rw [flagsA_val hT ht]; omega)⟩, + fun t ht => tw t ht _ (yieldA_aligned hT ht) (by rw [yieldA_val hT ht]; omega) (by rw [yieldA_val hT ht]; omega), + fun t ht => tw t ht _ (tIpcBufA_aligned (hT t ht)) (by rw [tIpcBufA_val (hT t ht)]; omega) + (by rw [tIpcBufA_val (hT t ht)]; omega), fun q hq => ?_, fun r hr => ?_, fun q z hq K hK8 hK => ?_⟩ + · obtain ⟨xq, nq, hkq, hin⟩ := screply_obj hS hq + have hHq := (hJ.sc q xq nq (J.c0 q hq) hkq).2 + exact scw q _ hHq _ (sc_reply_word hS hq).1 hin.1 hin.2 + · have hal := J.rp.align r hr + have hHr : H r = some .reply := J.zH.2.2.1 r hr + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + J.mp_objK hh hw hHo hHr (fun _ e => by cases e) (fun e => by cases e) (J.rp.aligned hr ha) (by omega) + (by simp only [P1.HKind.bits]; omega) + exact ⟨k _ (Or.inl (rPrevA_val hal)), k _ (Or.inr (rNextA_val hal))⟩ + · obtain ⟨hal, hv, hf⟩ := J.scW_in hq hK8 hK + exact scw q z hq _ hal (by omega) (by omega) + + + +/-- **P1's `BlkOK` after `MakePool`** at `H` grown by the pool's page: the +slot words are cap-slot words (`JAtZ.slotCapW`), the region's words the new +`other` object's (`Ω`, no frame word: no object of `H` there), the +ASID-table word untracked (`JAtZ.vsObjW`, `JAtZ.vsNodeW`). -/ +theorem JAtZB.mpBlkAfter {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {p : U64} (Ad : P1.AddOKP st H (· = p) (.other 12) Cs s) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + {hi : U64} (hh : hi.val < 2048) {s' : S} (hw : P1.WritesIn st s s' (MpW Z.th p (L.ksb s) hi)) : + P1.BlkOK st (P1.Heap.tcbs (hAddP H p)) Cs (P1.Heap.omega (hAddP H p)) (hAddP H p) (P1.Heap.frame (hAddP H p)) + (P1.Heap.replies (hAddP H p)) (P1.Heap.scs (hAddP H p)) (P1.Heap.ntfns (hAddP H p)) + (P1.Heap.scBits (hAddP H p)) Ac s' := by + have B0 := P1.BlkOK.addOther Ad J.kern.heap J.blk + have hw' : P1.WritesIn st s s' (fun b => P1.Aligned b ∧ MpW Z.th p (L.ksb s) hi b) := + ⟨hw.1, hw.2.1, fun b hb hn => hw.2.2 b hb fun h => hn ⟨hb, h⟩⟩ + have hT : P1.Heap.tcbs (hAddP H p) = P1.Heap.tcbs H := Ad.tcbs_eq (by simp) + have hF : P1.Heap.frame (hAddP H p) = P1.Heap.frame H := Ad.frame_eq_other + have hp : H p = none := Ad.fresh p rfl + have hHk := J.kern.heap + refine (P1.blk_of_capw_untracked B0 hw' fun b ⟨hal, hb⟩ => ?_).1 + rw [hT, hF] + rcases hb with ⟨x, k, hx, hk, rfl⟩ | ⟨h1, h2⟩ | rfl + · rcases J.slotCapW P1.tlow_slots _ ⟨x, hx, k, hk, rfl⟩ with h | ⟨h1, h2⟩ + · exact Or.inl (Or.inl h) + · exact Or.inl (Or.inr ⟨Ad.omega_sub h1, h2⟩) + · refine Or.inl (Or.inr ⟨⟨hal, p, .other 12, hAddP_self, Or.inr (Or.inr (Or.inl ⟨12, rfl⟩)), + ⟨h1, by simpa [P1.HKind.bits] using h2⟩⟩, fun hf => ?_⟩) + obtain ⟨-, q, k, hq, -, g1, g2⟩ := hf + rcases hHo q k hq with e | e <;> omega + · have hb : VsW Z (L.ksb s) (atA (L.ksb s) hi) := Or.inr (Or.inl ⟨hi, hh, rfl⟩) + have Ap : VsApart (L := L) Z H Cs s := ⟨J.scOut⟩ + refine Or.inr ⟨⟨J.vsW_al hb, fun t ht => ?_, fun c hc => ?_⟩, fun e he => ?_, fun hf => ?_⟩ + · rcases J.vsObjW Ap hb (p := t) (k := .tcb) ht (fun _ e => by cases e) with e | e + · left; omega + · right; simpa [P1.HKind.bits] using e + · rcases J.vsNodeW hb hc with e | e <;> omega + · have he' : H e = some .ep ∨ H e = some .ntfn := by + rcases he with he | he + · exact Or.inl ((hAddP_iff (fun _ e => by cases e) hp).1 he) + · exact Or.inr ((hAddP_iff (fun _ e => by cases e) hp).1 he) + have hb8 : (atA (L.ksb s) hi).val % 8 = 0 := J.vsW_al hb + rcases he' with he' | he' + · have ha := hHk.align e _ he' + simp only [P1.HKind.bits] at ha + rcases J.vsObjW Ap hb he' (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · have ha := hHk.align e _ he' + simp only [P1.HKind.bits] at ha + rcases J.vsObjW Ap hb he' (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · obtain ⟨-, q, k, hq, hk, hin⟩ := hf + have hko : ∀ z, k ≠ .other z := by + rcases hk with rfl | ⟨z, rfl⟩ | ⟨rfl, -⟩ <;> intro z e <;> cases e + obtain ⟨g1, g2⟩ := hin + rcases J.vsObjW Ap hb hq hko with e | e <;> omega + + + + +set_option maxHeartbeats 4000000 in +/-- **The untyped caps' facts after `MakePool`** (as `ins_utcG`): the +source's cap now full (free part empty; it has the new pool cap as its +child, so the childless facts are vacuous), every other untyped cap the +same, its free part missing every slot (`RegionFree`), the region (zero +after) and the ASID-table word (`UtKsApart`); the new MDB slot misses every +childless untyped region (the destination CNode live, `live_slot`). -/ +theorem JAtZB.mp_utc {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {σ' : Fl4Spec.State Unit} {G' : MdbGhost} {p hi : U64} (hh : hi.val < 2048) + {pp : U64} {rs : Nat} {idx : List Bool} (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {fi0 : U64} + (hsrc0 : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi0 false)) + (hnch : ¬ ∃ sl, σ.cdt sl = some (pp.bv, idx)) + {pd : U64} {rd : Nat} {idxd : List Bool} (hpd : Z.th pd = some (.cnode rd)) (hld : idxd.length = rd) + (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) (hDL : DestLive Z.th (L.mem s) (L.ksb s) pd) + (hsrc : capV (L.mem s') (cslotAddr (pp.bv, idx)) = .Untyped p fl4_abi.consts.bits.ASID_POOL 4096#u64 false) + (hdst : ∃ b, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p b) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pp.bv, idx) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) + (hw : P1.WritesIn st s s' (MpW Z.th p (L.ksb s) hi)) + (hzero : ∀ w : U64, P1.Aligned w → p.val ≤ w.val → w.val < p.val + 4096 → L.mem s' w = 0#u64) + (hcdt : σ'.cdt = Function.update σ.cdt (pd.bv, idxd) (some (pp.bv, idx))) + (horig : ∀ sl, sl ≠ (pd.bv, idxd) → σ'.isOriginalCap sl = σ.isOriginalCap sl) + (hgp : σ'.archState.x64GlobalPml4 = σ.archState.x64GlobalPml4) + (hM' : MdbLinks Z.th (L.mem s') G') + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl q sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped q sb fi dev) → dev = false → + UtCapP3 (L := L) arI Z.th s' σ' G' sl q sb fi := by + intro sl q sb fi dev hsl hcap' hdF + subst hdF + obtain ⟨hP, ⟨-, hV, hM, hC, hI⟩, -⟩ := J.xtop.rel + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hsd : cslotAddr (pp.bv, idx) ≠ cslotAddr (pd.bv, idxd) := by + intro e; rw [e, hnull] at hsrc0; cases eq_of_ok_eq hsrc0 + have hdnm : cslotAddr (pd.bv, idxd) ∉ Z.G.chains.flatten := fun h => by + obtain ⟨-, c0, h0, hn⟩ := (hM.members _).1 h + rw [hnull] at h0; exact hn (eq_of_ok_eq h0).symm + have hsm : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hAs.isSlot, _, hsrc0, fun h => by cases h⟩ + have hdl := fun {sl0 : Fl4Spec.CslotPtr} {q0 sb0 fi0' : U64} (h1 : AbsSlot Z.th sl0) + (h2 : capV (L.mem s) (cslotAddr sl0) = .Untyped q0 sb0 fi0' false) (h3 : ¬ ∃ x, σ.cdt x = some sl0) => + J.live_slot arI (w := (pd.bv, idxd)) h1 h2 rfl h3 ⟨pd, rd, rfl, hpd, hld, hDL⟩ + have hta : (atA (L.ksb s) hi).val = (L.ksb s).val + 288000 + 8 * hi.val := atA_val J.xtop.fin.1.2.vs.ksb_fit hh + by_cases hs : cslotAddr sl = cslotAddr (pp.bv, idx) + · -- the source: full now + have e := hsl.isSlot + have hsle : sl = (pp.bv, idx) := cslotAddr_inj hP hsl hAs hs + subst hsle + have hc := capV_of_capAt hcap' + rw [hsrc] at hc + simp only [capability.Cap.Untyped.injEq] at hc + obtain ⟨e1, e2, e3, -⟩ := hc + subst q; subst sb; subst fi + have U := J.utc _ _ _ fi0 false hAs hsrc0 rfl + have B : NotDev (L.mem s) (cslotAddr (pp.bv, idx)) p fl4_abi.consts.bits.ASID_POOL fi0 := + notDev_ram (capV_of_capAt hsrc0) + have hch : ∃ x, σ'.cdt x = some (pp.bv, idx) := ⟨(pd.bv, idxd), by rw [hcdt, Function.update_self]⟩ + have h4 : (4096#u64 : U64).val = 4096 := rfl + refine ⟨U.pal, U.pfit, by rw [two_pow_asidPool, h4], fun _ => U.high B, fun _ => by rw [hcpu, hna]; exact U.actZ B, + fun q k hk => by + have := U.nostr q k hk + rw [two_pow_asidPool] at this; rw [two_pow_asidPool, h4]; omega, fun _ w _ h1 h2 => by rw [h4] at h1; rw [two_pow_asidPool] at h2; omega, + U.sb4, U.nostr, fun y _ => by rw [two_pow_asidPool, h4]; omega, fun h0 => absurd hch h0, + fun _ h0 => absurd hch h0, + fun _ => by rw [hna]; exact U.dwReg B, fun _ => by rw [hgp]; exact U.gReg B⟩ + have hne : cslotAddr sl ≠ cslotAddr (pd.bv, idxd) := fun e => by + obtain ⟨b, hb⟩ := hdst + have hc := capV_of_capAt hcap' + rw [e, hb] at hc; cases hc + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped q sb fi false := by + rw [← hcap _ hsl.isSlot hs hne]; exact capV_of_capAt hcap' + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped q sb fi false) := by rw [capAt_capV, hcv] + have U := J.utc sl q sb fi false hsl hcap0 rfl + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + have hfi := U.fiLe + have hKs := J.jcov.ks _ hxm q sb fi hcv + -- a slot misses the free part + have hobj : ∀ y, IsSlot Z.th y → y.val + 32 ≤ q.val + fi.val ∨ q.val + 2 ^ sb.val ≤ y.val := by + intro y hy + obtain ⟨q', i, hi', rfl⟩ := hy + obtain ⟨k, hk, hs'⟩ := hi'.size + have hv := hi'.val hP + rcases U.free q' k hk with e | e <;> omega + have hnc' : (¬ ∃ x, σ'.cdt x = some sl) → ¬ ∃ x, σ.cdt x = some sl := by + rintro h0 ⟨x, hx⟩ + apply h0 + by_cases e : x = (pd.bv, idxd) + · exfalso + subst e + have h1 := hC.cdt _ hAd + rw [hx] at h1 + exact hdnm (hM.tree.par_mem _ _ h1.symm).1 + · exact ⟨x, by rw [hcdt, Function.update_of_ne e]; exact hx⟩ + have B := notDev_ram hcv + refine ⟨U.pal, U.pfit, U.fiLe, fun _ => U.high B, fun _ => by rw [hcpu, hna]; exact U.actZ B, U.free, + fun _ w hal h1 h2 => ?_, U.sb4, + U.nostr, fun y hy => ?_, fun h0 y hy => ?_, + fun _ h0 hf0 => ?_, fun _ => by rw [hna]; exact U.dwReg B, fun _ => by rw [hgp]; exact U.gReg B⟩ + · by_cases hb : MpW Z.th p (L.ksb s) hi w + · rcases hb with ⟨x, k, hx, hk, rfl⟩ | ⟨g1, g2⟩ | rfl + · exfalso + have hf := hx.fit hP + have := hk.lt + have hv := wordOf_val (a := x) (k := k) (by omega) + rw [hv] at h1 h2 + rcases hobj x hx with e | e <;> omega + · exact hzero w hal g1 g2 + · exfalso + rw [hta] at h1 h2 + rcases hKs.2.2 with e | e <;> omega + · rw [hw.2.2 w hal hb]; exact U.zero B w hal h1 h2 + · have := hobj y ((hM'.members y).1 hy).1 + omega + · have hnc := hnc' h0 + by_cases e : y = cslotAddr (pd.bv, idxd) + · subst e + exact (hdl hsl hcv hnc).1 + · obtain ⟨hys, c0, h0', hn⟩ := (hM'.members y).1 hy + by_cases e2 : y = cslotAddr (pp.bv, idx) + · subst e2; exact U.ns hnc _ hsm + have hc0 : capAt (L.mem s) y = ok c0 := by + rw [capAt_capV, ← hcap y hys e2 e, ← capAt_capV]; exact h0' + have hym : y ∈ Z.G.chains.flatten := (hM.members y).2 ⟨hys, c0, hc0, hn⟩ + exact U.ns hnc y hym + · have hnc := hnc' h0 + obtain ⟨r1, r3⟩ := U.rst B hnc hf0 + refine ⟨fun sl' h => ?_, by rw [hna, hDbg]; exact r3⟩ + by_cases e : sl' = (pd.bv, idxd) + · subst e + exact (hdl hsl hcv hnc).2 + · exact r1 sl' (by rw [← horig sl' e]; exact h) + + + +set_option maxHeartbeats 4000000 in +/-- **`SpecFree` after `MakePool`**: every spec object after is one before +of the same size (`SpecFree` before, the free indices no lower: the +source's now full) but the pool at `p`, which lies in the source's region: +the source's own free part is empty, and any other RAM untyped cap's free +part misses it (`UtInv.drange`: the caps' ranges disjoint, or the source a +descendant of that cap, so in its used part, `UtInv.inc`; the source has no +descendant). -/ +theorem JAtZB.mp_sfree {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {σ' : Fl4Spec.State Unit} {G' : MdbGhost} {p : U64} + {pp : U64} {rs : Nat} {idx : List Bool} (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {fi0 : U64} + (hsrc0 : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi0 false)) + (hnch : ¬ ∃ sl, σ.cdt sl = some (pp.bv, idx)) + {pd : U64} {idxd : List Bool} + (hsrc : capV (L.mem s') (cslotAddr (pp.bv, idx)) = .Untyped p fl4_abi.consts.bits.ASID_POOL 4096#u64 false) + (hdst : ∃ b, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p b) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pp.bv, idx) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) + (hM' : MdbLinks Z.th (L.mem s') G') + (hkp : σ'.kheap p.bv = some (.archObj (.asidPool fun _ => none))) + (hkh : ∀ o, o ≠ p.bv → σ'.kheap o = σ.kheap o ∨ (o ∈ Fl4Proofs.P3.regionSet p.bv 12 ∧ σ'.kheap o = none) ∨ + ∃ r cs cs', σ.kheap o = some (.cnode r cs) ∧ σ'.kheap o = some (.cnode r cs')) : + SpecFree (L.mem s') G' σ' := by + intro x hx q sb fi hc o ko ho + obtain ⟨hP, ⟨-, hV, hM, hC, hI⟩, -⟩ := J.xtop.rel + have hxs := ((hM'.members x).1 hx).1 + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hsm : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hAs.isSlot, _, hsrc0, fun h => by cases h⟩ + have hsv := capV_of_capAt hsrc0 + have Us := J.utc _ _ _ fi0 false hAs hsrc0 rfl + have hfi0 : fi0.val ≤ 4096 := by have := Us.fiLe; rwa [two_pow_asidPool] at this + have hold : o ≠ p.bv → ∃ ko0, σ.kheap o = some ko0 ∧ Fl4Spec.objBits ko0 = Fl4Spec.objBits ko := by + intro hne + rcases hkh o hne with e | ⟨-, e⟩ | ⟨r, cs, cs', h1, h2⟩ + · exact ⟨ko, by rw [← e]; exact ho, rfl⟩ + · rw [e] at ho; cases ho + · rw [h2] at ho; cases ho; exact ⟨_, h1, rfl⟩ + -- the source's range + have hsrng := ut_range (capV_ut_sb hsv) (by rw [← hsv]; exact J.ut.reg _ hsm (by rw [hsv]; exact physF_ut _ _ _ _)) + rw [two_pow_asidPool] at hsrng + by_cases hxs1 : x = cslotAddr (pp.bv, idx) + · subst hxs1 + rw [hsrc] at hc + simp only [capability.Cap.Untyped.injEq] at hc + obtain ⟨e1, e2, e3, -⟩ := hc + subst q; subst sb; subst fi + rw [two_pow_asidPool] + have h4 : (4096#u64 : U64).val = 4096 := rfl + rw [h4] + by_cases hop : o = p.bv + · subst hop + rw [hkp] at ho; cases ho + rw [objBits_pool, UScalar.bv_toNat] + right; exact ⟨le_rfl, by norm_num⟩ + · obtain ⟨ko0, h0, hb⟩ := hold hop + have := J.sfree _ hsm p _ fi0 hsv o ko0 h0 + rw [hb, two_pow_asidPool] at this + omega + by_cases hxd : x = cslotAddr (pd.bv, idxd) + · exfalso + obtain ⟨b, hb⟩ := hdst + rw [hxd, hb] at hc; cases hc + have hcv : capV (L.mem s) x = .Untyped q sb fi false := by rw [← hcap x hxs hxs1 hxd]; exact hc + have hxm : x ∈ Z.G.chains.flatten := (hM.members x).2 ⟨hxs, _, by rw [capAt_capV, hcv], fun h => by cases h⟩ + by_cases hop : o = p.bv + · subst hop + rw [hkp] at ho; cases ho + rw [objBits_pool, UScalar.bv_toNat] + have hxr := ut_range (capV_ut_sb hcv) (by rw [← hcv]; exact J.ut.reg _ hxm (by rw [hcv]; exact physF_ut _ _ _ _)) + have h12 : (2 : Nat) ^ 12 = 4096 := by norm_num + rw [h12] + by_cases hd : Disj (capV (L.mem s) x) (capV (L.mem s) (cslotAddr (pp.bv, idx))) + · rw [hcv, hsv] at hd + unfold Disj at hd + left; omega + · rcases J.ut.drange _ hsm p _ fi0 false hsv x hxm hxs1 (by rw [hcv]; exact physF_ut _ _ _ _) hd with h | h + · exact absurd h (nodesc_of_cdt hP hC hAs hnch x) + · obtain ⟨-, g1, g2⟩ := J.ut.inc x hxm q sb fi false hcv _ h + rw [hsv] at g1 g2 + right; omega + · obtain ⟨ko0, h0, hb⟩ := hold hop + rw [← hb]; exact J.sfree x hxm q sb fi hcv o ko0 h0 + + +set_option maxHeartbeats 16000000 in +/-- **`JAtZB` after `MakePool` when its region is clean** (module docstring), +from the restart's state `s`: FL4 writes the `MpW` words (slots, the +region, zero after, and one ASID-table word), the source's cap full and the +destination's the pool cap, the other slots kept; the spec's step is the +pool's creation, two CNodes' change and the CDT's and original bits' at the +destination (`MpFr` once the region holds no spec object, `hspec`). +`XTopAt` after at the new ghost and pool set given. -/ +theorem JAtZB.mpStepJ {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {p hi : U64} (hh : hi.val < 2048) + (hTh : ∀ q k, Z.th q = some k → q.val + max (objSize k) 1 ≤ p.val ∨ p.val + 4096 ≤ q.val) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + (Ad : P1.AddOKP st H (· = p) (.other 12) Cs s) + (hspec : ∀ o ko, σ.kheap o = some ko → o.toNat < p.val ∨ p.val + 4096 ≤ o.toNat) + {pp : U64} {rs : Nat} {idx : List Bool} (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {fi0 : U64} + (hsrc0 : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi0 false)) + (hnch : ¬ ∃ sl, σ.cdt sl = some (pp.bv, idx)) + {pd : U64} {rd : Nat} {idxd : List Bool} (hpd : Z.th pd = some (.cnode rd)) (hld : idxd.length = rd) + (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) (hDL : DestCap Z.th (L.mem s) pd) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {G' : MdbGhost} + (hX' : XTopAt (L := L) AR (Z.mp G' p) s' a' σ') + (hw : P1.WritesIn st s s' (MpW Z.th p (L.ksb s) hi)) + (hzero : ∀ w : U64, P1.Aligned w → p.val ≤ w.val → w.val < p.val + 4096 → L.mem s' w = 0#u64) + (hMs : MpStep p σ σ') + (hkp : σ'.kheap p.bv = some (.archObj (.asidPool fun _ => none))) + (hkh : ∀ o, o ≠ p.bv → σ'.kheap o = σ.kheap o ∨ (o ∈ Fl4Proofs.P3.regionSet p.bv 12 ∧ σ'.kheap o = none) ∨ + ∃ r cs cs', σ.kheap o = some (.cnode r cs) ∧ σ'.kheap o = some (.cnode r cs')) + (hcdt : σ'.cdt = Function.update σ.cdt (pd.bv, idxd) (some (pp.bv, idx))) + (horig : ∀ sl, sl ≠ (pd.bv, idxd) → σ'.isOriginalCap sl = σ.isOriginalCap sl) + (hsrc : capV (L.mem s') (cslotAddr (pp.bv, idx)) = .Untyped p fl4_abi.consts.bits.ASID_POOL 4096#u64 false) + (hdst : ∃ b, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p b) + (hdstb : ∀ b, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p b → b.val % 512 = 0 ∧ b.val < 2 ^ 20) + (hbhi : ∀ b, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p b → b.val = hi.val * 512) + (hat0 : σ.archState.x64AsidTable (Fl4Spec.ucast 11 hi.bv) = none) + (hat : σ'.archState.x64AsidTable = Function.update σ.archState.x64AsidTable (Fl4Spec.ucast 11 hi.bv) (some p.bv)) + (hp0 : p ≠ 0#u64) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pp.bv, idx) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) + (hDel : DelInv Z.th (L.mem s')) (hU' : UtInv (L.mem s') G') : + JAtZB (L := L) arI AR (Z.mp G' p) (hAddP H p) Cs Ac Lst D s' a' σ' ∧ MpFr σ σ' := by + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hPl := J.xtop.rel.1 + obtain ⟨-, ⟨-, -, hM, hC, -⟩, -⟩ := J.xtop.rel + obtain ⟨-, ⟨-, -, hM', -, -⟩, hFI', -, -, -, -⟩ := hX'.rel + have hfit : p.val + 4096 ≤ 2 ^ 64 := Ad.fits p rfl + have hp : H p = none := Ad.fresh p rfl + have hthp : Z.th p = none := by + cases e : Z.th p with + | none => rfl + | some k => have := hTh p k e; omega + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hsm : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hAs.isSlot, _, hsrc0, fun h => by cases h⟩ + have hsv := capV_of_capAt hsrc0 + have hnv := capV_of_capAt hnull + -- the spec's frame + have hσp : σ.kheap p.bv = none := by + cases e : σ.kheap p.bv with + | none => rfl + | some ko => have := hspec _ ko e; rw [UScalar.bv_toNat] at this; omega + have F : MpFr σ σ' := by + refine ⟨fun o => ?_, hMs.1.ct, hMs.1.idle, hMs.1.cons, hMs.1.ctime, hMs.1.csc, hMs.1.isc, hMs.1.act, hMs.1.rq, + hMs.1.rel, hMs.1.gpml4⟩ + by_cases hop : o = p.bv + · subst hop; exact Or.inr (Or.inr ⟨hσp, _, hkp⟩) + rcases hkh o hop with e | ⟨hin, e⟩ | h + · exact Or.inl e + · left + rw [e] + cases e' : σ.kheap o with + | none => rfl + | some ko => + have h1 := hspec o ko e' + have h2 := (regionSet_iff p ⟨o⟩ 12 (by decide) (by simpa using hfit)).1 hin + have h2' : p.val ≤ o.toNat ∧ o.toNat < p.val + 2 ^ 12 := h2 + omega + · exact Or.inr (Or.inl h) + -- the words kept + obtain ⟨k1, k2, kb, k3, k4, hOut⟩ := J.mp_keeps hh hw.2.2 hHo + have ck : Sched.CKeep σ σ' := ⟨fun q y n hq => ⟨y, F.sc.2 hq, rfl⟩, fun q x hq => ⟨x, F.reply.2 hq, rfl, rfl⟩, + fun p x n hp => ⟨x, F.sc.1 hp⟩⟩ + have yk : YKeep σ σ' := ⟨fun p x hp => let ⟨y, hy, hc⟩ := F.tcbF hp; ⟨y, hy, hc.yld⟩, fun p x n hp => ⟨x, F.sc.2 hp, rfl⟩, + fun p y n hp => ⟨y, F.sc.1 hp, rfl⟩⟩ + have mk : McpKeep σ σ' := fun p x hp => let ⟨y, hy, hc⟩ := F.tcbF hp; ⟨y, hy, hc.mcpF⟩ + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' : Sched.RRecv (L.mem s') Z.R σ' := fun d hd hr => by + obtain ⟨h1, h2⟩ := J.rrecv d hd (F.recvB hr) + exact ⟨(k4 d hd).2.trans h1, (k4 d hd).1.trans h2⟩ + have hS' := hX'.fin.1.1.1 + have hidl : Sched.IdleKeep (L := L) s s' := J.idleW_keep hS' F.idle F.isc + -- the debug word + have hN0 : P1.NodePlaced st Z.T s 0#u64 := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk _ J.kern.cs0 + have hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = + L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by + have hdv := P1.nodeAt_val hN0 (off := layout.node.DEBUG_TCBS) (by simp [layout.node.DEBUG_TCBS]) + have hdo : layout.node.DEBUG_TCBS.val < 4288 := by simp [layout.node.DEBUG_TCBS] + refine hw.2.2 _ J.kern.dwAl fun hb => ?_ + rcases hb with ⟨x, k, hx, hk, e⟩ | ⟨g1, g2⟩ | e + · have hf := hx.fit hPl + have := hk.lt + have hv := wordOf_val (a := x) (k := k) (by omega) + exact J.kern.dwObj x hx ⟨by rw [e, hv]; omega, by rw [e, hv]; omega⟩ + · have := Ad.nodes p rfl _ J.kern.cs0 + simp only [P1.HKind.bits] at this + have hnb : L.nodeBase s 0#u64 = (L.ksb s).val + 13568 + (0#u64 : U64).val * 4288 := rfl + omega + · have hb : VsW Z (L.ksb s) (atA (L.ksb s) hi) := Or.inr (Or.inl ⟨hi, hh, rfl⟩) + rw [← e] at hb + rcases J.vsNodeW hb J.kern.cs0 with e' | e' <;> omega + -- the dead stay dead + have hAI : Sched.ActIdle σ' := J.act.mp F + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_keep hS' hX'.ipc + (fun x hx _ => let ⟨y, hy, hc⟩ := F.tcbF hx; ⟨y, hy, hc.st, hc.ntfn, Or.inl hc.sc⟩) + (fun x hx => F.ep.2 hx) (fun x hx => ⟨x, F.ntfn.2 hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.reply.2 hx, rfl⟩) (fun x n hx => ⟨x, F.sc.2 hx, Or.inl rfl, Or.inl rfl⟩) (k3 q) + (fun z hz _ => hOut q z hz) F.idle hAI.1 (J.yld.frame k2 yk) (yk.dk q.bv) (J.jcov.dead q hq) + -- the slots + have hTKH : TypedKeptH Z.th H (Sched.idleScW (L := L) s) (L.mem s) (L.mem s') := by + intro y hy + by_cases e1 : y = cslotAddr (pp.bv, idx) + · left; rw [e1, hsrc]; rfl + by_cases e2 : y = cslotAddr (pd.bv, idxd) + · left; obtain ⟨b, hb⟩ := hdst; rw [e2, hb]; rfl + · right; left; exact ⟨y, hy, by rw [hcap y hy e1 e2]⟩ + have hfh : ∀ t, Z.th t = some .tcb → capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 3)) = .Null ∨ + ∃ ep b0 b1 b2 b3 b4, capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 3)) = + .Endpoint ep b0 b1 b2 b3 b4 := by + intro t ht + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hi3 : IsSlotAt Z.th t 3 := Or.inr ⟨ht, by decide⟩ + rw [cslot_tcb3, hcap _ ⟨t, 3, hi3, rfl⟩ (slotOf_ne_cnV hPl hi3 ht hpp hidx) (slotOf_ne_cnV hPl hi3 ht hpd hld), + ← cslot_tcb3] + exact hJ.fh t ht + have hbf : ∀ t, Z.th t = some .tcb → + Sched.BufSlotOK (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2))) := by + intro t ht + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hi2 : IsSlotAt Z.th t 2 := Or.inr ⟨ht, by decide⟩ + rw [cslot_tcb2, hcap _ ⟨t, 2, hi2, rfl⟩ (slotOf_ne_cnV hPl hi2 ht hpp hidx) (slotOf_ne_cnV hPl hi2 ht hpd hld), + ← cslot_tcb2] + exact hJ.bfs t ht + have Ad' : P1.AddOKP st H (· = p) (.other 12) Cs s' := addOKP_ksb Ad hks + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th (hAddP H p) Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, (hJ.slotTFHs (fun c y n h => F.sc.1 h) hks J.intL hTKH hfh hbf hidl.2).addP Ad'⟩ + -- the cover + have hCK : CoverKept Z.th (L.mem s) (L.mem s') := by + intro y hy hph hu + by_cases e1 : y = cslotAddr (pp.bv, idx) + · rw [e1, hsv] at hu; cases hu + by_cases e2 : y = cslotAddr (pd.bv, idxd) + · rw [e2, hnv, physF_null] at hph; cases hph + have e := hcap y hy e1 e2 + exact ⟨y, hy, by rw [e]; exact hph, by rw [e]; exact hu, by rw [e], by rw [e]⟩ + have hUK : UtKept Z.th (L.mem s) (L.mem s') := by + intro x hx q sb fi d hc + by_cases e1 : x = cslotAddr (pp.bv, idx) + · rw [e1, hsrc] at hc + simp only [capability.Cap.Untyped.injEq] at hc + obtain ⟨e1', e2', e3', e4'⟩ := hc + subst q; subst sb; subst fi; subst d + have h4 : (4096#u64 : U64).val = 4096 := rfl + have := (J.utc _ _ _ fi0 false hAs hsrc0 rfl).fiLe + rw [two_pow_asidPool] at this + exact ⟨_, hAs.isSlot, fi0, hsv, Or.inl (by rw [h4]; exact this)⟩ + by_cases e2 : x = cslotAddr (pd.bv, idxd) + · obtain ⟨b, hb⟩ := hdst; rw [e2, hb] at hc; cases hc + exact ⟨x, hx, fi, by rw [← hcap x hx e1 e2]; exact hc, Or.inl le_rfl⟩ + have hdm' : cslotAddr (pd.bv, idxd) ∈ G'.chains.flatten := by + obtain ⟨b, hb⟩ := hdst + exact (hM'.members _).2 ⟨hAd.isSlot, .AsidPool p b, by rw [capAt_capV, hb], fun h => by cases h⟩ + have hcovp : Covered (L.mem s') G' p.val (p.val + 4096) := by + obtain ⟨b, hb⟩ := hdst + obtain ⟨r1, r2⟩ := range_ap p b hfit + exact ⟨_, hdm', by rw [hb]; exact physF_ap p b, by rw [hb]; rfl, by rw [hb, r1], by rw [hb, r2] <;> omega⟩ + have hni : ¬ IdleAt (L := L) s' p := fun h => by + have := (J.jcov.idleOut _ hsm p _ fi0 hsv p ((idleAt_of hidl).1 h)).1 + rw [two_pow_asidPool] at this; omega + have hdkM : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := + dbgKeep_of_writes J.blk hw fun b hb => by + rcases hb with ⟨x, k, hx, hk, e⟩ | ⟨g1, g2⟩ | e + · have hf := hx.fit hPl + have := hk.lt + have hv := wordOf_val (a := x) (k := k) (by omega) + exact J.inSlot_notDbg ⟨x, hx, by rw [e, hv]; omega, by rw [e, hv]; omega⟩ + · refine J.notDbg' (fun q hq => ?_) (fun c hc => ?_) + · have := hHo q .tcb hq; simp only [P1.HKind.bits] at this ⊢; omega + · have := Ad.nodes p rfl c hc; simp only [P1.HKind.bits] at this; omega + · have hb' : VsW Z (L.ksb s) (atA (L.ksb s) hi) := Or.inr (Or.inl ⟨hi, hh, rfl⟩) + rw [← e] at hb' + exact J.notDbg' (fun q hq => J.vsObjW ⟨J.scOut⟩ hb' hq (fun _ e => by cases e)) + (fun c hc => by rcases J.vsNodeW hb' hc with e' | e' <;> omega) + have hTdM : Sched.TdKeep (L := L) H s s' := J.tdkeep_of hw fun b hb t ht e' => by + have hv := J.tdA_val' ht + have hHt : H t = some .tcb := J.zH.1 t ht + rcases hb with ⟨x, k, hx, hk, e⟩ | ⟨g1, g2⟩ | e + · have hf := hx.fit hPl + have := hk.lt + have hv' := wordOf_val (a := x) (k := k) (by omega) + exact J.inSlot_ntd ⟨x, hx, by rw [e, hv']; omega, by rw [e, hv']; omega⟩ t ht e' + · have := hHo t .tcb hHt; simp only [P1.HKind.bits] at this; rw [e'] at g1 g2; omega + · have hb' : VsW Z (L.ksb s) (atA (L.ksb s) hi) := Or.inr (Or.inl ⟨hi, hh, rfl⟩) + rw [← e] at hb' + have e2 := J.vsObjW ⟨J.scOut⟩ hb' hHt (fun _ e => by cases e) + simp only [P1.HKind.bits] at e2; rw [e'] at e2; omega + have hjc : JCovD (L := L) Z.th (hAddP H p) G' D Cs s' := + (J.jcov_cover hks hM' hCK hUK hdead hidl (DXKeep.of_dbg hdkM hTdM)).addP hp hthp hcovp hni + -- `JKern` + have horig' : ∀ sl, σ'.isOriginalCap sl = true → σ.isOriginalCap sl = true ∨ AbsSlot Z.th sl := by + intro sl h + by_cases e : sl = (pd.bv, idxd) + · right; rw [e]; exact hAd + · left; rw [← horig sl e]; exact h + have hkern : JKern (L := L) arI (hAddP H p) Cs D Z.th s' σ' := + (J.kern_keep hks hcpu hS' F.ct F.csc horig' (by rw [F.gpml4, hMs.1.huge]) (fun _ _ h => F.arch h) hDbg).addP Ad' + have hutc := J.mp_utc hh hpp hidx hsrc0 hnch hpd hld hnull (hDL.live _) hsrc hdst hcap hw hzero hcdt horig F.gpml4 hM' hDbg + have hblk := J.mpBlkAfter Ad hHo hh hw + have hJC : JCs (L := L) (Z.mp G' p) s' σ' := + ⟨hDel, hU', fun G hG => FinM.of_links J.xtop.rel.1 hM' hG hFI'.fm, + J.mp_sfree hpp hidx hsrc0 hnch hsrc hdst hcap hM' hkp hkh⟩ + have R : JRest (L := L) arI (Z.mp G' p) (hAddP H p) Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 yk, J.mcp.frame k1 mk, + J.buf.frame kb (fun p x hp => let ⟨y, hy, hc⟩ := F.tcbF hp; ⟨y, hy, hc.buf⟩), hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjc, hkern, hutc, J.nsc.mp F, J.inv.mp F, CurInvW.mp J.cur F, + J.own.mp F, J.off.mp F, hAI, J.bsa.mp F, J.isorted.mp F, J.curRun.mp F, hblk⟩ + have hnew : ∀ o ao, σ'.kheap o = some (.archObj ao) → σ.kheap o = none → o = p.bv := by + intro o ao h1 h2 + by_contra hne + rcases hkh o hne with e | ⟨-, e⟩ | ⟨r, cs, cs', e1, e2⟩ + · rw [e, h2] at h1; cases h1 + · rw [e] at h1; cases h1 + · rw [e2] at h1; cases h1 + have hk' : ∀ {k : P1.HKind}, (∀ z, k ≠ .other z) → ∀ q, hAddP H p q = some k ↔ H q = some k := + fun hk q => hAddP_iff hk hp + -- the VSpace caps placed: the source an untyped cap, the destination the + -- new pool's cap (its pool in the grown `P`), the other slots kept + have hvc : Vs.VsSlotsW Z.th (insert p Z.P) Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s') := by + intro y hy + by_cases e1 : y = cslotAddr (pp.bv, idx) + · rw [e1, hsrc]; trivial + by_cases e2 : y = cslotAddr (pd.bv, idxd) + · obtain ⟨b, hb⟩ := hdst + obtain ⟨h1, h2⟩ := hdstb b hb + rw [e2, hb] + exact VCapOK.toW (by simp only [VCapOK]; exact ⟨⟨Set.mem_insert _ _, hp0⟩, h1, h2⟩) + · rw [hcap y hy e1 e2] + exact (J.vsc y hy).mono (Set.subset_insert _ _) subset_rfl subset_rfl subset_rfl subset_rfl + -- the buffer fields (`JBuf`): the pool fresh, CNode slots changed, the + -- region's (absent) objects gone; the source an untyped cap, the + -- destination a pool cap + have hfk : IpcFrKeep σ σ' := by + refine ⟨fun t x' hx => ?_, fun q vms hq => ?_, fun q vms hq => ?_, fun q ao hq => ?_⟩ + · rcases F.kh t with e | ⟨r, cs, cs', -, e⟩ | ⟨-, a0, e⟩ + · exact Or.inr ⟨x', by rw [← e]; exact hx, rfl, rfl⟩ + · rw [hx] at e; cases e + · rw [hx] at e; cases e + · rcases F.kh q with e | ⟨r, cs, cs', e0, -⟩ | ⟨e0, -⟩ + · rw [e]; exact hq + · rw [hq] at e0; cases e0 + · rw [hq] at e0; cases e0 + · by_cases hq0 : q = p.bv + · subst hq0; rw [hkp] at hq; cases hq + · rcases hkh q hq0 with e | ⟨-, e⟩ | ⟨r, cs, cs', -, e⟩ + · rw [← e]; exact hq + · rw [hq] at e; cases e + · rw [hq] at e; cases e + · rcases F.kh q with e | ⟨r, cs, cs', e0, -⟩ | ⟨e0, -⟩ + · exact ⟨ao, by rw [e]; exact hq, rfl⟩ + · rw [hq] at e0; cases e0 + · rw [hq] at e0; cases e0 + have hbf : JBuf (L := L) (Z.mp G' p) s' σ' := by + refine ⟨J.bok.keep hfk, J.fcov.ipcFr hfk, fun y hy => ?_⟩ + by_cases e1 : y = cslotAddr (pp.bv, idx) + · rw [e1, hsrc]; trivial + by_cases e2 : y = cslotAddr (pd.bv, idxd) + · obtain ⟨b, hb⟩ := hdst; rw [e2, hb]; trivial + · rw [hcap y hy e1 e2]; exact (J.fcap y hy).mono fun _ => UFr.keep hfk.page + -- `LiveDN` and `zD` after (LiveFrame's `wstepR`: the destination slot in a + -- CNode named by a CNode cap, the source's free index changed) + have hTHM : P1.Heap.tcbs (hAddP H p) = P1.Heap.tcbs H := Set.ext fun q => hk' (fun _ e => by cases e) q + have hZfM : ∀ y, IsSlot Z.th y → ∀ p' zt n, capV (L.mem s') y = .Zombie p' zt n → + ∃ y0, IsSlot Z.th y0 ∧ capV (L.mem s) y0 = .Zombie p' zt n := fun y hy p' zt n h => by + by_cases e1 : y = cslotAddr (pp.bv, idx) + · subst e1; rw [hsrc] at h; cases h + by_cases e2 : y = cslotAddr (pd.bv, idxd) + · subst e2; obtain ⟨b, hb⟩ := hdst; rw [hb] at h; cases h + · exact ⟨y, hy, by rw [← hcap y hy e1 e2]; exact h⟩ + have hLvM : LiveDN (L := L) Z.th (hAddP H p) Cs G' s' σ' := by + obtain ⟨hL, ⟨lst, hDW⟩, hN⟩ := J.live + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + refine LiveDN.wstepRB J ⟨hL, ⟨lst, hDW⟩, hN⟩ hM' + ⟨lst, by rw [hTHM]; exact dbgWFL_step J.xtop hX' rfl rfl hTe hDW hdkM⟩ + (fun q hq => hjc.dead q (J.zD q hq)) F.idle hdkM [(pd.bv, idxd)] + (fun sl hsl => by + simp only [List.mem_cons, List.not_mem_nil, or_false] at hsl + subst hsl; exact ⟨pd, rd, rfl, hpd, hld, hDL⟩) + (fun y hy hne => by + have e2 : y ≠ cslotAddr (pd.bv, idxd) := hne _ (List.mem_singleton_self _) + by_cases e1 : y = cslotAddr (pp.bv, idx) + · subst e1; rw [hsrc, hsv]; exact LSim.of_shape rfl + · rw [hcap y hy e1 e2]; exact LSim.rfl' _) + (fun sl h => by + by_cases e : sl = (pd.bv, idxd) + · exact Or.inr (by simp [e]) + · exact Or.inl (by rw [← horig sl e]; exact h)) + (fun sl hsl h => by + simp only [List.mem_cons, List.not_mem_nil, or_false] at hsl + subst hsl; obtain ⟨b, hb⟩ := hdst; rw [hb] at h; cases h) + hZfM + (fun y0 hy0 hn => by + by_cases e2 : y0 = cslotAddr (pd.bv, idxd) + · subst e2; exact absurd hnv hn + by_cases e1 : y0 = cslotAddr (pp.bv, idx) + · subst e1 + exact ⟨_, hAs.isSlot, (by rw [hsrc]; exact fun h => by cases h), (by rw [hsrc, hsv]; rfl)⟩ + · exact ⟨y0, hy0, by rw [hcap y0 hy0 e1 e2]; exact hn, by rw [hcap y0 hy0 e1 e2]⟩) + refine ⟨?_, F⟩ + exact J.add_frame rfl rfl rfl hks hX' hJC R (by rw [hks]; exact J.ks) J.thT J.ip + (fun n hn => by rw [hks]; exact J.nk n hn) (J.cover.addP hp F hnew) (J.obj.mp F) + ⟨fun t ht => (hk' (fun _ e => by cases e) t).2 (J.zH.1 t ht), + fun e he => (hk' (fun _ e => by cases e) e).2 (J.zH.2.1 e he), + fun r hr => (hk' (fun _ e => by cases e) r).2 (J.zH.2.2.1 r hr), + fun n hn => (hk' (fun _ e => by cases e) n).2 (J.zH.2.2.2 n hn)⟩ + ⟨fun e he => J.hset.1 e ((hk' (fun _ e => by cases e) e).1 he), + fun n hn => J.hset.2.1 n ((hk' (fun _ e => by cases e) n).1 hn), + fun r hr => J.hset.2.2 r ((hk' (fun _ e => by cases e) r).1 hr)⟩ + (fun c z hc => J.hscC c z ((hk' (fun _ e => by cases e) c).1 hc)) hvc hbf + hLvM (fun q hq => J.zD q (ZTcb.of_zombies hZfM hq)) + ((J.carry.addP F hnew).withP + ⟨J.carry.devApart.frameH (lo := p.val) (hi := p.val + 4096) (fun _ _ h => h) + (fun q k h => by + rcases P1.Heap.add_eq_some.1 h with ⟨rfl, rfl⟩ | ⟨-, h⟩ + · exact Or.inr ⟨le_rfl, by simp [P1.HKind.bits]⟩ + · exact Or.inl h) + (fun y hy p' sb' fi' hc => by + have := J.carry.devSep _ hsm y hy _ _ _ _ _ _ (capV_of_capAt hsrc0) hc + rw [two_pow_asidPool] at this; exact this) + hks (devFrom_kept J.mdbL hM' J.ut hUK).1 + (J.idleH.keep hidl fun q hq => by + have hqp : q ≠ p := fun e => hni (e ▸ hq) + refine Option.ext fun k => ?_ + rw [P1.Heap.add_eq_some] + constructor + · rintro (⟨e, -⟩ | ⟨-, h⟩) + · exact absurd e hqp + · exact h + · intro h; exact Or.inr ⟨hqp, h⟩), + J.carry.devSep.frame (devFrom_kept J.mdbL hM' J.ut hUK).2⟩ + (J.carry.vsTyp.step (fun R h y hy => by + by_cases e1 : y = cslotAddr (pp.bv, idx) + · rw [e1, hsrc]; trivial + by_cases e2 : y = cslotAddr (pd.bv, idxd) + · obtain ⟨b, hb⟩ := hdst; rw [e2, hb]; trivial + · rw [hcap y hy e1 e2]; exact h y hy) (tabSet_mono subset_rfl subset_rfl subset_rfl) subset_rfl rfl hks + (ArchFwd.of_ipcFr hfk)) + (J.carry.devTab.step (lo := p.val) (hi := p.val + 4096) (devFrom_kept J.mdbL hM' J.ut hUK).1.sub + (fun o ao ho hv => by + rcases F.kh o with e | ⟨r, cs, cs', -, h2⟩ | ⟨h1, -⟩ + · rw [e] at ho; exact Or.inl ⟨ao, ho, hv⟩ + · rw [h2] at ho; cases ho + · right; obtain rfl := hnew o ao ho h1; exact ⟨le_rfl, le_rfl⟩) + (fun y hy p' sb' fi' hc => by + have := J.carry.devSep _ hsm y hy _ _ _ _ _ _ (capV_of_capAt hsrc0) hc + rw [two_pow_asidPool] at this; exact this)) + (fun hvx => by + obtain ⟨b, hb⟩ := hdst + have hR := J.xtop.fin.2.1 + have hPt := J.xtop.fin.2.2.1 + have hR' : Vs.VsRel (insert p Z.P) (L.mem s') (L.ksb s) σ' := by rw [← hks]; exact hX'.fin.2.1 + have hPt' : Vs.PtRel Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s') σ' := hX'.fin.2.2.1 + rw [hks] + refine Vs.VsNamed.makePoolS (J.carry.vsNamed hvx) J.vsc hR hPt J.xtop.fin.2.2.2.1 hR' hPt' + (fun q hq => Set.mem_insert_of_mem _ hq) (Set.mem_insert _ _) hAd.isSlot hb (by rw [hnv]; rfl) + (hbhi b hb) hh hp0 (fun y hy hk => ?_) (fun y hy hne hk => ?_) hat0 hat hkp (fun q hq => ?_) + · have hcw := J.vsc y hy + cases hc : capV (L.mem s) y <;> rw [hc] at hk <;> + simp only [Vs.VKey, reduceCtorEq, Option.some.injEq, Prod.mk.injEq] at hk + all_goals try omega + rename_i q0 b0 + rw [hc] at hcw + simp only [Vs.VCapW, Vs.VCapOK] at hcw + obtain ⟨g, hg, -⟩ := hR.pool q0 hcw.1.1 + have hq0 : q0 = p := by simpa using hk + rw [hq0, hσp] at hg; cases hg + · by_cases e1 : y = cslotAddr (pp.bv, idx) + · exfalso; subst e1; rw [hsrc, hsv] at hk; exact hk.elim (fun h => h rfl) (fun h => h rfl) + · exact hcap y hy e1 hne + · obtain ⟨ao, hao⟩ := Vs.arch_of_set hR hPt hq + rcases F.kh q.bv with e | ⟨r, cs, cs', e1, -⟩ | ⟨e1, -⟩ + · exact e + · rw [hao] at e1; cases e1 + · rw [hao] at e1; cases e1)) + +/-- **No `OObj` word in an empty region** (`JAtZ.mpOObj` from `MpEmptyAt`). -/ +theorem JAtZB.mpOObjAt {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {x : U64} (hx : x ∈ Z.G.chains.flatten) {p fi : U64} (hE : MpEmptyAt Z H p) + (hcx : capV (L.mem s) x = .Untyped p fl4_abi.consts.bits.ASID_POOL fi false) : + ∀ b : U64, p.val ≤ b.val → b.val < p.val + 4096 → ¬ Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b := by + intro b h1 h2 hO + obtain ⟨-, hHo, hB⟩ := hE + have hks := J.jcov.ks x hx p _ fi hcx + rw [two_pow_asidPool] at hks + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := by have := J.kern.cpuCs; rw [hc.cur] at this; exact this + have hobj : ∀ {q : U64} {k : P1.HKind}, H q = some k → q.val ≤ b.val → b.val < q.val + 2 ^ k.bits → False := + fun {q k} hq l1 l2 => by have := hHo q k hq; omega + obtain ⟨Tr, Cq, hP⟩ := J.jpl + rcases hO with ⟨o1, o2⟩ | ⟨o1, o2⟩ | ⟨t, ht, o1, o2⟩ | ⟨e, he, o1, o2⟩ | ⟨r, hr, o1, o2⟩ | ⟨n, hn, o1, o2⟩ | + ⟨q, hq, y, n, hk, o1, o2⟩ | ⟨wp, hw, o1, o2⟩ + · have := hks.2.1 Z.c hcC; omega + · have := hks.1; omega + · exact hobj (J.zH.1 t ht) (by omega) (by simp only [P1.HKind.bits]; omega) + · exact hobj (J.zH.2.1 e he) (by omega) (by simp only [P1.HKind.bits]; omega) + · exact hobj (J.zH.2.2.1 r hr) (by omega) (by simp only [P1.HKind.bits]; omega) + · exact hobj (J.zH.2.2.2 n hn) (by omega) (by simp only [P1.HKind.bits]; omega) + · exact hobj (hP.sc q y n (J.c0 q hq) hk).2 o1 (by + simp only [P1.HKind.bits] + have := Nat.pow_le_pow_right (show 0 < 2 by decide) (Nat.le_add_right n Fl4Spec.minSchedContextBits) + omega) + · have := (hB wp hw).1; omega + +set_option maxHeartbeats 1000000 in +/-- **`MpPlacedHyp` from `JAtZ` when the regions are clean** (as VsTopX's +`mpPlacedHyp_of`, from the dead-set cover `JCovD`: a clean region holds no +object, `JAtZ.mpEmpty`, so `restrictTh` keeps `th`, and no `OObj` word, +`JAtZ.mpOObj`). -/ +theorem JAtZB.mpPlacedCAt {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {p : U64} (hE : MpEmptyAt Z H p) {x fi : U64} (hx : x ∈ Z.G.chains.flatten) + (hcx : capV (L.mem s) x = .Untyped p fl4_abi.consts.bits.ASID_POOL fi false) + (th' : TypedHeap) (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) + (hth : th' = restrictTh Z.th p.val (p.val + 4096)) (hk : L.ksb s' = L.ksb s) + (hsc : ∀ (q : U64) y n, σ'.kheap q.bv = some (.schedContext y n) → ∃ y0, σ.kheap q.bv = some (.schedContext y0 n)) + (hRg : MpRegion Z.th Z.G Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s) p) : + (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR s' a' σ' → + TopSR st th' (insert p Z.P) Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR s' a' σ') ∧ + ∀ t : U64, Z.th t = some .tcb → th' t = some .tcb := by + have hTh := hE.1 + have hrt : restrictTh Z.th p.val (p.val + 4096) = Z.th := by + funext q + unfold restrictTh + split + · rename_i hq + cases hk' : Z.th q with + | none => rfl + | some k => have := hTh q k hk'; omega + · rfl + rw [hrt] at hth + subst th' + refine ⟨fun hS => ?_, fun t ht => ht⟩ + obtain ⟨hO, hTP⟩ := hS + obtain ⟨hpal, hpfit, hp0, -, -, -, hPo, hLo, hTo⟩ := hRg + have hout := hTh + have hoob : ∀ b : U64, p.val ≤ b.val → b.val < p.val + 4096 → ¬ Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ' b := + fun b h1 h2 hO' => J.mpOObjAt hx hE hcx b h1 h2 (Sched.OObj.mono hk hsc hO') + have hpool : ∀ b, VsWrite (insert p Z.P) (L.ksb s') b → VsWrite Z.P (L.ksb s') b ∨ + (p.val ≤ b.val ∧ b.val < p.val + 4096 ∧ b.val % 8 = 0) := by + rintro b (h | ⟨q, hq, lo, hl, rfl⟩) + · exact Or.inl (Or.inl h) + · rcases hq with rfl | hq + · right; rw [peA_val hpfit hl]; refine ⟨by omega, by omega, by omega⟩ + · exact Or.inl (Or.inr ⟨q, hq, lo, hl, rfl⟩) + refine ⟨hO, ⟨hTP.ep, ?_, hTP.pt, hTP.pl, fun b hb hv => ?_, hTP.nodeE, hTP.po, hTP.ps, fun b hb hw => ?_, + fun b hw => ?_, fun b hw hsw => ?_, hTP.tko⟩⟩ + · -- `vs` + refine ⟨hTP.vs.ksb_al, hTP.vs.ksb_fit, fun q hq => ?_, fun q hq => ?_⟩ + · rcases hq with rfl | hq + · exact ⟨hpal, hpfit⟩ + · exact hTP.vs.pal q hq + · rcases hq with rfl | hq + · rw [hk]; exact hTo + · exact hTP.vs.ptab q hq + · -- `disj` + rcases hv with h | h | ⟨q, hq, h1, h2'⟩ | h + · exact hTP.disj b hb (Or.inl h) + · exact hTP.disj b hb (Or.inr (Or.inl h)) + · rcases hq with (rfl | hq) | hq + · exact hoob b h1 h2' hb + · exact hTP.disj b hb (Or.inr (Or.inr (Or.inl ⟨q, Or.inl hq, h1, h2'⟩))) + · exact hTP.disj b hb (Or.inr (Or.inr (Or.inl ⟨q, Or.inr hq, h1, h2'⟩))) + · exact hTP.disj b hb (Or.inr (Or.inr (Or.inr h))) + · -- `pv` + rcases hpool b hw with h | ⟨h1, h2', -⟩ + · exact hTP.pv b hb h + · obtain ⟨q, hq, i, hi, rfl⟩ := hb + have hqa := hTP.pt q hq + rw [peA_val (tbl_fit hqa) hi] at h1 h2' + have := hLo q hq; omega + · -- `vo` + rcases hpool b hw with h | ⟨h1, h2', h8⟩ + · exact hTP.vo b h + · refine ⟨h8, fun q k hq hin => ?_⟩ + have := hout q k hq; omega + · -- `sd` + rcases hpool b hw with h | ⟨h1, h2', -⟩ + · exact hTP.sd b h hsw + · obtain ⟨y, hy, hb⟩ := hsw + obtain ⟨q, i, hqi, rfl⟩ := hy + have hv := hqi.val hTP.pl + obtain ⟨k, hk', hs⟩ := hqi.size + have := hout q k hk' + have hf := hTP.pl.fits q k hk' + rcases hb with rfl | rfl + · rw [hv] at h1 h2'; omega + · have : (core.num.U64.wrapping_add (slotOf q i) 8#u64).val = (slotOf q i).val + 8 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; rw [hv]; omega + rw [this, hv] at h1 h2'; omega + +/-- **The region facts of `MakePool`'s childless untyped from `JAtZ` when +the regions are clean** (what the old `JMpExtra` asked [J]): `MpRegion` +(alignment, fit and non-null pointer, no straddling object and no MDB slot +from `UtCapP3`; no CNode, pool or page table there: the region holds no +object of `th` or `H`, `JAtZ.mpEmpty`, and the spec's arch objects are `H`'s +`other` objects of at least a page, `SpecCover.arch`; the ASID table off it, +`UtKsApart`) and `RegOKTop` (`JAtZ.mpOObj`, the buffer bytes by +`MpClean`). -/ +theorem JAtZB.mpRegionCAt {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {pp : U64} {rs : Nat} {idx : List Bool} (hpp : Z.th pp = some (.cnode rs)) + (hidx : idx.length = rs) {p fi : U64} + (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi false)) + (hnch : ¬ ∃ sl, σ.cdt sl = some (pp.bv, idx)) (hE : MpEmptyAt Z H p) : + MpRegion Z.th Z.G Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s) p ∧ RegOKTop (L := L) Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ p := by + obtain ⟨hPl, ⟨-, -, hM, hC, -⟩, -⟩ := J.xtop.rel + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hcx := capV_of_capAt hcap + have hx : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hAs.isSlot, _, hcap, fun h => by cases h⟩ + have hnd := nodesc_of_cdt hPl hC hAs hnch + have U := J.utc _ p _ fi false hAs hcap rfl + have hTh := hE.1 + have hHo := hE.2.1 + have hks := J.jcov.ks _ hx p _ fi hcx + rw [two_pow_asidPool] at hks + have hpal : p.val % 4096 = 0 := by have := U.pal; rwa [two_pow_asidPool] at this + have hpfit : p.val + 4096 ≤ 2 ^ 64 := by have := U.pfit; rwa [two_pow_asidPool] at this + have hoff : ∀ q : U64, (∃ ao, σ.kheap q.bv = some (.archObj ao)) → q.val + 4096 ≤ p.val ∨ p.val + 4096 ≤ q.val := by + rintro q ⟨ao, hq⟩ + obtain ⟨n, hn, hHq⟩ := J.cover.arch _ ao hq + have h1 := hHo q _ hHq + simp only [P1.HKind.bits] at h1 + have h2 : 4096 ≤ 2 ^ n := by + calc 4096 = 2 ^ 12 := rfl + _ ≤ 2 ^ n := Nat.pow_le_pow_right (by decide) hn + omega + have hR := J.xtop.fin.2.1 + have hPt := J.xtop.fin.2.2.1 + refine ⟨⟨hpal, hpfit, ?_, ?_, ?_, ?_, ?_, ?_, by have := hks.2.2; omega⟩, hpal, hpfit, J.mpOObjAt hx hE hcx, + fun wp hw => (hE.2.2 wp hw).2⟩ + · intro e + have hh := U.high (Sched.notDev_ram hcx) + rw [e] at hh + have h0 : (0#u64 : U64).val = 0 := rfl + rw [h0] at hh + norm_num at hh + · have := U.nostr; rwa [two_pow_asidPool] at this + · have := U.ns hnch; rwa [two_pow_asidPool] at this + · rintro q r hq ⟨a1, a2⟩ + have := hTh q _ hq + simp only [objSize] at this + omega + · intro q hq + obtain ⟨f, hf, -⟩ := hR.pool q hq + exact hoff q ⟨_, hf⟩ + · rintro q (hq | hq | hq | hq) + · obtain ⟨f, hf, -⟩ := hPt.l4 q hq; exact hoff q ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l3 q hq; exact hoff q ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l2 q hq; exact hoff q ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l1 q hq; exact hoff q ⟨_, hf⟩ + +/-- **The new pool's page joins `H` as an `other` object** (P1's +`AddOKP`, for `BlkOK.addOther`): a clean region holds no object of `H` +(`JAtZ.mpEmpty`), and RAM untyped regions miss the nodes and the +kernel-state block (`UtKsApart`). -/ +theorem JAtZB.mpAddOKAt {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {x : U64} (hx : x ∈ Z.G.chains.flatten) {p fi : U64} (hE : MpEmptyAt Z H p) + (hcx : capV (L.mem s) x = .Untyped p fl4_abi.consts.bits.ASID_POOL fi false) + (hpal : p.val % 4096 = 0) (hpfit : p.val + 4096 ≤ 2 ^ 64) + (hp0 : p ≠ 0#u64) : + P1.AddOKP st H (· = p) (.other 12) Cs s := by + obtain ⟨-, hHo, -⟩ := hE + have hks := J.jcov.ks x hx p _ fi hcx + rw [two_pow_asidPool] at hks + have h12 : (P1.HKind.other 12).bits = 12 := rfl + refine ⟨fun q hq => ?_, fun q hq => ?_, fun q hq => ?_, fun q hq => ?_, fun q hq p' kp hp' => ?_, + fun q q' hq hq' hne => absurd (hq.trans hq'.symm) hne, fun q hq c hc => ?_, fun q hq => ?_⟩ + · subst hq + cases hk : H q with + | none => rfl + | some k => + have := hHo q k hk + have := Nat.one_le_two_pow (n := k.bits) + omega + · subst hq; rw [h12]; exact hpal + · subst hq; rw [h12]; exact hpfit + · subst hq; exact hp0 + · subst hq; rw [h12, show (2:Nat) ^ 12 = 4096 from rfl]; have := hHo p' kp hp'; omega + · subst hq; rw [h12]; exact hks.2.1 c hc + · subst hq; rw [h12]; exact hks.1 + +/-- **The destination CNode misses a childless untyped cap's region** +(`DestLive`: a CNode cap of the MDB names it, `cnode_off_childless`, or it +lies in the kernel-state block, off every RAM untyped region). -/ +theorem JAtZB.destOff {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {pd : U64} {rd : Nat} (hpd : Z.th pd = some (.cnode rd)) (hDL : DestLive Z.th (L.mem s) (L.ksb s) pd) + {x : U64} (hx : x ∈ Z.G.chains.flatten) {p sb fi : U64} (hcx : capV (L.mem s) x = .Untyped p sb fi false) + (hnd : ∀ y, ¬ Mdb.Desc Z.G.par y x) : ¬ (p.val ≤ pd.val ∧ pd.val < p.val + 2 ^ sb.val) := by + obtain ⟨-, ⟨-, -, hM, -, hI⟩, -⟩ := J.xtop.rel + rcases hDL with ⟨y, r, gs, g, hy, hc⟩ | h + · have hyG : y ∈ Z.G.chains.flatten := (hM.members y).2 ⟨hy, _, capAt_capV _ _, by rw [hc]; simp⟩ + exact cnode_off_childless J.ut hI hx hcx hnd hyG hc + · obtain ⟨k1, -, -⟩ := J.jcov.ks x hx p sb fi hcx + have := h _ hpd + simp only [objSize] at this + have := Nat.one_le_two_pow (n := rd) + omega + + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBRestartN.lean b/proofs/Fl4Proofs/P3/JAtZBRestartN.lean new file mode 100644 index 000000000..fafe816e0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBRestartN.lean @@ -0,0 +1,321 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **`sched::restart` of any thread over `JAtZB`, with `NatFoot`**. + +`JAtZB.undead`, `JAtZB.restartTailN`, `JAtZB.restartTcbN` are the `JAtZB` siblings of XTopJointRestart's `JAtZ.undead`, +`restartTailJ`, `restartTcbJD` (generated by `handoff/tools/gen_restartN.py`), with the native footprint; the cancel +steps are the `JCancelAtB` theorems. `natK_restartTail`: the restart tail is a native spec step. Premise [P] +`MIN_BUDGET` (as native). New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointRestart +import Fl4Proofs.P3.XTopJointCancel +import Fl4Proofs.P3.XTopJointCancelRecv +import Fl4Proofs.P3.XTopJointCancelReply +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.JAtZBThrSched +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section Spec +open Fl4Spec + +/-- The spec's `restart` tail of an inactive thread is a native spec step: the thread becomes `restart`, the rest +changes no thread state. -/ +theorem natK_restartTail {σ σb σc σd σ' : State Unit} {t : ObjRef} {x : Tcb} (hk : σ.kheap t = some (.tcb x)) + (hb : setThreadState (Z := Unit) t .restart σ = ({((), σb)}, False)) + (hc : ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σb = ({((), σc)}, False)) + (hd : NonDet.maybeM (schedContextResume (Z := Unit)) x.tcbSchedContext σc = ({((), σd)}, False)) + (he : testPossibleSwitchTo (Z := Unit) t σd = ({((), σ')}, False)) : NatK σ σ' := by + have mc : ((), σc) ∈ (ifCondRefillUnblockCheck (Z := Unit) x.tcbSchedContext (some false) (some false) σb).1 := by + show ((), σc) ∈ (ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σb).1 + rw [hc]; exact Set.mem_singleton _ + have md : ((), σd) ∈ (NonDet.maybeM (schedContextResume (Z := Unit)) x.tcbSchedContext σc).1 := by + rw [hd]; exact Set.mem_singleton _ + have me : ((), σ') ∈ (testPossibleSwitchTo (Z := Unit) t σd).1 := by rw [he]; exact Set.mem_singleton _ + have hσb : σb.kheap = (updK σ t (.tcb { x with tcbState := .restart })).kheap := by + have e := the_result (Notif.setThreadState_eq (Z := Unit) .restart hk) hb + rw [e, stsAct_kheap] + have nc : NKT (ifCondRefillUnblockCheck (Z := Unit) x.tcbSchedContext (some false) (some false)) := + nkt_ifCondRefillUnblockCheck _ _ _ + have nd : NKT (NonDet.maybeM (schedContextResume (Z := Unit)) x.tcbSchedContext) := + nkt_maybeM nkt_schedContextResume _ + have ne : NKT (testPossibleSwitchTo (Z := Unit) t) := by + unfold testPossibleSwitchTo; exact nkt_bind (nkt_gets _) fun _ => nkt_when _ (nkt_possibleSwitchTo _) + exact (natK_updTcb (y := { x with tcbState := .restart }) hk (fun h => by cases h)).trans + ((NatK.of_kheap hσb).trans (natK_tk (((nc σb _ mc).trans (nd σc _ md)).trans (ne σd _ me)))) + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **An object named by a non-zombie cap of a slot is no TCB zombie's TCB** +(`FinInv.zombie_alone`). -/ +theorem JAtZB.not_ztcb (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {x : U64} (hx : IsSlot Z.th x) + (hnz : ∀ p zt n, capV (L.mem s) x ≠ .Zombie p zt n) {p : U64} + (hp : p.bv ∈ Fl4Spec.objRefs (absCap (capV (L.mem s) x))) : p ∉ ZTcb (L := L) Z.th s := by + rintro ⟨y, hy, zt, n, hz, -⟩ + have hne : x ≠ y := fun e => hnz p zt n (by rw [e, hz]) + exact FinInv.zombie_alone J.xtop.rel.2.2.1 J.mdbL hy hz x hx hne hp + +set_option maxHeartbeats 4000000 in +/-- **A thread with a cap in a slot leaves the dead set** (`cover_cap` at the +same state). -/ +theorem JAtZB.undead (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {x t : U64} (hxS : IsSlot Z.th x) + (hc : capAt (L.mem s) x = ok (.Thread t)) (ht : t ∈ Z.T) : + JAtZB (L := L) arI AR Z H Cs Ac Lst (D \ {t}) s a σ := by + have hw : P1.WritesIn st s s (TFoot st Z.T Z.N Z.C Z.c s σ) := P1.WritesIn.refl _ _ + have hK : ScTKw (fun _ => False) σ σ := ScTKw.refl _ σ + have hHt : H t = some .tcb := J.zH.1 t ht + have hfit := J.kern.heap.fits t _ hHt + simp only [P1.HKind.bits] at hfit + have hcov := J.cover_cap hw hxS hc (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl (range_thread t hfit) + fun k hk' => by + rw [hHt] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + exact { J with + jcov := J.sc_jcov hw (fun q hq => J.jcov.dead q hq.1) (fun q hq hn => by + have e : q = t := Classical.byContradiction fun e => hn ⟨hq, e⟩ + subst e; exact hcov) ⟨rfl, rfl⟩ (P1.DbgKeep.refl s) Set.sdiff_subset + kern := J.sc_kern hw hK (FK.refl σ) J.xtop rfl Set.sdiff_subset + zD := fun q hq => ⟨J.zD q hq, fun (e : q = t) => J.not_ztcb hxS + (by rw [ok_inj ((capAt_capV _ _).symm.trans hc)]; intro _ _ _ h; cases h) + (by rw [ok_inj ((capAt_capV _ _).symm.trans hc), e]; simp [absCap, Fl4Spec.objRefs]) hq⟩ } + +set_option maxHeartbeats 16000000 in +/-- **`restart`'s tail at `J`** (module docstring). -/ +theorem JAtZB.restartTailN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hin : x.tcbState = .inactive) (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) + (hRS : ∀ σb σc, Fl4Spec.setThreadState (Z := Unit) t.bv .restart σ = ({((), σb)}, False) → + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) x.tcbSchedContext σb = ({((), σc)}, False) → + RelOK σc ∧ (∀ xc p, σc.kheap t.bv = some (.tcb xc) → xc.tcbSchedContext = some p → + ∃ y n, σc.kheap p = some (.schedContext y n) ∧ y.scTcb = some t.bv) ∧ + ∀ σd, Fl4Spec.NonDet.maybeM (Fl4Spec.schedContextResume (Z := Unit)) x.tcbSchedContext σc = + ({((), σd)}, False) → Inv σd) : + restartTailF st arI s a t ⦃ r => r.2 = a ∧ ∀ Ac', P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' r.1 → + P1.DbgKeep st (P1.Heap.tcbs H) Cs s r.1 → + ∃ σ', specRestartTail t.bv x.tcbSchedContext σ = ({((), σ')}, False) ∧ + ∃ Lst' : U64 → List U64, JAtZB (L := L) arI AR Z H Cs Ac' Lst' D r.1 a σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + have hti : t.bv ≠ σ.idleThread := fun e => by + obtain ⟨xi, hki, -, hti, -⟩ := J.act.2 + rw [← e, hk] at hki; cases hki; rw [hin] at hti; cases hti + have hq : ∀ n ∈ Z.N, ∀ z, σ.kheap n.bv = some (.notification z) → t.bv ∉ z.ntfnObj.ntfnQueue := by + intro n _ z hz hm + obtain ⟨xu, hku, hsu⟩ := (J.sv.ntfnQ n.bv z hz).2 t.bv hm + rw [hk] at hku; cases hku; rw [hin] at hsu; cases hsu + have hrq : ∀ d p, t.bv ∉ σ.readyQueues d p := by + intro d p hm + obtain ⟨x', -, -, -, h1, -, h3, -⟩ := J.inv.rq d p t.bv hm + rw [hk] at h1; cases h1; rw [hin] at h3; cases h3 + refine WP.spec_mono (restart_tail_kfoot s a σ hR hS ht hk hin hq hrq hRS hmb) ?_ + rintro ⟨s', a'⟩ ⟨ha', σb, σc, σd, σ', hb, hc, hd, he, hR', hS', hI', hw⟩ + simp only at ha' hR' hS' hw ⊢ + subst a' + refine ⟨rfl, fun Ac' hblk hKd0 => ⟨σ', specRestartTail_eq hb hc hd he, ?_⟩⟩ + have hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := + hKd0.2 _ (Or.inr ⟨0#u64, J.kern.cs0, rfl⟩) + have hId : Inv σd := (hRS σb σc hb hc).2.2 σd hd + obtain ⟨hK, hL, hF, hsok, hBK⟩ := restartTail_frame hk hin hb hc hd he + have hwT := WritesIn.tfoot_of_kfoot hw + have hX' : XTopAt (L := L) AR Z s' a σ' := xtop_sc_step hX J.ip J.nk hwT hK hS' hR' id + have hN' : NSC Z.N Z.C σ' := nsc_of_lk J.nsc hK hL + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree ⟨⟨hS', hR', hN', hI'⟩, hw, hsok⟩ + -- the current thread's facts: a reschedule pending (`restartTail_specCn`), or resumed (`MidKn.mid3`) + have hcase : σ.schedulerAction = .chooseNewThread ∨ (t.bv ≠ σ.curThread ∧ CurInv σ) := by + cases e : σ.schedulerAction with + | chooseNewThread => exact Or.inl rfl + | switchThread u => exact (hns u e).elim + | resumeCurThread => + right + have hC : CurInv σ := J.cur.resolve_left (by rw [e]; intro h; cases h) + refine ⟨fun htc => ?_, hC⟩ + rcases J.curRun.resolve_left (fun h => h e) with hrun | hidl + · have h1 := hrun x (htc ▸ hk); rw [hin] at h1; cases h1 + · exact hti (htc.trans hidl) + have hsch : (σ'.schedulerAction = .chooseNewThread ∨ CurInv σ') ∧ CurOwnW Z.T Z.C σ' ∧ OffOK σ' ∧ + ActIdle σ' ∧ CurRunM σ' := by + rcases hcase with hcn | ⟨htc, hC⟩ + · obtain ⟨h1, h2, h3, h4⟩ := + restartTail_specCn hk hin hti hcn J.inv J.act J.off J.own J.sv.scSpor hb hc hd he hId + exact ⟨Or.inl h1, h2, h3, h4, Or.inl (by rw [h1]; intro h; cases h)⟩ + obtain ⟨-, -, -, -, -, hMK, hAI⟩ := restartTail_specJ hk hin hti htc hns J.inv J.act hC hb hc hd he hId + obtain ⟨hC', hOw', hO'⟩ := hMK.mid3 hC J.off J.inv ⟨L.curW s, J.thT _ J.kern.curT, J.kern.cur.symm⟩ + have hcr : CurRunM σ' := by + by_cases hr : σ'.schedulerAction = .resumeCurThread + · have h0 : σ.schedulerAction = .resumeCurThread := by + cases e : σ.schedulerAction with + | resumeCurThread => rfl + | switchThread u => exact (hns u e).elim + | chooseNewThread => rw [hMK.cn e] at hr; cases hr + right + rcases J.curRun.resolve_left (fun h => h h0) with hrun | hidl + · left + intro y hy + obtain ⟨xt, hkt, -, -⟩ := hC.tcb + obtain ⟨y', hy', -, e⟩ := hMK.ctT xt hkt + rw [hMK.ct, hy'] at hy; cases hy + rw [e]; exact hrun xt hkt + · right; rw [hMK.ct, hMK.idle]; exact hidl + · exact Or.inl hr + exact ⟨hC', Or.inl (hC'.elim hOw' (curOwn_of_curInv hX'.fin.1.1.1 hI')), hO', hAI, hcr⟩ + obtain ⟨hC', hOw', hO', hAI, hcr⟩ := hsch + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_stepQ hX' hwT hK (fun _ hp => absurd (show q = t from P1.eq_of_bv hp) (fun e => htD (e ▸ hq))) + hL hF hAI.1 hY' ((hsok.ykeep rfl).dk q.bv) (fun h => h) (J.jcov.dead q hq) + refine ⟨Lst', J.sc_frame hwT hK hX' hCR' hRF' hY' hM' (J.sc_buf hwT hK) hRP' (J.sc_apart hwT hK) + (J.sc_jpl hwT hK (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) (J.sc_jcov hwT hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) + hKd0 subset_rfl) + (J.sc_kern hwT hK hF hX' hDbg fun _ h => h) (J.sc_utc hwT hK hDbg) hN' hI' hC' + hOw' hO' hAI (J.bsa.bk hBK hsok) hcr hblk + hKd0 (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 J.zD, + NatFoot.ofT J.xtop hwT (fun _ h => J.tfoot_nh h) (natK_restartTail hk hb hc hd he)⟩ + +set_option maxHeartbeats 16000000 in +/-- **`sched::restart` at `J`** (module docstring), the target not dead +(`htD`; `JAtZ.restartTcbJ` from its cap in a slot by `JAtZ.undead`). -/ +theorem JAtZB.restartTcbN (hAT : P0.ArchTotal arI) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + (hns : ∀ u, σ.schedulerAction ≠ .switchThread u) : + sched.restart st arI s a t ⦃ r => r.2 = a ∧ ∃ σ', Fl4Spec.restart (Z := Unit) t.bv σ = ({((), σ')}, False) ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + obtain ⟨x, hk, hTR⟩ := hI.1.tcb t ht + -- P1's `BlkOK` and the debug word across the whole restart + have hP1 : sched.restart st arI s a t ⦃ r => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.DbgKeep st (P1.Heap.tcbs H) Cs s r.1 ⦄ := by + obtain ⟨⟨Q, R, h0⟩, hB, hX, hS, hSg⟩ := J.blk + refine WP.spec_mono (P1.sched.restart.allK hAT s a t Q R h0 htH hB hX hS) fun r hr => ?_ + obtain ⟨s', a'⟩ := r + try simp only [WP.uncurry'_pair] at hr + obtain ⟨Q', R', h1, hB1, hX1, hS1, -, hF⟩ := hr + exact ⟨⟨⟨Q', R', h1⟩, hB1, hX1, hS1, ⟨hSg.sub, fun b hb => by rw [hF.1]; exact hSg.ks b hb, + fun o h1' h2' => by rw [hF.1]; exact hSg.ksΩ o h1' h2'⟩⟩, J.kw_dbgKeep hF⟩ + -- the debug word and the kernel-state base through `cancel_ipc` + obtain ⟨⟨Q0, R0, h0⟩, -, ⟨hO, hR, -⟩, -, hSg⟩ := J.blk + have hreply : L.mem s (P1.stA t) = sched.TS_BLOCKED_ON_REPLY → P1.ReplyT st (P1.Heap.replies H) + (P1.Heap.scs H) s t := fun hs => by + obtain ⟨h1, h2⟩ := hR.reply t htH hs + exact ⟨h1, h2, hR.next _ h1, hR.prev _ h1⟩ + have hkwc : ipc.cancel_ipc st arI s a t ⦃ r => L.ksb r.1 = L.ksb s ∧ + P1.DbgKeep st (P1.Heap.tcbs H) Cs s r.1 ⦄ := by + refine WP.spec_mono (P1.ipc.cancel_ipc.kw s a t h0 htH hO hSg.sub hreply + (fun h1 h2 => (hR.recv t htH h1 h2).1)) fun r hr => ?_ + obtain ⟨s1, a1⟩ := r + try simp only [WP.uncurry'_pair] at hr + exact ⟨hr.1, J.kw_dbgKeep hr⟩ + have hspec0 : Fl4Spec.restart (Z := Unit) t.bv σ = + Fl4Spec.NonDet.when (¬ Fl4Spec.runnable x.tcbState = true ∧ ¬ Fl4Spec.idle x.tcbState) (do + Fl4Spec.cancelIpc t.bv + Fl4Spec.setThreadState t.bv .restart + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck x.tcbSchedContext + Fl4Spec.NonDet.maybeM Fl4Spec.schedContextResume x.tcbSchedContext + Fl4Spec.testPossibleSwitchTo t.bv) σ := by + unfold Fl4Spec.restart + rw [nd_bind_one (getThreadState_eq hk), nd_bind_one (getTcbObjRef_eq _ hk)] + have hmain : sched.restart st arI s a t ⦃ r => ∀ Ac', P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' r.1 → + P1.DbgKeep st (P1.Heap.tcbs H) Cs s r.1 → + r.2 = a ∧ ∃ σ', Fl4Spec.restart (Z := Unit) t.bv σ = ({((), σ')}, False) ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + unfold sched.restart + rw [is_stopped_eq (ts := x.tcbState) hTR.st] + simp only [bind_ok] + by_cases hc : ¬ Fl4Spec.runnable x.tcbState = true ∧ ¬ Fl4Spec.idle x.tcbState + swap + · rw [decide_eq_false hc] + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + intro _ _ _ + refine ⟨trivial, σ, ?_, Z.vs, Ac, Lst, D, J, NatFoot.refl Z s σ⟩ + rw [hspec0, Fl4Spec.NonDet.when, if_neg hc]; rfl + rw [decide_eq_true hc] + simp only [↓reduceIte] + have hnr : Fl4Spec.runnable x.tcbState = false := by simpa using hc.1 + -- `cancel_ipc`, the dead set kept (without `t`) + have hcan : ipc.cancel_ipc st arI s a t ⦃ r => r.2 = a ∧ ∃ σa, + Fl4Spec.cancelIpc (Z := Unit) t.bv σ = ({((), σa)}, False) ∧ ∃ xa, σa.kheap t.bv = some (.tcb xa) ∧ + xa.tcbState = .inactive ∧ ∃ (V : XVs) (Ac1 : Set U64) (Lst1 : U64 → List U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac1 Lst1 D r.1 r.2 σa ∧ NatFoot (L := L) Z s σ r.1 σa ⦄ := by + by_cases hfree : IpcFree x.tcbState + · have hin : x.tcbState = .inactive := by + revert hfree hc + cases x.tcbState <;> simp [IpcFree, Fl4Spec.epBlocked, Fl4Spec.ntfnBlocked, Fl4Spec.runnable, Fl4Spec.idle] + refine WP.spec_mono (J.cancelFreeJ ht hk hfree) ?_ + rintro r ⟨h1, h2, V, J1, F1⟩ + exact ⟨h1, _, h2, { x with tcbFault := none }, by simp [updK], hin, V, Ac, Lst, J1, F1⟩ + have hcases : (∃ e d, x.tcbState = .blockedOnSend e d) ∨ (∃ e r d, x.tcbState = .blockedOnReceive e r d) ∨ + (∃ n, x.tcbState = .blockedOnNotification n) ∨ (∃ r, x.tcbState = .blockedOnReply r) := by + revert hfree + cases x.tcbState <;> simp [IpcFree, Fl4Spec.epBlocked, Fl4Spec.ntfnBlocked] + have hP : ipc.cancel_ipc st arI s a t ⦃ r => r.2 = a ∧ ∃ σ', Fl4Spec.cancelIpc (Z := Unit) t.bv σ = + ({((), σ')}, False) ∧ ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + rcases hcases with h | h | h | h + · exact jCancelB_send arI Z H Cs Ac Lst D s a σ rfl J t ht x hk h + · exact jCancelB_recv arI Z H Cs Ac Lst D s a σ rfl J t ht x hk h + · exact jCancelB_ntfn arI Z H Cs Ac Lst D s a σ rfl J t ht x hk h + · exact jCancelB_reply arI Z H Cs Ac Lst D s a σ rfl J t ht x hk h + refine WP.spec_mono (WP.spec_and hP (cancel_ipc_irelP (ar := arI) hP0 (removeTcbHypP_ok hP0) s a σ hI ht hk + J.sv J.obj J.inv (fun hfr => removeTcbOK_fault hP0 hI ht hk hfr J.removeTcbOK))) ?_ + rintro r ⟨⟨h1, σ1, h2, V, Ac1, Lst1, J1, F1⟩, -, σ1', x1, h2', -, hk1, hs1⟩ + have e1 : σ1' = σ1 := by + rw [h2] at h2'; exact ((Prod.mk.inj (Set.singleton_injective (congrArg Prod.fst h2'))).2).symm + subst e1 + have hin : x1.tcbState = .inactive := by + rw [hs1]; revert hfree; cases x.tcbState <;> simp [IpcFree, Fl4Spec.epBlocked, Fl4Spec.ntfnBlocked, cancelSt] + exact ⟨h1, σ1', h2', x1, hk1, hin, V, Ac1, Lst1, J1, F1⟩ + refine WP.spec_bind (WP.spec_and hcan hkwc) ?_ + rintro ⟨s1, a1⟩ ⟨⟨h1, σa, hca, xa, hka, hina, V1, Ac1, Lst1, J1, F1⟩, hks1, hd1⟩ + simp only at h1 hks1 hd1 + subst a1 + -- the spec facts after `cancel_ipc` + have hma : ((), σa) ∈ (Fl4Spec.cancelIpc (Z := Unit) t.bv σ).1 := by rw [hca]; exact Set.mem_singleton _ + have hKa := TscK.sk_cancelIpc t.bv σ ((), σa) hma + have hxsa : xa.tcbSchedContext = x.tcbSchedContext := by + obtain ⟨y0, hy0, e⟩ := hKa.1 _ _ hka; rw [hk] at hy0; cases hy0; exact e + have hnsa : ∀ u, σa.schedulerAction ≠ .switchThread u := + (Vs.ak_cancelIpc' t.bv).noSwitch (x := ((), σa)) hma hns + have hRS0 : RestartSpecOK t.bv σ := Sched.restartSpecOK_of J.inv J.sv.scSpor + have hRS : ∀ σb σc, Fl4Spec.setThreadState (Z := Unit) t.bv .restart σa = ({((), σb)}, False) → + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) xa.tcbSchedContext σb = ({((), σc)}, False) → + RelOK σc ∧ (∀ xc p, σc.kheap t.bv = some (.tcb xc) → xc.tcbSchedContext = some p → + ∃ y n, σc.kheap p = some (.schedContext y n) ∧ y.scTcb = some t.bv) ∧ + ∀ σd, Fl4Spec.NonDet.maybeM (Fl4Spec.schedContextResume (Z := Unit)) xa.tcbSchedContext σc = + ({((), σd)}, False) → Inv σd := by + intro σb σc hb hc' + rw [hxsa] at hc' ⊢ + exact hRS0 x σa σb σc hk hnr hca hb hc' + show restartTailF st arI s1 a t ⦃ _ ⦄ + refine WP.spec_mono (J1.restartTailN hmb ht htD hka hina hnsa hRS) ?_ + rintro r ⟨hr2, hr⟩ Ac' hblk hd6 + obtain ⟨σ', he', Lst', J', F2⟩ := hr Ac' hblk (dbgKeep_cancel hd1 hd6) + refine ⟨hr2, σ', ?_, V1, Ac', Lst', D, by rw [hr2]; exact J', J.natTrans F1 F2.cast⟩ + rw [hspec0, Fl4Spec.NonDet.when, if_pos hc, nd_bind_one hca] + rw [hxsa] at he' + exact he' + refine WP.spec_mono (WP.spec_and hP1 hmain) ?_ + rintro ⟨s6, a6⟩ ⟨⟨hb6, hd6⟩, hM⟩ + exact hM Ac hb6 hd6 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBRstPost.lean b/proofs/Fl4Proofs/P3/JAtZBRstPost.lean new file mode 100644 index 000000000..36db3f6c0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBRstPost.lean @@ -0,0 +1,325 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm): **`Restart`, then `postpone`, at `JAtZB`, with the footprint**. + +`JAtZB.rstPostJV` is P3/XTopJointStsI's `JAtZ.rstPostJ` (`set_thread_state Restart` of an `Inactive` thread, then `postpone` of +its SC) over `JAtZB`, its post extended by the three facts of P3/XTopJointRunPost's `JAtZB.runPostJV`: the `TFoot` +footprint `s → s7`, the new TCB and the unchanged rest of the spec heap (what `NatFoot.ofT` needs to enter `VmEptState`). +The proof is that of `JAtZ.rstPostJ` (generated by `handoff/tools/gen_rstpost.py`). New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointStsI +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 32000000 in +/-- **`Restart`, then `postpone`, at `J`, the post also naming the footprint and the spec heap** (P3/XTopJointStsI's +`JAtZ.rstPostJ`, proof verbatim over `JAtZB`; module docstring). -/ +theorem JAtZB.rstPostJV (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) + (hnrel : ¬ (Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true ∧ + Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true)) : + sched.set_thread_state st s t sched.TS_RESTART ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := .restart })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := .restart })) t.bv) ∧ + sched.postpone st s6 sc ⦃ s7 => ∃ σ7, Fl4Spec.postpone (Z := Unit) sc.bv + (stsAct (updK σ t.bv (.tcb { x with tcbState := .restart })) t.bv) = ({((), σ7)}, False) ∧ + P1.WritesIn st s s7 (TFoot st Z.T Z.N Z.C Z.c s σ) ∧ + σ7.kheap t.bv = some (.tcb { x with tcbState := .restart }) ∧ (∀ p, p ≠ t.bv → σ7.kheap p = σ.kheap p) ∧ + ∃ Lst' : U64 → List U64, JAtZB (L := L) arI AR Z H Cs Ac Lst' D s7 a σ7 ⦄ ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rw [hst, hrs]; decide, by rw [hst, hrs]; decide⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + have hr0 : L.mem s (P1.stA t) = sched.TS_INACTIVE := by + apply UScalar.eq_of_val_eq + rw [P1.stA_eq, ← tStA_eq, hst, hrs]; unfold sched.TS_INACTIVE; rfl + -- the thread is in no queue + have hnq : NotQueued σ t.bv := by + refine ⟨fun d p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x', -, -, -, h1, -, h3, -⟩ := J.inv.rq d p t.bv hm + rw [hk] at h1; cases h1; rw [hrs] at h3; cases h3 + · obtain ⟨x', -, -, -, h1, -, h3, -⟩ := J.inv.rel t.bv hm + rw [hk] at h1; cases h1; rw [hrs] at h3; cases h3 + have hts : t.bv ≠ sc.bv := fun e => by rw [e, hky] at hk; cases hk + obtain ⟨y0, n0, hky0, hyt⟩ := J.inv.back t.bv x sc.bv hk hxsc + rw [hky] at hky0; cases hky0 + -- the action is no switch to the thread (it is not schedulable) + have hnsw : σ.schedulerAction ≠ .switchThread t.bv := fun e => by + obtain ⟨x', -, -, -, h1, -, h3, -⟩ := schedulable_inv (J.act.1 _ e) + rw [hk] at h1; cases h1; rw [hrs] at h3; cases h3 + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by rw [hrs]; decide) .restart (by decide) sched.TS_RESTART (by unfold sched.TS_RESTART; rfl)) + (set_thread_state_sfoot hS ht _)) (set_thread_state_yw hS ht _)) + (P1.sched.set_thread_state.restart_blkI (Es := P1.EH H) s t J.blk htH hr0)) ?_ + rintro s6 ⟨⟨⟨⟨hR6, hS6r⟩, hw6⟩, hy6⟩, hblk6, hfoot6⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + set x6 : Fl4Spec.Tcb := { x with tcbState := .restart } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ p, p ≠ t.bv → σ6.kheap p = σ.kheap p := by + intro p hp; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hp] + have hact6 : σ6.schedulerAction = .chooseNewThread ∨ σ6.schedulerAction = σ.schedulerAction := + stsAct_action σ1 t.bv + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_csc : σ6.curSc = σ.curSc := by rw [hrec]; rfl + have e_cons : σ6.consumedTime = σ.consumedTime := by rw [hrec]; rfl + have e_tm : σ6.curTime = σ.curTime := by rw [hrec]; rfl + have e_idle : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have e_rel : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := by + rw [hrs]; exact ⟨rfl, rfl, fun _ e => by cases e⟩ + have hK : ScTKw (fun p => p = t.bv) σ σ6 := by + refine ⟨fun p => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, rfl, rfl, fun _ e => by cases e⟩⟩) + · rw [hkp p hp]; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + have hks6 : L.ksb s6 = L.ksb s := hW.1 + have hbuf6 : BufWordRel Z.T (L.mem s6) σ6 := J.sc_buf hW hK + have hI6 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 σ6 := by + have h := hX'.irelK (by rw [hks6]; exact J.ks) hbuf6 + have e : σ6.archState = σ.archState := hK.arch + rw [e] at h; exact h + refine ⟨hX', hI6, ?_⟩ + -- the spec's `Restart` + have hsp : Fl4Spec.setThreadState (Z := Unit) t.bv .restart σ = ({((), σ6)}, False) := + Notif.setThreadState_eq .restart hk + have hIB6 : InvBut σ6 t.bv ∧ NotQueued σ6 t.bv := (J.inv.toBut t.bv).ik hnq ((ikm_setThreadState .restart).of_eq hsp) + -- the yield words + have hY6 : YieldRel Z.T Z.C (L.mem s6) σ6 := by + have h1 := J.yld.frame (m' := L.mem s6) (σ' := σ1) + (fun y hy => hy6.mem_eq (yieldA_aligned hT hy) fun h => h ⟨y, hy, rfl⟩) + (YK.YKS.updTcb (y := x6) hk rfl).ykeep + exact h1.frame (m' := L.mem s6) (σ' := σ6) (fun _ _ => rfl) (YKeep.of_kheap (stsAct_kheap _ _)) + have hYK6 : YKeep σ σ6 := (YK.YKS.updTcb (y := x6) hk rfl).ykeep.trans (YKeep.of_kheap (stsAct_kheap _ _)) + have hsok6 : SOK (false, true) σ σ6 := Pres.of_eq (pres_setThreadState _ _ _) hsp + -- the FL4 facts at `s6` for `postpone` + have hS6 : SchedRel st Z.N Z.T Z.C Z.c s6 σ6 := hX6.2.2.1 + have hR6N : NtfnStateRel Z.N Z.T (L.mem s6) σ6 := hX6.1.toN + obtain ⟨Q6, R6, hc6, hs6⟩ := hS6.lists + obtain ⟨xt6, hkt6, -, -, hsct6, -⟩ := hs6.tcb t ht + rw [hk6] at hkt6; cases hkt6 + have hxsc6 : x6.tcbSchedContext = some sc.bv := hxsc + have h0 : L.mem s6 (scA t) ≠ 0#u64 := (Notif.optRef_some (hsct6.symm.trans hxsc6)).1 + have hst6 : L.mem s6 (scA t) = sc := P1.eq_of_bv (Notif.optRef_some (hsct6.symm.trans hxsc6)).2 + have hky6 : σ6.kheap sc.bv = some (.schedContext y n) := by rw [hkp _ (Ne.symm hts)]; exact hky + obtain ⟨y', n', hky', hy'⟩ := hs6.scs sc hsc + rw [hky6] at hky'; cases hky' + have htw : L.mem s6 (scW sc 24#u64) = t := P1.eq_of_bv (Notif.optRef_some (hy'.tcb.symm.trans hyt)).2 + have hnr6 : t.bv ∉ σ6.releaseQueue := hIB6.2.2 + have hr06 : (L.mem s6 (inqA t) &&& sched.Q_RELEASE) = 0#u64 := by + by_contra hne + have hm := (hc6.released t ht).1 hne + exact hnr6 (by rw [hs6.rel, mem_map_bv]; exact hm) + have hrw6 : L.mem s6 (tStA t) = sched.TS_RESTART := by + obtain ⟨x7, hk7, hTR7⟩ := hI6.1.tcb t ht + rw [hk6] at hk7; cases hk7 + apply UScalar.eq_of_val_eq; rw [hTR7.st]; unfold sched.TS_RESTART; rfl + have hrun6 : P1.Runnable (L.mem s6 (P1.stA t)) := by + rw [stA_tStA, hrw6]; exact P1.runnable_restart + have hns6 : σ6.schedulerAction ≠ .switchThread t.bv := by + rcases hact6 with e | e <;> rw [e] + · intro h; cases h + · exact hnsw + have hna6 : L.actionW s6 ≠ t := actionW_ne_of hS6 ht hns6 + have hmx : (L.rmax s6 (L.mem s6 (scA t))).val ≠ 0 := by rw [hst6]; exact hy'.rmax_ne hya + have htw' : L.mem s6 (P1.scTcbA sc) = t := by rw [Sched.scTcbA_eq]; exact htw + have hRel6 : RelOK σ6 := by + refine ⟨fun u hu => ?_, hIB6.1.sorted, fun u hu xu p hku hsu => ?_⟩ + · obtain ⟨xu, p, -, -, h1, h2, -⟩ := hIB6.1.rel u hu; exact ⟨xu, p, h1, h2⟩ + · obtain ⟨xu', p', y1, n1, h1, h2, -, h4, h5⟩ := hIB6.1.rel u hu + rw [hku] at h1; cases h1; rw [hsu] at h2; cases h2; exact ⟨y1, n1, h4, h5⟩ + refine WP.spec_mono (WP.spec_and (postpone_refines s6 σ6 hS6 hRel6 hsc ht htw h0 hr06 hmx) + (P1.sched.postpone.blk (Es := P1.EH H) s6 sc hblk6 (by rw [htw']; exact htH) (by rw [htw', hst6]) + (J.c0 sc hsc) (by rw [htw']; exact hr06) (by rw [htw']; exact hrun6) + (by rw [htw']; exact hna6) (fun e => hmx (by rw [hst6, e]; rfl)))) ?_ + rintro s7 ⟨⟨σ7, he, hke, hae, hte, hrq7, hfe, hw7, hS7⟩, hblk, hfoot7⟩ + refine ⟨σ7, he, ?_⟩ + have mem7 : ((), σ7) ∈ (Fl4Spec.postpone (Z := Unit) sc.bv σ6).1 := by rw [he]; exact Set.mem_singleton _ + -- the spec state after `postpone` + have hne6 : ∀ p y1 n1, σ6.kheap p = some (.schedContext y1 n1) → Fl4Spec.scActive y1 = true → + y1.scRefills ≠ [] := hIB6.1.ne + obtain ⟨b, hpe⟩ := postpone_eq hky6 hyt hk6 (specRt σ6) (fun u hu => by + rcases hu with rfl | hu + · exact ⟨x6, sc.bv, y, n, hk6, hxsc6, hky6, hne6 _ _ _ hky6 hya, specRt_eq hk6 hxsc6 hky6 (hne6 _ _ _ hky6 hya)⟩ + · obtain ⟨xu, scp, yu, nu, h1, h2, -, h4, h5⟩ := hIB6.1.rel u hu + exact ⟨xu, scp, yu, nu, h1, h2, h4, hne6 _ _ _ h4 h5, specRt_eq h1 h2 h4 (hne6 _ _ _ h4 h5)⟩) + have hσ7 := congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (he.symm.trans hpe))) + simp only at hσ7 + have hI7 : Inv σ7 := by + rw [hσ7]; exact hIB6.1.postponed hk6 hxsc6 rfl hky6 hya b + have e7_ct : σ7.curThread = σ.curThread := by rw [hσ7]; exact e_ct + have e7_csc : σ7.curSc = σ.curSc := by rw [hσ7]; exact e_csc + have e7_cons : σ7.consumedTime = σ.consumedTime := by rw [hσ7]; exact e_cons + have e7_tm : σ7.curTime = σ.curTime := hte.trans e_tm + have e7_idle : σ7.idleThread = σ.idleThread := by rw [hσ7]; exact e_idle + have e7_isc : σ7.idleSc = σ.idleSc := by rw [hσ7]; exact e_isc + have hk7 : σ7.kheap t.bv = some (.tcb x6) := by rw [hke]; exact hk6 + have hkp7 : ∀ p, p ≠ t.bv → σ7.kheap p = σ.kheap p := fun p hp => by rw [hke]; exact hkp p hp + have hrel7 : ∀ u, u ∈ σ7.releaseQueue ↔ u = t.bv ∨ u ∈ σ.releaseQueue := fun u => by + rw [hrq7 u, e_rel] + have hact7 : σ7.schedulerAction = .chooseNewThread ∨ σ7.schedulerAction = σ.schedulerAction := by + rw [hae]; exact hact6 + -- the frame + have hW7 : P1.WritesIn st s s7 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot hW hw7 + have hK7 : ScTKw (fun p => p = t.bv) σ σ7 := hK.trans (nkt_postpone _ σ6 _ mem7).ofFalse + have hR7 : NtfnStateRel Z.N Z.T (L.mem s7) σ7 := hR6N.frame hS6.placed hfe (KView.of_kheap hke) + have hX7 : XTopAt (L := L) AR Z s7 a σ7 := xtop_sc_step J.xtop hP0 J.nk hW7 hK7 hS7 hR7 id + have hNSC : NSC Z.N Z.C σ7 := by + intro m hm y1 p hy hp + by_cases e : m.bv = t.bv + · rw [e, hk7] at hy; cases hy + · rw [hkp7 _ e] at hy; exact J.nsc m hm y1 p hy hp + have hw7K : P1.WritesIn st s6 s7 (KFoot st Z.T Z.N Z.C Z.c s6 σ6) := WritesIn.kfoot_of_sfoot hw7 + have hY7 : YieldRel Z.T Z.C (L.mem s7) σ7 := + hY6.frame (fun y1 hy1 => hw7K.mem_eq (yieldA_aligned hT hy1) fun hb => hb.ne_yield hS6 hy1 rfl) + (YKeep.of_kheap hke) + have hsok7 : SOK (false, true) σ σ7 := hsok6.trans (pres_postpone (f := (false, true)) _ σ6 _ mem7) + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hS7, hR7, hNSC, hI7⟩ hY7 (hW7.mono fun _ h => Or.inl h) + hsok7 rfl + -- the scheduler facts + have hidt : σ.idleThread ≠ t.bv := by + intro e + obtain ⟨y1, h1, -, h3, -⟩ := J.act.2 + rw [e, hk] at h1; cases h1; rw [hrs] at h3; cases h3 + have hsched : ∀ u, u ≠ t.bv → Fl4Spec.schedulable u σ = true → Fl4Spec.schedulable u σ7 = true := by + intro u hut hs + obtain ⟨y1, scp, z, n1, hky1, hsc1, hrun, hkz, hac, hnr⟩ := schedulable_inv hs + have hzt : scp ≠ t.bv := by rintro rfl; rw [hk] at hkz; cases hkz + have hkz7 : σ7.kheap scp = some (.schedContext z n1) := by rw [hkp7 _ hzt]; exact hkz + have hnr7 : u ∉ σ7.releaseQueue := fun hm => (hrel7 u).1 hm |>.elim hut hnr + exact schedulable_of (by rw [hkp7 _ hut]; exact hky1) hsc1 hrun hkz7 hac hnr7 + have hAI : ActIdle σ7 := by + refine ⟨fun u hu => ?_, ?_⟩ + · have hu0 : σ.schedulerAction = .switchThread u := by + rcases hact7 with e | e + · rw [e] at hu; cases hu + · rw [← e]; exact hu + have hut : u ≠ t.bv := fun e => hnsw (e ▸ hu0) + exact hsched u hut (J.act.1 u hu0) + · obtain ⟨y1, h1, h2, h3, h4⟩ := J.act.2 + exact ⟨y1, by rw [e7_idle, hkp7 _ hidt]; exact h1, by rw [e7_isc]; exact h2, h3, by + rw [e7_idle]; intro hm; rcases (hrel7 _).1 hm with e | e; exact hidt e; exact h4 e⟩ + have hoff : OffOK σ7 := by + intro h0' y1 n1 hy ha + rw [e7_cons] at h0' ⊢; rw [e7_tm]; rw [e7_csc] at hy; rw [e7_csc, e7_isc] at ha + by_cases e : σ.curSc = t.bv + · rw [e, hk7] at hy; cases hy + · rw [hkp7 _ e] at hy; exact J.off h0' y1 n1 hy ha + have hcur7 : σ7.schedulerAction = .chooseNewThread ∨ CurInv σ7 := by + rcases J.cur with e | hC + · left + rcases hact7 with e' | e' + · exact e' + · rw [e', e] + · right + obtain ⟨xt, hkc, hcs, hnb⟩ := hC.tcb + obtain ⟨y1, n1, hk1'⟩ := hC.sc + have hne : σ.curSc ≠ t.bv := by intro e; rw [e, hk] at hk1'; cases hk1' + refine ⟨?_, ⟨y1, n1, by rw [e7_csc, hkp7 _ hne]; exact hk1'⟩, fun hne' y2 n2 hy => ?_, + fun hm => ?_⟩ + · exact ⟨xt, by rw [e7_ct, hkp7 _ (Ne.symm htc)]; exact hkc, by rw [e7_csc]; exact hcs, hnb⟩ + · rw [e7_csc, e7_isc] at hne'; rw [e7_csc] at hy + rw [hkp7 _ hne] at hy + exact hC.active hne' y2 n2 hy + · rw [e7_ct] at hm + rcases (hrel7 _).1 hm with e | e + · exact htc e.symm + · exact hC.notRel e + have hown7 : CurOwnW Z.T Z.C σ7 := by + have hsc7 : ∀ y1 n1, σ7.kheap σ7.curSc = some (.schedContext y1 n1) → + σ.kheap σ.curSc = some (.schedContext y1 n1) := by + intro y1 n1 hy + rw [e7_csc] at hy + by_cases e : σ.curSc = t.bv + · rw [e, hk7] at hy; cases hy + · rw [hkp7 _ e] at hy; exact hy + rcases J.own with ⟨u, hu, xt, hkt, hxs, hown, hnr⟩ | ⟨hcs, hun⟩ + · left + have hnr' : 0 < σ7.consumedTime → u.bv ∉ σ7.releaseQueue := by + intro h0' hm + rw [e7_cons] at h0' + rcases (hrel7 _).1 hm with e | e + · -- the postponed thread owns the current SC: `OffOK` makes its SC released + rw [e, hk] at hkt; cases hkt + have ecs : σ.curSc = sc.bv := Option.some.inj (hxs.symm.trans hxsc) + obtain ⟨h1, -, h3⟩ := J.off h0' y n (by rw [ecs]; exact hky) (fun _ => hya) + exact hnrel ⟨h3, restart_suff0 h1⟩ + · exact hnr h0' e + by_cases hut : u.bv = t.bv + · rw [hut, hk] at hkt; cases hkt + exact ⟨u, hu, x6, by rw [hut]; exact hk7, by rw [e7_csc]; exact hxs, + fun y1 n1 hy => hown y1 n1 (hsc7 y1 n1 hy), hnr'⟩ + · exact ⟨u, hu, xt, by rw [hkp7 _ hut]; exact hkt, by rw [e7_csc]; exact hxs, + fun y1 n1 hy => hown y1 n1 (hsc7 y1 n1 hy), hnr'⟩ + · exact Or.inr ⟨by rw [e7_csc]; exact hcs, fun y1 n1 hy => hun y1 n1 (hsc7 y1 n1 hy)⟩ + have hcr7 : CurRunM σ7 := by + by_cases hnr : σ7.schedulerAction ≠ .resumeCurThread + · exact Or.inl hnr + have hr' : σ7.schedulerAction = .resumeCurThread := not_not.1 hnr + have ha : σ7.schedulerAction = σ.schedulerAction := hact7.resolve_left (by rw [hr']; intro e; cases e) + have h0' : ¬ σ.schedulerAction ≠ .resumeCurThread := fun e => e (by rw [← ha]; exact hr') + right + rcases J.curRun.resolve_left h0' with h | h + · left + intro y1 hy + rw [e7_ct] at hy + rw [hkp7 _ (Ne.symm htc)] at hy; exact h y1 hy + · right; rw [e7_ct, e7_idle]; exact h + have hlk : LK (fun _ => False) σ σ7 := fun p => Or.inr (by + by_cases hp : p = t.bv + · subst hp; rw [hk, hk7] + show LObj _ _ + refine ⟨?_, ?_, ?_⟩ + · intro y1 ey; cases ey; exact ⟨_, rfl, Or.inl rfl⟩ + · intro y1 n1 ey; cases ey + · intro y1 ey; cases ey + · rw [hkp7 p hp]; exact OL.refl _) + have hfk : FK σ σ7 := ⟨e7_csc, e7_idle, e7_isc⟩ + have hDbg := J.kw_dbg (P1.Foot.trans hfoot6 hfoot7) + have hKd := J.kw_dbgKeep (P1.Foot.trans hfoot6 hfoot7) + have hdead : ∀ q ∈ D, DeadP (L := L) H s7 q := fun q hq => + J.deadP_sc_stepQ hX7 hW7 hK7 (fun _ hp => absurd (show q = t from P1.eq_of_bv hp) (fun e => htD (e ▸ hq))) + hlk hfk hAI.1 hY' ((hYK6.trans (YKeep.of_kheap hke)).dk q.bv) (fun h => h) (J.jcov.dead q hq) + exact ⟨hW7, hk7, hkp7, Lst', J.sc_frame hW7 hK7 hX7 hCR' hRF' hY' hM' (J.sc_buf hW7 hK7) hRP' (J.sc_apart hW7 hK7) + (J.sc_jpl hW7 hK7 (J.idleW_keep hX7.fin.1.1.1 hfk.2.1 hfk.2.2).2) (J.sc_jcov hW7 hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX7.fin.1.1.1 hfk.2.1 hfk.2.2) hKd subset_rfl) + (J.sc_kern hW7 hK7 hfk hX7 hDbg fun _ h => h) + (J.sc_utc hW7 hK7 hDbg) hNSC hI7 hcur7 hown7 hoff hAI (BlockedScActive.tkw J.bsa hK7 fun p y1 n1 hp ha => by + by_cases e : p = t.bv + · rw [e, hk7] at hp; cases hp + · rw [hkp7 _ e] at hp; exact ⟨y1, hp, ha, fun _ h => h, id⟩) hcr7 hblk hKd (J.idleW_keep hX7.fin.1.1.1 hfk.2.1 hfk.2.2).1 J.zD⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBRunTmo.lean b/proofs/Fl4Proofs/P3/JAtZBRunTmo.lean new file mode 100644 index 000000000..ed30f21f1 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBRunTmo.lean @@ -0,0 +1,476 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm, Timeout branch): **`Running` or `Restart` of an `Inactive` thread, then `handle_timeout` +blocking it on its timeout handler's endpoint, at `JAtZB` with the native footprint**. + +`JAtZB.runTmoFoot` is P3/XTopJointSendBlockG's `JAtZ.runTmoJ` over `JAtZB` (the proof verbatim; `JCs.sameB` for `JCs.same`), its +post extended by `NatFoot Z s σ res.1 z.2` (the footprint from the state before `set_thread_state`: the blocking send's `CnW` +words, its spec frame `natK_bfrt`) so that `VmEptState.ofNat` consumes it. Generated by `handoff/tools/gen_tmo.py`. +New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointSendBlockG +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.XTopJointStep +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- **`Running` or `Restart` of an `Inactive` thread other than the current +one, then `handle_timeout` blocking it on its timeout handler's endpoint, at +`J`** (module docstring). -/ +theorem JAtZB.runTmoFoot (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) + {p bd : Fl4Spec.Word} {r : Fl4Spec.CapRights} (hth : x.tcbTimeoutHandler = .endpointCap p bd r) + (hcb : ∀ xe, σ.kheap p = some (.endpoint xe) → Endp.CanBlock false xe) + (ts : Fl4Spec.ThreadState) (hrun : Fl4Spec.runnable ts = true) (hts : tsCode ts < 3) (v : U64) + (hv : v.val = tsCode ts) + (hB : sched.set_thread_state st s t v ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t v ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + ∀ b : U64, faults.handle_timeout st arI s6 a t (error.Fault.Timeout b) ⦃ res => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv)).1, + ∃ (V : XVs) (D' : Set U64), JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D' res.1 res.2 z.2 ∧ + NatFoot (L := L) Z s σ res.1 z.2 ⦄ ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have hta := hP0.e.tcb t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hP0.e.tcb + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rw [hst, hrs]; decide, by rw [hst, hrs]; decide⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + have hnq : NotQueued σ t.bv := J.inv.notQueued hk (by rw [hrs]; rfl) + obtain ⟨y0, n0, hky0, hyt⟩ := J.inv.back t.bv x sc.bv hk hxsc + rw [hky] at hky0; cases hky0 + have hidt : σ.idleThread ≠ t.bv := by + intro e + obtain ⟨y1, h1, -, h3, -⟩ := J.act.2 + rw [e, hk] at h1; cases h1; rw [hrs] at h3; cases h3 + -- the handler cap at the start, its endpoint + obtain ⟨⟨G0, hB0⟩, -⟩ := hI.2.1 + have hth0 : Z.th t = some .tcb := hB0.tcbT hk + obtain ⟨xh, cv, hkh, hcv, hca⟩ := hB0.2.1.1.timeoutCap hth0 + rw [hk] at hkh; cases hkh + have habs := hca.2 + rw [hth] at habs + obtain ⟨ptr, bdg, sd, rv, g, gr, rfl⟩ : ∃ ptr bdg sd rv g gr, cv = .Endpoint ptr bdg sd rv g gr := by + cases cv <;> simp [absCap] at habs; exact ⟨_, _, _, _, _, _, rfl⟩ + simp only [absCap, Fl4Spec.Cap.endpointCap.injEq] at habs + obtain ⟨hp, hbd, hr⟩ := habs + subst hp hbd hr + have hAS : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex 4) := ⟨t, rfl, Or.inr ⟨hth0, 4, by decide, rfl⟩⟩ + have hcv' : capAt (L.mem s) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 4)) = ok (.Endpoint ptr bdg sd rv g gr) := by + rw [tcbSlot4_eq]; exact hcv + have hpE : ptr ∈ Z.E := J.slotE hAS hcv' + obtain ⟨xe, hke, -⟩ := hI.1.ep ptr hpE + have hcbe : Endp.CanBlock false xe := hcb xe hke + have hpt : ptr.bv ≠ t.bv := fun e => by rw [e, hk] at hke; cases hke + have hps : ptr.bv ≠ sc.bv := fun e => by rw [e, hky] at hke; cases hke + -- the endpoint covered by the handler cap + have hHe : H ptr = some .ep := J.zH.2.1 ptr hpE + have hfit := J.kern.heap.fits ptr _ hHe + simp only [P1.HKind.bits] at hfit + have hcov := J.cover_cap (P1.WritesIn.refl s _) hAS.isSlot hcv' (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_ep ptr bdg sd rv g gr hfit) fun k hk' => by + rw [hHe] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + -- `set_thread_state` + refine WP.spec_mono (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by rw [hrs]; decide) ts hts v hv) (set_thread_state_sfoot hS ht _)) hB) ?_ + rintro s6 ⟨⟨⟨hR6, hS6r⟩, hw6⟩, hblk6, hfoot6⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + set x6 : Fl4Spec.Tcb := { x with tcbState := ts } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ q, q ≠ t.bv → σ6.kheap q = σ.kheap q := by + intro q hq'; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hq'] + have e61 : σ6 = σ1 := by + rw [hσ6]; unfold Notif.stsAct; rw [if_neg]; rintro ⟨h, -⟩; exact htc h + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := by + rw [hrs]; exact ⟨rfl, rfl, fun _ e => by cases e⟩ + have hK : ScTKw (fun q => q = t.bv) σ σ6 := by + refine ⟨fun q => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hq' : q = t.bv + · subst hq'; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, tsLow_free hts⟩⟩) + · rw [hkp q hq']; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + have hks6 : L.ksb s6 = L.ksb s := hW.1 + have hbuf6 : BufWordRel Z.T (L.mem s6) σ6 := J.sc_buf hW hK + have hI6 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 σ6 := by + have h := hX'.irelK (by rw [hks6]; exact J.ks) hbuf6 + have e : σ6.archState = σ.archState := hK.arch + rw [e] at h; exact h + refine ⟨hX', hI6, fun b => ?_⟩ + -- the FL4 step from the start to `s6` (`CnStep`) + have hC1 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ s6 σ6 := + ⟨hw6.mono fun b hb => Or.inl (Or.inl (KFoot.of_sfoot hb)), SzKeepC.of (SzKeep.of_tcb (p := t.bv) (x' := x6) + hkp hk6)⟩ + -- `handle_timeout`: the handler cap at `s6` + obtain ⟨⟨G6, hB6⟩, -⟩ := hI6.2.1 + obtain ⟨xh6, cv6, hkh6, hcv6, hca6⟩ := hB6.2.1.1.timeoutCap (hB6.tcbT hk6) + rw [hk6] at hkh6; cases hkh6 + have habs6 := hca6.2 + rw [show x6.tcbTimeoutHandler = x.tcbTimeoutHandler from rfl, hth] at habs6 + unfold faults.handle_timeout + rw [tcb_slot_timeout] + apply ok_bind_spec + rw [slot_cap_eq, hcv6] + apply ok_bind_spec + cases cv6 + case Endpoint ptr' bdg' sd' rv' g' gr' => + simp only [absCap, Fl4Spec.Cap.endpointCap.injEq] at habs6 + obtain ⟨hp', hbd', hr'⟩ := habs6 + obtain rfl : ptr = ptr' := (P1.eq_of_bv hp').symm + obtain rfl : bdg = bdg' := (P1.eq_of_bv hbd').symm + have hg : g' = g := by have := congrArg (fun R => decide (Fl4Spec.Rights.allowGrant ∈ R)) hr'; simpa [rights_grant] using this + have hgr : gr' = gr := by + have := congrArg (fun R => decide (Fl4Spec.Rights.allowGrantReply ∈ R)) hr'; simpa [rights_grantReply] using this + obtain rfl : g = g' := hg.symm + obtain rfl : gr = gr' := hgr.symm + refine WP.spec_bind (Pₘ := fun r : Bool × S × A => ∃ z ∈ (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) σ6).1, + ∃ (V : XVs) (D' : Set U64), JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D' r.2.1 r.2.2 z.2 ∧ + NatFoot (L := L) Z s σ r.2.1 z.2) ?_ ?_ + swap + · rintro ⟨b0, s2, a2⟩ hr0 + exact (WP.spec_ok _).2 hr0 + unfold faults.send_fault_ipc + dsimp only + -- `set_fault` + set x2 : Fl4Spec.Tcb := { x6 with tcbFault := some (.timeout b.bv) } with hx2 + set σf := updK σ6 t.bv (.tcb x2) with hσf + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_fault_timeout_top hP0 hI6 ht hk6 b) + (set_fault_timeout_eq s6 t b)) (WP.spec_and (P1.faults.set_fault.blk (U := ∅) s6 t (error.Fault.Timeout b) + (t0 := t) hblk6 htH (fun _ h => by simp at h) ?hcb6) + (P1.faults.set_fault.kw (Cs := Cs) (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s6 t (error.Fault.Timeout b) hTH htH))) ?_ + case hcb6 => + have hS6 : SchedRel st Z.N Z.T Z.C Z.c s6 σ6 := hX6.2.2.1 + obtain ⟨Q, R0, hcq, hs⟩ := hS6.lists + obtain ⟨x7, hk7, hTR7⟩ := hI6.1.tcb t ht + rw [hk6] at hk7; cases hk7 + have hv7 : (L.mem s6 (tStA t)).val = tsCode ts := hTR7.st + have hrr : L.mem s6 (P1.stA t) = sched.TS_RUNNING ∨ L.mem s6 (P1.stA t) = sched.TS_RESTART := by + have hrr' : ts = .running ∨ ts = .restart := by + cases ts with + | running => exact Or.inl rfl + | restart => exact Or.inr rfl + | _ => simp [tsCode, Fl4Spec.runnable] at hts hrun + rcases hrr' with e | e + · left; apply UScalar.eq_of_val_eq; rw [P1.stA_eq, ← tStA_eq, hv7, e]; unfold sched.TS_RUNNING; rfl + · right; apply UScalar.eq_of_val_eq; rw [P1.stA_eq, ← tStA_eq, hv7, e]; unfold sched.TS_RESTART; rfl + have hrq6 : σ6.readyQueues = σ.readyQueues := by rw [hrec]; rfl + have hrl6 : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hac6 : σ6.schedulerAction = σ.schedulerAction := by rw [e61]; rfl + have hid6 : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + obtain ⟨xt6, hkt6, -, -, hsct6, -⟩ := hs.tcb t ht + rw [hk6] at hkt6; cases hkt6 + have hst6 : L.mem s6 (scA t) = sc := P1.eq_of_bv (Notif.optRef_some (hsct6.symm.trans hxsc)).2 + have h06 : L.mem s6 (scA t) ≠ 0#u64 := (Notif.optRef_some (hsct6.symm.trans hxsc)).1 + have hky6 : σ6.kheap sc.bv = some (.schedContext y n) := by + rw [hkp _ (fun e => by rw [e, hk] at hky; cases hky)]; exact hky + obtain ⟨y', n', hky', hy'⟩ := hs.scs sc hsc + rw [hky6] at hky'; cases hky' + have hmx : (L.rmax s6 (L.mem s6 (scA t))).val ≠ 0 := by rw [hst6]; exact hy'.rmax_ne hya + refine ⟨htH, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rcases hrr with h | h <;> rw [h] + · exact P1.not_blocked_running + · exact P1.not_blocked_restart + · rcases hrr with h | h <;> rw [h] + · exact P1.running_ne.1 + · exact P1.restart_ne.1 + · by_contra hq' + have hm := (hcq.queued t ht).1 hq' + exact hnq.1 0 _ (by rw [← hrq6, hs.rq _ (hcq.prio t ht), mem_map_bv]; exact hm) + · by_contra hq' + have hm := (hcq.released t ht).1 hq' + exact hnq.2 (by rw [← hrl6, hs.rel, mem_map_bv]; exact hm) + · refine actionW_ne_of hS6 ht ?_ + rw [hac6]; intro e + obtain ⟨x', -, -, -, h1, -, h3, -⟩ := schedulable_inv (J.act.1 _ e) + rw [hk] at h1; cases h1; rw [hrs] at h3; cases h3 + · intro h + have ei : L.idleW s6 = nodeW st s6 Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW nodeW; rw [hcq.cur] + exact hidt (by rw [← hid6, hs.idleTh, ← ei, h]) + · intro hbd + obtain ⟨⟨Q', R', h0⟩, hBd, hXi, hSc, hSg⟩ := hblk6 + obtain ⟨hiS, -, -⟩ := hXi.2.2.bound _ htH hbd + exact (hSc.act _ hiS).2 (fun e => hmx (by rw [e]; rfl)) + rintro s7 ⟨⟨⟨hsp7, hI7⟩, hs7⟩, ⟨hblk7, -, hcb7⟩, hfoot7⟩ + have hkf : σf.kheap t.bv = some (.tcb x2) := by simp [hσf, updK] + have hkfp : ∀ q, q ≠ t.bv → σf.kheap q = σ.kheap q := by + intro q hq'; rw [hσf]; simp only [updK, Function.update_of_ne hq']; exact hkp q hq' + have hkef : σf.kheap ptr.bv = some (.endpoint xe) := by rw [hkfp _ hpt]; exact hke + -- the FL4 step of `set_fault` + have hC2 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s6 σ6 s7 σf := by + refine ⟨?_, SzKeepC.of (SzKeep.of_tcb (p := t.bv) (x' := x2) (fun q hq' => by simp [hσf, updK, hq']) hkf)⟩ + subst hs7 + have w := (((writesIn_wr (st := st) (s := s6) (v := fl4_abi.consts.fault.Timeout) (a_fault hta).1).trans + (writesIn_wr (v := b) (a_fw0 hta).1)).trans (writesIn_wr (v := 0#u64) (a_fw1 hta).1)).trans + (writesIn_wr (v := 0#u64) (a_fw2 hta).1) + refine w.mono fun w0 hw0 => ?_ + have k : ∃ K : Nat, 256 ≤ K ∧ K < 4096 ∧ K ≠ 3440 ∧ K ≠ 3472 ∧ ((K ≠ 3512 ∧ K ≠ 3528) ∧ K ≠ 3664 ∧ K ≠ 3456) ∧ w0.val = t.val + K := by + rcases hw0 with ((h | h) | h) | h <;> subst h + · exact ⟨3376, by omega, by omega, by omega, by omega, by omega, val_fault hta⟩ + · exact ⟨3384, by omega, by omega, by omega, by omega, by omega, val_fw0 hta⟩ + · exact ⟨3392, by omega, by omega, by omega, by omega, by omega, val_fw1 hta⟩ + · exact ⟨3400, by omega, by omega, by omega, by omega, by omega, val_fw2 hta⟩ + obtain ⟨K, k1, k2, k3, k4, k5, k6⟩ := k + exact Or.inl (Or.inr ⟨t, ht, K, k1, k2, k3, k4, k5, k6⟩) + -- the spec's blocking send at `σf` + have hprf : ∀ u, Sched.prioOf σf u = Sched.prioOf σ u := by + intro u + by_cases hut : u = t.bv + · subst hut; exact (prioOf_eq hkf).trans (prioOf_eq hk).symm + · unfold Sched.prioOf; rw [tcbsOf_eq, tcbsOf_eq, hkfp u hut] + have htq : t.bv ∉ xe.epQueue := + Endp.not_queued hI.1.toE ht hk ⟨by rw [hrs]; decide, by rw [hrs]; decide⟩ ptr hpE xe hke + have hqm : ∀ u ∈ xe.epQueue, ∃ z, σf.kheap u = some (.tcb z) := fun u hu => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hke).2 u hu + exact ⟨z, by rw [hkfp u (fun e => htq (e ▸ hu))]; exact hz⟩ + obtain ⟨σ', heq, F0, hik0, ⟨ds, hkt'⟩, -, hact', -, hsrt⟩ := + sendIpcBlock_eq hkf hkef hcbe htq hqm hrun ((J.isorted.1 _ _ hke).congr fun u _ => hprf u) + false bdg.bv g gr false + -- FL4's blocking send + have hIS : IpcSorted σf := by + have h1 : IpcSorted σ1 := QPK.QPS.sorted (QPK.qps_upd (ko := .tcb x6) hk rfl) J.isorted + have h2 : IpcSorted σ6 := by rw [e61]; exact h1 + exact QPK.QPS.sorted (QPK.qps_upd (ko := .tcb x2) hk6 rfl) h2 + obtain ⟨⟨Q0, R0, h0⟩, hB0', hX0, hS0, hSg0⟩ := hblk7 + have hst0 : L.epState s7 ptr = 0#u64 ∨ L.epState s7 ptr = 1#u64 := by + have hx := hI7.1.toE.at_ep hpE hkef + rw [epState_eq] + cases xe with + | idleEP => exact Or.inl hx.1 + | sendEP q => exact Or.inr hx.1 + | recvEP q => simp [Endp.CanBlock] at hcbe + have hts3 : tsCode ts < 3 := hts + have hnb : tsCode x2.tcbState ≠ 3 ∧ tsCode x2.tcbState ≠ 4 := + ⟨fun h => by have h' : tsCode ts = 3 := h; omega, fun h => by have h' : tsCode ts = 4 := h; omega⟩ + have hnn : ∀ m, x2.tcbState ≠ .blockedOnNotification m := fun m h => by + have h' : tsCode ts = 6 := congrArg tsCode h + omega + have p1f : endpoint.send_ipc st arI s7 a true false bdg g gr false t ptr ⦃ s' _ => P1.Foot st (P1.Heap.tcbs H) Cs + (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s7 s' ⦄ := + P1.endpoint.send_ipc.blockFoot arI s7 a true false bdg g gr false t ptr h0 (fun _ => hcb7) hHe hst0 + refine WP.spec_bind (WP.spec_and (WP.spec_and (send_ipc_blockF (ar := arI) hP0 s7 a σf hI7 ht hkf hnb hnn hpE hkef + hcbe (Sched.qSorted_of_spec (L := L) hI7.1.toE hI7.1.toN hT hIS ptr (Or.inl hpE)) false bdg g gr false) + (P1.endpoint.send_ipc.block arI s7 a true false bdg g gr false t ptr Q0 R0 h0 hB0' hX0 hS0 hSg0 + (fun _ => hcb7) hHe hst0)) + p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW3⟩, hbk⟩, hfoot3⟩ + simp only [WP.uncurry'_pair] at ea hbk hfoot3 hIR hW3 + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + apply (WP.spec_ok _).2 + -- the spec's outcome + have hth6 : x6.tcbTimeoutHandler = .endpointCap ptr.bv bdg.bv (rightsOf sd rv g gr) := hth + have hz : ((), σ') ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv + (decide (Fl4Spec.Rights.allowGrant ∈ rightsOf sd rv g gr)) + (decide (Fl4Spec.Rights.allowGrantReply ∈ rightsOf sd rv g gr)) false t.bv ptr.bv + (updK σ6 t.bv (.tcb { x6 with tcbFault := some (.timeout b.bv) }))).1 := by + rw [rights_grant, rights_grantReply] + show ((), σ') ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr false t.bv ptr.bv σf).1 + rw [heq]; exact Set.mem_singleton _ + refine ⟨((), σ'), handleTimeout_mem hk6 hth6 _ hz, ?_⟩ + -- the frame from the start + have hkt3 : σ'.kheap t.bv = + some (.tcb { x with tcbState := .blockedOnSend ptr.bv ds, tcbFault := some (.timeout b.bv) }) := hkt' + have hsk : Sk σ σ' := by + have h1 : Sk σ σ6 := ⟨e_ct, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_isc⟩ + have h2 : Sk σ6 σf := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + exact (h1.trans h2).trans F0.sk + have F : BFrT t.bv ptr.bv σ σ' := by + refine ⟨hsk, fun q => ?_⟩ + by_cases hqt : q = t.bv + · subst hqt + exact Or.inr ⟨_, _, hk, hkt3, Or.inl ⟨x, ptr.bv, ds, some (.timeout b.bv), rfl, rfl, rfl⟩⟩ + · rcases F0.kh q with e | ⟨o, o', h1, h2, hc⟩ + · exact Or.inl (e.trans (hkfp q hqt)) + · rw [hkfp q hqt] at h1 + rcases hc with ⟨_, _, _, _, _, e3, -⟩ | ⟨x1, y1, rfl, rfl, e3⟩ + · exact absurd e3 hqt + · exact Or.inr ⟨_, _, h1, h2, Or.inr ⟨x1, y1, rfl, rfl, e3⟩⟩ + -- the FL4 footprint from the start + have hC3 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf s' σ' := ⟨hW3, SzKeepC.of F0.szKeep⟩ + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := ((hC1.trans hC2).trans hC3).1 + have hfoot := P1.Foot.trans (P1.Foot.trans hfoot6 hfoot7) hfoot3 + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + -- the scheduler action: kept, or a reschedule + have hact3 : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread := by + have e6 : σf.schedulerAction = σ.schedulerAction := by + show σ6.schedulerAction = σ.schedulerAction; rw [e61]; rfl + rcases hact' with e | e + · exact Or.inl (e.trans e6) + · exact Or.inr e + have htc' : σ.curThread ≠ t.bv := fun e => htc e.symm + have hsched : ∀ u, Fl4Spec.schedulable u σ = true → Fl4Spec.schedulable u σ' = true := by + intro u hs + obtain ⟨y1, scp, z, n1, hky1, hsc1, hrun1, hkz, hac, hnr⟩ := schedulable_inv hs + have hut : u ≠ t.bv := by rintro rfl; rw [hk] at hky1; cases hky1; rw [hrs] at hrun1; cases hrun1 + exact schedulable_of (F.tcbO hut hky1) hsc1 hrun1 (F.scF hkz) hac (by rw [F.sk.rel]; exact hnr) + have hAI3 : ActIdle σ' := by + refine ⟨fun u hu => ?_, F.idleOK J.act.2 hidt⟩ + have hu0 : σ.schedulerAction = .switchThread u := by + rcases hact3 with e | e + · rw [← e]; exact hu + · rw [e] at hu; cases hu + exact hsched u (J.act.1 u hu0) + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases J.cur with e | hC + · left + rcases hact3 with e' | e' + · rw [e', e] + · exact e' + · rcases hact3 with e' | e' + · right + obtain ⟨xt, hkc, hcs, hnb'⟩ := hC.tcb + obtain ⟨y1, n1, hk1'⟩ := hC.sc + refine ⟨⟨xt, by rw [F.sk.ct]; exact F.tcbO htc' hkc, by rw [F.sk.csc]; exact hcs, hnb'⟩, + ⟨y1, n1, by rw [F.sk.csc]; exact F.scF hk1'⟩, fun hne' y2 n2 hy => ?_, fun hm => ?_⟩ + · rw [F.sk.csc, F.sk.isc] at hne'; rw [F.sk.csc] at hy + exact hC.active hne' y2 n2 (F.scB hy) + · rw [F.sk.ct, F.sk.rel] at hm; exact hC.notRel hm + · exact Or.inl e' + have hrun3 : CurRunM σ' := by + by_cases hnr : σ'.schedulerAction ≠ .resumeCurThread + · exact Or.inl hnr + have hr' : σ'.schedulerAction = .resumeCurThread := not_not.1 hnr + have ha : σ'.schedulerAction = σ.schedulerAction := hact3.resolve_right (by rw [hr']; intro e; cases e) + have h0' : ¬ σ.schedulerAction ≠ .resumeCurThread := fun e => e (by rw [← ha]; exact hr') + right + rcases J.curRun.resolve_left h0' with h | h + · left + intro y1 hy + rw [F.sk.ct] at hy + exact h y1 (F.tcbOB htc' hy) + · right; rw [F.sk.ct, F.sk.idle]; exact h + -- the dead: the thread is not dead; the endpoint leaves the dead set + have hdead : ∀ q ∈ D \ {ptr}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := fun h => htD (by rw [← P1.eq_of_bv h]; exact hq.1) + have hqe : q.bv ≠ ptr.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ⟨_, F.tcbO hqt hx, rfl, rfl, Or.inl rfl⟩) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {ptr}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {ptr}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) + (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) + (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread a sender with a timeout fault, `inClassR`) + have hsp : Fl4Spec.setThreadState (Z := Unit) t.bv ts σ = ({((), σ6)}, False) := Notif.setThreadState_eq ts hk + have hts6 : Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := some (.timeout b.bv) }) t.bv σ6 = + ({((), σf)}, False) := Notif.threadSet_eq _ hk6 + have hik : IK t.bv σ σ' := + (((ikm_setThreadState ts).of_eq hsp).trans ((ikm_threadSet (t := t.bv) (p := t.bv) + (g := fun tcb => { tcb with tcbFault := some (.timeout b.bv) }) + fun _ => ⟨rfl, fun h => absurd rfl h, fun h => absurd rfl h⟩).of_eq hts6)).trans hik0 + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnq hik + have I3 : Inv σ' := hIB.inv fun x' hx' hc => by + rw [hkt3] at hx'; cases hx' + exact (inClassR.notTmoS hc ptr.bv ds b.bv rfl rfl).elim + -- `BlockedScActive`: the thread's SC is `sc`, active + have hscA : ∀ q y1 n1, σ.kheap q = some (.schedContext y1 n1) → y1.scTcb = some t.bv → + Fl4Spec.scActive y1 = true := by + intro q y1 n1 hq' hyt' + obtain ⟨c, hcC, eq⟩ := J.obj.sc q y1 n1 hq' + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hc0 := J.c0 c hcC + have hcS : c ∈ P1.Heap.scs H := ⟨_, (hJ.sc c y1 n1 hc0 hq').2⟩ + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hcC hcS hc0 hq' hyt' + rw [hk] at hku; cases hku + have e1 : c.bv = sc.bv := Option.some.inj (hxs.symm.trans hxsc) + rw [e1, hky] at hq'; cases hq' + exact hya + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {ptr}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted fun y1 hy1 => (hsrt y1 hy1).congr fun u _ => (hprf u).symm, + hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have hNF : NatFoot (L := L) Z s σ s' σ' := NatFoot.ofWS hF.1 hW + (fun _ h => Or.inl (Ipc.CnW.oobj J.xtop.fin.1.1.1 h)) (fun _ h => J.cnW_nv h) (fun _ h => J.cnW_nh h) + (natK_bfrt F) + exact ⟨Z.vs, D \ {ptr}, J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (J.zD_diffE hpE), hNF⟩ + all_goals simp [absCap] at habs6 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBTcbBuf.lean b/proofs/Fl4Proofs/P3/JAtZBTcbBuf.lean new file mode 100644 index 000000000..753892548 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBTcbBuf.lean @@ -0,0 +1,174 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route D): **a TCB's IPC-buffer word over `JAtZB`, with `NatFoot`** +(`JAtZB.bufWrF`, the `JAtZB` sibling of XTopJointTcbInstall's `JAtZ.bufWrJ`; generated by `handoff/tools/gen_tcbbuf.py`). +No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointTcbInstall +import Fl4Proofs.P3.JAtZBThrSched +import Fl4Proofs.P3.LiveFrame +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A TCB word below the debug links is no debug word.** -/ +theorem JAtZB.tcbw_notDbg {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {b : U64} {K : Nat} (hb : b.val = t.val + K) (hK : K < 3576) : + ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := + J.notDbg_tw (fun y hy w K' hwv h1 h2 => tcbOff_ne J.ip.e.tcb ht hy hb hwv (by omega) (by omega) (by omega)) + (fun c hc e => by + have hN : P1.NodePlaced st Z.T s c := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk c hc + have hnv := P1.nodeAt_val hN (off := layout.node.DEBUG_TCBS) (by simp) + have := J.kern.hnd t _ (J.zH.1 t ht) c hc + simp only [P1.HKind.bits] at this + rw [← e] at hnv + have hd88 : layout.node.DEBUG_TCBS.val = 88 := P1.debug_val + omega) + +open Fl4Proofs.P1 Fl4Proofs.P3.Sched in +set_option maxHeartbeats 8000000 in +/-- **A TCB's IPC-buffer word at `J`** (as XTopJointTcbSched's +`JAtZ.mcpWrJ`): FL4's word (offset 3512; P1's `BlkOK` by `plainW_all`), +the spec's `tcb_ipc_buffer` (`XTopAt.bufSpec`, a `QKM` step), `BufWordRel` +by XTopBuf's `bufWordRel_setIpc`; the other fields as `JAtZ.mcpWrJ`. -/ +theorem JAtZB.bufWrF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (v : U64) (hnf : x.tcbIpcframe = .nullCap) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (tIpcBufA t) v) a + (Notif.updK σ t.bv (.tcb { x with tcbIpcBuffer := v.bv })) ∧ + NatFoot (L := L) Z s σ (L.wr s (tIpcBufA t) v) (Notif.updK σ t.bv (.tcb { x with tcbIpcBuffer := v.bv })) := by + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hta := hT t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hKt : (tIpcBufA t).val = t.val + 3512 := tIpcBufA_val hta + have hwa : P1.Aligned (tIpcBufA t) := tIpcBufA_aligned hta + set s' := L.wr s (tIpcBufA t) v with hs' + set σ' := Notif.updK σ t.bv (.tcb { x with tcbIpcBuffer := v.bv }) with hσ' + have hw : P1.WritesIn st s s' (· = tIpcBufA t) := P1.writesIn_wr hwa + obtain ⟨hblk, hfoot0, -⟩ := P1.plainW_all (K := 3512#u64) (v := v) J.blk htH (by decide) (by decide) (by decide) + (by decide) (by decide) (by decide) (by decide) + have hfoot : P1.WritesIn st s (L.wr s (tIpcBufA t) v) (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) ∅ s) := hfoot0 + have hA : QKM σ σ' := QKM.ipcBuf hk v.bv + have hX' : XTopAt (L := L) AR Z s' a σ' := (J.xtop.bufWr ht v).bufSpec (tcbBufStep_upd hk v.bv) + have hks : L.ksb s' = L.ksb s := hw.1 + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hwW : P1.WritesIn st s s' (WideW (st := st) Z.T Z.N Z.C Z.c s σ) := + hw.mono fun b hb => Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩ + -- the words other than `t`'s IPC-buffer word kept + have keep : ∀ b, P1.Aligned b → b ≠ tIpcBufA t → L.mem s' b = L.mem s b := fun b hb hne => + hw.mem_eq hb hne + have hBW : BufWordRel Z.T (L.mem s') σ' := bufWordRel_setIpc J.xtop ht hk v J.buf + have hI3 := hX'.irelK (by rw [hks]; exact J.ks) hBW + have hI := J.xtop.irelK J.ks J.buf + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => + Or.inr (Or.inr (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + have hck : Fl4Proofs.P3.CKeep σ σ' := hA.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + -- the MCP, yield, SC-reply and reply-link words: not `t`'s IPC-buffer word + have k1 : ∀ u ∈ Z.T, L.mem s' (mcpA u) = L.mem s (mcpA u) ∧ L.mem s' (flagsA u) = L.mem s (flagsA u) := fun u hu => + ⟨keep _ (mcpA_aligned hT hu) fun e => by + have h1 := mcpA_val hT hu + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega, + keep _ (flagsA_aligned hT hu) fun e => by + have h1 := flagsA_val hT hu + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega⟩ + have k2 : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => + keep _ (yieldA_aligned hT hu) fun e => by + have h1 := yieldA_val hT hu + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega + have k3 : ∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q) := fun q hq => + keep _ (sc_reply_word hS hq).1 fun e => by + obtain ⟨y, n, hky, -⟩ := hs.scs q hq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hHq := (hJ.sc q y n (J.c0 q hq) hky).2 + have hz := J.kern.heap.scmin q _ hHq + have h128 : 2 ^ 7 ≤ 2 ^ (n + Fl4Spec.minSchedContextBits) := Nat.pow_le_pow_right (by norm_num) hz + have hv := (sc_reply_word hS hq).2.1 + rw [e, hKt] at hv + have hne : q ≠ t := fun e' => by rw [e', J.zH.1 t ht] at hHq; cases hHq + have := J.kern.heap.disj q _ t _ hHq (J.zH.1 t ht) hne + simp only [P1.HKind.bits] at this + omega + have k4 : ∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r) := by + intro r hr + have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + keep _ (J.rp.aligned hr ha) fun e => by + have := J.rp.tcb r hr t ht; rw [e, hKt] at ha; omega + exact ⟨k _ (Or.inl h8), k _ (Or.inr h16)⟩ + have ck := hA.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hA.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hI3.2.2.1 + have hAI3 : ActIdle σ' := hA.actIdle J.act + have hyk : YKeep σ σ' := by + refine ⟨fun q y hq => ?_, fun q y n hq => ?_, fun q y n hq => ?_⟩ + · by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq; exact ⟨{ x with tcbIpcBuffer := v.bv }, by simp [hσ', Notif.updK], rfl⟩ + · exact ⟨y, by simp [hσ', Notif.updK, e, hq], rfl⟩ + · by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq + · exact ⟨y, by simp [hσ', Notif.updK, e, hq], rfl⟩ + · by_cases e : q = t.bv + · subst e; simp [hσ', Notif.updK] at hq + · exact ⟨y, by simpa [hσ', Notif.updK, e] using hq, rfl⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_qm hA hS3 hI3.1 hwW k3 hAI3.1 (J.yld.frame k2 hyk) hyk + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hA.szKeep q.bv y n hq) hks (J.wideW_irq hwW) + (J.idleW_keep hS3 hA.idle hA.isc).2⟩ + have hkern := J.kern_keep hks hwW.2.1 hS3 hA.ct hA.csc (fun sl h => Or.inl (by rw [← hA.orig]; exact h)) + (by rw [hA.arch]) + (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hDbg + (D' := D) + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 hyk, J.mcp.frame k1 (mcpKeep_buf hk v.bv), hBW, hRP', + J.apart.sc_frame hks hA.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hA.idle hA.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hw fun b hb u hu e => Ipc.tw_ne (a := b) hT ht hu (K0 := 3512) (K := 3528) + (by rw [hb, hKt]) (by omega) (by omega) (by omega) (by rw [e]; exact J.tdA_val' hu))), hkern, + J.wideW_utc hwW hF.1 hA.cdt hA.orig hA.arch hDbg, hA.nsc J.nsc, J.inv.updSame hk rfl rfl (Or.inl rfl), hA.cur J.cur, + hA.own J.own, hA.off J.off, hAI3, hA.bsa J.bsa, hA.isorted J.isorted, hA.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameB J hF.1 hA.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + refine ⟨Z.vs, J.cs_frameI hks hX3 hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := 3512) hT ht hu (by rw [hb, hKt]) (by omega) (by omega)) hA.arch (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hA.szKeep hF.1 hA.kback (IpcFrKeep.of_nullFrame hk hnf) hC R + (dbgKeep_of_writes J.blk hw fun b hb => J.tcbw_notDbg ht (K := 3512) (by rw [hb, hKt]) (by omega)) hA.orig + (XTopAt.idleW_eqS J.xtop hX3 hA.idle) J.zD, + NatFoot.ofW J.xtop hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := 3512) hT ht hu (by rw [hb, hKt]) (by omega) (by omega)) + (fun b hb => J.tcbW_nh ht (by rw [hb, hKt]; omega) (by rw [hb, hKt]; omega)) + (natK_updTcb hk fun h => h)⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrBind.lean b/proofs/Fl4Proofs/P3/JAtZBThrBind.lean new file mode 100644 index 000000000..302fe3d00 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrBind.lean @@ -0,0 +1,204 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **`bind_notification`**. + +Generated by `handoff/tools/gen_thrprim.py` (`JAtZB` siblings with the native footprint `NatFoot`). New file; no +existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointTcbMisc +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.ScSignalJN +import Fl4Proofs.P3.XTopJointReply +import Fl4Proofs.P3.JAtZBRestartN +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Fl4Proofs.P1 in +/-- **A thread cap and a notification cap in slots cover their objects** +(`JAtZ.cover_cap` with no write). -/ +theorem JAtZB.cover_tn (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n : U64} (ht : t ∈ Z.T) (hn : n ∈ Z.N) + {xT xN : U64} (hxT : IsSlot Z.th xT) (hcT : capAt (L.mem s) xT = ok (.Thread t)) + (hxN : IsSlot Z.th xN) {nb : U64} {nw nr : Bool} (hcN : capAt (L.mem s) xN = ok (.Notification n nb nw nr)) : + ((∀ k, Z.th t = some k → Sched.Covered (L.mem s) Z.G t.val (t.val + max (objSize k) 1)) ∧ + (∀ k, H t = some k → Sched.Covered (L.mem s) Z.G t.val (t.val + 2 ^ k.bits))) ∧ + ((∀ k, Z.th n = some k → Sched.Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Sched.Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) := by + have hH := J.kern.heap + have hHt : H t = some .tcb := J.zH.1 t ht + have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + have hft := hH.fits t _ hHt + have hfn := hH.fits n _ hHn + simp only [P1.HKind.bits] at hft hfn + refine ⟨J.cover_cap (P1.WritesIn.refl s _) hxT hcT (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_thread t hft) fun k hk => ?_, J.cover_cap (P1.WritesIn.refl s _) hxN hcN (fun h => by cases h) + (ok_inj (phys_eq _).symm) rfl (range_ntfn n nb nw nr hfn) fun k hk => ?_⟩ + · rw [hHt] at hk; cases hk + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + · rw [hHn] at hk; cases hk + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + +open Fl4Proofs.P1 in +set_option maxHeartbeats 16000000 in +/-- **`bind_notification` at `J`** (module docstring): FL4's two writes +(`bind_ntfn_eq`) against the spec's (`bindNotification_eq`), `IRel` after +(`bound_wr_irel`), the footprint `FinW` (as `unbindNJ`'s), the spec frame +`BKY` with `Inv` after (`Inv.updNtfn`, `Inv.tcbUpd`), P1's +`bind_notification.blk` (both unbound before, `hz1`/`hz2`: the decode's +checks) and its footprint (as `unbind_notification.blkF`); +the thread and the notification leave the dead set, covered by their caps +(`JAtZ.cover_tn`). -/ +theorem JAtZB.bindNF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n : U64} (ht : t ∈ Z.T) (hn : n ∈ Z.N) + (hn0 : n ≠ 0#u64) (hz1 : L.mem s (P1.boundA t) = 0#u64) (hz2 : L.mem s (P1.ntfnBtA n) = 0#u64) {xT xN : U64} (hxT : IsSlot Z.th xT) (hcT : capAt (L.mem s) xT = ok (.Thread t)) + (hxN : IsSlot Z.th xN) {nb : U64} {nw nr : Bool} (hcN : capAt (L.mem s) xN = ok (.Notification n nb nw nr)) : + notification.bind_notification st s t n ⦃ s' => ∃ σ', + Fl4Spec.bindNotification (Z := Unit) t.bv n.bv σ = ({((), σ')}, False) ∧ + ∃ V D', JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D' s' a σ' ∧ + NatFoot (L := L) Z s σ s' σ' ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + obtain ⟨x, hk, -⟩ := hI.1.tcb t ht + obtain ⟨xn, hkn, -⟩ := hI.1.ntfn n hn + have ht0 : t ≠ 0#u64 := fun e => (hP0.e.tcb t ht).ne0 (by rw [e]; rfl) + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hnH : n ∈ P1.Heap.ntfns H := J.zH.2.2.2 n hn + obtain ⟨⟨cT1, cT2⟩, ⟨cN1, cN2⟩⟩ := J.cover_tn ht hn hxT hcT hxN hcN + -- P1's `BlkOK` and footprint + have hblk0 := P1.notification.bind_notification.blk s t n J.blk htH hnH hz1 hz2 + rw [Sched.bind_ntfn_eq s t n, WP.spec_ok] at hblk0 + have hb := J.blk + have hT := hb.1.choose_spec.choose_spec.tcbs + have hm : P1.ntfnBtA n ∈ P1.Heap.frame H := (hb.2.2.1.1.ntB n hnH).2 + have f1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s + (L.wr s (P1.ntfnBtA n) t) := + P1.foot_wr (hb.1.choose_spec.choose_spec.omega _ (hb.2.2.2.2.sub _ hm)).1 (P1.kw_sg hm) + have hfoot0 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s + (L.wr (L.wr s (P1.ntfnBtA n) t) (core.num.U64.wrapping_add t 3368#u64) n) := + f1.trans (P1.foot_field (K := 3368#u64) hT htH (by simp) (by simp) (by simp) (by simp)) + have hfoot : P1.WritesIn st s (L.wr (L.wr s (Notif.nBoundA n) t) (P1.boundA t) n) (P1.KW st (P1.Heap.tcbs H) Cs + (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s) := hfoot0 + rw [Sched.bind_ntfn_eq s t n, WP.spec_ok] + -- `IRel` after + have hX2 := bound_wr_irel hP0 hI ht hn hk hkn t n (fun _ => ht) + rw [Notif.optRef_ne ht0, Notif.optRef_ne hn0] at hX2 + have he := Sched.bindNotification_eq hkn hk + refine ⟨_, he, ?_⟩ + have F : Sched.BKY σ (Notif.updK (Notif.updK σ n.bv (.notification { xn with ntfnBoundTcb := some t.bv })) t.bv + (.tcb { x with tcbBoundNotification := some n.bv })) := Sched.bky_bind hk hkn rfl rfl + set σ' : Fl4Spec.State Unit := Notif.updK (Notif.updK σ n.bv (.notification { xn with ntfnBoundTcb := some t.bv })) + t.bv (.tcb { x with tcbBoundNotification := some n.bv }) with hσ' + -- the footprint + have hba : Aligned (nBoundA n) := by rw [nBoundA_eq]; exact (nword hP0.n hn (K := 24#u64) (by simp) (by simp)).1 + have hbv : (nBoundA n).val = n.val + 24 := by rw [nBoundA_eq]; exact nfield_val (hP0.n.ntfn n hn) (by simp) + have hpa := hP0.e.tcb _ ht + have hta := (a_bound hpa).1 + have hbvp := val_bound hpa + have hw2 := (writesIn_wr (s := s) (v := t) hba).trans + (writesIn_wr (s := L.wr s (nBoundA n) t) (v := n) hta) + have hF : IFrame (L := L) Z.th s (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) := + IRel.iframe_of hI hw2 fun b hb => by + rcases hb with rfl | rfl + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨n, hn, by rw [hbv]; omega, by rw [hbv]; omega⟩))))) + · exact Or.inr (Or.inr (Or.inl ⟨_, ht, by rw [hbvp]; omega, by rw [hbvp]; omega⟩)) + have hw : P1.WritesIn st s (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) + (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := hw2.mono fun b hb => by + rcases hb with rfl | rfl + · exact NWrite.finW hP0.e.tcb (Or.inl ⟨n, hn, by rw [hbv]; omega, by rw [hbv]; omega⟩) + · exact finW_bound hP0.e.tcb ht + -- the spec's `Inv` + have htn : t.bv ≠ n.bv := by intro e; rw [e] at hk; rw [hk] at hkn; cases hkn + have I1 := J.inv.updNtfn hkn (x' := { xn with ntfnBoundTcb := some t.bv }) + (fun p hp => J.inv.ntfnSc n.bv xn p hkn hp) + have hkt1 : (Notif.updK σ n.bv (.notification { xn with ntfnBoundTcb := some t.bv })).kheap t.bv = + some (.tcb x) := by simp [Notif.updK, htn, hk] + have I3 := I1.tcbUpd hkt1 { x with tcbBoundNotification := some n.bv } rfl rfl rfl + -- the assembly (as `JAtZ.reg_frameFp`) + have hck : Fl4Proofs.P3.CKeep σ σ' := F.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hX2 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + have hks : L.ksb (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.finW_keeps hw + have ck := F.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 F.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 := hX2.2.2.1 + have hAI3 : ActIdle σ' := F.actIdle J.act + -- the dead set without the thread and the notification + set D' : Set U64 := {q | q ∈ D ∧ q ≠ t ∧ q ≠ n} with hD' + have hdead : ∀ q ∈ D', DeadP (L := L) H (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) q := by + intro q hq + have hqt : q.bv ≠ t.bv := fun e => hq.2.1 (P1.eq_of_bv e) + have hqn : q.bv ≠ n.bv := fun e => hq.2.2 (P1.eq_of_bv e) + have ek : σ'.kheap q.bv = σ.kheap q.bv := by + simp [hσ', Notif.updK, hqt, hqn] + refine J.deadP_keep hS3 hX2.1 (fun y hy _ => ⟨y, by rw [ek]; exact hy, rfl, rfl, Or.inl rfl⟩) + (fun y hy => by rw [ek]; exact hy) (fun y hy => ⟨y, by rw [ek]; exact hy, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun y hy => ⟨y, by rw [ek]; exact hy, rfl⟩) (fun y m hy => ⟨y, by rw [ek]; exact hy, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.finW_scOut hw hz hqC hK8 hK) rfl hAI3.1 + (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) (J.jcov.dead q hq.1) + -- `JCovD` with the new dead set + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hJ0 : JCovD (L := L) Z.th H Z.G D' Cs s := by + refine ⟨fun q k hq => ?_, fun q k hq => ?_, fun q hq => J.jcov.dead q hq.1, J.jcov.ks, J.jcov.idleOut, + fun q hq => J.jcov.dx q hq.1⟩ + · rcases J.jcov.th q k hq with hc | hd | hi + · exact Or.inl hc + · by_cases e1 : q = t + · subst e1; exact Or.inl (cT1 k hq) + · by_cases e2 : q = n + · subst e2; exact Or.inl (cN1 k hq) + · exact Or.inr (Or.inl ⟨hd, e1, e2⟩) + · exact Or.inr (Or.inr hi) + · rcases J.jcov.h q k hq with hc | hd | hi + · exact Or.inl hc + · by_cases e1 : q = t + · subst e1; exact Or.inl (cT2 k hq) + · by_cases e2 : q = n + · subst e2; exact Or.inl (cN2 k hq) + · exact Or.inr (Or.inl ⟨hd, e1, e2⟩) + · exact Or.inr (Or.inr hi) + have hidl := J.idleW_keep hS3 F.idle F.isc + have hjc : JCovD (L := L) Z.th H Z.G D' Cs (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) := by + exact hJ0.same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) + (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.tdkeep_of hw fun _ hb => Ipc.FinW.ntd J.xtop.fin.1.1.1 hb))) + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y m _ hq => F.szKeep q.bv y m hq) hks (J.finW_irq hw) hidl.2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 F.ct F.csc (fun sl h => Or.inl (by rw [← F.orig]; exact h)) + (by rw [F.arch]) + (fun q ao h => (F.oth (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hDbg (D' := D') + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D' (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjc, hkern, + J.finW_utc hw hF.1 F.cdt F.orig F.arch hDbg, F.nsc J.nsc, I3, F.cur J.cur, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa, F.isorted J.isorted, F.curRunM J.curRun, hblk0⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hC : JCs (L := L) (Z.mv Z.G V') (L.wr (L.wr s (nBoundA n) t) (P1.boundA t) n) σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + refine ⟨Z.vs, D', J.cs_frameI hks hX3 hw (fun b hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) F.arch (fun q ao h => (F.oth (fun _ e => by cases e) (fun _ e => by cases e)).2 h) F.szKeep hF.1 F.kback F.ipcFr hC R hKd hck.orig hidl.1 + (fun p hp => ⟨J.zD p hp, fun e => J.not_ztcb hxT (fun _ _ _ h => by rw [capV_of_capAt hcT] at h; cases h) + (by rw [capV_of_capAt hcT, e]; simp [absCap, Fl4Spec.objRefs]) hp, fun e => by + have h1 : H p = some .tcb := J.zH.1 p (J.thT p (J.ztcb_tcb hp)) + rw [e, J.zH.2.2.2 n hn] at h1; cases h1⟩), + NatFoot.ofW J.xtop hw (fun b hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) + (fun b hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) (fun b hb => J.finW_nh hb) + ((natK_ntfnUpd hkn).trans (natK_tcbW hkt1 rfl (fun h => h)))⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrBindTop.lean b/proofs/Fl4Proofs/P3/JAtZBThrBindTop.lean new file mode 100644 index 000000000..719cfec21 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrBindTop.lean @@ -0,0 +1,260 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **BindNotification**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrBind +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The BindNotification label at `JAt`** (as `tcb_bind_ntfnX`): the decode's +checks at the start's `JAtZ` (the notification cap's object among the +notifications by `JAtZ.capN`), then the caller's restart (`JAtZ.restartJ`, the +slots kept: `restart_dstate`) and XTopJointTcbMisc's `JAtZ.bindNJ` (the thread +and the notification covered by the invoked thread cap and the extra cap). -/ +theorem JAtZB.tcbBindNtfnF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbBindNotification) + (length buf : U64) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (ct : U64) (hct : sched.cur_thread st s = ok ct) + {xT : U64} (hxT : IsSlot Z.th xT) (hcT : capV (L.mem s) xT = .Thread p) + (call : Bool) (slot : obj.Slot) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (blk don : Bool) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBBindNotification p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl args ci sl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + have hcapN : x0 ≠ 0#u64 → ∀ q b w r, capV (L.mem s) x0 = .Notification q b w r → q ∈ Z.N := by + intro hx q b w r hc + obtain ⟨e, -, ex, -, ps, hps, he⟩ := hXc.get (show [x0, x1, x2][0]? = some x0 from rfl) hx + exact (J.capN ⟨x0, b, w, r, by rw [ex]; exact AbsSlot.isSlot (th := Z.th) (sl := e.2) ⟨ps, he, Or.inl hps⟩, + hc⟩).1 + have hdec0 : specDec false slbl args ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeBindNotification (Z := Unit) (.threadCap p.bv) excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + have hNR : Notif.NtfnStateRel Z.N Z.T (L.mem s) σ := hX.fin.1.1.2.2.1 + have hNP : Notif.NPlaced Z.T Z.N := hX.fin.1.1.1.placed + have hbnd : ∀ xp, σ.kheap p.bv = some (.tcb xp) → xp.tcbBoundNotification = Notif.optRef (L.mem s (P1.boundA p)) := by + intro xp hk + obtain ⟨xp', hkp', hxp⟩ := hX.fin.1.1.2.1.tcb p hpT + rw [hk] at hkp'; cases hkp'; exact hxp.bound + unfold tcb.decode + rw [hstall, hdec0] + simp only [bind_ok, u64_eq_iff, lbl_tcbSetEPTRoot_val, lbl_tcbReadRegisters_val, lbl_tcbWriteRegisters_val, lbl_tcbCopyRegisters_val, + lbl_tcbConfigure_val, lbl_tcbSetPriority_val, lbl_tcbSetMCPriority_val, lbl_tcbSetSchedParams_val, + lbl_tcbSetTimeoutEndpoint_val, lbl_tcbSetIPCBuffer_val, lbl_tcbSetSpace_val, lbl_tcbSuspend_val, + lbl_tcbResume_val, lbl_tcbBindNotification_val] + simp only [Nat.reduceEqDiff, ↓reduceIte] + refine let_pair_spec s ar (fun s1 => tcb.decode_bind_notification st s1 p ⟨length, buf, call, slot, extra⟩) _ ?_ + unfold tcb.decode_bind_notification tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + obtain ⟨xp, hkp, -⟩ := hX.fin.1.1.2.1.tcb p hpT + have hbp := hbnd xp hkp + by_cases h0 : x0 = 0#u64 + · -- no extra cap: truncated + have hl : excaps.length = 0 := by have := ((hXc.2 0 x0 hx0').1).1 h0; omega + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + simp only [Fl4Spec.decodeBindNotification] + rw [whenE_pos hl] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + obtain ⟨e, he, hxe, he1, ps, hps, hps1⟩ := hXc.get hx0' h0 + have hexl : excaps.length ≠ 0 := by + have := List.length_pos_of_ne_nil (List.ne_nil_of_mem (List.mem_of_getElem? he)); omega + have hhd : excaps.head? = some e := by rw [List.head?_eq_getElem?]; exact he + simp only [h0, ↓reduceIte, show length >= 0#u64 from by scalar_tac, bind_ok, + core.result.Result.Insts.CoreOpsTry.branch, tcb_bound_eq] + -- the spec up to the bound-notification check + have hsp0 : ∀ (k : Option Fl4Spec.ObjRef → Fl4Spec.SEMonad Unit Fl4Spec.TcbInvocation), + ((Fl4Spec.NonDetE.whenE (excaps.length = 0) (Fl4Spec.NonDetE.throwError .truncatedMessage) >>= fun _ => + Fl4Spec.NonDetE.liftE (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbBoundNotification p.bv) >>= k) : + Fl4Spec.SEMonad Unit _).run σ = (k (Notif.optRef (L.mem s (P1.boundA p)))).run σ := by + intro k + rw [whenE_neg hexl, retOk_bind, + lE_bind (m := Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbBoundNotification p.bv) + (Notif.threadGet_eq _ hkp), hbp] + by_cases hb0 : L.mem s (P1.boundA p) = 0#u64 + swap + · -- already bound + simp only [bne_iff_ne, ne_eq, hb0, not_false_eq_true, ↓reduceIte, error.illegal, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) + apply run_liftME_err + simp only [Fl4Spec.decodeBindNotification] + rw [hsp0] + simp only [Notif.optRef, hb0, ↓reduceIte] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : + Fl4Spec.SEMonad Unit Unit)) rfl _ + simp only [hb0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + have hsp1 : (Fl4Spec.decodeBindNotification (Z := Unit) (.threadCap p.bv) excaps).run σ = (do + let (ntfnptr, rights) ← (match (absCap (capV (L.mem s) x0)) with + | .notificationCap ptr _ r => Fl4Spec.NonDetE.returnOk (ptr, r) + | _ => Fl4Spec.NonDetE.throwError .illegalOperation : Fl4Spec.SEMonad Unit _) + Fl4Spec.NonDetE.whenE (Fl4Spec.AllowRecv ∉ rights) <| Fl4Spec.NonDetE.throwError .illegalOperation + let ntfn ← Fl4Spec.NonDetE.liftE <| Fl4Spec.getNotification ntfnptr + match ntfn.ntfnObj, ntfn.ntfnBoundTcb with + | .idleNtfn, none => Fl4Spec.NonDetE.returnOk () + | .activeNtfn _, none => Fl4Spec.NonDetE.returnOk () + | _, _ => Fl4Spec.NonDetE.throwError .illegalOperation + Fl4Spec.NonDetE.returnOk <| Fl4Spec.TcbInvocation.notificationControl p.bv (some ntfnptr)).run σ := by + simp only [Fl4Spec.decodeBindNotification] + rw [hsp0] + simp only [Notif.optRef, hb0, ↓reduceIte] + rw [retOk_bind, hhd, the_some, he1] + generalize absCap (capV (L.mem s) x0) = cc + cases cc <;> rfl + unfold tcb.bindable_ntfn + rw [slot_cap_eq, capAt_capV] + simp only [bind_ok] + cases hc : capV (L.mem s) x0 + case Notification q b w r => + rw [hc] at hsp1 + simp only [absCap] at hsp1 + rw [retOk_bind] at hsp1 + have hqN := hcapN h0 q b w r hc + obtain ⟨xn, hkn, hrn⟩ := hNR.ntfn q hqN + by_cases hr : r = true + swap + · simp only [hr, Bool.false_eq_true, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) + apply run_liftME_err + rw [hsp1, whenE_pos (by simp only [allowRecv_mem]; exact hr)] + exact run_throw_bind _ _ _ + rw [whenE_neg (by simp only [allowRecv_mem, not_not]; exact hr), retOk_bind] at hsp1 + simp only [hr, ↓reduceIte, ntfn_head_eq, bind_ok] + have hbt := hrn.bound + have hob := hrn.obj + by_cases hh : L.mem s (P1.epHeadA q) = 0#u64 + swap + · -- a waiting notification + simp only [bne_iff_ne, ne_eq, hh, not_false_eq_true, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) + apply run_liftME_err + rw [hsp1]; dsimp only + rw [lE_bind (m := Fl4Spec.getNotification (Z := Unit) q.bv) (Notif.getNotification_eq hkn)] + have hw : ∃ qs, xn.ntfnObj = .waitingNtfn qs := by + revert hob + cases xn.ntfnObj with + | idleNtfn => intro hob; exact absurd (by have := hob.2.head; simpa [Notif.qOf] using this) hh + | activeNtfn b => intro hob; exact absurd (by have := hob.2.1.head; simpa [Notif.qOf] using this) hh + | waitingNtfn qs => intro _; exact ⟨qs, rfl⟩ + obtain ⟨qs, hqs⟩ := hw + rw [hqs] + exact run_throw_bind _ _ _ + simp only [hh, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ntfn_bound_eq, bind_ok] + have hnw : ∀ qs, xn.ntfnObj ≠ .waitingNtfn qs := by + intro qs hq + rw [hq] at hob + obtain ⟨-, xs, hxs, -, hqr, -⟩ := hob + obtain ⟨y, ys, rfl⟩ := List.exists_cons_of_ne_nil hxs + have h1 := hqr.head + have := hqr.ne0 hNP.tcb y (by simp) + simp only [Notif.qOf, List.headD_cons] at h1 + rw [hh] at h1 + exact this h1.symm + by_cases hnb : L.mem s (Notif.nBoundA q) = 0#u64 + swap + · -- a bound notification + simp only [bne_iff_ne, ne_eq, hnb, not_false_eq_true, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) + apply run_liftME_err + rw [hsp1]; dsimp only + rw [lE_bind (m := Fl4Spec.getNotification (Z := Unit) q.bv) (Notif.getNotification_eq hkn), hbt] + simp only [Notif.optRef, hnb, ↓reduceIte] + generalize xn.ntfnObj = o + cases o <;> exact run_throw_bind _ _ _ + simp only [hnb, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeBindNotification (Z := Unit) (.threadCap p.bv) excaps)).run σ = + ({(.ok (.invokeTCB (.notificationControl p.bv (some q.bv))), σ)}, False) := by + apply run_liftME_ok + rw [hsp1]; dsimp only + rw [lE_bind (m := Fl4Spec.getNotification (Z := Unit) q.bv) (Notif.getNotification_eq hkn), hbt] + simp only [Notif.optRef, hnb, ↓reduceIte] + cases ho : xn.ntfnObj with + | waitingNtfn qs => exact absurd ho (hnw qs) + | idleNtfn => exact (congrArg (fun m : Fl4Spec.SEMonad Unit Fl4Spec.TcbInvocation => m.run σ) (retOk_bind () _)).trans rfl + | activeNtfn b => exact (congrArg (fun m : Fl4Spec.SEMonad Unit Fl4Spec.TcbInvocation => m.run σ) (retOk_bind () _)).trans rfl + have hx0s : IsSlot Z.th x0 := by + obtain ⟨e, -, ex, -, ps, hps, he⟩ := hXc.get hx0' h0 + rw [ex]; exact AbsSlot.isSlot (th := Z.th) (sl := e.2) ⟨ps, he, Or.inl hps⟩ + have hq0 : q ≠ 0#u64 := (J.capN ⟨x0, b, w, r, hx0s, hc⟩).2 + obtain ⟨s1, σ1, -, hrc1, hst, -, -, -, -, -, -, hss1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + obtain ⟨hcur, -⟩ := J.curEq hct + -- both still unbound after the restart (P1's `KeepT` of the restart) + have ectc : ct = L.curW s := by + have hc2 : sched.cur_thread st s = ok (L.curW s) := cur_thread_eq s + rw [hct] at hc2; exact eq_of_ok_eq hc2 + obtain ⟨-, xc, hkxc, hrxc⟩ := J.curRunnable hni (cur_thread_eq s) + have hcTZ : L.curW s ∈ Z.T := by rw [J.tcbs_eq.1]; exact J.kern.curH + have hrunc : P1.Runnable (L.mem s (P1.stA (L.curW s))) := by + rw [stA_tStA]; exact (runnable_iff (J.xtop.fin.1.1.2.2.1.at_tcb hcTZ hkxc).st).2 hrxc + have hkr := P1.sched.set_thread_state.restart_blk (Es := ∅) s (L.curW s) J.blk J.kern.curH + (P1.runnable_ok hrunc).1 (P1.runnable_ok hrunc).2 + rw [← ectc, hss1, WP.spec_ok] at hkr + obtain ⟨-, -, -, kr⟩ := hkr + have hz1 : L.mem s1 (P1.boundA p) = 0#u64 := by rw [kr.bndf (J.zH.1 p hpT)]; exact hb0 + have hz2 : L.mem s1 (P1.ntfnBtA q) = 0#u64 := by rw [kr.ntb q (J.zH.2.2.2 q hqN)]; exact hnb + obtain ⟨s1', σ1', hrc', -, -, hSS, -⟩ := restart_dstate Z.th hX.rel.1 s σ Z.G + ⟨hX.rel.2.1, J.del, hX.rel.2.2.1⟩ ct hct hcur (hX.curTcb hcur).1 J.kern.act + have e1 : s1' = s1 := by + have := hrc'.symm.trans hrc1 + simpa using this + subst e1 + have hcT1 : capAt (L.mem s1') xT = ok (.Thread p) := by rw [capAt_capV, hSS.capV hxT, hcT] + have hcN1 : capAt (L.mem s1') x0 = ok (.Notification q b w r) := by rw [capAt_capV, hSS.capV hx0s, hc] + refine leaf_assoc ?_ + rw [hrc1]; simp only [bind_ok] + refine WP.spec_bind (J1.bindNF hpT hqN hq0 hz1 hz2 hxT hcT1 hx0s hcN1) ?_ + rintro s2 ⟨σ', hbn, V, D', J2, F2⟩ + refine (WP.spec_ok _).2 ?_ + have hz : ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don + (.invokeTCB (.notificationControl p.bv (some q.bv)))).run σ1).1 := by + show _ ∈ ((Fl4Spec.NonDetE.liftE _).run σ1).1 + exact mem_liftE (x := ([], σ')) (mem_bind_nd (x := ((), σ')) (by rw [hbn]; exact Set.mem_singleton _) rfl) + exact Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst, _, hz, V, Ac1, Lst1, D', J2, J.natTrans F1 F2.cast⟩ + all_goals + rw [hc] at hsp1 + simp only [error.illegal, bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) + apply run_liftME_err + rw [hsp1] + exact run_throw_bind _ _ _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrBp.lean b/proofs/Fl4Proofs/P3/JAtZBThrBp.lean new file mode 100644 index 000000000..163d8da1e --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrBp.lean @@ -0,0 +1,162 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **SetBreakpoint, UnsetBreakpoint**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_threxttop.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrExtAux +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The UnsetBreakpoint label at `JAt`** (as `tcb_unset_bpX`): the +argument by `ArgRel` [J], the caller's restart (`JAtZ.restartJ`), the two +debug words (`JAtZ.dbgWrJ`); the spec keeps its state. -/ +theorem JAtZB.tcbUnsetBpF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .unsetBreakpoint) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (buffer : Option Fl4Spec.ObjRef) + (hA : ArgRel (st := st) s σ ct buf buffer 1) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + tcb.decode st arI s ar fl4_abi.label.TCBUnsetBreakpoint p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostF (L := L) arI AR Z H Cs s ar σ + (specDec false slbl args ci sl (.Thread p) excaps buffer) res ⦄ := by + obtain ⟨hcur, -⟩ := J.curEq hct + have hdec0 : specDec false slbl args ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4UnsetBreakpoint (Z := Unit) buffer p.bv) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [decode_unset_eq arI s ar p _ hstall, hdec0] + obtain ⟨bp, hbp, hbs⟩ := arg_rel hA hct hcur ⟨length, buf, call, slot, extra⟩ rfl 0#u64 (by decide) + rw [hbp]; simp only [bind_ok]; rw [check_bp_eq] + simp only [bind_ok] + have hsp : (Fl4Spec.decodeFl4UnsetBreakpoint (Z := Unit) buffer p.bv).run σ = + (Fl4Spec.fl4CheckBpNum (Z := Unit) bp.bv >>= fun _ => + Fl4Spec.NonDetE.returnOk (Fl4Spec.Fl4TcbInvocation.unsetBreakpoint p.bv bp.bv)).run σ := by + unfold Fl4Spec.decodeFl4UnsetBreakpoint + exact run_bind_ok hbs _ + by_cases hb : bp ≥ 4#u64 + · simp only [hb, ↓reduceIte, 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.inl ⟨_, rfl, rfl, rfl, _, ?_, bp_err⟩) + apply run_liftME_err + rw [hsp] + exact run_bind_error (by rw [checkBp_run, if_pos hb]) _ + · simp only [hb, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + obtain ⟨s1, σ1, x, hrc1, hst1, -, -, -, -, -, -, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + rw [hrc1]; simp only [bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4UnsetBreakpoint (Z := Unit) buffer p.bv)).run σ = + ({(.ok (.invokeFl4TCB (.unsetBreakpoint p.bv bp.bv)), σ)}, False) := by + apply run_liftME_ok + rw [hsp, run_bind_ok (by rw [checkBp_run, if_neg hb])] + rfl + have hbpv : bp.val < 4 := by + have : ¬ (4 ≤ bp.val) := fun h => hb (by scalar_tac) + omega + have hpf := (J1.ip.e.tcb p hpT).fits + refine WP.spec_bind (unset_bp_wr s1 p bp hbpv) ?_ + rintro s2 ⟨v, rfl⟩ + obtain ⟨V2, J2, F2⟩ := J1.dbgWrF hpT (dbgA p 5#u64) (448 + 40) (by omega) + (by rw [dbgA_val (by omega) (by decide)]; simp) v + obtain ⟨V3, J3, F3⟩ := J2.dbgWrF hpT (dbgA p bp) (448 + 8 * bp.val) (by omega) + (by rw [dbgA_val (i := bp) (by omega) (by omega)]; omega) 0#u64 + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdec, rfl, σ1, hst1, σ1, rfl, V3, Ac1, Lst1, D1, J3, + J.natTrans F1 (J1.natTrans F2.cast F3.cast).cast⟩) + +set_option maxHeartbeats 8000000 in +/-- **The SetBreakpoint label at `JAt`** (as `tcb_set_bpX`): FL4's checks +against the spec's (the five arguments by `ArgRel` [J]), the caller's restart +(`JAtZ.restartJ`), the two debug words (`JAtZ.dbgWrJ`); the spec keeps its +state. -/ +theorem JAtZB.tcbSetBpF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .setBreakpoint) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (buffer : Option Fl4Spec.ObjRef) + (hA : ArgRel (st := st) s σ ct buf buffer 5) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + tcb.decode st arI s ar fl4_abi.label.TCBSetBreakpoint p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostF (L := L) arI AR Z H Cs s ar σ + (specDec false slbl args ci sl (.Thread p) excaps buffer) res ⦄ := by + obtain ⟨hcur, -⟩ := J.curEq hct + have hdec0 : specDec false slbl args ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4SetBreakpoint (Z := Unit) buffer p.bv) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [decode_setbp_eq arI s ar p _ hstall, hdec0] + have hc : (⟨length, buf, call, slot, extra⟩ : tcb.Ctx).buf = buf := rfl + obtain ⟨bp, h0, g0⟩ := arg_rel hA hct hcur _ hc 0#u64 (by decide) + obtain ⟨vaddr, h1, g1⟩ := arg_rel hA hct hcur _ hc 1#u64 (by decide) + obtain ⟨ty, h2, g2⟩ := arg_rel hA hct hcur _ hc 2#u64 (by decide) + obtain ⟨size, h3, g3⟩ := arg_rel hA hct hcur _ hc 3#u64 (by decide) + obtain ⟨rw, h4, g4⟩ := arg_rel hA hct hcur _ hc 4#u64 (by decide) + rw [set_bp_checked_eq s p _ h0 h1 h2 h3 h4] + have hsp := set_bp_spec_run σ buffer p.bv g0 g1 g2 g3 g4 + have hrel := bpErr_rel bp vaddr ty size rw + cases hF : bpErrF bp vaddr ty size rw with + | some e => + cases hS : bpErrS bp vaddr.bv ty.bv size.bv rw.bv with + | none => rw [hF, hS] at hrel; exact hrel.elim + | some e' => + rw [hF, hS] at hrel + dsimp only at hrel + dsimp only + simp only [bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨e, rfl, rfl, rfl, e', ?_, hrel⟩) + apply run_liftME_err + rw [hsp, hS] + | none => + cases hS : bpErrS bp vaddr.bv ty.bv size.bv rw.bv with + | some e' => rw [hF, hS] at hrel; exact hrel.elim + | none => + have hbpv := bpErrF_bp hF + simp only + obtain ⟨s1, σ1, x, hrc1, hst1, -, -, -, -, -, -, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + rw [hrc1]; simp only [bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4SetBreakpoint (Z := Unit) buffer p.bv)).run σ = + ({(.ok (.invokeFl4TCB (.setBreakpoint p.bv bp.bv vaddr.bv ty.bv size.bv rw.bv)), σ)}, False) := by + apply run_liftME_ok + rw [hsp, hS] + have hpf := (J1.ip.e.tcb p hpT).fits + have hin : (do + let s2 ← debug.set_breakpoint st s1 p bp vaddr ty size rw + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2)) ⦃ x => + ∃ v, x = (.Ok (), L.wr (L.wr s1 (dbgA p bp) vaddr) (dbgA p 5#u64) v) ⦄ := + WP.spec_bind (set_bp_wr (st := st) s1 p bp vaddr ty size rw) (fun s2 ⟨v, h⟩ => (WP.spec_ok _).2 ⟨v, by rw [h]⟩) + refine WP.spec_bind hin ?_ + rintro x ⟨v, rfl⟩ + obtain ⟨V2, J2, F2⟩ := J1.dbgWrF hpT (dbgA p bp) (448 + 8 * bp.val) (by omega) + (by rw [dbgA_val (i := bp) (by omega) (by omega)]; omega) vaddr + obtain ⟨V3, J3, F3⟩ := J2.dbgWrF hpT (dbgA p 5#u64) (448 + 40) (by omega) + (by rw [dbgA_val (by omega) (by decide)]; simp) v + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdec, rfl, σ1, hst1, σ1, rfl, V3, Ac1, Lst1, D1, J3, + J.natTrans F1 (J1.natTrans F2.cast F3.cast).cast⟩) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrCopy.lean b/proofs/Fl4Proofs/P3/JAtZBThrCopy.lean new file mode 100644 index 000000000..494053cec --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrCopy.lean @@ -0,0 +1,341 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **`copy_registers`**. + +Generated by `handoff/tools/gen_thrregs.py` (`JAtZB` siblings with the native footprint `NatFoot`). New file; no +existing statement changed. Premise [P] `MIN_BUDGET` (as native). +-/ +import Fl4Proofs.P3.XTopJointCopyRegs +import Fl4Proofs.P3.JAtZBRestartN +import Fl4Proofs.P3.XTopJointSuspend +import Fl4Proofs.P3.XTopJointCancel +import Fl4Proofs.P3.XTopJointCancelRecv +import Fl4Proofs.P3.XTopJointCancelReply +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- `JAtZB` at some sets, with the native footprint from `(s0, σ0)`. -/ +abbrev JAtVF (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) + (s0 : S) (σ0 : Fl4Spec.State Unit) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + ∃ (V : XVs) (Ac : Set U64) (Lst : U64 → List U64) (D : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D s a σ ∧ NatFoot (L := L) Z s0 σ0 s σ + +set_option maxHeartbeats 8000000 in +/-- **A register-copy loop of `copy_registers` at `J`** (as `cr_loop_gen`). -/ +theorem crB_loop {a : A} {k : Usize} (arr : Aeneas.Std.Array U64 k) (Nl : U64) + (Lr : List Fl4Spec.Register) (harr : arr.val = Lr.map regIdx) (hN : Nl.val = Lr.length) (hLr : Lr.length ≤ 18) + {src dest : U64} (hs : src ∈ Z.T) (hd : dest ∈ Z.T) + (body : S → U64 → Result (ControlFlow (S × U64) S)) + (hbody : ∀ s i, body s i = if i < Nl then (do + let i1 ← lift (UScalar.cast .Usize i) + let r ← Aeneas.Std.Array.index_usize arr i1 + let v ← obj.Tcb.reg st src s r + let i2 ← regs.sanitise r v + let s1 ← obj.Tcb.set_reg st dest s r i2 + let i3 ← i + 1#u64 + ok (ControlFlow.cont (s1, i3))) + else ok (ControlFlow.done s)) + {s0 : S} {σ0 : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s0 a σ0) : + loop (fun (x : S × U64) => body x.1 x.2) (s0, 0#u64) ⦃ s' => ∃ σ', + Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) Lr σ0 = ({((), σ')}, False) ∧ + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D s' a σ' ∧ NatFoot (L := L) Z s0 σ0 s' σ' ⦄ := by + apply loop.spec_decr_nat (measure := fun x => Lr.length - x.2.val) + (inv := fun x => x.2.val ≤ Lr.length ∧ ∃ σ', Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) (Lr.take x.2.val) σ0 = + ({((), σ')}, False) ∧ ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D x.1 a σ' ∧ + NatFoot (L := L) Z s0 σ0 x.1 σ') + · rintro ⟨s', i⟩ ⟨hi, σ', hm, V', J', F'⟩ + simp only at hi hm J' ⊢ + have hX' := J'.xtop.irelK J'.ks J'.buf + rw [hbody] + by_cases h1 : i < Nl + swap + · rw [if_neg h1, WP.spec_ok] + have : i.val = Lr.length := by have := (P1.gt_iff_val).not.1 h1; omega + rw [this, List.take_length] at hm + exact ⟨σ', hm, V', J', F'⟩ + rw [if_pos h1] + have hiL : i.val < Lr.length := by have := (P1.gt_iff_val).1 h1; omega + simp only [lift, bind_ok] + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ (by + rw [cast_to_us' i (by omega)]; simp [harr]; omega)) ?_ + rintro rr hrr + have hrv : rr = regIdx Lr[i.val] := by + rw [hrr, List.getElem_of_eq harr, List.getElem_map] + congr 1 + simp [cast_to_us' i (by omega)] + subst hrv + obtain ⟨xs, hks, hTs⟩ := hX'.1.tcb src hs + obtain ⟨xd, hkd, -⟩ := hX'.1.tcb dest hd + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' src _) ?_ + rintro v rfl + obtain ⟨w, hw, hwv⟩ := sanitise_eq Lr[i.val] (L.mem s' (regA src (regIdx Lr[i.val]))) + rw [hw, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s' dest _ w) ?_ + rintro s2 rfl + obtain ⟨i3, h3e, h3v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h3e, bind_ok, WP.spec_ok] + have hi3 : i3.val = i.val + 1 := by simpa using h3v + obtain ⟨V2, J2, F2⟩ := J'.regWrJ hd hkd Lr[i.val] w (reg_any_wr_irel J'.ip hX' hd hkd Lr[i.val] w) + refine ⟨⟨by show i3.val ≤ Lr.length; omega, _, ?_, V2, J2, J.natTrans F' F2.cast⟩, + by show Lr.length - i3.val < Lr.length - i.val; omega⟩ + rw [hi3, List.take_succ, List.getElem?_eq_getElem hiL] + simp only [Option.toList_some] + rw [mapMx_snoc, nd_bind_one hm] + unfold crb + simp only [nd_assoc_f] + rw [nd_bind_one (asUser_getRegister_eq hks _), ← hTs.regs, ← hwv, nd_bind_one (asUser_setRegister_eq _ _ hkd)] + rfl + · exact ⟨by simp, σ0, by simp; rfl, Z.vs, by simpa using J, NatFoot.refl _ _ _⟩ + +set_option maxHeartbeats 8000000 in +/-- **The tail of `copy_registers` at `J`** (as `cr_tailA`): +`post_modify_registers` (`JAtZ.regWrJ`), the reschedule of the current +thread (`JAtZ.reschedJ`). -/ +theorem JAtZB.crTailF {a : A} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {dest : U64} (hd : dest ∈ Z.T) : + (do + let s6 ← tcb.post_modify_registers st s dest + let t ← sched.cur_thread st s6 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq dest t + if b then do + let s7 ← sched.reschedule_required st s6 + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s7, a) + else ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s6, a)) ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σ', (do + let cur ← Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread + Fl4Spec.archPostModifyRegisters cur dest.bv + Fl4Spec.NonDet.when (dest.bv = cur) Fl4Spec.rescheduleRequired) σ = ({((), σ')}, False) ∧ + JAtVF (L := L) arI AR Z H Cs s σ r.2.1 a σ' ⦄ := by + have hX := J.xtop.irelK J.ks J.buf + obtain ⟨x, hk, -⟩ := hX.1.tcb dest hd + have hpm : tcb.post_modify_registers st s dest ⦃ s4 => ∃ ct : U64, σ.curThread = ct.bv ∧ ∃ σ4, + Fl4Spec.archPostModifyRegisters (Z := Unit) ct.bv dest.bv σ = ({((), σ4)}, False) ∧ + σ4.curThread = ct.bv ∧ ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D s4 a σ4 ∧ + NatFoot (L := L) Z s σ s4 σ4 ⦄ := by + unfold tcb.post_modify_registers + refine WP.spec_bind (cur_thread_bv hX.2.2.1) ?_ + rintro ct hct + rw [tcb_ne_eq, bind_ok] + by_cases hpc' : dest = ct + · subst hpc' + simp only [ne_eq, not_true_eq_false, decide_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨dest, hct.symm, σ, ?_, hct.symm, Z.vs, by simpa using J, NatFoot.refl _ _ _⟩ + unfold Fl4Spec.archPostModifyRegisters Fl4Spec.NonDet.when + rw [if_neg (by simp)]; rfl + · simp only [ne_eq, hpc', not_false_eq_true, decide_true, ↓reduceIte] + have hE : layout.reg.ERROR = regIdx .errorRegister := by unfold layout.reg.ERROR; rfl + refine WP.spec_mono (Endp.obj.Tcb.set_reg.spec _ dest _ 0#u64) ?_ + rintro s4 rfl + rw [hE] + refine ⟨ct, hct.symm, _, ?_, by simp [Notif.updK, hct], + J.regWrJ hd hk .errorRegister 0#u64 (reg_any_wr_irel J.ip hX hd hk .errorRegister 0#u64)⟩ + unfold Fl4Spec.archPostModifyRegisters Fl4Spec.NonDet.when + rw [if_pos (fun e => hpc' (P1.eq_of_bv e)), asUser_setRegister_eq _ _ hk] + rfl + refine WP.spec_bind hpm ?_ + rintro s4 ⟨ct, hct, σ4, hpm4, hct4, V4, J4, F4⟩ + refine WP.spec_bind (cur_thread_bv (J4.xtop.irelK J4.ks J4.buf).2.2.1) ?_ + rintro ct5 hc5 + obtain rfl : ct = ct5 := (P1.eq_of_bv (hc5.trans hct4)).symm + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp only [bind_ok] + have hfin : ∀ σ6, Fl4Spec.NonDet.when (dest.bv = ct.bv) (Fl4Spec.rescheduleRequired (Z := Unit)) σ4 = + ({((), σ6)}, False) → (do + let cur ← Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread + Fl4Spec.archPostModifyRegisters cur dest.bv + Fl4Spec.NonDet.when (dest.bv = cur) Fl4Spec.rescheduleRequired) σ = ({((), σ6)}, False) := by + intro σ6 h6 + rw [nd_gets_bind, hct, nd_bind_one hpm4, h6] + by_cases hpc' : dest = ct + · subst hpc' + simp only [decide_true, ↓reduceIte] + obtain ⟨s6, hrun, σ6, h6, Ac6, Lst6, D6, J6, F6⟩ := J4.reschedJ + rw [hrun, bind_ok, WP.spec_ok] + exact ⟨rfl, rfl, σ6, hfin σ6 (by simpa [Fl4Spec.NonDet.when] using h6), V4, Ac6, Lst6, D6, J6, J.natTrans F4 F6.cast⟩ + · simp only [hpc', decide_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, trivial, σ4, hfin σ4 (by + unfold Fl4Spec.NonDet.when; rw [if_neg (fun e => hpc' (P1.eq_of_bv e))]; rfl), V4, Ac, Lst, D, J4, F4⟩ + +set_option maxHeartbeats 32000000 in +/-- **`copy_registers` at `J`** (module docstring): `Entered` at the start +(the caller's restart's); the source not idle (`hsI`); the target out of +the dead set (`hdD`, its cap in a slot by `JAtZ.undead`); the platform's +`MIN_BUDGET` [P]. -/ +theorem JAtZB.copyRegsF (hAT : P0.ArchTotal arI) + {a : A} + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {s : S} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hEn : Entered σ) + {src dest : U64} (hsT : src ∈ Z.T) (hdT : dest ∈ Z.T) (hsI : src.bv ≠ σ.idleThread) (hdD : dest ∉ D) + (flags : U64) : + tcb.copy_registers st arI s a dest src flags ⦃ r => r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σ', + specCr dest.bv src.bv (Fl4Spec.testBit flags.bv 0) (Fl4Spec.testBit flags.bv 1) (Fl4Spec.testBit flags.bv 2) + (Fl4Spec.testBit flags.bv 3) σ = ({((), σ')}, False) ∧ JAtVF (L := L) arI AR Z H Cs s σ r.2.1 r.2.2 σ' ⦄ := by + have hns : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u e => by rw [hEn.2] at e; cases e + unfold tcb.copy_registers + simp only [lift, bind_ok] + rw [andk_ne0 flags 1#u64 0 (by decide) (by decide), andk_ne0 flags 2#u64 1 (by decide) (by decide), + andk_ne0 flags 4#u64 2 (by decide) (by decide), andk_ne0 flags 8#u64 3 (by decide) (by decide)] + generalize Fl4Spec.testBit flags.bv 0 = b0 + generalize Fl4Spec.testBit flags.bv 1 = b1 + generalize Fl4Spec.testBit flags.bv 2 = b2 + generalize Fl4Spec.testBit flags.bv 3 = b3 + -- the source's suspend (the dead set kept) + have h1 : (if b0 = true then sched.suspend st arI s a src else ok (s, a)) ⦃ r => r.2 = a ∧ ∃ σ1, + Fl4Spec.NonDet.when (b0 = true) (Fl4Spec.suspend (Z := Unit) src.bv) σ = ({((), σ1)}, False) ∧ + (∀ u, σ1.schedulerAction ≠ .switchThread u) ∧ ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D r.1 r.2 σ1 ∧ NatFoot (L := L) Z s σ r.1 σ1 ⦄ := by + cases b0 + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, σ, rfl, hns, Z.vs, Ac, Lst, by simpa using J, NatFoot.refl _ _ _⟩ + · simp only [↓reduceIte] + refine WP.spec_mono (J.suspendJD (jCancelB_send arI) (jCancelB_ntfn arI) (jCancelB_recv arI) (jCancelB_reply arI) + hsT (fun u e => (hns u e).elim) hsI) ?_ + rintro r ⟨h1, σ1, h2, V, Ac', Lst', J', F'⟩ + exact ⟨h1, σ1, by simpa [Fl4Spec.NonDet.when] using h2, + (Vs.ak_suspend' src.bv).noSwitch (x := ((), σ1)) (by rw [h2]; exact Set.mem_singleton _) hns, + V, Ac', Lst', J', F'⟩ + refine WP.spec_bind h1 ?_ + rintro ⟨s1, a1⟩ ⟨ha1, σ1, hm1, hns1, V1, Ac1, Lst1, J1, F1⟩ + obtain rfl : a = a1 := ha1.symm + -- the target's restart + have h2 : (if b1 = true then sched.restart st arI s1 a dest else ok (s1, a)) ⦃ r => r.2 = a ∧ ∃ σ2, + Fl4Spec.NonDet.when (b1 = true) (Fl4Spec.restart (Z := Unit) dest.bv) σ1 = ({((), σ2)}, False) ∧ + JAtVF (L := L) arI AR Z H Cs s σ r.1 r.2 σ2 ⦄ := by + cases b1 + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, σ1, rfl, V1, Ac1, Lst1, D, J1, F1⟩ + · simp only [↓reduceIte] + have hrs : sched.restart st arI s1 a dest ⦃ r => r.2 = a ∧ ∃ σ', + Fl4Spec.restart (Z := Unit) dest.bv σ1 = ({((), σ')}, False) ∧ JAtVF (L := L) arI AR Z H Cs s σ r.1 r.2 σ' ⦄ := + WP.spec_mono (J1.restartTcbN (Z := Z.mv Z.G V1) hAT hmb hdT hdD hns1) + fun _ ⟨h1, σ2, h2, V, Ac', Lst', D', J2, F2⟩ => ⟨h1, σ2, h2, V, Ac', Lst', D', J2, J.natTrans F1 F2.cast⟩ + refine WP.spec_mono hrs ?_ + rintro r ⟨h1, σ2, h2, hJ⟩ + exact ⟨h1, σ2, by simpa [Fl4Spec.NonDet.when] using h2, hJ⟩ + refine WP.spec_bind h2 ?_ + rintro ⟨s2, a2⟩ ⟨ha2, σ2, hm2, V2, Ac2, Lst2, D2, J2, F2⟩ + obtain rfl : a = a2 := ha2.symm + -- the register copies and the tail + have hfr := frame_regs_val + have hgr := gp_regs_val + have hgp : ∀ (lp : S → Result S) (s3 : S) (σ3 : Fl4Spec.State Unit), JAtVF (L := L) arI AR Z H Cs s σ s3 a σ3 → + (∀ s' σ', JAtVF (L := L) arI AR Z H Cs s σ s' a σ' → lp s' ⦃ s'' => ∃ σ'', + Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) Fl4Spec.gpRegisters σ' = ({((), σ'')}, False) ∧ + JAtVF (L := L) arI AR Z H Cs s σ s'' a σ'' ⦄) → + (if b3 = true then do + let s5 ← lp s3 + let s6 ← tcb.post_modify_registers st s5 dest + let t ← sched.cur_thread st s6 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq dest t + if b then do + let s7 ← sched.reschedule_required st s6 + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s7, a) + else ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s6, a) + else do + let s6 ← tcb.post_modify_registers st s3 dest + let t ← sched.cur_thread st s6 + let b ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq dest t + if b then do + let s7 ← sched.reschedule_required st s6 + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s7, a) + else ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s6, a)) ⦃ r => + r.1 = .Ok () ∧ r.2.2 = a ∧ ∃ σ', (do + Fl4Spec.NonDet.when (b3 = true) (Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) Fl4Spec.gpRegisters) + let cur ← Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread + Fl4Spec.archPostModifyRegisters cur dest.bv + Fl4Spec.NonDet.when (dest.bv = cur) Fl4Spec.rescheduleRequired) σ3 = ({((), σ')}, False) ∧ + JAtVF (L := L) arI AR Z H Cs s σ r.2.1 r.2.2 σ' ⦄ := by + intro lp s3 σ3 hJ3 hlp + cases b3 + · simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨V3, Ac3, Lst3, D3, J3, F3⟩ := hJ3 + refine WP.spec_mono (J3.crTailF hdT) ?_ + rintro r ⟨h1, h2, σ', h3, Vt, Act, Lstt, Dt, Jt, Ft⟩ + refine ⟨h1, h2, σ', ?_, by rw [h2]; exact ⟨Vt, Act, Lstt, Dt, Jt, J.natTrans F3 Ft.cast⟩⟩ + rw [nd_bind_one (show Fl4Spec.NonDet.when (false = true) _ σ3 = ({((), σ3)}, False) from rfl)] + exact h3 + · simp only [↓reduceIte] + refine WP.spec_bind (hlp s3 σ3 hJ3) ?_ + rintro s5 ⟨σ5, hm5, V5, Ac5, Lst5, D5, J5, F5⟩ + refine WP.spec_mono (J5.crTailF hdT) ?_ + rintro r ⟨h1, h2, σ', h3, Vt, Act, Lstt, Dt, Jt, Ft⟩ + refine ⟨h1, h2, σ', ?_, by rw [h2]; exact ⟨Vt, Act, Lstt, Dt, Jt, J.natTrans F5 Ft.cast⟩⟩ + rw [nd_bind_one (show Fl4Spec.NonDet.when (true = true) _ σ3 = ({((), σ5)}, False) by + unfold Fl4Spec.NonDet.when; rw [if_pos rfl]; exact hm5)] + exact h3 + have hlpg : ∀ (body : S → U64 → Result (ControlFlow (S × U64) S)), (∀ s i, body s i = if i < tcb.N_GP then (do + let i1 ← lift (UScalar.cast .Usize i) + let r ← Aeneas.Std.Array.index_usize regs.GP_REGISTERS i1 + let v ← obj.Tcb.reg st src s r + let i2 ← regs.sanitise r v + let s1 ← obj.Tcb.set_reg st dest s r i2 + let i3 ← i + 1#u64 + ok (ControlFlow.cont (s1, i3))) + else ok (ControlFlow.done s)) → + ∀ s' σ', JAtVF (L := L) arI AR Z H Cs s σ s' a σ' → loop (fun (x : S × U64) => body x.1 x.2) (s', 0#u64) ⦃ s'' => + ∃ σ'', Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) Fl4Spec.gpRegisters σ' = ({((), σ'')}, False) ∧ + JAtVF (L := L) arI AR Z H Cs s σ s'' a σ'' ⦄ := + fun body hbody s' σ' ⟨V', Ac', Lst', D', J', F'⟩ => WP.spec_mono (crB_loop (Z := Z.mv Z.G V') regs.GP_REGISTERS + tcb.N_GP Fl4Spec.gpRegisters hgr (by unfold tcb.N_GP; rfl) (by decide) hsT hdT body hbody J') + fun _ ⟨σ'', hm, V'', J'', F''⟩ => ⟨σ'', hm, V'', Ac', Lst', D', J'', J.natTrans F' F''.cast⟩ + have hspec : ∀ σ3 σ', (Fl4Spec.NonDet.when (b2 = true) (do + Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) Fl4Spec.frame_registers + let pc ← Fl4Spec.asUser (Z := Unit) dest.bv Fl4Spec.getRestartPC + Fl4Spec.asUser dest.bv (Fl4Spec.setNextPC pc))) σ2 = ({((), σ3)}, False) → + (do + Fl4Spec.NonDet.when (b3 = true) (Fl4Spec.NonDet.mapMx (crb src.bv dest.bv) Fl4Spec.gpRegisters) + let cur ← Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread + Fl4Spec.archPostModifyRegisters cur dest.bv + Fl4Spec.NonDet.when (dest.bv = cur) Fl4Spec.rescheduleRequired) σ3 = ({((), σ')}, False) → + specCr dest.bv src.bv b0 b1 b2 b3 σ = ({((), σ')}, False) := by + intro σ3 σ' e3 e4 + unfold specCr + rw [nd_bind_one hm1, nd_bind_one hm2, nd_bind_one (show Fl4Spec.archGetSanitiseRegisterInfo (Z := Unit) dest.bv σ2 = + ({(false, σ2)}, False) from rfl)] + exact (nd_bind_one e3 _).trans e4 + cases b2 + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (hgp (fun s' => tcb.copy_registers_loop2 st s' dest src 0#u64) s2 σ2 + ⟨V2, Ac2, Lst2, D2, J2, F2⟩ + (fun s' σ' hJ' => by unfold tcb.copy_registers_loop2; exact hlpg _ (fun _ _ => rfl) s' σ' hJ')) ?_ + rintro r ⟨h1, h2, σ', h3, h4⟩ + exact ⟨h1, h2, σ', hspec σ2 σ' rfl h3, h4⟩ + · simp only [↓reduceIte] + have hl0 := crB_loop (Z := Z.mv Z.G V2) regs.FRAME_REGISTERS tcb.N_FRAME Fl4Spec.frame_registers hfr + (by unfold tcb.N_FRAME; rfl) (by decide) hsT hdT _ (fun _ _ => rfl) J2 + unfold tcb.copy_registers_loop0 + refine WP.spec_bind hl0 ?_ + rintro s3 ⟨σ3, hm3, V3, J3, F3⟩ + have hX3 := J3.xtop.irelK J3.ks J3.buf + obtain ⟨x3, hk3, hTR3⟩ := hX3.1.tcb dest hdT + have hF : layout.reg.FAULT_IP = regIdx .faultIP := by unfold layout.reg.FAULT_IP; rfl + have hNx : layout.reg.NEXT_IP = regIdx .nextIP := by unfold layout.reg.NEXT_IP; rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s3 dest _) ?_ + rintro pc hpc + have hpcv : pc.bv = x3.tcbArch.tcbContext.userRegs .faultIP := by rw [hpc, hF]; exact hTR3.regs .faultIP + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s3 dest _ pc) ?_ + rintro s4 rfl + rw [hNx] + obtain ⟨V4, J4, F4⟩ := J3.regWrJ hdT hk3 .nextIP pc (reg_any_wr_irel J3.ip hX3 hdT hk3 .nextIP pc) + refine WP.spec_mono (hgp (fun s' => tcb.copy_registers_loop1 st s' dest src 0#u64) _ _ ⟨V4, Ac2, Lst2, D2, J4, J.natTrans F2 (J2.natTrans F3.cast F4.cast).cast⟩ + (fun s' σ' hJ' => by unfold tcb.copy_registers_loop1; exact hlpg _ (fun _ _ => rfl) s' σ' hJ')) ?_ + rintro r ⟨h1, h2, σ', h3, h4⟩ + refine ⟨h1, h2, σ', hspec _ σ' ?_ h3, h4⟩ + unfold Fl4Spec.NonDet.when + rw [if_pos rfl, nd_bind_one hm3, + nd_bind_one (show Fl4Spec.asUser (Z := Unit) dest.bv Fl4Spec.getRestartPC σ3 = + ({(x3.tcbArch.tcbContext.userRegs .faultIP, σ3)}, False) from asUser_getRegister_eq hk3 .faultIP), ← hpcv] + exact asUser_setRegister_eq .nextIP pc.bv hk3 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrCopyTop.lean b/proofs/Fl4Proofs/P3/JAtZBThrCopyTop.lean new file mode 100644 index 000000000..18b89868e --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrCopyTop.lean @@ -0,0 +1,181 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **CopyRegisters**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrCopy +import Fl4Proofs.P3.XTopJointMRTcbL +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **The CopyRegisters label at `JAt`** (as `tcb_copy_regsX`): TcbCtl's +decode with the source the first extra cap's thread, the caller's restart +(`JAtZ.restartJ`, `Entered` after by `restart_mid`), the target out of the +dead set by its cap in the invoked slot (`JAtZ.undead`, the slots kept by +the restart), then XTopJointCopyRegs' `JAtZ.copyRegsJ`. The stalls of the target and the source +are `JAtZ.stallT` (no `hstall'`). Named: the +source not idle (`hidS`), the platform's `MIN_BUDGET` [P]. -/ +theorem JAtZB.tcbCopyRegsF (hAT : P0.ArchTotal arI) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) + (hidS : ∀ (sl' : Fl4Spec.CslotPtr) q, AbsSlot Z.th sl' → capV (L.mem s) (cslotAddr sl') = .Thread q → + q ≠ L.idleW s) + (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbCopyRegisters) + (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) + (call : Bool) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) (hsc : capV (L.mem s) (cslotAddr ssl) = .Thread p) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBCopyRegisters p ⟨length, buf, call, cslotAddr ssl, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + have hcapT : x0 ≠ 0#u64 → ∀ q, capV (L.mem s) x0 = .Thread q → q ∈ Z.T := by + intro hx q hc + have h0 := hXc.2 0 x0 rfl + obtain ⟨e, he⟩ : ∃ e, excaps[0]? = some e := by + rcases excaps with _ | ⟨e, rest⟩ + · exact absurd (h0.1.2 (by simp)) hx + · exact ⟨e, rfl⟩ + obtain ⟨hxe, -, ps, hps, hpe⟩ := h0.2 e he + have hA0 : AbsSlot Z.th e.2 := ⟨ps, hpe, Or.inl hps⟩ + exact J.thT q (show CapValid Z.th (.Thread q) from + J.xtop.rel.2.1.2.1.abs hA0 (by rw [← hxe, capAt_capV, hc])) + obtain ⟨hcur, -⟩ := J.curEq hct + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeCopyRegisters (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) (excaps.map Prod.fst)) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_copy_eq arI s ar p _ (J.stallT p hpT), hdec0] + unfold tcb.decode_copy_registers tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + have htr : args = [] ∨ excaps = [] → + (ok ((.Err (.Sys .TruncatedMessage) : core.result.Result Unit error.Exc), s, ar) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => ThrPost (L := L) arI AR Z H Cs s ar σ + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeCopyRegisters (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) (excaps.map Prod.fst))) + res ⦄ := by + intro h + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + rcases h with rfl | rfl + · exact copy_trunc0 σ _ _ + · cases args with + | nil => exact copy_trunc0 σ _ _ + | cons y0 rest => exact copy_trunc1 σ _ _ _ + by_cases h0 : x0 = 0#u64 + · have hl : excaps.length ≤ 0 := ((hXc.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + exact htr (Or.inr (List.eq_nil_of_length_eq_zero (by omega))) + by_cases hl : length < 1#u64 + · simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, show ¬ (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 1#u64 by + show ¬ length >= 1#u64; scalar_tac] + exact htr (Or.inl (List.eq_nil_of_length_eq_zero (by scalar_tac))) + simp only [h0, ↓reduceIte, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, + show (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 1#u64 by show length >= 1#u64; scalar_tac] + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by + match args, hlen with + | y0 :: rest, _ => exact ⟨_, _, rfl⟩ + | [], h => exfalso; simp only [List.length_nil] at h; scalar_tac + have ha0 : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + rw [ha0]; simp only [bind_ok, thread_arg_eq] + obtain ⟨e0, he0, hxe0, he01, ps0, hps0, hps01⟩ := hXc.get hx0' h0 + obtain ⟨erest, rfl⟩ : ∃ erest, excaps = e0 :: erest := by + match excaps, he0 with + | a :: r, h => simp at h; exact ⟨r, by rw [h]⟩ + have hsp : (Fl4Spec.decodeCopyRegisters (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) + ((e0 :: erest).map Prod.fst)) = + ((match absCap (capV (L.mem s) x0) :: erest.map Prod.fst with + | .threadCap q :: _ => Fl4Spec.NonDetE.returnOk q + | _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1) : Fl4Spec.SEMonad Unit Fl4Spec.ObjRef) >>= fun q => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.copyRegisters p.bv q (Fl4Spec.testBit y0.bv 0) + (Fl4Spec.testBit y0.bv 1) (Fl4Spec.testBit y0.bv 2) (Fl4Spec.testBit y0.bv 3) + Fl4Spec.ArchDefaultExtraRegisters)) := by + rw [List.map_cons, List.map_cons, copy_spec, he01] + rfl + rw [hsp] + rcases copy_src_rel (capV (L.mem s) x0) (erest.map Prod.fst) (σ := σ) with ⟨src, hsrc, hrun⟩ | ⟨hsrc, hrun⟩ + · rw [hsrc] + have hthr0 : capV (L.mem s) x0 = .Thread src := by + revert hsrc; cases capV (L.mem s) x0 <;> simp [threadArgF] + simp only [J.stallT src (hcapT h0 src hthr0), bind_ok] + obtain ⟨s1, σ1, x, hrc1, hst, hk, -, he1, -, hid1, hct1, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + obtain ⟨-, x', hk', hrun'⟩ := J.curRunnable hni hct + rw [hk] at hk'; cases hk' + refine let_pair2 s ar (fun s1 a1 => do let s2 ← tcb.restart_caller st s1; tcb.copy_registers st arI s2 a1 p src y0) + _ ?_ + rw [hrc1]; simp only [bind_ok] + have hthr : capV (L.mem s) x0 = .Thread src := by + revert hsrc; cases capV (L.mem s) x0 <;> simp [threadArgF] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + ((match absCap (capV (L.mem s) x0) :: erest.map Prod.fst with + | .threadCap q :: _ => Fl4Spec.NonDetE.returnOk q + | _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1) : Fl4Spec.SEMonad Unit Fl4Spec.ObjRef) >>= + fun q => Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.copyRegisters p.bv q (Fl4Spec.testBit y0.bv 0) + (Fl4Spec.testBit y0.bv 1) (Fl4Spec.testBit y0.bv 2) (Fl4Spec.testBit y0.bv 3) + Fl4Spec.ArchDefaultExtraRegisters))).run σ = + ({(.ok (.invokeTCB (.copyRegisters p.bv src.bv (Fl4Spec.testBit y0.bv 0) (Fl4Spec.testBit y0.bv 1) + (Fl4Spec.testBit y0.bv 2) (Fl4Spec.testBit y0.bv 3) Fl4Spec.ArchDefaultExtraRegisters)), σ)}, False) := by + apply run_liftME_ok + exact (run_bind_ok hrun _).trans rfl + have hA0 : AbsSlot Z.th e0.2 := ⟨ps0, hps01, Or.inl hps0⟩ + have hxs : IsSlot Z.th x0 := by rw [hxe0]; exact hA0.isSlot + have hsrcT : src ∈ Z.T := J.thT src (show CapValid Z.th (.Thread src) from + J.xtop.rel.2.1.2.1.abs hA0 (by rw [← hxe0, capAt_capV, hthr])) + have hsI : src.bv ≠ σ1.idleThread := by rw [hid1]; exact J.ne_idle (hidS e0.2 src hA0 (by rw [← hxe0]; exact hthr)) + -- the slots kept by the caller's restart: the target's cap, so the target out of the dead set + have hSS : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := XTopAt.restart J.xtop hcur hk hrun' + rw [hs1, WP.spec_ok] at h; exact h.2.1 + have hcap1 : capAt (L.mem s1) (cslotAddr ssl) = ok (.Thread p) := by + rw [capAt_capV, hSS.capV hAs.isSlot, hsc] + have hC := J.curInv (by rw [hni.2]; simp) + have hcs : Fl4Spec.schedulable σ.curThread σ = true := ct_schedulable hC J.act J.inv (hcur ▸ hk) hrun' + obtain ⟨hres1, -, -, -, -⟩ := restart_mid hcur hk (hcur ▸ hcs) hni.2 hC J.off J.act J.inv + have hEn1 : Entered σ1 := ⟨by rw [hct1, hid1]; exact hni.1, by rw [he1]; exact hres1⟩ + refine WP.spec_mono ((J1.undead hAs.isSlot hcap1 hpT).copyRegsF hAT (hmb ar) hEn1 hsrcT hpT hsI (fun h => h.2 rfl) y0) ?_ + rintro ⟨r2, s2, a2⟩ ⟨hr2, -, σ', hsp, V', Ac', Lst', D', J2, F2⟩ + simp only at hr2; subst hr2 + have hz : ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB (.copyRegisters p.bv src.bv + (Fl4Spec.testBit y0.bv 0) (Fl4Spec.testBit y0.bv 1) (Fl4Spec.testBit y0.bv 2) (Fl4Spec.testBit y0.bv 3) + Fl4Spec.ArchDefaultExtraRegisters))).run σ1).1 := by + show _ ∈ ((Fl4Spec.invokeTcb (Z := Unit) _).run σ1).1 + rw [invokeTcb_cr] + exact mem_liftE (x := ([], σ')) (mem_bind_nd (by rw [hsp]; exact Set.mem_singleton _) (Set.mem_singleton _)) + exact Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst, _, hz, V', Ac', Lst', D', J2, J.natTrans F1 F2.cast⟩ + · rw [hsrc] + simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩) + exact run_liftME_err (run_bind_error hrun _) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrExtAux.lean b/proofs/Fl4Proofs/P3/JAtZBThrExtAux.lean new file mode 100644 index 000000000..0a8ff47ae --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrExtAux.lean @@ -0,0 +1,318 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **helper steps of the TCB extension labels over `JAtZB`, with +`NatFoot`**: the flags across the caller's restart, the FPU release with the reply-tail facts, a debug word +(`dbgWrF`, footprint by `JAtZB.tcbWordN`), `configure_single_stepping` (`cssF`) and the one-word reply tail +(`replyWords1F`). `JAtZB` siblings of XTopJointTcb's `JAtZ` lemmas, generated by `handoff/tools/gen_thrext.py`. +No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPostF +import Fl4Proofs.P3.JAtZBThrSched +import Fl4Proofs.P3.XTopJointFpu +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`FlagsRel` across the caller's restart**: `set_thread_state ct Restart` +writes the caller's state word (offset 3328) and maybe the current core's +scheduler-action word (outside the objects, `JKern.act`), no flags word +(offset 3456 of a TCB of `T`, the TCBs disjoint); the spec changes the +caller's state only. -/ +theorem JAtZB.flagsRestart {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hFl : FlagsRel Z.T (L.mem s) σ) + {ct : U64} (hth : Z.th ct = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) + {s1 : S} (h1 : sched.set_thread_state st s ct sched.TS_RESTART = ok s1) : + FlagsRel Z.T (L.mem s1) (Notif.stsAct (Notif.updK σ ct.bv (.tcb { x with tcbState := .restart })) ct.bv) := by + have hc := set_thread_state_cases (st := st) s ct sched.TS_RESTART + rw [h1, WP.spec_ok] at hc + have hPl := J.xtop.rel.1 + have hctT := J.thT ct hth + have hta : P1.TcbAddr ct := J.ip.e.tcb ct hctT + have hstv : (stA ct).val = ct.val + 3328 := stA_val hPl hth + have hal_st : P1.Aligned (stA ct) := by unfold P1.Aligned; rw [hstv]; have := hta.align; omega + have hact := J.kern.act + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + refine FlagsRel.frame hFl (fun t ht => ?_) (fun q y hq => ?_) + · have htt : P1.TcbAddr t := J.ip.e.tcb t ht + have htth : Z.th t = some .tcb := (hJ.tcb t).2 (J.zH.1 t ht) + have hfv : (tFlagsWA t).val = t.val + 3456 := Notif.tfield_val htt (by decide) + have hfa : P1.Aligned (tFlagsWA t) := P1.field_aligned htt (by decide) (by decide) + have n1 : (tFlagsWA t).val ≠ (stA ct).val := by + rw [hfv, hstv] + by_cases e : t = ct + · subst e; omega + · have := hPl.disj t ct .tcb .tcb htth hth e + simp only [objSize] at this; omega + have n2 : (tFlagsWA t).val ≠ (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val := by + intro e + exact hact.2 t .tcb htth ⟨by omega, by simp only [objSize]; omega⟩ + rcases hc with rfl | rfl + · exact L.mem_wr_ne hal_st hfa (Ne.symm n1) + · rw [L.mem_wr_ne hact.1 hfa (Ne.symm n2)]; exact L.mem_wr_ne hal_st hfa (Ne.symm n1) + · rw [Notif.stsAct_kheap] + by_cases e : q = ct.bv + · subst e; rw [hk] at hq; cases hq + exact ⟨{ x with tcbState := .restart }, by simp [Notif.updK], rfl⟩ + · exact ⟨y, by simp [Notif.updK, e, hq], rfl⟩ + +set_option maxHeartbeats 8000000 in +/-- **`JAtZ.fpuReleaseJ` with the facts the reply tail reads**: the current +thread and every TCB's state kept by the spec's outcome. -/ +theorem JAtZB.fpuReleaseF' (hK : ARKeep arI AR) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : t ∈ Z.T) : + arI.fpu_release a t ⦃ a' => ∃ σ', ((), σ') ∈ (Fl4Spec.fpuRelease (Z := Unit) t.bv σ).1 ∧ + σ'.curThread = σ.curThread ∧ + (∀ q y, σ.kheap q = some (.tcb y) → ∃ y', σ'.kheap q = some (.tcb y') ∧ y'.tcbState = y.tcbState) ∧ + ∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D s a' σ' ∧ NatFoot (L := L) Z s σ s σ' ⦄ := by + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + refine WP.spec_mono (hK.fpu_release a t _ hAR) ?_ + intro a' hAR' + obtain ⟨Q, R0, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨x, hk, -⟩ := hs.tcb t ht + obtain ⟨ta, ms, o, hr, hum, hm⟩ := Sched.fpuRelease_outF hk + obtain ⟨V1, J1, F1⟩ := J.tcbArchJ ht hk ta hr + refine ⟨_, hm, rfl, fun q y hq => ?_, V1, J1.arFrS (arFr_fpuOwner _ ms o) (J1.xtop.fpuOwner ms o hum hAR'), + J.natTrans F1 (NatFoot.ofK rfl (NatK.of_kheap rfl))⟩ + by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq; exact ⟨{ x with tcbArch := ta }, by simp [updK], rfl⟩ + · exact ⟨y, by simp [updK, e, hq], rfl⟩ + +open Fl4Proofs.P1 in +set_option maxHeartbeats 8000000 in +/-- **`JAtZ` across a register step with debug words** (XTopJointRegFrame's +`JAtZ.reg_frameF`, the write set widened by the TCBs' debug words, offsets +448..512: TCB words outside every relation, as the register words). -/ +theorem JAtZB.reg_frameG (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} + (hI3 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hw : P1.WritesIn st s s' (fun b => RegFW Z.T Z.C b ∨ ∃ y ∈ Z.T, + y.val + 448 ≤ b.val ∧ b.val < y.val + 512)) (hA : QKY σ σ') (hyk : YKeep σ σ') (hInv : Inv σ') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) ∅ s)) : + ∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst D s' a σ' := by + have hI := J.xtop.irelK J.ks J.buf + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => by + rcases hb with ((hb | ⟨q, hq, rfl⟩) | ⟨y, hy, hb⟩) | ⟨y, hy, h1, h2⟩ + · obtain ⟨y, hy, h1, h2⟩ := hb + exact Or.inr (Or.inr (Or.inl ⟨y, hy, h1, by omega⟩)) + · obtain ⟨x0, n0, hk0, hx0⟩ := hs.scs q hq + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl + ⟨q, hq, x0, n0, hk0, hx0.in_obj (hx0.hdr (K := 8#u64) (by simp) (by simp)).2⟩)))))) + · exact Or.inr (Or.inr (Or.inl ⟨y, hy, by omega, by omega⟩)) + · exact Or.inr (Or.inr (Or.inl ⟨y, hy, by omega, by omega⟩)) + have hwF : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := hw.mono fun b hb => by + rcases hb with ((⟨y, hy, k1, k2⟩ | ⟨q, hq, rfl⟩) | ⟨y, hy, hb⟩) | ⟨y, hy, k1, k2⟩ + · exact Or.inr ⟨y, hy, b.val - y.val, by omega, by omega, by omega, by omega, by omega, by omega⟩ + · exact Or.inl (KFoot.of_sw (sw_hdr hS hq (K := 8#u64) (by simp) (by simp) (by decide))) + · exact Or.inr ⟨y, hy, 3376, by omega, by omega, by omega, by omega, by omega, hb⟩ + · exact Or.inr ⟨y, hy, b.val - y.val, by omega, by omega, by omega, by omega, by omega, by omega⟩ + have hck : Fl4Proofs.P3.CKeep σ σ' := hA.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.finW_keeps hwF + have ck := hA.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hA.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hI3.2.2.1 + have hAI3 : ActIdle σ' := hA.actIdle J.act + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_qk hA hS3 hI3.1 hwF k3 hAI3.1 (J.yld.frame k2 hyk) hyk + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hA.szKeep q.bv y n hq) hks (J.finW_irq hwF) + (J.idleW_keep hS3 hA.idle hA.isc).2⟩ + have hkern := J.kern_keep hks hwF.2.1 hS3 hA.ct hA.csc (fun sl h => Or.inl (by rw [← hA.orig]; exact h)) + (by rw [hA.arch]) + (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hDbg + (D' := D) + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac' Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 hyk, J.mcp.frame k1 hA.mcpKeep, J.buf.frame kb hA.bufKeep, hRP', + J.apart.sc_frame hks hA.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hA.idle hA.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hwF fun _ hb => Ipc.FinW.ntd J.xtop.fin.1.1.1 hb)), hkern, + J.finW_utc hwF hF.1 hA.cdt hA.orig hA.arch hDbg, hA.nsc J.nsc, hInv, hA.cur J.cur, hA.own J.own, hA.off J.off, + hAI3, hA.bsa J.bsa, hA.isorted J.isorted, hA.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameB J hF.1 hA.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact ⟨Z.vs, J.cs_frameI hks hX3 hwF (fun b hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) hA.arch (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hA.szKeep hF.1 hA.kback hA.ipcFr hC R hKd hck.orig (J.idleW_keep hS3 hA.idle hA.isc).1 J.zD⟩ + +open Fl4Proofs.P1 in +set_option maxHeartbeats 8000000 in +/-- **A debug word of a TCB written at `J`** (`JAtZ.reg_frameG` with the +spec state kept: `XTopAt.tcbWrA`, `IRel` by `XTopAt.irelK`, P1's `BlkOK` by +`blk_rf`, its footprint by `foot_field`). -/ +theorem JAtZB.dbgWrJB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (wa : U64) + (Kv : Nat) (hK : 448 ≤ Kv ∧ Kv < 512 ∧ Kv % 8 = 0) (hwav : wa.val = t.val + Kv) (v : U64) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s wa v) a σ := by + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hta := hT t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hT + set K : U64 := ⟨BitVec.ofNat 64 Kv⟩ with hKdef + have hKv : K.val = Kv := by + show (BitVec.ofNat 64 Kv).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hKt : (core.num.U64.wrapping_add t K).val = t.val + K.val := tfield_val hta (by omega) + have hreq : wa = core.num.U64.wrapping_add t K := UScalar.eq_of_val_eq (by rw [hwav, hKt, hKv]) + subst hreq + have hwa : P1.Aligned (core.num.U64.wrapping_add t K) := P1.field_aligned hta (by omega) (by omega) + have hw : P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add t K) v) (· = core.num.U64.wrapping_add t K) := + P1.writesIn_wr hwa + have hblk := (P1.blk_rf J.blk (hw.mono fun b hb => Or.inl ⟨t, htH, K, by omega, by omega, hb⟩)).1 + have hfoot : P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add t K) v) (P1.KW st (P1.Heap.tcbs H) Cs + (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s) := + P1.foot_field hTH htH (by omega) (by omega) (by omega) ⟨by omega, by omega⟩ + have hX' := J.xtop.tcbWrA ht _ Kv (Or.inr (Or.inr (Or.inl hK))) (by rw [hKt, hKv]) v + have hks : L.ksb (L.wr s (core.num.U64.wrapping_add t K) v) = L.ksb s := hw.1 + have hwF : P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add t K) v) (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := + hw.mono fun b hb => Or.inr ⟨t, ht, Kv, by omega, by omega, by omega, by omega, by omega, by rw [hb, hKt, hKv]⟩ + obtain ⟨-, -, kb, -, -⟩ := J.finW_keeps hwF + have hA : QKY σ σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun p => Or.inl rfl⟩ + have hBW : BufWordRel Z.T (L.mem (L.wr s (core.num.U64.wrapping_add t K) v)) σ := J.buf.frame kb hA.bufKeep + have hI3 := hX'.irelK (by rw [hks]; exact J.ks) hBW + exact J.reg_frameG hI3 (hw.mono fun b hb => Or.inr ⟨t, ht, by rw [hb, hKt, hKv]; omega, + by rw [hb, hKt, hKv]; omega⟩) hA (YKeep.of_kheap rfl) J.inv hblk hfoot + +/-- **A debug word at `J`, with the native footprint** (`JAtZB.dbgWrJB`, `JAtZB.tcbWordN`). -/ +theorem JAtZB.dbgWrF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (wa : U64) + (Kv : Nat) (hK : 448 ≤ Kv ∧ Kv < 512 ∧ Kv % 8 = 0) (hwav : wa.val = t.val + Kv) (v : U64) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s wa v) a σ ∧ + NatFoot (L := L) Z s σ (L.wr s wa v) σ := + let ⟨V, B'⟩ := J.dbgWrJB ht wa Kv hK hwav v + ⟨V, B', J.tcbWordN ht (by omega) (by omega) hK.2.2 (by omega) hwav v⟩ + +set_option maxHeartbeats 8000000 in +/-- **FL4's `configure_single_stepping` at `J`** (as `css_xtop`): the debug +words `SS_ENABLED`, `SS_COUNT` (`JAtZ.dbgWrJ`); with no instruction count, the +target's `FLAGS` register (`JAtZ.regWrJ`), against the spec's `cssState`. -/ +theorem JAtZB.cssF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p : U64} (hpT : p ∈ Z.T) + {xp : Fl4Spec.Tcb} (hkp : σ.kheap p.bv = some (.tcb xp)) (bp n : U64) : + debug.configure_single_stepping st arI s a p bp n false ⦃ r => r.1 = false ∧ r.2.2 = a ∧ + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D r.2.1 a (cssState σ p.bv xp n.bv) ∧ + NatFoot (L := L) Z s σ r.2.1 (cssState σ p.bv xp n.bv) ⦄ := by + have hpf := (J.ip.e.tcb p hpT).fits + unfold debug.configure_single_stepping + by_cases hn : n = 0#u64 + · subst hn + simp only [↓reduceIte, set_debug_reg_wr, bind_ok] + obtain ⟨V2, J2, F2⟩ := J.dbgWrF hpT (dbgA p debug.SS_ENABLED) (448 + 48) (by omega) + (by rw [dbgA_val (by omega) (by unfold debug.SS_ENABLED; decide)]; unfold debug.SS_ENABLED; simp) 0#u64 + simp only [Std.bind_assoc] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec _ p layout.reg.FLAGS) ?_ + rintro i rfl + rw [eflags_tf_eq]; simp only [bind_ok, lift] + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec _ p layout.reg.FLAGS _) ?_ + rintro s3 rfl + simp only [set_debug_reg_wr, bind_ok, WP.spec_ok] + set s2 := L.wr s (dbgA p debug.SS_ENABLED) 0#u64 + obtain ⟨x2, hk2, hx2⟩ := J2.xtop.fin.1.1.2.1.tcb p hpT + rw [hkp] at hk2; cases hk2 + have hXr := reg_wr_irel J2.ip (J2.xtop.irelK J2.ks J2.buf) hpT hkp Fl4Spec.Register.flags (by decide) + (L.mem s2 (regA p layout.reg.FLAGS) &&& ~~~256#u64) + obtain ⟨V3, J3, F3⟩ := J2.regWrJ hpT hkp Fl4Spec.Register.flags + (L.mem s2 (regA p layout.reg.FLAGS) &&& ~~~256#u64) hXr + have F23 := J.natTrans F2 (J2.natTrans F3.cast (NatFoot.refl _ _ _)).cast + rw [regIdx_flags] at J3 F23 + obtain ⟨V4, J4, F4⟩ := J3.dbgWrF hpT (dbgA p debug.SS_COUNT) (448 + 56) (by omega) + (by rw [dbgA_val (by omega) (by unfold debug.SS_COUNT; decide)]; unfold debug.SS_COUNT; simp) 0#u64 + have hflag : (L.mem s2 (regA p layout.reg.FLAGS) &&& ~~~256#u64).bv = + xp.tcbArch.tcbContext.userRegs .flags &&& tfMask := by + rw [UScalar.bv_and, ← regIdx_flags] + exact congrArg₂ (· &&& ·) (hx2.regs .flags) (by rfl) + have e : cssState σ p.bv xp (0#u64).bv = + Notif.updK σ p.bv (.tcb (Notif.setReg xp Fl4Spec.Register.flags + (L.mem s2 (regA p layout.reg.FLAGS) &&& ~~~256#u64).bv)) := by + unfold cssState; rw [if_pos (show U64.bv 0#u64 = (0 : Fl4Spec.Word) from rfl), hflag] + refine ⟨by first | rfl | trivial, by first | rfl | trivial, V4, ?_⟩ + rw [e]; exact ⟨J4, J.natTrans F23 F4.cast⟩ + · simp only [hn, ↓reduceIte, set_debug_reg_wr, bind_ok, WP.spec_ok] + obtain ⟨V2, J2, F2⟩ := J.dbgWrF hpT (dbgA p debug.SS_ENABLED) (448 + 48) (by omega) + (by rw [dbgA_val (by omega) (by unfold debug.SS_ENABLED; decide)]; unfold debug.SS_ENABLED; simp) 1#u64 + obtain ⟨V3, J3, F3⟩ := J2.dbgWrF hpT (dbgA p debug.SS_COUNT) (448 + 56) (by omega) + (by rw [dbgA_val (by omega) (by unfold debug.SS_COUNT; decide)]; unfold debug.SS_COUNT; simp) n + have e : cssState σ p.bv xp n.bv = σ := by + unfold cssState; rw [if_neg (fun h => hn (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat h; simpa using this)))] + refine ⟨by first | rfl | trivial, by first | rfl | trivial, V3, ?_⟩ + rw [e]; exact ⟨J3, J.natTrans F2 F3.cast⟩ + +set_option maxHeartbeats 8000000 in +/-- **The single-word reply tail at `J`** (ConfigureSingleStepping's, called): +FL4's `MR0`, then `reply_words` (badge, message info), then `Running` +(`JAtZ.regWrJ` three times, `JAtZ.runJ`); the spec's `reply_from_kernel` in +its own register order (`setReg_comm`), so the spec's success tail. -/ +theorem JAtZB.replyWords1F (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] {ct : U64} (hct : ct ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) (hrs : x.tcbState = .restart) (v : U64) : + (do + let s4 ← tcb.reply_words st (L.wr s (Endp.regA ct (Endp.regIdx (Endp.mrIdx 0))) v) ct 1#u64 + let s5 ← sched.set_thread_state st s4 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a)) ⦃ res => + ∀ z ∈ (specTail true ct.bv [v.bv] σ).1, ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 z.2 ∧ + NatFoot (L := L) Z s σ res.2.1 z.2 ⦄ := by + set s1 := L.wr s (Endp.regA ct (Endp.regIdx (Endp.mrIdx 0))) v + have hRW : tcb.reply_words st s1 ct 1#u64 ⦃ s3 => ∃ w : U64, + s3 = L.wr (L.wr s1 (Endp.regA ct (Endp.regIdx Fl4Spec.badge_register)) 0#u64) + (Endp.regA ct (Endp.regIdx Fl4Spec.msg_info_register)) w ∧ + w.bv = Fl4Spec.messageInfoToData ⟨Fl4Spec.natToLen 1, 0, 0, 0⟩ ⦄ := by + unfold tcb.reply_words + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s1 ct layout.reg.BADGE 0#u64) ?_ + rintro s2 rfl + unfold msginfo.MessageInfo.new + simp only [bind_ok] + refine WP.spec_mono (Endp.set_msg_info_spec _ ct 0#u64 1#u64 (by decide)) ?_ + rintro s3 ⟨w, rfl, hw⟩ + refine ⟨w, ?_, by rw [hw]; exact msg_info_one_bv⟩ + rw [Endp.regIdx_badge, Endp.regIdx_msgInfo] + refine WP.spec_bind hRW ?_ + rintro s3 ⟨w, rfl, hw⟩ + -- MR0 + have hX1 := reg_wr_irel J.ip (J.xtop.irelK J.ks J.buf) hct hk (Endp.mrIdx 0) (by decide) v + obtain ⟨V1, J1, F1⟩ := J.regWrJ hct hk (Endp.mrIdx 0) v hX1 + set x1 := setReg x (Endp.mrIdx 0) v.bv with hx1 + have hk1 : (updK σ ct.bv (.tcb x1)).kheap ct.bv = some (.tcb x1) := by simp [updK] + -- the badge + have hXb := badge_wr_irel J1.ip (J1.xtop.irelK J1.ks J1.buf) hct hk1 0#u64 + rw [badgeRegA_eq_regA] at hXb + obtain ⟨V2, J2, F2⟩ := J1.regWrJ hct hk1 Fl4Spec.badge_register 0#u64 hXb + set x2 := setReg x1 Fl4Spec.badge_register (0#u64 : U64).bv with hx2 + have hk2 : (updK (updK σ ct.bv (.tcb x1)) ct.bv (.tcb x2)).kheap ct.bv = some (.tcb x2) := by simp [updK] + -- the message info + have hX3 := reg_wr_irel J2.ip (J2.xtop.irelK J2.ks J2.buf) hct hk2 Fl4Spec.msg_info_register (by decide) w + obtain ⟨V3, J3, F3⟩ := J2.regWrJ hct hk2 Fl4Spec.msg_info_register w hX3 + have F13 := J.natTrans F1 (J1.natTrans F2.cast F3.cast).cast + have hxC : setReg x2 Fl4Spec.msg_info_register w.bv = Endp.replyTcb x v.bv := by + rw [hw, hx2, hx1, setReg_comm x (show Endp.mrIdx 0 ≠ Fl4Spec.badge_register by decide)] + rfl + rw [updK_updK, updK_updK, hxC] at J3 F13 + have hkC : (updK σ ct.bv (.tcb (Endp.replyTcb x v.bv))).kheap ct.bv = some (.tcb (Endp.replyTcb x v.bv)) := by + simp [updK] + refine WP.spec_bind (J3.runJ hct hkC hrs) ?_ + rintro s4 ⟨Lst', J4, F4⟩ + refine (WP.spec_ok _).2 fun z hz => ?_ + rw [specTail_restart hk hrs true v.bv] at hz + simp only [↓reduceIte] at hz + rw [Notif.setThreadState_eq .running hkC] at hz + rw [Set.mem_singleton_iff.1 hz] + exact ⟨V3, Ac, Lst', D, J4, J.natTrans F13 F4.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrFlags.lean b/proofs/Fl4Proofs/P3/JAtZBThrFlags.lean new file mode 100644 index 000000000..f4761db60 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrFlags.lean @@ -0,0 +1,213 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **SetFlags**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_threxttop.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrExtAux +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 16000000 in +/-- **The SetFlags label at `JAt`** (as `tcb_set_flagsX`): the caller's +restart (`JAtZ.restartJ`), FL4's flags read (`FlagsRel` at the entry state +[J], across the restart by `JAtZ.flagsRestart`) and write with the spec's `set_flags` (`JAtZ.flagsWrJ`), the FPU +release when the FPU is disabled (`JAtZ.fpuReleaseJ'`, `ARKeep` [P]), then +the data tail of the new flags word (`XTop`'s facts by the XTop tails, `JAt` +by `JAtZ.runJ` / `JAtZ.replyWords1J`). -/ +theorem JAtZB.tcbSetFlagsF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) + (hFl : FlagsRel Z.T (L.mem s) σ) + (p : U64) (hp : Z.th p = some .tcb) (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetFlags) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (hK : ARKeep arI AR) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBSetFlags p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s ar σ call + (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) ct res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + obtain ⟨htcb, xc, hkc⟩ := hX.curTcb hcur + have hpT : p ∈ Z.T := J.thT p hp + have hctT : ct ∈ Z.T := J.thT ct htcb + rw [decode_flags_eq arI s ar p _ hstall, setFlags_dec slbl hlbl] + by_cases h1 : length < 2#u64 + · unfold tcb.decode_set_flags + simp only [h1, ↓reduceIte, error.truncated, bind_ok] + exact (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, + setFlags_trunc σ p.bv _ (by rw [List.length_map, ← hlen]; scalar_tac), Or.inl trivial⟩) + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, hlen with + | y0 :: y1 :: rest, _ => exact ⟨_, _, _, rfl⟩ + | [], h => exfalso; simp only [List.length_nil] at h; scalar_tac + | [_], h => exfalso; simp only [List.length_singleton] at h; scalar_tac + have ha0 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have ha1 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 1#u64 = ok y1 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 1#u64 y1 (by simp) + obtain ⟨s1, σ1, x0, hrc1, hst1, hk0, -, hσ1, -, -, hcu1, hss1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + rw [hkc] at hk0; cases hk0 + have hX1 := J1.xtop + have hFl1 : FlagsRel Z.T (L.mem s1) σ1 := by rw [hσ1]; exact J.flagsRestart hFl htcb hkc hss1 + have hkc1 : σ1.kheap ct.bv = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1, Notif.stsAct_kheap]; simp [Notif.updK] + have hcurY1 : σ1.curThread = ct.bv := by rw [hcu1, hcur] + have hdec := DecOut.of_eq (setFlags_ok σ p.bv y0.bv y1.bv (rest.map UScalar.bv)) + unfold tcb.decode_set_flags + simp only [h1, ↓reduceIte] + rw [hct]; simp only [bind_ok]; rw [ha0]; simp only [bind_ok]; rw [ha1]; simp only [bind_ok] + rw [hrc1]; simp only [bind_ok] + rw [flags_eq]; simp only [bind_ok, lift, set_flags_eq] + -- the flags, both sides + obtain ⟨xp, hkp, hfp⟩ := hFl1 p hpT + set f := L.mem s1 (tFlagsWA p) + set f2 := (f &&& ~~~y0) ||| (y1 &&& fl4_abi.consts.tcb_flag.MASK) with hf2 + set nf : Fl4Spec.TcbFlags := xp.tcbFlags \ Fl4Spec.wordToTcbFlags y0.bv ∪ Fl4Spec.wordToTcbFlags y1.bv with hnf + have hW2 : Fl4Spec.wordToTcbFlags f2.bv = nf := by + rw [hnf, ← hfp, hf2, flags_mask_eq, UScalar.bv_or, UScalar.bv_and, UScalar.bv_and, UScalar.bv_not] + exact flags_new _ _ _ + obtain ⟨V2, J2, F2⟩ := J1.flagsWrF hpT hkp f2 nf hW2 + unfold flagsA at J2 F2 + have F12 := J.natTrans F1 F2.cast + set s2 := L.wr s1 (core.num.U64.wrapping_add p 3456#u64) f2 + set σ2 := Notif.updK σ1 p.bv (.tcb { xp with tcbFlags := nf }) + have hts : Fl4Spec.setFlags (Z := Unit) p.bv nf σ1 = ({((), σ2)}, False) := Notif.threadSet_eq _ hkp + have hcur2 : σ2.curThread = ct.bv := hcurY1 + have hkc2 : ∃ y, σ2.kheap ct.bv = some (.tcb y) ∧ y.tcbState = .restart := by + by_cases e : ct.bv = p.bv + · rw [e] at hkc1 ⊢ + have e2 := Fl4Spec.KernelObject.tcb.inj (Option.some.inj (hkp.symm.trans hkc1)) + exact ⟨{ xp with tcbFlags := nf }, by simp [σ2, Notif.updK], by rw [e2]⟩ + · exact ⟨{ xc with tcbState := .restart }, by simp [σ2, Notif.updK, Function.update_of_ne e, hkc1], rfl⟩ + have hfpu : ((f2 &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64) = true ↔ + Fl4Spec.TcbFlag.fpuDisabled ∈ nf := by + rw [← hW2, w2f_mem, ← and1_ne] + have key : ∀ x : U64, (x &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) = 0#u64 ↔ x.bv &&& 1 = 0 := by + intro x; rw [← bv_eq_iff'] + show UScalar.bv (x &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) = UScalar.bv 0#u64 ↔ _ + rw [UScalar.bv_and]; unfold fl4_abi.consts.tcb_flag.FPU_DISABLED; rfl + simp only [bne_iff_ne, ne_eq, key] + -- the tail, from the spec's state after the FPU step + have tail : ∀ (a1 : A) (σ3 : Fl4Spec.State Unit) (V3 : XVs) (Ac3 : Set U64) (Lst3 : U64 → List U64) + (D3 : Set U64), + ((), σ3) ∈ (Fl4Spec.NonDet.when (Fl4Spec.TcbFlag.fpuDisabled ∈ nf) (Fl4Spec.fpuRelease (Z := Unit) p.bv) + σ2).1 → JAtZB (L := L) arI AR (Z.mv Z.G V3) H Cs Ac3 Lst3 D3 s2 a1 σ3 → NatFoot (L := L) Z s σ s2 σ3 → + σ3.curThread = ct.bv → + (∃ y, σ3.kheap ct.bv = some (.tcb y) ∧ y.tcbState = .restart) → + (if call = true then (do + let buf ← msg.lookup_ipc_buffer st s2 true ct + let (len, s3) ← msg.set_mr st s2 ct buf 0#u64 f2 + let s4 ← tcb.reply_words st s3 ct len + let s5 ← sched.set_thread_state st s4 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a1)) + else (do + let s3 ← sched.set_thread_state st s2 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1))) ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s ar σ call (Fl4Spec.NonDetE.liftME + Fl4Spec.Fl4Invocation.invokeFl4TCB (Fl4Spec.decodeFl4SetFlags (Z := Unit) + ((y0 :: y1 :: rest).map UScalar.bv) p.bv)) ct res ⦄ := by + intro a1 σ3 V3 Ac3 Lst3 D3 h3 J3 F3 hcur3 ⟨y3, hk3, hrs3⟩ + set Z3 : XSets := Z.mv Z.G V3 with hZ3 + have hX3 := J3.xtop + have hmem : (.ok [f2.bv], σ3) ∈ ((Fl4Spec.invokeFl4Tcb (Z := Unit) + (.setFlags p.bv y0.bv y1.bv)).run σ1).1 := by + show _ ∈ ((Fl4Spec.NonDetE.liftE _).run σ1).1 + refine mem_liftE (x := ([f2.bv], σ3)) ?_ + show ([f2.bv], σ3) ∈ ((do + let flags ← Fl4Spec.threadGet (Z := Unit) Fl4Spec.Tcb.tcbFlags p.bv + let newFlags ← pure (flags \ Fl4Spec.wordToTcbFlags y0.bv ∪ Fl4Spec.wordToTcbFlags y1.bv) + Fl4Spec.setFlags p.bv newFlags + Fl4Spec.NonDet.when (Fl4Spec.TcbFlag.fpuDisabled ∈ newFlags) (Fl4Spec.fpuRelease p.bv) + let w ← Fl4Spec.NonDet.select {w | Fl4Spec.wordToTcbFlags w = newFlags} + return [w]) σ1).1 + rw [Notif.nd_bind_one (Notif.threadGet_eq _ hkp), Notif.nd_pure_bind', Notif.nd_bind_one hts] + exact mem_bind_nd (x := ((), σ3)) (by convert h3) (mem_bind_nd (x := (f2.bv, σ3)) (y := ([f2.bv], σ3)) ⟨hW2, rfl⟩ + (Set.mem_singleton _)) + have hcurY : ct.bv = σ3.curThread := hcur3.symm + have htcb3 : (Z.mv Z.G V3).th ct = some .tcb := htcb + have hact3 : OutsideObjects (Z.mv Z.G V3).th (L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION) := J3.kern.act + have hctT3 : ct ∈ (Z.mv Z.G V3).T := J3.thT ct htcb3 + cases call + · simp only [Bool.false_eq_true, ↓reduceIte] + have hJt : (do + let s5 ← sched.set_thread_state st s2 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a1)) ⦃ res => + ∀ z ∈ (specTail false ct.bv [f2.bv] σ3).1, ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) + (D' : Set U64), JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 z.2 ∧ + NatFoot (L := L) Z s σ res.2.1 z.2 ⦄ := by + refine WP.spec_bind (J3.runJ hctT3 hk3 hrs3) ?_ + rintro s5 ⟨Lst', J5, F5⟩ + refine (WP.spec_ok _).2 fun z hz => ?_ + rw [specTail_restart hk3 hrs3 false f2.bv] at hz + simp only [Bool.false_eq_true, ↓reduceIte] at hz + rw [Notif.setThreadState_eq .running hk3] at hz + rw [Set.mem_singleton_iff.1 hz] + exact ⟨V3, Ac3, Lst', D3, J5, J.natTrans F3 F5.cast⟩ + refine WP.spec_mono (WP.spec_and (nodata_tail_xtop hX3 hcurY hk3 hrs3 hact3 [f2.bv]) hJt) ?_ + rintro res ⟨⟨hres, hne, z, hz, hRT, -⟩, hJ5⟩ + exact Or.inr ⟨hres, hne, _, hdec, σ1, hst1, [f2.bv], σ3, hmem, z, hz, hRT, hJ5 z hz⟩ + · simp only [↓reduceIte] + refine WP.spec_bind (Fl4Proofs.P0.msg.lookup_ipc_buffer.p0 st s2 true ct L.total) ?_ + rintro bf - + have hI0 : Vs.RInv (L := L) Z3.th Z3.P Z3.L4 Z3.L3 Z3.L2 Z3.L1 Z3.N Z3.T Z3.C Z3.E Z3.R Z3.B Z3.c AR Z3.G s2 a1 + (MrS σ3 ct.bv y3 (Endp.ipcBufOf true y3) []) := by + rw [MrS_nil hk3]; exact hX3.toRInv + have hset : msg.set_mr st s2 ct bf 0#u64 f2 ⦃ r => + r = (1#u64, L.wr s2 (Endp.regA ct (Endp.regIdx (Endp.mrIdx 0))) f2) ⦄ := by + unfold msg.set_mr + have h4 : (0#u64 : U64) < msg.N_MSG_REGISTERS := by unfold msg.N_MSG_REGISTERS; decide + simp only [h4, ↓reduceIte] + refine WP.spec_bind (Endp.msg_register_spec 0#u64 (by decide)) ?_ + rintro r0 rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec _ ct _ f2) ?_ + rintro s2' rfl + simp only [lift, bind_ok, WP.spec_ok] + rfl + refine WP.spec_bind (WP.spec_and (setmr_reg_inv _ ct htcb3 hcurY y3 _ bf s2 a1 [] hI0 0#u64 rfl (by decide) f2) + hset) ?_ + rintro ⟨len, s3⟩ ⟨⟨hlen, hI3, hk3', hc3'⟩, he3⟩ + simp only [Prod.mk.injEq] at hlen hI3 hk3' hc3' he3 + obtain ⟨rfl, rfl⟩ := he3 + refine WP.spec_mono (WP.spec_and (data_tail_xtop hX3 hcurY hk3 hrs3 hact3 hI3 hk3' hc3' + (by simp [Fl4Spec.msgMaxLength]) (fun _ _ z hz => by simp [Endp.mrPairs] at hz) 1#u64 + (by cases Endp.ipcBufOf true y3 <;> simp [mrLen]) (by decide)) + (J3.replyWords1F hctT3 hk3 hrs3 f2)) ?_ + rintro res ⟨⟨hres, hne, z, hz, hRT, -⟩, hJ5⟩ + refine Or.inr ⟨hres, hne, _, hdec, σ1, hst1, [f2.bv], σ3, hmem, z, hz, hRT, ?_⟩ + obtain ⟨V5, Ac5, Lst5, D5, J5, F5⟩ := hJ5 z hz + exact ⟨V5, Ac5, Lst5, D5, J5, J.natTrans F3 F5.cast⟩ + by_cases hF : ((f2 &&& fl4_abi.consts.tcb_flag.FPU_DISABLED) != 0#u64) = true + · simp only [hF, ↓reduceIte] + refine WP.spec_bind (J2.fpuReleaseF' hK hpT) ?_ + rintro a1 ⟨σ3, h3, hc3, hst3, V3, J3, F3⟩ + obtain ⟨y2, hky2, hrs2⟩ := hkc2 + obtain ⟨y3, hky3, hrs3⟩ := hst3 _ y2 hky2 + refine tail a1 σ3 _ _ _ _ ?_ J3 (J.natTrans F12 F3.cast) (by rw [hc3]; exact hcur2) ⟨y3, hky3, by rw [hrs3, hrs2]⟩ + unfold Fl4Spec.NonDet.when; rw [if_pos (hfpu.1 hF)]; exact h3 + · simp only [Bool.not_eq_true] at hF + simp only [hF, Bool.false_eq_true, ↓reduceIte] + refine tail ar σ2 _ _ _ _ ?_ J2 F12 hcur2 hkc2 + unfold Fl4Spec.NonDet.when + rw [if_neg (fun h => by have := hfpu.2 h; rw [hF] at this; cases this)] + exact Set.mem_singleton _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrFpu.lean b/proofs/Fl4Proofs/P3/JAtZBThrFpu.lean new file mode 100644 index 000000000..ab81ae847 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrFpu.lean @@ -0,0 +1,233 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **WriteFPU, ReadFPU** (post `ThrPostFX`). + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrfpu.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrFpuAux +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The WriteFPU label at `JAt`** (as `tcb_fpuX` with `toFrame` false): +XTopTcbFpu's `fpu_refinesX` at `X := JAt` (the checks, the decode errors at +the start's `JAtZ`), the work at `J`: the caller's restart +(`JAtZ.restartJ`), `fpu_flush`, the copy into the target's FPU image +(XTopJointTcbMisc's `JAtZ.copyFpuJ`), `fpu_sanitise`, `fpu_reload` (the arch +relation kept: `ARKeep` [P], already a top-level premise; `JAtZ.archJ`), the +spec state the restart's. -/ +theorem JAtZB.tcbWriteFpuF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (hK : ARKeep arI AR) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .writeFpu) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (blk don : Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBWriteFPU p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostFX (L := L) arI AR Z H Cs s σ + (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + have htcb := (hX.curTcb hcur).1 + have hDS : DState (L := L) Z.th s σ Z.G := ⟨hX.rel.2.1, J.del, hX.rel.2.2.1⟩ + have hdec0 : specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4FpuTransfer (Z := Unit) false (args.map UScalar.bv) p.bv excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [(decode_fpu_eq arI s ar p _ hstall).2, hdec0] + refine WP.spec_mono (fpu_refinesX arI (JBN (L := L) arI AR Z H Cs s σ) + (fun _ _ _ ⟨V', _, _, _, J', _⟩ => ⟨Z.mv Z.G V', J'.xtop⟩) Z.th hX.rel.1 + s ar σ Z.G hDS hX.rel.2.2.2.2.1 p false length buf ct hct args hlen hargs hcur htcb J.kern.act call slot extra + x0 x1 x2 hx0 excaps hXc ⟨Z.vs, Ac, Lst, D, J, NatFoot.refl _ _ _⟩ blk don ?_) (fun _ h => ThrPostFX.of_dmx h) + intro s1 σ1 x ptr offset r asid va m ep sz hs1 hst1 hx hxn hl ho hc hpass + obtain ⟨s1', σ1', -, hrc1, hst1', -, -, -, -, -, -, hs1', Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + have e1 : s1' = s1 := by + have := hs1'.symm.trans hs1 + simpa using this + have e2 : σ1' = σ1 := by + rw [hcur] at hst1' + have h := (Prod.mk.inj (hst1'.symm.trans hst1)).1 + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 h) + subst e1 e2 + have hAR1 := J1.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨hr1, hr3, hm, hlt, hrm⟩ := hpass + have hvk : capability.VM_KERNEL_ONLY = 1#u64 := by simp only [capability.VM_KERNEL_ONLY] + have hvrw : capability.VM_READ_WRITE = 3#u64 := by simp only [capability.VM_READ_WRITE] + obtain ⟨z, hzv, hzf⟩ := fshift_eq sz + have hge : (⟨length, buf, call, slot, extra⟩ : tcb.Ctx).length >= 1#u64 := by scalar_tac + have hsz : fl4_abi.consts.FPU_STATE_SIZE + 7#u64 = ok 2703#u64 := by + unfold fl4_abi.consts.FPU_STATE_SIZE + obtain ⟨v, hv, hvv⟩ := spec_exists (U64.add_spec (x := 2696#u64) (y := 7#u64) (by scalar_tac)) + rw [hv]; congr 1; apply UScalar.eq_of_val_eq; rw [hvv]; rfl + have hwd : (2703#u64 : U64) / 8#u64 = ok 337#u64 := by + obtain ⟨v, hv, hvv⟩ := spec_exists (U64.div_spec (x := 2703#u64) (y := 8#u64) (by decide)) + rw [hv]; congr 1; apply UScalar.eq_of_val_eq; rw [hvv]; rfl + unfold tcb.fpu_transfer + rw [extra_eq _ 0#usize (by decide) hx] + simp only [hxn, ↓reduceIte, hge, bind_ok] + rw [ho]; simp only [bind_ok]; rw [slot_cap_eq, capAt_capV, hc]; simp only [bind_ok, hvk, hvrw, hzf] + simp only [Bool.false_eq_true, ↓reduceIte, hr1] + have hp' : offset.val % 64 = 0 ∧ offset.val < z.val ∧ 2696 ≤ z.val - offset.val := by + unfold FpuPass.fl4Spec_fpu Fl4Spec.fl4FpuStateSize at hrm + exact ⟨hm, hzv ▸ hlt, hzv ▸ hrm⟩ + rw [off_pass _ _ offset z hp'] + rw [hrc1]; simp only [bind_ok] + refine WP.spec_bind (hK.fpu_flush ar p _ hAR1) fun a1 ha1 => ?_ + simp only [hsz, hwd, bind_ok, lift, obj.Tcb.fpu_addr] + refine WP.spec_bind ((J1.archJ ha1).copyFpuF hpT _) ?_ + rintro s2 ⟨V2, J2, F2⟩ + try simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (hK.fpu_sanitise a1 p _ ha1) fun a2 ha2 => ?_ + refine WP.spec_bind (hK.fpu_reload a2 p _ ha2) fun a3 ha3 => ?_ + exact (WP.spec_ok _).2 ⟨rfl, σ1', rfl, V2, Ac1, Lst1, D1, J2.archJ ha3, J.natTrans F1 F2⟩ + +set_option maxHeartbeats 16000000 in +/-- **The ReadFPU label at `JAt`** (as `tcb_fpuX` with `toFrame` true): +XTopTcbFpu's `fpu_refinesX` at `X := JAt` (the checks, the decode errors at +the start's `JAtZ`), the work at `J`: the caller's restart +(`JAtZ.restartJ`), `fpu_flush` (`ARKeep` [P]; `JAtZ.archJ`), the copy of the +target's FPU image into the frame (XTopJointReadFpu's `JAtZ.copyFrameJ`: the +frame cap's range misses `H`'s non-`other` objects, `JPlaced.cobj`, hence the +TCBs; the frame's bytes in spec user frames from `FpuFrameJHyp` at the start, a +theorem of `JAtZ` (`JAtZ.fpuFrameJ`, from `fcap`), carried across the restart), the spec's step `fl4FpuToFrame` at the user memory FL4's copy leaves +(`fpuToFrame_mem`). -/ +theorem JAtZB.tcbReadFpuF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (hK : ARKeep arI AR) + (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .readFpu) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hFJ : FpuFrameJHyp (L := L) Z s σ x0) + (blk don : Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBReadFPU p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostFX (L := L) arI AR Z H Cs s σ + (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + have htcb := (hX.curTcb hcur).1 + have hDS : DState (L := L) Z.th s σ Z.G := ⟨hX.rel.2.1, J.del, hX.rel.2.2.1⟩ + have hdec0 : specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4FpuTransfer (Z := Unit) true (args.map UScalar.bv) p.bv excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [(decode_fpu_eq arI s ar p _ hstall).1, hdec0] + refine WP.spec_mono (fpu_refinesX arI (JBN (L := L) arI AR Z H Cs s σ) + (fun _ _ _ ⟨V', _, _, _, J', _⟩ => ⟨Z.mv Z.G V', J'.xtop⟩) Z.th hX.rel.1 + s ar σ Z.G hDS hX.rel.2.2.2.2.1 p true length buf ct hct args hlen hargs hcur htcb J.kern.act call slot extra + x0 x1 x2 hx0 excaps hXc ⟨Z.vs, Ac, Lst, D, J, NatFoot.refl _ _ _⟩ blk don ?_) (fun _ h => ThrPostFX.of_dmx h) + intro s1 σ1 x ptr offset r asid va m ep sz hs1 hst1 hx hxn hl ho hc hpass + have hxx : x = x0 := by + have h := hx.symm.trans hx0 + exact Fl4Proofs.P3.ok_inj (h : (ok x : Result U64) = ok x0) + subst hxx + -- the frame cap's slot, alignment and placement + obtain ⟨sl0, hA0, hxe⟩ : ∃ sl0, AbsSlot Z.th sl0 ∧ x = cslotAddr sl0 := by + have h0 := hXc.2 0 x rfl + obtain ⟨e, he⟩ : ∃ e, excaps[0]? = some e := by + rcases excaps with _ | ⟨e, rest⟩ + · exact absurd (h0.1.2 (by simp)) hxn + · exact ⟨e, rfl⟩ + obtain ⟨hxe, -, ps, hps, hpe⟩ := h0.2 e he + exact ⟨e.2, ⟨ps, hpe, Or.inl hps⟩, hxe⟩ + have hal : ptr.val % 2 ^ frameBitsN sz = 0 := (show CapValid Z.th (.Frame ptr sz r false m ep asid va) from + hX.rel.2.1.2.1.abs hA0 (by rw [← hxe, capAt_capV, hc])) + obtain ⟨h4096, hp8, hpfit⟩ := frame_fit hal + obtain ⟨hbs, hts⟩ := frame_topF (sz := sz) r false m ep asid va hal + have hFA : Sched.FrApartH H (.Frame ptr sz r false m ep asid va) := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have := hJ.cobj x (by rw [hxe]; exact hA0.isSlot); rw [hc] at this; exact this + have hU := hFJ sl0 hA0 hxe ptr sz r m ep asid va hc + -- the restart + obtain ⟨s1', σ1', xt, hrc1, hst1', hkt, -, e1σ, -, -, -, hs1', Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + have e1 : s1' = s1 := by + have := hs1'.symm.trans hs1 + simpa using this + have e2 : σ1' = σ1 := by + rw [hcur] at hst1' + have h := (Prod.mk.inj (hst1'.symm.trans hst1)).1 + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 h) + subst e1 e2 + have hU1 : ∀ a : Nat, ptr.val ≤ a → a < ptr.val + 2 ^ frameBitsN sz → _root_.Fl4Proofs.P3.UFr σ1' a := + fun a h1 h2 => _root_.Fl4Proofs.P3.UFr.keep (fun o vms hko => by + have hne : o ≠ ct.bv := fun e => by rw [e, hkt] at hko; cases hko + rw [e1σ, stsAct_kheap, Notif.updK_kheap, Function.update_of_ne hne]; exact hko) (hU a h1 h2) + have hAR1 := J1.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨hr1, hr3, hm, hlt, hrm⟩ := hpass + have hvk : capability.VM_KERNEL_ONLY = 1#u64 := by simp only [capability.VM_KERNEL_ONLY] + have hvrw : capability.VM_READ_WRITE = 3#u64 := by simp only [capability.VM_READ_WRITE] + obtain ⟨z, hzv, hzf⟩ := fshift_eq sz + have hge : (⟨length, buf, call, slot, extra⟩ : tcb.Ctx).length >= 1#u64 := by scalar_tac + have hsz : fl4_abi.consts.FPU_STATE_SIZE + 7#u64 = ok 2703#u64 := by + unfold fl4_abi.consts.FPU_STATE_SIZE + obtain ⟨v, hv, hvv⟩ := spec_exists (U64.add_spec (x := 2696#u64) (y := 7#u64) (by scalar_tac)) + rw [hv]; congr 1; apply UScalar.eq_of_val_eq; rw [hvv]; rfl + have hwd : (2703#u64 : U64) / 8#u64 = ok 337#u64 := by + obtain ⟨v, hv, hvv⟩ := spec_exists (U64.div_spec (x := 2703#u64) (y := 8#u64) (by decide)) + rw [hv]; congr 1; apply UScalar.eq_of_val_eq; rw [hvv]; rfl + unfold tcb.fpu_transfer + rw [extra_eq _ 0#usize (by decide) hx0] + simp only [hxn, ↓reduceIte, hge, bind_ok] + rw [ho]; simp only [bind_ok]; rw [slot_cap_eq, capAt_capV, hc]; simp only [bind_ok, hvk, hvrw, hzf] + simp only [Bool.false_eq_true, ↓reduceIte, hr1] + have hp' : offset.val % 64 = 0 ∧ offset.val < z.val ∧ 2696 ≤ z.val - offset.val := by + unfold FpuPass.fl4Spec_fpu Fl4Spec.fl4FpuStateSize at hrm + exact ⟨hm, hzv ▸ hlt, hzv ▸ hrm⟩ + try simp only [↓reduceIte, hr3 rfl, bne_self_eq_false, Bool.false_eq_true] + rw [off_pass _ _ offset z hp'] + rw [hrc1]; simp only [bind_ok] + refine WP.spec_bind (hK.fpu_flush ar p _ hAR1) fun a1 ha1 => ?_ + simp only [hsz, hwd, bind_ok, lift, obj.Tcb.fpu_addr] + -- the frame's 2696 bytes at `f` + have hrm' : 2696 ≤ 2 ^ frameBitsN sz - offset.val := hrm + have hfv : (core.num.U64.wrapping_add ptr offset).val = ptr.val + offset.val := by + simp only [core.num.U64.wrapping_add_val_eq] + simp [U64.size, U64.numBits] + omega + refine WP.spec_bind ((J1.archJ ha1).copyFrameF _ (core.num.U64.wrapping_add ptr offset) + (by rw [hfv]; omega) (by rw [hfv]; omega) (fun q k hq hk => ?_) + (fun a h1 h2 => hU1 a (by rw [hfv] at h1; omega) (by rw [hfv] at h2; omega))) ?_ + · rcases hFA q k hq hk with e | e + · right; rw [hbs] at e; rw [hfv]; omega + · left; rw [hts] at e; rw [hfv]; omega + rintro s2 ⟨um, hag, J2, F2⟩ + try simp only [↓reduceIte] + refine (WP.spec_ok _).2 ⟨rfl, Endp.setUm σ1' um, ?_, Z.vs, Ac1, Lst1, D1, J2, J.natTrans F1 F2⟩ + show _ ∈ ((Fl4Spec.NonDetE.liftE _).run σ1').1 + refine mem_liftE (x := ([], Endp.setUm σ1' um)) ?_ + refine mem_bind_nd (x := ((), Endp.setUm σ1' um)) ?_ (Set.mem_singleton_iff.2 rfl) + have hfb : (core.num.U64.wrapping_add ptr offset).bv = ptr.bv + offset.bv := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_add] + show (core.num.U64.wrapping_add ptr offset).val = (ptr.val + offset.val) % 2 ^ 64 + rw [hfv]; exact (Nat.mod_eq_of_lt (by omega)).symm + exact dmo_ex _ σ1' (fpuToFrame_mem _ σ1'.machineState um fun b hb => hag b (by rw [hfb]; exact hb)) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrFpuAux.lean b/proofs/Fl4Proofs/P3/JAtZBThrFpuAux.lean new file mode 100644 index 000000000..80b29b8cb --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrFpuAux.lean @@ -0,0 +1,220 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **the FPU steps over `JAtZB`, with `NatFoot`**: an FPU-image +word (`fpuWrF`, footprint by `JAtZB.tcbWordN`), the copy into a TCB's FPU image (`copyFpuF`), the copy of an FPU +image into a user frame (`copyFrameF`, footprint by `JAtZB.userWrN`), an arch step (`archJ`). `JAtZB` siblings of +XTopJointTcbMisc's / XTopJointReadFpu's `JAtZ` lemmas, generated by `handoff/tools/gen_thrfpu.py`. No premise. New +file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPostF +import Fl4Proofs.P3.JAtZBThrSched +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Fl4Proofs.P1 Fl4Spec in +set_option maxHeartbeats 8000000 in +/-- **`JAtZ` across a register step with FPU-image words** (as +XTopJointTcb's `JAtZ.reg_frameG`, the write set widened by the TCBs' FPU +images, offsets 512..3208). -/ +theorem JAtZB.reg_frameFp (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} + (hI3 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hw : P1.WritesIn st s s' (fun b => RegFW Z.T Z.C b ∨ ∃ y ∈ Z.T, + y.val + 512 ≤ b.val ∧ b.val < y.val + 3208)) (hA : QKY σ σ') (hyk : YKeep σ σ') (hInv : Inv σ') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) ∅ s)) : + ∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst D s' a σ' := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => by + rcases hb with ((hb | ⟨q, hq, rfl⟩) | ⟨y, hy, hb⟩) | ⟨y, hy, h1, h2⟩ + · obtain ⟨y, hy, h1, h2⟩ := hb + exact Or.inr (Or.inr (Or.inl ⟨y, hy, h1, by omega⟩)) + · obtain ⟨x0, n0, hk0, hx0⟩ := hs.scs q hq + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl + ⟨q, hq, x0, n0, hk0, hx0.in_obj (hx0.hdr (K := 8#u64) (by simp) (by simp)).2⟩)))))) + · exact Or.inr (Or.inr (Or.inl ⟨y, hy, by omega, by omega⟩)) + · exact Or.inr (Or.inr (Or.inl ⟨y, hy, by omega, by omega⟩)) + have hwF : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := hw.mono fun b hb => by + rcases hb with ((⟨y, hy, k1, k2⟩ | ⟨q, hq, rfl⟩) | ⟨y, hy, hb⟩) | ⟨y, hy, k1, k2⟩ + · exact Or.inr ⟨y, hy, b.val - y.val, by omega, by omega, by omega, by omega, by omega, by omega⟩ + · exact Or.inl (KFoot.of_sw (sw_hdr hS hq (K := 8#u64) (by simp) (by simp) (by decide))) + · exact Or.inr ⟨y, hy, 3376, by omega, by omega, by omega, by omega, by omega, hb⟩ + · exact Or.inr ⟨y, hy, b.val - y.val, by omega, by omega, by omega, by omega, by omega, by omega⟩ + have hck : Fl4Proofs.P3.CKeep σ σ' := hA.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.finW_keeps hwF + have ck := hA.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hA.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hI3.2.2.1 + have hAI3 : ActIdle σ' := hA.actIdle J.act + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_qk hA hS3 hI3.1 hwF k3 hAI3.1 (J.yld.frame k2 hyk) hyk + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hA.szKeep q.bv y n hq) hks (J.finW_irq hwF) + (J.idleW_keep hS3 hA.idle hA.isc).2⟩ + have hkern := J.kern_keep hks hwF.2.1 hS3 hA.ct hA.csc (fun sl h => Or.inl (by rw [← hA.orig]; exact h)) + (by rw [hA.arch]) + (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hDbg + (D' := D) + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac' Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 hyk, J.mcp.frame k1 hA.mcpKeep, J.buf.frame kb hA.bufKeep, hRP', + J.apart.sc_frame hks hA.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hA.idle hA.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hwF fun _ hb => Ipc.FinW.ntd J.xtop.fin.1.1.1 hb)), hkern, + J.finW_utc hwF hF.1 hA.cdt hA.orig hA.arch hDbg, hA.nsc J.nsc, hInv, hA.cur J.cur, hA.own J.own, hA.off J.off, + hAI3, hA.bsa J.bsa, hA.isorted J.isorted, hA.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameB J hF.1 hA.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact ⟨Z.vs, J.cs_frameI hks hX3 hwF (fun b hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) hA.arch (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hA.szKeep hF.1 hA.kback hA.ipcFr hC R hKd hck.orig (J.idleW_keep hS3 hA.idle hA.isc).1 J.zD⟩ + +open Fl4Proofs.P1 Fl4Spec in +set_option maxHeartbeats 8000000 in +/-- **A word of a TCB's FPU image written at `J`** (`JAtZ.reg_frameFp` with +the spec state kept: `XTopAt.tcbWrF`, `IRel` by `XTopAt.irelK`, P1's `BlkOK` +by `blk_rf`, its footprint by `foot_field`). -/ +theorem JAtZB.fpuWrJB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (wa : U64) + (Kv : Nat) (hK : 512 ≤ Kv ∧ Kv < 3208 ∧ Kv % 8 = 0) (hwav : wa.val = t.val + Kv) (v : U64) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s wa v) a σ := by + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hta := hT t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hT + set K : U64 := ⟨BitVec.ofNat 64 Kv⟩ with hKdef + have hKv : K.val = Kv := by + show (BitVec.ofNat 64 Kv).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hKt : (core.num.U64.wrapping_add t K).val = t.val + K.val := tfield_val hta (by omega) + have hreq : wa = core.num.U64.wrapping_add t K := UScalar.eq_of_val_eq (by rw [hwav, hKt, hKv]) + subst hreq + have hwa : P1.Aligned (core.num.U64.wrapping_add t K) := P1.field_aligned hta (by omega) (by omega) + have hw : P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add t K) v) (· = core.num.U64.wrapping_add t K) := + P1.writesIn_wr hwa + have hblk := (P1.blk_rf J.blk (hw.mono fun b hb => Or.inl ⟨t, htH, K, by omega, by omega, hb⟩)).1 + have hfoot : P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add t K) v) (P1.KW st (P1.Heap.tcbs H) Cs + (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s) := + P1.foot_field hTH htH (by omega) (by omega) (by omega) ⟨by omega, by omega⟩ + have hX' := J.xtop.tcbWrF ht _ Kv hK (by rw [hKt, hKv]) v + have hks : L.ksb (L.wr s (core.num.U64.wrapping_add t K) v) = L.ksb s := hw.1 + have hwF : P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add t K) v) (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := + hw.mono fun b hb => Or.inr ⟨t, ht, Kv, by omega, by omega, by omega, by omega, by omega, by rw [hb, hKt, hKv]⟩ + obtain ⟨-, -, kb, -, -⟩ := J.finW_keeps hwF + have hA : QKY σ σ := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun p => Or.inl rfl⟩ + have hBW : BufWordRel Z.T (L.mem (L.wr s (core.num.U64.wrapping_add t K) v)) σ := J.buf.frame kb hA.bufKeep + have hI3 := hX'.irelK (by rw [hks]; exact J.ks) hBW + exact J.reg_frameFp hI3 (hw.mono fun b hb => Or.inr ⟨t, ht, by rw [hb, hKt, hKv]; omega, + by rw [hb, hKt, hKv]; omega⟩) hA (YKeep.of_kheap rfl) J.inv hblk hfoot + +/-- **An FPU-image word at `J`, with the native footprint** (`JAtZB.fpuWrJB`, `JAtZB.tcbWordN`). -/ +theorem JAtZB.fpuWrF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (wa : U64) + (Kv : Nat) (hK : 512 ≤ Kv ∧ Kv < 3208 ∧ Kv % 8 = 0) (hwav : wa.val = t.val + Kv) (v : U64) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s wa v) a σ ∧ + NatFoot (L := L) Z s σ (L.wr s wa v) σ := + let ⟨V, B'⟩ := J.fpuWrJB ht wa Kv hK hwav v + ⟨V, B', J.tcbWordN ht (by omega) (by omega) hK.2.2 (by omega) hwav v⟩ + +/-- `JAtZ.archJ` over `JAtZB`: an arch step relating the same spec state. -/ +theorem JAtZB.archJ (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {a' : A} + (hAR : AR a' σ.archState.x64CurrentCr3) : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a' σ := + J.arFrS (arFr_fpuOwner σ σ.machineState σ.archState.x64CurrentFpuOwner) (J.xtop.arch hAR) + +set_option maxHeartbeats 8000000 in +/-- **WriteFPU's copy into a TCB's FPU image at `J`** (`copy_words_inv`, +each word by `JAtZ.fpuWrJ`). -/ +theorem JAtZB.copyFpuF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (src : U64) : + tcb.copy_words st s src (core.num.U64.wrapping_add t layout.tcb.FPU) 337#u64 ⦃ s' => + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D s' a σ ∧ NatFoot (L := L) Z s σ s' σ ⦄ := by + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hpf := (hcq.tcbs t ht).fits + have hpa := (hcq.tcbs t ht).align + have hfpu : (core.num.U64.wrapping_add t layout.tcb.FPU).val = t.val + 512 := by + unfold layout.tcb.FPU + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + refine copy_words_inv (fun s' => ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D s' a σ ∧ + NatFoot (L := L) Z s σ s' σ) s src _ _ ⟨Z.vs, J, NatFoot.refl _ _ _⟩ ?_ + rintro s2 i ⟨V, J2, F2⟩ hi v + have hiv : (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] + have : i.val < 337 := hi + omega + have hi' : i.val < 337 := hi + obtain ⟨V', J3, F3⟩ := J2.fpuWrF (t := t) ht + (core.num.U64.wrapping_add (core.num.U64.wrapping_add t layout.tcb.FPU) (core.num.U64.wrapping_mul i 8#u64)) + (512 + 8 * i.val) ⟨by omega, by omega, by omega⟩ (by + have h1 : (core.num.U64.wrapping_add t layout.tcb.FPU).val + (core.num.U64.wrapping_mul i 8#u64).val < 2 ^ 64 := by + rw [hfpu, hiv]; omega + clear h1 + unfold layout.tcb.FPU + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq] + simp [U64.size, U64.numBits] + omega) v + exact ⟨V', J3, J.natTrans F2 F3.cast⟩ + +set_option maxHeartbeats 8000000 in +/-- **ReadFPU's copy into the frame at `J`** (`copy_words_inv` with +`JAtZ.userWrJ`): the 2696 bytes at `f` miss every non-`other` object of `H` +and lie in spec user frames (`ufr_word`: a data page per word), so each word `w` written is the IPC-buffer word +`(w, w)` of `B` (`FrameCover`); the spec's user memory is stored as FL4's +there, and so agrees with the old one outside the 2696 bytes at `f` +(`fpu_store_other`: the outcomes of `Fl4Spec.fl4FpuToFrame`). -/ +theorem JAtZB.copyFrameF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (src f : U64) (hf8 : f.val % 8 = 0) (hfit : f.val + 2696 ≤ 2 ^ 64) + (hApR : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → f.val + 2696 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ f.val) + (hDP : ∀ a : Nat, f.val ≤ a → a < f.val + 2696 → _root_.Fl4Proofs.P3.UFr σ a) : + tcb.copy_words st s src f 337#u64 ⦃ s' => + ∃ um : Fl4Spec.Word → Fl4Spec.Word8, + (∀ a : Fl4Spec.Word, 2696 ≤ (a - f.bv).toNat → um a = σ.machineState.underlyingMemory a) ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst D s' a (Endp.setUm σ um) ∧ + NatFoot (L := L) Z s σ s' (Endp.setUm σ um) ⦄ := by + refine WP.spec_mono (copy_words_inv (fun s' => L.ksb s' = L.ksb s ∧ + ∃ um : Fl4Spec.Word → Fl4Spec.Word8, + (∀ a : Fl4Spec.Word, 2696 ≤ (a - f.bv).toNat → um a = σ.machineState.underlyingMemory a) ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst D s' a (Endp.setUm σ um) ∧ + NatFoot (L := L) Z s σ s' (Endp.setUm σ um)) s src f 337#u64 + ⟨rfl, σ.machineState.underlyingMemory, fun _ _ => rfl, J, NatFoot.refl _ _ _⟩ ?_) (fun _ h => h.2) + rintro s2 i ⟨hk2, um, hag, J2, F2⟩ hi v + have hi' : i.val < 337 := hi + set w := core.num.U64.wrapping_add f (core.num.U64.wrapping_mul i 8#u64) with hw + have hwv : w.val = f.val + 8 * i.val := by + rw [hw] + simp only [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq] + simp [U64.size, U64.numBits] + omega + have hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → w.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ w.val := + fun q k hq hk => by rcases hApR q k hq hk with e | e <;> omega + obtain ⟨o, vms, hko, g1, g2⟩ := ufr_word (σ := σ) (w := w) (by omega) (hDP w.val (by omega) (by omega)) + have hwB : (w, w.bv) ∈ Z.B := J2.fcov o vms hko w (by omega) (by omega) (by omega) + have hObj : ∃ o ko, (Endp.setUm σ um).kheap o = some ko ∧ o.toNat ≤ (w, w.bv).1.val ∧ + (w, w.bv).1.val < o.toNat + 2 ^ Fl4Spec.objBits ko := + ⟨o, _, hko, by show o.toNat ≤ w.val; omega, + by show w.val < o.toNat + 2 ^ Fl4Spec.pageBitsForSize vms; omega⟩ + have J3 := J2.userWrJ J2.xferH.2.2 hwB hAp hObj v + have F3 := J2.userWrN J2.xferH.2.2 hwB hAp v + refine ⟨by rw [L.ksb_wr, hk2], Endp.storeMem um w.bv v.bv, fun b hb => ?_, J3, J.natTrans F2 F3⟩ + have hwv' : w.val = (f.bv.toNat + 8 * i.val) % 2 ^ 64 := by + rw [hwv]; exact (Nat.mod_eq_of_lt (by show f.val + 8 * i.val < 2 ^ 64; omega)).symm + rw [fpu_store_other um f.bv w (8 * i.val) (by omega) hwv' v.bv b hb] + exact hag b hb + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrGetBp.lean b/proofs/Fl4Proofs/P3/JAtZBThrGetBp.lean new file mode 100644 index 000000000..1a0813e30 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrGetBp.lean @@ -0,0 +1,191 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **GetBreakpoint**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_threxttop.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrExtAux +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 32000000 in +/-- **The GetBreakpoint label at `JAt`** (as `tcb_get_bpX`): the breakpoint +number's check, the caller's restart (`JAtZ.restartJ`), the read (`GetBpHyp` +[P]), then the data tail of the five words, each step at `RInv` (for +`XTop`'s facts, `data_tail_core`) and at `JAtZ` (`JAtZ.setmrJ`, the buffer +word `MR4` by `JAtZ.userWrJ` with `JAtZ.bufWord`; `JAtZ.replyTailJ`). -/ +theorem JAtZB.tcbGetBpF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) + (p : U64) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .getBreakpoint) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (buffer : Option Fl4Spec.ObjRef) + (hA : ArgRel (st := st) s σ ct buf buffer 1) (hG : GetBpHyp (st := st) s ct p) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + tcb.decode st arI s ar fl4_abi.label.TCBGetBreakpoint p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s ar σ call + (specDec false slbl args ci sl (.Thread p) excaps buffer) ct res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + obtain ⟨htcb, xc, hkc⟩ := hX.curTcb hcur + have hctT : ct ∈ Z.T := J.thT ct htcb + have hdec0 : specDec false slbl args ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4GetBreakpoint (Z := Unit) buffer p.bv) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [decode_getbp_eq arI s ar p _ hstall, hdec0] + obtain ⟨bp, hbp, hbs⟩ := arg_rel hA hct hcur ⟨length, buf, call, slot, extra⟩ rfl 0#u64 (by decide) + have hsp : (Fl4Spec.decodeFl4GetBreakpoint (Z := Unit) buffer p.bv).run σ = + (Fl4Spec.fl4CheckBpNum (Z := Unit) bp.bv >>= fun _ => + Fl4Spec.NonDetE.returnOk (Fl4Spec.Fl4TcbInvocation.getBreakpoint p.bv bp.bv)).run σ := by + unfold Fl4Spec.decodeFl4GetBreakpoint + exact run_bind_ok hbs _ + unfold tcb.decode_get_breakpoint + rw [hct]; simp only [bind_ok]; rw [hbp]; simp only [bind_ok]; rw [check_bp_eq] + by_cases hb : bp ≥ 4#u64 + · simp only [hb, ↓reduceIte, 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.inl ⟨_, rfl, rfl, rfl, _, ?_, bp_err⟩) + apply run_liftME_err + rw [hsp] + exact run_bind_error (by rw [checkBp_run, if_pos hb]) _ + simp only [hb, ↓reduceIte, core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + obtain ⟨s1, σ1, x0, hrc1, hst1, hk0, -, hσ1, -, -, hcu1, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + rw [hkc] at hk0; cases hk0 + set xr : Fl4Spec.Tcb := { xc with tcbState := .restart } + have hkc1 : σ1.kheap ct.bv = some (.tcb xr) := by + rw [hσ1, Notif.stsAct_kheap]; simp [Notif.updK, xr] + have hcurY : ct.bv = σ1.curThread := by rw [hcu1, hcur] + have hX1 := J1.xtop + have hact1 : OutsideObjects Z.th (L.nodeAt s1 (L.cpu s1) layout.node.SCHED_ACTION) := J1.kern.act + rw [hrc1]; simp only [bind_ok] + obtain ⟨r1, hr1⟩ := hG s1 bp hs1 + rw [hr1]; simp only [bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4GetBreakpoint (Z := Unit) buffer p.bv)).run σ = + ({(.ok (.invokeFl4TCB (.getBreakpoint p.bv bp.bv)), σ)}, False) := by + apply run_liftME_ok + rw [hsp, run_bind_ok (by rw [checkBp_run, if_neg hb])] + rfl + have hy : ∀ rr : List Fl4Spec.Data, rr.length = 5 → (.ok rr, σ1) ∈ ((Fl4Spec.invokeFl4Tcb (Z := Unit) + (.getBreakpoint p.bv bp.bv)).run σ1).1 := + fun rr h => mem_liftE (x := (rr, σ1)) ⟨h, rfl⟩ + refine WP.spec_bind (Pₘ := fun x : core.result.Result Unit error.Exc × S => + ThrPostRF (L := L) arI AR Z H Cs s ar σ call (Fl4Spec.NonDetE.liftME + Fl4Spec.Fl4Invocation.invokeFl4TCB (Fl4Spec.decodeFl4GetBreakpoint (Z := Unit) buffer p.bv)) ct + (x.1, x.2, ar)) ?_ fun x hx => (WP.spec_ok _).2 hx + cases call + · simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (WP.spec_and (nodata_tail_core hX1 hcurY hkc1 rfl hact1 [0, 0, 0, 0, 0]) + (J1.runJ hctT hkc1 rfl)) ?_ + rintro s2 ⟨⟨hne, z, hz, hRT, -⟩, Lst', J5, F5⟩ + refine (WP.spec_ok _).2 (Or.inr ⟨rfl, hne, _, DecOut.of_eq hdec, σ1, hst1, _, σ1, hy _ rfl, z, hz, hRT, ?_⟩) + have hrs1 : xr.tcbState = .restart := rfl + unfold specTail at hz + rw [nd_bind_one (Notif.getThreadState_eq hkc1)] at hz + simp only [hrs1] at hz + rw [Endp.nd_when_false (by simp), Notif.nd_pure_bind', Notif.setThreadState_eq .running hkc1] at hz + rw [Set.mem_singleton_iff.1 hz] + exact ⟨Z.vs, Ac1, Lst', D1, by simpa using J5, J.natTrans F1 F5⟩ + · simp only [↓reduceIte] + have hOK1 := J1.xferH.1 + obtain ⟨x', hk', hwx⟩ := J1.buf ct hctT + rw [hkc1] at hk'; cases hk' + refine WP.spec_bind (lookup_buf_full s1 σ1 hX1.rel.2.1.1 hX1.rel.2.1.2.1 htcb hkc1 hOK1 true) ?_ + rintro bf ⟨hw0, hwp⟩ + have hbm : ∀ q, Endp.ipcBufOf true xr = some q → Endp.BufMap Z.B bf q := fun q hq => + J1.xferH.2.1 ct hctT xr true q hkc1 hq bf (hwp hwx q hq) + have hBP1 := J1.xferH.2.2 + have hWOK : ∀ q, Endp.ipcBufOf true xr = some q → ∀ i : U64, i.val < 128 → + BufWordOK H σ1 (Endp.fl4BufA bf i) := fun q hq i hi => + J1.bufWordG hOK1 htcb hkc1 hq i.val hi (by rw [fl4BufA_bv, hwp hwx q hq]) + have hI0 : Vs.RInv (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR Z.G s1 ar + (MrS σ1 ct.bv xr (Endp.ipcBufOf true xr) []) := by + rw [MrS_nil hkc1]; exact hX1.toRInv + have J0 : JAtZB (L := L) arI AR Z H Cs Ac1 Lst1 D1 s1 ar (MrS σ1 ct.bv xr (Endp.ipcBufOf true xr) []) := by + rw [MrS_nil hkc1]; exact J1 + have G1 : NatFoot (L := L) Z s σ s1 (MrS σ1 ct.bv xr (Endp.ipcBufOf true xr) []) := by + rw [MrS_nil hkc1]; exact F1 + refine WP.spec_bind (WP.spec_and (setmr_reg_inv σ1 ct htcb hcurY xr _ bf s1 ar [] hI0 0#u64 rfl (by decide) + r1.vaddr) (J0.setmrJ hctT hkc1 bf hw0 hbm hBP1 hWOK 0#u64 rfl (by decide) r1.vaddr)) ?_ + rintro ⟨l2, s2⟩ ⟨⟨-, hI2, hk2, hc2⟩, -, -, V2, J2, F2⟩ + simp only at hI2 hk2 hc2 J2 F2 + have G2 := J.natTrans G1 F2.cast + refine WP.spec_bind (WP.spec_and (setmr_reg_inv σ1 ct htcb hcurY xr _ bf s2 ar _ hI2 1#u64 rfl (by simp) r1.ty) + (J2.setmrJ hctT hkc1 bf hw0 hbm (hBP1.mono hk2 fun _ _ _ h => h) hWOK 1#u64 rfl (by simp) r1.ty)) ?_ + rintro ⟨l3, s3⟩ ⟨⟨-, hI3, hk3, hc3⟩, -, -, V3, J3, F3⟩ + simp only at hI3 hk3 hc3 J3 F3 + have G3 := J.natTrans G2 F3.cast + refine WP.spec_bind (WP.spec_and (setmr_reg_inv σ1 ct htcb hcurY xr _ bf s3 ar _ hI3 2#u64 rfl (by simp) r1.size) + (J3.setmrJ hctT hkc1 bf hw0 hbm (hBP1.mono (hk3.trans hk2) fun _ _ _ h => h) hWOK 2#u64 rfl (by simp) + r1.size)) ?_ + rintro ⟨l4, s4⟩ ⟨⟨-, hI4, hk4, hc4⟩, -, -, V4, J4, F4⟩ + simp only at hI4 hk4 hc4 J4 F4 + have G4 := J.natTrans G3 F4.cast + refine WP.spec_bind (WP.spec_and (setmr_reg_inv σ1 ct htcb hcurY xr _ bf s4 ar _ hI4 3#u64 rfl (by simp) r1.rw) + (J4.setmrJ hctT hkc1 bf hw0 hbm (hBP1.mono (hk4.trans (hk3.trans hk2)) fun _ _ _ h => h) hWOK 3#u64 rfl + (by simp) r1.rw)) ?_ + rintro ⟨l5, s5⟩ ⟨⟨-, hI5, hk5, hc5⟩, -, -, V5, J5, F5⟩ + simp only at hI5 hk5 hc5 J5 F5 + have G5 := J.natTrans G4 F5.cast + have hks5 : L.ksb s5 = L.ksb s1 := hk5.trans (hk4.trans (hk3.trans hk2)) + have hcp5 : L.cpu s5 = L.cpu s1 := hc5.trans (hc4.trans (hc3.trans hc2)) + have hBP5 : Vs.BufPlaced (L := L) Z.N Z.C Z.B Z.c s5 σ1 := hBP1.mono hks5 (fun _ _ _ h => h) + have tail : ∀ v : U64, (do + let (len, s6) ← msg.set_mr st s5 ct bf 4#u64 v + let s7 ← tcb.reply_words st s6 ct len + let s8 ← sched.set_thread_state st s7 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s8)) ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s ar σ true (Fl4Spec.NonDetE.liftME + Fl4Spec.Fl4Invocation.invokeFl4TCB (Fl4Spec.decodeFl4GetBreakpoint (Z := Unit) buffer p.bv)) ct + (res.1, res.2, ar) ⦄ := by + intro v + refine WP.spec_bind (WP.spec_and (Vs.setmr_inv σ1 ct htcb hcurY hkc1 _ bf hw0 hbm s5 ar _ hI5 hBP5 4#u64 rfl + (by simp) v) (J5.setmrJ hctT hkc1 bf hw0 hbm hBP5 hWOK 4#u64 rfl (by simp) v)) ?_ + rintro ⟨len, s6⟩ ⟨⟨hlen, hI6, hk6, hc6⟩, -, -, V6, J6, F6⟩ + simp only at hlen hI6 hk6 hc6 J6 F6 + have G6 := J.natTrans G5 F6.cast + have hfr : ∀ q, Endp.ipcBufOf true xr = some q → ∀ z ∈ Endp.mrPairs + ([] ++ [r1.vaddr.bv] ++ [r1.ty.bv] ++ [r1.size.bv] ++ [r1.rw.bv] ++ [v.bv]), + (Endp.bufAddr q z.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr q z.1) σ1 := + fun q hq z hz => ⟨Endp.bufAddr_aligned hX1.fin.1.2.ep (hbm q hq) z.1, + buf_frame_of_ok hOK1 hkc1 hq z.1 (mrPairs_lt hz)⟩ + have hl5 : ([] ++ [r1.vaddr.bv] ++ [r1.ty.bv] ++ [r1.size.bv] ++ [r1.rw.bv] ++ [v.bv]).length = 5 := rfl + have hn128 : len.val < 128 := by rw [hlen]; cases Endp.ipcBufOf true xr <;> simp [mrLen] + show (do + let s7 ← tcb.reply_words st s6 ct len + let s8 ← sched.set_thread_state st s7 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s8)) ⦃ _ ⦄ + rw [← Std.bind_assoc] + refine WP.spec_bind (WP.spec_and (data_tail_core hX1 hcurY hkc1 rfl hact1 hI6 (hk6.trans hks5) + (hc6.trans hcp5) (by rw [hl5]; decide) hfr len (by rw [hlen]; simp) hn128) + (J6.replyTailJ hctT hkc1 rfl (by rw [hl5]; decide) hfr len (by rw [hlen]; simp) hn128)) ?_ + rintro s8 ⟨⟨hne, z, hz, hRT, -⟩, hJ8⟩ + obtain ⟨V8, Lst8, J8, F8⟩ := hJ8 z hz + exact (WP.spec_ok _).2 (Or.inr ⟨rfl, hne, _, DecOut.of_eq hdec, σ1, hst1, _, σ1, hy _ hl5, z, hz, hRT, + V8, Ac1, Lst8, D1, J8, J.natTrans G6 F8.cast⟩) + split + · exact tail 1#u64 + · exact tail 0#u64 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrMcp.lean b/proofs/Fl4Proofs/P3/JAtZBThrMcp.lean new file mode 100644 index 000000000..9fb9e45d4 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrMcp.lean @@ -0,0 +1,161 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **SetMCPriority**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrSched +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The SetMCPriority label at `JAt`** (as `tcb_set_mcpX`): the decode with +the authority's MCP by `JAtZ`'s `McpRel` and the authority among the TCBs +(`hcapT`, from the slots' caps), the caller's restart (`JAtZ.restartJ`), the +target's `MCP` word and `tcb_mcpriority` (`JAtZ.mcpWrJ`). -/ +theorem JAtZB.tcbSetMcpF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetMCPriority) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hcapT : x0 ≠ 0#u64 → ∀ q, capV (L.mem s) x0 = .Thread q → q ∈ Z.T) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBSetMCPriority p ⟨length, buf, call, cslotAddr ssl, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + have hM := J.mcp + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetMcpriority (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_mcp_eq arI s ar p _ hstall, hdec0] + unfold tcb.decode_set_priority tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + by_cases h0 : x0 = 0#u64 + · -- no extra cap: truncated + have hl : excaps.length ≤ 0 := ((hXc.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetMcpriority + rw [whenE_pos (Or.inr (by omega))] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + obtain ⟨e, he, hxe, he1, ps, hps, hps1⟩ := hXc.get hx0' h0 + have hexl : excaps.length ≠ 0 := by + have := List.length_pos_of_ne_nil (List.ne_nil_of_mem (List.mem_of_getElem? he)); omega + by_cases h1 : length >= 1#u64 + swap + · simp only [h0, ↓reduceIte, if_neg h1, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetMcpriority + rw [whenE_pos (Or.inl (show (args.map UScalar.bv).length = 0 by rw [List.length_map, ← hlen]; scalar_tac))] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | y0 :: rest => exact ⟨y0, rest, rfl⟩ + have harg := tcb_arg_eq s ⟨length, buf, call, cslotAddr ssl, extra⟩ hct (hargs 0#u64 y0 (by simp)) + simp only [h0, ↓reduceIte, if_pos h1, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, harg] + by_cases hth : ∃ q, capV (L.mem s) x0 = .Thread q + · obtain ⟨q, hq⟩ := hth + rw [thread_arg_thread s 1#u64 hq] + simp only [bind_ok, check_prio_eq] + obtain ⟨xq, hkq, hmq, -⟩ := hM q (hcapT h0 q hq) + have hu : (Fl4Spec.ucast 64 xq.tcbMcpriority) = (L.mem s (mcpA q)).bv := by + apply BitVec.eq_of_toNat_eq; simp [Fl4Spec.ucast, hmq] + have := xq.tcbMcpriority.isLt; omega + have hspec1 : (Fl4Spec.decodeSetMcpriority (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps).run σ = (Fl4Spec.NonDetE.whenE (y0.bv > (L.mem s (mcpA q)).bv) + (Fl4Spec.NonDetE.throwError (.rangeError 0 (L.mem s (mcpA q)).bv)) >>= fun _ => + (Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl none + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) none none) : Fl4Spec.SEMonad Unit _)).run σ := by + have he1' : e.1 = .threadCap q.bv := by rw [he1, hq]; rfl + unfold Fl4Spec.decodeSetMcpriority + rw [whenE_neg (by simp [hexl]), retOk_bind, retOk_bind, he, the_some, he1'] + show ((Fl4Spec.NonDetE.returnOk q.bv : Fl4Spec.SEMonad Unit _) >>= _).run σ = _ + rw [retOk_bind] + have hcp : (Fl4Spec.checkPrio (Z := Unit) (Fl4Spec.the (List.map UScalar.bv (y0 :: rest))[0]?) q.bv).run σ = + (Fl4Spec.NonDetE.whenE (y0.bv > (L.mem s (mcpA q)).bv) + (Fl4Spec.NonDetE.throwError (.rangeError 0 (L.mem s (mcpA q)).bv)) : Fl4Spec.SEMonad Unit Unit).run σ := by + unfold Fl4Spec.checkPrio + rw [lE_bind (Notif.threadGet_eq _ hkq), hu]; rfl + rw [ExceptT.run_bind, nd_bind_congr hcp, ExceptT.run_bind]; rfl + by_cases hgt : y0 > L.mem s (mcpA q) + · rw [ite_eq_left hgt] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .rangeError 0 (L.mem s (mcpA q)).bv, ?_, Or.inl ⟨rfl, rfl⟩⟩) + apply run_liftME_err + rw [hspec1, whenE_pos (by rw [GT.gt, ← u64_lt_bv]; exact hgt)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 _) : + Fl4Spec.SEMonad Unit Unit)) rfl _ + · rw [ite_eq_right hgt] + simp only [bind_ok] + obtain ⟨s1, σ1, x1, hrc, hst1, -, -, -, -, -, -, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + rw [hrc]; simp only [bind_ok, tcs_mcp] + refine (WP.spec_ok _).2 ?_ + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetMcpriority (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)).run σ = ({(.ok (.invokeTCB (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl none + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) none none)), σ)}, False) := by + apply run_liftME_ok + rw [hspec1, whenE_neg (by rw [GT.gt, ← u64_lt_bv]; exact hgt), retOk_bind]; rfl + have hlt : y0.val < 256 := by + have := xq.tcbMcpriority.isLt + have h2 : ¬ (L.mem s (mcpA q)).val < y0.val := hgt + omega + obtain ⟨x, hkx, -⟩ := J1.xtop.fin.1.1.2.1.tcb p hpT + obtain ⟨V2, J2, F2⟩ := J1.mcpWrF hpT hkx y0 (Fl4Spec.ucast 8 y0.bv) + (by rw [Ipc.ucast8_specPrio, Sched.specPrio_toNat hlt]) + refine Or.inr ⟨_, DecOut.of_eq hdec, rfl, rfl, σ1, hst1, _, ?_, V2, Ac1, Lst1, D1, J2, J.natTrans F1 F2.cast⟩ + show _ ∈ ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB _)).run σ1).1 + rw [invoke_mcp_run hkx]; exact Set.mem_singleton _ + · have hne : ∀ r, absCap (capV (L.mem s) x0) ≠ .threadCap r := fun r h => by + obtain ⟨q, hq, -⟩ := absCap_thread h; exact hth ⟨q, hq⟩ + rw [thread_arg_other s 1#u64 (fun q h => hth ⟨q, h⟩)] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetMcpriority + rw [whenE_neg (by simp [hexl]), retOk_bind, retOk_bind, he, the_some, he1] + generalize absCap (capV (L.mem s) x0) = cc at hne ⊢ + cases cc + all_goals first + | exact absurd rfl (hne _) + | exact run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidCapability 1) : + Fl4Spec.SEMonad Unit Fl4Spec.ObjRef)) rfl _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrPost.lean b/proofs/Fl4Proofs/P3/JAtZBThrPost.lean new file mode 100644 index 000000000..c4d4e987e --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrPost.lean @@ -0,0 +1,144 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **the label post over `JAtZB` with `NatFoot`, and the caller's +restart in that form**. + +* `ThrPost`: what `tcb::decode` at a label leaves, over `JAtZB`: either the error arm (store and arch unchanged, the + decoder's single outcome a related error) or the decoder's invocation, the spec's caller restart, an `.ok []` outcome + of the native `invokeTcb`, and `JAtZB` with the native footprint `NatFoot` of the whole call. The VM adapter is + `VmEptState.thrPost` (P3/VmEntryS3ThrAdapt). None of the route-F labels preempts. +* `JAtZB.restartCallerF`: `JAtZ.restartJ` (`tcb::restart_caller`) over `JAtZB`, from `JAtZB.restartRunJ`. + +No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointBudgetF +import Fl4Proofs.P3.XTopJointTcbInstall +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.XTopJointStep +import Fl4Proofs.P3.XTopJointMRTcbL +import Fl4Proofs.P3.JAtZBThrSched +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The TCB invocations that install no cap: the extended perform `invokeTcbVF` is the native one on them +(`thrPost`'s adapter proves it). -/ +def NativeTcbInv : Fl4Spec.TcbInvocation → Prop + | .threadControlCaps .. => False + | .threadControlSched _ _ fh _ _ _ => fh = none + | _ => True + +/-- The post of a route-F Thread label (module docstring). -/ +def ThrPost (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) (a : A) + (σ : Fl4Spec.State Unit) (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ res.2.2 = a ∧ + ∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∨ + (∃ i : Fl4Spec.TcbInvocation, DecOut m σ (.invokeTCB i) ∧ NativeTcbInv i ∧ + res.1 = .Ok () ∧ ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ', ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.invokeTcb (Z := Unit) i).run σ1).1 ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 σ' ∧ + NatFoot (L := L) Z s σ res.2.1 σ') + +/-- The post of a route-F Thread label that replies inside the decoder (`ReadRegisters`): the error arm, or the +caller no longer `Restart`, an `.ok rr` outcome of the native `invokeTcb`, the spec's success tail and `JAtZB` with +the native footprint of the whole call. -/ +def ThrPostR (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) (a : A) + (σ : Fl4Spec.State Unit) (call : Bool) (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) (ct : U64) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ res.2.2 = a ∧ + ∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∨ + (res.1 = .Ok () ∧ L.mem res.2.1 (stA ct) ≠ sched.TS_RESTART ∧ + ∃ i : Fl4Spec.TcbInvocation, DecOut m σ (.invokeTCB i) ∧ NativeTcbInv i ∧ + ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ rr σy, ((.ok rr, σy) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.invokeTcb (Z := Unit) i).run σ1).1 ∧ + ∃ z ∈ (specTail call ct.bv rr σy).1, RelTopM (L := L) res.2.1 z.2 ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 z.2 ∧ + NatFoot (L := L) Z s σ res.2.1 z.2) + +/-- The error arm of `ThrPost`: the state unchanged. -/ +theorem ThrPost.err {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation} {e : error.Exc} {e' : Fl4Spec.SyscallError} + (h : m.run σ = ({(.error e', σ)}, False)) (hr : ErrRel e e') : + ThrPost (L := L) arI AR Z H Cs s a σ m (.Err e, s, a) := + Or.inl ⟨e, rfl, rfl, rfl, e', h, hr⟩ + +/-- `JAtZ.stallT` over `JAtZB`: every thread of `T` is on this core, so its remote stall is the no-op. -/ +theorem JAtZB.stallT (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ q ∈ Z.T, smp.remote_tcb_stall st arI s a q = ok (s, a) := + fun _ hq => spec_eq_ok (remote_tcb_stall_id (ar := arI) a J.xtop.fin.1.1.1 hq) + +/-- `JAtZ.ne_idle` over `JAtZB`. -/ +theorem JAtZB.ne_idle (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p : U64} (hpI : p ≠ L.idleW s) : + p.bv ≠ σ.idleThread := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have ei : L.idleW s = nodeW st s Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW nodeW; rw [hc.cur] + intro e + exact hpI (Eq.symm (by rw [ei]; apply P1.eq_of_bv; rw [← hs.idleTh, ← e])) + +/-- **A kheap-preserving scheduler step is a native step** (`SchW` words). -/ +theorem JAtZB.schedStepN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hS : SchedStep (L := L) Z.th s σ s' σ') : NatFoot (L := L) Z s σ s' σ' := by + have hW := (hS.2.1 Z.N Z.T Z.C Z.c J.thT J.xtop.fin.1.1.1 J.xtop.fin.1.1.2.2.1).2.1 + have hT : P1.TcbPlaced Z.T := J.ip.e.tcb + refine NatFoot.ofW J.xtop hW (fun _ h => schW_oobj J.xtop.fin.1.1.1.placed.tcb h) (fun _ h => J.schW_nv h) + (fun b h => ?_) (NatK.of_kheap hS.1.kh) + rcases h with ⟨y, hy, rfl | rfl | rfl⟩ | h | h + · have e := tfield_val (hT y hy) (K := 3480#u64) (by simp) + exact J.tcbW_nh hy (by rw [e]; omega) (by rw [e]; simp) + · have e := tfield_val (hT y hy) (K := 3488#u64) (by simp) + exact J.tcbW_nh hy (by rw [e]; omega) (by rw [e]; simp) + · have e := tfield_val (hT y hy) (K := 3592#u64) (by simp) + exact J.tcbW_nh hy (by rw [e]; omega) (by rw [e]; simp) + · exact J.tfoot_nh (Or.inl (Sched.KFoot.of_sfoot (Or.inr ⟨h.1, h.2.1⟩))) + · obtain ⟨K, hK, eK⟩ := h + intro e + have hv : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + rw [e, hv] at eK + simp only [List.mem_cons, List.not_mem_nil, or_false] at hK + omega + +/-- **`tcb::restart_caller` over `JAtZB`** (module docstring). -/ +theorem JAtZB.restartCallerF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) : + ∃ s1 σ1 x, tcb.restart_caller st s = ok s1 ∧ + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + σ.kheap ct.bv = some (.tcb x) ∧ σ.curThread = ct.bv ∧ + σ1 = Notif.stsAct (Notif.updK σ ct.bv (.tcb { x with tcbState := .restart })) ct.bv ∧ + (∀ u, σ1.schedulerAction = .switchThread u → False) ∧ σ1.idleThread = σ.idleThread ∧ + σ1.curThread = σ.curThread ∧ sched.set_thread_state st s ct sched.TS_RESTART = ok s1 ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' s1 a σ1 ∧ + NatFoot (L := L) Z s σ s1 σ1 := by + obtain ⟨hcur, x, hk, hrun⟩ := J.curRunnable hni hct + obtain ⟨s1, σ1, hst, e, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartRunJ hct hk hrun + have hrc : tcb.restart_caller st s = ok s1 := by + simp [tcb.restart_caller, hct, bind_ok, hs1] + refine ⟨s1, σ1, x, hrc, hst, hk, hcur, e, ?_, ?_, ?_, hs1, Ac1, Lst1, D1, J1, F1⟩ + · subst e + intro u e + unfold Notif.stsAct at e + split_ifs at e + all_goals first + | (have e2 : σ.schedulerAction = .switchThread u := e + rw [hni.2] at e2; cases e2) + | cases e + · subst e; unfold Notif.stsAct; split_ifs <;> rfl + · subst e; exact (stsAct_fields _ _).2.1 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrPostF.lean b/proofs/Fl4Proofs/P3/JAtZBThrPostF.lean new file mode 100644 index 000000000..6c0e54453 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrPostF.lean @@ -0,0 +1,112 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **the label posts of FL4's TCB extension labels** (SetFlags, the +debug, timer and FPU labels: `invokeFl4TCB`), over `JAtZB` with `NatFoot`. + +* `ThrPostF`: as `ThrPost` (P3/JAtZBThrPost) with the decoder's invocation an `invokeFl4TCB j` and the outcome one of + FL4's `invokeFl4Tcb j`. +* `ThrPostRF`: as `ThrPostR` for the extension labels that reply inside the decoder. + +The VM adapters are `VmEptState.thrPostF` / `thrPostRF` (P3/VmEntryS3ThrAdapt). No premise. New file; no existing +statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.XTopJointMidRunS +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The post of a route-F TCB extension label (module docstring). -/ +def ThrPostF (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) (a : A) + (σ : Fl4Spec.State Unit) (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ res.2.2 = a ∧ + ∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∨ + (∃ j : Fl4Spec.Fl4TcbInvocation, DecOut m σ (.invokeFl4TCB j) ∧ + res.1 = .Ok () ∧ ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ', ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.invokeFl4Tcb (Z := Unit) j).run σ1).1 ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 σ' ∧ + NatFoot (L := L) Z s σ res.2.1 σ') + +/-- The post of a route-F TCB extension label replying inside the decoder (module docstring). -/ +def ThrPostRF (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) + (a : A) (σ : Fl4Spec.State Unit) (call : Bool) (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) (ct : U64) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ res.2.2 = a ∧ + ∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∨ + (res.1 = .Ok () ∧ L.mem res.2.1 (stA ct) ≠ sched.TS_RESTART ∧ + ∃ j : Fl4Spec.Fl4TcbInvocation, DecOut m σ (.invokeFl4TCB j) ∧ + ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ rr σy, ((.ok rr, σy) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.invokeFl4Tcb (Z := Unit) j).run σ1).1 ∧ + ∃ z ∈ (specTail call ct.bv rr σy).1, RelTopM (L := L) res.2.1 z.2 ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 z.2 ∧ + NatFoot (L := L) Z s σ res.2.1 z.2) + +/-- `JAtZB` at the sets `Z` with some VSpace sets, with the native footprint from `(s0, σ0)`: the state predicate +the FPU labels hand to the native `fpu_refinesX`. -/ +def JBN (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s0 : S) + (σ0 : Fl4Spec.State Unit) (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) : Prop := + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' s' a' σ' ∧ NatFoot (L := L) Z s0 σ0 s' σ' + +/-- `ThrPostF` with `JBN` in the error arm (the FPU labels: the native decode's error arm keeps the store but +states the arch value only through its state predicate). -/ +def ThrPostFX (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) + (σ : Fl4Spec.State Unit) (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + (∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ (∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∧ + JBN (L := L) arI AR Z H Cs s σ res.2.1 res.2.2 σ) ∨ + (∃ j : Fl4Spec.Fl4TcbInvocation, DecOut m σ (.invokeFl4TCB j) ∧ + res.1 = .Ok () ∧ ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ', ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.invokeFl4Tcb (Z := Unit) j).run σ1).1 ∧ + JBN (L := L) arI AR Z H Cs s σ res.2.1 res.2.2 σ') + +/-- FL4's TCB extension invocations never fail: every outcome is `.ok`. -/ +theorem invokeFl4Tcb_ok {j : Fl4Spec.Fl4TcbInvocation} {σ1 : Fl4Spec.State Unit} + {y : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit} + (h : y ∈ ((Fl4Spec.invokeFl4Tcb (Z := Unit) j).run σ1).1) : ∃ v, y.1 = .ok v := by + cases j with + | setBreakpoint => exact ⟨[], by rw [(h : y = (.ok [], σ1))]⟩ + | unsetBreakpoint => exact ⟨[], by rw [(h : y = (.ok [], σ1))]⟩ + | fpuTransfer t toFrame frame => + cases toFrame + · exact ⟨[], by rw [(h : y = (.ok [], σ1))]⟩ + · obtain ⟨x, -, rfl⟩ := MidRun.mem_liftE_inv' h; exact ⟨_, rfl⟩ + | _ => obtain ⟨x, -, rfl⟩ := MidRun.mem_liftE_inv' h; exact ⟨_, rfl⟩ + +/-- **A native `TopPostDMX` over `JBN` of a TCB extension decode is a `ThrPostFX`** (the invocation an +`invokeFl4TCB`, P3/VsDomF's `decOut_liftME_inv`; no preempted outcome, `invokeFl4Tcb_ok`). -/ +theorem ThrPostFX.of_dmx {m0 : Fl4Spec.SEMonad Unit Fl4Spec.Fl4TcbInvocation} {blk call don : Bool} + {res : core.result.Result Unit error.Exc × S × A} + (h : TopPostDMX (L := L) (JBN (L := L) arI AR Z H Cs s σ) σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB m0) s res) : + ThrPostFX (L := L) arI AR Z H Cs s σ (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB m0) res := by + rcases h with ⟨e, h1, h2, h3, hX⟩ | ⟨inv, hD, σ1, hst, ⟨r, σ'⟩, hy, ⟨th', G', hc, _⟩, hX⟩ + · exact Or.inl ⟨e, h1, h2, h3, hX⟩ + · obtain ⟨j, rfl⟩ := decOut_liftME_inv hD + have hy' : (r, σ') ∈ ((Fl4Spec.invokeFl4Tcb (Z := Unit) j).run σ1).1 := hy + rcases hc with ⟨h1, h2⟩ | ⟨-, h2⟩ + · simp only at h2 hX + subst h2 + exact Or.inr ⟨j, hD, h1, σ1, hst, σ', hy', hX⟩ + · obtain ⟨v, hv⟩ := invokeFl4Tcb_ok hy' + simp only at h2 hv + rw [hv] at h2; cases h2 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrPrio.lean b/proofs/Fl4Proofs/P3/JAtZBThrPrio.lean new file mode 100644 index 000000000..b3314a3dc --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrPrio.lean @@ -0,0 +1,213 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **`set_priority`**. + +Generated by `handoff/tools/gen_thrprim.py` (`JAtZB` siblings with the native footprint `NatFoot`). New file; no +existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointTcbPrio +import Fl4Proofs.P3.NatFoot +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +/-- The priority change's spec frame is a native spec step (no thread state changes). -/ +theorem natK_pfr {t : Fl4Spec.ObjRef} {σ σ' : Fl4Spec.State Unit} (F : PFr t σ σ') : NatK σ σ' := + natK_kh (R := fun p o o' => PChg t p o o') (fun _ _ _ h => by + rcases h with ⟨x, pr, rfl, rfl, -⟩ | ⟨x, y, rfl, rfl, -⟩ | ⟨x, y, rfl, rfl, -⟩ + · exact zvChg_tcb fun h => h + · exact zvChg_ep + · exact zvChg_ntfn) F.kh + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead across a `PFr` step** with the footprint `CnW` +(XTopJointCancel's `deadP_kfr`: a dead TCB keeps its state; a dead endpoint +or notification holds nobody, so is kept). -/ +theorem JAtZB.deadP_pfr (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : Fl4Spec.ObjRef} {s3 : S} + {σ3 : Fl4Spec.State Unit} (F : PFr t σ σ3) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx h0 => ?_) (fun x hx => ?_) (fun x hx => ?_) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hw hz hqC hK8 hK) F.sk.idle hAS hY3 (hYK.dk q.bv) hd + · obtain ⟨pr, hk3⟩ := F.tcbF hx + exact ⟨_, hk3, rfl, rfl, Or.inl rfl⟩ + · -- a dead endpoint: idle, so holding nobody + obtain ⟨y, hy, hc⟩ := F.epF hx + rcases hc with rfl | hm + · exact hy + · exfalso + obtain ⟨e', he', e⟩ := J.obj.ep q.bv x hx + have heq : e' = q := P1.eq_of_bv e + subst heq + have d2 := hd.2.1 (J.zH.2.1 e' he') + obtain ⟨x', hk', hO⟩ := J.xtop.ipc.toE.ep e' he' + rw [hx] at hk'; cases hk' + rw [epState_eq] at d2 + cases x with + | idleEP => simp [Fl4Spec.Endpoint.epQueue] at hm + | sendEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + | recvEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + · -- a dead notification: not waiting, so holding nobody + obtain ⟨n', hn', e⟩ := J.obj.ntfn q.bv x hx + have hnq : n' = q := P1.eq_of_bv e + subst hnq + obtain ⟨d1, -, -⟩ := hd.2.2.1 (J.zH.2.2.2 n' hn') + obtain ⟨x', hk', hR⟩ := J.xtop.ipc.toN.ntfn n' hn' + rw [hx] at hk'; cases hk' + obtain ⟨y, hy, hsc', hbd, hc⟩ := F.ntfnF hx + have hnw : ∀ l, x.ntfnObj ≠ .waitingNtfn l := by + intro l e + have hO := hR.obj + rw [e] at hO + exact d1 (by rw [epState_eq]; exact hO.1) + refine ⟨y, hy, ?_, hbd, Or.inl hsc'⟩ + rcases hc with rfl | hm + · exact Or.inl rfl + · exfalso + cases h : x.ntfnObj with + | waitingNtfn l => exact hnw l h + | idleNtfn => rw [Fl4Spec.ntfnQueueOf, h] at hm; simp [Fl4Spec.Ntfn.ntfnQueue] at hm + | activeNtfn b => rw [Fl4Spec.ntfnQueueOf, h] at hm; simp [Fl4Spec.Ntfn.ntfnQueue] at hm + +set_option maxHeartbeats 4000000 in +/-- **The current thread's facts `JAtZ.setPrioJ` takes** when it is the +target, under `CurInv` and the current thread not the +idle one: in no release queue, bound to the current SC, which is active +(`CurInv.active`: the current SC is not the idle SC, since `Inv.back` names +the idle thread there, `IdleOK`); FL4's release bit clear, its SC word +nonzero and among P1's active SCs (`SCsOK.act`, `SchedRel.act_of`). -/ +theorem JAtZB.prioCur (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hne : σ.curThread ≠ σ.idleThread) + (hC : CurInv σ) {t : U64} (ht : t ∈ Z.T) : + (σ.curThread = t.bv → t.bv ∉ σ.releaseQueue ∧ ∃ x scp y n, σ.kheap t.bv = some (.tcb x) ∧ + x.tcbSchedContext = some scp ∧ σ.kheap scp = some (.schedContext y n) ∧ Fl4Spec.scActive y = true) ∧ + (t = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) → + (L.mem s (P1.inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (P1.scA t) ≠ 0#u64 ∧ L.mem s (P1.scA t) ∈ Ac) := by + obtain ⟨xt, hkt, hxs, -⟩ := hC.tcb + obtain ⟨y, n, hky⟩ := hC.sc + have hnidle : σ.curSc ≠ σ.idleSc := by + intro e + obtain ⟨xi, hki, hsi, -, -⟩ := J.act.2 + obtain ⟨y1, n1, hk1, hb1⟩ := J.inv.back _ _ _ hkt hxs + obtain ⟨y2, n2, hk2, hb2⟩ := J.inv.back _ _ _ hki hsi + rw [e] at hk1 + rw [hk1] at hk2; cases hk2 + exact hne (Option.some.inj (hb1.symm.trans hb2)) + have hact : Fl4Spec.scActive y = true := hC.active hnidle y n hky + have A : σ.curThread = t.bv → t.bv ∉ σ.releaseQueue ∧ ∃ x scp y n, σ.kheap t.bv = some (.tcb x) ∧ + x.tcbSchedContext = some scp ∧ σ.kheap scp = some (.schedContext y n) ∧ Fl4Spec.scActive y = true := + fun e => ⟨fun hm => hC.notRel (by rw [e]; exact hm), xt, σ.curSc, y, n, by rw [← e]; exact hkt, hxs, hky, hact⟩ + refine ⟨A, fun e => ?_⟩ + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hcur : σ.curThread = t.bv := by rw [e, hs.curThread, hc.cur]; rfl + obtain ⟨hnr, -⟩ := A hcur + obtain ⟨x', hk', -, -, hsc, -⟩ := hs.tcb t ht + rw [← hcur, hkt] at hk'; cases hk' + rw [hxs] at hsc + obtain ⟨h0, hb⟩ := Sched.optRef_some hsc.symm + refine ⟨?_, h0, ?_⟩ + · by_contra h0' + exact hnr (by rw [hs.rel, mem_map_bv]; exact (hc.released t ht).1 h0') + · have hmC := hs.scMem t ht h0 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hky' : σ.kheap (L.mem s (P1.scA t)).bv = some (.schedContext y n) := by rw [hb]; exact hky + have hH := (hJ.sc _ y n (J.c0 _ hmC) hky').2 + have hrm := SchedRel.act_of hS ht (by rw [← hcur]; exact hkt) hxs hky hact + exact (J.blk.2.2.2.1.act _ ⟨_, hH⟩).2 (fun h => hrm (by rw [h]; rfl)) + +set_option maxHeartbeats 8000000 in +/-- **`set_priority` at `J`** (module docstring): FL4's side +`set_priority_irelW` (`IRel` after, the footprint `CnW`) with P1's `BlkOK` +and footprint (`sched.set_priority.blk`); the spec's frame `PFr` +(`setPriority_pfr`, `BlkQ` by `blkQ_of_sv`), `Inv` by `setPriority_invC`, the +sorted IPC queues by `setPriority_sorted` (`QueuedOnly` by `queuedOnly_of`); +the rest as `JAtZ.kfrStep`. Premises: no switch pending, and the current +thread's facts when it is the target (`hcur`, `hP1`: `JAtZ.prioCur`). -/ +theorem JAtZB.setPrioF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hna : ∀ u, σ.schedulerAction ≠ .switchThread u) {t : U64} (ht : t ∈ Z.T) (v : U64) (hv : v.val < 256) + (hcur : σ.curThread = t.bv → t.bv ∉ σ.releaseQueue ∧ ∃ x scp y n, σ.kheap t.bv = some (.tcb x) ∧ + x.tcbSchedContext = some scp ∧ σ.kheap scp = some (.schedContext y n) ∧ Fl4Spec.scActive y = true) + (hP1 : t = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) → + (L.mem s (P1.inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ L.mem s (P1.scA t) ≠ 0#u64 ∧ L.mem s (P1.scA t) ∈ Ac) : + sched.set_priority st s t v ⦃ s' => ∃ σ', + Fl4Spec.setPriority (Z := Unit) t.bv (Fl4Spec.ucast 8 v.bv) σ = ({((), σ')}, False) ∧ + ∃ V' : XVs, JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D s' a σ' ∧ + NatFoot (L := L) Z s σ s' σ' ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hQ := qSorted_of_spec (L := L) hI.1.toE hI.1.toN hP0.e.tcb J.isorted + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + obtain ⟨x, hk, -⟩ := hI.1.toE.tcb t ht + refine WP.spec_mono (WP.spec_and (set_priority_irelW hP0 hI ht v hv J.inv.actReady (fun h => (hcur h).1) J.sv + J.inv J.obj (fun e he => hQ e (Or.inl he)) (fun e he => hQ e (Or.inr he))) + (P1.sched.set_priority.blk s t v J.blk htH hv (fun e => (hP1 e).1) (fun e => (hP1 e).2.1) + (fun e => (hP1 e).2.2))) ?_ + rintro s' ⟨⟨σ', hm, hIR, hw⟩, hblk, hfoot, -⟩ + have hm' : ((), σ') ∈ (Fl4Spec.setPriority (Z := Unit) t.bv (Fl4Spec.ucast 8 v.bv) σ).1 := by + rw [hm]; exact Set.mem_singleton _ + have hBQ := blkQ_of_sv J.sv hk + have F : PFr t.bv σ σ' := setPriority_pfr hk _ hna hBQ J.sv _ hm' + have I3 : Inv σ' := (setPriority_invC J.inv hk (fun e => by + obtain ⟨-, x', scp, y, n, hk', h1, h2, h3⟩ := hcur e.symm + rw [hk] at hk'; cases hk' + exact ⟨scp, y, n, h1, h2, h3⟩) _ hm').1 + have S3 : IpcSorted σ' := setPriority_sorted hk _ J.inv.actReady hBQ (queuedOnly_of hI J.obj ht hk) J.isorted _ hm' + refine ⟨σ', hm, ?_⟩ + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hw + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := F.actIdle J.act + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_pfr F hS3 hIR.1 hw k3 hAI3.1 (J.yld.frame k2 F.ykeep) F.ykeep + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hw) + (J.idleW_keep hS3 F.sk.idle F.sk.isc).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D) + have hRR' := J.rrecv.frame k4 F.khrb + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 F.sk.idle F.sk.isc) + (DXKeep.of_dbg hKd (J.cnW_td hw)), hkern, + J.cnW_utc hw hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, F.cur J.cur, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa, S3, F.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + refine ⟨Z.vs, J.cs_frameI hks hX3 hw (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hC R hKd F.sk.orig (J.idleW_keep hS3 F.sk.idle F.sk.isc).1 J.zD, + NatFoot.ofW J.xtop hw (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) + (fun b hb => J.cnW_nh hb) (natK_pfr F)⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrRead.lean b/proofs/Fl4Proofs/P3/JAtZBThrRead.lean new file mode 100644 index 000000000..5698e686b --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrRead.lean @@ -0,0 +1,605 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **`read_registers`**. + +Generated by `handoff/tools/gen_thrregs.py` (`JAtZB` siblings with the native footprint `NatFoot`). New file; no +existing statement changed. Premise [P] `MIN_BUDGET` (as native). +-/ +import Fl4Proofs.P3.XTopJointReadRegs +import Fl4Proofs.P3.JAtZBThrCopy +import Fl4Proofs.P3.XTopJointSuspend +import Fl4Proofs.P3.XTopJointCancel +import Fl4Proofs.P3.XTopJointCancelRecv +import Fl4Proofs.P3.XTopJointCancelReply +import Fl4Proofs.P3.XTopJointData +import Fl4Proofs.P3.XTopJointReply +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **A message-register word of the caller at `J`** (as `rr_regw`). -/ +theorem JAtZB.mrRegF {σ0 : Fl4Spec.State Unit} {ct : U64} {x0 : Fl4Spec.Tcb} {buf : Option Fl4Spec.ObjRef} + {ms : List Fl4Spec.Word} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a (MrS σ0 ct.bv x0 buf ms)) + (hct : ct ∈ Z.T) (j : Nat) (hj : j = ms.length) (h4 : ms.length < 4) (v : U64) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (regA ct (regIdx (mrIdx j))) v) a + (MrS σ0 ct.bv x0 buf (ms ++ [v.bv])) ∧ NatFoot (L := L) Z s (MrS σ0 ct.bv x0 buf ms) + (L.wr s (regA ct (regIdx (mrIdx j))) v) (MrS σ0 ct.bv x0 buf (ms ++ [v.bv])) := by + have hkx := MrS_kheap_t σ0 ct.bv x0 buf ms + have hX := reg_wr_irel J.ip (J.xtop.irelK J.ks J.buf) hct hkx (Endp.mrIdx j) (mrIdx_ne_badge _) v + obtain ⟨V, J1, F1⟩ := J.regWrJ hct hkx (Endp.mrIdx j) v hX + refine ⟨V, ?_⟩ + cases buf with + | none => + have e := Endp.mrStateN_snoc_lt σ0 ct.bv x0 ms v.bv j hj h4 + simp only [MrS] at J1 F1 ⊢; rw [← e]; exact ⟨J1, F1⟩ + | some p => + have e := Endp.mrState_snoc_lt σ0 ct.bv x0 p ms v.bv j hj h4 + simp only [MrS] at J1 F1 ⊢; rw [← e]; exact ⟨J1, F1⟩ + +set_option maxHeartbeats 8000000 in +/-- **A message word in the caller's IPC buffer at `J`** (as `rr_bufw`). -/ +theorem JAtZB.mrBufF {σ0 : Fl4Spec.State Unit} {ct : U64} {x0 : Fl4Spec.Tcb} {p : Fl4Spec.ObjRef} + {ms : List Fl4Spec.Word} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a (MrS σ0 ct.bv x0 (some p) ms)) + (hk0 : σ0.kheap ct.bv = some (.tcb x0)) (w : U64) (hbm : BufMap Z.B w p) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ0) (k : U64) (hk : k.val = ms.length + 1) + (h4 : 4 ≤ ms.length) (h120 : ms.length < 120) (hOK : BufWordOK H σ0 (fl4BufA w k)) (v : U64) : + JAtZB (L := L) arI AR Z H Cs Ac Lst D (L.wr s (fl4BufA w k) v) a (MrS σ0 ct.bv x0 (some p) (ms ++ [v.bv])) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 (some p) ms) (L.wr s (fl4BufA w k) v) (MrS σ0 ct.bv x0 (some p) (ms ++ [v.bv])) := by + have hwB := hbm k (by omega) + have hBP' : BufPlaced (L := L) Z.N Z.C Z.B Z.c s (MrS σ0 ct.bv x0 (some p) ms) := hBP.mono rfl (MrS_sc hk0 _ _) + obtain ⟨hAp, o, ko, hko, g1, g2⟩ := hOK + have hObj : ∃ o ko, (MrS σ0 ct.bv x0 (some p) ms).kheap o = some ko ∧ + o.toNat ≤ (fl4BufA w k).val ∧ (fl4BufA w k).val < o.toNat + 2 ^ Fl4Spec.objBits ko := by + by_cases e : o = ct.bv + · subst e; rw [hk0] at hko; cases hko + exact ⟨_, _, MrS_kheap_t _ _ _ _ _, g1, g2⟩ + · exact ⟨o, ko, by rw [MrS_kheap_ne _ _ _ _ _ e]; exact hko, g1, g2⟩ + have J1 := J.userWrJ hBP' hwB hAp hObj v + have F1 := J.userWrN hBP' hwB hAp v + simp only [hk] at J1 F1 + simp only [MrS] at J1 F1 ⊢ + rw [← Endp.mrState_snoc_ge _ _ _ _ _ _ h4 h120] + exact ⟨J1, F1⟩ + +set_option maxHeartbeats 8000000 in +/-- **The frame registers into the caller's message registers at `J`** +(`MR0`..`MR3`; as `rr_loop0`). -/ +theorem rrB_loop0 {σ0 : Fl4Spec.State Unit} {ct : U64} {x0 : Fl4Spec.Tcb} {buf : Option Fl4Spec.ObjRef} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a (MrS σ0 ct.bv x0 buf [])) + (hct : ct ∈ Z.T) {src : U64} (hne : src.bv ≠ ct.bv) (hsT : src ∈ Z.T) {xs : Fl4Spec.Tcb} + (hks : σ0.kheap src.bv = some (.tcb xs)) (n : U64) : + tcb.read_registers_loop0 st s src n ct 0#u64 ⦃ r => r.2.val = min n.val 4 ∧ + (∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D r.1 a (MrS σ0 ct.bv x0 buf ((rrOf xs n).take r.2.val)) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 buf []) r.1 (MrS σ0 ct.bv x0 buf ((rrOf xs n).take r.2.val))) ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ⦄ := by + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have hfl : Fl4Spec.frameRegisters.length = 18 := rfl + have hwl : wrRegs.length = 20 := rfl + unfold tcb.read_registers_loop0 + apply loop.spec_decr_nat (measure := fun x => 4 - x.2.val) + (inv := fun x => x.2.val ≤ min n.val 4 ∧ + (∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D x.1 a + (MrS σ0 ct.bv x0 buf ((rrOf xs n).take x.2.val)) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 buf []) x.1 (MrS σ0 ct.bv x0 buf ((rrOf xs n).take x.2.val))) ∧ + L.ksb x.1 = L.ksb s ∧ L.cpu x.1 = L.cpu s) + · rintro ⟨s', i⟩ ⟨hi, ⟨V', J', F'⟩, hk', hc'⟩ + unfold tcb.read_registers_loop0.body + simp only at hi J' hk' hc' ⊢ + by_cases h1 : i < n + swap + · rw [if_neg h1, WP.spec_ok] + have : i.val = min n.val 4 := by have := (P1.gt_iff_val).not.1 h1; omega + exact ⟨this, ⟨V', J', F'⟩, hk', hc'⟩ + rw [if_pos h1] + have hin : i.val < n.val := (P1.gt_iff_val).1 h1 + have h2 : i < tcb.N_FRAME := (P1.gt_iff_val).2 (by rw [hf]; omega) + rw [if_pos h2] + by_cases h3 : i < msg.N_MSG_REGISTERS + swap + · rw [if_neg h3, WP.spec_ok] + have : i.val = min n.val 4 := by have := (P1.gt_iff_val).not.1 h3; rw [hN] at this; omega + exact ⟨this, ⟨V', J', F'⟩, hk', hc'⟩ + rw [if_pos h3] + have hi4 : i.val < 4 := by have := (P1.gt_iff_val).1 h3; rwa [hN] at this + simp only [lift, bind_ok] + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ (by rw [cast_to_us' i (by omega)]; simp; omega)) ?_ + rintro rr hrr + have hrv : rr = regIdx (wrRegs[i.val]'(by omega)) := by + rw [hrr, List.getElem_of_eq frame_regs_val, List.getElem_map] + congr 1 + simp [cast_to_us' i (by omega), wrRegs, List.getElem_append_left (show i.val < Fl4Spec.frameRegisters.length by omega)] + subst hrv + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' src _) ?_ + rintro v rfl + refine WP.spec_bind (Endp.msg_register_spec i hi4) ?_ + rintro r3 rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s' ct _ _) ?_ + rintro s2 rfl + obtain ⟨i4, h4e, h4v⟩ := spec_exists (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 + have hlen : ((rrOf xs n).take i.val).length = i.val := by simp [List.length_take, rrOf_length]; omega + obtain ⟨V2, J2, F2⟩ := J'.mrRegF hct i.val hlen.symm (by omega) + (L.mem s' (regA src (regIdx (wrRegs[i.val]'(by omega))))) + rw [rr_src_reg hne hsT hks J'.xtop.toRInv, ← rrOf_take_succ xs n i.val (by omega)] at J2 F2 + refine ⟨⟨by show i4.val ≤ min n.val 4; omega, ⟨V2, by rw [hi4']; exact J2, by rw [hi4']; exact J.natTrans F' F2.cast⟩, + by show L.ksb (L.wr s' _ _) = L.ksb s; rw [L.ksb_wr]; exact hk', + by show L.cpu (L.wr s' _ _) = L.cpu s; rw [L.cpu_wr]; exact hc'⟩, + by show 4 - i4.val < 4 - i.val; omega⟩ + · exact ⟨by simp, ⟨Z.vs, by simpa using J, by simpa using NatFoot.refl (L := L) Z s (MrS σ0 ct.bv x0 buf [])⟩, rfl, rfl⟩ + +set_option maxHeartbeats 8000000 in +/-- **The frame registers past `MR3` into the caller's IPC buffer at `J`** +(as `rr_loop1`). -/ +theorem rrB_loop1 {σ0 : Fl4Spec.State Unit} {ct : U64} {x0 : Fl4Spec.Tcb} (hk0 : σ0.kheap ct.bv = some (.tcb x0)) + (pb : Fl4Spec.ObjRef) (w : U64) (hbm : BufMap Z.B w pb) + (hOK : ∀ i : U64, i.val < 128 → BufWordOK H σ0 (fl4BufA w i)) + {src : U64} (hne : src.bv ≠ ct.bv) (hsT : src ∈ Z.T) {xs : Fl4Spec.Tcb} + (hks : σ0.kheap src.bv = some (.tcb xs)) (n : U64) (i0 : U64) + (hi0 : 4 ≤ i0.val) (hi0n : i0.val ≤ min n.val 18) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ0) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take i0.val))) : + tcb.read_registers_loop1 st s src n w i0 ⦃ r => r.2.val = min n.val 18 ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst D r.1 a (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take r.2.val)) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take i0.val)) r.1 (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take r.2.val)) ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ⦄ := by + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hfl : Fl4Spec.frameRegisters.length = 18 := rfl + have hwl : wrRegs.length = 20 := rfl + unfold tcb.read_registers_loop1 + apply loop.spec_decr_nat (measure := fun x => 18 - x.2.val) + (inv := fun x => 4 ≤ x.2.val ∧ x.2.val ≤ min n.val 18 ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst D x.1 a (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take x.2.val)) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take i0.val)) x.1 (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take x.2.val)) ∧ + L.ksb x.1 = L.ksb s ∧ L.cpu x.1 = L.cpu s) + · rintro ⟨s', i⟩ ⟨hi4, hi, J', F', hk', hc'⟩ + unfold tcb.read_registers_loop1.body + simp only at hi4 hi J' F' hk' hc' ⊢ + by_cases h1 : i < n + swap + · rw [if_neg h1, WP.spec_ok] + have : i.val = min n.val 18 := by have := (P1.gt_iff_val).not.1 h1; omega + exact ⟨this, J', F', hk', hc'⟩ + rw [if_pos h1] + have hin : i.val < n.val := (P1.gt_iff_val).1 h1 + by_cases h2 : i < tcb.N_FRAME + swap + · rw [if_neg h2, WP.spec_ok] + have : i.val = min n.val 18 := by have := (P1.gt_iff_val).not.1 h2; rw [hf] at this; omega + exact ⟨this, J', F', hk', hc'⟩ + rw [if_pos h2] + have hi18 : i.val < 18 := by have := (P1.gt_iff_val).1 h2; rwa [hf] at this + simp only [lift, bind_ok] + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ (by rw [cast_to_us' i (by omega)]; simp; omega)) ?_ + rintro rr hrr + have hrv : rr = regIdx (wrRegs[i.val]'(by omega)) := by + rw [hrr, List.getElem_of_eq frame_regs_val, List.getElem_map] + congr 1 + simp [cast_to_us' i (by omega), wrRegs, List.getElem_append_left (show i.val < Fl4Spec.frameRegisters.length by omega)] + subst hrv + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' src _) ?_ + rintro v rfl + obtain ⟨i3, h3e, h3v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h3e, bind_ok] + have hi3 : i3.val = i.val + 1 := by simpa using h3v + refine WP.spec_bind (Endp.msg.buf_set.spec s' w i3 _) ?_ + rintro s2 rfl + rw [WP.spec_ok] + have hlen : ((rrOf xs n).take i.val).length = i.val := by simp [List.length_take, rrOf_length]; omega + have hBP' : BufPlaced (L := L) Z.N Z.C Z.B Z.c s' σ0 := hBP.mono hk' (fun _ _ _ h => h) + obtain ⟨J2, F2⟩ := J'.mrBufF hk0 w hbm hBP' i3 (by rw [hlen]; exact hi3) (by omega) (by omega) (hOK i3 (by omega)) + (L.mem s' (regA src (regIdx (wrRegs[i.val]'(by omega))))) + rw [rr_src_reg hne hsT hks J'.xtop.toRInv, ← rrOf_take_succ xs n i.val (by omega)] at J2 F2 + refine ⟨⟨by show 4 ≤ i3.val; omega, by show i3.val ≤ min n.val 18; omega, by rw [hi3]; exact J2, by rw [hi3]; exact J.natTrans F' F2, + by show L.ksb (L.wr s' _ _) = L.ksb s; rw [L.ksb_wr]; exact hk', + by show L.cpu (L.wr s' _ _) = L.cpu s; rw [L.cpu_wr]; exact hc'⟩, + by show 18 - i3.val < 18 - i.val; omega⟩ + · exact ⟨hi0, hi0n, J, NatFoot.refl _ _ _, rfl, rfl⟩ + +set_option maxHeartbeats 8000000 in +/-- **The integer registers into the caller's IPC buffer at `J`** (as +`rr_loopGB`). -/ +theorem rrB_loopGB {σ0 : Fl4Spec.State Unit} {ct : U64} {x0 : Fl4Spec.Tcb} (hk0 : σ0.kheap ct.bv = some (.tcb x0)) + (pb : Fl4Spec.ObjRef) (w : U64) (hbm : BufMap Z.B w pb) + (hOK : ∀ i : U64, i.val < 128 → BufWordOK H σ0 (fl4BufA w i)) + {src : U64} (hne : src.bv ≠ ct.bv) (hsT : src ∈ Z.T) {xs : Fl4Spec.Tcb} + (hks : σ0.kheap src.bv = some (.tcb xs)) (n : U64) (hn : 18 < n.val) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ0) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take 18))) + (body : S → U64 → Result (ControlFlow (S × U64) (S × U64))) + (hbody : ∀ s k, body s k = if k < tcb.N_GP then (do + let i ← k + tcb.N_FRAME + if i < n then do + let i1 ← lift (UScalar.cast .Usize k) + let i2 ← Aeneas.Std.Array.index_usize regs.GP_REGISTERS i1 + let v ← obj.Tcb.reg st src s i2 + let i3 ← i + 1#u64 + let s1 ← msg.buf_set st s w i3 v + let k1 ← k + 1#u64 + ok (ControlFlow.cont (s1, k1)) + else ok (ControlFlow.done (s, k))) + else ok (ControlFlow.done (s, k))) : + loop (fun (x : S × U64) => body x.1 x.2) (s, 0#u64) ⦃ r => 18 + r.2.val = min n.val 20 ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst D r.1 a (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take (18 + r.2.val))) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take 18)) r.1 (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take (18 + r.2.val))) ∧ + L.ksb r.1 = L.ksb s ∧ L.cpu r.1 = L.cpu s ⦄ := by + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hg : tcb.N_GP.val = 2 := by unfold tcb.N_GP; rfl + have hfl : Fl4Spec.frameRegisters.length = 18 := rfl + have hwl : wrRegs.length = 20 := rfl + apply loop.spec_decr_nat (measure := fun x => 2 - x.2.val) + (inv := fun x => 18 + x.2.val ≤ min n.val 20 ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst D x.1 a (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take (18 + x.2.val))) ∧ + NatFoot (L := L) Z s (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take 18)) x.1 (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take (18 + x.2.val))) ∧ + L.ksb x.1 = L.ksb s ∧ L.cpu x.1 = L.cpu s) + · rintro ⟨s', k⟩ ⟨hk, J', F', hk', hc'⟩ + simp only at hk J' F' hk' hc' ⊢ + rw [hbody] + by_cases h1 : k < tcb.N_GP + swap + · rw [if_neg h1, WP.spec_ok] + have : 18 + k.val = min n.val 20 := by have := (P1.gt_iff_val).not.1 h1; rw [hg] at this; omega + exact ⟨this, J', F', hk', hc'⟩ + rw [if_pos h1] + have hk2 : k.val < 2 := by have := (P1.gt_iff_val).1 h1; rwa [hg] at this + obtain ⟨i, hie, hiv⟩ := spec_exists (U64.add_spec (x := k) (y := tcb.N_FRAME) (by rw [hf]; scalar_tac)) + rw [hie, bind_ok] + have hiv' : i.val = k.val + 18 := by rw [← hf]; simpa using hiv + by_cases h2 : i < n + swap + · rw [if_neg h2, WP.spec_ok] + have : 18 + k.val = min n.val 20 := by have := (P1.gt_iff_val).not.1 h2; omega + exact ⟨this, J', F', hk', hc'⟩ + rw [if_pos h2] + have hin : i.val < n.val := (P1.gt_iff_val).1 h2 + simp only [lift, bind_ok] + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ (by rw [cast_to_us' k (by omega)]; simp; omega)) ?_ + rintro rr hrr + have hrv : rr = regIdx (wrRegs[18 + k.val]'(by omega)) := by + rw [hrr, List.getElem_of_eq gp_regs_val, List.getElem_map] + congr 1 + simp only [wrRegs, cast_to_us' k (by omega)] + rw [List.getElem_append_right (by rw [hfl]; omega)] + simp [hfl] + subst hrv + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' src _) ?_ + rintro v rfl + obtain ⟨i3, h3e, h3v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h3e, bind_ok] + have hi3 : i3.val = i.val + 1 := by simpa using h3v + refine WP.spec_bind (Endp.msg.buf_set.spec s' w i3 _) ?_ + rintro s2 rfl + obtain ⟨k1, h4e, h4v⟩ := spec_exists (U64.add_spec (x := k) (y := 1#u64) (by scalar_tac)) + rw [h4e, bind_ok, WP.spec_ok] + have hk1 : k1.val = k.val + 1 := by simpa using h4v + have hlen : ((rrOf xs n).take (18 + k.val)).length = 18 + k.val := by + simp [List.length_take, rrOf_length]; omega + have hBP' : BufPlaced (L := L) Z.N Z.C Z.B Z.c s' σ0 := hBP.mono hk' (fun _ _ _ h => h) + obtain ⟨J2, F2⟩ := J'.mrBufF hk0 w hbm hBP' i3 (by rw [hlen]; omega) (by omega) (by omega) (hOK i3 (by omega)) + (L.mem s' (regA src (regIdx (wrRegs[18 + k.val]'(by omega))))) + rw [rr_src_reg hne hsT hks J'.xtop.toRInv, ← rrOf_take_succ xs n (18 + k.val) (by omega)] at J2 F2 + refine ⟨⟨by show 18 + k1.val ≤ min n.val 20; omega, by rw [hk1, ← Nat.add_assoc]; exact J2, by rw [hk1, ← Nat.add_assoc]; exact J.natTrans F' F2, + by show L.ksb (L.wr s' _ _) = L.ksb s; rw [L.ksb_wr]; exact hk', + by show L.cpu (L.wr s' _ _) = L.cpu s; rw [L.cpu_wr]; exact hc'⟩, + by show 2 - k1.val < 2 - k.val; omega⟩ + · exact ⟨by simp; omega, by simpa using J, by simpa using NatFoot.refl (L := L) Z s (MrS σ0 ct.bv x0 (some pb) ((rrOf xs n).take 18)), rfl, rfl⟩ + +set_option maxHeartbeats 16000000 in +/-- **The message info and `Running` after the message words at `J`** (the +badge written first, as `rr_tail_xtop`): `JAt` at the spec's success tail +with data, the caller no longer `Restart` and `RelTopM` (`sts_inv` at the +same `J`'s `RInv`). -/ +theorem JAtZB.rrTailF {σy : Fl4Spec.State Unit} {ct : U64} {x : Fl4Spec.Tcb} {ms : List Fl4Spec.Word} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a + (MrS (Notif.updK σy ct.bv (.tcb (Notif.setReg x Fl4Spec.badge_register 0))) ct.bv + (Notif.setReg x Fl4Spec.badge_register 0) (Endp.ipcBufOf true x) ms)) [VxE Z] + (hct : ct ∈ Z.T) (hcur : ct.bv = σy.curThread) (hk : σy.kheap ct.bv = some (.tcb x)) + (hrs : x.tcbState = .restart) (hlen : ms.length ≤ Fl4Spec.msgMaxLength) + (hfr : ∀ p, Endp.ipcBufOf true x = some p → ∀ z ∈ Endp.mrPairs ms, + (Endp.bufAddr p z.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p z.1) σy) + (n : U64) (hn : n.val = mrLen (Endp.ipcBufOf true x) ms.length) (hn128 : n.val < 128) : + (do + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 n + let s6 ← msg.set_msg_info st s ct mi + let s7 ← sched.set_thread_state st s6 ct sched.TS_RUNNING + ok (s7, a)) ⦃ res => + L.mem res.1 (stA ct) ≠ sched.TS_RESTART ∧ + ∃ z ∈ (specTail true ct.bv ms σy).1, RelTopM (L := L) res.1 z.2 ∧ + JAtVF (L := L) arI AR Z H Cs s (MrS (Notif.updK σy ct.bv (.tcb (Notif.setReg x Fl4Spec.badge_register 0))) ct.bv + (Notif.setReg x Fl4Spec.badge_register 0) (Endp.ipcBufOf true x) ms) res.1 res.2 z.2 ⦄ := by + set xA := Notif.setReg x Fl4Spec.badge_register 0 with hxA + set buf := Endp.ipcBufOf true x + unfold msginfo.MessageInfo.new + simp only [bind_ok] + refine WP.spec_bind (Endp.set_msg_info_spec _ ct 0#u64 n hn128) ?_ + rintro s3 ⟨v, rfl, hv⟩ + rw [← Endp.regIdx_msgInfo] + have hkA := MrS_kheap_t (Notif.updK σy ct.bv (.tcb xA)) ct.bv xA buf ms + have hX2 := reg_wr_irel J.ip (J.xtop.irelK J.ks J.buf) hct hkA Fl4Spec.msg_info_register (by decide) v + obtain ⟨V2, J2, F2⟩ := J.regWrJ hct hkA Fl4Spec.msg_info_register v hX2 + have hvd : v.bv = Fl4Spec.messageInfoToData ⟨Fl4Spec.natToLen (mrLen buf ms.length), 0, 0, 0⟩ := by + rw [hv]; congr 2 + apply BitVec.eq_of_toNat_eq; simp only [Fl4Spec.natToLen, BitVec.toNat_ofNat, UScalar.bv_toNat, ← hn]; omega + have eR : Notif.updK (MrS (Notif.updK σy ct.bv (.tcb xA)) ct.bv xA buf ms) ct.bv + (.tcb (Notif.setReg (Endp.withRegs xA (Endp.mrRegs xA.tcbArch.tcbContext.userRegs ms)) + Fl4Spec.msg_info_register v.bv)) = replyStateB σy ct.bv x buf ms := by + unfold replyStateB; rw [hvd] + rw [eR] at J2 F2 + have htcb : (Z.mv Z.G V2).th ct = some .tcb := (J2.xtop.curTcb (by rw [replyStateB_cur]; exact hcur.symm)).1 + have hcode : sched.TS_RUNNING.val = Notif.tsCode .running := by unfold sched.TS_RUNNING; rfl + refine WP.spec_bind (WP.spec_and (sts_inv _ a _ J2.xtop.toRInv ct htcb (by rw [replyStateB_cur]; exact hcur) + (replyStateB_kheap_t _ _ _ _ _) (by rw [replyTcbB_state, hrs]; rfl) J2.kern.act .running (by decide) _ hcode) + (J2.runJ hct (replyStateB_kheap_t _ _ _ _ _) (by rw [replyTcbB_state, hrs]))) ?_ + rintro s5 ⟨⟨hI5, hst5, -⟩, Lst', J5, F5⟩ + rw [WP.spec_ok] + exact ⟨by rw [hst5]; exact run_ne_rst, _, by rw [specTail_data hk hrs ms hlen hfr]; exact Set.mem_singleton _, + J5.xtop.relTop, _, _, _, _, J5, by + have F5' : NatFoot (L := L) Z _ _ _ _ := F5.cast + exact J.natTrans F2 F5'⟩ + +set_option maxHeartbeats 64000000 in +/-- **ReadRegisters' leaf at `J`** (IpcReadRegsJ's `readRegsXHypJ` with +`JAtZ` carried): after the caller's restart, the source's suspend +(`JAtZ.suspendJ` with the four cancel steps, `recvReplyHyp_holds` for +`jCancel_recv`), then, called, the badge (`JAtZ.regWrJ`), the copy loops +(`rrJ_loop0`, `rrJ_loop1`, `rrJ_loopGB`; the buffer words by +`JAtZ.bufWord`) and the message info and `Running` (`JAtZ.rrTailJ`); not +called, `Running` (`JAtZ.runJ`, `XTop`'s facts by `nodata_tail_core`). -/ +theorem JAtZB.readRegsF {s1 : S} {σ1 : Fl4Spec.State Unit} {ct p : U64} {blk call don : Bool} + (J1 : JAtZB (L := L) arI AR Z H Cs Ac Lst D s1 a σ1) [VxE Z] + (hns : ∀ u, σ1.schedulerAction = .switchThread u → False) (hidp : p.bv ≠ σ1.idleThread) + (hcur1 : σ1.curThread = ct.bv) {x1 : Fl4Spec.Tcb} (hkc1 : σ1.kheap ct.bv = some (.tcb x1)) + (hrs1 : x1.tcbState = .restart) (hpne : p ≠ ct) (hpT : p ∈ Z.T) (flags n : U64) : + tcb.read_registers st arI s1 a p ((flags &&& 1#u64) != 0#u64) n call ⦃ r => + L.mem r.1 (stA ct) ≠ sched.TS_RESTART ∧ ∃ rr σy, + (.ok rr, σy) ∈ ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB (.readRegisters p.bv + (Fl4Spec.testBit flags.bv 0) n.bv Fl4Spec.ArchDefaultExtraRegisters))).run σ1).1 ∧ + ∃ z ∈ (specTail call ct.bv rr σy).1, RelTopM (L := L) r.1 z.2 ∧ + JAtVF (L := L) arI AR Z H Cs s1 σ1 r.1 r.2 z.2 ⦄ := by + have hI1 := J1.xtop.irelK J1.ks J1.buf + have hctT : ct ∈ Z.T := TopSR.cur_mem J1.xtop.fin.1 hcur1.symm + have hne : p.bv ≠ ct.bv := fun e => hpne (P1.eq_of_bv e) + unfold tcb.read_registers + refine WP.spec_bind (cur_thread_bv hI1.2.2.1) ?_ + rintro ct' hct' + obtain rfl : ct = ct' := (P1.eq_of_bv (hct'.trans hcur1)).symm + rw [and1_ne0] + -- the source's suspend + have hsus : (if Fl4Spec.testBit flags.bv 0 = true then sched.suspend st arI s1 a p else ok (s1, a)) ⦃ r => + r.2 = a ∧ ∃ σy, Fl4Spec.NonDet.when (Fl4Spec.testBit flags.bv 0 = true) (Fl4Spec.suspend (Z := Unit) p.bv) σ1 = + ({((), σy)}, False) ∧ StK.OSK p.bv σ1 σy ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' r.1 r.2 σy ∧ NatFoot (L := L) Z s1 σ1 r.1 σy ⦄ := by + cases Fl4Spec.testBit flags.bv 0 + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, σ1, rfl, StK.OSK.refl _, Z.vs, Ac, Lst, D, by simpa using J1, NatFoot.refl _ _ _⟩ + · simp only [↓reduceIte] + refine WP.spec_mono (J1.suspendJD (jCancelB_send arI) (jCancelB_ntfn arI) (jCancelB_recv arI) + (jCancelB_reply arI) hpT (fun u h => (hns u h).elim) hidp) ?_ + rintro ⟨s2, a2⟩ ⟨h1, σy, h2, V, Ac', Lst', J2, F2⟩ + refine ⟨h1, σy, by simpa [Fl4Spec.NonDet.when] using h2, StK.st_suspend σ1 ((), σy) ?_, V, Ac', Lst', D, J2, F2⟩ + rw [h2]; exact Set.mem_singleton _ + refine WP.spec_bind hsus ?_ + rintro ⟨s2, a2⟩ ⟨ha2, σy, hsy, hosk, V2, Ac2, Lst2, D2, J2, F2⟩ + obtain rfl : a = a2 := ha2.symm + simp only at J2 + -- the caller and the source after it + have hXy := J2.xtop.irelK J2.ks J2.buf + obtain ⟨xy, hky, -⟩ := hXy.1.tcb ct hctT + obtain ⟨y0, hy0, hys⟩ := hosk.1 ct.bv xy (Ne.symm hne) hky + rw [hkc1] at hy0; cases hy0 + have hrs : xy.tcbState = .restart := hys.trans hrs1 + have hcury : ct.bv = σy.curThread := (hosk.2.trans hcur1).symm + obtain ⟨xs, hks, -⟩ := hXy.1.tcb p hpT + have hXZ := J2.xtop + have hOKb := J2.xferH.1 + have hBCy := J2.xferH.2.1 + have hBPy := J2.xferH.2.2 + have hact := J2.kern.act + have htcb : (Z.mv Z.G V2).th ct = some .tcb := (hXZ.curTcb hcury.symm).1 + -- the spec's ReadRegisters + have hmem : ((.ok (rrOf xs n) : Except Unit (List Fl4Spec.Data)), σy) ∈ + ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB (.readRegisters p.bv + (Fl4Spec.testBit flags.bv 0) n.bv Fl4Spec.ArchDefaultExtraRegisters))).run σ1).1 := by + show _ ∈ ((Fl4Spec.invokeTcb (Z := Unit) _).run σ1).1 + unfold Fl4Spec.invokeTcb + refine mem_liftE (x := (rrOf xs n, σy)) (mem_bind_nd (x := ((), σy)) (by rw [hsy]; rfl) + (mem_bind_nd (x := (σy.curThread, σy)) rfl (mem_bind_nd rfl ?_))) + rw [asUser_mapM_get hks] + exact Set.mem_singleton _ + cases call + · -- not called: `Running` + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (WP.spec_and (nodata_tail_core hXZ hcury hky hrs hact (rrOf xs n)) + (J2.runJ hctT hky hrs)) ?_ + rintro s3 ⟨⟨hne3, z, hz, hRT, -⟩, Lst', J5, F5⟩ + refine (WP.spec_ok _).2 ⟨hne3, rrOf xs n, σy, hmem, z, hz, hRT, ?_⟩ + unfold specTail at hz + rw [nd_bind_one (Notif.getThreadState_eq hky)] at hz + simp only [hrs] at hz + rw [Endp.nd_when_false (by simp), Notif.nd_pure_bind', Notif.setThreadState_eq .running hky] at hz + rw [Set.mem_singleton_iff.1 hz] + exact ⟨_, _, _, _, J5, J1.natTrans F2 F5.cast⟩ + -- called: the caller's buffer, the badge, the words, the message info and `Running` + simp only [↓reduceIte] + obtain ⟨x', hk', hwx⟩ := J2.buf ct hctT + rw [hky] at hk'; cases hk' + refine WP.spec_bind (lookup_buf_full s2 σy hXZ.rel.2.1.1 hXZ.rel.2.1.2.1 htcb hky hOKb true) ?_ + rintro bf ⟨hw0, hwp⟩ + have hbm : ∀ q, Endp.ipcBufOf true xy = some q → Endp.BufMap Z.B bf q := fun q hq => + hBCy ct hctT xy true q hky hq bf (hwp hwx q hq) + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s2 ct _ 0#u64) ?_ + rintro s3 rfl + rw [← Endp.regIdx_badge] + set xb := Notif.setReg xy Fl4Spec.badge_register 0 with hxb + set σb := Notif.updK σy ct.bv (.tcb xb) with hσb + have hkb : σb.kheap ct.bv = some (.tcb xb) := by simp [hσb, Notif.updK] + have hksb : σb.kheap p.bv = some (.tcb xs) := by simp [hσb, Notif.updK, hne, hks] + have hXb := badge_wr_irel J2.ip hXy hctT hky 0#u64 + rw [badgeRegA_eq_regA] at hXb + obtain ⟨V3, J3, F3⟩ := J2.regWrJ hctT hky Fl4Spec.badge_register 0#u64 hXb + have e0 : U64.bv 0#u64 = (0 : Fl4Spec.Word) := rfl + rw [e0] at J3 F3 + have F03 := J1.natTrans F2 F3.cast + have F03' : NatFoot (L := L) Z s1 σ1 (L.wr s2 (regA ct (regIdx Fl4Spec.badge_register)) 0#u64) + (MrS σb ct.bv xb (Endp.ipcBufOf true xy) []) := by rw [MrS_nil hkb]; exact F03 + have J0 : JAtZB (L := L) arI AR (Z.mv Z.G V3) H Cs Ac2 Lst2 D2 + (L.wr s2 (regA ct (regIdx Fl4Spec.badge_register)) 0#u64) a (MrS σb ct.bv xb (Endp.ipcBufOf true xy) []) := by + rw [MrS_nil hkb]; exact J3 + have hOK3 := J3.xferH.1 + have hWOK : ∀ q, Endp.ipcBufOf true xy = some q → ∀ i : U64, i.val < 128 → + BufWordOK H σb (fl4BufA bf i) := fun q hq i hi => + J3.bufWordG hOK3 htcb hkb hq i.val hi (by rw [fl4BufA_bv, hwp hwx q hq]) + have hks3 : L.ksb (L.wr s2 (regA ct (regIdx Fl4Spec.badge_register)) 0#u64) = L.ksb s2 := L.ksb_wr _ _ _ + have hlenR := rrOf_length xs n + have hfr : ∀ q, Endp.ipcBufOf true xy = some q → ∀ z ∈ Endp.mrPairs (rrOf xs n), + (Endp.bufAddr q z.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr q z.1) σy := + fun q hq z hz => ⟨Endp.bufAddr_aligned hXZ.fin.1.2.ep (hbm q hq) z.1, + buf_frame_of_ok hOKb hky hq z.1 (mrPairs_lt hz)⟩ + refine WP.spec_bind (rrB_loop0 J0 hctT hne hpT hksb n) ?_ + rintro ⟨s4, i⟩ ⟨hiv, ⟨V4, J4, F4⟩, hk4, hc4⟩ + simp only at hiv J4 F4 hk4 hc4 + have F04 := J1.natTrans F03' F4.cast + try simp only [uncurry_apply_pair] + have fin : ∀ (s5 : S) (len : U64), + JAtZB (L := L) arI AR (Z.mv Z.G V4) H Cs Ac2 Lst2 D2 s5 a + (MrS σb ct.bv xb (Endp.ipcBufOf true xy) (rrOf xs n)) ∧ + NatFoot (L := L) Z s1 σ1 s5 (MrS σb ct.bv xb (Endp.ipcBufOf true xy) (rrOf xs n)) → + len.val = mrLen (Endp.ipcBufOf true xy) (rrOf xs n).length → + (do + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 len + let s6 ← msg.set_msg_info st s5 ct mi + let s7 ← sched.set_thread_state st s6 ct sched.TS_RUNNING + ok (s7, a)) ⦃ r => L.mem r.1 (stA ct) ≠ sched.TS_RESTART ∧ ∃ rr σy', + (.ok rr, σy') ∈ ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk true don (.invokeTCB (.readRegisters p.bv + (Fl4Spec.testBit flags.bv 0) n.bv Fl4Spec.ArchDefaultExtraRegisters))).run σ1).1 ∧ + ∃ z ∈ (specTail true ct.bv rr σy').1, RelTopM (L := L) r.1 z.2 ∧ JAtVF (L := L) arI AR Z H Cs s1 σ1 r.1 r.2 z.2 ⦄ := by + intro s5 len ⟨J5, F05⟩ hl + refine WP.spec_mono (J5.rrTailF hctT hcury hky hrs + (by rw [hlenR]; unfold Fl4Spec.msgMaxLength; omega) hfr len hl + (by rw [hl]; cases Endp.ipcBufOf true xy <;> simp [mrLen, hlenR] <;> omega)) ?_ + rintro r ⟨hne5, z, hz, hRT, Vt, Act, Lstt, Dt, Jt, Ft⟩ + exact ⟨hne5, rrOf xs n, σy, hmem, z, hz, hRT, Vt, Act, Lstt, Dt, Jt, J1.natTrans F05 Ft.cast⟩ + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hg : tcb.N_GP.val = 2 := by unfold tcb.N_GP; rfl + by_cases hb : bf = 0#u64 + · -- no buffer: the message registers only + have hbn : Endp.ipcBufOf true xy = none := hw0.1 hb + have hbb : (bf != 0#u64) = false := by simp [hb] + simp only [hbb, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (rr_loopGR n s4 _ (fun _ _ => rfl)) ?_ + rintro ⟨s5, k⟩ hk5 + obtain ⟨rfl, rfl⟩ : s4 = s5 ∧ k = 0#u64 := ⟨(Prod.mk.inj hk5).1.symm, (Prod.mk.inj hk5).2⟩ + try simp only [uncurry_apply_pair] + simp only [lift, bind_ok] + have J4 := And.intro J4 F04 + refine fin s4 _ ?_ ?_ + · rw [hbn] at J4 ⊢ + by_cases hn4 : n.val ≤ 4 + · rw [hiv, show min n.val 4 = (rrOf xs n).length by rw [hlenR]; omega, List.take_length] at J4; exact J4 + · rw [hiv, show min n.val 4 = 4 by omega] at J4 + simp only [MrS] at J4 ⊢ + rw [← List.take_append_drop 4 (rrOf xs n), Endp.mrStateN_append_ge _ _ _ _ _ (by simp [hlenR]; omega)] + exact J4 + · rw [hbn, hlenR] + simp [mrLen, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + · -- a buffer + obtain ⟨q, hq⟩ : ∃ q, Endp.ipcBufOf true xy = some q := by + cases h : Endp.ipcBufOf true xy with + | none => exact absurd (hw0.2 h) hb + | some q => exact ⟨q, rfl⟩ + have hbm' := hbm q hq + have hWq := hWOK q hq + have hbb : (bf != 0#u64) = true := by simp [hb] + simp only [hbb, ↓reduceIte] + rw [hq] at J4 F04 + have hBP3 : Vs.BufPlaced (L := L) Z.N Z.C Z.B Z.c s4 σb := + hBPy.mono (hk4.trans hks3) (fun r y n hr => by + by_cases e : r = ct.bv + · subst e; rw [hkb] at hr; cases hr + · simpa [hσb, Notif.updK, e] using hr) + by_cases hin : i < n + · -- past `MR3`: the frame registers into the buffer + rw [if_pos hin] + have hin' : i.val < n.val := (P1.gt_iff_val).1 hin + have hi4 : i.val = 4 := by omega + have hi18 : i < tcb.N_FRAME := (P1.gt_iff_val).2 (by rw [hf]; omega) + rw [if_pos hi18] + refine WP.spec_bind (rrB_loop1 (Z := (Z.mv Z.G V3).mv Z.G V4) hkb q bf hbm' hWq hne hpT hksb n i (by omega) (by omega) hBP3 J4) ?_ + rintro ⟨s5, i1⟩ ⟨hi1, J5, F5, hk5, hc5⟩ + simp only at hi1 J5 F5 hk5 hc5 + have F05 := J1.natTrans F04 F5.cast + try simp only [uncurry_apply_pair] + refine WP.spec_bind (rr_loopGR n s5 _ (fun _ _ => rfl)) ?_ + rintro ⟨s6, k⟩ hk6 + obtain ⟨rfl, rfl⟩ : s5 = s6 ∧ k = 0#u64 := ⟨(Prod.mk.inj hk6).1.symm, (Prod.mk.inj hk6).2⟩ + try simp only [uncurry_apply_pair] + try simp only [hbb, ↓reduceIte] + have hk0 : (0#u64 : U64) < tcb.N_GP := (P1.gt_iff_val).2 (by rw [hg]; decide) + rw [if_pos hk0] + obtain ⟨i2, h2e, h2v⟩ := spec_exists (U64.add_spec (x := 0#u64) (y := tcb.N_FRAME) (by rw [hf]; scalar_tac)) + rw [h2e, bind_ok] + have hi2 : i2.val = 18 := by rw [← hf]; simpa using h2v + by_cases h18 : i2 < n + · rw [if_pos h18] + have hn18 : 18 < n.val := by have := (P1.gt_iff_val).1 h18; omega + rw [show i1.val = 18 by omega] at J5 F05 + have hBP5 : Vs.BufPlaced (L := L) Z.N Z.C Z.B Z.c s5 σb := hBP3.mono hk5 (fun _ _ _ h => h) + refine WP.spec_bind (rrB_loopGB (Z := (Z.mv Z.G V3).mv Z.G V4) hkb q bf hbm' hWq hne hpT hksb n hn18 hBP5 J5 _ (fun _ _ => rfl)) ?_ + rintro ⟨s7, k1⟩ ⟨hk1, J7, F7, hk7, hc7⟩ + simp only at hk1 J7 F7 hk7 hc7 + have F07 := J1.natTrans F05 F7.cast + try simp only [uncurry_apply_pair] + simp only [lift, bind_ok] + have J7 := And.intro J7 F07 + refine fin s7 _ ?_ ?_ + · rw [hq]; rw [show 18 + k1.val = (rrOf xs n).length by rw [hlenR]; omega, List.take_length] at J7 + exact J7 + · rw [hq, hlenR] + simp [mrLen, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + · rw [if_neg h18] + have hn18 : n.val ≤ 18 := by have := (P1.gt_iff_val).not.1 h18; omega + simp only [lift, bind_ok] + have J5 := And.intro J5 F05 + refine fin s5 _ ?_ ?_ + · rw [hq]; rw [show i1.val = (rrOf xs n).length by rw [hlenR]; omega, List.take_length] at J5 + exact J5 + · rw [hq, hlenR] + simp [mrLen, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + · -- up to `MR3` + rw [if_neg hin] + have hn4 : n.val ≤ 4 := by have := (P1.gt_iff_val).not.1 hin; omega + refine WP.spec_bind (rr_loopGR n s4 _ (fun _ _ => rfl)) ?_ + rintro ⟨s5, k⟩ hk5 + obtain ⟨rfl, rfl⟩ : s4 = s5 ∧ k = 0#u64 := ⟨(Prod.mk.inj hk5).1.symm, (Prod.mk.inj hk5).2⟩ + try simp only [uncurry_apply_pair] + try simp only [hbb, ↓reduceIte] + have hk0 : (0#u64 : U64) < tcb.N_GP := (P1.gt_iff_val).2 (by rw [hg]; decide) + rw [if_pos hk0] + obtain ⟨i2, h2e, h2v⟩ := spec_exists (U64.add_spec (x := 0#u64) (y := tcb.N_FRAME) (by rw [hf]; scalar_tac)) + rw [h2e, bind_ok] + have hi2 : i2.val = 18 := by rw [← hf]; simpa using h2v + have h18 : ¬ i2 < n := fun h => by have := (P1.gt_iff_val).1 h; omega + rw [if_neg h18] + simp only [lift, bind_ok] + have J4 := And.intro J4 F04 + refine fin s4 _ ?_ ?_ + · rw [hq]; rw [show i.val = (rrOf xs n).length by rw [hlenR]; omega, List.take_length] at J4 + exact J4 + · rw [hq, hlenR] + simp [mrLen, core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrReadTop.lean b/proofs/Fl4Proofs/P3/JAtZBThrReadTop.lean new file mode 100644 index 000000000..a2600847f --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrReadTop.lean @@ -0,0 +1,138 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **ReadRegisters**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrRead +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The ReadRegisters label at `JAt`** (as `tcb_read_regsX`): TcbCtl's +decode, the caller's restart (`JAtZ.restartJ`), then XTopJointReadRegs' +`JAtZ.readRegsJ` (the source's suspend, the copy loops, the reply tail). -/ +theorem JAtZB.tcbReadRegsF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (hpI : p ≠ L.idleW s) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbReadRegisters) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBReadRegisters p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostR (L := L) arI AR Z H Cs s ar σ call + (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) ct res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + have htcb := (hX.curTcb hcur).1 + have hdec0 : specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeReadRegisters (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv)) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_read_eq arI s ar p _ hstall, hdec0] + unfold tcb.decode_read_registers + by_cases h1 : length < 2#u64 + · simp only [h1, ↓reduceIte, error.truncated, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + have : args.length < 2 := by scalar_tac + match args, this with + | [], _ => rfl + | [_], _ => rfl + simp only [h1, ↓reduceIte] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, hlen with + | y0 :: y1 :: rest, _ => exact ⟨_, _, _, rfl⟩ + | [], h => exfalso; simp at h; scalar_tac + | [_], h => exfalso; simp at h; scalar_tac + have ha0 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have ha1 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 1#u64 = ok y1 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 1#u64 y1 (by simp) + rw [ha0, ha1]; simp only [bind_ok, nregs_eq] + have hsp : ∀ (k : Unit → Fl4Spec.SEMonad Unit Fl4Spec.TcbInvocation), + (Fl4Spec.decodeReadRegisters (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv)) = + (Fl4Spec.rangeCheck (Z := Unit) y1.bv 1 (BitVec.ofNat 64 20) >>= fun _ => do + let self ← Fl4Spec.NonDetE.liftE (Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread) + Fl4Spec.NonDetE.whenE (p.bv = self) (Fl4Spec.NonDetE.throwError .illegalOperation) + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.readRegisters p.bv (Fl4Spec.testBit y0.bv 0) y1.bv + Fl4Spec.ArchDefaultExtraRegisters)) := by + intro _ + simp only [Fl4Spec.decodeReadRegisters, List.map_cons, nregs_spec, retOk_bind] + rw [hsp (fun _ => Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.suspend 0))] + have hrng := range20_iff y1 + by_cases hr : 1 ≤ y1.val ∧ y1.val ≤ 20 + · have hn1 : ¬ y1 < 1#u64 := by scalar_tac + have hn2 : ¬ y1 > 20#u64 := by scalar_tac + simp only [hn1, hn2, ↓reduceIte, hct, bind_ok, tcb_eq_ct] + have hrc : (Fl4Spec.rangeCheck (Z := Unit) y1.bv 1 (BitVec.ofNat 64 20)).run σ = ({(.ok (), σ)}, False) := by + unfold Fl4Spec.rangeCheck Fl4Spec.NonDetE.unlessE; rw [if_pos (hrng.2 hr)]; rfl + by_cases hpc : p = ct + · subst hpc + simp only [decide_true, ↓reduceIte, error.illegal, bind_ok] + refine (WP.spec_ok _).2 ?_ + apply Or.inl + refine ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩ + apply run_liftME_err + rw [run_bind_ok hrc, run_bind_ok (run_liftE (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => + s.curThread) rfl), whenE_pos (by show p.bv = σ.curThread; rw [hcur])] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : + Fl4Spec.SEMonad Unit Unit)) rfl _ + · simp only [decide_eq_false hpc, Bool.false_eq_true, ↓reduceIte] + obtain ⟨s1, σ1, x1, hrc1, hst1, hk1, -, hσ1, hns, hid1, hcu1, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + have hkc1 : σ1.kheap ct.bv = some (.tcb { x1 with tcbState := .restart }) := by + rw [hσ1, Notif.stsAct_kheap]; simp [Notif.updK] + rw [hrc1]; simp only [bind_ok, lift] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.rangeCheck (Z := Unit) y1.bv 1 (BitVec.ofNat 64 20) >>= fun _ => do + let self ← Fl4Spec.NonDetE.liftE (Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread) + Fl4Spec.NonDetE.whenE (p.bv = self) (Fl4Spec.NonDetE.throwError .illegalOperation) + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.readRegisters p.bv (Fl4Spec.testBit y0.bv 0) y1.bv + Fl4Spec.ArchDefaultExtraRegisters))).run σ = + ({(.ok (.invokeTCB (.readRegisters p.bv (Fl4Spec.testBit y0.bv 0) y1.bv + Fl4Spec.ArchDefaultExtraRegisters)), σ)}, False) := by + apply run_liftME_ok + rw [run_bind_ok hrc, run_bind_ok (run_liftE (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => + s.curThread) rfl), whenE_neg (by show ¬ p.bv = σ.curThread; rw [hcur, bv_eq_iff']; exact hpc), retOk_bind] + rfl + refine WP.spec_bind (J1.readRegsF (blk := blk) (don := don) hns + (by rw [hid1]; exact J.ne_idle hpI) (by rw [hcu1]; exact hcur) hkc1 rfl hpc hpT y0 y1) ?_ + rintro ⟨s2, a2⟩ ⟨hne, rr, σy, hm, z, hz, hRz, V2, Ac2, Lst2, D2, J2, F2⟩ + exact (WP.spec_ok _).2 (Or.inr ⟨rfl, hne, _, DecOut.of_eq hdec, trivial, σ1, hst1, rr, σy, hm, z, hz, hRz, + V2, Ac2, Lst2, D2, J2, J.natTrans F1 F2.cast⟩) + · have hrc : (Fl4Spec.rangeCheck (Z := Unit) y1.bv 1 (BitVec.ofNat 64 20)).run σ = + ({(.error (.rangeError 1 (BitVec.ofNat 64 20)), σ)}, False) := by + unfold Fl4Spec.rangeCheck Fl4Spec.NonDetE.unlessE; rw [if_neg (fun h => hr (hrng.1 h))]; rfl + have herr : ErrRel (.Sys (.RangeError 1#u64 20#u64)) (.rangeError 1 (BitVec.ofNat 64 20)) := + Or.inl ⟨rfl, rfl⟩ + by_cases hn1 : y1 < 1#u64 + · simp only [hn1, ↓reduceIte, error.range, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, _, ?_, herr⟩) + exact run_liftME_err (run_bind_error hrc _) + · have hn2 : y1 > 20#u64 := by scalar_tac + simp only [hn1, hn2, ↓reduceIte, error.range, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, _, ?_, herr⟩) + exact run_liftME_err (run_bind_error hrc _) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrResume.lean b/proofs/Fl4Proofs/P3/JAtZBThrResume.lean new file mode 100644 index 000000000..8cbdab35f --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrResume.lean @@ -0,0 +1,71 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **Resume**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBRestartN +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The Resume label at `JAt`** (as `tcb_resumeX`): the caller's restart +(`JAtZ.restartJ`; the slots kept, so the target's cap still in the invoked +slot; the current thread `Restart`, `CurInv` by `restart_mid`), then +XTopJointRestart's `JAtZ.restartTcbJ` with the four cancel steps and the +platform's `MIN_BUDGET` [P] (as IpcCapJ). -/ +theorem JAtZB.tcbResumeF (hAT : P0.ArchTotal arI) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (x0 : U64) (hxS : IsSlot Z.th x0) (hcap : capAt (L.mem s) x0 = ok (.Thread p)) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbResume) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (ct : U64) (hct : sched.cur_thread st s = ok ct) + (c : tcb.Ctx) (blk call don : Bool) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBResume p c ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl args ci sl (.Thread p) excaps buffer) res ⦄ := by + have hdec : (specDec false slbl args ci sl (.Thread p) excaps buffer).run σ = + ({(.ok (.invokeTCB (.resume p.bv)), σ)}, False) := by + show (Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer).run σ = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + apply run_liftME_ok + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl]; rfl + obtain ⟨s1, σ1, x, hrc1, hst, hk, hcur, -, hns, -, -, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + obtain ⟨-, x', hk', hrun⟩ := J.curRunnable hni hct + rw [hk] at hk'; cases hk' + -- the slots kept by the caller's restart + have hSS : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := XTopAt.restart J.xtop hcur hk hrun + rw [hs1, WP.spec_ok] at h; exact h.2.1 + have hcap1 : capAt (L.mem s1) x0 = ok (.Thread p) := by + rw [capAt_capV, hSS.capV hxS, ← capAt_capV]; exact hcap + rw [decode_resume_eq arI s ar p c hstall, hrc1] + simp only [bind_ok] + refine WP.spec_bind ((J1.undead hxS hcap1 hpT).restartTcbN hAT (hmb ar) hpT (fun h => h.2 rfl) + (fun u h => hns u h)) ?_ + rintro ⟨s2, a2⟩ ⟨-, σ', hres, V, Ac', Lst', D', J2, F2⟩ + refine (WP.spec_ok _).2 ?_ + have hz : ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB (.resume p.bv))).run σ1).1 := by + show _ ∈ ((Fl4Spec.NonDetE.liftE _).run σ1).1 + exact mem_liftE (x := ([], σ')) (mem_bind_nd (x := ((), σ')) (by rw [hres]; exact Set.mem_singleton _) rfl) + exact Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst, _, hz, V, Ac', Lst', D', J2, J.natTrans F1 F2.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrSched.lean b/proofs/Fl4Proofs/P3/JAtZBThrSched.lean new file mode 100644 index 000000000..c9065ab4a --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrSched.lean @@ -0,0 +1,469 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **the MCP and flags word steps over `JAtZB`, with `NatFoot`**. + +`JAtZB.wideW_scOut/wideW_irq/wideW_utc/deadP_qm` and `JAtZB.mcpWrF`, `JAtZB.flagsWrF` are the `JAtZB` siblings of +XTopJointTcbSched's `JAtZ` lemmas (generated by `handoff/tools/gen_thrsched.py`); the two word steps also give the +native footprint. `natK_updTcb`, `JAtZB.tcbW_nh`: one TCB updated without becoming `runningVM`; a TCB word is not the +VCPU-list head. No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointTcbSched +import Fl4Proofs.P3.NatFoot +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core + +/-- One TCB rewritten, not newly `runningVM`, is a native spec step. -/ +theorem natK_updTcb {σ : Fl4Spec.State Unit} {c : Fl4Spec.ObjRef} {x y : Fl4Spec.Tcb} + (hk : σ.kheap c = some (.tcb x)) (h : y.tcbState = .runningVM → x.tcbState = .runningVM) : + NatK σ (Notif.updK σ c (.tcb y)) := + natK_kh (R := fun _ o o' => ZvChg o o') (fun _ _ _ h => h) fun p => by + by_cases hp : p = c + · subst hp; exact Or.inr ⟨_, _, hk, by simp [Notif.updK], zvChg_tcb h⟩ + · exact Or.inl (by simp [Notif.updK, hp]) + +/-- A word inside a TCB of `T` is not the VCPU-list head word. -/ +theorem JAtZB.tcbW_nh {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t b : U64} + (ht : t ∈ Z.T) (h1 : t.val ≤ b.val) (h2 : b.val < t.val + 4096) : b ≠ P1.vHeadA st s := by + intro eb + subst eb + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + have d := J.kern.hks t _ (J.zH.1 t ht) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + omega + +end Fl4Proofs.P3 + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The header words of `H`'s SCs outside `Z.C` are kept** by a +finalisation (`sc_out_keep`'s argument). -/ +theorem JAtZB.wideW_scOut {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (WideW (st := st) Z.T Z.N Z.C Z.c s σ)) {q : U64} {z : Nat} + (hq : H q = some (.sc z)) (hqC : q ∉ Z.C) {K : U64} (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + L.mem s' (scW q K) = L.mem s (scW q K) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hS.lists + have hH := J.kern.heap + have hal := hH.align q _ hq + have hfit := hH.fits q _ hq + have hz := hH.scmin q z hq + simp only [P1.HKind.bits] at hal hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + refine hw.mem_eq (by show (scW q K).val % 8 = 0; rw [hv]; omega) fun hb => ?_ + rcases hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hHy : H y = some .tcb := J.zH.1 y hy + have hne : q ≠ y := fun e => by rw [e, hHy] at hq; cases hq + have := hH.disj q _ y _ hq hHy hne + simp only [P1.HKind.bits] at this + omega + · have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have := J.kern.hnd q _ hq Z.c hcC + simp only [P1.HKind.bits] at this + omega + · have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + have hne : q ≠ n := fun e => by rw [e, hHn] at hq; cases hq + have := hH.disj q _ n _ hq hHn hne + simp only [P1.HKind.bits] at this + omega + · have hHs := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have hne : q ≠ sc := fun e => hqC (e ▸ hsc) + have := hH.disj q _ sc _ hq hHs hne + simp only [P1.HKind.bits] at this + omega + +/-- **The IRQ node's caps are kept** by a finalisation (its words lie in the +kernel-state block; the TCBs, notifications and SCs outside it, the node +after it). -/ +theorem JAtZB.wideW_irq {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (WideW (st := st) Z.T Z.N Z.C Z.c s σ)) (i : U64) (hi : i.val < 128) : + capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64 := J.kern.ksbFit + refine irqNode_keep hw hkf J.kern.ksbAl (fun b hb => ?_) i hi + rcases hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have := hJ.ks y _ (J.zH.1 y hy) (fun _ e => by cases e); simp only [P1.HKind.bits] at this; omega + · simp only [P1.LawfulStore.nodeBase] at g1; omega + · have := J.nk n hn; omega + · have := (hS.scOut sc hsc x n hk b ⟨g1, g2⟩).2.2.2; omega + +set_option maxHeartbeats 1000000 in +/-- **The untyped caps' facts after a finalisation**: the caps kept (the slots +same), the free parts zero (no `FinW` word there: the TCBs are typed, the +notifications and SCs are spec objects outside it, `SpecFree`, the node is +outside every untyped cap, `JCovD`), the rest by the kept kernel state, CDT, +original bits and the node-0 `DEBUG_TCBS` word (`hDbg`). -/ +theorem JAtZB.wideW_utc {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (WideW (st := st) Z.T Z.N Z.C Z.c s σ)) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (harch : σ'.archState = σ.archState) + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hfi := U.fiLe + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [hcdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [harch]; exact U.gReg (B g)⟩ + · intro g w hal h1 h2 + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hth : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y .tcb hth + simp only [objSize] at this; omega + · have := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).2.1 Z.c hcC + omega + · obtain ⟨x, hx, -⟩ := hR.ntfn n hn + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) n.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.ntfnBits, UScalar.bv_toNat] at this + omega + · have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) sc.bv _ hk + simp only [Fl4Spec.objBits, UScalar.bv_toNat] at this + rw [Nat.add_comm Fl4Spec.minSchedContextBits n] at this + omega + · intro g h0 hf0 + rw [hcdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← horig]; exact h), by rw [hna, hDbg]; exact r3⟩ + +/-- **The dead stay dead across a `QKM` step** (`deadP_qk`). -/ +theorem JAtZB.deadP_qm (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s3 : S} {σ3 : Fl4Spec.State Unit} + (hA : QKM σ σ3) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (WideW (st := st) Z.T Z.N Z.C Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx _ => ?_) + (fun x hx => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 hx) + (fun x hx => ⟨x, (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 hx, rfl⟩) + (fun x n hx => ?_) (fun hqC => k3 q hqC) + (fun z hz hqC K hK8 hK => J.wideW_scOut hw hz hqC hK8 hK) hA.idle hAS hY3 (hYK.dk q.bv) hd + · obtain ⟨y, hy, e⟩ := hA.tcb hx + exact ⟨y, hy, by rw [e], by rw [e], Or.inl (by rw [e])⟩ + · obtain ⟨w, hw, e⟩ := hA.sc hx + exact ⟨w, hw, Or.inl (by rw [e]), Or.inl (by rw [e])⟩ + +open Fl4Proofs.P1 in +set_option maxHeartbeats 8000000 in +/-- **A TCB's `MCP` word at `J`** (module docstring): FL4's `MCP` word +(offset 3440; P1's `BlkOK` by `plainW_all`), the spec's `tcb_mcpriority` +(`XTopAt.bufSpec`, a `QKM` step), `McpRel` by `McpRel.set`; the other fields +as `JAtZ.reg_frameF` (the write a `WideW` word). -/ +theorem JAtZB.mcpWrF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (v : U64) (m : Fl4Spec.Priority) (hv : v.val = m.toNat) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (mcpA t) v) a + (updK σ t.bv (.tcb { x with tcbMcpriority := m })) ∧ + NatFoot (L := L) Z s σ (L.wr s (mcpA t) v) (updK σ t.bv (.tcb { x with tcbMcpriority := m })) := by + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hta := hT t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hKt : (mcpA t).val = t.val + 3440 := mcpA_val hT ht + have hwa : P1.Aligned (mcpA t) := mcpA_aligned hT ht + set s' := L.wr s (mcpA t) v with hs' + set σ' := updK σ t.bv (.tcb { x with tcbMcpriority := m }) with hσ' + have hw : P1.WritesIn st s s' (· = mcpA t) := P1.writesIn_wr hwa + obtain ⟨hblk, hfoot0, -⟩ := P1.plainW_all (K := 3440#u64) (v := v) J.blk htH (by decide) (by decide) (by decide) + (by decide) (by decide) (by decide) (by decide) + have hfoot : P1.WritesIn st s (L.wr s (mcpA t) v) (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) ∅ s) := hfoot0 + have hA : QKM σ σ' := QKM.mcp hk m + have hX' : XTopAt (L := L) AR Z s' a σ' := + (J.xtop.tcbWr ht 3440#u64 (Or.inl rfl) v).bufSpec (tcbMcpStep_upd hk m) + have hks : L.ksb s' = L.ksb s := hw.1 + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hwW : P1.WritesIn st s s' (WideW (st := st) Z.T Z.N Z.C Z.c s σ) := + hw.mono fun b hb => Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩ + -- the words other than `t`'s `MCP` kept + have keep : ∀ b, P1.Aligned b → b ≠ mcpA t → L.mem s' b = L.mem s b := fun b hb hne => + hw.mem_eq hb hne + have hBW : BufWordRel Z.T (L.mem s') σ' := by + refine J.buf.frame (fun u hu => keep _ (tIpcBufA_aligned (hT u hu)) fun e => ?_) (bufKeep_mcp hk m) + have h1 := tIpcBufA_val (hT u hu) + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega + have hI3 := hX'.irelK (by rw [hks]; exact J.ks) hBW + have hI := J.xtop.irelK J.ks J.buf + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => + Or.inr (Or.inr (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + have hck : Fl4Proofs.P3.CKeep σ σ' := hA.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + have hwF0 : P1.WritesIn st s s (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := + ⟨rfl, rfl, fun _ _ _ => rfl⟩ + -- the yield, SC-reply and reply-link words: not `t`'s `MCP` word + have k2 : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => + keep _ (yieldA_aligned hT hu) fun e => by + have h1 := yieldA_val hT hu + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega + have k3 : ∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q) := fun q hq => + keep _ (sc_reply_word hS hq).1 fun e => by + obtain ⟨y, n, hky, -⟩ := hs.scs q hq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hHq := (hJ.sc q y n (J.c0 q hq) hky).2 + have hz := J.kern.heap.scmin q _ hHq + have h128 : 2 ^ 7 ≤ 2 ^ (n + Fl4Spec.minSchedContextBits) := Nat.pow_le_pow_right (by norm_num) hz + have hv := (sc_reply_word hS hq).2.1 + rw [e, hKt] at hv + have hne : q ≠ t := fun e' => by rw [e', J.zH.1 t ht] at hHq; cases hHq + have := J.kern.heap.disj q _ t _ hHq (J.zH.1 t ht) hne + simp only [P1.HKind.bits] at this + omega + have k4 : ∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r) := by + intro r hr + have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + keep _ (J.rp.aligned hr ha) fun e => by + have := J.rp.tcb r hr t ht; rw [e, hKt] at ha; omega + exact ⟨k _ (Or.inl h8), k _ (Or.inr h16)⟩ + have ck := hA.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hA.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hI3.2.2.1 + have hAI3 : ActIdle σ' := hA.actIdle J.act + have hyk : YKeep σ σ' := by + refine ⟨fun q y hq => ?_, fun q y n hq => ?_, fun q y n hq => ?_⟩ + · by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq; exact ⟨{ x with tcbMcpriority := m }, by simp [hσ', updK], rfl⟩ + · exact ⟨y, by simp [hσ', updK, e, hq], rfl⟩ + · by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq + · exact ⟨y, by simp [hσ', updK, e, hq], rfl⟩ + · by_cases e : q = t.bv + · subst e; simp [hσ', updK] at hq + · exact ⟨y, by simpa [hσ', updK, e] using hq, rfl⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_qm hA hS3 hI3.1 hwW k3 hAI3.1 (J.yld.frame k2 hyk) hyk + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hA.szKeep q.bv y n hq) hks (J.wideW_irq hwW) + (J.idleW_keep hS3 hA.idle hA.isc).2⟩ + have hkern := J.kern_keep hks hwW.2.1 hS3 hA.ct hA.csc (fun sl h => Or.inl (by rw [← hA.orig]; exact h)) + (by rw [hA.arch]) + (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hDbg + (D' := D) + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 hyk, J.mcp.set hT ht hk hv, hBW, hRP', + J.apart.sc_frame hks hA.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hA.idle hA.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hw fun b hb u hu e => Ipc.tw_ne (a := b) hT ht hu (K0 := 3440) (K := 3528) + (by rw [hb, hKt]) (by omega) (by omega) (by omega) (by rw [e]; exact J.tdA_val' hu))), hkern, + J.wideW_utc hwW hF.1 hA.cdt hA.orig hA.arch hDbg, hA.nsc J.nsc, J.inv.updSame hk rfl rfl (Or.inl rfl), hA.cur J.cur, + hA.own J.own, hA.off J.off, hAI3, hA.bsa J.bsa, hA.isorted J.isorted, hA.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameB J hF.1 hA.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + refine ⟨Z.vs, J.cs_frameI hks hX3 hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := 3440) hT ht hu (by rw [hb, hKt]) (by omega) (by omega)) hA.arch (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hA.szKeep hF.1 hA.kback (IpcFrKeep.of_updTcb hk rfl rfl) hC R hKd hck.orig (J.idleW_keep hS3 hA.idle hA.isc).1 J.zD, + NatFoot.ofW J.xtop hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := 3440) hT ht hu (by rw [hb, hKt]) (by omega) (by omega)) + (fun b hb => J.tcbW_nh ht (by rw [hb, hKt]; omega) (by rw [hb, hKt]; omega)) + (natK_updTcb hk fun h => h)⟩ + +open Fl4Proofs.P1 in +set_option maxHeartbeats 8000000 in +/-- **A TCB's flags word at `J`** (SetFlags; `JAtZ.mcpWrJ` at offset 3456, +KENTRY-PLAN g5d): FL4's flags word (P1's `BlkOK` by `plainW_all`), the spec's +`tcb_flags` (`XTopAt.bufSpec`, a `QKM` step), `McpRel` by `McpRel.setF`; the other fields +as `JAtZ.reg_frameF` (the write a `WideW` word). -/ +theorem JAtZB.flagsWrF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (v : U64) (f : Fl4Spec.TcbFlags) (hv : Fl4Spec.wordToTcbFlags v.bv = f) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (flagsA t) v) a + (updK σ t.bv (.tcb { x with tcbFlags := f })) ∧ + NatFoot (L := L) Z s σ (L.wr s (flagsA t) v) (updK σ t.bv (.tcb { x with tcbFlags := f })) := by + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hta := hT t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hKt : (flagsA t).val = t.val + 3456 := flagsA_val hT ht + have hwa : P1.Aligned (flagsA t) := flagsA_aligned hT ht + set s' := L.wr s (flagsA t) v with hs' + set σ' := updK σ t.bv (.tcb { x with tcbFlags := f }) with hσ' + have hw : P1.WritesIn st s s' (· = flagsA t) := P1.writesIn_wr hwa + obtain ⟨hblk, hfoot0, -⟩ := P1.plainW_all (K := 3456#u64) (v := v) J.blk htH (by decide) (by decide) (by decide) + (by decide) (by decide) (by decide) (by decide) + have hfoot : P1.WritesIn st s (L.wr s (flagsA t) v) (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) ∅ s) := hfoot0 + have hA : QKM σ σ' := QKM.flags hk f + have hX' : XTopAt (L := L) AR Z s' a σ' := + (J.xtop.tcbWr ht 3456#u64 (Or.inr (Or.inr (Or.inr rfl))) v).bufSpec (tcbFlagsStep_upd hk f) + have hks : L.ksb s' = L.ksb s := hw.1 + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hwW : P1.WritesIn st s s' (WideW (st := st) Z.T Z.N Z.C Z.c s σ) := + hw.mono fun b hb => Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩ + -- the words other than `t`'s flags kept + have keep : ∀ b, P1.Aligned b → b ≠ flagsA t → L.mem s' b = L.mem s b := fun b hb hne => + hw.mem_eq hb hne + have hBW : BufWordRel Z.T (L.mem s') σ' := by + refine J.buf.frame (fun u hu => keep _ (tIpcBufA_aligned (hT u hu)) fun e => ?_) (bufKeep_flags hk f) + have h1 := tIpcBufA_val (hT u hu) + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega + have hI3 := hX'.irelK (by rw [hks]; exact J.ks) hBW + have hI := J.xtop.irelK J.ks J.buf + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => + Or.inr (Or.inr (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + have hck : Fl4Proofs.P3.CKeep σ σ' := hA.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + have hwF0 : P1.WritesIn st s s (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := + ⟨rfl, rfl, fun _ _ _ => rfl⟩ + -- the yield, SC-reply and reply-link words: not `t`'s flags word + have k2 : ∀ u ∈ Z.T, L.mem s' (yieldA u) = L.mem s (yieldA u) := fun u hu => + keep _ (yieldA_aligned hT hu) fun e => by + have h1 := yieldA_val hT hu + rw [e, hKt] at h1 + have a1 := (hT t ht).align; have a2 := (hT u hu).align + by_cases eu : u = t + · subst eu; omega + · have := val_ne_of_ne eu; omega + have k3 : ∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q) := fun q hq => + keep _ (sc_reply_word hS hq).1 fun e => by + obtain ⟨y, n, hky, -⟩ := hs.scs q hq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hHq := (hJ.sc q y n (J.c0 q hq) hky).2 + have hz := J.kern.heap.scmin q _ hHq + have h128 : 2 ^ 7 ≤ 2 ^ (n + Fl4Spec.minSchedContextBits) := Nat.pow_le_pow_right (by norm_num) hz + have hv := (sc_reply_word hS hq).2.1 + rw [e, hKt] at hv + have hne : q ≠ t := fun e' => by rw [e', J.zH.1 t ht] at hHq; cases hHq + have := J.kern.heap.disj q _ t _ hHq (J.zH.1 t ht) hne + simp only [P1.HKind.bits] at this + omega + have k4 : ∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r) := by + intro r hr + have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + keep _ (J.rp.aligned hr ha) fun e => by + have := J.rp.tcb r hr t ht; rw [e, hKt] at ha; omega + exact ⟨k _ (Or.inl h8), k _ (Or.inr h16)⟩ + have ck := hA.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hA.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hI3.2.2.1 + have hAI3 : ActIdle σ' := hA.actIdle J.act + have hyk : YKeep σ σ' := by + refine ⟨fun q y hq => ?_, fun q y n hq => ?_, fun q y n hq => ?_⟩ + · by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq; exact ⟨{ x with tcbFlags := f }, by simp [hσ', updK], rfl⟩ + · exact ⟨y, by simp [hσ', updK, e, hq], rfl⟩ + · by_cases e : q = t.bv + · subst e; rw [hk] at hq; cases hq + · exact ⟨y, by simp [hσ', updK, e, hq], rfl⟩ + · by_cases e : q = t.bv + · subst e; simp [hσ', updK] at hq + · exact ⟨y, by simpa [hσ', updK, e] using hq, rfl⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_qm hA hS3 hI3.1 hwW k3 hAI3.1 (J.yld.frame k2 hyk) hyk + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hA.szKeep q.bv y n hq) hks (J.wideW_irq hwW) + (J.idleW_keep hS3 hA.idle hA.isc).2⟩ + have hkern := J.kern_keep hks hwW.2.1 hS3 hA.ct hA.csc (fun sl h => Or.inl (by rw [← hA.orig]; exact h)) + (by rw [hA.arch]) + (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hDbg + (D' := D) + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 hyk, J.mcp.setF hT ht hk hv, hBW, hRP', + J.apart.sc_frame hks hA.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hA.idle hA.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hw fun b hb u hu e => Ipc.tw_ne (a := b) hT ht hu (K0 := 3456) (K := 3528) + (by rw [hb, hKt]) (by omega) (by omega) (by omega) (by rw [e]; exact J.tdA_val' hu))), hkern, + J.wideW_utc hwW hF.1 hA.cdt hA.orig hA.arch hDbg, hA.nsc J.nsc, J.inv.updSame hk rfl rfl (Or.inl rfl), hA.cur J.cur, + hA.own J.own, hA.off J.off, hAI3, hA.bsa J.bsa, hA.isorted J.isorted, hA.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameB J hF.1 hA.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + refine ⟨Z.vs, J.cs_frameI hks hX3 hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := 3456) hT ht hu (by rw [hb, hKt]) (by omega) (by omega)) hA.arch (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hA.szKeep hF.1 hA.kback (IpcFrKeep.of_updTcb hk rfl rfl) hC R hKd hck.orig (J.idleW_keep hS3 hA.idle hA.isc).1 J.zD, + NatFoot.ofW J.xtop hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hKt]; omega, by rw [hb, hKt]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := 3456) hT ht hu (by rw [hb, hKt]) (by omega) (by omega)) + (fun b hb => J.tcbW_nh ht (by rw [hb, hKt]; omega) (by rw [hb, hKt]; omega)) + (natK_updTcb hk fun h => h)⟩ + +/-- **A single word of a TCB of `T` outside the VCPU word** (offset 3664) written with the spec state kept is a native +step: the debug words (448..512) and the FPU image (512..3208) are such words. -/ +theorem JAtZB.tcbWordN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) {wa : U64} {Kv : Nat} + (h1 : 256 ≤ Kv) (h2 : Kv + 8 ≤ 4096) (h8 : Kv % 8 = 0) (hne : Kv ≠ 3664) (hwav : wa.val = t.val + Kv) (v : U64) : + NatFoot (L := L) Z s σ (L.wr s wa v) σ := by + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hta := hT t ht + have hwa : P1.Aligned wa := by + have := hta.align + show wa.val % 8 = 0 + omega + have hw : P1.WritesIn st s (L.wr s wa v) (· = wa) := P1.writesIn_wr hwa + exact NatFoot.ofW J.xtop hw (fun b hb => WideW.oobj (st := st) (T := Z.T) (N := Z.N) (C := Z.C) (c := Z.c) (σ := σ) + (Or.inl ⟨t, ht, by rw [hb, hwav]; omega, by rw [hb, hwav]; omega⟩)) + (fun b hb u hu => Sched.tVcpuA_ne (K := Kv) hT ht hu (by rw [hb, hwav]) (by omega) hne) + (fun b hb => J.tcbW_nh ht (by rw [hb, hwav]; omega) (by rw [hb, hwav]; omega)) + (NatK.of_kheap rfl) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrSetPrio.lean b/proofs/Fl4Proofs/P3/JAtZBThrSetPrio.lean new file mode 100644 index 000000000..fdf65909a --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrSetPrio.lean @@ -0,0 +1,169 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **SetPriority**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrPrio +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The SetPriority label at `JAt`** (as `tcb_set_prioX`): the decode with +the authority's MCP by `JAtZ`'s `McpRel` and the authority among the TCBs +(`hcapT`), the caller's restart (`JAtZ.restartJ`), then `set_priority` at +`J` (XTopJointTcbPrio's `JAtZ.setPrioJ`, the current thread's facts by +`JAtZ.prioCur` with `CurInv` carried across the restart, `CurInv.restart`). -/ +theorem JAtZB.tcbSetPrioF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetPriority) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hcapT : x0 ≠ 0#u64 → ∀ q, capV (L.mem s) x0 = .Thread q → q ∈ Z.T) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBSetPriority p ⟨length, buf, call, cslotAddr ssl, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + have hM := J.mcp + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetPriority (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_prio_eq arI s ar p _ hstall, hdec0] + unfold tcb.decode_set_priority tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + by_cases h0 : x0 = 0#u64 + · -- no extra cap: truncated + have hl : excaps.length ≤ 0 := ((hXc.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetPriority + rw [whenE_pos (Or.inr (by omega))] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + obtain ⟨e, he, hxe, he1, ps, hps, hps1⟩ := hXc.get hx0' h0 + have hexl : excaps.length ≠ 0 := by + have := List.length_pos_of_ne_nil (List.ne_nil_of_mem (List.mem_of_getElem? he)); omega + by_cases h1 : length >= 1#u64 + swap + · simp only [h0, ↓reduceIte, if_neg h1, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetPriority + rw [whenE_pos (Or.inl (show (args.map UScalar.bv).length = 0 by rw [List.length_map, ← hlen]; scalar_tac))] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | y0 :: rest => exact ⟨y0, rest, rfl⟩ + have harg := tcb_arg_eq s ⟨length, buf, call, cslotAddr ssl, extra⟩ hct (hargs 0#u64 y0 (by simp)) + simp only [h0, ↓reduceIte, if_pos h1, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, harg] + by_cases hth : ∃ q, capV (L.mem s) x0 = .Thread q + · obtain ⟨q, hq⟩ := hth + rw [thread_arg_thread s 1#u64 hq] + simp only [bind_ok, check_prio_eq] + obtain ⟨xq, hkq, hmq, -⟩ := hM q (hcapT h0 q hq) + have hu : (Fl4Spec.ucast 64 xq.tcbMcpriority) = (L.mem s (mcpA q)).bv := by + apply BitVec.eq_of_toNat_eq; simp [Fl4Spec.ucast, hmq] + have := xq.tcbMcpriority.isLt; omega + have hspec1 : (Fl4Spec.decodeSetPriority (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps).run σ = (Fl4Spec.NonDetE.whenE (y0.bv > (L.mem s (mcpA q)).bv) + (Fl4Spec.NonDetE.throwError (.rangeError 0 (L.mem s (mcpA q)).bv)) >>= fun _ => + (Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl none none + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) none) : Fl4Spec.SEMonad Unit _)).run σ := by + have he1' : e.1 = .threadCap q.bv := by rw [he1, hq]; rfl + unfold Fl4Spec.decodeSetPriority + rw [whenE_neg (by simp [hexl]), retOk_bind, retOk_bind, he, the_some, he1'] + show ((Fl4Spec.NonDetE.returnOk q.bv : Fl4Spec.SEMonad Unit _) >>= _).run σ = _ + rw [retOk_bind] + have hcp : (Fl4Spec.checkPrio (Z := Unit) (Fl4Spec.the (List.map UScalar.bv (y0 :: rest))[0]?) q.bv).run σ = + (Fl4Spec.NonDetE.whenE (y0.bv > (L.mem s (mcpA q)).bv) + (Fl4Spec.NonDetE.throwError (.rangeError 0 (L.mem s (mcpA q)).bv)) : Fl4Spec.SEMonad Unit Unit).run σ := by + unfold Fl4Spec.checkPrio + rw [lE_bind (Notif.threadGet_eq _ hkq), hu]; rfl + rw [ExceptT.run_bind, nd_bind_congr hcp, ExceptT.run_bind]; rfl + by_cases hgt : y0 > L.mem s (mcpA q) + · rw [ite_eq_left hgt] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .rangeError 0 (L.mem s (mcpA q)).bv, ?_, Or.inl ⟨rfl, rfl⟩⟩) + apply run_liftME_err + rw [hspec1, whenE_pos (by rw [GT.gt, ← u64_lt_bv]; exact hgt)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.rangeError 0 _) : + Fl4Spec.SEMonad Unit Unit)) rfl _ + · rw [ite_eq_right hgt] + simp only [bind_ok] + obtain ⟨s1, σ1, x1, hrc, hst1, hk1, hcur1, e1, hsw1, hid1, hct1, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + have hlt : y0.val < 256 := by + have := xq.tcbMcpriority.isLt + have h2 : ¬ (L.mem s (mcpA q)).val < y0.val := hgt + omega + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetPriority (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)).run σ = ({(.ok (.invokeTCB (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl none none + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) none)), σ)}, False) := by + apply run_liftME_ok + rw [hspec1, whenE_neg (by rw [GT.gt, ← u64_lt_bv]; exact hgt), retOk_bind]; rfl + -- the current thread's invariants after the restart + have hC1 : Sched.CurInv σ1 := by + have hC0 := J.curInv (by rw [hni.2]; intro h; cases h) + have h := hC0.restart (xt := x1) (by rw [hcur1]; exact hk1) + rw [hcur1] at h + rw [e1]; exact h + have hne1 : σ1.curThread ≠ σ1.idleThread := by rw [hct1, hid1]; exact hni.1 + obtain ⟨hc1, hp1⟩ := J1.prioCur hne1 hC1 hpT + rw [hrc]; simp only [bind_ok, tcs_prio] + refine WP.spec_bind (J1.setPrioF (fun u h => hsw1 u h) hpT y0 hlt hc1 hp1) ?_ + rintro s2 ⟨σ2, hm2, V2, J2, F2⟩ + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdec, rfl, rfl, σ1, hst1, σ2, ?_, V2, Ac1, Lst1, D1, J2, + J.natTrans F1 F2.cast⟩) + refine invoke_prio_mem ssl _ q.bv blk call don (y := ((), σ2)) ?_ + rw [hm2]; exact Set.mem_singleton _ + · have hne : ∀ r, absCap (capV (L.mem s) x0) ≠ .threadCap r := fun r h => by + obtain ⟨q, hq, -⟩ := absCap_thread h; exact hth ⟨q, hq⟩ + rw [thread_arg_other s 1#u64 (fun q h => hth ⟨q, h⟩)] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetPriority + rw [whenE_neg (by simp [hexl]), retOk_bind, retOk_bind, he, the_some, he1] + generalize absCap (capV (L.mem s) x0) = cc at hne ⊢ + cases cc + all_goals first + | exact absurd rfl (hne _) + | exact run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidCapability 1) : + Fl4Spec.SEMonad Unit Fl4Spec.ObjRef)) rfl _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrSingleStep.lean b/proofs/Fl4Proofs/P3/JAtZBThrSingleStep.lean new file mode 100644 index 000000000..05ab4a915 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrSingleStep.lean @@ -0,0 +1,149 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **ConfigureSingleStepping**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_threxttop.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrExtAux +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The ConfigureSingleStepping label at `JAt`** (as `tcb_single_stepX`): +the arguments by `ArgRel` [J], the caller's restart (`JAtZ.restartJ`), +`JAtZ.cssJ`, then the data tail of `[0]`: `XTop`'s facts by +`nodata_tail_xtop`/`data_tail_xtop`, `JAt` by `JAtZ.runJ` / +`JAtZ.replyWords1J` on the same program (the spec's tail a singleton). -/ +theorem JAtZB.tcbSingleStepF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hp : Z.th p = some .tcb) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .configureSingleStepping) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (buffer : Option Fl4Spec.ObjRef) + (hA : ArgRel (st := st) s σ ct buf buffer 2) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + tcb.decode st arI s ar fl4_abi.label.TCBConfigureSingleStepping p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s ar σ call + (specDec false slbl args ci sl (.Thread p) excaps buffer) ct res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + obtain ⟨htcb, xc, hkc⟩ := hX.curTcb hcur + have hpT : p ∈ Z.T := J.thT p hp + have hctT : ct ∈ Z.T := J.thT ct htcb + have hdec0 : specDec false slbl args ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4ConfigureSingleStepping (Z := Unit) buffer p.bv) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [decode_step_eq arI s ar p _ hstall, hdec0] + obtain ⟨bp, hbp, hbs⟩ := arg_rel hA hct hcur ⟨length, buf, call, slot, extra⟩ rfl 0#u64 (by decide) + obtain ⟨n, hn, hns⟩ := arg_rel hA hct hcur ⟨length, buf, call, slot, extra⟩ rfl 1#u64 (by decide) + unfold tcb.decode_configure_single_stepping + rw [hct]; simp only [bind_ok]; rw [hbp]; simp only [bind_ok]; rw [hn]; simp only [bind_ok] + obtain ⟨s1, σ1, x0, hrc1, hst1, hk0, -, hσ1, -, -, hcu1, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + rw [hkc] at hk0; cases hk0 + have hkc1 : σ1.kheap ct.bv = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1, Notif.stsAct_kheap]; simp [Notif.updK] + rw [hrc1]; simp only [bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4ConfigureSingleStepping (Z := Unit) buffer p.bv)).run σ = + ({(.ok (.invokeFl4TCB (.configureSingleStepping p.bv bp.bv n.bv)), σ)}, False) := by + apply run_liftME_ok + unfold Fl4Spec.decodeFl4ConfigureSingleStepping + rw [run_bind_ok (show (Fl4Spec.fl4Arg (Z := Unit) buffer 0).run σ = _ from hbs), + run_bind_ok (show (Fl4Spec.fl4Arg (Z := Unit) buffer 1).run σ = _ from hns)] + rfl + obtain ⟨xp, hkp, -⟩ := J1.xtop.fin.1.1.2.1.tcb p hpT + refine WP.spec_bind (J1.cssF hpT hkp bp n) ?_ + rintro ⟨consumed, s2, a2⟩ ⟨hcons, ha2, V2, J2, F2⟩ + simp only at hcons ha2 J2 F2 + have F12 := J.natTrans F1 F2.cast + subst hcons ha2 + set Z2 : XSets := Z.mv Z.G V2 with hZ2 + have hX2 := J2.xtop + obtain ⟨xy, hky, hrsy⟩ := css_tcb hkp hkc1 n.bv + have hcurY : ct.bv = (cssState σ1 p.bv xp n.bv).curThread := by + unfold cssState; split <;> (show ct.bv = σ1.curThread; rw [hcu1, hcur]) + have htcb2 : Z2.th ct = some .tcb := htcb + have hact2 : OutsideObjects Z2.th (L.nodeAt s2 (L.cpu s2) layout.node.SCHED_ACTION) := J2.kern.act + have hy := css_mem (bp := bp.bv) hkp n.bv + have hy' : (.ok [0], cssState σ1 p.bv xp n.bv) ∈ ((Fl4Spec.invokeFl4Tcb (Z := Unit) + (.configureSingleStepping p.bv bp.bv n.bv)).run σ1).1 := hy + cases call + · simp only [Bool.false_eq_true, ↓reduceIte] + have hJt : (do + let s5 ← sched.set_thread_state st s2 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a2)) ⦃ res => + ∀ z ∈ (specTail false ct.bv [0] (cssState σ1 p.bv xp n.bv)).1, ∃ (V : XVs) (Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' res.2.1 res.2.2 z.2 ∧ + NatFoot (L := L) Z s σ res.2.1 z.2 ⦄ := by + refine WP.spec_bind (J2.runJ hctT hky (by rw [hrsy])) ?_ + rintro s5 ⟨Lst', J5, F5⟩ + refine (WP.spec_ok _).2 fun z hz => ?_ + rw [specTail_restart hky (by rw [hrsy]) false 0] at hz + simp only [Bool.false_eq_true, ↓reduceIte] at hz + rw [Notif.setThreadState_eq .running hky] at hz + rw [Set.mem_singleton_iff.1 hz] + exact ⟨V2, Ac1, Lst', D1, J5, J.natTrans F12 F5.cast⟩ + refine WP.spec_mono (WP.spec_and (nodata_tail_xtop hX2 hcurY hky (by rw [hrsy]) hact2 [0]) hJt) ?_ + rintro res ⟨⟨hres, hne, z, hz, hRT, -⟩, hJ5⟩ + exact Or.inr ⟨hres, hne, _, DecOut.of_eq hdec, σ1, hst1, [0], _, hy', z, hz, hRT, hJ5 z hz⟩ + · simp only [↓reduceIte] + refine WP.spec_bind (Fl4Proofs.P0.msg.lookup_ipc_buffer.p0 st s2 true ct L.total) ?_ + rintro bf - + simp only [Bool.false_eq_true, ↓reduceIte] + have hI0 : Vs.RInv (L := L) Z2.th Z2.P Z2.L4 Z2.L3 Z2.L2 Z2.L1 Z2.N Z2.T Z2.C Z2.E Z2.R Z2.B Z2.c AR Z2.G s2 a2 + (MrS (cssState σ1 p.bv xp n.bv) ct.bv xy (Endp.ipcBufOf true xy) []) := by + rw [MrS_nil hky]; exact hX2.toRInv + have tail : (do + let (len, s3) ← msg.set_mr st s2 ct bf 0#u64 0#u64 + let s4 ← tcb.reply_words st s3 ct len + let s5 ← sched.set_thread_state st s4 ct sched.TS_RUNNING + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a2)) ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s a2 σ true (Fl4Spec.NonDetE.liftME + Fl4Spec.Fl4Invocation.invokeFl4TCB (Fl4Spec.decodeFl4ConfigureSingleStepping (Z := Unit) buffer p.bv)) + ct res ⦄ := by + have hset : msg.set_mr st s2 ct bf 0#u64 0#u64 ⦃ r => + r = (1#u64, L.wr s2 (Endp.regA ct (Endp.regIdx (Endp.mrIdx 0))) 0#u64) ⦄ := by + unfold msg.set_mr + have h4 : (0#u64 : U64) < msg.N_MSG_REGISTERS := by unfold msg.N_MSG_REGISTERS; decide + simp only [h4, ↓reduceIte] + refine WP.spec_bind (Endp.msg_register_spec 0#u64 (by decide)) ?_ + rintro r0 rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec _ ct _ 0#u64) ?_ + rintro s2' rfl + simp only [lift, bind_ok, WP.spec_ok] + rfl + refine WP.spec_bind (WP.spec_and (setmr_reg_inv _ ct htcb2 hcurY xy _ bf s2 a2 [] hI0 0#u64 rfl (by decide) + 0#u64) hset) ?_ + rintro ⟨len, s3⟩ ⟨⟨hlen, hI3, hk3, hc3⟩, he3⟩ + simp only [Prod.mk.injEq] at hlen hI3 hk3 hc3 he3 + obtain ⟨rfl, rfl⟩ := he3 + refine WP.spec_mono (WP.spec_and (data_tail_xtop hX2 hcurY hky (by rw [hrsy]) hact2 hI3 hk3 hc3 (by decide) + (fun _ _ z hz => by simp [Endp.mrPairs] at hz) 1#u64 + (by cases Endp.ipcBufOf true xy <;> simp [mrLen]) (by decide)) + (J2.replyWords1F hctT hky (by rw [hrsy]) 0#u64)) ?_ + rintro res ⟨⟨hres, hne, z, hz, hRT, -⟩, hJ5⟩ + refine Or.inr ⟨hres, hne, _, DecOut.of_eq hdec, σ1, hst1, [0], _, hy', z, hz, hRT, ?_⟩ + obtain ⟨V5, Ac5, Lst5, D5, J5, F5⟩ := hJ5 z hz + exact ⟨V5, Ac5, Lst5, D5, J5, J.natTrans F12 F5.cast⟩ + split <;> first | exact tail | (split <;> exact tail) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrTimer.lean b/proofs/Fl4Proofs/P3/JAtZBThrTimer.lean new file mode 100644 index 000000000..0602d66e4 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrTimer.lean @@ -0,0 +1,111 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **SetTimer**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrfpu.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrPostF +import Fl4Proofs.P3.JAtZBRestartN +import Fl4Proofs.P3.XTopJointTcb +import Fl4Proofs.P3.XTopJointFinThread +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The SetTimer label at `JAt`** (as `tcb_set_timerX`): the length check, +the caller's restart (`JAtZ.restartJ`), then `timer::insert` by P1's +`timer.insert.foot` (footprint `TmW`, `BlkOK` after, `REPROGRAM` kept or +set) and ScFireJ's `JAtZ.tmw_step`, the spec's reprogram flag chosen as +FL4's (`setTimer_memB`). -/ +theorem JAtZB.tcbSetTimerF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (x0 : U64) (hxS : IsSlot Z.th x0) + (hcap : capAt (L.mem s) x0 = ok (.Thread p)) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .invalidInvocation) + (hext : Fl4Spec.fl4TcbExtLabelOf slbl = some .setTimer) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBSetTimer p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPostF (L := L) arI AR Z H Cs s ar σ + (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) res ⦄ := by + have hdec0 : specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4SetTimer (Z := Unit) (args.map UScalar.bv) p.bv) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_ext hlbl hext]; rfl + rw [decode_timer_eq arI s ar p _ hstall, hdec0] + by_cases h1 : length < 2#u64 + · simp only [h1, ↓reduceIte, error.truncated, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + unfold Fl4Spec.decodeFl4SetTimer + rw [whenE_pos (by rw [List.length_map, ← hlen]; scalar_tac)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + simp only [h1, ↓reduceIte] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, hlen with + | y0 :: y1 :: rest, _ => exact ⟨_, _, _, rfl⟩ + | [], h => exfalso; simp only [List.length_nil] at h; scalar_tac + | [_], h => exfalso; simp only [List.length_singleton] at h; scalar_tac + have ha0 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have ha1 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 1#u64 = ok y1 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 1#u64 y1 (by simp) + rw [ha0, ha1]; simp only [bind_ok] + obtain ⟨s1, σ1, x, hrc1, hst1, hk, hcur, -, -, -, -, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + obtain ⟨-, x', hk', hrun⟩ := J.curRunnable hni hct + rw [hk] at hk'; cases hk' + -- the slots kept by the caller's restart: the target's cap, so the target out of the dead set + have hSS : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := XTopAt.restart J.xtop hcur hk hrun + rw [hs1, WP.spec_ok] at h; exact h.2.1 + have hcap1 : capAt (L.mem s1) x0 = ok (.Thread p) := by + rw [capAt_capV, hSS.capV hxS, ← capAt_capV]; exact hcap + have J1' := J1.undead hxS hcap1 hpT + rw [hrc1]; simp only [bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeFl4TCB + (Fl4Spec.decodeFl4SetTimer (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) p.bv)).run σ = + ({(.ok (.invokeFl4TCB (.setTimer p.bv y0.bv y1.bv)), σ)}, False) := by + apply run_liftME_ok + unfold Fl4Spec.decodeFl4SetTimer + rw [whenE_neg (by simp), retOk_bind] + rfl + have hTe : Z.T = P1.Heap.tcbs H := J1.tcbs_eq.1 + have hpH : p ∈ P1.Heap.tcbs H := by rw [← hTe]; exact hpT + have hc1 : L.cpu s1 = Z.c := by obtain ⟨Q, R0, hc, -⟩ := J1.xtop.fin.1.1.1.lists; exact hc.cur + have hI1 := J1.blk.tinv + refine WP.spec_bind (WP.spec_and (P1.timer.insert.foot s1 p y0 y1 hI1 hpH) + (insert_td s1 p y0 y1 hI1.tcbs hpH hI1.rb hI1.nodes J1.kern.cpuCs)) ?_ + rintro s2 ⟨⟨hblk2, -, hw, hR⟩, htd2⟩ + rw [hc1] at hw hR + have hw' : P1.WritesIn st s1 s2 (TmW (L := L) Z.T Cs Z.c s1) := + hw.mono fun b hb => by unfold TmW; rw [hTe]; exact hb + -- the dead TCBs (`p` not among them) stay unarmed + have hTd2 : ∀ q ∈ D1 \ {p}, H q = some .tcb → L.mem s1 (P1.tdA q) = 0#u64 → L.mem s2 (P1.tdA q) = 0#u64 := + fun q hq hH h0 => htd2 q (show q ∈ P1.Heap.tcbs H from hH) (fun e => hq.2 (by subst e; exact Set.mem_singleton _)) h0 + obtain ⟨r, J2⟩ := J1'.tmw_step hw' hR hblk2 hTd2 + have F2 : NatFoot (L := L) Z s1 σ1 s2 { σ1 with reprogramTimer := σ1.reprogramTimer || r } := + ⟨(J1'.tmwN hw').ss, (J1'.tmwN hw').fw, NatK.of_kheap rfl⟩ + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdec, rfl, σ1, hst1, _, + setTimer_memB blk call don σ1 p.bv y0.bv y1.bv r, Z.vs, Ac1, Lst1, D1 \ {p}, J2, J.natTrans F1 F2⟩) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrTls.lean b/proofs/Fl4Proofs/P3/JAtZBThrTls.lean new file mode 100644 index 000000000..81dc81a0c --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrTls.lean @@ -0,0 +1,169 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **SetTLSBase**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The SetTLSBase label at `JAt`** (as `tcb_set_tlsX`): the caller's +restart (`JAtZ.restartJ`), the `TLS_BASE` register (`JAtZ.regWrJ`), and for +the current thread `reschedule_required` as a scheduler step +(`JAtZ.schedJ`; `ActReady` from the `JAtZ` after the write, `Sched.Inv.actReady`); the +current-thread word outside the objects [P]. -/ +theorem JAtZB.tcbSetTlsF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) + (p : U64) (hp : Z.th p = some .tcb) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetTLSBase) + (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) + (hcw : OutsideObjects Z.th (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD)) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBSetTLSBase p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + have hP := hX.rel.1 + obtain ⟨hcur, -⟩ := J.curEq hct + have htcb := (hX.curTcb hcur).1 + have hact := J.kern.act + have hpT : p ∈ Z.T := J.thT p hp + have hdec0 : (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetTlsBase (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv)) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + unfold tcb.decode + rw [hstall, hdec0] + simp only [bind_ok, u64_eq_iff, lbl_tcbReadRegisters_val, lbl_tcbWriteRegisters_val, lbl_tcbCopyRegisters_val, + lbl_tcbConfigure_val, lbl_tcbSetPriority_val, lbl_tcbSetMCPriority_val, lbl_tcbSetSchedParams_val, + lbl_tcbSetTimeoutEndpoint_val, lbl_tcbSetIPCBuffer_val, lbl_tcbSetSpace_val, lbl_tcbSuspend_val, + lbl_tcbResume_val, lbl_tcbBindNotification_val, lbl_tcbUnbindNotification_val, lbl_tcbSetBreakpoint_val, + lbl_tcbGetBreakpoint_val, lbl_tcbUnsetBreakpoint_val, lbl_tcbConfigureSingleStepping_val, + lbl_tcbSetTLSBase_val, lbl_tcbSetEPTRoot_val] + simp only [Nat.reduceEqDiff, ↓reduceIte] + refine let_pair_spec s ar (fun s1 => tcb.decode_set_tls_base st s1 p ⟨length, buf, call, slot, extra⟩) _ ?_ + unfold tcb.decode_set_tls_base + by_cases h1 : length < 1#u64 + · simp only [if_pos h1, error.truncated, bind_ok] + refine (WP.spec_ok _).2 ?_ + apply Or.inl + refine ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩ + apply run_liftME_err + unfold Fl4Spec.decodeSetTlsBase + rw [whenE_pos (show (args.map UScalar.bv).length = 0 by rw [List.length_map, ← hlen]; scalar_tac)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + simp only [if_neg h1] + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | y0 :: rest => exact ⟨y0, rest, rfl⟩ + have harg : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetTlsBase (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv))).run σ = + ({(.ok (.invokeTCB (.setTLSBase p.bv y0.bv)), σ)}, False) := by + apply run_liftME_ok + unfold Fl4Spec.decodeSetTlsBase + rw [whenE_neg (by simp), retOk_bind] + show (Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.setTLSBase p.bv (Fl4Spec.ucast 64 y0.bv)) : + Fl4Spec.SEMonad Unit _).run σ = _ + rw [ucast64_self]; rfl + rw [harg]; simp only [bind_ok] + obtain ⟨s1, σ1, x0, hrc1, hst1, -, -, -, -, -, hcur1', hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + have hc1 := set_thread_state_cases (L := L) s ct sched.TS_RESTART + rw [hs1, WP.spec_ok] at hc1 + rw [hrc1]; simp only [bind_ok] + obtain ⟨w, hw, hwb⟩ := sanitise_tls y0 + rw [hw]; simp only [bind_ok, set_reg_tls_eq] + -- the current thread is unchanged + have hctv : L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = ct := by + have := cur_thread_spec' (L := L) s; rw [hct, WP.spec_ok] at this; exact this.symm + have hpf := hP.fits p _ hp + have hcf := hP.fits ct _ htcb + have hpa := hP.align p _ hp + have hca := hP.align ct _ htcb + simp only [objSize] at hpf hcf + have hstv : (stA ct).val = ct.val + 3328 := by + rw [stA_eq, wordOf_val (by simp; omega)]; simp + have hcs2 : sched.cur_thread st (L.wr s1 (tlsA p) w) = ok ct := by + rw [cur_thread_eq] + simp only [P1.nodeAt_wr, L.cpu_wr] + have hcwA : P1.Aligned (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) := hcw.1 + have hn1 : L.nodeAt s1 (L.cpu s1) layout.node.CUR_THREAD = L.nodeAt s (L.cpu s) layout.node.CUR_THREAD := by + rcases hc1 with rfl | rfl <;> simp + rw [hn1, L.mem_wr_ne (by unfold P1.Aligned; rw [tlsA_val (by omega)]; omega) hcwA + (fun e => out_ne hcw hp (w := tlsA p) (by rw [tlsA_val (by omega)]; omega) e)] + rcases hc1 with rfl | rfl + · rw [L.mem_wr_ne (by unfold P1.Aligned; rw [hstv]; omega) hcwA + (fun e => out_ne hcw htcb (by rw [hstv]; omega) e), hctv] + · rw [L.mem_wr_ne hact.1 hcwA (fun e => cur_ne_action s e.symm), + L.mem_wr_ne (by unfold P1.Aligned; rw [hstv]; omega) hcwA + (fun e => out_ne hcw htcb (by rw [hstv]; omega) e), hctv] + rw [hcs2]; simp only [bind_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq] + -- the spec: the target's TCB, its register at `J` + have hcur1 : σ1.curThread = ct.bv := by rw [hcur1', hcur] + obtain ⟨x, hkx, -⟩ := J1.xtop.fin.1.1.2.1.tcb p hpT + have htl : tlsA p = regA p (regIdx Fl4Spec.tlsBaseRegister) := by + unfold tlsA regA layout.tcb.REGS; rfl + have hXr := reg_wr_irel J1.ip (J1.xtop.irelK J1.ks J1.buf) hpT hkx Fl4Spec.tlsBaseRegister (by decide) w + obtain ⟨V2, J2, F2⟩ := J1.regWrJ hpT hkx Fl4Spec.tlsBaseRegister w hXr + rw [← htl] at J2 F2 + by_cases hpc : p = ct + · subst hpc + simp only [decide_true, ↓reduceIte] + have hA2 : Notif.ActReady _ := J2.inv.actReady + obtain ⟨σ', hres, hK', hdl', hds'⟩ := rescheduleRequired_k hA2 + have hS := J2.xtop.fin.1.1.1 + have hN := J2.xtop.fin.1.1.2.2.1 + obtain ⟨s3, hrun3, ⟨σ3, he3, -, -, -, -⟩, hw⟩ := spec_exists (WP.spec_and + (Sched.reschedule_required_refines _ _ hN hS hA2) (Sched.reschedule_required_sfootI hS)) + rw [hrun3]; simp only [bind_ok] + rw [hres] at he3 + have e3 : σ3 = σ' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst he3))).2.symm + subst σ3 + have hSt := schedStep_resched (th := Z.th) hA2 hrun3 hres hK' hdl' hds' + obtain ⟨Ac3, Lst3, D3, J3⟩ := J2.schedJ hSt + have F3 := J2.schedStepN hSt + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst1, σ', ?_, V2, Ac3, Lst3, D3, J3, + J.natTrans F1 (J1.natTrans F2 F3.cast).cast⟩) + refine invoke_tls_mem (y := ((), σ')) hkx y0.bv ?_ blk call don + rw [← hwb, hcur1] + simp only [Fl4Spec.NonDet.when, if_pos trivial] + rw [hres]; rfl + · have hne : decide (p = ct) = false := by simp [hpc] + rw [hne]; simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst1, _, ?_, V2, Ac1, Lst1, D1, J2, + J.natTrans F1 F2.cast⟩) + refine invoke_tls_mem (y := ((), Notif.updK σ1 p.bv (.tcb (Notif.setReg x Fl4Spec.tlsBaseRegister w.bv)))) + hkx y0.bv ?_ blk call don + have : ¬ p.bv = σ1.curThread := fun e => hpc (UScalar.eq_of_val_eq (by + rw [hcur1] at e; have := congrArg BitVec.toNat e; simpa using this)) + rw [← hwb] + simp only [Fl4Spec.NonDet.when, if_neg this] + exact Set.mem_singleton _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrUnbind.lean b/proofs/Fl4Proofs/P3/JAtZBThrUnbind.lean new file mode 100644 index 000000000..a11e4525d --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrUnbind.lean @@ -0,0 +1,122 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **UnbindNotification**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The UnbindNotification label at `JAt`** (as `tcb_unbind_ntfnX`): the +bound check; when bound, the caller's restart (`JAtZ.restartJ`, the bound +word kept: the spec's restart keeps the thread's binding), then +`JAtZ.unbindNJ`. -/ +theorem JAtZB.tcbUnbindNtfnF (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) (p : U64) (hpT : p ∈ Z.T) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbUnbindNotification) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) (ct : U64) (hct : sched.cur_thread st s = ok ct) + (length buf : U64) (call : Bool) (slot : obj.Slot) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBUnbindNotification p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl args ci sl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + have hdec0 : specDec false slbl args ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeUnbindNotification (Z := Unit) (.threadCap p.bv)) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + unfold tcb.decode + rw [hstall, hdec0] + simp only [bind_ok, u64_eq_iff, lbl_tcbSetEPTRoot_val, lbl_tcbReadRegisters_val, lbl_tcbWriteRegisters_val, lbl_tcbCopyRegisters_val, + lbl_tcbConfigure_val, lbl_tcbSetPriority_val, lbl_tcbSetMCPriority_val, lbl_tcbSetSchedParams_val, + lbl_tcbSetTimeoutEndpoint_val, lbl_tcbSetIPCBuffer_val, lbl_tcbSetSpace_val, lbl_tcbSuspend_val, + lbl_tcbResume_val, lbl_tcbBindNotification_val, lbl_tcbUnbindNotification_val] + simp only [Nat.reduceEqDiff, ↓reduceIte] + show (do + let i ← obj.Tcb.bound_ntfn st p s + if (i : U64).val = (0#u64).val then do + let r ← error.illegal Unit + ok (r, s, ar) + else do + let s2 ← tcb.restart_caller st s + let s3 ← notification.unbind_notification st s2 p + ok (core.result.Result.Ok (), s3, ar)) ⦃ r => ThrPost (L := L) arI AR Z H Cs s ar σ + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeUnbindNotification (Z := Unit) (Fl4Spec.Cap.threadCap p.bv))) r ⦄ + rw [tcb_bound_eq] + simp only [bind_ok, ← u64_eq_iff] + obtain ⟨xp, hkp, hxp⟩ := hX.fin.1.1.2.1.tcb p hpT + have hbp : xp.tcbBoundNotification = Notif.optRef (L.mem s (P1.boundA p)) := hxp.bound + have hsp0 : (Fl4Spec.decodeUnbindNotification (Z := Unit) (.threadCap p.bv)).run σ = + ((match Notif.optRef (L.mem s (P1.boundA p)) with + | none => Fl4Spec.NonDetE.throwError .illegalOperation + | some _ => Fl4Spec.NonDetE.returnOk () : Fl4Spec.SEMonad Unit Unit) >>= fun _ => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.notificationControl p.bv none)).run σ := by + simp only [Fl4Spec.decodeUnbindNotification] + rw [lE_bind (m := Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbBoundNotification p.bv) + (Notif.threadGet_eq _ hkp), hbp] + generalize Notif.optRef (L.mem s (P1.boundA p)) = o + cases o <;> rfl + by_cases hb0 : L.mem s (P1.boundA p) = 0#u64 + · simp only [hb0, ↓reduceIte, error.illegal, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) + apply run_liftME_err + rw [hsp0] + simp only [Notif.optRef, hb0, ↓reduceIte] + exact run_throw_bind _ _ _ + simp only [hb0, ↓reduceIte] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeUnbindNotification (Z := Unit) (.threadCap p.bv))).run σ = + ({(.ok (.invokeTCB (.notificationControl p.bv none)), σ)}, False) := by + apply run_liftME_ok + rw [hsp0] + simp only [Notif.optRef, hb0, ↓reduceIte] + exact (congrArg (fun m : Fl4Spec.SEMonad Unit Fl4Spec.TcbInvocation => m.run σ) (retOk_bind () _)).trans rfl + obtain ⟨s1, σ1, x, hrc1, hst, hk, -, he1, -, -, -, -, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + -- the bound word after the restart, through the spec (the restart keeps the binding) + have hb1 : L.mem s1 (P1.boundA p) ≠ 0#u64 := by + intro h1 + obtain ⟨xp1, hkp1, hxp1⟩ := J1.xtop.fin.1.1.2.1.tcb p hpT + have hb1' : xp1.tcbBoundNotification = Notif.optRef (L.mem s1 (P1.boundA p)) := hxp1.bound + have b1 : xp1.tcbBoundNotification = none := by rw [hb1', h1]; simp [Notif.optRef] + have b0 : xp.tcbBoundNotification ≠ none := by rw [hbp]; simp [Notif.optRef, hb0] + apply b0 + rw [he1, stsAct_kheap, updK_kheap] at hkp1 + by_cases hpc : p.bv = ct.bv + · rw [hpc, Function.update_self] at hkp1 + rw [hpc, hk] at hkp + cases hkp; cases hkp1; exact b1 + · rw [Function.update_of_ne hpc, hkp] at hkp1 + cases hkp1; exact b1 + rw [hrc1]; simp only [bind_ok] + refine WP.spec_bind (J1.unbindNJD hpT hb1) ?_ + rintro s3 ⟨σ', hun, -, -, -, V, J2, F2⟩ + refine (WP.spec_ok _).2 ?_ + have hz : ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don + (.invokeTCB (.notificationControl p.bv none))).run σ1).1 := by + show _ ∈ ((Fl4Spec.NonDetE.liftE _).run σ1).1 + exact mem_liftE (x := ([], σ')) (mem_bind_nd (x := ((), σ')) (by rw [hun]; exact Set.mem_singleton _) rfl) + exact Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst, _, hz, V, Ac1, Lst1, D1, J2, J.natTrans F1 F2.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrWrite.lean b/proofs/Fl4Proofs/P3/JAtZBThrWrite.lean new file mode 100644 index 000000000..6b43c7ab6 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrWrite.lean @@ -0,0 +1,389 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **`write_registers`**. + +Generated by `handoff/tools/gen_thrregs.py` (`JAtZB` siblings with the native footprint `NatFoot`). New file; no +existing statement changed. Premise [P] `MIN_BUDGET` (as native). +-/ +import Fl4Proofs.P3.XTopJointWriteRegs +import Fl4Proofs.P3.JAtZBRestartN +import Fl4Proofs.P3.XTopJointReply +import Fl4Proofs.P3.XTopJointRegFrame +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A step writing only the registers of one thread of `T` has the native footprint. -/ +theorem JAtZB.rcvRegN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p : U64} (hpT : p ∈ Z.T) {s' : S} + {σ' : Fl4Spec.State Unit} (hw : P1.WritesIn st s s' (RcvReg p)) (hK : NatK σ σ') : + NatFoot (L := L) Z s σ s' σ' := by + have hS := J.xtop.fin.1.1.1 + have hta := J.ip.e.tcb p hpT + have hwF : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := hw.mono fun b ⟨r, hb⟩ => by + have := regIdx_lt r + have hr := regA_val hta this + exact regFW_finW hS (Or.inl (Or.inl ⟨p, hpT, by omega, by omega⟩)) + exact NatFoot.ofW J.xtop hwF (fun _ hb => Ipc.FinW.oobj hS hb) (fun _ hb => Ipc.FinW.nv hS hb) + (fun _ hb => J.finW_nh hb) hK + +set_option maxHeartbeats 8000000 in +/-- **The next sanitised register of the target at `J`** (module docstring). -/ +theorem JAtZB.sanWrF {σ0 : Fl4Spec.State Unit} {p : U64} {x0 : Fl4Spec.Tcb} {rs : List Fl4Spec.Register} + {vs : List Fl4Spec.Word} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a (updK σ0 p.bv (.tcb (sanRegs x0 rs vs)))) + (hpT : p ∈ Z.T) (hl : rs.length = vs.length) (r : Fl4Spec.Register) (v : Fl4Spec.Word) (w : U64) + (hw : w.bv = Fl4Spec.sanitiseRegister false r v) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (regA p (regIdx r)) w) a + (updK σ0 p.bv (.tcb (sanRegs x0 (rs ++ [r]) (vs ++ [v])))) := by + have hk : (updK σ0 p.bv (.tcb (sanRegs x0 rs vs))).kheap p.bv = some (.tcb (sanRegs x0 rs vs)) := by + simp [updK] + have hX := reg_wr_irelN J.ip (J.xtop.irelK J.ks J.buf) hpT hk r w + obtain ⟨V, J1, -⟩ := J.regWrJ hpT hk r w hX + refine ⟨V, ?_⟩ + have e : updK (updK σ0 p.bv (.tcb (sanRegs x0 rs vs))) p.bv (.tcb (setReg (sanRegs x0 rs vs) r w.bv)) = + updK σ0 p.bv (.tcb (sanRegs x0 (rs ++ [r]) (vs ++ [v]))) := by + rw [sanRegs_snoc x0 rs vs hl r v, ← hw]; simp only [updK, Function.update_idem] + rw [← e]; exact J1 + +set_option maxHeartbeats 8000000 in +/-- **The frame-register loop of `write_registers` at `J`** (as `wr_loop0`). -/ +theorem wrB_loop0 {SR : S → Fl4Spec.State Unit → Prop} (hH : EpHyps st Z.T Z.E Z.R Z.B SR) + (hP : EPlaced Z.T Z.E Z.R Z.B) {s0 : S} {σ0 : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s0 a σ0) + {dest : U64} (hd : dest ∈ Z.T) {x0 : Fl4Spec.Tcb} (hk0 : σ0.kheap dest.bv = some (.tcb x0)) + {ct buf n : U64} (hn : n.val ≤ 20) {V : List Fl4Spec.Word} + (hVl : n.val ≤ V.length) (hRd : WrRead (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 ct buf n V) + (hI : RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 s0 [] []) : + tcb.write_registers_loop0 st s0 dest buf ct n 0#u64 ⦃ s2 => + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 s2 (wrRegs.take (min n.val 18)) (V.take (min n.val 18)) ∧ + (∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D s2 a + (updK σ0 dest.bv (.tcb (sanRegs x0 (wrRegs.take (min n.val 18)) (V.take (min n.val 18)))))) ∧ + P1.WritesIn st s0 s2 (RcvReg dest) ⦄ := by + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hfl : Fl4Spec.frameRegisters.length = 18 := rfl + have hgl : Fl4Spec.gpRegisters.length = 2 := rfl + have hwl : wrRegs.length = 20 := rfl + have hta := hP.tcb dest hd + unfold tcb.write_registers_loop0 + apply loop.spec_decr_nat (measure := fun x => 18 - x.2.val) + (inv := fun x => x.2.val ≤ min n.val 18 ∧ + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 x.1 (wrRegs.take x.2.val) (V.take x.2.val) ∧ + (∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D x.1 a + (updK σ0 dest.bv (.tcb (sanRegs x0 (wrRegs.take x.2.val) (V.take x.2.val))))) ∧ + P1.WritesIn st s0 x.1 (RcvReg dest)) + · rintro ⟨s', i⟩ ⟨hi, hI', ⟨V', J'⟩, hw'⟩ + unfold tcb.write_registers_loop0.body + simp only at hi hI' J' hw' ⊢ + by_cases h1 : i < tcb.N_FRAME + swap + · rw [if_neg h1, WP.spec_ok] + have : i.val = min n.val 18 := by have := (P1.gt_iff_val).not.1 h1; rw [hf] at this; omega + rw [← this]; exact ⟨hI', ⟨V', J'⟩, hw'⟩ + rw [if_pos h1] + have hi18 : i.val < 18 := by have := (P1.gt_iff_val).1 h1; rwa [hf] at this + by_cases h2 : i < n + swap + · rw [if_neg h2, WP.spec_ok] + have : i.val = min n.val 18 := by have := (P1.gt_iff_val).not.1 h2; omega + rw [← this]; exact ⟨hI', ⟨V', J'⟩, hw'⟩ + rw [if_pos h2] + have hin : i.val < n.val := (P1.gt_iff_val).1 h2 + simp only [lift, bind_ok] + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ (by rw [cast_to_us' i (by omega)]; simp; omega)) ?_ + rintro r hr + have hrv : r = regIdx wrRegs[i.val] := by + rw [hr, List.getElem_of_eq frame_regs_val, List.getElem_map] + congr 1 + simp [cast_to_us' i (by omega), wrRegs, List.getElem_append_left (show i.val < Fl4Spec.frameRegisters.length by omega)] + subst hrv + obtain ⟨i2, h2e, h2v⟩ := spec_exists (U64.add_spec (x := i) (y := 2#u64) (by scalar_tac)) + rw [h2e, bind_ok] + have hi2 : i2.val = i.val + 2 := by simpa using h2v + obtain ⟨w0, hw0, hw0v⟩ := hRd i.val i2 s' hi2 hin hI' + rw [hw0, bind_ok] + obtain ⟨w, hw, hwv⟩ := sanitise_eq wrRegs[i.val] w0 + rw [hw, bind_ok] + rw [hw0v] at hwv + have hl : (wrRegs.take i.val).length = (V.take i.val).length := by + simp [List.length_take]; omega + refine WP.spec_bind (WP.spec_and (set_reg_rg_inv hH hP hd hI' hl wrRegs[i.val] V[i.val]! w hwv) + (Endp.obj.Tcb.set_reg.spec s' dest _ w)) ?_ + rintro s2 ⟨hI2, rfl⟩ + obtain ⟨V2, J2⟩ := J'.sanWrF hd hl wrRegs[i.val] V[i.val]! w hwv + have hw2 := wr_regW hta wrRegs[i.val] w hw' + obtain ⟨i4, h4e, h4v⟩ := spec_exists (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 + refine ⟨⟨by show i4.val ≤ min n.val 18; rw [hi4]; exact Nat.le_min.2 ⟨by omega, by omega⟩, ?_⟩, + by show 18 - i4.val < 18 - i.val; omega⟩ + dsimp only + have hVl' : i.val < V.length := by omega + have hiL : i.val < wrRegs.length := by rw [wrRegs_length]; omega + rw [hi4, List.take_succ, List.take_succ, List.getElem?_eq_getElem hiL, List.getElem?_eq_getElem hVl'] + simp only [Option.toList_some] + rw [← getElem!_pos V i.val hVl'] + exact ⟨hI2, ⟨V2, J2⟩, hw2⟩ + · refine ⟨by simp, by simpa using hI, ⟨Z.vs, ?_⟩, P1.WritesIn.refl _ _⟩ + show JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D s0 a (updK σ0 dest.bv (.tcb (sanRegs x0 [] []))) + rw [show sanRegs x0 [] [] = x0 from rfl, Endp.updK_self hk0] + simpa using J + +set_option maxHeartbeats 8000000 in +/-- **The integer-register loop of `write_registers` at `J`** (as +`wr_loop1`). -/ +theorem wrB_loop1 {SR : S → Fl4Spec.State Unit → Prop} (hH : EpHyps st Z.T Z.E Z.R Z.B SR) + (hP : EPlaced Z.T Z.E Z.R Z.B) {s0 s1 : S} {σ0 : Fl4Spec.State Unit} + {dest : U64} (hd : dest ∈ Z.T) {x0 : Fl4Spec.Tcb} + {ct buf n : U64} (hn : n.val ≤ 20) {V : List Fl4Spec.Word} + (hVl : n.val ≤ V.length) (hRd : WrRead (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 ct buf n V) + (hI : RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 s1 (wrRegs.take (min n.val 18)) (V.take (min n.val 18))) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s1 a + (updK σ0 dest.bv (.tcb (sanRegs x0 (wrRegs.take (min n.val 18)) (V.take (min n.val 18)))))) + (hw : P1.WritesIn st s0 s1 (RcvReg dest)) : + tcb.write_registers_loop1 st s1 dest buf ct n 0#u64 ⦃ s2 => + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 s2 (wrRegs.take n.val) (V.take n.val) ∧ + (∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D s2 a + (updK σ0 dest.bv (.tcb (sanRegs x0 (wrRegs.take n.val) (V.take n.val))))) ∧ + P1.WritesIn st s0 s2 (RcvReg dest) ⦄ := by + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hg : tcb.N_GP.val = 2 := by unfold tcb.N_GP; rfl + have hfl : Fl4Spec.frameRegisters.length = 18 := rfl + have hgl : Fl4Spec.gpRegisters.length = 2 := rfl + have hwl : wrRegs.length = 20 := rfl + have hta := hP.tcb dest hd + unfold tcb.write_registers_loop1 + apply loop.spec_decr_nat (measure := fun x => 2 - x.2.val) + (inv := fun x => x.2.val ≤ 2 ∧ (18 + x.2.val ≤ n.val ∨ (x.2.val = 0 ∧ n.val ≤ 18)) ∧ + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 dest x0 x.1 (wrRegs.take (min n.val (18 + x.2.val))) + (V.take (min n.val (18 + x.2.val))) ∧ + (∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D x.1 a + (updK σ0 dest.bv (.tcb (sanRegs x0 (wrRegs.take (min n.val (18 + x.2.val))) + (V.take (min n.val (18 + x.2.val))))))) ∧ + P1.WritesIn st s0 x.1 (RcvReg dest)) + · rintro ⟨s', k⟩ ⟨hk2, hkn, hI', ⟨V', J'⟩, hw'⟩ + unfold tcb.write_registers_loop1.body + simp only at hk2 hkn hI' J' hw' ⊢ + by_cases h1 : k < tcb.N_GP + swap + · rw [if_neg h1, WP.spec_ok] + have hk : k.val = 2 := by have := (P1.gt_iff_val).not.1 h1; rw [hg] at this; omega + have : min n.val (18 + k.val) = n.val := by omega + rw [this] at hI' J'; exact ⟨hI', ⟨V', J'⟩, hw'⟩ + rw [if_pos h1] + have hk2' : k.val < 2 := by have := (P1.gt_iff_val).1 h1; rwa [hg] at this + obtain ⟨i, hie, hiv⟩ := spec_exists (U64.add_spec (x := k) (y := tcb.N_FRAME) (by rw [hf]; scalar_tac)) + rw [hie, bind_ok] + have hiv' : i.val = k.val + 18 := by rw [← hf]; simpa using hiv + by_cases h2 : i < n + swap + · rw [if_neg h2, WP.spec_ok] + have : min n.val (18 + k.val) = n.val := by have := (P1.gt_iff_val).not.1 h2; omega + rw [this] at hI' J'; exact ⟨hI', ⟨V', J'⟩, hw'⟩ + rw [if_pos h2] + have hin : i.val < n.val := (P1.gt_iff_val).1 h2 + have hm : min n.val (18 + k.val) = i.val := by omega + rw [hm] at hI' J' + simp only [lift, bind_ok] + refine WP.spec_bind (Aeneas.Std.Array.index_usize_spec _ _ (by rw [cast_to_us' k (by omega)]; simp; omega)) ?_ + rintro r hr + have hrv : r = regIdx wrRegs[i.val] := by + rw [hr, List.getElem_of_eq gp_regs_val, List.getElem_map] + congr 1 + simp only [wrRegs, hiv', cast_to_us' k (by omega)] + rw [List.getElem_append_right (by rw [hfl]; omega)] + simp [hfl] + subst hrv + obtain ⟨i2, h2e, h2v⟩ := spec_exists (U64.add_spec (x := i) (y := 2#u64) (by scalar_tac)) + rw [h2e, bind_ok] + have hi2 : i2.val = i.val + 2 := by simpa using h2v + obtain ⟨w0, hw0, hw0v⟩ := hRd i.val i2 s' hi2 hin hI' + rw [hw0, bind_ok] + obtain ⟨w, hw, hwv⟩ := sanitise_eq wrRegs[i.val] w0 + rw [hw, bind_ok] + rw [hw0v] at hwv + have hl : (wrRegs.take i.val).length = (V.take i.val).length := by + simp [List.length_take]; omega + refine WP.spec_bind (WP.spec_and (set_reg_rg_inv hH hP hd hI' hl wrRegs[i.val] V[i.val]! w hwv) + (Endp.obj.Tcb.set_reg.spec s' dest _ w)) ?_ + rintro s2 ⟨hI2, rfl⟩ + obtain ⟨V2, J2⟩ := J'.sanWrF hd hl wrRegs[i.val] V[i.val]! w hwv + have hw2 := wr_regW hta wrRegs[i.val] w hw' + obtain ⟨k1, h4e, h4v⟩ := spec_exists (U64.add_spec (x := k) (y := 1#u64) (by scalar_tac)) + rw [h4e, bind_ok, WP.spec_ok] + have hk1 : k1.val = k.val + 1 := by simpa using h4v + refine ⟨⟨by show k1.val ≤ 2; omega, by show 18 + k1.val ≤ n.val ∨ (k1.val = 0 ∧ n.val ≤ 18); omega, ?_⟩, + by show 2 - k1.val < 2 - k.val; omega⟩ + dsimp only + have hm1 : min n.val (18 + k1.val) = i.val + 1 := by omega + have hVl' : i.val < V.length := by omega + have hiL : i.val < wrRegs.length := by rw [wrRegs_length]; omega + rw [hm1, List.take_succ, List.take_succ, List.getElem?_eq_getElem hiL, List.getElem?_eq_getElem hVl'] + simp only [Option.toList_some] + rw [← getElem!_pos V i.val hVl'] + exact ⟨hI2, ⟨V2, J2⟩, hw2⟩ + · refine ⟨by simp, by simp; omega, by simpa using hI, ⟨Z.vs, by simpa using J⟩, hw⟩ + +set_option maxHeartbeats 32000000 in +/-- **`write_registers` at `J`** (module docstring): the target `p` not the +caller `ct`; its cap in a slot (`hxS`, `hcap`, for `JAtZ.restartTcbJ`); +no switch at the start (the caller's restart's); +the caller's buffer mapped and the syscall arguments read at the start +(`wrRead_of`); the platform's `MIN_BUDGET` [P]. -/ +theorem JAtZB.writeRegsF (hAT : P0.ArchTotal arI) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {s1 : S} {σ1 : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s1 a σ1) + {p ct buf : U64} (hpT : p ∈ Z.T) (hpc : p ≠ ct) (hcur : σ1.curThread = ct.bv) (hctT : ct ∈ Z.T) + {x0 : U64} (hxS : IsSlot Z.th x0) (hcap : capAt (L.mem s1) x0 = ok (.Thread p)) + (hns : ∀ u, σ1.schedulerAction ≠ .switchThread u) + {sp : Fl4Spec.Word} (hbm : buf ≠ 0#u64 → BufMap Z.B buf sp) + (resume : Bool) (n : U64) {V : List Fl4Spec.Word} (hVn : V.length = n.val) + (hargs : ∀ (j : Nat) (i2 : U64), i2.val = j + 2 → j < n.val → + ∃ w, msg.syscall_arg st s1 ct buf i2 = ok w ∧ w.bv = V[j]!) : + tcb.write_registers st arI s1 a p resume n buf ⦃ r => r.2 = a ∧ ∃ σ', + specWr p.bv resume V σ1 = ({((), σ')}, False) ∧ + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s1 σ1 r.1 σ' ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := hI.2.2.1 + have hta := hP0.e.tcb p hpT + obtain ⟨x, hk, -⟩ := hI.1.tcb p hpT + unfold tcb.write_registers + refine WP.spec_bind (cur_thread_bv hS) ?_ + rintro ct' hct' + obtain rfl : ct = ct' := (P1.eq_of_bv (hct'.trans hcur)).symm + have hf : tcb.N_FRAME.val = 18 := by unfold tcb.N_FRAME; rfl + have hg : tcb.N_GP.val = 2 := by unfold tcb.N_GP; rfl + obtain ⟨i, hie, hiv⟩ := spec_exists (U64.add_spec (x := tcb.N_FRAME) (y := tcb.N_GP) (by rw [hf, hg]; scalar_tac)) + rw [hie, bind_ok] + have hi20 : i.val = 20 := by have := hiv; simp only [hf, hg] at this ⊢; omega + obtain ⟨n', hn'e, hn'v⟩ : ∃ n' : U64, (if n > i then ok i else ok n : Result U64) = ok n' ∧ + n'.val = min n.val 20 := by + by_cases h : n > i + · exact ⟨i, if_pos h, by have := (P1.gt_iff_val).1 h; omega⟩ + · exact ⟨n, if_neg h, by have := (P1.gt_iff_val).not.1 h; omega⟩ + rw [hn'e, bind_ok] + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ1.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hEH := hH.epHyps hP0 + have hSR := IpcRel.toSRN hI.1 hI.2 + have hI0 := rgInv_init (L := L) (T := Z.T) (E := Z.E) (R := Z.R) (B := Z.B) hI.1.toE hSR.1 hk + have hn20 : n'.val ≤ 20 := by omega + have hVl : n'.val ≤ V.length := by omega + have hRd : WrRead (L := L) Z.T Z.E Z.R Z.B + (SRS st Z.T (SRT st Z.th σ1.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s1 σ1 p x ct buf n' V := + wrRead_of hP0.e hI.1.toE hpT hk hctT hbm hn20 fun j i2 hi2 hj => hargs j i2 hi2 (by omega) + refine WP.spec_bind (wrB_loop0 hEH hP0.e J hpT hk hn20 hVl hRd hI0) ?_ + rintro s2 ⟨hI2, ⟨V2, J2⟩, hw2⟩ + refine WP.spec_bind (wrB_loop1 (Z := Z.mv Z.G V2) hEH hP0.e hpT hn20 hVl hRd hI2 J2 hw2) ?_ + rintro s3 ⟨-, ⟨V3, J3⟩, hw3⟩ + rw [sanRegs_take x wrRegs V n'.val (by rw [wrRegs_length]; omega)] at J3 + set x2 := sanRegs x wrRegs V with hx2 + have hX3 := J3.xtop.irelK J3.ks J3.buf + have hk2 : (updK σ1 p.bv (.tcb x2)).kheap p.bv = some (.tcb x2) := by simp [updK] + obtain ⟨x2', hk2', hTR2⟩ := hX3.1.tcb p hpT + rw [hk2] at hk2'; cases hk2' + -- the restart PC + have hF : layout.reg.FAULT_IP = regIdx .faultIP := by unfold layout.reg.FAULT_IP; rfl + have hNx : layout.reg.NEXT_IP = regIdx .nextIP := by unfold layout.reg.NEXT_IP; rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s3 p _) ?_ + rintro pc hpc' + have hpcv : pc.bv = x2.tcbArch.tcbContext.userRegs .faultIP := by rw [hpc', hF]; exact hTR2.regs .faultIP + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s3 p _ pc) ?_ + rintro s4 rfl + rw [hNx] + obtain ⟨V4, J4, -⟩ := J3.regWrJ hpT hk2 .nextIP pc (reg_wr_irelN J3.ip hX3 hpT hk2 .nextIP pc) + rw [hpcv] at J4 + set x3 := setReg x2 .nextIP (x2.tcbArch.tcbContext.userRegs .faultIP) with hx3 + have e3 : updK (updK σ1 p.bv (.tcb x2)) p.bv (.tcb x3) = updK σ1 p.bv (.tcb x3) := by + simp only [updK, Function.update_idem] + rw [e3] at J4 + have hk3 : (updK σ1 p.bv (.tcb x3)).kheap p.bv = some (.tcb x3) := by simp [updK] + have hX4 := J4.xtop.irelK J4.ks J4.buf + -- `post_modify_registers`: the target is not the caller + have hpm : tcb.post_modify_registers st (L.wr s3 (regA p (regIdx .nextIP)) pc) p ⦃ s5 => + s5 = L.wr (L.wr s3 (regA p (regIdx .nextIP)) pc) (regA p (regIdx .errorRegister)) 0#u64 ⦄ := by + unfold tcb.post_modify_registers + refine WP.spec_bind (cur_thread_bv hX4.2.2.1) ?_ + rintro ct3 hc3 + obtain rfl : ct = ct3 := (P1.eq_of_bv (hc3.trans hcur)).symm + rw [tcb_ne_eq, bind_ok] + simp only [ne_eq, hpc, not_false_eq_true, decide_true, ↓reduceIte] + have hE : layout.reg.ERROR = regIdx .errorRegister := by unfold layout.reg.ERROR; rfl + rw [hE]; exact Endp.obj.Tcb.set_reg.spec _ p _ 0#u64 + refine WP.spec_bind hpm ?_ + rintro s5 rfl + obtain ⟨V5, J5, -⟩ := J4.regWrJ hpT hk3 .errorRegister 0#u64 (reg_wr_irelN J4.ip hX4 hpT hk3 .errorRegister 0#u64) + set x4 := setReg x3 .errorRegister (0#u64).bv with hx4 + have e4 : updK (updK σ1 p.bv (.tcb x3)) p.bv (.tcb x4) = updK σ1 p.bv (.tcb x4) := by + simp only [updK, Function.update_idem] + rw [e4] at J5 + have hpm4 : Fl4Spec.archPostModifyRegisters (Z := Unit) ct.bv p.bv (updK σ1 p.bv (.tcb x3)) = + ({((), updK σ1 p.bv (.tcb x4))}, False) := by + unfold Fl4Spec.archPostModifyRegisters Fl4Spec.NonDet.when + rw [if_pos (fun e => hpc (P1.eq_of_bv e)), asUser_setRegister_eq _ _ hk3, ← e4] + rfl + -- the restart's premises at the state after the register words + have hX5 := J5.xtop.irelK J5.ks J5.buf + have hw5 := wr_regW hta .errorRegister 0#u64 (wr_regW hta .nextIP pc hw3) + have hIF := IRel.iframe_of hI hw5 fun _ ⟨r, hb⟩ => by + have := regIdx_lt r + have hr := regA_val hta (regIdx_lt r) + exact Or.inr (Or.inr (Or.inl ⟨p, hpT, by omega, by omega⟩)) + have hcap5 : capAt (L.mem (L.wr (L.wr s3 (regA p (regIdx .nextIP)) pc) (regA p (regIdx .errorRegister)) 0#u64)) + x0 = ok (.Thread p) := by + rw [capAt_capV, hIF.1.capV hxS, ← capAt_capV]; exact hcap + -- the restart + have hrs : (if resume then sched.restart st arI (L.wr (L.wr s3 (regA p (regIdx .nextIP)) pc) + (regA p (regIdx .errorRegister)) 0#u64) a p else ok (L.wr (L.wr s3 (regA p (regIdx .nextIP)) pc) + (regA p (regIdx .errorRegister)) 0#u64, a)) ⦃ r => r.2 = a ∧ ∃ σ6, + Fl4Spec.NonDet.when resume (Fl4Spec.restart (Z := Unit) p.bv) (updK σ1 p.bv (.tcb x4)) = + ({((), σ6)}, False) ∧ σ6.curThread = ct.bv ∧ + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.1 r.2 σ6 ∧ + NatFoot (L := L) Z (L.wr (L.wr s3 (regA p (regIdx .nextIP)) pc) (regA p (regIdx .errorRegister)) 0#u64) + (updK σ1 p.bv (.tcb x4)) r.1 σ6 ⦄ := by + cases resume + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨trivial, _, rfl, hcur, V5, Ac, Lst, D, J5, NatFoot.ofK rfl (NatK.of_kheap rfl)⟩ + · simp only [↓reduceIte] + refine WP.spec_mono (WP.spec_and ((J5.undead (Z := Z.mv Z.G V5) hxS hcap5 hpT).restartTcbN hAT hmb hpT + (fun h => h.2 rfl) hns) + (restart_tcb_irelS (ar := arI) hP0 (removeTcbHypP_ok hP0) _ a _ hX5 hpT J5.sv J5.obj J5.inv + (fun x' hk' hfr => removeTcbOK_fault hP0 hX5 hpT hk' hfr J5.removeTcbOK) + (Sched.restartSpecOK_of J5.inv J5.sv.scSpor) hmb)) ?_ + rintro r ⟨⟨h1, σ6, h2, V', Ac', Lst', D', J6, F6⟩, -, σ6', h2', -, hc6, -⟩ + have e6 : σ6' = σ6 := by + rw [h2] at h2'; exact ((Prod.mk.inj (Set.singleton_injective (congrArg Prod.fst h2'))).2).symm + subst e6 + exact ⟨h1, σ6', by simpa [Fl4Spec.NonDet.when] using h2, hc6.trans hcur, V', Ac', Lst', D', J6, F6.cast⟩ + refine WP.spec_bind hrs ?_ + rintro ⟨s6, a1⟩ ⟨rfl, σ6, hrs6, hct6, V6, Ac6, Lst6, D6, J6, F6⟩ + have Freg := J.rcvRegN hpT hw5 (natK_tcbW (σ' := updK σ1 p.bv (.tcb x4)) hk rfl (fun h => by + have e : x4.tcbState = x.tcbState := (sanRegs_fields x wrRegs V).1 + rwa [e] at h)) + refine WP.spec_bind (cur_thread_bv (J6.xtop.irelK J6.ks J6.buf).2.2.1) ?_ + rintro ct5 hc5 + obtain rfl : ct = ct5 := (P1.eq_of_bv (hc5.trans hct6)).symm + unfold obj.Tcb.Insts.CoreCmpPartialEqTcb.eq + simp only [bind_ok, hpc, decide_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨trivial, σ6, ?_, V6, Ac6, Lst6, D6, J6, J.natTrans Freg F6⟩ + unfold specWr + rw [nd_gets_bind, hcur, nd_bind_one (show Fl4Spec.archGetSanitiseRegisterInfo (Z := Unit) p.bv σ1 = + ({(false, σ1)}, False) from rfl), nd_bind_one (wr_regs_eq hk V), nd_bind_one hpm4, nd_bind_one hrs6] + unfold Fl4Spec.NonDet.when + rw [if_neg (fun e => hpc (P1.eq_of_bv e))] + rfl + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBThrWriteTop.lean b/proofs/Fl4Proofs/P3/JAtZBThrWriteTop.lean new file mode 100644 index 000000000..21d11255a --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBThrWriteTop.lean @@ -0,0 +1,176 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route F): **WriteRegisters**. + +`JAtZB` siblings of XTopJointTcb's native label tops, concluding `ThrPost` (with the native footprint). Generated by +`handoff/tools/gen_thrtopA.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrWrite +import Fl4Proofs.P3.XTopJointTcb +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The WriteRegisters label at `JAt`** (as `tcb_write_regsX`): TcbCtl's +decode, the caller's restart (`JAtZ.restartJ`; the slots kept, so the +target's cap still in the invoked slot; `CurInv` by `restart_mid`), then +XTopJointWriteRegs' `JAtZ.writeRegsJ` (the register loops, the restart PC, +`post_modify_registers`, the optional `JAtZ.restartTcbJ`). Named, as +IpcWriteRegsJ's `writeRegsXHypJ`: the caller's buffer mapped (`hbuf`) and +the syscall arguments after the caller's restart (`hargs1`) [W, front end]; +the platform's `MIN_BUDGET` [P]. -/ +theorem JAtZB.tcbWriteRegsF (hAT : P0.ArchTotal arI) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) + (p : U64) (hpT : p ∈ Z.T) (x0 : U64) (hxS : IsSlot Z.th x0) (hcap : capAt (L.mem s) x0 = ok (.Thread p)) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbWriteRegisters) + (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) + (hbuf : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (hargs1 : ∀ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s1 ct buf i = ok x) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (slot : obj.Slot) (extra : Aeneas.Std.Array obj.Slot 3#usize) (blk don : Bool) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buffer : Option Fl4Spec.ObjRef) : + tcb.decode st arI s ar fl4_abi.label.TCBWriteRegisters p ⟨length, buf, call, slot, extra⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer) res ⦄ := by + have hX := J.xtop + obtain ⟨hcur, -⟩ := J.curEq hct + have hctT : ct ∈ Z.T := J.thT ct (hX.curTcb hcur).1 + have hdec0 : specDec false slbl (args.map UScalar.bv) ci sl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeWriteRegisters (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv)) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) sl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_write_eq arI s ar p _ hstall, hdec0] + unfold tcb.decode_write_registers + by_cases h1 : length < 2#u64 + · simp only [h1, ↓reduceIte, error.truncated, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + have : args.length < 2 := by scalar_tac + match args, this with + | [], _ => rfl + | [_], _ => rfl + simp only [h1, ↓reduceIte] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, hlen with + | y0 :: y1 :: rest, _ => exact ⟨_, _, _, rfl⟩ + | [], h => exfalso; simp only [List.length_nil] at h; scalar_tac + | [_], h => exfalso; simp only [List.length_singleton] at h; scalar_tac + have ha0 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have ha1 : tcb.arg st s ⟨length, buf, call, slot, extra⟩ 1#u64 = ok y1 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 1#u64 y1 (by simp) + obtain ⟨i, hi, hiv⟩ := spec_exists (U64.sub_spec (x := length) (y := 2#u64) (by scalar_tac)) + have hrl : rest.length = length.val - 2 := by simp at hlen; scalar_tac + have hiv' : i.val = length.val - 2 := by rw [hiv.1]; rfl + rw [ha0, ha1]; simp only [bind_ok, hi] + have hsp : (Fl4Spec.decodeWriteRegisters (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv)) = + (Fl4Spec.NonDetE.whenE ((rest.map UScalar.bv).length < Fl4Spec.unat y1.bv) + (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage) >>= fun _ => do + let self ← Fl4Spec.NonDetE.liftE (Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread) + Fl4Spec.NonDetE.whenE (p.bv = self) (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation) + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.writeRegisters p.bv (Fl4Spec.testBit y0.bv 0) + ((rest.map UScalar.bv).take (Fl4Spec.unat y1.bv)) Fl4Spec.ArchDefaultExtraRegisters)) := by + simp only [Fl4Spec.decodeWriteRegisters, List.map_cons, retOk_bind] + rw [hsp] + have hun : Fl4Spec.unat y1.bv = y1.val := by simp [Fl4Spec.unat] + by_cases hw : i < y1 + · simp only [hw, ↓reduceIte, error.truncated, bind_ok] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, .truncatedMessage, ?_, Or.inl trivial⟩) + apply run_liftME_err + rw [whenE_pos (by rw [hun, List.length_map, hrl]; have h' : i.val < y1.val := hw; omega)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + simp only [hw, ↓reduceIte, hct, bind_ok, tcb_eq_ct] + have hy1 : y1.val ≤ rest.length := by have h' : ¬ i.val < y1.val := hw; omega + have hwh : (Fl4Spec.NonDetE.whenE ((rest.map UScalar.bv).length < Fl4Spec.unat y1.bv) + (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage) : Fl4Spec.SEMonad Unit Unit).run σ = + ({(.ok (), σ)}, False) := by + rw [whenE_neg (by rw [hun, List.length_map]; omega)]; rfl + by_cases hpc : p = ct + · subst hpc + simp only [decide_true, ↓reduceIte, error.illegal, bind_ok] + refine (WP.spec_ok _).2 ?_ + apply Or.inl + refine ⟨_, rfl, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩ + apply run_liftME_err + rw [run_bind_ok hwh, run_bind_ok (run_liftE (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => + s.curThread) rfl), whenE_pos (by show p.bv = σ.curThread; rw [hcur])] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : + Fl4Spec.SEMonad Unit Unit)) rfl _ + simp only [decide_eq_false hpc, Bool.false_eq_true, ↓reduceIte] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.NonDetE.whenE ((rest.map UScalar.bv).length < Fl4Spec.unat y1.bv) + (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage) >>= fun _ => do + let self ← Fl4Spec.NonDetE.liftE (Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread) + Fl4Spec.NonDetE.whenE (p.bv = self) (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation) + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.writeRegisters p.bv (Fl4Spec.testBit y0.bv 0) + ((rest.map UScalar.bv).take (Fl4Spec.unat y1.bv)) Fl4Spec.ArchDefaultExtraRegisters))).run σ = + ({(.ok (.invokeTCB (.writeRegisters p.bv (Fl4Spec.testBit y0.bv 0) + ((((y0 :: y1 :: rest).drop 2).map UScalar.bv).take y1.val) Fl4Spec.ArchDefaultExtraRegisters)), σ)}, + False) := by + apply run_liftME_ok + rw [run_bind_ok hwh, run_bind_ok (run_liftE (m := Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => + s.curThread) rfl), whenE_neg (by show ¬ p.bv = σ.curThread; rw [hcur, bv_eq_iff']; exact hpc), + retOk_bind, hun] + rfl + -- the caller's restart + obtain ⟨s1, σ1, x, hrc1, hst, hk, -, -, hns, -, hct1, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.restartCallerF hni hct + obtain ⟨-, x', hk', hrun⟩ := J.curRunnable hni hct + rw [hk] at hk'; cases hk' + have hSS : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := XTopAt.restart J.xtop hcur hk hrun + rw [hs1, WP.spec_ok] at h; exact h.2.1 + have hcap1 : capAt (L.mem s1) x0 = ok (.Thread p) := by + rw [capAt_capV, hSS.capV hxS, ← capAt_capV]; exact hcap + have hcur1 : σ1.curThread = ct.bv := hct1.trans hcur + rw [hrc1]; simp only [bind_ok, lift] + -- the arguments after the restart + set V := (((y0 :: y1 :: rest).drop 2).map UScalar.bv).take y1.val with hV + have hVn : V.length = y1.val := by simp [hV]; omega + obtain ⟨sp, hbm⟩ : ∃ sp, buf ≠ 0#u64 → BufMap Z.B buf sp := by + by_cases hb : buf = 0#u64 + · exact ⟨0, fun h => absurd hb h⟩ + · obtain ⟨sp, h⟩ := hbuf hb; exact ⟨sp, fun _ => h⟩ + have hargsV : ∀ (j : Nat) (i2 : U64), i2.val = j + 2 → j < y1.val → + ∃ w, msg.syscall_arg st s1 ct buf i2 = ok w ∧ w.bv = V[j]! := by + intro j i2 hi2 hj + have hjl : j + 2 < (y0 :: y1 :: rest).length := by simp; omega + refine ⟨(y0 :: y1 :: rest)[j + 2], hargs1 s1 hs1 i2 _ (by rw [hi2]; exact List.getElem?_eq_getElem hjl), ?_⟩ + have hjV : j < V.length := by omega + rw [getElem!_pos V j hjV] + simp only [hV, List.getElem_take, List.getElem_map, List.getElem_drop] + congr 2; omega + refine WP.spec_bind (J1.writeRegsF hAT (hmb ar) hpT hpc hcur1 hctT hxS hcap1 (fun u h => hns u h) hbm + ((y0 &&& 1#u64) != 0#u64) y1 hVn hargsV) ?_ + rintro ⟨s2, a2⟩ ⟨-, σ', hsp, V', Ac', Lst', D', J2, F2⟩ + refine (WP.spec_ok _).2 ?_ + have hz : ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.fl4PerformInvocation (Z := Unit) blk call don (.invokeTCB (.writeRegisters p.bv + (Fl4Spec.testBit y0.bv 0) V Fl4Spec.ArchDefaultExtraRegisters))).run σ1).1 := by + show _ ∈ ((Fl4Spec.invokeTcb (Z := Unit) _).run σ1).1 + rw [invokeTcb_wr, ← and1_ne0] + exact mem_liftE (x := ([], σ')) (mem_bind_nd (by rw [hsp]; exact Set.mem_singleton _) (Set.mem_singleton _)) + exact Or.inr ⟨_, DecOut.of_eq hdec, trivial, rfl, σ1, hst, _, hz, V', Ac', Lst', D', J2, J.natTrans F1 F2.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBTmoRecv.lean b/proofs/Fl4Proofs/P3/JAtZBTmoRecv.lean new file mode 100644 index 000000000..e09b62e95 --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBTmoRecv.lean @@ -0,0 +1,1769 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm, Timeout branch with receivers waiting): **ext-free twins of the timeout-fault send to a +waiting receiver, over `JAtZB`, with `NatFoot`**. + +`JAtZB.tmoBaseJV`: XTopJointFaultTmo's `JAtZ.tmoBaseJ` (the sender's timeout fault words and its SC's consumed time) over `JAtZB` +(`JAtZB.fin_frameF`, `JAtZB.updConsumedN`), with the native footprint `NatFoot` from the state before. Generated by +`handoff/tools/gen_tmorecv.py`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.StPostTmo +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.JAtZBUpdConsumed +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **What the timeout-fault send to a waiting receiver leaves** (at `JAtZB`, with the native footprint from the start). -/ +def TmoFootPost (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) (s' : S) (a' : A) + (σ' : Fl4Spec.State Unit) : Prop := + ∃ (V : XVs) (Lst' : U64 → List U64), JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst' D s' a' σ' ∧ + NatFoot (L := L) Z s σ s' σ' + +set_option maxHeartbeats 16000000 in +/-- **The base `J` state of the timeout-fault send** (module docstring): +from `JAtZ`, the thread `t` (not blocked on send) given the timeout fault +(`set_fault`'s four words, `JAtZ.fin_frameF` as XTopJointFaultRecv's +`JAtZ.setFaultCapJ`, P1 by `blk_faultW`) and its SC's consumed time cleared +(`JAtZ.updConsumedJ`, the state by `update_consumed_irel`). -/ +theorem JAtZB.tmoBaseJV (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hns : ∀ e d, x.tcbState ≠ .blockedOnSend e d) (b : U64) {sc : U64} (hsc : sc ∈ Z.C) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (tmoS st (fltS st s t b) sc) a + (Ipc.tmoSt (updK σ t.bv (.tcb { x with tcbFault := some (.timeout b.bv) })) sc y n) ∧ + NatFoot (L := L) Z s σ (tmoS st (fltS st s t b) sc) (Ipc.tmoSt (updK σ t.bv (.tcb { x with tcbFault := some (.timeout b.bv) })) sc y n) := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hta := hP0.e.tcb t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hP0.e.tcb + obtain ⟨s1, -, ⟨⟨-, hI1⟩, hs1⟩, hfoot1⟩ := spec_exists (WP.spec_and (WP.spec_and + (set_fault_timeout_top hP0 hI ht hk b) (set_fault_timeout_eq s t b)) + (P1.faults.set_fault.kw (Cs := Cs) (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := ∅) s t (error.Fault.Timeout b) hTH htH)) + subst hs1 + have hblk1 := blk_faultW (v := 0#u64) (K := 3400#u64) (blk_faultW (v := 0#u64) (K := 3392#u64) + (blk_faultW (v := b) (K := 3384#u64) (blk_faultW (v := fl4_abi.consts.fault.Timeout) (K := 3376#u64) J.blk htH + (by simp) (by simp) (by simp)) htH (by simp) (by simp) (by simp)) htH (by simp) (by simp) (by simp)) + htH (by simp) (by simp) (by simp) + have hwF : P1.WritesIn st s (fltS st s t b) (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := by + have w := (((writesIn_wr (st := st) (s := s) (v := fl4_abi.consts.fault.Timeout) (a_fault hta).1).trans + (writesIn_wr (v := b) (a_fw0 hta).1)).trans (writesIn_wr (v := 0#u64) (a_fw1 hta).1)).trans + (writesIn_wr (v := 0#u64) (a_fw2 hta).1) + refine w.mono fun w0 hw0 => ?_ + have k : ∃ K : Nat, 256 ≤ K ∧ K < 4096 ∧ K ≠ 3440 ∧ K ≠ 3472 ∧ ((K ≠ 3512 ∧ K ≠ 3528) ∧ K ≠ 3664 ∧ K ≠ 3456) ∧ w0.val = t.val + K := by + rcases hw0 with ((h | h) | h) | h <;> subst h + · exact ⟨3376, by omega, by omega, by omega, by omega, by omega, val_fault hta⟩ + · exact ⟨3384, by omega, by omega, by omega, by omega, by omega, val_fw0 hta⟩ + · exact ⟨3392, by omega, by omega, by omega, by omega, by omega, val_fw1 hta⟩ + · exact ⟨3400, by omega, by omega, by omega, by omega, by omega, val_fw2 hta⟩ + obtain ⟨K, k1, k2, k3, k4, k5, k6⟩ := k + exact Or.inr ⟨t, ht, K, k1, k2, k3, k4, k5, k6⟩ + set y1 : Fl4Spec.Tcb := { x with tcbFault := some (.timeout b.bv) } with hy1 + have hA : QKY σ (updK σ t.bv (.tcb y1)) := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun p => ?_⟩ + by_cases hp : p = t.bv + · subst hp; exact Or.inr (Or.inl ⟨x, y1, hk, by simp [updK], rfl⟩) + · exact Or.inl (by simp [updK, hp]) + have J1 := J.fin_frameF hI1 hwF hA (YK.YKS.updTcb (y := y1) hk rfl).ykeep + (J.inv.updSame (y := y1) hk rfl rfl (Or.inr hns)) hblk1 hfoot1 + have hst : sc.bv ≠ t.bv := fun e => by rw [e, hk] at hky; cases hky + have hky1 : (updK σ t.bv (.tcb y1)).kheap sc.bv = some (.schedContext y n) := by simp [updK, hst, hky] + have hI1' := J1.xtop.irelK J1.ks J1.buf + obtain ⟨r, -, ⟨σ2, c2, he2, V2, J2, F2⟩, hs2, -, he2', -⟩ := spec_exists (WP.spec_and + (J1.updConsumedN hAT hmax htu hsc ht) (update_consumed_irel (ar := arI) J1.ip _ a _ hI1' hsc hky1 hmax htu)) + have eσ : σ2 = Ipc.tmoSt (updK σ t.bv (.tcb y1)) sc y n := by + have e := (Prod.mk.inj (he2.symm.trans he2')).1 + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e) + rw [eσ, hs2] at J2 F2 + have hS := J.xtop.fin.1.1.1 + have F1 : NatFoot (L := L) Z s σ (fltS st s t b) (updK σ t.bv (.tcb y1)) := + NatFoot.ofW J.xtop hwF (fun _ hb => Ipc.FinW.oobj hS hb) (fun _ hb => Ipc.FinW.nv hS hb) + (fun _ hb => J.finW_nh hb) (natK_qky hA) + obtain ⟨Q, R0, hc0, hs0⟩ := hS.lists + exact ⟨V2, J2, NatFoot.trans (fun q hq => by obtain ⟨y', n', hq', -⟩ := hs0.scs q hq; exact ⟨y', n', hq'⟩) F1 F2⟩ + +set_option maxHeartbeats 64000000 in +/-- (stable ghosts, `JStab`) **The timeout-fault `send_ipc` of a thread other than the current one to +a waiting receiver, the sender left `Inactive`** (module docstring): at the +send (`s7`, `σf`: `t` made `Running`/`Restart` and given the timeout fault, +from the `J` state `s`, `σ` where it is `Inactive`), FL4's dequeue, transfer +(`xferTmoStepJ`), reply unlink, `t` `Inactive` and the receiver `Running` on +`IRel` as XTopJointFaultRecv's `JAtZ.sendFaultIJ` (P1's queue bits of `t` +from its `CanBlock` at the send); `JAtZ` by `JAtZ.wake_postD` from the base +`JAtZ.tmoBaseJ` (`t` `Inactive` with the fault, its SC's consumed time +cleared), whose frame to the end is the receiver's wake (`CFr`, read on the +shadows `rhoT` of the states between: `t` and its SC as at the base). -/ +theorem JAtZB.sendTmoRecvIJFB (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + (hcr : ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ Fl4Spec.runnable xc.tcbState = true) + {t : U64} (ht : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (b : U64) {ts : Fl4Spec.ThreadState} (hrun : Fl4Spec.runnable ts = true) {s7 : S} + (hI7 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s7 + (updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) }))) + (hblk7 : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s7) + (hW7 : P1.WritesIn st s s7 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hfoot7 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s7) + (hcb7 : P1.CanBlock st (P1.Heap.tcbs H) Ac s7 t) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hke : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (badge : U64) (g gr : Bool) + (hno : (g = false ∧ gr = false) ∨ + ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep = none) : + endpoint.send_ipc st arI s7 a true false badge g gr false t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) true false badge.bv g gr false t.bv e.bv + (updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) }))).1, + TmoFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r.1 r.2 z.2 ⦄ := by + set xt : Fl4Spec.Tcb := { x with tcbState := ts, tcbFault := some (.timeout b.bv) } with hxt + set σf := updK σ t.bv (.tcb xt) with hσf + set xT : Fl4Spec.Tcb := { x with tcbFault := some (.timeout b.bv) } with hxT + set yS : Fl4Spec.SchedContext := { y with scConsumed := (0#u64 : U64).bv } with hyS + have hns : ∀ e d, x.tcbState ≠ .blockedOnSend e d := by rw [hrs]; intro _ _ h; cases h + have hst : t.bv ≠ sc.bv := fun e0 => by rw [e0, hky] at hk; cases hk + -- the base `J` state + obtain ⟨V0, J0, Fb⟩ := J.tmoBaseJV hAT hmax htu ht hk hns b hsc hky + set s0 := tmoS st (fltS st s t b) sc with hs0 + set σ0 := Ipc.tmoSt (updK σ t.bv (.tcb xT)) sc y n with hσ0 + have hσ0r : σ0 = rhoT σ t.bv sc.bv xT yS n := rfl + have hfr : rhoT σf t.bv sc.bv xT yS n = σ0 := by rw [hσf, rhoT_updKt, hσ0r] + have hk0 : ∀ q, q ≠ t.bv → q ≠ sc.bv → σ0.kheap q = σ.kheap q := fun q h1 h2 => by + rw [hσ0r]; exact rhoT_kh h1 h2 + have hkf : ∀ q, q ≠ t.bv → σf.kheap q = σ.kheap q := fun q h1 => by simp [hσf, updK, h1] + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hI := hI7 + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hte0 : t.bv ≠ e.bv := kheap_ne hk hke (fun _ h1 h2 => by subst h1; cases h2) + have hse0 : sc.bv ≠ e.bv := kheap_ne hky hke (fun _ h1 h2 => by subst h1; cases h2) + have hkx := hk + have hk : σf.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q))) := by rw [hkf _ (Ne.symm hte0)]; exact hke + have hkt : σf.kheap t.bv = some (.tcb xt) := by simp [hσf, updK] + have hrn : Fl4Spec.runnable xt.tcbState = true := hrun + have hft : xt.tcbFault = some (.timeout b.bv) := rfl + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s7 (tStA d')).val = 3 := (hB d' (by simp)).1 + have hdb : L.mem s7 (tBlockA d') = e := (hB d' (by simp)).2 + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (hxd.st.symm.trans hd3) + have he' : e' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := hte0 + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdsc : d'.bv ≠ sc.bv := fun e0 => by + have h1 : σf.kheap sc.bv = some (.schedContext y n) := by rw [hkf _ (Ne.symm hst)]; exact hky + rw [e0, h1] at hkd; cases hkd + have hkdσ : σ.kheap d'.bv = some (.tcb xd) := by rw [← hkf _ (Ne.symm htd')]; exact hkd + have hkd0 : σ0.kheap d'.bv = some (.tcb xd) := by rw [hk0 _ (Ne.symm htd') hdsc]; exact hkdσ + have hke0 : σ0.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := by + rw [hk0 _ (Ne.symm hte0) (Ne.symm hse0)]; exact hke + have hdc : d'.bv ≠ σf.curThread := by + intro h + obtain ⟨xc, hkc, hrc⟩ := hcr + have h' : d'.bv = σ.curThread := h + rw [← h', hkdσ] at hkc; cases hkc; rw [hstd] at hrc; cases hrc + -- P1's facts at the send + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := hblk7 + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s7 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s7 (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s7 d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s7 e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s7 e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s7 e) ?_ + rintro _ rfl + have hhead : (L.epQ s7 e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s7 σf hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s7 he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σf e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s7 s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s7 s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s7 s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, ea1, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + have hky1 : σ1.kheap sc.bv = some (.schedContext y n) := by + simp [σ1, hse0, hσf, updK, Ne.symm hst, hky] + 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 _) + -- the shadow frame from the base + have eρ1 : rhoT σ1 t.bv sc.bv xT yS n = updK σ0 e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + rw [hσ1, rhoT_updK _ (Ne.symm hte) (Ne.symm hse0), hfr] + have Fr1 : CFr e.bv d'.bv σ0 (rhoT σ1 t.bv sc.bv xT yS n) := by + rw [eρ1] + exact CFr.upd hke0 (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ0 (rhoT σ1 t.bv sc.bv xT yS n) := by + rw [eρ1] + exact IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hke0 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ0 (rhoT σ1 t.bv sc.bv xT yS n) := by + rw [eρ1] + exact Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) + (fun _ _ h => by cases h) + have hOKσ := (J.xferH).1 + have hBCσ := (J.xferH).2.1 + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (ipcBufCapOK_updT (y := xt) hOKσ hkx rfl rfl) (EpStep.ep hk) + have hBC1 : Endp.BufCover Z.T Z.B σ1 := bufCover_epStep (bufCover_updT (y := xt) hBCσ hkx rfl rfl) (EpStep.ep hk) + have hsc1 : sc ∈ P1.Heap.scs H := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨_, (hJ.sc sc y n (J.c0 sc hsc) hky).2⟩ + -- the transfer + refine WP.spec_bind (xferTmoStepJ hP0 (hH.epHyps hP0) hmax s1 a σ1 htu hR1 hS1 hOK1 hBC1 ht hdT htd hkt1 hkd1 hft + hxsc hky1 e badge g hTP hdH h1 hB1 hX1 hS1' hSg1 hsc1) ?_ + rintro s2 ⟨x2, e2, hx2, hR2, hS2, hA2, w2, ea2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Ipc.tmoSt σ1 sc y n) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by + rw [hσ2] + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne htd', Function.update_of_ne hst] + exact hkt1 + have hksc2 : σ2.kheap sc.bv = some (.schedContext yS n) := by + rw [hσ2, updK_kheap, Function.update_of_ne (Ne.symm hdsc)] + show Function.update σ1.kheap sc.bv _ sc.bv = _ + rw [Function.update_self] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc2 : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + rw [hσ2] + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne (Ne.symm hde), Function.update_of_ne (Ne.symm hse0)] + exact hk1e + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + have hns' : sc.bv ≠ e''.bv := by intro h; rw [← h, hksc2] at hz; cases hz + rw [hσ2] at hz + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne (Ne.symm hne), + Function.update_of_ne (Ne.symm hns')] at hz + exact hq1 e'' he'' z hz + have hrw : L.mem s2 (tReplyA d') = L.mem s7 (tReplyA d') := by + rw [show tReplyA d' = core.num.U64.wrapping_add d' 3360#u64 from rfl, f2 d' hdH _ (by simp) (by simp) (by simp), + ← show tReplyA d' = core.num.U64.wrapping_add d' 3360#u64 from rfl, + hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + have hscw8 : SW Z.C σf (scW sc 8#u64) := sw_hdr hI.2.2.1 hsc (K := 8#u64) (by simp) (by simp) (by decide) + have hW2 : P1.WritesIn st s7 s2 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW1.trans1 (w2.mono fun b hb => by + rcases hb with ⟨eb, -⟩ | (⟨y0, hy, K, h1', h2', -, eb⟩ | hb) + · subst eb; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))) + · have hyT : y0 ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inr ⟨y0, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩) + rw [eb]; exact tfield_val (hP.tcb y0 hyT) (by omega) + · exact absurd hb (Set.notMem_empty _)) + have F2 : P1.WritesIn st s1 s2 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1) := + w2.mono fun b hb => by + rcases hb with ⟨-, c0, hc0, hin⟩ | (⟨y0, hy, K, h1', h2', h3', eb⟩ | hb) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨c0, hc0, hin⟩))) + · exact Or.inl ⟨y0, hy, K, by omega, by omega, by omega, by omega, eb⟩ + · exact absurd hb (Set.notMem_empty _) + have eρ2 : rhoT σ2 t.bv sc.bv xT yS n = updK (rhoT σ1 t.bv sc.bv xT yS n) d'.bv (.tcb x2) := by + rw [hσ2, rhoT_updK _ (Ne.symm htd') hdsc, rhoT_updKsc _ hst] + have hkd1ρ : (rhoT σ1 t.bv sc.bv xT yS n).kheap d'.bv = some (.tcb xd) := by + rw [rhoT_kh (Ne.symm htd') hdsc]; exact hkd1 + have Fr2 : CFr e.bv d'.bv σ0 (rhoT σ2 t.bv sc.bv xT yS n) := by + rw [eρ2] + exact Fr1.trans (CFr.upd hkd1ρ (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, + by rw [hx2], Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ0 (rhoT σ2 t.bv sc.bv xT yS n) := by + rw [eρ2]; exact IK1.trans (IK.updSelf hkd1ρ hxsc2) + have SK2 : Ipc.SKeep σ0 (rhoT σ2 t.bv sc.bv xT yS n) := by + rw [eρ2] + exact SK1.trans (Ipc.SKeep.updK (p := d'.bv) (o := .tcb x2) (fun y h => ⟨xd, hkd1ρ, by cases h; exact hxsc2.symm⟩) + (fun _ _ h => by cases h)) + have hks2 : L.ksb s2 = L.ksb s7 := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s7 (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] + refine WP.spec_bind (Pₘ := fun s3 => ∃ σ3 x3 X3, + Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) rep σ2 = ({((), σ3)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s3) σ3 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s3 σ3 ∧ + σ3.kheap t.bv = some (.tcb xt) ∧ σ3.kheap d'.bv = some (.tcb x3) ∧ RbIn x3.tcbState ∧ + tsCode x3.tcbState ≠ 6 ∧ x3.tcbSchedContext = xd.tcbSchedContext ∧ + σ3.kheap sc.bv = some (.schedContext yS n) ∧ + (∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue) ∧ + P1.WritesIn st s7 s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) ∧ + P1.WritesIn st s7 s3 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s7 b ∨ False) ∧ + CFr e.bv d'.bv σ0 (rhoT σ3 t.bv sc.bv xT yS n) ∧ IK d'.bv σ0 (rhoT σ3 t.bv sc.bv xT yS n) ∧ + Ipc.SKeep σ0 (rhoT σ3 t.bv sc.bv xT yS n) ∧ σ3.curThread = σ.curThread ∧ + σ3.schedulerAction = σ.schedulerAction ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X3 s3 Q0 R0 ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s3 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s3 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s3 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 ∧ P1.ExcT X3 d' ∧ + (d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s3 (P1.stA d'))) ∧ + (∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d')) ?_ ?_ + · cases hrp : rep with + | none => + rw [hrp] at hrep + have h00 := optRef_none hrep + simp only [h00, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hrA : L.mem s2 (P1.replyA d') = 0#u64 := by + show L.mem s2 (tReplyA d') = 0#u64; rw [hrw]; exact h00 + exact ⟨σ2, x2, _, rfl, hR2, hS2, hkt2, hkd2, Or.inl ⟨_, _, _, hst2'⟩, + by rw [hst2']; simp [tsCode], hxsc2, hksc2, hq2, hW2, footW_trans (footW_of F1) (F2.mono fun b hb => Or.inl hb), + Fr2, IK2, SK2, rfl, rfl, h2, hB2, hX2, hS2'', + hSg2, fun y hy => by simpa using hy, Or.inl (Or.inr rfl), hX2.noref0 hdta hrA⟩ + | some r => + rw [hrp] at hrep hrepR hst2' + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + have hne0 : (L.mem s7 (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte] + set w := L.mem s7 (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hws : w.bv ≠ sc.bv := fun e0 => by + have h1 : σf.kheap sc.bv = some (.schedContext y n) := by rw [hkf _ (Ne.symm hst)]; exact hky + rw [e0, h1] at hkr; cases hkr + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + rw [hσ2] + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne hwd, Function.update_of_ne hws] + simp [σ1, hwe, hkr] + have hkrσ : σ.kheap w.bv = some (.reply xr) := by rw [← hkf _ hwt]; exact hkr + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkdσ e.bv w.bv dd xr (by rw [hstd, hrp]) hkrσ + have hX2' := IpcRel.ofSRN hR2 hS2 + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_mono (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2 hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + have hkd3a : (Notif.updK σ2 w.bv (.reply { xr with replyTcb := none })).kheap d'.bv = some (.tcb (x2)) := by + simp [Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK (Notif.updK σ2 w.bv (.reply { xr with replyTcb := none })) d'.bv + (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK (Notif.updK σ2 w.bv (.reply { xr with replyTcb := none })) d'.bv (.tcb x3)) + d'.bv = Notif.updK (Notif.updK σ2 w.bv (.reply { xr with replyTcb := none })) d'.bv (.tcb x3) := + stsAct_ne hct3a + have hW3' : P1.WritesIn st s7 s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW2.trans1 (hW3.mono fun b hb => cnW_back hks2 (SzKeepC.of (by + intro p y0 m hp + by_cases hps : p = sc.bv + · subst hps; rw [hksc2] at hp; cases hp; exact ⟨y, by rw [hkf _ (Ne.symm hst)]; exact hky⟩ + · refine ⟨y0, ?_⟩ + rw [hσ2] at hp + have hpd : p ≠ d'.bv := fun e0 => by rw [e0] at hp; simp at hp + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne hpd, Function.update_of_ne hps] at hp + have hpe : p ≠ e.bv := fun e0 => by rw [e0] at hp; simp [σ1] at hp + simpa [σ1, hpe] using hp)) hb) + -- the shadows + have hkr2ρ : (rhoT σ2 t.bv sc.bv xT yS n).kheap w.bv = some (.reply xr) := by + rw [rhoT_kh hwt hws]; exact hkr2 + have hkd3aρ : (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv (.reply { xr with replyTcb := none })).kheap d'.bv = + some (.tcb x2) := by + simp only [updK_kheap, Function.update_of_ne (Ne.symm hwd)] + rw [rhoT_kh (Ne.symm htd') hdsc]; exact hkd2 + have eρ3 : rhoT (Notif.stsAct (Notif.updK (Notif.updK σ2 w.bv (.reply { xr with replyTcb := none })) d'.bv + (.tcb x3)) d'.bv) t.bv sc.bv xT yS n = Notif.stsAct (Notif.updK (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv + (.reply { xr with replyTcb := none })) d'.bv (.tcb x3)) d'.bv := by + rw [e3s, stsAct_ne (show d'.bv ≠ (Notif.updK (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv + (.reply { xr with replyTcb := none })) d'.bv (.tcb x3)).curThread from hdc), + rhoT_updK _ (Ne.symm htd') hdsc, rhoT_updK _ hwt hws] + refine ⟨_, x3, _, e3, hR3, hS3, ?_, ?_, Or.inr rfl, by simp [x3, tsCode], by simp [x3, hxsc2], ?_, ?_, hW3', + footW_trans (footW_trans (footW_of F1) (F2.mono fun b hb => Or.inl hb)) (footW_of fu3), ?_, ?_, ?_, ?_, ?_, h3, + hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1), + hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr, hS2''.keepT hX2 k3, + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩, + fun y hy => ?_, Or.inr (by rw [e3']; exact P1.not_blocked_inactive), + (hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr).noref hdta (by rw [e3']; exact P1.inactive_ne.1) + (by rw [e3']; exact P1.inactive_ne.2)⟩ + · rw [stsAct_kheap]; simp [htd', hwt.symm, hkt2] + · rw [stsAct_kheap]; simp + · rw [stsAct_kheap]; simp [Ne.symm hdsc, hws.symm, hksc2] + · intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [hwd] at hz + exact hq2 e'' he'' z (by simpa [Ne.symm hne, Ne.symm hne'] using hz) + · rw [eρ3] + exact (Fr2.trans (CFr.upd hkr2ρ (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3aρ (Or.inl ⟨_, _, _, hst2'⟩)) + · rw [eρ3] + have IK3a : IK d'.bv σ0 (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv (.reply { xr with replyTcb := none })) := + IK2.trans (IK.updOther (o' := .reply { xr with replyTcb := none }) hkr2ρ (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + exact (IK3a.trans (IK.updSelf (y := x3) hkd3aρ rfl)).trans (IK.sts _ _ _) + · rw [eρ3] + exact ((SK2.trans (Ipc.SKeep.updK (p := w.bv) (o := .reply { xr with replyTcb := none }) (fun y h => by cases h) + (fun _ _ h => by cases h))).trans (Ipc.SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3aρ, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (Ipc.SKeep.stsAct _ _) + · rw [e3s]; rfl + · rw [e3s]; rfl + · obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2 + rintro s3 ⟨σ3, x3, X3, hU, hR3, hS3, hkt3, hkd3, hrb3, hns3, hsc3, hksc3, hq3, hW3, F3, Fr3, IK3, SK3, hct3, hact3, + h3, hB3, hX3, hS3', hSg3, hXc3, hx3, hnr3⟩ + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) = 5#u64 := by + have hfr := hxt3.faultR + unfold FaultRel at hfr; rw [hft] at hfr; exact hfr.1 + have hks3 : L.ksb s3 = L.ksb s7 := hW3.1 + -- the sender's `Inactive` + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := (IpcRel.ofSRN hR3 hS3).2.2.1 + have hna3 : L.actionW s3 ≠ t := actionW_ne_of hSR3 ht (by + rw [hact3]; intro e0 + obtain ⟨x', -, -, -, h1', -, h3', -⟩ := schedulable_inv (J.act.1 _ e0) + rw [hkx] at h1'; cases h1'; rw [hrs] at h3'; cases h3') + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + have hstsT := WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 ht hkt3 hqt3 + (by have := runnable_code hrn; omega) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hSR3 ht sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 t _ Q0 R0 h3 hB3 hX3 hS3' hSg3 htH P1.not_blocked_inactive P1.inactive_ne + (Or.inr hnbt3) (hX3.noref (hTP t htH) hnrp3 (fun e0 => hnbt3 (Or.inr (Or.inl e0)))) + ⟨fun c hc' p hp hm => absurd hm (h0.notQ htH hcb7.nq hc' hp), fun c hc' hm => absurd hm (h0.notR htH hcb7.nr hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 t sched.TS_INACTIVE h3 htH)) + -- FL4: no reply pushed, the sender `Inactive` + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ sched.set_thread_state st s3 t sched.TS_INACTIVE = ok r.1) + ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [hty3, if_pos (by unfold fl4_abi.consts.fault.NullFault; decide)] + by_cases hi : (L.mem s7 (tReplyA d') != 0#u64) = true + · rw [if_pos hi] + have hgg : g = false ∧ gr = false := by + rcases hno with h | hn + · exact h + · exfalso + have hr0 := hn xd hkdσ _ _ _ hstd + rw [hr0] at hrep + have h00 := optRef_none hrep + rw [h00] at hi + simp at hi + obtain ⟨rfl, rfl⟩ := hgg + simp only [Bool.false_eq_true, ↓reduceIte] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + · rw [if_neg hi] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + rintro ⟨s4, a4⟩ ⟨ha4, hs4⟩ + simp only at ha4 hs4 + subst ha4 + try dsimp only + have hpost := hstsT + rw [hs4] at hpost + obtain ⟨⟨⟨hR4, hS4⟩, hW4⟩, ⟨h4, hB4, hX4, hS4', hSg4, -, k4⟩, fu4⟩ := (WP.spec_ok _).1 hpost + have hkd4 : (ciSt σ3 t.bv xt).kheap d'.bv = some (.tcb x3) := by rw [ciSt_kh (Ne.symm htd')]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, (ciSt σ3 t.bv xt).kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z (by rw [← ciSt_kh (ciSt_ne hz (fun _ h => by cases h))]; exact hz) + have hx4 : d' ∈ {y | y ∈ X3 ∧ y ≠ t} ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := by + rcases hx3 with h | h + · exact Or.inl ⟨h, Ne.symm htd⟩ + · right + have e0 : L.mem s4 (P1.stA d') = L.mem s3 (P1.stA d') := by + rw [P1.stA_eq] + exact k4.fld d' hdH _ (by simp) (by simp) (by simp) (by simp) (fun e1 => absurd e1 (Ne.symm htd)) + rw [e0]; exact h + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := fun r hr => by + rcases k4.rtcb r hr with e0 | ⟨-, e0⟩ + · rw [e0]; exact hnr3 r hr + · rw [e0]; intro h0'; exact hdta.ne0 (by rw [← h0']; rfl) + have hks4 : L.ksb s4 = L.ksb s7 := hW4.1.trans hks3 + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 hns3 + .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q0 R0 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σ5 := Notif.stsAct (Notif.updK (ciSt σ3 t.bv xt) d'.bv (.tcb x5)) d'.bv with hσ5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 σ5 := IpcRel.ofSRN hR5 hS5 + have hW4' : P1.WritesIn st s7 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW3.trans1 (hW4.mono fun b hb => Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3).1 hb)))) + have hW5' : P1.WritesIn st s7 s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb)))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr (X := {y | y ∈ X3 ∧ y ≠ t}) (fun y hy => hXc3 y hy.1))⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + -- the shadows to the end, and the end its own shadow + have hc3 : t.bv ≠ (Notif.updK σ3 t.bv (.tcb { xt with tcbState := .inactive })).curThread := by + show t.bv ≠ σ3.curThread; rw [hct3]; exact htc + have eρ4 : rhoT (ciSt σ3 t.bv xt) t.bv sc.bv xT yS n = rhoT σ3 t.bv sc.bv xT yS n := by + unfold ciSt; rw [stsAct_ne hc3, rhoT_updKt] + have hdc4 : d'.bv ≠ (Notif.updK (ciSt σ3 t.bv xt) d'.bv (.tcb x5)).curThread := by + show d'.bv ≠ (ciSt σ3 t.bv xt).curThread + rw [(ciSt_skD (σ := σ3) (c := t.bv) (x := xt)).ct, hct3]; exact hdc + have hdc4ρ : d'.bv ≠ (Notif.updK (rhoT σ3 t.bv sc.bv xT yS n) d'.bv (.tcb x5)).curThread := by + show d'.bv ≠ σ3.curThread; rw [hct3]; exact hdc + have eρ5 : rhoT σ5 t.bv sc.bv xT yS n = + Notif.stsAct (Notif.updK (rhoT σ3 t.bv sc.bv xT yS n) d'.bv (.tcb x5)) d'.bv := by + rw [hσ5, stsAct_ne hdc4, rhoT_updK _ (Ne.symm htd') hdsc, eρ4, stsAct_ne hdc4ρ] + have hkd3ρ : (rhoT σ3 t.bv sc.bv xT yS n).kheap d'.bv = some (.tcb x3) := by + rw [rhoT_kh (Ne.symm htd') hdsc]; exact hkd3 + have exT : ({ xt with tcbState := .inactive } : Fl4Spec.Tcb) = xT := by + simp only [hxt, hxT]; cases x; cases hrs; rfl + have hk5t : σ5.kheap t.bv = some (.tcb xT) := by + rw [hσ5, stsAct_kheap]; simp only [updK_kheap, Function.update_of_ne htd']; rw [ciSt_kc, exT] + have hk5sc : σ5.kheap sc.bv = some (.schedContext yS n) := by + rw [hσ5, stsAct_kheap]; simp only [updK_kheap, Function.update_of_ne (Ne.symm hdsc)] + rw [ciSt_kh (Ne.symm hst)]; exact hksc3 + have eσ5 : rhoT σ5 t.bv sc.bv xT yS n = σ5 := rhoT_self hk5t hk5sc + have Fr5 : CFr e.bv d'.bv σ0 σ5 := by + rw [← eσ5, eρ5]; exact Fr3.trans (CFr.stsRun hkd3ρ hrb3) + have IK5 : IK d'.bv σ0 σ5 := by + rw [← eσ5, eρ5]; exact (IK3.trans (IK.updSelf (y := x5) hkd3ρ rfl)).trans (IK.sts _ _ _) + have SK5 : Ipc.SKeep σ0 σ5 := by + rw [← eσ5, eρ5] + exact (SK3.trans (Ipc.SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3ρ, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (Ipc.SKeep.stsAct _ _) + have hnqD : NotQueued σ0 d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x0, -, -, -, hkx0, -, hrx, -, -⟩ := J0.inv.rq dm p _ hm + rw [hkd0] at hkx0; cases hkx0; rw [hstd] at hrx; cases hrx + · obtain ⟨x0, -, -, -, hkx0, -, hrx, -, -⟩ := J0.inv.rel _ hm + rw [hkd0] at hkx0; cases hkx0; rw [hstd] at hrx; cases hrx + have hrib : RelIfBound σ5 d'.bv := + Ipc.RelIfBound.of_keep (J0.inv.rd d'.bv xd hkd0 (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σ5 := ((J0.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σ5.schedulerAction = σ0.schedulerAction := by + rw [hσ5, stsAct_ne hdc4] + show (ciSt σ3 t.bv xt).schedulerAction = σ.schedulerAction + unfold ciSt; rw [stsAct_ne hc3]; exact hact3 + have hAI5 : ActIdle σ5 := ⟨fun u hu => Fr5.sched (J0.act.1 u (by rw [← act5]; exact hu)), Fr5.idleOK J0.act.2⟩ + have hcur5 : σ5.schedulerAction = .chooseNewThread ∨ CurInv σ5 := + J0.cur.imp (fun e0 => by rw [act5]; exact e0) Fr5.curInv + have hrun5 : CurRunM σ5 := J0.curRun.of_cn Fr5.curRunW (Or.inr act5) + -- the footprints from the base + have hSσ := J.xtop.fin.1.1.1 + obtain ⟨Qs, Rs, hcs, hsps⟩ := hSσ.lists + obtain ⟨ys, ns, hkys, hys⟩ := hsps.scs sc hsc + rw [hky] at hkys; cases hkys + have hal8 : P1.Aligned (scW sc 8#u64) := (hys.hdr (K := 8#u64) (by simp) (by simp)).1 + have hta := hP.tcb t ht + have hw0 : P1.WritesIn st s s0 (fun b => (((b = tFaultA t ∨ b = tFw0A t) ∨ b = tFw1A t) ∨ b = tFw2A t) ∨ + b = scW sc 8#u64) := + ((((writesIn_wr (st := st) (s := s) (v := fl4_abi.consts.fault.Timeout) (a_fault hta).1).trans + (writesIn_wr (v := b) (a_fw0 hta).1)).trans (writesIn_wr (v := 0#u64) (a_fw1 hta).1)).trans + (writesIn_wr (v := 0#u64) (a_fw2 hta).1)).trans (writesIn_wr (v := 0#u64) hal8) + have hz7 : SzKeepC Z.C σ σf := SzKeepC.of (SzKeep.of_tcb (p := t.bv) (x' := xt) (fun q hq => hkf q hq) hkt) + have hWs5 : P1.WritesIn st s s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW7.trans1 (hW5'.mono fun b hb => cnW_back hW7.1 hz7 hb) + have hsw : SW Z.C σ (scW sc 8#u64) := sw_hdr hSσ hsc (K := 8#u64) (by simp) (by simp) (by decide) + have hsub0 : ∀ b0, ((((b0 = tFaultA t ∨ b0 = tFw0A t) ∨ b0 = tFw1A t) ∨ b0 = tFw2A t) ∨ b0 = scW sc 8#u64) → + CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b0 := by + intro b0 hb0 + rcases hb0 with (((h | h) | h) | h) | h <;> subst h + · exact Or.inl (Or.inr ⟨t, ht, 3376, by omega, by omega, by omega, by omega, by omega, val_fault hta⟩) + · exact Or.inl (Or.inr ⟨t, ht, 3384, by omega, by omega, by omega, by omega, by omega, val_fw0 hta⟩) + · exact Or.inl (Or.inr ⟨t, ht, 3392, by omega, by omega, by omega, by omega, by omega, val_fw1 hta⟩) + · exact Or.inl (Or.inr ⟨t, ht, 3400, by omega, by omega, by omega, by omega, by omega, val_fw2 hta⟩) + · exact Or.inl (Or.inl (Or.inr (Or.inr hsw))) + have hz0 : SzKeepC Z.C σ0 σ := by + intro p _ y0 m hp + by_cases hps : p = sc + · subst hps; rw [hky] at hp; cases hp; exact ⟨yS, by rw [hσ0r]; exact rhoT_ksc⟩ + · have hps' : p.bv ≠ sc.bv := fun e0 => hps (P1.eq_of_bv e0) + have hpt : p.bv ≠ t.bv := fun e0 => by rw [e0, hkx] at hp; cases hp + exact ⟨y0, by rw [hk0 _ hpt hps']; exact hp⟩ + have hW05 : P1.WritesIn st s0 s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s0 σ0) := + (writesIn_rebase hWs5 hw0 hsub0).mono fun b0 hb0 => CnW.back (s := s0) (s' := s) hw0.1.symm hz0 hb0 + have hfs5 := footW_trans (footW_of (W := fun _ => False) hfoot7) hfoot + have hsubK : ∀ b0, ((((b0 = tFaultA t ∨ b0 = tFw0A t) ∨ b0 = tFw1A t) ∨ b0 = tFw2A t) ∨ b0 = scW sc 8#u64) → + P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s b0 ∨ False := by + intro b0 hb0 + rcases hb0 with (((h | h) | h) | h) | h <;> subst h + · exact Or.inl (Or.inl ⟨t, htH, 3376#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inl ⟨t, htH, 3384#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inl ⟨t, htH, 3392#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inl ⟨t, htH, 3400#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨sc, hsc1, scW8_inSC J.blk hsc1⟩)))) + have hfoot05 : P1.WritesIn st s0 s5 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s0) := + (writesIn_rebase hfs5 hw0 hsubK).mono fun b0 hb0 => hb0.elim (P1.KW.ksb hw0.1.symm) False.elim + have hI0 := J0.xtop.irelK J0.ks J0.buf + have hF : IFrame (L := L) Z.th s0 s5 := IRel.iframe_of hI0 hW05 fun b0 hb0 => CnW.oobj hI0.2.2.1 hb0 + have htD := J0.not_dead_of_st hdT hkd0 (by rw [hstd]; simp [tsCode]) + have hRR : ∀ q' ∈ Z.R, ∀ xr, σ0.kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem s0 (rNextA q') = 0#u64 ∧ L.mem s0 (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB J0 q' hq'' xr d'.bv xd hx' + hxt' hkd0 (by rw [hstd]; rfl) + have J5 := J0.wake_postD hIR hF hW05 hblk hfoot05 (DWFr.ofCFr Fr5) I5 (Fr5.bsa J0.bsa) hAI5 + hcur5 hrun5 hRR htD ⟨d', hdT, rfl⟩ + have Fw : NatFoot (L := L) Z s0 σ0 s5 σ5 := + NatFoot.ofWS hF.1 hW05 (fun _ h => Or.inl (Ipc.CnW.oobj J0.xtop.fin.1.1.1 h)) (fun _ h => J0.cnW_nv h) + (fun _ h => J0.cnW_nh h) (natK_dwfr (DWFr.ofCFr Fr5)) + -- the tail + have hk5 : σ5.kheap d'.bv = some (.tcb x5) := by rw [hσ5, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ σ5.releaseQueue := by rw [SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → σ5.kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hy0 := SK5.2.2.2 p y n hy + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J0.inv.back _ _ _ hkd0 hp' + rw [hy0] at hky'; cases hky' + exact J0.bsa.tcb p y n d'.bv xd hy0 hyt hkd0 (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ σ5.idleThread := by + rw [Fr5.sk.idle] + intro h + obtain ⟨x0, hkx0, -, hxs, -⟩ := J0.act.2 + rw [← h, hkd0] at hkx0; cases hkx0; rw [hstd] at hxs; cases hxs + have htc5 : d'.bv ≠ σ5.curThread := by rw [Fr5.sk.ct]; exact hdc + have hcond : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨hg, hr⟩ + rcases hno with ⟨rfl, rfl⟩ | hn + · rcases hg with h | h <;> cases h + · exact hr (hn xd hkdσ _ _ _ hstd) + have hspec := sendIpc_faultInactive_eq (don := false) hk hkd hde htd' hstd true badge.bv g gr hcond e2 hU hkt3 hkd3 + (by rw [hft]; simp) (t := t.bv) + have hCsc : ∀ q ∈ Z.C, ∃ y n, σ.kheap q.bv = some (.schedContext y n) := fun q hq => by + obtain ⟨Qx, Rx, hcx, hsx⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨y', n', hq', -⟩ := hsx.scs q hq + exact ⟨y', n', hq'⟩ + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7', a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + refine ⟨((), σ7), ?_, V0, Lst7, J7, NatFoot.trans hCsc (NatFoot.trans hCsc Fb Fw) F7.cast⟩ + rw [hspec] + show ((), σ7) ∈ (Endp.specWakeTailE d'.bv σ5).1 + rw [h7]; exact Set.mem_singleton _ + +set_option maxHeartbeats 64000000 in +/-- (stable ghosts, `JStab`) **The timeout-fault `send_ipc` of a thread other than the current one to +a waiting receiver with a reply object, the sender with the grant or +grant-reply right: the reply pushed without a donation** (`can_donate` is +false for `handle_timeout`; as `JAtZ.sendTmoRecvIJSt` up to the reply unlink, +then `reply_push` as XTopJointFaultRecv's `JAtZ.sendFaultPJ` (P1's queue bits +of the sender from `NotQueued`, its SC's activity from its `CanBlock` at the +send, the action not the sender nor the receiver by `actionW_ne_of`); +`JAtZ` by `JAtZ.wake_postGR` from the base `JAtZ.tmoBaseJ` at the push from +the receiver's woken shadow (`pushSt`, the scheduler action kept: the sender +is not the current thread). -/ +theorem JAtZB.sendTmoRecvPJFB (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + (hcr : ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ Fl4Spec.runnable xc.tcbState = true) + {t : U64} (ht : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (b : U64) {ts : Fl4Spec.ThreadState} (hrun : Fl4Spec.runnable ts = true) {s7 : S} + (hI7 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s7 + (updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) }))) + (hblk7 : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s7) + (hW7 : P1.WritesIn st s s7 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hfoot7 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s7) + (hcb7 : P1.CanBlock st (P1.Heap.tcbs H) Ac s7 t) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hke : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (badge : U64) (g gr : Bool) + (hg : g = true ∨ gr = true) (htD : t ∉ D) (hya : Fl4Spec.scActive y = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) : + endpoint.send_ipc st arI s7 a true false badge g gr false t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) true false badge.bv g gr false t.bv e.bv + (updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) }))).1, + TmoFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r.1 r.2 z.2 ⦄ := by + set xt : Fl4Spec.Tcb := { x with tcbState := ts, tcbFault := some (.timeout b.bv) } with hxt + set σf := updK σ t.bv (.tcb xt) with hσf + set xT : Fl4Spec.Tcb := { x with tcbFault := some (.timeout b.bv) } with hxT + set yS : Fl4Spec.SchedContext := { y with scConsumed := (0#u64 : U64).bv } with hyS + have hns : ∀ e d, x.tcbState ≠ .blockedOnSend e d := by rw [hrs]; intro _ _ h; cases h + have hst : t.bv ≠ sc.bv := fun e0 => by rw [e0, hky] at hk; cases hk + -- the base `J` state + obtain ⟨V0, J0, Fb⟩ := J.tmoBaseJV hAT hmax htu ht hk hns b hsc hky + set s0 := tmoS st (fltS st s t b) sc with hs0 + set σ0 := Ipc.tmoSt (updK σ t.bv (.tcb xT)) sc y n with hσ0 + have hσ0r : σ0 = rhoT σ t.bv sc.bv xT yS n := rfl + have hfr : rhoT σf t.bv sc.bv xT yS n = σ0 := by rw [hσf, rhoT_updKt, hσ0r] + have hk0 : ∀ q, q ≠ t.bv → q ≠ sc.bv → σ0.kheap q = σ.kheap q := fun q h1 h2 => by + rw [hσ0r]; exact rhoT_kh h1 h2 + have hkf : ∀ q, q ≠ t.bv → σf.kheap q = σ.kheap q := fun q h1 => by simp [hσf, updK, h1] + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hI := hI7 + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hte0 : t.bv ≠ e.bv := kheap_ne hk hke (fun _ h1 h2 => by subst h1; cases h2) + have hse0 : sc.bv ≠ e.bv := kheap_ne hky hke (fun _ h1 h2 => by subst h1; cases h2) + have hkx := hk + have hk : σf.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q))) := by rw [hkf _ (Ne.symm hte0)]; exact hke + have hkt : σf.kheap t.bv = some (.tcb xt) := by simp [hσf, updK] + have hrn : Fl4Spec.runnable xt.tcbState = true := hrun + have hft : xt.tcbFault = some (.timeout b.bv) := rfl + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s7 (tStA d')).val = 3 := (hB d' (by simp)).1 + have hdb : L.mem s7 (tBlockA d') = e := (hB d' (by simp)).2 + obtain ⟨e', rep, dd, hstd⟩ := tsCode_three (hxd.st.symm.trans hd3) + have he' : e' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := hte0 + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdsc : d'.bv ≠ sc.bv := fun e0 => by + have h1 : σf.kheap sc.bv = some (.schedContext y n) := by rw [hkf _ (Ne.symm hst)]; exact hky + rw [e0, h1] at hkd; cases hkd + have hkdσ : σ.kheap d'.bv = some (.tcb xd) := by rw [← hkf _ (Ne.symm htd')]; exact hkd + have hkd0 : σ0.kheap d'.bv = some (.tcb xd) := by rw [hk0 _ (Ne.symm htd') hdsc]; exact hkdσ + have hke0 : σ0.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := by + rw [hk0 _ (Ne.symm hte0) (Ne.symm hse0)]; exact hke + have hdc : d'.bv ≠ σf.curThread := by + intro h + obtain ⟨xc, hkc, hrc⟩ := hcr + have h' : d'.bv = σ.curThread := h + rw [← h', hkdσ] at hkc; cases hkc; rw [hstd] at hrc; cases hrc + -- P1's facts at the send + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := hblk7 + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s7 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s7 (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s7 d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s7 e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s7 e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s7 e) ?_ + rintro _ rfl + have hhead : (L.epQ s7 e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s7 σf hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s7 he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σf e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s7 s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s7 s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s7 s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, ea1, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + have hky1 : σ1.kheap sc.bv = some (.schedContext y n) := by + simp [σ1, hse0, hσf, updK, Ne.symm hst, hky] + 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 _) + -- the shadow frame from the base + have eρ1 : rhoT σ1 t.bv sc.bv xT yS n = updK σ0 e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + rw [hσ1, rhoT_updK _ (Ne.symm hte) (Ne.symm hse0), hfr] + have Fr1 : CFr e.bv d'.bv σ0 (rhoT σ1 t.bv sc.bv xT yS n) := by + rw [eρ1] + exact CFr.upd hke0 (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ0 (rhoT σ1 t.bv sc.bv xT yS n) := by + rw [eρ1] + exact IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hke0 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ0 (rhoT σ1 t.bv sc.bv xT yS n) := by + rw [eρ1] + exact Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) + (fun _ _ h => by cases h) + have hOKσ := (J.xferH).1 + have hBCσ := (J.xferH).2.1 + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (ipcBufCapOK_updT (y := xt) hOKσ hkx rfl rfl) (EpStep.ep hk) + have hBC1 : Endp.BufCover Z.T Z.B σ1 := bufCover_epStep (bufCover_updT (y := xt) hBCσ hkx rfl rfl) (EpStep.ep hk) + have hsc1 : sc ∈ P1.Heap.scs H := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨_, (hJ.sc sc y n (J.c0 sc hsc) hky).2⟩ + -- the transfer + refine WP.spec_bind (xferTmoStepJ hP0 (hH.epHyps hP0) hmax s1 a σ1 htu hR1 hS1 hOK1 hBC1 ht hdT htd hkt1 hkd1 hft + hxsc hky1 e badge g hTP hdH h1 hB1 hX1 hS1' hSg1 hsc1) ?_ + rintro s2 ⟨x2, e2, hx2, hR2, hS2, hA2, w2, ea2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Ipc.tmoSt σ1 sc y n) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by + rw [hσ2] + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne htd', Function.update_of_ne hst] + exact hkt1 + have hksc2 : σ2.kheap sc.bv = some (.schedContext yS n) := by + rw [hσ2, updK_kheap, Function.update_of_ne (Ne.symm hdsc)] + show Function.update σ1.kheap sc.bv _ sc.bv = _ + rw [Function.update_self] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc2 : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + rw [hσ2] + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne (Ne.symm hde), Function.update_of_ne (Ne.symm hse0)] + exact hk1e + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + have hns' : sc.bv ≠ e''.bv := by intro h; rw [← h, hksc2] at hz; cases hz + rw [hσ2] at hz + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne (Ne.symm hne), + Function.update_of_ne (Ne.symm hns')] at hz + exact hq1 e'' he'' z hz + have hrw : L.mem s2 (tReplyA d') = L.mem s7 (tReplyA d') := by + rw [show tReplyA d' = core.num.U64.wrapping_add d' 3360#u64 from rfl, f2 d' hdH _ (by simp) (by simp) (by simp), + ← show tReplyA d' = core.num.U64.wrapping_add d' 3360#u64 from rfl, + hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + have hscw8 : SW Z.C σf (scW sc 8#u64) := sw_hdr hI.2.2.1 hsc (K := 8#u64) (by simp) (by simp) (by decide) + have hW2 : P1.WritesIn st s7 s2 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW1.trans1 (w2.mono fun b hb => by + rcases hb with ⟨eb, -⟩ | (⟨y0, hy, K, h1', h2', -, eb⟩ | hb) + · subst eb; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))) + · have hyT : y0 ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inr ⟨y0, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩) + rw [eb]; exact tfield_val (hP.tcb y0 hyT) (by omega) + · exact absurd hb (Set.notMem_empty _)) + have F2 : P1.WritesIn st s1 s2 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1) := + w2.mono fun b hb => by + rcases hb with ⟨-, c0, hc0, hin⟩ | (⟨y0, hy, K, h1', h2', h3', eb⟩ | hb) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨c0, hc0, hin⟩))) + · exact Or.inl ⟨y0, hy, K, by omega, by omega, by omega, by omega, eb⟩ + · exact absurd hb (Set.notMem_empty _) + have eρ2 : rhoT σ2 t.bv sc.bv xT yS n = updK (rhoT σ1 t.bv sc.bv xT yS n) d'.bv (.tcb x2) := by + rw [hσ2, rhoT_updK _ (Ne.symm htd') hdsc, rhoT_updKsc _ hst] + have hkd1ρ : (rhoT σ1 t.bv sc.bv xT yS n).kheap d'.bv = some (.tcb xd) := by + rw [rhoT_kh (Ne.symm htd') hdsc]; exact hkd1 + have Fr2 : CFr e.bv d'.bv σ0 (rhoT σ2 t.bv sc.bv xT yS n) := by + rw [eρ2] + exact Fr1.trans (CFr.upd hkd1ρ (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, + by rw [hx2], Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ0 (rhoT σ2 t.bv sc.bv xT yS n) := by + rw [eρ2]; exact IK1.trans (IK.updSelf hkd1ρ hxsc2) + have SK2 : Ipc.SKeep σ0 (rhoT σ2 t.bv sc.bv xT yS n) := by + rw [eρ2] + exact SK1.trans (Ipc.SKeep.updK (p := d'.bv) (o := .tcb x2) (fun y h => ⟨xd, hkd1ρ, by cases h; exact hxsc2.symm⟩) + (fun _ _ h => by cases h)) + have hks2 : L.ksb s2 = L.ksb s7 := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s7 (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkdσ _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s7 (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s7 (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hws : w.bv ≠ sc.bv := fun e0 => by + have h1 : σf.kheap sc.bv = some (.schedContext y n) := by rw [hkf _ (Ne.symm hst)]; exact hky + rw [e0, h1] at hkr; cases hkr + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + rw [hσ2] + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne hwd, Function.update_of_ne hws] + simp [σ1, hwe, hkr] + have hkrσ : σ.kheap w.bv = some (.reply xr) := by rw [← hkf _ hwt]; exact hkr + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkdσ e.bv w.bv dd xr (by rw [hstd]) hkrσ + have hX2' := IpcRel.ofSRN hR2 hS2 + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2 hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st s7 s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW2.trans1 (hW3.mono fun b hb => cnW_back hks2 (SzKeepC.of (by + intro p y0 m hp + by_cases hps : p = sc.bv + · subst hps; rw [hksc2] at hp; cases hp; exact ⟨y, by rw [hkf _ (Ne.symm hst)]; exact hky⟩ + · refine ⟨y0, ?_⟩ + rw [hσ2] at hp + have hpd : p ≠ d'.bv := fun e0 => by rw [e0] at hp; simp at hp + simp only [Ipc.tmoSt, Sched.updSc, updK_kheap, Function.update_of_ne hpd, Function.update_of_ne hps] at hp + have hpe : p ≠ e.bv := fun e0 => by rw [e0] at hp; simp [σ1] at hp + simpa [σ1, hpe] using hp)) hb) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hksc3 : σ3.kheap sc.bv = some (.schedContext yS n) := by + rw [stsAct_kheap]; simp [σ3a, Ne.symm hdsc, hws.symm, hksc2] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + -- the shadows + have hkr2ρ : (rhoT σ2 t.bv sc.bv xT yS n).kheap w.bv = some (.reply xr) := by + rw [rhoT_kh hwt hws]; exact hkr2 + have hkd3aρ : (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv (.reply xr3)).kheap d'.bv = some (.tcb x2) := by + simp only [updK_kheap, Function.update_of_ne (Ne.symm hwd)] + rw [rhoT_kh (Ne.symm htd') hdsc]; exact hkd2 + have eρ3 : rhoT σ3 t.bv sc.bv xT yS n = + Notif.stsAct (Notif.updK (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv (.reply xr3)) d'.bv (.tcb x3)) d'.bv := by + rw [e3s, stsAct_ne (show d'.bv ≠ (Notif.updK (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv + (.reply xr3)) d'.bv (.tcb x3)).curThread from hdc), hσ3a, rhoT_updK _ (Ne.symm htd') hdsc, rhoT_updK _ hwt hws] + have Fr3 : CFr e.bv d'.bv σ0 (rhoT σ3 t.bv sc.bv xT yS n) := by + rw [eρ3] + exact (Fr2.trans (CFr.upd hkr2ρ (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3aρ (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σ0 (rhoT σ3 t.bv sc.bv xT yS n) := by + rw [eρ3] + have IK3a : IK d'.bv σ0 (Notif.updK (rhoT σ2 t.bv sc.bv xT yS n) w.bv (.reply xr3)) := + IK2.trans (IK.updOther (o' := .reply xr3) hkr2ρ (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + exact (IK3a.trans (IK.updSelf (y := x3) hkd3aρ rfl)).trans (IK.sts _ _ _) + have SK3 : Ipc.SKeep σ0 (rhoT σ3 t.bv sc.bv xT yS n) := by + rw [eρ3] + exact ((SK2.trans (Ipc.SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) + (fun _ _ h => by cases h))).trans (Ipc.SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3aρ, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (Ipc.SKeep.stsAct _ _) + have hct3 : σ3.curThread = σ.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc2] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have F3 := footW_trans (footW_trans (footW_of (W := fun _ => False) F1) (F2.mono fun b hb => Or.inl hb)) + (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) = 5#u64 := by + have hfr := hxt3.faultR + unfold FaultRel at hfr; rw [hft] at hfr; exact hfr.1 + have hks3 : L.ksb s3 = L.ksb s7 := hW3'.1 + -- the sender's and the receiver's words at `s3` + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x0, -, -, -, hkx0, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkdσ] at hkx0; cases hkx0; rw [hstd] at hrx; cases hrx + · obtain ⟨x0, -, -, -, hkx0, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkdσ] at hkx0; cases hkx0; rw [hstd] at hrx; cases hrx + have hnqT : NotQueued σ t.bv := J.inv.notQueued hkx (by rw [hrs]; rfl) + have hqq3 : σ3.readyQueues = σ.readyQueues := IK3.2.1 + have hrq3 : σ3.releaseQueue = σ.releaseQueue := IK3.2.2.1 + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [hqq3]; exact hnqT.1, by rw [hrq3]; exact hnqT.2⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [hqq3]; exact hnqD.1, by rw [hrq3]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have hna3t : L.actionW s3 ≠ t := actionW_ne_of hSR3 ht (by + rw [hact3]; intro e0 + obtain ⟨x', -, -, -, h1', -, h3', -⟩ := schedulable_inv (J.act.1 _ e0) + rw [hkx] at h1'; cases h1'; rw [hrs] at h3'; cases h3') + have hna3d : L.actionW s3 ≠ d' := actionW_ne_of hSR3 hdT (by + rw [hact3]; intro e0 + obtain ⟨x', -, -, -, h1', -, h3', -⟩ := schedulable_inv (J.act.1 _ e0) + rw [hkdσ] at h1'; cases h1'; rw [hstd] at h3'; cases h3') + have hidl3 : σ3.idleThread = σ.idleThread := by rw [e3s]; rfl + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by + intro h + obtain ⟨x0, hkx0, -, hxs, -⟩ := J.act.2 + rw [← h, hkx] at hkx0; cases hkx0; rw [hrs] at hxs; cases hxs + have hid : d'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x0, hkx0, -, hxs, -⟩ := J.act.2 + rw [← h, hkdσ] at hkx0; cases hkx0; rw [hstd] at hxs; cases hxs + have hsA3 : L.mem s3 (P1.scA t) = L.mem s7 (P1.scA t) := by + rw [P1.scA_eq, k3.fld t htH _ (by simp) (by simp) (by simp) (by simp) (fun e0 => absurd e0 htd), + f2 t htH _ (by simp) (by simp) (by simp), f1 t htH _ (by simp) (by simp) (by simp) (by simp)] + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0' => by + rw [hsA3] at h0' ⊢; exact hcb7.ac h0' + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the push on `IRel`, at P1 + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hns3 : tsCode xt.tcbState ≠ 6 := by have := runnable_code hrn; omega + have hPush : ipc.reply_push st arI s3 a t d' w false ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv false σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1 ⦄ := by + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := false) (P := fun s' σ' => σ' = pushSt σ3 t.bv w.bv xr3 xt ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hdon => by cases hdon)) + (P1.ipc.reply_push.ipc hAT s3 a t d' w false Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + hna3t (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hqbD hrbD hna3d (hiw3 d' hdT hid) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a t d' w false h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + hna3t (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hcA (Or.inl rfl))) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, hb2, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hb2, hf, hsb⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv false σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1) ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [hty3, if_pos (by unfold fl4_abi.consts.fault.NullFault; decide)] + rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, ⟨⟨Q4, R4, h4⟩, hB4, hX4, hS4', hSg4⟩, hst4, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 h4 hB4 hX4 hS4' hSg4 hst4 fu4 hsb4 ⊢ + subst ha4 + try dsimp only + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hz3 : SzKeepC Z.C σf σ3 := by + intro p _ y0 m hp + by_cases hps : p = sc + · subst hps; rw [hksc3] at hp; cases hp; exact ⟨y, by rw [hkf _ (Ne.symm hst)]; exact hky⟩ + · have hps' : p.bv ≠ sc.bv := fun e0 => hps (P1.eq_of_bv e0) + have hpt : p.bv ≠ t.bv := fun e0 => by rw [e0, hkt3] at hp; cases hp + have hpd : p.bv ≠ d'.bv := fun e0 => by rw [e0, hkd3] at hp; cases hp + have hpw : p.bv ≠ w.bv := fun e0 => by rw [e0, hkr3] at hp; cases hp + have hpe : p.bv ≠ e.bv := fun e0 => by + have h1 : σ3.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + rw [stsAct_kheap]; simp [σ3a, updK, Ne.symm hde, Ne.symm hwe, hk1e2] + rw [e0, h1] at hp; cases hp + refine ⟨y0, ?_⟩ + rw [stsAct_kheap] at hp + simp [σ3a, hσ2, updK, hpd, hpw, hps', σ1, hpe] at hp + exact hp + have hW4' : P1.WritesIn st s7 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW3'.trans1 (hW4.mono fun b hb => cnW_back hks3 hz3 hb) + have hks4 : L.ksb s4 = L.ksb s7 := hW4'.1 + set σ4 := pushSt σ3 t.bv w.bv xr3 xt with hσ4 + have hkd4 : σ4.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, σ4.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z ((ps_oth hkt3 hkr3 hwt (fun _ h => by cases h) (fun _ h => by cases h)).1 hz) + have hst4d : L.mem s4 (P1.stA d') = sched.TS_INACTIVE := by rw [hst4 d' hdH (Ne.symm htd)]; exact e3' + have hx4 : d' ∈ (∅ : Set U64) ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := Or.inr (by rw [hst4d]; exact P1.not_blocked_inactive) + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := + hX4.noref hdta (by rw [hst4d]; exact P1.inactive_ne.1) (by rw [hst4d]; exact P1.inactive_ne.2) + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 + (by simp [x3, tsCode]) .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q4 R4 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK σ4 d'.bv (.tcb x5)) d'.bv = pushSt σa t.bv w.bv xr3 xt := + ps_swap htd' hwd hdc3 + have hW5' : P1.WritesIn st s7 s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb)))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q4, R4, h5.congrX P1.empty_congr⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have hrb3 : RbIn x3.tcbState := Or.inr rfl + -- the shadow of the state before the push + have hdcρ : d'.bv ≠ (Notif.updK (rhoT σ3 t.bv sc.bv xT yS n) d'.bv (.tcb x5)).curThread := by + show d'.bv ≠ σ3.curThread; exact hdc3 + set σb := rhoT σa t.bv sc.bv xT yS n with hσb + have eρa : σb = Notif.stsAct (Notif.updK (rhoT σ3 t.bv sc.bv xT yS n) d'.bv (.tcb x5)) d'.bv := by + rw [hσb, hσa, stsAct_ne (show d'.bv ≠ (Notif.updK σ3 d'.bv (.tcb x5)).curThread from hdc3), + rhoT_updK _ (Ne.symm htd') hdsc, stsAct_ne hdcρ] + have hkd3ρ : (rhoT σ3 t.bv sc.bv xT yS n).kheap d'.bv = some (.tcb x3) := by + rw [rhoT_kh (Ne.symm htd') hdsc]; exact hkd3 + have Fr5 : CFr e.bv d'.bv σ0 σb := by rw [eρa]; exact Fr3.trans (CFr.stsRun hkd3ρ hrb3) + have IK5 : IK d'.bv σ0 σb := by + rw [eρa]; exact (IK3.trans (IK.updSelf (y := x5) hkd3ρ rfl)).trans (IK.sts _ _ _) + have SK5 : Ipc.SKeep σ0 σb := by + rw [eρa] + exact (SK3.trans (Ipc.SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3ρ, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (Ipc.SKeep.stsAct _ _) + have hkda : σa.kheap d'.bv = some (.tcb x5) := by rw [hσa, stsAct_kheap]; simp + have hksca : σa.kheap sc.bv = some (.schedContext yS n) := by + rw [hσa, stsAct_kheap]; simp only [updK_kheap, Function.update_of_ne (Ne.symm hdsc)]; exact hksc3 + have hkta : σb.kheap t.bv = some (.tcb xT) := rhoT_kt hst + have hkwa : σb.kheap w.bv = some (.reply xr3) := by + rw [hσb, rhoT_kh hwt hws, hσa, stsAct_kheap]; simp only [updK_kheap, Function.update_of_ne hwd]; exact hkr3 + -- the push from the shadow is the push from the state + have hpb : pushSt σa t.bv w.bv xr3 xt = pushSt σb t.bv w.bv xr3 xT := by + have e1 : σb = Notif.updK σa t.bv (.tcb xT) := by + rw [hσb, rhoT_eq] + show ({ σa with kheap := Function.update (Function.update σa.kheap t.bv (some (Fl4Spec.KernelObject.tcb xT))) sc.bv (some (Fl4Spec.KernelObject.schedContext yS n)) } : Fl4Spec.State Unit) = { σa with kheap := Function.update σa.kheap t.bv (some (Fl4Spec.KernelObject.tcb xT)) } + have e2 : Function.update (Function.update σa.kheap t.bv (some (Fl4Spec.KernelObject.tcb xT))) sc.bv + (some (Fl4Spec.KernelObject.schedContext yS n)) = Function.update σa.kheap t.bv (some (Fl4Spec.KernelObject.tcb xT)) := by + have e3 : (some (Fl4Spec.KernelObject.schedContext yS n) : Option Fl4Spec.KernelObject) = + Function.update σa.kheap t.bv (some (Fl4Spec.KernelObject.tcb xT)) sc.bv := by + rw [Function.update_of_ne (Ne.symm hst)]; exact hksca.symm + rw [e3, Function.update_eq_self] + rw [e2] + rw [e1] + unfold pushSt + show Notif.stsAct ({ σa with kheap := Function.update (Function.update σa.kheap w.bv (some (Fl4Spec.KernelObject.reply { xr3 with replyTcb := some t.bv }))) t.bv (some (Fl4Spec.KernelObject.tcb { xt with tcbState := .blockedOnReply w.bv })) } : Fl4Spec.State Unit) t.bv = Notif.stsAct ({ σa with kheap := Function.update (Function.update (Function.update σa.kheap t.bv (some (Fl4Spec.KernelObject.tcb xT))) w.bv (some (Fl4Spec.KernelObject.reply { xr3 with replyTcb := some t.bv }))) t.bv (some (Fl4Spec.KernelObject.tcb { xT with tcbState := .blockedOnReply w.bv })) } : Fl4Spec.State Unit) t.bv + rw [Function.update_comm (f := σa.kheap) (Ne.symm hwt), Function.update_idem] + all_goals rfl + have hIR0 := IpcRel.ofSRN hR5 hS5 + set σ' := pushSt σb t.bv w.bv xr3 xT with hσ' + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 σ' := by + rw [← hpb, ← heq5]; exact hIR0 + -- the scheduler facts + have hrib : RelIfBound σb d'.bv := + Ipc.RelIfBound.of_keep (J0.inv.rd d'.bv xd hkd0 (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have hnqD0 : NotQueued σ0 d'.bv := ⟨hnqD.1, hnqD.2⟩ + have I5 : Inv σb := ((J0.inv.toBut d'.bv).ik hnqD0 IK5).1.inv fun _ _ _ => hrib + have hnqTb : ∀ dm p, t.bv ∉ σb.readyQueues dm p := by rw [IK5.2.1]; exact hnqT.1 + have hnrTb : t.bv ∉ σb.releaseQueue := by rw [IK5.2.2.1]; exact hnqT.2 + have I5' : Inv σ' := ps_inv hkta hkwa hwt I5 hnqTb hnrTb + have hscA : ∀ p y0 n0, σb.kheap p = some (.schedContext y0 n0) → y0.scTcb = some t.bv → + Fl4Spec.scActive y0 = true := by + intro p y0 n0 hp hyt + by_cases hps : p = sc.bv + · subst hps; rw [hσb, rhoT_ksc] at hp; cases hp; exact hya + · have hp0 : σ0.kheap p = some (.schedContext y0 n0) := SK5.2.2.2 _ _ _ hp + obtain ⟨q0, hqC, eq⟩ := J0.obj.sc p y0 n0 hp0 + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J0.jpl + have hq0 := J0.c0 q0 hqC + have hqS : q0 ∈ P1.Heap.scs H := ⟨_, (hJ.sc q0 y0 n0 hq0 hp0).2⟩ + have hS0' := J0.xtop.fin.1.1.1 + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS0' J0.blk.2.2.1 J0.tcbs_eq.1.symm hqC hqS hq0 hp0 hyt + have hkt0 : σ0.kheap t.bv = some (.tcb xT) := by rw [hσ0r]; exact rhoT_kt hst + rw [hkt0] at hku; cases hku + exact absurd (Option.some.inj (hxs.symm.trans hxsc)) hps + have B5' : BlockedScActive σ' := ps_bsa hkta hkwa hwt hscA (Fr5.bsa J0.bsa) + have htcb : t.bv ≠ σb.curThread := by rw [Fr5.sk.ct]; exact htc + have act5 : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σb t.bv w.bv xr3 xT).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (σ := Notif.updK (Notif.updK σb w.bv (.reply { xr3 with replyTcb := some t.bv })) t.bv + (.tcb { xT with tcbState := .blockedOnReply w.bv })) htcb] + show σb.schedulerAction = σ.schedulerAction + rw [eρa, stsAct_ne hdcρ]; exact hact3 + have hidT : σb.idleThread ≠ t.bv := by rw [Fr5.sk.idle]; exact fun e0 => hit e0.symm + have hsched : ∀ u, Fl4Spec.schedulable u σ0 = true → Fl4Spec.schedulable u σ' = true := by + intro u hu + have hub := Fr5.sched hu + obtain ⟨xu, scp, yu, nu, hku, hscu, hrunu, hkyu, hacu, hnru⟩ := schedulable_inv hub + have hut : u ≠ t.bv := by rintro rfl; rw [hkta] at hku; cases hku; rw [hrs] at hrunu; cases hrunu + have huw : u ≠ w.bv := by rintro rfl; rw [hkwa] at hku; cases hku + exact schedulable_of (by rw [ps_ko hut huw]; exact hku) hscu hrunu + ((ps_oth hkta hkwa hwt (fun _ h => by cases h) (fun _ h => by cases h)).2 hkyu) hacu (by rw [ps_rel]; exact hnru) + have hAI5 : ActIdle σ' := + ⟨fun u hu => hsched u (J0.act.1 u (by rw [act5] at hu; exact hu)), ps_idleOK hkta hkwa hwt hidT (Fr5.idleOK J0.act.2)⟩ + have hcur5 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases J0.cur with e0 | hC + · left; rw [act5]; exact e0 + · right + have hCb := Fr5.curInv hC + obtain ⟨xc, hkc, hcs, hnb'⟩ := hCb.tcb + have hcw : σb.curThread ≠ w.bv := fun e0 => by rw [e0, hkwa] at hkc; cases hkc + have F := (ps_skD (σ := σb) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xT)) + obtain ⟨yc, nc, hkyc⟩ := hCb.sc + refine ⟨⟨xc, by rw [F.ct, ps_ko (Ne.symm htcb) hcw]; exact hkc, by rw [F.csc]; exact hcs, hnb'⟩, + ⟨yc, nc, by rw [F.csc]; exact (ps_oth hkta hkwa hwt (fun _ h => by cases h) (fun _ h => by cases h)).2 hkyc⟩, + fun hne y2 n2 hy => ?_, fun hm => ?_⟩ + · rw [F.csc, F.isc] at hne; rw [F.csc] at hy + exact hCb.active hne y2 n2 ((ps_oth hkta hkwa hwt (fun _ h => by cases h) (fun _ h => by cases h)).1 hy) + · rw [F.ct, ps_rel] at hm; exact hCb.notRel hm + have hrun5 : CurRunM σ' := by + have hRb : CurRunM σb := J0.curRun.of_cn Fr5.curRunW (Or.inr (by rw [eρa, stsAct_ne hdcρ]; exact hact3)) + refine hRb.of_cn (fun hW => ?_) (Or.inr ?_) + · have F := (ps_skD (σ := σb) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xT)) + rcases hW with hW | e0 + · left + intro y1 hy1 + rw [F.ct] at hy1 + have hcw : σb.curThread ≠ w.bv := fun e0 => by rw [e0, ps_kw hwt] at hy1; cases hy1 + rw [ps_ko (Ne.symm htcb) hcw] at hy1 + exact hW y1 hy1 + · right; rw [F.ct, F.idle]; exact e0 + · rw [act5, eρa, stsAct_ne hdcρ]; exact hact3.symm + -- the footprints from the base + have hSσ := J.xtop.fin.1.1.1 + obtain ⟨Qs, Rs, hcs, hsps⟩ := hSσ.lists + obtain ⟨ys, ns, hkys, hys⟩ := hsps.scs sc hsc + rw [hky] at hkys; cases hkys + have hal8 : P1.Aligned (scW sc 8#u64) := (hys.hdr (K := 8#u64) (by simp) (by simp)).1 + have hta := hP.tcb t ht + have hw0 : P1.WritesIn st s s0 (fun b => (((b = tFaultA t ∨ b = tFw0A t) ∨ b = tFw1A t) ∨ b = tFw2A t) ∨ + b = scW sc 8#u64) := + ((((writesIn_wr (st := st) (s := s) (v := fl4_abi.consts.fault.Timeout) (a_fault hta).1).trans + (writesIn_wr (v := b) (a_fw0 hta).1)).trans (writesIn_wr (v := 0#u64) (a_fw1 hta).1)).trans + (writesIn_wr (v := 0#u64) (a_fw2 hta).1)).trans (writesIn_wr (v := 0#u64) hal8) + have hz7 : SzKeepC Z.C σ σf := SzKeepC.of (SzKeep.of_tcb (p := t.bv) (x' := xt) (fun q hq => hkf q hq) hkt) + have hWs5 : P1.WritesIn st s s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW7.trans1 (hW5'.mono fun b hb => cnW_back hW7.1 hz7 hb) + have hsw : SW Z.C σ (scW sc 8#u64) := sw_hdr hSσ hsc (K := 8#u64) (by simp) (by simp) (by decide) + have hsub0 : ∀ b0, ((((b0 = tFaultA t ∨ b0 = tFw0A t) ∨ b0 = tFw1A t) ∨ b0 = tFw2A t) ∨ b0 = scW sc 8#u64) → + CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b0 := by + intro b0 hb0 + rcases hb0 with (((h | h) | h) | h) | h <;> subst h + · exact Or.inl (Or.inr ⟨t, ht, 3376, by omega, by omega, by omega, by omega, by omega, val_fault hta⟩) + · exact Or.inl (Or.inr ⟨t, ht, 3384, by omega, by omega, by omega, by omega, by omega, val_fw0 hta⟩) + · exact Or.inl (Or.inr ⟨t, ht, 3392, by omega, by omega, by omega, by omega, by omega, val_fw1 hta⟩) + · exact Or.inl (Or.inr ⟨t, ht, 3400, by omega, by omega, by omega, by omega, by omega, val_fw2 hta⟩) + · exact Or.inl (Or.inl (Or.inr (Or.inr hsw))) + have hz0 : SzKeepC Z.C σ0 σ := by + intro p _ y0 m hp + by_cases hps : p = sc + · subst hps; rw [hky] at hp; cases hp; exact ⟨yS, by rw [hσ0r]; exact rhoT_ksc⟩ + · have hps' : p.bv ≠ sc.bv := fun e0 => hps (P1.eq_of_bv e0) + have hpt : p.bv ≠ t.bv := fun e0 => by rw [e0, hkx] at hp; cases hp + exact ⟨y0, by rw [hk0 _ hpt hps']; exact hp⟩ + have hW05 : P1.WritesIn st s0 s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s0 σ0) := + (writesIn_rebase hWs5 hw0 hsub0).mono fun b0 hb0 => CnW.back (s := s0) (s' := s) hw0.1.symm hz0 hb0 + have hfs5 := footW_trans (footW_of (W := fun _ => False) hfoot7) hfoot + have hsubK : ∀ b0, ((((b0 = tFaultA t ∨ b0 = tFw0A t) ∨ b0 = tFw1A t) ∨ b0 = tFw2A t) ∨ b0 = scW sc 8#u64) → + P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s b0 ∨ False := by + intro b0 hb0 + rcases hb0 with (((h | h) | h) | h) | h <;> subst h + · exact Or.inl (Or.inl ⟨t, htH, 3376#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inl ⟨t, htH, 3384#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inl ⟨t, htH, 3392#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inl ⟨t, htH, 3400#u64, by simp, by simp, by simp, by simp, rfl⟩) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨sc, hsc1, scW8_inSC J.blk hsc1⟩)))) + have hfoot05 : P1.WritesIn st s0 s5 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s0) := + (writesIn_rebase hfs5 hw0 hsubK).mono fun b0 hb0 => hb0.elim (P1.KW.ksb hw0.1.symm) False.elim + have hI0 := J0.xtop.irelK J0.ks J0.buf + have hF : IFrame (L := L) Z.th s0 s5 := IRel.iframe_of hI0 hW05 fun b0 hb0 => CnW.oobj hI0.2.2.1 hb0 + have htDd := J0.not_dead_of_st hdT hkd0 (by rw [hstd]; simp [tsCode]) + have hRR : ∀ q' ∈ Z.R, ∀ xr, σ0.kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem s0 (rNextA q') = 0#u64 ∧ L.mem s0 (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB J0 q' hq'' xr d'.bv xd hx' + hxt' hkd0 (by rw [hstd]; rfl) + have hcD : ∀ q0 ∈ D, q0.bv ≠ t.bv := fun q0 hq0 e0 => htD (by rw [← P1.eq_of_bv e0]; exact hq0) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := J.xtop.irelK J.ks J.buf |>.1.toE.reply w hwR + rw [hkrσ] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hkq : ∀ q0 ∈ D, σ'.kheap q0.bv = σb.kheap q0.bv := fun q0 hq0 => ps_ko (hcD q0 hq0) (hwD q0 hq0) + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σb.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hwt h1 h2 + have J5 := J0.wake_postGR hIR hF hW05 hblk hfoot05 (DWFr.ofCFr Fr5) ps_skD + (ps_ksub hkta hkwa hwt) (ps_szKeep hkta hkwa hwt) (ps_ykeep hkta hkwa hwt) (ps_mcpKeep hkta hkwa hwt) + (ps_bufKeep hkta hkwa hwt) (fun d _ h => ps_recvB hkta hkwa hwt h) (ps_ksz hkta hkwa hwt) + (ps_kback hkta hkwa hwt) (ps_ipcFr hkta hkwa hwt) (ps_ck hkta hkwa hwt) (fun d _ h => ps_free hkta hkwa hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I5' B5' hAI5 hcur5 hrun5 (ps_own hkta hkwa hwt ((DWFr.ofCFr Fr5).own ⟨d', hdT, rfl⟩ J0.own)) + (ps_off hkta hkwa hwt ((DWFr.ofCFr Fr5).off J0.off)) (ps_nsc hkta hkwa hwt ((DWFr.ofCFr Fr5).nsc J0.nsc)) + (ps_isorted hkta hkwa hwt ((DWFr.ofCFr Fr5).isorted J0.isorted)) hRR htDd + have Fw : NatFoot (L := L) Z s0 σ0 s5 σ' := + NatFoot.ofWS hF.1 hW05 (fun _ h => Or.inl (Ipc.CnW.oobj J0.xtop.fin.1.1.1 h)) (fun _ h => J0.cnW_nv h) + (fun _ h => J0.cnW_nh h) ((natK_dwfr (DWFr.ofCFr Fr5)).trans (natK_pushSt hkwa hkta (Ne.symm hwt))) + -- the tail + have hk5 : σ'.kheap d'.bv = some (.tcb x5) := by + rw [ps_ko (Ne.symm htd') (Ne.symm hwd), hσb, rhoT_kh (Ne.symm htd') hdsc]; exact hkda + have hnr5 : d'.bv ∉ σ'.releaseQueue := by rw [ps_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → σ'.kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya' : σb.kheap p = some (.schedContext y n) := + (ho (fun _ h => by cases h) (fun _ h => by cases h)).1 hy + have hy0 := SK5.2.2.2 p y n hya' + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J0.inv.back _ _ _ hkd0 hp' + rw [hy0] at hky'; cases hky' + exact J0.bsa.tcb p y n d'.bv xd hy0 hyt hkd0 (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ σ'.idleThread := by + rw [(ps_skD (σ := σb) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xT)).idle, Fr5.sk.idle] + exact hid + have htc5 : d'.bv ≠ σ'.curThread := by + rw [(ps_skD (σ := σb) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xT)).ct, Fr5.sk.ct]; exact hdc + have hspec := sendIpc_faultPush_eq (don := false) hk hkd hde hstd true badge.bv g gr hg e2 hU hkt3 hkd3 hpush hkd4 + (by rw [hft]; simp) (t := t.bv) + have hCsc : ∀ q ∈ Z.C, ∃ y n, σ.kheap q.bv = some (.schedContext y n) := fun q hq => by + obtain ⟨Qx, Rx, hcx, hsx⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨y', n', hq', -⟩ := hsx.scs q hq + exact ⟨y', n', hq'⟩ + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7', a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + refine ⟨((), σ7), ?_, V0, Lst7, J7, NatFoot.trans hCsc (NatFoot.trans hCsc Fb Fw) F7.cast⟩ + rw [hspec, heq5, hpb] + show ((), σ7) ∈ (Endp.specWakeTailE d'.bv σ').1 + rw [h7]; exact Set.mem_singleton _ + +set_option maxHeartbeats 64000000 in +/-- (stable ghosts, `JStab`) **`Running` or `Restart` of an `Inactive` thread other than the current +one, then `handle_timeout` sending its timeout fault to a receiver waiting on +its timeout handler's endpoint, at `J`** (XTopJointSendBlockG's +`JAtZ.runTmoJ` for an endpoint with receivers: `set_thread_state`, the +handler cap, `set_fault`, then `JAtZ.sendTmoRecvIJSt` (the sender left +`Inactive`) or `JAtZ.sendTmoRecvPJSt` (the reply pushed); `hcr`: the current +thread runnable, so not the receiver). -/ +theorem JAtZB.runTmoRecvFoot (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hcr : ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ Fl4Spec.runnable xc.tcbState = true) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) + {p bd : Fl4Spec.Word} {r : Fl4Spec.CapRights} (hth : x.tcbTimeoutHandler = .endpointCap p bd r) + (hcb : ∀ xe, σ.kheap p = some (.endpoint xe) → ¬ Endp.CanBlock false xe) + (ts : Fl4Spec.ThreadState) (hrun : Fl4Spec.runnable ts = true) (hts : tsCode ts < 3) (v : U64) + (hv : v.val = tsCode ts) + (hB : sched.set_thread_state st s t v ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t v ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + ∀ b : U64, faults.handle_timeout st arI s6 a t (error.Fault.Timeout b) ⦃ res => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv)).1, + TmoFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ res.1 res.2 z.2 ⦄ ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have hta := hP0.e.tcb t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hP0.e.tcb + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rw [hst, hrs]; decide, by rw [hst, hrs]; decide⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + have hnq : NotQueued σ t.bv := J.inv.notQueued hk (by rw [hrs]; rfl) + obtain ⟨y0, n0, hky0, hyt⟩ := J.inv.back t.bv x sc.bv hk hxsc + rw [hky] at hky0; cases hky0 + have hidt : σ.idleThread ≠ t.bv := by + intro e + obtain ⟨y1, h1, -, h3, -⟩ := J.act.2 + rw [e, hk] at h1; cases h1; rw [hrs] at h3; cases h3 + -- the handler cap at the start, its endpoint + obtain ⟨⟨G0, hB0⟩, -⟩ := hI.2.1 + have hth0 : Z.th t = some .tcb := hB0.tcbT hk + obtain ⟨xh, cv, hkh, hcv, hca⟩ := hB0.2.1.1.timeoutCap hth0 + rw [hk] at hkh; cases hkh + have habs := hca.2 + rw [hth] at habs + obtain ⟨ptr, bdg, sd, rv, g, gr, rfl⟩ : ∃ ptr bdg sd rv g gr, cv = .Endpoint ptr bdg sd rv g gr := by + cases cv <;> simp [absCap] at habs; exact ⟨_, _, _, _, _, _, rfl⟩ + simp only [absCap, Fl4Spec.Cap.endpointCap.injEq] at habs + obtain ⟨hp, hbd, hr⟩ := habs + subst hp hbd hr + have hAS : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex 4) := ⟨t, rfl, Or.inr ⟨hth0, 4, by decide, rfl⟩⟩ + have hcv' : capAt (L.mem s) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 4)) = ok (.Endpoint ptr bdg sd rv g gr) := by + rw [tcbSlot4_eq]; exact hcv + have hpE : ptr ∈ Z.E := J.slotE hAS hcv' + obtain ⟨xe, hke, -⟩ := hI.1.ep ptr hpE + have hcbe : ¬ Endp.CanBlock false xe := hcb xe hke + have hpt : ptr.bv ≠ t.bv := fun e => by rw [e, hk] at hke; cases hke + have hps : ptr.bv ≠ sc.bv := fun e => by rw [e, hky] at hke; cases hke + -- the endpoint covered by the handler cap + have hHe : H ptr = some .ep := J.zH.2.1 ptr hpE + have hfit := J.kern.heap.fits ptr _ hHe + simp only [P1.HKind.bits] at hfit + have hcov := J.cover_cap (P1.WritesIn.refl s _) hAS.isSlot hcv' (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_ep ptr bdg sd rv g gr hfit) fun k hk' => by + rw [hHe] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + -- `set_thread_state` + refine WP.spec_mono (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by rw [hrs]; decide) ts hts v hv) (set_thread_state_sfoot hS ht _)) hB) ?_ + rintro s6 ⟨⟨⟨hR6, hS6r⟩, hw6⟩, hblk6, hfoot6⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + set x6 : Fl4Spec.Tcb := { x with tcbState := ts } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ q, q ≠ t.bv → σ6.kheap q = σ.kheap q := by + intro q hq'; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hq'] + have e61 : σ6 = σ1 := by + rw [hσ6]; unfold Notif.stsAct; rw [if_neg]; rintro ⟨h, -⟩; exact htc h + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := by + rw [hrs]; exact ⟨rfl, rfl, fun _ e => by cases e⟩ + have hK : ScTKw (fun q => q = t.bv) σ σ6 := by + refine ⟨fun q => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hq' : q = t.bv + · subst hq'; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, tsLow_free hts⟩⟩) + · rw [hkp q hq']; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + have hks6 : L.ksb s6 = L.ksb s := hW.1 + have hbuf6 : BufWordRel Z.T (L.mem s6) σ6 := J.sc_buf hW hK + have hI6 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 σ6 := by + have h := hX'.irelK (by rw [hks6]; exact J.ks) hbuf6 + have e : σ6.archState = σ.archState := hK.arch + rw [e] at h; exact h + refine ⟨hX', hI6, fun b => ?_⟩ + -- the FL4 step from the start to `s6` (`CnStep`) + have hC1 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ s6 σ6 := + ⟨hw6.mono fun b hb => Or.inl (Or.inl (KFoot.of_sfoot hb)), SzKeepC.of (SzKeep.of_tcb (p := t.bv) (x' := x6) + hkp hk6)⟩ + -- `handle_timeout`: the handler cap at `s6` + obtain ⟨⟨G6, hB6⟩, -⟩ := hI6.2.1 + obtain ⟨xh6, cv6, hkh6, hcv6, hca6⟩ := hB6.2.1.1.timeoutCap (hB6.tcbT hk6) + rw [hk6] at hkh6; cases hkh6 + have habs6 := hca6.2 + rw [show x6.tcbTimeoutHandler = x.tcbTimeoutHandler from rfl, hth] at habs6 + unfold faults.handle_timeout + rw [tcb_slot_timeout] + apply ok_bind_spec + rw [slot_cap_eq, hcv6] + apply ok_bind_spec + cases cv6 + case Endpoint ptr' bdg' sd' rv' g' gr' => + simp only [absCap, Fl4Spec.Cap.endpointCap.injEq] at habs6 + obtain ⟨hp', hbd', hr'⟩ := habs6 + obtain rfl : ptr = ptr' := (P1.eq_of_bv hp').symm + obtain rfl : bdg = bdg' := (P1.eq_of_bv hbd').symm + have hg : g' = g := by have := congrArg (fun R => decide (Fl4Spec.Rights.allowGrant ∈ R)) hr'; simpa [rights_grant] using this + have hgr : gr' = gr := by + have := congrArg (fun R => decide (Fl4Spec.Rights.allowGrantReply ∈ R)) hr'; simpa [rights_grantReply] using this + obtain rfl : g = g' := hg.symm + obtain rfl : gr = gr' := hgr.symm + refine WP.spec_bind (Pₘ := fun r : Bool × S × A => ∃ z ∈ (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) σ6).1, + TmoFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r.2.1 r.2.2 z.2) ?_ ?_ + swap + · rintro ⟨b0, s2, a2⟩ hr0 + exact (WP.spec_ok _).2 hr0 + unfold faults.send_fault_ipc + dsimp only + -- `set_fault` + set x2 : Fl4Spec.Tcb := { x6 with tcbFault := some (.timeout b.bv) } with hx2 + set σf := updK σ6 t.bv (.tcb x2) with hσf + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_fault_timeout_top hP0 hI6 ht hk6 b) + (set_fault_timeout_eq s6 t b)) (WP.spec_and (P1.faults.set_fault.blk (U := ∅) s6 t (error.Fault.Timeout b) + (t0 := t) hblk6 htH (fun _ h => by simp at h) ?hcb6) + (P1.faults.set_fault.kw (Cs := Cs) (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s6 t (error.Fault.Timeout b) hTH htH))) ?_ + case hcb6 => + have hS6 : SchedRel st Z.N Z.T Z.C Z.c s6 σ6 := hX6.2.2.1 + obtain ⟨Q, R0, hcq, hs⟩ := hS6.lists + obtain ⟨x7, hk7, hTR7⟩ := hI6.1.tcb t ht + rw [hk6] at hk7; cases hk7 + have hv7 : (L.mem s6 (tStA t)).val = tsCode ts := hTR7.st + have hrr : L.mem s6 (P1.stA t) = sched.TS_RUNNING ∨ L.mem s6 (P1.stA t) = sched.TS_RESTART := by + have hrr' : ts = .running ∨ ts = .restart := by + cases ts with + | running => exact Or.inl rfl + | restart => exact Or.inr rfl + | _ => simp [tsCode, Fl4Spec.runnable] at hts hrun + rcases hrr' with e | e + · left; apply UScalar.eq_of_val_eq; rw [P1.stA_eq, ← tStA_eq, hv7, e]; unfold sched.TS_RUNNING; rfl + · right; apply UScalar.eq_of_val_eq; rw [P1.stA_eq, ← tStA_eq, hv7, e]; unfold sched.TS_RESTART; rfl + have hrq6 : σ6.readyQueues = σ.readyQueues := by rw [hrec]; rfl + have hrl6 : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hac6 : σ6.schedulerAction = σ.schedulerAction := by rw [e61]; rfl + have hid6 : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + obtain ⟨xt6, hkt6, -, -, hsct6, -⟩ := hs.tcb t ht + rw [hk6] at hkt6; cases hkt6 + have hst6 : L.mem s6 (scA t) = sc := P1.eq_of_bv (Notif.optRef_some (hsct6.symm.trans hxsc)).2 + have h06 : L.mem s6 (scA t) ≠ 0#u64 := (Notif.optRef_some (hsct6.symm.trans hxsc)).1 + have hky6 : σ6.kheap sc.bv = some (.schedContext y n) := by + rw [hkp _ (fun e => by rw [e, hk] at hky; cases hky)]; exact hky + obtain ⟨y', n', hky', hy'⟩ := hs.scs sc hsc + rw [hky6] at hky'; cases hky' + have hmx : (L.rmax s6 (L.mem s6 (scA t))).val ≠ 0 := by rw [hst6]; exact hy'.rmax_ne hya + refine ⟨htH, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rcases hrr with h | h <;> rw [h] + · exact P1.not_blocked_running + · exact P1.not_blocked_restart + · rcases hrr with h | h <;> rw [h] + · exact P1.running_ne.1 + · exact P1.restart_ne.1 + · by_contra hq' + have hm := (hcq.queued t ht).1 hq' + exact hnq.1 0 _ (by rw [← hrq6, hs.rq _ (hcq.prio t ht), mem_map_bv]; exact hm) + · by_contra hq' + have hm := (hcq.released t ht).1 hq' + exact hnq.2 (by rw [← hrl6, hs.rel, mem_map_bv]; exact hm) + · refine actionW_ne_of hS6 ht ?_ + rw [hac6]; intro e + obtain ⟨x', -, -, -, h1, -, h3, -⟩ := schedulable_inv (J.act.1 _ e) + rw [hk] at h1; cases h1; rw [hrs] at h3; cases h3 + · intro h + have ei : L.idleW s6 = nodeW st s6 Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW nodeW; rw [hcq.cur] + exact hidt (by rw [← hid6, hs.idleTh, ← ei, h]) + · intro hbd + obtain ⟨⟨Q', R', h0⟩, hBd, hXi, hSc, hSg⟩ := hblk6 + obtain ⟨hiS, -, -⟩ := hXi.2.2.bound _ htH hbd + exact (hSc.act _ hiS).2 (fun e => hmx (by rw [e]; rfl)) + rintro s7 ⟨⟨⟨hsp7, hI7⟩, hs7⟩, ⟨hblk7, -, hcb7⟩, hfoot7⟩ + have hC2 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s6 σ6 s7 σf := by + refine ⟨?_, SzKeepC.of (SzKeep.of_tcb (p := t.bv) (x' := x2) (fun q hq' => by simp [hσf, updK, hq']) (by simp [hσf, updK]))⟩ + subst hs7 + have w := (((writesIn_wr (st := st) (s := s6) (v := fl4_abi.consts.fault.Timeout) (a_fault hta).1).trans + (writesIn_wr (v := b) (a_fw0 hta).1)).trans (writesIn_wr (v := 0#u64) (a_fw1 hta).1)).trans + (writesIn_wr (v := 0#u64) (a_fw2 hta).1) + refine w.mono fun w0 hw0 => ?_ + have k : ∃ K : Nat, 256 ≤ K ∧ K < 4096 ∧ K ≠ 3440 ∧ K ≠ 3472 ∧ ((K ≠ 3512 ∧ K ≠ 3528) ∧ K ≠ 3664 ∧ K ≠ 3456) ∧ w0.val = t.val + K := by + rcases hw0 with ((h | h) | h) | h <;> subst h + · exact ⟨3376, by omega, by omega, by omega, by omega, by omega, val_fault hta⟩ + · exact ⟨3384, by omega, by omega, by omega, by omega, by omega, val_fw0 hta⟩ + · exact ⟨3392, by omega, by omega, by omega, by omega, by omega, val_fw1 hta⟩ + · exact ⟨3400, by omega, by omega, by omega, by omega, by omega, val_fw2 hta⟩ + obtain ⟨K, k1, k2, k3, k4, k5, k6⟩ := k + exact Or.inl (Or.inr ⟨t, ht, K, k1, k2, k3, k4, k5, k6⟩) + have hW7 : P1.WritesIn st s s7 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := (hC1.trans hC2).1 + have hfoot7' := P1.Foot.trans hfoot6 hfoot7 + have eσf : σf = updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) }) := by + rw [hσf, e61, hσ1] + show ({ σ with kheap := Function.update (Function.update σ.kheap t.bv (some (.tcb x6))) t.bv (some (.tcb x2)) } : + Fl4Spec.State Unit) = _ + rw [Function.update_idem] + rfl + have hI7' : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s7 + (updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) })) := by + rw [← eσf]; exact hI7 + -- the receivers waiting on the endpoint + cases xe with + | idleEP => exact absurd trivial hcbe + | sendEP l => exact absurd rfl hcbe + | recvEP l => + obtain ⟨-, xs, hxs0, hq, hQ, -⟩ := hI.1.toE.at_ep hpE hke + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons] at hq + subst hq + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hth6 : x6.tcbTimeoutHandler = .endpointCap ptr.bv bdg.bv (rightsOf sd rv g gr) := hth + have fin : ∀ z : Unit × Fl4Spec.State Unit, z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr false t.bv ptr.bv + (updK σ t.bv (.tcb { x with tcbState := ts, tcbFault := some (.timeout b.bv) }))).1 → + z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv + (decide (Fl4Spec.Rights.allowGrant ∈ rightsOf sd rv g gr)) + (decide (Fl4Spec.Rights.allowGrantReply ∈ rightsOf sd rv g gr)) false t.bv ptr.bv + (updK σ6 t.bv (.tcb { x6 with tcbFault := some (.timeout b.bv) }))).1 := by + intro z hz + rw [rights_grant, rights_grantReply] + show z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr false t.bv ptr.bv σf).1 + rw [eσf]; exact hz + by_cases hpush : (g = true ∨ gr = true) ∧ ∀ xd, σ.kheap d'.bv = some (.tcb xd) → + ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none + · refine WP.spec_bind (J.sendTmoRecvPJFB hAT hmax htu hcr ht hk hrs htc hsc hxsc hky b hrun hI7' hblk7 hW7 + hfoot7' hcb7 hpE hke bdg g gr hpush.1 htD hya hpush.2) ?_ + rintro ⟨s', a'⟩ ⟨z, hz, hJ⟩ + exact (WP.spec_ok _).2 ⟨((), z.2), handleTimeout_mem hk6 hth6 _ (fin z hz), hJ⟩ + · have hno : (g = false ∧ gr = false) ∨ ∀ xd, σ.kheap d'.bv = some (.tcb xd) → + ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep = none := by + by_cases hgg : g = true ∨ gr = true + · right + intro xd hkd e' rep dd hst + by_contra hr + apply hpush + refine ⟨hgg, fun xd' hkd' e'' rep' dd' hst' => ?_⟩ + rw [hkd] at hkd'; cases hkd' + rw [hst] at hst'; cases hst'; exact hr + · left + have h1 : g ≠ true := fun h => hgg (Or.inl h) + have h2 : gr ≠ true := fun h => hgg (Or.inr h) + exact ⟨by simpa using h1, by simpa using h2⟩ + refine WP.spec_bind (J.sendTmoRecvIJFB hAT hmax htu hcr ht hk hrs htc hsc hxsc hky b hrun hI7' hblk7 hW7 + hfoot7' hcb7 hpE hke bdg g gr hno) ?_ + rintro ⟨s', a'⟩ ⟨z, hz, hJ⟩ + exact (WP.spec_ok _).2 ⟨((), z.2), handleTimeout_mem hk6 hth6 _ (fin z hz), hJ⟩ + all_goals simp [absCap] at habs6 + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/JAtZBUpdConsumed.lean b/proofs/Fl4Proofs/P3/JAtZBUpdConsumed.lean new file mode 100644 index 000000000..a906e62cc --- /dev/null +++ b/proofs/Fl4Proofs/P3/JAtZBUpdConsumed.lean @@ -0,0 +1,65 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2): **`update_consumed` of an SC at `JAtZB`, with the native footprint** (moved out of +P3/VmEntryS3XferAll unchanged so that native-only twins can use it without importing VM files: those shadow native names such as +`SW` and `nodeW`). +-/ +import Fl4Proofs.P3.XTopJointRegFrame +import Fl4Proofs.P3.IpcTmo +import Fl4Proofs.P3.ScJointConsumed +import Fl4Proofs.P3.NatFoot +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`update_consumed` of an SC at `JAtZB`, with the native footprint** (ScJointConsumed's `JAtZ.updConsumedJ` +through `JAtZB.reg_frameNC`). -/ +theorem JAtZB.updConsumedN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) {p t : U64} (hpC : p ∈ Z.C) + (ht : t ∈ Z.T) : + schedcontext.update_consumed st arI s a p ⦃ r => ∃ σ1 c, + Fl4Spec.schedContextUpdateConsumed (Z := Unit) p.bv σ = ({(c, σ1)}, False) ∧ + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D r.2 a σ1 ∧ + NatFoot (L := L) Z s σ r.2 σ1 ⦄ := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨z, n, hkz, hz⟩ := hs.scs p hpC + obtain ⟨x, hkx, -⟩ := hs.tcb t ht + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hpS : p ∈ P1.Heap.scs H := ⟨_, (hJ.sc p z n (J.c0 p hpC) hkz).2⟩ + have hal : P1.Aligned (scW p 8#u64) := (hz.hdr (K := 8#u64) (by simp) (by simp)).1 + refine WP.spec_mono (WP.spec_and (update_consumed_irel (ar := arI) J.ip s a σ hI hpC hkz hmax htu) + (P1.schedcontext.update_consumed.blk hAT s a p J.blk hpS)) ?_ + rintro ⟨cv, s1⟩ ⟨⟨hs1, -, he, hI1⟩, hP1⟩ + simp only at hs1 he hI1 hP1 + set w : Fl4Spec.SchedContext := { z with scConsumed := (0#u64 : U64).bv } with hwd + have hpt : p.bv ≠ t.bv := fun e => by rw [e, hkx] at hkz; cases hkz + have hk1 : (updSc σ p.bv w n).kheap t.bv = some (.tcb x) := by + simp only [updSc, updK, Function.update_of_ne hpt.symm]; exact hkx + have eS : updK (updSc σ p.bv w n) t.bv (.tcb x) = updSc σ p.bv w n := by + unfold updK; rw [← hk1, Function.update_eq_self] + have hA := QKY.upd2 (w := w) (x' := x) hkz hkx rfl rfl + have hyk := ykeep_upd2 (w := w) (x' := x) hkz hkx rfl rfl + rw [eS] at hA hyk + have hInv := J.inv.scFields (y' := w) hkz rfl rfl rfl rfl + rw [hs1] at hI1 hP1 ⊢ + have hw : P1.WritesIn st s (L.wr s (scW p 8#u64) 0#u64) (RegCW Z.T Z.C) := + (P1.writesIn_wr (st := st) (s := s) (v := 0#u64) hal).mono fun b hb => Or.inr ⟨p, hpC, hb⟩ + obtain ⟨V, J1, F1⟩ := J.reg_frameNC hI1 hw hA hyk hInv hP1.1 hP1.2.1 + exact ⟨_, _, he, V, J1, F1⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/P3JKtG.lean b/proofs/Fl4Proofs/P3/P3JKtG.lean index fd8deba03..a2e73647c 100644 --- a/proofs/Fl4Proofs/P3/P3JKtG.lean +++ b/proofs/Fl4Proofs/P3/P3JKtG.lean @@ -1151,12 +1151,12 @@ theorem kqe_fbCode {α : Type} {m : SEMonad Unit α} (h : KQE m) : KQE (fbCode ( macro_rules | `(tactic| kq_leaf) => `(tactic| with_reducible refine kqe_fbCode ?_) theorem kqe_fbDelete (sl : CslotPtr) : KQE (fbDelete (Z := Unit) sl) := by - unfold fbDelete; exact dke_handleE (kq_capDelete sl) fun _ => dke_throw _ + unfold fbDelete fbDeleteWith; exact dke_handleE (kq_capDelete sl) fun _ => dke_throw _ macro_rules | `(tactic| kq_leaf) => `(tactic| with_reducible exact kqe_fbDelete _) /-- fb3: one `X86PML4FrameBatch` entry. -/ theorem kq_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : KQE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; kq_auto0 + unfold fbEntry fbEntryWith fbDeferDelete; kq_auto0 /-- fb3: the batch (SpecInvFbD's `fk_performFrameBatch`). -/ theorem kq_performFrameBatch (b : Fl4FrameBatchInvocation) : KQ (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/P3JRrG.lean b/proofs/Fl4Proofs/P3/P3JRrG.lean index 84c6c9f5d..bf2b09d97 100644 --- a/proofs/Fl4Proofs/P3/P3JRrG.lean +++ b/proofs/Fl4Proofs/P3/P3JRrG.lean @@ -1132,12 +1132,12 @@ theorem rqe_fbCode {α : Type} {m : SEMonad Unit α} (h : RQE m) : RQE (fbCode ( macro_rules | `(tactic| zq_leaf) => `(tactic| with_reducible refine rqe_fbCode ?_) theorem rqe_fbDelete (sl : CslotPtr) : RQE (fbDelete (Z := Unit) sl) := by - unfold fbDelete; exact dke_handleE (rq_capDelete sl) fun _ => dke_throw _ + unfold fbDelete fbDeleteWith; exact dke_handleE (rq_capDelete sl) fun _ => dke_throw _ macro_rules | `(tactic| zq_leaf) => `(tactic| with_reducible exact rqe_fbDelete _) /-- fb3: one `X86PML4FrameBatch` entry. -/ theorem rq_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : RQE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; zq_auto0 + unfold fbEntry fbEntryWith fbDeferDelete; zq_auto0 /-- fb3: the batch (SpecInvFbD's `fk_performFrameBatch`). -/ theorem rq_performFrameBatch (b : Fl4FrameBatchInvocation) : RQ (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/P3JSendFootBase.lean b/proofs/Fl4Proofs/P3/P3JSendFootBase.lean new file mode 100644 index 000000000..48fc1e981 --- /dev/null +++ b/proofs/Fl4Proofs/P3/P3JSendFootBase.lean @@ -0,0 +1,66 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **the native pieces the footprint twins of the waiting-send tops share** +(a file of its own, importing nothing VM-side, so the twins' names resolve as in the originals): `natK_dnSt` (the +donation's spec step is a native step), `SendFootPost` (the post of a send to a waiting receiver with its footprint), +`JAtZB.natFoot_post` (the footprint of the post-base phases). No named premise. New file. +-/ +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.NatFoot +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical + +noncomputable section + +/-- **The donation's spec step (`dnSt`) is a native step** (the caller's TCB, the SC, the receiver's TCB, in place). -/ +theorem natK_dnSt {σ : Fl4Spec.State Unit} {t d p : Fl4Spec.ObjRef} {xt xd : Fl4Spec.Tcb} {y : Fl4Spec.SchedContext} + {n : Nat} (hkt : σ.kheap t = some (.tcb xt)) (hkd : σ.kheap d = some (.tcb xd)) + (hkp : σ.kheap p = some (.schedContext y n)) (htd : t ≠ d) (hpt : p ≠ t) (hpd : p ≠ d) : + NatK σ (dnSt σ t d p xt xd y n) := by + refine natK_kh (R := fun _ o o' => ZvChg o o') (fun _ _ _ h => h) fun q => ?_ + by_cases e1 : q = t + · subst e1; exact Or.inr ⟨_, _, hkt, dn_kt htd hpt, zvChg_tcb (fun h => h)⟩ + · by_cases e2 : q = d + · subst e2; exact Or.inr ⟨_, _, hkd, dn_kd, zvChg_tcb (fun h => h)⟩ + · by_cases e3 : q = p + · subst e3; exact Or.inr ⟨_, _, hkp, dn_kp hpd, zvChg_sc⟩ + · exact Or.inl (dn_ko e1 e2 e3) + + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} {Lst : U64 → List U64} + +/-- **The post of a send to a waiting receiver with its footprint** (Stage 3, the IPC-transfer bridge): the +transfer's base step `(y, σy)` (`XferBase`), the joint relation there, and at the end the joint relation with the +native footprint `NatFoot` from the base. -/ +def SendFootPost (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) (r : S × A) + (σ' : Fl4Spec.State Unit) : Prop := + ∃ (y : S) (σy : Fl4Spec.State Unit) (G' : MdbGhost), XferBase (L := L) Z s y σ σy G' ∧ Ipc.CnK σ σy ∧ + JAtZB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D y a σy ∧ + ∃ (V2 : XVs) (Ac2 : Set U64) (Lst2 : U64 → List U64) (D2 : Set U64), + JAtZB (L := L) arI AR (Z.mv G' V2) H Cs Ac2 Lst2 D2 r.1 r.2 σ' ∧ + NatFoot (L := L) (Z.mv G' Z.vs) y σy r.1 σ' + +open Fl4Proofs.P3.Ipc in +/-- **The footprint of the post-base phases of a send** from the base state `(y, σy)`: the buffer rebase's +footprint, then the `CnW` writes `su → s5` (and the spec's `NatK`). -/ +theorem JAtZB.natFoot_post {y su s5 : S} {a : A} {σy σu σ5 : Fl4Spec.State Unit} + (Jy : JAtZB (L := L) arI AR Z H Cs Ac Lst D y a σy) + (Fsu : NatFoot (L := L) Z y σy su σu) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} + (Ju : JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' su a σu) + (hWu : P1.WritesIn st su s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c su σu)) (hK : NatK σu σ5) : + NatFoot (L := L) Z y σy s5 σ5 := by + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + exact Jy.natTrans Fsu (NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj hIu.2.2.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) hK) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/P3JSendStF.lean b/proofs/Fl4Proofs/P3/P3JSendStF.lean new file mode 100644 index 000000000..706fecc8d --- /dev/null +++ b/proofs/Fl4Proofs/P3/P3JSendStF.lean @@ -0,0 +1,2769 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **the footprint twins of the stable waiting-send tops** +(P3/P3JSendStA, P3JSendStB, P3JSendStC; generated from them by the textual edits noted in each theorem). + +Each `…F` theorem is its `…St` original with the same hypotheses and proof, except: the transfer step is +`JAtZ.xferStepGJX` (its base step's data, `XferBase`); the buffer rebase is `JAtZB.bufRebaseX` (its `NatFoot`); the +tail is `JAtZB.sendTailJ` (its `NatFoot`); the post is `SendFootPost` (the base `(y, σy)`, its joint relation, and the +joint relation at the end with `NatFoot` from the base). `sendAfterRestartSt` and the `…St` statements are untouched. + +No named premise. New file. +-/ +import Fl4Proofs.P3.P3JSendStA +import Fl4Proofs.P3.P3JSendStB +import Fl4Proofs.P3.P3JSendStC +import Fl4Proofs.P3.P3JSendFootBase +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 35; stable ghosts, `JStab`) **`send_ipc` to a waiting receiver at `J`, without a call, a donation +(`can_donate` only toward a receiver with an SC, `hdon`: then neither side +donates), a fault or a cap transferred** (module docstring): FL4's dequeue, +transfer (`xferStepJ`, with or without a receive buffer), reply unlink and `Running` on `IRel` with the footprint `CnW` +(`ep_dequeue_c`, `do_ipc_transfer_regsJ` with `IpcBufCapOK` carried, +`reply_unlink_c`, `set_thread_state_c`), P1's `BlkOK` through the +receiver's hole (`ep_dequeue.inv`, `xfer_all`, `reply_unlink.inv`, +`sts_all`) and its footprint `KW`, the spec's frame `CFr` (the receiver +woken as a cancelled receive: its TCB from receive-blocked to `Running` +with a new user context, the endpoint's queue shrunk, its reply unbound), +`Inv` by `InvBut.ik`; `JAtZ` by ScSignalDonJ's `JAtZ.wake_postD`; then the +tail `JAtZ.sendTailJ`; the spec's outcome `sendIpc_recv_eqD`. -/ +theorem JAtZB.sendRecvJ0FB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrs : xt.tcbState = .restart) (hft : xt.tcbFault = none) (hct : σ.curThread = t.bv) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hdon : don = false ∨ ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none) : + endpoint.send_ipc st arI s a blk false badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ).1, + SendFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by rw [hrs]; decide + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.recvEP (d'.bv :: q)) d'.bv) hR1 hS1 hW1 F1 ht hdT htd hkt hkd hft + badge g h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksy, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hkdy : σy.kheap d'.bv = some (.tcb xd) := hncn hkd (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hkd1y : σ1y.kheap d'.bv = some (.tcb xd) := by simp [σ1y, hde, hkdy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1y] + have Fr1y : CFr e.bv d'.bv σy σ1y := CFr.upd hky (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1y : IK d'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hky (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : SKeep σy σ1y := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have hq1y : ∀ e'' ∈ Z.E, ∀ z, σ1y.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + refine hq1 e'' he'' z ?_ + by_cases hee : e''.bv = e.bv + · rw [hee] at hz ⊢; rw [hk1ey] at hz; rw [hk1e]; exact hz + · simp only [σ1y, Notif.updK_kheap, Function.update_of_ne hee] at hz + simp only [σ1, Notif.updK_kheap, Function.update_of_ne hee] + exact keepC_back hKy.1 hz (fun _ _ h0 => by cases h0) + set σ2 := Notif.updK (Endp.setUm σ1y um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1y] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1ey] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1y e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨⟨d', hdT, (a_reply hda).2⟩, ?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have Fr2 : CFr e.bv d'.bv σy σ2 := (Fr1y.trans (CFr.um _ _ σ1y um2)).trans (CFr.upd (σ := Endp.setUm σ1y um2) hkd1y + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans (IK.updSelf (σ := Endp.setUm σ1y um2) hkd1y hxsc)) + have SK2 : SKeep σy σ2 := SK1y.trans ((SKeep.um σ1y um2).trans (SKeep.updK (σ := Endp.setUm σ1y um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1y, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb y := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun s3 => ∃ σ3 x3 X3, + Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) rep σ2 = ({((), σ3)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s3) σ3 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s3 σ3 ∧ + σ3.kheap t.bv = some (.tcb xt) ∧ σ3.kheap d'.bv = some (.tcb x3) ∧ RbIn x3.tcbState ∧ + tsCode x3.tcbState ≠ 6 ∧ x3.tcbSchedContext = xd.tcbSchedContext ∧ + (∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue) ∧ + P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) ∧ + P1.WritesIn st y s3 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) y b ∨ RcvBufW xd b) ∧ + CFr e.bv d'.bv σy σ3 ∧ IK d'.bv σy σ3 ∧ SKeep σy σ3 ∧ σ3.curThread = σy.curThread ∧ + σ3.schedulerAction = σy.schedulerAction ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X3 s3 Q0 R0 ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s3 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s3 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s3 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 ∧ P1.ExcT X3 d' ∧ + (d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s3 (P1.stA d'))) ∧ + (∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d')) ?_ ?_ + · cases hrp : rep with + | none => + rw [hrp] at hrep + have h00 := optRef_none hrep + simp only [h00, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hrA : L.mem s2 (P1.replyA d') = 0#u64 := by + show L.mem s2 (tReplyA d') = 0#u64; rw [hrw]; exact h00 + exact ⟨σ2, x2, _, rfl, hR2, hS2', hkt2, hkd2, Or.inl ⟨_, _, _, hst2'⟩, + by rw [hst2']; simp [tsCode], hxsc, hq2, hW2, F2, Fr2, IK2, SK2, rfl, rfl, h2, hB2, hX2, hS2'', + hSg2, fun y hy => by simpa using hy, Or.inl (Or.inr rfl), hX2.noref0 hdta hrA⟩ + | some r => + rw [hrp] at hrep hrepR hst2' + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + simp [σ2, σ1y, hwe, hwd, hncn hkr (fun _ _ h0 => by cases h0)] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd, hrp]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_mono (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set σ3a := Notif.updK σ2 w.bv (.reply { xr with replyTcb := none }) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := by + show d'.bv ≠ σy.curThread; rw [hcty]; exact hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + refine ⟨_, x3, _, e3, hR3, hS3, ?_, ?_, Or.inr rfl, by simp [x3, tsCode], by simp [x3, hxsc], ?_, hW3', + footW_trans F2 (footW_of fu3), ?_, ?_, ?_, ?_, ?_, h3, + hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1), + hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr, hS2''.keepT hX2 k3, + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩, + fun y hy => ?_, Or.inr (by rw [e3']; exact P1.not_blocked_inactive), + (hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr).noref hdta (by rw [e3']; exact P1.inactive_ne.1) + (by rw [e3']; exact P1.inactive_ne.2)⟩ + · rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + · rw [stsAct_kheap]; simp + · intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + · exact (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + · have IK3a : IK d'.bv σy σ3a := IK2.trans (IK.updOther (o' := .reply { xr with replyTcb := none }) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σy (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + · exact ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply { xr with replyTcb := none }) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + · rw [e3s]; rfl + · rw [e3s]; rfl + · obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2 + rintro s3 ⟨σ3, x3, X3, hU, hR3, hS3, hkt3, hkd3, hrb3, hns3, hsc3, hq3, hW3, F3, Fr3, IK3, SK3, hct3, hact3, + h3, hB3, hX3, hS3', hSg3, hXc3, hx3, hnr3⟩ + have hxt3 := hR3.at_tcb ht hkt3 + refine WP.spec_bind (Pₘ := fun r : S × A => r = (s3, a)) ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro 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] + cases don with + | false => exact (WP.spec_ok _).2 rfl + | true => + rw [if_pos rfl] + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i2, hi2⟩ + have hne : i2 ≠ 0#u64 := by + obtain ⟨Q3, R3', -, hs3⟩ := hS3.1.1.2.1.lists + obtain ⟨x3', hk3', -, -, hsc', -⟩ := hs3.tcb d' hdT + rw [hkd3] at hk3'; cases hk3' + intro h0 + rw [hi2] at h0 + rw [h0, optRef_zero, hsc3] at hsc' + rcases hdon with h | h + · cases h + · exact h xd hkd hsc' + rw [if_neg hne] + exact (WP.spec_ok _).2 rfl + rintro _ rfl + try dsimp only + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 hdT hkd3 hq3 hns3 + .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR3 hS3).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s3 d' _ Q0 R0 h3 hB3 hX3 hS3' hSg3 hdH P1.not_blocked_running P1.running_ne hx3 hnr3 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 d' sched.TS_RUNNING h3 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σ5 := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσ5 + have hks3 : L.ksb s3 = L.ksb y := hW3.1 + have hIR := IpcRel.ofSRN hR5 hS5 + have hW5' : P1.WritesIn st y s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) := + hW3.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr hXc3)⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans F3 (footW_of fu5) + have Fr5 : CFr e.bv d'.bv σy σ5 := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σy (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σy σ5 := IK5a.trans (IK.sts _ _ _) + have SK5 : SKeep σy σ5 := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hnqD : NotQueued σy d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hkdy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hkdy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hrib : RelIfBound σ5 d'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd d'.bv xd hkdy (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σ5 := ((Jy.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne (by show d'.bv ≠ σ3.curThread; rw [hct3, hcty]; exact hdc)]; exact hact3 + have hAI5 : ActIdle σ5 := ⟨fun u hu => Fr5.sched (Jy.act.1 u (by rw [← act5]; exact hu)), Fr5.idleOK Jy.act.2⟩ + have hcur5 : σ5.schedulerAction = .chooseNewThread ∨ CurInv σ5 := + Jy.cur.imp (fun e0 => by rw [act5]; exact e0) Fr5.curInv + have hrun5 : CurRunM σ5 := Jy.curRun.of_cn Fr5.curRunW (Or.inr act5) + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) Jy hdT hdth hkdy + hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hdT hkdy (by rw [hstd]; simp [tsCode]) + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σy umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB Ju q' hq'' xr d'.bv xd hx' + hxt' hkdy (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have J5 := Ju.wake_postD (by rw [← hKy.2.1] at hIR; exact hIR) hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) I5 (Fr5.bsa Jy.bsa) hAI5 + hcur5 hrun5 hRR htD ⟨d', hdT, rfl⟩ + -- the tail + have hk5 : σ5.kheap d'.bv = some (.tcb x5) := by rw [hσ5, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ σ5.releaseQueue := by rw [SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → σ5.kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hy0 := SK5.2.2.2 p y n hy + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := Jy.inv.back _ _ _ hkdy hp' + rw [hy0] at hky'; cases hky' + exact Jy.bsa.tcb p y n d'.bv xd hy0 hyt hkdy (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ σ5.idleThread := by + rw [Fr5.sk.idle] + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := Jy.act.2 + rw [← h, hkdy] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have htc5 : d'.bv ≠ σ5.curThread := by rw [Fr5.sk.ct, hcty]; exact hdc + have hdon3 : don = false ∨ x3.tcbSchedContext ≠ none := hdon.imp_right fun h => by + rw [hsc3]; exact h xd hkd + have hspec := sendIpc_recv_eqD hk hkd hde hstd blk badge.bv g gr don e2 hU hkt3 hft hkd3 hdon3 (t := t.bv) + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have Fy5 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)) + exact ⟨((), σ7), by rw [hspec, h7]; exact Set.mem_singleton _, y, σy, Gy, hBase, hCnKy, Jy, _, _, _, _, J7, + Jy.natTrans Fy5 F7.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 35; stable ghosts, `JStab`) **A send to a waiting receiver without an SC at `J`, with the caller's SC +donated** (module docstring): as `JAtZ.sendRecvJ0St` up to the reply unlink, +then FL4's `donate` of the caller's (current) SC on `IRel` +(`donate_bound_refines`, `tk_lift`), P1's `donate.ipc` and `donate.kwAX` +(the receiver on the SC's core), the spec's donation `donate_cur_eq`, the +receiver `Running`, `JAtZ` by `JAtZ.wake_postG` at `dnSt` (`dn_swap`), the +tail `JAtZ.sendTailJ`; the spec's outcome `sendIpc_recv_eqDon`. The +caller is current, resumed (`hres`), not idle (`hti`) and in no ready queue +(`hnq`, JEntryAt's `ctnq`). -/ +theorem JAtZB.sendDonJFB (hAT : P0.ArchTotal arI) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrs : xt.tcbState = .restart) (hft : xt.tcbFault = none) (hct : σ.curThread = t.bv) + (hres : σ.schedulerAction = .resumeCurThread) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr : Bool) + (hdn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext = none) : + endpoint.send_ipc st arI s a blk false badge g gr true t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr true t.bv e.bv σ).1, + SendFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by rw [hrs]; decide + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.recvEP (d'.bv :: q)) d'.bv) hR1 hS1 hW1 F1 ht hdT htd hkt hkd hft + badge g h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, yb, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksy, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hkdy : σy.kheap d'.bv = some (.tcb xd) := hncn hkd (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hkd1y : σ1y.kheap d'.bv = some (.tcb xd) := by simp [σ1y, hde, hkdy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1y] + have Fr1y : CFr e.bv d'.bv σy σ1y := CFr.upd hky (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1y : IK d'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hky (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : SKeep σy σ1y := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have hq1y : ∀ e'' ∈ Z.E, ∀ z, σ1y.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + refine hq1 e'' he'' z ?_ + by_cases hee : e''.bv = e.bv + · rw [hee] at hz ⊢; rw [hk1ey] at hz; rw [hk1e]; exact hz + · simp only [σ1y, Notif.updK_kheap, Function.update_of_ne hee] at hz + simp only [σ1, Notif.updK_kheap, Function.update_of_ne hee] + exact keepC_back hKy.1 hz (fun _ _ h0 => by cases h0) + set σ2 := Notif.updK (Endp.setUm σ1y um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1y] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1ey] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1y e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨⟨d', hdT, (a_reply hda).2⟩, ?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have Fr2 : CFr e.bv d'.bv σy σ2 := (Fr1y.trans (CFr.um _ _ σ1y um2)).trans (CFr.upd (σ := Endp.setUm σ1y um2) hkd1y + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans (IK.updSelf (σ := Endp.setUm σ1y um2) hkd1y hxsc)) + have SK2 : SKeep σy σ2 := SK1y.trans ((SKeep.um σ1y um2).trans (SKeep.updK (σ := Endp.setUm σ1y um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1y, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb yb := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun s3 => ∃ σ3 x3 X3, + Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) rep σ2 = ({((), σ3)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s3) σ3 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s3 σ3 ∧ + σ3.kheap t.bv = some (.tcb xt) ∧ σ3.kheap d'.bv = some (.tcb x3) ∧ RbIn x3.tcbState ∧ + tsCode x3.tcbState ≠ 6 ∧ x3.tcbSchedContext = xd.tcbSchedContext ∧ + (∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue) ∧ + P1.WritesIn st yb s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) ∧ + P1.WritesIn st yb s3 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) yb b ∨ RcvBufW xd b) ∧ + CFr e.bv d'.bv σy σ3 ∧ IK d'.bv σy σ3 ∧ SKeep σy σ3 ∧ σ3.curThread = σy.curThread ∧ + σ3.schedulerAction = σy.schedulerAction ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X3 s3 Q0 R0 ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s3 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s3 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s3 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 ∧ P1.ExcT X3 d' ∧ + (d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s3 (P1.stA d'))) ∧ + (∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d') ∧ + (L.mem s3 (P1.replyA d') = 0#u64 ∨ (L.mem s3 (P1.stA d') ≠ sched.TS_BLOCKED_ON_REPLY ∧ + L.mem s3 (P1.stA d') ≠ sched.TS_BLOCKED_ON_RECV))) ?_ ?_ + · cases hrp : rep with + | none => + rw [hrp] at hrep + have h00 := optRef_none hrep + simp only [h00, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hrA : L.mem s2 (P1.replyA d') = 0#u64 := by + show L.mem s2 (tReplyA d') = 0#u64; rw [hrw]; exact h00 + exact ⟨σ2, x2, _, rfl, hR2, hS2', hkt2, hkd2, Or.inl ⟨_, _, _, hst2'⟩, + by rw [hst2']; simp [tsCode], hxsc, hq2, hW2, F2, Fr2, IK2, SK2, rfl, rfl, h2, hB2, hX2, hS2'', + hSg2, fun y hy => by simpa using hy, Or.inl (Or.inr rfl), hX2.noref0 hdta hrA, Or.inl hrA⟩ + | some r => + rw [hrp] at hrep hrepR hst2' + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + simp [σ2, σ1y, hwe, hwd, hncn hkr (fun _ _ h0 => by cases h0)] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd, hrp]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_mono (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set σ3a := Notif.updK σ2 w.bv (.reply { xr with replyTcb := none }) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := by + show d'.bv ≠ σy.curThread; rw [hcty]; exact hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + have hW3' : P1.WritesIn st yb s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + refine ⟨_, x3, _, e3, hR3, hS3, ?_, ?_, Or.inr rfl, by simp [x3, tsCode], by simp [x3, hxsc], ?_, hW3', + footW_trans F2 (footW_of fu3), ?_, ?_, ?_, ?_, ?_, h3, + hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1), + hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr, hS2''.keepT hX2 k3, + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩, + fun y hy => ?_, Or.inr (by rw [e3']; exact P1.not_blocked_inactive), + (hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr).noref hdta (by rw [e3']; exact P1.inactive_ne.1) + (by rw [e3']; exact P1.inactive_ne.2), Or.inr (by rw [e3']; exact P1.inactive_ne)⟩ + · rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + · rw [stsAct_kheap]; simp + · intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + · exact (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + · have IK3a : IK d'.bv σy σ3a := IK2.trans (IK.updOther (o' := .reply { xr with replyTcb := none }) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σy (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + · exact ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply { xr with replyTcb := none }) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + · rw [e3s]; rfl + · rw [e3s]; rfl + · obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2 + rintro s3 ⟨σ3, x3, X3, hU, hR3, hS3, hkt3, hkd3, hrb3, hns3, hsc3, hq3, hW3, F3, Fr3, IK3, SK3, hct3, hact3, + h3, hB3, hX3, hS3', hSg3, hXc3, hx3, hnr3, hra3⟩ + + have J0 := J + have hk0 := hk + have hkd0 := hkd + have hkt0 := hkt + have J := Jy + have hkd : σy.kheap d'.bv = some (.tcb xd) := hkdy + have hkt : σy.kheap t.bv = some (.tcb xt) := hkty + have hk : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hky + have hres : σy.schedulerAction = .resumeCurThread := hsay.trans hres + have hti : σy.curThread ≠ σy.idleThread := by rw [hcty, hidy]; exact hti + have hct : σy.curThread = t.bv := hcty.trans hct + have hnq : ∀ dm q', t.bv ∉ σy.readyQueues dm q' := by rw [hrqy]; exact hnq + have hdc : d'.bv ≠ σy.curThread := by rw [hcty]; exact hdc + have hdn : ∀ xd, σy.kheap d'.bv = some (.tcb xd) → xd.tcbSchedContext = none := fun xd h => hdn xd (by + rw [hkdy] at h; rw [hkd0]; exact h) + have hxt3 := hR3.at_tcb ht hkt3 + have hks3 : L.ksb s3 = L.ksb yb := hW3.1 + -- the caller's SC, the current SC + have hCI : CurInv σy := J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hnrT : t.bv ∉ σy.releaseQueue := by rw [← hct]; exact hCI.notRel + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc + cases hkc + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + obtain ⟨xt', hkt', hsct⟩ := hSM t ht + rw [hkt3] at hkt'; cases hkt' + set sc := L.mem s3 (scA t) with hscdef + have hsc0 : sc ≠ 0#u64 := fun h0 => by + have : xt.tcbSchedContext = none := by rw [hsct, h0, optRef_zero] + rw [hcs] at this; cases this + have hxtsc : xt.tcbSchedContext = some sc.bv := by rw [hsct, Notif.optRef_ne hsc0] + have hscp : sc.bv = σy.curSc := Option.some.inj (hxtsc.symm.trans hcs) + have hscC : sc ∈ Z.C := hs3.scMem t ht hsc0 + obtain ⟨y, n, hky3, -⟩ := hs3.scs sc hscC + have hky : σy.kheap sc.bv = some (.schedContext y n) := SK3.2.2.2 _ _ _ hky3 + have hyt : y.scTcb = some t.bv := by + obtain ⟨y', n', hk', hyt⟩ := J.inv.back t.bv xt sc.bv hkt hxtsc + rw [hky] at hk'; cases hk'; exact hyt + have hpt : sc.bv ≠ t.bv := kheap_ne hky hkt (fun _ h1 h2 => by subst h1; cases h2) + have hpd : sc.bv ≠ d'.bv := kheap_ne hky hkd (fun _ h1 h2 => by subst h1; cases h2) + -- the receiver has no SC + have hxd0 : xd.tcbSchedContext = none := hdn xd hkd + have hx30 : x3.tcbSchedContext = none := by rw [hsc3, hxd0] + obtain ⟨xd3, hkd3', hdsc⟩ := hSM d' hdT + rw [hkd3] at hkd3'; cases hkd3' + have hd0 : L.mem s3 (scA d') = 0#u64 := by + by_contra h + rw [Notif.optRef_ne h] at hdsc; rw [hx30] at hdsc; cases hdsc + -- in no queue + have hnqD : NotQueued σy d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + -- the scheduler words + have ha0 : L.actionW s3 = 0#u64 := by + have h := hSR3.act + rw [hact3, hres] at h + unfold P1.LawfulStore.actionW; rw [hcq3.cur]; exact h + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu h' => by + rw [ha0] at h'; exact (hcq3.tcbs u hu).ne0 (by rw [← h']; rfl) + have hcw3 : L.curW s3 = t := by + have h : (nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σy.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hscS : sc ∈ P1.Heap.scs H := (hX3.2.2.bound t htH hsc0).1 + have hsA : sc ∈ Ac := hB3.sact t htH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨ha0, hcw3⟩))))) hsc0 + have haff : L.mem s3 (P1.affA d') = L.mem s3 (P1.scCoreA sc) := by + rw [hcq3.aff d' hdT, Fl4Proofs.P3.Sched.scCoreA_eq, hSR3.core sc hscC] + have htcb3 : L.mem s3 (P1.scTcbA sc) = t := by + rw [scTcbA_eq]; exact word24_of hSR3 hscC hky3 hyt + -- the spec facts for the donation + have hrib3 : RelIfBound σ3 d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK3 + have I3 : Inv σ3 := ((J.inv.toBut d'.bv).ik hnqD IK3).1.inv fun _ _ _ => hrib3 + have hN3 : NSC Z.N Z.C σ3 := Fr3.nsc J.nsc + have hrelσ : Fl4Spec.scActive y = true → released σy y := fun ha => + J.inv.rd t.bv xt hkt (Or.inl (by rw [hrs]; rfl)) xt sc.bv y n hkt hxtsc hky ha hnrT + have hrel3 : inClass x3.tcbState → Fl4Spec.scActive y = true → released σ3 y := fun _ ha => by + have := hrelσ ha; unfold released at this ⊢; rw [SK3.2.1]; exact this + -- FL4: no fault, the donation + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ ∃ σ4, + Fl4Spec.schedContextDonate (Z := Unit) sc.bv d'.bv σ3 = ({((), σ4)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) σ4 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 σ4 ∧ + P1.WritesIn st s3 r.1 (fun b => SFoot st Z.T Z.c s3 b ∨ b = scW sc 24#u64) ∧ + P1.BlkOKX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac X3 r.1 ∧ + (∀ u ∈ P1.Heap.tcbs H, L.mem r.1 (P1.stA u) = L.mem s3 (P1.stA u)) ∧ + (∀ u ∈ P1.Heap.tcbs H, L.mem r.1 (P1.replyA u) = L.mem s3 (P1.replyA u)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1) ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro 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] + try rw [if_pos rfl] + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i2, hi2⟩ + rw [hi2, if_pos hd0] + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + subst hi3 + refine WP.spec_mono (WP.spec_and (WP.spec_and + (donate_bound_refines s3 a σ3 hSR3 hX3'.1.toN hN3 I3 hscC hsc0 hky3 ht hyt hkt3 hdT hkd3 hx30 htd (fun h => hrel3 h.cls)) + (P1.schedcontext.donate.ipc hAT s3 a sc d' t Q0 R0 h3 hB3 hX3 hS3' hSg3 htH rfl hsc0 + ⟨hqbT, hrbT, hna3 t ht⟩ (hiw3 t ht hit) (J.zH.1 d' hdT) hd0 hqbD hrbD (hna3 d' hdT) (Ne.symm htd) hsA + (hiw3 d' hdT hid))) + (P1.schedcontext.donate.kwAX (Es := P1.EH H) hAT s3 a sc d' t h3 hB3 hX3 hS3' hSg3 hscS (J.zH.1 d' hdT) + htH htcb3 ⟨hqbT, hrbT⟩)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨σ', hdon, ha, hS', hN', -, -, hW, -⟩, hP1⟩, hkw⟩ + simp only at ha hS' hN' hW hP1 hkw ⊢ + have hK : ScTKw (fun p => p = σ3.curThread) σ3 σ' := + ScTKw.ofFalse (NKT.of_eq (nkt_schedContextDonate sc.bv d'.bv) hdon) + have hI' := tk_lift hP0 hX3' (hW.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl h)) + (fun h => Or.inl (Or.inr (Or.inr (h ▸ sw_hdr hSR3 hscC (K := 24#u64) (by simp) (by simp) (by decide)))))) + hK hS' hN' + obtain ⟨hBX4, hst4, hrp4, -⟩ := hP1 + exact ⟨ha, σ', hdon, hI'.1.toE, IpcRel.toSRN hI'.1 hI'.2, hW, hBX4, hst4, hrp4, hkw haff⟩ + rintro ⟨s4, a4⟩ ⟨ha4, σ4, hdon4, hR4, hS4, hW4, hBX4, hst4, hrp4, fu4⟩ + simp only at ha4 hR4 hS4 hW4 hBX4 hst4 hrp4 fu4 ⊢ + subst ha4 + try dsimp only + have e4 := pair_eq (hdon4.symm.trans (donate_cur_eq hkt3 hkd3 hky3 hyt htd' hpt hpd + (by rw [hct3, hct]) hnqT3 (by rw [hact3, hres]))) + subst e4 + set x4 : Fl4Spec.Tcb := { x3 with tcbSchedContext := some sc.bv } with hx4 + have hkd4 : (dnSt σ3 t.bv d'.bv sc.bv xt x3 y n).kheap d'.bv = some (.tcb x4) := dn_kd + have hq4 : ∀ e'' ∈ Z.E, ∀ z, (dnSt σ3 t.bv d'.bv sc.bv xt x3 y n).kheap e''.bv = some (.endpoint z) → + d'.bv ∉ z.epQueue := fun e'' he'' z hz => hq3 e'' he'' z + ((dn_oth hkt3 hkd3 hky3 htd' hpt hpd (fun _ h => by cases h) (fun _ _ h => by cases h)).1 hz) + obtain ⟨⟨Q4, R4, h4⟩, hB4, hX4, hS4', hSg4⟩ := hBX4 + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hx4 : d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := by + rcases hx3 with h | h + · exact Or.inl h + · right; rw [hst4 d' hdH]; exact h + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := by + rcases hra3 with h | ⟨h1, h2⟩ + · exact hX4.noref0 hdta (by rw [hrp4 d' hdH]; exact h) + · exact hX4.noref hdta (by rw [hst4 d' hdH]; exact h1) (by rw [hst4 d' hdH]; exact h2) + have hks4 : L.ksb s4 = L.ksb yb := hW4.1.trans hks3 + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 hns3 + .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q4 R4 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK (dnSt σ3 t.bv d'.bv sc.bv xt x3 y n) d'.bv + (.tcb { x4 with tcbState := .running })) d'.bv = dnSt σa t.bv d'.bv sc.bv xt x5 y n := + dn_swap htd' hpd hdc3 + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 + (dnSt σa t.bv d'.bv sc.bv xt x5 y n) := by + rw [← heq5]; exact hIR0 + have hsw : SW Z.C σy (scW sc 24#u64) := sw_hdr J.xtop.fin.1.1.1 hscC (K := 24#u64) (by simp) (by simp) (by decide) + have hW4' : P1.WritesIn st yb s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW3.trans1 (hW4.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3).1 h))))) + (fun h => Or.inl (Or.inl (Or.inl (Or.inr (Or.inr (h ▸ hsw))))))) + have hW5' : P1.WritesIn st yb s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q4, R4, h5.congrX (P1.excT_congr hXc3)⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have Fr5 : CFr e.bv d'.bv σy σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σy (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σy σa := IK5a.trans (IK.sts _ _ _) + have SK5 : SKeep σy σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((J.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + -- the donation after the wake (`dn_swap`) + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hkda : σa.kheap d'.bv = some (.tcb x5) := by rw [hσa, stsAct_kheap]; simp [Notif.updK] + have hkpa : σa.kheap sc.bv = some (.schedContext y n) := by + rw [hσa, stsAct_kheap]; simp [Notif.updK, hpd, hky3] + have hnqTa : NotQueued σa t.bv := ⟨by rw [IK5.2.1]; exact hnq, by rw [IK5.2.2.1]; exact hnrT⟩ + have hnqDa : NotQueued σa d'.bv := ⟨by rw [IK5.2.1]; exact hnqD.1, by rw [IK5.2.2.1]; exact hnqD.2⟩ + have hrelA : Fl4Spec.scActive y = true → released σa y := fun ha => by + have := hrelσ ha; unfold released at this ⊢; rw [SK5.2.1]; exact this + set σ' := dnSt σa t.bv d'.bv sc.bv xt x5 y n with hσ' + have hcscA : sc.bv = σa.curSc := by rw [Fr5.sk.csc]; exact hscp + have hitA : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle]; exact Ne.symm hit + have hidA : σa.idleThread ≠ d'.bv := by rw [Fr5.sk.idle]; exact Ne.symm hid + have I5' : Inv σ' := dn_inv hkta hkda hkpa htd' hpt hpd I5 hyt hnqTa hnqDa hrelA + have B5' : BlockedScActive σ' := dn_bsa hkta hkda hkpa htd' hpt hpd rfl (Fr5.bsa J.bsa) + have hAI5 : ActIdle σ' := + ⟨fun u hu => (by rw [dn_act] at hu; cases hu), dn_idleOK hkta hkda hkpa htd' hpt hpd hitA hidA (Fr5.idleOK J.act.2)⟩ + have hrun5 : CurRunM σ' := Or.inl (by rw [dn_act]; intro h; cases h) + have hown5 : CurOwnW Z.T Z.C σ' := + dn_own hkta hkda hkpa htd' hpt hpd ⟨d', hdT, rfl⟩ hcscA (fun _ => by rw [SK5.1]; exact hnqD.2) + have hoff5 : OffOK σ' := dn_off hkta hkda hkpa htd' hpt hpd hcscA (Fr5.off J.off) + have hnsc5 : NSC Z.N Z.C σ' := dn_nsc hkta hkda hkpa htd' hpt hpd (Fr5.nsc J.nsc) + have hiso5 : IpcSorted σ' := dn_isorted hkta hkda hkpa htd' hpt hpd (Fr5.isorted J.isorted) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rw [hrs]; simp [tsCode]) + have hpD : ∀ q' ∈ D, q'.bv ≠ sc.bv := fun q' hq' e0 => by + have hqs : q' = sc := P1.eq_of_bv e0 + rw [hqs] at hq' + exact J.scOwned_not_dead ht hky hyt (J.jcov.dead sc hq') + have hkq : ∀ q ∈ D, σ'.kheap q.bv = σa.kheap q.bv := fun q hq => + dn_ko (hcD q hq) (htD q hq) (hpD q hq) + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) Jy hdT hdth hkd + hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σy umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB Ju q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x m, o ≠ .schedContext x m) → + (σ'.kheap q = some o ↔ σa.kheap q = some o) := fun h1 h2 => dn_oth hkta hkda hkpa htd' hpt hpd h1 h2 + obtain ⟨V', J5⟩ := Ju.wake_postGB (by rw [← hKy.2.1] at hIR; exact hIR) hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) dn_skD + (dn_ksub hkta hkda hkpa htd' hpt hpd) (dn_szKeep hkta hkda hkpa htd' hpt hpd) + (dn_ykeep hkta hkda hkpa htd' hpt hpd) (dn_mcpKeep hkta hkda hkpa htd' hpt hpd) + (dn_bufKeep hkta hkda hkpa htd' hpt hpd) (dn_khrb hkta hkda hkpa htd' hpt hpd) + (dn_ksz hkta hkda hkpa htd' hpt hpd) (dn_kback hkta hkda hkpa htd' hpt hpd) (dn_ipcFr hkta hkda hkpa htd' hpt hpd) + (dn_ck hkta hkda hkpa htd' hpt hpd) + (fun q x h => (ho (fun _ e => by cases e) (fun _ _ e => by cases e)).1 h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hkq + I5' B5' hAI5 (Or.inl dn_act) hrun5 hown5 hoff5 hnsc5 hiso5 hRR htD + -- the tail + have hk5 : σ'.kheap d'.bv = some (.tcb { x5 with tcbSchedContext := some sc.bv }) := dn_kd + have hnr5 : d'.bv ∉ σ'.releaseQueue := by rw [dn_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y' n', ({ x5 with tcbSchedContext := some sc.bv } : Fl4Spec.Tcb).tcbSchedContext = some p → + σ'.kheap p = some (.schedContext y' n') → Fl4Spec.scActive y' = true := by + intro p y' n' hp hy + cases hp + rw [dn_kp hpd] at hy; cases hy + have hne := J.curSc_ne_idle hCI hti + exact hCI.active hne y n (by rw [← hscp]; exact hky) + have hti5 : d'.bv ≠ σ'.idleThread := by + show d'.bv ≠ σa.idleThread; exact Ne.symm hidA + have htc5 : d'.bv ≠ σ'.curThread := by + show d'.bv ≠ σa.curThread; rw [Fr5.sk.ct]; exact hdc + have hspec := sendIpc_recv_eqDon hk0 hkd0 hde hstd blk badge.bv g gr e2 hU hkt3 hft hkd3 hx30 hxtsc hdon4 hkd4 + (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr true t.bv e.bv σ = + Endp.specWakeTailE d'.bv σ' := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have Fy5 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (NatK.trans (natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)) (natK_dnSt hkta hkda hkpa htd' hpt hpd)) + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, yb, σy, Gy, hBase, hCnKy, Jy, _, _, _, _, J7, + Jy.natTrans Fy5 F7.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 35; stable ghosts, `JStab`) **A call to a waiting receiver at `J` where the caller gets no reply** +(module docstring): the receiver has no reply object, or the caller neither +the grant nor the grant-reply right (`hno`); as `JAtZ.sendRecvJ0St` up to the +reply unlink, then the caller `Inactive` on `IRel` (its queue bits clear by +`JAtZ.curBits`: in no ready queue, `hnq`, l4v `ct_not_in_q`, and not in the +release queue, `CurInv.notRel`), the receiver `Running`, `JAtZ` by +`JAtZ.wake_postCI` (the spec's two state changes swapped, `ciSt_swap`), the +tail `JAtZ.sendTailJ`; the spec's outcome `sendIpc_callInactive_eq`. -/ +theorem JAtZB.sendCallIJFB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrs : xt.tcbState = .restart) (hft : xt.tcbFault = none) (hct : σ.curThread = t.bv) + (hres : σ.schedulerAction = .resumeCurThread) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hno : (g = false ∧ gr = false) ∨ + ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep = none) : + endpoint.send_ipc st arI s a blk true badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk true badge.bv g gr don t.bv e.bv σ).1, + SendFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by rw [hrs]; decide + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.recvEP (d'.bv :: q)) d'.bv) hR1 hS1 hW1 F1 ht hdT htd hkt hkd hft + badge g h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksy, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hkdy : σy.kheap d'.bv = some (.tcb xd) := hncn hkd (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hkd1y : σ1y.kheap d'.bv = some (.tcb xd) := by simp [σ1y, hde, hkdy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1y] + have Fr1y : CFr e.bv d'.bv σy σ1y := CFr.upd hky (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1y : IK d'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hky (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : SKeep σy σ1y := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have hq1y : ∀ e'' ∈ Z.E, ∀ z, σ1y.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + refine hq1 e'' he'' z ?_ + by_cases hee : e''.bv = e.bv + · rw [hee] at hz ⊢; rw [hk1ey] at hz; rw [hk1e]; exact hz + · simp only [σ1y, Notif.updK_kheap, Function.update_of_ne hee] at hz + simp only [σ1, Notif.updK_kheap, Function.update_of_ne hee] + exact keepC_back hKy.1 hz (fun _ _ h0 => by cases h0) + set σ2 := Notif.updK (Endp.setUm σ1y um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1y] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1ey] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1y e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨⟨d', hdT, (a_reply hda).2⟩, ?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have Fr2 : CFr e.bv d'.bv σy σ2 := (Fr1y.trans (CFr.um _ _ σ1y um2)).trans (CFr.upd (σ := Endp.setUm σ1y um2) hkd1y + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans (IK.updSelf (σ := Endp.setUm σ1y um2) hkd1y hxsc)) + have SK2 : SKeep σy σ2 := SK1y.trans ((SKeep.um σ1y um2).trans (SKeep.updK (σ := Endp.setUm σ1y um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1y, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb y := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] + refine WP.spec_bind (Pₘ := fun s3 => ∃ σ3 x3 X3, + Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) rep σ2 = ({((), σ3)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s3) σ3 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s3 σ3 ∧ + σ3.kheap t.bv = some (.tcb xt) ∧ σ3.kheap d'.bv = some (.tcb x3) ∧ RbIn x3.tcbState ∧ + tsCode x3.tcbState ≠ 6 ∧ x3.tcbSchedContext = xd.tcbSchedContext ∧ + (∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue) ∧ + P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) ∧ + P1.WritesIn st y s3 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) y b ∨ RcvBufW xd b) ∧ + CFr e.bv d'.bv σy σ3 ∧ IK d'.bv σy σ3 ∧ SKeep σy σ3 ∧ σ3.curThread = σy.curThread ∧ + σ3.schedulerAction = σy.schedulerAction ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X3 s3 Q0 R0 ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s3 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s3 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s3 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 ∧ P1.ExcT X3 d' ∧ + (d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s3 (P1.stA d'))) ∧ + (∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d')) ?_ ?_ + · cases hrp : rep with + | none => + rw [hrp] at hrep + have h00 := optRef_none hrep + simp only [h00, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hrA : L.mem s2 (P1.replyA d') = 0#u64 := by + show L.mem s2 (tReplyA d') = 0#u64; rw [hrw]; exact h00 + exact ⟨σ2, x2, _, rfl, hR2, hS2', hkt2, hkd2, Or.inl ⟨_, _, _, hst2'⟩, + by rw [hst2']; simp [tsCode], hxsc, hq2, hW2, F2, Fr2, IK2, SK2, rfl, rfl, h2, hB2, hX2, hS2'', + hSg2, fun y hy => by simpa using hy, Or.inl (Or.inr rfl), hX2.noref0 hdta hrA⟩ + | some r => + rw [hrp] at hrep hrepR hst2' + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + simp [σ2, σ1y, hwe, hwd, hncn hkr (fun _ _ h0 => by cases h0)] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd, hrp]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_mono (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set σ3a := Notif.updK σ2 w.bv (.reply { xr with replyTcb := none }) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := by + show d'.bv ≠ σy.curThread; rw [hcty]; exact hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + refine ⟨_, x3, _, e3, hR3, hS3, ?_, ?_, Or.inr rfl, by simp [x3, tsCode], by simp [x3, hxsc], ?_, hW3', + footW_trans F2 (footW_of fu3), ?_, ?_, ?_, ?_, ?_, h3, + hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1), + hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr, hS2''.keepT hX2 k3, + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩, + fun y hy => ?_, Or.inr (by rw [e3']; exact P1.not_blocked_inactive), + (hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr).noref hdta (by rw [e3']; exact P1.inactive_ne.1) + (by rw [e3']; exact P1.inactive_ne.2)⟩ + · rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + · rw [stsAct_kheap]; simp + · intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + · exact (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + · have IK3a : IK d'.bv σy σ3a := IK2.trans (IK.updOther (o' := .reply { xr with replyTcb := none }) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σy (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + · exact ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply { xr with replyTcb := none }) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + · rw [e3s]; rfl + · rw [e3s]; rfl + · obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2 + rintro s3 ⟨σ3, x3, X3, hU, hR3, hS3, hkt3, hkd3, hrb3, hns3, hsc3, hq3, hW3, F3, Fr3, IK3, SK3, hct3, hact3, + h3, hB3, hX3, hS3', hSg3, hXc3, hx3, hnr3⟩ + have hxt3 := hR3.at_tcb ht hkt3 + have hks3 : L.ksb s3 = L.ksb y := hW3.1 + -- the caller's facts for its `Inactive` + have hCI : CurInv σ := J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + obtain ⟨hqb, hrb'⟩ := J.curBits ht hnq hnrT + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = 2 := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st, hrs]; rfl + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (Or.inr (Or.inl hw3)) + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := (IpcRel.ofSRN hR3 hS3).2.2.1 + have hna3 : L.actionW s3 ≠ t := by + obtain ⟨Q, R', hcq, -⟩ := hSR3.lists + have h := hSR3.act + rw [hact3, hsay, hres] at h + have ha0 : L.actionW s3 = 0#u64 := by unfold P1.LawfulStore.actionW; rw [hcq.cur]; exact h + rw [ha0]; intro h'; exact (hcq.tcbs t ht).ne0 (by rw [← h']; rfl) + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + have hstsT := WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 ht hkt3 hqt3 + (by rw [hrs]; decide) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hSR3 ht sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 t _ Q0 R0 h3 hB3 hX3 hS3' hSg3 htH P1.not_blocked_inactive P1.inactive_ne + (Or.inr hnbt3) (hX3.noref (hTP t htH) hnrp3 (fun e0 => hnbt3 (Or.inr (Or.inl e0)))) + ⟨fun c hc' p hp hm => absurd hm (h0.notQ htH hqb hc' hp), fun c hc' hm => absurd hm (h0.notR htH hrb' hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 t sched.TS_INACTIVE h3 htH)) + -- FL4: no reply pushed, the caller `Inactive` + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ sched.set_thread_state st s3 t sched.TS_INACTIVE = ok r.1) + ?_ ?_ + · by_cases hi : (L.mem s (tReplyA d') != 0#u64) = true + · rw [if_pos hi] + have hgg : g = false ∧ gr = false := by + rcases hno with h | hn + · exact h + · exfalso + have hr0 := hn xd hkd _ _ _ hstd + rw [hr0] at hrep + have h00 := optRef_none hrep + rw [h00] at hi + simp at hi + obtain ⟨rfl, rfl⟩ := hgg + simp only [Bool.false_eq_true, ↓reduceIte] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + · rw [if_neg hi] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + rintro ⟨s4, a4⟩ ⟨ha4, hs4⟩ + simp only at ha4 hs4 + subst ha4 + try dsimp only + have hpost := hstsT + rw [hs4] at hpost + obtain ⟨⟨⟨hR4, hS4⟩, hW4⟩, ⟨h4, hB4, hX4, hS4', hSg4, -, k4⟩, fu4⟩ := (WP.spec_ok _).1 hpost + have hkd4 : (ciSt σ3 t.bv xt).kheap d'.bv = some (.tcb x3) := by rw [ciSt_kh (Ne.symm htd')]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, (ciSt σ3 t.bv xt).kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z (by rw [← ciSt_kh (ciSt_ne hz (fun _ h => by cases h))]; exact hz) + have hx4 : d' ∈ {y | y ∈ X3 ∧ y ≠ t} ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := by + rcases hx3 with h | h + · exact Or.inl ⟨h, Ne.symm htd⟩ + · right + have e0 : L.mem s4 (P1.stA d') = L.mem s3 (P1.stA d') := by + rw [P1.stA_eq] + exact k4.fld d' hdH _ (by simp) (by simp) (by simp) (by simp) (fun e1 => absurd e1 (Ne.symm htd)) + rw [e0]; exact h + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := fun r hr => by + rcases k4.rtcb r hr with e0 | ⟨-, e0⟩ + · rw [e0]; exact hnr3 r hr + · rw [e0]; intro h0'; exact hdta.ne0 (by rw [← h0']; rfl) + have hks4 : L.ksb s4 = L.ksb y := hW4.1.trans hks3 + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 hns3 + .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q0 R0 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3, hcty]; exact hdc + have heq5 : Notif.stsAct (Notif.updK (ciSt σ3 t.bv xt) d'.bv (.tcb x5)) d'.bv = ciSt σa t.bv xt := + ciSt_swap htd' hdc3 + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 (ciSt σa t.bv xt) := by + rw [← heq5]; exact hIR0 + have hW4' : P1.WritesIn st y s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) := + hW3.trans1 (hW4.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3).1 hb))))) + have hW5' : P1.WritesIn st y s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr (X := {y | y ∈ X3 ∧ y ≠ t}) (fun y hy => hXc3 y hy.1))⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have Fr5 : CFr e.bv d'.bv σy σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σy (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σy σa := IK5a.trans (IK.sts _ _ _) + have SK5 : SKeep σy σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hnqD : NotQueued σy d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hkdy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hkdy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd d'.bv xd hkdy (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((Jy.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σa.schedulerAction = σy.schedulerAction := by + rw [hσa, stsAct_ne (by show d'.bv ≠ σ3.curThread; exact hdc3)]; exact hact3 + -- the caller's `Inactive` after the wake (`ciSt_swap`) + have hnqT : ∀ dm p, t.bv ∉ σa.readyQueues dm p := by rw [IK5.2.1, hrqy]; exact hnq + have hnrT' : t.bv ∉ σa.releaseQueue := by rw [IK5.2.2.1, hrly]; exact hnrT + have I5' : Inv (ciSt σa t.bv xt) := ciSt_inv hkta I5 hnqT hnrT' + have B5' : BlockedScActive (ciSt σa t.bv xt) := ciSt_bsa (Fr5.bsa Jy.bsa) + have hcn : (ciSt σa t.bv xt).schedulerAction = .chooseNewThread := + ciSt_act (by rw [Fr5.sk.ct, hcty, hct]) (by rw [act5, hsay, hres]) + have hidT : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle, hidy, ← hct]; exact fun e0 => hti e0.symm + have hAI5 : ActIdle (ciSt σa t.bv xt) := + ⟨fun u hu => (by rw [hcn] at hu; cases hu), ciSt_idleOK hidT (Fr5.idleOK Jy.act.2)⟩ + have hrun5 : CurRunM (ciSt σa t.bv xt) := Or.inl (by rw [hcn]; intro h; cases h) + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rw [hrs]; simp [tsCode]) + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) Jy hdT hdth hkdy + hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hdT hkdy (by rw [hstd]; simp [tsCode]) + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σy umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB Ju q' hq'' xr d'.bv xd hx' + hxt' hkdy (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have J5 := Ju.wake_postCI hkta (by rw [← hKy.2.1] at hIR; exact hIR) hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) I5' B5' hAI5 + (Or.inl hcn) hrun5 hRR htD hcD ⟨d', hdT, rfl⟩ + -- the tail + have G := (ciSt_skD (σ := σa) (c := t.bv) (x := xt)) + have hk5 : (ciSt σa t.bv xt).kheap d'.bv = some (.tcb x5) := by + rw [ciSt_kh (Ne.symm htd'), hσa, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ (ciSt σa t.bv xt).releaseQueue := by rw [ciSt_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → (ciSt σa t.bv xt).kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya : σa.kheap p = some (.schedContext y n) := by + rw [← ciSt_kh (ciSt_ne hy (fun _ h => by cases h))]; exact hy + have hy0 := SK5.2.2.2 p y n hya + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := Jy.inv.back _ _ _ hkdy hp' + rw [hy0] at hky'; cases hky' + exact Jy.bsa.tcb p y n d'.bv xd hy0 hyt hkdy (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ (ciSt σa t.bv xt).idleThread := by + rw [G.idle, Fr5.sk.idle] + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := Jy.act.2 + rw [← h, hkdy] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have htc5 : d'.bv ≠ (ciSt σa t.bv xt).curThread := by rw [G.ct, Fr5.sk.ct, hcty]; exact hdc + have hcond : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨hg, hr⟩ + rcases hno with ⟨rfl, rfl⟩ | hn + · rcases hg with h | h <;> cases h + · exact hr (hn xd hkd _ _ _ hstd) + have hspec := sendIpc_callInactive_eq (don := don) hk hkd hde htd' hstd blk badge.bv g gr hcond e2 hU hkt3 hkd3 + (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk true badge.bv g gr don t.bv e.bv σ = + Endp.specWakeTailE d'.bv (ciSt σa t.bv xt) := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have Fy5 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (NatK.trans (natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)) (natK_ciSt hkta)) + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, y, σy, Gy, hBase, hCnKy, Jy, _, _, _, _, J7, + Jy.natTrans Fy5 F7.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 35; stable ghosts, `JStab`) **A call to a waiting receiver at `J` that pushes a reply and donates +the caller's SC** (module docstring): the caller has the grant or grant-reply +right (`hg`), the receiver a reply object (`hrepn`) and no SC (`hxdn`), the +call donates; as `JAtZ.sendCallPJSt` up to the reply unlink, then `reply_push` +(`reply_push_iC`): the link and block on `IRel` and P1 (`set_blocked_on_reply`), +`JAtZ` there by `JAtZ.wake_postGR` at `pushSt`, the donation by +XTopJointPushDon's `JAtZ.pushDonJ`; the receiver `Running` by XTopJointStsI's +`JAtZ.stsIJ` (released: the caller's SC, `Inv.rd`), the tail +`JAtZ.sendTailJ`; the spec's outcome `sendIpc_call_eq` with +`replyPush_don_eq`/`pd_eq`. -/ +theorem JAtZB.sendCallPDJFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrs : xt.tcbState = .restart) (hft : xt.tcbFault = none) (hct : σ.curThread = t.bv) + (hres : σ.schedulerAction = .resumeCurThread) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr : Bool) + (hg : g = true ∨ gr = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) + (hxdn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext = none) : + endpoint.send_ipc st arI s a blk true badge g gr true t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk true badge.bv g gr true t.bv e.bv σ).1, + SendFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by rw [hrs]; decide + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.recvEP (d'.bv :: q)) d'.bv) hR1 hS1 hW1 F1 ht hdT htd hkt hkd hft + badge g h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, yb, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksy, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hkdy : σy.kheap d'.bv = some (.tcb xd) := hncn hkd (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hkd1y : σ1y.kheap d'.bv = some (.tcb xd) := by simp [σ1y, hde, hkdy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1y] + have Fr1y : CFr e.bv d'.bv σy σ1y := CFr.upd hky (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1y : IK d'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hky (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : SKeep σy σ1y := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have hq1y : ∀ e'' ∈ Z.E, ∀ z, σ1y.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + refine hq1 e'' he'' z ?_ + by_cases hee : e''.bv = e.bv + · rw [hee] at hz ⊢; rw [hk1ey] at hz; rw [hk1e]; exact hz + · simp only [σ1y, Notif.updK_kheap, Function.update_of_ne hee] at hz + simp only [σ1, Notif.updK_kheap, Function.update_of_ne hee] + exact keepC_back hKy.1 hz (fun _ _ h0 => by cases h0) + set σ2 := Notif.updK (Endp.setUm σ1y um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1y] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1ey] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1y e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨⟨d', hdT, (a_reply hda).2⟩, ?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have Fr2 : CFr e.bv d'.bv σy σ2 := (Fr1y.trans (CFr.um _ _ σ1y um2)).trans (CFr.upd (σ := Endp.setUm σ1y um2) hkd1y + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans (IK.updSelf (σ := Endp.setUm σ1y um2) hkd1y hxsc)) + have SK2 : SKeep σy σ2 := SK1y.trans ((SKeep.um σ1y um2).trans (SKeep.updK (σ := Endp.setUm σ1y um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1y, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb yb := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkd _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + simp [σ2, σ1y, hwe, hwd, hncn hkr (fun _ _ h0 => by cases h0)] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + + have J0 := J + have hk0 := hk + have hkd0 := hkd + have hkt0 := hkt + have J := Jy + have hkd : σy.kheap d'.bv = some (.tcb xd) := hkdy + have hkt : σy.kheap t.bv = some (.tcb xt) := hkty + have hk : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hky + have hres : σy.schedulerAction = .resumeCurThread := hsay.trans hres + have hti : σy.curThread ≠ σy.idleThread := by rw [hcty, hidy]; exact hti + have hct : σy.curThread = t.bv := hcty.trans hct + have hnq : ∀ dm q', t.bv ∉ σy.readyQueues dm q' := by rw [hrqy]; exact hnq + have hdc : d'.bv ≠ σy.curThread := by rw [hcty]; exact hdc + have hrepn : ∀ xd, σy.kheap d'.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none := + fun xd h => hrepn xd (by rw [hkdy] at h; rw [hkd0]; exact h) + have hxdn : ∀ xd, σy.kheap d'.bv = some (.tcb xd) → xd.tcbSchedContext = none := + fun xd h => hxdn xd (by rw [hkdy] at h; rw [hkd0]; exact h) + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st yb s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + have Fr3 : CFr e.bv d'.bv σy σ3 := (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σy σ3 := by + have IK3a : IK d'.bv σy σ3a := IK2.trans (IK.updOther (o' := .reply xr3) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σy (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + have SK3 : SKeep σy σ3 := + ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans + (SKeep.stsAct _ _) + have hct3 : σ3.curThread = σy.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σy.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d' := + hX3.noref hdta (by rw [e3']; exact P1.inactive_ne.1) (by rw [e3']; exact P1.inactive_ne.2) + have F3 := footW_trans F2 (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hks3 : L.ksb s3 = L.ksb yb := hW3'.1 + -- the caller's and the receiver's words at `s3` + have hCI : CurInv σy := J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hnrT : t.bv ∉ σy.releaseQueue := by rw [← hct]; exact hCI.notRel + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = 2 := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st, hrs]; rfl + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (Or.inr (Or.inl hw3)) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σy d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have ha0 : L.actionW s3 = 0#u64 := by + have h := hSR3.act + rw [hact3, hres] at h + unfold P1.LawfulStore.actionW; rw [hcq3.cur]; exact h + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu h' => by + rw [ha0] at h'; exact (hcq3.tcbs u hu).ne0 (by rw [← h']; rfl) + have hcw3 : L.curW s3 = t := by + have h : (nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σy.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0 => + hB3.sact t htH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨ha0, hcw3⟩))))) h0 + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the caller's SC, donated + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + obtain ⟨y, n, hky0⟩ := hCI.sc + have hya : Fl4Spec.scActive y = true := hCI.active (J.curSc_ne_idle hCI hti) y n hky0 + obtain ⟨sc, hsC, esc⟩ := J.obj.sc _ y n hky0 + rw [← esc] at hky0 hcs + obtain ⟨y1, n1, hky1, hyt⟩ := J.inv.back _ _ _ hkt hcs + rw [hky0] at hky1; cases hky1 + have hrelσ : Fl4Spec.scActive y = true → released σy y := fun ha => + J.inv.rd t.bv xt hkt (Or.inl (by rw [hrs]; rfl)) xt sc.bv y n hkt hcs hky0 ha hnrT + have hse : sc.bv ≠ e.bv := kheap_ne hky0 hk (fun _ h1 h2 => by subst h1; cases h2) + have hsd : sc.bv ≠ d'.bv := kheap_ne hky0 hkd (fun _ h1 h2 => by subst h1; cases h2) + have hsw : sc.bv ≠ w.bv := kheap_ne hky0 (hncn hkr (fun _ _ h0 => by cases h0)) (fun _ h1 h2 => by subst h1; cases h2) + have hst : sc.bv ≠ t.bv := kheap_ne hky0 hkt (fun _ h1 h2 => by subst h1; cases h2) + have hky2 : σ2.kheap sc.bv = some (.schedContext y n) := by simp [σ2, σ1y, hsd, hse, hky0] + have hky3 : σ3.kheap sc.bv = some (.schedContext y n) := by rw [stsAct_kheap]; simp [σ3a, hsd, hsw, hky2] + have hsw3 : L.mem s3 (P1.scA t) = sc := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs3.tcb t ht + rw [hkt3] at hk'; cases hk' + exact optRef_some_eq (hscw.symm.trans hcs) + have hxdn3 : x3.tcbSchedContext = none := by rw [hsc3]; exact hxdn xd hkd + -- the J state after the block + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rw [hrs]; simp [tsCode]) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J0.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := hR.reply w hwR + rw [hkr] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hRR : ∀ q' ∈ Z.R, ∀ xr, σy.kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem yb (rNextA q') = 0#u64 ∧ L.mem yb (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB J q' hq'' xr d'.bv xd hx' hxt' hkd + (by rw [hstd]; rfl) + have hrib3 : RelIfBound σ3 d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK3 + have I3 : Inv σ3 := ((J.inv.toBut d'.bv).ik hnqD IK3).1.inv fun _ _ _ => hrib3 + have hscA3 : ∀ p y n, σ3.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + have hp0 : σy.kheap p = some (.schedContext y n) := SK3.2.2.2 _ _ _ hp + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp0 + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp0).2⟩ + have hS := J.xtop.fin.1.1.1 + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp0 hyt + rw [hkt] at hku; cases hku + have e1 : σy.curSc = q.bv := by rw [← esc]; exact Option.some.inj (hcs.symm.trans hxs) + rw [← e1] at hp0 + exact hCI.active (J.curSc_ne_idle hCI hti) y n hp0 + set xtP : Fl4Spec.Tcb := { xt with tcbState := .blockedOnReply w.bv } with hxtP + set xwP : Fl4Spec.Reply := { xr3 with replyTcb := some t.bv } with hxwP + set σP := pushSt σ3 t.bv w.bv xr3 xt with hσP + set σD := pdSt σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n with hσD + have hktP : σP.kheap t.bv = some (.tcb xtP) := ps_kt + have hkwP : σP.kheap w.bv = some (.reply xwP) := ps_kw hwt + have hkdP : σP.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hkyP : σP.kheap sc.bv = some (.schedContext y n) := by rw [ps_ko hst hsw]; exact hky3 + have hcnP : σP.schedulerAction = .chooseNewThread := ps_act (by rw [hct3, hct]) (by rw [hact3, hres]) + have hnsw : ∀ u, σP.schedulerAction ≠ .switchThread u := fun u e0 => by rw [hcnP] at e0; cases e0 + have hctP : σP.curThread = t.bv := + (ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).ct.trans (hct3.trans hct) + have hcscP : σP.curSc = sc.bv := + (ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).csc.trans (Fr3.sk.csc.trans esc.symm) + have hnqtP : NotQueued σP t.bv := ⟨by rw [ps_rq]; exact hnqT3.1, by rw [ps_rel]; exact hnqT3.2⟩ + have hnqdP : NotQueued σP d'.bv := ⟨by rw [ps_rq]; exact hnqD3.1, by rw [ps_rel]; exact hnqD3.2⟩ + have hhP := J.stackHead hsC hky0 (hRR w hwR xr (hncn hkr (fun _ _ h0 => by cases h0)) hb').1 + have hhP' : ∀ h l, y.scReplies = h :: l → h ≠ sc.bv ∧ h ≠ w.bv ∧ ∃ x, σP.kheap h = some (.reply x) := by + intro h l hr + obtain ⟨h1, h2, x, hx⟩ := hhP h l hr + refine ⟨h1, h2, x, ?_⟩ + have ht' : h ≠ t.bv := fun e0 => by rw [e0, hkt] at hx; cases hx + have hd' : h ≠ d'.bv := fun e0 => by rw [e0, hkd] at hx; cases hx + have he' : h ≠ e.bv := fun e0 => by rw [e0, hk] at hx; cases hx + rw [ps_ko ht' h2, stsAct_kheap] + simp [σ3a, hd', h2, σ2, σ1y, he', hx] + have hpdE := pd_eq hktP hkdP hkyP hkwP (Ne.symm hsw) hhP' hyt htd' hst hsd hwt hwd + hctP.symm hnqtP hnsw + have hpushE : Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σD)}, False) := by + rw [replyPush_don_eq hkt3 hkd3 hkr3 hwt hcs hxdn3]; exact hpdE + have hns3 : tsCode xt.tcbState ≠ 6 := by rw [hrs]; decide + -- the push: link and block (`J` again), then the donation at `J` + have hPush : ipc.reply_push st arI s3 a t d' w true ⦃ r => r.2 = a ∧ ∃ σ', + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σ')}, False) ∧ + (σ' = σD → ∃ (V : XVs) (Lst' : U64 → List U64), JAtZB (L := L) arI AR ((Z.mv Gy Z.vs).mv (Z.mv Gy Z.vs).G V) H Cs Ac Lst' D r.1 a σ' ∧ + NatFoot (L := L) (Z.mv Gy Z.vs) yb σy r.1 σ') ⦄ := by + refine reply_push_iC (don := true) (P := fun s' σ' => σ' = σD → ∃ (V : XVs) (Lst' : U64 → List U64), + JAtZB (L := L) arI AR ((Z.mv Gy Z.vs).mv (Z.mv Gy Z.vs).G V) H Cs Ac Lst' D s' a σ' ∧ + NatFoot (L := L) (Z.mv Gy Z.vs) yb σy s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) (fun s' _ _ e0 => ?_) ?_ + · exfalso + have h1 : σD.kheap t.bv = some (.tcb xtP) := by rw [← e0]; exact hktP + have h2 : σD.kheap t.bv = some (.tcb { xtP with tcbSchedContext := none }) := + @dn_kt (bdSt σP sc.bv w.bv y n xwP) t.bv d'.bv sc.bv xtP x3 { y with scReplies := w.bv :: y.scReplies } n + htd' hst + rw [h2] at h1 + have h3 : ({ xtP with tcbSchedContext := none } : Fl4Spec.Tcb) = xtP := by + injection h1 with h1; injection h1 + have h4 : (none : Option Fl4Spec.ObjRef) = some sc.bv := (congrArg Fl4Spec.Tcb.tcbSchedContext h3).trans hcs + cases h4 + · intro _ s4 hsb hR4 hS4 _ _ _ _ + rw [hsw3] + have hsb' : sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok s4 := hsb + -- FL4's words and P1 at the block + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb', WP.spec_ok] at h; exact h + have hW4' : P1.WritesIn st yb s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3 (SzKeepC.of Fr3.szKeep) hb)) + have hb4 := P1.sched.set_blocked_on_reply.ipc s3 t w Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 hcA + rw [hsb', WP.spec_ok] at hb4 + obtain ⟨h4, hB4, hX4, hS4', hSg4, -, -, -, -, -, -⟩ := hb4 + have hO3 := hX3.1 + obtain ⟨hrΩ3, -⟩ := P1.objs_omega hO3 hSg3.sub + have f1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) + s3 (L.wr s3 (P1.rTcbA w) t) := + P1.foot_wr (h3.omega _ (hrΩ3 w hwH).1).1 (P1.kw_sg (hO3.tcbW hwH)) + have q1 := (P1.omega_wr (v := t) h3 (hrΩ3 w hwH).1).1 + have f4 := P1.sched.set_blocked_on_reply.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) (Es := P1.EH H) _ t w q1 htH + rw [hsb', WP.spec_ok] at f4 + have hfoot4 : P1.WritesIn st yb s4 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) yb b ∨ RcvBufW xd b) := + footW_trans (footW_trans F3 (footW_of f1)) (footW_of f4) + have hIR4 := IpcRel.ofSRN hR4 hS4 + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) Jy hdT hdth hkd hW4' hfoot4 + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRRu : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σy umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB Ju q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF4 : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hblk4 : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s4 := ⟨⟨Q0, R0, h4⟩, hB4, hX4, hS4', hSg4⟩ + have I4 : Inv σP := ps_inv hkt3 hkr3 hwt I3 hnqT3.1 hnqT3.2 + have B4 : BlockedScActive σP := ps_bsa hkt3 hkr3 hwt hscA3 (Fr3.bsa J.bsa) + have hidT3 : σ3.idleThread ≠ t.bv := by rw [Fr3.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI4 : ActIdle σP := + ⟨fun u hu => (by rw [hcnP] at hu; cases hu), ps_idleOK hkt3 hkr3 hwt hidT3 (Fr3.idleOK J.act.2)⟩ + have hkq : ∀ q ∈ D, σP.kheap q.bv = σ3.kheap q.bv := fun q hq => ps_ko (hcD q hq) (hwD q hq) + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σP.kheap q = some o ↔ σ3.kheap q = some o) := fun h1 h2 => ps_oth hkt3 hkr3 hwt h1 h2 + have J4 := Ju.wake_postGR (by rw [← hKy.2.1] at hIR4; exact hIR4) hF4 hWu hblk4 hFu (DWFr.ofUm (DWFr.ofCFr Fr3) umu) ps_skD + (ps_ksub hkt3 hkr3 hwt) (ps_szKeep hkt3 hkr3 hwt) (ps_ykeep hkt3 hkr3 hwt) (ps_mcpKeep hkt3 hkr3 hwt) + (ps_bufKeep hkt3 hkr3 hwt) (fun d _ h => ps_recvB hkt3 hkr3 hwt h) (ps_ksz hkt3 hkr3 hwt) + (ps_kback hkt3 hkr3 hwt) (ps_ipcFr hkt3 hkr3 hwt) (ps_ck hkt3 hkr3 hwt) (fun d _ h => ps_free hkt3 hkr3 hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I4 B4 hAI4 (Or.inl hcnP) (Or.inl (by rw [hcnP]; intro h; cases h)) + (ps_own hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).own ⟨d', hdT, rfl⟩ J.own)) + (ps_off hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).off J.off)) (ps_nsc hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).nsc J.nsc)) + (ps_isorted hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).isorted J.isorted)) hRRu htD + -- the reply's links at the block + obtain ⟨hdet, hn0, hp0⟩ := hRRu w hwR xr (hncn hkr (fun _ _ h0 => by cases h0)) hb' + obtain ⟨-, -, -, -, k4⟩ := Ju.cnW_keeps hWu + refine WP.spec_mono (J4.pushDonJ hAT ht hdT hwR hsC htd hktP hkdP hkwP hkyP rfl hcs hyt hxdn3 rfl rfl + hctP hcscP hcnP hnqtP hnqdP hya hdet (by rw [(k4 w hwR).2]; exact hn0) (by rw [(k4 w hwR).1]; exact hp0) + htD) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, hsp5, Lst5, J5, hwR5, hK5⟩ + simp only at ha5 + subst a5 + have Fy4 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (NatK.trans (natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr3) umu)) + (natK_pushSt hkr3 hkt3 (Ne.symm hwt))) + have F45 := NatFoot.ofW J4.xtop hwR5 (fun b hb => RmW.oobj J4.xtop.fin.1.1.1 hb) + (fun b hb => J4.rmW_nv hb) (fun b hb => J4.rmW_nh hb) (natK_tk hK5) + exact ⟨rfl, _, hsp5, fun _ => ⟨(Z.mv Gy Z.vs).vs, Lst5, J5, Jy.natTrans Fy4 F45.cast⟩⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ ∃ σ', + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σ')}, False) ∧ + (σ' = σD → ∃ (V : XVs) (Lst' : U64 → List U64), JAtZB (L := L) arI AR ((Z.mv Gy Z.vs).mv (Z.mv Gy Z.vs).G V) H Cs Ac Lst' D r.1 a σ' ∧ + NatFoot (L := L) (Z.mv Gy Z.vs) yb σy r.1 σ')) + ?_ ?_ + · rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s5, a5⟩ ⟨ha5, σ5', hpush, hPσ⟩ + simp only at ha5 hpush hPσ ⊢ + subst a5 + try dsimp only + have e5 : σ5' = σD := by + have h := hpush.symm.trans hpushE + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + obtain ⟨V5, Lst5, J5, Fpush⟩ := hPσ e5 + subst e5 + -- the receiver runs (`Inactive` with the donated SC) + set x4 : Fl4Spec.Tcb := { x3 with tcbSchedContext := some sc.bv } with hx4 + have hk5 : σD.kheap d'.bv = some (.tcb x4) := @pd_kd σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n + have hst5 : L.mem s5 (P1.stA d') = sched.TS_INACTIVE := by + have h := ((J5.xtop.irelK J5.ks J5.buf).1.toN.at_tcb hdT hk5).st + rw [stA_tStA, P1.ts_inactive] + exact UScalar.eq_of_val_eq (by rw [h]; rfl) + have htmD : σD.curTime = σy.curTime := (@pd_tm σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans + ((ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).tm.trans SK3.2.1) + have hkpD : σD.kheap sc.bv = some (.schedContext { { y with scReplies := w.bv :: y.scReplies } with + scTcb := some d'.bv } n) := + @dn_kp (bdSt σP sc.bv w.bv y n xwP) t.bv d'.bv sc.bv xtP x3 { y with scReplies := w.bv :: y.scReplies } n hsd + have hrelD : RelIfBound σD d'.bv := by + intro x' scp y' n' hx' hsc' hky' ha' _ + rw [hk5] at hx'; cases hx' + simp only [x4, Option.some.injEq] at hsc' + subst hsc' + rw [hkpD] at hky' + cases hky' + have := hrelσ hya + unfold released at this ⊢ + rw [htmD]; exact this + refine WP.spec_bind (J5.stsIJ hdT (fun h => htD d' h rfl) hk5 rfl .running (by simp [tsCode]) sched.TS_RUNNING + (by unfold sched.TS_RUNNING; rfl) (fun _ => hrelD) + (P1.sched.set_thread_state.running_blkI (Es := P1.EH H) s5 d' J5.blk hdH hst5)) ?_ + rintro s6 ⟨Lst6, J6, hW6⟩ + set x6 : Fl4Spec.Tcb := { x4 with tcbState := .running } with hx6 + have hdcD : d'.bv ≠ σD.curThread := by + rw [show σD.curThread = t.bv from (@pd_ct σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans hctP] + exact Ne.symm htd' + have e6 : stsAct (updK σD d'.bv (.tcb x6)) d'.bv = updK σD d'.bv (.tcb x6) := stsAct_ne hdcD + have hk6 : (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).kheap d'.bv = some (.tcb x6) := by + rw [e6]; simp [updK] + have hnr6 : d'.bv ∉ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).releaseQueue := by + rw [e6] + show d'.bv ∉ σD.releaseQueue + rw [show σD.releaseQueue = σy.releaseQueue from (@pd_rel σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans + ((@ps_rel σ3 t.bv w.bv xr3 xt).trans SK3.1)] + exact hnqD.2 + have hact6 : ∀ p y' n', x6.tcbSchedContext = some p → + (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).kheap p = some (.schedContext y' n') → Fl4Spec.scActive y' = true := by + intro p y' n' hp hy' + simp only [x6, x4, Option.some.injEq] at hp + subst hp + rw [e6] at hy' + simp only [updK_kheap, Function.update_of_ne hsd] at hy' + rw [hkpD] at hy' + cases hy' + exact hya + have hti6 : d'.bv ≠ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).idleThread := by + rw [e6] + show d'.bv ≠ σD.idleThread + rw [show σD.idleThread = σy.idleThread from (@pd_idle σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans + ((ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).idle.trans Fr3.sk.idle)] + exact hid + have htc6 : d'.bv ≠ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).curThread := by + rw [e6]; exact hdcD + have hspec := sendIpc_call_eq (don := true) hk0 hkd0 hde hstd blk badge.bv g gr hg e2 hU hkt3 hkd3 hpushE hk5 + (t := t.bv) + refine WP.spec_mono (J6.sendTailJ hdT hk6 rfl hnr6 hact6 hti6 htc6) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have F56 := NatFoot.ofT J5.xtop hW6 (fun b hb => J5.tfoot_nh hb) + (natK_tcbW (t := d'.bv) (σ' := stsAct (updK σD d'.bv (.tcb x6)) d'.bv) (y := x6) hk5 (stsAct_kheap _ _) (fun e => by cases e)) + exact ⟨((), σ7), by rw [hspec, h7]; exact Set.mem_singleton _, yb, σy, Gy, hBase, hCnKy, Jy, _, _, _, _, J7, + Jy.natTrans (Jy.natTrans Fpush F56.cast) F7.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 35; stable ghosts, `JStab`) **A call to a waiting receiver at `J` that pushes a reply without a +donation** (module docstring): the caller has the grant or grant-reply right +(`hg`), the receiver a reply object (`hrepn`), and the call does not donate or +the receiver has an SC (`hnd`); as `JAtZ.sendCallIJSt` up to the reply unlink, +then `reply_push` on `IRel` (`reply_push_iG`, no donation), P1's +`reply_push.ipc` and `reply_push.nd`, the receiver `Running`, `JAtZ` by +`JAtZ.wake_postGR` at `pushSt` (`ps_swap`), the tail `JAtZ.sendTailJ`; the +spec's outcome `sendIpc_call_eq`. -/ +theorem JAtZB.sendCallPJFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrs : xt.tcbState = .restart) (hft : xt.tcbFault = none) (hct : σ.curThread = t.bv) + (hres : σ.schedulerAction = .resumeCurThread) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hg : g = true ∨ gr = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) + (hnd : don = false ∨ ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none) : + endpoint.send_ipc st arI s a blk true badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk true badge.bv g gr don t.bv e.bv σ).1, + SendFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by rw [hrs]; decide + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.recvEP (d'.bv :: q)) d'.bv) hR1 hS1 hW1 F1 ht hdT htd hkt hkd hft + badge g h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, yb, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksy, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hkdy : σy.kheap d'.bv = some (.tcb xd) := hncn hkd (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hkd1y : σ1y.kheap d'.bv = some (.tcb xd) := by simp [σ1y, hde, hkdy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1y] + have Fr1y : CFr e.bv d'.bv σy σ1y := CFr.upd hky (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1y : IK d'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hky (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : SKeep σy σ1y := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have hq1y : ∀ e'' ∈ Z.E, ∀ z, σ1y.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + refine hq1 e'' he'' z ?_ + by_cases hee : e''.bv = e.bv + · rw [hee] at hz ⊢; rw [hk1ey] at hz; rw [hk1e]; exact hz + · simp only [σ1y, Notif.updK_kheap, Function.update_of_ne hee] at hz + simp only [σ1, Notif.updK_kheap, Function.update_of_ne hee] + exact keepC_back hKy.1 hz (fun _ _ h0 => by cases h0) + set σ2 := Notif.updK (Endp.setUm σ1y um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1y] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1ey] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1y e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨⟨d', hdT, (a_reply hda).2⟩, ?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have Fr2 : CFr e.bv d'.bv σy σ2 := (Fr1y.trans (CFr.um _ _ σ1y um2)).trans (CFr.upd (σ := Endp.setUm σ1y um2) hkd1y + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans (IK.updSelf (σ := Endp.setUm σ1y um2) hkd1y hxsc)) + have SK2 : SKeep σy σ2 := SK1y.trans ((SKeep.um σ1y um2).trans (SKeep.updK (σ := Endp.setUm σ1y um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1y, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb yb := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkd _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by + simp [σ2, σ1y, hwe, hwd, hncn hkr (fun _ _ h0 => by cases h0)] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + + have J0 := J + have hk0 := hk + have hkd0 := hkd + have hkt0 := hkt + have J := Jy + have hkd : σy.kheap d'.bv = some (.tcb xd) := hkdy + have hkt : σy.kheap t.bv = some (.tcb xt) := hkty + have hk : σy.kheap e.bv = some (.endpoint (.recvEP (d'.bv :: q))) := hky + have hres : σy.schedulerAction = .resumeCurThread := hsay.trans hres + have hti : σy.curThread ≠ σy.idleThread := by rw [hcty, hidy]; exact hti + have hct : σy.curThread = t.bv := hcty.trans hct + have hnq : ∀ dm q', t.bv ∉ σy.readyQueues dm q' := by rw [hrqy]; exact hnq + have hdc : d'.bv ≠ σy.curThread := by rw [hcty]; exact hdc + have hrepn : ∀ xd, σy.kheap d'.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none := + fun xd h => hrepn xd (by rw [hkdy] at h; rw [hkd0]; exact h) + have hnd : don = false ∨ ∀ xd, σy.kheap d'.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none := + hnd.imp_right fun h xd h' => h xd (by rw [hkdy] at h'; rw [hkd0]; exact h') + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st yb s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + have Fr3 : CFr e.bv d'.bv σy σ3 := (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σy σ3 := by + have IK3a : IK d'.bv σy σ3a := IK2.trans (IK.updOther (o' := .reply xr3) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σy (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + have SK3 : SKeep σy σ3 := + ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans + (SKeep.stsAct _ _) + have hct3 : σ3.curThread = σy.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σy.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d' := + hX3.noref hdta (by rw [e3']; exact P1.inactive_ne.1) (by rw [e3']; exact P1.inactive_ne.2) + have F3 := footW_trans F2 (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hks3 : L.ksb s3 = L.ksb yb := hW3'.1 + -- the caller's and the receiver's words at `s3` + have hCI : CurInv σy := J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hnrT : t.bv ∉ σy.releaseQueue := by rw [← hct]; exact hCI.notRel + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = 2 := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st, hrs]; rfl + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (Or.inr (Or.inl hw3)) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σy d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have ha0 : L.actionW s3 = 0#u64 := by + have h := hSR3.act + rw [hact3, hres] at h + unfold P1.LawfulStore.actionW; rw [hcq3.cur]; exact h + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu h' => by + rw [ha0] at h'; exact (hcq3.tcbs u hu).ne0 (by rw [← h']; rfl) + have hcw3 : L.curW s3 = t := by + have h : (nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σy.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0 => + hB3.sact t htH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨ha0, hcw3⟩))))) h0 + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the receiver's SC: no donation + have hndF : don = true → x3.tcbSchedContext = none → False := fun hdon hx0 => by + rcases hnd with h | h + · rw [h] at hdon; cases hdon + · exact h xd hkd (by rw [← hsc3]; exact hx0) + obtain ⟨xd3, hkd3', hdsc⟩ := hSM d' hdT + rw [hkd3] at hkd3'; cases hkd3' + have hnd1 : don = false ∨ L.mem s3 (P1.scA d') ≠ 0#u64 := by + cases don + · exact Or.inl rfl + · right; intro h0; exact hndF rfl (by rw [hdsc, h0, optRef_zero]) + -- the push on `IRel`, at P1 + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hns3 : tsCode xt.tcbState ≠ 6 := by rw [hrs]; decide + have hPush : ipc.reply_push st arI s3 a t d' w don ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv don σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1 ⦄ := by + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := don) (P := fun s' σ' => σ' = pushSt σ3 t.bv w.bv xr3 xt ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hdon _ _ _ _ _ _ _ hx0 => (hndF hdon hx0).elim)) + (P1.ipc.reply_push.ipc hAT s3 a t d' w don Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hqbD hrbD (hna3 d' hdT) (hiw3 d' hdT hid) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a t d' w don h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hcA hnd1)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, hb2, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hb2, hf, hsb⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv don σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1) ?_ ?_ + · rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, ⟨⟨Q4, R4, h4⟩, hB4, hX4, hS4', hSg4⟩, hst4, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 h4 hB4 hX4 hS4' hSg4 hst4 fu4 hsb4 ⊢ + subst ha4 + try dsimp only + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hW4' : P1.WritesIn st yb s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3 (SzKeepC.of Fr3.szKeep) hb)) + have hks4 : L.ksb s4 = L.ksb yb := hW4'.1 + set σ4 := pushSt σ3 t.bv w.bv xr3 xt with hσ4 + have hkd4 : σ4.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, σ4.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z ((ps_oth hkt3 hkr3 hwt (fun _ h => by cases h) (fun _ h => by cases h)).1 hz) + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hst4d : L.mem s4 (P1.stA d') = sched.TS_INACTIVE := by rw [hst4 d' hdH (Ne.symm htd)]; exact e3' + have hx4 : d' ∈ (∅ : Set U64) ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := Or.inr (by rw [hst4d]; exact P1.not_blocked_inactive) + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := + hX4.noref hdta (by rw [hst4d]; exact P1.inactive_ne.1) (by rw [hst4d]; exact P1.inactive_ne.2) + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 + (by simp [x3, tsCode]) .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q4 R4 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK σ4 d'.bv (.tcb x5)) d'.bv = pushSt σa t.bv w.bv xr3 xt := + ps_swap htd' hwd hdc3 + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 (pushSt σa t.bv w.bv xr3 xt) := by + rw [← heq5]; exact hIR0 + have hW5' : P1.WritesIn st yb s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c yb σy b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q4, R4, h5.congrX P1.empty_congr⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have hrb3 : RbIn x3.tcbState := Or.inr rfl + have Fr5 : CFr e.bv d'.bv σy σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σy (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σy σa := IK5a.trans (IK.sts _ _ _) + have SK5 : SKeep σy σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((J.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σa.schedulerAction = σy.schedulerAction := by + rw [hσa, stsAct_ne (by show d'.bv ≠ σ3.curThread; exact hdc3)]; exact hact3 + -- the push after the wake (`ps_swap`) + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hkwa : σa.kheap w.bv = some (.reply xr3) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, hwd, hkr3] + have hnqT : ∀ dm p, t.bv ∉ σa.readyQueues dm p := by rw [IK5.2.1]; exact hnq + have hnrT' : t.bv ∉ σa.releaseQueue := by rw [IK5.2.2.1]; exact hnrT + set σ' := pushSt σa t.bv w.bv xr3 xt with hσ' + have I5' : Inv σ' := ps_inv hkta hkwa hwt I5 hnqT hnrT' + -- an SC bound to the caller is the current SC, active + have hscA : ∀ p y n, σa.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + have hp0 : σy.kheap p = some (.schedContext y n) := SK5.2.2.2 _ _ _ hp + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp0 + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp0).2⟩ + have hS := J.xtop.fin.1.1.1 + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp0 hyt + rw [hkt] at hku; cases hku + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + have e1 : σy.curSc = q.bv := Option.some.inj (hcs.symm.trans hxs) + rw [← e1] at hp0 + exact hCI.active (J.curSc_ne_idle hCI hti) y n hp0 + have B5' : BlockedScActive σ' := ps_bsa hkta hkwa hwt hscA (Fr5.bsa J.bsa) + have hcn : σ'.schedulerAction = .chooseNewThread := + ps_act (by rw [Fr5.sk.ct, hct]) (by rw [act5, hres]) + have hidT : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI5 : ActIdle σ' := + ⟨fun u hu => (by rw [hcn] at hu; cases hu), ps_idleOK hkta hkwa hwt hidT (Fr5.idleOK J.act.2)⟩ + have hrun5 : CurRunM σ' := Or.inl (by rw [hcn]; intro h; cases h) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rw [hrs]; simp [tsCode]) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J0.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := hR.reply w hwR + rw [hkr] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hkq : ∀ q ∈ D, σ'.kheap q.bv = σa.kheap q.bv := fun q hq => ps_ko (hcD q hq) (hwD q hq) + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) Jy hdT hdth hkd + hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σy umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => JAtZB.recvReplyB Ju q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σa.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hwt h1 h2 + have J5 := Ju.wake_postGR (by rw [← hKy.2.1] at hIR; exact hIR) hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) ps_skD + (ps_ksub hkta hkwa hwt) (ps_szKeep hkta hkwa hwt) (ps_ykeep hkta hkwa hwt) (ps_mcpKeep hkta hkwa hwt) + (ps_bufKeep hkta hkwa hwt) (fun d _ h => ps_recvB hkta hkwa hwt h) (ps_ksz hkta hkwa hwt) + (ps_kback hkta hkwa hwt) (ps_ipcFr hkta hkwa hwt) (ps_ck hkta hkwa hwt) (fun d _ h => ps_free hkta hkwa hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I5' B5' hAI5 (Or.inl hcn) hrun5 (ps_own hkta hkwa hwt ((DWFr.ofCFr Fr5).own ⟨d', hdT, rfl⟩ J.own)) + (ps_off hkta hkwa hwt ((DWFr.ofCFr Fr5).off J.off)) (ps_nsc hkta hkwa hwt ((DWFr.ofCFr Fr5).nsc J.nsc)) + (ps_isorted hkta hkwa hwt ((DWFr.ofCFr Fr5).isorted J.isorted)) hRR htD + -- the tail + have hk5 : σ'.kheap d'.bv = some (.tcb x5) := by + rw [ps_ko (Ne.symm htd') (Ne.symm hwd), hσa, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ σ'.releaseQueue := by rw [ps_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → σ'.kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya : σa.kheap p = some (.schedContext y n) := + (ho (fun _ h => by cases h) (fun _ h => by cases h)).1 hy + have hy0 := SK5.2.2.2 p y n hya + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J.inv.back _ _ _ hkd hp' + rw [hy0] at hky'; cases hky' + exact J.bsa.tcb p y n d'.bv xd hy0 hyt hkd (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ σ'.idleThread := by + rw [(ps_skD (σ := σa) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).idle, Fr5.sk.idle] + exact hid + have htc5 : d'.bv ≠ σ'.curThread := by + rw [(ps_skD (σ := σa) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).ct, Fr5.sk.ct]; exact hdc + have hspec := sendIpc_call_eq (don := don) hk0 hkd0 hde hstd blk badge.bv g gr hg e2 hU hkt3 hkd3 hpush hkd4 + (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk true badge.bv g gr don t.bv e.bv σ = + Endp.specWakeTailE d'.bv σ' := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have Fy5 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (NatK.trans (natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)) (natK_pushSt hkwa hkta (Ne.symm hwt))) + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, yb, σy, Gy, hBase, hCnKy, Jy, _, _, _, _, J7, + Jy.natTrans Fy5 F7.cast⟩ + +end + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} + +/-- **`JSendRestSt` with the footprint post** (`SendFootPost`). -/ +def JSendRestF (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) + (s1 : S) (ar : A) (σ1 : Fl4Spec.State Unit) (blk call : Bool) (b : U64) (g gr don : Bool) (ct p : U64) : Prop := + ∀ x : Fl4Spec.Endpoint, σ1.kheap p.bv = some (.endpoint x) → ¬ CanBlock false x → + endpoint.send_ipc st arI s1 ar blk call b g gr don ct p ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk call b.bv g gr don ct.bv p.bv σ1).1, + SendFootPost (L := L) arI AR Z H Cs Ac Lst D s1 ar σ1 r z.2 ⦄ + +end + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- (p3J gen 35) **`jSendRest_of` at stable ghosts** (module docstring). -/ +theorem jSendRest_ofF (hAT : P0.ArchTotal arI) {s1 : S} {ar : A} {σ1 : Fl4Spec.State Unit} + (J1 : JAtZB (L := L) arI AR Z H Cs Ac Lst D s1 ar σ1) [VxE Z] {ct p : U64} (hct : ct ∈ Z.T) (hp : p ∈ Z.E) + {xt : Fl4Spec.Tcb} (hkt : σ1.kheap ct.bv = some (.tcb xt)) (hrs : xt.tcbState = .restart) + (hft0 : xt.tcbFault = none) (hcur : σ1.curThread = ct.bv) (hres : σ1.schedulerAction = .resumeCurThread) + (hti : σ1.curThread ≠ σ1.idleThread) (hnq : ∀ dm q, ct.bv ∉ σ1.readyQueues dm q) + (blk call : Bool) (b : U64) (g gr don : Bool) : + JSendRestF (L := L) arI AR Z H Cs Ac Lst D s1 ar σ1 blk call b g gr don ct p := by + intro x hk hc + by_cases hb : SendRecvBasic σ1 call don ct.bv p.bv + · obtain ⟨rfl, xt', dv, q, hkt', hft, hk', hdon⟩ := hb + rw [hkt] at hkt'; cases hkt' + have hx := (J1.xtop.irelK J1.ks J1.buf).1.toE.at_ep hp hk' + obtain ⟨-, xs, hxs0, hq, -, -⟩ := hx + obtain ⟨d, xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd, -⟩ := hq + obtain rfl : dv = d.bv := by first | exact hd | exact hd.symm + exact J1.sendRecvJ0FB hct hkt hrs hft hcur hp hk' blk b g gr don hdon + by_cases hci : SendRecvCI σ1 call g gr ct.bv p.bv + · obtain ⟨rfl, xt', dv, q, hkt', hft, hk', hno⟩ := hci + rw [hkt] at hkt'; cases hkt' + have hx := (J1.xtop.irelK J1.ks J1.buf).1.toE.at_ep hp hk' + obtain ⟨-, xs, hxs0, hq, -, -⟩ := hx + obtain ⟨d, xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd, -⟩ := hq + obtain rfl : dv = d.bv := by first | exact hd | exact hd.symm + exact J1.sendCallIJFB hct hkt hrs hft hcur hres hti hnq hp hk' blk b g gr don hno + by_cases hdo : SendRecvDon σ1 call don ct.bv p.bv + · obtain ⟨rfl, rfl, xt', dv, q, hkt', hft, hk', hsn⟩ := hdo + rw [hkt] at hkt'; cases hkt' + have hx := (J1.xtop.irelK J1.ks J1.buf).1.toE.at_ep hp hk' + obtain ⟨-, xs, hxs0, hq, -, -⟩ := hx + obtain ⟨d, xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd, -⟩ := hq + obtain rfl : dv = d.bv := by first | exact hd | exact hd.symm + exact J1.sendDonJFB hAT hct hkt hrs hft hcur hres hti hnq hp hk' blk b g gr hsn + by_cases hcp : SendRecvCP σ1 call g gr don ct.bv p.bv + · obtain ⟨rfl, xt', dv, q, hkt', hft, hk', hg, hrepn, hnd⟩ := hcp + rw [hkt] at hkt'; cases hkt' + have hx := (J1.xtop.irelK J1.ks J1.buf).1.toE.at_ep hp hk' + obtain ⟨-, xs, hxs0, hq, -, -⟩ := hx + obtain ⟨d, xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd, -⟩ := hq + obtain rfl : dv = d.bv := by first | exact hd | exact hd.symm + exact J1.sendCallPJFB hAT hct hkt hrs hft hcur hres hti hnq hp hk' blk b g gr don hg hrepn hnd + by_cases hcpd : SendRecvCPD σ1 call g gr don ct.bv p.bv + · obtain ⟨rfl, rfl, xt', dv, q, hkt', hft, hk', hg, hrepn, hxdn⟩ := hcpd + rw [hkt] at hkt'; cases hkt' + have hx := (J1.xtop.irelK J1.ks J1.buf).1.toE.at_ep hp hk' + obtain ⟨-, xs, hxs0, hq, -, -⟩ := hx + obtain ⟨d, xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons, List.cons.injEq] at hq + obtain ⟨hd, -⟩ := hq + obtain rfl : dv = d.bv := by first | exact hd | exact hd.symm + exact J1.sendCallPDJFB hAT hct hkt hrs hft hcur hres hti hnq hp hk' blk b g gr hg hrepn hxdn + · -- every send to a waiting receiver is one of the five cases + exfalso + cases x with + | idleEP => exact hc trivial + | sendEP l => exact hc rfl + | recvEP l => + have hx := (J1.xtop.irelK J1.ks J1.buf).1.toE.at_ep hp hk + obtain ⟨-, xs, hxs0, hq, hQ, -⟩ := hx + obtain ⟨d, xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + have hk2 : ∃ q', σ1.kheap p.bv = some (.endpoint (.recvEP (d.bv :: q'))) := by + first + | (rw [← hq] at hk; exact ⟨_, hk⟩) + | (rw [hq] at hk; exact ⟨_, hk⟩) + obtain ⟨q', hk2⟩ := hk2 + cases call with + | false => + by_cases hsc : don = false ∨ ∀ xd, σ1.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none + · exact hb ⟨rfl, xt, d.bv, q', hkt, hft0, hk2, hsc⟩ + · have hdon : don = true := by + cases don + · exact absurd (Or.inl rfl) hsc + · rfl + refine hdo ⟨rfl, hdon, xt, d.bv, q', hkt, hft0, hk2, fun xd h' => ?_⟩ + by_contra hne + exact hsc (Or.inr fun xd' h'' => by rw [h'] at h''; cases h''; exact hne) + | true => + by_cases hggr : g = true ∨ gr = true + · by_cases hrep : ∀ xd, σ1.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, + xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none + · by_cases hsc : don = false ∨ ∀ xd, σ1.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none + · exact hcp ⟨rfl, xt, d.bv, q', hkt, hft0, hk2, hggr, hrep, hsc⟩ + · have hdon : don = true := by + cases don + · exact absurd (Or.inl rfl) hsc + · rfl + refine hcpd ⟨rfl, hdon, xt, d.bv, q', hkt, hft0, hk2, hggr, hrep, fun xd h' => ?_⟩ + by_contra hne + exact hsc (Or.inr fun xd' h'' => by rw [h'] at h''; cases h''; exact hne) + · refine hci ⟨rfl, xt, d.bv, q', hkt, hft0, hk2, Or.inr fun xd h' e' rep dd hst => ?_⟩ + by_contra hne + exact hrep fun xd' h'' e'' rep' dd' hst' => by + rw [h'] at h''; cases h''; rw [hst] at hst'; cases hst'; exact hne + · refine hci ⟨rfl, xt, d.bv, q', hkt, hft0, hk2, Or.inl ⟨?_, ?_⟩⟩ + · cases g + · rfl + · exact absurd (Or.inl rfl) hggr + · cases gr + · rfl + · exact absurd (Or.inr rfl) hggr + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/SInvXV.lean b/proofs/Fl4Proofs/P3/SInvXV.lean new file mode 100644 index 000000000..88b674b75 --- /dev/null +++ b/proofs/Fl4Proofs/P3/SInvXV.lean @@ -0,0 +1,322 @@ +import Fl4Proofs.P3.P3JCapDom +import Fl4Proofs.P3.XTopJointKEntry27b +import Fl4Proofs.P3.XTopJointKEntry35 +import Fl4Proofs.P3.XTopJointKEntryU13 + +/-! +P3 (line mmuAD, Stage 3): **the carried spec bundle without the native scope** — `SInvXV` is `SInvR.G4.SInvX` with the +one field `SInvA.ns` (`NativeScopeS`: no TCB is `runningVM`) removed, so it can hold while a guest runs. Merged +`SInvX` is untouched; `SInvX.toXV` gives the conservativity. The projections and the lemmas the Stage-3 entry reads +are copies of the merged ones over the sibling bundle (their proofs read no `ns`). No preservation is proved: +`SInvXV` is an entry premise only. Generated by `handoff/tools/gen_sinvxv.py`. No named premise. +-/ + +open Aeneas Aeneas.Std Result +open Classical + +namespace Fl4Proofs.P3.SInvAll +open Fl4Spec + +noncomputable section + +/-- `SInvA` without `ns` (`NativeScopeS`); every other field verbatim. -/ +structure SInvAV (σ : State Unit) : Prop where + vfh : SInv.VfhAll σ + cnDom : SInv.CnDom σ + fts : SInv.FaultTcbsS σ + nqb : SInv.NQB σ + sg : SInvG.SpecGlobS σ + idleOut : SInvG.IdleOutV σ + dom : DomListOK σ + vim : SInvV.ValidIrqMasks σ + vih : SInvV.ValidIrqHandlers σ + vmt : SInvV.ValidMachineTime σ + ut : SInvU.UtInv3 σ + nq : SInv.NtfnQB σ + epq : SInv.EpQB σ + live : SInv.IfLiveT σ + vi : SInvI.ValidIdle σ + ft : SInvF.FT σ + sl : SInvL.SlB2 σ + cs : SInvQ.CurScT σ + +/-- `SInvB` over `SInvAV`. -/ +abbrev SInvBV (σ : State Unit) : Prop := (SInvAV σ ∧ CtExit σ) ∧ OnlyIdle σ + +theorem SInvAV.idleSc {σ : State Unit} (h : SInvAV σ) : SInvI.IdleScOutV σ := idleSc_L_I h.sl.1.2.1 + +theorem SInvAV.scInv {σ : State Unit} (h : SInvAV σ) : SInvB.ScInv σ := h.sl.1.2.2.2 + +theorem SInvBV.entryJ {σ : State Unit} (h : SInvBV σ) (hct : σ.curThread ≠ σ.idleThread) : EntryJ σ := + entryJ_of h.1.2 hct h.2 + +theorem SInvA.toAV {σ : State Unit} (h : SInvA σ) : SInvAV σ := { h with } + +/-- **The current thread's handler and fault at a dispatch state** (module docstring). -/ +theorem sinvB_dispXV {σ σb : State Unit} (h : SInvBV σ) (hct : σ.curThread ≠ σ.idleThread) + {x x' : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (hfh : x'.tcbFaultHandler = x.tcbFaultHandler) (hf : x'.tcbFault = x.tcbFault) + (ek : σb.kheap = Function.update σ.kheap σ.curThread (some (.tcb x'))) + (ect : σb.curThread = σ.curThread) : + (∀ y, σb.kheap σb.curThread = some (.tcb y) → validFaultHandler y.tcbFaultHandler) ∧ + (∀ y, σb.kheap σb.curThread = some (.tcb y) → y.tcbFault = none) := by + have hy : ∀ y, σb.kheap σb.curThread = some (.tcb y) → y = x' := by + intro y hy + rw [ek, ect, Function.update_self] at hy + cases hy + rfl + refine ⟨fun y hy' => ?_, fun y hy' => ?_⟩ + · rw [hy y hy', hfh] + exact h.1.1.vfh _ x hk + · rw [hy y hy', hf] + obtain ⟨x1, hx1, ha⟩ := (h.entryJ hct).ctAct + rw [hk] at hx1 + cases hx1 + by_contra hne + have hs := h.1.1.fts _ x hk hne + revert ha hs + generalize x.tcbState = ts + intro ha hs + cases ts <;> first | exact (ha : False).elim | exact (hs : False).elim + +end + +end Fl4Proofs.P3.SInvAll + +namespace Fl4Proofs.P3.SInvK +open Fl4Spec + +noncomputable section + +set_option maxHeartbeats 4000000 +open Fl4Spec +variable {σ σ0 σb : State Unit} + +/-- **The current thread** (l4v `if_live_then_nonz_cap`: it is active, so named). -/ +theorem out_ctXV {σ : State Unit} (hS : SInvAll.SInvBV σ) (hct : σ.curThread ≠ σ.idleThread) : + Out σ σ.curThread := by + obtain ⟨x, hx, ha⟩ := (hS.entryJ hct).ctAct + refine ⟨(by rw [hx]; exact fun h => by cases h), Or.inl ?_⟩ + obtain ⟨s, c, hs, hr⟩ := SInv.ifLiveT_objRefs hS.1.1.live hx (SInv.live_of_active ha) + exact ⟨s, c, hs, SInvU.objRefs_sub_capRng c hr⟩ + +/-- **An SC a TCB is bound to** (l4v `sym_refs` + `if_live_then_nonz_cap`, or the +idle SC / idle thread, outside every cap's range by `valid_global_refs`). -/ +theorem out_boundXV {σ : State Unit} (hS : SInvAll.SInvBV σ) {u p : ObjRef} {x : Tcb} + (hu : σ.kheap u = some (.tcb x)) (hp : x.tcbSchedContext = some p) : Out σ p := by + obtain ⟨y, k, hy, hyt, -⟩ := hS.1.1.scInv.back u x p hu hp + refine ⟨(by rw [hy]; exact fun h => by cases h), ?_⟩ + by_cases h1 : p = σ.idleThread + · right + intro s c hs + rw [h1] + exact hS.1.1.idleOut (default : Irq) s c hs + by_cases h2 : p = σ.idleSc + · right + intro s c hs + rw [h2] + exact hS.1.1.idleSc (default : Irq) s c hs + left + have hl : SInvL.liveO (.schedContext y k) := Or.inl (by rw [hyt]; exact Option.some_ne_none u) + obtain ⟨s, c, hs, hn⟩ := hS.1.1.sl.1.1 p (.schedContext y k) hy hl h1 h2 + refine ⟨s, c, hs, SInvU.objRefs_sub_capRng c ?_⟩ + cases c <;> simp only [SInvL.NCap] at hn + all_goals first | exact hn.elim | (subst hn; simp [objRefs]) + +set_option maxHeartbeats 2000000 in +/-- **`EpQAll` at the dispatch state** (module docstring). -/ +theorem epQAll_dispXV {σ σb : State Unit} (hS : SInvAll.SInvBV σ) (ha : σ.schedulerAction = .resumeCurThread) + {x : Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) + (hu : ((), σb) ∈ (updateTimeStamp (Z := Unit) (fipSt σ x)).1) (hE : SInvE.EpQAll σ) : + SInvE.EpQAll σb := by + obtain ⟨ek, -, et, -⟩ := SInvO.uts_off hu + obtain ⟨-, ea, -, -⟩ := SInvR.qa_updateTimeStamp (fipSt σ x).readyQueues (fipSt σ x).schedulerAction + (fipSt σ x).releaseQueue (fipSt σ x).idleSc (fipSt σ x) ⟨rfl, rfl, rfl, rfl⟩ _ hu + have ht : σ.curTime.toNat ≤ σb.curTime.toNat := by + have hv := hS.1.1.vmt + have a1 : σ.curTime.toNat ≤ σ.machineState.lastMachineTime.toNat := BitVec.le_def.mp hv.1 + have a2 : σ.machineState.lastMachineTime.toNat ≤ SInvV.gctN σ.machineState := by + unfold SInvV.gctN; exact le_min hv.2 (Nat.le_add_right _ _) + rw [et, SInvV.gctN_toNat] + exact Nat.le_trans a1 a2 + have hkn : ∀ p, p ≠ σ.curThread → σb.kheap p = σ.kheap p := fun p hp => by + rw [ek]; exact Function.update_of_ne hp _ _ + have hkc : σb.kheap σ.curThread = + some (.tcb (Notif.setReg x .faultIP (x.tcbArch.tcbContext.userRegs .nextIP - 2))) := by + rw [ek]; exact Function.update_self _ _ _ + have hsc : ∀ c y k, σ.kheap c = some (.schedContext y k) → σb.kheap c = some (.schedContext y k) := by + intro c y k h + have hc : c ≠ σ.curThread := by rintro rfl; rw [hk] at h; cases h + rw [hkn c hc]; exact h + have hok : ∀ z, Sched.ScOK σ z → Sched.ScOK σb z := by + intro z h c hc + obtain ⟨y, k, hy, h1, h2, h3⟩ := h c hc + exact ⟨y, k, hsc c y k hy, h1, + fun hA => ⟨SInvR.ready_of_nat (Nat.le_trans (SInvR.nat_of_ready (h2 hA).1) ht), (h2 hA).2⟩, h3⟩ + have hT : ∀ u xu, σb.kheap u = some (.tcb xu) → ∃ x0, σ.kheap u = some (.tcb x0) ∧ + xu.tcbSchedContext = x0.tcbSchedContext ∧ xu.tcbFault = x0.tcbFault := by + intro u xu h + by_cases hu : u = σ.curThread + · subst hu; rw [hkc] at h; cases h; exact ⟨x, hk, rfl, rfl⟩ + · rw [hkn u hu] at h; exact ⟨xu, h, rfl, rfl⟩ + have hT' : ∀ u x0, σ.kheap u = some (.tcb x0) → ∃ xu, σb.kheap u = some (.tcb xu) ∧ + xu.tcbSchedContext = x0.tcbSchedContext ∧ xu.tcbFault = x0.tcbFault := by + intro u x0 h + by_cases hu : u = σ.curThread + · subst hu; rw [hk] at h; cases h; exact ⟨_, hkc, rfl, rfl⟩ + · rw [← hkn u hu] at h; exact ⟨x0, h, rfl, rfl⟩ + intro e y hy + have he : e ≠ σ.curThread := by rintro rfl; rw [hkc] at hy; cases hy + rw [hkn e he] at hy + obtain ⟨hm, hb, -⟩ := hE e y hy + refine ⟨fun u hu => ?_, ?_, ?_⟩ + · obtain ⟨x0, hx0, hc0⟩ := hm u hu + obtain ⟨xu, hxu, hs, hf⟩ := hT' u x0 hx0 + refine ⟨xu, hxu, fun hn => ?_⟩ + rw [hf] at hn + have h1 := hok x0 (hc0 hn) + intro c hc + rw [hs] at hc + exact h1 c hc + · intro u xu c h1 h2 + obtain ⟨x0, hx0, hs, -⟩ := hT u xu h1 + rw [hs] at h2 + obtain ⟨y', k', hy', h3, h4⟩ := hb u x0 c hx0 h2 + exact ⟨y', k', hsc c y' k' hy', h3, h4⟩ + · intro t htt + have e1 : σb.schedulerAction = σ.schedulerAction := ea + rw [e1, ha] at htt + cases htt + +end + +end Fl4Proofs.P3.SInvK + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +/-- **The dispatch state's spec front** (module docstring). -/ +theorem disp27bXV (hnd : Fl4Spec.numDomains = 1) {σ σb : Fl4Spec.State Unit} (hS : SInvAll.SInvBV σ) + (hBA : BudgetAt σ σb true) : + ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ + ((), SInvK.fipSt σ x) ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1 ∧ + ((), σb) ∈ (Fl4Spec.updateTimeStamp (Z := Unit) (SInvK.fipSt σ x)).1 ∧ + σb.kheap = (faultIPSt σ σ.curThread x).kheap ∧ σb.curThread = σ.curThread := by + obtain ⟨x, hk, -⟩ := hS.1.2 + obtain ⟨⟨σu, hu, ebu⟩, ek, ect, -, -, -⟩ := BudgetAt.shape27 hnd hk hBA + subst ebu + refine ⟨x, hk, ?_, hu, ek, ect⟩ + rw [fl4SyscallFaultIP_eq hk] + exact Set.mem_singleton _ + +set_option maxHeartbeats 4000000 in +/-- **[J] `JPerfRegHyp` holds at the dispatch state** (module docstring). -/ +theorem jPerfRegHyp_27bXV (hnd : Fl4Spec.numDomains = 1) {σ σb : Fl4Spec.State Unit} (hS : SInvAll.SInvBV σ) + (hct : σ.curThread ≠ σ.idleThread) (hBA : BudgetAt σ σb true) (cptr : Fl4Spec.CapRef) : + MidRun.JPerfRegHyp cptr σb := by + obtain ⟨x, hk, h0, hu, ek, ect⟩ := disp27bXV hnd hS hBA + have hbu : SInvU.uV σb = SInvU.uV σ := SInvK.disp_uv h0 hu + have hU : SInvU.UtInv'' σ := hS.1.1.ut.1 + intro inv σ1 hA + obtain ⟨hv1, -, hinv⟩ := SInvK.midAt_facts (show SInvK.MidAt cptr σb inv σ1 from hA) + have hm : MidRun.mV σ1 = MidRun.mV σb := congrArg (fun v : MidRun.VT => v.2.2.2) (midPerfAt_sV hA) + have hv : SInvU.uV σ1 = SInvU.uV σ := hv1.trans hbu + rw [hbu] at hinv + have hc : SInvK.Out σ σb.curThread := by rw [ect]; exact SInvK.out_ctXV hS hct + have hB : ∀ u p, (MidRun.mV σ1 u).1 = some (some p) → SInvK.Out σ p := fun u p h => by + obtain ⟨x0, hx0, hp⟩ := bound27b hk ek hm h + exact SInvK.out_boundXV hS hx0 hp + cases inv with + | invokeUntyped ui => + cases ui with + | retype src reset base rb ty sz slots dev => + have hok := (show SInvU.UtRetOKV _ _ ∧ SInvU.DestNullV _ _ from hinv).1 + exact And.intro (SInvK.out_ut hU hv hok hc) (fun u p h => SInvK.out_ut hU hv hok (hB u p h)) + | invokeArchObject ai => + cases ai with + | spec i => + cases i with + | invokeASIDControl a => + cases a with + | makePool frame slot parent base => + have hok : SInvU.MpOKV (SInvU.uV σ) (.makePool frame slot parent base) := hinv + exact And.intro (SInvK.out_mp hU hok hc) (fun u p h => SInvK.out_mp hU hok (hB u p h)) + | _ => trivial + | _ => trivial + | _ => trivial + +/-- **[J] `JPerfIrqSlotHyp` holds at the dispatch state** (module docstring). -/ +theorem jPerfIrqSlotHyp_27bXV (hnd : Fl4Spec.numDomains = 1) {σ σb : Fl4Spec.State Unit} (hS : SInvAll.SInvBV σ) + (hBA : BudgetAt σ σb true) (cptr : Fl4Spec.CapRef) : MidRun.JPerfIrqSlotHyp cptr σb := by + obtain ⟨x, -, h0, hu, -, -⟩ := disp27bXV hnd hS hBA + intro i σ1 hA + obtain ⟨hv1, hn1, -⟩ := SInvK.midAt_facts (show SInvK.MidAt cptr σb _ σ1 from hA) + exact SInvK.irqSlot_of hS.1.1.sg (hv1.trans (SInvK.disp_uv h0 hu)) (hn1.trans (SInvK.disp_node h0 hu)) + +/-- **`JEntryAt.epQ` at the dispatch state from `EpQAll σ`** (module docstring). -/ +theorem epQPre_of_sinvXV (hnd : Fl4Spec.numDomains = 1) {σ σb : Fl4Spec.State Unit} (hS : SInvAll.SInvBV σ) + (hni : Entered σ) (hE : SInvE.EpQAll σ) (hBA : BudgetAt σ σb true) : ∀ e, Sched.EpQPre σb e := by + obtain ⟨x, hk, -, hu, -, -⟩ := disp27bXV hnd hS hBA + exact SInvK.epQAll_dispXV hS hni.2 hk hu hE + +end + +end Fl4Proofs.P3 + +namespace Fl4Proofs.P3.SInvR.G4 +open Fl4Spec Fl4Proofs.P3.SInvR.B8w + +noncomputable section + +/-- **`SInvX` without `NativeScopeS`**: the same conjunction chain (`SInvW`, `SInvCRBV`, … ) over `SInvAV`. -/ +abbrev SInvXV (σ : State Unit) : Prop := + (((((SInvAll.SInvBV σ ∧ SInvQ.EX σ ∧ SInvQ.RB σ ∧ SInvQ.T4 σ) ∧ SInvQ.RS σ) ∧ B8g.BSA σ) ∧ B8n.VB σ) ∧ SInvR.B8w.RdL σ) ∧ + SInvE.RPos σ + +variable {σ : State Unit} + +theorem SInvX.toXV (h : SInvX σ) : SInvXV σ := + ⟨⟨⟨⟨⟨⟨⟨⟨h.sb.1.1.toAV, h.sb.1.2⟩, h.sb.2⟩, h.cq.2⟩, h.cr.2⟩, h.1.1.1.2⟩, h.1.1.2⟩, h.1.2⟩, h.2⟩ + +theorem SInvXV.cr (h : SInvXV σ) : + (SInvAll.SInvBV σ ∧ SInvQ.EX σ ∧ SInvQ.RB σ ∧ SInvQ.T4 σ) ∧ SInvQ.RS σ := h.1.1.1.1 + +theorem SInvXV.sb (h : SInvXV σ) : SInvAll.SInvBV σ := h.cr.1.1 + +theorem SInvXV.ctQ (h : SInvXV σ) : ∀ d p, σ.curThread ∉ σ.readyQueues d p := h.cr.1.2.1.2.1 + +theorem SInvXV.entered (h : SInvXV σ) (hct : σ.curThread ≠ σ.idleThread) : Entered σ := And.intro hct h.cr.2 + +theorem SInvXV.offReady (h : SInvXV σ) : Sched.OffReady σ := h.1.2.2.1 + +theorem SInvXV.epQAll (h : SInvXV σ) : SInvE.EpQAll σ := + SInvE.epQAll_of h.1.2.1 h.sb.1.1.scInv h.sb.1.1.epq h.2 + +theorem sinvXV_sg (h : SInvXV σ) : SInvG.SpecGlobS σ := h.sb.1.1.sg + +end + +end Fl4Proofs.P3.SInvR.G4 + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +open Fl4Proofs.P3.SInvR.G4 + +noncomputable section + +/-- (p3J gen 36) The dispatch state's `Entered` and `SpecGlob` (module docstring). -/ +theorem dispEnteredSgXV (hnd : Fl4Spec.numDomains = 1) {σ σb : Fl4Spec.State Unit} (hX : SInvXV σ) + (hni : σ.curThread ≠ σ.idleThread) (hBA : BudgetAt σ σb true) : Entered σb ∧ SpecGlob σb := by + obtain ⟨x0, hk0, -⟩ := hX.sb.1.2 + have hnib : Entered σb := BudgetAt.entered hnd hk0 (hX.entered hni) hBA + obtain ⟨-, ek, -, en0, ei0, -⟩ := BudgetAt.shape27 hnd hk0 hBA + have ek' : σb.kheap = Function.update σ.kheap σ.curThread + (some (.tcb (Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)))) := ek + exact ⟨hnib, specGlob_disp (x' := Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)) + (sinvXV_sg hX) hk0 rfl ek' en0 ei0⟩ + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/SInvXVWit.lean b/proofs/Fl4Proofs/P3/SInvXVWit.lean new file mode 100644 index 000000000..417be02ca --- /dev/null +++ b/proofs/Fl4Proofs/P3/SInvXVWit.lean @@ -0,0 +1,661 @@ +import Fl4Proofs.P3.VmEntryS3Start +import Fl4Proofs.P3.JBootQ1Spec + +/-! +P3 (line mmuAD, Stage 3): **a spec-level non-vacuity witness of the Stage-3 start record's spec fields** (not of +`VEAnyX ∧ S3Pre`: no concrete store is built here, and `StartS3.air` / `StartS3.core` are not witnessed). + +`σw` is jboot's root-thread state `JBootQ.σ1` with a radix-3 root CNode holding the root's thread and SC caps, a RAM +Untyped cap, a SchedControl cap and a thread cap to a second TCB that is `runningVM` (no scheduling context). +* `startSpec_witness`: `SInvXV`, current ≠ idle, `SpecIoPorts`, `SpecUtZ`, `DevRangeS`, `GpmOutV`, `UtInv''`, `RV`, + `IdleVtS` hold together at `σw`, with the two caps in the current thread's CSpace and the `runningVM` TCB. +* `σw_not_sinvX`: the native `SInvX` is false at `σw`. +No premise. New file. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.XVWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-- The guest TCB's address `kernelBase + 0xB000`. -/ +def gT : U64 := 18446744071562113024#u64 +/-- The Untyped region's base `kernelBase + 0xC000`. -/ +def uB : U64 := 18446744071562117120#u64 + +theorem gT_toNat : gT.bv.toNat = 18446744071562113024 := rfl +theorem uB_toNat : uB.bv.toNat = 18446744071562117120 := rfl + +abbrev cnW : Cap := .cnodeCap rC.bv 3 [] +abbrev utW : Cap := .untypedCap false uB.bv 12 0 + +def rootTcbW : Tcb := { rootTcbV with tcbCtable := cnW } +def guestTcbV : Tcb := { idleTcbV with tcbState := .runningVM, tcbSchedContext := none } + +def wCs : CnodeContents := fun l => + if l = [false, false, false] then some (.threadCap rT.bv) + else if l = [false, false, true] then some (.schedContextCap rS.bv 0) + else if l = [false, true, false] then some utW + else if l = [false, true, true] then some .schedControlCap + else if l = [true, false, false] then some (.threadCap gT.bv) + else if l.length = 3 then some .nullCap else none + +def khW : KHeap := + Function.update (Function.update (Function.update (Function.update fInitKheap rT.bv (some (.tcb rootTcbW))) + rS.bv (some (.schedContext rootScV 0))) rC.bv (some (.cnode 3 wCs))) gT.bv (some (.tcb guestTcbV)) + +def σw : State Unit := { σ1 with kheap := khW, isOriginalCap := fun sl => decide (sl = (rC.bv, [false, true, false])) } + +theorem σw_kheap : σw.kheap = khW := rfl + +/-! ## The heap -/ + +theorem ne_TS : rT.bv ≠ rS.bv := by decide +theorem ne_TC : rT.bv ≠ rC.bv := by decide +theorem ne_TG : rT.bv ≠ gT.bv := by decide +theorem ne_SC : rS.bv ≠ rC.bv := by decide +theorem ne_SG : rS.bv ≠ gT.bv := by decide +theorem ne_CG : rC.bv ≠ gT.bv := by decide + +theorem kh_rT : khW rT.bv = some (.tcb rootTcbW) := by + unfold khW + rw [Function.update_of_ne ne_TG, Function.update_of_ne ne_TC, Function.update_of_ne ne_TS, Function.update_self] +theorem kh_rS : khW rS.bv = some (.schedContext rootScV 0) := by + unfold khW; rw [Function.update_of_ne ne_SG, Function.update_of_ne ne_SC, Function.update_self] +theorem kh_rC : khW rC.bv = some (.cnode 3 wCs) := by + unfold khW; rw [Function.update_of_ne ne_CG, Function.update_self] +theorem kh_gT : khW gT.bv = some (.tcb guestTcbV) := by + unfold khW; rw [Function.update_self] +theorem kh_old {x : ObjRef} (h1 : x ≠ rT.bv) (h2 : x ≠ rS.bv) (h3 : x ≠ rC.bv) (h4 : x ≠ gT.bv) : + khW x = fInitKheap x := by + unfold khW + rw [Function.update_of_ne h4, Function.update_of_ne h3, Function.update_of_ne h2, Function.update_of_ne h1] + +theorem kh_some {p : ObjRef} {o : KernelObject} (h : khW p = some o) : + (p = rT.bv ∧ o = .tcb rootTcbW) ∨ (p = rS.bv ∧ o = .schedContext rootScV 0) ∨ + (p = rC.bv ∧ o = .cnode 3 wCs) ∨ (p = gT.bv ∧ o = .tcb guestTcbV) ∨ fInitKheap p = some o := by + by_cases e1 : p = rT.bv + · subst e1; rw [kh_rT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + by_cases e2 : p = rS.bv + · subst e2; rw [kh_rS] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + by_cases e3 : p = rC.bv + · subst e3; rw [kh_rC] at h; cases h; exact Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)) + by_cases e4 : p = gT.bv + · subst e4; rw [kh_gT] at h; cases h; exact Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩))) + rw [kh_old e1 e2 e3 e4] at h + exact Or.inr (Or.inr (Or.inr (Or.inr h))) + +theorem kh_tcb_of {p : ObjRef} {x : Tcb} (h : khW p = some (.tcb x)) : + (p = rT.bv ∧ x = rootTcbW) ∨ (p = gT.bv ∧ x = guestTcbV) ∨ (p = idleThreadPtr ∧ x = idleTcbV) := by + rcases kh_some h with ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, e⟩ | h' + · cases e; exact Or.inl ⟨rfl, rfl⟩ + · cases e + · cases e + · cases e; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inr (Or.inr (bootHeap_fInit.tcb_of h')) + +theorem kh_sc_of {p : ObjRef} {y : SchedContext} {n : Nat} (h : khW p = some (.schedContext y n)) : + (p = rS.bv ∧ y = rootScV ∧ n = 0) ∨ (p = idleScPtr ∧ y = idleScV ∧ n = 0) := by + rcases kh_some h with ⟨-, e⟩ | ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e; exact Or.inl ⟨rfl, rfl, rfl⟩ + · cases e + · cases e + · exact Or.inr (bootHeap_fInit.sc_of h') + +theorem kh_no_ep {p : ObjRef} {x : Endpoint} (h : khW p = some (.endpoint x)) : False := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · exact bootHeap_fInit.no_ep h' + +theorem kh_no_ntfn {p : ObjRef} {x : Notification} (h : khW p = some (.notification x)) : False := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · exact bootHeap_fInit.no_ntfn h' + +theorem kh_idleT : khW idleThreadPtr = some (.tcb idleTcbV) := by + rw [kh_old (by decide) (by decide) (by decide) (by decide)]; exact bootHeap_fInit.tcb +theorem kh_idleS : khW idleScPtr = some (.schedContext idleScV 0) := by + rw [kh_old (by decide) (by decide) (by decide) (by decide)]; exact bootHeap_fInit.sc + +/-! ## The caps -/ + +theorem wCs_wf : wellFormedCnodeN 3 wCs := by + unfold wellFormedCnodeN dom + ext l + simp only [Set.mem_setOf_eq] + constructor + · intro h + unfold wCs at h + split_ifs at h with h1 h2 h3 h4 h5 h6 + · subst h1; rfl + · subst h2; rfl + · subst h3; rfl + · subst h4; rfl + · subst h5; rfl + · exact h6 + · exact absurd h (by simp) + · intro hl + unfold wCs + split_ifs <;> simp + +theorem capView_wC : SInvU.capView (.cnode 3 wCs) = wCs := by simp [SInvU.capView, wCs_wf] +theorem capOpt_wC (l : CnodeIndex) : capOpt (some (.cnode 3 wCs)) l = wCs l := by simp [capOpt, wCs_wf] + +theorem wCs_cases {l : CnodeIndex} {c : Cap} (h : wCs l = some c) : + (c = .nullCap ∧ l.length = 3) ∨ (l = [false, false, false] ∧ c = .threadCap rT.bv) ∨ + (l = [false, false, true] ∧ c = .schedContextCap rS.bv 0) ∨ (l = [false, true, false] ∧ c = utW) ∨ + (l = [false, true, true] ∧ c = .schedControlCap) ∨ (l = [true, false, false] ∧ c = .threadCap gT.bv) := by + unfold wCs at h + split_ifs at h with h1 h2 h3 h4 h5 h6 + · cases h; exact Or.inr (Or.inl ⟨h1, rfl⟩) + · cases h; exact Or.inr (Or.inr (Or.inl ⟨h2, rfl⟩)) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inl ⟨h3, rfl⟩))) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨h4, rfl⟩)))) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨h5, rfl⟩)))) + · cases h; exact Or.inl ⟨rfl, h6⟩ + +theorem rootW_map {l : CnodeIndex} {c : Cap} (h : tcbCnodeMap rootTcbW l = some c) : + (l = tcbCnodeIndex 0 ∧ c = cnW) ∨ c = .nullCap := by + unfold tcbCnodeMap at h + simp only [Function.update_apply] at h + split_ifs at h with h1 h2 h3 h4 h5 h6 <;> (try cases h) <;> (try exact Or.inr rfl) + exact Or.inl ⟨h6, rfl⟩ + +theorem guest_map {l : CnodeIndex} {c : Cap} (h : tcbCnodeMap guestTcbV l = some c) : c = .nullCap := by + unfold tcbCnodeMap at h + simp only [Function.update_apply] at h + split_ifs at h <;> (try cases h) <;> rfl + +/-- **The spec caps of `σw` by slot.** -/ +theorem capAtW {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) : + c = .nullCap ∨ (sl = (rT.bv, tcbCnodeIndex 0) ∧ c = cnW) ∨ + (sl = (rC.bv, [false, false, false]) ∧ c = .threadCap rT.bv) ∨ + (sl = (rC.bv, [false, false, true]) ∧ c = .schedContextCap rS.bv 0) ∨ + (sl = (rC.bv, [false, true, false]) ∧ c = utW) ∨ + (sl = (rC.bv, [false, true, true]) ∧ c = .schedControlCap) ∨ + (sl = (rC.bv, [true, false, false]) ∧ c = .threadCap gT.bv) := by + obtain ⟨p, i⟩ := sl + unfold SInvU.capAt at h + rw [σw_kheap] at h + split at h + · rename_i o ho + rcases kh_some ho with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h' + · rcases rootW_map h with ⟨rfl, rfl⟩ | rfl + · exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inl rfl + · cases h + · rw [capView_wC] at h + rcases wCs_cases h with ⟨rfl, -⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact Or.inl rfl + all_goals simp + · exact Or.inl (guest_map h) + · left + have hb := bootHeap_fInit.capAt (N := fIrqNode) (sl := (p, i)) (c := c) + apply hb + unfold SInvU.capAt + show (match fInitKheap p with | some o => SInvU.capView o i | none => none) = some c + rw [h']; exact h + · cases h + +theorem capW {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) : + c = .nullCap ∨ c = cnW ∨ c = .threadCap rT.bv ∨ c = .schedContextCap rS.bv 0 ∨ c = utW ∨ + c = .schedControlCap ∨ c = .threadCap gT.bv := by + rcases capAtW h with e | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> simp [e] + +theorem capOptW {p : ObjRef} {cref : CnodeIndex} {c : Cap} (h : capOpt (σw.kheap p) cref = some c) : + SInvU.capAt σw (p, cref) = some c := (SInvUZ.capAt_capOpt σw (p, cref)).trans h + +theorem capAt_slot {l : CnodeIndex} {c : Cap} (h : wCs l = some c) : SInvU.capAt σw (rC.bv, l) = some c := by + have e : SInvU.capAt σw (rC.bv, l) = SInvU.capView (.cnode 3 wCs) l := by + unfold SInvU.capAt; rw [σw_kheap, kh_rC] + rw [e, capView_wC]; exact h + +/-- The only untyped cap. -/ +theorem ut_only {sl : CslotPtr} {d : Bool} {b : ObjRef} {n f : Nat} + (h : SInvU.capAt σw sl = some (.untypedCap d b n f)) : + sl = (rC.bv, [false, true, false]) ∧ d = false ∧ b = uB.bv ∧ n = 12 ∧ f = 0 := by + rcases capAtW h with e | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨hs, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> cases e + exact ⟨hs, rfl, rfl, rfl, rfl⟩ + +/-! ## The Untyped region -/ + +theorem delR_toNat {x : ObjRef} (h : x ∈ SInvU.delR uB.bv 12) : + 18446744071562117120 ≤ x.toNat ∧ x.toNat ≤ 18446744071562121215 := by + obtain ⟨h1, h2⟩ := h + have e : (uB.bv + 2 ^ 12 - 1 : BitVec 64).toNat = 18446744071562121215 := by decide + rw [BitVec.le_def] at h1 h2 + rw [e] at h2 + rw [uB_toNat] at h1 + exact ⟨h1, h2⟩ + +theorem irq_not_region {x : ObjRef} (hx : x ∈ SInvU.delR uB.bv 12) (irq : Irq) : fIrqNode irq ≠ x := + fIrqNode_ne (by have := delR_toNat hx; omega) irq + +theorem not_region {x : ObjRef} (h : x.toNat < 18446744071562117120) : x ∉ SInvU.delR uB.bv 12 := + fun hx => by have := delR_toNat hx; omega + +/-- Every object of `σw` lies outside the Untyped region's range of addresses. -/ +theorem obj_out {p : ObjRef} {o : KernelObject} (h : khW p = some o) : + p.toNat < 18446744071562117120 ∨ 18446744071562121215 < p.toNat := by + by_contra hc + have hx : 18446744071562117120 ≤ p.toNat ∧ p.toNat ≤ 18446744071562121215 := by omega + have ne : ∀ q : ObjRef, q.toNat < 18446744071562117120 → p ≠ q := fun q hq e => by subst e; omega + have h1 := ne rT.bv (by rw [rT_toNat]; omega) + have h2 := ne rS.bv (by rw [rS_toNat]; omega) + have h3 := ne rC.bv (by rw [rC_toNat]; omega) + have h4 := ne gT.bv (by rw [gT_toNat]; omega) + rw [kh_old h1 h2 h3 h4, fInitKheap_none (ne _ (by decide)) (ne _ (by decide)) (ne _ (by decide)) + (ne _ (by decide)) (ne _ (by decide)) (fIrqNode_ne (by omega))] at h + cases h + +theorem cov_cases {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) {r : ObjRef} (hr : r ∈ SInvU.covR c) : + r = rT.bv ∨ r = rS.bv ∨ r = rC.bv ∨ r = gT.bv ∨ r ∈ SInvU.delR uB.bv 12 := by + rcases capW h with rfl | rfl | rfl | rfl | rfl | rfl | rfl + all_goals first + | exact Or.inr (Or.inr (Or.inr (Or.inr hr))) + | (simp [SInvU.covR, SInvU.capRng, objRefs] at hr <;> simp [hr]) + +theorem objRefsW {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) {r : ObjRef} (hr : r ∈ objRefs c) : + r = rT.bv ∨ r = rS.bv ∨ r = rC.bv ∨ r = gT.bv := by + rcases capW h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [objRefs] at hr <;> simp [hr] + +theorem irqOutW (N : Irq → ObjRef) + (hN : ∀ irq, N irq ≠ rT.bv ∧ N irq ≠ rS.bv ∧ N irq ≠ rC.bv ∧ N irq ≠ gT.bv ∧ N irq ∉ SInvU.delR uB.bv 12) : + SInvU.IrqOutV N (SInvU.uV σw) := by + intro irq s c hs hr + rcases cov_cases hs hr with e | e | e | e | e + · exact (hN irq).1 e + · exact (hN irq).2.1 e + · exact (hN irq).2.2.1 e + · exact (hN irq).2.2.2.1 e + · exact (hN irq).2.2.2.2 e + +theorem gT_irq (irq : Irq) : fIrqNode irq ≠ gT.bv := fIrqNode_ne (by rw [gT_toNat]; omega) irq + +theorem irq_out (irq : Irq) : fIrqNode irq ≠ rT.bv ∧ fIrqNode irq ≠ rS.bv ∧ fIrqNode irq ≠ rC.bv ∧ + fIrqNode irq ≠ gT.bv ∧ fIrqNode irq ∉ SInvU.delR uB.bv 12 := + ⟨rT_irq irq, rS_irq irq, rC_irq irq, gT_irq irq, fun h => irq_not_region h irq rfl⟩ + +theorem idleT_out : idleThreadPtr ≠ rT.bv ∧ idleThreadPtr ≠ rS.bv ∧ idleThreadPtr ≠ rC.bv ∧ + idleThreadPtr ≠ gT.bv ∧ idleThreadPtr ∉ SInvU.delR uB.bv 12 := + ⟨by decide, by decide, by decide, by decide, not_region (by decide)⟩ + +/-! ## `SInvAV` -/ + +theorem tiW {Q : Tcb → Prop} (h0 : SInv.TI Q (initASt (Z := Unit))) (h1 : Q rootTcbW) (h2 : Q guestTcbV) : + SInv.TI Q σw := by + intro p x hp + rcases kh_tcb_of hp with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ + · exact h1 + · exact h2 + · exact h0 _ _ initA_idleT + +theorem w_vfh : SInv.VfhAll σw := tiW SInv.boot_vfhAll (Or.inr rfl) (Or.inr rfl) + +theorem w_cnDom : SInv.CnDom σw := by + intro sl c hc p r g e + subst e + rcases capAtW (capOptW hc) with e | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> cases e + intro l hl + rw [σw_kheap, kh_rC, capOpt_wC] + unfold wCs + split_ifs <;> simp + +theorem w_fts : SInv.FaultTcbsS σw := tiW SInv.boot_faultTcbsS (fun h => absurd rfl h) (fun h => absurd rfl h) + +theorem w_nqb : SInv.NQB σw := by + rintro t ⟨n, x, hn, -⟩ + exact (kh_no_ntfn hn).elim + +theorem w_specGlob : SInvG.SpecGlobS σw ∧ SInvG.IdleOutV σw := by + refine ⟨⟨fun irq a ha => ?_, fun p cref a ha _ irq hr => ?_, fun p cref a ha _ hr => ?_⟩, ?_⟩ + · left + rcases capAtW ha with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact e + · exact absurd (congrArg Prod.fst e) (rT_irq irq) + all_goals exact absurd (congrArg Prod.fst e) (rC_irq irq) + · rcases objRefsW ha hr with e | e | e | e + · exact rT_irq irq e + · exact rS_irq irq e + · exact rC_irq irq e + · exact gT_irq irq e + · rcases objRefsW ha hr with e | e | e | e <;> exact absurd e (by decide) + · exact irqOutW _ (fun _ => idleT_out) + +theorem w_dom : DomListOK σw := SInvDom.domListOK_of_dlV (σ := σ1) (σ' := σw) rfl σ1_dom +theorem w_vim : SInvV.ValidIrqMasks σw := fun _ _ => rfl + +theorem w_vih : SInvV.ValidIrqHandlers σw := by + intro p cref irq hc + rcases capW (capOptW hc) with e | e | e | e | e | e | e <;> cases e + +theorem w_vmt : SInvV.ValidMachineTime σw := σ1_vmt + +theorem w_cdt (x : CslotPtr) : (SInvU.uV σw).2.2.1 x = none := by + simp [SInvU.uV, σw, σ1, fInitASt, initASt, initCdt] + +theorem w_utInv : SInvU.UtInv σw := by + refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · intro sl d b n f hc p hp hm + obtain ⟨-, -, rfl, rfl, rfl⟩ := ut_only hc + have hs : (khW p).isSome = true := hp + obtain ⟨o, ho⟩ := Option.isSome_iff_exists.1 hs + have hm' : uB.bv.toNat + 0 ≤ p.toNat ∧ p.toNat < uB.bv.toNat + 2 ^ 12 := hm + rw [uB_toNat] at hm' + rcases obj_out ho with h | h <;> omega + · intro sl d b n f hc + obtain ⟨-, -, rfl, rfl, rfl⟩ := ut_only hc + refine ⟨by decide, by rw [uB_toNat], by omega⟩ + · intro s1 s2 d1 b1 n1 f1 d2 b2 n2 f2 hne h1 h2 + exact absurd ((ut_only h1).1.trans (ut_only h2).1.symm) hne + · intro s1 s2 d1 b1 n1 f1 d2 b2 n2 f2 hne h1 h2 + exact absurd ((ut_only h1).1.trans (ut_only h2).1.symm) hne + · intro sl d b n f hc + obtain ⟨rfl, -⟩ := ut_only hc + show decide _ = true + simp + +theorem w_utInv' : SInvU.UtInv' σw := by + refine ⟨w_utInv, fun c p h => ?_, fun x hx => ?_⟩ + · rw [w_cdt] at h; cases h + · obtain ⟨c, hc⟩ := SInvU.tg_first hx + rw [w_cdt] at hc; cases hc + +/-- No cap's object references lie in the Untyped region. -/ +theorem rng_out {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) {r : ObjRef} (hr : r ∈ SInvU.capRng c) : + r.toNat < 18446744071562117120 := by + rcases capW h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.capRng, objRefs] at hr <;> subst hr <;> + decide + +theorem w_utNV : SInvU.UtNV (SInvU.uV σw) := by + have hc : ∀ sl c, (SInvU.uV σw).2.1 sl = some c → SInvU.capAt σw sl = some c := fun _ _ h => h + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro u dev b n fi hu s c r hs hr hd + obtain ⟨-, -, rfl, rfl, -⟩ := ut_only (hc _ _ hu) + exact absurd hd (not_region (rng_out (hc _ _ hs) hr)) + · rintro p c hp hne ⟨u, dev, b, n, fi, hu, hm⟩ + obtain ⟨-, -, rfl, rfl, -⟩ := ut_only (hc _ _ hu) + rcases capAtW (hc _ _ hp) with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + all_goals + subst e + exact absurd hm (not_region (by dsimp only; decide)) + · intro u dev b n fi hu s c r hs hr hm + obtain ⟨-, -, rfl, rfl, rfl⟩ := ut_only (hc _ _ hu) + have hm' : uB.bv.toNat + 0 ≤ r.toNat ∧ r.toNat < uB.bv.toNat + 2 ^ 12 := hm + rw [uB_toNat] at hm' + have := rng_out (hc _ _ hs) hr + omega + · intro s c s' c' r hs hs' hr hr' + rcases capW (hc _ _ hs) with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + rcases capW (hc _ _ hs') with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + simp [SInvU.capRng, SInvU.sRefs, objRefs] at hr hr' <;> (try subst hr) <;> + first | rfl | (exact absurd hr' (by decide)) | (exact absurd hr'.symm (by decide)) | + (rw [hr] at hr'; exact absurd hr' (by decide)) + · intro s c r hs hr i c' hc' hne + rcases capW (hc _ _ hs) with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.sRefs] at hr <;> subst hr + · rcases capAtW (hc _ _ hc') with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + · exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + all_goals cases e; rfl + · rcases capAtW (hc _ _ hc') with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + · cases e; exact ⟨0, by decide, rfl⟩ + all_goals exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + · rcases capAtW (hc _ _ hc') with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + all_goals exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + · intro s r b n hs; rcases capW (hc _ _ hs) with e | e | e | e | e | e | e <;> cases e + · intro s c hs b hb + rcases capW (hc _ _ hs) with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.shp] at hb + omega + +theorem w_utInv'' : SInvU.UtInv'' σw := ⟨w_utInv', w_utNV⟩ + +theorem w_utInv3 : SInvU.UtInv3 σw := ⟨w_utInv'', irqOutW _ irq_out⟩ + +theorem w_ntfnQB : SInv.NtfnQB σw := fun _ _ _ hn _ => (kh_no_ntfn hn).elim +theorem w_epQB : SInv.EpQB σw := fun _ _ _ hn _ => (kh_no_ep hn).elim + +theorem w_thr : SInvU.capAt σw (rC.bv, [false, false, false]) = some (.threadCap rT.bv) := capAt_slot rfl +theorem w_scc : SInvU.capAt σw (rC.bv, [false, false, true]) = some (.schedContextCap rS.bv 0) := capAt_slot rfl +theorem w_ut : SInvU.capAt σw (rC.bv, [false, true, false]) = some utW := capAt_slot rfl +theorem w_sctl : SInvU.capAt σw (rC.bv, [false, true, true]) = some .schedControlCap := capAt_slot rfl +theorem w_gthr : SInvU.capAt σw (rC.bv, [true, false, false]) = some (.threadCap gT.bv) := capAt_slot rfl + +theorem w_ifLiveT : SInv.IfLiveT σw := by + intro t y ht hl + rcases kh_tcb_of ht with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, rfl⟩ + · exact ⟨_, w_thr⟩ + · exact ⟨_, w_gthr⟩ + · exact False.elim hl + +theorem w_validIdle : SInvI.ValidIdle σw := by + unfold SInvI.ValidIdle SInvI.VI SInvI.VIT + refine ⟨rfl, rfl, fun e => absurd e Bool.false_ne_true, ⟨_, kh_idleT⟩, ⟨_, 0, kh_idleS⟩, ?_⟩ + intro p o hp + change SInvI.ObjOK idleThreadPtr idleScPtr idleThreadPtr p o + rcases kh_some hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h' + · refine ⟨fun e => absurd e (by decide), fun _ => ?_, fun e => absurd e (by decide)⟩ + simp only [rootTcbW, rootTcbV, ne_eq, Option.some.injEq]; decide + · refine ⟨fun e => absurd e (by decide), fun _ => ?_⟩ + simp only [rootScV, ne_eq, Option.some.injEq]; decide + · trivial + · refine ⟨fun e => absurd e (by decide), fun _ => ?_, fun e => absurd e (by decide)⟩ + simp [guestTcbV] + · exact (BootHeap.validIdle (N := fIrqNode) bootHeap_fInit).2.2.2.2.2 _ _ h' + +theorem w_ft : SInvF.FT σw := fun t ht => by + simp [σw, σ1, fInitASt, initASt, initArchState] at ht + +theorem w_scLiveT : SInvL.ScLiveT (SInvL.idleP σw) σw := by + intro p o hp hl h1 h2 + rcases kh_some hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h' + · exact ⟨_, _, w_thr, rfl⟩ + · exact ⟨_, _, w_scc, rfl⟩ + · exact hl.elim + · exact absurd rfl hl + · exfalso + rcases bootHeap_fInit.obj _ _ h' with rfl | rfl | rfl | rfl | rfl | rfl + · exact h1 rfl + · exact h2 rfl + all_goals exact hl + +theorem w_idleScOutV : SInvL.IdleScOutV σw := + irqOutW _ (fun _ => ⟨by show idleScPtr ≠ _; decide, by show idleScPtr ≠ _; decide, by show idleScPtr ≠ _; decide, + by show idleScPtr ≠ _; decide, not_region (by show idleScPtr.toNat < _; decide)⟩) + +theorem w_ntfnScB : SInvL.NtfnScB σw := fun _ _ _ hn _ => (kh_no_ntfn hn).elim + +theorem w_scInv : SInvB.ScInv σw := by + refine ⟨⟨fun u xu c hu hc => ?_, fun p y n u hp hu => ?_⟩, fun p y n hp => ?_⟩ + · rcases kh_tcb_of hu with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · cases hc; exact ⟨rootScV, 0, kh_rS, rfl, idleSc_rne⟩ + · cases hc + · cases hc; exact ⟨idleScV, 0, kh_idleS, rfl, idleSc_rne⟩ + · rcases kh_sc_of hp with ⟨rfl, rfl, -⟩ | ⟨rfl, rfl, -⟩ + · cases hu; exact ⟨rootTcbW, kh_rT, rfl⟩ + · cases hu; exact ⟨idleTcbV, kh_idleT, rfl⟩ + · rcases kh_sc_of hp with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_rne + · exact idleSc_rne + +theorem w_scKindR : SInvL.ScKindR σw := by + intro s r b s' c' h1 h2 hr + have h1' : SInvU.capAt σw s = some (.schedContextCap r b) := h1 + rcases capW h1' with e | e | e | e | e | e | e <;> cases e + rcases capW h2 with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.capRng, objRefs] at hr + all_goals first | exact ⟨0, rfl⟩ | exact absurd hr (by decide) + +theorem w_curScT : SInvQ.CurScT σw := by + intro x hx + have hx' : khW rT.bv = some (.tcb x) := hx + rw [kh_rT] at hx'; cases hx'; rfl + +theorem w_sinvAV : SInvAll.SInvAV σw where + vfh := w_vfh + cnDom := w_cnDom + fts := w_fts + nqb := w_nqb + sg := w_specGlob.1 + idleOut := w_specGlob.2 + dom := w_dom + vim := w_vim + vih := w_vih + vmt := w_vmt + ut := w_utInv3 + nq := w_ntfnQB + epq := w_epQB + live := w_ifLiveT + vi := w_validIdle + ft := w_ft + sl := ⟨⟨w_scLiveT, w_idleScOutV, w_ntfnScB, w_scInv⟩, w_scKindR⟩ + cs := w_curScT + +/-! ## The rest of `SInvXV` -/ + +theorem w_ctExit : SInvAll.CtExit σw := ⟨_, kh_rT, Or.inl rfl⟩ + +theorem w_onlyIdle : SInvAll.OnlyIdle σw := by + intro t x ht hs + rcases kh_tcb_of ht with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · cases hs + · cases hs + · rfl + +theorem w_ex : SInvQ.EX σw := + ⟨⟨⟨fun _ _ _ h => (List.not_mem_nil h).elim, trivial⟩, + ⟨fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, + fun _ _ _ h => List.not_mem_nil h⟩⟩, + ⟨fun _ _ h => by simp [σw, σ1, fInitASt, initASt] at h, + ⟨fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, w_curScT⟩⟩⟩ + +theorem w_rb (k : Bool) : SInvQ.RBk k σw := by + refine ⟨⟨⟨fun _ _ _ h => by simp [σw, σ1, fInitASt, initASt] at h, + fun _ h => by simp [σw, σ1, fInitASt, initASt] at h⟩, + fun _ => ⟨fun _ _ h => by simp [σw, σ1, fInitASt, initASt] at h, + ⟨fun h => by simp [σw, σ1, fInitASt, initASt] at h, fun h => by simp [σw, σ1, fInitASt, initASt] at h⟩⟩, + fun _ h => by simp [σw, σ1, fInitASt, initASt] at h⟩, + fun _ => Or.inl ⟨rootTcbW, kh_rT, rfl⟩⟩ + +theorem w_t4 : SInvQ.T4 σw := + ⟨SInvQ.boot_t4.1, rfl, tiW SInvQ.boot_t4.2.2 + (by have h := SInvQ.boot_t4.2.2 idleThreadPtr idleTcbV initA_idleT; exact h) + (by have h := SInvQ.boot_t4.2.2 idleThreadPtr idleTcbV initA_idleT; exact h)⟩ + +theorem w_bsa : Sched.BlockedScActive σw := by + refine ⟨fun p y n t x hp hyt hx hq => ?_, fun p y n hp hr => ?_⟩ + · rcases kh_sc_of hp with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_active + · exact idleSc_active + · rcases kh_sc_of hp with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_active + · exact idleSc_active + +theorem w_vb : SInvR.B8n.VB σw := by + intro t hs hne _ + obtain ⟨x, c, -, -, hx, hc, hr, -, -, -⟩ := Sched.schedulable_inv hs + rcases kh_tcb_of hx with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd rfl hne + · simp [guestTcbV] at hc + · simp [idleTcbV, runnable] at hr + +theorem w_rdy : SInvR.Rdy σw := by + refine ⟨fun d p t h => by simp [σw, σ1, fInitASt, initASt] at h, + fun t h => by simp [σw, σ1, fInitASt, initASt] at h, + fun t x c hx hb _ => ?_, fun p y n hy ha => ?_, fun t h => by simp [σw, σ1, fInitASt, initASt] at h, + fun y n hy => ?_⟩ + · exfalso + rcases kh_tcb_of hx with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ + · have e : rootTcbW.tcbState = .running := rfl + rcases hb with ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ <;> rw [e] at h <;> cases h + · have e : guestTcbV.tcbState = .runningVM := rfl + rcases hb with ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ <;> rw [e] at h <;> cases h + · have e : idleTcbV.tcbState = .idleThreadState := rfl + rcases hb with ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ <;> rw [e] at h <;> cases h + · rcases kh_sc_of hy with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_rvs + · exact idleSc_rvs + · have hy' : khW idleScPtr = some (.schedContext y n) := hy + rw [kh_idleS] at hy'; cases hy' + exact SInvR.boot_rdy.2.2.2.2.2 _ _ initA_idleS + +theorem w_offReady : Sched.OffReady σw := by + intro y n hy + have hy' : khW rS.bv = some (.schedContext y n) := hy + rw [kh_rS] at hy'; cases hy' + exact SInvV.boot_offReady idleScV 0 initA_idleS + +theorem w_cAx : SInvR.CAx σw := fun _ => ⟨rootTcbW, rS.bv, rootScV, 0, kh_rT, rfl, kh_rS, idleSc_active⟩ + +theorem w_rpos : SInvE.RPos σw := by + intro p y n hy + rcases kh_sc_of hy with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact SInvE.boot_rpos idleScPtr idleScV 0 initA_idleS + · exact SInvE.boot_rpos idleScPtr idleScV 0 initA_idleS + +theorem w_sinvXV : SInvR.G4.SInvXV σw := + ⟨⟨⟨⟨⟨⟨⟨⟨w_sinvAV, w_ctExit⟩, w_onlyIdle⟩, w_ex, w_rb true, w_t4⟩, rfl⟩, w_bsa⟩, w_vb⟩, + w_rdy, w_offReady, w_cAx⟩, w_rpos⟩ + +/-! ## The other spec fields of `StartS3` -/ + +theorem w_ports : SpecIoPorts σw := by + intro sl f l o h + obtain ⟨p, i⟩ := sl + have hc := capOptW ((getCap_pair_iff σw p i _).1 h) + rcases capW hc with e | e | e | e | e | e | e <;> cases e + +theorem w_utZ : SInvR.G5.SpecUtZ σw := fun _ _ _ _ _ _ _ _ => rfl + +theorem w_devRange : SInvDV.DevRangeS σw := by + intro p cref b n f hc + cases (ut_only (capOptW hc)).2.1 + +theorem w_gpmOut : SInvGp.GpmOutV σw := + irqOutW _ (fun _ => ⟨by show initGlobalPml4 ≠ _; decide, by show initGlobalPml4 ≠ _; decide, + by show initGlobalPml4 ≠ _; decide, by show initGlobalPml4 ≠ _; decide, + not_region (by show initGlobalPml4.toNat < _; decide)⟩) + +theorem w_rv : SInvR.RV σw := fun p y n hy ha => w_rdy.2.2.2.1 p y n hy ha + +theorem w_idleVtS : SInvIV.IdleVtS σw := by + intro a ha + rcases capAtW (sl := (σw.idleThread, tcbCnodeIndex 1)) ha with + e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact e + all_goals exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + +theorem w_nonIdle : σw.curThread ≠ σw.idleThread := σ1_nonIdle + +/-- **The spec fields of `StartS3` hold together at a state with an Untyped cap and a SchedControl cap in the current +thread's CSpace and a `runningVM` TCB** (module docstring). -/ +theorem startSpec_witness : + (SInvR.G4.SInvXV σw ∧ σw.curThread ≠ σw.idleThread ∧ SpecIoPorts σw ∧ SInvR.G5.SpecUtZ σw ∧ + SInvDV.DevRangeS σw ∧ SInvGp.GpmOutV σw ∧ SInvU.UtInv'' σw ∧ SInvR.RV σw ∧ SInvIV.IdleVtS σw) ∧ + (∃ x, σw.kheap σw.curThread = some (.tcb x) ∧ x.tcbCtable = cnW) ∧ + SInvU.capAt σw (rC.bv, [false, true, false]) = some utW ∧ + SInvU.capAt σw (rC.bv, [false, true, true]) = some .schedControlCap ∧ + (∃ t x, σw.kheap t = some (.tcb x) ∧ x.tcbState = .runningVM) := + ⟨⟨w_sinvXV, w_nonIdle, w_ports, w_utZ, w_devRange, w_gpmOut, w_utInv'', w_rv, w_idleVtS⟩, + ⟨rootTcbW, kh_rT, rfl⟩, w_ut, w_sctl, ⟨gT.bv, guestTcbV, kh_gT, rfl⟩⟩ + +/-- The native bundle is false at `σw`: it carries `NativeScopeS`. -/ +theorem σw_not_sinvX : ¬ SInvR.G4.SInvX σw := + fun h => h.sb.1.1.ns gT.bv guestTcbV kh_gT rfl + +end + +end Fl4Proofs.P3.JBootQ.XVWit diff --git a/proofs/Fl4Proofs/P3/SpecFbCopyRun.lean b/proofs/Fl4Proofs/P3/SpecFbCopyRun.lean index c4f26faeb..67cbb069d 100644 --- a/proofs/Fl4Proofs/P3/SpecFbCopyRun.lean +++ b/proofs/Fl4Proofs/P3/SpecFbCopyRun.lean @@ -49,7 +49,7 @@ variable {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {τ : State Un theorem fbEntry_copy_destErr (hop : b.op = .copyMap) {e : SyscallError} (h : (.error e, τ) ∈ ((lookupTargetSlot (Z := Unit) b.destCnode (dataToCptr w.2.1) b.depth).run τ).1) : (.error (msgFromSyscallError e).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_err (fbr_code_err h) /-- The destination is not empty. -/ @@ -57,7 +57,7 @@ theorem fbEntry_copy_full (hop : b.op = .copyMap) {dsl : CslotPtr} (hd : (.ok dsl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.destCnode (dataToCptr w.2.1) b.depth).run τ).1) (he : (.error .deleteFirst, τ) ∈ ((ensureEmpty (Z := Unit) dsl).run τ).1) : (.error (msgFromSyscallError .deleteFirst).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_ok (fbr_code_ok hd) (fbr_bind_err (fbr_code_err he)) /-- The source's lookup fails. -/ @@ -66,7 +66,7 @@ theorem fbEntry_copy_srcErr (hop : b.op = .copyMap) {dsl : CslotPtr} {e : Syscal (he : (.ok (), τ) ∈ ((ensureEmpty (Z := Unit) dsl).run τ).1) (h : (.error e, τ) ∈ ((lookupSourceSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) : (.error (msgFromSyscallError e).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_ok (fbr_code_ok hd) (fbr_bind_ok (fbr_code_ok he) (fbr_bind_err (fbr_code_err h))) /-- The source's cap is no frame. -/ @@ -76,7 +76,7 @@ theorem fbEntry_copy_notPage (hop : b.op = .copyMap) {dsl ssl : CslotPtr} {sc : (hs : (.ok ssl, τ) ∈ ((lookupSourceSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) (hg : (sc, τ) ∈ (getCap (Z := Unit) ssl τ).1) (hp : ¬ isPageCap sc = true) : (.error (msgFromSyscallError (.invalidCapability 0)).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_ok (fbr_code_ok hd) (fbr_bind_ok (fbr_code_ok he) (fbr_bind_ok (fbr_code_ok hs) (fbr_bind_ok (fbr_liftE hg) (fbr_bind_err (fbr_code_err (fbr_unless_err hp (fbr_throw _ τ))))))) @@ -88,7 +88,7 @@ theorem fbEntry_copy_deriveErr (hop : b.op = .copyMap) {dsl ssl : CslotPtr} {sc (hg : (sc, τ) ∈ (getCap (Z := Unit) ssl τ).1) (hp : isPageCap sc = true) (hv : (.error e, τ) ∈ ((deriveCap (Z := Unit) ssl (maskCap (dataToRights b.capRights) sc)).run τ).1) : (.error (msgFromSyscallError e).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_ok (fbr_code_ok hd) (fbr_bind_ok (fbr_code_ok he) (fbr_bind_ok (fbr_code_ok hs) (fbr_bind_ok (fbr_liftE hg) (fbr_bind_ok (fbr_code_ok (fbr_unless_ok hp _ τ)) (fbr_bind_err (fbr_code_err hv)))))) @@ -103,7 +103,7 @@ theorem fbEntry_copy_ok (hop : b.op = .copyMap) {dsl ssl : CslotPtr} {sc nc : Ca (hi : ((), τ1) ∈ (capInsert (Z := Unit) nc ssl dsl τ).1) (hm : (.ok (), τ2) ∈ ((fbMap (Z := Unit) b dsl w.2.2).run τ1).1) : (.ok false, τ2) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_ok (fbr_code_ok hd) (fbr_bind_ok (fbr_code_ok he) (fbr_bind_ok (fbr_code_ok hs) (fbr_bind_ok (fbr_liftE hg) (fbr_bind_ok (fbr_code_ok (fbr_unless_ok hp _ τ)) (fbr_bind_ok (fbr_code_ok hv) (fbr_bind_ok (fbr_liftE hi) @@ -121,7 +121,7 @@ theorem fbEntry_copy_mapErr (hop : b.op = .copyMap) {dsl ssl : CslotPtr} {sc nc (hm : (.error e, τ2) ∈ ((fbMap (Z := Unit) b dsl w.2.2).run τ1).1) (hdl : (.ok (), τ3) ∈ ((capDelete (Z := Unit) dsl).run τ2).1) : (.error (msgFromSyscallError e).1, τ3) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + unfold fbEntry fbEntryWith fbDeferDelete; rw [hop] exact fbr_bind_ok (fbr_code_ok hd) (fbr_bind_ok (fbr_code_ok he) (fbr_bind_ok (fbr_code_ok hs) (fbr_bind_ok (fbr_liftE hg) (fbr_bind_ok (fbr_code_ok (fbr_unless_ok hp _ τ)) (fbr_bind_ok (fbr_code_ok hv) (fbr_bind_ok (fbr_liftE hi) diff --git a/proofs/Fl4Proofs/P3/SpecFbDelRun.lean b/proofs/Fl4Proofs/P3/SpecFbDelRun.lean index a3b0375dd..db127306a 100644 --- a/proofs/Fl4Proofs/P3/SpecFbDelRun.lean +++ b/proofs/Fl4Proofs/P3/SpecFbDelRun.lean @@ -40,36 +40,51 @@ theorem fbr_code_err {m : SEMonad Unit α} {s t : State Unit} {e : SyscallError} (.error (msgFromSyscallError e).1, t) ∈ ((fbCode (Z := Unit) m).run s).1 := by unfold fbCode NonDetE.handleE; rw [ExceptT.run_mk]; exact ⟨_, h, Set.mem_singleton _⟩ +theorem fbr_deleteWith_ok {del : CslotPtr → PMonad Unit Unit} {sl : CslotPtr} {s t : State Unit} + (h : (.ok (), t) ∈ ((del sl).run s).1) : (.ok (), t) ∈ ((fbDeleteWith del sl).run s).1 := by + unfold fbDeleteWith NonDetE.handleE; rw [ExceptT.run_mk]; exact ⟨_, h, Set.mem_singleton _⟩ + theorem fbr_delete_ok {sl : CslotPtr} {s t : State Unit} (h : (.ok (), t) ∈ ((capDelete (Z := Unit) sl).run s).1) : - (.ok (), t) ∈ ((fbDelete (Z := Unit) sl).run s).1 := by - unfold fbDelete NonDetE.handleE; rw [ExceptT.run_mk]; exact ⟨_, h, Set.mem_singleton _⟩ + (.ok (), t) ∈ ((fbDelete (Z := Unit) sl).run s).1 := + fbr_deleteWith_ok h end variable {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {τ : State Unit} -/-- The target slot's lookup fails. -/ -theorem fbEntry_del_lookupErr (hop : b.op = .delete) {e : SyscallError} +/-- The target slot's lookup fails before finalisation. -/ +theorem fbEntryWith_del_lookupErr {del : CslotPtr → PMonad Unit Unit} (hop : b.op = .delete) {e : SyscallError} (h : (.error e, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) : - (.error (msgFromSyscallError e).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + (.error (msgFromSyscallError e).1, τ) ∈ ((fbEntryWith del b w).run τ).1 := by + unfold fbEntryWith; rw [hop] exact fbr_bind_err (fbr_code_err h) -/-- A null target slot. -/ -theorem fbEntry_del_null (hop : b.op = .delete) {sl : CslotPtr} +theorem fbEntry_del_lookupErr (hop : b.op = .delete) {e : SyscallError} + (h : (.error e, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) : + (.error (msgFromSyscallError e).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := + fbEntryWith_del_lookupErr hop h + +/-- A null target slot succeeds without finalisation. -/ +theorem fbEntryWith_del_null {del : CslotPtr → PMonad Unit Unit} (hop : b.op = .delete) {sl : CslotPtr} (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) (hg : (.nullCap, τ) ∈ (getCap (Z := Unit) sl τ).1) : - (.ok false, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + (.ok false, τ) ∈ ((fbEntryWith del b w).run τ).1 := by + unfold fbEntryWith; rw [hop] exact fbr_bind_ok (fbr_code_ok h) (fbr_bind_ok (fbr_liftE hg) (fbr_ret _)) +theorem fbEntry_del_null (hop : b.op = .delete) {sl : CslotPtr} + (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) + (hg : (.nullCap, τ) ∈ (getCap (Z := Unit) sl τ).1) : + (.ok false, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := + fbEntryWith_del_null hop h hg + /-- A cap neither null nor a frame. -/ -theorem fbEntry_del_other (hop : b.op = .delete) {sl : CslotPtr} {c : Cap} +theorem fbEntryWith_del_other {del : CslotPtr → PMonad Unit Unit} (hop : b.op = .delete) {sl : CslotPtr} {c : Cap} (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) (hg : (c, τ) ∈ (getCap (Z := Unit) sl τ).1) (hn : c ≠ .nullCap) (hf : ∀ dev p R mt sz m, c ≠ .archObjectCap (.pageCap dev p R mt sz m)) : - (.error (msgFromSyscallError .illegalOperation).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + (.error (msgFromSyscallError .illegalOperation).1, τ) ∈ ((fbEntryWith del b w).run τ).1 := by + unfold fbEntryWith; rw [hop] refine fbr_bind_ok (fbr_code_ok h) (fbr_bind_ok (fbr_liftE hg) ?_) cases c all_goals first @@ -78,42 +93,69 @@ theorem fbEntry_del_other (hop : b.op = .delete) {sl : CslotPtr} {c : Cap} | (rename_i ac cases ac <;> first | exact Set.mem_singleton _ | exact absurd rfl (hf _ _ _ _ _ _)) -/-- A frame deleted without the deferred unmap. -/ -theorem fbEntry_del_frame (hop : b.op = .delete) {sl : CslotPtr} {dev : Bool} {p : ObjRef} {R : CapRights} +theorem fbEntry_del_other (hop : b.op = .delete) {sl : CslotPtr} {c : Cap} + (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) + (hg : (c, τ) ∈ (getCap (Z := Unit) sl τ).1) (hn : c ≠ .nullCap) + (hf : ∀ dev p R mt sz m, c ≠ .archObjectCap (.pageCap dev p R mt sz m)) : + (.error (msgFromSyscallError .illegalOperation).1, τ) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := + fbEntryWith_del_other hop h hg hn hf + +/-- A frame deleted without the deferred unmap, using the selected finaliser. -/ +theorem fbEntryWith_del_frame {del : CslotPtr → PMonad Unit Unit} + (hop : b.op = .delete) {sl : CslotPtr} {dev : Bool} {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} {τ1 : State Unit} (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) (hg : (.archObjectCap (.pageCap dev p R mt sz m), τ) ∈ (getCap (Z := Unit) sl τ).1) (hnd : ∀ asid' vaddr, m = some (asid', vaddr) → ¬ (b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid)) - (hd : (.ok (), τ1) ∈ ((capDelete (Z := Unit) sl).run τ).1) : - (.ok false, τ1) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + (hd : (.ok (), τ1) ∈ ((del sl).run τ).1) : + (.ok false, τ1) ∈ ((fbEntryWith del b w).run τ).1 := by + unfold fbEntryWith fbDeferDelete; rw [hop] refine fbr_bind_ok (fbr_code_ok h) (fbr_bind_ok (fbr_liftE hg) ?_) dsimp only rcases m with _ | ⟨a', v⟩ <;> dsimp only - · first - | exact fbr_bind_ok (fbr_delete_ok hd) (fbr_ret false) - | exact fbr_bind_ok (fbr_ret false) (fbr_bind_ok (fbr_delete_ok hd) (fbr_ret false)) + · exact fbr_bind_ok (fbr_ret false) (fbr_bind_ok (fbr_deleteWith_ok hd) (fbr_ret false)) · rw [if_neg (hnd a' v rfl)] - first - | exact fbr_bind_ok (fbr_delete_ok hd) (fbr_ret false) - | exact fbr_bind_ok (fbr_ret false) (fbr_bind_ok (fbr_delete_ok hd) (fbr_ret false)) + exact fbr_bind_ok (fbr_ret false) (fbr_bind_ok (fbr_deleteWith_ok hd) (fbr_ret false)) + +/-- A frame deleted without the deferred unmap. -/ +theorem fbEntry_del_frame (hop : b.op = .delete) {sl : CslotPtr} {dev : Bool} {p : ObjRef} {R : CapRights} + {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} {τ1 : State Unit} + (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) + (hg : (.archObjectCap (.pageCap dev p R mt sz m), τ) ∈ (getCap (Z := Unit) sl τ).1) + (hnd : ∀ asid' vaddr, m = some (asid', vaddr) → + ¬ (b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid)) + (hd : (.ok (), τ1) ∈ ((capDelete (Z := Unit) sl).run τ).1) : + (.ok false, τ1) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := + fbEntryWith_del_frame hop h hg hnd hd /-- A frame deleted after the deferred unmap. -/ -theorem fbEntry_del_frameD (hop : b.op = .delete) {sl : CslotPtr} {dev : Bool} {p : ObjRef} {R : CapRights} +theorem fbEntryWith_del_frameD {del : CslotPtr → PMonad Unit Unit} + (hop : b.op = .delete) {sl : CslotPtr} {dev : Bool} {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} {asid' vaddr : Data} {τu τs τ1 : State Unit} {r : Bool} (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) (hg : (.archObjectCap (.pageCap dev p R mt sz (some (asid', vaddr))), τ) ∈ (getCap (Z := Unit) sl τ).1) (hdf : b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid) (hu : (r, τu) ∈ (unmapPageUnflushed (Z := Unit) sz b.vspace vaddr p τ).1) (hs : ((), τs) ∈ (setCap (Z := Unit) (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) sl τu).1) - (hd : (.ok (), τ1) ∈ ((capDelete (Z := Unit) sl).run τs).1) : - (.ok r, τ1) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := by - unfold fbEntry; rw [hop] + (hd : (.ok (), τ1) ∈ ((del sl).run τs).1) : + (.ok r, τ1) ∈ ((fbEntryWith del b w).run τ).1 := by + unfold fbEntryWith fbDeferDelete; rw [hop] refine fbr_bind_ok (fbr_code_ok h) (fbr_bind_ok (fbr_liftE hg) ?_) dsimp only rw [if_pos hdf] - exact fbr_bind_ok (fbr_liftE ⟨_, hu, _, hs, Set.mem_singleton _⟩) (fbr_bind_ok (fbr_delete_ok hd) (fbr_ret r)) + exact fbr_bind_ok (fbr_liftE ⟨_, hu, _, hs, Set.mem_singleton _⟩) (fbr_bind_ok (fbr_deleteWith_ok hd) (fbr_ret r)) + +theorem fbEntry_del_frameD (hop : b.op = .delete) {sl : CslotPtr} {dev : Bool} {p : ObjRef} {R : CapRights} + {mt : VMMapType} {sz : VMPageSize} {asid' vaddr : Data} {τu τs τ1 : State Unit} {r : Bool} + (h : (.ok sl, τ) ∈ ((lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth).run τ).1) + (hg : (.archObjectCap (.pageCap dev p R mt sz (some (asid', vaddr))), τ) ∈ (getCap (Z := Unit) sl τ).1) + (hdf : b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid) + (hu : (r, τu) ∈ (unmapPageUnflushed (Z := Unit) sz b.vspace vaddr p τ).1) + (hs : ((), τs) ∈ (setCap (Z := Unit) (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) sl τu).1) + (hd : (.ok (), τ1) ∈ ((capDelete (Z := Unit) sl).run τs).1) : + (.ok r, τ1) ∈ ((fbEntry (Z := Unit) b w).run τ).1 := + fbEntryWith_del_frameD hop h hg hdf hu hs hd end diff --git a/proofs/Fl4Proofs/P3/SpecFbRestart.lean b/proofs/Fl4Proofs/P3/SpecFbRestart.lean index 1885e0ba3..db302431b 100644 --- a/proofs/Fl4Proofs/P3/SpecFbRestart.lean +++ b/proofs/Fl4Proofs/P3/SpecFbRestart.lean @@ -77,18 +77,19 @@ theorem fbs_stsAct_sa (s : State Unit) (t : ObjRef) : (Notif.stsAct s t).schedul else s.schedulerAction := by unfold Notif.stsAct; split_ifs <;> rfl -/-- **The batch after `Restart`.** -/ -theorem performFrameBatch_restart (b : Fl4FrameBatchInvocation) {σ τ : State Unit} {ct : ObjRef} {x : Tcb} - (hk : σ.kheap ct = some (.tcb x)) {r : List Data} (hx : (r, τ) ∈ (performFrameBatch (Z := Unit) b σ).1) : +/-- Replay an outcome after Restart when the operation preserves the +scheduler's view and commutes with changes to the caller and action. -/ +theorem frameBatch_restart_of {m : SMonad Unit (List Data)} {σ τ : State Unit} {ct : ObjRef} {x : Tcb} + (hk : σ.kheap ct = some (.tcb x)) {r : List Data} (hx : (r, τ) ∈ (m σ).1) + (hcm : CM ct m) (hsv : ∀ q, SVK.sv (τ.kheap q) = SVK.sv (σ.kheap q)) + (hF : fbsF τ = fbsF σ) : ∃ y, τ.kheap ct = some (.tcb y) ∧ y.tcbSchedContext = x.tcbSchedContext ∧ (r, Notif.stsAct (Notif.updK τ ct (.tcb { y with tcbState := .restart })) ct) ∈ - (performFrameBatch (Z := Unit) b (Notif.stsAct (Notif.updK σ ct (.tcb { x with tcbState := .restart })) ct)).1 := by - have hsv := SInvR.A13b.svfb_performFrameBatch b σ _ hx - have hF := fbsF_performFrameBatch b σ _ hx + (m (Notif.stsAct (Notif.updK σ ct (.tcb { x with tcbState := .restart })) ct)).1 := by simp only [fbsF, Prod.mk.injEq] at hF obtain ⟨hF1, hF2, hF3⟩ := hF have h1 : SVK.sv (τ.kheap ct) = (1, x.tcbSchedContext, [], 0, none, 0) := by - rw [hsv.1 ct, hk]; rfl + rw [hsv ct, hk]; rfl obtain ⟨y, hy, hysc⟩ := SVK.sv_tcb h1 refine ⟨y, hy, hysc, ?_⟩ rw [stsAct_updK_fbsM hk, stsAct_updK_fbsM hy] @@ -100,7 +101,7 @@ theorem performFrameBatch_restart (b : Fl4FrameBatchInvocation) {σ τ : State U (by simp [Notif.updK]) (by simp [Notif.updK]) rfl hysc (fun q => ?_) hF3 by_cases hq : q = ct · subst hq; simp [Notif.updK, SVK.sv, hysc] - · simp only [Notif.updK, Function.update_of_ne hq]; exact hsv.1 q + · simp only [Notif.updK, Function.update_of_ne hq]; exact hsv q have c1 : (Notif.updK τ ct (.tcb { y with tcbState := .restart })).curThread = σ.curThread := hF2 have c2 : (Notif.updK σ ct (.tcb { x with tcbState := .restart })).curThread = σ.curThread := rfl have c3 : (Notif.updK τ ct (.tcb { y with tcbState := .restart })).schedulerAction = σ.schedulerAction := hF1 @@ -108,7 +109,16 @@ theorem performFrameBatch_restart (b : Fl4FrameBatchInvocation) {σ τ : State U rw [fbs_stsAct_sa, fbs_stsAct_sa] simp only [hsch, c1, c2, c3, c4] rw [hsa] - exact performFrameBatch_fbsM ct b σ .restart _ (r, τ) hx + exact hcm .restart _ σ (r, τ) hx + +/-- **The batch after `Restart`.** -/ +theorem performFrameBatch_restart (b : Fl4FrameBatchInvocation) {σ τ : State Unit} {ct : ObjRef} {x : Tcb} + (hk : σ.kheap ct = some (.tcb x)) {r : List Data} (hx : (r, τ) ∈ (performFrameBatch (Z := Unit) b σ).1) : + ∃ y, τ.kheap ct = some (.tcb y) ∧ y.tcbSchedContext = x.tcbSchedContext ∧ + (r, Notif.stsAct (Notif.updK τ ct (.tcb { y with tcbState := .restart })) ct) ∈ + (performFrameBatch (Z := Unit) b (Notif.stsAct (Notif.updK σ ct (.tcb { x with tcbState := .restart })) ct)).1 := + frameBatch_restart_of hk hx (fun ts sa s y h => performFrameBatch_fbsM ct b s ts sa y h) + (SInvR.A13b.svfb_performFrameBatch b σ _ hx).1 (fbsF_performFrameBatch b σ _ hx) end diff --git a/proofs/Fl4Proofs/P3/SpecFbSim.lean b/proofs/Fl4Proofs/P3/SpecFbSim.lean index a0bf5bf4c..00061d3d0 100644 --- a/proofs/Fl4Proofs/P3/SpecFbSim.lean +++ b/proofs/Fl4Proofs/P3/SpecFbSim.lean @@ -694,7 +694,7 @@ theorem fbEntry_supX (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s cases op case remap => exact fun y hy => hy case copyMap => - unfold fbEntry fbEntryX + unfold fbEntry fbEntryWith fbDeferDelete fbEntryX dsimp only refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun dest t0 _ => ?_ refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun _ t1 _ => ?_ @@ -735,7 +735,7 @@ theorem fbEntry_supX (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s all_goals exact (h5 : False).elim all_goals exact (h5 : False).elim case delete => - unfold fbEntry fbEntryX + unfold fbEntry fbEntryWith fbDeferDelete fbEntryX dsimp only refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun slot t0 _ => ?_ refine fbx_sub_bindE (fun c t => capAt t slot = some c) (fun c t h => ?_) fun c t1 h1 => ?_ @@ -760,7 +760,7 @@ theorem fbEntry_supX (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s · exact ⟨_, _, _, _, _, _, h2⟩ · exact ⟨_, _, _, _, _, _, h1⟩ obtain ⟨_, _, _, _, _, _, hc2⟩ := hc2 - exact fbx_sub_bindE2 (by unfold fbDelete fbDeleteX; exact fbx_sub_handleE (capDelete_supX hc2)) + exact fbx_sub_bindE2 (by unfold fbDelete fbDeleteWith fbDeleteX; exact fbx_sub_handleE (capDelete_supX hc2)) (fun _ _ => True) (fun _ _ _ => trivial) fun _ _ _ => fun y hy => hy dsimp only rcases m with _ | ⟨asid', vaddr⟩ @@ -791,7 +791,7 @@ theorem fbRun_supX (b : Fl4FrameBatchInvocation) : ∀ y ∈ (fbRunX b ws n d s).1, y ∈ (fbRun (Z := Unit) b ws n d s).1 | [], n, d, s => fun y hy => hy | w :: ws, n, d, s => by - unfold fbRunX fbRun + unfold fbRunX fbRun fbRunWith refine fbx_sub_bind (fbEntry_supX b w s) fun r t _ => ?_ cases r with | ok d' => exact fbRun_supX b ws (n + 1) (d || d') t @@ -800,7 +800,7 @@ theorem fbRun_supX (b : Fl4FrameBatchInvocation) : /-- **The batch**. -/ theorem performFrameBatch_supX (b : Fl4FrameBatchInvocation) (s : State Unit) : ∀ y ∈ (performFrameBatchX b s).1, y ∈ (performFrameBatch (Z := Unit) b s).1 := by - unfold performFrameBatchX performFrameBatch + unfold performFrameBatchX performFrameBatch performFrameBatchWith exact fbx_sub_bind (fbRun_supX b _ _ _ s) fun _ _ _ => fun y hy => hy /-- **The batch from the changed state**: an outcome `(r, τ)` of the batch from `σ` gives the outcome diff --git a/proofs/Fl4Proofs/P3/SpecFbX.lean b/proofs/Fl4Proofs/P3/SpecFbX.lean index 29a4a9c0c..df87e8eea 100644 --- a/proofs/Fl4Proofs/P3/SpecFbX.lean +++ b/proofs/Fl4Proofs/P3/SpecFbX.lean @@ -459,7 +459,7 @@ theorem fbEntry_subX (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s cases op case remap => exact fun y hy => hy case copyMap => - unfold fbEntry fbEntryX + unfold fbEntry fbEntryWith fbDeferDelete fbEntryX dsimp only refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun dest t0 _ => ?_ refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun _ t1 _ => ?_ @@ -500,7 +500,7 @@ theorem fbEntry_subX (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s all_goals exact (h5 : False).elim all_goals exact (h5 : False).elim case delete => - unfold fbEntry fbEntryX + unfold fbEntry fbEntryWith fbDeferDelete fbEntryX dsimp only refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun slot t0 _ => ?_ refine fbx_sub_bindE (fun c t => capAt t slot = some c) (fun c t h => ?_) fun c t1 h1 => ?_ @@ -525,7 +525,7 @@ theorem fbEntry_subX (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s · exact ⟨_, _, _, _, _, _, h2⟩ · exact ⟨_, _, _, _, _, _, h1⟩ obtain ⟨_, _, _, _, _, _, hc2⟩ := hc2 - exact fbx_sub_bindE2 (by unfold fbDelete fbDeleteX; exact fbx_sub_handleE (capDelete_subX hc2)) + exact fbx_sub_bindE2 (by unfold fbDelete fbDeleteWith fbDeleteX; exact fbx_sub_handleE (capDelete_subX hc2)) (fun _ _ => True) (fun _ _ _ => trivial) fun _ _ _ => fun y hy => hy dsimp only rcases m with _ | ⟨asid', vaddr⟩ @@ -556,7 +556,7 @@ theorem fbRun_subX (b : Fl4FrameBatchInvocation) : ∀ y ∈ (fbRun (Z := Unit) b ws n d s).1, y ∈ (fbRunX b ws n d s).1 | [], n, d, s => fun y hy => hy | w :: ws, n, d, s => by - unfold fbRun fbRunX + unfold fbRun fbRunWith fbRunX refine fbx_sub_bind (fbEntry_subX b w s) fun r t _ => ?_ cases r with | ok d' => exact fbRun_subX b ws (n + 1) (d || d') t @@ -565,7 +565,7 @@ theorem fbRun_subX (b : Fl4FrameBatchInvocation) : /-- **The batch**. -/ theorem performFrameBatch_subX (b : Fl4FrameBatchInvocation) (s : State Unit) : ∀ y ∈ (performFrameBatch (Z := Unit) b s).1, y ∈ (performFrameBatchX b s).1 := by - unfold performFrameBatch performFrameBatchX + unfold performFrameBatch performFrameBatchWith performFrameBatchX exact fbx_sub_bind (fbRun_subX b _ _ _ s) fun _ _ _ => fun y hy => hy /-! ## Families -/ diff --git a/proofs/Fl4Proofs/P3/SpecInvAllO5.lean b/proofs/Fl4Proofs/P3/SpecInvAllO5.lean index 363bc2835..6f52b4473 100644 --- a/proofs/Fl4Proofs/P3/SpecInvAllO5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvAllO5.lean @@ -290,7 +290,7 @@ macro_rules | `(tactic| oi_leaf) => `(tactic| with_reducible exact oi_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem oi_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : OIKE N (fbEntry (Z := Unit) b w) := by - unfold fbEntry; oi_auto + unfold fbEntry fbEntryWith fbDeferDelete; oi_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem oi_performFrameBatch (b : Fl4FrameBatchInvocation) : OIK N (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvDomG.lean b/proofs/Fl4Proofs/P3/SpecInvDomG.lean index 5aa4cdc8a..48c1ff428 100644 --- a/proofs/Fl4Proofs/P3/SpecInvDomG.lean +++ b/proofs/Fl4Proofs/P3/SpecInvDomG.lean @@ -1176,12 +1176,12 @@ theorem dle_fbCode {α : Type} {m : SEMonad Unit α} (h : DLE m) : DLE (fbCode ( macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible refine dle_fbCode ?_) theorem dle_fbDelete (sl : CslotPtr) : DLE (fbDelete (Z := Unit) sl) := by - unfold fbDelete; exact dke_handleE (dl_capDelete sl) fun _ => dke_throw _ + unfold fbDelete fbDeleteWith; exact dke_handleE (dl_capDelete sl) fun _ => dke_throw _ macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dle_fbDelete _) /-- fb3: one `X86PML4FrameBatch` entry. -/ theorem dl_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : DLE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; dl_auto0 + unfold fbEntry fbEntryWith fbDeferDelete; dl_auto0 /-- fb3: the batch (SpecInvFbD's `fk_performFrameBatch`). -/ theorem dl_performFrameBatch (b : Fl4FrameBatchInvocation) : DL (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvFaultTcbsH7.lean b/proofs/Fl4Proofs/P3/SpecInvFaultTcbsH7.lean index 9cd2482a9..c0c828927 100644 --- a/proofs/Fl4Proofs/P3/SpecInvFaultTcbsH7.lean +++ b/proofs/Fl4Proofs/P3/SpecInvFaultTcbsH7.lean @@ -145,7 +145,7 @@ theorem b_fbMap (b : Fl4FrameBatchInvocation) (sl : CslotPtr) (v : Data) : BKE L b_of_gfE fun _ _ => by unfold fbMap; fg_auto theorem b_fbDelete (sl : CslotPtr) : BKE L E K C (fbDelete (Z := Unit) sl) := by - unfold fbDelete; exact ike_handleE (b_capDelete sl) fun _ => ike_throw _ + unfold fbDelete fbDeleteWith; exact ike_handleE (b_capDelete sl) fun _ => ike_throw _ macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_fbDelete _) @@ -155,7 +155,7 @@ theorem b_unmapPageUnflushed (sz : VMPageSize) (vs : ObjRef) (v : VSpaceRef) (p macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_unmapPageUnflushed _ _ _ _) theorem b_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : BKE L E K C (fbEntry (Z := Unit) b w) := by - unfold fbEntry + unfold fbEntry fbEntryWith fbDeferDelete split · refine ike_bind (b_fbCode (b_of_gfE fun _ _ => by fg_auto)) fun dest => ?_ refine ike_bind (b_fbCode (b_of_gfE fun _ _ => by fg_auto)) fun _ => ?_ diff --git a/proofs/Fl4Proofs/P3/SpecInvFbD.lean b/proofs/Fl4Proofs/P3/SpecInvFbD.lean index f5f1dd727..4eb8f3f5a 100644 --- a/proofs/Fl4Proofs/P3/SpecInvFbD.lean +++ b/proofs/Fl4Proofs/P3/SpecInvFbD.lean @@ -18,7 +18,7 @@ theorem fk_fbRun {b : Fl4FrameBatchInvocation} (h : ∀ w, FKE f (fbEntry (Z := ∀ (ws : List (Data × Data × Data)) (n : Nat) (d : Bool), FK f (fbRun (Z := Unit) b ws n d) | [], n, d => dk_pure _ | w :: ws, n, d => by - unfold fbRun + unfold fbRun fbRunWith refine dk_bind (h w) fun r => ?_ cases r with | ok d' => exact fk_fbRun h ws (n + 1) (d || d') @@ -26,7 +26,7 @@ theorem fk_fbRun {b : Fl4FrameBatchInvocation} (h : ∀ w, FKE f (fbEntry (Z := theorem fk_performFrameBatch {b : Fl4FrameBatchInvocation} (h : ∀ w, FKE f (fbEntry (Z := Unit) b w)) (hf : FK f (flushAll (Z := Unit) b.vspace b.asid)) : FK f (performFrameBatch (Z := Unit) b) := by - unfold performFrameBatch + unfold performFrameBatch performFrameBatchWith refine dk_bind (fk_fbRun h _ _ _) fun r => ?_ obtain ⟨done, err, d⟩ := r refine dk_bind ?_ fun _ => dk_pure _ diff --git a/proofs/Fl4Proofs/P3/SpecInvFbK.lean b/proofs/Fl4Proofs/P3/SpecInvFbK.lean index 92ca3a659..56f2f2648 100644 --- a/proofs/Fl4Proofs/P3/SpecInvFbK.lean +++ b/proofs/Fl4Proofs/P3/SpecInvFbK.lean @@ -16,25 +16,71 @@ noncomputable section variable {P : State Unit → Prop} -/-- The loop keeps `P` when every entry does. -/ -theorem ik_fbRun {b : Fl4FrameBatchInvocation} (h : ∀ w, IKE P (fbEntry (Z := Unit) b w)) : - ∀ (ws : List (Data × Data × Data)) (n : Nat) (d : Bool), IK P (fbRun (Z := Unit) b ws n d) - | [], n, d => ik_pure _ +/-- The preparatory action preserves an invariant if the native writes do. -/ +theorem ik_fbDeferDelete (b : Fl4FrameBatchInvocation) (sl : CslotPtr) (dev : Bool) + (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) (m : Option (Asid × VSpaceRef)) + (hun : ∀ va, IK P (unmapPageUnflushed (Z := Unit) sz b.vspace va p)) + (hset : IK P (setCap (Z := Unit) (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) sl)) : + IKE P (fbDeferDelete b sl dev p R mt sz m) := by + cases m with + | none => exact ike_returnOk _ + | some pair => + obtain ⟨asid, va⟩ := pair + by_cases h : b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid = b.asid + · simpa only [fbDeferDelete, if_pos h] using + (ike_liftE (ε := Data) (ik_bind (hun va) fun d => ik_bind hset fun _ => ik_pure d)) + · simpa only [fbDeferDelete, if_neg h] using + (ike_returnOk (P := P) (ε := Data) false) + +/-- An entry keeps the invariant when the native prefix and the selected deletion keep it. -/ +theorem ik_fbEntryWith {del : CslotPtr → PMonad Unit Unit} (b : Fl4FrameBatchInvocation) + (w : Data × Data × Data) (hbase : b.op ≠ .delete → IKE P (fbEntry b w)) + (hlookup : IKE P (lookupTargetSlot (Z := Unit) b.cnode (dataToCptr w.1) b.depth)) + (hget : ∀ sl, IK P (getCap (Z := Unit) sl)) + (hprep : ∀ sl dev p R mt sz m, IKE P (fbDeferDelete b sl dev p R mt sz m)) + (hdel : ∀ sl, IKE P (del sl)) : IKE P (fbEntryWith del b w) := by + cases hop : b.op <;> simp only [fbEntryWith, hop] + · simpa only [fbEntry, fbEntryWith, hop] using hbase (by rw [hop]; decide) + · simpa only [fbEntry, fbEntryWith, hop] using hbase (by rw [hop]; decide) + · refine ike_bind (ike_handleE hlookup fun _ => ike_throw _) fun sl => ?_ + refine ike_bind (ike_liftE (hget sl)) fun cap => ?_ + split + · exact ike_returnOk _ + · refine ike_bind (hprep sl _ _ _ _ _ _) fun d => ?_ + exact ike_bind (ike_handleE (hdel sl) fun _ => ike_throw _) fun _ => ike_returnOk d + · exact ike_throw _ + +/-- The loop keeps the invariant when each entry does. -/ +theorem ik_fbRunWith {del : CslotPtr → PMonad Unit Unit} {b : Fl4FrameBatchInvocation} + (h : ∀ w, IKE P (fbEntryWith del b w)) : + ∀ ws n d, IK P (fbRunWith del b ws n d) + | [], _, _ => ik_pure _ | w :: ws, n, d => by - unfold fbRun + unfold fbRunWith refine ik_bind (h w) fun r => ?_ cases r with - | ok d' => exact ik_fbRun h ws (n + 1) (d || d') - | error c => exact ik_pure _ + | ok d' => exact ik_fbRunWith h ws (n + 1) (d || d') + | error _ => exact ik_pure _ -/-- **The batch keeps `P`** when every entry and the final flush do. -/ -theorem ik_performFrameBatch {b : Fl4FrameBatchInvocation} (h : ∀ w, IKE P (fbEntry (Z := Unit) b w)) - (hf : IK P (flushAll (Z := Unit) b.vspace b.asid)) : IK P (performFrameBatch (Z := Unit) b) := by - unfold performFrameBatch - refine ik_bind (ik_fbRun h _ _ _) fun r => ?_ +/-- The loop keeps `P` when every entry does. -/ +theorem ik_fbRun {b : Fl4FrameBatchInvocation} (h : ∀ w, IKE P (fbEntry (Z := Unit) b w)) : + ∀ (ws : List (Data × Data × Data)) (n : Nat) (d : Bool), IK P (fbRun (Z := Unit) b ws n d) := + ik_fbRunWith h + +/-- The batch keeps the invariant when each entry and the final flush do. -/ +theorem ik_performFrameBatchWith {del : CslotPtr → PMonad Unit Unit} {b : Fl4FrameBatchInvocation} + (h : ∀ w, IKE P (fbEntryWith del b w)) (hf : IK P (flushAll (Z := Unit) b.vspace b.asid)) : + IK P (performFrameBatchWith del b) := by + unfold performFrameBatchWith + refine ik_bind (ik_fbRunWith h _ _ _) fun r => ?_ obtain ⟨done, err, d⟩ := r exact ik_bind (ik_when _ hf) fun _ => ik_pure _ +/-- **The batch keeps `P`** when every entry and the final flush do. -/ +theorem ik_performFrameBatch {b : Fl4FrameBatchInvocation} (h : ∀ w, IKE P (fbEntry (Z := Unit) b w)) + (hf : IK P (flushAll (Z := Unit) b.vspace b.asid)) : IK P (performFrameBatch (Z := Unit) b) := + ik_performFrameBatchWith h hf + end end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/SpecInvGpK5.lean b/proofs/Fl4Proofs/P3/SpecInvGpK5.lean index 154907c1f..10d648285 100644 --- a/proofs/Fl4Proofs/P3/SpecInvGpK5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvGpK5.lean @@ -290,7 +290,7 @@ macro_rules | `(tactic| gpk_leaf) => `(tactic| with_reducible exact gpk_performT /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem gpk_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : GpKE N (fbEntry (Z := Unit) b w) := by - unfold fbEntry; gpk_auto + unfold fbEntry fbEntryWith fbDeferDelete; gpk_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem gpk_performFrameBatch (b : Fl4FrameBatchInvocation) : GpK N (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvIoxG.lean b/proofs/Fl4Proofs/P3/SpecInvIoxG.lean index 769a493c4..26b9b687d 100644 --- a/proofs/Fl4Proofs/P3/SpecInvIoxG.lean +++ b/proofs/Fl4Proofs/P3/SpecInvIoxG.lean @@ -1174,12 +1174,12 @@ theorem ioxe_fbCode {α : Type} {m : SEMonad Unit α} (h : IOXE m) : IOXE (fbCod macro_rules | `(tactic| iox_leaf) => `(tactic| with_reducible refine ioxe_fbCode ?_) theorem ioxe_fbDelete (sl : CslotPtr) : IOXE (fbDelete (Z := Unit) sl) := by - unfold fbDelete; exact dke_handleE (iox_capDelete sl) fun _ => dke_throw _ + unfold fbDelete fbDeleteWith; exact dke_handleE (iox_capDelete sl) fun _ => dke_throw _ macro_rules | `(tactic| iox_leaf) => `(tactic| with_reducible exact ioxe_fbDelete _) /-- fb3: one `X86PML4FrameBatch` entry. -/ theorem iox_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : IOXE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; iox_auto0 + unfold fbEntry fbEntryWith fbDeferDelete; iox_auto0 /-- fb3: the batch (SpecInvFbD's `fk_performFrameBatch`). -/ theorem iox_performFrameBatch (b : Fl4FrameBatchInvocation) : IOX (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvNativeScopeF5.lean b/proofs/Fl4Proofs/P3/SpecInvNativeScopeF5.lean index 3db46c742..9ecac2e73 100644 --- a/proofs/Fl4Proofs/P3/SpecInvNativeScopeF5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvNativeScopeF5.lean @@ -288,7 +288,7 @@ macro_rules | `(tactic| ns_leaf) => `(tactic| with_reducible exact ns_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem ns_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : NSKE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; ns_auto + unfold fbEntry fbEntryWith fbDeferDelete; ns_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem ns_performFrameBatch (b : Fl4FrameBatchInvocation) : NSK (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvNtfnQC4.lean b/proofs/Fl4Proofs/P3/SpecInvNtfnQC4.lean index 4517a58a8..841fb4026 100644 --- a/proofs/Fl4Proofs/P3/SpecInvNtfnQC4.lean +++ b/proofs/Fl4Proofs/P3/SpecInvNtfnQC4.lean @@ -153,7 +153,7 @@ macro_rules | `(tactic| ct_leaf) => `(tactic| with_reducible exact ct_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem ct_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : CTKE cq (fbEntry (Z := Unit) b w) := by - unfold fbEntry; ct_auto + unfold fbEntry fbEntryWith fbDeferDelete; ct_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem ct_performFrameBatch (b : Fl4FrameBatchInvocation) : CTK cq (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvQDomF5.lean b/proofs/Fl4Proofs/P3/SpecInvQDomF5.lean index 42e7e616e..60f46a553 100644 --- a/proofs/Fl4Proofs/P3/SpecInvQDomF5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvQDomF5.lean @@ -73,7 +73,7 @@ macro_rules | `(tactic| q4_leaf) => `(tactic| with_reducible exact q4_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem q4_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : Q4KE (fbEntry (Z := Unit) b w) := by - unfold fbEntry; q4_auto + unfold fbEntry fbEntryWith fbDeferDelete; q4_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem q4_performFrameBatch (b : Fl4FrameBatchInvocation) : Q4K (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvSgI5.lean b/proofs/Fl4Proofs/P3/SpecInvSgI5.lean index f787a6266..1386ef9f4 100644 --- a/proofs/Fl4Proofs/P3/SpecInvSgI5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvSgI5.lean @@ -290,7 +290,7 @@ macro_rules | `(tactic| idk_leaf) => `(tactic| with_reducible exact idk_performT /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem idk_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : IdKE N (fbEntry (Z := Unit) b w) := by - unfold fbEntry; idk_auto + unfold fbEntry fbEntryWith fbDeferDelete; idk_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem idk_performFrameBatch (b : Fl4FrameBatchInvocation) : IdK N (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvSlI5.lean b/proofs/Fl4Proofs/P3/SpecInvSlI5.lean index 4155d404f..ccf87280a 100644 --- a/proofs/Fl4Proofs/P3/SpecInvSlI5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvSlI5.lean @@ -290,7 +290,7 @@ macro_rules | `(tactic| isk_leaf) => `(tactic| with_reducible exact isk_performT /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem isk_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : IsKE N (fbEntry (Z := Unit) b w) := by - unfold fbEntry; isk_auto + unfold fbEntry fbEntryWith fbDeferDelete; isk_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem isk_performFrameBatch (b : Fl4FrameBatchInvocation) : IsK N (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvUtE.lean b/proofs/Fl4Proofs/P3/SpecInvUtE.lean index 5660ca812..6fc811e1a 100644 --- a/proofs/Fl4Proofs/P3/SpecInvUtE.lean +++ b/proofs/Fl4Proofs/P3/SpecInvUtE.lean @@ -151,7 +151,7 @@ theorem vk_capInsert {ε β : Type} {w : UView} {c : Cap} {src dest : CslotPtr} `okv_decodeCnode`'s copy), the rest by the generated frames. -/ theorem ui_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : IKE UtInv' (fbEntry (Z := Unit) b w) := by - unfold fbEntry + unfold fbEntry fbEntryWith fbDeferDelete split · refine ike_of_vk fun v hw => ?_ refine vk_bind_uk (Q := fun _ => True) hw (fun _ => by uk_auto) (okv_triv fun _ => trivial) fun dest _ => ?_ diff --git a/proofs/Fl4Proofs/P3/SpecInvUtNK5.lean b/proofs/Fl4Proofs/P3/SpecInvUtNK5.lean index e90b9f86f..0b5244fd7 100644 --- a/proofs/Fl4Proofs/P3/SpecInvUtNK5.lean +++ b/proofs/Fl4Proofs/P3/SpecInvUtNK5.lean @@ -290,7 +290,7 @@ macro_rules | `(tactic| nk_leaf) => `(tactic| with_reducible exact nk_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem nk_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : NKE N (fbEntry (Z := Unit) b w) := by - unfold fbEntry; nk_auto + unfold fbEntry fbEntryWith fbDeferDelete; nk_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem nk_performFrameBatch (b : Fl4FrameBatchInvocation) : NK N (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvYieldBlockF4.lean b/proofs/Fl4Proofs/P3/SpecInvYieldBlockF4.lean index 35a7948c5..3bd7f62ad 100644 --- a/proofs/Fl4Proofs/P3/SpecInvYieldBlockF4.lean +++ b/proofs/Fl4Proofs/P3/SpecInvYieldBlockF4.lean @@ -187,7 +187,7 @@ macro_rules | `(tactic| yb_leaf) => `(tactic| with_reducible exact yb_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem yb_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : YBKE L (fbEntry (Z := Unit) b w) := by - unfold fbEntry; yb_auto + unfold fbEntry fbEntryWith fbDeferDelete; yb_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem yb_performFrameBatch (b : Fl4FrameBatchInvocation) : YBK L (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/SpecInvYieldScopeF4.lean b/proofs/Fl4Proofs/P3/SpecInvYieldScopeF4.lean index aad4abd87..a450a53a0 100644 --- a/proofs/Fl4Proofs/P3/SpecInvYieldScopeF4.lean +++ b/proofs/Fl4Proofs/P3/SpecInvYieldScopeF4.lean @@ -191,7 +191,7 @@ macro_rules | `(tactic| ys_leaf) => `(tactic| with_reducible exact ys_performTes /-- fb-spec-2: one `X86PML4FrameBatch` entry (no recursion). -/ theorem ys_fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : YSKE L (fbEntry (Z := Unit) b w) := by - unfold fbEntry; ys_auto + unfold fbEntry fbEntryWith fbDeferDelete; ys_auto /-- fb-spec-2: the batch (`Fl4Proofs.P3.SInv.ik_performFrameBatch`). -/ theorem ys_performFrameBatch (b : Fl4FrameBatchInvocation) : YSK L (performFrameBatch (Z := Unit) b) := diff --git a/proofs/Fl4Proofs/P3/VirtF13.lean b/proofs/Fl4Proofs/P3/VirtF13.lean index 2ac8dbd66..84c9bea32 100644 --- a/proofs/Fl4Proofs/P3/VirtF13.lean +++ b/proofs/Fl4Proofs/P3/VirtF13.lean @@ -143,6 +143,7 @@ theorem fl4PerformInvocationVF_base (onCore remoteCur : ObjRef → Bool) (block | invokeNotification _ _ => exact absurd trivial h | invokeTCB _ => exact absurd trivial h | invokeCNode _ => exact absurd trivial h + | invokeArchObject i => cases i <;> first | exact absurd trivial h | rfl | _ => rfl /-! ## The exit (VirtR7d's twins) -/ @@ -474,6 +475,11 @@ theorem fl4PerformInvocationVF_eqNR (onCore remoteCur : ObjRef → Bool) (blk ca ExceptT.run (fl4PerformInvocation blk call don i) s := by by_cases hp : PerformSubst i · cases i with + | invokeArchObject i => + cases i with + | frameBatch b => + exact run_liftE_congr (performFrameBatchWith_eqNV b hs.1 fun sl _ h => capDeleteVF_eq sl h) + | _ => exact (hp : False).elim | invokeUntyped ui => obtain ⟨src, reset, base, rb, t, sz, slots, dev⟩ := ui show ExceptT.run (fl4InvokeUntypedV (Z := Unit) (.retype src reset base rb t sz slots dev) >>= fun _ => diff --git a/proofs/Fl4Proofs/P3/VirtFrameBatch.lean b/proofs/Fl4Proofs/P3/VirtFrameBatch.lean new file mode 100644 index 000000000..9bce45cad --- /dev/null +++ b/proofs/Fl4Proofs/P3/VirtFrameBatch.lean @@ -0,0 +1,112 @@ +import Fl4Proofs.P3.VirtR7b +import Fl4Proofs.P3.SpecFbX +import Fl4Proofs.P3.SpecInvFbK + +/-! +FrameBatch agreement at the delete calls an entry actually reaches. +The loop advances only after successful entries; equality includes +exceptional outcomes and the nondeterministic failure flag. +-/ + +namespace Fl4Proofs.P3 +open Classical Fl4Spec SInv VirtR7b VirtR7d SInvV SInvL SInvTY + +noncomputable section + +/-- The slot and state at which a DELETE entry calls the chosen finaliser. -/ +def FbDeleteReady (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) + (s : State Unit) (sl : CslotPtr) (t : State Unit) : Prop := + b.op = .delete ∧ ∃ u v dev p R mt sz m d, + (.ok sl, u) ∈ ((fbCode (Z := Unit) (lookupTargetSlot b.cnode (dataToCptr w.1) b.depth)).run s).1 ∧ + (.ok (.archObjectCap (.pageCap dev p R mt sz m)), v) ∈ + ((NonDetE.liftE (ε := Data) (getCap (Z := Unit) sl)).run u).1 ∧ + (.ok d, t) ∈ ((fbDeferDelete b sl dev p R mt sz m).run v).1 + +theorem fbEntryWith_congr {del₁ del₂ : CslotPtr → PMonad Unit Unit} + {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {s : State Unit} + (hdel : ∀ sl t, FbDeleteReady b w s sl t → (del₁ sl).run t = (del₂ sl).run t) : + (fbEntryWith del₁ b w).run s = (fbEntryWith del₂ b w).run s := by + cases hop : b.op with + | copyMap => simp only [fbEntryWith, hop] + | remap => simp only [fbEntryWith, hop] + | delete => + simp only [fbEntryWith, hop] + refine nde_bind_congr rfl fun sl u hlu => ?_ + refine nde_bind_congr rfl fun c v hget => ?_ + cases c with + | archObjectCap a => + cases a with + | pageCap dev p R mt sz m => + refine nde_bind_congr rfl fun d t hprep => ?_ + refine nde_bind_congr ?_ fun _ _ _ => rfl + unfold fbDeleteWith NonDetE.handleE + exact nd_bind_eq_of (hdel sl t ⟨hop, u, v, dev, p, R, mt, sz, m, d, hlu, hget, hprep⟩) _ + | _ => rfl + | _ => rfl + +/-- Agreement on a list prefix, carried through each successful matching entry. -/ +theorem performFrameBatchWith_congr {del₁ del₂ : CslotPtr → PMonad Unit Unit} + {b : Fl4FrameBatchInvocation} {s : State Unit} + {P : List (Data × Data × Data) → State Unit → Prop} + (h₀ : P b.entries s) + (hdel : ∀ w ws u sl t, P (w :: ws) u → FbDeleteReady b w u sl t → + (del₁ sl).run t = (del₂ sl).run t) + (hstep : ∀ w ws u d t, P (w :: ws) u → + (.ok d, t) ∈ ((fbEntryWith del₂ b w).run u).1 → P ws t) : + performFrameBatchWith del₁ b s = performFrameBatchWith del₂ b s := by + have loop : ∀ ws n d u, P ws u → fbRunWith del₁ b ws n d u = fbRunWith del₂ b ws n d u := by + intro ws + induction ws with + | nil => intro n d u _; rfl + | cons w ws ih => + intro n d u hu + unfold fbRunWith + rw [nd_bind_eq_of (fbEntryWith_congr (fun sl t hr => hdel w ws u sl t hu hr))] + refine nd_bind_congr_on _ _ _ _ fun x hx => ?_ + obtain ⟨r, t⟩ := x + cases r with + | ok d' => exact ih (n + 1) (d || d') t (hstep w ws u d' t hu hx) + | error _ => rfl + unfold performFrameBatchWith + exact nd_bind_eq_of (loop _ _ _ _ h₀) _ + +/-- Agreement where the chosen entry invariant holds at each delete call. -/ +theorem performFrameBatchWith_agree {del₁ del₂ : CslotPtr → PMonad Unit Unit} + {b : Fl4FrameBatchInvocation} {s : State Unit} {P : State Unit → Prop} + (h₀ : P s) (hentry : ∀ w, SInv.IKE P (fbEntryWith del₂ b w)) + (hprep : ∀ sl dev p R mt sz m, SInv.IKE P (fbDeferDelete b sl dev p R mt sz m)) + (hdel : ∀ sl t, P t → (del₁ sl).run t = (del₂ sl).run t) : + performFrameBatchWith del₁ b s = performFrameBatchWith del₂ b s := by + refine performFrameBatchWith_congr (P := fun _ => P) h₀ ?_ ?_ + · intro w ws u sl t hu hr + obtain ⟨_, v, v', dev, p, R, mt, sz, m, d, hlu, hg, hpre⟩ := hr + have hv : P v := (SInv.ike_handleE (fbx_lookupTargetSlot _ _ _) fun _ => SInv.ike_throw _) u hu _ hlu + have hv' : P v' := (SInv.ike_liftE (fbx_ik_getCap _)) v hv _ hg + exact hdel sl t (hprep sl dev p R mt sz m v' hv' _ hpre) + · intro w ws u d t hu h + exact hentry w u hu _ h + +set_option maxHeartbeats 1000000 in +/-- Both VM-aware deleters can use the native batch proof on an NV state. -/ +theorem performFrameBatchWith_eqNV {del : CslotPtr → PMonad Unit Unit} + (b : Fl4FrameBatchInvocation) {s : State Unit} (hs : NV s) + (hdel : ∀ sl t, NV t → (del sl).run t = (capDelete sl).run t) : + performFrameBatchWith del b s = performFrameBatch b s := by + by_cases hop : b.op = .delete + · have hprep (sl dev p R mt sz m) : SInv.IKE NV (fbDeferDelete b sl dev p R mt sz m) := by + intro u hu y hy + have hn : SInv.IKE NVC (fbDeferDelete b sl dev p R mt sz m) := + ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun _ => by unfold unmapPageUnflushed; hv_auto) + (nvc_setCap _ (fun h => h)) + have hv : SInv.IKE NVV (fbDeferDelete b sl dev p R mt sz m) := + ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun _ => by unfold unmapPageUnflushed; ty_auto) (ty_setCap _ _) + exact ⟨hn u hu.1 y hy, hv u hu.2 y hy⟩ + refine performFrameBatchWith_agree (P := NV) hs ?_ hprep hdel + intro w + exact ik_fbEntryWith b w (fun h => (h hop).elim) (fbx_lookupTargetSlot _ _ _) + fbx_ik_getCap hprep nv_capDelete + · exact performFrameBatchWith_congr (P := fun _ _ => True) trivial + (fun _ _ _ _ _ _ hr => False.elim (hop hr.1)) (fun _ _ _ _ _ _ _ => trivial) + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VirtFrameBatchConservative.lean b/proofs/Fl4Proofs/P3/VirtFrameBatchConservative.lean new file mode 100644 index 000000000..ba33fdde2 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VirtFrameBatchConservative.lean @@ -0,0 +1,270 @@ +import Fl4Proofs.P3.VirtF13 +import Fl4Proofs.P3.SpecFbKeepDel +import Fl4Proofs.P3.VmDelFinSpec +import Fl4Proofs.P3.VsRel +import Fl4Proofs.P3.DelSpec + +/-! +Native and VM FrameBatch DELETE agree on reached mapped EPT frames +whose native and guest unmaps both return the unchanged state. +This side result does not constrain the VM refinement. +-/ + +namespace Fl4Proofs.P3 +open Classical Fl4Spec SInv SInvU VirtR7b VirtR7d VirtF13 Vs + +noncomputable section + +/-- At a mapped EPT cap, both finalisers' unmaps succeed without writing. -/ +def FbEptDeleteNoop (sl : CslotPtr) (s : State Unit) : Prop := + ∀ dev p R sz asid gpa, + SInvU.capAt s sl = some (.archObjectCap (.pageCap dev p R .vmEptMap sz (some (asid, gpa)))) → + unmapPage (Z := Unit) sz asid gpa p s = ({((), s)}, False) ∧ + NonDet.when (asid ≠ 0) (guestUnmap (Z := Unit) asid gpa p (pageBitsForSize sz) false) s = + ({((), s)}, False) + +/-- Every DELETE call in the native run has the no-op property. Errors +stop the batch, so only successful entries contribute to the tail. -/ +def FbEptDeleteSafe (b : Fl4FrameBatchInvocation) : + List (Data × Data × Data) → State Unit → Prop + | [], _ => True + | w :: ws, s => + (∀ sl t, FbDeleteReady b w s sl t → FbEptDeleteNoop sl t) ∧ + ∀ d t, (.ok d, t) ∈ ((fbEntry (Z := Unit) b w).run s).1 → FbEptDeleteSafe b ws t + +/-- The deferred native unmap either keeps the frame or clears its mapping +metadata before DELETE. -/ +theorem fbDeferDelete_frame {b : Fl4FrameBatchInvocation} {sl : CslotPtr} {u t : State Unit} + {dev : Bool} {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} + {m : Option (Asid × VSpaceRef)} {d : Bool} + (hc : SInvU.capAt u sl = some (.archObjectCap (.pageCap dev p R mt sz m))) + (hp : (.ok d, t) ∈ ((fbDeferDelete b sl dev p R mt sz m).run u).1) : + ∃ dev' p' R' mt' sz' m', SInvU.capAt t sl = some (.archObjectCap (.pageCap dev' p' R' mt' sz' m')) := by + have unchanged (h : (.ok d, t) ∈ + ((NonDetE.returnOk (ε := Data) false : NonDetE (State Unit) Data Bool).run u).1) : + ∃ dev' p' R' mt' sz' m', SInvU.capAt t sl = some (.archObjectCap (.pageCap dev' p' R' mt' sz' m')) := by + have ht := (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + exact ⟨dev, p, R, mt, sz, m, by simpa only [ht] using hc⟩ + cases m with + | none => exact unchanged hp + | some pair => + obtain ⟨asid, va⟩ := pair + by_cases hdf : b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid = b.asid + · simp only [fbDeferDelete, if_pos hdf] at hp + obtain ⟨x, hx, hxe⟩ := fbx_mem_liftE hp + obtain ⟨_, rfl⟩ := Prod.mk.inj hxe + obtain ⟨_, _, z, hz, hr⟩ := hx + rw [Set.mem_singleton_iff.1 hr] + exact ⟨dev, p, R, .vmNoMap, sz, none, setCap_at hz⟩ + · exact unchanged (by simpa only [fbDeferDelete, if_neg hdf] using hp) + +private theorem FbDeleteReady.frame {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} + {s t : State Unit} {sl : CslotPtr} (h : FbDeleteReady b w s sl t) : + ∃ dev p R mt sz m, SInvU.capAt t sl = some (.archObjectCap (.pageCap dev p R mt sz m)) := by + obtain ⟨_, u, v, dev, p, R, mt, sz, m, d, _, hg, hp⟩ := h + have hread := getCap_capAt (mem_run_liftE' hg) + have he : v = u := hread.1 + have hc : SInvU.capAt u sl = some (.archObjectCap (.pageCap dev p R mt sz m)) := hread.2 + subst v + exact fbDeferDelete_frame hc hp + +private theorem frameFinalise_conservative {sl : CslotPtr} {s : State Unit} {dev : Bool} + {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m))) + (hn : FbEptDeleteNoop sl s) (fin : Bool) : + finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin s = + finaliseCap (.archObjectCap (.pageCap dev p R mt sz m)) fin s := by + cases mt <;> cases m <;> try rfl + case vmEptMap.some pair => + obtain ⟨asid, gpa⟩ := pair + obtain ⟨hn, hv⟩ := hn dev p R sz asid gpa hc + simp only [finaliseCapVF, finaliseCap, archFinaliseCap] + rw [Notif.nd_bind_one hv, Notif.nd_bind_one hn] + +private theorem lfp_step {σ α ι : Type} {F : (ι → NonDet σ α) → ι → NonDet σ α} + (hm : NDMono F) (i : ι) (s : σ) : NonDet.lfp F i s = F (NonDet.lfp F) i s := + le_antisymm (ik_lfp_le F _ (hm _ _ (lfp_prefixed F hm)) i s) (lfp_prefixed F hm i s) + +private theorem recDel_step (c : RecDelCall) (s : State Unit) : + (recDel (Z := Unit) c).run s = (recDelF recDel c).run s := + lfp_step recDelF_mono c s + +/-- One step of the VM-aware deletion fixed point, including the failure flag. -/ +theorem recDelVF_step (c : RecDelCall) (s : State Unit) : + (recDelVF (Z := Unit) c).run s = (recDelFVF recDelVF c).run s := + lfp_step recDelFVF_mono c s + +/-- A frame finaliser removes the cap without a cleanup cap. -/ +theorem finaliseCapVF_frame_pair {dev : Bool} {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} + {m : Option (Asid × VSpaceRef)} {fin : Bool} {s : State Unit} {y : (Cap × Cap) × State Unit} + (hy : y ∈ (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin s).1) : + y.1 = (.nullCap, .nullCap) := by + cases mt <;> cases m <;> try exact archFinaliseCap_frame hy + case vmEptMap.some mapping => + obtain ⟨asid, gpa⟩ := mapping + obtain ⟨_, _, h⟩ := hy + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).1 + +set_option maxHeartbeats 1000000 in +theorem capDeleteVF_frame_mem {sl : CslotPtr} {s t : State Unit} + {dev : Bool} {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} + {m : Option (Asid × VSpaceRef)} {r : Except Unit Unit} + (hget : getCap (Z := Unit) sl s = ({(.archObjectCap (.pageCap dev p R mt sz m), s)}, False)) : + (r, t) ∈ ((capDeleteVF (Z := Unit) sl).run s).1 ↔ + r = .ok () ∧ ∃ u, ((.nullCap, .nullCap), u) ∈ + (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) + (decide (isFinalCap' (.archObjectCap (.pageCap dev p R mt sz m)) s)) s).1 ∧ + ((), t) ∈ (emptySlot (Z := Unit) sl .nullCap u).1 := by + let c := Cap.archObjectCap (.pageCap dev p R mt sz m) + have hc : c ≠ .nullCap := by intro h; simp [c] at h + change getCap (Z := Unit) sl s = ({(c, s)}, False) at hget + constructor + · intro h + unfold capDeleteVF at h + obtain ⟨x, hx, hnext⟩ := fbx_mem_bindE h + rw [recDelVF_step] at hx + unfold recDelFVF withoutPreemption at hx + obtain ⟨y, hy, hbody⟩ := fbx_mem_bindE hx + rw [recDelVF_step] at hy + unfold recDelFVF withoutPreemption at hy + rw [runE_bind_ok (runE_liftE hget), ite_eq_right hc] at hy + rw [runE_bind_ok (runE_liftE (m := isFinalCap (Z := Unit) c) rfl)] at hy + obtain ⟨z, hz, hznext⟩ := fbx_mem_bindE hy + obtain ⟨⟨pair, u⟩, hw, rfl⟩ := fbx_mem_liftE hz + have hpair := finaliseCapVF_frame_pair hw + dsimp only at hpair + subst pair + rcases hznext with ⟨q, epair, hsuc⟩ | ⟨_, he, _⟩ + swap + · cases he + simp only [Except.ok.injEq] at epair + subst q + simp only [capRemoveable, ↓reduceIte] at hsuc + obtain rfl := Set.mem_singleton_iff.1 hsuc + rcases hbody with ⟨q, epair, hbody⟩ | ⟨_, he, _⟩ + swap + · cases he + simp only [Except.ok.injEq] at epair + subst q + simp only [true_or, NonDet.when, ↓reduceIte] at hbody + obtain ⟨q, hq, hqnext⟩ := fbx_mem_bindE hbody + obtain ⟨⟨value, v⟩, hv, rfl⟩ := fbx_mem_liftE hq + cases value + rcases hqnext with ⟨_, _, hret⟩ | ⟨_, he', _⟩ + · have hx' := Set.mem_singleton_iff.1 hret + rw [hx'] at hnext + rcases hnext with ⟨_, _, hr⟩ | ⟨_, he, _⟩ + · obtain ⟨er, ht⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hr) + subst r t + exact ⟨rfl, u, hw, hv⟩ + · cases he + · cases he' + · rintro ⟨rfl, u, hfin, hslot⟩ + have hd := rd_fin_removableG (Fn := finaliseCapVF) (e := true) hget hc hfin (by simp [capRemoveable]) + obtain ⟨v, hv⟩ := rd_cte_okG (Fn := finaliseCapVF) (σ2 := t) hd (by simpa only [true_or, NonDet.when, ↓reduceIte] using hslot) + exact capDeleteG_ok (Fn := finaliseCapVF) hv + +/-- The live guest write and CNode removal keep the fields used by the batch loop. -/ +theorem fbKeep_capDeleteVF_eptFrame {sl : CslotPtr} {s t : State Unit} + {dev : Bool} {p : ObjRef} {R : CapRights} {sz : VMPageSize} {asid : Asid} {gpa : Word} + (hg : getCap (Z := Unit) sl s = + ({(.archObjectCap (.pageCap dev p R .vmEptMap sz (some (asid, gpa))), s)}, False)) + (h : (.ok (), t) ∈ ((capDeleteVF (Z := Unit) sl).run s).1) : + FbKeep s t ∧ t.machineState.underlyingMemory = s.machineState.underlyingMemory := by + obtain ⟨-, u, hfin, hempty⟩ := (capDeleteVF_frame_mem hg).1 h + change ((Cap.nullCap, Cap.nullCap), u) ∈ + ((NonDet.when (asid ≠ 0) (guestUnmap (Z := Unit) asid gpa p (pageBitsForSize sz) false) >>= + fun _ => pure (Cap.nullCap, Cap.nullCap)) s).1 at hfin + obtain ⟨v, hv, hret⟩ := hfin + have hu := (Prod.mk.inj (Set.mem_singleton_iff.1 hret)).2 + have hUnmap : FK fbUF (guestUnmap (Z := Unit) asid gpa p (pageBitsForSize sz) false) := + fk_guestUnmapEpt (fun _ l es => fbu_setObject _ (.ept l es) (by simp [aaType])) _ _ _ _ _ + have huf := (dk_when (asid ≠ 0) hUnmap) s v hv + have hC := emptySlot_null_nkn sl u ((), t) hempty + refine ⟨?_, ?_⟩ + · exact (FbKeep.of_fbUF (by rw [hu]; exact huf)).trans (FbKeep.of_cnK hC) + · have hm := congrArg (fun q : AbstractState => q.machineState.underlyingMemory) hC.1 + change t.machineState.underlyingMemory = u.machineState.underlyingMemory at hm + rw [hu] at hm + exact hm.trans (congrArg (fun q => q.2.2.2.2.2.2.2.2) huf) + +private theorem capDeleteVF_conservative {s : State Unit} {sl : CslotPtr} {dev : Bool} + {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m))) + (hn : FbEptDeleteNoop sl s) : (capDeleteVF (Z := Unit) sl).run s = (capDelete sl).run s := by + have hslot : (recDel (Z := Unit) (.finaliseSlotCall sl true)).run s = + (recDelVF (.finaliseSlotCall sl true)).run s := by + rw [recDel_step, recDelVF_step] + unfold recDelF recDelFVF + refine nde_bind_congr rfl fun cap t hg => ?_ + obtain ⟨rfl, hcap⟩ := getCap_capAt (mem_run_liftE' hg) + have e := Option.some.inj (hcap.symm.trans hc) + change cap = _ at e + subst cap + have hnonnull : Cap.archObjectCap (.pageCap dev p R mt sz m) ≠ .nullCap := by + intro contra + cases contra + simp only [if_neg hnonnull] + refine nde_bind_congr rfl fun fin u hf => ?_ + have hu : u = t := (Prod.mk.inj (Set.mem_singleton_iff.1 + (mem_run_liftE' hf : (fin, u) ∈ ({(decide (isFinalCap' _ t), t)} : Set _)))).2 + subst u + refine nde_bind_congr (run_liftE_congr (frameFinalise_conservative hc hn fin).symm) fun pair v hv => ?_ + have hr : pair = (.nullCap, .nullCap) := by + have hx := mem_run_liftE' hv + cases m with + | none => exact (Prod.mk.inj (Set.mem_singleton_iff.1 hx)).1 + | some mapping => + obtain ⟨asid, va⟩ := mapping + obtain ⟨_, _, h⟩ := hx + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).1 + subst pair + simp only [capRemoveable, ↓reduceIte] + symm + unfold capDelete capDeleteVF + refine nde_bind_congr ?_ fun _ _ _ => rfl + rw [recDel_step, recDelVF_step] + unfold recDelF recDelFVF + exact nde_bind_congr hslot fun _ _ _ => rfl + +/-- Native and unmapped frame caps select the same finaliser in both deletions. -/ +theorem capDeleteVF_frame_eq_native {s : State Unit} {sl : CslotPtr} {dev : Bool} + {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m))) + (h : mt = .vmEptMap → m = none) : + (capDeleteVF (Z := Unit) sl).run s = (capDelete sl).run s := by + refine capDeleteVF_conservative hc ?_ + intro dev' p' R' sz' asid gpa he + rw [hc] at he + have e := Cap.archObjectCap.inj (Option.some.inj he) + simp only [ArchCap.pageCap.injEq] at e + have hm := (h e.2.2.2.1).symm.trans e.2.2.2.2.2 + cases hm + +/-- A non-EPT frame deletion keeps the native batch's loop fields. -/ +theorem fbKeep_capDeleteVF_frame {s t : State Unit} {sl : CslotPtr} {dev : Bool} + {p : ObjRef} {R : CapRights} {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m))) + (hm : mt = .vmEptMap → m = none) + (h : (.ok (), t) ∈ ((capDeleteVF (Z := Unit) sl).run s).1) : + FbKeep s t ∧ t.machineState.underlyingMemory = s.machineState.underlyingMemory := by + rw [capDeleteVF_frame_eq_native hc hm] at h + exact fbKeep_capDelete_frame hc h + +/-- The VM batch agrees with the native batch when both unmaps succeed +as no-ops at every reached DELETE of a mapped EPT frame. A stale EPT cap +can refer to an ASID since reused by a native root. In that case the +native unmap can clear a native entry even without a live guest +mapping. Agreement also compares the monad's failure bit. -/ +theorem performFrameBatchVF_eq_native (b : Fl4FrameBatchInvocation) {s : State Unit} + (h : FbEptDeleteSafe b b.entries s) : + performFrameBatchVF b s = performFrameBatch b s := by + refine performFrameBatchWith_congr (P := FbEptDeleteSafe b) h ?_ ?_ + · intro w ws u sl t hs hr + obtain ⟨dev, p, R, mt, sz, m, hcap⟩ := hr.frame + exact capDeleteVF_conservative hcap (hs.1 sl t hr) + · intro w ws u d t hs hx + exact hs.2 d t hx + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VirtNoRunStep.lean b/proofs/Fl4Proofs/P3/VirtNoRunStep.lean index c6dafeea2..ef0d788ea 100644 --- a/proofs/Fl4Proofs/P3/VirtNoRunStep.lean +++ b/proofs/Fl4Proofs/P3/VirtNoRunStep.lean @@ -109,6 +109,18 @@ theorem ns_performVcpuInvocation (i : VcpuInvocation) : SInv.NSK (performVcpuInv theorem ns_performEptInvocation (i : EptInvocation) : SInv.NSK (performEptInvocation (Z := Unit) i) := by cases i <;> (unfold performEptInvocation; ns_auto) +private theorem ns_fbDeferDelete (b : Fl4FrameBatchInvocation) (sl : CslotPtr) + (dev : Bool) (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) + (m : Option (Asid × VSpaceRef)) : + SInv.NSKE (fbDeferDelete (Z := Unit) b sl dev p R mt sz m) := + ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun _ => by unfold unmapPageUnflushed; ns_auto) + (by ns_auto) + +private theorem ns_fbEntryV (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : + SInv.NSKE (fbEntryWith (Z := Unit) capDeleteV b w) := + ik_fbEntryWith b w (fun _ => ns_fbEntry b w) (ns_lookupTargetSlot _ _ _) ns_getCap + (ns_fbDeferDelete b) ns_capDeleteV + /-- **The extended perform keeps `NoRunVM`**. -/ theorem ns_fl4PerformInvocationV (onCore : ObjRef → Bool) (blk call don : Bool) (i : Fl4InvocationV) : SInv.NSKE (fl4PerformInvocationV (Z := Unit) onCore blk call don i) := by @@ -121,6 +133,10 @@ theorem ns_fl4PerformInvocationV (onCore : ObjRef → Bool) (blk call don : Bool exact ike_bind (ike_liftE (ns_sendSignalV onCore ep badge)) fun _ => ike_returnOk _ | invokeTCB ti => exact ns_invokeTcbV ti | invokeCNode ci => exact ike_bind (ns_invokeCnodeV ci) fun _ => ike_returnOk _ + | invokeArchObject ai => + cases ai with + | frameBatch b => exact ike_liftE (ik_performFrameBatchWith (ns_fbEntryV b) (ns_flushAll _ _)) + | _ => exact ns_fl4PerformInvocation _ _ _ _ | _ => exact ns_fl4PerformInvocation _ _ _ _ | vcpu vi => exact ike_liftE (ns_performVcpuInvocation vi) | ept ei => exact ike_liftE (ik_bind (ns_performEptInvocation ei) fun _ => ik_pure _) @@ -181,6 +197,12 @@ theorem fl4PerformInvocationVF_eq_nr (onCore remoteCur : ObjRef → Bool) (blk c show ExceptT.run (invokeCnodeVF (Z := Unit) ci >>= fun _ => NonDetE.returnOk []) s = ExceptT.run (invokeCnodeV ci >>= fun _ => NonDetE.returnOk []) s exact nde_bind_congr (invokeCnodeVF_eq_nr ci hs) fun _ _ _ => rfl + | invokeArchObject ai => + cases ai with + | frameBatch b => + exact run_liftE_congr (performFrameBatchWith_agree (P := NoRunVM) hs + (ns_fbEntryV b) (ns_fbDeferDelete b) fun sl _ h => capDeleteVF_eq_nr sl h) + | _ => rfl | _ => rfl | vcpu vi => show ExceptT.run (NonDetE.liftE (ε := Unit) (performVcpuInvocationF (Z := Unit) vi)) s = diff --git a/proofs/Fl4Proofs/P3/VirtR7e.lean b/proofs/Fl4Proofs/P3/VirtR7e.lean index eb142b7d8..cc05a5ed9 100644 --- a/proofs/Fl4Proofs/P3/VirtR7e.lean +++ b/proofs/Fl4Proofs/P3/VirtR7e.lean @@ -33,6 +33,7 @@ No [J]/[P]/[W]. New file only. -/ import Fl4Proofs.P3.VirtAwaken import Fl4Proofs.P3.VirtNvTcb +import Fl4Proofs.P3.VirtFrameBatch import Fl4Proofs.P3.VirtTyK6 open Classical @@ -72,6 +73,11 @@ theorem fl4PerformInvocationV_eqNR (onCore : ObjRef → Bool) (blk call don : Bo ExceptT.run (fl4PerformInvocation blk call don i) s := by by_cases hp : PerformSubst i · cases i with + | invokeArchObject i => + cases i with + | frameBatch b => + exact run_liftE_congr (performFrameBatchWith_eqNV b hs.1 fun sl _ h => capDeleteV_eq sl h) + | _ => exact (hp : False).elim | invokeUntyped ui => obtain ⟨src, reset, base, rb, t, sz, slots, dev⟩ := ui show ExceptT.run (fl4InvokeUntypedV (Z := Unit) (.retype src reset base rb t sz slots dev) >>= fun _ => diff --git a/proofs/Fl4Proofs/P3/VmCnodeVtx.lean b/proofs/Fl4Proofs/P3/VmCnodeVtx.lean index 325831a60..0cafc557e 100644 --- a/proofs/Fl4Proofs/P3/VmCnodeVtx.lean +++ b/proofs/Fl4Proofs/P3/VmCnodeVtx.lean @@ -20,6 +20,7 @@ VCPU and EPT records, no cap kind excluded. No named premise. New file. -/ import Fl4Proofs.P3.VmCnodeRD +import Fl4Proofs.P3.DeriveVt open Aeneas Aeneas.Std Result namespace Fl4Proofs.P3 @@ -27,13 +28,6 @@ open fl4_core noncomputable section -/-- **The VT-x / thread view of an FL4 cap** (module docstring). -/ -def vkeyF : capability.Cap → Option (Nat × U64 × U64 × Option (U64 × U64)) - | .Thread t => some (0, t, 0#u64, none) - | .Vcpu w => some (1, w, 0#u64, none) - | .Ept p l m a v => some (2, p, l, if m then some (a, v) else none) - | .Frame _ _ _ _ true true a v => some (3, 0#u64, 0#u64, some (a, v)) - | _ => none theorem vkeyF_some {c : capability.Cap} {k : Nat × U64 × U64 × Option (U64 × U64)} (h : vkeyF c = some k) : (∃ t, c = .Thread t ∧ k = (0, t, 0#u64, none)) ∨ (∃ w, c = .Vcpu w ∧ k = (1, w, 0#u64, none)) ∨ @@ -75,9 +69,6 @@ theorem md_eq {m m' : Bool} {a v a' v' : U64} m = m' ∧ (m = true → a = a' ∧ v = v') := by cases m <;> cases m' <;> simp_all -/-- **A slot's cap made by a copy** (module docstring): neither an unmapped `Ept` cap nor an EPT-mapped frame cap. -/ -def NewOK (c : capability.Cap) : Prop := - (∀ p l a v, c ≠ .Ept p l false a v) ∧ ∀ p sz r d a v, c ≠ .Frame p sz r d true true a v /-- **The slot fact of a CNode-only step** (module docstring). -/ def CnVtx (th : TypedHeap) (m m' : U64 → U64) : Prop := diff --git a/proofs/Fl4Proofs/P3/VmDelFinSpec.lean b/proofs/Fl4Proofs/P3/VmDelFinSpec.lean index 3a88d2dcd..3076d72a2 100644 --- a/proofs/Fl4Proofs/P3/VmDelFinSpec.lean +++ b/proofs/Fl4Proofs/P3/VmDelFinSpec.lean @@ -142,9 +142,9 @@ theorem keepSt_guestLookupSlot (root : ObjRef) (gpa : Word) (level : Nat) : theorem keepSt_getVcpu (v : ObjRef) : KeepSt (getVcpu (Z := Unit) v) := keepSt_of_ik fun _ => by unfold getVcpu; ns_auto -/-- `guestUnmap` keeps what its arch `set_object` keeps. -/ -theorem fk_guestUnmap {β : Type} {f : State Unit → β} - (hs : ∀ p (a : ArchKernelObj), Vs.FK f (setObject (Z := Unit) p (.archObj a))) +/-- `guestUnmap` keeps the fields preserved by writes to an EPT table. -/ +theorem fk_guestUnmapEpt {β : Type} {f : State Unit → β} + (hs : ∀ p l es, Vs.FK f (setObject (Z := Unit) p (.archObj (.ept l es)))) (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : Vs.FK f (guestUnmap (Z := Unit) asid gpa ptr level table) := by unfold guestUnmap @@ -159,7 +159,14 @@ theorem fk_guestUnmap {β : Type} {f : State Unit → β} dsimp only apply dk_unless unfold storeGuestEntry - exact dk_bind (fk_of_keepSt (keepSt_getEpt _)) fun e => hs _ _ + exact dk_bind (fk_of_keepSt (keepSt_getEpt _)) fun ⟨l, _⟩ => hs _ l _ + +/-- `guestUnmap` keeps what its arch `set_object` keeps. -/ +theorem fk_guestUnmap {β : Type} {f : State Unit → β} + (hs : ∀ p (a : ArchKernelObj), Vs.FK f (setObject (Z := Unit) p (.archObj a))) + (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + Vs.FK f (guestUnmap (Z := Unit) asid gpa ptr level table) := + fk_guestUnmapEpt (fun p l es => hs p (.ept l es)) asid gpa ptr level table /-- `vcpuUpdate` keeps what its arch `set_object` keeps. -/ theorem fk_vcpuUpdate {β : Type} {f : State Unit → β} diff --git a/proofs/Fl4Proofs/P3/VmDisp.lean b/proofs/Fl4Proofs/P3/VmDisp.lean index 19c1bedc9..c084d3540 100644 --- a/proofs/Fl4Proofs/P3/VmDisp.lean +++ b/proofs/Fl4Proofs/P3/VmDisp.lean @@ -367,19 +367,19 @@ theorem VmEptState.frameMapEptReject (J : VmEptState (L := L) arI V X XL AR Z H Option.isSome_some, ↓reduceIte] rfl -/-- Labels outside the frame decoder return IllegalOperation for EPT-mapped frames. -/ -theorem VmEptState.frameOtherDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) +/-- Labels outside the reached frame decoder return `IllegalOperation`. -/ +theorem VmEptState.frameOtherAnyDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {ct lbl : U64} (hct : sched.cur_thread st s = ok ct) (hlbl : FrameLabel lbl lbl.bv) (hm : lbl ≠ fl4_abi.label.X86PageMap) (hu : lbl ≠ fl4_abi.label.X86PageUnmap) (hg : lbl ≠ fl4_abi.label.X86PageGetAddress) (ht : lbl ≠ fl4_abi.label.X86PageTestAndClearAccessed) (hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe) (p rt asid va len buf : U64) - (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (sz : capability.FrameSize) (dev mp ep blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : - syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev mp ep asid va) blk call don fp buf ex ⦃ res => VDispPostVE (L := L) arI AR MR on rc σ blk call don - (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := by + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer) s res ⦄ := by obtain ⟨o1, o2, o3⟩ := hlbl.other hm hu hg ht hd unfold syscall.decode_invocation rw [hct] @@ -400,24 +400,39 @@ theorem VmEptState.frameOtherDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs rw [if_neg o1, if_neg o2, if_neg o3] rfl +/-- Labels outside the frame decoder return IllegalOperation for EPT-mapped frames. -/ +theorem VmEptState.frameOtherDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct lbl : U64} (hct : sched.cur_thread st s = ok ct) (hlbl : FrameLabel lbl lbl.bv) + (hm : lbl ≠ fl4_abi.label.X86PageMap) (hu : lbl ≠ fl4_abi.label.X86PageUnmap) + (hg : lbl ≠ fl4_abi.label.X86PageGetAddress) (ht : lbl ≠ fl4_abi.label.X86PageTestAndClearAccessed) + (hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe) (p rt asid va len buf : U64) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + blk call don fp buf ex ⦃ res => + VDispPostVE (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := + J.frameOtherAnyDisp arI hct hlbl hm hu hg ht hd p rt asid va len buf sz dev true true blk call don fp sl ex args ci ec buffer on rc + set_option maxHeartbeats 4000000 in -/-- A large EPT-mapped frame can derive ordinary unmapped subframe caps. -/ -theorem VmEptState.frameDeriveDisp +/-- Derive unmapped subframe caps using the selected parent and destination lookups. -/ +theorem VmEptState.frameDeriveAnyDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {ct len buf pc p rt asid va : U64} {idx : List Bool} (hpc : Z.th pc = some (.cnode idx.length)) (hct : sched.cur_thread st s = ok ct) - {sz : capability.FrameSize} {dev : Bool} - (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt dev true true asid va)) + {sz : capability.FrameSize} {dev mp ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt dev mp ep asid va)) (args : List U64) (hlen : len.val = args.length) (hargs : ∀ i : U64, ∀ v, args[i.val]? = some v → msg.syscall_arg st s ct buf i = ok v) (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : JVsExtra (L := L) Z s ex ec) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : syscall.decode_invocation st arI s a fl4_abi.label.X86PageDeriveSubframe len (cslotAddr (pc.bv, idx)) - (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + (.Frame p sz rt dev mp ep asid va) blk call don fp buf ex ⦃ res => VDispPostVE (L := L) arI AR MR on rc σ blk call don (specDecV fp fl4_abi.label.X86PageDeriveSubframe.bv (args.map UScalar.bv) ci (pc.bv, idx) - (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := by + (.Frame p sz rt dev mp ep asid va) ec buffer) s res ⦄ := by let lbl := fl4_abi.label.X86PageDeriveSubframe have hn : lbl ≠ fl4_abi.label.X86PageMapEPT := by unfold lbl fl4_abi.label.X86PageDeriveSubframe fl4_abi.label.X86PageMapEPT @@ -441,12 +456,13 @@ theorem VmEptState.frameDeriveDisp have hma : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageMap := by rw [hi]; intro h; cases h have hua : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageUnmap := by rw [hi]; intro h; cases h have hspec : specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) - (.Frame p sz rt dev true true asid va) ec buffer = + (.Frame p sz rt dev mp ep asid va) ec buffer = Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.deriveSubframe (Fl4Spec.decodeDeriveSubframe (Z := Unit) (args.map UScalar.bv) (pc.bv, idx) - (.pageCap dev p.bv (vmRightsOf rt) .vmEptMap (frameSizeOf sz) (some (asid.bv, va.bv))) ec))) := by + (.pageCap dev p.bv (vmRightsOf rt) (if mp then (if ep then .vmEptMap else .vmVSpaceMap) else .vmNoMap) + (frameSizeOf sz) (if mp then some (asid.bv, va.bv) else none)) ec))) := by simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hn), hma, hua, and_false, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, if_pos (hl.dsr rfl)] change syscall.decode_invocation st arI s a lbl len _ _ blk call don fp buf ex ⦃ _ ⦄ @@ -528,6 +544,25 @@ theorem VmEptState.frameDeriveDisp exact Set.mem_singleton _ exact mem_liftE (ε := Unit) (x := ([], σ')) h +/-- A large EPT-mapped frame can derive ordinary unmapped subframe caps. -/ +theorem VmEptState.frameDeriveDisp + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct len buf pc p rt asid va : U64} {idx : List Bool} (hpc : Z.th pc = some (.cnode idx.length)) + (hct : sched.cur_thread st s = ok ct) + {sz : capability.FrameSize} {dev : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt dev true true asid va)) + (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ i : U64, ∀ v, args[i.val]? = some v → msg.syscall_arg st s ct buf i = ok v) + (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : JVsExtra (L := L) Z s ex ec) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageDeriveSubframe len (cslotAddr (pc.bv, idx)) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispPostVE (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageDeriveSubframe.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := + J.frameDeriveAnyDisp arI hni hpc hct hc args hlen hargs blk call don fp ex ci ec hex buffer on rc + set_option maxHeartbeats 4000000 in /-- Clearing the hardware accessed bit preserves the VM/EPT relation. -/ theorem VmEptState.clearAccessed @@ -749,17 +784,105 @@ theorem VmEptState.frameTacDisp [PlainStore st] exact (WP.spec_ok _).2 h set_option maxHeartbeats 4000000 in -/-- GetAddress on an EPT-mapped frame replies with the frame's physical address. -/ -theorem VmEptState.frameGetAddrDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) +/-- TestAndClearAccessed on a selected native frame walks the native ASID table. -/ +theorem VmEptState.frameTacNativeDisp [PlainStore st] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct len buf p rt asid va : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (sz : capability.FrameSize) (dev mp ep blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (hsl : AbsSlot Z.th sl) (hc : capV (L.mem s) (cslotAddr sl) = .Frame p sz rt dev mp ep asid va) + (hnat : NativeCap (.Frame p sz rt dev mp ep asid va)) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageTestAndClearAccessed len (cslotAddr sl) + (.Frame p sz rt dev mp ep asid va) blk call don fp buf ex ⦃ res => + VDispReplyVE (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageTestAndClearAccessed.bv args ci sl + (.Frame p sz rt dev mp ep asid va) ec buffer) ct s res ⦄ := by + let lbl := fl4_abi.label.X86PageTestAndClearAccessed + have hn : lbl ≠ fl4_abi.label.X86PageMapEPT := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageMapEPT + decide + have hm : lbl ≠ fl4_abi.label.X86PageMap := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageMap + decide + have hu : lbl ≠ fl4_abi.label.X86PageUnmap := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageUnmap + decide + have hg : lbl ≠ fl4_abi.label.X86PageGetAddress := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageGetAddress + decide + have hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageDeriveSubframe + decide + have hl := frame_label lbl hn + have hi : Fl4Spec.invocationType lbl.bv = .genInvocationLabel .invalidInvocation := by + rw [invocationType_eq, dataToNat_bv, show lbl.val = 58 by simp [lbl, fl4_abi.label.X86PageTestAndClearAccessed]] + rfl + have hma : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageMap := by rw [hi]; intro h; cases h + have hua : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageUnmap := by rw [hi]; intro h; cases h + let f : vspace.FrameCap := ⟨p, sz, rt, dev, mp, ep, asid, va⟩ + let dec := Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.testAndClearAccessed + (Fl4Spec.decodeTestAndClearAccessed (Z := Unit) (frameACapG f)))) + have hspec : specDecV fp lbl.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer = dec := by + cases mp <;> cases ep <;> first + | exact (hnat ⟨rfl, rfl⟩).elim + | simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hn), hma, hua, + and_false, false_and, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, + if_neg (fun h => hd (hl.ds h)), if_pos (hl.tacr rfl)] + rfl + change syscall.decode_invocation st arI s a lbl len _ _ blk call don fp buf ex ⦃ _ ⦄ + rw [hspec] + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hm, hu, hg, hd, hn, if_pos (show lbl = fl4_abi.label.X86PageTestAndClearAccessed from rfl), ↓reduceIte] + have B := J.1.1 + obtain ⟨hcur, x, hk⟩ := B.curEq hct + obtain ⟨σ1, hst, -⟩ := setThreadState_eq σ ct.bv x hk .restart + have hv := (B.vsc (cslotAddr sl) hsl.isSlot).okTab + (show TabIn Z.L3 Z.L2 Z.L1 (capV (L.mem s) (cslotAddr sl)) from by rw [hc]; trivial) + rw [hc] at hv + have huv : mp = true → UserV va := hv.2.2 + refine WP.spec_bind (tac_gen_leaf (L := L) s σ B.xtop.fin.2.1 B.xtop.fin.2.2.1 B.xtop.fin.1.2.pt + B.xtop.fin.2.2.2.1 ct hct f huv call + (fun r => VDispReplyVE (L := L) arI (AR := AR) on rc σ blk call don dec ct s (r.1, r.2, a)) + (fun e e' he hD => Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err (run_liftME_err hD)), he⟩, + J.any arI⟩) ?_) ?_ + · intro b hb hal slot hD v hv + refine WP.spec_mono (J.clearAccessedReply arI hb hal hct hRun call v) ?_ + rintro s2 ⟨hne, htail⟩ + have hst' : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) := by + rw [hcur] + exact hst + have hsel : ([v.bv], σ1) ∈ (Fl4Spec.performTestAndClearAccessed (Z := Unit) slot σ1).1 := by + simp only [Fl4Spec.performTestAndClearAccessed, Fl4Spec.NonDet.select, Set.mem_setOf_eq, + Set.mem_insert_iff, Set.mem_singleton_iff, and_true] + rcases hv with rfl | rfl + · right + rfl + · left + rfl + exact Or.inr ⟨rfl, hne, _, DecOut.of_eq (run_liftME_ok (run_liftME_ok (run_liftME_ok hD))), σ1, hst', + [v.bv], σ1, mem_liftE (ε := Unit) (x := ([v.bv], σ1)) hsel, htail σ1 hst'⟩ + · rintro ⟨r, s2⟩ h + exact (WP.spec_ok _).2 h + +set_option maxHeartbeats 4000000 in +/-- GetAddress replies with the selected frame's physical address. -/ +theorem VmEptState.frameGetAddrAnyDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {ct lbl p rt asid va len buf : U64} (hct : sched.cur_thread st s = ok ct) (hr : ZvCtRun σ ct) (hlbl : FrameLabel lbl lbl.bv) (hg : lbl = fl4_abi.label.X86PageGetAddress) - (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (sz : capability.FrameSize) (dev mp ep blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : - syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev mp ep asid va) blk call don fp buf ex ⦃ res => VDispReplyVE (L := L) arI (AR := AR) on rc σ blk call don - (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) ct s res ⦄ := by + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer) ct s res ⦄ := by subst lbl have hmap : fl4_abi.label.X86PageGetAddress ≠ fl4_abi.label.X86PageMap := by unfold fl4_abi.label.X86PageGetAddress fl4_abi.label.X86PageMap @@ -775,15 +898,16 @@ theorem VmEptState.frameGetAddrDisp (J : VmEptState (L := L) arI V X XL AR Z H C decide have hl := hlbl.ga rfl have hdec : DecOut - (specDecV fp fl4_abi.label.X86PageGetAddress.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) σ + (specDecV fp fl4_abi.label.X86PageGetAddress.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer) σ (.base (.invokeArchObject (.spec (.invokePage (.pageGetAddr p.bv))))) := by apply DecOut.of_eq simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hlbl.native), hl, - Fl4Spec.InvocationLabel.archInvocationLabel.injEq, reduceCtorEq, + Fl4Spec.InvocationLabel.archInvocationLabel.injEq, reduceCtorEq, and_false, false_and, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, if_neg (fun h => hds (hlbl.ds h)), if_neg (fun h => htac (hlbl.tac h))] exact run_liftME_ok (run_liftME_ok (run_liftME_ok - (decodeGetAddr_eq hl args sl dev p.bv (vmRightsOf rt) .vmEptMap (frameSizeOf sz) (some (asid.bv, va.bv)) ec σ))) + (decodeGetAddr_eq hl args sl dev p.bv (vmRightsOf rt) (if mp then (if ep then .vmEptMap else .vmVSpaceMap) else .vmNoMap) (frameSizeOf sz) + (if mp then some (asid.bv, va.bv) else none) ec σ))) obtain ⟨hcur, -, -⟩ := J.1.1.curEq hct obtain ⟨htcb, x, hk⟩ := J.1.1.xtop.curTcb hcur have ht := J.1.1.thT ct htcb @@ -809,6 +933,19 @@ theorem VmEptState.frameGetAddrDisp (J : VmEptState (L := L) arI V X XL AR Z H C exact ⟨((), σ2), by rw [hz]; exact Set.mem_singleton _, B2.xtop.relTop, (J1.ofNat (V' := Z.vs) B2 F2).any arI⟩ +/-- GetAddress on an EPT-mapped frame replies with the frame's physical address. -/ +theorem VmEptState.frameGetAddrDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct lbl p rt asid va len buf : U64} (hct : sched.cur_thread st s = ok ct) (hr : ZvCtRun σ ct) + (hlbl : FrameLabel lbl lbl.bv) (hg : lbl = fl4_abi.label.X86PageGetAddress) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + blk call don fp buf ex ⦃ res => + VDispReplyVE (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) ct s res ⦄ := + J.frameGetAddrAnyDisp arI hct hr hlbl hg sz dev true true blk call don fp sl ex args ci ec buffer on rc + end section @@ -819,6 +956,27 @@ variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (a variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} +/-- A two-level `liftME` outcome is the image of an inner outcome. -/ +theorem DecOut.liftME2_inv {α β γ : Type} {f : β → γ} {g : α → β} {m : Fl4Spec.SEMonad Unit α} + {σ : Fl4Spec.State Unit} {inv : γ} + (h : DecOut (Fl4Spec.NonDetE.liftME f (Fl4Spec.NonDetE.liftME g m)) σ inv) : ∃ j, inv = f (g j) := by + unfold DecOut Fl4Spec.NonDetE.liftME at h + rw [ExceptT.run_bind] at h + obtain ⟨⟨⟨r, σ'⟩, hr, hm⟩, -⟩ := h + cases r with + | error e => exact absurd (Set.mem_singleton_iff.1 hm) (by simp) + | ok k => + have e1 := Set.mem_singleton_iff.1 hm + simp only [Prod.mk.injEq, Except.ok.injEq] at e1 + rw [ExceptT.run_bind] at hr + obtain ⟨⟨r', σ''⟩, -, hm'⟩ := hr + cases r' with + | error e => exact absurd (Set.mem_singleton_iff.1 hm') (by simp) + | ok j => + have e2 := Set.mem_singleton_iff.1 hm' + simp only [Prod.mk.injEq, Except.ok.injEq] at e2 + exact ⟨j, by rw [e1.1, e2.1]⟩ + private theorem noPortPreemption (blk call don : Bool) (i : Fl4Spec.Fl4ArchInvocation) (σ : Fl4Spec.State Unit) (y : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) (hy : y ∈ ((Fl4Spec.fl4PerformInvocation blk call don (.invokeArchObject i)).run σ).1) : @@ -915,19 +1073,28 @@ theorem VmEptState.ioPortDisp (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac L exact ⟨rr, σy, hY, w, hw, hR, (J.ofNat (V' := Z.vs) B' F').any arI⟩) have archOut {inv : Fl4Spec.Fl4Invocation} (h : DecOut m σ inv) : ∃ i, inv = .invokeArchObject i := DecOut.liftME_inv h + -- the I/O-port decoder yields a `spec` arch invocation, which the VM perform does not substitute + have hbase {i : Fl4Spec.Fl4ArchInvocation} (h : DecOut m σ (.invokeArchObject i)) : + Fl4Spec.fl4PerformInvocationVF (Z := Unit) on rc blk call don (.base (.invokeArchObject i)) = + Fl4Spec.fl4PerformInvocation blk call don (.invokeArchObject i) := by + refine VirtF13.fl4PerformInvocationVF_base on rc blk call don _ ?_ + obtain ⟨j, hj⟩ := DecOut.liftME2_inv (f := Fl4Spec.Fl4Invocation.invokeArchObject) + (g := Fl4Spec.Fl4ArchInvocation.spec) h + cases hj + exact id rcases hD with hD | hD · refine WP.spec_mono hD ?_ rintro res (⟨e, hr, hs, ⟨e', hde, he⟩, hQ⟩ | ⟨inv, hde, σ1, hs, y, hy, ⟨_, _, hret, -⟩, hQ⟩) · exact Or.inl (Or.inl ⟨e, hr, hs, ⟨e', run_liftME_err hde, he⟩, hQ⟩) · obtain ⟨i, rfl⟩ := archOut hde rcases hret with ⟨hr, hy1⟩ | ⟨_, hy1⟩ - · exact Or.inl (Or.inr ⟨_, hde.liftME, σ1, hs, y, hy, Or.inl ⟨hr, hy1, hQ⟩⟩) + · exact Or.inl (Or.inr ⟨_, hde.liftME, σ1, hs, y, by rw [hbase hde]; exact hy, Or.inl ⟨hr, hy1, hQ⟩⟩) · exact (noPortPreemption blk call don i σ1 y hy hy1).elim · refine WP.spec_mono hD ?_ rintro res (⟨e, hr, hs, ⟨e', hde, he⟩, hQ⟩ | ⟨hr, hn, inv, hde, σ1, hs, rr, σy, hy, z, hz, ht, hQ⟩) · exact Or.inr (Or.inl ⟨e, hr, hs, ⟨e', run_liftME_err hde, he⟩, hQ⟩) · obtain ⟨i, rfl⟩ := archOut hde - exact Or.inr (Or.inr ⟨hr, hn, _, hde.liftME, σ1, hs, rr, σy, hy, z, hz, ht, hQ⟩) + exact Or.inr (Or.inr ⟨hr, hn, _, hde.liftME, σ1, hs, rr, σy, by rw [hbase hde]; exact hy, z, hz, ht, hQ⟩) end diff --git a/proofs/Fl4Proofs/P3/VmDispX.lean b/proofs/Fl4Proofs/P3/VmDispX.lean new file mode 100644 index 000000000..12ced2216 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmDispX.lean @@ -0,0 +1,1266 @@ +import Fl4Proofs.P3.VmDisp +import Fl4Proofs.P3.VmVxLow + +/-! +`…X` siblings of `VmDisp`'s dispatcher-chain statements: the same results concluding `VEAnyX` (`VEAny` plus the ghost +fact `VxE`), asking `[VxE Z]` of the entry state. The merged statements are unchanged. Generated by +`handoff/tools/gen_vxsib.py`. The siblings of the merged theorems below the family are in `VmVxLow`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +/-- `VEAny` with the carried `VxE` ghost fact. -/ +def VEAnyX (AR : A → Fl4Spec.Cr3 → Prop) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + ∃ (V X : Set U64) (XL : U64 → Nat) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) + (D : Set U64), VxE Z ∧ VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ + +/-- `…X` sibling of `VDispPostVE` (`VEAnyX`; module docstring). -/ +def VDispPostVEX (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) + (onCore remoteCur : Fl4Spec.ObjRef → Bool) (σ : Fl4Spec.State Unit) (blk call don : Bool) + (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV) (s : S) (res : core.result.Result Unit error.Exc × S × A) : + Prop := + (∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ (∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∧ + VEAnyX (L := L) arI AR res.2.1 res.2.2 σ) ∨ + (∃ inv, DecOut m σ inv ∧ ∃ σ1, + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ y ∈ ((Fl4Spec.fl4PerformInvocationVF (Z := Unit) onCore remoteCur blk call don inv).run σ1).1, + (res.1 = .Ok () ∧ y.1 = .ok [] ∧ VEAnyX (L := L) arI AR res.2.1 res.2.2 y.2) ∨ + (res.1 = .Err .Preempted ∧ y.1 = .error () ∧ + ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR res.2.1 res.2.2 y.2)) + +/-- `…X` sibling of `VmEptState.any` (`VEAnyX`; module docstring). -/ +theorem VmEptState.anyX {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : VEAnyX (L := L) arI AR s a σ := + ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, inferInstance, J⟩ + +/-- `…X` sibling of `CnAllPostVE.toDisp` (`VEAnyX`; module docstring). -/ +theorem CnAllPostVE.toDispX {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {res : core.result.Result Unit error.Exc × S × A} + (h : CnAllPostVE (L := L) arI AR MR Z H Cs σ m s a res) (onCore remoteCur : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) : VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don m s res := by + rcases h with ⟨e, e', rfl, he, hm⟩ | ⟨inv, σ1, hd, hst, σ', G', hr, -, hW⟩ + · exact Or.inl ⟨e, rfl, rfl, ⟨e', hm, he⟩, J.anyX arI⟩ + · refine Or.inr ⟨_, hd, σ1, hst, ?_⟩ + rcases hr with ⟨h1, hy⟩ | ⟨h1, hy⟩ + · rw [h1] at hW + obtain ⟨⟨V', X', XL', Vv, Ac', Lst', D', hJ⟩, -⟩ := hW + exact ⟨_, perfNil_ok hy, Or.inl ⟨h1, rfl, hJ.anyX arI⟩⟩ + · rw [h1] at hW + obtain ⟨V', X', XL', hP⟩ := hW + exact ⟨_, perfNil_err hy, Or.inr ⟨h1, rfl, V', X', XL', hP⟩⟩ + +/-- `…X` sibling of `EptInvPost.toDisp` (`VEAnyX`; module docstring). -/ +theorem EptInvPost.toDispX {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {res : core.result.Result Unit error.Exc × S × A} + (h : EptInvPost (L := L) arI V X XL AR Z H Cs Ac D σ m s a res) (onCore remoteCur : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) : VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don m s res := by + rcases h with ⟨e, e', rfl, he, hm⟩ | ⟨h1, inv, σ', hd, hb, Lst', hJ⟩ + · exact Or.inl ⟨e, rfl, rfl, ⟨e', hm, he⟩, J.anyX arI⟩ + · obtain ⟨-, σ1, -, -, -, -, -, hst, -⟩ := J.restartK hni + have hy : ∃ x ∈ (Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ).1, + ((), σ') ∈ (Fl4Spec.performEptInvocation (Z := Unit) inv x.2).1 := by + have : ((), σ') ∈ ((Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart >>= fun _ => + Fl4Spec.performEptInvocation (Z := Unit) inv) σ).1 := by rw [hb]; rfl + exact this + obtain ⟨x, hx, hpx⟩ := hy + rw [hst] at hx + obtain rfl : x = ((), σ1) := hx + refine Or.inr ⟨_, hd, σ1, hst, (.ok [], σ'), ?_, Or.inl ⟨h1, rfl, hJ.anyX arI⟩⟩ + show ((.ok [], σ') : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (do Fl4Spec.performEptInvocation (Z := Unit) inv; return ([] : List Fl4Spec.Data))).run + σ1).1 + exact mem_liftE (x := (([] : List Fl4Spec.Data), σ')) (mem_bind_nd hpx rfl) + +/-- `…X` sibling of `SetEptDecPost.toDisp` (`VEAnyX`; module docstring). -/ +theorem SetEptDecPost.toDispX {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {p : U64} + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {res : core.result.Result Unit error.Exc × S × A} + (h : SetEptDecPost (L := L) arI V X XL AR Z H Cs σ s a p m res) (onCore remoteCur : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) : VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don m s res := by + rcases h with ⟨e, rfl, e', hm, he⟩ | ⟨ps, idxs, c, -, hm, h1, σ1, σ3, G', Ac', Lst', D', hst, hy, hJ, -⟩ + · exact Or.inl ⟨e, rfl, rfl, ⟨e', hm, he⟩, J.anyX arI⟩ + · exact Or.inr ⟨_, DecOut.of_eq hm, σ1, hst, _, perfNil_ok hy, Or.inl ⟨h1, rfl, hJ.anyX arI⟩⟩ + +end + +section + +open Notif Endp Ipc Classical + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- `…X` sibling of `VDispReplyVE` (`VEAnyX`; module docstring). -/ +abbrev VDispReplyVEX (arI : machine.Arch A) {AR : A → Fl4Spec.Cr3 → Prop} (on rc : Fl4Spec.ObjRef → Bool) (σ : Fl4Spec.State Unit) (blk call don : Bool) + (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV) (ct : U64) (s : S) + (res : core.result.Result Unit error.Exc × S × A) : Prop := + TopPostRMXG (L := L) (Fl4Spec.fl4PerformInvocationVF on rc) (VEAnyX (L := L) arI AR) + σ blk call don m ct s res + +/-- `…X` sibling of `VmEptState.replyTailPost` (`VEAnyX`; module docstring). -/ +private theorem VmEptState.replyTailPostX + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (hct : ct ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) + (hrun : x.tcbState = .running) (hcur : σ.curThread = ct.bv) (call : Bool) (ms : List Fl4Spec.Data) + (hlen : ms.length ≤ Fl4Spec.msgMaxLength) {s' : S} {Lst' : U64 → List U64} + (B : VmBaseF (L := L) arI s σ V AR Z H Cs Ac Lst' D s' a + (stsAct (updK (if call then replyState σ ct.bv x ms else σ) ct.bv + (.tcb { (if call then replyTcbB x (ipcBufOf true x) ms else x) with + tcbState := .running })) ct.bv)) : + L.mem s' (stA ct) ≠ sched.TS_RESTART ∧ + ∃ z ∈ (specTail call ct.bv ms (zcR ct.bv σ)).1, RelTopM (L := L) s' z.2 ∧ + VEAnyX (L := L) arI AR s' a z.2 := by + let x' := { (if call then replyTcbB x (ipcBufOf true x) ms else x) with tcbState := .running } + let σ' := stsAct (updK (if call then replyState σ ct.bv x ms else σ) ct.bv (.tcb x')) ct.bv + have hk' : σ'.kheap ct.bv = some (.tcb x') := by + simp only [σ', stsAct_kheap, updK_kheap, Function.update_self] + have J' : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D s' a σ' := by + refine J.ofFBind B (fun v _ old new ho hn => ?_) + by_cases hv : v.bv = ct.bv + · rw [hv, hk] at ho + cases ho + · have hs : σ'.kheap v.bv = σ.kheap v.bv := by + simp only [σ', stsAct_kheap, updK_kheap, Function.update_of_ne hv] + cases call + · rfl + · exact replyStateB_kheap_ne σ ct.bv x (ipcBufOf true x) ms hv + rw [hs] at hn + exact VcpuBindingKeep.refl σ v old new ho hn + have hz : ((), σ') ∈ (specTail call ct.bv ms (zcR ct.bv σ)).1 := by + rw [zc_tail hk hrun hcur call ms hlen (fun p hp z hz => J.1.1.bufFrames hct hk p hp z.1 (mrPairs_lt hz))] + exact Set.mem_singleton _ + exact ⟨J'.1.1.notRestart hct hk' (by intro h; cases h), ((), σ'), hz, J'.1.1.xtop.relTop, J'.anyX arI⟩ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.replyWsTailPost` (`VEAnyX`; module docstring). -/ +private theorem VmEptState.replyWsTailPostX + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (hct : ct ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) + (hrun : x.tcbState = .running) (hcur : σ.curThread = ct.bv) (call : Bool) (ws : List U64) + (hlen : (ws.map (·.bv)).length ≤ Fl4Spec.msgMaxLength) {s' : S} + (hs : vcpuReplyWs st s ct call ws = ok s') : + L.mem s' (stA ct) ≠ sched.TS_RESTART ∧ + ∃ z ∈ (specTail call ct.bv (ws.map (·.bv)) (zcR ct.bv σ)).1, + RelTopM (L := L) s' z.2 ∧ VEAnyX (L := L) arI AR s' a z.2 := by + let ms : List Fl4Spec.Data := ws.map (fun w => w.bv) + let σ1 := stsAct (updK (if call then replyState σ ct.bv x ms else σ) ct.bv + (.tcb { (if call then replyTcbB x (ipcBufOf true x) ms else x) with tcbState := .running })) ct.bv + let Q (r : S) := ∃ Lst' : U64 → List U64, + VmBaseF (L := L) arI s σ V AR Z H Cs Ac Lst' D r a σ1 + have hl : ws.length ≤ 120 := by + have hm : (ws.map (fun w : U64 => w.bv)).length ≤ 120 := hlen + simpa only [List.length_map] using hm + have hR : vcpuReplyWs st s ct call ws ⦃ Q ⦄ := + VmBaseF.replyWsRun3 (sA := s) (σA := σ) J.toF hct hk hrun call ws hl + obtain ⟨Lst', B⟩ : Q s' := spec_ok_post (P := Q) hR hs + exact VmEptState.replyTailPostX (s' := s') (Lst' := Lst') J hct hk hrun hcur call ms hlen B + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.vcpuDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.vcpuDispX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hK : ARKeep arI AR) (hSy : ZvSyncKeep arI AR) (hVA : ZvVirtAgree arI) + (hCap : ∀ a', VirtSanEq.CapAgree arI a') (hAT : P0.ArchTotal arI) + {p lbl len buf ct : U64} (hp : p ∈ V) (hcan : Canonical p) + (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (blk call don fp : Bool) (hbuf : BufOut V buf) (ex : Array obj.Slot 3#usize) + (args : List Fl4Spec.Data) (hargs : VArgs st s ct buf len args) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (he : ExtraOK Z.th (L.mem s) ex ec) + (on rc : Fl4Spec.ObjRef → Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (buffer : Option Fl4Spec.ObjRef) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Vcpu p) blk call don fp buf ex ⦃ res => + let m := specDecV fp lbl.bv args ci sl (.Vcpu p) ec buffer + VDispPostVEX (L := L) arI AR MR on rc σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don m ct s res ⦄ := by + have B := J.1.1 + obtain ⟨hcur, -, -⟩ := B.curEq hct + obtain ⟨htcb, x, hk⟩ := B.xtop.curTcb hcur + have ht : ct ∈ Z.T := B.thT ct htcb + have hrun := hRun x hk + have hsl := exAt_idx0 ex + obtain ⟨b, hbn, -⟩ := P1.spec_ex_fd (P0.obj.Slot.is_null.p0 (Ipc.exAt ex 0)) + have hex := ExtraOK.vcaps B.xtop.rel.1 he hsl hbn + have hslot := fun hb : b = false => ExtraOK.eioSlot B.xtop.rel.1 he (Ipc.exAt ex 0) hsl (hb ▸ hbn) + have hcap : b = false → obj.Slot.cap st (Ipc.exAt ex 0) s = ok (capV (L.mem s) (Ipc.exAt ex 0)) := by + intro hb + rw [slot_cap_eq, capAt_capV] + have hth : b = false → ∀ t, capV (L.mem s) (Ipc.exAt ex 0) = .Thread t → Z.th t = some .tcb := by + intro hb t h + obtain ⟨pc, r, idx, hpc, hidx, e⟩ := hslot hb + have Hs : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hs : IsSlot Z.th (Ipc.exAt ex 0) := by rw [e]; exact Hs.isSlot + exact B.thrCap hs t h + rw [VirtDispVcpu.decode_invocation_vcpu_eq arI] + refine WP.spec_mono (J.vcpuDecodeJ hK hSy hVA hCap hAT hp hcan call hbuf ex hsl hbn hcap hslot hth) ?_ + rintro res ⟨ct', hct', hPost⟩ + have hctEq : ct' = ct := ok_inj (hct'.symm.trans hct) + subst ct' + rcases hPost args ec hargs hex with ⟨e, e', hr, he', hm, hs, J'⟩ | + ⟨inv, rep, s', σ', Ac', Lst', hr, hm, hperf, J', hTail⟩ + · exact Or.inl (Or.inl ⟨e, hr, hs, ⟨e', VirtDispVcpu.decV_err hm, Or.inl he'⟩, hs.symm ▸ J'.anyX arI⟩) + · have hde := VirtDispVcpu.decV_ok (fp := fp) (ci := ci) (slot := sl) (buffer := buffer) hm + have hkc : σ.kheap σ.curThread = some (.tcb x) := by rw [hcur]; exact hk + have hsl5 : ∀ v lo hi a sl', inv = .enableIoPort v lo hi a sl' → sl'.1 ≠ σ.curThread := by + rintro v lo hi a sl' rfl + have hd := zv_dec_eio_slot (zv_decV_mem hde) + obtain ⟨c0, rest, hec⟩ := hd + cases b + · obtain ⟨csl, rest', ee, hAs, haa⟩ := hex.2 rfl + obtain ⟨pc, r, idx, hpc, hidx, hps⟩ := hslot rfl + rw [hec] at ee + obtain rfl : sl' = csl := congrArg Prod.snd (List.cons.inj ee).1 + have hAs' : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + rw [cslotAddr_inj B.xtop.rel.1 hAs hAs' (haa.trans hps), hcur] + intro e + have ep : pc = ct := VirtSanEq.u64_ext e + subst ep + rw [htcb] at hpc + cases hpc + · rw [hex.1 rfl] at hec + cases hec + obtain ⟨hst, hpR, hk', hc'⟩ := zc_restart_perform hperf hkc (by rw [hrun]; intro h; cases h) + hsl5 on rc blk call don + rw [hcur] at hst hpR hk' hc' + have hstCur : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), zcR ct.bv σ)}, False) := by + rw [hcur] + exact hst + have hmem : (.ok rep, zcR ct.bv σ') ∈ + ((Fl4Spec.fl4PerformInvocationVF on rc blk call don (.vcpu inv)).run (zcR ct.bv σ)).1 := by + rw [hpR] + exact Set.mem_singleton _ + have hct' : sched.cur_thread st s' = ok ct := by + obtain ⟨c', heq, -⟩ := P1.spec_ex_fd (P0.sched.cur_thread.p0 st s' (P1.LawfulStore.total (st := st))) + rw [heq, VirtSanEq.u64_ext ((J'.1.1.curEq heq).1.symm.trans hc')] + rcases hTail with ⟨rfl, hs⟩ | ⟨y, rfl, hs⟩ | ⟨ws, rfl, hs⟩ + · obtain ⟨s1, σ1, -, eσ1, hs1, Ac1, Lst1, D1, B1, hF⟩ := + J'.1.1.restartRunJ hct' hk' (by rw [hrun]; rfl) + have eσ : σ1 = zcR ct.bv σ' := eσ1.trans (zcR_of hk').symm + rw [ok_inj (hs1.symm.trans hs), eσ] at B1 hF + have J1 := J'.ofNat (V' := Z.vs) B1 hF + exact Or.inl (Or.inr ⟨_, DecOut.of_eq hde, zcR ct.bv σ, hstCur, _, hmem, Or.inl ⟨hr, rfl, J1.anyX arI⟩⟩) + · have hR := J'.toF.vreply3 hct' ht hk' hrun call y + rw [hs, WP.spec_ok] at hR + obtain ⟨Lst1, B1⟩ := hR + obtain ⟨hn, hz⟩ := J'.replyTailPostX ht hk' hrun hc' call [y.bv] (by simp [Fl4Spec.msgMaxLength]) B1 + exact Or.inr (Or.inr ⟨hr, hn, _, DecOut.of_eq hde, zcR ct.bv σ, hstCur, _, _, hmem, hz⟩) + · have hlen : (ws.map (·.bv)).length ≤ 64 := zv_rep_len (zv_decV_mem hde) hperf + have hmax : (ws.map (·.bv)).length ≤ Fl4Spec.msgMaxLength := + Nat.le_trans hlen (by decide) + obtain ⟨hn, hz⟩ := VmEptState.replyWsTailPostX J' ht hk' hrun hc' call ws hmax hs + exact Or.inr (Or.inr ⟨hr, hn, _, DecOut.of_eq hde, zcR ct.bv σ, hstCur, _, _, hmem, hz⟩) + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P3.Ipc Fl4Proofs.P1 + +/-- `…X` sibling of `pageMapEpt_label` (`VEAnyX`; module docstring). -/ +private theorem pageMapEpt_label {lbl : U64} (hne : lbl ≠ fl4_abi.label.X86PageMapEPT) : + Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4PageMapEptLabel := by + intro h + apply hne + apply UScalar.eq_of_val_eq + rw [← UScalar.bv_toNat, show fl4_abi.label.X86PageMapEPT.val = 60 by + unfold fl4_abi.label.X86PageMapEPT + rfl] + exact h + +/-- `…X` sibling of `VmEptState.frameMapEptReject` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameMapEptRejectX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (p rt asid va len buf : U64) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageMap len (cslotAddr sl) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don (specDecV fp fl4_abi.label.X86PageMap.bv + args ci sl (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := by + have hne : fl4_abi.label.X86PageMap ≠ fl4_abi.label.X86PageMapEPT := by + unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageMapEPT + decide + have hmap : Fl4Spec.invocationType fl4_abi.label.X86PageMap.bv = .archInvocationLabel .x64PageMap := by + rw [invocationType_eq, dataToNat_bv, show fl4_abi.label.X86PageMap.val = 55 by + unfold fl4_abi.label.X86PageMap + rfl] + rfl + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hne, ↓reduceIte, error.invalid_cap, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hne), hmap, and_self, + Option.isSome_some, ↓reduceIte] + rfl + +/-- `…X` sibling of `VmEptState.frameOtherAnyDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameOtherAnyDispX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct lbl : U64} (hct : sched.cur_thread st s = ok ct) (hlbl : FrameLabel lbl lbl.bv) + (hm : lbl ≠ fl4_abi.label.X86PageMap) (hu : lbl ≠ fl4_abi.label.X86PageUnmap) + (hg : lbl ≠ fl4_abi.label.X86PageGetAddress) (ht : lbl ≠ fl4_abi.label.X86PageTestAndClearAccessed) + (hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe) (p rt asid va len buf : U64) + (sz : capability.FrameSize) (dev mp ep blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev mp ep asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer) s res ⦄ := by + obtain ⟨o1, o2, o3⟩ := hlbl.other hm hu hg ht hd + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hlbl.native, hm, hu, hg, ht, hd, ↓reduceIte, + error.illegal, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.illegalOperation, ?_, Or.inl trivial⟩, J.anyX arI⟩ + simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hlbl.native), o1, o2, + and_false, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, + if_neg (fun h => hd (hlbl.ds h)), if_neg (fun h => ht (hlbl.tac h))] + refine run_liftME_err (run_liftME_err (run_liftME_err ?_)) + unfold Fl4Spec.archDecodeInvocation + dsimp only + unfold Fl4Spec.decodePageInvocation + dsimp only + rw [if_neg o1, if_neg o2, if_neg o3] + rfl + +/-- `…X` sibling of `VmEptState.frameOtherDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameOtherDispX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct lbl : U64} (hct : sched.cur_thread st s = ok ct) (hlbl : FrameLabel lbl lbl.bv) + (hm : lbl ≠ fl4_abi.label.X86PageMap) (hu : lbl ≠ fl4_abi.label.X86PageUnmap) + (hg : lbl ≠ fl4_abi.label.X86PageGetAddress) (ht : lbl ≠ fl4_abi.label.X86PageTestAndClearAccessed) + (hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe) (p rt asid va len buf : U64) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := + J.frameOtherAnyDispX arI hct hlbl hm hu hg ht hd p rt asid va len buf sz dev true true blk call don fp sl ex args ci ec buffer on rc + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.frameDeriveAnyDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameDeriveAnyDispX + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct len buf pc p rt asid va : U64} {idx : List Bool} (hpc : Z.th pc = some (.cnode idx.length)) + (hct : sched.cur_thread st s = ok ct) + {sz : capability.FrameSize} {dev mp ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt dev mp ep asid va)) + (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ i : U64, ∀ v, args[i.val]? = some v → msg.syscall_arg st s ct buf i = ok v) + (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : JVsExtra (L := L) Z s ex ec) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageDeriveSubframe len (cslotAddr (pc.bv, idx)) + (.Frame p sz rt dev mp ep asid va) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageDeriveSubframe.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt dev mp ep asid va) ec buffer) s res ⦄ := by + let lbl := fl4_abi.label.X86PageDeriveSubframe + have hn : lbl ≠ fl4_abi.label.X86PageMapEPT := by + unfold lbl fl4_abi.label.X86PageDeriveSubframe fl4_abi.label.X86PageMapEPT + decide + have hm : lbl ≠ fl4_abi.label.X86PageMap := by + unfold lbl fl4_abi.label.X86PageDeriveSubframe fl4_abi.label.X86PageMap + decide + have hu : lbl ≠ fl4_abi.label.X86PageUnmap := by + unfold lbl fl4_abi.label.X86PageDeriveSubframe fl4_abi.label.X86PageUnmap + decide + have hg : lbl ≠ fl4_abi.label.X86PageGetAddress := by + unfold lbl fl4_abi.label.X86PageDeriveSubframe fl4_abi.label.X86PageGetAddress + decide + have ht : lbl ≠ fl4_abi.label.X86PageTestAndClearAccessed := by + unfold lbl fl4_abi.label.X86PageDeriveSubframe fl4_abi.label.X86PageTestAndClearAccessed + decide + have hl := frame_label lbl hn + have hi : Fl4Spec.invocationType lbl.bv = .genInvocationLabel .invalidInvocation := by + rw [invocationType_eq, dataToNat_bv, show lbl.val = 57 by simp [lbl, fl4_abi.label.X86PageDeriveSubframe]] + rfl + have hma : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageMap := by rw [hi]; intro h; cases h + have hua : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageUnmap := by rw [hi]; intro h; cases h + have hspec : specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt dev mp ep asid va) ec buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.deriveSubframe + (Fl4Spec.decodeDeriveSubframe (Z := Unit) (args.map UScalar.bv) (pc.bv, idx) + (.pageCap dev p.bv (vmRightsOf rt) (if mp then (if ep then .vmEptMap else .vmVSpaceMap) else .vmNoMap) + (frameSizeOf sz) (if mp then some (asid.bv, va.bv) else none)) ec))) := by + simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hn), hma, hua, + and_false, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, if_pos (hl.dsr rfl)] + change syscall.decode_invocation st arI s a lbl len _ _ blk call don fp buf ex ⦃ _ ⦄ + rw [hspec] + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hm, hu, hg, ht, hn, if_pos (show lbl = fl4_abi.label.X86PageDeriveSubframe from rfl), ↓reduceIte] + rcases hex.hext with ⟨hex0, rfl⟩ | ⟨y, nc, nc', nsl, rest, hex0, hcy, hnr, rfl⟩ + · unfold vspace.derive_subframe + rw [hct, hex0] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, ⟨_, run_liftME_err (run_liftME_err (run_liftME_err + (decodeSub_trunc (Or.inl rfl) _ _ σ))), Or.inl trivial⟩, J.anyX arI⟩ + have B := J.1.1 + have hM := B.xtop.rel.2.1.2.2.1 + have hVC := B.vsc.caps hM + have hAs : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl hpc⟩ + have hms := (hM.members _).2 ⟨hAs.isSlot, _, hc, by simp⟩ + have hno := NoOverlap.hno B.xtop.rel.2.2.2.2.1.1.2.2 hms (by rw [capV_of_capAt hc]; rfl) + rw [capV_of_capAt hc] at hno + have hy := hex.hxs y hex0 + have hnv : CapValid Z.th nc := by + have h := Vs.capValid_slot B.xtop.rel.2.1.2.1 hy + rwa [capV_of_capAt hcy] at h + let Q := fun G s' σ' => ∃ Ac' Lst' D', + VmEptState (L := L) arI V X XL AR (Z.mv G Z.vs) H Cs Ac' Lst' D' s' a σ' + have hRestart : ∃ s1 σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 ∧ Q Z.G s1 σ1 := by + obtain ⟨_, x, hk, hr⟩ := B.curRunnable hni hct + obtain ⟨s1, σ1, hst, _, hs1, Ac1, Lst1, D1, B1, F1⟩ := B.restartRunJ hct hk hr + exact ⟨s1, σ1, hst, hs1, Ac1, Lst1, D1, J.ofNat (V' := Z.vs) B1 F1⟩ + have hBase : ∀ {s1 σ1 G1}, Q G1 s1 σ1 → ∃ Ac1 Lst1 D1, + JAtZB (L := L) arI AR (Z.mv G1 Z.vs) H Cs Ac1 Lst1 D1 s1 a σ1 := by + rintro _ _ _ ⟨Ac1, Lst1, D1, J1⟩ + exact ⟨Ac1, Lst1, D1, J1.1.1⟩ + refine WP.spec_mono (derive_subframeQ arI B hpc rfl hc (fun _ _ => hno) hVC ct hct len buf args hlen hargs + ex hex0 hy hcy hnr hnv nsl rest (Q := Q) hRestart hBase ?_) ?_ + · rintro si σi hQi hSi hal dp r hdp hr first count base vm hfc hb1 hb2 hvm hbal hemp hDL + refine WP.spec_mono (insert_subframesQ (Z := Z) hQi hpc rfl hSi hal hdp hr hfc hb1 hb2 hvm hbal hemp hDL ?_) ?_ + · rintro sj σj Gj k q ⟨Acj, Lstj, Dj, Jj⟩ hSj hk hnull hq1 hq2 hqa hDLj + refine JAtZB.subInsQ (Z := Z.mv Gj Z.vs) Jj.1.1 hpc rfl hSj hal hdp hr hk hnull hq1 hq2 hvm hqa hDLj ?_ + intro sf σf Gf Bf hw hvT hcn hmdb hOther hNew hTCB + have hw' : P1.WritesIn st sj sf (SlotWd Z.th) := + ⟨hw.1, hw.2.1, fun b hb hn => hw.2.2 b hb fun h => hn (Sched.slotSpan_wd Bf.xtop.rel.1 hb h)⟩ + have hV : CnVtx Z.th (L.mem sj) (L.mem sf) := by + refine ⟨id, fun _ hy => hy, fun _ _ _ _ h => h, fun z hz => ⟨z, hz, rfl⟩, ?_⟩ + intro y hy + simp only [id_eq] + by_cases e : y = slotAddr dp.bv (BitVec.ofNat 64 k) + · subst y + rw [hNew, capV_of_capAt hnull] + exact Or.inl rfl + · exact Or.inl (congrArg vkeyF (hOther y hy e)) + have h5 : EptSlotStep Z.th Z.th (L.mem sj) (L.mem sf) := by + intro t ht + have he : cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 5) = slotOf t 5 := by + show slotAddr t.bv (Fl4Spec.ofBl _) = _ + rw [slotAddr_ofBl, tcbCnodeIndex_blVal 5 (by decide)] + rw [he] + have e := hTCB 5 (by decide) t ht + rw [capAt_capV, capAt_capV] at e + exact Or.inr (Or.inr ⟨ht, eq_of_ok_eq e⟩) + exact ⟨Acj, Lstj, Dj, Jj.ofCn Bf hw' hV hmdb h5 hcn⟩ + · rintro s' ⟨σ', G', hmap, _, _, _, hQ⟩ + exact ⟨σ', G', hmap, hQ⟩ + · rintro res (⟨e, e', h1, he, hD, hs, ha⟩ | ⟨h1, ha, iv, hD, σ', hp, G', Ac', Lst', D', J'⟩) + · exact Or.inl ⟨e, h1, hs, ⟨e', run_liftME_err (run_liftME_err (run_liftME_err hD)), Or.inl he⟩, + hs.symm ▸ ha.symm ▸ J.anyX arI⟩ + · obtain ⟨s1, σ1, hst, _, _⟩ := hRestart + rw [nd_bind_one hst] at hp + refine Or.inr ⟨_, DecOut.of_eq (run_liftME_ok (run_liftME_ok (run_liftME_ok hD))), σ1, hst, + (.ok [], σ'), ?_, Or.inl ⟨h1, rfl, ha.symm ▸ J'.anyX arI⟩⟩ + have h : ([], σ') ∈ (Fl4Spec.archNoReturn (Fl4Spec.performDeriveSubframe (Z := Unit) iv) σ1).1 := by + unfold Fl4Spec.archNoReturn + rw [nd_bind_one hp] + exact Set.mem_singleton _ + exact mem_liftE (ε := Unit) (x := ([], σ')) h + +/-- `…X` sibling of `VmEptState.frameDeriveDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameDeriveDispX + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct len buf pc p rt asid va : U64} {idx : List Bool} (hpc : Z.th pc = some (.cnode idx.length)) + (hct : sched.cur_thread st s = ok ct) + {sz : capability.FrameSize} {dev : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt dev true true asid va)) + (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ i : U64, ∀ v, args[i.val]? = some v → msg.syscall_arg st s ct buf i = ok v) + (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : JVsExtra (L := L) Z s ex ec) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageDeriveSubframe len (cslotAddr (pc.bv, idx)) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageDeriveSubframe.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ := + J.frameDeriveAnyDispX arI hni hpc hct hc args hlen hargs blk call don fp ex ci ec hex buffer on rc + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.clearAccessedReply` (`VEAnyX`; module docstring). -/ +theorem VmEptState.clearAccessedReplyX + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {b ct : U64} (hb : PtW Z.L1 b ∨ PdW Z.L2 b) (hal : Aligned b) + (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) (call : Bool) (v : U64) : + vspace.reply1 st (L.wr s b (L.mem s b &&& ~~~32#u64)) ct call v ⦃ s2 => + L.mem s2 (stA ct) ≠ sched.TS_RESTART ∧ + ∀ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + ∃ z ∈ (specTail call ct.bv [v.bv] σ1).1, + RelTopM (L := L) s2 z.2 ∧ VEAnyX (L := L) arI AR s2 a z.2 ⦄ := by + have J0 := J.clearAccessed arI hb hal + obtain ⟨hcur, x, hk⟩ := J.1.1.curEq hct + have ht := J.1.1.thT ct (J.1.1.xtop.curTcb hcur).1 + have hr := hRun x hk + refine J0.1.1.reply1Prep ht hk call v ?_ + intro s1 B F + let y := if call then Endp.replyTcb x v.bv else x + have hy : (if call then Notif.updK σ ct.bv (.tcb (Endp.replyTcb x v.bv)) else σ).kheap ct.bv = some (.tcb y) := by + cases call <;> simp [y, Notif.updK, hk] + have hyr : y.tcbState = .running := by + cases call <;> exact hr + refine WP.spec_mono (B.runRJDB ht hy hyr) ?_ + rintro s2 ⟨Lst2, B2, _, _, hW⟩ + let σy := if call then Notif.updK σ ct.bv (.tcb (Endp.replyTcb x v.bv)) else σ + let ko : Fl4Spec.KernelObject := .tcb { y with tcbState := .running } + have hF2 : NatFoot (L := L) Z s1 σy s2 (Notif.stsAct (Notif.updK σy ct.bv ko) ct.bv) := + NatFoot.ofT B.xtop hW (fun _ h => B.tfoot_nh h) + (natK_tcbW hy (Notif.stsAct_kheap (Notif.updK σy ct.bv ko) ct.bv) (by intro h; cases h)) + have J2 := J0.ofNat (V' := Z.vs) B2 (J0.1.1.natTrans F hF2) + have he : Notif.stsAct (Notif.updK (if call then Notif.updK σ ct.bv (.tcb (Endp.replyTcb x v.bv)) else σ) + ct.bv (.tcb { y with tcbState := .running })) ct.bv = + Notif.stsAct (Notif.updK σ ct.bv (.tcb { y with tcbState := .running })) ct.bv := by + cases call <;> simp only [Bool.false_eq_true, ↓reduceIte, updK_updK] + rw [he] at B2 J2 + have hk2 : (Notif.stsAct (Notif.updK σ ct.bv (.tcb { y with tcbState := .running })) ct.bv).kheap ct.bv = + some (.tcb { y with tcbState := .running }) := by simp [Notif.stsAct_kheap, Notif.updK_kheap] + refine ⟨B2.notRestart ht hk2 (by intro e; cases e), fun σ1 hst => ?_⟩ + rw [hcur, Notif.setThreadState_eq .restart hk] at hst + have he1 : σ1 = Notif.stsAct (Notif.updK σ ct.bv (.tcb { x with tcbState := .restart })) ct.bv := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hst))).2.symm + subst σ1 + exact ⟨_, specTail_one_after_restart σ ct x call v, B2.xtop.relTop, J2.anyX arI⟩ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.frameTacDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameTacDispX [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct len buf p rt asid va : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (hsl : AbsSlot Z.th sl) (hc : capV (L.mem s) (cslotAddr sl) = .Frame p sz rt dev true true asid va) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageTestAndClearAccessed len (cslotAddr sl) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageTestAndClearAccessed.bv args ci sl + (.Frame p sz rt dev true true asid va) ec buffer) ct s res ⦄ := by + let lbl := fl4_abi.label.X86PageTestAndClearAccessed + have hn : lbl ≠ fl4_abi.label.X86PageMapEPT := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageMapEPT + decide + have hm : lbl ≠ fl4_abi.label.X86PageMap := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageMap + decide + have hu : lbl ≠ fl4_abi.label.X86PageUnmap := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageUnmap + decide + have hg : lbl ≠ fl4_abi.label.X86PageGetAddress := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageGetAddress + decide + have hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageDeriveSubframe + decide + have hl := frame_label lbl hn + have hi : Fl4Spec.invocationType lbl.bv = .genInvocationLabel .invalidInvocation := by + rw [invocationType_eq, dataToNat_bv, show lbl.val = 58 by simp [lbl, fl4_abi.label.X86PageTestAndClearAccessed]] + rfl + have hma : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageMap := by rw [hi]; intro h; cases h + have hua : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageUnmap := by rw [hi]; intro h; cases h + let f : vspace.FrameCap := ⟨p, sz, rt, dev, true, true, asid, va⟩ + let dec := Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.testAndClearAccessed + (Fl4Spec.decodeTestAndClearAccessed (Z := Unit) (frameACapG f)))) + have hspec : specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer = dec := by + simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hn), hma, hua, + and_false, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, + if_neg (fun h => hd (hl.ds h)), if_pos (hl.tacr rfl)] + rfl + change syscall.decode_invocation st arI s a lbl len _ _ blk call don fp buf ex ⦃ _ ⦄ + rw [hspec] + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hm, hu, hg, hd, hn, if_pos (show lbl = fl4_abi.label.X86PageTestAndClearAccessed from rfl), ↓reduceIte] + have B := J.1.1 + obtain ⟨hcur, x, hk⟩ := B.curEq hct + obtain ⟨σ1, hst, -⟩ := setThreadState_eq σ ct.bv x hk .restart + have huv : UserV va := by + have hv := J.2.exte.vtop.2 _ hsl.isSlot p sz rt dev asid va hc + exact Nat.le_of_lt_succ hv + refine WP.spec_bind (tac_gen_leaf (L := L) s σ B.xtop.fin.2.1 B.xtop.fin.2.2.1 B.xtop.fin.1.2.pt + B.xtop.fin.2.2.2.1 ct hct f (fun _ => huv) call + (fun r => VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don dec ct s (r.1, r.2, a)) + (fun e e' he hD => Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err (run_liftME_err hD)), he⟩, + J.anyX arI⟩) ?_) ?_ + · intro b hb hal slot hD v hv + refine WP.spec_mono (J.clearAccessedReplyX arI hb hal hct hRun call v) ?_ + rintro s2 ⟨hne, htail⟩ + have hst' : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) := by + rw [hcur] + exact hst + have hsel : ([v.bv], σ1) ∈ (Fl4Spec.performTestAndClearAccessed (Z := Unit) slot σ1).1 := by + simp only [Fl4Spec.performTestAndClearAccessed, Fl4Spec.NonDet.select, Set.mem_setOf_eq, + Set.mem_insert_iff, Set.mem_singleton_iff, and_true] + rcases hv with rfl | rfl + · right + rfl + · left + rfl + exact Or.inr ⟨rfl, hne, _, DecOut.of_eq (run_liftME_ok (run_liftME_ok (run_liftME_ok hD))), σ1, hst', + [v.bv], σ1, mem_liftE (ε := Unit) (x := ([v.bv], σ1)) hsel, htail σ1 hst'⟩ + · rintro ⟨r, s2⟩ h + exact (WP.spec_ok _).2 h + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.frameTacNativeDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameTacNativeDispX [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct len buf p rt asid va : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (sz : capability.FrameSize) (dev mp ep blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (hsl : AbsSlot Z.th sl) (hc : capV (L.mem s) (cslotAddr sl) = .Frame p sz rt dev mp ep asid va) + (hnat : NativeCap (.Frame p sz rt dev mp ep asid va)) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageTestAndClearAccessed len (cslotAddr sl) + (.Frame p sz rt dev mp ep asid va) blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageTestAndClearAccessed.bv args ci sl + (.Frame p sz rt dev mp ep asid va) ec buffer) ct s res ⦄ := by + let lbl := fl4_abi.label.X86PageTestAndClearAccessed + have hn : lbl ≠ fl4_abi.label.X86PageMapEPT := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageMapEPT + decide + have hm : lbl ≠ fl4_abi.label.X86PageMap := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageMap + decide + have hu : lbl ≠ fl4_abi.label.X86PageUnmap := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageUnmap + decide + have hg : lbl ≠ fl4_abi.label.X86PageGetAddress := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageGetAddress + decide + have hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe := by + unfold lbl fl4_abi.label.X86PageTestAndClearAccessed fl4_abi.label.X86PageDeriveSubframe + decide + have hl := frame_label lbl hn + have hi : Fl4Spec.invocationType lbl.bv = .genInvocationLabel .invalidInvocation := by + rw [invocationType_eq, dataToNat_bv, show lbl.val = 58 by simp [lbl, fl4_abi.label.X86PageTestAndClearAccessed]] + rfl + have hma : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageMap := by rw [hi]; intro h; cases h + have hua : Fl4Spec.invocationType lbl.bv ≠ .archInvocationLabel .x64PageUnmap := by rw [hi]; intro h; cases h + let f : vspace.FrameCap := ⟨p, sz, rt, dev, mp, ep, asid, va⟩ + let dec := Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.testAndClearAccessed + (Fl4Spec.decodeTestAndClearAccessed (Z := Unit) (frameACapG f)))) + have hspec : specDecV fp lbl.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer = dec := by + cases mp <;> cases ep <;> first + | exact (hnat ⟨rfl, rfl⟩).elim + | simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hn), hma, hua, + and_false, false_and, ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, + if_neg (fun h => hd (hl.ds h)), if_pos (hl.tacr rfl)] + rfl + change syscall.decode_invocation st arI s a lbl len _ _ blk call don fp buf ex ⦃ _ ⦄ + rw [hspec] + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hm, hu, hg, hd, hn, if_pos (show lbl = fl4_abi.label.X86PageTestAndClearAccessed from rfl), ↓reduceIte] + have B := J.1.1 + obtain ⟨hcur, x, hk⟩ := B.curEq hct + obtain ⟨σ1, hst, -⟩ := setThreadState_eq σ ct.bv x hk .restart + have hv := (B.vsc (cslotAddr sl) hsl.isSlot).okTab + (show TabIn Z.L3 Z.L2 Z.L1 (capV (L.mem s) (cslotAddr sl)) from by rw [hc]; trivial) + rw [hc] at hv + have huv : mp = true → UserV va := hv.2.2 + refine WP.spec_bind (tac_gen_leaf (L := L) s σ B.xtop.fin.2.1 B.xtop.fin.2.2.1 B.xtop.fin.1.2.pt + B.xtop.fin.2.2.2.1 ct hct f huv call + (fun r => VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don dec ct s (r.1, r.2, a)) + (fun e e' he hD => Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err (run_liftME_err hD)), he⟩, + J.anyX arI⟩) ?_) ?_ + · intro b hb hal slot hD v hv + refine WP.spec_mono (J.clearAccessedReplyX arI hb hal hct hRun call v) ?_ + rintro s2 ⟨hne, htail⟩ + have hst' : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) := by + rw [hcur] + exact hst + have hsel : ([v.bv], σ1) ∈ (Fl4Spec.performTestAndClearAccessed (Z := Unit) slot σ1).1 := by + simp only [Fl4Spec.performTestAndClearAccessed, Fl4Spec.NonDet.select, Set.mem_setOf_eq, + Set.mem_insert_iff, Set.mem_singleton_iff, and_true] + rcases hv with rfl | rfl + · right + rfl + · left + rfl + exact Or.inr ⟨rfl, hne, _, DecOut.of_eq (run_liftME_ok (run_liftME_ok (run_liftME_ok hD))), σ1, hst', + [v.bv], σ1, mem_liftE (ε := Unit) (x := ([v.bv], σ1)) hsel, htail σ1 hst'⟩ + · rintro ⟨r, s2⟩ h + exact (WP.spec_ok _).2 h + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.frameGetAddrAnyDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameGetAddrAnyDispX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct lbl p rt asid va len buf : U64} (hct : sched.cur_thread st s = ok ct) (hr : ZvCtRun σ ct) + (hlbl : FrameLabel lbl lbl.bv) (hg : lbl = fl4_abi.label.X86PageGetAddress) + (sz : capability.FrameSize) (dev mp ep blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev mp ep asid va) + blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer) ct s res ⦄ := by + subst lbl + have hmap : fl4_abi.label.X86PageGetAddress ≠ fl4_abi.label.X86PageMap := by + unfold fl4_abi.label.X86PageGetAddress fl4_abi.label.X86PageMap + decide + have hun : fl4_abi.label.X86PageGetAddress ≠ fl4_abi.label.X86PageUnmap := by + unfold fl4_abi.label.X86PageGetAddress fl4_abi.label.X86PageUnmap + decide + have hds : fl4_abi.label.X86PageGetAddress ≠ fl4_abi.label.X86PageDeriveSubframe := by + unfold fl4_abi.label.X86PageGetAddress fl4_abi.label.X86PageDeriveSubframe + decide + have htac : fl4_abi.label.X86PageGetAddress ≠ fl4_abi.label.X86PageTestAndClearAccessed := by + unfold fl4_abi.label.X86PageGetAddress fl4_abi.label.X86PageTestAndClearAccessed + decide + have hl := hlbl.ga rfl + have hdec : DecOut + (specDecV fp fl4_abi.label.X86PageGetAddress.bv args ci sl (.Frame p sz rt dev mp ep asid va) ec buffer) σ + (.base (.invokeArchObject (.spec (.invokePage (.pageGetAddr p.bv))))) := by + apply DecOut.of_eq + simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, if_neg (pageMapEpt_label hlbl.native), hl, + Fl4Spec.InvocationLabel.archInvocationLabel.injEq, reduceCtorEq, and_false, false_and, + ↓reduceIte, Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, + if_neg (fun h => hds (hlbl.ds h)), if_neg (fun h => htac (hlbl.tac h))] + exact run_liftME_ok (run_liftME_ok (run_liftME_ok + (decodeGetAddr_eq hl args sl dev p.bv (vmRightsOf rt) (if mp then (if ep then .vmEptMap else .vmVSpaceMap) else .vmNoMap) (frameSizeOf sz) + (if mp then some (asid.bv, va.bv) else none) ec σ))) + obtain ⟨hcur, -, -⟩ := J.1.1.curEq hct + obtain ⟨htcb, x, hk⟩ := J.1.1.xtop.curTcb hcur + have ht := J.1.1.thT ct htcb + obtain ⟨s1, σ1, hst, eσ1, hs1, Ac1, Lst1, D1, B1, F1⟩ := + J.1.1.restartRunJ hct hk (by rw [hr x hk]; rfl) + have J1 := J.ofNat (V' := Z.vs) B1 F1 + have hk1 : σ1.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + simp only [eσ1, Notif.stsAct_kheap, Notif.updK_kheap, Function.update_self] + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, vspace.decode_frame, vspace.as_frame, hlbl.native, hmap, hun, ↓reduceIte] + rw [hct, bind_ok, hs1, bind_ok] + unfold layout.paddr + simp only [lift, bind_ok] + refine WP.spec_bind (B1.reply1JFoot ht hk1 rfl call _) ?_ + rintro s2 ⟨hne, σ2, Lst2, hz, B2, F2⟩ + refine (WP.spec_ok _).2 (Or.inr ⟨rfl, hne, _, hdec, σ1, hst, [Fl4Spec.addrFromPPtr p.bv], σ1, ?_, ?_⟩) + · exact mem_liftE (ε := Unit) (x := ([Fl4Spec.addrFromPPtr p.bv], σ1)) rfl + · rw [paddr_bv] at hz + exact ⟨((), σ2), by rw [hz]; exact Set.mem_singleton _, B2.xtop.relTop, + (J1.ofNat (V' := Z.vs) B2 F2).anyX arI⟩ + +/-- `…X` sibling of `VmEptState.frameGetAddrDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.frameGetAddrDispX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct lbl p rt asid va len buf : U64} (hct : sched.cur_thread st s = ok ct) (hr : ZvCtRun σ ct) + (hlbl : FrameLabel lbl lbl.bv) (hg : lbl = fl4_abi.label.X86PageGetAddress) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) ct s res ⦄ := + J.frameGetAddrAnyDispX arI hct hr hlbl hg sz dev true true blk call don fp sl ex args ci ec buffer on rc + +end + +section + +open Notif Endp Ipc Sched + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- `…X` sibling of `noPortPreemption` (`VEAnyX`; module docstring). -/ +private theorem noPortPreemption (blk call don : Bool) (i : Fl4Spec.Fl4ArchInvocation) (σ : Fl4Spec.State Unit) + (y : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) + (hy : y ∈ ((Fl4Spec.fl4PerformInvocation blk call don (.invokeArchObject i)).run σ).1) : + y.1 ≠ .error () := by + change y ∈ ((Fl4Spec.fl4ArchPerformInvocation i).run σ).1 at hy + have liftOk : ∀ (m : Fl4Spec.SMonad Unit (List Fl4Spec.Data)), + y ∈ ((Fl4Spec.NonDetE.liftE (ε := Unit) m).run σ).1 → y.1 ≠ .error () := by + intro m h + unfold Fl4Spec.NonDetE.liftE at h + rw [ExceptT.run_lift] at h + obtain ⟨x, -, hx⟩ := h + rw [Set.mem_singleton_iff.mp hx] + intro e + cases e + cases i <;> first + | exact liftOk _ hy + | exact liftOk _ (show y ∈ ((Fl4Spec.archPerformInvocation _).run σ).1 from hy) + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.ioPortDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.ioPortDispX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hAM : ARMask arI AR) (hAT : P0.ArchTotal arI) + {lbl len buf ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (hval : PortInValAt arI a σ) (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (sl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th sl) {first last owner : U64} + (hcap : capV (L.mem s) (cslotAddr sl) = .IoPort first last owner) + (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.IoPort first last owner) blk call don fp buf ex ⦃ res => + let m := specDecV fp lbl.bv (args.map (·.bv)) ci sl (.IoPort first last owner) ec buffer + VDispPostVEX (L := L) arI AR MR on rc σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don m ct s res ⦄ := by + let m := specDec fp lbl.bv (args.map (·.bv)) ci sl (.IoPort first last 0#u64) ec buffer + have hm : specDecV fp lbl.bv (args.map (·.bv)) ci sl (.IoPort first last owner) ec buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base m := rfl + have hc : syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.IoPort first last owner) blk call don fp buf ex = + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.IoPort first last 0#u64) blk call don fp buf ex := by + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + rfl + simp only [hm, hc] + have B := J.1.1 + obtain ⟨hcur, -, -⟩ := B.curEq hct + have htcb := (B.xtop.curTcb hcur).1 + obtain ⟨⟨-, hl, hfl⟩, -⟩ := slot_cap_wf B.xtop hAs hcap + have hC : CurRun σ := fun x hx => by + rw [hcur] at hx + rw [hRun x hx] + rfl + let Q := VEAnyX (L := L) arI AR + have hX : ∀ s' a' σ', Q s' a' σ' → XTop (L := L) AR s' a' σ' := by + rintro s' a' σ' ⟨_, _, _, Z', _, _, _, _, _, _, J'⟩ + exact ⟨Z', J'.1.1.xtop⟩ + have hin : ∀ a p z, ∃ v a', arI.port_in a p z = ok (v, a') := by + intro a p z + obtain ⟨⟨v, a'⟩, h, -⟩ := P1.spec_ex_fd (hAT.port_in a p z) + exact ⟨v, a', h⟩ + have hout : ∀ a p z d, ∃ a', arI.port_out a p z d = ok a' := by + intro a p z d + obtain ⟨a', h, -⟩ := P1.spec_ex_fd (hAT.port_out a p z d) + exact ⟨a', h⟩ + have hOut : ∀ (s' : S) (a' : A) (σ1 : Fl4Spec.State Unit) (ms : Fl4Spec.MachineState) (p data : U64) + (x : Fl4Spec.Tcb), σ.kheap ct.bv = some (.tcb x) → + sched.set_thread_state st s ct sched.TS_RESTART = ok s' → + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + ms.underlyingMemory = σ1.machineState.underlyingMemory → + arI.port_out a p ⟨BitVec.ofNat 64 (portSz lbl)⟩ data = ok a' → + Q s' a' { σ1 with machineState := ms } := by + intro s' a' σ1 ms p data x hk hs hst hu hport + obtain ⟨s1, σ2, hh, -, hs1, Ac1, Lst1, D1, B1, F1⟩ := + B.restartRunJ hct hk (by rw [hRun x hk]; rfl) + have es : s1 = s' := ok_inj (hs1.symm.trans hs) + have eσ : σ2 = σ1 := (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (congrArg Prod.fst (hh.symm.trans hst)))).2 + subst s1 σ2 + have J1 := J.ofNat (V' := Z.vs) B1 F1 + have hp : ArchIrqStep arI a a' := + Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨_, _, _, hport⟩))))) + obtain ⟨Lst2, B2, F2⟩ := B1.msArJ ms a' hu (fun h => hAM _ _ _ h hp) + exact (J1.ofNat (V' := Z.vs) B2 F2).anyX arI + have hD := decode_invocation_ioportXQ arI Z.th B.xtop.rel.1 s a σ lbl lbl.bv (port_label lbl) + len buf ct hct args hlen hargs hcur htcb B.kern.act first last hfl (by simpa using hl) call hout rfl B.xtop + (show Q s a σ from J.anyX arI) hOut hX (cslotAddr sl) blk don fp ex ci sl ec buffer (fun hI => by + refine Or.inr (decode_invocation_ioport_inQ arI s a σ B.xtop (J.anyX arI) lbl hI lbl.bv + ((port_label lbl).1 hI) len buf ct hct args hlen hargs hcur first last hfl (by simpa using hl) + call (cslotAddr sl) blk don fp ex ci sl ec buffer ?_) + intro p + refine WP.spec_mono (B.portInTailJFoot arI hAM hC hct p ⟨BitVec.ofNat 64 (portSz lbl)⟩ call hin + (fun _ => hval)) ?_ + rintro res ⟨hr, hn, hh⟩ + refine ⟨hr, hn, fun σ1 hs => ?_⟩ + obtain ⟨rr, σy, hY, w, hw, hR, _, _, _, B', F'⟩ := hh σ1 hs + exact ⟨rr, σy, hY, w, hw, hR, (J.ofNat (V' := Z.vs) B' F').anyX arI⟩) + have archOut {inv : Fl4Spec.Fl4Invocation} (h : DecOut m σ inv) : + ∃ i, inv = .invokeArchObject i := DecOut.liftME_inv h + -- the I/O-port decoder yields a `spec` arch invocation, which the VM perform does not substitute + have hbase {i : Fl4Spec.Fl4ArchInvocation} (h : DecOut m σ (.invokeArchObject i)) : + Fl4Spec.fl4PerformInvocationVF (Z := Unit) on rc blk call don (.base (.invokeArchObject i)) = + Fl4Spec.fl4PerformInvocation blk call don (.invokeArchObject i) := by + refine VirtF13.fl4PerformInvocationVF_base on rc blk call don _ ?_ + obtain ⟨j, hj⟩ := DecOut.liftME2_inv (f := Fl4Spec.Fl4Invocation.invokeArchObject) + (g := Fl4Spec.Fl4ArchInvocation.spec) h + cases hj + exact id + rcases hD with hD | hD + · refine WP.spec_mono hD ?_ + rintro res (⟨e, hr, hs, ⟨e', hde, he⟩, hQ⟩ | ⟨inv, hde, σ1, hs, y, hy, ⟨_, _, hret, -⟩, hQ⟩) + · exact Or.inl (Or.inl ⟨e, hr, hs, ⟨e', run_liftME_err hde, he⟩, hQ⟩) + · obtain ⟨i, rfl⟩ := archOut hde + rcases hret with ⟨hr, hy1⟩ | ⟨_, hy1⟩ + · exact Or.inl (Or.inr ⟨_, hde.liftME, σ1, hs, y, by rw [hbase hde]; exact hy, Or.inl ⟨hr, hy1, hQ⟩⟩) + · exact (noPortPreemption blk call don i σ1 y hy hy1).elim + · refine WP.spec_mono hD ?_ + rintro res (⟨e, hr, hs, ⟨e', hde, he⟩, hQ⟩ | ⟨hr, hn, inv, hde, σ1, hs, rr, σy, hy, z, hz, ht, hQ⟩) + · exact Or.inr (Or.inl ⟨e, hr, hs, ⟨e', run_liftME_err hde, he⟩, hQ⟩) + · obtain ⟨i, rfl⟩ := archOut hde + exact Or.inr (Or.inr ⟨hr, hn, _, hde.liftME, σ1, hs, rr, σy, by rw [hbase hde]; exact hy, z, hz, ht, hQ⟩) + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.decodeUtDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.decodeUtDispX [Fl4Proofs.P1.PlainStore st] {s : S} {ar : A} {σ : Fl4Spec.State Unit} + {MR : A → Fl4Spec.MachineState → Prop} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) + (hA : P0.ArchTotal arI) (hag : ∀ r, arI.virtualization ar = ok r → r.val = Fl4Spec.virtKind.code) + (hVA : ZvVirtAgree arI) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (lbl : U64) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (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) + {pp : U64} {rs : Nat} {idx : List Bool} (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) + {p sb fi : U64} {dev : Bool} (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev)) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraRel Z.th (L.mem s) x0 (excaps.map Prod.fst)) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hc0 : Z.c = 0#u64) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) (hRU : RegionUnrefAt (L := L) H Cs D s) + (hUZ : dev = false → UtZero (L.mem s) Z.G σ) (hUN : dev = false → UtNodesJ (L.mem s) Z.G (L.ksb s)) + (hDA : dev = true → DevApart (L := L) Z.th H Z.G Cs s) (hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (hGP : dev = true → DevGpm (L.mem s) Z.G σ) + (hAK : ARKeep arI AR) (hnd : Fl4Spec.numDomains = 1) (hMP : MachPlat arI MR) (hPA : PPPlatA arI AR) + (hrcb : Fl4Spec.resetChunkBits = 16) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → Fl4Spec.scActive y = true → + Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hVM : (∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx))) → ∀ s1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1)) + (hMR : MR ar σ.machineState) (hVT : ValidMachineTime σ) (hVIM : ValidIrqMasks σ) + (onCore remoteCur : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) (ci : Fl4Spec.CapRef) + (buffer : Option Fl4Spec.ObjRef) : + syscall.decode_invocation st arI s ar lbl length (cslotAddr (pp.bv, idx)) (.Untyped p sb fi dev) blk call don fp + buf extra ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don + (specDecV fp slbl (args.map UScalar.bv) ci (pp.bv, idx) (.Untyped p sb fi dev) excaps buffer) s res ⦄ := by + rw [decode_invocation_untyped_eq arI s ar lbl length _ p sb fi dev blk call don fp buf extra ct hct] + refine WP.spec_mono (J.decodeUtX hni hA hag hVA hCap lbl slbl hlbl length buf ct hct args hlen hargs hpp hidx hcap + extra x0 hx0 (excaps.map Prod.fst) hex hfan hc0 hBO hAF hRU hUZ hUN hDA hDF hGP hAK hnd hMP hPA hrcb hRd0 hnq hVM + hMR hVT hVIM) ?_ + rintro res (⟨e, e', rfl, hm, he⟩ | ⟨inv, hm, σ1, hst, hr⟩) + · exact Or.inl ⟨e, rfl, rfl, ⟨e', decV_untyped_run hm, Or.inl he⟩, J.anyX arI⟩ + · refine Or.inr ⟨_, DecOut.of_eq (decV_untyped_run hm), σ1, hst, ?_⟩ + rcases hr with ⟨h1, σ', hy, V', X', XL', Z', H', Cs', Ac', Lst', D', hv, hJ⟩ | ⟨h1, σ', hy, V', X', XL', hP⟩ + · exact ⟨_, perfNil_ok hy, Or.inl ⟨h1, rfl, ⟨_, _, _, _, _, _, _, _, _, hv, hJ⟩⟩⟩ + · exact ⟨_, perfNil_err hy, Or.inr ⟨h1, rfl, V', X', XL', hP⟩⟩ + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P3.Ipc Fl4Proofs.P1 + +/-- `…X` sibling of `VDispAll` (`VEAnyX`; module docstring). -/ +structure VDispAllX (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) + (onCore remoteCur : Fl4Spec.ObjRef → Bool) (V X : Set U64) (XL : U64 → Nat) (Z : XSets) (H : P1.Heap) + (Cs Ac : Set U64) (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop where + /-- CNode labels 25–32 (`VmEptState.cnodeDecodeAll`). -/ + cnode : ∀ (_hD : P0.StoreTotal st ∧ P0.ArchTotal arI ∧ MRKeepD arI AR) (_hK : ARKeep arI AR) + (_hticks : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ (a : A) (v : U64), arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (_hnd : Fl4Spec.numDomains = 1) (_hM : MachPlatC arI MR) (_hKD : MRKeepD arI MR) (_hPA : PPPlatA arI AR) + (_hAM : ARMask arI AR) [VxE Z] (_hsg : SpecGlob σ) (_hp0 : PPR0 σ) (_hmv : MRV MR a σ) + (_hQ : ∀ e, EpQPre σ e) (lbl : U64) (_hrd : 25 ≤ lbl.val ∧ lbl.val ≤ 32) (slbl : Fl4Spec.Data) + (_hlbl : Fl4Spec.genInvocationType slbl = cnodeLabelOf lbl) (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) + (sl : Fl4Spec.CslotPtr) (_hAs : AbsSlot Z.th sl) (p r gs g : U64) + (_hsc : capV (L.mem s) (cslotAddr sl) = .CNode p r gs g) (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) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (_hex : ExtraRel2 Z.th (L.mem s) x0 x1 (excaps.map Prod.fst)) (blk call don fp : Bool) (ci : Fl4Spec.CapRef) + (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl length (cslotAddr sl) (.CNode p r gs g) blk call don fp buf extra + ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don + (specDecV fp slbl (args.map UScalar.bv) ci sl (.CNode p r gs g) excaps buffer) s res ⦄ + /-- A VCPU cap, including replies written by the decoder. -/ + vcpu : ∀ (hK : ARKeep arI AR) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hAT : P0.ArchTotal arI) {p lbl len buf ct : U64} (hp : p ∈ V) (hcan : Canonical p) + (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (blk call don fp : Bool) (hbuf : BufOut V buf) (ex : Array obj.Slot 3#usize) + (args : List Fl4Spec.Data) (hargs : VArgs st s ct buf len args) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (he : ExtraOK Z.th (L.mem s) ex ec) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Vcpu p) blk call don fp buf ex ⦃ res => + let m := specDecV fp lbl.bv args ci sl (.Vcpu p) ec buffer + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) onCore remoteCur σ blk call don m ct s res ⦄ + /-- Direct invocation of an I/O port, including ports owned by a VCPU. -/ + ioPort : ∀ (hAM : ARMask arI AR) (hAT : P0.ArchTotal arI) {lbl len buf ct : U64} + (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) (hval : PortInValAt arI a σ) + (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (sl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th sl) {first last owner : U64} + (hcap : capV (L.mem s) (cslotAddr sl) = .IoPort first last owner) + (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.IoPort first last owner) blk call don fp buf ex ⦃ res => + let m := specDecV fp lbl.bv (args.map (·.bv)) ci sl (.IoPort first last owner) ec buffer + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) onCore remoteCur σ blk call don m ct s res ⦄ + /-- A native PageMap on an EPT-mapped frame is rejected. -/ + frameMapReject : ∀ {ct : U64} (hct : sched.cur_thread st s = ok ct) (p rt asid va len buf : U64) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a fl4_abi.label.X86PageMap len (cslotAddr sl) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don (specDecV fp fl4_abi.label.X86PageMap.bv + args ci sl (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ + /-- An EPT-mapped frame at GetAddress, which replies in the decoder. -/ + frameGetAddr : ∀ {ct lbl p rt asid va len buf : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (hlbl : FrameLabel lbl lbl.bv) (hg : lbl = fl4_abi.label.X86PageGetAddress) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) onCore remoteCur σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) ct s res ⦄ + /-- Labels outside the frame decoder on an EPT-mapped frame. -/ + frameOther : ∀ {ct lbl : U64} (hct : sched.cur_thread st s = ok ct) (hlbl : FrameLabel lbl lbl.bv) + (hm : lbl ≠ fl4_abi.label.X86PageMap) (hu : lbl ≠ fl4_abi.label.X86PageUnmap) + (hg : lbl ≠ fl4_abi.label.X86PageGetAddress) (ht : lbl ≠ fl4_abi.label.X86PageTestAndClearAccessed) + (hd : lbl ≠ fl4_abi.label.X86PageDeriveSubframe) (p rt asid va len buf : U64) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt dev true true asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don + (specDecV fp lbl.bv args ci sl (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ + /-- A large EPT-mapped frame can derive ordinary unmapped subframe caps. -/ + frameDerive : ∀ {ct len buf pc p rt asid va : U64} {idx : List Bool} (hpc : Z.th pc = some (.cnode idx.length)) + (hct : sched.cur_thread st s = ok ct) + {sz : capability.FrameSize} {dev : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt dev true true asid va)) + (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ i : U64, ∀ v, args[i.val]? = some v → msg.syscall_arg st s ct buf i = ok v) + (blk call don fp : Bool) (ex : Array obj.Slot 3#usize) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : JVsExtra (L := L) Z s ex ec) + (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a fl4_abi.label.X86PageDeriveSubframe len (cslotAddr (pc.bv, idx)) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don + (specDecV fp fl4_abi.label.X86PageDeriveSubframe.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt dev true true asid va) ec buffer) s res ⦄ + /-- TestAndClearAccessed on an EPT-mapped cap still walks the native ASID table. -/ + frameTac : ∀ {ct len buf p rt asid va : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (sz : capability.FrameSize) (dev blk call don fp : Bool) (sl : Fl4Spec.CslotPtr) + (hsl : AbsSlot Z.th sl) (hc : capV (L.mem s) (cslotAddr sl) = .Frame p sz rt dev true true asid va) + (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a fl4_abi.label.X86PageTestAndClearAccessed len (cslotAddr sl) + (.Frame p sz rt dev true true asid va) blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) onCore remoteCur σ blk call don + (specDecV fp fl4_abi.label.X86PageTestAndClearAccessed.bv args ci sl + (.Frame p sz rt dev true true asid va) ec buffer) ct s res ⦄ + /-- An invoked `Ept` cap, every label (`VmEptState.eptDecode`). -/ + ept : ∀ {lbl len ptr level asid va : U64} {m : Bool} (block call cd fp : Bool) (buf : U64) + (ex : Array obj.Slot 3#usize) {pc : U64} {r : Nat} (_hpc : Z.th pc = some (.cnode r)) {idx : List Bool} + (_hidx : idx.length = r) (_hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Ept ptr level m asid va)) + (args : List Fl4Spec.Data) (_hV : VArgs st s (L.curW s) buf len args) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (_hex : ExtraSRel Z.th (L.mem s) (exAt ex 0) ec) + (fp' : Bool) (cref : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Ept ptr level m asid va) block call cd fp + buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ block call cd + (specDecV fp' lbl.bv args cref (pc.bv, idx) (.Ept ptr level m asid va) ec buffer) s res ⦄ + /-- A `Frame` cap at `X86PageMapEPT` (`VmEptState.frameEptDecode`). -/ + frameEpt : ∀ {lbl len ptr rights asid va : U64} {size : capability.FrameSize} {dev m ep : Bool} + (_hlb : lbl.val = Fl4Spec.fl4PageMapEptLabel) (block call cd fp : Bool) (buf : U64) (ex : Array obj.Slot 3#usize) + {pc : U64} {r : Nat} (_hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (_hidx : idx.length = r) + (_hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame ptr size rights dev m ep asid va)) + (args : List Fl4Spec.Data) (_hV : VArgs st s (L.curW s) buf len args) + (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (_hex : ExtraSRel Z.th (L.mem s) (exAt ex 0) ec) + (fp' : Bool) (cref : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Frame ptr size rights dev m ep asid va) + block call cd fp buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ block call cd + (specDecV fp' lbl.bv args cref (pc.bv, idx) (.Frame ptr size rights dev m ep asid va) ec buffer) s res ⦄ + /-- An EPT-mapped `Frame` cap at `X86PageUnmap` (`VmEptState.frameUnmapDecode`). -/ + frameUnmap : ∀ {lbl len ptr rights asid va : U64} {size : capability.FrameSize} {dev : Bool} + (_hl : lbl = fl4_abi.label.X86PageUnmap) (block call cd fp : Bool) (buf : U64) (ex : Array obj.Slot 3#usize) + {pc : U64} {r : Nat} (_hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (_hidx : idx.length = r) + (_hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame ptr size rights dev true true asid va)) + (args : List Fl4Spec.Data) (ec : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (fp' : Bool) (cref : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Frame ptr size rights dev true true asid va) + block call cd fp buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ block call cd + (specDecV fp' lbl.bv args cref (pc.bv, idx) (.Frame ptr size rights dev true true asid va) ec buffer) s res ⦄ + /-- An `AsidPool` cap at `X86ASIDPoolAssign` with an unmapped level-0 `Ept` first extra cap + (`VmEptState.poolAssignDecode`). -/ + pool : ∀ (ptr base : U64) {slot : U64} (_hsl : IsSlot Z.th slot) + (_hcap : capAt (L.mem s) slot = ok (.AsidPool ptr base)) {lbl : U64} (_hl : lbl = fl4_abi.label.X86ASIDPoolAssign) + (len : U64) (block call cd fp : Bool) (buf : U64) (ex : Array obj.Slot 3#usize) {pc : U64} {r : Nat} + (_hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (_hidx : idx.length = r) + (_hex : vspace.extra ex 0#usize = ok (some (cslotAddr (pc.bv, idx)))) + {ptr1 asid0 va0 : U64} (_hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Ept ptr1 0#u64 false asid0 va0)) + (args : List Fl4Spec.Data) (fp' : Bool) (cref : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (rest : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl len slot (.AsidPool ptr base) block call cd fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ block call cd + (specDecV fp' lbl.bv args cref sl (.AsidPool ptr base) + ((absCap (.Ept ptr1 0#u64 false asid0 va0), (pc.bv, idx)) :: rest) buffer) s res ⦄ + /-- `TCBSetEPTRoot` on an invoked `Thread` cap (`VmEptState.decodeSetEpt`). -/ + setEpt : ∀ (length slot : U64) (p : U64) (blk call don : Bool) (buf : U64) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (_hstall : smp.remote_tcb_stall st arI s a p = ok (s, a)) (x0 : U64) + (_hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (cps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (_hex : ExtraSRel Z.th (L.mem s) x0 cps) + (slbl : Fl4Spec.Data) (_hl : Fl4Spec.dataToNat slbl = Fl4Spec.fl4SetEptRootLabel) (args : List Fl4Spec.Data) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (buffer : Option Fl4Spec.ObjRef) + (_ht : Z.th p = some .tcb) (_hsl : IsSlot Z.th slot) (_hcs : capV (L.mem s) slot = .Thread p), + syscall.decode_invocation st arI s a fl4_abi.label.TCBSetEPTRoot length slot (.Thread p) blk call don false buf + extra ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don + (specDecV false slbl args ci sl (.Thread p) cps buffer) s res ⦄ + /-- An invoked `Untyped` cap, every label (`VmEptState.decodeUtDispX`). -/ + ut : ∀ [VxE Z] (_hAT : P0.ArchTotal arI) (_hag : ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (_hCap : ∀ a', VirtSanEq.CapAgree arI a') (lbl : U64) (slbl : Fl4Spec.Data) + (_hlbl : Fl4Spec.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (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) + {pp : U64} {rs : Nat} {idx : List Bool} (_hpp : Z.th pp = some (.cnode rs)) (_hidx : idx.length = rs) + {p sb fi : U64} {dev : Bool} (_hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev)) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) + (_hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (_hex : ExtraRel Z.th (L.mem s) x0 (excaps.map Prod.fst)) + (_hfan : Fl4Spec.retypeFanOutLimit = 256#64) (_hc0 : Z.c = 0#u64) + (_hBO : BufObjHyp Z.B σ) (_hAF : ArchFitHyp H σ) (_hRU : RegionUnrefAt (L := L) H Cs D s) + (_hUZ : dev = false → UtZero (L.mem s) Z.G σ) (_hUN : dev = false → UtNodesJ (L.mem s) Z.G (L.ksb s)) + (_hDA : dev = true → DevApart (L := L) Z.th H Z.G Cs s) (_hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (_hGP : dev = true → DevGpm (L.mem s) Z.G σ) + (_hAK : ARKeep arI AR) (_hnd : Fl4Spec.numDomains = 1) (_hMP : MachPlat arI MR) (_hPA : PPPlatA arI AR) + (_hrcb : Fl4Spec.resetChunkBits = 16) + (_hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → Fl4Spec.scActive y = true → + Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (_hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (_hVM : (∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx))) → ∀ s1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1)) + (_hMR : MR a σ.machineState) (_hVT : ValidMachineTime σ) (_hVIM : ValidIrqMasks σ) + (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef), + syscall.decode_invocation st arI s a lbl length (cslotAddr (pp.bv, idx)) (.Untyped p sb fi dev) blk call don fp + buf extra ⦃ res => VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don + (specDecV fp slbl (args.map UScalar.bv) ci (pp.bv, idx) (.Untyped p sb fi dev) excaps buffer) s res ⦄ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.decodeDisp` (`VEAnyX`; module docstring). -/ +theorem VmEptState.decodeDispX [Fl4Proofs.P1.PlainStore st] {AR : A → Fl4Spec.Cr3 → Prop} + {MR : A → Fl4Spec.MachineState → Prop} (hA : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (onCore remoteCur : Fl4Spec.ObjRef → Bool) : + VDispAllX (L := L) arI AR MR onCore remoteCur V X XL Z H Cs Ac Lst D s a σ := by + constructor + · intro hD hK hticks hnd hM hKD hPA hAM _ hsg hp0 hmv hQ lbl hrd slbl hlbl length buf ct hct args hlen hargs sl + hAs p r gs g hsc extra x0 x1 hx0 hx1 excaps hex blk call don fp ci buffer + exact WP.spec_mono (VmEptState.cnodeDecodeAll arI hA hD hK hVA hticks hnd hM hKD hPA hAM J hni hsg hp0 hmv hQ + lbl hrd slbl hlbl length buf ct hct args hlen hargs sl hAs p r gs g hsc extra x0 x1 hx0 hx1 excaps hex + blk call don fp ci buffer) fun _ h => h.toDispX arI J onCore remoteCur blk call don + · intro hK hSy hCap hAT p lbl len buf ct hp hcan hct hRun blk call don fp hbuf ex args hargs ec he ci sl buffer + exact J.vcpuDispX hK hSy hVA hCap hAT hp hcan hct hRun blk call don fp hbuf ex args hargs ec he + onCore remoteCur ci sl buffer + · intro hAM hAT lbl len buf ct hct hRun hval args hlen hargs sl hAs first last owner hcap blk call don fp ex ci ec buffer + exact J.ioPortDispX arI hAM hAT hct hRun hval args hlen hargs sl hAs hcap blk call don fp ex ci ec buffer onCore remoteCur + · intro ct hct p rt asid va len buf sz dev blk call don fp sl ex args ci ec buffer + exact J.frameMapEptRejectX arI hct p rt asid va len buf sz dev blk call don fp sl ex args ci ec buffer onCore remoteCur + · intro ct lbl p rt asid va len buf hct hRun hlbl hg sz dev blk call don fp sl ex args ci ec buffer + exact J.frameGetAddrDispX arI hct hRun hlbl hg sz dev blk call don fp sl ex args ci ec buffer onCore remoteCur + · intro ct lbl hct hlbl hm hu hg ht hd p rt asid va len buf sz dev blk call don fp sl ex args ci ec buffer + exact J.frameOtherDispX arI hct hlbl hm hu hg ht hd p rt asid va len buf sz dev blk call don fp sl ex args ci ec buffer + onCore remoteCur + · intro ct len buf pc p rt asid va idx hpc hct sz dev hc args hlen hargs blk call don fp ex ci ec hex buffer + exact J.frameDeriveDispX arI hni hpc hct hc args hlen hargs blk call don fp ex ci ec hex buffer onCore remoteCur + · intro ct len buf p rt asid va hct hRun sz dev blk call don fp sl hsl hc ex args ci ec buffer + exact J.frameTacDispX arI hct hRun sz dev blk call don fp sl hsl hc ex args ci ec buffer onCore remoteCur + · intro lbl len ptr level asid va m block call cd fp buf ex pc r hpc idx hidx hcx args hV ec hex fp' cref buffer + exact WP.spec_mono (J.eptDecode hni hA block call cd fp buf ex hpc hidx hcx args hV ec hex fp' cref buffer) + fun _ h => h.toDispX arI J hni onCore remoteCur block call cd + · intro lbl len ptr rights asid va size dev m ep hlb block call cd fp buf ex pc r hpc idx hidx hcx args hV ec hex + fp' cref buffer + exact WP.spec_mono (J.frameEptDecode hni hVA hlb block call cd fp buf ex hpc hidx hcx args hV ec hex fp' cref + buffer) fun _ h => h.toDispX arI J hni onCore remoteCur block call cd + · intro lbl len ptr rights asid va size dev hl block call cd fp buf ex pc r hpc idx hidx hcx args ec fp' cref buffer + exact WP.spec_mono (J.frameUnmapDecode hni hA hl block call cd fp buf ex hpc hidx hcx args ec fp' cref buffer) + fun _ h => h.toDispX arI J hni onCore remoteCur block call cd + · intro ptr base slot hsl hcap lbl hl len block call cd fp buf ex pc r hpc idx hidx hex ptr1 asid0 va0 hcx args fp' + cref sl rest buffer + exact WP.spec_mono (J.poolAssignDecode hni ptr base hsl hcap hl len block call cd fp buf ex hpc hidx hex hcx args + fp' cref sl rest buffer) fun _ h => h.toDispX arI J hni onCore remoteCur block call cd + · intro length slot p blk call don buf extra hstall x0 hx0 cps hex slbl hl args ci sl buffer ht hsl hcs + exact WP.spec_mono (VmEptState.decodeSetEpt arI hA J hni length slot p blk call don buf extra hstall x0 hx0 cps + hex slbl hl args ci sl buffer ht hsl hcs) fun _ h => h.toDispX arI J onCore remoteCur blk call don + · intro _ hAT hag hCap lbl slbl hlbl length buf ct hct args hlen hargs pp rs idx hpp hidx p sb fi dev hcap extra x0 + hx0 excaps hex hfan hc0 hBO hAF hRU hUZ hUN hDA hDF hGP hAK hnd hMP hPA hrcb hRd0 hnq hVM hMR hVT hVIM blk call + don fp ci buffer + exact J.decodeUtDispX hni hAT hag hVA hCap lbl slbl hlbl length buf ct hct args hlen hargs hpp hidx hcap extra x0 + hx0 excaps hex hfan hc0 hBO hAF hRU hUZ hUN hDA hDF hGP hAK hnd hMP hPA hrcb hRd0 hnq hVM hMR hVT hVIM onCore + remoteCur blk call don fp ci buffer + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3AsidControl.lean b/proofs/Fl4Proofs/P3/VmEntryS3AsidControl.lean new file mode 100644 index 000000000..649aa1795 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3AsidControl.lean @@ -0,0 +1,360 @@ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmEptMakePool +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmEntryS3Untyped +import Fl4Proofs.P3.VmEntryS3Decode + +/-! +Selected native ASIDControl at the reached source, destination and ASID +high slot, every label: MakePool from the restart's state detyped at the +pool's frame (clean or dirty region alike, P3/MpDirtyJ's `perform_mp_detype`), +then the pool's insertion (`JAtZB.mpStepJ`, `VmEptState.frameMakePool`). +The region's kept references from `JAtZB.regionUnrefAt` (as the Untyped +reset); no new premise. `selectedAsidControlS3`: at the selected decode's reads. +-/ +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} +variable {Lst : U64 → List U64} {XL : U64 → Nat} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + +/-- The outcome of two nested `liftME`s is one of the inner decode's. -/ +theorem decOut_liftME2_inv {α β γ : Type} {f : β → γ} {g : α → β} {m : Fl4Spec.SEMonad Unit α} + {σ : Fl4Spec.State Unit} {inv : γ} + (h : DecOut (Fl4Spec.NonDetE.liftME f (Fl4Spec.NonDetE.liftME g m)) σ inv) : ∃ i, inv = f (g i) := by + have h1 := h.1 + unfold Fl4Spec.NonDetE.liftME at h1 + rw [ExceptT.run_bind] at h1 + obtain ⟨⟨r, σ'⟩, hr, hm⟩ := h1 + cases r with + | error e => exact absurd (Set.mem_singleton_iff.1 hm) (by simp) + | ok j => + have e1 := Set.mem_singleton_iff.1 hm + simp only [Prod.mk.injEq, Except.ok.injEq] at e1 + rw [ExceptT.run_bind] at hr + obtain ⟨⟨r2, σ2⟩, -, hm2⟩ := hr + cases r2 with + | error e => exact absurd (Set.mem_singleton_iff.1 hm2) (by simp) + | ok i => + have e2 := Set.mem_singleton_iff.1 hm2 + simp only [Prod.mk.injEq, Except.ok.injEq] at e2 + exact ⟨i, by rw [e1.1, e2.1]⟩ + +set_option maxHeartbeats 16000000 in +/-- Match all selected ASIDControl decoder outcomes, including the restart, +childless Untyped drop and insertion on successful MakePool. +[J] Matched syscall arguments and ExtraOK on the reached invocation. +[S] `VxE` supplies the carried native VSpace naming facts. -/ +theorem VmEptState.selectedAsidControl + (Jv : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) + [VxE Z] (hni : Entered σ) {lbl 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) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hE : ExtraOK Z.th (L.mem s) extra excaps) + (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (buffer : Option Fl4Spec.ObjRef) {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s ar lbl length (cslotAddr sl) .AsidControl blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci sl .AsidControl excaps buffer) s res ⦄ := by + let slbl := lbl.bv + let slot := cslotAddr sl + have hlM : lbl = fl4_abi.label.X86ASIDControlMakePool ↔ + Fl4Spec.invocationType slbl = .archInvocationLabel .x64ASIDControlMakePool := makePool_label lbl + have J := Jv.1.1 + have hX := J.xtop + have hext : JMpExtra (L := L) Z s extra excaps := ExtraOK.mpExtra hX.rel.1 hE + obtain ⟨hcur, xct, hkt0, hrun⟩ := J.curRunnable hni hct + have htcb := (hX.curTcb hcur).1 + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := hX.rel + have hRU := J.regionUnrefAt hni + have hVN := J.carry.vsNamed (VxE.eq (Z := Z)) + have hkt : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt0 + have hc2 : sched.cur_thread st s = ok (L.curW s) := cur_thread_eq s + have hctE : ct = L.curW s := by rw [hct] at hc2; exact eq_of_ok_eq hc2 + -- the footprint (VsThX's `domFrame_asid_control`) + let W : U64 → Prop := fun b => VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ + ¬ ((L.ksb s).val ≤ b.val ∧ b.val < (L.ksb s).val + 13568) + have hext' : (∃ o0 o1, vspace.extra extra 0#usize = ok o0 ∧ vspace.extra extra 1#usize = ok o1 ∧ + (o0 = none ∨ o1 = none) ∧ excaps.length ≤ 1) ∨ + ∃ (pp : U64) (rs : Nat) (idx : List Bool) (y : obj.Slot) (uc : capability.Cap) (uc' : Fl4Spec.Cap) + (rc : capability.Cap) (rc' : Fl4Spec.Cap) (ysl : Fl4Spec.CslotPtr) + (rest : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), + Z.th pp = some (.cnode rs) ∧ idx.length = rs ∧ + vspace.extra extra 0#usize = ok (some (cslotAddr (pp.bv, idx))) ∧ vspace.extra extra 1#usize = ok (some y) ∧ + capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok uc ∧ CapRel uc uc' ∧ + capAt (L.mem s) y = ok rc ∧ CapRel rc rc' ∧ CapValid Z.th rc ∧ + excaps = (uc', (pp.bv, idx)) :: (rc', ysl) :: rest ∧ + ∀ (p sb fi : U64), sb = fl4_abi.consts.bits.ASID_POOL → + capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi false) → + (¬ ∃ sl, σ.cdt sl = some (pp.bv, idx)) → + ∀ (pd : U64) (rd : Nat) (idxd : List Bool), Z.th pd = some (.cnode rd) → idxd.length = rd → + capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null → CnRef Z.th (L.mem s) rc pd → + ∀ (i base : U64), i.val < 2048 → base.val = i.val * 512 → + σ.archState.x64AsidTable (Fl4Spec.ucast 11 i.bv) = none → + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let i1 ← lift (sb &&& 63#u64) + let i2 ← 1#u64 <<< i1 + let s2 ← obj.Slot.set_cap st (cslotAddr (pp.bv, idx)) s1 (capability.Cap.Untyped p sb i2 false) + let s3 ← st.clear_free s2 p 4096#u64 + let s4 ← cdt.insert st s3 (capability.Cap.AsidPool p base) (cslotAddr (pp.bv, idx)) (cslotAddr (pd.bv, idxd)) + let s5 ← vspace.set_asid_table st s4 i p + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, ar)) ⦃ res => + res.1 = .Ok () ∧ res.2.2 = ar ∧ ∃ σ' th' G', + ((), σ') ∈ ((Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart >>= fun _ => + Fl4Spec.performAsidControlInvocation (.makePool p.bv (pd.bv, idxd) (pp.bv, idx) base.bv)) σ).1 ∧ + ¬ ((Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart >>= fun _ => + Fl4Spec.performAsidControlInvocation (.makePool p.bv (pd.bv, idxd) (pp.bv, idx) base.bv)) σ).2 ∧ + Placed th' ∧ CRel th' (L.mem res.2.1) σ' G' ∧ FinInv th' (L.mem res.2.1) σ' G' ∧ + MdbOKV (L.ksb s) (L.mem res.2.1) G' ∧ P1.WritesIn st s res.2.1 W ∧ + (DomRel (L.mem res.2.1) (L.ksb res.2.1) σ' → VEAnyX (L := L) arI AR res.2.1 res.2.2 σ') ⦄ := by + rcases hext.2 with h | ⟨pp, rs, idx, y, uc, uc', rc, rc', ysl, rest, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10⟩ + · exact Or.inl h + refine Or.inr ⟨pp, rs, idx, y, uc, uc', rc, rc', ysl, rest, h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, ?_⟩ + intro p sb fi hsb hcap hnch pd rd idxd hpd hld hnull hCn i base hi hbase hfree + subst hsb + have hpp := h1 + have hidx := h2 + have hsbv : (fl4_abi.consts.bits.ASID_POOL).val = 12 := by unfold fl4_abi.consts.bits.ASID_POOL; rfl + have h2sb : 2 ^ (fl4_abi.consts.bits.ASID_POOL).val = 4096 := by rw [hsbv]; norm_num + have hRg12 : ∀ o, RgOf p fl4_abi.consts.bits.ASID_POOL o ↔ o ∈ regionSet p.bv 12 := fun o => by + show o ∈ regionSet p.bv (fl4_abi.consts.bits.ASID_POOL).val ↔ _; rw [hsbv] + -- the source cap: in the MDB, childless, its region's facts + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hxG : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := + (hCR.2.2.1.members _).2 ⟨hAs.isSlot, _, hcap, fun h => by cases h⟩ + have hnd := nodesc_of_cdt hPl hCR.2.2.2.1 hAs hnch + have hcv := capV_of_capAt hcap + have U := J.utc _ p _ fi false hAs hcap rfl + have hfit := U.pfit + rw [h2sb] at hfit + have hns := U.ns hnch + rw [h2sb] at hns + have hN := U.nostr + have hppOff : ¬ (p.val ≤ pp.val ∧ pp.val < p.val + 2 ^ (fl4_abi.consts.bits.ASID_POOL).val) := + cnode_off_of_slot hPl hN hpp hidx (by rw [h2sb]; exact hns _ hxG) + have hDL0 : DestLive Z.th (L.mem s) (L.ksb s) pd := + DestLive.of_cnRef (kb := L.ksb s) hCn ⟨y, hext.1 y h4, capV_of_capAt h7⟩ + have hpdOff := J.destOff hpd hDL0 hxG hcv hnd + -- the restart + obtain ⟨s1, σ1, Lst1, hs1c, hst1, J1v⟩ := Jv.restart hni + have J1 := J1v.1.1 + have hs1 : sched.set_thread_state st s ct sched.TS_RESTART = ok s1 := hctE ▸ hs1c + have he1 : σ1 = stsAct (updK σ ct.bv (.tcb { xct with tcbState := .restart })) ct.bv := by + have e := hst1 + rw [hcur, Notif.setThreadState_eq .restart hkt0] at e + have hm := congrArg (fun q => ((), σ1) ∈ q.1) e + simp only [Set.mem_setOf_eq, Set.mem_singleton_iff, Prod.mk.injEq, true_and] at hm + exact of_eq_true hm + obtain ⟨s1', σ1', -, hs1', -, hSS1, -, -, hk1, hcpu1⟩ := + restart_dstate Z.th hPl s σ Z.G ⟨hCR, J.del, hFI⟩ ct hct hcur htcb J.kern.act + rw [hs1] at hs1'; cases eq_of_ok_eq hs1' + have hσ1cdt : σ1.cdt = σ.cdt := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hres1 : σ1.schedulerAction ≠ .chooseNewThread := by + rw [he1, J.restart_keeps_resume hni hkt0 hcur] + simp + have hcap1 : capAt (L.mem s1) (cslotAddr (pp.bv, idx)) = + ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi false) := (hSS1.capAt hAs.isSlot).trans hcap + have hnch1 : ¬ ∃ y, σ1.cdt y = some (pp.bv, idx) := by rw [hσ1cdt]; exact hnch + -- the kept references at the restart's state (`RegionUnrefJ` [J]) + obtain ⟨hKR, hDT⟩ := hRU s1 hs1c p.val (p.val + 2 ^ (fl4_abi.consts.bits.ASID_POOL).val) + (fun q k hq hr => (region_deadP J.jcov J.ut hCR.2.2.2.2 hxG hcv (Or.inr ⟨hnd, le_rfl⟩) le_rfl q k hq hr).1) + -- the drop at the restart's state + set σd : Fl4Spec.State Unit := Fl4Spec.detype (regionSet p.bv 12) σ1 with hσd + have hSD : SDrop (RgOf p fl4_abi.consts.bits.ASID_POOL) σ1 σd := + ⟨fun o ho => by have h := (hRg12 o).1 ho; rw [hσd]; simp only [Fl4Spec.detype]; exact if_pos h, + fun o ho => by + have h : o ∉ regionSet p.bv 12 := fun h => ho ((hRg12 o).2 h) + rw [hσd]; simp only [Fl4Spec.detype]; exact if_neg h, + rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have Jdv := J1v.drop hres1 hAs hcap1 hnch1 hKR hDT J1.carry.archFit hSD rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl + (fun _ _ => rfl) J1.vsc.needR.1 J1.vsc.needR.2.1 J1.vsc.needR.2.2.1 J1.vsc.needR.2.2.2.1 + J1.vsc.needR.2.2.2.2 (J.vsReachMissRestart hVN hni hct hxG hcv hnd s1 hs1) Vs.needR_roots + have Jd := Jdv.1.1 + set Zd := ((Z.keep (KOff (RgOf p fl4_abi.consts.bits.ASID_POOL)) (KBOf p fl4_abi.consts.bits.ASID_POOL)).dropV + p.val (p.val + 2 ^ (fl4_abi.consts.bits.ASID_POOL).val) (L.mem s1)) with hZd + set Hd := H.drop (P1.RegionD H p.val (p.val + 2 ^ (fl4_abi.consts.bits.ASID_POOL).val)) with hHd + obtain ⟨hPld, hCRd, hFId, hDRd, hMOd, hIAd, -⟩ := Jd.xtop.rel + have hppd : Zd.th pp = some (.cnode rs) := by + show restrictTh Z.th _ _ pp = _; rw [restrictTh_out hppOff]; exact hpp + have hpdd : Zd.th pd = some (.cnode rd) := by + show restrictTh Z.th _ _ pd = _; rw [restrictTh_out hpdOff]; exact hpd + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hnull1 : capAt (L.mem s1) (cslotAddr (pd.bv, idxd)) = ok .Null := (hSS1.capAt hAd.isSlot).trans hnull + have hnchd : ¬ ∃ y, σd.cdt y = some (pp.bv, idx) := hnch1 + obtain ⟨k1, -, -⟩ := J.jcov.ks _ hxG p _ fi hcv + rw [h2sb] at k1 + have hN4 := hN + rw [h2sb] at hN4 + -- the region is empty at the drop's state + have hE : MpEmptyAt Zd Hd p := by + refine ⟨fun q k hq => ?_, fun q k hq => ?_, fun wp hw => ?_⟩ + · obtain ⟨hq0, hqo⟩ := restrictTh_some hq + rw [h2sb] at hqo + have hm1 := le_max_left (objSize k) 1 + have hm2 := le_max_right (objSize k) 1 + have hm3 := max_choice (objSize k) 1 + rcases hN4 q k hq0 with h | h | h <;> rcases hm3 with e | e <;> rw [e] <;> omega + · obtain ⟨hr, hq0⟩ := P1.Heap.drop_eq_some.1 hq + unfold P1.RegionD P1.MeetsR at hr + rw [hq0] at hr + simp only [Option.elim, not_not] at hr + rw [h2sb] at hr + omega + · obtain ⟨-, hb, hw1⟩ := hw + rw [h2sb] at hw1 + refine ⟨hw1, fun i hi => ?_⟩ + have h0 : ¬ ((wp.2 + (7 - BitVec.ofInt 64 i)) ∈ regionSet p.bv 12) := fun h => hb i hi ((hRg12 _).2 h) + rw [regionSet_iff' p _ 12 (by decide) hfit] at h0 + omega + have hxGd : cslotAddr (pp.bv, idx) ∈ Zd.G.chains.flatten := hxG + have hcvd := capV_of_capAt hcap1 + obtain ⟨hRg, hRO⟩ := Jd.mpRegionCAt hppd hidx hcap1 hnchd hE + -- FL4's restart, then the perform from the drop's state + rw [hs1]; simp only [bind_ok] + refine WP.spec_mono (mp_perform_relB (L := L) vsMpHyps_top s1 ar σd Zd.G hPld hCRd.1 hCRd.2.1 hCRd.2.2.1 + hCRd.2.2.2.1 hCRd.2.2.2.2 Jd.ut Jd.xtop.fin.2.1 Jd.xtop.fin.2.2.1 Jd.xtop.fin.1 Jd.xtop.fin.1.2.vs + Jd.xtop.fin.1.2.pt hppd hidx rfl hcap1 hnchd hpdd hld hnull1 hi hbase + (by show σ1.archState.x64AsidTable _ = none; rw [he1]; unfold Notif.stsAct; split_ifs <;> exact hfree) hRg hRO Jd.xtop.fin.1.2.vo + Jd.xtop.fin.1.2.po Jd.xtop.fin.1.2.pv hFId hMOd.1.1 (rc := rc)) ?_ + rintro res ⟨hok, har, σ', th', G', hm, hnf, hP', hSt', hSV', hM', hC', hI', hU', hR', hPt', hSRq, hF', -, -, hMS, -, + hcur', hth', hk', hsc, hRg', hxu, hPI', hMid⟩ + obtain ⟨hi', hh, hks1, hcpu1', hW, hzero, -, hMs, hkp, hkh, hcdt, horig, hsrc, hdst, hdstb, hbhi, hat0, hat, hcap', hDI, -, -⟩ := hMid + have hTh := hE.1 + have hrt := restrictTh_clean hTh + rw [hrt] at hth' + subst hth' + -- the spec: the restart, then the perform from the restart's state + have hpe := perform_mp_detype p.bv (pd.bv, idxd) (pp.bv, idx) base.bv σ1 + have hmem : ((), σ') ∈ ((Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart >>= fun _ => + Fl4Spec.performAsidControlInvocation (.makePool p.bv (pd.bv, idxd) (pp.bv, idx) base.bv)) σ).1 := by + rw [nd_bind_one hst1, ← hpe]; exact hm + have hnf1 : ¬ ((Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart >>= fun _ => + Fl4Spec.performAsidControlInvocation (.makePool p.bv (pd.bv, idxd) (pp.bv, idx) base.bv)) σ).2 := by + rw [nd_bind_one hst1, ← hpe]; exact hnf + have hMO' : MdbOKV (L.ksb s1) (L.mem res.2.1) G' := hMS.okv hMOd + -- the footprint + have hWr0 : P1.WritesIn st s s1 W := by + have h := sts_foot (W := W) hPl (fun b h => Or.inl h.toW) (P1.WritesIn.refl s W) htcb J.kern.act + (v := sched.TS_RESTART) + rw [hs1, WP.spec_ok] at h; exact h + have hWr1 : P1.WritesIn st s1 res.2.1 W := ⟨hks1, hcpu1', fun b hb hn => hW b hb (fun hw => hn (by + rcases hw with ⟨x', k, hx', hk, rfl⟩ | hr | rfl + · exact Or.inl (Or.inr (Or.inl ⟨x', k, hx'.of_restrict, hk, rfl⟩)) + · right; rintro ⟨g1, g2⟩; rcases k1 with e | e <;> omega + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl ⟨hi', hh, by rw [hk1]⟩)))))))⟩ + refine ⟨hok, har, σ', Zd.th, G', hmem, hnf1, hP', ⟨hSt', hSV', hM', hC', hI'⟩, hF', by rw [← hk1]; exact hMO', + hWr0.trans1 hWr1, fun hDR' => ?_⟩ + -- `XTopAt` after (as `decode_invocation_asid_controlJC`) + have hTSR := Jd.mpPlacedCAt hE hxGd hcvd Zd.th res.2.1 res.2.2 σ' hrt.symm hk' hsc hRg' + have hXd := Jd.xtop + have hFx : FinX st Zd.th (insert p Zd.P) Zd.L4 Zd.L3 Zd.L2 Zd.L1 + (TopSR st Zd.th (insert p Zd.P) Zd.L4 Zd.L3 Zd.L2 Zd.L1 Zd.N Zd.T Zd.C Zd.E Zd.R Zd.B Zd.c AR) + res.2.1 res.2.2 σ' := by + refine ⟨hTSR.1 hSRq, hR', hPt', hPI' Zd.L4 hXd.fin.2.2.2.1, ?_⟩ + obtain ⟨t, h1, h2, -⟩ := hXd.fin.2.2.2.2 + obtain ⟨⟨P', L4', L3', L2', L1', hVC⟩, -⟩ := hMO'.2.2 + have h2' := hTSR.2 t h2 + refine ⟨t, h1.trans hcur'.symm, h2', fun q asid h => ?_⟩ + have hv := hVC.at hM' ⟨t, 1, Or.inr ⟨h2', by decide⟩, rfl⟩ h (by simp) + simp only [VCapOK] at hv + exact (hv.2.2 trivial).1 + obtain ⟨⟨⟨hSch, hEp, hNt, -⟩, -⟩, -⟩ := id hFx + have hX' : XTopAt (L := L) AR (Zd.mp G' p) res.2.1 res.2.2 σ' := + ⟨hFx, ⟨hFx.1.2.pl, ⟨hSt', hSV', hM', hC', hI'⟩, hF', hDR', by rw [hk']; exact hMO', + IpcApart.of_topD hFx.1.2 (by rw [hk']; exact hIAd.dom), ScApart.of_top hFx.1.2⟩, + IpcRel.of hNt hEp (sched_sc hSch), hXd.esa.mono hk' (fun q y n hq => hsc ⟨q⟩ y n hq)⟩ + -- The shared state and guest overlays after insertion in the dropped region. + have Ad := Jd.mpAddOKAt hxGd hE hcvd hRg.pal hRg.pfit hRg.p0 + have hspecd : ∀ o ko, σd.kheap o = some ko → o.toNat < p.val ∨ p.val + 4096 ≤ o.toNat := by + intro o ko ho + have h0 : o ∉ regionSet p.bv 12 := fun hin => by + have e : σd.kheap o = none := hSD.kin o ((hRg12 o).2 hin) + rw [e] at ho; cases ho + rw [regionSet_iff' p o 12 (by decide) hfit] at h0 + omega + have hDLd : DestCap Zd.th (L.mem s1) pd := by + obtain ⟨y', r, gs, g, hy', hc⟩ := DestCap.of_cnRef hCn ⟨y, hext.1 y h4, capV_of_capAt h7⟩ + have hy'G : y' ∈ Z.G.chains.flatten := (hCR.2.2.1.members y').2 ⟨hy', _, capAt_capV _ _, by rw [hc]; simp⟩ + exact ⟨y', r, gs, g, isSlot_restrict_off hPl hN hy' (by rw [h2sb]; exact hns y' hy'G), + by rw [hSS1.capV hy']; exact hc⟩ + have hw := (show P1.WritesIn st s1 res.2.1 (MpW Zd.th p (L.ksb s1) hi') from ⟨hks1, hcpu1', hW⟩) + obtain ⟨B', hFr⟩ := Jd.mpStepJ hh hTh hE.2.1 Ad hspecd hppd hidx hcap1 hnchd hpdd hld hnull1 hDLd hX' + hw hzero hMs hkp hkh hcdt horig hsrc hdst hdstb hbhi hat0 hat hRg.p0 hcap' (hDI Jd.del) hU' + have hFresh : σd.kheap p.bv = none := by + apply hSD.kin p.bv ((hRg12 _).2 ((regionSet_iff' p p.bv 12 (by decide) hfit).2 ?_)) + change p.val ≤ p.val ∧ p.val < p.val + 2 ^ 12 + omega + exact (Jdv.frameMakePool hh hE.2.1 Ad hFresh hppd hidx hpdd hld hcap1 hnull1 B' + hw hFr hkh hkp hat hsrc hdst hcap' hMS).anyX arI + have hD := decode_invocation_asid_controlXP arI Z.th hPl s ar σ Z.G hCR hFI J.ut hMO vsMpHyps_top hX.fin.1 + hX.fin.2.1 hX.fin.2.2.1 hX.fin.1.2.vs hX.fin.1.2.pt hX.fin.1.2.vo hX.fin.1.2.po hX.fin.1.2.pv lbl slbl hlM length + buf ct hct hcur htcb J.kern.act (J.curRun.run hni) args hlen hargs extra excaps (Q := fun s' => P1.WritesIn st s s' W) + (X := fun s' a' σ' => DomRel (L.mem s') (L.ksb s') σ' → VEAnyX (L := L) arI AR s' a' σ') hext' + (fun _ => Jv.anyX arI) slot blk call don fp ci sl buffer + -- the domain schedule (VsThX's `domFrame_asid_control`) + have hS := hX.fin.1 + have hF : DomFrame (L := L) arI s ar σ lbl length slot .AsidControl blk call don fp buf extra slbl + (args.map UScalar.bv) ci sl excaps buffer := by + refine ⟨WP.spec_mono hD fun res hr => ?_, dk_spec_arch ⟨_, rfl⟩ hkt⟩ + rcases hr.2 with e | hw + · rw [e]; exact ⟨rfl, fun _ _ => rfl⟩ + · refine ⟨hw.1, fun o ho => hw.2.2 _ ?_ ?_⟩ + · have := hS.2.vs.ksb_fit + exact ksW_al (by omega) hS.2.vs.ksb_al (domOffs_lt' ho).1 (domOffs_lt' ho).2 + · rintro (h | h) + · exact dom_not_foot hS ho h + · apply h + have := hS.2.vs.ksb_fit + have hb : (ksW (L.ksb s) o).val = (L.ksb s).val + o.val := ksW_val (by have := (domOffs_lt' ho).1; omega) + have := (domOffs_lt' ho).1 + rw [hb]; omega + have hNative := DecOKMX.toXD arI (DecOKMQX.toX arI hD (fun _ _ _ h => h)) hF + (okAll_specDec_asidControl fp slbl (args.map UScalar.bv) ci sl excaps buffer) hDR (fun _ _ _ hx hd => hx hd) + refine WP.spec_mono hNative fun res post => ?_ + have hEq : specDecV fp slbl (args.map UScalar.bv) ci sl .AsidControl excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec fp slbl (args.map UScalar.bv) ci sl .AsidControl excaps buffer) := rfl + change VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp slbl (args.map UScalar.bv) ci sl .AsidControl excaps buffer) s res + rw [hEq] + rcases post with ⟨e, he, hs, ⟨e', hm, hRel⟩, hY⟩ | ⟨iv, hi, σ1, hst, y, hy, ⟨_, _, hr, -⟩, hY⟩ + · exact Or.inl ⟨e, he, hs, ⟨e', run_liftME_err hm, hRel⟩, hY⟩ + · have hArch : specDec fp slbl (args.map UScalar.bv) ci sl .AsidControl excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec + (Fl4Spec.archDecodeInvocation (Z := Unit) slbl (args.map UScalar.bv) ci sl .asidControlCap excaps)) := rfl + have hi' := hi + rw [hArch] at hi' + obtain ⟨iv', rfl⟩ := decOut_liftME2_inv hi' + refine Or.inr ⟨.base (.invokeArchObject (.spec iv')), hi.liftME, σ1, hst, y, hy, Or.inl ?_⟩ + rcases hr with ⟨h1, h2⟩ | ⟨_, h2⟩ + · exact ⟨h1, h2, hY⟩ + · exact (NoPre.perform_noErr blk call don (.invokeArchObject (.spec iv')) (fun h => h) σ1 y hy () h2).elim + +/-- **The selected ASID control cap, every label** (`selectedAsidControl` at the selected decode's reads). -/ +theorem VmEptState.selectedAsidControlS3 {MR : A → Fl4Spec.MachineState → Prop} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) + {ct cptr len buf lbl : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl .AsidControl ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s ar lbl len (cslotAddr sl) .AsidControl blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .AsidControl exs buffer) s res ⦄ := by + have h := J.selectedAsidControl (MR := MR) (lbl := lbl) hni q.current (args.map fun w => ⟨w⟩) q.length q.words ex exs q.extra + blk call don fp (Fl4Spec.toBl cptr.bv) sl buffer on rc + rw [zv_map_mk args] at h + exact h + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Budget.lean b/proofs/Fl4Proofs/P3/VmEntryS3Budget.lean new file mode 100644 index 000000000..9fdcdd1ed --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Budget.lean @@ -0,0 +1,68 @@ +/- +The x86 VM entry's post-FaultIP budget guard and the declining ReplyRecv fast path. + +The budget guard uses the existing [J] entry facts `Entered`, the current +thread outside the ready queues, `ValidMachineTime` and `Sched.OffReady`. +Its [P] facts are those of `VmEptState.budgetVX`: machine and CR3 agreement, +architecture totality, the MCS timing constants and the one-domain configuration. +Fast-path decline uses architecture and store totality. +-/ +import Fl4Proofs.P3.VmKEntryX +import Fl4Proofs.P3.VmKEntry + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Fl4Spec Fl4Proofs.P1 Sched Vs + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Cr3 → Prop} + +/-- The related post-FaultIP state has the entry facts needed by the budget guard. +The concrete and abstract states must be paired at this boundary. -/ +theorem VmEptState.entryBudget + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s sf : S} {a : A} {σ σf : State Unit} {MR : A → MachineState → Prop} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) + (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hnd : numDomains = 1) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hf : ((), σf) ∈ (fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1) + (hV : VEAnyX (L := L) arI AR sf a σf) : + syscall.budget_ok st arI sf a ⦃ r => + ∃ σb, (r.1, σb) ∈ (specBudget σf).1 ∧ VEAnyX (L := L) arI AR r.2.1 r.2.2 σb ⦄ := by + have hct := cur_thread_eq (L := L) s + let ct := L.curW s + change sched.cur_thread st s = ok ct at hct + have hcur := (J.1.1.curEq hct).1 + have ht := TopSR.cur_mem J.1.1.xtop.fin.1 hcur.symm + obtain ⟨x, hk, _⟩ := (J.1.1.xtop.irelK J.1.1.ks J.1.1.buf).1.tcb ct ht + have hk' : σ.kheap σ.curThread = some (.tcb x) := by rwa [hcur] + rw [fl4SyscallFaultIP_eq hk'] at hf + have e : σf = faultIPSt σ σ.curThread x := congrArg Prod.snd (Set.mem_singleton_iff.mp hf) + subst σf + obtain ⟨Vf, Xf, XLf, Zf, Hf, Csf, Acf, Lstf, Df, _, Jf⟩ := hV + exact Jf.budgetVX hni hnq hM hMR hVM + (SInvR.G4.offReady_faultIPSt (t := σ.curThread) (x := x) hOff) hK hAT hnd hmrt hmb hmax htu + +/-- A declining in-gate ReplyRecv keeps both concrete state components. -/ +theorem replyRecvDecline (hAT : P0.ArchTotal arI) (s : S) (a : A) : + fastpath.reply_recv st arI s a ⦃ r => r.1 = false → r.2 = (s, a) ⦄ := by + obtain ⟨⟨run, s', a'⟩, h, _⟩ := P1.spec_ex_fd (P0.fastpath.reply_recv.p0 st arI s a L.total hAT) + rw [h, WP.spec_ok] + intro hrun + change run = false at hrun + subst run + obtain ⟨rfl, rfl⟩ := P1.reply_recv_decline st arI h + rfl + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Coverage.lean b/proofs/Fl4Proofs/P3/VmEntryS3Coverage.lean new file mode 100644 index 000000000..fdcdfc976 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Coverage.lean @@ -0,0 +1,35 @@ +/- +P3 (line mmuAD, Stage 3): **coverage of the selected-cap dispatcher**. `SelectedVmDecoder` holds at every native cap +(only `Vcpu` and `Ept` caps are outside it, and neither is native), so with `dispatchReached`'s VM-side split every +reached cap has its decoder post (`VDispReachAt`). + +No premise beyond those of `dispatchKnownSelected` and `dispatchReached`. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.VmEntryS3Selected +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Vs Notif Endp Ipc Sched P1 + +noncomputable section + +/-- Every native cap, at every label and phase, is one of `dispatchKnownSelected`'s routes. -/ +theorem selectedVmDecoder_of_native (fp : Bool) (lbl : U64) {c : capability.Cap} (h : NativeCap c) : + SelectedVmDecoder fp lbl c := by + cases c <;> first | trivial | exact h | exact h.elim + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} + +/-- The selected-cap dispatcher's reach covers the native decoder branches left by `dispatchReached`. -/ +theorem VDispReachAtFor.native_of_selected {on rc : Fl4Spec.ObjRef → Bool} {Z : XSets} {s : S} {a : A} + {σ : Fl4Spec.State Unit} {ct : U64} {call blk don fp : Bool} {cptr : U64} + (h : VDispReachAtFor (L := L) arI AR MR (SelectedVmDecoder fp) on rc Z s a σ ct call blk don fp cptr) : + VDispReachNativeAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr := by + intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hlook hr he hN + exact h info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hlook hr he + (selectedVmDecoder_of_native fp info.label hN.1) + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Decode.lean b/proofs/Fl4Proofs/P3/VmEntryS3Decode.lean new file mode 100644 index 000000000..942d63a11 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Decode.lean @@ -0,0 +1,649 @@ +/- +Selected-cap VM decoder adapters for CNode, TCBSetEPTRoot, Untyped and EPT +ASID assignment. The front facts come from the successful paired reads and +the invocation's actual LookAt. + +[J] CNode carries SpecGlob, current-SC readiness, queue exclusion, the +endpoint queue invariant and MRV at the reached state. Untyped uses the +existing retype invariants and the restart footprint at the selected slot. +[P] The existing per-leaf architecture, scheduling, machine and +configuration agreements. [S] The local-target condition for SetEPTRoot +and core zero for Untyped are checked only on their reached routes. +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmEntryS3Oracle +import Fl4Proofs.P3.VmEntryS3Pool +import Fl4Proofs.P3.VmEntryS3ScCtl +import Fl4Proofs.P3.XTopJointVsCap +import Fl4Proofs.P3.FrontEndLabel +import Fl4Proofs.P1.LocalTimer + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +private abbrev selectedWords (args : List Fl4Spec.Data) : List U64 := args.map fun x => ⟨x⟩ + +/-- The selected CNode slot and the decoder reads at the matched prefix. +The spec label is normalized to the bitvector of the concrete label. -/ +structure VSelectedDecode (st : machine.Store S) [P1.LawfulStore st] (Z : XSets) (s : S) + (σ : Fl4Spec.State Unit) (ct cptr len buf : U64) (sl : Fl4Spec.CslotPtr) + (cap : capability.Cap) (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : Prop where + current : sched.cur_thread st s = ok ct + lookup : LookAt ct cptr σ sl cap + slot : ∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length) + capAt : Fl4Proofs.P3.capAt (P1.LawfulStore.mem st s) (cslotAddr sl) = ok cap + extra : ExtraOK Z.th (P1.LawfulStore.mem st s) ex exs + vargs : VArgs st s ct buf len args + length : len.val = (selectedWords args).length + words : ∀ (i : U64) x, (selectedWords args)[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x + +/-- Project the successful front reads to the selected decoder arguments. -/ +theorem JAtZB.selectedDecode + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {ct cptr : U64} (hct : sched.cur_thread st s = ok ct) + {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s ct = ok info) + {mi : Fl4Spec.MessageInfo} (hmi : Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False)) + {buf : U64} (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) + {buffer : Option Fl4Spec.ObjRef} + (hbuffer : Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False)) + {r2 : core.result.Result (Array obj.Slot 3#usize) error.Fault} + (hr2 : ipc.lookup_extra_caps st s ct buf info = ok r2) + {er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))} + (her : (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False)) + {args : List Fl4Spec.Data} (hmrs : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False)) + {sl : Fl4Spec.CslotPtr} {cap : capability.Cap} {ex : Array obj.Slot 3#usize} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (hslot : ∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) + (hcap : capAt (L.mem s) (cslotAddr sl) = ok cap) + (hlook : LookAt ct cptr σ sl cap) (hEx : r2 = .Ok ex) (hExs : er = .ok exs) : + info.label.bv = mi.miLabel ∧ VSelectedDecode st Z s σ ct cptr (msgLen info buf) buf sl cap ex args exs := by + have hX := J.xferH + obtain ⟨hlbl, hex⟩ := J.frontJ hX.1 hX.2.1 hct hinfo hmi hbuf hbuffer hr2 her + have hmrs' : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = + ({((selectedWords args).map UScalar.bv, σ)}, False) := by + rw [show (selectedWords args).map UScalar.bv = args from zv_map_mk args] + exact hmrs + obtain ⟨hlen, hwords, -⟩ := J.argsJ hX.1 hX.2.1 hct hinfo hmi hbuf hbuffer hmrs' + exact ⟨hlbl, hct, hlook, hslot, hcap, hex ex exs hEx hExs, J.vargs hct hinfo hmi hbuf hbuffer hmrs, + hlen, hwords⟩ + +/-- The selected slot is a typed CNode slot. -/ +theorem VSelectedDecode.absSlot {ct cptr len buf : U64} {sl : Fl4Spec.CslotPtr} + {c : capability.Cap} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl c ex args exs) : AbsSlot Z.th sl := by + obtain ⟨p, hp, hth⟩ := q.slot + exact ⟨p, hp, Or.inl hth⟩ + +/-- The inherited core restrictions apply at the actual invoking slot. +[S] K1 is the selected SchedControl core. K4 is core zero for the selected +Untyped cap. `core` is the executing core. A TCB's stall identity uses the +local target from the matched scheduler relation. The cap `c` is the one FL4 +decodes from `mem` at the looked-up slot: `LookAt` alone reads only +`absCap c`, which drops a SchedControl cap's core. -/ +def SelectedCoreScope (core : U64) (mem : U64 → U64) (σ : Fl4Spec.State Unit) (ct cptr : U64) : Prop := + ∀ sl c, LookAt ct cptr σ sl c → capAt mem (cslotAddr sl) = ok c → match c with + | .SchedControl cpu => cpu = core + | .Untyped .. => core = 0#u64 + | _ => True + +/-- The system calls that invoke a capability. -/ +def InvokesCap (n : I64) : Prop := + n = fl4_abi.syscall.SysSend ∨ n = fl4_abi.syscall.SysNBSend ∨ n = fl4_abi.syscall.SysCall ∨ + n = fl4_abi.syscall.SysReplyRecv ∨ n = fl4_abi.syscall.SysNBSendRecv ∨ n = fl4_abi.syscall.SysNBSendWait + +/-- The capability pointer `dispatch_syscall` invokes for system call `n`: the reply register for ReplyRecv and +NBSendWait, the destination register for NBSendRecv, the cap register otherwise. -/ +def invokedCptr (st : machine.Store S) [P1.LawfulStore st] (s : S) (ct : U64) (n : I64) : U64 := + if n = fl4_abi.syscall.SysReplyRecv ∨ n = fl4_abi.syscall.SysNBSendWait then + P1.LawfulStore.mem st s (regA ct layout.reg.REPLY) + else if n = fl4_abi.syscall.SysNBSendRecv then + P1.LawfulStore.mem st s (regA ct layout.reg.NBSEND_RECV_DEST) + else P1.LawfulStore.mem st s (regA ct layout.reg.CAP) + +/-- A TCB cap at the selected typed slot names a thread of this core's +scheduler relation. This is the concrete check used for the stall. -/ +theorem JAtZB.selectedThreadLocal (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p : U64} {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} + {args : List Fl4Spec.Data} {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) : + L.mem s (P1.affA p) = L.cpu s := by + have ht : Z.th p = some .tcb := B.xtop.rel.2.1.2.1.abs q.absSlot q.capAt + obtain ⟨_, _, hC, -⟩ := B.xtop.fin.1.1.1.lists + exact (hC.aff p (B.thT p ht)).trans hC.cur.symm + +/-- The EPT map label is selected independently of the frame's mapping bits. +An unmapped frame may satisfy `NativeCap` and still reach this decoder. -/ +theorem VmEptState.selectedFrameMapEpt [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + {ct cptr len buf ptr rights asid va : U64} {sz : capability.FrameSize} {dev m ep : Bool} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Frame ptr sz rights dev m ep asid va) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PageMapEPT len (cslotAddr sl) + (.Frame ptr sz rights dev m ep asid va) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PageMapEPT.bv args (Fl4Spec.toBl cptr.bv) sl + (.Frame ptr sz rights dev m ep asid va) exs buffer) s res ⦄ := by + have hB := J.1.1 + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + have hct : ct = L.curW s := ok_inj (q.current.symm.trans (cur_thread_eq (L := L) s)) + have hV : VArgs st s (L.curW s) buf len args := hct ▸ q.vargs + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + exact hd.frameEpt zv_frameEptLbl blk call don fp buf ex hpc rfl q.capAt args hV exs + (ExtraOK.relS hB.xtop.rel.1 q.extra) fp (Fl4Spec.toBl cptr.bv) buffer + +/-- Send through the cap selected by the invocation lookup. The signal +uses the VM-aware handler at the matched post-Restart read point. +[J] The caller is entered. [P] Architecture and minimum budget agreement. -/ +theorem VmEptState.selectedNotificationS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hAK : ARKeep arI AR) + (hmb : ∀ a0, sporadic.min_budget arI a0 ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {ct cptr len buf lbl n badge : U64} {send recv : Bool} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Notification n badge send recv) ex args exs) + (buffer : Option Fl4Spec.ObjRef) {on rc : Fl4Spec.ObjRef → Bool} + (hO : VmOrc st on rc s σ) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) + (.Notification n badge send recv) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Notification n badge send recv) exs buffer) s res ∧ + (res.1 = .Ok () → ∃ s1 σ1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 ∧ + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + VmOrc st on rc s1 σ1) ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases send + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩, + fun he => by cases he⟩ + exact run_liftME_err (specDec_notification fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl n badge false recv exs buffer σ) + simp only [↓reduceIte] + have B := J.1.1 + have hn := (B.slotN q.absSlot q.capAt).1 + obtain ⟨_, x, hk, hr⟩ := B.curRunnable hni q.current + obtain ⟨s1, σ1, hst, _, hRestart, _, _, _, B1, F1⟩ := B.restartRunJ q.current hk hr + rw [hRestart, bind_ok] + have J1 := J.ofNat (V' := Z.vs) B1 F1 + have hO1 := B.vmOrcAcross B1 hO F1.fk.1.2.2.1 + refine WP.spec_bind (J1.signalVFk hAT hVA hAK hmb hn badge hO1) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, _, _, _, hsig, _, J2, _⟩ + apply (WP.spec_ok _).2 + refine ⟨Or.inr ⟨.base (.invokeNotification n.bv badge.bv), ?_, σ1, hst, + (.ok [], σ2), ?_, Or.inl ⟨rfl, rfl, J2.anyX arI⟩⟩, fun _ => ⟨s1, σ1, rfl, hst, hO1⟩⟩ + · have hd := specDec_notification fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl n badge true recv exs buffer σ + exact (DecOut.of_eq hd).liftME + · apply perfNil_ok + unfold Fl4Spec.withoutPreemption + exact mem_liftE (x := ((), σ2)) (by rw [hsig]; exact Set.mem_singleton _) + +/-- The selected I/O decoder supports both the unowned and VCPU-owned +forms of an I/O-port cap and preserves its decoder reply. -/ +theorem VmEptState.selectedIoPortS3 [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hVal : PortInValAt arI a σ) + {ct cptr len buf lbl first last owner : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.IoPort first last owner) ex args exs) + (hRun : ZvCtRun σ ct) (buffer : Option Fl4Spec.ObjRef) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) + (.IoPort first last owner) blk call don fp buf ex ⦃ res => + let m := specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.IoPort first last owner) exs buffer + VDispPostVEX (L := L) arI AR MR on rc σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don m ct s res ⦄ := by + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + rw [← zv_map_mk args] + exact hd.ioPort hAM hAT q.current hRun hVal (selectedWords args) q.length q.words sl q.absSlot + (capV_of_capAt q.capAt) blk call don fp ex (Fl4Spec.toBl cptr.bv) exs buffer + +/-- Null or zombie caps take the existing state-preserving error. -/ +theorem VmEptState.selectedNullZombie + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf lbl : U64} {sl : Fl4Spec.CslotPtr} {c : capability.Cap} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl c ex args exs) + (hCap : c = .Null ∨ ∃ p zt n, c = .Zombie p zt n) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) c blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl c exs buffer) s res ⦄ := by + rcases hCap with rfl | ⟨p, zt, n, rfl⟩ <;> + rw [syscall.decode_invocation, q.current, bind_ok, WP.spec_ok] <;> + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ <;> + unfold specDecV <;> + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] <;> + exact run_liftME_err (run_throwError _ σ) + +set_option maxHeartbeats 4000000 in +/-- The selected CNode at labels 25–32, including the first-phase rejection. +The deletion and preemption path is the VM deletion chain. +[J] The exact CNode start invariants. [P] Per-leaf architecture and clock agreement. -/ +theorem VmEptState.selectedCNode [P1.PlainStore st] [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hAT : P0.ArchTotal arI) (hKDAR : MRKeepD arI AR) (hK : ARKeep arI AR) + (hticks : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlatC arI MR) (hKD : MRKeepD arI MR) + (hPA : PPPlatA arI AR) (hAM : ARMask arI AR) + (hsg : SpecGlob σ) (hmv : MRV MR a σ) (hQ : ∀ e, EpQPre σ e) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d prio, σ.curThread ∉ σ.readyQueues d prio) + {ct cptr len buf lbl p r gs g : U64} (hrd : 25 ≤ lbl.val ∧ lbl.val ≤ 32) + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.CNode p r gs g) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.CNode p r gs g) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.CNode p r gs g) exs buffer) s res ⦄ := by + have B := J.1.1 + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + rw [← zv_map_mk args] + exact hd.cnode ⟨L.total, hAT, hKDAR⟩ hK hticks hnd hM hKD hPA hAM hsg + (B.ppr0_start arI hni hRd0 hnq) hmv hQ lbl hrd lbl.bv (cnode_in lbl hrd.1 hrd.2) + len buf ct q.current (selectedWords args) q.length q.words sl q.absSlot p r gs g + (capV_of_capAt q.capAt) ex (exAt ex 0) (exAt ex 1) (exAt_idx0 ex) (exAt_idx1 ex) + exs (ExtraOK.rel2 B.xtop.rel.1 q.extra) blk call don fp (Fl4Spec.toBl cptr.bv) buffer + +/-- Out-of-range CNode labels have a related error and keep the VM epoch. +On the first phase the generic invalid-capability error takes precedence. -/ +theorem VmEptState.selectedCNodeOutside + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf lbl p r gs g : U64} (hrd : ¬ (25 ≤ lbl.val ∧ lbl.val ≤ 32)) + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.CNode p r gs g) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.CNode p r gs g) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.CNode p r gs g) exs buffer) s res ⦄ := by + unfold syscall.decode_invocation + rw [q.current]; simp only [bind_ok] + cases fp + · simp only [Bool.false_eq_true, ↓reduceIte] + have hdec := cnodeDec_out (cnode_out lbl hrd).1 args (absCap (.CNode p r gs g)) (exs.map Prod.fst) + have hspec : (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl + (.CNode p r gs g) exs buffer).run σ = ({(.error .illegalOperation, σ)}, False) := by + change (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeCNode + (Fl4Spec.decodeCnodeInvocation lbl.bv args (absCap (.CNode p r gs g)) (exs.map Prod.fst)))).run σ = _ + rw [hdec] + exact run_liftME_err (run_liftME_err (run_throwError _ σ)) + unfold cnode.decode + by_cases hlo : lbl.val < 25 + · have h : lbl < fl4_abi.label.CNodeRevoke := by have := lbl_revoke_val; scalar_tac + rw [if_pos h] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, ⟨_, hspec, Or.inl trivial⟩, J.anyX arI⟩ + · have hn : ¬ lbl < fl4_abi.label.CNodeRevoke := by have := lbl_revoke_val; scalar_tac + have hh : lbl > fl4_abi.label.CNodeRotate := by have := lbl_rotate_val; scalar_tac + rw [if_neg hn, if_pos hh] + simp only [error.illegal, bind_ok, WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, ⟨_, hspec, Or.inl trivial⟩, J.anyX arI⟩ + · simp only [↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + change (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidCapability 0))).run σ = _ + exact run_liftME_err (run_throwError _ σ) + +/-- TCBSetEPTRoot in the second phase. [S] Only the selected target must be +local. The existing leaf covers malformed and missing extra caps. -/ +theorem VmEptState.selectedSetEpt [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + {ct cptr len buf p : U64} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.TCBSetEPTRoot len (cslotAddr sl) (.Thread p) + blk call don false buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV false fl4_abi.label.TCBSetEPTRoot.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ⦄ := by + have B := J.1.1 + have hPl := B.xtop.rel.1 + have ht : Z.th p = some .tcb := B.xtop.rel.2.1.2.1.abs q.absSlot q.capAt + have hstall := P1.stall_none (L := L) arI s a p (B.selectedThreadLocal q) + have hlbl : Fl4Spec.dataToNat fl4_abi.label.TCBSetEPTRoot.bv = Fl4Spec.fl4SetEptRootLabel := by + rw [dataToNat_bv, lbl_tcbSetEPTRoot_val] + rfl + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + exact hd.setEpt len (cslotAddr sl) p blk call don buf ex hstall (exAt ex 0) (exAt_idx0 ex) exs + (ExtraOK.relS hPl q.extra) fl4_abi.label.TCBSetEPTRoot.bv hlbl args (Fl4Spec.toBl cptr.bv) sl buffer + ht q.absSlot.isSlot (capV_of_capAt q.capAt) + +/-- A selected Thread cap is rejected in the first phase before the stall. -/ +theorem VmEptState.selectedThreadFirst + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf lbl p : U64} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don true buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV true lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ⦄ := by + unfold syscall.decode_invocation + rw [q.current] + simp only [bind_ok, ↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base true lbl.bv args (Fl4Spec.toBl cptr.bv) sl + (absCap (.Thread p)) exs buffer (by simp [absCap, Fl4Spec.VirtRoute])] + change (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidCapability 0))).run σ = _ + exact run_liftME_err (run_throwError _ σ) + +set_option maxHeartbeats 4000000 in +/-- The selected Untyped decoder, including the non-retype label and error +arms. [S] Core zero is required only for this selected Untyped route. +[J] The region-read inputs refer to its actual cap range and restart state. -/ +theorem VmEptState.selectedUntyped [P1.PlainStore st] [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hAT : P0.ArchTotal arI) (hag : ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hCap : ∀ a', VirtSanEq.CapAgree arI a') + {ct cptr lbl len buf p sb fi : U64} {dev : Bool} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Untyped p sb fi dev) ex args exs) + (hc0 : Z.c = 0#u64) (hfan : Fl4Spec.retypeFanOutLimit = 256#64) + (hRU : RegionUnrefAt (L := L) H Cs D s) + (hUZ : dev = false → UtZero (L.mem s) Z.G σ) + (hUN : dev = false → UtNodesJ (L.mem s) Z.G (L.ksb s)) + (hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (hGP : dev = true → DevGpm (L.mem s) Z.G σ) + (hAK : ARKeep arI AR) (hnd : Fl4Spec.numDomains = 1) (hMP : MachPlat arI MR) (hPA : PPPlatA arI AR) + (hrcb : Fl4Spec.resetChunkBits = 16) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d prio, σ.curThread ∉ σ.readyQueues d prio) + (hVM : (∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr sl)) → ∀ s1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1)) + (hmv : MRV MR a σ) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Untyped p sb fi dev) blk call don fp + buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Untyped p sb fi dev) exs buffer) s res ⦄ := by + have B := J.1.1 + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + rw [← zv_map_mk args] + exact hd.ut hAT hag hCap lbl lbl.bv (genInvocationType_untyped_iff lbl) + len buf ct q.current (selectedWords args) q.length q.words hpc rfl q.capAt ex (exAt ex 0) (exAt_idx0 ex) + exs (ExtraOK.rel1 B.xtop.rel.1 q.extra) hfan hc0 B.carry.bufObj B.carry.archFit hRU hUZ hUN + (fun _ => B.carry.devApart) hDF hGP hAK hnd hMP hPA hrcb hRd0 hnq hVM hmv.1 hmv.2.1 hmv.2.2 + blk call don fp (Fl4Spec.toBl cptr.bv) buffer + +private theorem concreteExtraEptRoot + {c : capability.Cap} {sl : Fl4Spec.CslotPtr} {rest : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (h : Fl4Spec.startsWithEptRoot ((absCap c, sl) :: rest) = true) : + ∃ p asid va, c = .Ept p 0#u64 false asid va := by + cases c <;> try (exact False.elim (Bool.noConfusion h)) + case Ept ptr level mapped asid va => + cases hl : level.val with + | zero => + cases mapped + · have hzero : level = 0#u64 := UScalar.eq_of_val_eq (by simpa using hl) + exact ⟨ptr, asid, va, by rw [hzero]⟩ + · simp [absCap, Fl4Spec.startsWithEptRoot, hl] at h + | succ n => + cases mapped <;> simp [absCap, Fl4Spec.startsWithEptRoot, hl] at h + +/-- EPT assignment on the selected ASID pool. Read the actual first extra +cap through ExtraOK and select the EPT path by the extended spec's test. -/ +theorem VmEptState.selectedEptAsidPool [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + {ct cptr len buf p base : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.AsidPool p base) ex args exs) + (hRoot : Fl4Spec.startsWithEptRoot exs = true) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86ASIDPoolAssign len (cslotAddr sl) (.AsidPool p base) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86ASIDPoolAssign.bv args (Fl4Spec.toBl cptr.bv) sl + (.AsidPool p base) exs buffer) s res ⦄ := by + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + rcases ExtraOK.poolExtra J.1.1.xtop.rel.1 q.extra with ⟨_, hex⟩ | ⟨pc, r, idx, cv, sc, rest, hpc, hidx, he, hc, hrel, hex⟩ + · rw [hex] at hRoot + cases hRoot + · rw [hex, ← hrel.2] at hRoot ⊢ + obtain ⟨ptr, asid, va, rfl⟩ := concreteExtraEptRoot hRoot + exact hd.pool p base q.absSlot.isSlot q.capAt rfl len blk call don fp buf ex hpc hidx he hc args fp + (Fl4Spec.toBl cptr.bv) sl rest buffer + + +/-- The ported ASID-pool assign accepts a first extra unmapped PML4. -/ +def StartsWithPml4Root (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : Prop := + ∃ p sl rest, exs = (.archObjectCap (.pml4Cap p none), sl) :: rest + +private theorem poolBaseDecode + (lbl : Fl4Spec.Data) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (sl : Fl4Spec.CslotPtr) (p base : U64) (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buf : Option Fl4Spec.ObjRef) (fp : Bool) + (h : ¬ (Fl4Spec.invocationType lbl = .archInvocationLabel .x64ASIDPoolAssign ∧ + Fl4Spec.startsWithEptRoot exs = true)) : + specDecV fp lbl args ci sl (.AsidPool p base) exs buf = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec + (Fl4Spec.archDecodeInvocation (Z := Unit) lbl args ci sl (.asidPoolCap p.bv base.bv) exs))) := by + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base fp lbl args ci sl + (absCap (.AsidPool p base)) exs buf h] <;> rfl + +/-- A wrong label on the selected ASID-pool cap is rejected before the extra cap read. -/ +theorem VmEptState.selectedPoolIllegal + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr lbl len buf p base : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.AsidPool p base) ex args exs) + (hne : lbl ≠ fl4_abi.label.X86ASIDPoolAssign) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.AsidPool p base) blk call don fp + buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.AsidPool p base) exs buffer) s res ⦄ := by + have hl := fun h => hne ((pool_label lbl).2 h) + rw [poolBaseDecode _ _ _ _ _ _ _ _ _ (fun h => hl h.1)] + unfold syscall.decode_invocation + rw [q.current]; simp only [bind_ok] + unfold vspace.decode + rw [q.current]; simp only [bind_ok] + have hb : (lbl != fl4_abi.label.X86ASIDPoolAssign) = true := by simpa using hne + simp only [hb, ↓reduceIte, error.illegal, bind_ok, WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, ⟨.illegalOperation, + run_liftME_err (run_liftME_err (run_liftME_err (decodeAssign_illegal hl _ _ _ _ _ _ σ))), + Or.inl trivial⟩, J.anyX arI⟩ + +private theorem badPoolExtra {c : capability.Cap} {x : U64} + {ct p base len buf slot : U64} {ex : Array obj.Slot 3#usize} (call : Bool) + (hct : sched.cur_thread st s = ok ct) + (he : vspace.extra ex 0#usize = ok (some x)) + (hc : capAt (L.mem s) x = ok c) + (hPml4 : ∀ ptr asid, c ≠ .Pml4 ptr false asid) + (hEpt : ∀ ptr asid va, c ≠ .Ept ptr 0#u64 false asid va) : + vspace.decode st arI s a fl4_abi.label.X86ASIDPoolAssign len slot (.AsidPool p base) call buf ex = + ok (.Err (.Sys (.InvalidCapability 1#u64)), s, a) := by + unfold vspace.decode + rw [hct]; simp only [bind_ok, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [he]; simp only [bind_ok] + rw [slot_cap_eq, hc]; simp only [bind_ok] + cases c <;> (try dsimp only) <;> + try (simp only [error.invalid_cap, bind_ok]) + case Pml4 ptr mapped asid => + cases mapped + · exact False.elim (hPml4 ptr asid rfl) + · simp only [↓reduceIte, error.invalid_cap, bind_ok] + case Ept ptr level mapped asid va => + by_cases h0 : level = 0#u64 + · subst level + cases mapped + · exact False.elim (hEpt ptr asid va rfl) + · simp only [↓reduceIte] + split <;> rfl + · split + · exact False.elim (h0 rfl) + · rfl + +/-- The selected ASID-pool assignment rejects a missing extra cap and a first +extra cap other than an unmapped PML4 or unmapped level-zero EPT root. -/ +theorem VmEptState.selectedPoolBadExtra + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p base : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.AsidPool p base) ex args exs) + (hPml4 : ¬ StartsWithPml4Root exs) (hEpt : Fl4Spec.startsWithEptRoot exs = false) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86ASIDPoolAssign len (cslotAddr sl) (.AsidPool p base) + blk call don fp buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86ASIDPoolAssign.bv args (Fl4Spec.toBl cptr.bv) sl + (.AsidPool p base) exs buffer) s res ⦄ := by + have hl := (pool_label fl4_abi.label.X86ASIDPoolAssign).1 rfl + rw [poolBaseDecode _ _ _ _ _ _ _ _ _ (fun h => by rw [hEpt] at h; cases h.2)] + unfold syscall.decode_invocation + rw [q.current]; simp only [bind_ok] + rcases ExtraOK.poolExtra J.1.1.xtop.rel.1 q.extra with + ⟨he, rfl⟩ | ⟨pc, r, idx, cv, sc, rest, hpc, hidx, he, hc, hrel, rfl⟩ + · unfold vspace.decode + rw [q.current]; simp only [bind_ok, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + rw [he]; simp only [bind_ok, error.truncated, WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, ⟨.truncatedMessage, + run_liftME_err (run_liftME_err (run_liftME_err (decodeAssign_trunc hl _ _ _ _ _ σ))), + Or.inl trivial⟩, J.anyX arI⟩ + have hbad : ∀ root, sc ≠ .archObjectCap (.pml4Cap root none) := by + intro root heq + exact hPml4 ⟨root, (pc.bv, idx), rest, by rw [heq]⟩ + have hPc : ∀ ptr asid, cv ≠ .Pml4 ptr false asid := by + intro ptr asid h + apply hbad ptr.bv + rw [← hrel.2, h] + rfl + have hEc : ∀ ptr asid va, cv ≠ .Ept ptr 0#u64 false asid va := by + intro ptr asid va h + rw [← hrel.2, h] at hEpt + cases hEpt + rw [badPoolExtra call q.current he hc hPc hEc] + rw [WP.spec_ok] + exact Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 1, + run_liftME_err (run_liftME_err (run_liftME_err (decodeAssign_bad hl _ _ _ _ _ hbad _ _ σ))), + Or.inr ⟨1#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + +/-- Native assignment reads the unmapped PML4 from the actual first extra +CNode slot selected by the paired front end. -/ +theorem VmEptState.selectedNativeAsidPool + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {ct cptr len buf p base : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.AsidPool p base) ex args exs) + (hRoot : StartsWithPml4Root exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86ASIDPoolAssign len (cslotAddr sl) (.AsidPool p base) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86ASIDPoolAssign.bv args (Fl4Spec.toBl cptr.bv) sl + (.AsidPool p base) exs buffer) s res ⦄ := by + rcases ExtraOK.poolExtra J.1.1.xtop.rel.1 q.extra with + ⟨_, he⟩ | ⟨pc, r, idx, cv, sc, rest, hpc, hidx, he, hc, hrel, hEx⟩ + · rw [he] at hRoot + obtain ⟨root, src, rest, h⟩ := hRoot + cases h + · obtain ⟨root, src, rest', h⟩ := hRoot + have hhead := congrArg + (fun l : List (Fl4Spec.Cap × Fl4Spec.CslotPtr) => l.head?.map Prod.fst) (hEx.symm.trans h) + have hsc : sc = .archObjectCap (.pml4Cap root none) := Option.some.inj hhead + have hP : ∃ ptr asid, cv = .Pml4 ptr false asid := by + have e := hrel.2.trans hsc + cases cv <;> try (simp [absCap] at e) + case Pml4 ptr mapped asid => + cases mapped + · exact ⟨ptr, asid, rfl⟩ + · simp [absCap] at e + obtain ⟨ptr1, asid0, rfl⟩ := hP + rw [hEx, ← hrel.2] + exact J.nativePoolDecode arI hni p base q.absSlot.isSlot q.capAt len blk call don fp buf ex + hpc hidx he hc args (Fl4Spec.toBl cptr.bv) sl rest buffer on rc + + +/-- Every ASID-pool decode at the selected cap, with the native and guest +routes distinguished by the actual first extra cap. -/ +theorem VmEptState.selectedAsidPool [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + {ct cptr lbl len buf p base : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.AsidPool p base) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.AsidPool p base) blk call don fp + buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.AsidPool p base) exs buffer) s res ⦄ := by + by_cases hl : lbl = fl4_abi.label.X86ASIDPoolAssign + · subst lbl + cases hEpt : Fl4Spec.startsWithEptRoot exs + · by_cases hPml4 : StartsWithPml4Root exs + · exact J.selectedNativeAsidPool hni q hPml4 buffer on rc blk call don fp + · exact J.selectedPoolBadExtra q hPml4 hEpt buffer on rc blk call don fp + · exact J.selectedEptAsidPool hni hRef hVA q hEpt buffer on rc blk call don fp + · exact J.selectedPoolIllegal q hl buffer on rc blk call don fp + +/-- Configure the selected SchedControl cap using its actual first extra +cap and arguments. [S] K1 is required at this selected route. -/ +theorem VmEptState.selectedSchedControl + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hAK : ARKeep arI AR) (hT : TimeAgree arI) + {ct cptr lbl cpu len buf : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedControl cpu) ex args exs) + (hcpu : cpu = Z.c) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedControl cpu) blk call don fp + buf ex ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.SchedControl cpu) exs buffer) s res ⦄ := + J.schedControlDecode hni hAT hAK hT lbl cpu len (cslotAddr sl) buf ct hcpu q.current args + (by simpa using q.length) q.words ex exs (ExtraOK.rel1 J.1.1.xtop.rel.1 q.extra) + (Fl4Spec.toBl cptr.bv) sl buffer on rc blk call don fp + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Disp.lean b/proofs/Fl4Proofs/P3/VmEntryS3Disp.lean new file mode 100644 index 000000000..da64bbb1c --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Disp.lean @@ -0,0 +1,110 @@ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmEntryS3TopS +import Fl4Proofs.P3.VmEntryS3Mid +import Fl4Proofs.P3.VmEntryS3Start +import Fl4Proofs.P3.VmEntryS3Orc +import Fl4Proofs.P3.VmEntryS3Yield +import Fl4Proofs.P3.VmEntryS3RecvEndpoint +import Fl4Proofs.P3.VmRecvLeaves +import Fl4Proofs.P3.VmRecvMidTop +import Fl4Proofs.P3.VmRecvS + +/-! +P3 (line mmuAD, Stage 3, 3.5): **`dispatch_syscall` at the dispatch pair of the entry from the start record** +(`StartS3.dispatchS3`): `dispatchVRS` fed by +* `DispFactsS3` = `StartS3.dispFacts` (the spec-only transport of the start record), +* `hO` = `VmEptState.orcPre`, +* `hRecv` = `handleRecvReachedV` with `RecvSenderPrefixes` = `recvSenderPrefixes`, +* `hMid` = `StartS3.midProd` (the `VMidF` producer) then `handleRecvMidV` (resume / choose) or `handleRecvS` + (pending switch, `VEAnyS` post), +* `hY` = `yieldArmV`. +No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +variable (arI AR) + +variable (MR) + +set_option maxHeartbeats 8000000 in +/-- **`dispatch_syscall` at the dispatch pair from the start record** (module docstring). -/ +theorem StartS3.dispatchS3 [P1.PlainStore st] (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hVA : ZvVirtAgree arI) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) (hRef : ArchRefines arI AR) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) (hT : TimeAgree arI) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hag : ∀ a, ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hrcb : Fl4Spec.resetChunkBits = 16) + {on rc : Fl4Spec.ObjRef → Bool} + {n : I64} {c : Fl4Spec.Syscall} {s s2 : S} {a a2 : A} {σ σb : Fl4Spec.State Unit} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + {Vb Xb : Set U64} {XLb : U64 → Nat} {Zb : XSets} [VxE Zb] {Hb : P1.Heap} {Csb Acb : Set U64} + {Lstb : U64 → List U64} {Db : Set U64} + (hon : on = v3OnCore st s) (hrc : rc = v3RemCur st s) + (St : StartS3 (L := L) (st := st) (arI := arI) n s a σ) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (Jb : VmEptState (L := L) arI Vb Xb XLb AR Zb Hb Csb Acb Lstb Db s2 a2 σb) + (hKF : KFrontB (L := L) arI s a s2 a2) (hBA : BudgetAt σ σb true) + (hMRb : MR a2 σb.machineState) (hVal : PortInValAt arI a2 σb) + {ct : U64} (hct : sched.cur_thread st s2 = ok ct) (hRun : ZvCtRun σb ct) (hn : NativeSys n c) : + syscall.dispatch_syscall st arI s2 a2 n ⦃ r => DispPostVES (L := L) arI AR on rc c σb r ⦄ := by + subst hon hrc + obtain ⟨x0, hk0, -⟩ := St.X.sb.1.2 + have hni : Entered σ := St.X.entered St.ct + obtain ⟨-, ek, ect, -, -, eq⟩ := BudgetAt.shape27 hnd hk0 hBA + have hnib : Entered σb := BudgetAt.entered hnd hk0 hni hBA + obtain ⟨hvfh, hnf⟩ := SInvAll.sinvB_dispXV (x' := Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)) + St.X.sb St.ct hk0 rfl rfl ek ect + have hnqb : ∀ d p, σb.curThread ∉ σb.readyQueues d p := by rw [ect, eq]; exact St.X.ctQ + have hvfhb : ∀ x', σb.kheap σb.curThread = some (.tcb x') → + Fl4Spec.validFaultHandler x'.tcbFaultHandler := hvfh + have F := St.dispFacts arI AR MR hnd J.1.1 Jb.1.1 hKF hBA hMRb + refine Jb.dispatchVRS arI AR MR hARI hAT hVA hSy hCap hKDAR hKD hRef hnd hM hMK hPA hIA hT hmrt hmb hmax htu + hag hfan hrcb hnib hVal F hct hRun hnqb hvfhb hn ?_ ?_ ?_ ?_ + · intro call bl don fp cptr _ y hy hyE s1 a1 hP + exact Jb.orcPre hARI.toARKeep hAT hnd hM hPA F.orc call bl don fp cptr hy hyE hP + · intro bl cr _ + refine VEAny_toS (Jb.handleRecvReachedV hAT hnib hct hnqb hvfhb bl cr ?_) + intro e b sd g gr rc rc' _ _ _ _ + exact recvSenderPrefixes (arI := arI) (AR := AR) hAT hmax htu Hb Csb s2 a2 σb ct e b sd g gr bl rc rc' + · intro rg sr cr hm cptr y hy hok s1 a1 hV + obtain ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', hvx, J'⟩ := hV + haveI := hvx + have hF := St.midProd hnd J.1.1 Jb.1.1 hBA cptr y hy hok + obtain ⟨ct1, hct1, -⟩ := spec_exists (sched.cur_thread.spec' (L := L) s1) + have hSnd : ∀ e b sd g gr rc rc', + (∃ x, y.2.kheap e.bv = some (.endpoint x)) → + ((rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, y.2.kheap q.bv = some (.reply x))) → + JHeld (L := L) Z'.th s1 (.Endpoint e b sd true g gr) → + (∀ q g', rc = .Reply q g' → JHeld (L := L) Z'.th s1 rc) → + RecvSenderPrefixes (L := L) arI AR H' Cs' s1 a1 y.2 ct1 e b sd g gr true rc rc' := + fun e b sd g gr rc rc' _ _ _ _ => + recvSenderPrefixes (arI := arI) (AR := AR) hAT hmax htu H' Cs' s1 a1 y.2 ct1 e b sd g gr true rc rc' + cases hsa : y.2.schedulerAction with + | resumeCurThread => + exact VEAny_toS (J'.handleRecvMidV hAT hmb hF (Or.inl hsa) hct1 true cr hSnd) + | chooseNewThread => + exact VEAny_toS (J'.handleRecvMidV hAT hmb hF (Or.inr hsa) hct1 true cr hSnd) + | switchThread u => + exact J'.handleRecvS hAT hF ⟨u, hsa⟩ hct1 true cr hSnd + · intro hy + exact VEAny_toS (yieldArmV hAT hnd hmrt hmb hni hk0 hBA (Jb.anyX arI)) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Dispatch.lean b/proofs/Fl4Proofs/P3/VmEntryS3Dispatch.lean new file mode 100644 index 000000000..d12317e9b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Dispatch.lean @@ -0,0 +1,111 @@ +import Fl4Proofs.P3.VmInvAfterX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmEntryS3Decode + +/-! +The reached VM invocation decoder receives the successful lookup equation +for the invoked slot and capability. It applies to ordinary invocations +and to the first phase of the three receive continuations. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) +variable (MR : A → Fl4Spec.MachineState → Prop) + +/-- Decode the cap actually selected for this invocation. -/ +def VDispReachAtFor (scope : U64 → capability.Cap → Prop) (on rc : Fl4Spec.ObjRef → Bool) (Z : XSets) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (ct : U64) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) : Prop := + ∀ (info : msginfo.MessageInfo) (mi : Fl4Spec.MessageInfo) (buf : U64) (buffer : Option Fl4Spec.ObjRef) + (r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault) + (er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))) (args : List Fl4Spec.Data), + msg.msg_info st s ct = ok info → Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False) → + msg.lookup_ipc_buffer st s false ct = ok buf → + Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False) → + ipc.lookup_extra_caps st s ct buf info = ok r2 → + (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False) → + Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False) → + ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) → + capAt (L.mem s) (cslotAddr sl) = ok c → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → LookAt ct cptr σ sl c → r2 = .Ok e → er = .ok exs → scope info.label c → + syscall.decode_invocation st arI s ar info.label (msgLen info buf) (cslotAddr sl) c blocking is_call + can_donate first_phase buf e ⦃ res => + let m := specDecV first_phase mi.miLabel args (Fl4Spec.toBl cptr.bv) sl c exs buffer + VDispPostVEX (L := L) arI AR MR on rc σ blocking is_call can_donate m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blocking is_call can_donate m ct s res ⦄ + +/-- All caps returned by lookup. -/ +abbrev VDispReachAt := VDispReachAtFor (L := L) arI AR MR (fun _ _ => True) + +/-- An EPT page-map label takes the EPT decoder independently of the +mapping fields in a selected frame cap. -/ +def FrameMapEptAt (lbl : U64) : capability.Cap → Prop + | .Frame .. => lbl = fl4_abi.label.X86PageMapEPT + | _ => False + +/-- The native decoder branches remaining after the VM-side split. -/ +abbrev VDispReachNativeAt := VDispReachAtFor (L := L) arI AR MR + (fun lbl c => NativeCap c ∧ ¬ FrameMapEptAt lbl c) + +set_option maxHeartbeats 4000000 in +/-- Assemble the VM branches and the selected native decoder branches. +A Frame at `X86PageMapEPT` is handled by the EPT decoder before the +native-cap split, including an unmapped Frame. -/ +theorem VmEptState.dispatchReached [PlainStore st] + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hK : ARKeep arI AR) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hVal : PortInValAt arI a σ) + (on rc : Fl4Spec.ObjRef → Bool) {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (call blk don fp : Bool) (cptr : U64) + (hNative : VDispReachNativeAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr) : + VDispReachAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr := by + intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hlook hr he _ + by_cases hMap : FrameMapEptAt info.label c + · cases c <;> try exact hMap.elim + case Frame ptr sz rights dev m ep asid va => + have hLabel : info.label = fl4_abi.label.X86PageMapEPT := hMap + obtain ⟨hlbl, q⟩ := J.1.1.selectedDecode hct hinfo hmi hbuf hbufS hr2 her hmrs hsl hc hlook hr he + rw [← hlbl, hLabel] + exact WP.spec_mono (J.selectedFrameMapEpt hni hRef hVA q buffer on rc blk call don fp) fun _ h => Or.inl h + · by_cases hN : NativeCap c + · exact hNative info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs + hlook hr he ⟨hN, hMap⟩ + · exact (J.dispatchAtNonNativeX arI AR MR hni hRef hVA hK hSy hCap hAT hAM hVal on rc hct hRun call blk don fp cptr) + info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he hN + +variable {arI AR MR} + +/-- Pass the successful lookup to the reached decoder. +[J] Entry/current-thread facts and the decoder's selected-cap post. +[P] The invocation's machine and scheduler agreements. -/ +theorem VmEptState.handleInvocationReached (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hM : MachPlatC arI MR) (hPA : PPPlatA arI AR) (on rc : Fl4Spec.ObjRef → Bool) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) + (hD : VDispReachAt (L := L) arI AR MR on rc Z s ar σ ct is_call blocking can_donate first_phase cptr) : + syscall.handle_invocation st arI s ar is_call blocking can_donate first_phase cptr ⦃ res => + HIPostVEX (L := L) arI AR MR on rc σ is_call blocking can_donate first_phase cptr res ⦄ := by + have hB := JAtZB.xferH (J := J.1.1) + obtain ⟨info, mi, buf, buffer, r2, er, argsU, h1, h2, h3, h4, h5, h6, h7⟩ := + JAtZB.frontEx arI J.1.1 hB.1 hB.2.1 hct + exact J.handleInvocationAtLookupX arI hAK hA hnd hM hPA on rc hni hct hnq hvfh is_call blocking can_donate first_phase + cptr info h1 mi h2 buf h3 buffer h4 r2 h5 er h6 _ h7 + (fun sl c e exs hsl hc hsc hlook h2e hre => hD info mi buf buffer r2 er _ h1 h2 h3 h4 h5 h6 h7 sl c e exs hsl hc hsc hlook h2e hre trivial) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Domain.lean b/proofs/Fl4Proofs/P3/VmEntryS3Domain.lean new file mode 100644 index 000000000..ec138e5be --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Domain.lean @@ -0,0 +1,188 @@ +import Fl4Proofs.P3.DomSched +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.XTopJointCopyRegs +import Fl4Proofs.P3.JEntryKs + +/-! The Domain decoder at the actual selected capability. The shared Domain +trace records only restart and scheduler steps. Replay preserves the VM state +using the paired native footprints. [P] The spec uses one domain. -/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Endp Ipc +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} + +section +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs : Set U64} {a : A} + +private theorem JAtZB.domainSchedHead {Ac D : Set U64} {Lst : U64 → List U64} + {s : S} {σ : Fl4Spec.State Unit} (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hw : SchW (L := L) Z.T Z.c s b) : b ≠ P1.vHeadA st s := by + intro e + subst b + have hhead : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val B.kern.ksbFit + rcases hw with ⟨t, ht, hw⟩ | ⟨hb, -, -⟩ | ⟨k, hk, hv⟩ + · have hd := B.kern.hks t _ (B.zH.1 t ht) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at hd + have hf := tfield3_val (B.ip.e.tcb t ht) hw + rw [hhead] at hf + rcases hf with h | h | h <;> omega + · have hbase : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + omega + · simp only [List.mem_cons, List.not_mem_nil, or_false] at hk + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omega + +/-- Replay only the selected Domain steps through the VM-compatible shared +base. The native footprint preserves the VM extensions at each actual pair. -/ +theorem DomainTrace.replay {s s' : S} {σ σ' : Fl4Spec.State Unit} + (tr : DomainTrace (L := L) Z.th s σ s' σ') : + ∀ {Ac D : Set U64} {Lst : U64 → List U64}, + JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ → + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' := by + induction tr with + | @restart s s1 σ σ1 ct x hcur hk ht hs hm hr => + intro Ac D Lst B + have hct : sched.cur_thread st s = ok ct := by + have hc := P1.cur_thread_ok (L := L) s + have he := (B.curEq hc).1 + rwa [P1.eq_of_bv (he.symm.trans hcur)] at hc + have hrun := hr x (by rw [hcur]; exact hk) + obtain ⟨sr, σr, hm', -, hs', Ac', Lst', D', B', F⟩ := B.restartRunJ hct hk hrun + have es : s1 = sr := ok_inj (hs.symm.trans hs') + have em : σ1 = σr := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hm.symm.trans hm')))).2 + subst s1 + subst σ1 + exact ⟨Ac', Lst', D', B', F⟩ + | sched h => + intro Ac D Lst B + have hX := B.xtop + have hw := (h.2.1 Z.N Z.T Z.C Z.c B.thT hX.fin.1.1.1 hX.fin.1.1.2.2.1).2.1 + obtain ⟨Ac', Lst', D', B'⟩ := B.schedJ h + exact ⟨Ac', Lst', D', B', NatFoot.ofW hX hw + (fun _ hb => schW_oobj hX.fin.1.1.1.placed.tcb hb) + (fun _ hb => B.schW_nv hb) (fun _ hb => B.domainSchedHead hb) (NatK.of_kheap h.1.kh)⟩ + | trans _ _ hi hj => + intro Ac D Lst B + obtain ⟨_, _, _, B1, F1⟩ := hi B + obtain ⟨Ac', Lst', D', B2, F2⟩ := hj B1 + exact ⟨Ac', Lst', D', B2, B.natTrans F1 F2⟩ + +variable {Ac D V X : Set U64} {Lst : U64 → List U64} {XL : U64 → Nat} +variable {s : S} {σ : Fl4Spec.State Unit} + +private theorem JAtZB.domainWordsOut (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ o : U64, o.val < 256 → o.val % 8 = 0 → OutsideObjects Z.th (ksW (L.ksb s) o) := by + intro o ho h8 + have hf := B.kern.ksbFit + have hal := B.kern.ksbAl + have hv : (ksW (L.ksb s) o).val = (L.ksb s).val + o.val := ksW_val (by omega) + refine ⟨?_, fun p k hp => ?_⟩ + · change (ksW (L.ksb s) o).val % 8 = 0 + change (L.ksb s).val % 8 = 0 at hal + rw [hv] + omega + · have hout : p.val + objSize k ≤ (L.ksb s).val ∨ (L.ksb s).val + 256 ≤ p.val := by + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hIA := B.xtop.rel.2.2.2.2.2.1 + rcases B.thk p k hp with rfl | ⟨r, rfl⟩ + · have h := B.kern.hks p .tcb ((hJ.tcb p).1 hp) (fun z h => by cases h) + simp only [P1.HKind.bits] at h + simp only [objSize] + omega + · by_contra hc + simp only [objSize, not_or, not_le] at hc + obtain ⟨h1, h2⟩ := hc + by_cases hle : p.val ≤ (L.ksb s).val + · have hi : ((L.ksb s).val - p.val) / 32 < 2 ^ r := by omega + have hAt : IsSlotAt Z.th p (((L.ksb s).val - p.val) / 32) := Or.inl ⟨r, hp, hi⟩ + have hval := hAt.val B.xtop.rel.1 + exact hIA.ks (L.ksb s) le_rfl (by omega) (Or.inl (by omega)) _ + ⟨p, _, hAt, rfl⟩ ⟨by omega, by omega⟩ + · have hAt : IsSlotAt Z.th p 0 := Or.inl ⟨r, hp, by positivity⟩ + have hval := hAt.val B.xtop.rel.1 + exact hIA.ks (slotOf p 0) (by omega) (by omega) (Or.inl (by omega)) _ + ⟨p, _, hAt, rfl⟩ ⟨le_rfl, by omega⟩ + rw [hv] + rcases hout with h | h <;> omega + +private theorem JAtZB.domainActionOut (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ o : U64, o.val < 256 → (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val ≠ (ksW (L.ksb s) o).val := by + intro o ho h + have ha : layout.node.SCHED_ACTION.val = 16 := P1.act_val + have hf := B.kern.ksbFit + have hn : (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val = + (L.ksb s).val + 13568 + (L.cpu s).val * 4288 + layout.node.SCHED_ACTION.val := + Sched.nodeAt_val' B.kern.nodeFit (by omega) + rw [hn, ha, ksW_val (by omega)] at h + omega + +set_option maxHeartbeats 8000000 in +/-- The Domain branch at the actual selected slot. First phase is rejected +before a write. All three normal labels use the paired domain steps. -/ +theorem VmEptState.selectedDomainS3 {MR : A → Fl4Spec.MachineState → Prop} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnd : Fl4Spec.numDomains = 1) + {ct cptr len buf lbl : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl .Domain ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) .Domain blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .Domain exs buffer) s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases fp + case true => + simp only [↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + exact run_liftME_err rfl + case false => + simp only [Bool.false_eq_true, ↓reduceIte] + let words : List U64 := args.map fun x => ⟨x⟩ + have ew : words.map UScalar.bv = args := zv_map_mk args + obtain ⟨label, hlabel, hDecode⟩ := dom_canon lbl + have hd : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl .Domain exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeDomain + (Fl4Spec.decodeDomainInvocation (Z := Unit) label (words.map UScalar.bv) exs)) := by + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + simp only [Fl4Spec.fl4DecodeInvocation, absCap, ew, hDecode, Bool.false_eq_true, ↓reduceIte] + rw [hd] + have B := J.1.1 + have hX := B.xtop + have hC := B.curRun.run hni + obtain ⟨hcur, -, -⟩ := B.curEq q.current + have hAct := B.inv.actReady + have hex := ExtraOK.relS hX.rel.1 q.extra + refine WP.spec_mono (domain_decode_trace arI ⟨hnd⟩ Z.th hX.rel.1 s a σ Z.G hX.rel.2.1 + hX.rel.2.2.1 hX.rel.2.2.2.1 ct q.current hcur (hX.curTcb hcur).1 B.kern.act B.kern.ksbFit + B.kern.ksbAl B.domainWordsOut B.domainActionOut lbl label hlabel len buf words q.length q.words ex _ + (exAt_idx0 ex) exs hex (domSetTraceHyp_of hX B.thT hC hAct hcur) + (domResTraceHyp_of hX B.ks B.thT hC hAct hcur) + (domStallHyp_of arI hX.fin.1.1.1 B.thT)) ?_ + rintro ⟨r, s', a'⟩ ⟨ha, hp⟩ + subst a' + rcases hp with ⟨e, hr, hs, e', hErr, hRel⟩ | ⟨hr, i, hi, σ1, h1, y, hy, -, -, -, -, -, ht⟩ + · dsimp only at hs + subst s' + exact Or.inl ⟨e, hr, rfl, ⟨e', run_liftME_err (run_liftME_err hErr), Or.inl hRel⟩, J.anyX arI⟩ + · obtain ⟨_, _, _, B', F⟩ := (ht hC).replay B + refine Or.inr ⟨.base (.invokeDomain i), (DecOut.of_eq hi).liftME.liftME, σ1, h1, + (.ok [], y.2), ?_, Or.inl ⟨hr, rfl, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩⟩ + change ((.ok [], y.2) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (do Fl4Spec.invokeDomain (Z := Unit) i; return ([] : List Fl4Spec.Data))).run σ1).1 + exact mem_liftE (x := ([], y.2)) (mem_bind_nd hy rfl) + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Entry.lean b/proofs/Fl4Proofs/P3/VmEntryS3Entry.lean new file mode 100644 index 000000000..0127e2009 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Entry.lean @@ -0,0 +1,253 @@ +import Fl4Proofs.P3.VmEntryS3Disp +import Fl4Proofs.P3.VmEntryS3Budget +import Fl4Proofs.P3.VmEntryS3Fast +import Fl4Proofs.P3.VmRecvFin +import Fl4Proofs.P3.VmKEntry + +/-! +P3 (line mmuAD, Stage 3, 3.5): **`VmEptState.kernel_entryS3`**, the syscall entry from the start record instead of +`KEntryArmsV` (sibling of `VmEptState.kernel_entry`; same skeleton). + +* budget: `VmEptState.entryBudget` with `budget_ok_mr`; +* dispatch: `StartS3.dispatchS3` (`dispatchVRS` over the reached decode; `DispPostVES`); +* the slow path (dispatch, finish) is proved first; the `SysReplyRecv` fast path then declines (state unchanged) + or is accepted: `VEAnyX.replyRecvAcceptedPost` re-uses the slow-path post; +* finish: `VEAnyS.finish` with `FinishFactsS3` from `StartS3.finFacts`. + +No new named premise; `[P]` `FpuImplDet`, `FpuWriteDet` (the existing finish platform facts), +`P1.StoreExt` (the existing fast-path equality premise), `MRKeepD`, `hag`, `hfan`, `hrcb` (the existing dispatch +premises of `dispatchVR`). +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +open Fl4Proofs.P3.SInvR.G4 + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- The budget guard after the `FaultIP` write, with the explicit `Z.mv` of `budgetFramesV` kept (so `[VxE Z]` +survives to the dispatch state). -/ +theorem VmEptState.entryBudgetF + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s sf : S} {a : A} {σ σf : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {Vf Xf : Set U64} {XLf : U64 → Nat} {Zf : XSets} {Hf : P1.Heap} {Csf Acf : Set U64} + {Lstf : U64 → List U64} {Df : Set U64} + (Jf : VmEptState (L := L) arI Vf Xf XLf AR Zf Hf Csf Acf Lstf Df sf a σf) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) + (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hf : ((), σf) ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1) : + syscall.budget_ok st arI sf a ⦃ r => + ∃ σb, (r.1, σb) ∈ (specBudget σf).1 ∧ + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI Vf Xf XLf AR (Zf.mv Zf.G V') Hf Csf Ac' Lst' D' r.2.1 r.2.2 σb ⦄ := by + have hct := cur_thread_eq (L := L) s + let ct := L.curW s + change sched.cur_thread st s = ok ct at hct + have hcur := (J.1.1.curEq hct).1 + have ht := TopSR.cur_mem J.1.1.xtop.fin.1 hcur.symm + obtain ⟨x, hk, _⟩ := (J.1.1.xtop.irelK J.1.1.ks J.1.1.buf).1.tcb ct ht + have hk' : σ.kheap σ.curThread = some (.tcb x) := by rwa [hcur] + rw [fl4SyscallFaultIP_eq hk'] at hf + have e : σf = faultIPSt σ σ.curThread x := congrArg Prod.snd (Set.mem_singleton_iff.mp hf) + subst σf + exact WP.spec_mono (Jf.budgetFramesV hni hnq hM hMR hVM + (SInvR.G4.offReady_faultIPSt (t := σ.curThread) (x := x) hOff) hK hAT hnd hmrt hmb hmax htu) + fun _ ⟨σb, hb, V', Ac', Lst', D', Jb, _⟩ => ⟨σb, hb, V', Ac', Lst', D', Jb⟩ + +variable (arI AR MR) + +set_option maxHeartbeats 8000000 in +/-- **The exit from every finish state of the entry** (from `StartS3.finFacts`; module docstring). -/ +theorem StartS3.finStep (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hFpu : Sched.FpuImplDet) (hFW : FpuWriteDet) + {on rc : Fl4Spec.ObjRef → Bool} {n : I64} {c : Fl4Spec.Syscall} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + (St : StartS3 (L := L) (st := st) (arI := arI) n s a σ) + (hon : on = v3OnCore st s) (hrc : rc = v3RemCur st s) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + ∀ xo ∈ (specFrontVF on rc c σ).1, ∀ (s' : S) (a' : A), + KFrontF (L := L) arI n s a s' a' → VEAnyS (L := L) arI AR s' a' xo.2 → + syscall.finish st arI s' a' ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) on rc true c σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + intro xo hxo s' a' hKF hV + have F := St.finFacts arI AR hnd hon hrc J xo hxo s' a' hKF hV + have h1 : syscall.finish st arI s' a' ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelExitVF (Z := Unit) on rc xo.2).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := + VEAnyS.finish hARI hAT hVA hnd hT hRef hDL hFpu hFW hmb hmax htu hV F + refine WP.spec_mono h1 ?_ + rintro r ⟨σ', hmem, J2⟩ + exact ⟨σ', mem_kentryVF hxo hmem, J2⟩ + +set_option maxHeartbeats 8000000 in +/-- **FL4's syscall entry at `VmEptState` from the start record** (module docstring). -/ +theorem VmEptState.kernel_entryS3 [P1.PlainStore st] (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hVA : ZvVirtAgree arI) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hPI : PortInValMR arI MR) (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hag : ∀ a, ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hrcb : Fl4Spec.resetChunkBits = 16) + (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hFpu : Sched.FpuImplDet) (hFW : FpuWriteDet) (hStore : P1.StoreExt st) + {on rc : Fl4Spec.ObjRef → Bool} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (hon : on = v3OnCore st s) (hrc : rc = v3RemCur st s) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hMR : MR a σ.machineState) + (hRun : ∀ ct, sched.cur_thread st s = ok ct → ZvCtRun σ ct) + {n : I64} {c : Fl4Spec.Syscall} (hn : NativeSys n c) + (hflag : ∀ ct v, sched.cur_thread st s = ok ct → obj.Tcb.flags st ct (L.wr s (regA ct layout.reg.FAULT_IP) v) + ⦃ f => (f &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 ⦄) + (St : StartS3 (L := L) (st := st) (arI := arI) n s a σ) : + syscall.handle_syscall st arI s a n ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) on rc true c σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + have hAK := hARI.toARKeep + have hni : Entered σ := St.X.entered St.ct + have hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p := St.X.ctQ + have hct := cur_thread_eq (L := L) s + generalize L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = ct at hct + have B := J.1.1 + obtain ⟨hcur, -⟩ := B.curEq hct + have hctT : ct ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨x, hk, -⟩ := hI.1.tcb ct hctT + have hxs := hI.1.toE.at_tcb hctT hk + have hk' : σ.kheap σ.curThread = some (.tcb x) := by rw [hcur]; exact hk + have hF : ((), faultIPSt σ σ.curThread x) ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1 := by + rw [fl4SyscallFaultIP_eq hk']; exact Set.mem_singleton _ + have fin := St.finStep arI AR hARI hAT hVA hnd hmb hmax htu hT hRef hDL hFpu hFW hon hrc J (c := c) + have hS : syscall.syscall st arI s a n true ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) on rc true c σ).1 ∧ + VEAnyX (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := by + unfold syscall.syscall + rw [hct]; simp only [bind_ok] + obtain ⟨nx, hnx, hnxv⟩ := spec_exists (Endp.obj.Tcb.reg.spec (L := L) s ct layout.reg.NEXT_IP) + rw [hnx]; simp only [lift, bind_ok] + obtain ⟨s1, hs1, rfl⟩ := spec_exists (Endp.obj.Tcb.set_reg.spec (L := L) s ct layout.reg.FAULT_IP + (core.num.U64.wrapping_sub nx 2#u64)) + rw [hs1]; simp only [bind_ok] + generalize hv : core.num.U64.wrapping_sub nx 2#u64 = v + have hvb : v.bv = x.tcbArch.tcbContext.userRegs .nextIP - 2 := by + rw [← hv, core.num.U64.wrapping_sub_bv_eq, hnxv, ← hxs.regs .nextIP, regIdx_nextIP]; rfl + have hX2 := reg_wr_irel B.ip hI hctT hk Fl4Spec.Register.faultIP (by decide) v + rw [regIdx_faultIP] at hX2 + obtain ⟨V0, B0, F0⟩ := B.regWrJ hctT hk Fl4Spec.Register.faultIP v (by rw [regIdx_faultIP]; exact hX2) + rw [regIdx_faultIP] at B0 F0 + have J0 := J.ofNat B0 F0 + rw [hvb, ← hcur] at J0 + obtain ⟨f, hf, hf0⟩ := spec_exists (hflag ct v hct) + rw [hf]; simp only [bind_ok, hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + simp only [if_neg hn.range.1, if_neg hn.range.2] + obtain ⟨⟨b, s2, a2⟩, hbe, ⟨σb, hb, V2, Ac2, Lst2, D2, Jb⟩, hMRb⟩ := spec_exists + (WP.spec_and (J.entryBudgetF J0 hni hnq hM hMR St.X.sb.1.1.vmt St.X.offReady hAK hAT hnd hmrt hmb hmax htu hF) + (budget_ok_mr hM hMK hAT hMR _)) + rw [hbe]; simp only [bind_ok] + cases b + · -- the budget check stops the system call + try simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (fin _ (mem_frontVF_stop hF hb) s2 a2 + ⟨ct, v, false, s2, a2, hct, hbe, Or.inl ⟨rfl, rfl, rfl⟩⟩ (Or.inl (Jb.anyX arI))) ?_ + rintro ⟨s3, a3⟩ h + exact (WP.spec_ok _).2 h + -- the system call runs + have hBA : BudgetAt σ σb true := ⟨_, hF, hb⟩ + have hfr : KFrontB (L := L) arI s a s2 a2 := ⟨ct, v, hct, hbe⟩ + obtain ⟨⟨σu, hu, hσu⟩, hkb, hcb, -, -, hrqb⟩ := BudgetAt.shape27 hnd hk' hBA + have hms : σb.machineState = gctState σ.machineState := by + rw [Sched.updateTimeStamp_eq' hnd (doMachineOp_single (getCurrentTime_eq (faultIPSt σ σ.curThread x).machineState))] at hu + have he := congrArg Prod.snd (Set.mem_singleton_iff.1 hu) + dsimp only at he + rw [hσu, he] + rfl + have hPort := hPI.at (hms.symm ▸ hMRb) + have hMRb' : MR a2 σb.machineState := hms.symm ▸ hMRb + have hct2 := cur_thread_eq (L := L) s2 + generalize L.mem s2 (L.nodeAt s2 (L.cpu s2) layout.node.CUR_THREAD) = ct2 at hct2 + have hRunB : ZvCtRun σb ct2 := by + obtain ⟨hcur2, -, -⟩ := Jb.1.1.curEq hct2 + intro xb hxb + rw [← hcur2, hcb, hkb] at hxb + simp only [faultIPSt, updK, Function.update_self, Option.some.injEq, + Fl4Spec.KernelObject.tcb.injEq] at hxb + subst xb + exact hRun ct hct x hk + have disp := St.dispatchS3 arI AR MR hARI hAT hVA hSy hCap hKDAR hKD hRef hnd hM hMK hPA hIA hT hmrt hmb hmax htu + hag hfan hrcb hon hrc J Jb hfr hBA hMRb' hPort hct2 hRunB hn + have slowE : ∃ (s3 : S) (a3 : A), syscall.dispatch_syscall st arI s2 a2 n = ok (s3, a3) ∧ + syscall.finish st arI s3 a3 ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) on rc true c σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + obtain ⟨⟨s3, a3⟩, hde, y, hy, hP⟩ := spec_exists disp + refine ⟨s3, a3, hde, ?_⟩ + have hKF : KFrontF (L := L) arI n s a s3 a3 := ⟨ct, v, true, s2, a2, hct, hbe, Or.inr ⟨rfl, hde⟩⟩ + rcases hP with ⟨hy1, hV⟩ | ⟨hy1, z, hz, hV⟩ + · exact fin _ (mem_frontVF_ok hF hb hy hy1) s3 a3 hKF hV + · exact fin _ (mem_frontVF_err hF hb hy hy1 hz) s3 a3 hKF hV + have dispFin : (do + let (s3, a3) ← syscall.dispatch_syscall st arI s2 a2 n + let (s4, a4) ← syscall.finish st arI s3 a3 + ok (false, s4, a4)) ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) on rc true c σ).1 ∧ + VEAnyX (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := by + obtain ⟨s3, a3, hde, hf⟩ := slowE + rw [hde]; simp only [bind_ok] + refine WP.spec_bind hf ?_ + rintro ⟨s4, a4⟩ h + exact (WP.spec_ok _).2 h + change (if n = fl4_abi.syscall.SysReplyRecv then _ else _) ⦃ _ ⦄ + by_cases hrr : n = fl4_abi.syscall.SysReplyRecv + · rw [if_pos hrr] + obtain ⟨⟨b1, s3, a3⟩, hfe, -⟩ := P1.spec_ex_fd (P0.fastpath.reply_recv.p0 st arI s2 a2 L.total hAT) + have hdec := replyRecvDecline (L := L) hAT s2 a2 + rw [hfe, WP.spec_ok] at hdec + rw [hfe]; simp only [bind_ok] + cases b1 + · have e : (s3, a3) = (s2, a2) := hdec rfl + simp only [Prod.mk.injEq] at e + obtain ⟨rfl, rfl⟩ := e + exact dispFin + · refine (WP.spec_ok _).2 ?_ + obtain ⟨s5, a5, hde, hf⟩ := slowE + refine VEAnyX.replyRecvAcceptedPost (Q := fun r : S × A => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) on rc true c σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ') (Jb.anyX arI) hStore hAT hnd hk' St.rv hBA hfe ?_ + subst hrr + rw [hde]; simp only [bind_ok] + exact hf + · rw [if_neg hrr] + exact dispFin + unfold syscall.handle_syscall + refine WP.spec_bind hS ?_ + rintro ⟨b0, s', a'⟩ h + exact (WP.spec_ok _).2 h + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Fast.lean b/proofs/Fl4Proofs/P3/VmEntryS3Fast.lean new file mode 100644 index 000000000..0b7283a63 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Fast.lean @@ -0,0 +1,182 @@ +/- +The accepted in-gate ReplyRecv at the actual passing-budget dispatch state. + +The shared joint relation supplies the fast-path read bundle. The only extra +entry invariant is [J] `SInvR.RV`, the refill-sum bound. [P] architecture +totality, store extensionality and the one-domain configuration are the +existing concrete fast-path and passing-budget premises. +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.XTopJointKEntryRRG + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + +private theorem JAtZB.curW_tcb {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (et : sched.cur_thread st s = ok t) : Z.th t = some .tcb := + (J.xtop.curTcb (J.curEq et).1).1 + +/-- **The receive-side cap typing at a `JAtZB` state** (what P1's +`EntryHyps0.recvOK` gave): every cap the current thread looks up is in a slot +of `Z.th` (`lookup_cap_held`), typed by `JPlaced.typed`. -/ +private theorem JAtZB.lkTyped {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (cptr : U64) : cspace.lookup_cap st s (L.curW s) cptr ⦃ r => ∀ c, r = .Ok c → + P1.CapTyped H (P1.Heap.replies H) (P1.Heap.ntfns H) c ⦄ := by + have htcb := J.curW_tcb (cur_thread_eq (L := L) s) + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + refine WP.spec_mono (lookup_cap_held Z.th s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 _ htcb cptr) ?_ + intro r hr c hc + obtain ⟨y, hy, hcv⟩ := hr c hc + rw [← hcv]; exact hJ.typed y hy + +/-- **`RRSum` at a `JAtZB` state from `RV`** (m66's bridge): a sporadic SC of +`H` is one of `Z.C` (`hscC`), related to a spec SC with its refills +(`ScRelW.strongS`), active (`ScRelW.active_of_rmax`), so `RV`'s +`rsum < 2 ^ 64` is its ring sum. -/ +private theorem JAtZB.rrSum {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hRV : SInvR.RV σ) : P1.RRSum (L := L) (P1.Heap.scs H) s := by + intro c ⟨z, hz⟩ hsp + have hcC := J.hscC c z hz + obtain ⟨Q, R, -, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨y, n, hk, hW⟩ := hs.scs c hcC + have hsp' : L.mem s (scW c 64#u64) ≠ 0#u64 := by + have e : P1.sporA c = scW c 64#u64 := by unfold P1.sporA scW; simp [layout.sc.SPORADIC] + rw [← e]; exact hsp + have h1 := hW.strongS hsp' + have ha := hW.active_of_rmax (hW.spor hsp') + have hr := (hRV _ y n hk ha).2.2.1 + rw [h1.refills, SInvR.rsum_map] at hr + rw [amtSum_eq_lsum]; exact hr + +/-- The words of a CNode slot of `Z.th` are cap-slot words for P1 +(`CapSW`): in `Ω` (an `other` object of `H`), so outside the TCBs and nodes, +and outside the frame words. -/ +private theorem JAtZB.capSW {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {p : U64} {n i : Nat} (hth : Z.th p = some (.cnode n)) (hi : i < 2 ^ n) {b : U64} + (hb : b = slotOf p i ∨ b = core.num.U64.wrapping_add (slotOf p i) 8#u64) : + P1.CapSW (L := L) (P1.Heap.tcbs H) Cs (P1.Heap.frame H) s b := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hqi : IsSlotAt Z.th p i := Or.inl ⟨n, hth, hi⟩ + have hv := hqi.val hJ.pl + have hal := hqi.aligned hJ.pl + have hp := hJ.cnode p n hth + have hpa := hJ.heap.align p _ hp + have hpf := hJ.heap.fits p _ hp + simp only [P1.HKind.bits] at hpa hpf + have e32 : 2 ^ (n + 5) = 32 * 2 ^ n := by rw [pow_add]; ring + have hi1 : 32 * (i + 1) ≤ 2 ^ (n + 5) := by rw [e32]; have : i + 1 ≤ 2 ^ n := hi; omega + have hbv : b.val % 8 = 0 ∧ p.val ≤ b.val ∧ b.val < p.val + 2 ^ (n + 5) := by + rcases hb with rfl | rfl + · refine ⟨by omega, by omega, by omega⟩ + · have e : (core.num.U64.wrapping_add (slotOf p i) 8#u64).val = (slotOf p i).val + 8 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + rw [e]; refine ⟨by omega, by omega, by omega⟩ + have hΩ : b ∈ P1.Heap.omega H := ⟨hbv.1, p, _, hp, Or.inr (Or.inr (Or.inl ⟨_, rfl⟩)), hbv.2.1, hbv.2.2⟩ + obtain ⟨⟨Q, R, h0⟩, -⟩ := J.blk + refine ⟨h0.omega b hΩ, fun hf => ?_⟩ + obtain ⟨-, q, k, hq, hk, hin1, hin2⟩ := hf + have hpq : q ≠ p := fun e => by + subst e; rw [hp] at hq; cases hq + rcases hk with h' | ⟨z', h'⟩ | ⟨h', -⟩ <;> cases h' + obtain ⟨-, hb1, hb2⟩ := hbv + have hd := hJ.heap.disj q k p _ hq hp hpq + rw [show (P1.HKind.other (n + 5)).bits = n + 5 from rfl] at hd + rcases hd with e | e + · omega + · omega + +/-- **`CnOff` at a `JAtZB` state** (m68's [J] `CnOffAt`, derived): the CNode +caps reachable from the current thread's root are CNodes of `Z.th` with their +radix (`SlotsValid`, `StateRel.ctable`), whose indexed slots +`x &&& mask r < 2 ^ r` are slots of `Z.th` (`JAtZB.capSW`). -/ +private theorem JAtZB.cnOff {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (et : sched.cur_thread st s = ok t) : + P1.CnOff (L := L) (P1.Heap.tcbs H) Cs (P1.Heap.frame H) s t := by + have htcb := J.curW_tcb et + have hR := J.xtop.rel.2.1.1 + have hV := J.xtop.rel.2.1.2.1 + intro sl root hsl hroot + obtain ⟨tcb, c, -, hc, -, hval⟩ := hR.ctable hV htcb + have hsl' := tcb_slot_spec t layout.tcb.SLOT_CTABLE + rw [hsl, WP.spec_ok] at hsl' + rw [hsl', show (layout.tcb.SLOT_CTABLE.bv : BitVec 64) = 0#64 by simp [layout.tcb.SLOT_CTABLE], + slot_cap_eq, hc] at hroot + obtain rfl := P1.ok_inj_ff hroot + refine ⟨fun p r => Z.th p = some (.cnode r.val), fun p r gs g e => ?_, fun p r m x p' r' gs' g' hC hm e => ?_, + fun p r m x hC hm => ?_⟩ + · subst e; exact hval.1 + · rw [cnSlot_eq, slot_cap_eq] at e + exact (hV.cnode p r.val hC _ (andMask_lt_pow hm x) _ e).1 + · have hi := andMask_lt_pow hm x + rw [cnSlot_eq] + exact ⟨J.capSW hC hi (Or.inl rfl), J.capSW hC hi (Or.inr rfl)⟩ + +/-- The native fast-path reads follow from the joint base at the dispatch state. +[J] `SInvR.RV` bounds the refill sum. [P] architecture totality supplies +the concrete switch-tail facts. -/ +theorem JAtZB.replyRecvPre {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hRV : SInvR.RV σ) : P1.RRPre (L := L) arI s a := by + have hB := J.blk.bxi + have hL := J.lkTyped + have hS := J.rrSum hRV + have hTail : P1.RRPre5 (L := L) arI s a := P1.rrPre5_of arI hB + (fun _ _ _ _ _ hct hcaps hchecks => P1.rrPl_lk arI (fun _ h => h) hB hL hct hcaps hchecks) hS hAT + exact rrPre_of_lk (fun _ h => h) hB hL hS (fun _ hct => J.cnOff hct) hTail + +end + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} + +/-- An accepted in-gate ReplyRecv has the exact dispatch and finish outcomes of +the slow ReplyRecv. The concrete lookup and acceptance checks are those of +this run. [J] The entry refill-sum invariant transfers to the matched +passing-budget state. [P] `StoreExt`, architecture totality and one domain +are the existing equality and budget premises. -/ +theorem VEAnyX.replyRecvAccepted + {s s' : S} {a a' : A} {σ σb : Fl4Spec.State Unit} {x : Fl4Spec.Tcb} + (J : VEAnyX (L := L) arI AR s a σb) + (hStore : P1.StoreExt st) (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hk : σ.kheap σ.curThread = some (.tcb x)) (hRV : SInvR.RV σ) + (hb : BudgetAt σ σb true) + (hAccepted : fastpath.reply_recv st arI s a = ok (true, s', a')) : + ∃ (sd : S) (ad : A), + syscall.dispatch_syscall st arI s a fl4_abi.syscall.SysReplyRecv = ok (sd, ad) ∧ + syscall.finish st arI sd ad = ok (s', a') := by + obtain ⟨_, X, XL, Z, H, Cs, Ac, Lst, D, _, JV⟩ := J + have hRVb := rv_faultIP (BudgetAt.shape27 hnd hk hb).2.1 hRV + exact P1.rr_same arI hStore hAccepted (JV.1.1.replyRecvPre hAT hRVb) + +/-- Reuse a matched slow-path postcondition on the accepted concrete result. +The postcondition can retain the spec witness and its oracle-read evidence. -/ +theorem VEAnyX.replyRecvAcceptedPost + {s s' : S} {a a' : A} {σ σb : Fl4Spec.State Unit} {x : Fl4Spec.Tcb} + (J : VEAnyX (L := L) arI AR s a σb) + (hStore : P1.StoreExt st) (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hk : σ.kheap σ.curThread = some (.tcb x)) (hRV : SInvR.RV σ) + (hb : BudgetAt σ σb true) + (hAccepted : fastpath.reply_recv st arI s a = ok (true, s', a')) + {Q : S × A → Prop} + (hSlow : (do + let (sd, ad) ← syscall.dispatch_syscall st arI s a fl4_abi.syscall.SysReplyRecv + syscall.finish st arI sd ad) ⦃ Q ⦄) : Q (s', a') := by + obtain ⟨sd, ad, hDisp, hFinish⟩ := J.replyRecvAccepted hStore hAT hnd hk hRV hb hAccepted + simpa only [hDisp, bind_ok, uncurry_apply_pair, hFinish, WP.spec_ok] using hSlow + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Fault.lean b/proofs/Fl4Proofs/P3/VmEntryS3Fault.lean new file mode 100644 index 000000000..d7f5e9628 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Fault.lean @@ -0,0 +1,568 @@ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmEntryS3Finish +import Fl4Proofs.P3.SpecInvDomE +import Fl4Proofs.P3.SpecInvFoG6 +import Fl4Proofs.P3.SpecInvSgI6 +import Fl4Proofs.P3.VmEntryS3Oracle + +/-! +x86 VM user exceptions, VM faults, traps and ordinary unknown syscalls +with the finish facts carried from the actual fault and budget outcomes. +The abstract finish invariants are the native domain list, FPU owner +and idle vtable. The concrete oracles are read at the entry. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Spec Fl4Proofs.P1 Vs Notif Endp Ipc Sched SInvDom + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → MachineState → Prop} +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} +variable {Cs Ac D : Set U64} {Lst : U64 → List U64} {s : S} {a : A} {σ : State Unit} + +/-- The finish facts carried by the matched event prefix. -/ +abbrev FinishFactsS3 (on rc : ObjRef → Bool) (s' : S) (σ' : State Unit) : Prop := + VmOrc st on rc s' σ' ∧ DomListOK σ' ∧ FpuOwnOK σ' ∧ IdleVt σ' + +/-- Fault delivery carries its matched scheduler and slot footprints to +the finish. [J] The entry/fault and finish invariants at the starting pair. +[P] The existing architecture totality. -/ +theorem VmEptState.handleFaultFinishS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → validFaultHandler x.tcbFaultHandler) + {f : error.Fault} {f' : Fault} (hf : FaultCorrG f f') {on rc : ObjRef → Bool} + (hI : FinishFactsS3 (st := st) on rc s σ) : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (handleFault (Z := Unit) ct.bv f' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ FinishFactsS3 (st := st) on rc r.1 z.2 ⦄ := by + obtain ⟨hO, hDom, hFpu, hIdle⟩ := hI + have B := J.1.1 + refine WP.spec_mono (B.faultHypJZG hAT hni hct hnq hvfh hf) ?_ + rintro ⟨s', a'⟩ ⟨z, hz, _, V', Ac', Lst', D', B', F⟩ + have hDom' := domListOK_of_dlV (dl_handleFault ct.bv f' σ z hz) hDom + obtain ⟨hOwn, hFpu'⟩ := SInvF.oi_handleFault ct.bv f' σ ⟨rfl, hFpu⟩ z hz + have hIdle' := idleVt_ofSlots B.xtop B.jpl B.zH.1 B'.xtop F.ss + (SInvG.idk_handleFault ct.bv f' σ rfl z hz) hIdle + exact ⟨z, hz, (J.ofNat (V' := V') B' F).anyX arI, + B.vmOrcAcross B' hO F.fk.1.2.2.1, hDom', fun o ho => hFpu' o (hOwn.symm.trans ho), hIdle'⟩ + +private theorem budgetTrueS3 (hnd : Fl4Spec.numDomains = 1) {σ σb : Fl4Spec.State Unit} + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hb : (true, σb) ∈ (specBudget σ).1) : + Entered σb ∧ (∀ d p, σb.curThread ∉ σb.readyQueues d p) ∧ + (∀ x, σb.kheap σb.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) ∧ + σb.kheap σb.curThread = σ.kheap σ.curThread ∧ σb.curThread = σ.curThread ∧ + σb.machineState = gctState σ.machineState := by + obtain ⟨ek, ect, eid, erq, esa, ems⟩ := specBudget_true_shape hnd hb + refine ⟨⟨by rw [ect, eid]; exact hni.1, by rw [esa]; exact hni.2⟩, by rw [ect, erq]; exact hnq, + by rw [ek, ect]; exact hvfh, by rw [ek, ect], ect, ems⟩ + + +private theorem userFaultArmS3 + (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + {on rc : ObjRef → Bool} {σ : State Unit} (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (n c : U64) (hU : UserHw n c) + {s' : S} {a' : A} {σb : Fl4Spec.State Unit} (hb : (true, σb) ∈ (specBudget σ).1) + (hV : VEAnyX (L := L) arI AR s' a' σb) (hI : FinishFactsS3 (st := st) on rc s' σb) : + brU (st := st) arI n c s' a' ⦃ r => ∃ z ∈ (brUS n c σb).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ FinishFactsS3 (st := st) on rc r.1 z.2 ⦄ := by + obtain ⟨hni1, hnq1, hvfh1, -, -, -⟩ := budgetTrueS3 hnd hni hnq hvfh hb + obtain ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, _, J⟩ := hV + obtain ⟨ct, hct, -⟩ := spec_exists (sched.cur_thread.spec' (L := L) s') + have hcur := (J.1.1.curEq hct).1 + have hf : FaultCorrG (.UserException n c) (.userException n.bv c.bv) := + Or.inr (Or.inr (Or.inl ⟨n, c, rfl, rfl⟩)) + unfold brU + rw [hct, bind_ok] + refine WP.spec_mono (J.handleFaultFinishS3 hAT hni1 hct hnq1 hvfh1 hf hI) ?_ + rintro r ⟨z, hz, hV'⟩ + have en : n.bv &&& (Fl4Spec.mask 32 : BitVec 64) = n.bv := and_mask_lt n.bv (by decide) hU.1 + have ec : c.bv &&& (Fl4Spec.mask 28 : BitVec 64) = c.bv := and_mask_lt c.bv (by decide) hU.2 + refine ⟨z, mem_bind_nd (x := (σb.curThread, σb)) rfl ?_, hV'⟩ + rw [hcur, en, ec] + exact hz + +private theorem vmFaultArmS3 + (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + {on rc : ObjRef → Bool} {σ : State Unit} (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (ad c : U64) (ft : Fl4Spec.VMFaultType) (hV : VmfHw ad c ft σ) + {s' : S} {a' : A} {σb : Fl4Spec.State Unit} (hb : (true, σb) ∈ (specBudget σ).1) + (hJ : VEAnyX (L := L) arI AR s' a' σb) (hI : FinishFactsS3 (st := st) on rc s' σb) : + brV (st := st) arI ad c s' a' ⦃ r => ∃ z ∈ (brVS ft σb).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ FinishFactsS3 (st := st) on rc r.1 z.2 ⦄ := by + obtain ⟨hni1, hnq1, hvfh1, ek, _, ems⟩ := budgetTrueS3 hnd hni hnq hvfh hb + obtain ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, _, J⟩ := hJ + obtain ⟨ct, hct, -⟩ := spec_exists (sched.cur_thread.spec' (L := L) s') + obtain ⟨hcur, x0, hx0⟩ := J.1.1.curEq hct + have hkb : σb.kheap σb.curThread = some (.tcb x0) := by rwa [hcur] + have hk : σ.kheap σ.curThread = some (.tcb x0) := ek ▸ hkb + unfold brV + rw [hct, bind_ok, shr_eq c 4#i32 (by decide) (by decide), bind_ok] + simp only [lift, bind_ok, vspace.vm_fault] + generalize hins : (((⟨c.bv >>> (4#i32 : I32).toNat⟩ : U64) &&& 1#u64) != 0#u64) = ins + have hii := (ins_iff c).trans hV.kind.symm + rw [hins] at hii + have hf : FaultCorrG (.VmFault ad c ins) (.archFault (.vmFault ad.bv [(if ins then 1#u64 else 0#u64).bv, c.bv])) := + Or.inr (Or.inr (Or.inr ⟨ad, c, ins, rfl, rfl⟩)) + refine WP.spec_mono (J.handleFaultFinishS3 hAT hni1 hct hnq1 hvfh1 hf hI) ?_ + rintro r ⟨z, hz, hV'⟩ + have hb0 : (if ins then 1#u64 else 0#u64).bv = ftBit ft := by + cases ft <;> cases ins + · rfl + · exact absurd (hii.1 rfl) (by intro h; cases h) + · exact absurd (hii.2 rfl) (by intro h; cases h) + · rfl + have he := handleVmFault_err hkb ft hb0 + have e5 : x0.tcbArch.tcbContext.userRegs .errorRegister &&& (Fl4Spec.mask 5 : BitVec 64) = c.bv := by + rw [hV.err x0 hk] + exact and_mask_lt c.bv (by decide) hV.lt + rw [e5, ems, hV.addr] at he + refine ⟨z, mem_bind_nd (x := (σb.curThread, σb)) rfl ?_, hV'⟩ + exact mem_catch_err he (by rw [hcur]; exact hz) + + +set_option maxHeartbeats 4000000 in +/-- Compose the exact budget outcome, guarded fault and VM-aware finish. -/ +private theorem VmEptState.finishFaultS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) + (hDL : SetDlDet) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {on rc : ObjRef → Bool} (hEntryOrc : VmOrc st on rc s σ) + {ev : Event} {B : S → A → Result (S × A)} {G : SMonad Unit Unit} + (hEv : (fl4HandleEventVF (Z := Unit) on rc true ev).run σ = + (withoutPreemption (do + updateTimeStamp + let run ← checkBudgetRestart + NonDet.when run G)).run σ) + (hArm : ∀ s' a' σb, (true, σb) ∈ (specBudget σ).1 → + VEAnyX (L := L) arI AR s' a' σb → + FinishFactsS3 (st := st) on rc s' σb → + B s' a' ⦃ r => ∃ z ∈ (G σb).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ + FinishFactsS3 (st := st) on rc r.1 z.2 ⦄) : + (do + let (run, s', a') ← syscall.budget_ok st arI s a + let (s2, a2) ← if run then B s' a' else ok (s', a') + syscall.finish st arI s2 a2) ⦃ r => ∃ σ', + ((), σ') ∈ (fl4CallKernelVF (Z := Unit) on rc true ev σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + refine WP.spec_bind (J.budgetFramesV hni hnq hM hMR hVM hOff hARI.toARKeep hAT hnd hmrt hmb hmax htu) ?_ + rintro ⟨run, sb, ab⟩ ⟨σb, hb, _, _, _, _, Jb, Fb⟩ + have J0 := J.1.1 + have B' := Jb.1.1 + obtain ⟨hDom', hFo', hIdleEq⟩ := specBudgetFinishInvariants hb hDom hFo + have hIdle' := idleVt_ofSlots J0.xtop J0.jpl J0.zH.1 B'.xtop Fb.ss hIdleEq hIdle + have hO := J0.vmOrcAcross B' hEntryOrc Fb.fk.1.2.2.1 + have hFacts : FinishFactsS3 (st := st) on rc sb σb := + ⟨hO, hDom', hFo', hIdle'⟩ + have hPrefix : (if run then B sb ab else ok (sb, ab)) ⦃ r => + ∃ z ∈ (NonDet.when run G σb).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ + FinishFactsS3 (st := st) on rc r.1 z.2 ⦄ := by + cases run + · exact (WP.spec_ok _).2 ⟨((), σb), Set.mem_singleton _, Jb.anyX arI, hFacts⟩ + · exact hArm sb ab σb hb (Jb.anyX arI) hFacts + refine WP.spec_bind hPrefix ?_ + rintro ⟨sf, af⟩ ⟨z, hz, ⟨Vf, Xf, XLf, Zf, Hf, Csf, Acf, Lstf, Df, _, Jf⟩, hOf, hDf, hFf, hIf⟩ + have F := Jf.1.1.kfinVE_ofFacts hAT hFpu hFW hDf hFf hIf hOf + refine WP.spec_mono (Jf.finishVF hAT hVA hARI.toARKeep hmb F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, _, _, _, _, J'⟩ + refine ⟨σ', ?_, J'.anyX arI⟩ + obtain ⟨u, hu, hb⟩ := mem_bind_elim hb + have hz' : ((), z.2) ∈ (NonDet.when run G σb).1 := by obtain ⟨⟨⟩, _⟩ := z; exact hz + have he : ((.ok (), z.2) : Except Unit Unit × State Unit) ∈ + ((fl4HandleEventVF (Z := Unit) on rc true ev).run σ).1 := by + rw [hEv] + exact mem_liftE (mem_bind_nd hu (mem_bind_nd hb hz')) + unfold fl4CallKernelVF + exact mem_bind_nd (mem_handleE_ok he) hmem + +private theorem VmEptState.userExceptionAtOracle + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + {on rc : ObjRef → Bool} (hEntryOrc : VmOrc st on rc s σ) + (n c : U64) (hw : UserHw n c) : + syscall.handle_user_exception st arI s a n c ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) on rc true (.userLevelFault n.bv c.bv) σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := + J.finishFaultS3 hARI hAT hVA hnd hT hRef hDL hFpu hFW hmrt hmb hmax htu hni hnq + hM hMR hVM hOff hDom hFo hIdle hEntryOrc (B := brU (st := st) arI n c) (G := brUS n c) rfl + (fun _ _ _ hb hV hI => userFaultArmS3 hAT hnd hni hnq hvfh n c hw hb hV hI) + +/-- The user exception with the finish at its paired prefix outcome. +[J] Native time, refill, fault-handler, queue and finish invariants. +[P] The existing event, hardware and platform agreements. -/ +theorem VmEptState.userExceptionEntryS3 + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (n c : U64) (hw : UserHw n c) : + syscall.handle_user_exception st arI s a n c ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true (.userLevelFault n.bv c.bv) σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := + J.userExceptionAtOracle hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu hFpu hFW hDom hFo hIdle + hni hnq hvfh hM hMR hVM hOff (fun _ _ _ _ => ⟨rfl, fun _ => rfl⟩) n c hw + +/-- The VM fault with the finish at its paired prefix outcome. +[J] Native time, refill, fault-handler, queue and finish invariants. +[P] The existing event, hardware and platform agreements. -/ +theorem VmEptState.vmFaultEntryS3 + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (ad c : U64) (ft : Fl4Spec.VMFaultType) (hw : VmfHw ad c ft σ) : + syscall.handle_vm_fault st arI s a ad c ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true (.vmFaultEvent ft) σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := + J.finishFaultS3 hARI hAT hVA hnd hT hRef hDL hFpu hFW hmrt hmb hmax htu hni hnq + hM hMR hVM hOff hDom hFo hIdle (fun _ _ _ _ => ⟨rfl, fun _ => rfl⟩) (B := brV (st := st) arI ad c) (G := brVS ft) rfl + (fun _ _ _ hb hV hI => vmFaultArmS3 hAT hnd hni hnq hvfh ad c ft hw hb hV hI) + +private theorem unknownFaultArmS3 + (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + {on rc : ObjRef → Bool} {σ : State Unit} (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (num : U64) {s' : S} {a' : A} {σb : Fl4Spec.State Unit} + (hb : (true, σb) ∈ (specBudget σ).1) (hV : VEAnyX (L := L) arI AR s' a' σb) + (hI : FinishFactsS3 (st := st) on rc s' σb) : + brK (st := st) (arI := arI) num s' a' ⦃ r => ∃ z ∈ (brKS num.val σb).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ FinishFactsS3 (st := st) on rc r.1 z.2 ⦄ := by + obtain ⟨hni', hnq', hvfh', -, -, -⟩ := budgetTrueS3 hnd hni hnq hvfh hb + obtain ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, _, J⟩ := hV + obtain ⟨ct, hct, -⟩ := spec_exists (sched.cur_thread.spec' (L := L) s') + have hcur := (J.1.1.curEq hct).1 + have hf : FaultCorrG (.UnknownSyscall num) (.unknownSyscallException num.bv) := + Or.inr (Or.inl ⟨num, rfl, rfl⟩) + unfold brK + rw [hct, bind_ok] + refine WP.spec_mono (J.handleFaultFinishS3 hAT hni' hct hnq' hvfh' hf hI) ?_ + rintro r ⟨z, hz, hV'⟩ + have hnum : (BitVec.ofNat 64 num.val : BitVec 64) = num.bv := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt num.bv.isLt + refine ⟨z, mem_bind_nd (x := (σb.curThread, σb)) rfl ?_, hV'⟩ + rw [hnum, hcur] + exact hz + +set_option maxHeartbeats 4000000 in +/-- The native unknown-syscall fault after the FaultIP write. The +extended event must select its ordinary fault branch. -/ +private theorem VmEptState.unknownFaultAtOracle + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {on rc : Fl4Spec.ObjRef → Bool} {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (num : U64) (hNotEnter : Fl4Spec.isVmEnterCall num.val σ = false) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hFacts : FinishFactsS3 (st := st) on rc s σ) : + syscall.unknown_syscall_fault st arI s a num false ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) on rc true (.unknownSyscall num.val) σ).1 ∧ + VEAnyX (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := by + have hguard (z : Fl4Spec.State Unit) (hz : Fl4Spec.isVmEnterCall num.val z = false) : + (Fl4Spec.fl4HandleEventVF (Z := Unit) on rc true (.unknownSyscall num.val)).run z = + (Fl4Spec.handleEvent (Z := Unit) (.unknownSyscall num.val)).run z := by + unfold Fl4Spec.fl4HandleEventVF + rw [ExceptT.run_bind] + have hread : (Fl4Spec.NonDetE.liftE (ε := Unit) + (Fl4Spec.NonDet.gets (Fl4Spec.isVmEnterCall (Z := Unit) num.val))).run z = + ({(.ok false, z)}, False) := by + refine run_liftE ?_ + show ({(Fl4Spec.isVmEnterCall num.val z, z)}, False) = _ + rw [hz] + rw [nd_bind_single hread] + rfl + -- The VM branch discriminator is read only at the event's entry. + have hEv : + (Fl4Spec.fl4HandleEventVF (Z := Unit) on rc true (.unknownSyscall num.val)).run σ = + (Fl4Spec.withoutPreemption (do + Fl4Spec.updateTimeStamp + let run ← Fl4Spec.checkBudgetRestart + Fl4Spec.NonDet.when run (brKS num.val))).run σ := + hguard σ hNotEnter + obtain ⟨hO, hDom, hFo, hIdle⟩ := hFacts + refine usf_spec (st := st) (arI := arI) s a num (P := fun r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) on rc true (.unknownSyscall num.val) σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ') ?_ + exact J.finishFaultS3 hARI hAT hVA hnd hT hRef hDL hFpu hFW hmrt hmb hmax htu hni hnq + hM hMR hVM hOff hDom hFo hIdle hO (B := brK (st := st) (arI := arI) num) (G := brKS num.val) hEv + (fun _ _ _ hb hv hI => unknownFaultArmS3 hAT hnd hni hnq hvfh num hb hv hI) + +private theorem validFHAtFaultIP {σ : Fl4Spec.State Unit} {x : Fl4Spec.Tcb} + (hk : σ.kheap σ.curThread = some (.tcb x)) + (hv : ∀ y, σ.kheap σ.curThread = some (.tcb y) → Fl4Spec.validFaultHandler y.tcbFaultHandler) : + ∀ y, (faultIPSt σ σ.curThread x).kheap σ.curThread = some (.tcb y) → + Fl4Spec.validFaultHandler y.tcbFaultHandler := by + intro y hy + simp only [faultIPSt, Notif.updK, Function.update_self, Option.some.injEq, + Fl4Spec.KernelObject.tcb.injEq] at hy + subst y + exact hv x hk + +/-- FaultIP changes only the current thread's saved register image. -/ +private theorem faultIPFinishS3 + {s0 : S} {x : Tcb} {W : XVs} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (J0 : VmEptState (L := L) arI V X XL AR (Z.mv Z.G W) H Cs Ac Lst D s0 a (faultIPSt σ σ.curThread x)) + (hk : σ.kheap σ.curThread = some (.tcb x)) (hni : Entered σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) : + FinishFactsS3 (st := st) (v3OnCore st s) (v3RemCur st s) s0 (faultIPSt σ σ.curThread x) := by + refine ⟨J.1.1.vmOrcOfEntry J0.1.1 ?_, domListOK_of_dlV (σ := σ) (σ' := faultIPSt σ σ.curThread x) rfl hDom, ?_, ?_⟩ + · intro t y hy _ + by_cases e : t = σ.curThread + · subst t + exact ⟨x, hk⟩ + · exact ⟨y, by simpa only [faultIPSt, Notif.updK, Function.update_of_ne e] using hy⟩ + · intro o ho + obtain ⟨y, hy⟩ := hFo o ho + by_cases e : o = σ.curThread + · subst o + exact ⟨Notif.setReg x .faultIP (x.tcbArch.tcbContext.userRegs .nextIP - 2), + by simp only [faultIPSt, Notif.updK, Function.update_self]⟩ + · exact ⟨y, by simpa only [faultIPSt, Notif.updK, Function.update_of_ne e] using hy⟩ + · obtain ⟨y, hy, hv⟩ := hIdle + exact ⟨y, by simpa only [faultIPSt, Notif.updK, Function.update_of_ne hni.1.symm] using hy, hv⟩ + +set_option maxHeartbeats 4000000 in +/-- The trapped syscall uses the oracle at the concrete entry and +carries the actual post-FaultIP and budget states through the fault finish. +[J] The current thread, fault handler, time, queues and finish invariants. +[P] The existing entry and machine agreements. [S] The Stage 2 event guards. -/ +theorem VmEptState.trapSyscallEntryS3 + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (v : U64) (hv : v.val < 256): + syscall.handle_trap_syscall st arI s a v ⦃ r => ∃ ev σ', + KEvSpec (.trap v) true ev ∧ FaultIpOutVF (v3OnCore st s) (v3RemCur st s) ev σ σ' ∧ VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + have hct := cur_thread_eq (L := L) s + generalize L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = ct at hct + obtain ⟨x, hk, -, V', J0⟩ := J.faultIPEntry hct + generalize hs : L.wr s (Endp.regA ct layout.reg.FAULT_IP) + (core.num.U64.wrapping_sub (L.mem s (Endp.regA ct layout.reg.NEXT_IP)) 2#u64) = s0 at J0 + let σ0 := faultIPSt σ σ.curThread x + have hF : ((), σ0) ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1 := by + rw [fl4SyscallFaultIP_eq hk] + exact Set.mem_singleton _ + have hf0 := validFHAtFaultIP hk hvfh + obtain ⟨hO0, hDom0, hFo0, hIdle0⟩ := faultIPFinishS3 J J0 hk hni hDom hFo hIdle + have hoff := SInvR.G4.offReady_faultIPSt (t := σ.curThread) (x := x) hOff + unfold syscall.handle_trap_syscall + rw [hct, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.NEXT_IP) ?_ + rintro nx rfl + simp only [lift, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s ct layout.reg.FAULT_IP _) ?_ + rintro s1 rfl + rw [hs] + obtain ⟨fl, hfl, -⟩ := spec_exists (Fl4Proofs.P0.obj.Tcb.flags.p0 st ct s0 L.total) + rw [hfl, bind_ok] + by_cases htr : (fl &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS != 0#u64) = true + · rw [if_pos htr, shl_eq v 3#i32 (by decide) (by decide), bind_ok] + try simp only [lift, bind_ok] + generalize hcd : ((⟨v.bv <<< (3#i32 : I32).toNat⟩ : U64) ||| 2#u64) = code + have hcode : code.val < 2 ^ 28 := by + rw [← hcd, UScalar.val_or] + exact Nat.or_lt_two_pow (shl3_lt v hv) (by decide) + have hU : UserHw 13#u64 code := ⟨by decide, hcode⟩ + have hev : KEvSpec (.trap v) true (.userLevelFault (13#u64 : U64).bv code.bv) := KEvSpec.trapF _ _ _ + refine WP.spec_mono (J0.userExceptionAtOracle hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu + hFpu hFW hDom0 hFo0 hIdle0 hni hnq hf0 hM hMR hVM hoff hO0 _ _ hU) ?_ + rintro r ⟨σ', hmem, hV⟩ + exact ⟨_, σ', hev, ⟨((), σ0), hF, hmem⟩, hV⟩ + · rw [if_neg htr] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s0 ct layout.reg.RAX) ?_ + rintro eax - + rw [shl_eq v 24#i32 (by decide) (by decide), bind_ok] + try simp only [lift, bind_ok] + generalize hnm : ((⟨v.bv <<< (24#i32 : I32).toNat⟩ : U64) ||| (eax &&& 16777215#u64)) = num + have hnum : num.val < 2 ^ 32 := by + rw [← hnm, UScalar.val_or] + refine Nat.or_lt_two_pow (shl24_lt v hv) ?_ + rw [UScalar.val_and] + exact lt_of_le_of_lt Nat.and_le_right (by decide) + have hne : num.val ≠ Fl4Spec.fl4SysVMEnterNum := by + unfold Fl4Spec.fl4SysVMEnterNum + omega + have hnx : Fl4Spec.isVmEnterCall num.val σ0 = false := by + unfold Fl4Spec.isVmEnterCall + simp [hne] + have hev : KEvSpec (.trap v) true (.unknownSyscall num.val) := KEvSpec.trapU _ _ + rw [handle_unknown_syscall_pos s0 a num hnum] + refine WP.spec_bind (J0.unknownFaultAtOracle hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu + hni hnq hf0 hM hMR hVM hoff num hnx hFpu hFW ⟨hO0, hDom0, hFo0, hIdle0⟩) ?_ + rintro ⟨b, sf, af⟩ ⟨σ', hmem, hV⟩ + exact (WP.spec_ok _).2 ⟨_, σ', hev, ⟨((), σ0), hF, hmem⟩, hV⟩ + +set_option maxHeartbeats 4000000 in +/-- The ordinary unknown syscall uses the oracle at the concrete entry and +carries the actual post-FaultIP and budget states through the fault finish. +[J] The current thread, fault handler, time, queues and finish invariants. +[P] The existing entry and machine agreements. [S] The Stage 2 event guards. -/ +theorem VmEptState.unknownSyscallEntryS3 + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (n : I64) (hU : UnkSys (L := L) n s) (hD : NoDbgSys (L := L) arI n s a) + (hSX : P1.StoreExt st) (hFF : P1.FastFaultOK (L := L) arI s a n) + (hNotEnter : ∀ x ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1, + Fl4Spec.isVmEnterCall (IScalar.hcast .U64 n).val x.2 = false): + syscall.handle_syscall st arI s a n ⦃ r => ∃ ev σ', + KEvSpec (.sysU n) true ev ∧ FaultIpOutVF (v3OnCore st s) (v3RemCur st s) ev σ σ' ∧ VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + rw [handle_syscall_unk_eq hAT hSX hFF hU] + have hct := cur_thread_eq (L := L) s + generalize L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = ct at hct + obtain ⟨x, hk, hcur, V', J0⟩ := J.faultIPEntry hct + let val := core.num.U64.wrapping_sub (L.mem s (Endp.regA ct layout.reg.NEXT_IP)) 2#u64 + generalize hs : L.wr s (Endp.regA ct layout.reg.FAULT_IP) val = s0 at J0 + let σ0 := faultIPSt σ σ.curThread x + have hF : ((), σ0) ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1 := by + rw [fl4SyscallFaultIP_eq hk] + exact Set.mem_singleton _ + have hf0 := validFHAtFaultIP hk hvfh + obtain ⟨hO0, hDom0, hFo0, hIdle0⟩ := faultIPFinishS3 J J0 hk hni hDom hFo hIdle + have hoff := SInvR.G4.offReady_faultIPSt (t := σ.curThread) (x := x) hOff + let num := IScalar.hcast .U64 n + have hev : KEvSpec (.sysU n) true (.unknownSyscall num.val) := KEvSpec.sysU _ _ + have hNot : Fl4Spec.isVmEnterCall num.val σ0 = false := hNotEnter ((), σ0) hF + have hUnknown := J0.unknownFaultAtOracle hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu + hni hnq hf0 hM hMR hVM hoff num hNot hFpu hFW ⟨hO0, hDom0, hFo0, hIdle0⟩ + unfold syscall.handle_syscall_general + refine WP.spec_bind (Pₘ := fun r : Bool × S × A => ∃ σ', FaultIpOutVF (v3OnCore st s) (v3RemCur st s) (.unknownSyscall num.val) σ σ' ∧ + VEAnyX (L := L) arI AR r.2.1 r.2.2 σ') ?_ ?_ + · have hUSF : syscall.unknown_syscall_fault st arI s0 a num false ⦃ r => ∃ σ', + FaultIpOutVF (v3OnCore st s) (v3RemCur st s) (.unknownSyscall num.val) σ σ' ∧ VEAnyX (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := + WP.spec_mono hUnknown fun _ ⟨σ', hmem, hVE⟩ => ⟨σ', ⟨((), σ0), hF, hmem⟩, hVE⟩ + unfold syscall.syscall + rw [hct, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.NEXT_IP) ?_ + rintro nx rfl + simp only [lift, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s ct layout.reg.FAULT_IP _) ?_ + rintro s1 rfl + rw [hs] + obtain ⟨fl, hfl, -⟩ := spec_exists (Fl4Proofs.P0.obj.Tcb.flags.p0 st ct s0 L.total) + rw [hfl, bind_ok] + try simp only [lift, bind_ok] + by_cases htr : (fl &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS != 0#u64) = true + · rw [if_pos htr] + exact hUSF + rw [if_neg htr] + have hrange : n < fl4_abi.syscall.SYSCALL_MIN ∨ n > fl4_abi.syscall.SYSCALL_MAX := by + rcases hU with hu | hu | hu + · exact Or.inl hu + · exact Or.inr hu + · exfalso + have h0 := hu ct val hct + rw [hs, hfl, WP.spec_ok] at h0 + exact h0 (by by_contra hne; exact htr (bne_iff_ne.2 hne)) + obtain ⟨d1, d2, d3, d4, d5, d6, d7, d8, d9, dv⟩ := hD + have hv : n = fl4_abi.syscall.SysVMEnter → arI.virtualization a = ok 0#u64 ∨ + ∀ t, sched.cur_thread st s0 = ok t → vcpu.bound st s0 t = ok 0#u64 := fun hn => by + rcases dv hn with e | e + · exact Or.inl e + · refine Or.inr fun t ht => ?_ + have e' : σ0.curThread = t.bv := (J0.1.1.curEq ht).1 + have ect : t = ct := VirtSanEq.u64_ext (e'.symm.trans hcur) + subst t + simpa only [hs] using e ct val hct + have hNd := handle_unknown_syscall_nd arI hAT s0 a n false d1 d2 d3 d4 d5 d6 d7 d8 d9 hv + rcases hrange with hlo | hhi + · rw [if_pos hlo, hNd] + exact hUSF + · by_cases hlo : n < fl4_abi.syscall.SYSCALL_MIN + · rw [if_pos hlo, hNd] + exact hUSF + · rw [if_neg hlo, if_pos hhi, hNd] + exact hUSF + · rintro ⟨b, sf, af⟩ ⟨σ', hmem, hVE⟩ + exact (WP.spec_ok _).2 ⟨_, σ', hev, hmem, hVE⟩ + + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Finish.lean b/proofs/Fl4Proofs/P3/VmEntryS3Finish.lean new file mode 100644 index 000000000..869a3e08e --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Finish.lean @@ -0,0 +1,906 @@ +import Fl4Proofs.P3.VmKEntryX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmEntryS3Oracle +import Fl4Proofs.P3.ArchSwThr +import Fl4Proofs.P3.ArchSwIdle +import Fl4Proofs.P3.ScFpuNd +import Fl4Proofs.P3.VmDelFinSpec +import Fl4Proofs.P3.SpecInvFoG4 +import Fl4Proofs.P3.SpecInvSgI4 + +/-! +The x86 VM entry finish uses the native architecture switch and domain +producers with the VM-aware state relation. + +* [J] `DomListOK`, `FpuOwnOK` and `IdleVt` are the native finish + invariants. The leaves here use their individual propositions. +* [J] `VmOrc` relates a pair of signal oracles to the current matched state. +* [P] `FpuImplDet` and `FpuWriteDet` supply the native architecture switch + and architecture nondeterminism facts. The remaining clock, IRQ and + VMEnter platform assumptions match the respective Stage-2 entries. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Fl4Proofs.P1 Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Sched Fl4Proofs.P3.Ipc + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] + {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} + {MR : A → Fl4Spec.MachineState → Prop} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- Assemble the finish facts at a matched state. [J] the domain list, FPU +owner, idle vtable and local oracle agreement. [P] architecture totality and +the determinate FPU operations. -/ +theorem JAtZB.kfinVE_ofFacts + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) : + KFinVE (L := L) arI on rc Z H Cs s σ := by + have hD : DomRel (L.mem s) (L.ksb s) σ := J.xtop.rel.2.2.2.1 + have hNd : NdOK (L := L) s σ := by + refine ⟨hD, hDom, ?_⟩ + rw [hD.2.2.2, hDom.idx0 hD.1]; rfl + exact ⟨Or.inr hNd, hO, ⟨archSwFT_of hFpu hFW hAT, archSwFI_of hAT⟩, + archNdF_of hFpu, hFo, hIdle⟩ + +section Spec + +open Fl4Spec Classical + +private theorem nkc_releaseInsert (t : ObjRef) (qs : List ObjRef) : NKC (readyTimesAppend (Z := Unit) t qs) := by + have hRead (p : ObjRef) : NKC (NonDet.getsThe (readTcbReadyTime (Z := Unit) p)) := nkc_getsThe _ + have hMap (l : List ObjRef) : NKC (NonDet.mapM (fun p => NonDet.getsThe (readTcbReadyTime (Z := Unit) p)) l) := by + induction l with + | nil => exact nkc_pure _ + | cons p ps ih => exact nkc_bind (hRead p) fun _ => nkc_bind ih fun _ => nkc_pure _ + unfold readyTimesAppend orderedInsert + exact nkc_bind (hRead t) fun _ => nkc_bind (hMap qs) fun _ => + nkc_bind (nkc_pure _) fun _ => nkc_bind (nkc_pure _) fun _ => nkc_pure _ + +private theorem nkc_resumeDonated (sc : ObjRef) : NKC (schedContextResume (Z := Unit) sc) := by + unfold schedContextResume getScRefillReady getScRefillSufficient getTcbQueue postpone tcbReleaseEnqueue setReleaseQueue + repeat (first | exact nkc_releaseInsert _ _ | nkc_step) + +private theorem nkc_maybeDonateScS3 (t n : ObjRef) : NKC (maybeDonateSc (Z := Unit) t n) := by + unfold maybeDonateSc + repeat (first | exact nkc_getSkObjRef _ _ _ | exact nkc_schedContextDonate _ _ | exact nkc_resumeDonated _ | nkc_step) + +private theorem nkc_waitingSignal (n : ObjRef) (q : List ObjRef) (b : Badge) : + NKC (updateWaitingNtfn (Z := Unit) n q b) := by + unfold updateWaitingNtfn + repeat first + | exact nkc_tcbNtfnDequeue _ _ | exact nkc_setThreadState _ _ | exact nkc_asUser _ _ + | exact nkc_maybeDonateScS3 _ _ | exact nkc_possibleSwitchTo _ | exact nkc_ifCondRefillUnblockCheck _ _ _ + | nkc_step + +private theorem nkc_sendSignalS3 (n : ObjRef) (b : Badge) : NKC (sendSignal (Z := Unit) n b) := by + unfold sendSignal getThreadState + repeat first + | exact nkc_waitingSignal _ _ _ | exact nkc_cancelIpc _ | exact nkc_setThreadState _ _ + | exact nkc_asUser _ _ | exact nkc_maybeDonateScS3 _ _ | exact nkc_possibleSwitchTo _ + | exact nkc_ifCondRefillUnblockCheck _ _ _ | nkc_step + +set_option maxHeartbeats 4000000 in +private theorem nkc_sendSignalVFS3 (on rc : ObjRef → Bool) (n : ObjRef) (b : Badge) : + NKC (sendSignalVF (Z := Unit) on rc n b) := by + unfold sendSignalVF getThreadState getScActive vmSignalCompletes getTcbVcpu vmFullReply + repeat first + | exact nkc_sendSignalS3 _ _ | exact nkc_vmReportF _ _ | exact nkc_maybeDonateScS3 _ _ + | exact nkc_possibleSwitchTo _ | exact nkc_getSkObjRef _ _ _ | exact nkc_refillUnblockCheck _ + | exact nkc_getVcpuV _ | exact nkc_asUser _ _ | nkc_step + +private theorem savedIrqFrames (on rc : ObjRef → Bool) (i : Irq) {σ : State Unit} {z : Unit × State Unit} + (hz : z ∈ (handleSavedIrqVF (Z := Unit) on rc i σ).1) : + z.2.domainList = σ.domainList ∧ z.2.domainStartIndex = σ.domainStartIndex ∧ + owF z.2 = owF σ ∧ z.2.idleThread = σ.idleThread ∧ CKeep σ z.2 := by + have hd : DK (handleInterruptVF (Z := Unit) on rc i) := by + rw [handleInterruptVF_hiS] + unfold hiS getIrqState getIrqSlot archMaskIrqSignal handleReservedIrq + dk_auto + unfold handleSavedIrqVF at hz + split at hz + · have ho := crk_doMachineOp (do maskInterrupt true i; ackInterrupt i) σ z hz + exact ⟨(dk_doMachineOp _).dom hz |>.1, (dk_doMachineOp _).dom hz |>.2, + owk_doMachineOp _ σ z hz, congrArg (fun q => q.2.2.2.1) ho, + nkc_doMachineOp _ σ z hz⟩ + · have ho : OWK (handleInterruptVF (Z := Unit) on rc i) := by + rw [handleInterruptVF_hiS] + unfold hiS getIrqState getIrqSlot archMaskIrqSignal handleReservedIrq + owk_auto + all_goals first | exact owk_sendSignalVF _ _ _ _ | exact owk_getCap _ + have hc : CRK (handleInterruptVF (Z := Unit) on rc i) := by + rw [handleInterruptVF_hiS] + unfold hiS getIrqState getIrqSlot archMaskIrqSignal handleReservedIrq + crk_auto + all_goals first | exact crk_sendSignalVF _ _ _ _ | exact crk_getCap _ + have hk : NKC (handleInterruptVF (Z := Unit) on rc i) := by + rw [handleInterruptVF_hiS] + unfold hiS getIrqState getIrqSlot archMaskIrqSignal handleReservedIrq + repeat (first | exact nkc_sendSignalVFS3 _ _ _ _ | exact nkc_getCap _ | exact nkc_doMachineOp _ | nkc_step) + exact ⟨(hd.dom hz).1, (hd.dom hz).2, ho σ z hz, congrArg (fun q => q.2.2.2.1) (hc σ z hz), hk σ z hz⟩ + +private theorem IdleVt.ofCKeep {σ σ' : State Unit} (h : IdleVt σ) + (hi : σ'.idleThread = σ.idleThread) (hk : CKeep σ σ') : IdleVt σ' := by + obtain ⟨x, hx, hv⟩ := h + obtain ⟨y, hy, hmap⟩ := hk.tc σ.idleThread x hx + have hvtab := congrFun hmap (tcbCnodeIndex 1) + rw [tcbCnodeMap_one, tcbCnodeMap_one] at hvtab + exact ⟨y, by rw [hi]; exact hy, fun p asid => by rw [Option.some.inj hvtab]; exact hv p asid⟩ + +private theorem FpuOwnOK.ofCKeep {σ σ' : State Unit} (h : FpuOwnOK σ) + (ho : σ'.archState.x64CurrentFpuOwner = σ.archState.x64CurrentFpuOwner) (hk : CKeep σ σ') : FpuOwnOK σ' := by + intro o he + rw [ho] at he + obtain ⟨x, hx⟩ := h o he + obtain ⟨y, hy, -⟩ := hk.tc o x hx + exact ⟨y, hy⟩ + +/-- A delivered saved vector preserves the three abstract finish invariants. +[J] The domain list, FPU owner, and idle vtable at the saved-vector prefix. -/ +theorem savedIrqFinishInvariants (on rc : ObjRef → Bool) (i : Irq) {σ : State Unit} {z : Unit × State Unit} + (hz : z ∈ (handleSavedIrqVF (Z := Unit) on rc i σ).1) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) : + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ IdleVt z.2 := by + obtain ⟨hdl, hds, how, hi, hk⟩ := savedIrqFrames on rc i hz + have hidx : z.2.domainIndex = σ.domainIndex := congrArg Prod.snd how + exact ⟨SInvDom.domListOK_of_dlV (by unfold SInvDom.dlV; rw [hdl, hds, hidx]) hDom, + FpuOwnOK.ofCKeep hFo (congrArg Prod.fst how) hk, IdleVt.ofCKeep hIdle hi hk⟩ + +set_option maxHeartbeats 4000000 in +private theorem nkc_vmCheckS3 (t : ObjRef) : NKC (vmCheckF (Z := Unit) t) := by + unfold vmCheckF getThreadState getTcbVcpu + repeat' first + | exact nkc_getVcpuV _ | exact nkc_vmReportF _ _ | exact nkc_possibleSwitchTo _ + | nkc_step | dsimp only [letFun] | unfold NonDet.when + +open SInvDom in +set_option maxHeartbeats 4000000 in +private theorem checkTimestampFinishInvariants (hnd : numDomains = 1) {σ σ' : State Unit} + (h : specCheckAfterTimestamp σ = ({((), σ')}, False)) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) : + DomListOK σ' ∧ FpuOwnOK σ' ∧ IdleVt σ' := by + let σt : State Unit := { σ with + curTime := gctVal σ.machineState + consumedTime := σ.consumedTime + (gctVal σ.machineState - σ.curTime) + machineState := gctState σ.machineState } + have htime : updateTimeStamp (Z := Unit) σ = ({((), σt)}, False) := + updateTimeStamp_eq' hnd (doMachineOp_single (getCurrentTime_eq σ.machineState)) + unfold specCheckAfterTimestamp at h + rw [Notif.nd_bind_one htime, nd_gets_bind] at h + change vmCheckF σ.curThread σt = _ at h + have hm : ((), σ') ∈ (vmCheckF σ.curThread σt).1 := by rw [h]; exact Set.mem_singleton _ + have hd : DL (vmCheckF (Z := Unit) σ.curThread) := by + unfold vmCheckF + dl_auto + all_goals (unfold NonDet.when; split <;> dl_auto) + have ho : OWK (vmCheckF (Z := Unit) σ.curThread) := by + unfold vmCheckF vmReportF getTcbVcpu vcpuUpdate setVcpu getVcpu vmFullReply vmFaultIpNext + owk_auto + all_goals (unfold NonDet.when; split <;> owk_auto) + have hc : CRK (vmCheckF (Z := Unit) σ.curThread) := by + unfold vmCheckF vmReportF getTcbVcpu vcpuUpdate setVcpu getVcpu vmFullReply vmFaultIpNext + crk_auto + all_goals (unfold NonDet.when; split <;> crk_auto) + have hk := nkc_vmCheckS3 σ.curThread σt _ hm + have he : σ'.archState.x64CurrentFpuOwner = σt.archState.x64CurrentFpuOwner := congrArg Prod.fst (ho σt _ hm) + have hi : σ'.idleThread = σt.idleThread := congrArg (fun q => q.2.2.2.1) (hc σt _ hm) + have hDomT : DomListOK σt := domListOK_of_dlV (σ := σ) (σ' := σt) rfl hDom + have hFoT : FpuOwnOK σt := by simpa only [FpuOwnOK, σt] using hFo + have hIdleT : IdleVt σt := by simpa only [IdleVt, σt] using hIdle + exact ⟨domListOK_of_dlV (hd σt _ hm) hDomT, FpuOwnOK.ofCKeep hFoT he hk, IdleVt.ofCKeep hIdleT hi hk⟩ + +open SInvDom in +set_option maxHeartbeats 4000000 in +private theorem checkBudgetFinishInvariants {σ : State Unit} {z : Bool × State Unit} + (h : z ∈ (checkBudget (Z := Unit) σ).1) (hd : DomListOK σ) (hf : FpuOwnOK σ) : + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ z.2.idleThread = σ.idleThread := by + have hdl : DL (checkBudget (Z := Unit)) := by dl_auto + obtain ⟨he, hf'⟩ := SInvF.oi_checkBudget σ ⟨rfl, hf⟩ z h + exact ⟨domListOK_of_dlV (hdl σ z h) hd, fun o ho => hf' o (he.symm.trans ho), + SInvG.idk_checkBudget σ rfl z h⟩ + +/-- The timestamp and restart-aware budget guard keep the abstract finish +data used with the guard's concrete slot frame. [J] The domain list and FPU +owner at the start of the guard. -/ +theorem specBudgetFinishInvariants {σ : State Unit} {z : Bool × State Unit} + (hz : z ∈ (specBudget σ).1) (hd : DomListOK σ) (hf : FpuOwnOK σ) : + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ z.2.idleThread = σ.idleThread := by + obtain ⟨u, hu, hb⟩ := mem_bind_elim hz + have ed := (SInvDom.dl_checkBudgetRestart u.2 z hb).trans (SInvDom.dl_updateTimeStamp σ u hu) + have hfo := SInvF.oi_updateTimeStamp σ ⟨rfl, hf⟩ u hu + obtain ⟨he, hf'⟩ := SInvF.oi_checkBudgetRestart u.2 hfo z hb + have hi : u.2.idleThread = σ.idleThread := SInvG.idk_updateTimeStamp σ rfl u hu + have hj := SInvG.idk_checkBudgetRestart u.2 hi z hb + exact ⟨SInvDom.domListOK_of_dlV ed hd, fun o ho => hf' o (he.symm.trans ho), hj⟩ + +private theorem nkc_completeSignalS3 (n t : ObjRef) : NKC (completeSignal (Z := Unit) n t) := by + unfold completeSignal + repeat' first + | exact nkc_asUser _ _ | exact nkc_maybeDonateScS3 _ _ | exact nkc_getSkObjRef _ _ _ + | exact nkc_refillUnblockCheck _ | nkc_step | dsimp only [letFun] + +set_option maxHeartbeats 4000000 in +private theorem nkc_vmEnterS3 (t : ObjRef) : NKC (vmEnterF (Z := Unit) t) := by + unfold vmEnterF getTcbVcpu vmFullReply + repeat' first + | exact nkc_asUser _ _ | exact nkc_vcpuUpdateV _ _ | exact nkc_vmReportF _ _ + | exact nkc_completeSignalS3 _ _ | exact nkc_setThreadState _ _ | nkc_step | dsimp only [letFun] + +open SInvDom in +set_option maxHeartbeats 4000000 in +/-- VMEnter preserves the abstract finish invariants and creates no TCB. +Its transition into `runningVM` is covered without a native-only premise. +[J] The domain list, FPU owner and idle vtable before VMEnter. -/ +theorem vmEnterFinishInvariants (t : ObjRef) {σ : State Unit} {z : Unit × State Unit} + (hm : z ∈ (vmEnterF (Z := Unit) t σ).1) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) : + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ IdleVt z.2 ∧ KSub σ z.2 := by + have hd : DL (vmEnterF (Z := Unit) t) := by unfold vmEnterF; dl_auto + have ho : OWK (vmEnterF (Z := Unit) t) := by + unfold vmEnterF completeSignal vmReportF getTcbVcpu vcpuUpdate setVcpu vmFullReply vmFaultIpNext getVcpu + owk_auto + have hc : CRK (vmEnterF (Z := Unit) t) := by + unfold vmEnterF completeSignal vmReportF getTcbVcpu vcpuUpdate setVcpu vmFullReply vmFaultIpNext getVcpu + crk_auto + have hk := nkc_vmEnterS3 t σ _ hm + have he : z.2.archState.x64CurrentFpuOwner = σ.archState.x64CurrentFpuOwner := congrArg Prod.fst (ho σ _ hm) + have hi : z.2.idleThread = σ.idleThread := congrArg (fun q => q.2.2.2.1) (hc σ _ hm) + exact ⟨domListOK_of_dlV (hd σ _ hm) hDom, FpuOwnOK.ofCKeep hFo he hk, IdleVt.ofCKeep hIdle hi hk, hk.ks⟩ + +end Spec + +open Fl4Spec in +private theorem savedMaskAck (σ : State Unit) (i : Irq) : + doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i) σ = + ({((), maskMs σ i true)}, False) := by + apply doMachineOp_single + unfold maskInterrupt ackInterrupt + rw [Notif.nd_bind_one (by rfl)] + rfl + +set_option maxHeartbeats 4000000 in +/-- Keep the concrete signal-oracle agreement in the paired outcome of the +saved-vector delivery. The inactive line only masks and acknowledges, so its +store and the heap read by the oracle are unchanged. +[J] `VmOrc` and the machine relation hold before delivery. +[P] The native saved-IRQ platform facts. [S] The saved vector is non-IPI. -/ +theorem VmEptState.handleSavedIrqOrcS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hM : MachPlat arI MR) (hMK : MRKeep arI MR) (hIA : IrqPlatA arI AR) (hPA : PPPlatA arI AR) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) (hMR : MR a σ.machineState) + (w : U64) (i : Fl4Spec.Irq) (hs : SavedIrqScope w i) : + interrupt.handle_interrupt st arI s a w ⦃ r => + ∃ z ∈ (Fl4Spec.handleSavedIrqVF (Z := Unit) on rc i σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ VmOrc st on rc r.1 z.2 ∧ MR r.2 z.2.machineState ⦄ := by + obtain ⟨hwi, h126, h127⟩ := hs + have hwide := setWidth8_eq hwi + have h256 : w.val < 256 := by rw [hwi]; exact i.isLt + have hwne126 : w.val ≠ 126 := fun e => h126 (BitVec.eq_of_toNat_eq (hwi.symm.trans e)) + have hwne127 : w.val ≠ 127 := fun e => h127 (BitVec.eq_of_toNat_eq (hwi.symm.trans e)) + by_cases hin : i ≤ Fl4Spec.maxIRQ ∧ σ.interruptStates i = .irqInactive + · obtain ⟨hle, hst⟩ := hin + have hn := BitVec.le_def.1 hle + change i.toNat ≤ 125 at hn + have hlow : ¬ w > 127#u64 := by scalar_tac + have h128 : w.val < 128 := by omega + have B := J.1.1 + have hR : IrqRel (L.mem s) (L.ksb s) σ := B.xtop.fin.1.1.2.2.2.1 + have heSt := hR.st w h128 + have hei : (BitVec.ofNat 8 w.val : Fl4Spec.Irq) = i := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat, hwi, Nat.mod_eq_of_lt i.isLt] + rw [hei, hst] at heSt + have hz := (irqSt_inactive _).1 heSt.symm + unfold interrupt.handle_interrupt + rw [irq_max_eq, bind_ok, if_neg hlow, irq_state_eq', bind_ok, hz] + have hn1 : (0#u64 : U64) ≠ 1#u64 := by decide + have hn2 : (0#u64 : U64) ≠ 2#u64 := by decide + have hn3 : (0#u64 : U64) ≠ 3#u64 := by decide + simp only [irqSig_eq, irqTimer_eq, irqIpi_eq, irqInactive_eq, if_neg hn1, if_neg hn2, if_neg hn3, + if_pos rfl, bind_ok, uncurry_apply_pair] + obtain ⟨a1, he, hm⟩ := P1.spec_ex_fd (hM.mask a w true σ.machineState hMR h256) + rw [hwide] at hm + have ha1 := hARI.mask _ _ _ B.ar (Or.inr (Or.inl ⟨w, true, he⟩)) + rw [he] + simp only [if_true, bind_ok, uncurry_apply_pair] + refine WP.spec_bind (WP.spec_and (hIA.ackAR a1 w _ ha1) (hM.ack a1 w _ hm)) ?_ + intro a2 ha2 + obtain ⟨Lst2, B2, F2⟩ := B.msArJ (maskMs σ i true).machineState a2 rfl (fun _ => ha2.1) + have hmem : ((), maskMs σ i true) ∈ (Fl4Spec.handleSavedIrqVF (Z := Unit) on rc i σ).1 := by + rw [Fl4Spec.handleSavedIrqVF_inactive on rc i σ hle hst, savedMaskAck] + exact Set.mem_singleton _ + exact (WP.spec_ok _).2 ⟨_, hmem, (J.ofNat (V' := Z.vs) B2 F2).anyX arI, hO, ha2.2⟩ + · have h : i > Fl4Spec.maxIRQ ∨ σ.interruptStates i ≠ .irqInactive := by + by_cases hi : i > Fl4Spec.maxIRQ + · exact Or.inl hi + · exact Or.inr fun hst => hin ⟨BitVec.not_lt.1 hi, hst⟩ + rw [Fl4Spec.handleSavedIrqVF_eq on rc i σ h] + refine WP.spec_mono (J.handleIrqVFk hARI hAT hVA hARI.toARKeep hPA.minBudget + hO hMR w i hwi hwne126 hwne127 + (fun a1 c ha => hIA.ackAR a1 w c ha) + (fun a0 a1 σ0 hm he => ?_) (fun a1 ms hm => hM.ack a1 w ms hm) + (msig_sendSignalVF hAT hM hMK on rc)) ?_ + · have hm1 := hM.mask a0 w true σ0.machineState hm h256 + rw [he, WP.spec_ok, hwide] at hm1 + exact hm1 + · rintro res (⟨hst, hfail⟩ | ⟨z, hz, ⟨⟨V1, Ac1, Lst1, D1, J1⟩, hOrc⟩, hm⟩) + · have hgt := h.resolve_right (fun hne => hne hst) + rw [Fl4Spec.handleInterruptVF, if_pos hgt, savedMaskAck] at hfail + exact hfail.elim + · exact ⟨z, hz, J1.anyX arI, hOrc, hm⟩ + +private theorem VmEptState.savedIrqPrefixS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hM : MachPlat arI MR) (hMK : MRKeep arI MR) (hIA : IrqPlatA arI AR) (hPA : PPPlatA arI AR) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) (hMR : MR a σ.machineState) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {w : U64} {irq : Option Fl4Spec.Irq} (hw : SavedIrqAt w irq) : + (if w != syscall.NO_IRQ then interrupt.handle_interrupt st arI s a w else ok (s, a)) ⦃ r => + ∃ z ∈ (Fl4Spec.NonDet.maybeM (Fl4Spec.handleSavedIrqVF (Z := Unit) on rc) irq σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ VmOrc st on rc r.1 z.2 ∧ + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ IdleVt z.2 ⦄ := by + cases irq with + | none => + change w = syscall.NO_IRQ at hw + subst w + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨((), σ), Set.mem_singleton _, J.anyX arI, hO, hDom, hFo, hIdle⟩ + | some i => + have hne : w ≠ syscall.NO_IRQ := by + intro he + have hlt := i.isLt + have hwi : w.val = i.toNat := hw.1 + rw [he] at hwi + unfold syscall.NO_IRQ at hwi + change 18446744073709551615 = i.toNat at hwi + omega + have hb : (w != syscall.NO_IRQ) = true := by simpa using hne + rw [hb, if_pos rfl] + refine WP.spec_mono (J.handleSavedIrqOrcS3 hARI hAT hVA hM hMK hIA hPA hO hMR w i hw) ?_ + rintro r ⟨z, hz, hv, hOrc, _⟩ + exact ⟨z, hz, hv, hOrc, savedIrqFinishInvariants on rc i hz hDom hFo hIdle⟩ + +set_option maxHeartbeats 4000000 in +/-- Stalled guest exit with an optional saved vector. The finish receives +the invariants at the paired delivery outcome and the concrete entry +oracle pair is carried through it. +[J] The domain list, FPU owner, idle vtable and machine relation at entry. +[P] The native FPU, scheduler and IRQ platform facts. +[S] Saved IRQs exclude the cross-core IPI lines, as in Stage 2. -/ +theorem VmEptState.vcpuExitStalledS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hM : MachPlat arI MR) (hMK : MRKeep arI MR) (hIA : IrqPlatA arI AR) (hPA : PPPlatA arI AR) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) + (hDL : SetDlDet) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hMR : MR a σ.machineState) (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {w : U64} {irq : Option Fl4Spec.Irq} (hw : SavedIrqAt w irq) : + syscall.handle_vcpu_exit st arI s a w true ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4VcpuExitSavedVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) irq true σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + have hO : VmOrc st (v3OnCore st s) (v3RemCur st s) s σ := fun _ _ _ _ => ⟨rfl, fun _ => rfl⟩ + have hstalled : Fl4Spec.vcpuExitPrefixVF (Z := Unit) true σ = ({((), σ)}, False) := by + simp [Fl4Spec.vcpuExitPrefixVF, Fl4Spec.NonDet.unless, Fl4Spec.NonDet.when] <;> rfl + unfold syscall.handle_vcpu_exit + simp only [if_true, bind_ok, uncurry_apply_pair] + refine WP.spec_bind (J.savedIrqPrefixS3 hARI hAT hVA hM hMK hIA hPA hO hMR hDom hFo hIdle hw) ?_ + rintro ⟨s', a'⟩ ⟨z, hz, hVE, hOrc, hDom', hFo', hIdle'⟩ + obtain ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ := hVE + have F := J'.1.1.kfinVE_ofFacts hAT hFpu hFW hDom' hFo' hIdle' hOrc + refine WP.spec_mono (J'.finishVF hAT hVA hARI.toARKeep hPA.minBudget F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, _, _, _, _, J''⟩ + refine ⟨σ', ?_, J''.anyX arI⟩ + unfold Fl4Spec.fl4VcpuExitSavedVF + rw [Notif.nd_bind_one hstalled] + exact mem_bind_nd hz hmem + +private theorem handleVcpuExitEqS3 (s : S) (a : A) (w : U64) (stalled : Bool) : + syscall.handle_vcpu_exit st arI s a w stalled = + (do let r ← vcpuExitFront (st := st) arI s a w stalled; syscall.finish st arI r.1 r.2) := by + unfold syscall.handle_vcpu_exit vcpuExitFront + simp only [Std.bind_assoc] + congr 1 + +set_option maxHeartbeats 4000000 in +/-- Unstalled guest exit through the timestamp, VCPU check, budget charge +and optional saved vector. The charge's slot frame transports the idle +vtable to the saved-vector state. Entry oracles agree after the check and +charge by the matched scheduler relations and the native running-VM frame. +The IRQ proof carries them through delivery. +[J] The native time, refill, current-thread and finish invariants and the +machine relation at entry. [P] The native clock, FPU, scheduler and IRQ +platform facts. [S] Saved IRQs exclude the cross-core IPI lines. -/ +theorem VmEptState.vcpuExitUnstalledS3 [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hM : MachPlat arI MR) (hMK : MRKeep arI MR) (hIA : IrqPlatA arI AR) (hPA : PPPlatA arI AR) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) + (hDL : SetDlDet) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hMR : MR a σ.machineState) (hVTime : ValidMachineTime σ) (hOff : OffReady σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {w : U64} {irq : Option Fl4Spec.Irq} (hw : SavedIrqAt w irq) : + syscall.handle_vcpu_exit st arI s a w false ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4VcpuExitSavedVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) irq false σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + let on := v3OnCore st s + let rc := v3RemCur st s + let saved := Fl4Spec.NonDet.maybeM (Fl4Spec.handleSavedIrqVF (Z := Unit) on rc) irq + let pre := Fl4Spec.vcpuExitPrefixVF (Z := Unit) false >>= fun _ => saved + have hpre : pre = (specCheckAfterTimestamp >>= fun _ => + (Fl4Spec.checkBudget >>= fun _ => pure ()) >>= fun _ => saved) := by + dsimp only [pre] + unfold Fl4Spec.vcpuExitPrefixVF Fl4Spec.NonDet.unless specCheckAfterTimestamp + simp only [Bool.false_eq_true, ↓reduceIte, Endp.nd_bind_assoc_fun] + let deliver (p : S × A) : Result (S × A) := + if w != syscall.NO_IRQ then interrupt.handle_interrupt st arI p.1 p.2 w else ok p + let concrete : Result (S × A) := do + let (s1, a1) ← checkAfterTimestamp st arI s a + let (_, s2, a2) ← sched.check_budget st arI s1 a1 + deliver (s2, a2) + have hFront : vcpuExitFront (st := st) arI s a w false = concrete := by + obtain ⟨⟨s1, a1⟩, hTime, _⟩ := spec_exists (P0.sched.update_timestamp.p0 st arI s a L.total hAT) + obtain ⟨ct, hCur, _⟩ := spec_exists (P0.sched.cur_thread.p0 st s1 L.total) + obtain ⟨⟨s2, a2⟩, hCheck, _⟩ := spec_exists (P0.vcpu.check.p0 st arI s1 a1 ct L.total hAT) + obtain ⟨⟨run, s3, a3⟩, hBudget, _⟩ := spec_exists (P0.sched.check_budget.p0 st arI s2 a2 L.total hAT) + simp only [vcpuExitFront, Bool.false_eq_true, ↓reduceIte, concrete, checkAfterTimestamp, deliver, + hTime, hCur, hCheck, hBudget, bind_ok, uncurry_apply_pair] + have hPrefix : concrete ⦃ r => ∃ x ∈ (pre σ).1, + VEAnyX (L := L) arI AR r.1 r.2 x.2 ∧ VmOrc st on rc r.1 x.2 ∧ + DomListOK x.2 ∧ FpuOwnOK x.2 ∧ IdleVt x.2 ⦄ := by + unfold concrete + refine WP.spec_bind (J.checkTimestampHole hARI.toARKeep hAT hVA hnd hM hni hnq hMR hVTime hOff) ?_ + rintro ⟨s1, a1⟩ ⟨sc, σc, Ac', Lst', σr, hs, Jc, Hh, _, hOrc, hMR1, hOff1, hEntered, hnq1⟩ + have Bc := Jc.1.1 + have hCB := Hh.checkBudgetNat hEntered hOff1 (Or.inr hnq1) hAT hnd hmrt hPA.minBudget hmax htu + have hCBM := MK.sched_check_budget (s := s1) L.total hAT hMK hMR1 + obtain ⟨hDomR, hFoR, hIdleR⟩ := checkTimestampFinishInvariants hnd hs hDom hFo hIdle + obtain ⟨ms, t, c, _, hσr⟩ := Hh.spec + have hIdleC : IdleVt σc := by rw [hσr] at hIdleR; simpa only [IdleVt] using hIdleR + have hIdlePtr : σr.idleThread = σc.idleThread := by simpa only [hσr] + refine WP.spec_bind (WP.spec_and hCB hCBM) ?_ + rintro ⟨b, s2, a2⟩ ⟨⟨z, hz, _, V', Ac2, Lst2, D2, B2, F2⟩, hMR2⟩ + have J2 := Jc.ofNat B2 F2 + have hO2 := Bc.vmOrcAcross B2 hOrc F2.fk.1.2.2.1 + have hMF := MachF.mfk_checkBudget σr _ hz + simp only [MachF.msF, Prod.mk.injEq] at hMF + obtain ⟨hm1, hm2, hm3, hm4, _⟩ := hMF + have hMRz : MR a2 z.2.machineState := hM.frame _ _ _ hMR2 hm1 hm2 hm3 hm4 + obtain ⟨hDomZ, hFoZ, hIdZ⟩ := checkBudgetFinishInvariants hz hDomR hFoR + have hIdleZ := idleVt_ofSlots Bc.xtop Bc.jpl Bc.zH.1 B2.xtop F2.ss (hIdZ.trans hIdlePtr) hIdleC + change deliver (s2, a2) ⦃ r => _ ⦄ + refine WP.spec_mono (J2.savedIrqPrefixS3 hARI hAT hVA hM hMK hIA hPA hO2 hMRz hDomZ hFoZ hIdleZ hw) ?_ + rintro r ⟨x, hx, hVE, hO, hDom', hFo', hIdle'⟩ + refine ⟨x, ?_, hVE, hO, hDom', hFo', hIdle'⟩ + rw [hpre, Notif.nd_bind_one hs, Endp.nd_bind_assoc_fun] + refine mem_bind_nd hz ?_ + rw [Notif.nd_pure_bind'] + exact hx + rw [handleVcpuExitEqS3, hFront] + refine WP.spec_bind hPrefix ?_ + rintro ⟨s', a'⟩ ⟨z, hz, hVE, hOrc, hDom', hFo', hIdle'⟩ + obtain ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ := hVE + have F := J'.1.1.kfinVE_ofFacts hAT hFpu hFW hDom' hFo' hIdle' hOrc + refine WP.spec_mono (J'.finishVF hAT hVA hARI.toARKeep hPA.minBudget F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, _, _, _, _, J''⟩ + refine ⟨σ', ?_, J''.anyX arI⟩ + simpa only [pre, saved, on, rc, Fl4Spec.fl4VcpuExitSavedVF, Endp.nd_bind_assoc_fun] using mem_bind_nd hz hmem + +open Fl4Spec in +private theorem memHostIrqEventS3 {on rc : ObjRef → Bool} {σ σ3 : State Unit} + {z : Bool × State Unit} {y : Unit × State Unit} {i : Irq} + (hz : z ∈ ((updateTimeStamp (Z := Unit) >>= fun _ => checkBudget) σ).1) + (hr : doMachineOp (Z := Unit) (getActiveIRQ false) z.2 = ({(some i, σ3)}, False)) + (hy : y ∈ (handleInterruptVF (Z := Unit) on rc i σ3).1) : + ((.ok (), y.2) : Except Unit Unit × State Unit) ∈ ((fl4HandleEventVF on rc true .interrupt).run σ).1 := by + obtain ⟨u, hu, hb⟩ := mem_bind_elim hz + unfold fl4HandleEventVF maybeHandleInterruptVF + obtain ⟨⟨⟩, σ'⟩ := y + exact mem_liftE (mem_bind_nd hu (mem_bind_nd hb + (mem_bind_nd (x := (some i, σ3)) (by rw [hr]; exact Set.mem_singleton _) hy))) + +set_option maxHeartbeats 4000000 in +/-- Fresh host IRQ with the finish discharged at the paired state after +the budget and delivery. This keeps the idle-thread budget case and the +entry's concrete signal oracles. +[J] The entry action, ready queues, clock, refill order, masks, domain +list, FPU owner and idle vtable. [P] Native scheduler, FPU and fresh IRQ +platform facts. [S] The native IrqHw line excludes both cross-core IPIs. -/ +theorem VmEptState.interruptEntryS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hM : MachPlat arI MR) (hMK : MRKeep arI MR) (hIA : IrqPlatA arI AR) (hPA : PPPlatA arI AR) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) + (hDL : SetDlDet) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hres : σ.schedulerAction = .resumeCurThread) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hTime : ValidMachineTime σ) (hOff : OffReady σ) (hMask : ValidIrqMasks σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {a' : A} {w : U64} (hw : IrqHw AR MR a a' σ w) : + syscall.handle_interrupt_entry st arI s a' w ⦃ r => ∃ σ', + ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true .interrupt σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + obtain ⟨i, hli, hwi⟩ := hw.line + let on := v3OnCore st s + let rc := v3RemCur st s + have hOrc : VmOrc st on rc s σ := fun _ _ _ _ => ⟨rfl, fun _ => rfl⟩ + obtain ⟨Lst0, B0, F0⟩ := J.1.1.msArJ σ.machineState a' rfl (hw.ar _) + have J0 := J.ofNat (V' := Z.vs) B0 F0 + let pre := (Fl4Spec.NonDetE.handleE (Fl4Spec.fl4HandleEventVF (Z := Unit) on rc true .interrupt) + (fun _ => Fl4Spec.withoutPreemption (Fl4Spec.preemptionPathVF on rc))).run + let concrete : Result (S × A) := + Std.bind (Std.bind (sched.update_timestamp st arI s a') (uncurry fun s1 a1 => + sched.check_budget st arI s1 a1)) (uncurry fun _ => uncurry fun s2 a2 => + interrupt.handle_interrupt st arI s2 a2 w) + have hPrefix : concrete ⦃ r => ∃ x ∈ (pre σ).1, + VEAnyX (L := L) arI AR r.1 r.2 x.2 ∧ VmOrc st on rc r.1 x.2 ∧ + DomListOK x.2 ∧ FpuOwnOK x.2 ∧ IdleVt x.2 ⦄ := by + refine WP.spec_bind (WP.spec_and + (J0.entryBudgetFrames hOrc (nowAgree_gai hM hw.mr hTime) hOff hres hnq hARI.toARKeep hAT hnd + hmrt hPA.minBudget hmax htu) + (ut_cb_mr hM hMK hAT hw.mr s)) ?_ + rintro ⟨b, s2, a2⟩ ⟨⟨z, hz, V2, Ac2, Lst2, D2, J2, F2, hO⟩, hMR2⟩ + obtain ⟨⟨u, σu⟩, hu, hcb⟩ := mem_bind_elim hz + rw [Sched.updateTimeStamp_eq' hnd (doMachineOp_single (getCurrentTime_eq σ.machineState))] at hu + have eu := congrArg Prod.snd (Set.mem_singleton_iff.1 hu) + dsimp only at eu + have hms : σu.machineState = gctState σ.machineState := by rw [eu] + have his : σu.interruptStates = σ.interruptStates := by rw [eu] + have hdU : DomListOK σu := SInvDom.domListOK_of_dlV (σ := σ) (σ' := σu) + (by simp only [SInvDom.dlV, eu]) hDom + have hfU : FpuOwnOK σu := by rw [eu]; simpa only [FpuOwnOK] using hFo + obtain ⟨hDomZ, hFoZ, hIdZ⟩ := checkBudgetFinishInvariants hcb hdU hfU + have hIdlePtr : σu.idleThread = σ.idleThread := by rw [eu] + have hIdleZ := idleVt_ofSlots B0.xtop B0.jpl B0.zH.1 J2.1.1.xtop F2.ss (hIdZ.trans hIdlePtr) hIdle + have hk := MachF.mfk_checkBudget σu _ hcb + have huMask : ValidIrqMasks σu := by + intro j hj + rw [hms] + exact hMask j (his ▸ hj) + have h2Mask := msF_vim huMask hk + simp only [MachF.msF, Prod.mk.injEq] at hk + obtain ⟨hm1, hm2, hm3, hm4, -⟩ := hk + have hl2 : gaiLine false z.2.machineState = some i := by + rw [gaiLine_congr (ms := σ.machineState) (by rw [hm1, hms]; rfl) (by rw [hm2, hms]; rfl)] + exact hli + let σ3 := { z.2 with machineState := gaiState z.2.machineState } + have hDom3 : DomListOK σ3 := SInvDom.domListOK_of_dlV (σ := z.2) (σ' := σ3) rfl hDomZ + have hFo3 : FpuOwnOK σ3 := by simpa only [σ3, FpuOwnOK] using hFoZ + have hIdle3 : IdleVt σ3 := by simpa only [σ3, IdleVt] using hIdleZ + have hO3 : VmOrc st on rc s2 σ3 := by simpa only [σ3, VmOrc, OrcAg] using hO + have hRead : Fl4Spec.doMachineOp (Z := Unit) (Fl4Spec.getActiveIRQ false) z.2 = ({(some i, σ3)}, False) := by + rw [doMachineOp_single (getActiveIRQ_eq false z.2.machineState), hl2] + have hAct : σ3.interruptStates i ≠ .irqInactive := by + intro hin + have := h2Mask i hin + rw [gaiLine_unmasked hl2] at this + cases this + have hMR3 : MR a2 σ3.machineState := hM.frame a2 _ _ hMR2 + (by show z.2.machineState.irqMasks = _; rw [hm1, hms]; rfl) + (by show z.2.machineState.irqState + 1 = _; rw [hm2, hms]; rfl) + (by show z.2.machineState.timeState = _; rw [hm3, hms]; rfl) + (by show z.2.machineState.lastMachineTime = _; rw [hm4, hms]; rfl) + obtain ⟨Lst3, B3, F3⟩ := J2.1.1.msArJ σ3.machineState a2 rfl id + have J3 := J2.ofNat (V' := V2) B3 F3 + have hs : SavedIrqScope w i := ⟨hwi, + fun e => hw.n126 (by simpa [e] using hwi), + fun e => hw.n127 (by simpa [e] using hwi)⟩ + refine WP.spec_mono (J3.handleSavedIrqOrcS3 hARI hAT hVA hM hMK hIA hPA hO3 hMR3 w i hs) ?_ + rintro r ⟨y, hy, hv, hOrcY, _⟩ + obtain ⟨hDomY, hFoY, hIdleY⟩ := savedIrqFinishInvariants on rc i hy hDom3 hFo3 hIdle3 + rw [Fl4Spec.handleSavedIrqVF_eq on rc i σ3 (Or.inr hAct)] at hy + exact ⟨(.ok (), y.2), mem_handleE_ok (memHostIrqEventS3 hz hRead hy), hv, hOrcY, hDomY, hFoY, hIdleY⟩ + have hFinish : (do let r ← concrete; syscall.finish st arI r.1 r.2) ⦃ r => ∃ σ', + ((), σ') ∈ ((pre >>= fun _ => Fl4Spec.fl4KernelExitVF on rc) σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + refine WP.spec_bind hPrefix ?_ + rintro ⟨sf, af⟩ ⟨z, hz, hVE, hOrcZ, hDomZ, hFoZ, hIdleZ⟩ + obtain ⟨Vf, Xf, XLf, Zf, Hf, Csf, Acf, Lstf, Df, _, Jf⟩ := hVE + have F := Jf.1.1.kfinVE_ofFacts hAT hFpu hFW hDomZ hFoZ hIdleZ hOrcZ + refine WP.spec_mono (Jf.finishVF hAT hVA hARI.toARKeep hPA.minBudget F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, _, _, _, _, J'⟩ + exact ⟨σ', mem_bind_nd hz hmem, J'.anyX arI⟩ + rw [handle_interrupt_entry_eq] + obtain ⟨⟨s1, a1⟩, h1, -⟩ := spec_exists (P0.sched.update_timestamp.p0 st arI s a' L.total hAT) + obtain ⟨⟨b, s2, a2⟩, h2, -⟩ := spec_exists (P0.sched.check_budget.p0 st arI s1 a1 L.total hAT) + obtain ⟨⟨s3, a3⟩, h3, -⟩ := spec_exists (P0.interrupt.handle_interrupt.p0 st arI s2 a2 w L.total hAT) + have hce : concrete = ok (s3, a3) := by + dsimp only [concrete] + simp only [h1, h2, h3, bind_ok, uncurry_apply_pair] + simp only [hce, bind_ok, uncurry_apply_pair, pre, on, rc] at hFinish + simpa only [h1, h2, h3, bind_ok, uncurry_apply_pair, Fl4Spec.fl4CallKernelVF] using hFinish + +/-- `enterRunning` uses the same scheduler thread set on both sides of the +transition. Its result therefore agrees with the entry oracle pair even +on the newly created `runningVM` thread. [J] The selected TCB starts running +with a bound VCPU, as in `enterRunning`. -/ +theorem VmEptState.enterRunningOrcS3 + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hr : x.tcbState = .running) (hBound : L.mem s (tVcpuA t) ≠ 0#u64) : + sched.set_thread_state st s t sched.TS_RUNNING_VM ⦃ r => + ∃ σ' Lst', Fl4Spec.setThreadState (Z := Unit) t.bv .runningVM σ = ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D r a σ' ∧ + VmOrc st (v3OnCore st s) (v3RemCur st s) r σ' ⦄ := by + refine WP.spec_mono (J.enterRunning ht hk hr hBound) ?_ + rintro s' ⟨σ', Lst', he, J'⟩ + refine ⟨σ', Lst', he, J', J.1.1.vmOrcOfEntry J'.1.1 ?_⟩ + obtain ⟨Q, R, _, hsc⟩ := J.1.1.xtop.fin.1.1.1.lists + intro p y hky _ + obtain ⟨u, hu, rfl⟩ := J'.1.1.obj.tcb p y hky + obtain ⟨old, hold, _⟩ := hsc.tcb u hu + exact ⟨old, hold⟩ + +open Fl4Spec in +private theorem tcbVcpuOf_eqHeap {before after : State Unit} + (h : after.kheap = before.kheap) (t : ObjRef) : + tcbVcpuOf after t = tcbVcpuOf before t := by + have hat (v : ObjRef) : vcpuAt after v = vcpuAt before v := by + unfold vcpuAt + rw [h] + simp only [tcbVcpuOf, hat] + +open Fl4Spec in +set_option maxHeartbeats 4000000 in +private theorem VmEptState.vmEnterGuardedFinishS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hnd : numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) + (hDL : SetDlDet) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {on rc : ObjRef → Bool} (hOn : ∀ p y, σ.kheap p = some (.tcb y) → on p = true) + {t : U64} (hcur : σ.curThread = t.bv) + (hRun : ∀ x, σ.kheap t.bv = some (.tcb x) → x.tcbState = .running) + (hSel : isVmEnterCall fl4SysVMEnterNum σ = true) : + (do + let (run, s', a') ← syscall.budget_ok st arI s a + let (s2, a2) ← if run then vcpu.enter st arI s' a' t else ok (s', a') + syscall.finish st arI s2 a2) ⦃ r => ∃ σ', + ((), σ') ∈ (fl4CallKernelVF (Z := Unit) on rc true (.unknownSyscall fl4SysVMEnterNum) σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + have hBound : (tcbVcpuOf σ σ.curThread).isSome := (decide_eq_true_eq.mp hSel).2.2 + refine WP.spec_bind (J.budgetFramesV hni hnq hM hMR hVM hOff hARI.toARKeep hAT hnd hmrt hmb hmax htu) ?_ + rintro ⟨run, sb, ab⟩ ⟨σb, hb, Vb, Acb, Lstb, Db, Jb, Fb⟩ + obtain ⟨hdB, hfB, hiB⟩ := specBudgetFinishInvariants hb hDom hFo + have hivB := idleVt_ofSlots J.1.1.xtop J.1.1.jpl J.1.1.zH.1 Jb.1.1.xtop Fb.ss hiB hIdle + let body := NonDet.gets (fun z : State Unit => z.curThread) >>= vmEnterF + have hArm : (if run then vcpu.enter st arI sb ab t else ok (sb, ab)) ⦃ r => + ∃ z ∈ (NonDet.when run body σb).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ DomListOK z.2 ∧ FpuOwnOK z.2 ∧ IdleVt z.2 ∧ + (∀ p y, z.2.kheap p = some (.tcb y) → y.tcbState = .runningVM → on p = true) ⦄ := by + cases run + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨((), σb), Set.mem_singleton _, Jb.anyX arI, hdB, hfB, hivB, ?_⟩ + intro p y hy hr + obtain ⟨old, hOld, _⟩ := Fb.fk.1.2.2.1 p y hy hr + exact hOn p old hOld + · simp only [↓reduceIte] + obtain ⟨ek, ect, eid, _, esa, _⟩ := specBudget_true_shape hnd hb + have hniB : Entered σb := ⟨by rw [ect, eid]; exact hni.1, esa.trans hni.2⟩ + have hcurB : σb.curThread = t.bv := ect.trans hcur + obtain ⟨hx, xb, hk⟩ := Jb.1.1.curEq (cur_thread_eq (L := L) sb) + rw [← hx, hcurB] at hk + have hk0 : σ.kheap t.bv = some (.tcb xb) := ek ▸ hk + have hv : (tcbVcpuOf σb t.bv).isSome := by + rw [tcbVcpuOf_eqHeap ek, ← hcur] + exact hBound + refine WP.spec_mono (Jb.vcpuEnterStableJ hARI.toARKeep hAT hVA hCap hniB hcurB hk (hRun xb hk0) hv) ?_ + rintro r ⟨σ', Ac', Lst', he, J'⟩ + have hz : ((), σ') ∈ (vmEnterF (Z := Unit) t.bv σb).1 := by rw [he]; exact Set.mem_singleton _ + obtain ⟨hd, hf, hi, _⟩ := vmEnterFinishInvariants t.bv hz hdB hfB hivB + refine ⟨((), σ'), ?_, J'.anyX arI, hd, hf, hi, ?_⟩ + · simpa only [NonDet.when, body, ite_true, nd_gets_bind, hcurB] using hz + · intro p y hy _ + obtain ⟨u, hu, rfl⟩ := J'.1.1.obj.tcb p y hy + obtain ⟨Q, R, _, hsc⟩ := J.1.1.xtop.fin.1.1.1.lists + obtain ⟨old, hOld, _⟩ := hsc.tcb u hu + exact hOn u.bv old hOld + refine WP.spec_bind hArm ?_ + rintro ⟨sf, af⟩ ⟨z, hz, hVE, hd, hf, hi, ho⟩ + obtain ⟨Vf, Xf, XLf, Zf, Hf, Csf, Acf, Lstf, Df, _, Jf⟩ := hVE + have hO : VmOrc st on rc sf z.2 := by + intro p y hk hr + have ep := ho p y hk hr + exact ⟨ep.trans (Jf.1.1.onCoreTcb hk).symm, fun h => by rw [ep] at h; cases h⟩ + have F := Jf.1.1.kfinVE_ofFacts hAT hFpu hFW hd hf hi hO + refine WP.spec_mono (Jf.finishVF hAT hVA hARI.toARKeep hmb F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, _, _, _, _, J'⟩ + refine ⟨σ', ?_, J'.anyX arI⟩ + obtain ⟨u, hu, hbu⟩ := mem_bind_elim hb + have hz' : ((), z.2) ∈ (NonDet.when run body σb).1 := by obtain ⟨⟨⟩, _⟩ := z; exact hz + have he : ((.ok (), z.2) : Except Unit Unit × State Unit) ∈ + ((fl4HandleEventVF on rc true (.unknownSyscall fl4SysVMEnterNum)).run σ).1 := by + rw [vmEnterEventGuard hSel] + exact mem_liftE (mem_bind_nd hu (mem_bind_nd hbu hz')) + unfold fl4CallKernelVF + exact mem_bind_nd (mem_handleE_ok he) hmem + +open Fl4Spec in +set_option maxRecDepth 1024 in +set_option maxHeartbeats 4000000 in +/-- The VMEnter syscall with the concrete entry oracle readings and paired +finish facts through both budget verdicts and both VMEnter continuations. +[J] The entry scheduler, clock, refill, idle and FPU invariants and the +machine relation. [P] The native architecture, timing and VM backend +agreements. [S] VMEnter has a bound VCPU, enabled backend and clear trap flag. -/ +theorem VmEptState.vmEnterEntryS3 + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hnd : numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = MIN_BUDGET ⦄) + (hmax : maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = ticksToUs v.bv ⦄) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hRun : ∀ ct, sched.cur_thread st s = ok ct → ZvCtRun σ ct) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + (hCode : ∀ i, arI.virtualization a = ok i → i.val = virtKind.code) + (hFlag : ∀ ct v, sched.cur_thread st s = ok ct → obj.Tcb.flags st ct (L.wr s (Endp.regA ct layout.reg.FAULT_IP) v) + ⦃ f => (f &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 ⦄) + (hEnter : ∀ x ∈ (fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1, + isVmEnterCall fl4SysVMEnterNum x.2 = true) : + syscall.handle_syscall st arI s a fl4_abi.syscall.SysVMEnter ⦃ r => ∃ σ', + FaultIpOutVF (v3OnCore st s) (v3RemCur st s) (.unknownSyscall fl4SysVMEnterNum) σ σ' ∧ VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + let on := v3OnCore st s + let rc := v3RemCur st s + have hct := cur_thread_eq (L := L) s + generalize L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) = t at hct + obtain ⟨x, hk, hcur, V', J0⟩ := J.faultIPEntry hct + let val := core.num.U64.wrapping_sub (L.mem s (Endp.regA t layout.reg.NEXT_IP)) 2#u64 + let s0 := L.wr s (Endp.regA t layout.reg.FAULT_IP) val + let σ0 := faultIPSt σ σ.curThread x + have hF : ((), σ0) ∈ (fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1 := by + rw [fl4SyscallFaultIP_eq hk] + exact Set.mem_singleton _ + have hSel : isVmEnterCall fl4SysVMEnterNum σ0 = true := hEnter ((), σ0) hF + have hGuards := decide_eq_true_eq.mp hSel + have hBack := hGuards.2.1 + have hBoundSpec : (tcbVcpuOf σ0 t.bv).isSome = true := by + have he : σ0.curThread = t.bv := hcur + rw [← he] + exact hGuards.2.2 + have hct0 : sched.cur_thread st s0 = ok t := by + have h := J0.1.1.xtop.fin.1.1.1.lists + obtain ⟨ct', hct', _⟩ := spec_exists (sched.cur_thread.spec' (L := L) s0) + have eq := (J0.1.1.curEq hct').1 + have et : ct' = t := VirtSanEq.u64_ext (eq.symm.trans hcur) + simpa only [et] using hct' + have ht0 := J0.1.1.thTcb (TopSR.cur_mem J0.1.1.xtop.fin.1 hcur.symm) + obtain ⟨i, hi, eI⟩ := spec_exists (virtualizationGuard (arI := arI) hAT hCode) + obtain ⟨v, hv, eV⟩ := spec_exists (J0.toB.boundIff ht0) + have hi0 : i ≠ 0#u64 := fun e => hBack (eI.1 e) + have hv0 : v ≠ 0#u64 := by + intro e + rw [eV.1 e] at hBoundSpec + cases hBoundSpec + have hOff0 := SInvR.G4.offReady_faultIPSt (t := σ.curThread) (x := x) hOff + have hBud := J0.budgetVX hni hnq hM hMR hVM hOff0 hARI.toARKeep hAT hnd hmrt hmb hmax htu + have hkF := (show NKC (fl4SyscallFaultIP (Z := Unit) σ.curThread) from nkc_asUser _ _) σ _ hF + have hDom0 : DomListOK σ0 := SInvDom.domListOK_of_dlV (σ := σ) (σ' := σ0) rfl hDom + have hFo0 : FpuOwnOK σ0 := FpuOwnOK.ofCKeep hFo rfl hkF + have hIdle0 : IdleVt σ0 := IdleVt.ofCKeep hIdle rfl hkF + have hOn : ∀ p y, σ0.kheap p = some (.tcb y) → on p = true := by + intro p y hp + obtain ⟨old, hOld⟩ := (hkF.ks p).2.1 y hp + exact J.1.1.onCoreTcb hOld + have hRun0 : ∀ y, σ0.kheap t.bv = some (.tcb y) → y.tcbState = .running := by + intro y hy + rw [← hcur] at hy + simp only [σ0, faultIPSt, updK, Function.update_self, Option.some.injEq, + KernelObject.tcb.injEq] at hy + subst y + exact hRun t hct x (hcur ▸ hk) + have hBody := J0.vmEnterGuardedFinishS3 hARI hAT hVA hCap hnd hT hRef hDL hFpu hFW hmrt hmb + hmax htu hni hnq hM hMR hVM hOff0 hDom0 hFo0 hIdle0 (rc := rc) hOn hcur hRun0 hSel + unfold syscall.handle_syscall + refine WP.spec_bind (Pₘ := fun r : Bool × S × A => ∃ σ', + FaultIpOutVF on rc (.unknownSyscall fl4SysVMEnterNum) σ σ' ∧ + VEAnyX (L := L) arI AR r.2.1 r.2.2 σ') ?_ ?_ + · unfold syscall.syscall + rw [hct, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s t layout.reg.NEXT_IP) ?_ + rintro nx rfl + simp only [lift, bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s t layout.reg.FAULT_IP val) ?_ + rintro sw rfl + refine WP.spec_bind (hFlag t val hct) ?_ + intro f hf0 + have hmin : fl4_abi.syscall.SysVMEnter < fl4_abi.syscall.SYSCALL_MIN := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SYSCALL_MIN + decide + simp only [lift, bind_ok, hf0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, if_pos hmin] + have hct0' : sched.cur_thread st (L.wr s (Endp.regA t layout.reg.FAULT_IP) val) = ok t := hct0 + have hv' : vcpu.bound st (L.wr s (Endp.regA t layout.reg.FAULT_IP) val) t = ok v := hv + have hi' : (i != 0#u64) = true := bne_iff_ne.mpr hi0 + have hv0' : (v != 0#u64) = true := bne_iff_ne.mpr hv0 + have h1 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugPutChar := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugPutChar + decide + have h2 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugDumpScheduler := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugDumpScheduler + decide + have h3 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugHalt := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugHalt + decide + have h4 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugSnapshot := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugSnapshot + decide + have h5 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugCapIdentify := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugCapIdentify + decide + have h6 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugNameThread := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugNameThread + decide + have h7 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugGetThreadAffinity := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugGetThreadAffinity + decide + have h8 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysDebugSendIPI := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysDebugSendIPI + decide + have h9 : fl4_abi.syscall.SysVMEnter ≠ fl4_abi.syscall.SysSetTLSBase := by + unfold fl4_abi.syscall.SysVMEnter fl4_abi.syscall.SysSetTLSBase + decide + unfold syscall.handle_unknown_syscall syscall.debug_syscall + simp only [hct0', hi, hv', bind_ok, fl4_abi.config.PRINTING, fl4_abi.config.DEBUG_BUILD, + fl4_abi.config.SET_TLS_BASE_SELF, fl4_abi.config.ENABLE_SMP_SUPPORT, + h1, h2, h3, h4, h5, h6, h7, h8, h9, hi', hv0', if_pos rfl, ↓reduceIte, + Bool.false_eq_true, uncurry_apply_pair] + have hLift : Std.bind _ (fun r : S × A => ok (false, r.1, r.2)) ⦃ r => ∃ σ', + FaultIpOutVF on rc (.unknownSyscall fl4SysVMEnterNum) σ σ' ∧ + VEAnyX (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := + WP.spec_bind hBody fun r ⟨σ', hmem, hVE⟩ => + (WP.spec_ok _).2 ⟨σ', ⟨((), σ0), hF, hmem⟩, hVE⟩ + obtain ⟨⟨run, sb, ab⟩, hBudget, _⟩ := spec_exists hBud + change syscall.budget_ok st arI (L.wr s (Endp.regA t layout.reg.FAULT_IP) val) a = + ok (run, sb, ab) at hBudget + have hBudgetRaw := hBudget + dsimp only [val] at hBudgetRaw + simp only [hBudget, hBudgetRaw, bind_ok, uncurry_apply_pair] at hLift ⊢ + have hEnter : (if run = true then vcpu.enter st arI sb ab t else ok (sb, ab)) ⦃ _ => True ⦄ := by + cases run + · exact (WP.spec_ok _).2 trivial + · exact P0.vcpu.enter.p0 st arI sb ab t L.total hAT + obtain ⟨⟨s2, a2⟩, he, _⟩ := spec_exists hEnter + obtain ⟨⟨s3, a3⟩, hf, _⟩ := spec_exists (P0.syscall.finish.p0 st arI s2 a2 L.total hAT) + simpa only [he, hf, bind_ok, uncurry_apply_pair] using hLift + · rintro ⟨_, sf, af⟩ ⟨σ', hm, hV⟩ + exact (WP.spec_ok _).2 ⟨σ', hm, hV⟩ + + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Frame.lean b/proofs/Fl4Proofs/P3/VmEntryS3Frame.lean new file mode 100644 index 000000000..b3da3a1e7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Frame.lean @@ -0,0 +1,76 @@ +/- +A selected native Frame cap at a VM entry, every label: PageMap and PageUnmap +(`VmEntryS3FrameMap`), PageMapEPT (`selectedFrameMapEpt`), DeriveSubframe, TestAndClearAccessed, GetAddress and the +illegal labels (VmDisp's frame leaves, generalised in place to any mapping state). +[P] Native VSpace architecture refinement and VM agreement. No existing statement changed. +-/ +import Fl4Proofs.P3.VmEntryS3FrameMap +import Fl4Proofs.P3.FrontEndLabel +import Fl4Proofs.P3.XTopJointVsMap + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched Fl4Proofs.P1 + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **A selected native Frame cap, every label** (module docstring): PageMap / PageUnmap by +`selectedNativeFrameS3`, PageMapEPT by `selectedFrameMapEpt`, DeriveSubframe, TestAndClearAccessed and GetAddress by the +VmDisp leaves, every other label `IllegalOperation`. +[J] The running caller at the reached dispatch. [P] Native VSpace refinement and VM agreement. -/ +theorem VmEptState.selectedFrameS3 [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + {lbl ct cptr len buf p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Frame p sz rt d m ep asid va) ex args exs) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) (hRun : ZvCtRun σ ct) + {MR : A → Fl4Spec.MachineState → Prop} + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt d m ep asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Frame p sz rt d m ep asid va) exs buffer) s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Frame p sz rt d m ep asid va) exs buffer) ct s res ⦄ := by + by_cases hE : lbl = fl4_abi.label.X86PageMapEPT + · subst hE + exact WP.spec_mono (J.selectedFrameMapEpt hni hRef hVA q buffer on rc blk call don fp) fun _ h => Or.inl h + have hlbl := frame_label lbl hE + by_cases hmu : lbl = fl4_abi.label.X86PageMap ∨ lbl = fl4_abi.label.X86PageUnmap + · exact WP.spec_mono (J.selectedNativeFrameS3 hni hRef hmu q hn (MR := MR) buffer on rc blk call don fp) + fun _ h => Or.inl h + have hm : lbl ≠ fl4_abi.label.X86PageMap := fun h => hmu (Or.inl h) + have hu : lbl ≠ fl4_abi.label.X86PageUnmap := fun h => hmu (Or.inr h) + by_cases hg : lbl = fl4_abi.label.X86PageGetAddress + · exact WP.spec_mono (J.frameGetAddrAnyDispX arI q.current hRun hlbl hg sz d m ep blk call don fp sl ex + args (Fl4Spec.toBl cptr.bv) exs buffer on rc) fun _ h => Or.inr h + by_cases ht : lbl = fl4_abi.label.X86PageTestAndClearAccessed + · subst ht + exact WP.spec_mono (J.frameTacNativeDispX arI q.current hRun sz d m ep blk call don fp sl q.absSlot + (capV_of_capAt q.capAt) hn ex args (Fl4Spec.toBl cptr.bv) exs buffer on rc) fun _ h => Or.inr h + by_cases hd : lbl = fl4_abi.label.X86PageDeriveSubframe + · subst hd + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + have hs := J.frameDeriveAnyDispX arI (MR := MR) hni hpc q.current q.capAt (args.map fun x => (⟨x⟩ : U64)) + q.length q.words blk call don fp ex (Fl4Spec.toBl cptr.bv) exs + (ExtraOK.vsExtra J.1.1.xtop.rel.1 q.extra) buffer on rc + rw [show (args.map fun x => (⟨x⟩ : U64)).map UScalar.bv = args from zv_map_mk args] at hs + exact WP.spec_mono hs fun _ h => Or.inl h + · exact WP.spec_mono (J.frameOtherAnyDispX arI (MR := MR) q.current hlbl hm hu hg ht hd p rt asid va len buf sz d m ep + blk call don fp sl ex args (Fl4Spec.toBl cptr.bv) exs buffer on rc) fun _ h => Or.inl h + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3FrameBatch.lean b/proofs/Fl4Proofs/P3/VmEntryS3FrameBatch.lean new file mode 100644 index 000000000..6a3fece87 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3FrameBatch.lean @@ -0,0 +1,1854 @@ +import Fl4Proofs.P3.VmEntryS3Pml4 +import Fl4Proofs.P3.VmFrameBatchRestart +import Fl4Proofs.P3.VmEntryS3IoControl + +/-! +The native FrameBatch steps of a mapped native PML4 batch in the VM refinement. + +* `VmEptState.vsLeafStep`: the VM relation after one native leaf step on a frame slot (a + `batch_map`, or a deferred `unmap_page_unflushed` with the cleared cap's `set_cap`). The step + writes the native VSpace words and that slot's two cap words. Guest pools and EPT tables are + unchanged and VCPU contents are framed, as in the native `JAtZ.batchMapJX` and + `JAtZ.fbDeferStepJ`, with `JAtZB.vsStepJXB` for the base. +* `VmEptState.batchMapV`: `batch_map` at `VmEptState` against `fbMap`, for a native source cap. +* `VmEptState.fbDeferStepV`: the deferred native unmap and the cleared cap's `set_cap`. +* `VmEptState.delFrameNative`: deleting a native-mapped frame matches `capDeleteVF`. + +[P] Uses the existing native VSpace architecture refinement and totality contracts. +No existing statement changed. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} +variable {Lst : U64 → List U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The architecture objects used by the VM relation. -/ +def VmGuestArch : Fl4Spec.ArchKernelObj → Prop + | .vcpu _ | .ept _ _ | .asidPool _ => True + | _ => False + +theorem fbNativeMapObject {σ' : Fl4Spec.State Unit} {x : U64} {c : capability.Cap} {m' : U64 → U64} + (B : XTopAt (L := L) AR Z s a σ) (hK : KFoot σ σ') + (hF : MapLeafF (L.mem s) m' x c σ σ') + {a' : A} {s' : S} (B' : XTopAt (L := L) AR Z s' a' σ') : + ∀ o ao, VmGuestArch ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + obtain ⟨-, -, -, asid, pm, q, h0, h20, hfind, hko, hW⟩ := hF + have hpm := VsPath.of_find B.fin.2.1 h0 h20 hfind + have nativeTable : ∀ t τ (hP : PtRel Z.L4 Z.L3 Z.L2 Z.L1 t τ), + ∃ ao, τ.kheap q.bv = some (.archObj ao) ∧ ¬ VmGuestArch ao := by + intro t τ hP + rcases hW with h | ⟨h, -⟩ | ⟨h, -⟩ + · obtain ⟨_, hp⟩ := h.path hpm + obtain ⟨f, hk, -⟩ := hP.l1 q (hp.mem B.fin.2.1 B.fin.2.2.1 B.fin.2.2.2.1) + exact ⟨_, hk, id⟩ + · obtain ⟨_, hp⟩ := h.path hpm + obtain ⟨f, hk, -⟩ := hP.l2 q (hp.mem B.fin.2.1 B.fin.2.2.1 B.fin.2.2.2.1) + exact ⟨_, hk, id⟩ + · obtain ⟨_, hp⟩ := h.path hpm + obtain ⟨f, hk, -⟩ := hP.l3 q (hp.mem B.fin.2.1 B.fin.2.2.1 B.fin.2.2.2.1) + exact ⟨_, hk, id⟩ + intro o ao hao + constructor + · intro h + have hn : (⟨o⟩ : U64) ≠ q := by + intro e + have e' := congrArg (fun w : U64 => w.bv) e + obtain ⟨ap, hq, hnap⟩ := nativeTable _ _ B.fin.2.2.1 + change o = q.bv at e' + rw [← e', h] at hq + cases hq + exact hnap hao + exact (hko ⟨o⟩ ⟨ao, h⟩ hn).trans h + · intro h + refine archKept_of hko hK ?_ h + intro e + obtain ⟨ap, hq, hnap⟩ := nativeTable _ _ B'.fin.2.2.1 + rw [← e, h] at hq + cases hq + exact hnap hao + +theorem fbNativeUnmapObject {σ' : Fl4Spec.State Unit} + (hK : KFoot σ σ') (hS : ArchShr σ σ') : + ∀ o ao, VmGuestArch ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + intro o ao hao + have heq : ∀ x y, EntShr x y → (VmGuestArch x ∨ VmGuestArch y) → y = x := by + rintro x y (rfl | ⟨f, g, rfl, rfl, -⟩ | ⟨f, g, rfl, rfl, -⟩ | + ⟨f, g, rfl, rfl, -⟩ | ⟨f, g, rfl, rfl, -⟩) h + · rfl + all_goals exact False.elim (h.elim id id) + constructor + · intro h + obtain ⟨ao', h', hs⟩ := hS.2 o ao h + rw [heq _ _ hs (Or.inl hao)] at h' + exact h' + · intro h + by_cases e : σ'.kheap o = σ.kheap o + · rw [← e]; exact h + rcases hK.2.2.1 o e with ⟨x, y, hx, hy⟩ | ⟨r, cs, cs', -, -, hy⟩ | ⟨t, t', -, hy⟩ + · obtain ⟨z, hy', hs⟩ := hS.2 o x hx + rw [h] at hy' + have ez : ao = z := by cases hy'; rfl + subst z + rw [← heq _ _ hs (Or.inr hao)] at hx + exact hx + · rw [h] at hy; cases hy + · rw [h] at hy; cases hy + +set_option maxHeartbeats 8000000 in +/-- **The VM relation after a native step that rewrites one frame slot** (module docstring): from +the relation `XTopAt` after, the spec step `VsSFr`, FL4's writes in the slot's two cap words and the +native VSpace words, the frame cap after alike to the native frame before, and the spec's guest +objects and ASID table unchanged. -/ +theorem VmEptState.nativeSlotLift [Fl4Proofs.P1.PlainStore st] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {p rt fa fv : U64} {sz : capability.FrameSize} {d mp ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d mp ep fa fv)) + (hn : NativeCap (.Frame p sz rt d mp ep fa fv)) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') (F : VsSFr σ σ') + (hW : P1.WritesIn st s s' (VsXW Z (L.ksb s) (cslotAddr (pc.bv, idx)))) + (hSF : SlotFoot Z.th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx))) + (hLk : VsLike (capV (L.mem s') (cslotAddr (pc.bv, idx))) (.Frame p sz rt d mp ep fa fv)) + (hpostF : IsFrameC (capV (L.mem s') (cslotAddr (pc.bv, idx)))) + (hvn : Z.vx = ∅ → VsNamed Z.th (L.mem s') (L.ksb s')) + (harch : ∀ o ao, VmGuestArch ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao))) + (hat : σ'.archState.x64AsidTable = σ.archState.x64AsidTable) : + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s' a' σ' ∧ + ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pc.bv, idx) → capV (L.mem s') y = capV (L.mem s) y := by + obtain ⟨⟨B, Cv⟩, C⟩ := J + set c : capability.Cap := .Frame p sz rt d mp ep fa fv with hcf + set x := cslotAddr (pc.bv, idx) with hxdef + have hks' : L.ksb s' = L.ksb s := hW.1 + have hA0 : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hxsl : IsSlot Z.th x := hA0.isSlot + have hcv0 : capV (L.mem s) x = c := capV_of_capAt hc + have hu : isUt c = false := rfl + -- the slots + have hcap : ∀ y, IsSlot Z.th y → y ≠ x → capV (L.mem s') y = capV (L.mem s) y := by + intro y hy hne + obtain ⟨-, -, h⟩ := hSF y hy + unfold capV; rw [(h hne).1, (h hne).2] + have Ap : VsApart (L := L) Z H Cs s := ⟨B.scOut⟩ + have hw1 := B.vsc x hxsl + rw [hcv0] at hw1 + have hvc : VsSlotsW Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s') := by + intro y hy + by_cases e : y = x + · rw [e] + exact (hLk.2 _ _ _ _ _ (hw1.okTab trivial)).toW + · rw [hcap y hy e] + exact B.vsc y hy + have hsh : shape (capV (L.mem s') x) = shape (capV (L.mem s) x) := by + rw [hcv0]; exact hLk.1.1 + have B' : JAtZB (L := L) arI AR Z H Cs Ac Lst D s' a' σ' := + B.vsStepJXB Ap hpc hidx (TabsArch.empty σ) (L3' := Z.L3) (L2' := Z.L2) (L1' := Z.L1) hX' F + (hW.mono fun _ h => Or.inl h) hcap hsh (by rw [hcv0]; exact hLk.1.2.1) + (hcv0 ▸ hu) (fun y hy => ⟨(hSF y hy).1, (hSF y hy).2.1⟩) hvc + ⟨subset_rfl, subset_rfl, subset_rfl⟩ hvn + have sw : ∀ b, (b = x ∨ b = wordOf x 8#u64) → SlotWd Z.th b := by + rintro b (rfl | rfl) + · exact ⟨x, hxsl, 0#u64, Or.inl rfl, by rw [wordOf_zero]⟩ + · exact ⟨x, hxsl, 8#u64, Or.inr (Or.inl rfl), rfl⟩ + have nV : ∀ u ∈ V, ∀ b, VsXW Z (L.ksb s) x b → b.val < u.val ∨ u.val + 32768 ≤ b.val := by + intro u hu b hb + rcases hb with hb | hb + · obtain ⟨vx, hvx⟩ := Cv.vm.spec u hu + have := B.vsW_apart (t := u) (n := 32768) (fun o ko hk hne => Cv.vm.sep u hu o ko hk hne) (Cv.vm.ks u hu) + (fun q hq e => by + obtain ⟨ao, hk, -, -, hnv⟩ := B.vsFrSpec hq + rw [e, hvx] at hk; cases hk; exact hnv vx rfl) hb + omega + · exact B.slotWd_vcpu Cv.vm (sw b hb) u hu + have nX : ∀ b, VsXW Z (L.ksb s) x b → ∀ t ∈ X, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + rcases hb with hb | hb + · obtain ⟨es, hxe, -⟩ := C.rel.obj t ht + have := B.vsW_apart (t := t) (n := 4096) (fun o ko hk hne => C.sep t ht o ko hk hne) (C.ks t ht) + (fun q hq e => by + obtain ⟨ao, hk, -, hne, -⟩ := B.vsFrSpec hq + rw [e, hxe] at hk; cases hk; exact hne _ es rfl) hb + omega + · exact JAtZB.slotWd_ept B C b (sw b hb) t ht + have hv : VKeep (L := L) Z.th s s' := by + intro t ht + have htT := B.thT t ht + obtain ⟨Q, R0, hcq, -⟩ := B.xtop.fin.1.1.1.lists + have hT := hcq.tcbs + have ev := tVcpuA_val hT htT + refine hW.mem_eq (tVcpuA_aligned hT htT) fun hb => ?_ + rcases hb with hb | hb + · have hP1 := B.xtop.fin.1.2 + have hO : OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ (tVcpuA t) := + Or.inr (Or.inr (Or.inl ⟨t, htT, by omega, by omega⟩)) + have hvo : VObj Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s) (tVcpuA t) := by + rcases hb with ⟨q, hq, i, hi, e⟩ | h + · rw [e]; exact table_vobj hP1.pt hq hi _ + · exact vsWrite_vobj hP1.vs h + exact hP1.disj _ hO hvo + · exact B.slotWd_tcb (sw _ hb) ht (by omega) (by omega) rfl + have hpostN : NativeCap (capV (L.mem s') x) := hn.of_vtF hLk.1.2.2.2 + obtain ⟨pp, ss, rr, dd, mm, ee, aa, vv, hpost⟩ : + ∃ pp ss rr dd mm ee aa vv, capV (L.mem s') x = .Frame pp ss rr dd mm ee aa vv := by + generalize h : capV (L.mem s') x = c' at hpostF ⊢ + cases c' <;> first | exact False.elim hpostF | exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + have hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv := by + intro u hu + obtain ⟨v, h⟩ := Cv.vm.spec u hu + exact ((harch u.bv (.vcpu v) trivial).mp h).trans h.symm + have hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM := fun o x' ho hx => by + rcases F.kh o with e | ⟨a0, a1, -, h1, -⟩ | ⟨r', cs0, cs1, -, h1⟩ | ⟨y0, y1, h0, h1, hco⟩ + · exact ⟨x', by rw [← e]; exact ho, hx⟩ + · rw [h1] at ho; cases ho + · rw [h1] at ho; cases ho + · rw [h1] at ho; cases ho; exact ⟨y0, h0, by rw [← hco.1]; exact hx⟩ + have hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs)) := fun o v => (harch o (.vcpu v) trivial).mpr + have hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ + Fl4Spec.objBits ko' = Fl4Spec.objBits ko := by + intro o ko' ho + obtain ⟨ko, hk, ht⟩ := F.ksz o ko' ho + exact ⟨ko, hk, (objBits_of_aType ht).symm⟩ + have hcs' : ∀ y, IsSlot Z.th y → capV (L.mem s') y = capV (L.mem s) y ∨ + (shape (capV (L.mem s') y) = shape (capV (L.mem s) y) ∧ VcpuCapV V (capV (L.mem s') y) ∧ + ∀ f l o, capV (L.mem s') y ≠ .IoPort f l o) := by + intro y hy + by_cases e : y = x + · subst y + refine Or.inr ⟨hsh, ?_⟩ + rw [hpost] + exact ⟨trivial, fun _ _ _ e => by cases e⟩ + · exact Or.inl (hcap y hy e) + have Cv' : VcpuCore (L := L) V Z H s' σ' := + Cv.ofWG B B' hW hcs' nV (fun b hb => by + rcases hb with hb | hb + · exact B.vsW_head hb + · exact B.slotWd_head (sw b hb)) hv hvk hsz hrv hnv + have hEptSlot : EptSlotStep Z.th Z.th (L.mem s) (L.mem s') := by + intro t ht + have h := cnode_slot_ne_tcb B.xtop.rel.1 hpc hidx ht 5 (by decide) + exact Or.inr (Or.inr ⟨ht, hcap _ (isSlot_eptSlot ht) h.symm⟩) + have hck : ∀ y, IsSlot Z.th y → ∀ p l a v mm, capV (L.mem s) y = .Ept p l mm a v → + capV (L.mem s') y = .Ept p l mm a v := by + intro y hy p l a v mm h + have hne : y ≠ x := by + intro e + rw [e, hcv0, hcf] at h + cases h + exact (hcap y hy hne).trans h + have hcb : ∀ y, IsSlot Z.th y → ∀ p l a v mm, capV (L.mem s') y = .Ept p l mm a v → + capV (L.mem s) y = .Ept p l mm a v := by + intro y hy p l a v mm h + have hne : y ≠ x := by + intro e + rw [e, hpost] at h + cases h + exact (hcap y hy hne).symm.trans h + have hpools : ∀ hi lo e, PoolAt σ' hi lo e → PoolAt σ hi lo e := by + rintro hi lo e ⟨pool, f, htab, hpool, hent⟩ + exact ⟨pool, f, by rw [← hat]; exact htab, (harch pool (.asidPool f) trivial).mpr hpool, hent⟩ + have hepts : ∀ o l es, σ'.kheap o = some (.archObj (.ept l es)) → + σ.kheap o = some (.archObj (.ept l es)) := fun o l es => (harch o (.ept l es) trivial).mpr + have hnm := C.named.frameA hat (fun hi lo e he _ => hpools hi lo e he) hepts + (fun y p l a v hy h _ => ⟨y, hy, hck y hy p l a v true h⟩) + (fun y p l mm a v hy h => ⟨y, hy, hcb y hy p l a v mm h⟩) + (fun _ _ _ _ _ _ y p l a v hy _ hl h => ⟨y, l, a, v, hy, hl, hck y hy p l a v true h⟩) + have hEx : JExtEpt (L := L) X Z.th s' := by + refine ⟨fun y hy => ?_, ⟨fun y hy p l a v h => C.exte.vtop.1 y hy p l a v (hcb y hy p l a v true h), ?_⟩, + fun y hy p l a v h => C.exte.anz y hy p l a v (hcb y hy p l a v true h)⟩ + · by_cases e : y = x + · rw [e, hpost]; trivial + · rw [hcap y hy e]; exact C.exte.cap y hy + · intro y hy p sz r d a v h + by_cases e : y = x + · have hN := hpostN + rw [← e, h] at hN + exact (hN ⟨rfl, rfl⟩).elim + · rw [hcap y hy e] at h + exact C.exte.vtop.2 y hy p sz r d a v h + have C' : EptCore (L := L) X XL Z.th s' σ' := by + refine C.frame hW nX + (fun y hy p l a v mm h => ⟨hy, hcb y hy p l a v mm h⟩) hEx + (fun t ht => ?_) (fun o l es h => ⟨l, es, hepts o l es h⟩) + (fun e ⟨hi, p, f, lo, htab, hk, hl⟩ => (hpools hi lo e ⟨p, f, htab, hk, hl⟩).guest) + hsz hEptSlot hnm + obtain ⟨es, hk, -⟩ := C.rel.obj t ht + exact ((harch t.bv (.ept (XL t) es) trivial).mp hk).trans hk.symm + exact ⟨⟨⟨B', Cv'⟩, C'⟩, hcap⟩ + + +set_option maxHeartbeats 8000000 in +/-- **The VM relation after one native leaf step on a frame slot** (module docstring): the +outputs of a native `*_rel` leaf lemma (`batch_map_rel`, or `unmap_unflushed_rel` and the cleared +cap's `set_cap`) at a native frame cap of the selected CNode slot. -/ +theorem VmEptState.vsLeafStep [Fl4Proofs.P1.PlainStore st] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {p rt fa fv : U64} {sz : capability.FrameSize} {d mp ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d mp ep fa fv)) + (hn : NativeCap (.Frame p sz rt d mp ep fa fv)) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hSRq : FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) s' a' σ') + (hSt' : StateRel Z.th (L.mem s') σ') (hSV' : SlotsValid Z.th (L.mem s')) + (hR' : VsRel Z.P (L.mem s') (L.ksb s') σ') (hPt' : PtRel Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s') σ') + (hSF : SlotFoot Z.th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx))) + (hLk : VsLike (capV (L.mem s') (cslotAddr (pc.bv, idx))) (.Frame p sz rt d mp ep fa fv)) + (hK : KFoot σ σ') (F : VsSFr σ σ') + (hLeaf : MapLeafF (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) (.Frame p sz rt d mp ep fa fv) σ σ' ∨ + UnmapLeafF (L.mem s') (cslotAddr (pc.bv, idx)) (.Frame p sz rt d mp ep fa fv) σ σ') + (hZ0 : ZNoBlk Z.th (L.mem s) (L.ksb s)) : + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s' a' σ' ∧ + P1.WritesIn st s s' (VsXW Z (L.ksb s) (cslotAddr (pc.bv, idx))) ∧ + FbFootK (L := L) Z s s' ∧ CnSlotsKept (L := L) Z.th s s' ∧ ZNoBlk Z.th (L.mem s') (L.ksb s') := by + obtain ⟨⟨B, Cv⟩, C⟩ := J + set c : capability.Cap := .Frame p sz rt d mp ep fa fv with hcf + set x := cslotAddr (pc.bv, idx) with hxdef + have hX := B.xtop + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := id hX.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := id hX.rel + have hA0 : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hxsl : IsSlot Z.th x := hA0.isSlot + have hcv0 : capV (L.mem s) x = c := capV_of_capAt hc + have hu : isUt c = false := rfl + obtain ⟨hCR', hFI', hMO'⟩ := crelM_after hCR hFI hMO hc hSt' hSV' hSF hLk hK hu + obtain ⟨hTS', hW0⟩ := id hSRq + have hks' : L.ksb s' = L.ksb s := hW0.1 + obtain ⟨⟨hSch', hEp', hNt', -⟩, -⟩ := id hTS' + have hIpc' : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s') σ' := IpcRel.of hNt' hEp' (sched_sc hSch') + -- the TCBs' state words and the action word, pinned + have pinS : ∀ t, Z.th t = some .tcb → L.mem s' (P1.stA t) = L.mem s (P1.stA t) := by + intro t ht + have htT := B.thT t ht + obtain ⟨y1, hk1, hR1⟩ := B.xtop.ipc.tcb t htT + obtain ⟨y, hky, hRy⟩ := hIpc'.tcb t htT + obtain ⟨y', hy', hc'⟩ := F.tcbF hk1 + rw [hky] at hy'; cases hy' + have h1 : (L.mem s' (tStA t)).val = (L.mem s (tStA t)).val := by rw [hRy.st, hR1.st, hc'.st] + rw [stA_tStA]; exact UScalar.eq_of_val_eq h1 + have pinA : 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) := by + have a1 := B.xtop.fin.1.1.1.act + have a2 := hSch'.act + rw [F.act] at a2 + have e := ActRel.det a2 a1 + obtain ⟨Q, R, hcq, -⟩ := B.xtop.fin.1.1.1.lists + unfold nodeW at e + rw [P1.nodeAt_of_ksb hks', ← hcq.cur] at e + exact e + -- FL4's writes in `VsXW` + have hW : P1.WritesIn st s s' (VsXW Z (L.ksb s) x) := by + refine ⟨hks', hW0.2.1, fun b hb hn => ?_⟩ + have hslw : ∀ y, IsSlot Z.th y → ∀ k : U64, Off k → b = wordOf y k → L.mem s' b = L.mem s b := by + intro y hy k hk e + subst e + obtain ⟨h16, h24, hne⟩ := hSF y hy + rcases hk with rfl | rfl | rfl | rfl + · by_cases ex : y = x + · exact (hn (Or.inr (Or.inl (by rw [wordOf_zero, ex])))).elim + · rw [wordOf_zero]; exact (hne ex).1 + · by_cases ex : y = x + · exact (hn (Or.inr (Or.inr (by rw [ex])))).elim + · exact (hne ex).2 + · exact h16 + · exact h24 + by_cases hb0 : VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s b + · rcases hb0 with ⟨y, hy, e | e⟩ | ⟨y, k, hy, hk, e⟩ | hpt | ⟨t, ht, e⟩ | e + · exact hslw y hy 0#u64 (Or.inl rfl) (by rw [wordOf_zero]; exact e) + · exact hslw y hy 8#u64 (Or.inr (Or.inl rfl)) e + · exact hslw y hy k hk e + · exact (hn (Or.inl (Or.inl hpt))).elim + · subst e; exact pinS t ht + · subst e; exact pinA + · exact hW0.2.2 b hb hb0 + -- the domain schedule's words (in `OObj`, so outside `VsXW`) + have hD' : DomRel (L.mem s') (L.ksb s') σ' := by + rw [hks'] + refine DomRel.frame hDR (fun o ho => ?_) F.dl F.dsi + have ho' : o.val < 256 ∧ o.val % 8 = 0 := by + simp only [domOffs, List.mem_cons, List.mem_nil_iff, or_false] at ho + rcases ho with rfl | rfl | rfl | rfl | rfl | rfl <;> decide + have hal := ksW_al B.kern.ksbFit B.kern.ksbAl ho'.1 ho'.2 + have hv := ksW_val (kb := L.ksb s) (o := o) (by have := B.kern.ksbFit; omega) + exact hW.mem_eq hal fun hb => hS.2.disj _ (Or.inr (Or.inl ⟨by omega, by omega, Or.inl (by omega)⟩)) + (vsxw_vobj hPl hS.2.vs hS.2.pt hxsl hb) + have hFXT := vs_x_afterT hS hPI hCur hSRq hR' hPt' hCR' hFI' hMO' hK + have hFXR := FinXT.toXR Z.G hX.esa hIA.dom hFXT hD' + have hX' : XTopAt (L := L) AR Z s' a' σ' := by + obtain ⟨hFin, ⟨G0, hPl', -, -, hD, -, hIA', hSA⟩, hE⟩ := hFXR + obtain ⟨⟨⟨hSch, hEp, hNt, -⟩, -⟩, -⟩ := id hFin + exact ⟨hFin, ⟨hPl', hCR', hFI', hD, by rw [hks']; exact hMO', hIA', hSA⟩, IpcRel.of hNt hEp (sched_sc hSch), hE⟩ + have hvn : Z.vx = ∅ → VsNamed Z.th (L.mem s') (L.ksb s') := fun hvx => by + have hR'' : VsRel Z.P (L.mem s') (L.ksb s) σ' := by rw [← hks']; exact hR' + rw [hks'] + exact hLeaf.elim + (fun h => (B.carry.vsNamed hvx).ofMapLeafF B.vsc B.xtop.fin.2.1 B.xtop.fin.2.2.1 + B.xtop.fin.2.2.2.1 hR'' hPt' hcv0 hSF h) + (fun h => (B.carry.vsNamed hvx).ofUnmapLeafF B.vsc B.xtop.fin.2.1 B.xtop.fin.2.2.1 + B.xtop.fin.2.2.2.1 hR'' hPt' hcv0 hSF h) + have harch : ∀ o ao, VmGuestArch ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := + hLeaf.elim (fun h => fbNativeMapObject B.xtop hK h hX') (fun h => fbNativeUnmapObject hK h.2.2) + have hat : σ'.archState.x64AsidTable = σ.archState.x64AsidTable := + hLeaf.elim (fun h => h.2.2.1) (fun h => h.2.2.1) + have hpostF : IsFrameC (capV (L.mem s') x) := hLeaf.elim (fun h => h.2.1) (fun h => h.2.1) + obtain ⟨J', hcap⟩ := VmEptState.nativeSlotLift ⟨⟨B, Cv⟩, C⟩ hpc hidx hc hn hX' F hW hSF hLk hpostF hvn harch hat + have Ap : VsApart (L := L) Z H Cs s := ⟨B.scOut⟩ + obtain ⟨pp, ss, rr, dd, mm, ee, aa, vv, hpost⟩ : + ∃ pp ss rr dd mm ee aa vv, capV (L.mem s') x = .Frame pp ss rr dd mm ee aa vv := by + generalize h : capV (L.mem s') x = c' at hpostF ⊢ + cases c' <;> first | exact False.elim hpostF | exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + -- the batch's own frames + have hFK : FbFootK (L := L) Z s s' := by + refine ⟨fun t ht g => ?_, fun wp hw => ?_⟩ + · have hta : P1.TcbAddr t := B.ip.e.tcb t ht + have hr := regA_val hta (regIdx_lt g) + have hrl := regIdx_lt g + have hal := hta.align + simp only [Nat.reducePow] at hal + exact hW.mem_eq (by unfold P1.Aligned; rw [hr]; omega) fun hb => by + rcases B.vsxObjW Ap hpc hidx hb (B.zH.1 t ht) (fun _ e => by cases e) with e | e <;> + (try simp only [P1.HKind.bits, Nat.reducePow] at e) <;> omega + · have hal : P1.Aligned wp.1 := (hS.2.ep.balign wp hw).1 + exact hW.mem_eq hal fun hb => hS.2.disj _ + (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨wp, hw, le_refl _, by omega⟩))))))) + (vsxw_vobj hPl hS.2.vs hS.2.pt hxsl hb) + have hCSK : CnSlotsKept (L := L) Z.th s s' := by + intro y hy p' r' gs' g' hy' + by_cases e : y = x + · rw [e, hcv0, hcf] at hy'; cases hy' + · rw [hcap y hy e]; exact hy' + exact ⟨J', hW, hFK, hCSK, + hZ0.ofSlot hks' (fun _ _ _ h => by rw [hpost] at h; cases h) hcap⟩ + +set_option maxHeartbeats 8000000 in +/-- **`batch_map` at `VmEptState`** (module docstring): for a native source cap, an `fbMap` +outcome of the same kind; on success the VM relation, the batch's loop frames and the CNode slots +after. -/ +theorem VmEptState.batchMapV [Fl4Proofs.P1.PlainStore st] (hA : ArchRefines arI AR) + {τ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a τ) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {c : capability.Cap} (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok c) (hnat : NativeCap c) + {p1 as1 : U64} (has1 : as1 ≠ 0#u64) (has2 : as1.val < 2 ^ 20) + (hf : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) as1.bv) τ = ({(.ok p1.bv, τ)}, False)) + (op depth a1 cr a2 : U64) (dd : Bool) (cn dcn : capability.Cap) (a0 : U64) + (b : Fl4Spec.Fl4FrameBatchInvocation) (hbv : b.vspace = p1.bv) (hba : b.asid = as1.bv) + (hbm : b.mapRights = a1.bv) (hbat : b.attr = a2.bv) (hZ0 : ZNoBlk Z.th (L.mem s) (L.ksb s)) : + vspace.batch_map st arI s a (cslotAddr (pc.bv, idx)) ⟨p1, as1, op, depth, a1, cr, a2, dd, cn, dcn⟩ a0 ⦃ res => + (∃ e e', res.1 = .Err e ∧ MapErrRel e e' ∧ + ExceptT.run (Fl4Spec.fbMap (Z := Unit) b (pc.bv, idx) a0.bv) τ = ({(.error e', τ)}, False) ∧ + res.2.1 = s ∧ res.2.2 = a) ∨ + (res.1 = .Ok () ∧ ∃ τ1, ((.ok (), τ1) : Except Fl4Spec.SyscallError Unit × Fl4Spec.State Unit) ∈ + (ExceptT.run (Fl4Spec.fbMap (Z := Unit) b (pc.bv, idx) a0.bv) τ).1 ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D res.2.1 res.2.2 τ1 ∧ FbKeep τ τ1 ∧ + FbFootK (L := L) Z s res.2.1 ∧ CnSlotsKept (L := L) Z.th s res.2.1 ∧ + ZNoBlk Z.th (L.mem res.2.1) (L.ksb res.2.1)) ⦄ := by + have B := J.1.1 + have hX := B.xtop + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := id hX.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := id hX.rel + obtain ⟨hSt, hSV, -, -, -⟩ := id hCR + have hA0 : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hxsl : IsSlot Z.th (cslotAddr (pc.bv, idx)) := hA0.isSlot + have hcv0 : capV (L.mem s) (cslotAddr (pc.bv, idx)) = c := capV_of_capAt hc + have hok : ∀ p sz rt d m e fa fv, c = .Frame p sz rt d m e fa fv → VCapOK Z.P Z.L4 Z.L3 Z.L2 Z.L1 c := by + intro p sz rt d m e fa fv he + have h := B.vsc _ hxsl + rw [hcv0] at h + subst he + exact h.okTab trivial + have hp1 : p1 ∈ Z.L4 := fb_root_L4 hR hPI has1 hf + have hH : VsFrameHyps st A arI Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 + Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) := + (vsFrameHyps_top hA).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl + have hHL : VsFrameHypsL st A arI Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 + Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) := + (vsFrameHypsL_top hA).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl + refine WP.spec_mono (batch_map_rel hH hHL s a τ hS.2.pl hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po + hSt hSV hR hPt ⟨hS, P1.WritesIn.refl _ _⟩ hpc hidx hc hok hnat hp1 has1 has2 hf op depth a1 cr a2 dd cn dcn a0 + b hbv hba hbm hbat) ?_ + rintro ⟨r0, s', a'⟩ (⟨e, e', h1, h2, h3, h4, h5⟩ | ⟨h1, σ', hmem, ⟨pi, hpi⟩, hSt', hSV', hR', hPt', hSRq, hSF, hLk, + hK, hFK, -, hML, cp, csz, crt, cd, cm, ce, cfa, cfv, hcf⟩) + · exact Or.inl ⟨e, e', h1, h2, h3, h4, h5⟩ + try simp only at h1 hSt' hSV' hR' hPt' hSRq hSF hLk hML ⊢ + subst hcf + have F : VsSFr τ σ' := VsSFr.of_fk (fv_performPageInvocation pi) hpi + obtain ⟨J', -, hFF, hCSK, hZ'⟩ := + J.vsLeafStep hpc hidx hc hnat hSRq hSt' hSV' hR' hPt' hSF hLk hK F (Or.inl hML) hZ0 + exact Or.inr ⟨h1, σ', hmem, J', hFK, hFF, hCSK, hZ'⟩ + +set_option maxHeartbeats 8000000 in +/-- **The deferred native unmap and the cleared cap's `set_cap` at `VmEptState`** (module +docstring; `JAtZ.fbDeferStepJ`'s spec step and frames). -/ +theorem VmEptState.fbDeferStepV [Fl4Proofs.P1.PlainStore st] (hA : ArchRefines arI AR) + {τ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a τ) + {pc : U64} {idx : List Bool} (hpc : Z.th pc = some (.cnode idx.length)) {p : U64} {sz : capability.FrameSize} + {rt asd va : U64} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt false true false asd va)) + {root : U64} (hfv : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) asd.bv) τ = ({(.ok root.bv, τ)}, False)) + {b2 : Bool} {sU s' : S} + (hu : vspace.unmap_page_unflushed st s root sz va p = ok (b2, sU)) + (hs : obj.Slot.set_cap st (cslotAddr (pc.bv, idx)) sU (.Frame p sz rt false false false 0#u64 0#u64) = ok s') + (hZ0 : ZNoBlk Z.th (L.mem s) (L.ksb s)) : + ∃ τu σ' : Fl4Spec.State Unit, + (b2, τu) ∈ (Fl4Spec.unmapPageUnflushed (Z := Unit) (frameSizeOf sz) root.bv va.bv p.bv τ).1 ∧ + ((), σ') ∈ (Fl4Spec.setCap (Z := Unit) (.archObjectCap (.pageCap false p.bv (vmRightsOf rt) .vmNoMap + (frameSizeOf sz) none)) (pc.bv, idx) τu).1 ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s' a σ' ∧ L.ksb s' = L.ksb s ∧ FbKeep τ σ' ∧ + σ'.machineState.underlyingMemory = τ.machineState.underlyingMemory ∧ CnSlotsKept (L := L) Z.th s s' ∧ + FbFootK (L := L) Z s s' ∧ ZNoBlk Z.th (L.mem s') (L.ksb s') ∧ + capAt (L.mem s') (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt false false false 0#u64 0#u64) := by + set c : capability.Cap := .Frame p sz rt false true false asd va with hcf + have hidx : idx.length = idx.length := rfl + have B := J.1.1 + have hX := B.xtop + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := id hX.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := id hX.rel + obtain ⟨hSt, hSV, -, -, -⟩ := id hCR + have hA0 : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl hpc⟩ + have hxsl : IsSlot Z.th (cslotAddr (pc.bv, idx)) := hA0.isSlot + have hcv0 : capV (L.mem s) (cslotAddr (pc.bv, idx)) = c := capV_of_capAt hc + have hok : VCapOK Z.P Z.L4 Z.L3 Z.L2 Z.L1 c := by + have h := B.vsc _ hxsl + rw [hcv0] at h + exact h.okTab trivial + have hasd : asd ≠ 0#u64 := hok.1 rfl + have hvu : UserV va := hok.2.2 rfl + have hr1 : root ∈ Z.L4 := fb_root_L4 hR hPI hasd hfv + have hHU : VsUnmapPageHyps st A arI Z.L3 Z.L2 Z.L1 (FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 + Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) := + (vsUnmapPageHyps_top hA).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) + have hHF : VsFrameHyps st A arI Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 + Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) := + (vsFrameHyps_top hA).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl + -- the deferred unmap + have h1 := unmap_unflushed_rel (th := Z.th) (P := Z.P) (L4 := Z.L4) hHU s a τ hS.2.pl hS.2.pt hS.2.ps hS.2.vs hS.2.pv + ⟨⟨hS, P1.WritesIn.refl _ _⟩, hSt, hSV, hR, hPt⟩ sz root va p hr1 hvu + rw [hu, WP.spec_ok] at h1 + obtain ⟨τu, hmu, hshape, ⟨hSu, hStu, hSVu, hRu, hPtu⟩, hksu, hKu, hASu, hWu⟩ := h1 + dsimp only at hmu hSu hStu hSVu hRu hPtu hksu hWu + -- the cleared cap's `set_cap` + obtain ⟨cs, hkc, hwf, -⟩ := hSt.cnode pc _ hpc + have hkcu : τu.kheap pc.bv = some (.cnode idx.length cs) := by + rcases hshape with e | ⟨q, ao, ao', hq, -, -, e⟩ + · rw [e]; exact hkc + · have hpq : pc.bv ≠ q := fun e' => by rw [e', hq] at hkc; cases hkc + rw [e, Notif.updK_kheap, Function.update_of_ne hpq]; exact hkc + obtain ⟨cS, aS, hcS, hcaS, hgS⟩ := hSt.slot_cap (Z := Unit) (sl := (pc.bv, idx)) (p := pc) rfl hpc + rw [hc] at hcS; cases eq_of_ok_eq hcS + have hfw : CapWf c := hcaS.1 + have hfe : EncWf c := by have h := hc; rw [capAt_words] at h; exact decode_encWf h + have hfcv : CapValid Z.th c := + hSV.cnode pc _ hpc (blVal 0 idx) (blVal_lt idx) _ + (by rw [show slotAddr pc.bv (BitVec.ofNat 64 (blVal 0 idx)) = cslotAddr (pc.bv, idx) from + (slotAddr_ofBl pc idx).symm]; exact hc) + set nc : Fl4Spec.Cap := .archObjectCap (.pageCap false p.bv (vmRightsOf rt) .vmNoMap (frameSizeOf sz) none) with hnc + have hca : CapRel (.Frame p sz rt false false false 0#u64 0#u64) nc := ⟨hfw, rfl⟩ + have hew : EncWf (.Frame p sz rt false false false 0#u64 0#u64) := ⟨hfe.1, hfe.2.1, by decide, by decide⟩ + have h2 := WP.spec_and (set_cap_refines Z.th sU τu hStu hSVu hS.2.pl hpc rfl _ _ hca (by exact hfcv) hew) + (WP.spec_and (set_cap_spec (L := L) _ sU _) (set_cap_foot hS.2.pl hxsl sU _ hew)) + rw [hs, WP.spec_ok] at h2 + obtain ⟨⟨σ5, hsc, hSt', hSV', -, -, -, hfr5⟩, hs5, hF5, hcv5⟩ := h2 + have hsc' := hsc + rw [setCap_cnode nc hkcu hwf rfl] at hsc + have hσ5 : σ5 = setCnodeSlot τu pc.bv idx.length cs idx nc := by + have := congrArg Prod.fst hsc + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at this + exact this.symm + subst hσ5 + set σ' := setCnodeSlot τu pc.bv idx.length cs idx nc with hσ' + have hxs : IsSlotAt Z.th pc (blVal 0 idx) := Or.inl ⟨idx.length, hpc, blVal_lt idx⟩ + have hx0 : cslotAddr (pc.bv, idx) = slotOf pc (blVal 0 idx) := slotAddr_ofBl pc idx + have hks5 : L.ksb s' = L.ksb sU := by rw [hs5]; simp + have hks' : L.ksb s' = L.ksb s := hks5.trans hksu + have hWs : P1.WritesIn st sU s' (SlotW Z.th) := by + refine ⟨by rw [hs5]; simp, by rw [hs5]; simp, fun b hb hn => ?_⟩ + obtain ⟨h1, h2⟩ := ne_slot_words hS.2.pl hxs hn + exact hfr5 b hb (by rw [hx0]; exact h1) (by rw [hx0]; exact h2) + have hSRq := hHF.capStable _ _ a _ _ hSu hWs ⟨pc.bv, idx.length, cs, idx, nc, hkcu, rfl⟩ + have hR' : VsRel Z.P (L.mem s') (L.ksb s') σ' := by + have hfrV : ∀ b, VsWrite Z.P (L.ksb s) b → L.mem s' b = L.mem sU b := by + intro b hb + obtain ⟨h1, h2⟩ := ne_slot_words hS.2.pl hxs (hS.2.sd b hb) + exact hfr5 b (hb.aligned hS.2.vs) (by rw [hx0]; exact h1) (by rw [hx0]; exact h2) + have hpools : ∀ q ∈ Z.P, σ'.kheap q.bv = τu.kheap q.bv := by + intro q hq + obtain ⟨g, hg, -⟩ := hRu.pool q hq + have : q.bv ≠ pc.bv := kheap_ne hg hkcu (fun _ h1 h2 => by subst h1; cases h2) + simp [hσ', setCnodeSlot, this] + rw [hks5]; rw [hksu] at hRu ⊢ + exact hRu.frame hfrV rfl hpools + have hPt' : PtRel Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s') σ' := by + refine hPtu.frame (fun b hb => ?_) (fun q hq => ?_) + · obtain ⟨h1, h2⟩ := ne_slot_words hS.2.pl hxs (hS.2.ps b hb) + exact hfr5 b (hb.aligned hS.2.pt) (by rw [hx0]; exact h1) (by rw [hx0]; exact h2) + · obtain ⟨ao, hao⟩ := hPtu.arch hq + have : q.bv ≠ pc.bv := kheap_ne hao hkcu (fun _ h1 h2 => by subst h1; cases h2) + simp [hσ', setCnodeSlot, this] + have hK : KFoot τ σ' := hKu.trans (KFoot.cnode hkcu hwf idx nc) + have hSF : SlotFoot Z.th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) := + (SlotFoot.of_writes hS.2.pl hWu (fun b hb => hS.2.po b hb) _).trans hF5 + have hLk : VsLike (capV (L.mem s') (cslotAddr (pc.bv, idx))) c := by + rw [hcv5] + refine ⟨⟨rfl, rfl, rfl, rfl⟩, fun P L4 L3 L2 L1 h => ?_⟩ + rw [hcf] at h + simp only [VCapOK] at h ⊢ + exact ⟨fun e => absurd e (by decide), h.2.1, fun e => absurd e (by decide)⟩ + have F : VsSFr τ σ' := vsSFr_defer hshape hkcu hwf rfl + have hUL : Vs.UnmapLeafF (L.mem s') (cslotAddr (pc.bv, idx)) c τ σ' := + ⟨True.intro, by rw [hcv5]; exact True.intro, + Vs.ArchShr.keep_right hASu (Vs.cnode_keep hkcu idx nc).1 (Vs.cnode_keep hkcu idx nc).2⟩ + obtain ⟨J', -, hFF, hCSK, hZ'⟩ := J.vsLeafStep hpc hidx hc (by simp [NativeCap]) hSRq hSt' hSV' hR' hPt' hSF hLk + hK F (Or.inr hUL) hZ0 + have hKs : FbKeep τ σ' := fbKeep_defer hshape hkcu + have hum : σ'.machineState.underlyingMemory = τ.machineState.underlyingMemory := by + rcases hshape with e | ⟨q, ao, ao', -, -, -, e⟩ <;> rw [hσ', e] <;> rfl + refine ⟨τu, σ', hmu, by rw [hsc']; exact Set.mem_singleton _, J', hks', hKs, hum, hCSK, hFF, hZ', ?_⟩ + rw [capAt_capV]; exact congrArg ok hcv5 + +set_option maxHeartbeats 4000000 in +/-- **Deleting a native-mapped frame** matches the VM-aware finaliser and empties the slot while +keeping the VM relation. [P] Native architecture refinement. -/ +theorem VmEptState.delFrameNative [P1.PlainStore st] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hRef : ArchRefines arI AR) + {sl : Fl4Spec.CslotPtr} {p rt asid va : U64} {sz : capability.FrameSize} {dev : Bool} + (hAs : AbsSlot Z.th sl) (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Frame p sz rt dev true false asid va)) : + cdt.delete st arI s a (cslotAddr sl) true ⦃ r => r.1 = .Ok () ∧ + ∃ σ' G' Ac' Lst' D', (.ok (), σ') ∈ ((Fl4Spec.capDeleteVF (Z := Unit) sl).run σ).1 ∧ + VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' r.2.1 r.2.2 σ' ∧ + capV (L.mem r.2.1) (cslotAddr sl) = .Null ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr sl → capV (L.mem r.2.1) y = capV (L.mem s) y) ∧ + L.ksb r.2.1 = L.ksb s ⦄ := by + let c := capability.Cap.Frame p sz rt dev true false asid va + have B := J.1.1 + have hR := B.xtop.rel.2.1.1 + have hDS : DState (L := L) Z.th s σ Z.G := ⟨B.xtop.rel.2.1, B.del, B.xtop.rel.2.2.1⟩ + obtain ⟨fin, hfin, -, -⟩ := finalHyp_full Z.th B.xtop.rel.1 s σ Z.G sl c hDS hAs hc (by simp [c]) + obtain ⟨⟨⟨rem, cl⟩, s1, a1⟩, hf, hrem, -, hwf, σ1, hm, ⟨E, J1, hE⟩, -, hSS, hks⟩ := + spec_exists (J.finVsK arI hRef hDS hAs hc (fun _ _ h => by cases h) rfl ⟨_, rfl⟩ fin + (Or.inr ⟨p, sz, rt, dev, false, asid, va, rfl⟩)) + change rem = .Null at hrem + subst rem + have hE' : E = Z.vx := hE.resolve_right (fun ⟨_, _, e⟩ => by cases e) + subst E + have hclose : absCap cl = .nullCap := by + change ((Fl4Spec.Cap.nullCap, absCap cl), σ1) ∈ + ((Fl4Spec.unmapPage (Z := Unit) (frameSizeOf sz) asid.bv va.bv p.bv >>= fun _ => + pure (Fl4Spec.Cap.nullCap, Fl4Spec.Cap.nullCap)) σ).1 at hm + obtain ⟨_, _, hr⟩ := hm + exact (Prod.mk.inj (Prod.mk.inj (Set.mem_singleton_iff.1 hr)).1).2 + have hcl := (absCap_eq_null hwf).1 hclose + subst hcl + have hfc : ((.nullCap, .nullCap), σ1) ∈ + (Fl4Spec.finaliseCapVF (Z := Unit) (absCap c) (decide (Fl4Spec.isFinalCap' (absCap c) σ)) σ).1 := by + rw [VirtF13.finaliseCapVF_eq _ _ id] + exact hm + have hcv1 : capV (L.mem s1) (cslotAddr sl) = .Frame p sz rt dev true false asid va := by + rw [hSS.capV hAs.isSlot, capV_of_capAt hc] + have hap0 : FrApartH H c := by + have h := J1.1.1.cobj hAs.isSlot + rw [hcv1] at h + exact h + refine WP.spec_mono (JAtZB.delStep arI hAs hc hR (by simp [c]) (by simp [c, absCap]) + (Or.inr hap0) (fun _ h => h) rfl hfin hf hfc J1.1.1 hSS) ?_ + rintro ⟨r1, s2, a2⟩ ⟨hr, σ2, G2, Ac', Lst', D', hdel, B2, hks2, hw2, hcn2, hmS, hnull, hoth⟩ + refine ⟨hr, σ2, G2, Ac', Lst', D', hdel, ⟨⟨B2, J1.1.2.ofEmpty J1.1.1 hw2 hoth hnull ?_ ?_ hmS hcn2⟩, + J1.2.ofEmpty J1.1.1 hw2 hoth hnull hcn2 ?_ ?_⟩, hnull, + fun y hy hne => (hoth y hy hne).trans (hSS.capV hy), hks2.trans hks⟩ + · intro v h + rw [hcv1] at h + cases h + · intro t h + rw [hcv1] at h + cases h + · intro p l asid va h _ + rw [hcv1] at h + cases h + · intro p l asid va h _ + rw [hcv1] at h + cases h + +/-- The CNode slots after deleting a frame slot. -/ +private theorem fbCnKeptOfDel {sl : Fl4Spec.CslotPtr} {c : capability.Cap} {s' : S} + (hc : capAt (L.mem s) (cslotAddr sl) = ok c) (hnc : ∀ p r gs g, c ≠ .CNode p r gs g) + (hslot : ∀ y, IsSlot Z.th y → y ≠ cslotAddr sl → capV (L.mem s') y = capV (L.mem s) y) : + CnSlotsKept (L := L) Z.th s s' := by + intro y hy pp rad gs guard hcap + by_cases hne : y = cslotAddr sl + · subst y + rw [capV_of_capAt hc] at hcap + exact (hnc _ _ _ _ hcap).elim + · exact (hslot y hy hne).trans hcap + +set_option maxHeartbeats 16000000 in +/-- **One DELETE entry at `FbMidVE`** (module docstring): the target lookup, a null slot, a +non-frame cap, an unmapped or EPT-mapped frame (`FbMidVE.offOrEptDelete`), a native-mapped frame +deleted directly (`VmEptState.delFrameNative`), and the deferred native unmap of a frame in the +batch's ASID followed by the unmapped frame's delete. -/ +theorem FbMidVE.deleteEntry [P1.PlainStore st] {ct buf : U64} {args : List U64} + {τ : Fl4Spec.State Unit} {bt : vspace.Batch} {b : Fl4Spec.Fl4FrameBatchInvocation} + (C : FbCtx σ Z.th args bt b) (hct : ct ∈ Z.T) (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (mid : FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s a τ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) + (hopC : bt.op ≠ vspace.frame_batch.COPY_MAP) (hopR : bt.op ≠ vspace.frame_batch.REMAP) + (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) (w0 w1 w2 : U64) (hw0 : w0.bv = w.1) (df : Bool) : + vspace.frame_batch_entry st arI s a bt w0 w1 w2 df ⦃ r => FbEntryWithPost Fl4Spec.capDeleteVF + (FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt) b w τ df r ⦄ := by + have hbop : b.op = .delete := by + cases e : b.op + · exact absurd (C.hopC.2 e) hopC + · exact absurd (C.hopR.2 e) hopR + · rfl + obtain ⟨G, E, Ac0, Lst0, D0, J⟩ := mid.rel + obtain ⟨hR, hV, -, -, -⟩ := J.1.1.xtop.rel.2.1 + obtain ⟨rl, hrl, e, he, hrel⟩ := spec_exists (lookup_slot_for_cnode_op_refines (Z := Unit) Z.th s τ hR hV false + bt.cnode b.cnode ⟨C.hcw, C.hbc.symm⟩ C.hcv w0 bt.depth) + have hlk : (Fl4Spec.lookupTargetSlot (Z := Unit) b.cnode (Fl4Spec.dataToCptr w.1) b.depth).run τ = + ({(e, τ)}, False) := by + rw [← hw0, C.hdep]; exact he + have hlt : cspace.lookup_target_slot st s bt.cnode w0 bt.depth = ok rl := hrl + cases rl with + | Err err => + cases e with + | ok _ => exact hrel.elim + | error e' => + unfold vspace.frame_batch_entry + rw [if_neg hopC, if_neg hopR, hlt] + 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 ⟨τ, mid, Or.inr ⟨err, rfl, rfl, ?_⟩⟩ + cases err with + | Sys e1 => + show error.SysError.code e1 ⦃ _ ⦄ + exact WP.spec_mono (code_fb (Or.inl hrel)) fun c hc => by + rw [hc]; exact fbEntryWith_del_lookupErr hbop (by rw [hlk]; exact Set.mem_singleton _) + | _ => cases e' <;> exact hrel.elim + | Ok sl0 => + cases e with + | error _ => exact hrel.elim + | ok sl => + obtain ⟨rfl, pp, hpp1, hpp, -⟩ := hrel + have hlkm : (Except.ok sl, τ) ∈ ((Fl4Spec.lookupTargetSlot (Z := Unit) b.cnode (Fl4Spec.dataToCptr w.1) + b.depth).run τ).1 := by rw [hlk]; exact Set.mem_singleton _ + have hAs : AbsSlot Z.th sl := ⟨pp, hpp1, Or.inl hpp⟩ + obtain ⟨c, ac, hc, hca, hg⟩ := hR.slot_cap (Z := Unit) hpp1 hpp + have hac : ac = absCap c := hca.abs + subst hac + have hgm : (absCap c, τ) ∈ (Fl4Spec.getCap (Z := Unit) sl τ).1 := by rw [hg]; exact Set.mem_singleton _ + by_cases hoe : ∃ p sz rt dv mp ep asd va, c = .Frame p sz rt dv mp ep asd va ∧ (mp = true → ep = true) + · obtain ⟨p, sz, rt, dv, mp, ep, asd, va, rfl, hept⟩ := hoe + exact FbMidVE.offOrEptDelete C hct hbm mid hRef hAT hKD hbop hopC hopR hAs hlt hlkm hc hept + unfold vspace.frame_batch_entry + rw [if_neg hopC, if_neg hopR, hlt] + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + rw [slot_cap_eq, hc]; simp only [bind_ok] + by_cases hcn : c = .Null + · subst hcn + rw [cap_is_null_null]; simp only [bind_ok, ↓reduceIte, WP.spec_ok] + exact ⟨τ, mid, Or.inl ⟨rfl, false, fbEntryWith_del_null hbop hlkm hgm, by simp⟩⟩ + rw [cap_is_null_ne hcn]; simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hacn : absCap c ≠ .nullCap := fun h => hcn ((absCap_eq_null hca.1).1 h) + by_cases hfr : ∃ p sz rt dv mp ep asd va, c = .Frame p sz rt dv mp ep asd va + swap + · have ho : vspace.as_frame c = ok none := by + cases c <;> first | rfl | exact absurd ⟨_, _, _, _, _, _, _, _, rfl⟩ hfr + rw [ho]; simp only [bind_ok, error.illegal, WP.spec_ok] + refine ⟨τ, mid, Or.inr ⟨_, rfl, rfl, ?_⟩⟩ + show error.SysError.code .IllegalOperation ⦃ _ ⦄ + refine WP.spec_mono (code_fb (e' := .illegalOperation) (Or.inl trivial)) fun cc hcc => ?_ + rw [hcc] + refine fbEntryWith_del_other hbop hlkm hgm hacn fun dev p R mt sz m h => ?_ + cases c <;> simp [absCap] at h + exact hfr ⟨_, _, _, _, _, _, _, _, rfl⟩ + obtain ⟨p, sz, rt, dv, mp, ep, asd, va, rfl⟩ := hfr + have hmp : mp = true := by + cases mp + · exact absurd ⟨p, sz, rt, dv, false, ep, asd, va, rfl, fun h => by cases h⟩ hoe + · rfl + have hep : ep = false := by + cases ep + · rfl + · exact absurd ⟨p, sz, rt, dv, mp, true, asd, va, rfl, fun _ => rfl⟩ hoe + subst hmp hep + simp only [vspace.as_frame, bind_ok] + have hdd := C.hdd + have hba := C.hba + have hpos : bt.asid.bv > 0 := by + have h := C.h0 + show 0 < bt.asid.bv.toNat + exact Nat.pos_of_ne_zero (fun e => h (UScalar.eq_of_val_eq (by simpa using e))) + have hcN : ∀ q r gs g, capability.Cap.Frame p sz rt dv true false asd va ≠ .CNode q r gs g := + fun _ _ _ _ h => by cases h + split_ifs with k1 k2 k3 + all_goals first + | (obtain ⟨⟨b2, sU⟩, hu, -⟩ := spec_exists (P0.vspace.unmap_page_unflushed.p0 st s bt.root sz va p L.total) + rw [hu] + have k1' : bt.defer_delete = true := by assumption + have k4 : ¬ dv = true := by assumption + have k5 : asd = bt.asid := by assumption + have k4' : dv = false := by simpa using k4 + subst k4' k5 + try simp only [bind_ok] + fbj_red + obtain ⟨sS, hs, -⟩ := spec_exists (P0.obj.Slot.set_cap.p0 st (cslotAddr sl) sU + (.Frame p sz rt false false false 0#u64 0#u64) L.total) + have hfv := mid.keep.findVs hpos C.hf + obtain ⟨sa, idx⟩ := sl + simp only at hpp1 hpp + subst hpp1 + obtain ⟨τu, τs, hmu, hms, J1, hks1, hK1, hum1, hCK1, -, hZ1, hc1⟩ := + J.fbDeferStepV hRef hpp hc hfv hu hs mid.znb + have hdf : b.deferDelete = true ∧ + (if true then (if false then Fl4Spec.VMMapType.vmEptMap else .vmVSpaceMap) else .vmNoMap) = + .vmVSpaceMap ∧ false = false ∧ bt.asid.bv = b.asid := ⟨by rw [hdd]; exact k1', rfl, rfl, hba.symm⟩ + have hAs' : AbsSlot Z.th (pp.bv, idx) := hAs + cases b2 <;> simp only [Bool.false_eq_true, ↓reduceIte, bind_ok, hs] <;> fbj_red <;> + refine WP.spec_bind (J1.delFrame hRef hAs' hc1 (fun h => by cases h) (fun _ => ⟨L.total, hAT, hKD⟩)) ?_ <;> + rintro ⟨r1, s4, a4⟩ ⟨hr1, τ1, G', Ac', Lst', D', hd, J', hnull, hslot, hks4⟩ <;> + fbj_red <;> + (obtain ⟨c9, a9, hc9, hca9, hg9⟩ := (J1.1.1.xtop.rel.2.1.1).slot_cap (Z := Unit) (sl := (pp.bv, idx)) + (p := pp) rfl hpp + rw [hc1] at hc9; cases eq_of_ok_eq hc9 + have hac9 : a9 = absCap (.Frame p sz rt false false false 0#u64 0#u64) := hca9.abs + subst hac9 + have hcap9 := (SInvU.getCap_capAt (show (absCap (.Frame p sz rt false false false 0#u64 0#u64), τs) ∈ + (Fl4Spec.getCap (Z := Unit) (pp.bv, idx) τs).1 by rw [hg9]; exact Set.mem_singleton _)).2 + obtain ⟨hK2, hum2⟩ := fbKeep_capDeleteVF_frame hcap9 (fun h => by cases h) hd + have hrel' : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D + s4 a4 τ1 := ⟨G', E, Ac', Lst', D', J'⟩ + have hFF := FbFootK.of_relB arI J.1.1 J'.1.1 rfl rfl (hK1.trans hK2) (hum2.trans hum1) + have hCK2 : CnSlotsKept (L := L) Z.th sS s4 := + fbCnKeptOfDel (s := sS) hc1 (fun _ _ _ _ h => by cases h) hslot + have hZ4 := hZ1.ofSlot hks4 (fun _ _ _ h => by rw [hnull] at h; cases h) hslot + refine (WP.spec_ok _).2 ⟨τ1, FbMidVE.step C hct hbm mid hrel' (hK1.trans hK2) hFF (hCK1.trans hCK2) hZ4, + Or.inl ⟨hr1, _, fbEntryWith_del_frameD hbop hlkm hgm hdf (by rw [C.hbv]; exact hmu) hms hd, + by simp⟩⟩)) + | (have hnot : ¬ (bt.defer_delete = true ∧ dv = false ∧ asd = bt.asid) := by + rintro ⟨q1, q2, q3⟩; simp_all + try simp only [bind_ok] + fbj_red + refine WP.spec_bind (J.delFrameNative hRef hAs hc) ?_ + rintro ⟨r1, s4, a4⟩ ⟨hr1, σ', G', Ac', Lst', D', hd, J', hnull, hslot, hks4⟩ + fbj_red + have hcA := (SInvU.getCap_capAt hgm).2 + obtain ⟨hK', hum'⟩ := fbKeep_capDeleteVF_frame hcA (fun h => by cases h) hd + have hndP : ∀ asid' vaddr, (if true then some (asd.bv, va.bv) else none) = some (asid', vaddr) → + ¬ (b.deferDelete = true ∧ (if true then (if false then Fl4Spec.VMMapType.vmEptMap else .vmVSpaceMap) + else .vmNoMap) = .vmVSpaceMap ∧ dv = false ∧ asid' = b.asid) := by + intro a' v hm ⟨h1, _, h3, h4⟩ + simp only [↓reduceIte, Option.some.injEq, Prod.mk.injEq] at hm + obtain ⟨rfl, rfl⟩ := hm + exact hnot ⟨by rw [← hdd]; exact h1, h3, P1.eq_of_bv (h4.trans hba)⟩ + have hrel' : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D + s4 a4 σ' := ⟨G', E, Ac', Lst', D', J'⟩ + have hFF : FbFootK (L := L) (Z.mv G E) s s4 := FbFootK.of_relB arI J.1.1 J'.1.1 rfl rfl hK' hum' + exact (WP.spec_ok _).2 ⟨σ', FbMidVE.step C hct hbm mid hrel' hK' + hFF (fbCnKeptOfDel hc hcN hslot) + (mid.znb.ofSlot hks4 (fun _ _ _ h => by rw [hnull] at h; cases h) hslot), + Or.inl ⟨hr1, false, fbEntryWith_del_frame hbop hlkm hgm hndP hd, by simp⟩⟩) + +/-- A COPY_MAP or REMAP entry does not use the DELETE finaliser. -/ +theorem fbEntryWith_ne_delete {del : Fl4Spec.CslotPtr → Fl4Spec.PMonad Unit Unit} + {b : Fl4Spec.Fl4FrameBatchInvocation} (h : b.op ≠ .delete) (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) : + Fl4Spec.fbEntryWith (Z := Unit) del b w = Fl4Spec.fbEntry b w := by + show Fl4Spec.fbEntryWith del b w = Fl4Spec.fbEntryWith Fl4Spec.capDelete b w + unfold Fl4Spec.fbEntryWith + split + · rfl + · rfl + · rename_i hd; exact absurd hd h + +set_option maxHeartbeats 4000000 in +/-- **`batch_map` of a non-native source cap**: FL4 and the spec both reject it with +`InvalidCapability 0`, the state unchanged (a VCPU, EPT or owned I/O-port cap is no frame; an +EPT-mapped frame is rejected before the map). -/ +theorem VmEptState.batchMapNonNative {τ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a τ) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {c : capability.Cap} (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok c) (hnn : ¬ NativeCap c) + (bt : vspace.Batch) (b : Fl4Spec.Fl4FrameBatchInvocation) (a0 : U64) : + vspace.batch_map st arI s a (cslotAddr (pc.bv, idx)) bt a0 ⦃ res => ∃ e e', res.1 = .Err e ∧ MapErrRel e e' ∧ + ExceptT.run (Fl4Spec.fbMap (Z := Unit) b (pc.bv, idx) a0.bv) τ = ({(.error e', τ)}, False) ∧ + res.2.1 = s ∧ res.2.2 = a ⦄ := by + obtain ⟨hSt, -, -, -, -⟩ := J.1.1.xtop.rel.2.1 + obtain ⟨cS, aS, hcS, hcaS, hgS⟩ := hSt.slot_cap (Z := Unit) (sl := (pc.bv, idx)) (p := pc) rfl + (by simpa [hidx] using hpc) + rw [hc] at hcS; cases eq_of_ok_eq hcS + rw [hcaS.abs] at hgS + unfold vspace.batch_map + rw [slot_cap_eq, hc] + cases c with + | Frame ptr sz rt dev mp ep fa fv => + have hme : mp = true ∧ ep = true := by + by_contra h + exact hnn h + obtain ⟨rfl, rfl⟩ := hme + simp only [bind_ok, vspace.as_frame, ↓reduceIte, error.invalid_cap] + exact (WP.spec_ok _).2 ⟨_, .invalidCapability 0, rfl, Or.inl (Or.inr ⟨0#u64, rfl, rfl⟩), fbMap_ept hgS, rfl, rfl⟩ + | _ => + first + | exact absurd trivial hnn + | (simp only [bind_ok, vspace.as_frame, error.invalid_cap] + exact (WP.spec_ok _).2 ⟨_, .invalidCapability 0, rfl, Or.inl (Or.inr ⟨0#u64, rfl, rfl⟩), + fbMap_nonPage hgS (fun _ _ _ _ _ _ h => by simp [absCap] at h), rfl, rfl⟩) + +/-- **`batch_map` at `VmEptState` for any source cap** (`batchMapV` or `batchMapNonNative`). -/ +theorem VmEptState.batchMapAny [Fl4Proofs.P1.PlainStore st] (hA : ArchRefines arI AR) + {τ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a τ) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {c : capability.Cap} (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok c) + {p1 as1 : U64} (has1 : as1 ≠ 0#u64) (has2 : as1.val < 2 ^ 20) + (hf : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) as1.bv) τ = ({(.ok p1.bv, τ)}, False)) + (op depth a1 cr a2 : U64) (dd : Bool) (cn dcn : capability.Cap) (a0 : U64) + (b : Fl4Spec.Fl4FrameBatchInvocation) (hbv : b.vspace = p1.bv) (hba : b.asid = as1.bv) + (hbm : b.mapRights = a1.bv) (hbat : b.attr = a2.bv) (hZ0 : ZNoBlk Z.th (L.mem s) (L.ksb s)) : + vspace.batch_map st arI s a (cslotAddr (pc.bv, idx)) ⟨p1, as1, op, depth, a1, cr, a2, dd, cn, dcn⟩ a0 ⦃ res => + (∃ e e', res.1 = .Err e ∧ MapErrRel e e' ∧ + ExceptT.run (Fl4Spec.fbMap (Z := Unit) b (pc.bv, idx) a0.bv) τ = ({(.error e', τ)}, False) ∧ + res.2.1 = s ∧ res.2.2 = a) ∨ + (res.1 = .Ok () ∧ ∃ τ1, ((.ok (), τ1) : Except Fl4Spec.SyscallError Unit × Fl4Spec.State Unit) ∈ + (ExceptT.run (Fl4Spec.fbMap (Z := Unit) b (pc.bv, idx) a0.bv) τ).1 ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D res.2.1 res.2.2 τ1 ∧ FbKeep τ τ1 ∧ + FbFootK (L := L) Z s res.2.1 ∧ CnSlotsKept (L := L) Z.th s res.2.1 ∧ + ZNoBlk Z.th (L.mem res.2.1) (L.ksb res.2.1)) ⦄ := by + by_cases hnat : NativeCap c + · exact J.batchMapV hA hpc hidx hc hnat has1 has2 hf op depth a1 cr a2 dd cn dcn a0 b hbv hba hbm hbat hZ0 + · exact WP.spec_mono (J.batchMapNonNative hpc hidx hc hnat _ b a0) fun _ h => Or.inl h + +set_option maxHeartbeats 8000000 in +/-- **One REMAP entry at `FbMidVE`** (module docstring): the source lookup, then `batch_map` +against `fbMap` (`VmEptState.batchMapAny`). -/ +theorem FbMidVE.remapEntry [P1.PlainStore st] {ct buf : U64} {args : List U64} + {τ : Fl4Spec.State Unit} {bt : vspace.Batch} {b : Fl4Spec.Fl4FrameBatchInvocation} + (C : FbCtx σ Z.th args bt b) (hct : ct ∈ Z.T) (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (mid : FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s a τ) + (hRef : ArchRefines arI AR) + (hopC : bt.op ≠ vspace.frame_batch.COPY_MAP) (hopR : bt.op = vspace.frame_batch.REMAP) + (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) (w0 w1 w2 : U64) (hw0 : w0.bv = w.1) (hw1 : w1.bv = w.2.1) + (df : Bool) : + vspace.frame_batch_entry st arI s a bt w0 w1 w2 df ⦃ r => FbEntryWithPost Fl4Spec.capDeleteVF + (FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt) b w τ df r ⦄ := by + have hbop : b.op = .remap := C.hopR.1 hopR + have hE : Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w = _ := + (fbEntryWith_ne_delete (by rw [hbop]; decide) w).trans (fbEntry_remap hbop w) + have hpos : bt.asid.bv > 0 := by + have h := C.h0 + show 0 < bt.asid.bv.toNat + exact Nat.pos_of_ne_zero (fun e => h (UScalar.eq_of_val_eq (by simpa using e))) + have hf : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) bt.asid.bv) τ = ({(.ok bt.root.bv, τ)}, False) := + mid.keep.findVs hpos C.hf + obtain ⟨G, E, Ac0, Lst0, D0, J⟩ := mid.rel + obtain ⟨hSt, hSV, -, -, -⟩ := J.1.1.xtop.rel.2.1 + obtain ⟨p1, as1, op, depth, a1, cr, a2, dd, cn, dcn⟩ := bt + unfold vspace.frame_batch_entry + simp only at hopC hopR ⊢ + rw [if_neg hopC, if_pos hopR, lookup_source_eq] + refine WP.spec_bind (lookup_slot_for_cnode_op_refines (Z := Unit) Z.th s τ hSt hSV true cn (absCap cn) + ⟨C.hcw, rfl⟩ C.hcv w0 depth) ?_ + rintro r1 ⟨e1, he1, hrel1⟩ + have hls : ExceptT.run (Fl4Spec.lookupSourceSlot (Z := Unit) b.cnode (Fl4Spec.dataToCptr w.1) b.depth) τ = + ({(e1, τ)}, False) := by rw [C.hbc, C.hdep, ← hw0]; exact he1 + cases r1 with + | Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [fbr_branch_err, bind_ok, fbr_fromResidual] + refine (WP.spec_ok _).2 ⟨τ, mid, Or.inr ⟨e, rfl, rfl, ?_⟩⟩ + have hrun : ((Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w).run τ) = + ({(.error (Fl4Spec.msgFromSyscallError e').1, τ)}, False) := by + rw [hE]; exact runE_bind_err (fbCode_err hls) + cases e with + | Sys e2 => + refine WP.spec_mono (fb_code (Or.inl (Or.inl hrel1))) fun c hc => ?_ + rw [hrun, hc]; exact Set.mem_singleton _ + | _ => exact hrel1.elim + | Ok sl => + cases e1 with + | error _ => exact hrel1.elim + | ok sl' => + obtain ⟨rfl, pc, hpc1, hpc, -⟩ := hrel1 + obtain ⟨sa, idx⟩ := sl' + simp only at hpc1 hpc + subst hpc1 + simp only [fbr_branch_ok, bind_ok] + have hrun1 : (Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w).run τ = + ExceptT.run (Fl4Spec.fbCode (Fl4Spec.fbMap (Z := Unit) b (pc.bv, idx) w.2.1) >>= fun _ => + Fl4Spec.NonDetE.returnOk false) τ := by + rw [hE]; exact runE_bind_ok (fbCode_ok hls) + obtain ⟨c, hc⟩ := capAt_total (L.mem s) (cslotAddr (pc.bv, idx)) + refine WP.spec_bind (J.batchMapAny hRef hpc rfl hc C.h0 C.hasid hf op depth a1 cr a2 dd cn dcn w1 b C.hbv + C.hba C.hmr C.hat mid.znb) ?_ + rintro ⟨r2, s2, a2'⟩ (⟨e, e', h1, h2, h3, h4, h5⟩ | ⟨h1, τ1, hmem, J', hK1, hF, hCSK, hZ'⟩) + · simp only at h1 h4 h5 + subst h1 h4 h5 + fbj_red + refine (WP.spec_ok _).2 ⟨τ, mid, Or.inr ⟨e, rfl, rfl, ?_⟩⟩ + have hrun : ((Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w).run τ) = + ({(.error (Fl4Spec.msgFromSyscallError e').1, τ)}, False) := by + rw [hrun1, ← hw1]; exact runE_bind_err (fbCode_err h3) + cases e with + | Sys e2 => + have h2' : ErrRel (.Sys e2) e' ∨ (e2 = .AlignmentError ∧ e' = .alignmentError) := by + rcases h2 with h | ⟨he2, rfl⟩ + · exact Or.inl h + · cases he2; exact Or.inr ⟨rfl, rfl⟩ + refine WP.spec_mono (fb_code h2') fun c hc => ?_ + rw [hrun, hc]; exact Set.mem_singleton _ + | _ => rcases h2 with (h | ⟨n, h, -⟩) | ⟨h, -⟩ <;> cases h + · simp only at h1 + subst h1 + fbj_red + have hrel' : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D + s2 a2' τ1 := ⟨G, E, Ac0, Lst0, D0, J'⟩ + refine (WP.spec_ok _).2 ⟨τ1, FbMidVE.step C hct hbm mid hrel' hK1 hF hCSK hZ', + Or.inl ⟨rfl, false, ?_, by simp⟩⟩ + rw [hrun1] + exact mem_bindE_ok (fbCode_mem (by rw [← hw1]; exact hmem)) (Set.mem_singleton _) + +set_option maxHeartbeats 8000000 in +/-- **The COPY_MAP insert at `VmEptState`**: `cdt::insert` of the derived (unmapped) frame copy +into an empty CNode slot, by `insert_full_finG` and `VmEptState.insertIssuedS3` (the source slot +keeps its frame cap, `fullF`). -/ +theorem VmEptState.fbInsV {τ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a τ) + (hZ0 : ZNoBlk Z.th (L.mem s) (L.ksb s)) + {ps pd : U64} {rs rd : Nat} (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) + {idxs idxd : List Bool} (hls : idxs.length = rs) (hld : idxd.length = rd) + {p : U64} {sz : capability.FrameSize} {rt : U64} {d m e : Bool} {asid va : U64} + (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok (.Frame p sz rt d m e asid va)) + (hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) {rt' : U64} + (hw : CapWf (.Frame p sz rt' d false false 0#u64 0#u64)) + (hcv : CapValid Z.th (.Frame p sz rt' d false false 0#u64 0#u64)) + (he : EncWf (.Frame p sz rt' d false false 0#u64 0#u64)) + (hDd : DestCap Z.th (L.mem s) pd) : + cdt.insert st s (.Frame p sz rt' d false false 0#u64 0#u64) (cslotAddr (ps.bv, idxs)) + (cslotAddr (pd.bv, idxd)) ⦃ s' => ∃ (σ' : Fl4Spec.State Unit) (G' : MdbGhost) (Ac' : Set U64) + (Lst' : U64 → List U64), + Fl4Spec.capInsert (Z := Unit) (absCap (.Frame p sz rt' d false false 0#u64 0#u64)) (ps.bv, idxs) (pd.bv, idxd) τ = + ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D s' a σ' ∧ FbKeep τ σ' ∧ + σ'.machineState.underlyingMemory = τ.machineState.underlyingMemory ∧ + capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .Frame p sz rt' d false false 0#u64 0#u64 ∧ + CnSlotsKept (L := L) Z.th s s' ∧ ZNoBlk Z.th (L.mem s') (L.ksb s') ⦄ := by + set cc : capability.Cap := .Frame p sz rt' d false false 0#u64 0#u64 with hcc + have B := J.1.1 + have hX := B.xtop + obtain ⟨hPl, ⟨hSt, hV, hM, hC, hI⟩, hFI, -, hMO, -, -⟩ := hX.rel + have hAs : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hsnn : (capability.Cap.Frame p sz rt d m e asid va) ≠ .Null := by simp + have hsm : cslotAddr (ps.bv, idxs) ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hAs.isSlot, _, hsrcC, hsnn⟩ + have hdnm : cslotAddr (pd.bv, idxd) ∉ Z.G.chains.flatten := fun h => by + obtain ⟨-, c0, h0, hn⟩ := (hM.members _).1 h; rw [hdst] at h0; exact hn (eq_of_ok_eq h0).symm + have hnv : ∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 (.Frame p sz rt d m e asid va) → + Vs.VCapOK P L4 L3 L2 L1 cc := by + intro P L4 L3 L2 L1 h + exact ⟨fun h' => absurd h' (by simp), h.2.1, fun h' => absurd h' (by simp)⟩ + have hsh : shape cc = shape (.Frame p sz rt d m e asid va) := by simp [hcc, shape] + have hhuc : isUt (.Frame p sz rt d m e asid va) = true → cc = .Frame p sz rt d m e asid va := + fun h => by simp [isUt] at h + have hkey : okey cc = okey (.Frame p sz rt d m e asid va) := by simp [hcc, okey] + have hcnn : cc ≠ .Null := by simp [hcc] + refine WP.spec_mono (WP.spec_and (WP.spec_and (insert_full_finG Z.th s τ Z.G hSt hV hPl hM hC hI hFI hps hpd hls + hld hsrcC hsnn hdst cc (absCap cc) ⟨hw, rfl⟩ hcv he hcnn hsh (Or.inr rfl) (fun h => by simp [isUt] at h) rfl + hkey hnv) + (Vs.insert_trace hPl s Z.G hM hsm hAd.isSlot hdnm hsrcC cc he)) + (insert_delinv_imp hPl s Z.G hM hsm hAd.isSlot hdnm hsrcC cc he hcnn (by rw [isZb_of_shape hsh]; rfl))) ?_ + rintro s' ⟨⟨⟨σ', G', hci, hSt', hV', hM', hC', hI', hFI', hstep, -⟩, hF, hoth, hdc, hsc⟩, hdi⟩ + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) (absCap cc) (ps.bv, idxs) (pd.bv, idxd) τ).1 := by + rw [hci]; rfl + have hcn := capInsert_nkn (absCap cc) _ _ τ _ hmem + have hMO' : MdbOKV (L.ksb s) (L.mem s') G' := (hstep hhuc).okv hMO + have hw' : P1.WritesIn st s s' (InSlot Z.th) := by + refine ⟨hF.1, hF.2.1, fun b hb hn => hF.2.2 b hb fun ⟨y, k, hy, hk', e⟩ => hn ⟨y, hy, ?_⟩⟩ + have hf := hy.fit hPl + have := hk'.lt + have hv := wordOf_val (a := y) (k := k) (by omega) + rw [e, hv]; omega + have hsv : capV (L.mem s) (cslotAddr (ps.bv, idxs)) = .Frame p sz rt d m e asid va := capV_of_capAt hsrcC + have hdv : capV (L.mem s) (cslotAddr (pd.bv, idxd)) = .Null := capV_of_capAt hdst + have hsc' : capV (L.mem s') (cslotAddr (ps.bv, idxs)) = capV (L.mem s) (cslotAddr (ps.bv, idxs)) := by + rw [hsc, hsv]; rfl + have hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pd.bv, idxd) → capV (L.mem s') y = capV (L.mem s) y := by + intro y hy hne + by_cases es : y = cslotAddr (ps.bv, idxs) + · rw [es]; exact hsc' + · exact hoth y hy es hne + have hTK := typedKept_ins hAs.isSlot hsv hoth hdc hsc hsh + have hvT := vsSlotsT_insert hAs.isSlot hoth hdc (by rw [hsv]; exact hsc) (by rw [hsv]; exact hnv) + (by rw [hsv]; exact fun h => by simp [hcc, Vs.TabUn] at h) (by rw [hsv]; exact hkey) + (by rw [hsv]; exact fun _ => by simp [hcc, Vs.NativeCap]) + have hVK := Vs.vsKeep_insert hAs.isSlot hoth hdc (by rw [hsv]; exact hsc) (by rw [hdv]; rfl) + (by rw [hsv]; exact fun h => by rcases h with h | h <;> exact (h rfl).elim) + (fun h => by simp [hcc, Vs.TabUn] at h) + obtain ⟨Ac', Lst', J'⟩ := VmEptState.insertIssuedS3 arI J hZ0 hps hpd hld hci hcap hdc hdst + (by rintro _ _ _ _ h; rw [hcc] at h; cases h) (by intro h; rw [hcc] at h; cases h) + (by rintro _ _ _ h; rw [hcc] at h; cases h) + hw' ⟨hSt', hV', hM', hC', hI'⟩ hFI' hMO' hTK (hDd.live _) hdi (hstep hhuc) hvT hVK rfl + have hK : FbKeep τ σ' := FbKeep.of_cnK hcn + have hum : σ'.machineState.underlyingMemory = τ.machineState.underlyingMemory := by + have e := congrArg (fun z => z.machineState.underlyingMemory) hcn.1; exact e + refine ⟨σ', G', Ac', Lst', hci, J', hK, hum, hdc, ?_, + hZ0.ofSlot hw'.1 (fun _ _ _ h => by rw [hdc, hcc] at h; cases h) hcap⟩ + intro y hy p0 r0 gs0 g0 h + by_cases ey : y = cslotAddr (pd.bv, idxd) + · rw [ey, hdv] at h; cases h + · rw [hcap y hy ey]; exact h + +set_option maxHeartbeats 16000000 in +/-- **One COPY_MAP entry at `FbMidVE`** (module docstring; `fb_entry_copyJ`'s steps): the +destination lookup and emptiness, the source lookup and frame check, mask and derive, the insert +(`VmEptState.fbInsV`), `batch_map` at the copy, and on its error the copy's delete (an unmapped +frame, where `capDeleteVF` is the native `capDelete`). -/ +theorem FbMidVE.copyEntry [P1.PlainStore st] {ct buf : U64} {args : List U64} + {τ : Fl4Spec.State Unit} {bt : vspace.Batch} {b : Fl4Spec.Fl4FrameBatchInvocation} + (C : FbCtx σ Z.th args bt b) (hct : ct ∈ Z.T) (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (mid : FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s a τ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) + (hopC : bt.op = vspace.frame_batch.COPY_MAP) + (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) (w0 w1 w2 : U64) (hw0 : w0.bv = w.1) (hw1 : w1.bv = w.2.1) + (hw2 : w2.bv = w.2.2) (df : Bool) : + vspace.frame_batch_entry st arI s a bt w0 w1 w2 df ⦃ r => FbEntryWithPost Fl4Spec.capDeleteVF + (FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt) b w τ df r ⦄ := by + have hbop : b.op = .copyMap := C.hopC.1 hopC + have hEq : Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w = Fl4Spec.fbEntry b w := + fbEntryWith_ne_delete (by rw [hbop]; decide) w + have hpos : bt.asid.bv > 0 := by + have h := C.h0 + show 0 < bt.asid.bv.toNat + exact Nat.pos_of_ne_zero (fun e => h (UScalar.eq_of_val_eq (by simpa using e))) + obtain ⟨G, E, Ac0, Lst0, D0, J⟩ := mid.rel + obtain ⟨hSt, hSV, -, -, -⟩ := J.1.1.xtop.rel.2.1 + have hRt := mid.roots + obtain ⟨p1, as1, op, depth, a1, cr, a2, dd, cn, dcn⟩ := bt + unfold vspace.frame_batch_entry + simp only at hopC ⊢ + rw [if_pos hopC] + -- the destination's lookup + rw [show cspace.lookup_target_slot st s dcn w1 depth = cspace.lookup_slot_for_cnode_op st s false dcn w1 depth + from rfl] + refine WP.spec_bind (lookup_slot_for_cnode_op_refines (Z := Unit) Z.th s τ hSt hSV false dcn (absCap dcn) + ⟨C.hdcw, rfl⟩ C.hdcv w1 depth) ?_ + rintro r1 ⟨e1, he1, hrel1⟩ + have hld : ExceptT.run (Fl4Spec.lookupTargetSlot (Z := Unit) b.destCnode (Fl4Spec.dataToCptr w.2.1) b.depth) τ = + ({(e1, τ)}, False) := by rw [C.hbd, C.hdep, ← hw1]; exact he1 + cases r1 + case Err e => + cases e1 with + | ok _ => exact hrel1.elim + | error e' => + simp only [fbr_branch_err, bind_ok, fbr_fromResidual] + refine (WP.spec_ok _).2 ⟨τ, mid, Or.inr ⟨e, rfl, rfl, ?_⟩⟩ + have hmem := fbEntry_copy_destErr (w := w) hbop (by rw [hld]; exact Set.mem_singleton _) + cases e with + | Sys e2 => exact WP.spec_mono (fb_code (Or.inl (Or.inl hrel1))) fun c hc => by rw [hEq, hc]; exact hmem + | _ => exact hrel1.elim + rename_i dsl + cases e1 + case error => exact hrel1.elim + rename_i dsl' + obtain ⟨rfl, pd, hpd1, hpd, hcrd⟩ := hrel1 + obtain ⟨da, idxd⟩ := dsl' + simp only at hpd1 hpd + subst hpd1 + have hdm : (.ok (pd.bv, idxd), τ) ∈ (ExceptT.run (Fl4Spec.lookupTargetSlot (Z := Unit) b.destCnode + (Fl4Spec.dataToCptr w.2.1) b.depth) τ).1 := by rw [hld]; exact Set.mem_singleton _ + simp only [fbr_branch_ok, bind_ok] + -- the destination must be empty + unfold cdt.ensure_empty_slot + rw [slot_cap_eq] + obtain ⟨cd, ad, hcd, hcad, hgd⟩ := hSt.slot_cap (Z := Unit) (sl := (pd.bv, idxd)) (p := pd) rfl hpd + rw [hcd, bind_ok] + have hee := ensureEmpty_run (pd.bv, idxd) τ hgd + by_cases hdn : cd = .Null + swap + · rw [cap_is_null_ne hdn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, fbr_branch_err, fbr_fromResidual] + refine (WP.spec_ok _).2 ⟨τ, mid, Or.inr ⟨_, rfl, rfl, ?_⟩⟩ + have hadn : ad ≠ .nullCap := fun h => hdn ((hcad.null_iff).1 h) + have hmem := fbEntry_copy_full (w := w) hbop hdm (by rw [hee, if_neg hadn]; exact Set.mem_singleton _) + exact WP.spec_mono (fb_code (Or.inl (Or.inl (show SysErrRel (.Sys .DeleteFirst) .deleteFirst from trivial)))) + fun c hc => by rw [hEq, hc]; exact hmem + subst hdn + have hadn : ad = .nullCap := (hcad.null_iff).2 rfl + have hem : (.ok (), τ) ∈ (ExceptT.run (Fl4Spec.ensureEmpty (Z := Unit) (pd.bv, idxd)) τ).1 := by + rw [hee, if_pos hadn]; exact Set.mem_singleton _ + rw [cap_is_null_null] + simp only [bind_ok, ↓reduceIte, fbr_branch_ok] + -- the source's lookup + rw [lookup_source_eq] + refine WP.spec_bind (lookup_slot_for_cnode_op_refines (Z := Unit) Z.th s τ hSt hSV true cn (absCap cn) + ⟨C.hcw, rfl⟩ C.hcv w0 depth) ?_ + rintro r2 ⟨e2, he2, hrel2⟩ + have hls : ExceptT.run (Fl4Spec.lookupSourceSlot (Z := Unit) b.cnode (Fl4Spec.dataToCptr w.1) b.depth) τ = + ({(e2, τ)}, False) := by rw [C.hbc, C.hdep, ← hw0]; exact he2 + cases r2 + case Err e => + cases e2 with + | ok _ => exact hrel2.elim + | error e' => + simp only [fbr_branch_err, bind_ok, fbr_fromResidual] + refine (WP.spec_ok _).2 ⟨τ, mid, Or.inr ⟨e, rfl, rfl, ?_⟩⟩ + have hmem := fbEntry_copy_srcErr (w := w) hbop hdm hem (by rw [hls]; exact Set.mem_singleton _) + cases e with + | Sys e3 => exact WP.spec_mono (fb_code (Or.inl (Or.inl hrel2))) fun c hc => by rw [hEq, hc]; exact hmem + | _ => exact hrel2.elim + rename_i ssl + cases e2 + case error => exact hrel2.elim + rename_i ssl' + obtain ⟨rfl, ps, hps1, hps, hcrs⟩ := hrel2 + obtain ⟨sa, idxs⟩ := ssl' + simp only at hps1 hps + subst hps1 + have hsm : (.ok (ps.bv, idxs), τ) ∈ (ExceptT.run (Fl4Spec.lookupSourceSlot (Z := Unit) b.cnode + (Fl4Spec.dataToCptr w.1) b.depth) τ).1 := by rw [hls]; exact Set.mem_singleton _ + simp only [fbr_branch_ok, bind_ok] + -- the source's cap + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl hpd⟩ + rw [slot_cap_eq] + obtain ⟨srcC, srcA, hsc, hsca, hgs⟩ := hSt.slot_cap (Z := Unit) (sl := (ps.bv, idxs)) (p := ps) rfl hps + rw [hsc, bind_ok] + have hgsm : (srcA, τ) ∈ (Fl4Spec.getCap (Z := Unit) (ps.bv, idxs) τ).1 := by rw [hgs]; exact Set.mem_singleton _ + obtain ⟨o, ho⟩ : ∃ o, vspace.as_frame srcC = ok o := by cases srcC <;> simp [vspace.as_frame] + rw [ho, bind_ok] + rcases o with _ | f + · simp only [core.option.Option.is_none, ↓reduceIte, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 ⟨τ, mid, Or.inr ⟨_, rfl, rfl, ?_⟩⟩ + have hmem := fbEntry_copy_notPage (w := w) hbop hdm hem hsm hgsm (by rw [hsca.abs]; exact fbc_notPage ho) + exact WP.spec_mono (fb_code (Or.inl (Or.inr ⟨0#u64, rfl, rfl⟩))) fun c hc => by rw [hEq, hc]; exact hmem + obtain ⟨p, sz, rt, d, m, ep, asid, va, rfl⟩ : + ∃ p sz rt d m ep asid va, srcC = .Frame p sz rt d m ep asid va := by + cases srcC <;> simp [vspace.as_frame] at ho + exact ⟨_, _, _, _, _, _, _, _, rfl⟩ + simp only [core.option.Option.is_none, Bool.false_eq_true, ↓reduceIte] + have hp : Fl4Spec.isPageCap srcA = true := by rw [hsca.abs]; rfl + -- mask and derive + rw [from_word_eq, bind_ok] + obtain ⟨c1, hm, hw1', ha1, he1, hv1, -, -⟩ := mask_rel (rightsW cr) _ hsca.1 + rw [hm, bind_ok] + obtain ⟨rt', rfl⟩ := fbc_mask_frame hm + have hse : EncWf (capability.Cap.Frame p sz rt d m ep asid va) := by + have h := hsc; rw [capAt_words] at h; exact decode_encWf h + have hAs : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl hps⟩ + have hscv : CapValid Z.th (capability.Cap.Frame p sz rt d m ep asid va) := SlotsValid.abs hSV hAs hsc + obtain ⟨r4, hr4, r', hr', hdr⟩ := derive_rel (Z := Unit) s τ (cslotAddr (ps.bv, idxs)) (ps.bv, idxs) _ hw1' + (he1 hse) (fun h => False.elim h) + have hr4' : objects.derive_cap st s (cslotAddr (ps.bv, idxs)) (.Frame p sz rt' d m ep asid va) = + ok (.Ok (.Frame p sz rt' d false false 0#u64 0#u64)) := rfl + rw [hr4'] at hr4 + cases eq_of_ok_eq hr4 + cases r' + case error => exact hdr.elim + rename_i na + obtain ⟨hwc, hna, hec, hvc, -⟩ := hdr + subst hna + rw [hr4', bind_ok] + simp only [fbr_branch_ok, bind_ok] + have hmask : Fl4Spec.maskCap (Fl4Spec.dataToRights b.capRights) srcA = + absCap (.Frame p sz rt' d m ep asid va) := by rw [hsca.abs, ha1, rightsW_spec, C.hcr] + have hdvm : (.ok (absCap (.Frame p sz rt' d false false 0#u64 0#u64)), τ) ∈ + (ExceptT.run (Fl4Spec.deriveCap (Z := Unit) (ps.bv, idxs) (Fl4Spec.maskCap (Fl4Spec.dataToRights b.capRights) + srcA)) τ).1 := by + rw [hmask, hr']; exact Set.mem_singleton _ + -- the insert + rw [insert_with_cap_eq s _ _ _ _ hsc] + have hDd := fbc_destCap hcrd hRt.2 + refine WP.spec_bind (J.fbInsV mid.znb hps hpd rfl rfl hsc hcd hwc (hvc Z.th (hv1 Z.th hscv)) hec hDd) ?_ + rintro s1 ⟨σ1, G1, Ac1, Lst1, hci, J1, hK1, hum1, hdc1, hcn1, hZ1⟩ + have hcim : ((), σ1) ∈ (Fl4Spec.capInsert (Z := Unit) (absCap (.Frame p sz rt' d false false 0#u64 0#u64)) + (ps.bv, idxs) (pd.bv, idxd) τ).1 := by rw [hci]; rfl + have hf1 : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) as1.bv) σ1 = ({(.ok p1.bv, σ1)}, False) := + (mid.keep.trans hK1).findVs hpos C.hf + have hc1 : capAt (L.mem s1) (cslotAddr (pd.bv, idxd)) = ok (.Frame p sz rt' d false false 0#u64 0#u64) := by + rw [capAt_capV, hdc1] + -- the map + refine WP.spec_bind (J1.batchMapAny hRef (pc := pd) hpd rfl hc1 C.h0 C.hasid hf1 op depth a1 cr a2 dd cn dcn w2 b + C.hbv C.hba C.hmr C.hat hZ1) ?_ + rintro ⟨r5, s2, a2'⟩ (⟨e5, e', h1, h2, h3, h4, h5⟩ | ⟨h1, τ2, hmem2, J2, hK2, hF2, hcn2, hZ2⟩) + · simp only at h1 h4 h5 + subst h1 + subst s2 a2' + fbj_red + simp only [core.result.Result.is_err, bind_ok, ↓reduceIte] + refine WP.spec_bind (J1.delFrame hRef hAd hc1 (fun h => by cases h) (fun _ => ⟨L.total, hAT, hKD⟩)) ?_ + rintro ⟨r6, s3, a3⟩ ⟨-, σ3, G3, Ac3, Lst3, D3, hdelVF, J3, hnull3, hslot3, hks3⟩ + fbj_red + -- the spec's cap at the destination after the insert, for `FbKeep` across the delete + obtain ⟨c9, a9, hc9, hca9, hg9⟩ := (J1.1.1.xtop.rel.2.1.1).slot_cap (Z := Unit) (sl := (pd.bv, idxd)) (p := pd) + rfl hpd + rw [hc1] at hc9 + cases eq_of_ok_eq hc9 + obtain ⟨-, hcap9⟩ := SInvU.getCap_capAt (show (a9, σ1) ∈ (Fl4Spec.getCap (Z := Unit) (pd.bv, idxd) σ1).1 by + rw [hg9]; exact Set.mem_singleton _) + rw [hca9.abs] at hcap9 + have hdel : (.ok (), σ3) ∈ ((Fl4Spec.capDelete (Z := Unit) (pd.bv, idxd)).run σ1).1 := by + rw [← capDeleteVF_frame_eq_native hcap9 (fun h => by cases h)]; exact hdelVF + obtain ⟨hKd, hum3⟩ := fbKeep_capDelete_frame hcap9 hdel + have hrel' : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D + s3 a3 σ3 := ⟨G3, E, Ac3, Lst3, D3, J3⟩ + have hFF := FbFootK.of_relB arI J.1.1 J3.1.1 rfl rfl (hK1.trans hKd) (hum3.trans hum1) + have hCK3 : CnSlotsKept (L := L) Z.th s1 s3 := fbCnKeptOfDel (s := s1) hc1 (fun _ _ _ _ h => by cases h) hslot3 + have hZ3 := hZ1.ofSlot hks3 (fun _ _ _ h => by rw [hnull3] at h; cases h) hslot3 + refine (WP.spec_ok _).2 ⟨σ3, FbMidVE.step C hct hbm mid hrel' (hK1.trans hKd) hFF (hcn1.trans hCK3) hZ3, + Or.inr ⟨e5, rfl, rfl, ?_⟩⟩ + have hm3 : (.error e', σ1) ∈ (ExceptT.run (Fl4Spec.fbMap (Z := Unit) b (pd.bv, idxd) w.2.2) σ1).1 := by + rw [← hw2, h3]; exact Set.mem_singleton _ + have hmem := fbEntry_copy_mapErr (w := w) hbop hdm hem hsm hgsm hp hdvm hcim hm3 hdel + cases e5 with + | Sys e6 => + have h2' : ErrRel (.Sys e6) e' ∨ (e6 = .AlignmentError ∧ e' = .alignmentError) := by + rcases h2 with h | ⟨he6, rfl⟩ + · exact Or.inl h + · cases he6; exact Or.inr ⟨rfl, rfl⟩ + exact WP.spec_mono (fb_code h2') fun c hc => by rw [hEq, hc]; exact hmem + | _ => rcases h2 with (h | ⟨n, h, -⟩) | ⟨h, -⟩ <;> cases h + · simp only at h1 + subst h1 + fbj_red + simp only [core.result.Result.is_err, bind_ok, Bool.false_eq_true, ↓reduceIte] + have hrel' : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D + s2 a2' τ2 := ⟨G1, E, Ac1, Lst1, D0, J2⟩ + have hFF : FbFootK (L := L) Z s s2 := + ⟨fun t ht g => (hF2.1 t ht g).trans + ((FbFootK.of_relB arI J.1.1 J1.1.1 rfl rfl hK1 hum1).1 t ht g), + fun wp hw => (hF2.2 wp hw).trans ((FbFootK.of_relB arI J.1.1 J1.1.1 rfl rfl hK1 hum1).2 wp hw)⟩ + refine (WP.spec_ok _).2 ⟨τ2, FbMidVE.step C hct hbm mid hrel' (hK1.trans hK2) hFF (hcn1.trans hcn2) hZ2, + Or.inl ⟨rfl, false, ?_, by simp⟩⟩ + rw [hEq] + exact fbEntry_copy_ok hbop hdm hem hsm hgsm hp hdvm hcim (by rw [← hw2]; exact hmem2) + +/-! ## The batch's tail and the selected mapped PML4 batch -/ + +/-- The native batch's deferred invalidation updates the matched machine +state and preserves both guest overlays. -/ +theorem VmEptState.fbFlushV (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hA : ArchRefines arI AR) (vs : Fl4Spec.ObjRef) (asid : U64) : + arI.invalidate_asid a asid ⦃ a' => ∃ σ', ((), σ') ∈ (Fl4Spec.flushAll (Z := Unit) vs asid.bv σ).1 ∧ + FbKeep σ σ' ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a' σ' ⦄ := by + have B := J.1.1 + refine WP.spec_mono (hA.invAsid a _ asid B.xtop.fin.1.1.2.2.2.2.2.2) ?_ + intro a' hAR + obtain ⟨-, ⟨y, hy⟩, h3⟩ := flushAll_ok vs asid.bv σ + obtain ⟨ms, e, hum⟩ := h3 _ hy + obtain ⟨u, σ'⟩ := y + cases u + simp only at e + subst e + have B' := B.arFrS (σ' := { σ with machineState := ms }) ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + rfl, rfl, rfl⟩ (B.xtop.msAR' ms a' hum hAR) + refine ⟨_, hy, ?_, J.ofB B' (P1.WritesIn.refl s (fun _ => False)) + (fun _ h => h.elim) (fun _ h => h.elim) (fun _ _ => rfl) rfl rfl⟩ + exact ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, + rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + +/-- The actual return-to-Running of the batch's native caller. -/ +private theorem VmEptState.fbRunV (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) + (hr : x.tcbState = .restart ∨ x.tcbState = .running) : + sched.set_thread_state st s ct sched.TS_RUNNING ⦃ s' => ∃ Lst' : U64 → List U64, + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D s' a + (stsAct (updK σ ct.bv (.tcb { x with tcbState := .running })) ct.bv) ⦄ := by + have B := J.1.1 + rcases hr with h | h + · exact WP.spec_mono (B.runJ ht hk h) fun _ ⟨l, B', F⟩ => ⟨l, J.ofNat (V' := Z.vs) B' F⟩ + · refine WP.spec_mono (B.runRJDB ht hk h) ?_ + rintro s' ⟨l, B', -, -, hW⟩ + have F := NatFoot.ofT B.xtop hW (fun _ h => B.tfoot_nh h) + (natK_tcbW (y := { x with tcbState := .running }) hk + (Notif.stsAct_kheap (updK σ ct.bv (.tcb { x with tcbState := .running })) ct.bv) fun e => by cases e) + exact ⟨l, J.ofNat (V' := Z.vs) B' F⟩ + +set_option maxHeartbeats 4000000 in +/-- Match the in-decoder reply words and `TS_RUNNING` after the native +batch loop. `hr` records the reached caller's state. -/ +theorem VmEptState.fbTailV [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) + (hr : x.tcbState = .restart ∨ x.tcbState = .running) (call : Bool) (d e : U64) : + Vs.fbTail (st := st) s ct call d e ⦃ s' => + ∃ z ∈ (specTail call ct.bv [d.bv, e.bv] + (stsAct (updK σ ct.bv (.tcb { x with tcbState := .restart })) ct.bv)).1, + L.mem s' (P1.stA ct) ≠ sched.TS_RESTART ∧ RelTopM (L := L) s' z.2 ∧ + VEAnyX (L := L) arI AR s' a z.2 ⦄ := by + have hpost := Vs.specTail_fb σ ct hk call d e + have tail : Vs.fbTail (st := st) s ct call d e ⦃ s' => ∃ Lst' : U64 → List U64, + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D s' a (stsAct (updK σ ct.bv + (.tcb { (if call then Endp.replyTcb2 x d.bv e.bv else x) with tcbState := .running })) ct.bv) ⦄ := by + unfold Vs.fbTail + cases call + · simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + exact J.fbRunV ht hk hr + · simp only [↓reduceIte] + refine WP.spec_bind (Vs.fbTail_words s ct d e) ?_ + rintro s5 ⟨w, rfl, hw⟩ + have B := J.1.1 + have hXb := badge_wr_irel B.ip (B.xtop.irelK B.ks B.buf) ht hk 0#u64 + rw [badgeRegA_eq_regA] at hXb + obtain ⟨B1, F1⟩ := B.regWrJFixed ht hk Fl4Spec.badge_register 0#u64 hXb + let x1 := setReg x Fl4Spec.badge_register (0#u64 : U64).bv + have hk1 : (updK σ ct.bv (.tcb x1)).kheap ct.bv = some (.tcb x1) := by simp [updK] + have hX2 := reg_wr_irel B1.ip (B1.xtop.irelK B1.ks B1.buf) ht hk1 (Endp.mrIdx 0) (by decide) d + obtain ⟨B2, F2⟩ := B1.regWrJFixed ht hk1 (Endp.mrIdx 0) d hX2 + let x2 := setReg x1 (Endp.mrIdx 0) d.bv + have hk2 : (updK (updK σ ct.bv (.tcb x1)) ct.bv (.tcb x2)).kheap ct.bv = some (.tcb x2) := by simp [updK] + have hX3 := reg_wr_irel B2.ip (B2.xtop.irelK B2.ks B2.buf) ht hk2 (Endp.mrIdx 1) (by decide) e + obtain ⟨B3, F3⟩ := B2.regWrJFixed ht hk2 (Endp.mrIdx 1) e hX3 + let x3 := setReg x2 (Endp.mrIdx 1) e.bv + have hk3 : (updK (updK (updK σ ct.bv (.tcb x1)) ct.bv (.tcb x2)) ct.bv (.tcb x3)).kheap ct.bv = + some (.tcb x3) := by simp [updK] + have hX4 := reg_wr_irel B3.ip (B3.xtop.irelK B3.ks B3.buf) ht hk3 Fl4Spec.msg_info_register (by decide) w + obtain ⟨B4, F4⟩ := B3.regWrJFixed ht hk3 Fl4Spec.msg_info_register w hX4 + have hxC : setReg x3 Fl4Spec.msg_info_register w.bv = Endp.replyTcb2 x d.bv e.bv := by rw [hw]; rfl + have F := B.natTrans F1 (B1.natTrans F2 (B2.natTrans F3 F4)) + rw [updK_updK, updK_updK, updK_updK, hxC] at B4 F + have J4 := J.ofNat (V' := Z.vs) B4 F + have hkC : (updK σ ct.bv (.tcb (Endp.replyTcb2 x d.bv e.bv))).kheap ct.bv = + some (.tcb (Endp.replyTcb2 x d.bv e.bv)) := by simp [updK] + refine WP.spec_mono (J4.fbRunV ht hkC hr) ?_ + rintro s6 ⟨l, J6⟩ + rw [updK_updK] at J6 + exact ⟨l, J6⟩ + refine WP.spec_mono tail ?_ + rintro s' ⟨l, J'⟩ + have B' := J'.1.1 + have hk' : (stsAct (updK σ ct.bv + (.tcb { (if call then Endp.replyTcb2 x d.bv e.bv else x) with tcbState := .running })) ct.bv).kheap ct.bv = + some (.tcb { (if call then Endp.replyTcb2 x d.bv e.bv else x) with tcbState := .running }) := by + simp [stsAct_kheap, updK] + exact ⟨_, hpost, B'.notRestart ht hk' (fun h => by cases h), B'.xtop.relTop, J'.anyX arI⟩ + +/-- The code after the batch loop: the optional invalidation, the reply words and `TS_RUNNING`. -/ +def FbTailK (st : machine.Store S) (arI : machine.Arch A) (ct asid : U64) (call : Bool) + (K : S × A × U64 × U64 × Bool → Result (core.result.Result Unit error.Exc × S × A)) : Prop := + ∀ s1 a1 done1 err deferred, K (s1, a1, done1, err, deferred) = (do + let a2 ← if deferred then arI.invalidate_asid a1 asid else ok a1 + let s2 ← + if call + then + do + let rbuf ← msg.lookup_ipc_buffer st s1 true ct + let s3 ← obj.Tcb.set_reg st ct s1 layout.reg.BADGE 0#u64 + let (_, s4) ← msg.set_mr st s3 ct rbuf 0#u64 done1 + let (len, s5) ← msg.set_mr st s4 ct rbuf 1#u64 err + let mi ← msginfo.MessageInfo.new 0#u64 0#u64 0#u64 len + msg.set_msg_info st s5 ct mi + else ok s1 + let s3 ← sched.set_thread_state st s2 ct sched.TS_RUNNING + ok (core.result.Result.Ok (), s3, a2)) + +set_option maxHeartbeats 4000000 in +/-- After the matched loop, flush the native ASID if requested and finish +the in-decoder reply at the paired VM state. -/ +theorem VmEptState.fbFinish + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hA : ArchRefines arI AR) {σ0 : Fl4Spec.State Unit} {s0 : S} {ct asid : U64} (ht : ct ∈ Z.T) + (hcur : σ0.curThread = ct.bv) {x : Fl4Spec.Tcb} (hk : σ0.kheap ct.bv = some (.tcb x)) + (hr : x.tcbState = .running) (b : Fl4Spec.Fl4FrameBatchInvocation) (hAsid : b.asid = asid.bv) + (d e : U64) (deferred : Bool) (hKeep : FbKeep σ0 σ) + (hloop : ((d.val, e.bv, deferred), σ) ∈ (Fl4Spec.fbRunWith Fl4Spec.capDeleteVF b b.entries 0 false σ0).1) + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} + (hDec : DecOut m σ0 (.base (.invokeArchObject (.frameBatch b)))) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) + {K : S × A × U64 × U64 × Bool → Result (core.result.Result Unit error.Exc × S × A)} + (hK : FbTailK st arI ct asid call K) : + K (s, a, d, e, deferred) ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ0 blk call don m ct s0 res ⦄ := by + rw [hK] + have hflush : (if deferred then arI.invalidate_asid a asid else ok a) ⦃ a2 => ∃ τ, + ((), τ) ∈ (Fl4Spec.NonDet.when (deferred = true) (Fl4Spec.flushAll (Z := Unit) b.vspace b.asid) σ).1 ∧ + FbKeep σ τ ∧ VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a2 τ ⦄ := by + cases deferred + · exact (WP.spec_ok _).2 ⟨σ, rfl, FbKeep.refl σ, J⟩ + · refine WP.spec_mono (J.fbFlushV hA b.vspace asid) ?_ + rintro a2 ⟨τ, hτ, hK', J2⟩ + exact ⟨τ, by simpa only [Fl4Spec.NonDet.when, hAsid, ↓reduceIte] using hτ, hK', J2⟩ + refine WP.spec_bind hflush ?_ + rintro a2 ⟨τ, hflush, hK', J2⟩ + have keep := hKeep.trans hK' + have hd : BitVec.ofNat 64 d.val = d.bv := by + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt (by scalar_tac) + have hp : ([d.bv, e.bv], τ) ∈ (Fl4Spec.performFrameBatchVF b σ0).1 := by + rw [← hd] + unfold Fl4Spec.performFrameBatchVF Fl4Spec.performFrameBatchWith + exact ⟨_, hloop, _, hflush, rfl⟩ + obtain ⟨y, hy, -, hp'⟩ := SInv.performFrameBatchVF_restart b hk hp keep + obtain ⟨y', hy', hsame⟩ := keep.tcb hk + have eY : y = y' := Fl4Spec.KernelObject.tcb.inj (Option.some.inj (hy.symm.trans hy')) + subst y' + have hry : y.tcbState = .running := hsame.1.trans hr + refine fb_assoc_spec (WP.spec_bind (J2.fbTailV ht hy (Or.inr hry) call d e) ?_) + rintro s2 ⟨z, hz, hnr, hRt, hVE⟩ + have hst : Fl4Spec.setThreadState (Z := Unit) σ0.curThread .restart σ0 = + ({((), stsAct (updK σ0 ct.bv (.tcb { x with tcbState := .restart })) ct.bv)}, False) := by + rw [hcur] + exact Notif.setThreadState_eq .restart hk + have hpm := mem_liftE (ε := Unit) (x := (_, _)) hp' + exact (WP.spec_ok _).2 (Or.inr ⟨rfl, hnr, _, hDec, _, hst, _, _, hpm, z, hz, hRt, hVE⟩) + +set_option maxHeartbeats 8000000 in +/-- **The mapped PML4 batch's loop and tail at `VmEptState`** (module docstring): `fb_loopWith` at +`FbMidVE`, each entry by its op (`FbMidVE.copyEntry`, `remapEntry`, `deleteEntry`), then +`VmEptState.fbFinish`. -/ +theorem VmEptState.fbLoopV [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (p asid buf : U64) (args : List U64) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (b : Fl4Spec.Fl4FrameBatchInvocation) + (n words op depth mr cr attr : U64) (dd : Bool) (cnode dcnode : capability.Cap) + (hw : words.val = b.op.words) (hn255 : n.val ≤ 255) + (hbe : b.entries = Fl4Spec.fbEntries (args.map UScalar.bv) b.op.words n.val) + (hln : 1 + words.val * n.val ≤ args.length) + (C : FbCtx σ Z.th args { root := p, asid := asid, op := op, depth := depth, map_rights := mr, cap_rights := cr, attr := attr, defer_delete := dd, cnode := cnode, dest_cnode := dcnode } b) + (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (hRt0 : (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = cnode) ∧ (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = dcnode)) + (hZ0 : ZNoBlk Z.th (L.mem s) (L.ksb s)) + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} (hDec : DecOut m σ (.base (.invokeArchObject (.frameBatch b)))) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) + {K : S × A × U64 × U64 × Bool → Result (core.result.Result Unit error.Exc × S × A)} + (hK : FbTailK st arI ct asid call K) : + (vspace.decode_frame_batch_loop st arI s a buf ct n words p asid op depth mr cr attr dd cnode dcnode + 0#u64 false >>= K) ⦃ res => VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don m ct s res ⦄ := by + obtain ⟨hcur, x, hk, -⟩ := J.1.1.curRunnable hni hct + have hrx : x.tcbState = .running := hRun x hk + let bt : vspace.Batch := { root := p, asid := asid, op := op, depth := depth, map_rights := mr, cap_rights := cr, attr := attr, defer_delete := dd, cnode := cnode, dest_cnode := dcnode } + have hctT : ct ∈ Z.T := J.1.1.thT ct (J.1.1.xtop.curTcb hcur).1 + have hInit : FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s a σ := + ⟨⟨Z.G, Z.vs, Ac, Lst, D, J⟩, hargs, FbKeep.refl σ, hRt0, hZ0⟩ + refine WP.spec_bind (fb_loopWith arI Fl4Spec.capDeleteVF (FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt) + s a σ ct buf n words args bt b hInit hw hn255 hbe hln (fun _ _ _ h => h.words) ?_) ?_ + · intro x' a' τ h w w0 w1 w2 hw0 hw1 hw2 df + by_cases h1 : bt.op = vspace.frame_batch.COPY_MAP + · exact FbMidVE.copyEntry C hctT hbm h hRef hAT hKD h1 w w0 w1 w2 hw0 hw1 hw2 df + by_cases h2 : bt.op = vspace.frame_batch.REMAP + · exact FbMidVE.remapEntry C hctT hbm h hRef h1 h2 w w0 w1 w2 hw0 hw1 df + · exact FbMidVE.deleteEntry C hctT hbm h hRef hAT hKD h1 h2 w w0 w1 w2 hw0 df + rintro ⟨s1, a1, d1, e1, df⟩ ⟨τ, hrun1, hM⟩ + obtain ⟨G1, E1, Ac1, Lst1, D1, J1⟩ := hM.rel + exact J1.fbFinish hRef hctT hcur hk hrx b C.hba d1 e1 df hM.keep hrun1 hDec on rc blk call don hK + +/-- The actual message length is at most `120` words. -/ +theorem msgLen_le120 {ct : U64} {info : msginfo.MessageInfo} (hinfo : msg.msg_info st s ct = ok info) + (buf : U64) : (msgLen info buf).val ≤ 120 := by + have hm := Endp.msg_info_spec (L := L) s ct + rw [hinfo, WP.spec_ok] at hm + have hle := hm.2.2.2 + have h4 : msg.N_MSG_REGISTERS.val = 4 := by simp [msg.N_MSG_REGISTERS] + unfold msgLen + split_ifs <;> omega + +set_option maxHeartbeats 16000000 in +/-- **FrameBatch on the selected mapped PML4 at `VmEptState`** (module docstring; +`decode_invocation_pml4_fbJVM`'s checks with the VM batch loop and tail): every decode failure +leaves the state unchanged with the spec's error; a decoded batch runs `VmEptState.fbLoopV`. +[J] `SpecGlob` and the running caller at the reached dispatch. -/ +theorem VmEptState.selectedFrameBatchMappedS3 [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) (hsg : SpecGlob σ) + {ct cptr len buf ptr asid : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Pml4 ptr true asid) ex args exs) + (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) (hRun : ZvCtRun σ ct) (hlen120 : len.val ≤ 120) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PML4FrameBatch len (cslotAddr sl) + (.Pml4 ptr true asid) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PML4FrameBatch.bv args (Fl4Spec.toBl cptr.bv) sl + (.Pml4 ptr true asid) exs buffer) s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp fl4_abi.label.X86PML4FrameBatch.bv args (Fl4Spec.toBl cptr.bv) sl + (.Pml4 ptr true asid) exs buffer) ct s res ⦄ := by + let lbl := fl4_abi.label.X86PML4FrameBatch + let argsU : List U64 := args.map fun x => (⟨x⟩ : U64) + have hmap : argsU.map UScalar.bv = args := zv_map_mk args + have hct := q.current + have hlen : len.val = argsU.length := q.length + have hargs : ∀ (i : U64) x, argsU[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x := q.words + have hl : argsU.length ≤ 120 := by rw [← hlen]; exact hlen120 + have B := J.1.1 + have hR := B.xtop.fin.2.1 + have hPl : Placed Z.th := B.xtop.rel.1 + have hAs := q.absSlot + have hok := (B.vsc.caps B.xtop.rel.2.1.2.2.1).at B.xtop.rel.2.1.2.2.1 hAs.isSlot q.capAt (by simp) + have h0' : asid ≠ 0#u64 := (hok.2.2 rfl).1 + have h20 : asid.val < 2 ^ 20 := (hok.2.2 rfl).2 + have hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp := by + intro hb0 + have hctH : Z.th ct = some .tcb := (B.xtop.curTcb (B.curEq hct).1).1 + have hctT : ct ∈ Z.T := B.thT ct hctH + obtain ⟨x, hk, hwx⟩ := B.buf ct hctT + have hL := lookup_buf_full s σ B.xtop.rel.2.1.1 B.xtop.rel.2.1.2.1 hctH hk (JAtZB.xferH B).1 false + rw [hbuf, WP.spec_ok] at hL + cases hq : Endp.ipcBufOf false x with + | none => exact absurd (hL.1.2 hq) hb0 + | some q' => exact ⟨q', (JAtZB.xferH B).2.1 ct hctT x false q' hk hq buf (hL.2 hwx q' hq)⟩ + have hFlush : lbl ≠ fl4_abi.label.X86PML4Flush := by + dsimp only [lbl] + unfold fl4_abi.label.X86PML4FrameBatch fl4_abi.label.X86PML4Flush + decide + have hsFlush : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4Pml4FlushLabel := + fun h => hFlush ((pml4_label lbl).1 h) + have hsBatch : Fl4Spec.dataToNat lbl.bv = Fl4Spec.fl4FrameBatchLabel := (pml4fb_label lbl).2 rfl + have hAbs : specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.frameBatch + (Fl4Spec.decodeFrameBatch (Z := Unit) (argsU.map UScalar.bv) (.pml4Cap ptr.bv (some asid.bv)) exs))) := by + rw [hmap] + show Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.fl4ArchDecodeInvocation (Z := Unit) lbl.bv args (Fl4Spec.toBl cptr.bv) sl + (.pml4Cap ptr.bv (some asid.bv)) exs)) = _ + simp only [Fl4Spec.fl4ArchDecodeInvocation, if_neg hsFlush, if_pos hsBatch] + have fail (err : error.Exc) (e : Fl4Spec.SyscallError) (hRel : ErrRel err e) + (hd : ExceptT.run (Fl4Spec.decodeFrameBatch (Z := Unit) (argsU.map UScalar.bv) + (.pml4Cap ptr.bv (some asid.bv)) exs) σ = ({(.error e, σ)}, False)) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) s (.Err err, s, a) ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) ct s + (.Err err, s, a) := by + rw [hAbs] + exact Or.inl (Or.inl ⟨err, rfl, rfl, ⟨e, run_liftME_err (run_liftME_err (run_liftME_err hd)), hRel⟩, J.anyX arI⟩) + change syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Pml4 ptr true asid) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) ct s res ⦄ + unfold syscall.decode_invocation + rw [hct]; simp only [bind_ok] + unfold vspace.decode + rw [hct]; simp only [bind_ok] + rw [if_neg hFlush, if_pos (show lbl = fl4_abi.label.X86PML4FrameBatch from rfl)] + unfold vspace.decode_frame_batch + rw [hct]; simp only [bind_ok, ↓reduceIte] + refine WP.spec_bind (check_vspace_fb s σ hR ptr asid h0') ?_ + rintro r (⟨rfl, hf⟩ | ⟨e, rfl, hdec⟩) + swap + · rw [fb_mapErr_err] + simp only [bind_tc_ok, bind_ok, fb_branch_err, fb_fromResidual, WP.spec_ok] + exact fail _ _ (Or.inr ⟨0#u64, rfl, rfl⟩) (hdec _ _) + rw [fb_mapErr_ok] + simp only [bind_tc_ok, bind_ok, fb_branch_ok] + try simp only [WP.uncurry'_pair] + try simp only [uncurry_apply_pair] + try simp only [Aeneas.Std.uncurry] + try dsimp only + have hfd := decodeFrameBatch_run (args := argsU.map UScalar.bv) (ex := exs) hf + -- no header + by_cases hl1 : len < 1#u64 + · simp only [hl1, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + have h1 : (argsU.map UScalar.bv).length < 1 := by rw [List.length_map, ← hlen]; scalar_tac + exact fail _ .truncatedMessage (Or.inl trivial) (by rw [hfd, fbDecP_short h1]) + simp only [hl1, ↓reduceIte] + have h1 : ¬ (argsU.map UScalar.bv).length < 1 := by rw [List.length_map, ← hlen]; scalar_tac + obtain ⟨a0, ha0⟩ : ∃ a0, argsU[0]? = some a0 := by + cases h : argsU with + | nil => rw [h] at h1; simp at h1 + | cons a r => exact ⟨a, rfl⟩ + have hhd : Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? = a0.bv := by + rw [List.getElem?_map, ha0]; rfl + rw [hargs 0#u64 a0 ha0] + simp only [bind_ok, lift] + rw [show vspace.frame_batch.COUNT_SHIFT = 8#u64 from (by unfold vspace.frame_batch.COUNT_SHIFT; rfl), + show a0 >>> (8#u64 : U64) = ok ⟨a0.bv >>> 8⟩ from shr_u64_eq a0 8#u64 (by decide)] + simp only [bind_ok, lift] + rw [show vspace.frame_batch.DEPTH_SHIFT = 16#u64 from (by unfold vspace.frame_batch.DEPTH_SHIFT; rfl), + show a0 >>> (16#u64 : U64) = ok ⟨a0.bv >>> 16⟩ from shr_u64_eq a0 16#u64 (by decide)] + simp only [bind_ok, lift] + refine WP.spec_bind (fb_words a0) ?_ + rintro words hw + refine WP.spec_bind P0.Consts.vspace.frame_batch.DEFINED.spec ?_ + rintro i2 hi2 + try simp only [bind_ok, lift] + have hdep : Fl4Spec.dataToNat ((a0.bv >>> 16) &&& 0x7f#64) = + ((⟨a0.bv >>> 16⟩ : U64) &&& 127#u64).val := fb_and_val _ _ + have hnn : Fl4Spec.dataToNat ((a0.bv >>> 8) &&& 0xff#64) = ((⟨a0.bv >>> 8⟩ : U64) &&& 255#u64).val := + fb_and_val _ _ + have hIA : ∀ (hh : Fl4Spec.fbOpOf (Fl4Spec.dataToNat (a0.bv &&& 0x3#64)) = none ∨ + (a0.bv &&& ~~~Fl4Spec.fbDefined ≠ 0 ∨ Fl4Spec.dataToNat ((a0.bv >>> 16) &&& 0x7f#64) = 0 ∨ + Fl4Spec.dataToNat ((a0.bv >>> 16) &&& 0x7f#64) > 64)), + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) s + (.Err (.Sys (.InvalidArgument 0#u64)), s, a) ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) ct s + (.Err (.Sys (.InvalidArgument 0#u64)), s, a) := fun hh => + fail _ (.invalidArgument 0) (Or.inl rfl) (by rw [hfd, fbDecP_hdr h1 (by rw [hhd]; exact hh)]) + -- reserved bits + by_cases hd1 : ((a0 &&& ~~~i2) != 0#u64) = true + · simp only [hd1, ↓reduceIte, error.invalid_arg, bind_ok, WP.spec_ok] + exact hIA (Or.inr (Or.inl ((fb_defined' a0 i2 hi2).1 hd1))) + simp only [hd1, Bool.false_eq_true, ↓reduceIte] + rcases hw with ⟨rfl, hnone⟩ | ⟨op', hop', hwv, hw0, hcpy⟩ + · simp only [eq_self_iff_true, ↓reduceIte, error.invalid_arg, bind_ok, WP.spec_ok] + exact hIA (Or.inl hnone) + simp only [hw0, ↓reduceIte] + by_cases hd2 : (⟨a0.bv >>> 16⟩ : U64) &&& 127#u64 = 0#u64 + · simp only [hd2, ↓reduceIte, error.invalid_arg, bind_ok, WP.spec_ok] + exact hIA (Or.inr (Or.inr (Or.inl (by rw [hdep, hd2]; rfl)))) + simp only [hd2, ↓reduceIte] + by_cases hd3 : (⟨a0.bv >>> 16⟩ : U64) &&& 127#u64 > 64#u64 + · simp only [hd3, ↓reduceIte, error.invalid_arg, bind_ok, WP.spec_ok] + exact hIA (Or.inr (Or.inr (Or.inr (by rw [hdep]; scalar_tac)))) + simp only [hd3, ↓reduceIte] + have h2 : ¬ (Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? &&& ~~~Fl4Spec.fbDefined ≠ 0 ∨ + Fl4Spec.dataToNat ((Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? >>> 16) &&& 0x7f#64) = 0 ∨ + Fl4Spec.dataToNat ((Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? >>> 16) &&& 0x7f#64) > 64) := by + have hd1' : ¬ a0.bv &&& ~~~Fl4Spec.fbDefined ≠ 0 := fun h => hd1 ((fb_defined' a0 i2 hi2).2 h) + rw [hhd, hdep] + have : ((⟨a0.bv >>> 16⟩ : U64) &&& 127#u64).val ≠ 0 := fun h => hd2 (UScalar.eq_of_val_eq (by simpa using h)) + have : ¬ ((⟨a0.bv >>> 16⟩ : U64) &&& 127#u64).val > 64 := fun h => hd3 (by scalar_tac) + tauto + have hQ := fbDecP_some (p := ptr.bv) (asid := asid.bv) (ex := exs) h1 (by rw [hhd]; exact hop') h2 + -- the entries' words + have hwn : words.val * ((⟨a0.bv >>> 8⟩ : U64) &&& 255#u64).val ≤ 3 * 255 := by + have : ((⟨a0.bv >>> 8⟩ : U64) &&& 255#u64).val ≤ 255 := by + rw [UScalar.val_and]; exact Nat.le_trans Nat.and_le_right (by decide) + have : words.val ≤ 3 := by rw [hwv]; cases op' <;> decide + exact Nat.mul_le_mul (by assumption) (by assumption) + step as ⟨i5, hi5⟩ + step as ⟨i6, hi6⟩ + by_cases hl2 : len < i6 + · simp only [hl2, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + have h3 : (argsU.map UScalar.bv : List Fl4Spec.Data).length < 1 + op'.words * + Fl4Spec.dataToNat ((Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? >>> 8) &&& 0xff#64) := by + rw [List.length_map, hhd, hnn, ← hwv, ← hlen]; scalar_tac + exact fail _ .truncatedMessage (Or.inl trivial) (by rw [hfd, hQ]; unfold fbDecQ; rw [if_pos h3]) + simp only [hl2, ↓reduceIte] + have h3 : ¬ (argsU.map UScalar.bv : List Fl4Spec.Data).length < 1 + op'.words * + Fl4Spec.dataToNat ((Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? >>> 8) &&& 0xff#64) := by + rw [List.length_map, hhd, hnn, ← hwv, ← hlen]; scalar_tac + -- extra cap 0: the lookup CNode + obtain ⟨hXl, hXs, hXz⟩ := q.extra + rw [vsExtra_at ex 0#usize (by decide) _ (exAt_idx0 ex)] + simp only [bind_ok] + by_cases hx0 : exAt ex 0 = 0#u64 + · simp only [hx0, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + have hl0 : exs.length < 1 := by + by_contra hn + have := (srcOK_facts hPl (hXs 0 (by omega))).1 + exact this hx0 + exact fail _ .truncatedMessage (Or.inl trivial) (by rw [hfd, hQ]; unfold fbDecQ; rw [if_neg h3, if_pos hl0]) + simp only [hx0, ↓reduceIte] + have hl0 : ¬ exs.length < 1 := fun h => hx0 (hXz 0 (by omega) (by decide)) + obtain ⟨y0, rest, hy⟩ : ∃ y0 rest, exs = y0 :: rest := by + cases h : exs with + | nil => rw [h] at hl0; simp at hl0 + | cons a r => exact ⟨a, r, rfl⟩ + obtain ⟨-, hIs0, -, cv, hcv, hy1, hcr0, hcvv0, -⟩ := srcOK_facts hPl (hXs 0 (by omega)) + have hthe0 : (Fl4Spec.the exs[0]?).1 = absCap cv := by subst hy; exact hy1 + rw [slot_cap_eq, hcv] + simp only [bind_ok] + cases cv + case CNode cp cr cg cgs => + simp only [bind_ok, ↓reduceIte] + have h5 : Fl4Spec.isCnodeCap (Fl4Spec.the exs[0]?).1 = true := by rw [hthe0]; rfl + -- extra cap 1: the destination CNode (`dest_cnode`) + rw [vsExtra_at ex 1#usize (by decide) _ (exAt_idx1 ex)] + simp only [bind_ok] + refine WP.spec_bind (show _ ⦃ dc => (dc = .CNode cp cr cg cgs ∨ + ∃ x, vspace.extra ex 1#usize = ok (some x) ∧ capAt (L.mem s) x = ok dc) ∧ + (if op' = .copyMap ∧ exs.length > 1 then (Fl4Spec.the exs[1]?).1 + else (Fl4Spec.the exs[0]?).1) = absCap dc ∧ CapValid Z.th dc ∧ CapWf dc ∧ + ∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = dc ⦄ from ?_) ?_ + · by_cases hx1 : exAt ex 1 = 0#u64 + · simp only [hx1, ↓reduceIte, WP.spec_ok] + have hl1 : ¬ exs.length > 1 := fun h => (srcOK_facts hPl (hXs 1 h)).1 hx1 + exact ⟨Or.inl (by first | rfl | trivial), by rw [if_neg (fun h => hl1 h.2)]; exact hthe0, hcvv0, hcr0.1, + _, hIs0, capV_of_capAt hcv⟩ + simp only [hx1, ↓reduceIte] + have hl1 : exs.length > 1 := by + by_contra hn + exact hx1 (hXz 1 (by omega) (by decide)) + have hx1e : vspace.extra ex 1#usize = ok (some (exAt ex 1)) := by + rw [vsExtra_at ex 1#usize (by decide) _ (exAt_idx1 ex), if_neg hx1] + by_cases hcm : op' = .copyMap + · have hcm' : a0 &&& vspace.frame_batch.OP = vspace.frame_batch.COPY_MAP := hcpy.2 hcm + simp only [hcm', ↓reduceIte] + obtain ⟨-, hIs1, -, cv1, hcv1, hy11, hcr1, hcvv1, -⟩ := srcOK_facts hPl (hXs 1 hl1) + rw [slot_cap_eq, hcv1, WP.spec_ok] + refine ⟨Or.inr ⟨_, hx1e, hcv1⟩, ?_, hcvv1, hcr1.1, _, hIs1, capV_of_capAt hcv1⟩ + rw [if_pos ⟨hcm, hl1⟩, List.getElem?_eq_getElem hl1]; exact hy11 + · have hcm' : ¬ a0 &&& vspace.frame_batch.OP = vspace.frame_batch.COPY_MAP := fun h => hcm (hcpy.1 h) + simp only [hcm', ↓reduceIte, WP.spec_ok] + exact ⟨Or.inl (by first | rfl | trivial), by rw [if_neg (fun h => hcm h.1)]; exact hthe0, hcvv0, hcr0.1, + _, hIs0, capV_of_capAt hcv⟩ + rintro dc ⟨-, hdeq, hdcv, hdcw, hdcs⟩ + refine WP.spec_bind (show _ ⦃ b1 => b1 = Fl4Spec.isCnodeCap (absCap dc) ⦄ from ?_) ?_ + · cases dc <;> first | exact rfl | (simp only [WP.spec_ok]; rfl) | (simp only [WP.spec_ok]; decide) | simp [WP.spec_ok, absCap, Fl4Spec.isCnodeCap] + rintro b1 rfl + by_cases h6 : Fl4Spec.isCnodeCap (absCap dc) = true + swap + · simp only [h6, Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + have h6' : ¬ Fl4Spec.isCnodeCap (if op' = .copyMap ∧ exs.length > 1 then (Fl4Spec.the exs[1]?).1 + else (Fl4Spec.the exs[0]?).1) = true := by rw [hdeq]; exact h6 + exact fail _ (.invalidCapability 2) (Or.inr ⟨2#u64, rfl, rfl⟩) + (by rw [hfd, hQ]; unfold fbDecQ; rw [if_neg h3, if_neg hl0, if_pos h5, if_neg h6']) + simp only [h6, ↓reduceIte] + have h6' : Fl4Spec.isCnodeCap (if op' = .copyMap ∧ exs.length > 1 then (Fl4Spec.the exs[1]?).1 + else (Fl4Spec.the exs[0]?).1) = true := by rw [hdeq]; exact h6 + have hb : fbDecP ptr.bv asid.bv (argsU.map UScalar.bv) exs = + .ok (fbDecB ptr.bv asid.bv (argsU.map UScalar.bv) exs op') := + hQ.trans (fbDecQ_ok h3 hl0 h5 h6') + have hDs : ExceptT.run (Fl4Spec.decodeFrameBatch (Z := Unit) (argsU.map UScalar.bv) + (.pml4Cap ptr.bv (some asid.bv)) exs) σ = + ({(.ok (fbDecB ptr.bv asid.bv (argsU.map UScalar.bv) exs op'), σ)}, False) := by rw [hfd, hb] + have hDec : DecOut (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr true asid) exs buffer) σ + (.base (.invokeArchObject (.frameBatch (fbDecB ptr.bv asid.bv (argsU.map UScalar.bv) exs op')))) := by + rw [hAbs]; exact DecOut.of_eq (run_liftME_ok (run_liftME_ok (run_liftME_ok hDs))) + rw [show vspace.frame_batch.MAP_RIGHTS_SHIFT = 24#u64 from (by unfold vspace.frame_batch.MAP_RIGHTS_SHIFT; rfl), + show a0 >>> (24#u64 : U64) = ok ⟨a0.bv >>> 24⟩ from shr_u64_eq a0 24#u64 (by decide)] + simp only [bind_ok, lift] + rw [show vspace.frame_batch.CAP_RIGHTS_SHIFT = 28#u64 from (by unfold vspace.frame_batch.CAP_RIGHTS_SHIFT; rfl), + show a0 >>> (28#u64 : U64) = ok ⟨a0.bv >>> 28⟩ from shr_u64_eq a0 28#u64 (by decide)] + simp only [bind_ok, lift] + rw [show vspace.frame_batch.ATTR_SHIFT = 32#u64 from (by unfold vspace.frame_batch.ATTR_SHIFT; rfl), + show a0 >>> (32#u64 : U64) = ok ⟨a0.bv >>> 32⟩ from shr_u64_eq a0 32#u64 (by decide)] + simp only [bind_ok, lift] + refine WP.spec_bind P0.Consts.vspace.frame_batch.DEFER_DELETE_FLUSH.spec ?_ + rintro i13 hi13 + try simp only [bind_ok, lift] + have hn255 : ((⟨a0.bv >>> 8⟩ : U64) &&& 255#u64).val ≤ 255 := by + rw [UScalar.val_and]; exact Nat.le_trans Nat.and_le_right (by decide) + exact WP.spec_mono (J.fbLoopV hni hRef hAT hKD hct hRun ptr asid buf argsU hargs _ _ words _ _ _ _ _ _ _ _ + hwv hn255 + (by show Fl4Spec.fbEntries _ _ (Fl4Spec.dataToNat ((Fl4Spec.the (argsU.map UScalar.bv : List Fl4Spec.Data)[0]? >>> 8) &&& 0xff#64)) = _ + rw [hhd, hnn] + try rfl) + (by have h3' := h3; rw [List.length_map, hhd, hnn, ← hwv] at h3'; omega) + (fbCtx_mk hni h0' h20 hf hhd hop' hcpy hd2 hd3 hi13 hthe0 hdeq h6 ⟨_, _, _, _, rfl⟩ hcvv0 hcr0.1 hdcv hdcw + (by omega)) hbm ⟨⟨_, hIs0, capV_of_capAt hcv⟩, hdcs⟩ (B.zNoBlk hsg) hDec on rc blk call don + (fun _ _ _ _ _ => rfl)) fun _ h => Or.inr h + all_goals + simp only [Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + have h5 : ¬ Fl4Spec.isCnodeCap (Fl4Spec.the exs[0]?).1 = true := by + rw [hthe0] + intro h + obtain ⟨_, _, _, _, he⟩ := fb_isCN h + cases he + exact fail _ (.invalidCapability 1) (Or.inr ⟨1#u64, rfl, rfl⟩) + (by rw [hfd, hQ]; unfold fbDecQ; rw [if_neg h3, if_neg hl0, if_neg h5]) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3FrameMap.lean b/proofs/Fl4Proofs/P3/VmEntryS3FrameMap.lean new file mode 100644 index 000000000..b348df29b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3FrameMap.lean @@ -0,0 +1,785 @@ +/- +Native Frame PageMap and PageUnmap at a VM entry. The selected slot comes from the invocation lookup; +the native walk's actual write footprint and the guest objects' preservation give the VM relation +after the caller's restart (`VmEptState.nativeSlotLift`). The decode/perform proof is split per +label, per frame size and per outcome so each declaration elaborates within the default-scale budget. +[P] Native VSpace architecture refinement. No existing statement changed. +-/ +import Fl4Proofs.P3.VmEntryS3FrameBatch +import Fl4Proofs.P3.FrontEndLabel +import Fl4Proofs.P3.XTopJointVsMap + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched Fl4Proofs.P1 + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- Lift the exact native frame delta after the caller's restart. -/ +private theorem VmEptState.nativeFramePost [Fl4Proofs.P1.PlainStore st] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d m ep asid va)) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hW0 : P1.WritesIn st s s' (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s)) + (hSF : SlotFoot Z.th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx))) + (hLike : VsLike (capV (L.mem s') (cslotAddr (pc.bv, idx))) (.Frame p sz rt d m ep asid va)) + (hK : KFoot σ σ') (hMap : ∃ iv, MapSpec σ iv σ') + (hLeaf : MapLeafF (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) σ σ' ∨ + UnmapLeafF (L.mem s') (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) σ σ') : + ∃ Lst', VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D s' a' σ' := by + have B := J.1.1 + set x := cslotAddr (pc.bv, idx) with hxdef + obtain ⟨s1, σ1, Lst1, hs1, hst1, J1⟩ := VmEptState.restart (arI := arI) J hni + have B1 := J1.1.1 + have Cv1 := J1.1.2 + have C1 := J1.2 + have hct := P1.cur_thread_ok (L := L) s + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have htcb : Z.th (L.curW s) = some .tcb := (B.xtop.curTcb hcur).1 + have e1 : σ1 = stsAct (updK σ (L.curW s).bv (.tcb { xct with tcbState := .restart })) (L.curW s).bv := by + have hst := hst1 + rw [hcur, Notif.setThreadState_eq .restart hkt] at hst + have := congrArg (fun q => ((), σ1) ∈ q.1) hst + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at this + exact of_eq_true this + have hk1 : ∀ o, o ≠ (L.curW s).bv → σ1.kheap o = σ.kheap o := by + intro o ho + rw [e1, stsAct_kheap] + simp [updK, ho] + have hct1 : σ1.kheap (L.curW s).bv = some (.tcb { xct with tcbState := .restart }) := by + rw [e1, stsAct_kheap] + simp [updK] + have har1 : σ1.archState = σ.archState := by rw [e1]; unfold stsAct; split <;> rfl + have F : VsSFr σ1 σ' := vsSFr_of_post hst1 (Or.inl (hMap.imp fun _ h => Or.inl h)) + have harch : ∀ o ao, VmGuestArch ao → + (σ1.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + intro o ao ho + by_cases e : o = (L.curW s).bv + · rw [e, hct1] + constructor + · intro h; cases h + · intro h + have hk := (hLeaf.elim (fun h => fbNativeMapObject B.xtop hK h hX' _ _ ho) + (fun h => fbNativeUnmapObject hK h.2.2 _ _ ho)).mpr h + rw [hkt] at hk + cases hk + · rw [hk1 _ e] + exact hLeaf.elim (fun h => fbNativeMapObject B.xtop hK h hX' _ _ ho) + (fun h => fbNativeUnmapObject hK h.2.2 _ _ ho) + have hat : σ'.archState.x64AsidTable = σ1.archState.x64AsidTable := by + rw [har1] + exact hLeaf.elim (fun h => h.2.2.1) (fun h => h.2.2.1) + -- 2. FL4's writes after the restart (`JAtZ.vsPostJ`'s pins) + have hP := B.xtop.rel.1 + have hact := B.kern.act + have hF2 : Frame2 (L := L) s s1 (P1.stA (L.curW s)) (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have h := set_thread_state_frame (L := L) s (L.curW s) sched.TS_RESTART (stA_al hP htcb) hact.1 + rw [hs1, WP.spec_ok] at h; exact h + have hSS1 : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := set_thread_state_slots (L := L) Z.th hP s (L.curW s) sched.TS_RESTART htcb hact + rw [hs1, WP.spec_ok] at h; exact h + have hkc1 : L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s := by + have h := set_thread_state_cases (L := L) s (L.curW s) sched.TS_RESTART + rw [hs1, WP.spec_ok] at h + rcases h with rfl | rfl <;> simp + have hks' : L.ksb s' = L.ksb s := hW0.1 + have pinS : ∀ t, Z.th t = some .tcb → L.mem s' (P1.stA t) = L.mem s1 (P1.stA t) := by + intro t ht + have htT := B.thT t ht + obtain ⟨y1, hy1, hR1⟩ := B1.xtop.ipc.tcb t htT + obtain ⟨y, hky, hRy⟩ := hX'.ipc.tcb t htT + obtain ⟨y', hy', hc⟩ := F.tcbF hy1 + rw [hky] at hy'; cases hy' + have h1 : (L.mem s' (tStA t)).val = (L.mem s1 (tStA t)).val := by rw [hRy.st, hR1.st, hc.st] + rw [stA_tStA]; exact UScalar.eq_of_val_eq h1 + have pinA : L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) = + L.mem s1 (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have a1 := B1.xtop.fin.1.1.1.act + have a2 := hX'.fin.1.1.1.act + rw [F.act] at a2 + have e := Sched.ActRel.det a2 a1 + obtain ⟨Q, R, hc, -⟩ := B.xtop.fin.1.1.1.lists + unfold nodeW at e + rw [P1.nodeAt_of_ksb hks', P1.nodeAt_of_ksb hkc1.1, ← hc.cur] at e + exact e + have hSlot : ∀ y, IsSlot Z.th y → L.mem s' (wordOf y 16#u64) = L.mem s1 (wordOf y 16#u64) ∧ + L.mem s' (wordOf y 24#u64) = L.mem s1 (wordOf y 24#u64) ∧ + (y ≠ x → L.mem s' y = L.mem s1 y ∧ L.mem s' (wordOf y 8#u64) = L.mem s1 (wordOf y 8#u64)) := by + intro y hy + obtain ⟨a0, a8, a16, a24⟩ := hSS1 y hy + obtain ⟨b16, b24, bx⟩ := hSF y hy + exact ⟨by rw [b16, a16], by rw [b24, a24], fun hne => ⟨by rw [(bx hne).1, a0], by rw [(bx hne).2, a8]⟩⟩ + have hW : P1.WritesIn st s1 s' (VsXW Z (L.ksb s1) x) := by + refine ⟨hks'.trans hkc1.1.symm, hW0.2.1.trans hkc1.2.symm, fun b hb hn => ?_⟩ + have hslw : ∀ y, IsSlot Z.th y → ∀ k : U64, Off k → b = wordOf y k → L.mem s' b = L.mem s1 b := by + intro y hy k hk e + subst e + obtain ⟨h16, h24, hne⟩ := hSlot y hy + rcases hk with rfl | rfl | rfl | rfl + · by_cases ex : y = x + · exact (hn (Or.inr (Or.inl (by rw [wordOf_zero, ex])))).elim + · rw [wordOf_zero]; exact (hne ex).1 + · by_cases ex : y = x + · exact (hn (Or.inr (Or.inr (by rw [ex])))).elim + · exact (hne ex).2 + · exact h16 + · exact h24 + by_cases hb0 : VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b + · rcases hb0 with ⟨y, hy, e | e⟩ | ⟨y, k, hy, hk, e⟩ | hpt | hvw | ⟨t, ht, e⟩ | e + · exact hslw y hy 0#u64 (Or.inl rfl) (by rw [wordOf_zero]; exact e) + · exact hslw y hy 8#u64 (Or.inr (Or.inl rfl)) e + · exact hslw y hy k hk e + · exact (hn (Or.inl (Or.inl hpt))).elim + · exact (hn (Or.inl (Or.inr (by rw [hkc1.1]; exact hvw)))).elim + · subst e; exact pinS t ht + · subst e; exact pinA + · have e0 : L.mem s' b = L.mem s b := hW0.2.2 b hb hb0 + have e1 : L.mem s1 b = L.mem s b := hF2 b hb (fun e => hb0 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl + ⟨L.curW s, htcb, UScalar.eq_of_val_eq e⟩)))))) (fun e => hb0 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + (UScalar.eq_of_val_eq e))))))) + rw [e0, e1] + have hcv0 : capV (L.mem s) x = .Frame p sz rt d m ep asid va := capV_of_capAt hc + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hxs : IsSlot Z.th x := hA.isSlot + have hc1 : capAt (L.mem s1) x = ok (.Frame p sz rt d m ep asid va) := by + rw [capAt_capV, hSS1.capV hxs, hcv0] + have hvn : Z.vx = ∅ → VsNamed Z.th (L.mem s') (L.ksb s') := fun hvx => by + have hR' : VsRel Z.P (L.mem s') (L.ksb s) σ' := by rw [← hks']; exact hX'.fin.2.1 + rw [hks'] + exact hLeaf.elim + (fun h => (B.carry.vsNamed hvx).ofMapLeafF B.vsc B.xtop.fin.2.1 B.xtop.fin.2.2.1 + B.xtop.fin.2.2.2.1 hR' hX'.fin.2.2.1 hcv0 hSF h) + (fun h => (B.carry.vsNamed hvx).ofUnmapLeafF B.vsc B.xtop.fin.2.1 B.xtop.fin.2.2.1 + B.xtop.fin.2.2.2.1 hR' hX'.fin.2.2.1 hcv0 hSF h) + have hpostF : IsFrameC (capV (L.mem s') x) := hLeaf.elim (fun h => h.2.1) (fun h => h.2.1) + obtain ⟨J', -⟩ := J1.nativeSlotLift hpc hidx hc1 hn hX' F hW (fun y hy => hSlot y hy) hLike hpostF hvn harch hat + exact ⟨Lst1, J'⟩ + +/-- On an unmapped frame the native Map or Unmap does not read the EPT +selection bit. The source slot still holds the actual, selected cap. -/ +theorem nativeFrameUnmappedDecodeEq + (lbl len slot buf p rt asid va : U64) (sz : capability.FrameSize) (d call : Bool) + (ex : Array obj.Slot 3#usize) + (hl : lbl = fl4_abi.label.X86PageMap ∨ lbl = fl4_abi.label.X86PageUnmap) : + vspace.decode_frame st arI s a lbl len slot (.Frame p sz rt d false true asid va) call buf ex = + vspace.decode_frame st arI s a lbl len slot (.Frame p sz rt d false false asid va) call buf ex := by + rcases hl with rfl | rfl + · have hne : fl4_abi.label.X86PageMap ≠ fl4_abi.label.X86PageMapEPT := by + unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageMapEPT; decide + simp only [vspace.decode_frame, vspace.as_frame, bind_ok, hne, ↓reduceIte, + Bool.false_eq_true, Bool.false_and] <;> rfl + · have hne : fl4_abi.label.X86PageUnmap ≠ fl4_abi.label.X86PageMapEPT := by + unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageMapEPT; decide + have hnm : fl4_abi.label.X86PageUnmap ≠ fl4_abi.label.X86PageMap := by + unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageMap; decide + simp only [vspace.decode_frame, vspace.as_frame, bind_ok, hne, hnm, ↓reduceIte, + Bool.false_eq_true, Bool.false_and] <;> rfl + +/-- Presentation of the concrete cap stored in the invoked slot. +The native decoder's `frameCapOf` projects away the EPT selection bit. -/ +private def observedFrame (f : vspace.FrameCap) : capability.Cap := + .Frame f.ptr f.size f.rights f.is_device f.mapped f.is_ept f.asid f.vaddr + +private theorem observedFrame_like (f : vspace.FrameCap) (hn : NativeCap (observedFrame f)) : + VsLike (frameCapOf f) (observedFrame f) := by + obtain ⟨p, sz, rt, d, m, ep, asid, va⟩ := f + cases m <;> cases ep <;> first + | exact False.elim (hn ⟨rfl, rfl⟩) + | exact ⟨⟨rfl, rfl, rfl, rfl⟩, fun _ _ _ _ _ h => h⟩ + +private theorem nativeFrameLike_trans {c c' c'' : capability.Cap} + (h1 : VsLike c c') (h2 : VsLike c' c'') : VsLike c c'' := + ⟨⟨h1.1.1.trans h2.1.1, h1.1.2.1.trans h2.1.2.1, h1.1.2.2.1.trans h2.1.2.2.1, + h1.1.2.2.2.trans h2.1.2.2.2⟩, fun P L4 L3 L2 L1 h => h1.2 P L4 L3 L2 L1 (h2.2 P L4 L3 L2 L1 h)⟩ + +/- Reuse the native unmap for a plain frame. The remaining case has an + unmapped frame whose stored EPT bit is set, so only the cap write runs. -/ +set_option maxHeartbeats 8000000 in +private theorem observed_frame_unmap_rel [Fl4Proofs.P1.PlainStore st] {A : Type} {ar : machine.Arch A} {th : TypedHeap} {P L4 L3 L2 L1 : Set U64} + {SR : S → A → Fl4Spec.State Unit → Prop} (hH : VsFrameUnmapHyps st A ar th P L4 L3 L2 L1 SR) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (hPl : Placed th) (hVP : VsPlaced P (L.ksb s)) + (hD : VsSlotsDisj th P (L.ksb s)) (hPP : PtPlaced L4 L3 L2 L1) + (hPS : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ SlotW th b) (hPV : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ VsWrite P (L.ksb s) b) + (hPI : PoolsIn P L4 (L.mem s)) (hPO : ∀ b, PtWAll L4 L3 L2 L1 b → OutsideObjects th b) + (hSt : StateRel th (L.mem s) σ) (hSV : SlotsValid th (L.mem s)) (hR : VsRel P (L.mem s) (L.ksb s) σ) + (hPt : PtRel L4 L3 L2 L1 (L.mem s) σ) (hS : SR s a σ) + (ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : ct.bv = σ.curThread) + {xct : Fl4Spec.Tcb} (hkt : σ.kheap σ.curThread = some (.tcb xct)) + (hrun : Fl4Spec.runnable xct.tcbState = true) + (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (f : vspace.FrameCap) (hn : NativeCap (observedFrame f)) (hfw : CapWf (observedFrame f)) (hfe : EncWf (observedFrame f)) + (hfm : f.mapped = true → f.asid ≠ 0#u64) (hfu : f.mapped = true → Vs.UserV f.vaddr) + {pc : U64} {r : Nat} (hpc : th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + (hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (observedFrame f)) + {slbl : Fl4Spec.Data} (hl : Fl4Spec.invocationType slbl = .archInvocationLabel .x64PageUnmap) + (length buf : U64) (call : Bool) (ex : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) + (xs : Fl4Spec.CapRef) (vcs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + vspace.decode_frame st ar s a fl4_abi.label.X86PageUnmap length (cslotAddr (pc.bv, idx)) (observedFrame f) call buf ex + ⦃ res => res.1 = .Ok () ∧ ∃ iv σ', + ExceptT.run (Fl4Spec.archDecodeInvocation (Z := Unit) slbl args xs (pc.bv, idx) (frameACapG f) vcs) σ = + ({(.ok iv, σ)}, False) ∧ + MapSpec σ iv σ' ∧ UR st th P L4 L3 L2 L1 SR res.2.1 res.2.2 σ' ∧ + SlotFoot th (L.mem s) (L.mem res.2.1) (cslotAddr (pc.bv, idx)) ∧ + VsLike (capV (L.mem res.2.1) (cslotAddr (pc.bv, idx))) (observedFrame f) ∧ KFoot σ σ' ∧ + UnmapLeafF (L.mem res.2.1) (cslotAddr (pc.bv, idx)) (observedFrame f) σ σ' ⦄ := by + obtain ⟨ptr, sz, rt, dev, mp, ep, fa, fv⟩ := f + cases ep + · exact frame_unmap_rel hH s a σ hPl hVP hD hPP hPS hPV hPI hPO hSt hSV hR hPt hS + ct hct hcur hkt hrun htcb hact ⟨ptr, sz, rt, dev, mp, false, fa, fv⟩ + hfw hfe hfm hfu hpc hidx hcx hl length buf call ex args xs vcs + · have em : mp = false := by + cases mp + · rfl + · exact (hn ⟨rfl, rfl⟩).elim + subst mp + rw [show Fl4Spec.archDecodeInvocation (Z := Unit) slbl args xs (pc.bv, idx) + (frameACapG ⟨ptr, sz, rt, dev, false, true, fa, fv⟩) vcs = + Fl4Spec.decodePageInvocation slbl args (pc.bv, idx) (frameACapG ⟨ptr, sz, rt, dev, false, true, fa, fv⟩) vcs from rfl] + unfold frameACapG + dsimp only + simp only [observedFrame] + rw [decodeUnmap_eq hl, nativeFrameUnmappedDecodeEq fl4_abi.label.X86PageUnmap length + (cslotAddr (pc.bv, idx)) buf ptr rt fa fv sz dev call ex (Or.inr rfl)] + unfold vspace.decode_frame + have hne : ¬ fl4_abi.label.X86PageUnmap = fl4_abi.label.X86PageMap := by + unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageMap; decide + have hneEpt : ¬ fl4_abi.label.X86PageUnmap = fl4_abi.label.X86PageMapEPT := by + unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageMapEPT; decide + simp only [observedFrame, vspace.as_frame, bind_ok, hne, hneEpt, ↓reduceIte] + have hfcv : CapValid th (observedFrame ⟨ptr, sz, rt, dev, false, true, fa, fv⟩) := + hSV.cnode pc r hpc (blVal 0 idx) (hidx ▸ blVal_lt idx) _ (by rw [show slotAddr pc.bv (BitVec.ofNat 64 (blVal 0 idx)) = cslotAddr (pc.bv, idx) from (slotAddr_ofBl pc idx).symm]; exact hcx) + obtain ⟨cs, hkc, hwf, hsl⟩ := hSt.cnode pc r hpc + have hcs : cs idx = some (.archObjectCap (.pageCap dev ptr.bv (vmRightsOf rt) (if false then .vmVSpaceMap else .vmNoMap) + (frameSizeOf sz) (if false then some (fa.bv, fv.bv) else none))) := by + obtain ⟨c, ac, hc, hac, hca⟩ := hsl idx hidx + have : c = .Frame ptr sz rt dev false true fa fv := by + have h2 : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok c := hc + rw [hcx] at h2; exact (by simpa [observedFrame] using h2 : capability.Cap.Frame ptr sz rt dev false true fa fv = c).symm + subst this + rw [hac, ← hca.2] + rfl + rw [hct] + apply ok_bind_spec + have hkt' : σ.kheap ct.bv = some (.tcb xct) := by rw [hcur]; exact hkt + refine WP.spec_bind (WP.spec_and (hH.restart s a σ ct xct hSt hSV hR hPt hS hcur hkt' htcb hact hrun hPI) + (set_thread_state_slots th hPl s ct sched.TS_RESTART htcb hact)) ?_ + rintro s1 ⟨⟨hSt1, hSV1, hR1, hPt1, hS1, hk1, hPI1⟩, hSS1⟩ + set σ1 := stsAct (updK σ ct.bv (.tcb { xct with tcbState := .restart })) ct.bv with hσ1 + have hpct : pc.bv ≠ ct.bv := kheap_ne hkc hkt' (fun _ h1 h2 => by subst h1; cases h2) + have hkc1 : σ1.kheap pc.bv = some (.cnode r cs) := by + rw [hσ1, stsAct_kheap]; simp [updK, hpct, hkc] + have hxs : IsSlotAt th pc (blVal 0 idx) := Or.inl ⟨r, hpc, hidx ▸ blVal_lt idx⟩ + have hx0 : cslotAddr (pc.bv, idx) = slotOf pc (blVal 0 idx) := slotAddr_ofBl pc idx + set nc : Fl4Spec.Cap := .archObjectCap (.pageCap dev ptr.bv (vmRightsOf rt) .vmNoMap (frameSizeOf sz) none) with hnc + have hca : CapRel (.Frame ptr sz rt dev false false 0#u64 0#u64) nc := ⟨hfw, rfl⟩ + have hew : EncWf (.Frame ptr sz rt dev false false 0#u64 0#u64) := ⟨hfe.1, hfe.2.1, by decide, by decide⟩ + have hxsl : IsSlot th (cslotAddr (pc.bv, idx)) := + (show AbsSlot th (pc.bv, idx) from ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩).isSlot + -- the final `set_cap`, from a state `s4`/`σ4` with the CNode unchanged + have fin_cap : ∀ (s4 : S) (a4 : A) (σ4 : Fl4Spec.State Unit), UR st th P L4 L3 L2 L1 SR s4 a4 σ4 → + σ4.kheap pc.bv = some (.cnode r cs) → L.ksb s4 = L.ksb s → + obj.Slot.set_cap st (cslotAddr (pc.bv, idx)) s4 (.Frame ptr sz rt dev false false 0#u64 0#u64) ⦃ s5 => + UR st th P L4 L3 L2 L1 SR s5 a4 (setCnodeSlot σ4 pc.bv r cs idx nc) ∧ + SlotFoot th (L.mem s4) (L.mem s5) (cslotAddr (pc.bv, idx)) ∧ + capV (L.mem s5) (cslotAddr (pc.bv, idx)) = .Frame ptr sz rt dev false false 0#u64 0#u64 ⦄ := by + rintro s4 a4 σ4 ⟨hS4, hSt4, hSV4, hR4, hPt4⟩ hkc4 hks4 + refine WP.spec_mono (WP.spec_and (set_cap_refines th s4 σ4 hSt4 hSV4 hPl hpc hidx _ _ hca (by exact hfcv) hew) + (WP.spec_and (set_cap_spec (L := L) _ s4 _) (set_cap_foot hPl hxsl s4 _ hew))) ?_ + rintro s5 ⟨⟨σ5, hsc, hSt5, hSV5, -, -, -, hfr5⟩, hs5, hF5, hcv5⟩ + have hσ5 : σ5 = setCnodeSlot σ4 pc.bv r cs idx nc := by + rw [setCap_cnode nc hkc4 hwf hidx] at hsc + have := congrArg Prod.fst hsc + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at this + exact this.symm + subst hσ5 + have hks5 : L.ksb s5 = L.ksb s4 := by rw [hs5]; simp + have hfrV : ∀ b, VsWrite P (L.ksb s) b → L.mem s5 b = L.mem s4 b := by + intro b hb + obtain ⟨h1, h2⟩ := ne_slot_words hPl hxs (hD b hb) + exact hfr5 b (hb.aligned hVP) (by rw [hx0]; exact h1) (by rw [hx0]; exact h2) + have hpools : ∀ q ∈ P, (setCnodeSlot σ4 pc.bv r cs idx nc).kheap q.bv = σ4.kheap q.bv := by + intro q hq + obtain ⟨g, hg, -⟩ := hR4.pool q hq + have : q.bv ≠ pc.bv := kheap_ne hg hkc4 (fun _ h1 h2 => by subst h1; cases h2) + simp [setCnodeSlot, this] + have hWs : WritesIn st s4 s5 (SlotW th) := by + refine ⟨by rw [hs5]; simp, by rw [hs5]; simp, fun b hb hn => ?_⟩ + obtain ⟨h1, h2⟩ := ne_slot_words hPl hxs hn + exact hfr5 b hb (by rw [hx0]; exact h1) (by rw [hx0]; exact h2) + refine ⟨⟨hH.capStable _ _ a4 _ _ hS4 hWs ⟨pc.bv, r, cs, idx, nc, hkc4, rfl⟩, hSt5, hSV5, ?_, ?_⟩, hF5, hcv5⟩ + · rw [hks5]; rw [hks4] at hR4 ⊢ + exact hR4.frame hfrV rfl hpools + · refine hPt4.frame (fun b hb => ?_) (fun q hq => ?_) + · obtain ⟨h1, h2⟩ := ne_slot_words hPl hxs (hPS b hb) + exact hfr5 b (hb.aligned hPP) (by rw [hx0]; exact h1) (by rw [hx0]; exact h2) + · obtain ⟨ao, hao⟩ := hPt4.arch hq + have : q.bv ≠ pc.bv := kheap_ne hao hkc4 (fun _ h1 h2 => by subst h1; cases h2) + simp [setCnodeSlot, this] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] at hcs ⊢ + refine WP.spec_bind (fin_cap s1 a σ1 ⟨hS1, hSt1, hSV1, hR1, hPt1⟩ hkc1 hk1) ?_ + rintro s5 ⟨hU5, hF5, hcv5⟩ + rw [setCnodeSlot_self hkc1 hcs] at hU5 + have hLike : VsLike (capV (L.mem s5) (cslotAddr (pc.bv, idx))) + (.Frame ptr sz rt dev false true fa fv) := by + rw [hcv5] + refine ⟨⟨rfl, rfl, rfl, rfl⟩, ?_⟩ + intro P' L4' L3' L2' L1' h + simp only [VCapOK] at h ⊢ + refine ⟨?_, h.2.1, ?_⟩ + · intro e + cases e + · intro e + cases e + refine (WP.spec_ok _).2 ⟨rfl, _, σ1, rfl, ?_, hU5, (SlotFoot.of_same hSS1 _).trans hF5, hLike, KFoot.sts hkt' _, + ⟨True.intro, by rw [hcv5]; exact True.intro, ArchShr.of_keep (sts_keep hkt' _).1 (sts_keep hkt' _).2⟩⟩ + unfold MapSpec + dsimp only + rw [← hcur, nd_bind_one (Notif.setThreadState_eq .restart hkt'), performUnmap_none] + exact ⟨rfl, id⟩ + + +set_option maxHeartbeats 4000000 in +/-- The VM decode of a native Frame cap at PageMap / PageUnmap is the ported arch decode. -/ +private theorem nativeFrameSpecDec {lbl : U64} + (hl : lbl = fl4_abi.label.X86PageMap ∨ lbl = fl4_abi.label.X86PageUnmap) + (fp : Bool) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) : + specDecV fp lbl.bv args ci sl (.Frame p sz rt d m ep asid va) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec + (Fl4Spec.archDecodeInvocation (Z := Unit) lbl.bv args ci sl + (frameACapG ⟨p, sz, rt, d, m, ep, asid, va⟩) exs))) := by + have hne : lbl ≠ fl4_abi.label.X86PageMapEPT := by + rcases hl with rfl | rfl + · unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageMapEPT; decide + · unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageMapEPT; decide + have hlbl := frame_label lbl hne + have hds : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4DeriveSubframeLabel := by + intro he + have hd := hlbl.ds he + rcases hl with hm | hu + · exact (show fl4_abi.label.X86PageMap ≠ fl4_abi.label.X86PageDeriveSubframe by + unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageDeriveSubframe; decide) (hm.symm.trans hd) + · exact (show fl4_abi.label.X86PageUnmap ≠ fl4_abi.label.X86PageDeriveSubframe by + unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageDeriveSubframe; decide) (hu.symm.trans hd) + have hta : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4TestAndClearLabel := by + intro he + have hd := hlbl.tac he + rcases hl with hm | hu + · exact (show fl4_abi.label.X86PageMap ≠ fl4_abi.label.X86PageTestAndClearAccessed by + unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageTestAndClearAccessed; decide) (hm.symm.trans hd) + · exact (show fl4_abi.label.X86PageUnmap ≠ fl4_abi.label.X86PageTestAndClearAccessed by + unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageTestAndClearAccessed; decide) (hu.symm.trans hd) + have hept : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4PageMapEptLabel := by + rcases hl with rfl | rfl + · rw [dataToNat_bv]; unfold fl4_abi.label.X86PageMap Fl4Spec.fl4PageMapEptLabel; decide + · rw [dataToNat_bv]; unfold fl4_abi.label.X86PageUnmap Fl4Spec.fl4PageMapEptLabel; decide + cases m <;> cases ep <;> first + | exact False.elim (hn ⟨rfl, rfl⟩) + | simp only [specDecV, absCap, Fl4Spec.fl4DecodeInvocationV, + Fl4Spec.fl4DecodeInvocation, Fl4Spec.fl4ArchDecodeInvocation, hds, hta, hept, + frameACapG, Bool.false_eq_true, ↓reduceIte, reduceCtorEq, false_and] + +/-- The footprint relation the native frame walks run under at a VM entry. -/ +private abbrev NFSR (st : machine.Store S) [L : P1.LawfulStore st] (AR : A → Fl4Spec.Cr3 → Prop) + (Z : XSets) (s : S) : S → A → Fl4Spec.State Unit → Prop := + FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) + s (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) + +set_option maxHeartbeats 4000000 in +/-- A successful native frame Map / Unmap walk: the VM relation after the caller's restart. -/ +private theorem VmEptState.nativeFrameSelOk [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d m ep asid va)) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + {dv : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {dA : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} + (hDec : dv = Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec dA))) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) + (res : core.result.Result Unit error.Exc × S × A) (iv : Fl4Spec.ArchInvocation) (σ' : Fl4Spec.State Unit) + (hok : res.1 = .Ok ()) (hm : dA.run σ = ({(.ok iv, σ)}, False)) (hmap : MapSpec σ iv σ') + (hSR' : NFSR st AR Z s res.2.1 res.2.2 σ') + (hSt' : StateRel Z.th (L.mem res.2.1) σ') (hSV' : SlotsValid Z.th (L.mem res.2.1)) + (hR' : VsRel Z.P (L.mem res.2.1) (L.ksb res.2.1) σ') (hPt' : PtRel Z.L4 Z.L3 Z.L2 Z.L1 (L.mem res.2.1) σ') + (hSF : SlotFoot Z.th (L.mem s) (L.mem res.2.1) (cslotAddr (pc.bv, idx))) + (hLike : VsLike (capV (L.mem res.2.1) (cslotAddr (pc.bv, idx))) (.Frame p sz rt d m ep asid va)) + (hK : KFoot σ σ') + (hLeaf : MapLeafF (L.mem s) (L.mem res.2.1) (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) σ σ' ∨ + UnmapLeafF (L.mem res.2.1) (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) σ σ') : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don dv s res := by + have B := J.1.1 + obtain ⟨hS, -, -, hPI, hCur⟩ := B.xtop.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have hk : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have hst : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stsAct (updK σ ct.bv (.tcb {xct with tcbState := .restart})) ct.bv)}, False) := by + rw [hcur] + exact Notif.setThreadState_eq .restart hkt + have hW : P1.WritesIn st s res.2.1 (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s) := + hSR'.2.mono fun _ h => h.toW + obtain ⟨hCR', hFI', hMO'⟩ := crelM_after hCR hFI hMO hc hSt' hSV' hSF hLike hK rfl + have hFin := vs_x_afterT hS hPI hCur hSR' hR' hPt' hCR' hFI' hMO' hK + have F : VsSFr (stsAct (updK σ ct.bv (.tcb {xct with tcbState := .restart})) ct.bv) σ' := + vsSFr_of_post hst (Or.inl ⟨iv, Or.inl hmap⟩) + obtain ⟨hkb, hdw⟩ := dom_of_foot hS (Or.inr hW) + have hD' : DomRel (L.mem res.2.1) (L.ksb res.2.1) σ' := by + have hdom := setThreadState_dom hk hst + rw [hkb] + exact hDR.frame hdw (F.dl.trans hdom.1) (F.dsi.trans hdom.2) + have hXR := FinXT.toXR Z.G B.xtop.esa hIA.dom hFin hD' + have hX' : XTopAt (L := L) AR Z res.2.1 res.2.2 σ' := by + obtain ⟨hFin', ⟨G', hPl', -, -, hD', -, hIA', hSA'⟩, hE'⟩ := hXR + obtain ⟨⟨⟨hSch, hEp, hNt, -⟩, -⟩, -⟩ := id hFin' + exact ⟨hFin', ⟨hPl', hCR', hFI', hD', by rw [hkb]; exact hMO', hIA', hSA'⟩, + IpcRel.of hNt hEp (sched_sc hSch), hE'⟩ + obtain ⟨_, J'⟩ := J.nativeFramePost hni hpc hidx hc hn hX' hW hSF hLike hK ⟨iv, hmap⟩ hLeaf + have hns : ¬ Fl4Spec.PerformSubst (.invokeArchObject (.spec iv)) := fun h => h + refine Or.inr ⟨.base (.invokeArchObject (.spec iv)), ?_, _, hst, (.ok [], σ'), ?_, + Or.inl ⟨hok, rfl, J'.anyX arI⟩⟩ + · rw [hDec] + exact DecOut.of_eq (run_liftME_ok (run_liftME_ok (run_liftME_ok hm))) + · rw [VirtF13.fl4PerformInvocationVF_base on rc blk call don _ hns] + exact perf_page_mem blk call don hst hmap + +/-- A failed native frame Map / Unmap decode: the state is unchanged. -/ +private theorem VmEptState.nativeFrameSelErr + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {dv : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {dA : Fl4Spec.SEMonad Unit Fl4Spec.ArchInvocation} + (hDec : dv = Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec dA))) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) + (res : core.result.Result Unit error.Exc × S × A) (e : error.Exc) (e' : Fl4Spec.SyscallError) + (he : res.1 = .Err e) (hs : res.2.1 = s) (ha : res.2.2 = a) (hee : ErrRel e e') + (hm : dA.run σ = ({(.error e', σ)}, False)) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don dv s res := by + refine Or.inl ⟨e, he, hs, ⟨e', ?_, hee⟩, ?_⟩ + · rw [hDec] + exact run_liftME_err (run_liftME_err (run_liftME_err hm)) + · rw [hs, ha] + exact J.anyX arI + +set_option maxHeartbeats 4000000 in +/-- A native frame Map walk's outcome (any size), in the VM relation. -/ +private theorem VmEptState.nativeFrameMapPost [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d m ep asid va)) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + {dv : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {slbl : Fl4Spec.Data} {sargs : List Fl4Spec.Data} + {ci : Fl4Spec.CapRef} {ac : Fl4Spec.ArchCap} {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (hDec : dv = Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec + (Fl4Spec.archDecodeInvocation (Z := Unit) slbl sargs ci (pc.bv, idx) ac exs)))) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) + (res : core.result.Result Unit error.Exc × S × A) + (hres : (∃ e e', res.1 = .Err e ∧ MapErrRel e e' ∧ + ExceptT.run (Fl4Spec.archDecodeInvocation (Z := Unit) slbl sargs ci (pc.bv, idx) ac exs) σ = + ({(.error e', σ)}, False) ∧ res.2.1 = s ∧ res.2.2 = a) ∨ + (res.1 = .Ok () ∧ ∃ iv σ', ExceptT.run (Fl4Spec.archDecodeInvocation (Z := Unit) slbl sargs ci + (pc.bv, idx) ac exs) σ = ({(.ok iv, σ)}, False) ∧ + MapSpec σ iv σ' ∧ StateRel Z.th (L.mem res.2.1) σ' ∧ SlotsValid Z.th (L.mem res.2.1) ∧ + VsRel Z.P (L.mem res.2.1) (L.ksb res.2.1) σ' ∧ PtRel Z.L4 Z.L3 Z.L2 Z.L1 (L.mem res.2.1) σ' ∧ + NFSR st AR Z s res.2.1 res.2.2 σ' ∧ + SlotFoot Z.th (L.mem s) (L.mem res.2.1) (cslotAddr (pc.bv, idx)) ∧ + VsLike (capV (L.mem res.2.1) (cslotAddr (pc.bv, idx))) (.Frame p sz rt d m false asid va) ∧ + KFoot σ σ' ∧ + MapLeafF (L.mem s) (L.mem res.2.1) (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m false asid va) σ σ')) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don dv s res := by + rcases hres with ⟨e, e', he, hr, hdec, hs, ha⟩ | + ⟨hok, iv, σ', hdec, hmap, hSt', hSV', hR', hPt', hSR', hSF, hLk, hK, hMF⟩ + · exact J.nativeFrameSelErr hDec on rc blk call don res e e' he hs ha (ErrRel.of_map hr) hdec + · have hf : VsLike (.Frame p sz rt d m false asid va) (.Frame p sz rt d m ep asid va) := by + cases m <;> cases ep <;> first + | exact False.elim (hn ⟨rfl, rfl⟩) + | exact ⟨⟨rfl, rfl, rfl, rfl⟩, fun _ _ _ _ _ h => h⟩ + exact J.nativeFrameSelOk hni hpc hidx hc hn hct hDec on rc blk call don res iv σ' hok hdec hmap + hSR' hSt' hSV' hR' hPt' hSF (nativeFrameLike_trans hLk hf) hK (Or.inl ⟨trivial, hMF.2⟩) + +set_option maxHeartbeats 8000000 in +/-- Native PageMap at the looked-up Frame slot, with the actual message words and extra-cap lookup. +[J] The current thread is entered and the selected cap is a native Frame. +[P] Native VSpace architecture refinement. -/ +theorem VmEptState.selectedNativeFrameMap [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d m ep asid va)) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {lbl : U64} (hMapLabel : lbl = fl4_abi.label.X86PageMap) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (len buf : U64) (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (ex : Array obj.Slot 3#usize) (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hE : ExtraOK Z.th (L.mem s) ex exs) + (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt d m ep asid va) exs buffer) s res ⦄ := by + have B := J.1.1 + have hne : lbl ≠ fl4_abi.label.X86PageMapEPT := by + rw [hMapLabel]; unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageMapEPT; decide + have hlMap := (frame_label lbl hne).map hMapLabel + have hDec := nativeFrameSpecDec (Or.inl hMapLabel) fp (args.map UScalar.bv) ci (pc.bv, idx) hn exs buffer + obtain ⟨hS, hR, hPt, hPI, -⟩ := B.xtop.fin + obtain ⟨hPl, hCR, -, -, -, -, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, hrun⟩ := B.curRunnable hni hct + have htcb := (B.xtop.curTcb hcur).1 + have hk : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hd := (B.vsc (cslotAddr (pc.bv, idx)) hA.isSlot).okTab + (show Vs.TabIn Z.L3 Z.L2 Z.L1 (capV (L.mem s) (cslotAddr (pc.bv, idx))) from by + rw [capV_of_capAt hc]; trivial) + rw [capV_of_capAt hc] at hd + have hplace : (m = true → asid ≠ 0#u64) ∧ PaM p (frameMaskOf sz) ∧ (m = true → UserV va) := hd + obtain ⟨c0, ac, hcap0, hrel, -⟩ := hCR.1.abs_cap hA + rw [hc] at hcap0 + cases eq_of_ok_eq hcap0 + have hfw : CapWf (.Frame p sz rt d m false asid va) := hrel.1 + have hfe : EncWf (.Frame p sz rt d m false asid va) := by + have h := hc + rw [capAt_words] at h + have h2 : EncWf (.Frame p sz rt d m ep asid va) := decode_encWf h + exact h2 + have hfcv : CapValid Z.th (.Frame p sz rt d m false asid va) := by + have h := hCR.2.1.abs hA hc + cases m <;> cases ep <;> first | exact False.elim (hn ⟨rfl, rfl⟩) | exact h + have hSR : NFSR st AR Z s s a σ := ⟨hS, P1.WritesIn.refl _ _⟩ + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok] + nth_rw 1 [hMapLabel] + have hcanon : vspace.decode_frame st arI s a fl4_abi.label.X86PageMap len (cslotAddr (pc.bv, idx)) + (.Frame p sz rt d m ep asid va) call buf ex = + vspace.decode_page_map st arI s a len (cslotAddr (pc.bv, idx)) ⟨p, sz, rt, d, m, false, asid, va⟩ buf ex := by + have hne' : fl4_abi.label.X86PageMap ≠ fl4_abi.label.X86PageMapEPT := by + unfold fl4_abi.label.X86PageMap fl4_abi.label.X86PageMapEPT; decide + have h2 : vspace.decode_frame st arI s a fl4_abi.label.X86PageMap len (cslotAddr (pc.bv, idx)) + (.Frame p sz rt d m false asid va) call buf ex = + vspace.decode_page_map st arI s a len (cslotAddr (pc.bv, idx)) ⟨p, sz, rt, d, m, false, asid, va⟩ buf ex := by + simp only [vspace.decode_frame, vspace.as_frame, bind_ok, hne', ↓reduceIte, Bool.false_eq_true, ite_self] + rw [← h2] + cases m <;> cases ep + · rfl + · exact nativeFrameUnmappedDecodeEq fl4_abi.label.X86PageMap len (cslotAddr (pc.bv, idx)) + buf p rt asid va sz d call ex (Or.inl rfl) + · rfl + · exact (hn ⟨rfl, rfl⟩).elim + rw [hcanon] + rcases ExtraOK.relS hPl hE with ⟨hz, he⟩ | ⟨hnz, pp, rr, ii, rest, hpp, hii, hex, he⟩ + · subst he + have hex : vspace.extra ex 0#usize = ok none := by + rw [vsExtra_at ex 0#usize (by decide) _ (exAt_idx0 ex), hz, if_pos rfl] + unfold vspace.decode_page_map + rw [hct, hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + refine J.nativeFrameSelErr hDec on rc blk call don _ _ .truncatedMessage rfl rfl rfl (Or.inl trivial) ?_ + rw [show Fl4Spec.archDecodeInvocation (Z := Unit) lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) + (frameACapG ⟨p, sz, rt, d, m, ep, asid, va⟩) [] = + Fl4Spec.decodePageInvocation lbl.bv (args.map UScalar.bv) (pc.bv, idx) + (frameACapG ⟨p, sz, rt, d, m, ep, asid, va⟩) [] from rfl] + unfold frameACapG + exact decodeMap_trunc hlMap _ _ _ _ _ _ _ _ _ (by simp) σ + · have hEx : vspace.extra ex 0#usize = ok (some (exAt ex 0)) := by + rw [vsExtra_at ex 0#usize (by decide) _ (exAt_idx0 ex), if_neg hnz] + have hxsl : AbsSlot Z.th (pp.bv, ii) := ⟨pp, rfl, Or.inl (by simpa [hii] using hpp)⟩ + obtain ⟨vc, vc', hcv, hRel, _⟩ := hCR.1.abs_cap hxsl + rw [← hex] at hcv + rw [capV_of_capAt hcv] at he + subst he + have hvc : ∀ pp asid', vc = .Pml4 pp true asid' → asid' ≠ 0#u64 ∧ asid'.val < 2 ^ 20 := by + intro pp asid' eq + have hx' : IsSlot Z.th (exAt ex 0) := by rw [hex]; exact hxsl.isSlot + have hx := B.vsc (exAt ex 0) hx' + rw [capV_of_capAt hcv, eq] at hx + exact hx.2.2 rfl + have hPV1 : ∀ b, PtW Z.L1 b → ¬ VsWrite Z.P (L.ksb s) b := fun b ⟨q, hq, i, hi, e⟩ => + hS.2.pv b ⟨q, Or.inr (Or.inr (Or.inr hq)), i, hi, e⟩ + have hVR : CapRel vc (absCap vc) := ⟨hRel.1, rfl⟩ + have hCore := B.kern.act + have hcapN : frameACapG ⟨p, sz, rt, d, m, ep, asid, va⟩ = frameACapG ⟨p, sz, rt, d, m, false, asid, va⟩ := rfl + rw [hcapN] at hDec + cases sz + · have hcapS : frameACapG ⟨p, .Small, rt, d, m, false, asid, va⟩ = + frameACap ⟨p, .Small, rt, d, m, false, asid, va⟩ := rfl + rw [hcapS] at hDec + have hW := frame_map_rel ((vsFrameHyps_top hRef).foot (s0 := s) + (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + s a σ hPl hS.2.vs hS.2.sd hS.2.pt hS.2.ps hPV1 hS.2.po hCR.1 hCR.2.1 hR hPt hSR + ct hct hcur.symm hk hrun htcb hCore ⟨p, .Small, rt, d, m, false, asid, va⟩ rfl hfw hfe hfcv + hplace.1 hplace.2.2 hplace.2.1 hpc hidx hlMap len buf args hlen hargs ci ex hEx hcv hVR + (pp.bv, ii) rest hvc hPI + exact WP.spec_mono hW fun res hres => + J.nativeFrameMapPost hni hpc hidx hc hn hct hDec on rc blk call don res hres + · have hW := frame_map_large_rel ((vsFrameHypsL_top hRef).foot (s0 := s) + (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + s a σ hPl hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hCR.1 hCR.2.1 hR hPt hSR + ct hct hcur.symm hk hrun htcb hCore ⟨p, .Large, rt, d, m, false, asid, va⟩ rfl hfw hfe hfcv + hplace.1 hplace.2.2 hplace.2.1 hpc hidx hlMap len buf args hlen hargs ci ex hEx hcv hVR + (pp.bv, ii) rest hvc hPI + exact WP.spec_mono hW fun res hres => + J.nativeFrameMapPost hni hpc hidx hc hn hct hDec on rc blk call don res hres + · have hW := frame_map_huge_rel ((vsFrameHypsL_top hRef).foot (s0 := s) + (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + s a σ hPl hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hCR.1 hCR.2.1 hR hPt hSR + ct hct hcur.symm hk hrun htcb hCore ⟨p, .Huge, rt, d, m, false, asid, va⟩ rfl hfw hfe hfcv + hplace.1 hplace.2.2 hplace.2.1 hpc hidx hlMap len buf args hlen hargs ci ex hEx hcv hVR + (pp.bv, ii) rest hvc hPI + exact WP.spec_mono hW fun res hres => + J.nativeFrameMapPost hni hpc hidx hc hn hct hDec on rc blk call don res hres + +set_option maxHeartbeats 8000000 in +/-- Native PageUnmap at the looked-up Frame slot. +[J] The current thread is entered and the selected cap is a native Frame. +[P] Native VSpace architecture refinement. -/ +theorem VmEptState.selectedNativeFrameUnmap [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d m ep asid va)) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {lbl : U64} (hUnmapLabel : lbl = fl4_abi.label.X86PageUnmap) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (len buf : U64) (args : List U64) + (ex : Array obj.Slot 3#usize) (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt d m ep asid va) exs buffer) s res ⦄ := by + have B := J.1.1 + have hne : lbl ≠ fl4_abi.label.X86PageMapEPT := by + rw [hUnmapLabel]; unfold fl4_abi.label.X86PageUnmap fl4_abi.label.X86PageMapEPT; decide + have hlU := (frame_label lbl hne).unmap hUnmapLabel + have hDec := nativeFrameSpecDec (Or.inr hUnmapLabel) fp (args.map UScalar.bv) ci (pc.bv, idx) hn exs buffer + obtain ⟨hS, hR, hPt, hPI, -⟩ := B.xtop.fin + obtain ⟨hPl, hCR, -, -, -, -, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, hrun⟩ := B.curRunnable hni hct + have htcb := (B.xtop.curTcb hcur).1 + have hk : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hd := (B.vsc (cslotAddr (pc.bv, idx)) hA.isSlot).okTab + (show Vs.TabIn Z.L3 Z.L2 Z.L1 (capV (L.mem s) (cslotAddr (pc.bv, idx))) from by + rw [capV_of_capAt hc]; trivial) + rw [capV_of_capAt hc] at hd + have hplace : (m = true → asid ≠ 0#u64) ∧ PaM p (frameMaskOf sz) ∧ (m = true → UserV va) := hd + obtain ⟨c0, ac, hcap0, hrel, -⟩ := hCR.1.abs_cap hA + rw [hc] at hcap0 + cases eq_of_ok_eq hcap0 + have hfe : EncWf (.Frame p sz rt d m ep asid va) := by + have h := hc + rw [capAt_words] at h + exact decode_encWf h + have hSR : NFSR st AR Z s s a σ := ⟨hS, P1.WritesIn.refl _ _⟩ + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok] + nth_rw 1 [hUnmapLabel] + have hW := observed_frame_unmap_rel ((vsFrameUnmapHyps_top hRef).foot (s0 := s) + (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + s a σ hPl hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hPI hS.2.po hCR.1 hCR.2.1 hR hPt hSR + ct hct hcur.symm hk hrun htcb B.kern.act ⟨p, sz, rt, d, m, ep, asid, va⟩ hn hrel.1 hfe + hplace.1 hplace.2.2 hpc hidx hc hlU len buf call ex (args.map UScalar.bv) ci exs + refine WP.spec_mono hW ?_ + rintro res ⟨hok, iv, σ', hdec, hmap, ⟨hSR', hSt', hSV', hR', hPt'⟩, hSF, hLike, hK, hUF⟩ + exact J.nativeFrameSelOk hni hpc hidx hc hn hct hDec on rc blk call don res iv σ' hok hdec hmap + hSR' hSt' hSV' hR' hPt' hSF hLike hK (.inr hUF) + +/-- PageMap and PageUnmap at the looked-up Frame slot (`selectedNativeFrameMap`, +`selectedNativeFrameUnmap`). The label split stays at the caller so EPT-map, reply-in-decode and +derivation remain on their respective paths. +[J] The current thread is entered and the selected cap is a native Frame. +[P] Native VSpace architecture refinement. -/ +theorem VmEptState.selectedNativeFrame [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Frame p sz rt d m ep asid va)) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {lbl : U64} (hl : lbl = fl4_abi.label.X86PageMap ∨ lbl = fl4_abi.label.X86PageUnmap) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (len buf : U64) (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (ex : Array obj.Slot 3#usize) (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hE : ExtraOK Z.th (L.mem s) ex exs) + (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Frame p sz rt d m ep asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) + (.Frame p sz rt d m ep asid va) exs buffer) s res ⦄ := by + rcases hl with hM | hU + · exact J.selectedNativeFrameMap hni hRef hpc hidx hc hn hM hct len buf args hlen hargs ex exs hE ci buffer + on rc blk call don fp + · exact J.selectedNativeFrameUnmap hni hRef hpc hidx hc hn hU hct len buf args ex exs ci buffer + on rc blk call don fp + + +/-- The ordinary or first-phase native frame lookup selects PageMap or +PageUnmap. The selected capability and arguments are those from `LookAt` +and the matched message and extra-cap reads. -/ +theorem VmEptState.selectedNativeFrameS3 [PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {lbl : U64} (hl : lbl = fl4_abi.label.X86PageMap ∨ lbl = fl4_abi.label.X86PageUnmap) + {ct cptr len buf p rt asid va : U64} {sz : capability.FrameSize} {d m ep : Bool} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Frame p sz rt d m ep asid va) ex args exs) + (hn : NativeCap (.Frame p sz rt d m ep asid va)) + {MR : A → Fl4Spec.MachineState → Prop} + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Frame p sz rt d m ep asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Frame p sz rt d m ep asid va) exs buffer) s res ⦄ := by + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + have hs := J.selectedNativeFrame hni hRef hpc rfl q.capAt hn hl q.current len buf + (args.map fun x => (⟨x⟩ : U64)) q.length q.words ex exs q.extra (Fl4Spec.toBl cptr.bv) buffer + (MR := MR) on rc blk call don fp + rw [show (args.map fun x => (⟨x⟩ : U64)).map UScalar.bv = args from zv_map_mk args] at hs + exact hs + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3InsTcb.lean b/proofs/Fl4Proofs/P3/VmEntryS3InsTcb.lean new file mode 100644 index 000000000..3f9f6d67c --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3InsTcb.lean @@ -0,0 +1,88 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route D): **a cap inserted into an empty TCB slot at a `VmEptState`** +(`VmEptState.insTcbV`): P3/XTopJointTcbInstall's `JAtZB.insTJ`, then `VmEptState.ofCn`. The slot fact `CnVtx` by +`cnVtx_one` (only the TCB slot changes; its cap's VT-x view `VtOK` against the source's, as `derive_vt` gives for a +derived cap: a derived Frame cap is unmapped, no view), the TCBs' `SLOT_EPT` kept (the slot written is not slot 5). + +No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.VmEntryS3XferAll +import Fl4Proofs.P3.XTopJointTcbInstall +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +/-- `CnVtx` of a step writing one empty slot `d` with a cap whose VT-x view is `VtOK` against a source slot's. -/ +theorem cnVtx_one {th : TypedHeap} {m m' : U64 → U64} {x d : U64} {c' : capability.Cap} + (hx : IsSlot th x) (hdv : capV m d = .Null) (hdc : capV m' d = c') + (hoth : ∀ z, IsSlot th z → z ≠ d → capV m' z = capV m z) (hvt : VtOK (capV m x) c') : CnVtx th m m' := by + refine ⟨fun y => y, fun y hy => hy, fun y y' _ _ h => h, fun y hy => ⟨y, hy, rfl⟩, fun z hz => ?_⟩ + by_cases ed : z = d + · subst ed + exact Or.inr ⟨hdv, x, hx, by rw [hdc]; exact hvt⟩ + · exact Or.inl (by rw [hoth z hz ed]) + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {VX XX : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- **An insertion into an empty TCB slot (not `SLOT_EPT`) keeps `VmEptState`** (module docstring). -/ +theorem VmEptState.insTcbV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {n : Nat} + {σ' : Fl4Spec.State Unit} + (hok : n = 2 → Endp.IpcBufCapOK σ') {t : U64} (ht : Z.th t = some .tcb) + (hn : n < tcbSlots) (hn5 : n ≠ 5) {c : Fl4Spec.Cap} {src : Fl4Spec.CslotPtr} {s' : S} + {G' : MdbGhost} + (hci : Fl4Spec.capInsert (Z := Unit) c src (t.bv, Fl4Spec.tcbCnodeIndex n) σ = ({((), σ')}, False)) + (hDS' : DState (L := L) Z.th s' σ' G') (hSt : MdbStep (L.mem s) Z.G (L.mem s') G') + (hwS : P1.WritesIn st s s' (Sched.SlotWd Z.th)) + (hnull : capV (L.mem s) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Null) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) → + capV (L.mem s') y = capV (L.mem s) y) + (hnu : isUt (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n))) = false) + (hnn : capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) ≠ .Null) + (hsh : ∃ y0, IsSlot Z.th y0 ∧ + shape (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n))) = shape (capV (L.mem s) y0)) + (hfh : n = 3 → capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Null ∨ + ∃ ep b0 b1 b2 b3 b4, capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Endpoint ep b0 b1 b2 b3 b4) + (hbs : n = 2 → Sched.BufSlotOK (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) + (hlive : ∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = .Thread t) + (hvt : ∀ P L4 L3 L2 L1 : Set U64, Vs.VsSlotsW Z.th P L4 L3 L2 L1 (L.mem s) → + Vs.VCapW P L4 L3 L2 L1 (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) + (hft : ∀ U : Nat → Prop, Vs.FrSlots Z.th U (L.mem s) → + Vs.FrIn U (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) + (hnt : Vs.VsNat Z.th (L.mem s) → Vs.NativeCap (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) + (htt : ∀ R, Vs.TabSlots Z.th R (L.mem s) → Vs.TabR R (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) + (hkr : Vs.VKey (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n))) ≠ none → + (∃ y0, IsSlot Z.th y0 ∧ y0 ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) ∧ + capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = capV (L.mem s) y0) ∧ + ¬ Vs.TabUn (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) + {x : U64} (hx : IsSlot Z.th x) + (hvtx : VtOK (capV (L.mem s) x) (capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) : + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI VX XX XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' s' a σ' := by + obtain ⟨Ac', Lst', D', B'⟩ := J.1.1.insTJ hok ht hn hci hDS' hSt hwS hnull hcap hnu hnn hsh hfh hbs hlive hvt hft + hnt htt hkr + have hAd : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex n) := ⟨t, rfl, Or.inr ⟨ht, n, hn, rfl⟩⟩ + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) c src (t.bv, Fl4Spec.tcbCnodeIndex n) σ).1 := by + rw [hci]; rfl + refine ⟨Ac', Lst', D', J.ofCn B' hwS (cnVtx_one hx hnull rfl hcap hvtx) hSt ?_ (capInsert_nkn c _ _ σ _ hmem)⟩ + intro u hu + refine Or.inr (Or.inr ⟨hu, hcap _ (isSlot_eptSlot hu) fun e => hn5 ?_⟩) + have hA5 : AbsSlot Z.th (u.bv, Fl4Spec.tcbCnodeIndex 5) := ⟨u, rfl, Or.inr ⟨hu, 5, by decide, rfl⟩⟩ + have e2 := cslotAddr_inj J.1.1.xtop.rel.1 hA5 hAd e + have e3 := congrArg (fun p : Fl4Spec.CslotPtr => blVal 0 p.2) e2 + simp only at e3 + rw [tcbCnodeIndex_blVal 5 (by decide), tcbCnodeIndex_blVal n hn] at e3 + exact e3.symm + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3IoControl.lean b/proofs/Fl4Proofs/P3/VmEntryS3IoControl.lean new file mode 100644 index 000000000..75624d068 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3IoControl.lean @@ -0,0 +1,299 @@ +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.IoControl +import Fl4Proofs.P3.IntInv +import Fl4Proofs.P3.EptDelIo + +/-! +Selected I/O-port control. The decoder trace records the actual bitmap +update and insertion. [J] Global references and the spec I/O-port bitmap +agreement at invocation. The reached fresh slot is used for VM/EPT framing. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched Fl4Proofs.P1 + +noncomputable section +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s s' : S} {a : A} {σ σ' : Fl4Spec.State Unit} + {G' : MdbGhost} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- Issuing a cap into an empty CNode slot preserves VM/EPT when the issued +cap has neither the native VSpace nor the VM/EPT slot view. -/ +theorem VmEptState.insertIssuedS3 + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hZ : ZNoBlk Z.th (L.mem s) (L.ksb s)) + {ps pd : U64} {rs rd : Nat} {idxs idxd : List Bool} {c : capability.Cap} + (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) (hld : idxd.length = rd) + (hci : Fl4Spec.capInsert (Z := Unit) (absCap c) (ps.bv, idxs) (pd.bv, idxd) σ = ({((), σ')}, False)) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pd.bv, idxd) → capV (L.mem s') y = capV (L.mem s) y) + (hcd : capV (L.mem s') (cslotAddr (pd.bv, idxd)) = c) + (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + (hnu : ∀ p sb fi d, c ≠ .Untyped p sb fi d) + (hnn : c ≠ .Null) (hnz : ∀ p zt n, c ≠ .Zombie p zt n) + (hw : P1.WritesIn st s s' (InSlot Z.th)) (hCR : CRel Z.th (L.mem s') σ' G') + (hFI : FinInv Z.th (L.mem s') σ' G') (hMO : MdbOKV (L.ksb s) (L.mem s') G') + (hTK : TypedKept Z.th (L.mem s) (L.mem s')) (hDL : DestLive Z.th (L.mem s) (L.ksb s) pd) + (hDl : DelInv Z.th (L.mem s) → DelInv Z.th (L.mem s')) (hM : MdbStep (L.mem s) Z.G (L.mem s') G') + (hVT : Vs.VsSlotsT Z.th (L.mem s) (L.mem s')) (hVK : Vs.VsKeep Z.th Z.th (L.mem s) (L.mem s')) + (hV : vkeyF c = none) : + ∃ Ac' Lst', VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D s' a σ' := by + let dst : Fl4Spec.CslotPtr := (pd.bv, idxd) + have B := J.1.1 + have hX := B.xtop + have hDs : AbsSlot Z.th dst := ⟨pd, rfl, Or.inl (by rw [hld]; exact hpd)⟩ + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) (absCap c) (ps.bv, idxs) dst σ).1 := by + rw [hci] + rfl + have hcn := capInsert_nkn (absCap c) (ps.bv, idxs) dst σ ((), σ') hmem + have hkc : KeepC σ σ' := keepC_of hcn (capInsert_nko (absCap c) _ _ σ _ hmem) (by + rintro p (rfl | hp) + · obtain ⟨cs, h, -⟩ := hX.rel.2.1.1.cnode ps rs hps + exact ⟨rs, cs, h⟩ + · rw [Set.mem_singleton_iff.1 hp] + obtain ⟨cs, h, -⟩ := hX.rel.2.1.1.cnode pd rd hpd + exact ⟨rd, cs, h⟩) + have hslot : ∀ n < tcbSlots, ∀ t, Z.th t = some .tcb → slotOf t n ≠ cslotAddr dst := by + intro n hn t ht he + have hI : IsSlotAt Z.th t n := Or.inr ⟨ht, hn⟩ + obtain ⟨p, i, hp, hp1, hp2⟩ := hDs.at + obtain rfl : p = pd := P1.eq_of_bv hp1.symm + have hne := IsSlotAt.apart hX.rel.1 hI hp (fun e => by + obtain ⟨e, _⟩ := e + subst t + rw [hpd] at ht + cases ht) + rw [he, hp2] at hne + omega + have hkeep : ∀ n < tcbSlots, ∀ t, Z.th t = some .tcb → + capAt (L.mem s') (slotOf t n) = capAt (L.mem s) (slotOf t n) := by + intro n hn t ht + rw [capAt_capV, capAt_capV, hcap _ ⟨t, n, Or.inr ⟨ht, hn⟩, rfl⟩ (hslot n hn t ht)] + have hWd : P1.WritesIn st s s' (Sched.SlotWd Z.th) := + ⟨hw.1, hw.2.1, fun b hb hn => hw.2.2 b hb fun h => hn (Sched.slotSpan_wd hX.rel.1 hb h)⟩ + have hnotUt : ∀ p sb fi d, capV (L.mem s') (cslotAddr dst) ≠ .Untyped p sb fi d := by + rw [hcd] + exact hnu + have hX1 := XTopAt.slotStep hX hWd hcn hkc hCR hFI hMO (hkeep 1 (by decide)) + obtain ⟨hC, Ac', Lst', R⟩ := B.insCs arI (absCap c) ps pd rs rd idxs idxd hps hpd hci hld hcap + hnull hnotUt hw hFI hMO hTK (hkeep 3 (by decide)) hDL hDl hX1 + have hX1' : XTopAt (L := L) AR (Z.mv G' Z.vs) s' a σ' := hX1 + have hn0 := capV_of_capAt hnull + have hZf : ∀ y, IsSlot Z.th y → ∀ p zt n, capV (L.mem s') y = .Zombie p zt n → + ∃ y0, IsSlot Z.th y0 ∧ capV (L.mem s) y0 = .Zombie p zt n := by + intro y hy p zt n h + by_cases e : y = cslotAddr dst + · subst y + rw [hcd] at h + exact (hnz p zt n h).elim + · exact ⟨y, hy, (hcap y hy e).symm.trans h⟩ + obtain ⟨hL1, _⟩ := LiveDN.wstepXB B B.live hZ hX1' (fun p hp => R.jcov.dead p (B.zD p hp)) hcn.idle hw + [(pd.bv, idxd)] + (fun sl hsl => by + simp only [List.mem_cons, List.not_mem_nil, or_false] at hsl + subst hsl; exact ⟨pd, rd, rfl, hpd, hld, hDL⟩) + (fun y hy hne => hcap y hy (hne (pd.bv, idxd) (List.mem_singleton_self _))) + (fun sl h => by + by_cases e : sl = (pd.bv, idxd) + · exact Or.inr (by simp [e]) + · exact Or.inl (by rw [← capInsert_orig (absCap c) _ _ σ ((), σ') hmem sl e]; exact h)) + (fun sl hsl h => by + simp only [List.mem_cons, List.not_mem_nil, or_false] at hsl + subst hsl; exact (hnn (hcd.symm.trans h)).elim) + hZf + (fun y0 hy0 hn => by + have e : y0 ≠ cslotAddr (pd.bv, idxd) := fun e => hn (by rw [e]; exact hn0) + exact ⟨y0, hy0, by rw [hcap y0 hy0 e]; exact hn, by rw [hcap y0 hy0 e]⟩) + have B' := B.cs_frame (hvn := B.vsShr (G' := G') hX1 hw.1 hVK + (Vs.ArchShr.of_objCn hcn.2 (ostep_of_cnk hcn hkc).2.2)) hw.1 hX1 (KBack.of_keepC hkc) (IpcFrKeep.of_keepC hkc) (B.carry.of_keepC hkc) + (devFrom_kept B.mdbL hX1.rel.2.1.2.2.1 B.ut (utKept_ins hcap hnotUt)) hC R hVT + hL1 (fun p hp => B.zD p (ZTcb.of_zombies hZf hp)) + have hvtx : CnVtx Z.th (L.mem s) (L.mem s') := by + refine ⟨id, fun _ h => h, fun _ _ _ _ h => h, fun y hy => ⟨y, hy, rfl⟩, ?_⟩ + intro y hy + by_cases e : y = cslotAddr dst + · subst y + left + change vkeyF (capV (L.mem s') (cslotAddr (pd.bv, idxd))) = vkeyF (capV (L.mem s) (cslotAddr (pd.bv, idxd))) + rw [hcd, hV, capV_of_capAt hnull] + rfl + · exact Or.inl (by simp only [id_eq, hcap y hy e]) + have hept : EptSlotStep Z.th Z.th (L.mem s) (L.mem s') := by + intro t ht + exact Or.inr (Or.inr ⟨ht, hcap _ (isSlot_eptSlot ht) (by + exact (cnode_slot_ne_tcb hX.rel.1 hpd hld ht 5 (by decide)).symm)⟩) + exact ⟨Ac', Lst', J.ofCn B' hWd hvtx hM hept hcn⟩ + +end + +section + +open Fl4Proofs.P1 Fl4Proofs.P3.Vs + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s s' : S} {a a' : A} {σ σ' : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +private theorem VmEptState.ioMaskS3 + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {low high : U64} (hh : high.val < 65536) (hlh : low.val ≤ high.val) + (hM : MaskPost (L := L) (L.ksb s) low high true s s') : + ∃ Ac' Lst', VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a + (ioMaskSt (BitVec.ofNat 16 low.val) (BitVec.ofNat 16 high.val) true σ) := by + let τ := ioMaskSt (BitVec.ofNat 16 low.val) (BitVec.ofNat 16 high.val) true σ + have B := J.1.1 + have hX := B.xtop + have hf := B.kern.ksbFit + have hSS := MaskPost.slotsSame hX.rel.1 B.ioLay hM + have hO : KsStep σ τ := by constructor <;> rfl + have hw : P1.WritesIn st s s' (IoBW (L.ksb s)) := + ⟨hM.1, hM.2.1, fun b hb hn => hM.2.2.1 b hb fun w hw e => hn ⟨w, hw, e⟩⟩ + have hwK := hw.mono fun _ hb => ioBW_ksW hf hb + have hD := hX.rel.2.2.2.1 + have hIR := hX.fin.1.1.2.2.2.1 + have hIo := hX.fin.1.1.2.2.2.2.1 + have hAR := hX.fin.1.1.2.2.2.2.2.2 + have hD4 := DK4.of_mask hf B.kern.ksbAl hM + have hIR' : IrqRel (L.mem s') (L.ksb s) τ := by + refine ⟨fun i hi => ?_, hIR.node⟩ + have hv := irqStA_val hf hi + rw [hM.2.2.1 _ (by unfold P1.Aligned; rw [hv]; have hAl := B.kern.ksbAl; omega) fun w hw e => by + have := ioA_val hf hw + rw [← e, hv] at this + omega] + exact hIR.st i hi + have hX' : XTopAt (L := L) AR Z s' a τ := + XTopAt.ksStep hX B.ks hwK hO hIR' (IoRel.mask hIo hh hlh hM) + (hD.frame (fun o ho => hD4.2 o ho) rfl rfl) hAR + obtain ⟨Ac', Lst', B'⟩ := B.ofPostX hSS hwK hO hX' + exact ⟨Ac', Lst', J.ofB B' hw (fun b hb => ioBW_oobj hf hb) (fun b hb => ioBW_nh hf hb) + (B.vkeep_of hw fun _ hb => B.ksW_nv (ioBW_ksW hf hb)) rfl rfl⟩ + +set_option maxHeartbeats 5000000 in +private theorem VmEptState.ioIssueTraceS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {G' : MdbGhost} + (h : IoIssueAt (L := L) Z.th Z.G s a σ s' a' σ' G') + (hCR : CRel Z.th (L.mem s') σ' G') (hFI : FinInv Z.th (L.mem s') σ' G') + (hMO : MdbOKV (L.ksb s) (L.mem s') G') (hsg : SpecGlob σ) : + VEAnyX (L := L) arI AR s' a' σ' := by + obtain ⟨rfl, ct, s1, σ1, low, high, s2, ps, pd, rs, rd, idxs, idxd, hct, hR, hst, + hfl, hhl, hMask, hps, hpd, hld, hci, hcap, hnull, hF, hDL, hDl, hMS⟩ := h + have B := J.1.1 + obtain ⟨_, x, hk, hr⟩ := B.curRunnable hni hct + obtain ⟨sR, σR, hstR, _, hRR, _, _, _, BR, F⟩ := B.restartRunJ hct hk hr + have eS := ok_inj (hRR.symm.trans hR) + have eT := (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (congrArg Prod.fst (hstR.symm.trans hst)))).2 + subst sR + cases eT + have J1 := J.ofNat (V' := Z.vs) BR F + have hfZ1 := F.ss + obtain ⟨_, _, J2⟩ := J1.ioMaskS3 hhl hfl hMask + have B2 := J2.1.1 + have hSS := MaskPost.slotsSame BR.xtop.rel.1 BR.ioLay hMask + have hZ2 := (B.zNoBlk hsg).of_same (hfZ1.trans hSS) (hMask.1.trans F.fw.1) + have hS : IsSlot Z.th (cslotAddr (pd.bv, idxd)) := + (show AbsSlot Z.th (pd.bv, idxd) from ⟨pd, rfl, Or.inl (by rw [hld]; exact hpd)⟩).isSlot + have hCap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s2) y := fun y hy hne => by rw [hcap y hy, if_neg hne] + have hd : capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .IoPort low high 0#u64 := by + rw [hcap _ hS, if_pos rfl] + have hw : P1.WritesIn st s2 s' (InSlot Z.th) := by + refine ⟨hF.1, hF.2.1, fun b hb hn => hF.2.2 b hb fun ⟨x, k, hx, hk, e⟩ => hn ⟨x, hx, ?_⟩⟩ + have hf := hx.fit B2.xtop.rel.1 + have := hk.lt + have hv := wordOf_val (a := x) (k := k) (by omega) + rw [e, hv] + omega + have hTK := typedKept_set hcap (Or.inl rfl) + have hVT := Vs.VsSlotsT.of_some (· = cslotAddr (pd.bv, idxd)) hCap + (fun y _ e => by rw [e, hd]; intros; trivial) (fun y _ e => by rw [e, hd]; intros; trivial) + (fun y _ e => by rw [e, hd]; intros; trivial) (fun y _ e => by rw [e, hd]; intros; trivial) + have hVK := Vs.VsKeep.of_nokey (W := (· = cslotAddr (pd.bv, idxd))) hCap + (fun _ _ e => by rw [e, capV_of_capAt hnull]; rfl) fun _ _ e => by rw [e, hd]; rfl + have hMA : MdbStep (L.mem s2) Z.G (L.mem s') G' := hMS + have hMO2 : MdbOKV (L.ksb s2) (L.mem s') G' := by + rw [hMask.1, F.fw.1] + exact hMO + obtain ⟨_, _, J'⟩ := VmEptState.insertIssuedS3 arI J2 hZ2 hps hpd hld hci hCap hd hnull + (by rintro _ _ _ _ h; cases h) (by intro h; cases h) (by rintro _ _ _ h; cases h) + hw hCR hFI hMO2 hTK hDL hDl hMA hVT hVK rfl + exact J'.anyX arI + +set_option maxHeartbeats 6000000 in +/-- The actual I/O-port-control lookup and the new cap's single destination. +[J] SpecGlob and the spec's per-cap I/O-port bitmap agreement hold at dispatch. -/ +theorem VmEptState.selectedIoPortControlS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hsg : SpecGlob σ) (hPorts : SpecIoPorts σ) + {ct cptr len buf lbl : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl .IoPortControl ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) .IoPortControl blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .IoPortControl exs buffer) s res ⦄ := by + let words : List U64 := args.map fun w => ⟨w⟩ + have hw : words.map UScalar.bv = args := by + simp [words, List.map_map, Function.comp_def] + let d := Fl4Spec.decodeIoportControlInvocation (Z := Unit) lbl.bv args sl .ioPortControlCap (exs.map Prod.fst) + have hdec : specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .IoPortControl exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec d)) := rfl + rw [syscall.decode_invocation, q.current, bind_ok, hdec] + have B := J.1.1 + have X0 := B.xtop + have hIo := X0.fin.1.1.2.2.2.2.1 + have hPA := ioPortsAlloc_of_spec X0.rel.2.1.1 X0.rel.2.1.2.2.1 hIo hPorts + obtain ⟨hcur, _, _⟩ := B.curEq q.current + have ht := (X0.curTcb hcur).1 + have hact : ∀ w : U64, w.val < 1024 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ ioA (L.ksb s) w := by + intro w hw' e + have ev := congrArg UScalar.val e + rw [Sched.nodeAt_val' B.kern.nodeFit (by rw [P1.act_val]; decide), P1.act_val, + ioA_val B.kern.ksbFit hw'] at ev + omega + have h : ioport.decode_control st arI s a lbl len (cslotAddr sl) buf ex ⦃ res => + IoCPostTrace (L := L) arI Z.th Z.G (L.ksb s) σ d s a res ⦄ := by + by_cases h63 : lbl.val = 63 + · have he : lbl = fl4_abi.label.X86IOPortControlIssue := UScalar.eq_of_val_eq (h63.trans lbl_ioctl_val.symm) + obtain ⟨ps, hps, hpt⟩ := q.slot + obtain ⟨x, idxs⟩ := sl + dsimp only at hps hpt + subst x + have hi := ioport_control_refines_trace arI Z.th X0.rel.1 s a σ Z.G X0.rel.2.1 B.ioLay hIo hPA + X0.rel.2.2.1 X0.rel.2.2.2.2.1 lbl.bv ((ioControl_label lbl).1 h63) + len buf ct q.current words q.length q.words hcur ht B.kern.act hact ex _ (exAt_idx0 ex) + (exs.map Prod.fst) (ExtraOK.rel1 X0.rel.1 q.extra) hpt rfl q.capAt .ioPortControlCap + rw [← he, hw] at hi + exact hi + · exact ioport_control_other_trace arI Z.th Z.G s a σ lbl h63 lbl.bv + (fun h => h63 ((ioControl_label lbl).2 h)) len (cslotAddr sl) buf ex args sl + .ioPortControlCap (exs.map Prod.fst) + refine WP.spec_mono h ?_ + rintro ⟨res, s1, a1⟩ hP + cases res with + | Err e => + obtain ⟨rfl, ⟨e', hd, hE⟩, rfl⟩ := hP + exact Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err (run_liftME_err hd)), hE⟩, J.anyX arI⟩ + | Ok u => + cases u + obtain ⟨i, hd, σ1, hs, σ', G', hp, hr, _, hf, _, hm, _, _, htr⟩ := hP + exact Or.inr ⟨.base (.invokeArchObject (.spec i)), DecOut.of_eq + (run_liftME_ok (run_liftME_ok (run_liftME_ok hd))), σ1, hs, + (.ok [], σ'), hp, Or.inl ⟨rfl, rfl, J.ioIssueTraceS3 hni htr hr hf hm hsg⟩⟩ + +end + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Irq.lean b/proofs/Fl4Proofs/P3/VmEntryS3Irq.lean new file mode 100644 index 000000000..be31ae401 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Irq.lean @@ -0,0 +1,402 @@ +import Fl4Proofs.P3.VmEntryS3IoControl +import Fl4Proofs.P3.VmCnodeRD +import Fl4Proofs.P3.VmDelIrq +import Fl4Proofs.P3.JEntryIrq + +/-! +Selected IRQ-handler cap (Ack, SetNotification, ClearNotification). The node's +`delete_one` runs at the VM deletion record, the new notification cap is +inserted as an issued cap with no VSpace or VM view. [J] Global references. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched Fl4Proofs.P1 + +noncomputable section +section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- [J] The extra cap's slot is no IRQ node slot. -/ +theorem JAtZB.irqExtraNe (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hsg : SpecGlob σ) + {extra : Aeneas.Std.Array obj.Slot 3#usize} (hN : ExtraNamed Z.th (L.mem s) extra) {irq : U64} + (hirq : irq.val < 128) : Ipc.exAt extra 0 ≠ nodeA (L.ksb s) irq := by + intro e + have hP : Placed Z.th := J.xtop.rel.1 + have hAn : AbsSlot Z.th ((nodeA (L.ksb s) irq).bv, []) := + ⟨_, rfl, Or.inl (by simpa using J.intL.node irq hirq)⟩ + have hne : Ipc.exAt extra 0 ≠ 0#u64 := by + have h := hAn.isSlot.ne_zero hP + rw [cslotAddr_nil] at h + rw [e]; exact h + obtain ⟨ps, idx, hx, hps, y, r, gs, g, hy, hyc⟩ := hN 0 (by omega) hne + have hAp : AbsSlot Z.th (ps.bv, idx) := ⟨ps, rfl, Or.inl hps⟩ + have hsl := cslotAddr_inj hP hAp hAn (by rw [← hx, e, cslotAddr_nil]) + have hpsb : ps.bv = (nodeA (L.ksb s) irq).bv := congrArg Prod.fst hsl + obtain rfl : ps = nodeA (L.ksb s) irq := P1.eq_of_bv hpsb + obtain ⟨sl0, hA0, rfl⟩ := hy.abs + obtain ⟨c0, ac0, hc0, hca0, hg0⟩ := J.xtop.rel.2.1.1.abs_cap hA0 + rw [capAt_capV, hyc] at hc0; cases eq_of_ok_eq hc0 + rw [hca0.abs] at hg0 + have ha0 := (getCap_set_iff σ sl0.1 sl0.2 _).1 (congrArg Prod.fst hg0) + apply hsg.ref sl0.1 sl0.2 _ ha0 (by simp [IsPageCap, absCap]) (BitVec.ofNat 8 irq.val) + rw [J.xtop.fin.1.1.2.2.2.1.node irq hirq] + simp [absCap, Fl4Spec.objRefs] + +/-- The IRQ node's `delete_one` from the restarted caller, with the VM +state kept. -/ +theorem VmEptState.irqNodeDelS3 [P1.PlainStore st] [VxE Z] {s2 : S} {σ1 : Fl4Spec.State Unit} {kb irq : U64} + {Lst2 : U64 → List U64} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst2 D s2 a σ1) + (hW : WVE (L := L) arI AR Z H Cs kb s2 a σ1 Z.G) (hirq : irq.val < 128) (hkb : L.ksb s2 = kb) + (hok : capV (L.mem s2) (nodeA kb irq) = .Null ∨ ∃ p b w r, capV (L.mem s2) (nodeA kb irq) = .Notification p b w r) : + cdt.delete_one st arI s2 a (nodeA kb irq) ⦃ r => + ∃ σ' G' V' X' XL' Vv Ac' Lst' D', ((), σ') ∈ (Fl4Spec.capDeleteOne (Z := Unit) ((nodeA kb irq).bv, []) σ1).1 ∧ + DState (L := L) Z.th r.1 σ' G' ∧ capV (L.mem r.1) (nodeA kb irq) = .Null ∧ + (∀ y, IsSlot Z.th y → y ≠ nodeA kb irq → capV (L.mem r.1) y = capV (L.mem s2) y) ∧ + MdbStep (L.mem s2) Z.G (L.mem r.1) G' ∧ SpecGlob σ' ∧ L.ksb r.1 = kb ∧ + VmEptState (L := L) arI V' X' XL' AR (Z.mv G' Vv) H Cs Ac' Lst' D' r.1 r.2 σ' ⦄ := by + have B := J.1.1 + have hA : AbsSlot Z.th ((nodeA kb irq).bv, []) := + ⟨_, rfl, Or.inl (by simpa using hkb ▸ B.intL.node irq hirq)⟩ + obtain ⟨V2, X2, XL2, Vv, Ac3, Lst3, D3, J2, hk2⟩ := hW + rcases hok with hn | ⟨p, b, w, r, hk⟩ + · have hc : capAt (L.mem s2) (nodeA kb irq) = ok .Null := by rw [capAt_capV, hn] + have e1 : cdt.delete_one st arI s2 a (nodeA kb irq) = ok (s2, a) := by + unfold cdt.delete_one + rw [slot_cap_eq, hc] + simp only [bind_ok, cap_is_null_eq, isNullB, ↓reduceIte] + obtain ⟨c, ac, hc', hca, hg⟩ := B.xtop.rel.2.1.1.abs_cap hA + rw [cslotAddr_nil, hc] at hc'; cases eq_of_ok_eq hc' + rw [hca.abs] at hg + have e2 : Fl4Spec.capDeleteOne (Z := Unit) ((nodeA kb irq).bv, []) σ1 = ({((), σ1)}, False) := by + unfold Fl4Spec.capDeleteOne + rw [nd_bind_one hg] + simp [absCap, Fl4Spec.NonDet.unless, Fl4Spec.NonDet.pure] + rw [e1, WP.spec_ok] + have hDS : DState (L := L) Z.th s2 σ1 Z.G := ⟨B.xtop.rel.2.1, B.del, B.xtop.rel.2.2.1⟩ + refine ⟨σ1, Z.G, V2, X2, XL2, Vv, Ac3, Lst3, D3, by rw [e2]; rfl, hDS, hn, fun _ _ _ => rfl, + MdbStep.refl B.xtop.rel.2.1.2.2.1, J2.sg, hkb, ?_⟩ + exact J2.st + · have h := J2.deleteOneV arI hk2 hA (p := p) (b := b) (w := w) (r := r) (by rw [cslotAddr_nil]; exact hk) + rw [cslotAddr_nil] at h + refine WP.spec_mono h ?_ + rintro res ⟨σ', G', hm, hDS', hn', hsl, hmS, V', X', XL', Vv', Ac', Lst', D', J', hk'⟩ + exact ⟨σ', G', V', X', XL', Vv', Ac', Lst', D', hm, hDS', hn', hsl, hmS, J'.sg, hk', J'.st⟩ + + +set_option maxHeartbeats 4000000 in +/-- The caller's restart on the selected IRQ-handler route. -/ +theorem VmEptState.irqRestartS3 [P1.PlainStore st] [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hM : MachPlatC arI MR) (hsg : SpecGlob σ) (hmv : MRV MR a σ) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d prio, σ.curThread ∉ σ.readyQueues d prio) + {ct : U64} (hct : sched.cur_thread st s = ok ct) : + ∃ s2 σ1 Lst1, sched.set_thread_state st s ct sched.TS_RESTART = ok s2 ∧ + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst1 D s2 a σ1 ∧ + SlotsSame Z.th (L.mem s) (L.mem s2) ∧ L.ksb s2 = L.ksb s ∧ + WVE (L := L) arI AR Z H Cs (L.ksb s) s2 a σ1 Z.G ∧ + σ1.interruptIrqNode = σ.interruptIrqNode := by + have B := J.1.1 + have hct' : ct = L.curW s := (eq_of_ok_eq ((cur_thread_eq (L := L) s).symm.trans hct)).symm + obtain ⟨s2, σ2, Lst1, x, hcur, hk, hs2, hst, J1, he, hSS, hks, -⟩ := J.restartK hni + rw [← hct'] at hs2 + obtain ⟨K, -, -, hDE⟩ := B.bound.delE B.del B.xtop.rel.2.2.1.kt B.finM + have hE := J.restartEVE arI hM hni hsg (B.ppr0_start arI hni hRd0 hnq) hmv hDE hct s2 σ2 hs2 hSS hst + have hnode : σ2.interruptIrqNode = σ.interruptIrqNode := by + have h := hst + rw [hcur] at h + exact (setThreadState_keeps hk h).1 + exact ⟨s2, σ2, Lst1, hs2, hst, J1, hSS, hks, hE.2.1, hnode⟩ + +set_option maxHeartbeats 8000000 in +/-- **The selected IRQ-handler cap.** [J] `SpecGlob` at dispatch; the extra +caps are named by a CNode cap. -/ +theorem VmEptState.selectedIrqHandlerS3 [P1.PlainStore st] [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hM : MachPlatC arI MR) + (hsg : SpecGlob σ) (hmv : MRV MR a σ) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d prio, σ.curThread ∉ σ.readyQueues d prio) + {ct cptr len buf lbl irq : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.IrqHandler irq) ex args exs) + (hN : ExtraNamed Z.th (L.mem s) ex) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.IrqHandler irq) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.IrqHandler irq) exs buffer) s res ⦄ := by + have B := J.1.1 + have hX := B.xtop + have hP : Placed Z.th := hX.rel.1 + obtain ⟨hR, hV, hMl, hC, hI⟩ := hX.rel.2.1 + have hct := q.current + obtain ⟨hcur, -⟩ := B.curEq hct + have hirq : irq.val < 128 := (slot_cap_wf hX q.absSlot (capV_of_capAt q.capAt)).1 + obtain ⟨slbl, hsl, hd⟩ := irqH_canon lbl + have hspec : specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.IrqHandler irq) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeIRQHandler + (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs)) := by + rw [← hd] + rfl + rw [syscall.decode_invocation, hct, bind_ok, hspec] + have hok : IrqSlotOK (L.mem s) (nodeA (L.ksb s) irq) := + irqSlotOK_of_spec B.intL hR hX.fin.1.1.2.2.2.1 hsg.irqNode hirq + have hIR := hX.fin.1.1.2.2.2.1 + have hA : AbsSlot Z.th ((nodeA (L.ksb s) irq).bv, []) := + ⟨_, rfl, Or.inl (by simpa using B.intL.node irq hirq)⟩ + have hns : IsSlot Z.th (nodeA (L.ksb s) irq) := by have := hA.isSlot; rwa [cslotAddr_nil] at this + have htcb : Z.th ct = some .tcb := (hX.curTcb hcur).1 + have hmask : ∀ (a : A) (irq : U64) (b : Bool), ∃ a', arI.mask_irq a irq b = ok a' := fun a i m => by + obtain ⟨a', h, -⟩ := spec_exists (hAT.mask_irq a i m); exact ⟨a', h⟩ + by_cases h34 : lbl.val = 34 + · have e : lbl = fl4_abi.label.IRQAckIRQ := UScalar.eq_of_val_eq (by rw [h34]; simp [fl4_abi.label.IRQAckIRQ]) + subst e + have hg : Fl4Spec.genInvocationType slbl = .irqAckIRQ := by rw [hsl]; simp [irqHLabelOf, h34] + obtain ⟨s2, σ1, Lst1, hs2, hst, J1, hSS, hks, -, hnode⟩ := + J.irqRestartS3 hni hM hsg hmv hRd0 hnq hct + obtain ⟨a', ha'⟩ := hmask a irq false + unfold interrupt.decode_irq_handler + rw [hct]; simp only [bind_ok, ↓reduceIte, hs2, ha', WP.spec_ok] + let ms' : Fl4Spec.MachineState := { σ1.machineState with + irqMasks := Function.update σ1.machineState.irqMasks (BitVec.ofNat 8 irq.val) false } + let τ : Fl4Spec.State Unit := { σ1 with machineState := ms' } + have X1 := J1.1.1.xtop + have hAR' : AR a' σ1.archState.x64CurrentCr3 := + hAM a _ a' X1.fin.1.1.2.2.2.2.2.2 (Or.inr (Or.inl ⟨irq, false, ha'⟩)) + have J2 := J1.archV hAR' + have X2 := J2.1.1.xtop + have hO : KsStep σ1 τ := by constructor <;> rfl + have hIR' : IrqRel (L.mem s2) (L.ksb s2) τ := + ⟨fun i hi => X2.fin.1.1.2.2.2.1.st i hi, X2.fin.1.1.2.2.2.1.node⟩ + have hX' : XTopAt (L := L) AR Z s2 a' τ := + XTopAt.ksStep X2 J2.1.1.ks (P1.WritesIn.refl _ _) hO hIR' X2.fin.1.1.2.2.2.2.1 X2.rel.2.2.2.1 hAR' + obtain ⟨_, _, B'⟩ := J2.1.1.ofPostX (SlotsSame.refl _ _) (P1.WritesIn.refl _ _) hO hX' + have hw : P1.WritesIn st s2 s2 (fun _ => False) := P1.WritesIn.refl _ _ + have J3 := J2.ofB B' hw (fun _ h => h.elim) (fun _ h => h.elim) + (J2.1.1.vkeep_of hw fun _ h => h.elim) rfl rfl + refine Or.inr ⟨_, DecOut.of_eq (run_liftME_ok (run_liftME_ok + (show (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs).run σ = + ({(.ok (.aCKIrq (BitVec.ofNat 8 irq.val)), σ)}, False) by unfold Fl4Spec.decodeIrqHandlerInvocation; rw [hg]; rfl))), σ1, hst, (.ok [], τ), ?_, + Or.inl ⟨rfl, rfl, J3.anyX arI⟩⟩ + refine mem_bindE_ok (mem_liftE (x := ((), τ)) ?_) (Set.mem_singleton _) + show _ ∈ (Fl4Spec.doMachineOp _ σ1).1 + unfold Fl4Spec.doMachineOp Fl4Spec.maskInterrupt + refine mem_bind_nd (x := (σ1.machineState, σ1)) rfl ?_ + refine mem_bind_nd (x := (((), ms'), σ1)) ⟨rfl, rfl⟩ ?_ + refine mem_bind_nd (x := ((), τ)) (Set.mem_singleton _) ?_ + rfl + by_cases h36 : lbl.val = 36 + · have e : lbl = fl4_abi.label.IRQClearIRQHandler := + UScalar.eq_of_val_eq (by rw [h36]; simp [fl4_abi.label.IRQClearIRQHandler]) + subst e + have hg : Fl4Spec.genInvocationType slbl = .irqClearIRQHandler := by rw [hsl]; simp [irqHLabelOf, h36] + obtain ⟨s2, σ1, Lst1, hs2, hst, J1, hSS, hks, hW, hnode⟩ := + J.irqRestartS3 hni hM hsg hmv hRd0 hnq hct + unfold interrupt.decode_irq_handler + have n1 : ¬ fl4_abi.label.IRQClearIRQHandler = fl4_abi.label.IRQAckIRQ := by rw [u64_eq_iff]; simp + have n2 : ¬ fl4_abi.label.IRQClearIRQHandler = fl4_abi.label.IRQSetIRQHandler := by rw [u64_eq_iff]; simp + rw [hct]; simp only [bind_ok, n1, n2, ↓reduceIte, hs2, irq_slot_eq, hks] + have hok2 : capV (L.mem s2) (nodeA (L.ksb s) irq) = .Null ∨ + ∃ p b w r, capV (L.mem s2) (nodeA (L.ksb s) irq) = .Notification p b w r := by + rw [hSS.capV hns]; exact hok + refine WP.spec_bind (J1.irqNodeDelS3 hW hirq hks hok2) ?_ + rintro ⟨s3, a3⟩ ⟨σ', G', V', X', XL', Vv, Ac', Lst', D', hdel, -, -, -, -, -, -, J3⟩ + rw [uncurry_apply_pair, WP.spec_ok] + refine Or.inr ⟨_, DecOut.of_eq (run_liftME_ok (run_liftME_ok + (show (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs).run σ = + ({(.ok (.clearIRQHandler (BitVec.ofNat 8 irq.val)), σ)}, False) by unfold Fl4Spec.decodeIrqHandlerInvocation; rw [hg]; rfl))), σ1, hst, (.ok [], σ'), ?_, + Or.inl ⟨rfl, rfl, J3.anyX arI⟩⟩ + refine mem_bindE_ok (mem_liftE (x := ((), σ')) ?_) (Set.mem_singleton _) + show _ ∈ ((Fl4Spec.getIrqSlot _ >>= fun sl => Fl4Spec.capDeleteOne sl) σ1).1 + refine mem_bind_nd (x := ((σ1.interruptIrqNode (BitVec.ofNat 8 irq.val), []), σ1)) rfl ?_ + rw [hnode, hIR.node irq hirq] + exact hdel + by_cases h35 : lbl.val = 35 + swap + · have hg : Fl4Spec.genInvocationType slbl = .invalidInvocation := by rw [hsl]; simp [irqHLabelOf, h34, h35, h36] + refine WP.spec_mono (irq_other_refines arI Z.th s a σ (G := Z.G) lbl h34 h35 h36 slbl (by rw [hg]; decide) + ct hct irq ex exs) ?_ + rintro ⟨r0, s', a'⟩ hQ + cases r0 with + | Err e => + obtain ⟨rfl, ⟨e', h1, h2⟩, rfl⟩ := hQ + exact Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err h1), h2⟩, J.anyX arI⟩ + | Ok u => + obtain ⟨inv, hrun, -⟩ := hQ + have hbad : (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs).run σ = + ({(.error .illegalOperation, σ)}, False) := by + unfold Fl4Spec.decodeIrqHandlerInvocation + rw [hg]; rfl + rw [hbad] at hrun + have := (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (congrArg Prod.fst hrun))).1 + cases this + have e : lbl = fl4_abi.label.IRQSetIRQHandler := + UScalar.eq_of_val_eq (by rw [h35]; simp [fl4_abi.label.IRQSetIRQHandler]) + subst e + have hg : Fl4Spec.genInvocationType slbl = .irqSetIRQHandler := by rw [hsl]; simp [irqHLabelOf, h35] + have hdec : Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs = + (if exs ≠ [] then + (let (cap, slot) := Fl4Spec.the exs.head? + if Fl4Spec.isNtfnCap cap ∧ Fl4Spec.AllowSend ∈ Fl4Spec.capRights cap + then Fl4Spec.NonDetE.returnOk (.setIRQHandler (BitVec.ofNat 8 irq.val) cap slot) + else Fl4Spec.NonDetE.throwError (.invalidCapability 0)) + else Fl4Spec.NonDetE.throwError .truncatedMessage) := by + unfold Fl4Spec.decodeIrqHandlerInvocation; rw [hg]; rfl + have hex : ExtraSRel Z.th (L.mem s) (exAt ex 0) exs := ExtraOK.relS hP q.extra + have hxn : exAt ex 0 ≠ nodeA (L.ksb s) irq := B.irqExtraNe hsg hN hirq + have hx0 := exAt_idx0 ex + generalize exAt ex 0 = x0 at hex hxn hx0 + have herr : ∀ (e : error.Exc) (e' : Fl4Spec.SyscallError), ErrRel e e' → + (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs).run σ = + ({(.error e', σ)}, False) → + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeIRQHandler + (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) exs))) s (.Err e, s, a) := + fun e e' h h1 => Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err h1), h⟩, J.anyX arI⟩ + unfold interrupt.decode_irq_handler + have n1 : ¬ fl4_abi.label.IRQSetIRQHandler = fl4_abi.label.IRQAckIRQ := by rw [u64_eq_iff]; simp + rw [hct]; simp only [bind_ok, n1, ↓reduceIte, hx0, is_null_eq] + rcases hex with ⟨rfl, rfl⟩ | ⟨hx0n, ps, rs, idxs, rest, hps, hls, hxe, rfl⟩ + · simp only [decide_true, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + refine herr _ .truncatedMessage (Or.inl trivial) ?_ + rw [hdec]; simp only [ne_eq, not_true_eq_false, ↓reduceIte]; rfl + simp only [hx0n, decide_false, Bool.false_eq_true, ↓reduceIte, slot_cap_eq, capAt_capV, bind_ok] + have hdec2 : Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) + ((absCap (capV (L.mem s) x0), (ps.bv, idxs)) :: rest) = + if Fl4Spec.isNtfnCap (absCap (capV (L.mem s) x0)) ∧ + Fl4Spec.AllowSend ∈ Fl4Spec.capRights (absCap (capV (L.mem s) x0)) + then Fl4Spec.NonDetE.returnOk (.setIRQHandler (BitVec.ofNat 8 irq.val) (absCap (capV (L.mem s) x0)) (ps.bv, idxs)) + else Fl4Spec.NonDetE.throwError (.invalidCapability 0) := by + rw [hdec, if_pos (List.cons_ne_nil _ _)]; rfl + generalize hcx : capV (L.mem s) x0 = c at hdec2 ⊢ + by_cases hgood : ∃ p b r, c = .Notification p b true r + swap + · have hbad : ¬ (Fl4Spec.isNtfnCap (absCap c) = true ∧ Fl4Spec.AllowSend ∈ Fl4Spec.capRights (absCap c)) := by + rintro ⟨h1, h2⟩ + apply hgood + cases c <;> simp [absCap, Fl4Spec.isNtfnCap] at h1 + rename_i p b w r + refine ⟨p, b, r, ?_⟩ + have : w = true := h2 + rw [this] + rw [if_neg hbad] at hdec2 + have hfin : ∀ e : core.result.Result Unit error.Exc, e = .Err (.Sys (.InvalidCapability 0#u64)) → + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeIRQHandler + (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) + ((absCap c, (ps.bv, idxs)) :: rest)))) s (e, s, a) := by + rintro e rfl + exact Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, run_liftME_err (run_liftME_err (by rw [hdec2]; rfl)), + Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + cases c <;> simp only [error.invalid_cap, bind_ok, WP.spec_ok] <;> + first + | exact hfin _ rfl + | (rename_i p b w r + cases w + · simp only [Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok, WP.spec_ok] + exact hfin _ rfl + · exact absurd ⟨p, b, r, rfl⟩ hgood) + obtain ⟨p, b, r, rfl⟩ := hgood + have h12 : Fl4Spec.isNtfnCap (absCap (.Notification p b true r)) = true ∧ + Fl4Spec.AllowSend ∈ Fl4Spec.capRights (absCap (.Notification p b true r)) := + ⟨rfl, show (true = true) from rfl⟩ + rw [if_pos h12] at hdec2 + simp only [↓reduceIte] + obtain ⟨s2, σ1, Lst1, hs2, hst, J1, hS, hks, hW, hnode⟩ := + J.irqRestartS3 hni hM hsg hmv hRd0 hnq hct + simp only [hs2, bind_ok, irq_slot_eq, hks] + have hAs : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hok2 : capV (L.mem s2) (nodeA (L.ksb s) irq) = .Null ∨ + ∃ p b w r, capV (L.mem s2) (nodeA (L.ksb s) irq) = .Notification p b w r := by + rw [hS.capV hns]; exact hok + refine WP.spec_bind (J1.irqNodeDelS3 hW hirq hks hok2) ?_ + rintro ⟨s3, a3⟩ ⟨σ2, G2, V', X', XL', Vv, Ac', Lst', D', hdel, hDS3, hnull, hoth, hmS3, hsg3, hks3, J3⟩ + simp only [uncurry_apply_pair] + obtain ⟨⟨hR3, hV3, hM3, hC3, hI3⟩, hD3, hFI3⟩ := hDS3 + have hxs : IsSlot Z.th x0 := by rw [hxe]; exact hAs.isSlot + have hsrc3 : capAt (L.mem s3) (cslotAddr (ps.bv, idxs)) = ok (.Notification p b true r) := by + rw [capAt_capV, ← hxe, hoth x0 hxs hxn, hS.capV hxs, hcx] + have hdst3 : capAt (L.mem s3) (cslotAddr ((nodeA (L.ksb s) irq).bv, [])) = .ok .Null := by + rw [capAt_capV, cslotAddr_nil, hnull] + have he : EncWf (capability.Cap.Notification p b true r) := by + have := capAt_capV (L.mem s) x0 + rw [hcx, capAt_words] at this; exact decode_encWf this + have hins := insert_full_fin Z.th s3 σ2 G2 hR3 hV3 hP hM3 hC3 hI3 hFI3 hps (B.intL.node irq hirq) hls (rd := 0) + (idxd := []) rfl hsrc3 (by simp) hdst3 (.Notification p b true r) _ ⟨trivial, rfl⟩ (hV3.abs hAs hsrc3) he + (by simp) rfl (Or.inl rfl) (by simp [isUt]) (by simp [isZb]) rfl (fun _ _ _ _ _ _ => trivial) + rw [cslotAddr_nil, ← hxe] at hins + have hsrcx : capAt (L.mem s3) x0 = ok (.Notification p b true r) := by rw [hxe]; exact hsrc3 + have hsrcm : x0 ∈ G2.chains.flatten := (hM3.members _).2 ⟨hxs, _, hsrcx, by simp⟩ + have hdestm : nodeA (L.ksb s) irq ∉ G2.chains.flatten := by + intro h; obtain ⟨-, c0, h0, hn⟩ := (hM3.members _).1 h + rw [capAt_capV, hnull] at h0 + exact hn (eq_of_ok_eq h0).symm + have hnx : nextOf (L.mem s3) x0 = 0#u64 ∨ IsSlot Z.th (nextOf (L.mem s3) x0) := by + rcases hM3.next_mem hsrcm with h | ⟨h, -⟩ + · exact Or.inl h + · exact Or.inr ((hM3.members _).1 h).1 + refine WP.spec_bind (WP.spec_and (WP.spec_and (WP.spec_and hins (insert_caps_nofull hP s3 G2 hM3 hsrcm hns hdestm + hsrcx (.Notification p b true r) he rfl)) (Vs.insert_frame2 hP s3 (.Notification p b true r) x0 _ hxs hns _ + hsrcx hnx)) (insert_delinv hP s3 G2 hM3 hD3 hsrcm hns hdestm hsrcx (.Notification p b true r) he rfl + (by simp) rfl)) ?_ + rintro s4 ⟨⟨⟨⟨σ', G', hci, hR', hV', hM', hC', hI', hFI', hSti⟩, hcap4⟩, hF4⟩, hD4⟩ + rw [WP.spec_ok] + have hmst : MdbStep (L.mem s3) G2 (L.mem s4) G' := hSti (fun h => absurd h (by simp [isUt])) + have B3 := J3.1.1 + have hCap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr ((nodeA (L.ksb s) irq).bv, []) → + capV (L.mem s4) y = capV (L.mem s3) y := fun y hy hne => by + rw [hcap4 y hy, if_neg (by rwa [cslotAddr_nil] at hne)] + have hd4 : capV (L.mem s4) (cslotAddr ((nodeA (L.ksb s) irq).bv, [])) = .Notification p b true r := by + rw [cslotAddr_nil, hcap4 _ hns, if_pos rfl] + have hVT := Vs.VsSlotsT.of_some (· = cslotAddr ((nodeA (L.ksb s) irq).bv, [])) hCap + (fun y _ e => by rw [e, hd4]; intros; trivial) (fun y _ e => by rw [e, hd4]; intros; trivial) + (fun y _ e => by rw [e, hd4]; intros; trivial) (fun y _ e => by rw [e, hd4]; intros; trivial) + have hVK := Vs.VsKeep.of_nokey (W := (· = cslotAddr ((nodeA (L.ksb s) irq).bv, []))) hCap + (fun _ _ e => by rw [e, capV_of_capAt hdst3]; rfl) fun _ _ e => by rw [e, hd4]; rfl + have hw : P1.WritesIn st s3 s4 (InSlot Z.th) := by + refine ⟨hF4.1, hF4.2.1, fun x hx hn => hF4.2.2 x hx fun ⟨y, k, hy, hk, e⟩ => hn ⟨y, hy, ?_⟩⟩ + have hf := hy.fit hP + have := hk.lt + have hv := wordOf_val (a := y) (k := k) (by omega) + rw [e, hv] + omega + have hZ3 := B3.zNoBlk hsg3 + have hMO : MdbOKV (L.ksb s3) (L.mem s4) G' := hmst.okv B3.xtop.rel.2.2.2.2.1 + have hDL : DestLive Z.th (L.mem s3) (L.ksb s3) (nodeA (L.ksb s) irq) := + Or.inr fun k hk => by + rw [B.intL.node irq hirq] at hk; cases hk + rw [hks3, nodeA_val B.intL.fit hirq]; simp only [objSize]; omega + obtain ⟨_, _, J4⟩ := VmEptState.insertIssuedS3 arI (Z := Z.mv G2 Vv) J3 hZ3 hps (B.intL.node irq hirq) + (rd := 0) (idxd := []) rfl hci hCap hd4 hdst3 (by rintro _ _ _ _ h; cases h) (by intro h; cases h) + (by rintro _ _ _ h; cases h) hw ⟨hR', hV', hM', hC', hI'⟩ hFI' hMO + (typedKept_set hcap4 (Or.inr ⟨x0, hxs, by rw [capV_of_capAt hsrcx]⟩)) hDL (fun _ => hD4) hmst hVT hVK rfl + refine Or.inr ⟨_, DecOut.of_eq (run_liftME_ok (run_liftME_ok + (show (Fl4Spec.decodeIrqHandlerInvocation (Z := Unit) slbl (BitVec.ofNat 8 irq.val) + ((absCap (.Notification p b true r), (ps.bv, idxs)) :: rest)).run σ = + ({(.ok (.setIRQHandler (BitVec.ofNat 8 irq.val) (absCap (.Notification p b true r)) (ps.bv, idxs)), σ)}, False) + by rw [hdec2]; rfl))), σ1, hst, (.ok [], σ'), ?_, Or.inl ⟨rfl, rfl, J4.anyX arI⟩⟩ + refine mem_bindE_ok (mem_liftE (x := ((), σ')) ?_) (Set.mem_singleton _) + show _ ∈ ((Fl4Spec.getIrqSlot _ >>= fun irqSlot => Fl4Spec.capDeleteOne irqSlot >>= fun _ => + Fl4Spec.capInsert _ _ irqSlot) σ1).1 + refine mem_bind_nd (x := ((σ1.interruptIrqNode (BitVec.ofNat 8 irq.val), []), σ1)) rfl ?_ + rw [hnode, hIR.node irq hirq] + refine mem_bind_nd (x := ((), σ2)) hdel ?_ + rw [hci]; rfl + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3IrqCtl.lean b/proofs/Fl4Proofs/P3/VmEntryS3IrqCtl.lean new file mode 100644 index 000000000..bf0c4551d --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3IrqCtl.lean @@ -0,0 +1,206 @@ +import Fl4Proofs.P3.VmEntryS3IoControl +import Fl4Proofs.P3.IrqCtlTrace +import Fl4Proofs.P3.XTopJointKEntry36E + +/-! +Selected IRQ control cap (every label). The decoder trace (`IrqIssueAt`, P3/IrqCtlTrace) records the +restart, the arch step, the spec-only kernel-state step, the IRQ state word with its mask and the issued +handler cap's insert; each is framed at the VM/EPT state in turn (`VmEptState.irqIssueTraceS3`). +[J] `SpecGlob` and l4v `valid_irq_handlers` (`SInvV.ValidIrqHandlers`, giving `HandlersActive`) at +dispatch. [P] `ArchIrqRel` (the IOAPIC count, pins and programming, as the native entry's `archIrq`). +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched Fl4Proofs.P1 + +noncomputable section +section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- A spec-only kernel-state step with an arch IRQ step keeps the VM/EPT state. -/ +theorem VmEptState.ksSpecS3 (hAM : ARMask arI AR) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {a1 : A} {σb : Fl4Spec.State Unit} + (ha : ArchIrqStep arI a a1) (hO : KsStep σ σb) (hdl : σb.domainList = σ.domainList) + (hds : σb.domainStartIndex = σ.domainStartIndex) + (hIrb : ∀ m kb, IrqRel m kb σ → IrqRel m kb σb) (hIob : ∀ m kb, IoRel m kb σ → IoRel m kb σb) : + ∃ Ac' Lst', VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D s a1 σb := by + have X0 := J.1.1.xtop + have hAR' : AR a1 σ.archState.x64CurrentCr3 := hAM a _ a1 X0.fin.1.1.2.2.2.2.2.2 ha + have J1 := J.archV hAR' + have X1 := J1.1.1.xtop + have hX' : XTopAt (L := L) AR Z s a1 σb := + XTopAt.ksStep X1 J1.1.1.ks (P1.WritesIn.refl _ _) hO (hIrb _ _ X1.fin.1.1.2.2.2.1) + (hIob _ _ X1.fin.1.1.2.2.2.2.1) (X1.rel.2.2.2.1.frame (fun _ _ => rfl) hdl hds) + (by rw [hO.cr3]; exact hAR') + obtain ⟨Ac', Lst', B'⟩ := J1.1.1.ofPostX (SlotsSame.refl _ _) (P1.WritesIn.refl _ _) hO hX' + have hw : P1.WritesIn st s s (fun _ => False) := P1.WritesIn.refl _ _ + exact ⟨Ac', Lst', J1.ofB B' hw (fun _ h => h.elim) (fun _ h => h.elim) + (J1.1.1.vkeep_of hw fun _ h => h.elim) hO.kh hO.asidT⟩ + +set_option maxHeartbeats 4000000 in +/-- The IRQ's state word written and its line masked keep the VM/EPT state. -/ +theorem VmEptState.irqWrS3 (hAM : ARMask arI AR) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {irq : U64} (hirq : irq.val < 128) + {b : Bool} {a' : A} (hm : arI.mask_irq a irq b = ok a') : + ∃ Ac' Lst', VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D + (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL) a' (setIrqSt .irqSignal (BitVec.ofNat 8 irq.val) σ) := by + have B := J.1.1 + have hX := B.xtop + have hL := B.intL + have hv := irqStA_val hL.fit hirq + have ha8 : (L.ksb s).val % 8 = 0 := by have := hL.align; omega + have hw1 : P1.WritesIn st s (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL) + (fun b => b = irqStA (L.ksb s) irq) := + P1.writesIn_wr (by unfold P1.Aligned; rw [hv]; omega) + have hw : P1.WritesIn st s (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL) (KsW (L.ksb s)) := by + refine hw1.mono fun b e => ?_ + subst e + have hK : (⟨BitVec.ofNat 64 (224 + 8 * irq.val)⟩ : U64).val = 224 + 8 * irq.val := by + show (BitVec.ofNat 64 _).toNat = _; simp; omega + exact ⟨⟨BitVec.ofNat 64 (224 + 8 * irq.val)⟩, by rw [hK]; omega, by rw [hK]; omega, by rw [hK]; omega, + by rw [hK, hv]; omega, by rw [hK]; omega⟩ + have hsig : irqSt interrupt.IRQ_SIGNAL = .irqSignal := by simp [irqSt, interrupt.IRQ_SIGNAL] + have hO : KsStep σ (setIrqSt .irqSignal (BitVec.ofNat 8 irq.val) σ) := by constructor <;> rfl + have hX' : XTopAt (L := L) AR Z (L.wr s (irqStA (L.ksb s) irq) interrupt.IRQ_SIGNAL) a' + (setIrqSt .irqSignal (BitVec.ofNat 8 irq.val) σ) := + XTopAt.ksStep hX B.ks hw hO + (IrqRel.wr hL hX.fin.1.1.2.2.2.1 hirq _ _ (by rw [hsig]; rfl) rfl) + (IoRel.irqWr hL hX.fin.1.1.2.2.2.2.1 hirq _ _ rfl) + (hX.rel.2.2.2.1.frame (fun o ho => (DK4.irqSt hL.fit ha8 hirq interrupt.IRQ_SIGNAL).2 o ho) rfl rfl) + (hAM _ _ _ hX.fin.1.1.2.2.2.2.2.2 (Or.inr (Or.inl ⟨irq, b, hm⟩))) + obtain ⟨Ac', Lst', B'⟩ := B.ofPostX (IntLayout.slotsSame (L := L) hX.rel.1 hL s hirq _) hw hO hX' + have hh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val B.kern.ksbFit + exact ⟨Ac', Lst', J.ofB B' hw1 + (fun b hb => by subst hb; exact Or.inr (Or.inl ⟨by omega, by omega, Or.inl (by omega)⟩)) + (fun b hb e => by subst hb; rw [e] at hv; omega) (B.vkeep_of hw fun _ hb => B.ksW_nv hb) hO.kh rfl⟩ + +set_option maxHeartbeats 6000000 in +/-- A successful IRQ control invocation's trace keeps the VM/EPT state. -/ +theorem VmEptState.irqIssueTraceS3 (hAM : ARMask arI AR) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {ct0 : U64} (hct : sched.cur_thread st s = ok ct0) {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} {G' : MdbGhost} + (h : IrqIssueAt (L := L) arI Z.th Z.G s a σ s' a' σ' G') + (hCR : CRel Z.th (L.mem s') σ' G') (hFI : FinInv Z.th (L.mem s') σ' G') + (hMO : MdbOKV (L.ksb s) (L.mem s') G') (hsg : SpecGlob σ) : + VEAnyX (L := L) arI AR s' a' σ' := by + obtain ⟨ct, s1, σ1, a1, σb, hcur, hR, hst, hA, hO, hdl, hds, hIrb, hIob, + irq, ps, pd, rs, rd, idxs, idxd, b, hirq, hm, hps, hpd, hld, hci, hcap, hnull, hF, hDL, hDl, hMS⟩ := h + have B := J.1.1 + obtain ⟨hcur0, -⟩ := B.curEq hct + obtain rfl : ct = ct0 := P1.eq_of_bv (hcur.symm.trans hcur0) + obtain ⟨_, x, hk, hr⟩ := B.curRunnable hni hct + obtain ⟨sR, σR, hstR, _, hRR, _, _, _, BR, F⟩ := B.restartRunJ hct hk hr + have eS := ok_inj (hRR.symm.trans hR) + have eT := (Prod.mk.inj (Set.singleton_eq_singleton_iff.mp (congrArg Prod.fst (hstR.symm.trans hst)))).2 + subst sR + cases eT + have J1 := J.ofNat (V' := Z.vs) BR F + have hfZ1 := F.ss + obtain ⟨_, _, J2⟩ := J1.ksSpecS3 hAM hA hO hdl hds hIrb hIob + obtain ⟨_, _, J3⟩ := J2.irqWrS3 hAM hirq hm + have B3 := J3.1.1 + set s2 := L.wr s1 (irqStA (L.ksb s1) irq) interrupt.IRQ_SIGNAL with hs2 + have hSS2 : SlotsSame Z.th (L.mem s1) (L.mem s2) := IntLayout.slotsSame (L := L) BR.xtop.rel.1 BR.intL s1 hirq _ + have hk2 : L.ksb s2 = L.ksb s1 := by simp [s2] + have hZ2 := (B.zNoBlk hsg).of_same (hfZ1.trans hSS2) (hk2.trans F.fw.1) + have hS : IsSlot Z.th (cslotAddr (pd.bv, idxd)) := + (show AbsSlot Z.th (pd.bv, idxd) from ⟨pd, rfl, Or.inl (by rw [hld]; exact hpd)⟩).isSlot + have hCap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s2) y := fun y hy hne => by rw [hcap y hy, if_neg hne] + have hd : capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .IrqHandler irq := by + rw [hcap _ hS, if_pos rfl] + have hw : P1.WritesIn st s2 s' (InSlot Z.th) := by + refine ⟨hF.1, hF.2.1, fun b hb hn => hF.2.2 b hb fun ⟨x, k, hx, hk, e⟩ => hn ⟨x, hx, ?_⟩⟩ + have hf := hx.fit B3.xtop.rel.1 + have := hk.lt + have hv := wordOf_val (a := x) (k := k) (by omega) + rw [e, hv] + omega + have hTK := typedKept_set hcap (Or.inl rfl) + have hVT := Vs.VsSlotsT.of_some (· = cslotAddr (pd.bv, idxd)) hCap + (fun y _ e => by rw [e, hd]; intros; trivial) (fun y _ e => by rw [e, hd]; intros; trivial) + (fun y _ e => by rw [e, hd]; intros; trivial) (fun y _ e => by rw [e, hd]; intros; trivial) + have hVK := Vs.VsKeep.of_nokey (W := (· = cslotAddr (pd.bv, idxd))) hCap + (fun _ _ e => by rw [e, capV_of_capAt hnull]; rfl) fun _ _ e => by rw [e, hd]; rfl + have hMO2 : MdbOKV (L.ksb s2) (L.mem s') G' := by + rw [hk2, F.fw.1] + exact hMO + obtain ⟨_, _, J'⟩ := VmEptState.insertIssuedS3 arI J3 hZ2 hps hpd hld hci hCap hd hnull + (by rintro _ _ _ _ h; cases h) (by intro h; cases h) (by rintro _ _ _ h; cases h) + hw hCR hFI hMO2 hTK hDL hDl hMS hVT hVK rfl + exact J'.anyX arI + +set_option maxHeartbeats 8000000 in +/-- **The selected IRQ control cap, every label.** [J] `SpecGlob` and `valid_irq_handlers` at dispatch; +[P] `ArchIrqRel`. -/ +theorem VmEptState.selectedIrqControlS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAM : ARMask arI AR) (hsg : SpecGlob σ) (hVIH : SInvV.ValidIrqHandlers σ) + (hAR : ArchIrqRel arI a σ) + {ct cptr len buf lbl : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl .IrqControl ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) .IrqControl blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .IrqControl exs buffer) s res ⦄ := by + let words : List U64 := args.map fun w => ⟨w⟩ + have hw : words.map UScalar.bv = args := by + simp [words, List.map_map, Function.comp_def] + have hdec : specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .IrqControl exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeIRQControl + (Fl4Spec.decodeFl4IrqControl (decide (lbl.val = 72)) lbl.bv args sl (exs.map Prod.fst))) := by + rw [irqControl_remap lbl]; rfl + rw [syscall.decode_invocation, q.current, bind_ok, hdec] + have B := J.1.1 + have X0 := B.xtop + have hP := X0.rel.1 + have hCR := X0.rel.2.1 + have hFI := X0.rel.2.2.1 + have hFR := X0.rel.2.2.2.2.1 + have hIR := X0.fin.1.1.2.2.2.1 + obtain ⟨hcur, _, _⟩ := B.curEq q.current + have htcb := (X0.curTcb hcur).1 + have hHA : HandlersActive (L.mem s) (L.ksb s) Z.G := + handlersActive_of_vih X0.rel.2.1.1 X0.rel.2.1.2.2.1 hIR hVIH + have hact2 : ∀ i : U64, i.val < 128 → L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION ≠ irqStA (L.ksb s) i := by + intro i hi e + have ev := congrArg UScalar.val e + rw [Sched.nodeAt_val' B.kern.nodeFit (by rw [P1.act_val]; decide), P1.act_val, + irqStA_val B.kern.ksbFit hi] at ev + omega + obtain ⟨nA, hnum, hnA⟩ := hAR.num + obtain ⟨ps, hps, hpt⟩ := q.slot + obtain ⟨x, idxs⟩ := sl + dsimp only at hps hpt + subst x + have hi := irq_control_decode_refinesT arI Z.th hP s a σ Z.G hCR B.intL hIR hHA hFI hFR lbl lbl.bv + (irqControl_label lbl) len buf ct q.current words q.length q.words hcur htcb B.kern.act hact2 ex _ + (exAt_idx0 ex) (exs.map Prod.fst) (ExtraOK.rel1 hP q.extra) hpt rfl q.capAt nA hnum hnA hAR.mask hAR + rw [hw] at hi + refine WP.spec_mono hi ?_ + rintro ⟨res, s1, a1⟩ hQ + cases res with + | Err e => + obtain ⟨rfl, ⟨e', hd, hE⟩, rfl⟩ := hQ + exact Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err hd), hE⟩, J.anyX arI⟩ + | Ok u => + cases u + obtain ⟨i, hd, σ1, hs, σ', G', hp, hr, _, hf, _, hm, _, _, htr⟩ := hQ + exact Or.inr ⟨.base (.invokeIRQControl i), DecOut.of_eq (run_liftME_ok (run_liftME_ok hd)), σ1, hs, + (.ok [], σ'), mem_bindE_ok hp (Set.mem_singleton _), + Or.inl ⟨rfl, rfl, J.irqIssueTraceS3 hAM hni q.current htr hr hf hm hsg⟩⟩ + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Mid.lean b/proofs/Fl4Proofs/P3/VmEntryS3Mid.lean new file mode 100644 index 000000000..465bf6109 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Mid.lean @@ -0,0 +1,152 @@ +import Fl4Proofs.P3.P3JMidSpec +import Fl4Proofs.P3.VmMidProd +import Fl4Proofs.P3.VmRecvMid +import Fl4Proofs.P3.XTopJointMidRunS +import Fl4Proofs.P3.VmEntryS3Start + +/-! +P3 (line mmuAD, Stage 3, 3.5): the `VMidF` producer at the dispatch pair of the entry. + +* `JAtZB.midEntryB`, `midEntry_dispB`: the `JAtZB` siblings of `JAtZ.midEntry` / `midEntry_disp` (generated by + `handoff/tools/gen_s3mid.py`, same bodies: `JAtZB` has `curRun`, `obj`, `xtop`, `cur`). +* `StartS3.midProd`: every `ok` outcome of the first phase of ReplyRecv / NBSendRecv / NBSendWait from the dispatch + state is a `VMidF` state: `vmidF_of_handleInvocationVF` fed by the start record `SInvX` (`disp27b`, `disp_vfc`, + `jPerfRegHyp_27b`, `jPerfIrqSlotHyp_27b`), `JAtZB.cur` and `JAtZB.inv`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +open Fl4Proofs.P3.SInvR.G4 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`MidEntry` at a system call's entry**: `run` from `JAtZ.curRun` and +`Entered`, `nq` from the notification relation (a runnable thread is in no +waiting queue, `not_in_ntfnQueue`, the notification and the TCB in the +relation's sets by `ObjCover`); `nf` is `JEntryAt.cnf`'s [J] fact (l4v +`fault_tcbs_valid_states`), passed in. -/ +theorem JAtZB.midEntryB {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hnf : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none) : MidEntry σ := by + have hrun := J.curRun.run hni + refine ⟨hrun, hnf, fun p n q hp hq hm => ?_⟩ + obtain ⟨x, hk⟩ : ∃ x, σ.kheap σ.curThread = some (.tcb x) := by + rcases J.cur with h | h + · rw [hni.2] at h; cases h + · obtain ⟨xt, hk, -⟩ := h.tcb; exact ⟨xt, hk⟩ + obtain ⟨t, ht, et⟩ := J.obj.tcb _ _ hk + obtain ⟨n', hn', rfl⟩ := J.obj.ntfn _ _ hp + have hR : NtfnStateRel Z.N Z.T (L.mem s) σ := J.xtop.fin.1.1.2.2.1 + have hr := hrun x hk + rw [← et] at hk hm + exact not_in_ntfnQueue hR ht hk hr n' hn' n hp (by rw [hq]; exact hm) + +end + +end + +end Fl4Proofs.P3 + +namespace Fl4Proofs.P3.Mc +open fl4_core Classical Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P2.Wit +open Fl4Proofs.P3.JBoot Fl4Proofs.P3.SInvR.G5 Fl4Proofs.P3.Vs Fl4Proofs.P3.JBootQ +open Fl4Proofs.P3.SInvR.G4 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} + +set_option maxHeartbeats 4000000 in +/-- (p3J gen 49) **`CQ ∧ NQ` of the current thread at a dispatch state, from the entry state** (module +docstring). -/ +theorem midEntry_dispB (hnd : Fl4Spec.numDomains = 1) {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ σb : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hX : SInvXV σ) (hct : σ.curThread ≠ σ.idleThread) + (hBA : BudgetAt σ σb true) : + Entered σb ∧ MidRun.CQ σb.curThread σb ∧ MidRun.NQ σb.curThread σb := by + have hS := hX.sb + have hni : Entered σ := hX.entered hct + obtain ⟨x0, hk0, -⟩ := hS.1.2 + obtain ⟨-, ek, ect, -, -, eq0⟩ := BudgetAt.shape27 hnd hk0 hBA + have ek' : σb.kheap = Function.update σ.kheap σ.curThread + (some (.tcb (Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)))) := ek + obtain ⟨-, hnfb⟩ := SInvAll.sinvB_dispXV (x' := Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)) + hS hct hk0 rfl rfl ek' ect + have ek0 : σ.kheap = Function.update σ.kheap σ.curThread (some (.tcb x0)) := by + rw [← hk0]; exact (Function.update_eq_self _ _).symm + obtain ⟨-, hnf0⟩ := SInvAll.sinvB_dispXV (σb := σ) (x' := x0) hS hct hk0 rfl rfl ek0 rfl + have hME := J.midEntryB hni hnf0 + have hnib : Entered σb := BudgetAt.entered hnd hk0 hni hBA + have hxb : ∀ x, σb.kheap σb.curThread = some (.tcb x) → + x = Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2) := by + intro x hx + rw [ek', ect, Function.update_self] at hx + simp only [Option.some.injEq, Fl4Spec.KernelObject.tcb.injEq] at hx + exact hx.symm + refine ⟨hnib, ⟨⟨rfl, fun x hx => ⟨?_, hnfb x hx⟩⟩, ?_, ?_⟩, ?_⟩ + · obtain rfl := hxb x hx + exact hME.run x0 hk0 + · rw [ect, eq0]; exact hX.ctQ + · rw [hnib.2]; intro e; cases e + · intro p n q hp hq + by_cases e : p = σ.curThread + · subst e + rw [ek', Function.update_self] at hp + simp at hp + · rw [ek', Function.update_of_ne e] at hp + rw [ect] + exact hME.nq p n q hp hq + +end + +end + +end Fl4Proofs.P3.Mc + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Spec Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched +open Fl4Proofs.P3.SInvR.G4 Fl4Proofs.P3.SInvK + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +set_option maxHeartbeats 8000000 in +/-- **The `VMidF` producer at the dispatch pair** (module docstring). -/ +theorem StartS3.midProd (hnd : Fl4Spec.numDomains = 1) {on rc : Fl4Spec.ObjRef → Bool} {n : I64} + {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s s2 : S} {a a2 : A} {σ σb : Fl4Spec.State Unit} + {Zb : XSets} {Hb : P1.Heap} {Csb Acb : Set U64} {Lstb : U64 → List U64} {Db : Set U64} + (St : StartS3 (L := L) (st := st) (arI := arI) n s a σ) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (Jb : JAtZB (L := L) arI AR Zb Hb Csb Acb Lstb Db s2 a2 σb) (hBA : BudgetAt σ σb true) + (cptr : Fl4Spec.CapRef) : + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true false false true true cptr).run σb).1, + y.1 = .ok () → VMidF y.2 := by + have hS := St.X.sb + obtain ⟨x, hk, h0, hu, ek, ect⟩ := disp27bXV hnd hS hBA + obtain ⟨hnib, hcq, hnqb⟩ := Mc.midEntry_dispB hnd J St.X St.ct hBA + have hPQ : MidRun.JMidPerfHypQ true cptr σb := + MidRun.jMidPerfHypQ_of2 hcq hnqb (jPerfRegHyp_27bXV hnd hS St.ct hBA cptr) (jPerfIrqSlotHyp_27bXV hnd hS hBA cptr) + have hPB : MidRun.JMidPerfHypB true cptr σb := + MidRun.jMidPerfHypB_of2 hcq hnqb (jPerfRegHyp_27bXV hnd hS St.ct hBA cptr) (jPerfIrqSlotHyp_27bXV hnd hS hBA cptr) + have hC : Sched.CurInv σb := Jb.curInv (by rw [hnib.2]; intro e; cases e) + have hV : SInv.VfC σb := disp_vfc h0 hu hS.1.1.vfh hS.1.1.cnDom + have hU : SInvU.UtInv'' σb := SInv.utInv''_of_view (disp_uv h0 hu) hS.1.1.ut.1 + exact vmidF_of_handleInvocationVF on rc cptr σb hcq hnqb hnib.1 hC Jb.inv hV hU hPQ hPB + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Oracle.lean b/proofs/Fl4Proofs/P3/VmEntryS3Oracle.lean new file mode 100644 index 000000000..d9dec0358 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Oracle.lean @@ -0,0 +1,62 @@ +import Fl4Proofs.P3.VmOracle + +/-! +Concrete signal-oracle readings at the precisely paired x86 scheduler +states. The covered TCBs have the executing core's affinity by +`SchedCore.aff`. Oracle preservation across a native step needs its +running-VM footprint. VMEnter separately relates its newly running guest. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} + +/-- A TCB covered by the joint scheduler relation is on the executing core. -/ +theorem JAtZB.onCoreTcb {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} {Lst : U64 → List U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : Fl4Spec.ObjRef} {x : Fl4Spec.Tcb} (hk : σ.kheap t = some (.tcb x)) : + v3OnCore st s t = true := by + obtain ⟨u, hu, rfl⟩ := B.obj.tcb t x hk + obtain ⟨Q, R, hC, _⟩ := B.xtop.fin.1.1.1.lists + have e : ({ bv := u.bv } : U64) = u := by cases u; rfl + unfold v3OnCore + simp only [e, hC.aff u hu, hC.cur, decide_true] + +/-- Two matched scheduler states give the same entry pair on surviving guests. +On-core readings are supplied by the scheduler relation at each state. -/ +theorem JAtZB.vmOrcOfEntry {Z Z' : XSets} {H H' : P1.Heap} {Cs Cs' Ac Ac' D D' : Set U64} + {Lst Lst' : U64 → List U64} {s s' : S} {a a' : A} {σ σ' : Fl4Spec.State Unit} + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (B' : JAtZB (L := L) arI AR Z' H' Cs' Ac' Lst' D' s' a' σ') + (hT : ∀ t x, σ'.kheap t = some (.tcb x) → x.tcbState = .runningVM → + ∃ y, σ.kheap t = some (.tcb y)) : + VmOrc st (v3OnCore st s) (v3RemCur st s) s' σ' := by + intro t x hk hx + obtain ⟨y, hy⟩ := hT t x hk hx + have h0 := B.onCoreTcb hy + exact ⟨h0.trans (B'.onCoreTcb hk).symm, fun h => by rw [h0] at h; cases h⟩ + +/-- At related scheduler states the oracle remains valid for any running +guest that already had a running VM at the previous read. -/ +theorem JAtZB.vmOrcAcross {Z Z' : XSets} {H H' : P1.Heap} {Cs Cs' Ac Ac' D D' : Set U64} + {Lst Lst' : U64 → List U64} {s s' : S} {a a' : A} {σ σ' : Fl4Spec.State Unit} + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (B' : JAtZB (L := L) arI AR Z' H' Cs' Ac' Lst' D' s' a' σ') + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) (hR : RvmSub σ σ') : + VmOrc st on rc s' σ' := by + intro t x hk hx + obtain ⟨y, hy, hry⟩ := hR t x hk hx + have h0 := (hO t y hy hry).1.trans (B.onCoreTcb hy) + exact ⟨h0.trans (B'.onCoreTcb hk).symm, fun h => by rw [h0] at h; cases h⟩ + + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Orc.lean b/proofs/Fl4Proofs/P3/VmEntryS3Orc.lean new file mode 100644 index 000000000..0321f8a98 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Orc.lean @@ -0,0 +1,64 @@ +import Fl4Proofs.P3.VmHiRvm +import Fl4Proofs.P3.VmEntryS3Oracle +import Fl4Proofs.P3.VmPreempt + +/-! +P3 (line mmuAD, Stage 3, 3.3 oracle, ruling item 3): **`VmOrc` at the preempted outcomes of the invocation** +(`KEntryArmsV.orc`'s statement, proved). + +* `JHole.onCoreTcb`: a TCB of the spec at a preemption hole is on the executing core (the hole's `XTopAt`). +* `VmEptState.orcPre`: from `VmOrc` at the dispatch state, every preempted outcome `y` of + `fl4HandleInvocationVF on rc true …` and every `VmEptPre` witness at `y.2` carry `VmOrc` at `(s1, y.2)`: + `rvmSub_hiVF` (no thread becomes `runningVM`) and the hole's on-core fact. The oracle pair stays the one given + at the dispatch state (no existential choice). + +No named premise. [P] `ARKeep`, `ArchTotal`, `MachPlat`, `PPPlatA`, one domain (existing). New file. +-/ +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +/-- A TCB of the spec at a preemption hole is on the executing core. -/ +theorem JHole.onCoreTcb {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} {Lst : U64 → List U64} + {s0 : S} {a0 : A} {σ0 : Fl4Spec.State Unit} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (Hh : JHole (L := L) arI AR Z H Cs Ac Lst D s0 a0 σ0 s a σ) + {t : Fl4Spec.ObjRef} {x : Fl4Spec.Tcb} (hk : σ.kheap t = some (.tcb x)) : + v3OnCore st s t = true := by + have hkh : σ.kheap = σ0.kheap := Hh.sfacts.2.2.2.2.1 + obtain ⟨u, hu, rfl⟩ := Hh.j0.obj.tcb t x (by rw [← hkh]; exact hk) + obtain ⟨Q, R, hC, _⟩ := Hh.xtop.fin.1.1.1.lists + have e : ({ bv := u.bv } : U64) = u := by cases u; rfl + unfold v3OnCore + simp only [e, hC.aff u hu, hC.cur, decide_true] + +/-- **`VmOrc` at the preempted outcomes of the invocation** (module docstring). -/ +theorem VmEptState.orcPre (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hM : MachPlat arI MR) (hPA : PPPlatA arI AR) {on rc : Fl4Spec.ObjRef → Bool} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hO : VmOrc st on rc s σ) + (call bl don fp : Bool) (cptr : Fl4Spec.CapRef) + {y : Except Unit Unit × Fl4Spec.State Unit} + (hy : y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp cptr).run σ).1) + (hyE : y.1 = .error ()) {s1 : S} {a1 : A} + (hP : ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s1 a1 y.2) : + VmOrc st on rc s1 y.2 := by + obtain ⟨V', X', XL', Z0, H0, Cs0, Ac0, Lst0, D0, s0, a0, σ0, ⟨J0, -, -, -, hR0, hV0, -⟩, hpp, hσ⟩ := hP + have hw := J0.1.1.ppJHM hAK hA hnd hM hPA hR0 hV0 + rw [hpp, WP.spec_ok] at hw + obtain ⟨s1', Ac1, Lst1, Hh, -⟩ := hw rfl y.2 hσ + have hR : RvmSub σ y.2 := rvmSub_hiVF on rc true call bl don fp cptr σ y hy + intro t x hk hr + obtain ⟨x0, hk0, hr0⟩ := hR t x hk hr + have h0 := (hO t x0 hk0 hr0).1.trans (J.1.1.onCoreTcb hk0) + exact ⟨h0.trans (Hh.onCoreTcb hk).symm, fun h => by rw [h0] at h; cases h⟩ + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Outer.lean b/proofs/Fl4Proofs/P3/VmEntryS3Outer.lean new file mode 100644 index 000000000..d8f8bb882 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Outer.lean @@ -0,0 +1,174 @@ +import Fl4Proofs.P3.VmEntryS3Entry +import Fl4Proofs.P3.VmEntryS3Fault +import Fl4Proofs.P3.VmEntryS3Finish + +/-! +P3 (line mmuAD, Stage 3): **the outer top** — one theorem over the nine concrete kernel entries (`S3Ev`: user +exception, VM fault, trap, unknown syscall, VMEnter, interrupt, VM exit stalled / unstalled, native system call) from +`VEAnyX` (with that entry's `S3Pre`) to `VEAnyX`. Only the state relation is carried: the post does **not** re-establish +`S3Pre`, so every following entry must supply the whole `S3Pre` of its event again (`S3Fault`, or the scheduler / domain / +FPU / idle facts of the interrupt and VM-exit entries, or `StartS3` for a system call, and the hardware arguments). +No run of several entries is proved here. A case split over +`userExceptionEntryS3`, `vmFaultEntryS3`, `trapSyscallEntryS3`, `unknownSyscallEntryS3`, `vmEnterEntryS3`, +`interruptEntryS3`, `vcpuExitStalledS3` / `vcpuExitUnstalledS3` and `kernel_entryS3`. +Premises: the platform premises (arguments of `outerS3`) and, per entry, the fields of `S3Pre` ([J] the entry's spec +facts, [P] the hardware's arguments, [S] the entry's scope; native system call: the start record `StartS3`). +The oracle pair is fixed at the entry (`v3OnCore st s`, `v3RemCur st s`). +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open Classical fl4_core Fl4Proofs.P1 Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc + Fl4Proofs.P3.Sched Fl4Proofs.P3.SInvDom + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +/-- **The concrete kernel entries** (module docstring). -/ +inductive S3Ev (A : Type) where + | user (n c : U64) + | vmf (ad c : U64) (ft : Fl4Spec.VMFaultType) + | trap (v : U64) + | unk (n : I64) + | vmEnter + | irq (a' : A) (w : U64) + | vcpu (w : U64) (irq : Option Fl4Spec.Irq) (stalled : Bool) + | sys (n : I64) (c : Fl4Spec.Syscall) + +/-- The FL4 function each entry runs. -/ +def S3Ev.run (st : machine.Store S) (arI : machine.Arch A) : S3Ev A → S → A → Result (S × A) + | .user n c, s, a => syscall.handle_user_exception st arI s a n c + | .vmf ad c _, s, a => syscall.handle_vm_fault st arI s a ad c + | .trap v, s, a => syscall.handle_trap_syscall st arI s a v + | .unk n, s, a => syscall.handle_syscall st arI s a n + | .vmEnter, s, a => syscall.handle_syscall st arI s a fl4_abi.syscall.SysVMEnter + | .irq a' w, s, _ => syscall.handle_interrupt_entry st arI s a' w + | .vcpu w _ b, s, a => syscall.handle_vcpu_exit st arI s a w b + | .sys n _, s, a => syscall.handle_syscall st arI s a n + +variable (st) in +/-- **The extended-spec outcome of each entry**, at the oracle pair of the entry state. -/ +def S3Ev.Out (e : S3Ev A) (s : S) (σ σ' : Fl4Spec.State Unit) : Prop := + match e with + | .user n c => ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true + (.userLevelFault n.bv c.bv) σ).1 + | .vmf _ _ ft => ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true + (.vmFaultEvent ft) σ).1 + | .trap v => ∃ ev, KEvSpec (.trap v) true ev ∧ FaultIpOutVF (v3OnCore st s) (v3RemCur st s) ev σ σ' + | .unk n => ∃ ev, KEvSpec (.sysU n) true ev ∧ FaultIpOutVF (v3OnCore st s) (v3RemCur st s) ev σ σ' + | .vmEnter => FaultIpOutVF (v3OnCore st s) (v3RemCur st s) (.unknownSyscall Fl4Spec.fl4SysVMEnterNum) σ σ' + | .irq _ _ => ((), σ') ∈ (Fl4Spec.fl4CallKernelVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true .interrupt σ).1 + | .vcpu _ v b => ((), σ') ∈ (Fl4Spec.fl4VcpuExitSavedVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) v b σ).1 + | .sys _ c => ((), σ') ∈ (Fl4Spec.fl4KernelEntrySyscallVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) true c σ).1 + +/-- The spec facts of the entry state that the fault entries ask ([J]; [P] `MR`). -/ +structure S3Fault (MR : A → Fl4Spec.MachineState → Prop) (a : A) (σ : Fl4Spec.State Unit) : Prop where + dom : DomListOK σ + fo : FpuOwnOK σ + idle : IdleVt σ + ni : Entered σ + nq : ∀ d p, σ.curThread ∉ σ.readyQueues d p + vfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler + mr : MR a σ.machineState + vmt : ValidMachineTime σ + off : OffReady σ + +variable (st arI AR MR) in +/-- **The premises of each entry** (module docstring). -/ +def S3Pre (e : S3Ev A) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + match e with + | .user n c => S3Fault MR a σ ∧ UserHw n c + | .vmf ad c ft => S3Fault MR a σ ∧ VmfHw ad c ft σ + | .trap v => S3Fault MR a σ ∧ v.val < 256 + | .unk n => S3Fault MR a σ ∧ UnkSys (L := L) n s ∧ NoDbgSys (L := L) arI n s a ∧ P1.FastFaultOK (L := L) arI s a n ∧ + (∀ x ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1, + Fl4Spec.isVmEnterCall (IScalar.hcast .U64 n).val x.2 = false) + | .vmEnter => S3Fault MR a σ ∧ (∀ ct, sched.cur_thread st s = ok ct → ZvCtRun σ ct) ∧ + (∀ i, arI.virtualization a = ok i → i.val = Fl4Spec.virtKind.code) ∧ + (∀ ct v, sched.cur_thread st s = ok ct → obj.Tcb.flags st ct (L.wr s (Endp.regA ct layout.reg.FAULT_IP) v) + ⦃ f => (f &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 ⦄) ∧ + (∀ x ∈ (Fl4Spec.fl4SyscallFaultIP (Z := Unit) σ.curThread σ).1, + Fl4Spec.isVmEnterCall Fl4Spec.fl4SysVMEnterNum x.2 = true) + | .irq a' w => σ.schedulerAction = .resumeCurThread ∧ (∀ d p, σ.curThread ∉ σ.readyQueues d p) ∧ + ValidMachineTime σ ∧ OffReady σ ∧ ValidIrqMasks σ ∧ DomListOK σ ∧ FpuOwnOK σ ∧ IdleVt σ ∧ IrqHw AR MR a a' σ w + | .vcpu w v true => MR a σ.machineState ∧ DomListOK σ ∧ FpuOwnOK σ ∧ IdleVt σ ∧ SavedIrqAt w v + | .vcpu w v false => Entered σ ∧ (∀ d p, σ.curThread ∉ σ.readyQueues d p) ∧ MR a σ.machineState ∧ + ValidMachineTime σ ∧ OffReady σ ∧ DomListOK σ ∧ FpuOwnOK σ ∧ IdleVt σ ∧ SavedIrqAt w v + | .sys n c => MR a σ.machineState ∧ (∀ ct, sched.cur_thread st s = ok ct → ZvCtRun σ ct) ∧ NativeSys n c ∧ + (∀ ct v, sched.cur_thread st s = ok ct → obj.Tcb.flags st ct (L.wr s (Endp.regA ct layout.reg.FAULT_IP) v) + ⦃ f => (f &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 ⦄) ∧ + StartS3 (L := L) (st := st) (arI := arI) n s a σ + +set_option maxHeartbeats 4000000 in +/-- **The outer top** (module docstring): every concrete entry from a `VEAnyX` state satisfying its `S3Pre` refines its extended-spec +outcome and ends in `VEAnyX`. +The post does not re-establish `S3Pre` (module docstring). -/ +theorem VEAnyX.outerS3 [P1.PlainStore st] (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hVA : ZvVirtAgree arI) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hPI : PortInValMR arI MR) (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hag : ∀ a, ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hrcb : Fl4Spec.resetChunkBits = 16) + (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hFpu : Sched.FpuImplDet) (hFW : FpuWriteDet) (hStore : P1.StoreExt st) + {s : S} {a : A} {σ : Fl4Spec.State Unit} (h : VEAnyX (L := L) arI AR s a σ) + (e : S3Ev A) (hE : S3Pre (L := L) st arI AR MR e s a σ) : + e.run st arI s a ⦃ r => ∃ σ', S3Ev.Out st e s σ σ' ∧ VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + obtain ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, hvx, J⟩ := h + haveI := hvx + cases e with + | user n c => + obtain ⟨F, hw⟩ := hE + exact VmEptState.userExceptionEntryS3 (MR := MR) hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu J hFpu hFW + F.dom F.fo F.idle F.ni F.nq F.vfh hM F.mr F.vmt F.off n c hw + | vmf ad c ft => + obtain ⟨F, hw⟩ := hE + exact VmEptState.vmFaultEntryS3 (MR := MR) hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu J hFpu hFW + F.dom F.fo F.idle F.ni F.nq F.vfh hM F.mr F.vmt F.off ad c ft hw + | trap v => + obtain ⟨F, hv⟩ := hE + refine WP.spec_mono (VmEptState.trapSyscallEntryS3 (MR := MR) hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu J + hFpu hFW F.dom F.fo F.idle F.ni F.nq F.vfh hM F.mr F.vmt F.off v hv) ?_ + rintro r ⟨ev, σ', h1, h2, h3⟩ + exact ⟨σ', ⟨ev, h1, h2⟩, h3⟩ + | unk n => + obtain ⟨F, hU, hD, hFF, hNE⟩ := hE + refine WP.spec_mono (VmEptState.unknownSyscallEntryS3 (MR := MR) hARI hAT hVA hnd hT hRef hDL hmrt hmb hmax htu J + hFpu hFW F.dom F.fo F.idle F.ni F.nq F.vfh hM F.mr F.vmt F.off n hU hD hStore hFF hNE) ?_ + rintro r ⟨ev, σ', h1, h2, h3⟩ + exact ⟨σ', ⟨ev, h1, h2⟩, h3⟩ + | vmEnter => + obtain ⟨F, hRun, hCode, hFlag, hEnter⟩ := hE + exact VmEptState.vmEnterEntryS3 (MR := MR) hARI hAT hVA hCap hnd hT hRef hDL hFpu hFW hmrt hmb hmax htu J + F.ni F.nq hRun hM F.mr F.vmt F.off F.dom F.fo F.idle hCode hFlag hEnter + | irq a' w => + obtain ⟨hres, hnq, hTime, hOff, hMask, hDom, hFo, hIdle, hw⟩ := hE + exact VmEptState.interruptEntryS3 (MR := MR) J hARI hAT hVA hM hMK hIA hPA hnd hT hRef hDL hFpu hFW hmax htu hmrt + hres hnq hTime hOff hMask hDom hFo hIdle hw + | vcpu w v b => + cases b with + | true => + obtain ⟨hMR, hDom, hFo, hIdle, hw⟩ := hE + exact VmEptState.vcpuExitStalledS3 (MR := MR) J hARI hAT hVA hM hMK hIA hPA hnd hT hRef hDL hFpu hFW hmax htu + hMR hDom hFo hIdle hw + | false => + obtain ⟨hni, hnq, hMR, hVT, hOff, hDom, hFo, hIdle, hw⟩ := hE + exact VmEptState.vcpuExitUnstalledS3 (MR := MR) J hARI hAT hVA hM hMK hIA hPA hnd hT hRef hDL hFpu hFW hmrt hmax + htu hni hnq hMR hVT hOff hDom hFo hIdle hw + | sys n c => + obtain ⟨hMR, hRun, hn, hflag, St⟩ := hE + exact J.kernel_entryS3 arI AR MR hARI hAT hVA hSy hCap hPI hKDAR hKD hnd hM hMK hPA hIA hmrt hmb hmax htu hag + hfan hrcb hT hRef hDL hFpu hFW hStore rfl rfl hMR hRun hn hflag St + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Pml4.lean b/proofs/Fl4Proofs/P3/VmEntryS3Pml4.lean new file mode 100644 index 000000000..b424bc050 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Pml4.lean @@ -0,0 +1,542 @@ +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.VsThX +import Fl4Proofs.P3.FrontEndLabel +import Fl4Proofs.P3.XTopJointFpu +import Fl4Proofs.P3.XTopJointVsCap +import Fl4Proofs.P3.SpecNoPre +import Fl4Proofs.P3.VsFbDec +import Fl4Proofs.P3.EptFinAll +import Fl4Proofs.P3.SpecFbDelRun +import Fl4Proofs.P3.VirtFrameBatchConservative +import Fl4Proofs.P3.XTopJointVsFb + +/-! +Selected PML4 flush and decoder failures in the VM refinement. [P] Flush +uses the native VSpace architecture refinement and total ASID invalidation. +[J] The current thread is entered, and the cap comes from the selected slot. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} +variable {V X : Set U64} {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} +variable {Lst : U64 → List U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The reached unmapped or EPT-mapped frame DELETE of a FrameBatch, after target lookup. +The concrete delete supplies the same state used by the VM batch outcome. +[P] Uses the existing architecture refinement, totality and device +finalisation contracts. -/ +theorem VmEptState.fbDeleteOffOrEpt [Fl4Proofs.P1.PlainStore st] {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) + {b : Fl4Spec.Fl4FrameBatchInvocation} {w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data} + (hop : b.op = .delete) {sl : Fl4Spec.CslotPtr} (hAs : AbsSlot Z.th sl) + (hlook : (.ok sl, σ) ∈ ((Fl4Spec.lookupTargetSlot (Z := Unit) b.cnode + (Fl4Spec.dataToCptr w.1) b.depth).run σ).1) + {p rt asid va : U64} {sz : capability.FrameSize} {dev mapped ept : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Frame p sz rt dev mapped ept asid va)) + (hept : mapped = true → ept = true) : + cdt.delete st arI s a (cslotAddr sl) true ⦃ r => r.1 = .Ok () ∧ + ∃ σ' G' Ac' Lst' D', + (.ok false, σ') ∈ ((Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w).run σ).1 ∧ + VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' r.2.1 r.2.2 σ' ∧ + capV (L.mem r.2.1) (cslotAddr sl) = .Null ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr sl → capV (L.mem r.2.1) y = capV (L.mem s) y) ∧ + L.ksb r.2.1 = L.ksb s ∧ FbKeep σ σ' ∧ + FbFootK (L := L) Z s r.2.1 ∧ CnSlotsKept (L := L) Z.th s r.2.1 ⦄ := by + obtain ⟨c, ac, hc', hca, hget⟩ := J.1.1.xtop.rel.2.1.1.abs_cap hAs + rw [hc] at hc' + cases eq_of_ok_eq hc' + rw [hca.abs] at hget + have hg : (absCap (.Frame p sz rt dev mapped ept asid va), σ) ∈ (Fl4Spec.getCap (Z := Unit) sl σ).1 := + by rw [hget]; exact Set.mem_singleton _ + refine WP.spec_mono (VmEptState.delFrame hRef J hAs hc hept (fun _ => ⟨L.total, hAT, hKD⟩)) ?_ + rintro r ⟨hr, σ', G', Ac', Lst', D', hd, J', hnull, hslot, hks⟩ + have keep : FbKeep σ σ' ∧ σ'.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + cases mapped + · exact fbKeep_capDeleteVF_frame (SInvU.getCap_capAt hg).2 (by intro h; cases h) hd + · have he := hept rfl + subst ept + exact fbKeep_capDeleteVF_eptFrame hget hd + obtain ⟨hK, hum⟩ := keep + refine ⟨hr, σ', G', Ac', Lst', D', ?_, J', hnull, hslot, hks, hK, + FbFootK.of_relB arI J.1.1 J'.1.1 rfl rfl hK hum, ?_⟩ + · refine fbEntryWith_del_frame hop hlook hg (fun _ _ hm h => ?_) hd + cases mapped + · cases hm + · have he := hept rfl + subst ept + exact (by decide : Fl4Spec.VMMapType.vmEptMap ≠ .vmVSpaceMap) h.2.1 + · intro y hy pp rad gs guard hcap + by_cases hne : y = cslotAddr sl + · subst y + rw [capV_of_capAt hc] at hcap + cases hcap + · exact (hslot y hy hne).trans hcap + +set_option maxHeartbeats 1000000 in +/-- A DELETE whose matched lookup resolves an unmapped or EPT-mapped frame reaches +the VM-aware batch outcome and preserves the deferred native flush flag. +[P] Uses the existing architecture refinement, totality and device +finalisation contracts. -/ +theorem VmEptState.fbDeleteOffOrEptEntry [Fl4Proofs.P1.PlainStore st] {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) + {b : Fl4Spec.Fl4FrameBatchInvocation} {w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data} + (hop : b.op = .delete) {bt : vspace.Batch} {w0 w1 w2 : U64} {deferred : Bool} + (hopC : bt.op ≠ vspace.frame_batch.COPY_MAP) (hopR : bt.op ≠ vspace.frame_batch.REMAP) + {sl : Fl4Spec.CslotPtr} (hAs : AbsSlot Z.th sl) + (hlookup : cspace.lookup_target_slot st s bt.cnode w0 bt.depth = ok (.Ok (cslotAddr sl))) + (hlook : (.ok sl, σ) ∈ ((Fl4Spec.lookupTargetSlot (Z := Unit) b.cnode + (Fl4Spec.dataToCptr w.1) b.depth).run σ).1) + {p rt asid va : U64} {sz : capability.FrameSize} {dev mapped ept : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Frame p sz rt dev mapped ept asid va)) + (hept : mapped = true → ept = true) : + vspace.frame_batch_entry st arI s a bt w0 w1 w2 deferred ⦃ r => + r.1 = .Ok () ∧ r.2.2.2 = deferred ∧ ∃ σ' G' Ac' Lst' D', + (.ok false, σ') ∈ ((Fl4Spec.fbEntryWith (Z := Unit) Fl4Spec.capDeleteVF b w).run σ).1 ∧ + VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' r.2.1 r.2.2.1 σ' ∧ + capV (L.mem r.2.1) (cslotAddr sl) = .Null ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr sl → capV (L.mem r.2.1) y = capV (L.mem s) y) ∧ + L.ksb r.2.1 = L.ksb s ∧ FbKeep σ σ' ∧ + FbFootK (L := L) Z s r.2.1 ∧ CnSlotsKept (L := L) Z.th s r.2.1 ⦄ := by + unfold vspace.frame_batch_entry + rw [ite_eq_right hopC, ite_eq_right hopR, hlookup] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch] + rw [slot_cap_eq, hc] + simp only [bind_ok, capability.Cap.is_null, Bool.false_eq_true, ↓reduceIte, vspace.as_frame] + cases mapped <;> (try (have he := hept rfl; subst ept)) <;> by_cases hd : bt.defer_delete <;> + simp only [hd, Bool.false_eq_true, ↓reduceIte, bind_ok] <;> fbj_red <;> + refine WP.spec_bind (J.fbDeleteOffOrEpt hRef hAT hKD hop hAs hlook hc hept) ?_ <;> + rintro ⟨r, s', a'⟩ ⟨hr, h⟩ <;> + exact (WP.spec_ok _).2 ⟨hr, rfl, h⟩ + +/-- `ZNoBlk` after a step that rewrites one slot with a cap that is no zombie and keeps the other +slots and the kernel-state base. -/ +theorem ZNoBlk.ofSlot {th : TypedHeap} {m m' : U64 → U64} {kb kb' : U64} (h : ZNoBlk th m kb) (hk : kb' = kb) + {x : U64} (hx : ∀ p zt n, capV m' x ≠ .Zombie p zt n) + (hcap : ∀ y, IsSlot th y → y ≠ x → capV m' y = capV m y) : ZNoBlk th m' kb' := by + subst hk + intro y hy p zt n hz + by_cases e : y = x + · subst e; exact absurd hz (hx p zt n) + · exact h y hy p zt n (by rw [← hcap y hy e]; exact hz) + +/-- The VM relation and the matched reads that the batch carries between entries. -/ +structure FbMidVE (arI : machine.Arch A) (V X : Set U64) (XL : U64 → Nat) (AR : A → Fl4Spec.Cr3 → Prop) + (Z : XSets) (H : P1.Heap) (Cs : Set U64) (σ : Fl4Spec.State Unit) (ct buf : U64) + (args : List U64) (bt : vspace.Batch) (s : S) (a : A) (τ : Fl4Spec.State Unit) : Prop where + rel : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D s a τ + words : ∀ (i : U64) v, args[i.val]? = some v → msg.syscall_arg st s ct buf i = ok v + keep : FbKeep σ τ + roots : (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = bt.cnode) ∧ + (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = bt.dest_cnode) + /-- No zombie names an object in the kernel-state block (for the COPY_MAP insert). -/ + znb : ZNoBlk Z.th (L.mem s) (L.ksb s) + +/-- Transfer the matched message and held CNodes to the state produced by one entry. -/ +theorem FbMidVE.step {XL : U64 → Nat} {ct buf : U64} {args : List U64} {bt : vspace.Batch} + {b : Fl4Spec.Fl4FrameBatchInvocation} {τ τ' : Fl4Spec.State Unit} {s' : S} {a' : A} + (C : FbCtx σ Z.th args bt b) (hct : ct ∈ Z.T) (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (mid : FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s a τ) + (hrel : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D s' a' τ') + (hK : FbKeep τ τ') (hF : FbFootK (L := L) Z s s') (hCN : CnSlotsKept (L := L) Z.th s s') + (hZ : ZNoBlk Z.th (L.mem s') (L.ksb s')) : + FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s' a' τ' := by + refine ⟨hrel, ?_, mid.keep.trans hK, ⟨hCN.held C.hcnC mid.roots.1, hCN.held C.hdcC mid.roots.2⟩, hZ⟩ + intro i v hv + rw [← mid.words i v hv] + refine syscall_arg_keep (hF.1 ct hct) ?_ + intro hb0 + obtain ⟨sp, hsp⟩ := hbm hb0 + have hi := (List.getElem?_eq_some_iff.1 hv).1 + have hal := C.hal + have hi1 : (core.num.U64.wrapping_add i 1#u64).val = i.val + 1 := by + have := i.hBounds + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] at this ⊢ + omega + exact hF.2 _ (hsp _ (by rw [hi1]; omega)) + +/-- A reached frame DELETE without a native mapping supplies an entry outcome for the shared batch loop. -/ +theorem FbMidVE.offOrEptDelete [P1.PlainStore st] {XL : U64 → Nat} {ct buf : U64} {args : List U64} + {τ : Fl4Spec.State Unit} {bt : vspace.Batch} {b : Fl4Spec.Fl4FrameBatchInvocation} + (C : FbCtx σ Z.th args bt b) (hct : ct ∈ Z.T) (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (mid : FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt s a τ) + (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) (hKD : MRKeepD arI AR) + (hop : b.op = .delete) {w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data} {w0 w1 w2 : U64} {df : Bool} + (hopC : bt.op ≠ vspace.frame_batch.COPY_MAP) (hopR : bt.op ≠ vspace.frame_batch.REMAP) + {sl : Fl4Spec.CslotPtr} (hAs : AbsSlot Z.th sl) + (hlookup : cspace.lookup_target_slot st s bt.cnode w0 bt.depth = ok (.Ok (cslotAddr sl))) + (hlook : (.ok sl, τ) ∈ ((Fl4Spec.lookupTargetSlot (Z := Unit) b.cnode + (Fl4Spec.dataToCptr w.1) b.depth).run τ).1) + {p rt asid va : U64} {sz : capability.FrameSize} {dev mapped ept : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Frame p sz rt dev mapped ept asid va)) + (hept : mapped = true → ept = true) : + vspace.frame_batch_entry st arI s a bt w0 w1 w2 df ⦃ r => FbEntryWithPost Fl4Spec.capDeleteVF + (FbMidVE (L := L) arI V X XL AR Z H Cs σ ct buf args bt) b w τ df r ⦄ := by + obtain ⟨G, E, Ac, Lst, D, J⟩ := mid.rel + refine WP.spec_mono (J.fbDeleteOffOrEptEntry hRef hAT hKD hop hopC hopR hAs hlookup hlook hc hept) ?_ + rintro r ⟨hr, hdf, τ', G', Ac', Lst', D', hmem, J', hnull, hslot, hks, hK, hF, hCN⟩ + have hrel : ∃ G E Ac Lst D, VmEptState (L := L) arI V X XL AR (Z.mv G E) H Cs Ac Lst D + r.2.1 r.2.2.1 τ' := ⟨G', E, Ac', Lst', D', J'⟩ + have hZ := mid.znb.ofSlot hks (fun _ _ _ h => by rw [hnull] at h; cases h) hslot + exact ⟨τ', FbMidVE.step C hct hbm mid hrel hK hF hCN hZ, Or.inl ⟨hr, false, hmem, by simpa using hdf⟩⟩ + +private theorem VmEptState.nativeFlushPost {XL : U64 → Nat} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hF : FinXR (L := L) AR Z s' a' σ') (hV : VsJFlush (L := L) Z.th Z.G s a σ s' a' σ') : + VEAnyX (L := L) arI AR s' a' σ' := by + rcases hV with ⟨rfl, rfl, rfl⟩ | ⟨ct, σ1, ms, hct, hs, hst, rfl, hCR, hFI, hMO⟩ + · exact J.anyX arI + have hCur := P1.cur_thread_ok (L := L) s + have hctEq : ct = L.curW s := eq_of_ok_eq (hct.symm.trans hCur) + subst ct + obtain ⟨s1, σ1', Lst1, hs1, hst1, J1⟩ := J.restart hni + have es : s' = s1 := eq_of_ok_eq (hs.symm.trans hs1) + subst s1 + have eσ : σ1' = σ1 := by + rw [hst] at hst1 + have e := congrArg (fun q => ((), σ1') ∈ q.1) hst1 + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at e + exact of_eq_true e + subst σ1' + have hks : L.ksb s' = L.ksb s := by + have h := set_thread_state_cases (L := L) s (L.curW s) sched.TS_RESTART + rw [hs, WP.spec_ok] at h + rcases h with rfl | rfl <;> simp + have hX : XTopAt (L := L) AR Z s' a' {σ1 with machineState := ms} := by + obtain ⟨hFin, ⟨_, hPl, -, -, hD, -, hIA, hSA⟩, hE⟩ := hF + obtain ⟨⟨⟨hSch, hEp, hNt, -⟩, -⟩, -⟩ := id hFin + exact ⟨hFin, ⟨hPl, hCR, hFI, hD, by rw [hks]; exact hMO, hIA, hSA⟩, + IpcRel.of hNt hEp (sched_sc hSch), hE⟩ + have hAr : ArFr σ1 {σ1 with machineState := ms} := + ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have B' := J1.1.1.arFrS hAr hX + have hW : P1.WritesIn st s' s' (fun _ => False) := P1.WritesIn.refl _ _ + exact (J1.ofB B' hW (fun _ h => h.elim) (fun _ h => h.elim) (fun _ _ => rfl) rfl rfl).anyX arI + +set_option maxHeartbeats 8000000 in +/-- Flush and every non-FrameBatch error on the concrete selected PML4. +A successful flush changes only the architecture after the matched Restart. -/ +theorem VmEptState.selectedNativePml4NonBatch {XL : U64 → Nat} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) + {lbl : U64} (hBatch : lbl ≠ fl4_abi.label.X86PML4FrameBatch) + {ct ptr asid len buf : U64} {mapped : Bool} {sl : Fl4Spec.CslotPtr} + (hct : sched.cur_thread st s = ok ct) (hslot : AbsSlot Z.th sl) + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Pml4 ptr mapped asid)) + (extra : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Pml4 ptr mapped asid) + blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Pml4 ptr mapped asid) excaps buffer) s res ⦄ := by + have B := J.1.1 + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := B.xtop.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have htcb := (B.xtop.curTcb hcur).1 + have hrun := B.curRun.run hni + have hact := B.kern.act + have hV := B.vsc _ hslot.isSlot + rw [capV_of_capAt hc] at hV + have h0 : mapped = true → asid ≠ 0#u64 := fun h => (hV.2.2 h).1 + have hfb : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4FrameBatchLabel ∧ lbl ≠ fl4_abi.label.X86PML4FrameBatch := + ⟨fun h => hBatch ((pml4fb_label lbl).1 h), hBatch⟩ + have hinv : ∀ (a : A) (asid : U64), ∃ a', arI.invalidate_asid a asid = ok a' := fun a asid => by + obtain ⟨r, hr, -⟩ := spec_exists (hAT.invalidate_asid a asid) + exact ⟨r, hr⟩ + have hD := DecOKMX.toXD arI + (decode_invocation_pml4_capX arI Z.th s a σ Z.G hCR hFI hMO hRef hS hPI hCur hR hPt lbl lbl.bv + (pml4_label lbl) hfb ct hct hcur htcb hact hrun ptr asid mapped h0 len (cslotAddr sl) buf + blk call don fp extra args ci sl excaps buffer) + (domFrame_pml4 arI Z.th hPl s a σ Z.G hCR hFI hMO hS lbl lbl.bv (pml4_label lbl) hfb ct hct hcur htcb hact + hinv ptr asid mapped len (cslotAddr sl) buf blk call don fp extra args ci sl excaps buffer) + (okAll_specDec_pml4 fp lbl.bv args ci sl ptr mapped asid excaps buffer hfb.1) hDR + (fun _ _ _ hx hd => ⟨FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd, hx.2⟩ : + ∀ s' a' σ', + (FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR + (L.ksb s) σ s' a' σ' ∧ VsJFlush (L := L) Z.th Z.G s a σ s' a' σ') → + DomRel (L.mem s') (L.ksb s') σ' → + FinXR (L := L) AR Z s' a' σ' ∧ VsJFlush (L := L) Z.th Z.G s a σ s' a' σ') + refine WP.spec_mono hD ?_ + intro res h + let m := specDec fp lbl.bv args ci sl (.Pml4 ptr mapped asid) excaps buffer + change VDispPostVEX (L := L) arI AR MR on rc σ blk call don (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base m) s res + change TopPostDMX (L := L) _ σ blk call don m s res at h + rcases h with ⟨e, he, hs, ⟨e', hm, hRel⟩, hF, hV⟩ | ⟨i, hi, σ1, hst, y, hy, ⟨_, _, hr, -⟩, hF, hV⟩ + · exact Or.inl ⟨e, he, hs, ⟨e', run_liftME_err hm, hRel⟩, J.nativeFlushPost hni hF hV⟩ + · change DecOut (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.fl4ArchDecodeInvocation (Z := Unit) lbl.bv args ci sl + (.pml4Cap ptr.bv (if mapped then some asid.bv else none)) excaps)) σ i at hi + obtain ⟨iv, rfl⟩ := decOut_liftME_inv hi + -- a non-FrameBatch PML4 label decodes to a flush or a ported invocation, not substituted by the VM perform + have hns : ¬ Fl4Spec.PerformSubst (.invokeArchObject iv) := by + have hi' := hi + simp only [Fl4Spec.fl4ArchDecodeInvocation] at hi' + by_cases hA : Fl4Spec.dataToNat lbl.bv = Fl4Spec.fl4Pml4FlushLabel + · rw [if_pos hA] at hi' + obtain ⟨j, hj⟩ := DecOut.liftME2_inv hi' + cases hj + exact id + · rw [if_neg hA, if_neg hfb.1] at hi' + obtain ⟨j, hj⟩ := DecOut.liftME2_inv hi' + cases hj + exact id + have hy' : y ∈ ((Fl4Spec.fl4PerformInvocationVF (Z := Unit) on rc blk call don + (.base (.invokeArchObject iv))).run σ1).1 := by + rw [VirtF13.fl4PerformInvocationVF_base on rc blk call don _ hns]; exact hy + refine Or.inr ⟨.base (.invokeArchObject iv), hi.liftME, σ1, hst, y, hy', Or.inl ?_⟩ + rcases hr with ⟨h1, h2⟩ | ⟨_, h2⟩ + · exact ⟨h1, h2, J.nativeFlushPost hni hF hV⟩ + · exact (NoPre.perform_noErr blk call don (.invokeArchObject iv) (fun h => h) σ1 y hy () h2).elim + +/-- The selected ASID-control cap rejects non-MakePool labels and +MakePool with fewer than two matched extra capabilities. -/ +theorem VmEptState.selectedAsidControlReject {XL : U64 → Nat} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {lbl ct len buf : U64} {sl : Fl4Spec.CslotPtr} + (hct : sched.cur_thread st s = ok ct) + (extra : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hE : ExtraOK Z.th (L.mem s) extra excaps) + (hReject : lbl ≠ fl4_abi.label.X86ASIDControlMakePool ∨ excaps.length ≤ 1) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) .AsidControl blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl .AsidControl excaps buffer) s res ⦄ := by + let dec := Fl4Spec.archDecodeInvocation (Z := Unit) lbl.bv args ci sl .asidControlCap excaps + have hSpec : specDecV fp lbl.bv args ci sl .AsidControl excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec dec)) := rfl + have fail (err : error.Exc) (e : Fl4Spec.SyscallError) (hRel : ErrRel err e) + (hd : dec.run σ = ({(.error e, σ)}, False)) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl .AsidControl excaps buffer) s (.Err err, s, a) := by + rw [hSpec] + exact Or.inl ⟨err, rfl, rfl, ⟨e, run_liftME_err (run_liftME_err (run_liftME_err hd)), hRel⟩, J.anyX arI⟩ + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok] + by_cases hl : lbl = fl4_abi.label.X86ASIDControlMakePool + · have hSmall := hReject.resolve_left (fun hn => hn hl) + subst lbl + have hTrunc := decodeMP_trunc ((makePool_label _).1 rfl) args ci sl excaps hSmall σ + have h := (ExtraOK.mpExtra J.1.1.xtop.rel.1 hE).2 + rcases h with ⟨o0, o1, h0, h1, hn, -⟩ | ⟨pp, rs, idx, y, uc, uc', rc', sc', ysl, rest, _, _, _, _, _, _, _, _, _, hl⟩ + · simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, h0, h1, bind_ok] + rcases hn with rfl | rfl + · simp only [error.truncated, bind_ok, WP.spec_ok] + exact fail _ .truncatedMessage (Or.inl trivial) hTrunc + · cases o0 <;> simp only [error.truncated, bind_ok, WP.spec_ok] <;> + exact fail _ .truncatedMessage (Or.inl trivial) hTrunc + · rw [hl] at hSmall + simp at hSmall + · have hb : (lbl != fl4_abi.label.X86ASIDControlMakePool) = true := by simpa using hl + simp only [hb, ↓reduceIte, error.illegal, bind_ok, WP.spec_ok] + exact fail _ .illegalOperation (Or.inl trivial) (decodeMP_illegal (fun h => hl ((makePool_label lbl).2 h)) args ci sl excaps σ) + +/-- FrameBatch on an unmapped selected PML4 fails before the header or +any table or slot operation. -/ +theorem VmEptState.selectedFrameBatchUnmapped {XL : U64 → Nat} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct ptr asid len buf : U64} {sl : Fl4Spec.CslotPtr} + (hct : sched.cur_thread st s = ok ct) + (extra : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PML4FrameBatch len (cslotAddr sl) + (.Pml4 ptr false asid) blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PML4FrameBatch.bv args ci sl + (.Pml4 ptr false asid) excaps buffer) s res ⦄ := by + let lbl := fl4_abi.label.X86PML4FrameBatch + have hFlush : lbl ≠ fl4_abi.label.X86PML4Flush := by + dsimp only [lbl] + unfold fl4_abi.label.X86PML4FrameBatch fl4_abi.label.X86PML4Flush + decide + have hsFlush : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4Pml4FlushLabel := + fun h => hFlush ((pml4_label lbl).1 h) + have hsBatch : Fl4Spec.dataToNat lbl.bv = Fl4Spec.fl4FrameBatchLabel := (pml4fb_label lbl).2 rfl + have hAbs : specDecV fp lbl.bv args ci sl (.Pml4 ptr false asid) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.frameBatch + (Fl4Spec.decodeFrameBatch (Z := Unit) args (.pml4Cap ptr.bv none) excaps))) := by + show Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.fl4ArchDecodeInvocation (Z := Unit) lbl.bv args ci sl (.pml4Cap ptr.bv none) excaps)) = _ + simp only [Fl4Spec.fl4ArchDecodeInvocation, if_neg hsFlush, if_pos hsBatch] + change syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Pml4 ptr false asid) + blk call don fp buf extra ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Pml4 ptr false asid) excaps buffer) s res ⦄ + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, if_neg hFlush, if_pos (show lbl = fl4_abi.label.X86PML4FrameBatch from rfl)] + unfold vspace.decode_frame_batch + rw [hct] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte, error.invalid_cap, WP.spec_ok, hAbs] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + exact run_liftME_err (run_liftME_err (run_liftME_err (by rfl))) + +/-- A mapped selected PML4 rejects FrameBatch before reading the header +if the ASID lookup fails or the matched message has no header word. -/ +theorem VmEptState.selectedFrameBatchBeforeHeader {XL : U64 → Nat} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct ptr asid len buf : U64} {sl : Fl4Spec.CslotPtr} + (hct : sched.cur_thread st s = ok ct) + (hslot : AbsSlot Z.th sl) (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Pml4 ptr true asid)) + (extra : Array obj.Slot 3#usize) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) + (hlen : len.val = args.length) + (hReject : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) asid.bv) σ ≠ ({(.ok ptr.bv, σ)}, False) ∨ + args.length < 1) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PML4FrameBatch len (cslotAddr sl) + (.Pml4 ptr true asid) blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PML4FrameBatch.bv args ci sl + (.Pml4 ptr true asid) excaps buffer) s res ⦄ := by + let lbl := fl4_abi.label.X86PML4FrameBatch + let dec := Fl4Spec.decodeFrameBatch (Z := Unit) args (.pml4Cap ptr.bv (some asid.bv)) excaps + have hFlush : lbl ≠ fl4_abi.label.X86PML4Flush := by + dsimp only [lbl] + unfold fl4_abi.label.X86PML4FrameBatch fl4_abi.label.X86PML4Flush + decide + have hsFlush : Fl4Spec.dataToNat lbl.bv ≠ Fl4Spec.fl4Pml4FlushLabel := + fun h => hFlush ((pml4_label lbl).1 h) + have hsBatch : Fl4Spec.dataToNat lbl.bv = Fl4Spec.fl4FrameBatchLabel := (pml4fb_label lbl).2 rfl + have hAbs : specDecV fp lbl.bv args ci sl (.Pml4 ptr true asid) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.frameBatch dec)) := by + show Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.fl4ArchDecodeInvocation (Z := Unit) lbl.bv args ci sl + (.pml4Cap ptr.bv (some asid.bv)) excaps)) = _ + simp only [Fl4Spec.fl4ArchDecodeInvocation, if_neg hsFlush, if_pos hsBatch, dec] + have fail (err : error.Exc) (e : Fl4Spec.SyscallError) (hRel : ErrRel err e) + (hd : dec.run σ = ({(.error e, σ)}, False)) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Pml4 ptr true asid) excaps buffer) s (.Err err, s, a) := by + rw [hAbs] + exact Or.inl ⟨err, rfl, rfl, ⟨e, run_liftME_err (run_liftME_err (run_liftME_err hd)), hRel⟩, J.anyX arI⟩ + have hV := J.1.1.vsc _ hslot.isSlot + rw [capV_of_capAt hc] at hV + have h0 : asid ≠ 0#u64 := (hV.2.2 rfl).1 + change syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Pml4 ptr true asid) + blk call don fp buf extra ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.Pml4 ptr true asid) excaps buffer) s res ⦄ + unfold syscall.decode_invocation + rw [hct] + simp only [bind_ok] + unfold vspace.decode + rw [hct] + simp only [bind_ok, if_neg hFlush, if_pos (show lbl = fl4_abi.label.X86PML4FrameBatch from rfl)] + unfold vspace.decode_frame_batch + rw [hct] + simp only [bind_ok, ↓reduceIte] + refine WP.spec_bind (check_vspace_fb s σ J.1.1.xtop.fin.2.1 ptr asid h0) ?_ + rintro r (⟨rfl, hf⟩ | ⟨e, rfl, hdec⟩) + · rw [fb_mapErr_ok] + simp only [bind_tc_ok, bind_ok, fb_branch_ok] + try simp only [WP.uncurry'_pair, uncurry_apply_pair, Aeneas.Std.uncurry] + try dsimp only + have hSmall := hReject.resolve_left (fun h => h hf) + have hShort : len < 1#u64 := by + show len.val < (1#u64 : U64).val + rw [hlen] + exact hSmall + simp only [hShort, ↓reduceIte, error.truncated, bind_ok, WP.spec_ok] + exact fail _ .truncatedMessage (Or.inl trivial) (by + dsimp only [dec] + rw [decodeFrameBatch_run hf, fbDecP_short hSmall]) + · rw [fb_mapErr_err] + simp only [bind_tc_ok, bind_ok, fb_branch_err, fb_fromResidual, WP.spec_ok] + exact fail _ _ (Or.inr ⟨0#u64, rfl, rfl⟩) (hdec args excaps) + +/-- The ordinary or first-phase call reaches its selected PML4, outside +the FrameBatch label. -/ +theorem VmEptState.selectedNativePml4NonBatchS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) (hAT : P0.ArchTotal arI) + {lbl : U64} (hBatch : lbl ≠ fl4_abi.label.X86PML4FrameBatch) + {ct cptr len buf ptr asid : U64} {mapped : Bool} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Pml4 ptr mapped asid) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Pml4 ptr mapped asid) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pml4 ptr mapped asid) exs buffer) s res ⦄ := + J.selectedNativePml4NonBatch hni hRef hAT hBatch q.current q.absSlot q.capAt ex args + (Fl4Spec.toBl cptr.bv) exs buffer on rc blk call don fp + +/-- Rejected control invocation using the extras actually read in the +matched syscall prefix. -/ +theorem VmEptState.selectedAsidControlRejectS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {lbl ct cptr len buf : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl .AsidControl ex args exs) + (hReject : lbl ≠ fl4_abi.label.X86ASIDControlMakePool ∨ exs.length ≤ 1) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) .AsidControl blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .AsidControl exs buffer) s res ⦄ := + J.selectedAsidControlReject q.current ex args (Fl4Spec.toBl cptr.bv) exs q.extra hReject buffer on rc blk call don fp + +/-- The selected unmapped PML4 fails before any batch read. -/ +theorem VmEptState.selectedFrameBatchUnmappedS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf ptr asid : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Pml4 ptr false asid) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PML4FrameBatch len (cslotAddr sl) + (.Pml4 ptr false asid) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PML4FrameBatch.bv args (Fl4Spec.toBl cptr.bv) sl + (.Pml4 ptr false asid) exs buffer) s res ⦄ := + J.selectedFrameBatchUnmapped q.current ex args (Fl4Spec.toBl cptr.bv) exs buffer on rc blk call don fp + +/-- The selected mapped PML4 fails before the header if its ASID lookup +fails or its actual message has no header word. -/ +theorem VmEptState.selectedFrameBatchBeforeHeaderS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf ptr asid : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Pml4 ptr true asid) ex args exs) + (hReject : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) asid.bv) σ ≠ ({(.ok ptr.bv, σ)}, False) ∨ + args.length < 1) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86PML4FrameBatch len (cslotAddr sl) + (.Pml4 ptr true asid) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86PML4FrameBatch.bv args (Fl4Spec.toBl cptr.bv) sl + (.Pml4 ptr true asid) exs buffer) s res ⦄ := by + have hlen : len.val = args.length := + q.length.trans (List.length_map (fun x : Fl4Spec.Data => (⟨x⟩ : U64))) + exact J.selectedFrameBatchBeforeHeader q.current q.absSlot q.capAt ex args (Fl4Spec.toBl cptr.bv) exs buffer + hlen hReject on rc blk call don fp + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Pool.lean b/proofs/Fl4Proofs/P3/VmEntryS3Pool.lean new file mode 100644 index 000000000..d97b194ed --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Pool.lean @@ -0,0 +1,565 @@ +/- +The native PML4 assignment route in an ASID pool shared with EPT. +Its write carries the native tag, so guest-tagged entries and EPT paths +are preserved. The proof uses the selected first extra cap's source slot. +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.FrontEndLabel + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched +open Fl4Proofs.P1 +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable (arI : machine.Arch A) {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- Assign the actual unmapped PML4 extra cap in a combined native and EPT pool. +This specializes the shared native VSpace correctness theorem, with no native +kernel-state restriction. -/ +theorem JAtZB.nativePoolAssignX (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) + (ptr base : U64) {slot : U64} (hsl : IsSlot Z.th slot) (hcap : capAt (L.mem s) slot = ok (.AsidPool ptr base)) + (length : U64) (call : Bool) (buf : U64) (ex : Array obj.Slot 3#usize) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + (hex : vspace.extra ex 0#usize = ok (some (cslotAddr (pc.bv, idx)))) + {ptr1 asid0 : U64} (hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Pml4 ptr1 false asid0)) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (rest : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + let dec := (Fl4Spec.archDecodeInvocation (Z := Unit) fl4_abi.label.X86ASIDPoolAssign.bv args ci sl + (.asidPoolCap ptr.bv base.bv) ((.archObjectCap (.pml4Cap ptr1.bv none), (pc.bv, idx)) :: rest)).run σ + vspace.decode st arI s a fl4_abi.label.X86ASIDPoolAssign length slot (.AsidPool ptr base) call buf ex ⦃ res => + (∃ e e', res.1 = .Err e ∧ VsErrRel e e' ∧ dec = ({(.error e', σ)}, False) ∧ res.2.1 = s ∧ res.2.2 = a) ∨ + (res.1 = .Ok () ∧ ¬ dec.2 ∧ + ∃ asid σ', (.ok (.invokeASIDPool (.assign asid ptr.bv (pc.bv, idx))), σ) ∈ dec.1 ∧ + AssignSpec σ asid ptr.bv (pc.bv, idx) σ' ∧ + XTopAt (L := L) AR Z res.2.1 res.2.2 σ' ∧ + P1.WritesIn st s res.2.1 (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s) ∧ + SlotFoot Z.th (L.mem s) (L.mem res.2.1) (cslotAddr (pc.bv, idx)) ∧ KFoot σ σ' ∧ + VsLike (capV (L.mem res.2.1) (cslotAddr (pc.bv, idx))) (.Pml4 ptr1 false asid0) ∧ + AssignF (L.mem res.2.1) (cslotAddr (pc.bv, idx)) (.Pml4 ptr1 false asid0) σ σ') ⦄ := by + dsimp only + have hX := J.xtop + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := hX.fin + obtain ⟨-, hCR, hFI, hDR, hMO, hIA, -⟩ := hX.rel + obtain ⟨hcur, xct, hkt, hrun⟩ := J.curRunnable hni (P1.cur_thread_ok (L := L) s) + have htcb : Z.th (L.curW s) = some .tcb := (hX.curTcb hcur).1 + obtain ⟨hSt, hSV, -, -, -⟩ := id hCR + have hw := J.vsc slot hsl + rw [capV_of_capAt hcap] at hw + simp only [VCapW, VCapOK] at hw + have hcan : Canonical ptr1 := by + have h := hcx + rw [capAt_words] at h + exact decode_encWf h + have hxs : IsSlot Z.th (cslotAddr (pc.bv, idx)) := + AbsSlot.isSlot (th := Z.th) (sl := (pc.bv, idx)) ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hw1 := J.vsc _ hxs + rw [capV_of_capAt hcx] at hw1 + have hL4 : ptr1 ∈ Z.L4 := hw1.1 rfl + have hkt' : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have hst1 : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), stsAct (updK σ (L.curW s).bv (.tcb { xct with tcbState := .restart })) (L.curW s).bv)}, False) := by + rw [hcur]; exact Notif.setThreadState_eq .restart hkt + have hH := VsInvHyps.ptTop (L1 := Z.L1) (L2 := Z.L2) (L3 := Z.L3) (L4 := Z.L4) (A := A) (N := Z.N) (T := Z.T) + (C := Z.C) (E := Z.E) (R := Z.R) (B := Z.B) (c := Z.c) (AR := AR) + (Vs.VsInvHyps.foot (s0 := s) (W := VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h.toW) hS.2.pl + (fun _ h => Or.inr (Or.inr (Or.inr (Or.inl h)))) (vsInvHyps_top curT_top)) + refine WP.spec_mono (assign_rel (ar := arI) hH s a σ hS.2.pl hS.2.vs hS.2.sd hSt hSV hR + ⟨⟨hS, P1.WritesIn.refl _ _⟩, hPt⟩ hkt' hrun ptr base hw.2.1 hw.2.2 + ((pool_label fl4_abi.label.X86ASIDPoolAssign).1 rfl) args ci sl rest length slot call buf + ex hpc hidx hex hcx hcan hw1.2.1 + (fun ct' h => by rw [show ct' = L.curW s from P1.eq_of_bv (h.trans hcur)]; exact htcb) J.kern.act hS.2.vo) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | + ⟨h1, hnf, asid, σ', hmem, hAS, hSt', hSV', hR', hSRq, hF, hL, hK, hAW, hAF⟩) + · exact Or.inl ⟨e, e', h1, he, hdec, h2, h2a⟩ + refine Or.inr ⟨h1, hnf, asid, σ', hmem, hAS, ?_, hSRq.1.2, hF, hK, hL, hAF⟩ + obtain ⟨hCR', hFI', hMO'⟩ := crelM_after hCR hFI hMO hcx hSt' hSV' hF hL hK rfl + have hks' : L.ksb res.2.1 = L.ksb s := hSRq.1.2.1 + have hPI' : PoolsIn Z.P Z.L4 (L.mem res.2.1) := by + intro p hp lo hlo hne + by_cases e : L.mem res.2.1 (peA p lo) = L.mem s (peA p lo) + · rw [e] at hne ⊢ + exact hPI p hp lo hlo hne + · rw [hAW _ (Or.inr ⟨p, hp, lo, hlo, rfl⟩) e] + exact fun _ => hL4 + have hFin : FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR (L.ksb s) σ res.2.1 res.2.2 σ' := + ⟨⟨hSRq.1.1, hR', hSRq.2, hPI', CurOK.of_inv hCur hK hCR'.2.2.1 hMO'.2.2⟩, + ⟨Z.G, hCR', hFI', by rw [hks']; exact hMO'⟩, hks', KFoot.szKeep hK⟩ + obtain ⟨-, hdw⟩ := dom_of_foot hS (Or.inr hSRq.1.2) + have hF1 := vsSFr_of_post hst1 (Or.inr ⟨_, _, _, hAS⟩) + have hd1 := setThreadState_dom hkt' hst1 + have hD' : DomRel (L.mem res.2.1) (L.ksb res.2.1) σ' := by + rw [hks'] + exact hDR.frame hdw (hF1.dl.trans hd1.1) (hF1.dsi.trans hd1.2) + have hXR := FinXT.toXR Z.G hX.esa hIA.dom hFin hD' + obtain ⟨hFin2, ⟨G0, hPl2, -, -, hD2, -, hIA2, hSA2⟩, hE⟩ := hXR + obtain ⟨⟨⟨hSch, hEp, hNt, -⟩, -⟩, -⟩ := id hFin2 + exact ⟨hFin2, ⟨hPl2, hCR', hFI', hD2, by rw [hks']; exact hMO', hIA2, hSA2⟩, IpcRel.of hNt hEp (sched_sc hSch), hE⟩ + + +private theorem nativeAssignAfterRestart {m : U64 → U64} {x : U64} {c : capability.Cap} + {σ1 σ' : Fl4Spec.State Unit} {ct : Fl4Spec.ObjRef} {y y1 : Fl4Spec.Tcb} + (hA : AssignF m x c σ σ') (hk : ∀ o, o ≠ ct → σ1.kheap o = σ.kheap o) + (hct : σ.kheap ct = some (.tcb y)) (hct1 : σ1.kheap ct = some (.tcb y1)) + (har : σ1.archState = σ.archState) : AssignF m x c σ1 σ' := by + obtain ⟨ptr, a0, asid, pool, hi, lo, f, hcv, hcv', hhi, htab, hlo, hval, hnz, hkp, hkp', hAK, htab'⟩ := hA + have hpc : pool.bv ≠ ct := fun e => by rw [e, hct] at hkp; cases hkp + refine ⟨ptr, a0, asid, pool, hi, lo, f, hcv, hcv', hhi, by rw [har]; exact htab, + hlo, hval, hnz, by rw [hk _ hpc]; exact hkp, hkp', ?_, by rw [har]; exact htab'⟩ + rintro q ⟨ao, hq⟩ hne + have hqc : q.bv ≠ ct := fun e => by rw [e, hct1] at hq; cases hq + rw [hk _ hqc] at hq ⊢ + exact hAK q ⟨ao, hq⟩ hne + +private theorem nativeAssignSize {m : U64 → U64} {x : U64} {c : capability.Cap} + {σ' : Fl4Spec.State Unit} (hA : AssignF m x c σ σ') (hK : KFoot σ σ') + (o : Fl4Spec.ObjRef) (ko' : Fl4Spec.KernelObject) (h' : σ'.kheap o = some ko') : + ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko := by + obtain ⟨_, _, _, pool, _, _, _, -, -, -, -, -, -, -, hkp, hkp', hAK, -⟩ := hA + by_cases e : σ'.kheap o = σ.kheap o + · exact ⟨ko', by rw [← e]; exact h', rfl⟩ + rcases hK.2.2.1 o e with ⟨a0, a1, h0, h1⟩ | ⟨r, cs, cs', h0, -, h1⟩ | ⟨t, t', h0, h1⟩ + · by_cases hop : o = pool.bv + · subst hop + rw [hkp'] at h'; cases h' + exact ⟨_, hkp, rfl⟩ + · exact absurd (hAK ⟨o⟩ ⟨a0, h0⟩ (fun h => hop (by rw [← h]))) e + · rw [h1] at h'; cases h'; exact ⟨_, h0, rfl⟩ + · rw [h1] at h'; cases h'; exact ⟨_, h0, rfl⟩ + +private theorem nativeAssignCap {m : U64 → U64} {x ptr asid0 : U64} + {σ' : Fl4Spec.State Unit} (hA : AssignF m x (.Pml4 ptr false asid0) σ σ') : + ∃ asid, capV m x = .Pml4 ptr true asid := by + obtain ⟨q, a, asid, _, _, _, _, hc, hc', -⟩ := hA + cases hc + exact ⟨asid, hc'⟩ + +private theorem nativeAssignPoolAt {m : U64 → U64} {x ptr asid0 : U64} + {σ' : Fl4Spec.State Unit} (hA : AssignF m x (.Pml4 ptr false asid0) σ σ') + (hK : KFoot σ σ') (hTag : ptr.bv &&& 1 = 0) + {hi : Fl4Spec.AsidHighIndex} {lo : Fl4Spec.AsidLowIndex} {e : Fl4Spec.ObjRef} + (hp : PoolAt σ' hi lo e) (he : e &&& 1 = 1) : PoolAt σ hi lo e := by + obtain ⟨q, a, _, pool, _, lo0, f, hc, -, -, -, -, -, -, hk, hk', hAK, htab⟩ := hA + cases hc + obtain ⟨p, g, ht, hkg, hge⟩ := hp + rw [htab] at ht + by_cases hpp : p = pool.bv + · subst p + rw [hk'] at hkg + injection hkg with hkg + injection hkg with hkg + injection hkg with hkg + subst g + by_cases hl : lo = Fl4Spec.ucast 9 lo0.bv + · subst lo + rw [Function.update_self] at hge + injection hge with hh + rw [← hh, hTag] at he + cases he + · rw [Function.update_of_ne hl] at hge + exact ⟨pool.bv, f, ht, hk, hge⟩ + · exact ⟨p, g, ht, archKept_of hAK hK hpp hkg, hge⟩ + +/-- Frame the EPT state across a native ASID-pool write. Only the set of +guest-tagged entries matters to the guest walk and unmapped EPT bounds. -/ +private theorem EptCore.frameNativePool {th : TypedHeap} + (C : EptCore (L := L) X XL th s σ) + {s' : S} {σ' : Fl4Spec.State Unit} {W : U64 → Prop} {x ptr asid0 : U64} + (hw : P1.WritesIn st s s' W) + (hW : ∀ b, W b → ∀ t ∈ X, b.val < t.val ∨ t.val + 4096 ≤ b.val) + (hcap : ∀ y, IsSlot th y → y ≠ x → capV (L.mem s') y = capV (L.mem s) y) + (hcx : capV (L.mem s) x = .Pml4 ptr false asid0) + (hA : AssignF (L.mem s') x (.Pml4 ptr false asid0) σ σ') (hK : KFoot σ σ') + (hTag : ptr.bv &&& 1 = 0) (h5 : EptSlotStep th th (L.mem s) (L.mem s')) : + EptCore (L := L) X XL th s' σ' := by + obtain ⟨asid, hcx'⟩ := nativeAssignCap hA + obtain ⟨_, _, _, pool, _, _, _, -, -, -, -, -, -, -, hkp, hkp', hAK, htab⟩ := id hA + have hxk : ∀ t ∈ X, σ'.kheap t.bv = σ.kheap t.bv := by + intro t ht + obtain ⟨es, hk, -⟩ := C.rel.obj t ht + have hne : t ≠ pool := fun e => by subst e; rw [hk] at hkp; cases hkp + exact hAK t ⟨_, hk⟩ hne + have hkBack : ∀ o l es, σ'.kheap o = some (.archObj (.ept l es)) → + σ.kheap o = some (.archObj (.ept l es)) := by + intro o l es ho + exact archKept_of hAK hK (fun e => by subst o; rw [hkp'] at ho; cases ho) ho + have hpool : ∀ hi lo e, PoolAt σ' hi lo e → e &&& 1 = 1 → PoolAt σ hi lo e := + fun _ _ _ => nativeAssignPoolAt hA hK hTag + have hge : ∀ e, GuestEntry σ' e → e &&& 1 = 1 → GuestEntry σ e := by + rintro e ⟨hi, p, f, lo, ht, hk, he⟩ htag + exact (hpool hi lo e ⟨p, f, ht, hk, he⟩ htag).guest + have hcb : ∀ y p l (mm : Bool) a v, IsSlot th y → capV (L.mem s') y = .Ept p l mm a v → + capV (L.mem s) y = .Ept p l mm a v := by + intro y p l mm a v hy hc + by_cases h : y = x + · subst y; rw [hcx'] at hc; cases hc + · rwa [← hcap y hy h] + have hck : ∀ y p l (mm : Bool) a v, IsSlot th y → capV (L.mem s) y = .Ept p l mm a v → + capV (L.mem s') y = .Ept p l mm a v := by + intro y p l mm a v hy hc + by_cases h : y = x + · subst y; rw [hcx] at hc; cases hc + · rwa [hcap y hy h] + have hmem : ∀ t ∈ X, ∀ i : BitVec 9, L.mem s' (eptA t i) = L.mem s (eptA t i) := by + intro t ht i + have hp := C.rel.place t ht + have ha := hp.1 + have e := eptA_val hp.2 i + have hi := i.isLt + exact hw.mem_eq (b := eptA t i) (by unfold P1.Aligned; rw [e]; omega) + (fun hb => by rcases hW _ hb t ht with h | h <;> omega) + have hX' : JExtEpt (L := L) X th s' := + ⟨fun y hy => by + by_cases h : y = x + · rw [h, hcx']; trivial + · rw [hcap y hy h]; exact C.exte.cap y hy, + ⟨fun y hy p l a v hc => C.exte.vtop.1 y hy p l a v (hcb y p l true a v hy hc), + fun y hy p sz r d a v hc => by + by_cases h : y = x + · subst y; rw [hcx'] at hc; cases hc + · exact C.exte.vtop.2 y hy p sz r d a v (by rw [← hcap y hy h]; exact hc)⟩, + fun y hy p l a v hc => C.exte.anz y hy p l a v (hcb y p l true a v hy hc)⟩ + refine + { rel := ⟨fun t ht => ?_, fun r l es hk => C.rel.spec r l es (hkBack r l es hk), C.rel.place⟩ + capL := fun y hy p l a v mm hc => C.capL y hy p l a v mm (hcb y p l mm a v hy hc) + exte := hX' + tag := fun e he ht => C.tag e (hge e he ht) ht + ks := fun t ht => by rw [hw.1]; exact C.ks t ht + sep := fun t ht o ko ho hne => ?_ + ue := ?_ + eslot := C.eslot.step h5 + walk := C.walkZ (Z0 := ∅) (fun t ht _ => hxk t ht) (fun _ h => h.elim) + (fun r l es hk => Or.inr ⟨l, es, hkBack r l es hk⟩) (fun e he ht => Or.inl (hge e he ht)) + named := C.named.frameA htab hpool hkBack + (fun y p l a v hy hc _ => ⟨y, hy, hck y p l true a v hy hc⟩) + (fun y p l mm a v hy hc => ⟨y, hy, hcb y p l mm a v hy hc⟩) + (fun _ _ _ _ _ _ y p l a v hy _ hl hc => ⟨y, l, a, v, hy, hl, hck y p l true a v hy hc⟩) + tbl := C.tbl } + · obtain ⟨es, hk, hf⟩ := C.rel.obj t ht + exact ⟨es, (hxk t ht).trans hk, fun i hi => by rw [hmem t ht i]; exact hf i hi⟩ + · obtain ⟨old, hk, hb⟩ := nativeAssignSize hA hK o ko ho + unfold EptApart + rw [hb] + exact C.sep t ht o old hk hne + · intro y hy p l a v hc + have hc0 := hcb y p l false a v hy hc + have hp := C.exte.cap y hy + rw [hc0] at hp + obtain ⟨hm, hk, huniq, hroot⟩ := C.ue y hy p l a v hc0 + exact ⟨fun i => by rw [hmem p hp i]; exact hm i, (hxk p hp).trans hk, + fun y' hy' hne l' mm a' v' hc' => huniq y' hy' hne l' mm a' v' (hcb y' p l' mm a' v' hy' hc'), + fun e he ht => hroot e (hge e he ht) ht⟩ + + +set_option maxHeartbeats 8000000 in +/-- The matched PML4 assignment leaves the VM relation after its single +restart and native pool update. The pool's native tag follows from PML4 +placement. There is no condition on other EPT or VCPU objects. -/ +theorem VmEptState.nativePoolAssign (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) + (ptr base : U64) {slot : U64} (hsl : IsSlot Z.th slot) (hcap : capAt (L.mem s) slot = ok (.AsidPool ptr base)) + (length : U64) (call : Bool) (buf : U64) (ex : Array obj.Slot 3#usize) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + (hex : vspace.extra ex 0#usize = ok (some (cslotAddr (pc.bv, idx)))) + {ptr1 asid0 : U64} (hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Pml4 ptr1 false asid0)) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (rest : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) : + let dec := (Fl4Spec.archDecodeInvocation (Z := Unit) fl4_abi.label.X86ASIDPoolAssign.bv args ci sl + (.asidPoolCap ptr.bv base.bv) ((.archObjectCap (.pml4Cap ptr1.bv none), (pc.bv, idx)) :: rest)).run σ + vspace.decode st arI s a fl4_abi.label.X86ASIDPoolAssign length slot (.AsidPool ptr base) call buf ex ⦃ res => + (∃ e e', res.1 = .Err e ∧ VsErrRel e e' ∧ dec = ({(.error e', σ)}, False) ∧ res.2.1 = s ∧ res.2.2 = a) ∨ + (res.1 = .Ok () ∧ ¬ dec.2 ∧ + ∃ asid σ', (.ok (.invokeASIDPool (.assign asid ptr.bv (pc.bv, idx))), σ) ∈ dec.1 ∧ + AssignSpec σ asid ptr.bv (pc.bv, idx) σ' ∧ + ∃ Lst' : U64 → List U64, VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D res.2.1 res.2.2 σ') ⦄ := by + dsimp only + have B := J.1.1 + refine WP.spec_mono (JAtZB.nativePoolAssignX arI B hni ptr base hsl hcap length call buf ex + hpc hidx hex hcx args ci sl rest) ?_ + rintro ⟨res1, s', a'⟩ (⟨e, e', h1, he, hdec, h2, h2a⟩ | + ⟨h1, hnf, asid, σ', hmem, hAS, hX', hW0, hSF, hK, hLike, hAF⟩) + · exact Or.inl ⟨e, e', h1, he, hdec, h2, h2a⟩ + refine Or.inr ⟨h1, hnf, asid, σ', hmem, hAS, ?_⟩ + simp only at hX' hW0 hSF hAF hLike ⊢ + set x := cslotAddr (pc.bv, idx) with hxdef + -- 1. the restart + obtain ⟨s1, σ1, Lst1, hs1, hst1, J1⟩ := VmEptState.restart (arI := arI) J hni + have B1 := J1.1.1 + have Cv1 := J1.1.2 + have C1 := J1.2 + have hct := P1.cur_thread_ok (L := L) s + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have htcb : Z.th (L.curW s) = some .tcb := (B.xtop.curTcb hcur).1 + have e1 : σ1 = stsAct (updK σ (L.curW s).bv (.tcb { xct with tcbState := .restart })) (L.curW s).bv := by + have hst := hst1 + rw [hcur, Notif.setThreadState_eq .restart hkt] at hst + have := congrArg (fun q => ((), σ1) ∈ q.1) hst + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at this + exact of_eq_true this + have hk1 : ∀ o, o ≠ (L.curW s).bv → σ1.kheap o = σ.kheap o := by + intro o ho; rw [e1, stsAct_kheap]; simp [updK, ho] + have hct1 : σ1.kheap (L.curW s).bv = some (.tcb { xct with tcbState := .restart }) := by + rw [e1, stsAct_kheap]; simp [updK] + have har1 : σ1.archState = σ.archState := by rw [e1]; unfold stsAct; split <;> rfl + have F : VsSFr σ1 σ' := vsSFr_of_post hst1 (Or.inr ⟨_, _, _, hAS⟩) + have hAF1 : AssignF (L.mem s') x (.Pml4 ptr1 false asid0) σ1 σ' := + nativeAssignAfterRestart hAF hk1 hkt hct1 har1 + have hK1 : KFoot σ1 σ' := KFoot.ofTcb hK hk1 hkt hct1 F.cdt F.orig F.ct + -- 2. FL4's writes after the restart (`JAtZ.vsPostJ`'s pins) + have hP := B.xtop.rel.1 + have hact := B.kern.act + have hF2 : Frame2 (L := L) s s1 (P1.stA (L.curW s)) (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have h := set_thread_state_frame (L := L) s (L.curW s) sched.TS_RESTART (stA_al hP htcb) hact.1 + rw [hs1, WP.spec_ok] at h; exact h + have hSS1 : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := set_thread_state_slots (L := L) Z.th hP s (L.curW s) sched.TS_RESTART htcb hact + rw [hs1, WP.spec_ok] at h; exact h + have hkc1 : L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s := by + have h := set_thread_state_cases (L := L) s (L.curW s) sched.TS_RESTART + rw [hs1, WP.spec_ok] at h + rcases h with rfl | rfl <;> simp + have hks' : L.ksb s' = L.ksb s := hW0.1 + have pinS : ∀ t, Z.th t = some .tcb → L.mem s' (P1.stA t) = L.mem s1 (P1.stA t) := by + intro t ht + have htT := B.thT t ht + obtain ⟨y1, hy1, hR1⟩ := B1.xtop.ipc.tcb t htT + obtain ⟨y, hky, hRy⟩ := hX'.ipc.tcb t htT + obtain ⟨y', hy', hc⟩ := F.tcbF hy1 + rw [hky] at hy'; cases hy' + have h1 : (L.mem s' (tStA t)).val = (L.mem s1 (tStA t)).val := by rw [hRy.st, hR1.st, hc.st] + rw [stA_tStA]; exact UScalar.eq_of_val_eq h1 + have pinA : L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) = + L.mem s1 (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have a1 := B1.xtop.fin.1.1.1.act + have a2 := hX'.fin.1.1.1.act + rw [F.act] at a2 + have e := Sched.ActRel.det a2 a1 + obtain ⟨Q, R, hc, -⟩ := B.xtop.fin.1.1.1.lists + unfold nodeW at e + rw [P1.nodeAt_of_ksb hks', P1.nodeAt_of_ksb hkc1.1, ← hc.cur] at e + exact e + have hSlot : ∀ y, IsSlot Z.th y → L.mem s' (wordOf y 16#u64) = L.mem s1 (wordOf y 16#u64) ∧ + L.mem s' (wordOf y 24#u64) = L.mem s1 (wordOf y 24#u64) ∧ + (y ≠ x → L.mem s' y = L.mem s1 y ∧ L.mem s' (wordOf y 8#u64) = L.mem s1 (wordOf y 8#u64)) := by + intro y hy + obtain ⟨a0, a8, a16, a24⟩ := hSS1 y hy + obtain ⟨b16, b24, bx⟩ := hSF y hy + exact ⟨by rw [b16, a16], by rw [b24, a24], fun hne => ⟨by rw [(bx hne).1, a0], by rw [(bx hne).2, a8]⟩⟩ + have hW : P1.WritesIn st s1 s' (VsXW Z (L.ksb s1) x) := by + refine ⟨hks'.trans hkc1.1.symm, hW0.2.1.trans hkc1.2.symm, fun b hb hn => ?_⟩ + have hslw : ∀ y, IsSlot Z.th y → ∀ k : U64, Off k → b = wordOf y k → L.mem s' b = L.mem s1 b := by + intro y hy k hk e + subst e + obtain ⟨h16, h24, hne⟩ := hSlot y hy + rcases hk with rfl | rfl | rfl | rfl + · by_cases ex : y = x + · exact (hn (Or.inr (Or.inl (by rw [wordOf_zero, ex])))).elim + · rw [wordOf_zero]; exact (hne ex).1 + · by_cases ex : y = x + · exact (hn (Or.inr (Or.inr (by rw [ex])))).elim + · exact (hne ex).2 + · exact h16 + · exact h24 + by_cases hb0 : VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b + · rcases hb0 with ⟨y, hy, e | e⟩ | ⟨y, k, hy, hk, e⟩ | hpt | hvw | ⟨t, ht, e⟩ | e + · exact hslw y hy 0#u64 (Or.inl rfl) (by rw [wordOf_zero]; exact e) + · exact hslw y hy 8#u64 (Or.inr (Or.inl rfl)) e + · exact hslw y hy k hk e + · exact (hn (Or.inl (Or.inl hpt))).elim + · exact (hn (Or.inl (Or.inr (by rw [hkc1.1]; exact hvw)))).elim + · subst e; exact pinS t ht + · subst e; exact pinA + · have e0 : L.mem s' b = L.mem s b := hW0.2.2 b hb hb0 + have e1 : L.mem s1 b = L.mem s b := hF2 b hb (fun e => hb0 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl + ⟨L.curW s, htcb, UScalar.eq_of_val_eq e⟩)))))) (fun e => hb0 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + (UScalar.eq_of_val_eq e))))))) + rw [e0, e1] + have hcv0 : capV (L.mem s) x = .Pml4 ptr1 false asid0 := capV_of_capAt hcx + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hxs : IsSlot Z.th x := hA.isSlot + have hcv1 : capV (L.mem s1) x = .Pml4 ptr1 false asid0 := by rw [hSS1.capV hxs]; exact hcv0 + obtain ⟨a1, hcvN⟩ := nativeAssignCap hAF1 + have hcap' : ∀ y, IsSlot Z.th y → y ≠ x → capV (L.mem s') y = capV (L.mem s1) y := by + intro y hy hne + obtain ⟨-, -, h⟩ := hSlot y hy + unfold capV; rw [(h hne).1, (h hne).2] + have Ap : VsApart (L := L) Z H Cs s1 := ⟨B1.scOut⟩ + have hw1 := B.vsc x hxs + rw [hcv0] at hw1 + have hL4 : ptr1 ∈ Z.L4 := hw1.1 rfl + have hvc : VsSlotsW Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s') := by + intro y hy + by_cases e : y = x + · rw [e] + exact (hLike.2 _ _ _ _ _ (hw1.okTab rfl)).toW + · rw [hcap' y hy e]; exact B1.vsc y hy + have hvn : Z.vx = ∅ → VsNamed Z.th (L.mem s') (L.ksb s') := fun hvx => by + have hR' : VsRel Z.P (L.mem s') (L.ksb s) σ' := by rw [← hks']; exact hX'.fin.2.1 + rw [hks'] + exact (B.carry.vsNamed hvx).ofAssignF B.vsc B.xtop.fin.2.1 B.xtop.fin.2.2.1 + B.xtop.fin.2.2.2.1 B.xtop.fin.1.2.pt hR' hX'.fin.2.2.1 hxs hcv0 hSF hAF + have hsh : shape (capV (L.mem s') x) = shape (capV (L.mem s1) x) := by + rw [hcv1]; exact hLike.1.1 + have B' : JAtZB (L := L) arI AR Z H Cs Ac Lst1 D s' a' σ' := + B1.vsStepJXB Ap hpc hidx (TabsArch.empty σ1) (L3' := Z.L3) (L2' := Z.L2) (L1' := Z.L1) hX' F + (hW.mono fun _ h => Or.inl h) hcap' hsh (by rw [hcv1]; exact hLike.1.2.1) + (by rw [hcv1]; rfl) (fun y hy => ⟨(hSlot y hy).1, (hSlot y hy).2.1⟩) hvc + ⟨subset_rfl, subset_rfl, subset_rfl⟩ hvn + have sw : ∀ b, (b = x ∨ b = wordOf x 8#u64) → SlotWd Z.th b := by + rintro b (rfl | rfl) + · exact ⟨x, hxs, 0#u64, Or.inl rfl, by rw [wordOf_zero]⟩ + · exact ⟨x, hxs, 8#u64, Or.inr (Or.inl rfl), rfl⟩ + obtain ⟨_, _, _, pool, _, _, _, -, -, -, -, -, -, -, hkp1, hkp1', hAK1, -⟩ := id hAF1 + have nV : ∀ u ∈ V, ∀ b, VsXW Z (L.ksb s1) x b → b.val < u.val ∨ u.val + 32768 ≤ b.val := by + intro u hu b hb + rcases hb with hb | hb + · obtain ⟨vx, hvx⟩ := Cv1.vm.spec u hu + have := B1.vsW_apart (t := u) (n := 32768) (fun o ko hk hne => Cv1.vm.sep u hu o ko hk hne) (Cv1.vm.ks u hu) + (fun q hq e => by + obtain ⟨ao, hk, -, -, hnv⟩ := B1.vsFrSpec hq + rw [e, hvx] at hk; cases hk; exact hnv vx rfl) hb + omega + · exact B1.slotWd_vcpu Cv1.vm (sw b hb) u hu + have nX : ∀ b, VsXW Z (L.ksb s1) x b → ∀ t ∈ X, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + rcases hb with hb | hb + · obtain ⟨es, hxe, -⟩ := C1.rel.obj t ht + have := B1.vsW_apart (t := t) (n := 4096) (fun o ko hk hne => C1.sep t ht o ko hk hne) (C1.ks t ht) + (fun q hq e => by + obtain ⟨ao, hk, -, hne, -⟩ := B1.vsFrSpec hq + rw [e, hxe] at hk; cases hk; exact hne _ es rfl) hb + omega + · exact JAtZB.slotWd_ept B1 C1 b (sw b hb) t ht + have hv : VKeep (L := L) Z.th s1 s' := by + intro t ht + have htT := B1.thT t ht + obtain ⟨Q, R0, hcq, -⟩ := B1.xtop.fin.1.1.1.lists + have hT := hcq.tcbs + have ev := tVcpuA_val hT htT + refine hW.mem_eq (tVcpuA_aligned hT htT) fun hb => ?_ + rcases hb with hb | hb + · have hP1 := B1.xtop.fin.1.2 + have hO : OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s1 σ1 (tVcpuA t) := + Or.inr (Or.inr (Or.inl ⟨t, htT, by omega, by omega⟩)) + have hvo : VObj Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s1) (tVcpuA t) := by + rcases hb with ⟨q, hq, i, hi, e⟩ | h + · rw [e]; exact table_vobj hP1.pt hq hi _ + · exact vsWrite_vobj hP1.vs h + exact hP1.disj _ hO hvo + · exact B1.slotWd_tcb (sw _ hb) ht (by omega) (by omega) rfl + have hvk : ∀ u ∈ V, σ'.kheap u.bv = σ1.kheap u.bv := fun u hu => by + obtain ⟨vx, hvx⟩ := Cv1.vm.spec u hu + have hne : u ≠ pool := fun e => by subst e; rw [hkp1] at hvx; cases hvx + exact hAK1 u ⟨_, hvx⟩ hne + have hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ1.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM := fun o x' ho hx => by + rcases F.kh o with e | ⟨a0, a1, -, h1, -⟩ | ⟨r', cs0, cs1, -, h1⟩ | ⟨y0, y1, h0, h1, hco⟩ + · exact ⟨x', by rw [← e]; exact ho, hx⟩ + · rw [h1] at ho; cases ho + · rw [h1] at ho; cases ho + · rw [h1] at ho; cases ho; exact ⟨y0, h0, by rw [← hco.1]; exact hx⟩ + have hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ1.kheap p = some (.archObj (.vcpu vs)) := fun p vs hp => + archKept_of hAK1 hK1 (fun e => by subst e; rw [hkp1'] at hp; cases hp) hp + have hcs' : ∀ y, IsSlot Z.th y → capV (L.mem s') y = capV (L.mem s1) y ∨ + (shape (capV (L.mem s') y) = shape (capV (L.mem s1) y) ∧ VcpuCapV V (capV (L.mem s') y) ∧ + ∀ f l o, capV (L.mem s') y ≠ .IoPort f l o) := by + intro y hy + by_cases e : y = x + · subst y + refine Or.inr ⟨hsh, ?_⟩ + rw [hcvN] + exact ⟨trivial, fun _ _ _ e => by cases e⟩ + · exact Or.inl (hcap' y hy e) + have Cv' : VcpuCore (L := L) V Z H s' σ' := + Cv1.ofWG B1 B' hW hcs' nV (fun b hb => by + rcases hb with hb | hb + · exact B1.vsW_head hb + · exact B1.slotWd_head (sw b hb)) hv hvk (nativeAssignSize hAF1 hK1) hrv hnv + have hEptSlot : EptSlotStep Z.th Z.th (L.mem s1) (L.mem s') := by + intro t ht + have h := cnode_slot_ne_tcb B1.xtop.rel.1 hpc hidx ht 5 (by decide) + exact Or.inr (Or.inr ⟨ht, hcap' _ (isSlot_eptSlot ht) h.symm⟩) + have hTagVal : ptr1 &&& 1#u64 = 0#u64 := by + have ha := B.xtop.fin.1.2.pt ptr1 (Or.inl hL4) + apply UScalar.eq_of_val_eq + have he : (ptr1 &&& 1#u64).val = ptr1.val % 2 := by + rw [UScalar.val_and]; exact Nat.and_two_pow_sub_one_eq_mod ptr1.val 1 + rw [he] + show ptr1.val % 2 = 0 + omega + have hTag : ptr1.bv &&& 1 = 0 := (u_and_zero ptr1 1#u64).mp hTagVal + exact ⟨Lst1, ⟨B', Cv'⟩, C1.frameNativePool hW nX hcap' hcv1 hAF1 hK1 hTag hEptSlot⟩ + + +/-- The native PML4 route of the shared ASID-pool dispatcher. The source +slot was selected by the first extra-cap read. -/ +theorem VmEptState.nativePoolDecode + {MR : A → Fl4Spec.MachineState → Prop} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (ptr base : U64) {slot : U64} (hsl : IsSlot Z.th slot) (hcap : capAt (L.mem s) slot = ok (.AsidPool ptr base)) + (len : U64) (blk call don fp : Bool) (buf : U64) (ex : Array obj.Slot 3#usize) + {pc : U64} {r : Nat} (hpc : Z.th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + (hex : vspace.extra ex 0#usize = ok (some (cslotAddr (pc.bv, idx)))) + {ptr1 asid0 : U64} (hcx : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Pml4 ptr1 false asid0)) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (rest : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) + (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.X86ASIDPoolAssign len slot (.AsidPool ptr base) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp fl4_abi.label.X86ASIDPoolAssign.bv args ci sl (.AsidPool ptr base) + ((.archObjectCap (.pml4Cap ptr1.bv none), (pc.bv, idx)) :: rest) buffer) s res ⦄ := by + have hd : syscall.decode_invocation st arI s a fl4_abi.label.X86ASIDPoolAssign len slot (.AsidPool ptr base) + blk call don fp buf ex = vspace.decode st arI s a fl4_abi.label.X86ASIDPoolAssign len slot + (.AsidPool ptr base) call buf ex := by + unfold syscall.decode_invocation + rw [cur_thread_eq s] + simp only [bind_ok] + rw [hd] + let label := fl4_abi.label.X86ASIDPoolAssign.bv + let exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr) := + ((.archObjectCap (.pml4Cap ptr1.bv none), (pc.bv, idx)) :: rest) + have hr : ¬ Fl4Spec.VirtRoute fp label args (absCap (.AsidPool ptr base)) exs := by + show ¬ (_ ∧ false = true) + simp + have hs : specDecV fp label args ci sl (.AsidPool ptr base) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec + (Fl4Spec.archDecodeInvocation (Z := Unit) label args ci sl (.asidPoolCap ptr.bv base.bv) exs))) := by + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base fp label args ci sl + (absCap (.AsidPool ptr base)) exs buffer hr] <;> rfl + change _ ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp label args ci sl (.AsidPool ptr base) exs buffer) s res ⦄ + rw [hs] + refine WP.spec_mono (J.nativePoolAssign arI hni ptr base hsl hcap len call buf ex hpc hidx hex hcx args ci sl rest) ?_ + rintro res (⟨e, e', he, hr, hdec, hS, hAr⟩ | ⟨hok, hnf, asid, σ', hmem, hAS, Lst', J'⟩) + · exact Or.inl ⟨e, he, hS, ⟨e', run_liftME_err (run_liftME_err (run_liftME_err hdec)), hr⟩, + hS.symm ▸ hAr.symm ▸ J.anyX arI⟩ + · obtain ⟨-, σ1, -, -, -, -, -, hst, -⟩ := J.restartK hni + unfold AssignSpec at hAS + rw [nd_bind_one hst] at hAS + have hdec := DecOut.liftME (f := Fl4Spec.Fl4InvocationV.base) + (DecOut.liftME (f := Fl4Spec.Fl4Invocation.invokeArchObject) + (DecOut.liftME (f := Fl4Spec.Fl4ArchInvocation.spec) ⟨hmem, hnf⟩)) + refine Or.inr ⟨_, hdec, σ1, hst, (.ok [], σ'), ?_, Or.inl ⟨hok, rfl, J'.anyX arI⟩⟩ + have hp : ([], σ') ∈ + (Fl4Spec.archNoReturn (Fl4Spec.performAsidPoolInvocation (Z := Unit) (.assign asid ptr.bv (pc.bv, idx))) σ1).1 := by + unfold Fl4Spec.archNoReturn + rw [nd_bind_one hAS] + exact Set.mem_singleton _ + exact mem_liftE (ε := Unit) (x := ([], σ')) hp + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Recv.lean b/proofs/Fl4Proofs/P3/VmEntryS3Recv.lean new file mode 100644 index 000000000..fa56194c6 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Recv.lean @@ -0,0 +1,495 @@ +/- +P3, x86 VM stage 3. The receive dispatcher at the shared joint state. + +The projections used by `handle_recvJStG` are already available on `JAtZB`. +Use those projections to instantiate `handle_recv_refinesH` at the VM relation. +The lookup-fault leaf uses `VmEptState.handleFaultVX`. Polling a non-active +notification and receiving an active one use the shared register and +notification writes with their native footprints. The active bound-notification +endpoint case uses the same writes. The remaining [J] premise is at the selected +endpoint outside the polling and active bound-notification cases. The blocking +notification paths use `JAtZB.recvSigBlockB`, `JAtZB.recvSigBlockPreB` and +`JAtZB.maybeReturnB`. +-/ +import Fl4Proofs.P3.VmHandleX +import Fl4Proofs.P3.VmHandle +import Fl4Proofs.P3.VmMidBlock +import Fl4Proofs.P3.P3JRecvCls +import Fl4Proofs.P3.ScSignalJN +import Fl4Proofs.P3.XTopJointRecvIpc +import Fl4Proofs.P3.XTopJointRecvIpcAct +import Fl4Proofs.P3.XTopJointRecvSigRet + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp + Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The nonblocking receive of a non-active notification, including its VM and EPT frame. -/ +theorem VmEptState.recvSigPollVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} + (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (cb : Fl4Spec.Badge) (cr : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n false ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) false σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + refine WP.spec_mono (receive_signal_poll_eq (arI := arI) (a := a) hI.1 hn hk hna) ?_ + rintro r rfl + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hXb := badge_wr_irel B.ip hI ht hkt 0#u64 + rw [badgeRegA_eq_regA] at hXb + obtain ⟨B', F⟩ := B.regWrJFixed ht hkt Fl4Spec.badge_register 0#u64 hXb + have hspec : Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) false σ = + ({((), updK σ t.bv (.tcb (setReg xt Fl4Spec.badge_register (0#u64 : U64).bv)))}, False) := by + rw [receiveSignal_notActive hk hna] + unfold Fl4Spec.receiveSignalBlocked Fl4Spec.doNbrecvFailedTransfer + simp only [Bool.false_eq_true, ↓reduceIte] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt)] + rfl + rw [hspec, badgeRegA_eq_regA] + exact ⟨_, Set.mem_singleton _, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +set_option maxHeartbeats 8000000 in +/-- Direct receive and bound-notification completion share the badge and notification writes, at a resumed entry +or any mid state keeping `CurInv` (the current thread keeps running). -/ +theorem VmEptState.recvActiveMidVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) + (hC : CurInv σ) (hni : σ.curThread ≠ σ.idleThread) (hrun : CurRun σ) + (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blocking : Bool) (cb : Fl4Spec.Badge) (cr : Fl4Spec.CapRights) : + (notification.receive_signal st arI s a t n blocking ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blocking σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) ∧ + (notification.complete_signal st arI s a n t ⦃ r => + ∃ z ∈ (Fl4Spec.completeSignal (Z := Unit) n.bv t.bv σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hcur, hkt] at hkc + cases hkc + obtain ⟨ys, ms, hks⟩ := hC.sc + have hrn : Fl4Spec.runnable xt.tcbState = true := hrun xt (by rw [hcur]; exact hkt) + have hcb := B.canBlockInvB ht hcur hkt hC hni hrn (by rw [← hcur]; exact hnq) + (fun v hv e => hnsw (by rw [hv, e, hcur])) + have hx := hI.1.toN.at_ntfn hn hk + have hst : stOf (L.mem s) n = obj.NTFN_ACTIVE := stOf_active (T := Z.T) (hact ▸ hx.obj) rfl + have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := hx.obj + rw [hact] at this + exact this.2.2 + have hXb := badge_wr_irel B.ip hI ht hkt (L.mem s (nBadgeA n)) + rw [badgeRegA_eq_regA] at hXb + obtain ⟨B1, F1⟩ := B.regWrJFixed ht hkt Fl4Spec.badge_register _ hXb + rw [hb0] at B1 F1 + have hnt : n.bv ≠ t.bv := by + intro e + rw [e, hkt] at hk + cases hk + have hk1 : (updK σ t.bv (.tcb (setReg xt Fl4Spec.badge_register b0))).kheap n.bv = some (.notification x) := by + simp [updK, hnt, hk] + have hI1 := B1.xtop.irelK B1.ks B1.buf + obtain ⟨hR2, hW2, hS2⟩ := set_idle_relI B1.ip hI1.1 hn hk1 hact + have hSch2 := hI1.2.2.1.stable hW2 hS2 + obtain ⟨Q, R, hc, hs⟩ := hSch2.lists + obtain ⟨xt2, hkt2, -, -, hsct2, -⟩ := hs.tcb t ht + have hkt2' : (updK (updK σ t.bv (.tcb (setReg xt Fl4Spec.badge_register b0))) n.bv + (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x))).kheap t.bv = + some (.tcb (setReg xt Fl4Spec.badge_register b0)) := by + simp [updK, hnt.symm] + rw [hkt2'] at hkt2 + cases hkt2 + have hsw : optRef (L.mem (recvActS st s t n) (P1.scA t)) = some σ.curSc := hsct2.symm.trans hscc + have h0 : L.mem (recvActS st s t n) (P1.scA t) ≠ 0#u64 := by + intro e + rw [e, optRef_zero] at hsw + cases hsw + have hcw : L.mem (recvActS st s t n) (P1.scA t) = + Sched.nodeW st (recvActS st s t n) (L.cpu (recvActS st s t n)) layout.node.CUR_SC := by + apply P1.eq_of_bv + rw [Notif.optRef_ne h0] at hsw + rw [Option.some.inj hsw] + exact hs.curSc.trans + (congrArg (fun q => (Sched.nodeW st (recvActS st s t n) q layout.node.CUR_SC).bv) hc.cur).symm + have heq := spec_eq_ok (receive_signal_active_eq (arI := arI) (a := a) blocking hst h0 hcw) + obtain ⟨⟨Q0, R0, h0'⟩, hB0, hX0, hS0, hSg0⟩ := B.blk + have hp1 := P1.notification.receive_signal.ipc hAT s a t n blocking Q0 R0 h0' hB0 hX0 hS0 hSg0 hcb + (B.zH.2.2.2 n hn) (B.zH.2.2.2 n hn) (by rw [epState_eq, hst, ntfn_consts.2.2]; decide) + rw [heq, WP.spec_ok] at hp1 + simp only [WP.uncurry'_pair] at hp1 + have hxw : ∀ l, x.ntfnObj ≠ .waitingNtfn l := fun l h => by rw [hact] at h; cases h + have F2 := B1.badgeN hk1 + (y := Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x) + ⟨_, rfl, hR2, hS2, hW2, NoBlock.ntfnUpd _ _ _⟩ + obtain ⟨σ', hsp', -, B2⟩ := B1.signal_badge_post hn hk1 hxw + (y := Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x) (fun l h => by cases h) rfl rfl rfl + ⟨_, rfl, hR2, hS2, hW2, NoBlock.ntfnUpd _ _ _⟩ hp1 + obtain rfl := (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 (Prod.mk.inj hsp').1)) + have hspec : Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blocking σ = + specRecvTail t.bv n.bv (updK (updK σ t.bv (.tcb (setReg xt Fl4Spec.badge_register b0))) n.bv + (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x))) := by + rw [receiveSignal_cap hk] + simp only [hact] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt), nd_bind_one (setNotification_eq _ hk1)] + have hct : σ.curSc ≠ t.bv := fun e => by rw [e, hkt] at hks; cases hks + have hcn : σ.curSc ≠ n.bv := fun e => by rw [e, hk] at hks; cases hks + have hVE := (J.ofNat (V' := Z.vs) B2 (B.natTrans F1 F2)).anyX arI + constructor + · rw [hspec, specRecvTail_cur hkt2' hscc (y := ys) (m := ms) (by simp [updK, hct, hcn, hks]), heq, WP.spec_ok] + exact ⟨_, Set.mem_singleton _, hVE⟩ + · have ht0 : t ≠ 0#u64 := fun e => by + obtain ⟨Q1, R1, hc1, -⟩ := B.xtop.fin.1.1.1.lists + exact (hc1.tcbs t ht).ne0 (by rw [e]; rfl) + rw [completeSignal_cur hkt hk hact hscc hks hct hcn hnt, + spec_eq_ok (complete_signal_cur_eq (arI := arI) (a := a) ht0 hst h0 hcw), WP.spec_ok] + exact ⟨_, Set.mem_singleton _, hVE⟩ + +/-- Direct receive and bound-notification completion at a resumed entry. -/ +private theorem VmEptState.recvActiveVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blocking : Bool) (cb : Fl4Spec.Badge) (cr : Fl4Spec.CapRights) : + (notification.receive_signal st arI s a t n blocking ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blocking σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) ∧ + (notification.complete_signal st arI s a n t ⦃ r => + ∃ z ∈ (Fl4Spec.completeSignal (Z := Unit) n.bv t.bv σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) := + J.recvActiveMidVE hAT ht hcur (J.1.1.curInv (by rw [hni.2]; intro e; cases e)) hni.1 + (J.1.1.curRun.run hni) (by rw [hni.2]; intro e; cases e) hnq hn hk hact blocking cb cr + +/-- Receiving an active notification changes only the receiver's badge and the notification's state. -/ +theorem VmEptState.recvSigActiveVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blocking : Bool) (cb : Fl4Spec.Badge) (cr : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n blocking ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blocking σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := + (J.recvActiveVE hAT ht hcur hni hnq hn hk hact blocking cb cr).1 + +/-- Both endpoint polling reductions produce the same zero-badge VM frame. -/ +theorem VmEptState.recvIpcPollFrame {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) {e : U64} (cb : U64) (c1 c2 c3 c4 : Bool) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (hpoll : endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) false rc ⦃ r => + r = (L.wr s (regA t (regIdx Fl4Spec.badge_register)) 0#u64, a) ⦄) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) {reply : Option Fl4Spec.ObjRef} + (hsp : Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) false rc' σ = + Fl4Spec.receiveIpcBlocked false t.bv e.bv reply σ) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) false rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) false rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + refine WP.spec_mono hpoll ?_ + rintro r rfl + have hspec : Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) false rc' σ = + ({((), updK σ t.bv (.tcb (setReg xt Fl4Spec.badge_register (0#u64 : U64).bv)))}, False) := by + rw [hsp] + unfold Fl4Spec.receiveIpcBlocked Fl4Spec.doNbrecvFailedTransfer + rw [if_neg (by simp), nd_bind_one (asUser_setRegister_eq _ _ hkt)] + rfl + have hXb := badge_wr_irel B.ip hI ht hkt 0#u64 + rw [badgeRegA_eq_regA] at hXb + obtain ⟨B1, F1⟩ := B.regWrJFixed ht hkt Fl4Spec.badge_register 0#u64 hXb + rw [hspec] + exact ⟨_, Set.mem_singleton _, (J.ofNat (V' := Z.vs) B1 F1).anyX arI⟩ + +/-- Poll a non-sending endpoint with no bound notification and a null or free reply. -/ +theorem VmEptState.recvIpcPollVE {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hbn : xt.tcbBoundNotification = none) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hns : ∀ q, x ≠ .sendEP q) (cb : U64) (c1 c2 c3 c4 : Bool) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) false rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) false rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + have hR := hI.1.toE + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hbound : L.mem s (tBoundA t) = 0#u64 := optRef_none (hxt.bound.symm.trans hbn) + have hst : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + exact J.recvIpcPollFrame ht hkt cb c1 c2 c3 c4 (receive_ipc_poll_eq hrc hbound hst) b rights + (receiveIpc_noSend b rights false (replyArg_spec B.ip.e hR hrc) hkt hbn hk hns) + +/-- Poll a non-sending endpoint with a bound non-active notification and a null or free reply. -/ +theorem VmEptState.recvIpcPollBoundVE {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hns : ∀ q, x ≠ .sendEP q) (cb : U64) (c1 c2 c3 c4 : Bool) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) false rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) false rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + have hR := hI.1.toE + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hst : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + exact J.recvIpcPollFrame ht hkt cb c1 c2 c3 c4 + (receive_ipc_pollN_pre hrc hbound (by rw [← hbound]; exact hbz) hstN (receive_ipc_poll_cont hst)) b rights + (receiveIpc_boundNA b rights false (replyArg_spec B.ip.e hR hrc) hkt hbn hk hns hkn hna (fun h => by cases h)) + +theorem JAtZB.recvReplyArg {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (hrc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hrr : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (rTcbA q) = 0#u64) : + ∃ rv, ReplyArg Z.R (L.mem s) rc rc' rv := by + rcases hrc with ⟨rfl, rfl⟩ | ⟨hcr, q, g', rfl, xr, hkr⟩ + · exact ⟨0#u64, Or.inl ⟨rfl, rfl, rfl⟩⟩ + · have hrc' : rc' = absCap (.Reply q g') := hcr.abs + subst hrc' + obtain ⟨r, hr, h⟩ := J.obj.reply _ xr hkr + rw [Sched.bv_inj h] at hr + have h0 : L.mem s (rTcbA q) = 0#u64 := by + rcases hrr with h | ⟨q', g'', h, h0⟩ + · cases h + · cases h; exact h0 + exact ⟨q, Or.inr ⟨g', _, rfl, rfl, hr, h0⟩⟩ + +/-- Resolve the endpoint polling case from the selected endpoint and reply. -/ +theorem VmEptState.recvIpcPollCaseVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hPoll : RecvIpcPollCase (L := L) ct blocking s σ p rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he, h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he + obtain ⟨rfl, hbn, hns, hrr⟩ := hPoll + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + obtain ⟨rv, hra⟩ := B.recvReplyArg hRc hrr + rcases hbn xt hkt with hb | ⟨n0, xn, hb, hkn, hna⟩ + · exact J.recvIpcPollVE ht hkt hb he hke (hns xe hke) b sd true g gr hra b.bv (rightsOf sd true g gr) + · obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + exact J.recvIpcPollBoundVE ht hkt hn hb hkn hna he hke (hns xe hke) b sd true g gr hra b.bv + (rightsOf sd true g gr) + +/-- Resolve an endpoint receive that completes its active bound notification, with a null or free reply. -/ +theorem VmEptState.recvIpcActCaseVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hAct : RecvIpcActCase (L := L) ct s σ rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨⟨xt, np, xn, badge, hkt, hbn, hkn, hactive⟩, hrr⟩ := hAct + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + obtain ⟨rv, hra⟩ := B.recvReplyArg hRc hrr + have hxt := hI.1.toE.at_tcb ht hkt + have hb : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hbn + have hb0 : L.mem s (tBoundA ct) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb; cases hb + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hb0] at hb + exact P1.eq_of_bv (Option.some.inj hb) + have hst : stOf (L.mem s) n = obj.NTFN_ACTIVE := + stOf_active (T := Z.T) (hactive ▸ (hI.1.toN.at_ntfn hn hkn).obj) rfl + let rights := rightsOf sd true g gr + rw [show absCap (.Endpoint p b sd true g gr) = .endpointCap p.bv b.bv rights from rfl, + receiveIpc_active b.bv rights blocking (replyArg_spec B.ip.e hI.1.toE hra) hkt hbn hke hkn hactive] + exact receive_ipc_preA s a ct p b sd true g gr blocking rc rc' rv hra hbound + (by rw [← hbound]; exact hb0) hst (J.recvActiveVE hAT ht hcur hni hnq hn hkn hactive blocking b.bv rights).2 + +/-- The [J] post at the selected endpoint and checked reply cap outside the proven endpoint cases. -/ +def RecvEndpointVE (ar : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) + (th : TypedHeap) (s : S) (a : A) (σ : Fl4Spec.State Unit) (ct : U64) (blocking : Bool) : Prop := + ∀ p b sd g gr rc rc', (∃ x, σ.kheap p.bv = some (.endpoint x)) → + ((rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) → + JHeld (L := L) th s (.Endpoint p b sd true g gr) → + (∀ q g', rc = .Reply q g' → JHeld (L := L) th s rc) → + ¬ RecvIpcPollCase (L := L) ct blocking s σ p rc → + ¬ RecvIpcActCase (L := L) ct s σ rc → + endpoint.receive_ipc st ar s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) ar AR r.1 r.2 z.2 ⦄ + +set_option maxHeartbeats 4000000 in +/-- Receive from a non-active notification and return its scheduling context. -/ +theorem VmEptState.recvSigReturnVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hsc : x.ntfnSc = some σ.curSc) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hC := B.curInv (by rw [hni.2]; intro e; cases e) + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hcur, hkt] at hkc + cases hkc + obtain ⟨i, hiC, hib⟩ := B.nsc n hn x σ.curSc hk hsc + have hx := hI.1.toN.at_ntfn hn hk + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + have h := hx.obj + cases he : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := Z.T) (he ▸ h) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (he ▸ h) rfl) + | activeNtfn b => exact absurd he (hna b) + refine receive_signal_split hst (B.recvSigBlockPreB hkt ht hcur hni hnq hn hk hna hcov cb rt) ?_ + rintro s4 ⟨σ3, hspec, hnrw, hkt3, ⟨q, hkn3⟩, hact3, -, hid3, B3, F3⟩ + refine WP.spec_mono (B3.maybeReturnB ht hn hiC hkt3 hkn3 (by rw [hib]; exact hscc) (by rw [hib]; exact hsc) + (by rfl) hnrw (fun u hu => by rw [hact3] at hu; cases hu) (by rw [hid3, ← hcur]; exact hni.1)) ?_ + rintro s5 ⟨σ5, he5, Ac', Lst', B5, F5⟩ + exact ⟨((), σ5), by rw [hspec, he5]; exact Set.mem_singleton _, + (J.ofNat (V' := Z.vs) B5 (B.natTrans F3 ⟨F5.ss, F5.fw, F5.fk⟩)).anyX arI⟩ + +set_option maxHeartbeats 4000000 in +/-- Resolve both capability lookups, the bound-TCB read, the fault route, every +notification branch, and the active or polling endpoint cases from the VM state. +The remaining [J] input is `RecvEndpointVE`. -/ +theorem VmEptState.handleRecvV + (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (blocking canReply : Bool) + (hIpc : RecvEndpointVE (L := L) arI AR Z.th s a σ ct blocking) : + syscall.handle_recv st arI s a blocking canReply ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) blocking canReply σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + obtain ⟨hcur, -⟩ := B.curEq hct + obtain ⟨htcb, xs, hks⟩ := B.xtop.curTcb hcur + have hctT : ct ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm + have hI := B.xtop.irelK B.ks B.buf + have hxs := hI.1.toE.at_tcb hctT hks + have hR := B.xtop.rel.2.1.1 + have hV := B.xtop.rel.2.1.2.1 + refine handle_recv_refinesH (VEAnyX (L := L) arI AR) (JHeld (L := L) Z.th s) s a σ + blocking canReply ct hct hcur hks + (by rw [← regIdx_cap]; exact hxs.regs _) + (by rw [← regIdx_reply]; exact hxs.regs _) + (fun cptr => lookup_cap_refines Z.th s σ hR hV ct htcb cptr) + (fun cptr => WP.spec_mono (lookup_cap_held Z.th s σ hR hV ct htcb cptr) fun r hr c hc => ⟨?_, hr c hc⟩) + ⟨?_, ?_, recv_bound_leaf hI.1 B.obj, ?_⟩ + · obtain ⟨y, hy, hcv⟩ := hr c hc + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + cases c with + | Endpoint p b x1 x2 x3 x4 => + obtain ⟨xe, hke, -⟩ := hI.1.ep p (B.hset.1 p ((hJ.typed _ hy).1 p b x1 x2 x3 x4 hcv)) + exact ⟨xe, hke⟩ + | Reply p g => + obtain ⟨xr, hkr, -⟩ := hI.1.reply p (B.hset.2.2 p ((hJ.typed _ hy).2.2 p g hcv)) + exact ⟨xr, hkr⟩ + | Notification p b w r => + obtain ⟨xn, hkn, -⟩ := hI.1.ntfn p (B.capN ⟨y, b, w, r, hy, hcv⟩).1 + exact ⟨xn, hkn⟩ + | _ => trivial + · intro p b sd g gr rc rc' hEp hRc hHeld hReply + by_cases hPoll : RecvIpcPollCase (L := L) ct blocking s σ p rc + · exact J.recvIpcPollCaseVE hctT blocking p b sd g gr rc rc' hEp hRc hPoll + · by_cases hAct : RecvIpcActCase (L := L) ct s σ rc + · exact J.recvIpcActCaseVE hAT hni hnq hctT hcur blocking p b sd g gr rc rc' hEp hRc hAct + · exact hIpc p b sd g gr rc rc' hEp hRc hHeld hReply hPoll hAct + · rintro p b w rd ac hcr ⟨x, hk⟩ hh + obtain ⟨n, hn, h⟩ := B.obj.ntfn _ x hk + rw [Sched.bv_inj h] at hn + by_cases hs : ∃ badge, x.ntfnObj = .activeNtfn badge + · obtain ⟨badge, hactive⟩ := hs + rw [hcr.abs] + exact J.recvSigActiveVE hAT hctT hcur hni hnq hn hk hactive blocking b.bv (rightsOf w rd false false) + have hna : ∀ badge, x.ntfnObj ≠ .activeNtfn badge := fun badge h => hs ⟨badge, h⟩ + cases blocking + · rw [hcr.abs] + exact J.recvSigPollVE hctT hn hk hna b.bv (rightsOf w rd false false) + · obtain ⟨y, hy, hcv⟩ := hh + have hHn : H p = some .ntfn := B.zH.2.2.2 p hn + have hfit := B.kern.heap.fits p _ hHn + simp only [P1.HKind.bits] at hfit + have hcov := B.cover_cap (P1.WritesIn.refl s _) hy (by rw [capAt_capV, hcv]) (fun h => by cases h) + (ok_inj (phys_eq _).symm) rfl (range_ntfn p b w rd hfit) fun k hk' => by + rw [hHn] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + rw [hcr.abs] + by_cases hsc : x.ntfnSc = some σ.curSc + · exact J.recvSigReturnVE hctT hcur hni hnq hn hk hna hsc hcov b.bv (rightsOf w rd false false) + exact WP.spec_mono (B.recvSigBlockB hAT hctT hcur hni hnq hn hk hna hsc hcov b.bv (rightsOf w rd false false)) + fun _ ⟨z, hz, B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + · intro cptr lf lf' h + exact WP.spec_mono (J.handleFaultVX arI hAT hni hct hnq hvfh (f := .Cap cptr true lf) + (f' := .capFault (Fl4Spec.ofBl (Fl4Spec.toBl cptr.bv)) true lf') + ⟨cptr, true, lf, lf', rfl, by rw [ofBl_toBl64], h⟩) + fun _ ⟨z, hz, _, hJ⟩ => ⟨z, hz, hJ⟩ + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3RecvEndpoint.lean b/proofs/Fl4Proofs/P3/VmEntryS3RecvEndpoint.lean new file mode 100644 index 000000000..dd0093c69 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3RecvEndpoint.lean @@ -0,0 +1,1140 @@ +/- +The endpoint receive proof carries the actual checked reply, optional +cancellation and scheduling-context return into the matched endpoint read. +The waiting, bound-notification and poll arms are produced from the shared +scheduler and IPC frames. + +[J] Only the selected waiting sender's basic, pushed-reply and timeout +transfer producers are used as premises. [P] Architecture totality is +used by the native receive and cancellation leaves. +-/ +import Fl4Proofs.P3.VmEntryS3Recv +import Fl4Proofs.P3.XTopJointRecvIpcRet +import Fl4Proofs.P3.VmHandle +import Fl4Proofs.P3.VsMakePoolRel +import Fl4Proofs.P3.XTopJointRecvIpcLink +import Fl4Proofs.P3.XTopJointRecvSendRet + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +theorem JAtZB.canBlockCurG {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) + {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) (hrn : Fl4Spec.runnable x.tcbState = true) + (hti : σ.curThread ≠ σ.idleThread) (hnr : ct.bv ∉ σ.releaseQueue) + (hsa : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ x.tcbSchedContext = none)) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) : + P1.CanBlock st (P1.Heap.tcbs H) Ac s ct := by + have hS := J.xtop.fin.1.1.1 + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have htH : ct ∈ P1.Heap.tcbs H := J.zH.1 ct ht + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + obtain ⟨bq, br⟩ := J.curBits ht hnq' hnr + have hsw : ∀ u, σ.schedulerAction = .switchThread u → u ≠ ct.bv := fun u hu => by + rcases hsa with h | ⟨h, -⟩ <;> rw [h] at hu <;> cases hu + obtain ⟨hna, -⟩ := J.words_of_spec ht hsw (fun e => hti (by rw [hcur, e])) + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb ct ht + rw [hk] at hk0; cases hk0 + have hv : (L.mem s (P1.stA ct)).val = tsCode x.tcbState := by + rw [P1.stA_eq, ← tStA_eq]; exact hTR.st + obtain ⟨hnb, hnrp⟩ := P1.run_word (w := L.mem s (P1.stA ct)) (by rw [hv]; exact runnable_code hrn) + have hcw : L.curW s = ct := by + apply P1.eq_of_bv + rw [← hcur, hs.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq.cur] + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + refine ⟨htH, hnb, hnrp, bq, br, hna, ?_, ?_⟩ + · intro h + have ei : L.idleW s = Sched.nodeW st s Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW Sched.nodeW; rw [hcq.cur] + exact hti (by rw [hcur, hs.idleTh, ← ei, h]) + · rcases hsa with hres | ⟨-, hsc0⟩ + · have ha0 : L.actionW s = 0#u64 := by + have h := hS.act + rw [hres] at h + unfold P1.LawfulStore.actionW; rw [hcq.cur]; exact h + exact fun h1 => hB0.sact ct htH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨ha0, hcw⟩))))) h1 + · intro h1 + obtain ⟨xt0, hkt0, -, -, hsct, -⟩ := hs.tcb ct ht + rw [hk] at hkt0; cases hkt0 + rw [hsc0] at hsct + exact absurd (optRef_none hsct.symm) h1 + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvCov_ofW (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) {p b : U64} {sd g gr : Bool} (he : p ∈ Z.E) + (hHd : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) {rc : capability.Cap} + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) {rc' : Fl4Spec.Cap} {rv : U64} + (hra : ReplyArg Z.R (L.mem s) rc rc' rv) : + ((∀ k, Z.th p = some k → Sched.Covered (L.mem s') Z.G p.val (p.val + max (objSize k) 1)) ∧ + (∀ k, H p = some k → Sched.Covered (L.mem s') Z.G p.val (p.val + 2 ^ k.bits))) ∧ + (rv ≠ 0#u64 → + (∀ k, Z.th rv = some k → Sched.Covered (L.mem s') Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Sched.Covered (L.mem s') Z.G rv.val (rv.val + 2 ^ k.bits))) := by + have hHe : H p = some .ep := J.zH.2.1 p he + have hfit := J.kern.heap.fits p _ hHe + simp only [P1.HKind.bits] at hfit + obtain ⟨y, hy, hcv⟩ := hHd + refine ⟨J.cover_cap hw hy (by rw [capAt_capV, hcv]) (fun h => by cases h) + (ok_inj (phys_eq _).symm) rfl (range_ep p b sd true g gr hfit) fun k hk' => by + rw [hHe] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩, fun h0 => ?_⟩ + rcases hra with ⟨-, -, rfl⟩ | ⟨g', r', rfl, -, hrR, -⟩ + · exact absurd rfl h0 + obtain ⟨y', hy', hcv'⟩ := hHr rv g' rfl + have hHr' : H rv = some .reply := J.zH.2.2.1 rv hrR + have hfit' := J.kern.heap.fits rv _ hHr' + simp only [P1.HKind.bits] at hfit' + exact J.cover_cap hw hy' (by rw [capAt_capV, hcv']) (fun h => by cases h) + (ok_inj (phys_eq _).symm) rfl (range_reply rv g' hfit') fun k hk' => by + rw [hHr'] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcWaitB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hnrl : t.bv ∉ σ.releaseQueue) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hsa : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ xt.tcbSchedContext = none)) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : rv ≠ 0#u64 → (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + (hsp : Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ = + Fl4Spec.receiveIpcBlocked true t.bv e.bv (optRef rv) σ) : + (do + let s3 ← endpoint.wait_for_sender st s t e true rv + ok (s3, a)) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have htq : t.bv ∉ x.epQueue := Endp.not_queued hI.1.toE ht hkt hnb3 e he x hk + have hq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, hcn, hsrt, hcc⟩ := + recvIpcBlock_eq hkt hk hc htq hq hr (J.isorted.1 _ _ hk) b rights (replyArg_spec J.ip.e hI.1.toE hrc) hsp + -- the reply object (if any): in `R`, linked to no thread + have hrv : rv ≠ 0#u64 → rv ∈ Z.R ∧ L.mem s (Endp.rTcbA rv) = 0#u64 := fun h0 => replyArg_ne J.ip.e hrc h0 + have hcn' : σ'.schedulerAction = .chooseNewThread := by + rcases hsa with h | ⟨h, -⟩ + · exact hcn hcur.symm h + · exact hcc h + -- P1's `BlkOK` after and footprint + have hcb := J.canBlockCurG ht hcur hkt hr hti hnrl hsa hnq + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hHe : H e = some .ep := J.zH.2.1 e he + have hx := hI.1.toE.at_ep he hk + have hst2 : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => simp [Endp.CanBlock] at hc + have hst0 : L.epState s e = 0#u64 ∨ L.epState s e = 2#u64 := by rw [epState_eq]; exact hst2 + have hrH : rv ≠ 0#u64 → rv ∈ P1.Heap.replies H := fun h => J.zH.2.2.1 rv (hrv h).1 + have p1b := spec_lift_ok (P1.endpoint.wait_for_sender.inv s t e true rv Q0 R0 h0 hB0 hX0 hS0 hSg0 + (J.zH.1 t ht) (fun _ => hcb) hHe hst0 (fun h => ⟨hrH h, Or.inl (hrv h).2⟩)) a + have p1f := spec_lift_ok (P1.endpoint.wait_for_sender.footR (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) s t e rv h0 hcb hHe + (fun h => ⟨hSg0.sub _ (hX0.1.tcbW (hrH h)), hX0.1.tcbW (hrH h)⟩)) a + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (WP.spec_and (wait_for_sender_blockF J.ip s a σ hI ht hkt hnb3 hnn he hk hc + (hsort e (Or.inl he)) b rights hrc hsp) p1b) p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW⟩, hbk⟩, hfoot⟩ + simp only at ea hbk hfoot hIR hW + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB_free hy hyt, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := ⟨fun u hu => (by rw [hcn'] at hu; cases hu), F.idleOK J.act.2⟩ + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcn' + have hrun3 : CurRunM σ' := Or.inl (by rw [hcn']; intro h; cases h) + -- the dead: the thread is not dead (runnable); the endpoint and the reply leave the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + set D' : Set U64 := if rv = 0#u64 then D \ {e} else (D \ {e}) \ {rv} with hD' + have hD'D : ∀ q ∈ D', q ∈ D \ {e} ∧ Notif.optRef rv ≠ some q.bv := by + intro q hq + by_cases h0 : rv = 0#u64 + · rw [hD', if_pos h0] at hq + exact ⟨hq, by rw [h0, optRef_zero]; intro h; cases h⟩ + · rw [hD', if_neg h0] at hq + refine ⟨hq.1, fun h => hq.2 ?_⟩ + rw [Notif.optRef_ne h0] at h + exact (P1.eq_of_bv (Option.some.inj h)).symm + have hdead : ∀ q ∈ D', DeadP (L := L) H s' q := fun q hq' => by + obtain ⟨hq, hqr⟩ := hD'D q hq' + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqe : q.bv ≠ e.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF' hx hqr, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 + (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D') + -- no reply names the runnable thread (`SchedValid.replyTcb`) + have hnt : ∀ d ∈ Z.R, ∀ xr, σ.kheap d.bv = some (.reply xr) → xr.replyTcb ≠ some t.bv := by + intro d _ xr hkr hrt + obtain ⟨x', hk', hs'⟩ := J.sv.replyTcb _ xr _ hkr hrt + rw [hkt] at hk'; cases hk' + rcases hs' with hs' | ⟨ep, dd, hs'⟩ <;> rw [hs'] at hr <;> cases hr + -- the reply (if any) was free, so it has null links + have hrq : ∀ d ∈ Z.R, Notif.optRef rv = some d.bv → L.mem s' (rNextA d) = 0#u64 ∧ L.mem s' (rPrevA d) = 0#u64 := by + intro d hd hdr + have h0 : rv ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hdr; cases hdr + rw [Notif.optRef_ne h0] at hdr + obtain rfl : rv = d := P1.eq_of_bv (Option.some.inj hdr) + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply rv hd + obtain ⟨-, h2, h3⟩ := J.rfree rv hd ⟨xr, hkr, by rw [hxr, (hrv h0).2, optRef_zero]⟩ + obtain ⟨e1, e2⟩ := k4 rv hd + exact ⟨by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hRR' := J.rrecv.frameR k4 F hnt hrq + have hjcov : JCovD (L := L) Z.th H Z.G D' Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hcv : JCovD (L := L) Z.th H Z.G D' Cs s := by + by_cases h0 : rv = 0#u64 + · rw [hD', if_pos h0]; exact J.jcov.undead hcov.1 hcov.2 + · rw [hD', if_neg h0]; exact (J.jcov.undead hcov.1 hcov.2).undead (hcovR h0).1 (hcovR h0).2 + exact hcv.same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + -- `BlockedScActive`: the thread's SC is the current SC, active + have hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp).2⟩ + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp hyt + rw [hkt] at hku; cases hku + rcases hsa with hres | ⟨-, hsc0⟩ + · have hC : CurInv σ := J.curInv (by rw [hres]; intro e; cases e) + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hcur, hkt] at hkc; cases hkc + have e1 : σ.curSc = q.bv := Option.some.inj (hscc.symm.trans hxs) + rw [← e1] at hp + exact hC.active (J.curSc_ne_idle hC hti) y n hp + · rw [hsc0] at hxs; cases hxs + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D' s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have B' : JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' s' a σ' := J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (fun p hp => by + have h1 := J.zD_diffE he p hp + have hT : H p = some .tcb := J.zH.1 p (J.thT p (J.ztcb_tcb hp)) + rw [hD'] + split_ifs with h0 + · exact h1 + · exact ⟨h1, fun e' => by rw [Set.mem_singleton_iff.1 e', J.zH.2.2.1 rv (hrv h0).1] at hT; cases hT⟩) + rw [heq] + have hK : NatK σ σ' := natK_kh (R := BChgR t.bv e.bv (optRef rv)) (by + intro p o o' h + rcases h with ⟨xt, e, d, rfl, rfl, -, -⟩ | ⟨x, y, rfl, rfl, -⟩ | ⟨x, rfl, rfl, -⟩ + · exact zvChg_tcb fun h => by cases h + · exact zvChg_ep + · exact zvChg_reply) F.kh + exact ⟨_, Set.mem_singleton _, D', B', NatFoot.ofWS hF.1 hW + (fun _ h => Or.inl (Ipc.CnW.oobj hS h)) (fun _ h => J.cnW_nv h) (fun _ h => J.cnW_nh h) hK⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcBlockBoundB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hnrl : t.bv ∉ σ.releaseQueue) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hsa : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ xt.tcbSchedContext = none)) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) (hnr : ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none)) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : rv ≠ 0#u64 → (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (cb : U64) (c1 c2 c3 c4 : Bool) (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) true rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hns : ∀ q, x ≠ .sendEP q := fun q h => by subst h; simp [Endp.CanBlock] at hc + have hx := hI.1.toE.at_ep he hk + have hst2 : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hwn : L.mem s (nScA n) = 0#u64 ∨ L.mem s (nScA n) ≠ L.mem s (P1.scA t) := by + have hsN : xn.ntfnSc = optRef (L.mem s (nScA n)) := hxn.sc + obtain ⟨Q0, R0, -, hs0⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨xt0, hkt0, -, -, hsct, -⟩ := hs0.tcb t ht + rw [hkt] at hkt0; cases hkt0 + by_cases h0 : L.mem s (nScA n) = 0#u64 + · exact Or.inl h0 + · refine Or.inr fun he' => hnr ⟨?_, ?_⟩ + · rw [hsN, he']; exact hsct.symm + · rw [hsN, Notif.optRef_ne h0]; simp + exact receive_ipc_blockN_red hrc hbound (by rw [← hbound]; exact hbz) hstN hwn hst2 + (J.recvIpcWaitB ht hcur hti hnq hnrl hkt hr hsa he hk hc hcov hrc hcovR b rights + (receiveIpc_boundNA b rights true (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hk hns hkn hna (fun _ => hnr))) + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcRetB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) (hlend : xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) {cb : U64} {sd g gr : Bool} + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) {rc : capability.Cap} + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb sd true g gr) true rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ Ac' Lst' D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac' Lst' D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hC : CurInv σ := J.curInv (by rw [hni.2]; intro e; cases e) + have hnrl : t.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hr : Fl4Spec.runnable xt.tcbState = true := (J.curRun.run hni) xt (by rw [hcur]; exact hkt) + have hns : ∀ q, x ≠ .sendEP q := fun q h => by subst h; simp [Endp.CanBlock] at hc + -- the SC `i` lent by the notification, the thread's + obtain ⟨hl1, hl2⟩ := hlend + obtain ⟨i0, hi0⟩ := Option.ne_none_iff_exists'.1 hl2 + have hxs0 : xt.tcbSchedContext = some i0 := hl1.symm.trans hi0 + obtain ⟨y, m, hky, -⟩ := J.inv.back t.bv xt i0 hkt hxs0 + obtain ⟨i, hiC, rfl⟩ := J.obj.sc _ y m hky + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨σ2, he2, kt, -, ko, ct, -, -, -, hidl, -, rq, a1, -, hrq2⟩ := + maybeReturn_shape J.inv hkn hkt hky hxs0 hi0 hnqT + -- FL4's bound-notification words + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hcb := (J.faultEntryBlock hni ht hcur hkt hr (by rw [← hcur]; exact hnq)).1 + have hsp1 := receiveIpc_boundR b rights (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hk hns hkn hna he2 + refine receive_ipc_blockR_red hrc hbound (by rw [← hbound]; exact hbz) hstN ?_ + refine WP.spec_mono (J.maybeReturnBQ ht hn hiC hkt hkn hxs0 hi0 hnqT ⟨hcb.nq, hcb.nr, hcb.na⟩ + (fun u hu => by rw [hni.2] at hu; cases hu) (by rw [← hcur]; exact hni.1)) ?_ + rintro s' ⟨hW, σ', he', Ac', Lst', J2, F2⟩ + rw [the_result he2 he'] at J2 F2 + have hI2 := J2.xtop.irelK J2.ks J2.buf + -- the objects kept by `maybe_return_sc` + have hte : e.bv ≠ t.bv := fun h => by rw [h, hkt] at hk; cases hk + have hie : e.bv ≠ i.bv := fun h => by rw [h, hky] at hk; cases hk + have hk2 : σ2.kheap e.bv = some (.endpoint x) := by rw [ko _ hte hie]; exact hk + have hnt : n.bv ≠ t.bv := fun h => by rw [h, hkt] at hkn; cases hkn + have hni' : n.bv ≠ i.bv := fun h => by rw [h, hky] at hkn; cases hkn + have hkn2 : σ2.kheap n.bv = some (.notification xn) := by rw [ko _ hnt hni']; exact hkn + have hcur2 : σ2.curThread = t.bv := ct.trans hcur + have hti2 : σ2.curThread ≠ σ2.idleThread := by rw [ct, hidl]; exact hni.1 + have hnq2 : ∀ d p, σ2.curThread ∉ σ2.readyQueues d p := by rw [hcur2]; exact hrq2 + -- the reply cap at the state after + have hrc2 : ReplyArg Z.R (L.mem s') rc rc' rv := by + rcases hrc with h | ⟨bb, r', h1, h2, hrR, h0⟩ + · exact Or.inl h + refine Or.inr ⟨bb, r', h1, h2, hrR, ?_⟩ + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply rv hrR + obtain ⟨xr2, hkr2, hxr2⟩ := hI2.1.toE.reply rv hrR + have hrt : rv.bv ≠ t.bv := fun h => by rw [h, hkt] at hkr; cases hkr + have hri : rv.bv ≠ i.bv := fun h => by rw [h, hky] at hkr; cases hkr + rw [ko _ hrt hri, hkr] at hkr2; cases hkr2 + rw [h0, optRef_zero] at hxr + exact optRef_none (hxr2.symm.trans hxr) + obtain ⟨hcov, hcovR⟩ := J.recvCov_ofW hW he hHd hHr hrc + have hx2 := hI2.1.toE.at_ep he hk2 + have hst2 : stOf (L.mem s') e = 0#u64 ∨ stOf (L.mem s') e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx2.1 + | recvEP q => exact Or.inr hx2.1 + | sendEP q => exact absurd rfl (hns q) + have hsp2 := receiveIpc_boundNA b rights true (replyArg_spec J2.ip.e hI2.1.toE hrc2) kt hbn hk2 hns hkn2 hna + (fun _ h => h.2 h.1) + have key := recvTail_wait (ar := arI) hst2 + (J2.recvIpcWaitB ht hcur2 hti2 hnq2 (fun h => hnrl (rq _ h)) kt hr (Or.inr ⟨a1 hcur.symm, rfl⟩) + he hk2 hc hcov hrc2 hcovR b rights hsp2) + rw [hsp1, ← hsp2] + exact WP.spec_mono key fun _ ⟨z, hz, D', B', F'⟩ => + ⟨z, hz, Ac', Lst', D', B', J.natTrans F2 F'⟩ + +set_option maxHeartbeats 16000000 in +/-- A checked null or unlinked reply gives the precise concrete and spec argument. -/ +theorem JAtZB.checkedFreeArg {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (hrc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hrr : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64) : + ∃ rv, ReplyArg Z.R (L.mem s) rc rc' rv := by + rcases hrc with ⟨rfl, rfl⟩ | ⟨hcr, q, g', rfl, xr, hkr⟩ + · exact ⟨0#u64, Or.inl ⟨rfl, rfl, rfl⟩⟩ + · have hrc' : rc' = absCap (.Reply q g') := hcr.abs + subst hrc' + obtain ⟨r, hr, h⟩ := J.obj.reply _ xr hkr + rw [Sched.bv_inj h] at hr + have h0 : L.mem s (Endp.rTcbA q) = 0#u64 := by + rcases hrr with h | ⟨q', g'', h, h0⟩ + · cases h + · cases h; exact h0 + exact ⟨q, Or.inr ⟨g', _, rfl, rfl, hr, h0⟩⟩ + +set_option maxHeartbeats 16000000 in +theorem VmEptState.recvIpcWaitCaseVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHeld : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hReply : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hWait : RecvIpcBlockCase (L := L) ct blocking s σ p rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ p rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he, h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he + have hb : blocking = true := hWait.elim (fun h => h.1) (fun h => h.1) + subst blocking + have hns : ∀ q, xe ≠ .sendEP q := hWait.elim (fun h => h.2.2.1 _ hke) (fun h => h.2.2.1 _ hke) + have hrr : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 := + hWait.elim (fun h => h.2.2.2) (fun h => h.2.2.2) + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hrr + have hcB : Endp.CanBlock true xe := by + cases xe with + | idleEP => trivial + | recvEP q => rfl + | sendEP q => exact absurd rfl (hns q) + have hnrl : ct.bv ∉ σ.releaseQueue := by + rw [← hcur] + exact (B.curInv (by rw [hni.2]; intro e; cases e)).notRel + obtain ⟨hcE, hcR⟩ := B.recvCov_ofW (P1.WritesIn.refl s _) he hHeld hReply hra + let rights := rightsOf sd true g gr + rcases hWait with ⟨-, hb, -, -⟩ | ⟨-, ⟨xt, np, xn, hkt, hb, hkn, hna⟩, -, -⟩ + · obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + have hrun := (B.curRun.run hni) xt (by rw [hcur]; exact hkt) + have hx := hI.1.toE.at_ep he hke + have hst : stOf (L.mem s) p = 0#u64 ∨ stOf (L.mem s) p = 2#u64 := by + cases xe with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hb0 := optRef_none ((hI.1.toE.at_tcb ht hkt).bound.symm.trans (hb xt hkt)) + have hsp := receiveIpc_noSend b.bv rights true (replyArg_spec B.ip.e hI.1.toE hra) hkt (hb xt hkt) hke hns + exact receive_ipc_block_red hra hb0 hst (WP.spec_mono + (B.recvIpcWaitB ht hcur hni.1 hnq hnrl hkt hrun (Or.inl hni.2) he hke hcB hcE hra hcR b.bv rights hsp) + fun _ ⟨z, hz, D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩) + · obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + have hrun := (B.curRun.run hni) xt (by rw [hcur]; exact hkt) + by_cases hret : xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none + · exact WP.spec_mono (B.recvIpcRetB ht hcur hni hnq hkt hn hb hkn hna hret he hke hcB hHeld hReply hra + b.bv rights) fun _ ⟨z, hz, Ac', Lst', D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + · exact WP.spec_mono (B.recvIpcBlockBoundB ht hcur hni.1 hnq hnrl hkt hrun (Or.inl hni.2) hn hb hkn hna hret + he hke hcB hcE hra hcR b sd true g gr b.bv rights) + fun _ ⟨z, hz, D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +end +end Fl4Proofs.P3 + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp + Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- Cancel the exact TCB reached through the checked reply and keep the native footprint. -/ +theorem JAtZB.cancelLinkForRecv + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {i : U64} (hi : i ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap i.bv = some (.tcb x)) + {q : Fl4Spec.ObjRef} + (hst : x.tcbState = .blockedOnReply q ∨ ∃ e d, x.tcbState = .blockedOnReceive e (some q) d) : + ipc.cancel_ipc st arI s a i ⦃ r => + r.2 = a ∧ ∃ σ', Fl4Spec.cancelIpc (Z := Unit) i.bv σ = ({((), σ')}, False) ∧ + (∃ y, σ'.kheap i.bv = some (.tcb y) ∧ y.tcbState = .inactive) ∧ + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64), + JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D r.1 r.2 σ' ∧ + NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + rcases hst with hst | ⟨e, d, hst⟩ + · refine WP.spec_mono (B.cancelReplyJ hi hk hst) ?_ + rintro ⟨s', a'⟩ ⟨ha, σ', he, V', Ac', Lst', B', F⟩ + refine ⟨ha, σ', he, ?_, V', Ac', Lst', B', F⟩ + have hremove : Fl4Spec.cancelIpc (Z := Unit) i.bv σ = + Fl4Spec.replyRemoveTcb i.bv q (updK σ i.bv (.tcb { x with tcbFault := none })) := by + unfold Fl4Spec.cancelIpc + rw [Notif.nd_bind_one (Notif.getThreadState_eq hk), Notif.nd_bind_one (Notif.threadSet_eq _ hk), hst] + have hm : ((), σ') ∈ (Fl4Spec.replyRemoveTcb (Z := Unit) i.bv q + (updK σ i.bv (.tcb { x with tcbFault := none }))).1 := by + rw [← hremove, he] + exact Set.mem_singleton _ + exact postI_replyRemoveTcb _ _ _ _ hm + · refine WP.spec_mono (B.cancelRecvJ hi hk hst) ?_ + rintro ⟨s', a'⟩ ⟨ha, σ', he, V', Ac', Lst', B', F⟩ + refine ⟨ha, σ', he, ?_, V', Ac', Lst', B', F⟩ + obtain ⟨xe, hke, hq⟩ := B.sv.recvQ i.bv x e (some q) d hk hst + have hm : ((), σ') ∈ (Fl4Spec.cancelIpc (Z := Unit) i.bv σ).1 := by + rw [he] + exact Set.mem_singleton _ + exact ⟨_, (Sched.cancelIpc_recv_cfr (r := i.bv) hk hst hke hq + (fun rp h => B.sv.recvReply i.bv x e rp d hk (by rw [hst, h])) hm).2.1, rfl⟩ + +set_option maxHeartbeats 4000000 in +/-- The endpoint receive with a checked reply linked to another thread. Replay +only the same selected endpoint and reply after the matched cancellation. -/ +theorem VmEptState.recvIpcLinkedVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (p b : U64) (sd g gr : Bool) (q : U64) (g' : Bool) + (rc' : Fl4Spec.Cap) (hcr : CapRel (.Reply q g') rc') + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + {xr : Fl4Spec.Reply} (hkr : σ.kheap q.bv = some (.reply xr)) + (hHd : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hHr : JHeld (L := L) Z.th s (.Reply q g')) (hi0 : L.mem s (rTcbA q) ≠ 0#u64) + (afterFree : ∀ i V' Ac' Lst' s' a' σ', + L.mem s (rTcbA q) = i → i ≠ 0#u64 → i ≠ ct → + ipc.cancel_ipc st arI s a i = ok (s', a') → + Fl4Spec.cancelIpc (Z := Unit) i.bv σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D s' a' σ' → + σ'.curThread = ct.bv → Entered σ' → (∀ d p, σ'.curThread ∉ σ'.readyQueues d p) → + (∃ xe, σ'.kheap p.bv = some (.endpoint xe)) → + (∃ xr', σ'.kheap q.bv = some (.reply xr') ∧ xr'.replyTcb = none) → + L.mem s' (rTcbA q) = 0#u64 → + JHeld (L := L) Z.th s' (.Endpoint p b sd true g gr) → + JHeld (L := L) Z.th s' (.Reply q g') → + endpoint.receive_ipc st arI s' a' ct (.Endpoint p b sd true g gr) blocking (.Reply q g') ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ').1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking (.Reply q g') ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hR := (B.xtop.irelK B.ks B.buf).1.toE + obtain ⟨q0, hq, hqe⟩ := B.obj.reply _ xr hkr + rw [Sched.bv_inj hqe] at hq + have hrt : xr.replyTcb = some (L.mem s (rTcbA q)).bv := (hR.at_reply hq hkr).trans (Notif.optRef_ne hi0) + obtain ⟨xi, hki, hsti⟩ := B.sv.replyTcb q.bv xr _ hkr hrt + obtain ⟨i, hiT, hie⟩ := B.obj.tcb _ xi hki + have hiq : L.mem s (rTcbA q) = i := (P1.eq_of_bv hie).symm + rw [hiq] at hrt hki hi0 + have hict : i ≠ ct := by + intro e + subst e + have hr := (B.curRun.run hni) xi (by rw [hcur]; exact hki) + rcases hsti with h | ⟨_, _, h⟩ <;> rw [h] at hr <;> simp [Fl4Spec.runnable] at hr + have hne : σ.curThread ≠ i.bv := fun e => hict (P1.eq_of_bv (e.symm.trans hcur)) + refine receive_ipc_linked hiq hi0 hict ?_ + refine WP.spec_mono (Vs.spec_eq_mp (B.cancelLinkForRecv hiT hki hsti)) ?_ + rintro ⟨s1, a1⟩ ⟨hconc, ha, σ', he, hin, V', Ac', Lst', B', F⟩ + dsimp only at ha B' ⊢ + subst a1 + have hm : ((), σ') ∈ (Fl4Spec.cancelIpc (Z := Unit) i.bv σ).1 := by + rw [he] + exact Set.mem_singleton _ + obtain ⟨hct', hid', hact', hrq', -⟩ := cancelOther_keep hm hne + have hR' := (B'.xtop.irelK B'.ks B'.buf).1.toE + obtain ⟨xr', hkr', hrl⟩ := hR'.reply q hq + have hnone : xr'.replyTcb = none := cancelOther_reply B.sv B'.sv hm hkr hrt hin hkr' + have h0' : L.mem s1 (rTcbA q) = 0#u64 := optRef_none (hrl.symm.trans hnone) + refine ⟨h0', ?_⟩ + have hcr' : rc' = absCap (.Reply q g') := hcr.abs + subst rc' + have hsp : Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking + (absCap (.Reply q g')) σ = + Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking + (absCap (.Reply q g')) σ' := + receiveIpc_linked _ _ _ _ hkr hrt (fun e => hne (hcur.trans e.symm)) he hkr' hnone + rw [hsp] + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he', h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he' + obtain ⟨xe', hke', -⟩ := hR'.ep p he' + exact afterFree i V' Ac' Lst' s1 a σ' hiq hi0 hict hconc he (J.ofNat (V' := V') B' F) + (hct'.trans hcur) ⟨by rw [hct', hid']; exact hni.1, by rw [hact']; exact hni.2⟩ + (by rw [hct', hrq']; exact hnq) ⟨xe', hke'⟩ ⟨xr', hkr', hnone⟩ h0' + (jHeld_of_same F.ss hHd) (jHeld_of_same F.ss hHr) + +end +end Fl4Proofs.P3 + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The blocked TCB at the head of an actual sending endpoint, on both sides. -/ +theorem JAtZB.recvSenderHead + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {e : U64} (he : e ∈ Z.E) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap e.bv = some (.endpoint (.sendEP queue))) : + ∃ sd : U64, ∃ xs pl tl, sd ∈ Z.T ∧ queue = sd.bv :: tl ∧ (L.epQ s e).head = sd ∧ + σ.kheap sd.bv = some (.tcb xs) ∧ xs.tcbState = .blockedOnSend e.bv pl := by + have hI := B.xtop.irelK B.ks B.buf + have hR := hI.1.toE + obtain ⟨-, qs, hqs, hq, hQ, hB⟩ := hR.at_ep he hk + obtain ⟨sd, qs', rfl⟩ := List.exists_cons_of_ne_nil hqs + have hsT : sd ∈ Z.T := hQ.mem sd (by simp) + obtain ⟨xs, hks, -⟩ := hI.1.tcb sd hsT + have hx := hR.at_tcb hsT hks + obtain ⟨h4, hback⟩ := hB sd (by simp) + obtain ⟨e', pl, hsend⟩ := tsCode_four (hx.st.symm.trans h4) + have hb : e' = e.bv := by + have h := hx.blk e' (by rw [hsend]; rfl) + rw [hback] at h + exact h.symm + subst e' + exact ⟨sd, xs, pl, qs'.map UScalar.bv, hsT, by rw [hq]; rfl, by rw [epQ_eq]; exact hQ.head, hks, hsend⟩ + +/-- A receiver that will go straight to the endpoint state read. -/ +def RecvNoReturn (ct : U64) (blocking : Bool) (σ : Fl4Spec.State Unit) : Prop := + ∀ xt, σ.kheap ct.bv = some (.tcb xt) → + xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blocking = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none)) + +/-- One [J] sender-transfer leaf, at the matched concrete queue head and reply. +The expected spec outcome is the selected receive after any prior cancellation +and scheduling-context return. -/ +def RecvFromSenderFor (ar : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (ct e cb : U64) (sd g gr blocking : Bool) + (rc : capability.Cap) (rc' : Fl4Spec.Cap) (senderCase : Fl4Spec.Tcb → Prop) : Prop := + ∀ (r head : U64) (queue : List Fl4Spec.ObjRef) (sender receiver : Fl4Spec.Tcb) (pl : Fl4Spec.SenderPayload), + ct ∈ Z.T → e ∈ Z.E → head ∈ Z.T → σ.curThread = ct.bv → + σ.kheap ct.bv = some (.tcb receiver) → Fl4Spec.runnable receiver.tcbState = true → + (σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread ∨ + ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ ct.bv) → + (∀ dom prio, ct.bv ∉ σ.readyQueues dom prio) → ct.bv ∉ σ.releaseQueue → + (L.epQ s e).head = head → + σ.kheap e.bv = some (.endpoint (.sendEP (head.bv :: queue))) → + σ.kheap head.bv = some (.tcb sender) → sender.tcbState = .blockedOnSend e.bv pl → + RecvNoReturn ct blocking σ → + ReplyArg Z.R (L.mem s) rc rc' r → + JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr) → + (∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) → + senderCase sender → + endpoint.receive_from_sender st ar s a ct e r ⦃ out => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) ar AR out.1 out.2 z.2 ⦄ + +/-- The unresolved [J] producer for a basic, pushed-reply or timeout transfer +from the selected head. It is called only by the sender branch. -/ +structure RecvSenderLeaves (ar : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (ct e cb : U64) (sd g gr blocking : Bool) + (rc : capability.Cap) (rc' : Fl4Spec.Cap) : Prop where + basic : RecvFromSenderFor (L := L) ar AR Z s a σ ct e cb sd g gr blocking rc rc' (fun x => SendOK x rc) + push : RecvFromSenderFor (L := L) ar AR Z s a σ ct e cb sd g gr blocking rc rc' (fun x => PushOK x rc) + timeout : RecvFromSenderFor (L := L) ar AR Z s a σ ct e cb sd g gr blocking rc rc' TmoOK + +theorem RecvSenderLeaves.run + {ct e cb r head : U64} {sd g gr blocking : Bool} {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (hs : RecvSenderLeaves (L := L) arI AR Z s a σ ct e cb sd g gr blocking rc rc') + (ht : ct ∈ Z.T) (he : e ∈ Z.E) (hh : head ∈ Z.T) (hcur : σ.curThread = ct.bv) + {recv sender : Fl4Spec.Tcb} (hkt : σ.kheap ct.bv = some (.tcb recv)) + (hrun : Fl4Spec.runnable recv.tcbState = true) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread ∨ + ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ ct.bv) + (hnq : ∀ d p, ct.bv ∉ σ.readyQueues d p) (hnrl : ct.bv ∉ σ.releaseQueue) + (hhead : (L.epQ s e).head = head) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap e.bv = some (.endpoint (.sendEP (head.bv :: queue)))) + (hks : σ.kheap head.bv = some (.tcb sender)) {pl : Fl4Spec.SenderPayload} + (hss : sender.tcbState = .blockedOnSend e.bv pl) + (hnr : RecvNoReturn ct blocking σ) (hra : ReplyArg Z.R (L.mem s) rc rc' r) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) : + endpoint.receive_from_sender st arI s a ct e r ⦃ out => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + rcases sendOK_split rc hss with h | h | h + · exact hs.basic r head queue sender recv pl ht he hh hcur hkt hrun hact hnq hnrl hhead hk hks hss hnr hra hHd hHr h + · exact hs.push r head queue sender recv pl ht he hh hcur hkt hrun hact hnq hnrl hhead hk hks hss hnr hra hHd hHr h + · exact hs.timeout r head queue sender recv pl ht he hh hcur hkt hrun hact hnq hnrl hhead hk hks hss hnr hra hHd hHr h + +theorem recvAtSendHead {ct e r : U64} (blocking : Bool) + (h : stOf (L.mem s) e = 1#u64) {P : S × A → Prop} + (hLeaf : endpoint.receive_from_sender st arI s a ct e r ⦃ P ⦄) : + (do + let st1 ← obj.Ep.state st e s + if st1 = obj.EP_IDLE then do + let s3 ← endpoint.wait_for_sender st s ct e blocking r + ok (s3, a) + else if st1 = obj.EP_RECV then do + let s3 ← endpoint.wait_for_sender st s ct e blocking r + ok (s3, a) + else endpoint.receive_from_sender st arI s a ct e r) ⦃ P ⦄ := by + refine WP.spec_bind (P1.obj.Ep.state.spec s e) ?_ + rintro v rfl + rw [epState_eq, h] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.2]; decide)] + exact hLeaf + +set_option maxHeartbeats 4000000 in +/-- The endpoint has a waiting sender, with no state change before the endpoint read. -/ +theorem VmEptState.recvSendingNoReturnV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct e cb r : U64} {sd g gr blocking : Bool} {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (ht : ct ∈ Z.T) (he : e ∈ Z.E) (hcur : σ.curThread = ct.bv) + {recv : Fl4Spec.Tcb} (hkt : σ.kheap ct.bv = some (.tcb recv)) + (hrun : Fl4Spec.runnable recv.tcbState = true) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread ∨ + ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ ct.bv) + (hnq : ∀ d p, ct.bv ∉ σ.readyQueues d p) (hnrl : ct.bv ∉ σ.releaseQueue) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap e.bv = some (.endpoint (.sendEP queue))) + (hnr : RecvNoReturn ct blocking σ) (hra : ReplyArg Z.R (L.mem s) rc rc' r) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hs : RecvSenderLeaves (L := L) arI AR Z s a σ ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ out => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨head, sender, pl, tl, hh, rfl, hhead, hks, hss⟩ := B.recvSenderHead he hk + have htail := recvAtSendHead blocking (hI.1.toE.at_ep he hk).1 + (hs.run ht he hh hcur hkt hrun hact hnq hnrl hhead hk hks hss hnr hra hHd hHr) + have hxt := hI.1.toE.at_tcb ht hkt + rcases hnr recv hkt with hb | ⟨np, xn, hb, hkn, hna, hnor⟩ + · exact receive_ipc_pre s a ct e cb sd true g gr blocking rc rc' r hra + (optRef_none (hxt.bound.symm.trans hb)) htail + · obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + have hb1 : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hb + have hbz : L.mem s (tBoundA ct) ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hbz] at hb1 + exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + refine receive_ipc_preN hra hbound (by rw [← hbound]; exact hbz) hstN ?_ htail + cases blocking + · exact Or.inl rfl + · right + have hsN : xn.ntfnSc = optRef (L.mem s (nScA n)) := hxn.sc + obtain ⟨Q, R, -, hS⟩ := B.xtop.fin.1.1.1.lists + obtain ⟨xt, hkt', -, -, hsc, -⟩ := hS.tcb ct ht + rw [hkt] at hkt' + cases hkt' + by_cases hz : L.mem s (nScA n) = 0#u64 + · exact Or.inl hz + · refine Or.inr fun eh => hnor rfl ⟨?_, ?_⟩ + · rw [hsN, eh]; exact hsc.symm + · rw [hsN, Notif.optRef_ne hz]; simp + +set_option maxHeartbeats 6000000 in +/-- Return the bound notification's scheduling context, then reduce to the +same selected endpoint's actual sender transfer at that matched state. -/ +theorem VmEptState.recvSendingReturnV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct e cb r n : U64} {sd g gr : Bool} {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (ht : ct ∈ Z.T) (he : e ∈ Z.E) (hcur : σ.curThread = ct.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {recv : Fl4Spec.Tcb} (hkt : σ.kheap ct.bv = some (.tcb recv)) + (hn : n ∈ Z.N) {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hbn : recv.tcbBoundNotification = some n.bv) (hna : Fl4Spec.isActive xn = false) + (hlend : xn.ntfnSc = recv.tcbSchedContext ∧ xn.ntfnSc ≠ none) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap e.bv = some (.endpoint (.sendEP queue))) + (hra : ReplyArg Z.R (L.mem s) rc rc' r) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (afterReturn : ∀ s' σ' Ac' Lst', + L.mem s (tBoundA ct) = n → + notification.maybe_return_sched_context st s n ct = ok s' → + Fl4Spec.maybeReturnSc (Z := Unit) n.bv ct.bv σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR (Z.mv Z.G Z.vs) H Cs Ac' Lst' D s' a σ' → + RecvSenderLeaves (L := L) arI AR Z s' a σ' ct e cb sd g gr true rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) true rc ⦃ out => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) true rc' σ).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + have hC : CurInv σ := B.curInv (by rw [hni.2]; intro e; cases e) + have hnrl : ct.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hrun := (B.curRun.run hni) recv (by rw [hcur]; exact hkt) + have hnqT : NotQueued σ ct.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨i0, hi0⟩ := Option.ne_none_iff_exists'.1 hlend.2 + have hxs0 : recv.tcbSchedContext = some i0 := hlend.1.symm.trans hi0 + obtain ⟨y, m, hky, -⟩ := B.inv.back ct.bv recv i0 hkt hxs0 + obtain ⟨i, hiC, rfl⟩ := B.obj.sc _ y m hky + obtain ⟨σ2, he2, kt, ki, ko, ct2, csc2, -, -, _, -, hrel, hact2, -, hnq2⟩ := + maybeReturn_shape B.inv hkn hkt hky hxs0 hi0 hnqT + have hxt := hI.1.toE.at_tcb ht hkt + have hb : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hbn + have hb0 : L.mem s (tBoundA ct) ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hb; cases hb + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hb0] at hb + exact P1.eq_of_bv (Option.some.inj hb) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hcb := (B.faultEntryBlock hni ht hcur hkt hrun (by rw [← hcur]; exact hnq)).1 + refine receive_ipc_blockR_red hra hbound (by rw [← hbound]; exact hb0) hstN ?_ + refine WP.spec_mono (Vs.spec_eq_mp (B.maybeReturnBQ ht hn hiC hkt hkn hxs0 hi0 hnqT + ⟨hcb.nq, hcb.nr, hcb.na⟩ (fun u hu => by rw [hni.2] at hu; cases hu) + (by rw [← hcur]; exact hni.1))) ?_ + rintro s' ⟨hconc, -, σ', he', Ac', Lst', B2, F2⟩ + rw [the_result he2 he'] at B2 F2 + have hI2 := B2.xtop.irelK B2.ks B2.buf + have het : e.bv ≠ ct.bv := fun h => by rw [h, hkt] at hk; cases hk + have hei : e.bv ≠ i.bv := fun h => by rw [h, hky] at hk; cases hk + have hk2 : σ2.kheap e.bv = some (.endpoint (.sendEP queue)) := by rw [ko _ het hei]; exact hk + have hnt : n.bv ≠ ct.bv := fun h => by rw [h, hkt] at hkn; cases hkn + have hnic : n.bv ≠ i.bv := fun h => by rw [h, hky] at hkn; cases hkn + have hkn2 : σ2.kheap n.bv = some (.notification xn) := by rw [ko _ hnt hnic]; exact hkn + have hra2 : ReplyArg Z.R (L.mem s') rc rc' r := by + rcases hra with h | ⟨bb, r', h1, h2, hrR, h0⟩ + · exact Or.inl h + refine Or.inr ⟨bb, r', h1, h2, hrR, ?_⟩ + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply r hrR + obtain ⟨xr2, hkr2, hxr2⟩ := hI2.1.toE.reply r hrR + have hrt : r.bv ≠ ct.bv := fun h => by rw [h, hkt] at hkr; cases hkr + have hri : r.bv ≠ i.bv := fun h => by rw [h, hky] at hkr; cases hkr + rw [ko _ hrt hri, hkr] at hkr2 + cases hkr2 + rw [h0, optRef_zero] at hxr + exact optRef_none (hxr2.symm.trans hxr) + have hnr2 : ¬ (xn.ntfnSc = (Fl4Spec.tcbSchedContextUpdate (fun _ => none) recv).tcbSchedContext ∧ + xn.ntfnSc ≠ none) := fun h => h.2 h.1 + have hno : RecvNoReturn ct true σ2 := by + intro xt hkt' + rw [kt] at hkt' + cases hkt' + exact Or.inr ⟨n.bv, xn, hbn, hkn2, hna, fun _ => hnr2⟩ + have hsp := receiveIpc_lend_eq cb.bv (rightsOf sd true g gr) (replyArg_spec B.ip.e hI.1.toE hra) + (replyArg_spec B2.ip.e hI2.1.toE hra2) hkt kt hbn hbn hk hk2 hkn hkn2 hna he2 hnr2 + rw [show absCap (.Endpoint e cb sd true g gr) = .endpointCap e.bv cb.bv (rightsOf sd true g gr) from rfl, hsp] + obtain ⟨head, sender, pl, tl, hh, l, hhead, hks, hss⟩ := B2.recvSenderHead he hk2 + rw [l] at hk2 + have hLeaf := (afterReturn s' σ2 Ac' Lst' hbound hconc he2 (J.ofNat (V' := Z.vs) B2 F2)).run + ht he hh (ct2.trans hcur) kt hrun (Or.inr (Or.inl (hact2 hcur.symm))) hnq2 (fun h => hnrl (hrel _ h)) + hhead hk2 hks hss hno hra2 (jHeld_of_same F2.ss hHd) (fun q g' h => jHeld_of_same F2.ss (hHr q g' h)) + unfold Endp.recvTail + exact recvAtSendHead true (hI2.1.toE.at_ep he hk2).1 hLeaf + +end +end Fl4Proofs.P3 + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The checked reply leaves the initial state unchanged, or cancels the +other TCB reached through that same reply with both results matched. -/ +def RecvReplyReached (ar : machine.Arch A) (ct : U64) (rc : capability.Cap) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) : Prop := + (s1 = s ∧ a1 = a ∧ σ1 = σ) ∨ + ∃ q g' i, rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = i ∧ i ≠ 0#u64 ∧ i ≠ ct ∧ + ipc.cancel_ipc st ar s a i = ok (s1, a1) ∧ Fl4Spec.cancelIpc (Z := Unit) i.bv σ = ({((), σ1)}, False) + +/-- The optional scheduling-context return follows the actual bound notification read. -/ +def RecvCtxReached (ct : U64) (blocking : Bool) (s : S) (σ : Fl4Spec.State Unit) + (s1 : S) (σ1 : Fl4Spec.State Unit) : Prop := + (s1 = s ∧ σ1 = σ) ∨ + ∃ n, blocking = true ∧ L.mem s (tBoundA ct) = n ∧ + notification.maybe_return_sched_context st s n ct = ok s1 ∧ + Fl4Spec.maybeReturnSc (Z := Unit) n.bv ct.bv σ = ({((), σ1)}, False) + +/-- Actual reply handling followed by the optional scheduling-context return. -/ +def RecvSenderReached (ar : machine.Arch A) (ct : U64) (blocking : Bool) (rc : capability.Cap) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (s2 : S) (a2 : A) (σ2 : Fl4Spec.State Unit) : Prop := + ∃ s1 σ1, RecvReplyReached (L := L) ar ct rc s a σ s1 a2 σ1 ∧ + RecvCtxReached (L := L) ct blocking s1 σ1 s2 σ2 + +/-- The [J] sender-transfer leaves along the actual receive prefix, +for the endpoint and reply that were checked at the start. -/ +def RecvSenderPrefixes (ar : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (H : P1.Heap) (Cs : Set U64) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (ct e cb : U64) (sd g gr blocking : Bool) + (rc : capability.Cap) (rc' : Fl4Spec.Cap) : Prop := + ∀ (V X : Set U64) (XL : U64 → Nat) (Z : XSets) [VxE Z] (Ac : Set U64) (Lst : U64 → List U64) (D : Set U64) + (s' : S) (a' : A) (σ' : Fl4Spec.State Unit), + RecvSenderReached (L := L) ar ct blocking rc s a σ s' a' σ' → + VmEptState (L := L) ar V X XL AR Z H Cs Ac Lst D s' a' σ' → + RecvSenderLeaves (L := L) ar AR Z s' a' σ' ct e cb sd g gr blocking rc rc' + +set_option maxHeartbeats 8000000 in +private theorem VmEptState.recvFreeEndpointV [VxE Z] + (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (e cb : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap e.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hFree : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64) + (hSender : ∀ s' σ' Z' [VxE Z'] Ac' Lst', + RecvCtxReached (L := L) ct blocking s σ s' σ' → + VmEptState (L := L) arI V X XL AR Z' H Cs Ac' Lst' D s' a σ' → + RecvSenderLeaves (L := L) arI AR Z' s' a σ' ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hPoll : RecvIpcPollCase (L := L) ct blocking s σ e rc + · exact J.recvIpcPollCaseVE ht blocking e cb sd g gr rc rc' hEp hRc hPoll + by_cases hAct : RecvIpcActCase (L := L) ct s σ rc + · exact J.recvIpcActCaseVE hAT hni hnq ht hcur blocking e cb sd g gr rc rc' hEp hRc hAct + by_cases hWait : RecvIpcBlockCase (L := L) ct blocking s σ e rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ e rc + · exact J.recvIpcWaitCaseVE hni hnq ht hcur blocking e cb sd g gr rc rc' hEp hRc hHd hHr hWait + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + have hrun := (B.curRun.run hni) xt (by rw [hcur]; exact hkt) + have hq : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hrl : ct.bv ∉ σ.releaseQueue := by rw [← hcur]; exact (B.curInv (by rw [hni.2]; intro e; cases e)).notRel + have hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false := by + cases hbn : xt.tcbBoundNotification with + | none => exact Or.inl rfl + | some n => + obtain ⟨xn, hkn⟩ := B.sv.tcbNtfn ct.bv xt n hkt hbn + refine Or.inr ⟨n, xn, rfl, hkn, ?_⟩ + cases hxo : xn.ntfnObj with + | activeNtfn b => exact (hAct ⟨⟨xt, n, xn, b, hkt, hbn, hkn, hxo⟩, hFree⟩).elim + | idleNtfn => simp [Fl4Spec.isActive, hxo] + | waitingNtfn q => simp [Fl4Spec.isActive, hxo] + have hkSend : ∃ queue, σ.kheap e.bv = some (.endpoint (.sendEP queue)) := by + by_contra hSend + have hn : ∀ x, σ.kheap e.bv = some (.endpoint x) → ∀ queue, x ≠ .sendEP queue := by + rintro x hx q rfl + exact hSend ⟨q, hx⟩ + cases blocking + · refine hPoll ⟨rfl, ?_, hn, hFree⟩ + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + exact hbN + · rcases hbN with hb | ⟨n, xn, hb, hkn, hna⟩ + · refine hWait (Or.inl ⟨rfl, ?_, hn, hFree⟩) + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + exact hb + · exact hWait (Or.inr ⟨rfl, ⟨xt, n, xn, hkt, hb, hkn, hna⟩, hn, hFree⟩) + obtain ⟨queue, hk⟩ := hkSend + obtain ⟨ee, he, eq⟩ := B.obj.ep _ (.sendEP queue) hk + rw [Sched.bv_inj eq] at he + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hFree + by_cases hReturn : blocking = true ∧ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none + · obtain ⟨rfl, np, xn, hb, hkn, hsc⟩ := hReturn + have hna : Fl4Spec.isActive xn = false := by + rcases hbN with h | ⟨n', xn', hb', hkn', h⟩ + · rw [hb] at h + cases h + · rw [hb] at hb' + cases hb' + rw [hkn] at hkn' + cases hkn' + exact h + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + exact J.recvSendingReturnV ht he hcur hni hnq hkt hn hkn hb hna hsc hk hra hHd hHr + (fun s' σ' Ac' Lst' hread hc hs J' => + hSender s' σ' (Z.mv Z.G Z.vs) Ac' Lst' (Or.inr ⟨n, rfl, hread, hc, hs⟩) J') + · have hno : RecvNoReturn ct blocking σ := by + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + rcases hbN with hb | ⟨n, xn, hb, hkn, hna⟩ + · exact Or.inl hb + · exact Or.inr ⟨n, xn, hb, hkn, hna, fun hbl hsc => hReturn ⟨hbl, n, xn, hb, hkn, hsc⟩⟩ + exact J.recvSendingNoReturnV ht he hcur hkt hrun (Or.inl hni.2) hq hrl hk hno + hra hHd hHr + (hSender s σ Z Ac Lst (Or.inl ⟨rfl, rfl⟩) J) + +set_option maxHeartbeats 8000000 in +/-- The endpoint leaf of receive obtained from only the three exact +sender-transfer premises at the reached states. -/ +theorem VmEptState.recvEndpointReachedV [VxE Z] + (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (e cb : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap e.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hSender : RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hFree : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 + · exact J.recvFreeEndpointV hAT ht hcur hni hnq blocking e cb sd g gr rc rc' hEp hRc hHd hHr hFree + (fun s' σ' Z' [VxE Z'] Ac' Lst' hCtx J' => + hSender V X XL Z' Ac' Lst' D s' a σ' ⟨s, σ, Or.inl ⟨rfl, rfl, rfl⟩, hCtx⟩ J') + rcases hRc with ⟨hr, -⟩ | ⟨hcr, q, g', rfl, xr, hkr⟩ + · exact (hFree (Or.inl hr)).elim + · have hne : L.mem s (Endp.rTcbA q) ≠ 0#u64 := fun h => hFree (Or.inr ⟨q, g', rfl, h⟩) + refine J.recvIpcLinkedVE ht hcur hni hnq blocking e cb sd g gr q g' rc' hcr hEp hkr hHd (hHr q g' rfl) hne ?_ + intro i V' Ac' Lst' s' a' σ' hread hi0 hiNe hc hs J' hcur' hni' hnq' hEp' hx h0' hHd' hHr' + obtain ⟨xr', hkr', -⟩ := hx + exact J'.recvFreeEndpointV hAT ht hcur' hni' hnq' blocking e cb sd g gr (.Reply q g') rc' hEp' + (Or.inr ⟨hcr, q, g', rfl, xr', hkr'⟩) hHd' (fun _ _ _ => hHr') (Or.inr ⟨q, g', rfl, h0'⟩) + (fun s2 σ2 Z2 [VxE Z2] Ac2 Lst2 hCtx J2 => + hSender V X XL Z2 Ac2 Lst2 D s2 a' σ2 + ⟨s', σ', Or.inr ⟨q, g', i, rfl, hread, hi0, hiNe, hc, hs⟩, hCtx⟩ J2) + +/-- Refine `handle_recv` using only the reached sender-transfer leaves. -/ +theorem VmEptState.handleRecvReachedV [VxE Z] + (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (blocking canReply : Bool) + (hSender : ∀ e b sd g gr rc rc', + (∃ x, σ.kheap e.bv = some (.endpoint x)) → + ((rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) → + JHeld (L := L) Z.th s (.Endpoint e b sd true g gr) → + (∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) → + RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e b sd g gr blocking rc rc') : + syscall.handle_recv st arI s a blocking canReply ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) blocking canReply σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, -⟩ := J.1.1.curEq hct + have ht := TopSR.cur_mem J.1.1.xtop.fin.1 hcur.symm + refine J.handleRecvV hAT hni hct hnq hvfh blocking canReply ?_ + intro e b sd g gr rc rc' hEp hRc hHd hHr _ _ + exact J.recvEndpointReachedV hAT ht hcur hni hnq blocking e b sd g gr rc rc' hEp hRc hHd hHr + (hSender e b sd g gr rc rc' hEp hRc hHd hHr) + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ReplyArm.lean b/proofs/Fl4Proofs/P3/VmEntryS3ReplyArm.lean new file mode 100644 index 000000000..f115a14f0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ReplyArm.lean @@ -0,0 +1,78 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm): **the selected Reply cap at `VmEptState`**. + +`VmEptState.selectedReplyS3` is P3/VmEntryS3SendF's `selectedEndpointS3F` for `.Reply p g`: `decode_invocation` of a Reply cap is +`set_thread_state Restart` (`VmEptState.restartK`) then `do_reply_transfer` (`VmEptState.replyXferV`, P3/VmEntryS3ReplyTmo) and +`Ok`; the spec's decode is `specDec_reply` and its perform `mem_perform_reply`. New file; no existing statement changed. + +Premises: `hAT`, `hmax`, `htu`, `hmb` [P] (as the native `replyLeafJ`); `hRT` [J] (`RunTmoPV` for a timeout handler endpoint with +receivers waiting; the native `runTmoP_recv`). +-/ +import Fl4Proofs.P3.VmEntryS3ReplyTmo +import Fl4Proofs.P3.VmEntryS3Send +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A reply cap in a slot names one of the replies (`JAtZ.slotR`, at a `VmEptState`). -/ +theorem VmEptState.slotR (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {sl : Fl4Spec.CslotPtr} + (hA : AbsSlot Z.th sl) {p : U64} {g : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Reply p g)) : p ∈ Z.R := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact J.1.1.hset.2.2 p ((hJ.typed _ hA.isSlot).2.2 p g (capV_of_capAt hc)) + +set_option maxHeartbeats 16000000 in +/-- Invoke the selected Reply cap (module docstring). -/ +theorem VmEptState.selectedReplyS3 [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hRT : RunTmoPV (L := L) arI V X XL AR Z (fun xe => ¬ Endp.CanBlock false xe)) + {ct cptr len buf lbl p : U64} {g : Bool} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Reply p g) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) + (.Reply p g) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Reply p g) exs buffer) s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + dsimp only + have hpR : p ∈ Z.R := J.slotR q.absSlot q.capAt + obtain ⟨s1, σ1, Lst1, x, hcur, hk, hs1, hst, J1, eσ1, hslots, _, hni1⟩ := J.restartK hni + have hct : ct = L.curW s := eq_of_ok_eq (q.current.symm.trans (cur_thread_eq s)) + rw [← hct] at hcur hk hs1 eσ1 + rw [hs1, bind_ok] + have hcur1 : σ1.curThread = ct.bv := by + rw [eσ1]; unfold Notif.stsAct; split <;> exact hcur + have hk1 : σ1.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + rw [eσ1, stsAct_kheap] + simp [Notif.updK] + have hcs : σ1.curThread = ct.bv ∧ ∃ xc, σ1.kheap ct.bv = some (.tcb xc) ∧ xc.tcbState = .restart := + ⟨hcur1, { x with tcbState := .restart }, hk1, rfl⟩ + refine WP.spec_bind (J1.replyXferV hAT hmax htu hmb hpR g hcs hRT) ?_ + rintro ⟨s2, a2⟩ ⟨z, hz, J2⟩ + apply (WP.spec_ok _).2 + refine Or.inr ⟨.base (.invokeReply p.bv g), ?_, σ1, hst, + (.ok [], z.2), ?_, Or.inl ⟨rfl, rfl, J2⟩⟩ + · exact (DecOut.of_eq + (specDec_reply fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl p g exs buffer σ)).liftME + · rw [← hcur1] at hz + exact mem_perform_reply (blk := blk) (call := call) (don := don) hz + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ReplyFault.lean b/proofs/Fl4Proofs/P3/VmEntryS3ReplyFault.lean new file mode 100644 index 000000000..b6b2f8a0e --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ReplyFault.lean @@ -0,0 +1,963 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm): **the fault branch of the rest of `do_reply_transfer` at `VmEptState`**. + +* `VmEptState.sanWrV`, `faultClearV`, `stsIV`, `rstPostV`, `switchV`: the register write of a sanitised register, `clear_fault`, + `set_thread_state` of an `Inactive` thread to a state of code below 3, `Restart` then `postpone` + (`JAtZB.rstPostJV`, P3/JAtZBRstPost) and `possible_switch_to` at `VmEptState`: the native `JAtZB` step then + `VmEptState.ofNat` with `NatFoot.ofT` (as P3/VmEntryS3ReplyNormal's `replyRunV`/`replyPostV`). +* `Endp.cmfrV_loop0/loop1/top`, `VmEptState.hfrV`: P3/XTopJointFaultReply's `copy_mrs_fault_reply` loops and + `handle_fault_reply` at `VmEptState` (generated from them by `JAtZ` read as `VmEptState`). +* `faultBranchV`: P3/XTopJointIpcJ's `faultBranchJ` at `VmEptState`; `replyRestFaultV`: its `replyRestFaultJ`, ending at + `VEAnyX` (no `hTransfer`: the fault reply reads the replier's registers and buffer and writes the receiver's registers). + +Generated by `handoff/tools/gen_fault.py` from the native proofs; new file, no existing statement changed. Premises: none new +(`hmb` is `min_budget` as in `replyRestSwV`). +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmEntryS3ReplyXfer +import Fl4Proofs.P3.XTopJointFaultReply +import Fl4Proofs.P3.XTopJointIpcJ +import Fl4Proofs.P3.JAtZBRstPost +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section Wrap +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {VX XX : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 8000000 in +/-- **The next sanitised register of the target at `J`** (module docstring). -/ +theorem VmEptState.sanWrV {σ0 : Fl4Spec.State Unit} {p : U64} {x0 : Fl4Spec.Tcb} {rs : List Fl4Spec.Register} + {vs : List Fl4Spec.Word} (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a (updK σ0 p.bv (.tcb (sanRegs x0 rs vs)))) + (hpT : p ∈ Z.T) (hl : rs.length = vs.length) (r : Fl4Spec.Register) (v : Fl4Spec.Word) (w : U64) + (hw : w.bv = Fl4Spec.sanitiseRegister false r v) : + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (Endp.regA p (regIdx r)) w) a + (updK σ0 p.bv (.tcb (sanRegs x0 (rs ++ [r]) (vs ++ [v])))) := by + have hk : (updK σ0 p.bv (.tcb (sanRegs x0 rs vs))).kheap p.bv = some (.tcb (sanRegs x0 rs vs)) := by + simp [updK] + have hX := reg_wr_irelN J.ip (J.xtop.irelK J.ks J.buf) hpT hk r w + obtain ⟨V, J1⟩ := J.regWrJ hpT hk r w hX + refine ⟨V, ?_⟩ + have e : updK (updK σ0 p.bv (.tcb (sanRegs x0 rs vs))) p.bv (.tcb (setReg (sanRegs x0 rs vs) r w.bv)) = + updK σ0 p.bv (.tcb (sanRegs x0 (rs ++ [r]) (vs ++ [v]))) := by + rw [sanRegs_snoc x0 rs vs hl r v, ← hw]; simp only [updK, Function.update_idem] + rw [← e]; exact J1 + +set_option maxHeartbeats 8000000 in +/-- **`clear_fault` at `VmEptState`** (`JAtZB.faultClearJ`, then `VmEptState.ofNat`). -/ +theorem VmEptState.faultClearV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hns : ∀ e d, x.tcbState ≠ .blockedOnSend e d) : + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (tFaultA t) 0#u64) a + (updK σ t.bv (.tcb { x with tcbFault := none })) := + let ⟨V, B', F⟩ := J.1.1.faultClearJ ht hk hns + ⟨V, J.ofNat B' F⟩ + +set_option maxHeartbeats 8000000 in +/-- **`set_thread_state` of an `Inactive` thread at `VmEptState`** (`JAtZB.stsIJ`, then `VmEptState.ofNat`). -/ +theorem VmEptState.stsIV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (htD : t ∉ D) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (ts : Fl4Spec.ThreadState) (hts : tsCode ts < 3) (v : U64) (hv : v.val = tsCode ts) + (hrel : inClass ts → RelIfBound σ t.bv) + (hB : sched.set_thread_state st s t v ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t v ⦃ s6 => ∃ (Lst' : U64 → List U64), + VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ⦄ := by + have B := J.1.1 + refine WP.spec_mono (B.stsIJ ht htD hk hrs ts hts v hv hrel hB) ?_ + rintro s' ⟨Lst', B', hw⟩ + have hk' := natK_tcbW (t := t.bv) + (σ' := stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) + (y := { x with tcbState := ts }) hk (stsAct_kheap _ _) (fun e => by + have h : ts = .runningVM := e + subst h; simp [tsCode] at hts) + refine ⟨Lst', ?_⟩ + simpa using J.ofNat (V' := Z.vs) (by simpa using B') (NatFoot.ofT B.xtop hw (fun _ h => B.tfoot_nh h) hk') + +set_option maxHeartbeats 16000000 in +/-- **`Restart`, then `postpone`, at `VmEptState`** (`JAtZB.rstPostJV`, then `VmEptState.ofNat`; as `replyPostV`). -/ +theorem VmEptState.rstPostV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hi : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hs : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (ha : Fl4Spec.scActive y = true) + (hnr : ¬ (Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true ∧ + Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true)) : + sched.set_thread_state st s t sched.TS_RESTART ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := .restart })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := .restart })) t.bv) ∧ + sched.postpone st s6 sc ⦃ s7 => ∃ σ7, Fl4Spec.postpone (Z := Unit) sc.bv + (stsAct (updK σ t.bv (.tcb { x with tcbState := .restart })) t.bv) = ({((), σ7)}, False) ∧ + ∃ Lst', VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst' D s7 a σ7 ⦄ ⦄ := by + have B := J.1.1 + refine WP.spec_mono (B.rstPostJV ht htD hk hi htc hs hxsc hky ha hnr) ?_ + rintro s6 ⟨hX, hI, hpost⟩ + refine ⟨hX, hI, WP.spec_mono hpost ?_⟩ + rintro s7 ⟨σ7, hpp, hw, hk7, ho, Lst', B'⟩ + have hh : σ7.kheap = (updK σ t.bv (.tcb { x with tcbState := .restart })).kheap := by + funext p + by_cases e : p = t.bv + · subst p; simpa only [updK_kheap, Function.update_self] using hk7 + · simpa only [updK_kheap, Function.update_of_ne e] using ho p e + have hk' := natK_tcbW hk hh (fun e => by cases e) + refine ⟨σ7, hpp, Lst', ?_⟩ + simpa using J.ofNat (V' := Z.vs) (by simpa using B') (NatFoot.ofT B.xtop hw (fun _ h => B.tfoot_nh h) hk') + +set_option maxHeartbeats 8000000 in +/-- **`possible_switch_to` of a schedulable thread at `VmEptState`** (`JAtZB.switchJ`, then `VmEptState.ofNat`). -/ +theorem VmEptState.switchV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (hti : t.bv ≠ σ.idleThread) (htc : t.bv ≠ σ.curThread) (hs : Fl4Spec.schedulable t.bv σ = true) : + sched.possible_switch_to st s t ⦃ s' => ∃ σ', Fl4Spec.possibleSwitchTo (Z := Unit) t.bv σ = ({((), σ')}, False) ∧ + ∃ Lst' : U64 → List U64, VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst' D s' a σ' ⦄ := by + have B := J.1.1 + refine WP.spec_mono (WP.spec_and (B.switchJ ht hti htc hs) + (possible_switch_to_sfoot B.xtop.fin.1.1.2.2.1 B.xtop.fin.1.1.1 B.inv.actReady ht)) ?_ + rintro s' ⟨⟨σ', he, Lst', B'⟩, hw⟩ + have mem : ((), σ') ∈ (Fl4Spec.possibleSwitchTo (Z := Unit) t.bv σ).1 := by rw [he]; exact Set.mem_singleton _ + refine ⟨σ', he, Lst', ?_⟩ + simpa using J.ofNat (V' := Z.vs) (by simpa using B') (NatFoot.ofT B.xtop (WritesIn.tfoot_of_kfoot (WritesIn.kfoot_of_sfoot hw)) + (fun b hb => B.tfoot_nh hb) (natK_tk (nkt_possibleSwitchTo t.bv σ _ mem))) + +end Wrap + +end + +end Fl4Proofs.P3 + +namespace Fl4Proofs.P3.Endp +open fl4_core Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Ipc + +noncomputable section + +section Loops +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {a : A} + {VX XX : Set U64} {XL : U64 → Nat} + {SR : S → Fl4Spec.State Unit → Prop} + +set_option maxHeartbeats 8000000 in +/-- **The first loop of `copy_mrs_fault_reply` at `J`** (as `cmfr_loop0`). -/ +theorem cmfrV_loop0 (hH : EpHyps st Z.T Z.E Z.R Z.B SR) (hP : EPlaced Z.T Z.E Z.R Z.B) {s0 : S} + {σ0 : Fl4Spec.State Unit} + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {x0 : Fl4Spec.Tcb} + (hk0 : σ0.kheap rcv.bv = some (.tcb x0)) + {regs : Fl4Spec.Register → Fl4Spec.Word} (hreg : ∀ r, (L.mem s0 (regA snd (regIdx r))).bv = regs r) + {sl : Slice U64} {Lr : List Fl4Spec.Register} (hsl : sl.val = Lr.map regIdx) (hLr : Lr.length < 32) + (n : Usize) (hn : n.val ≤ Lr.length) {V : List Fl4Spec.Word} + (hV : ∀ i (h : i < min n.val 4), i < V.length ∧ V[i]! = regs (mrIdx i)) + (hI : RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 s0 [] []) + (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s0 a σ0) : + faults.copy_mrs_fault_reply_loop0 st s0 snd rcv sl n 0#usize ⦃ r => r.2.val = min n.val 4 ∧ + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 r.1 (Lr.take r.2.val) (V.take r.2.val) ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D r.1 a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take r.2.val) (V.take r.2.val)))) ⦄ := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have hlen : (Slice.len sl).val = Lr.length := by simp [Slice.len, hsl] + unfold faults.copy_mrs_fault_reply_loop0 + apply loop.spec_decr_nat (measure := fun x => n.val - x.2.val) + (inv := fun x => x.2.val ≤ min n.val 4 ∧ + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 x.1 (Lr.take x.2.val) (V.take x.2.val) ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D x.1 a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take x.2.val) (V.take x.2.val))))) + · rintro ⟨s', i⟩ ⟨hi4, hI', V', J'⟩ + unfold faults.copy_mrs_fault_reply_loop0.body + simp only at hi4 hI' J' ⊢ + by_cases hcn : i < n + swap + · rw [if_neg hcn, WP.spec_ok] + have : i.val = min n.val 4 := by have := us_lt_iff.not.1 hcn; omega + exact ⟨this, this ▸ hI', V', this ▸ J'⟩ + rw [if_pos hcn] + have hin : i.val < n.val := us_lt_iff.1 hcn + have h1 : i < Slice.len sl := us_lt_iff.2 (by omega) + rw [if_pos h1] + simp only [lift, bind_ok] + by_cases h2 : (UScalar.cast .U64 i : U64) < msg.N_MSG_REGISTERS + swap + · rw [if_neg h2, WP.spec_ok] + have : i.val = min n.val 4 := by + have h2' : ¬ i.val < 4 := fun h => h2 ((gt_iff_val).2 (by rw [hN, cast_us i (by omega)]; omega)) + omega + exact ⟨this, this ▸ hI', V', this ▸ J'⟩ + rw [if_pos h2] + have hi4' : i.val < 4 := by have := (gt_iff_val).1 h2; rw [hN, cast_us i (by omega)] at this; omega + have hiL : i.val < Lr.length := by omega + have hidx : Slice.index_usize sl i = ok (regIdx Lr[i.val]) := by + unfold Slice.index_usize + have : sl[i]? = some (regIdx Lr[i.val]) := by + simp [Slice.getElem?_Usize_eq, hsl, List.getElem?_map, List.getElem?_eq_getElem hiL] + rw [this] + rw [hidx] + simp only [bind_ok] + refine WP.spec_bind (msg_register_spec (UScalar.cast .U64 i) (by rw [cast_us i (by omega)]; exact hi4')) ?_ + rintro r5 rfl + rw [cast_us i (by omega)] + refine WP.spec_bind (obj.Tcb.reg.spec s' snd _) ?_ + rintro v rfl + obtain ⟨w, hw, hwv⟩ := sanitise_eq Lr[i.val] (L.mem s' (regA snd (regIdx (mrIdx i.val)))) + rw [hw] + simp only [bind_ok] + have hvv : (L.mem s' (regA snd (regIdx (mrIdx i.val)))).bv = V[i.val]! := by + rw [snd_reg_keep hP hs hr hne hI'.2.2.1, hreg, (hV i.val (by omega)).2] + rw [hvv] at hwv + have hl : (Lr.take i.val).length = (V.take i.val).length := by + simp [List.length_take]; have := (hV i.val (by omega)).1; omega + refine WP.spec_bind (WP.spec_and (set_reg_rg_inv hH hP hr hI' hl Lr[i.val] V[i.val]! w hwv) + (obj.Tcb.set_reg.spec s' rcv _ w)) ?_ + rintro s2 ⟨hI2, rfl⟩ + obtain ⟨V2, J2⟩ := J'.sanWrV hr hl Lr[i.val] V[i.val]! w hwv + obtain ⟨i6, h6e, h6v⟩ := spec_exists (Usize.add_spec (x := i) (y := 1#usize) (by scalar_tac)) + rw [h6e, bind_ok, WP.spec_ok] + have hi6 : i6.val = i.val + 1 := by simpa using h6v + refine ⟨⟨by show i6.val ≤ min n.val 4; rw [hi6]; exact Nat.le_min.2 ⟨by omega, by omega⟩, ?_⟩, + by show n.val - i6.val < n.val - i.val; omega⟩ + show RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 _ (Lr.take i6.val) (V.take i6.val) ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D _ a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take i6.val) (V.take i6.val)))) + have hVl := (hV i.val (by omega)).1 + rw [hi6, List.take_succ, List.take_succ, List.getElem?_eq_getElem hiL, List.getElem?_eq_getElem hVl] + simp only [Option.toList_some] + rw [← getElem!_pos V i.val hVl] + exact ⟨hI2, V2, J2⟩ + · refine ⟨by simp, by simpa using hI, Z.vs, ?_⟩ + show VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G Z.vs) H Cs Ac Lst D s0 a (updK σ0 rcv.bv (.tcb (sanRegs x0 [] []))) + rw [show sanRegs x0 [] [] = x0 from rfl, Endp.updK_self hk0] + simpa using J + +set_option maxHeartbeats 8000000 in +/-- **The second loop of `copy_mrs_fault_reply` at `J`** (as `cmfr_loop1`). -/ +theorem cmfrV_loop1 (hH : EpHyps st Z.T Z.E Z.R Z.B SR) (hP : EPlaced Z.T Z.E Z.R Z.B) {s0 : S} + {σ0 : Fl4Spec.State Unit} + (hR0 : EpStateRel Z.T Z.E Z.R Z.B (L.mem s0) σ0) {rcv : U64} (hr : rcv ∈ Z.T) {x0 : Fl4Spec.Tcb} + {sw : U64} {sp : Fl4Spec.Word} (hms : BufMap Z.B sw sp) + {sl : Slice U64} {Lr : List Fl4Spec.Register} (hsl : sl.val = Lr.map regIdx) (hLr : Lr.length < 32) + (n : Usize) (hn : n.val ≤ Lr.length) (hn120 : n.val ≤ 120) {V : List Fl4Spec.Word} + (hV : ∀ i, 4 ≤ i → i < n.val → i < V.length ∧ + V[i]! = loadWordAt σ0.machineState.underlyingMemory (bufAddr sp (i + 1))) + (s1 : S) (i0 : Usize) (hi0 : 4 ≤ i0.val) (hi0n : i0.val ≤ n.val) + (hI : RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 s1 (Lr.take i0.val) (V.take i0.val)) + (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s1 a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take i0.val) (V.take i0.val))))) : + faults.copy_mrs_fault_reply_loop1 st s1 rcv sl n i0 sw ⦃ s2 => + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 s2 (Lr.take n.val) (V.take n.val) ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D s2 a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take n.val) (V.take n.val)))) ⦄ := by + have hlen : (Slice.len sl).val = Lr.length := by simp [Slice.len, hsl] + unfold faults.copy_mrs_fault_reply_loop1 + apply loop.spec_decr_nat (measure := fun x => n.val - x.2.val) + (inv := fun x => 4 ≤ x.2.val ∧ x.2.val ≤ n.val ∧ + RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 x.1 (Lr.take x.2.val) (V.take x.2.val) ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D x.1 a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take x.2.val) (V.take x.2.val))))) + · rintro ⟨s', i⟩ ⟨hi4, hin', hI', V', J'⟩ + unfold faults.copy_mrs_fault_reply_loop1.body + simp only at hi4 hin' hI' J' ⊢ + by_cases hcn : i < n + swap + · rw [if_neg hcn, WP.spec_ok] + have : i.val = n.val := by have := us_lt_iff.not.1 hcn; omega + exact ⟨this ▸ hI', V', this ▸ J'⟩ + rw [if_pos hcn] + have hin : i.val < n.val := us_lt_iff.1 hcn + have h1 : i < Slice.len sl := us_lt_iff.2 (by omega) + rw [if_pos h1] + have hiL : i.val < Lr.length := by omega + have hidx : Slice.index_usize sl i = ok (regIdx Lr[i.val]) := by + unfold Slice.index_usize + have : sl[i]? = some (regIdx Lr[i.val]) := by + simp [Slice.getElem?_Usize_eq, hsl, List.getElem?_map, List.getElem?_eq_getElem hiL] + rw [this] + rw [hidx] + simp only [lift, bind_ok] + set k := core.num.U64.wrapping_add (UScalar.cast .U64 i) 1#u64 with hk + have hkv : k.val = i.val + 1 := by + rw [hk]; simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, cast_us i (by omega)]; omega + refine WP.spec_bind (msg.buf_get.spec s' sw k) ?_ + rintro v rfl + have hw := hms k (by omega) + have hvv : (L.mem s' (fl4BufA sw k)).bv = V[i.val]! := by + rw [buf_keep hP hr hI'.2.2.1 hw, hR0.buf _ hw, (hV i.val hi4 hin).2, hkv] + obtain ⟨w, hw', hwv⟩ := sanitise_eq Lr[i.val] (L.mem s' (fl4BufA sw k)) + rw [hw'] + simp only [bind_ok] + rw [hvv] at hwv + have hl : (Lr.take i.val).length = (V.take i.val).length := by + simp [List.length_take]; have := (hV i.val hi4 hin).1; omega + refine WP.spec_bind (WP.spec_and (set_reg_rg_inv hH hP hr hI' hl Lr[i.val] V[i.val]! w hwv) + (obj.Tcb.set_reg.spec s' rcv _ w)) ?_ + rintro s2 ⟨hI2, rfl⟩ + obtain ⟨V2, J2⟩ := J'.sanWrV hr hl Lr[i.val] V[i.val]! w hwv + obtain ⟨i5, h5e, h5v⟩ := spec_exists (Usize.add_spec (x := i) (y := 1#usize) (by scalar_tac)) + rw [h5e, bind_ok, WP.spec_ok] + have hi5 : i5.val = i.val + 1 := by simpa using h5v + refine ⟨⟨by show 4 ≤ i5.val; omega, by show i5.val ≤ n.val; omega, ?_⟩, + by show n.val - i5.val < n.val - i.val; omega⟩ + show RgInv (L := L) Z.T Z.E Z.R Z.B SR s0 σ0 rcv x0 _ (Lr.take i5.val) (V.take i5.val) ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D _ a + (updK σ0 rcv.bv (.tcb (sanRegs x0 (Lr.take i5.val) (V.take i5.val)))) + have hVl := (hV i.val hi4 hin).1 + rw [hi5, List.take_succ, List.take_succ, List.getElem?_eq_getElem hiL, List.getElem?_eq_getElem hVl] + simp only [Option.toList_some] + rw [← getElem!_pos V i.val hVl] + exact ⟨hI2, V2, J2⟩ + · exact ⟨hi0, hi0n, hI, Z.vs, by simpa using J⟩ + +end Loops + +section Top +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {a : A} + {VX XX : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 8000000 in +/-- **`copy_mrs_fault_reply` at `J`** over a register list `Lr` (as +`cmfr_top`): the receiver's registers `Lr` set to the sanitised `get_mrs` +words of the replier; its buffer looked up at the `J` state after the first +loop. -/ +theorem cmfrV_top + (s : S) (σ : Fl4Spec.State Unit) (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + {sl : Slice U64} {Lr : List Fl4Spec.Register} (hsl : sl.val = Lr.map regIdx) (hLr : Lr.length < 32) + (len : U64) (hlen : len.val ≤ 120) : + faults.copy_mrs_fault_reply st s snd rcv sl len ⦃ s' => ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D s' a + (updK σ rcv.bv (.tcb (sanRegs xr Lr + (specMrs xs.tcbArch.tcbContext.userRegs (ipcBufOf false xs) σ.machineState.underlyingMemory len.val)))) ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hR : EpStateRel Z.T Z.E Z.R Z.B (L.mem s) σ := hI.1.toE + have hHc := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hH := hHc.epHyps hP0 + have hSR := IpcRel.toSRN hI.1 hI.2 + have hP := hP0.e + set V := specMrs xs.tcbArch.tcbContext.userRegs (ipcBufOf false xs) σ.machineState.underlyingMemory len.val + with hVdef + have hxs := hR.at_tcb hs hks + have hreg : ∀ r, (L.mem s (regA snd (regIdx r))).bv = xs.tcbArch.tcbContext.userRegs r := fun r => hxs.regs r + have hslen : (Slice.len sl).val = Lr.length := by simp [Slice.len, hsl] + unfold faults.copy_mrs_fault_reply + simp only [lift, bind_ok] + obtain ⟨n, hn, hnv⟩ : ∃ n : Usize, (if len < UScalar.cast .U64 (Slice.len sl) then ok (UScalar.cast .Usize len) + else ok (Slice.len sl) : Result Usize) = ok n ∧ n.val = min len.val Lr.length := by + have hc : (UScalar.cast .U64 (Slice.len sl) : U64).val = Lr.length := by + rw [cast_us _ (by omega), hslen] + by_cases h : len < UScalar.cast .U64 (Slice.len sl) + · refine ⟨_, if_pos h, ?_⟩ + have := (gt_iff_val).1 h; rw [hc] at this + rw [UScalar.cast_val_eq] + rw [Nat.mod_eq_of_lt (by rw [UScalarTy.Usize_numBits_eq]; rcases System.Platform.numBits_eq with e | e <;> + rw [e] <;> omega)] + omega + · refine ⟨_, if_neg h, ?_⟩ + have : ¬ len.val < Lr.length := fun h' => h ((gt_iff_val).2 (by rw [hc]; exact h')) + rw [hslen]; omega + rw [hn] + simp only [bind_ok] + refine WP.spec_bind (cmfrV_loop0 (L := L) hH hP hs hr hne hkr hreg hsl hLr n (by omega) + (fun i hi => specMrs_lo xs.tcbArch.tcbContext.userRegs (ipcBufOf false xs) σ.machineState.underlyingMemory + len.val i (by omega) (by omega)) (rgInv_init hR hSR.1 hkr) J) ?_ + rintro ⟨s1, i2⟩ ⟨hi2, hI1, V1, J1⟩ + show (if i2 < n then _ else _) ⦃ _ ⦄ + have h2a : i2.val ≤ 4 := by rw [hi2]; exact Nat.min_le_right _ _ + have h2b : i2.val ≤ n.val := by rw [hi2]; exact Nat.min_le_left _ _ + have h2c : i2.val = n.val ∨ i2.val = 4 := by rw [hi2]; exact min_choice _ _ + have hna : n.val ≤ len.val := by rw [hnv]; exact Nat.min_le_left _ _ + have hnb : n.val ≤ Lr.length := by rw [hnv]; exact Nat.min_le_right _ _ + have hnc : n.val = len.val ∨ n.val = Lr.length := by rw [hnv]; exact min_choice _ _ + have hVl : V.length ≤ len.val := specMrs_len_le _ _ _ _ + have mle : ∀ k, (Lr.length ≤ k ∨ V.length ≤ k) → min Lr.length V.length ≤ k := fun k h => by + rcases h with h | h + · exact (Nat.min_le_left _ _).trans h + · exact (Nat.min_le_right _ _).trans h + have fin : ∀ s' k, min Lr.length V.length ≤ k → + (∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D s' a + (updK σ rcv.bv (.tcb (sanRegs xr (Lr.take k) (V.take k))))) → + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D s' a + (updK σ rcv.bv (.tcb (sanRegs xr Lr V))) := by + intro s' k hk h + rw [sanRegs_take xr Lr V k hk] at h; exact h + by_cases hlt : i2 < n + swap + · rw [if_neg hlt, WP.spec_ok] + refine fin s1 i2.val (mle _ ?_) ⟨V1, J1⟩ + have := us_lt_iff.not.1 hlt + omega + rw [if_pos hlt] + have hi2n : i2.val < n.val := us_lt_iff.1 hlt + have hi24 : i2.val = 4 := by omega + -- the replier's buffer, looked up at the `J` state after the first loop + set σ1 := updK σ rcv.bv (.tcb (sanRegs xr (Lr.take i2.val) (V.take i2.val))) with hσ1 + have hk1 : σ1.kheap snd.bv = some (.tcb xs) := by + simp [hσ1, updK, show snd.bv ≠ rcv.bv from fun e => hne (eq_of_bv e), hks] + obtain ⟨Tr, Cq, hJ⟩ := J1.jpl + have thT : Z.th snd = some .tcb := (hJ.tcb snd).2 (J1.zH.1 snd hs) + obtain ⟨hOK1, hBC1, -⟩ := J1.xferH + obtain ⟨xs', hks', hws⟩ := J1.buf snd hs + rw [hk1] at hks'; cases hks' + refine WP.spec_bind (lookup_buf_full s1 σ1 J1.xtop.rel.2.1.1 J1.xtop.rel.2.1.2.1 thT hk1 hOK1 false) ?_ + rintro sw ⟨hsw0, hswp⟩ + cases hsb : ipcBufOf false xs with + | none => + have hswz : sw = 0#u64 := hsw0.2 hsb + rw [if_neg (by simp [hswz]), WP.spec_ok] + refine fin s1 i2.val (mle _ ?_) ⟨V1, J1⟩ + have : V.length ≤ 4 := by rw [hVdef, hsb]; exact specMrs_len_none _ _ _ + omega + | some sp => + have hsw0' : sw ≠ 0#u64 := fun e => by have := hsw0.1 e; rw [hsb] at this; cases this + have hms : BufMap Z.B sw sp := hBC1 snd hs xs false sp hk1 hsb sw (hswp hws sp hsb) + rw [if_pos (by simp [hsw0'])] + refine WP.spec_mono (cmfrV_loop1 (L := L) (Z := Z.mv Z.G V1) hH hP hR hr hms hsl hLr n (by omega) (by omega) (V := V) + (fun i h4 hin => by + rw [hVdef, hsb]; exact specMrs_hi xs.tcbArch.tcbContext.userRegs sp σ.machineState.underlyingMemory len.val i h4 + (by omega) (by omega)) s1 i2 + (by omega) (by omega) hI1 J1) ?_ + rintro s2 ⟨-, V2, J2⟩ + exact fin s2 n.val (mle _ (by omega)) ⟨V2, J2⟩ + +set_option maxHeartbeats 8000000 in +/-- **`handle_fault_reply` at `J`** (module docstring), as +`handle_fault_reply_rel`: the spec's outcome `σ'` is the receiver's TCB +updated in its user context (`x'`), and the whole spec prefix of the fault +reply up to the fault clear is identified. -/ +theorem _root_.Fl4Proofs.P3.VmEptState.hfrV + {s : S} {σ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {rcv snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) (hne : snd ≠ rcv) {xr xs : Fl4Spec.Tcb} + (hkr : σ.kheap rcv.bv = some (.tcb xr)) (hks : σ.kheap snd.bv = some (.tcb xs)) + {f : Fl4Spec.Fault} (hf : xr.tcbFault = some f) : + faults.handle_fault_reply st arI s a rcv snd ⦃ res => res.2.2 = a ∧ ∃ x', + Fl4Spec.handleFaultReply (Z := Unit) f rcv.bv + (Fl4Spec.dataToMessageInfo (xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register)).miLabel + (specMrs xs.tcbArch.tcbContext.userRegs (ipcBufOf false xs) σ.machineState.underlyingMemory + (Fl4Spec.unat (Fl4Spec.dataToMessageInfo + (xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register)).miLength)) σ = + ({(res.1, updK σ rcv.bv (.tcb x'))}, False) ∧ + (∀ z, z ∈ ((Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) rcv.bv >>= fun _ => + Fl4Spec.setThreadState (Z := Unit) rcv.bv (if res.1 then .restart else .inactive)) + (updK σ rcv.bv (.tcb x'))).1 → z ∈ (specFaultReply snd.bv rcv.bv f σ).1) ∧ + x' = { xr with tcbArch := x'.tcbArch } ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D res.2.1 a (updK σ rcv.bv (.tcb x')) ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hR : EpStateRel Z.T Z.E Z.R Z.B (L.mem s) σ := hI.1.toE + have hxs := hR.at_tcb hs hks + have hxr := hR.at_tcb hr hkr + obtain ⟨-, hBC, -⟩ := J.xferH + -- the replier's buffer words are aligned + have hal : ∀ sb, ipcBufOf false xs = some sb → ∀ k, k < 128 → (bufAddr sb k).toNat % 8 = 0 := by + intro sb hsb k hk + have hmb := hBC snd hs xs false sb hks hsb ⟨sb⟩ rfl + have hv : (⟨BitVec.ofNat 64 k⟩ : U64).val = k := by + show (BitVec.ofNat 64 k).toNat = k; simp; omega + have h := (hP0.e.balign _ (hmb ⟨BitVec.ofNat 64 k⟩ (by rw [hv]; omega))).2 + rw [hv] at h; exact h + have hwmi : (L.mem s (regA snd layout.reg.MSG_INFO)).bv = xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register := by + rw [← regIdx_msgInfo]; exact hxs.regs _ + set w := xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register with hwdef + set V := specMrs xs.tcbArch.tcbContext.userRegs (ipcBufOf false xs) σ.machineState.underlyingMemory + (Fl4Spec.unat (Fl4Spec.dataToMessageInfo w).miLength) with hVdef + -- the spec's prefix from its `handle_fault_reply` + have hpre : ∀ (rst : Bool) (σ1 : Fl4Spec.State Unit), + Fl4Spec.handleFaultReply (Z := Unit) f rcv.bv (Fl4Spec.dataToMessageInfo w).miLabel V σ = ({(rst, σ1)}, False) → + ∀ z, z ∈ ((Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) rcv.bv >>= fun _ => + Fl4Spec.setThreadState (Z := Unit) rcv.bv (if rst then .restart else .inactive)) σ1).1 → + z ∈ (specFaultReply snd.bv rcv.bv f σ).1 := by + intro rst σ1 h1 z hz + unfold specFaultReply + rw [nd_bind_one (getMessageInfo_eq hks), nd_bind_one (lookupIpcBuffer_eq hks false), + nd_bind_one (getMrs_eq hks _ hal _), nd_bind_one h1] + exact hz + have hfr := hxr.faultR + unfold FaultRel at hfr + rw [hf] at hfr + unfold faults.handle_fault_reply + refine WP.spec_bind (msg_info_spec (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, -, hle⟩ + rw [hwmi] at hlab hlen + have hlab0 : decide (tag.label = 0#u64) = decide ((Fl4Spec.dataToMessageInfo w).miLabel = 0) := by + refine decide_eq_decide.2 ⟨fun h => ?_, fun h => ?_⟩ + · show (w >>> 12) &&& Fl4Spec.mask Fl4Spec.msgLabelBits = 0 + rw [label_mask, ← hlab, h]; rfl + · apply UScalar.eq_of_val_eq + have : tag.label.bv = 0 := by rw [hlab]; rw [← label_mask w]; exact h + show tag.label.bv.toNat = _; rw [this]; rfl + have hlv : tag.length.val = Fl4Spec.unat (Fl4Spec.dataToMessageInfo w).miLength := by + show tag.length.bv.toNat = _; rw [hlen] + step with Fl4Proofs.P3.Endp.obj.Tcb.fault_type.spec as ⟨ty, hty⟩ + have same : ∀ rst, Fl4Spec.handleFaultReply (Z := Unit) f rcv.bv (Fl4Spec.dataToMessageInfo w).miLabel V σ = + ({(rst, σ)}, False) → + (ok (rst, s, a) : Result (Bool × S × A)) ⦃ res => res.2.2 = a ∧ ∃ x', + Fl4Spec.handleFaultReply (Z := Unit) f rcv.bv (Fl4Spec.dataToMessageInfo w).miLabel V σ = + ({(res.1, updK σ rcv.bv (.tcb x'))}, False) ∧ + (∀ z, z ∈ ((Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) rcv.bv >>= fun _ => + Fl4Spec.setThreadState (Z := Unit) rcv.bv (if res.1 then .restart else .inactive)) + (updK σ rcv.bv (.tcb x'))).1 → z ∈ (specFaultReply snd.bv rcv.bv f σ).1) ∧ + x' = { xr with tcbArch := x'.tcbArch } ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D res.2.1 a (updK σ rcv.bv (.tcb x')) ⦄ := by + intro rst h1 + rw [WP.spec_ok] + refine ⟨rfl, xr, by rw [Endp.updK_self hkr]; exact h1, + fun z hz => hpre rst σ h1 z (by rw [Endp.updK_self hkr] at hz; exact hz), rfl, Z.vs, ?_⟩ + rw [Endp.updK_self hkr]; simpa using J + have regs_case : ∀ (sl : Slice U64) (lst : List Fl4Spec.Register), sl.val = lst.map regIdx → lst.length < 32 → + (Fl4Spec.handleFaultReply (Z := Unit) f rcv.bv (Fl4Spec.dataToMessageInfo w).miLabel V σ = + ({(decide ((Fl4Spec.dataToMessageInfo w).miLabel = 0), updK σ rcv.bv (.tcb (sanRegs xr lst V)))}, False)) → + (do + let s2 ← faults.copy_mrs_fault_reply st s snd rcv sl tag.length + ok (decide (tag.label = 0#u64), s2, a)) ⦃ res => res.2.2 = a ∧ ∃ x', + Fl4Spec.handleFaultReply (Z := Unit) f rcv.bv (Fl4Spec.dataToMessageInfo w).miLabel V σ = + ({(res.1, updK σ rcv.bv (.tcb x'))}, False) ∧ + (∀ z, z ∈ ((Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) rcv.bv >>= fun _ => + Fl4Spec.setThreadState (Z := Unit) rcv.bv (if res.1 then .restart else .inactive)) + (updK σ rcv.bv (.tcb x'))).1 → z ∈ (specFaultReply snd.bv rcv.bv f σ).1) ∧ + x' = { xr with tcbArch := x'.tcbArch } ∧ + ∃ V', VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst D res.2.1 a (updK σ rcv.bv (.tcb x')) ⦄ := by + intro sl lst hsl hl hspec + refine WP.spec_bind (cmfrV_top (L := L) s σ J hs hr hne hks hkr hsl hl tag.length hle) ?_ + intro s2 hJ2 + rw [hlv] at hJ2 + refine (WP.spec_ok _).2 ⟨rfl, sanRegs xr lst V, by rw [hspec, hlab0], + fun z hz => hpre _ _ (by rw [hspec, hlab0]) z hz, ?_, hJ2⟩ + rw [← sanRegs_eq] + cases f with + | capFault c rp lf => + obtain ⟨h1, -⟩ := hfr + rw [hty, h1, if_pos (by unfold fl4_abi.consts.fault.CapFault; rfl)] + simpa using same 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] + simpa using regs_case _ _ Fl4Proofs.P3.Endp.syscall_slice_val (by decide) (hfr_regs_eq hkr Fl4Spec.syscallMessage (Fl4Spec.dataToMessageInfo w).miLabel V) + | 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] + simpa using regs_case _ _ Fl4Proofs.P3.Endp.exc_slice_val (by decide) (hfr_regs_eq hkr Fl4Spec.exceptionMessage (Fl4Spec.dataToMessageInfo w).miLabel V) + | 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] + simpa using regs_case _ _ timeout_slice_val (by decide) (hfr_regs_eq hkr Fl4Spec.timeoutMessage (Fl4Spec.dataToMessageInfo w).miLabel V) + | archFault af => + cases af with + | vmFault ad d => + 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_neg (by unfold fl4_abi.consts.fault.Timeout; decide), + if_neg (by unfold fl4_abi.consts.fault.DebugException; decide)] + simpa using same true rfl + +end Top + +end + +end Fl4Proofs.P3.Endp + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section Fault +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {VX XX : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 8000000 in +/-- **FL4's fault reply at `J`** (`fl4FaultReply`): `handle_fault_reply` +(XTopJointFaultReply's `VmEptState.hfrV`), `clear_fault` (`JAtZ.faultClearJ`), +then the receiver's new state by the given step `hQ` (for each outcome +`rst` of the spec's `handle_fault_reply`). The spec's fault reply ends at +`frSt`. -/ +theorem faultBranchV + {s2 : S} {ar : A} {σ2 : Fl4Spec.State Unit} (J2 : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s2 ar σ2) [VxE Z] + {ct i : U64} (hne : ct ≠ i) (hiT : i ∈ Z.T) (hcT : ct ∈ Z.T) {xs xi : Fl4Spec.Tcb} {f : Fl4Spec.Fault} + (hks : σ2.kheap ct.bv = some (.tcb xs)) (hki : σ2.kheap i.bv = some (.tcb xi)) (hfi : xi.tcbFault = some f) + (hns : ∀ e d, xi.tcbState ≠ .blockedOnSend e d) + (Q : Bool → S → Fl4Spec.State Unit → Prop) + (hQ : ∀ (rst : Bool) (x4 : Fl4Spec.Tcb) (s5 : S) (V : XVs), x4 = { xi with tcbArch := x4.tcbArch } → + VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s5 ar + (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })) → + sched.set_thread_state st s5 i (if rst then sched.TS_RESTART else sched.TS_INACTIVE) ⦃ s6 => + Q rst s6 (frSt σ2 i x4 rst) ⦄) : + fl4FaultReply st arI s2 ar i ct ⦃ res => res.2 = ar ∧ ∃ (rst : Bool) (x4 : Fl4Spec.Tcb), + x4 = { xi with tcbArch := x4.tcbArch } ∧ + Fl4Spec.handleFaultReply (Z := Unit) f i.bv (frLbl xs) (frMrs xs σ2) σ2 = + ({(rst, updK σ2 i.bv (.tcb x4))}, False) ∧ + ((), frSt σ2 i x4 rst) ∈ (specFaultReply ct.bv i.bv f σ2).1 ∧ Q rst res.1 (frSt σ2 i x4 rst) ⦄ := by + unfold fl4FaultReply + refine WP.spec_bind (J2.hfrV hiT hcT hne hki hks hfi) ?_ + rintro ⟨rst, s4, a3⟩ ⟨ha, x4, hhf, hpre, hx4, V4, J4⟩ + dsimp only at ha hhf hpre J4 + subst ha + have hk4 : (updK σ2 i.bv (.tcb x4)).kheap i.bv = some (.tcb x4) := by simp [updK] + have hk5 : (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap i.bv = + some (.tcb { x4 with tcbFault := none }) := by simp [updK] + have hmem : ∀ ts : Fl4Spec.ThreadState, ((), stsAct (updK (updK (updK σ2 i.bv (.tcb x4)) i.bv + (.tcb { x4 with tcbFault := none })) i.bv (.tcb { ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := ts })) i.bv) ∈ ((Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := none }) i.bv >>= + fun _ => Fl4Spec.setThreadState (Z := Unit) i.bv ts) (updK σ2 i.bv (.tcb x4))).1 := by + intro ts + rw [nd_bind_one (Notif.threadSet_eq _ hk4), Notif.setThreadState_eq _ hk5]; rfl + unfold faults.clear_fault + step with Fl4Proofs.P1.obj.Tcb.set_fault_type.spec as ⟨s5, hs5⟩ + have e5 : s5 = L.wr s4 (tFaultA i) 0#u64 := by rw [hs5]; unfold fl4_abi.consts.fault.NullFault; rfl + subst e5 + obtain ⟨V5, J5⟩ := J4.faultClearV hiT hk4 fun e d h => hns e d ((congrArg Fl4Spec.Tcb.tcbState hx4).symm.trans h) + have key := hQ rst x4 _ V5 hx4 J5 + cases rst + · simp only [Bool.false_eq_true, ↓reduceIte] at key hpre ⊢ + refine WP.spec_bind key ?_ + intro s6 hq + exact (WP.spec_ok _).2 ⟨rfl, false, x4, hx4, hhf, hpre _ (hmem _), hq⟩ + · simp only [eq_self_iff_true, if_true, ↓reduceIte] at key hpre ⊢ + refine WP.spec_bind key ?_ + intro s6 hq + exact (WP.spec_ok _).2 ⟨rfl, true, x4, hx4, hhf, hpre _ (hmem _), hq⟩ + +set_option maxHeartbeats 32000000 in +/-- **The rest of `do_reply_transfer` at `J`, for `ReplyRestFault`**: FL4's +fault word of the receiver is not 0 (`fault0`), so the fault reply +(`faultBranchJ`), the new state `Inactive` or `Restart` by `JAtZ.stsIJ` +(XTopJointStsI; `RelIfBound` with no SC or a released one), then the tail: +nothing for no SC or `Inactive`; `possible_switch_to` (`JAtZ.switchJ`) for a +released SC; for an SC not released, `Restart` and `postpone` as one step +(`JAtZ.rstPostJ`), the timeout handler invalid or the fault a timeout +(`faultT`: FL4's fault word 5) by `ReplyRestFault`. The spec's outcome by +`mem_doReplyTransfer` (the fault branch). -/ +theorem replyRestFaultV (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {s2 : S} {ar : A} {σ1 σ0 σ2 : Fl4Spec.State Unit} (J2 : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s2 ar σ2) [VxE Z] + {ct i p : U64} (g : Bool) (hne : ct ≠ i) (hiD : i ∉ D) (hct2 : σ2.curThread = ct.bv) + {xr : Fl4Spec.Reply} (hkr : σ1.kheap p.bv = some (.reply xr)) (hrep : xr.replyTcb = some i.bv) + {x : Fl4Spec.Tcb} (hkx : σ1.kheap i.bv = some (.tcb x)) (hst : x.tcbState = .blockedOnReply p.bv) + (hrm : Fl4Spec.replyRemove (Z := Unit) i.bv p.bv σ1 = ({((), σ0)}, False)) + (hub : (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext i.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ0 = ({((), σ2)}, False)) + (hin : ∃ x', σ2.kheap i.bv = some (.tcb x') ∧ x'.tcbState = .inactive) + (hact : ∀ q y n, σ2.kheap q = some (.schedContext y n) → y.scTcb = some i.bv → Fl4Spec.scActive y = true) + (hC : ReplyRestFault σ2 ct i) : + fl4ReplyRest2 st arI s2 ar ct i g ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + obtain ⟨xs, xi, f, hks, hki, hfi, hnt⟩ := hC + obtain ⟨xi', hki', hin⟩ := hin + rw [hki] at hki'; cases hki' + have hI := J2.1.1.xtop.irelK J2.ks J2.buf + have hiT : i ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.1.1.obj.tcb _ xi hki + rw [Sched.bv_inj h] at ht; exact ht + have hcT : ct ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.1.1.obj.tcb _ xs hks + rw [Sched.bv_inj h] at ht; exact ht + have hnr2 : i.bv ∉ σ2.releaseQueue := fun hm => by + obtain ⟨x', -, -, -, h1, -, h3, -⟩ := J2.1.1.inv.rel _ hm + rw [hki] at h1; cases h1; rw [hin] at h3; cases h3 + have hxr := hI.1.toE.at_tcb hiT hki + obtain ⟨Q0, R0, hc, hsp⟩ := J2.1.1.xtop.fin.1.1.1.lists + obtain ⟨x0, hk0, -, -, hscw, -⟩ := hsp.tcb i hiT + rw [hki] at hk0; cases hk0 + unfold fl4ReplyRest2 + step with Fl4Proofs.P3.Endp.obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ¬ ft = fl4_abi.consts.fault.NullFault := by + intro e + have h := hxr.fault0.1 (by rw [← hft, e]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hfi] at h; cases h + have hftT : ft = fl4_abi.consts.fault.Timeout ↔ Fl4Spec.isTimeoutFault f = true := by + rw [hft, show fl4_abi.consts.fault.Timeout = 5#u64 by unfold fl4_abi.consts.fault.Timeout; rfl, hxr.faultT, hfi] + cases f <;> simp [Fl4Spec.isTimeoutFault] + rw [if_neg hft0] + have e0 : U64.bv 0#u64 = (0 : Fl4Spec.Word) := rfl + -- the receiver's new state, `J` after it (no SC, or a released one, or `Inactive`) + have stsQ : ∀ (rst : Bool) (x4 : Fl4Spec.Tcb) (s5 : S) (V : XVs), x4 = { xi with tcbArch := x4.tcbArch } → + VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s5 ar + (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })) → + (rst = true → RelIfBound (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })) i.bv) → + sched.set_thread_state st s5 i (if rst then sched.TS_RESTART else sched.TS_INACTIVE) ⦃ s6 => + ∃ (V' : XVs) (Lst' : U64 → List U64), VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst' D s6 ar + (frSt σ2 i x4 rst) ⦄ := by + intro rst x4 s5 V hx4 J5 hrel + have hk5 : (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap i.bv = + some (.tcb { x4 with tcbFault := none }) := by simp [updK] + have hrs : ({ x4 with tcbFault := none } : Fl4Spec.Tcb).tcbState = .inactive := by + have h := congrArg Fl4Spec.Tcb.tcbState hx4; exact h.trans hin + obtain ⟨x5, hk5', hTR5⟩ := (J5.1.1.xtop.irelK J5.ks J5.buf).1.tcb i hiT + rw [hk5] at hk5'; cases hk5' + have hr0 : L.mem s5 (P1.stA i) = sched.TS_INACTIVE := by + apply UScalar.eq_of_val_eq + rw [P1.stA_eq, ← tStA_eq, hTR5.st, hrs]; unfold sched.TS_INACTIVE; rfl + have htH : i ∈ P1.Heap.tcbs H := J5.zH.1 i hiT + cases rst + · refine WP.spec_mono (J5.stsIV hiT hiD hk5 hrs .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) (fun h => by + rcases h with h | ⟨_, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h⟩ <;> cases h) + (P1.sched.set_thread_state.inactive_blkI (Es := P1.EH H) s5 i J5.1.1.blk htH hr0)) ?_ + rintro s6 ⟨Lst', J6⟩ + exact ⟨V, Lst', J6⟩ + · refine WP.spec_mono (J5.stsIV hiT hiD hk5 hrs .restart (by decide) sched.TS_RESTART + (by unfold sched.TS_RESTART; rfl) (fun _ => hrel rfl) + (P1.sched.set_thread_state.restart_blkI (Es := P1.EH H) s5 i J5.1.1.blk htH hr0)) ?_ + rintro s6 ⟨Lst', J6⟩ + exact ⟨V, Lst', J6⟩ + -- the spec's facts at the state after the fault reply + have hk6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), (frSt σ2 i x4 rst).kheap i.bv = some (.tcb { ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := if rst then .restart else .inactive }) := by + intro x4 rst; simp [frSt, stsAct_kheap, updK] + have hkp6 : ∀ x4 rst q, q ≠ i.bv → (frSt σ2 i x4 rst).kheap q = σ2.kheap q := by + intro x4 rst q hq; simp [frSt, stsAct_kheap, updK, hq] + have hrec6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), frSt σ2 i x4 rst = { (updK (updK (updK σ2 i.bv (.tcb x4)) i.bv + (.tcb { x4 with tcbFault := none })) i.bv (.tcb { ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := if rst then .restart else .inactive })) with + schedulerAction := (frSt σ2 i x4 rst).schedulerAction } := fun x4 rst => stsAct_rec _ _ + have htm6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), (frSt σ2 i x4 rst).curTime = σ2.curTime := fun x4 rst => by rw [hrec6]; rfl + have hct6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), (frSt σ2 i x4 rst).curThread = ct.bv := fun x4 rst => by rw [hrec6]; exact hct2 + have hrq6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), (frSt σ2 i x4 rst).releaseQueue = σ2.releaseQueue := fun x4 rst => by rw [hrec6]; rfl + have hsc6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), x4 = { xi with tcbArch := x4.tcbArch } → + ({ ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := if rst then Fl4Spec.ThreadState.restart else .inactive } : Fl4Spec.Tcb).tcbSchedContext = + xi.tcbSchedContext := by + intro x4 rst hx4; have h := congrArg Fl4Spec.Tcb.tcbSchedContext hx4; exact h + have hth6 : ∀ (x4 : Fl4Spec.Tcb) (rst : Bool), x4 = { xi with tcbArch := x4.tcbArch } → + ({ ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := if rst then Fl4Spec.ThreadState.restart else .inactive } : Fl4Spec.Tcb).tcbTimeoutHandler = + xi.tcbTimeoutHandler := by + intro x4 rst hx4; have h := congrArg Fl4Spec.Tcb.tcbTimeoutHandler hx4; exact h + have fin : ∀ (rst : Bool) (x4 : Fl4Spec.Tcb) (z : Unit × Fl4Spec.State Unit) (s' : S) (a' : A), + ((), frSt σ2 i x4 rst) ∈ (specFaultReply ct.bv i.bv f σ2).1 → + z ∈ (specReplyTail i.bv xi.tcbFault (frSt σ2 i x4 rst)).1 → VEAnyX (L := L) arI AR s' a' z.2 → + ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, VEAnyX (L := L) arI AR s' a' z.2 := + fun rst x4 z s' a' hz1 hz hJ => ⟨z, mem_doReplyTransfer hkr hrep hkx hst g hrm hub hki (Or.inr ⟨f, hfi, hz1⟩) hz, hJ⟩ + -- the receiver's SC word, and whether its SC is released + obtain ⟨i2, hi2⟩ : ∃ i2 : U64, i2 = L.mem s2 (P1.scA i) := ⟨_, rfl⟩ + rw [← hi2] at hscw + set relC : Prop := ∀ sp y n, xi.tcbSchedContext = some sp → σ2.kheap sp = some (.schedContext y n) → + Fl4Spec.refillReady σ2.curTime (Fl4Spec.refillHd y) = true ∧ Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true + with hrelC + -- the step after the fault reply: `Inactive`, `J`; `Restart`, `J` when `relC`, otherwise with the tail's `postpone` + refine WP.spec_bind (faultBranchV J2 hne hiT hcT hks hki hfi (fun e d h => by rw [hin] at h; cases h) + (fun rst s σ => (rst = false → ∃ (V' : XVs) (Lst' : U64 → List U64), + VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst' D s ar σ) ∧ + (rst = true → ∃ V' : XVs, XTopAt (L := L) AR (Z.mv Z.G V') s ar σ ∧ + (relC → ∃ Lst' : U64 → List U64, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst' D s ar σ) ∧ + (¬ relC → sched.postpone st s i2 ⦃ s7 => ∃ σ7, Fl4Spec.postpone (Z := Unit) i2.bv σ = ({((), σ7)}, False) ∧ + ∃ Lst' : U64 → List U64, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V') H Cs Ac Lst' D s7 ar σ7 ⦄))) + ?_) ?_ + · intro rst x4 s5 V hx4 J5 + have hk5 : (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap i.bv = + some (.tcb { x4 with tcbFault := none }) := by simp [updK] + have hsc5 : ({ x4 with tcbFault := none } : Fl4Spec.Tcb).tcbSchedContext = xi.tcbSchedContext := by + have h := congrArg Fl4Spec.Tcb.tcbSchedContext hx4; exact h + cases rst + · refine WP.spec_mono (stsQ false x4 s5 V hx4 J5 (fun h => absurd h Bool.false_ne_true)) ?_ + intro s6 h6 + exact ⟨fun _ => h6, fun h => absurd h Bool.false_ne_true⟩ + · by_cases hR : relC + · have hrel : RelIfBound (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })) i.bv := by + intro x' scp y' n' h1 h2 h3 _ _ + rw [hk5] at h1; cases h1 + have hsp' : xi.tcbSchedContext = some scp := hsc5.symm.trans h2 + have hti' : scp ≠ i.bv := fun e => by + rw [e] at h3; simp [updK] at h3 + have h3' : σ2.kheap scp = some (.schedContext y' n') := by simpa [updK, hti'] using h3 + exact hR scp y' n' hsp' h3' + refine WP.spec_mono (stsQ true x4 s5 V hx4 J5 (fun _ => hrel)) ?_ + rintro s6 ⟨V', Lst', J6⟩ + exact ⟨fun h => absurd h.symm Bool.false_ne_true, fun _ => ⟨V', J6.1.1.xtop, fun _ => ⟨Lst', J6⟩, + fun h => absurd hR h⟩⟩ + · -- the SC is bound and not released: `Restart` and `postpone` as one step + have hR' : ∃ sp y n, xi.tcbSchedContext = some sp ∧ σ2.kheap sp = some (.schedContext y n) ∧ + ¬ (Fl4Spec.refillReady σ2.curTime (Fl4Spec.refillHd y) = true ∧ + Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true) := by + by_contra hc + exact hR fun sp y n h1 h2 => by + by_contra h3; exact hc ⟨sp, y, n, h1, h2, h3⟩ + obtain ⟨sp, y, n, hxsc, hky, hnrel⟩ := hR' + have h0 : i2 ≠ 0#u64 := by + intro e; rw [e, optRef_zero, hxsc] at hscw; cases hscw + have esp : sp = i2.bv := by rw [Notif.optRef_ne h0, hxsc] at hscw; exact Option.some.inj hscw + subst esp + have hi2C : i2 ∈ Z.C := by rw [hi2]; exact hsp.scMem i hiT (by rw [← hi2]; exact h0) + obtain ⟨y0, n0, hky0, hyt⟩ := J2.1.1.inv.back _ _ _ hki hxsc + rw [hky] at hky0; cases hky0 + have hya := hact _ y n hky hyt + have hti : i2.bv ≠ i.bv := fun e => by rw [e, hki] at hky; cases hky + have hky5 : (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap i2.bv = + some (.schedContext y n) := by simp [updK, hti, hky] + have hrs : ({ x4 with tcbFault := none } : Fl4Spec.Tcb).tcbState = .inactive := by + have h := congrArg Fl4Spec.Tcb.tcbState hx4; exact h.trans hin + have htc5 : i.bv ≠ (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).curThread := by + show i.bv ≠ σ2.curThread + rw [hct2]; intro e; exact hne (P1.eq_of_bv e).symm + refine WP.spec_mono (J5.rstPostV hiT hiD hk5 hrs htc5 hi2C (hsc5.trans hxsc) hky5 hya hnrel) ?_ + rintro s6 ⟨hX6, -, hpost⟩ + exact ⟨fun h => absurd h.symm Bool.false_ne_true, fun _ => ⟨V, hX6, fun h => absurd h hR, fun _ => hpost⟩⟩ + rintro ⟨s6, a2⟩ ⟨ha, rst, x4, hx4, hhf, hz1, hq⟩ + dsimp only at ha hq + subst ha + -- the tail's reads at the state after the fault reply + have hX6 : ∃ V6 : XVs, XTopAt (L := L) AR (Z.mv Z.G V6) s6 a2 (frSt σ2 i x4 rst) := by + cases rst + · obtain ⟨V6, _, J6⟩ := hq.1 rfl; exact ⟨V6, J6.1.1.xtop⟩ + · obtain ⟨V6, hX, -⟩ := hq.2 rfl; exact ⟨V6, hX⟩ + obtain ⟨V6, hX6⟩ := hX6 + have hS6 := hX6.fin.1.1.1 + obtain ⟨Q6, R6, hc6, hsp6⟩ := hS6.lists + obtain ⟨x6, hk6', -, -, hscw6, -⟩ := hsp6.tcb i hiT + rw [hk6 x4 rst] at hk6'; cases hk6' + obtain ⟨x6', hk6'', hTR6⟩ := hX6.ipc.tcb i hiT + rw [hk6 x4 rst] at hk6''; cases hk6'' + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + step with Fl4Proofs.P3.Sched.obj.Sc.is_null.spec' as ⟨b2, hb2⟩ + by_cases h3 : i3 = 0#u64 + · -- no SC: nothing + rw [hb2, if_pos (by simp [h3]), WP.spec_ok] + have hn : xi.tcbSchedContext = none := by + have e := hscw6 + rw [← hi3, h3, optRef_zero] at e + exact (hsc6 x4 rst hx4).symm.trans e + have hJ6 : VEAnyX (L := L) arI AR s6 a2 (frSt σ2 i x4 rst) := by + cases rst + · obtain ⟨V', Lst', J6⟩ := hq.1 rfl; exact (J6.anyX arI) + · obtain ⟨V', -, hJ, -⟩ := hq.2 rfl + obtain ⟨Lst', J6⟩ := hJ (fun sp y n h => by rw [hn] at h; cases h) + exact (J6.anyX arI) + exact fin rst x4 ((), frSt σ2 i x4 rst) s6 a2 hz1 + (by rw [specReplyTail_skip (hk6 x4 rst) (Or.inl ((hsc6 x4 rst hx4).trans hn))]; rfl) hJ6 + rw [hb2, if_neg (by simp [h3])] + have e3 : xi.tcbSchedContext = some i3.bv := by + have e := hscw6 + rw [← hi3, Notif.optRef_ne h3] at e + exact (hsc6 x4 rst hx4).symm.trans e + have hi23 : i3 = i2 := by + rw [Notif.optRef_ne (fun e => by rw [e, optRef_zero, e3] at hscw; cases hscw), e3] at hscw + exact P1.eq_of_bv (Option.some.inj hscw) + subst hi23 + have hi2C : i3 ∈ Z.C := by + rw [hi2]; exact hsp.scMem i hiT (by rw [← hi2]; exact h3) + obtain ⟨y, n, hky, hyt⟩ := J2.1.1.inv.back _ _ _ hki e3 + have hya := hact _ y n hky hyt + have hti : i3.bv ≠ i.bv := fun e => by rw [e, hki] at hky; cases hky + have hky6 : (frSt σ2 i x4 rst).kheap i3.bv = some (.schedContext y n) := by rw [hkp6 x4 rst _ hti]; exact hky + refine WP.spec_bind (Fl4Proofs.P3.Sched.is_runnable_spec s6 i) ?_ + rintro b3 rfl + cases rst + · -- `Inactive`: not runnable, nothing + have hrw : L.mem s6 (tStA i) = sched.TS_INACTIVE := by + apply UScalar.eq_of_val_eq; rw [hTR6.st]; unfold sched.TS_INACTIVE; rfl + rw [if_neg (by rw [hrw]; unfold sched.TS_INACTIVE sched.TS_RUNNING sched.TS_RESTART sched.TS_RUNNING_VM; decide), + WP.spec_ok] + obtain ⟨V', Lst', J6⟩ := hq.1 rfl + exact fin false x4 ((), frSt σ2 i x4 false) s6 a2 hz1 (by rw [specReplyTail_skip (hk6 x4 false) (Or.inr rfl)]; rfl) + (J6.anyX arI) + -- `Restart`: runnable + have hrw : L.mem s6 (tStA i) = sched.TS_RESTART := by + apply UScalar.eq_of_val_eq; rw [hTR6.st]; unfold sched.TS_RESTART; rfl + rw [if_pos (by rw [hrw]; simp)] + obtain ⟨V', hX6', hJR, hpost⟩ := hq.2 rfl + have hne6 : y.scRefills ≠ [] := J2.1.1.inv.ne _ y n hky hya + have hsc6' := hsc6 x4 true hx4 + refine WP.spec_bind (Fl4Proofs.P3.Sched.ready_refines s6 (frSt σ2 i x4 true) hS6 hi2C hky6 hya) ?_ + rintro b4 hb4 + rw [Fl4Proofs.P3.Sched.getScRefillReady_eq hky6 hne6] at hb4 + have hb4' : b4 = Fl4Spec.refillReady (frSt σ2 i x4 true).curTime (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b4, frSt σ2 i x4 true) ∈ q.1) hb4; simpa using this.symm + by_cases hrl : Fl4Spec.refillReady σ2.curTime (Fl4Spec.refillHd y) = true ∧ + Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true + · -- released: `possible_switch_to` + have hR : relC := fun sp y' n' h1 h2 => by + rw [e3] at h1; cases h1; rw [hky] at h2; cases h2; exact hrl + obtain ⟨Lst', J6⟩ := hJR hR + rw [if_pos (by rw [hb4', htm6]; exact hrl.1)] + refine WP.spec_bind (Fl4Proofs.P3.Sched.sufficient_refines s6 _ (frSt σ2 i x4 true) hS6 hi2C hky6 hya 0#u64 (hmb _)) ?_ + rintro b5 hb5 + rw [Fl4Proofs.P3.Sched.getScRefillSufficient_eq hky6 hne6] at hb5 + have hb5' : b5 = Fl4Spec.refillSufficient (U64.bv 0#u64) (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b5, frSt σ2 i x4 true) ∈ q.1) hb5; simpa using this.symm + rw [if_pos (by rw [hb5', e0]; exact hrl.2)] + have hnr6 : i.bv ∉ (frSt σ2 i x4 true).releaseQueue := by rw [hrq6]; exact hnr2 + have hsch : Fl4Spec.schedulable i.bv (frSt σ2 i x4 true) = true := + schedulable_of (hk6 x4 true) (hsc6'.trans e3) rfl hky6 hya hnr6 + have hti6 : i.bv ≠ (frSt σ2 i x4 true).idleThread := by + intro e + obtain ⟨yi, h1, -, h3, -⟩ := J6.1.1.act.2 + rw [← e, hk6 x4 true] at h1; cases h1; cases h3 + have htc6 : i.bv ≠ (frSt σ2 i x4 true).curThread := by + rw [hct6]; intro e; exact hne (P1.eq_of_bv e).symm + refine WP.spec_bind (J6.switchV hiT hti6 htc6 hsch) ?_ + rintro s7 ⟨σ7, hps, Lst7, J7⟩ + apply (WP.spec_ok _).2 + refine fin true x4 ((), σ7) s7 a2 hz1 ?_ (J7.anyX arI) + rw [specReplyTail_run (hk6 x4 true) (hsc6'.trans e3) rfl hky6, if_pos ⟨by rw [htm6]; exact hrl.1, hrl.2⟩, hps] + exact Set.mem_singleton _ + · -- not released: `postpone` (no valid timeout handler, or a timeout fault) + have hnR : ¬ relC := fun h => hrl (h _ _ _ e3 hky) + have hnot : ¬ (Fl4Spec.isEpCap xi.tcbTimeoutHandler = true ∧ Fl4Spec.isTimeoutFault f = false) := + fun ⟨h1, h2⟩ => hnt _ hhf ⟨i3.bv, y, n, e3, hky, hrl, h1, h2⟩ + have hpost' := hpost hnR + have tmo : fl4ReplyTmo st arI i ft i3 s6 a2 ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + unfold fl4ReplyTmo + refine WP.spec_bind (valid_timeout_handler_rel s6 (frSt σ2 i x4 true) hX6.rel.2.1.1 + (hX6.rel.tcbT (hk6 x4 true))) ?_ + rintro b5 ⟨-, x', hkx', hb5⟩ + rw [hk6 x4 true] at hkx'; cases hkx' + have hb5x : b5 = Fl4Spec.isEpCap xi.tcbTimeoutHandler := + hb5.trans (congrArg Fl4Spec.isEpCap (hth6 x4 true hx4)) + have post : (do let s4 ← sched.postpone st s6 i3; ok (s4, a2)) ⦃ res => + ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + refine WP.spec_bind hpost' ?_ + rintro s7 ⟨σ7, hpp, Lst7, J7⟩ + apply (WP.spec_ok _).2 + refine fin true x4 ((), σ7) s7 a2 hz1 ?_ (J7.anyX arI) + rw [specReplyTail_run (hk6 x4 true) (hsc6'.trans e3) rfl hky6, if_neg (by rw [htm6]; exact hrl)] + unfold specReplyTmo + rw [nd_bind_one (getTcb_eq (hk6 x4 true)), if_neg (fun h => hnot ⟨by rw [← hth6 x4 true hx4]; exact h.1, + by have h2 := h.2; rw [hfi] at h2; simpa using h2⟩), hpp] + exact Set.mem_singleton _ + by_cases hb : b5 = true + · rw [if_pos hb] + have htf : Fl4Spec.isTimeoutFault f = true := by + by_contra h; exact hnot ⟨by rw [← hb5x]; exact hb, by simpa using h⟩ + rw [if_neg (by simp [hftT.2 htf])] + exact post + · rw [if_neg hb] + exact post + unfold fl4ReplyTmo at tmo + by_cases hr4 : b4 = true + · rw [if_pos hr4] + refine WP.spec_bind (Fl4Proofs.P3.Sched.sufficient_refines s6 _ (frSt σ2 i x4 true) hS6 hi2C hky6 hya 0#u64 (hmb _)) ?_ + rintro b5 hb5 + rw [Fl4Proofs.P3.Sched.getScRefillSufficient_eq hky6 hne6] at hb5 + have hb5' : b5 = Fl4Spec.refillSufficient (U64.bv 0#u64) (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b5, frSt σ2 i x4 true) ∈ q.1) hb5; simpa using this.symm + have hr5 : ¬ b5 = true := fun h => hrl ⟨by rw [← htm6 x4 true, ← hb4']; exact hr4, by rw [← e0, ← hb5']; exact h⟩ + rw [if_neg hr5] + exact tmo + · rw [if_neg hr4] + exact tmo + +end Fault + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ReplyNormal.lean b/proofs/Fl4Proofs/P3/VmEntryS3ReplyNormal.lean new file mode 100644 index 000000000..cd1415603 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ReplyNormal.lean @@ -0,0 +1,729 @@ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmDisp +import Fl4Proofs.P3.XTopJointIpcJ +import Fl4Proofs.P3.XTopJointSendRecv + +/-! +Normal reply continuations for the actual linked receiver after the reply +removal and sporadic check. The remaining [J] input is precisely the paired +no-endpoint IPC transfer from the selected replier, including its receiver +register update and the heap alternatives for a grant or sender timeout. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D VX X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- [J] Matched no-endpoint IPC at the reached, unfaulted receiver. The +replier may be faulted, and granted caps may update destination CNodes. +The heap frame excludes the precise receiver TCB and the replier's SC. -/ +def ReplyMessageTransferV (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) + (Z : XSets) (H : P1.Heap) (Cs D VX X : Set U64) (XL : U64 → Nat) + (s : S) (a : A) (σ : Fl4Spec.State Unit) + (ct i : U64) (g : Bool) (xs xi : Fl4Spec.Tcb) : Prop := + ipc.do_ipc_transfer st arI s a ct 0#u64 0#u64 g i ⦃ s' => ∃ σ', + Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none (U64.bv 0#u64) g i.bv σ = ({((), σ')}, False) ∧ + (∀ p, p ≠ i.bv → (∀ sc, xs.tcbSchedContext = some sc → p ≠ sc) → σ'.kheap p = σ.kheap p ∨ + ∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs')) ∧ + (∃ x', σ'.kheap i.bv = some (.tcb x') ∧ x' = { xi with tcbArch := x'.tcbArch }) ∧ + ∃ (G : MdbGhost) (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64), + VmEptState (L := L) arI VX X XL AR (Z.mv G V) H Cs Ac' Lst' D s' a σ' ⦄ + +set_option maxHeartbeats 16000000 in +theorem replyCopyMrsV_loop0 (J : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s a σ) {snd rcv : U64} (hs : snd ∈ Z.T) + (hr : rcv ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs xr : Fl4Spec.Tcb} (hks : σ.kheap snd.bv = some (.tcb xs)) + (hkr : σ.kheap rcv.bv = some (.tcb xr)) (n : U64) : + msg.copy_mrs_loop0 st s snd rcv n 0#u64 ⦃ r => r.2.val = min n.val 4 ∧ + ∃ V, VmEptState (L := L) arI VX X XL AR (Z.mv Z.G V) H Cs Ac Lst D r.1 a + (Notif.updK σ rcv.bv (.tcb (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take n.val)))) ⦄ := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + 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 ∧ + ∃ V, VmEptState (L := L) arI VX X XL AR (Z.mv Z.G V) H Cs Ac Lst D x.1 a + (Notif.updK σ rcv.bv (.tcb (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take x.2.val))))) + · rintro ⟨s', i⟩ ⟨hi4, hin, V', J'⟩ + unfold msg.copy_mrs_loop0.body + simp only at hi4 hin J' ⊢ + have hks' : (Notif.updK σ rcv.bv (.tcb (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take i.val)))).kheap snd.bv = some (.tcb xs) := by + simp [Notif.updK, hne, hks] + have hkr' : (Notif.updK σ rcv.bv (.tcb (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take i.val)))).kheap rcv.bv = some (.tcb (Endp.copyRegsTcb xr + xs.tcbArch.tcbContext.userRegs (Fl4Spec.msgRegisters.take i.val))) := by + simp [Notif.updK] + by_cases h1 : i < n + · simp only [h1, ↓reduceIte] + by_cases h2 : i < msg.N_MSG_REGISTERS + · have hi : i.val < 4 := by have := (P1.gt_iff_val).1 h2; omega + simp only [h2, ↓reduceIte] + refine WP.spec_bind (Endp.msg_register_spec i hi) ?_ + rintro r rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' snd _) ?_ + rintro v rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s' rcv _ _) ?_ + rintro s2 rfl + obtain ⟨i2, h2e, h2v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h2e, bind_ok, WP.spec_ok] + have hi2 : i2.val = i.val + 1 := by simpa using h2v + have B := J'.1.1 + have hX := reg_wr_irel B.ip (B.xtop.irelK B.ks B.buf) hr hkr' (Endp.mrIdx i.val) + (mrIdx_ne_badge _) (L.mem s' (Endp.regA snd (regIdx (Endp.mrIdx i.val)))) + obtain ⟨V2, B2, F2⟩ := B.regWrJ hr hkr' (Endp.mrIdx i.val) _ hX + have J2 := J'.ofNat (V' := V2) B2 F2 + have hv := ((B.xtop.irelK B.ks B.buf).1.toE.at_tcb hs hks').regs (Endp.mrIdx i.val) + refine ⟨⟨show i2.val ≤ 4 by omega, show i2.val ≤ n.val by have := (P1.gt_iff_val).1 h1; omega, V2, ?_⟩, by show 4 - i2.val < 4 - i.val; omega⟩ + simp only + rw [hi2, copyRegs_take_succ _ _ _ hi] + have e : Notif.updK (Notif.updK σ rcv.bv (.tcb (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take i.val)))) rcv.bv (.tcb (Notif.setReg (Endp.copyRegsTcb xr + xs.tcbArch.tcbContext.userRegs (Fl4Spec.msgRegisters.take i.val)) (Endp.mrIdx i.val) + (L.mem s' (Endp.regA snd (regIdx (Endp.mrIdx i.val)))).bv)) = + Notif.updK σ rcv.bv (.tcb (Notif.setReg (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take i.val)) (Endp.mrIdx i.val) + (xs.tcbArch.tcbContext.userRegs (Endp.mrIdx i.val)))) := by + rw [hv]; simp only [Notif.updK, Function.update_idem] + rw [← e]; exact J2 + · simp only [h2, ↓reduceIte, WP.spec_ok] + have hi : i.val = 4 := by + have h2' : ¬ i.val < 4 := fun h => h2 ((P1.gt_iff_val).2 (by rw [hN]; exact h)) + omega + have hn4 : 4 ≤ n.val := by have := (P1.gt_iff_val).1 h1; omega + refine ⟨by omega, V', ?_⟩ + have e : Fl4Spec.msgRegisters.take n.val = Fl4Spec.msgRegisters.take i.val := by + rw [Endp.take_four n.val hn4, Endp.take_four i.val (by omega)] + rw [e]; exact J' + · simp only [h1, ↓reduceIte, WP.spec_ok] + have hni : i.val = n.val := by + have : ¬ i.val < n.val := fun h => h1 ((P1.gt_iff_val).2 h) + omega + refine ⟨by omega, V', ?_⟩ + rw [← hni]; exact J' + · refine ⟨by simp, by simp, Z.vs, ?_⟩ + show VmEptState (L := L) arI VX X XL AR (Z.mv Z.G Z.vs) H Cs Ac Lst D s a (Notif.updK σ rcv.bv (.tcb xr)) + rw [Endp.updK_self hkr]; simpa using J + +set_option maxHeartbeats 16000000 in +theorem replyCopyMrsV_norecv (J : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s a σ) {snd rcv : U64} (hs : snd ∈ Z.T) + (hr : rcv ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs xr : Fl4Spec.Tcb} (hks : σ.kheap snd.bv = some (.tcb xs)) + (hkr : σ.kheap rcv.bv = some (.tcb xr)) (sb n : U64) : + msg.copy_mrs st s snd sb rcv 0#u64 n ⦃ r => r.1.val = min n.val 4 ∧ + ∃ V, VmEptState (L := L) arI VX X XL AR (Z.mv Z.G V) H Cs Ac Lst D r.2 a + (Notif.updK σ rcv.bv (.tcb (Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs + (Fl4Spec.msgRegisters.take n.val)))) ⦄ := by + unfold msg.copy_mrs + refine WP.spec_bind (replyCopyMrsV_loop0 J hs hr hne hks hkr n) ?_ + rintro ⟨s1, i⟩ h + simp only [↓reduceIte] + show (ok (i, s1)) ⦃ _ ⦄ + simp only [WP.spec_ok] + exact h + +set_option maxHeartbeats 16000000 in +theorem VmEptState.replyRegsV (Jv : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s a σ) (hOK : Endp.IpcBufCapOK σ) + (hBC : Endp.BufCover Z.T Z.B σ) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) (hf : xs.tcbFault = none) + (hrb : Endp.ipcBufOf true xr = none) (epo : Option Fl4Spec.ObjRef) (ep badge : U64) (grant : Bool) : + ipc.do_ipc_transfer st arI s a snd ep badge grant rcv ⦃ s' => + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv epo badge.bv grant rcv.bv σ = + ({((), Notif.updK σ rcv.bv (.tcb (Endp.xferTcb xr xs badge.bv)))}, False) ∧ + ∃ V, VmEptState (L := L) arI VX X XL AR (Z.mv Z.G V) H Cs Ac Lst D s' a + (Notif.updK σ rcv.bv (.tcb (Endp.xferTcb xr xs badge.bv))) ⦄ := by + have J := Jv.1.1 + have hne' : snd.bv ≠ rcv.bv := fun e => hne (P1.eq_of_bv e) + have hI := J.xtop.irelK J.ks J.buf + have hxs := hI.1.toE.at_tcb hs hks + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have thT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (hJ.tcb t).2 (J.zH.1 t ht) + have hwmi : (L.mem s (Endp.regA snd layout.reg.MSG_INFO)).bv = + xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register := by + rw [← regIdx_msgInfo]; exact hxs.regs _ + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 (thT rcv hr) hkr hOK true) ?_ + rintro rb ⟨hrb0, -⟩ + have hrb' : rb = 0#u64 := hrb0.2 hrb + subst hrb' + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_ + rintro ft rfl + have hft0 : L.mem s (tFaultA snd) = fl4_abi.consts.fault.NullFault := by + rw [hxs.fault0.2 hf]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hft0, ↓reduceIte] + refine WP.spec_bind (lookup_buf_full s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 (thT snd hs) hks hOK false) ?_ + rintro sb ⟨hsb, hsbp⟩ + unfold ipc.do_normal_transfer + refine WP.spec_bind (Endp.msg_info_spec (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, hext, hle⟩ + obtain ⟨xs', hks', hws⟩ := J.buf snd hs + rw [hks] at hks'; cases hks' + have hsm : ∀ sp, Endp.ipcBufOf false xs = some sp → Endp.BufMap Z.B sb sp := + fun sp h => hBC snd hs xs false sp hks h sb (hsbp hws sp h) + have hlkF := Ipc.lookup_extra_top J.ip J.xtop.rel hI.1 (thT snd hs) sb (Endp.ipcBufOf false xs) + ⟨hsb.1, hsb.2⟩ hsm tag (Fl4Spec.dataToMessageInfo (xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register)) + (by show tag.extra_caps.bv.toNat = _; rw [hext, hwmi]) + (by show tag.extra_caps.bv.toNat ≤ 3; rw [hext, hwmi]; exact Endp.extra_le3 _) + have hspec := Endp.doIpcTransfer_regsG hne' hks hkr hf hrb epo badge.bv grant (fun _ => by + obtain ⟨r, caps, hc, -⟩ := WP.spec_exists hlkF; exact ⟨caps, hc⟩) + have hex : (if grant = true then (do + let r ← ipc.lookup_extra_caps st s snd sb 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) ⦃ _ => True ⦄ := by + cases grant with + | false => simp + | true => + simp only [↓reduceIte] + refine WP.spec_bind hlkF ?_ + rintro r - + split <;> simp + refine WP.spec_bind hex ?_ + rintro extra - + refine WP.spec_bind (replyCopyMrsV_norecv Jv hs hr hne' hks hkr sb tag.length) ?_ + rintro ⟨i, s1⟩ ⟨hi, V1, J1⟩ + refine WP.spec_bind (Endp.transfer_caps_norecv (L := L) s1 tag extra ep rcv) ?_ + 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 := i } + obj.Tcb.set_reg st rcv s3 layout.reg.BADGE badge) ⦃ _ ⦄ + have hi4 : i.val ≤ 4 := by rw [hi]; omega + refine WP.spec_bind (Endp.set_msg_info_spec (L := L) s1 rcv tag.label i (by omega)) ?_ + rintro s3 ⟨v, rfl, hv⟩ + refine WP.spec_mono (Endp.obj.Tcb.set_reg.spec _ rcv _ badge) ?_ + rintro s4 rfl + -- the spec's message info + set w := xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register with hwdef + have hLv : Fl4Spec.unat (Fl4Spec.dataToMessageInfo w).miLength = tag.length.val := by + show _ = tag.length.bv.toNat + rw [hlen, hwmi] + have hvx : v.bv = Fl4Spec.messageInfoToData (Endp.xferInfo w) := by + rw [hv] + unfold Endp.xferInfo + congr 1 + congr 1 + · exact Endp.xfer_len (by rw [hi]; show _ = min (Fl4Spec.unat _) 4; rw [hLv]) + · rw [hlab, hwmi]; rfl + set x1 := Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs (Fl4Spec.msg_registers.take + (Fl4Spec.unat (Fl4Spec.dataToMessageInfo w).miLength)) with hx1 + rw [← hLv] at J1 + change VmEptState (L := L) arI VX X XL AR (Z.mv Z.G V1) H Cs Ac Lst D s1 a (Notif.updK σ rcv.bv (.tcb x1)) at J1 + have hkr1 : (Notif.updK σ rcv.bv (.tcb x1)).kheap rcv.bv = some (.tcb x1) := by simp [Notif.updK] + -- the message info + have B1 := J1.1.1 + have hX2 := reg_wr_irel B1.ip (B1.xtop.irelK B1.ks B1.buf) hr hkr1 Fl4Spec.msg_info_register (by decide) v + obtain ⟨V2, B2, F2⟩ := B1.regWrJ hr hkr1 Fl4Spec.msg_info_register v hX2 + rw [regIdx_msgInfo] at B2 F2 + have J2 := J1.ofNat (V' := V2) B2 F2 + have hkr2 : (Notif.updK (Notif.updK σ rcv.bv (.tcb x1)) rcv.bv + (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv))).kheap rcv.bv = + some (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv)) := by simp [Notif.updK] + -- the badge + have hX3 := badge_wr_irel B2.ip (B2.xtop.irelK B2.ks B2.buf) hr hkr2 badge + rw [badgeRegA_eq_regA] at hX3 + obtain ⟨V3, B3, F3⟩ := B2.regWrJ hr hkr2 Fl4Spec.badge_register badge hX3 + rw [regIdx_badge] at B3 F3 + have J3 := J2.ofNat (V' := V3) B3 F3 + have hfin : Notif.updK (Notif.updK (Notif.updK σ rcv.bv (.tcb x1)) rcv.bv + (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv))) rcv.bv + (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register v.bv) Fl4Spec.badge_register badge.bv)) = + Notif.updK σ rcv.bv (.tcb (Endp.xferTcb xr xs badge.bv)) := by + simp only [Notif.updK, Function.update_idem] + rw [hvx]; rfl + refine ⟨hspec, V3, ?_⟩ + rw [← hfin]; exact J3 + +/-- Real register-only reply transfer. Extra caps may be looked up. +With no receive buffer the paired operation drops them. -/ +theorem VmEptState.replyMessageNoBufV + (J : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s a σ) + {ct i : U64} (hct : ct ∈ Z.T) (hi : i ∈ Z.T) (hne : ct ≠ i) + {xs xi : Fl4Spec.Tcb} (hks : σ.kheap ct.bv = some (.tcb xs)) + (hki : σ.kheap i.bv = some (.tcb xi)) (hf : xs.tcbFault = none) + (hrb : Endp.ipcBufOf true xi = none) (g : Bool) : + ReplyMessageTransferV (L := L) arI AR Z H Cs D VX X XL s a σ ct i g xs xi := by + obtain ⟨hOK, hBC, -⟩ := J.1.1.xferH + unfold ReplyMessageTransferV + refine WP.spec_mono (J.replyRegsV hOK hBC hct hi hne hks hki hf hrb none 0#u64 0#u64 g) ?_ + rintro s' ⟨he, V, J'⟩ + refine ⟨_, he, fun p hp _ => Or.inl (by simp [Notif.updK, hp]), ?_, Z.G, V, Ac, Lst, J'⟩ + refine ⟨Endp.xferTcb xi xs (U64.bv 0#u64), by simp only [Notif.updK_kheap, Function.update_self], ?_⟩ + simp only [Endp.xferTcb] + rw [Endp.copyRegsTcb_other] + rfl + +private theorem VmEptState.replyRunV + (J : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) {x : Fl4Spec.Tcb} + (hk : σ.kheap t.bv = some (.tcb x)) (hi : x.tcbState = .inactive) + (hrel : RelIfBound σ t.bv) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s' => ∃ Lst', + VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst' D s' a + (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) ⦄ := by + have B := J.1.1 + have htH := B.zH.1 t ht + have htR := (B.xtop.irelK B.ks B.buf).1.toE.at_tcb ht hk + have hr0 : L.mem s (P1.stA t) = sched.TS_INACTIVE := by + apply UScalar.eq_of_val_eq + rw [P1.stA_eq, ← tStA_eq, htR.st, hi] + unfold sched.TS_INACTIVE + rfl + refine WP.spec_mono (B.stsIJ ht htD hk hi .running (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) + (fun _ => hrel) (P1.sched.set_thread_state.running_blkI (Es := P1.EH H) s t B.blk htH hr0)) ?_ + rintro s' ⟨Lst', B', hw⟩ + have hk' := natK_tcbW (t := t.bv) + (σ' := stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) + (y := { x with tcbState := .running }) hk (stsAct_kheap _ _) (fun e => by cases e) + refine ⟨Lst', ?_⟩ + simpa using J.ofNat (V' := Z.vs) (by simpa using B') (NatFoot.ofT B.xtop hw (fun _ h => B.tfoot_nh h) hk') + +private theorem VmEptState.replyPostV + (J : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hi : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hs : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (ha : Fl4Spec.scActive y = true) + (hnr : ¬ (Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true ∧ + Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true)) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) ∧ + sched.postpone st s6 sc ⦃ s7 => ∃ σ7, Fl4Spec.postpone (Z := Unit) sc.bv + (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) = ({((), σ7)}, False) ∧ + ∃ Lst', VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst' D s7 a σ7 ⦄ ⦄ := by + have B := J.1.1 + refine WP.spec_mono (B.runPostJV ht htD hk hi htc hs hxsc hky ha hnr) ?_ + rintro s6 ⟨hX, -, hpost⟩ + refine ⟨hX, WP.spec_mono hpost ?_⟩ + rintro s7 ⟨σ7, hpp, hw, hk7, ho, Lst', B'⟩ + have hh : σ7.kheap = (updK σ t.bv (.tcb { x with tcbState := .running })).kheap := by + funext p + by_cases e : p = t.bv + · subst p; simpa only [updK_kheap, Function.update_self] using hk7 + · simpa only [updK_kheap, Function.update_of_ne e] using ho p e + have hk' := natK_tcbW hk hh (fun e => by cases e) + refine ⟨σ7, hpp, Lst', ?_⟩ + simpa using J.ofNat (V' := Z.vs) (by simpa using B') (NatFoot.ofT B.xtop hw (fun _ h => B.tfoot_nh h) hk') + +set_option maxHeartbeats 12000000 in +/-- Match the native NoSc reply continuation after paired IPC. -/ +theorem replyRestNoScV + + {s2 : S} {ar : A} {σ1 σ0 σ2 : Fl4Spec.State Unit} + [VxE Z] (Jv : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s2 ar σ2) + {ct i p : U64} (g : Bool) (hiD : i ∉ D) + {xr : Fl4Spec.Reply} (hkr : σ1.kheap p.bv = some (.reply xr)) (hrep : xr.replyTcb = some i.bv) + {x : Fl4Spec.Tcb} (hkx : σ1.kheap i.bv = some (.tcb x)) (hst : x.tcbState = .blockedOnReply p.bv) + (hrm : Fl4Spec.replyRemove (Z := Unit) i.bv p.bv σ1 = ({((), σ0)}, False)) + (hub : (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext i.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ0 = ({((), σ2)}, False)) + (hin : ∃ x', σ2.kheap i.bv = some (.tcb x') ∧ x'.tcbState = .inactive) + (hC : ReplyRestNoSc σ2 ct i g) + + (hTransfer : ∀ xs xi, σ2.kheap ct.bv = some (.tcb xs) → σ2.kheap i.bv = some (.tcb xi) → + xi.tcbFault = none → + ReplyMessageTransferV (L := L) arI AR Z H Cs D VX X XL s2 ar σ2 ct i g xs xi) : + fl4ReplyRest2 st arI s2 ar ct i g ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + have J2 := Jv.1.1 + obtain ⟨xs, xi, hks, hki, hnsc, hfi⟩ := hC + obtain ⟨xi', hki', hin⟩ := hin + rw [hki] at hki'; cases hki' + have hI := J2.xtop.irelK J2.ks J2.buf + have hiT : i ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.obj.tcb _ xi hki + rw [Sched.bv_inj h] at ht; exact ht + obtain ⟨Q, R0, hc, hsp⟩ := J2.xtop.fin.1.1.1.lists + obtain ⟨x0, hk0, -, -, hscw, -⟩ := hsp.tcb i hiT + rw [hki] at hk0; cases hk0 + obtain ⟨x0', hk0', hTR⟩ := hI.1.tcb i hiT + rw [hki] at hk0'; cases hk0' + unfold fl4ReplyRest2 + step with Fl4Proofs.P3.Endp.obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ft = 0#u64 := by rw [hft]; exact hTR.fault0.2 hfi + rw [if_pos (by rw [hft0]; unfold fl4_abi.consts.fault.NullFault; rfl)] + have e0 : U64.bv 0#u64 = (0 : Fl4Spec.Word) := rfl + refine WP.spec_bind (Pₘ := fun res => res.2 = ar ∧ ∃ (σ3 : Fl4Spec.State Unit) (x3 : Fl4Spec.Tcb) (G : MdbGhost) (V : XVs) + (Ac' : Set U64) (Lst' : U64 → List U64), + Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2 = ({((), σ3)}, False) ∧ + σ3.kheap i.bv = some (.tcb x3) ∧ x3.tcbSchedContext = none ∧ + VmEptState (L := L) arI VX X XL AR (Z.mv G V) H Cs Ac' Lst' D res.1 ar + (stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv)) ?_ ?_ + · refine WP.spec_bind (hTransfer xs xi hks hki hfi) ?_ + rintro s4 ⟨σ3, htr, -, ⟨x3, hk3, hx3⟩, G, V, Ac', Lst3, J3⟩ + rw [e0] at htr + have hs3 : x3.tcbState = .inactive := (congrArg Fl4Spec.Tcb.tcbState hx3).trans hin + have hsc3 : x3.tcbSchedContext = none := (congrArg Fl4Spec.Tcb.tcbSchedContext hx3).trans hnsc + refine WP.spec_bind (J3.replyRunV hiT hiD hk3 hs3 (relIfBound_noSc hk3 hsc3)) ?_ + rintro s5 ⟨Lst', J5⟩ + exact (WP.spec_ok _).2 ⟨rfl, σ3, x3, G, V, Ac', Lst', htr, hk3, hsc3, J5⟩ + rintro ⟨s5, a2⟩ ⟨ha, σ3, x3, G, V, Ac', Lst', htr, hk3, hsc3, J5⟩ + simp only at ha + subst ha + have B5 := J5.1.1 + set σ5 := stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv with hσ5 + have hk5 : σ5.kheap i.bv = some (.tcb { x3 with tcbState := .running }) := by + rw [hσ5, stsAct_kheap]; simp [updK] + obtain ⟨Q5, R5, hc5, hsp5⟩ := B5.xtop.fin.1.1.1.lists + obtain ⟨x5, hk5', -, -, hscw5, -⟩ := hsp5.tcb i hiT + rw [hk5] at hk5'; cases hk5' + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + have h3 : i3 = 0#u64 := by + by_contra h + have e := hscw5 + rw [← hi3, Notif.optRef_ne h] at e + rw [hsc3] at e + cases e + step with Fl4Proofs.P3.Sched.obj.Sc.is_null.spec' as ⟨b2, hb2⟩ + rw [hb2, if_pos (by simp [h3]), WP.spec_ok] + have hz1 : ((), σ5) ∈ ((Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv >>= fun _ => + Fl4Spec.setThreadState i.bv .running) σ2).1 := by + rw [nd_bind_one htr, Notif.setThreadState_eq _ hk3]; rfl + have hz : ((), σ5) ∈ (specReplyTail i.bv xi.tcbFault σ5).1 := by + rw [specReplyTail_skip hk5 (Or.inl hsc3)]; rfl + exact ⟨((), σ5), mem_doReplyTransfer hkr hrep hkx hst g hrm hub hki (Or.inl ⟨hfi, hz1⟩) hz, + J5.anyX arI⟩ + +set_option maxHeartbeats 12000000 in +/-- Match the native Sw reply continuation after paired IPC. -/ +theorem replyRestSwV (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + + {s2 : S} {ar : A} {σ1 σ0 σ2 : Fl4Spec.State Unit} + [VxE Z] (Jv : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s2 ar σ2) + {ct i p : U64} (g : Bool) (hne : ct ≠ i) (hiD : i ∉ D) (hct2 : σ2.curThread = ct.bv) + {xr : Fl4Spec.Reply} (hkr : σ1.kheap p.bv = some (.reply xr)) (hrep : xr.replyTcb = some i.bv) + {x : Fl4Spec.Tcb} (hkx : σ1.kheap i.bv = some (.tcb x)) (hst : x.tcbState = .blockedOnReply p.bv) + (hrm : Fl4Spec.replyRemove (Z := Unit) i.bv p.bv σ1 = ({((), σ0)}, False)) + (hub : (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext i.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ0 = ({((), σ2)}, False)) + (hin : ∃ x', σ2.kheap i.bv = some (.tcb x') ∧ x'.tcbState = .inactive) + (hC : ReplyRestSw σ2 ct i g) + + (hTransfer : ∀ xs xi, σ2.kheap ct.bv = some (.tcb xs) → σ2.kheap i.bv = some (.tcb xi) → + xi.tcbFault = none → + ReplyMessageTransferV (L := L) arI AR Z H Cs D VX X XL s2 ar σ2 ct i g xs xi) : + fl4ReplyRest2 st arI s2 ar ct i g ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + have J2 := Jv.1.1 + obtain ⟨xs, xi, sp, y, n, hks, hki, hxsc, hky, hya, hrdy, hsuf, hfi⟩ := hC + obtain ⟨xi', hki', hin⟩ := hin + rw [hki] at hki'; cases hki' + have hI := J2.xtop.irelK J2.ks J2.buf + have hiT : i ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.obj.tcb _ xi hki + rw [Sched.bv_inj h] at ht; exact ht + have hnr2 : i.bv ∉ σ2.releaseQueue := fun hm => by + obtain ⟨x', -, -, -, h1, -, h3, -⟩ := J2.inv.rel _ hm + rw [hki] at h1; cases h1; rw [hin] at h3; cases h3 + obtain ⟨Q, R0, hc, hsp⟩ := J2.xtop.fin.1.1.1.lists + obtain ⟨x0, hk0, -, -, hscw, -⟩ := hsp.tcb i hiT + rw [hki] at hk0; cases hk0 + obtain ⟨x0', hk0', hTR⟩ := hI.1.tcb i hiT + rw [hki] at hk0'; cases hk0' + unfold fl4ReplyRest2 + obtain ⟨i2, hi2⟩ : ∃ i2 : U64, i2 = L.mem s2 (P1.scA i) := ⟨_, rfl⟩ + rw [← hi2] at hscw + have h0 : i2 ≠ 0#u64 := by + intro e; rw [e, optRef_zero, hxsc] at hscw; cases hscw + have esp : sp = i2.bv := by rw [Notif.optRef_ne h0, hxsc] at hscw; exact Option.some.inj hscw + subst esp + have hi2C : i2 ∈ Z.C := by rw [hi2]; exact hsp.scMem i hiT (by rw [← hi2]; exact h0) + obtain ⟨y', n', hky', hy⟩ := hsp.scs i2 hi2C + rw [hky] at hky'; cases hky' + step with Fl4Proofs.P3.Endp.obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ft = 0#u64 := by rw [hft]; exact hTR.fault0.2 hfi + rw [if_pos (by rw [hft0]; unfold fl4_abi.consts.fault.NullFault; rfl)] + have e0 : U64.bv 0#u64 = (0 : Fl4Spec.Word) := rfl + refine WP.spec_bind (Pₘ := fun res => res.2 = ar ∧ ∃ (σ3 : Fl4Spec.State Unit) (x3 : Fl4Spec.Tcb) (G : MdbGhost) (V : XVs) + (Ac' : Set U64) (Lst' : U64 → List U64), + Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2 = ({((), σ3)}, False) ∧ + σ3.kheap i.bv = some (.tcb x3) ∧ x3.tcbSchedContext = some i2.bv ∧ x3.tcbFault = none ∧ + σ3.kheap i2.bv = σ2.kheap i2.bv ∧ + VmEptState (L := L) arI VX X XL AR (Z.mv G V) H Cs Ac' Lst' D res.1 ar + (stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv)) ?_ ?_ + · refine WP.spec_bind (hTransfer xs xi hks hki hfi) ?_ + rintro s4 ⟨σ3, htr, hkp3, ⟨x3, hk3, hx3⟩, G, V, Ac', Lst3, J3⟩ + rw [e0] at htr + have mem3 : ((), σ3) ∈ (Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2).1 := by + rw [htr]; exact Set.mem_singleton _ + have hs3 : x3.tcbState = .inactive := (congrArg Fl4Spec.Tcb.tcbState hx3).trans hin + have hsc3 : x3.tcbSchedContext = some i2.bv := (congrArg Fl4Spec.Tcb.tcbSchedContext hx3).trans hxsc + have hf3 : x3.tcbFault = none := (congrArg Fl4Spec.Tcb.tcbFault hx3).trans hfi + have hti : i2.bv ≠ i.bv := fun e => by rw [e, hki] at hky; cases hky + have hsd : ∀ sc, xs.tcbSchedContext = some sc → i2.bv ≠ sc := by + rintro sc hsc rfl + obtain ⟨y1, n1, hk1, hb1⟩ := J2.inv.back _ _ _ hks hsc + obtain ⟨y2, n2, hk2, hb2⟩ := J2.inv.back _ _ _ hki hxsc + rw [hk1] at hk2; cases hk2; rw [hb1] at hb2 + exact hne (P1.eq_of_bv (Option.some.inj hb2)) + have hkp3 : σ3.kheap i2.bv = σ2.kheap i2.bv := (hkp3 _ hti hsd).elim id + (fun h => by obtain ⟨r, cs, cs', h1, _⟩ := h; rw [hky] at h1; cases h1) + have hky3 : σ3.kheap i2.bv = some (.schedContext y n) := by rw [hkp3]; exact hky + have htm3 : σ3.curTime = σ2.curTime := (SVK.sv_doIpcTransfer' _ _ _ _ _ σ2 _ mem3).2.1 + have hrel : RelIfBound σ3 i.bv := by + intro x' scp y' n' h1 h2 h3 _ _ + rw [hk3] at h1; cases h1; rw [hsc3] at h2; cases h2; rw [hky3] at h3; cases h3 + exact ⟨by rw [htm3]; exact hrdy, hsuf⟩ + refine WP.spec_bind (J3.replyRunV hiT hiD hk3 hs3 hrel) ?_ + rintro s5 ⟨Lst', J5⟩ + exact (WP.spec_ok _).2 ⟨rfl, σ3, x3, G, V, Ac', Lst', htr, hk3, hsc3, hf3, hkp3, J5⟩ + rintro ⟨s5, a2⟩ ⟨ha, σ3, x3, G, V, Ac', Lst', htr, hk3, hsc3, hf3, hkp3, J5⟩ + simp only at ha + subst ha + have B5 := J5.1.1 + have mem3 : ((), σ3) ∈ (Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2).1 := by + rw [htr]; exact Set.mem_singleton _ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σ4 := updK σ3 i.bv (.tcb x5) with hσ4 + set σ5 := stsAct σ4 i.bv with hσ5 + have hrec : σ5 = { σ4 with schedulerAction := σ5.schedulerAction } := stsAct_rec σ4 i.bv + have hk5 : σ5.kheap i.bv = some (.tcb x5) := by rw [hσ5, stsAct_kheap]; simp [hσ4, updK] + have hti : i2.bv ≠ i.bv := fun e => by rw [e, hki] at hky; cases hky + have hky5 : σ5.kheap i2.bv = some (.schedContext y n) := by + rw [hσ5, stsAct_kheap, hσ4]; simp only [updK, Function.update_of_ne hti]; rw [hkp3]; exact hky + have htm5 : σ5.curTime = σ2.curTime := by + rw [hrec]; exact (SVK.sv_doIpcTransfer' _ _ _ _ _ σ2 _ mem3).2.1 + have hct5 : σ5.curThread = ct.bv := by + rw [hrec]; show σ3.curThread = _; rw [(dk_doIpcTransfer' _ _ _ _ _).ct mem3, hct2] + have hnr5 : i.bv ∉ σ5.releaseQueue := fun hm => by + rw [hrec] at hm + exact hnr2 (RelQ.rq_doIpcTransfer' _ _ _ _ _ σ2 _ mem3 _ hm) + have hti5 : i.bv ≠ σ5.idleThread := by + intro e + obtain ⟨yi, h1, -, h3, -⟩ := B5.act.2 + rw [← e, hk5] at h1; cases h1; cases h3 + have htc5 : i.bv ≠ σ5.curThread := by + rw [hct5]; intro e; exact hne (P1.eq_of_bv e).symm + obtain ⟨Q5, R5, hc5, hsp5⟩ := B5.xtop.fin.1.1.1.lists + obtain ⟨x6, hk6, -, -, hscw5, -⟩ := hsp5.tcb i hiT + rw [hk5] at hk6; cases hk6 + obtain ⟨x6', hk6', hTR5⟩ := (B5.xtop.irelK B5.ks B5.buf).1.tcb i hiT + rw [hk5] at hk6'; cases hk6' + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + rw [← hi3] at hscw5 + have h3 : i3 ≠ 0#u64 := by + intro e; rw [e, optRef_zero] at hscw5; rw [hsc3] at hscw5; cases hscw5 + have e3 : i3 = i2 := by + rw [Notif.optRef_ne h3] at hscw5; rw [hsc3] at hscw5 + exact P1.eq_of_bv (Option.some.inj hscw5).symm + subst e3 + step with Fl4Proofs.P3.Sched.obj.Sc.is_null.spec' as ⟨b2, hb2⟩ + rw [hb2, if_neg (by simp [h3])] + refine WP.spec_bind (Fl4Proofs.P3.Sched.is_runnable_spec s5 i) ?_ + rintro b3 rfl + have hrw : L.mem s5 (tStA i) = sched.TS_RUNNING := by + apply UScalar.eq_of_val_eq; rw [hTR5.st]; unfold sched.TS_RUNNING; rfl + rw [if_pos (by simp [hrw])] + have hS5 := B5.xtop.fin.1.1.1 + have hne5 : y.scRefills ≠ [] := B5.inv.ne _ y n hky5 hya + refine WP.spec_bind (Fl4Proofs.P3.Sched.ready_refines s5 σ5 hS5 hi2C hky5 hya) ?_ + rintro b4 hb4 + rw [Fl4Proofs.P3.Sched.getScRefillReady_eq hky5 hne5] at hb4 + have hb4' : b4 = Fl4Spec.refillReady σ5.curTime (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b4, σ5) ∈ q.1) hb4; simpa using this.symm + rw [if_pos (by rw [hb4', htm5]; exact hrdy)] + refine WP.spec_bind (Fl4Proofs.P3.Sched.sufficient_refines s5 _ σ5 hS5 hi2C hky5 hya 0#u64 (hmb _)) ?_ + rintro b5 hb5 + rw [Fl4Proofs.P3.Sched.getScRefillSufficient_eq hky5 hne5] at hb5 + have hb5' : b5 = Fl4Spec.refillSufficient (U64.bv 0#u64) (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b5, σ5) ∈ q.1) hb5; simpa using this.symm + rw [if_pos (by rw [hb5', e0]; exact hsuf)] + have hactive : ∀ p y' n', x5.tcbSchedContext = some p → + σ5.kheap p = some (.schedContext y' n') → Fl4Spec.scActive y' = true := by + intro p y' n' hp hk + change x3.tcbSchedContext = some p at hp + rw [hsc3] at hp + cases hp + rw [hky5] at hk + cases hk + exact hya + refine WP.spec_bind (B5.sendSwitchJ hiT hk5 (by rfl) hnr5 hactive hti5 htc5) ?_ + rintro s6 ⟨σ6, hps, Lst6, B6, F6⟩ + have J6 := J5.ofNat (V' := V) B6 F6 + apply (WP.spec_ok _).2 + have hz1 : ((), σ5) ∈ ((Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv >>= fun _ => + Fl4Spec.setThreadState i.bv .running) σ2).1 := by + rw [nd_bind_one htr, Notif.setThreadState_eq _ hk3]; rfl + have hz : ((), σ6) ∈ (specReplyTail i.bv xi.tcbFault σ5).1 := by + rw [specReplyTail_run hk5 hsc3 rfl hky5, if_pos ⟨by rw [htm5]; exact hrdy, hsuf⟩, hps] + exact Set.mem_singleton _ + exact ⟨((), σ6), mem_doReplyTransfer hkr hrep hkx hst g hrm hub hki (Or.inl ⟨hfi, hz1⟩) hz, + J6.anyX arI⟩ + +set_option maxHeartbeats 12000000 in +/-- Match the native Post reply continuation after paired IPC. -/ +theorem replyRestPostV (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + + {s2 : S} {ar : A} {σ1 σ0 σ2 : Fl4Spec.State Unit} + [VxE Z] (Jv : VmEptState (L := L) arI VX X XL AR Z H Cs Ac Lst D s2 ar σ2) + {ct i p : U64} (g : Bool) (hne : ct ≠ i) (hiD : i ∉ D) (hct2 : σ2.curThread = ct.bv) + {xr : Fl4Spec.Reply} (hkr : σ1.kheap p.bv = some (.reply xr)) (hrep : xr.replyTcb = some i.bv) + {x : Fl4Spec.Tcb} (hkx : σ1.kheap i.bv = some (.tcb x)) (hst : x.tcbState = .blockedOnReply p.bv) + (hrm : Fl4Spec.replyRemove (Z := Unit) i.bv p.bv σ1 = ({((), σ0)}, False)) + (hub : (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext i.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ0 = ({((), σ2)}, False)) + (hin : ∃ x', σ2.kheap i.bv = some (.tcb x') ∧ x'.tcbState = .inactive) + (hC : ReplyRestPost σ2 ct i g) + + (hTransfer : ∀ xs xi, σ2.kheap ct.bv = some (.tcb xs) → σ2.kheap i.bv = some (.tcb xi) → + xi.tcbFault = none → + ReplyMessageTransferV (L := L) arI AR Z H Cs D VX X XL s2 ar σ2 ct i g xs xi) : + fl4ReplyRest2 st arI s2 ar ct i g ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + have J2 := Jv.1.1 + obtain ⟨xs, xi, sp, y, n, hks, hki, hxsc, hky, hya, hnrel, hvt, hfi⟩ := hC + obtain ⟨xi', hki', hin⟩ := hin + rw [hki] at hki'; cases hki' + have hI := J2.xtop.irelK J2.ks J2.buf + have hiT : i ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.obj.tcb _ xi hki + rw [Sched.bv_inj h] at ht; exact ht + obtain ⟨Q, R0, hc, hsp⟩ := J2.xtop.fin.1.1.1.lists + obtain ⟨x0, hk0, -, -, hscw, -⟩ := hsp.tcb i hiT + rw [hki] at hk0; cases hk0 + obtain ⟨x0', hk0', hTR⟩ := hI.1.tcb i hiT + rw [hki] at hk0'; cases hk0' + unfold fl4ReplyRest2 + obtain ⟨i2, hi2⟩ : ∃ i2 : U64, i2 = L.mem s2 (P1.scA i) := ⟨_, rfl⟩ + rw [← hi2] at hscw + have h0 : i2 ≠ 0#u64 := by + intro e; rw [e, optRef_zero, hxsc] at hscw; cases hscw + have esp : sp = i2.bv := by rw [Notif.optRef_ne h0, hxsc] at hscw; exact Option.some.inj hscw + subst esp + have hi2C : i2 ∈ Z.C := by rw [hi2]; exact hsp.scMem i hiT (by rw [← hi2]; exact h0) + obtain ⟨y', n', hky', hy⟩ := hsp.scs i2 hi2C + rw [hky] at hky'; cases hky' + step with Fl4Proofs.P3.Endp.obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + have hft0 : ft = 0#u64 := by rw [hft]; exact hTR.fault0.2 hfi + rw [if_pos (by rw [hft0]; unfold fl4_abi.consts.fault.NullFault; rfl)] + have e0 : U64.bv 0#u64 = (0 : Fl4Spec.Word) := rfl + have hti : i2.bv ≠ i.bv := fun e => by rw [e, hki] at hky; cases hky + refine WP.spec_bind (Pₘ := fun res => res.2 = ar ∧ ∃ (σ3 : Fl4Spec.State Unit) (x3 : Fl4Spec.Tcb) (G : MdbGhost) (V : XVs) (Ac' : Set U64), + Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2 = ({((), σ3)}, False) ∧ + σ3.kheap i.bv = some (.tcb x3) ∧ x3.tcbSchedContext = some i2.bv ∧ x3.tcbFault = none ∧ + x3.tcbTimeoutHandler = xi.tcbTimeoutHandler ∧ + σ3.kheap i2.bv = σ2.kheap i2.bv ∧ + XTopAt (L := L) AR (Z.mv G V) res.1 ar + (stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv) ∧ + sched.postpone st res.1 i2 ⦃ s7 => ∃ σ7, Fl4Spec.postpone (Z := Unit) i2.bv + (stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv) = ({((), σ7)}, False) ∧ + ∃ Lst' : U64 → List U64, VmEptState (L := L) arI VX X XL AR (Z.mv G V) H Cs Ac' Lst' D s7 ar σ7 ⦄) ?_ ?_ + · refine WP.spec_bind (hTransfer xs xi hks hki hfi) ?_ + rintro s4 ⟨σ3, htr, hkp3, ⟨x3, hk3, hx3⟩, G, V, Ac', Lst3, J3⟩ + rw [e0] at htr + have mem3 : ((), σ3) ∈ (Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2).1 := by + rw [htr]; exact Set.mem_singleton _ + have hs3 : x3.tcbState = .inactive := (congrArg Fl4Spec.Tcb.tcbState hx3).trans hin + have hsc3 : x3.tcbSchedContext = some i2.bv := (congrArg Fl4Spec.Tcb.tcbSchedContext hx3).trans hxsc + have hf3 : x3.tcbFault = none := (congrArg Fl4Spec.Tcb.tcbFault hx3).trans hfi + have hth3 : x3.tcbTimeoutHandler = xi.tcbTimeoutHandler := (congrArg Fl4Spec.Tcb.tcbTimeoutHandler hx3).trans rfl + have hsd : ∀ sc, xs.tcbSchedContext = some sc → i2.bv ≠ sc := by + rintro sc hsc rfl + obtain ⟨y1, n1, hk1, hb1⟩ := J2.inv.back _ _ _ hks hsc + obtain ⟨y2, n2, hk2, hb2⟩ := J2.inv.back _ _ _ hki hxsc + rw [hk1] at hk2; cases hk2; rw [hb1] at hb2 + exact hne (P1.eq_of_bv (Option.some.inj hb2)) + have hkp3 : σ3.kheap i2.bv = σ2.kheap i2.bv := (hkp3 _ hti hsd).elim id + (fun h => by obtain ⟨r, cs, cs', h1, _⟩ := h; rw [hky] at h1; cases h1) + have hky3 : σ3.kheap i2.bv = some (.schedContext y n) := by rw [hkp3]; exact hky + have htm3 : σ3.curTime = σ2.curTime := (SVK.sv_doIpcTransfer' _ _ _ _ _ σ2 _ mem3).2.1 + have hct3 : i.bv ≠ σ3.curThread := by + rw [(dk_doIpcTransfer' _ _ _ _ _).ct mem3, hct2]; intro e; exact hne (P1.eq_of_bv e).symm + refine WP.spec_bind (J3.replyPostV hiT hiD hk3 hs3 hct3 hi2C hsc3 hky3 hya (by rw [htm3]; exact hnrel)) ?_ + rintro s5 ⟨hX5, hpost⟩ + exact (WP.spec_ok _).2 ⟨rfl, σ3, x3, G, V, Ac', htr, hk3, hsc3, hf3, hth3, hkp3, hX5, hpost⟩ + rintro ⟨s5, a2⟩ ⟨ha, σ3, x3, G, V, Ac', htr, hk3, hsc3, hf3, hth3, hkp3, hX5, hpost⟩ + simp only at ha + subst ha + have mem3 : ((), σ3) ∈ (Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2).1 := by + rw [htr]; exact Set.mem_singleton _ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σ4 := updK σ3 i.bv (.tcb x5) with hσ4 + set σ5 := stsAct σ4 i.bv with hσ5 + have hrec : σ5 = { σ4 with schedulerAction := σ5.schedulerAction } := stsAct_rec σ4 i.bv + have hk5 : σ5.kheap i.bv = some (.tcb x5) := by rw [hσ5, stsAct_kheap]; simp [hσ4, updK] + have hky5 : σ5.kheap i2.bv = some (.schedContext y n) := by + rw [hσ5, stsAct_kheap, hσ4]; simp only [updK, Function.update_of_ne hti]; rw [hkp3]; exact hky + have htm5 : σ5.curTime = σ2.curTime := by + rw [hrec]; exact (SVK.sv_doIpcTransfer' _ _ _ _ _ σ2 _ mem3).2.1 + have hS5 := hX5.fin.1.1.1 + obtain ⟨Q5, R5, hc5, hsp5⟩ := hS5.lists + obtain ⟨x6, hk6, -, -, hscw5, -⟩ := hsp5.tcb i hiT + rw [hk5] at hk6; cases hk6 + obtain ⟨x6', hk6', hTR5⟩ := hX5.ipc.tcb i hiT + rw [hk5] at hk6'; cases hk6' + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + rw [← hi3] at hscw5 + have h3 : i3 ≠ 0#u64 := by + intro e; rw [e, optRef_zero] at hscw5; rw [hsc3] at hscw5; cases hscw5 + have e3 : i3 = i2 := by + rw [Notif.optRef_ne h3] at hscw5; rw [hsc3] at hscw5 + exact P1.eq_of_bv (Option.some.inj hscw5).symm + subst e3 + step with Fl4Proofs.P3.Sched.obj.Sc.is_null.spec' as ⟨b2, hb2⟩ + rw [hb2, if_neg (by simp [h3])] + refine WP.spec_bind (Fl4Proofs.P3.Sched.is_runnable_spec s5 i) ?_ + rintro b3 rfl + have hrw : L.mem s5 (tStA i) = sched.TS_RUNNING := by + apply UScalar.eq_of_val_eq; rw [hTR5.st]; unfold sched.TS_RUNNING; rfl + rw [if_pos (by simp [hrw])] + have hne5 : y.scRefills ≠ [] := J2.inv.ne _ y n hky hya + -- the spec's outcome + have hz1 : ((), σ5) ∈ ((Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv >>= fun _ => + Fl4Spec.setThreadState i.bv .running) σ2).1 := by + rw [nd_bind_one htr, Notif.setThreadState_eq _ hk3]; rfl + have hvt5 : Fl4Spec.isEpCap x5.tcbTimeoutHandler = false := by + show Fl4Spec.isEpCap x3.tcbTimeoutHandler = false; rw [hth3]; exact hvt + -- the timeout branch: no valid handler, so `postpone` + have tmo : fl4ReplyTmo st arI i ft i3 s5 a2 ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + unfold fl4ReplyTmo + refine WP.spec_bind (valid_timeout_handler_rel s5 σ5 hX5.rel.2.1.1 (hX5.rel.tcbT hk5)) ?_ + rintro b5 ⟨-, x', hkx', hb5⟩ + rw [hk5] at hkx'; cases hkx' + rw [if_neg (by rw [hb5, hvt5]; simp)] + refine WP.spec_bind hpost ?_ + rintro s7 ⟨σ7, hpp, Lst7, J7⟩ + apply (WP.spec_ok _).2 + have hz : ((), σ7) ∈ (specReplyTail i.bv xi.tcbFault σ5).1 := by + rw [specReplyTail_run hk5 hsc3 rfl hky5, if_neg (by rw [htm5]; exact hnrel)] + unfold specReplyTmo + rw [nd_bind_one (getTcb_eq hk5), if_neg (fun h => by rw [hvt5] at h; exact Bool.false_ne_true h.1), hpp] + exact Set.mem_singleton _ + exact ⟨((), σ7), mem_doReplyTransfer hkr hrep hkx hst g hrm hub hki (Or.inl ⟨hfi, hz1⟩) hz, + J7.anyX arI⟩ + unfold fl4ReplyTmo at tmo + refine WP.spec_bind (Fl4Proofs.P3.Sched.ready_refines s5 σ5 hS5 hi2C hky5 hya) ?_ + rintro b4 hb4 + rw [Fl4Proofs.P3.Sched.getScRefillReady_eq hky5 hne5] at hb4 + have hb4' : b4 = Fl4Spec.refillReady σ5.curTime (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b4, σ5) ∈ q.1) hb4; simpa using this.symm + by_cases hr4 : b4 = true + · rw [if_pos hr4] + refine WP.spec_bind (Fl4Proofs.P3.Sched.sufficient_refines s5 _ σ5 hS5 hi2C hky5 hya 0#u64 (hmb _)) ?_ + rintro b5 hb5 + rw [Fl4Proofs.P3.Sched.getScRefillSufficient_eq hky5 hne5] at hb5 + have hb5' : b5 = Fl4Spec.refillSufficient (U64.bv 0#u64) (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b5, σ5) ∈ q.1) hb5; simpa using this.symm + have hr5 : ¬ b5 = true := fun h => hnrel ⟨by rw [← htm5, ← hb4']; exact hr4, by rw [← e0, ← hb5']; exact h⟩ + rw [if_neg hr5] + exact tmo + · rw [if_neg hr4] + exact tmo + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ReplyRecv.lean b/proofs/Fl4Proofs/P3/VmEntryS3ReplyRecv.lean new file mode 100644 index 000000000..87f5b4c84 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ReplyRecv.lean @@ -0,0 +1,72 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm): **`RunTmoPV` for a timeout handler endpoint with receivers waiting**. + +`VmEptState.runTmoRecvV` is P3/JAtZBTmoRecv's `JAtZB.runTmoRecvFoot` (the timeout fault sent to a waiting receiver, with the native +footprint) then `VmEptState.ofNat`; `runTmoP_recvV` is the native `runTmoP_recv` at `VmEptState`. With `runTmoP_idleV` +(P3/VmEntryS3ReplyTmo) it discharges `hRT` of `VmEptState.replyXferV` and `VmEptState.selectedReplyS3`. + +Premises: `hAT`, `hmax`, `htu` [P]. New file. +-/ +import Fl4Proofs.P3.JAtZBTmoRecv +import Fl4Proofs.P3.VmEntryS3ReplyTmo +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {VX XX : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 16000000 in +/-- **`Running` or `Restart` of an `Inactive` thread, then `handle_timeout` sending its timeout fault to a receiver waiting on its +timeout handler's endpoint, at `VmEptState`** (`JAtZB.runTmoRecvFoot`, then `VmEptState.ofNat`). -/ +theorem VmEptState.runTmoRecvV (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hcr : ∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ Fl4Spec.runnable xc.tcbState = true) + (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) [VxE Z] {t : U64} (ht : t ∈ Z.T) (htD : t ∉ D) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) + {p bd : Fl4Spec.Word} {r : Fl4Spec.CapRights} (hth : x.tcbTimeoutHandler = .endpointCap p bd r) + (hcb : ∀ xe, σ.kheap p = some (.endpoint xe) → ¬ Endp.CanBlock false xe) + (ts : Fl4Spec.ThreadState) (hrun : Fl4Spec.runnable ts = true) (hts : tsCode ts < 3) (v : U64) + (hv : v.val = tsCode ts) + (hB : sched.set_thread_state st s t v ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t v ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + ∀ b : U64, faults.handle_timeout st arI s6 a t (error.Fault.Timeout b) ⦃ res => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv)).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ ⦄ := by + refine WP.spec_mono (JAtZB.runTmoRecvFoot hAT hmax htu hcr J.1.1 ht htD hk hrs htc hsc hxsc hky hya hth hcb ts hrun hts + v hv hB) ?_ + rintro s6 ⟨h1, h2, h3⟩ + refine ⟨h1, h2, fun b => WP.spec_mono (h3 b) ?_⟩ + rintro res ⟨z, hz, V, Lst', B', F⟩ + exact ⟨z, hz, (J.ofNat B' F).anyX arI⟩ + +variable (arI) in +/-- **`RunTmoPV` for a timeout handler endpoint with receivers waiting** (the native `runTmoP_recv`). -/ +theorem runTmoP_recvV (Z0 : XSets) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : + RunTmoPV (L := L) arI VX XX XL AR Z0 (fun xe => ¬ Endp.CanBlock false xe) := + fun _ _ _ _ _ _ _ a _ hth J _ hcr _ ht htD _ hk hrs htc _ hsc hxsc _ _ hky hya _ _ _ hthd hcb ts hrun hts v hv hB => + J.runTmoRecvV hAT hmax (htu a) hcr ht htD hk hrs htc hsc hxsc hky hya hthd hcb ts hrun hts v hv hB + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ReplyTmo.lean b/proofs/Fl4Proofs/P3/VmEntryS3ReplyTmo.lean new file mode 100644 index 000000000..81fcd2d72 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ReplyTmo.lean @@ -0,0 +1,634 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm): **the Timeout branch of the rest of `do_reply_transfer`, and `do_reply_transfer`, at +`VmEptState`**. + +* `RunTmoPV`: P3/XTopJointIpcJ's `RunTmoP` at `VmEptState` (ending at `VEAnyX`); `VmEptState.runTmoV` / `runTmoP_idleV`: its + instance for an endpoint that takes a blocking send (`CanBlock false`), by P3/JAtZBRunTmo's `JAtZB.runTmoFoot` and + `VmEptState.ofNat`. +* `VmEptState.tmoEpObj`, `replyRestTmoV`: the native `tmoEpObj` / `replyRestTmoJ` at `VmEptState` (`faultBranchV`, `xferAllV`, + `stsIV`). +* `VmEptState.replyXferV`: P3/XTopJointIpcJ's `replyXferJ` at `VmEptState`: `reply_remove` (`replyRemoveV`), the unblock check + (`replyUnblockV`), then the five rest cases (`replyRestNoScV`, `replyRestSwV`, `replyRestPostV`, `replyRestFaultV`, + `replyRestTmoV`); the paired transfer is `VmEptState.replyMessageTransferV`. + +Generated by `handoff/tools/gen_tmov.py` from the native proofs (`JAtZ` read as `VmEptState`). New file; no existing statement +changed. Premises: `hRT` ([J], receivers waiting on the timeout handler's endpoint: `RunTmoPV` for `¬ CanBlock false`), `hAT`, +`hmax`, `htu`, `hmb` ([P]). +-/ +import Fl4Proofs.P3.VmEntryS3ReplyFault +import Fl4Proofs.P3.JAtZBRunTmo +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section Tmo +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {VX XX : Set U64} {XL : U64 → Nat} + +variable (arI) in +/-- **`Running` or `Restart` of an `Inactive` thread other than the current +one, then its `handle_timeout`, at `J`, for a timeout handler endpoint in the +class `EC`** (the statement of XTopJointSendBlockG's `JAtZ.runTmoJ`, ending +at `JAt`): `runTmoP_idle` (idle or sending, `JAtZ.runTmoJ`) here, +XTopJointFaultTmo's `runTmoP_recv` (receivers waiting) downstream, for the +`J` states whose typed heap is `Z0`'s. `hcr`: the current thread runnable. -/ +def RunTmoPV (VX XX : Set U64) (XL : U64 → Nat) (AR : A → Fl4Spec.Cr3 → Prop) (Z0 : XSets) (EC : Fl4Spec.Endpoint → Prop) : Prop := + ∀ (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) + (σ : Fl4Spec.State Unit), Z.th = Z0.th → VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ → [VxE Z] → + (∃ xc, σ.kheap σ.curThread = some (.tcb xc) ∧ Fl4Spec.runnable xc.tcbState = true) → + ∀ (t : U64), t ∈ Z.T → t ∉ D → ∀ (x : Fl4Spec.Tcb), σ.kheap t.bv = some (.tcb x) → x.tcbState = .inactive → + t.bv ≠ σ.curThread → ∀ (sc : U64), sc ∈ Z.C → x.tcbSchedContext = some sc.bv → + ∀ (y : Fl4Spec.SchedContext) (n : Nat), σ.kheap sc.bv = some (.schedContext y n) → Fl4Spec.scActive y = true → + ∀ (p bd : Fl4Spec.Word) (r : Fl4Spec.CapRights), x.tcbTimeoutHandler = .endpointCap p bd r → + (∀ xe, σ.kheap p = some (.endpoint xe) → EC xe) → + ∀ (ts : Fl4Spec.ThreadState), Fl4Spec.runnable ts = true → tsCode ts < 3 → ∀ (v : U64), v.val = tsCode ts → + sched.set_thread_state st s t v ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄ → + sched.set_thread_state st s t v ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + ∀ b : U64, faults.handle_timeout st arI s6 a t (error.Fault.Timeout b) ⦃ res => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv)).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ ⦄ + +/-- The timeout handler cap of a TCB names an endpoint object (the cap +valid in `J`, as in XTopJointSendBlockG's `JAtZ.runTmoJ`). -/ +theorem VmEptState.tmoEpObj (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) [VxE Z] {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) {p bd : Fl4Spec.Word} {r : Fl4Spec.CapRights} + (hth : x.tcbTimeoutHandler = .endpointCap p bd r) : ∃ xe, σ.kheap p = some (.endpoint xe) := by + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨⟨G0, hB0⟩, -⟩ := hI.2.1 + have hth0 : Z.th t = some .tcb := hB0.tcbT hk + obtain ⟨xh, cv, hkh, hcv, hca⟩ := hB0.2.1.1.timeoutCap hth0 + rw [hk] at hkh; cases hkh + have habs := hca.2 + rw [hth] at habs + obtain ⟨ptr, bdg, sd, rv, g, gr, rfl⟩ : ∃ ptr bdg sd rv g gr, cv = .Endpoint ptr bdg sd rv g gr := by + cases cv <;> simp [absCap] at habs; exact ⟨_, _, _, _, _, _, rfl⟩ + simp only [absCap, Fl4Spec.Cap.endpointCap.injEq] at habs + obtain ⟨hp, -, -⟩ := habs + subst hp + have hAS : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex 4) := ⟨t, rfl, Or.inr ⟨hth0, 4, by decide, rfl⟩⟩ + have hcv' : capAt (L.mem s) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 4)) = ok (.Endpoint ptr bdg sd rv g gr) := by + rw [tcbSlot4_eq]; exact hcv + have hpE : ptr ∈ Z.E := J.1.1.slotE hAS hcv' + obtain ⟨xe, hke, -⟩ := hI.1.ep ptr hpE + exact ⟨xe, hke⟩ + +set_option maxHeartbeats 16000000 in +/-- **`Running` or `Restart` of an `Inactive` thread, then `handle_timeout`, at `VmEptState`** (`JAtZB.runTmoFoot`, then +`VmEptState.ofNat`). -/ +theorem VmEptState.runTmoV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (htD : t ∉ D) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + (htc : t.bv ≠ σ.curThread) {sc : U64} (hsc : sc ∈ Z.C) (hxsc : x.tcbSchedContext = some sc.bv) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) + {p bd : Fl4Spec.Word} {r : Fl4Spec.CapRights} (hth : x.tcbTimeoutHandler = .endpointCap p bd r) + (hcb : ∀ xe, σ.kheap p = some (.endpoint xe) → Endp.CanBlock false xe) + (ts : Fl4Spec.ThreadState) (hrun : Fl4Spec.runnable ts = true) (hts : tsCode ts < 3) (v : U64) + (hv : v.val = tsCode ts) + (hB : sched.set_thread_state st s t v ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t v ⦃ s6 => + XTopAt (L := L) AR Z s6 a (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s6 + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + ∀ b : U64, faults.handle_timeout st arI s6 a t (error.Fault.Timeout b) ⦃ res => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) t.bv (.timeout b.bv) + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv)).1, + ∃ (V : XVs) (D' : Set U64), VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D' res.1 res.2 z.2 ⦄ ⦄ := by + refine WP.spec_mono (J.1.1.runTmoFoot ht htD hk hrs htc hsc hxsc hky hya hth hcb ts hrun hts v hv hB) ?_ + rintro s6 ⟨h1, h2, h3⟩ + refine ⟨h1, h2, fun b => WP.spec_mono (h3 b) ?_⟩ + rintro res ⟨z, hz, V, D', B', F⟩ + exact ⟨z, hz, V, D', J.ofNat B' F⟩ + +variable (arI) in +/-- `RunTmoPV` for an idle or sending handler endpoint (`VmEptState.runTmoV`). -/ +theorem runTmoP_idleV (Z0 : XSets) : RunTmoPV (L := L) arI VX XX XL AR Z0 (Endp.CanBlock false) := + fun _ _ _ _ _ _ _ _ _ _ J _ _ _ ht htD _ hk hrs htc _ hsc hxsc _ _ hky hya _ _ _ hth hcb ts hrun hts v hv hB => + WP.spec_mono (J.runTmoV ht htD hk hrs htc hsc hxsc hky hya hth hcb ts hrun hts v hv hB) fun _ ⟨h1, h2, h3⟩ => + ⟨h1, h2, fun b => WP.spec_mono (h3 b) fun _ ⟨z, hz, V, D', J'⟩ => ⟨z, hz, J'.anyX arI⟩⟩ + +set_option maxHeartbeats 32000000 in +/-- **The rest of `do_reply_transfer` at `J`, for `ReplyRestTmo` with an +idle or sending timeout handler endpoint (`TmoIdle`)**: the transfer +(`JAtZ.xferAllJ`) or the fault reply (`faultBranchJ`), then `Running` / +`Restart` and the tail's `handle_timeout` as one step +(XTopJointSendBlockG's `JAtZ.runTmoJ`: `set_fault` of the timeout fault and +the blocking `send_ipc` on the handler endpoint; `Inv` by the weakened +`Inv.rd`, a sender with a timeout fault exempt). The tail's reads (its SC, +runnable, the ready and sufficient tests not both true, the valid timeout +handler, the fault not a timeout, the SC's badge) at the state between, on +`XTopAt` there. The spec's outcome by `mem_doReplyTransfer`. -/ +theorem replyRestTmoV (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {s2 : S} {ar : A} {σ1 σ0 σ2 : Fl4Spec.State Unit} (J2 : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s2 ar σ2) [VxE Z] + {ct i p : U64} (g : Bool) (hne : ct ≠ i) (hiD : i ∉ D) (hct2 : σ2.curThread = ct.bv) + {xr : Fl4Spec.Reply} (hkr : σ1.kheap p.bv = some (.reply xr)) (hrep : xr.replyTcb = some i.bv) + {x : Fl4Spec.Tcb} (hkx : σ1.kheap i.bv = some (.tcb x)) (hst : x.tcbState = .blockedOnReply p.bv) + (hrm : Fl4Spec.replyRemove (Z := Unit) i.bv p.bv σ1 = ({((), σ0)}, False)) + (hub : (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext i.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ0 = ({((), σ2)}, False)) + (hin : ∃ x', σ2.kheap i.bv = some (.tcb x') ∧ x'.tcbState = .inactive) + {xs : Fl4Spec.Tcb} (hks : σ2.kheap ct.bv = some (.tcb xs)) + (hxsr : Fl4Spec.runnable xs.tcbState = true) {EC : Fl4Spec.Endpoint → Prop} + (hRT : RunTmoPV (L := L) arI VX XX XL AR Z EC) (hC : ReplyRestTmo σ2 ct i) (hid : TmoEp EC σ2 i) : + fl4ReplyRest2 st arI s2 ar ct i g ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + obtain ⟨xi, sp, y, n, hki, hxsc, hky, hya, hnrel, hvt, hcase⟩ := hC + obtain ⟨xi', hki', hin⟩ := hin + rw [hki] at hki'; cases hki' + obtain ⟨xi', ep, bd, r, xe2, hki', hth, hke2, hcb2⟩ := hid + rw [hki] at hki'; cases hki' + have hep : ep ≠ i.bv := fun e => by rw [e, hki] at hke2; cases hke2 + have hI := J2.xtop.irelK J2.ks J2.buf + have hiT : i ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.1.1.obj.tcb _ xi hki + rw [Sched.bv_inj h] at ht; exact ht + have hcT : ct ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J2.1.1.obj.tcb _ xs hks + rw [Sched.bv_inj h] at ht; exact ht + have hxr := hI.1.toE.at_tcb hiT hki + obtain ⟨Q, R0, hc, hsp⟩ := J2.xtop.fin.1.1.1.lists + obtain ⟨x0, hk0, -, -, hscw, -⟩ := hsp.tcb i hiT + rw [hki] at hk0; cases hk0 + obtain ⟨i2, hi2⟩ : ∃ i2 : U64, i2 = L.mem s2 (P1.scA i) := ⟨_, rfl⟩ + rw [← hi2] at hscw + have h0 : i2 ≠ 0#u64 := by + intro e; rw [e, optRef_zero, hxsc] at hscw; cases hscw + have esp : sp = i2.bv := by rw [Notif.optRef_ne h0, hxsc] at hscw; exact Option.some.inj hscw + subst esp + have hi2C : i2 ∈ Z.C := by rw [hi2]; exact hsp.scMem i hiT (by rw [← hi2]; exact h0) + have hti : i2.bv ≠ i.bv := fun e => by rw [e, hki] at hky; cases hky + have hne2 : y.scRefills ≠ [] := J2.1.1.inv.ne _ y n hky hya + have e0 : U64.bv 0#u64 = (0 : Fl4Spec.Word) := rfl + unfold fl4ReplyRest2 + step with Fl4Proofs.P3.Endp.obj.Tcb.fault_type.spec as ⟨ft, hft⟩ + rcases hcase with hfi | ⟨xs', f, σ', hks', hfi, htf, hhf⟩ + · ------------------------------------------------------------------ no fault + have hft0 : ft = 0#u64 := by rw [hft]; exact hxr.fault0.2 hfi + rw [if_pos (by rw [hft0]; unfold fl4_abi.consts.fault.NullFault; rfl)] + have hto : ¬ ft = fl4_abi.consts.fault.Timeout := by + rw [hft0, show fl4_abi.consts.fault.Timeout = 5#u64 by unfold fl4_abi.consts.fault.Timeout; rfl]; decide + refine WP.spec_bind (Pₘ := fun res => res.2 = ar ∧ ∃ (σ3 : Fl4Spec.State Unit) (x3 : Fl4Spec.Tcb) + (G : MdbGhost) (V : XVs), + Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2 = ({((), σ3)}, False) ∧ + σ3.kheap i.bv = some (.tcb x3) ∧ x3.tcbSchedContext = some i2.bv ∧ x3.tcbFault = none ∧ + x3.tcbTimeoutHandler = xi.tcbTimeoutHandler ∧ + σ3.kheap i2.bv = σ2.kheap i2.bv ∧ + XTopAt (L := L) AR (Z.mv G V) res.1 ar + (stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv) ∧ + ∀ b : U64, faults.handle_timeout st arI res.1 ar i (error.Fault.Timeout b) ⦃ res' => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) i.bv (.timeout b.bv) + (stsAct (updK σ3 i.bv (.tcb { x3 with tcbState := .running })) i.bv)).1, + VEAnyX (L := L) arI AR res'.1 res'.2 z.2 ⦄) ?_ ?_ + · refine WP.spec_bind (J2.xferAllV hAT hmax (htu ar) hcT hiT hne hks hki g none 0#u64 0#u64 + (Or.inr ⟨rfl, rfl⟩)) ?_ + rintro s4 ⟨σ3, htr, hkp3, ⟨x3, hk3, hx3⟩, G, V, J3⟩ + rw [e0] at htr + have mem3 : ((), σ3) ∈ (Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2).1 := by + rw [htr]; exact Set.mem_singleton _ + have hs3 : x3.tcbState = .inactive := (congrArg Fl4Spec.Tcb.tcbState hx3).trans hin + have hsc3 : x3.tcbSchedContext = some i2.bv := (congrArg Fl4Spec.Tcb.tcbSchedContext hx3).trans hxsc + have hf3 : x3.tcbFault = none := (congrArg Fl4Spec.Tcb.tcbFault hx3).trans hfi + have hth3 : x3.tcbTimeoutHandler = xi.tcbTimeoutHandler := + (congrArg Fl4Spec.Tcb.tcbTimeoutHandler hx3).trans rfl + have hsd : ∀ sc, xs.tcbSchedContext = some sc → i2.bv ≠ sc := by + rintro sc hsc rfl + obtain ⟨y1, n1, hk1, hb1⟩ := J2.1.1.inv.back _ _ _ hks hsc + obtain ⟨y2, n2, hk2, hb2⟩ := J2.1.1.inv.back _ _ _ hki hxsc + rw [hk1] at hk2; cases hk2; rw [hb1] at hb2 + exact hne (P1.eq_of_bv (Option.some.inj hb2)) + have hes : ∀ sc, xs.tcbSchedContext = some sc → ep ≠ sc := by + rintro sc hsc rfl + obtain ⟨y1, n1, hk1, -⟩ := J2.1.1.inv.back _ _ _ hks hsc + rw [hk1] at hke2; cases hke2 + have hcb3 : ∀ xe, σ3.kheap ep = some (.endpoint xe) → EC xe := by + intro xe he + rcases hkp3 ep hep hes with h | ⟨r', cs, cs', h1, -⟩ + · rw [h, hke2] at he; cases he; exact hcb2 + · rw [hke2] at h1; cases h1 + have hkpi : σ3.kheap i2.bv = σ2.kheap i2.bv := (hkp3 _ hti hsd).elim id + (fun h => by obtain ⟨r', cs, cs', h1, _⟩ := h; rw [hky] at h1; cases h1) + have hky3 : σ3.kheap i2.bv = some (.schedContext y n) := by rw [hkpi]; exact hky + have hct3 : i.bv ≠ σ3.curThread := by + rw [(dk_doIpcTransfer' _ _ _ _ _).ct mem3, hct2]; intro e; exact hne (P1.eq_of_bv e).symm + obtain ⟨x5, hk5', hTR5⟩ := (J3.xtop.irelK J3.ks J3.buf).1.tcb i hiT + rw [hk3] at hk5'; cases hk5' + have hr0 : L.mem s4 (P1.stA i) = sched.TS_INACTIVE := by + apply UScalar.eq_of_val_eq + rw [P1.stA_eq, ← tStA_eq, hTR5.st, hs3]; unfold sched.TS_INACTIVE; rfl + have htH : i ∈ P1.Heap.tcbs H := J3.zH.1 i hiT + have hcs3 : σ3.kheap ct.bv = some (.tcb xs) := by + rcases hkp3 ct.bv (fun e => hne (P1.eq_of_bv e)) (fun sc hsc e => by + obtain ⟨y1, n1, hk1, -⟩ := J2.1.1.inv.back _ _ _ hks hsc + rw [← e, hks] at hk1; cases hk1) with h | ⟨r', cs, cs', h1, -⟩ + · rw [h]; exact hks + · rw [hks] at h1; cases h1 + have hcr3 : ∃ xc, σ3.kheap σ3.curThread = some (.tcb xc) ∧ Fl4Spec.runnable xc.tcbState = true := + ⟨xs, by rw [(dk_doIpcTransfer' _ _ _ _ _).ct mem3, hct2]; exact hcs3, hxsr⟩ + refine WP.spec_bind (hRT _ _ _ _ _ _ _ _ _ (by rfl) J3 hcr3 i hiT hiD x3 hk3 hs3 hct3 i2 hi2C hsc3 y n hky3 hya + ep bd r (hth3.trans hth) hcb3 + .running rfl (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl) + (P1.sched.set_thread_state.running_blkI (Es := P1.EH H) s4 i J3.1.1.blk htH hr0)) ?_ + rintro s5 ⟨hX5, -, htmo⟩ + exact (WP.spec_ok _).2 ⟨rfl, σ3, x3, G, V, htr, hk3, hsc3, hf3, hth3, hkpi, hX5, htmo⟩ + rintro ⟨s5, a2⟩ ⟨ha, σ3, x3, G, V, htr, hk3, hsc3, hf3, hth3, hkpi, hX5, htmo⟩ + simp only at ha + subst ha + have mem3 : ((), σ3) ∈ (Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv σ2).1 := by + rw [htr]; exact Set.mem_singleton _ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σ4 := updK σ3 i.bv (.tcb x5) with hσ4 + set σ5 := stsAct σ4 i.bv with hσ5 + have hrec : σ5 = { σ4 with schedulerAction := σ5.schedulerAction } := stsAct_rec σ4 i.bv + have hk5 : σ5.kheap i.bv = some (.tcb x5) := by rw [hσ5, stsAct_kheap]; simp [hσ4, updK] + have hky5 : σ5.kheap i2.bv = some (.schedContext y n) := by + rw [hσ5, stsAct_kheap, hσ4]; simp only [updK, Function.update_of_ne hti]; rw [hkpi]; exact hky + have htm5 : σ5.curTime = σ2.curTime := by + rw [hrec]; exact (SVK.sv_doIpcTransfer' _ _ _ _ _ σ2 _ mem3).2.1 + have hS5 := hX5.fin.1.1.1 + obtain ⟨Q5, R5, hc5, hsp5⟩ := hS5.lists + obtain ⟨x6, hk6, -, -, hscw5, -⟩ := hsp5.tcb i hiT + rw [hk5] at hk6; cases hk6 + obtain ⟨x6', hk6', hTR5⟩ := hX5.ipc.tcb i hiT + rw [hk5] at hk6'; cases hk6' + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + rw [← hi3] at hscw5 + have h3 : i3 ≠ 0#u64 := by + intro e; rw [e, optRef_zero] at hscw5; rw [hsc3] at hscw5; cases hscw5 + have e3 : i3 = i2 := by + rw [Notif.optRef_ne h3] at hscw5; rw [hsc3] at hscw5 + exact P1.eq_of_bv (Option.some.inj hscw5).symm + subst e3 + step with Fl4Proofs.P3.Sched.obj.Sc.is_null.spec' as ⟨b2, hb2⟩ + rw [hb2, if_neg (by simp [h3])] + refine WP.spec_bind (Fl4Proofs.P3.Sched.is_runnable_spec s5 i) ?_ + rintro b3 rfl + have hrw : L.mem s5 (tStA i) = sched.TS_RUNNING := by + apply UScalar.eq_of_val_eq; rw [hTR5.st]; unfold sched.TS_RUNNING; rfl + rw [if_pos (by simp [hrw])] + have hz1 : ((), σ5) ∈ ((Fl4Spec.doIpcTransfer (Z := Unit) ct.bv none 0 g i.bv >>= fun _ => + Fl4Spec.setThreadState i.bv .running) σ2).1 := by + rw [nd_bind_one htr, Notif.setThreadState_eq _ hk3]; rfl + have hvt5 : Fl4Spec.isEpCap x5.tcbTimeoutHandler = true := by + show Fl4Spec.isEpCap x3.tcbTimeoutHandler = true; rw [hth3]; exact hvt + have tmo : fl4ReplyTmo st arI i ft i3 s5 a2 ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + unfold fl4ReplyTmo + refine WP.spec_bind (valid_timeout_handler_rel s5 σ5 hX5.rel.2.1.1 (hX5.rel.tcbT hk5)) ?_ + rintro b5 ⟨-, x', hkx', hb5⟩ + rw [hk5] at hkx'; cases hkx' + rw [if_pos (by rw [hb5]; exact hvt5)] + rw [if_pos (by simpa using hto)] + step with Fl4Proofs.P3.Sched.obj.Sc.badge.spec' as ⟨bw, hbw⟩ + subst hbw + obtain ⟨y5, n5, hky5', hy5⟩ := hsp5.scs i3 hi2C + rw [hky5] at hky5'; cases hky5' + refine WP.spec_mono (htmo _) ?_ + rintro res ⟨z, hz, hJz⟩ + refine ⟨z, mem_doReplyTransfer hkr hrep hkx hst g hrm hub hki (Or.inl ⟨hfi, hz1⟩) ?_, hJz⟩ + rw [specReplyTail_run hk5 hsc3 rfl hky5, if_neg (by rw [htm5]; exact hnrel)] + unfold specReplyTmo + rw [nd_bind_one (getTcb_eq hk5), if_pos ⟨hvt5, by rw [hfi]; simp [Option.elim]⟩, hy5.badge] + exact hz + unfold fl4ReplyTmo at tmo + refine WP.spec_bind (Fl4Proofs.P3.Sched.ready_refines s5 σ5 hS5 hi2C hky5 hya) ?_ + rintro b4 hb4 + rw [Fl4Proofs.P3.Sched.getScRefillReady_eq hky5 hne2] at hb4 + have hb4' : b4 = Fl4Spec.refillReady σ5.curTime (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b4, σ5) ∈ q.1) hb4; simpa using this.symm + by_cases hr4 : b4 = true + · rw [if_pos hr4] + refine WP.spec_bind (Fl4Proofs.P3.Sched.sufficient_refines s5 _ σ5 hS5 hi2C hky5 hya 0#u64 (hmb _)) ?_ + rintro b5 hb5 + rw [Fl4Proofs.P3.Sched.getScRefillSufficient_eq hky5 hne2] at hb5 + have hb5' : b5 = Fl4Spec.refillSufficient (U64.bv 0#u64) (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b5, σ5) ∈ q.1) hb5; simpa using this.symm + have hr5 : ¬ b5 = true := fun h => hnrel ⟨by rw [← htm5, ← hb4']; exact hr4, by rw [← e0, ← hb5']; exact h⟩ + rw [if_neg hr5] + exact tmo + · rw [if_neg hr4] + exact tmo + · ------------------------------------------------------------------ a fault, restarted by its reply + rw [hks] at hks'; cases hks' + have hft0 : ¬ ft = fl4_abi.consts.fault.NullFault := by + intro e + have h := hxr.fault0.1 (by rw [← hft, e]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hfi] at h; cases h + have hftT : ft = fl4_abi.consts.fault.Timeout ↔ Fl4Spec.isTimeoutFault f = true := by + rw [hft, show fl4_abi.consts.fault.Timeout = 5#u64 by unfold fl4_abi.consts.fault.Timeout; rfl, hxr.faultT, hfi] + cases f <;> simp [Fl4Spec.isTimeoutFault] + have hto : ¬ ft = fl4_abi.consts.fault.Timeout := fun h => absurd (hftT.1 h) (by rw [htf]; decide) + rw [if_neg hft0] + -- after the fault reply: `Restart` and the tail's `handle_timeout` as one step + refine WP.spec_bind (faultBranchV J2 hne hiT hcT hks hki hfi (fun e d h => by rw [hin] at h; cases h) + (fun rst s σ => rst = true → ∃ V' : XVs, XTopAt (L := L) AR (Z.mv Z.G V') s ar σ ∧ + ∀ b : U64, faults.handle_timeout st arI s ar i (error.Fault.Timeout b) ⦃ res' => ∃ z ∈ + (Fl4Spec.handleTimeout (Z := Unit) i.bv (.timeout b.bv) σ).1, VEAnyX (L := L) arI AR res'.1 res'.2 z.2 ⦄) + ?_) ?_ + · intro rst x4 s5 V hx4 J5 + have hk5 : (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap i.bv = + some (.tcb { x4 with tcbFault := none }) := by simp [updK] + have hrs : ({ x4 with tcbFault := none } : Fl4Spec.Tcb).tcbState = .inactive := by + have h := congrArg Fl4Spec.Tcb.tcbState hx4; exact h.trans hin + have hsc5 : ({ x4 with tcbFault := none } : Fl4Spec.Tcb).tcbSchedContext = some i2.bv := by + have h := congrArg Fl4Spec.Tcb.tcbSchedContext hx4; exact h.trans hxsc + have hth5 : ({ x4 with tcbFault := none } : Fl4Spec.Tcb).tcbTimeoutHandler = .endpointCap ep bd r := by + have h := congrArg Fl4Spec.Tcb.tcbTimeoutHandler hx4; exact h.trans hth + obtain ⟨x5, hk5', hTR5⟩ := (J5.xtop.irelK J5.ks J5.buf).1.tcb i hiT + rw [hk5] at hk5'; cases hk5' + have hr0 : L.mem s5 (P1.stA i) = sched.TS_INACTIVE := by + apply UScalar.eq_of_val_eq + rw [P1.stA_eq, ← tStA_eq, hTR5.st, hrs]; unfold sched.TS_INACTIVE; rfl + have htH : i ∈ P1.Heap.tcbs H := J5.zH.1 i hiT + cases rst + · refine WP.spec_mono (J5.stsIV hiT hiD hk5 hrs .inactive (by decide) sched.TS_INACTIVE + (by unfold sched.TS_INACTIVE; rfl) (fun h => by + rcases h with h | ⟨_, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h⟩ <;> cases h) + (P1.sched.set_thread_state.inactive_blkI (Es := P1.EH H) s5 i J5.1.1.blk htH hr0)) ?_ + intro s6 _ h + exact absurd h Bool.false_ne_true + · have hky5 : (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap i2.bv = + some (.schedContext y n) := by simp [updK, hti, hky] + have htc5 : i.bv ≠ (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).curThread := by + show i.bv ≠ σ2.curThread + rw [hct2]; intro e; exact hne (P1.eq_of_bv e).symm + have hcb5 : ∀ xe, (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap ep = + some (.endpoint xe) → EC xe := by + intro xe he + have he' : σ2.kheap ep = some (.endpoint xe) := by simpa [updK, hep] using he + rw [hke2] at he'; cases he'; exact hcb2 + have hcr5 : ∃ xc, (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap + (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).curThread = some (.tcb xc) ∧ + Fl4Spec.runnable xc.tcbState = true := by + have hci : ct.bv ≠ i.bv := fun e => hne (P1.eq_of_bv e) + refine ⟨xs, ?_, hxsr⟩ + show (updK (updK σ2 i.bv (.tcb x4)) i.bv (.tcb { x4 with tcbFault := none })).kheap σ2.curThread = _ + rw [hct2]; simp [updK, hci, hks] + refine WP.spec_mono (hRT _ _ _ _ _ _ _ _ _ (by rfl) J5 hcr5 i hiT hiD _ hk5 hrs htc5 i2 hi2C hsc5 y n hky5 hya + ep bd r hth5 hcb5 + .restart rfl (by decide) sched.TS_RESTART (by unfold sched.TS_RESTART; rfl) + (P1.sched.set_thread_state.restart_blkI (Es := P1.EH H) s5 i J5.1.1.blk htH hr0)) ?_ + rintro s6 ⟨hX6, -, htmo⟩ - + exact ⟨V, hX6, htmo⟩ + rintro ⟨s6, a2⟩ ⟨ha, rst, x4, hx4, hhf2, hz1, hq⟩ + dsimp only at ha hq + subst ha + -- the fault reply restarts (`ReplyRestTmo`) + have hrst : rst = true := by + rw [hhf2] at hhf + exact (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hhf))).1 + subst hrst + obtain ⟨V6, hX6, htmo⟩ := hq rfl + have hk6 : (frSt σ2 i x4 true).kheap i.bv = some (.tcb { ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := .restart }) := by simp [frSt, stsAct_kheap, updK] + have hkp6 : ∀ q, q ≠ i.bv → (frSt σ2 i x4 true).kheap q = σ2.kheap q := by + intro q hq'; simp [frSt, stsAct_kheap, updK, hq'] + have hrec6 : frSt σ2 i x4 true = { (updK (updK (updK σ2 i.bv (.tcb x4)) i.bv + (.tcb { x4 with tcbFault := none })) i.bv (.tcb { ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := if true then .restart else .inactive })) with + schedulerAction := (frSt σ2 i x4 true).schedulerAction } := stsAct_rec _ _ + have htm6 : (frSt σ2 i x4 true).curTime = σ2.curTime := by rw [hrec6]; rfl + have hsc6 : ({ ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := Fl4Spec.ThreadState.restart } : Fl4Spec.Tcb).tcbSchedContext = some i2.bv := by + have h := congrArg Fl4Spec.Tcb.tcbSchedContext hx4; exact h.trans hxsc + have hth6 : ({ ({ x4 with tcbFault := none } : Fl4Spec.Tcb) with + tcbState := Fl4Spec.ThreadState.restart } : Fl4Spec.Tcb).tcbTimeoutHandler = xi.tcbTimeoutHandler := by + have h := congrArg Fl4Spec.Tcb.tcbTimeoutHandler hx4; exact h + have hky6 : (frSt σ2 i x4 true).kheap i2.bv = some (.schedContext y n) := by rw [hkp6 _ hti]; exact hky + have hS6 := hX6.fin.1.1.1 + obtain ⟨Q6, R6, hc6, hsp6⟩ := hS6.lists + obtain ⟨x6, hk6', -, -, hscw6, -⟩ := hsp6.tcb i hiT + rw [hk6] at hk6'; cases hk6' + obtain ⟨x6', hk6'', hTR6⟩ := hX6.ipc.tcb i hiT + rw [hk6] at hk6''; cases hk6'' + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i3, hi3⟩ + rw [← hi3] at hscw6 + have h3 : i3 ≠ 0#u64 := by + intro e; rw [e, optRef_zero] at hscw6; rw [hsc6] at hscw6; cases hscw6 + have e3 : i3 = i2 := by + rw [Notif.optRef_ne h3] at hscw6; rw [hsc6] at hscw6 + exact P1.eq_of_bv (Option.some.inj hscw6).symm + subst e3 + step with Fl4Proofs.P3.Sched.obj.Sc.is_null.spec' as ⟨b2, hb2⟩ + rw [hb2, if_neg (by simp [h3])] + refine WP.spec_bind (Fl4Proofs.P3.Sched.is_runnable_spec s6 i) ?_ + rintro b3 rfl + have hrw : L.mem s6 (tStA i) = sched.TS_RESTART := by + apply UScalar.eq_of_val_eq; rw [hTR6.st]; unfold sched.TS_RESTART; rfl + rw [if_pos (by rw [hrw]; simp)] + have hvt6 : Fl4Spec.isEpCap xi.tcbTimeoutHandler = true := hvt + have tmo : fl4ReplyTmo st arI i ft i3 s6 a2 ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + unfold fl4ReplyTmo + refine WP.spec_bind (valid_timeout_handler_rel s6 (frSt σ2 i x4 true) hX6.rel.2.1.1 (hX6.rel.tcbT hk6)) ?_ + rintro b5 ⟨-, x', hkx', hb5⟩ + rw [hk6] at hkx'; cases hkx' + rw [if_pos (by rw [hb5]; exact (congrArg Fl4Spec.isEpCap hth6).trans hvt6)] + rw [if_pos (by simpa using hto)] + step with Fl4Proofs.P3.Sched.obj.Sc.badge.spec' as ⟨bw, hbw⟩ + subst hbw + obtain ⟨y6, n6, hky6', hy6⟩ := hsp6.scs i3 hi2C + rw [hky6] at hky6'; cases hky6' + refine WP.spec_mono (htmo _) ?_ + rintro res ⟨z, hz, hJz⟩ + refine ⟨z, mem_doReplyTransfer hkr hrep hkx hst g hrm hub hki (Or.inr ⟨f, hfi, hz1⟩) ?_, hJz⟩ + rw [specReplyTail_run hk6 hsc6 rfl hky6, if_neg (by rw [htm6]; exact hnrel)] + unfold specReplyTmo + rw [nd_bind_one (getTcb_eq hk6), if_pos ⟨by rw [hth6]; exact hvt6, by rw [hfi]; simp [Option.elim, htf]⟩, + hy6.badge] + exact hz + unfold fl4ReplyTmo at tmo + refine WP.spec_bind (Fl4Proofs.P3.Sched.ready_refines s6 (frSt σ2 i x4 true) hS6 hi2C hky6 hya) ?_ + rintro b4 hb4 + rw [Fl4Proofs.P3.Sched.getScRefillReady_eq hky6 hne2] at hb4 + have hb4' : b4 = Fl4Spec.refillReady (frSt σ2 i x4 true).curTime (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b4, frSt σ2 i x4 true) ∈ q.1) hb4; simpa using this.symm + by_cases hr4 : b4 = true + · rw [if_pos hr4] + refine WP.spec_bind (Fl4Proofs.P3.Sched.sufficient_refines s6 _ (frSt σ2 i x4 true) hS6 hi2C hky6 hya 0#u64 + (hmb _)) ?_ + rintro b5 hb5 + rw [Fl4Proofs.P3.Sched.getScRefillSufficient_eq hky6 hne2] at hb5 + have hb5' : b5 = Fl4Spec.refillSufficient (U64.bv 0#u64) (Fl4Spec.refillHd y) := by + have := congrArg (fun q => (b5, frSt σ2 i x4 true) ∈ q.1) hb5; simpa using this.symm + have hr5 : ¬ b5 = true := fun h => hnrel ⟨by rw [← htm6, ← hb4']; exact hr4, by rw [← e0, ← hb5']; exact h⟩ + rw [if_neg hr5] + exact tmo + · rw [if_neg hr4] + exact tmo + +set_option maxHeartbeats 8000000 in +/-- **`do_reply_transfer` at `J`**: no thread on the reply, or a thread not +blocked on it, nothing happens on either side; a thread blocked on it, +`reply_remove` at `J` (`JAtZ.replyRemoveJD`), then `replyRestNoScJ`, +`replyRestSwJ`, `replyRestPostJ`, `replyRestFaultJ`, or (`ReplyRestTmo`, the +split complete) `replyRestTmoJ` with `runTmoP_idle` (`TmoIdle`) or with +`hRT` (receivers waiting on the handler endpoint: XTopJointFaultTmo's +`runTmoP_recv`). The caller stays `Restart` across `reply_remove` +(`st_replyRemove'`), so it is not the receiver the timeout fault wakes. -/ +theorem VmEptState.replyXferV (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {s1 : S} {ar : A} {σ1 : Fl4Spec.State Unit} + (J1 : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s1 ar σ1) [VxE Z] {ct p : U64} (hp : p ∈ Z.R) (g : Bool) + (hcs : σ1.curThread = ct.bv ∧ ∃ xc, σ1.kheap ct.bv = some (.tcb xc) ∧ xc.tcbState = .restart) + (hRT : RunTmoPV (L := L) arI VX XX XL AR Z (fun xe => ¬ Endp.CanBlock false xe)) : + ipc.do_reply_transfer st arI s1 ar ct p g ⦃ res => + ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ := by + have hJ1 : VEAnyX (L := L) arI AR s1 ar σ1 := J1.anyX arI + have hI := J1.xtop.irelK J1.ks J1.buf + obtain ⟨xr, hkr, hxr⟩ := hI.1.reply p hp + unfold ipc.do_reply_transfer + step with Fl4Proofs.P3.Endp.obj.Reply.tcb.spec as ⟨i, hi⟩ + step with Fl4Proofs.P1.obj.Tcb.is_null.spec as ⟨b, hb⟩ + by_cases h0 : i = 0#u64 + · rw [hb, if_pos (by simp [h0]), WP.spec_ok] + exact ⟨((), σ1), by rw [doReplyTransfer_none hkr (by rw [hxr, ← hi, h0, optRef_zero])]; rfl, hJ1⟩ + rw [hb, if_neg (by simp [h0])] + have hrep : xr.replyTcb = some i.bv := by rw [hxr, ← hi, Notif.optRef_ne h0] + obtain ⟨x, hkx, hxs⟩ := J1.1.1.sv.replyTcb p.bv xr i.bv hkr hrep + have hiT : i ∈ Z.T := by + obtain ⟨t, ht, h⟩ := J1.1.1.obj.tcb _ x hkx + rw [Sched.bv_inj h] at ht; exact ht + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb i hiT + rw [hkx] at hk0; cases hk0 + step with Fl4Proofs.P3.Notif.obj.Tcb.state.spec as ⟨i1, hi1⟩ + rcases hxs with hst | ⟨ep, d, hst⟩ + swap + · -- receiving with the reply: nothing happens + have hne : (i1 != sched.TS_BLOCKED_ON_REPLY) = true := by + have h := hTR.st + rw [hst] at h + rw [hi1] + have : L.mem s1 (tStA i) ≠ sched.TS_BLOCKED_ON_REPLY := by + intro e; rw [e] at h; unfold sched.TS_BLOCKED_ON_REPLY at h; simp [tsCode] at h + simpa using this + rw [if_pos hne, WP.spec_ok] + refine ⟨((), σ1), ?_, hJ1⟩ + rw [doReplyTransfer_notReply hkr hrep hkx (by rw [hst]; intro r' h; cases h)]; rfl + have hne : (i1 != sched.TS_BLOCKED_ON_REPLY) = false := by + have h := hTR.st + rw [hst] at h + rw [hi1] + have : L.mem s1 (tStA i) = sched.TS_BLOCKED_ON_REPLY := by + apply UScalar.eq_of_val_eq; rw [h]; unfold sched.TS_BLOCKED_ON_REPLY; rfl + simp [this] + rw [if_neg (by rw [hne]; simp)] + have hiD : i ∉ D := fun h => J1.1.1.notDead hiT hkx (by rw [hst]; simp [tsCode]) i h rfl + have hne : ct ≠ i := by + rintro rfl + obtain ⟨xc, hkc, hcr⟩ := hcs.2 + rw [hkc] at hkx; cases hkx; rw [hcr] at hst; cases hst + obtain ⟨⟨s2, a2⟩, hrr, hpost⟩ := spec_exists (J1.replyRemoveV hAT hp hiT hkx hst) + obtain ⟨ea, σ2, V', Ac', Lst', hrm, J2, hin, hact⟩ := hpost + simp only at ea + subst ea + rw [hrr] + simp only [bind_ok] + have hct2 : σ2.curThread = ct.bv := by + rw [(Vs.dk_replyRemove' i.bv p.bv).ct (x := ((), σ2)) (by rw [hrm]; exact Set.mem_singleton _)] + exact hcs.1 + obtain ⟨xi0, hki0, hin0⟩ := hin + suffices key : fl4ReplyRest st arI s2 a2 ct i g ⦃ res => ∃ z ∈ (Fl4Spec.doReplyTransfer (Z := Unit) ct.bv p.bv g σ1).1, + VEAnyX (L := L) arI AR res.1 res.2 z.2 ⦄ from key + rw [fl4ReplyRest_eq] + refine J2.replyUnblockV hiT hki0 hin0 (fun s3 => fl4ReplyRest2 st arI s3 a2 ct i g) _ ?_ + intro s3 σ3 Lst3 hub J3 htk hct3 hscK + -- the receiver's SC is active (`replyRemoveJD`'s `remove_act`, kept by the unblock test) + have hact3 : ∀ q y n, σ3.kheap q = some (.schedContext y n) → y.scTcb = some i.bv → + Fl4Spec.scActive y = true := by + intro q y n hq hyt + obtain ⟨y0, hq0, hm, htc⟩ := hscK q y n hq + have h0 := hact q y0 n hq0 (htc.symm.trans hyt) + show Fl4Spec.activeSc y.scRefillMax = true + rw [hm]; exact h0 + have hin3 : ∃ x', σ3.kheap i.bv = some (.tcb x') ∧ x'.tcbState = .inactive := ⟨xi0, htk _ _ hki0, hin0⟩ + have hct3' : σ3.curThread = ct.bv := hct3.trans hct2 + have hcT0 : ct ∈ Z.T := by + obtain ⟨xc, hkc, -⟩ := hcs.2 + obtain ⟨t, ht, h⟩ := J1.1.1.obj.tcb _ xc hkc + rw [Sched.bv_inj h] at ht; exact ht + have hTr : ∀ xs xi, σ3.kheap ct.bv = some (.tcb xs) → σ3.kheap i.bv = some (.tcb xi) → xi.tcbFault = none → + ReplyMessageTransferV (L := L) arI AR (Z.mv Z.G V') H Cs D VX XX XL s3 a2 σ3 ct i g xs xi := + fun xs xi hks hki _ => J3.replyMessageTransferV hAT hmax (htu a2) hcT0 hiT hne hks hki g + by_cases hC : ReplyRestNoSc σ3 ct i g + · exact replyRestNoScV (Z := Z.mv Z.G V') J3 g hiD hkr hrep hkx hst hrm hub hin3 hC hTr + by_cases hC2 : ReplyRestSw σ3 ct i g + · exact replyRestSwV (Z := Z.mv Z.G V') hmb J3 g hne hiD hct3' hkr hrep hkx hst hrm hub hin3 hC2 hTr + by_cases hC3 : ReplyRestPost σ3 ct i g + · exact replyRestPostV (Z := Z.mv Z.G V') hmb J3 g hne hiD hct3' hkr hrep hkx hst hrm hub hin3 hC3 hTr + by_cases hC4 : ReplyRestFault σ3 ct i + · exact replyRestFaultV (Z := Z.mv Z.G V') hmb J3 g hne hiD hct3' hkr hrep hkx hst hrm hub hin3 hact3 hC4 + -- the case left: the split is complete (`ReplyRestTmo`) + have hcT : ct ∈ Z.T := by + obtain ⟨xc, hkc, -⟩ := hcs.2 + obtain ⟨t, ht, h⟩ := J1.1.1.obj.tcb _ xc hkc + rw [Sched.bv_inj h] at ht; exact ht + obtain ⟨xs, hks3, -⟩ := (J3.xtop.irelK J3.ks J3.buf).1.tcb ct hcT + have hTmo : ReplyRestTmo σ3 ct i := by + obtain ⟨xi, hki3, -⟩ := hin3 + cases hf : xi.tcbFault with + | none => + cases hsc : xi.tcbSchedContext with + | none => exact absurd ⟨xs, xi, hks3, hki3, hsc, hf⟩ hC + | some sp => + obtain ⟨y, n, hky, hyt⟩ := J3.1.1.inv.back _ _ _ hki3 hsc + have hya := hact3 _ y n hky hyt + have hnrel : ¬ (Fl4Spec.refillReady σ3.curTime (Fl4Spec.refillHd y) = true ∧ + Fl4Spec.refillSufficient 0 (Fl4Spec.refillHd y) = true) := + fun h => hC2 ⟨xs, xi, sp, y, n, hks3, hki3, hsc, hky, hya, h.1, h.2, hf⟩ + have hvt : Fl4Spec.isEpCap xi.tcbTimeoutHandler = true := by + cases h : Fl4Spec.isEpCap xi.tcbTimeoutHandler + · exact absurd ⟨xs, xi, sp, y, n, hks3, hki3, hsc, hky, hya, hnrel, h, hf⟩ hC3 + · rfl + exact ⟨xi, sp, y, n, hki3, hsc, hky, hya, hnrel, hvt, Or.inl hf⟩ + | some f => + have h4 : ∃ σ', Fl4Spec.handleFaultReply (Z := Unit) f i.bv (frLbl xs) (frMrs xs σ3) σ3 = + ({(true, σ')}, False) ∧ ReplyFaultTmo σ3 xi f := by + by_contra hc + exact hC4 ⟨xs, xi, f, hks3, hki3, hf, fun σ' h1 h2 => hc ⟨σ', h1, h2⟩⟩ + obtain ⟨σ', hhf, sp, y, n, hsc, hky, hnrel, hvt, htf⟩ := h4 + obtain ⟨y0, n0, hky0, hyt⟩ := J3.1.1.inv.back _ _ _ hki3 hsc + rw [hky] at hky0; cases hky0 + exact ⟨xi, sp, y, n, hki3, hsc, hky, hact3 _ y n hky hyt, hnrel, hvt, Or.inr ⟨xs, f, σ', hks3, hf, htf, hhf⟩⟩ + -- the caller is still `Restart` + have hxsr : Fl4Spec.runnable xs.tcbState = true := by + obtain ⟨xc, hkc, hcr⟩ := hcs.2 + obtain ⟨y2, hk2, -⟩ := (J2.xtop.irelK J2.ks J2.buf).1.tcb ct hcT + obtain ⟨y0, hy0, hst0⟩ := (st_replyRemove' i.bv p.bv σ1 ((), σ2) (by rw [hrm]; exact Set.mem_singleton _)).1 + ct.bv y2 (fun e => hne (P1.eq_of_bv e)) hk2 + rw [hkc] at hy0; cases hy0 + have h3 := htk _ _ hk2 + rw [hks3] at h3; cases h3 + rw [hst0, hcr]; rfl + by_cases hid : TmoIdle σ3 i + · exact replyRestTmoV (Z := Z.mv Z.G V') hmb hAT hmax htu J3 g hne hiD hct3' hkr hrep hkx hst hrm hub hin3 hks3 + hxsr (runTmoP_idleV arI _) hTmo hid + have hid' : TmoEp (fun xe => ¬ Endp.CanBlock false xe) σ3 i := by + obtain ⟨xi, sp, y, n, hki, -, -, -, -, hvt, -⟩ := hTmo + obtain ⟨ep, bd, r, hth⟩ : ∃ ep bd r, xi.tcbTimeoutHandler = .endpointCap ep bd r := by + cases h : xi.tcbTimeoutHandler <;> simp [h, Fl4Spec.isEpCap] at hvt + exact ⟨_, _, _, rfl⟩ + obtain ⟨xe, hke⟩ := J3.tmoEpObj hiT hki hth + exact ⟨xi, ep, bd, r, xe, hki, hth, hke, fun hcb => hid ⟨xi, ep, bd, r, xe, hki, hth, hke, hcb⟩⟩ + exact replyRestTmoV (Z := Z.mv Z.G V') hmb hAT hmax htu J3 g hne hiD hct3' hkr hrep hkx hst hrm hub hin3 hks3 + hxsr hRT hTmo hid' + +end Tmo + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ReplyXfer.lean b/proofs/Fl4Proofs/P3/VmEntryS3ReplyXfer.lean new file mode 100644 index 000000000..587761a64 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ReplyXfer.lean @@ -0,0 +1,150 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, Reply arm): **`reply_remove` and the sporadic unblock check at `VmEptState`**. + +* `VmEptState.replyRemoveV`: P3/XTopJointRemove's `JAtZB.replyRemoveJD` then `VmEptState.ofNat` (its `NatFoot`). +* `VmEptState.replyUnblockV`: P3/XTopJointUnblock's `JAtZ.replyUnblockJ` at `VmEptState`; the unblock step is + `JAtZB.unblockRJ` + `VmEptState.ofNat` (as `JAtZB.sendUnblockJ`). + +Premises: `P0.ArchTotal` [P] for `reply_remove`. No other. New file. +-/ +import Fl4Proofs.P3.VmEntryS3XferAll +import Fl4Proofs.P3.XTopJointRemove +import Fl4Proofs.P3.XTopJointUnblock +import Fl4Proofs.P3.XTopJointSendRecv +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`reply_remove` at `VmEptState`** (module docstring). -/ +theorem VmEptState.replyRemoveV (hT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {r t : U64} {x : Fl4Spec.Tcb} (hr : r ∈ Z.R) (ht : t ∈ Z.T) (hk : σ.kheap t.bv = some (.tcb x)) + (hst : x.tcbState = .blockedOnReply r.bv) : + ipc.reply_remove st arI s a r t ⦃ res => res.2 = a ∧ ∃ σ' V' Ac' Lst', + Fl4Spec.replyRemove (Z := Unit) t.bv r.bv σ = ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D res.1 res.2 σ' ∧ + (∃ x', σ'.kheap t.bv = some (.tcb x') ∧ x'.tcbState = .inactive) ∧ + (∀ q y n, σ'.kheap q = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) ⦄ := by + refine WP.spec_mono (J.1.1.replyRemoveJD hT hr ht hk hst) ?_ + rintro res ⟨ha, σ', V', Ac', Lst', D', hrm, B', F, -, -, hD, hin, hact⟩ + subst hD + exact ⟨ha, σ', V', Ac', Lst', hrm, J.ofNat B' F, hin, hact⟩ + +set_option maxHeartbeats 8000000 in +/-- **The reply's sporadic unblock check at `VmEptState`** (`JAtZ.replyUnblockJ`, XTopJointUnblock). -/ +theorem VmEptState.replyUnblockV (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .inactive) + {X' : Type} (k : S → Result X') (P : X' → Prop) + (hc : ∀ (s' : S) (σ' : Fl4Spec.State Unit) (Lst' : U64 → List U64), + (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext t.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D s' a σ' → + (∀ q xq, σ.kheap q = some (.tcb xq) → σ'.kheap q = some (.tcb xq)) → σ'.curThread = σ.curThread → + (∀ q y n, σ'.kheap q = some (.schedContext y n) → ∃ y0, σ.kheap q = some (.schedContext y0 n) ∧ + y.scRefillMax = y0.scRefillMax ∧ y.scTcb = y0.scTcb) → + k s' ⦃ P ⦄) : + (do + let i2 ← obj.Tcb.sc st t s + let b1 ← sporadic.sc_sporadic st s i2 + let s2 ← + if b1 + then + do + let i3 ← obj.Sc.cpu st i2 s + let i4 ← obj.node_get st s i3 layout.node.CUR_SC + if i2 != i4 + then sporadic.unblock_check st s i2 + else ok s + else ok s + k s2) ⦃ P ⦄ := by + have B := J.1.1 + have hS := B.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc0, hs⟩ := hS.lists + obtain ⟨xt, hkt, -, -, hsct, -⟩ := hs.tcb t ht + rw [hk] at hkt; cases hkt + have hspec0 : (Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext t.bv >>= + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck) σ = (Fl4Spec.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) + (optRef (L.mem s (scA t)))) σ := by + unfold Fl4Spec.getTcbObjRef + rw [nd_bind_one (threadGet_eq _ hk), hsct] + have skip : (Fl4Spec.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) (optRef (L.mem s (scA t)))) σ = + ({((), σ)}, False) → k s ⦃ P ⦄ := fun h => + hc s σ Lst (by rw [hspec0, h]) J (fun _ _ h => h) rfl (fun _ y _ h => ⟨y, h, rfl, rfl⟩) + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i2, hi2⟩ + step with sporadic.sc_sporadic.spec' as ⟨b, b_post⟩ + subst hi2 + by_cases h0 : L.mem s (scA t) = 0#u64 + · have hb : b = false := by rw [b_post]; simp [h0] + simp only [hb, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact skip (by rw [h0, optRef_zero]; rfl) + have hscC := hs.scMem t ht h0 + obtain ⟨y, n, hky, hy⟩ := hs.scs _ hscC + have hsp : (y.scSporadic = true) ↔ L.mem s (scW (L.mem s (scA t)) 64#u64) ≠ 0#u64 := by + rw [hy.sporadic]; simp + have hif : ∀ (P : Prop) [Decidable P] (Q : Prop) [Decidable Q], + Fl4Spec.ifSporadicCurScTestRefillUnblockCheck (Z := Unit) (optRef (L.mem s (scA t))) σ = + (if y.scSporadic = true ∧ (L.mem s (scA t)).bv ≠ σ.curSc then + Fl4Spec.refillUnblockCheck (L.mem s (scA t)).bv σ else ({((), σ)}, False)) := by + intro _ _ _ _ + rw [Notif.optRef_ne h0] + unfold Fl4Spec.ifSporadicCurScTestRefillUnblockCheck Fl4Spec.ifCondRefillUnblockCheck + simp only [Fl4Spec.NonDet.maybeM] + rw [nd_bind_one (getSchedContext_eq hky), nd_gets_bind, nd_pure_bind'] + simp only [Fl4Spec.NonDet.when] + by_cases e : y.scSporadic = true ∧ (L.mem s (scA t)).bv ≠ σ.curSc + · simp only [e, ne_eq, not_false_eq_true, forall_const, and_self, ↓reduceIte, Option.some.injEq, + Bool.false_eq_true, IsEmpty.forall_iff] + · simp only [ne_eq, forall_const, ↓reduceIte, Option.some.injEq] + rw [ite_f (c := y.scSporadic = true ∧ ¬ (L.mem s (scA t)).bv = σ.curSc) (by simpa only [ne_eq] using e), + ite_f (c := y.scSporadic = true ∧ ¬ (L.mem s (scA t)).bv = σ.curSc) (by simpa only [ne_eq] using e)] + rfl + have hif' := hif True True + by_cases hsp' : L.mem s (scW (L.mem s (scA t)) 64#u64) ≠ 0#u64 + swap + · have hb : b = false := by rw [b_post]; simp [h0]; simpa using hsp' + simp only [hb, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact skip (by rw [hif', ite_f (fun h => hsp' (hsp.1 h.1))]) + have hb : b = true := by rw [b_post]; simp [h0, hsp'] + simp only [hb, ↓reduceIte] + step with obj.Sc.cpu.spec as ⟨cpu, hcpu⟩ + have hcpu' : cpu = Z.c := by rw [hcpu, Fl4Proofs.P3.Sched.scCoreA_eq]; exact hS.core _ hscC + step with obj.node_get.spec as ⟨i4, hi4⟩ + rw [hcpu'] at hi4 + by_cases e : L.mem s (scA t) = nodeW st s Z.c layout.node.CUR_SC + · have : (L.mem s (scA t) != i4) = false := by rw [hi4]; simpa [nodeW] using e + simp only [this, Bool.false_eq_true, ↓reduceIte, bind_ok] + exact skip (by rw [hif', ite_f]; rw [e, ← hs.curSc]; simp) + · have : (L.mem s (scA t) != i4) = true := by rw [hi4]; simpa [nodeW] using e + simp only [this, ↓reduceIte] + have hpc : (L.mem s (scA t)).bv ≠ σ.curSc := by rw [hs.curSc]; exact bv_ne e + have hxsc : x.tcbSchedContext = some (L.mem s (scA t)).bv := by + rw [hsct, Notif.optRef_ne h0] + have hya : Fl4Spec.scActive y = true := hy.active_of_rmax (hy.spor hsp') + have hnr : t.bv ∉ σ.releaseQueue := Ipc.not_released_of B.inv hk (by rw [hrs]; rfl) + refine WP.spec_bind (B.unblockRJ ht hk hnr hscC hxsc hpc hky hya) ?_ + rintro s' ⟨σ', he, hkp, hct, hid, hrl, ⟨z, hkz, hzm, hzt⟩, -, hK0, hwT, Lst', B'⟩ + have J' := J.ofNat (V' := Z.vs) (by simpa using B') + (NatFoot.ofT B.xtop hwT (fun b hb => B.tfoot_nh hb) (natK_tk hK0)) + refine hc s' σ' Lst' ?_ (by simpa using J') (fun q xq hq => ?_) hct (fun q y' n' hq => ?_) + · rw [hspec0, hif', ite_t ⟨hsp.2 hsp', hpc⟩, he] + · by_cases eq : q = (L.mem s (scA t)).bv + · rw [eq, hky] at hq; cases hq + · rw [hkp q eq]; exact hq + · by_cases eq : q = (L.mem s (scA t)).bv + · subst eq + rw [hkz] at hq; cases hq + exact ⟨y, hky, hzm, hzt⟩ + · rw [hkp q eq] at hq; exact ⟨y', hq, rfl, rfl⟩ + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Sc.lean b/proofs/Fl4Proofs/P3/VmEntryS3Sc.lean new file mode 100644 index 000000000..13158e2b2 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Sc.lean @@ -0,0 +1,1714 @@ +/- +Selected SchedContext dispatch from the actual successful lookup. The first +phase and unsupported labels give a paired decoder error. The ordinary Unbind +branch uses the three shared SC unlink refinements and carries their VM frames. + +Bind and UnbindObject use a scoped scheduler footprint. Consumed and YieldTo +use the paired eager-reply state when the decoder has already replied. +-/ +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.FrontEndLabel +import Fl4Proofs.P3.ScJointConsumed +import Fl4Proofs.P3.ScJointYield +import Fl4Proofs.P3.VmEntryS3Yield +import Fl4Proofs.P3.ScRestJ +import Fl4Proofs.P3.XTopJointKEntryU30 +import Fl4Proofs.P3.VmEntryS3XferAll + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched +open Fl4Proofs.P1 + +noncomputable section +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} +variable {MR : A → Fl4Spec.MachineState → Prop} + +private theorem JAtZB.selectedScBack + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {sl : Fl4Spec.CslotPtr} {sc sb : U64} (hs : AbsSlot Z.th sl) + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.SchedContext sc sb)) : + sc ∈ Z.C ∧ sc.bv ≠ σ.idleSc ∧ + (L.mem s (scW sc 24#u64) ≠ 0#u64 → + L.mem s (scW sc 24#u64) ∈ Z.T ∧ L.mem s (P1.scA (L.mem s (scW sc 24#u64))) = sc ∧ + (L.mem s (scW sc 24#u64)).bv ≠ σ.idleThread) ∧ + (∀ y n, σ.kheap sc.bv = some (.schedContext y n) → ∀ m : U64, y.scNtfn = some m.bv → m ∈ Z.N) := by + obtain ⟨⟨y, n, hk, hn⟩, hpi⟩ := B.scCap arI hs hc + obtain ⟨p, hp, e⟩ := B.obj.sc _ y n hk + have hpC : sc ∈ Z.C := by rw [← P1.eq_of_bv e]; exact hp + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hH : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc y n (B.c0 sc hpC) hk).2⟩ + have hp0 := B.c0 sc hpC + refine ⟨hpC, hpi, fun h0 => ?_, fun y n hk m hm => ?_⟩ + · have hX := B.blk.2.2.1 + have h0' : L.mem s (P1.scTcbA sc) ≠ 0#u64 := by rw [scTcbA_eq]; exact h0 + have huT : L.mem s (P1.scTcbA sc) ∈ Z.T := by + rw [B.tcbs_eq.1]; exact hX.2.2.yt.1 sc hH h0' + have htsc : L.mem s (P1.scA (L.mem s (P1.scTcbA sc))) = sc := hX.2.2.tsc sc hH hp0 h0' + rw [scTcbA_eq] at huT htsc + refine ⟨huT, htsc, fun e => ?_⟩ + obtain ⟨Q, R, hc', hS⟩ := B.xtop.fin.1.1.1.lists + obtain ⟨xu, hku, -, -, hsu, -⟩ := hS.tcb _ huT + obtain ⟨xi, hki, hsi, -⟩ := B.act.2 + rw [e, hki] at hku + cases hku + rw [hsu, htsc, Notif.optRef_ne hp0] at hsi + exact hpi (Option.some.inj hsi) + · obtain ⟨xn, hxn⟩ := B.sv.scNtfn _ y n _ hk hm + obtain ⟨m', hm', hmm⟩ := B.obj.ntfn _ xn hxn + rw [← P1.eq_of_bv hmm]; exact hm' + +private theorem JAtZB.scWordsResume + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {p : U64} (hpC : p ∈ Z.C) (hp0 : p ≠ 0#u64) + (hpi : p.bv ≠ σ.idleSc) (hni : Entered σ) : + (L.mem s (P1.scTcbA p) ≠ 0#u64 → L.actionW s ≠ L.mem s (P1.scTcbA p)) ∧ + (L.mem s (P1.scTcbA p) ≠ 0#u64 → L.idleW s ≠ L.mem s (P1.scTcbA p)) := by + have hw : L.mem s (P1.scTcbA p) ≠ 0#u64 → + L.actionW s ≠ L.mem s (P1.scTcbA p) ∧ L.idleW s ≠ L.mem s (P1.scTcbA p) := by + intro h0 + obtain ⟨Q, R, hc', hs⟩ := B.xtop.fin.1.1.1.lists + obtain ⟨y, n, hk, hx⟩ := hs.scs p hpC + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hpS : p ∈ P1.Heap.scs H := ⟨_, (hJ.sc p y n hp0 hk).2⟩ + have hf : y.scTcb = some (L.mem s (P1.scTcbA p)).bv := by + rw [hx.tcb, ← scTcbA_eq, Notif.optRef_ne h0] + obtain ⟨u, huT, hu, x, hkx, hxs⟩ := scBack_of B.xtop.fin.1.1.1 + B.blk.2.2.1 B.tcbs_eq.1.symm hpC hpS hp0 hk hf + have hu' : u = L.mem s (P1.scTcbA p) := P1.eq_of_bv hu + subst hu' + refine B.words_of_spec huT (by simp [hni.2]) fun e => ?_ + obtain ⟨xi, hki, hsci, -⟩ := B.act.2 + rw [← e, hkx] at hki + cases hki + rw [hxs] at hsci + exact hpi (Option.some.inj hsci) + exact ⟨fun h => (hw h).1, fun h => (hw h).2⟩ + +private theorem scSpecDecV + (lbl d : Fl4Spec.Data) (sc sb : U64) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) + (h : ∀ p xs, Fl4Spec.decodeSchedContextInvocation (Z := Unit) lbl p xs = + Fl4Spec.decodeSchedContextInvocation d p xs) : + specDecV false lbl args ci sl (.SchedContext sc sb) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeSchedContext + (Fl4Spec.decodeSchedContextInvocation (Z := Unit) d sc.bv (exs.map Prod.fst))) := by + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + simp only [Fl4Spec.fl4DecodeInvocation, absCap, Bool.false_eq_true, ↓reduceIte, h] + +private theorem scStallLocal + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (sc : U64) + (hback : L.mem s (scW sc 24#u64) ≠ 0#u64 → L.mem s (scW sc 24#u64) ∈ Z.T) : + (if (L.mem s (scW sc 24#u64) != 0#u64) = true then + smp.remote_tcb_stall st arI s a (L.mem s (scW sc 24#u64)) + else ok (s, a)) ⦃ r => r = (s, a) ⦄ := by + by_cases h0 : L.mem s (scW sc 24#u64) = 0#u64 + · simp only [h0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + · have hne : (L.mem s (scW sc 24#u64) != 0#u64) = true := by simp [h0] + simp only [hne, ↓reduceIte] + exact remote_tcb_stall_id (ar := arI) a B.xtop.fin.1.1.1 (hback h0) + +set_option maxHeartbeats 8000000 in +private theorem VmEptState.scUnbindInvokeS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {ct : U64} (hct : sched.cur_thread st s = ok ct) + {sc : U64} (hsc : sc ∈ Z.C) (hpi : sc.bv ≠ σ.idleSc) : + (do + let s1 ← sched.set_thread_state st s ct sched.TS_RESTART + let (s2, a2) ← schedcontext.unbind_all_tcbs st arI s1 a sc + let s3 ← schedcontext.unbind_ntfn st s2 sc + let s4 ← schedcontext.unbind_reply st s3 sc + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s4, a2)) ⦃ res => + res.1 = .Ok () ∧ ∃ σ1 σ', + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + Fl4Spec.invokeSchedContext (Z := Unit) (.invokeSchedContextUnbind sc.bv) σ1 = ({([], σ')}, False) ∧ + VEAnyX (L := L) arI AR res.2.1 res.2.2 σ' ⦄ := by + have hcw : ct = L.curW s := by + have h := P1.sched.cur_thread.spec (st := st) s + rw [hct, WP.spec_ok] at h + exact h + subst ct + obtain ⟨s1, σ1, Lst1, x, -, -, hs1, he1, J1, -, -, -, hni1⟩ := J.restartK hni + rw [hs1]; simp only [bind_ok] + have B1 := J1.1.1 + have hp0 : sc ≠ 0#u64 := B1.c0 sc hsc + have hpi1 : sc.bv ≠ σ1.idleSc := by + rw [((pres_setThreadState (f := (true, true)) _ _).of_eq he1).isc] + exact hpi + obtain ⟨hna, hidle⟩ := B1.scWordsResume hsc hp0 hpi1 hni1 + refine WP.spec_bind (B1.unbindAllJD arI hsc hp0 hpi1 hna hidle) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, he2, V2, Ac2, Lst2, B2, F2⟩ + have J2 := J1.ofNat B2 F2 + refine WP.spec_bind (B2.unbindNtfnJD arI hsc hp0) ?_ + rintro s3 ⟨σ3, he3, V3, Ac3, Lst3, B3, F3⟩ + have J3 := J2.ofNat B3 F3 + refine WP.spec_bind (B3.unbindReplyJD arI hsc hp0) ?_ + rintro s4 ⟨σ4, he4, V4, Ac4, Lst4, B4, F4⟩ + rw [WP.spec_ok] + refine ⟨rfl, σ1, σ4, he1, ?_, (J3.ofNat B4 F4).anyX arI⟩ + show (Fl4Spec.schedContextUnbindAllTcbs sc.bv >>= _) σ1 = _ + rw [nd_bind_one he2, nd_bind_one he3, nd_bind_one he4] + rfl + +set_option maxHeartbeats 8000000 in +/-- Selected SchedContext first-phase rejection and ordinary Unbind. The +ordinary invalid label follows the same reached local-stall prefix. -/ +theorem VmEptState.selectedSchedContextUnbindS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) + {ct cptr len buf lbl sc sb : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedContext sc sb) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don fp : Bool) + (hl : fp = true ∨ lbl = fl4_abi.label.SchedContextUnbind ∨ + scLabelOf lbl = .invalidInvocation) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf ex + ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.SchedContext sc sb) exs buffer) s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases fp + case true => + simp only [↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + exact run_liftME_err rfl + case false => + simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨d, hd, -, hcanon⟩ := sc_canon lbl + rw [scSpecDecV lbl.bv d sc sb args (Fl4Spec.toBl cptr.bv) sl exs buffer hcanon] + have B := J.1.1 + have hs : AbsSlot Z.th sl := by + obtain ⟨p, ep, hp⟩ := q.slot + exact ⟨p, ep, Or.inl hp⟩ + obtain ⟨hsc, hpi, hback, -⟩ := B.selectedScBack hs q.capAt + have hS := B.xtop.fin.1.1.1 + obtain ⟨Q, R', hc, hs'⟩ := hS.lists + obtain ⟨hcur, -⟩ := B.curEq q.current + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [q.current, WP.spec_ok, hc.cur] at h; exact h + have hctT : ct ∈ Z.T := hct' ▸ hc.curT + have hct0 : ct ≠ 0#u64 := fun e => (hc.tcbs ct hctT).ne0 (by rw [e]; rfl) + unfold schedcontext.decode + step with P1.obj.Sc.tcb.spec as ⟨i, hi⟩ + rw [scTcbA_eq] at hi + subst hi + refine WP.spec_bind (scStallLocal B sc (fun h => (hback h).1)) ?_ + intro r hr + subst hr + show (sched.cur_thread st s >>= _) ⦃ _ ⦄ + rw [q.current, bind_tc_ok] + have hcase : lbl = fl4_abi.label.SchedContextUnbind ∨ scLabelOf lbl = .invalidInvocation := by + simpa using hl + rcases hcase with l42 | hg + · obtain ⟨y, n, hk, hx⟩ := hs'.scs sc hsc + have hg : Fl4Spec.genInvocationType d = .schedContextUnbind := by + rw [hd, l42]; unfold scLabelOf; simp + have hpre : (Fl4Spec.decodeSchedContextInvocation (Z := Unit) d sc.bv (exs.map Prod.fst)).run σ = (do + let scx ← Fl4Spec.NonDetE.liftE <| Fl4Spec.getSchedContext sc.bv + let t ← Fl4Spec.NonDetE.liftE <| Fl4Spec.NonDet.gets fun s : Fl4Spec.State Unit => s.curThread + Fl4Spec.NonDetE.whenE (scx.scTcb = some t) <| Fl4Spec.NonDetE.throwError .illegalOperation + Fl4Spec.NonDetE.returnOk <| .invokeSchedContextUnbind sc.bv : + Fl4Spec.SEMonad Unit Fl4Spec.SchedContextInvocation).run σ := by + unfold Fl4Spec.decodeSchedContextInvocation; rw [hg] + rw [lE_bind (getSchedContext_eq hk), lE_bind (show Fl4Spec.NonDet.gets + (fun s : Fl4Spec.State Unit => s.curThread) σ = ({(σ.curThread, σ)}, False) from rfl), hcur] at hpre + have l44 : lbl ≠ fl4_abi.label.SchedContextConsumed := lbl_ne l42 (by simp) + have l41 : lbl ≠ fl4_abi.label.SchedContextBind := lbl_ne l42 (by simp) + have l43 : lbl ≠ fl4_abi.label.SchedContextUnbindObject := lbl_ne l42 (by simp) + rw [ite_eq_right l44, ite_eq_right l41, ite_eq_right l43, ite_eq_left l42] + step with P1.obj.Sc.tcb.spec as ⟨k, hkw⟩ + rw [scTcbA_eq] at hkw + subst hkw + by_cases he : L.mem s (scW sc 24#u64) = ct + · rw [ite_eq_left he] + refine illegal_ok s a _ (Or.inl ⟨_, rfl, rfl, ⟨.illegalOperation, ?_, Or.inl trivial⟩, J.anyX arI⟩) + exact run_liftME_err (run_liftME_err (by + rw [hpre]; exact wE_pos (by rw [hx.tcb]; exact (bv_optRef_some hct0).2 he) _ _ σ)) + rw [ite_eq_right he] + have hrun : (Fl4Spec.decodeSchedContextInvocation (Z := Unit) d sc.bv (exs.map Prod.fst)).run σ = + ({(.ok (.invokeSchedContextUnbind sc.bv), σ)}, False) := by + rw [hpre, wE_neg (fun h => he ((bv_optRef_some hct0).1 (hx.tcb ▸ h)))]; rfl + refine WP.spec_mono (J.scUnbindInvokeS3 hni q.current hsc hpi) ?_ + rintro r ⟨hr, σ1, σ2, h1, h2, J'⟩ + refine Or.inr ⟨.base (.invokeSchedContext (.invokeSchedContextUnbind sc.bv)), + (DecOut.of_eq hrun).liftME.liftME, σ1, h1, (.ok [], σ2), ?_, Or.inl ⟨hr, rfl, J'⟩⟩ + change ((.ok [], σ2) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (Fl4Spec.invokeSchedContext (Z := Unit) + (.invokeSchedContextUnbind sc.bv))).run σ1).1 + exact mem_liftE (x := ([], σ2)) (by rw [h2]; exact Set.mem_singleton _) + · have dInvalid : Fl4Spec.genInvocationType d = .invalidInvocation := hd.trans hg + have l44 : lbl ≠ fl4_abi.label.SchedContextConsumed := fun e => by rw [e] at hg; simp [scLabelOf] at hg + have l41 : lbl ≠ fl4_abi.label.SchedContextBind := fun e => by rw [e] at hg; simp [scLabelOf] at hg + have l43 : lbl ≠ fl4_abi.label.SchedContextUnbindObject := fun e => by rw [e] at hg; simp [scLabelOf] at hg + have l42 : lbl ≠ fl4_abi.label.SchedContextUnbind := fun e => by rw [e] at hg; simp [scLabelOf] at hg + have l45 : lbl ≠ fl4_abi.label.SchedContextYieldTo := fun e => by rw [e] at hg; simp [scLabelOf] at hg + rw [ite_eq_right l44, ite_eq_right l41, ite_eq_right l43, ite_eq_right l42, ite_eq_right l45] + refine illegal_ok s a _ (Or.inl ⟨_, rfl, rfl, ⟨.illegalOperation, ?_, Or.inl trivial⟩, J.anyX arI⟩) + apply run_liftME_err + apply run_liftME_err + unfold Fl4Spec.decodeSchedContextInvocation + rw [dInvalid] + rfl + + +set_option maxHeartbeats 4000000 in +/-- Paired decoder, invocation and eager-reply outcomes for the selected SC. +The scheduler footprint is retained at the actual matched state. This +producer uses the native scheduler refinement over the common base relations. -/ +theorem VmEptState.selectedSchedContextReceiptS3 + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hT : TimeAgree arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {ct cptr len buf lbl sc sb : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedContext sc sb) ex args exs) + (blk call don : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedContext sc sb) blk call don false buf ex + ⦃ res => Sched.CPost σ (Fl4Spec.decodeSchedContextInvocation (Z := Unit) lbl.bv sc.bv (exs.map Prod.fst)) s a + (ScQ (fun s' σ' => + ScAll (st := st) Z.N Z.T Z.C Z.R Z.c s σ s' σ' ∧ + ScFine (st := st) Z.N Z.T Z.C Z.c lbl sc ct s σ s' σ') σ ct call) res ⦄ := by + have B := J.1.1 + have hs : AbsSlot Z.th sl := by + obtain ⟨p, ep, hp⟩ := q.slot + exact ⟨p, ep, Or.inl hp⟩ + obtain ⟨hpC, hpi, hback, hbn⟩ := B.selectedScBack hs q.capAt + have hX := B.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + obtain ⟨hcur, xc, hkc, hrun⟩ := B.curRunnable hni q.current + have hcs := ct_schedulable (B.curInv (by rw [hni.2]; simp)) B.act B.inv (hcur ▸ hkc) hrun + rw [hcur] at hcs + have hex := ExtraOK.rel1 hX.rel.1 q.extra + have hcapT : exAt ex 0 ≠ 0#u64 → ∀ p, capAt (L.mem s) (exAt ex 0) = ok (.Thread p) → p ∈ Z.T := by + intro hx p hc + rcases hex with ⟨h0, -⟩ | ⟨-, -, c, a', rest, hc', -, hv, -⟩ + · exact absurd h0 hx + · rw [hc] at hc'; cases eq_of_ok_eq hc' + exact B.thT p hv + have hcapN : exAt ex 0 ≠ 0#u64 → ∀ p b w r, + capAt (L.mem s) (exAt ex 0) = ok (.Notification p b w r) → p ∈ Z.N ∧ p ≠ 0#u64 := by + intro hx p b w r hc + rcases hex with ⟨h0, -⟩ | ⟨-, hS, -⟩ + · exact absurd h0 hx + · obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hH : H p = some .ntfn := ((hJ.typed (exAt ex 0) hS).2.1 p b w r (capV_of_capAt hc)).2 + refine ⟨B.hset.2.1 p hH, fun e => ?_⟩ + rw [e, B.kern.h0] at hH; cases hH + obtain ⟨d, hd, -, hcanon⟩ := sc_canon lbl + rw [syscall.decode_invocation, q.current, bind_ok, hcanon] + simp only [Bool.false_eq_true, ↓reduceIte] + exact sc_decode_refines hT Z.th s a σ hS hR B.nsc B.inv B.yld B.act B.mcp B.c0 B.call B.rp B.rfree + lbl d hd hpC hpi hback hbn q.current hcs call ex (exAt ex 0) (exAt_idx0 ex) _ hex + hcapT hcapN (hT.mb a) hmax htu Ipc.msgOK_holds + +private theorem JAtZB.selectedScStepP1OfFoot {s s' : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {Ac' : Set U64} + (hb : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hf : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s s') : + ScStepP1 (L := L) Z H Cs s s' := by + have hw : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s) := P1.WritesIn.mono hf fun _ h => P1.KW.mono h (Set.empty_subset _) + exact ⟨Ac', J.kw_dbg hw, J.kw_dbgKeep hw, hb⟩ + +/-- A word of the current node is no slot word. -/ +private theorem JAtZB.selectedScNodeNotSlot {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (hb : P1.NodeW st s b) : ¬ SlotW Z.th b := by + obtain ⟨o, ho, -, rfl⟩ := hb + rintro ⟨x, hx, e⟩ + have hfit := hx.fit J.xtop.rel.1 + have hsn := J.slot_node hx J.kern.cpuCs + have hN : P1.NodePlaced st (P1.Heap.tcbs H) s (L.cpu s) := P1.TInv.nodes J.blk _ J.kern.cpuCs + have hv := P1.nodeAt_val hN ho + rcases e with e | e + · have := congrArg UScalar.val e + omega + · have := congrArg UScalar.val e + have h8 : (core.num.U64.wrapping_add x 8#u64).val = x.val + 8 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + omega + +/-- The current thread's FL4 facts at an `Entered` dispatch state: it is +`L.curW s`, one of the TCBs, runnable, and the action word is 0. -/ +private theorem JAtZB.selectedScCurFacts {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) : + ct = L.curW s ∧ ct ∈ Z.T ∧ P1.Runnable (L.mem s (P1.stA (L.curW s))) ∧ L.actionW s = 0#u64 := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hcq.cur] at h; exact h + have ht : ct ∈ Z.T := by rw [hct']; exact hcq.curT + have hcw : ct = L.curW s := by + have h := P1.sched.cur_thread.spec (st := st) s + rw [hct, WP.spec_ok] at h; exact h + obtain ⟨hcur, xc, hkc, hrun⟩ := J.curRunnable hni hct + have hcs := ct_schedulable (J.curInv (by rw [hni.2]; simp)) J.act J.inv (hcur ▸ hkc) hrun + rw [hcur] at hcs + obtain ⟨hrw, -, -, -⟩ := of_decide_eq_true ((schedulable_eq hR hS ht).trans hcs) + have ha0 : L.actionW s = 0#u64 := by + have h := hS.act + rw [hni.2] at h + unfold P1.LawfulStore.actionW; rw [hcq.cur]; exact h + refine ⟨hcw, ht, ?_, ha0⟩ + rw [← hcw, stA_tStA]; exact hrw + +set_option maxHeartbeats 4000000 in +/-- **C9: `ScStepP1` after the SchedContext decode** (Consumed, Bind, +UnbindObject) at an `Entered` `JAtZ` state (module docstring). -/ +private theorem JAtZB.selectedScStepP1 (hA : P0.ArchTotal arI) {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) {lbl length sc sb : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} (sl : Fl4Spec.CslotPtr) (hslotA : AbsSlot Z.th sl) + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.SchedContext sc sb)) (hl : ScJLabel s lbl sc) + {excaps : List Fl4Spec.Cap} (hex : ExtraRel Z.th (L.mem s) (exAt extra 0) excaps) : + syscall.decode_invocation st arI s ar lbl length (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf extra + ⦃ r => ScStepP1 (L := L) Z H Cs s r.2.1 ⦄ := by + obtain ⟨hcw, hctT, hrun, hact⟩ := J.selectedScCurFacts hni hct + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + obtain ⟨hpC, -, hback, -⟩ := J.selectedScBack hslotA hc + have hslot := hslotA.isSlot + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hO := hJ.cobj _ hslot + rw [capV_of_capAt hc] at hO + have hpS : sc ∈ P1.Heap.scs H := ⟨_, hO.1⟩ + have hp0 : sc ≠ 0#u64 := J.c0 sc hpC + have hidle : L.mem s (P1.scTcbA sc) ≠ 0#u64 → L.mem s (P1.scTcbA sc) ≠ L.idleW s := fun h0 e => by + rw [scTcbA_eq] at h0 e + exact (hback h0).2.2 (by rw [e, ← J.idleThread_bv]) + have haff : ∀ t ∈ P1.Heap.tcbs H, L.mem s (P1.affA t) = L.mem s (P1.scCoreA sc) := fun t ht => by + rw [hcq.aff t (hTe ▸ ht), scCoreA_eq, hS.core sc hpC] + have hx : ∀ s', P1.WritesIn st s s' (P1.NodeW st s) → ∀ sl, Aeneas.Std.Array.index_usize extra 0#usize = ok sl → + sl ≠ 0#u64 → obj.Slot.cap st sl s' ⦃ c => P1.ScExtraOK (P1.Heap.tcbs H) (P1.Heap.ntfns H) c ⦄ := by + intro s' hw sl hsl h0 + obtain rfl : exAt extra 0 = sl := eq_of_ok_eq ((exAt_idx0 extra).symm.trans hsl) + rcases hex with ⟨e, -⟩ | ⟨-, hxs, c, a, rest, hcx, -, hcv, -⟩ + · exact absurd e h0 + rw [slot_cap_eq, capAt_of_writes J.xtop.rel.1 hw (fun b hb => J.selectedScNodeNotSlot hb) _ hxs, hcx, WP.spec_ok] + refine ⟨fun p x y z e => ?_, fun p e => ?_⟩ + · subst e + exact J.zH.2.2.2 p (J.capN ⟨_, x, y, z, hxs, capV_of_capAt hcx⟩).1 + · subst e + exact J.zH.1 p (J.thT p hcv) + rw [syscall.decode_invocation, hct, bind_ok] + cases fp + case true => + simp only [↓reduceIte, WP.spec_ok] + exact ⟨Ac, rfl, P1.DbgKeep.refl _, J.blk⟩ + case false => + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_mono (P1.schedcontext.decode.footK hA s ar lbl sc call extra J.blk hl hpS hp0 hidle hrun hact + hx haff) fun r hr => ?_ + obtain ⟨r1, s', a'⟩ := r + obtain ⟨⟨Ac', hb⟩, hf⟩ := hr + exact J.selectedScStepP1OfFoot hb hf + +private theorem JAtZB.selectedScNotZtcb (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {x : U64} (hx : IsSlot Z.th x) + (hnz : ∀ p zt n, capV (L.mem s) x ≠ .Zombie p zt n) {p : U64} + (hp : p.bv ∈ Fl4Spec.objRefs (absCap (capV (L.mem s) x))) : p ∉ ZTcb (L := L) Z.th s := by + rintro ⟨y, hy, zt, n, hz, -⟩ + have hne : x ≠ y := fun e => hnz p zt n (by rw [e, hz]) + exact FinInv.zombie_alone J.xtop.rel.2.2.1 J.mdbL hy hz x hx hne hp + +set_option maxHeartbeats 6000000 in +private theorem schedContextBindLikeStepS3 {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} (s : S) (a : A) (σ : Fl4Spec.State Unit) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) (hA : P0.ArchTotal arI) + (htm : TimeAgree arI) (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) (hmsg : MsgOK st Z.N Z.T Z.C Z.c) + (lbl : U64) (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = scLabelOf lbl) + {sc : U64} (sb : U64) (hsc : sc ∈ Z.C) (hpi : sc.bv ≠ σ.idleSc) + (hl : lbl = fl4_abi.label.SchedContextBind ∨ + lbl = fl4_abi.label.SchedContextUnbindObject) + (hback : L.mem s (scW sc 24#u64) ≠ 0#u64 → + L.mem s (scW sc 24#u64) ∈ Z.T ∧ L.mem s (P1.scA (L.mem s (scW sc 24#u64))) = sc ∧ + (L.mem s (scW sc 24#u64)).bv ≠ σ.idleThread) + (hbn : ∀ y n, σ.kheap sc.bv = some (.schedContext y n) → ∀ m : U64, y.scNtfn = some m.bv → m ∈ Z.N) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hcs : Fl4Spec.schedulable ct.bv σ = true) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraRel Z.th (L.mem s) x0 (excaps.map Prod.fst)) + (length buf : U64) (blk call don fp : Bool) (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) + (sl : Fl4Spec.CslotPtr) (buffer : Option Fl4Spec.ObjRef) + (hslot : capAt (L.mem s) (cslotAddr sl) = ok (.SchedContext sc sb)) (hslotA : AbsSlot Z.th sl) (hsb : sb.val < 64) + (hssz : ∀ y n, σ.kheap sc.bv = some (.schedContext y n) → n + Fl4Spec.minSchedContextBits = sb.val) + (hx0S : x0 ≠ 0#u64 → IsSlot Z.th x0) + (hArch : syscall.decode_invocation st arI s a lbl length (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf extra + ⦃ r => ∀ c, AR a c → AR r.2.2 c ⦄) + : + syscall.decode_invocation st arI s a lbl length (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf extra + ⦃ r => (∃ e e', r = (.Err e, s, a) ∧ + (specDecV fp slbl args ci sl (.SchedContext sc sb) excaps buffer).run σ = ({(.error e', σ)}, False) ∧ + ErrRel e e') ∨ + (r.1 = .Ok () ∧ fp = false ∧ ∃ inv σ0 σ2, + (Fl4Spec.decodeSchedContextInvocation (Z := Unit) slbl sc.bv (excaps.map Prod.fst)).run σ = + ({(.ok inv, σ)}, False) ∧ + Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ0)}, False) ∧ + Fl4Spec.invokeSchedContext (Z := Unit) inv σ0 = ({([], σ2)}, False) ∧ + ∃ Ac' Lst' D', JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' r.2.1 r.2.2 σ2 ∧ + NatFoot (L := L) Z s σ r.2.1 σ2) ⦄ := by + have hres := hni.2 + have hslotS := hslotA.isSlot + have hex0 : ExtraRel Z.th (L.mem s) (exAt extra 0) (excaps.map Prod.fst) := by + have e : x0 = exAt extra 0 := ok_inj (hx0.symm.trans (exAt_idx0 extra)) + rwa [← e] + have hRest := J.selectedScStepP1 hA hni hct (lbl := lbl) (length := length) (blk := blk) + (call := call) (don := don) (fp := fp) (buf := buf) sl hslotA hslot (Or.inr hl) hex0 + have hcapT : x0 ≠ 0#u64 → ∀ p, capAt (L.mem s) x0 = ok (.Thread p) → p ∈ Z.T := by + intro hx p hc + rcases hex with ⟨h0, -⟩ | ⟨-, -, c, a', rest, hc', -, hv, -⟩ + · exact absurd h0 hx + · rw [hc] at hc'; cases eq_of_ok_eq hc' + exact J.thT p hv + have hcapN : x0 ≠ 0#u64 → ∀ p b w r, capAt (L.mem s) x0 = ok (.Notification p b w r) → p ∈ Z.N ∧ p ≠ 0#u64 := by + intro hx p b w r hc + rcases hex with ⟨h0, -⟩ | ⟨-, hS, -⟩ + · exact absurd h0 hx + · obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH : H p = some .ntfn := ((hJ.typed x0 hS).2.1 p b w r (capV_of_capAt hc)).2 + refine ⟨J.hset.2.1 p hH, fun e => ?_⟩ + rw [e, J.kern.h0] at hH; cases hH + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + obtain ⟨Q, R', hc, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hc.cur] at h; exact h + have hcur : σ.curThread = ct.bv := by rw [hs.curThread, hct'] + obtain ⟨xc, -, -, -, hkc, -, hrunc, -⟩ := schedulable_inv hcs + have hctT : ct ∈ Z.T := hct' ▸ hc.curT + have key : syscall.decode_invocation st arI s a lbl length (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf extra + ⦃ res => (fp = true ∧ res = (.Err (.Sys (.InvalidCapability 0#u64)), s, a)) ∨ + (fp = false ∧ Sched.CPost σ (Fl4Spec.decodeSchedContextInvocation (Z := Unit) slbl sc.bv (excaps.map Prod.fst)) s a + (ScQ (fun s' σ' => ScAll (st := st) Z.N Z.T Z.C Z.R Z.c s σ s' σ' ∧ + ScFine (st := st) Z.N Z.T Z.C Z.c lbl sc ct s σ s' σ') σ ct call) res) ⦄ := by + unfold syscall.decode_invocation + rw [hct]; simp only [bind_ok] + cases fp + · simp only [Bool.false_eq_true, ↓reduceIte] + exact WP.spec_mono (sc_decode_refines htm Z.th s a σ hS hR J.nsc J.inv J.yld J.act J.mcp J.c0 J.call J.rp + J.rfree lbl slbl hlbl hsc hpi hback hbn hct hcs call extra x0 hx0 _ hex hcapT hcapN hmb hmax htu hmsg) + fun r h => Or.inr ⟨by trivial, h⟩ + · simp only [↓reduceIte, WP.spec_ok] + exact Or.inl ⟨by trivial, by trivial⟩ + refine WP.spec_mono (WP.spec_and hArch (WP.spec_and key hRest)) ?_ + rintro ⟨r0, s', a'⟩ ⟨hA2, hq, hRst⟩ + rcases hq with ⟨rfl, he⟩ | ⟨rfl, hq⟩ + · cases he + refine Or.inl ⟨_, .invalidCapability 0, rfl, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩ + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + exact run_liftME_err rfl + rcases hq with ⟨e, e', he, hrun, herr⟩ | ⟨inv, hrun, hr1, σ0, data, σ1, σ2, he0, he1, hcase, hall, hfine⟩ + · exact Or.inl ⟨e, e', he, by + rw [scSpecDecV slbl slbl sc sb args ci sl excaps buffer (fun _ _ => rfl)] + exact run_liftME_err (run_liftME_err hrun), herr⟩ + have hshape2 : ((∃ q cap, inv = .invokeSchedContextBind q cap) ∨ + (∃ q cap, inv = .invokeSchedContextUnbindObject q cap)) ∧ + ∀ q t, inv = .invokeSchedContextBind q (.threadCap t) → ScBindOK q t σ := by + rcases hl with l | l + · have hg : Fl4Spec.genInvocationType slbl = .schedContextBind := by rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + obtain ⟨cap, rfl⟩ := (retP_bindDec _ _).of_run hrun + refine ⟨Or.inl ⟨_, _, rfl⟩, fun q t e => ?_⟩ + have := ((rdp_decodeBind _ _ σ _ (by rw [hrun]; exact Set.mem_singleton _)).2 _ rfl) t + cases e; exact this rfl + · have hg : Fl4Spec.genInvocationType slbl = .schedContextUnbindObject := by + rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + obtain ⟨cap, rfl⟩ := (retP_unbindObjDec _ _).of_run hrun + exact ⟨Or.inr ⟨_, _, rfl⟩, fun q t e => by cases e⟩ + have hnil := invokeSc_data_nil hshape2.1 he1 + rcases hcase with ⟨hd, hσ⟩ | ⟨hdn, -⟩ + swap + · exact (hdn hnil).elim + subst hd + subst hσ + have hm0 : ((), σ0) ∈ (Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ).1 := by rw [he0]; exact Set.mem_singleton _ + have hm1 : (([] : List Fl4Spec.Data), σ2) ∈ (Fl4Spec.invokeSchedContext (Z := Unit) inv σ0).1 := by + rw [he1]; exact Set.mem_singleton _ + have hσ0 := the_result (Fl4Proofs.P3.Notif.setThreadState_eq .restart hkc) he0 + have hko : ∀ q, q ≠ ct.bv → σ0.kheap q = σ.kheap q := fun q hq => by + rw [hσ0, stsAct_kheap]; simp [updK, hq] + have hkc0 : σ0.kheap ct.bv = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ0, stsAct_kheap]; simp [updK] + have hK0 : ScTKw (fun p => p = ct.bv) σ σ0 := tk_setThreadState hkc (ipcFree_of_runnable hrunc) ipcFree_restart _ hm0 + have hshape : ((∃ q cap, inv = .invokeSchedContextBind q cap) ∨ (∃ q cap, inv = .invokeSchedContextUnbindObject q cap) ∨ + (∃ q x n, inv = .invokeSchedContextYieldTo q ∧ σ0.kheap q = some (.schedContext x n) ∧ x.scYieldFrom = none)) ∧ + ∀ q t, inv = .invokeSchedContextBind q (.threadCap t) → ScBindOK q t σ := + ⟨hshape2.1.imp id Or.inl, hshape2.2⟩ + have hnY : ∀ p, inv ≠ .invokeSchedContextYieldTo p := by + rcases hshape2.1 with ⟨q, cap, rfl⟩ | ⟨q, cap, rfl⟩ <;> intro p h <;> cases h + obtain ⟨hshape, hbind⟩ := hshape + have hK1 : ScTKw (fun _ => False) σ0 σ2 := tk_invokeSchedContext hshape _ hm1 + -- FL4's writes + have hW : P1.WritesIn st s s' (TFoot st Z.T Z.N Z.C Z.c s σ) := by + rcases hl with l | l + · exact hfine.1 (Or.inl l) + · exact hfine.1 (Or.inr l) + have hX' : XTopAt (L := L) AR Z s' a' σ2 := + xtop_sc_step hX J.ip J.nk hW (hK0.trans (hK1.mono fun _ h => h.elim)) hall.1.1 hall.1.2.1 fun h => hA2 _ h + obtain ⟨⟨-, -, hN', hI'⟩, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := hall + -- `BlockedScActive` + have hKb0 := bk_restart (hcur ▸ hkc) hrunc (hcur ▸ hm0) + have hB0 := J.bsa.bk hKb0 (f1 := true) (pres_setThreadState (f := (true, true)) _ _ σ _ hm0) + have hinv : (∃ q cap, inv = .invokeSchedContextBind q cap) ∨ (∃ q cap, inv = .invokeSchedContextUnbindObject q cap) ∨ + (∃ q, inv = .invokeSchedContextYieldTo q) := by + rcases hshape with h | h | ⟨q, -, -, e, -⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · exact Or.inr (Or.inr ⟨q, e⟩) + have hB' : BlockedScActive σ2 := by + have hu : decide (∀ p, inv ≠ .invokeSchedContextUnbind p) = true := by + rcases hinv with ⟨q, cap, rfl⟩ | ⟨q, cap, rfl⟩ | ⟨q, rfl⟩ <;> simp + exact hB0.bk (bk_invokeSchedContext hm1 hK1 hinv fun q t e => (hbind q t e).bk hKb0) + (f1 := decide (∀ p, inv ≠ .invokeSchedContextYieldTo p)) + ((pres_invokeSchedContext inv σ0 _ hm1).weaken (f' := (decide (∀ p, inv ≠ .invokeSchedContextYieldTo p), true)) + id (fun _ => hu)) + obtain ⟨Ac', hDbg, hKd, hBlk'⟩ := hRst + -- the mid-entry facts: the restart keeps them, the invocation is a `MidK` step + have hC := J.curInv (by rw [hres]; simp) + obtain ⟨hres0, hC0, hO0, hA0, hI0⟩ := restart_mid hcur hkc hcs hres hC J.off J.act J.inv + rw [← hσ0] at hres0 hC0 hO0 hA0 hI0 + have hf0 := stsAct_fields (Fl4Proofs.P3.Notif.updK σ ct.bv (.tcb { xc with tcbState := .restart })) ct.bv + have hct0 : σ0.curThread = ct.bv := by rw [hσ0, hf0.2.1]; exact hcur + have hcs0 : σ0.curSc = σ.curSc := by rw [hσ0, hf0.2.2.1]; rfl + have hid0 : σ0.idleThread = σ.idleThread := by + rw [hσ0]; unfold Fl4Proofs.P3.Notif.stsAct; split_ifs <;> rfl + -- the current SC's thread is the current thread + obtain ⟨xt, hkt, hxs, -⟩ := hC.tcb + obtain ⟨ycs, ncs, hkcs, hycs⟩ := J.inv.back _ xt σ.curSc hkt hxs + -- the SC's bound thread (if any) is not idle (`hback`) + have hnid : ∀ y n t, σ.kheap sc.bv = some (.schedContext y n) → y.scTcb = some t → t ≠ σ.idleThread := by + intro y n t hy ht + obtain ⟨x, n', hk, hx⟩ := hs.scs sc hsc + rw [hk] at hy; cases hy + rw [hx.tcb] at ht + obtain ⟨h0, rfl⟩ := Sched.optRef_some ht + exact (hback h0).2.2 + have hpc : sc.bv ≠ ct.bv := fun e => by + obtain ⟨x, n', hk, -⟩ := hs.scs sc hsc; rw [e, hkc] at hk; cases hk + have hMid : MidK σ0 σ2 := by + rcases hl with l | l + · have hg : Fl4Spec.genInvocationType slbl = .schedContextBind := by rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + have hpre := (rdp_decodeBindPre _ _ σ _ (by rw [hrun]; exact Set.mem_singleton _)).2 _ rfl + obtain ⟨cap, rfl⟩ := (retP_bindDec _ _).of_run hrun + cases cap + case threadCap t => + obtain ⟨⟨y, n, hy, hyt⟩, xt0, hkt0, hxt0⟩ := hpre t rfl + have hP : BindPre sc.bv t σ0 := by + refine ⟨fun e => ?_, fun e => ?_, fun e => ?_⟩ + · rw [hcs0] at e; rw [e, hkcs] at hy; cases hy; rw [hycs] at hyt; cases hyt + · rw [hct0, ← hcur] at e; rw [e, hkt] at hkt0; cases hkt0; rw [hxs] at hxt0; cases hxt0 + · rw [hid0] at e + obtain ⟨xi, hki, hsi, -⟩ := J.act.2 + rw [e, hki] at hkt0; cases hkt0; rw [hsi] at hxt0; cases hxt0 + exact (mka_bind_ret (mka_schedContextBindTcb _ _) _) σ0 hP _ hm1 + case notificationCap n b r => + exact (mka_bind_ret (mka_schedContextBindNtfn _ _) _) σ0 trivial _ hm1 + all_goals exact (mka_fail (P := fun _ => True)) σ0 trivial _ hm1 + · have hg : Fl4Spec.genInvocationType slbl = .schedContextUnbindObject := by + rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + have hpre := (rdp_decodeUnbindObjPre _ _ σ _ (by rw [hrun]; exact Set.mem_singleton _)).2 _ rfl + obtain ⟨cap, rfl⟩ := (retP_unbindObjDec _ _).of_run hrun + cases cap + case threadCap t => + obtain ⟨⟨y, n, hy, hyt⟩, htc⟩ := hpre t rfl + have hP : sc.bv ≠ σ0.curSc ∧ ∀ y' n' t', σ0.kheap sc.bv = some (.schedContext y' n') → y'.scTcb = some t' → + t' ≠ σ0.curThread ∧ t' ≠ σ0.idleThread := by + refine ⟨fun e => ?_, fun y' n' t' hy' ht' => ?_⟩ + · rw [hcs0] at e; rw [e, hkcs] at hy; cases hy; rw [hycs] at hyt; cases hyt; exact htc rfl + · rw [hko _ hpc, hy] at hy'; cases hy'; rw [hyt] at ht'; cases ht' + rw [hct0, ← hcur, hid0]; exact ⟨htc, hnid y n t hy hyt⟩ + exact (mka_bind_ret (mka_schedContextUnbindTcb _) _) σ0 hP _ hm1 + case notificationCap n b r => + exact (mka_bind_ret (mka_schedContextUnbindNtfn _) _) σ0 trivial _ hm1 + all_goals exact (mka_fail (P := fun _ => True)) σ0 trivial _ hm1 + obtain ⟨hC', hOw', hO', hAI'⟩ := hMid.mid (T := Z.T) hres0 hC0 hO0 hA0 hI0 ⟨ct, hctT, hct0.symm⟩ + have hF : FK σ σ2 := ((fk_setThreadState _ _).of_eq he0).trans (fk_invokeSchedContext hshape _ hm1) + have hCR'' : CurRun σ2 := curRun_sc_step hkc hcur he0 hK1 + have hKt := hK0.trans (hK1.mono fun _ h => h.elim) + -- the dead set: the step cuts links only outside Bind's pair, which is covered (its caps) + have hL : LK (invLinkW inv) σ σ2 := (lkm_setThreadState _ _ σ _ hm0).trans (lk_invokeSchedContext hshape _ hm1) + have hP : ∀ p x, p = ct.bv → σ.kheap p = some (.tcb x) → Fl4Spec.runnable x.tcbState = true := by + rintro p x rfl hk; rw [hkc] at hk; cases hk; exact hrunc + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH := J.kern.heap + have scCov : (∀ k, Z.th sc = some k → Covered (L.mem s') Z.G sc.val (sc.val + max (objSize k) 1)) ∧ + (∀ k, H sc = some k → Covered (L.mem s') Z.G sc.val (sc.val + 2 ^ k.bits)) := by + obtain ⟨x, n, hk, -⟩ := hs.scs sc hsc + have hHs := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have hsz := hssz x n hk + have hfit := hH.fits sc _ hHs + simp only [P1.HKind.bits] at hfit + rw [hsz] at hfit + refine J.cover_cap hW hslotS hslot (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_sc sc sb hsb hfit) fun k hk' => ?_ + rw [hHs] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + have hcovW : ∀ q ∈ D, invLinkW inv q.bv → + (∀ k, Z.th q = some k → Covered (L.mem s') Z.G q.val (q.val + max (objSize k) 1)) ∧ + (∀ k, H q = some k → Covered (L.mem s') Z.G q.val (q.val + 2 ^ k.bits)) := by + intro q _ hqW + rcases hl with l | l + · have hg : Fl4Spec.genInvocationType slbl = .schedContextBind := by rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + obtain ⟨a0, rest, hex', rfl⟩ := (retP_bindHead _ _).of_run hrun + rcases hex with ⟨-, he⟩ | ⟨hx0n, -, c, a1, rest', hc, hCR, hCV, he⟩ + · rw [he] at hex'; cases hex' + rw [he] at hex' + injection hex' with ha + subst ha + have hCa := hCR.abs + have hxS := hx0S hx0n + have hqsc : q.bv = sc.bv → q = sc := fun e => bv_inj e + cases a1 with + | threadCap t => + rcases hqW with e | e + · rw [hqsc e]; exact scCov + · cases c <;> simp only [absCap, reduceCtorEq, Fl4Spec.Cap.threadCap.injEq] at hCa + rename_i p + have hq : q = p := bv_inj (e.trans hCa) + subst hq + have hHp : H q = some .tcb := (hJ.tcb q).1 hCV + have hfit := hH.fits q _ hHp + simp only [P1.HKind.bits] at hfit + refine J.cover_cap hW hxS hc (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl (range_thread q hfit) + fun k hk' => ?_ + rw [hHp] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + | notificationCap n b r => + rcases hqW with e | e + · rw [hqsc e]; exact scCov + · cases c <;> simp only [absCap, reduceCtorEq, Fl4Spec.Cap.notificationCap.injEq] at hCa + rename_i p b' x1 x2 + have hq : q = p := bv_inj (e.trans hCa.1) + subst hq + have hty := hJ.typed x0 hxS + rw [capV_of_capAt hc] at hty + have hHp : H q = some .ntfn := (hty.2.1 q b' x1 x2 rfl).2 + have hfit := hH.fits q _ hHp + simp only [P1.HKind.bits] at hfit + refine J.cover_cap hW hxS hc (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_ntfn q b' x1 x2 hfit) fun k hk' => ?_ + rw [hHp] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + | _ => exact hqW.elim + · have hg : Fl4Spec.genInvocationType slbl = .schedContextUnbindObject := by + rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + obtain ⟨cap, rfl⟩ := (retP_unbindObjDec _ _).of_run hrun + exact hqW.elim + -- the yield fields kept (no YieldTo here) + have hYK2 : YKeep σ σ2 := + (((pres_setThreadState (f := (true, true)) _ _ σ _ hm0).weaken (f' := (true, false)) (fun _ => rfl) + (fun h => absurd h (by decide))).trans + ((pres_invokeSchedContext inv σ0 _ hm1).weaken (f' := (true, false)) (fun _ => decide_eq_true hnY) + (fun h => absurd h (by decide)))).ykeep rfl + have hDead : ∀ q ∈ {q | q ∈ D ∧ ¬ invLinkW inv q.bv}, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_step hX' hW hKt hP hL hF hAI'.1 hY' (hYK2.dk q.bv) hq.2 (J.jcov.dead q hq.1) + have hCov : ∀ q ∈ D, q ∉ {q | q ∈ D ∧ ¬ invLinkW inv q.bv} → + (∀ k, Z.th q = some k → Covered (L.mem s') Z.G q.val (q.val + max (objSize k) 1)) ∧ + (∀ k, H q = some k → Covered (L.mem s') Z.G q.val (q.val + 2 ^ k.bits)) := fun q hq hn => + hcovW q hq (by_contra fun h => hn ⟨hq, h⟩) + -- the TCB zombies' TCBs stay in the dead set: Bind's pair is named by the SC cap and the extra cap + have hzW : ∀ p ∈ ZTcb (L := L) Z.th s, ¬ invLinkW inv p.bv := by + intro p hp hqW + have hqsc : p.bv = sc.bv → p = sc := fun e => bv_inj e + rcases hl with l | l + · have hg : Fl4Spec.genInvocationType slbl = .schedContextBind := by rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + obtain ⟨a0, rest, hex', rfl⟩ := (retP_bindHead _ _).of_run hrun + rcases hex with ⟨-, he⟩ | ⟨hx0n, -, c, a1, rest', hc, hCR, hCV, he⟩ + · rw [he] at hex'; cases hex' + rw [he] at hex' + injection hex' with ha + subst ha + have hCa := hCR.abs + have hxS := hx0S hx0n + have hscN : p = sc → False := fun e => J.selectedScNotZtcb hslotS + (fun _ _ _ h => by rw [capV_of_capAt hslot] at h; cases h) + (by rw [capV_of_capAt hslot, e]; simp [absCap, Fl4Spec.objRefs]) hp + cases a1 with + | threadCap t => + rcases hqW with e | e + · exact hscN (hqsc e) + · cases c <;> simp only [absCap, reduceCtorEq, Fl4Spec.Cap.threadCap.injEq] at hCa + rename_i p' + exact J.selectedScNotZtcb hxS (fun _ _ _ h => by rw [capV_of_capAt hc] at h; cases h) + (by rw [capV_of_capAt hc]; show p.bv ∈ Fl4Spec.objRefs (absCap (.Thread p')); rw [e, hCa] + simp [absCap, Fl4Spec.objRefs]) hp + | notificationCap n b r => + rcases hqW with e | e + · exact hscN (hqsc e) + · cases c <;> simp only [absCap, reduceCtorEq, Fl4Spec.Cap.notificationCap.injEq] at hCa + rename_i p' b' x1 x2 + exact J.selectedScNotZtcb hxS (fun _ _ _ h => by rw [capV_of_capAt hc] at h; cases h) + (by rw [capV_of_capAt hc]; show p.bv ∈ Fl4Spec.objRefs (absCap (.Notification p' b' x1 x2)) + rw [e, hCa.1]; simp [absCap, Fl4Spec.objRefs]) hp + | _ => exact hqW.elim + · have hg : Fl4Spec.genInvocationType slbl = .schedContextUnbindObject := by + rw [hlbl, l]; unfold scLabelOf; simp + simp only [Fl4Spec.decodeSchedContextInvocation, hg] at hrun + obtain ⟨cap, rfl⟩ := (retP_unbindObjDec _ _).of_run hrun + exact hqW.elim + have B' := J.sc_frame hW hKt hX' hCR' hRF' hY' hM' (J.sc_buf hW hKt) hRP' (J.sc_apart hW hKt) + (J.sc_jpl hW hKt (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) + (J.sc_jcov hW hDead hCov (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) hKd (fun q hq => hq.1)) + (J.sc_kern hW hKt hF hX' hDbg fun _ h => h.1) + (J.sc_utc hW hKt hDbg) hN' hI' hC' (Or.inl (hC'.elim hOw' (curOwn_of_curInv hX'.fin.1.1.1 hI'))) + hO' hAI' hB' (Or.inr (Or.inl hCR'')) hBlk' hKd (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 + (fun p hp => ⟨J.zD p hp, hzW p hp⟩) + have F0 : NatK σ σ0 := natK_tkw hK0 (by + intro p e y hy + subst p + rw [hkc0] at hy + cases hy + simp) + have F := NatFoot.ofT hX hW (fun _ hb => J.tfoot_nh hb) (F0.trans (natK_tk hK1)) + exact Or.inr ⟨hr1, rfl, inv, σ0, σ2, hrun, he0, he1, Ac', Lst', _, B', F⟩ + + +set_option maxHeartbeats 8000000 in +/-- Bind and UnbindObject at the selected cap. Either first phase rejects +the cap before writing, or the native decoder's scoped scheduler frame +carries the exact paired state to the VM relation. -/ +theorem VmEptState.selectedSchedContextBindLikeS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hA : P0.ArchTotal arI) (hK : ARKeep arI AR) (hT : TimeAgree arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {ct cptr len buf lbl sc sb : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedContext sc sb) ex args exs) + (hl : lbl = fl4_abi.label.SchedContextBind ∨ lbl = fl4_abi.label.SchedContextUnbindObject) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf ex + ⦃ res => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.SchedContext sc sb) exs buffer) s res ⦄ := by + have B := J.1.1 + have hs : AbsSlot Z.th sl := by + obtain ⟨p, ep, hp⟩ := q.slot + exact ⟨p, ep, Or.inl hp⟩ + obtain ⟨hsc, hpi, hback, hbn⟩ := B.selectedScBack hs q.capAt + obtain ⟨⟨y, n, hk, hn⟩, -⟩ := B.scCap arI hs q.capAt + have hsc0 := B.c0 sc hsc + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hHb := (hJ.sc sc y n hsc0 hk).2 + rw [hn] at hHb + have hfit := B.kern.heap.fits sc _ hHb + have hp1 : 1 ≤ sc.val := by + rcases Nat.eq_zero_or_pos sc.val with h | h + · exact absurd (UScalar.eq_of_val_eq (by rw [h]; rfl)) hsc0 + · exact h + have hsb : sb.val < 64 := by + simp only [P1.HKind.bits] at hfit + by_contra hge + have : 2 ^ 64 ≤ 2 ^ sb.val := Nat.pow_le_pow_right (by decide) (by omega) + omega + have hssz : ∀ y n, σ.kheap sc.bv = some (.schedContext y n) → + n + Fl4Spec.minSchedContextBits = sb.val := by + intro y' n' h' + rw [hk] at h' + cases h' + exact hn + obtain ⟨hcur, xc, hkc, hrun⟩ := B.curRunnable hni q.current + have hcs := ct_schedulable (B.curInv (by rw [hni.2]; simp)) B.act B.inv (hcur ▸ hkc) hrun + rw [hcur] at hcs + have hex := ExtraOK.rel1 B.xtop.rel.1 q.extra + have hxs : exAt ex 0 ≠ 0#u64 → IsSlot Z.th (exAt ex 0) := by + intro hnz + rcases hex with ⟨e, -⟩ | ⟨-, hS, -⟩ + · exact (hnz e).elim + · exact hS + obtain ⟨d, hd, hsame, _⟩ := sc_canon lbl + have ed : d = lbl.bv := hsame (by rcases hl with e | e <;> rw [e] <;> simp) + subst d + refine WP.spec_mono (schedContextBindLikeStepS3 s a σ B hni hA hT (hT.mb a) hmax htu + Ipc.msgOK_holds lbl lbl.bv hd sb hsc hpi hl hback hbn q.current hcs ex _ (exAt_idx0 ex) exs hex + len buf blk call don fp args (Fl4Spec.toBl cptr.bv) sl buffer q.capAt hs hsb hssz hxs + (decode_sched_context_keepAR L.total hA hK)) ?_ + intro r hr + rcases hr with ⟨e, e', hr, he, hErr⟩ | ⟨hr, rfl, inv, σ1, σ2, he, hs1, hp, Ac2, Lst2, D2, B2, F2⟩ + · subst r + exact Or.inl ⟨e, rfl, rfl, ⟨e', he, hErr⟩, J.anyX arI⟩ + · rw [scSpecDecV lbl.bv lbl.bv sc sb args (Fl4Spec.toBl cptr.bv) sl exs buffer (fun _ _ => rfl)] + refine Or.inr ⟨.base (.invokeSchedContext inv), (DecOut.of_eq he).liftME.liftME, σ1, + by rw [hcur]; exact hs1, (.ok [], σ2), ?_, + Or.inl ⟨hr, rfl, (J.ofNat (V' := Z.vs) B2 F2).anyX arI⟩⟩ + change ((.ok [], σ2) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (Fl4Spec.invokeSchedContext (Z := Unit) inv)).run σ1).1 + exact mem_liftE (x := ([], σ2)) (by rw [hp]; exact Set.mem_singleton _) + + +set_option maxHeartbeats 16000000 in +/-- Consumed at the selected SC cap. The returned time words and the +conditional eager reply are taken from the actual matched execution. -/ +theorem VmEptState.selectedSchedContextConsumedS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {ct cptr len buf lbl sc sb : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedContext sc sb) ex args exs) + (h44 : lbl = fl4_abi.label.SchedContextConsumed) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf ex + ⦃ res => VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.SchedContext sc sb) exs buffer) ct s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases fp + case true => + simp only [↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + exact run_liftME_err rfl + case false => + simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨d, hd, -, hcanon⟩ := sc_canon lbl + rw [scSpecDecV lbl.bv d sc sb args (Fl4Spec.toBl cptr.bv) sl exs buffer hcanon] + have B := J.1.1 + have hs : AbsSlot Z.th sl := by + obtain ⟨p, ep, hp⟩ := q.slot + exact ⟨p, ep, Or.inl hp⟩ + obtain ⟨hsc, -, hback, -⟩ := B.selectedScBack hs q.capAt + have hS := B.xtop.fin.1.1.1 + obtain ⟨Q, R', hc, hs'⟩ := hS.lists + have hct' : ct = nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [q.current, WP.spec_ok, hc.cur] at h; exact h + have hctT : ct ∈ Z.T := hct' ▸ hc.curT + have hcW : ct = L.curW s := by + have h := P1.sched.cur_thread.spec (st := st) s + rw [q.current, WP.spec_ok] at h + exact h + have hg : Fl4Spec.genInvocationType d = .schedContextConsumed := by + rw [hd, h44]; unfold scLabelOf; simp + have hdec : + (Fl4Spec.decodeSchedContextInvocation (Z := Unit) d sc.bv (exs.map Prod.fst)).run σ = + ({(.ok (.invokeSchedContextConsumed sc.bv), σ)}, False) := by + unfold Fl4Spec.decodeSchedContextInvocation + rw [hg] + rfl + obtain ⟨s1, σ1, Lst1, x, _, _, hs1, he0, J1, eσ1, _, _, _⟩ := J.restartK hni + have hs1' : sched.set_thread_state st s ct sched.TS_RESTART = ok s1 := hcW.symm ▸ hs1 + have hk1 : σ1.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + rw [eσ1, ← hcW, stsAct_kheap] + simp [updK] + have B1 := J1.1.1 + have hS1 := B1.xtop.fin.1.1.1 + obtain ⟨Q1, R1, hc1, hs1S⟩ := hS1.lists + obtain ⟨z, n, hkz, -⟩ := hs1S.scs sc hsc + obtain ⟨c, v, hv⟩ := updConsumed_one hkz + let σu := updSc σ1 sc.bv (Fl4Spec.scConsumedUpdate (fun _ => v) z) n + have hsct : ct.bv ≠ sc.bv := fun e => by rw [e, hkz] at hk1; cases hk1 + have hku : σu.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + simp only [σu, updSc, updK, Function.update_of_ne hsct] + exact hk1 + have hrc : Fl4Spec.returnConsumed (Z := Unit) sc.bv σ1 = ({(Fl4Spec.wordsFromTime c, σu)}, False) := by + unfold Fl4Spec.returnConsumed + rw [nd_bind_one hv] + rfl + have hsetc : Fl4Spec.setConsumed (Z := Unit) sc.bv ct.bv σ1 = + Fl4Spec.replyFromKernel (Z := Unit) ct.bv (0, Fl4Spec.wordsFromTime c) σu := by + unfold Fl4Spec.setConsumed + rw [nd_bind_one hv] + have hSC : schedcontext.set_consumed st arI s1 a sc ct call ⦃ s3 => ∃ σ2, + Fl4Spec.NonDet.when (call = true) (Fl4Spec.replyFromKernel (Z := Unit) ct.bv (0, Fl4Spec.wordsFromTime c)) σu = + ({((), σ2)}, False) ∧ + ∃ y, σ2.kheap ct.bv = some (.tcb y) ∧ y.tcbState = .restart ∧ + ∃ (W : XVs), VmEptState (L := L) arI V X XL AR (Z.mv Z.G W) H Cs Ac Lst1 D s3 a σ2 ⦄ := by + cases call + case false => + unfold schedcontext.set_consumed + refine WP.spec_bind (B1.updConsumedN hAT hmax htu hsc hctT) ?_ + rintro ⟨cv, s3⟩ ⟨σ2, c', he2, W, B2, F2⟩ + simp only [Bool.false_eq_true, ↓reduceIte] + rw [hv] at he2 + obtain ⟨-, e⟩ := Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst he2)) + have e' : σu = σ2 := e + subst σ2 + refine (WP.spec_ok _).2 ⟨σu, ?_, _, hku, rfl, W, J1.ofNat B2 F2⟩ + unfold Fl4Spec.NonDet.when + rw [ite_eq_right (by simp)] + rfl + case true => + refine WP.spec_mono (B1.setConsumedJV hAT hmax htu hsc hctT) ?_ + rintro s3 ⟨σ2, he2, hy, hQ, hw, B2⟩ + have F2 := NatFoot.ofW B1.xtop hw (fun _ h => Ipc.FinW.oobj hS1 h) + (fun _ h => Ipc.FinW.nv hS1 h) (fun _ h => B1.finW_nh h) (natK_qky hQ) + obtain ⟨v', w', hshape⟩ := Sched.setConsumed_shape hkz hk1 + have eσ2 := the_result hshape he2 + refine ⟨σ2, ?_, Endp.replyTcb { x with tcbState := .restart } w', + by rw [eσ2]; simp [updK], rfl, Z.vs, J1.ofNat (V' := Z.vs) B2 F2⟩ + unfold Fl4Spec.NonDet.when + rw [ite_eq_left rfl, ← hsetc] + exact he2 + unfold schedcontext.decode + step with P1.obj.Sc.tcb.spec as ⟨i, hi⟩ + rw [scTcbA_eq] at hi + subst hi + refine WP.spec_bind (scStallLocal B sc (fun h => (hback h).1)) ?_ + intro r hr + subst hr + show (sched.cur_thread st s >>= _) ⦃ _ ⦄ + rw [q.current, bind_tc_ok, ite_eq_left h44, hs1'] + simp only [bind_ok] + refine WP.spec_bind hSC ?_ + rintro s3 ⟨σ2, hrf, y, hk2, hrs2, W, J2⟩ + refine WP.spec_bind (J2.1.1.runJ hctT hk2 hrs2) ?_ + rintro s4 ⟨Lst4, B4, F4⟩ + rw [WP.spec_ok] + let σ4 := stsAct (updK σ2 ct.bv (.tcb { y with tcbState := .running })) ct.bv + have hk4 : σ4.kheap ct.bv = some (.tcb { y with tcbState := .running }) := by + simp [σ4, stsAct_kheap, updK] + have hNR := B4.notRestart hctT hk4 (by intro h; cases h) + have J4 := J2.ofNat (V' := (Z.mv Z.G W).vs) B4 F4 + refine Or.inr ⟨rfl, hNR, .base (.invokeSchedContext (.invokeSchedContextConsumed sc.bv)), + (DecOut.of_eq hdec).liftME.liftME, σ1, he0, Fl4Spec.wordsFromTime c, σu, ?_, + ((), σ4), ?_, B4.xtop.relTop, J4.anyX arI⟩ + · change ((.ok (Fl4Spec.wordsFromTime c), σu) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (Fl4Spec.returnConsumed (Z := Unit) sc.bv)).run σ1).1 + exact mem_liftE (x := (Fl4Spec.wordsFromTime c, σu)) (by rw [hrc]; exact Set.mem_singleton _) + · rw [specTail_of_restart hku rfl, nd_bind_one hrf, Notif.setThreadState_eq .running hk2] + exact Set.mem_singleton _ + + +private theorem JAtZB.selectedScSchedHead {Ac D : Set U64} {Lst : U64 → List U64} + {s : S} {σ : Fl4Spec.State Unit} (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hw : SchW (L := L) Z.T Z.c s b) : b ≠ P1.vHeadA st s := by + intro e + subst b + have hhead : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val B.kern.ksbFit + rcases hw with ⟨t, ht, hw⟩ | ⟨hb, -, -⟩ | ⟨k, hk, hv⟩ + · have hd := B.kern.hks t _ (B.zH.1 t ht) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at hd + have hf := tfield3_val (B.ip.e.tcb t ht) hw + rw [hhead] at hf + rcases hf with h | h | h <;> omega + · have hbase : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + omega + · simp only [List.mem_cons, List.not_mem_nil, or_false] at hk + rcases hk with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omega + +private theorem JAtZB.selectedScSchedStep + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (h : SchedStep (L := L) Z.th s σ s' σ') : + ∃ Ac' Lst' D', JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' := by + have hX := J.xtop + have hw := (h.2.1 Z.N Z.T Z.C Z.c J.thT hX.fin.1.1.1 hX.fin.1.1.2.2.1).2.1 + obtain ⟨Ac', Lst', D', B'⟩ := J.schedJ h + exact ⟨Ac', Lst', D', B', NatFoot.ofW hX hw + (fun _ hb => schW_oobj hX.fin.1.1.1.placed.tcb hb) + (fun _ hb => J.schW_nv hb) (fun _ hb => J.selectedScSchedHead hb) (NatK.of_kheap h.1.kh)⟩ + +set_option maxHeartbeats 4000000 in +/-- **`dequeue` at `J`** (DomSched's `dequeue_schedStep`, a scheduler step: +`JAtZ.schedJ`). -/ +private theorem JAtZB.scDequeueS3 (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) : + sched.dequeue st s t ⦃ s' => ∃ σ', Fl4Spec.tcbSchedAction (Z := Unit) Fl4Spec.tcbSchedDequeue t.bv σ = + ({((), σ')}, False) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' ⦄ := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨x, hk, -⟩ := hs.tcb t ht + refine WP.spec_mono (dequeue_schedStep (J.thTcb ht) hk hS ht) ?_ + rintro s' ⟨-, -, -, hSS⟩ + exact ⟨_, tcbSchedAction_gen hk _, J.selectedScSchedStep hSS⟩ + +set_option maxHeartbeats 4000000 in +/-- **`enqueue` of a schedulable thread at `J`** (DomSched's +`enqueue_schedStep`; `JAtZ.schedJ`). -/ +private theorem JAtZB.scEnqueueS3 (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (hsch : Fl4Spec.schedulable t.bv σ = true) : + sched.enqueue st s t ⦃ s' => ∃ σ', Fl4Spec.tcbSchedAction (Z := Unit) Fl4Spec.tcbSchedEnqueue t.bv σ = + ({((), σ')}, False) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' ⦄ := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨x, hk, -⟩ := hs.tcb t ht + have hd := (schedulable_eq hR hS ht).trans hsch + obtain ⟨hrw, hbd, hmx, hrl⟩ := of_decide_eq_true hd + have hrun : P1.Runnable (L.mem s (P1.stA t)) := by rw [stA_tStA]; exact hrw + refine WP.spec_mono (enqueue_schedStep (J.thTcb ht) hk hS ht (fun _ => hrl) hsch hrun hbd hrl + (fun e => by rw [e] at hmx; simp at hmx)) ?_ + rintro s' ⟨-, -, -, hSS⟩ + exact ⟨_, tcbSchedAction_gen hk _, J.selectedScSchedStep hSS⟩ + +set_option maxHeartbeats 8000000 in +/-- **The yield links at `J`** (`yield_switches`' `set_yield_to` and +`set_yield_from` of a switching YieldTo; as `JAtZ.cancelYieldJ` with the +links set: `YieldRel.pair`, P1's `yield_link.blk`). -/ +private theorem JAtZB.scLinkYieldS3 (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {ct sc : U64} (hct : ct ∈ Z.T) + (hsc : sc ∈ Z.C) (hy0 : L.mem s (yieldA ct) = 0#u64) {xc : Fl4Spec.Tcb} + (hkc : σ.kheap ct.bv = some (.tcb xc)) {y : Fl4Spec.SchedContext} {n : Nat} + (hks : σ.kheap sc.bv = some (.schedContext y n)) (hyf : y.scYieldFrom = none) + (hrun : P1.Runnable (L.mem s (P1.stA ct))) (htw : L.mem s (P1.scTcbA sc) ≠ 0#u64) : + let s' := L.wr (L.wr s (yieldA ct) sc) (scYieldFromA sc) ct + let σ' := updSc (updK σ ct.bv (.tcb { xc with tcbYieldTo := some sc.bv })) sc.bv + { y with scYieldFrom := some ct.bv } n + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' + s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hT := hc.tcbs + have hct0 : ct ≠ 0#u64 := fun e => (hc.tcbs ct hct).ne0 (by rw [e]; rfl) + have hsc0 : sc ≠ 0#u64 := J.c0 sc hsc + obtain ⟨x', n', hk', hx⟩ := hs.scs sc hsc + rw [hks] at hk'; cases hk' + have hsw56 := sw_hdr hS hsc (K := 56#u64) (by simp) (by simp) (by decide) + have hal56 : Aligned (scW sc 56#u64) := (hx.hdr (K := 56#u64) (by simp) (by simp)).1 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hscS : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc y n hsc0 hks).2⟩ + have hctH : ct ∈ P1.Heap.tcbs H := J.zH.1 ct hct + have hblk := (P1.yield_link.blk J.blk hctH hscS).1 + -- the spec side + obtain ⟨hS1, hf1, hK1⟩ := hS.set_yt hct hkc sc + rw [Notif.optRef_ne hsc0] at hS1 hK1 + have hpc' : sc.bv ≠ ct.bv := fun e => by rw [e, hkc] at hks; cases hks + have hka : (updK σ ct.bv (.tcb { xc with tcbYieldTo := some sc.bv })).kheap sc.bv = + some (.schedContext y n) := by simp [updK, hpc', hks] + obtain ⟨hS2, hf2, hK2, hw2⟩ := hS1.set_sc_yf hsc hka ct + rw [Notif.optRef_ne hct0] at hS2 hK2 + have hR2 := (hR.frame hS.placed hf1 hK1).frame hS1.placed hf2 hK2 + have hN2 := (J.nsc.updTcb (t := ct.bv) (x := { xc with tcbYieldTo := some sc.bv })).updSc + (p := sc.bv) (y := { y with scYieldFrom := some ct.bv }) (n := n) + have hI2 := (J.inv.ytUpd hkc (some sc.bv)).scFields (y' := { y with scYieldFrom := some ct.bv }) hka rfl rfl rfl rfl + have hY2 := J.yld.pair J.c0 hct hsc hy0 hkc hks hyf + (m' := L.mem (L.wr (L.wr s (yieldA ct) sc) (scYieldFromA sc) ct)) + (fun u hu hne => by + rw [WritesIn.yield_keep hT hw2 u hu, L.mem_wr_ne (yieldA_aligned hT hct) (yieldA_aligned hT hu) + (fun e => hne (yieldA_inj hT hct hu (UScalar.eq_of_val_eq e).symm))]) + (by rw [WritesIn.yield_keep hT hw2 ct hct]; exact L.mem_wr_same (yieldA_aligned hT hct)) + set σ' := updSc (updK σ ct.bv (.tcb { xc with tcbYieldTo := some sc.bv })) sc.bv + { y with scYieldFrom := some ct.bv } n with hσ' + have hm : ((), σ') ∈ (yieldLink ct.bv sc.bv σ).1 := by + rw [yieldLink_eq hkc hks hpc']; exact Set.mem_singleton _ + have hK : ScTKw (fun _ => False) σ σ' := nkt_yieldLink ct.bv sc.bv σ _ hm + have hsok : SOK (false, true) σ σ' := pres_yieldLink ct.bv sc.bv σ _ hm + have hA : QKY σ σ' := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun q => ?_⟩ + by_cases e1 : q = ct.bv + · subst e1; exact Or.inr (Or.inl ⟨xc, { xc with tcbYieldTo := some sc.bv }, hkc, by simp [σ', updK, Ne.symm hpc'], rfl⟩) + by_cases e2 : q = sc.bv + · subst e2; exact Or.inr (Or.inr ⟨y, { y with scYieldFrom := some ct.bv }, n, hks, by simp [σ', updK], rfl⟩) + · exact Or.inl (by simp [σ', updK, e1, e2]) + -- FL4's writes + have hal' : Aligned (scYieldFromA sc) := by rw [scYieldFromA_eq]; exact hal56 + have hW : P1.WritesIn st s (L.wr (L.wr s (yieldA ct) sc) (scYieldFromA sc) ct) (TFoot st Z.T Z.N Z.C Z.c s σ) := + ((writesIn_wr (st := st) (s := s) (v := sc) (yieldA_aligned hT hct)).trans + (writesIn_wr (st := st) (s := L.wr s (yieldA ct) sc) (v := ct) hal')).mono fun b hb => by + rcases hb with rfl | rfl + · exact Or.inr ⟨ct, hct, rfl⟩ + · exact Or.inl (KFoot.of_sw (by rw [scYieldFromA_eq]; exact hsw56)) + have hKW : P1.WritesIn st s (L.wr (L.wr s (yieldA ct) sc) (scYieldFromA sc) ct) + (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s) := + ((writesIn_wr (st := st) (s := s) (v := sc) (yieldA_aligned hT hct)).trans + (writesIn_wr (st := st) (s := L.wr s (yieldA ct) sc) (v := ct) hal')).mono fun b hb => by + rcases hb with rfl | rfl + · exact P1.kw_field (K := 3472#u64) hctH (by simp) (by simp) (by simp) + · obtain ⟨h128, -, -, -, -, -, -, e56⟩ := P1.sc_facts J.blk.2.2.1.1 hscS + have e : (scYieldFromA sc).val = sc.val + 56 := e56 + exact Or.inr (Or.inr (Or.inr (Or.inl ⟨sc, hscS, by rw [e]; omega, by rw [e]; omega⟩))) + have hDbg := J.kw_dbg hKW + have hKd := J.kw_dbgKeep hKW + have hX' : XTopAt (L := L) AR Z _ a σ' := + xtop_sc_step (ct := σ.curThread) J.xtop J.ip J.nk hW (hK.mono fun _ h => h.elim) hS2 hR2 id + have hall := ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hS2, hR2, hN2, hI2⟩ hY2 (hW.mono fun b hb => Or.inl hb) + hsok rfl + obtain ⟨-, -, hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := hall + have hAI := hA.actIdle J.act + -- the dead stay dead: the caller (runnable) and the SC (with a thread) are not dead + have hdead : ∀ q ∈ D, DeadP (L := L) H (L.wr (L.wr s (yieldA ct) sc) (scYieldFromA sc) ct) q := fun q hq => by + have hd := J.jcov.dead q hq + have hqc : q.bv ≠ ct.bv := fun e => by + rw [P1.eq_of_bv e] at hd + exact P1.runnable_ne_inactive hrun (hd.1 (J.zH.1 ct hct)).1 + have hqs : q.bv ≠ sc.bv := fun e => by + rw [P1.eq_of_bv e] at hd + exact htw (hd.2.2.2.2 _ (hJ.sc sc y n hsc0 hks).2).1 + exact J.deadP_sc_step hX' hW hK (fun _ _ h => h.elim) (hA.lk (W := fun _ => False)) hA.fk hAI.1 hY2 + (YDeadKeep.of_at (by simp [σ', updK, hqc, hqs])) (fun h => h) hd + refine ⟨Ac, Lst', D, J.sc_frame hW hK hX' hCR' hRF' hY2 hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hA.fk.2.1 hA.fk.2.2).2) (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hA.fk.2.1 hA.fk.2.2) hKd subset_rfl) + (J.sc_kern hW hK hA.fk hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hN2 hI2 (hA.cur J.cur) (hA.own J.own) (hA.off J.off) hAI (hA.bsa J.bsa) + (hA.curRunM J.curRun) hblk hKd (J.idleW_keep hX'.fin.1.1.1 hA.fk.2.1 hA.fk.2.2).1 J.zD, ?_⟩ + exact NatFoot.ofT J.xtop hW (fun _ h => J.tfoot_nh h) (natK_tk hK) + +set_option maxHeartbeats 16000000 in +/-- **`yield_switches` at `J`** (ScYieldTo's `yield_switches_refines` with +`JAtZ` carried): the SC's thread not schedulable (nothing), of lower +priority than the caller (requeued: `JAtZ.scDequeueS3`, `JAtZ.scEnqueueS3`), or +else the yield links (`JAtZ.scLinkYieldS3`), the requeues and the reschedule +(`JAtZ.reschedJ`); the spec's `return_now` computation (`yieldSwitch`) +returns the negation. Without a switch the heap is kept. -/ +private theorem JAtZB.scYieldSwitchesS3 (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {sc : U64} (hsc : sc ∈ Z.C) {x : Fl4Spec.SchedContext} {n : Nat} (hk : σ.kheap sc.bv = some (.schedContext x n)) + (hyf : x.scYieldFrom = none) {t : U64} (ht : t ∈ Z.T) (htc : x.scTcb = some t.bv) + {ct : U64} (hct : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hyc : L.mem s (yieldA ct) = 0#u64) + (hcs : Fl4Spec.schedulable ct.bv σ = true) : + schedcontext.yield_switches st s sc ct ⦃ r => ∃ σ', yieldSwitch sc.bv σ = ({(!r.1, σ')}, False) ∧ + (r.1 = false → σ'.kheap = σ.kheap) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' r.2 a σ' ∧ NatFoot (L := L) Z s σ r.2 σ' ⦄ := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + obtain ⟨x', n', hk', hx⟩ := hs.scs sc hsc + rw [hk] at hk'; cases hk' + obtain ⟨xt, hkt, -, -, -, hpt⟩ := hs.tcb t ht + obtain ⟨xc, hkc, -, -, -, hpc⟩ := hs.tcb ct hct + have htw := word24_of hS hsc hk htc + have hpre : ∀ (k : Fl4Spec.ObjRef → Bool → Fl4Spec.SMonad Unit Bool), + (Fl4Spec.getScObjRef Fl4Spec.SchedContext.scTcb sc.bv >>= fun scTcbOpt => + Fl4Spec.NonDet.assertOpt scTcbOpt >>= fun tcbPtr => + Fl4Spec.NonDet.gets (Fl4Spec.schedulable tcbPtr) >>= fun b => k tcbPtr b) σ = + k t.bv (Fl4Spec.schedulable t.bv σ) σ := by + intro k + rw [nd_bind_one (getScObjRef_eq hk _), htc, + nd_bind_one (show Fl4Spec.NonDet.assertOpt (σ := Fl4Spec.State Unit) (some t.bv) σ = ({(t.bv, σ)}, False) + from rfl), nd_gets_bind] + unfold schedcontext.yield_switches + step with obj.Sc.tcb.spec as ⟨i, hi⟩ + rw [scTcbA_eq, htw] at hi + subst hi + step with Sched.sched.is_schedulable.spec' as ⟨b, hb⟩ + rw [schedulable_eq hR hS ht] at hb + subst hb + by_cases hsb : Fl4Spec.schedulable i.bv σ = true + swap + · simp only [hsb, Bool.false_eq_true, ↓reduceIte] + refine (WP.spec_ok _).mpr ⟨σ, ?_, fun _ => rfl, Ac, Lst, D, J, NatFoot.refl Z s σ⟩ + unfold yieldSwitch + rw [hpre] + simp only [hsb, Bool.false_eq_true, ↓reduceIte]; rfl + simp only [hsb, ↓reduceIte] + step with obj.Sc.cpu.spec as ⟨i1, hi1⟩ + have hi1' : i1 = Z.c := by rw [hi1, scCoreA_eq]; exact hS.core sc hsc + subst hi1' + step with LawfulStore.cur_cpu_spec as ⟨i2, hi2⟩ + rw [hc.cur] at hi2 + subst hi2 + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.prio.spec as ⟨i3, hi3⟩ + step with obj.Tcb.prio.spec as ⟨i4, hi4⟩ + subst hi3 hi4 + by_cases hlt : L.mem s (prioA i) < L.mem s (prioA ct) + · simp only [hlt, ↓reduceIte] + refine WP.spec_bind (J.scDequeueS3 ht) ?_ + rintro s1 ⟨σ1, he1, Ac1, Lst1, D1, J1, F1⟩ + obtain ⟨hk1, hr1, -⟩ := tsa_keep hkt he1 + have hsb1 : Fl4Spec.schedulable i.bv σ1 = true := by rw [schedulable_heap hk1 hr1]; exact hsb + refine WP.spec_bind (J1.scEnqueueS3 ht hsb1) ?_ + rintro s2 ⟨σ2, he2, Ac2, Lst2, D2, J2, F2⟩ + obtain ⟨hk2, -, -⟩ := tsa_keep (show σ1.kheap i.bv = some (.tcb xt) by rw [hk1]; exact hkt) he2 + refine (WP.spec_ok _).mpr ⟨σ2, ?_, fun _ => hk2.trans hk1, Ac2, Lst2, D2, J2, J.natTrans F1 F2⟩ + unfold yieldSwitch + rw [hpre] + simp only [hsb, ↓reduceIte] + rw [nd_gets_bind, hcur, nd_bind_one (threadGet_eq _ hkt), nd_bind_one (threadGet_eq _ hkc), + ite_eq_left ((prio_lt_iff hpt hpc).1 hlt), nd_bind_one he1, nd_bind_one he2]; rfl + · simp only [hlt, ↓reduceIte] + refine WP.spec_bind (obj.Tcb.set_yield_to.spec s ct sc) ?_ + rintro s1 rfl + refine WP.spec_bind (obj.Sc.set_yield_from.spec _ sc ct) ?_ + rintro s2 rfl + have hpc' : sc.bv ≠ ct.bv := fun e => by rw [e, hkc] at hk; cases hk + have hka : (updK σ ct.bv (.tcb { xc with tcbYieldTo := some sc.bv })).kheap sc.bv = + some (.schedContext x n) := by simp [updK, hpc', hk] + have hsf := (SFrame.ytUpd hkc (some sc.bv)).trans + (SFrame.updSc (x' := { x with scYieldFrom := some ct.bv }) hka rfl rfl) + have hrunc : P1.Runnable (L.mem s (P1.stA ct)) := by + obtain ⟨hrw, -, -, -⟩ := of_decide_eq_true ((schedulable_eq hR hS hct).trans hcs) + rw [stA_tStA]; exact hrw + obtain ⟨AcL, LstL, DL, JL, FL⟩ := J.scLinkYieldS3 hct hsc hyc hkc hk hyf hrunc + (by rw [scTcbA_eq, htw]; exact fun e => (hc.tcbs i ht).ne0 (by rw [e]; rfl)) + obtain ⟨xtL, hktL, -⟩ := hsf.tcb _ xt hkt + obtain ⟨xcL, hkcL, -⟩ := hsf.tcb _ xc hkc + refine WP.spec_bind (JL.scDequeueS3 ht) ?_ + rintro s3 ⟨σ3, he3, Ac3, Lst3, D3, J3, F3⟩ + obtain ⟨hk3, hr3, -⟩ := tsa_keep hktL he3 + have hcs3 : Fl4Spec.schedulable ct.bv σ3 = true := by + rw [schedulable_heap hk3 hr3]; exact hsf.schedulable hcs + refine WP.spec_bind (J3.scEnqueueS3 hct hcs3) ?_ + rintro s4 ⟨σ4, he4, Ac4, Lst4, D4, J4, F4⟩ + obtain ⟨hk4, hr4, -⟩ := tsa_keep (show σ3.kheap ct.bv = some (.tcb xcL) by rw [hk3]; exact hkcL) he4 + have hsb4 : Fl4Spec.schedulable i.bv σ4 = true := by + rw [schedulable_heap hk4 hr4, schedulable_heap hk3 hr3]; exact hsf.schedulable hsb + refine WP.spec_bind (J4.scEnqueueS3 ht hsb4) ?_ + rintro s5 ⟨σ5, he5, Ac5, Lst5, D5, J5, F5⟩ + obtain ⟨s6, hr6, σ6, he6, Ac6, Lst6, D6, J6, F6⟩ := J5.reschedJ + rw [hr6, bind_ok] + refine (WP.spec_ok _).mpr ⟨σ6, ?_, fun h => absurd h (by simp), Ac6, Lst6, D6, J6, J.natTrans FL (JL.natTrans F3 (J3.natTrans F4 (J4.natTrans F5 F6)))⟩ + unfold yieldSwitch + rw [hpre] + simp only [hsb, ↓reduceIte] + rw [nd_gets_bind, hcur, nd_bind_one (threadGet_eq _ hkt), nd_bind_one (threadGet_eq _ hkc), + ite_eq_right (fun h => hlt ((prio_lt_iff hpt hpc).2 h))] + show (Fl4Spec.threadSet _ ct.bv >>= _) σ = _ + rw [nd_bind_one (threadSet_eq _ hkc)] + show (Fl4Spec.updateSchedContext sc.bv _ >>= _) (updK σ ct.bv (.tcb { xc with tcbYieldTo := some sc.bv })) = _ + rw [nd_bind_one (updateSchedContext_eq hka _)] + show (Fl4Spec.tcbSchedAction (Z := Unit) Fl4Spec.tcbSchedDequeue i.bv >>= _) + (updSc (updK σ ct.bv (.tcb { xc with tcbYieldTo := some sc.bv })) sc.bv + { x with scYieldFrom := some ct.bv } n) = _ + rw [nd_bind_one he3, nd_bind_one he4, nd_bind_one he5, nd_bind_one he6]; rfl + +/-- Completing an existing yield leaves the architecture state unchanged. -/ +private theorem scYieldCompletionArch (s : S) (a : A) (hAT : P0.ArchTotal arI) (y : U64) : + schedcontext.complete_yield_to st arI s a y ⦃ r => r.2 = a ⦄ := by + unfold schedcontext.complete_yield_to + refine WP.spec_bind (P0.obj.Tcb.is_null.p0 y) ?_ + intro b _ + split + · exact (WP.spec_ok _).mpr rfl + · refine WP.spec_bind (P0.obj.Tcb.yield_to.p0 st y s L.total) ?_ + intro i _ + split + · refine WP.spec_bind (P0.schedcontext.set_consumed.p0 st arI s a i y true L.total hAT) ?_ + intro s1 _ + refine WP.spec_bind (P0.schedcontext.cancel_yield_to.p0 st s1 y L.total) ?_ + intro s2 _ + exact (WP.spec_ok _).mpr rfl + · exact (WP.spec_ok _).mpr rfl + + +set_option maxHeartbeats 16000000 in +private theorem VmEptState.scYieldToS3 [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {sc : U64} (hsc : sc ∈ Z.C) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.SchedContext} {n : Nat} (hk : σ.kheap sc.bv = some (.schedContext x n)) (htc : x.scTcb = some t.bv) + {ct : U64} (hct : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) + {xc : Fl4Spec.Tcb} (hkc : σ.kheap ct.bv = some (.tcb xc)) (hrs : xc.tcbState = .restart) + (hycn : xc.tcbYieldTo = none) (hcs : Fl4Spec.schedulable ct.bv σ = true) (call : Bool) : + schedcontext.yield_to st arI s a sc call ⦃ r => r.2 = a ∧ ∃ data σy, + Fl4Spec.schedContextYieldTo (Z := Unit) sc.bv σ = ({(data, σy)}, False) ∧ + ((data = [] ∧ VEAnyX (L := L) arI AR r.1 r.2 σy) ∨ + (L.mem r.1 (stA ct) ≠ sched.TS_RESTART ∧ ∃ σ2, ((), σ2) ∈ (specTail call ct.bv data σy).1 ∧ + RelTopM (L := L) r.1 σ2 ∧ VEAnyX (L := L) arI AR r.1 r.2 σ2)) ⦄ := by + have B := J.1.1 + have hS := B.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + obtain ⟨x0, n0, hk0, hx⟩ := hs.scs sc hsc + rw [hk] at hk0; cases hk0 + have hsc0 := B.c0 sc hsc + have hyfw : x.scYieldFrom = Notif.optRef (L.mem s (scYieldFromA sc)) := by + rw [scYieldFromA_eq]; exact hx.yieldFrom + have hyc : L.mem s (yieldA ct) = 0#u64 := by + obtain ⟨y0, hy0, e0⟩ := B.yld.tcb ct hct + rw [hkc] at hy0; cases hy0 + exact optRef_eq_none (by rw [← e0]; exact hycn) + -- the SC's thread holds the SC + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hscS : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc x n hsc0 hk).2⟩ + obtain ⟨-, -, -, xt, hkt, hxt⟩ := scBack_of hS B.blk.2.2.1 B.tcbs_eq.1.symm hsc hscS hsc0 hk htc + unfold schedcontext.yield_to + step with obj.Sc.yield_from.spec' as ⟨yf, hyf⟩ + subst hyf + step with obj.Tcb.is_null.spec as ⟨b, hb⟩ + subst hb + refine WP.spec_bind (show _ ⦃ r => r.2 = a ∧ ∃ σc, (Fl4Spec.NonDet.when (x.scYieldFrom ≠ none) (do + Fl4Spec.completeYieldTo (Z := Unit) (Fl4Spec.the x.scYieldFrom) + let o ← Fl4Spec.getScObjRef (Z := Unit) Fl4Spec.SchedContext.scYieldFrom sc.bv + Fl4Spec.NonDet.assert (o = none)) : Fl4Spec.SMonad Unit Unit) σ = ({((), σc)}, False) ∧ + SFrame σ σc ∧ (∃ x1, σc.kheap sc.bv = some (.schedContext x1 n) ∧ x1.scYieldFrom = none) ∧ + (∃ xc1, σc.kheap ct.bv = some (.tcb xc1) ∧ xc1.tcbYieldTo = none) ∧ + ∃ (V1 : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V1) H Cs Ac' Lst' D' r.1 r.2 σc ⦄ from ?_) ?_ + · by_cases h0 : L.mem s (scYieldFromA sc) = 0#u64 + · simp only [h0, decide_true, ↓reduceIte] + have hxn : x.scYieldFrom = none := by rw [hyfw, h0, Notif.optRef_zero] + refine (WP.spec_ok _).mpr ⟨rfl, σ, ?_, SFrame.refl σ, ⟨x, hk, hxn⟩, ⟨xc, hkc, hycn⟩, Z.vs, Ac, Lst, D, J⟩ + rw [nd_when_false_y (by rw [hxn]; simp)]; rfl + · simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte] + have hxf : x.scYieldFrom = some (L.mem s (scYieldFromA sc)).bv := by rw [hyfw, Notif.optRef_ne h0] + obtain ⟨u, hu, hub, hum⟩ := B.yld.frm sc hsc x n hk _ hxf + have hue := eq_of_bv hub + rw [← hue] at hxf ⊢ + obtain ⟨xu, hku, hyu⟩ := B.yld.tcb u hu + have hyu' : xu.tcbYieldTo = some sc.bv := by rw [hyu, hum, Notif.optRef_ne hsc0] + have hcu : ct ≠ u := fun e => by rw [e, hum] at hyc; exact hsc0 hyc + refine WP.spec_mono (WP.spec_and (scYieldCompletionArch s a hAT u) + (B.completeYieldJDN hAT hmax htu hu)) ?_ + rintro ⟨s1, a1⟩ ⟨ha1, σc, hcy, V1, Ac1, Lst1, B1, F1⟩ + have J1 := J.ofNat B1 F1 + obtain ⟨hsf, ⟨x1, hk1, hyf1⟩, hyk⟩ := completeYieldTo_sframe hku hyu' hk hcy + obtain ⟨xc1, hkc1, hyc1⟩ := hyk _ xc (bv_ne hcu) hkc + refine ⟨ha1, σc, ?_, hsf, ⟨x1, hk1, hyf1⟩, ⟨xc1, hkc1, hyc1.trans hycn⟩, V1, Ac1, Lst1, D, J1⟩ + unfold Fl4Spec.NonDet.when + rw [ite_eq_left (by rw [hxf]; simp), hxf] + show (Fl4Spec.completeYieldTo u.bv >>= _) σ = _ + rw [nd_bind_one hcy, nd_bind_one (getScObjRef_eq hk1 _), hyf1] + show (({x | (none : Option Fl4Spec.ObjRef) = none ∧ x = ((), σc)}, ¬ (none : Option Fl4Spec.ObjRef) = none) : + Set (Unit × Fl4Spec.State Unit) × Prop) = _ + simp + rintro ⟨s1, a1⟩ ⟨ha1, σc, hw1, hsf, ⟨x1, hk1, hyf1⟩, ⟨xc1, hkc1, hyc1⟩, V1, Ac1, Lst1, D1, J1⟩ + change a1 = a at ha1 + subst a1 + have B1 := J1.1.1 + -- the objects after the yielder's completion + obtain ⟨x1', hk1', htc1', -⟩ := hsf.sc _ x n hk + rw [hk1] at hk1'; cases hk1' + have htc1 : x1.scTcb = some t.bv := htc1'.trans htc + obtain ⟨xt1, hkt1, -, hxt1', -, -⟩ := hsf.tcb _ xt hkt + have hxt1 : xt1.tcbSchedContext = some sc.bv := hxt1'.trans hxt + obtain ⟨xc1', hkc1', hst1, -⟩ := hsf.tcb _ xc hkc + rw [hkc1] at hkc1'; cases hkc1' + have hrs1 : xc1.tcbState = .restart := hst1.trans hrs + have hcur1 : σc.curThread = ct.bv := hsf.cur.trans hcur + have hcs1 : Fl4Spec.schedulable ct.bv σc = true := hsf.schedulable hcs + have hyc1' : L.mem s1 (yieldA ct) = 0#u64 := by + obtain ⟨y1, hy1, e1⟩ := B1.yld.tcb ct hct + rw [hkc1] at hy1; cases hy1 + exact optRef_eq_none (by rw [← e1]; exact hyc1) + have hS1 := B1.xtop.fin.1.1.1 + have hR1 := B1.xtop.fin.1.1.2.2.1 + obtain ⟨Q1, R1, hc1, hs1⟩ := hS1.lists + obtain ⟨hrun1, hrel1⟩ := B1.inv.yieldRes hk1 hkt1 hxt1 + have hne1 := B1.inv.ne _ _ _ hk1 + have hres : Fl4Spec.schedContextResume (Z := Unit) sc.bv σc = ({((), σc)}, False) := + resume_spec_noop hk1 htc1 hkt1 hne1 hrun1 hrel1 + refine WP.spec_bind (resume_noop (ar := arI) s1 a σc hR1 hS1 hmb hsc hsc0 ht (word24_of hS1 hsc hk1 htc1) + (scA_of hS1 ht hkt1 hxt1) hk1 hrel1) ?_ + rintro r rfl + step with sched.cur_thread.spec' as ⟨ct', hct'⟩ + have hct'' : ct' = ct := by + rw [hct', hc1.cur]; exact eq_of_bv (hs1.curThread.symm.trans hcur1) + clear hct' + subst ct' + refine WP.spec_bind (B1.scYieldSwitchesS3 hsc hk1 hyf1 ht htc1 hct hcur1 hyc1' hcs1) ?_ + rintro ⟨b1, s3⟩ ⟨σ3, hsw, hk3, Ac3, Lst3, D3, B3, F3⟩ + have J3 := J1.ofNat (V' := (Z.mv Z.G V1).vs) B3 F3 + have hsw' := hsw + unfold yieldSwitch at hsw' + have hpre : Fl4Spec.schedContextYieldTo (Z := Unit) sc.bv σ = + (if (!b1) = true then Fl4Spec.returnConsumed (Z := Unit) sc.bv else pure []) σ3 := by + unfold Fl4Spec.schedContextYieldTo + rw [nd_bind_one (getScObjRef_eq hk _), nd_bind_one hw1, nd_bind_one hres, nd_bind_one hsw'] + cases b1 + swap + · show (if true = true then _ else _) ⦃ _ ⦄ + rw [ite_eq_left rfl] + exact (WP.spec_ok _).mpr ⟨rfl, [], σ3, by rw [hpre]; rfl, Or.inl ⟨rfl, J3.anyX arI⟩⟩ + -- the call returns at once: the consumed time + show (if false = true then _ else _) ⦃ _ ⦄ + rw [ite_eq_right (fun h => Bool.false_ne_true h)] + have hk3' : σ3.kheap = σc.kheap := hk3 rfl + have hkc3 : σ3.kheap ct.bv = some (.tcb xc1) := by rw [hk3']; exact hkc1 + have hks3 : σ3.kheap sc.bv = some (.schedContext x1 n) := by rw [hk3']; exact hk1 + obtain ⟨c, v, hv⟩ := updConsumed_one hks3 + set σu := updSc σ3 sc.bv (Fl4Spec.scConsumedUpdate (fun _ => v) x1) n with hσu + have hsct : ct.bv ≠ sc.bv := fun e => by rw [e, hks3] at hkc3; cases hkc3 + have hku : σu.kheap ct.bv = some (.tcb xc1) := by + simp only [hσu, updSc, updK, Function.update_of_ne hsct]; exact hkc3 + have hrc : Fl4Spec.returnConsumed (Z := Unit) sc.bv σ3 = ({(Fl4Spec.wordsFromTime c, σu)}, False) := by + unfold Fl4Spec.returnConsumed; rw [nd_bind_one hv]; rfl + have hsetc : Fl4Spec.setConsumed (Z := Unit) sc.bv ct.bv σ3 = + Fl4Spec.replyFromKernel (Z := Unit) ct.bv (0, Fl4Spec.wordsFromTime c) σu := by + unfold Fl4Spec.setConsumed; rw [nd_bind_one hv] + + have hS3 := B3.xtop.fin.1.1.1 + have hSC : schedcontext.set_consumed st arI s3 a sc ct call ⦃ s4 => ∃ σ4, + Fl4Spec.NonDet.when (call = true) (Fl4Spec.replyFromKernel (Z := Unit) ct.bv (0, Fl4Spec.wordsFromTime c)) σu = + ({((), σ4)}, False) ∧ ∃ y, σ4.kheap ct.bv = some (.tcb y) ∧ y.tcbState = .restart ∧ + ∃ W, VmEptState (L := L) arI V X XL AR ((Z.mv Z.G V1).mv Z.G W) + H Cs Ac3 Lst3 D3 s4 a σ4 ⦄ := by + cases call + · unfold schedcontext.set_consumed + refine WP.spec_bind (B3.updConsumedN hAT hmax htu hsc hct) ?_ + rintro ⟨cv, s4⟩ ⟨σ4, c', he4, W, B4, F4⟩ + simp only [Bool.false_eq_true, ↓reduceIte] + rw [hv] at he4 + have e : σu = σ4 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst he4))).2 + subst σ4 + refine (WP.spec_ok _).2 ⟨σu, ?_, _, hku, hrs1, W, J3.ofNat B4 F4⟩ + unfold Fl4Spec.NonDet.when + rw [ite_eq_right (by simp)] + rfl + · refine WP.spec_mono (B3.setConsumedJV hAT hmax htu hsc hct) ?_ + rintro s4 ⟨σ4, he4, -, hQ, hw4, B4⟩ + have F4 := NatFoot.ofW B3.xtop hw4 (fun _ h => Ipc.FinW.oobj hS3 h) + (fun _ h => Ipc.FinW.nv hS3 h) (fun _ h => B3.finW_nh h) (natK_qky hQ) + obtain ⟨v', w', he4'⟩ := Sched.setConsumed_shape hks3 hkc3 + have eσ4 := the_result he4' he4 + refine ⟨σ4, ?_, Endp.replyTcb xc1 w', by rw [eσ4]; simp [updK], hrs1, + (Z.mv Z.G V1).vs, J3.ofNat (V' := (Z.mv Z.G V1).vs) B4 F4⟩ + unfold Fl4Spec.NonDet.when + rw [ite_eq_left rfl, ← hsetc] + exact he4 + refine WP.spec_bind hSC ?_ + rintro s4 ⟨σ4, hrf, y, hk4, hrs4, W, J4⟩ + refine WP.spec_bind (J4.1.1.runJ hct hk4 hrs4) ?_ + rintro s5 ⟨Lst5, B5, F5⟩ + let σ5 := stsAct (updK σ4 ct.bv (.tcb { y with tcbState := .running })) ct.bv + have hk5 : σ5.kheap ct.bv = some (.tcb { y with tcbState := .running }) := by + simp [σ5, stsAct_kheap, updK] + have hne := B5.notRestart hct hk5 (by intro e; cases e) + have J5 := J4.ofNat (V' := ((Z.mv Z.G V1).mv Z.G W).vs) B5 F5 + refine (WP.spec_ok _).mpr ⟨rfl, Fl4Spec.wordsFromTime c, σu, + by rw [hpre]; exact hrc, Or.inr ⟨hne, σ5, ?_, B5.xtop.relTop, J5.anyX arI⟩⟩ + rw [specTail_of_restart hku hrs1, nd_bind_one hrf, Notif.setThreadState_eq .running hk4] + exact Set.mem_singleton _ + +set_option maxHeartbeats 16000000 in +/-- YieldTo at the selected cap and its bound thread. A switch returns no +data. An at-once return carries its own eager reply completion. -/ +theorem VmEptState.selectedSchedContextYieldS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hT : TimeAgree arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {ct cptr len buf lbl sc sb : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedContext sc sb) ex args exs) + (l45 : lbl = fl4_abi.label.SchedContextYieldTo) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf ex + ⦃ res => let m := specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.SchedContext sc sb) exs buffer + VDispPostVEX (L := L) arI AR MR on rc σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don m ct s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases fp + case true => + simp only [↓reduceIte, WP.spec_ok] + refine Or.inl (Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩) + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute])] + exact run_liftME_err rfl + case false => + simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨d, hd, -, hcanon⟩ := sc_canon lbl + rw [scSpecDecV lbl.bv d sc sb args (Fl4Spec.toBl cptr.bv) sl exs buffer hcanon] + have B := J.1.1 + have hs : AbsSlot Z.th sl := by + obtain ⟨p, ep, hp⟩ := q.slot + exact ⟨p, ep, Or.inl hp⟩ + obtain ⟨hsc, -, hback, -⟩ := B.selectedScBack hs q.capAt + have hS := B.xtop.fin.1.1.1 + obtain ⟨Q, R', hc, hs⟩ := hS.lists + obtain ⟨hcur, xc, hkc, hrunc⟩ := B.curRunnable hni q.current + have hct' : ct = nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [q.current, WP.spec_ok, hc.cur] at h; exact h + have hctT : ct ∈ Z.T := hct' ▸ hc.curT + have hcs : Fl4Spec.schedulable ct.bv σ = true := by + have h := ct_schedulable (B.curInv (by rw [hni.2]; simp)) B.act B.inv (hcur ▸ hkc) hrunc + rw [hcur] at h; exact h + obtain ⟨x, n, hk, hx⟩ := hs.scs sc hsc + have hsc0 := B.c0 sc hsc + have hg : Fl4Spec.genInvocationType d = .schedContextYieldTo := by + rw [hd, l45]; unfold scLabelOf; simp + have hdec : (Fl4Spec.decodeSchedContextInvocation (Z := Unit) d sc.bv (exs.map Prod.fst)).run σ = + (Fl4Spec.decodeSchedContextYieldTo (Z := Unit) sc.bv).run σ := by + unfold Fl4Spec.decodeSchedContextInvocation; rw [hg] + have hill : ∀ (P : Prop) [Decidable P], P → ∀ f : Unit → Fl4Spec.SEMonad Unit Fl4Spec.SchedContextInvocation, + (Fl4Spec.NonDetE.whenE P (Fl4Spec.NonDetE.throwError .illegalOperation) >>= f).run σ = + ({(.error .illegalOperation, σ)}, False) := fun P _ h f => wE_pos h _ f σ + have hxtw : x.scTcb = Notif.optRef (L.mem s (scW sc 24#u64)) := hx.tcb + unfold schedcontext.decode + step with P1.obj.Sc.tcb.spec as ⟨i, hi⟩ + rw [scTcbA_eq] at hi + subst hi + refine WP.spec_bind (scStallLocal B sc (fun h => (hback h).1)) ?_ + intro r hr + subst hr + show (sched.cur_thread st s >>= _) ⦃ _ ⦄ + rw [q.current, bind_tc_ok] + have l44 : lbl ≠ fl4_abi.label.SchedContextConsumed := lbl_ne l45 (by simp) + have l41 : lbl ≠ fl4_abi.label.SchedContextBind := lbl_ne l45 (by simp) + have l43 : lbl ≠ fl4_abi.label.SchedContextUnbindObject := lbl_ne l45 (by simp) + have l42 : lbl ≠ fl4_abi.label.SchedContextUnbind := lbl_ne l45 (by simp) + rw [ite_eq_right l44, ite_eq_right l41, ite_eq_right l43, ite_eq_right l42, ite_eq_left l45] + -- `decode_sched_context_yield_to`'s checks + step with P1.obj.Sc.tcb.spec as ⟨i1, hi1⟩ + rw [scTcbA_eq] at hi1 + subst hi1 + step with obj.Tcb.is_null.spec as ⟨b, hb⟩ + subst hb + by_cases h0 : L.mem s (scW sc 24#u64) = 0#u64 + · simp only [h0, decide_true, ↓reduceIte] + refine illegal_ok s a _ (Or.inl (Or.inl ⟨_, rfl, rfl, + ⟨.illegalOperation, run_liftME_err (run_liftME_err ?_), Or.inl trivial⟩, J.anyX arI⟩)) + rw [hdec] + unfold Fl4Spec.decodeSchedContextYieldTo + rw [lE_bind (getScObjRef_eq hk _), hxtw, h0, Notif.optRef_zero] + exact hill _ rfl _ + simp only [h0, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨ht, -, -⟩ := hback h0 + set t := L.mem s (scW sc 24#u64) with htdef + have htc : x.scTcb = some t.bv := by rw [hxtw, Notif.optRef_ne h0] + obtain ⟨xt, hkt, -, -, hxts, hpt⟩ := hs.tcb t ht + have hpre : ∀ f : Option Fl4Spec.ObjRef → Fl4Spec.SEMonad Unit Fl4Spec.SchedContextInvocation, + (Fl4Spec.NonDetE.liftE (Fl4Spec.getScObjRef (Z := Unit) Fl4Spec.SchedContext.scTcb sc.bv) >>= f).run σ = + (f (some t.bv)).run σ := fun f => by rw [lE_bind (getScObjRef_eq hk _), htc] + step as ⟨b1, hb1⟩ + by_cases hte : t = ct + · have : b1 = true := hb1.2 hte + simp only [this, ↓reduceIte] + refine illegal_ok s a _ (Or.inl (Or.inl ⟨_, rfl, rfl, + ⟨.illegalOperation, run_liftME_err (run_liftME_err ?_), Or.inl trivial⟩, J.anyX arI⟩)) + rw [hdec] + unfold Fl4Spec.decodeSchedContextYieldTo + rw [hpre, wE_neg (by simp), lE_bind (show Fl4Spec.NonDet.gets (fun s : Fl4Spec.State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl), hcur, ← hte] + exact hill _ rfl _ + have : b1 = false := Bool.eq_false_iff.2 (fun h => hte (hb1.1 h)) + simp only [this, Bool.false_eq_true, ↓reduceIte] + have hne' : t.bv ≠ ct.bv := bv_ne hte + obtain ⟨xm, hkm, hmc, -⟩ := B.mcp ct hctT + rw [hkc] at hkm; cases hkm + obtain ⟨xy, hky, hyc⟩ := B.yld.tcb ct hctT + rw [hkc] at hky; cases hky + have hpre2 : (Fl4Spec.decodeSchedContextYieldTo (Z := Unit) sc.bv).run σ = + (Fl4Spec.NonDetE.whenE (xt.tcbPriority > xc.tcbMcpriority) + (Fl4Spec.NonDetE.throwError .illegalOperation) >>= fun _ => do + let ytPtr ← Fl4Spec.NonDetE.liftE <| Fl4Spec.threadGet (Z := Unit) Fl4Spec.Tcb.tcbYieldTo ct.bv + Fl4Spec.NonDetE.whenE (ytPtr ≠ none) <| Fl4Spec.NonDetE.throwError .illegalOperation + Fl4Spec.NonDetE.returnOk <| .invokeSchedContextYieldTo sc.bv).run σ := by + unfold Fl4Spec.decodeSchedContextYieldTo + rw [hpre, wE_neg (by simp), lE_bind (show Fl4Spec.NonDet.gets (fun s : Fl4Spec.State Unit => s.curThread) σ = + ({(σ.curThread, σ)}, False) from rfl), hcur, wE_neg (by simpa using hne'), + show Fl4Spec.the (some t.bv) = t.bv from rfl, lE_bind (threadGet_eq _ hkt), lE_bind (threadGet_eq _ hkc)] + step with obj.Tcb.prio.spec as ⟨i2, hi2⟩ + step with Sched.obj.Tcb.mcp.spec' as ⟨i3, hi3⟩ + subst hi2 hi3 + by_cases hgt : L.mem s (prioA t) > L.mem s (mcpA ct) + · simp only [hgt, ↓reduceIte] + refine illegal_ok s a _ (Or.inl (Or.inl ⟨_, rfl, rfl, + ⟨.illegalOperation, run_liftME_err (run_liftME_err ?_), Or.inl trivial⟩, J.anyX arI⟩)) + rw [hdec, hpre2] + exact hill _ ((w8_gt_iff hpt hmc).1 hgt) _ + simp only [hgt, ↓reduceIte] + have hpre3 := hpre2 + rw [wE_neg (fun h => hgt ((w8_gt_iff hpt hmc).2 h)), lE_bind (threadGet_eq _ hkc)] at hpre3 + step with obj.Tcb.yield_to.spec as ⟨i4, hi4⟩ + subst hi4 + by_cases hy0 : L.mem s (yieldA ct) = 0#u64 + swap + · have : (L.mem s (yieldA ct) != 0#u64) = true := by simp [hy0] + simp only [this, ↓reduceIte] + refine illegal_ok s a _ (Or.inl (Or.inl ⟨_, rfl, rfl, + ⟨.illegalOperation, run_liftME_err (run_liftME_err ?_), Or.inl trivial⟩, J.anyX arI⟩)) + rw [hdec, hpre3] + exact hill _ (by rw [hyc, Notif.optRef_ne hy0]; simp) _ + have : (L.mem s (yieldA ct) != 0#u64) = false := by simp [hy0] + simp only [this, Bool.false_eq_true, ↓reduceIte] + have hyn : xc.tcbYieldTo = none := by rw [hyc, hy0, Notif.optRef_zero] + rw [wE_neg (by simp [hyn])] at hpre3 + have hrun : (Fl4Spec.decodeSchedContextInvocation (Z := Unit) d sc.bv (exs.map Prod.fst)).run σ = + ({(.ok (.invokeSchedContextYieldTo sc.bv), σ)}, False) := by + rw [hdec, hpre3]; rfl + obtain ⟨s1, σ1, Lst1, x1, hcur0, hkx1, hs1, he0, J1, eσ1, _, _, _⟩ := J.restartK hni + have hcW := P1.eq_of_bv (hcur.symm.trans hcur0) + rw [← hcW, hkc] at hkx1 + cases hkx1 + rw [← hcW] at hs1 eσ1 + rw [hs1]; simp only [bind_ok] + have hpcs : sc.bv ≠ ct.bv := fun e => by rw [e, hkc] at hk; cases hk + have hf := stsAct_fields (Notif.updK σ ct.bv (.tcb { xc with tcbState := .restart })) ct.bv + have hk1 : σ1.kheap sc.bv = some (.schedContext x n) := by + rw [eσ1, hf.1]; simp [updK, hpcs, hk] + have hkc1 : σ1.kheap ct.bv = some (.tcb { xc with tcbState := .restart }) := by + rw [eσ1, hf.1]; simp [updK] + have hcur1 : σ1.curThread = ct.bv := by + rw [eσ1, hf.2.1] + exact hcur + have hcs1 : Fl4Spec.schedulable ct.bv σ1 = true := by + obtain ⟨xq, scp, yq, nq, hq1, hq2, -, hq4, hq5, hq6⟩ := schedulable_inv hcs + rw [hkc] at hq1; cases hq1 + have hscp : scp ≠ ct.bv := fun e => by rw [e, hkc] at hq4; cases hq4 + refine schedulable_of (y := yq) (n := nq) hkc1 hq2 rfl ?_ hq5 ?_ + · rw [eσ1, hf.1]; simp [updK, hscp, hq4] + · rw [eσ1, hf.2.2.2]; exact hq6 + refine WP.spec_bind (J1.scYieldToS3 hAT (hT.mb a) hmax htu hsc ht hk1 htc hctT hcur1 hkc1 rfl hyn hcs1 call) ?_ + rintro ⟨s3, a2⟩ ⟨-, data, σy, hyt, hcase⟩ + show (ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a2)) ⦃ _ ⦄ + rw [WP.spec_ok] + have hp : ((.ok data, σy) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.fl4PerformInvocationVF (Z := Unit) on rc blk call don + (.base (.invokeSchedContext (.invokeSchedContextYieldTo sc.bv)))).run σ1).1 := by + change ((.ok data, σy) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (Fl4Spec.schedContextYieldTo (Z := Unit) sc.bv)).run σ1).1 + exact mem_liftE (x := (data, σy)) (by rw [hyt]; exact Set.mem_singleton _) + rcases hcase with ⟨rfl, hJ⟩ | ⟨hne, σ2, htail, hRel, hJ⟩ + · exact Or.inl (Or.inr ⟨.base (.invokeSchedContext (.invokeSchedContextYieldTo sc.bv)), + (DecOut.of_eq hrun).liftME.liftME, σ1, he0, (.ok [], σy), hp, Or.inl ⟨rfl, rfl, hJ⟩⟩) + · exact Or.inr (Or.inr ⟨rfl, hne, .base (.invokeSchedContext (.invokeSchedContextYieldTo sc.bv)), + (DecOut.of_eq hrun).liftME.liftME, σ1, he0, data, σy, hp, ((), σ2), htail, hRel, hJ⟩) + + +/-- Dispatch every SchedContext label from the same actual selected cap +and first-phase bit. The eager outcomes carry their matched reply state. -/ +theorem VmEptState.selectedSchedContextS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hK : ARKeep arI AR) (hT : TimeAgree arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {ct cptr len buf lbl sc sb : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.SchedContext sc sb) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.SchedContext sc sb) blk call don fp buf ex + ⦃ res => let m := specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.SchedContext sc sb) exs buffer + VDispPostVEX (L := L) arI AR MR on rc σ blk call don m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don m ct s res ⦄ := by + by_cases hf : fp = true + · exact WP.spec_mono (J.selectedSchedContextUnbindS3 (MR := MR) hni q buffer on rc blk call don fp + (Or.inl hf)) fun _ h => Or.inl h + by_cases h42 : lbl = fl4_abi.label.SchedContextUnbind + · exact WP.spec_mono (J.selectedSchedContextUnbindS3 (MR := MR) hni q buffer on rc blk call don fp + (Or.inr (Or.inl h42))) fun _ h => Or.inl h + by_cases hl : lbl = fl4_abi.label.SchedContextBind ∨ lbl = fl4_abi.label.SchedContextUnbindObject + · exact WP.spec_mono (J.selectedSchedContextBindLikeS3 (MR := MR) hni hAT hK hT hmax htu q hl + buffer on rc blk call don fp) fun _ h => Or.inl h + by_cases h44 : lbl = fl4_abi.label.SchedContextConsumed + · exact WP.spec_mono (J.selectedSchedContextConsumedS3 hni hAT hmax htu q h44 buffer on rc blk call don fp) + fun _ h => Or.inr h + by_cases h45 : lbl = fl4_abi.label.SchedContextYieldTo + · exact J.selectedSchedContextYieldS3 (MR := MR) hni hAT hT hmax htu q h45 buffer on rc blk call don fp + have h41' : lbl.val ≠ 41 := fun e => hl (Or.inl (UScalar.eq_of_val_eq (by simp [e]))) + have h42' : lbl.val ≠ 42 := fun e => h42 (UScalar.eq_of_val_eq (by simp [e])) + have h43' : lbl.val ≠ 43 := fun e => hl (Or.inr (UScalar.eq_of_val_eq (by simp [e]))) + have h44' : lbl.val ≠ 44 := fun e => h44 (UScalar.eq_of_val_eq (by simp [e])) + have h45' : lbl.val ≠ 45 := fun e => h45 (UScalar.eq_of_val_eq (by simp [e])) + have he : scLabelOf lbl = .invalidInvocation := by + simp [scLabelOf, h41', h42', h43', h44', h45'] + exact WP.spec_mono (J.selectedSchedContextUnbindS3 (MR := MR) hni q buffer on rc blk call don fp + (Or.inr (Or.inr he))) fun _ h => Or.inl h + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ScCtl.lean b/proofs/Fl4Proofs/P3/VmEntryS3ScCtl.lean new file mode 100644 index 000000000..3904e92c2 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ScCtl.lean @@ -0,0 +1,368 @@ +/- +SchedControl ConfigureFlags at a VM dispatch state. [S] The actually selected +SchedControl cap names the executing core. The concrete extra-cap read supplies +the SC and any bound TCB. Locality of that TCB follows from SchedCore.aff and +no affinity write is reached. +[P] The existing architecture-totality, AR preservation and time agreement +predicates at the reached decoder. +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.ScRestJ +import Fl4Proofs.P3.XTopJointKEntryU30 +import Fl4Proofs.P3.FrontEndLabel + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched +open Fl4Proofs.P1 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +private theorem JAtZB.scCtlExtra {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {x p sb : U64} (hx : IsSlot Z.th x) + (hc : capAt (L.mem s) x = ok (.SchedContext p sb)) : + sb.val < 64 ∧ + (p ∈ Z.C ∧ p ≠ 0#u64 ∧ + ∃ y, σ.kheap p.bv = some (.schedContext y (sb.val - Fl4Spec.minSchedContextBits)) ∧ + ∀ t : U64, y.scTcb = some t.bv → + t ∈ Z.T ∧ ∃ xt, σ.kheap t.bv = some (.tcb xt) ∧ xt.tcbSchedContext = some p.bv) ∧ + p.bv ≠ σ.idleSc := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hO := hJ.cobj _ hx + rw [capV_of_capAt hc] at hO + obtain ⟨hH, hpi0⟩ := hO + have hpi := J.xtop.fin.1.1.1.ne_idleSc hpi0 + have hpC : p ∈ Z.C := J.hscC p _ hH + have hp0 : p ≠ 0#u64 := J.c0 p hpC + obtain ⟨Q, R, hc', hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨y, n, hk, -⟩ := hs.scs p hpC + have hH' := (hJ.sc p y n hp0 hk).2 + rw [hH] at hH' + have hn : sb.val = n + Fl4Spec.minSchedContextBits := HKind.sc.inj (Option.some.inj hH') + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := by + have := J.kern.heap.fits p _ hH + simpa only [P1.HKind.bits] using this + have hp1 : 1 ≤ p.val := by + rcases Nat.eq_zero_or_pos p.val with h | h + · exact absurd (UScalar.eq_of_val_eq (by rw [h]; rfl)) hp0 + · exact h + have hsb : sb.val < 64 := by + by_contra hge + have : 2 ^ 64 ≤ 2 ^ sb.val := Nat.pow_le_pow_right (by decide) (by omega) + omega + have hpS : p ∈ P1.Heap.scs H := ⟨_, hH⟩ + refine ⟨hsb, ⟨hpC, hp0, y, by rw [hn, Nat.add_sub_cancel]; exact hk, fun t ht => ?_⟩, hpi⟩ + obtain ⟨u, huT, hu, xt, hkt, hxt⟩ := + scBack_of J.xtop.fin.1.1.1 J.blk.2.2.1 J.tcbs_eq.1.symm hpC hpS hp0 hk ht + have e := P1.eq_of_bv hu + subst e + exact ⟨huT, xt, hkt, hxt⟩ + +private theorem JAtZB.stepP1_of_foot {s s' : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {Ac' : Set U64} + (hb : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hf : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s s') : + ScStepP1 (L := L) Z H Cs s s' := by + have hw : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s) := P1.WritesIn.mono hf fun _ h => P1.KW.mono h (Set.empty_subset _) + exact ⟨Ac', J.kw_dbg hw, J.kw_dbgKeep hw, hb⟩ + +private theorem JAtZB.curFacts {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) : + ct = L.curW s ∧ ct ∈ Z.T ∧ P1.Runnable (L.mem s (P1.stA (L.curW s))) ∧ L.actionW s = 0#u64 := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hcq.cur] at h; exact h + have ht : ct ∈ Z.T := by rw [hct']; exact hcq.curT + have hcw : ct = L.curW s := by + have h := P1.sched.cur_thread.spec (st := st) s + rw [hct, WP.spec_ok] at h; exact h + obtain ⟨hcur, xc, hkc, hrun⟩ := J.curRunnable hni hct + have hcs := ct_schedulable (J.curInv (by rw [hni.2]; simp)) J.act J.inv (hcur ▸ hkc) hrun + rw [hcur] at hcs + obtain ⟨hrw, -, -, -⟩ := of_decide_eq_true ((schedulable_eq hR hS ht).trans hcs) + have ha0 : L.actionW s = 0#u64 := by + have h := hS.act + rw [hni.2] at h + unfold P1.LawfulStore.actionW; rw [hcq.cur]; exact h + refine ⟨hcw, ht, ?_, ha0⟩ + rw [← hcw, stA_tStA]; exact hrw + +set_option maxHeartbeats 4000000 in +theorem JAtZB.scCtlStepP1 (hA : P0.ArchTotal arI) {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) {lbl length cpu : U64} {blk call don fp : Bool} {buf : U64} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {slot : U64} (hcpu : cpu = Z.c) + {excaps : List Fl4Spec.Cap} (hex : ExtraRel Z.th (L.mem s) (exAt extra 0) excaps) : + syscall.decode_invocation st arI s ar lbl length slot (.SchedControl cpu) blk call don fp buf extra + ⦃ r => ScStepP1 (L := L) Z H Cs s r.2.1 ⦄ := by + obtain ⟨hcw, hctT, hrun, hact⟩ := J.curFacts hni hct + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hcC : cpu ∈ Cs := by rw [hcpu, ← hcq.cur]; exact J.kern.cpuCs + have hx : ∀ sl, Aeneas.Std.Array.index_usize extra 0#usize = ok sl → sl ≠ 0#u64 → + obj.Slot.cap st sl s ⦃ c => ∀ ptr sz, c = .SchedContext ptr sz → + ptr ∈ P1.Heap.scs H ∧ ptr ≠ 0#u64 ∧ sz.val = P1.Heap.scBits H ptr ∧ + (L.mem s (P1.scTcbA ptr) ≠ 0#u64 → L.mem s (P1.affA (L.mem s (P1.scTcbA ptr))) = cpu) ⦄ := by + intro sl hsl h0 + obtain rfl : exAt extra 0 = sl := eq_of_ok_eq ((exAt_idx0 extra).symm.trans hsl) + rcases hex with ⟨e, -⟩ | ⟨-, hxs, c, a, rest, hcx, -, -, -⟩ + · exact absurd e h0 + rw [slot_cap_eq, hcx, WP.spec_ok] + rintro ptr sz rfl + obtain ⟨hpC, hp0, z, hk, hbnd⟩ := (J.scCtlExtra hxs hcx).2.1 + obtain ⟨z', n', hk', hz⟩ := hs.scs ptr hpC + rw [hk] at hk'; cases hk' + have hback : ∀ t : U64, t = L.mem s (P1.scTcbA ptr) → t ≠ 0#u64 → t ∈ Z.T := by + rintro t rfl hne + exact (hbnd _ (by rw [hz.tcb, ← scTcbA_eq, Notif.optRef_ne hne])).1 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hO := hJ.cobj _ hxs + rw [capV_of_capAt hcx] at hO + refine ⟨⟨_, hO.1⟩, J.c0 ptr hpC, (P1.Heap.scBits_eq hO.1).symm, fun h0' => ?_⟩ + rw [hcq.aff _ (hback _ rfl h0'), hcpu] + unfold syscall.decode_invocation + step with sched.cur_thread.spec' as ⟨ct', _⟩ + try dsimp only + split + · exact (WP.spec_ok _).2 ⟨Ac, rfl, P1.DbgKeep.refl _, J.blk⟩ + · refine WP.spec_mono (P1.schedcontext.decode_sched_control.footK hA J.blk hcC hrun hact hx) fun r hr => ?_ + obtain ⟨r1, s', a'⟩ := r + obtain ⟨⟨Ac', hb⟩, hf⟩ := hr + exact J.stepP1_of_foot hb hf + + +set_option maxHeartbeats 8000000 in +private theorem sched_controlVmStep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D V X : Set U64} {XL : U64 → Nat} (s : S) (a : A) (σ : Fl4Spec.State Unit) + [VxE Z] (J0 : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (htm : TimeAgree arI) + (lbl : U64) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .schedControlConfigureFlags ↔ + lbl = fl4_abi.label.SchedControlConfigureFlags) + (cpu length buf ct : U64) (hcpu : cpu = Z.c) (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 (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hex : ExtraRel Z.th (L.mem s) x0 (excaps.map Prod.fst)) + (hsb : x0 ≠ 0#u64 → ∀ p sb, capAt (L.mem s) x0 = ok (.SchedContext p sb) → sb.val < 64) + (hsc : x0 ≠ 0#u64 → ∀ p sb, capAt (L.mem s) x0 = ok (.SchedContext p sb) → p ∈ Z.C ∧ p ≠ 0#u64 ∧ + ∃ x, σ.kheap p.bv = some (.schedContext x (sb.val - Fl4Spec.minSchedContextBits)) ∧ + ∀ t : U64, x.scTcb = some t.bv → + t ∈ Z.T ∧ ∃ xt, σ.kheap t.bv = some (.tcb xt) ∧ xt.tcbSchedContext = some p.bv) + (hpi : x0 ≠ 0#u64 → ∀ p sb, capAt (L.mem s) x0 = ok (.SchedContext p sb) → p.bv ≠ σ.idleSc) + (slot : U64) (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (buffer : Option Fl4Spec.ObjRef) + (hArch : syscall.decode_invocation st arI s a lbl length slot (.SchedControl cpu) blk call don fp buf extra + ⦃ r => ∀ c, AR a c → AR r.2.2 c ⦄) + (hRest : syscall.decode_invocation st arI s a lbl length slot (.SchedControl cpu) blk call don fp buf extra + ⦃ r => ScStepP1 (L := L) Z H Cs s r.2.1 ⦄) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) : + syscall.decode_invocation st arI s a lbl length slot (.SchedControl cpu) blk call don fp buf extra + ⦃ r => VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp slbl (args.map UScalar.bv) ci sl (.SchedControl cpu) excaps buffer) s r ⦄ := by + have J := J0.1.1 + have hres := hni.2 + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + obtain ⟨hcur, xc, hkc, hrun⟩ := J.curRunnable hni hct + have hC := J.curInv (by rw [hres]; simp) + have key : syscall.decode_invocation st arI s a lbl length slot (.SchedControl cpu) blk call don fp buf extra + ⦃ res => (fp = true ∧ res = (.Err (.Sys (.InvalidCapability 0#u64)), s, a)) ∨ + (fp = false ∧ Sched.CPost σ (Fl4Spec.decodeSchedControlInvocationFlags (Z := Unit) slbl (args.map UScalar.bv) + (excaps.map Prod.fst)) s a + (fun inv res => ∃ σ1 σ', Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ1)}, False) ∧ + Fl4Spec.invokeSchedControlConfigureFlags (Z := Unit) inv σ1 = ({((), σ')}, False) ∧ + res.1 = .Ok () ∧ SchedRel st Z.N Z.T Z.C Z.c res.2.1 σ' ∧ NtfnStateRel Z.N Z.T (L.mem res.2.1) σ' ∧ + NSC Z.N Z.C σ' ∧ Inv σ' ∧ P1.WritesIn st s res.2.1 (KFoot st Z.T Z.N Z.C Z.c s σ) ∧ + (σ1.schedulerAction = .resumeCurThread → IdleOK σ1 → + (x0 ≠ 0#u64 → ∀ p sb, capAt (L.mem s) x0 = ok (.SchedContext p sb) → p.bv ≠ σ.idleSc) → ActIdle σ')) res) ⦄ := by + unfold syscall.decode_invocation + rw [hct]; simp only [bind_ok] + cases fp + · simp only [Bool.false_eq_true, ↓reduceIte] + exact WP.spec_mono (decode_configure_refinesA htm Z.th s a σ hS hR J.nsc J.inv hC J.off lbl slbl hlbl cpu + length buf ct hcpu hct args hlen hargs extra x0 hx0 _ hex hsb hsc hkc hrun) fun r h => Or.inr ⟨by trivial, h⟩ + · simp only [↓reduceIte, WP.spec_ok] + exact Or.inl ⟨by trivial, by trivial⟩ + refine WP.spec_mono (WP.spec_and hArch (WP.spec_and key hRest)) ?_ + rintro ⟨r0, s', a'⟩ ⟨hA, hq, hRst⟩ + rcases hq with ⟨rfl, he⟩ | ⟨rfl, hq⟩ + · cases he + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J0.anyX arI⟩ + change (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidCapability 0))).run σ = _ + exact run_liftME_err (run_throwError _ σ) + rcases hq with ⟨e, e', he, hrun', herr⟩ | ⟨inv, hdec, σ1, σ', he1, he2, hr1, hS', hR', hN', hI', hw, hAc⟩ + · cases he + exact Or.inl ⟨_, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err hrun'), herr⟩, + J0.anyX arI⟩ + have hm1 : ((), σ1) ∈ (Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ).1 := by rw [he1]; exact Set.mem_singleton _ + have hm2 : ((), σ') ∈ (Fl4Spec.invokeSchedControlConfigureFlags (Z := Unit) inv σ1).1 := by + rw [he2]; exact Set.mem_singleton _ + have hK : ScTKw (fun p => p = ct.bv) σ σ' := + (tk_setThreadState hkc (ipcFree_of_runnable hrun) ipcFree_restart _ hm1).trans + ((nkt_invokeSchedControlConfigureFlags inv σ1 ((), σ') hm2).mono fun _ h => h.elim) + have hX' : XTopAt (L := L) AR Z s' a' σ' := + xtop_sc_step hX J.ip J.nk (WritesIn.tfoot_of_kfoot hw) hK hS' hR' fun h => hA _ h + have hSOK : SOK (true, true) σ σ' := + (pres_setThreadState (f := (true, true)) _ _ σ _ hm1).trans + (pres_invokeSchedControlConfigureFlags (true, true) _ σ1 _ hm2) + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree ⟨⟨hS', hR', hN', hI'⟩, hw, hSOK⟩ + obtain ⟨Ac', hDbg, hKd, hBlk'⟩ := hRst + obtain ⟨a0, rest, hex', q, sz, rfl, b, per, k, badge, flag, rfl⟩ := + (rdp_decodeCfgHead slbl (args.map UScalar.bv) (excaps.map Prod.fst) σ _ (by rw [hdec]; exact Set.mem_singleton _)).2 + _ rfl + have hB' : BlockedScActive σ' := bsa_configure J.bsa (hcur ▸ hkc) hrun (hcur ▸ hm1) hm2 + -- the configured SC is the extra cap's + obtain ⟨p, sb, hx0n, hcap, hpq⟩ : ∃ p sb, x0 ≠ 0#u64 ∧ capAt (L.mem s) x0 = ok (.SchedContext p sb) ∧ p.bv = q := by + rcases hex with ⟨-, he⟩ | ⟨hx0n, -, c, a1, rest', hc, hCR, -, he⟩ + · rw [he] at hex'; cases hex' + rw [he] at hex' + injection hex' with ha + have hCa := hCR.abs + rw [ha] at hCa + cases c <;> simp only [absCap, reduceCtorEq, Fl4Spec.Cap.schedContextCap.injEq] at hCa + exact ⟨_, _, hx0n, hc, hCa.1.symm⟩ + -- the mid-entry facts: the restart keeps them; Configure of another SC is `MidKn` + have hcs : Fl4Spec.schedulable σ.curThread σ = true := ct_schedulable hC J.act J.inv (hcur ▸ hkc) hrun + obtain ⟨hres1, hC1, hO1, hA1, hI1⟩ := restart_mid hcur hkc (hcur ▸ hcs) hres hC J.off J.act J.inv + have hσ1 := the_result (Fl4Proofs.P3.Notif.setThreadState_eq .restart hkc) he1 + rw [← hσ1] at hres1 hC1 hO1 hA1 hI1 + have hAI' : ActIdle σ' := hAc hres1 hA1.2 hpi + have hctT : ct ∈ Z.T := J.thT ct (J.kern.curT ▸ by rw [show L.curW s = ct from P1.eq_of_bv (by rw [← J.kern.cur, hcur])]) + have hf1 := stsAct_fields (Fl4Proofs.P3.Notif.updK σ ct.bv (.tcb { xc with tcbState := .restart })) ct.bv + have hct1 : σ1.curThread = ct.bv := by rw [hσ1, hf1.2.1]; exact hcur + have hcs1 : σ1.curSc = σ.curSc := by rw [hσ1, hf1.2.2.1]; rfl + have hid1 : σ1.idleThread = σ.idleThread := by + rw [hσ1]; unfold Fl4Proofs.P3.Notif.stsAct; split_ifs <;> rfl + obtain ⟨hC', hOw', hO'⟩ : MidA Z.T σ' := by + obtain ⟨hpC, -, x, hkx, hbnd⟩ := hsc hx0n p sb hcap + have hpc : p.bv ≠ ct.bv := fun e => by rw [e, hkc] at hkx; cases hkx + have hkx1 : σ1.kheap p.bv = some (.schedContext x (sb.val - Fl4Spec.minSchedContextBits)) := by + rw [hσ1, (stsAct_fields _ _).1]; simp [Fl4Proofs.P3.Notif.updK, hpc, hkx] + obtain ⟨xt, hkt, hxs, -⟩ := hC.tcb + by_cases hq : q = σ.curSc + · -- the current SC, bound to the current thread: `commit_time`, a reschedule + obtain ⟨y1, n1, hy1, hy1t⟩ := J.inv.back _ xt σ.curSc hkt hxs + rw [← hq, ← hpq, hkx] at hy1; cases hy1 + have hci : σ1.curThread ≠ σ1.idleThread := by + rw [hct1, hid1, ← hcur]; intro e + obtain ⟨xi, hki, -, hti, -⟩ := J.act.2 + rw [← e, hkt] at hki; cases hki + rw [hcur, hkc] at hkt; cases hkt; rw [hti] at hrun; cases hrun + obtain ⟨kW, h0, hcn⟩ := mkw_configureCur (b := b) (per := per) (mr := k + Fl4Spec.MIN_REFILLS) (badge := badge) + (flag := flag) (hpq ▸ hkx1) (by rw [hcs1]; exact hq) (by rw [hy1t, hct1, hcur]) hci + (by simp [Fl4Spec.activeSc, Fl4Spec.MIN_REFILLS]) _ hm2 + exact kW.midA h0 hcn hC1 hI1 ⟨ct, hctT, hct1.symm⟩ + have ht : ∀ t, x.scTcb = some t → t ≠ σ1.idleThread ∧ t ≠ σ1.curThread := by + intro t ht + obtain ⟨-, xt', hkt', hxt'⟩ := hbnd ⟨t⟩ ht + have hkt2 : σ.kheap t = some (.tcb xt') := hkt' + refine ⟨fun e => ?_, fun e => ?_⟩ + · rw [hid1] at e + obtain ⟨xi, hki, hsi, -⟩ := J.act.2 + rw [e, hki] at hkt2; cases hkt2; rw [hsi] at hxt' + exact hpi hx0n p sb hcap (Option.some.inj hxt').symm + · rw [hct1, ← hcur] at e + rw [e, hkt] at hkt2; cases hkt2; rw [hxs] at hxt' + exact hq (by rw [← hpq]; exact (Option.some.inj hxt').symm) + have hK1 := mkn_configure (b := b) (per := per) (mr := k + Fl4Spec.MIN_REFILLS) (badge := badge) (flag := flag) + (hpq ▸ hkx1) (by rw [hcs1]; exact hq) ht (by simp [Fl4Spec.activeSc, Fl4Spec.MIN_REFILLS]) + _ hm2 + exact hK1.mid3 hC1 hO1 hI1 ⟨ct, hctT, hct1.symm⟩ + have hF : FK σ σ' := ((fk_setThreadState _ _).of_eq he1).trans ((fk_invokeSchedControlConfigureFlags _).of_eq he2) + have hwT := WritesIn.tfoot_of_kfoot hw + -- the dead set: Configure cuts links only + have hL : LK (fun _ => False) σ σ' := (lkm_setThreadState _ _ σ _ hm1).trans (lkm_configure _ σ1 _ hm2) + have hP : ∀ p x, p = ct.bv → σ.kheap p = some (.tcb x) → Fl4Spec.runnable x.tcbState = true := by + rintro p x rfl hk; rw [hkc] at hk; cases hk; exact hrun + have hDead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_step hX' hwT hK hP hL hF hAI'.1 hY' ((hSOK.ykeep rfl).dk q.bv) (fun h => h) (J.jcov.dead q hq) + have hCR'' : CurRun σ' := curRun_sc_step hkc hcur he1 (NKT.of_eq (nkt_invokeSchedControlConfigureFlags _) he2) + have J' := J.sc_frame hwT hK hX' hCR' hRF' hY' hM' (J.sc_buf hwT hK) hRP' (J.sc_apart hwT hK) + (J.sc_jpl hwT hK (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) (J.sc_jcov hwT hDead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) hKd subset_rfl) (J.sc_kern hwT hK hF hX' hDbg fun _ h => h) + (J.sc_utc hwT hK hDbg) hN' hI' hC' (Or.inl (hC'.elim hOw' (curOwn_of_curInv hX'.fin.1.1.1 hI'))) hO' hAI' hB' (Or.inr (Or.inl hCR'')) hBlk' + hKd (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 J.zD + have k1 := NKT.of_eq (nkt_invokeSchedControlConfigureFlags _) he2 + have hctHeap : σ1.kheap ct.bv = some (.tcb { xc with tcbState := .restart }) := by + rw [hσ1, stsAct_kheap] + simp [updK] + have hNatK : NatK σ σ' := natK_tkw hK (by + rintro p rfl y hy hvm + obtain ⟨y', hk', _, hState⟩ := k1.tcb hctHeap + rw [hk'] at hy + cases hy + rcases hState with he | ⟨hfalse, -⟩ + · rw [he] at hvm + cases hvm + · exact hfalse) + have F : NatFoot (L := L) Z s σ s' σ' := + NatFoot.ofT J.xtop hwT (fun _ h => J.tfoot_nh h) hNatK + have JV := J0.ofNat (V' := Z.vs) J' F + refine Or.inr ⟨.base (.invokeSchedControl _), DecOut.of_eq (run_liftME_ok (run_liftME_ok hdec)), + σ1, by rw [hcur]; exact he1, (.ok [], σ'), ?_, Or.inl ⟨hr1, rfl, JV.anyX arI⟩⟩ + change (Except.ok [], σ') ∈ ((do + Fl4Spec.NonDetE.liftE (ε := Unit) (Fl4Spec.invokeSchedControlConfigureFlags (Z := Unit) + (.invokeSchedControlConfigureFlags q b per (k + Fl4Spec.MIN_REFILLS) badge flag)) + Fl4Spec.NonDetE.returnOk ([] : List Fl4Spec.Data)).run σ1).1 + refine mem_bindE_ok (a := ()) (s' := σ') ?_ ?_ + · rw [run_liftE' he2]; exact Set.mem_singleton _ + · rw [run_returnOk]; exact Set.mem_singleton _ + + +/-- ConfigureFlags on the selected SchedControl route. Its first-phase +rejection and other decoder errors keep the VM state. [S] The reached cap's +core is the local core for the native Configure proof. -/ +theorem VmEptState.schedControlDecode + {MR : A → Fl4Spec.MachineState → Prop} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hAK : ARKeep arI AR) (hT : TimeAgree arI) + (lbl cpu len slot buf ct : U64) (hcpu : cpu = Z.c) + (hct : sched.cur_thread st s = ok ct) + (args : List Fl4Spec.Data) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) v, (args.map fun d => (⟨d⟩ : U64))[i.val]? = some v → + msg.syscall_arg st s ct buf i = ok v) + (ex : Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (he : ExtraRel Z.th (L.mem s) (exAt ex 0) (exs.map Prod.fst)) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (buffer : Option Fl4Spec.ObjRef) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len slot (.SchedControl cpu) blk call don fp buf ex ⦃ r => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args ci sl (.SchedControl cpu) exs buffer) s r ⦄ := by + have B := J.1.1 + have hSlot : exAt ex 0 ≠ 0#u64 → IsSlot Z.th (exAt ex 0) := by + intro h + rcases he with ⟨e, -⟩ | ⟨-, hx, -⟩ + · exact (h e).elim + · exact hx + have hRest := B.scCtlStepP1 hAT hni hct (lbl := lbl) (length := len) (blk := blk) + (call := call) (don := don) (fp := fp) (buf := buf) (slot := slot) hcpu he + rw [← zv_map_mk args] + exact sched_controlVmStep s a σ J hni hT lbl lbl.bv (schedControl_label lbl) cpu len buf ct hcpu hct + (args.map fun d => (⟨d⟩ : U64)) (by simpa using hlen) hargs ex (exAt ex 0) (exAt_idx0 ex) exs he + (fun hn _ _ hc => (B.scCtlExtra (hSlot hn) hc).1) + (fun hn _ _ hc => (B.scCtlExtra (hSlot hn) hc).2.1) + (fun hn _ _ hc => (B.scCtlExtra (hSlot hn) hc).2.2) + slot blk call don fp ci sl buffer (decode_sched_control_keepAR L.total hAT hAK) hRest on rc + +end +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Selected.lean b/proofs/Fl4Proofs/P3/VmEntryS3Selected.lean new file mode 100644 index 000000000..639f85cc5 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Selected.lean @@ -0,0 +1,174 @@ +import Fl4Proofs.P3.VmEntryS3ThrDisp +import Fl4Proofs.P3.VmEntryS3Dispatch +import Fl4Proofs.P3.VmEntryS3Untyped +import Fl4Proofs.P3.VmEntryS3Tdx +import Fl4Proofs.P3.VmEntryS3Pml4 +import Fl4Proofs.P3.VmEntryS3FrameBatch +import Fl4Proofs.P3.VmEntryS3Frame +import Fl4Proofs.P3.VmEntryS3Tables +import Fl4Proofs.P3.VmEntryS3Thread +import Fl4Proofs.P3.VmEntryS3Sc +import Fl4Proofs.P3.VmEntryS3Domain +import Fl4Proofs.P3.VmEntryS3IoControl +import Fl4Proofs.P3.VmEntryS3SendF +import Fl4Proofs.P3.VmEntryS3ReplyArm +import Fl4Proofs.P3.VmEntryS3ReplyRecv +import Fl4Proofs.P3.VmEntryS3Irq +import Fl4Proofs.P3.VmEntryS3IrqCtl +import Fl4Proofs.P3.VmEntryS3AsidControl + +/-! +The selected-cap dispatcher for the VM-aware Endpoint, Reply, IrqHandler, CNode, Untyped, Thread +(every label), ASID-pool, Notification, I/O-port and SchedControl routes, and +every PML4 label including FrameBatch on a mapped PML4, the ASID control cap (MakePool, both the clean and the dirty region) and the IRQ control cap +([J] `valid_irq_handlers`, [P] `ArchIrqRel` at the dispatch state). + +[J] The existing current-thread, fault and retype premises are at the +reached dispatch state. [S] The K1 and K4 conditions refer to its +successful lookup. [P] The same per-operation platform agreements used +by their VM and native proofs. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} + +/-- The selected routes whose decoder uses the shared VM producers. -/ +def SelectedVmDecoder (fp : Bool) (lbl : U64) : capability.Cap → Prop + | .CNode .. | .Untyped .. | .AsidPool .. | .Notification .. | .IoPort .. + | .SchedControl .. | .SchedContext .. | .TdxControl | .IoPortControl | .Domain + | .Null | .Zombie .. | .Endpoint .. | .Reply .. | .IrqHandler .. | .IrqControl | .AsidControl => True + | .Pml4 .. | .PageTable .. | .PageDirectory .. | .Pdpt .. => True + | .Frame p sz rt d m ep asid va => NativeCap (.Frame p sz rt d m ep asid va) + | .Thread .. => True + | _ => False + +set_option maxHeartbeats 8000000 in +/-- Compose the selected-cap leaves with the reads from this invocation. -/ +theorem VmEptState.dispatchKnownSelected [PlainStore st] + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hK : ARKeep arI AR) (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) + (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hVal : PortInValAt arI a σ) + (hM : MachPlat arI MR) (hPA : PPPlatA arI AR) (hT : TimeAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hmb : ∀ a', sporadic.min_budget arI a' ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hticks : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ a' v, arI.ticks_to_us a' v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hag : ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hrcb : Fl4Spec.resetChunkBits = 16) + (hsg : SpecGlob σ) (hPorts : SpecIoPorts σ) (hVIH : SInvV.ValidIrqHandlers σ) + (hAR : ArchIrqRel arI a σ) (hmv : MRV MR a σ) (hQ : ∀ e, EpQPre σ e) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d prio, σ.curThread ∉ σ.readyQueues d prio) + (hnf : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none) + (hUZ : SInvR.G5.SpecUtZ σ) (hDF : SInvDV.DevRangeS σ) (hDP : SInvGp.GpmOutV σ) (hUt : SInvU.UtInv'' σ) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (call blk don fp : Bool) (cptr : U64) (hCore : SelectedCoreScope (L.cpu s) (L.mem s) σ ct cptr) : + VDispReachAtFor (L := L) arI AR MR (SelectedVmDecoder fp) on rc Z s a σ ct call blk don fp cptr := by + intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hlook hr he hcRoute + have B := J.1.1 + obtain ⟨hlbl, q⟩ := B.selectedDecode hct hinfo hmi hbuf hbufS hr2 her hmrs hsl hc hlook hr he + have hcore : L.cpu s = Z.c := by + obtain ⟨_, _, hC0, -⟩ := J.1.1.xtop.fin.1.1.1.lists + exact hC0.cur + rw [hcore] at hCore + have hC := hCore sl c hlook hc + rw [← hlbl] + cases c <;> try exact hcRoute.elim + case Null => + exact WP.spec_mono (J.selectedNullZombie q (Or.inl rfl) buffer on rc blk call don fp) fun _ h => Or.inl h + case Zombie p zt n => + exact WP.spec_mono (J.selectedNullZombie q (Or.inr ⟨p, zt, n, rfl⟩) buffer on rc blk call don fp) fun _ h => Or.inl h + case CNode p r gs g => + by_cases h : 25 ≤ info.label.val ∧ info.label.val ≤ 32 + · exact WP.spec_mono (J.selectedCNode hni hRef hVA hAT hKDAR hK hticks hnd hM.toC hKD hPA hAM hsg hmv + hQ hRd0 hnq h q buffer on rc blk call don fp) fun _ post => Or.inl post + · exact WP.spec_mono (J.selectedCNodeOutside h q buffer on rc blk call don fp) fun _ post => Or.inl post + case Thread p => + cases fp + · by_cases h : info.label = fl4_abi.label.TCBSetEPTRoot + · rw [h] + exact WP.spec_mono (J.selectedSetEpt hni hRef hVA q buffer on rc blk call don) fun _ post => Or.inl post + · have hmrs' : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = + ({((args.map fun x => (⟨x⟩ : U64)).map UScalar.bv, σ)}, False) := by + rw [show (args.map fun x => (⟨x⟩ : U64)).map UScalar.bv = args from zv_map_mk args]; exact hmrs + have hA5 := (B.argsJ B.xferH.1 B.xferH.2.1 hct hinfo hmi hbuf hbufS hmrs').2.2 + exact J.selectedThreadS3 hni hRef hVA hK hKDAR hKD hAT hAM hM hPA hT hnd hmb hticks hsg hmv hRd0 hnq q + hbuf (msgLen_le120 hinfo buf) buffer hA5 h on rc blk call don + · exact WP.spec_mono (J.selectedThreadFirst q buffer on rc blk call don) fun _ post => Or.inl post + case AsidPool p base => + exact WP.spec_mono (J.selectedAsidPool hni hRef hVA q buffer on rc blk call don fp) fun _ post => Or.inl post + case Notification n badge send recv => + exact WP.spec_mono (J.selectedNotificationS3 hni hAT hVA hK hmb q buffer hO blk call don fp) + fun _ post => Or.inl post.1 + case IoPort first last owner => + exact J.selectedIoPortS3 hni hRef hVA hAT hAM hVal q hRun buffer on rc blk call don fp + case SchedControl cpu => + exact WP.spec_mono (J.selectedSchedControl hni hAT hK hT q hC buffer on rc blk call don fp) fun _ post => Or.inl post + case SchedContext p sb => + exact J.selectedSchedContextS3 hni hAT hK hT hticks.1 (hticks.2 a) q buffer on rc blk call don fp + case Domain => + exact WP.spec_mono (J.selectedDomainS3 hni hnd q buffer on rc blk call don fp) fun _ post => Or.inl post + case IoPortControl => + exact WP.spec_mono (J.selectedIoPortControlS3 hni hsg hPorts q buffer on rc blk call don fp) fun _ post => Or.inl post + case AsidControl => + exact WP.spec_mono (J.selectedAsidControlS3 hni q buffer on rc blk call don fp) fun _ post => Or.inl post + case IrqControl => + exact WP.spec_mono (J.selectedIrqControlS3 hni hAM hsg hVIH hAR q buffer on rc blk call don fp) + fun _ post => Or.inl post + case TdxControl => + exact WP.spec_mono (J.selectedTdxS3 hni hAT hKDAR q buffer on rc blk call don fp) + fun _ post => Or.inr post + case Pml4 p mapped asid => + by_cases hb : info.label = fl4_abi.label.X86PML4FrameBatch + · rw [hb] + cases mapped + · exact WP.spec_mono (J.selectedFrameBatchUnmappedS3 q buffer on rc blk call don fp) fun _ post => Or.inl post + · exact J.selectedFrameBatchMappedS3 hni hRef hAT hKDAR hsg q hbuf hRun (msgLen_le120 hinfo buf) + buffer on rc blk call don fp + · exact WP.spec_mono (J.selectedNativePml4NonBatchS3 hni hRef hAT hb q buffer on rc blk call don fp) + fun _ post => Or.inl post + case PageTable p mp asid va => + exact WP.spec_mono (J.selectedNativePageTableS3 hni hRef q buffer on rc blk call don fp) fun _ post => Or.inl post + case PageDirectory p mp asid va => + exact WP.spec_mono (J.selectedNativePageDirectoryS3 hni hRef q buffer on rc blk call don fp) + fun _ post => Or.inl post + case Pdpt p mp asid va => + exact WP.spec_mono (J.selectedNativePdptS3 hni hRef q buffer on rc blk call don fp) fun _ post => Or.inl post + case Frame p sz rt d m ep asid va => + exact J.selectedFrameS3 hni hRef hVA q hcRoute hRun buffer on rc blk call don fp + case Endpoint ep badge send recv g gr => + exact WP.spec_mono (J.selectedEndpointS3F hni hnq q buffer on rc blk call don fp hAT hnf) fun _ post => Or.inl post + case Reply p g => + exact WP.spec_mono (J.selectedReplyS3 hni hAT hticks.1 hticks.2 hmb + (runTmoP_recvV (L := L) (VX := V) (XX := X) (XL := XL) (AR := AR) arI Z hAT hticks.1 hticks.2) + q buffer on rc blk call don fp) fun _ post => Or.inl post + case IrqHandler irq => + have hN : ExtraNamed Z.th (L.mem s) ex := by + obtain ⟨hcur, -⟩ := B.curEq hct + have h := lookup_extra_named Z.th s σ B.xtop.rel.2.1.1 B.xtop.rel.2.1.2.1 (B.xtop.curTcb hcur).1 buf info + rw [hr2, WP.spec_ok] at h + exact h _ hr + exact WP.spec_mono (J.selectedIrqHandlerS3 hni hAT hAM hM.toC hsg hmv hRd0 hnq q hN buffer on rc blk call don fp) + fun _ post => Or.inl post + case Untyped p sb fi dev => + exact WP.spec_mono (J.selectedUntyped hni hRef hVA hAT hag hCap q hC hfan (B.regionUnrefAt hni) + (fun _ => B.utZeroOfSpec hUZ) (fun _ => B.utNodesOfJoint) (fun _ => B.devFreeOfSpec hDF) + (fun _ => B.devGpmOfSpec hDP hUt) hK hnd hM hPA hrcb hRd0 hnq + (B.vsReachSelected hni q.current q.absSlot q.capAt) hmv buffer on rc blk call don fp) fun _ post => Or.inl post + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Send.lean b/proofs/Fl4Proofs/P3/VmEntryS3Send.lean new file mode 100644 index 000000000..b4c8beecc --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Send.lean @@ -0,0 +1,436 @@ +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.XTopJointSendBlock +import Fl4Proofs.P3.IpcSendTop +import Fl4Proofs.P3.NatFoot + +/-! +Selected Endpoint invocation at the shared joint state. Idle and sending +endpoints need no callback. The waiting branch reads the actual endpoint +queue and reaches the selected receiver before its transfer obligation. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- Blocking send at the VM-compatible joint state, with the native frame. -/ +theorem JAtZB.sendBlockFoot (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hrs : xt.tcbState = .restart) + (hct : σ.curThread = t.bv) (hres : σ.schedulerAction = .resumeCurThread) + (hti : σ.curThread ≠ σ.idleThread) (hnq : ∀ d p, t.bv ∉ σ.readyQueues d p) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock false x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + (call : Bool) (b : U64) (g gr don : Bool) : + endpoint.send_ipc st arI s a true call b g gr don t e ⦃ r => r.2 = a ∧ + ∃ σ', Fl4Spec.sendIpc (Z := Unit) true call b.bv g gr don t.bv e.bv σ = ({((), σ')}, False) ∧ + JAtZB (L := L) arI AR Z H Cs Ac Lst (D \ {e}) r.1 r.2 σ' ∧ + NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hC : CurInv σ := J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hr : Fl4Spec.runnable xt.tcbState = true := by rw [hrs]; rfl + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by rw [hrs]; decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [hrs] at h; cases h + have htq : t.bv ∉ x.epQueue := Endp.not_queued hI.1.toE ht hkt hnb e he x hk + have hq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, -, hact, hcn, hsrt⟩ := + sendIpcBlock_eq hkt hk hc htq hq hr (J.isorted.1 _ _ hk) call b.bv g gr don + have hcn' : σ'.schedulerAction = .chooseNewThread := hcn hct.symm hres + -- P1's `CanBlock` of the current thread + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t := by + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have h3 := (hI.1.toN.at_tcb ht hkt).st + have hsw : L.mem s (P1.stA t) = sched.TS_RESTART := by + rw [P1.stA_eq, ← tStA_eq]; apply UScalar.eq_of_val_eq; rw [h3, hrs, tsVals.2.1]; rfl + have ha0 : L.actionW s = 0#u64 := by + have h := hS.act + rw [hres] at h + unfold P1.LawfulStore.actionW; rw [hcq.cur]; exact h + have hcw : L.curW s = t := by + apply P1.eq_of_bv + rw [← hct, hs.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq.cur] + refine ⟨htH, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [hsw]; unfold P1.Blocked; rw [tsVals.2.1, tsVals.2.2.2.1, tsVals.2.2.1, tsVals.2.2.2.2]; decide + · rw [hsw, tsVals.2.1]; unfold sched.TS_BLOCKED_ON_REPLY; decide + · by_contra hq' + have hm := (hcq.queued t ht).1 hq' + exact hnq 0 _ (by rw [hs.rq _ (hcq.prio t ht), mem_map_bv]; exact hm) + · by_contra hq' + have hm := (hcq.released t ht).1 hq' + exact hC.notRel (by rw [hct, hs.rel, mem_map_bv]; exact hm) + · rw [ha0]; intro h; exact (hcq.tcbs t ht).ne0 (by rw [← h]; rfl) + · intro h + have ei : L.idleW s = Sched.nodeW st s Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW Sched.nodeW; rw [hcq.cur] + exact hti (by rw [hct, hs.idleTh, ← ei, h]) + · exact fun h1 => hB0.sact t htH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨ha0, hcw⟩))))) h1 + have hHe : H e = some .ep := J.zH.2.1 e he + have hst0 : L.epState s e = 0#u64 ∨ L.epState s e = 1#u64 := by + have hx := hI.1.toE.at_ep he hk + rw [epState_eq] + cases x with + | idleEP => exact Or.inl hx.1 + | sendEP q => exact Or.inr hx.1 + | recvEP q => simp [Endp.CanBlock] at hc + have p1b := P1.endpoint.send_ipc.block arI s a true call b g gr don t e Q0 R0 h0 hB0 hX0 hS0 hSg0 + (fun _ => hcb) hHe hst0 + have p1f : endpoint.send_ipc st arI s a true call b g gr don t e ⦃ s' _ => P1.Foot st (P1.Heap.tcbs H) Cs + (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄ := + P1.endpoint.send_ipc.blockFoot arI s a true call b g gr don t e h0 (fun _ => hcb) hHe hst0 + refine WP.spec_mono (WP.spec_and (WP.spec_and (send_ipc_blockF (ar := arI) J.ip s a σ hI ht hkt hnb hnn he hk hc + (fun xs hq => Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted e (Or.inl he) xs hq) call b g gr don) p1b) p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW⟩, hbk⟩, hfoot⟩ + simp only [WP.uncurry'_pair] at ea hbk hfoot hIR hW + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + -- the assembly (as XTopJointCancel's `JAtZ.kfrStep`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := ⟨fun u hu => (by rw [hcn'] at hu; cases hu), F.idleOK J.act.2⟩ + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcn' + have hrun3 : CurRunM σ' := Or.inl (by rw [hcn']; intro h; cases h) + -- the dead: the thread is not dead (`Restart`); the endpoint leaves the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rw [hrs]; simp [tsCode]) + have hdead : ∀ q ∈ D \ {e}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqe : q.bv ≠ e.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {e}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {e}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) + (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from `Restart`) + have hnqT : NotQueued σ t.bv := ⟨hnq, by rw [← hct]; exact hC.notRel⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + -- `BlockedScActive`: the thread's SC is the current SC, active + have hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp).2⟩ + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp hyt + rw [hkt] at hku; cases hku + obtain ⟨xc, hkc, hcs, -⟩ := hC.tcb + rw [hct, hkt] at hkc; cases hkc + have e1 : σ.curSc = q.bv := Option.some.inj (hcs.symm.trans hxs) + rw [← e1] at hp + exact hC.active (J.curSc_ne_idle hC hti) y n hp + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {e}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + refine ⟨rfl, σ', heq, J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) + (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) + F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (J.zD_diffE he), ?_⟩ + have hK : NatK σ σ' := natK_kh (R := BChg t.bv e.bv) (by + intro p o o' h + rcases h with ⟨x, ep, d, rfl, rfl, -, -⟩ | ⟨x, y, rfl, rfl, -⟩ + · exact zvChg_tcb (fun h => by cases h) + · exact zvChg_ep) F.kh + exact NatFoot.ofWS hF.1 hW (fun _ h => Or.inl (Ipc.CnW.oobj J.xtop.fin.1.1.1 h)) + (fun _ h => J.cnW_nv h) (fun _ h => J.cnW_nh h) hK + +/-- The concrete branch after reading a receiving endpoint's queue at its head. -/ +def sendWaitingBody (st : machine.Store S) (arI : machine.Arch A) (s : S) (a : A) + (do_call : Bool) (badge : U64) (can_grant can_grant_reply can_donate : Bool) + (thread ep_ptr receiver : U64) : Result (S × A) := do + let s1 ← endpoint.ep_dequeue st s ep_ptr receiver + let s2 ← + ipc.do_ipc_transfer st arI s1 a thread ep_ptr + badge can_grant receiver + let i ← obj.Tcb.reply_obj st receiver s2 + let s3 ← + if i != 0#u64 + then ipc.reply_unlink st s2 i receiver + else ok s2 + let (s4, a1) ← + if do_call + then + if i != 0#u64 + then + if can_grant + then + ipc.reply_push st arI s3 a thread + receiver i can_donate + else + if can_grant_reply + then + ipc.reply_push st arI s3 a thread + receiver i can_donate + else + do + let s5 ← + sched.set_thread_state st s3 thread + sched.TS_INACTIVE + ok (s5, a) + else + do + let s5 ← + sched.set_thread_state st s3 thread + sched.TS_INACTIVE + ok (s5, a) + else + do + let i1 ← obj.Tcb.fault_type st thread s3 + if i1 != fl4_abi.consts.fault.NullFault + then + if i != 0#u64 + then + if can_grant + then + ipc.reply_push st arI s3 a thread + receiver i can_donate + else + if can_grant_reply + then + ipc.reply_push st arI s3 a thread + receiver i can_donate + else + let s5 ← + sched.set_thread_state st s3 thread + sched.TS_INACTIVE + ok (s5, a) + else + let s5 ← + sched.set_thread_state st s3 thread + sched.TS_INACTIVE + ok (s5, a) + else + if can_donate + then + let i2 ← obj.Tcb.sc st receiver s3 + if i2 = 0#u64 + then + let i3 ← obj.Tcb.sc st thread s3 + schedcontext.donate st arI s3 a i3 + receiver + else ok (s3, a) + else ok (s3, a) + let s5 ← + sched.set_thread_state st s4 receiver sched.TS_RUNNING + let s6 ← endpoint.unblock_sporadic st s5 receiver + let s7 ← sched.possible_switch_to st s6 receiver + ok (s7, a1) + +/-- [J] The pending transfer at the actual head of a selected receiving +endpoint. The state's restart and the endpoint and receiver reads have +already matched. -/ +def SendWaitingVE (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (ct ep badge : U64) (blk call g gr don : Bool) : Prop := + ∀ (d : U64) (queue : List Fl4Spec.ObjRef) (receiver sender : Fl4Spec.Tcb) (reply : Option Fl4Spec.ObjRef) + (data : Fl4Spec.ReceiverPayload), + ct ∈ Z.T → ep ∈ Z.E → d ∈ Z.T → σ.curThread = ct.bv → + σ.kheap ct.bv = some (.tcb sender) → sender.tcbState = .restart → + σ.kheap ep.bv = some (.endpoint (.recvEP (d.bv :: queue))) → + (L.epQ s ep).head = d → σ.kheap d.bv = some (.tcb receiver) → + receiver.tcbState = .blockedOnReceive ep.bv reply data → + sendWaitingBody st arI s a call badge g gr don ct ep d ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk call badge.bv g gr don ct.bv ep.bv σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ + +set_option maxHeartbeats 12000000 in +/-- Match the endpoint-state and queue-head reads before the waiting transfer. -/ +theorem VmEptState.sendWaitingV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct ep badge : U64} {blk call g gr don : Bool} + (ht : ct ∈ Z.T) (he : ep ∈ Z.E) (hcur : σ.curThread = ct.bv) + {sender : Fl4Spec.Tcb} (hks : σ.kheap ct.bv = some (.tcb sender)) (hr : sender.tcbState = .restart) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap ep.bv = some (.endpoint (.recvEP queue))) + (hTail : SendWaitingVE (L := L) arI AR Z s a σ ct ep badge blk call g gr don) : + endpoint.send_ipc st arI s a blk call badge g gr don ct ep ⦃ out => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk call badge.bv g gr don ct.bv ep.bv σ).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨hst, qs, hqs, hq, hQ, hB⟩ := hI.1.toE.at_ep he hk + obtain ⟨d, ds, rfl⟩ := List.exists_cons_of_ne_nil hqs + have hdT : d ∈ Z.T := hQ.mem d (by simp) + obtain ⟨xd, hkd, hxd⟩ := hI.1.tcb d hdT + have hx := hI.1.toE.at_tcb hdT hkd + obtain ⟨h3, hback⟩ := hB d (by simp) + obtain ⟨p, reply, data, hrecv⟩ := tsCode_three (hx.st.symm.trans h3) + have hp : p = ep.bv := by + have h := hx.blk p (by rw [hrecv]; rfl) + rw [hback] at h + exact h.symm + subst p + have hhead : (L.epQ s ep).head = d := by rw [epQ_eq]; exact hQ.head + have heq : σ.kheap ep.bv = some (.endpoint (.recvEP (d.bv :: ds.map UScalar.bv))) := by + rw [hq] at hk + exact hk + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s ep) ?_ + rintro _ rfl + rw [epState_eq, hst] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s ep) ?_ + rintro _ rfl + rw [hhead] + exact hTail d (ds.map UScalar.bv) xd sender reply data ht he hdT hcur hks hr heq hhead hkd hrecv + +/-- [J] A waiting transfer reached from the same selected Endpoint cap by +the matched concrete and abstract restart. -/ +def SelectedSendWaitingVE (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) + (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) + (ct ep badge : U64) (blk call g gr don : Bool) : Prop := + ∀ V X XL Ac Lst D s1 σ1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s1 a σ1 → + SendWaitingVE (L := L) arI AR Z s1 a σ1 ct ep badge blk call g gr don + +set_option maxHeartbeats 16000000 in +/-- Invoke the selected Endpoint. The missing [J] transfer is limited to +the actual waiting receiver at the matched Restart state. -/ +theorem VmEptState.selectedEndpointS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ dm prio, σ.curThread ∉ σ.readyQueues dm prio) + {ct cptr len buf lbl ep badge : U64} {send recv g gr : Bool} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Endpoint ep badge send recv g gr) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don fp : Bool) + (hWait : send = true → SelectedSendWaitingVE (L := L) arI AR Z H Cs s a σ ct ep badge blk call g gr don) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) + (.Endpoint ep badge send recv g gr) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Endpoint ep badge send recv g gr) exs buffer) s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases send + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + exact run_liftME_err (specDec_endpoint fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl ep badge false recv g gr exs buffer σ) + simp only [↓reduceIte] + obtain ⟨s1, σ1, Lst1, x, hcur, hk, hs1, hst, J1, eσ1, hslots, _, hni1⟩ := J.restartK hni + have hct : ct = L.curW s := eq_of_ok_eq (q.current.symm.trans (cur_thread_eq s)) + rw [← hct] at hcur hk hs1 eσ1 + rw [hs1, bind_ok] + have B := J.1.1 + have B1 := J1.1.1 + have ht : ct ∈ Z.T := B.thT ct (B.xtop.curTcb hcur).1 + have he := B.slotE q.absSlot q.capAt + have hcur1 : σ1.curThread = ct.bv := by + rw [eσ1]; unfold Notif.stsAct; split <;> exact hcur + have hk1 : σ1.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + rw [eσ1, stsAct_kheap] + simp [Notif.updK] + have hrq1 : σ1.readyQueues = σ.readyQueues := by + rw [eσ1]; unfold Notif.stsAct; split <;> rfl + have hnq1 : ∀ dm prio, ct.bv ∉ σ1.readyQueues dm prio := by + rw [hrq1, ← hcur] + exact hnq + have hI := B1.xtop.irelK B1.ks B1.buf + obtain ⟨xe, hke, -⟩ := hI.1.ep ep he + have hsend : endpoint.send_ipc st arI s1 a blk call badge g gr don ct ep ⦃ out => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk call badge.bv g gr don ct.bv ep.bv σ1).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + by_cases hcb : Endp.CanBlock false xe + · cases blk + · refine WP.spec_mono (send_ipc_noblock (ar := arI) s1 a σ1 hI.1.toE he hke hcb call badge g gr don ct) ?_ + rintro r ⟨rfl, h2⟩ + exact ⟨((), σ1), by rw [h2]; exact Set.mem_singleton _, J1.anyX arI⟩ + · have hc1 : capAt (L.mem s1) (cslotAddr sl) = ok (.Endpoint ep badge true recv g gr) := by + rw [hslots.capAt q.absSlot.isSlot] + exact q.capAt + have hHe : H ep = some .ep := B1.zH.2.1 ep he + have hfit := B1.kern.heap.fits ep _ hHe + simp only [P1.HKind.bits] at hfit + have hcov := B1.cover_cap (P1.WritesIn.refl s1 _) q.absSlot.isSlot hc1 + (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl (range_ep ep badge true recv g gr hfit) + fun k hk => by + rw [hHe] at hk; cases hk + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + refine WP.spec_mono (B1.sendBlockFoot ht hk1 rfl hcur1 hni1.2 hni1.1 hnq1 he hke hcb hcov call badge g gr don) ?_ + rintro out ⟨-, σ2, hs, B2, F⟩ + exact ⟨((), σ2), by rw [hs]; exact Set.mem_singleton _, (J1.ofNat (V' := Z.vs) B2 F).anyX arI⟩ + · cases xe with + | idleEP => exact False.elim (hcb trivial) + | sendEP xs => exact False.elim (hcb rfl) + | recvEP xs => + exact J1.sendWaitingV ht he hcur1 hk1 rfl hke (hWait rfl _ _ _ _ _ _ s1 σ1 hs1 hst J1) + refine WP.spec_bind hsend ?_ + rintro ⟨s2, a2⟩ ⟨z, hz, J2⟩ + apply (WP.spec_ok _).2 + refine Or.inr ⟨.base (.invokeEndpoint ep.bv badge.bv g gr), ?_, σ1, hst, + (.ok [], z.2), ?_, Or.inl ⟨rfl, rfl, J2⟩⟩ + · exact (DecOut.of_eq + (specDec_endpoint fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl ep badge true recv g gr exs buffer σ)).liftME + · rw [← hcur1] at hz + exact mem_perform_endpoint hz + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3SendF.lean b/proofs/Fl4Proofs/P3/VmEntryS3SendF.lean new file mode 100644 index 000000000..b809105ea --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3SendF.lean @@ -0,0 +1,140 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **the send to a waiting receiver at `VmEptState`**. + +* **`VmEptState.sendFootV`**: `SendFootPost` (P3/VmEntryS3Xfer: the transfer's base step, the `JAtZB` there and at the end, the + native footprint from the base) gives `VEAnyX`: the record at the base by `VmEptState.xferBaseV` (the cap insert's + `ofCn`), then `VmEptState.ofNat` across the post-base native phases. +* **`VmEptState.sendWaitingFootV`**: `endpoint.send_ipc` to a waiting receiver at the restarted state, the five native + cases (`jSendRest_ofF`, P3/P3JSendStF). +* **`VmEptState.selectedEndpointS3F`**: `selectedEndpointS3` without `hWait` (`SelectedSendWaitingVE`). + +Premises: `P0.ArchTotal` [P]; the current thread's `tcbFault = none` (`hnf`, the projection of the carried dispatch +invariant `SInvB`, as every native send). No other premise. New file. +-/ +import Fl4Proofs.P3.P3JSendStF +import Fl4Proofs.P3.VmEntryS3Send +import Fl4Proofs.P3.VmEntryS3Xfer +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`SendFootPost` gives the record** (module docstring). -/ +theorem VmEptState.sendFootV [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {r : S × A} + {σ' : Fl4Spec.State Unit} (h : SendFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r σ') : + VEAnyX (L := L) arI AR r.1 r.2 σ' := by + obtain ⟨y, σy, G', hB, hcn, Jy, V2, Ac2, Lst2, D2, J', F⟩ := h + exact ((J.xferBaseV Jy hB hcn).ofNat (V' := V2) J' F).anyX arI + +set_option maxHeartbeats 16000000 in +/-- **A send to a waiting receiver at the restarted state** (module docstring). -/ +theorem VmEptState.sendWaitingFootV [VxE Z] (hAT : P0.ArchTotal arI) + (J1 : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {ct ep : U64} (ht : ct ∈ Z.T) (he : ep ∈ Z.E) + (hcur : σ.curThread = ct.bv) {xt : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb xt)) + (hrs : xt.tcbState = .restart) (hft : xt.tcbFault = none) (hres : σ.schedulerAction = .resumeCurThread) + (hti : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm q, ct.bv ∉ σ.readyQueues dm q) + {x : Fl4Spec.Endpoint} (hke : σ.kheap ep.bv = some (.endpoint x)) (hcb : ¬ Endp.CanBlock false x) + (blk call : Bool) (badge : U64) (g gr don : Bool) : + endpoint.send_ipc st arI s a blk call badge g gr don ct ep ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk call badge.bv g gr don ct.bv ep.bv σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + refine WP.spec_mono (jSendRest_ofF hAT J1.1.1 ht he hk hrs hft hcur hres hti hnq blk call badge g gr don + x hke hcb) ?_ + rintro r ⟨z, hz, hF⟩ + exact ⟨z, hz, J1.sendFootV hF⟩ + +set_option maxHeartbeats 16000000 in +/-- Invoke the selected Endpoint (`VmEptState.selectedEndpointS3` with the waiting transfer proved: `hWait` is gone, replaced +by the platform record `hAT` and the carried no-fault fact of the current thread `hnf` (`SInvB`'s `sinvB_disp` projection)). -/ +theorem VmEptState.selectedEndpointS3F [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ dm prio, σ.curThread ∉ σ.readyQueues dm prio) + {ct cptr len buf lbl ep badge : U64} {send recv g gr : Bool} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Endpoint ep badge send recv g gr) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don fp : Bool) + (hAT : P0.ArchTotal arI) + (hnf : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) + (.Endpoint ep badge send recv g gr) blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Endpoint ep badge send recv g gr) exs buffer) s res ⦄ := by + rw [syscall.decode_invocation, q.current, bind_ok] + cases send + · simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine Or.inl ⟨_, rfl, rfl, ⟨.invalidCapability 0, ?_, Or.inr ⟨0#u64, rfl, rfl⟩⟩, J.anyX arI⟩ + exact run_liftME_err (specDec_endpoint fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl ep badge false recv g gr exs buffer σ) + simp only [↓reduceIte] + obtain ⟨s1, σ1, Lst1, x, hcur, hk, hs1, hst, J1, eσ1, hslots, _, hni1⟩ := J.restartK hni + have hct : ct = L.curW s := eq_of_ok_eq (q.current.symm.trans (cur_thread_eq s)) + rw [← hct] at hcur hk hs1 eσ1 + rw [hs1, bind_ok] + have B := J.1.1 + have B1 := J1.1.1 + have ht : ct ∈ Z.T := B.thT ct (B.xtop.curTcb hcur).1 + have he := B.slotE q.absSlot q.capAt + have hcur1 : σ1.curThread = ct.bv := by + rw [eσ1]; unfold Notif.stsAct; split <;> exact hcur + have hk1 : σ1.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + rw [eσ1, stsAct_kheap] + simp [Notif.updK] + have hrq1 : σ1.readyQueues = σ.readyQueues := by + rw [eσ1]; unfold Notif.stsAct; split <;> rfl + have hnq1 : ∀ dm prio, ct.bv ∉ σ1.readyQueues dm prio := by + rw [hrq1, ← hcur] + exact hnq + have hI := B1.xtop.irelK B1.ks B1.buf + obtain ⟨xe, hke, -⟩ := hI.1.ep ep he + have hsend : endpoint.send_ipc st arI s1 a blk call badge g gr don ct ep ⦃ out => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk call badge.bv g gr don ct.bv ep.bv σ1).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + by_cases hcb : Endp.CanBlock false xe + · cases blk + · refine WP.spec_mono (send_ipc_noblock (ar := arI) s1 a σ1 hI.1.toE he hke hcb call badge g gr don ct) ?_ + rintro r ⟨rfl, h2⟩ + exact ⟨((), σ1), by rw [h2]; exact Set.mem_singleton _, J1.anyX arI⟩ + · have hc1 : capAt (L.mem s1) (cslotAddr sl) = ok (.Endpoint ep badge true recv g gr) := by + rw [hslots.capAt q.absSlot.isSlot] + exact q.capAt + have hHe : H ep = some .ep := B1.zH.2.1 ep he + have hfit := B1.kern.heap.fits ep _ hHe + simp only [P1.HKind.bits] at hfit + have hcov := B1.cover_cap (P1.WritesIn.refl s1 _) q.absSlot.isSlot hc1 + (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl (range_ep ep badge true recv g gr hfit) + fun k hk => by + rw [hHe] at hk; cases hk + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + refine WP.spec_mono (B1.sendBlockFoot ht hk1 rfl hcur1 hni1.2 hni1.1 hnq1 he hke hcb hcov call badge g gr don) ?_ + rintro out ⟨-, σ2, hs, B2, F⟩ + exact ⟨((), σ2), by rw [hs]; exact Set.mem_singleton _, (J1.ofNat (V' := Z.vs) B2 F).anyX arI⟩ + · cases xe with + | idleEP => exact False.elim (hcb trivial) + | sendEP xs => exact False.elim (hcb rfl) + | recvEP xs => + exact J1.sendWaitingFootV hAT ht he hcur1 hk1 rfl (hnf x (by rw [hcur]; exact hk)) hni1.2 hni1.1 hnq1 hke + hcb blk call badge g gr don + refine WP.spec_bind hsend ?_ + rintro ⟨s2, a2⟩ ⟨z, hz, J2⟩ + apply (WP.spec_ok _).2 + refine Or.inr ⟨.base (.invokeEndpoint ep.bv badge.bv g gr), ?_, σ1, hst, + (.ok [], z.2), ?_, Or.inl ⟨rfl, rfl, J2⟩⟩ + · exact (DecOut.of_eq + (specDec_endpoint fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl ep badge true recv g gr exs buffer σ)).liftME + · rw [← hcur1] at hz + exact mem_perform_endpoint hz + + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Start.lean b/proofs/Fl4Proofs/P3/VmEntryS3Start.lean new file mode 100644 index 000000000..c7b5d4c0c --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Start.lean @@ -0,0 +1,184 @@ +import Fl4Proofs.P3.VmEntryS3TopR +import Fl4Proofs.P3.SInvXV +import Fl4Proofs.P3.VmEntryS3Budget +import Fl4Proofs.P3.VmEntryS3Untyped +import Fl4Proofs.P3.VmEntryS3Oracle +import Fl4Proofs.P3.VmRecvFin +import Fl4Proofs.P3.VmFinFrontS3 +import Fl4Proofs.P3.P3JCapDom +import Fl4Proofs.P3.XTopJointKEntry27b +import Fl4Proofs.P3.XTopJointKEntry36E +import Fl4Proofs.P3.P3JThSsp +import Fl4Proofs.P3.JBootQ1Disp +import Fl4Proofs.P3.XTopJointKEntryU14 + +/-! +P3 (line mmuAD, Stage 3, 3.4): **the start record of the syscall entry** and its spec-only transport across the +`FaultIP` write and the passing budget step (`BudgetAt σ σb true`) to `DispFactsS3` at the dispatch pair. + +* `StartS3`: the entry-state facts. `X` is the carried bundle without the native scope, `SInvXV` (it supplies `SpecGlob`, + `valid_irq_handlers`, the masks, `EpQAll`, `OffReady`, `FaultIP` TCB shape); the rest are the entry facts the + existing transports (`BudgetAt.gpmOutAndUtInv/utZeroSpec/devRange`) ask for. `air` and `core` are asked at the + dispatch pair; `core` only at the executing core, the current thread and the invoked capability pointer. +* `StartS3.idleVt`: [J] the idle vtable fact (`IdleVtS`, the native carried spec fact of the finish). +* `StartS3.dispFacts`: `DispFactsS3` at the dispatch pair. +* `StartS3.finFacts`: the finish facts at every finish state the entry reaches (`PostPrefixFinJ`), from the start + record and the entry state (P3/VmFinFrontS3). + +No named premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +open Fl4Proofs.P3.SInvR.G4 Fl4Proofs.P3.SInvR.G5 + +noncomputable section + +section Spec +open Fl4Spec + +theorem getCap_pair_iff (σ : State Unit) (o : ObjRef) (c : CnodeIndex) (a : Cap) : + getCap (Z := Unit) (o, c) σ = ({(a, σ)}, False) ↔ capOpt (σ.kheap o) c = some a := by + unfold getCap getObject + cases h : σ.kheap o with + | none => + simp [nd_bind_apply, NonDet.getsThe, readObject, h, NonDet.assertOpt, NonDet.fail, capOpt] + | some obj => + cases obj <;> simp [nd_bind_apply, NonDet.getsThe, readObject, h, NonDet.assertOpt, NonDet.fail, capOpt, + NonDet.pure, NonDet.assert] + case cnode sz cs => + by_cases hw : wellFormedCnodeN sz cs + · cases hc : cs c <;> simp [hw, hc, NonDet.pure, NonDet.fail] + · simp [hw] + case tcb t => + cases hc : tcbCnodeMap t c <;> simp [NonDet.pure, NonDet.fail] + +/-- The IO port invariant at a dispatch state (the `FaultIP` write keeps every cap, the budget step the allocated +ports). -/ +theorem BudgetAt.ioPorts (hnd : Fl4Spec.numDomains = 1) {σ σb : State Unit} {x : Tcb} + (hk : σ.kheap σ.curThread = some (.tcb x)) (h : BudgetAt σ σb true) (hP : SpecIoPorts σ) : SpecIoPorts σb := by + obtain ⟨⟨σu, hu, ebu⟩, ek, -⟩ := BudgetAt.shape27 hnd hk h + have hE := Sched.updateTimeStamp_eq' hnd + (doMachineOp_single (getCurrentTime_eq (faultIPSt σ σ.curThread x).machineState)) + rw [hE] at hu + have e : σu = _ := congrArg Prod.snd (Set.mem_singleton_iff.1 hu) + have ha : σb.archState = σ.archState := by rw [ebu, e]; rfl + intro sl f l o hg + obtain ⟨o', i⟩ := sl + have hc := (getCap_pair_iff σb o' i _).1 hg + rw [budgetAt_capOpt hnd h] at hc + obtain ⟨h1, h2⟩ := hP (o', i) f l o ((getCap_pair_iff σ o' i _).2 hc) + exact ⟨h1, fun p hp1 hp2 => by rw [ha]; exact h2 p hp1 hp2⟩ + +end Spec + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) + +/-- **The finish facts at every finish state the entry reaches**: `FinishFactsS3` (`VmOrc`, `DomListOK`, +`FpuOwnOK`, `IdleVt`) after the budget stop, after `fl4HandleSyscallVF` (every outcome of the invocation arms and the +receive / yield arms) and after `preemptionPathVF`, pinned by `KFrontF` as the native `KFinEv8`. Proved by +`StartS3.finFacts`. -/ +def PostPrefixFinJ (on rc : Fl4Spec.ObjRef → Bool) (n : I64) (c : Fl4Spec.Syscall) (s : S) (a : A) + (σ : Fl4Spec.State Unit) : Prop := + ∀ x ∈ (specFrontVF on rc c σ).1, ∀ (s' : S) (a' : A), KFrontF (L := L) arI n s a s' a' → + VEAnyS (L := L) arI AR s' a' x.2 → FinishFactsS3 (st := st) on rc s' x.2 + +variable {arI AR MR} + +/-- **The start record of the syscall entry** (module docstring). -/ +structure StartS3 (n : I64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop where + /-- [J] the carried spec bundle at the entry, without the native scope (`SInvXV`). -/ + X : SInvXV σ + /-- the current thread is not the idle thread. -/ + ct : σ.curThread ≠ σ.idleThread + /-- [J] `valid_ioports`. -/ + ports : SpecIoPorts σ + /-- [J] RAM Untyped zeroing. -/ + uz : SInvR.G5.SpecUtZ σ + /-- [J] the device range. -/ + df : SInvDV.DevRangeS σ + /-- [J] the global-PML4 exclusion. -/ + dp : SInvGp.GpmOutV σ + /-- [J] the Untyped view's well-formedness. -/ + ut : SInvU.UtInv'' σ + /-- [J] the refill-sum bound (the fast path). -/ + rv : SInvR.RV σ + /-- [P] `ArchIrqRel` at every dispatch pair of the entry. -/ + air : ∀ (s2 : S) (a2 : A) (σb : Fl4Spec.State Unit), KFrontB (L := L) arI s a s2 a2 → BudgetAt σ σb true → + ArchIrqRel arI a2 σb + /-- [S] the inherited core restrictions at every dispatch pair of the entry, on the executing core, for the current + thread and the capability pointer system call `n` invokes. -/ + core : ∀ (s2 : S) (a2 : A) (σb : Fl4Spec.State Unit), KFrontB (L := L) arI s a s2 a2 → + BudgetAt σ σb true → InvokesCap n → ∀ ct, sched.cur_thread st s2 = ok ct → + SelectedCoreScope (L.cpu s2) (L.mem s2) σb ct (invokedCptr st s2 ct n) + /-- [J] the idle thread's vtable slot holds the null cap. -/ + idleVt : SInvIV.IdleVtS σ + +variable (arI AR MR) + +/-- **The dispatch-state facts at the dispatch pair from the start record** (spec-only transport across the +`FaultIP` write and the passing budget step; `MR` at the pair is `budget_ok_mr`'s). -/ +theorem StartS3.dispFacts [P1.PlainStore st] (hnd : Fl4Spec.numDomains = 1) + {n : I64} + {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s s2 : S} {a a2 : A} {σ σb : Fl4Spec.State Unit} + {Zb : XSets} {Hb : P1.Heap} {Csb Acb : Set U64} {Lstb : U64 → List U64} {Db : Set U64} + (St : StartS3 (L := L) (st := st) (arI := arI) n s a σ) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (Jb : JAtZB (L := L) arI AR Zb Hb Csb Acb Lstb Db s2 a2 σb) + (hKF : KFrontB (L := L) arI s a s2 a2) (hBA : BudgetAt σ σb true) + (hMR : MR a2 σb.machineState) : + DispFactsS3 (st := st) arI MR (v3OnCore st s) (v3RemCur st s) n s2 a2 σb := by + obtain ⟨x0, hk0, -⟩ := St.X.sb.1.2 + have hni : Entered σ := St.X.entered St.ct + obtain ⟨-, ek, ect, -, -, eq⟩ := BudgetAt.shape27 hnd hk0 hBA + obtain ⟨hnib, hsg⟩ := dispEnteredSgXV hnd St.X St.ct hBA + obtain ⟨-, hnfb⟩ := SInvAll.sinvB_dispXV (x' := Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)) + St.X.sb St.ct hk0 rfl rfl ek ect + have hD := hBA.dispatchReadiness hnd hk0 hni St.X.sb.1.1.vmt St.X.offReady St.X.ctQ + obtain ⟨-, -, hOb, -⟩ := hD + have ek' : σb.kheap = Function.update σ.kheap σ.curThread + (some (.tcb (Notif.setReg x0 .faultIP (x0.tcbArch.tcbContext.userRegs .nextIP - 2)))) := ek + refine ⟨hsg, BudgetAt.ioPorts hnd hk0 hBA St.ports, vih_disp hnd hk0 hBA St.X.sb.1.1.vih, + St.air s2 a2 σb hKF hBA, ⟨hMR, hBA.vmt hnd, BudgetAt.vimD hnd St.X.sb.1.1.vim hk0 hBA⟩, + epQPre_of_sinvXV hnd St.X.sb hni St.X.epQAll hBA, hOb.rd0, hnfb, + BudgetAt.utZeroSpec hBA hnd hk0 St.uz, SInvR.G5.BudgetAt.devRange St.df hBA, + (BudgetAt.gpmOutAndUtInv hBA St.dp St.ut).1, (BudgetAt.gpmOutAndUtInv hBA St.dp St.ut).2, ?_, + St.core s2 a2 σb hKF hBA⟩ + refine J.vmOrcOfEntry Jb ?_ + intro t xt hk _ + by_cases e : t = σ.curThread + · subst e; exact ⟨x0, hk0⟩ + · rw [ek', Function.update_of_ne e] at hk + exact ⟨xt, hk⟩ + +/-- **The finish facts at every finish state the entry reaches**, from the start record and the entry state +(the spec facts: P3/VmFinFrontS3; the oracle pair is the entry's). -/ +theorem StartS3.finFacts (hnd : Fl4Spec.numDomains = 1) {on rc : Fl4Spec.ObjRef → Bool} {n : I64} + {c : Fl4Spec.Syscall} {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (St : StartS3 (L := L) (st := st) (arI := arI) n s a σ) + (hon : on = v3OnCore st s) (hrc : rc = v3RemCur st s) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + PostPrefixFinJ (L := L) (st := st) arI AR on rc n c s a σ := by + intro x hx s' a' _ hV + have A := St.X.sb.1.1 + obtain ⟨hD, hFo, hVt⟩ := FinFront.specFrontVF_f3 hnd on rc c (St.X.sb.entryJ St.ct).ctAct ⟨A.nq, A.epq⟩ A.ut A.live + A.ft A.sg A.idleOut St.idleVt A.dom x hx + have hR := FinFront.specFrontVF_rvm on rc c σ x hx + have hO : VmOrc st on rc s σ := by + subst hon hrc + exact J.1.1.vmOrcOfEntry J.1.1 (fun t xt hk _ => ⟨xt, hk⟩) + rcases hV with ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ | ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ + · exact ⟨J.1.1.vmOrcAcross J'.1.1 hO hR, hD, hFo, SInvR.G5.idleVt_of J'.1.1.idleTcb hVt⟩ + · exact ⟨J.1.1.vmOrcAcrossR J'.1.1 hO hR, hD, hFo, SInvR.G5.idleVt_of J'.1.1.idleTcb hVt⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Tables.lean b/proofs/Fl4Proofs/P3/VmEntryS3Tables.lean new file mode 100644 index 000000000..99d5c87b0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Tables.lean @@ -0,0 +1,1637 @@ +/- +Native PageTable, PageDirectory, and PDPT invocation refinement at a VM +entry. The tracked decoder retains its selected slot in the frame. +An off-set Unmap empties the selected native table, grows only its native +level set, and does not touch any VCPU or EPT memory. +-/ +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.XTopJointKEntryU20 +import Fl4Proofs.P3.FrontEndLabel +import Fl4Proofs.P3.SpecNoPre +import Fl4Proofs.P3.VmEntryS3Decode + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched Fl4Proofs.P1 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} +variable {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **A table off its set, at the shared record: a spec table of its level, its +frame `OffFrame`** (`OffK` carried; the rest from `H`). -/ +theorem JAtZB.stage3OffFrame (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {k : Nat} {q : U64} + (hk : k = 1 ∨ k = 2 ∨ k = 3) (hq : q ∉ tabSet Z k) (hO : OffR (L := L) Z s σ k q) : + (∃ ao, σ.kheap q.bv = some (.archObj ao) ∧ Fl4Spec.aaType ao = tabT k) ∧ OffFrame (L := L) Z s σ q := by + obtain ⟨⟨ao, h1, h2⟩, hK⟩ := hO hq + refine ⟨⟨ao, h1, h2⟩, ?_⟩ + -- the table is a 4 KiB `other` object of `H` + obtain ⟨n, hn, hH⟩ := J.cover.arch _ ao h1 + have hn' : n ≤ 12 := by have := J.carry.archFit q.bv ao n h1 hH; rwa [objBits_tabT h2] at this + have hHq : H q = some (.other 12) := by + have e : n = 12 := by omega + subst e; exact hH + have hHO := J.kern.heap + have hqa : q.val % 4096 = 0 := by have := hHO.align q _ hHq; simpa [P1.HKind.bits] using this + have h4096 : (2 : Nat) ^ 12 = 4096 := by norm_num + -- apart from every other `H` object + have hap : ∀ p k', H p = some k' → p ≠ q → q.val + 4096 ≤ p.val ∨ p.val + 2 ^ k'.bits ≤ q.val := by + intro p k' hp hne + rcases hHO.disj q _ p k' hHq hp (Ne.symm hne) with h | h + · left; simpa [P1.HKind.bits, h4096] using h + · right; exact h + have hneK : ∀ p k', H p = some k' → (∀ z, k' ≠ .other z) → p ≠ q := fun p k' hp hk' e => by + subst e; rw [hHq] at hp; exact hk' 12 (Option.some.inj hp).symm + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hR := J.xtop.rel.2.1.1 + -- a typed-heap CNode is a spec CNode, so not at `q` + have hcn : ∀ p r, Z.th p = some (.cnode r) → q.val + 4096 ≤ p.val ∨ p.val + 32 * 2 ^ r ≤ q.val := by + intro p r hp + have hne : p ≠ q := fun e => by + subst e; obtain ⟨cs, hc, -⟩ := hR.cnode p r hp; rw [hc] at h1; cases h1 + have e32 : 2 ^ (r + 5) = 32 * 2 ^ r := by rw [pow_add]; norm_num; ring + have := hap p _ (hJ.cnode p r hp) hne + simp only [P1.HKind.bits] at this; rw [e32] at this; exact this + have htc : ∀ t, Z.th t = some .tcb → q.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ q.val := by + intro t ht + have hHt := (hJ.tcb t).1 ht + have := hap t _ hHt (hneK t _ hHt fun _ e => by cases e) + simpa [P1.HKind.bits, h4096] using this + -- the sets' tables are other spec tables + have hF := J.xtop.fin + have hnv : ¬ (q ∈ Z.P ∨ q ∈ Z.L4 ∨ q ∈ Z.L3 ∨ q ∈ Z.L2 ∨ q ∈ Z.L1) := by + intro hv + rcases hv with hv | hv | hv | hv | hv + · obtain ⟨f, hf, -⟩ := hF.2.1.pool q hv; rw [hf] at h1; cases h1 + rcases hk with rfl | rfl | rfl <;> simp [Fl4Spec.aaType, tabT] at h2 + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l4 q hv; rw [hf] at h1; cases h1 + rcases hk with rfl | rfl | rfl <;> simp [Fl4Spec.aaType, tabT] at h2 + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l3 q hv; rw [hf] at h1; cases h1 + rcases hk with rfl | rfl | rfl <;> simp [Fl4Spec.aaType, tabT] at h2 + exact hq (by simpa [tabSet] using hv) + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l2 q hv; rw [hf] at h1; cases h1 + rcases hk with rfl | rfl | rfl <;> simp [Fl4Spec.aaType, tabT] at h2 + exact hq (by simpa [tabSet] using hv) + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l1 q hv; rw [hf] at h1; cases h1 + rcases hk with rfl | rfl | rfl <;> simp [Fl4Spec.aaType, tabT] at h2 + exact hq (by simpa [tabSet] using hv) + have hvs : ∀ q', (q' ∈ Z.P ∨ q' ∈ Z.L4 ∨ q' ∈ Z.L3 ∨ q' ∈ Z.L2 ∨ q' ∈ Z.L1) → + q.val + 4096 ≤ q'.val ∨ q'.val + 4096 ≤ q.val := by + intro q' hq' + obtain ⟨n', hn', hH'⟩ := J.vsFr_other hq' + have hne : q' ≠ q := fun e => hnv (e ▸ hq') + have h4 : 2 ^ 12 ≤ 2 ^ n' := Nat.pow_le_pow_right (by decide) hn' + rcases hap q' _ hH' hne with h | h + · exact Or.inl h + · simp only [P1.HKind.bits] at h; right; omega + refine ⟨hqa, fun b g1 g2 => ⟨fun ho => ?_, fun hv => ?_, fun p k' hp g => ?_⟩⟩ + · obtain ⟨a1, a2, -, a4⟩ := hK b g1 g2 + rcases ho with h | h | ⟨t, ht, c1, c2⟩ | ⟨e, he, c1, c2⟩ | ⟨r, hr, c1, c2⟩ | ⟨m, hm, c1, c2⟩ | + ⟨sc, hsc, x, nn, hkx, c1, c2⟩ | ⟨wp, hw, c1, c2⟩ + · exact a1 h + · exact a2 h + · have hHt := J.zH.1 t ht + have := hap t _ hHt (hneK t _ hHt fun _ e => by cases e) + simp only [P1.HKind.bits, h4096] at this; omega + · have hHe := J.zH.2.1 e he + have := hap e _ hHe (hneK e _ hHe fun _ e => by cases e) + simp only [P1.HKind.bits] at this; omega + · have hHr := J.zH.2.2.1 r hr + have := hap r _ hHr (hneK r _ hHr fun _ e => by cases e) + simp only [P1.HKind.bits] at this; omega + · have hHm := J.zH.2.2.2 m hm + have := hap m _ hHm (hneK m _ hHm fun _ e => by cases e) + simp only [P1.HKind.bits] at this; omega + · have hHs := (hJ.sc sc x nn (J.c0 sc hsc) hkx).2 + have := hap sc _ hHs (hneK sc _ hHs fun _ e => by cases e) + simp only [P1.HKind.bits] at this; omega + · exact a4 wp hw ⟨c1, c2⟩ + · obtain ⟨-, -, a3, -⟩ := hK b g1 g2 + rcases hv with ⟨p, r, hp, c1, c2⟩ | ⟨t, ht, c1, c2⟩ | ⟨q', hq', c1, c2⟩ | h + · have := hcn p r hp; omega + · have := htc t ht; omega + · have := hvs q' hq'; omega + · exact a3 h + · rcases J.thk p k' hp with rfl | ⟨r, rfl⟩ + · have := htc p hp; simp only [objSize] at g; omega + · have := hcn p r hp; simp only [objSize] at g; omega + +/-- **XTopJointVsOff's `OffTabOK` at every slot of a `JAtZ` state** (C4, from +`JAtZB.carry.vsTyp`, jc3). -/ +theorem JAtZB.stage3OffTabOK (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ y, IsSlot Z.th y → OffTabOK (L := L) Z s σ (capV (L.mem s) y) := by + intro y hy + have hO := J.carry.vsTyp y hy + cases hc : capV (L.mem s) y with + | PageTable q mp a v => + rw [hc] at hO + cases mp + · trivial + · intro hq + obtain ⟨⟨ao, h1, h2⟩, hF⟩ := J.stage3OffFrame (Or.inl rfl) (by simpa [tabSet] using hq) hO + obtain ⟨f, rfl⟩ := tabT_pt h2 + exact ⟨⟨f, h1⟩, hF⟩ + | PageDirectory q mp a v => + rw [hc] at hO + cases mp + · trivial + · intro hq + obtain ⟨⟨ao, h1, h2⟩, hF⟩ := J.stage3OffFrame (Or.inr (Or.inl rfl)) (by simpa [tabSet] using hq) hO + obtain ⟨f, rfl⟩ := tabT_pd h2 + exact ⟨⟨f, h1⟩, hF⟩ + | Pdpt q mp a v => + rw [hc] at hO + cases mp + · trivial + · intro hq + obtain ⟨⟨ao, h1, h2⟩, hF⟩ := J.stage3OffFrame (Or.inr (Or.inr rfl)) (by simpa [tabSet] using hq) hO + obtain ⟨f, rfl⟩ := tabT_pdpt h2 + exact ⟨⟨f, h1⟩, hF⟩ + | _ => trivial + + +/-- Grow the native table sets while preserving the VCPU relation. +The relation reads the same typed heap and MDB ghost. -/ +private theorem VcpuCore.ofNativeTableWrites (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : VcpuCore (L := L) V Z H s σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {L3' L2' L1' : Set U64} + (B' : JAtZB (L := L) arI AR (Z.mv Z.G (Z.P, Z.L4, L3', L2', L1')) H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hcs : ∀ y, IsSlot Z.th y → capV (L.mem s') y = capV (L.mem s) y ∨ + (shape (capV (L.mem s') y) = shape (capV (L.mem s) y) ∧ VcpuCapV V (capV (L.mem s') y) ∧ + ∀ f l o, capV (L.mem s') y ≠ .IoPort f l o)) + (nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VcpuCore (L := L) V (Z.mv Z.G (Z.P, Z.L4, L3', L2', L1')) H s' σ' := by + have Ly := B.jvLay C.vm + have kV : ∀ u ∈ V, L.mem s' (vTcbA u) = L.mem s (vTcbA u) := by + intro u hu + have hf := Ly.vfit u hu + have e := lay_vTcb_val hf.2 + exact hw.mem_eq (by unfold P1.Aligned; rw [e]; omega) fun hb => by + rcases nV u hu _ hb with h | h <;> omega + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := B.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have kTV : ∀ t, Z.th t = some .tcb → L.mem s' (tVcpuA t) = L.mem s (tVcpuA t) := hv + have E0 := C.extv + have E' : JExtVcpu (L := L) V Z.th s' := + { cap := fun y hy => by + rcases hcs y hy with e | ⟨-, hv, -⟩ + · rw [e]; exact E0.cap y hy + · exact hv + bnd := fun t ht => by + rcases E0.bnd t ht with h | ⟨h1, h2⟩ + · left; rw [kTV t ht]; exact h + · right; rw [kTV t ht]; exact ⟨h1, by rw [kV _ h1]; exact h2⟩ + back := fun v hv => by + rcases E0.back v hv with h | ⟨h1, h2⟩ + · left; rw [kV v hv]; exact h + · right; rw [kV v hv]; exact ⟨h1, by rw [kTV _ h1]; exact h2⟩ + nz := E0.nz } + have R' : RunVmB (L := L) Z.th s' := by + intro t ht h8 + rw [hv t ht] + refine C.rvm t ht ?_ + obtain ⟨x', hk', -⟩ := B'.xtop.rel.2.1.1.tcb t ht + have w' := (B'.xtop.fin.1.1.2.2.1.at_tcb (B'.thT t ht) hk').st + rw [Sched.stA_tStA] at h8 ⊢ + rw [h8] at w' + have hx' : x'.tcbState = .runningVM := by + cases hx : x'.tcbState <;> rw [hx] at w' <;> simp_all [tsCode] + obtain ⟨x, hk, hx⟩ := hrv _ x' hk' hx' + have w0 := (B.xtop.fin.1.1.2.2.1.at_tcb (B.thT t ht) hk).st + rw [hx] at w0 + apply UScalar.eq_of_val_eq + rw [w0]; simp [tsCode] + have Jv' : JVm (L := L) V H s' σ' := + C.vm.step (fun _ h => h) (fun _ _ => rfl) hw.1 (fun u hu => by rw [hvk u hu]; exact C.vm.spec u hu) + (fun o ko' ho => Or.inl (hsz o ko' ho)) + have Vl' : VList st V Z.th s' := C.vl.writes hw fun b hb => by + refine ⟨C.vl.lw_al Ly.vfit hb, fun hW => ?_⟩ + rcases hb with rfl | ⟨u, hu, rfl | rfl⟩ + · exact hh _ hW rfl + · have := lay_next_val (Ly.vfit u hu).2 + rcases nV u hu _ hW with h | h <;> omega + · have := lay_prev_val (Ly.vfit u hu).2 + rcases nV u hu _ hW with h | h <;> omega + exact ⟨E', C.vo.frame hnv, + C.vr.frame (fun u _ vs hu => hnv _ vs hu) (v3_agreeW (L := L) hw (fun u hu => (Ly.vfit u hu).2) nV), + VirtRngOne.rngOne_congr C.rngOne fun z hz _ => by + rcases hcs z (hsl z hz) with e | ⟨e, -⟩ + · rw [e]; exact NoA.refl _ + · exact NoA.of_shape e, + R', Jv', Vl', C.vb.ofV hvk kV, fun v hv => by + obtain ⟨y, hy, hc⟩ := C.vcap v hv + refine ⟨y, hy, ?_⟩ + rcases hcs y hy with e | ⟨e, -⟩ + · rw [e]; exact hc + · rw [hc] at e; exact shape_vcpu e, + C.tcap.of_caps (fun v hv _ => ⟨hv, kV v hv⟩) fun y hy t hc => ⟨y, hy, by + rcases hcs y hy with e | ⟨e, -⟩ + · rw [e]; exact hc + · rw [hc] at e; exact (shape_thread _ t).1 e⟩⟩ + +end + +/- Retain the selected slot in the three native decoder callbacks. -/ +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} +variable (arI : machine.Arch A) + +set_option maxHeartbeats 8000000 in +private theorem selected_page_table_core (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) + (hMO : MdbOKV (L.ksb s) (L.mem s) G) + {P L4 L3 L2 L1 : Set U64} {SR : S → A → Fl4Spec.State Unit → Prop} + {Q : S → Prop} (hQ : ∀ s' a' σ', SR s' a' σ' → Q s') + {X : S → A → Fl4Spec.State Unit → Prop} (hX0 : X s ar σ) + (hHM : VsPtMapHyps st A arI th P L4 L3 L2 L1 SR) (hHU : VsPtUnmapHyps st A arI th P L4 L3 L2 L1 SR) (hS : SR s ar σ) + (hVP : VsPlaced P (L.ksb s)) (hD : VsSlotsDisj th P (L.ksb s)) (hPP : PtPlaced L4 L3 L2 L1) + (hPS : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ SlotW th b) (hPV : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ VsWrite P (L.ksb s) b) + (hPO : ∀ b, PtWAll L4 L3 L2 L1 b → OutsideObjects th b) (hPI : PoolsIn P L4 (L.mem s)) + (hR : VsRel P (L.mem s) (L.ksb s) σ) (hPt : PtRel L4 L3 L2 L1 (L.mem s) σ) + (lbl : U64) (slbl : Fl4Spec.Data) + (hlM : lbl = fl4_abi.label.X86PageTableMap → Fl4Spec.invocationType slbl = .archInvocationLabel .x64PageTableMap) + (hlU : lbl = fl4_abi.label.X86PageTableUnmap → Fl4Spec.invocationType slbl = .archInvocationLabel .x64PageTableUnmap) + (hlO : lbl ≠ fl4_abi.label.X86PageTableMap → lbl ≠ fl4_abi.label.X86PageTableUnmap → + Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64PageTableMap ∧ Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64PageTableUnmap) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) + (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hrun : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.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) + {pc : U64} {r : Nat} (hpc : th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {ptr asid0 va0 : U64} {mp : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.PageTable ptr mp asid0 va0)) (hpL : ptr ∈ L1) + (hpa : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE &&& 4503599627366400#u64) = + core.num.U64.wrapping_sub ptr layout.PPTR_BASE) + (hm0 : mp = true → asid0 ≠ 0#u64 ∧ va0.bv &&& Fl4Spec.mask (Fl4Spec.ptTranslationBits + Fl4Spec.pageBits) = 0) + (hmu : mp = true → Vs.UserV va0) + (hXs : ∀ s' a' σ', SR s' a' σ' → VsRel P (L.mem s') (L.ksb s') σ' → PtRel L4 L3 L2 L1 (L.mem s') σ' → + CRel th (L.mem s') σ' G → FinInv th (L.mem s') σ' G → MdbOKV (L.ksb s) (L.mem s') G → KFoot σ σ' → + VsPostJ th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) (.PageTable ptr mp asid0 va0) σ σ' → + X s' a' σ') + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hext : (vspace.extra extra 0#usize = ok none ∧ excaps = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ excaps = (vc', xsl) :: rest ∧ + ∀ p' asid', vc = .Pml4 p' true asid' → asid' ≠ 0#u64 ∧ asid'.val < 2 ^ 20) + (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + DecOKMQX (L := L) arI Q X s ar σ lbl length (cslotAddr (pc.bv, idx)) (.PageTable ptr mp asid0 va0) blk call don fp + buf extra slbl (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + have hCR0 := hCR + obtain ⟨hSt, hSV, -, -, -⟩ := hCR0 + have hfe : EncWf (.PageTable ptr mp asid0 va0) := by + have h := hcap; rw [capAt_words] at h; exact decode_encWf h + obtain ⟨xct, hkt0, -⟩ := hSt.tcb ct htcb + have hkt : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt0 + have hcur' : ct.bv = σ.curThread := hcur.symm + have hst1 : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), Notif.stsAct (Notif.updK σ ct.bv (.tcb { xct with tcbState := .restart })) ct.bv)}, False) := by + rw [hcur]; exact Notif.setThreadState_eq .restart hkt0 + have hAs : AbsSlot th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + unfold DecOKMQX syscall.decode_invocation + rw [hct]; simp only [bind_ok] + have hspec : specDec fp slbl (args.map UScalar.bv) ci (pc.bv, idx) (.PageTable ptr mp asid0 va0) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec (Fl4Spec.archDecodeInvocation (Z := Unit) slbl + (args.map UScalar.bv) ci (pc.bv, idx) (.pageTableCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) + excaps)) := rfl + rw [hspec] + unfold vspace.decode + rw [hct]; simp only [bind_ok] + have hne : ¬ fl4_abi.label.X86PageTableMap = fl4_abi.label.X86PageTableUnmap := by + unfold fl4_abi.label.X86PageTableMap fl4_abi.label.X86PageTableUnmap; decide + by_cases hm : lbl = fl4_abi.label.X86PageTableMap + · subst hm + have hl := hlM rfl + rcases hext with ⟨hex, rfl⟩ | ⟨x, vc, vc', xsl, rest, hex, hcx, hvr, rfl, hvc⟩ + · unfold vspace.decode_table + simp only [vspace.as_table, bind_ok, hne, ↓reduceIte] + have htr : ExceptT.run (Fl4Spec.archDecodeInvocation (Z := Unit) slbl (args.map UScalar.bv) ci (pc.bv, idx) + (.pageTableCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) []) σ = + ({(.error .truncatedMessage, σ)}, False) := ptDecode_trunc hl _ _ _ _ _ (by simp) σ + cases mp + · simp only [Bool.false_eq_true, ↓reduceIte] + unfold vspace.decode_table_map + rw [hct, hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact topMQX_err (⟨_, rfl, rfl, .truncatedMessage, htr, Or.inl trivial⟩) hX0 + · simp only [↓reduceIte] + rw [hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact topMQX_err (⟨_, rfl, rfl, .truncatedMessage, htr, Or.inl trivial⟩) hX0 + refine WP.spec_mono (pt_map_rel hHM s ar σ hP hVP hD hPP hPS (fun b hb => hPV b (let ⟨q, hq, i, hi, e⟩ := hb; ⟨q, Or.inr (Or.inr (Or.inl hq)), i, hi, e⟩)) hSt hSV hR hPt hS + ct hct hcur' hkt (hrun _ hkt) htcb hact hPO ptr asid0 va0 mp hpL hfe hpa hpc hidx hl length buf args hlen hargs ci extra + hex hcx hvr xsl rest hvc hPI) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | ⟨h1, iv, σ', hdec, hms, hSt', hSV', hR', hPt', hSRq, hF, hL, hK, hMF⟩) + · exact topMQX_err (⟨e, h1, h2, e', hdec, he⟩) (by rw [h2, h2a]; exact hX0) + · exact ⟨topMX_of_lift2 ⟨iv, hdec, _, hst1, _, perf_tbl_mem blk call don hst1 (Or.inl hms), + (fun h3 => ⟨⟨th, G, Or.inl ⟨h1, [], rfl⟩, hP, h3⟩, hXs _ _ _ hSRq hR' hPt' h3.1 h3.2.1 h3.2.2 hK ⟨hF, hL, Or.inl ⟨iv, Or.inr (Or.inl hms)⟩, Or.inr (Or.inr (Or.inl hMF))⟩⟩) + (crelM_after hCR hFI hMO hcap hSt' hSV' hF hL hK rfl)⟩, Or.inr (hQ _ _ _ hSRq)⟩ + by_cases hu : lbl = fl4_abi.label.X86PageTableUnmap + · subst hu + have hl := hlU rfl + have hfinR := fin_iff_of hP hCR hFI hAs hcap (Or.inl ⟨trivial, rfl⟩) rfl (by simp) + (fun h => by + have : Fl4Spec.GenObjRef.objRef ptr.bv ∈ Fl4Spec.genObjRefs (absCap (.PageTable ptr mp asid0 va0)) := + Set.mem_union_left _ (Set.mem_union_left _ ⟨ptr.bv, by simp [absCap, Fl4Spec.objRefs, Fl4Spec.aobjRef], rfl⟩) + rw [h] at this; exact this) + refine WP.spec_mono (pt_unmap_rel hHU s ar σ hP hVP hD hPP hPS (fun b ⟨q, hq, i, hi, e⟩ => hPV b ⟨q, Or.inr (Or.inr (Or.inl hq)), i, hi, e⟩) (fun b ⟨q, hq, i, hi, e⟩ => hPV b ⟨q, Or.inr (Or.inr (Or.inr hq)), i, hi, e⟩) hPI hSt hSV hR hPt hS ct hct hcur' hkt (hrun _ hkt) htcb + hact hPO ptr asid0 va0 mp hpL hfe hm0 hmu hpc hidx hcap hl length buf (args.map UScalar.bv) ci extra excaps + (is_final_eq s _) hfinR) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | ⟨h1, iv, σ', hdec, hms, ⟨hSRq, hSt', hSV', hR', hPt'⟩, hF, hL, hK, hfn, hUT⟩) + · exact topMQX_err (⟨e, h1, h2, e', hdec, he⟩) (by rw [h2, h2a]; exact hX0) + · exact ⟨topMX_of_lift2 ⟨iv, hdec, _, hst1, _, perf_tbl_mem blk call don hst1 (Or.inl hms), + (fun h3 => ⟨⟨th, G, Or.inl ⟨h1, [], rfl⟩, hP, h3⟩, hXs _ _ _ hSRq hR' hPt' h3.1 h3.2.1 h3.2.2 hK ⟨hF, hL, Or.inl ⟨iv, Or.inr (Or.inl hms)⟩, VsOpF.unmapTab ⟨hUT, hfn⟩⟩⟩) + (crelM_after hCR hFI hMO hcap hSt' hSV' hF hL hK rfl)⟩, Or.inr (hQ _ _ _ hSRq)⟩ + obtain ⟨o1, o2⟩ := hlO hm hu + unfold vspace.decode_table + simp only [vspace.as_table, bind_ok, hm, hu, ↓reduceIte, error.illegal, WP.spec_ok] + refine topMQX_err (⟨_, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) hX0 + show ExceptT.run (Fl4Spec.decodePageTableInvocation (Z := Unit) slbl (args.map UScalar.bv) (pc.bv, idx) + (.pageTableCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) excaps) σ = _ + unfold Fl4Spec.decodePageTableInvocation; dsimp only + rw [if_neg o1, if_neg o2]; rfl + +set_option maxHeartbeats 8000000 in +private theorem selected_page_directory_core (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) + (hMO : MdbOKV (L.ksb s) (L.mem s) G) + {P L4 L3 L2 L1 : Set U64} {SR : S → A → Fl4Spec.State Unit → Prop} + {Q : S → Prop} (hQ : ∀ s' a' σ', SR s' a' σ' → Q s') + {X : S → A → Fl4Spec.State Unit → Prop} (hX0 : X s ar σ) + (hHM : VsPdMapHyps st A arI th P L4 L3 L2 L1 SR) (hHU : VsPdUnmapHyps st A arI th P L4 L3 L2 L1 SR) (hS : SR s ar σ) + (hVP : VsPlaced P (L.ksb s)) (hD : VsSlotsDisj th P (L.ksb s)) (hPP : PtPlaced L4 L3 L2 L1) + (hPS : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ SlotW th b) (hPV : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ VsWrite P (L.ksb s) b) + (hPO : ∀ b, PtWAll L4 L3 L2 L1 b → OutsideObjects th b) (hPI : PoolsIn P L4 (L.mem s)) + (hR : VsRel P (L.mem s) (L.ksb s) σ) (hPt : PtRel L4 L3 L2 L1 (L.mem s) σ) + (lbl : U64) (slbl : Fl4Spec.Data) + (hlM : lbl = fl4_abi.label.X86PageDirectoryMap → Fl4Spec.invocationType slbl = .archInvocationLabel .x64PageDirectoryMap) + (hlU : lbl = fl4_abi.label.X86PageDirectoryUnmap → Fl4Spec.invocationType slbl = .archInvocationLabel .x64PageDirectoryUnmap) + (hlO : lbl ≠ fl4_abi.label.X86PageDirectoryMap → lbl ≠ fl4_abi.label.X86PageDirectoryUnmap → + Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64PageDirectoryMap ∧ Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64PageDirectoryUnmap) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) + (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hrun : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.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) + {pc : U64} {r : Nat} (hpc : th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {ptr asid0 va0 : U64} {mp : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.PageDirectory ptr mp asid0 va0)) (hpL : ptr ∈ L2) + (hpa : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE &&& 4503599627366400#u64) = + core.num.U64.wrapping_sub ptr layout.PPTR_BASE) + (hm0 : mp = true → asid0 ≠ 0#u64) (hmu : mp = true → Vs.UserV va0) + (hXs : ∀ s' a' σ', SR s' a' σ' → VsRel P (L.mem s') (L.ksb s') σ' → PtRel L4 L3 L2 L1 (L.mem s') σ' → + CRel th (L.mem s') σ' G → FinInv th (L.mem s') σ' G → MdbOKV (L.ksb s) (L.mem s') G → KFoot σ σ' → + VsPostJ th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) (.PageDirectory ptr mp asid0 va0) σ σ' → + X s' a' σ') + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hext : (vspace.extra extra 0#usize = ok none ∧ excaps = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ excaps = (vc', xsl) :: rest ∧ + ∀ p' asid', vc = .Pml4 p' true asid' → asid' ≠ 0#u64 ∧ asid'.val < 2 ^ 20) + (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + DecOKMQX (L := L) arI Q X s ar σ lbl length (cslotAddr (pc.bv, idx)) (.PageDirectory ptr mp asid0 va0) blk call don fp + buf extra slbl (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + have hCR0 := hCR + obtain ⟨hSt, hSV, -, -, -⟩ := hCR0 + have hfe : EncWf (.PageDirectory ptr mp asid0 va0) := by + have h := hcap; rw [capAt_words] at h; exact decode_encWf h + obtain ⟨xct, hkt0, -⟩ := hSt.tcb ct htcb + have hkt : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt0 + have hcur' : ct.bv = σ.curThread := hcur.symm + have hst1 : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), Notif.stsAct (Notif.updK σ ct.bv (.tcb { xct with tcbState := .restart })) ct.bv)}, False) := by + rw [hcur]; exact Notif.setThreadState_eq .restart hkt0 + have hAs : AbsSlot th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + unfold DecOKMQX syscall.decode_invocation + rw [hct]; simp only [bind_ok] + have hspec : specDec fp slbl (args.map UScalar.bv) ci (pc.bv, idx) (.PageDirectory ptr mp asid0 va0) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec (Fl4Spec.archDecodeInvocation (Z := Unit) slbl + (args.map UScalar.bv) ci (pc.bv, idx) (.pageDirectoryCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) + excaps)) := rfl + rw [hspec] + unfold vspace.decode + rw [hct]; simp only [bind_ok] + have hne : ¬ fl4_abi.label.X86PageDirectoryMap = fl4_abi.label.X86PageDirectoryUnmap := by + unfold fl4_abi.label.X86PageDirectoryMap fl4_abi.label.X86PageDirectoryUnmap; decide + by_cases hm : lbl = fl4_abi.label.X86PageDirectoryMap + · subst hm + have hl := hlM rfl + rcases hext with ⟨hex, rfl⟩ | ⟨x, vc, vc', xsl, rest, hex, hcx, hvr, rfl, hvc⟩ + · unfold vspace.decode_table + simp only [vspace.as_table, bind_ok, hne, ↓reduceIte] + have htr : ExceptT.run (Fl4Spec.archDecodeInvocation (Z := Unit) slbl (args.map UScalar.bv) ci (pc.bv, idx) + (.pageDirectoryCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) []) σ = + ({(.error .truncatedMessage, σ)}, False) := pdDecode_trunc hl _ _ _ _ _ (by simp) σ + cases mp + · simp only [Bool.false_eq_true, ↓reduceIte] + unfold vspace.decode_table_map + rw [hct, hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact topMQX_err (⟨_, rfl, rfl, .truncatedMessage, htr, Or.inl trivial⟩) hX0 + · simp only [↓reduceIte] + rw [hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact topMQX_err (⟨_, rfl, rfl, .truncatedMessage, htr, Or.inl trivial⟩) hX0 + refine WP.spec_mono (pd_map_rel hHM s ar σ hP hVP hD hPP hPS (fun b hb => hPV b (let ⟨q, hq, i, hi, e⟩ := hb; ⟨q, Or.inr (Or.inl hq), i, hi, e⟩)) hSt hSV hR hPt hS + ct hct hcur' hkt (hrun _ hkt) htcb hact hPO ptr asid0 va0 mp hpL hfe hpa hpc hidx hl length buf args hlen hargs ci extra + hex hcx hvr xsl rest hvc hPI) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | ⟨h1, iv, σ', hdec, hms, hSt', hSV', hR', hPt', hSRq, hF, hL, hK, hMF⟩) + · exact topMQX_err (⟨e, h1, h2, e', hdec, he⟩) (by rw [h2, h2a]; exact hX0) + · exact ⟨topMX_of_lift2 ⟨iv, hdec, _, hst1, _, perf_tbl_mem blk call don hst1 ((Or.inr ∘ Or.inl) hms), + (fun h3 => ⟨⟨th, G, Or.inl ⟨h1, [], rfl⟩, hP, h3⟩, hXs _ _ _ hSRq hR' hPt' h3.1 h3.2.1 h3.2.2 hK ⟨hF, hL, Or.inl ⟨iv, Or.inr (Or.inr (Or.inl hms))⟩, Or.inr (Or.inl hMF)⟩⟩) + (crelM_after hCR hFI hMO hcap hSt' hSV' hF hL hK rfl)⟩, Or.inr (hQ _ _ _ hSRq)⟩ + by_cases hu : lbl = fl4_abi.label.X86PageDirectoryUnmap + · subst hu + have hl := hlU rfl + have hfinR := fin_iff_of hP hCR hFI hAs hcap (Or.inl ⟨trivial, rfl⟩) rfl (by simp) + (fun h => by + have : Fl4Spec.GenObjRef.objRef ptr.bv ∈ Fl4Spec.genObjRefs (absCap (.PageDirectory ptr mp asid0 va0)) := + Set.mem_union_left _ (Set.mem_union_left _ ⟨ptr.bv, by simp [absCap, Fl4Spec.objRefs, Fl4Spec.aobjRef], rfl⟩) + rw [h] at this; exact this) + refine WP.spec_mono (pd_unmap_rel hHU s ar σ hP hVP hD hPP hPS (fun b ⟨q, hq, i, hi, e⟩ => hPV b ⟨q, Or.inr (Or.inl hq), i, hi, e⟩) (fun b ⟨q, hq, i, hi, e⟩ => hPV b ⟨q, Or.inr (Or.inr (Or.inl hq)), i, hi, e⟩) hPI hSt hSV hR hPt hS ct hct hcur' hkt (hrun _ hkt) htcb + hact hPO ptr asid0 va0 mp hpL hfe hm0 hmu hpc hidx hcap hl length buf (args.map UScalar.bv) ci extra excaps + (is_final_eq s _) hfinR) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | ⟨h1, iv, σ', hdec, hms, ⟨hSRq, hSt', hSV', hR', hPt'⟩, hF, hL, hK, hfn, hUT⟩) + · exact topMQX_err (⟨e, h1, h2, e', hdec, he⟩) (by rw [h2, h2a]; exact hX0) + · exact ⟨topMX_of_lift2 ⟨iv, hdec, _, hst1, _, perf_tbl_mem blk call don hst1 ((Or.inr ∘ Or.inl) hms), + (fun h3 => ⟨⟨th, G, Or.inl ⟨h1, [], rfl⟩, hP, h3⟩, hXs _ _ _ hSRq hR' hPt' h3.1 h3.2.1 h3.2.2 hK ⟨hF, hL, Or.inl ⟨iv, Or.inr (Or.inr (Or.inl hms))⟩, VsOpF.unmapTab ⟨hUT, hfn⟩⟩⟩) + (crelM_after hCR hFI hMO hcap hSt' hSV' hF hL hK rfl)⟩, Or.inr (hQ _ _ _ hSRq)⟩ + obtain ⟨o1, o2⟩ := hlO hm hu + unfold vspace.decode_table + simp only [vspace.as_table, bind_ok, hm, hu, ↓reduceIte, error.illegal, WP.spec_ok] + refine topMQX_err (⟨_, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) hX0 + show ExceptT.run (Fl4Spec.decodePageDirectoryInvocation (Z := Unit) slbl (args.map UScalar.bv) (pc.bv, idx) + (.pageDirectoryCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) excaps) σ = _ + unfold Fl4Spec.decodePageDirectoryInvocation; dsimp only + rw [if_neg o1, if_neg o2]; rfl + +set_option maxHeartbeats 8000000 in +private theorem selected_pdpt_core (th : TypedHeap) (hP : Placed th) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (G : MdbGhost) (hCR : CRel th (L.mem s) σ G) (hFI : FinInv th (L.mem s) σ G) + (hMO : MdbOKV (L.ksb s) (L.mem s) G) + {P L4 L3 L2 L1 : Set U64} {SR : S → A → Fl4Spec.State Unit → Prop} + {Q : S → Prop} (hQ : ∀ s' a' σ', SR s' a' σ' → Q s') + {X : S → A → Fl4Spec.State Unit → Prop} (hX0 : X s ar σ) + (hHM : VsPdptMapHyps st A arI th P L4 L3 L2 L1 SR) (hHU : VsPdptUnmapHyps st A arI th P L4 L3 L2 L1 SR) (hS : SR s ar σ) + (hVP : VsPlaced P (L.ksb s)) (hD : VsSlotsDisj th P (L.ksb s)) (hPP : PtPlaced L4 L3 L2 L1) + (hPS : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ SlotW th b) (hPV : ∀ b, PtWAll L4 L3 L2 L1 b → ¬ VsWrite P (L.ksb s) b) + (hPO : ∀ b, PtWAll L4 L3 L2 L1 b → OutsideObjects th b) (hPI : PoolsIn P L4 (L.mem s)) + (hR : VsRel P (L.mem s) (L.ksb s) σ) (hPt : PtRel L4 L3 L2 L1 (L.mem s) σ) + (lbl : U64) (slbl : Fl4Spec.Data) + (hlM : lbl = fl4_abi.label.X86PDPTMap → Fl4Spec.invocationType slbl = .archInvocationLabel .x64PDPTMap) + (hlU : lbl = fl4_abi.label.X86PDPTUnmap → Fl4Spec.invocationType slbl = .archInvocationLabel .x64PDPTUnmap) + (hlO : lbl ≠ fl4_abi.label.X86PDPTMap → lbl ≠ fl4_abi.label.X86PDPTUnmap → + Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64PDPTMap ∧ Fl4Spec.invocationType slbl ≠ .archInvocationLabel .x64PDPTUnmap) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) (hcur : σ.curThread = ct.bv) + (htcb : th ct = some .tcb) (hact : OutsideObjects th (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION)) + (hrun : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.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) + {pc : U64} {r : Nat} (hpc : th pc = some (.cnode r)) {idx : List Bool} (hidx : idx.length = r) + {ptr asid0 va0 : U64} {mp : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Pdpt ptr mp asid0 va0)) (hpL : ptr ∈ L3) + (hpa : (core.num.U64.wrapping_sub ptr layout.PPTR_BASE &&& 4503599627366400#u64) = + core.num.U64.wrapping_sub ptr layout.PPTR_BASE) + (hm0 : mp = true → asid0 ≠ 0#u64) (hmu : mp = true → Vs.UserV va0) + (hXs : ∀ s' a' σ', SR s' a' σ' → VsRel P (L.mem s') (L.ksb s') σ' → PtRel L4 L3 L2 L1 (L.mem s') σ' → + CRel th (L.mem s') σ' G → FinInv th (L.mem s') σ' G → MdbOKV (L.ksb s) (L.mem s') G → KFoot σ σ' → + VsPostJ th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) (.Pdpt ptr mp asid0 va0) σ σ' → + X s' a' σ') + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hext : (vspace.extra extra 0#usize = ok none ∧ excaps = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ excaps = (vc', xsl) :: rest ∧ + ∀ p' asid', vc = .Pml4 p' true asid' → asid' ≠ 0#u64 ∧ asid'.val < 2 ^ 20) + (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + DecOKMQX (L := L) arI Q X s ar σ lbl length (cslotAddr (pc.bv, idx)) (.Pdpt ptr mp asid0 va0) blk call don fp + buf extra slbl (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + have hCR0 := hCR + obtain ⟨hSt, hSV, -, -, -⟩ := hCR0 + have hfe : EncWf (.Pdpt ptr mp asid0 va0) := by + have h := hcap; rw [capAt_words] at h; exact decode_encWf h + obtain ⟨xct, hkt0, -⟩ := hSt.tcb ct htcb + have hkt : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt0 + have hcur' : ct.bv = σ.curThread := hcur.symm + have hst1 : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = + ({((), Notif.stsAct (Notif.updK σ ct.bv (.tcb { xct with tcbState := .restart })) ct.bv)}, False) := by + rw [hcur]; exact Notif.setThreadState_eq .restart hkt0 + have hAs : AbsSlot th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + unfold DecOKMQX syscall.decode_invocation + rw [hct]; simp only [bind_ok] + have hspec : specDec fp slbl (args.map UScalar.bv) ci (pc.bv, idx) (.Pdpt ptr mp asid0 va0) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec (Fl4Spec.archDecodeInvocation (Z := Unit) slbl + (args.map UScalar.bv) ci (pc.bv, idx) (.pdPointerTableCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) + excaps)) := rfl + rw [hspec] + unfold vspace.decode + rw [hct]; simp only [bind_ok] + have hne : ¬ fl4_abi.label.X86PDPTMap = fl4_abi.label.X86PDPTUnmap := by + unfold fl4_abi.label.X86PDPTMap fl4_abi.label.X86PDPTUnmap; decide + by_cases hm : lbl = fl4_abi.label.X86PDPTMap + · subst hm + have hl := hlM rfl + rcases hext with ⟨hex, rfl⟩ | ⟨x, vc, vc', xsl, rest, hex, hcx, hvr, rfl, hvc⟩ + · unfold vspace.decode_table + simp only [vspace.as_table, bind_ok, hne, ↓reduceIte] + have htr : ExceptT.run (Fl4Spec.archDecodeInvocation (Z := Unit) slbl (args.map UScalar.bv) ci (pc.bv, idx) + (.pdPointerTableCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) []) σ = + ({(.error .truncatedMessage, σ)}, False) := pdptDecode_trunc hl _ _ _ _ _ (by simp) σ + cases mp + · simp only [Bool.false_eq_true, ↓reduceIte] + unfold vspace.decode_table_map + rw [hct, hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact topMQX_err (⟨_, rfl, rfl, .truncatedMessage, htr, Or.inl trivial⟩) hX0 + · simp only [↓reduceIte] + rw [hex] + simp only [bind_ok, error.truncated, WP.spec_ok] + exact topMQX_err (⟨_, rfl, rfl, .truncatedMessage, htr, Or.inl trivial⟩) hX0 + refine WP.spec_mono (pdpt_map_rel hHM s ar σ hP hVP hD hPP hPS (fun b hb => hPV b (let ⟨q, hq, i, hi, e⟩ := hb; ⟨q, Or.inl hq, i, hi, e⟩)) hSt hSV hR hPt hS + ct hct hcur' hkt (hrun _ hkt) htcb hact hPO ptr asid0 va0 mp hpL hfe hpa hpc hidx hl length buf args hlen hargs ci extra + hex hcx hvr xsl rest hvc hPI) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | ⟨h1, iv, σ', hdec, hms, hSt', hSV', hR', hPt', hSRq, hF, hL, hK, hMF⟩) + · exact topMQX_err (⟨e, h1, h2, e', hdec, he⟩) (by rw [h2, h2a]; exact hX0) + · exact ⟨topMX_of_lift2 ⟨iv, hdec, _, hst1, _, perf_tbl_mem blk call don hst1 ((Or.inr ∘ Or.inr) hms), + (fun h3 => ⟨⟨th, G, Or.inl ⟨h1, [], rfl⟩, hP, h3⟩, hXs _ _ _ hSRq hR' hPt' h3.1 h3.2.1 h3.2.2 hK ⟨hF, hL, Or.inl ⟨iv, Or.inr (Or.inr (Or.inr hms))⟩, Or.inl hMF⟩⟩) + (crelM_after hCR hFI hMO hcap hSt' hSV' hF hL hK rfl)⟩, Or.inr (hQ _ _ _ hSRq)⟩ + by_cases hu : lbl = fl4_abi.label.X86PDPTUnmap + · subst hu + have hl := hlU rfl + have hfinR := fin_iff_of hP hCR hFI hAs hcap (Or.inl ⟨trivial, rfl⟩) rfl (by simp) + (fun h => by + have : Fl4Spec.GenObjRef.objRef ptr.bv ∈ Fl4Spec.genObjRefs (absCap (.Pdpt ptr mp asid0 va0)) := + Set.mem_union_left _ (Set.mem_union_left _ ⟨ptr.bv, by simp [absCap, Fl4Spec.objRefs, Fl4Spec.aobjRef], rfl⟩) + rw [h] at this; exact this) + refine WP.spec_mono (pdpt_unmap_rel hHU s ar σ hP hVP hD hPP hPS (fun b ⟨q, hq, i, hi, e⟩ => hPV b ⟨q, Or.inl hq, i, hi, e⟩) (fun b ⟨q, hq, i, hi, e⟩ => hPV b ⟨q, Or.inr (Or.inl hq), i, hi, e⟩) hPI hSt hSV hR hPt hS ct hct hcur' hkt (hrun _ hkt) htcb + hact hPO ptr asid0 va0 mp hpL hfe hm0 hmu hpc hidx hcap hl length buf (args.map UScalar.bv) ci extra excaps + (is_final_eq s _) hfinR) ?_ + rintro res (⟨e, e', h1, he, hdec, h2, h2a⟩ | ⟨h1, iv, σ', hdec, hms, ⟨hSRq, hSt', hSV', hR', hPt'⟩, hF, hL, hK, hfn, hUT⟩) + · exact topMQX_err (⟨e, h1, h2, e', hdec, he⟩) (by rw [h2, h2a]; exact hX0) + · exact ⟨topMX_of_lift2 ⟨iv, hdec, _, hst1, _, perf_tbl_mem blk call don hst1 ((Or.inr ∘ Or.inr) hms), + (fun h3 => ⟨⟨th, G, Or.inl ⟨h1, [], rfl⟩, hP, h3⟩, hXs _ _ _ hSRq hR' hPt' h3.1 h3.2.1 h3.2.2 hK ⟨hF, hL, Or.inl ⟨iv, Or.inr (Or.inr (Or.inr hms))⟩, VsOpF.unmapTab ⟨hUT, hfn⟩⟩⟩) + (crelM_after hCR hFI hMO hcap hSt' hSV' hF hL hK rfl)⟩, Or.inr (hQ _ _ _ hSRq)⟩ + obtain ⟨o1, o2⟩ := hlO hm hu + unfold vspace.decode_table + simp only [vspace.as_table, bind_ok, hm, hu, ↓reduceIte, error.illegal, WP.spec_ok] + refine topMQX_err (⟨_, rfl, rfl, .illegalOperation, ?_, Or.inl trivial⟩) hX0 + show ExceptT.run (Fl4Spec.decodePdptInvocation (Z := Unit) slbl (args.map UScalar.bv) (pc.bv, idx) + (.pdPointerTableCap ptr.bv (if mp then some (asid0.bv, va0.bv) else none)) excaps) σ = _ + unfold Fl4Spec.decodePdptInvocation; dsimp only + rw [if_neg o1, if_neg o2]; rfl + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} +variable {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A native table cap that is operated on through the native table decoder. -/ +private def NativeTableCap : capability.Cap → Prop + | .PageTable .. | .PageDirectory .. | .Pdpt .. => True + | _ => False + +private theorem tableCapOfShape {c c' : capability.Cap} (hc : NativeTableCap c) + (h : shape c' = shape c) : NativeTableCap c' := by + cases c <;> try exact False.elim hc + all_goals cases c' <;> first | exact True.intro | cases h + +/-- The architecture objects a native table operation keeps exactly. -/ +private def TableGuestObject : Fl4Spec.ArchKernelObj → Prop + | .vcpu _ | .ept _ _ | .asidPool _ => True + | _ => False + +private theorem tableArchKeepAt {σ' : Fl4Spec.State Unit} {q : U64} + {old new : Fl4Spec.ArchKernelObj} + (hOld : σ.kheap q.bv = some (.archObj old)) + (hNew : σ'.kheap q.bv = some (.archObj new)) + (hnO : ¬ TableGuestObject old) (hnN : ¬ TableGuestObject new) + (hKeep : ArchKeepBut q σ σ') + (hBack : ∀ o ao, σ'.kheap o = some (.archObj ao) → ∃ ao0, σ.kheap o = some (.archObj ao0)) : + ∀ o ao, TableGuestObject ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + intro o ao ha + by_cases hq : o = q.bv + · subst o + rw [hOld, hNew] + constructor + · intro e + cases e + exact (hnO ha).elim + · intro e + cases e + exact (hnN ha).elim + · constructor + · intro hk + have hu : (⟨o⟩ : U64) ≠ q := fun e => hq (congrArg (fun x : U64 => x.bv) e) + exact (hKeep ⟨o⟩ ⟨ao, hk⟩ hu).trans hk + · intro hk + obtain ⟨ao0, hko⟩ := hBack o ao hk + have hu : (⟨o⟩ : U64) ≠ q := fun e => hq (congrArg (fun x : U64 => x.bv) e) + rw [← hKeep ⟨o⟩ ⟨ao0, hko⟩ hu] + exact hk + +private theorem tableArchShrink {σ' : Fl4Spec.State Unit} (hShrink : ArchShr σ σ') + (hBack : ∀ o ao, σ'.kheap o = some (.archObj ao) → ∃ ao0, σ.kheap o = some (.archObj ao0)) : + ∀ o ao, TableGuestObject ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + have heq : ∀ x y, EntShr x y → (TableGuestObject x ∨ TableGuestObject y) → y = x := by + rintro x y (rfl | ⟨_, _, rfl, rfl, -⟩ | ⟨_, _, rfl, rfl, -⟩ | + ⟨_, _, rfl, rfl, -⟩ | ⟨_, _, rfl, rfl, -⟩) h + · rfl + all_goals exact False.elim (h.elim id id) + intro o ao ha + constructor + · intro hk + obtain ⟨ao', hk', hq⟩ := hShrink.2 o ao hk + rw [heq _ _ hq (Or.inl ha)] at hk' + exact hk' + · intro hk + obtain ⟨ao0, hk0⟩ := hBack o ao hk + obtain ⟨ao1, hk1, hq⟩ := hShrink.2 o ao0 hk0 + rw [hk] at hk1 + have e : ao = ao1 := by cases hk1; rfl + subst ao1 + rw [← heq _ _ hq (Or.inr ha)] at hk0 + exact hk0 + +private theorem tableSpecFrame {m m' : U64 → U64} {x : U64} {c : capability.Cap} + {σ' : Fl4Spec.State Unit} (hc : NativeTableCap c) (h : VsOpF m m' x c σ σ') + (hBack : ∀ o ao, σ'.kheap o = some (.archObj ao) → ∃ ao0, σ.kheap o = some (.archObj ao0)) : + σ'.archState.x64AsidTable = σ.archState.x64AsidTable ∧ + ∀ o ao, TableGuestObject ao → + (σ.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + rcases h with hp3 | hp2 | hp1 | hpA | hpM | hpU | ⟨hU, _⟩ + · obtain ⟨q0, as0, va0, asid, va, pm, j, f, ta, tr, h0, h1, h2, h3, h4, h5, h6, + hOld, hNew, hKeep, hAt⟩ := hp3 + exact ⟨hAt, tableArchKeepAt hOld hNew id id hKeep hBack⟩ + · obtain ⟨q0, as0, va0, asid, va, pm, i, pd, j, f, ta, tr, h0, h1, h2, h3, h4, h5, h6, + h7, h8, h9, hOld, hNew, hKeep, hAt⟩ := hp2 + exact ⟨hAt, tableArchKeepAt hOld hNew id id hKeep hBack⟩ + · obtain ⟨q0, as0, va0, asid, va, pm, i, pt3, k, pd, j, f, ta, tr, h0, h1, h2, h3, h4, + h5, h6, h7, h8, h9, h10, h11, h12, h13, hOld, hNew, hKeep, hAt⟩ := hp1 + exact ⟨hAt, tableArchKeepAt hOld hNew id id hKeep hBack⟩ + · obtain ⟨_, _, _, _, _, _, _, e, -⟩ := hpA + rw [e] at hc + exact hc.elim + · have hF := hpM.1 + cases c <;> first | exact False.elim hF | exact False.elim hc + · have hF := hpU.1 + cases c <;> first | exact False.elim hF | exact False.elim hc + · exact ⟨hU.1.1, tableArchShrink hU.1 hBack⟩ + +/-- The optional additional write region is a native page-table object that +was outside its level set at the selected decode. -/ +private def NativeOffTable (Z : XSets) (s : S) (σ : Fl4Spec.State Unit) (q : U64) : Prop := + OffFrame (L := L) Z s σ q ∧ ∃ k, (k = 1 ∨ k = 2 ∨ k = 3) ∧ + ∃ ao, σ.kheap q.bv = some (.archObj ao) ∧ Fl4Spec.aaType ao = tabT k + +private theorem nativeOff_notGuest {k : Nat} {ao : Fl4Spec.ArchKernelObj} + (hk : k = 1 ∨ k = 2 ∨ k = 3) (ho : Fl4Spec.aaType ao = tabT k) : ¬ TableGuestObject ao := by + rcases hk with rfl | rfl | rfl + · obtain ⟨f, rfl⟩ := tabT_pt ho + exact id + · obtain ⟨f, rfl⟩ := tabT_pd ho + exact id + · obtain ⟨f, rfl⟩ := tabT_pdpt ho + exact id + +set_option maxHeartbeats 12000000 in +/-- Reconstruct the VM state after the selected table operation, including +the additional table frame on the off-set Unmap path. -/ +private theorem VmEptState.nativeTablePost + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {c : capability.Cap} (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok c) + (hTable : NativeTableCap c) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {O L3' L2' L1' : Set U64} + (hOld : XTopAt (L := L) AR Z s' a' σ') + (hX' : XTopAt (L := L) AR (Z.mv Z.G (Z.P, Z.L4, L3', L2', L1')) s' a' σ') + (hW0 : P1.WritesIn st s s' (fun b => VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW O b)) + (hS : ∀ q ∈ O, NativeOffTable (L := L) Z s σ q) + (hGr : Z.L3 ⊆ L3' ∧ Z.L2 ⊆ L2' ∧ Z.L1 ⊆ L1') + (hC : VCapOK Z.P Z.L4 L3' L2' L1' c) + (hPost : VsPostJ Z.th (L.mem s) (L.mem s') (cslotAddr (pc.bv, idx)) c σ σ') : + VEAnyX (L := L) arI AR s' a' σ' := by + obtain ⟨hSF, hLike, hOpSpec, hOp⟩ := hPost + have B := J.1.1 + set x := cslotAddr (pc.bv, idx) with hxdef + obtain ⟨s1, σ1, Lst1, hs1, hst1, J1⟩ := VmEptState.restart (arI := arI) J hni + have B1 := J1.1.1 + have Cv1 := J1.1.2 + have C1 := J1.2 + have hct := P1.cur_thread_ok (L := L) s + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have htcb : Z.th (L.curW s) = some .tcb := (B.xtop.curTcb hcur).1 + have e1 : σ1 = stsAct (updK σ (L.curW s).bv (.tcb { xct with tcbState := .restart })) (L.curW s).bv := by + have hst := hst1 + rw [hcur, Notif.setThreadState_eq .restart hkt] at hst + have := congrArg (fun q => ((), σ1) ∈ q.1) hst + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at this + exact of_eq_true this + have hk1 : ∀ o, o ≠ (L.curW s).bv → σ1.kheap o = σ.kheap o := by + intro o ho + rw [e1, stsAct_kheap] + simp [updK, ho] + have hct1 : σ1.kheap (L.curW s).bv = some (.tcb { xct with tcbState := .restart }) := by + rw [e1, stsAct_kheap] + simp [updK] + have har1 : σ1.archState = σ.archState := by rw [e1]; unfold stsAct; split <;> rfl + have F : VsSFr σ1 σ' := vsSFr_of_post hst1 hOpSpec + have hBack : ∀ o ao, σ'.kheap o = some (.archObj ao) → ∃ ao0, σ.kheap o = some (.archObj ao0) := by + intro o ao ho + obtain ⟨ko, hk, -, -, -, -, -, ha⟩ := F.kback o _ ho + obtain ⟨ao0, rfl⟩ := ha ⟨ao, rfl⟩ + have hne : o ≠ (L.curW s).bv := fun e => by rw [e, hct1] at hk; cases hk + exact ⟨ao0, by rw [← hk1 _ hne]; exact hk⟩ + obtain ⟨hat, hKeep⟩ := tableSpecFrame hTable hOp hBack + have harch : ∀ o ao, TableGuestObject ao → + (σ1.kheap o = some (.archObj ao) ↔ σ'.kheap o = some (.archObj ao)) := by + intro o ao ho + by_cases e : o = (L.curW s).bv + · rw [e, hct1] + constructor + · intro h; cases h + · intro h + have hk := (hKeep _ _ ho).mpr h + rw [hkt] at hk + cases hk + · rw [hk1 _ e] + exact hKeep _ _ ho + have hat1 : σ'.archState.x64AsidTable = σ1.archState.x64AsidTable := by rw [har1]; exact hat + have hTable1 : ∀ q ∈ O, ∃ ao, σ1.kheap q.bv = some (.archObj ao) ∧ + Fl4Spec.objBits (.archObj ao) = 12 ∧ ¬ TableGuestObject ao := by + intro q hq + obtain ⟨-, k, hk, ao, ho, ht⟩ := hS q hq + have hne : q.bv ≠ (L.curW s).bv := fun e => by rw [e, hkt] at ho; cases ho + exact ⟨ao, by rw [hk1 _ hne]; exact ho, objBits_tabT ht, nativeOff_notGuest hk ht⟩ + have hXa : TabsArch O σ1 := by + intro q hq + obtain ⟨ao, ho, hb, -⟩ := hTable1 q hq + exact ⟨(hS q hq).1.1, ao, ho, hb⟩ + have hP := B.xtop.rel.1 + have hact := B.kern.act + have hF2 : Frame2 (L := L) s s1 (P1.stA (L.curW s)) (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have h := set_thread_state_frame (L := L) s (L.curW s) sched.TS_RESTART (stA_al hP htcb) hact.1 + rw [hs1, WP.spec_ok] at h + exact h + have hSS1 : SlotsSame Z.th (L.mem s) (L.mem s1) := by + have h := set_thread_state_slots (L := L) Z.th hP s (L.curW s) sched.TS_RESTART htcb hact + rw [hs1, WP.spec_ok] at h + exact h + have hkc1 : L.ksb s1 = L.ksb s ∧ L.cpu s1 = L.cpu s := by + have h := set_thread_state_cases (L := L) s (L.curW s) sched.TS_RESTART + rw [hs1, WP.spec_ok] at h + rcases h with rfl | rfl <;> simp + have hks' : L.ksb s' = L.ksb s := hW0.1 + -- the TCBs' state words and the action word, pinned + have pinS : ∀ t, Z.th t = some .tcb → L.mem s' (P1.stA t) = L.mem s1 (P1.stA t) := by + intro t ht + have htT := B.thT t ht + obtain ⟨y1, hk1, hR1⟩ := B1.xtop.ipc.tcb t htT + obtain ⟨y, hky, hRy⟩ := hOld.ipc.tcb t htT + obtain ⟨y', hy', hc⟩ := F.tcbF hk1 + rw [hky] at hy'; cases hy' + have h1 : (L.mem s' (tStA t)).val = (L.mem s1 (tStA t)).val := by rw [hRy.st, hR1.st, hc.st] + rw [stA_tStA]; exact UScalar.eq_of_val_eq h1 + have pinA : L.mem s' (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) = + L.mem s1 (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + have a1 := B1.xtop.fin.1.1.1.act + have a2 := hOld.fin.1.1.1.act + rw [F.act] at a2 + have e := Sched.ActRel.det a2 a1 + obtain ⟨Q, R, hc, -⟩ := B.xtop.fin.1.1.1.lists + unfold nodeW at e + rw [P1.nodeAt_of_ksb hks', P1.nodeAt_of_ksb hkc1.1, ← hc.cur] at e + exact e + -- the slots + have hSlot : ∀ y, IsSlot Z.th y → L.mem s' (wordOf y 16#u64) = L.mem s1 (wordOf y 16#u64) ∧ + L.mem s' (wordOf y 24#u64) = L.mem s1 (wordOf y 24#u64) ∧ + (y ≠ cslotAddr (pc.bv, idx) → L.mem s' y = L.mem s1 y ∧ L.mem s' (wordOf y 8#u64) = L.mem s1 (wordOf y 8#u64)) := by + intro y hy + obtain ⟨a0, a8, a16, a24⟩ := hSS1 y hy + obtain ⟨b16, b24, bx⟩ := hSF y hy + exact ⟨by rw [b16, a16], by rw [b24, a24], fun hne => ⟨by rw [(bx hne).1, a0], by rw [(bx hne).2, a8]⟩⟩ + -- FL4's writes after the restart + have hW : P1.WritesIn st s1 s' (fun b => VsXW Z (L.ksb s1) (cslotAddr (pc.bv, idx)) b ∨ PtW O b) := by + refine ⟨hks'.trans hkc1.1.symm, hW0.2.1.trans hkc1.2.symm, fun b hb hn => ?_⟩ + have hslw : ∀ y, IsSlot Z.th y → ∀ k : U64, Off k → b = wordOf y k → L.mem s' b = L.mem s1 b := by + intro y hy k hk e + subst e + obtain ⟨h16, h24, hne⟩ := hSlot y hy + rcases hk with rfl | rfl | rfl | rfl + · by_cases ex : y = cslotAddr (pc.bv, idx) + · exact (hn (Or.inl (Or.inr (Or.inl (by rw [wordOf_zero, ex]))))).elim + · rw [wordOf_zero]; exact (hne ex).1 + · by_cases ex : y = cslotAddr (pc.bv, idx) + · exact (hn (Or.inl (Or.inr (Or.inr (by rw [ex]))))).elim + · exact (hne ex).2 + · exact h16 + · exact h24 + by_cases hb0 : VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW O b + · rcases hb0 with (⟨y, hy, e | e⟩ | ⟨y, k, hy, hk, e⟩ | hpt | hvw | ⟨t, ht, e⟩ | e) | hqw + · exact hslw y hy 0#u64 (Or.inl rfl) (by rw [wordOf_zero]; exact e) + · exact hslw y hy 8#u64 (Or.inr (Or.inl rfl)) e + · exact hslw y hy k hk e + · exact (hn (Or.inl (Or.inl (Or.inl hpt)))).elim + · exact (hn (Or.inl (Or.inl (Or.inr (by rw [hkc1.1]; exact hvw))))).elim + · subst e; exact pinS t ht + · subst e; exact pinA + · exact (hn (Or.inr hqw)).elim + · have e0 : L.mem s' b = L.mem s b := hW0.2.2 b hb hb0 + have e1 : L.mem s1 b = L.mem s b := hF2 b hb (fun e => hb0 (Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl + ⟨L.curW s, htcb, UScalar.eq_of_val_eq e⟩))))))) (fun e => hb0 (Or.inl (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr + (UScalar.eq_of_val_eq e)))))))) + rw [e0, e1] + -- the invoked slot's cap + have hcv0 : capV (L.mem s) (cslotAddr (pc.bv, idx)) = c := capV_of_capAt hc + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hcv1 : capV (L.mem s1) (cslotAddr (pc.bv, idx)) = c := by rw [hSS1.capV hA.isSlot]; exact hcv0 + have hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pc.bv, idx) → capV (L.mem s') y = capV (L.mem s1) y := by + intro y hy hne + obtain ⟨-, -, h⟩ := hSlot y hy + unfold capV; rw [(h hne).1, (h hne).2] + have Ap : VsApart (L := L) Z H Cs s1 := ⟨B1.scOut⟩ + have hvc : VsSlotsW Z.th Z.P Z.L4 L3' L2' L1' (L.mem s') := by + intro y hy + by_cases e : y = x + · rw [e] + exact (hLike.2 _ _ _ _ _ hC).toW + · rw [hcap y hy e] + exact (B1.vsc y hy).mono subset_rfl subset_rfl hGr.1 hGr.2.1 hGr.2.2 + have hvn : Z.vx = ∅ → VsNamed Z.th (L.mem s') (L.ksb s') := fun hvx => by + obtain ⟨hPl, ⟨-, -, hML, -, hMI⟩, hFI0, -⟩ := B.xtop.rel + have hR' : VsRel Z.P (L.mem s') (L.ksb s) σ' := by rw [← hks']; exact hOld.fin.2.1 + have h := (B.carry.vsNamed hvx).ofOpF B.vsc B.xtop.fin.2.1 B.xtop.fin.2.2.1 B.xtop.fin.2.2.2.1 B.xtop.fin.1.2.pt + hR' hOld.fin.2.2.1 hA.isSlot hcv0 hSF hOp (fun hf hk => by + have hk' : Vs.VKey c ≠ none := by rw [← hcv0]; exact hk + have hnz : NeedsFin c ∧ isZb c = false := by + cases c <;> first | exact absurd rfl hk' | exact ⟨Or.inl ⟨trivial, rfl⟩, rfl⟩ + have hx0 : capV (L.mem s) x ≠ .Null := fun e => hk (by rw [e]; rfl) + exact Vs.huq_of_finF hPl hML hMI hFI0 ((hML.members _).2 ⟨hA.isSlot, _, capAt_capV _ _, hx0⟩) + (by rw [hcv0]; exact hnz.1) (by rw [hcv0]; exact hnz.2) hf hk) + rw [hks'] + exact h + have hShape : shape (capV (L.mem s') x) = shape (capV (L.mem s1) x) := by + rw [hcv1]; exact hLike.1.1 + have hPostC : NativeTableCap (capV (L.mem s') x) := tableCapOfShape hTable hLike.1.1 + have hNu : isUt c = false := by cases c <;> (first | rfl | exact hTable.elim) + have B' := B1.vsStepJXB Ap hpc hidx hXa hX' F hW hcap hShape (by rw [hcv1]; exact hLike.1.2.1) + (by rw [hcv1]; exact hNu) (fun y hy => ⟨(hSlot y hy).1, (hSlot y hy).2.1⟩) hvc hGr hvn + have sw : ∀ b, (b = x ∨ b = wordOf x 8#u64) → SlotWd Z.th b := by + rintro b (rfl | rfl) + · exact ⟨x, hA.isSlot, 0#u64, Or.inl rfl, by rw [wordOf_zero]⟩ + · exact ⟨x, hA.isSlot, 8#u64, Or.inr (Or.inl rfl), rfl⟩ + let W := fun b => VsXW Z (L.ksb s1) x b ∨ PtW O b + have hOffW : ∀ b, PtW O b → ∃ q ∈ O, q.val ≤ b.val ∧ b.val + 8 ≤ q.val + 4096 := + fun b hb => by + obtain ⟨q, hq, -, -, -, -, h1, h2, -⟩ := tabW_val hXa hb + exact ⟨q, hq, h1, h2⟩ + have nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val := by + intro u hu b hb + rcases hb with (hb | hb) | hb + · obtain ⟨vx, hvx⟩ := Cv1.vm.spec u hu + have := B1.vsW_apart (t := u) (n := 32768) (fun o ko hk hne => Cv1.vm.sep u hu o ko hk hne) (Cv1.vm.ks u hu) + (fun q hq e => by + obtain ⟨ao, hk, -, -, hnv⟩ := B1.vsFrSpec hq + rw [e, hvx] at hk; cases hk; exact hnv vx rfl) hb + omega + · exact B1.slotWd_vcpu Cv1.vm (sw b hb) u hu + · obtain ⟨q, hq, hb1, hb2⟩ := hOffW b hb + obtain ⟨ao, hqk, hb12, hnq⟩ := hTable1 q hq + obtain ⟨vx, hvx⟩ := Cv1.vm.spec u hu + have hne : q.bv ≠ u.bv := fun e => by + rw [e, hvx] at hqk + cases hqk + exact hnq trivial + have e := Cv1.vm.sep u hu q.bv (.archObj ao) hqk hne + unfold VcpuApart at e + rw [hb12] at e + have hval : q.bv.toNat = q.val := rfl + rw [hval] at e + omega + have nX : ∀ b, W b → ∀ t ∈ X, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + rcases hb with (hb | hb) | hb + · obtain ⟨es, hxe, -⟩ := C1.rel.obj t ht + have := B1.vsW_apart (t := t) (n := 4096) (fun o ko hk hne => C1.sep t ht o ko hk hne) (C1.ks t ht) + (fun q hq e => by + obtain ⟨ao, hk, -, hne, -⟩ := B1.vsFrSpec hq + rw [e, hxe] at hk; cases hk; exact hne _ es rfl) hb + omega + · exact JAtZB.slotWd_ept B1 C1 b (sw b hb) t ht + · obtain ⟨q, hq, hb1, hb2⟩ := hOffW b hb + obtain ⟨ao, hqk, hb12, hnq⟩ := hTable1 q hq + obtain ⟨es, hqe, -⟩ := C1.rel.obj t ht + have hne : q.bv ≠ t.bv := fun e => by + rw [e, hqe] at hqk + cases hqk + exact hnq trivial + have e := C1.sep t ht q.bv (.archObj ao) hqk hne + unfold EptApart at e + rw [hb12] at e + have hval : q.bv.toNat = q.val := rfl + rw [hval] at e + omega + have hv : VKeep (L := L) Z.th s1 s' := by + intro t ht + have htT := B1.thT t ht + obtain ⟨Q, R0, hcq, -⟩ := B1.xtop.fin.1.1.1.lists + have hT := hcq.tcbs + have ev := tVcpuA_val hT htT + refine hW.mem_eq (tVcpuA_aligned hT htT) fun hb => ?_ + rcases hb with (hb | hb) | hb + · have hP1 := B1.xtop.fin.1.2 + have hO : OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s1 σ1 (tVcpuA t) := + Or.inr (Or.inr (Or.inl ⟨t, htT, by omega, by omega⟩)) + have hvo : VObj Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s1) (tVcpuA t) := by + rcases hb with ⟨q, hq, i, hi, e⟩ | h + · rw [e]; exact table_vobj hP1.pt hq hi _ + · exact vsWrite_vobj hP1.vs h + exact hP1.disj _ hO hvo + · exact B1.slotWd_tcb (sw _ hb) ht (by omega) (by omega) rfl + · have h := B1.tabObjW hXa hb (B1.zH.1 t htT) (fun _ e => by cases e) + simp only [P1.HKind.bits] at h + omega + have hh : ∀ b, W b → b ≠ P1.vHeadA st s1 := by + intro b hb + rcases hb with (hb | hb) | hb + · exact B1.vsW_head hb + · exact B1.slotWd_head (sw b hb) + · intro e + obtain ⟨q, hq, hb1, hb2⟩ := hOffW b hb + have hhead : b.val = (L.ksb s).val + 80 := by + rw [e, P1.vHeadA, vHead_val B1.kern.ksbFit, hkc1.1] + exact ((hS q hq).1.2 b hb1 (by omega)).1 + (Or.inr (Or.inl ⟨by omega, by omega, Or.inl (by omega)⟩)) + have hvk : ∀ u ∈ V, σ'.kheap u.bv = σ1.kheap u.bv := by + intro u hu + obtain ⟨v, h⟩ := Cv1.vm.spec u hu + exact ((harch u.bv (.vcpu v) trivial).mp h).trans h.symm + have hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ1.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM := fun o x' ho hx => by + rcases F.kh o with e | ⟨a0, a1, -, h1, -⟩ | ⟨r', cs0, cs1, -, h1⟩ | ⟨y0, y1, h0, h1, hco⟩ + · exact ⟨x', by rw [← e]; exact ho, hx⟩ + · rw [h1] at ho; cases ho + · rw [h1] at ho; cases ho + · rw [h1] at ho; cases ho; exact ⟨y0, h0, by rw [← hco.1]; exact hx⟩ + have hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ1.kheap p = some (.archObj (.vcpu vs)) := fun o v => (harch o (.vcpu v) trivial).mpr + have hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ1.kheap o = some ko ∧ + Fl4Spec.objBits ko' = Fl4Spec.objBits ko := by + intro o ko' ho + obtain ⟨ko, hk, ht⟩ := F.ksz o ko' ho + exact ⟨ko, hk, (objBits_of_aType ht).symm⟩ + have hcs' : ∀ y, IsSlot Z.th y → capV (L.mem s') y = capV (L.mem s1) y ∨ + (shape (capV (L.mem s') y) = shape (capV (L.mem s1) y) ∧ VcpuCapV V (capV (L.mem s') y) ∧ + ∀ f l o, capV (L.mem s') y ≠ .IoPort f l o) := by + intro y hy + by_cases e : y = x + · subst y + refine Or.inr ⟨hShape, ?_⟩ + generalize e' : capV (L.mem s') x = cx at hPostC ⊢ + cases cx <;> first | exact False.elim hPostC | exact ⟨trivial, fun _ _ _ e => by cases e⟩ + · exact Or.inl (hcap y hy e) + have Cv' := Cv1.ofNativeTableWrites B1 B' hW hcs' nV hh hv hvk hsz hrv hnv + have hEptSlot : EptSlotStep Z.th Z.th (L.mem s1) (L.mem s') := by + intro t ht + have h := cnode_slot_ne_tcb B1.xtop.rel.1 hpc hidx ht 5 (by decide) + exact Or.inr (Or.inr ⟨ht, hcap _ (isSlot_eptSlot ht) h.symm⟩) + have hck : ∀ y, IsSlot Z.th y → ∀ p l a v mm, capV (L.mem s1) y = .Ept p l mm a v → + capV (L.mem s') y = .Ept p l mm a v := by + intro y hy p l a v mm h + have hne : y ≠ x := by + intro e + rw [e, hcv1] at h + rw [h] at hTable + exact hTable.elim + exact (hcap y hy hne).trans h + have hcb : ∀ y, IsSlot Z.th y → ∀ p l a v mm, capV (L.mem s') y = .Ept p l mm a v → + capV (L.mem s1) y = .Ept p l mm a v := by + intro y hy p l a v mm h + have hne : y ≠ x := by + intro e + rw [← e, h] at hPostC + exact hPostC.elim + exact (hcap y hy hne).symm.trans h + have hpools : ∀ hi lo e, PoolAt σ' hi lo e → PoolAt σ1 hi lo e := by + rintro hi lo e ⟨pool, f, htab, hpool, hent⟩ + exact ⟨pool, f, by rw [← hat1]; exact htab, (harch pool (.asidPool f) trivial).mpr hpool, hent⟩ + have hepts : ∀ o l es, σ'.kheap o = some (.archObj (.ept l es)) → + σ1.kheap o = some (.archObj (.ept l es)) := fun o l es => (harch o (.ept l es) trivial).mpr + have hnm := C1.named.frameA hat1 (fun hi lo e he _ => hpools hi lo e he) hepts + (fun y p l a v hy h _ => ⟨y, hy, hck y hy p l a v true h⟩) + (fun y p l mm a v hy h => ⟨y, hy, hcb y hy p l a v mm h⟩) + (fun _ _ _ _ _ _ y p l a v hy _ hl h => ⟨y, l, a, v, hy, hl, hck y hy p l a v true h⟩) + have hEx : JExtEpt (L := L) X Z.th s' := by + refine ⟨fun y hy => ?_, ⟨fun y hy p l a v h => C1.exte.vtop.1 y hy p l a v (hcb y hy p l a v true h), ?_⟩, + fun y hy p l a v h => C1.exte.anz y hy p l a v (hcb y hy p l a v true h)⟩ + · by_cases e : y = x + · rw [e] + generalize e' : capV (L.mem s') x = cx at hPostC ⊢ + cases cx <;> first | exact False.elim hPostC | trivial + · rw [hcap y hy e]; exact C1.exte.cap y hy + · intro y hy p sz r d a v h + by_cases e : y = x + · rw [← e, h] at hPostC + exact hPostC.elim + · rw [hcap y hy e] at h + exact C1.exte.vtop.2 y hy p sz r d a v h + have J' : VmEptState (L := L) arI V X XL AR (Z.mv Z.G (Z.P, Z.L4, L3', L2', L1')) H Cs Ac Lst1 D s' a' σ' := by + refine ⟨⟨B', Cv'⟩, C1.frame hW nX + (fun y hy p l a v mm h => ⟨hy, hcb y hy p l a v mm h⟩) hEx + (fun t ht => ?_) (fun o l es h => ⟨l, es, hepts o l es h⟩) + (fun e ⟨hi, p, f, lo, htab, hk, hl⟩ => (hpools hi lo e ⟨p, f, htab, hk, hl⟩).guest) + hsz hEptSlot hnm⟩ + obtain ⟨es, hk, -⟩ := C1.rel.obj t ht + exact ((harch t.bv (.ept (XL t) es) trivial).mp hk).trans hk.symm + exact J'.anyX arI + + +private theorem nativeTableTop {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hFin : FinXR (L := L) AR Z s' a' σ') (hkb : L.ksb s' = L.ksb s) + (hCR : CRel Z.th (L.mem s') σ' Z.G) (hFI : FinInv Z.th (L.mem s') σ' Z.G) + (hMO : MdbOKV (L.ksb s) (L.mem s') Z.G) : + XTopAt (L := L) AR Z s' a' σ' := by + obtain ⟨hFin, ⟨_, hPl, -, -, hDR, -, hIA, hSA⟩, hE⟩ := hFin + obtain ⟨⟨⟨hSch, hEp, hNt, -⟩, -⟩, -⟩ := id hFin + exact ⟨hFin, ⟨hPl, hCR, hFI, hDR, by rw [hkb]; exact hMO, hIA, hSA⟩, + IpcRel.of hNt hEp (sched_sc hSch), hE⟩ + +private def SelectedTablePost (Z : XSets) (slot : U64) (c : capability.Cap) + (s0 : S) (a0 : A) (σ0 : Fl4Spec.State Unit) (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) : Prop := + (s' = s0 ∧ a' = a0 ∧ σ' = σ0) ∨ + (P1.WritesIn st s0 s' (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s0) ∧ + VsPostJ Z.th (L.mem s0) (L.mem s') slot c σ0 σ' ∧ + CRel Z.th (L.mem s') σ' Z.G ∧ FinInv Z.th (L.mem s') σ' Z.G ∧ + MdbOKV (L.ksb s0) (L.mem s') Z.G) + +private theorem VmEptState.nativeTableTracked + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {c : capability.Cap} (hc : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok c) + (hTable : NativeTableCap c) (hC : VCapOK Z.P Z.L4 Z.L3 Z.L2 Z.L1 c) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hF : FinXR (L := L) AR Z s' a' σ' ∧ + SelectedTablePost (L := L) Z (cslotAddr (pc.bv, idx)) c s a σ s' a' σ') : + VEAnyX (L := L) arI AR s' a' σ' := by + obtain ⟨hFin, hF⟩ := hF + rcases hF with ⟨rfl, rfl, rfl⟩ | ⟨hW, hPost, hCR, hFI, hMO⟩ + · exact J.anyX arI + have hX := nativeTableTop hFin hW.1 hCR hFI hMO + exact J.nativeTablePost hni hpc hidx hc hTable hX hX + (O := ∅) (hW.mono fun _ hb => Or.inl hb) (fun _ he => he.elim) + ⟨subset_rfl, subset_rfl, subset_rfl⟩ hC hPost + +private theorem VmEptState.nativeTableOff + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {pc : U64} {r : Nat} {idx : List Bool} {c : capability.Cap} {q : U64} + (hTable : NativeTableCap c) (hOff : NativeOffTable (L := L) Z s σ q) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hF : FinXR (L := L) AR Z s' a' σ' ∧ + VsJPostO (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.G pc r idx c q s a σ s' a' σ') : + VEAnyX (L := L) arI AR s' a' σ' := by + obtain ⟨hFin, hF⟩ := hF + rcases hF with ⟨rfl, rfl, rfl⟩ | ⟨hW, hpc, hidx, hc, _, hPost, hCR, hFI, hMO, hz, he⟩ + · exact J.anyX arI + have B := J.1.1 + have hX := nativeTableTop hFin hW.1 hCR hFI hMO + obtain ⟨W3, W2, W1, hk3, hk2, hk1, hqW, hok⟩ := TabEmptied.split he + obtain ⟨s1, σ1, Lst1, hs1, hst1, J1⟩ := J.restart hni + have hct := P1.cur_thread_ok (L := L) s + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have hσ1 : σ1 = stsAct (updK σ (L.curW s).bv (.tcb {xct with tcbState := .restart})) (L.curW s).bv := by + have h := hst1 + rw [hcur, Notif.setThreadState_eq .restart hkt] at h + have e := congrArg (fun z => ((), σ1) ∈ z.1) h + simp only [Set.mem_singleton_iff, Prod.mk.injEq, true_and] at e + exact of_eq_true e + have F := vsSFr_of_post hst1 hPost.2.2.1 + have hscB : ∀ p y n, σ'.kheap p = some (.schedContext y n) → σ.kheap p = some (.schedContext y n) := by + intro p y n hk + have h1 : σ1.kheap p = some (.schedContext y n) := + (F.look (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)).1 hk + rw [hσ1, stsAct_kheap] at h1 + by_cases e : p = (L.curW s).bv + · subst p; simp [updK] at h1 + · simpa [updK, e] using h1 + have hfr' : ∀ q', (q' ∈ W3 ∨ q' ∈ W2 ∨ q' ∈ W1) → ∀ b : U64, q'.val ≤ b.val → b.val < q'.val + 4096 → + ¬ OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ' b ∧ + ¬ VObj Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s') b ∧ + ∀ p k, Z.th p = some k → ¬ (p.val ≤ b.val ∧ b.val < p.val + objSize k) := by + intro q' hq' b h1 h2 + obtain rfl := hqW q' hq' + obtain ⟨n1, n2, n3⟩ := hOff.1.2 b h1 h2 + exact ⟨fun e => n1 (Vs.OObj.back hW.1 hscB e), by rw [hW.1]; exact n2, n3⟩ + have hX' := hX.addTab hk3 hk2 hk1 (fun q' h => by rw [hqW q' h]; exact hOff.1.1) + (fun q' h i hi => by rw [hqW q' h]; exact hz i hi) hfr' + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hC := B.vsc _ hA.isSlot + rw [capV_of_capAt hc] at hC + refine J.nativeTablePost hni hpc hidx hc hTable hX hX' hW ?_ + ⟨Set.subset_union_left, Set.subset_union_left, Set.subset_union_left⟩ (hok _ _ _ _ _ hC) hPost + intro q' hq' + rwa [Set.mem_singleton_iff.1 hq'] + +private theorem nativeTableExtra + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {extra : Array obj.Slot 3#usize} {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (hE : ExtraOK Z.th (L.mem s) extra exs) : + (vspace.extra extra 0#usize = ok none ∧ exs = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ exs = (vc', xsl) :: rest ∧ + ∀ p' asid', vc = .Pml4 p' true asid' → asid' ≠ 0#u64 ∧ asid'.val < 2 ^ 20 := by + rcases ExtraOK.relS J.xtop.rel.1 hE with ⟨hz, he⟩ | ⟨hnz, p, r, i, rest, hp, hi, he, hex⟩ + · left + exact ⟨by rw [vsExtra_at extra 0#usize (by decide) _ (exAt_idx0 extra), hz, if_pos rfl], he⟩ + · right + have hA : AbsSlot Z.th (p.bv, i) := ⟨p, rfl, Or.inl (by simpa [hi] using hp)⟩ + obtain ⟨vc, vc', hc, hRel, hAbs⟩ := J.xtop.rel.2.1.1.abs_cap hA + rw [← he] at hc + rw [capV_of_capAt hc] at hex + refine ⟨_, vc, vc', (p.bv, i), rest, ?_, hc, hRel, ?_, ?_⟩ + · rw [vsExtra_at extra 0#usize (by decide) _ (exAt_idx0 extra), if_neg hnz] + · rw [hRel.2] at hex + exact hex + · intro p' asid' h + have hS : IsSlot Z.th (exAt extra 0) := by rw [he]; exact hA.isSlot + have hV := J.vsc _ hS + rw [capV_of_capAt hc, h] at hV + exact hV.2.2 rfl + +private theorem nativeTableToDisp {MR : A → Fl4Spec.MachineState → Prop} + {on rc : Fl4Spec.ObjRef → Bool} {blk call don fp : Bool} {lbl : U64} + {args : List Fl4Spec.Data} {ci : Fl4Spec.CapRef} {sl : Fl4Spec.CslotPtr} {c : capability.Cap} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {buffer : Option Fl4Spec.ObjRef} + {Y : S → A → Fl4Spec.State Unit → Prop} {res : core.result.Result Unit error.Exc × S × A} + (hTable : NativeTableCap c) + (frame : ∀ s' a' σ', Y s' a' σ' → VEAnyX (L := L) arI AR s' a' σ') + (h : TopPostDMX (L := L) Y σ blk call don (specDec fp lbl.bv args ci sl c exs buffer) s res) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don (specDecV fp lbl.bv args ci sl c exs buffer) s res := by + have hEq : specDecV fp lbl.bv args ci sl c exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base (specDec fp lbl.bv args ci sl c exs buffer) := by + cases c <;> first | exact False.elim hTable | rfl + have hc : ∃ ac, absCap c = .archObjectCap ac := by + cases c <;> first | exact False.elim hTable | exact ⟨_, rfl⟩ + rw [hEq] + rcases h with ⟨e, he, hs, ⟨e', hm, hRel⟩, hY⟩ | ⟨i, hi, σ1, hst, y, hy, ⟨_, _, hr, -⟩, hY⟩ + · exact Or.inl ⟨e, he, hs, ⟨e', run_liftME_err hm, hRel⟩, frame _ _ _ hY⟩ + · obtain ⟨ac, hac⟩ := hc + have he : specDec fp lbl.bv args ci sl c exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeArchObject + (Fl4Spec.fl4ArchDecodeInvocation (Z := Unit) lbl.bv args ci sl ac exs) := by + unfold specDec Fl4Spec.fl4DecodeInvocation; rw [hac] + have hspec : Fl4Spec.fl4ArchDecodeInvocation (Z := Unit) lbl.bv args ci sl ac exs = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4ArchInvocation.spec + (Fl4Spec.archDecodeInvocation (Z := Unit) lbl.bv args ci sl ac exs) := by + cases c <;> (try exact False.elim hTable) <;> (simp only [absCap] at hac; cases hac; rfl) + have hi' := hi + rw [he] at hi' + obtain ⟨iv, rfl⟩ := decOut_liftME_inv hi' + -- a table cap decodes to a ported invocation, which the VM perform does not substitute + have hns : ¬ Fl4Spec.PerformSubst (.invokeArchObject iv) := by + have h3 := hi' + rw [hspec] at h3 + obtain ⟨j, hj⟩ := DecOut.liftME2_inv h3 + cases hj + exact id + have hy' : y ∈ ((Fl4Spec.fl4PerformInvocationVF (Z := Unit) on rc blk call don + (.base (.invokeArchObject iv))).run σ1).1 := by + rw [VirtF13.fl4PerformInvocationVF_base on rc blk call don _ hns]; exact hy + refine Or.inr ⟨.base (.invokeArchObject iv), ?_, σ1, hst, y, hy', Or.inl ?_⟩ + · exact hi.liftME + · rcases hr with ⟨h1, h2⟩ | ⟨_, h2⟩ + · exact ⟨h1, h2, frame _ _ _ hY⟩ + · exact (NoPre.perform_noErr blk call don (.invokeArchObject iv) (fun h => h) σ1 y hy () h2).elim + +set_option maxHeartbeats 12000000 in +/-- The selected native page table decoder on a VM state, for every label. +A mapped table outside its tracking set enters that set only after the checked +Unmap empties the concrete and abstract tables. +[J] The selected cap read, entered current thread and matched syscall arguments. +[P] Native VSpace architecture refinement. -/ +theorem VmEptState.selectedNativePageTable + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {ptr asid0 va0 : U64} {mp : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.PageTable ptr mp asid0 va0)) + {lbl ct : U64} (hct : sched.cur_thread st s = ok ct) + (len buf : U64) (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (extra : Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hE : ExtraOK Z.th (L.mem s) extra excaps) + (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.PageTable ptr mp asid0 va0) + blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) (.PageTable ptr mp asid0 va0) excaps buffer) s res ⦄ := by + have B := J.1.1 + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := B.xtop.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have hrun := B.curRun.run hni + have hkt' : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have htcb := (B.xtop.curTcb hcur).1 + have hact := B.kern.act + obtain ⟨hlM, hlU, hlO⟩ := pt_label lbl + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hw := B.vsc _ hA.isSlot + rw [capV_of_capAt hcap] at hw + have hext := nativeTableExtra B hE + by_cases hIn : ptr ∈ Z.L1 + · have hok := hw.okTab hIn + let tPost := fun s' a' σ' => FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR + (L.ksb s) σ s' a' σ' ∧ + SelectedTablePost (L := L) Z (cslotAddr (pc.bv, idx)) (.PageTable ptr mp asid0 va0) s a σ s' a' σ' + have hD := selected_page_table_core arI Z.th hS.2.pl s a σ Z.G hCR hFI hMO + (SR := FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) + (Q := fun s' => P1.WritesIn st s s' (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s)) + (fun _ _ _ h => h.2.mono fun _ hw => hw.toW) (X := tPost) + ⟨vs_x0T hS hR hPt hPI hCur hCR hFI hMO, Or.inl ⟨rfl, rfl, rfl⟩⟩ + ((vsPtMapHyps_top hRef).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + ((vsPtUnmapHyps_top hRef).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + ⟨hS, P1.WritesIn.refl _ _⟩ hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hPI hR hPt + lbl lbl.bv hlM hlU hlO len buf ct hct hcur htcb hact hrun args hlen hargs hpc hidx hcap + hok.1 hok.2.1 hok.2.2.1 hok.2.2.2 + (fun _ _ _ hSR hR' hPt' hCR' hFI' hMO' hK hPost => + ⟨vs_x_afterT hS hPI hCur hSR hR' hPt' hCR' hFI' hMO' hK, + Or.inr ⟨hSR.2.mono fun _ h => h.toW, hPost, hCR', hFI', hMO'⟩⟩) + extra excaps hext blk call don fp ci buffer + have hFrame : DomFrame (L := L) arI s a σ lbl len (cslotAddr (pc.bv, idx)) + (.PageTable ptr mp asid0 va0) blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + refine ⟨?_, dk_spec_arch ⟨_, rfl⟩ hkt'⟩ + exact WP.spec_mono hD fun _ hr => dom_of_foot hS hr.2 + have hD' := DecOKMX.toXD arI (DecOKMQX.toX arI hD fun _ _ _ h => h) hFrame + (okAll_specDec_pageTable fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) ptr mp asid0 va0 excaps buffer) hDR + (fun _ _ _ hx hd => And.intro (FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd) hx.2) + exact WP.spec_mono hD' fun _ hp => nativeTableToDisp (c := .PageTable ptr mp asid0 va0) trivial + (fun _ _ _ h => J.nativeTableTracked hni hpc hidx hcap trivial hok h) hp + · cases mp with + | false => exact (hIn (hw.1 rfl)).elim + | true => + have hOff := B.stage3OffTabOK _ hA.isSlot + rw [capV_of_capAt hcap] at hOff + obtain ⟨hTy, hOff⟩ := hOff hIn + have hNative : NativeOffTable (L := L) Z s σ ptr := by + obtain ⟨f, hf⟩ := hTy + exact ⟨hOff, 1, Or.inl rfl, .pageTable f, hf, rfl⟩ + have hext0 : (vspace.extra extra 0#usize = ok none ∧ excaps = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ excaps = (vc', xsl) :: rest := by + rcases hext with h | ⟨x, vc, vc', xsl, rest, hEx, hAt, hRel, hList, -⟩ + · exact Or.inl h + · exact Or.inr ⟨x, vc, vc', xsl, rest, hEx, hAt, hRel, hList⟩ + have hm0 := hw.2.2.1 + have hmu := hw.2.2.2 + obtain ⟨hoff, hnS, hno, hWV, hWO⟩ := offFr_of hS hOff.1 hOff.2 + have hD := decode_invocation_page_tableXO arI Z.th hS.2.pl s a σ Z.G hCR hFI hMO + (SR := FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (fun b => VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW {ptr} b)) + (Q := fun s' => P1.WritesIn st s s' (fun b => VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW {ptr} b)) + (fun _ _ _ h => h.2.mono fun _ hb => hb.imp_left fun h => h.toW) + (X := fun s' a' σ' => FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR (L.ksb s) σ s' a' σ' ∧ + VsJPostO (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.G pc r idx (.PageTable ptr true asid0 va0) ptr s a σ s' a' σ') + ⟨vs_x0T hS hR hPt hPI hCur hCR hFI hMO, Or.inl ⟨rfl, rfl, rfl⟩⟩ + (fun _ _ _ hSR hR' hPt' hCR' hFI' hMO' hK hJ hz1 hz2 => + ⟨vs_x_afterTO hS hPI hCur (fun b hb => hb.elim (foot0_not_vs hS) (hWV b)) hSR hR' hPt' hCR' hFI' hMO' hK, + Or.inr ⟨hSR.2.mono fun _ hb => hb.imp_left fun h => h.toW, hpc, hidx, hcap, rfl, hJ, hCR', hFI', hMO', + hz1, by simp only [TabEmptied, true_and]; first | exact hz2 | exact ⟨rfl, hz2⟩⟩⟩) + ((vsPtUnmapHyps_top hRef).foot (s0 := s) (fun _ h => Or.inl h) hS.2.pl) ⟨hS, P1.WritesIn.refl _ _⟩ + hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hPI hR hPt lbl lbl.bv hlM hlU hlO len buf ct hct hcur htcb hact + hrun args hlen hargs hpc hidx hcap rfl hTy hOff.1 hoff hnS (offStable_footPT (fun b h => Or.inr h) hno) hm0 hmu + extra excaps hext0 blk call don fp ci buffer + have hFrame : DomFrame (L := L) arI s a σ lbl len (cslotAddr (pc.bv, idx)) + (.PageTable ptr true asid0 va0) blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + refine ⟨?_, dk_spec_arch ⟨_, rfl⟩ hkt'⟩ + exact WP.spec_mono hD fun _ hr => dom_of_footO hS hWO hr.2 + have hD' := DecOKMX.toXD arI (DecOKMQX.toX arI hD fun _ _ _ h => h) hFrame + (okAll_specDec_pageTable fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) ptr true asid0 va0 excaps buffer) hDR + (fun _ _ _ hx hd => And.intro (FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd) hx.2) + exact WP.spec_mono hD' fun _ hp => nativeTableToDisp (c := .PageTable ptr true asid0 va0) trivial + (fun _ _ _ h => J.nativeTableOff (pc := pc) (r := r) (idx := idx) + (c := .PageTable ptr true asid0 va0) hni trivial hNative h) hp + +set_option maxHeartbeats 12000000 in +/-- The selected native page directory decoder on a VM state, for every label. +A mapped table outside its tracking set enters that set only after the checked +Unmap empties the concrete and abstract tables. +[J] The selected cap read, entered current thread and matched syscall arguments. +[P] Native VSpace architecture refinement. -/ +theorem VmEptState.selectedNativePageDirectory + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {ptr asid0 va0 : U64} {mp : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.PageDirectory ptr mp asid0 va0)) + {lbl ct : U64} (hct : sched.cur_thread st s = ok ct) + (len buf : U64) (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (extra : Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hE : ExtraOK Z.th (L.mem s) extra excaps) + (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.PageDirectory ptr mp asid0 va0) + blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) (.PageDirectory ptr mp asid0 va0) excaps buffer) s res ⦄ := by + have B := J.1.1 + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := B.xtop.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have hrun := B.curRun.run hni + have hkt' : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have htcb := (B.xtop.curTcb hcur).1 + have hact := B.kern.act + obtain ⟨hlM, hlU, hlO⟩ := pd_label lbl + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hw := B.vsc _ hA.isSlot + rw [capV_of_capAt hcap] at hw + have hext := nativeTableExtra B hE + by_cases hIn : ptr ∈ Z.L2 + · have hok := hw.okTab hIn + let tPost := fun s' a' σ' => FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR + (L.ksb s) σ s' a' σ' ∧ + SelectedTablePost (L := L) Z (cslotAddr (pc.bv, idx)) (.PageDirectory ptr mp asid0 va0) s a σ s' a' σ' + have hD := selected_page_directory_core arI Z.th hS.2.pl s a σ Z.G hCR hFI hMO + (SR := FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) + (Q := fun s' => P1.WritesIn st s s' (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s)) + (fun _ _ _ h => h.2.mono fun _ hw => hw.toW) (X := tPost) + ⟨vs_x0T hS hR hPt hPI hCur hCR hFI hMO, Or.inl ⟨rfl, rfl, rfl⟩⟩ + ((vsPdMapHyps_top hRef).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + ((vsPdUnmapHyps_top hRef).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + ⟨hS, P1.WritesIn.refl _ _⟩ hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hPI hR hPt + lbl lbl.bv hlM hlU hlO len buf ct hct hcur htcb hact hrun args hlen hargs hpc hidx hcap + hok.1 hok.2.1 hok.2.2.1 hok.2.2.2 + (fun _ _ _ hSR hR' hPt' hCR' hFI' hMO' hK hPost => + ⟨vs_x_afterT hS hPI hCur hSR hR' hPt' hCR' hFI' hMO' hK, + Or.inr ⟨hSR.2.mono fun _ h => h.toW, hPost, hCR', hFI', hMO'⟩⟩) + extra excaps hext blk call don fp ci buffer + have hFrame : DomFrame (L := L) arI s a σ lbl len (cslotAddr (pc.bv, idx)) + (.PageDirectory ptr mp asid0 va0) blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + refine ⟨?_, dk_spec_arch ⟨_, rfl⟩ hkt'⟩ + exact WP.spec_mono hD fun _ hr => dom_of_foot hS hr.2 + have hD' := DecOKMX.toXD arI (DecOKMQX.toX arI hD fun _ _ _ h => h) hFrame + (okAll_specDec_pageDirectory fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) ptr mp asid0 va0 excaps buffer) hDR + (fun _ _ _ hx hd => And.intro (FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd) hx.2) + exact WP.spec_mono hD' fun _ hp => nativeTableToDisp (c := .PageDirectory ptr mp asid0 va0) trivial + (fun _ _ _ h => J.nativeTableTracked hni hpc hidx hcap trivial hok h) hp + · cases mp with + | false => exact (hIn (hw.1 rfl)).elim + | true => + have hOff := B.stage3OffTabOK _ hA.isSlot + rw [capV_of_capAt hcap] at hOff + obtain ⟨hTy, hOff⟩ := hOff hIn + have hNative : NativeOffTable (L := L) Z s σ ptr := by + obtain ⟨f, hf⟩ := hTy + exact ⟨hOff, 2, Or.inr (Or.inl rfl), .pageDirectory f, hf, rfl⟩ + have hext0 : (vspace.extra extra 0#usize = ok none ∧ excaps = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ excaps = (vc', xsl) :: rest := by + rcases hext with h | ⟨x, vc, vc', xsl, rest, hEx, hAt, hRel, hList, -⟩ + · exact Or.inl h + · exact Or.inr ⟨x, vc, vc', xsl, rest, hEx, hAt, hRel, hList⟩ + have hm0 := hw.2.2.1 + have hmu := hw.2.2.2 + obtain ⟨hoff, hnS, hno, hWV, hWO⟩ := offFr_of hS hOff.1 hOff.2 + have hD := decode_invocation_page_directoryXO arI Z.th hS.2.pl s a σ Z.G hCR hFI hMO + (SR := FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (fun b => VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW {ptr} b)) + (Q := fun s' => P1.WritesIn st s s' (fun b => VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW {ptr} b)) + (fun _ _ _ h => h.2.mono fun _ hb => hb.imp_left fun h => h.toW) + (X := fun s' a' σ' => FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR (L.ksb s) σ s' a' σ' ∧ + VsJPostO (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.G pc r idx (.PageDirectory ptr true asid0 va0) ptr s a σ s' a' σ') + ⟨vs_x0T hS hR hPt hPI hCur hCR hFI hMO, Or.inl ⟨rfl, rfl, rfl⟩⟩ + (fun _ _ _ hSR hR' hPt' hCR' hFI' hMO' hK hJ hz1 hz2 => + ⟨vs_x_afterTO hS hPI hCur (fun b hb => hb.elim (foot0_not_vs hS) (hWV b)) hSR hR' hPt' hCR' hFI' hMO' hK, + Or.inr ⟨hSR.2.mono fun _ hb => hb.imp_left fun h => h.toW, hpc, hidx, hcap, rfl, hJ, hCR', hFI', hMO', + hz1, by simp only [TabEmptied, true_and]; first | exact hz2 | exact ⟨rfl, hz2⟩⟩⟩) + ((vsPdUnmapHyps_top hRef).foot (s0 := s) (fun _ h => Or.inl h) hS.2.pl) ⟨hS, P1.WritesIn.refl _ _⟩ + hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hPI hR hPt lbl lbl.bv hlM hlU hlO len buf ct hct hcur htcb hact + hrun args hlen hargs hpc hidx hcap rfl hTy hOff.1 hoff hnS (offStable_footPD (fun b h => Or.inr h) hno) hm0 hmu + extra excaps hext0 blk call don fp ci buffer + have hFrame : DomFrame (L := L) arI s a σ lbl len (cslotAddr (pc.bv, idx)) + (.PageDirectory ptr true asid0 va0) blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + refine ⟨?_, dk_spec_arch ⟨_, rfl⟩ hkt'⟩ + exact WP.spec_mono hD fun _ hr => dom_of_footO hS hWO hr.2 + have hD' := DecOKMX.toXD arI (DecOKMQX.toX arI hD fun _ _ _ h => h) hFrame + (okAll_specDec_pageDirectory fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) ptr true asid0 va0 excaps buffer) hDR + (fun _ _ _ hx hd => And.intro (FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd) hx.2) + exact WP.spec_mono hD' fun _ hp => nativeTableToDisp (c := .PageDirectory ptr true asid0 va0) trivial + (fun _ _ _ h => J.nativeTableOff (pc := pc) (r := r) (idx := idx) + (c := .PageDirectory ptr true asid0 va0) hni trivial hNative h) hp + +set_option maxHeartbeats 12000000 in +/-- The selected native PDPT decoder on a VM state, for every label. +A mapped table outside its tracking set enters that set only after the checked +Unmap empties the concrete and abstract tables. +[J] The selected cap read, entered current thread and matched syscall arguments. +[P] Native VSpace architecture refinement. -/ +theorem VmEptState.selectedNativePdpt + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {pc : U64} {r : Nat} {idx : List Bool} (hpc : Z.th pc = some (.cnode r)) (hidx : idx.length = r) + {ptr asid0 va0 : U64} {mp : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pc.bv, idx)) = ok (.Pdpt ptr mp asid0 va0)) + {lbl ct : U64} (hct : sched.cur_thread st s = ok ct) + (len buf : U64) (args : List U64) (hlen : len.val = args.length) + (hargs : ∀ (i : U64) x, args[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x) + (extra : Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hE : ExtraOK Z.th (L.mem s) extra excaps) + (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) + {MR : A → Fl4Spec.MachineState → Prop} (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr (pc.bv, idx)) (.Pdpt ptr mp asid0 va0) + blk call don fp buf extra ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) (.Pdpt ptr mp asid0 va0) excaps buffer) s res ⦄ := by + have B := J.1.1 + obtain ⟨hS, hR, hPt, hPI, hCur⟩ := B.xtop.fin + obtain ⟨hPl, hCR, hFI, hDR, hMO, hIA, -⟩ := B.xtop.rel + obtain ⟨hcur, xct, hkt, -⟩ := B.curRunnable hni hct + have hrun := B.curRun.run hni + have hkt' : σ.kheap σ.curThread = some (.tcb xct) := by rw [hcur]; exact hkt + have htcb := (B.xtop.curTcb hcur).1 + have hact := B.kern.act + obtain ⟨hlM, hlU, hlO⟩ := pdpt_label lbl + have hA : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl (by simpa [hidx] using hpc)⟩ + have hw := B.vsc _ hA.isSlot + rw [capV_of_capAt hcap] at hw + have hext := nativeTableExtra B hE + by_cases hIn : ptr ∈ Z.L3 + · have hok := hw.okTab hIn + let tPost := fun s' a' σ' => FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR + (L.ksb s) σ s' a' σ' ∧ + SelectedTablePost (L := L) Z (cslotAddr (pc.bv, idx)) (.Pdpt ptr mp asid0 va0) s a σ s' a' σ' + have hD := selected_pdpt_core arI Z.th hS.2.pl s a σ Z.G hCR hFI hMO + (SR := FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s)) + (Q := fun s' => P1.WritesIn st s s' (VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s)) + (fun _ _ _ h => h.2.mono fun _ hw => hw.toW) (X := tPost) + ⟨vs_x0T hS hR hPt hPI hCur hCR hFI hMO, Or.inl ⟨rfl, rfl, rfl⟩⟩ + ((vsPdptMapHyps_top hRef).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + ((vsPdptUnmapHyps_top hRef).foot (s0 := s) (W := VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s) (fun _ h => h) hS.2.pl) + ⟨hS, P1.WritesIn.refl _ _⟩ hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hPI hR hPt + lbl lbl.bv hlM hlU hlO len buf ct hct hcur htcb hact hrun args hlen hargs hpc hidx hcap + hok.1 hok.2.1 hok.2.2.1 hok.2.2.2 + (fun _ _ _ hSR hR' hPt' hCR' hFI' hMO' hK hPost => + ⟨vs_x_afterT hS hPI hCur hSR hR' hPt' hCR' hFI' hMO' hK, + Or.inr ⟨hSR.2.mono fun _ h => h.toW, hPost, hCR', hFI', hMO'⟩⟩) + extra excaps hext blk call don fp ci buffer + have hFrame : DomFrame (L := L) arI s a σ lbl len (cslotAddr (pc.bv, idx)) + (.Pdpt ptr mp asid0 va0) blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + refine ⟨?_, dk_spec_arch ⟨_, rfl⟩ hkt'⟩ + exact WP.spec_mono hD fun _ hr => dom_of_foot hS hr.2 + have hD' := DecOKMX.toXD arI (DecOKMQX.toX arI hD fun _ _ _ h => h) hFrame + (okAll_specDec_pdpt fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) ptr mp asid0 va0 excaps buffer) hDR + (fun _ _ _ hx hd => And.intro (FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd) hx.2) + exact WP.spec_mono hD' fun _ hp => nativeTableToDisp (c := .Pdpt ptr mp asid0 va0) trivial + (fun _ _ _ h => J.nativeTableTracked hni hpc hidx hcap trivial hok h) hp + · cases mp with + | false => exact (hIn (hw.1 rfl)).elim + | true => + have hOff := B.stage3OffTabOK _ hA.isSlot + rw [capV_of_capAt hcap] at hOff + obtain ⟨hTy, hOff⟩ := hOff hIn + have hNative : NativeOffTable (L := L) Z s σ ptr := by + obtain ⟨f, hf⟩ := hTy + exact ⟨hOff, 3, Or.inr (Or.inr rfl), .pdPointerTable f, hf, rfl⟩ + have hext0 : (vspace.extra extra 0#usize = ok none ∧ excaps = []) ∨ + ∃ x vc vc' xsl rest, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok vc ∧ + CapRel vc vc' ∧ excaps = (vc', xsl) :: rest := by + rcases hext with h | ⟨x, vc, vc', xsl, rest, hEx, hAt, hRel, hList, -⟩ + · exact Or.inl h + · exact Or.inr ⟨x, vc, vc', xsl, rest, hEx, hAt, hRel, hList⟩ + have hm0 := hw.2.2.1 + have hmu := hw.2.2.2 + obtain ⟨hoff, hnS, hno, hWV, hWO⟩ := offFr_of hS hOff.1 hOff.2 + have hD := decode_invocation_pdptXO arI Z.th hS.2.pl s a σ Z.G hCR hFI hMO + (SR := FootSR st (TopSR st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR) s + (fun b => VsFootW0 st Z.th Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW {ptr} b)) + (Q := fun s' => P1.WritesIn st s s' (fun b => VsFootW st Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 s b ∨ PtW {ptr} b)) + (fun _ _ _ h => h.2.mono fun _ hb => hb.imp_left fun h => h.toW) + (X := fun s' a' σ' => FinXT (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.N Z.T Z.C Z.E Z.R Z.B Z.c AR (L.ksb s) σ s' a' σ' ∧ + VsJPostO (L := L) Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 Z.G pc r idx (.Pdpt ptr true asid0 va0) ptr s a σ s' a' σ') + ⟨vs_x0T hS hR hPt hPI hCur hCR hFI hMO, Or.inl ⟨rfl, rfl, rfl⟩⟩ + (fun _ _ _ hSR hR' hPt' hCR' hFI' hMO' hK hJ hz1 hz2 => + ⟨vs_x_afterTO hS hPI hCur (fun b hb => hb.elim (foot0_not_vs hS) (hWV b)) hSR hR' hPt' hCR' hFI' hMO' hK, + Or.inr ⟨hSR.2.mono fun _ hb => hb.imp_left fun h => h.toW, hpc, hidx, hcap, rfl, hJ, hCR', hFI', hMO', + hz1, by simp only [TabEmptied, true_and]; first | exact hz2 | exact ⟨rfl, hz2⟩⟩⟩) + ((vsPdptUnmapHyps_top hRef).foot (s0 := s) (fun _ h => Or.inl h) hS.2.pl) ⟨hS, P1.WritesIn.refl _ _⟩ + hS.2.vs hS.2.sd hS.2.pt hS.2.ps hS.2.pv hS.2.po hPI hR hPt lbl lbl.bv hlM hlU hlO len buf ct hct hcur htcb hact + hrun args hlen hargs hpc hidx hcap rfl hTy hOff.1 hoff hnS (offStable_footPDPT (fun b h => Or.inr h) hno) hm0 hmu + extra excaps hext0 blk call don fp ci buffer + have hFrame : DomFrame (L := L) arI s a σ lbl len (cslotAddr (pc.bv, idx)) + (.Pdpt ptr true asid0 va0) blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci (pc.bv, idx) excaps buffer := by + refine ⟨?_, dk_spec_arch ⟨_, rfl⟩ hkt'⟩ + exact WP.spec_mono hD fun _ hr => dom_of_footO hS hWO hr.2 + have hD' := DecOKMX.toXD arI (DecOKMQX.toX arI hD fun _ _ _ h => h) hFrame + (okAll_specDec_pdpt fp lbl.bv (args.map UScalar.bv) ci (pc.bv, idx) ptr true asid0 va0 excaps buffer) hDR + (fun _ _ _ hx hd => And.intro (FinXT.toXR Z.G B.xtop.esa hIA.dom hx.1 hd) hx.2) + exact WP.spec_mono hD' fun _ hp => nativeTableToDisp (c := .Pdpt ptr true asid0 va0) trivial + (fun _ _ _ h => J.nativeTableOff (pc := pc) (r := r) (idx := idx) + (c := .Pdpt ptr true asid0 va0) hni trivial hNative h) hp + + +/-- The selected PageTable decode in the ordinary or first-phase syscall prefix. -/ +theorem VmEptState.selectedNativePageTableS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {lbl ct cptr len buf p asid va : U64} {mapped : Bool} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.PageTable p mapped asid va) ex args exs) + {MR : A → Fl4Spec.MachineState → Prop} + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.PageTable p mapped asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.PageTable p mapped asid va) exs buffer) s res ⦄ := by + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + have hs := J.selectedNativePageTable hni hRef hpc rfl q.capAt (lbl := lbl) q.current len buf + (args.map fun x => (⟨x⟩ : U64)) q.length q.words ex exs q.extra (Fl4Spec.toBl cptr.bv) buffer + (MR := MR) on rc blk call don fp + rw [show (args.map fun x => (⟨x⟩ : U64)).map UScalar.bv = args from zv_map_mk args] at hs + exact hs + +/-- The selected PageDirectory decode in the ordinary or first-phase syscall prefix. -/ +theorem VmEptState.selectedNativePageDirectoryS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {lbl ct cptr len buf p asid va : U64} {mapped : Bool} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.PageDirectory p mapped asid va) ex args exs) + {MR : A → Fl4Spec.MachineState → Prop} + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.PageDirectory p mapped asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.PageDirectory p mapped asid va) exs buffer) s res ⦄ := by + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + have hs := J.selectedNativePageDirectory hni hRef hpc rfl q.capAt (lbl := lbl) q.current len buf + (args.map fun x => (⟨x⟩ : U64)) q.length q.words ex exs q.extra (Fl4Spec.toBl cptr.bv) buffer + (MR := MR) on rc blk call don fp + rw [show (args.map fun x => (⟨x⟩ : U64)).map UScalar.bv = args from zv_map_mk args] at hs + exact hs + +/-- The selected Pdpt decode in the ordinary or first-phase syscall prefix. -/ +theorem VmEptState.selectedNativePdptS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hRef : ArchRefines arI AR) + {lbl ct cptr len buf p asid va : U64} {mapped : Bool} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Pdpt p mapped asid va) ex args exs) + {MR : A → Fl4Spec.MachineState → Prop} + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Pdpt p mapped asid va) + blk call don fp buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Pdpt p mapped asid va) exs buffer) s res ⦄ := by + obtain ⟨pc, hp, hpc⟩ := q.slot + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + have hs := J.selectedNativePdpt hni hRef hpc rfl q.capAt (lbl := lbl) q.current len buf + (args.map fun x => (⟨x⟩ : U64)) q.length q.words ex exs q.extra (Fl4Spec.toBl cptr.bv) buffer + (MR := MR) on rc blk call don fp + rw [show (args.map fun x => (⟨x⟩ : U64)).map UScalar.bv = args from zv_map_mk args] at hs + exact hs + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3TcbInst.lean b/proofs/Fl4Proofs/P3/VmEntryS3TcbInst.lean new file mode 100644 index 000000000..42f11fadb --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3TcbInst.lean @@ -0,0 +1,1627 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, route D): **the TCB cap installs at the VM record**. + +The chain of XTopJointMRTcb's `install_tcb_capJMp` … `thread_control_capsJMp` over the VM deletion loop's invariant +`WMR MR (WVE …)` (P3/VmDelCap): `InsWV` (the loop's invariant `W` / `Wp` with the record kept by an insert into an empty +TCB slot, `insTcbV`, the deletion `VmEptState.capDelete`, `BufHypW`), `InsWV` at `WMR MR (WVE …)` (`insWV_ve`), the +installs with spec `installTcbCapVF` / `installTcbFrameCapVF` (the deletion `capDeleteVF`). Generated by +`handoff/tools/gen_tcbinst.py`. A new cap's VT-x view `VtOK` against its source's is a hypothesis of each install (the +decoders' derived caps give it: `derive_vt`, a valid fault handler or root has none). + +No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.VmDelCap +import Fl4Proofs.P3.VmEntryS3InsTcb +import Fl4Proofs.P3.JAtZBTcbBuf +import Fl4Proofs.P3.XTopJointMRTcb +import Fl4Proofs.P3.XTopJointMRTcbL +import Fl4Proofs.P3.DeriveVt +import Fl4Proofs.P3.VmDisp +import Fl4Proofs.P3.JAtZBThrPost +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs : Set U64} +variable {Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + +/-- The spec's `cap_delete` of an empty slot with the VM-aware delete does nothing. -/ +theorem capDeleteVF_null_mem {sl : Fl4Spec.CslotPtr} {σ : Fl4Spec.State Unit} + (hg : Fl4Spec.getCap (Z := Unit) sl σ = ({(.nullCap, σ)}, False)) : + (.ok (), σ) ∈ ((Fl4Spec.capDeleteVF (Z := Unit) sl).run σ).1 := by + have he : Fl4Spec.emptySlot (Z := Unit) sl .nullCap σ = ({((), σ)}, False) := by + unfold Fl4Spec.emptySlot + rw [nd_bind_single hg]; simp + have hm : ((), σ) ∈ ((Fl4Spec.NonDet.when (true = true ∨ true = true) (Fl4Spec.emptySlot (Z := Unit) sl .nullCap)) σ).1 := by + simp only [Fl4Spec.NonDet.when, or_self, ↓reduceIte, he] + exact Set.mem_singleton _ + exact capDeleteG_ok (Fn := Fl4Spec.finaliseCapVF) (rd_cte_okG (rd_fin_nullG hg) hm).choose_spec + +/-- **The premises of `VmEptState.capDelete`** (P3/VmDelCap) as a bundle. -/ +structure VmDelPrem (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) : Prop where + hA : ArchRefines arI AR + hD : P0.StoreTotal st ∧ P0.ArchTotal arI ∧ MRKeepD arI AR + hK : ARKeep arI AR + hVA : ZvVirtAgree arI + hticks : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ (a : A) (v : U64), arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄ + hnd : Fl4Spec.numDomains = 1 + hM : MachPlatC arI MR + hKD : MRKeepD arI MR + hPA : PPPlatA arI AR + hAM : ARMask arI AR + +/-- **The TCB installs' invariants at the record** (module docstring): the loop's invariant `W` (after an Ok install) and +`Wp` (after a preempted one), the record `WVE` read off `W`, an insert into an empty TCB slot (the record after it given), +the IPC-buffer step, and the deletion (`VmEptState.capDelete`'s statement at `W` / `Wp`). -/ +structure InsWV (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) (Z : XSets) (H : P1.Heap) + (Cs : Set U64) (kb : U64) (W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop) : Prop where + wvr : ∀ s a σ G, W s a σ G → WVE (L := L) arI AR Z H Cs kb s a σ G + any : ∀ s a σ G, W s a σ G → VEAnyX (L := L) arI AR s a σ + pre : ∀ s a σ G, Wp s a σ G → ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s a σ + ins : ∀ (s s' : S) (a : A) (σ σ' : Fl4Spec.State Unit) (G G' : MdbGhost) (c : Fl4Spec.Cap) + (src dst : Fl4Spec.CslotPtr), W s a σ G → WVE (L := L) arI AR Z H Cs kb s' a σ' G' → + Fl4Spec.capInsert (Z := Unit) c src dst σ = ({((), σ')}, False) → W s' a σ' G' + buf : ∀ t, Z.th t = some .tcb → ∀ addr : U64, BufHypW (L := L) W Z.th t addr addr.bv + del : ∀ (K : Nat), (∀ p r, Z.th p = some (.cnode r) → 2 ^ r ≤ K) → 6 ≤ K → ∀ (sl : Fl4Spec.CslotPtr), + AbsSlot Z.th sl → ∀ (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost), DState (L := L) Z.th s σ G → + W s a σ G → capV (L.mem s) (cslotAddr sl) ≠ .Null → psiD (L.mem s) G + 2 * K + 2 < 2 ^ 64 - 1 → + cdt.delete st arI s a (cslotAddr sl) true ⦃ r => ∃ σ' G', DState (L := L) Z.th r.2.1 σ' G' ∧ + psiD (L.mem r.2.1) G' ≤ psiD (L.mem s) G ∧ (nnS G').card ≤ (nnS G).card ∧ + MdbStep (L.mem s) G (L.mem r.2.1) G' ∧ + ((r.1 = .Ok () ∧ (.ok (), σ') ∈ ((Fl4Spec.capDeleteVF (Z := Unit) sl).run σ).1 ∧ + psiD (L.mem r.2.1) G' < psiD (L.mem s) G ∧ capV (L.mem r.2.1) (cslotAddr sl) = .Null ∧ + ∀ y, IsSlot Z.th y → y ≠ cslotAddr sl → ChgZ (L.mem s) (L.mem r.2.1) y) ∨ + (r.1 = .Err .Preempted ∧ (.error (), σ') ∈ ((Fl4Spec.capDeleteVF (Z := Unit) sl).run σ).1)) ∧ + WPost W Wp r.1 r.2.1 r.2.2 σ' G' ⦄ + +/-- `InsWV` at every kernel-state base. -/ +structure TcbInsWV (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) (Z : XSets) (H : P1.Heap) + (Cs : Set U64) (W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop) : Prop where + ins : ∀ kb, InsWV (L := L) arI AR MR Z H Cs kb (W kb) (Wp kb) + +/-- CnodeAll's bound and `DelE` over `JAtZB` (`JAtZ.delE`). -/ +theorem JAtZB.delE {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∃ K, (∀ p r, Z.th p = some (.cnode r) → 2 ^ r ≤ K) ∧ 6 ≤ K ∧ DelE (L := L) Z.th K s σ := + J.bound.delE J.del J.xtop.rel.2.2.1.kt J.finM + +/-- A valid fault handler's cap has no VT-x view (an endpoint or null cap), so it is its own `VtOK` copy. -/ +theorem vtOK_vfh {c : capability.Cap} (h : Fl4Spec.validFaultHandler (absCap c)) : VtOK c c := by + refine Or.inl ?_ + cases c <;> simp_all [vkeyF, absCap, Fl4Spec.validFaultHandler, Fl4Spec.isEpCap] + +/-- **The spec's cap installs of ThreadControl with the VM-aware delete** (`tcCapsSpec` over `installTcbCapVF` / +`installTcbFrameCapVF`). -/ +noncomputable def tcCapsSpecVF (t : U64) (ssl : Fl4Spec.CslotPtr) (flags : U64) + (fh tmh cr vr : capability.Cap × Fl4Spec.CslotPtr) (ptr : Fl4Spec.VSpaceRef) + (bfr : Option (capability.Cap × Fl4Spec.CslotPtr)) : Fl4Spec.PMonad Unit Unit := + Fl4Spec.installTcbCapVF t.bv ssl 3 (optArg ((flags &&& tcb.UPDATE_FAULT) ≠ 0#u64) fh) >>= fun _ => + Fl4Spec.installTcbCapVF t.bv ssl 4 (optArg ((flags &&& tcb.UPDATE_TIMEOUT) ≠ 0#u64) tmh) >>= fun _ => + Fl4Spec.installTcbCapVF t.bv ssl 0 (optArg ((flags &&& tcb.UPDATE_SPACE) ≠ 0#u64) cr) >>= fun _ => + Fl4Spec.installTcbCapVF t.bv ssl 1 (optArg ((flags &&& tcb.UPDATE_SPACE) ≠ 0#u64) vr) >>= fun _ => + Fl4Spec.installTcbFrameCapVF t.bv ssl + (if (flags &&& tcb.UPDATE_IPC_BUFFER) ≠ 0#u64 then some (ptr, frameOf bfr) else none) >>= fun _ => + Fl4Spec.NonDetE.returnOk () + +theorem installTcbCapVF_none (t : Fl4Spec.ObjRef) (sl : Fl4Spec.CslotPtr) (n : Nat) : + Fl4Spec.installTcbCapVF (Z := Unit) t sl n none = Fl4Spec.NonDetE.returnOk () := rfl + +theorem installTcbFrameCapVF_none (t : Fl4Spec.ObjRef) (sl : Fl4Spec.CslotPtr) : + Fl4Spec.installTcbFrameCapVF (Z := Unit) t sl none = Fl4Spec.NonDetE.returnOk () := rfl + +/-! ## The IPC-buffer step and the tail at the record -/ + +set_option maxHeartbeats 8000000 in +/-- **TcbCaps' `BufHypW` at the record's loop invariant `WVE`** (XTopJointTcbInstall's `bufHypW_J` over `JAtZB.bufWrF` / +`JAtZB.reschedJ`, `VmEptState.ofNat`). -/ +theorem bufHypW_VE [VxE Z] {t : U64} (ht : Z.th t = some .tcb) (kb addr : U64) : + BufHypW (L := L) (WVE (L := L) arI AR Z H Cs kb) Z.th t addr addr.bv := by + refine ⟨fun s a σ G hDS hW hnull => ?_, fun s a σ G _ hW => ?_⟩ + · obtain ⟨V, X, XL, Vv, Ac, Lst, D, JV, hkb⟩ := hW + have J := JV.st.1.1 + have hns := JV.ns + have hsg := JV.sg + have hp0 := JV.pp + have hX := J.xtop + have htT : t ∈ (Z.mv G Vv).T := J.thT t ht + obtain ⟨Q, R0, hc, hs⟩ := hX.fin.1.1.1.lists + obtain ⟨x, hk, -⟩ := hs.tcb t htT + have hta := hc.tcbs t htT + have hwa : P1.Aligned (tIpcBufA t) := tIpcBufA_aligned hta + refine WP.spec_mono (set_ipc_buffer_wr s t addr) ?_ + rintro s' rfl + have hstep := tcbBufStep_upd hk addr.bv + obtain ⟨V', J', F'⟩ := J.bufWrF htT hk addr (ipcframe_null_of hX.rel.2.1.1 ht hk hnull) + refine ⟨hX.slotsSame_of (P1.writesIn_wr hwa) (fun b hb => by subst hb; exact tcb_oobj htT hta (by decide) (by decide)), + _, ?_, J'.xtop.rel.2.1, hstep.ksub, V, X, XL, V', Ac, Lst, D, + ⟨JV.st.ofNat J' F', fun u e => hns u e, hsg.of_slotK (SlotK.ipcBuf hk addr.bv) rfl rfl, + hp0.of_ppk (PPK.updTcbSc hk rfl)⟩, by rw [L.ksb_wr]; exact hkb⟩ + rw [Notif.threadSet_eq _ hk]; rfl + · obtain ⟨V, X, XL, Vv, Ac, Lst, D, JV, hkb⟩ := hW + have J := JV.st.1.1 + have hns := JV.ns + have hsg := JV.sg + have hp0 := JV.pp + have hX := J.xtop + have hS := hX.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hcur : σ.curThread = (L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD)).bv := by + rw [hs.curThread, hc.cur]; rfl + rw [cur_thread_eq] + simp only [bind_ok, tcb_eq_ct] + by_cases e : t = L.mem s (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) + · simp only [e, decide_true, ↓reduceIte] + have hA1 : Notif.ActReady σ := J.inv.actReady + obtain ⟨σ', hres, hK', hdl', hds'⟩ := rescheduleRequired_k hA1 + obtain ⟨s5, hrun5, σ5, hres5, Ac5, Lst5, D5, J5, F5⟩ := J.reschedJ + have e3 : σ5 = σ' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hres5.symm.trans hres)))).2 + subst e3 + rw [hrun5, bind_ok, WP.spec_ok] + have hact5 := Sched.rescheduleRequired_action hA1 hres + have hgk := gk_rescheduleRequired' σ ((), σ5) (by rw [hres]; rfl) + refine ⟨rfl, rfl, F5.ss, σ5, ?_, J5.xtop.rel.2.1, KSub.of_kheap hK'.kh, V, X, XL, Vv, Ac5, Lst5, D5, + ⟨JV.st.ofNat J5 F5, (fun u e' => by rw [hact5] at e'; cases e'), + hsg.of_eq hK'.kh (congrArg Prod.fst hgk) (congrArg Prod.snd hgk), + hp0.of_ppk (PPK.of_schedK hK' (fun u e' => by rw [hact5] at e'; cases e') (rq_of_resched hns hres))⟩, + by rw [F5.fw.1]; exact hkb⟩ + refine mem_bindE_ok (a := σ.curThread) (s' := σ) (mem_liftE_ok (x := (σ.curThread, σ)) rfl) ?_ + have hb : t.bv = σ.curThread := by rw [hcur, e] + simp only [Fl4Spec.NonDet.when, hb, ↓reduceIte] + exact mem_liftE_ok (x := ((), σ5)) (by rw [hres]; rfl) + · simp only [e, decide_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + refine ⟨trivial, trivial, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩, σ, ?_, hX.rel.2.1, KSub.refl σ, V, X, XL, Vv, Ac, Lst, D, + ⟨JV.st, hns, hsg, hp0⟩, hkb⟩ + refine mem_bindE_ok (a := σ.curThread) (s' := σ) (mem_liftE_ok (x := (σ.curThread, σ)) rfl) ?_ + have hb : ¬ t.bv = σ.curThread := fun h => e (P1.eq_of_bv (h.trans hcur)) + simp only [Fl4Spec.NonDet.when, hb, ↓reduceIte] + exact mem_liftE_ok (x := ((), σ)) rfl + +/-- `BufHypW.mrv` at `MachPlatC`. -/ +theorem BufHypW.mrvC {W : S → A → Fl4Spec.State Unit → MdbGhost → Prop} {th : TypedHeap} {t addr : U64} + {ptr : Fl4Spec.VSpaceRef} (hM : MachPlatC arI MR) (h : BufHypW (L := L) W th t addr ptr) : + BufHypW (L := L) (WMR MR W) th t addr ptr := by + refine ⟨fun s a σ G hDS hW hnull => ?_, fun s a σ G hDS hW => ?_⟩ + · refine WP.spec_mono (h.ipc s a σ G hDS hW.1 hnull) ?_ + rintro s' ⟨hSS, σ', hm, hCR, hK, hW'⟩ + exact ⟨hSS, σ', hm, hCR, hK, ⟨hW', mrv_keepC hM hW.2 (MachT.mtk_threadSet' _ _ σ ((), σ') hm)⟩⟩ + · refine WP.spec_mono (h.tail s a σ G hDS hW.1) ?_ + rintro res ⟨h1, h2, hSS, σ', hm, hCR, hK, hW'⟩ + refine ⟨h1, h2, hSS, σ', hm, hCR, hK, ⟨hW', ?_⟩⟩ + rw [h2] + exact mrv_keepC hM hW.2 (dke_bind (dke_liftE (dk_gets _)) + (fun _ => dke_liftE (dk_when _ MachT.mtk_rescheduleRequired')) σ _ hm) + +set_option maxHeartbeats 4000000 in +/-- **`InsWV` at `WMR MR (WVE …)` / `WpVE …`** (module docstring). -/ +theorem insWV_ve [Fl4Proofs.P1.PlainStore st] [VxE Z] (hP : VmDelPrem (st := st) arI AR MR) (kb : U64) : + InsWV (L := L) arI AR MR Z H Cs kb (WMR MR (WVE (L := L) arI AR Z H Cs kb)) + (WpVE (L := L) arI AR MR Z H Cs kb) := by + refine ⟨fun s a σ G h => h.1, fun s a σ G h => ?_, fun s a σ G h => h.2, ?_, fun t ht addr => (bufHypW_VE arI ht kb addr).mrvC arI hP.hM, ?_⟩ + · obtain ⟨⟨V, X, XL, Vv, Ac, Lst, D, J, -⟩, -⟩ := h + exact J.st.anyX arI + · intro s s' a σ σ' G G' c src dst hW hW' hci + exact ⟨hW', mrv_keepC hP.hM hW.2 (MachT.mtk_capInsert c src dst σ ((), σ') (mem_of_single hci))⟩ + · intro K hK hK6 sl hAs s a σ G hDS hW hxn hbound + obtain ⟨⟨V, X, XL, Vv, Ac, Lst, D, J, hkb⟩, hmv⟩ := hW + subst hkb + exact VmEptState.capDelete arI hP.hA hP.hD hP.hK hP.hVA hP.hticks hP.hnd hP.hM hP.hKD hP.hPA hP.hAM J.st J.ns J.sg + J.pp hmv K hK hK6 sl hAs hxn hbound + +/-- The decoder's error outcome is the VM dispatcher post (`TopPostDMX.errP`'s analogue, the decode lifted to `.base`). -/ +theorem VDispPostVEX.errV {on rc : Fl4Spec.ObjRef → Bool} {σ : Fl4Spec.State Unit} {blk call don : Bool} + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation} {s : S} {res : core.result.Result Unit error.Exc × S × A} + (h : ∃ e, res.1 = .Err e ∧ res.2.1 = s ∧ (∃ e', m.run σ = ({(.error e', σ)}, False) ∧ ErrRel e e') ∧ + VEAnyX (L := L) arI AR res.2.1 res.2.2 σ) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base m) s res := by + obtain ⟨e, h1, h2, ⟨e', h3, h3'⟩, h4⟩ := h + exact Or.inl ⟨e, h1, h2, ⟨e', run_liftME_err h3, h3'⟩, h4⟩ + +/-- The VM dispatcher post over a native decode `m` lifted to `.base` (the form `TopPostDMXP` has in the native tops). -/ +abbrev TopPostVT (on rc : Fl4Spec.ObjRef → Bool) (σ : Fl4Spec.State Unit) (blk call don : Bool) + (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) (s : S) (res : core.result.Result Unit error.Exc × S × A) : Prop := + VDispPostVEX (L := L) arI AR MR on rc σ blk call don (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base m) s res + +/-- `tcb::decode` at one label with the VM dispatcher post. -/ +abbrev TcbOKV (on rc : Fl4Spec.ObjRef → Bool) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (lbl : U64) + (p : U64) (c : tcb.Ctx) (blk call don : Bool) (slbl : Fl4Spec.Data) (args : List Fl4Spec.Data) + (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (buffer : Option Fl4Spec.ObjRef) : Prop := + tcb.decode st arI s ar lbl p c ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base (specDec false slbl args ci sl (.Thread p) excaps buffer)) s res ⦄ + +/-- **`tcCapsSpecVF` is `invoke_tcb ThreadControlCaps`** (`invokeTcbVF`) up to its result. -/ +theorem tcCapsSpecVF_mem (t : U64) (ssl : Fl4Spec.CslotPtr) (flags : U64) + (fh tmh cr vr : capability.Cap × Fl4Spec.CslotPtr) (ptr : Fl4Spec.VSpaceRef) + (bfr : Option (capability.Cap × Fl4Spec.CslotPtr)) {σ : Fl4Spec.State Unit} + {x : Except Unit Unit × Fl4Spec.State Unit} + (hx : x ∈ ((tcCapsSpecVF t ssl flags fh tmh cr vr ptr bfr).run σ).1) : + tcRet x ∈ ((Fl4Spec.invokeTcbVF (Z := Unit) (.threadControlCaps t.bv ssl + (optArg ((flags &&& tcb.UPDATE_FAULT) ≠ 0#u64) fh) (optArg ((flags &&& tcb.UPDATE_TIMEOUT) ≠ 0#u64) tmh) + (optArg ((flags &&& tcb.UPDATE_SPACE) ≠ 0#u64) cr) (optArg ((flags &&& tcb.UPDATE_SPACE) ≠ 0#u64) vr) + (if (flags &&& tcb.UPDATE_IPC_BUFFER) ≠ 0#u64 then some (ptr, frameOf bfr) else none))).run σ).1 := by + have hF : ∀ e (s : Fl4Spec.State Unit), tcRet (.error e, s) = (.error e, s) := fun _ _ => rfl + unfold tcCapsSpecVF at hx + unfold Fl4Spec.invokeTcbVF + refine mem_bindE_mono hF (fun _ _ _ h => ?_) hx + refine mem_bindE_mono hF (fun _ _ _ h => ?_) h + refine mem_bindE_mono hF (fun _ _ _ h => ?_) h + refine mem_bindE_mono hF (fun _ _ _ h => ?_) h + refine mem_bindE_mono hF (fun _ _ _ h => ?_) h + have : _ = _ := Set.mem_singleton_iff.1 h + subst this; rfl + +/-- **An install run's outcome at `W` / `Wp` after the restart is a VM dispatcher outcome** of the spec's +ThreadControlCaps invocation (`top_of_instJM` at the record). -/ +theorem top_of_instVE {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) {on rc : Fl4Spec.ObjRef → Bool} + {σ σ1 : Fl4Spec.State Unit} {blk call don : Bool} + {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation} {t : U64} {ssl : Fl4Spec.CslotPtr} {flags : U64} + {fh tmh cr vr : capability.Cap × Fl4Spec.CslotPtr} {ptr : Fl4Spec.VSpaceRef} + {bfr : Option (capability.Cap × Fl4Spec.CslotPtr)} {s s1 : S} + {res : core.result.Result Unit error.Exc × S × A} + (hdec : DecOut m σ (tcInv t ssl flags fh tmh cr vr ptr bfr)) + (hst : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (h : InstPostWP (L := L) W Wp Z.th t (tcCapsSpecVF t ssl flags fh tmh cr vr ptr bfr) s1 σ1 res) : + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base m) s res := by + obtain ⟨σ', G', hc, hDS'⟩ := h + rcases hc with ⟨hr, hx, -, hW'⟩ | ⟨hr, hx, hW'⟩ + · exact Or.inr ⟨.base (tcInv t ssl flags fh tmh cr vr ptr bfr), DecOut.liftME hdec, σ1, hst, + tcRet (.ok (), σ'), tcCapsSpecVF_mem t ssl flags fh tmh cr vr ptr bfr hx, + Or.inl ⟨hr, rfl, hI.any _ _ _ _ hW'⟩⟩ + · exact Or.inr ⟨.base (tcInv t ssl flags fh tmh cr vr ptr bfr), DecOut.liftME hdec, σ1, hst, + tcRet (.error (), σ'), tcCapsSpecVF_mem t ssl flags fh tmh cr vr ptr bfr hx, + Or.inr ⟨hr, rfl, hI.pre _ _ _ _ hW'⟩⟩ + +/-- **The caller's restart at the record's installs** (`JAtZ.restartWM`): `tcb::restart_caller` over `JAtZB`, the loop's +invariant after it by the caller's `hR`. -/ +theorem VmEptState.restartWV {V X : Set U64} {XL : U64 → Nat} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) {W : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hR : ∀ s1 σ1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) → W (L.ksb s) s1 ar σ1 Z.G) : + ∃ s1 σ1, tcb.restart_caller st s = ok s1 ∧ + Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + DState (L := L) Z.th s1 σ1 Z.G ∧ W (L.ksb s1) s1 ar σ1 Z.G ∧ + SlotsSame Z.th (L.mem s) (L.mem s1) := by + obtain ⟨s1, σ1, x, hrc, hst, -, -, -, -, -, -, hs1, Ac1, Lst1, D1, J1, F1⟩ := J.1.1.restartCallerF hni hct + refine ⟨s1, σ1, hrc, hst, ⟨J1.xtop.rel.2.1, J1.del, J1.xtop.rel.2.2.1⟩, ?_, F1.ss⟩ + rw [F1.fw.1] + exact hR s1 σ1 hs1 hst + +set_option maxHeartbeats 8000000 in +/-- **`IpcBufCapOK` after SetIPCBuffer/Configure's frame insert** (decision 2 +of gl11): the new IPC-frame cap of TCB `t` a non-device frame at a nonzero +pointer (`hc`, `hq0`) with the range of the source slot's cap (`hk`, so its +bytes in user frames by `JAtZ.fcap`), aligned (`hcv`), the buffer address +aligned (`hbuf`); the other TCBs' IPC frames kept. -/ +theorem JAtZB.bokInstall {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : Z.th t = some .tcb) + {c : capability.Cap} {src : Fl4Spec.CslotPtr} {σ' : Fl4Spec.State Unit} {s' : S} + (hci : Fl4Spec.capInsert (Z := Unit) (absCap c) src (t.bv, Fl4Spec.tcbCnodeIndex 2) σ = ({((), σ')}, False)) + (hR' : StateRel Z.th (L.mem s') σ') + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2) → capV (L.mem s') y = capV (L.mem s) y) + (hdst : capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2)) = c) + {q : U64} {sz : capability.FrameSize} {rr aa va : U64} {m : Bool} (hc : c = .Frame q sz rr false m false aa va) + (hq0 : q ≠ 0#u64) (hcv : CapValid Z.th c) {y0 : U64} (hy0 : IsSlot Z.th y0) + (hk : okey c = okey (capV (L.mem s) y0)) + (hbuf : ∀ x, σ.kheap t.bv = some (.tcb x) → x.tcbIpcBuffer.toNat % 1024 = 0) : + Endp.IpcBufCapOK σ' := by + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) (absCap c) src (t.bv, Fl4Spec.tcbCnodeIndex 2) σ).1 := by + rw [hci]; rfl + have hkc : KeepCT σ σ' := KeepCT.of_cnK (capInsert_nkn _ _ _ σ _ hmem) + have hpg : ∀ p vms, σ.kheap p = some (.archObj (.dataPage false vms)) → + σ'.kheap p = some (.archObj (.dataPage false vms)) := fun p vms h => by + rcases hkc p with e | ⟨_, _, _, h1, _⟩ | ⟨_, _, h1, _, _⟩ + · rw [e]; exact h + · rw [h] at h1; cases h1 + · rw [h] at h1; cases h1 + intro t' x hx dev pb R mt vms md he hR + -- the TCB before + obtain ⟨x0, hx0, hbx⟩ : ∃ x0, σ.kheap t' = some (.tcb x0) ∧ x.tcbIpcBuffer = x0.tcbIpcBuffer := by + rcases hkc t' with e | ⟨_, _, _, -, h2⟩ | ⟨x1, y, h1, h2, hco⟩ + · rw [e] at hx; exact ⟨x, hx, rfl⟩ + · rw [hx] at h2; cases h2 + · rw [hx] at h2; cases h2; exact ⟨x1, h1, hco.2.1⟩ + obtain ⟨u, rfl, hu⟩ := J.thOfTcb hx0 + obtain ⟨x', c', hkx, hcap', hcr⟩ := hR'.bufCap hu + rw [hx] at hkx; cases hkx + have he2 : cslotAddr (u.bv, Fl4Spec.tcbCnodeIndex 2) = slotOf u 2 := by + show slotAddr u.bv (Fl4Spec.ofBl _) = _ + rw [slotAddr_ofBl, tcbCnodeIndex_blVal 2 (by decide)] + have hAu : AbsSlot Z.th (u.bv, Fl4Spec.tcbCnodeIndex 2) := ⟨u, rfl, Or.inr ⟨hu, 2, by decide, rfl⟩⟩ + have hAt : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex 2) := ⟨t, rfl, Or.inr ⟨ht, 2, by decide, rfl⟩⟩ + by_cases e : u = t + · subst e + have hc'e : c' = c := by + rw [← he2, capAt_capV, hdst] at hcap'; exact (eq_of_ok_eq hcap').symm + rw [he, hc'e, hc] at hcr + obtain ⟨-, h2⟩ := hcr + simp only [absCap] at h2 + injection h2 with h2 + injection h2 with hd hp0 _ _ hsz _ + subst hd + have hpv : q.val = pb.toNat := by rw [← hp0]; rfl + have hbits : (fbitsF sz).val = Fl4Spec.pageBitsForSize vms := by rw [fbitsF_val, hsz] + have hal : pb.toNat % 2 ^ Fl4Spec.pageBitsForSize vms = 0 := by + rw [hc] at hcv + have h1 : q.val % 2 ^ frameBitsN sz = 0 := hcv + have h2 : frameBitsN sz = (fbitsF sz).val := by + rw [fbitsF_val]; cases sz <;> rfl + rw [← hpv, ← hbits, ← h2]; exact h1 + have hba : x.tcbIpcBuffer.toNat % 1024 = 0 := by rw [hbx]; exact hbuf x0 hx0 + have hF : Vs.FrIn (UFr σ) c := Vs.FrIn.of_okey hk (J.fcap y0 hy0) + rw [hc] at hF + refine ⟨rfl, fun h0 => hq0 (P1.eq_of_bv (by rw [← hp0] at h0; exact h0)), fun k hk128 => ?_, hba, hal⟩ + obtain ⟨r1, r2, -⟩ := buf_frame_rng vms pb x.tcbIpcBuffer k hk128 hal hba + have hU := hF rfl (Endp.bufAddr (pb + (x.tcbIpcBuffer &&& Fl4Spec.mask (Fl4Spec.pageBitsForSize vms))) k).toNat + (by rw [hpv]; exact r1) (by rw [hpv, hbits]; omega) + have hU' : Fl4Spec.inUserFrame (Endp.bufAddr (pb + (x.tcbIpcBuffer &&& Fl4Spec.mask + (Fl4Spec.pageBitsForSize vms))) k) σ := by + unfold UFr at hU; rwa [BitVec.ofNat_toNat, BitVec.setWidth_eq] at hU + exact inUserFrame_keep hpg hU' + · have hne : cslotAddr (u.bv, Fl4Spec.tcbCnodeIndex 2) ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2) := fun e' => + e (P1.eq_of_bv (congrArg Prod.fst (cslotAddr_inj J.xtop.rel.1 hAu hAt e'))) + obtain ⟨x2, c2, hkx2, hcap2, hcr2⟩ := J.xtop.rel.2.1.1.bufCap hu + rw [hx0] at hkx2; cases hkx2 + have hcc : c' = c2 := by + rw [← he2, capAt_capV, hcap _ hAu.isSlot hne] at hcap' + rw [← he2, capAt_capV] at hcap2 + exact eq_of_ok_eq (hcap'.symm.trans hcap2) + have hfr : x0.tcbIpcframe = x.tcbIpcframe := by rw [← hcr2.2, ← hcr.2, hcc] + obtain ⟨h1, h2, h3, h4, h5⟩ := J.bok u.bv x0 hx0 dev pb R mt vms md (by rw [hfr]; exact he) hR + rw [hbx] + exact ⟨h1, h2, fun k hk => inUserFrame_keep hpg (h3 k hk), h4, h5⟩ + +set_option maxHeartbeats 4000000 in +/-- (mmuAD Stage 3, route D) **`install_tcb_capJM` at the VM record** (`install_tcb_capJM` is the `WJPM` instance). -/ +theorem install_tcb_capVE [Fl4Proofs.P1.PlainStore st] {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) + (s0 s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hDS : DState (L := L) Z.th s σ G) + (hW : W s a σ G) + (t : U64) (ht : Z.th t = some .tcb) (n : Nat) (hn : n < tcbSlots) (hn2 : n ≠ 2) (idx : U64) (hidx : idx.val = n) + (hcz0 : InstFr Z.th t (L.mem s0) (L.mem s)) + (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (nc : capability.Cap) (hwf : CapWf nc) (hcv : CapValid Z.th nc) (he : EncWf nc) + (hfw : isFrame nc = true → Fl4Spec.sameObjectAs (absCap nc) (absCap nc)) + {ps : U64} {rs : Nat} (hps : Z.th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + {srcC0 : capability.Cap} (hsrc0 : capV (L.mem s0) (cslotAddr (ps.bv, idxs)) = srcC0) + (hsh : shape nc = shape srcC0) (hbd : badgeF nc = badgeF srcC0 ∨ badgeF srcC0 = 0#u64) + (hkey : okey nc = okey srcC0) + (hv : ∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 srcC0 → Vs.VCapOK P L4 L3 L2 L1 nc) + (htu : Vs.TabUn nc → Vs.TabUn srcC0) (hnat : Vs.NativeCap srcC0 → Vs.NativeCap nc) + (hkr0 : Vs.VKey nc ≠ none → nc = srcC0 ∧ ¬ Vs.TabUn nc) + (hep : n = 3 → nc ≠ .Null → ∃ e b x1 x2 x3 x4, nc = .Endpoint e b x1 x2 x3 x4) (hn5 : n ≠ 5) + (hvt0 : VtOK srcC0 nc) : + tcb.install_tcb_cap st arI s a t (cslotAddr ssl) idx nc (cslotAddr (ps.bv, idxs)) ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (Fl4Spec.installTcbCapVF t.bv ssl n (some (absCap nc, (ps.bv, idxs)))) s0 σ res ⦄ := by + obtain ⟨V0, X0, XL0, Vv0, Ac0, Lst0, D0, J0V, -⟩ := hI.wvr _ _ _ _ hW + have J0 := J0V.st.1.1 + have hP : Placed Z.th := J0.xtop.rel.1 + obtain ⟨K, hK, hK6, hDE⟩ := J0.delE + have hAt : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex n) := ⟨t, rfl, Or.inr ⟨ht, n, hn, rfl⟩⟩ + have hAsrc : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hsrcT := cnode_slot_ne_tcb hP hps hls ht + have hczs0 := hcz0.1 _ hAsrc.isSlot hsrcT + have hsh0 : shape nc = shape (capV (L.mem s0) (cslotAddr (ps.bv, idxs))) := by rw [hsrc0]; exact hsh + -- the insert after the delete, at `WJR` + have after : ∀ (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) (G1 : MdbGhost), DState (L := L) Z.th s1 σ1 G1 → + W s1 a1 σ1 G1 → + capV (L.mem s1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Null → + ChgZ (L.mem s0) (L.mem s1) (cslotAddr (ps.bv, idxs)) → + (do + let b ← nc.is_null + if b = true then ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s1, a1) + else do + let b1 ← tcb.still_valid st s1 t (cslotAddr ssl) nc (cslotAddr (ps.bv, idxs)) + if b1 = true then do + let s2 ← cdt.insert st s1 nc (cslotAddr (ps.bv, idxs)) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) + ok (core.result.Result.Ok (), s2, a1) + else ok (core.result.Result.Ok (), s1, a1)) ⦃ res => + ∃ σ' G', (Fl4Spec.NonDetE.unlessE (ε := Unit) (absCap nc = .nullCap) (Fl4Spec.NonDetE.liftE + (Fl4Spec.checkCapAt (absCap nc) (ps.bv, idxs) (Fl4Spec.checkCapAt (.threadCap t.bv) ssl + (Fl4Spec.capInsert (absCap nc) (ps.bv, idxs) (t.bv, Fl4Spec.tcbCnodeIndex n)))))).run σ1 = + ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ DState (L := L) Z.th res.2.1 σ' G' ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) → + capV (L.mem res.2.1) y = capV (L.mem s1) y) ∧ + (capV (L.mem res.2.1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Null ∨ + capV (L.mem res.2.1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = nc) ∧ + W res.2.1 res.2.2 σ' G' ⦄ := by + intro s1 a1 σ1 G1 hDS1 hW1 hroot hczs + refine WP.spec_mono (WP.spec_and (install_after Z.th hP s0 s1 a1 σ1 G1 hDS1 t ht n hn hroot ssl hAs nc hwf hcv he + hfw hps hls hczs hsrc0 hsh hbd hkey hv) + (install_after_frame hP s1 a1 hDS1.1.2.2.1 hDS1.2.1 t (cslotAddr ssl) nc hAsrc.isSlot hAt.isSlot)) ?_ + rintro res ⟨⟨σ', G', hrun, hr1, ha1, hDS', hcap, hst, htg, hcase⟩, hF⟩ + refine ⟨σ', G', hrun, hr1, hDS', hcap, htg, ?_⟩ + rcases hcase with ⟨e1, e2, e3⟩ | ⟨hins, hthr, hsrc1, hnn, hnut, hdst⟩ + · rw [ha1, e1, e2, e3]; exact hW1 + obtain ⟨V1, X1, XL1, Vv1, Ac1, Lst1, D1, J1V, hkb1⟩ := hI.wvr _ _ _ _ hW1 + have hns1 := J1V.ns + have hsg1 := J1V.sg + have hp1 := J1V.pp + have hsg' : SpecGlob σ' := by + obtain ⟨c0, a0, hc0, hca0, hg0⟩ := hDS1.1.1.abs_cap hAsrc + have e0 : c0 = srcC0 := by rw [← hsrc1, capV_of_capAt hc0] + have ha0 := (getCap_set_iff σ1 _ _ _).1 (congrArg Prod.fst hg0) + rw [hca0.abs, e0] at ha0 + exact hsg1.insTcb hins hnut hsh ha0 + have hwS := SlotFrame2.writesIn hF + obtain ⟨Ac2, Lst2, D2, J2⟩ := J1V.st.insTcbV (fun e => absurd e hn2) ht hn hn5 hins hDS' hst hwS hroot hcap + (by rw [hdst]; exact hnut) (by rw [hdst]; exact hnn) ⟨_, hAsrc.isSlot, by rw [hdst, hsrc1]; exact hsh⟩ + (fun h3 => by rw [hdst]; exact Or.inr (hep h3 hnn)) (fun e => absurd e hn2) ⟨_, hAs.isSlot, hthr⟩ + (fun P L4 L3 L2 L1 h => by + rw [hdst]; exact h.copy hAsrc.isSlot (by rw [hsrc1]; exact hv) (by rw [hsrc1]; exact htu)) + (fun U h => by rw [hdst]; exact (h _ hAsrc.isSlot).of_okey (by rw [hsrc1]; exact hkey)) + (fun h => by rw [hdst]; exact hnat (by rw [← hsrc1]; exact h _ hAsrc.isSlot)) + (fun R h => by rw [hdst]; exact (h _ hAsrc.isSlot).of_okey (by rw [hsrc1]; exact hkey)) + (fun hk => by + rw [hdst] at hk ⊢ + obtain ⟨e, hnt⟩ := hkr0 hk + exact ⟨⟨_, hAsrc.isSlot, hsrcT n hn, by rw [e, hsrc1]⟩, hnt⟩) hAsrc.isSlot (by rw [hdst, hsrc1]; exact hvt0) + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) (absCap nc) (ps.bv, idxs) + (t.bv, Fl4Spec.tcbCnodeIndex n) σ1).1 := by rw [hins]; rfl + have hcn := capInsert_nkn _ _ _ σ1 _ hmem + have hact : σ'.schedulerAction = σ1.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hcn.1; exact e + rw [ha1] + exact hI.ins s1 res.2.1 a1 σ1 σ' G1 G' _ _ _ hW1 + ⟨V1, X1, XL1, Vv1, Ac2, Lst2, D2, ⟨J2, fun u e => hns1 u (hact ▸ e), hsg', hp1.of_ppk (PPK.of_cnK hcn)⟩, + by rw [hwS.1]; exact hkb1⟩ hins + unfold tcb.install_tcb_cap + rw [spec_eq_ok (tcb_slot_spec t idx), tcb_root_eq t idx hn hidx] + simp only [bind_ok] + obtain ⟨c0, a0, hc0, hca0, hg0⟩ := hDS.1.1.abs_cap hAt + by_cases hc0n : c0 = .Null + · subst hc0n + rw [delete_null_eq arI s a _ hc0 σ] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, uncurry_apply_pair] + refine WP.spec_mono (after s a σ G hDS hW (by rw [capV_of_capAt hc0]) hczs0) ?_ + rintro res ⟨σ', G', hrun, hr1, hDS', hcap, htg, hW'⟩ + have hg0' : Fl4Spec.getCap (Z := Unit) (t.bv, Fl4Spec.tcbCnodeIndex n) σ = ({(.nullCap, σ)}, False) := by + rw [hg0, hca0.abs]; rfl + exact ⟨σ', G', Or.inl ⟨hr1, mem_bindE_ok (capDeleteVF_null_mem hg0') (mem_of_run hrun), + hcz0.install hP ht hn (fun y hy hne => ChgZ.of_eq (hcap y hy hne)) htg hAsrc.isSlot hsh0 hep, hW'⟩, hDS'⟩ + · apply WP.spec_bind (hI.del K hK hK6 _ hAt s a σ G hDS hW + (by rw [capV_of_capAt hc0]; exact hc0n) (hDE.2.1 G hDS.1.2.2.1)) + rintro ⟨r, s1, a1⟩ ⟨σ1, G1, hDS1, -, -, hmD, hcase, hW1⟩ + simp only [uncurry_apply_pair] at hcase hDS1 hmD hW1 ⊢ + rcases hcase with ⟨rfl, hmem, -, hnull, hcz⟩ | ⟨rfl, hmem⟩ + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + refine WP.spec_mono (after s1 a1 σ1 G1 hDS1 hW1 hnull (hczs0.trans (hcz _ hAsrc.isSlot (hsrcT n hn)))) ?_ + rintro res ⟨σ', G', hrun, hr1, hDS', hcap, htg, hW'⟩ + exact ⟨σ', G', Or.inl ⟨hr1, mem_bindE_ok hmem (mem_of_run hrun), + hcz0.install hP ht hn (fun y hy hne => (hcz y hy hne).trans (ChgZ.of_eq (hcap y hy hne))) htg + hAsrc.isSlot hsh0 hep, hW'⟩, hDS'⟩ + · 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, Or.inr ⟨rfl, mem_bindE_err hmem, hW1⟩, hDS1⟩ + +/-- (mmuAD Stage 3, route D) **`install_okJM` at the VM record** (`install_okJM` is the `WJPM` instance). -/ +theorem install_okVE [Fl4Proofs.P1.PlainStore st] {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) + (s0 s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hDS : DState (L := L) Z.th s σ G) + (hW : W s a σ G) + (t : U64) (ht : Z.th t = some .tcb) (n : Nat) (hn : n < tcbSlots) (hn2 : n ≠ 2) (idx : U64) (hidx : idx.val = n) + (hcz0 : InstFr Z.th t (L.mem s0) (L.mem s)) + (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (nc : capability.Cap) (sl : Fl4Spec.CslotPtr) (hok : NewCapOK Z.th (L.mem s0) nc sl) + (hep : n = 3 → nc ≠ .Null → ∃ e b x1 x2 x3 x4, nc = .Endpoint e b x1 x2 x3 x4) (hn5 : n ≠ 5) + (hvt : VtOK (capV (L.mem s0) (cslotAddr sl)) nc) : + tcb.install_tcb_cap st arI s a t (cslotAddr ssl) idx nc (cslotAddr sl) ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (Fl4Spec.installTcbCapVF t.bv ssl n (some (absCap nc, sl))) s0 σ res ⦄ := by + obtain ⟨ps, rs, hps, h1s, hls, hwf, hcv, he, hfw, hsh, hbd, hkey, hvk, htu, hnat, hkr0⟩ := hok + obtain ⟨sa, idxs⟩ := sl + simp only at h1s hls; subst h1s + exact install_tcb_capVE arI hI s0 s a σ G hDS hW t ht n hn hn2 idx hidx hcz0 ssl hAs nc hwf hcv he hfw hps hls rfl + hsh hbd hkey hvk htu hnat hkr0 hep hn5 hvt + +set_option maxHeartbeats 4000000 in +/-- (mmuAD Stage 3, route D) **`install_bufferJM` at the VM record** (`install_bufferJM` is the `WJPM` instance). -/ +theorem install_bufferVE [Fl4Proofs.P1.PlainStore st] {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) + (s0 s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hDS : DState (L := L) Z.th s σ G) + (hW : W s a σ G) (t : U64) (ht : Z.th t = some .tcb) + (hcz0 : InstFr Z.th t (L.mem s0) (L.mem s)) + (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (addr : U64) (ptr : Fl4Spec.VSpaceRef) + (hBuf : BufHypW (L := L) W Z.th t addr ptr) + (bc0 : capability.Cap) (bfr : Option (capability.Cap × Fl4Spec.CslotPtr)) + (hbfr : ∀ c sl, bfr = some (c, sl) → NewCapOK Z.th (L.mem s0) c sl) + (hbfd : ∀ c sl, bfr = some (c, sl) → BufFrOK ptr c) (hfnz : ∀ c sl, bfr = some (c, sl) → FrNZ Z.th (L.mem s0)) + (hbvt : ∀ c sl, bfr = some (c, sl) → VtOK (capV (L.mem s0) (cslotAddr sl)) c) : + tcb.install_buffer st arI s a t (cslotAddr ssl) addr (bufArg bc0 bfr) ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (Fl4Spec.installTcbFrameCapVF t.bv ssl (some (ptr, frameOf bfr))) s0 σ res ⦄ := by + obtain ⟨V0, X0, XL0, Vv0, Ac0, Lst0, D0, J0V, -⟩ := hI.wvr _ _ _ _ hW + have J0 := J0V.st.1.1 + have hP : Placed Z.th := J0.xtop.rel.1 + obtain ⟨K, hK, hK6, hDE⟩ := J0.delE + have hAt : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex 2) := ⟨t, rfl, Or.inr ⟨ht, 2, by decide, rfl⟩⟩ + obtain ⟨c, s3, hbuf⟩ : ∃ c s3, bufArg bc0 bfr = (c, s3) := ⟨_, _, rfl⟩ + have cont : ∀ (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) (G1 : MdbGhost), DState (L := L) Z.th s1 σ1 G1 → + W s1 a1 σ1 G1 → + capV (L.mem s1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2)) = .Null → + InstFr Z.th t (L.mem s0) (L.mem s1) → + (.ok (), σ1) ∈ ((Fl4Spec.capDeleteVF (Z := Unit) (t.bv, Fl4Spec.tcbCnodeIndex 2)).run σ).1 → + (do + let s2 ← obj.Tcb.set_ipc_buffer st t s1 addr + let b ← tcb.still_valid st s2 t (cslotAddr ssl) c s3 + let s4 ← if b = true then cdt.insert st s2 c s3 (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2)) else ok s2 + let t_1 ← sched.cur_thread st s4 + let b1 ← obj.Tcb.Insts.CoreCmpPartialEqTcb.eq t t_1 + if b1 = true then do + let s5 ← sched.reschedule_required st s4 + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a1) + else ok (core.result.Result.Ok (), s4, a1)) ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (Fl4Spec.installTcbFrameCapVF t.bv ssl (some (ptr, frameOf bfr))) s0 σ res ⦄ := by + intro s1 a1 σ1 G1 hDS1 hW1 hnull hcz hmem + apply WP.spec_bind (hBuf.ipc s1 a1 σ1 G1 hDS1 hW1 hnull) + rintro s2 ⟨hS2, σ2, hts, hCR2, hK2, hW2⟩ + have hDS2 := hDS1.of_same hS2 hCR2 hK2 + have hnull2 : capV (L.mem s2) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2)) = .Null := by + rw [hS2.capV hAt.isSlot, hnull] + cases bfr with + | none => + simp only [bufArg, Prod.mk.injEq] at hbuf + obtain ⟨rfl, rfl⟩ := hbuf + simp only [still_valid_zero, bind_ok, Bool.false_eq_true, ↓reduceIte] + apply WP.spec_mono (hBuf.tail s2 a1 σ2 G1 hDS2 hW2) + rintro res ⟨hr1, -, hS5, σ5, htail, hCR5, hK6', hW5⟩ + refine ⟨σ5, G1, Or.inl ⟨hr1, ?_, (hcz.same hS2 ht).same hS5 ht, hW5⟩, hDS2.of_same hS5 hCR5 hK6'⟩ + refine mem_bindE_ok hmem (mem_bindE_ok (mem_liftE_ok hts) (mem_bindE_ok (s' := σ2) (a := ()) ?_ htail)) + exact mem_liftE_ok (x := ((), σ2)) rfl + | some p => + obtain ⟨c', sl⟩ := p + simp only [bufArg, Prod.mk.injEq] at hbuf + obtain ⟨rfl, rfl⟩ := hbuf + have hok := hbfr c' sl rfl + obtain ⟨ps, rs, hps, h1s, hls, -, -, -, -, hshc, -⟩ := hok + have hsrcIs : IsSlot Z.th (cslotAddr sl) := by + obtain ⟨sa, idxs⟩ := sl; simp only at h1s hls; subst h1s + exact (show AbsSlot Z.th (ps.bv, idxs) from ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩).isSlot + have hsrcT : NotTcbSlot t (cslotAddr sl) := by + obtain ⟨sa, idxs⟩ := sl; simp only at h1s hls; subst h1s + exact cnode_slot_ne_tcb hP hps hls ht + have hczs : ChgZ (L.mem s0) (L.mem s2) (cslotAddr sl) := + (hcz.1 _ hsrcIs hsrcT).trans (ChgZ.of_eq (hS2.capV hsrcIs)) + rw [← Std.bind_assoc] + apply WP.spec_bind (WP.spec_and (check_insert Z.th hP s0 s2 σ2 G1 hDS2 t ht 2 (by decide) hnull2 ssl hAs c' sl + (hbfr c' sl rfl) hczs) (check_insert_frame hP s2 hDS2.1.2.2.1 hDS2.2.1 t (cslotAddr ssl) c' hsrcIs hAt.isSlot)) + rintro s4 ⟨⟨σ4, G4, hchk, hDS4, hcap4, hst4, htg4, hcase⟩, hF4⟩ + have hW4 : W s4 a1 σ4 G4 := by + rcases hcase with ⟨e1, e2, e3⟩ | ⟨hins, hthr, hsrc1, hnn, hnut, hdst⟩ + · rw [e1, e2, e3]; exact hW2 + obtain ⟨V2, X2, XL2, Vv2, Ac2, Lst2, D2, J2V, hkb2⟩ := hI.wvr _ _ _ _ hW2 + have J2 := J2V.st.1.1 + have hns2 := J2V.ns + have hsg2 := J2V.sg + have hp2 := J2V.pp + have hsg4 : SpecGlob σ4 := by + obtain ⟨c0, a0, hc0, hca0, hg0⟩ := hDS2.1.1.abs_cap + (show AbsSlot Z.th sl by + obtain ⟨sa, idxs⟩ := sl; simp only at h1s hls; subst h1s + exact ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩) + have e0 : c0 = capV (L.mem s0) (cslotAddr sl) := by rw [← hsrc1, capV_of_capAt hc0] + have ha0 := (getCap_set_iff σ2 _ _ _).1 (congrArg Prod.fst hg0) + rw [hca0.abs, e0] at ha0 + exact hsg2.insTcb hins hnut hshc ha0 + have hwS := SlotFrame2.writesIn hF4 + obtain ⟨⟨q, sz, rr, m, aa, va, hcF⟩, hbal⟩ := hbfd c' sl rfl + obtain ⟨-, -, -, -, -, -, hcvc, -, -, -, -, hk', -, -⟩ := hbfr c' sl rfl + have hk2 : okey c' = okey (capV (L.mem s2) (cslotAddr sl)) := by rw [hsrc1]; exact hk' + have hq0 : q ≠ 0#u64 := by + obtain ⟨r0, m0, e0, a0, v0, hs0⟩ : ∃ r0 m0 e0 a0 v0, + capV (L.mem s0) (cslotAddr sl) = .Frame q sz r0 false m0 e0 a0 v0 := by + rw [hcF] at hk' + generalize capV (L.mem s0) (cslotAddr sl) = c0 at hk' ⊢ + cases c0 <;> simp [okey] at hk' + obtain ⟨rfl, rfl, rfl⟩ := hk' + exact ⟨_, _, _, _, _, rfl⟩ + exact hfnz c' sl rfl _ hsrcIs q sz r0 m0 e0 a0 v0 hs0 + have hbuf2 : ∀ x, σ2.kheap t.bv = some (.tcb x) → x.tcbIpcBuffer.toNat % 1024 = 0 := by + intro x hx + obtain ⟨x0, -, he⟩ := PPS.threadSet_mem hts + have he' : σ2 = _ := he + rw [he'] at hx + simp only [Function.update_self, Option.some.injEq, Fl4Spec.KernelObject.tcb.injEq] at hx + subst hx; exact hbal + have hbok4 := J2.bokInstall arI ht hins hDS4.1.1 hcap4 hdst hcF hq0 hcvc hsrcIs hk2 hbuf2 + obtain ⟨Ac4, Lst4, D4, J4⟩ := J2V.st.insTcbV (fun _ => hbok4) ht (n := 2) (by decide) (by decide) hins hDS4 hst4 hwS hnull2 hcap4 + (by rw [hdst]; exact hnut) (by rw [hdst, hcF]; exact fun h => by cases h) ⟨_, hsrcIs, by rw [hdst, hsrc1]; exact hshc⟩ + (fun h => absurd h (by decide)) (fun _ => by rw [hdst, hcF]; exact Or.inr ⟨q, sz, rr, m, false, aa, va, rfl⟩) + ⟨_, hAs.isSlot, hthr⟩ + (fun P L4 L3 L2 L1 h => by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, hvk', htu', -⟩ := hbfr c' sl rfl + rw [hdst]; exact h.copy hsrcIs (by rw [hsrc1]; exact hvk') (by rw [hsrc1]; exact htu')) + (fun U h => by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, hk', -, -⟩ := hbfr c' sl rfl + rw [hdst]; exact (h _ hsrcIs).of_okey (by rw [hsrc1]; exact hk')) + (fun h => by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, -, -, hnt', -⟩ := hbfr c' sl rfl + rw [hdst]; exact hnt' (by rw [← hsrc1]; exact h _ hsrcIs)) + (fun R h => by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, hk', -, -⟩ := hbfr c' sl rfl + rw [hdst]; exact (h _ hsrcIs).of_okey (by rw [hsrc1]; exact hk')) + (fun hk => by rw [hdst, hcF] at hk; exact absurd rfl hk) hsrcIs (by rw [hdst, hsrc1]; exact hbvt c' sl rfl) + have hmem' : ((), σ4) ∈ (Fl4Spec.capInsert (Z := Unit) (absCap c') sl + (t.bv, Fl4Spec.tcbCnodeIndex 2) σ2).1 := by rw [hins]; rfl + have hcn := capInsert_nkn _ _ _ σ2 _ hmem' + have hact : σ4.schedulerAction = σ2.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hcn.1; exact e + exact hI.ins s2 s4 a1 σ2 σ4 G1 G4 _ _ _ hW2 + ⟨V2, X2, XL2, Vv2, Ac4, Lst4, D4, ⟨J4, fun u e => hns2 u (hact ▸ e), hsg4, hp2.of_ppk (PPK.of_cnK hcn)⟩, + by rw [hwS.1]; exact hkb2⟩ hins + apply WP.spec_mono (hBuf.tail s4 a1 σ4 G4 hDS4 hW4) + rintro res ⟨hr1, -, hS5, σ5, htail, hCR5, hK6', hW5⟩ + have hAt2 : IsSlot Z.th (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2)) := hAt.isSlot + refine ⟨σ5, G4, Or.inl ⟨hr1, ?_, (hcz.same hS2 ht).install hP ht (n := 2) (by decide) + (fun y hy hne => (ChgZ.of_eq (hcap4 y hy hne)).trans (ChgZ.of_eq (hS5.capV hy))) + (by rw [hS5.capV hAt2]; exact htg4) hsrcIs hshc (fun h => absurd h (by decide)), hW5⟩, + hDS4.of_same hS5 hCR5 hK6'⟩ + refine mem_bindE_ok hmem (mem_bindE_ok (mem_liftE_ok hts) (mem_bindE_ok (s' := σ4) (a := ()) ?_ htail)) + exact mem_liftE_ok (x := ((), σ4)) (by simp only [frameOf]; rw [hchk]; rfl) + rw [hbuf] + unfold tcb.install_buffer + rw [tcb_slot_buffer] + simp only [bind_ok] + obtain ⟨c0, a0, hc0, hca0, hg0⟩ := hDS.1.1.abs_cap hAt + by_cases hc0n : c0 = .Null + · subst hc0n + rw [delete_null_eq arI s a _ hc0 σ] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, uncurry_apply_pair] + have hg0' : Fl4Spec.getCap (Z := Unit) (t.bv, Fl4Spec.tcbCnodeIndex 2) σ = ({(.nullCap, σ)}, False) := by + rw [hg0, hca0.abs]; rfl + exact cont s a σ G hDS hW (by rw [capV_of_capAt hc0]) hcz0 (capDeleteVF_null_mem hg0') + · apply WP.spec_bind (hI.del K hK hK6 _ hAt s a σ G hDS hW + (by rw [capV_of_capAt hc0]; exact hc0n) (hDE.2.1 G hDS.1.2.2.1)) + rintro ⟨r, s1, a1⟩ ⟨σ1, G1, hDS1, -, -, hmD, hcase, hW1⟩ + simp only [uncurry_apply_pair] at hcase hDS1 hmD hW1 ⊢ + rcases hcase with ⟨rfl, hmem, -, hnull, hcz⟩ | ⟨rfl, hmem⟩ + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + exact cont s1 a1 σ1 G1 hDS1 hW1 hnull (hcz0.delete hP ht (n := 2) (by decide) hcz hnull) hmem + · 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, Or.inr ⟨rfl, mem_bindE_err hmem, hW1⟩, hDS1⟩ + +set_option maxHeartbeats 4000000 in +/-- (mmuAD Stage 3, route D) **`thread_control_capsJM` at the VM record** (`thread_control_capsJM` is the `WJPM` instance). -/ +theorem thread_control_capsVE [Fl4Proofs.P1.PlainStore st] {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hDS : DState (L := L) Z.th s σ G) + (hW : W s a σ G) + (t : U64) (ht : Z.th t = some .tcb) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) (flags : U64) + (fh tmh cr vr : capability.Cap × Fl4Spec.CslotPtr) + (hfh : (flags &&& tcb.UPDATE_FAULT) ≠ 0#u64 → NewCapOK Z.th (L.mem s) fh.1 fh.2) + (htmh : (flags &&& tcb.UPDATE_TIMEOUT) ≠ 0#u64 → NewCapOK Z.th (L.mem s) tmh.1 tmh.2) + (hcr : (flags &&& tcb.UPDATE_SPACE) ≠ 0#u64 → NewCapOK Z.th (L.mem s) cr.1 cr.2) + (hvr : (flags &&& tcb.UPDATE_SPACE) ≠ 0#u64 → NewCapOK Z.th (L.mem s) vr.1 vr.2) + (hfhE : (flags &&& tcb.UPDATE_FAULT) ≠ 0#u64 → fh.1 ≠ .Null → + ∃ e b x1 x2 x3 x4, fh.1 = .Endpoint e b x1 x2 x3 x4) + (hfv : (flags &&& tcb.UPDATE_FAULT) ≠ 0#u64 → VtOK (capV (L.mem s) (cslotAddr fh.2)) fh.1) + (htv : (flags &&& tcb.UPDATE_TIMEOUT) ≠ 0#u64 → VtOK (capV (L.mem s) (cslotAddr tmh.2)) tmh.1) + (hcv : (flags &&& tcb.UPDATE_SPACE) ≠ 0#u64 → VtOK (capV (L.mem s) (cslotAddr cr.2)) cr.1) + (hvv : (flags &&& tcb.UPDATE_SPACE) ≠ 0#u64 → VtOK (capV (L.mem s) (cslotAddr vr.2)) vr.1) + (addr : U64) (ptr : Fl4Spec.VSpaceRef) + (hBuf : (flags &&& tcb.UPDATE_IPC_BUFFER) ≠ 0#u64 → BufHypW (L := L) W Z.th t addr ptr) + (bc0 : capability.Cap) (bfr : Option (capability.Cap × Fl4Spec.CslotPtr)) + (hbfr : ∀ c sl, bfr = some (c, sl) → NewCapOK Z.th (L.mem s) c sl) + (hbfd : ∀ c sl, bfr = some (c, sl) → BufFrOK ptr c) (hfnz : ∀ c sl, bfr = some (c, sl) → FrNZ Z.th (L.mem s)) + (hbvt : ∀ c sl, bfr = some (c, sl) → VtOK (capV (L.mem s) (cslotAddr sl)) c) : + tcb.thread_control_caps st arI s a t (cslotAddr ssl) (fh.1, cslotAddr fh.2) (tmh.1, cslotAddr tmh.2) + (cr.1, cslotAddr cr.2) (vr.1, cslotAddr vr.2) addr (bufArg bc0 bfr) flags ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (tcCapsSpecVF t ssl flags fh tmh cr vr ptr bfr) s σ res ⦄ := by + have r0 : InstFr Z.th t (L.mem s) (L.mem s) := InstFr.refl _ _ _ + have i3 : (layout.tcb.SLOT_FAULT_HANDLER).val = 3 := by simp [layout.tcb.SLOT_FAULT_HANDLER] + have i4 : (layout.tcb.SLOT_TIMEOUT_HANDLER).val = 4 := by simp [layout.tcb.SLOT_TIMEOUT_HANDLER] + have i0 : (layout.tcb.SLOT_CTABLE).val = 0 := by simp [layout.tcb.SLOT_CTABLE] + have i1 : (layout.tcb.SLOT_VTABLE).val = 1 := by simp [layout.tcb.SLOT_VTABLE] + unfold tcb.thread_control_caps tcCapsSpecVF + by_cases hF : (flags &&& tcb.UPDATE_FAULT) = 0#u64 <;> + by_cases hT : (flags &&& tcb.UPDATE_TIMEOUT) = 0#u64 <;> + by_cases hS : (flags &&& tcb.UPDATE_SPACE) = 0#u64 <;> + by_cases hU : (flags &&& tcb.UPDATE_IPC_BUFFER) = 0#u64 <;> + simp only [lift, bind_ok, hF, hT, hS, hU, bne_iff_ne, ne_eq, not_false_eq_true, not_true_eq_false, ↓reduceIte, + optArg, installTcbCapVF_none, installTcbFrameCapVF_none] <;> + -- the fault handler + (first + | with_reducible refine inst_skipWP ?_ + | refine inst_seqWP (install_okVE arI hI s s a σ G hDS hW t ht 3 (by decide) (by decide) _ i3 r0 ssl hAs + _ _ (hfh hF) (fun _ => hfhE hF) (by decide) (hfv hF)) (fun s1 a1 σ1 G1 hDS1 _ hcz1 hmo1 => ?_)) <;> + -- the timeout handler + (first + | with_reducible refine inst_skipWP ?_ + | refine inst_seqWP (install_okVE arI hI s _ _ _ _ ‹DState _ _ _ _› (by first | assumption | exact hW) t ht 4 + (by decide) (by decide) _ i4 (by first | assumption | exact r0) ssl hAs _ _ (htmh hT) (fun h => absurd h (by decide)) (by decide) (htv hT)) + (fun s1 a1 σ1 G1 hDS1 _ hcz1 hmo1 => ?_)) <;> + -- the CSpace root + (first + | with_reducible refine inst_skipWP ?_ + | refine inst_seqWP (install_okVE arI hI s _ _ _ _ ‹DState _ _ _ _› (by first | assumption | exact hW) t ht 0 + (by decide) (by decide) _ i0 (by first | assumption | exact r0) ssl hAs _ _ (hcr hS) (fun h => absurd h (by decide)) (by decide) (hcv hS)) + (fun s1 a1 σ1 G1 hDS1 _ hcz1 hmo1 => ?_)) <;> + -- the VSpace root + (first + | with_reducible refine inst_skipWP ?_ + | refine inst_seqWP (install_okVE arI hI s _ _ _ _ ‹DState _ _ _ _› (by first | assumption | exact hW) t ht 1 + (by decide) (by decide) _ i1 (by first | assumption | exact r0) ssl hAs _ _ (hvr hS) (fun h => absurd h (by decide)) (by decide) (hvv hS)) + (fun s1 a1 σ1 G1 hDS1 _ hcz1 hmo1 => ?_)) <;> + -- the IPC buffer + (first + | with_reducible refine inst_skipWP ?_ + | refine inst_seqWP (install_bufferVE arI hI s _ _ _ _ ‹DState _ _ _ _› (by first | assumption | exact hW) t ht + (by first | assumption | exact r0) ssl hAs addr ptr (hBuf hU) bc0 bfr hbfr hbfd hfnz hbvt) + (fun s1 a1 σ1 G1 hDS1 _ hcz1 hmo1 => ?_)) <;> + exact inst_doneWP ‹DState _ _ _ _› (by first | assumption | exact hW) (by first | assumption | exact r0) + +/-- **FL4's `derive_roots` is the spec's `decode_cv_space`** (two arguments, +two extra caps): the derived CSpace and VSpace roots, each a new cap of an +install from its slot (`NewCapOK`), or the same error. -/ +theorem roots_rel_vt (th : TypedHeap) (hP : Placed th) (s : S) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hDS : DState (L := L) th s σ G) (p : U64) (hp : th p = some .tcb) (cdata vdata : U64) + (ec ev : Fl4Spec.Cap × Fl4Spec.CslotPtr) + (hec : ec.1 = absCap (capV (L.mem s) (cslotAddr ec.2))) + (hecs : ∃ ps, th ps = some (.cnode ec.2.2.length) ∧ ec.2.1 = ps.bv) + (hev : ev.1 = absCap (capV (L.mem s) (cslotAddr ev.2))) + (hevs : ∃ ps, th ps = some (.cnode ev.2.2.length) ∧ ev.2.1 = ps.bv) + (hncc : isUt (capV (L.mem s) (cslotAddr ec.2)) = true → NoChildrenRel (st := st) s σ (cslotAddr ec.2) ec.2) + (hncv : isUt (capV (L.mem s) (cslotAddr ev.2)) = true → NoChildrenRel (st := st) s σ (cslotAddr ev.2) ev.2) + (slot : Fl4Spec.CslotPtr) : + ∃ r, tcb.derive_roots st s p cdata vdata (cslotAddr ec.2) (cslotAddr ev.2) = ok r ∧ + ((∃ cc vc, r = .Ok (cc, vc) ∧ + (Fl4Spec.decodeCvSpace (Z := Unit) [cdata.bv, vdata.bv] (.threadCap p.bv) slot [ec, ev]).run σ = + ({(.ok (.threadControlCaps p.bv slot none none (some (absCap cc, ec.2)) (some (absCap vc, ev.2)) + none), σ)}, False) ∧ + NewCapOK th (L.mem s) cc ec.2 ∧ NewCapOK th (L.mem s) vc ev.2 ∧ + vkeyF cc = none ∧ vkeyF vc = none) ∨ + (∃ e e', r = .Err e ∧ + (Fl4Spec.decodeCvSpace (Z := Unit) [cdata.bv, vdata.bv] (.threadCap p.bv) slot [ec, ev]).run σ = + ({(.error e', σ)}, False) ∧ ErrRel e e')) := by + obtain ⟨b0, hb0, hs0⟩ := lrd_rel th hP s σ G hDS p hp 0 (by decide) + obtain ⟨b1, hb1, hs1⟩ := lrd_rel th hP s σ G hDS p hp 1 (by decide) + have hcv := cv_spec σ p.bv slot cdata.bv vdata.bv ec ev b0 b1 hs0 hs1 + unfold tcb.derive_roots + rw [tcb_slot_ct]; simp only [bind_ok]; rw [hb0] + simp only [bind_ok] + by_cases h0 : b0 = true + · simp only [h0, ↓reduceIte, error.illegal] + exact ⟨_, rfl, Or.inr ⟨_, .illegalOperation, rfl, by rw [hcv, if_pos (Or.inl h0)], Or.inl trivial⟩⟩ + simp only [h0, Bool.false_eq_true, ↓reduceIte] + rw [tcb_slot_vt]; simp only [bind_ok]; rw [hb1]; simp only [bind_ok] + by_cases h1 : b1 = true + · simp only [h1, ↓reduceIte, error.illegal] + exact ⟨_, rfl, Or.inr ⟨_, .illegalOperation, rfl, by rw [hcv, if_pos (Or.inr h1)], Or.inl trivial⟩⟩ + simp only [h1, Bool.false_eq_true, ↓reduceIte] + rw [if_neg (by simp_all)] at hcv + obtain ⟨psc, hpsc, hpsc1⟩ := hecs + obtain ⟨psv, hpsv, hpsv1⟩ := hevs + have hwc := slot_wf hDS.1.1 hpsc hpsc1 + have hwv := slot_wf hDS.1.1 hpsv hpsv1 + have hvc : CapValid th (capV (L.mem s) (cslotAddr ec.2)) := + hDS.1.2.1.abs ⟨psc, hpsc1, Or.inl hpsc⟩ (capAt_capV _ _) + have hvv : CapValid th (capV (L.mem s) (cslotAddr ev.2)) := + hDS.1.2.1.abs ⟨psv, hpsv1, Or.inl hpsv⟩ (capAt_capV _ _) + obtain ⟨rc, hrc, rc', hrc', hrelc⟩ := dwd_rel s σ (cslotAddr ec.2) ec.2 _ (capAt_capV _ _) hwc.1 hwc.2 hncc cdata + rw [hrc] + rw [← hec] at hrc' + have ill : ∀ {α : Type} (k : Unit → Fl4Spec.SEMonad Unit α), + ((Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : Fl4Spec.SEMonad Unit Unit) >>= k).run σ = + ({(.error .illegalOperation, σ)}, False) := fun k => + run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.illegalOperation : + Fl4Spec.SEMonad Unit Unit)) rfl k + 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, + bind_tc_ok] + exact ⟨_, rfl, Or.inr ⟨e, e', rfl, by rw [hcv]; exact run_bind_error hrc' _, Or.inl hrelc⟩⟩ + | Ok c' => + cases rc' with + | error _ => exact hrelc.elim + | ok a => + obtain ⟨hw', ha', he', hcv', hcn, -⟩ := hrelc + subst ha' + rw [run_bind_ok hrc'] at hcv + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, is_cnode_eq] + unfold Fl4Spec.NonDetE.unlessE at hcv + by_cases hcn' : Fl4Spec.isCnodeCap (absCap c') = true + · simp only [hcn', ↓reduceIte] at hcv ⊢ + rw [show (pure () : Fl4Spec.SEMonad Unit Unit) = Fl4Spec.NonDetE.returnOk () from rfl, retOk_bind] at hcv + obtain ⟨rv, hrv, rv', hrv', hrelv⟩ := dwd_rel s σ (cslotAddr ev.2) ev.2 _ (capAt_capV _ _) hwv.1 hwv.2 hncv + vdata + rw [hrv] + rw [← hev] at hrv' + have hokc : NewCapOK th (L.mem s) c' ec.2 := by + obtain ⟨q, r, gs, g, rfl⟩ := cnode_of_abs hcn' + obtain ⟨gs0, g0, hc0⟩ := hcn _ _ _ _ rfl + refine ⟨psc, _, hpsc, hpsc1, rfl, hw', hcv' th hvc, he', fun h => (by cases h), ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> + rw [hc0] + · rfl + · exact Or.inl rfl + · rfl + · exact fun _ _ _ _ _ _ => trivial + · exact fun h => False.elim h + · exact fun _ => trivial + · exact fun h => absurd rfl h + cases rv with + | Err e => + cases rv' with + | ok _ => exact hrelv.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, bind_tc_ok] + exact ⟨_, rfl, Or.inr ⟨e, e', rfl, by rw [hcv]; exact run_bind_error hrv' _, Or.inl hrelv⟩⟩ + | Ok v' => + cases rv' with + | error _ => exact hrelv.elim + | ok av => + obtain ⟨hwv', hav, hev', hcvv', -, hpm⟩ := hrelv + subst hav + rw [run_bind_ok hrv'] at hcv + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, Vs.is_valid_vtable_root_eq] + by_cases hvr : Fl4Spec.isValidVtableRoot (absCap v') + · simp only [hvr, decide_true, ↓reduceIte] at hcv ⊢ + rw [retOk_bind] at hcv + have hokv : NewCapOK th (L.mem s) v' ev.2 := by + obtain ⟨q, aa, rfl⟩ := pml4_of_abs hvr + have hv0 := hpm _ _ _ rfl + refine ⟨psv, _, hpsv, hpsv1, rfl, hwv', hcvv' th hvv, hev', fun h => (by cases h), ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> + rw [hv0] + · exact Or.inl rfl + · exact fun _ _ _ _ _ h => h + · exact fun h => absurd h (by simp [Vs.TabUn]) + · exact id + · exact fun _ => ⟨rfl, by simp [Vs.TabUn]⟩ + have hvkc : vkeyF c' = none := by + obtain ⟨q, r, gs, g, rfl⟩ := cnode_of_abs hcn' + rfl + have hvkv : vkeyF v' = none := by + obtain ⟨q, aa, rfl⟩ := pml4_of_abs hvr + rfl + exact ⟨_, rfl, Or.inl ⟨c', v', rfl, hcv, hokc, hokv, hvkc, hvkv⟩⟩ + · simp only [hvr, decide_false, Bool.false_eq_true, ↓reduceIte, error.illegal] at hcv ⊢ + exact ⟨_, rfl, Or.inr ⟨_, .illegalOperation, rfl, by rw [hcv]; exact ill _, Or.inl trivial⟩⟩ + · simp only [hcn', Bool.false_eq_true, ↓reduceIte, error.illegal] at hcv ⊢ + exact ⟨_, rfl, Or.inr ⟨_, .illegalOperation, rfl, by rw [hcv]; exact ill _, Or.inl trivial⟩⟩ + +set_option maxHeartbeats 16000000 in +/-- **The SetTimeoutEndpoint label at `JAt`** (`JAtPre` if preempted), as +TcbCtl's `tcb_set_timeout_refinesW`: the decode, the caller's restart +(`JAtZ.restartWJR`), the timeout handler's install by `thread_control_capsJM` +at the deletion loop's steps (`DelHypsWR` at `WJR`), `top_of_instJM`. -/ +theorem tcb_set_timeoutVE [Fl4Proofs.P1.PlainStore st] {W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} (on rc : Fl4Spec.ObjRef → Bool) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) {V X : Set U64} {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hsg : SpecGlob σ) (hP0 : PPR0 σ) + (hHp : TcbInsWV (L := L) arI AR MR Z H Cs W Wp) (hR : RestartWJ (st := st) Z s ar σ (W (L.ksb s))) + (p : U64) (hp : Z.th p = some .tcb) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetTimeoutEndpoint) + (length buf ct : U64) (hct : sched.cur_thread st s = ok ct) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hX : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (args : List Fl4Spec.Data) (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + TcbOKV (L := L) arI (AR := AR) (MR := MR) on rc s ar σ fl4_abi.label.TCBSetTimeoutEndpoint p + ⟨length, buf, call, cslotAddr ssl, extra⟩ blk call don slbl args ci ssl excaps buffer := by + have hJ0 : VEAnyX (L := L) arI AR s ar σ := J.anyX arI + have hdec0 : specDec false slbl args ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetTimeoutEp (Z := Unit) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + unfold TcbOKV + rw [decode_timeout_eq arI s ar p _ hstall, hdec0] + unfold tcb.decode_set_timeout_endpoint tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + by_cases h0 : x0 = 0#u64 + · -- no extra cap: truncated + have hl : excaps.length ≤ 0 := ((hX.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.truncatedMessage, ?_, Or.inl trivial⟩, hJ0⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetTimeoutEp + rw [whenE_pos (by omega)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + obtain ⟨e, he, hxe, he1, ps, hps, hps1⟩ := hX.get hx0' h0 + have hge : (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 0#u64 := by scalar_tac + simp only [h0, ↓reduceIte, if_pos hge, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, slot_cap_eq, + capAt_capV, vfh_eq] + have hspec0 : ∀ v : Prop, [Decidable v] → v = Fl4Spec.validFaultHandler (absCap (capV (L.mem s) x0)) → + (Fl4Spec.decodeSetTimeoutEp (Z := Unit) (.threadCap p.bv) ssl excaps).run σ = + (Fl4Spec.checkHandlerEp (Z := Unit) 1 e >>= fun handler => + Fl4Spec.NonDetE.returnOk (.threadControlCaps p.bv ssl none (some handler) none none none)).run σ := by + intro v _ _ + unfold Fl4Spec.decodeSetTimeoutEp + rw [whenE_neg (by have := List.length_pos_of_ne_nil (List.ne_nil_of_mem (List.mem_of_getElem? he)); omega), + retOk_bind, retOk_bind, he, the_some] + rfl + by_cases hv : Fl4Spec.validFaultHandler (absCap (capV (L.mem s) x0)) + · rw [decide_eq_true hv]; simp only [↓reduceIte] + obtain ⟨s1, σ1, hrc, hst1, hDS1, hW1, hS1⟩ := J.restartWV arI hni hct (hR ct hct) + rw [hrc]; simp only [bind_ok] + have hxs : IsSlot Z.th x0 := by rw [hxe]; exact AbsSlot.isSlot ⟨ps, hps1, Or.inl hps⟩ + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetTimeoutEp (Z := Unit) (.threadCap p.bv) ssl excaps)).run σ = + ({(.ok (tcInv p ssl tcb.UPDATE_TIMEOUT (.Null, (0, [])) (capV (L.mem s) x0, e.2) (.Null, (0, [])) + (.Null, (0, [])) (0 : BitVec 64) none), σ)}, False) := by + apply run_liftME_ok + rw [hspec0 _ rfl] + unfold Fl4Spec.checkHandlerEp + rw [Fl4Spec.NonDetE.unlessE, if_pos (by rw [he1]; exact hv)] + show ((Fl4Spec.NonDetE.returnOk () >>= fun _ => Fl4Spec.NonDetE.returnOk e) >>= _).run σ = _ + rw [retOk_bind, retOk_bind] + obtain ⟨e1, e2⟩ := e + simp only at he1 + subst he1 + simp only [tcInv, optArg, flags_T, ↓reduceIte] + rfl + have hnf := vfh_not_frame hv + have hc1 : capV (L.mem s1) x0 = capV (L.mem s) x0 := hS1.capV hxs + have hU0 : (tcb.UPDATE_TIMEOUT &&& tcb.UPDATE_IPC_BUFFER) = 0#u64 := by + simp only [tcb.UPDATE_TIMEOUT, tcb.UPDATE_IPC_BUFFER]; decide + have hT' := thread_control_capsVE arI (hHp.ins (L.ksb s1)) s1 ar σ1 Z.G hDS1 hW1 p hp ssl hAs tcb.UPDATE_TIMEOUT + (.Null, (0, [])) (capV (L.mem s) x0, e.2) (.Null, (0, [])) (.Null, (0, [])) (fun h => (Eq.mp flags_T.1 h).elim) + (fun _ => by + show NewCapOK Z.th (L.mem s1) (capV (L.mem s) x0) e.2 + rw [← hc1, hxe] + exact newCapOK_self hDS1.1.1 hDS1.1.2.1 hps hps1 (by rw [← hxe, hc1]; exact hv)) + (fun h => (Eq.mp flags_T.2.2.1 h).elim) (fun h => (Eq.mp flags_T.2.2.1 h).elim) (fun _ h => absurd rfl h) + (fun h => (Eq.mp flags_T.1 h).elim) (fun _ => by rw [← hxe, hc1]; exact vtOK_vfh hv) (fun h => (Eq.mp flags_T.2.2.1 h).elim) (fun h => (Eq.mp flags_T.2.2.1 h).elim) + 0#u64 0 (fun h => absurd hU0 h) .Null none (fun _ _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) + simp only [cslot_zero, ← hxe] at hT' + exact WP.spec_mono hT' (fun res h => top_of_instVE arI (hHp.ins _) (DecOut.of_eq hdec) hst1 h) + · rw [decide_eq_false hv]; simp only [Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.invalidCapability 1, ?_, + Or.inr ⟨1#u64, rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec0 _ rfl] + unfold Fl4Spec.checkHandlerEp + rw [Fl4Spec.NonDetE.unlessE, if_neg (by rw [he1]; exact hv)] + exact run_bind_error (run_bind_error (m := (Fl4Spec.NonDetE.throwError (Fl4Spec.SyscallError.invalidCapability 1) : + Fl4Spec.SEMonad Unit Unit)) rfl _) _ + +/-- **FL4's `derive_for_buffer` is the spec's buffer frame** (`bufSpec`): +FL4's buffer argument of `thread_control_caps` (`bufArg`), a new cap of an +install (`NewCapOK`) if a frame, or the same error. -/ +theorem buf_rel_vt (th : TypedHeap) (s : S) (σ : Fl4Spec.State Unit) (G : MdbGhost) + (hDS : DState (L := L) th s σ G) (y : U64) (e : Fl4Spec.Cap × Fl4Spec.CslotPtr) + (he1 : e.1 = absCap (capV (L.mem s) (cslotAddr e.2))) + (hes : ∃ ps, th ps = some (.cnode e.2.2.length) ∧ e.2.1 = ps.bv) + (hnc : isUt (capV (L.mem s) (cslotAddr e.2)) = true → NoChildrenRel (st := st) s σ (cslotAddr e.2) e.2) : + ∃ r, tcb.derive_for_buffer st s y (cslotAddr e.2) = ok r ∧ + ((∃ bc0 bfr, r = .Ok (bufArg bc0 bfr) ∧ (bufSpec y.bv e).run σ = ({(.ok (frameOf bfr), σ)}, False) ∧ + (∀ c sl, bfr = some (c, sl) → NewCapOK th (L.mem s) c sl) ∧ + (∀ c sl, bfr = some (c, sl) → BufFrOK y.bv c) ∧ ∀ c sl, bfr = some (c, sl) → vkeyF c = none) ∨ + (∃ er er', r = .Err er ∧ (bufSpec y.bv e).run σ = ({(.error er', σ)}, False) ∧ ErrRel er er')) := by + obtain ⟨ps, hps, hps1⟩ := hes + obtain ⟨e1, e2⟩ := e + simp only at he1 hps hps1 hnc ⊢ + subst he1 + have hcwf := slot_wf hDS.1.1 hps hps1 + have hcv0 : CapValid th (capV (L.mem s) (cslotAddr e2)) := + hDS.1.2.1.abs ⟨ps, hps1, Or.inl hps⟩ (capAt_capV _ _) + unfold tcb.derive_for_buffer bufSpec + by_cases hy : y = 0#u64 + · have hy' : y.bv = 0 := by rw [hy]; rfl + simp only [hy, ↓reduceIte, slot_cap_eq, capAt_capV, bind_ok, show U64.bv 0#u64 = 0 from rfl] + exact ⟨_, rfl, Or.inl ⟨_, none, rfl, rfl, fun _ _ h => (by cases h), fun _ _ h => (by cases h), fun _ _ h => (by cases h)⟩⟩ + have hy' : ¬ y.bv = 0 := fun h => hy (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat h; simpa using this)) + simp only [hy, hy', ↓reduceIte, slot_cap_eq, capAt_capV, bind_ok] + generalize hc0e : capV (L.mem s) (cslotAddr e2) = c0 at hcwf hcv0 hnc ⊢ + obtain ⟨r, hr, r', hr', hrel⟩ := derive_rel (Z := Unit) (st := st) s σ (cslotAddr e2) e2 c0 + hcwf.1 hcwf.2 (fun h => hnc (by cases c0 <;> simp_all [isUt])) + rw [hr] + cases r with + | Err er => + cases r' with + | ok _ => exact hrel.elim + | error er' => + 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 ⟨_, rfl, Or.inr ⟨er, er', rfl, run_bind_error hr' _, Or.inl hrel⟩⟩ + | Ok c' => + cases r' with + | error _ => exact hrel.elim + | ok a => + obtain ⟨hwf', ha, hev', hcv', -⟩ := hrel + subst ha + obtain ⟨rc, hrc', hcase⟩ := Vs.check_valid_ipc_buffer_rel y c' σ + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, hrc'] + rcases hcase with ⟨rfl, hck⟩ | ⟨er, er', rfl, hmer, hck⟩ + · obtain ⟨q, sz, rr, m, ep, aa, va, rfl⟩ := check_ok_frame hrc' + obtain ⟨rfl, m0, ep0, a0, va0, hc0⟩ := derive_frame_inv (fun h => ⟨_, hnc h⟩) hr + subst hc0 + have hcvc : CapValid th (capability.Cap.Frame q sz rr false m false aa va) := hcv' th hcv0 + refine ⟨.Ok (bufArg .Null (some (.Frame q sz rr false m false aa va, e2))), by simp [bufArg], + Or.inl ⟨.Null, some (.Frame q sz rr false m false aa va, e2), rfl, ?_, ?_, ?_, fun c sl h => by cases h; cases m <;> simp [vkeyF]⟩⟩ + · rw [run_bind_ok hr', run_bind_ok hck]; rfl + rotate_left + · intro c sl h + cases h + exact ⟨⟨q, sz, rr, m, aa, va, rfl⟩, Vs.check_valid_ipc_buffer_al hrc'⟩ + · intro c sl h + cases h + refine ⟨ps, _, hps, hps1, rfl, hwf', hcvc, hev', fun _ => frame_so_refl hcvc, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ <;> + rw [hc0e] <;> first | rfl | exact Or.inl rfl | exact derive_vcap hr | exact fun h => False.elim h | exact fun h => absurd rfl h | exact fun _ => by simp [Vs.NativeCap] + · simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, bind_ok] + exact ⟨_, rfl, Or.inr ⟨er, er', rfl, (run_bind_ok hr' _).trans (run_bind_error hck _), ErrRel.of_map hmer⟩⟩ + +set_option maxHeartbeats 16000000 in +/-- **The SetSpace label at `JAt`** (`JAtPre` if preempted), as TcbCtl's +`tcb_set_space_refinesW`: the decode (the roots by `roots_rel`), the +caller's restart (`JAtZ.restartWJR`), the fault handler's and the roots' +installs by `thread_control_capsJM`, `top_of_instJM`. -/ +theorem tcb_set_spaceVE [Fl4Proofs.P1.PlainStore st] {W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} (on rc : Fl4Spec.ObjRef → Bool) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) {V X : Set U64} {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hsg : SpecGlob σ) (hP0 : PPR0 σ) + (hHp : TcbInsWV (L := L) arI AR MR Z H Cs W Wp) (hR : RestartWJ (st := st) Z s ar σ (W (L.ksb s))) + (p : U64) (hp : Z.th p = some .tcb) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetSpace) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (hx2 : Aeneas.Std.Array.index_usize extra 2#usize = ok x2) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hX : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hnc : NcHyp (st := st) Z.th s σ x0 x1 x2) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + TcbOKV (L := L) arI (AR := AR) (MR := MR) on rc s ar σ fl4_abi.label.TCBSetSpace p + ⟨length, buf, call, cslotAddr ssl, extra⟩ blk call don slbl (args.map UScalar.bv) ci ssl excaps buffer := by + have hJ0 : VEAnyX (L := L) arI AR s ar σ := J.anyX arI + have hP := J.1.1.xtop.rel.1 + have hDS : DState (L := L) Z.th s σ Z.G := ⟨J.1.1.xtop.rel.2.1, J.1.1.del, J.1.1.xtop.rel.2.2.1⟩ + unfold TcbOKV + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSpace (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_space_eq arI s ar p _ hstall, hdec0] + have htr : args.length < 2 ∨ excaps.length < 3 → + (ok ((.Err (.Sys .TruncatedMessage) : core.result.Result Unit error.Exc), s, ar) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSpace (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps)) s res ⦄ := by + intro h + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.truncatedMessage, ?_, Or.inl trivial⟩, hJ0⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetSpace + rw [whenE_pos (by simpa using h)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + unfold tcb.decode_set_space tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0, extra_eq _ 1#usize (by decide) hx1, extra_eq _ 2#usize (by decide) hx2] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + have hx1' : [x0, x1, x2][1]? = some x1 := rfl + have hx2' : [x0, x1, x2][2]? = some x2 := rfl + have hge : (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 2#u64 ↔ 2 ≤ args.length := by + show length >= 2#u64 ↔ _; rw [← hlen]; scalar_tac + by_cases h0 : x0 = 0#u64 + · have hl : excaps.length ≤ 0 := ((hX.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + exact htr (Or.inr (by omega)) + by_cases hl : 2 ≤ args.length + swap + · simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inl (by omega)) + by_cases h1 : x1 = 0#u64 + · have hl1 : excaps.length ≤ 1 := ((hX.2 1 x1 hx1').1).1 h1 + simp only [h0, h1, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inr (by omega)) + by_cases h2 : x2 = 0#u64 + · have hl2 : excaps.length ≤ 2 := ((hX.2 2 x2 hx2').1).1 h2 + simp only [h0, h1, h2, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inr (by omega)) + simp only [h0, h1, h2, ↓reduceIte, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, hge, hl] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, hl with + | y0 :: y1 :: rest, _ => exact ⟨_, _, _, rfl⟩ + have ha0 : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have ha1 : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 1#u64 = ok y1 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 1#u64 y1 (by simp) + rw [ha0, ha1]; simp only [bind_ok] + obtain ⟨e0, he0, hxe0, he01, ps0, hps0, hps01⟩ := hX.get hx0' h0 + obtain ⟨e1, he1, hxe1, he11, ps1, hps1, hps11⟩ := hX.get hx1' h1 + obtain ⟨e2, he2, hxe2, he21, ps2, hps2, hps21⟩ := hX.get hx2' h2 + have hexc : excaps = [e0, e1, e2] := by + have h3 := hX.1 + match excaps, he0, he1, he2, h3 with + | [a, b, c], h0', h1', h2', _ => simp at h0' h1' h2'; rw [h0', h1', h2'] + subst hexc + subst hxe1 hxe2 + obtain ⟨r, hr, hcase⟩ := roots_rel_vt Z.th hP s σ Z.G hDS p hp y0 y1 e1 e2 he11 ⟨ps1, hps1, hps11⟩ he21 + ⟨ps2, hps2, hps21⟩ (hnc _ (by simp) e1.2 ⟨ps1, hps11, Or.inl hps1⟩ rfl) (hnc _ (by simp) e2.2 ⟨ps2, hps21, Or.inl hps2⟩ rfl) ssl + rw [hr] + have hspec : (Fl4Spec.decodeSetSpace (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + [e0, e1, e2]).run σ = + (Fl4Spec.decodeCvSpace (Z := Unit) [y0.bv, y1.bv] (.threadCap p.bv) ssl [e1, e2] >>= fun space => + Fl4Spec.checkHandlerEp (Z := Unit) 1 e0 >>= fun fh => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlCaps p.bv ssl (some fh) none + space.tcNewCroot space.tcNewVroot none)).run σ := by + unfold Fl4Spec.decodeSetSpace + rw [whenE_neg (by simp), retOk_bind] + simp only [List.map_cons, List.take_succ_cons, List.take_zero, List.drop_succ_cons, List.drop_zero, + List.getElem?_cons_zero, the_some, retOk_bind] + rfl + rcases hcase with ⟨cc, vc, rfl, hcv, hokc, hokv, hvkc, hvkv⟩ | ⟨e, e', rfl, hcv, herr⟩ + · simp only [slot_cap_eq, capAt_capV, vfh_eq, bind_ok] + rw [run_bind_ok hcv] at hspec + by_cases hv : Fl4Spec.validFaultHandler (absCap (capV (L.mem s) x0)) + · rw [decide_eq_true hv]; simp only [↓reduceIte, lift, bind_ok] + obtain ⟨s1, σ1, hrc, hst1, hDS1, hW1, hS1⟩ := J.restartWV arI hni hct (hR ct hct) + rw [hrc]; simp only [bind_ok] + have hxs : IsSlot Z.th x0 := by rw [hxe0]; exact AbsSlot.isSlot ⟨ps0, hps01, Or.inl hps0⟩ + have hc1 : capV (L.mem s1) x0 = capV (L.mem s) x0 := hS1.capV hxs + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSpace (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + [e0, e1, e2])).run σ = + ({(.ok (tcInv p ssl (tcb.UPDATE_SPACE ||| tcb.UPDATE_FAULT) (capV (L.mem s) x0, e0.2) (.Null, (0, [])) + (cc, e1.2) (vc, e2.2) (0 : BitVec 64) none), σ)}, False) := by + apply run_liftME_ok + rw [hspec] + unfold Fl4Spec.checkHandlerEp + rw [Fl4Spec.NonDetE.unlessE, if_pos (by rw [he01]; exact hv)] + show ((Fl4Spec.NonDetE.returnOk () >>= fun _ => Fl4Spec.NonDetE.returnOk e0) >>= _).run σ = _ + rw [retOk_bind, retOk_bind] + obtain ⟨e01, e02⟩ := e0 + simp only at he01 + subst he01 + simp only [optArg, flags_SF, ↓reduceIte] + rfl + have hU0 : ((tcb.UPDATE_SPACE ||| tcb.UPDATE_FAULT) &&& tcb.UPDATE_IPC_BUFFER) = 0#u64 := by + simp only [tcb.UPDATE_SPACE, tcb.UPDATE_FAULT, tcb.UPDATE_IPC_BUFFER]; decide + have hT' := thread_control_capsVE arI (hHp.ins (L.ksb s1)) s1 ar σ1 Z.G hDS1 hW1 p hp ssl hAs + (tcb.UPDATE_SPACE ||| tcb.UPDATE_FAULT) + (capV (L.mem s) x0, e0.2) (.Null, (0, [])) (cc, e1.2) (vc, e2.2) + (fun _ => by + show NewCapOK Z.th (L.mem s1) (capV (L.mem s) x0) e0.2 + rw [← hc1, hxe0] + exact newCapOK_self hDS1.1.1 hDS1.1.2.1 hps0 hps01 (by rw [← hxe0, hc1]; exact hv)) + (fun h => (Eq.mp flags_SF.2.1 h).elim) (fun _ => hokc.of_same hS1) (fun _ => hokv.of_same hS1) + (fun _ => vfh_ep hv) (fun _ => by rw [← hxe0, hc1]; exact vtOK_vfh hv) (fun h => (Eq.mp flags_SF.2.1 h).elim) + (fun _ => Or.inl hvkc) (fun _ => Or.inl hvkv) + 0#u64 0 (fun h => absurd hU0 h) .Null none (fun _ _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) + simp only [cslot_zero, ← hxe0, bufArg] at hT' + exact WP.spec_mono hT' (fun res h => top_of_instVE arI (hHp.ins _) (DecOut.of_eq hdec) hst1 h) + · rw [decide_eq_false hv]; simp only [Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.invalidCapability 1, ?_, + Or.inr ⟨1#u64, rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec] + unfold Fl4Spec.checkHandlerEp + rw [Fl4Spec.NonDetE.unlessE, if_neg (by rw [he01]; exact hv)] + exact run_bind_error (run_bind_error (m := (Fl4Spec.NonDetE.throwError + (Fl4Spec.SyscallError.invalidCapability 1) : Fl4Spec.SEMonad Unit Unit)) rfl _) _ + · simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, bind_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨e', ?_, herr⟩, hJ0⟩) + apply run_liftME_err + rw [hspec] + exact run_bind_error hcv _ + +set_option maxHeartbeats 16000000 in +/-- **The SetIPCBuffer label at `JAt`** (`JAtPre` if preempted), as TcbCtl's +`tcb_set_ipc_refinesW`: the decode (the buffer's frame cap derived and +checked), the caller's restart (`JAtZ.restartWJR`), the buffer's install by +`thread_control_capsJM` (`install_bufferJM`, the IPC-buffer word and the tail +at `WJR` by `bufHypW_J`), `top_of_instJM`. -/ +theorem tcb_set_ipcVE [Fl4Proofs.P1.PlainStore st] {W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} (on rc : Fl4Spec.ObjRef → Bool) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) {V X : Set U64} {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hsg : SpecGlob σ) (hP0 : PPR0 σ) + (hHp : TcbInsWV (L := L) arI AR MR Z H Cs W Wp) (hR : RestartWJ (st := st) Z s ar σ (W (L.ksb s))) + (p : U64) (hp : Z.th p = some .tcb) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetIPCBuffer) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hX : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hnc : ∀ sl : Fl4Spec.CslotPtr, AbsSlot Z.th sl → x0 = cslotAddr sl → isUt (capV (L.mem s) x0) = true → + NoChildrenRel (st := st) s σ x0 sl) + (hfnz : FrNZ Z.th (L.mem s)) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + TcbOKV (L := L) arI (AR := AR) (MR := MR) on rc s ar σ fl4_abi.label.TCBSetIPCBuffer p + ⟨length, buf, call, cslotAddr ssl, extra⟩ blk call don slbl (args.map UScalar.bv) ci ssl excaps buffer := by + have hJ0 : VEAnyX (L := L) arI AR s ar σ := J.anyX arI + have hDS : DState (L := L) Z.th s σ Z.G := ⟨J.1.1.xtop.rel.2.1, J.1.1.del, J.1.1.xtop.rel.2.2.1⟩ + unfold TcbOKV + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetIpcBuffer (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_ipc_eq arI s ar p _ hstall, hdec0] + unfold tcb.decode_set_ipc_buffer tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + -- the truncated cases + have htr : args = [] ∨ excaps = [] → + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetIpcBuffer (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps)).run σ = + ({(.error .truncatedMessage, σ)}, False) := by + intro h + apply run_liftME_err + unfold Fl4Spec.decodeSetIpcBuffer + by_cases ha : args = [] + · subst ha + rw [whenE_pos (by simp)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + · have he : excaps = [] := h.resolve_left ha + subst he + rw [whenE_neg (by simpa using ha), retOk_bind, whenE_pos (by simp)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + have htrS : (args = [] ∨ excaps = []) → TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetIpcBuffer (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps)) s + (.Err (.Sys .TruncatedMessage), s, ar) := fun h => + VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.truncatedMessage, htr h, Or.inl trivial⟩, hJ0⟩ + by_cases h0 : x0 = 0#u64 + · have hl : excaps.length ≤ 0 := ((hX.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, 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 (htrS (Or.inr (List.eq_nil_of_length_eq_zero (by omega)))) + obtain ⟨e, he, hxe, he1, ps, hps, hps1⟩ := hX.get hx0' h0 + by_cases h1 : length < 1#u64 + · have hge : ¬ (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 1#u64 := by + show ¬ length >= 1#u64; scalar_tac + simp only [h0, ↓reduceIte, if_neg hge, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + refine (WP.spec_ok _).2 (htrS (Or.inl ?_)) + exact List.eq_nil_of_length_eq_zero (by scalar_tac) + have hge : (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 1#u64 := by + show length >= 1#u64; scalar_tac + obtain ⟨y0, rest, rfl⟩ : ∃ y0 rest, args = y0 :: rest := by + match args with + | [] => exfalso; simp at hlen; scalar_tac + | y0 :: rest => exact ⟨y0, rest, rfl⟩ + have harg : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + simp only [h0, ↓reduceIte, if_pos hge, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, harg] + -- the spec, up to the buffer + obtain ⟨e1, e2⟩ := e + simp only at hxe he1 hps hps1 + subst he1 hxe + have hspec : (Fl4Spec.decodeSetIpcBuffer (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps).run σ = + ((if y0.bv = 0 then Fl4Spec.NonDetE.returnOk none else do + let bc ← Fl4Spec.deriveCap (Z := Unit) e2 (absCap (capV (L.mem s) (cslotAddr e2))) + Fl4Spec.checkValidIpcBuffer y0.bv bc + Fl4Spec.NonDetE.returnOk (some (bc, e2)) : Fl4Spec.SEMonad Unit _) >>= fun nb => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlCaps p.bv ssl none none none none + (some (y0.bv, nb)))).run σ := by + unfold Fl4Spec.decodeSetIpcBuffer + have hne : excaps ≠ [] := List.ne_nil_of_mem (List.mem_of_getElem? he) + rw [whenE_neg (by simp), retOk_bind, whenE_neg (by simpa using hne), retOk_bind, retOk_bind] + rw [show (List.map UScalar.bv (y0 :: rest))[0]? = some y0.bv from rfl, the_some, retOk_bind, he, the_some] + rfl + obtain ⟨s1, σ1, hrc, hst1, hDS1, hW1, hS1⟩ := J.restartWV arI hni hct (hR ct hct) + have hxs : IsSlot Z.th (cslotAddr e2) := AbsSlot.isSlot ⟨ps, hps1, Or.inl hps⟩ + have hc1 : capV (L.mem s1) (cslotAddr e2) = capV (L.mem s) (cslotAddr e2) := hS1.capV hxs + have hcwf := slot_wf hDS.1.1 hps hps1 + have hcv0 : CapValid Z.th (capV (L.mem s) (cslotAddr e2)) := + hDS.1.2.1.abs ⟨ps, hps1, Or.inl hps⟩ (capAt_capV _ _) + have hB : (tcb.UPDATE_IPC_BUFFER &&& tcb.UPDATE_IPC_BUFFER) ≠ 0#u64 → + BufHypW (L := L) (W (L.ksb s1)) Z.th p y0 y0.bv := + fun _ => (hHp.ins (L.ksb s1)).buf p hp y0 + by_cases hy : y0 = 0#u64 + · -- no frame + have hy' : y0.bv = 0 := by rw [hy]; rfl + have hdfb : tcb.derive_for_buffer st s y0 (cslotAddr e2) = ok (.Ok (capV (L.mem s) (cslotAddr e2), 0#u64)) := by + unfold tcb.derive_for_buffer; rw [if_pos hy]; simp [slot_cap_eq, capAt_capV] + rw [hdfb]; simp only [bind_ok, hrc] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetIpcBuffer (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl excaps)).run σ = + ({(.ok (tcInv p ssl tcb.UPDATE_IPC_BUFFER (.Null, (0, [])) (.Null, (0, [])) (.Null, (0, [])) + (.Null, (0, [])) y0.bv none), σ)}, False) := by + apply run_liftME_ok + rw [hspec, if_pos hy', retOk_bind] + simp only [tcInv, optArg, flags_B, ↓reduceIte, frameOf] + rfl + have hT' := thread_control_capsVE arI (hHp.ins (L.ksb s1)) s1 ar σ1 Z.G hDS1 hW1 p hp ssl hAs tcb.UPDATE_IPC_BUFFER + (.Null, (0, [])) (.Null, (0, [])) (.Null, (0, [])) (.Null, (0, [])) (fun h => (Eq.mp flags_B.1 h).elim) + (fun h => (Eq.mp flags_B.2.1 h).elim) (fun h => (Eq.mp flags_B.2.2.1 h).elim) + (fun h => (Eq.mp flags_B.2.2.1 h).elim) (fun _ h => absurd rfl h) + (fun h => (Eq.mp flags_B.1 h).elim) (fun h => (Eq.mp flags_B.2.1 h).elim) (fun h => (Eq.mp flags_B.2.2.1 h).elim) (fun h => (Eq.mp flags_B.2.2.1 h).elim) + y0 y0.bv hB + (capV (L.mem s) (cslotAddr e2)) none (fun _ _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) + simp only [cslot_zero, bufArg] at hT' + exact WP.spec_mono hT' (fun res h => top_of_instVE arI (hHp.ins _) (DecOut.of_eq hdec) hst1 h) + -- a frame: derived and checked + have hy' : ¬ y0.bv = 0 := fun h => hy (UScalar.eq_of_val_eq (by + have := congrArg BitVec.toNat h; simpa using this)) + unfold tcb.derive_for_buffer + rw [if_neg hy] + simp only [slot_cap_eq, capAt_capV, bind_ok] + generalize capV (L.mem s) (cslotAddr e2) = c0 at hcwf hcv0 hc1 hnc hspec ⊢ + obtain ⟨r, hr, r', hr', hrel⟩ := derive_rel (st := st) s σ (cslotAddr e2) e2 c0 + hcwf.1 hcwf.2 (fun h => hnc e2 ⟨ps, hps1, Or.inl hps⟩ rfl (by cases c0 <;> simp_all [isUt])) + simp only [hr, bind_ok] + cases r with + | Err er => + cases r' with + | ok _ => exact hrel.elim + | error er' => + 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 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨er', ?_, Or.inl hrel⟩, hJ0⟩) + apply run_liftME_err + rw [hspec, if_neg hy'] + exact run_bind_error (run_bind_error hr' _) _ + | Ok c' => + cases r' with + | error _ => exact hrel.elim + | ok a => + obtain ⟨hwf', ha, hev', hcv', -⟩ := hrel + obtain ⟨rc, hrc', hcase⟩ := Vs.check_valid_ipc_buffer_rel y0 c' σ + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, hrc'] + rcases hcase with ⟨rfl, hck⟩ | ⟨er, er', rfl, hmer, hck⟩ + · simp only [bind_ok, hrc] + obtain ⟨q, sz, rr, m, ep, aa, va, rfl⟩ := check_ok_frame hrc' + obtain ⟨rfl, m0, ep0, a0, va0, hc0⟩ := derive_frame_inv (fun h => ⟨_, hnc e2 ⟨ps, hps1, Or.inl hps⟩ rfl h⟩) hr + have hcvc : CapValid Z.th (capability.Cap.Frame q sz rr false m false aa va) := hcv' Z.th hcv0 + have hok : NewCapOK Z.th (L.mem s1) (.Frame q sz rr false m false aa va) e2 := by + refine ⟨ps, _, hps, hps1, rfl, hwf', hcvc, hev', fun _ => frame_so_refl hcvc, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [hc1, hc0]; rfl + · rw [hc1, hc0]; exact Or.inl rfl + · rw [hc1, hc0]; rfl + · rw [hc1]; exact derive_vcap hr + · exact fun h => False.elim h + · exact fun _ => by simp [Vs.NativeCap] + · exact fun h => absurd rfl h + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetIpcBuffer (Z := Unit) ((y0 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)).run σ = + ({(.ok (tcInv p ssl tcb.UPDATE_IPC_BUFFER (.Null, (0, [])) (.Null, (0, [])) (.Null, (0, [])) + (.Null, (0, [])) y0.bv (some (.Frame q sz rr false m false aa va, e2))), σ)}, False) := by + apply run_liftME_ok + rw [← ha] at hr' + have hin : ((do + let bc ← Fl4Spec.deriveCap (Z := Unit) e2 (absCap c0) + Fl4Spec.checkValidIpcBuffer y0.bv bc + Fl4Spec.NonDetE.returnOk (some (bc, e2)) : Fl4Spec.SEMonad Unit _)).run σ = + ({(.ok (some (absCap (.Frame q sz rr false m false aa va), e2)), σ)}, False) := by + rw [run_bind_ok hr', run_bind_ok hck]; rfl + rw [hspec, if_neg hy', run_bind_ok hin] + simp only [tcInv, optArg, flags_B, ↓reduceIte, frameOf] + rfl + have hT' := thread_control_capsVE arI (hHp.ins (L.ksb s1)) s1 ar σ1 Z.G hDS1 hW1 p hp ssl hAs + tcb.UPDATE_IPC_BUFFER (.Null, (0, [])) (.Null, (0, [])) (.Null, (0, [])) (.Null, (0, [])) + (fun h => (Eq.mp flags_B.1 h).elim) (fun h => (Eq.mp flags_B.2.1 h).elim) + (fun h => (Eq.mp flags_B.2.2.1 h).elim) (fun h => (Eq.mp flags_B.2.2.1 h).elim) (fun _ h => absurd rfl h) + (fun h => (Eq.mp flags_B.1 h).elim) (fun h => (Eq.mp flags_B.2.1 h).elim) (fun h => (Eq.mp flags_B.2.2.1 h).elim) (fun h => (Eq.mp flags_B.2.2.1 h).elim) + y0 y0.bv hB .Null (some (.Frame q sz rr false m false aa va, e2)) (fun c sl h => by cases h; exact hok) + (fun c sl h => by cases h; exact ⟨⟨q, sz, rr, m, aa, va, rfl⟩, Vs.check_valid_ipc_buffer_al hrc'⟩) + (fun _ _ _ => hfnz.of_same hS1) (fun c sl h => by cases h; exact Or.inl (by cases m <;> simp [vkeyF])) + simp only [cslot_zero, bufArg] at hT' + exact WP.spec_mono hT' (fun res h => top_of_instVE arI (hHp.ins _) (DecOut.of_eq hdec) hst1 h) + · simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, bind_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨er', ?_, ErrRel.of_map hmer⟩, hJ0⟩) + apply run_liftME_err + rw [hspec, if_neg hy'] + rw [← ha] at hr' + exact run_bind_error ((run_bind_ok hr' _).trans (run_bind_error hck _)) _ + +set_option maxHeartbeats 16000000 in +/-- **The Configure label at `JAt`** (`JAtPre` if preempted), as TcbCtl's +`tcb_configure_refinesW`: the decode (the buffer by `buf_rel`, the roots by +`roots_rel`), the caller's restart (`JAtZ.restartWJR`), the roots' and the +buffer's installs by `thread_control_capsJM` (the buffer's word and tail at +`WJR` by `bufHypW_J`), `top_of_instJM`. -/ +theorem tcb_configureVE [Fl4Proofs.P1.PlainStore st] {W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} (on rc : Fl4Spec.ObjRef → Bool) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) {V X : Set U64} {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hsg : SpecGlob σ) (hP0 : PPR0 σ) + (hHp : TcbInsWV (L := L) arI AR MR Z H Cs W Wp) (hR : RestartWJ (st := st) Z s ar σ (W (L.ksb s))) + (p : U64) (hp : Z.th p = some .tcb) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = .tcbConfigure) + (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) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (hx2 : Aeneas.Std.Array.index_usize extra 2#usize = ok x2) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hX : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hnc : NcHyp (st := st) Z.th s σ x0 x1 x2) + (hfnz : FrNZ Z.th (L.mem s)) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + TcbOKV (L := L) arI (AR := AR) (MR := MR) on rc s ar σ fl4_abi.label.TCBConfigure p + ⟨length, buf, call, cslotAddr ssl, extra⟩ blk call don slbl (args.map UScalar.bv) ci ssl excaps buffer := by + have hJ0 : VEAnyX (L := L) arI AR s ar σ := J.anyX arI + have hP := J.1.1.xtop.rel.1 + have hDS : DState (L := L) Z.th s σ Z.G := ⟨J.1.1.xtop.rel.2.1, J.1.1.del, J.1.1.xtop.rel.2.2.1⟩ + unfold TcbOKV + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeTcbConfigure (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rw [decode_configure_eq arI s ar p _ hstall, hdec0] + have htr : args.length < 3 ∨ excaps.length < 3 → + (ok ((.Err (.Sys .TruncatedMessage) : core.result.Result Unit error.Exc), s, ar) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeTcbConfigure (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps)) s res ⦄ := by + intro h + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.truncatedMessage, ?_, Or.inl trivial⟩, hJ0⟩) + apply run_liftME_err + unfold Fl4Spec.decodeTcbConfigure + by_cases ha : args.length < 3 + · rw [whenE_pos (by simpa using ha)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + · rw [whenE_neg (by simpa using ha), retOk_bind, whenE_pos (h.resolve_left ha)] + exact run_bind_error (m := (Fl4Spec.NonDetE.throwError Fl4Spec.SyscallError.truncatedMessage : + Fl4Spec.SEMonad Unit Unit)) rfl _ + unfold tcb.decode_configure tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0, extra_eq _ 1#usize (by decide) hx1, extra_eq _ 2#usize (by decide) hx2] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + have hx1' : [x0, x1, x2][1]? = some x1 := rfl + have hx2' : [x0, x1, x2][2]? = some x2 := rfl + have hge : (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 3#u64 ↔ 3 ≤ args.length := by + show length >= 3#u64 ↔ _; rw [← hlen]; scalar_tac + by_cases h0 : x0 = 0#u64 + · have hl : excaps.length ≤ 0 := ((hX.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + exact htr (Or.inr (by omega)) + by_cases hl : 3 ≤ args.length + swap + · simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inl (by omega)) + by_cases h1 : x1 = 0#u64 + · have hl1 : excaps.length ≤ 1 := ((hX.2 1 x1 hx1').1).1 h1 + simp only [h0, h1, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inr (by omega)) + by_cases h2 : x2 = 0#u64 + · have hl2 : excaps.length ≤ 2 := ((hX.2 2 x2 hx2').1).1 h2 + simp only [h0, h1, h2, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inr (by omega)) + simp only [h0, h1, h2, ↓reduceIte, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, hge, hl] + obtain ⟨y0, y1, y2, rest, rfl⟩ : ∃ y0 y1 y2 rest, args = y0 :: y1 :: y2 :: rest := by + match args, hl with + | y0 :: y1 :: y2 :: rest, _ => exact ⟨_, _, _, _, rfl⟩ + have ha0 : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 0#u64 = ok y0 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 0#u64 y0 (by simp) + have ha1 : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 1#u64 = ok y1 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 1#u64 y1 (by simp) + have ha2 : tcb.arg st s ⟨length, buf, call, cslotAddr ssl, extra⟩ 2#u64 = ok y2 := by + unfold tcb.arg; rw [hct]; simp only [bind_ok]; exact hargs 2#u64 y2 (by simp) + rw [ha0, ha1, ha2]; simp only [bind_ok] + obtain ⟨e0, he0, hxe0, he01, ps0, hps0, hps01⟩ := hX.get hx0' h0 + obtain ⟨e1, he1, hxe1, he11, ps1, hps1, hps11⟩ := hX.get hx1' h1 + obtain ⟨e2, he2, hxe2, he21, ps2, hps2, hps21⟩ := hX.get hx2' h2 + have hexc : excaps = [e0, e1, e2] := by + have h3 := hX.1 + match excaps, he0, he1, he2, h3 with + | [a, b, c], h0', h1', h2', _ => simp at h0' h1' h2'; rw [h0', h1', h2'] + subst hexc + subst hxe0 hxe1 hxe2 + have hspec : (Fl4Spec.decodeTcbConfigure (Z := Unit) ((y0 :: y1 :: y2 :: rest).map UScalar.bv) (.threadCap p.bv) + ssl [e0, e1, e2]).run σ = + (Fl4Spec.decodeSetIpcBuffer (Z := Unit) [y2.bv] (.threadCap p.bv) ssl [e2] >>= fun sp => + Fl4Spec.decodeCvSpace (Z := Unit) [y0.bv, y1.bv] (.threadCap p.bv) ssl [e0, e1] >>= fun ss => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlCaps p.bv ssl none none + ss.tcNewCroot ss.tcNewVroot sp.tcNewBuffer)).run σ := by + unfold Fl4Spec.decodeTcbConfigure + rw [whenE_neg (by simp), retOk_bind, whenE_neg (by simp), retOk_bind] + simp only [List.map_cons, List.take_succ_cons, List.take_zero, List.getElem?_cons_succ, + List.getElem?_cons_zero, the_some, retOk_bind] + rfl + obtain ⟨rb, hrb, hbcase⟩ := buf_rel_vt Z.th s σ Z.G hDS y2 e2 he21 ⟨ps2, hps2, hps21⟩ (hnc _ (by simp) e2.2 ⟨ps2, hps21, Or.inl hps2⟩ rfl) + rw [hrb] + rcases hbcase with ⟨bc0, bfr, rfl, hbs, hbok, hbfd, hbvk⟩ | ⟨er, er', rfl, hbs, herr⟩ + swap + · simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, bind_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨er', ?_, herr⟩, hJ0⟩) + apply run_liftME_err + rw [hspec] + exact run_bind_error ((ipc_spec σ p.bv ssl y2.bv [] e2 []).trans (run_bind_error hbs _)) _ + simp only [bind_ok] + have hip : (Fl4Spec.decodeSetIpcBuffer (Z := Unit) [y2.bv] (.threadCap p.bv) ssl [e2]).run σ = + ({(.ok (Fl4Spec.TcbInvocation.threadControlCaps p.bv ssl none none none none (some (y2.bv, frameOf bfr))), + σ)}, False) := + (ipc_spec σ p.bv ssl y2.bv [] e2 []).trans ((run_bind_ok hbs _).trans rfl) + rw [run_bind_ok hip] at hspec + obtain ⟨r, hr, hcase⟩ := roots_rel_vt Z.th hP s σ Z.G hDS p hp y0 y1 e0 e1 he01 ⟨ps0, hps0, hps01⟩ he11 + ⟨ps1, hps1, hps11⟩ (hnc _ (by simp) e0.2 ⟨ps0, hps01, Or.inl hps0⟩ rfl) (hnc _ (by simp) e1.2 ⟨ps1, hps11, Or.inl hps1⟩ rfl) ssl + rw [hr] + rcases hcase with ⟨cc, vc, rfl, hcv, hokc, hokv, hvkc, hvkv⟩ | ⟨e, e', rfl, hcv, herr⟩ + · simp only [lift, bind_ok] + rw [run_bind_ok hcv] at hspec + obtain ⟨s1, σ1, hrc, hst1, hDS1, hW1, hS1⟩ := J.restartWV arI hni hct (hR ct hct) + rw [hrc]; simp only [bind_ok] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeTcbConfigure (Z := Unit) ((y0 :: y1 :: y2 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + [e0, e1, e2])).run σ = + ({(.ok (tcInv p ssl (tcb.UPDATE_SPACE ||| tcb.UPDATE_IPC_BUFFER) (.Null, (0, [])) (.Null, (0, [])) + (cc, e0.2) (vc, e1.2) y2.bv bfr), σ)}, False) := by + apply run_liftME_ok + rw [hspec] + simp only [tcInv, optArg, flags_SB, ↓reduceIte] + rfl + have hT' := thread_control_capsVE arI (hHp.ins (L.ksb s1)) s1 ar σ1 Z.G hDS1 hW1 p hp ssl hAs + (tcb.UPDATE_SPACE ||| tcb.UPDATE_IPC_BUFFER) (.Null, (0, [])) (.Null, (0, [])) (cc, e0.2) (vc, e1.2) + (fun h => (Eq.mp flags_SB.1 h).elim) (fun h => (Eq.mp flags_SB.2.1 h).elim) + (fun _ => hokc.of_same hS1) (fun _ => hokv.of_same hS1) (fun _ h => absurd rfl h) + (fun h => (Eq.mp flags_SB.1 h).elim) (fun h => (Eq.mp flags_SB.2.1 h).elim) (fun _ => Or.inl hvkc) (fun _ => Or.inl hvkv) y2 y2.bv + (fun _ => (hHp.ins (L.ksb s1)).buf p hp y2) bc0 bfr (fun c sl h => (hbok c sl h).of_same hS1) hbfd + (fun _ _ _ => hfnz.of_same hS1) (fun c sl h => Or.inl (hbvk c sl h)) + simp only [cslot_zero] at hT' + exact WP.spec_mono hT' (fun res h => top_of_instVE arI (hHp.ins _) (DecOut.of_eq hdec) hst1 h) + · simp only [core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok, bind_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨e', ?_, herr⟩, hJ0⟩) + apply run_liftME_err + rw [hspec] + exact run_bind_error hcv _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3TcbSsp.lean b/proofs/Fl4Proofs/P3/VmEntryS3TcbSsp.lean new file mode 100644 index 000000000..533912100 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3TcbSsp.lean @@ -0,0 +1,1227 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels, SetSchedParams): **route D (the fault-handler install, slot 3) then route F +(`set_mcp`, `set_priority`, the SC bind / unbind) at the VM record**. + +`JAtZB.bindTcbF` / `scCtxCapB` (`JAtZB` siblings of XTopJointTcbSsp's `bind_tcb` and XTopJointFinSc's `scCtxCap`), +`installTcbCapVF_ep_eq` (over a null or endpoint cap the VM-aware install is the native one), `install_fhVE`, +`install_fhOkVE`, `ssp_tailVE`, `tcb_set_sched_paramsVE`. Generated by `handoff/tools/gen_ssp.py`. + +No premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.VmEntryS3TcbInst +import Fl4Proofs.P3.XTopJointTcbSsp +import Fl4Proofs.P3.XTopJointFinSc +import Fl4Proofs.P3.XTopJointFinThreadSc +import Fl4Proofs.P3.JAtZBThrPrio +import Fl4Proofs.P3.JAtZBThrSched +import Fl4Proofs.P3.JAtZBRestartN +import Fl4Proofs.P3.VirtF13 +import Fl4Proofs.P3.VirtFrameBatchConservative +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +open Fl4Spec SInv SInvU VirtR7b VirtR7d VirtF13 + +private theorem recDel_step_ssp (c : RecDelCall) (s : Fl4Spec.State Unit) : + (recDel (Z := Unit) c).run s = (recDelF recDel c).run s := + le_antisymm (ik_lfp_le recDelF _ (recDelF_mono _ _ (lfp_prefixed recDelF recDelF_mono)) c s) + (lfp_prefixed recDelF recDelF_mono c s) + +/-- **The VM-aware `cap_delete` of a slot holding the null cap or an endpoint cap is the native one** (the finalisers +agree on endpoints, and an endpoint's finalisation leaves no cleanup). -/ +theorem capDeleteVF_ep_eq {sl : CslotPtr} {s : Fl4Spec.State Unit} {c : Cap} + (hg : getCap (Z := Unit) sl s = ({(c, s)}, False)) (hc : c = .nullCap ∨ ∃ e b R, c = .endpointCap e b R) : + (capDeleteVF (Z := Unit) sl).run s = (capDelete (Z := Unit) sl).run s := by + have hslot : (recDel (Z := Unit) (.finaliseSlotCall sl true)).run s = + (recDelVF (.finaliseSlotCall sl true)).run s := by + rw [recDel_step_ssp, recDelVF_step] + unfold recDelF recDelFVF + refine nde_bind_congr rfl fun cap t hh => ?_ + have h2 := mem_run_liftE' hh + rw [hg] at h2 + obtain ⟨e1, e2⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 h2) + subst cap t + rcases hc with hn | ⟨e, b, R, he⟩ + · subst hn; simp only [↓reduceIte] + subst he + have hnn : Cap.endpointCap e b R ≠ .nullCap := fun h => by cases h + simp only [if_neg hnn] + refine nde_bind_congr rfl fun fin u _ => ?_ + refine nde_bind_congr rfl fun pair v hv => ?_ + have hr : pair = (.nullCap, .nullCap) := by + obtain ⟨_, _, h⟩ := mem_run_liftE' hv + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).1 + subst pair + simp only [capRemoveable, ↓reduceIte] + symm + have hd : (recDel (Z := Unit) (.cTEDeleteCall sl true)).run s = (recDelVF (.cTEDeleteCall sl true)).run s := by + rw [recDel_step_ssp, recDelVF_step] + unfold recDelF recDelFVF + exact nde_bind_congr hslot fun _ _ _ => rfl + unfold capDelete capDeleteVF + exact nde_bind_congr hd fun _ _ _ => rfl + +theorem installTcbCapVF_ep_eq (target : ObjRef) (slot : CslotPtr) (n : Nat) (so : Option (Cap × CslotPtr)) + {s : Fl4Spec.State Unit} {c : Cap} (hg : getCap (Z := Unit) (target, tcbCnodeIndex n) s = ({(c, s)}, False)) + (hc : c = .nullCap ∨ ∃ e b R, c = .endpointCap e b R) : + (installTcbCapVF (Z := Unit) target slot n so).run s = (installTcbCap (Z := Unit) target slot n so).run s := by + cases so with + | none => rfl + | some p => + obtain ⟨c', src⟩ := p + unfold installTcbCapVF installTcbCap + exact nde_bind_congr (capDeleteVF_ep_eq hg hc) fun _ _ _ => rfl + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`bind_tcb` at `J`** (module docstring): an SC of `Z.C` bound to no +thread, a thread with no SC (not idle), a blocked thread's SC released (the +decode's check), no switch pending and `CurInv`; the pair's caps in slots +(the invoked thread cap, the SC cap). -/ +theorem JAtZB.bindTcbF (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hAK : ARKeep arI AR) (hTA : TimeAgree arI) + {sc t : U64} (hsC : sc ∈ Z.C) (ht : t ∈ Z.T) {y : Fl4Spec.SchedContext} {n : Nat} + (hk : σ.kheap sc.bv = some (.schedContext y n)) (hy0 : y.scTcb = none) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hxt0 : xt.tcbSchedContext = none) + (hrel : Fl4Spec.ipcQueuedThreadState xt.tcbState = true → Fl4Spec.scReleased σ.curTime y = true) + (hnsw : ∀ u, σ.schedulerAction ≠ .switchThread u) (hC : CurInv σ) + (htid : t.bv ≠ σ.idleThread) + {xs ys sb : U64} (hxS : IsSlot Z.th xs) (hxc : capAt (L.mem s) xs = ok (.Thread t)) + (hyS : IsSlot Z.th ys) (hyc : capAt (L.mem s) ys = ok (.SchedContext sc sb)) (hsb : sb.val < 64) + (hssz : n + Fl4Spec.minSchedContextBits = sb.val) : + schedcontext.bind_tcb st arI s a sc t ⦃ r => ∃ σ', + Fl4Spec.schedContextBindTcb (Z := Unit) sc.bv t.bv σ = ({((), σ')}, False) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZB (L := L) arI AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hsc0 : sc ≠ 0#u64 := J.c0 sc hsC + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hscH : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc y n hsc0 hk).2⟩ + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + -- the thread is not the current one, the SC not the current SC + obtain ⟨xc, hkc, hxcs, -⟩ := hC.tcb + have htc : t.bv ≠ σ.curThread := fun e => by + rw [← e, hkt] at hkc; cases hkc; rw [hxt0] at hxcs; cases hxcs + have hscc : sc.bv ≠ σ.curSc := fun e => by + obtain ⟨y1, n1, hk1, hb1⟩ := J.inv.back _ xc _ hkc hxcs + rw [← e, hk] at hk1; cases hk1; rw [hy0] at hb1; cases hb1 + -- the release check + have hact_of : Fl4Spec.ipcQueuedThreadState xt.tcbState = true → Fl4Spec.scActive y = true := fun h => by + have h1 := hrel h + unfold Fl4Spec.scReleased at h1; simp only [Bool.and_eq_true] at h1; exact h1.1 + have hblk : Fl4Spec.runnable xt.tcbState = false → inClassR xt.tcbState xt.tcbFault → Fl4Spec.scActive y = true → + released σ y := by + intro hr hcl ha + have hq : Fl4Spec.ipcQueuedThreadState xt.tcbState = true := by + rcases hcl.cls with h | ⟨m, h⟩ | ⟨e, d, h⟩ | ⟨e, r, d, h⟩ + · rw [h] at hr; cases hr + all_goals rw [h]; rfl + have h1 := hrel hq + unfold Fl4Spec.scReleased at h1; simp only [Bool.and_eq_true] at h1 + exact ⟨h1.2, J.inv.suff hk ha⟩ + -- FL4's side of the decode's checks + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb t ht + rw [hkt] at hk'; cases hk' + have ht0 : L.mem s (P1.scA t) = 0#u64 := Notif.optRef_none (by rw [← hscw]; exact hxt0) + obtain ⟨y', n', hk2, hx⟩ := hs.scs sc hsC + rw [hk] at hk2; cases hk2 + have hc0 : L.mem s (P1.scTcbA sc) = 0#u64 := by + rw [scTcbA_eq]; exact Notif.optRef_none (by rw [← hx.tcb]; exact hy0) + have hbA : P1.BlockedR (L.mem s (P1.stA t)) → sc ∈ Ac := fun hb => by + have hq := blockedR_queued (hR.at_tcb ht hkt).st hb + have hrm := SchedRel.act_sc hS hsC hk (hact_of hq) + exact (J.blk.2.2.2.1.act _ hscH).2 (fun h => hrm (by rw [h]; rfl)) + have hA0 : AR a σ.archState.x64CurrentCr3 := hX.fin.1.1.2.2.2.2.2.2 + have haff : L.mem s (P1.affA t) = L.mem s (P1.scCoreA sc) := by + rw [hc.aff t ht, scCoreA_eq, hS.core sc hsC] + refine WP.spec_mono (WP.spec_and (bind_tcb_refines s a σ hS hR J.nsc J.inv hsC hsc0 hk hy0 ht hkt hxt0 hblk hTA) + (WP.spec_and (P1.schedcontext.bind_tcb.blkK (Es := ∅) hAT s a sc t J.blk htH hscH hsc0 ht0 hc0 hbA haff) + (AK.schedcontext_bind_tcb (P1.LawfulStore.total (st := st)) hAT hAK hA0))) ?_ + rintro ⟨s', a'⟩ ⟨⟨σ', he, hS', hR', hN', hI', -, hwk⟩, ⟨hblk', hfD⟩, hA'⟩ + have hDbg := J.kw_dbg (P1.Foot.mono hfD (Set.empty_subset _)) + have hKd := J.kw_dbgKeep (P1.Foot.mono hfD (Set.empty_subset _)) + have hm : ((), σ') ∈ (Fl4Spec.schedContextBindTcb (Z := Unit) sc.bv t.bv σ).1 := by + rw [he]; exact Set.mem_singleton _ + have hWk : P1.WritesIn st s s' (KFoot st Z.T Z.N Z.C Z.c s σ) := hwk + have hW : P1.WritesIn st s s' (TFoot st Z.T Z.N Z.C Z.c s σ) := Sched.WritesIn.tfoot_of_kfoot hWk + have hK : ScTKw (fun _ => False) σ σ' := nkt_schedContextBindTcb sc.bv t.bv σ ((), σ') hm + have hX' : XTopAt (L := L) AR Z s' a' σ' := + xtop_sc_step (ct := σ.curThread) hX J.ip J.nk hW (hK.mono fun _ h => h.elim) hS' hR' (fun _ => hA') + have hall := ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree + ⟨⟨hS', hR', hN', hI'⟩, hWk, (pres_schedContextBindTcb (true, true) _ _).of_eq he⟩ + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := hall + -- the mid-entry facts + have hMid : MidK σ σ' := mka_schedContextBindTcb sc.bv t.bv σ ⟨hscc, htc, htid⟩ _ hm + have hcT : ∃ u ∈ Z.T, u.bv = σ.curThread := ⟨_, hc.curT, hs.curThread.symm⟩ + obtain ⟨hC', hOw', hO', hAI'⟩ := hMid.midN (T := Z.T) hnsw hC J.off J.act J.inv hcT + have hF : FK σ σ' := ⟨hMid.csc, hMid.idle, hMid.isc⟩ + have hcr' : CurRunM σ' := hMid.curRunM hnsw hC J.curRun + have hB' : BlockedScActive σ' := by + refine J.bsa.bk (bka_schedContextBindTcb sc.bv t.bv σ (fun y2 n2 hy2 => ?_) _ hm) + ((pres_schedContextBindTcb (true, true) _ _).of_eq he) + rw [hk] at hy2; cases hy2 + by_cases hq : Fl4Spec.ipcQueuedThreadState xt.tcbState = true + · exact Or.inl (hact_of hq) + · exact Or.inr (fun x hx => by rw [hkt] at hx; cases hx; simpa using hq) + -- the dead set: links cut only at the pair, which is covered by its caps + have hL : LK (fun p => p = sc.bv ∨ p = t.bv) σ σ' := lkm_schedContextBindTcb sc.bv t.bv σ _ hm + have hH := J.kern.heap + have covT : (∀ k, Z.th t = some k → Sched.Covered (L.mem s') Z.G t.val (t.val + max (objSize k) 1)) ∧ + (∀ k, H t = some k → Sched.Covered (L.mem s') Z.G t.val (t.val + 2 ^ k.bits)) := by + have hHt : H t = some .tcb := J.zH.1 t ht + have hfit := hH.fits t _ hHt + simp only [P1.HKind.bits] at hfit + exact J.cover_cap hW hxS hxc (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl (range_thread t hfit) + fun k hk' => by + rw [hHt] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + have covS : (∀ k, Z.th sc = some k → Sched.Covered (L.mem s') Z.G sc.val (sc.val + max (objSize k) 1)) ∧ + (∀ k, H sc = some k → Sched.Covered (L.mem s') Z.G sc.val (sc.val + 2 ^ k.bits)) := by + have hHs := (hJ.sc sc y n hsc0 hk).2 + have hfit := hH.fits sc _ hHs + simp only [P1.HKind.bits] at hfit + rw [hssz] at hfit + refine J.cover_cap hW hyS hyc (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_sc sc sb hsb hfit) fun k hk' => ?_ + rw [hHs] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + have hDead : ∀ q ∈ {q : U64 | q ∈ D ∧ ¬ (q.bv = sc.bv ∨ q.bv = t.bv)}, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_step hX' hW hK (fun _ _ h => h.elim) hL hF hAI'.1 hY' + ((((pres_schedContextBindTcb (true, true) _ _).of_eq he).ykeep rfl).dk q.bv) hq.2 (J.jcov.dead q hq.1) + have hCov : ∀ q ∈ D, q ∉ {q : U64 | q ∈ D ∧ ¬ (q.bv = sc.bv ∨ q.bv = t.bv)} → + (∀ k, Z.th q = some k → Sched.Covered (L.mem s') Z.G q.val (q.val + max (objSize k) 1)) ∧ + (∀ k, H q = some k → Sched.Covered (L.mem s') Z.G q.val (q.val + 2 ^ k.bits)) := fun q hq hn => by + have hw : q.bv = sc.bv ∨ q.bv = t.bv := by_contra fun h => hn ⟨hq, h⟩ + rcases hw with e | e + · rw [bv_inj e]; exact covS + · rw [bv_inj e]; exact covT + have hIK := J.idleW_keep hS' hF.2.1 hF.2.2 + have hsub : {q : U64 | q ∈ D ∧ ¬ (q.bv = sc.bv ∨ q.bv = t.bv)} ⊆ D := fun q h => h.1 + have hjc := J.sc_jcov hW hDead hCov hIK hKd hsub + have hkern := J.sc_kern hW hK hF hX' hDbg hsub + refine ⟨σ', he, Ac, Lst', {q : U64 | q ∈ D ∧ ¬ (q.bv = sc.bv ∨ q.bv = t.bv)}, J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK hIK.2) hjc hkern (J.sc_utc hW hK hDbg) hN' hI' hC' + (Or.inl (hC'.elim hOw' (curOwn_of_curInv hS' hI'))) hO' hAI' hB' hcr' hblk' + hKd hIK.1 (fun p hp => ⟨J.zD p hp, fun e => by + rcases e with e | e + · exact J.not_ztcb hyS (fun _ _ _ h => by rw [capV_of_capAt hyc] at h; cases h) + (by rw [capV_of_capAt hyc, bv_inj e]; simp [absCap, Fl4Spec.objRefs]) hp + · exact J.not_ztcb hxS (fun _ _ _ h => by rw [capV_of_capAt hxc] at h; cases h) + (by rw [capV_of_capAt hxc, bv_inj e]; simp [absCap, Fl4Spec.objRefs]) hp⟩), + NatFoot.ofT hX hW (fun _ h => J.tfoot_nh h) (natK_tk hK)⟩ + +end + +section +open Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **An SC cap in a slot, as SchedControl's decode reads it** (l4v +`valid_cap`, `sym_refs`, `valid_global_refs`), from `JAtZ` (the slot any +slot of the typed heap, e.g. an extra cap's, `ExtraRel`'s `IsSlot`): the +cap's size below 64 (`H`'s SC of the cap's size fits, `JPlaced.cobj`, +`HeapOK.fits`, the pointer non-null), its SC one of `Z.C` (`JAtZ.hscC`), +the spec's of the cap's size (`JPlaced.sc`), whose bound thread is one of +`Z.T` holding it (`scBack_of`), and not the idle SC (`JPlaced.cobj`). -/ +theorem JAtZB.scCtlExtraB {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {x p sb : U64} (hx : IsSlot Z.th x) + (hc : capAt (L.mem s) x = ok (.SchedContext p sb)) : + sb.val < 64 ∧ + (p ∈ Z.C ∧ p ≠ 0#u64 ∧ + ∃ y, σ.kheap p.bv = some (.schedContext y (sb.val - Fl4Spec.minSchedContextBits)) ∧ + ∀ t : U64, y.scTcb = some t.bv → + t ∈ Z.T ∧ ∃ xt, σ.kheap t.bv = some (.tcb xt) ∧ xt.tcbSchedContext = some p.bv) ∧ + p.bv ≠ σ.idleSc := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hO := hJ.cobj _ hx + rw [capV_of_capAt hc] at hO + obtain ⟨hH, hpi0⟩ := hO + have hpi := J.xtop.fin.1.1.1.ne_idleSc hpi0 + have hpC : p ∈ Z.C := J.hscC p _ hH + have hp0 : p ≠ 0#u64 := J.c0 p hpC + obtain ⟨Q, R, hc', hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨y, n, hk, -⟩ := hs.scs p hpC + have hH' := (hJ.sc p y n hp0 hk).2 + rw [hH] at hH' + have hn : sb.val = n + Fl4Spec.minSchedContextBits := HKind.sc.inj (Option.some.inj hH') + have hpfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := by + have := J.kern.heap.fits p _ hH + simpa only [P1.HKind.bits] using this + have hp1 : 1 ≤ p.val := by + rcases Nat.eq_zero_or_pos p.val with h | h + · exact absurd (UScalar.eq_of_val_eq (by rw [h]; rfl)) hp0 + · exact h + have hsb : sb.val < 64 := by + by_contra hge + have : 2 ^ 64 ≤ 2 ^ sb.val := Nat.pow_le_pow_right (by decide) (by omega) + omega + have hpS : p ∈ P1.Heap.scs H := ⟨_, hH⟩ + refine ⟨hsb, ⟨hpC, hp0, y, by rw [hn, Nat.add_sub_cancel]; exact hk, fun t ht => ?_⟩, hpi⟩ + obtain ⟨u, huT, hu, xt, hkt, hxt⟩ := + scBack_of J.xtop.fin.1.1.1 J.blk.2.2.1 J.tcbs_eq.1.symm hpC hpS hp0 hk ht + have e := P1.eq_of_bv hu + subst e + exact ⟨huT, xt, hkt, hxt⟩ + +/-- **The invoked SC cap, as the SchedContext decode reads it** (l4v +`valid_cap`, `sym_refs`, `valid_global_refs`), from `JAtZ` +(`JAtZ.scCtlExtra`; a slot of the typed heap): the SC one of `Z.C`, not the +idle SC, the cap's size the SC's; its `TCB` word, when set, a thread of +`Z.T` whose SC word is the SC (P1's `XInv`: `yt`, `tsc`) and not the idle +thread (the idle thread holds the idle SC, `ActIdle`); its notification one +of `Z.N` (`SchedValid`'s link, `ObjCover`). -/ +theorem JAtZB.scCtxCapB {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {x p sb : U64} (hx : IsSlot Z.th x) + (hc : capAt (L.mem s) x = ok (.SchedContext p sb)) : + p ∈ Z.C ∧ p.bv ≠ σ.idleSc ∧ sb.val < 64 ∧ + (∀ y n, σ.kheap p.bv = some (.schedContext y n) → n + Fl4Spec.minSchedContextBits = sb.val) ∧ + (L.mem s (scW p 24#u64) ≠ 0#u64 → + L.mem s (scW p 24#u64) ∈ Z.T ∧ L.mem s (P1.scA (L.mem s (scW p 24#u64))) = p ∧ + (L.mem s (scW p 24#u64)).bv ≠ σ.idleThread) ∧ + (∀ y n, σ.kheap p.bv = some (.schedContext y n) → ∀ m : U64, y.scNtfn = some m.bv → m ∈ Z.N) := by + obtain ⟨hsb, ⟨hpC, hp0, -⟩, hpi⟩ := J.scCtlExtraB arI hx hc + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hO := hJ.cobj _ hx + rw [capV_of_capAt hc] at hO + have hH := hO.1 + have hpS : p ∈ P1.Heap.scs H := ⟨_, hH⟩ + refine ⟨hpC, hpi, hsb, fun y n hk => ?_, fun h0 => ?_, fun y n hk m hm => ?_⟩ + · have hH' := (hJ.sc p y n hp0 hk).2 + rw [hH] at hH' + exact (HKind.sc.inj (Option.some.inj hH')).symm + · have hX := J.blk.2.2.1 + have h0' : L.mem s (P1.scTcbA p) ≠ 0#u64 := by rw [scTcbA_eq]; exact h0 + have huT : L.mem s (P1.scTcbA p) ∈ Z.T := by + rw [J.tcbs_eq.1]; exact hX.2.2.yt.1 p hpS h0' + have htsc : L.mem s (P1.scA (L.mem s (P1.scTcbA p))) = p := hX.2.2.tsc p hpS hp0 h0' + rw [scTcbA_eq] at huT htsc + refine ⟨huT, htsc, fun e => ?_⟩ + obtain ⟨Q, R, hc', hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨xu, hku, -, -, hsu, -⟩ := hs.tcb _ huT + obtain ⟨xi, hki, hsi, -⟩ := J.act.2 + rw [e, hki] at hku + cases hku + rw [hsu, htsc, Notif.optRef_ne hp0] at hsi + exact hpi (Option.some.inj hsi) + · obtain ⟨xn, hxn⟩ := J.sv.scNtfn _ y n _ hk hm + obtain ⟨m', hm', hmm⟩ := J.obj.ntfn _ xn hxn + rw [← P1.eq_of_bv hmm]; exact hm' + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs : Set U64} {MR : A → Fl4Spec.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- (mmuAD Stage 3, route D) **`install_fhJM` at the VM record** (`install_fhJM` is the `WJPM` instance). -/ +theorem install_fhVE [Fl4Proofs.P1.PlainStore st] {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) + (s0 s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hDS : DState (L := L) Z.th s σ G) + (hW : W s a σ G) + (t : U64) (ht : Z.th t = some .tcb) (n : Nat) (hn : n < tcbSlots) (hn2 : n ≠ 2) (idx : U64) (hidx : idx.val = n) + (hcz0 : InstFr Z.th t (L.mem s0) (L.mem s)) + (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (nc : capability.Cap) (hwf : CapWf nc) (hcv : CapValid Z.th nc) (he : EncWf nc) + (hfw : isFrame nc = true → Fl4Spec.sameObjectAs (absCap nc) (absCap nc)) + {ps : U64} {rs : Nat} (hps : Z.th ps = some (.cnode rs)) {idxs : List Bool} (hls : idxs.length = rs) + {srcC0 : capability.Cap} (hsrc0 : capV (L.mem s0) (cslotAddr (ps.bv, idxs)) = srcC0) + (hsh : shape nc = shape srcC0) (hbd : badgeF nc = badgeF srcC0 ∨ badgeF srcC0 = 0#u64) + (hkey : okey nc = okey srcC0) + (hv : ∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 srcC0 → Vs.VCapOK P L4 L3 L2 L1 nc) + (htu : Vs.TabUn nc → Vs.TabUn srcC0) (hnat : Vs.NativeCap srcC0 → Vs.NativeCap nc) + (hkr0 : Vs.VKey nc ≠ none → nc = srcC0 ∧ ¬ Vs.TabUn nc) + (hep : n = 3 → nc ≠ .Null → ∃ e b x1 x2 x3 x4, nc = .Endpoint e b x1 x2 x3 x4) + (hnoe : P1.NullOrEp H (capV (L.mem s) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) (hn5 : n ≠ 5) + (hvt0 : VtOK srcC0 nc) : + tcb.install_tcb_cap st arI s a t (cslotAddr ssl) idx nc (cslotAddr (ps.bv, idxs)) ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (Fl4Spec.installTcbCapVF t.bv ssl n (some (absCap nc, (ps.bv, idxs)))) s0 σ res ∧ + (res.1 = .Ok () → ∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) → + capV (L.mem res.2.1) y = capV (L.mem s) y) ⦄ := by + obtain ⟨V0, X0, XL0, Vv0, Ac0, Lst0, D0, J0V, -⟩ := hI.wvr _ _ _ _ hW + have J0 := J0V.st.1.1 + have hP : Placed Z.th := J0.xtop.rel.1 + obtain ⟨K, hK, hK6, hDE⟩ := J0.delE + have hAt : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex n) := ⟨t, rfl, Or.inr ⟨ht, n, hn, rfl⟩⟩ + have hAsrc : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hsrcT := cnode_slot_ne_tcb hP hps hls ht + have hczs0 := hcz0.1 _ hAsrc.isSlot hsrcT + have hsh0 : shape nc = shape (capV (L.mem s0) (cslotAddr (ps.bv, idxs))) := by rw [hsrc0]; exact hsh + -- the insert after the delete, at `WJR` + have after : ∀ (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) (G1 : MdbGhost), DState (L := L) Z.th s1 σ1 G1 → + W s1 a1 σ1 G1 → + capV (L.mem s1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Null → + ChgZ (L.mem s0) (L.mem s1) (cslotAddr (ps.bv, idxs)) → + (do + let b ← nc.is_null + if b = true then ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s1, a1) + else do + let b1 ← tcb.still_valid st s1 t (cslotAddr ssl) nc (cslotAddr (ps.bv, idxs)) + if b1 = true then do + let s2 ← cdt.insert st s1 nc (cslotAddr (ps.bv, idxs)) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) + ok (core.result.Result.Ok (), s2, a1) + else ok (core.result.Result.Ok (), s1, a1)) ⦃ res => + ∃ σ' G', (Fl4Spec.NonDetE.unlessE (ε := Unit) (absCap nc = .nullCap) (Fl4Spec.NonDetE.liftE + (Fl4Spec.checkCapAt (absCap nc) (ps.bv, idxs) (Fl4Spec.checkCapAt (.threadCap t.bv) ssl + (Fl4Spec.capInsert (absCap nc) (ps.bv, idxs) (t.bv, Fl4Spec.tcbCnodeIndex n)))))).run σ1 = + ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ DState (L := L) Z.th res.2.1 σ' G' ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) → + capV (L.mem res.2.1) y = capV (L.mem s1) y) ∧ + (capV (L.mem res.2.1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = .Null ∨ + capV (L.mem res.2.1) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) = nc) ∧ + W res.2.1 res.2.2 σ' G' ⦄ := by + intro s1 a1 σ1 G1 hDS1 hW1 hroot hczs + refine WP.spec_mono (WP.spec_and (install_after Z.th hP s0 s1 a1 σ1 G1 hDS1 t ht n hn hroot ssl hAs nc hwf hcv he + hfw hps hls hczs hsrc0 hsh hbd hkey hv) + (install_after_frame hP s1 a1 hDS1.1.2.2.1 hDS1.2.1 t (cslotAddr ssl) nc hAsrc.isSlot hAt.isSlot)) ?_ + rintro res ⟨⟨σ', G', hrun, hr1, ha1, hDS', hcap, hst, htg, hcase⟩, hF⟩ + refine ⟨σ', G', hrun, hr1, hDS', hcap, htg, ?_⟩ + rcases hcase with ⟨e1, e2, e3⟩ | ⟨hins, hthr, hsrc1, hnn, hnut, hdst⟩ + · rw [ha1, e1, e2, e3]; exact hW1 + obtain ⟨V1, X1, XL1, Vv1, Ac1, Lst1, D1, J1V, hkb1⟩ := hI.wvr _ _ _ _ hW1 + have J1 := J1V.st.1.1 + have hns1 := J1V.ns + have hsg1 := J1V.sg + have hp1 := J1V.pp + have hsg' : SpecGlob σ' := by + obtain ⟨c0, a0, hc0, hca0, hg0⟩ := hDS1.1.1.abs_cap hAsrc + have e0 : c0 = srcC0 := by rw [← hsrc1, capV_of_capAt hc0] + have ha0 := (getCap_set_iff σ1 _ _ _).1 (congrArg Prod.fst hg0) + rw [hca0.abs, e0] at ha0 + exact hsg1.insTcb hins hnut hsh ha0 + have hwS := SlotFrame2.writesIn hF + obtain ⟨Ac2, Lst2, D2, J2⟩ := J1V.st.insTcbV (fun e => absurd e hn2) ht hn hn5 hins hDS' hst hwS hroot hcap + (by rw [hdst]; exact hnut) (by rw [hdst]; exact hnn) ⟨_, hAsrc.isSlot, by rw [hdst, hsrc1]; exact hsh⟩ + (fun h3 => by rw [hdst]; exact Or.inr (hep h3 hnn)) (fun e => absurd e hn2) ⟨_, hAs.isSlot, hthr⟩ + (fun P L4 L3 L2 L1 h => by + rw [hdst]; exact h.copy hAsrc.isSlot (by rw [hsrc1]; exact hv) (by rw [hsrc1]; exact htu)) + (fun U h => by rw [hdst]; exact (h _ hAsrc.isSlot).of_okey (by rw [hsrc1]; exact hkey)) + (fun h => by rw [hdst]; exact hnat (by rw [← hsrc1]; exact h _ hAsrc.isSlot)) + (fun R h => by rw [hdst]; exact (h _ hAsrc.isSlot).of_okey (by rw [hsrc1]; exact hkey)) + (fun hk => by + rw [hdst] at hk ⊢ + obtain ⟨e, hnt⟩ := hkr0 hk + exact ⟨⟨_, hAsrc.isSlot, hsrcT n hn, by rw [e, hsrc1]⟩, hnt⟩) hAsrc.isSlot (by rw [hdst, hsrc1]; exact hvt0) + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) (absCap nc) (ps.bv, idxs) + (t.bv, Fl4Spec.tcbCnodeIndex n) σ1).1 := by rw [hins]; rfl + have hcn := capInsert_nkn _ _ _ σ1 _ hmem + have hact : σ'.schedulerAction = σ1.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hcn.1; exact e + rw [ha1] + exact hI.ins s1 res.2.1 a1 σ1 σ' G1 G' _ _ _ hW1 + ⟨V1, X1, XL1, Vv1, Ac2, Lst2, D2, ⟨J2, fun u e => hns1 u (hact ▸ e), hsg', hp1.of_ppk (PPK.of_cnK hcn)⟩, + by rw [hwS.1]; exact hkb1⟩ hins + unfold tcb.install_tcb_cap + rw [spec_eq_ok (tcb_slot_spec t idx), tcb_root_eq t idx hn hidx] + simp only [bind_ok] + obtain ⟨c0, a0, hc0, hca0, hg0⟩ := hDS.1.1.abs_cap hAt + by_cases hc0n : c0 = .Null + · subst hc0n + rw [delete_null_eq arI s a _ hc0 σ] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry.branch, uncurry_apply_pair] + refine WP.spec_mono (after s a σ G hDS hW (by rw [capV_of_capAt hc0]) hczs0) ?_ + rintro res ⟨σ', G', hrun, hr1, hDS', hcap, htg, hW'⟩ + have hg0' : Fl4Spec.getCap (Z := Unit) (t.bv, Fl4Spec.tcbCnodeIndex n) σ = ({(.nullCap, σ)}, False) := by + rw [hg0, hca0.abs]; rfl + exact ⟨⟨σ', G', Or.inl ⟨hr1, mem_bindE_ok (capDeleteVF_null_mem hg0') (mem_of_run hrun), + hcz0.install hP ht hn (fun y hy hne => ChgZ.of_eq (hcap y hy hne)) htg hAsrc.isSlot hsh0 hep, hW'⟩, hDS'⟩, + fun _ y hy hne => hcap y hy hne⟩ + · have hcapNE : obj.Slot.cap st (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)) s ⦃ c => P1.NullOrEp H c ⦄ := by + rw [slot_cap_eq, capAt_capV]; exact (WP.spec_ok _).2 hnoe + apply WP.spec_bind (WP.spec_and (hI.del K hK hK6 _ hAt s a σ G hDS hW (by rw [capV_of_capAt hc0]; exact hc0n) (hDE.2.1 G hDS.1.2.2.1)) + (P1.cdt.delete.ep (ar := arI) s a _ J0.blk hcapNE + (P := fun s2 => ∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) → + capV (L.mem s2) y = capV (L.mem s) y) + (fun s1 a1 hag => WP.spec_mono (empty_capV_frame (arI := arI) hP (J0.slotCapW (tLowSlots_all _)) + hDS.1.2.2.1 hAt.isSlot s1 a1 hag) fun r h => h))) + rintro ⟨r, s1, a1⟩ ⟨⟨σ1, G1, hDS1, -, -, hmD, hcase, hW1⟩, -, -, -, hfr⟩ + simp only [uncurry_apply_pair] at hcase hDS1 hmD hW1 ⊢ + rcases hcase with ⟨rfl, hmem, -, hnull, hcz⟩ | ⟨rfl, hmem⟩ + · simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok] + refine WP.spec_mono (after s1 a1 σ1 G1 hDS1 hW1 hnull (hczs0.trans (hcz _ hAsrc.isSlot (hsrcT n hn)))) ?_ + rintro res ⟨σ', G', hrun, hr1, hDS', hcap, htg, hW'⟩ + exact ⟨⟨σ', G', Or.inl ⟨hr1, mem_bindE_ok hmem (mem_of_run hrun), + hcz0.install hP ht hn (fun y hy hne => (hcz y hy hne).trans (ChgZ.of_eq (hcap y hy hne))) htg + hAsrc.isSlot hsh0 hep, hW'⟩, hDS'⟩, fun _ y hy hne => (hcap y hy hne).trans (hfr rfl y hy 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, Or.inr ⟨rfl, mem_bindE_err hmem, hW1⟩, hDS1⟩, fun h => by cases h⟩ + +/-- (mmuAD Stage 3, route D) **`install_fhOkJM` at the VM record** (`install_fhOkJM` is the `WJPM` instance). -/ +theorem install_fhOkVE [Fl4Proofs.P1.PlainStore st] {kb : U64} {W Wp : S → A → Fl4Spec.State Unit → MdbGhost → Prop} + (hI : InsWV (L := L) arI AR MR Z H Cs kb W Wp) + (s0 s : S) (a : A) (σ : Fl4Spec.State Unit) (G : MdbGhost) (hDS : DState (L := L) Z.th s σ G) + (hW : W s a σ G) + (t : U64) (ht : Z.th t = some .tcb) (n : Nat) (hn : n < tcbSlots) (hn2 : n ≠ 2) (idx : U64) (hidx : idx.val = n) + (hcz0 : InstFr Z.th t (L.mem s0) (L.mem s)) + (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (nc : capability.Cap) (sl : Fl4Spec.CslotPtr) (hok : NewCapOK Z.th (L.mem s0) nc sl) + (hep : n = 3 → nc ≠ .Null → ∃ e b x1 x2 x3 x4, nc = .Endpoint e b x1 x2 x3 x4) + (hnoe : P1.NullOrEp H (capV (L.mem s) (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n)))) (hn5 : n ≠ 5) + (hvt : VtOK (capV (L.mem s0) (cslotAddr sl)) nc) : + tcb.install_tcb_cap st arI s a t (cslotAddr ssl) idx nc (cslotAddr sl) ⦃ res => + InstPostWP (L := L) W Wp Z.th t + (Fl4Spec.installTcbCapVF t.bv ssl n (some (absCap nc, sl))) s0 σ res ∧ + (res.1 = .Ok () → ∀ y, IsSlot Z.th y → y ≠ cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex n) → + capV (L.mem res.2.1) y = capV (L.mem s) y) ⦄ := by + obtain ⟨ps, rs, hps, h1s, hls, hwf, hcv, he, hfw, hsh, hbd, hkey, hvk, htu, hnat, hkr0⟩ := hok + obtain ⟨sa, idxs⟩ := sl + simp only at h1s hls; subst h1s + exact install_fhVE arI hI s0 s a σ G hDS hW t ht n hn hn2 idx hidx hcz0 ssl hAs nc hwf hcv he hfw hps hls rfl + hsh hbd hkey hvk htu hnat hkr0 hep hnoe hn5 hvt + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {MR : A → Fl4Spec.MachineState → Prop} + +set_option maxHeartbeats 32000000 in +/-- **SetSchedParams after its checks at `J`** (as XTopTcbSched's `ssp_tailX`): +the caller's restart (`JAtZ.restartWJR`), the fault handler's install at the +deletion loop's invariant (`install_fhOkJM`, `JAtPre` when preempted; the +other slots' caps kept), then `set_mcp` (`JAtZ.mcpWrJ`), `set_priority` +(`JAtZ.setPrioJS`, the current thread's facts by `JAtZ.prioCur` from +`CurInv`) and the SC step: none, `unbind_tcb` (`JAtZ.unbindTcbJ`) or +`bind_tcb` (`JAtZ.bindTcbJ`, the decode's checks carried: `SpecPost` by +`specPost_restart`, `specPost_install`, `BindOK.after`; the SC and the +release check by `install_sched`). -/ +theorem ssp_tailVE [Fl4Proofs.P1.PlainStore st] {W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} (on rc : Fl4Spec.ObjRef → Bool) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) {V X : Set U64} {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hsg : SpecGlob σ) (hP0 : PPR0 σ) + (hHp : TcbInsWV (L := L) arI AR MR Z H Cs W Wp) (hR : RestartWJ (st := st) Z s ar σ (W (L.ksb s))) + (hAT : P0.ArchTotal arI) (hAK : ARKeep arI AR) (hTA : TimeAgree arI) + (p : U64) (hp : Z.th p = some .tcb) (ct : U64) (hct : sched.cur_thread st s = ok ct) + (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) (hpsl : capV (L.mem s) (cslotAddr ssl) = .Thread p) + (x2 : U64) (e2 : Fl4Spec.Cap × Fl4Spec.CslotPtr) + (hxe2 : x2 = cslotAddr e2.2) (he21 : e2.1 = absCap (capV (L.mem s) x2)) (ps : U64) + (hps : Z.th ps = some (.cnode e2.2.2.length)) (hps1 : e2.2.1 = ps.bv) + (hv : Fl4Spec.validFaultHandler (absCap (capV (L.mem s) x2))) + (y0 y1 scv : U64) (hy0 : y0.val < 256) (hy1 : y1.val < 256) (scf : Bool) (q : Fl4Spec.ObjRef) + (sco : Option (Option Fl4Spec.ObjRef)) (h1 : sco = none ↔ scf = false) (h2 : sco = some none → scv = 0#u64) + (h3 : ∀ v, sco = some (some v) → v = scv.bv ∧ scv ≠ 0#u64) + (hD : ScDec σ p.bv sco) (hpI : p ≠ L.idleW s) + (hscs : scv ≠ 0#u64 → ∃ xs sb, IsSlot Z.th xs ∧ capV (L.mem s) xs = .SchedContext scv sb) + (m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4Invocation) + (hdec : DecOut m σ (.invokeTCB (.threadControlSched p.bv ssl (some e2) (some (Fl4Spec.ucast 8 y0.bv, q)) + (some (Fl4Spec.ucast 8 y1.bv, q)) sco))) + (blk call don : Bool) : + (do + let s1 ← tcb.restart_caller st s + tcb.thread_control_sched st arI s1 ar p (cslotAddr ssl) (capV (L.mem s) x2, x2) y0 y1 scv + (if scf then (tcb.SCHED_UPDATE_MCP ||| tcb.SCHED_UPDATE_PRIO ||| tcb.SCHED_UPDATE_FAULT ||| tcb.SCHED_UPDATE_SC) + else (tcb.SCHED_UPDATE_MCP ||| tcb.SCHED_UPDATE_PRIO ||| tcb.SCHED_UPDATE_FAULT))) ⦃ res => + TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don m s res ⦄ := by + have hpT : p ∈ Z.T := J.1.1.thT p hp + obtain ⟨hcur, xc, hkc, hrunc⟩ := J.1.1.curRunnable hni hct + obtain ⟨s1, σ1, hrc, hst1, hDS1, hW1, hS1⟩ := J.restartWV arI hni hct (hR ct hct) + obtain ⟨s1', σ1', xc', hrc', hst1', hkc', -, e1, -, hid1, hct1, -⟩ := J.1.1.restartCallerF hni hct + have es : s1' = s1 := Result.ok_injective (hrc'.symm.trans hrc) + have eσ : σ1' = σ1 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hst1'.symm.trans hst1)))).2 + subst es eσ + rw [hkc] at hkc'; cases hkc' + have hst1c : Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = ({((), σ1')}, False) := by + rw [← hcur]; exact hst1 + have hC0 := J.1.1.curInv (by rw [hni.2]; intro h; cases h) + have hC1 : Sched.CurInv σ1' := by + have h := hC0.restart (xt := xc) (by rw [hcur]; exact hkc) + rw [hcur] at h; rw [e1]; exact h + have htm1 : σ1'.curTime = σ.curTime := by rw [e1]; unfold Notif.stsAct; split_ifs <;> rfl + -- the decode's checks of the target, after the restart + obtain ⟨xp, hkp, hDU, hDB⟩ := hD + obtain ⟨xt0, hkt0, hxs0, -⟩ := hC0.tcb + have hpc : ∀ v, sco = some (some v) → p.bv ≠ σ.curThread := fun v e hpe => by + rw [hpe, hkt0] at hkp; cases hkp; rw [(hDB v e).1] at hxs0; cases hxs0 + have hBI : BlkIn σ p.bv xp.tcbState := fun e he => by + rcases epBlocked_some he with ⟨d, hst⟩ | ⟨ro, d, hst⟩ + · exact J.1.1.sv.sendQ _ _ e d hkp hst + · exact J.1.1.sv.recvQ _ _ e ro d hkp hst + have hpost1 := specPost_restart J.1.1.inv hkc hrunc hst1c ⟨xp, hkp, hDU, hDB⟩ + (fun x hx => by rw [hkp] at hx; cases hx; exact ⟨blkQ_of_sv J.1.1.sv hkp, hBI⟩) hpc + -- the old fault handler at the restart: null or an endpoint cap + obtain ⟨V1, X1, XL1, Vv1, Ac1, Lst1, D1, J1V, -⟩ := (hHp.ins _).wvr _ _ _ _ hW1 + have J1 := J1V.st.1.1 + obtain ⟨Tr1, Cq1, hJ1⟩ := J1.jpl + have hnoe : P1.NullOrEp H (capV (L.mem s1') (cslotAddr (p.bv, Fl4Spec.tcbCnodeIndex 3))) := hJ1.fhSlot p hp + obtain ⟨xp1, hkp1, -⟩ := id hpost1 + obtain ⟨c, a', hca, hCR, hg⟩ := hDS1.1.1.abs_cap (sl := (p.bv, Fl4Spec.tcbCnodeIndex 3)) + ⟨p, rfl, Or.inr ⟨hp, 3, by decide, rfl⟩⟩ + rw [getCap_tcb_three hkp1] at hg + have ha' : xp1.tcbFaultHandler = a' := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hg))).1 + have hfc : xp1.tcbFaultHandler = absCap c := by rw [ha', hCR.abs] + have hcv : capV (L.mem s1') (cslotAddr (p.bv, Fl4Spec.tcbCnodeIndex 3)) = c := capV_of_capAt hca + have hcNE : c = .Null ∨ ∃ ep b0 b1 b2 b3 b4, c = .Endpoint ep b0 b1 b2 b3 b4 := by + rw [← hcv] + rcases hnoe with h | ⟨ep, b0, b1, b2, b3, b4, h, -⟩ + · exact Or.inl h + · exact Or.inr ⟨ep, b0, b1, b2, b3, b4, h⟩ + have hfcS : xp1.tcbFaultHandler = .nullCap ∨ ∃ e b R, xp1.tcbFaultHandler = .endpointCap e b R := by + rw [hfc] + rcases hcNE with h | ⟨ep, b0, b1, b2, b3, b4, h⟩ + · exact Or.inl (by rw [h]; rfl) + · exact Or.inr ⟨_, _, _, by rw [h]; rfl⟩ + -- the install + rw [hrc]; simp only [bind_ok]; rw [tcs_fault_eq] + have hxs : IsSlot Z.th x2 := by rw [hxe2]; exact AbsSlot.isSlot ⟨ps, hps1, Or.inl hps⟩ + have hc1 : capV (L.mem s1') x2 = capV (L.mem s) x2 := hS1.capV hxs + have hnf := vfh_not_frame hv + have hok : NewCapOK Z.th (L.mem s1') (capV (L.mem s) x2) e2.2 := by + rw [← hc1, hxe2]; exact newCapOK_self hDS1.1.1 hDS1.1.2.1 hps hps1 (by rw [← hxe2, hc1]; exact hv) + have hI := install_fhOkVE arI (hHp.ins (L.ksb s1')) s1' s1' ar σ1' Z.G hDS1 hW1 p hp 3 (by simp [tcbSlots]) (by decide) + layout.tcb.SLOT_FAULT_HANDLER (by simp [layout.tcb.SLOT_FAULT_HANDLER]) (InstFr.refl _ _ _) ssl hAs + (capV (L.mem s) x2) e2.2 hok (fun _ => vfh_ep hv) hnoe (by decide) + (by rw [← hxe2, hc1]; exact vtOK_vfh hv) + rw [← hxe2] at hI + obtain ⟨⟨r, s2, a1⟩, hins, ⟨σ2, G2, hc, hDS2⟩, hfr⟩ := spec_exists hI + have he2 : e2 = (absCap (capV (L.mem s) x2), e2.2) := by rw [← he21] + rw [hins] + simp only [bind_ok, uncurry_apply_pair] + rcases hc with ⟨hr, hx, -, hW2⟩ | ⟨hr, hx, hWp⟩ + swap + · -- preempted: `JAtPre` + simp only at hr; subst hr + rw [← he2] at hx + refine (WP.spec_ok _).2 (Or.inr ⟨.base (.invokeTCB (.threadControlSched p.bv ssl (some e2) + (some (Fl4Spec.ucast 8 y0.bv, q)) (some (Fl4Spec.ucast 8 y1.bv, q)) sco)), DecOut.liftME hdec, σ1', hst1, + (.error (), σ2), ?_, Or.inr ⟨rfl, rfl, (hHp.ins _).pre _ _ _ _ hWp⟩⟩) + show _ ∈ ((Fl4Spec.invokeTcbVF (Z := Unit) (.threadControlSched p.bv ssl (some e2) + (some (Fl4Spec.ucast 8 y0.bv, q)) (some (Fl4Spec.ucast 8 y1.bv, q)) sco)).run σ1').1 + simp only [Fl4Spec.invokeTcbVF] + exact mem_bindE_err hx + simp only at hr; subst hr + rw [← he2] at hx + obtain ⟨V2, X2, XL2, Vv2, Ac2, Lst2, D2, J2V, -⟩ := (hHp.ins _).wvr _ _ _ _ hW2 + have J2 := J2V.st.1.1 + have hns2 := J2V.ns + -- the VM-aware delete of a null or endpoint cap is the native one + have hxN : (.ok (), σ2) ∈ ((Fl4Spec.installTcbCap (Z := Unit) p.bv ssl 3 (some e2)).run σ1').1 := by + have hg3 : Fl4Spec.getCap (Z := Unit) (p.bv, Fl4Spec.tcbCnodeIndex 3) σ1' = ({(xp1.tcbFaultHandler, σ1')}, False) := by + rw [getCap_tcb_three hkp1] + rw [← installTcbCapVF_ep_eq p.bv ssl 3 (some e2) hg3 hfcS] + exact hx + -- the spec after the install + have hpost2 := specPost_install hkp1 hfc hcNE hxN hpost1 + obtain ⟨hCI, htm2, hid2, hscK, hstK⟩ := install_sched σ1' σ2 p.bv xp1 ssl e2 hkp1 hfcS J1.inv hxN + have hC2 : Sched.CurInv σ2 := hCI hC1 + have hct2 : σ2.curThread = σ1'.curThread := Vs.DK.ct (Vs.dke_installTcbCap' p.bv ssl 3 (some e2)) hxN + obtain ⟨x, hk2, -, -, -, hU2, hB2⟩ := hpost2 + -- the slots after the install: those holding neither the null cap nor an endpoint cap kept + have hroot1 : ∀ y, IsSlot Z.th y → capV (L.mem s) y ≠ .Null → + (∀ e b0 b1 b2 b3 b4, capV (L.mem s) y ≠ .Endpoint e b0 b1 b2 b3 b4) → + capV (L.mem s2) y = capV (L.mem s) y := fun y hy hn he => by + have hne : y ≠ cslotAddr (p.bv, Fl4Spec.tcbCnodeIndex 3) := fun e => by + rw [← hS1.capV hy, e] at hn he + rcases hnoe with h | ⟨ep, b0, b1, b2, b3, b4, h, -⟩ + · exact hn h + · exact he _ _ _ _ _ _ h + rw [hfr rfl y hy hne, hS1.capV hy] + -- `set_mcp` + unfold schedTail + rw [set_mcp_eq, bind_ok] + obtain ⟨V3, J3, F3⟩ := J2.mcpWrF hpT hk2 y0 (Fl4Spec.ucast 8 y0.bv) + (by rw [Ipc.ucast8_specPrio, Sched.specPrio_toNat hy0]) + set σa := Notif.updK σ2 p.bv (.tcb { x with tcbMcpriority := Fl4Spec.ucast 8 y0.bv }) with hσa + have hSS3 := F3.ss + have st3 := J2V.st.ofNat J3 F3 + have ha : ((), σa) ∈ (Fl4Spec.setMcpriority (Z := Unit) p.bv (Fl4Spec.ucast 8 y0.bv) σ2).1 := by + show _ ∈ (Fl4Spec.threadSet (Z := Unit) _ p.bv σ2).1 + rw [Notif.threadSet_eq _ hk2]; exact Set.mem_singleton _ + have hCa : Sched.CurInv σa := (QKM.mcp hk2 _).curInv hC2 + have hneA : σa.curThread ≠ σa.idleThread := by + show σ2.curThread ≠ σ2.idleThread + rw [hct2, hid2, hct1, hid1]; exact hni.1 + obtain ⟨hc3, hp3⟩ := J3.prioCur hneA hCa hpT + -- `set_priority` + refine WP.spec_bind (J3.setPrioF (fun u => hns2 u) hpT y1 hy1 hc3 hp3) ?_ + rintro s4 ⟨σb, hmb, V4, J4, F4⟩ + have hSS4 := F4.ss + have st4 := st3.ofNat J4 F4 + have hb : ((), σb) ∈ (Fl4Spec.setPriority (Z := Unit) p.bv (Fl4Spec.ucast 8 y1.bv) σa).1 := by + rw [hmb]; exact Set.mem_singleton _ + have hb0 : ((), σb) ∈ (Fl4Spec.setPriority (Z := Unit) p.bv (Fl4Spec.ucast 8 y1.bv) σa).1 := by + rw [hmb]; exact Set.mem_singleton _ + have hka : σa.kheap p.bv = some (.tcb { x with tcbMcpriority := Fl4Spec.ucast 8 y0.bv }) := by + rw [hσa]; simp [Notif.updK] + have hPF : PFr p.bv σa σb := + setPriority_pfr hka _ (fun u => hns2 u) (blkQ_of_sv J3.sv hka) J3.sv _ hb0 + have hCb : Sched.CurInv σb := hPF.curInv hCa + have hnsb : ∀ u, σb.schedulerAction ≠ .switchThread u := fun u e => by + rcases hPF.act with e' | e' <;> rw [e'] at e + · exact hns2 u e + · cases e + have hvk : VK σ2 σb := (presV_setMcpriority _ _ σ2 _ ha).trans (presV_setPriority _ _ σa _ hb) + obtain ⟨xb, hkb, hstb, hscb⟩ := hvk.tcb _ x hk2 + have hidb : p.bv ≠ σb.idleThread := by + rw [hPF.sk.idle] + show p.bv ≠ σ2.idleThread + rw [hid2, hid1] + obtain ⟨Q0, R0, hc0, hs0⟩ := J.1.1.xtop.fin.1.1.1.lists + have ei : L.idleW s = nodeW st s Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW nodeW; rw [hc0.cur] + intro e + exact hpI (Eq.symm (by rw [ei]; apply P1.eq_of_bv; rw [← hs0.idleTh, ← e])) + -- the outcome + have fin : ∀ (s5 : S) (a5 : A) (σc : Fl4Spec.State Unit), ((), σc) ∈ (scStep p.bv sco σb).1 → + VEAnyX (L := L) arI AR s5 a5 σc → + (ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s5, a5) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => + TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don m s res ⦄ := by + rintro s5 a5 σc hcc hany + rw [WP.spec_ok] + refine Or.inr ⟨.base (.invokeTCB (.threadControlSched p.bv ssl (some e2) + (some (Fl4Spec.ucast 8 y0.bv, q)) (some (Fl4Spec.ucast 8 y1.bv, q)) sco)), DecOut.liftME hdec, σ1', hst1, + (.ok [], σc), ?_, Or.inl ⟨rfl, rfl, hany⟩⟩ + show _ ∈ ((Fl4Spec.invokeTcbVF (Z := Unit) (.threadControlSched p.bv ssl (some e2) + (some (Fl4Spec.ucast 8 y0.bv, q)) (some (Fl4Spec.ucast 8 y1.bv, q)) sco)).run σ1').1 + simp only [Fl4Spec.invokeTcbVF] + exact mem_bindE_ok hx (mem_bindE_ok (mem_liftE ha) (mem_bindE_ok (mem_liftE hb) + (mem_bindE_ok (mem_liftE hcc) (Set.mem_singleton _)))) + cases scf + · -- no SC step + have hsco : sco = none := h1.2 rfl + subst hsco + simp only [Bool.false_eq_true, ↓reduceIte] + exact fin s4 a1 σb (Set.mem_singleton _) (st4.anyX arI) + simp only [↓reduceIte] + step with Sched.obj.Sc.is_null.spec' as ⟨bz, hbz⟩ + by_cases hs0 : scv = 0#u64 + · -- the unbind + have hsco : sco = some none := by + cases sco with + | none => exact absurd (h1.1 rfl) (by simp) + | some o => + cases o with + | none => rfl + | some v => exact absurd hs0 (h3 v rfl).2 + subst hsco + rw [hbz, decide_eq_true hs0] + simp only [↓reduceIte] + obtain ⟨cc, y, n, hcx, hks, hy⟩ := hU2 rfl + have hksb : σb.kheap cc = some (.schedContext y n) := (hvk.sc cc y n).2 hks + have hxbsc : xb.tcbSchedContext = some cc := by rw [hscb, hcx] + obtain ⟨Qb, Rb, hcb, hsbl⟩ := J4.xtop.fin.1.1.1.lists + obtain ⟨xb', hkb', -, -, hscw, -⟩ := hsbl.tcb p hpT + rw [hkb] at hkb'; cases hkb' + step with P1.obj.Tcb.sc.spec as ⟨i4, hi4⟩ + have hi40 : i4 ≠ 0#u64 := fun e => by rw [hscw, ← hi4, e, Notif.optRef_zero] at hxbsc; cases hxbsc + have hi4c : i4.bv = cc := by rw [hscw, ← hi4, Notif.optRef_ne hi40] at hxbsc; exact Option.some.inj hxbsc + have hi4C : i4 ∈ Z.C := by rw [hi4]; exact hsbl.scMem p hpT (by rw [← hi4]; exact hi40) + step with Sched.obj.Sc.is_null.spec' as ⟨b1, hb1⟩ + rw [hb1, decide_eq_false hi40] + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (J4.unbindTcbJD hpT hi4C hkb (by rw [hxbsc, hi4c]) (fun u e => absurd e (hnsb u)) hidb) ?_ + rintro s5 ⟨σc, hsc, Ac5, Lst5, J5, F5⟩ + refine fin s5 a1 σc ?_ ((st4.ofNat (V' := V4) J5 F5).anyX arI) + show _ ∈ ((Fl4Spec.getTcbObjRef (Z := Unit) Fl4Spec.Tcb.tcbSchedContext p.bv >>= fun o => + Fl4Spec.NonDet.assertOpt o >>= fun c => Fl4Spec.schedContextUnbindTcb c) σb).1 + rw [nd_bind_one (Endp.getTcbObjRef_eq _ hkb), hxbsc] + rw [nd_bind_one (show (Fl4Spec.NonDet.assertOpt (some cc) : Fl4Spec.SMonad Unit Fl4Spec.ObjRef) σb = + ({(cc, σb)}, False) from rfl), ← hi4c, hsc] + exact Set.mem_singleton _ + · -- the bind + have hsco : sco = some (some scv.bv) := by + cases sco with + | none => exact absurd (h1.1 rfl) (by simp) + | some o => + cases o with + | none => exact absurd (h2 rfl) hs0 + | some v => rw [(h3 v rfl).1] + subst hsco + rw [hbz, decide_eq_false hs0] + simp only [Bool.false_eq_true, ↓reduceIte] + obtain ⟨-, yd, nd, hkd, hyd0, hreld⟩ := hDB _ rfl + obtain ⟨x0, -, -, hkt0', hxs0', -⟩ := hB2 _ rfl + rw [hk2] at hkt0'; cases hkt0' + have hxb0 : xb.tcbSchedContext = none := by rw [hscb]; exact hxs0' + -- the SC and the release check, carried from the decode + have hdc : scv.bv ≠ ct.bv := fun e => by rw [e, hkc] at hkd; cases hkd + have hk1d : σ1'.kheap scv.bv = some (.schedContext yd nd) := by + rw [restart_kheap hkc hst1c, Function.update_of_ne hdc]; exact hkd + have hkbd : σb.kheap scv.bv = some (.schedContext yd nd) := (hvk.sc _ yd nd).2 (hscK _ yd nd hk1d hyd0) + have hpct : p.bv ≠ ct.bv := fun e => hpc _ rfl (by rw [e, hcur]) + have hkp1' : σ1'.kheap p.bv = some (.tcb xp) := by + rw [restart_kheap hkc hst1c, Function.update_of_ne hpct]; exact hkp + rw [hkp1'] at hkp1; cases hkp1 + have hrelb : Fl4Spec.ipcQueuedThreadState xb.tcbState = true → Fl4Spec.scReleased σb.curTime yd = true := by + intro hq + rw [hstb] at hq + have e := hstK x hk2 hq + rw [hvk.tm, htm2, htm1] + exact hreld (by rw [← e]; exact hq) + -- the pair's caps, kept by the steps + have hTn : capV (L.mem s) (cslotAddr ssl) ≠ .Null := by rw [hpsl]; exact fun h => by cases h + have hTe : ∀ e b0 b1 b2 b3 b4, capV (L.mem s) (cslotAddr ssl) ≠ .Endpoint e b0 b1 b2 b3 b4 := by + intro e b0 b1 b2 b3 b4 h; rw [hpsl] at h; cases h + have hT4 : capAt (L.mem s4) (cslotAddr ssl) = ok (.Thread p) := by + rw [capAt_capV, hSS4.capV hAs.isSlot, hSS3.capV hAs.isSlot, hroot1 _ hAs.isSlot hTn hTe, hpsl] + obtain ⟨xs, sb, hxsS, hxsc⟩ := hscs hs0 + have hSn : capV (L.mem s) xs ≠ .Null := by rw [hxsc]; exact fun h => by cases h + have hSe : ∀ e b0 b1 b2 b3 b4, capV (L.mem s) xs ≠ .Endpoint e b0 b1 b2 b3 b4 := by + intro e b0 b1 b2 b3 b4 h; rw [hxsc] at h; cases h + have hS4 : capAt (L.mem s4) xs = ok (.SchedContext scv sb) := by + rw [capAt_capV, hSS4.capV hxsS, hSS3.capV hxsS, hroot1 _ hxsS hSn hSe, hxsc] + obtain ⟨hscvC, -, hsb, hssz, -⟩ := J4.scCtxCapB arI hxsS hS4 + refine WP.spec_bind (J4.bindTcbF hAT hAK hTA hscvC hpT hkbd hyd0 hkb hxb0 hrelb hnsb hCb hidb + hAs.isSlot hT4 hxsS hS4 hsb (hssz yd nd hkbd)) ?_ + rintro ⟨s5, a5⟩ ⟨σc, hsc, Ac5, Lst5, D5, J5, F5⟩ + try simp only [uncurry_apply_pair] + refine fin s5 a5 σc ?_ ((st4.ofNat (V' := V4) J5 F5).anyX arI) + show _ ∈ (Fl4Spec.schedContextBindTcb (Z := Unit) scv.bv p.bv σb).1 + rw [hsc]; exact Set.mem_singleton _ + +set_option maxHeartbeats 8000000 in +/-- **The SetSchedParams label at `JAt`** (`JAtPre` if preempted; as +XTopTcbSched's `tcb_set_sched_paramsX`): the decode (the authority's MCP by +`JAtZ`'s `McpRel`, the SC checks by the scheduler and notification +relations), then `ssp_tailJM`. -/ +theorem tcb_set_sched_paramsVE [Fl4Proofs.P1.PlainStore st] {W Wp : U64 → S → A → Fl4Spec.State Unit → MdbGhost → Prop} (on rc : Fl4Spec.ObjRef → Bool) + (s : S) (ar : A) (σ : Fl4Spec.State Unit) {V X : Set U64} {XL : U64 → Nat} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hsg : SpecGlob σ) (hP0 : PPR0 σ) + (hHp : TcbInsWV (L := L) arI AR MR Z H Cs W Wp) (hRs : RestartWJ (st := st) Z s ar σ (W (L.ksb s))) + (hAT : P0.ArchTotal arI) (hAK : ARKeep arI AR) (hTA : TimeAgree arI) + (p : U64) (hp : Z.th p = some .tcb) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .tcbSetSchedParams) + (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) + (hpI : p ≠ L.idleW s) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (call : Bool) (ssl : Fl4Spec.CslotPtr) (hAs : AbsSlot Z.th ssl) + (hpsl : capV (L.mem s) (cslotAddr ssl) = .Thread p) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 x1 x2 : U64) + (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (hx1 : Aeneas.Std.Array.index_usize extra 1#usize = ok x1) + (hx2 : Aeneas.Std.Array.index_usize extra 2#usize = ok x2) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (hXc : XcRel Z.th (L.mem s) x0 x1 x2 excaps) + (hcapT : x0 ≠ 0#u64 → ∀ q, capV (L.mem s) x0 = .Thread q → q ∈ Z.T) + (hcapC : x1 ≠ 0#u64 → ∀ q b, capV (L.mem s) x1 = .SchedContext q b → q ∈ Z.C) + (blk don : Bool) (ci : Fl4Spec.CapRef) (buffer : Option Fl4Spec.ObjRef) : + TcbOKV (L := L) arI (AR := AR) (MR := MR) on rc s ar σ fl4_abi.label.TCBSetSchedParams p + ⟨length, buf, call, cslotAddr ssl, extra⟩ blk call don slbl (args.map UScalar.bv) ci ssl excaps buffer := by + have hS : SchedRel st Z.N Z.T Z.C Z.c s σ := J.1.1.xtop.fin.1.1.1 + have hR : Notif.NtfnStateRel Z.N Z.T (L.mem s) σ := J.1.1.xtop.fin.1.1.2.2.1 + have hpT : p ∈ Z.T := J.1.1.thT p hp + have hM := J.1.1.mcp + have hC0 := J.1.1.c0 + have hcur : σ.curThread = ct.bv := (J.1.1.curEq hct).1 + have hJ0 : VEAnyX (L := L) arI AR s ar σ := J.anyX arI + have hdec0 : specDec false slbl (args.map UScalar.bv) ci ssl (.Thread p) excaps buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSchedParams (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps) := by + show Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl (args.map UScalar.bv) (.threadCap p.bv) ssl excaps buffer = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + unfold TcbOKV + rw [decode_ssp_eq arI s ar p _ hstall, hdec0] + have htr : args.length < 2 ∨ excaps.length < 3 → + (ok ((.Err (.Sys .TruncatedMessage) : core.result.Result Unit error.Exc), s, ar) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSchedParams (Z := Unit) (args.map UScalar.bv) (.threadCap p.bv) ssl excaps)) s res ⦄ := by + intro h + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.truncatedMessage, ?_, Or.inl trivial⟩, hJ0⟩) + apply run_liftME_err + unfold Fl4Spec.decodeSetSchedParams + by_cases ha : args.length < 2 + · rw [whenE_pos (by simpa using ha)] + exact run_throw_bind _ _ _ + · rw [whenE_neg (by simpa using ha), retOk_bind, whenE_pos (h.resolve_left ha)] + exact run_throw_bind _ _ _ + unfold tcb.decode_set_sched_params tcb.extra_with_length + rw [extra_eq _ 0#usize (by decide) hx0, extra_eq _ 1#usize (by decide) hx1, extra_eq _ 2#usize (by decide) hx2] + have hx0' : [x0, x1, x2][0]? = some x0 := rfl + have hx1' : [x0, x1, x2][1]? = some x1 := rfl + have hx2' : [x0, x1, x2][2]? = some x2 := rfl + have hge : (⟨length, buf, call, cslotAddr ssl, extra⟩ : tcb.Ctx).length >= 2#u64 ↔ 2 ≤ args.length := by + show length >= 2#u64 ↔ _; rw [← hlen]; scalar_tac + by_cases h0 : x0 = 0#u64 + · have hl : excaps.length ≤ 0 := ((hXc.2 0 x0 hx0').1).1 h0 + simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok] + exact htr (Or.inr (by omega)) + by_cases hl : 2 ≤ args.length + swap + · simp only [h0, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inl (by omega)) + by_cases h1 : x1 = 0#u64 + · have hl1 : excaps.length ≤ 1 := ((hXc.2 1 x1 hx1').1).1 h1 + simp only [h0, h1, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inr (by omega)) + by_cases h2 : x2 = 0#u64 + · have hl2 : excaps.length ≤ 2 := ((hXc.2 2 x2 hx2').1).1 h2 + simp only [h0, h1, h2, ↓reduceIte, bind_ok, error.truncated, core.result.Result.Insts.CoreOpsTry.branch, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, core.convert.FromSame.from, + bind_tc_ok, hge, hl] + exact htr (Or.inr (by omega)) + simp only [h0, h1, h2, ↓reduceIte, bind_ok, core.result.Result.Insts.CoreOpsTry.branch, hge, hl] + obtain ⟨y0, y1, rest, rfl⟩ : ∃ y0 y1 rest, args = y0 :: y1 :: rest := by + match args, hl with + | y0 :: y1 :: rest, _ => exact ⟨_, _, _, rfl⟩ + have ha0 := tcb_arg_eq s ⟨length, buf, call, cslotAddr ssl, extra⟩ hct (hargs 0#u64 y0 (by simp)) + have ha1 := tcb_arg_eq s ⟨length, buf, call, cslotAddr ssl, extra⟩ hct (hargs 1#u64 y1 (by simp)) + simp only [ha0, ha1, bind_ok] + obtain ⟨e0, he0, hxe0, he01, ps0, hps0, hps01⟩ := hXc.get hx0' h0 + obtain ⟨e1, he1, hxe1, he11, ps1, hps1, hps11⟩ := hXc.get hx1' h1 + obtain ⟨e2, he2, hxe2, he21, ps2, hps2, hps21⟩ := hXc.get hx2' h2 + have hexl : ¬ excaps.length < 3 := by + have := (List.getElem?_eq_some_iff.1 he2).1; omega + -- the spec up to the authority's cap + have hspec0 : (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps).run σ = ((match e0.1 with + | .threadCap tcbPtr => Fl4Spec.NonDetE.returnOk tcbPtr + | _ => Fl4Spec.NonDetE.throwError (.invalidCapability 1) : Fl4Spec.SEMonad Unit Fl4Spec.ObjRef) >>= fun q => + Fl4Spec.checkPrio (Z := Unit) y0.bv q >>= fun _ => Fl4Spec.checkPrio (Z := Unit) y1.bv q >>= fun _ => + Fl4Spec.decodeUpdateSc (Z := Unit) (.threadCap p.bv) ssl e1.1 >>= fun sco => + Fl4Spec.checkHandlerEp (Z := Unit) 3 e2 >>= fun fh => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl (some fh) + (some (Fl4Spec.ucast 8 y0.bv, q)) (some (Fl4Spec.ucast 8 y1.bv, q)) sco)).run σ := by + unfold Fl4Spec.decodeSetSchedParams + rw [whenE_neg (by simp), retOk_bind, whenE_neg hexl, retOk_bind, retOk_bind, retOk_bind, retOk_bind, + retOk_bind, retOk_bind, he0, he1, he2, the_some, the_some, the_some] + generalize e0.1 = cc + cases cc <;> rfl + by_cases hth : ∃ q, capV (L.mem s) x0 = .Thread q + swap + · have hne : ∀ r, absCap (capV (L.mem s) x0) ≠ .threadCap r := fun r h => by + obtain ⟨q, hq, -⟩ := absCap_thread h; exact hth ⟨q, hq⟩ + rw [thread_arg_other s 1#u64 (fun q h => hth ⟨q, h⟩)] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.invalidCapability 1, ?_, Or.inr ⟨1#u64, rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec0, he01] + generalize absCap (capV (L.mem s) x0) = cc at hne ⊢ + cases cc + all_goals first + | exact absurd rfl (hne _) + | exact run_throw_bind _ _ _ + obtain ⟨q, hq⟩ := hth + rw [thread_arg_thread s 1#u64 hq] + simp only [bind_ok, check_prio_eq] + have hqT := hcapT h0 q hq + have he0' : e0.1 = .threadCap q.bv := by rw [he01, hq]; rfl + rw [he0'] at hspec0 + simp only [] at hspec0 + rw [retOk_bind] at hspec0 + have hcp0 := checkPrio_run hM hqT y0.bv + have hcp1 := checkPrio_run hM hqT y1.bv + obtain ⟨xq, hkq, hmq, -⟩ := hM q hqT + have hmq8 : (L.mem s (mcpA q)).val < 256 := by rw [hmq]; exact xq.tcbMcpriority.isLt + by_cases hg0 : y0 > L.mem s (mcpA q) + · rw [ite_eq_left hg0] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.rangeError 0 (L.mem s (mcpA q)).bv, ?_, Or.inl ⟨rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec0, runE_bind_congr hcp0, whenE_pos (by rw [GT.gt, ← u64_lt_bv]; exact hg0)] + exact run_throw_bind _ _ _ + rw [ite_eq_right hg0] + simp only [bind_ok] + by_cases hg1 : y1 > L.mem s (mcpA q) + · rw [ite_eq_left hg1] + simp only [bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.rangeError 0 (L.mem s (mcpA q)).bv, ?_, Or.inl ⟨rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec0, runE_bind_congr hcp0, whenE_neg (by rw [GT.gt, ← u64_lt_bv]; exact hg0), retOk_bind, + runE_bind_congr hcp1, whenE_pos (by rw [GT.gt, ← u64_lt_bv]; exact hg1)] + exact run_throw_bind _ _ _ + rw [ite_eq_right hg1] + simp only [bind_ok] + have hy0 : y0.val < 256 := by have : ¬ (L.mem s (mcpA q)).val < y0.val := hg0; omega + have hy1 : y1.val < 256 := by have : ¬ (L.mem s (mcpA q)).val < y1.val := hg1; omega + have hspec1 : (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps).run σ = (Fl4Spec.decodeUpdateSc (Z := Unit) (.threadCap p.bv) ssl e1.1 >>= fun sco => + Fl4Spec.checkHandlerEp (Z := Unit) 3 e2 >>= fun fh => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl (some fh) + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) (some (Fl4Spec.ucast 8 y1.bv, q.bv)) sco)).run σ := by + rw [hspec0, runE_bind_congr hcp0, whenE_neg (by rw [GT.gt, ← u64_lt_bv]; exact hg0), retOk_bind, + runE_bind_congr hcp1, whenE_neg (by rw [GT.gt, ← u64_lt_bv]; exact hg1), retOk_bind] + obtain ⟨Q, R', hcL, hsL⟩ := hS.lists + obtain ⟨xp, hkp, -, -, hxts, -⟩ := hsL.tcb p hpT + have hsc0 : obj.Tcb.sc st p s = ok (L.mem s (P1.scA p)) := by + obtain ⟨w, hw, rfl⟩ := spec_exists (P1.obj.Tcb.sc.spec (L := L) s p); exact hw + -- after the SC check: the fault handler, the restart and the invocation + have hafter : ∀ (scv : U64) (sco : Option (Option Fl4Spec.ObjRef)), + (Fl4Spec.decodeUpdateSc (Z := Unit) (.threadCap p.bv) ssl e1.1).run σ = ({(.ok sco, σ)}, False) → + (sco = none ↔ (L.mem s (P1.scA p) != scv) = false) → (sco = some none → scv = 0#u64) → + (∀ v, sco = some (some v) → v = scv.bv ∧ scv ≠ 0#u64) → ScDec σ p.bv sco → + (scv ≠ 0#u64 → ∃ xs sb, IsSlot Z.th xs ∧ capV (L.mem s) xs = .SchedContext scv sb) → + (do + let i ← lift (tcb.SCHED_UPDATE_MCP ||| tcb.SCHED_UPDATE_PRIO) + let flags ← lift (i ||| tcb.SCHED_UPDATE_FAULT) + if (L.mem s (P1.scA p) != scv) = true then do + let flags1 ← lift (flags ||| tcb.SCHED_UPDATE_SC) + let fh ← obj.Slot.cap st x2 s + let b ← faults.valid_fault_handler fh + if b = true then do + let s1 ← tcb.restart_caller st s + tcb.thread_control_sched st arI s1 ar p (cslotAddr ssl) (fh, x2) y0 y1 scv flags1 + else do + let r7 ← error.invalid_cap Unit 3#u64 + ok (r7, s, ar) + else do + let fh ← obj.Slot.cap st x2 s + let b ← faults.valid_fault_handler fh + if b = true then do + let s1 ← tcb.restart_caller st s + tcb.thread_control_sched st arI s1 ar p (cslotAddr ssl) (fh, x2) y0 y1 scv flags + else do + let r7 ← error.invalid_cap Unit 3#u64 + ok (r7, s, ar)) ⦃ res => TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)) s res ⦄ := by + intro scv sco hdus hs1 hs2 hs3 hD hscs + simp only [lift, bind_ok, slot_cap_eq, capAt_capV, vfh_eq] + by_cases hv : Fl4Spec.validFaultHandler (absCap (capV (L.mem s) x2)) + · simp only [decide_eq_true hv, ↓reduceIte] + have hdec : (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)).run σ = ({(.ok (.invokeTCB (.threadControlSched p.bv ssl (some e2) + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) (some (Fl4Spec.ucast 8 y1.bv, q.bv)) sco)), σ)}, False) := by + apply run_liftME_ok + rw [hspec1, run_bind_ok_st hdus] + unfold Fl4Spec.checkHandlerEp + rw [Fl4Spec.NonDetE.unlessE, if_pos (by rw [he21]; exact hv)] + show ((Fl4Spec.NonDetE.returnOk () >>= fun _ => Fl4Spec.NonDetE.returnOk e2) >>= _).run σ = _ + rw [retOk_bind, retOk_bind]; rfl + by_cases hscf : (L.mem s (P1.scA p) != scv) = true + · simp only [hscf, ↓reduceIte] + have h1' : sco = none ↔ true = false := by rw [hs1, hscf] + have := ssp_tailVE arI on rc s ar σ J hni hsg hP0 hHp hRs hAT hAK hTA p hp ct hct ssl hAs hpsl x2 e2 hxe2 he21 ps2 + hps2 hps21 hv y0 y1 scv hy0 hy1 true q.bv sco h1' hs2 hs3 hD hpI hscs _ (DecOut.of_eq hdec) blk call don + simp only [↓reduceIte] at this + exact this + · simp only [Bool.not_eq_true] at hscf + simp only [hscf, Bool.false_eq_true, ↓reduceIte] + have h1' : sco = none ↔ false = false := by rw [hs1, hscf] + have := ssp_tailVE arI on rc s ar σ J hni hsg hP0 hHp hRs hAT hAK hTA p hp ct hct ssl hAs hpsl x2 e2 hxe2 he21 ps2 + hps2 hps21 hv y0 y1 scv hy0 hy1 false q.bv sco h1' hs2 hs3 hD hpI hscs _ (DecOut.of_eq hdec) blk call don + simp only [Bool.false_eq_true, ↓reduceIte] at this + exact this + · simp only [decide_eq_false hv, Bool.false_eq_true, ↓reduceIte, error.invalid_cap, bind_ok] + have hiv : ∀ (scf : Bool), (ok ((.Err (.Sys (.InvalidCapability 3#u64)) : core.result.Result Unit error.Exc), s, ar) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)) s res ⦄ := by + intro _ + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.invalidCapability 3, ?_, Or.inr ⟨3#u64, rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec1, run_bind_ok_st hdus] + unfold Fl4Spec.checkHandlerEp + rw [Fl4Spec.NonDetE.unlessE, if_neg (by rw [he21]; exact hv)] + exact run_bind_error (run_throw_bind _ _ _) _ + split <;> exact hiv true + have hpre : ∀ cp : Fl4Spec.Cap, e1.1 = cp → + (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps).run σ = (Fl4Spec.decodeUpdateSc (Z := Unit) (.threadCap p.bv) ssl cp >>= fun sco => + Fl4Spec.checkHandlerEp (Z := Unit) 3 e2 >>= fun fh => + Fl4Spec.NonDetE.returnOk (Fl4Spec.TcbInvocation.threadControlSched p.bv ssl (some fh) + (some (Fl4Spec.ucast 8 y0.bv, q.bv)) (some (Fl4Spec.ucast 8 y1.bv, q.bv)) sco)).run σ := by + rintro cp rfl; exact hspec1 + unfold tcb.sched_params_sc + rw [slot_cap_eq, capAt_capV] + simp only [bind_ok] + cases hc1 : capV (L.mem s) x1 + case Null => + rw [he11, hc1] at hspec1 + simp only [absCap] at hspec1 + rw [hct] + simp only [bind_ok, obj.Tcb.Insts.CoreCmpPartialEqTcb.eq] + by_cases hpc : p = ct + · simp only [decide_eq_true hpc, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.illegalOperation, ?_, Or.inl trivial⟩, hJ0⟩) + apply run_liftME_err + rw [hspec1] + refine run_bind_error ?_ _ + rw [dus_null hkp ssl, whenE_pos (by rw [hcur, hpc])] + exact run_throw_bind _ _ _ + · simp only [decide_eq_false hpc, Bool.false_eq_true, ↓reduceIte, bind_ok, hsc0] + have hpc' : ¬ p.bv = σ.curThread := by rw [hcur]; intro e; exact hpc (P1.eq_of_bv e) + refine hafter 0#u64 _ (by rw [he11, hc1]; simp only [absCap]; rw [dus_null hkp ssl, whenE_neg hpc', retOk_bind]; rfl) + ?_ (fun _ => rfl) (fun v h => by split at h <;> cases h) + ⟨xp, hkp, fun h => ⟨hpc', fun e => (by simp [e] at h)⟩, fun v h => (by split at h <;> cases h)⟩ (fun h => absurd rfl h) + rw [hxts] + by_cases hz : L.mem s (P1.scA p) = 0#u64 + · simp [hz, Notif.optRef_zero] + · simp [hz, Notif.optRef_ne hz] + case SchedContext ptr sz => + have hx1S : IsSlot Z.th x1 := by rw [hxe1]; exact AbsSlot.isSlot ⟨ps1, hps11, Or.inl hps1⟩ + have hscsP : ptr ≠ 0#u64 → ∃ xs sb, IsSlot Z.th xs ∧ capV (L.mem s) xs = .SchedContext ptr sb := + fun _ => ⟨x1, sz, hx1S, hc1⟩ + rw [he11, hc1] at hspec1 + simp only [absCap] at hspec1 + have hpC := hcapC h1 ptr sz hc1 + have hptr0 := hC0 ptr hpC + obtain ⟨y, n, hks, hx⟩ := hsL.scs ptr hpC + have hne : Fl4Spec.scActive y = true → y.scRefills ≠ [] := fun ha => by + rw [(hx.strong ha).refills]; exact refills_ne (hx.strong ha).wf + have hdus := dus_sc hkp hks hne ssl (sz.val - Fl4Spec.minSchedContextBits) + have hst := (hR.at_tcb hpT hkp).st + have hxt' : y.scTcb = Notif.optRef (L.mem s (scW ptr 24#u64)) := hx.tcb + have hcons : (((if xp.tcbSchedContext ≠ some ptr.bv then some (some ptr.bv) else none) = none ↔ + (L.mem s (P1.scA p) != ptr) = false) ∧ + ((if xp.tcbSchedContext ≠ some ptr.bv then some (some ptr.bv) else none) = some none → ptr = 0#u64) ∧ + (∀ v, (if xp.tcbSchedContext ≠ some ptr.bv then some (some ptr.bv) else none) = some (some v) → + v = ptr.bv ∧ ptr ≠ 0#u64)) := by + rw [hxts] + refine ⟨?_, (fun h => by split at h <;> cases h), (fun v h => by split at h <;> cases h; exact ⟨rfl, hptr0⟩)⟩ + simp only [ne_eq, bv_optRef_some hptr0] + by_cases e : L.mem s (P1.scA p) = ptr <;> simp [e] + have hillT : (Fl4Spec.decodeUpdateSc (Z := Unit) (.threadCap p.bv) ssl + (.schedContextCap ptr.bv (sz.val - Fl4Spec.minSchedContextBits))).run σ = + ({(.error .illegalOperation, σ)}, False) → + (ok ((.Err (.Sys .IllegalOperation) : core.result.Result Unit error.Exc), s, ar) : + Result (core.result.Result Unit error.Exc × S × A)) ⦃ res => TopPostVT (L := L) arI (AR := AR) (MR := MR) on rc σ blk call don + (Fl4Spec.NonDetE.liftME Fl4Spec.Fl4Invocation.invokeTCB + (Fl4Spec.decodeSetSchedParams (Z := Unit) ((y0 :: y1 :: rest).map UScalar.bv) (.threadCap p.bv) ssl + excaps)) s res ⦄ := by + intro h + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.illegalOperation, ?_, Or.inl trivial⟩, hJ0⟩) + apply run_liftME_err + rw [hspec1] + exact run_bind_error h _ + simp only [hsc0, bind_ok] + by_cases hip : L.mem s (P1.scA p) = ptr + · -- the thread's own SC + have hc1' : ¬ (xp.tcbSchedContext ≠ some ptr.bv ∧ (xp.tcbSchedContext ≠ none ∨ y.scTcb ≠ none)) := by + rw [hxts, hip, Notif.optRef_ne hptr0]; simp + rw [if_neg (show ¬ (L.mem s (P1.scA p) != ptr) = true by simp [hip])] + obtain ⟨bb, hbb, rfl⟩ := spec_exists (is_blocked_spec s p xp.tcbState hst) + rw [hbb]; simp only [bind_ok] + by_cases hb : Fl4Spec.ipcQueuedThreadState xp.tcbState = true + · simp only [hb, ↓reduceIte] + obtain ⟨b1, hb1e, hb1⟩ := spec_exists (sc_released_refines s σ hS hpC hks) + rw [hb1e]; simp only [bind_ok] + rw [getScReleased_eq hks hne] at hb1 + have eb := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb1))).1 + by_cases hr : b1 = true + · simp only [hr, ↓reduceIte, bind_ok] + refine hafter _ _ ?_ hcons.1 hcons.2.1 hcons.2.2 + ⟨xp, hkp, fun h => (by split at h <;> cases h), + fun v h => (by rw [if_neg (by rw [hxts, hip, Notif.optRef_ne hptr0]; simp)] at h; cases h)⟩ hscsP + rw [he11, hc1]; simp only [absCap] + rw [hdus, whenE_neg hc1', retOk_bind, whenE_neg (by rw [eb, hr]; simp), retOk_bind]; rfl + · simp only [hr, Bool.false_eq_true, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine hillT ?_ + rw [hdus, whenE_neg hc1', retOk_bind, whenE_pos ⟨hb, by rw [eb]; simpa using hr⟩ +] + exact run_throw_bind _ _ _ + · simp only [hb, Bool.false_eq_true, ↓reduceIte, bind_ok] + refine hafter _ _ ?_ hcons.1 hcons.2.1 hcons.2.2 + ⟨xp, hkp, fun h => (by split at h <;> cases h), + fun v h => (by rw [if_neg (by rw [hxts, hip, Notif.optRef_ne hptr0]; simp)] at h; cases h)⟩ hscsP + rw [he11, hc1]; simp only [absCap] + rw [hdus, whenE_neg hc1', retOk_bind, whenE_neg (by simp [hb]), retOk_bind]; rfl + + · have hne1 : (L.mem s (P1.scA p) != ptr) = true := by simp [hip] + simp only [hne1, ↓reduceIte] + by_cases hi0 : L.mem s (P1.scA p) = 0#u64 + swap + · have : (L.mem s (P1.scA p) != 0#u64) = true := by simp [hi0] + simp only [this, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine hillT ?_ + rw [hdus, whenE_pos ⟨by rw [hxts, Ne, bv_optRef_some hptr0]; exact hip, + Or.inl (by rw [hxts, Notif.optRef_ne hi0]; simp)⟩] + exact run_throw_bind _ _ _ + have : (L.mem s (P1.scA p) != 0#u64) = false := by simp [hi0] + simp only [this, Bool.false_eq_true, ↓reduceIte] + have hscT : obj.Sc.tcb st ptr s = ok (L.mem s (scW ptr 24#u64)) := by + obtain ⟨w, hw, rfl⟩ := spec_exists (P1.obj.Sc.tcb.spec (L := L) s ptr); rw [scTcbA_eq] at hw; exact hw + simp only [hscT, bind_ok] + have hx0n : xp.tcbSchedContext = none := by rw [hxts, hi0, Notif.optRef_zero] + by_cases ht0 : L.mem s (scW ptr 24#u64) = 0#u64 + swap + · have : (L.mem s (scW ptr 24#u64) != 0#u64) = true := by simp [ht0] + simp only [this, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine hillT ?_ + rw [hdus, whenE_pos ⟨by rw [hx0n]; simp, Or.inr (by rw [hxt', Notif.optRef_ne ht0]; simp)⟩] + exact run_throw_bind _ _ _ + have : (L.mem s (scW ptr 24#u64) != 0#u64) = false := by simp [ht0] + simp only [this, Bool.false_eq_true, ↓reduceIte] + have hc1' : ¬ (xp.tcbSchedContext ≠ some ptr.bv ∧ (xp.tcbSchedContext ≠ none ∨ y.scTcb ≠ none)) := by + rw [hx0n, hxt', ht0, Notif.optRef_zero]; simp + obtain ⟨bb, hbb, rfl⟩ := spec_exists (is_blocked_spec s p xp.tcbState hst) + rw [hbb]; simp only [bind_ok] + by_cases hb : Fl4Spec.ipcQueuedThreadState xp.tcbState = true + · simp only [hb, ↓reduceIte] + obtain ⟨b1, hb1e, hb1⟩ := spec_exists (sc_released_refines s σ hS hpC hks) + rw [hb1e]; simp only [bind_ok] + rw [getScReleased_eq hks hne] at hb1 + have eb := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hb1))).1 + by_cases hr : b1 = true + · simp only [hr, ↓reduceIte, bind_ok] + refine hafter _ _ ?_ hcons.1 hcons.2.1 hcons.2.2 + ⟨xp, hkp, fun h => (by split at h <;> cases h), fun v h => ⟨hx0n, (by + have hv : v = ptr.bv := (by split at h <;> cases h; rfl) + subst hv + exact ⟨y, n, hks, (by rw [hxt', ht0, Notif.optRef_zero]), (fun _ => by rw [← hr, eb])⟩)⟩⟩ hscsP + rw [he11, hc1]; simp only [absCap] + rw [hdus, whenE_neg hc1', retOk_bind, whenE_neg (by rw [eb, hr]; simp), retOk_bind]; rfl + · simp only [hr, Bool.false_eq_true, ↓reduceIte, error.illegal, bind_ok, + core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine hillT ?_ + rw [hdus, whenE_neg hc1', retOk_bind, whenE_pos ⟨hb, by rw [eb]; simpa using hr⟩ +] + exact run_throw_bind _ _ _ + · simp only [hb, Bool.false_eq_true, ↓reduceIte, bind_ok] + refine hafter _ _ ?_ hcons.1 hcons.2.1 hcons.2.2 + ⟨xp, hkp, fun h => (by split at h <;> cases h), fun v h => ⟨hx0n, (by + have hv : v = ptr.bv := (by split at h <;> cases h; rfl) + subst hv + exact ⟨y, n, hks, (by rw [hxt', ht0, Notif.optRef_zero]), (fun hq => absurd hq hb)⟩)⟩⟩ hscsP + rw [he11, hc1]; simp only [absCap] + rw [hdus, whenE_neg hc1', retOk_bind, whenE_neg (by simp [hb]), retOk_bind]; rfl + + all_goals + rw [he11, hc1] at hspec1 + simp only [error.invalid_cap, bind_ok, core.result.Result.Insts.CoreOpsTry_traitFromResidualResult.from_residual, + core.convert.FromSame.from, bind_tc_ok] + refine (WP.spec_ok _).2 (VDispPostVEX.errV arI ⟨_, rfl, rfl, ⟨.invalidCapability 2, ?_, Or.inr ⟨2#u64, rfl, rfl⟩⟩, hJ0⟩) + apply run_liftME_err + rw [hspec1] + exact run_bind_error (dus_other hkp ssl (by simp [absCap]) (by simp [absCap])) _ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Tdx.lean b/proofs/Fl4Proofs/P3/VmEntryS3Tdx.lean new file mode 100644 index 000000000..068877222 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Tdx.lean @@ -0,0 +1,108 @@ +import Fl4Proofs.P3.XTopJointTdx +import Fl4Proofs.P3.VmEntryS3Decode + +/-! +Selected TDX dispatch. The shared decoder matches the status and reply at +the reached caller. [P] TDCALL preserves the native CR3 relation. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Endp Ipc + +noncomputable section + +section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] + {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +private theorem TdxArchStep.keepS3 (hK : MRKeepD arI AR) {a' : A} + (h : TdxArchStep arI a a') {c : Fl4Spec.Cr3} (ha : AR a c) : AR a' c := by + rcases h with ⟨p, d0, d1, d2, d3, d4, d5, d6, d7, v, ht⟩ | + ⟨i, d0, d1, d2, d3, d4, d5, v, ht⟩ | ⟨p, n, v, ht⟩ + · have hR := hK.tdx_report a p d0 d1 d2 d3 d4 d5 d6 d7 c ha + rw [ht, WP.spec_ok] at hR + exact hR + · have hR := hK.tdx_extend_rtmr a i d0 d1 d2 d3 d4 d5 c ha + rw [ht, WP.spec_ok] at hR + exact hR + · have hR := hK.tdx_get_quote a p n c ha + rw [ht, WP.spec_ok] at hR + exact hR + +set_option maxHeartbeats 3000000 in +/-- The selected TDX decoder follows the shared label, frame, length, and +hardware-status proofs. [P] The TDCALL preserves the CR3 relation. -/ +theorem VmEptState.selectedTdxS3 [P1.PlainStore st] + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hAT : P0.ArchTotal arI) (hK : MRKeepD arI AR) + {ct cptr len buf lbl : U64} {sl : Fl4Spec.CslotPtr} + {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl .TdxControl ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don fp : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) .TdxControl blk call don fp buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .TdxControl exs buffer) ct s res ⦄ := by + let words : List U64 := args.map fun w => ⟨w⟩ + have hm : words.map UScalar.bv = args := by + simp [words, List.map_map, Function.comp_def] + have B := J.1.1 + have hR := decodeTdxOfTail arI s a σ (VEAnyX (L := L) arI AR) (J.anyX arI) B.kern.tdx hAT + lbl len buf ct q.current words q.length q.words ex exs (ExtraOK.rel1 B.xtop.rel.1 q.extra) + (cslotAddr sl) blk call don fp (Fl4Spec.toBl cptr.bv) sl buffer + have hBase : DecOKRMX (L := L) arI (VEAnyX (L := L) arI AR) s a σ lbl len (cslotAddr sl) + .TdxControl blk call don fp buf ex lbl.bv (words.map UScalar.bv) (Fl4Spec.toBl cptr.bv) + sl exs buffer ct := by + apply hR + intro inv v a1 ha hd + obtain ⟨_, x, hk, hr⟩ := B.curRunnable hni q.current + obtain ⟨hcur, _, _⟩ := B.curEq q.current + obtain ⟨htcb, _, _⟩ := B.xtop.curTcb hcur + have ht := B.thT ct htcb + obtain ⟨s1, σ1, hst, eσ1, hs1, _, _, _, B1, F1⟩ := B.restartRunJ q.current hk hr + have J1 := J.ofNat (V' := Z.vs) B1 F1 + have J1A := J1.archV (ha.keepS3 hK B1.xtop.fin.1.1.2.2.2.2.2.2) + have hk1 : σ1.kheap ct.bv = some (.tcb { x with tcbState := .restart }) := by + simp only [eσ1, Notif.stsAct_kheap, Notif.updK_kheap, Function.update_self] + rw [hs1, bind_ok] + refine WP.spec_bind (J1A.1.1.reply1JFoot ht hk1 rfl call v) ?_ + rintro s2 ⟨hne, σ2, _, hTail, B2, F2⟩ + refine (WP.spec_ok _).2 (Or.inr ⟨rfl, hne, .invokeArchObject (.tdx inv), + DecOut.of_eq (run_liftME_ok (run_liftME_ok hd)), σ1, hst, [v.bv], σ1, ?_, ?_⟩) + · exact mem_liftE (ε := Unit) (x := ([v.bv], σ1)) (tdx_perform_out inv σ1 v.bv) + · exact ⟨((), σ2), by rw [hTail]; exact Set.mem_singleton _, B2.xtop.relTop, + (J1A.ofNat (V' := Z.vs) B2 F2).anyX arI⟩ + rw [hm] at hBase + refine WP.spec_mono hBase ?_ + intro res post + have hDec : specDecV fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .TdxControl exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec fp lbl.bv args (Fl4Spec.toBl cptr.bv) sl .TdxControl exs buffer) := by + exact Fl4Spec.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute]) + rw [hDec] + rcases post with ⟨e, he, hs, ⟨e', hd, hE⟩, hJ⟩ | + ⟨hres, hne, inv, hdi, σ1, hst, rr, σy, hp, z, hz, hRt, hJ⟩ + · exact Or.inl ⟨e, he, hs, ⟨e', run_liftME_err hd, hE⟩, hJ⟩ + · have hI : ∃ op, inv = .invokeArchObject op := Sched.DecOut.liftME_inv hdi + obtain ⟨op, rfl⟩ := hI + -- the TDX decoder yields a `tdx` arch invocation, which the VM perform does not substitute + have hns : ¬ Fl4Spec.PerformSubst (.invokeArchObject op) := by + obtain ⟨j, hj⟩ := DecOut.liftME2_inv (f := Fl4Spec.Fl4Invocation.invokeArchObject) + (g := Fl4Spec.Fl4ArchInvocation.tdx) hdi + cases hj + exact id + have hp' : (Except.ok rr, σy) ∈ ((Fl4Spec.fl4PerformInvocationVF (Z := Unit) on rc blk call don + (.base (.invokeArchObject op))).run σ1).1 := by + rw [VirtF13.fl4PerformInvocationVF_base on rc blk call don _ hns]; exact hp + exact Or.inr ⟨hres, hne, _, hdi.liftME, σ1, hst, rr, σy, hp', z, hz, hRt, hJ⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ThrAdapt.lean b/proofs/Fl4Proofs/P3/VmEntryS3ThrAdapt.lean new file mode 100644 index 000000000..562d10b42 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ThrAdapt.lean @@ -0,0 +1,174 @@ +import Fl4Proofs.P3.JAtZBThrPost +import Fl4Proofs.P3.JAtZBThrPostF +import Fl4Proofs.P3.VmEntryS3Decode + +/-! The adapter from a route-F Thread label's `ThrPost` (over `JAtZB` with `NatFoot`) to the VM dispatcher post. + +No premise. New file; no existing statement changed. -/ + +open Aeneas Aeneas.Std Result +namespace Fl4Proofs.P3 +open Classical fl4_core Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} +variable {Lst : U64 → List U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- On the TCB invocations installing no cap the extended perform is the native one. -/ +theorem invokeTcbVF_native {i : Fl4Spec.TcbInvocation} (h : NativeTcbInv i) : + Fl4Spec.invokeTcbVF (Z := Unit) i = Fl4Spec.invokeTcb i := by + cases i <;> try rfl + case threadControlCaps => exact h.elim + case threadControlSched t sl fh mcp prio sc => + cases h + rfl + +/-- The second-phase decoder at a Thread cap is `tcb::decode`; a `ThrPost` there is the VM dispatcher post. The +spec decoder is the native one under `.base` (`hdV`, every label but SetEPTRoot). -/ +theorem VmEptState.thrPost + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p : U64} {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (lbl : U64) (slbl : Fl4Spec.Data) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) + (hdV : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer)) + (h : tcb.decode st arI s a lbl p ⟨len, buf, call, cslotAddr sl, ex⟩ ⦃ res => + ThrPost (L := L) arI AR Z H Cs s a σ (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) + res ⦄) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don false buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ⦄ := by + rw [decode_invocation_thread_eq arI s a lbl len (cslotAddr sl) p blk call don buf ex ct q.current] + refine WP.spec_mono h ?_ + rintro res (⟨e, h1, h2, h3, e', hm, hr⟩ | ⟨i, hD, hVF, h1, σ1, hst, σ', hz, V', Ac', Lst', D', B', F⟩) + · refine Or.inl ⟨e, h1, h2, ⟨e', ?_, hr⟩, ?_⟩ + · rw [hdV]; exact run_liftME_err hm + · rw [h2, h3]; exact J.anyX arI + · refine Or.inr ⟨.base (.invokeTCB i), ?_, σ1, hst, (.ok [], σ'), ?_, + Or.inl ⟨h1, rfl, (J.ofNat B' F).anyX arI⟩⟩ + · rw [hdV]; exact DecOut.liftME hD + · show _ ∈ ((Fl4Spec.invokeTcbVF i).run σ1).1 + rw [invokeTcbVF_native hVF]; exact hz + +/-- `VmEptState.thrPost` for a label replying inside the decoder: a `ThrPostR` is the VM reply post. -/ +theorem VmEptState.thrPostR + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p : U64} {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (lbl : U64) (slbl : Fl4Spec.Data) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) + (hdV : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer)) + (h : tcb.decode st arI s a lbl p ⟨len, buf, call, cslotAddr sl, ex⟩ ⦃ res => + ThrPostR (L := L) arI AR Z H Cs s a σ call + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) ct res ⦄) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don false buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) ct s res ⦄ := by + rw [decode_invocation_thread_eq arI s a lbl len (cslotAddr sl) p blk call don buf ex ct q.current] + refine WP.spec_mono h ?_ + rintro res (⟨e, h1, h2, h3, e', hm, hr⟩ | + ⟨h1, hne, i, hD, hVF, σ1, hst, rr, σy, hz, z, hzm, hR, V', Ac', Lst', D', B', F⟩) + · refine Or.inl ⟨e, h1, h2, ⟨e', ?_, hr⟩, ?_⟩ + · rw [hdV]; exact run_liftME_err hm + · rw [h2, h3]; exact J.anyX arI + · refine Or.inr ⟨h1, hne, .base (.invokeTCB i), ?_, σ1, hst, rr, σy, ?_, z, hzm, hR, (J.ofNat B' F).anyX arI⟩ + · rw [hdV]; exact DecOut.liftME hD + · show _ ∈ ((Fl4Spec.invokeTcbVF i).run σ1).1 + rw [invokeTcbVF_native hVF]; exact hz + +/-- `VmEptState.thrPost` for a TCB extension label (`ThrPostF`): the extended perform of `.base (.invokeFl4TCB j)` is +the native one. -/ +theorem VmEptState.thrPostF + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p : U64} {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (lbl : U64) (slbl : Fl4Spec.Data) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) + (hdV : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer)) + (h : tcb.decode st arI s a lbl p ⟨len, buf, call, cslotAddr sl, ex⟩ ⦃ res => + ThrPostF (L := L) arI AR Z H Cs s a σ (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) + res ⦄) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don false buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ⦄ := by + rw [decode_invocation_thread_eq arI s a lbl len (cslotAddr sl) p blk call don buf ex ct q.current] + refine WP.spec_mono h ?_ + rintro res (⟨e, h1, h2, h3, e', hm, hr⟩ | ⟨j, hD, h1, σ1, hst, σ', hz, V', Ac', Lst', D', B', F⟩) + · refine Or.inl ⟨e, h1, h2, ⟨e', ?_, hr⟩, ?_⟩ + · rw [hdV]; exact run_liftME_err hm + · rw [h2, h3]; exact J.anyX arI + · refine Or.inr ⟨.base (.invokeFl4TCB j), ?_, σ1, hst, (.ok [], σ'), ?_, + Or.inl ⟨h1, rfl, (J.ofNat B' F).anyX arI⟩⟩ + · rw [hdV]; exact DecOut.liftME hD + · exact hz + +/-- `VmEptState.thrPostR` for a TCB extension label (`ThrPostRF`). -/ +theorem VmEptState.thrPostRF + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p : U64} {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (lbl : U64) (slbl : Fl4Spec.Data) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) + (hdV : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer)) + (h : tcb.decode st arI s a lbl p ⟨len, buf, call, cslotAddr sl, ex⟩ ⦃ res => + ThrPostRF (L := L) arI AR Z H Cs s a σ call + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) ct res ⦄) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don false buf ex ⦃ res => + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) ct s res ⦄ := by + rw [decode_invocation_thread_eq arI s a lbl len (cslotAddr sl) p blk call don buf ex ct q.current] + refine WP.spec_mono h ?_ + rintro res (⟨e, h1, h2, h3, e', hm, hr⟩ | + ⟨h1, hne, j, hD, σ1, hst, rr, σy, hz, z, hzm, hR, V', Ac', Lst', D', B', F⟩) + · refine Or.inl ⟨e, h1, h2, ⟨e', ?_, hr⟩, ?_⟩ + · rw [hdV]; exact run_liftME_err hm + · rw [h2, h3]; exact J.anyX arI + · refine Or.inr ⟨h1, hne, .base (.invokeFl4TCB j), ?_, σ1, hst, rr, σy, hz, z, hzm, hR, (J.ofNat B' F).anyX arI⟩ + rw [hdV]; exact DecOut.liftME hD + +/-- `VmEptState.thrPostF` for the FPU labels (`ThrPostFX`: `JBN` in the error arm). -/ +theorem VmEptState.thrPostFX + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct cptr len buf p : U64} {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (lbl : U64) (slbl : Fl4Spec.Data) (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) + (blk call don : Bool) + (hdV : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer)) + (h : tcb.decode st arI s a lbl p ⟨len, buf, call, cslotAddr sl, ex⟩ ⦃ res => + ThrPostFX (L := L) arI AR Z H Cs s σ (specDec false slbl args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) + res ⦄) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don false buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ⦄ := by + rw [decode_invocation_thread_eq arI s a lbl len (cslotAddr sl) p blk call don buf ex ct q.current] + refine WP.spec_mono h ?_ + rintro res (⟨e, h1, h2, ⟨e', hm, hr⟩, V', Ac', Lst', D', B', F⟩ | + ⟨j, hD, h1, σ1, hst, σ', hz, V', Ac', Lst', D', B', F⟩) + · refine Or.inl ⟨e, h1, h2, ⟨e', ?_, hr⟩, ?_⟩ + · rw [hdV]; exact run_liftME_err hm + · exact (J.ofNat B' F).anyX arI + · refine Or.inr ⟨.base (.invokeFl4TCB j), ?_, σ1, hst, (.ok [], σ'), hz, + Or.inl ⟨h1, rfl, (J.ofNat B' F).anyX arI⟩⟩ + rw [hdV]; exact DecOut.liftME hD + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3ThrDisp.lean b/proofs/Fl4Proofs/P3/VmEntryS3ThrDisp.lean new file mode 100644 index 000000000..e88ec6b05 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3ThrDisp.lean @@ -0,0 +1,528 @@ +/- +P3 (line mmuAD, Stage 3, Thread labels): **the Thread cap's dispatcher over every second-phase label but SetEPTRoot**. + +`JAtZB` siblings of the dispatch-state facts (`cwOut`, `fnzJ`, `fpuFrameJ`, `idleThread_eq`, `argsRestart`), the error +leaf `tcb_illegalThr`, and `VmEptState.selectedThreadS3`. Generated by `handoff/tools/gen_thrdisp.py`. + +No premise beyond `dispatchKnownSelected`'s. New file. +-/ +import Fl4Proofs.P3.VmEntryS3TcbSsp +import Fl4Proofs.P3.VmEntryS3ThrAdapt +import Fl4Proofs.P3.VmEntryS3Thread +import Fl4Proofs.P3.JAtZBThrResume +import Fl4Proofs.P3.JAtZBThrSetPrio +import Fl4Proofs.P3.JAtZBThrMcp +import Fl4Proofs.P3.JAtZBThrBindTop +import Fl4Proofs.P3.JAtZBThrUnbind +import Fl4Proofs.P3.JAtZBThrTls +import Fl4Proofs.P3.JAtZBThrWriteTop +import Fl4Proofs.P3.JAtZBThrCopyTop +import Fl4Proofs.P3.JAtZBThrReadTop +import Fl4Proofs.P3.JAtZBThrFlags +import Fl4Proofs.P3.JAtZBThrSingleStep +import Fl4Proofs.P3.JAtZBThrGetBp +import Fl4Proofs.P3.JAtZBThrBp +import Fl4Proofs.P3.JAtZBThrTimer +import Fl4Proofs.P3.JAtZBThrFpu +import Fl4Proofs.P3.XTopJointKEntry35E +import Fl4Proofs.P3.JEntryKs +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +open Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **The syscall arguments after the caller's restart** (WriteRegisters +reads them after `set_thread_state ct Restart`): that step writes the +caller's state word and maybe the current core's scheduler-action word +(IntRel's `set_thread_state_cases`). The caller's message registers are +other words of its TCB, outside the node (`JKern.hnd`); its mapped buffer +words (`BufMap`, in `B`) lie apart from the TCBs (`EPlaced.bt`) and from the +kernel-state block up to the end of the current core's node +(`BufPlaced.far`). An index below 127 keeps the buffer word among the 128 +mapped ones. -/ +theorem JAtZB.argsRestart {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ) + {ct buf : U64} (hctT : ct ∈ Z.T) (hth : Z.th ct = some .tcb) + {sp : Fl4Spec.Word} (hbm : buf ≠ 0#u64 → BufMap Z.B buf sp) {i : U64} (hi : i.val < 127) + {s1 : S} (h1 : sched.set_thread_state st s ct sched.TS_RESTART = ok s1) : + msg.syscall_arg st s1 ct buf i = msg.syscall_arg st s ct buf i := by + have hc := set_thread_state_cases (st := st) s ct sched.TS_RESTART + rw [h1, WP.spec_ok] at hc + have hta : P1.TcbAddr ct := J.ip.e.tcb ct hctT + have hal := hta.align + have hstv : (stA ct).val = ct.val + 3328 := stA_val J.xtop.rel.1 hth + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hcq, -⟩ := hS.lists + have hcpu : L.cpu s = Z.c := hcq.cur + have ho : layout.node.SCHED_ACTION.val < 4288 := by simp [layout.node.SCHED_ACTION] + have ho8 : layout.node.SCHED_ACTION.val % 8 = 0 := by simp [layout.node.SCHED_ACTION] + have hav := Sched.nodeAt_val' (s := s) J.kern.nodeFit ho + have hkal := J.kern.ksbAl + have hal_st : P1.Aligned (stA ct) := by unfold P1.Aligned; rw [hstv]; omega + have hal_act : P1.Aligned (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION) := by + unfold P1.Aligned; rw [hav]; omega + have key : ∀ b : U64, P1.Aligned b → b.val ≠ (stA ct).val → + b.val ≠ (L.nodeAt s (L.cpu s) layout.node.SCHED_ACTION).val → L.mem s1 b = L.mem s b := by + intro b hb n1 n2 + rcases hc with rfl | rfl + · exact L.mem_wr_ne hal_st hb (Ne.symm n1) + · rw [L.mem_wr_ne hal_act hb (Ne.symm n2)]; exact L.mem_wr_ne hal_st hb (Ne.symm n1) + have hnd := J.kern.hnd ct _ (J.zH.1 ct hctT) (L.cpu s) J.kern.cpuCs + simp only [P1.HKind.bits, P1.LawfulStore.nodeBase, Nat.reducePow] at hnd + apply syscall_arg_keep + · intro g + have hr := regA_val hta (regIdx_lt g) + have := regIdx_lt g + exact key _ (by unfold P1.Aligned; rw [hr]; omega) (by rw [hr, hstv]; omega) (by rw [hr, hav]; omega) + · intro hb0 + have hi1 : (core.num.U64.wrapping_add i 1#u64).val = i.val + 1 := by + have := i.hBounds + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits] at this ⊢ + omega + have hw := hbm hb0 (core.num.U64.wrapping_add i 1#u64) (by rw [hi1]; omega) + set j := core.num.U64.wrapping_add i 1#u64 + have h8 : (fl4BufA buf j).val % 8 = 0 := (J.ip.e.balign _ hw).1 + have hbt : (fl4BufA buf j).val < ct.val ∨ ct.val + 4096 ≤ (fl4BufA buf j).val := J.ip.e.bt _ hw ct hctT + have hfar : (fl4BufA buf j).val + 8 ≤ (L.ksb s).val ∨ L.nodeBase s Z.c + 4288 ≤ (fl4BufA buf j).val := + hBP.far _ hw + simp only [P1.LawfulStore.nodeBase] at hfar + rw [← hcpu] at hfar + exact key (fl4BufA buf j) h8 (by rw [hstv]; omega) (by rw [hav]; omega) + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A word of the current core's node. -/ +theorem JAtZB.nodeAt_cur_val (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {off : U64} (ho : off.val < 4288) : + (L.nodeAt s (L.cpu s) off).val = (L.ksb s).val + 13568 + (L.cpu s).val * 4288 + off.val := + Sched.nodeAt_val' J.kern.nodeFit ho + +/-- **An object of the typed heap misses the current core's node.** -/ +theorem JAtZB.th_node (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p : U64} {k : ObjKind} + (hp : Z.th p = some k) : + p.val + objSize k ≤ L.nodeBase s (L.cpu s) ∨ L.nodeBase s (L.cpu s) + 4288 ≤ p.val := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + rcases J.thk p k hp with rfl | ⟨r, rfl⟩ + · have h := J.kern.hnd p .tcb ((hJ.tcb p).1 hp) (L.cpu s) J.kern.cpuCs + simp only [P1.HKind.bits] at h + simp only [objSize] + omega + · have h := J.kern.hnd p _ (hJ.cnode p r hp) (L.cpu s) J.kern.cpuCs + have e : (2 : Nat) ^ (r + 5) = 32 * 2 ^ r := by rw [pow_add]; ring + simp only [P1.HKind.bits, e] at h + simp only [objSize] + omega + +/-- **The current-thread word outside the objects** (the Thread decode's +`hcw`). -/ +theorem JAtZB.cwOut (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + OutsideObjects Z.th (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD) := by + have h0 : layout.node.CUR_THREAD.val = 0 := P1.curth0 + have hv := J.nodeAt_cur_val (off := layout.node.CUR_THREAD) (by omega) + have hal := J.kern.ksbAl + refine ⟨?_, fun p k hp => ?_⟩ + · show (L.nodeAt s (L.cpu s) layout.node.CUR_THREAD).val % 8 = 0 + rw [hv, h0]; omega + · have h := J.th_node hp + unfold P1.LawfulStore.nodeBase at h + omega + +/-- **No non-device frame cap at 0** (module docstring; INV-PLAN E16 (a)). -/ +theorem JAtZB.fnzJ (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : FrNZ Z.th (L.mem s) := by + intro y hy q sz r m e v w hc hq + subst hq + have h := J.fcap y hy + rw [hc] at h + obtain ⟨sz', hk⟩ := h rfl 0 (by simp) (by positivity) + have e0 : ∀ m : BitVec 64, BitVec.ofNat 64 0 &&& m = 0 := fun m => by simp + rw [e0] at hk + obtain ⟨n, -, hH⟩ := J.cover.arch _ _ hk + have h0 : H ⟨0⟩ = none := J.kern.h0 + rw [h0] at hH + cases hH + +/-- **ReadFPU's destination frame from `JAtZ`** (INV-PLAN P1, KENTRY-PLAN g4a +P1; g5b): `FpuFrameJHyp` is `JAtZ.fcap` at the slot (every byte of a +non-device frame cap in a spec user frame). -/ +theorem JAtZB.fpuFrameJ (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (x : U64) : + FpuFrameJHyp (L := L) Z s σ x := by + intro sl0 hA hx p sz r m ep asid va hc a h1 h2 + subst hx + have h := J.fcap _ hA.isSlot + rw [hc] at h + have hfb : (fbitsF sz).val = frameBitsN sz := by rw [fbitsF_val]; cases sz <;> rfl + exact h rfl a h1 (by rw [hfb]; exact h2) + +/-- **The spec's idle thread is the current core's `IDLE_THREAD` word** +(`SchedSpec.idleTh`). -/ +theorem JAtZB.idleThread_eq {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : σ.idleThread = (L.idleW s).bv := by + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + unfold P1.LawfulStore.idleW + have h2 := hs.idleTh + unfold Sched.nodeW at h2 + rw [hc.cur, ← h2] + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs : Set U64} + +/-- **A label of no TCB invocation is `IllegalOperation`** (`tcb_illegalX` as a `ThrPost`). -/ +theorem tcb_illegalThr (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (lbl p : U64) (c : tcb.Ctx) + (slbl : Fl4Spec.Data) (hlbl : Fl4Spec.genInvocationType slbl = tcbLabelOf lbl) + (args : List Fl4Spec.Data) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (buffer : Option Fl4Spec.ObjRef) + (hext : Fl4Spec.genInvocationType slbl = .invalidInvocation → Fl4Spec.fl4TcbExtLabelOf slbl = tcbExtOf lbl) + (hstall : smp.remote_tcb_stall st arI s ar p = ok (s, ar)) + (hNative : lbl ≠ fl4_abi.label.TCBSetEPTRoot) + (hlo : ¬ (2 ≤ lbl.val ∧ lbl.val ≤ 24)) : + tcb.decode st arI s ar lbl p c ⦃ res => + ThrPost (L := L) arI AR Z H Cs s ar σ (specDec false slbl args ci sl (.Thread p) excaps buffer) res ⦄ := by + have h2 : lbl.val ≠ 2 := by omega + have h3 : lbl.val ≠ 3 := by omega + have h4 : lbl.val ≠ 4 := by omega + have h5 : lbl.val ≠ 5 := by omega + have h6 : lbl.val ≠ 6 := by omega + have h7 : lbl.val ≠ 7 := by omega + have h8 : lbl.val ≠ 8 := by omega + have h9 : lbl.val ≠ 9 := by omega + have h10 : lbl.val ≠ 10 := by omega + have h11 : lbl.val ≠ 11 := by omega + have h12 : lbl.val ≠ 12 := by omega + have h13 : lbl.val ≠ 13 := by omega + have h14 : lbl.val ≠ 14 := by omega + have h15 : lbl.val ≠ 15 := by omega + have h16 : lbl.val ≠ 16 := by omega + have h17 : lbl.val ≠ 17 := by omega + have h18 : lbl.val ≠ 18 := by omega + have h19 : lbl.val ≠ 19 := by omega + have h20 : lbl.val ≠ 20 := by omega + have h21 : lbl.val ≠ 21 := by omega + have h22 : lbl.val ≠ 22 := by omega + have h23 : lbl.val ≠ 23 := by omega + have h24 : lbl.val ≠ 24 := by omega + have hg : Fl4Spec.genInvocationType slbl = .invalidInvocation := by + rw [hlbl]; simp [tcbLabelOf, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, h15, h16, h17, h18, h19, h20, h21, h22, h23, h24] + unfold tcb.decode + rw [hstall] + simp only [hNative, ↓reduceIte] + simp only [bind_ok, u64_eq_iff, lbl_tcbReadRegisters_val, lbl_tcbWriteRegisters_val, lbl_tcbCopyRegisters_val, lbl_tcbConfigure_val, lbl_tcbSetPriority_val, lbl_tcbSetMCPriority_val, lbl_tcbSetSchedParams_val, lbl_tcbSetTimeoutEndpoint_val, lbl_tcbSetIPCBuffer_val, lbl_tcbSetSpace_val, lbl_tcbSuspend_val, lbl_tcbResume_val, lbl_tcbBindNotification_val, lbl_tcbUnbindNotification_val, lbl_tcbSetBreakpoint_val, lbl_tcbGetBreakpoint_val, lbl_tcbUnsetBreakpoint_val, lbl_tcbConfigureSingleStepping_val, lbl_tcbSetTLSBase_val, lbl_tcbSetFlags_val, lbl_tcbReadFPU_val, lbl_tcbWriteFPU_val, lbl_tcbSetTimer_val, + h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13, h14, h15, h16, h17, h18, h19, h20, h21, h22, h23, h24, ↓reduceIte] + refine (WP.spec_ok _).2 (Or.inl ⟨_, rfl, rfl, rfl, ⟨.illegalOperation, ?_, Or.inl trivial⟩⟩) + show (Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) slbl args (.threadCap p.bv) sl excaps buffer).run σ = _ + rw [fl4DecodeTcb_spec' (by rw [hg]; decide) (by rw [hext hg]; simp [tcbExtOf, h16, h17, h18, h19, h22, h23, h24])] + apply run_liftME_err + unfold Fl4Spec.decodeTcbInvocation + rw [hg] + rfl + +end + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} +variable {Lst : U64 → List U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- **Every second-phase label of a selected Thread cap but SetEPTRoot** (module docstring): route F through the +adapters of P3/VmEntryS3ThrAdapt, route D by the VM-record installs, Suspend by `selectedThreadSuspendS3`, the other +labels `IllegalOperation`. [J] `SpecGlob`: no cap but a frame cap names the idle thread. -/ +theorem VmEptState.selectedThreadS3 [P1.PlainStore st] [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hK : ARKeep arI AR) (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) + (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) + (hM : MachPlat arI MR) (hPA : PPPlatA arI AR) (hT : TimeAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hmb : ∀ a', sporadic.min_budget arI a' ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hticks : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ a' v, arI.ticks_to_us a' v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hsg : SpecGlob σ) (hmv : MRV MR a σ) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d prio, σ.curThread ∉ σ.readyQueues d prio) + {ct cptr len buf lbl p : U64} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) (hl : len.val ≤ 120) + (buffer : Option Fl4Spec.ObjRef) (hA5 : ArgRel (st := st) s σ ct buf buffer 5) + (hNat : lbl ≠ fl4_abi.label.TCBSetEPTRoot) + (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) : + syscall.decode_invocation st arI s a lbl len (cslotAddr sl) (.Thread p) blk call don false buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blk call don + (specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) ct s res ⦄ := by + have B := J.1.1 + have hPl := B.xtop.rel.1 + have hxS : IsSlot Z.th (cslotAddr sl) := q.absSlot.isSlot + have hsc : capV (L.mem s) (cslotAddr sl) = .Thread p := capV_of_capAt q.capAt + have hp : Z.th p = some .tcb := B.xtop.rel.2.1.2.1.abs q.absSlot q.capAt + have hpT : p ∈ Z.T := B.thT p hp + have hstall := P1.stall_none (L := L) arI s a p (B.selectedThreadLocal q) + have hidS : ∀ (sl' : Fl4Spec.CslotPtr) r, AbsSlot Z.th sl' → capV (L.mem s) (cslotAddr sl') = .Thread r → + r ≠ L.idleW s := by + intro sl' r hAs' hsc' e + obtain ⟨c0, ac0, hc0, hca0, hg0⟩ := B.xtop.rel.2.1.1.abs_cap hAs' + rw [capAt_capV, hsc'] at hc0; cases eq_of_ok_eq hc0 + rw [hca0.abs] at hg0 + have ha0 := (getCap_set_iff σ sl'.1 sl'.2 _).1 (congrArg Prod.fst hg0) + apply hsg.idle sl'.1 sl'.2 _ ha0 (by simp [IsPageCap, absCap]) + rw [B.idleThread_eq, ← e]; simp [absCap, Fl4Spec.objRefs] + have hpI : p ≠ L.idleW s := hidS sl p q.absSlot hsc + -- the arguments + obtain ⟨argsU, hargsU, hlen, hargs⟩ : ∃ argsU : List U64, argsU = args.map (fun x => (⟨x⟩ : U64)) ∧ len.val = argsU.length ∧ + ∀ (i : U64) x, argsU[i.val]? = some x → msg.syscall_arg st s ct buf i = ok x := + ⟨_, rfl, q.length, q.words⟩ + have hm : args = argsU.map UScalar.bv := by rw [hargsU]; exact (zv_map_mk args).symm + -- the extra caps + have hx0 := exAt_idx0 ex + have hx1 := exAt_idx1 ex + have hx2 := exAt_idx2 ex + have hXc := ExtraOK.xc hPl q.extra + have hnc : NcHyp (st := st) Z.th s σ (exAt ex 0) (exAt ex 1) (exAt ex 2) := by + obtain ⟨-, -, hM', hC, hI⟩ := B.xtop.rel.2.1 + intro x _ sl0 hAs0 hx hu + subst hx + refine noChildren_of_adj Z.th hPl s σ Z.G hM' hC (hI.parentAdj (th := Z.th)) sl0 hAs0 + ((hM'.members _).2 ⟨hAs0.isSlot, capV (L.mem s) (cslotAddr sl0), capAt_capV _ _, ?_⟩) + intro h0; rw [h0] at hu; simp [isUt] at hu + have hcapT : exAt ex 0 ≠ 0#u64 → ∀ r, capV (L.mem s) (exAt ex 0) = .Thread r → r ∈ Z.T := by + intro hx r hc + have h0 := hXc.2 0 _ rfl + obtain ⟨e, he⟩ : ∃ e, exs[0]? = some e := by + rcases exs with _ | ⟨e, rest⟩ + · exact absurd (h0.1.2 (by simp)) hx + · exact ⟨e, rfl⟩ + obtain ⟨hxe, -, ps, hps, hpe⟩ := h0.2 e he + have hA0 : AbsSlot Z.th e.2 := ⟨ps, hpe, Or.inl hps⟩ + exact B.thT r (show CapValid Z.th (.Thread r) from + B.xtop.rel.2.1.2.1.abs hA0 (by rw [← hxe, capAt_capV, hc])) + have hcapC : exAt ex 1 ≠ 0#u64 → ∀ r b, capV (L.mem s) (exAt ex 1) = .SchedContext r b → r ∈ Z.C := by + intro h1 r b hq + obtain ⟨hz, he⟩ := hXc.2 1 _ rfl + rcases hee : exs[1]? with _ | e + · exact absurd (hz.2 (List.getElem?_eq_none_iff.1 hee)) h1 + · obtain ⟨hxe, -, ps, hps, hps1⟩ := he e hee + have hAs1 : AbsSlot Z.th e.2 := ⟨ps, hps1, Or.inl hps⟩ + rw [hxe] at hq + exact (B.scCtxCapB arI hAs1.isSlot (by rw [capAt_capV, hq])).1 + -- WriteRegisters: the caller's mapped buffer, the arguments after its restart + have hcur : σ.curThread = ct.bv := (B.curEq q.current).1 + have hctH : Z.th ct = some .tcb := (B.xtop.curTcb hcur).1 + have hctT : ct ∈ Z.T := B.thT ct hctH + have hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp := by + intro hb0 + obtain ⟨x, hk, hwx⟩ := B.buf ct hctT + have hL := lookup_buf_full s σ B.xtop.rel.2.1.1 B.xtop.rel.2.1.2.1 hctH hk (B.xferH.1) false + rw [hbuf, WP.spec_ok] at hL + cases hq : Endp.ipcBufOf false x with + | none => exact absurd (hL.1.2 hq) hb0 + | some r => exact ⟨r, B.xferH.2.1 ct hctT x false r hk hq buf (hL.2 hwx r hq)⟩ + have hWR : ∀ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + ∀ (i : U64) x, argsU[i.val]? = some x → msg.syscall_arg st s1 ct buf i = ok x := by + intro s1 h1 i x hx + obtain ⟨hi, -⟩ := List.getElem?_eq_some_iff.1 hx + obtain ⟨sp, hsp⟩ : ∃ sp, buf ≠ 0#u64 → BufMap Z.B buf sp := by + by_cases hb : buf = 0#u64 + · exact ⟨0, fun h => absurd hb h⟩ + · obtain ⟨sp, h⟩ := hbm hb; exact ⟨sp, fun _ => h⟩ + rw [B.argsRestart arI (B.xferH.2.2) hctT hctH hsp (by omega) h1] + exact hargs i x hx + -- route D: the installs' invariants and the caller's restart + have hPrem : VmDelPrem (st := st) arI AR MR := + ⟨hRef, ⟨P1.LawfulStore.total (st := st), hAT, hKDAR⟩, hK, hVA, hticks, hnd, hM.toC, hKD, hPA, hAM⟩ + have hHp : TcbInsWV (L := L) arI AR MR Z H Cs (fun kb => WMR MR (WVE (L := L) arI AR Z H Cs kb)) + (fun kb => WpVE (L := L) arI AR MR Z H Cs kb) := ⟨fun kb => insWV_ve arI hPrem kb⟩ + have hP0 := B.ppr0_start arI hni hRd0 hnq + have hR : RestartWJ (st := st) Z s a σ (WMR MR (WVE (L := L) arI AR Z H Cs (L.ksb s))) := by + intro ct' hct' s1 σ1 h1 h2 + obtain ⟨hcur', x, hk, hrun⟩ := B.curRunnable hni hct' + obtain ⟨s1', σ1', hst, e, hs1, Ac1, Lst1, D1, B1, F1⟩ := B.restartRunJ hct' hk hrun + have es : s1' = s1 := ok_inj (hs1.symm.trans h1) + have eσ : σ1' = σ1 := + (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst (hst.symm.trans h2)))).2 + subst es eσ + have hns1 : Vs.NoSwitch σ1' := by + rw [e]; exact SVK.noSwitch_sts (fun _ e' => by rw [hni.2] at e'; cases e') _ _ + have J1 := J.ofNat (V' := Z.vs) B1 F1 + exact ⟨⟨V, X, XL, Z.vs, Ac1, Lst1, D1, ⟨J1, hns1, SpecGlob.sts (mem_of_single h2) hsg, + hP0.of_ppk (e ▸ PPK.restart hk .restart (Or.inl hcur'.symm))⟩, F1.fw.1⟩, + mrv_keep hM hmv (MachT.mtk_setThreadState' _ _ σ ((), σ1') (mem_of_single h2))⟩ + -- the label + obtain ⟨sl', hlbl, hext, hdt⟩ := tcb_canon lbl + have hdV : specDecV false lbl.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer = + Fl4Spec.NonDetE.liftME Fl4Spec.Fl4InvocationV.base + (specDec false sl' args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) := by + rw [← specDec_thread p hdt] + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base false _ _ _ _ _ _ _ (by + simp [absCap, Fl4Spec.VirtRoute, dataToNat_bv, Fl4Spec.fl4SetEptRootLabel] + exact fun h => hNat (UScalar.eq_of_val_eq (h.trans lbl_tcbSetEPTRoot_val.symm)))] + by_cases cTCBReadRegisters : lbl = fl4_abi.label.TCBReadRegisters + · subst cTCBReadRegisters + refine WP.spec_mono (J.thrPostR q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbReadRegsF s a σ hni p hpT hpI sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall call (cslotAddr sl) ex blk don (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inr h + by_cases cTCBWriteRegisters : lbl = fl4_abi.label.TCBWriteRegisters + · subst cTCBWriteRegisters + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbWriteRegsF hAT hmb s a σ hni p hpT (cslotAddr sl) hxS q.capAt sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hbm hWR hstall call (cslotAddr sl) ex blk don (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inl h + by_cases cTCBCopyRegisters : lbl = fl4_abi.label.TCBCopyRegisters + · subst cTCBCopyRegisters + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbCopyRegsF hAT hmb s a σ hni hidS p hpT sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs call sl q.absSlot hsc ex _ _ _ hx0 exs hXc blk don (Fl4Spec.toBl cptr.bv) buffer)) fun _ h => Or.inl h + by_cases cTCBSetPriority : lbl = fl4_abi.label.TCBSetPriority + · subst cTCBSetPriority + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbSetPrioF s a σ hni p hpT sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall call sl ex _ _ _ hx0 exs hXc hcapT blk don (Fl4Spec.toBl cptr.bv) buffer)) fun _ h => Or.inl h + by_cases cTCBSetMCPriority : lbl = fl4_abi.label.TCBSetMCPriority + · subst cTCBSetMCPriority + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbSetMcpF s a σ hni p hpT sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall call sl ex _ _ _ hx0 exs hXc hcapT blk don (Fl4Spec.toBl cptr.bv) buffer)) fun _ h => Or.inl h + by_cases cTCBResume : lbl = fl4_abi.label.TCBResume + · subst cTCBResume + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (B.tcbResumeF hAT hmb s a σ hni p hpT (cslotAddr sl) hxS q.capAt sl' (by rw [hlbl]; simp [tcbLabelOf]) hstall ct q.current _ blk call don args (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inl h + by_cases cTCBBindNotification : lbl = fl4_abi.label.TCBBindNotification + · subst cTCBBindNotification + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (B.tcbBindNtfnF s a σ hni p hpT sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf hstall ct q.current hxS hsc call (cslotAddr sl) ex _ _ _ hx0 exs hXc blk don args (Fl4Spec.toBl cptr.bv) sl buffer)) fun _ h => Or.inl h + by_cases cTCBUnbindNotification : lbl = fl4_abi.label.TCBUnbindNotification + · subst cTCBUnbindNotification + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (B.tcbUnbindNtfnF s a σ hni p hpT sl' (by rw [hlbl]; simp [tcbLabelOf]) hstall ct q.current len buf call (cslotAddr sl) ex blk don args (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inl h + by_cases cTCBSetTLSBase : lbl = fl4_abi.label.TCBSetTLSBase + · subst cTCBSetTLSBase + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbSetTlsF s a σ hni p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs B.cwOut hstall call (cslotAddr sl) ex blk don (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inl h + by_cases cTCBSetFlags : lbl = fl4_abi.label.TCBSetFlags + · subst cTCBSetFlags + refine WP.spec_mono (J.thrPostRF q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbSetFlagsF s a σ hni (FlagsRel.of_mcp B.mcp) p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall hK call (cslotAddr sl) ex blk don (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inr h + by_cases cTCBSetBreakpoint : lbl = fl4_abi.label.TCBSetBreakpoint + · subst cTCBSetBreakpoint + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBSetBreakpoint = some .setBreakpoint by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostF q _ sl' buffer on rc blk call don hdV + (B.tcbSetBpF s a σ hni p hpT sl' hg he len buf ct q.current hstall buffer hA5 call (cslotAddr sl) ex blk don args (Fl4Spec.toBl cptr.bv) sl exs)) fun _ h => Or.inl h + by_cases cTCBGetBreakpoint : lbl = fl4_abi.label.TCBGetBreakpoint + · subst cTCBGetBreakpoint + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBGetBreakpoint = some .getBreakpoint by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostRF q _ sl' buffer on rc blk call don hdV + (B.tcbGetBpF s a σ hni p sl' hg he len buf ct q.current hstall buffer (fun i hi => hA5 i (by omega)) (getBpHyp_of s ct p) call (cslotAddr sl) ex blk don args (Fl4Spec.toBl cptr.bv) sl exs)) fun _ h => Or.inr h + by_cases cTCBUnsetBreakpoint : lbl = fl4_abi.label.TCBUnsetBreakpoint + · subst cTCBUnsetBreakpoint + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBUnsetBreakpoint = some .unsetBreakpoint by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostF q _ sl' buffer on rc blk call don hdV + (B.tcbUnsetBpF s a σ hni p hpT sl' hg he len buf ct q.current hstall buffer (fun i hi => hA5 i (by omega)) call (cslotAddr sl) ex blk don args (Fl4Spec.toBl cptr.bv) sl exs)) fun _ h => Or.inl h + by_cases cTCBConfigureSingleStepping : lbl = fl4_abi.label.TCBConfigureSingleStepping + · subst cTCBConfigureSingleStepping + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBConfigureSingleStepping = some .configureSingleStepping by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostRF q _ sl' buffer on rc blk call don hdV + (B.tcbSingleStepF s a σ hni p hp sl' hg he len buf ct q.current hstall buffer (fun i hi => hA5 i (by omega)) call (cslotAddr sl) ex blk don args (Fl4Spec.toBl cptr.bv) sl exs)) fun _ h => Or.inr h + by_cases cTCBReadFPU : lbl = fl4_abi.label.TCBReadFPU + · subst cTCBReadFPU + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBReadFPU = some .readFpu by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostFX q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbReadFpuF s a σ hni hK p hpT sl' hg he len buf ct q.current argsU hlen hargs hstall call (cslotAddr sl) ex _ _ _ hx0 exs hXc (B.fpuFrameJ _) blk don (Fl4Spec.toBl cptr.bv) sl buffer)) fun _ h => Or.inl h + by_cases cTCBWriteFPU : lbl = fl4_abi.label.TCBWriteFPU + · subst cTCBWriteFPU + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBWriteFPU = some .writeFpu by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostFX q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbWriteFpuF s a σ hni hK p hpT sl' hg he len buf ct q.current argsU hlen hargs hstall call (cslotAddr sl) ex _ _ _ hx0 exs hXc blk don (Fl4Spec.toBl cptr.bv) sl buffer)) fun _ h => Or.inl h + by_cases cTCBSetTimer : lbl = fl4_abi.label.TCBSetTimer + · subst cTCBSetTimer + obtain ⟨hg, he⟩ := ext_lbl hlbl hext (by simp [tcbLabelOf]) (show tcbExtOf fl4_abi.label.TCBSetTimer = some .setTimer by + simp [tcbExtOf]) + refine WP.spec_mono (J.thrPostF q _ sl' buffer on rc blk call don hdV + (by rw [hm]; exact B.tcbSetTimerF s a σ hni p hpT (cslotAddr sl) hxS q.capAt sl' hg he len buf ct q.current argsU hlen hargs hstall call (cslotAddr sl) ex blk don (Fl4Spec.toBl cptr.bv) sl exs buffer)) fun _ h => Or.inl h + by_cases cTCBSetTimeoutEndpoint : lbl = fl4_abi.label.TCBSetTimeoutEndpoint + · subst cTCBSetTimeoutEndpoint + rw [decode_invocation_thread_eq arI s a _ len (cslotAddr sl) p blk call don buf ex ct q.current, hdV] + refine WP.spec_mono ?_ fun _ h => Or.inl h + exact tcb_set_timeoutVE arI on rc s a σ J hni hsg hP0 hHp hR p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current hstall call sl q.absSlot ex _ _ _ hx0 exs hXc args blk don (Fl4Spec.toBl cptr.bv) buffer + by_cases cTCBSetSpace : lbl = fl4_abi.label.TCBSetSpace + · subst cTCBSetSpace + rw [decode_invocation_thread_eq arI s a _ len (cslotAddr sl) p blk call don buf ex ct q.current, hdV] + refine WP.spec_mono ?_ fun _ h => Or.inl h + rw [hm] + exact tcb_set_spaceVE arI on rc s a σ J hni hsg hP0 hHp hR p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall call sl q.absSlot ex _ _ _ hx0 hx1 hx2 exs hXc hnc blk don (Fl4Spec.toBl cptr.bv) buffer + by_cases cTCBSetIPCBuffer : lbl = fl4_abi.label.TCBSetIPCBuffer + · subst cTCBSetIPCBuffer + rw [decode_invocation_thread_eq arI s a _ len (cslotAddr sl) p blk call don buf ex ct q.current, hdV] + refine WP.spec_mono ?_ fun _ h => Or.inl h + rw [hm] + exact tcb_set_ipcVE arI on rc s a σ J hni hsg hP0 hHp hR p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall call sl q.absSlot ex _ _ _ hx0 exs hXc + (fun sl0 hA h hu => hnc _ (by simp) sl0 hA h hu) B.fnzJ blk don (Fl4Spec.toBl cptr.bv) buffer + by_cases cTCBConfigure : lbl = fl4_abi.label.TCBConfigure + · subst cTCBConfigure + rw [decode_invocation_thread_eq arI s a _ len (cslotAddr sl) p blk call don buf ex ct q.current, hdV] + refine WP.spec_mono ?_ fun _ h => Or.inl h + rw [hm] + exact tcb_configureVE arI on rc s a σ J hni hsg hP0 hHp hR p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hstall call sl q.absSlot ex _ _ _ hx0 hx1 hx2 exs hXc hnc B.fnzJ blk don (Fl4Spec.toBl cptr.bv) buffer + by_cases cTCBSetSchedParams : lbl = fl4_abi.label.TCBSetSchedParams + · subst cTCBSetSchedParams + rw [decode_invocation_thread_eq arI s a _ len (cslotAddr sl) p blk call don buf ex ct q.current, hdV] + refine WP.spec_mono ?_ fun _ h => Or.inl h + rw [hm] + exact tcb_set_sched_paramsVE arI on rc s a σ J hni hsg hP0 hHp hR hAT hK hT p hp sl' (by rw [hlbl]; simp [tcbLabelOf]) len buf ct q.current argsU hlen hargs hpI hstall call sl q.absSlot hsc ex _ _ _ hx0 hx1 hx2 exs hXc hcapT hcapC blk don (Fl4Spec.toBl cptr.bv) buffer + by_cases cS : lbl = fl4_abi.label.TCBSuspend + · subst cS + exact WP.spec_mono (J.selectedThreadSuspendS3 hni hsg q buffer on rc blk call don) fun _ h => Or.inl h + have hout : ¬ (2 ≤ lbl.val ∧ lbl.val ≤ 24) := by + have nReadRegisters : lbl.val ≠ 2 := fun e => cTCBReadRegisters (UScalar.eq_of_val_eq (e.trans lbl_tcbReadRegisters_val.symm)) + have nWriteRegisters : lbl.val ≠ 3 := fun e => cTCBWriteRegisters (UScalar.eq_of_val_eq (e.trans lbl_tcbWriteRegisters_val.symm)) + have nCopyRegisters : lbl.val ≠ 4 := fun e => cTCBCopyRegisters (UScalar.eq_of_val_eq (e.trans lbl_tcbCopyRegisters_val.symm)) + have nConfigure : lbl.val ≠ 5 := fun e => cTCBConfigure (UScalar.eq_of_val_eq (e.trans lbl_tcbConfigure_val.symm)) + have nSetPriority : lbl.val ≠ 6 := fun e => cTCBSetPriority (UScalar.eq_of_val_eq (e.trans lbl_tcbSetPriority_val.symm)) + have nSetMCPriority : lbl.val ≠ 7 := fun e => cTCBSetMCPriority (UScalar.eq_of_val_eq (e.trans lbl_tcbSetMCPriority_val.symm)) + have nSetSchedParams : lbl.val ≠ 8 := fun e => cTCBSetSchedParams (UScalar.eq_of_val_eq (e.trans lbl_tcbSetSchedParams_val.symm)) + have nSetTimeoutEndpoint : lbl.val ≠ 9 := fun e => cTCBSetTimeoutEndpoint (UScalar.eq_of_val_eq (e.trans lbl_tcbSetTimeoutEndpoint_val.symm)) + have nSetIPCBuffer : lbl.val ≠ 10 := fun e => cTCBSetIPCBuffer (UScalar.eq_of_val_eq (e.trans lbl_tcbSetIPCBuffer_val.symm)) + have nSetSpace : lbl.val ≠ 11 := fun e => cTCBSetSpace (UScalar.eq_of_val_eq (e.trans lbl_tcbSetSpace_val.symm)) + have nSuspend : lbl.val ≠ 12 := fun e => cS (UScalar.eq_of_val_eq (e.trans lbl_tcbSuspend_val.symm)) + have nResume : lbl.val ≠ 13 := fun e => cTCBResume (UScalar.eq_of_val_eq (e.trans lbl_tcbResume_val.symm)) + have nBindNotification : lbl.val ≠ 14 := fun e => cTCBBindNotification (UScalar.eq_of_val_eq (e.trans lbl_tcbBindNotification_val.symm)) + have nUnbindNotification : lbl.val ≠ 15 := fun e => cTCBUnbindNotification (UScalar.eq_of_val_eq (e.trans lbl_tcbUnbindNotification_val.symm)) + have nSetBreakpoint : lbl.val ≠ 16 := fun e => cTCBSetBreakpoint (UScalar.eq_of_val_eq (e.trans lbl_tcbSetBreakpoint_val.symm)) + have nGetBreakpoint : lbl.val ≠ 17 := fun e => cTCBGetBreakpoint (UScalar.eq_of_val_eq (e.trans lbl_tcbGetBreakpoint_val.symm)) + have nUnsetBreakpoint : lbl.val ≠ 18 := fun e => cTCBUnsetBreakpoint (UScalar.eq_of_val_eq (e.trans lbl_tcbUnsetBreakpoint_val.symm)) + have nConfigureSingleStepping : lbl.val ≠ 19 := fun e => cTCBConfigureSingleStepping (UScalar.eq_of_val_eq (e.trans lbl_tcbConfigureSingleStepping_val.symm)) + have nSetTLSBase : lbl.val ≠ 20 := fun e => cTCBSetTLSBase (UScalar.eq_of_val_eq (e.trans lbl_tcbSetTLSBase_val.symm)) + have nSetFlags : lbl.val ≠ 21 := fun e => cTCBSetFlags (UScalar.eq_of_val_eq (e.trans lbl_tcbSetFlags_val.symm)) + have nReadFPU : lbl.val ≠ 22 := fun e => cTCBReadFPU (UScalar.eq_of_val_eq (e.trans lbl_tcbReadFPU_val.symm)) + have nWriteFPU : lbl.val ≠ 23 := fun e => cTCBWriteFPU (UScalar.eq_of_val_eq (e.trans lbl_tcbWriteFPU_val.symm)) + have nSetTimer : lbl.val ≠ 24 := fun e => cTCBSetTimer (UScalar.eq_of_val_eq (e.trans lbl_tcbSetTimer_val.symm)) + omega + refine WP.spec_mono (J.thrPost q _ sl' buffer on rc blk call don hdV + (tcb_illegalThr arI s a σ lbl p _ sl' hlbl args (Fl4Spec.toBl cptr.bv) sl exs buffer hext hstall hNat hout)) + fun _ h => Or.inl h + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Thread.lean b/proofs/Fl4Proofs/P3/VmEntryS3Thread.lean new file mode 100644 index 000000000..d64b10bf3 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Thread.lean @@ -0,0 +1,92 @@ +import Fl4Proofs.P3.VmEntryS3Decode +import Fl4Proofs.P3.XTopJointTcb + +/-! The selected native TCB Suspend proof at the VM/EPT relation. -/ + +open Aeneas Aeneas.Std Result +namespace Fl4Proofs.P3 +open Classical fl4_core Vs Notif Endp Ipc Sched + +noncomputable section + +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {MR : A → Fl4Spec.MachineState → Prop} +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} +variable {Lst : U64 → List U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 6000000 in +/-- Suspend the TCB named by the actual invocation. [J] SpecGlob supplies +that the selected Thread cap cannot refer to the idle TCB. The shared +blocked IPC and scheduler steps preserve the VM data and match the +change to inactive, including when the target had been running a VM. -/ +theorem VmEptState.selectedThreadSuspendS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hsg : SpecGlob σ) + {ct cptr len buf p : U64} + {sl : Fl4Spec.CslotPtr} {ex : Array obj.Slot 3#usize} {args : List Fl4Spec.Data} + {exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + (q : VSelectedDecode st Z s σ ct cptr len buf sl (.Thread p) ex args exs) + (buffer : Option Fl4Spec.ObjRef) (on rc : Fl4Spec.ObjRef → Bool) (blk call don : Bool) : + syscall.decode_invocation st arI s a fl4_abi.label.TCBSuspend len (cslotAddr sl) (.Thread p) + blk call don false buf ex ⦃ res => + VDispPostVEX (L := L) arI AR MR on rc σ blk call don + (specDecV false fl4_abi.label.TCBSuspend.bv args (Fl4Spec.toBl cptr.bv) sl (.Thread p) exs buffer) s res ⦄ := by + have B := J.1.1 + have hp := B.thT p (B.xtop.rel.2.1.2.1.abs q.absSlot q.capAt) + have hid : p.bv ≠ σ.idleThread := by + intro e + obtain ⟨c, ac, hc, hca, hg⟩ := B.xtop.rel.2.1.1.abs_cap q.absSlot + rw [q.capAt] at hc + cases eq_of_ok_eq hc + rw [hca.abs] at hg + have hcap := (getCap_set_iff σ sl.1 sl.2 _).1 (congrArg Prod.fst hg) + apply hsg.idle sl.1 sl.2 _ hcap (by simp [IsPageCap, absCap]) + rw [← e] + simp [absCap, Fl4Spec.objRefs] + have hstall := P1.stall_none (L := L) arI s a p (B.selectedThreadLocal q) + obtain ⟨hcur, x, hk, hr⟩ := B.curRunnable hni q.current + obtain ⟨s1, σ1, hst, hσ1, hs1, Ac1, Lst1, D1, B1, F1⟩ := B.restartRunJ q.current hk hr + have hrestart : tcb.restart_caller st s = ok s1 := by + simp [tcb.restart_caller, q.current, bind_ok, hs1] + have hid1 : p.bv ≠ σ1.idleThread := by + rw [hσ1] + unfold Notif.stsAct + split <;> exact hid + have hns : Vs.NoSwitch σ1 := by + rw [hσ1] + exact SVK.noSwitch_sts (fun _ e => by rw [hni.2] at e; cases e) _ _ + have J1 := J.ofNat (V' := Z.vs) B1 F1 + have hlbl : Fl4Spec.genInvocationType fl4_abi.label.TCBSuspend.bv = .tcbSuspend := by + rw [tcb_in _ (by rw [lbl_tcbSuspend_val]; decide) (by rw [lbl_tcbSuspend_val]; decide)] + simp [tcbLabelOf] + have hdec : (specDec false fl4_abi.label.TCBSuspend.bv args (Fl4Spec.toBl cptr.bv) sl + (.Thread p) exs buffer).run σ = ({(.ok (.invokeTCB (.suspend p.bv)), σ)}, False) := by + show (Fl4Spec.fl4DecodeTcbInvocation (Z := Unit) fl4_abi.label.TCBSuspend.bv args + (.threadCap p.bv) sl exs buffer).run σ = _ + rw [fl4DecodeTcb_spec (by rw [hlbl]; decide) (by rw [hlbl]; decide)] + apply run_liftME_ok + unfold Fl4Spec.decodeTcbInvocation + rw [hlbl] + rfl + have hdecV : (specDecV false fl4_abi.label.TCBSuspend.bv args (Fl4Spec.toBl cptr.bv) sl + (.Thread p) exs buffer).run σ = ({(.ok (.base (.invokeTCB (.suspend p.bv))), σ)}, False) := by + unfold specDecV + rw [Fl4Spec.fl4DecodeInvocationV_base false _ _ _ _ _ _ _ (by simp [absCap, Fl4Spec.VirtRoute, dataToNat_bv, Fl4Spec.fl4SetEptRootLabel])] + exact run_liftME_ok hdec + unfold syscall.decode_invocation + rw [q.current] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + rw [decode_suspend_eq arI s a p _ hstall, hrestart] + simp only [bind_ok] + refine WP.spec_bind (JAtZB.suspendJD (jCancelB_send arI) (jCancelB_ntfn arI) + (jCancelB_recv arI) (jCancelB_reply arI) B1 hp (fun u e => (hns u e).elim) hid1) ?_ + rintro ⟨s2, a2⟩ ⟨-, σ2, hSus, V2, Ac2, Lst2, B2, F2⟩ + refine (WP.spec_ok _).2 (Or.inr ⟨_, DecOut.of_eq hdecV, σ1, hst, (.ok [], σ2), ?_, + Or.inl ⟨rfl, rfl, (J1.ofNat B2 F2).anyX arI⟩⟩) + show ((.ok [], σ2) : Except Unit (List Fl4Spec.Data) × Fl4Spec.State Unit) ∈ + ((Fl4Spec.NonDetE.liftE (do Fl4Spec.suspend p.bv; return ([] : List Fl4Spec.Data))).run σ1).1 + exact mem_liftE (x := ([], σ2)) (mem_bind_nd (x := ((), σ2)) (mem_of_single hSus) rfl) + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3TopG.lean b/proofs/Fl4Proofs/P3/VmEntryS3TopG.lean new file mode 100644 index 000000000..36a598245 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3TopG.lean @@ -0,0 +1,238 @@ +import Fl4Proofs.P3.VmKEntryX +import Fl4Proofs.P3.VmEntryS3Selected +import Fl4Proofs.P3.VmEntryS3Dispatch +import Fl4Proofs.P3.VmEntryS3Coverage + +/-! +P3 (line mmuAD, Stage 3, 3.3): `VmEptState.dispatchVG`, `dispatch_syscall` at a dispatch state with the +invocation cell an explicit hypothesis (generated from `dispatchV` by `handoff/tools/gen_dispvg.py`). +`dispatchV`'s old statement is unchanged. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +/-- Send / NBSend / Call invoke the cap register. -/ +theorem invokedCptr_cap {n : I64} {call bl don : Bool} (h : InvSys n call bl don) (s : S) (ct : U64) : + InvokesCap n ∧ L.mem s (regA ct layout.reg.CAP) = invokedCptr st s ct n := by + rcases h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ <;> + refine ⟨by simp [InvokesCap], ?_⟩ <;> + · unfold invokedCptr + rw [if_neg (by simp only [fl4_abi.syscall.SysSend, fl4_abi.syscall.SysNBSend, fl4_abi.syscall.SysCall, fl4_abi.syscall.SysReplyRecv, fl4_abi.syscall.SysNBSendRecv, fl4_abi.syscall.SysNBSendWait] <;> decide), + if_neg (by simp only [fl4_abi.syscall.SysSend, fl4_abi.syscall.SysNBSend, fl4_abi.syscall.SysCall, fl4_abi.syscall.SysReplyRecv, fl4_abi.syscall.SysNBSendRecv, fl4_abi.syscall.SysNBSendWait] <;> decide)] + +/-- ReplyRecv / NBSendWait invoke the reply register. -/ +theorem invokedCptr_reply {n : I64} (h : n = fl4_abi.syscall.SysReplyRecv ∨ n = fl4_abi.syscall.SysNBSendWait) + (s : S) (ct : U64) : InvokesCap n ∧ L.mem s (regA ct layout.reg.REPLY) = invokedCptr st s ct n := by + refine ⟨?_, by unfold invokedCptr; rw [if_pos h]⟩ + rcases h with rfl | rfl <;> simp [InvokesCap] + +/-- NBSendRecv invokes its destination register. -/ +theorem invokedCptr_dest (s : S) (ct : U64) : InvokesCap fl4_abi.syscall.SysNBSendRecv ∧ + L.mem s (regA ct layout.reg.NBSEND_RECV_DEST) = invokedCptr st s ct fl4_abi.syscall.SysNBSendRecv := by + refine ⟨by simp [InvokesCap], ?_⟩ + unfold invokedCptr + rw [if_neg (by simp only [fl4_abi.syscall.SysSend, fl4_abi.syscall.SysNBSend, fl4_abi.syscall.SysCall, fl4_abi.syscall.SysReplyRecv, fl4_abi.syscall.SysNBSendRecv, fl4_abi.syscall.SysNBSendWait] <;> decide), if_pos rfl] + +set_option maxHeartbeats 4000000 in +/-- **`dispatch_syscall` at `VmEptState` with the invocation cell supplied** (`dispatchV`'s sibling). -/ +theorem VmEptState.dispatchVG (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {on rc : Fl4Spec.ObjRef → Bool} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + {n : I64} {c : Fl4Spec.Syscall} (hn : NativeSys n c) + (hI : ∀ (call bl don fp : Bool) (cptr : U64), InvArgs n c call bl don fp → InvokesCap n → + cptr = invokedCptr st s ct n → + syscall.handle_invocation st arI s a call bl don fp cptr ⦃ res => + HIPostVEX (L := L) arI AR MR on rc σ call bl don fp cptr res ⦄) + (hO : ∀ (call bl don fp : Bool) (cptr : Fl4Spec.CapRef), InvArgs n c call bl don fp → + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp cptr).run σ).1, y.1 = .error () → + ∀ (s1 : S) (a1 : A), (∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s1 a1 y.2) → + VmOrc st on rc s1 y.2) + (hRecv : ∀ (bl cr : Bool), RecvSys n bl cr → syscall.handle_recv st arI s a bl cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) bl cr σ).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) + (hMid : ∀ (rg : U64) (sr : Fl4Spec.Register) (cr : Bool), MidSysR n c rg sr cr → ∀ (cptr : Fl4Spec.CapRef), + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true false false true true cptr).run σ).1, + y.1 = .ok () → ∀ (s1 : S) (a1 : A), VEAnyX (L := L) arI AR s1 a1 y.2 → + syscall.handle_recv st arI s1 a1 true cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) true cr y.2).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) + (hY : n = fl4_abi.syscall.SysYield → syscall.handle_yield st arI s a ⦃ r => + ∃ z ∈ (Fl4Spec.handleYield (Z := Unit) σ).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) : + syscall.dispatch_syscall st arI s a n ⦃ r => DispPostVEX (L := L) arI AR on rc c σ r ⦄ := by + have B := J.1.1 + obtain ⟨hcur, -⟩ := B.curEq hct + obtain ⟨-, xs, hks⟩ := B.xtop.curTcb hcur + have hctT : ct ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm + have hxs := (B.xtop.irelK B.ks B.buf).1.toE.at_tcb hctT hks + have hk' : σ.kheap σ.curThread = some (.tcb xs) := by rw [hcur]; exact hks + have hreg : ∀ r : Fl4Spec.Register, (Fl4Spec.toBl (L.mem s (regA ct (regIdx r))).bv, σ) ∈ + (Fl4Spec.getCapReg (Z := Unit) r σ).1 := by + intro r + have h := mem_getCapReg hk' r + rwa [show Fl4Spec.dataToCptr (xs.tcbArch.tcbContext.userRegs r) = + Fl4Spec.toBl (L.mem s (regA ct (regIdx r))).bv by rw [hxs.regs]; rfl] at h + have hAf : ∀ (call bl don fp : Bool) (cptr : U64), InvArgs n c call bl don fp → + ∀ res, HIPostVEX (L := L) arI AR MR on rc σ call bl don fp cptr res → + syscall.after_invocation st arI res.2.1 res.2.2 res.1 ⦃ r => InvAfterVEX (L := L) arI AR on rc + (Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp (Fl4Spec.toBl cptr.bv)) σ r ⦄ := + fun call bl don fp cptr hia res h => VmEptState.afterVX hARI hAT hVA hnd hM hMK hPA hIA hmrt hmax htu h + (hO call bl don fp _ hia) + have hC := hreg Fl4Spec.cap_register + rw [regIdx_cap] at hC + have hRp := hreg Fl4Spec.replyRegister + rw [regIdx_reply] at hRp + have hDs := hreg Fl4Spec.nbsendRecvDest + rw [regIdx_nbsendRecvDest] at hDs + unfold syscall.dispatch_syscall + rw [hct]; simp only [bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.CAP) ?_ + rintro c0 rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.REPLY) ?_ + rintro c1 rfl + rcases hn with ⟨call, bl, don, hi, rfl⟩ | ⟨bl, cr, hr, rfl⟩ | ⟨rfl, rfl⟩ | hm + · -- Send / NBSend / Call + have hia : InvArgs n (invSysOf call bl) call bl don false := Or.inl ⟨hi, rfl⟩ + have inv : (do + let (r, s1, a1) ← syscall.handle_invocation st arI s a call bl don false (L.mem s (regA ct layout.reg.CAP)) + let (_, s2, a2) ← syscall.after_invocation st arI s1 a1 r + ok (s2, a2)) ⦃ r => DispPostVEX (L := L) arI AR on rc (invSysOf call bl) σ r ⦄ := by + refine WP.spec_bind (hI call bl don false _ hia (invokedCptr_cap (st := st) hi s ct).1 (invokedCptr_cap (st := st) hi s ct).2) ?_ + rintro ⟨r, s1, a1⟩ h1 + refine WP.spec_bind (hAf call bl don false _ hia _ h1) ?_ + rintro ⟨b, s2, a2⟩ ⟨y, hy, ⟨-, hy1, hV⟩ | ⟨-, hy1, z, hz, hV⟩⟩ + · refine (WP.spec_ok _).2 ⟨y, ?_, Or.inl ⟨hy1, hV⟩⟩ + rcases hi with ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hC) hy + · refine (WP.spec_ok _).2 ⟨y, ?_, Or.inr ⟨hy1, z, hz, hV⟩⟩ + rcases hi with ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hC) hy + rcases hi with ⟨rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl⟩ + · rw [if_pos rfl]; exact inv + · rw [if_neg (show fl4_abi.syscall.SysNBSend ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBSend fl4_abi.syscall.SysSend; decide), if_pos rfl]; exact inv + · rw [if_neg (show fl4_abi.syscall.SysCall ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysCall fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysCall ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysCall fl4_abi.syscall.SysNBSend; decide), if_pos rfl]; exact inv + · -- Recv / NBRecv / Wait / NBWait + have rcv : syscall.handle_recv st arI s a bl cr ⦃ r => DispPostVEX (L := L) arI AR on rc (recvSysOf bl cr) σ r ⦄ := by + refine WP.spec_mono (hRecv bl cr hr) ?_ + rintro r ⟨z, hz, hV⟩ + refine ⟨(.ok (), z.2), ?_, Or.inl ⟨rfl, hV⟩⟩ + rcases hr with ⟨-, rfl, rfl⟩ | ⟨-, rfl, rfl⟩ | ⟨-, rfl, rfl⟩ | ⟨-, rfl, rfl⟩ <;> exact mem_liftE hz + rcases hr with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · rw [if_neg (show fl4_abi.syscall.SysRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysRecv fl4_abi.syscall.SysCall; decide), if_pos rfl]; exact rcv + · rw [if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysRecv; decide), if_pos rfl]; exact rcv + · rw [if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysNBRecv; decide), if_pos rfl]; exact rcv + · rw [if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysWait; decide), if_pos rfl]; exact rcv + · -- Yield + rw [if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBWait; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysReplyRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysReplyRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBSendRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBSendRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBSendWait by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBSendWait; decide), if_pos rfl] + refine WP.spec_mono (hY rfl) ?_ + rintro r ⟨z, hz, hV⟩ + exact ⟨(.ok (), z.2), mem_liftE hz, Or.inl ⟨rfl, hV⟩⟩ + · -- ReplyRecv / NBSendRecv / NBSendWait + obtain ⟨rg, sr, cr, hmr⟩ := midSysR_of hm + have hia : InvArgs n c false false true true := Or.inr ⟨hm, rfl, rfl, rfl, rfl⟩ + have mid : ∀ (cptr : U64), (Fl4Spec.toBl cptr.bv, σ) ∈ (Fl4Spec.getCapReg (Z := Unit) sr σ).1 → + InvokesCap n → cptr = invokedCptr st s ct n → + (do + let (r, s1, a1) ← syscall.handle_invocation st arI s a false false true true cptr + let (b, s2, a2) ← syscall.after_invocation st arI s1 a1 r + if b then syscall.handle_recv st arI s2 a2 true cr else ok (s2, a2)) ⦃ r => + DispPostVEX (L := L) arI AR on rc c σ r ⦄ := by + intro cptr hrg hic hcp + refine WP.spec_bind (hI false false true true _ hia hic hcp) ?_ + rintro ⟨r, s1, a1⟩ h1 + refine WP.spec_bind (hAf false false true true _ hia _ h1) ?_ + rintro ⟨b, s2, a2⟩ ⟨y, hy, ⟨hb, hy1, hV⟩ | ⟨hb, hy1, z, hz, hV⟩⟩ <;> dsimp only at hb <;> subst hb + · try rw [if_pos rfl] + refine WP.spec_mono (hMid rg sr cr hmr _ y hy hy1 s2 a2 hV) ?_ + rintro r ⟨z, hz, hVz⟩ + have hy' : ((.ok (), y.2) : Except Unit Unit × Fl4Spec.State Unit) ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) + on rc true false false true true (Fl4Spec.toBl cptr.bv)).run σ).1 := by rw [← hy1]; exact hy + refine ⟨(.ok (), z.2), ?_, Or.inl ⟨rfl, hVz⟩⟩ + rcases hmr with ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hrg) (mem_bindE_ok hy' (mem_liftE hz)) + · try rw [if_neg (show ¬ (false = true) by decide)] + have hy' : ((.error (), y.2) : Except Unit Unit × Fl4Spec.State Unit) ∈ ((Fl4Spec.fl4HandleInvocationVF + (Z := Unit) on rc true false false true true (Fl4Spec.toBl cptr.bv)).run σ).1 := by rw [← hy1]; exact hy + refine (WP.spec_ok _).2 ⟨(.error (), y.2), ?_, Or.inr ⟨rfl, z, hz, hV⟩⟩ + rcases hmr with ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hrg) (mem_bindE_err hy') + rcases hmr with ⟨rfl, rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl, rfl⟩ + · rw [if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBWait; decide), if_pos rfl]; exact mid _ hRp (invokedCptr_reply (st := st) (Or.inl rfl) s ct).1 (invokedCptr_reply (st := st) (Or.inl rfl) s ct).2 + · rw [if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysNBWait; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysReplyRecv by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysReplyRecv; decide), if_pos rfl] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.NBSEND_RECV_DEST) ?_ + rintro c2 rfl + exact mid _ hDs (invokedCptr_dest (st := st) s ct).1 (invokedCptr_dest (st := st) s ct).2 + · rw [if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBWait; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysReplyRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysReplyRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBSendRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBSendRecv; decide), if_pos rfl]; exact mid _ hRp (invokedCptr_reply (st := st) (Or.inr rfl) s ct).1 (invokedCptr_reply (st := st) (Or.inr rfl) s ct).2 + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3TopR.lean b/proofs/Fl4Proofs/P3/VmEntryS3TopR.lean new file mode 100644 index 000000000..d2ac1194c --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3TopR.lean @@ -0,0 +1,96 @@ +import Fl4Proofs.P3.VmEntryS3TopG + +/-! +P3 (line mmuAD, Stage 3, 3.3): `VmEptState.dispatchVR`, `dispatch_syscall` at a dispatch state whose invocation +decode is the reached one (`handleInvocationReached`: the lookup is the real `LookAt` of the invoked slot; +`dispatchReached` + `dispatchKnownSelected` supply the decoder), instead of `KEntryArmsV.disp`. + +* `DispFactsS3`: the dispatch-state facts `dispatchKnownSelected` reads, as one [J] record (the 3.4 start record + transported to the budget state); `core` is the [S] `SelectedCoreScope`. +No existing statement changed. No new premise beyond the record. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +/-- **[J] The dispatch-state facts of the selected-cap dispatcher** (`dispatchKnownSelected`'s state hypotheses). -/ +structure DispFactsS3 (arI : machine.Arch A) (MR : A → Fl4Spec.MachineState → Prop) (on rc : Fl4Spec.ObjRef → Bool) + (n : I64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop where + sg : SpecGlob σ + ports : SpecIoPorts σ + vih : SInvV.ValidIrqHandlers σ + air : ArchIrqRel arI a σ + mv : MRV MR a σ + q : ∀ e, EpQPre σ e + rd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → + Fl4Spec.scActive y = true → Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true + nf : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbFault = none + uz : SInvR.G5.SpecUtZ σ + df : SInvDV.DevRangeS σ + dp : SInvGp.GpmOutV σ + ut : SInvU.UtInv'' σ + orc : VmOrc st on rc s σ + /-- [S] the inherited core restrictions at the invoked cap: the executing core, the current thread and the + capability pointer system call `n` invokes. -/ + core : InvokesCap n → ∀ ct, sched.cur_thread st s = ok ct → + SelectedCoreScope (L.cpu s) (L.mem s) σ ct (invokedCptr st s ct n) + +variable (arI AR MR) + +set_option maxHeartbeats 8000000 in +/-- **`dispatch_syscall` at `VmEptState` with the reached decode** (module docstring). -/ +theorem VmEptState.dispatchVR [P1.PlainStore st] (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hVA : ZvVirtAgree arI) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) (hRef : ArchRefines arI AR) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) (hT : TimeAgree arI) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hag : ∀ a, ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hrcb : Fl4Spec.resetChunkBits = 16) + {on rc : Fl4Spec.ObjRef → Bool} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {n : I64} {c : Fl4Spec.Syscall} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hVal : PortInValAt arI a σ) (F : DispFactsS3 (st := st) arI MR on rc n s a σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hn : NativeSys n c) + (hO : ∀ (call bl don fp : Bool) (cptr : Fl4Spec.CapRef), InvArgs n c call bl don fp → + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp cptr).run σ).1, y.1 = .error () → + ∀ (s1 : S) (a1 : A), (∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s1 a1 y.2) → + VmOrc st on rc s1 y.2) + (hRecv : ∀ (bl cr : Bool), RecvSys n bl cr → syscall.handle_recv st arI s a bl cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) bl cr σ).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) + (hMid : ∀ (rg : U64) (sr : Fl4Spec.Register) (cr : Bool), MidSysR n c rg sr cr → ∀ (cptr : Fl4Spec.CapRef), + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true false false true true cptr).run σ).1, + y.1 = .ok () → ∀ (s1 : S) (a1 : A), VEAnyX (L := L) arI AR s1 a1 y.2 → + syscall.handle_recv st arI s1 a1 true cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) true cr y.2).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) + (hY : n = fl4_abi.syscall.SysYield → syscall.handle_yield st arI s a ⦃ r => + ∃ z ∈ (Fl4Spec.handleYield (Z := Unit) σ).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) : + syscall.dispatch_syscall st arI s a n ⦃ r => DispPostVEX (L := L) arI AR on rc c σ r ⦄ := by + refine J.dispatchVG hARI hAT hVA hnd hM hMK hPA hIA hmrt hmax htu hni hct hnq hvfh hn ?_ hO hRecv hMid hY + intro call bl don fp cptr _ hic hcp + refine J.handleInvocationReached hARI.toARKeep hAT hnd hM.toC hPA on rc hni hct hnq hvfh call bl don fp cptr ?_ + refine J.dispatchReached arI AR MR hni hRef hVA hARI.toARKeep hSy hCap hAT hARI.toMask hVal on rc hct hRun + call bl don fp cptr ?_ + refine VDispReachAtFor.native_of_selected ?_ + exact J.dispatchKnownSelected hni hRef hVA hARI.toARKeep hKDAR hKD hCap hAT hARI.toMask hVal hM hPA hT hnd hmb + ⟨hmax, htu⟩ (hag a) hfan hrcb F.sg F.ports F.vih F.air F.mv F.q F.rd0 hnq F.nf F.uz F.df F.dp F.ut F.orc hct hRun + call bl don fp cptr (by rw [hcp]; exact F.core hic ct hct) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3TopS.lean b/proofs/Fl4Proofs/P3/VmEntryS3TopS.lean new file mode 100644 index 000000000..d591631ee --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3TopS.lean @@ -0,0 +1,273 @@ +import Fl4Proofs.P3.VmEntryS3TopR +import Fl4Proofs.P3.VmRecvS + +/-! +P3 (line mmuAD, Stage 3, 3.5): `dispatchVGS` / `dispatchVRS`, the `VEAnyS` siblings of `dispatchVG` / `dispatchVR` +(generated by `handoff/tools/gen_dispvs.py`). A pending-switch receive after the first phase of ReplyRecv / +NBSendRecv / NBSendWait ends at an R state, so the receive and yield cells end in `VEAnyS`; every other arm lifts +its `VEAnyX` post by `Or.inl`. `DispPostVES` is `DispPostVEX` with `VEAnyS`. Existing statements unchanged. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +/-- **The dispatch's post with `VEAnyS`.** -/ +def DispPostVES {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + (AR : A → Fl4Spec.Cr3 → Prop) (on rc : Fl4Spec.ObjRef → Bool) (c : Fl4Spec.Syscall) (σ : Fl4Spec.State Unit) + (r : S × A) : Prop := + ∃ y ∈ ((Fl4Spec.fl4HandleSyscallVF (Z := Unit) on rc true c).run σ).1, + (y.1 = .ok () ∧ VEAnyS (L := L) arI AR r.1 r.2 y.2) ∨ + (y.1 = .error () ∧ ∃ z ∈ (Fl4Spec.preemptionPathVF (Z := Unit) on rc y.2).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2) + +set_option maxHeartbeats 4000000 in +/-- **`dispatchVG` with `VEAnyS` posts** (module docstring). -/ +theorem VmEptState.dispatchVGS (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {on rc : Fl4Spec.ObjRef → Bool} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + {n : I64} {c : Fl4Spec.Syscall} (hn : NativeSys n c) + (hI : ∀ (call bl don fp : Bool) (cptr : U64), InvArgs n c call bl don fp → InvokesCap n → + cptr = invokedCptr st s ct n → + syscall.handle_invocation st arI s a call bl don fp cptr ⦃ res => + HIPostVEX (L := L) arI AR MR on rc σ call bl don fp cptr res ⦄) + (hO : ∀ (call bl don fp : Bool) (cptr : Fl4Spec.CapRef), InvArgs n c call bl don fp → + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp cptr).run σ).1, y.1 = .error () → + ∀ (s1 : S) (a1 : A), (∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s1 a1 y.2) → + VmOrc st on rc s1 y.2) + (hRecv : ∀ (bl cr : Bool), RecvSys n bl cr → syscall.handle_recv st arI s a bl cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) bl cr σ).1, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄) + (hMid : ∀ (rg : U64) (sr : Fl4Spec.Register) (cr : Bool), MidSysR n c rg sr cr → ∀ (cptr : Fl4Spec.CapRef), + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true false false true true cptr).run σ).1, + y.1 = .ok () → ∀ (s1 : S) (a1 : A), VEAnyX (L := L) arI AR s1 a1 y.2 → + syscall.handle_recv st arI s1 a1 true cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) true cr y.2).1, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄) + (hY : n = fl4_abi.syscall.SysYield → syscall.handle_yield st arI s a ⦃ r => + ∃ z ∈ (Fl4Spec.handleYield (Z := Unit) σ).1, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄) : + syscall.dispatch_syscall st arI s a n ⦃ r => DispPostVES (L := L) arI AR on rc c σ r ⦄ := by + have B := J.1.1 + obtain ⟨hcur, -⟩ := B.curEq hct + obtain ⟨-, xs, hks⟩ := B.xtop.curTcb hcur + have hctT : ct ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm + have hxs := (B.xtop.irelK B.ks B.buf).1.toE.at_tcb hctT hks + have hk' : σ.kheap σ.curThread = some (.tcb xs) := by rw [hcur]; exact hks + have hreg : ∀ r : Fl4Spec.Register, (Fl4Spec.toBl (L.mem s (regA ct (regIdx r))).bv, σ) ∈ + (Fl4Spec.getCapReg (Z := Unit) r σ).1 := by + intro r + have h := mem_getCapReg hk' r + rwa [show Fl4Spec.dataToCptr (xs.tcbArch.tcbContext.userRegs r) = + Fl4Spec.toBl (L.mem s (regA ct (regIdx r))).bv by rw [hxs.regs]; rfl] at h + have hAf : ∀ (call bl don fp : Bool) (cptr : U64), InvArgs n c call bl don fp → + ∀ res, HIPostVEX (L := L) arI AR MR on rc σ call bl don fp cptr res → + syscall.after_invocation st arI res.2.1 res.2.2 res.1 ⦃ r => InvAfterVEX (L := L) arI AR on rc + (Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp (Fl4Spec.toBl cptr.bv)) σ r ⦄ := + fun call bl don fp cptr hia res h => VmEptState.afterVX hARI hAT hVA hnd hM hMK hPA hIA hmrt hmax htu h + (hO call bl don fp _ hia) + have hC := hreg Fl4Spec.cap_register + rw [regIdx_cap] at hC + have hRp := hreg Fl4Spec.replyRegister + rw [regIdx_reply] at hRp + have hDs := hreg Fl4Spec.nbsendRecvDest + rw [regIdx_nbsendRecvDest] at hDs + unfold syscall.dispatch_syscall + rw [hct]; simp only [bind_ok] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.CAP) ?_ + rintro c0 rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.REPLY) ?_ + rintro c1 rfl + rcases hn with ⟨call, bl, don, hi, rfl⟩ | ⟨bl, cr, hr, rfl⟩ | ⟨rfl, rfl⟩ | hm + · -- Send / NBSend / Call + have hia : InvArgs n (invSysOf call bl) call bl don false := Or.inl ⟨hi, rfl⟩ + have inv : (do + let (r, s1, a1) ← syscall.handle_invocation st arI s a call bl don false (L.mem s (regA ct layout.reg.CAP)) + let (_, s2, a2) ← syscall.after_invocation st arI s1 a1 r + ok (s2, a2)) ⦃ r => DispPostVES (L := L) arI AR on rc (invSysOf call bl) σ r ⦄ := by + refine WP.spec_bind (hI call bl don false _ hia (invokedCptr_cap (st := st) hi s ct).1 (invokedCptr_cap (st := st) hi s ct).2) ?_ + rintro ⟨r, s1, a1⟩ h1 + refine WP.spec_bind (hAf call bl don false _ hia _ h1) ?_ + rintro ⟨b, s2, a2⟩ ⟨y, hy, ⟨-, hy1, hV⟩ | ⟨-, hy1, z, hz, hV⟩⟩ + · refine (WP.spec_ok _).2 ⟨y, ?_, Or.inl ⟨hy1, Or.inl hV⟩⟩ + rcases hi with ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hC) hy + · refine (WP.spec_ok _).2 ⟨y, ?_, Or.inr ⟨hy1, z, hz, Or.inl hV⟩⟩ + rcases hi with ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ | ⟨-, rfl, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hC) hy + rcases hi with ⟨rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl⟩ + · rw [if_pos rfl]; exact inv + · rw [if_neg (show fl4_abi.syscall.SysNBSend ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBSend fl4_abi.syscall.SysSend; decide), if_pos rfl]; exact inv + · rw [if_neg (show fl4_abi.syscall.SysCall ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysCall fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysCall ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysCall fl4_abi.syscall.SysNBSend; decide), if_pos rfl]; exact inv + · -- Recv / NBRecv / Wait / NBWait + have rcv : syscall.handle_recv st arI s a bl cr ⦃ r => DispPostVES (L := L) arI AR on rc (recvSysOf bl cr) σ r ⦄ := by + refine WP.spec_mono (hRecv bl cr hr) ?_ + rintro r ⟨z, hz, hV⟩ + refine ⟨(.ok (), z.2), ?_, Or.inl ⟨rfl, hV⟩⟩ + rcases hr with ⟨-, rfl, rfl⟩ | ⟨-, rfl, rfl⟩ | ⟨-, rfl, rfl⟩ | ⟨-, rfl, rfl⟩ <;> exact mem_liftE hz + rcases hr with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · rw [if_neg (show fl4_abi.syscall.SysRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysRecv fl4_abi.syscall.SysCall; decide), if_pos rfl]; exact rcv + · rw [if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBRecv ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBRecv fl4_abi.syscall.SysRecv; decide), if_pos rfl]; exact rcv + · rw [if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysWait ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysWait fl4_abi.syscall.SysNBRecv; decide), if_pos rfl]; exact rcv + · rw [if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysNBWait ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysNBWait fl4_abi.syscall.SysWait; decide), if_pos rfl]; exact rcv + · -- Yield + rw [if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBWait; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysReplyRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysReplyRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBSendRecv by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBSendRecv; decide), if_neg (show fl4_abi.syscall.SysYield ≠ fl4_abi.syscall.SysNBSendWait by + unfold fl4_abi.syscall.SysYield fl4_abi.syscall.SysNBSendWait; decide), if_pos rfl] + refine WP.spec_mono (hY rfl) ?_ + rintro r ⟨z, hz, hV⟩ + exact ⟨(.ok (), z.2), mem_liftE hz, Or.inl ⟨rfl, hV⟩⟩ + · -- ReplyRecv / NBSendRecv / NBSendWait + obtain ⟨rg, sr, cr, hmr⟩ := midSysR_of hm + have hia : InvArgs n c false false true true := Or.inr ⟨hm, rfl, rfl, rfl, rfl⟩ + have mid : ∀ (cptr : U64), (Fl4Spec.toBl cptr.bv, σ) ∈ (Fl4Spec.getCapReg (Z := Unit) sr σ).1 → + InvokesCap n → cptr = invokedCptr st s ct n → + (do + let (r, s1, a1) ← syscall.handle_invocation st arI s a false false true true cptr + let (b, s2, a2) ← syscall.after_invocation st arI s1 a1 r + if b then syscall.handle_recv st arI s2 a2 true cr else ok (s2, a2)) ⦃ r => + DispPostVES (L := L) arI AR on rc c σ r ⦄ := by + intro cptr hrg hic hcp + refine WP.spec_bind (hI false false true true _ hia hic hcp) ?_ + rintro ⟨r, s1, a1⟩ h1 + refine WP.spec_bind (hAf false false true true _ hia _ h1) ?_ + rintro ⟨b, s2, a2⟩ ⟨y, hy, ⟨hb, hy1, hV⟩ | ⟨hb, hy1, z, hz, hV⟩⟩ <;> dsimp only at hb <;> subst hb + · try rw [if_pos rfl] + refine WP.spec_mono (hMid rg sr cr hmr _ y hy hy1 s2 a2 hV) ?_ + rintro r ⟨z, hz, hVz⟩ + have hy' : ((.ok (), y.2) : Except Unit Unit × Fl4Spec.State Unit) ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) + on rc true false false true true (Fl4Spec.toBl cptr.bv)).run σ).1 := by rw [← hy1]; exact hy + refine ⟨(.ok (), z.2), ?_, Or.inl ⟨rfl, hVz⟩⟩ + rcases hmr with ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hrg) (mem_bindE_ok hy' (mem_liftE hz)) + · try rw [if_neg (show ¬ (false = true) by decide)] + have hy' : ((.error (), y.2) : Except Unit Unit × Fl4Spec.State Unit) ∈ ((Fl4Spec.fl4HandleInvocationVF + (Z := Unit) on rc true false false true true (Fl4Spec.toBl cptr.bv)).run σ).1 := by rw [← hy1]; exact hy + refine (WP.spec_ok _).2 ⟨(.error (), y.2), ?_, Or.inr ⟨rfl, z, hz, Or.inl hV⟩⟩ + rcases hmr with ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ | ⟨-, rfl, -, rfl, rfl⟩ <;> + exact mem_bindE_ok (mem_liftE hrg) (mem_bindE_err hy') + rcases hmr with ⟨rfl, rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl, rfl, rfl⟩ + · rw [if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysReplyRecv ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBWait; decide), if_pos rfl]; exact mid _ hRp (invokedCptr_reply (st := st) (Or.inl rfl) s ct).1 (invokedCptr_reply (st := st) (Or.inl rfl) s ct).2 + · rw [if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysNBWait; decide), if_neg (show fl4_abi.syscall.SysNBSendRecv ≠ fl4_abi.syscall.SysReplyRecv by + unfold fl4_abi.syscall.SysNBSendRecv fl4_abi.syscall.SysReplyRecv; decide), if_pos rfl] + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s ct layout.reg.NBSEND_RECV_DEST) ?_ + rintro c2 rfl + exact mid _ hDs (invokedCptr_dest (st := st) s ct).1 (invokedCptr_dest (st := st) s ct).2 + · rw [if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysSend by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysSend; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBSend by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBSend; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysCall by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysCall; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysWait by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysWait; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBWait by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBWait; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysReplyRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysReplyRecv; decide), if_neg (show fl4_abi.syscall.SysNBSendWait ≠ fl4_abi.syscall.SysNBSendRecv by + unfold fl4_abi.syscall.SysNBSendWait fl4_abi.syscall.SysNBSendRecv; decide), if_pos rfl]; exact mid _ hRp (invokedCptr_reply (st := st) (Or.inr rfl) s ct).1 (invokedCptr_reply (st := st) (Or.inr rfl) s ct).2 + +variable (arI AR MR) + +set_option maxHeartbeats 8000000 in +/-- **`dispatchVR` with `VEAnyS` posts** (module docstring). -/ +theorem VmEptState.dispatchVRS [P1.PlainStore st] (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hVA : ZvVirtAgree arI) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hKDAR : MRKeepD arI AR) (hKD : MRKeepD arI MR) (hRef : ArchRefines arI AR) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) (hT : TimeAgree arI) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (hag : ∀ a, ∀ r, arI.virtualization a = ok r → r.val = Fl4Spec.virtKind.code) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hrcb : Fl4Spec.resetChunkBits = 16) + {on rc : Fl4Spec.ObjRef → Bool} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {n : I64} {c : Fl4Spec.Syscall} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hVal : PortInValAt arI a σ) (F : DispFactsS3 (st := st) arI MR on rc n s a σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (hn : NativeSys n c) + (hO : ∀ (call bl don fp : Bool) (cptr : Fl4Spec.CapRef), InvArgs n c call bl don fp → + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true call bl don fp cptr).run σ).1, y.1 = .error () → + ∀ (s1 : S) (a1 : A), (∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s1 a1 y.2) → + VmOrc st on rc s1 y.2) + (hRecv : ∀ (bl cr : Bool), RecvSys n bl cr → syscall.handle_recv st arI s a bl cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) bl cr σ).1, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄) + (hMid : ∀ (rg : U64) (sr : Fl4Spec.Register) (cr : Bool), MidSysR n c rg sr cr → ∀ (cptr : Fl4Spec.CapRef), + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true false false true true cptr).run σ).1, + y.1 = .ok () → ∀ (s1 : S) (a1 : A), VEAnyX (L := L) arI AR s1 a1 y.2 → + syscall.handle_recv st arI s1 a1 true cr ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) true cr y.2).1, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄) + (hY : n = fl4_abi.syscall.SysYield → syscall.handle_yield st arI s a ⦃ r => + ∃ z ∈ (Fl4Spec.handleYield (Z := Unit) σ).1, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄) : + syscall.dispatch_syscall st arI s a n ⦃ r => DispPostVES (L := L) arI AR on rc c σ r ⦄ := by + refine J.dispatchVGS hARI hAT hVA hnd hM hMK hPA hIA hmrt hmax htu hni hct hnq hvfh hn ?_ hO hRecv hMid hY + intro call bl don fp cptr _ hic hcp + refine J.handleInvocationReached hARI.toARKeep hAT hnd hM.toC hPA on rc hni hct hnq hvfh call bl don fp cptr ?_ + refine J.dispatchReached arI AR MR hni hRef hVA hARI.toARKeep hSy hCap hAT hARI.toMask hVal on rc hct hRun + call bl don fp cptr ?_ + refine VDispReachAtFor.native_of_selected ?_ + exact J.dispatchKnownSelected hni hRef hVA hARI.toARKeep hKDAR hKD hCap hAT hARI.toMask hVal hM hPA hT hnd hmb + ⟨hmax, htu⟩ (hag a) hfan hrcb F.sg F.ports F.vih F.air F.mv F.q F.rd0 hnq F.nf F.uz F.df F.dp F.ut F.orc hct hRun + call bl don fp cptr (by rw [hcp]; exact F.core hic ct hct) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Untyped.lean b/proofs/Fl4Proofs/P3/VmEntryS3Untyped.lean new file mode 100644 index 000000000..5bb354d57 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Untyped.lean @@ -0,0 +1,449 @@ +/- +The Untyped decoder's shared start facts. The region-read proofs use the +shared restart and the same heap/dead-set ghosts. No native state embedding +or additional platform premises are needed. + +The RAM/device spec-side invariants are SpecUtZ, DevRangeS, GpmOutV and +UtInv''. Carry them through the actual successful budget outcome. The +refill-readiness and queue facts come from OffReady and the same outcome. +-/ +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.UnrefDbg +import Fl4Proofs.P3.UnrefBnd +import Fl4Proofs.P3.VsNamedJ +import Fl4Proofs.P3.XTopJointKEntryU14 +import Fl4Proofs.P3.XTopJointKEntry35E + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Vs Notif Endp Ipc Sched +open Fl4Proofs.P3.SInvR.G5 + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} {Lst : U64 → List U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A dead reply is free**: on no call stack, with null links (`DeadP`'s `rTcb`, +`IpcRel.reply`, `RFree`). -/ +private theorem JAtZB.replyDead (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {y : U64} (hy : y ∈ Z.R) (hyD : y ∈ D) : + Detached Z.C Lst y ∧ L.mem s (P1.rNextA y) = 0#u64 ∧ L.mem s (P1.rPrevA y) = 0#u64 := by + have hH : H y = some .reply := J.zH.2.2.1 y hy + have d : L.mem s (P1.rTcbA y) = 0#u64 := (J.jcov.dead y hyD).2.2.2.1 hH + obtain ⟨x, hk, hx⟩ := J.xtop.ipc.reply y hy + have hn : x.replyTcb = none := by + rw [hx]; show optRef (L.mem s (P1.rTcbA y)) = none; rw [d]; exact optRef_zero + exact J.rfree y hy ⟨x, hk, hn⟩ + +/-- A reply of `Z.R` is even (5-bit aligned in `H`). -/ +private theorem JAtZB.replyEven (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {y : U64} (hy : y ∈ Z.R) : + y.val % 2 = 0 := by + have := J.kern.heap.align y _ (J.zH.2.2.1 y hy) + simp only [P1.HKind.bits] at this + omega + +/-- The masked link of a reply: zero, or a live reply. -/ +private theorem JAtZB.keptMasked (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {Rg : U64 → Prop} + (hD : ∀ q k, H q = some k → Rg q → q ∈ D) {x : U64} + (hx : x = 0#u64 ∨ (x ∈ Z.R ∧ x ∉ D)) : P1.KeptRef H Rg (x &&& ~~~1#u64) := by + rcases hx with rfl | ⟨hxR, hxD⟩ + · rw [P1.zero_or_masked]; intro h; exact absurd J.kern.h0 h + · rw [and_not1 x (J.replyEven hxR)] + intro hH hr + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.mp hH + exact hxD (hD x k hk hr) + +/-- A reply on a call stack is live. -/ +private theorem JAtZB.stackLive (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {sc : U64} (hsc : sc ∈ Z.C) + {y : U64} (hy : y ∈ Lst sc) : y ∈ Z.R ∧ y ∉ D := by + have hyR := (J.call.stack sc hsc).2.1 y hy + exact ⟨hyR, fun hyD => (J.replyDead hyR hyD).1 sc hsc hy⟩ + +/-- **(a) at every `JAtZB` state**, at every region (any `Rg`) whose `H` objects are +all dead (module docstring). -/ +private theorem JAtZB.keptA (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {Rg : U64 → Prop} + (hD : ∀ q k, H q = some k → Rg q → q ∈ D) : KeptRefsA (L := L) H Rg s := by + have h0 := J.kern.h0 + have hk0 : ∀ x : U64, x = 0#u64 → P1.KeptRef H Rg x := fun x e h => by subst e; exact absurd h0 h + have hlive : ∀ x : U64, (H x ≠ none → x ∉ D) → P1.KeptRef H Rg x := fun x h hH hr => by + obtain ⟨k, hk⟩ := Option.ne_none_iff_exists'.mp hH + exact h hH (hD x k hk hr) + have hr0 : ∀ r ∈ P1.Heap.replies H, r ≠ 0#u64 := fun r hr e => by + subst e; have : H 0#u64 = some .reply := hr; rw [h0] at this; cases this + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + refine ⟨fun r hr _ hhb => ?_, fun r hr _ => ?_, fun c hc' _ => ?_, fun t ht _ => ?_, fun c hc' _ => ?_⟩ + · -- a reply's next link + have hrR : r ∈ Z.R := J.hset.2.2 r hr + by_cases hon : ∃ sc ∈ Z.C, r ∈ Lst sc + · obtain ⟨sc, hsc, hm⟩ := hon + obtain ⟨i, hi, e⟩ := List.mem_iff_getElem.mp hm + have hW := ((J.call.stack sc hsc).2.2.2.2 i hi).2 + rw [e] at hW + rw [hW] at hhb ⊢ + cases i with + | zero => rw [headBit_or1] at hhb; cases hhb + | succ j => + have hj : j < (Lst sc).length := by omega + show P1.KeptRef H Rg ((Lst sc).getD j 0#u64 &&& ~~~1#u64) + rw [List.getD_eq_getElem _ _ hj] + exact J.keptMasked hD (Or.inr (J.stackLive hsc (List.getElem_mem hj))) + · have hdet : Detached Z.C Lst r := fun sc hsc hm => hon ⟨sc, hsc, hm⟩ + obtain ⟨-, hn, -⟩ := J.call.det r hrR hdet + refine J.keptMasked hD ?_ + by_cases hz : L.mem s (P1.rNextA r) = 0#u64 + · exact Or.inl hz + · obtain ⟨hR, -, hb⟩ := hn hz + exact Or.inr ⟨hR, fun hd => hr0 r hr (by rw [← hb]; exact (J.replyDead hR hd).2.2)⟩ + · -- a reply's previous link + have hrR : r ∈ Z.R := J.hset.2.2 r hr + by_cases hon : ∃ sc ∈ Z.C, r ∈ Lst sc + · obtain ⟨sc, hsc, hm⟩ := hon + obtain ⟨i, hi, e⟩ := List.mem_iff_getElem.mp hm + have hW := ((J.call.stack sc hsc).2.2.2.2 i hi).1 + rw [e] at hW + rw [hW] + refine J.keptMasked hD ?_ + by_cases hj : i + 1 < (Lst sc).length + · rw [List.getD_eq_getElem _ _ hj] + exact Or.inr (J.stackLive hsc (List.getElem_mem hj)) + · left; rw [List.getD_eq_default _ _ (by omega)] + · have hdet : Detached Z.C Lst r := fun sc hsc hm => hon ⟨sc, hsc, hm⟩ + obtain ⟨-, -, hp⟩ := J.call.det r hrR hdet + refine J.keptMasked hD ?_ + by_cases hz : L.mem s (P1.rPrevA r) = 0#u64 + · exact Or.inl hz + · obtain ⟨hR, -, hb⟩ := hp hz + exact Or.inr ⟨hR, fun hd => hr0 r hr (by rw [← hb]; exact (J.replyDead hR hd).2.1)⟩ + · -- an SC's reply head + obtain ⟨z, hz⟩ := hc' + have hcC : c ∈ Z.C := J.hscC c z hz + have hW := (J.call.stack c hcC).2.2.2.1 + rw [hW] + cases hL : Lst c with + | nil => exact hk0 _ rfl + | cons y ys => + have hy : y ∈ Lst c := by rw [hL]; exact List.mem_cons_self + obtain ⟨hyR, hyD⟩ := J.stackLive hcC hy + exact hlive _ fun _ => hyD + · -- a TCB's yield-to link + have htT : t ∈ Z.T := by rw [J.tcbs_eq.1]; exact ht + by_cases hz : L.mem s (P1.yieldA t) = 0#u64 + · exact hk0 _ hz + obtain ⟨hyC, y, n, hky, hyf⟩ := J.yld.toSc t htT hz + refine hlive _ fun _ hd => ?_ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH := (hJ.sc _ y n (J.c0 _ hyC) hky).2 + have d := ((J.jcov.dead _ hd).2.2.2.2 _ hH).2.2.2 + obtain ⟨y0, n0, hk0', hx⟩ := hs.scs _ hyC + rw [hky] at hk0'; cases hk0' + have : y.scYieldFrom = none := by + rw [hx.yieldFrom, ← scYieldFromA_eq, d]; exact optRef_zero + rw [hyf] at this; cases this + · -- an SC's yield-from link + obtain ⟨z, hz⟩ := hc' + have hcC : c ∈ Z.C := J.hscC c z hz + by_cases hw : L.mem s (P1.scYieldFromA c) = 0#u64 + · exact hk0 _ hw + obtain ⟨y, n, hky, hx⟩ := hs.scs c hcC + have hyf : y.scYieldFrom = some (L.mem s (P1.scYieldFromA c)).bv := by + rw [hx.yieldFrom, ← scYieldFromA_eq]; exact Notif.optRef_ne hw + obtain ⟨t, htT, htb, hyt⟩ := J.yld.frm c hcC y n hky _ hyf + have et : t = L.mem s (P1.scYieldFromA c) := P1.eq_of_bv htb + refine hlive _ fun _ hd => ?_ + rw [← et] at hd + have d := ((J.jcov.dead t hd).1 (J.zH.1 t htT)).2.2.2.2.2 + rw [hyt] at d + exact J.c0 c hcC d + +/-- **(b), carried form, at any `JAtZB` state** (module docstring). -/ +private theorem JAtZB.keptBW (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : KeptRefsBAll (L := L) H D s := by + have h0 := J.kern.h0 + have hE := J.blk.2.2.1.2.2 + have nz : ∀ q, H q ≠ none → q ≠ 0#u64 := fun q hq e => hq (by rw [e]; exact h0) + refine ⟨fun t ht hw hD => ?_, fun c hc hw hD => ?_, fun n hn hw hD => ?_⟩ + · -- a TCB's bound notification + have hw0 : L.mem s (P1.boundA t) ≠ 0#u64 := fun e => hw (by rw [e]; exact h0) + have hwN : L.mem s (P1.boundA t) ∈ P1.Heap.ntfns H := hE.bnd t ht hw0 + have tb : L.mem s (P1.ntfnBtA (L.mem s (P1.boundA t))) = t := hE.bs.tb t ht hw0 + have d : L.mem s (Notif.nBoundA (L.mem s (P1.boundA t))) = 0#u64 := ((J.jcov.dead _ hD).2.2.1 hwN).2.1 + have e : t = 0#u64 := tb.symm.trans d + exact nz t (by rw [show H t = some .tcb from ht]; simp) e + · -- an SC's notification + have hw0 : L.mem s (P1.scNtfnA c) ≠ 0#u64 := fun e => hw (by rw [e]; exact h0) + have hwN : L.mem s (P1.scNtfnA c) ∈ P1.Heap.ntfns H := hE.scn c hc hw0 + obtain ⟨z, hz⟩ := id hc + have hc0 : c ≠ 0#u64 := nz c (by rw [hz]; simp) + have sn : L.mem s (P1.ntfnScA (L.mem s (P1.scNtfnA c))) = c := hE.bs.sn c hc hc0 hw0 + have d : L.mem s (Notif.nScA (L.mem s (P1.scNtfnA c))) = 0#u64 := ((J.jcov.dead _ hD).2.2.1 hwN).2.2 + exact hc0 (sn.symm.trans d) + · -- a notification's bound TCB + have hw0 : L.mem s (P1.ntfnBtA n) ≠ 0#u64 := fun e => hw (by rw [e]; exact h0) + have hwT : L.mem s (P1.ntfnBtA n) ∈ P1.Heap.tcbs H := (hE.ntb n hn).ne0 hw0 + have hwZ : L.mem s (P1.ntfnBtA n) ∈ Z.T := by rw [J.tcbs_eq.1]; exact hwT + have nt : L.mem s (P1.boundA (L.mem s (P1.ntfnBtA n))) = n := hE.bs.nt n hn hw0 + have d : L.mem s (Endp.tBoundA (L.mem s (P1.ntfnBtA n))) = 0#u64 := + ((J.jcov.dead _ hD).1 (J.zH.1 _ hwZ)).2.2.1 + exact nz n (by rw [show H n = some .ntfn from hn]; simp) (nt.symm.trans d) + +/-- **(c), carried form, at any `JAtZB` state** (module docstring). -/ +private theorem JAtZB.keptCW (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : DbgKeptCAll (L := L) H Cs D s := by + obtain ⟨lst, hW⟩ := J.live.2.1 + have h0 : H 0#u64 = none := J.kern.h0 + have hnl : ∀ c ∈ Cs, ∀ x ∈ lst c, x ∉ D := fun c hc x hx hxD => by + have hxT : H x = some .tcb := (hW.mem c hc x hx).1 + obtain ⟨d1, d2, -⟩ := J.jcov.dx x hxD hxT + exact hW.notMem_of_off d1 d2 c hc hx + have kr : ∀ c ∈ Cs, ∀ w : U64, (w = 0#u64 ∨ w ∈ lst c) → P1.KeptRef H (· ∈ D) w := by + intro c hc w hw hH hD + rcases hw with rfl | hw + · exact hH h0 + · exact hnl c hc w hw hD + refine ⟨fun y hy => ?_, fun c hc => ?_⟩ + · by_cases hl : ∃ c ∈ Cs, y ∈ lst c + · obtain ⟨c, hc, hyc⟩ := hl + refine ⟨kr c hc _ ?_, kr c hc _ ?_⟩ + · rw [hW.next c hc y hyc]; exact nextIn_zero_or_mem _ _ + · rw [hW.prev c hc y hyc] + rcases prevA_zero_or_mem (lst c) 0#u64 y with h | h | h + · exact Or.inl h + · exact Or.inl h + · exact Or.inr h + · have hn : ∀ c ∈ Cs, y ∉ lst c := fun c hc hyc => hl ⟨c, hc, hyc⟩ + obtain ⟨e1, e2⟩ := hW.off y hy hn + exact ⟨fun hH _ => hH (by rw [e1]; exact h0), fun hH _ => hH (by rw [e2]; exact h0)⟩ + · refine kr c hc _ ?_ + have e : L.mem s (L.nodeAt s c layout.node.DEBUG_TCBS) = (lst c).headD 0#u64 := hW.head c hc + rw [e]; exact headD_zero_or_mem _ + +/-- No kept object links into the selected retype range after restart when +every heap object in the range is dead. The restart fixes the heap and dead +set ghosts, so each of the four reference classes is derived there. -/ +theorem JAtZB.regionUnrefAt + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) : + RegionUnrefAt (L := L) H Cs D s := by + intro s1 hs1 lo hi hD + have hct : sched.cur_thread st s = ok (L.curW s) := cur_thread_eq s + obtain ⟨hcur, x, hk, hrun⟩ := J.curRunnable hni hct + have htcb := (J.xtop.curTcb hcur).1 + obtain ⟨Lst', J1, _⟩ := J.restartJB hcur hk htcb hrun hs1 + have hDead : DeadUnarmed (L := L) H D s1 := fun q hq hH => (J1.jcov.dx q hq hH).2.2 + obtain ⟨⟨_, _, h0⟩, _, hX, _, _⟩ := J1.blk + have hTimer := timerKeptDAll_of J1.kern.h0 h0.tcbs hX.2.2.tm hX.2.2.rb hDead + exact ⟨keptRefsP3_iff.2 ⟨J1.keptA hD, J1.keptBW.region hD⟩, + dbgTimerKept_iff.2 ⟨J1.keptCW.region hD, hTimer.region hD⟩⟩ + +/-- Native joint states use the same shared region proof. -/ +theorem JAtZ.regionUnrefFromBase (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) : RegionUnrefAt (L := L) H Cs D s := + J.toB.regionUnrefAt hni + +/-- **(F1) C17 / C23 / C24 vsReach at a `JAtZB` state with `VsNamed`**: every +table reached from the `NeedR` roots misses a childless untyped cap's region +(module docstring). -/ +private theorem JAtZB.vsReachMiss (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hN : Vs.VsNamed Z.th (L.mem s) (L.ksb s)) {x p sb fi : U64} {dv : Bool} (hx : x ∈ Z.G.chains.flatten) + (hcx : capV (L.mem s) x = .Untyped p sb fi dv) (hnd : ∀ y, ¬ Mdb.Desc Z.G.par y x) : + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s) (L.ksb s) + (Vs.NeedR Z.th (L.mem s) 5) (Vs.NeedR Z.th (L.mem s) 4) (Vs.NeedR Z.th (L.mem s) 3) + (Vs.NeedR Z.th (L.mem s) 2) (Vs.NeedR Z.th (L.mem s) 1) := by + have hca : capAt (L.mem s) x = ok (.Untyped p sb fi dv) := by rw [capAt_capV, hcx] + have hsb : sb.val < 64 := capAt_untyped_inv hca + have hrx := J.ut.reg x hx (by rw [hcx]; exact physF_ut _ _ _ _) + rw [hcx] at hrx + obtain ⟨-, -, hfit⟩ := ut_range hsb hrx + intro k q hR + have hy : ∃ y, IsSlot Z.th y ∧ Vs.VKey (capV (L.mem s) y) = some (k, q) := by + rcases hR.split hN J.vsc with ⟨y, hy, hn⟩ | ⟨a, v, hw⟩ + · exact ⟨y, hy, (Vs.vkey_vneed hn).1⟩ + · obtain ⟨y, hy, hk, -⟩ := hN.named _ _ _ _ hw + exact ⟨y, hy, hk⟩ + obtain ⟨y, hy, hk⟩ := hy + obtain ⟨hp, hs, hph, hnu⟩ := Vs.vkey_tab hk + exact J.tabOff hsb hfit hx hcx hnd hy hp hs hph hnu + +/-- **(F1) The premise shape: C17 / C23 / C24 vsReach at the caller's restart +state** (`JUtPath.vsReach`, `JUtPathM(U16).vsReach`, `JDevPath.vsReach` with +`x = cslotAddr sl`): `set_thread_state Restart` keeps the slots and the `VObj` +words (`restart_top`), so a reach at `s1` is one at `s` (`VsReach.frame`). -/ +theorem JAtZB.vsReachMissRestart (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hN : Vs.VsNamed Z.th (L.mem s) (L.ksb s)) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) {x p sb fi : U64} {dv : Bool} (hx : x ∈ Z.G.chains.flatten) + (hcx : capV (L.mem s) x = .Untyped p sb fi dv) (hnd : ∀ y, ¬ Mdb.Desc Z.G.par y x) : + ∀ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1) := by + intro s1 hs1 + obtain ⟨hcur, x0, hk, hrun⟩ := J.curRunnable hni hct + have hF := J.xtop.fin + have h := Vs.restart_top s a σ ct x0 hF.1 hF.2.1 hcur.symm hk hrun + rw [hs1, WP.spec_ok] at h + obtain ⟨-, -, -, hks, hmV⟩ := h + have hSS : SlotsSame Z.th (L.mem s) (L.mem s1) := slotsSame_of_vobj J.xtop.rel.1 hmV + have hc : ∀ y, IsSlot Z.th y → capV (L.mem s1) y = capV (L.mem s) y := fun y hy => hSS.capV hy + obtain ⟨hWt, hWp⟩ := Vs.walk_of_vobj hF.1.2.vs hF.1.2.pt hmV + intro k q hR + rw [hks] at hR + simp only [Vs.NeedR.congr hc] at hR + have hN' := J.vsc.needR + exact J.vsReachMiss hN hx hcx hnd k q + (hR.frame hF.2.1 hF.2.2.1 hF.2.2.2.1 hWt hWp hN'.1 hN'.2.1 hN'.2.2.1 hN'.2.2.2.1 hN'.2.2.2.2) + +/-- The exact childless range of the selected Untyped slot misses every +native table reached from the VSpace roots after the caller's restart. -/ +theorem JAtZB.vsReachSelected [VxE Z] + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + {sl : Fl4Spec.CslotPtr} (hsl : AbsSlot Z.th sl) {p sb fi : U64} {dev : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev)) + (hnd : ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr sl)) {s1 : S} + (hs1 : sched.set_thread_state st s ct sched.TS_RESTART = ok s1) : + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) + (Vs.NeedR Z.th (L.mem s1) 3) (Vs.NeedR Z.th (L.mem s1) 2) + (Vs.NeedR Z.th (L.mem s1) 1) := by + have hx := ((J.xtop.rel.2.1.2.2.1.members _).2 ⟨hsl.isSlot, _, hc, by intro h; cases h⟩) + exact J.vsReachMissRestart (J.carry.vsNamed (VxE.eq (Z := Z))) hni hct hx (capV_of_capAt hc) hnd s1 hs1 + +/-- Native joint states use the shared restart and VSpace frame. -/ +theorem JAtZ.vsReachRestartFromBase + (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hN : Vs.VsNamed Z.th (L.mem s) (L.ksb s)) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) {x p sb fi : U64} {dv : Bool} (hx : x ∈ Z.G.chains.flatten) + (hcx : capV (L.mem s) x = .Untyped p sb fi dv) (hnd : ∀ y, ¬ Mdb.Desc Z.G.par y x) : + ∀ s1, sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1) := + J.toB.vsReachMissRestart hN hni hct hx hcx hnd + +/-- RAM Untyped regions miss the kernel node block. -/ +theorem JAtZB.utNodesOfJoint + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : UtNodesJ (L.mem s) Z.G (L.ksb s) := + utNodesJ_of_ks J.jcov.ks + +/-- **`UtZero` from the spec's `SpecUtZ`** (INV-PLAN E17): an MDB slot's +non-device untyped cap is the spec's `untypedCap false p sb fi` there. -/ +theorem JAtZB.utZeroOfSpec (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hS : SpecUtZ σ) : + UtZero (L.mem s) Z.G σ := by + intro x hx p sb fi hc w h1 h2 + have hR := J.xtop.rel.2.1.1 + have hM := J.xtop.rel.2.1.2.2.1 + obtain ⟨ysl, hys, rfl⟩ := ((hM.members _).1 hx).1.abs + obtain ⟨c, ac, hc', hca, hg⟩ := hR.abs_cap hys + rw [capV_of_capAt hc'] at hc; subst hc + rw [hca.abs] at hg + have hcap := (SInvU.getCap_capAt (x := (_, σ)) (by rw [hg]; exact Set.mem_singleton _)).2 + exact hS ysl p.bv sb.val fi.val hcap w h1 h2 + +/-- **The bridge** (module docstring): `DevFreeT` from the spec's `DevRangeS` at a `JAtZB` state. -/ +theorem JAtZB.devFreeOfSpec {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hD : SInvDV.DevRangeS σ) : DevFreeT (L.mem s) Z.G σ := by + intro x hx p sb fi d hc hP o ko ho + obtain ⟨hd, -⟩ := hP + subst hd + obtain ⟨sl0, hA0, rfl⟩ := ((J.xtop.rel.2.1.2.2.1.members x).1 hx).1.abs + obtain ⟨c0, ac0, hc0, hca0, hg0⟩ := J.xtop.rel.2.1.1.abs_cap hA0 + rw [capAt_capV, hc] at hc0; cases eq_of_ok_eq hc0 + rw [hca0.abs] at hg0 + have ha0 := (getCap_set_iff σ sl0.1 sl0.2 _).1 (congrArg Prod.fst hg0) + have h := hD sl0.1 sl0.2 p.bv sb.val fi.val ha0 o ko ho + unfold SInvDV.InUsed at h + simp only [UScalar.bv_toNat] at h + omega + +/-- **C27 at a `JAtZB` state from the spec fact** (module docstring). -/ +theorem JAtZB.devGpmOfSpec {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hI : SInvGp.GpmOutV σ) (hU : SInvU.UtInv'' σ) : + DevGpm (L.mem s) Z.G σ := by + intro x hx p sb fi hcv hr + have hM := J.xtop.rel.2.1.2.2.1 + obtain ⟨sl, hAs, rfl⟩ := ((hM.members x).1 hx).1.abs + obtain ⟨c0, ac0, hc0, hca0, hg0⟩ := J.xtop.rel.2.1.1.abs_cap hAs + rw [capAt_capV, hcv] at hc0; cases eq_of_ok_eq hc0 + rw [hca0.abs] at hg0 + have ha0 := (getCap_set_iff σ sl.1 sl.2 _).1 (congrArg Prod.fst hg0) + have hsc : (SInvU.uV σ).2.1 sl = some (absCap (.Untyped p sb fi true)) := by + show SInvU.capAt σ sl = _; rw [SInv.vfh_capAt]; exact ha0 + obtain ⟨hn, hal, -⟩ := utWf_of hU sl true p.bv sb.val fi.val hsc + have hn' : sb.val < 64 := by simp [Fl4Spec.untypedMaxBits] at hn; omega + refine hI (default : Fl4Spec.Irq) sl _ hsc ?_ + show σ.archState.x64GlobalPml4 ∈ SInvU.delR p.bv sb.val + exact (SInvU.mem_delR_iff hn' (fit_of_align hn' hal) _).2 hr + +/-- The RAM-zero bridge also applies to native states. -/ +theorem JAtZ.utZeroOfSpec (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) (h : SpecUtZ σ) : + UtZero (L.mem s) Z.G σ := J.toB.utZeroOfSpec h + +/-- The device-range bridge also applies to native states. -/ +theorem JAtZ.devFreeOfSpec (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + (h : SInvDV.DevRangeS σ) : DevFreeT (L.mem s) Z.G σ := + J.toB.devFreeOfSpec h + +/-- The global-PML4 bridge also applies to native states. -/ +theorem JAtZ.devGpmOfSpec (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hI : SInvGp.GpmOutV σ) (hU : SInvU.UtInv'' σ) : DevGpm (L.mem s) Z.G σ := + J.toB.devGpmOfSpec hI hU + +end + +section +variable {σ σb : Fl4Spec.State Unit} + +/-- A passing budget keeps the device Untyped global-PML4 exclusion and the +Untyped view's well-formedness. [J] Only these entry projections are needed. -/ +theorem BudgetAt.gpmOutAndUtInv (h : BudgetAt σ σb true) + (hI : SInvGp.GpmOutV σ) (hU : SInvU.UtInv'' σ) : + SInvGp.GpmOutV σb ∧ SInvU.UtInv'' σb := by + obtain ⟨σ0, h0, hb⟩ := h + have hv : SInvU.uV σb = SInvU.uV σ := (SInvGp.budget_view hb).trans (SInvGp.fip_view h0) + have hg := (SInvGp.budget_gpm (y := (true, σb)) hb).trans (SInvGp.fip_gpm h0) + refine ⟨?_, ?_⟩ + · unfold SInvGp.GpmOutV + rw [hv, hg] + exact hI + · show SInvU.UtInv''V (SInvU.uV σb) + rwa [hv] + +/-- A passing budget keeps RAM Untyped zeroing. [J] The RAM-zero invariant +is used without the unrelated I/O-port or other spec premises. -/ +theorem BudgetAt.utZeroSpec (h : BudgetAt σ σb true) (hnd : Fl4Spec.numDomains = 1) + {x : Fl4Spec.Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (hZ : SpecUtZ σ) : SpecUtZ σb := by + obtain ⟨⟨σu, hu, ebu⟩, ek, _⟩ := BudgetAt.shape27 hnd hk h + have ek' : σb.kheap = Function.update σ.kheap σ.curThread + (some (.tcb (setReg x .faultIP (x.tcbArch.tcbContext.userRegs .nextIP - 2)))) := ek + rw [Sched.updateTimeStamp_eq' hnd + (doMachineOp_single (getCurrentTime_eq (faultIPSt σ σ.curThread x).machineState))] at hu + have e := congrArg Prod.snd (Set.mem_singleton_iff.1 hu) + dsimp only at e + have hm : σb.machineState.underlyingMemory = σ.machineState.underlyingMemory := by + rw [ebu, e] + rfl + intro sl p sz fi hc b hlo hhi + rw [hm] + exact hZ sl p sz fi (by rw [← capAt_updTcb hk (by rfl) ek']; exact hc) b hlo hhi + +/-- Entry readiness propagates through the actual passing budget. [J] These +facts allow CNode and Untyped to form their restart or preemption conditions. -/ +theorem BudgetAt.dispatchReadiness (h : BudgetAt σ σb true) (hnd : Fl4Spec.numDomains = 1) + {x : Fl4Spec.Tcb} (hk : σ.kheap σ.curThread = some (.tcb x)) (hni : Entered σ) + (hVM : ValidMachineTime σ) (hOff : OffReady σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) : + Entered σb ∧ ValidMachineTime σb ∧ OffReady σb ∧ (∀ d p, σb.curThread ∉ σb.readyQueues d p) := by + obtain ⟨⟨σu, hu, ebu⟩, _, hcur, _, _, hq⟩ := BudgetAt.shape27 hnd hk h + have hOb : OffReady σb := by + rw [ebu] + exact SInvO.offReady_updateTimeStamp (σ := faultIPSt σ σ.curThread x) hVM + (SInvR.G4.offReady_faultIPSt (t := σ.curThread) (x := x) hOff) hu + exact ⟨h.entered hnd hk hni, h.vmt hnd, hOb, fun d p => by rw [hcur, hq]; exact hnq d p⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Xfer.lean b/proofs/Fl4Proofs/P3/VmEntryS3Xfer.lean new file mode 100644 index 000000000..48023dbe7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Xfer.lean @@ -0,0 +1,91 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **the VM record across the base step of a send's transfer**. + +`xferStepGJX`'s `XferBase` is either no step (`y = s`) or FL4's one cap insert from a source slot into the empty +receive slot. For the insert the record follows `VmEptState.ofCn`: `CnVtx` with the identity relabelling (the +inserted cap's view is none or the source's, and `NewOK`: `derive_vt`; the source slot keeps its view, `fullF` only +changes an untyped cap), `EptSlotStep` from the TCBs' own slots kept, `MdbStep` and the slot-word writes from the +insert. **`VmEptState.xferBaseV`** gives the record at the base `(y, σy)`. + +No named premise. New file. +-/ +import Fl4Proofs.P3.XTopJointXferG +import Fl4Proofs.P3.VmCnodeVtx +import Fl4Proofs.P3.P3JSendFootBase +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical + +noncomputable section + +theorem vkeyF_fullF (a b : capability.Cap) : vkeyF ((fullF a b).getD a) = vkeyF a := by + cases a with + | Untyped p1 b1 f1 d1 => + cases b with + | Untyped p2 b2 f2 d2 => unfold fullF; dsimp only; split <;> rfl + | _ => rfl + | _ => cases b <;> rfl + +/-- **`CnVtx` of one cap insert** (identity relabelling). -/ +theorem cnVtx_ins {th : TypedHeap} {m m' : U64 → U64} {x d : U64} {cv c' : capability.Cap} + (hx : IsSlot th x) (hd : IsSlot th d) (hxd : x ≠ d) (hxv : capV m x = cv) (hdv : capV m d = .Null) + (hdc : capV m' d = c') (hxc : capV m' x = (fullF cv c').getD cv) + (hoth : ∀ z, IsSlot th z → z ≠ x → z ≠ d → capV m' z = capV m z) (hvt : VtOK cv c') : CnVtx th m m' := by + refine ⟨fun y => y, fun y hy => hy, fun y y' _ _ h => h, fun y hy => ⟨y, hy, rfl⟩, fun z hz => ?_⟩ + by_cases ed : z = d + · subst ed + refine Or.inr ⟨hdv, x, hx, ?_⟩ + rw [hdc, hxv] + exact hvt + · by_cases ex : z = x + · subst ex + refine Or.inl ?_ + show vkeyF (capV m' z) = vkeyF (capV m z) + rw [hxc, hxv]; exact vkeyF_fullF _ _ + · refine Or.inl ?_ + show vkeyF (capV m' z) = vkeyF (capV m z) + rw [hoth z hz ex ed] + +/-- **`EptSlotStep` from the TCBs' own `SLOT_EPT` slots kept** (as `VmEptState.cnStep`). -/ +theorem eptSlotStep_of_tcb {th : TypedHeap} {m m' : U64 → U64} + (hT : ∀ t, th t = some .tcb → capAt m' (slotOf t 5) = capAt m (slotOf t 5)) : EptSlotStep th th m m' := by + intro t ht + refine Or.inr (Or.inr ⟨ht, ?_⟩) + have hc5 : cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 5) = slotOf t 5 := by + show slotAddr t.bv (Fl4Spec.ofBl _) = _ + rw [slotAddr_ofBl, tcbCnodeIndex_blVal 5 (by decide)] + have h5 := hT t ht + rw [capAt_capV, capAt_capV] at h5 + rw [hc5]; exact eq_of_ok_eq h5 + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The record at the base of a send's transfer** (module docstring). -/ +theorem VmEptState.xferBaseV (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {y : S} {σy : Fl4Spec.State Unit} {G' : MdbGhost} + (Jy : JAtZB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D y a σy) + (hB : XferBase (L := L) Z s y σ σy G') (hcn : Ipc.CnK σ σy) : + VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac Lst D y a σy := by + rcases hB with ⟨rfl, rfl, rfl⟩ | ⟨hmS, hw, hT, cv, c', x, d, hx, hd, hxd, hxv, hdv, hdc, hxc, hoth, hvt⟩ + · exact J + · exact J.ofCn Jy hw (cnVtx_ins hx hd hxd hxv hdv hdc hxc hoth hvt) hmS + (eptSlotStep_of_tcb fun t ht => hT 5 (by decide) t ht) hcn + +end + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} {Lst : U64 → List U64} + + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3XferAll.lean b/proofs/Fl4Proofs/P3/VmEntryS3XferAll.lean new file mode 100644 index 000000000..ff93f55ec --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3XferAll.lean @@ -0,0 +1,1706 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **`do_ipc_transfer` at `VmEptState`, every case**. + +`VmEptState.xferAllV` is P3/XTopJointGrant's `JAtZ.xferAllJ` at a `VmEptState`: a faulted sender (`VmEptState.xferFaultV`, the +P3/XTopJointFault chain), no receive buffer (P3/VmEntryS3ReplyNormal's `VmEptState.replyRegsV`), or a receive buffer with +caps granted (`VmEptState.xferGrantV`, the P3/XTopJointGrant transfer loop). The proofs are those of the native lemmas with +`JAtZ` read as `VmEptState` (generated from them by textual edits); the native steps they use are lifted once here: +a register or buffer word written (`JAtZB.regWrJ` / `JAtZB.userWrJ` with their `NatFoot`, then `VmEptState.ofNat`), +`update_consumed` (`JAtZB.updConsumedN`), and one cap insert (`JAtZB.insDJXB`, then `VmEptState.ofCn` with the +inserted cap's VT-x view `VtOK` from `derive_vt`). + +`VmEptState.replyMessageTransferV` discharges P3/VmEntryS3ReplyNormal's `ReplyMessageTransferV`. + +Premises: [P] `hAT`, `hmax`, `htu` (only for a timeout fault of a sender with an SC, as `JAtZ.xferAllJ`). New file. +-/ +import Fl4Proofs.P3.VmEntryS3ReplyNormal +import Fl4Proofs.P3.VmEntryS3Xfer +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.JAtZBUpdConsumed +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +end + +/-! ## The native steps at `VmEptState` -/ + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + {VX XX : Set U64} {XL : U64 → Nat} + +theorem VmEptState.xtop (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + XTopAt (L := L) AR Z s a σ := J.1.1.xtop +theorem VmEptState.ks (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + KsApart Z.N Z.T Z.E Z.R Z.B (L.ksb s) := J.1.1.ks +theorem VmEptState.buf (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + BufWordRel Z.T (L.mem s) σ := J.1.1.buf +theorem VmEptState.ip (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + IPlaced Z.N Z.T Z.E Z.R Z.B := J.1.1.ip +theorem VmEptState.jpl (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s σ := J.1.1.jpl +theorem VmEptState.zH (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + (∀ t ∈ Z.T, H t = some .tcb) ∧ (∀ e ∈ Z.E, H e = some .ep) ∧ (∀ r ∈ Z.R, H r = some .reply) ∧ + (∀ n ∈ Z.N, H n = some .ntfn) := J.1.1.zH +theorem VmEptState.xferH (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) : + Endp.IpcBufCapOK σ ∧ Endp.BufCover Z.T Z.B σ ∧ Vs.BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ := J.1.1.xferH + +/-- A thread's register read at a `VmEptState` (`JAtZ.regRead`). -/ +theorem VmEptState.regRead (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (r : Fl4Spec.Register) : + (L.mem s (regA t (regIdx r))).bv = x.tcbArch.tcbContext.userRegs r := + ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb ht hk).regs r + +/-- A word of a TCB's receive buffer at a `VmEptState` (`JAtZ.bufWord`). -/ +theorem VmEptState.bufWord (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) (hOK : Endp.IpcBufCapOK σ) + {t : U64} (ht : Z.th t = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) {q : Fl4Spec.ObjRef} + (hq : Endp.ipcBufOf true x = some q) (k : Nat) (hk128 : k < 128) {b : U64} (hb : b.bv = Endp.bufAddr q k) : + (∀ p k', H p = some k' → (∀ z, k' ≠ .other z) → b.val + 8 ≤ p.val ∨ p.val + 2 ^ k'.bits ≤ b.val) ∧ + ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ b.val ∧ b.val < o.toNat + 2 ^ Fl4Spec.objBits ko := + J.1.1.bufWordG hOK ht hk hq k hk128 hb + +/-- One register write of a thread at a `VmEptState` (`JAtZB.regWrJ`, then `VmEptState.ofNat`). -/ +theorem VmEptState.regWrJ (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (r : Fl4Spec.Register) (v : U64) + (hX2 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c (L.wr s (regA t (regIdx r)) v) + (updK σ t.bv (.tcb (setReg x r v.bv)))) : + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (regA t (regIdx r)) v) a + (updK σ t.bv (.tcb (setReg x r v.bv))) := + let ⟨V, B', F⟩ := J.1.1.regWrJ ht hk r v hX2 + ⟨V, J.ofNat B' F⟩ + +/-- One IPC-buffer word written at a `VmEptState` (`JAtZB.userWrJ` with `JAtZB.userWrN`, then `VmEptState.ofNat`). -/ +theorem VmEptState.userWrJ (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ) {wp : U64 × Fl4Spec.Word} (hwB : wp ∈ Z.B) + (hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → wp.1.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ wp.1.val) + (hObj : ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ wp.1.val ∧ wp.1.val < o.toNat + 2 ^ Fl4Spec.objBits ko) + (v : U64) : + VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D (L.wr s wp.1 v) a + (Endp.setUm σ (Endp.storeMem σ.machineState.underlyingMemory wp.2 v.bv)) := + J.ofNat (V' := Z.vs) (J.1.1.userWrJ hBP hwB hAp hObj v) (J.1.1.userWrN hBP hwB hAp v) + +set_option maxHeartbeats 4000000 in +/-- `update_consumed` of an SC at a `VmEptState` (`JAtZB.updConsumedN`, then `VmEptState.ofNat`). -/ +theorem VmEptState.updConsumedJ (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) {p t : U64} (hpC : p ∈ Z.C) + (ht : t ∈ Z.T) : + schedcontext.update_consumed st arI s a p ⦃ r => ∃ σ1 c, + Fl4Spec.schedContextUpdateConsumed (Z := Unit) p.bv σ = ({(c, σ1)}, False) ∧ + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D r.2 a σ1 ⦄ := + WP.spec_mono (J.1.1.updConsumedN hAT hmax htu hpC ht) fun _ ⟨σ1, c, h1, V, B', F⟩ => ⟨σ1, c, h1, V, J.ofNat B' F⟩ + +set_option maxHeartbeats 8000000 in +/-- One cap insert at a `VmEptState` (`JAtZ.insDJ`'s statement, with the inserted cap's VT-x view `hvt`): +`JAtZB.insDJXB`, then `VmEptState.ofCn` (`cnVtx_ins`, the TCBs' own slots kept). -/ +theorem VmEptState.insDJV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {ps pd : U64} {rs rd : Nat} (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) + {idxs idxd : List Bool} (hls : idxs.length = rs) (hld : idxd.length = rd) + {srcC : capability.Cap} (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok srcC) + (hsnn : srcC ≠ .Null) + (hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + (cc : capability.Cap) (ac : Fl4Spec.Cap) (hca : CapRel cc ac) (hcv : CapValid Z.th cc) (he : EncWf cc) + (hcnn : cc ≠ .Null) (hsh : shape cc = shape srcC) (hbd : badgeF cc = badgeF srcC ∨ badgeF srcC = 0#u64) + (hut : isUt srcC = true → ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (ps.bv, idxs))) (hzb : isZb srcC = false) + (hkey : okey cc = okey srcC) + (hv : ∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 srcC → Vs.VCapOK P L4 L3 L2 L1 cc) + (htu : Vs.TabUn cc → Vs.TabUn srcC) (hkr : Vs.Keyed2 cc srcC → cc = srcC) (hnt : ¬ Vs.TabUn cc) + (huc : isUt srcC = true → cc = srcC) (hnat : Vs.NativeCap srcC → Vs.NativeCap cc) + (hDs : DestCap Z.th (L.mem s) ps) (hDd : DestCap Z.th (L.mem s) pd) (hvt : VtOK srcC cc) : + cdt.insert st s cc (cslotAddr (ps.bv, idxs)) (cslotAddr (pd.bv, idxd)) ⦃ s' => ∃ σ', + Fl4Spec.capInsert (Z := Unit) ac (ps.bv, idxs) (pd.bv, idxd) σ = ({((), σ')}, False) ∧ + KeepCV σ σ' ∧ Ipc.CnK σ σ' ∧ L.ksb s' = L.ksb s ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr (ps.bv, idxs) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) ∧ + capV (L.mem s') (cslotAddr (pd.bv, idxd)) = cc ∧ + capV (L.mem s') (cslotAddr (ps.bv, idxs)) = (fullF srcC cc).getD srcC ∧ + ∃ G' : MdbGhost, VmEptState (L := L) arI VX XX XL AR (Z.mv G' Z.vs) H Cs Ac Lst D s' a σ' ⦄ := by + have hAs : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hsd : cslotAddr (ps.bv, idxs) ≠ cslotAddr (pd.bv, idxd) := fun e => by + rw [e, hdst] at hsrcC; exact hsnn (eq_of_ok_eq hsrcC).symm + refine WP.spec_mono (J.1.1.insDJXB hps hpd hls hld hsrcC hsnn hdst cc ac hca hcv he hcnn hsh hbd hut hzb hkey hv htu + hkr hnt huc hnat hDs hDd) ?_ + rintro s' ⟨σ', h1, h2, h3, h4, h5, h6, h7, G', B', hmS, hw, hT⟩ + exact ⟨σ', h1, h2, h3, h4, h5, h6, h7, G', J.ofCn B' hw + (cnVtx_ins hAs.isSlot hAd.isSlot hsd (capV_of_capAt hsrcC) (capV_of_capAt hdst) h6 h7 h5 hvt) hmS + (eptSlotStep_of_tcb fun t ht => hT 5 (by decide) t ht) h3⟩ + +/-! ## The transfer (generated from P3/XTopJointFault, P3/XTopJointXfer, P3/XTopJointGrant) -/ + +variable {σ0 : Fl4Spec.State Unit} {rcv : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} + +/-- `VmEptState` at the state after the words `ms` of a message. -/ +def MrV {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) (VX XX : Set U64) (XL : U64 → Nat) + (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) + (D : Set U64) (a : A) (σ0 : Fl4Spec.State Unit) (rcv : U64) (x0 : Fl4Spec.Tcb) (pb : Option Fl4Spec.ObjRef) + (s : S) (ms : List Fl4Spec.Word) : Prop := + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s a (jMrSt σ0 rcv.bv x0 pb ms) + +theorem MrV.init {V : XVs} (J : VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s a σ0) + (hk0 : σ0.kheap rcv.bv = some (.tcb x0)) (pb : Option Fl4Spec.ObjRef) : + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s [] := + ⟨V, by rw [jMrSt_nil hk0]; exact J⟩ + +theorem MrV.appendN {ms : List Fl4Spec.Word} (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 none s ms) + (h4 : 4 ≤ ms.length) (ys : List Fl4Spec.Word) : + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 none s (ms ++ ys) := by + obtain ⟨V, J⟩ := h + exact ⟨V, by rw [jMrSt_appendN _ _ _ _ _ h4]; exact J⟩ + +/-- A sender's register at a message state (its TCB unchanged). -/ +theorem MrV.sndReg {ms : List Fl4Spec.Word} (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s ms) + {snd : U64} (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs : Fl4Spec.Tcb} + (hks : σ0.kheap snd.bv = some (.tcb xs)) (r : Fl4Spec.Register) : + (L.mem s (regA snd (regIdx r))).bv = xs.tcbArch.tcbContext.userRegs r := by + obtain ⟨V, J⟩ := h + exact J.regRead hs (by rw [jMrSt_other _ _ _ _ _ hne]; exact hks) r + +/-- A sender's fault words at a message state. -/ +theorem MrV.sndFault {ms : List Fl4Spec.Word} (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s ms) + {snd : U64} (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs : Fl4Spec.Tcb} + (hks : σ0.kheap snd.bv = some (.tcb xs)) : FaultRel (L.mem s) snd xs.tcbFault := by + obtain ⟨V, J⟩ := h + exact ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb hs (by rw [jMrSt_other _ _ _ _ _ hne]; exact hks)).faultR + +set_option maxHeartbeats 8000000 in +/-- **A message register written at `J`** (word `k < 4`). -/ +theorem MrV.reg {ms : List Fl4Spec.Word} (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s ms) + (hr : rcv ∈ Z.T) (k : Nat) (hk : k = ms.length) (hk4 : k < 4) (v : U64) : + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb (L.wr s (regA rcv (regIdx (Endp.mrIdx k))) v) + (ms ++ [v.bv]) := by + obtain ⟨V, J⟩ := h + have hkr := jMrSt_rcv σ0 rcv.bv x0 pb ms + have hX := reg_wr_irel J.ip (J.xtop.irelK J.ks J.buf) hr hkr (Endp.mrIdx k) (mrIdx_ne_badge _) v + obtain ⟨V2, J2⟩ := J.regWrJ hr hkr (Endp.mrIdx k) v hX + refine ⟨V2, ?_⟩ + have e : Notif.updK (jMrSt σ0 rcv.bv x0 pb ms) rcv.bv (.tcb (Notif.setReg (Endp.withRegs x0 + (Endp.mrRegs x0.tcbArch.tcbContext.userRegs ms)) (Endp.mrIdx k) v.bv)) = + jMrSt σ0 rcv.bv x0 pb (ms ++ [v.bv]) := by + cases pb with + | none => exact Endp.mrStateN_snoc_lt _ _ _ _ _ k hk (by omega) + | some p => exact Endp.mrState_snoc_lt _ _ _ _ _ _ k hk (by omega) + rw [← e]; exact J2 + +set_option maxHeartbeats 8000000 in +/-- **A buffer word of the message written at `J`** (word `i1 = |ms| + 1` +of the receive buffer, `4 ≤ |ms| < 120`). -/ +theorem MrV.buf {ms : List Fl4Spec.Word} {p : Fl4Spec.ObjRef} + (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 (some p) s ms) + (hr : rcv ∈ Z.T) (hq : Endp.ipcBufOf true x0 = some p) {rw : U64} (hmr : Endp.BufMap Z.B rw p) + (hrw : rw.bv = p) (i1 : U64) (hi1 : i1.val = ms.length + 1) (h4 : 4 ≤ ms.length) (h120 : ms.length < 120) + (v : U64) : + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 (some p) (L.wr s (Endp.fl4BufA rw i1) v) (ms ++ [v.bv]) := by + obtain ⟨V, J⟩ := h + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hrT : Z.th rcv = some .tcb := (hJ.tcb rcv).2 (J.zH.1 rcv hr) + obtain ⟨hOK, -, hBP⟩ := J.xferH + have hwB := hmr i1 (by omega) + have hk := jMrSt_rcv σ0 rcv.bv x0 (some p) ms + obtain ⟨hAp, hObj⟩ := J.bufWord hOK hrT hk (q := p) hq i1.val (by omega) (b := Endp.fl4BufA rw i1) + (by rw [fl4BufA_bv, hrw]) + have J2 := J.userWrJ hBP hwB hAp hObj v + refine ⟨V, ?_⟩ + show VmEptState (L := L) arI VX XX XL AR _ H Cs Ac Lst D _ a (Endp.mrState σ0 rcv.bv x0 p (ms ++ [v.bv])) + rw [← Endp.mrState_snoc_ge _ _ _ _ _ _ h4 h120, ← hi1] + exact J2 + +set_option maxHeartbeats 8000000 in +/-- **FL4's `set_mr` of the next message word at `J`**, into a receive +buffer or without one. -/ +theorem setMrV + (hr : rcv ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {ms : List Fl4Spec.Word} (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s ms) + (i : U64) (hi : i.val = ms.length) (hi' : i.val < 120) (v : U64) : + msg.set_mr st s rcv rw i v ⦃ r => r.1.val = jMrRet pb (i.val + 1) ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb r.2 (ms ++ [v.bv]) ⦄ := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + unfold msg.set_mr + by_cases h4 : i < msg.N_MSG_REGISTERS + · have hi4 : i.val < 4 := by have := (P1.gt_iff_val).1 h4; omega + simp only [h4, ↓reduceIte] + refine WP.spec_bind (Endp.msg_register_spec i hi4) ?_ + rintro r rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s rcv _ v) ?_ + rintro s1 rfl + unfold lift + apply ok_bind_spec + simp only [WP.spec_ok] + refine ⟨?_, h.reg hr i.val hi hi4 v⟩ + have e : (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 + rw [e]; cases pb <;> simp only [jMrRet] <;> omega + · have hi4 : 4 ≤ i.val := by + have : ¬ i.val < 4 := fun h' => h4 ((P1.gt_iff_val).2 (by rw [hN]; exact h')); omega + rw [if_neg h4] + cases pb with + | none => + have hb' : rw = 0#u64 := hb + subst hb' + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨by rw [hN]; simp only [jMrRet]; omega, h.appendN (by omega) _⟩ + | some p => + obtain ⟨hrw0, hmr, hrw⟩ := hb + have hb1 : (rw != 0#u64) = true := by simp [hrw0] + simp only [hb1, ↓reduceIte] + unfold lift + apply ok_bind_spec + refine WP.spec_bind (Endp.msg.buf_set.spec s rw _ v) ?_ + rintro s1 rfl + apply ok_bind_spec + simp only [WP.spec_ok] + have hiv : (core.num.U64.wrapping_add i 1#u64).val = ms.length + 1 := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + refine ⟨by rw [hiv]; simp only [jMrRet]; omega, + h.buf hr (hq p rfl) hmr hrw _ hiv (by omega) (by omega) v⟩ + +set_option maxHeartbeats 8000000 in +/-- **`set_mrs_lookup_failure` at `J`**: the lookup-failure words, appended. -/ +theorem setMrsLfV + (hr : rcv ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {ms : List Fl4Spec.Word} (h : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb 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 rw lfv off ⦃ r => + r.1.val = jMrRet pb (ms.length + (Endp.fl4LfMsg lfv).length) ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb r.2 (ms ++ ((Endp.fl4LfMsg lfv).map UScalar.bv : List Fl4Spec.Word)) ⦄ := by + have hset := fun {s' : S} {ms' : List Fl4Spec.Word} + (h' : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s' ms') => setMrV hr hq hb h' + unfold faults.set_mrs_lookup_failure error.LookupFault.type_code + cases lfv with + | InvalidRoot => + dsimp only + apply ok_bind_spec + refine WP.spec_bind (hset h 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 [Endp.fl4LfMsg, fl4_abi.consts.lookup.InvalidRoot] using h2 + | MissingCapability bl => + dsimp only + apply ok_bind_spec + refine WP.spec_bind (hset h off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + unfold lift + apply ok_bind_spec + refine WP.spec_mono (hset h2 (core.num.U64.wrapping_add off 1#u64) + (by rw [Endp.wadd_val off 1 (by omega) _ rfl]; simp; omega) + (by rw [Endp.wadd_val off 1 (by omega) _ rfl]; omega) bl) ?_ + rintro ⟨i2, s2⟩ ⟨g1, g2⟩ + refine ⟨by simp only at g1 ⊢; rw [g1, Endp.wadd_val off 1 (by omega) _ rfl, hoff]; rfl, ?_⟩ + simpa [Endp.fl4LfMsg, fl4_abi.consts.lookup.MissingCapability] using g2 + | DepthMismatch bf bl => + dsimp only + apply ok_bind_spec + refine WP.spec_bind (hset h off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + unfold lift + apply ok_bind_spec + refine WP.spec_bind (hset h2 (core.num.U64.wrapping_add off 1#u64) + (by rw [Endp.wadd_val off 1 (by omega) _ rfl]; simp; omega) + (by rw [Endp.wadd_val off 1 (by omega) _ rfl]; omega) bl) ?_ + rintro ⟨i2, s2⟩ ⟨g1, g2⟩ + apply ok_bind_spec + refine WP.spec_mono (hset g2 (core.num.U64.wrapping_add off 2#u64) + (by rw [Endp.wadd_val off 2 (by omega) _ rfl]; simp; omega) + (by rw [Endp.wadd_val off 2 (by omega) _ rfl]; omega) bf) ?_ + rintro ⟨i3, s3⟩ ⟨k1, k2⟩ + refine ⟨by simp only at k1 ⊢; rw [k1, Endp.wadd_val off 2 (by omega) _ rfl, hoff]; rfl, ?_⟩ + simpa [Endp.fl4LfMsg, fl4_abi.consts.lookup.DepthMismatch] using k2 + | GuardMismatch g bl gs => + dsimp only + apply ok_bind_spec + refine WP.spec_bind (hset h off hoff (by omega) _) ?_ + rintro ⟨i1, s1⟩ ⟨h1, h2⟩ + unfold lift + apply ok_bind_spec + refine WP.spec_bind (hset h2 (core.num.U64.wrapping_add off 1#u64) + (by rw [Endp.wadd_val off 1 (by omega) _ rfl]; simp; omega) + (by rw [Endp.wadd_val off 1 (by omega) _ rfl]; omega) bl) ?_ + rintro ⟨i2, s2⟩ ⟨g1, g2⟩ + apply ok_bind_spec + refine WP.spec_bind (hset g2 (core.num.U64.wrapping_add off 2#u64) + (by rw [Endp.wadd_val off 2 (by omega) _ rfl]; simp; omega) + (by rw [Endp.wadd_val off 2 (by omega) _ rfl]; omega) g) ?_ + rintro ⟨i3, s3⟩ ⟨k1, k2⟩ + apply ok_bind_spec + refine WP.spec_mono (hset k2 (core.num.U64.wrapping_add off 3#u64) + (by rw [Endp.wadd_val off 3 (by omega) _ rfl]; simp; omega) + (by rw [Endp.wadd_val off 3 (by omega) _ rfl]; omega) gs) ?_ + rintro ⟨i4, s4⟩ ⟨l1, l2⟩ + refine ⟨by simp only at l1 ⊢; rw [l1, Endp.wadd_val off 3 (by omega) _ rfl, hoff]; rfl, ?_⟩ + simpa [Endp.fl4LfMsg, fl4_abi.consts.lookup.GuardMismatch] using l2 + +set_option maxHeartbeats 8000000 in +/-- **`copy_mrs_fault`'s first loop at `J`**: message registers +`0 .. min |rl| 4`. -/ +theorem copyMrsFaultV_loop0 {s0 : S} (hr : rcv ∈ Z.T) {snd : U64} (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) + {xs : Fl4Spec.Tcb} (hks : σ0.kheap snd.bv = some (.tcb xs)) + (h0 : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s0 []) + {sl : Slice U64} {rl : List Fl4Spec.Register} (hsl : sl.val = rl.map regIdx) (hn : rl.length ≤ 120) : + faults.copy_mrs_fault_loop0 st s0 snd rcv sl 0#usize ⦃ r => r.2.val = min rl.length 4 ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb r.1 + ((rl.take r.2.val).map xs.tcbArch.tcbContext.userRegs) ⦄ := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + have hlen := Endp.slice_len_val hsl + 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 ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb x.1 ((rl.take x.2.val).map xs.tcbArch.tcbContext.userRegs)) + · 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 := Endp.us_lt_iff.1 h1; omega + simp only [h1, ↓reduceIte] + step as ⟨i2, hi2⟩ + have hi2v : i2.val = i.val := by rw [hi2]; exact Endp.cast_us i (by omega) + by_cases h2 : i2 < msg.N_MSG_REGISTERS + · have hi : i.val < 4 := by have := (P1.gt_iff_val).1 h2; omega + simp only [h2, ↓reduceIte] + step as ⟨r, hr'⟩ + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' snd r) ?_ + rintro v rfl + step as ⟨i4, hi4'⟩ + have hi4v : i4.val = i.val := by rw [hi4']; exact Endp.cast_us i (by omega) + refine WP.spec_bind (Endp.msg_register_spec i4 (by omega)) ?_ + rintro r5 rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s' rcv _ _) ?_ + rintro s1 rfl + have hlen' : ((rl.take i.val).map xs.tcbArch.tcbContext.userRegs).length = i.val := by simp; omega + have hI1 := hI.reg hr i4.val (by omega) (by omega) (L.mem s' (regA snd r)) + step as ⟨i6, hi6⟩ + have hi6v : i6.val = i.val + 1 := by simpa using hi6 + have hvb : (L.mem s' (regA snd r)).bv = xs.tcbArch.tcbContext.userRegs rl[i.val] := by + rw [hr', Endp.getElem_regs hsl]; exact hI.sndReg hs hne hks _ + refine ⟨by omega, ?_, by omega⟩ + rw [hi6v, Endp.take_succ_map _ 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 ((P1.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 (Endp.us_lt_iff.2 (by omega)) + exact ⟨by omega, hI⟩ + · exact ⟨by simp, by simpa using h0⟩ + +set_option maxHeartbeats 8000000 in +/-- **`copy_mrs_fault`'s second loop at `J`**: message words `4 .. |rl|`, +in the receive buffer. -/ +theorem copyMrsFaultV_loop1 + (hr : rcv ∈ Z.T) {snd : U64} (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) + {xs : Fl4Spec.Tcb} (hks : σ0.kheap snd.bv = some (.tcb xs)) {p : Fl4Spec.ObjRef} + (hq : Endp.ipcBufOf true x0 = some p) {rw : U64} (hmr : Endp.BufMap Z.B rw p) (hrw : rw.bv = p) + {sl : Slice U64} {rl : List Fl4Spec.Register} (hsl : sl.val = rl.map regIdx) (hn : rl.length ≤ 120) + (s1 : S) (i0 : Usize) (hi0 : i0.val = min rl.length 4) + (hI : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 (some p) s1 + ((rl.take i0.val).map xs.tcbArch.tcbContext.userRegs)) : + faults.copy_mrs_fault_loop1 st s1 snd sl rw i0 ⦃ s2 => + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 (some p) s2 (rl.map xs.tcbArch.tcbContext.userRegs) ⦄ := by + have hlen := Endp.slice_len_val hsl + 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 ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 (some p) x.1 + ((rl.take x.2.val).map xs.tcbArch.tcbContext.userRegs)) + · 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 := Endp.us_lt_iff.1 h1; omega + simp only [h1, ↓reduceIte] + step as ⟨r, hr'⟩ + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s' snd r) ?_ + rintro v rfl + step as ⟨i3, hi3⟩ + step as ⟨i4, hi4⟩ + have hi4v : i4.val = i.val + 1 := by + have h3v : i3.val = i.val := by rw [hi3]; exact Endp.cast_us i (by omega) + rw [hi4]; simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, h3v]; omega + have hlen' : ((rl.take i.val).map xs.tcbArch.tcbContext.userRegs).length = i.val := by simp; omega + refine WP.spec_bind (Endp.msg.buf_set.spec s' rw i4 _) ?_ + rintro s3 rfl + have hI3 := hI'.buf hr hq hmr hrw i4 (by omega) (by omega) (by omega) (L.mem s' (regA snd r)) + step as ⟨i5, hi5⟩ + have hi5v : i5.val = i.val + 1 := by simpa using hi5 + have hvb : (L.mem s' (regA snd r)).bv = xs.tcbArch.tcbContext.userRegs rl[i.val] := by + rw [hr', Endp.getElem_regs hsl]; exact hI'.sndReg hs hne hks _ + refine ⟨by omega, by omega, ?_, by omega⟩ + rw [hi5v, Endp.take_succ_map _ 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 (Endp.us_lt_iff.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⟩ + +set_option maxHeartbeats 8000000 in +/-- **`copy_mrs_fault` at `J`** of the sender's registers `rl`: the words +`0 .. |rl|` (into a buffer), or the first four (without one). -/ +theorem copyMrsFaultV + (hr : rcv ∈ Z.T) {snd : U64} (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) + {xs : Fl4Spec.Tcb} (hks : σ0.kheap snd.bv = some (.tcb xs)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) {rw : U64} (hb : JBufR Z.B rw pb) + (h0 : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s []) + {sl : Slice U64} {rl : List Fl4Spec.Register} (hsl : sl.val = rl.map regIdx) (hn : rl.length ≤ 120) : + faults.copy_mrs_fault st s snd rcv sl rw ⦃ s2 => + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ0 rcv x0 pb s2 (rl.map xs.tcbArch.tcbContext.userRegs) ⦄ := by + unfold faults.copy_mrs_fault + refine WP.spec_bind (copyMrsFaultV_loop0 hr hs hne hks h0 hsl hn) ?_ + rintro ⟨s1, i⟩ ⟨hi, hI⟩ + simp only at hi hI + cases pb with + | none => + have hb' : rw = 0#u64 := hb + subst hb' + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + show (ok s1) ⦃ _ ⦄ + simp only [WP.spec_ok] + by_cases hn4 : rl.length ≤ 4 + · rw [hi, Nat.min_eq_left hn4, List.take_of_length_le (le_refl _)] at hI; exact hI + · rw [hi, Nat.min_eq_right (by omega)] at hI + have e : rl.map xs.tcbArch.tcbContext.userRegs = (rl.take 4).map xs.tcbArch.tcbContext.userRegs ++ + (rl.drop 4).map xs.tcbArch.tcbContext.userRegs := by + rw [← List.map_append, List.take_append_drop] + rw [e] + exact hI.appendN (by simp; omega) _ + | some p => + obtain ⟨hrw0, hmr, hrw⟩ := hb + have hb1 : (rw != 0#u64) = true := by simp [hrw0] + simp only [hb1, ↓reduceIte] + exact copyMrsFaultV_loop1 hr hs hne hks (hq p rfl) hmr hrw hsl hn s1 i hi hI + +set_option maxHeartbeats 16000000 in +/-- **`set_mrs_fault` of a cap fault at `J`**. -/ +theorem setMrsFaultCapV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true xr = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {c : Fl4Spec.ObjRef} {rp : Bool} {lf : Fl4Spec.LookupFailure} (hf : xs.tcbFault = some (.capFault c rp lf)) : + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => + r.1.val = jMrRet pb (Endp.capFaultMsg (xs.tcbArch.tcbContext.userRegs .faultIP) c rp lf).length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb r.2 + (Endp.capFaultMsg (xs.tcbArch.tcbContext.userRegs .faultIP) c rp lf) ⦄ := by + have hfr := ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0, hw1, -⟩ := hfr + have hset := fun {s' : S} {ms' : List Fl4Spec.Word} + (h' : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb s' ms') => setMrV hr hq hb h' + have hpc : (L.mem s (regA snd layout.reg.FAULT_IP)).bv = xs.tcbArch.tcbContext.userRegs .faultIP := by + rw [fault_ip_idx]; exact J.regRead hs hks _ + unfold faults.set_mrs_fault + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w0.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w1.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w2.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s snd _) ?_; rintro _ rfl + rw [hty, if_pos (by unfold fl4_abi.consts.fault.CapFault; rfl)] + refine WP.spec_bind (hset (MrV.init (V := Z.vs) J hkr pb) 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⟩ + have hf3 := hI3.sndFault hs hne hks + unfold FaultRel at hf3 + rw [hf] at hf3 + refine WP.spec_bind (Pₘ := fun lfv => + ((Endp.fl4LfMsg lfv).map UScalar.bv : List Fl4Spec.Word) = Fl4Spec.msgFromLookupFailure lf) ?_ ?_ + · unfold faults.lookup_failure + refine WP.spec_bind (Endp.obj.Tcb.lf_type.spec s3 snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.lf_w0.spec s3 snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.lf_w1.spec s3 snd) ?_; rintro _ rfl + exact Endp.decode_rel hf3.2.2.2 + rintro lfv hlfv + refine WP.spec_mono (setMrsLfV hr hq hb hI3 3#u64 rfl (by simp) lfv) ?_ + rintro ⟨n, s4⟩ ⟨hn, hI4⟩ + have hlist : [(L.mem s (regA snd layout.reg.FAULT_IP)).bv, (L.mem s (tFw0A snd)).bv, (L.mem s (tFw1A snd)).bv] ++ + ((Endp.fl4LfMsg lfv).map UScalar.bv : List Fl4Spec.Word) = + Endp.capFaultMsg (xs.tcbArch.tcbContext.userRegs .faultIP) c rp lf := by + rw [hlfv, hpc, hw0, hw1] + unfold Endp.capFaultMsg + 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, ← hlist] + apply congrArg (jMrRet pb) + simp only [List.length_append, List.length_cons, List.length_nil, List.length_map, List.nil_append] <;> omega + · rw [← hlist] + simpa using hI4 + +set_option maxHeartbeats 16000000 in +/-- **`set_mrs_fault` of an unknown-syscall fault at `J`**. -/ +theorem setMrsFaultUnknownV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true xr = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {n : Fl4Spec.Word} (hf : xs.tcbFault = some (.unknownSyscallException n)) : + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => + r.1.val = jMrRet pb (Endp.usMsg xs.tcbArch.tcbContext.userRegs n).length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb r.2 (Endp.usMsg xs.tcbArch.tcbContext.userRegs n) ⦄ := by + have hfr := ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + unfold faults.set_mrs_fault + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w0.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w1.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w2.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s snd _) ?_; rintro _ rfl + rw [hty, ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.CapFault; decide), + ite_eq_left_of_eq_true _ _ (eq_true (by unfold fl4_abi.consts.fault.UnknownSyscall; rfl))] + unfold lift + apply ok_bind_spec + refine WP.spec_bind (copyMrsFaultV hr hs hne hks hq hb (MrV.init (V := Z.vs) J hkr pb) + Endp.syscall_slice_val (by decide)) ?_ + rintro s2 hI2 + refine WP.spec_mono (setMrV hr hq hb hI2 18#u64 (by simp [Endp.syscall_len]) (by decide) + (L.mem s (tFw0A snd))) ?_ + rintro ⟨k, s3⟩ ⟨hk, hI3⟩ + rw [hw0] at hI3 + exact ⟨by rw [Endp.usMsg_length]; simpa using hk, hI3⟩ + +set_option maxHeartbeats 16000000 in +/-- **`set_mrs_fault` of a user exception at `J`**. -/ +theorem setMrsFaultUserV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) (hne : snd.bv ≠ rcv.bv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true xr = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {e c : Fl4Spec.Word} (hf : xs.tcbFault = some (.userException e c)) : + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => + r.1.val = jMrRet pb (Endp.ueMsg xs.tcbArch.tcbContext.userRegs e c).length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb r.2 (Endp.ueMsg xs.tcbArch.tcbContext.userRegs e c) ⦄ := by + have hfr := ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0, hw1⟩ := hfr + unfold faults.set_mrs_fault + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w0.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w1.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w2.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s snd _) ?_; rintro _ rfl + rw [hty, ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.CapFault; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + ite_eq_left_of_eq_true _ _ (eq_true (by unfold fl4_abi.consts.fault.UserException; rfl))] + unfold lift + apply ok_bind_spec + refine WP.spec_bind (copyMrsFaultV hr hs hne hks hq hb (MrV.init (V := Z.vs) J hkr pb) + Endp.exc_slice_val (by decide)) ?_ + rintro s2 hI2 + refine WP.spec_bind (setMrV hr hq hb hI2 3#u64 (by simp [Fl4Spec.exceptionMessage]) (by decide) + (L.mem s (tFw0A snd))) ?_ + rintro ⟨k3, s3⟩ ⟨-, hI3⟩ + refine WP.spec_mono (setMrV hr hq hb hI3 4#u64 (by simp [Fl4Spec.exceptionMessage]) (by decide) + (L.mem s (tFw1A snd))) ?_ + rintro ⟨k4, s4⟩ ⟨hk4, hI4⟩ + rw [hw0, hw1, List.append_assoc] at hI4 + exact ⟨by rw [Endp.ueMsg_length]; simpa using hk4, hI4⟩ + +set_option maxHeartbeats 16000000 in +/-- **`set_mrs_fault` of a timeout fault of a sender without an SC at +`J`**. -/ +theorem setMrsFaultTmoV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true xr = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {b : Fl4Spec.Word} (hf : xs.tcbFault = some (.timeout b)) (hsc0 : L.mem s (P1.scA snd) = 0#u64) : + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => + r.1.val = jMrRet pb [b].length ∧ MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb r.2 [b] ⦄ := by + have hfr := ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + unfold faults.set_mrs_fault + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w0.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w1.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w2.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s snd _) ?_; rintro _ rfl + rw [hty, ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.CapFault; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UserException; decide), + ite_eq_left_of_eq_true _ _ (eq_true (by unfold fl4_abi.consts.fault.Timeout; rfl))] + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s snd) ?_ + rintro i rfl + unfold obj.Sc.is_null + apply ok_bind_spec + simp only [hsc0, decide_true, ↓reduceIte] + refine WP.spec_mono (setMrV hr hq hb (MrV.init (V := Z.vs) J hkr pb) 0#u64 rfl (by decide) + (L.mem s (tFw0A snd))) ?_ + rintro ⟨k, s1⟩ ⟨hk, hI⟩ + rw [hw0, List.nil_append] at hI + exact ⟨by simpa using hk, hI⟩ + +set_option maxHeartbeats 16000000 in +/-- **`set_mrs_fault` of a VM fault at `J`**. -/ +theorem setMrsFaultVmV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true xr = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {va : Fl4Spec.VSpaceRef} {d : List Fl4Spec.Word} (hf : xs.tcbFault = some (.archFault (.vmFault va d))) : + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => + r.1.val = jMrRet pb (Endp.vmMsg (xs.tcbArch.tcbContext.userRegs .faultIP) va d).length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb r.2 (Endp.vmMsg (xs.tcbArch.tcbContext.userRegs .faultIP) va d) ⦄ := by + have hfr := ((J.xtop.irelK J.ks J.buf).1.toE.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0, hd⟩ := hfr + have hset := fun {s' : S} {ms' : List Fl4Spec.Word} + (h' : MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr pb s' ms') => setMrV hr hq hb h' + have hpc : (L.mem s (regA snd layout.reg.FAULT_IP)).bv = xs.tcbArch.tcbContext.userRegs .faultIP := by + rw [fault_ip_idx]; exact J.regRead hs hks _ + unfold faults.set_mrs_fault + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w0.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w1.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w2.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s snd _) ?_; rintro _ rfl + rw [hty, ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.CapFault; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UserException; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.Timeout; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.DebugException; decide), + ite_eq_left_of_eq_true _ _ (eq_true (by unfold fl4_abi.consts.fault.VMFault; rfl))] + refine WP.spec_bind (hset (MrV.init (V := Z.vs) J hkr pb) 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⟩ + refine WP.spec_mono (hset hI3 3#u64 rfl (by decide) _) ?_ + rintro ⟨k, s4⟩ ⟨hk, hI4⟩ + have hl : [(L.mem s (regA snd layout.reg.FAULT_IP)).bv] ++ [(L.mem s (tFw0A snd)).bv] ++ + [(L.mem s (tFw2A snd)).bv] ++ [(L.mem s (tFw1A snd)).bv] = + Endp.vmMsg (xs.tcbArch.tcbContext.userRegs .faultIP) va d := by + rw [hpc, hw0, hd]; rfl + rw [List.nil_append, hl] at hI4 + exact ⟨by rw [← hl]; simpa using hk, hI4⟩ + +set_option maxHeartbeats 16000000 in +/-- **`set_mrs_fault` of a timeout fault of a sender with an SC at `J`**: +`update_consumed` (`VmEptState.updConsumedJ`, with `update_consumed_irel` for the +word written and the time returned), then the badge and the consumed time. +[P] `hAT`, `hmax`, `htu` (as ScJointConsumed). -/ +theorem setMrsFaultTmoScV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) + (hq : ∀ p, pb = some p → Endp.ipcBufOf true xr = some p) {rw : U64} (hb : JBufR Z.B rw pb) + {b : Fl4Spec.Word} (hf : xs.tcbFault = some (.timeout b)) {sc : U64} (hsc : L.mem s (P1.scA snd) = sc) + (hsc0 : sc ≠ 0#u64) (hscC : sc ∈ Z.C) {y : Fl4Spec.SchedContext} {n : Nat} + (hky : σ.kheap sc.bv = some (.schedContext y n)) : + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => r.1.val = jMrRet pb [b, Sched.consumedUs y.scConsumed].length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a (Ipc.tmoSt σ sc y n) rcv xr pb r.2 [b, Sched.consumedUs y.scConsumed] ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hfr := (hI.1.toE.at_tcb hs hks).faultR + unfold FaultRel at hfr + rw [hf] at hfr + obtain ⟨hty, hw0⟩ := hfr + have hrsc : rcv.bv ≠ sc.bv := fun e => by rw [e, hky] at hkr; cases hkr + have hkr1 : (Ipc.tmoSt σ sc y n).kheap rcv.bv = some (.tcb xr) := by + simp [Sched.updSc, Notif.updK, hrsc, hkr] + unfold faults.set_mrs_fault + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w0.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w1.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.fault_w2.spec s snd) ?_; rintro _ rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s snd _) ?_; rintro _ rfl + rw [hty, ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.CapFault; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UnknownSyscall; decide), + ite_eq_right_of_eq_false _ _ (by unfold fl4_abi.consts.fault.UserException; decide), + ite_eq_left_of_eq_true _ _ (eq_true (by unfold fl4_abi.consts.fault.Timeout; rfl))] + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s snd) ?_ + rintro i rfl + unfold obj.Sc.is_null + apply ok_bind_spec + simp only [hsc, hsc0, decide_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (WP.spec_and (J.updConsumedJ hAT hmax htu hscC hs) + (Ipc.update_consumed_irel (ar := arI) J.ip s a σ hI hscC hky hmax htu)) ?_ + rintro ⟨cv, s1⟩ ⟨⟨σ1, c1, he1, V1, J1⟩, -, hcv, he2, -⟩ + simp only at he1 J1 hcv he2 + have eσ : σ1 = Ipc.tmoSt σ sc y n := by + have e := (Prod.mk.inj (he1.symm.trans he2)).1 + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e) + subst eσ + have hM0 := MrV.init (L := L) (arI := arI) (AR := AR) (Z := Z) (V := V1) J1 hkr1 pb + refine WP.spec_bind (setMrV hr hq hb hM0 0#u64 rfl (by decide) (L.mem s (tFw0A snd))) ?_ + rintro ⟨k, s2⟩ ⟨hk, hM1⟩ + simp only at hk hM1 + have hk1 : k.val = 1 := by rw [hk]; cases pb <;> rfl + refine WP.spec_mono (setMrV hr hq hb hM1 k (by rw [hk1]; rfl) (by omega) cv) ?_ + rintro ⟨k2, s3⟩ ⟨hk2, hM2⟩ + simp only at hk2 hM2 ⊢ + rw [hw0, hcv, List.nil_append, List.singleton_append] at hM2 + exact ⟨by rw [hk2, hk1]; rfl, hM2⟩ + +set_option maxHeartbeats 16000000 in +/-- **`do_fault_transfer` at `J`**, given that `set_mrs_fault` writes the +spec's fault message `ms` (from the state `σm` after `make_fault_msg`). -/ +theorem doFaultXferV {snd : U64} (hr : rcv ∈ Z.T) (hs : snd ∈ Z.T) + (hne : snd.bv ≠ rcv.bv) {xs xr : Fl4Spec.Tcb} (hks : σ.kheap snd.bv = some (.tcb xs)) {f : Fl4Spec.Fault} + (hf : xs.tcbFault = some f) {σm : Fl4Spec.State Unit} (hksm : σm.kheap snd.bv = some (.tcb xs)) + (hkrm : σm.kheap rcv.bv = some (.tcb xr)) + (hu : ∀ p, pb = some p → ∀ k, k < 128 → Fl4Spec.inUserFrame (Endp.bufAddr p k) σm) + {label : Fl4Spec.Data} {ms : List Fl4Spec.Word} + (hmsg : Fl4Spec.makeFaultMsg (Z := Unit) f snd.bv σ = ({((label, ms), σm)}, False)) (hlen : ms.length < 120) + {lab : U64} (hlab0 : ∀ m : U64 → U64, FaultRel m snd xs.tcbFault → m (tFaultA snd) = lab) + (hlab : lab.bv &&& Fl4Spec.mask Fl4Spec.msgLabelBits = label) + {rw : U64} (hb : JBufR Z.B rw pb) (badge : U64) + (hset : faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => r.1.val = jMrRet pb ms.length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σm rcv xr pb r.2 ms ⦄) : + ipc.do_fault_transfer st arI s a badge snd rcv rw ⦃ s' => ∃ σ', + Fl4Spec.doFaultTransfer (Z := Unit) badge.bv snd.bv rcv.bv pb σ = ({((), σ')}, False) ∧ + (∀ q, q ≠ rcv.bv → σ'.kheap q = σm.kheap q) ∧ + (∃ x', σ'.kheap rcv.bv = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch }) ∧ + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s' a σ' ⦄ := by + unfold ipc.do_fault_transfer + refine WP.spec_bind hset ?_ + rintro ⟨sent, s1⟩ ⟨hn, hI1⟩ + simp only at hn hI1 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s1 snd) ?_ + rintro ft rfl + rw [hlab0 _ (hI1.sndFault hs hne hksm)] + unfold msginfo.MessageInfo.new + apply ok_bind_spec + have hsent : sent.val < 128 := by rw [hn]; cases pb <;> simp only [jMrRet] <;> omega + refine WP.spec_bind (Endp.set_msg_info_spec (L := L) s1 rcv lab sent hsent) ?_ + rintro s2 ⟨v, rfl, hv⟩ + refine WP.spec_mono (Endp.obj.Tcb.set_reg.spec _ rcv _ badge) ?_ + rintro s3 rfl + obtain ⟨V1, J1⟩ := hI1 + have hEP := J1.xtop.fin.1.2.ep + have hk1 := jMrSt_rcv σm rcv.bv xr pb ms + set x1 := Endp.withRegs xr (Endp.mrRegs xr.tcbArch.tcbContext.userRegs ms) with hx1 + have hX2 := reg_wr_irel J1.ip (J1.xtop.irelK J1.ks J1.buf) hr hk1 Fl4Spec.msg_info_register (by decide) v + obtain ⟨V2, J2⟩ := J1.regWrJ hr hk1 Fl4Spec.msg_info_register v hX2 + rw [regIdx_msgInfo] at J2 + have hk2 : (Notif.updK (jMrSt σm rcv.bv xr pb ms) rcv.bv + (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv))).kheap rcv.bv = + some (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv)) := by simp [Notif.updK] + have hX3 := badge_wr_irel J2.ip (J2.xtop.irelK J2.ks J2.buf) hr hk2 badge + rw [badgeRegA_eq_regA] at hX3 + obtain ⟨V3, J3⟩ := J2.regWrJ hr hk2 Fl4Spec.badge_register badge hX3 + rw [regIdx_badge] at J3 + have hal : ∀ p, pb = some p → ∀ q ∈ Endp.mrPairs ms, (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ + Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σm := by + intro p hp q hq + have := (List.of_mem_zip hq).1 + simp at this + rw [hp] at hb + exact ⟨Endp.bufAddr_aligned hEP hb.2.1 _, hu p hp _ (by omega)⟩ + have hspec := doFaultTransfer_j hks hkrm hf hmsg pb hal badge.bv + have hvx : v.bv = Fl4Spec.messageInfoToData ⟨Fl4Spec.natToLen (jLen pb ms.length), 0, 0, label⟩ := by + rw [hv, hlab, Endp.bv_ofNat_val sent (hn.trans (jMrRet_eq_jLen pb hlen))]; rfl + have hfin : Notif.updK (Notif.updK (jMrSt σm rcv.bv xr pb ms) rcv.bv + (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv))) rcv.bv + (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register v.bv) Fl4Spec.badge_register badge.bv)) = + Notif.updK (jMrSt σm rcv.bv xr pb ms) rcv.bv (.tcb (Notif.setReg (Notif.setReg x1 + Fl4Spec.msg_info_register (Fl4Spec.messageInfoToData ⟨Fl4Spec.natToLen (jLen pb ms.length), 0, 0, label⟩)) + Fl4Spec.badge_register badge.bv)) := by + rw [hvx]; simp only [Notif.updK, Function.update_idem] + refine ⟨_, hspec, fun q hq => ?_, ⟨Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register + (Fl4Spec.messageInfoToData ⟨Fl4Spec.natToLen (jLen pb ms.length), 0, 0, label⟩)) Fl4Spec.badge_register badge.bv, + (by simp only [Notif.updK_kheap, Function.update_self]; try rfl), rfl⟩, V3, by rw [← hfin]; exact J3⟩ + simp only [Notif.updK_kheap, Function.update_of_ne hq] + exact jMrSt_other _ _ _ _ _ hq + +set_option maxHeartbeats 16000000 in +/-- **`do_ipc_transfer` of a faulted sender at `J`** (module docstring): +the receive buffer by `lookup_buf_full` (`BufWordRel`, `BufCover` [J]), the +fault branch, then `doFaultXferV` with the fault type's `set_mrs_fault`. +The spec's outcome changes only the receiver's TCB (in its user context) and, +for a timeout with an SC, that SC. [P] `hAT`, `hmax`, `htu` (only for a +timeout with an SC). -/ +theorem VmEptState.xferFaultV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) {f : Fl4Spec.Fault} + (hf : xs.tcbFault = some f) + (htmo : ∀ b sc, xs.tcbFault = some (.timeout b) → xs.tcbSchedContext = some sc → + P0.ArchTotal arI ∧ Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (epo : Option Fl4Spec.ObjRef) (ep badge : U64) (grant : Bool) : + ipc.do_ipc_transfer st arI s a snd ep badge grant rcv ⦃ s' => ∃ σ', + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv epo badge.bv grant rcv.bv σ = ({((), σ')}, False) ∧ + (∀ q, q ≠ rcv.bv → (∀ b sc, xs.tcbFault = some (.timeout b) → xs.tcbSchedContext = some sc → q ≠ sc) → + σ'.kheap q = σ.kheap q) ∧ + (∃ x', σ'.kheap rcv.bv = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch }) ∧ + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s' a σ' ⦄ := by + have hne' : snd.bv ≠ rcv.bv := fun e => hne (P1.eq_of_bv e) + have hI := J.xtop.irelK J.ks J.buf + have hxs := hI.1.toE.at_tcb hs hks + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have thT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (hJ.tcb t).2 (J.zH.1 t ht) + obtain ⟨hOK, hBC, -⟩ := J.xferH + obtain ⟨xr', hkr', hwr⟩ := J.buf rcv hr + rw [hkr] at hkr'; cases hkr' + have hspec : Fl4Spec.doIpcTransfer (Z := Unit) snd.bv epo badge.bv grant rcv.bv σ = + Fl4Spec.doFaultTransfer (Z := Unit) badge.bv snd.bv rcv.bv (Endp.ipcBufOf true xr) σ := by + unfold Fl4Spec.doIpcTransfer + rw [nd_bind_one (Endp.lookupIpcBuffer_eq hkr true), nd_bind_one (threadGet_eq _ hks), hf] + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 (thT rcv hr) hkr hOK true) ?_ + rintro rw ⟨hrw0, hrwp⟩ + have hb : JBufR Z.B rw (Endp.ipcBufOf true xr) := by + cases hrb : Endp.ipcBufOf true xr with + | none => exact hrw0.2 hrb + | some rp => + have hrwv : rw.bv = rp := hrwp hwr rp hrb + exact ⟨(fun e => by have := hrw0.1 e; rw [hrb] at this; cases this), hBC rcv hr xr true rp hkr hrb rw hrwv, hrwv⟩ + have hq : ∀ p, Endp.ipcBufOf true xr = some p → Endp.ipcBufOf true xr = some p := fun _ h => h + have hu : ∀ p, Endp.ipcBufOf true xr = some p → ∀ k, k < 128 → Fl4Spec.inUserFrame (Endp.bufAddr p k) σ := + fun p h k hk => buf_frame_of_ok hOK hkr h k hk + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_ + rintro ft rfl + have hft0 : ¬ L.mem s (tFaultA snd) = fl4_abi.consts.fault.NullFault := by + intro h + have : L.mem s (tFaultA snd) = 0#u64 := by rw [h]; unfold fl4_abi.consts.fault.NullFault; rfl + rw [hxs.fault0.1 this] at hf; cases hf + simp only [hft0, ↓reduceIte] + rw [hspec] + have hfr := hxs.faultR + unfold FaultRel at hfr + rw [hf] at hfr + -- the faults whose message reads no SC + have hgen : ∀ {label : Fl4Spec.Data} {ms : List Fl4Spec.Word}, + Fl4Spec.makeFaultMsg (Z := Unit) f snd.bv σ = ({((label, ms), σ)}, False) → ms.length < 120 → + ∀ (lab : U64), (∀ m : U64 → U64, FaultRel m snd xs.tcbFault → m (tFaultA snd) = lab) → + lab.bv &&& Fl4Spec.mask Fl4Spec.msgLabelBits = label → + faults.set_mrs_fault st arI s a snd rcv rw ⦃ r => r.1.val = jMrRet (Endp.ipcBufOf true xr) ms.length ∧ + MrV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a σ rcv xr (Endp.ipcBufOf true xr) r.2 ms ⦄ → + ipc.do_fault_transfer st arI s a badge snd rcv rw ⦃ s' => ∃ σ', + Fl4Spec.doFaultTransfer (Z := Unit) badge.bv snd.bv rcv.bv (Endp.ipcBufOf true xr) σ = ({((), σ')}, False) ∧ + (∀ q, q ≠ rcv.bv → (∀ b sc, xs.tcbFault = some (.timeout b) → xs.tcbSchedContext = some sc → q ≠ sc) → + σ'.kheap q = σ.kheap q) ∧ + (∃ x', σ'.kheap rcv.bv = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch }) ∧ + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D s' a σ' ⦄ := + fun hmsg hlen _ hlab0 hlab hset => + WP.spec_mono (doFaultXferV hr hs hne' hks hf hks hkr hu hmsg hlen hlab0 hlab hb badge hset) + fun _ ⟨σ', h1, h2, h3, h4⟩ => ⟨σ', h1, fun q hq _ => h2 q hq, h3, h4⟩ + cases f with + | capFault c rp lf => + exact hgen (Endp.makeFaultMsg_cap hks c rp lf) + (by have := Endp.msgFromLookupFailure_len lf; simp [Endp.capFaultMsg]; omega) + 1#u64 (fun m h => by unfold FaultRel at h; rw [hf] at h; exact h.1) (by rw [Endp.mask52]; decide) + (setMrsFaultCapV J hr hs hne' hks hkr hq hb hf) + | unknownSyscallException n => + exact hgen (Endp.makeFaultMsg_unknown hks n) (by rw [Endp.usMsg_length]; omega) + 2#u64 (fun m h => by unfold FaultRel at h; rw [hf] at h; exact h.1) (by rw [Endp.mask52]; decide) + (setMrsFaultUnknownV J hr hs hne' hks hkr hq hb hf) + | userException e c => + exact hgen (Endp.makeFaultMsg_user hks e c) (by rw [Endp.ueMsg_length]; omega) + 3#u64 (fun m h => by unfold FaultRel at h; rw [hf] at h; exact h.1) (by rw [Endp.mask52]; decide) + (setMrsFaultUserV J hr hs hne' hks hkr hq hb hf) + | archFault af => + cases af with + | vmFault va d => + have hd := hfr.2.2 + exact hgen (Endp.makeFaultMsg_vm hks va d) (by rw [hd]; simp [Endp.vmMsg]) + 6#u64 (fun m h => by unfold FaultRel at h; rw [hf] at h; exact h.1) (by rw [Endp.mask52]; decide) + (setMrsFaultVmV J hr hs hks hkr hq hb hf) + | timeout bb => + obtain ⟨Q, R0, hc, hsp⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨x0, hk0, -, -, hscw, -⟩ := hsp.tcb snd hs + rw [hks] at hk0; cases hk0 + by_cases hscb : xs.tcbSchedContext = none + · have hsc0 : L.mem s (P1.scA snd) = 0#u64 := by + by_contra h + have e := hscw + rw [Notif.optRef_ne h, hscb] at e + cases e + exact hgen (Endp.makeFaultMsg_timeout hks hscb bb) (by simp) + 5#u64 (fun m h => by unfold FaultRel at h; rw [hf] at h; exact h.1) (by rw [Endp.mask52]; decide) + (setMrsFaultTmoV J hr hs hks hkr hq hb hf hsc0) + · obtain ⟨scb, hscb⟩ := Option.ne_none_iff_exists'.1 hscb + set sc := L.mem s (P1.scA snd) with hscdef + obtain ⟨hsc0, hscbv⟩ := Notif.optRef_some (hscw.symm.trans hscb) + have hscC := hsp.scMem snd hs hsc0 + obtain ⟨y, n, hky, -⟩ := hsp.scs sc hscC + have hscx : xs.tcbSchedContext = some sc.bv := by rw [hscb, hscbv] + obtain ⟨hAT, hmax, htu⟩ := htmo bb sc.bv hf hscx + have hrsc : rcv.bv ≠ sc.bv := fun e => by rw [e, hky] at hkr; cases hkr + have hssc : snd.bv ≠ sc.bv := fun e => by rw [e, hky] at hks; cases hks + have hkm : ∀ q, q ≠ sc.bv → (Ipc.tmoSt σ sc y n).kheap q = σ.kheap q := fun q hq => by + simp [Notif.updK, hq] + have hksm : (Ipc.tmoSt σ sc y n).kheap snd.bv = some (.tcb xs) := by rw [hkm _ hssc]; exact hks + have hkrm : (Ipc.tmoSt σ sc y n).kheap rcv.bv = some (.tcb xr) := by rw [hkm _ hrsc]; exact hkr + have hu' : ∀ p, Endp.ipcBufOf true xr = some p → ∀ k, k < 128 → + Fl4Spec.inUserFrame (Endp.bufAddr p k) (Ipc.tmoSt σ sc y n) := by + intro p hp k hk + obtain ⟨sz, hz⟩ := hu p hp k hk + refine ⟨sz, ?_⟩ + have hne2 : Endp.bufAddr p k &&& ~~~(Fl4Spec.mask (Fl4Spec.pageBitsForSize sz)) ≠ sc.bv := by + intro e'; rw [e', hky] at hz; cases hz + simp [Sched.updSc, Notif.updK, hne2, hz] + have hmsg := Ipc.makeFaultMsg_timeoutSC hks hscx (Ipc.schedContextUpdateConsumed_eq hky hmax) bb + refine WP.spec_mono (doFaultXferV hr hs hne' hks hf hksm hkrm hu' hmsg (by simp) + (lab := 5#u64) (fun m h => by unfold FaultRel at h; rw [hf] at h; exact h.1) (by rw [Endp.mask52]; decide) + hb badge (setMrsFaultTmoScV J hAT hmax htu hr hs hks hkr hq hb hf rfl hsc0 hscC hky)) ?_ + rintro s' ⟨σ', h1, h2, h3, h4⟩ + refine ⟨σ', h1, fun q hq hqs => ?_, h3, h4⟩ + rw [h2 q hq, hkm q (hqs bb _ hf hscx)] + +set_option maxHeartbeats 8000000 in +/-- **`copy_mrs`' second loop at `J`**: the sender's buffer words `5 .. n` +into the receiver's buffer, each by `VmEptState.userWrJ` (the word apart from `H`'s +objects and inside the receiver's frame, `VmEptState.bufWord`; `BufPlaced` [J]); +the sender's word read through the endpoint relation's buffer words. -/ +theorem copyMrsV_loop1 {σ1 : Fl4Spec.State Unit} + {rcv : U64} (hrT : Z.th rcv = some .tcb) {x1 : Fl4Spec.Tcb} (hk1 : σ1.kheap rcv.bv = some (.tcb x1)) + {rp sp : Fl4Spec.Word} (hq : Endp.ipcBufOf true x1 = some rp) {sw rw : U64} (hmr : Endp.BufMap Z.B rw rp) + (hrw : rw.bv = rp) (hms : Endp.BufMap Z.B sw sp) (n i0 : U64) (hn : n.val ≤ 120) + (hi0 : i0.val = min n.val 4) (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ1) : + msg.copy_mrs_loop1 st s sw rw n i0 ⦃ r => r.1.val = n.val ∧ + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D r.2 a + (Endp.setUm σ1 (Endp.copyBufMem σ1.machineState.underlyingMemory sp rp (List.range' 5 (n.val - 4)))) ⦄ := 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 ∧ ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D x.1 a + (Endp.setUm σ1 (Endp.copyBufMem σ1.machineState.underlyingMemory sp rp (List.range' 5 (x.2.val - 4))))) + · rintro ⟨s', i⟩ ⟨h1, h2, V', J'⟩ + unfold msg.copy_mrs_loop1.body + simp only at h1 h2 J' ⊢ + by_cases hc1 : i < n + · have hlt : i.val < n.val := (P1.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 := (P1.gt_iff_val).2 (by rw [hmax]; omega) + simp only [hc1, hc2, ↓reduceIte] + obtain ⟨i1, h1e, h1v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h1e, bind_ok] + have hi1v : i1.val = i.val + 1 := by simpa using h1v + refine WP.spec_bind (Endp.msg.buf_get.spec s' sw i1) ?_ + rintro v rfl + refine WP.spec_bind (Endp.msg.buf_set.spec s' rw i1 _) ?_ + rintro s1 rfl + rw [WP.spec_ok] + set um' := Endp.copyBufMem σ1.machineState.underlyingMemory sp rp (List.range' 5 (i.val - 4)) with hum' + have hwr := hmr i1 (by omega) + have hws := hms i1 (by omega) + obtain ⟨hOK', -, hBP'⟩ := J'.xferH + have hvb : (L.mem s' (Endp.fl4BufA sw i1)).bv = Endp.loadWordAt um' (Endp.bufAddr sp i1.val) := + (J'.xtop.irelK J'.ks J'.buf).1.toE.buf _ hws + obtain ⟨hAp, hObj⟩ := J'.bufWord hOK' hrT hk1 hq i1.val (by omega) (b := Endp.fl4BufA rw i1) + (by rw [fl4BufA_bv, hrw]) + have J2 := J'.userWrJ hBP' hwr hAp hObj (L.mem s' (Endp.fl4BufA sw i1)) + 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, Endp.range'_snoc5]; congr 2; omega + refine ⟨⟨show i0.val ≤ i1.val by omega, show i1.val ≤ n.val by omega, V', ?_⟩, + by show n.val - i1.val < n.val - i.val; omega⟩ + dsimp only + rw [hlist, Endp.copyBufMem_snoc, ← hum', ← hvb] + exact J2 + · simp only [hc1, ↓reduceIte, WP.spec_ok] + have : i.val = n.val := by have : ¬ i.val < n.val := fun h => hc1 ((P1.gt_iff_val).2 h); omega + refine ⟨this, V', ?_⟩ + rw [← this]; exact J' + · dsimp only + refine ⟨le_refl _, by omega, Z.vs, ?_⟩ + rw [show i0.val - 4 = 0 by omega] + have e : Endp.setUm σ1 (Endp.copyBufMem σ1.machineState.underlyingMemory sp rp (List.range' 5 0)) = σ1 := rfl + rw [e]; simpa using J + +/-- `VmEptState` at some MDB ghost and VSpace sets, the rest of `Z` and the other +indices fixed. -/ +def JMV (arI : machine.Arch A) (VX XX : Set U64) (XL : U64 → Nat) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (a : A) (s : S) (σ : Fl4Spec.State Unit) : Prop := + ∃ G V, VmEptState (L := L) arI VX XX XL AR (Z.mv G V) H Cs Ac Lst D s a σ + +/-- **The transfer loop's invariant at `J`** (IpcGrant's `TInv` with `VmEptState` +for the composite relation, and the receiver's TCB kept). -/ +def TInvV (arI : machine.Arch A) (VX XX : Set U64) (XL : U64 → Nat) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (a : A) + (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (extra : Aeneas.Std.Array obj.Slot 3#usize) + (ep : U64) (rb : Fl4Spec.Word) (s0 : S) (σ0 : Fl4Spec.State Unit) (dest0 : U64) + (slots0 : List Fl4Spec.CslotPtr) (mi0 : Fl4Spec.MessageInfo) (x : S × msginfo.MessageInfo × obj.Slot × U64) : + Prop := + ∃ σ mi slots, x.2.2.2.val ≤ caps.length ∧ MiRel x.2.1 mi ∧ mi.miLength = mi0.miLength ∧ + mi.miLabel = mi0.miLabel ∧ + (∀ r σf, Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb x.2.2.2.val (caps.drop x.2.2.2.val) slots mi σ = + ({(r, σf)}, False) → + Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb 0 caps slots0 mi0 σ0 = ({(r, σf)}, False)) ∧ + JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a x.1 σ ∧ + (∀ k, k < 128 → Fl4Spec.inUserFrame (bufAddr rb k) σ) ∧ + (∀ j (hj : j < caps.length), x.2.2.2.val ≤ j → + ∃ cv, capAt (L.mem x.1) (exAt extra j) = ok cv ∧ UwEq ep cv (caps[j]).1) ∧ + ((x.2.2.1 = 0#u64 ∧ slots = []) ∨ + (x.2.2.1 = dest0 ∧ slots = slots0 ∧ dest0 ≠ 0#u64 ∧ SlotsSame Z.th (L.mem s0) (L.mem x.1))) ∧ + KeepCV σ0 σ ∧ L.ksb x.1 = L.ksb s0 ∧ x.2.1.caps_unwrapped.val < 8 + +/-- The transfer loop's result at `J`. -/ +def TPostV (arI : machine.Arch A) (VX XX : Set U64) (XL : U64 → Nat) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (a : A) + (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) (ep : U64) (rb : Fl4Spec.Word) + (s0 : S) (σ0 : Fl4Spec.State Unit) (slots0 : List Fl4Spec.CslotPtr) (mi0 : Fl4Spec.MessageInfo) + (y : S × msginfo.MessageInfo × U64) : Prop := + ∃ σ' mf, Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb 0 caps slots0 mi0 σ0 = ({(mf, σ')}, False) ∧ + MiRel y.2.1 mf ∧ mf.miExtraCaps = BitVec.ofNat 64 y.2.2.val ∧ + y.2.1.caps_unwrapped.val < 8 ∧ y.2.2.val ≤ 3 ∧ mf.miLabel = mi0.miLabel ∧ + KeepCV σ0 σ' ∧ L.ksb y.1 = L.ksb s0 ∧ JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a y.1 σ' + +theorem tpostV_of {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {ep : U64} {rb : Fl4Spec.Word} + {σ0 σ : Fl4Spec.State Unit} {slots0 slots : List Fl4Spec.CslotPtr} {mi0 mi : Fl4Spec.MessageInfo} {s s0 : S} + {out : msginfo.MessageInfo} {i : U64} + (hmi : MiRel out mi) + (hch : ∀ r σf, Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb i.val (caps.drop i.val) slots mi σ = + ({(r, σf)}, False) → + Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb 0 caps slots0 mi0 σ0 = ({(r, σf)}, False)) + (hend : Fl4Spec.transferCapsLoop (Z := Unit) (some ep.bv) rb i.val (caps.drop i.val) slots mi σ = + ({(miAt mi i.val, σ)}, False)) + (hcu : out.caps_unwrapped.val < 8) (hi3 : i.val ≤ 3) (hlb : mi.miLabel = mi0.miLabel) + (hK : KeepCV σ0 σ) (hks : L.ksb s = L.ksb s0) (J : JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a s σ) : + TPostV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a caps ep rb s0 σ0 slots0 mi0 (s, out, i) := + ⟨σ, miAt mi i.val, hch _ _ hend, hmi, rfl, hcu, hi3, hlb, hK, hks, J⟩ + +set_option maxHeartbeats 16000000 in +/-- **One step of the transfer loop at `J`** (IpcGrant's `tc_body`: a badge +into the receive buffer by `VmEptState.userWrJ`, an insert by `VmEptState.insDJV`). -/ +theorem tc_bodyV {rcv : U64} (hrT : Z.th rcv = some .tcb) {x0 : Fl4Spec.Tcb} + {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} + {extra : Aeneas.Std.Array obj.Slot 3#usize} {ep rbw : U64} {rb : Fl4Spec.Word} {s0 : S} + {σ0 : Fl4Spec.State Unit} (hk0 : σ0.kheap rcv.bv = some (.tcb x0)) (hq0 : Endp.ipcBufOf true x0 = some rb) + (hrw : rbw.bv = rb) {dest0 : U64} {slots0 : List Fl4Spec.CslotPtr} {mi0 : Fl4Spec.MessageInfo} + (hlen : caps.length ≤ 3) + (hsrc : ∀ j (hj : j < caps.length), SrcOK Z.th (L.mem s0) (exAt extra j) caps[j]) + (hsrcL : ∀ j (hj : j < caps.length), ∃ ps, (caps[j]).2.1 = ps.bv ∧ DestCap Z.th (L.mem s0) ps) + (hnull : ∀ j, caps.length ≤ j → j < 3 → exAt extra j = 0#u64) + (hdest : dest0 ≠ 0#u64 → ∃ d, slots0 = [d] ∧ DestOK Z.th (L.mem s0) dest0 d ∧ + ∃ pd, d.1 = pd.bv ∧ DestCap Z.th (L.mem s0) pd) + (hm : BufMap Z.B rbw rb) (x : S × msginfo.MessageInfo × obj.Slot × U64) + (hx : TInvV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a caps extra ep rb s0 σ0 dest0 slots0 mi0 x) : + ipc.transfer_caps_loop.body st extra ep rbw x.1 x.2.1 x.2.2.1 x.2.2.2 ⦃ r => match r with + | .done y => TPostV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a caps ep rb s0 σ0 slots0 mi0 y + | .cont x' => TInvV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a caps extra ep rb s0 σ0 dest0 slots0 mi0 x' ∧ + 3 - x'.2.2.2.val < 3 - x.2.2.2.val ⦄ := by + obtain ⟨s, out, dest, i⟩ := x + obtain ⟨σ, mi, slots, hi, hmi, hml, hmlb, hch, ⟨G, V, J⟩, hu, hpend, hdst, hK0, hks0, hcu⟩ := hx + dsimp only at hi hmi hch J hpend hdst hks0 ⊢ + have hP0 : IPlaced Z.N Z.T Z.E Z.R Z.B := J.ip + have hB : RelTopB (L := L) Z.th G Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ := J.xtop.rel + have hR : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s) σ := J.xtop.ipc + have hS : SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ := ⟨J.xtop.fin.1.1.1, J.xtop.esa⟩ + have hPl : Placed Z.th := hB.1 + have hkr : σ.kheap rcv.bv = some (.tcb x0) := by + rcases hK0.1 rcv.bv with e | ⟨r, cs, cs', h1, -⟩ + · rw [e, hk0] + · rw [hk0] at h1; cases h1 + unfold ipc.transfer_caps_loop.body + by_cases h3 : i < 3#u64 + · have hi3 : i.val < 3 := by scalar_tac + rw [if_pos h3] + simp only [lift, bind_ok] + rw [index_exAt extra _ (by rw [cast_us_val i hi3]; exact hi3), cast_us_val i hi3] + simp only [bind_ok, obj.Slot.is_null] + by_cases hlt : i.val < caps.length + · obtain ⟨ps, rs, idx, cv0, hps, hls, hsl, hxa, hcv0, habs0, hw0, he0, hcvv0⟩ := hsrc i.val hlt + have hsI : IsSlot Z.th (exAt extra i.val) := by + rw [hxa]; exact AbsSlot.isSlot (⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ : AbsSlot Z.th (ps.bv, idx)) + have hne : exAt extra i.val ≠ 0#u64 := hsI.ne_zero hPl + simp only [hne, decide_false, Bool.false_eq_true, ↓reduceIte] + obtain ⟨cv, hcv, huw⟩ := hpend i.val hlt le_rfl + rw [slot_cap_eq, hcv] + simp only [bind_ok] + rw [unwrap_eq] + simp only [bind_ok] + have hdrop : caps.drop i.val = caps[i.val] :: caps.drop (i.val + 1) := List.drop_eq_getElem_cons hlt + by_cases hu' : UnwrapP ep (absCap cv) + · rw [if_pos hu'] + simp only [buf_caps_eq, bind_ok] + have hk : (core.num.U64.wrapping_add 122#u64 i).val = 122 + i.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + refine WP.spec_bind (buf_set_top hP0 hB hR hS hm (k := core.num.U64.wrapping_add 122#u64 i) + (by rw [hk]; omega) (epBadgeW cv) (hu _ (by rw [hk]; omega))) ?_ + rintro s2 ⟨rfl, hst, -, -, -⟩ + try simp only [lift, bind_ok] + have hand : (i &&& 63#u64).val = i.val := by + rw [UScalar.val_and] + have : i.val = 0 ∨ i.val = 1 ∨ i.val = 2 := by omega + rcases this with h | h | h <;> rw [h] <;> rfl + obtain ⟨i6, h6e, h6v, h6⟩ := spec_exists (U64.ShiftLeft_spec 1#u64 (i &&& 63#u64) (by rw [hand]; omega)) + have hi6 : i6.val < 8 := by + rw [h6v, hand] + rcases (show i.val = 0 ∨ i.val = 1 ∨ i.val = 2 by omega) with h | h | h <;> rw [h] <;> + simp [U64.size, U64.numBits] + rw [h6e] + simp only [bind_ok] + obtain ⟨i8, h8e, h8⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h8e, bind_ok, WP.spec_ok] + have hi8 : i8.val = i.val + 1 := by simpa using h8 + set σ1 := setUm σ (storeMem σ.machineState.underlyingMemory (bufAddr rb (core.num.U64.wrapping_add 122#u64 i).val) + (epBadgeW cv).bv) with hσ1 + have hwp := hm _ (by rw [hk]; omega : (core.num.U64.wrapping_add 122#u64 i).val < 128) + have hal := (hP0.e.balign _ hwp).1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem (L.wr s (fl4BufA rbw (core.num.U64.wrapping_add 122#u64 i)) + (epBadgeW cv))) := + slotsSame_wr hPl s _ hal fun a ha h => hB.2.2.2.2.2.1.slot _ + (Or.inr (Or.inr (Or.inr (Or.inr ⟨_, hwp, le_refl _, by omega⟩)))) a ha h + -- `VmEptState` after the badge word + obtain ⟨hOK, -, hBP⟩ := J.xferH + obtain ⟨hAp, hObj⟩ := J.bufWord hOK hrT hkr hq0 (core.num.U64.wrapping_add 122#u64 i).val + (by rw [hk]; omega) (b := fl4BufA rbw (core.num.U64.wrapping_add 122#u64 i)) (by rw [fl4BufA_bv, hrw]) + have J2 := J.userWrJ hBP hwp hAp hObj (epBadgeW cv) + have hks2 : L.ksb (L.wr s (fl4BufA rbw (core.num.U64.wrapping_add 122#u64 i)) (epBadgeW cv)) = L.ksb s := + (P1.writesIn_wr (L := L) (s := s) (v := epBadgeW cv) hal).1 + refine ⟨⟨σ1, miUnwrap mi i.val, slots, by rw [hi8]; omega, ⟨hmi.1, ?_, hmi.2.2⟩, hml, hmlb, ?_, + ⟨G, V, J2⟩, fun k hk => (inUserFrame_setUm _ _ _).2 (hu k hk), fun j hj hij => ?_, ?_, + ⟨fun p => hK0.1 p, hK0.2⟩, hks2.trans hks0, + by show (out.caps_unwrapped ||| i6).val < 2 ^ 3; rw [UScalar.val_or]; exact Nat.or_lt_two_pow hcu hi6⟩, + by rw [hi8]; omega⟩ + · show (out.caps_unwrapped ||| i6).bv = mi.miCapsUnwrapped ||| ((1 : Fl4Spec.Data) <<< i.val) + rw [UScalar.bv_or, h6, hand]; exact congrArg₂ (· ||| ·) hmi.2.1 rfl + · intro r σf hr + refine hch r σf ?_ + rw [hdrop] + refine tcl_badge (hc := huw.1.mp hu') ?_ + (by rw [hi8] at hr; exact hr) + unfold Fl4Spec.setExtraBadge + show Fl4Spec.storeWordOffs rb (Fl4Spec.bufferCptrIndex + i.val) (Fl4Spec.capEpBadge (caps[i.val]).1) σ = _ + rw [← huw.2 (huw.1.mp hu')] + rw [show Fl4Spec.bufferCptrIndex + i.val = (core.num.U64.wrapping_add 122#u64 i).val by + rw [hk]; rfl] + exact hst + · obtain ⟨cv', h1, h2⟩ := hpend j hj (by rw [hi8] at hij; omega) + exact ⟨cv', by rw [hSS.capAt (by + obtain ⟨ps', rs', idx', cv'', hps', hls', -, hxa', -⟩ := hsrc j hj + rw [hxa']; exact AbsSlot.isSlot (⟨ps', rfl, Or.inl (by simpa [hls'] using hps')⟩ : + AbsSlot Z.th (ps'.bv, idx')))]; exact h1, h2⟩ + · rcases hdst with h | ⟨h1, h2, h3', h4⟩ + · exact Or.inl h + · exact Or.inr ⟨h1, h2, h3', SlotsSame.trans' h4 hSS⟩ + · rw [if_neg hu'] + simp only [] + rcases hdst with ⟨rfl, rfl⟩ | ⟨rfl, rfl, hd0, hSS⟩ + · simp only [decide_true, ↓reduceIte, WP.spec_ok] + refine tpostV_of hmi hch ?_ hcu (by omega) hmlb hK0 hks0 ⟨G, V, J⟩ + rw [hdrop] + exact tcl_noslot (fun h => hu' (huw.1.mpr h)) + · obtain ⟨d, hsl0, ⟨pd, rd, idxd, hpd, hld, rfl, hdA, hdN⟩, pd', hpd1, hDd0⟩ := hdest hd0 + have epd : pd' = pd := P1.eq_of_bv hpd1.symm + rw [epd] at hDd0 + subst hsl0 + simp only [hd0, decide_false, Bool.false_eq_true, ↓reduceIte] + have hcvs : capAt (L.mem s) (exAt extra i.val) = ok cv0 := by rw [hSS.capAt hsI, hcv0] + rw [hcv] at hcvs + have hcvs' : cv = cv0 := by simpa using hcvs + subst hcvs' + have hsl' : (caps[i.val]).2 = (ps.bv, idx) := hsl + obtain ⟨ps', hps1, hDs0⟩ := hsrcL i.val hlt + rw [hsl'] at hps1 + have eps : ps' = ps := P1.eq_of_bv hps1.symm + rw [eps] at hDs0 + have hnot : ¬ UnwrapP ep (caps[i.val]).1 := fun h => hu' (huw.1.mpr h) + have hAs : AbsSlot Z.th (ps.bv, idx) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hdI : IsSlot Z.th dest := by rw [hdA]; exact hAd.isSlot + have hncs : cv ≠ .Null → NoChildrenRel (st := st) s σ (exAt extra i.val) (caps[i.val]).2 := by + intro hut + obtain ⟨-, ⟨-, -, hM, hC, hI⟩, -⟩ := hB + rw [hsl', hxa] + refine noChildren_of_adj Z.th hPl s σ G hM hC (hI.parentAdj (th := Z.th)) _ hAs + ((hM.members _).2 ⟨hAs.isSlot, cv, by rw [← hxa]; exact hcv, ?_⟩) + exact hut + obtain ⟨r, hr, r', hr', hdr⟩ := derive_rel (Z := Unit) s σ (exAt extra i.val) (caps[i.val]).2 cv hw0 he0 + (fun h => hncs (by rintro rfl; exact h)) + rw [← habs0] at hr' + rw [hr] + simp only [bind_ok] + cases r with + | Err e => + cases r' with + | ok _ => exact hdr.elim + | error e' => + simp only [WP.spec_ok] + refine tpostV_of hmi hch ?_ hcu (by omega) hmlb hK0 hks0 ⟨G, V, J⟩ + rw [hdrop] + exact tcl_derr hnot hr' + | Ok c' => + cases r' with + | error _ => exact hdr.elim + | ok a' => + obtain ⟨hw', ha', he', hv', hk'⟩ := hdr + simp only [] + by_cases hn : c' = .Null + · subst hn + rw [cap_is_null_null] + simp only [bind_ok, ↓reduceIte, WP.spec_ok] + refine tpostV_of hmi hch ?_ hcu (by omega) hmlb hK0 hks0 ⟨G, V, J⟩ + rw [hdrop] + rw [← ha'] at hr' + exact tcl_dnull hnot hr' + · rw [cap_is_null_ne hn] + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + have hcvn : cv ≠ .Null := by + rintro rfl; unfold objects.derive_cap at hr; simp at hr; exact hn hr.symm + obtain ⟨hsh, hbd, hzb, hnu⟩ := derive_shape hr hn (hncs hcvn) + have hsnn : cv ≠ .Null := fun h => hn ((Vs.shape_kind hsh).2.2.2.2.2 h) + have hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idx)) = ok cv := by rw [← hxa]; exact hcv + have hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null := by + rw [← hdA, hSS.capAt hdI]; exact hdN + have hut : isUt cv = true → ∀ y, ¬ Mdb.Desc G.par y (cslotAddr (ps.bv, idx)) := by + intro hu0 y hy + obtain ⟨z, hz⟩ := desc_tail_child hy + exact hnu hu0 (by rw [hsl']; exact (hB.2.1.2.2.2.1.has_child_iff hPl hAs).2 ⟨z, hz⟩) + have hsI' : IsSlot Z.th (cslotAddr (ps.bv, idx)) := hAs.isSlot + have hnx : nextOf (L.mem s) (cslotAddr (ps.bv, idx)) = 0#u64 ∨ + IsSlot Z.th (nextOf (L.mem s) (cslotAddr (ps.bv, idx))) := by + have hm' := (hB.2.1.2.2.1.members _).2 ⟨hsI', cv, hsrcC, hsnn⟩ + rcases hB.2.1.2.2.1.next_mem hm' with h | ⟨h, -⟩ + · exact Or.inl h + · exact Or.inr ((hB.2.1.2.2.1.members _).1 h).1 + have hDs : DestCap Z.th (L.mem s) ps := hDs0.same hSS + have hDd : DestCap Z.th (L.mem s) pd := hDd0.same hSS + have hins := WP.spec_and (J.insDJV hps hpd hls hld hsrcC hsnn hdst c' a' ⟨hw', ha'⟩ + (hv' Z.th hcvv0) he' hn hsh (Or.inl hbd) hut hzb (derive_okey hr hn) + (fun P L4 L3 L2 L1 => derive_vcap hr P L4 L3 L2 L1) (fun h => (derive_tabun hr h).elim) + (fun hk => Vs.vkey_derive hr hk) (fun h => derive_tabun hr h) + (derive_ut hr) (derive_nat hr) hDs hDd (derive_vt hr)) + (insert_capfr2 hPl s c' _ _ hsI' hAd.isSlot cv hsrcC he0 he' hnx) + rw [← hxa, ← hdA] at hins + refine WP.spec_bind hins ?_ + rintro s2 ⟨⟨σ2, hci, hK2, hcn2, hks2, -, -, -, G2, J2⟩, hfr⟩ + obtain ⟨i3, h3e, h3v⟩ := spec_exists (U64.add_spec (x := i) (y := 1#u64) (by scalar_tac)) + rw [h3e, bind_ok, WP.spec_ok] + have hi3v : i3.val = i.val + 1 := by simpa using h3v + refine ⟨⟨σ2, mi, [], by rw [hi3v]; omega, hmi, hml, hmlb, fun r σf hr0 => ?_, ⟨G2, V, J2⟩, + fun k hk => hcn2.inUserFrame (hu k hk), fun j hj hij => ?_, Or.inl ⟨rfl, rfl⟩, hK0.trans hK2, + hks2.trans hks0, hcu⟩, by rw [hi3v]; omega⟩ + · refine hch r σf ?_ + rw [hdrop] + refine tcl_insert hnot hr' (fun h => hn ((CapRel.null_iff ⟨hw', ha'⟩).1 h)) ?_ + (by rw [hi3v] at hr0; exact hr0) + show Fl4Spec.capInsert a' (caps[i.val]).2 (pd.bv, idxd) σ = _ + rw [hsl']; exact hci + · obtain ⟨cv', h1, h2⟩ := hpend j hj (by rw [hi3v] at hij; omega) + obtain ⟨ps2, rs2, idx2, cv'', hps2, hls2, -, hxa2, -⟩ := hsrc j hj + have hjI : IsSlot Z.th (exAt extra j) := by + rw [hxa2]; exact AbsSlot.isSlot (⟨ps2, rfl, Or.inl (by simpa [hls2] using hps2)⟩ : + AbsSlot Z.th (ps2.bv, idx2)) + obtain ⟨-, -, -, cvj, -, -, -, -, hcvj, habsj, -⟩ := hsrc j hj + have hsrcfull : ∀ c'', fullF cv c' = some c'' → ¬ UnwrapP ep (absCap cv) := fun c'' hf => + not_unwrap_of_ut (by + cases cv <;> simp [fullF] at hf + exact ⟨_, _, _, _, rfl⟩) + by_cases ejd : exAt extra j = dest + · refine ⟨c', by rw [ejd]; exact hfr.2.1, ?_, fun h => ?_⟩ + · rw [ejd, hdN] at hcvj + have : cvj = .Null := by simpa using hcvj.symm + rw [habsj, this] + refine ⟨fun h => absurd ((unwrap_shape ep cv).2 (hsh ▸ (unwrap_shape ep c').1 h)) hu', + fun h => absurd h not_unwrap_of_null⟩ + · rw [ejd, hdN] at hcvj + have : cvj = .Null := by simpa using hcvj.symm + rw [habsj, this] at h; exact absurd h not_unwrap_of_null + · rcases hfr.2.2 _ hjI ejd with heq | ⟨ejs, c'', hfc, hc''⟩ + · exact ⟨cv', by rw [heq]; exact h1, h2⟩ + · rw [ejs, hcv0] at hcvj + have : cvj = cv := by simpa using hcvj.symm + have hnu := hsrcfull c'' hfc + have hnu'' := not_unwrap_of_ut (ep := ep) (fullF_ut hfc) + refine ⟨c'', hc'', ⟨fun h => absurd h hnu'', fun h => ?_⟩, fun h => ?_⟩ + · rw [habsj, this] at h; exact absurd h hnu + · rw [habsj, this] at h; exact absurd h hnu + · have hieq : i.val = caps.length := by omega + rw [hnull i.val (by omega) hi3] + simp only [decide_true, ↓reduceIte, WP.spec_ok] + refine tpostV_of hmi hch ?_ hcu (by omega) hmlb hK0 hks0 ⟨G, V, J⟩ + rw [hieq, List.drop_length, ← hieq] + exact tcl_nil _ _ _ + · rw [if_neg h3] + simp only [WP.spec_ok] + have hieq : i.val = caps.length := by + have : 3 ≤ i.val := by scalar_tac + omega + refine tpostV_of hmi hch ?_ hcu (by omega) hmlb hK0 hks0 ⟨G, V, J⟩ + rw [hieq, List.drop_length, ← hieq] + exact tcl_nil _ _ _ + +set_option maxHeartbeats 8000000 in +/-- **The transfer loop at `J`** (IpcGrant's `transfer_loop_top`). -/ +theorem transfer_loopV {rcv : U64} (hrT : Z.th rcv = some .tcb) {x0 : Fl4Spec.Tcb} + {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {extra : Aeneas.Std.Array obj.Slot 3#usize} {ep rbw : U64} + {rb : Fl4Spec.Word} {s0 : S} {σ0 : Fl4Spec.State Unit} (hk0 : σ0.kheap rcv.bv = some (.tcb x0)) + (hq0 : Endp.ipcBufOf true x0 = some rb) (hrw : rbw.bv = rb) {dest0 : U64} {slots0 : List Fl4Spec.CslotPtr} + {mi0 : Fl4Spec.MessageInfo} (out0 : msginfo.MessageInfo) (hmi0 : MiRel out0 mi0) + (hcu0 : out0.caps_unwrapped.val < 8) (J0 : JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a s0 σ0) (hlen : caps.length ≤ 3) + (hsrc : ∀ j (hj : j < caps.length), SrcOK Z.th (L.mem s0) (exAt extra j) caps[j]) + (hsrcL : ∀ j (hj : j < caps.length), ∃ ps, (caps[j]).2.1 = ps.bv ∧ DestCap Z.th (L.mem s0) ps) + (hnull : ∀ j, caps.length ≤ j → j < 3 → exAt extra j = 0#u64) + (hD : DestConf Z.th (L.mem s0) caps extra dest0 slots0) + (hDL : dest0 ≠ 0#u64 → ∃ pd idx, dest0 = cslotAddr (pd.bv, idx) ∧ Z.th pd = some (.cnode idx.length) ∧ + DestCap Z.th (L.mem s0) pd) + (hm : BufMap Z.B rbw rb) (hu : ∀ k, k < 128 → Fl4Spec.inUserFrame (bufAddr rb k) σ0) : + ipc.transfer_caps_loop st s0 extra ep rbw out0 dest0 0#u64 ⦃ y => + TPostV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a caps ep rb s0 σ0 slots0 mi0 y ⦄ := by + obtain ⟨G0, V0, J00⟩ := J0 + have hdest := destLive_of J00.xtop.rel.1 hD hDL + unfold ipc.transfer_caps_loop + refine loop.spec_decr_nat (fun x => 3 - x.2.2.2.val) + (TInvV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a caps extra ep rb s0 σ0 dest0 slots0 mi0) _ _ _ (fun x hx => ?_) ?_ + · obtain ⟨s, o, d, i⟩ := x + have h := tc_bodyV hrT hk0 hq0 hrw hlen hsrc hsrcL hnull hdest hm (s, o, d, i) hx + refine WP.spec_mono h ?_ + intro r hr + cases r with + | done y => exact hr + | cont x' => exact hr + · refine ⟨σ0, mi0, slots0, by simp, hmi0, rfl, rfl, fun r σf h => by simpa using h, ⟨G0, V0, J00⟩, hu, + fun j hj _ => ?_, ?_, KeepCV.refl σ0, rfl, hcu0⟩ + · obtain ⟨ps, rs, idx, cv, -, -, -, -, hcv, habs, -⟩ := hsrc j hj + exact ⟨cv, hcv, UwEq.of_abs habs.symm⟩ + · rcases hD with ⟨h0, h1⟩ | ⟨h0, -⟩ + · exact Or.inl ⟨h0, h1⟩ + · exact Or.inr ⟨rfl, rfl, h0, fun _ _ => ⟨rfl, rfl, rfl, rfl⟩⟩ + +set_option maxHeartbeats 8000000 in +/-- **`transfer_caps` at `J`** (IpcGrant's `transfer_caps_top` with the +receive slot's CNode live, `get_receive_slot_live`). -/ +theorem transfer_capsV {rcv : U64} (hrT : Z.th rcv = some .tcb) {x0 : Fl4Spec.Tcb} + {s : S} {σ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + (hk0 : σ.kheap rcv.bv = some (.tcb x0)) {rp : Fl4Spec.Word} (hq0 : Endp.ipcBufOf true x0 = some rp) + (info : msginfo.MessageInfo) (mi : Fl4Spec.MessageInfo) + (hil : info.length.bv = mi.miLength) (hlb : info.label.bv = mi.miLabel) + {caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)} {extra : Aeneas.Std.Array obj.Slot 3#usize} + (hlen : caps.length ≤ 3) + (hsrc : ∀ j (hj : j < caps.length), SrcOK Z.th (L.mem s) (exAt extra j) caps[j]) + (hsrcL : ∀ j (hj : j < caps.length), ∃ ps, (caps[j]).2.1 = ps.bv ∧ DestCap Z.th (L.mem s) ps) + (hnull : ∀ j, caps.length ≤ j → j < 3 → exAt extra j = 0#u64) + (ep : U64) (rbw : U64) (hrw : rbw.bv = rp) (hr0 : rbw ≠ 0#u64) (hm : BufMap Z.B rbw rp) + (hu : ∀ k, k < 128 → Fl4Spec.inUserFrame (bufAddr rp k) σ) : + ipc.transfer_caps st s info extra ep rcv rbw ⦃ r => ∃ σ' mf, + Fl4Spec.transferCaps (Z := Unit) mi caps (some ep.bv) rcv.bv (some rp) σ = ({(mf, σ')}, False) ∧ + MiRel r.1 mf ∧ r.1.extra_caps.bv = mf.miExtraCaps ∧ + r.1.caps_unwrapped.val < 8 ∧ r.1.extra_caps.val ≤ 3 ∧ mf.miLabel = mi.miLabel ∧ + KeepCV σ σ' ∧ L.ksb r.2 = L.ksb s ∧ JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a r.2 σ' ⦄ := by + have hmi0 : MiRel { label := info.label, caps_unwrapped := 0#u64, extra_caps := 0#u64, length := info.length } + ⟨mi.miLength, 0, 0, mi.miLabel⟩ := ⟨hil, rfl, hlb⟩ + have hJ0 : JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a s σ := ⟨Z.G, Z.vs, J⟩ + have hB := J.xtop.rel + have hI := J.xtop.irelK J.ks J.buf + have hGRs : ∃ sl, Fl4Spec.getReceiveSlots (Z := Unit) rcv.bv (some rp) σ = ({(sl, σ)}, False) := by + obtain ⟨dest, sl, hsl, -⟩ := WP.spec_exists (Ipc.get_receive_slot_top J.ip hB hI.1.toE hrT hm hr0 caps extra) + exact ⟨sl, hsl⟩ + have hnil : caps = [] → Fl4Spec.transferCaps (Z := Unit) mi caps (some ep.bv) rcv.bv (some rp) σ = + ({(⟨mi.miLength, 0, 0, mi.miLabel⟩, σ)}, False) := by + rintro rfl + obtain ⟨sl, hsl⟩ := hGRs + unfold Fl4Spec.transferCaps + rw [nd_bind_one hsl, nd_pure_bind'] + exact tcl_nil _ _ _ + unfold ipc.transfer_caps msginfo.MessageInfo.new + simp only [bind_ok] + rw [index_exAt extra 0#usize (by decide), show ((0#usize : Usize).val) = 0 from rfl] + simp only [bind_ok, obj.Slot.is_null] + by_cases h0 : exAt extra 0 = 0#u64 + · have hcn : caps = [] := by + cases caps with + | nil => rfl + | cons y ys => + exfalso + obtain ⟨ps, rs, idx, cv, hps, hls, -, hxa, -⟩ := hsrc 0 (by simp) + have := (AbsSlot.isSlot (⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ : AbsSlot Z.th (ps.bv, idx))).ne_zero hB.1 + rw [← hxa] at this; exact this h0 + rw [decide_eq_true h0, if_pos rfl, WP.spec_ok] + exact ⟨σ, ⟨mi.miLength, 0, 0, mi.miLabel⟩, hnil hcn, hmi0, rfl, by simp, by simp, rfl, KeepCV.refl σ, rfl, hJ0⟩ + · rw [decide_eq_false h0, if_neg Bool.false_ne_true, if_neg hr0] + refine WP.spec_bind (WP.spec_and (Ipc.get_receive_slot_top J.ip hB hI.1.toE hrT hm hr0 caps extra) + (get_receive_slot_live Z.th s σ hB.2.1.1 hB.2.1.2.1 hrT rbw)) ?_ + rintro dest ⟨⟨slots, hsl, hD⟩, hDL⟩ + refine WP.spec_bind (transfer_loopV hrT hk0 hq0 hrw _ hmi0 (by simp) hJ0 hlen hsrc hsrcL hnull hD hDL hm hu) ?_ + rintro ⟨s2, out1, i⟩ ⟨σ', mf, hloop, hmi1, hex, hcu1, hi31, hlb1, hK, hks, J'⟩ + refine (WP.spec_ok _).2 ⟨σ', mf, ?_, hmi1, ?_, hcu1, hi31, hlb1, hK, hks, J'⟩ + swap + · show i.bv = mf.miExtraCaps + rw [hex]; apply BitVec.eq_of_toNat_eq + show i.val = (BitVec.ofNat 64 i.val).toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by scalar_tac)] + unfold Fl4Spec.transferCaps + rw [nd_bind_one hsl, nd_pure_bind'] + exact hloop + +set_option maxHeartbeats 16000000 in +/-- **`do_ipc_transfer` into a receive buffer at `J`, caps granted** (XTopJointXfer's +`VmEptState.xferBufJ` with the extra caps looked up, `lookup_extra_top` and +`lookup_extra_live`, `copy_mrs` writing no slot, `copy_mrs_wr`, and +`transfer_capsV`; the spec's outcome IpcGrantX's `doIpcTransfer_grant`). -/ +theorem VmEptState.xferGrantV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) (hf : xs.tcbFault = none) + {rp : Fl4Spec.ObjRef} (hrb : Endp.ipcBufOf true xr = some rp) (epo : Option Fl4Spec.ObjRef) (ep badge : U64) + (grant : Bool) (hepo : epo = some ep.bv ∨ (epo = none ∧ ep = 0#u64)) : + ipc.do_ipc_transfer st arI s a snd ep badge grant rcv ⦃ s' => ∃ σ', + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv epo badge.bv grant rcv.bv σ = ({((), σ')}, False) ∧ + (∀ p, p ≠ rcv.bv → σ'.kheap p = σ.kheap p ∨ + ∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs')) ∧ + (∃ x', σ'.kheap rcv.bv = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch }) ∧ + JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a s' σ' ⦄ := by + have hne' : snd.bv ≠ rcv.bv := fun e => hne (P1.eq_of_bv e) + have hI := J.xtop.irelK J.ks J.buf + have hxs := hI.1.toE.at_tcb hs hks + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have thT : ∀ t ∈ Z.T, Z.th t = some .tcb := fun t ht => (hJ.tcb t).2 (J.zH.1 t ht) + obtain ⟨hOK, hBC, -⟩ := J.xferH + have hEP := J.xtop.fin.1.2.ep + have hwmi : (L.mem s (regA snd layout.reg.MSG_INFO)).bv = + xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register := by + rw [← regIdx_msgInfo]; exact hxs.regs _ + obtain ⟨xr', hkr', hwr⟩ := J.buf rcv hr + rw [hkr] at hkr'; cases hkr' + obtain ⟨xs', hks', hws⟩ := J.buf snd hs + rw [hks] at hks'; cases hks' + unfold ipc.do_ipc_transfer + refine WP.spec_bind (lookup_buf_full s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 (thT rcv hr) hkr hOK true) ?_ + rintro rw ⟨hrw0, hrwp⟩ + have hrwv : rw.bv = rp := hrwp hwr rp hrb + have hmr : Endp.BufMap Z.B rw rp := hBC rcv hr xr true rp hkr hrb rw hrwv + have hrwn : rw ≠ 0#u64 := fun e => by have := hrw0.1 e; rw [hrb] at this; cases this + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s snd) ?_ + rintro ft rfl + have hft0 : L.mem s (tFaultA snd) = fl4_abi.consts.fault.NullFault := by + rw [hxs.fault0.2 hf]; unfold fl4_abi.consts.fault.NullFault; rfl + simp only [hft0, ↓reduceIte] + refine WP.spec_bind (lookup_buf_full s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 (thT snd hs) hks hOK false) ?_ + rintro sw ⟨hsw0, hswp⟩ + have hsw : (Endp.ipcBufOf false xs = none ∧ sw = 0#u64) ∨ + (∃ sp, Endp.ipcBufOf false xs = some sp ∧ sw ≠ 0#u64 ∧ Endp.BufMap Z.B sw sp) := by + cases h : Endp.ipcBufOf false xs with + | none => exact Or.inl ⟨rfl, hsw0.2 h⟩ + | some sp => + exact Or.inr ⟨sp, rfl, (fun e => by have := hsw0.1 e; rw [h] at this; cases this), + hBC snd hs xs false sp hks h sw (hswp hws sp h)⟩ + unfold ipc.do_normal_transfer + refine WP.spec_bind (Endp.msg_info_spec (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, hext, hle⟩ + have hsb' : sw = 0#u64 ↔ Endp.ipcBufOf false xs = none := ⟨hsw0.1, hsw0.2⟩ + have hsm : ∀ sp, Endp.ipcBufOf false xs = some sp → Endp.BufMap Z.B sw sp := + fun sp h => hBC snd hs xs false sp hks h sw (hswp hws sp h) + 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) ⦃ extra => ∃ caps, + (if grant = true then Fl4Spec.NonDetE.catch (Fl4Spec.lookupExtraCaps (Z := Unit) snd.bv + (Endp.ipcBufOf false xs) (Fl4Spec.dataToMessageInfo (xs.tcbArch.tcbContext.userRegs + Fl4Spec.msg_info_register))) (fun _ => pure []) else pure []) σ = ({(caps, σ)}, False) ∧ + Ipc.ExtraOK Z.th (L.mem s) extra caps ∧ ExtraLive Z.th (L.mem s) (L.ksb s) extra ⦄ := by + have hnl : ExtraLive Z.th (L.mem s) (L.ksb s) ipc.NO_EXTRA_CAPS := + fun j hj hne => absurd (Ipc.exAt_none j hj) hne + cases grant with + | false => + rw [if_neg (by decide), WP.spec_ok] + exact ⟨[], rfl, Ipc.extraOK_none Z.th _, hnl⟩ + | true => + rw [if_pos rfl] + refine WP.spec_bind (WP.spec_and (Ipc.lookup_extra_top J.ip J.xtop.rel hI.1 (thT snd hs) sw + (Endp.ipcBufOf false xs) hsb' hsm tag _ (by show tag.extra_caps.bv.toNat = _; rw [hext, hwmi]) + (by show tag.extra_caps.bv.toNat ≤ 3; rw [hext, hwmi]; exact Endp.extra_le3 _)) + (lookup_extra_live Z.th s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 (thT snd hs) sw tag)) ?_ + rintro r ⟨⟨caps, hc, hok⟩, hl⟩ + cases r with + | Ok e => simp only [WP.spec_ok]; exact ⟨caps, by simp only [↓reduceIte]; exact hc, hok, hl _ rfl⟩ + | Err _ => simp only [WP.spec_ok]; exact ⟨caps, by simp only [↓reduceIte]; exact hc, hok, hnl⟩ + refine WP.spec_bind hex ?_ + rintro extra ⟨caps, hlk, hok, hlive⟩ + -- `copy_mrs` + have hun : Fl4Spec.unat tag.length.bv = tag.length.val := rfl + set x1 := Endp.copyRegsTcb xr xs.tcbArch.tcbContext.userRegs (Fl4Spec.msg_registers.take tag.length.val) + with hx1 + have hcm : msg.copy_mrs st s snd sw rcv rw tag.length ⦃ r => ∃ σc, + Fl4Spec.copyMrs (Z := Unit) snd.bv (Endp.ipcBufOf false xs) rcv.bv (some rp) tag.length.bv σ = + ({(r.1.bv, σc)}, False) ∧ + σc.kheap = Function.update σ.kheap rcv.bv (some (.tcb x1)) ∧ r.1.val ≤ 120 ∧ + ∃ V, VmEptState (L := L) arI VX XX XL AR (Z.mv Z.G V) H Cs Ac Lst D r.2 a σc ⦄ := by + unfold msg.copy_mrs + refine WP.spec_bind (replyCopyMrsV_loop0 J hs hr hne' hks hkr tag.length) ?_ + rintro ⟨s1, i⟩ ⟨hi, V1, J1⟩ + simp only [hrwn, ↓reduceIte] + rcases hsw with ⟨hsn, rfl⟩ | ⟨sp, hsp, hsw0', hms⟩ + · simp only [↓reduceIte] + show (ok (i, s1)) ⦃ _ ⦄ + simp only [WP.spec_ok] + refine ⟨Notif.updK σ rcv.bv (.tcb x1), ?_, rfl, by rw [hi]; omega, V1, J1⟩ + rw [hsn, Endp.copyMrs_noSendBuf hne' hks hkr _ _, hun] + congr + exact (Endp.xfer_len (by rw [hi]; rfl)).symm + · simp only [hsw0', ↓reduceIte] + have hk1 : (Notif.updK σ rcv.bv (.tcb x1)).kheap rcv.bv = some (.tcb x1) := by simp [Notif.updK] + have hq1 : Endp.ipcBufOf true x1 = some rp := by rw [hx1, Endp.ipcBufOf_copyRegs]; exact hrb + refine WP.spec_mono (copyMrsV_loop1 (Z := Z.mv Z.G V1) (thT rcv hr) + hk1 hq1 hmr hrwv hms tag.length i hle hi J1) ?_ + rintro ⟨n2, s2⟩ ⟨hn2, V2, J2⟩ + refine ⟨Endp.setUm (Notif.updK σ rcv.bv (.tcb x1)) (Endp.copyBufMem σ.machineState.underlyingMemory sp rp + (List.range' 5 (tag.length.val - 4))), ?_, rfl, by show n2.val ≤ 120; rw [hn2]; exact hle, V2, J2⟩ + rw [hsp, Endp.copyMrs_bufs hne' hks hkr sp rp tag.length.bv ?_] + · have e : n2 = tag.length := UScalar.eq_of_val_eq hn2 + subst e + rw [← Endp.xfer_len_buf hle, Endp.bufIdx_eq, hun] + · intro x hx + rw [Endp.bufIdx_eq] at hx + have hx' : x < 128 := by simp at hx; omega + exact ⟨Endp.bufAddr_aligned hEP hms x, Endp.bufAddr_aligned hEP hmr x, buf_frame_of_ok hOK hkr hrb x hx'⟩ + refine WP.spec_bind (WP.spec_and hcm (copy_mrs_wr hEP s hs hr hne sw rw tag.length (fun _ => ⟨rp, hmr⟩))) ?_ + rintro ⟨n, s1⟩ ⟨⟨σc, hcopy, hkcf, hn, V1, J1⟩, hWc⟩ + simp only at hcopy hkcf hn J1 hWc + have hkc : σc.kheap rcv.bv = some (.tcb x1) := by rw [hkcf]; simp + have hS1 : SlotsSame Z.th (L.mem s) (L.mem s1) := + Ipc.slotsSame_of_iwords J.xtop.rel.1 J.xtop.rel.2.2.2.2.2.1 hWc (fun _ ha => EWrite.iwords ha) + have hks1 : L.ksb s1 = L.ksb s := hWc.1 + have hok1 := hok.of_same hS1 + set w := xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register with hwdef + have hlenw : tag.length.bv = (Fl4Spec.dataToMessageInfo w).miLength := by rw [hlen, hwmi] + have hlabw : tag.label.bv = (Fl4Spec.dataToMessageInfo w).miLabel := by + rw [hlab, hwmi]; exact (Endp.label_mask w).symm + have hq1 : Endp.ipcBufOf true x1 = some rp := by rw [hx1, Endp.ipcBufOf_copyRegs]; exact hrb + obtain ⟨hOKc, -, -⟩ := J1.xferH + have hu1 : ∀ k, k < 128 → Fl4Spec.inUserFrame (Endp.bufAddr rp k) σc := fun k hk => buf_frame_of_ok hOKc hkc hq1 k hk + have hsrcL1 : ∀ j (hj : j < caps.length), ∃ ps, (caps[j]).2.1 = ps.bv ∧ + DestCap Z.th (L.mem s1) ps := by + intro j hj + obtain ⟨ps, rs, idx, cv, hps, hls, hsl, hxa, -⟩ := hok.2.1 j hj + have hA : AbsSlot Z.th (ps.bv, idx) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hne0 : Ipc.exAt extra j ≠ 0#u64 := by rw [hxa]; exact hA.isSlot.ne_zero J.xtop.rel.1 + obtain ⟨ps', idx', he', hth', hD'⟩ := hlive j (by have := hok.1; omega) hne0 + have e := cslotAddr_inj J.xtop.rel.1 hA (⟨ps', rfl, Or.inl hth'⟩ : AbsSlot Z.th (ps'.bv, idx')) + (hxa.symm.trans he') + exact ⟨ps', by rw [hsl]; exact congrArg Prod.fst e, hD'.same hS1⟩ + refine WP.spec_bind (transfer_capsV (Z := Z.mv Z.G V1) (thT rcv hr) J1 hkc hq1 tag (Fl4Spec.dataToMessageInfo w) + hlenw hlabw hok1.1 hok1.2.1 hsrcL1 hok1.2.2 ep rw hrwv hrwn hmr hu1) ?_ + rintro ⟨out, s2⟩ ⟨σt, mf, htc, hMi, hexc, hcu, he3, hlbt, hKC, hks2, ⟨G2, V2, J2⟩⟩ + simp only at hMi hexc hcu he3 hks2 J2 + 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) ⦃ _ ⦄ + refine WP.spec_bind (Endp.set_msg_info_specG (L := L) s2 rcv { out with length := n } (by show n.val < 128; omega) + hcu (by show out.extra_caps.val < 4; omega)) ?_ + rintro s3 ⟨v, rfl, hv⟩ + refine WP.spec_mono (Endp.obj.Tcb.set_reg.spec _ rcv _ badge) ?_ + rintro s4 rfl + have hkt : σt.kheap rcv.bv = some (.tcb x1) := by + rcases hKC.1 rcv.bv with e | ⟨r, cs, cs', h1, -⟩ + · rw [e, hkc] + · rw [hkc] at h1; cases h1 + have hcopy' : Fl4Spec.copyMrs (Z := Unit) snd.bv (Endp.ipcBufOf false xs) rcv.bv (some rp) + (Fl4Spec.dataToMessageInfo w).miLength σ = ({(n.bv, σc)}, False) := by + rw [← hlenw]; exact hcopy + have htc' : Fl4Spec.transferCaps (Z := Unit) (Fl4Spec.dataToMessageInfo w) caps epo rcv.bv (some rp) σc = + ({(mf, σt)}, False) := by + rcases hepo with rfl | ⟨rfl, rfl⟩ + · exact htc + · rw [Endp.transferCaps_none _ _ _ _ (Ipc.noEp0_of_src hok1.2.1)]; exact htc + have hspec := Endp.doIpcTransfer_grant hks hkr hf hrb epo badge.bv grant hlk hcopy' htc' hkt + have hvx : v.bv = Fl4Spec.messageInfoToData + ⟨n.bv, mf.miExtraCaps, mf.miCapsUnwrapped, (Fl4Spec.dataToMessageInfo w).miLabel⟩ := by + rw [hv] + show Fl4Spec.messageInfoToData ⟨n.bv, out.extra_caps.bv, out.caps_unwrapped.bv, + out.label.bv &&& Fl4Spec.mask Fl4Spec.msgLabelBits⟩ = _ + rw [hexc, hMi.2.1, hMi.2.2, hlbt] + congr 2 + show ((w >>> 12) &&& Fl4Spec.mask Fl4Spec.msgLabelBits) &&& Fl4Spec.mask Fl4Spec.msgLabelBits = + (w >>> 12) &&& Fl4Spec.mask Fl4Spec.msgLabelBits + rw [BitVec.and_assoc, BitVec.and_self] + -- the message info + have hI2 := J2.xtop.irelK J2.ks J2.buf + have hX2 := reg_wr_irel J2.ip hI2 hr hkt Fl4Spec.msg_info_register (by decide) v + obtain ⟨V3, J3⟩ := J2.regWrJ hr hkt Fl4Spec.msg_info_register v hX2 + rw [regIdx_msgInfo] at J3 + have hkt2 : (Notif.updK σt rcv.bv (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv))).kheap rcv.bv = + some (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv)) := by simp [Notif.updK] + -- the badge + have hX4 := badge_wr_irel J3.ip (J3.xtop.irelK J3.ks J3.buf) hr hkt2 badge + rw [badgeRegA_eq_regA] at hX4 + obtain ⟨V4, J4⟩ := J3.regWrJ hr hkt2 Fl4Spec.badge_register badge hX4 + rw [regIdx_badge] at J4 + have hfin : Notif.updK (Notif.updK σt rcv.bv (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register v.bv))) rcv.bv + (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register v.bv) Fl4Spec.badge_register badge.bv)) = + Notif.updK σt rcv.bv (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register + (Fl4Spec.messageInfoToData ⟨n.bv, mf.miExtraCaps, mf.miCapsUnwrapped, + (Fl4Spec.dataToMessageInfo w).miLabel⟩)) Fl4Spec.badge_register badge.bv)) := by + simp only [Notif.updK, Function.update_idem]; rw [hvx] + refine ⟨_, hspec, ?_, ⟨Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register + (Fl4Spec.messageInfoToData ⟨n.bv, mf.miExtraCaps, mf.miCapsUnwrapped, + (Fl4Spec.dataToMessageInfo w).miLabel⟩)) Fl4Spec.badge_register badge.bv, + (by simp only [Notif.updK_kheap, Function.update_self]; try rfl), ?_⟩, G2, V4, by rw [← hfin]; exact J4⟩ + · intro p hp + simp only [Notif.updK_kheap, Function.update_of_ne hp] + rcases hKC.1 p with e | ⟨r, cs, cs', h1, h2⟩ + · left; rw [e, hkcf]; simp [Function.update_of_ne hp] + · right; exact ⟨r, cs, cs', by rw [← h1, hkcf]; simp [Function.update_of_ne hp], h2⟩ + · rw [hx1, Endp.copyRegsTcb_other]; rfl + +set_option maxHeartbeats 8000000 in +/-- **`do_ipc_transfer` at `J`, every case** (module docstring): a faulted +sender (`VmEptState.xferFaultV`; [P] `hAT`, `hmax`, `htu` for a timeout with an +SC), the receiver without a buffer (`VmEptState.replyRegsV`), or with one +(`VmEptState.xferGrantV`, granted caps included). The spec's outcome changes the +receiver's TCB in its user context, the sender's SC (a timeout fault's +consumed time) and CNodes (the granted caps' inserts) only. -/ +theorem VmEptState.xferAllV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) (grant : Bool) + (epo : Option Fl4Spec.ObjRef) (ep badge : U64) (hepo : epo = some ep.bv ∨ (epo = none ∧ ep = 0#u64)) : + ipc.do_ipc_transfer st arI s a snd ep badge grant rcv ⦃ s' => ∃ σ', + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv epo badge.bv grant rcv.bv σ = ({((), σ')}, False) ∧ + (∀ p, p ≠ rcv.bv → (∀ sc, xs.tcbSchedContext = some sc → p ≠ sc) → σ'.kheap p = σ.kheap p ∨ + ∃ r cs cs', σ.kheap p = some (.cnode r cs) ∧ σ'.kheap p = some (.cnode r cs')) ∧ + (∃ x', σ'.kheap rcv.bv = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch }) ∧ + JMV (L := L) arI VX XX XL AR Z H Cs Ac Lst D a s' σ' ⦄ := by + by_cases hf : xs.tcbFault = none + · obtain ⟨hOK, hBC, -⟩ := J.xferH + cases hrb : Endp.ipcBufOf true xr with + | none => + refine WP.spec_mono (J.replyRegsV hOK hBC hs hr hne hks hkr hf hrb epo ep badge grant) ?_ + rintro s' ⟨h1, V, J'⟩ + refine ⟨_, h1, fun p hp _ => Or.inl (by simp [Notif.updK, hp]), ⟨Endp.xferTcb xr xs badge.bv, + (by simp only [Notif.updK_kheap, Function.update_self]), ?_⟩, Z.G, V, J'⟩ + simp only [Endp.xferTcb] + rw [Endp.copyRegsTcb_other] + rfl + | some rp => + refine WP.spec_mono (J.xferGrantV hs hr hne hks hkr hf hrb epo ep badge grant hepo) ?_ + rintro s' ⟨σ', h1, h2, h3, h4⟩ + exact ⟨σ', h1, fun p hp _ => h2 p hp, h3, h4⟩ + · obtain ⟨f, hf'⟩ := Option.ne_none_iff_exists'.1 hf + refine WP.spec_mono (J.xferFaultV hs hr hne hks hkr hf' (fun _ _ _ _ => ⟨hAT, hmax, htu⟩) + epo ep badge grant) ?_ + rintro s' ⟨σ', h1, h2, h3, V, h4⟩ + exact ⟨σ', h1, fun q hq hsc => Or.inl (h2 q hq (fun b sc _ h2 => hsc sc h2)), h3, Z.G, V, h4⟩ + +/-- **The paired no-endpoint transfer of a reply at a `VmEptState`** (`ReplyMessageTransferV`, by +`VmEptState.xferAllV` with no endpoint and badge 0). -/ +theorem VmEptState.replyMessageTransferV (J : VmEptState (L := L) arI VX XX XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {ct i : U64} (hct : ct ∈ Z.T) (hi : i ∈ Z.T) (hne : ct ≠ i) {xs xi : Fl4Spec.Tcb} + (hks : σ.kheap ct.bv = some (.tcb xs)) (hki : σ.kheap i.bv = some (.tcb xi)) (g : Bool) : + ReplyMessageTransferV (L := L) arI AR Z H Cs D VX XX XL s a σ ct i g xs xi := by + unfold ReplyMessageTransferV + refine WP.spec_mono (J.xferAllV hAT hmax htu hct hi hne hks hki g none 0#u64 0#u64 (Or.inr ⟨rfl, rfl⟩)) ?_ + rintro s' ⟨σ', h1, h2, h3, G, V, J'⟩ + exact ⟨σ', h1, h2, h3, G, V, Ac, Lst, J'⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEntryS3Yield.lean b/proofs/Fl4Proofs/P3/VmEntryS3Yield.lean new file mode 100644 index 000000000..416b0c996 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEntryS3Yield.lean @@ -0,0 +1,186 @@ +/- +P3, x86 VM kernel entry: the Yield arm at its passing-budget state. +The native operation order is the one in XTopJointYieldSys. The charge +without timeout delivery and the SC consumed-word write each carry the +footprint used by VmEptState.ofNat. + +Platform premises [P]: ArchTotal and the existing MAX_RELEASE_TIME and +MIN_BUDGET agreements. The passing dispatch state is Entered [J]. +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.XTopJointYieldSys +import Fl4Proofs.P3.XTopJointPPH +import Fl4Proofs.P3.VmEntryS3Oracle +import Fl4Proofs.P3.SpecInvDomE +import Fl4Proofs.P3.SpecInvSgI6 +import Fl4Proofs.P3.SpecInvFoG6 + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac V X : Set U64} + {XL : U64 → Nat} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- The SC's consumed word can be written without disturbing VCPU or EPT state. -/ +private theorem JAtZB.setScConsumedN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p t : U64} (hpC : p ∈ Z.C) + (ht : t ∈ Z.T) {z : Fl4Spec.SchedContext} {n : Nat} (hkz : σ.kheap p.bv = some (.schedContext z n)) (v : U64) : + ∃ V', JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D (L.wr s (scW p 8#u64) v) a + (updSc σ p.bv { z with scConsumed := v.bv } n) ∧ + NatFoot (L := L) Z s σ (L.wr s (scW p 8#u64) v) (updSc σ p.bv { z with scConsumed := v.bv } n) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨z', n', hkz', hz⟩ := hs.scs p hpC + rw [hkz] at hkz'; cases hkz' + obtain ⟨x, hkx, -⟩ := hs.tcb t ht + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hpS : p ∈ P1.Heap.scs H := ⟨_, (hJ.sc p z n (J.c0 p hpC) hkz).2⟩ + have hal : P1.Aligned (scW p 8#u64) := (hz.hdr (K := 8#u64) (by simp) (by simp)).1 + obtain ⟨hS1, -, -⟩ := hS.set_sc_consumed hpC hkz v + have hW1 : P1.WritesIn st s (L.wr s (scW p 8#u64) v) + (fun b => ScW st Z.C Z.c s σ b ∨ SFootI st Z.T Z.c s b) := + (P1.writesIn_wr hal).mono fun b hb => Or.inl (Or.inl (hb ▸ sw_hdr hS hpC (K := 8#u64) (by simp) (by simp) (by decide))) + have hK := Ipc.nks_updateSchedContext p.bv (fun y => { y with scConsumed := v.bv }) σ ((), _) + (by rw [updateSchedContext_eq hkz]; exact Set.mem_singleton _) + have hI1 := sc_step_lift J.ip hI hW1 hK hS1 + set w : Fl4Spec.SchedContext := { z with scConsumed := v.bv } with hwd + have hpt : p.bv ≠ t.bv := fun e => by rw [e, hkx] at hkz; cases hkz + have hk1 : (updSc σ p.bv w n).kheap t.bv = some (.tcb x) := by + simp only [updSc, updK, Function.update_of_ne hpt.symm]; exact hkx + have eS : updK (updSc σ p.bv w n) t.bv (.tcb x) = updSc σ p.bv w n := by + unfold updK; rw [← hk1, Function.update_eq_self] + have hA := QKY.upd2 (w := w) (x' := x) hkz hkx rfl rfl + have hyk := ykeep_upd2 (w := w) (x' := x) hkz hkx rfl rfl + rw [eS] at hA hyk + have hInv := J.inv.scFields (y' := w) hkz rfl rfl rfl rfl + have ea : scW p 8#u64 = core.num.U64.wrapping_add p layout.sc.CONSUMED := by + unfold scW layout.sc.CONSUMED; rfl + obtain ⟨hB1, hF1, -⟩ := P1.consumedW_all (v := v) J.blk hpS + rw [← ea] at hB1 hF1 + have hw : P1.WritesIn st s (L.wr s (scW p 8#u64) v) (RegCW Z.T Z.C) := + (P1.writesIn_wr (st := st) (s := s) (v := v) hal).mono fun b hb => Or.inr ⟨p, hpC, hb⟩ + exact J.reg_frameNC hI1 hw hA hyk hInv hB1 hF1 + +set_option maxHeartbeats 16000000 in +/-- Refine Yield with the slot and native state frame at its matched outcome. -/ +theorem VmEptState.handleYieldFramesV + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hEnter : Entered σ) + (hmrt : sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) : + syscall.handle_yield st arI s a ⦃ r => r.2 = a ∧ + ∃ (σ' : Fl4Spec.State Unit) (V' : XVs) (Lst' : U64 → List U64), Fl4Spec.handleYield (Z := Unit) σ = ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac Lst' D r.1 r.2 σ' ∧ + NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have B := J.1.1 + have hC : CurInv σ := B.cur.resolve_left (by rw [hEnter.2]; intro h; cases h) + have hnsw : σ.schedulerAction ≠ .switchThread σ.curThread := by rw [hEnter.2]; intro h; cases h + have hS := B.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨hscC, hsc0, htw, hts, hact, hnr, hnb⟩ := CurInv.facts hS B.inv hC + have hct : nodeW st s Z.c layout.node.CUR_THREAD ∈ Z.T := hc.curT + have hpc : (nodeW st s Z.c layout.node.CUR_SC).bv = σ.curSc := hs.curSc.symm + obtain ⟨x, n, hk, hx⟩ := hs.scs _ hscC + have hxa : Fl4Spec.scActive x = true := hact (by rw [hpc]; exact B.curSc_ne_idle hC hEnter.1) x n hk + have hx1 := hx.strong hxa + have hne : x.scRefills ≠ [] := by + rw [hx1.refills]; intro e; exact refills_ne' hx1.wf (List.map_eq_nil_iff.1 e) + unfold syscall.handle_yield + step with sched.cur_sc.spec' as ⟨csc, hcsc⟩ + rw [hc.cur] at hcsc + subst hcsc + step with obj.Sc.consumed.spec' as ⟨i, hi⟩ + step with sched.consumed.spec' as ⟨i1, hi1⟩ + rw [hc.cur] at hi1 + simp only [lift, bind_ok] + step with sporadic.head.spec as ⟨hd, hhd⟩ + have hhead : (Fl4Spec.refillHd x).rAmount = hd.amount.bv := by + rw [refillHd_eq hx1.wf hx1.refills, rf0_eq hx1.wf, ← hhd]; rfl + refine WP.spec_bind (B.toHole.chargeBudgetJ hAT hC hEnter.1 hnsw hmrt hmb hd.amount) ?_ + rintro ⟨s1, a1⟩ ⟨ha1, σ1, h1, -, Lst1, B1, F1⟩ + simp only at ha1 B1 F1 + subst a1 + step with Sched.obj.Sc.set_consumed.spec' as ⟨s2, hs2⟩ + subst hs2 + obtain ⟨Q1, R1, hc1, hs1⟩ := B1.xtop.fin.1.1.1.lists + obtain ⟨z, n1, hkz, -⟩ := hs1.scs _ hscC + obtain ⟨V', B2, F2⟩ := B1.setScConsumedN hscC hct hkz (core.num.U64.wrapping_add i i1) + have F := B.natTrans F1 F2 + have J2 := J.ofNat B2 F + refine ⟨_, V', Lst1, ?_, B2, J2.1.2, J2.2, F⟩ + have hsp : Fl4Spec.handleYield (Z := Unit) σ = (Fl4Spec.chargeBudget (Z := Unit) hd.amount.bv false >>= fun _ => + Fl4Spec.setScObjRef Fl4Spec.scConsumedUpdate (nodeW st s Z.c layout.node.CUR_SC).bv + (x.scConsumed + σ.consumedTime)) σ := by + unfold Fl4Spec.handleYield + rw [nd_gets_bind, ← hpc, nd_bind_one (getSchedContext_eq hk), nd_gets_bind, + nd_bind_one (getRefillHead_eq hk hne), hhead] + rw [hsp, nd_bind_one h1] + unfold Fl4Spec.setScObjRef + rw [updateSchedContext_eq hkz] + have ev : x.scConsumed + σ.consumedTime = (core.num.U64.wrapping_add i i1).bv := by + rw [core.num.U64.wrapping_add_bv_eq, hi, hi1, hx.consumed, hs.consumed] + rw [ev]; rfl + +/-- The concrete Yield arm refines the spec's head-refill charge and consumed-word update. -/ +theorem VmEptState.handleYieldV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hEnter : Entered σ) + (hmrt : sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) : + syscall.handle_yield st arI s a ⦃ r => r.2 = a ∧ + ∃ σ', Fl4Spec.handleYield (Z := Unit) σ = ({((), σ')}, False) ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := + WP.spec_mono (J.handleYieldFramesV hAT hEnter hmrt hmb) + fun _ ⟨e, σ', _, _, hσ', J', _⟩ => ⟨e, σ', hσ', J'.anyX arI⟩ + +/-- The Yield post for the entry's paired passing-budget outcomes. -/ +theorem yieldArmV (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hEnter : Entered σ) {x : Fl4Spec.Tcb} (hx : σ.kheap σ.curThread = some (.tcb x)) + {s2 : S} {a2 : A} {σb : Fl4Spec.State Unit} + (hb : BudgetAt σ σb true) (hVE : VEAnyX (L := L) arI AR s2 a2 σb) : + syscall.handle_yield st arI s2 a2 ⦃ r => + ∃ z ∈ (Fl4Spec.handleYield (Z := Unit) σb).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, J⟩ := hVE + refine WP.spec_mono (J.handleYieldV hAT (hb.entered hnd hx hEnter) (hmrt a2) (hmb a2)) ?_ + rintro r ⟨-, σ', hs, hV'⟩ + exact ⟨((), σ'), by rw [hs]; exact Set.mem_singleton _, hV'⟩ + +/-- Carry the finish facts through the matched Yield result. +[J] The finish invariants at the passing budget state. +[P] The architecture and scheduler constants for the native charge. -/ +theorem VmEptState.handleYieldFinishS3 + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hEnter : Entered σ) + (hmrt : sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) : + syscall.handle_yield st arI s a ⦃ r => ∃ z ∈ (Fl4Spec.handleYield (Z := Unit) σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ∧ VmOrc st on rc r.1 z.2 ∧ + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ IdleVt z.2 ⦄ := by + refine WP.spec_mono (J.handleYieldFramesV hAT hEnter hmrt hmb) ?_ + rintro r ⟨_, σ', _, _, hYield, J', F⟩ + have hz : ((), σ') ∈ (Fl4Spec.handleYield (Z := Unit) σ).1 := by + rw [hYield] + exact Set.mem_singleton _ + have B := J.1.1 + have B' := J'.1.1 + have hd := SInvDom.domListOK_of_dlV (SInvDom.dl_handleYield σ ((), σ') hz) hDom + obtain ⟨hOwn, hFo'⟩ := SInvF.oi_handleYield σ ⟨rfl, hFo⟩ ((), σ') hz + have hIdle' := idleVt_ofSlots B.xtop B.jpl B.zH.1 B'.xtop F.ss + (SInvG.idk_handleYield σ rfl ((), σ') hz) hIdle + exact ⟨((), σ'), hz, J'.anyX arI, B.vmOrcAcross B' hO F.fk.1.2.2.1, + hd, fun o ho => hFo' o (hOwn.symm.trans ho), hIdle'⟩ + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmEptMakePool.lean b/proofs/Fl4Proofs/P3/VmEptMakePool.lean new file mode 100644 index 000000000..8b0d2a003 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmEptMakePool.lean @@ -0,0 +1,447 @@ +import Fl4Proofs.P3.JAtZBMakePool +import Fl4Proofs.P3.EptCore +import Fl4Proofs.P3.EptStep2 +import Fl4Proofs.P3.EptFinRoot +import Fl4Proofs.P3.EptDelRoot +import Fl4Proofs.P3.XTopJointMakePool +import Fl4Proofs.P3.VsMakePool +import Fl4Proofs.P3.RetFoot + +/-! +Guest ASID and EPT naming facts for the installation of a fresh empty +native pool after the matched MakePool detype. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Classical fl4_core Vs Notif Sched + +noncomputable section + +private theorem freshPoolNotNamed {P : Set U64} {m : U64 → U64} {kb : U64} + {σ : Fl4Spec.State Unit} (R : VsRel P m kb σ) {pool : Fl4Spec.ObjRef} + (hFresh : σ.kheap pool = none) (hi : Fl4Spec.AsidHighIndex) : + σ.archState.x64AsidTable hi ≠ some pool := by + have hlt : hi.toNat < 2048 := hi.isLt + let i : U64 := ⟨BitVec.ofNat 64 hi.toNat⟩ + have hval : i.val = hi.toNat := by + change (BitVec.ofNat 64 hi.toNat).toNat = hi.toNat + rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt (by omega)] + have hcast : (Fl4Spec.ucast 11 i.bv : BitVec 11) = hi := by + apply BitVec.eq_of_toNat_eq + simp only [Fl4Spec.ucast, BitVec.toNat_setWidth] + change i.val % (2 ^ 11) = hi.toNat + rw [hval, Nat.mod_eq_of_lt (by omega)] + intro hEntry + rw [← hcast, R.table i (by omega)] at hEntry + have hNonzero : m (atA kb i) ≠ 0#u64 := fun h => by + rw [h, optRef_zero] at hEntry + cases hEntry + obtain ⟨f, hPool, -⟩ := R.pool _ (R.tmem i (by omega) hNonzero) + rw [optRef_some hNonzero] at hEntry + rw [Option.some.inj hEntry, hFresh] at hPool + cases hPool + +/-- The pair of VM facts needed after MakePool's fresh pool insertion. +Every populated entry is kept at the same ASID. The newly named pool is +empty, and its object address could not be named before allocation. -/ +theorem EptNamed.frameEmptyNativePool + {th : TypedHeap} {m m' : U64 → U64} {σ σ' : Fl4Spec.State Unit} + (N : EptNamed th m σ) {P : Set U64} {kb : U64} (R : VsRel P m kb σ) + {pool : Fl4Spec.ObjRef} {slot : Fl4Spec.AsidHighIndex} (hFresh : σ.kheap pool = none) + (hTable : σ'.archState.x64AsidTable = Function.update σ.archState.x64AsidTable slot (some pool)) + (hEmpty : σ'.kheap pool = some (.archObj (.asidPool fun _ => none))) + (hArch : ∀ o ao, o ≠ pool → σ'.kheap o = some (.archObj ao) → σ.kheap o = some (.archObj ao)) + (hCaps : ∀ y, IsSlot th y → ∀ p l mm a v, capV m y = .Ept p l mm a v ↔ capV m' y = .Ept p l mm a v) : + (∀ e, GuestEntry σ' e → GuestEntry σ e) ∧ EptNamed th m' σ' := by + have hOld (hi : Fl4Spec.AsidHighIndex) := freshPoolNotNamed R hFresh hi + have hAt : σ'.archState.x64AsidTable slot = some pool := by rw [hTable, Function.update_self] + have hNeTab : ∀ hi, hi ≠ slot → σ'.archState.x64AsidTable hi = σ.archState.x64AsidTable hi := by + intro hi h + rw [hTable, Function.update_of_ne h] + have hBack : ∀ hi lo e, PoolAt σ' hi lo e → PoolAt σ hi lo e := by + rintro hi lo e ⟨p, f, htab, hk, he⟩ + by_cases hs : hi = slot + · subst hi + rw [hAt] at htab + have hp : p = pool := (Option.some.inj htab).symm + subst p + rw [hEmpty] at hk + injection hk with hk + injection hk with hk + injection hk with hk + subst f + cases he + · rw [hNeTab hi hs] at htab + have hn : p ≠ pool := fun e => hOld hi (e ▸ htab) + exact ⟨p, f, htab, hArch p (.asidPool f) hn hk, he⟩ + have hInj : PoolInj σ' := by + refine ⟨?_, fun hi lo hi' lo' e e' h h' ht ht' he => N.inj.root hi lo hi' lo' e e' + (hBack hi lo e h) (hBack hi' lo' e' h') ht ht' he⟩ + intro hi hi' p h h' + by_cases h1 : hi = slot <;> by_cases h2 : hi' = slot + · exact h1.trans h2.symm + · subst hi + rw [hAt] at h + have hp : p = pool := (Option.some.inj h).symm + subst p + exact (hOld hi' ((hNeTab _ h2).symm.trans h')).elim + · subst hi' + rw [hAt] at h' + have hp : p = pool := (Option.some.inj h').symm + subst p + exact (hOld hi ((hNeTab _ h1).symm.trans h)).elim + · exact N.inj.tab hi hi' p ((hNeTab _ h1).symm.trans h) ((hNeTab _ h2).symm.trans h') + have hEpt : ∀ o l es, σ'.kheap o = some (.archObj (.ept l es)) → σ.kheap o = some (.archObj (.ept l es)) := by + intro o l es h + exact hArch o (.ept l es) (fun e => by rw [e, hEmpty] at h; cases h) h + have hck : ∀ y p l mm a v, IsSlot th y → capV m y = .Ept p l mm a v → capV m' y = .Ept p l mm a v := + fun y p l mm a v hy h => (hCaps y hy p l mm a v).1 h + have hcb : ∀ y p l mm a v, IsSlot th y → capV m' y = .Ept p l mm a v → ∃ z, IsSlot th z ∧ capV m z = .Ept p l mm a v := + fun y p l mm a v hy h => ⟨y, hy, (hCaps y hy p l mm a v).2 h⟩ + refine ⟨?_, N.keep (GPath.mono' (fun hi lo e h _ => Or.inl (hBack hi lo e h)) + (fun o l es h => Or.inl (hEpt o l es h))) hInj + (fun y p l a v hy h _ => ⟨y, hy, hck y p l true a v hy h⟩) + (fun y p l mm a v hy h => Or.inl (hcb y p l mm a v hy h)) + (N.rootK (fun hi lo e h _ => hBack hi lo e h) + (fun _ _ _ _ _ _ y p l a v hy _ hzero h => ⟨y, l, a, v, hy, hzero, hck y p l true a v hy h⟩) + (fun _ _ _ _ _ _ y p l mm a v hy _ h => hcb y p l mm a v hy h))⟩ + rintro e ⟨hi, p, f, lo, ht, hk, he⟩ + exact (hBack hi lo e ⟨p, f, ht, hk, he⟩).guest + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac D X : Set U64} +variable {Lst : U64 → List U64} {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The post-drop MakePool write keeps the EPT state. Its only new heap +object is the empty native pool, in the selected free 4 KiB frame. -/ +theorem EptCore.frameMakePool + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : EptCore (L := L) X XL Z.th s σ) + {p hi : U64} (hh : hi.val < 2048) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + (hFresh : σ.kheap p.bv = none) + {pp pd : U64} {rs rd : Nat} {idx idxd : List Bool} {fi : U64} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) + (hpd : Z.th pd = some (.cnode rd)) (hld : idxd.length = rd) + (hsrc0 : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi false)) + (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (MpW Z.th p (L.ksb s) hi)) + (F : MpFr σ σ') + (hNew : ∀ o ao, σ'.kheap o = some (.archObj ao) → σ.kheap o = none → o = p.bv) + (hkp : σ'.kheap p.bv = some (.archObj (.asidPool fun _ => none))) + (hat : σ'.archState.x64AsidTable = Function.update σ.archState.x64AsidTable (Fl4Spec.ucast 11 hi.bv) (some p.bv)) + (hsrc : capV (L.mem s') (cslotAddr (pp.bv, idx)) = .Untyped p fl4_abi.consts.bits.ASID_POOL 4096#u64 false) + (hdst : ∃ base, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p base) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pp.bv, idx) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) : + EptCore (L := L) X XL Z.th s' σ' := by + have hpApart : ∀ t ∈ X, t.val + 4096 ≤ p.val ∨ p.val + 4096 ≤ t.val := by + intro t ht + have h := hHo t _ (B.eptHeap C ht) + simpa [P1.HKind.bits] using h + have hW : ∀ b, MpW Z.th p (L.ksb s) hi b → ∀ t ∈ X, b.val < t.val ∨ t.val + 4096 ≤ b.val := by + intro b hb t ht + rcases hb with ⟨x, off, hIs, hOff, e⟩ | hRg | rfl + · exact JAtZB.slotWd_ept B C b ⟨x, hIs, off, hOff, e⟩ t ht + · rcases hpApart t ht with h | h + · exact Or.inr (h.trans hRg.1) + · exact Or.inl (lt_of_lt_of_le hRg.2 h) + · obtain ⟨es, hEpt, -⟩ := C.rel.obj t ht + have h := B.vsW_apart (t := t) (n := 4096) (fun o ko hk hn => C.sep t ht o ko hk hn) + (C.ks t ht) + (fun q hq e => by + obtain ⟨obj, hk, -, hn, -⟩ := B.vsFrSpec hq + rw [e, hEpt] at hk + cases hk + exact hn _ _ rfl) + (show VsW Z (L.ksb s) (atA (L.ksb s) hi) from Or.inr (Or.inl ⟨hi, hh, rfl⟩)) + omega + obtain ⟨base, hdst⟩ := hdst + have hOldSrc := capV_of_capAt hsrc0 + have hOldDst := capV_of_capAt hnull + have hCaps : ∀ y, IsSlot Z.th y → ∀ ptr lv mm asid va, + capV (L.mem s) y = .Ept ptr lv mm asid va ↔ capV (L.mem s') y = .Ept ptr lv mm asid va := by + intro y hy ptr lv mm asid va + by_cases hsrcEq : y = cslotAddr (pp.bv, idx) + · rw [hsrcEq, hsrc, hOldSrc] + constructor <;> intro h <;> cases h + by_cases hdstEq : y = cslotAddr (pd.bv, idxd) + · rw [hdstEq, hdst, hOldDst] + constructor <;> intro h <;> cases h + · rw [hcap y hy hsrcEq hdstEq] + have hArch : ∀ o ao, o ≠ p.bv → σ'.kheap o = some (.archObj ao) → σ.kheap o = some (.archObj ao) := by + intro o ao hn hk + rcases F.kh o with e | ⟨r, cs, cs', h0, h1⟩ | ⟨h0, ao', h1⟩ + · rwa [e] at hk + · rw [h1] at hk; cases hk + · exact (hn (hNew o ao hk h0)).elim + have hkEpt : ∀ o l es, σ'.kheap o = some (.archObj (.ept l es)) → σ.kheap o = some (.archObj (.ept l es)) := by + intro o l es hk + exact hArch o (.ept l es) (fun e => by rw [e, hkp] at hk; cases hk) hk + have hxk : ∀ t ∈ X, σ'.kheap t.bv = σ.kheap t.bv := by + intro t ht + obtain ⟨es, hk, -⟩ := C.rel.obj t ht + exact (F.arch hk).trans hk.symm + obtain ⟨hge, hnm⟩ := C.named.frameEmptyNativePool B.xtop.fin.2.1 hFresh hat hkp hArch hCaps + have hEx : JExtEpt (L := L) X Z.th s' := by + refine ⟨?_, ⟨?_, ?_⟩, ?_⟩ + · intro y hy + by_cases hs : y = cslotAddr (pp.bv, idx) + · rw [hs, hsrc]; trivial + by_cases hd : y = cslotAddr (pd.bv, idxd) + · rw [hd, hdst]; trivial + rw [hcap y hy hs hd] + exact C.exte.cap y hy + · intro y hy ptr lv asid va hc + exact C.exte.vtop.1 y hy ptr lv asid va ((hCaps y hy ptr lv true asid va).2 hc) + · intro y hy ptr sz rt dev asid va hc + by_cases hs : y = cslotAddr (pp.bv, idx) + · rw [hs, hsrc] at hc; cases hc + by_cases hd : y = cslotAddr (pd.bv, idxd) + · rw [hd, hdst] at hc; cases hc + · rw [hcap y hy hs hd] at hc + exact C.exte.vtop.2 y hy ptr sz rt dev asid va hc + · intro y hy ptr lv asid va hc + exact C.exte.anz y hy ptr lv asid va ((hCaps y hy ptr lv true asid va).2 hc) + have h5 : EptSlotStep Z.th Z.th (L.mem s) (L.mem s') := by + intro t ht + refine Or.inr (Or.inr ⟨ht, hcap _ (isSlot_eptSlot ht) ?_ ?_⟩) + · exact (cnode_slot_ne_tcb B.xtop.rel.1 hpp hidx ht 5 (by decide)).symm + · exact (cnode_slot_ne_tcb B.xtop.rel.1 hpd hld ht 5 (by decide)).symm + have hSep : ∀ t ∈ X, ∀ o ko, σ'.kheap o = some ko → o ≠ t.bv → EptApart t o ko := by + intro t ht o ko hk hn + rcases F.kh o with e | ⟨r, cs, cs', h0, h1⟩ | ⟨h0, ao, h1⟩ + · rw [e] at hk + exact C.sep t ht o ko hk hn + · rw [h1] at hk + cases hk + have h := C.sep t ht o (.cnode r cs) h0 hn + simpa only [EptApart, Fl4Spec.objBits] using h + · have he := hNew o ao h1 h0 + subst o + rw [hkp] at hk + cases hk + unfold EptApart + rw [objBits_pool] + exact (hpApart t ht).elim Or.inr Or.inl + have hEmpty : ∀ t : U64, t ∈ (∅ : Set U64) → False := fun _ h => h.elim + have hC := C.growS (T := ∅) (fun t h => (hEmpty t h).elim) XL (fun _ _ => rfl) + hw hW (fun t h => (hEmpty t h).elim) (fun t h => (hEmpty t h).elim) + (fun t h => (hEmpty t h).elim) hxk + (fun o l es h => Or.inr ⟨l, es, hkEpt o l es h⟩) + (fun y hy ptr lv asid va mm hc => Or.inr ⟨hy, (hCaps y hy ptr lv mm asid va).2 hc⟩) + (fun t h => (hEmpty t h).elim) (by simpa only [Set.empty_union] using hEx) + hge (fun t h => (hEmpty t h).elim) + (by simpa only [Set.empty_union] using hSep) h5 hnm (fun t h => (hEmpty t h).elim) + simpa only [Set.empty_union] using hC + +variable {V : Set U64} + +/-- Fresh installation of the native pool adds its heap object outside +the VCPU regions. The only other changes are same-size CNode updates. -/ +theorem JVm.frameMakePool (J : JVm (L := L) V H s σ) {p : U64} + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + {s' : S} {σ' : Fl4Spec.State Unit} (hk : L.ksb s' = L.ksb s) (F : MpFr σ σ') + (hNew : ∀ o ao, σ'.kheap o = some (.archObj ao) → σ.kheap o = none → o = p.bv) + (hkp : σ'.kheap p.bv = some (.archObj (.asidPool fun _ => none))) : + JVm (L := L) V (hAddP H p) s' σ' := by + have hOut (v) (hv : v ∈ V) : v.val + 32768 ≤ p.val ∨ p.val + 4096 ≤ v.val := by + simpa only [P1.HKind.bits, Nat.reducePow] using hHo v _ (J.heap v hv) + refine J.step (fun _ h => h) (fun v hv => hAddP_ne (fun e => by + have hh := hOut v hv + rw [e] at hh + omega)) hk (fun v hv => ?_) (fun o ko ho => ?_) + · obtain ⟨x, hx⟩ := J.spec v hv + exact ⟨x, F.arch hx⟩ + · rcases F.kh o with e | ⟨r, cs, cs', h1, h2⟩ | ⟨h1, ao, h2⟩ + · rw [e] at ho + exact Or.inl ⟨ko, ho, rfl⟩ + · rw [h2] at ho + cases ho + exact Or.inl ⟨.cnode r cs, h1, rfl⟩ + · have e := hNew o ao h2 h1 + subst o + rw [hkp] at ho + cases ho + right + intro v hv _ + unfold VcpuApart + rw [objBits_pool] + exact (hOut v hv).elim Or.inr Or.inl + +/-- The slot writes, the new pool page, and the ASID table word miss +the guest VCPU words, VCPU list head, and TCB binding/state words. -/ +theorem JAtZB.makePoolRetFootV (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (C : VcpuCore (L := L) V Z H s σ) {p hi : U64} (hh : hi.val < 2048) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + (Ad : P1.AddOKP st H (· = p) (.other 12) Cs s) : + RetFootV (L := L) V Z.th s (MpW Z.th p (L.ksb s) hi) := by + intro b hb + rcases hb with ⟨y, off, hy, hOff, rfl⟩ | hRg | rfl + · exact B.retFootV_slot C _ ⟨y, hy, off, hOff, rfl⟩ + · refine ⟨fun v hv => ?_, ?_, fun t ht => ?_⟩ + · have e := hHo v _ (C.vm.heap v hv) + simp only [P1.HKind.bits, Nat.reducePow] at e + omega + · intro e + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val B.kern.ksbFit + rw [e, hvh] at hRg + have hKs := Ad.ks p rfl + simp only [P1.HKind.bits, Nat.reducePow] at hKs + omega + · obtain ⟨hV, hS⟩ := B.tcbVS ht + have hT := hHo t _ (B.zH.1 t (B.thT t ht)) + simp only [P1.HKind.bits, Nat.reducePow] at hT + constructor <;> intro e <;> have eq := congrArg UScalar.val e <;> omega + · have hAt : VsW Z (L.ksb s) (atA (L.ksb s) hi) := Or.inr (Or.inl ⟨hi, hh, rfl⟩) + refine ⟨fun v hv => ?_, B.vsW_head hAt, fun t ht => ?_⟩ + · obtain ⟨x, hx⟩ := C.vm.spec v hv + have h := B.vsW_apart (C.vm.sep v hv) (C.vm.ks v hv) + (fun q hq e => by + obtain ⟨ao, ha, -, -, hnv⟩ := B.vsFrSpec hq + rw [e, hx] at ha + cases ha + exact hnv x rfl) hAt + omega + · obtain ⟨hV, hS⟩ := B.tcbVS ht + have h := B.vsObjW ⟨B.scOut⟩ hAt (B.zH.1 t (B.thT t ht)) (fun _ he => by cases he) + simp only [P1.HKind.bits, Nat.reducePow] at h + constructor <;> intro e <;> have eq := congrArg UScalar.val e <;> omega + +/-- The VCPU overlay after the exact native MakePool step. The insertion +supplies its `MdbStep`, with the new native pool disjoint from every VCPU. -/ +theorem VcpuCore.frameMakePool + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : VcpuCore (L := L) V Z H s σ) + {p hi : U64} (hh : hi.val < 2048) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + (Ad : P1.AddOKP st H (· = p) (.other 12) Cs s) + {ps ds : U64} {fi : U64} + (hsrc0 : capV (L.mem s) ps = .Untyped p fl4_abi.consts.bits.ASID_POOL fi false) + (hnull : capV (L.mem s) ds = .Null) + {s' : S} {σ' : Fl4Spec.State Unit} {G' : MdbGhost} + (hw : P1.WritesIn st s s' (MpW Z.th p (L.ksb s) hi)) (F : MpFr σ σ') + (hNew : ∀ o ao, σ'.kheap o = some (.archObj ao) → σ.kheap o = none → o = p.bv) + (hkp : σ'.kheap p.bv = some (.archObj (.asidPool fun _ => none))) + (hsrc : capV (L.mem s') ps = .Untyped p fl4_abi.consts.bits.ASID_POOL 4096#u64 false) + (hdst : ∃ base, capV (L.mem s') ds = .AsidPool p base) + (hcap : ∀ y, IsSlot Z.th y → y ≠ ps → y ≠ ds → capV (L.mem s') y = capV (L.mem s) y) + (hStep : MdbStep (L.mem s) Z.G (L.mem s') G') : + VcpuCore (L := L) V (Z.mp G' p) (hAddP H p) s' σ' := by + have hFoot := B.makePoolRetFootV C hh hHo Ad + have nV : ∀ v ∈ V, ∀ b, MpW Z.th p (L.ksb s) hi b → b.val < v.val ∨ v.val + 32768 ≤ b.val := + fun v hv b hb => (hFoot b hb).1 v hv + have Ly := B.jvLay C.vm + have Pw := B.jwPl C.vm + have kTV : VKeep (L := L) Z.th s s' := fun t ht => + hw.mem_eq (Pw.jw _ (Or.inr (Or.inl ⟨t, ht, rfl⟩))) fun hW => ((hFoot _ hW).2.2 t ht).1 rfl + have kSt : ∀ t, Z.th t = some .tcb → L.mem s' (P1.stA t) = L.mem s (P1.stA t) := fun t ht => + hw.mem_eq (Pw.sw _ ⟨t, ht, rfl⟩) fun hW => ((hFoot _ hW).2.2 t ht).2 rfl + have kVT := B.kVn C.vm hw nV + have hvk : ∀ v ∈ V, σ'.kheap v.bv = σ.kheap v.bv := fun v hv => by + obtain ⟨x, hx⟩ := C.vm.spec v hv + exact (F.arch hx).trans hx.symm + have hnv : ∀ o x, σ'.kheap o = some (.archObj (.vcpu x)) → σ.kheap o = some (.archObj (.vcpu x)) := by + intro o x ho + rcases F.kh o with e | ⟨r, cs, cs', -, e⟩ | ⟨h0, ao, h1⟩ + · rwa [e] at ho + · rw [e] at ho + cases ho + · have e := hNew o ao h1 h0 + rw [e, hkp] at ho + cases ho + obtain ⟨base, hdst⟩ := hdst + have E : JExtVcpu (L := L) V Z.th s' := by + refine ⟨fun y hy => ?_, fun t ht => ?_, fun v hv => ?_, C.extv.nz⟩ + · by_cases hs : y = ps + · rw [hs, hsrc] + trivial + by_cases hd : y = ds + · rw [hd, hdst] + trivial + rw [hcap y hy hs hd] + exact C.extv.cap y hy + · rw [kTV t ht] + rcases C.extv.bnd t ht with e | ⟨hv, e⟩ + · exact Or.inl e + · exact Or.inr ⟨hv, by rw [kVT _ hv]; exact e⟩ + · rw [kVT v hv] + rcases C.extv.back v hv with e | ⟨ht, e⟩ + · exact Or.inl e + · exact Or.inr ⟨ht, by rw [kTV _ ht]; exact e⟩ + have R : RunVmB (L := L) Z.th s' := fun t ht hst => by + rw [kTV t ht] + exact C.rvm t ht (by rwa [kSt t ht] at hst) + have Vl : VList st V Z.th s' := C.vl.writes hw fun b hb => by + refine ⟨C.vl.lw_al Ly.vfit hb, fun hW => ?_⟩ + rcases hb with rfl | ⟨v, hv, rfl | rfl⟩ + · exact (hFoot _ hW).2.1 rfl + · have h := lay_next_val (Ly.vfit v hv).2 + rcases nV v hv _ hW with e | e <;> omega + · have h := lay_prev_val (Ly.vfit v hv).2 + rcases nV v hv _ hW with e | e <;> omega + have keepSlot : ∀ y, IsSlot Z.th y → ∀ cap, capV (L.mem s) y = cap → + (∀ q sb f d, cap ≠ .Untyped q sb f d) → cap ≠ .Null → capV (L.mem s') y = cap := by + intro y hy cap hc hU hN + have hs : y ≠ ps := fun e => by rw [e, hsrc0] at hc; exact hU _ _ _ _ hc.symm + have hd : y ≠ ds := fun e => by rw [e, hnull] at hc; exact hN hc.symm + exact (hcap y hy hs hd).trans hc + exact ⟨E, C.vo.frame hnv, + C.vr.frame (fun v _ vs hk => hnv _ vs hk) (v3_agreeW hw (fun v hv => (Ly.vfit v hv).2) nV), + (VirtRngOne.mdbStep_rngOne hStep B.ut C.rngOne).2, + R, C.vm.frameMakePool hHo hw.1 F hNew hkp, Vl, C.vb.ofV hvk kVT, + fun v hv => by + obtain ⟨y, hy, hc⟩ := C.vcap v hv + exact ⟨y, hy, keepSlot y hy _ hc (fun _ _ _ _ e => by cases e) (fun e => by cases e)⟩, + C.tcap.of_caps (fun v hv _ => ⟨hv, kVT v hv⟩) fun y hy t hc => + ⟨y, hy, keepSlot y hy _ hc (fun _ _ _ _ e => by cases e) (fun e => by cases e)⟩⟩ + +/-- Reassemble the VM state from the native MakePool base and the two +architecture overlays, using the native step's exact writes and insertion. -/ +theorem VmEptState.frameMakePool + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {p hi : U64} (hh : hi.val < 2048) + (hHo : ∀ q k, H q = some k → q.val + 2 ^ k.bits ≤ p.val ∨ p.val + 4096 ≤ q.val) + (Ad : P1.AddOKP st H (· = p) (.other 12) Cs s) + (hFresh : σ.kheap p.bv = none) + {pp pd : U64} {rs rd : Nat} {idx idxd : List Bool} {fi : U64} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) + (hpd : Z.th pd = some (.cnode rd)) (hld : idxd.length = rd) + (hsrc0 : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p fl4_abi.consts.bits.ASID_POOL fi false)) + (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {G' : MdbGhost} + (B' : JAtZB (L := L) arI AR (Z.mp G' p) (hAddP H p) Cs Ac Lst D s' a' σ') + (hw : P1.WritesIn st s s' (MpW Z.th p (L.ksb s) hi)) (F : MpFr σ σ') + (hkh : ∀ o, o ≠ p.bv → σ'.kheap o = σ.kheap o ∨ + (o ∈ regionSet p.bv 12 ∧ σ'.kheap o = none) ∨ + ∃ r cs cs', σ.kheap o = some (.cnode r cs) ∧ σ'.kheap o = some (.cnode r cs')) + (hkp : σ'.kheap p.bv = some (.archObj (.asidPool fun _ => none))) + (hat : σ'.archState.x64AsidTable = Function.update σ.archState.x64AsidTable (Fl4Spec.ucast 11 hi.bv) (some p.bv)) + (hsrc : capV (L.mem s') (cslotAddr (pp.bv, idx)) = .Untyped p fl4_abi.consts.bits.ASID_POOL 4096#u64 false) + (hdst : ∃ base, capV (L.mem s') (cslotAddr (pd.bv, idxd)) = .AsidPool p base) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pp.bv, idx) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) + (hStep : MdbStep (L.mem s) Z.G (L.mem s') G') : + VmEptState (L := L) arI V X XL AR (Z.mp G' p) (hAddP H p) Cs Ac Lst D s' a' σ' := by + obtain ⟨⟨B, Cv⟩, Ce⟩ := J + have hNew : ∀ o ao, σ'.kheap o = some (.archObj ao) → σ.kheap o = none → o = p.bv := by + intro o ao h' h0 + by_contra he + rcases hkh o he with h | ⟨-, h⟩ | ⟨r, cs, cs', -, h⟩ + · rw [h, h0] at h' + cases h' + · rw [h] at h' + cases h' + · rw [h] at h' + cases h' + exact ⟨⟨B', Cv.frameMakePool B hh hHo Ad (capV_of_capAt hsrc0) (capV_of_capAt hnull) + hw F hNew hkp hsrc hdst hcap hStep⟩, + Ce.frameMakePool B hh hHo hFresh hpp hidx hpd hld hsrc0 hnull hw F hNew hkp hat hsrc hdst hcap⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmFaultMid.lean b/proofs/Fl4Proofs/P3/VmFaultMid.lean new file mode 100644 index 000000000..ea99cfe31 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFaultMid.lean @@ -0,0 +1,2293 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 2, generated by `handoff/tools/gen_faultmid.py`): **`handle_fault` at a mid +state whose action is resumed or choose** (`CurInv` or an SC-less current thread): twins `…MB` of the resumed `JAtZB` +fault lemmas (P3/XTopJointNoFault, XTopJointFaultEp, XTopJointFaultRecv), same post `JAtN` (with `NatFoot`); +the leaf `VmEptState.handleFaultMVE`. The pending-switch state is not here. No named premise beyond `P0.ArchTotal`. +-/ +import Fl4Proofs.P3.VmRecvMidEp2 +import Fl4Proofs.P3.VmMidBlock +import Fl4Proofs.P3.XTopJointFaultRecv +import Fl4Proofs.P3.XTopJointFaultRecvM +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- Replacing the fault of a thread keeps `CurInv`. -/ +theorem curInv_setFault {σ : Fl4Spec.State Unit} (h : CurInv σ) {t : Fl4Spec.ObjRef} {x : Fl4Spec.Tcb} + (hk : σ.kheap t = some (.tcb x)) (f : Option Fl4Spec.Fault) : CurInv (updK σ t (.tcb { x with tcbFault := f })) := by + obtain ⟨xc, hkc, hcs, hnb⟩ := h.tcb + obtain ⟨y, n, hy⟩ := h.sc + have hsc : σ.curSc ≠ t := fun e => by rw [e, hk] at hy; cases hy + have hk2 : (updK σ t (.tcb { x with tcbFault := f })).kheap σ.curSc = σ.kheap σ.curSc := by + simp [updK, hsc] + refine ⟨?_, ⟨y, n, hk2.trans hy⟩, fun hne y' n' h' => h.active hne y' n' (hk2.symm.trans h'), h.notRel⟩ + by_cases e : σ.curThread = t + · rw [e, hk] at hkc; cases hkc + exact ⟨{ x with tcbFault := f }, by show Function.update σ.kheap t _ σ.curThread = _; rw [e, Function.update_self], hcs, hnb⟩ + · exact ⟨xc, by show Function.update σ.kheap t _ σ.curThread = _; rw [Function.update_of_ne e]; exact hkc, hcs, hnb⟩ + +/-- The concrete SC word of a thread agrees with the spec's donation flag. -/ +theorem JAtZB.faultDonWord (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {ct : U64} (ht : ct ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) : + (L.mem s (scA ct) != 0#u64) = decide (x.tcbSchedContext ≠ none) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + obtain ⟨xt6, hkt6, -, -, hsct6, -⟩ := hs.tcb ct ht + rw [hk] at hkt6; cases hkt6 + rw [hsct6] + by_cases h0 : L.mem s (scA ct) = 0#u64 + · simp [h0, Notif.optRef] + · simp [h0, Notif.optRef] + +set_option maxHeartbeats 4000000 in +/-- `stsCurJ` at a resumed or choose state. -/ +theorem JAtZB.stsCurMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hti : σ.curThread ≠ σ.idleThread) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) {t : U64} (ht : t ∈ Z.T) + (hnr : t.bv ∉ σ.releaseQueue) (htD : t ∉ D) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hcur : σ.curThread = t.bv) + (hrn : Fl4Spec.runnable x.tcbState = true) (hnq : ∀ d p, t.bv ∉ σ.readyQueues d p) + (hB : sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s6 => ∃ (Lst' : U64 → List U64), + JAtZB (L := L) arI AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := .inactive })) t.bv) ∧ + P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) ⦄ := by + have hts : tsCode Fl4Spec.ThreadState.inactive < 3 := by decide + have hcode := runnable_code hrn + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rw [hst]; omega, by rw [hst]; omega⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by omega) .inactive hts sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hS ht _)) (set_thread_state_yw hS ht _)) hB) ?_ + rintro s6 ⟨⟨⟨⟨hR6, hS6r⟩, hw6⟩, hy6⟩, hblk, hfoot⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + set x6 : Fl4Spec.Tcb := { x with tcbState := .inactive } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ p, p ≠ t.bv → σ6.kheap p = σ.kheap p := by + intro p hp; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hp] + have hcn : σ6.schedulerAction = .chooseNewThread := by + have h3 : ¬ Fl4Spec.schedulable t.bv σ1 = true := fun h => by + obtain ⟨y, _, _, _, hky, _, hr, _⟩ := schedulable_inv h + rw [hk1] at hky; cases hky; simp [hx6, Fl4Spec.runnable] at hr + rcases hsa with hsa | hsa + swap + · rw [hσ6]; unfold stsAct + rw [if_neg (fun h' => by + have h2 : σ.schedulerAction = .resumeCurThread := h'.2.1 + rw [hsa] at h2; cases h2)] + exact hsa + have h12 : t.bv = σ1.curThread ∧ σ1.schedulerAction = .resumeCurThread ∧ + ¬ Fl4Spec.schedulable t.bv σ1 = true := + ⟨hcur.symm, hsa, h3⟩ + rw [hσ6]; unfold stsAct; rw [if_pos h12] + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_csc : σ6.curSc = σ.curSc := by rw [hrec]; rfl + have e_cons : σ6.consumedTime = σ.consumedTime := by rw [hrec]; rfl + have e_tm : σ6.curTime = σ.curTime := by rw [hrec]; rfl + have e_idle : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have e_rel : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := runnable_free hrn + have hK : ScTKw (fun p => p = t.bv) σ σ6 := by + refine ⟨fun p => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, tsLow_free hts⟩⟩) + · rw [hkp p hp]; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + -- `Inv` + have hI1 : Inv σ1 := by + refine J.inv.step (NtfnStep.tcb hk .inactive x.tcbArch) (fun u x0 y h1 h2 hqd hr => ?_) + (fun u x0 y h1 h2 hc => ?_) + · by_cases e : u = t.bv + · subst e + rcases hqd with ⟨d, p, hm⟩ | hm + · exact absurd hm (hnq d p) + · exact absurd hm hnr + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + rw [h1] at h2'; cases h2'; exact hr + · by_cases e : u = t.bv + · subst e; simp [hσ1, updK] at h2; subst h2 + exact absurd hc (by intro h; rcases h with h | ⟨_, h⟩ | ⟨_, _, h, -⟩ | ⟨_, _, _, h⟩ <;> cases h) + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + exact J.inv.rd u y h2' hc + have hInv : Inv σ6 := hI1.qchange (qchange_stsAct σ1 t.bv) + -- the yield words + have hY6 : YieldRel Z.T Z.C (L.mem s6) σ6 := by + have h1 := J.yld.frame (m' := L.mem s6) (σ' := σ1) + (fun y hy => hy6.mem_eq (yieldA_aligned hT hy) fun h => h ⟨y, hy, rfl⟩) + (YK.YKS.updTcb (y := x6) hk rfl).ykeep + exact h1.frame (m' := L.mem s6) (σ' := σ6) (fun _ _ => rfl) (YKeep.of_kheap (stsAct_kheap _ _)) + have hYK6 : YKeep σ σ6 := (YK.YKS.updTcb (y := x6) hk rfl).ykeep.trans (YKeep.of_kheap (stsAct_kheap _ _)) + have hsp : Fl4Spec.setThreadState (Z := Unit) t.bv .inactive σ = ({((), σ6)}, False) := + Notif.setThreadState_eq .inactive hk + have hsok : SOK (false, true) σ σ6 := Pres.of_eq (pres_setThreadState _ _ _) hsp + have hNSC : NSC Z.N Z.C σ6 := by + intro n hn y p hy hp + by_cases e : n.bv = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.nsc n hn y p hy hp + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hX6.2.2.1, hX6.1.toN, hNSC, hInv⟩ hY6 (hW.mono fun _ h => Or.inl h) + hsok rfl + -- the scheduler facts + have hidt : σ.idleThread ≠ t.bv := fun e => hti (by rw [hcur, e]) + have hAI : ActIdle σ6 := by + refine ⟨fun u hu => ?_, ?_⟩ + · rw [hcn] at hu; cases hu + · obtain ⟨y, h1, h2, h3, h4⟩ := J.act.2 + exact ⟨y, by rw [e_idle, hkp _ hidt]; exact h1, by rw [e_isc]; exact h2, h3, by rw [e_idle, e_rel]; exact h4⟩ + have hoff : OffOK σ6 := by + intro h0 y n hy ha + rw [e_cons] at h0 ⊢; rw [e_tm]; rw [e_csc] at hy; rw [e_csc, e_isc] at ha + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.off h0 y n hy ha + have hcur6 : σ6.schedulerAction = .chooseNewThread ∨ CurInv σ6 := Or.inl hcn + have hown6 : CurOwnW Z.T Z.C σ6 := by + have hsc6 : ∀ y n, σ6.kheap σ6.curSc = some (.schedContext y n) → σ.kheap σ.curSc = some (.schedContext y n) := by + intro y n hy + rw [e_csc] at hy + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact hy + rcases J.own with ⟨u, hu, xt, hkt, hxs, hown, hnr'⟩ | ⟨hcs, hun⟩ + · left + by_cases hut : u.bv = t.bv + · rw [hut, hk] at hkt; cases hkt + exact ⟨u, hu, x6, by rw [hut]; exact hk6, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr' (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact ⟨u, hu, xt, by rw [hkp _ hut]; exact hkt, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr' (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact Or.inr ⟨by rw [e_csc]; exact hcs, fun y n hy => hun y n (hsc6 y n hy)⟩ + have hcr6 : CurRunM σ6 := Or.inl (by rw [hcn]; intro e; cases e) + have hlk : LK (fun _ => False) σ σ6 := fun p => Or.inr (by + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show LObj _ _ + refine ⟨?_, ?_, ?_⟩ + · intro y ey; cases ey; exact ⟨_, rfl, Or.inl rfl⟩ + · intro y n ey; cases ey + · intro y ey; cases ey + · rw [hkp p hp]; exact OL.refl _) + have hfk : FK σ σ6 := ⟨e_csc, e_idle, e_isc⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s6 q := fun q hq => + J.deadP_sc_stepQ hX' hW hK (fun _ hp => absurd (show q = t from P1.eq_of_bv hp) (fun e => htD (e ▸ hq))) + hlk hfk hAI.1 hY' (hYK6.dk q.bv) (fun h => h) (J.jcov.dead q hq) + exact ⟨Lst', J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).2) (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2) hKd subset_rfl) (J.sc_kern hW hK hfk hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hNSC hInv hcur6 hown6 hoff hAI (BlockedScActive.tkw J.bsa hK fun p y n hp ha => by + by_cases e : p = t.bv + · rw [e, hk6] at hp; cases hp + · rw [hkp _ e] at hp; exact ⟨y, hp, ha, fun _ h => h, id⟩) hcr6 hblk hKd (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).1 J.zD, hW⟩ + +set_option maxHeartbeats 8000000 in +/-- **Null fault handler** at a resumed or choose mid state. -/ +theorem JAtZB.noFaultMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hnull : capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok .Null) + (f : error.Fault) (f' : Fl4Spec.Fault) : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x (by rw [hcur]; exact hk) + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have e1 : c1 = .Null := by rw [hnull] at hc1; simpa using hc1.symm + subst e1 + have hfh : x.tcbFaultHandler = .nullCap := by rw [← hcr.2]; rfl + -- the current thread among the TCBs + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hc.cur] at h; exact h + have hT : ct ∈ Z.T := by rw [hct']; exact hc.curT + have hD : ct ∉ D := fun h => J.notDead hT hk (by have := runnable_code hrn; omega) ct h rfl + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hnr : ct.bv ∉ σ.releaseQueue := (J.midBlkFacts hT hcur hF hsa hk).1 + obtain ⟨bq, br⟩ := J.curBits hT hnq' hnr + obtain ⟨hna, -⟩ := J.words_of_spec hT (fun u hu => by rcases hsa with h | h <;> rw [h] at hu <;> cases hu) + (fun e => hF.ni (by rw [hcur, e])) + obtain ⟨x0, hk0, hTR⟩ := (J.xtop.irelK J.ks J.buf).1.tcb ct hT + rw [hk] at hk0; cases hk0 + have hv : (L.mem s (P1.stA ct)).val = tsCode x.tcbState := by + rw [P1.stA_eq, ← tStA_eq]; exact hTR.st + obtain ⟨hnb, hnrp⟩ := P1.run_word (w := L.mem s (P1.stA ct)) (by rw [hv]; exact runnable_code hrn) + have hB := P1.sched.set_thread_state.inactive_blkR (Es := P1.EH H) s ct J.blk (J.zH.1 ct hT) hnb hnrp bq br hna + have hspec := handleFault_null hk hfh f' + unfold faults.handle_fault + rw [tcb_slot_fault] + apply ok_bind_spec + rw [slot_cap_eq, hnull] + apply ok_bind_spec + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i, _hi⟩ + unfold faults.send_fault_ipc + dsimp only + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (J.stsCurMB hF.ni hsa hT hnr hD hk hcur hrn hnq' hB) ?_ + rintro s6 ⟨Lst', J6, hW⟩ + refine (WP.spec_ok _).2 ⟨_, by rw [hspec]; exact Set.mem_singleton _, + ⟨Z.th, Z.G, J6.xtop.rel.1, J6.xtop.rel.2.1, J6.xtop.rel.2.2.1, J6.xtop.rel.2.2.2.1, J6.xtop.rel.2.2.2.2.1⟩, + JAtN.of J6 (NatFoot.ofT J.xtop hW (fun b hb => J.tfoot_nh hb) + (natK_tcbW hk (Notif.stsAct_kheap _ _) fun h => by simp at h))⟩ + +end + +end + +end Fl4Proofs.P3 + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- **Fault send to a waiting receiver without grant** at a resumed or choose mid state. -/ +theorem JAtZB.sendFaultIJMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrn : Fl4Spec.runnable xt.tcbState = true) {f : Fl4Spec.Fault} + (hft : xt.tcbFault = some f) (hnt : ∀ b, f ≠ .timeout b) (hct : σ.curThread = t.bv) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hCN : CurInv σ ∨ xt.tcbSchedContext = none) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hno : (g = false ∧ gr = false) ∨ + ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep = none) : + endpoint.send_ipc st arI s a blk false badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ).1, + JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) ht hdT htd hkt1 hkd1 hft (faultOK_nt (L := L) s1 t xt hnt) e badge g + hTP hdH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 hdT hA2 (fun p hp => by simp [σ2, hp]) + hkd1 hkd2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1e] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xd b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + have Fr2 : CFr e.bv d'.bv σ σ2 := (Fr1.trans (CFr.um _ _ σ1 um2)).trans (CFr.upd (σ := Endp.setUm σ1 um2) hkd1 + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans (IK.updSelf (σ := Endp.setUm σ1 um2) hkd1 hxsc)) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((SKeep.um σ1 um2).trans (SKeep.updK (σ := Endp.setUm σ1 um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb s := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] + refine WP.spec_bind (Pₘ := fun s3 => ∃ σ3 x3 X3, + Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) rep σ2 = ({((), σ3)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s3) σ3 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s3 σ3 ∧ + σ3.kheap t.bv = some (.tcb xt) ∧ σ3.kheap d'.bv = some (.tcb x3) ∧ RbIn x3.tcbState ∧ + tsCode x3.tcbState ≠ 6 ∧ x3.tcbSchedContext = xd.tcbSchedContext ∧ + (∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue) ∧ + P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) ∧ + P1.WritesIn st s s3 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s b ∨ RcvBufW xd b) ∧ + CFr e.bv d'.bv σ σ3 ∧ IK d'.bv σ σ3 ∧ Ipc.SKeep σ σ3 ∧ σ3.curThread = σ.curThread ∧ + σ3.schedulerAction = σ.schedulerAction ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X3 s3 Q0 R0 ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s3 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s3 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s3 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 ∧ P1.ExcT X3 d' ∧ + (d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s3 (P1.stA d'))) ∧ + (∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d')) ?_ ?_ + · cases hrp : rep with + | none => + rw [hrp] at hrep + have h00 := optRef_none hrep + simp only [h00, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hrA : L.mem s2 (P1.replyA d') = 0#u64 := by + show L.mem s2 (tReplyA d') = 0#u64; rw [hrw]; exact h00 + exact ⟨σ2, x2, _, rfl, hR2, hS2', hkt2, hkd2, Or.inl ⟨_, _, _, hst2'⟩, + by rw [hst2']; simp [tsCode], hxsc, hq2, hW2, footW_trans (footW_of F1) F2, Fr2, IK2, SK2, rfl, rfl, h2, hB2, hX2, hS2'', + hSg2, fun y hy => by simpa using hy, Or.inl (Or.inr rfl), hX2.noref0 hdta hrA⟩ + | some r => + rw [hrp] at hrep hrepR hst2' + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by simp [σ2, σ1, hwe, hwd, hkr] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd, hrp]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_mono (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set σ3a := Notif.updK σ2 w.bv (.reply { xr with replyTcb := none }) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + refine ⟨_, x3, _, e3, hR3, hS3, ?_, ?_, Or.inr rfl, by simp [x3, tsCode], by simp [x3, hxsc], ?_, hW3', + footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3), ?_, ?_, ?_, ?_, ?_, h3, + hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1), + hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr, hS2''.keepT hX2 k3, + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩, + fun y hy => ?_, Or.inr (by rw [e3']; exact P1.not_blocked_inactive), + (hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr).noref hdta (by rw [e3']; exact P1.inactive_ne.1) + (by rw [e3']; exact P1.inactive_ne.2)⟩ + · rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + · rw [stsAct_kheap]; simp + · intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + · exact (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + · have IK3a : IK d'.bv σ σ3a := IK2.trans (IK.updOther (o' := .reply { xr with replyTcb := none }) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σ (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + · exact ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply { xr with replyTcb := none }) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + · rw [e3s]; rfl + · rw [e3s]; rfl + · obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2 + rintro s3 ⟨σ3, x3, X3, hU, hR3, hS3, hkt3, hkd3, hrb3, hns3, hsc3, hq3, hW3, F3, Fr3, IK3, SK3, hct3, hact3, + h3, hB3, hX3, hS3', hSg3, hXc3, hx3, hnr3⟩ + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := fun e0 => by + have h00 := hxt3.fault0.1 (by rw [e0]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hft] at h00; cases h00 + have hks3 : L.ksb s3 = L.ksb s := hW3.1 + -- the caller's facts for its `Inactive` + have hnrT : t.bv ∉ σ.releaseQueue := by + rcases hCN with h | h + · rw [← hct]; exact h.notRel + · exact J.inv.notRel_of_noSc hkt h + obtain ⟨hqb, hrb'⟩ := J.curBits ht hnq hnrT + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := (IpcRel.ofSRN hR3 hS3).2.2.1 + have hna3 : L.actionW s3 ≠ t := by + obtain ⟨Q, R', hcq, -⟩ := hSR3.lists + have h := hSR3.act + rw [hact3] at h + have ha01 : L.actionW s3 = 0#u64 ∨ L.actionW s3 = 1#u64 := by + unfold P1.LawfulStore.actionW; rw [hcq.cur] + rcases hsa with e | e <;> rw [e] at h + · exact Or.inl h + · exact Or.inr h + intro h' + rcases ha01 with e | e <;> rw [e] at h' + · exact (hcq.tcbs t ht).ne0 (by rw [← h']; rfl) + · exact P1.notT_small hcq.tcbs (by simp) (h' ▸ ht) + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + have hstsT := WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 ht hkt3 hqt3 + (by have := runnable_code hrn; omega) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hSR3 ht sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 t _ Q0 R0 h3 hB3 hX3 hS3' hSg3 htH P1.not_blocked_inactive P1.inactive_ne + (Or.inr hnbt3) (hX3.noref (hTP t htH) hnrp3 (fun e0 => hnbt3 (Or.inr (Or.inl e0)))) + ⟨fun c hc' p hp hm => absurd hm (h0.notQ htH hqb hc' hp), fun c hc' hm => absurd hm (h0.notR htH hrb' hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 t sched.TS_INACTIVE h3 htH)) + -- FL4: no reply pushed, the caller `Inactive` + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ sched.set_thread_state st s3 t sched.TS_INACTIVE = ok r.1) + ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [if_pos (bne_iff_ne.2 hty3)] + by_cases hi : (L.mem s (tReplyA d') != 0#u64) = true + · rw [if_pos hi] + have hgg : g = false ∧ gr = false := by + rcases hno with h | hn + · exact h + · exfalso + have hr0 := hn xd hkd _ _ _ hstd + rw [hr0] at hrep + have h00 := optRef_none hrep + rw [h00] at hi + simp at hi + obtain ⟨rfl, rfl⟩ := hgg + simp only [Bool.false_eq_true, ↓reduceIte] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + · rw [if_neg hi] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + rintro ⟨s4, a4⟩ ⟨ha4, hs4⟩ + simp only at ha4 hs4 + subst ha4 + try dsimp only + have hpost := hstsT + rw [hs4] at hpost + obtain ⟨⟨⟨hR4, hS4⟩, hW4⟩, ⟨h4, hB4, hX4, hS4', hSg4, -, k4⟩, fu4⟩ := (WP.spec_ok _).1 hpost + have hkd4 : (ciSt σ3 t.bv xt).kheap d'.bv = some (.tcb x3) := by rw [ciSt_kh (Ne.symm htd')]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, (ciSt σ3 t.bv xt).kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z (by rw [← ciSt_kh (ciSt_ne hz (fun _ h => by cases h))]; exact hz) + have hx4 : d' ∈ {y | y ∈ X3 ∧ y ≠ t} ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := by + rcases hx3 with h | h + · exact Or.inl ⟨h, Ne.symm htd⟩ + · right + have e0 : L.mem s4 (P1.stA d') = L.mem s3 (P1.stA d') := by + rw [P1.stA_eq] + exact k4.fld d' hdH _ (by simp) (by simp) (by simp) (by simp) (fun e1 => absurd e1 (Ne.symm htd)) + rw [e0]; exact h + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := fun r hr => by + rcases k4.rtcb r hr with e0 | ⟨-, e0⟩ + · rw [e0]; exact hnr3 r hr + · rw [e0]; intro h0'; exact hdta.ne0 (by rw [← h0']; rfl) + have hks4 : L.ksb s4 = L.ksb s := hW4.1.trans hks3 + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 hns3 + .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q0 R0 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK (ciSt σ3 t.bv xt) d'.bv (.tcb x5)) d'.bv = ciSt σa t.bv xt := + ciSt_swap htd' hdc3 + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 (ciSt σa t.bv xt) := by + rw [← heq5]; exact hIR0 + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW3.trans1 (hW4.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3).1 hb))))) + have hW5' : P1.WritesIn st s s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr (X := {y | y ∈ X3 ∧ y ≠ t}) (fun y hy => hXc3 y hy.1))⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have Fr5 : CFr e.bv d'.bv σ σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σ (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σ σa := IK5a.trans (IK.sts _ _ _) + have SK5 : Ipc.SKeep σ σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((J.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σa.schedulerAction = σ.schedulerAction := by + rw [hσa, stsAct_ne (by show d'.bv ≠ σ3.curThread; exact hdc3)]; exact hact3 + -- the caller's `Inactive` after the wake (`ciSt_swap`) + have hnqT : ∀ dm p, t.bv ∉ σa.readyQueues dm p := by rw [IK5.2.1]; exact hnq + have hnrT' : t.bv ∉ σa.releaseQueue := by rw [IK5.2.2.1]; exact hnrT + have I5' : Inv (ciSt σa t.bv xt) := ciSt_inv hkta I5 hnqT hnrT' + have B5' : BlockedScActive (ciSt σa t.bv xt) := ciSt_bsa (Fr5.bsa J.bsa) + have hcn : (ciSt σa t.bv xt).schedulerAction = .chooseNewThread := + by + rcases hsa with h | h + · exact ciSt_act (by rw [Fr5.sk.ct, hct]) (by rw [act5, h]) + · exact (ciSt_actN (by rw [act5, h]; intro e; cases e)).trans (act5.trans h) + have hidT : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI5 : ActIdle (ciSt σa t.bv xt) := + ⟨fun u hu => (by rw [hcn] at hu; cases hu), ciSt_idleOK hidT (Fr5.idleOK J.act.2)⟩ + have hrun5 : CurRunM (ciSt σa t.bv xt) := Or.inl (by rw [hcn]; intro h; cases h) + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by have := runnable_code hrn; omega) + obtain ⟨su, umu, Ju, hWu, hFu, -, -, Fu⟩ := J.bufRebaseX hdT hdth hkd hW5' hfoot + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σ umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => Ju.recvReplyB q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have J5 := Ju.wake_postCI hkta hIR hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) I5' B5' hAI5 + (Or.inl hcn) hrun5 hRR htD hcD ⟨d', hdT, rfl⟩ + have N5 := NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj Ju.xtop.fin.1.1.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) ((natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)).trans (natK_ciSt hkta)) + -- the tail + have G := (ciSt_skD (σ := σa) (c := t.bv) (x := xt)) + have hk5 : (ciSt σa t.bv xt).kheap d'.bv = some (.tcb x5) := by + rw [ciSt_kh (Ne.symm htd'), hσa, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ (ciSt σa t.bv xt).releaseQueue := by rw [ciSt_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → (ciSt σa t.bv xt).kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya : σa.kheap p = some (.schedContext y n) := by + rw [← ciSt_kh (ciSt_ne hy (fun _ h => by cases h))]; exact hy + have hy0 := SK5.2.2.2 p y n hya + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J.inv.back _ _ _ hkd hp' + rw [hy0] at hky'; cases hky' + exact J.bsa.tcb p y n d'.bv xd hy0 hyt hkd (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ (ciSt σa t.bv xt).idleThread := by + rw [G.idle, Fr5.sk.idle] + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have htc5 : d'.bv ≠ (ciSt σa t.bv xt).curThread := by rw [G.ct, Fr5.sk.ct]; exact hdc + have hcond : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨hg, hr⟩ + rcases hno with ⟨rfl, rfl⟩ | hn + · rcases hg with h | h <;> cases h + · exact hr (hn xd hkd _ _ _ hstd) + have hspec := sendIpc_faultInactive_eq (don := don) hk hkd hde htd' hstd blk badge.bv g gr hcond e2 hU hkt3 hkd3 + (by rw [hft]; simp) (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ = + Endp.specWakeTailE d'.bv (ciSt σa t.bv xt) := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, + ⟨Z.vs, _, _, _, J7, J.natTrans Fu (Ju.natTrans N5 F7)⟩⟩ + +set_option maxHeartbeats 64000000 in +/-- **Fault send pushing a reply without donation** at a resumed or choose mid state. -/ +theorem JAtZB.sendFaultPJMB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrn : Fl4Spec.runnable xt.tcbState = true) {f : Fl4Spec.Fault} + (hft : xt.tcbFault = some f) (hnt : ∀ b, f ≠ .timeout b) (hct : σ.curThread = t.bv) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hCN : CurInv σ ∨ xt.tcbSchedContext = none) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hg : g = true ∨ gr = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) + (hnd : don = false ∨ ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none) : + endpoint.send_ipc st arI s a blk false badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ).1, + JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) ht hdT htd hkt1 hkd1 hft (faultOK_nt (L := L) s1 t xt hnt) e badge g + hTP hdH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 hdT hA2 (fun p hp => by simp [σ2, hp]) + hkd1 hkd2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1e] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xd b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + have Fr2 : CFr e.bv d'.bv σ σ2 := (Fr1.trans (CFr.um _ _ σ1 um2)).trans (CFr.upd (σ := Endp.setUm σ1 um2) hkd1 + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans (IK.updSelf (σ := Endp.setUm σ1 um2) hkd1 hxsc)) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((SKeep.um σ1 um2).trans (SKeep.updK (σ := Endp.setUm σ1 um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb s := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkd _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by simp [σ2, σ1, hwe, hwd, hkr] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + have Fr3 : CFr e.bv d'.bv σ σ3 := (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σ σ3 := by + have IK3a : IK d'.bv σ σ3a := IK2.trans (IK.updOther (o' := .reply xr3) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σ (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + have SK3 : Ipc.SKeep σ σ3 := + ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans + (SKeep.stsAct _ _) + have hct3 : σ3.curThread = σ.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d' := + hX3.noref hdta (by rw [e3']; exact P1.inactive_ne.1) (by rw [e3']; exact P1.inactive_ne.2) + have F3 := footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := fun e0 => by + have h00 := hxt3.fault0.1 (by rw [e0]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hft] at h00; cases h00 + have hks3 : L.ksb s3 = L.ksb s := hW3'.1 + -- the caller's and the receiver's words at `s3` + have hnrT : t.bv ∉ σ.releaseQueue := by + rcases hCN with h | h + · rw [← hct]; exact h.notRel + · exact J.inv.notRel_of_noSc hkt h + have hscAm : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true := by + rcases hCN with h | h + · exact J.blockScA hct h hti hkt + · exact J.blockScA_noSc hkt h + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have ha01 : L.actionW s3 = 0#u64 ∨ L.actionW s3 = 1#u64 := by + have h := hSR3.act + rw [hact3] at h + unfold P1.LawfulStore.actionW; rw [hcq3.cur] + rcases hsa with e | e <;> rw [e] at h + · exact Or.inl h + · exact Or.inr h + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu h' => by + rcases ha01 with e | e <;> rw [e] at h' + · exact (hcq3.tcbs u hu).ne0 (by rw [← h']; rfl) + · exact P1.notT_small hcq3.tcbs (by simp) (h' ▸ hu) + have hcw3 : L.curW s3 = t := by + have h : (Sched.nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σ.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0 => by + obtain ⟨Q', R'', hcq0, hs0⟩ := J.xtop.fin.1.1.1.lists + rcases hCN with hCI | hsc0 + · obtain ⟨e1, hA⟩ := J.curScAcB hCI hti + have hcw : L.curW s = t := by + apply P1.eq_of_bv + rw [← hct, hs0.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq0.cur] + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + have h1 : L.mem s3 (P1.scA t) = L.mem s (P1.scA t) := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs0.tcb t ht + obtain ⟨x'', hk'', -, -, hscw', -⟩ := hs3.tcb t ht + rw [hkt] at hk'; cases hk' + rw [hkt3] at hk''; cases hk'' + have e2 := hscw.symm.trans hcs + have hv0 : L.mem s (P1.scA t) ≠ 0#u64 := fun e => by rw [e, Notif.optRef_zero] at e2; cases e2 + exact optRef_some_eq ((hscw'.symm.trans hcs).trans (e2.symm.trans (Notif.optRef_ne hv0))) + rw [h1, ← hcw, e1]; exact hA + · obtain ⟨x'', hk'', -, -, hscw', -⟩ := hs3.tcb t ht + rw [hkt3] at hk''; cases hk'' + exact absurd (Notif.optRef_none (hscw'.symm.trans hsc0)) h0 + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the receiver's SC: no donation + have hndF : don = true → x3.tcbSchedContext = none → False := fun hdon hx0 => by + rcases hnd with h | h + · rw [h] at hdon; cases hdon + · exact h xd hkd (by rw [← hsc3]; exact hx0) + obtain ⟨xd3, hkd3', hdsc⟩ := hSM d' hdT + rw [hkd3] at hkd3'; cases hkd3' + have hnd1 : don = false ∨ L.mem s3 (P1.scA d') ≠ 0#u64 := by + cases don + · exact Or.inl rfl + · right; intro h0; exact hndF rfl (by rw [hdsc, h0, optRef_zero]) + -- the push on `IRel`, at P1 + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hns3 : tsCode xt.tcbState ≠ 6 := by have := runnable_code hrn; omega + have hPush : ipc.reply_push st arI s3 a t d' w don ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv don σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1 ⦄ := by + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := don) (P := fun s' σ' => σ' = pushSt σ3 t.bv w.bv xr3 xt ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hdon _ _ _ _ _ _ _ hx0 => (hndF hdon hx0).elim)) + (P1.ipc.reply_push.ipc hAT s3 a t d' w don Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hqbD hrbD (hna3 d' hdT) (hiw3 d' hdT hid) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a t d' w don h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hcA hnd1)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, hb2, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hb2, hf, hsb⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv don σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1) ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [if_pos (bne_iff_ne.2 hty3)] + rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, ⟨⟨Q4, R4, h4⟩, hB4, hX4, hS4', hSg4⟩, hst4, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 h4 hB4 hX4 hS4' hSg4 hst4 fu4 hsb4 ⊢ + subst ha4 + try dsimp only + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3 (SzKeepC.of Fr3.szKeep) hb)) + have hks4 : L.ksb s4 = L.ksb s := hW4'.1 + set σ4 := pushSt σ3 t.bv w.bv xr3 xt with hσ4 + have hkd4 : σ4.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, σ4.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z ((ps_oth hkt3 hkr3 hwt (fun _ h => by cases h) (fun _ h => by cases h)).1 hz) + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hst4d : L.mem s4 (P1.stA d') = sched.TS_INACTIVE := by rw [hst4 d' hdH (Ne.symm htd)]; exact e3' + have hx4 : d' ∈ (∅ : Set U64) ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := Or.inr (by rw [hst4d]; exact P1.not_blocked_inactive) + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := + hX4.noref hdta (by rw [hst4d]; exact P1.inactive_ne.1) (by rw [hst4d]; exact P1.inactive_ne.2) + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 + (by simp [x3, tsCode]) .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q4 R4 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK σ4 d'.bv (.tcb x5)) d'.bv = pushSt σa t.bv w.bv xr3 xt := + ps_swap htd' hwd hdc3 + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 (pushSt σa t.bv w.bv xr3 xt) := by + rw [← heq5]; exact hIR0 + have hW5' : P1.WritesIn st s s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q4, R4, h5.congrX P1.empty_congr⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have hrb3 : RbIn x3.tcbState := Or.inr rfl + have Fr5 : CFr e.bv d'.bv σ σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σ (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σ σa := IK5a.trans (IK.sts _ _ _) + have SK5 : Ipc.SKeep σ σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((J.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σa.schedulerAction = σ.schedulerAction := by + rw [hσa, stsAct_ne (by show d'.bv ≠ σ3.curThread; exact hdc3)]; exact hact3 + -- the push after the wake (`ps_swap`) + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hkwa : σa.kheap w.bv = some (.reply xr3) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, hwd, hkr3] + have hnqT : ∀ dm p, t.bv ∉ σa.readyQueues dm p := by rw [IK5.2.1]; exact hnq + have hnrT' : t.bv ∉ σa.releaseQueue := by rw [IK5.2.2.1]; exact hnrT + set σ' := pushSt σa t.bv w.bv xr3 xt with hσ' + have I5' : Inv σ' := ps_inv hkta hkwa hwt I5 hnqT hnrT' + -- an SC bound to the caller is the current SC, active + have hscA : ∀ p y n, σa.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := fun p y n hp hyt => hscAm p y n (SK5.2.2.2 _ _ _ hp) hyt + have B5' : BlockedScActive σ' := ps_bsa hkta hkwa hwt hscA (Fr5.bsa J.bsa) + have hcn : σ'.schedulerAction = .chooseNewThread := + by + rcases hsa with h | h + · exact ps_act (by rw [Fr5.sk.ct, hct]) (by rw [act5, h]) + · exact (ps_actN (by rw [act5, h]; intro e; cases e)).trans (act5.trans h) + have hidT : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI5 : ActIdle σ' := + ⟨fun u hu => (by rw [hcn] at hu; cases hu), ps_idleOK hkta hkwa hwt hidT (Fr5.idleOK J.act.2)⟩ + have hrun5 : CurRunM σ' := Or.inl (by rw [hcn]; intro h; cases h) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by have := runnable_code hrn; omega) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := hR.reply w hwR + rw [hkr] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hkq : ∀ q ∈ D, σ'.kheap q.bv = σa.kheap q.bv := fun q hq => ps_ko (hcD q hq) (hwD q hq) + obtain ⟨su, umu, Ju, hWu, hFu, -, -, Fu⟩ := J.bufRebaseX hdT hdth hkd hW5' hfoot + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σ umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => Ju.recvReplyB q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σa.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hwt h1 h2 + have J5 := Ju.wake_postGR hIR hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) ps_skD + (ps_ksub hkta hkwa hwt) (ps_szKeep hkta hkwa hwt) (ps_ykeep hkta hkwa hwt) (ps_mcpKeep hkta hkwa hwt) + (ps_bufKeep hkta hkwa hwt) (fun d _ h => ps_recvB hkta hkwa hwt h) (ps_ksz hkta hkwa hwt) + (ps_kback hkta hkwa hwt) (ps_ipcFr hkta hkwa hwt) (ps_ck hkta hkwa hwt) (fun d _ h => ps_free hkta hkwa hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I5' B5' hAI5 (Or.inl hcn) hrun5 (ps_own hkta hkwa hwt ((DWFr.ofCFr Fr5).own ⟨d', hdT, rfl⟩ J.own)) + (ps_off hkta hkwa hwt ((DWFr.ofCFr Fr5).off J.off)) (ps_nsc hkta hkwa hwt ((DWFr.ofCFr Fr5).nsc J.nsc)) + (ps_isorted hkta hkwa hwt ((DWFr.ofCFr Fr5).isorted J.isorted)) hRR htD + -- the tail + have hk5 : σ'.kheap d'.bv = some (.tcb x5) := by + rw [ps_ko (Ne.symm htd') (Ne.symm hwd), hσa, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ σ'.releaseQueue := by rw [ps_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → σ'.kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya : σa.kheap p = some (.schedContext y n) := + (ho (fun _ h => by cases h) (fun _ h => by cases h)).1 hy + have hy0 := SK5.2.2.2 p y n hya + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J.inv.back _ _ _ hkd hp' + rw [hy0] at hky'; cases hky' + exact J.bsa.tcb p y n d'.bv xd hy0 hyt hkd (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ σ'.idleThread := by + rw [(ps_skD (σ := σa) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).idle, Fr5.sk.idle] + exact hid + have htc5 : d'.bv ≠ σ'.curThread := by + rw [(ps_skD (σ := σa) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).ct, Fr5.sk.ct]; exact hdc + have hspec := sendIpc_faultPush_eq (don := don) hk hkd hde hstd blk badge.bv g gr hg e2 hU hkt3 hkd3 hpush hkd4 (by rw [hft]; simp) + (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ = + Endp.specWakeTailE d'.bv σ' := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have N5 := NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj Ju.xtop.fin.1.1.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) + ((natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)).trans (natK_pushSt hkwa hkta (fun e0 => hwt e0.symm))) + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, + ⟨Z.vs, _, _, _, J7, J.natTrans Fu (Ju.natTrans N5 F7)⟩⟩ + +set_option maxHeartbeats 64000000 in +/-- **Donating fault send** at a resumed or choose mid state with `CurInv`. -/ +theorem JAtZB.sendFaultPDJMB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrn : Fl4Spec.runnable xt.tcbState = true) {f : Fl4Spec.Fault} + (hft : xt.tcbFault = some f) (hnt : ∀ b, f ≠ .timeout b) (hct : σ.curThread = t.bv) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hCI : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr : Bool) + (hg : g = true ∨ gr = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) + (hxdn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext = none) : + endpoint.send_ipc st arI s a blk false badge g gr true t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr true t.bv e.bv σ).1, + JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) ht hdT htd hkt1 hkd1 hft (faultOK_nt (L := L) s1 t xt hnt) e badge g + hTP hdH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 hdT hA2 (fun p hp => by simp [σ2, hp]) + hkd1 hkd2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1e] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xd b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + have Fr2 : CFr e.bv d'.bv σ σ2 := (Fr1.trans (CFr.um _ _ σ1 um2)).trans (CFr.upd (σ := Endp.setUm σ1 um2) hkd1 + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans (IK.updSelf (σ := Endp.setUm σ1 um2) hkd1 hxsc)) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((SKeep.um σ1 um2).trans (SKeep.updK (σ := Endp.setUm σ1 um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb s := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkd _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by simp [σ2, σ1, hwe, hwd, hkr] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + have Fr3 : CFr e.bv d'.bv σ σ3 := (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σ σ3 := by + have IK3a : IK d'.bv σ σ3a := IK2.trans (IK.updOther (o' := .reply xr3) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σ (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + have SK3 : Ipc.SKeep σ σ3 := + ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans + (SKeep.stsAct _ _) + have hct3 : σ3.curThread = σ.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d' := + hX3.noref hdta (by rw [e3']; exact P1.inactive_ne.1) (by rw [e3']; exact P1.inactive_ne.2) + have F3 := footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := fun e0 => by + have h00 := hxt3.fault0.1 (by rw [e0]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hft] at h00; cases h00 + have hks3 : L.ksb s3 = L.ksb s := hW3'.1 + -- the caller's and the receiver's words at `s3` + have hCN : CurInv σ ∨ xt.tcbSchedContext = none := Or.inl hCI + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have ha01 : L.actionW s3 = 0#u64 ∨ L.actionW s3 = 1#u64 := by + have h := hSR3.act + rw [hact3] at h + unfold P1.LawfulStore.actionW; rw [hcq3.cur] + rcases hsa with e | e <;> rw [e] at h + · exact Or.inl h + · exact Or.inr h + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu h' => by + rcases ha01 with e | e <;> rw [e] at h' + · exact (hcq3.tcbs u hu).ne0 (by rw [← h']; rfl) + · exact P1.notT_small hcq3.tcbs (by simp) (h' ▸ hu) + have hcw3 : L.curW s3 = t := by + have h : (Sched.nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σ.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0 => by + obtain ⟨Q', R'', hcq0, hs0⟩ := J.xtop.fin.1.1.1.lists + rcases hCN with hCI | hsc0 + · obtain ⟨e1, hA⟩ := J.curScAcB hCI hti + have hcw : L.curW s = t := by + apply P1.eq_of_bv + rw [← hct, hs0.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq0.cur] + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + have h1 : L.mem s3 (P1.scA t) = L.mem s (P1.scA t) := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs0.tcb t ht + obtain ⟨x'', hk'', -, -, hscw', -⟩ := hs3.tcb t ht + rw [hkt] at hk'; cases hk' + rw [hkt3] at hk''; cases hk'' + have e2 := hscw.symm.trans hcs + have hv0 : L.mem s (P1.scA t) ≠ 0#u64 := fun e => by rw [e, Notif.optRef_zero] at e2; cases e2 + exact optRef_some_eq ((hscw'.symm.trans hcs).trans (e2.symm.trans (Notif.optRef_ne hv0))) + rw [h1, ← hcw, e1]; exact hA + · obtain ⟨x'', hk'', -, -, hscw', -⟩ := hs3.tcb t ht + rw [hkt3] at hk''; cases hk'' + exact absurd (Notif.optRef_none (hscw'.symm.trans hsc0)) h0 + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the caller's SC, donated + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + obtain ⟨y, n, hky0⟩ := hCI.sc + have hya : Fl4Spec.scActive y = true := hCI.active (J.curSc_ne_idle hCI hti) y n hky0 + obtain ⟨sc, hsC, esc⟩ := J.obj.sc _ y n hky0 + rw [← esc] at hky0 hcs + obtain ⟨y1, n1, hky1, hyt⟩ := J.inv.back _ _ _ hkt hcs + rw [hky0] at hky1; cases hky1 + have hrelσ : Fl4Spec.scActive y = true → released σ y := fun ha => + J.inv.rd t.bv xt hkt (Or.inl hrn) xt sc.bv y n hkt hcs hky0 ha hnrT + have hse : sc.bv ≠ e.bv := kheap_ne hky0 hk (fun _ h1 h2 => by subst h1; cases h2) + have hsd : sc.bv ≠ d'.bv := kheap_ne hky0 hkd (fun _ h1 h2 => by subst h1; cases h2) + have hsw : sc.bv ≠ w.bv := kheap_ne hky0 hkr (fun _ h1 h2 => by subst h1; cases h2) + have hst : sc.bv ≠ t.bv := kheap_ne hky0 hkt (fun _ h1 h2 => by subst h1; cases h2) + have hky2 : σ2.kheap sc.bv = some (.schedContext y n) := by simp [σ2, σ1, hsd, hse, hky0] + have hky3 : σ3.kheap sc.bv = some (.schedContext y n) := by rw [stsAct_kheap]; simp [σ3a, hsd, hsw, hky2] + have hsw3 : L.mem s3 (P1.scA t) = sc := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs3.tcb t ht + rw [hkt3] at hk'; cases hk' + exact optRef_some_eq (hscw.symm.trans hcs) + have hxdn3 : x3.tcbSchedContext = none := by rw [hsc3]; exact hxdn xd hkd + -- the J state after the block + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by have := runnable_code hrn; omega) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := hR.reply w hwR + rw [hkr] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hRR : ∀ q' ∈ Z.R, ∀ xr, σ.kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem s (rNextA q') = 0#u64 ∧ L.mem s (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => J.recvReplyB q' hq'' xr d'.bv xd hx' hxt' hkd + (by rw [hstd]; rfl) + have hrib3 : RelIfBound σ3 d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK3 + have I3 : Inv σ3 := ((J.inv.toBut d'.bv).ik hnqD IK3).1.inv fun _ _ _ => hrib3 + have hscA3 : ∀ p y n, σ3.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + have hp0 : σ.kheap p = some (.schedContext y n) := SK3.2.2.2 _ _ _ hp + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp0 + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp0).2⟩ + have hS := J.xtop.fin.1.1.1 + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp0 hyt + rw [hkt] at hku; cases hku + have e1 : σ.curSc = q.bv := by rw [← esc]; exact Option.some.inj (hcs.symm.trans hxs) + rw [← e1] at hp0 + exact hCI.active (J.curSc_ne_idle hCI hti) y n hp0 + set xtP : Fl4Spec.Tcb := { xt with tcbState := .blockedOnReply w.bv } with hxtP + set xwP : Fl4Spec.Reply := { xr3 with replyTcb := some t.bv } with hxwP + set σP := pushSt σ3 t.bv w.bv xr3 xt with hσP + set σD := pdSt σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n with hσD + have hktP : σP.kheap t.bv = some (.tcb xtP) := ps_kt + have hkwP : σP.kheap w.bv = some (.reply xwP) := ps_kw hwt + have hkdP : σP.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hkyP : σP.kheap sc.bv = some (.schedContext y n) := by rw [ps_ko hst hsw]; exact hky3 + have hcnP : σP.schedulerAction = .chooseNewThread := by + rcases hsa with h | h + · exact ps_act (by rw [hct3, hct]) (by rw [hact3, h]) + · exact (ps_actN (by rw [hact3, h]; intro e; cases e)).trans (hact3.trans h) + have hnsw : ∀ u, σP.schedulerAction ≠ .switchThread u := fun u e0 => by rw [hcnP] at e0; cases e0 + have hctP : σP.curThread = t.bv := + (ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).ct.trans (hct3.trans hct) + have hcscP : σP.curSc = sc.bv := + (ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).csc.trans (Fr3.sk.csc.trans esc.symm) + have hnqtP : NotQueued σP t.bv := ⟨by rw [ps_rq]; exact hnqT3.1, by rw [ps_rel]; exact hnqT3.2⟩ + have hnqdP : NotQueued σP d'.bv := ⟨by rw [ps_rq]; exact hnqD3.1, by rw [ps_rel]; exact hnqD3.2⟩ + have hhP := J.stackHead hsC hky0 (hRR w hwR xr hkr hb').1 + have hhP' : ∀ h l, y.scReplies = h :: l → h ≠ sc.bv ∧ h ≠ w.bv ∧ ∃ x, σP.kheap h = some (.reply x) := by + intro h l hr + obtain ⟨h1, h2, x, hx⟩ := hhP h l hr + refine ⟨h1, h2, x, ?_⟩ + have ht' : h ≠ t.bv := fun e0 => by rw [e0, hkt] at hx; cases hx + have hd' : h ≠ d'.bv := fun e0 => by rw [e0, hkd] at hx; cases hx + have he' : h ≠ e.bv := fun e0 => by rw [e0, hk] at hx; cases hx + rw [ps_ko ht' h2, stsAct_kheap] + simp [σ3a, hd', h2, σ2, σ1, he', hx] + have hpdE := pd_eq hktP hkdP hkyP hkwP (Ne.symm hsw) hhP' hyt htd' hst hsd hwt hwd + hctP.symm hnqtP hnsw + have hpushE : Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σD)}, False) := by + rw [replyPush_don_eq hkt3 hkd3 hkr3 hwt hcs hxdn3]; exact hpdE + have hns3 : tsCode xt.tcbState ≠ 6 := by have := runnable_code hrn; omega + -- the push: link and block (`J` again), then the donation at `J` + have hPush : ipc.reply_push st arI s3 a t d' w true ⦃ r => r.2 = a ∧ ∃ σ', + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σ')}, False) ∧ + (σ' = σD → ∃ (Lst' : U64 → List U64), JAtZB (L := L) arI AR Z H Cs Ac Lst' D r.1 a σ' ∧ + NatFoot (L := L) Z s σ r.1 σ') ⦄ := by + refine reply_push_iC (don := true) (P := fun s' σ' => σ' = σD → ∃ (Lst' : U64 → List U64), + JAtZB (L := L) arI AR Z H Cs Ac Lst' D s' a σ' ∧ NatFoot (L := L) Z s σ s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) (fun s' _ _ e0 => ?_) ?_ + · exfalso + have h1 : σD.kheap t.bv = some (.tcb xtP) := by rw [← e0]; exact hktP + have h2 : σD.kheap t.bv = some (.tcb { xtP with tcbSchedContext := none }) := + @dn_kt (bdSt σP sc.bv w.bv y n xwP) t.bv d'.bv sc.bv xtP x3 { y with scReplies := w.bv :: y.scReplies } n + htd' hst + rw [h2] at h1 + have h3 : ({ xtP with tcbSchedContext := none } : Fl4Spec.Tcb) = xtP := by + injection h1 with h1; injection h1 + have h4 : (none : Option Fl4Spec.ObjRef) = some sc.bv := (congrArg Fl4Spec.Tcb.tcbSchedContext h3).trans hcs + cases h4 + · intro _ s4 hsb hR4 hS4 _ _ _ _ + rw [hsw3] + have hsb' : sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok s4 := hsb + -- FL4's words and P1 at the block + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb', WP.spec_ok] at h; exact h + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3 (SzKeepC.of Fr3.szKeep) hb)) + have hb4 := P1.sched.set_blocked_on_reply.ipc s3 t w Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 hcA + rw [hsb', WP.spec_ok] at hb4 + obtain ⟨h4, hB4, hX4, hS4', hSg4, -, -, -, -, -, -⟩ := hb4 + have hO3 := hX3.1 + obtain ⟨hrΩ3, -⟩ := P1.objs_omega hO3 hSg3.sub + have f1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) + s3 (L.wr s3 (P1.rTcbA w) t) := + P1.foot_wr (h3.omega _ (hrΩ3 w hwH).1).1 (P1.kw_sg (hO3.tcbW hwH)) + have q1 := (P1.omega_wr (v := t) h3 (hrΩ3 w hwH).1).1 + have f4 := P1.sched.set_blocked_on_reply.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) (Es := P1.EH H) _ t w q1 htH + rw [hsb', WP.spec_ok] at f4 + have hfoot4 : P1.WritesIn st s s4 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s b ∨ RcvBufW xd b) := + footW_trans (footW_trans F3 (footW_of f1)) (footW_of f4) + have hIR4 := IpcRel.ofSRN hR4 hS4 + obtain ⟨su, umu, Ju, hWu, hFu, -, -, Fu⟩ := J.bufRebaseX hdT hdth hkd hW4' hfoot4 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRRu : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σ umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => Ju.recvReplyB q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF4 : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hblk4 : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s4 := ⟨⟨Q0, R0, h4⟩, hB4, hX4, hS4', hSg4⟩ + have I4 : Inv σP := ps_inv hkt3 hkr3 hwt I3 hnqT3.1 hnqT3.2 + have B4 : BlockedScActive σP := ps_bsa hkt3 hkr3 hwt hscA3 (Fr3.bsa J.bsa) + have hidT3 : σ3.idleThread ≠ t.bv := by rw [Fr3.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI4 : ActIdle σP := + ⟨fun u hu => (by rw [hcnP] at hu; cases hu), ps_idleOK hkt3 hkr3 hwt hidT3 (Fr3.idleOK J.act.2)⟩ + have hkq : ∀ q ∈ D, σP.kheap q.bv = σ3.kheap q.bv := fun q hq => ps_ko (hcD q hq) (hwD q hq) + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σP.kheap q = some o ↔ σ3.kheap q = some o) := fun h1 h2 => ps_oth hkt3 hkr3 hwt h1 h2 + have J4 := Ju.wake_postGR hIR4 hF4 hWu hblk4 hFu (DWFr.ofUm (DWFr.ofCFr Fr3) umu) ps_skD + (ps_ksub hkt3 hkr3 hwt) (ps_szKeep hkt3 hkr3 hwt) (ps_ykeep hkt3 hkr3 hwt) (ps_mcpKeep hkt3 hkr3 hwt) + (ps_bufKeep hkt3 hkr3 hwt) (fun d _ h => ps_recvB hkt3 hkr3 hwt h) (ps_ksz hkt3 hkr3 hwt) + (ps_kback hkt3 hkr3 hwt) (ps_ipcFr hkt3 hkr3 hwt) (ps_ck hkt3 hkr3 hwt) (fun d _ h => ps_free hkt3 hkr3 hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I4 B4 hAI4 (Or.inl hcnP) (Or.inl (by rw [hcnP]; intro h; cases h)) + (ps_own hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).own ⟨d', hdT, rfl⟩ J.own)) + (ps_off hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).off J.off)) (ps_nsc hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).nsc J.nsc)) + (ps_isorted hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).isorted J.isorted)) hRRu htD + have N4 := NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj Ju.xtop.fin.1.1.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) + ((natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr3) umu)).trans (natK_pushSt hkr3 hkt3 (fun e0 => hwt e0.symm))) + -- the reply's links at the block + obtain ⟨hdet, hn0, hp0⟩ := hRRu w hwR xr hkr hb' + obtain ⟨-, -, -, -, k4⟩ := Ju.cnW_keeps hWu + refine WP.spec_mono (J4.pushDonJ hAT ht hdT hwR hsC htd hktP hkdP hkwP hkyP rfl hcs hyt hxdn3 rfl rfl + hctP hcscP hcnP hnqtP hnqdP hya hdet (by rw [(k4 w hwR).2]; exact hn0) (by rw [(k4 w hwR).1]; exact hp0) + htD) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, hsp5, Lst5, J5, hW5r, hK5⟩ + simp only at ha5 + subst a5 + have N5 := NatFoot.ofW J4.xtop hW5r (fun b hb => RmW.oobj J4.xtop.fin.1.1.1 hb) (fun b hb => J4.rmW_nv hb) + (fun b hb => J4.rmW_nh hb) (natK_tk hK5) + exact ⟨rfl, _, hsp5, fun _ => ⟨Lst5, J5, J.natTrans Fu (Ju.natTrans N4 N5)⟩⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ ∃ σ', + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σ')}, False) ∧ + (σ' = σD → ∃ (Lst' : U64 → List U64), JAtZB (L := L) arI AR Z H Cs Ac Lst' D r.1 a σ' ∧ + NatFoot (L := L) Z s σ r.1 σ')) + ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [if_pos (bne_iff_ne.2 hty3)] + rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s5, a5⟩ ⟨ha5, σ5', hpush, hPσ⟩ + simp only at ha5 hpush hPσ ⊢ + subst a5 + try dsimp only + have e5 : σ5' = σD := by + have h := hpush.symm.trans hpushE + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + obtain ⟨Lst5, J5, F5⟩ := hPσ e5 + subst e5 + -- the receiver runs (`Inactive` with the donated SC) + set x4 : Fl4Spec.Tcb := { x3 with tcbSchedContext := some sc.bv } with hx4 + have hk5 : σD.kheap d'.bv = some (.tcb x4) := @pd_kd σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n + have hst5 : L.mem s5 (P1.stA d') = sched.TS_INACTIVE := by + have h := ((J5.xtop.irelK J5.ks J5.buf).1.toN.at_tcb hdT hk5).st + rw [stA_tStA, P1.ts_inactive] + exact UScalar.eq_of_val_eq (by rw [h]; rfl) + have htmD : σD.curTime = σ.curTime := (@pd_tm σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans + ((ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).tm.trans SK3.2.1) + have hkpD : σD.kheap sc.bv = some (.schedContext { { y with scReplies := w.bv :: y.scReplies } with + scTcb := some d'.bv } n) := + @dn_kp (bdSt σP sc.bv w.bv y n xwP) t.bv d'.bv sc.bv xtP x3 { y with scReplies := w.bv :: y.scReplies } n hsd + have hrelD : RelIfBound σD d'.bv := by + intro x' scp y' n' hx' hsc' hky' ha' _ + rw [hk5] at hx'; cases hx' + simp only [x4, Option.some.injEq] at hsc' + subst hsc' + rw [hkpD] at hky' + cases hky' + have := hrelσ hya + unfold released at this ⊢ + rw [htmD]; exact this + refine WP.spec_bind (J5.stsIJ hdT (fun h => htD d' h rfl) hk5 rfl .running (by simp [tsCode]) sched.TS_RUNNING + (by unfold sched.TS_RUNNING; rfl) (fun _ => hrelD) + (P1.sched.set_thread_state.running_blkI (Es := P1.EH H) s5 d' J5.blk hdH hst5)) ?_ + rintro s6 ⟨Lst6, J6, hW6⟩ + have N6 : NatFoot (L := L) Z s5 σD s6 (stsAct (updK σD d'.bv (.tcb { x4 with tcbState := .running })) d'.bv) := + NatFoot.ofT J5.xtop hW6 (fun b hb => J5.tfoot_nh hb) + (natK_tcbW (y := { x4 with tcbState := .running }) hk5 (Notif.stsAct_kheap _ _) fun h => by simp at h) + set x6 : Fl4Spec.Tcb := { x4 with tcbState := .running } with hx6 + have hdcD : d'.bv ≠ σD.curThread := by + rw [show σD.curThread = t.bv from (@pd_ct σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans hctP] + exact Ne.symm htd' + have e6 : stsAct (updK σD d'.bv (.tcb x6)) d'.bv = updK σD d'.bv (.tcb x6) := stsAct_ne hdcD + have hk6 : (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).kheap d'.bv = some (.tcb x6) := by + rw [e6]; simp [updK] + have hnr6 : d'.bv ∉ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).releaseQueue := by + rw [e6] + show d'.bv ∉ σD.releaseQueue + rw [show σD.releaseQueue = σ.releaseQueue from (@pd_rel σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans + ((@ps_rel σ3 t.bv w.bv xr3 xt).trans SK3.1)] + exact hnqD.2 + have hact6 : ∀ p y' n', x6.tcbSchedContext = some p → + (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).kheap p = some (.schedContext y' n') → Fl4Spec.scActive y' = true := by + intro p y' n' hp hy' + simp only [x6, x4, Option.some.injEq] at hp + subst hp + rw [e6] at hy' + simp only [updK_kheap, Function.update_of_ne hsd] at hy' + rw [hkpD] at hy' + cases hy' + exact hya + have hti6 : d'.bv ≠ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).idleThread := by + rw [e6] + show d'.bv ≠ σD.idleThread + rw [show σD.idleThread = σ.idleThread from (@pd_idle σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n).trans + ((ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).idle.trans Fr3.sk.idle)] + exact hid + have htc6 : d'.bv ≠ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).curThread := by + rw [e6]; exact hdcD + have hspec := sendIpc_faultPush_eq (don := true) hk hkd hde hstd blk badge.bv g gr hg e2 hU hkt3 hkd3 hpushE hk5 (by rw [hft]; simp) + (t := t.bv) + refine WP.spec_mono (J6.sendTailJ hdT hk6 rfl hnr6 hact6 hti6 htc6) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + exact ⟨((), σ7), by rw [hspec, h7]; exact Set.mem_singleton _, + ⟨Z.vs, _, _, _, J7, J.natTrans F5 (J5.natTrans N6 F7)⟩⟩ + +end + +end + +end Fl4Proofs.P3 + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- **Idle or sending handler endpoint** at a resumed or choose mid state. -/ +theorem JAtZB.faultBlockMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ptr bdg : U64} {sd rv g gr : Bool} + (hep : capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok (.Endpoint ptr bdg sd rv g gr)) + (hcb : ∀ xe, σ.kheap ptr.bv = some (.endpoint xe) → Endp.CanBlock false xe) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x (by rw [hcur]; exact hk) + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hP0.e.tcb + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hcq.cur] at h; exact h + have ht : ct ∈ Z.T := by rw [hct']; exact hcq.curT + have htH : ct ∈ P1.Heap.tcbs H := J.zH.1 ct ht + have htD : ct ∉ D := fun h => J.notDead ht hk (by have := runnable_code hrn; omega) ct h rfl + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + obtain ⟨hnr, hscAm, hcbm⟩ := J.midBlkFacts ht hcur hF hsa hk + have hti : σ.curThread ≠ σ.idleThread := hF.ni + have hidt : σ.idleThread ≠ ct.bv := fun e => hti (by rw [hcur, e]) + -- the handler cap, its endpoint + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have e1 : c1 = .Endpoint ptr bdg sd rv g gr := by rw [hep] at hc1; simpa using hc1.symm + subst e1 + have habs := hcr.2 + simp only [absCap] at habs + have hfh : x.tcbFaultHandler = .endpointCap ptr.bv bdg.bv (rightsOf sd rv g gr) := habs.symm + have hAS : AbsSlot Z.th (ct.bv, Fl4Spec.tcbCnodeIndex 3) := ⟨ct, rfl, Or.inr ⟨htcb, 3, by decide, rfl⟩⟩ + have hpE : ptr ∈ Z.E := J.slotE hAS hep + obtain ⟨xe, hke, -⟩ := hI.1.ep ptr hpE + have hcbe : Endp.CanBlock false xe := hcb xe hke + have hpt : ptr.bv ≠ ct.bv := fun e => by rw [e, hk] at hke; cases hke + have hHe : H ptr = some .ep := J.zH.2.1 ptr hpE + have hfit := J.kern.heap.fits ptr _ hHe + simp only [P1.HKind.bits] at hfit + have hcov := J.cover_cap (P1.WritesIn.refl s _) hAS.isSlot hep (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_ep ptr bdg sd rv g gr hfit) fun k hk' => by + rw [hHe] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + have hcbP := hcbm + have hdon := J.faultDonWord ht hk + refine handleFault_ep_wp hep ?_ + unfold faults.send_fault_ipc + dsimp only + -- `set_fault` + set x2 : Fl4Spec.Tcb := { x with tcbFault := some f' } with hx2 + set σf := updK σ ct.bv (.tcb x2) with hσf + refine WP.spec_bind (WP.spec_and (set_fault_G_top hP0 hI ht hk hf) + (WP.spec_and (P1.faults.set_fault.blk (U := ∅) s ct f (t0 := ct) J.blk htH + (fun _ h => by simp at h) hcbP) + (P1.faults.set_fault.kw (Cs := Cs) (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s ct f hTH htH))) ?_ + rintro s7 ⟨⟨⟨-, hI7⟩, hw7⟩, ⟨hblk7, -, hcb7⟩, hfoot7⟩ + have hkf : σf.kheap ct.bv = some (.tcb x2) := by simp [hσf, updK] + have hkfp : ∀ q, q ≠ ct.bv → σf.kheap q = σ.kheap q := by + intro q hq'; rw [hσf]; simp only [updK, Function.update_of_ne hq'] + have hkef : σf.kheap ptr.bv = some (.endpoint xe) := by rw [hkfp _ hpt]; exact hke + -- the FL4 step of `set_fault`: the seven words + have hC2 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ s7 σf := by + refine ⟨?_, SzKeepC.of (SzKeep.of_tcb (p := ct.bv) (x' := x2) (fun q hq' => by simp [hσf, updK, hq']) hkf)⟩ + refine hw7.mono fun w0 hw0 => ?_ + obtain ⟨K, k1, k2, k6⟩ := hw0 + exact Or.inl (Or.inr ⟨ct, ht, K, by omega, by omega, by omega, by omega, ⟨by omega, by omega⟩, k6⟩) + -- the spec's blocking send at `σf` + have hprf : ∀ u, Sched.prioOf σf u = Sched.prioOf σ u := by + intro u + by_cases hut : u = ct.bv + · subst hut; exact (prioOf_eq hkf).trans (prioOf_eq hk).symm + · unfold Sched.prioOf; rw [tcbsOf_eq, tcbsOf_eq, hkfp u hut] + have hnb0 : tsCode x.tcbState ≠ 3 ∧ tsCode x.tcbState ≠ 4 := by + have := runnable_code hrn; omega + have htq : ct.bv ∉ xe.epQueue := Endp.not_queued hI.1.toE ht hk hnb0 ptr hpE xe hke + have hqm : ∀ u ∈ xe.epQueue, ∃ z, σf.kheap u = some (.tcb z) := fun u hu => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hke).2 u hu + exact ⟨z, by rw [hkfp u (fun e => htq (e ▸ hu))]; exact hz⟩ + obtain ⟨σ', heq, F0, hik0, ⟨ds, hkt'⟩, -, hor, hcn, hsrt⟩ := + sendIpcBlock_eq hkf hkef hcbe htq hqm hrn ((J.isorted.1 _ _ hke).congr fun u _ => hprf u) + false bdg.bv g gr (L.mem s (scA ct) != 0#u64) + -- FL4's blocking send + have hIS : IpcSorted σf := QPK.QPS.sorted (QPK.qps_upd (ko := .tcb x2) hk rfl) J.isorted + obtain ⟨⟨Q0, R0, h0⟩, hB0', hX0, hS0, hSg0⟩ := hblk7 + have hst0 : L.epState s7 ptr = 0#u64 ∨ L.epState s7 ptr = 1#u64 := by + have hx := hI7.1.toE.at_ep hpE hkef + rw [epState_eq] + cases xe with + | idleEP => exact Or.inl hx.1 + | sendEP q => exact Or.inr hx.1 + | recvEP q => simp [Endp.CanBlock] at hcbe + have hnn : ∀ m, x2.tcbState ≠ .blockedOnNotification m := fun m h => by + have h' : tsCode x.tcbState = 6 := congrArg tsCode h + have := runnable_code hrn; omega + have p1f : endpoint.send_ipc st arI s7 a true false bdg g gr (L.mem s (scA ct) != 0#u64) ct ptr ⦃ s' _ => + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s7 s' ⦄ := + P1.endpoint.send_ipc.blockFoot arI s7 a true false bdg g gr _ ct ptr h0 (fun _ => hcb7) hHe hst0 + refine WP.spec_bind (WP.spec_and (WP.spec_and (send_ipc_blockF (ar := arI) hP0 s7 a σf hI7 ht hkf hnb0 hnn hpE hkef + hcbe (Sched.qSorted_of_spec (L := L) hI7.1.toE hI7.1.toN hT hIS ptr (Or.inl hpE)) false bdg g gr + (L.mem s (scA ct) != 0#u64)) + (P1.endpoint.send_ipc.block arI s7 a true false bdg g gr _ ct ptr Q0 R0 h0 hB0' hX0 hS0 hSg0 + (fun _ => hcb7) hHe hst0)) + p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW3⟩, hbk⟩, hfoot3⟩ + simp only [WP.uncurry'_pair] at ea hbk hfoot3 hIR hW3 + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + apply (WP.spec_ok _).2 + refine ⟨rfl, ?_⟩ + -- the spec's outcome + have hz : ((), σ') ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv + (decide (Fl4Spec.Rights.allowGrant ∈ rightsOf sd rv g gr)) + (decide (Fl4Spec.Rights.allowGrantReply ∈ rightsOf sd rv g gr)) (decide (x.tcbSchedContext ≠ none)) ct.bv + ptr.bv (updK σ ct.bv (.tcb { x with tcbFault := some f' }))).1 := by + rw [rights_grant, rights_grantReply, ← hdon] + show ((), σ') ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr _ ct.bv ptr.bv σf).1 + rw [heq]; exact Set.mem_singleton _ + refine ⟨((), σ'), handleFault_ep_mem hk hfh _ hz, ?_⟩ + -- the frame from the start + have hkt3 : σ'.kheap ct.bv = + some (.tcb { x with tcbState := .blockedOnSend ptr.bv ds, tcbFault := some f' }) := hkt' + have hsk : Sk σ σ' := by + have h2 : Sk σ σf := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + exact h2.trans F0.sk + have F : BFrT ct.bv ptr.bv σ σ' := by + refine ⟨hsk, fun q => ?_⟩ + by_cases hqt : q = ct.bv + · subst hqt + exact Or.inr ⟨_, _, hk, hkt3, Or.inl ⟨x, ptr.bv, ds, some f', rfl, rfl, rfl⟩⟩ + · rcases F0.kh q with e | ⟨o, o', h1, h2, hc⟩ + · exact Or.inl (e.trans (hkfp q hqt)) + · rw [hkfp q hqt] at h1 + rcases hc with ⟨_, _, _, _, _, e3, -⟩ | ⟨x1, y1, rfl, rfl, e3⟩ + · exact absurd e3 hqt + · exact Or.inr ⟨_, _, h1, h2, Or.inr ⟨x1, y1, rfl, rfl, e3⟩⟩ + -- the FL4 footprint from the start + have hC3 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf s' σ' := ⟨hW3, SzKeepC.of F0.szKeep⟩ + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := (hC2.trans hC3).1 + have hfoot := P1.Foot.trans hfoot7 hfoot3 + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + -- the assembly (as XTopJointSendBlockG's `JAtZ.runTmoJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + -- the scheduler: the current thread blocked, a reschedule pending + have hcn' : σ'.schedulerAction = .chooseNewThread := by + rcases hsa with h | h + · exact hcn hcur.symm h + · rcases hor with e | e + · exact e.trans h + · exact e + have hAI3 : ActIdle σ' := ⟨fun u hu => (by rw [hcn'] at hu; cases hu), F.idleOK J.act.2 hidt⟩ + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcn' + have hrun3 : CurRunM σ' := Or.inl (by rw [hcn']; intro h; cases h) + -- the dead: the thread is not dead; the endpoint leaves the dead set + have hdead : ∀ q ∈ D \ {ptr}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ ct.bv := fun h => htD (by rw [← P1.eq_of_bv h]; exact hq.1) + have hqe : q.bv ≠ ptr.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ⟨_, F.tcbO hqt hx, rfl, rfl, Or.inl rfl⟩) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 + (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {ptr}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {ptr}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) + (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) + (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the blocked sender's `RelIfBound` from the runnable thread's) + have hts6 : Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := some f' }) ct.bv σ = + ({((), σf)}, False) := Notif.threadSet_eq _ hk + have hik : IK ct.bv σ σ' := + ((ikm_threadSet (t := ct.bv) (p := ct.bv) (g := fun tcb => { tcb with tcbFault := some f' }) + fun _ => ⟨rfl, fun h => absurd rfl h, fun h => absurd rfl h⟩).of_eq hts6).trans hik0 + have hnqT : NotQueued σ ct.bv := ⟨hnq', hnr⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut ct.bv).ik hnqT hik + have hskp : Ipc.SKeep σ σ' := ⟨F.sk.rel, F.sk.tm, fun p y hy => by + obtain ⟨x', hx', e, -⟩ := F.tcbScB hy; exact ⟨x', hx', e.symm⟩, fun p y n hy => F.scB hy⟩ + have hrib : RelIfBound σ' ct.bv := Ipc.RelIfBound.of_keep (J.inv.rd ct.bv x hk (Or.inl hrn)) hskp + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have hscA := hscAm + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {ptr}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted fun y1 hy1 => (hsrt y1 hy1).congr fun u _ => (hprf u).symm, + hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have J' := J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (J.zD_diffE hpE) + exact ⟨⟨_, _, J'.xtop.rel.1, J'.xtop.rel.2.1, J'.xtop.rel.2.2.1, J'.xtop.rel.2.2.2.1, J'.xtop.rel.2.2.2.2.1⟩, + ⟨Z.vs, Ac, Lst, D \ {ptr}, J', NatFoot.ofW J.xtop hW (fun b hb => CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) + (fun b hb => J.cnW_nh hb) (natK_bfrt F)⟩⟩ + +set_option maxHeartbeats 64000000 in +/-- **Waiting handler receiver** at a resumed or choose mid state. -/ +theorem JAtZB.faultRecvMB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ptr bdg : U64} {sd rv g gr : Bool} + (hep : capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok (.Endpoint ptr bdg sd rv g gr)) + {d : U64} {q : List Fl4Spec.ObjRef} (hkq : σ.kheap ptr.bv = some (.endpoint (.recvEP (d.bv :: q)))) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x (by rw [hcur]; exact hk) + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hcq.cur] at h; exact h + have ht : ct ∈ Z.T := by rw [hct']; exact hcq.curT + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hti : σ.curThread ≠ σ.idleThread := hF.ni + -- the handler cap, its endpoint + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have e1 : c1 = .Endpoint ptr bdg sd rv g gr := by rw [hep] at hc1; simpa using hc1.symm + subst e1 + have habs := hcr.2 + simp only [absCap] at habs + have hfh : x.tcbFaultHandler = .endpointCap ptr.bv bdg.bv (rightsOf sd rv g gr) := habs.symm + have hAS : AbsSlot Z.th (ct.bv, Fl4Spec.tcbCnodeIndex 3) := ⟨ct, rfl, Or.inr ⟨htcb, 3, by decide, rfl⟩⟩ + have hpE : ptr ∈ Z.E := J.slotE hAS hep + have hpt : ptr.bv ≠ ct.bv := fun e => by rw [e, hk] at hkq; cases hkq + have hcbP := (J.midBlkFacts ht hcur hF hsa hk).2.2 + have hdon := J.faultDonWord ht hk + have hCN : CurInv σ ∨ x.tcbSchedContext = none := hF.inv_or_noSc J hsa (by rw [hcur]; exact hk) + refine handleFault_ep_wp hep ?_ + unfold faults.send_fault_ipc + dsimp only + -- `set_fault` + set x2 : Fl4Spec.Tcb := { x with tcbFault := some f' } with hx2 + set σf := updK σ ct.bv (.tcb x2) with hσf + -- `set_fault` at `J` + refine WP.spec_bind (J.setFaultJG ht hk (fun e0 d0 h => by + have := runnable_code hrn; rw [h] at this; simp [tsCode] at this) hcbP hf) ?_ + rintro s7 ⟨J7, -, hwF⟩ + have F7 : NatFoot (L := L) Z s σ s7 (updK σ ct.bv (.tcb x2)) := + NatFoot.ofW J.xtop hwF (fun b hb => Ipc.FinW.oobj hS hb) (fun b hb => Ipc.FinW.nv hS hb) (fun b hb => J.finW_nh hb) + (natK_tcbW hk rfl fun h => h) + have hkf : σf.kheap ct.bv = some (.tcb x2) := by simp [hσf, updK] + have hkfp : ∀ q, q ≠ ct.bv → σf.kheap q = σ.kheap q := by + intro q hq'; rw [hσf]; simp only [updK, Function.update_of_ne hq'] + have hkef : σf.kheap ptr.bv = some (.endpoint (.recvEP (d.bv :: q))) := by rw [hkfp _ hpt]; exact hkq + have hnb0 : tsCode x.tcbState ≠ 3 ∧ tsCode x.tcbState ≠ 4 := by + have := runnable_code hrn; omega + have htq : ct.bv ∉ (Fl4Spec.Endpoint.recvEP (d.bv :: q)).epQueue := + Endp.not_queued hI.1.toE ht hk hnb0 ptr hpE _ hkq + have hdc : d.bv ≠ ct.bv := fun e0 => htq (by rw [← e0]; simp [Fl4Spec.Endpoint.epQueue]) + have hCN7 : CurInv σf ∨ x2.tcbSchedContext = none := hCN.imp (fun h => curInv_setFault h hk (some f')) id + have hpost : ∀ don : Bool, (don = true → x.tcbSchedContext ≠ none) → endpoint.send_ipc st arI s7 a true false bdg g gr don ct ptr ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr don ct.bv ptr.bv σf).1, + JAtN (L := L) arI AR Z H Cs s7 σf r.1 r.2 z.2 ⦄ := by + intro don hdn + by_cases hgg : g = true ∨ gr = true + · by_cases hA : ∀ xd, σf.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, + xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none + · by_cases hS : don = false ∨ ∀ xd, σf.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none + · exact J7.sendFaultPJMB hAT ht hkf hrn rfl hf.nt hcur hsa hCN7 hti hnq' hpE hkef true bdg g gr don hgg hA hS + · have hd1 : don = true := by + cases don + · exact absurd (Or.inl rfl) hS + · rfl + subst hd1 + have hCI7 : CurInv σf := hCN7.resolve_right (hdn rfl) + refine J7.sendFaultPDJMB hAT ht hkf hrn rfl hf.nt hcur hsa hCI7 hti hnq' hpE hkef true bdg g gr hgg hA ?_ + intro xd hxd + by_contra hne + exact hS (Or.inr fun xd' hxd' => by rw [hxd] at hxd'; cases hxd'; exact hne) + · refine J7.sendFaultIJMB ht hkf hrn rfl hf.nt hcur hsa hCN7 hti hnq' hpE hkef true bdg g gr don (Or.inr ?_) + intro xd hxd e' rep dd hst + by_contra hne + apply hA + intro xd' hxd' e'' rep' dd' hst' + rw [hxd] at hxd'; cases hxd' + rw [hst] at hst'; cases hst'; exact hne + · exact J7.sendFaultIJMB ht hkf hrn rfl hf.nt hcur hsa hCN7 hti hnq' hpE hkef true bdg g gr don + (Or.inl ⟨Bool.eq_false_iff.2 fun h => hgg (Or.inl h), Bool.eq_false_iff.2 fun h => hgg (Or.inr h)⟩) + refine WP.spec_bind (hpost _ (fun h => by rw [hdon] at h; exact of_decide_eq_true h)) ?_ + rintro ⟨s', a'⟩ ⟨z, hz, hJ⟩ + apply (WP.spec_ok _).2 + refine ⟨rfl, ?_⟩ + have hz' : z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv + (decide (Fl4Spec.Rights.allowGrant ∈ rightsOf sd rv g gr)) + (decide (Fl4Spec.Rights.allowGrantReply ∈ rightsOf sd rv g gr)) (decide (x.tcbSchedContext ≠ none)) ct.bv + ptr.bv (updK σ ct.bv (.tcb { x with tcbFault := some f' }))).1 := by + rw [rights_grant, rights_grantReply, ← hdon]; exact hz + obtain ⟨V', Ac', Lst', D', J', F'⟩ := hJ + exact ⟨((), z.2), handleFault_ep_mem hk hfh _ hz', + ⟨_, _, J'.xtop.rel.1, J'.xtop.rel.2.1, J'.xtop.rel.2.2.1, J'.xtop.rel.2.2.2.1, J'.xtop.rel.2.2.2.2.1⟩, + ⟨V', Ac', Lst', D', J', J.natTrans F7 F'⟩⟩ + +set_option maxHeartbeats 4000000 in +/-- **`handle_fault` at a resumed or choose mid state.** -/ +theorem JAtZB.faultHypMB (hAT : P0.ArchTotal arI) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) {ct : U64} + (hct : sched.cur_thread st s = ok ct) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hnq := hF.nq + have hvfh := hF.vfh + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have hv : Fl4Spec.validFaultHandler (absCap c1) := by + rw [hcr.2]; exact hvfh x (by rw [hcur]; exact hk) + by_cases hn : c1 = .Null + · subst hn + exact J.noFaultMB hF hsa hct hnq hc1 f f' + obtain ⟨p, b, sd, rv, g, gr, rfl⟩ := vfh_ep hv hn + have hAS : AbsSlot Z.th (ct.bv, Fl4Spec.tcbCnodeIndex 3) := ⟨ct, rfl, Or.inr ⟨htcb, 3, by decide, rfl⟩⟩ + have hpE : p ∈ Z.E := J.slotE hAS hc1 + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨xe, hke, -⟩ := hI.1.ep p hpE + cases xe with + | recvEP l => + obtain ⟨-, xs, hxs0, hq, -⟩ := hI.1.toE.at_ep hpE hke + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons] at hq + subst hq + exact J.faultRecvMB hAT hF hsa hct hnq hc1 hke hf + | idleEP => + exact J.faultBlockMB hF hsa hct hnq hc1 (fun xe h => by rw [hke] at h; cases h; trivial) hf + | sendEP q => + exact J.faultBlockMB hF hsa hct hnq hc1 (fun xe h => by rw [hke] at h; cases h; trivial) hf + +/-- **`handle_fault` at `VmEptState` at a resumed or choose mid state.** -/ +theorem VmEptState.handleFaultMVE {V X : Set U64} {XL : U64 → Nat} (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) {ct : U64} + (hct : sched.cur_thread st s = ok ct) {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.1.1.faultHypMB hAT hF hsa hct hf) fun _ ⟨z, hz, hR, _, _, _, _, B', F⟩ => + ⟨z, hz, hR, (J.ofNat B' F).anyX arI⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmFaultSw.lean b/proofs/Fl4Proofs/P3/VmFaultSw.lean new file mode 100644 index 000000000..61789a671 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFaultSw.lean @@ -0,0 +1,2022 @@ +/- +P3 (line mmuAD, Stage 3 step 3 (S, V post), generated by `handoff/tools/gen_faultsw.py`): **fault sends to a waiting receiver at a +pending switch** (`switchThread u0`, `u0 ≠ ct`, `CurInv`): twins `…SB` of the resumed `JAtZB` lemmas (P3/XTopJointFaultRecv) +with the switch edits of the native XTopJointFaultRecvM / XTopJointFaultDonM; post `JAtN` (with `NatFoot`). No named premise +beyond `P0.ArchTotal`. +-/ +import Fl4Proofs.P3.VmFaultMid +import Fl4Proofs.P3.VmSwitchR +import Fl4Proofs.P3.XTopJointFaultDonM +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- **Fault send to a waiting receiver without grant** at a pending switch. -/ +theorem JAtZB.sendFaultIJSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrn : Fl4Spec.runnable xt.tcbState = true) {f : Fl4Spec.Fault} + (hft : xt.tcbFault = some f) (hnt : ∀ b, f ≠ .timeout b) (hct : σ.curThread = t.bv) + {u0 : Fl4Spec.ObjRef} (hu0 : σ.schedulerAction = .switchThread u0) (hu0t : u0 ≠ t.bv) + (hCI : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hno : (g = false ∧ gr = false) ∨ + ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep = none) : + endpoint.send_ipc st arI s a blk false badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ).1, + JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) ht hdT htd hkt1 hkd1 hft (faultOK_nt (L := L) s1 t xt hnt) e badge g + hTP hdH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 hdT hA2 (fun p hp => by simp [σ2, hp]) + hkd1 hkd2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1e] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xd b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + have Fr2 : CFr e.bv d'.bv σ σ2 := (Fr1.trans (CFr.um _ _ σ1 um2)).trans (CFr.upd (σ := Endp.setUm σ1 um2) hkd1 + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans (IK.updSelf (σ := Endp.setUm σ1 um2) hkd1 hxsc)) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((SKeep.um σ1 um2).trans (SKeep.updK (σ := Endp.setUm σ1 um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb s := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] + refine WP.spec_bind (Pₘ := fun s3 => ∃ σ3 x3 X3, + Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) rep σ2 = ({((), σ3)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s3) σ3 ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s3 σ3 ∧ + σ3.kheap t.bv = some (.tcb xt) ∧ σ3.kheap d'.bv = some (.tcb x3) ∧ RbIn x3.tcbState ∧ + tsCode x3.tcbState ≠ 6 ∧ x3.tcbSchedContext = xd.tcbSchedContext ∧ + (∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue) ∧ + P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) ∧ + P1.WritesIn st s s3 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s b ∨ RcvBufW xd b) ∧ + CFr e.bv d'.bv σ σ3 ∧ IK d'.bv σ σ3 ∧ Ipc.SKeep σ σ3 ∧ σ3.curThread = σ.curThread ∧ + σ3.schedulerAction = σ.schedulerAction ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X3 s3 Q0 R0 ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s3 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s3 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s3 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 ∧ P1.ExcT X3 d' ∧ + (d' ∈ X3 ∨ ¬ P1.Blocked (L.mem s3 (P1.stA d'))) ∧ + (∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d')) ?_ ?_ + · cases hrp : rep with + | none => + rw [hrp] at hrep + have h00 := optRef_none hrep + simp only [h00, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + have hrA : L.mem s2 (P1.replyA d') = 0#u64 := by + show L.mem s2 (tReplyA d') = 0#u64; rw [hrw]; exact h00 + exact ⟨σ2, x2, _, rfl, hR2, hS2', hkt2, hkd2, Or.inl ⟨_, _, _, hst2'⟩, + by rw [hst2']; simp [tsCode], hxsc, hq2, hW2, footW_trans (footW_of F1) F2, Fr2, IK2, SK2, rfl, rfl, h2, hB2, hX2, hS2'', + hSg2, fun y hy => by simpa using hy, Or.inl (Or.inr rfl), hX2.noref0 hdta hrA⟩ + | some r => + rw [hrp] at hrep hrepR hst2' + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by simp [σ2, σ1, hwe, hwd, hkr] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd, hrp]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_mono (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set σ3a := Notif.updK σ2 w.bv (.reply { xr with replyTcb := none }) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + refine ⟨_, x3, _, e3, hR3, hS3, ?_, ?_, Or.inr rfl, by simp [x3, tsCode], by simp [x3, hxsc], ?_, hW3', + footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3), ?_, ?_, ?_, ?_, ?_, h3, + hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1), + hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr, hS2''.keepT hX2 k3, + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩, + fun y hy => ?_, Or.inr (by rw [e3']; exact P1.not_blocked_inactive), + (hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr).noref hdta (by rw [e3']; exact P1.inactive_ne.1) + (by rw [e3']; exact P1.inactive_ne.2)⟩ + · rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + · rw [stsAct_kheap]; simp + · intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + · exact (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + · have IK3a : IK d'.bv σ σ3a := IK2.trans (IK.updOther (o' := .reply { xr with replyTcb := none }) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σ (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + · exact ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply { xr with replyTcb := none }) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + · rw [e3s]; rfl + · rw [e3s]; rfl + · obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2 + rintro s3 ⟨σ3, x3, X3, hU, hR3, hS3, hkt3, hkd3, hrb3, hns3, hsc3, hq3, hW3, F3, Fr3, IK3, SK3, hct3, hact3, + h3, hB3, hX3, hS3', hSg3, hXc3, hx3, hnr3⟩ + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := fun e0 => by + have h00 := hxt3.fault0.1 (by rw [e0]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hft] at h00; cases h00 + have hks3 : L.ksb s3 = L.ksb s := hW3.1 + -- the caller's facts for its `Inactive` + have hsw : σ.schedulerAction ≠ .resumeCurThread := by rw [hu0]; intro h; cases h + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + obtain ⟨hqb, hrb'⟩ := J.curBits ht hnq hnrT + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := (IpcRel.ofSRN hR3 hS3).2.2.1 + have hna3 : L.actionW s3 ≠ t := by + obtain ⟨Q, R', hcq, -⟩ := hSR3.lists + have h := hSR3.act + rw [hact3, hu0] at h + obtain ⟨-, -, hw⟩ := h + intro h' + apply hu0t + rw [← hw] + unfold P1.LawfulStore.actionW at h'; rw [hcq.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.SCHED_ACTION = t from h') + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + have hstsT := WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 ht hkt3 hqt3 + (by have := runnable_code hrn; omega) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hSR3 ht sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 t _ Q0 R0 h3 hB3 hX3 hS3' hSg3 htH P1.not_blocked_inactive P1.inactive_ne + (Or.inr hnbt3) (hX3.noref (hTP t htH) hnrp3 (fun e0 => hnbt3 (Or.inr (Or.inl e0)))) + ⟨fun c hc' p hp hm => absurd hm (h0.notQ htH hqb hc' hp), fun c hc' hm => absurd hm (h0.notR htH hrb' hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 t sched.TS_INACTIVE h3 htH)) + -- FL4: no reply pushed, the caller `Inactive` + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ sched.set_thread_state st s3 t sched.TS_INACTIVE = ok r.1) + ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [if_pos (bne_iff_ne.2 hty3)] + by_cases hi : (L.mem s (tReplyA d') != 0#u64) = true + · rw [if_pos hi] + have hgg : g = false ∧ gr = false := by + rcases hno with h | hn + · exact h + · exfalso + have hr0 := hn xd hkd _ _ _ hstd + rw [hr0] at hrep + have h00 := optRef_none hrep + rw [h00] at hi + simp at hi + obtain ⟨rfl, rfl⟩ := hgg + simp only [Bool.false_eq_true, ↓reduceIte] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + · rw [if_neg hi] + exact WP.spec_bind (spec_self hstsT) fun s4 h4 => (WP.spec_ok _).2 ⟨rfl, h4⟩ + rintro ⟨s4, a4⟩ ⟨ha4, hs4⟩ + simp only at ha4 hs4 + subst ha4 + try dsimp only + have hpost := hstsT + rw [hs4] at hpost + obtain ⟨⟨⟨hR4, hS4⟩, hW4⟩, ⟨h4, hB4, hX4, hS4', hSg4, -, k4⟩, fu4⟩ := (WP.spec_ok _).1 hpost + have hkd4 : (ciSt σ3 t.bv xt).kheap d'.bv = some (.tcb x3) := by rw [ciSt_kh (Ne.symm htd')]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, (ciSt σ3 t.bv xt).kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z (by rw [← ciSt_kh (ciSt_ne hz (fun _ h => by cases h))]; exact hz) + have hx4 : d' ∈ {y | y ∈ X3 ∧ y ≠ t} ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := by + rcases hx3 with h | h + · exact Or.inl ⟨h, Ne.symm htd⟩ + · right + have e0 : L.mem s4 (P1.stA d') = L.mem s3 (P1.stA d') := by + rw [P1.stA_eq] + exact k4.fld d' hdH _ (by simp) (by simp) (by simp) (by simp) (fun e1 => absurd e1 (Ne.symm htd)) + rw [e0]; exact h + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := fun r hr => by + rcases k4.rtcb r hr with e0 | ⟨-, e0⟩ + · rw [e0]; exact hnr3 r hr + · rw [e0]; intro h0'; exact hdta.ne0 (by rw [← h0']; rfl) + have hks4 : L.ksb s4 = L.ksb s := hW4.1.trans hks3 + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 hns3 + .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q0 R0 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK (ciSt σ3 t.bv xt) d'.bv (.tcb x5)) d'.bv = ciSt σa t.bv xt := + ciSt_swap htd' hdc3 + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 (ciSt σa t.bv xt) := by + rw [← heq5]; exact hIR0 + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW3.trans1 (hW4.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3).1 hb))))) + have hW5' : P1.WritesIn st s s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr (X := {y | y ∈ X3 ∧ y ≠ t}) (fun y hy => hXc3 y hy.1))⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have Fr5 : CFr e.bv d'.bv σ σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σ (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σ σa := IK5a.trans (IK.sts _ _ _) + have SK5 : Ipc.SKeep σ σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((J.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σa.schedulerAction = σ.schedulerAction := by + rw [hσa, stsAct_ne (by show d'.bv ≠ σ3.curThread; exact hdc3)]; exact hact3 + -- the caller's `Inactive` after the wake (`ciSt_swap`) + have hnqT : ∀ dm p, t.bv ∉ σa.readyQueues dm p := by rw [IK5.2.1]; exact hnq + have hnrT' : t.bv ∉ σa.releaseQueue := by rw [IK5.2.2.1]; exact hnrT + have I5' : Inv (ciSt σa t.bv xt) := ciSt_inv hkta I5 hnqT hnrT' + have B5' : BlockedScActive (ciSt σa t.bv xt) := ciSt_bsa (Fr5.bsa J.bsa) + have hcn : (ciSt σa t.bv xt).schedulerAction = σ.schedulerAction := + (ciSt_actN (by rw [act5]; exact hsw)).trans act5 + have hidT : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI5 : ActIdle (ciSt σa t.bv xt) := + ⟨fun u hu => by + rw [hcn, hu0] at hu; cases hu + exact ciSt_sched hkta (Fr5.sched (J.act.1 u0 hu0)) hu0t, ciSt_idleOK hidT (Fr5.idleOK J.act.2)⟩ + have hrun5 : CurRunM (ciSt σa t.bv xt) := Or.inl (by rw [hcn]; exact hsw) + have hCI5 : CurInv (ciSt σa t.bv xt) := ciSt_curInv (Fr5.curInv hCI) (by rw [Fr5.sk.ct, hct]) hkta + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by have := runnable_code hrn; omega) + obtain ⟨su, umu, Ju, hWu, hFu, -, -, Fu⟩ := J.bufRebaseX hdT hdth hkd hW5' hfoot + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σ umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => Ju.recvReplyB q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have J5 := Ju.wake_postCI hkta hIR hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) I5' B5' hAI5 + (Or.inr hCI5) hrun5 hRR htD hcD ⟨d', hdT, rfl⟩ + have N5 := NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj Ju.xtop.fin.1.1.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) ((natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)).trans (natK_ciSt hkta)) + -- the tail + have G := (ciSt_skD (σ := σa) (c := t.bv) (x := xt)) + have hk5 : (ciSt σa t.bv xt).kheap d'.bv = some (.tcb x5) := by + rw [ciSt_kh (Ne.symm htd'), hσa, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ (ciSt σa t.bv xt).releaseQueue := by rw [ciSt_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → (ciSt σa t.bv xt).kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya : σa.kheap p = some (.schedContext y n) := by + rw [← ciSt_kh (ciSt_ne hy (fun _ h => by cases h))]; exact hy + have hy0 := SK5.2.2.2 p y n hya + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J.inv.back _ _ _ hkd hp' + rw [hy0] at hky'; cases hky' + exact J.bsa.tcb p y n d'.bv xd hy0 hyt hkd (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ (ciSt σa t.bv xt).idleThread := by + rw [G.idle, Fr5.sk.idle] + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have htc5 : d'.bv ≠ (ciSt σa t.bv xt).curThread := by rw [G.ct, Fr5.sk.ct]; exact hdc + have hcond : ¬ ((g = true ∨ gr = true) ∧ rep ≠ none) := by + rintro ⟨hg, hr⟩ + rcases hno with ⟨rfl, rfl⟩ | hn + · rcases hg with h | h <;> cases h + · exact hr (hn xd hkd _ _ _ hstd) + have hspec := sendIpc_faultInactive_eq (don := don) hk hkd hde htd' hstd blk badge.bv g gr hcond e2 hU hkt3 hkd3 + (by rw [hft]; simp) (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ = + Endp.specWakeTailE d'.bv (ciSt σa t.bv xt) := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, + ⟨Z.vs, _, _, _, J7, J.natTrans Fu (Ju.natTrans N5 F7)⟩⟩ + +set_option maxHeartbeats 64000000 in +/-- **Fault send pushing a reply without donation** at a pending switch. -/ +theorem JAtZB.sendFaultPJSB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrn : Fl4Spec.runnable xt.tcbState = true) {f : Fl4Spec.Fault} + (hft : xt.tcbFault = some f) (hnt : ∀ b, f ≠ .timeout b) (hct : σ.curThread = t.bv) + {u0 : Fl4Spec.ObjRef} (hu0 : σ.schedulerAction = .switchThread u0) (hu0t : u0 ≠ t.bv) + (hCI : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr don : Bool) + (hg : g = true ∨ gr = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) + (hnd : don = false ∨ ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none) : + endpoint.send_ipc st arI s a blk false badge g gr don t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ).1, + JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) ht hdT htd hkt1 hkd1 hft (faultOK_nt (L := L) s1 t xt hnt) e badge g + hTP hdH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 hdT hA2 (fun p hp => by simp [σ2, hp]) + hkd1 hkd2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1e] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xd b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + have Fr2 : CFr e.bv d'.bv σ σ2 := (Fr1.trans (CFr.um _ _ σ1 um2)).trans (CFr.upd (σ := Endp.setUm σ1 um2) hkd1 + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans (IK.updSelf (σ := Endp.setUm σ1 um2) hkd1 hxsc)) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((SKeep.um σ1 um2).trans (SKeep.updK (σ := Endp.setUm σ1 um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb s := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkd _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by simp [σ2, σ1, hwe, hwd, hkr] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + have Fr3 : CFr e.bv d'.bv σ σ3 := (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σ σ3 := by + have IK3a : IK d'.bv σ σ3a := IK2.trans (IK.updOther (o' := .reply xr3) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σ (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + have SK3 : Ipc.SKeep σ σ3 := + ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans + (SKeep.stsAct _ _) + have hct3 : σ3.curThread = σ.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d' := + hX3.noref hdta (by rw [e3']; exact P1.inactive_ne.1) (by rw [e3']; exact P1.inactive_ne.2) + have F3 := footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := fun e0 => by + have h00 := hxt3.fault0.1 (by rw [e0]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hft] at h00; cases h00 + have hks3 : L.ksb s3 = L.ksb s := hW3'.1 + -- the caller's and the receiver's words at `s3` + have hsw : σ.schedulerAction ≠ .resumeCurThread := by rw [hu0]; intro h; cases h + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have hna3 : ∀ u ∈ Z.T, u.bv ≠ u0 → L.actionW s3 ≠ u := fun u hu hne h' => by + have h := hSR3.act + rw [hact3, hu0] at h + obtain ⟨-, -, hw⟩ := h + apply hne + rw [← hw] + unfold P1.LawfulStore.actionW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.SCHED_ACTION = u from h').symm + have hdu0 : d'.bv ≠ u0 := by + intro e0 + obtain ⟨xu, -, -, -, hku, -, hru, -⟩ := schedulable_inv (J.act.1 u0 hu0) + rw [← e0, hkd] at hku; cases hku; simp [hstd, Fl4Spec.runnable] at hru + have hcw3 : L.curW s3 = t := by + have h : (Sched.nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σ.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0 => by + obtain ⟨Q', R'', hcq0, hs0⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨e1, hA⟩ := J.curScAcB hCI hti + have hcw : L.curW s = t := by + apply P1.eq_of_bv + rw [← hct, hs0.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq0.cur] + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + have h1 : L.mem s3 (P1.scA t) = L.mem s (P1.scA t) := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs0.tcb t ht + obtain ⟨x'', hk'', -, -, hscw', -⟩ := hs3.tcb t ht + rw [hkt] at hk'; cases hk' + rw [hkt3] at hk''; cases hk'' + have e2 := hscw.symm.trans hcs + have hv0 : L.mem s (P1.scA t) ≠ 0#u64 := fun e => by rw [e, Notif.optRef_zero] at e2; cases e2 + exact optRef_some_eq ((hscw'.symm.trans hcs).trans (e2.symm.trans (Notif.optRef_ne hv0))) + rw [h1, ← hcw, e1]; exact hA + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the receiver's SC: no donation + have hndF : don = true → x3.tcbSchedContext = none → False := fun hdon hx0 => by + rcases hnd with h | h + · rw [h] at hdon; cases hdon + · exact h xd hkd (by rw [← hsc3]; exact hx0) + obtain ⟨xd3, hkd3', hdsc⟩ := hSM d' hdT + rw [hkd3] at hkd3'; cases hkd3' + have hnd1 : don = false ∨ L.mem s3 (P1.scA d') ≠ 0#u64 := by + cases don + · exact Or.inl rfl + · right; intro h0; exact hndF rfl (by rw [hdsc, h0, optRef_zero]) + -- the push on `IRel`, at P1 + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hns3 : tsCode xt.tcbState ≠ 6 := by have := runnable_code hrn; omega + have hPush : ipc.reply_push st arI s3 a t d' w don ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv don σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1 ⦄ := by + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := don) (P := fun s' σ' => σ' = pushSt σ3 t.bv w.bv xr3 xt ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hdon _ _ _ _ _ _ _ hx0 => (hndF hdon hx0).elim)) + (P1.ipc.reply_push.ipc hAT s3 a t d' w don Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht (fun e0 => hu0t e0.symm)) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hqbD hrbD (hna3 d' hdT hdu0) (hiw3 d' hdT hid) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a t d' w don h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht (fun e0 => hu0t e0.symm)) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 (J.zH.1 d' hdT) + (Ne.symm htd) hcA hnd1)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, hb2, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hb2, hf, hsb⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv don σ3 = ({((), pushSt σ3 t.bv w.bv xr3 xt)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 t.bv w.bv xr3 xt) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 t.bv w.bv xr3 xt) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + (∀ y ∈ P1.Heap.tcbs H, y ≠ t → L.mem r.1 (P1.stA y) = L.mem s3 (P1.stA y)) ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok r.1) ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [if_pos (bne_iff_ne.2 hty3)] + rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, ⟨⟨Q4, R4, h4⟩, hB4, hX4, hS4', hSg4⟩, hst4, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 h4 hB4 hX4 hS4' hSg4 hst4 fu4 hsb4 ⊢ + subst ha4 + try dsimp only + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3 (SzKeepC.of Fr3.szKeep) hb)) + have hks4 : L.ksb s4 = L.ksb s := hW4'.1 + set σ4 := pushSt σ3 t.bv w.bv xr3 xt with hσ4 + have hkd4 : σ4.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hq4 : ∀ e'' ∈ Z.E, ∀ z, σ4.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := + fun e'' he'' z hz => hq3 e'' he'' z ((ps_oth hkt3 hkr3 hwt (fun _ h => by cases h) (fun _ h => by cases h)).1 hz) + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hst4d : L.mem s4 (P1.stA d') = sched.TS_INACTIVE := by rw [hst4 d' hdH (Ne.symm htd)]; exact e3' + have hx4 : d' ∈ (∅ : Set U64) ∨ ¬ P1.Blocked (L.mem s4 (P1.stA d')) := Or.inr (by rw [hst4d]; exact P1.not_blocked_inactive) + have hnr4 : ∀ r ∈ P1.Heap.replies H, L.mem s4 (P1.rTcbA r) ≠ d' := + hX4.noref hdta (by rw [hst4d]; exact P1.inactive_ne.1) (by rw [hst4d]; exact P1.inactive_ne.2) + -- the receiver runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s4 _ hR4 hS4 hdT hkd4 hq4 + (by simp [x3, tsCode]) .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot (IpcRel.ofSRN hR4 hS4).2.2.1 hdT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s4 d' _ Q4 R4 h4 hB4 hX4 hS4' hSg4 hdH P1.not_blocked_running P1.running_ne hx4 hnr4 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s4 d' sched.TS_RUNNING h4 hdH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { x3 with tcbState := .running } with hx5 + set σa := Notif.stsAct (Notif.updK σ3 d'.bv (.tcb x5)) d'.bv with hσa + have hdc3 : d'.bv ≠ σ3.curThread := by rw [hct3]; exact hdc + have heq5 : Notif.stsAct (Notif.updK σ4 d'.bv (.tcb x5)) d'.bv = pushSt σa t.bv w.bv xr3 xt := + ps_swap htd' hwd hdc3 + have hIR0 := IpcRel.ofSRN hR5 hS5 + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s5 (pushSt σa t.bv w.bv xr3 xt) := by + rw [← heq5]; exact hIR0 + have hW5' : P1.WritesIn st s s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW4'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks4).1 hb))))) + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q4, R4, h5.congrX P1.empty_congr⟩, hB5, hX5, hS5', hSg5⟩ + have hfoot := footW_trans (footW_trans F3 (footW_of fu4)) (footW_of fu5) + have hrb3 : RbIn x3.tcbState := Or.inr rfl + have Fr5 : CFr e.bv d'.bv σ σa := Fr3.trans (CFr.stsRun hkd3 hrb3) + have IK5a : IK d'.bv σ (Notif.updK σ3 d'.bv (.tcb x5)) := IK3.trans (IK.updSelf (y := x5) hkd3 rfl) + have IK5 : IK d'.bv σ σa := IK5a.trans (IK.sts _ _ _) + have SK5 : Ipc.SKeep σ σa := (SK3.trans (SKeep.updK (p := d'.bv) (o := .tcb x5) (fun y h => ⟨x3, hkd3, by cases h; rfl⟩) + (fun _ _ h => by cases h))).trans (SKeep.stsAct _ _) + have hrib : RelIfBound σa d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK5 + have I5 : Inv σa := ((J.inv.toBut d'.bv).ik hnqD IK5).1.inv fun _ _ _ => hrib + have act5 : σa.schedulerAction = σ.schedulerAction := by + rw [hσa, stsAct_ne (by show d'.bv ≠ σ3.curThread; exact hdc3)]; exact hact3 + -- the push after the wake (`ps_swap`) + have hkta : σa.kheap t.bv = some (.tcb xt) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, htd', hkt3] + have hkwa : σa.kheap w.bv = some (.reply xr3) := by rw [hσa, stsAct_kheap]; simp [Notif.updK, hwd, hkr3] + have hnqT : ∀ dm p, t.bv ∉ σa.readyQueues dm p := by rw [IK5.2.1]; exact hnq + have hnrT' : t.bv ∉ σa.releaseQueue := by rw [IK5.2.2.1]; exact hnrT + set σ' := pushSt σa t.bv w.bv xr3 xt with hσ' + have I5' : Inv σ' := ps_inv hkta hkwa hwt I5 hnqT hnrT' + -- an SC bound to the caller is the current SC, active + have hscA : ∀ p y n, σa.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + have hp0 : σ.kheap p = some (.schedContext y n) := SK5.2.2.2 _ _ _ hp + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp0 + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp0).2⟩ + have hS := J.xtop.fin.1.1.1 + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp0 hyt + rw [hkt] at hku; cases hku + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + have e1 : σ.curSc = q.bv := Option.some.inj (hcs.symm.trans hxs) + rw [← e1] at hp0 + exact hCI.active (J.curSc_ne_idle hCI hti) y n hp0 + have B5' : BlockedScActive σ' := ps_bsa hkta hkwa hwt hscA (Fr5.bsa J.bsa) + have hcn : σ'.schedulerAction = σ.schedulerAction := (ps_actN (by rw [act5]; exact hsw)).trans act5 + have hidT : σa.idleThread ≠ t.bv := by rw [Fr5.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI5 : ActIdle σ' := + ⟨fun u hu => by + rw [hcn, hu0] at hu; cases hu + exact ps_sched hkta hkwa hwt (Fr5.sched (J.act.1 u0 hu0)) hu0t, + ps_idleOK hkta hkwa hwt hidT (Fr5.idleOK J.act.2)⟩ + have hrun5 : CurRunM σ' := Or.inl (by rw [hcn]; exact hsw) + have hCI5 : CurInv σ' := ps_curInv hkta hkwa hwt (Fr5.curInv hCI) (by rw [Fr5.sk.ct, hct]) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by have := runnable_code hrn; omega) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := hR.reply w hwR + rw [hkr] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hkq : ∀ q ∈ D, σ'.kheap q.bv = σa.kheap q.bv := fun q hq => ps_ko (hcD q hq) (hwD q hq) + obtain ⟨su, umu, Ju, hWu, hFu, -, -, Fu⟩ := J.bufRebaseX hdT hdth hkd hW5' hfoot + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRR : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σ umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => Ju.recvReplyB q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σa.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hwt h1 h2 + have J5 := Ju.wake_postGR hIR hF hWu hblk hFu (DWFr.ofUm (DWFr.ofCFr Fr5) umu) ps_skD + (ps_ksub hkta hkwa hwt) (ps_szKeep hkta hkwa hwt) (ps_ykeep hkta hkwa hwt) (ps_mcpKeep hkta hkwa hwt) + (ps_bufKeep hkta hkwa hwt) (fun d _ h => ps_recvB hkta hkwa hwt h) (ps_ksz hkta hkwa hwt) + (ps_kback hkta hkwa hwt) (ps_ipcFr hkta hkwa hwt) (ps_ck hkta hkwa hwt) (fun d _ h => ps_free hkta hkwa hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I5' B5' hAI5 (Or.inr hCI5) hrun5 (ps_own hkta hkwa hwt ((DWFr.ofCFr Fr5).own ⟨d', hdT, rfl⟩ J.own)) + (ps_off hkta hkwa hwt ((DWFr.ofCFr Fr5).off J.off)) (ps_nsc hkta hkwa hwt ((DWFr.ofCFr Fr5).nsc J.nsc)) + (ps_isorted hkta hkwa hwt ((DWFr.ofCFr Fr5).isorted J.isorted)) hRR htD + -- the tail + have hk5 : σ'.kheap d'.bv = some (.tcb x5) := by + rw [ps_ko (Ne.symm htd') (Ne.symm hwd), hσa, stsAct_kheap]; simp + have hnr5 : d'.bv ∉ σ'.releaseQueue := by rw [ps_rel, SK5.1]; exact hnqD.2 + have hact5 : ∀ p y n, x5.tcbSchedContext = some p → σ'.kheap p = some (.schedContext y n) → + Fl4Spec.scActive y = true := by + intro p y n hp hy + have hya : σa.kheap p = some (.schedContext y n) := + (ho (fun _ h => by cases h) (fun _ h => by cases h)).1 hy + have hy0 := SK5.2.2.2 p y n hya + have hp' : xd.tcbSchedContext = some p := by rw [← hsc3]; exact hp + obtain ⟨y', n', hky', hyt⟩ := J.inv.back _ _ _ hkd hp' + rw [hy0] at hky'; cases hky' + exact J.bsa.tcb p y n d'.bv xd hy0 hyt hkd (by rw [hstd]; rfl) + have hti5 : d'.bv ≠ σ'.idleThread := by + rw [(ps_skD (σ := σa) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).idle, Fr5.sk.idle] + exact hid + have htc5 : d'.bv ≠ σ'.curThread := by + rw [(ps_skD (σ := σa) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).ct, Fr5.sk.ct]; exact hdc + have hspec := sendIpc_faultPush_eq (don := don) hk hkd hde hstd blk badge.bv g gr hg e2 hU hkt3 hkd3 hpush hkd4 (by rw [hft]; simp) + (t := t.bv) + have hspec' : Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr don t.bv e.bv σ = + Endp.specWakeTailE d'.bv σ' := by + rw [hspec]; exact congrArg _ heq5 + refine WP.spec_mono (J5.sendTailJ hdT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + have N5 := NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj Ju.xtop.fin.1.1.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) + ((natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr5) umu)).trans (natK_pushSt hkwa hkta (fun e0 => hwt e0.symm))) + exact ⟨((), σ7), by rw [hspec', h7]; exact Set.mem_singleton _, + ⟨Z.vs, _, _, _, J7, J.natTrans Fu (Ju.natTrans N5 F7)⟩⟩ + +set_option maxHeartbeats 32000000 in +/-- **The donating part of `reply_push` at a pending switch** (`JAtZB`, with its footprint). -/ +theorem JAtZB.pushDonJSB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t d w sc : U64} (ht : t ∈ Z.T) (hd : d ∈ Z.T) (hw : w ∈ Z.R) (hsC : sc ∈ Z.C) (htd : t ≠ d) + {xt xd : Fl4Spec.Tcb} {xw : Fl4Spec.Reply} {y : Fl4Spec.SchedContext} {n : Nat} + (hkt : σ.kheap t.bv = some (.tcb xt)) (hkd : σ.kheap d.bv = some (.tcb xd)) + (hkw : σ.kheap w.bv = some (.reply xw)) (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hst : xt.tcbState = .blockedOnReply w.bv) (hxt : xt.tcbSchedContext = some sc.bv) (hyt : y.scTcb = some t.bv) + (hxd : xd.tcbSchedContext = none) (hsd : xd.tcbState = .inactive) (hwt : xw.replyTcb = some t.bv) + (hct : σ.curThread = t.bv) (hcsc : σ.curSc = sc.bv) {u : Fl4Spec.ObjRef} {xu : Fl4Spec.Tcb} (hsw : σ.schedulerAction = .switchThread u) (hut : u ≠ t.bv) + (hud : u ≠ d.bv) (hku : σ.kheap u = some (.tcb xu)) + (hnqt : NotQueued σ t.bv) (hnqd : NotQueued σ d.bv) (hya : Fl4Spec.scActive y = true) + (hdet : Detached Z.C Lst w) (hn0 : L.mem s (rNextA w) = 0#u64) (hp0 : L.mem s (rPrevA w) = 0#u64) + (hdD : ∀ q ∈ D, q.bv ≠ d.bv) : + (do + let i2 ← obj.Sc.reply st sc s + let s3 ← if i2 != 0#u64 then obj.Reply.set_next st i2 s w false else ok s + let s4 ← obj.Reply.set_prev st w s3 i2 + let s5 ← obj.Sc.set_reply st sc s4 w + let s6 ← obj.Reply.set_next st w s5 sc true + schedcontext.donate st arI s6 a sc d) ⦃ res => res.2 = a ∧ + (do + Fl4Spec.bindScReply (Z := Unit) sc.bv w.bv + Fl4Spec.schedContextDonate (Z := Unit) sc.bv d.bv) σ = + ({((), pdStS σ t.bv d.bv sc.bv w.bv xt xd xw y n (enqRQ (bdSt σ sc.bv w.bv y n xw) u xu))}, False) ∧ + ∃ Lst', JAtZB (L := L) arI AR Z H Cs Ac Lst' D res.1 a (pdStS σ t.bv d.bv sc.bv w.bv xt xd xw y n (enqRQ (bdSt σ sc.bv w.bv y n xw) u xu)) ∧ + P1.WritesIn st s res.1 (RmW (st := st) Z.T Z.N Z.C Z.R Z.c s σ) ∧ + ScTKw (fun _ => False) σ (pdStS σ t.bv d.bv sc.bv w.bv xt xd xw y n (enqRQ (bdSt σ sc.bv w.bv y n xw) u xu)) ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q0, R0, hc, hs⟩ := hS.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hsc0 : sc ≠ 0#u64 := J.c0 sc hsC + have hscH : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc y n hsc0 hky).2⟩ + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hdH : d ∈ P1.Heap.tcbs H := J.zH.1 d hd + have hwH : w ∈ P1.Heap.replies H := J.tcbs_eq.2 ▸ hw + -- the objects apart + have htd' : t.bv ≠ d.bv := bv_ne htd + have hst' : sc.bv ≠ t.bv := kheap_ne hky hkt (fun _ h1 h2 => by subst h1; cases h2) + have hsd' : sc.bv ≠ d.bv := kheap_ne hky hkd (fun _ h1 h2 => by subst h1; cases h2) + have hws : w.bv ≠ sc.bv := kheap_ne hkw hky (fun _ h1 h2 => by subst h1; cases h2) + have hwt' : w.bv ≠ t.bv := kheap_ne hkw hkt (fun _ h1 h2 => by subst h1; cases h2) + have hwd' : w.bv ≠ d.bv := kheap_ne hkw hkd (fun _ h1 h2 => by subst h1; cases h2) + -- the stack's old head + obtain ⟨⟨y0, n0, hk0, hyL⟩, hLR, -, hLw⟩ := J.call.stack sc hsC + rw [hky] at hk0; cases hk0 + have hh : ∀ h l, y.scReplies = h :: l → h ≠ sc.bv ∧ h ≠ w.bv ∧ ∃ x, σ.kheap h = some (.reply x) := by + intro h l hr + rw [hyL] at hr + cases e : Lst sc with + | nil => rw [e] at hr; cases hr + | cons h' l' => + rw [e] at hr + simp only [List.map_cons, List.cons.injEq] at hr + obtain ⟨rfl, -⟩ := hr + have hh' : h' ∈ Z.R := hLR h' (by rw [e]; exact List.mem_cons_self ..) + obtain ⟨x, hx, -⟩ := J.call.reply h' hh' + refine ⟨fun e' => ?_, fun e' => ?_, x, hx⟩ + · have h2 : σ.kheap sc.bv = some (.reply x) := by rw [← e']; exact hx + rw [hky] at h2; cases h2 + · exact hdet sc hsC (by rw [e, P1.eq_of_bv e']; exact List.mem_cons_self ..) + have hpd := pd_eqS hkt hkd hky hkw hws hh hyt htd' hst' hsd' hwt' hwd' hct.symm hnqt hsw hut hku + (J.act.1 u hsw) J.inv.actReady J.inv.back + have hdn : Fl4Spec.schedContextDonate (Z := Unit) sc.bv d.bv (bdSt σ sc.bv w.bv y n xw) = + ({((), pdStS σ t.bv d.bv sc.bv w.bv xt xd xw y n (enqRQ (bdSt σ sc.bv w.bv y n xw) u xu))}, False) := by + have h := hpd + rw [nd_bind_one (bindScReply_eq hky hkw hws hh)] at h + exact h + -- FL4's words at the start + have bits : ∀ u ∈ Z.T, NotQueued σ u.bv → + (L.mem s (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hc.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s (P1.prioA u))) + rw [hs.rq _ (hc.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hc.released u hu).1 h + apply hnu.2 + rw [hs.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits t ht hnqt + obtain ⟨hqbD, hrbD⟩ := bits d hd hnqd + have hna : ∀ v ∈ Z.T, v.bv ≠ u → L.actionW s ≠ v := fun v hv hne h' => by + have h := hS.act + rw [hsw] at h + obtain ⟨-, -, hw⟩ := h + apply hne + rw [← hw] + unfold P1.LawfulStore.actionW at h'; rw [hc.cur] at h' + exact congrArg _ (show Sched.nodeW st s Z.c layout.node.SCHED_ACTION = v from h').symm + have hiw : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s ≠ u := fun u hu hne h' => by + apply hne + rw [hs.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hc.cur] at h' + exact congrArg _ (show Sched.nodeW st s Z.c layout.node.IDLE_THREAD = u from h').symm + have hitd : t.bv ≠ σ.idleThread := by + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + intro e; rw [← e, hkt] at hkx; cases hkx; rw [hst] at hxs; cases hxs + have hidd : d.bv ≠ σ.idleThread := by + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + intro e; rw [← e, hkd] at hkx; cases hkx; rw [hsd] at hxs; cases hxs + have hsw : L.mem s (P1.scA t) = sc := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb t ht + rw [hkt] at hk'; cases hk' + exact optRef_some_eq (hscw.symm.trans hxt) + have hd0 : L.mem s (P1.scA d) = 0#u64 := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb d hd + rw [hkd] at hk'; cases hk' + exact Notif.optRef_none (by rw [← hscw]; exact hxd) + have hiA : sc ∈ Ac := by + have hrm := SchedRel.act_sc hS hsC hky hya + exact (J.blk.2.2.2.1.act _ hscH).2 (fun h => hrm (by rw [h]; rfl)) + have haff : L.mem s (P1.affA d) = L.mem s (P1.scCoreA sc) := by + rw [hc.aff d hd, Fl4Proofs.P3.Sched.scCoreA_eq, hS.core sc hsC] + have hR0 : ∀ r ∈ Z.R, r ≠ 0#u64 ∧ r.val % 2 = 0 := fun r hr => + ⟨(J.rp.align r hr).2.1, by have := (J.rp.align r hr).1; omega⟩ + have hiso : Isolated (L.mem s) Z.R w := J.call.isolated hR0 hw hdet hn0 hp0 + have hrel : inClassR xd.tcbState xd.tcbFault → Fl4Spec.scActive y = true → released σ y := by + intro hc' _ + exfalso + rw [hsd] at hc' + rcases hc'.cls with h | ⟨_, h⟩ | ⟨_, _, h⟩ | ⟨_, _, _, h⟩ + · simp [Fl4Spec.runnable] at h + all_goals cases h + have hnf : ¬ Free σ w := by + rintro ⟨x, hx, hn⟩; rw [hkw] at hx; cases hx; rw [hwt] at hn; cases hn + obtain ⟨⟨Q1, R1, h1⟩, hB1, hX1, hS1, hSg1⟩ := J.blk + refine WP.spec_mono (WP.spec_and + (push_donate_refinesW s a σ hS hR J.nsc J.inv J.call J.rp hsC hsc0 hky ht hyt hkt hd hkd hxd htd hrel hw hdet + hiso J.rfree hnf) + (P1.ipc.push_don.blk (Es := P1.EH H) hAT s a t d w sc Q1 R1 h1 hB1 hX1 hS1 hSg1 htH hqbT hrbT + (hna t ht (Ne.symm hut)) (hiw t ht hitd) hwH hdH (Ne.symm htd) hqbD hrbD (hna d hd (Ne.symm hud)) (hiw d hd hidd) hsw hsc0 hiA hd0 haff)) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ', hsp, hS', hR', hN', hI', hCR', hRP', hRF', hw0, hwd⟩, hP1⟩ + simp only [WP.uncurry'_pair] at hP1 ha + obtain ⟨hblk, hfoot⟩ := hP1 + subst a' + have e' : σ' = pdStS σ t.bv d.bv sc.bv w.bv xt xd xw y n (enqRQ (bdSt σ sc.bv w.bv y n xw) u xu) := by + have h := hsp.symm.trans hpd + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + subst e' + refine ⟨rfl, hpd, Function.update Lst sc (w :: Lst sc), ?_⟩ + set σ' := pdStS σ t.bv d.bv sc.bv w.bv xt xd xw y n (enqRQ (bdSt σ sc.bv w.bv y n xw) u xu) with hσ' + set σB := bdSt σ sc.bv w.bv y n xw with hσB + set σBS : Fl4Spec.State Unit := { σB with readyQueues := enqRQ σB u xu, schedulerAction := .chooseNewThread } + with hσBS + -- the spec's step and FL4's writes + have hK : ScTKw (fun _ => False) σ σ' := + NKT.of_eq (nkt_bind (nkt_bindScReply _ _) fun _ => nkt_schedContextDonate _ _) hpd + have hW : P1.WritesIn st s s' (fun b => TFoot st Z.T Z.N Z.C Z.c s σ b ∨ CallW' Z.R Z.C b) := + hw0.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl h)) Or.inr + have hX' : XTopAt (L := L) AR Z s' a σ' := + xtop_sc_stepCW (ct := σ.curThread) J.xtop J.ip J.nk J.ks.rep hW (hK.mono fun _ h => h.elim) hS' hR' id + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ' := + tk_liftCW (ct := σ.curThread) J.ip hI hW (ScTKw.ofFalse (P := fun p => p = σ.curThread) hK) hS' hR' + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw0 fun b hb => + hb.elim (fun h => tfoot_oobj hS (Or.inl h)) (fun h => callW'_oobj J.ip hS h) + have hSS := hF.1 + have hks : L.ksb s' = L.ksb s := hF.2 + have hwR : P1.WritesIn st s s' (RmW (st := st) Z.T Z.N Z.C Z.R Z.c s σ) := + J.rmW_of (hw0.mono fun b hb => hb.elim Or.inl (fun h => Or.inr (Or.inl h))) + obtain ⟨k1, k2, kb⟩ := J.rmW_keeps hwR + have hsok : SOK (true, false) σ σ' := + (bd_sok hky hkw hws hh).trans ((pres_schedContextDonate (true, false) _ _).of_eq hdn) + have hyk : YKeep σ σ' := hsok.ykeep rfl + have hmk : McpKeep σ σ' := hsok.mcpKeep + have hcn' : σ'.schedulerAction = .chooseNewThread := rfl + have hFK : FK σ σ' := ⟨pdS_csc, pdS_idle, pdS_isc⟩ + have hIK := J.idleW_keep hS' pdS_idle pdS_isc + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + -- the bound-before objects at `σB` + have hktB : σB.kheap t.bv = some (.tcb xt) := bd_oth hky hkw hws hh (fun _ e => by cases e) (Ne.symm hst') hkt + have hkdB : σB.kheap d.bv = some (.tcb xd) := bd_oth hky hkw hws hh (fun _ e => by cases e) (Ne.symm hsd') hkd + have hkpB : σB.kheap sc.bv = some (.schedContext { y with scReplies := w.bv :: y.scReplies } n) := bd_kp hws + have hpc : sc.bv = σB.curSc := by rw [bd_csc, hcsc] + -- the receivers' replies + have hrr : RRecv (L.mem s') Z.R σ' := by + intro d' hd' hrd + have hrd0 := (KhRB.of_sctkw hK).recvB hrd + obtain ⟨h1', h2'⟩ := J.rrecv d' hd' hrd0 + have hdw : d' ≠ w := by + rintro rfl + obtain ⟨x, t', xt', hx, ht', hxt', hrb⟩ := hrd0 + rw [hkw] at hx; cases hx; rw [hwt] at ht'; cases ht'; rw [hkt] at hxt'; cases hxt' + rw [hst] at hrb; cases hrb + have hdet' := detached_of_next0 J.call (fun r hr => (J.rp.align r hr).2.1) h1' + have hnh : (Lst sc).head? ≠ some d' := fun e => hdet' sc hsC (List.mem_of_mem_head? e) + obtain ⟨e1, e2⟩ := hwd d' hd' hdw hnh + exact ⟨e1.trans h1', e2.trans h2'⟩ + -- the dead + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => by + have hdq := J.jcov.dead q hq + have hqt : q.bv ≠ t.bv := J.notDead ht hkt (by rw [hst]; simp [tsCode]) q hq + have hqd : q.bv ≠ d.bv := hdD q hq + have hqs : q.bv ≠ sc.bv := fun e => J.scOwned_not_dead ht hky hyt (by rw [← P1.eq_of_bv e]; exact hdq) + have hko : σ'.kheap q.bv = σB.kheap q.bv := pdS_ko hqt hqd hqs + refine J.deadP_keep hIR.2.2.1 hIR.1 (fun x0 hx0 _ => ?_) (fun x0 hx0 => ?_) (fun x0 hx0 => ?_) + (fun x0 hx0 => ?_) (fun x0 m hx0 => ?_) (fun hqC => ?_) + (fun z hz hqC K hK8 hK' => J.rmW_scOut hwR hz hqC hK8 hK') pdS_idle + (fun u hu => by rw [hcn'] at hu; cases hu) (J.yld.frame k2 hyk) (hyk.dk q.bv) hdq + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, rfl, rfl, Or.inl rfl⟩ + · rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0 + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, Or.inl rfl, rfl, + Or.inl rfl⟩ + · obtain ⟨x', h1', h2'⟩ := bd_reply hky hkw hws hh hx0 + exact ⟨x', by rw [hko]; exact h1', h2'⟩ + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, Or.inl rfl, Or.inl rfl⟩ + · have hqsc : q ≠ sc := fun e => hqs (by rw [e]) + obtain ⟨-, -, -, hW1⟩ := J.call.stack q hqC + obtain ⟨-, -, -, hW2⟩ := hCR'.stack q hqC + rw [hW2.1, hW1.1, Function.update_of_ne hqsc] + -- the scheduler after + have hown : CurOwnW Z.T Z.C σ' := + dn_own (σ := σBS) hktB hkdB hkpB htd' hst' hsd' ⟨d, hd, rfl⟩ hpc (fun _ => by show d.bv ∉ σB.releaseQueue; rw [bd_rel]; exact hnqd.2) + have hoff : OffOK σ' := dn_off (σ := σBS) hktB hkdB hkpB htd' hst' hsd' hpc (bd_off hky hkw hws hh J.off hcsc) + have hact : ActIdle σ' := + ⟨(fun u hu => by rw [hcn'] at hu; cases hu), + dn_idleOK (σ := σBS) hktB hkdB hkpB htd' hst' hsd' (by show σB.idleThread ≠ t.bv; rw [bd_idle]; exact Ne.symm hitd) + (by show σB.idleThread ≠ d.bv; rw [bd_idle]; exact Ne.symm hidd) (bd_idleOK hky hkw hws hh J.act.2)⟩ + have hbsa : BlockedScActive σ' := + dn_bsa (σ := σBS) hktB hkdB hkpB htd' hst' hsd' (by rw [hsd]; rfl) (let hb := bd_bsa hky hkw hws hh J.bsa hya; ⟨hb.tcb, hb.replies⟩) + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => hK.szKeep q.bv y n hq) hks (J.rmW_irq hwR) hIK.2⟩ + refine ⟨?_, hwR, hK⟩ + exact J.sc_frameS hks hSS (J.vkeep_of hwR fun b hb => J.rmW_nv hb) hK hX' hCR' hRF' hrr (J.yld.frame k2 hyk) (J.mcp.frame k1 hmk) + (J.buf.frame kb hK.bufKeep) hRP' (J.apart.sc_frame hks hK.szKeep) hjpl (J.jcov_keep hks hSS hdead hIK (DXKeep.of_dbg hKd (J.tdkeep_of hwR fun b hb => J.rmW_ntd hb))) + (J.sc_kernW hW hK hFK hX' hDbg (subset_refl D)) + (J.rmW_utc hwR hSS hK.cdt hK.orig pdS_arch hDbg) hN' hI' (Or.inl hcn') hown hoff hact hbsa + (Or.inl (by rw [hcn']; intro h; cases h)) hblk hKd hIK.1 J.zD + +set_option maxHeartbeats 64000000 in +/-- **Donating fault send** at a pending switch. -/ +theorem JAtZB.sendFaultPDJSB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrn : Fl4Spec.runnable xt.tcbState = true) {f : Fl4Spec.Fault} + (hft : xt.tcbFault = some f) (hnt : ∀ b, f ≠ .timeout b) (hct : σ.curThread = t.bv) + {u0 : Fl4Spec.ObjRef} (hu0 : σ.schedulerAction = .switchThread u0) (hu0t : u0 ≠ t.bv) + (hCI : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ dm p, t.bv ∉ σ.readyQueues dm p) + {e d : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.recvEP (d.bv :: q)))) + (blk : Bool) (badge : U64) (g gr : Bool) + (hg : g = true ∨ gr = true) + (hrepn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none) + (hxdn : ∀ xd, σ.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext = none) : + endpoint.send_ipc st arI s a blk false badge g gr true t e ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) blk false badge.bv g gr true t.bv e.bv σ).1, + JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + obtain ⟨hst2, xs, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hdT : d' ∈ Z.T := hQ.mem d' (by simp) + have hda := hP.tcb d' hdT + obtain ⟨xd, hkd, -⟩ := hI.1.tcb d' hdT + have hxd := hR.at_tcb hdT hkd + have hd3 : (L.mem s (tStA 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' = e.bv := by + have := hxd.blk e' (by rw [hstd]; rfl); rw [hdb] at this; exact this.symm + subst he' + obtain ⟨hrep, hrepR⟩ := hxd.recv _ _ _ hstd + have hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by have := runnable_code hrn; omega + have htd : t ≠ d' := by intro h; subst h; exact hnb.1 (hxt.st.symm.trans hd3) + have hde : d'.bv ≠ e.bv := kheap_ne hkd hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have htd' : t.bv ≠ d'.bv := bv_ne htd + have hdc : d'.bv ≠ σ.curThread := by rw [hct]; exact Ne.symm htd' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hdH : d' ∈ P1.Heap.tcbs H := J.zH.1 d' hdT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hdta := hTP d' hdH + have hsub := hSg0.sub + have hds : L.mem s (P1.stA d') = sched.TS_BLOCKED_ON_RECV := by + rw [stA_tStA, tsVals.2.2.1]; exact UScalar.eq_of_val_eq hd3 + have hbe : L.mem s (P1.blockA d') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hdb + have hdq := P1.endpoint.ep_dequeue.inv s d' Q0 R0 h0 hdH (by simp) (Or.inr (Or.inl hds)) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : d' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 d' hdH (by simp) (Or.inr (Or.inl hds)); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e d' (EQ0 e) hTP hE0 hq0' hdm + -- FL4: the endpoint's state and queue + unfold endpoint.send_ipc + refine WP.spec_bind (Fl4Proofs.P1.obj.Ep.state.spec s e) ?_ + rintro _ rfl + rw [epState_eq, hst2] + rw [if_neg (by rw [ep_state_vals.1]; decide), if_neg (by rw [ep_state_vals.2.1]; decide)] + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = d' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := d') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he d' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, c1, nc1⟩, w1⟩ + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) with hσ1 + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (d' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hkd1 : σ1.kheap d'.bv = some (.tcb xd) := by simp [σ1, hde, hkd] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by simp [σ1] + 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 _) + have Fr1 : CFr e.bv d'.bv σ σ1 := CFr.upd hk (Or.inr (Or.inr (Or.inl ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩))) + have IK1 : IK d'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) hk (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) (fun y h => by cases h) (fun _ _ h => by cases h) + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + -- the transfer + have hdth : Z.th d' = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkd + have hUd := J.rbufU hdT hdth hkd + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) ht hdT htd hkt1 hkd1 hft (faultOK_nt (L := L) s1 t xt hnt) e badge g + hTP hdH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) d'.bv (.tcb x2) with hσ2 + have hkd2 : σ2.kheap d'.bv = some (.tcb x2) := by simp [σ2] + have hkt2 : σ2.kheap t.bv = some (.tcb xt) := by simp [σ2, htd', hkt1] + have hst2' : x2.tcbState = .blockedOnReceive e.bv rep dd := by rw [hx2]; exact hstd + have hxsc : x2.tcbSchedContext = xd.tcbSchedContext := by rw [hx2] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 hdT hA2 (fun p hp => by simp [σ2, hp]) + hkd1 hkd2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hk1e2 : σ2.kheap e.bv = some (.endpoint (epDeq (.recvEP (d'.bv :: q)) d'.bv)) := by + simp [σ2, Ne.symm hde, hk1e] + have hq2 : ∀ e'' ∈ Z.E, ∀ z, σ2.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h, hkd2] at hz; cases hz + exact hq1 e'' he'' z (by simpa [σ2, Ne.symm hne] using hz) + have hrw : L.mem s2 (tReplyA d') = L.mem s (tReplyA d') := by + rw [hA2 _ (a_reply hda).1 ⟨?_, ?_⟩, hA1 _ (a_reply hda).1 ⟨d', hdT, (a_reply hda).2⟩] + · rintro ⟨r', hr'⟩ + rw [val_reply hda, regA_val hda (regIdx_lt r')] at hr'; have := regIdx_lt r'; omega + · rintro ⟨wp, hwp, e'⟩ + have h1' := hP.bt wp hwp d' hdT + have h2' := val_reply hda + rw [← e'] at h1'; omega + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xd b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + have Fr2 : CFr e.bv d'.bv σ σ2 := (Fr1.trans (CFr.um _ _ σ1 um2)).trans (CFr.upd (σ := Endp.setUm σ1 um2) hkd1 + (Or.inl ⟨xd, xd.tcbState, xd.tcbBoundNotification, xd.tcbFault, x2.tcbArch, rfl, by rw [hx2], + Or.inr ⟨Or.inl rfl, rfl, Or.inl ⟨_, _, _, hstd⟩, Or.inr (Or.inr rfl)⟩⟩)) + have IK2 : IK d'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans (IK.updSelf (σ := Endp.setUm σ1 um2) hkd1 hxsc)) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((SKeep.um σ1 um2).trans (SKeep.updK (σ := Endp.setUm σ1 um2) (p := d'.bv) + (o := .tcb x2) (fun y h => ⟨xd, hkd1, by cases h; exact hxsc.symm⟩) (fun _ _ h => by cases h))) + have hks2 : L.ksb s2 = L.ksb s := hW2.1 + have Fst2 : L.mem s2 (P1.stA d') = L.mem s (P1.stA d') := by + rw [P1.stA_eq] + exact (f2 d' hdH _ (by simp) (by simp) (by simp)).trans (f1 d' hdH _ (by simp) (by simp) (by simp) (by simp)) + have hds2 : L.mem s2 (P1.stA d') = sched.TS_BLOCKED_ON_RECV := Fst2.trans hds + -- the reply object + obtain ⟨r, hrp⟩ := Option.ne_none_iff_exists'.1 (hrepn xd hkd _ _ _ hstd) + subst hrp + obtain ⟨hw0, hwr⟩ := Notif.optRef_some hrep + have hwR := hrepR (by simp) + refine WP.spec_bind (Endp.obj.Tcb.reply_obj.spec s2 d') ?_ + rintro _ rfl + rw [hrw] + have hne0 : (L.mem s (tReplyA d') != 0#u64) = true := by simp [hw0] + simp only [hne0, ↓reduceIte, Bool.false_eq_true, eq_self_iff_true, if_true] + set w := L.mem s (tReplyA d') with hwdef + subst hwr + obtain ⟨xr, hkr, -⟩ := hR.reply w hwR + have hwe : w.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hwd : w.bv ≠ d'.bv := kheap_ne hkr hkd (fun _ h1 h2 => by subst h1; cases h2) + have hwt : w.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + have hkr2 : σ2.kheap w.bv = some (.reply xr) := by simp [σ2, σ1, hwe, hwd, hkr] + have hb' : xr.replyTcb = some d'.bv := hback_of J.sv hkd e.bv w.bv dd xr (by rw [hstd]) hkr + have hX2' := IpcRel.ofSRN hR2 hS2' + have hrA : L.mem s2 (P1.replyA d') = w := by show L.mem s2 (tReplyA d') = w; rw [hrw] + obtain ⟨hiR, hit⟩ := hX2.2.1.recv d' hdH hds2 (by rw [hrA]; exact hw0) + rw [hrA] at hiR hit + refine WP.spec_bind (WP.spec_and (WP.spec_and (reply_unlink_c hH hP0 hP s2 σ2 hR2 hS2' hdT hkd2 hst2' hwR hkr2 + hb' hq2) (reply_unlink_cn hP0 hX2' hdT hwR hkr2)) + (WP.spec_and (P1.ipc.reply_unlink.inv s2 w d' Q0 R0 h2 hdH hX2.1 hsub hiR hit + (by rw [hds2]; exact P1.blocked_not_runnable (Or.inr (Or.inl rfl))) (Or.inl (Or.inr rfl))) + (P1.ipc.reply_unlink.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s2 w d' h2 hX2 hsub hiR hdH))) ?_ + rintro s3 ⟨⟨⟨e3, hR3, hS3⟩, hW3⟩, ⟨h3, e3', k3, z3⟩, fu3⟩ + have hclr : ∀ r ∈ P1.Heap.replies H, L.mem s2 (P1.rTcbA r) = d' → L.mem s3 (P1.rTcbA r) = 0#u64 := by + intro r hr er + obtain ⟨-, -, b2, -⟩ := hX2.2.1.back r hr (by rw [er]; exact fun e0 => hdta.ne0 (by rw [e0]; rfl)) + rw [er, hrA] at b2; rw [← b2]; exact z3 + set x3 : Fl4Spec.Tcb := { x2 with tcbState := .inactive } with hx3 + set xr3 : Fl4Spec.Reply := { xr with replyTcb := none } with hxr3 + set σ3a := Notif.updK σ2 w.bv (.reply xr3) with hσ3a + have hkd3a : σ3a.kheap d'.bv = some (.tcb (x2)) := by simp [σ3a, Ne.symm hwd, hkd2] + have hct3a : d'.bv ≠ (Notif.updK σ3a d'.bv (.tcb x3)).curThread := hdc + have e3s : Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv = Notif.updK σ3a d'.bv (.tcb x3) := + stsAct_ne hct3a + set σ3 := Notif.stsAct (Notif.updK σ3a d'.bv (.tcb x3)) d'.bv with hσ3 + have hU : Fl4Spec.NonDet.maybeM (Fl4Spec.replyUnlinkTcb (Z := Unit) d'.bv) (some w.bv) σ2 = + ({((), σ3)}, False) := e3 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2 (SzKeepC.of Fr2.szKeep) hb)) + have hkt3 : σ3.kheap t.bv = some (.tcb xt) := by rw [stsAct_kheap]; simp [σ3a, htd', hwt.symm, hkt2] + have hkd3 : σ3.kheap d'.bv = some (.tcb x3) := by rw [stsAct_kheap]; simp + have hkr3 : σ3.kheap w.bv = some (.reply xr3) := by rw [stsAct_kheap]; simp [σ3a, hwd] + have hq3 : ∀ e'' ∈ Z.E, ∀ z, σ3.kheap e''.bv = some (.endpoint z) → d'.bv ∉ z.epQueue := by + intro e'' he'' z hz + rw [stsAct_kheap] at hz + have hne : d'.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp at hz + have hne' : w.bv ≠ e''.bv := by intro h; rw [← h] at hz; simp [σ3a, hwd] at hz + exact hq2 e'' he'' z (by simpa [σ3a, Ne.symm hne, Ne.symm hne'] using hz) + have Fr3 : CFr e.bv d'.bv σ σ3 := (Fr2.trans (CFr.upd hkr2 (Or.inr (Or.inr (Or.inr ⟨xr, rfl, rfl, hb'⟩))))).trans + (CFr.stsIn hkd3a (Or.inl ⟨_, _, _, hst2'⟩)) + have IK3 : IK d'.bv σ σ3 := by + have IK3a : IK d'.bv σ σ3a := IK2.trans (IK.updOther (o' := .reply xr3) hkr2 (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩)) + have IK3b : IK d'.bv σ (Notif.updK σ3a d'.bv (.tcb x3)) := IK3a.trans (IK.updSelf (y := x3) hkd3a rfl) + exact IK3b.trans (IK.sts _ _ _) + have SK3 : Ipc.SKeep σ σ3 := + ((SK2.trans (SKeep.updK (p := w.bv) (o := .reply xr3) (fun y h => by cases h) (fun _ _ h => by cases h))).trans + (SKeep.updK (p := d'.bv) (o := .tcb x3) (fun y h => ⟨_, hkd3a, by cases h; rfl⟩) (fun _ _ h => by cases h))).trans + (SKeep.stsAct _ _) + have hct3 : σ3.curThread = σ.curThread := by rw [e3s]; rfl + have hact3 : σ3.schedulerAction = σ.schedulerAction := by rw [e3s]; rfl + have hsc3 : x3.tcbSchedContext = xd.tcbSchedContext := by simp [x3, hxsc] + have hB3 := hB2.keepT h2 k3 (by rw [e3']; exact P1.not_blocked_inactive) (by rw [e3']; exact P1.inactive_ne.1) + have hX3 := hX2.keepT k3 (by rw [e3']; exact P1.inactive_ne) hclr + have hS3' := hS2''.keepT hX2 k3 + have hSg3 : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s3 := + ⟨hsub, fun b hb => by rw [k3.ksb]; exact hSg2.ks b hb, fun o h1' h2' => by rw [k3.ksb]; exact hSg2.ksΩ o h1' h2'⟩ + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ d' := + hX3.noref hdta (by rw [e3']; exact P1.inactive_ne.1) (by rw [e3']; exact P1.inactive_ne.2) + have F3 := footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3) + have hxt3 := hR3.at_tcb ht hkt3 + have hty3 : L.mem s3 (tFaultA t) ≠ fl4_abi.consts.fault.NullFault := fun e0 => by + have h00 := hxt3.fault0.1 (by rw [e0]; unfold fl4_abi.consts.fault.NullFault; rfl) + rw [hft] at h00; cases h00 + have hks3 : L.ksb s3 = L.ksb s := hW3'.1 + -- the caller's and the receiver's words at `s3` + have hsr : σ.schedulerAction ≠ .resumeCurThread := by rw [hu0]; intro h; cases h + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hw3 : (L.mem s3 (P1.stA t)).val = tsCode xt.tcbState := by + rw [P1.stA_eq, ← tStA_eq, hxt3.st] + obtain ⟨hnbt3, hnrp3⟩ := P1.run_word (w := L.mem s3 (P1.stA t)) (by rw [hw3]; exact runnable_code hrn) + have hX3' := IpcRel.ofSRN hR3 hS3 + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hX3'.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have hSM := ScRelM.of hX3'.1 + have hnqD : NotQueued σ d'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hkd] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqT3 : NotQueued σ3 t.bv := ⟨by rw [IK3.2.1]; exact hnq, by rw [IK3.2.2.1]; exact hnrT⟩ + have hnqD3 : NotQueued σ3 d'.bv := ⟨by rw [IK3.2.1]; exact hnqD.1, by rw [IK3.2.2.1]; exact hnqD.2⟩ + have bits3 : ∀ u ∈ Z.T, NotQueued σ3 u.bv → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hnu.2 + rw [hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT3 + obtain ⟨hqbD, hrbD⟩ := bits3 d' hdT hnqD3 + have hna3 : ∀ u ∈ Z.T, u.bv ≠ u0 → L.actionW s3 ≠ u := fun u hu hne h' => by + have h := hSR3.act + rw [hact3, hu0] at h + obtain ⟨-, -, hw⟩ := h + apply hne + rw [← hw] + unfold P1.LawfulStore.actionW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.SCHED_ACTION = u from h').symm + have hdu0 : d'.bv ≠ u0 := by + intro e0 + obtain ⟨xu, -, -, -, hku, -, hru, -⟩ := schedulable_inv (J.act.1 u0 hu0) + rw [← e0, hkd] at hku; cases hku; simp [hstd, Fl4Spec.runnable] at hru + have hcw3 : L.curW s3 = t := by + have h : (Sched.nodeW st s3 Z.c layout.node.CUR_THREAD).bv = t.bv := by rw [← hs3.curThread, hct3, hct] + unfold P1.LawfulStore.curW; rw [hcq3.cur]; exact P1.eq_of_bv h + have hidl3 : σ3.idleThread = σ.idleThread := Fr3.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show Sched.nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hti + have hid : d'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hkd] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA t) ≠ 0#u64 → L.mem s3 (P1.scA t) ∈ Ac := fun h0 => by + obtain ⟨Q', R'', hcq0, hs0⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨e1, hA⟩ := J.curScAcB hCI hti + have hcw : L.curW s = t := by + apply P1.eq_of_bv + rw [← hct, hs0.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq0.cur] + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + have h1 : L.mem s3 (P1.scA t) = L.mem s (P1.scA t) := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs0.tcb t ht + obtain ⟨x'', hk'', -, -, hscw', -⟩ := hs3.tcb t ht + rw [hkt] at hk'; cases hk' + rw [hkt3] at hk''; cases hk'' + have e2 := hscw.symm.trans hcs + have hv0 : L.mem s (P1.scA t) ≠ 0#u64 := fun e => by rw [e, Notif.optRef_zero] at e2; cases e2 + exact optRef_some_eq ((hscw'.symm.trans hcs).trans (e2.symm.trans (Notif.optRef_ne hv0))) + rw [h1, ← hcw, e1]; exact hA + have hwH : w ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : w ≠ 0#u64 := (J.rp.align w hwR).2.1 + have hqt3 := Endp.not_queued hR3 ht hkt3 hnb + -- the caller's SC, donated + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc + obtain ⟨y, n, hky0⟩ := hCI.sc + have hya : Fl4Spec.scActive y = true := hCI.active (J.curSc_ne_idle hCI hti) y n hky0 + obtain ⟨sc, hsC, esc⟩ := J.obj.sc _ y n hky0 + rw [← esc] at hky0 hcs + obtain ⟨y1, n1, hky1, hyt⟩ := J.inv.back _ _ _ hkt hcs + rw [hky0] at hky1; cases hky1 + have hrelσ : Fl4Spec.scActive y = true → released σ y := fun ha => + J.inv.rd t.bv xt hkt (Or.inl hrn) xt sc.bv y n hkt hcs hky0 ha hnrT + have hse : sc.bv ≠ e.bv := kheap_ne hky0 hk (fun _ h1 h2 => by subst h1; cases h2) + have hsd : sc.bv ≠ d'.bv := kheap_ne hky0 hkd (fun _ h1 h2 => by subst h1; cases h2) + have hsw : sc.bv ≠ w.bv := kheap_ne hky0 hkr (fun _ h1 h2 => by subst h1; cases h2) + have hst : sc.bv ≠ t.bv := kheap_ne hky0 hkt (fun _ h1 h2 => by subst h1; cases h2) + have hky2 : σ2.kheap sc.bv = some (.schedContext y n) := by simp [σ2, σ1, hsd, hse, hky0] + have hky3 : σ3.kheap sc.bv = some (.schedContext y n) := by rw [stsAct_kheap]; simp [σ3a, hsd, hsw, hky2] + have hsw3 : L.mem s3 (P1.scA t) = sc := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs3.tcb t ht + rw [hkt3] at hk'; cases hk' + exact optRef_some_eq (hscw.symm.trans hcs) + have hxdn3 : x3.tcbSchedContext = none := by rw [hsc3]; exact hxdn xd hkd + -- the J state after the block + have hcD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by have := runnable_code hrn; omega) + have htD := J.not_dead_of_st hdT hkd (by rw [hstd]; simp [tsCode]) + have hwD : ∀ q0 ∈ D, q0.bv ≠ w.bv := fun q0 hq0 e0 => by + have hqw : q0 = w := P1.eq_of_bv e0 + rw [hqw] at hq0 + have d1 : L.mem s (Endp.rTcbA w) = 0#u64 := (J.jcov.dead w hq0).2.2.2.1 hwH + obtain ⟨xr', hkr', hxr'⟩ := hR.reply w hwR + rw [hkr] at hkr'; cases hkr' + rw [hb', d1, optRef_zero] at hxr' + cases hxr' + have hRR : ∀ q' ∈ Z.R, ∀ xr, σ.kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem s (rNextA q') = 0#u64 ∧ L.mem s (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => J.recvReplyB q' hq'' xr d'.bv xd hx' hxt' hkd + (by rw [hstd]; rfl) + have hrib3 : RelIfBound σ3 d'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd d'.bv xd hkd (Or.inr (Or.inr (Or.inr ⟨_, _, _, hstd⟩)))) SK3 + have I3 : Inv σ3 := ((J.inv.toBut d'.bv).ik hnqD IK3).1.inv fun _ _ _ => hrib3 + have hscA3 : ∀ p y n, σ3.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + have hp0 : σ.kheap p = some (.schedContext y n) := SK3.2.2.2 _ _ _ hp + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp0 + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp0).2⟩ + have hS := J.xtop.fin.1.1.1 + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp0 hyt + rw [hkt] at hku; cases hku + have e1 : σ.curSc = q.bv := by rw [← esc]; exact Option.some.inj (hcs.symm.trans hxs) + rw [← e1] at hp0 + exact hCI.active (J.curSc_ne_idle hCI hti) y n hp0 + set xtP : Fl4Spec.Tcb := { xt with tcbState := .blockedOnReply w.bv } with hxtP + set xwP : Fl4Spec.Reply := { xr3 with replyTcb := some t.bv } with hxwP + set σP := pushSt σ3 t.bv w.bv xr3 xt with hσP + have hsr' : Fl4Spec.SchedulerAction.switchThread u0 ≠ .resumeCurThread := fun h => by cases h + have hswP : σP.schedulerAction = .switchThread u0 := + (ps_actN (by rw [hact3]; exact hsr)).trans (hact3.trans hu0) + have I4P : Inv σP := ps_inv hkt3 hkr3 hwt I3 hnqT3.1 hnqT3.2 + have hCI3 : CurInv σ3 := Fr3.curInv hCI + have hsP : Fl4Spec.schedulable u0 σP = true := ps_sched hkt3 hkr3 hwt (Fr3.sched (J.act.1 u0 hu0)) hu0t + obtain ⟨xu0, -, -, -, hku0, -⟩ := schedulable_inv hsP + set σD := pdStS σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n (enqRQ (bdSt σP sc.bv w.bv y n xwP) u0 xu0) with hσD + have hktP : σP.kheap t.bv = some (.tcb xtP) := ps_kt + have hkwP : σP.kheap w.bv = some (.reply xwP) := ps_kw hwt + have hkdP : σP.kheap d'.bv = some (.tcb x3) := by rw [ps_ko (Ne.symm htd') (Ne.symm hwd)]; exact hkd3 + have hkyP : σP.kheap sc.bv = some (.schedContext y n) := by rw [ps_ko hst hsw]; exact hky3 + have hctP : σP.curThread = t.bv := + (ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).ct.trans (hct3.trans hct) + have hcscP : σP.curSc = sc.bv := + (ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).csc.trans (Fr3.sk.csc.trans esc.symm) + have hnqtP : NotQueued σP t.bv := ⟨by rw [ps_rq]; exact hnqT3.1, by rw [ps_rel]; exact hnqT3.2⟩ + have hnqdP : NotQueued σP d'.bv := ⟨by rw [ps_rq]; exact hnqD3.1, by rw [ps_rel]; exact hnqD3.2⟩ + have hhP := J.stackHead hsC hky0 (hRR w hwR xr hkr hb').1 + have hhP' : ∀ h l, y.scReplies = h :: l → h ≠ sc.bv ∧ h ≠ w.bv ∧ ∃ x, σP.kheap h = some (.reply x) := by + intro h l hr + obtain ⟨h1, h2, x, hx⟩ := hhP h l hr + refine ⟨h1, h2, x, ?_⟩ + have ht' : h ≠ t.bv := fun e0 => by rw [e0, hkt] at hx; cases hx + have hd' : h ≠ d'.bv := fun e0 => by rw [e0, hkd] at hx; cases hx + have he' : h ≠ e.bv := fun e0 => by rw [e0, hk] at hx; cases hx + rw [ps_ko ht' h2, stsAct_kheap] + simp [σ3a, hd', h2, σ2, σ1, he', hx] + have hpdE := pd_eqS hktP hkdP hkyP hkwP (Ne.symm hsw) hhP' hyt htd' hst hsd hwt hwd + hctP.symm hnqtP hswP hu0t hku0 hsP I4P.actReady I4P.back + have hpushE : Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σD)}, False) := by + rw [replyPush_don_eq hkt3 hkd3 hkr3 hwt hcs hxdn3]; exact hpdE + have hns3 : tsCode xt.tcbState ≠ 6 := by have := runnable_code hrn; omega + -- the push: link and block (`J` again), then the donation at `J` + have hPush : ipc.reply_push st arI s3 a t d' w true ⦃ r => r.2 = a ∧ ∃ σ', + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σ')}, False) ∧ + (σ' = σD → ∃ (Lst' : U64 → List U64), JAtZB (L := L) arI AR Z H Cs Ac Lst' D r.1 a σ' ∧ + NatFoot (L := L) Z s σ r.1 σ') ⦄ := by + refine reply_push_iC (don := true) (P := fun s' σ' => σ' = σD → ∃ (Lst' : U64 → List U64), + JAtZB (L := L) arI AR Z H Cs Ac Lst' D s' a σ' ∧ NatFoot (L := L) Z s σ s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 ht hkt3 hkd3 hqt3 hns3 hwR hkr3 (fun _ => hdT) (fun s' _ _ e0 => ?_) ?_ + · exfalso + have h1 : σD.kheap t.bv = some (.tcb xtP) := by rw [← e0]; exact hktP + have h2 : σD.kheap t.bv = some (.tcb { xtP with tcbSchedContext := none }) := + @pdS_kt σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _ + htd' hst + rw [h2] at h1 + have h3 : ({ xtP with tcbSchedContext := none } : Fl4Spec.Tcb) = xtP := by + injection h1 with h1; injection h1 + have h4 : (none : Option Fl4Spec.ObjRef) = some sc.bv := (congrArg Fl4Spec.Tcb.tcbSchedContext h3).trans hcs + cases h4 + · intro _ s4 hsb hR4 hS4 _ _ _ _ + rw [hsw3] + have hsb' : sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA w) t) t w = ok s4 := hsb + -- FL4's words and P1 at the block + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA w) t) (fun b => b.val = w.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 t + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 ht hwR hw1 + rw [hsb', WP.spec_ok] at h; exact h + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xd b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3 (SzKeepC.of Fr3.szKeep) hb)) + have hb4 := P1.sched.set_blocked_on_reply.ipc s3 t w Q0 R0 h3 (fun y hy => by + obtain ⟨hy1, hy2⟩ := hy + rcases hy1 with hy1 | hy1 + · exact absurd hy1 (Set.notMem_empty _) + · exact absurd hy1 hy2) (Or.inr hnbt3) hB3 hX3 hS3' hSg3 htH hqbT hrbT + (hna3 t ht (fun e0 => hu0t e0.symm)) (hiw3 t ht hit) ⟨hnrp3, fun e0 => hnbt3 (Or.inr (Or.inl e0))⟩ hwH hw00 z3 hcA + rw [hsb', WP.spec_ok] at hb4 + obtain ⟨h4, hB4, hX4, hS4', hSg4, -, -, -, -, -, -⟩ := hb4 + have hO3 := hX3.1 + obtain ⟨hrΩ3, -⟩ := P1.objs_omega hO3 hSg3.sub + have f1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) + s3 (L.wr s3 (P1.rTcbA w) t) := + P1.foot_wr (h3.omega _ (hrΩ3 w hwH).1).1 (P1.kw_sg (hO3.tcbW hwH)) + have q1 := (P1.omega_wr (v := t) h3 (hrΩ3 w hwH).1).1 + have f4 := P1.sched.set_blocked_on_reply.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) (Es := P1.EH H) _ t w q1 htH + rw [hsb', WP.spec_ok] at f4 + have hfoot4 : P1.WritesIn st s s4 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s b ∨ RcvBufW xd b) := + footW_trans (footW_trans F3 (footW_of f1)) (footW_of f4) + have hIR4 := IpcRel.ofSRN hR4 hS4 + obtain ⟨su, umu, Ju, hWu, hFu, -, -, Fu⟩ := J.bufRebaseX hdT hdth hkd hW4' hfoot4 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have hRRu : ∀ q' ∈ Z.R, ∀ xr, (Endp.setUm σ umu).kheap q'.bv = some (.reply xr) → xr.replyTcb = some d'.bv → + Detached Z.C Lst q' ∧ L.mem su (rNextA q') = 0#u64 ∧ L.mem su (rPrevA q') = 0#u64 := + fun q' hq'' xr hx' hxt' => Ju.recvReplyB q' hq'' xr d'.bv xd hx' + hxt' hkd (by rw [hstd]; rfl) + have hF4 : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hblk4 : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s4 := ⟨⟨Q0, R0, h4⟩, hB4, hX4, hS4', hSg4⟩ + have I4 : Inv σP := ps_inv hkt3 hkr3 hwt I3 hnqT3.1 hnqT3.2 + have B4 : BlockedScActive σP := ps_bsa hkt3 hkr3 hwt hscA3 (Fr3.bsa J.bsa) + have hidT3 : σ3.idleThread ≠ t.bv := by rw [Fr3.sk.idle, ← hct]; exact fun e0 => hti e0.symm + have hAI4 : ActIdle σP := + ⟨fun u hu => by rw [hswP] at hu; cases hu; exact hsP, ps_idleOK hkt3 hkr3 hwt hidT3 (Fr3.idleOK J.act.2)⟩ + have hkq : ∀ q ∈ D, σP.kheap q.bv = σ3.kheap q.bv := fun q hq => ps_ko (hcD q hq) (hwD q hq) + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σP.kheap q = some o ↔ σ3.kheap q = some o) := fun h1 h2 => ps_oth hkt3 hkr3 hwt h1 h2 + have J4 := Ju.wake_postGR hIR4 hF4 hWu hblk4 hFu (DWFr.ofUm (DWFr.ofCFr Fr3) umu) ps_skD + (ps_ksub hkt3 hkr3 hwt) (ps_szKeep hkt3 hkr3 hwt) (ps_ykeep hkt3 hkr3 hwt) (ps_mcpKeep hkt3 hkr3 hwt) + (ps_bufKeep hkt3 hkr3 hwt) (fun d _ h => ps_recvB hkt3 hkr3 hwt h) (ps_ksz hkt3 hkr3 hwt) + (ps_kback hkt3 hkr3 hwt) (ps_ipcFr hkt3 hkr3 hwt) (ps_ck hkt3 hkr3 hwt) (fun d _ h => ps_free hkt3 hkr3 hwt h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I4 B4 hAI4 (Or.inr (ps_curInv hkt3 hkr3 hwt hCI3 (by rw [hct3, hct]))) (Or.inl (by rw [hswP]; exact hsr')) + (ps_own hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).own ⟨d', hdT, rfl⟩ J.own)) + (ps_off hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).off J.off)) (ps_nsc hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).nsc J.nsc)) + (ps_isorted hkt3 hkr3 hwt ((DWFr.ofCFr Fr3).isorted J.isorted)) hRRu htD + have N4 := NatFoot.ofW Ju.xtop hWu (fun b hb => CnW.oobj Ju.xtop.fin.1.1.1 hb) (fun b hb => Ju.cnW_nv hb) + (fun b hb => Ju.cnW_nh hb) + ((natK_dwfr (DWFr.ofUm (DWFr.ofCFr Fr3) umu)).trans (natK_pushSt hkr3 hkt3 (fun e0 => hwt e0.symm))) + -- the reply's links at the block + obtain ⟨hdet, hn0, hp0⟩ := hRRu w hwR xr hkr hb' + obtain ⟨-, -, -, -, k4⟩ := Ju.cnW_keeps hWu + refine WP.spec_mono (J4.pushDonJSB hAT ht hdT hwR hsC htd hktP hkdP hkwP hkyP rfl hcs hyt hxdn3 rfl rfl + hctP hcscP hswP hu0t (Ne.symm hdu0) hku0 hnqtP hnqdP hya hdet (by rw [(k4 w hwR).2]; exact hn0) (by rw [(k4 w hwR).1]; exact hp0) + htD) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, hsp5, Lst5, J5, hW5r, hK5⟩ + simp only at ha5 + subst a5 + have N5 := NatFoot.ofW J4.xtop hW5r (fun b hb => RmW.oobj J4.xtop.fin.1.1.1 hb) (fun b hb => J4.rmW_nv hb) + (fun b hb => J4.rmW_nh hb) (natK_tk hK5) + exact ⟨rfl, _, hsp5, fun _ => ⟨Lst5, J5, J.natTrans Fu (Ju.natTrans N4 N5)⟩⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => r.2 = a ∧ ∃ σ', + Fl4Spec.replyPush (Z := Unit) t.bv d'.bv w.bv true σ3 = ({((), σ')}, False) ∧ + (σ' = σD → ∃ (Lst' : U64 → List U64), JAtZB (L := L) arI AR Z H Cs Ac Lst' D r.1 a σ' ∧ + NatFoot (L := L) Z s σ r.1 σ')) + ?_ ?_ + · refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 t) ?_ + rintro i1 rfl + rw [if_pos (bne_iff_ne.2 hty3)] + rcases hg with rfl | rfl + · simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true]; exact hPush + · cases g <;> simp only [Bool.false_eq_true, ↓reduceIte, eq_self_iff_true, if_true] <;> exact hPush + rintro ⟨s5, a5⟩ ⟨ha5, σ5', hpush, hPσ⟩ + simp only at ha5 hpush hPσ ⊢ + subst a5 + try dsimp only + have e5 : σ5' = σD := by + have h := hpush.symm.trans hpushE + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + obtain ⟨Lst5, J5, F5⟩ := hPσ e5 + subst e5 + -- the receiver runs (`Inactive` with the donated SC) + set x4 : Fl4Spec.Tcb := { x3 with tcbSchedContext := some sc.bv } with hx4 + have hk5 : σD.kheap d'.bv = some (.tcb x4) := @pdS_kd σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _ + have hst5 : L.mem s5 (P1.stA d') = sched.TS_INACTIVE := by + have h := ((J5.xtop.irelK J5.ks J5.buf).1.toN.at_tcb hdT hk5).st + rw [stA_tStA, P1.ts_inactive] + exact UScalar.eq_of_val_eq (by rw [h]; rfl) + have htmD : σD.curTime = σ.curTime := (@pdS_tm σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _).trans + ((ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).tm.trans SK3.2.1) + have hkpD : σD.kheap sc.bv = some (.schedContext { { y with scReplies := w.bv :: y.scReplies } with + scTcb := some d'.bv } n) := + @pdS_kp σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _ hsd + have hrelD : RelIfBound σD d'.bv := by + intro x' scp y' n' hx' hsc' hky' ha' _ + rw [hk5] at hx'; cases hx' + simp only [x4, Option.some.injEq] at hsc' + subst hsc' + rw [hkpD] at hky' + cases hky' + have := hrelσ hya + unfold released at this ⊢ + rw [htmD]; exact this + refine WP.spec_bind (J5.stsIJ hdT (fun h => htD d' h rfl) hk5 rfl .running (by simp [tsCode]) sched.TS_RUNNING + (by unfold sched.TS_RUNNING; rfl) (fun _ => hrelD) + (P1.sched.set_thread_state.running_blkI (Es := P1.EH H) s5 d' J5.blk hdH hst5)) ?_ + rintro s6 ⟨Lst6, J6, hW6⟩ + have N6 : NatFoot (L := L) Z s5 σD s6 (stsAct (updK σD d'.bv (.tcb { x4 with tcbState := .running })) d'.bv) := + NatFoot.ofT J5.xtop hW6 (fun b hb => J5.tfoot_nh hb) + (natK_tcbW (y := { x4 with tcbState := .running }) hk5 (Notif.stsAct_kheap _ _) fun h => by simp at h) + set x6 : Fl4Spec.Tcb := { x4 with tcbState := .running } with hx6 + have hdcD : d'.bv ≠ σD.curThread := by + rw [show σD.curThread = t.bv from (@pdS_ct σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _).trans hctP] + exact Ne.symm htd' + have e6 : stsAct (updK σD d'.bv (.tcb x6)) d'.bv = updK σD d'.bv (.tcb x6) := stsAct_ne hdcD + have hk6 : (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).kheap d'.bv = some (.tcb x6) := by + rw [e6]; simp [updK] + have hnr6 : d'.bv ∉ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).releaseQueue := by + rw [e6] + show d'.bv ∉ σD.releaseQueue + rw [show σD.releaseQueue = σ.releaseQueue from (@pdS_rel σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _).trans + ((@ps_rel σ3 t.bv w.bv xr3 xt).trans SK3.1)] + exact hnqD.2 + have hact6 : ∀ p y' n', x6.tcbSchedContext = some p → + (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).kheap p = some (.schedContext y' n') → Fl4Spec.scActive y' = true := by + intro p y' n' hp hy' + simp only [x6, x4, Option.some.injEq] at hp + subst hp + rw [e6] at hy' + simp only [updK_kheap, Function.update_of_ne hsd] at hy' + rw [hkpD] at hy' + cases hy' + exact hya + have hti6 : d'.bv ≠ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).idleThread := by + rw [e6] + show d'.bv ≠ σD.idleThread + rw [show σD.idleThread = σ.idleThread from (@pdS_idle σP t.bv d'.bv sc.bv w.bv xtP x3 xwP y n _).trans + ((ps_skD (σ := σ3) (t := t.bv) (w := w.bv) (xr := xr3) (xt := xt)).idle.trans Fr3.sk.idle)] + exact hid + have htc6 : d'.bv ≠ (stsAct (updK σD d'.bv (.tcb x6)) d'.bv).curThread := by + rw [e6]; exact hdcD + have hspec := sendIpc_faultPush_eq (don := true) hk hkd hde hstd blk badge.bv g gr hg e2 hU hkt3 hkd3 hpushE hk5 (by rw [hft]; simp) + (t := t.bv) + refine WP.spec_mono (J6.sendTailJ hdT hk6 rfl hnr6 hact6 hti6 htc6) ?_ + rintro ⟨s7, a7⟩ ⟨ha7, σ7, h7, Lst7, J7, F7⟩ + simp only at ha7 + subst ha7 + exact ⟨((), σ7), by rw [hspec, h7]; exact Set.mem_singleton _, + ⟨Z.vs, _, _, _, J7, J.natTrans F5 (J5.natTrans N6 F7)⟩⟩ + +set_option maxHeartbeats 64000000 in +/-- **Waiting handler receiver** at a pending switch. -/ +theorem JAtZB.faultRecvSB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) + {u0 : Fl4Spec.ObjRef} (hu0 : σ.schedulerAction = .switchThread u0) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hu0t : u0 ≠ ct.bv) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ptr bdg : U64} {sd rv g gr : Bool} + (hep : capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok (.Endpoint ptr bdg sd rv g gr)) + {d : U64} {q : List Fl4Spec.ObjRef} (hkq : σ.kheap ptr.bv = some (.endpoint (.recvEP (d.bv :: q)))) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x (by rw [hcur]; exact hk) + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hcq.cur] at h; exact h + have ht : ct ∈ Z.T := by rw [hct']; exact hcq.curT + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hti : σ.curThread ≠ σ.idleThread := hF.ni + -- the handler cap, its endpoint + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have e1 : c1 = .Endpoint ptr bdg sd rv g gr := by rw [hep] at hc1; simpa using hc1.symm + subst e1 + have habs := hcr.2 + simp only [absCap] at habs + have hfh : x.tcbFaultHandler = .endpointCap ptr.bv bdg.bv (rightsOf sd rv g gr) := habs.symm + have hAS : AbsSlot Z.th (ct.bv, Fl4Spec.tcbCnodeIndex 3) := ⟨ct, rfl, Or.inr ⟨htcb, 3, by decide, rfl⟩⟩ + have hpE : ptr ∈ Z.E := J.slotE hAS hep + have hpt : ptr.bv ≠ ct.bv := fun e => by rw [e, hk] at hkq; cases hkq + have hcbP := (J.midBlkFactsS ht hcur hF ⟨u0, hu0⟩ hk).2.2 + have hdon := J.faultDonWord ht hk + have hCI : CurInv σ := J.curInv (by rw [hu0]; intro e; cases e) + refine handleFault_ep_wp hep ?_ + unfold faults.send_fault_ipc + dsimp only + -- `set_fault` + set x2 : Fl4Spec.Tcb := { x with tcbFault := some f' } with hx2 + set σf := updK σ ct.bv (.tcb x2) with hσf + -- `set_fault` at `J` + refine WP.spec_bind (J.setFaultJG ht hk (fun e0 d0 h => by + have := runnable_code hrn; rw [h] at this; simp [tsCode] at this) hcbP hf) ?_ + rintro s7 ⟨J7, -, hwF⟩ + have F7 : NatFoot (L := L) Z s σ s7 (updK σ ct.bv (.tcb x2)) := + NatFoot.ofW J.xtop hwF (fun b hb => Ipc.FinW.oobj hS hb) (fun b hb => Ipc.FinW.nv hS hb) (fun b hb => J.finW_nh hb) + (natK_tcbW hk rfl fun h => h) + have hkf : σf.kheap ct.bv = some (.tcb x2) := by simp [hσf, updK] + have hkfp : ∀ q, q ≠ ct.bv → σf.kheap q = σ.kheap q := by + intro q hq'; rw [hσf]; simp only [updK, Function.update_of_ne hq'] + have hkef : σf.kheap ptr.bv = some (.endpoint (.recvEP (d.bv :: q))) := by rw [hkfp _ hpt]; exact hkq + have hnb0 : tsCode x.tcbState ≠ 3 ∧ tsCode x.tcbState ≠ 4 := by + have := runnable_code hrn; omega + have htq : ct.bv ∉ (Fl4Spec.Endpoint.recvEP (d.bv :: q)).epQueue := + Endp.not_queued hI.1.toE ht hk hnb0 ptr hpE _ hkq + have hdc : d.bv ≠ ct.bv := fun e0 => htq (by rw [← e0]; simp [Fl4Spec.Endpoint.epQueue]) + have hCI7 : CurInv σf := curInv_setFault hCI hk (some f') + have hpost : ∀ don : Bool, (don = true → x.tcbSchedContext ≠ none) → endpoint.send_ipc st arI s7 a true false bdg g gr don ct ptr ⦃ r => + ∃ z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr don ct.bv ptr.bv σf).1, + JAtN (L := L) arI AR Z H Cs s7 σf r.1 r.2 z.2 ⦄ := by + intro don hdn + by_cases hgg : g = true ∨ gr = true + · by_cases hA : ∀ xd, σf.kheap d.bv = some (.tcb xd) → ∀ e' rep dd, + xd.tcbState = .blockedOnReceive e' rep dd → rep ≠ none + · by_cases hS : don = false ∨ ∀ xd, σf.kheap d.bv = some (.tcb xd) → xd.tcbSchedContext ≠ none + · exact J7.sendFaultPJSB hAT ht hkf hrn rfl hf.nt hcur hu0 hu0t hCI7 hti hnq' hpE hkef true bdg g gr don hgg hA hS + · have hd1 : don = true := by + cases don + · exact absurd (Or.inl rfl) hS + · rfl + subst hd1 + refine J7.sendFaultPDJSB hAT ht hkf hrn rfl hf.nt hcur hu0 hu0t hCI7 hti hnq' hpE hkef true bdg g gr hgg hA ?_ + intro xd hxd + by_contra hne + exact hS (Or.inr fun xd' hxd' => by rw [hxd] at hxd'; cases hxd'; exact hne) + · refine J7.sendFaultIJSB ht hkf hrn rfl hf.nt hcur hu0 hu0t hCI7 hti hnq' hpE hkef true bdg g gr don (Or.inr ?_) + intro xd hxd e' rep dd hst + by_contra hne + apply hA + intro xd' hxd' e'' rep' dd' hst' + rw [hxd] at hxd'; cases hxd' + rw [hst] at hst'; cases hst'; exact hne + · exact J7.sendFaultIJSB ht hkf hrn rfl hf.nt hcur hu0 hu0t hCI7 hti hnq' hpE hkef true bdg g gr don + (Or.inl ⟨Bool.eq_false_iff.2 fun h => hgg (Or.inl h), Bool.eq_false_iff.2 fun h => hgg (Or.inr h)⟩) + refine WP.spec_bind (hpost _ (fun h => by rw [hdon] at h; exact of_decide_eq_true h)) ?_ + rintro ⟨s', a'⟩ ⟨z, hz, hJ⟩ + apply (WP.spec_ok _).2 + refine ⟨rfl, ?_⟩ + have hz' : z ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv + (decide (Fl4Spec.Rights.allowGrant ∈ rightsOf sd rv g gr)) + (decide (Fl4Spec.Rights.allowGrantReply ∈ rightsOf sd rv g gr)) (decide (x.tcbSchedContext ≠ none)) ct.bv + ptr.bv (updK σ ct.bv (.tcb { x with tcbFault := some f' }))).1 := by + rw [rights_grant, rights_grantReply, ← hdon]; exact hz + obtain ⟨V', Ac', Lst', D', J', F'⟩ := hJ + exact ⟨((), z.2), handleFault_ep_mem hk hfh _ hz', + ⟨_, _, J'.xtop.rel.1, J'.xtop.rel.2.1, J'.xtop.rel.2.2.1, J'.xtop.rel.2.2.2.1, J'.xtop.rel.2.2.2.2.1⟩, + ⟨V', Ac', Lst', D', J', J.natTrans F7 F'⟩⟩ + +set_option maxHeartbeats 4000000 in +/-- **`handle_fault` at a pending switch**: a waiting handler receiver ends in `JAtN`, everything else in `JAtNR`. -/ +theorem JAtZB.faultHypSD (hAT : P0.ArchTotal arI) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + {u0 : Fl4Spec.ObjRef} (hu0 : σ.schedulerAction = .switchThread u0) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hu0t : u0 ≠ ct.bv) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ + (JAtN (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ∨ JAtNR (L := L) arI AR Z H Cs s σ r.1 r.2 z.2) ⦄ := by + have hsw : ∃ u, σ.schedulerAction = .switchThread u := ⟨u0, hu0⟩ + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hnq := hF.nq + have hvfh := hF.vfh + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have hv : Fl4Spec.validFaultHandler (absCap c1) := by + rw [hcr.2]; exact hvfh x (by rw [hcur]; exact hk) + by_cases hn : c1 = .Null + · subst hn + exact WP.spec_mono (J.noFaultSB hF hsw hct hnq hc1 f f') fun _ ⟨z, hz, hR, h⟩ => ⟨z, hz, hR, Or.inr h⟩ + obtain ⟨p, b, sd, rv, g, gr, rfl⟩ := vfh_ep hv hn + have hAS : AbsSlot Z.th (ct.bv, Fl4Spec.tcbCnodeIndex 3) := ⟨ct, rfl, Or.inr ⟨htcb, 3, by decide, rfl⟩⟩ + have hpE : p ∈ Z.E := J.slotE hAS hc1 + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨xe, hke, -⟩ := hI.1.ep p hpE + cases xe with + | recvEP l => + obtain ⟨-, xs, hxs0, hq, -⟩ := hI.1.toE.at_ep hpE hke + obtain ⟨d', xs', rfl⟩ := List.exists_cons_of_ne_nil hxs0 + simp only [List.map_cons] at hq + subst hq + exact WP.spec_mono (J.faultRecvSB hAT hF hu0 hct hu0t hnq hc1 hke hf) fun _ ⟨z, hz, hR, h⟩ => ⟨z, hz, hR, Or.inl h⟩ + | idleEP => + exact WP.spec_mono (J.faultBlockSB hF hsw hct hnq hc1 (fun xe h => by rw [hke] at h; cases h; trivial) hf) + fun _ ⟨z, hz, hR, h⟩ => ⟨z, hz, hR, Or.inr h⟩ + | sendEP q => + exact WP.spec_mono (J.faultBlockSB hF hsw hct hnq hc1 (fun xe h => by rw [hke] at h; cases h; trivial) hf) + fun _ ⟨z, hz, hR, h⟩ => ⟨z, hz, hR, Or.inr h⟩ + +/-- **`handle_fault` at `VmEptState` at a pending switch**, V or R post. -/ +theorem VmEptState.handleFaultSV {V X : Set U64} {XL : U64 → Nat} (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + {u0 : Fl4Spec.ObjRef} (hu0 : σ.schedulerAction = .switchThread u0) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hu0t : u0 ≠ ct.bv) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ + (VEAnyX (L := L) arI AR r.1 r.2 z.2 ∨ VEAnyR (L := L) arI AR r.1 r.2 z.2) ⦄ := + WP.spec_mono (J.1.1.faultHypSD hAT hF hu0 hct hu0t hf) fun _ ⟨z, hz, hR, h⟩ => + ⟨z, hz, hR, h.elim (fun ⟨_, _, _, _, B', F⟩ => Or.inl ((J.ofNat B' F).anyX arI)) + (fun ⟨_, _, _, _, B', F⟩ => Or.inr (J.ofNatR B' F).any)⟩ + + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmFinArmB.lean b/proofs/Fl4Proofs/P3/VmFinArmB.lean new file mode 100644 index 000000000..50ba5e5a0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmB.lean @@ -0,0 +1,102 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): the `B` bundle (SpecInvFaultTcbsN; `FaultTcbsS` with `NQB`) +across the arms of the extended dispatcher `fl4PerformInvocationVF` (spec only): the TCB and CNode arms over +`capDeleteVF` / `capRevokeVF`, the untyped arm with its VCPU initialisation, the VM signal (`sendSignalVF`: the +`runningVM` target has no fault under `B`, so the report's `Running` keeps it), the frame batch over `capDeleteVF`, +the VCPU, EPT and SetEPTRoot arms. `b_fl4PerformInvocationVF` has the native `b_fl4PerformInvocation`'s statement +(SpecInvFaultTcbsH8) at the extended dispatcher. No named premise. +-/ +import Fl4Proofs.P3.VmFinVF2 +import Fl4Proofs.P3.SpecInvFaultTcbsH8 +import Fl4Proofs.P3.SpecInvFbK +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.VmFin + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {L E K : List ObjRef} {C : ObjRef → Prop} + +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_capDeleteVF _) +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_capRevokeVF _) +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_setArchV _ _) +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_vcpuDetachF _) +macro_rules | `(tactic| b_leaf) => `(tactic| ((with_reducible refine b_vmReportF _ _ ?_); simp; done)) +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact ik_guestUnmapV (fun p a => b_setArchV p a) _ _ _ _ _) +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact ik_ioPortRevokeV (fun v g => b_vcpuUpdateV v g) _) + +theorem b_installTcbCapVF (t : ObjRef) (sl : CslotPtr) (n : Nat) (o : Option (Cap × CslotPtr)) : + BKE L E K C (installTcbCapVF (Z := Unit) t sl n o) := by + unfold installTcbCapVF; b_autoE +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_installTcbCapVF _ _ _ _) + +theorem b_installTcbFrameCapVF (t : ObjRef) (sl : CslotPtr) (b : Option (VSpaceRef × Option (Cap × CslotPtr))) : + BKE L E K C (installTcbFrameCapVF (Z := Unit) t sl b) := by + unfold installTcbFrameCapVF; b_autoE +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_installTcbFrameCapVF _ _ _) + +theorem b_invokeTcbVF (i : TcbInvocation) : BKE L E K C (invokeTcbVF (Z := Unit) i) := by + cases i <;> unfold invokeTcbVF <;> b_autoE + +theorem b_invokeCnodeVF (i : CnodeInvocation) : BKE L E K C (invokeCnodeVF (Z := Unit) i) := by + cases i <;> first | exact b_capRevokeVF _ | exact b_capDeleteVF _ | exact b_invokeCnode _ + +theorem b_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + BK L E K C (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + unfold sendSignalVF vmSignalCompletes getTcbVcpu vmFullReply; b_autoE + all_goals first | exact b_sendSignal _ _ | simp_all + +theorem b_fl4InvokeUntypedV (ui : UntypedInvocation) : BKE L E K C (fl4InvokeUntypedV (Z := Unit) ui) := by + unfold fl4InvokeUntypedV setVcpu + refine ike_bind (b_of_gfE fun _ _ => by fg_auto) fun _ => ?_ + b_autoE + +theorem b_performVcpuInvocationF (i : VcpuInvocation) : BK L E K C (performVcpuInvocationF (Z := Unit) i) := by + cases i <;> unfold performVcpuInvocationF <;> (try unfold associateVcpuTcbF) <;> (try unfold ioPortSetOwner) <;> + b_autoE + +theorem b_performEptInvocation (i : EptInvocation) : BK L E K C (performEptInvocation (Z := Unit) i) := by + cases i <;> unfold performEptInvocation <;> (try unfold storeGuestEntry) <;> b_autoE + +theorem b_invokeSetEptRootF (i : SetEptRootInvocation) : BKE L E K C (invokeSetEptRootF (Z := Unit) i) := by + unfold invokeSetEptRootF; b_autoE + +theorem b_performFrameBatchVF (b : Fl4FrameBatchInvocation) : BK L E K C (performFrameBatchVF (Z := Unit) b) := by + unfold performFrameBatchVF + refine ik_performFrameBatchWith (fun w => ?_) (by unfold flushAll; b_autoE) + exact ik_fbEntryWith b w (fun _ => b_fbEntry b w) (b_of_gfE fun _ _ => by fg_auto) + (fun _ => b_of_gf fun _ _ => by fg_auto) + (fun _ _ _ _ _ _ _ => ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun va => b_unmapPageUnflushed _ _ va _) (by b_autoE)) + (fun sl => b_capDeleteVF sl) + +/-- **The extended dispatcher keeps `B`** (the native `b_fl4PerformInvocation`'s statement). -/ +theorem b_fl4PerformInvocationVF (ct : ObjRef) (onCore remoteCur : ObjRef → Bool) (bl call don : Bool) + (inv : Fl4InvocationV) : + HKE (fun σ => B [ct] [ct] [ct] (· = ct) σ ∧ NoRpl [ct] σ) (B [ct] [ct] [ct] (· = ct)) + (fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv) := by + cases inv with + | base b => + cases b + case invokeUntyped i => + exact hk_pre (hk_of_ik (ike_bind (b_fl4InvokeUntypedV i) fun _ => ike_returnOk _)) fun _ h => h.1 + case invokeNotification n bd => + exact hk_pre (hk_of_ik (ike_bind (ike_liftE (b_sendSignalVF onCore remoteCur n bd)) fun _ => ike_returnOk _)) + fun _ h => h.1 + case invokeTCB i => exact hk_pre (hk_of_ik (b_invokeTcbVF i)) fun _ h => h.1 + case invokeCNode i => exact hk_pre (hk_of_ik (ike_bind (b_invokeCnodeVF i) fun _ => ike_returnOk _)) fun _ h => h.1 + case invokeArchObject i => + cases i with + | frameBatch b => exact hk_pre (hk_of_ik (ike_liftE (b_performFrameBatchVF b))) fun _ h => h.1 + | _ => exact b_fl4PerformInvocation ct _ _ _ _ + all_goals exact b_fl4PerformInvocation ct _ _ _ _ + | vcpu i => exact hk_pre (hk_of_ik (ike_liftE (b_performVcpuInvocationF i))) fun _ h => h.1 + | ept i => exact hk_pre (hk_of_ik (ike_liftE (ik_bind (b_performEptInvocation i) fun _ => ik_pure _))) fun _ h => h.1 + | setEptRoot i => + exact hk_pre (hk_of_ik (ike_bind (b_invokeSetEptRootF i) fun _ => ike_returnOk _)) fun _ h => h.1 + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinArmDl.lean b/proofs/Fl4Proofs/P3/VmFinArmDl.lean new file mode 100644 index 000000000..4899a037a --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmDl.lean @@ -0,0 +1,110 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): the domain list (`dlV`, SpecInvDomF) across the arms of the +extended dispatcher `fl4PerformInvocationVF` (spec only): `capDeleteVF` / `capRevokeVF` by the generalised loops +(P3/VmFinGenDl) at `finaliseCapVF` (P3/VmFinFamDl), the VM hooks, and the native arms. `dle_fl4PerformVF` has the native +`dle_fl4Perform`'s statement (every perform but the Domain invocation's). No named premise. +-/ +import Fl4Proofs.P3.VmFinGenDl +import Fl4Proofs.P3.VmFinFamDl +import Fl4Proofs.P3.SpecInvDomE +import Fl4Proofs.P3.SpecInvFbK +import Fl4Proofs.P3.DelVF +open Classical + +namespace Fl4Proofs.P3.SInvDom +open Fl4Spec Fl4Proofs.P3 + +noncomputable section + +set_option maxHeartbeats 8000000 + +theorem dl_capDeleteVF (sl : CslotPtr) : DLE (capDeleteVF (Z := Unit) sl) := by + rw [← capDeleteG_vf]; exact dl_capDeleteG dl_finaliseCapVF sl +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_capDeleteVF _) + +theorem dl_capRevokeVF (sl : CslotPtr) : DLE (capRevokeVF (Z := Unit) sl) := by + rw [← capRevokeG_vf]; exact dle_capRevokeG dl_capDeleteVF sl +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_capRevokeVF _) + +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_vcpuDetachF _) +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_vmReportF _ _) +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_guestUnmapV _ _ _ _ _) +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_ioPortRevokeV _) +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_vcpuUpdateV _ _) +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_getVcpuV _) + +theorem dl_installTcbCapVF (t : ObjRef) (sl : CslotPtr) (n : Nat) (o : Option (Cap × CslotPtr)) : + DLE (installTcbCapVF (Z := Unit) t sl n o) := by + unfold installTcbCapVF; dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_installTcbCapVF _ _ _ _) + +theorem dl_installTcbFrameCapVF (t : ObjRef) (sl : CslotPtr) (b : Option (VSpaceRef × Option (Cap × CslotPtr))) : + DLE (installTcbFrameCapVF (Z := Unit) t sl b) := by + unfold installTcbFrameCapVF; dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_installTcbFrameCapVF _ _ _) + +theorem dl_invokeTcbVF (i : TcbInvocation) : DLE (invokeTcbVF (Z := Unit) i) := by + cases i <;> unfold invokeTcbVF <;> dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_invokeTcbVF _) + +theorem dl_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + DL (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + unfold sendSignalVF vmSignalCompletes getTcbVcpu vmFullReply; dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_sendSignalVF _ _ _ _) + +theorem dl_fl4InvokeUntypedV (ui : UntypedInvocation) : DLE (fl4InvokeUntypedV (Z := Unit) ui) := by + unfold fl4InvokeUntypedV setVcpu; dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_fl4InvokeUntypedV _) + +theorem dl_performVcpuInvocationF (i : VcpuInvocation) : DL (performVcpuInvocationF (Z := Unit) i) := by + cases i <;> unfold performVcpuInvocationF <;> (try unfold associateVcpuTcbF) <;> (try unfold ioPortSetOwner) <;> + dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_performVcpuInvocationF _) + +theorem dl_performEptInvocation (i : EptInvocation) : DL (performEptInvocation (Z := Unit) i) := by + cases i <;> unfold performEptInvocation <;> (try unfold storeGuestEntry) <;> dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_performEptInvocation _) + +theorem dl_invokeSetEptRootF (i : SetEptRootInvocation) : DLE (invokeSetEptRootF (Z := Unit) i) := by + unfold invokeSetEptRootF; dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_invokeSetEptRootF _) + +theorem dl_performFrameBatchVF (b : Fl4FrameBatchInvocation) : DL (performFrameBatchVF (Z := Unit) b) := by + intro σ x hx + have hik : ∀ {α : Type} {m : NonDet (State Unit) α}, DL m → SInv.IK (fun s => dlV s = dlV σ) m := + fun h s hs y hy => (h s y hy).trans hs + exact SInv.ik_performFrameBatchWith (P := fun s => dlV s = dlV σ) (del := capDeleteVF) (b := b) + (fun w => SInv.ik_fbEntryWith b w (fun _ => hik (dl_fbEntry b w)) (hik (dl_lookupTargetSlot' _ _ _)) + (fun sl => hik (dl_getCap sl)) + (fun _ _ _ _ _ _ _ => SInv.ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun _ => hik (dl_unmapPageUnflushed _ _ _ _)) + (hik (dl_setCap _ _))) + (fun sl => hik (dl_capDeleteVF sl))) + (hik (dl_flushAll _ _)) σ rfl x hx +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_performFrameBatchVF _) + +/-- **The extended dispatcher keeps the domain list** but at the Domain invocation. -/ +theorem dle_fl4PerformVF (onCore remoteCur : ObjRef → Bool) (blk call don : Bool) (inv : Fl4InvocationV) + (h : ∀ d, inv ≠ .base (.invokeDomain d)) : + DLE (fl4PerformInvocationVF (Z := Unit) onCore remoteCur blk call don inv) := by + cases inv with + | base b => + cases b + case invokeUntyped i => simp only [fl4PerformInvocationVF]; dl_auto + case invokeNotification n bd => simp only [fl4PerformInvocationVF, withoutPreemption]; dl_auto + case invokeTCB i => exact dl_invokeTcbVF i + case invokeCNode i => + simp only [fl4PerformInvocationVF] + cases i <;> simp only [invokeCnodeVF] <;> dl_auto + case invokeArchObject i => + cases i with + | frameBatch b => simp only [fl4PerformInvocationVF]; dl_auto + | _ => exact dle_fl4Perform _ _ _ _ fun _ e => by cases e + case invokeDomain d => exact absurd rfl (h d) + all_goals exact dle_fl4Perform _ _ _ _ fun _ e => by cases e + | vcpu i => simp only [fl4PerformInvocationVF]; dl_auto + | ept i => simp only [fl4PerformInvocationVF]; dl_auto + | setEptRoot i => simp only [fl4PerformInvocationVF]; dl_auto + +end + +end Fl4Proofs.P3.SInvDom diff --git a/proofs/Fl4Proofs/P3/VmFinArmFo.lean b/proofs/Fl4Proofs/P3/VmFinArmFo.lean new file mode 100644 index 000000000..728579622 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmFo.lean @@ -0,0 +1,104 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): `FP N K` (`FN K` with `P3 N`) and `ILF` across the deleting +arms of the extended dispatcher `fl4PerformInvocationVF` (spec only): the TCB arm over `capDeleteVF` +(`fo_installTcbCapVF`, `fo_installTcbFrameCapVF`, `fo_invokeTcbVF`), the CNode arm (`fo_invokeCnodeVF`: delete and +revoke by `fu_capDeleteVF` / `fu_capRevokeVF`) and SetEPTRoot (`fo_invokeSetEptRootF`: the delete keeps `FU`; the +insert writes a cap, which needs no `UtInv''`). No named premise. The non-deleting arms (untyped, signal, frame batch, +VCPU, EPT) need the FPU-owner frame `OIK q` (SpecInvFoG1) at the VM leaves: the next module. +-/ +import Fl4Proofs.P3.VmFinArmIl +import Fl4Proofs.P3.SpecInvFoP +open Classical + +namespace Fl4Proofs.P3.SInvF +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 4000000 + +open Lean in +/-- SpecInvFoG1's `oi_auto` (`oi_auto` / `oi_leaf` also name SpecInvAllO1's, imported through VmSignal, so this names +the node kind). -/ +macro "ofg_auto" : tactic => pure ⟨(mkNode ``Fl4Proofs.P3.SInvF.tacticOi_auto #[mkAtom "oi_auto"]).raw⟩ + +theorem fo_installTcbCapVF (N : Irq → ObjRef) (target : ObjRef) (slot : CslotPtr) (n : Nat) + (o : Option (Cap × CslotPtr)) (hn : n < 6) : IKE (FP N []) (installTcbCapVF (Z := Unit) target slot n o) := by + intro σ hσ y hy + have hF : ILF [] y.2 := by + revert y hy + refine (?_ : HKE (FP N []) (ILF []) (installTcbCapVF (Z := Unit) target slot n o)) σ hσ + cases o with + | none => exact fun σ h y hy => ike_returnOk _ σ (fn_ilf h.1) y hy + | some p => + obtain ⟨c, src⟩ := p + simp only [installTcbCapVF] + refine hke_bind (R := ILF []) (fun σ h y hy => fn_ilf (Fl4Proofs.P3.VmFin.fu_capDeleteVF _ σ (fp_fu h) y hy).1) + (fun _ => hk_of_ik ?_) (fun _ h => h) + unfold NonDetE.unlessE + split + · exact ike_returnOk _ + · exact ike_liftE (fo_ik (fun _ => il_checkCapAt _ _ _ (il_checkCapAt _ _ _ (il_capInsert _ _ _))) + (fun _ => oi_checkCapAt _ _ _ (oi_checkCapAt _ _ _ (oi_capInsert _ _ _)))) + exact ⟨⟨⟨hF.1, nq_installTcbCapVF (K := []) target slot n o σ hσ.1.1.2 y hy⟩, hF.2⟩, + Fl4Proofs.P3.VmFin.p3_installTcbCapVF target slot n o hn σ hσ.2 y hy⟩ + +theorem fo_installTcbFrameCapVF (N : Irq → ObjRef) (target : ObjRef) (slot : CslotPtr) + (buffer : Option (VSpaceRef × Option (Cap × CslotPtr))) : + IKE (FP N []) (installTcbFrameCapVF (Z := Unit) target slot buffer) := by + intro σ hσ y hy + have hF : ILF [] y.2 := by + revert y hy + refine (?_ : HKE (FP N []) (ILF []) (installTcbFrameCapVF (Z := Unit) target slot buffer)) σ hσ + cases buffer with + | none => exact fun σ h y hy => ike_returnOk _ σ (fn_ilf h.1) y hy + | some p => + obtain ⟨ptr, frame⟩ := p + simp only [installTcbFrameCapVF] + exact hke_bind (R := ILF []) (fun σ h y hy => fn_ilf (Fl4Proofs.P3.VmFin.fu_capDeleteVF _ σ (fp_fu h) y hy).1) + (fun _ => hk_of_ik (fo_ik (fun _ => by il_auto) (fun _ => by ofg_auto))) (fun _ h => h) + exact ⟨⟨⟨hF.1, nq_installTcbFrameCapVF (K := []) target slot buffer σ hσ.1.1.2 y hy⟩, hF.2⟩, + Fl4Proofs.P3.VmFin.p3_installTcbFrameCapVF target slot buffer σ hσ.2 y hy⟩ + +/-- **`invoke_tcb` at the extended dispatcher keeps `ILF []`** (the native `fo_invokeTcb`'s statement). -/ +theorem fo_invokeTcbVF (N : Irq → ObjRef) (ti : TcbInvocation) (σ : State Unit) (K : List ObjRef) + (hσ : FP N K σ) (hK : ∀ t ∈ tcbRT ti, t ∈ K) (hct : σ.curThread ∈ K) : + ∀ y ∈ ((invokeTcbVF (Z := Unit) ti).run σ).1, ILF [] y.2 := by + have h0 : FP N [] σ := ⟨⟨⟨il_nil_of hσ.1.1.1, hσ.1.1.2⟩, hσ.1.2⟩, hσ.2⟩ + cases ti + case threadControlCaps target slot fh th cr vr buf => + intro y hy + have key : IKE (FP N []) (invokeTcbVF (Z := Unit) (.threadControlCaps target slot fh th cr vr buf)) := by + simp only [invokeTcbVF] + exact ike_bind (fo_installTcbCapVF N _ _ 3 _ (by decide)) fun _ => + ike_bind (fo_installTcbCapVF N _ _ 4 _ (by decide)) fun _ => + ike_bind (fo_installTcbCapVF N _ _ 0 _ (by decide)) fun _ => + ike_bind (fo_installTcbCapVF N _ _ 1 _ (by decide)) fun _ => + ike_bind (fo_installTcbFrameCapVF N _ _ _) fun _ => ike_returnOk _ + exact fn_ilf (key σ h0 y hy).1 + case threadControlSched target slot fh mcp prio sc => + intro y hy + have key : HKE (FP N []) (ILF []) (invokeTcbVF (Z := Unit) (.threadControlSched target slot fh mcp prio sc)) := by + simp only [invokeTcbVF] + exact hke_bind (R := ILF []) (fun σ h y hy => fn_ilf (fo_installTcbCapVF N _ _ 3 _ (by decide) σ h y hy).1) + (fun _ => hk_of_ik (fo_ik (fun _ => by il_auto) (fun _ => by ofg_auto))) (fun _ h => h) + exact key σ h0 y hy + all_goals exact fo_invokeTcb N _ σ K hσ hK hct + +theorem fo_invokeCnodeVF (N : Irq → ObjRef) (i : CnodeInvocation) (σ : State Unit) (hσ : FP N [] σ) + (hok : CnOKN (uV σ) i) : ∀ y ∈ ((invokeCnodeVF (Z := Unit) i).run σ).1, ILF [] y.2 := by + cases i + case revokeCall s => exact fun y hy => fn_ilf (Fl4Proofs.P3.VmFin.fu_capRevokeVF s σ (fp_fu hσ) y hy).1 + case deleteCall s => exact fun y hy => fn_ilf (Fl4Proofs.P3.VmFin.fu_capDeleteVF s σ (fp_fu hσ) y hy).1 + all_goals exact fo_invokeCnode N _ σ hσ hok + +/-- **SetEPTRoot keeps `ILF []`**: the delete keeps `FU`; the insert keeps every `IL` and the FPU owner frame. -/ +theorem fo_invokeSetEptRootF (i : SetEptRootInvocation) : HKE FU (ILF []) (invokeSetEptRootF (Z := Unit) i) := by + unfold invokeSetEptRootF + exact hke_bind (R := ILF []) (fun σ h y hy => fn_ilf (Fl4Proofs.P3.VmFin.fu_capDeleteVF _ σ h y hy).1) + (fun _ => hk_of_ik (ike_liftE (fo_ik (fun _ => il_capInsert _ _ _) (fun _ => oi_capInsert _ _ _)))) + (fun _ h => h) + +end + +end Fl4Proofs.P3.SInvF diff --git a/proofs/Fl4Proofs/P3/VmFinArmFoTop.lean b/proofs/Fl4Proofs/P3/VmFinArmFoTop.lean new file mode 100644 index 000000000..ddea2c3f7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmFoTop.lean @@ -0,0 +1,257 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): `FP N K` / `ILF` across the non-deleting arms of the extended +dispatcher (untyped with its VCPU initialisation, VM signal, frame batch, VCPU, EPT; spec only) and the assembled +**`fo_fl4PerformInvocationVF`** (the native `fo_fl4PerformInvocation`'s statement). The VM arms take the slot facts `NT` +that their decoders give (as `il_fl4PerformInvocationVF`). No named premise. +-/ +import Fl4Proofs.P3.VmFinArmFo +import Fl4Proofs.P3.VmFinArmIlTop +import Fl4Proofs.P3.VmFinFamFpu +open Classical + +namespace Fl4Proofs.P3.SInvF +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 8000000 + +variable {Qo : Option ObjRef} + +/-! ## FPU-owner frame at the VM leaves -/ + +theorem oif_setVcpu (v : ObjRef) (s : VcpuState) : OIK Qo (setVcpu (Z := Unit) v s) := by + unfold setVcpu; exact oi_setObject _ _ + +theorem oif_setArch (p : ObjRef) (a : ArchKernelObj) : OIK Qo (setObject (Z := Unit) p (.archObj a)) := + oi_setObject _ _ + +theorem oif_storeGuestEntry (p : ObjRef) (i : BitVec 9) (e : Word) : OIK Qo (storeGuestEntry (Z := Unit) p i e) := by + unfold storeGuestEntry + exact ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun ⟨_, _⟩ => oi_setObject _ _ + +theorem oif_guestUnmap (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + OIK Qo (guestUnmap (Z := Unit) asid gpa ptr level table) := + Fl4Proofs.P3.VmFin.ik_guestUnmapV (fun p a => oif_setArch p a) _ _ _ _ _ + +theorem oif_ioPortRevoke (c : ArchCap) : OIK Qo (ioPortRevoke (Z := Unit) c) := + Fl4Proofs.P3.VmFin.ik_ioPortRevokeV (fun v g => oif_vcpuUpdate v g) _ + +theorem oif_performEptInvocation (i : EptInvocation) : OIK Qo (performEptInvocation (Z := Unit) i) := by + cases i with + | tableMap ptr level slot tbl i asid gpa => + unfold performEptInvocation + exact ik_bind (oif_storeGuestEntry _ _ _) fun _ => oi_setCap _ _ + | tableUnmap ptr level mapped slot => + unfold performEptInvocation + cases mapped with + | none => exact ik_pure _ + | some p => + obtain ⟨asid, gpa⟩ := p + dsimp only + refine ik_bind (oif_guestUnmap _ _ _ _ _) fun _ => ?_ + refine ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun ⟨_, _⟩ => ?_ + exact ik_bind (oif_setArch _ _) fun _ => oi_setCap _ _ + | frameMap newCap slot tbl i entry => + unfold performEptInvocation + exact ik_bind (oi_setCap _ _) fun _ => oif_storeGuestEntry _ _ _ + | frameUnmap cap slot => + unfold performEptInvocation + cases cap with + | pageCap dev p R mt sz m => + dsimp only + cases m with + | none => exact ik_pure _ + | some q => + obtain ⟨asid, gpa⟩ := q + dsimp only + refine ik_bind ?_ fun _ => oi_setCap _ _ + unfold NonDet.when + split + · exact oif_guestUnmap _ _ _ _ _ + · exact ik_pure _ + | _ => exact fun _ _ _ hx => hx.elim + | poolAssign asid pool slot => + unfold performEptInvocation + refine ik_bind (oi_getCap _) fun c => ?_ + cases c with + | archObjectCap ac => + cases ac with + | eptCap r level mapped => + cases level with + | zero => exact ik_bind (oi_setCap _ _) fun _ => by unfold storeAsidPoolEntry; ofg_auto + | succ n => exact fun _ _ _ hx => hx.elim + | _ => exact fun _ _ _ hx => hx.elim + | _ => exact fun _ _ _ hx => hx.elim + +theorem oif_performVcpuInvocationF (i : VcpuInvocation) : OIK Qo (performVcpuInvocationF (Z := Unit) i) := by + cases i with + | enableIoPort v lo hi c slot => + unfold performVcpuInvocationF + dsimp only + refine ik_bind (oif_ioPortRevoke _) fun _ => ?_ + refine ik_bind ?_ fun _ => ik_bind (oif_vcpuUpdate _ _) fun _ => ik_pure _ + unfold ioPortSetOwner + split + · exact oi_setCap _ _ + · exact ik_pure _ + | setTcb v t => + unfold performVcpuInvocationF associateVcpuTcbF + dsimp only + refine ik_bind ?_ fun _ => ik_pure _ + refine ik_bind (oif_vcpuDetachF _) fun _ => ?_ + refine ik_bind (ik_of_keepSt (keepSt_getVcpu _) _) fun st => ?_ + cases st.tcb with + | none => exact oif_vcpuUpdate _ _ + | some old => exact ik_bind (oif_vcpuDetachF _) fun _ => oif_vcpuUpdate _ _ + | _ => + unfold performVcpuInvocationF + dsimp only + repeat' (first + | exact ik_pure _ + | exact oif_vcpuUpdate _ _ + | exact ik_of_keepSt (keepSt_getVcpu _) _ + | refine ik_bind ?_ fun _ => ?_) + +theorem oif_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + OIK Qo (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + unfold sendSignalVF vmSignalCompletes + repeat' (first | exact oif_vmReportF _ _ | exact oi_sendSignal _ _ | ofg_auto | split) + +theorem oif_finaliseCapVF_frame (dev : Bool) (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) + (m : Option (Asid × VSpaceRef)) (fin : Bool) : + OIK Qo (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin) := by + rcases m with _ | ⟨a, gpa⟩ <;> cases mt <;> first + | exact oi_archFinaliseCap _ _ + | (simp only [finaliseCapVF] + refine ik_bind ?_ fun _ => ik_pure _ + unfold NonDet.when + split + · exact oif_guestUnmap _ _ _ _ _ + · exact ik_pure _) + +theorem oif_performFrameBatchVF (b : Fl4FrameBatchInvocation) : OIK Qo (performFrameBatchVF (Z := Unit) b) := by + refine ik_performFrameBatchXV (fun w => ik_fbEntryXV_of b w fbx_lookupTargetSlot fbx_lookupSourceSlot fbx_ensureEmpty + fbx_deriveCap (fun s c src dest _ hs _ _ _ _ _ _ => oi_capInsert c src dest s hs) + (fun sl v => ik_fbMapF b sl v (fun _ _ _ _ _ _ => oi_decodePageInvocation _ _ _ _ _) + (fun s pi hs _ x hx => oi_performPageInvocation pi s hs x hx)) + (fun sl => ik_fbDelXF sl (fun _ => ik_gets _) (fun c x => oi_archFinaliseCap c x) + (fun s _ _ _ _ _ _ hs _ x hx => oi_emptySlot sl .nullCap s hs x hx)) + (fun sl => ik_fbDelXVF sl (fun _ => ik_gets _) (fun dev p R mt sz m fin => oif_finaliseCapVF_frame dev p R mt sz m fin) + (fun dev p R mt sz m fin v => uk_finaliseCapVF_frame dev p R mt sz m fin) + (fun s _ _ _ _ _ _ hs _ x hx => oi_emptySlot sl .nullCap s hs x hx)) + (fun _ _ _ _ => by unfold unmapPageUnflushed; ofg_auto) + (fun s _ _ _ _ _ _ sl hs _ x hx => oi_setCap _ sl s hs x hx)) + (oi_flushAll _ _) + +/-! ## The arms -/ + +/-- The extended dispatcher's untyped arm (the native `fo_invokeUntyped`'s statement). -/ +theorem fo_fl4InvokeUntypedV {K : List ObjRef} (ui : UntypedInvocation) (σ : State Unit) (hσ : ILF K σ) + (hU : UtInv'' σ) (hok : UtGapOK (uV σ) ui) : + ∀ y ∈ (ExceptT.run (fl4InvokeUntypedV (Z := Unit) ui) σ).1, ILF K y.2 := by + intro y hy + refine ft_back (Fl4Proofs.P3.VmFin.il_fl4InvokeUntypedV ui σ (il_oL hσ.1 hσ.2) hU hok y hy) ?_ + have hO : OI σ.archState.x64CurrentFpuOwner σ := ⟨rfl, fun t ht => (hσ.2 t ht).1⟩ + unfold fl4InvokeUntypedV at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := oi_invokeUntyped ui σ hO hU hok (fun t ht => (hσ.2 t ht).2) z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + | ok u => + dsimp only at hy + refine (?_ : OIKE _ _) s1 hbz y hy + cases ui + refine ike_liftE ?_ + unfold NonDet.when + split + · refine ik_mapMx ?_ _ + exact fun v => oif_setVcpu _ _ + · exact ik_pure _ + +/-- **The extended dispatcher keeps `FT`** (the native `fo_fl4PerformInvocation`'s statement; the base facts asked of +the `.base` invocations only). -/ +theorem fo_fl4PerformInvocationVF (N : Irq → ObjRef) (onCore remoteCur : ObjRef → Bool) + (bl call don fp : Bool) (inv : Fl4InvocationV) (σ : State Unit) (K : List ObjRef) (hσ : FP N K σ) + (hct : σ.curThread ∈ K) + (hB : ∀ i, inv = .base i → (∀ t ∈ invRT i, HasTC σ t) ∧ (fp = true → NoDelI i) ∧ InvOKN (uV σ) i ∧ + DecodedFrom (uV σ) i) + (hE : ∀ i, inv = .ept i → Fl4Proofs.P3.VmFin.NT (Fl4Proofs.P3.VmFin.eptInvSlot i) σ) + (hV : ∀ i, inv = .vcpu i → ∀ w lo hi c sl, i = .enableIoPort w lo hi c sl → Fl4Proofs.P3.VmFin.NT sl σ) + (hS : fp = true → ∀ i, inv ≠ .setEptRoot i) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv).run σ).1, + ILF [] y.2 ∧ (fp = true → ILF K y.2) := by + have h0 : ILF K σ := fn_ilf hσ.1 + cases inv with + | base b => + obtain ⟨hT, hF, hok, hD⟩ := hB b rfl + have native := fo_fl4PerformInvocation N bl call don fp b σ K hσ hct hT hF hok hD + cases b + case invokeUntyped ui => + intro y hy + obtain ⟨fp0, label, args, ci, sl, cap, ex, buf, σ0, s0, hv, hc, hex, hmem⟩ := hD + have hf : GapOKN (uV σ) (.invokeUntyped ui) := + okv_fl4DecodeG fp0 label args ci sl cap ex buf hc (fun p hp => (hex p hp).1) σ0 hv _ s0 hmem + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact ilfQ (fo_fl4InvokeUntypedV ui σ h0 hσ.2.1 hf z hz) + case invokeNotification n bd => + intro y hy + refine ilfQ ((?_ : HKE (FN K) (ILF K) _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF, withoutPreemption] + exact hke_bind (R := ILF K) (hk_liftE (fun s h z hz => + fn_ilf (fo_ikn (fun L => Fl4Proofs.P3.VmFin.iln_sendSignalVF (K := L ++ K) onCore remoteCur n bd) + (fun _ => oif_sendSignalVF _ _ _ _) s h z hz))) + (fun _ => hk_of_ik (ike_returnOk _)) (fun _ h => h) + case invokeTCB ti => + refine fun y hy => ⟨?_, fun h => (hF h).elim⟩ + simp only [fl4PerformInvocationVF] at hy + exact fo_invokeTcbVF N ti σ (tcbRT ti ++ K) ⟨⟨⟨il_app hσ.1.1.1 hT, hσ.1.1.2⟩, hσ.1.2⟩, hσ.2⟩ + (fun t ht => List.mem_append_left _ ht) (List.mem_append_right _ hct) y hy + case invokeCNode ci => + refine fun y hy => ⟨?_, fun h => (hF h).elim⟩ + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact fo_invokeCnodeVF N ci σ ⟨⟨⟨il_nil_of hσ.1.1.1, hσ.1.1.2⟩, hσ.1.2⟩, hσ.2⟩ hok z hz + case invokeArchObject i => + cases i with + | frameBatch bi => + intro y hy + refine ilfQ ((?_ : IKE (ILF K) _) σ h0 y hy) + simp only [fl4PerformInvocationVF] + exact ike_liftE (fo_ik (fun _ => Fl4Proofs.P3.SInvU.il_performFrameBatchVF bi) (fun _ => oif_performFrameBatchVF bi)) + | _ => exact native + all_goals exact native + | vcpu i => + intro y hy + refine ilfQ ?_ + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := ft_mem_liftE hy + exact ft_back (Fl4Proofs.P3.VmFin.il_performVcpuInvocationF i σ (hV i rfl) (il_oL h0.1 h0.2) x hx) + (oif_performVcpuInvocationF i σ ⟨rfl, fun t ht => (h0.2 t ht).1⟩ x hx) + | ept i => + intro y hy + refine ilfQ ?_ + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := ft_mem_liftE hy + obtain ⟨a, ha, hx⟩ := hx + rw [Set.mem_singleton_iff.1 hx] + exact ft_back (Fl4Proofs.P3.VmFin.il_performEptInvocation i σ (hE i rfl) (il_oL h0.1 h0.2) a ha) + (oif_performEptInvocation i σ ⟨rfl, fun t ht => (h0.2 t ht).1⟩ a ha) + | setEptRoot i => + intro y hy + refine ⟨?_, fun h => (hS h i rfl).elim⟩ + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact fo_invokeSetEptRootF i σ ⟨⟨⟨il_nil_of hσ.1.1.1, hσ.1.1.2⟩, hσ.1.2⟩, hσ.2.1⟩ z hz + +end + +end Fl4Proofs.P3.SInvF diff --git a/proofs/Fl4Proofs/P3/VmFinArmIl.lean b/proofs/Fl4Proofs/P3/VmFinArmIl.lean new file mode 100644 index 000000000..34d8fbccd --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmIl.lean @@ -0,0 +1,191 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): `IL K` / `ILP N K` (IfLiveT with the queue bundle and `P3`) across +the overridden arms of the extended dispatcher `fl4PerformInvocationVF` (spec only): the TCB arm over `capDeleteVF` +(`ilp_installTcbCapVF`, `ilp_installTcbFrameCapVF`, `il_invokeTcbVF`), the CNode arm (`il_invokeCnodeVF`), the VM signal +(`il_sendSignalVF` / `iln_sendSignalVF`: a `runningVM` thread is live, so it joins `K`), the untyped arm with its +VCPU initialisation (`il_fl4InvokeUntypedV`) and SetEPTRoot (`il_invokeSetEptRootF`: IL needs no fact about `(t,5)`). +No named premise. +-/ +import Fl4Proofs.P3.VmFinArmIlVm +import Fl4Proofs.P3.VmFinArmNq +import Fl4Proofs.P3.SpecInvNtfnQLP +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {K : List ObjRef} + +/-! ## `P3` across the VM-aware deletions -/ + +theorem p3_capDeleteVF {N : Irq → ObjRef} (slot : CslotPtr) : IKE (P3 N) (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact p3_capDeleteG finFacts_VF (fun c b => io_VF N c b) slot + +theorem p3_capRevokeVF {N : Irq → ObjRef} (slot : CslotPtr) : IKE (P3 N) (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact un_capRevokeG finFacts_VF (fun c b => io_VF N c b) slot + +theorem p3_installTcbCapVF {N : Irq → ObjRef} (target : ObjRef) (slot : CslotPtr) (n : Nat) + (slotOpt : Option (Cap × CslotPtr)) (hn : n < 6) : IKE (P3 N) (installTcbCapVF (Z := Unit) target slot n slotOpt) := by + cases slotOpt with + | none => exact ike_returnOk _ + | some p => + obtain ⟨c, src⟩ := p + simp only [installTcbCapVF] + refine ike_bind (p3_capDeleteVF _) fun _ => ?_ + unfold NonDetE.unlessE + split + · exact ike_returnOk _ + · exact ike_liftE (p3_checkCapAt_ins c src target slot n hn) + +macro_rules | `(tactic| un_leaf) => `(tactic| with_reducible exact p3_capDeleteVF _) + +theorem p3_installTcbFrameCapVF {N : Irq → ObjRef} (target : ObjRef) (slot : CslotPtr) + (buffer : Option (VSpaceRef × Option (Cap × CslotPtr))) : + IKE (P3 N) (installTcbFrameCapVF (Z := Unit) target slot buffer) := by + unfold installTcbFrameCapVF; un_auto + +/-! ## `ILP` across `installTcbCapVF` / `installTcbFrameCapVF` and `invoke_tcb` -/ + +theorem ilp_installTcbCapVF (N : Irq → ObjRef) (target : ObjRef) (slot : CslotPtr) (n : Nat) + (o : Option (Cap × CslotPtr)) (hn : n < 6) : IKE (ILP N []) (installTcbCapVF (Z := Unit) target slot n o) := by + intro σ hσ y hy + refine ⟨⟨?_, nq_installTcbCapVF (K := []) target slot n o σ hσ.1.2 y hy⟩, + p3_installTcbCapVF target slot n o hn σ hσ.2 y hy⟩ + revert y hy + refine (?_ : HKE (ILP N []) (IL []) (installTcbCapVF (Z := Unit) target slot n o)) σ hσ + cases o with + | none => exact fun σ h y hy => ike_returnOk _ σ h.1.1 y hy + | some p => + obtain ⟨c, src⟩ := p + simp only [installTcbCapVF] + refine hke_bind (R := IL []) (fun σ h y hy => (ilu_capDeleteVF _ σ (ilp_ilu h) y hy).1.1) + (fun _ => hk_of_ik ?_) (fun _ h => h) + unfold NonDetE.unlessE + split + · exact ike_returnOk _ + · exact ike_liftE (il_checkCapAt _ _ _ (il_checkCapAt _ _ _ (il_capInsert _ _ _))) + +theorem ilp_installTcbFrameCapVF (N : Irq → ObjRef) (target : ObjRef) (slot : CslotPtr) + (buffer : Option (VSpaceRef × Option (Cap × CslotPtr))) : + IKE (ILP N []) (installTcbFrameCapVF (Z := Unit) target slot buffer) := by + intro σ hσ y hy + refine ⟨⟨?_, nq_installTcbFrameCapVF (K := []) target slot buffer σ hσ.1.2 y hy⟩, + p3_installTcbFrameCapVF target slot buffer σ hσ.2 y hy⟩ + revert y hy + refine (?_ : HKE (ILP N []) (IL []) (installTcbFrameCapVF (Z := Unit) target slot buffer)) σ hσ + cases buffer with + | none => exact fun σ h y hy => ike_returnOk _ σ h.1.1 y hy + | some p => + obtain ⟨ptr, frame⟩ := p + simp only [installTcbFrameCapVF] + exact hke_bind (R := IL []) (fun σ h y hy => (ilu_capDeleteVF _ σ (ilp_ilu h) y hy).1.1) + (fun _ => hk_of_ik (by il_auto)) (fun _ h => h) + +/-- **`invoke_tcb` at the extended dispatcher keeps `IL []`** (the native `il_invokeTcb`'s statement). -/ +theorem il_invokeTcbVF (N : Irq → ObjRef) (ti : TcbInvocation) (σ : State Unit) (K : List ObjRef) + (hσ : ILP N K σ) (hK : ∀ t ∈ tcbRT ti, t ∈ K) : + ∀ y ∈ ((invokeTcbVF (Z := Unit) ti).run σ).1, IL [] y.2 := by + have h0 : ILP N [] σ := ⟨⟨il_nil_of hσ.1.1, hσ.1.2⟩, hσ.2⟩ + cases ti + case threadControlCaps target slot fh th cr vr buf => + intro y hy + have key : IKE (ILP N []) (invokeTcbVF (Z := Unit) (.threadControlCaps target slot fh th cr vr buf)) := by + simp only [invokeTcbVF] + exact ike_bind (ilp_installTcbCapVF N _ _ 3 _ (by decide)) fun _ => + ike_bind (ilp_installTcbCapVF N _ _ 4 _ (by decide)) fun _ => + ike_bind (ilp_installTcbCapVF N _ _ 0 _ (by decide)) fun _ => + ike_bind (ilp_installTcbCapVF N _ _ 1 _ (by decide)) fun _ => + ike_bind (ilp_installTcbFrameCapVF N _ _ _) fun _ => ike_returnOk _ + exact (key σ h0 y hy).1.1 + case threadControlSched target slot fh mcp prio sc => + intro y hy + have key : HKE (ILP N []) (IL []) (invokeTcbVF (Z := Unit) (.threadControlSched target slot fh mcp prio sc)) := by + simp only [invokeTcbVF] + exact hke_bind (R := IL []) (fun σ h y hy => (ilp_installTcbCapVF N _ _ 3 _ (by decide) σ h y hy).1.1) + (fun _ => hk_of_ik (by il_auto)) (fun _ h => h) + exact key σ h0 y hy + all_goals exact il_invokeTcb N _ σ K hσ hK + +/-! ## `invoke_cnode` -/ + +theorem il_invokeCnodeVF (N : Irq → ObjRef) (i : CnodeInvocation) (σ : State Unit) (hσ : ILP N [] σ) + (hok : CnOKN (uV σ) i) : ∀ y ∈ ((invokeCnodeVF (Z := Unit) i).run σ).1, IL [] y.2 := by + cases i + case revokeCall s => exact fun y hy => (ilu_capRevokeVF s σ (ilp_ilu hσ) y hy).1.1 + case deleteCall s => exact fun y hy => (ilu_capDeleteVF s σ (ilp_ilu hσ) y hy).1.1 + all_goals exact il_invokeCnode N _ σ hσ hok + +/-! ## The VM signal -/ + +theorem il_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + HK (ILN K) (IL K) (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + intro σ hσ y hy + unfold sendSignalVF at hy + obtain ⟨z, hz, hy⟩ := hy + have hz2 : z.2 = σ := (show IK (fun s => s = σ) (getNotification (Z := Unit) n) by + unfold getNotification; repeat' (first | exact ik_getObject _ | ik_step0 | ik_unfold)) σ rfl z hz + rw [hz2] at hy + dsimp only at hy + cases ho : z.1.ntfnObj <;> cases hbt : z.1.ntfnBoundTcb <;> simp only [ho, hbt] at hy + case idleNtfn.some tcb => + obtain ⟨w, hw, hy⟩ := hy + obtain ⟨e2, x, hk, e1⟩ := PPS.getThreadState_mem hw + rw [e2] at hy + obtain ⟨c, hc⟩ := Fl4Proofs.P3.v3_sigComp_one onCore remoteCur hk + obtain ⟨u, hu, hy⟩ := hy + rw [hc] at hu + obtain rfl := Set.mem_singleton_iff.1 hu + dsimp only at hy + by_cases hg : w.1 = .runningVM ∧ c = true + · rw [if_pos hg] at hy + have hs : x.tcbState = .runningVM := e1.trans hg.1 + have h1 : IL (tcb :: K) σ := il_consR hσ.1 hk (by rw [hs]; trivial) + obtain ⟨v, hv, hy⟩ := hy + have h2 : IL K v.2 := il_tail (il_vmReportF (K := tcb :: K) tcb bd (List.mem_cons_self ..) σ h1 v hv) + exact (show ILK K _ by il_auto) v.2 h2 y hy + · rw [if_neg hg] at hy + exact il_sendSignal n bd σ hσ y hy + all_goals exact il_sendSignal n bd σ hσ y hy + +theorem iln_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + IK (ILN K) (sendSignalVF (Z := Unit) onCore remoteCur n bd) := + iln_of (il_sendSignalVF onCore remoteCur n bd) (nq_sendSignalVF (K := []) onCore remoteCur n bd) + +/-! ## The untyped arm and SetEPTRoot -/ + +theorem il_setVcpuV (v : ObjRef) (s : VcpuState) : ILK K (setVcpu (Z := Unit) v s) := by + unfold setVcpu; exact il_setArchV _ _ _ +macro_rules | `(tactic| il_leaf) => `(tactic| with_reducible exact il_setVcpuV _ _) + +/-- The extended dispatcher's untyped arm (the native `il_invokeUntyped`'s statement). -/ +theorem il_fl4InvokeUntypedV (ui : UntypedInvocation) (σ : State Unit) (hσ : IL K σ) (hU : UtInv'' σ) + (hok : UtGapOK (uV σ) ui) : + ∀ y ∈ (ExceptT.run (fl4InvokeUntypedV (Z := Unit) ui) σ).1, IL K y.2 := by + intro y hy + unfold fl4InvokeUntypedV at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := il_invokeUntyped (Kq := K) ui σ hσ hU hok z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + | ok u => + dsimp only at hy + refine (?_ : ILKE K _) s1 hbz y hy + cases ui <;> il_auto + +/-- **SetEPTRoot keeps `IL []`**: the delete keeps `ILU` and the insert keeps every `IL`; no fact about `(t,5)`. -/ +theorem il_invokeSetEptRootF (i : SetEptRootInvocation) : HKE ILU (IL []) (invokeSetEptRootF (Z := Unit) i) := by + unfold invokeSetEptRootF + exact hke_bind (R := IL []) (fun σ h y hy => (ilu_capDeleteVF _ σ h y hy).1.1) + (fun _ => hk_of_ik (ike_liftE (il_capInsert _ _ _))) (fun _ h => h) + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinArmIlTop.lean b/proofs/Fl4Proofs/P3/VmFinArmIlTop.lean new file mode 100644 index 000000000..4ecc62616 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmIlTop.lean @@ -0,0 +1,105 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): **the extended dispatcher keeps IfLiveT**. +`il_fl4PerformInvocationVF` has the native `il_fl4PerformInvocation`'s statement (SpecInvNtfnQLP) at +`fl4PerformInvocationVF` (spec only): post `IL []`, and `IL K` for a first-phase decode. Its base-invocation facts +(restart targets hold thread caps, first-phase decode names no deleting invocation, `InvOKN`, `DecodedFrom`) are asked of +the `.base` invocations only; the VM arms take the slot facts `NT` that their decoders give (`nt_eptInvSlot`, +`nt_vcpuInv`); SetEPTRoot deletes, so it is excluded at a first-phase decode (`hS`). The untyped and the arch performs +other than the frame batch are `PerfGapIL`'s (the native `il_fl4PerformInvocation`'s hypothesis `hG`). No new premise. +-/ +import Fl4Proofs.P3.VmFinFbV +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 8000000 + +/-- **The extended dispatcher keeps `IL`** (the native `il_fl4PerformInvocation`'s statement). -/ +theorem il_fl4PerformInvocationVF (hG : PerfGapIL) (N : Irq → ObjRef) (onCore remoteCur : ObjRef → Bool) + (bl call don fp : Bool) (inv : Fl4InvocationV) (σ : State Unit) (K : List ObjRef) (hσ : ILP N K σ) + (hN : NodeIs N σ) (hct : σ.curThread ∈ K) + (hB : ∀ i, inv = .base i → (∀ t ∈ invRT i, HasTC σ t) ∧ (fp = true → NoDelI i) ∧ InvOKN (uV σ) i ∧ + DecodedFrom (uV σ) i) + (hE : ∀ i, inv = .ept i → NT (eptInvSlot i) σ) + (hV : ∀ i, inv = .vcpu i → ∀ w lo hi c sl, i = .enableIoPort w lo hi c sl → NT sl σ) + (hS : fp = true → ∀ i, inv ≠ .setEptRoot i) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv).run σ).1, + IL [] y.2 ∧ (fp = true → IL K y.2) := by + cases inv with + | base b => + obtain ⟨hT, hF, hok, hD⟩ := hB b rfl + have native := il_fl4PerformInvocation hG N bl call don fp b σ K hσ hN hct hT hF hok hD + cases b + case invokeUntyped ui => + intro y hy + refine ilQ_of (?_ : IL K y.2) + obtain ⟨fp0, label, args, ci, sl, cap, ex, buf, σ0, s0, hv, hc, hex, hmem⟩ := hD + have hf : GapOKN (uV σ) (.invokeUntyped ui) := + okv_fl4DecodeG fp0 label args ci sl cap ex buf hc (fun p hp => (hex p hp).1) σ0 hv _ s0 hmem + simp only [fl4PerformInvocationVF] at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := il_fl4InvokeUntypedV ui σ hσ.1.1 hσ.2.1 hf z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + | ok u => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + case invokeNotification n bd => + intro y hy + refine ilQ_of ((?_ : HKE (ILN K) (IL K) _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF, withoutPreemption] + exact hke_bind (R := IL K) (hk_liftE (fun s h z hz => (iln_sendSignalVF (K := K) onCore remoteCur n bd s h z hz).1)) + (fun _ => hk_of_ik (ike_returnOk _)) (fun _ h => h) + case invokeTCB ti => + refine fun y hy => ⟨?_, fun h => (hF h).elim⟩ + simp only [fl4PerformInvocationVF] at hy + exact il_invokeTcbVF N ti σ (tcbRT ti ++ K) ⟨⟨il_app hσ.1.1 hT, hσ.1.2⟩, hσ.2⟩ + (fun t ht => List.mem_append_left _ ht) y hy + case invokeCNode ci => + refine fun y hy => ⟨?_, fun h => (hF h).elim⟩ + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact il_invokeCnodeVF N ci σ ⟨⟨il_nil_of hσ.1.1, hσ.1.2⟩, hσ.2⟩ hok z hz + case invokeArchObject i => + cases i with + | frameBatch bi => + intro y hy + refine ilQ_of ((?_ : ILKE K _) σ hσ.1.1 y hy) + simp only [fl4PerformInvocationVF] + exact ike_liftE (il_performFrameBatchVF bi) + | _ => exact native + all_goals exact native + | vcpu i => + intro y hy + refine ilQ_of (?_ : IL K y.2) + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + exact il_performVcpuInvocationF i σ (hV i rfl) hσ.1.1 x hx + | ept i => + intro y hy + refine ilQ_of (?_ : IL K y.2) + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + obtain ⟨a, ha, hx⟩ := hx + have h1 := il_performEptInvocation i σ (hE i rfl) hσ.1.1 a ha + rw [Set.mem_singleton_iff.1 hx] + exact h1 + | setEptRoot i => + intro y hy + refine ⟨?_, fun h => (hS h i rfl).elim⟩ + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact il_invokeSetEptRootF i σ (ilp_ilu ⟨⟨il_nil_of hσ.1.1, hσ.1.2⟩, hσ.2⟩) z hz + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinArmIlVm.lean b/proofs/Fl4Proofs/P3/VmFinArmIlVm.lean new file mode 100644 index 000000000..a8e5e80dd --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmIlVm.lean @@ -0,0 +1,195 @@ +/- +P3 (line mmuAD, Wave 3 step (c)): `IL K` (IfLiveT) across the VCPU and EPT arms of the extended dispatcher (spec +only). The caps these arms overwrite by `set_cap` sit in slots holding no thread cap (`NT`), a fact their decoders +give (`VmFinDecode`) about the slots the invocation was looked up in. No named premise. +-/ +import Fl4Proofs.P3.VmFinVF +import Fl4Proofs.P3.VmFinFamA +import Fl4Proofs.P3.VmFinFamHook +import Fl4Proofs.P3.VmFinDecode +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 2000000 + +variable {K : List ObjRef} + +/-- The slot holds no thread cap. -/ +def NT (sl : CslotPtr) (σ : State Unit) : Prop := ∀ t, SInvU.capAt σ sl ≠ some (.threadCap t) + +theorem nt_of_uV {sl : CslotPtr} {σ σ' : State Unit} (h : NT sl σ) (e : uV σ' = uV σ) : NT sl σ' := by + intro t + have e' : SInvU.capAt σ' = SInvU.capAt σ := congrArg (fun v => v.2.1) e + rw [e']; exact h t + +/-- `IL K` with a slot that holds no thread cap. -/ +def ILT (K : List ObjRef) (sl : CslotPtr) (σ : State Unit) : Prop := IL K σ ∧ NT sl σ + +theorem ik_ilt {α : Type} {sl : CslotPtr} {m : NonDet (State Unit) α} (hi : ILK K m) (hu : ∀ v, UK v m) : + IK (ILT K sl) m := + fun σ h y hy => ⟨hi σ h.1 y hy, nt_of_uV h.2 (hu (uV σ) σ rfl y hy)⟩ + +theorem uk_storeGuestEntry {v : UView} (p : ObjRef) (i : BitVec 9) (e : Word) : + UK v (storeGuestEntry (Z := Unit) p i e) := by + unfold storeGuestEntry + exact ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun ⟨_, _⟩ => uk_setArch _ _ + +theorem il_storeGuestEntry (p : ObjRef) (i : BitVec 9) (e : Word) : ILK K (storeGuestEntry (Z := Unit) p i e) := by + unfold storeGuestEntry + exact ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun ⟨_, _⟩ => il_setArchV _ _ _ + +theorem il_guestUnmapV (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + ILK K (guestUnmap (Z := Unit) asid gpa ptr level table) := + ik_guestUnmapV (fun p a => il_setArchV _ p a) _ _ _ _ _ + +theorem il_ioPortRevokeV (c : ArchCap) : ILK K (ioPortRevoke (Z := Unit) c) := + ik_ioPortRevokeV (fun v g => il_vcpuUpdateV v g) _ + +/-- The slot a guest page-table invocation rewrites. -/ +def eptInvSlot : EptInvocation → CslotPtr + | .tableMap _ _ sl _ _ _ _ => sl + | .tableUnmap _ _ _ sl => sl + | .frameMap _ sl _ _ _ => sl + | .frameUnmap _ sl => sl + | .poolAssign _ _ sl => sl + +theorem il_performEptInvocation (i : EptInvocation) (σ : State Unit) (h : NT (eptInvSlot i) σ) : + IKp (IL K) σ (performEptInvocation (Z := Unit) i) := by + intro hσ y hy + have h0 : ILT K (eptInvSlot i) σ := ⟨hσ, h⟩ + cases i with + | tableMap ptr level slot tbl i asid gpa => + unfold performEptInvocation at hy + obtain ⟨a, ha, hy⟩ := hy + have h1 := ik_ilt (K := K) (sl := slot) (il_storeGuestEntry _ _ _) (fun _ => uk_storeGuestEntry _ _ _) σ h0 a ha + exact il_setCap_nt _ _ a.2 h1.2 h1.1 y hy + | tableUnmap ptr level mapped slot => + unfold performEptInvocation at hy + dsimp only at hy + cases mapped with + | none => exact ik_pure () σ hσ y hy + | some p => + obtain ⟨asid, gpa⟩ := p + dsimp only at hy + -- the unmap and the clearing, then `set_cap` + obtain ⟨a, ha, hy⟩ := hy + have h1 := ik_ilt (K := K) (sl := slot) (il_guestUnmapV _ _ _ _ _) (fun _ => uk_guestUnmap _ _ _ _ _) σ h0 a ha + obtain ⟨b, hb, hy⟩ := hy + have h2 := (ik_of_keepSt (keepSt_getEpt ptr) (ILT K slot)) a.2 h1 b hb + obtain ⟨c, hc, hy⟩ := hy + have h3 := ik_ilt (K := K) (sl := slot) (il_setArchV _ _ _) (fun _ => uk_setArch _ _) b.2 h2 c hc + exact il_setCap_nt _ _ c.2 h3.2 h3.1 y hy + | frameMap newCap slot tbl i entry => + unfold performEptInvocation at hy + obtain ⟨a, ha, hy⟩ := hy + exact il_storeGuestEntry _ _ _ a.2 (il_setCap_nt _ _ σ h hσ a ha) y hy + | frameUnmap cap slot => + unfold performEptInvocation at hy + cases cap with + | pageCap dev p R mt sz m => + dsimp only at hy + cases m with + | none => exact ik_pure () σ hσ y hy + | some q => + obtain ⟨asid, gpa⟩ := q + dsimp only at hy + obtain ⟨a, ha, hy⟩ := hy + have pre : IK (ILT K slot) (NonDet.when (asid ≠ 0) + (guestUnmap (Z := Unit) asid gpa p (pageBitsForSize sz) false)) := by + unfold NonDet.when + split + · exact ik_ilt (il_guestUnmapV _ _ _ _ _) fun _ => uk_guestUnmap _ _ _ _ _ + · exact ik_pure _ + have h1 := pre σ h0 a ha + exact il_setCap_nt _ _ a.2 h1.2 h1.1 y hy + | _ => exact hy.elim + | poolAssign asid pool slot => + unfold performEptInvocation at hy + obtain ⟨a, ha, hy⟩ := hy + have e : a.2 = σ := (getCap_capAt ha).1 + rw [e] at hy + obtain ⟨c, s1⟩ := a + cases c with + | archObjectCap ac => + cases ac with + | eptCap r level mapped => + cases level with + | zero => + obtain ⟨b, hb, hy⟩ := hy + exact il_storeAsidPoolEntry _ _ _ b.2 (il_setCap_nt _ _ σ h hσ b hb) y hy + | succ n => exact hy.elim + | _ => exact hy.elim + | _ => exact hy.elim + +theorem il_performVcpuInvocationF (i : VcpuInvocation) (σ : State Unit) + (h : ∀ v lo hi c sl, i = .enableIoPort v lo hi c sl → NT sl σ) : + IKp (IL K) σ (performVcpuInvocationF (Z := Unit) i) := by + intro hσ y hy + cases i with + | enableIoPort v lo hi c slot => + have h0 : ILT K slot σ := ⟨hσ, h _ _ _ _ _ rfl⟩ + unfold performVcpuInvocationF at hy + dsimp only at hy + obtain ⟨a, ha, hy⟩ := hy + have h1 := ik_ilt (K := K) (sl := slot) (il_ioPortRevokeV c) + (fun _ => ik_ioPortRevokeV (fun w g => uk_vcpuUpdateV w g) _) σ h0 a ha + obtain ⟨b, hb, hy⟩ := hy + have h2 : IL K b.2 := by + unfold ioPortSetOwner at hb + split at hb + · exact il_setCap_nt _ _ a.2 h1.2 h1.1 b hb + · exact ik_pure () a.2 h1.1 b hb + refine (?_ : ILK K _) b.2 h2 y hy + exact ik_bind (il_vcpuUpdateV _ _) fun _ => ik_pure _ + | setTcb v t => + refine (?_ : ILK K _) σ hσ y hy + unfold performVcpuInvocationF associateVcpuTcbF + dsimp only + refine ik_bind ?_ fun _ => ik_pure _ + refine ik_bind (il_vcpuDetachF _) fun _ => ?_ + refine ik_bind (ik_of_keepSt (keepSt_getVcpu _) _) fun st => ?_ + cases st.tcb with + | none => exact il_vcpuUpdateV _ _ + | some old => exact ik_bind (il_vcpuDetachF _) fun _ => il_vcpuUpdateV _ _ + | _ => + refine (?_ : ILK K _) σ hσ y hy + unfold performVcpuInvocationF + dsimp only + repeat' (first + | exact ik_pure _ + | exact il_vcpuUpdateV _ _ + | exact ik_of_keepSt (keepSt_getVcpu _) _ + | refine ik_bind ?_ fun _ => ?_) + +/-! ## From the decode facts -/ + +theorem nt_of_arch {sl : CslotPtr} {σ : State Unit} {c : ArchCap} (h : SInvU.capAt σ sl = some (.archObjectCap c)) : + NT sl σ := fun t e => by rw [h] at e; cases e + +theorem nt_eptInvSlot {cap : Cap} {slot : CslotPtr} {ex : List (Cap × CslotPtr)} {i : EptInvocation} {σ : State Unit} + (hd : EptDec cap slot ex i) (hl : ∀ p ∈ (cap, slot) :: ex, SInvU.capAt σ p.2 = some p.1) : + NT (eptInvSlot i) σ := by + have h0 := hl (cap, slot) (List.mem_cons_self ..) + cases i with + | tableMap => obtain ⟨rfl, rfl, -⟩ := hd; exact nt_of_arch h0 + | tableUnmap => obtain ⟨rfl, rfl, -⟩ := hd; exact nt_of_arch h0 + | frameMap => obtain ⟨rfl, _, _, _, _, _, _, _, rfl, -⟩ := hd; exact nt_of_arch h0 + | frameUnmap => obtain ⟨rfl, rfl, -⟩ := hd; exact nt_of_arch h0 + | poolAssign => + obtain ⟨-, r, rest, e⟩ := hd + exact nt_of_arch (hl (_, _) (List.mem_cons_of_mem _ (by rw [e]; exact List.mem_cons_self ..))) + +theorem nt_vcpuInv {v : ObjRef} {slot : CslotPtr} {cap : Cap} {ex : List (Cap × CslotPtr)} {i : VcpuInvocation} + {σ : State Unit} (hd : VcpuDec v ex i) (hl : ∀ p ∈ (cap, slot) :: ex, SInvU.capAt σ p.2 = some p.1) : + ∀ w lo hi c sl, i = .enableIoPort w lo hi c sl → NT sl σ := by + rintro w lo hi c sl rfl + obtain ⟨-, -, rest, e⟩ := hd + exact nt_of_arch (hl (_, _) (List.mem_cons_of_mem _ (by rw [e]; exact List.mem_cons_self ..))) + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinArmNq.lean b/proofs/Fl4Proofs/P3/VmFinArmNq.lean new file mode 100644 index 000000000..773679935 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmNq.lean @@ -0,0 +1,215 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): the queue bundle `NQ K` (SpecInvNtfnQ) across the arms of the +extended dispatcher `fl4PerformInvocationVF` (spec only): the TCB and CNode arms over `capDeleteVF` / `capRevokeVF`, +the untyped arm with its VCPU initialisation, the VM signal (`sendSignalVF`: a `runningVM` target is in no queue), the +frame batch over `capDeleteVF`, the VCPU, EPT and SetEPTRoot arms. `nq_fl4PerformInvocationVF` has the native +`nq_fl4PerformInvocation`'s statement (SpecInvNtfnQH2), its decode fact asked of the `.base` invocations only. No +named premise. +-/ +import Fl4Proofs.P3.VmFinVF2 +import Fl4Proofs.P3.VmFinFamA +import Fl4Proofs.P3.VmSignal +import Fl4Proofs.P3.SpecInvNtfnQH2 +import Fl4Proofs.P3.SpecInvFbK +import Fl4Proofs.P3.DelVF +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.VmFin + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {K : List ObjRef} + +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_capDeleteVF _) +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_capRevokeVF _) +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_setArchV _ _ _) +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_vcpuDetachF _) +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact ik_guestUnmapV (fun p a => nq_setArchV _ p a) _ _ _ _ _) +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact ik_ioPortRevokeV (fun v g => nq_vcpuUpdateV v g) _) + +theorem nq_installTcbCapVF (t : ObjRef) (sl : CslotPtr) (n : Nat) (o : Option (Cap × CslotPtr)) : + NQKE K (installTcbCapVF (Z := Unit) t sl n o) := by + unfold installTcbCapVF; nq2_auto +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_installTcbCapVF _ _ _ _) + +theorem nq_installTcbFrameCapVF (t : ObjRef) (sl : CslotPtr) (b : Option (VSpaceRef × Option (Cap × CslotPtr))) : + NQKE K (installTcbFrameCapVF (Z := Unit) t sl b) := by + unfold installTcbFrameCapVF; nq2_auto +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_installTcbFrameCapVF _ _ _) + +theorem pj_installTcbCapVF {L : List ObjRef} (t : ObjRef) (sl : CslotPtr) (n : Nat) (o : Option (Cap × CslotPtr)) : + PJKE L (installTcbCapVF (Z := Unit) t sl n o) := by + unfold installTcbCapVF + cases o with + | none => exact ike_returnOk _ + | some p => + obtain ⟨c, src⟩ := p + refine ike_bind (pj_capDeleteVF _) fun _ => ?_ + pj_auto +macro_rules | `(tactic| pj_leaf) => `(tactic| with_reducible exact pj_installTcbCapVF _ _ _ _) + +/-- `invoke_tcb` at the extended dispatcher (the native `nq_invokeTcbK`'s statement). -/ +theorem nq_invokeTcbKVF (a0 : TcbInvocation) : + HKE (fun s => NQ K s ∧ PJ K s) (NQ K) (invokeTcbVF (Z := Unit) a0) := by + cases a0 + case threadControlCaps target slot fh th cr vr buf => + refine fun σ hσ y hy => + (?_ : NQKE K (invokeTcbVF (Z := Unit) (.threadControlCaps target slot fh th cr vr buf))) σ hσ.1 y hy + unfold invokeTcbVF; nq2_auto + case threadControlSched target slot fh mcp prio sc => + cases prio with + | none => + refine fun σ hσ y hy => + (?_ : NQKE K (invokeTcbVF (Z := Unit) (.threadControlSched target slot fh mcp none sc))) σ hσ.1 y hy + simp only [invokeTcbVF, NonDet.maybeM] + nq2_auto + | some p => + obtain ⟨p1, p2⟩ := p + simp only [invokeTcbVF, NonDet.maybeM] + refine hke_bind (hk_of_ik (ik_nqpj (by nq2_auto) (by pj_auto))) (fun _ => ?_) (fun _ h => h.1) + refine hke_bind (hk_of_ik (ik_nqpj (by nq2_auto) (by pj_auto))) (fun _ => ?_) (fun _ h => h.1) + refine hke_bind (R := NQ K) (hk_liftE (hk_pre (nq_setPriority_K target p1) fun s h => + ⟨h.1, fun ht x hx => pj_epB h.2 ht hx⟩)) (fun _ => hk_of_ik (by nq2_auto)) (fun _ h => h) + all_goals + simp only [invokeTcbVF] + exact nq_invokeTcbK _ + +theorem nq_invokeCnodeVF (i : CnodeInvocation) : NQKE K (invokeCnodeVF (Z := Unit) i) := by + cases i <;> first | exact nq_capRevokeVF _ | exact nq_capDeleteVF _ | exact nq_invokeCnode _ + +theorem nq_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + NQK K (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + intro σ hσ y hy + unfold sendSignalVF at hy + obtain ⟨z, hz, hy⟩ := hy + have hz2 : z.2 = σ := (show IK (fun s => s = σ) (getNotification (Z := Unit) n) by + unfold getNotification; repeat' (first | exact ik_getObject _ | ik_step0 | ik_unfold)) σ rfl z hz + rw [hz2] at hy + dsimp only at hy + cases ho : z.1.ntfnObj <;> cases hbt : z.1.ntfnBoundTcb <;> simp only [ho, hbt] at hy + case idleNtfn.some tcb => + obtain ⟨w, hw, hy⟩ := hy + obtain ⟨e2, x, hk, e1⟩ := PPS.getThreadState_mem hw + rw [e2] at hy + obtain ⟨c, hc⟩ := Fl4Proofs.P3.v3_sigComp_one onCore remoteCur hk + obtain ⟨u, hu, hy⟩ := hy + rw [hc] at hu + obtain rfl := Set.mem_singleton_iff.1 hu + dsimp only at hy + by_cases hg : w.1 = .runningVM ∧ c = true + · rw [if_pos hg] at hy + have hs : x.tcbState = .runningVM := e1.trans hg.1 + have h1 : NQ (tcb :: K) σ := nq_consR hσ hk (by rw [hs]; simp [isBoN]) (by rw [hs]; simp [epBlocked]) + obtain ⟨v, hv, hy⟩ := hy + have h2 : NQ K v.2 := nq_tail (nq_vmReportF (K := tcb :: K) tcb bd (List.mem_cons_self ..) σ h1 v hv) + exact (show NQK K _ by nq2_auto) v.2 h2 y hy + · rw [if_neg hg] at hy + exact nq_sendSignal n bd σ hσ y hy + all_goals exact nq_sendSignal n bd σ hσ y hy + +theorem nq_setVcpuV (v : ObjRef) (s : VcpuState) : NQK K (setVcpu (Z := Unit) v s) := by + unfold setVcpu; exact nq_setArchV _ _ _ +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_setVcpuV _ _) + +theorem nq_performVcpuInvocationF (i : VcpuInvocation) : NQK K (performVcpuInvocationF (Z := Unit) i) := by + cases i <;> unfold performVcpuInvocationF <;> (try unfold associateVcpuTcbF) <;> (try unfold ioPortSetOwner) <;> + nq_auto + +theorem nq_performEptInvocation (i : EptInvocation) : NQK K (performEptInvocation (Z := Unit) i) := by + cases i <;> unfold performEptInvocation <;> (try unfold storeGuestEntry) <;> nq_auto + +theorem nq_invokeSetEptRootF (i : SetEptRootInvocation) : NQKE K (invokeSetEptRootF (Z := Unit) i) := by + unfold invokeSetEptRootF; nq_auto + +theorem nq_performFrameBatchVF (b : Fl4FrameBatchInvocation) : NQK K (performFrameBatchVF (Z := Unit) b) := by + unfold performFrameBatchVF + refine ik_performFrameBatchWith (fun w => ?_) (by unfold flushAll; nq_auto) + refine ik_fbEntryWith b w (fun _ => by unfold fbEntry; nq_auto) (by nq_auto) (fun _ => by nq_auto) + (fun _ _ _ _ _ _ _ => ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun va => by unfold unmapPageUnflushed; nq_auto) + (by nq_auto)) + (fun sl => nq_capDeleteVF sl) + +/-- The extended dispatcher's untyped arm. -/ +theorem nq_fl4InvokeUntypedV (ui : UntypedInvocation) (σ : State Unit) (hI : SInvU.UtInv σ) + (hok : SInvU.UtRetOK σ ui) (hD : NqDet σ) (hσ : NQ K σ) : + ∀ y ∈ (ExceptT.run (fl4InvokeUntypedV (Z := Unit) ui) σ).1, NQ K y.2 := by + intro y hy + unfold fl4InvokeUntypedV at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := nq_invokeUntyped (K := K) ui σ hI hok hD hσ z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + | ok u => + dsimp only at hy + refine (?_ : NQKE K _) s1 hbz y hy + cases ui <;> nq_auto + +/-- **The extended dispatcher keeps `NQ`** (the native `nq_fl4PerformInvocation`'s statement). -/ +theorem nq_fl4PerformInvocationVF (onCore remoteCur : ObjRef → Bool) (bl call don : Bool) (inv : Fl4InvocationV) : + HKE (fun σ => NQ K σ ∧ σ.curThread ∈ K ∧ PJ K σ ∧ (∀ i, inv = .base i → NqDec σ i) ∧ SInvU.UtInv σ ∧ NqDet σ) + (fun s => NQ [] s ∧ (bl = false → NQ K s)) + (fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv) := by + cases inv with + | base b => + have native : HKE (fun σ => NQ K σ ∧ σ.curThread ∈ K ∧ PJ K σ ∧ (∀ i, (Fl4InvocationV.base b) = .base i → NqDec σ i) + ∧ SInvU.UtInv σ ∧ NqDet σ) (fun s => NQ [] s ∧ (bl = false → NQ K s)) + (fl4PerformInvocation (Z := Unit) bl call don b) := + hk_pre (nq_fl4PerformInvocation bl call don b) fun σ h => + ⟨h.1, h.2.1, h.2.2.1, h.2.2.2.1 b rfl, h.2.2.2.2.1, h.2.2.2.2.2⟩ + cases b + case invokeUntyped i => + intro σ hσ y hy + simp only [fl4PerformInvocationVF] at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := nq_fl4InvokeUntypedV (K := K) i σ hσ.2.2.2.2.1 ((hσ.2.2.2.1 _ rfl).1 i rfl) hσ.2.2.2.2.2 hσ.1 z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact nqP_of hbz + | ok u => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact nqP_of hbz + case invokeNotification n bd => + refine fun σ hσ y hy => nqP_of ((?_ : NQKE K _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF, withoutPreemption] + exact ike_bind (ike_liftE (nq_sendSignalVF onCore remoteCur n bd)) fun _ => ike_returnOk _ + case invokeTCB i => + simp only [fl4PerformInvocationVF] + exact fun σ hσ y hy => nqP_of (nq_invokeTcbKVF i σ ⟨hσ.1, hσ.2.2.1⟩ y hy) + case invokeCNode i => + refine fun σ hσ y hy => nqP_of ((?_ : NQKE K _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF] + exact ike_bind (nq_invokeCnodeVF i) fun _ => ike_returnOk _ + case invokeArchObject i => + cases i with + | frameBatch b => + refine fun σ hσ y hy => nqP_of ((?_ : NQKE K _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF] + exact ike_liftE (nq_performFrameBatchVF b) + | _ => exact native + all_goals exact native + | vcpu i => + refine fun σ hσ y hy => nqP_of ((?_ : NQKE K _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF] + exact ike_liftE (nq_performVcpuInvocationF i) + | ept i => + refine fun σ hσ y hy => nqP_of ((?_ : NQKE K _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF] + exact ike_liftE (ik_bind (nq_performEptInvocation i) fun _ => ik_pure _) + | setEptRoot i => + refine fun σ hσ y hy => nqP_of ((?_ : NQKE K _) σ hσ.1 y hy) + simp only [fl4PerformInvocationVF] + exact ike_bind (nq_invokeSetEptRootF i) fun _ => ike_returnOk _ + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinArmVgA.lean b/proofs/Fl4Proofs/P3/VmFinArmVgA.lean new file mode 100644 index 000000000..78422e4b8 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmVgA.lean @@ -0,0 +1,229 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c), VG arms, part A): `SI N t` (`iw`) and `S N M` (`sg`) across the +overridden base arms of the extended dispatcher `fl4PerformInvocationVF` (spec only): the deletions over `capDeleteVF` +(`iw_/sg_capDeleteVF`, `iw_/sg_capRevokeVF`), the TCB installs and `invoke_tcb` (`iw_/sg_invokeTcbVF`), `invoke_cnode` +(`iw_/sg_invokeCnodeVF`), the untyped arm with its VCPU initialisation (`iw_/sg_fl4InvokeUntypedV`) and the frame batch +(`iw_/sg_performFrameBatchVF`, over `fbq_runV`). No named premise. +-/ +import Fl4Proofs.P3.VmFinVgFb +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvG Fl4Proofs.P3.SInvIV + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {N M : Irq → ObjRef} {t : ObjRef} + +/-! ## The deletions -/ + +theorem iw_capDeleteVF (slot : CslotPtr) : IKE (SI N t) (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf] + exact iw_capDeleteG Fl4Proofs.P3.VmFin.finFacts_VF (Fl4Proofs.P3.VmFin.ni_VF t) Fl4Proofs.P3.VmFin.iv_fin_remVF + Fl4Proofs.P3.VmFin.io_VF slot + +theorem iw_capRevokeVF (slot : CslotPtr) : IKE (SI N t) (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf] + exact iw_capRevokeG Fl4Proofs.P3.VmFin.finFacts_VF (Fl4Proofs.P3.VmFin.ni_VF t) Fl4Proofs.P3.VmFin.iv_fin_remVF + Fl4Proofs.P3.VmFin.io_VF slot + +theorem sg_capDeleteVF (slot : CslotPtr) : IKE (S N M) (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf] + exact sg_capDeleteG Fl4Proofs.P3.VmFin.finFacts_VF (Fl4Proofs.P3.VmFin.no_VF N) Fl4Proofs.P3.VmFin.fin_remVF + Fl4Proofs.P3.VmFin.io_VF slot + +theorem sg_capRevokeVF (slot : CslotPtr) : IKE (S N M) (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf] + exact sg_capRevokeG Fl4Proofs.P3.VmFin.finFacts_VF (Fl4Proofs.P3.VmFin.no_VF N) Fl4Proofs.P3.VmFin.fin_remVF + Fl4Proofs.P3.VmFin.io_VF slot + +macro_rules | `(tactic| iw_leaf) => `(tactic| with_reducible exact iw_capDeleteVF _) +macro_rules | `(tactic| iw_leaf) => `(tactic| with_reducible exact iw_capRevokeVF _) +macro_rules | `(tactic| sg_leaf) => `(tactic| with_reducible exact sg_capDeleteVF _) +macro_rules | `(tactic| sg_leaf) => `(tactic| with_reducible exact sg_capRevokeVF _) + +/-! ## The TCB installs and `invoke_tcb` -/ + +theorem iw_installTcbCapVF (target : ObjRef) (slot : CslotPtr) (n : Nat) (o : Option (Cap × CslotPtr)) + (hn : n < 6) : IKE (SI N t) (installTcbCapVF (Z := Unit) target slot n o) := by + cases o with + | none => exact ike_returnOk _ + | some p => + obtain ⟨c, src⟩ := p + simp only [installTcbCapVF] + refine ike_bind (iw_capDeleteVF _) fun _ => ?_ + unfold NonDetE.unlessE + split + · exact ike_returnOk _ + · exact ike_liftE (iw_checkCapAt_ins c src target slot n hn) + +theorem sg_installTcbCapVF (target : ObjRef) (slot : CslotPtr) (n : Nat) (o : Option (Cap × CslotPtr)) + (hn : n < 6) : IKE (S N M) (installTcbCapVF (Z := Unit) target slot n o) := by + cases o with + | none => exact ike_returnOk _ + | some p => + obtain ⟨c, src⟩ := p + simp only [installTcbCapVF] + refine ike_bind (sg_capDeleteVF _) fun _ => ?_ + unfold NonDetE.unlessE + split + · exact ike_returnOk _ + · exact ike_liftE (sg_checkCapAt_ins c src target slot n hn) + +macro_rules | `(tactic| iw_leaf) => `(tactic| with_reducible exact iw_installTcbCapVF _ _ _ _ (by decide)) +macro_rules | `(tactic| sg_leaf) => `(tactic| with_reducible exact sg_installTcbCapVF _ _ _ _ (by decide)) + +theorem iw_installTcbFrameCapVF (target : ObjRef) (slot : CslotPtr) + (buffer : Option (VSpaceRef × Option (Cap × CslotPtr))) : + IKE (SI N t) (installTcbFrameCapVF (Z := Unit) target slot buffer) := by + unfold installTcbFrameCapVF; iw_auto + +theorem sg_installTcbFrameCapVF (target : ObjRef) (slot : CslotPtr) + (buffer : Option (VSpaceRef × Option (Cap × CslotPtr))) : + IKE (S N M) (installTcbFrameCapVF (Z := Unit) target slot buffer) := by + unfold installTcbFrameCapVF; sg_auto + +macro_rules | `(tactic| iw_leaf) => `(tactic| with_reducible exact iw_installTcbFrameCapVF _ _ _) +macro_rules | `(tactic| sg_leaf) => `(tactic| with_reducible exact sg_installTcbFrameCapVF _ _ _) + +theorem iw_invokeTcbVF (ti : TcbInvocation) : IKE (SI N t) (invokeTcbVF (Z := Unit) ti) := by + cases ti + case threadControlCaps => simp only [invokeTcbVF]; iw_auto + case threadControlSched => simp only [invokeTcbVF]; iw_auto + all_goals exact iw_invokeTcb _ + +theorem sg_invokeTcbVF (ti : TcbInvocation) : IKE (S N M) (invokeTcbVF (Z := Unit) ti) := by + cases ti + case threadControlCaps => simp only [invokeTcbVF]; sg_auto + case threadControlSched => simp only [invokeTcbVF]; sg_auto + all_goals exact sg_invokeTcb _ + +/-! ## `invoke_cnode` -/ + +theorem iw_invokeCnodeVF (ci : CnodeInvocation) (σ : State Unit) (hI : SI N t σ) (hok : CnOKN (uV σ) ci) + (hr : RotOKN (uV σ) ci) : ∀ y ∈ ((invokeCnodeVF (Z := Unit) ci).run σ).1, SI N t y.2 := by + cases ci + case revokeCall s => exact iw_capRevokeVF s σ hI + case deleteCall s => exact iw_capDeleteVF s σ hI + all_goals exact iw_invokeCnode _ σ hI hok hr + +theorem sg_invokeCnodeVF (ci : CnodeInvocation) (σ : State Unit) (hI : S N M σ) (hok : CnOKN (uV σ) ci) + (hr : RotOKN (uV σ) ci) : ∀ y ∈ ((invokeCnodeVF (Z := Unit) ci).run σ).1, S N M y.2 := by + cases ci + case revokeCall s => exact sg_capRevokeVF s σ hI + case deleteCall s => exact sg_capDeleteVF s σ hI + all_goals exact sg_invokeCnode _ σ hI hok hr + +/-! ## The untyped arm -/ + +theorem uk_setVcpuV {v : UView} (p : ObjRef) (s : VcpuState) : UK v (setVcpu (Z := Unit) p s) := by + unfold setVcpu; exact Fl4Proofs.P3.VmFin.uk_setArch _ _ + +theorem iw_fl4InvokeUntypedV (ui : UntypedInvocation) (σ : State Unit) (hP : SI N t σ) (hok : UtGapOK (uV σ) ui) : + ∀ y ∈ (ExceptT.run (fl4InvokeUntypedV (Z := Unit) ui) σ).1, SI N t y.2 := by + intro y hy + unfold fl4InvokeUntypedV at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := iw_invokeUntyped ui σ hP hok z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + | ok u => + dsimp only at hy + refine iwe_of_uk (fun v => ?_) s1 hbz y hy + cases ui + refine ike_liftE ?_ + unfold NonDet.when + split + · exact ik_mapMx (fun _ => uk_setVcpuV _ _) _ + · exact ik_pure _ + +theorem sg_fl4InvokeUntypedV (ui : UntypedInvocation) (σ : State Unit) (hP : S N M σ) (hok : UtGapOK (uV σ) ui) : + ∀ y ∈ (ExceptT.run (fl4InvokeUntypedV (Z := Unit) ui) σ).1, S N M y.2 := by + intro y hy + unfold fl4InvokeUntypedV at hy + rw [ExceptT.run_bind] at hy + obtain ⟨z, hz, hy⟩ := hy + have hbz := sg_invokeUntyped ui σ hP hok z hz + obtain ⟨r, s1⟩ := z + cases r with + | error e => + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy; exact hbz + | ok u => + dsimp only at hy + refine se_of_uk (fun v => ?_) s1 hbz y hy + cases ui + refine ike_liftE ?_ + unfold NonDet.when + split + · exact ik_mapMx (fun _ => uk_setVcpuV _ _) _ + · exact ik_pure _ + +/-! ## The frame batch -/ + +theorem iw_performFrameBatchVF (bi : Fl4FrameBatchInvocation) (σ : State Unit) (hI : SI N t σ) + (hok : FbOKN (uV σ) bi) : ∀ x ∈ (performFrameBatchVF (Z := Unit) bi σ).1, SI N t x.2 := by + intro x hx + refine ⟨p3_performFrameBatchVF bi σ hI.1 hok x hx, + (p3_performFrameBatchVF (N := fun _ => t) bi σ ⟨hI.1.1, hI.2.1⟩ hok x hx).2, ?_⟩ + have nn : ∀ (a : ArchCap), (.archObjectCap a : Cap) ≠ .nullCap := fun a e => by cases e + refine (fbq_runV (N := fun _ => t) (RV := IVV t) bi ?_ ?_ ?_ ?_ σ ⟨⟨hI.1.1, hI.2.1⟩, hok⟩ hI.2.2 x hx).2 + · intro s c src dest c0 hs hrs hD _ _ _ _ x hx + obtain ⟨s1, c1, hs1, hr1, -, -⟩ := hD + exact ivk_capInsert (Or.inl (notIV_of_cov hs.1.2 hs1 (capRng_sub_covR _ (sRefs_sub_capRng _ hr1)))) s hrs x hx + · intro s pi sl dev p R mt sz m hs hrs hok hpg hcap x hx + have hS : ∀ sl', ArchWr (.invokePage pi) sl' → NotIV t sl' := by + intro sl' hw + cases pi with + | pageMap c s0 en vs => + simp only [ArchWr] at hw; simp only [PgSl] at hpg; subst hw; subst hpg + exact notIV_of_held hrs hcap (nn _) + | pageUnmap a s0 => + simp only [ArchWr] at hw; subst hw + exact notIV_of_held hrs (hok : capAt s _ = some (.archObjectCap a)) (nn _) + | pageGetAddr _ => simp only [ArchWr] at hw + exact ivk_archPerform (.invokePage pi) (fun a e => by cases e) hS s hrs _ (fb_mem_archPage hx) + · intro s sl dev p R mt sz m hs hrs hc x hx a ha + rcases fb_emptySlot_nul hx (t, tcbCnodeIndex 1) with e | ⟨-, e⟩ + · rw [e] at ha; exact hrs a ha + · rw [e] at ha; exact (Option.some.inj ha).symm + · intro s dev p R mt sz m sl hs hrs hc x hx + exact ivk_setCap (Or.inl (notIV_of_held hrs hc (nn _))) s hrs x hx + +theorem sg_performFrameBatchVF (bi : Fl4FrameBatchInvocation) (σ : State Unit) (hI : S N M σ) + (hok : FbOKN (uV σ) bi) : ∀ x ∈ (performFrameBatchVF (Z := Unit) bi σ).1, S N M x.2 := by + intro x hx + refine ⟨p3_performFrameBatchVF bi σ hI.1 hok x hx, + (p3_performFrameBatchVF (N := M) bi σ ⟨hI.1.1, hI.2.1⟩ hok x hx).2, ?_⟩ + have nn : ∀ (a : ArchCap), ¬ NtfnOrNull (.archObjectCap a) := fun a => by rintro (e | ⟨_, _, _, e⟩) <;> cases e + refine (fbq_runV (N := N) (RV := NOV N) bi ?_ ?_ ?_ ?_ σ ⟨hI.1, hok⟩ hI.2.2 x hx).2 + · intro s c src dest c0 hs hrs hD _ _ _ _ x hx + exact nvk_capInsert (Or.inl (notNode_of_destRef hs.1.2 hD)) s hrs x hx + · intro s pi sl dev p R mt sz m hs hrs hok hpg hcap x hx + have hS : ∀ sl', ArchWr (.invokePage pi) sl' → NotNode N sl' := by + intro sl' hw + cases pi with + | pageMap c s0 en vs => + simp only [ArchWr] at hw; simp only [PgSl] at hpg; subst hw; subst hpg + exact notNode_of_held hrs hcap (nn _) + | pageUnmap a s0 => + simp only [ArchWr] at hw; subst hw + exact notNode_of_held hrs (hok : capAt s _ = some (.archObjectCap a)) (nn _) + | pageGetAddr _ => simp only [ArchWr] at hw + exact nvk_archPerform (.invokePage pi) (fun a e => by cases e) hS s hrs _ (fb_mem_archPage hx) + · intro s sl dev p R mt sz m hs hrs hc x hx irq a ha + rcases fb_emptySlot_nul hx (N irq, []) with e | ⟨-, e⟩ + · rw [e] at ha; exact hrs irq a ha + · rw [e] at ha; exact Or.inl (Option.some.inj ha).symm + · intro s dev p R mt sz m sl hs hrs hc x hx + exact nvk_setCap (Or.inl (notNode_of_held hrs hc (nn _))) s hrs x hx + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinArmVgB.lean b/proofs/Fl4Proofs/P3/VmFinArmVgB.lean new file mode 100644 index 000000000..e8aaa064d --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmVgB.lean @@ -0,0 +1,187 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c), VG arms, part B): `SG N M t := SI N t ∧ S N M` across the VM arms of +the extended dispatcher (spec only): the VM signal (`uk_sendSignalVF`: the untyped view is kept), the VCPU arms +(`sg_performVcpuInvocationF`; EnableIOPort's `set_cap` of the owned I/O-port cap over the cap in its slot), the EPT arms +(`sg_performEptInvocation`; each `set_cap` rewrites the slot's own EPT / frame cap, `RefEq`), and SetEPTRoot +(`sg_invokeSetEptRootF`). The slot facts are the VM decoders' (`VmFinDecode`'s `EptDec`, `VcpuDec`), read at the perform +state; SetEPTRoot's record `SetEptOK` adds the internal hypothesis that the target's slot 5 holds null or a level-0 EPT +cap (the concrete `EptSlotOK`, P3/EptCore) and that the source slot is not that slot. No named premise. +-/ +import Fl4Proofs.P3.VmFinArmVgA +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvG Fl4Proofs.P3.SInvIV + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {N M : Irq → ObjRef} {t : ObjRef} + +/-- The two families together. -/ +def SG (N M : Irq → ObjRef) (t : ObjRef) (σ : State Unit) : Prop := SI N t σ ∧ S N M σ + +theorem sg_of_uk {α : Type} {m : NonDet (State Unit) α} (h : ∀ v, UK v m) : IK (SG N M t) m := + fun σ hσ y hy => ⟨iw_of_uk h σ hσ.1 y hy, s_of_uk h σ hσ.2 y hy⟩ + +theorem sg_view {σ σ' : State Unit} (e : uV σ' = uV σ) (h : SG N M t σ) : SG N M t σ' := + ⟨iw_of_view e h.1, s_of_view e h.2⟩ + +theorem capAt_view {σ σ' : State Unit} (e : uV σ' = uV σ) (sl : CslotPtr) : capAt σ' sl = capAt σ sl := by + have := congrArg (fun w : UView => w.2.1 sl) e + simpa only [uV] using this + +/-- **A `set_cap` of an arch cap over an arch cap with the same object and range** keeps `SG`. -/ +theorem sg_setCapArch {a0 a : ArchCap} (sl : CslotPtr) (ho : aobjRef a = aobjRef a0) (hr : acRng a = acRng a0) + (σ : State Unit) (hσ : SG N M t σ) (hc : capAt σ sl = some (.archObjectCap a0)) : + ∀ y ∈ (setCap (Z := Unit) (.archObjectCap a) sl σ).1, SG N M t y.2 := by + intro y hy + have hre : RefEq (.archObjectCap a0) (.archObjectCap a) := refEq_arch ho hr + have nn : ¬ NtfnOrNull (.archObjectCap a0) := by rintro (e | ⟨_, _, _, e⟩) <;> cases e + obtain ⟨hI, hS⟩ := hσ + refine ⟨⟨hv_setCapRE (N := N) (nnu_arch a) sl σ ⟨hI.1, hc, hre⟩ y hy, + (hv_setCapRE (N := fun _ => t) (nnu_arch a) sl σ ⟨⟨hI.1.1, hI.2.1⟩, hc, hre⟩ y hy).2, + ivk_setCap (Or.inl (notIV_of_held hI.2.2 hc (by intro e; cases e))) σ hI.2.2 y hy⟩, + hv_setCapRE (N := N) (nnu_arch a) sl σ ⟨hS.1, hc, hre⟩ y hy, + (hv_setCapRE (N := M) (nnu_arch a) sl σ ⟨⟨hS.1.1, hS.2.1⟩, hc, hre⟩ y hy).2, + nvk_setCap (Or.inl (notNode_of_held hS.2.2 hc nn)) σ hS.2.2 y hy⟩ + +/-! ## The VM signal -/ + +theorem uk_sendSignalVF {v : UView} (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + UK v (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + unfold sendSignalVF vmSignalCompletes Fl4Spec.vmFullReply + repeat' (first + | (with_reducible exact Fl4Proofs.P3.VmFin.uk_vmReportF _ _) + | (with_reducible exact Fl4Proofs.P3.VmFin.uk_getVcpuV _) + | (with_reducible exact uk_sendSignal _ _) + | uk_leaf | ik_step0 | ik_unfold | (unfold NonDet.when) | (unfold NonDet.unless) | split) + +/-! ## The VCPU arms -/ + +/-- What a decoded VCPU invocation names, at the perform state. -/ +def VcpuOK (σ : State Unit) (i : VcpuInvocation) : Prop := + ∃ v cap slot ex, Fl4Proofs.P3.VmFin.VcpuDec v ex i ∧ ∀ p ∈ (cap, slot) :: ex, capAt σ p.2 = some p.1 + +theorem sg_performVcpuInvocationF (i : VcpuInvocation) (σ : State Unit) (hσ : SG N M t σ) (h : VcpuOK σ i) : + ∀ y ∈ (performVcpuInvocationF (Z := Unit) i σ).1, SG N M t y.2 := by + intro y hy + cases i with + | enableIoPort v lo hi c slot => + obtain ⟨w, cap, sl0, ex, hd, hl⟩ := h + obtain ⟨-, ⟨f, l, o, rfl⟩, rest, e⟩ := hd + have h0 : capAt σ slot = some (.archObjectCap (.ioPortCap f l o)) := + hl (_, _) (List.mem_cons_of_mem _ (by rw [e]; exact List.mem_cons_self ..)) + unfold performVcpuInvocationF at hy + dsimp only at hy + obtain ⟨a, ha, hy⟩ := hy + have e1 : uV a.2 = uV σ := Fl4Proofs.P3.VmFin.uk_ioPortRevoke (v := uV σ) _ σ rfl a ha + obtain ⟨b, hb, hy⟩ := hy + have h2 : SG N M t b.2 := by + simp only [ioPortSetOwner] at hb + exact sg_setCapArch slot (by rfl) (by rfl) a.2 (sg_view e1 hσ) ((capAt_view e1 slot).trans h0) b hb + exact (sg_of_uk fun _ => ik_bind (Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _) fun _ => ik_pure _) b.2 h2 y hy + | setTcb v t' => + refine (sg_of_uk fun _ => ?_) σ hσ y hy + unfold performVcpuInvocationF associateVcpuTcbF + dsimp only + refine ik_bind ?_ fun _ => ik_pure _ + refine ik_bind (Fl4Proofs.P3.VmFin.uk_vcpuDetachF _) fun _ => ?_ + refine ik_bind (Fl4Proofs.P3.VmFin.uk_getVcpuV _) fun st => ?_ + cases st.tcb with + | none => exact Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _ + | some old => exact ik_bind (Fl4Proofs.P3.VmFin.uk_vcpuDetachF _) fun _ => Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _ + | _ => + refine (sg_of_uk fun _ => ?_) σ hσ y hy + unfold performVcpuInvocationF + dsimp only + repeat' (first + | exact ik_pure _ + | exact Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _ + | exact Fl4Proofs.P3.VmFin.uk_getVcpuV _ + | refine ik_bind ?_ fun _ => ?_) + +/-! ## The EPT arms -/ + +/-- What a decoded guest page-table invocation names, at the perform state. -/ +def EptOK (σ : State Unit) (i : EptInvocation) : Prop := + ∃ cap slot ex, Fl4Proofs.P3.VmFin.EptDec cap slot ex i ∧ ∀ p ∈ (cap, slot) :: ex, capAt σ p.2 = some p.1 + +theorem sg_performEptInvocation (i : EptInvocation) (σ : State Unit) (hσ : SG N M t σ) (h : EptOK σ i) : + ∀ y ∈ (performEptInvocation (Z := Unit) i σ).1, SG N M t y.2 := by + obtain ⟨cap, slot0, ex, hd, hl⟩ := h + have h0 := hl (cap, slot0) (List.mem_cons_self ..) + intro y hy + cases i with + | tableMap ptr level slot tbl i asid gpa => + obtain ⟨rfl, rfl, -⟩ := hd + unfold performEptInvocation at hy + obtain ⟨a, ha, hy⟩ := hy + have e := Fl4Proofs.P3.VmFin.uk_storeGuestEntry (v := uV σ) _ _ _ σ rfl a ha + exact sg_setCapArch slot (by rfl) (by rfl) a.2 (sg_view e hσ) ((capAt_view e slot).trans h0) y hy + | tableUnmap ptr level mapped slot => + obtain ⟨rfl, rfl, -⟩ := hd + unfold performEptInvocation at hy + dsimp only at hy + cases mapped with + | none => exact ik_pure () σ hσ y hy + | some p => + obtain ⟨asid, gpa⟩ := p + dsimp only at hy + obtain ⟨a, ha, hy⟩ := hy + have e1 := Fl4Proofs.P3.VmFin.uk_guestUnmap (v := uV σ) _ _ _ _ _ σ rfl a ha + obtain ⟨b, hb, hy⟩ := hy + have e2 : uV b.2 = uV σ := (ik_of_keepSt (keepSt_getEpt ptr) (fun s => uV s = uV σ)) a.2 e1 b hb + obtain ⟨c, hc, hy⟩ := hy + have e3 : uV c.2 = uV σ := (Fl4Proofs.P3.VmFin.uk_setArch (v := uV σ) _ _) b.2 e2 c hc + exact sg_setCapArch slot (by rfl) (by rfl) c.2 (sg_view e3 hσ) ((capAt_view e3 slot).trans h0) y hy + | frameMap newCap slot tbl i entry => + obtain ⟨rfl, dev, p, R, mt, sz, mt', m', rfl, rfl⟩ := hd + unfold performEptInvocation at hy + obtain ⟨a, ha, hy⟩ := hy + have h1 := sg_setCapArch (N := N) (M := M) (t := t) (a0 := .pageCap dev p R mt sz none) + (a := .pageCap dev p R mt' sz m') slot (by rfl) (by rfl) σ hσ h0 a ha + exact (sg_of_uk fun _ => Fl4Proofs.P3.VmFin.uk_storeGuestEntry _ _ _) a.2 h1 y hy + | frameUnmap c slot => + obtain ⟨rfl, rfl, dev, p, R, sz, m, rfl⟩ := hd + unfold performEptInvocation at hy + dsimp only at hy + cases m with + | none => exact ik_pure () σ hσ y hy + | some q => + obtain ⟨asid, gpa⟩ := q + dsimp only at hy + obtain ⟨a, ha, hy⟩ := hy + have pre : ∀ v, UK v (NonDet.when (asid ≠ 0) + (guestUnmap (Z := Unit) asid gpa p (pageBitsForSize sz) false)) := by + intro v + unfold NonDet.when + split + · exact Fl4Proofs.P3.VmFin.uk_guestUnmap _ _ _ _ _ + · exact ik_pure _ + have e1 : uV a.2 = uV σ := pre (uV σ) σ rfl a ha + exact sg_setCapArch slot (by rfl) (by rfl) a.2 (sg_view e1 hσ) ((capAt_view e1 slot).trans h0) y hy + | poolAssign asid pool slot => + unfold performEptInvocation at hy + obtain ⟨a, ha, hy⟩ := hy + obtain ⟨e, hca⟩ := getCap_capAt ha + rw [e] at hy + obtain ⟨c, s1⟩ := a + cases c with + | archObjectCap ac => + cases ac with + | eptCap r level mapped => + cases level with + | zero => + obtain ⟨b, hb, hy⟩ := hy + have h1 := sg_setCapArch (N := N) (M := M) (t := t) (a0 := .eptCap r 0 mapped) + (a := .eptCap r 0 (some (asid, 0))) slot (by rfl) (by rfl) σ hσ hca b hb + exact (sg_of_uk fun _ => uk_storeAsidPoolEntry _ _ _) b.2 h1 y hy + | succ n => exact hy.elim + | _ => exact hy.elim + | _ => exact hy.elim + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinArmVgC.lean b/proofs/Fl4Proofs/P3/VmFinArmVgC.lean new file mode 100644 index 000000000..94bd7cf31 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinArmVgC.lean @@ -0,0 +1,415 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c), VG arms, part C): SetEPTRoot and the assembled tops (spec only). + +* `capDeleteVF_keep0`: a `capDeleteVF` of a slot holding null or a level-0 EPT cap writes no other slot (its + finaliser keeps the untyped view and returns `(nullCap, nullCap)`; then `empty_slot`). +* `sg_invokeSetEptRootF`: SetEPTRoot keeps `SG N M t` under `SetEptOK` (the target's slot 5 holds null or a level-0 EPT + cap — derived at the call site from the concrete `EptSlotOK`, P3/EptCore, an internal hypothesis; the source slot holds + the inserted level-0 EPT cap and is not slot 5; a thread cap to the target is held, so slot 5 is a lookup + destination). +* `nk_/idk_fl4PerformInvocationVF`: the extended dispatcher keeps the IRQ node and the idle thread. +* **`sg_fl4PerformInvocationVF`** (`SI N t ∧ S N M` from a state whose node is `N`) and **`vg_fl4PerformInvocationVF`** + (VmFinVF's bundle `VG` = `SpecGlobS ∧ IdleOutV ∧ UtInv3 ∧ IdleVtS`): the base facts `InvOKN`, `DecodedFrom` asked of the + `.base` invocations only (as `fo_/il_fl4PerformInvocationVF`), the VM decoders' slot facts `EptOK` / `VcpuOK` and the + SetEPTRoot record `SetEptOK` at the perform state. + +No named premise. +-/ +import Fl4Proofs.P3.VmFinArmVgB +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvG Fl4Proofs.P3.SInvIV + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {N M : Irq → ObjRef} {t : ObjRef} + +/-! ## `capDeleteVF` of a slot holding null or a level-0 EPT cap -/ + +/-- Every outcome of `capDeleteVF` of a slot whose cap finalises to `(nullCap, nullCap)` is its finaliser, then +`empty_slot` (the forward half of `capDeleteVF_frame_mem`, for any such cap). -/ +theorem capDeleteVF_rm_mem {sl : CslotPtr} {s t' : State Unit} {c : Cap} {r : Except Unit Unit} + (hget : getCap (Z := Unit) sl s = ({(c, s)}, False)) (hc : c ≠ .nullCap) + (hp : ∀ fin (y : (Cap × Cap) × State Unit), y ∈ (finaliseCapVF (Z := Unit) c fin s).1 → y.1 = (.nullCap, .nullCap)) + (h : (r, t') ∈ ((capDeleteVF (Z := Unit) sl).run s).1) : + ∃ u, ((.nullCap, .nullCap), u) ∈ (finaliseCapVF (Z := Unit) c (decide (isFinalCap' c s)) s).1 ∧ + ((), t') ∈ (emptySlot (Z := Unit) sl .nullCap u).1 := by + unfold capDeleteVF at h + obtain ⟨x, hx, hnext⟩ := fbx_mem_bindE h + rw [recDelVF_step] at hx + unfold recDelFVF withoutPreemption at hx + obtain ⟨y, hy, hbody⟩ := fbx_mem_bindE hx + rw [recDelVF_step] at hy + unfold recDelFVF withoutPreemption at hy + rw [Fl4Proofs.P3.Vs.runE_bind_ok (Fl4Proofs.P3.Vs.runE_liftE hget), ite_eq_right hc] at hy + rw [Fl4Proofs.P3.Vs.runE_bind_ok (Fl4Proofs.P3.Vs.runE_liftE (m := isFinalCap (Z := Unit) c) rfl)] at hy + obtain ⟨z, hz, hznext⟩ := fbx_mem_bindE hy + obtain ⟨⟨pair, u⟩, hw, rfl⟩ := fbx_mem_liftE hz + have hpair := hp _ _ hw + dsimp only at hpair + subst pair + rcases hznext with ⟨q, epair, hsuc⟩ | ⟨_, he, _⟩ + swap + · cases he + simp only [Except.ok.injEq] at epair + subst q + simp only [capRemoveable, ↓reduceIte] at hsuc + obtain rfl := Set.mem_singleton_iff.1 hsuc + rcases hbody with ⟨q, epair, hbody⟩ | ⟨_, he, _⟩ + swap + · cases he + simp only [Except.ok.injEq] at epair + subst q + simp only [true_or, NonDet.when, ↓reduceIte] at hbody + obtain ⟨q, hq, hqnext⟩ := fbx_mem_bindE hbody + obtain ⟨⟨value, v⟩, hv, rfl⟩ := fbx_mem_liftE hq + cases value + rcases hqnext with ⟨_, _, hret⟩ | ⟨_, he', _⟩ + · have hx' := Set.mem_singleton_iff.1 hret + rw [hx'] at hnext + rcases hnext with ⟨_, _, hr⟩ | ⟨_, he, _⟩ + · obtain ⟨-, ht⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hr) + subst t' + exact ⟨u, hw, hv⟩ + · cases he + · cases he' + +/-- Every outcome of `capDeleteVF` of an empty slot is `empty_slot`'s. -/ +theorem capDeleteVF_null_mem {sl : CslotPtr} {s t' : State Unit} {r : Except Unit Unit} + (hget : getCap (Z := Unit) sl s = ({(.nullCap, s)}, False)) + (h : (r, t') ∈ ((capDeleteVF (Z := Unit) sl).run s).1) : + ((), t') ∈ (emptySlot (Z := Unit) sl .nullCap s).1 := by + unfold capDeleteVF at h + obtain ⟨x, hx, hnext⟩ := fbx_mem_bindE h + rw [recDelVF_step] at hx + unfold recDelFVF withoutPreemption at hx + obtain ⟨y, hy, hbody⟩ := fbx_mem_bindE hx + rw [recDelVF_step] at hy + unfold recDelFVF withoutPreemption at hy + rw [Fl4Proofs.P3.Vs.runE_bind_ok (Fl4Proofs.P3.Vs.runE_liftE hget), if_pos rfl] at hy + obtain rfl := Set.mem_singleton_iff.1 hy + rcases hbody with ⟨q, epair, hbody⟩ | ⟨_, he, _⟩ + swap + · cases he + simp only [Except.ok.injEq] at epair + subst q + simp only [true_or, NonDet.when, ↓reduceIte] at hbody + obtain ⟨q, hq, hqnext⟩ := fbx_mem_bindE hbody + obtain ⟨⟨value, v⟩, hv, rfl⟩ := fbx_mem_liftE hq + cases value + rcases hqnext with ⟨_, _, hret⟩ | ⟨_, he', _⟩ + · have hx' := Set.mem_singleton_iff.1 hret + rw [hx'] at hnext + rcases hnext with ⟨_, _, hr⟩ | ⟨_, he, _⟩ + · obtain ⟨-, ht⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hr) + subst t' + exact hv + · cases he + · cases he' + +theorem finaliseCapVF_ept0 (p : ObjRef) (m : Option (Asid × VSpaceRef)) (fin : Bool) : + finaliseCapVF (Z := Unit) (.archObjectCap (.eptCap p 0 m)) fin = + archFinaliseCap (.eptCap p 0 m) fin := by + rcases m with _ | ⟨a, g⟩ + · rfl + · show (if fin = true ∧ (0 : Nat) ≠ 0 then _ else _) = _ + rw [if_neg (by simp)] + rfl + +theorem ept0_pair {p : ObjRef} {m : Option (Asid × VSpaceRef)} {fin : Bool} {s : State Unit} + {y : (Cap × Cap) × State Unit} (hy : y ∈ (finaliseCapVF (Z := Unit) (.archObjectCap (.eptCap p 0 m)) fin s).1) : + y.1 = (.nullCap, .nullCap) := by + rw [finaliseCapVF_ept0] at hy + rcases m with _ | ⟨a, g⟩ <;> cases fin + all_goals first + | (rw [Set.mem_singleton_iff.1 hy]) + | (obtain ⟨z, -, hx⟩ := hy; rw [Set.mem_singleton_iff.1 hx]) + +/-- The slot holds null or a level-0 EPT cap. -/ +def NullOrEpt0 (σ : State Unit) (sl : CslotPtr) : Prop := + capAt σ sl = some .nullCap ∨ ∃ p m, capAt σ sl = some (.archObjectCap (.eptCap p 0 m)) + +/-- **`capDeleteVF` of a slot holding null or a level-0 EPT cap writes no other slot.** -/ +theorem capDeleteVF_keep0 {sl : CslotPtr} {s : State Unit} (hc : NullOrEpt0 s sl) : + ∀ y ∈ ((capDeleteVF (Z := Unit) sl).run s).1, ∀ x, x ≠ sl → capAt y.2 x = capAt s x := by + intro y hy x hx + obtain ⟨r, t'⟩ := y + rcases hc with hc | ⟨p, m, hc⟩ + · have he := capDeleteVF_null_mem (getCap_det hc) hy + rcases fb_emptySlot_nul he x with e | ⟨e, -⟩ + · exact e + · exact absurd e hx + · obtain ⟨u, hu, he⟩ := capDeleteVF_rm_mem (getCap_det hc) (by intro e; cases e) + (fun fin z hz => ept0_pair hz) hy + have e1 : uV u = uV s := by + rw [finaliseCapVF_ept0] at hu + exact uk_archFinaliseCap (uvw := uV s) _ _ s rfl _ hu + rcases fb_emptySlot_nul he x with e | ⟨e, -⟩ + · exact e.trans (capAt_view e1 x) + · exact absurd e hx + +/-! ## SetEPTRoot -/ + +/-- What SetEPTRoot needs at its perform state (module docstring). -/ +structure SetEptOK (σ : State Unit) (i : SetEptRootInvocation) : Prop where + slot5 : NullOrEpt0 σ (i.target, tcbCnodeIndex 5) + src : capAt σ i.srcSlot = some i.newCap + newEpt : ∃ r m, i.newCap = .archObjectCap (.eptCap r 0 m) + srcNe : i.srcSlot ≠ (i.target, tcbCnodeIndex 5) + live : ∃ s0, capAt σ s0 = some (.threadCap i.target) + +theorem sg_invokeSetEptRootF (i : SetEptRootInvocation) (σ : State Unit) (hσ : SG N M t σ) (h : SetEptOK σ i) : + ∀ y ∈ ((invokeSetEptRootF (Z := Unit) i).run σ).1, SG N M t y.2 := by + intro y hy + have hdel : ∀ z ∈ ((capDeleteVF (Z := Unit) (i.target, tcbCnodeIndex 5)).run σ).1, SG N M t z.2 := + fun z hz => ⟨iw_capDeleteVF _ σ hσ.1 z hz, sg_capDeleteVF _ σ hσ.2 z hz⟩ + unfold invokeSetEptRootF at hy + obtain ⟨z, hz, ⟨a, -, hy⟩ | ⟨e, -, rfl⟩⟩ := fbx_mem_bindE hy + swap + · exact hdel z hz + have hz1 := hdel z hz + have keep := capDeleteVF_keep0 h.slot5 z hz + obtain ⟨s0, hs0⟩ := h.live + have hs0ne : s0 ≠ (i.target, tcbCnodeIndex 5) := by + intro e; subst e + rcases h.slot5 with h5 | ⟨p, m, h5⟩ <;> rw [h5] at hs0 <;> cases hs0 + have hsrc : capAt z.2 i.srcSlot = some i.newCap := (keep _ h.srcNe).trans h.src + have hs0' : capAt z.2 s0 = some (.threadCap i.target) := (keep _ hs0ne).trans hs0 + obtain ⟨x, hx, rfl⟩ := fbx_mem_liftE hy + obtain ⟨r, m, hn⟩ := h.newEpt + have hR : RefRel i.newCap i.newCap := by + rw [hn] + exact ⟨fun _ h => h, rfl, fun _ _ _ e => (by cases e), fun _ _ _ e => (by cases e), fun _ _ _ _ e => (by cases e)⟩ + have hD : DestRefV (uV z.2) (i.target, tcbCnodeIndex 5) := + ⟨s0, _, hs0', (by simp [sRefs]), fun _ _ _ e => (by cases e), ⟨5, (by decide), rfl⟩⟩ + have hP : PosOK (uV z.2) (i.target, tcbCnodeIndex 5) := posOK_of_destRef hz1.1.1.1.2 hD + exact ⟨hv_capInsertSI _ _ _ z.2 ⟨hz1.1, ⟨_, hsrc, Or.inr hR⟩, hP, Or.inl (notIV_of_destRef hz1.1.2.1 hD)⟩ x hx, + hv_capInsertS _ _ _ z.2 ⟨hz1.2, ⟨_, hsrc, Or.inr hR⟩, hP, Or.inl (notNode_of_destRef hz1.2.1.2 hD)⟩ x hx⟩ + +/-! ## The IRQ node and the idle thread -/ + +theorem nk_of_gkV {α : Type} {m : NonDet (State Unit) α} (h : Fl4Proofs.P3.Vs.GK m) : NK N m := + fun _ hσ _ hy => (h.inode hy).trans hσ + +theorem idk_of_gkV {K : ObjRef} {α : Type} {m : NonDet (State Unit) α} (h : Fl4Proofs.P3.Vs.GK m) : IdK K m := + fun _ hσ _ hy => (h.idle hy).trans hσ + +theorem gkV_guestUnmap (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + Fl4Proofs.P3.Vs.GK (guestUnmap (Z := Unit) asid gpa ptr level table) := + fk_guestUnmap (fun p _ => Fl4Proofs.P3.Vs.gk_setObject p _) _ _ _ _ _ + +theorem gkV_ioPortRevoke (c : ArchCap) : Fl4Proofs.P3.Vs.GK (ioPortRevoke (Z := Unit) c) := + fk_ioPortRevoke (fun p _ => Fl4Proofs.P3.Vs.gk_setObject p _) _ + +theorem nk_capDeleteVF (slot : CslotPtr) : NKE N (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact nk_capDeleteG (Fl4Proofs.P3.VmFin.nk_VF N) slot + +theorem nk_capRevokeVF (slot : CslotPtr) : NKE N (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact nk_capRevokeG (Fl4Proofs.P3.VmFin.nk_VF N) slot + +theorem idk_capDeleteVF {K : ObjRef} (slot : CslotPtr) : IdKE K (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact idk_capDeleteG (Fl4Proofs.P3.VmFin.idk_VF K) slot + +theorem idk_capRevokeVF {K : ObjRef} (slot : CslotPtr) : IdKE K (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact idk_capRevokeG (Fl4Proofs.P3.VmFin.idk_VF K) slot + +macro_rules | `(tactic| nk_leaf) => `(tactic| with_reducible exact nk_capDeleteVF _) +macro_rules | `(tactic| nk_leaf) => `(tactic| with_reducible exact nk_capRevokeVF _) +macro_rules | `(tactic| nk_leaf) => `(tactic| exact nk_of_gkV (gk_vmReportF _ _)) +macro_rules | `(tactic| nk_leaf) => `(tactic| exact nk_of_gkV (gk_getVcpuV _)) +macro_rules | `(tactic| nk_leaf) => `(tactic| exact nk_of_gkV (gk_vcpuUpdateV _ _)) +macro_rules | `(tactic| nk_leaf) => `(tactic| exact nk_of_gkV (gk_vcpuDetachF _)) +macro_rules | `(tactic| nk_leaf) => `(tactic| exact nk_of_gkV (gkV_guestUnmap _ _ _ _ _)) +macro_rules | `(tactic| nk_leaf) => `(tactic| exact nk_of_gkV (gkV_ioPortRevoke _)) +macro_rules | `(tactic| idk_leaf) => `(tactic| with_reducible exact idk_capDeleteVF _) +macro_rules | `(tactic| idk_leaf) => `(tactic| with_reducible exact idk_capRevokeVF _) +macro_rules | `(tactic| idk_leaf) => `(tactic| exact idk_of_gkV (gk_vmReportF _ _)) +macro_rules | `(tactic| idk_leaf) => `(tactic| exact idk_of_gkV (gk_getVcpuV _)) +macro_rules | `(tactic| idk_leaf) => `(tactic| exact idk_of_gkV (gk_vcpuUpdateV _ _)) +macro_rules | `(tactic| idk_leaf) => `(tactic| exact idk_of_gkV (gk_vcpuDetachF _)) +macro_rules | `(tactic| idk_leaf) => `(tactic| exact idk_of_gkV (gkV_guestUnmap _ _ _ _ _)) +macro_rules | `(tactic| idk_leaf) => `(tactic| exact idk_of_gkV (gkV_ioPortRevoke _)) + +theorem nk_performFrameBatchVF (b : Fl4FrameBatchInvocation) : NK N (performFrameBatchVF (Z := Unit) b) := by + unfold performFrameBatchVF + refine ik_performFrameBatchWith (fun w => ?_) (by unfold flushAll; nk_auto) + refine ik_fbEntryWith b w (fun _ => by unfold fbEntry; nk_auto) (by nk_auto) (fun _ => by nk_auto) + (fun _ _ _ _ _ _ _ => ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun va => by unfold unmapPageUnflushed; nk_auto) + (by nk_auto)) + (fun sl => nk_capDeleteVF sl) + +theorem idk_performFrameBatchVF {K : ObjRef} (b : Fl4FrameBatchInvocation) : + IdK K (performFrameBatchVF (Z := Unit) b) := by + unfold performFrameBatchVF + refine ik_performFrameBatchWith (fun w => ?_) (by unfold flushAll; idk_auto) + refine ik_fbEntryWith b w (fun _ => by unfold fbEntry; idk_auto) (by idk_auto) (fun _ => by idk_auto) + (fun _ _ _ _ _ _ _ => ik_fbDeferDelete _ _ _ _ _ _ _ _ (fun va => by unfold unmapPageUnflushed; idk_auto) + (by idk_auto)) + (fun sl => idk_capDeleteVF sl) + +set_option maxHeartbeats 8000000 in +theorem nk_fl4PerformInvocationVF (onCore remoteCur : ObjRef → Bool) (bl call don : Bool) (inv : Fl4InvocationV) : + NKE N (fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv) := by + cases inv with + | base b => + cases b + case invokeUntyped ui => simp only [fl4PerformInvocationVF, fl4InvokeUntypedV]; cases ui; unfold setVcpu; nk_auto + case invokeNotification n bd => + simp only [fl4PerformInvocationVF]; unfold sendSignalVF vmSignalCompletes Fl4Spec.vmFullReply; nk_auto + case invokeTCB ti => + cases ti + case threadControlCaps => simp only [fl4PerformInvocationVF, invokeTcbVF, installTcbCapVF, installTcbFrameCapVF]; nk_auto + case threadControlSched => simp only [fl4PerformInvocationVF, invokeTcbVF, installTcbCapVF]; nk_auto + all_goals (simp only [fl4PerformInvocationVF, invokeTcbVF]; exact nk_invokeTcb _) + case invokeCNode ci => + cases ci + case revokeCall s => simp only [fl4PerformInvocationVF, invokeCnodeVF]; nk_auto + case deleteCall s => simp only [fl4PerformInvocationVF, invokeCnodeVF]; nk_auto + all_goals (simp only [fl4PerformInvocationVF, invokeCnodeVF]; nk_auto) + case invokeArchObject i => + cases i with + | frameBatch bi => simp only [fl4PerformInvocationVF]; exact ike_liftE (nk_performFrameBatchVF bi) + | _ => exact nk_fl4PerformInvocation _ _ _ _ + all_goals exact nk_fl4PerformInvocation _ _ _ _ + | vcpu i => + simp only [fl4PerformInvocationVF] + refine ike_liftE ?_ + cases i <;> simp only [performVcpuInvocationF, associateVcpuTcbF, ioPortSetOwner] <;> nk_auto + | ept i => + simp only [fl4PerformInvocationVF] + refine ike_liftE ?_ + cases i <;> simp only [performEptInvocation, storeGuestEntry] <;> nk_auto + | setEptRoot i => simp only [fl4PerformInvocationVF, invokeSetEptRootF]; nk_auto + +set_option maxHeartbeats 8000000 in +theorem idk_fl4PerformInvocationVF {K : ObjRef} (onCore remoteCur : ObjRef → Bool) (bl call don : Bool) + (inv : Fl4InvocationV) : IdKE K (fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv) := by + cases inv with + | base b => + cases b + case invokeUntyped ui => simp only [fl4PerformInvocationVF, fl4InvokeUntypedV]; cases ui; unfold setVcpu; idk_auto + case invokeNotification n bd => + simp only [fl4PerformInvocationVF]; unfold sendSignalVF vmSignalCompletes Fl4Spec.vmFullReply; idk_auto + case invokeTCB ti => + cases ti + case threadControlCaps => simp only [fl4PerformInvocationVF, invokeTcbVF, installTcbCapVF, installTcbFrameCapVF]; idk_auto + case threadControlSched => simp only [fl4PerformInvocationVF, invokeTcbVF, installTcbCapVF]; idk_auto + all_goals (simp only [fl4PerformInvocationVF, invokeTcbVF]; exact idk_invokeTcb _) + case invokeCNode ci => + cases ci + case revokeCall s => simp only [fl4PerformInvocationVF, invokeCnodeVF]; idk_auto + case deleteCall s => simp only [fl4PerformInvocationVF, invokeCnodeVF]; idk_auto + all_goals (simp only [fl4PerformInvocationVF, invokeCnodeVF]; idk_auto) + case invokeArchObject i => + cases i with + | frameBatch bi => simp only [fl4PerformInvocationVF]; exact ike_liftE (idk_performFrameBatchVF bi) + | _ => exact idk_fl4PerformInvocation _ _ _ _ + all_goals exact idk_fl4PerformInvocation _ _ _ _ + | vcpu i => + simp only [fl4PerformInvocationVF] + refine ike_liftE ?_ + cases i <;> simp only [performVcpuInvocationF, associateVcpuTcbF, ioPortSetOwner] <;> idk_auto + | ept i => + simp only [fl4PerformInvocationVF] + refine ike_liftE ?_ + cases i <;> simp only [performEptInvocation, storeGuestEntry] <;> idk_auto + | setEptRoot i => simp only [fl4PerformInvocationVF, invokeSetEptRootF]; idk_auto + +/-! ## The tops -/ + +set_option maxHeartbeats 8000000 in +/-- **The extended dispatcher keeps `SI N t ∧ S N M`** from a state whose IRQ node is `N` (module docstring). -/ +theorem sg_fl4PerformInvocationVF (onCore remoteCur : ObjRef → Bool) (bl call don : Bool) (inv : Fl4InvocationV) + (σ : State Unit) (hσ : SG N M t σ) (hN : NodeIs N σ) + (hB : ∀ i, inv = .base i → InvOKN (uV σ) i ∧ DecodedFrom (uV σ) i) + (hE : ∀ i, inv = .ept i → EptOK σ i) + (hV : ∀ i, inv = .vcpu i → VcpuOK σ i) + (hR : ∀ i, inv = .setEptRoot i → SetEptOK σ i) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv).run σ).1, SG N M t y.2 := by + cases inv with + | base b => + obtain ⟨hok, hD⟩ := hB b rfl + obtain ⟨fp0, label, args, ci, sl, cap, ex, buf, σ0, s0, hv, hc, hex, hmem⟩ := hD + have hg : GapOKN (uV σ) b := + okv_fl4DecodeG fp0 label args ci sl cap ex buf hc (fun p hp => (hex p hp).1) σ0 hv _ s0 hmem + have hs : SgOKN (uV σ) b := + okv_fl4DecodeS fp0 label args ci sl cap ex buf hc (fun p hp => (hex p hp).1) σ0 hv _ s0 hmem + have native : ∀ y ∈ ((fl4PerformInvocation (Z := Unit) bl call don b).run σ).1, SG N M t y.2 := + fun y hy => ⟨iwP_fl4Perform bl call don b σ hσ.1 hN hok hg hs y hy, + sgP_fl4Perform bl call don b σ hσ.2 hN hok hg hs y hy⟩ + cases b + case invokeUntyped ui => + intro y hy + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact ⟨iw_fl4InvokeUntypedV ui σ hσ.1 hg z hz, sg_fl4InvokeUntypedV ui σ hσ.2 hg z hz⟩ + case invokeNotification n bd => + intro y hy + refine (?_ : IKE (SG N M t) _) σ hσ y hy + simp only [fl4PerformInvocationVF, withoutPreemption] + exact ike_bind (ike_liftE (sg_of_uk fun _ => uk_sendSignalVF _ _ _ _)) fun _ => ike_returnOk _ + case invokeTCB ti => + intro y hy + simp only [fl4PerformInvocationVF] at hy + exact ⟨iw_invokeTcbVF ti σ hσ.1 y hy, sg_invokeTcbVF ti σ hσ.2 y hy⟩ + case invokeCNode ci => + intro y hy + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact ⟨iw_invokeCnodeVF ci σ hσ.1 hok hs z hz, sg_invokeCnodeVF ci σ hσ.2 hok hs z hz⟩ + case invokeArchObject i => + cases i with + | frameBatch bi => + intro y hy + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + exact ⟨iw_performFrameBatchVF bi σ hσ.1 hg x hx, sg_performFrameBatchVF bi σ hσ.2 hg x hx⟩ + | _ => exact native + all_goals exact native + | vcpu i => + intro y hy + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + exact sg_performVcpuInvocationF i σ hσ (hV i rfl) x hx + | ept i => + intro y hy + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + obtain ⟨a, ha, hx⟩ := hx + rw [Set.mem_singleton_iff.1 hx] + exact sg_performEptInvocation i σ hσ (hE i rfl) a ha + | setEptRoot i => + intro y hy + simp only [fl4PerformInvocationVF] at hy + obtain ⟨z, hz, e⟩ := mem_ret_bindE hy + rw [e] + exact sg_invokeSetEptRootF i σ hσ (hR i rfl) z hz + +/-- **The extended dispatcher keeps VmFinVF's bundle `VG`** (`SpecGlobS`, `IdleOutV`, `UtInv3`, `IdleVtS`). -/ +theorem vg_fl4PerformInvocationVF (onCore remoteCur : ObjRef → Bool) (bl call don : Bool) (inv : Fl4InvocationV) + (σ : State Unit) (hσ : Fl4Proofs.P3.VmFin.VG σ) + (hB : ∀ i, inv = .base i → InvOKN (uV σ) i ∧ DecodedFrom (uV σ) i) + (hE : ∀ i, inv = .ept i → EptOK σ i) + (hV : ∀ i, inv = .vcpu i → VcpuOK σ i) + (hR : ∀ i, inv = .setEptRoot i → SetEptOK σ i) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur bl call don inv).run σ).1, + Fl4Proofs.P3.VmFin.VG y.2 := by + intro y hy + obtain ⟨hG, hI, hU, hV0⟩ := hσ + obtain ⟨h1, h2⟩ := sg_fl4PerformInvocationVF (N := σ.interruptIrqNode) (M := fun _ => σ.idleThread) + (t := σ.idleThread) onCore remoteCur bl call don inv σ ⟨si_at hG hI hU hV0, S_of_specGlob hG hI hU⟩ rfl + hB hE hV hR y hy + have en : y.2.interruptIrqNode = σ.interruptIrqNode := nk_fl4PerformInvocationVF onCore remoteCur bl call don inv σ rfl y hy + have et : y.2.idleThread = σ.idleThread := idk_fl4PerformInvocationVF onCore remoteCur bl call don inv σ rfl y hy + rw [← en, ← et] at h1 h2 + obtain ⟨a, b⟩ := specGlob_of_S h2 + exact ⟨a, b, h2.1, h1.2.2⟩ + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinDecode.lean b/proofs/Fl4Proofs/P3/VmFinDecode.lean new file mode 100644 index 000000000..b2c2536de --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinDecode.lean @@ -0,0 +1,263 @@ +/- +P3 (line mmuAD, Wave 3 step (c)): **decode facts of the VM decoders** (spec only). Every successful outcome of +`decodeEptInvocation`, `decodePageMapEpt`, `decodeEptAsidPoolAssign`, `decodeVcpuInvocation` and `decodeSetEptRoot` +names the invoked slot / cap / extra caps it will write; `fl4DecodeInvocationV_vmDec` collects them for the extended +decoder, and `fl4DecodeInvocationV_base` says its `.base` outcomes are outcomes of the native decoder. No named premise. +-/ +import Fl4Spec.Ext.VirtDispatch +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-- Every `.ok` outcome of `m`, from any state, satisfies `P`. -/ +def OkR {ε α : Type} (P : α → Prop) (m : NonDetE (State Unit) ε α) : Prop := + ∀ σ a s, ((.ok a, s) : Except ε α × State Unit) ∈ (m.run σ).1 → P a + +section +variable {ε α β : Type} + +theorem okr_bind' {P : β → Prop} {Q : α → Prop} {m : NonDetE (State Unit) ε α} {g : α → NonDetE (State Unit) ε β} + (hm : OkR Q m) (hg : ∀ a, Q a → OkR P (g a)) : OkR P (m >>= g) := by + intro σ b s hb + rw [ExceptT.run_bind] at hb + obtain ⟨⟨r, s'⟩, hz, hb⟩ := hb + cases r with + | error e => + dsimp only at hb + have h := Set.mem_singleton_iff.1 hb; simp at h + | ok a => exact hg a (hm σ a s' hz) s' b s hb + +theorem okr_bind {P : β → Prop} {m : NonDetE (State Unit) ε α} {g : α → NonDetE (State Unit) ε β} + (hg : ∀ a, OkR P (g a)) : OkR P (m >>= g) := + okr_bind' (Q := fun _ => True) (fun _ _ _ _ => trivial) fun a _ => hg a + +theorem okr_returnOk {P : α → Prop} {a : α} (h : P a) : OkR P (NonDetE.returnOk a : NonDetE (State Unit) ε α) := by + intro σ b s hb + have e := Set.mem_singleton_iff.1 hb + simp only [Prod.mk.injEq, Except.ok.injEq] at e + obtain ⟨rfl, -⟩ := e; exact h + +theorem okr_throw {P : α → Prop} (e : ε) : OkR P (NonDetE.throwError e : NonDetE (State Unit) ε α) := by + intro σ _ s hb + have e := Set.mem_singleton_iff.1 hb; simp at e + +theorem okr_ite {P : α → Prop} {c : Prop} [Decidable c] {m1 m2 : NonDetE (State Unit) ε α} + (h1 : c → OkR P m1) (h2 : ¬ c → OkR P m2) : OkR P (if c then m1 else m2) := by + split + · exact h1 ‹_› + · exact h2 ‹_› + +theorem okr_liftME {P : β → Prop} {f : α → β} {m : NonDetE (State Unit) ε α} (h : OkR (fun a => P (f a)) m) : + OkR P (NonDetE.liftME f m) := by + unfold NonDetE.liftME + exact okr_bind' h fun _ ha => okr_returnOk ha + +end + +/-! ## The VM decoders -/ + +/-- What a decoded guest page-table invocation writes, in terms of the invoked cap, its slot and the extra caps. -/ +def EptDec (cap : Cap) (slot : CslotPtr) (ex : List (Cap × CslotPtr)) : EptInvocation → Prop + | .tableMap p l sl _ _ _ _ => sl = slot ∧ cap = .archObjectCap (.eptCap p l none) ∧ l ≠ 0 + | .tableUnmap p l m sl => sl = slot ∧ cap = .archObjectCap (.eptCap p l m) ∧ l ≠ 0 + | .frameMap newCap sl _ _ _ => sl = slot ∧ ∃ dev p R mt sz mt' m', + cap = .archObjectCap (.pageCap dev p R mt sz none) ∧ newCap = .pageCap dev p R mt' sz m' + | .frameUnmap c sl => sl = slot ∧ cap = .archObjectCap c ∧ ∃ dev p R sz m, c = .pageCap dev p R .vmEptMap sz m + | .poolAssign _ pool sl => (∃ b, cap = .archObjectCap (.asidPoolCap pool b)) ∧ + ∃ r rest, ex = (.archObjectCap (.eptCap r 0 none), sl) :: rest + +/-- What a decoded VCPU invocation names. -/ +def VcpuDec (v : ObjRef) (ex : List (Cap × CslotPtr)) : VcpuInvocation → Prop + | .setTcb v' t => v' = v ∧ ∃ s rest, ex = (.threadCap t, s) :: rest + | .enableIoPort v' _ _ c sl => v' = v ∧ (∃ f l o, c = .ioPortCap f l o) ∧ ∃ rest, ex = (.archObjectCap c, sl) :: rest + | .writeRegisters v' _ => v' = v + | .flushTlb v' => v' = v + | .readVmcs v' _ => v' = v + | .writeVmcs v' _ _ => v' = v + | .readVmcb v' _ _ => v' = v + | .writeVmcb v' _ _ => v' = v + | .readMsr v' _ => v' = v + | .readVmxMisc => True + | .writeMsr v' _ _ => v' = v + | .disableIoPort v' _ _ => v' = v + +/-- What a decoded TCBSetEPTRoot names: the derived cap is the mapped level-0 EPT cap in the source slot. -/ +def SetEptDec (t : ObjRef) (ex : List (Cap × CslotPtr)) (i : SetEptRootInvocation) : Prop := + i.target = t ∧ ∃ r m rest, ex = (.archObjectCap (.eptCap r 0 (some m)), i.srcSlot) :: rest ∧ + i.newCap = .archObjectCap (.eptCap r 0 (some m)) + +theorem okr_decodeEptInvocation (label : Data) (args : List Data) (slot : CslotPtr) (p : ObjRef) (l : Nat) + (m : Option (Asid × VSpaceRef)) (ex : List (Cap × CslotPtr)) : + OkR (EptDec (.archObjectCap (.eptCap p l m)) slot ex) + (decodeEptInvocation (Z := Unit) label args slot (.eptCap p l m) ex) := by + unfold decodeEptInvocation + dsimp only + refine okr_ite (fun _ => okr_throw _) fun hl => ?_ + refine okr_ite (fun _ => ?_) fun _ => ?_ + · exact okr_bind fun _ => okr_bind fun _ => okr_returnOk ⟨rfl, rfl, hl⟩ + refine okr_ite (fun _ => ?_) fun _ => okr_throw _ + cases ex with + | nil => exact okr_throw _ + | cons a rest => + obtain ⟨rootCap, s⟩ := a + dsimp only + refine okr_ite (fun _ => okr_throw _) fun _ => ?_ + refine okr_ite (fun _ => okr_throw _) fun hm => ?_ + have e : m = none := by simpa using hm + subst e + refine okr_bind fun _ => okr_bind fun ⟨_, _⟩ => okr_bind fun _ => okr_bind fun ⟨_, _⟩ => okr_bind fun _ => ?_ + exact okr_returnOk ⟨rfl, rfl, hl⟩ + +theorem pageCap_ite (c : Prop) [Decidable c] (dev : Bool) (p : ObjRef) (R : VMRights) (sz : VMPageSize) + (a b : VMMapType) (x y : Option (Asid × VSpaceRef)) : + (if c then ArchCap.pageCap dev p R a sz x else .pageCap dev p R b sz y) = + .pageCap dev p R (if c then a else b) sz (if c then x else y) := by + split <;> rfl + +theorem okr_decodePageMapEpt (args : List Data) (slot : CslotPtr) (cap : ArchCap) (ex : List (Cap × CslotPtr)) : + OkR (EptDec (.archObjectCap cap) slot ex) (decodePageMapEpt (Z := Unit) args slot cap ex) := by + unfold decodePageMapEpt + refine okr_ite (fun _ => okr_throw _) fun _ => ?_ + refine okr_bind fun _ => ?_ + dsimp only + split + · refine okr_bind fun ⟨_, _⟩ => okr_bind fun _ => okr_bind fun _ => okr_bind fun _ => okr_bind fun ⟨_, _⟩ => + okr_bind fun _ => okr_returnOk ?_ + exact ⟨rfl, _, _, _, _, _, _, _, rfl, pageCap_ite _ _ _ _ _ _ _ _ _⟩ + · exact okr_throw _ + +theorem okr_decodeEptAsidPoolAssign (p : ObjRef) (base : Asid) (r : ObjRef) (sl slot : CslotPtr) + (rest : List (Cap × CslotPtr)) : + OkR (EptDec (.archObjectCap (.asidPoolCap p base)) slot ((.archObjectCap (.eptCap r 0 none), sl) :: rest)) + (decodeEptAsidPoolAssign (Z := Unit) p base sl) := by + unfold decodeEptAsidPoolAssign + refine okr_bind fun _ => okr_bind fun _ => okr_bind fun _ => okr_bind fun _ => okr_bind fun _ => + okr_bind fun _ => okr_bind fun _ => okr_bind fun _ => okr_returnOk ?_ + exact ⟨⟨_, rfl⟩, _, _, rfl⟩ + +theorem okr_decodeVcpuInvocation (label : Data) (args : List Data) (v : ObjRef) (ex : List (Cap × CslotPtr)) : + OkR (VcpuDec v ex) (decodeVcpuInvocation (Z := Unit) label args v ex) := by + unfold decodeVcpuInvocation + dsimp only + repeat' (first + | exact okr_throw _ + | (refine okr_returnOk ?_; first + | exact rfl + | exact trivial + | exact ⟨rfl, _, _, rfl⟩ + | exact ⟨rfl, ⟨_, _, _, rfl⟩, _, rfl⟩) + | refine okr_bind fun _ => ?_ + | split) + +theorem okr_decodeSetEptRoot (t : ObjRef) (ex : List (Cap × CslotPtr)) : + OkR (SetEptDec t ex) (decodeSetEptRoot (Z := Unit) t ex) := by + unfold decodeSetEptRoot + split + · exact okr_throw _ + · split + · refine okr_bind' (Q := fun d => d = _) (m := deriveCap _ _) ?_ fun d hd => okr_returnOk ⟨rfl, _, _, _, rfl, hd⟩ + unfold deriveCap archDeriveCap + exact okr_returnOk rfl + · exact okr_throw _ + +/-! ## The extended decoder -/ + +/-- The decode facts of the VM invocations (nothing is said of `.base`; see `fl4DecodeInvocationV_base`). -/ +def VmDec (cap : Cap) (slot : CslotPtr) (ex : List (Cap × CslotPtr)) : Fl4InvocationV → Prop + | .base _ => True + | .vcpu i => ∃ v, cap = .archObjectCap (.vcpuCap v) ∧ VcpuDec v ex i + | .ept i => EptDec cap slot ex i + | .setEptRoot i => ∃ t, cap = .threadCap t ∧ SetEptDec t ex i + +theorem fl4DecodeInvocationV_vmDec (fp : Bool) (label : Data) (args : List Data) (ci : CapRef) (slot : CslotPtr) + (cap : Cap) (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) : + OkR (VmDec cap slot ex) (fl4DecodeInvocationV (Z := Unit) fp label args ci slot cap ex buf) := by + have base : ∀ m : SEMonad Unit Fl4Invocation, OkR (VmDec cap slot ex) (NonDetE.liftME Fl4InvocationV.base m) := + fun _ => okr_liftME fun _ _ _ _ => trivial + unfold fl4DecodeInvocationV + dsimp only + split + · exact okr_liftME fun σ a s h => ⟨_, rfl, okr_decodeVcpuInvocation _ _ _ _ σ a s h⟩ + · exact okr_liftME (okr_decodeEptInvocation _ _ _ _ _ _ _) + · refine okr_ite (fun _ => okr_liftME (okr_decodePageMapEpt _ _ _ _)) fun _ => ?_ + refine okr_ite (fun _ => okr_throw _) fun _ => ?_ + refine okr_ite (fun h => okr_returnOk ?_) fun _ => base _ + obtain ⟨rfl, hm, -⟩ := h + exact ⟨rfl, rfl, _, _, _, _, _, rfl⟩ + · refine okr_ite (fun h => okr_liftME ?_) fun _ => base _ + obtain ⟨-, h⟩ := h + cases ex with + | nil => simp [startsWithEptRoot] at h + | cons a rest => + obtain ⟨c, sl⟩ := a + unfold startsWithEptRoot at h + split at h + · rename_i heq + cases heq + exact okr_decodeEptAsidPoolAssign _ _ _ _ _ _ + · simp at h + · refine okr_ite (fun _ => okr_liftME ?_) fun _ => base _ + exact fun σ a s h => ⟨_, rfl, okr_decodeSetEptRoot _ _ σ a s h⟩ + · exact okr_ite (fun _ => okr_throw _) fun _ => base _ + · exact base _ + +/-- A `.base` outcome of the extended decoder is an outcome of the native one, from the same state to the same state. -/ +theorem fl4DecodeInvocationV_base (fp : Bool) (label : Data) (args : List Data) (ci : CapRef) (slot : CslotPtr) + (cap : Cap) (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) (σ s : State Unit) (b : Fl4Invocation) + (h : ((.ok (.base b), s) : Except SyscallError Fl4InvocationV × State Unit) ∈ + ((fl4DecodeInvocationV (Z := Unit) fp label args ci slot cap ex buf).run σ).1) : + (Except.ok b, s) ∈ ((fl4DecodeInvocation (Z := Unit) fp label args ci slot cap ex buf).run σ).1 := by + -- an outcome of a VM branch is never `.base` + have vm : ∀ {α : Type} (f : α → Fl4InvocationV) (_ : ∀ a, f a ≠ .base b) (m : SEMonad Unit α), + ((.ok (.base b), s) : Except SyscallError Fl4InvocationV × State Unit) ∉ ((NonDetE.liftME f m).run σ).1 := + fun f hf m hm => (okr_liftME (P := fun x => x ≠ .base b) (m := m) fun _ a _ _ => hf a) σ _ s hm rfl + have base : ((.ok (.base b), s) : Except SyscallError Fl4InvocationV × State Unit) ∈ + ((NonDetE.liftME Fl4InvocationV.base (fl4DecodeInvocation (Z := Unit) fp label args ci slot cap ex buf)).run σ).1 → + (Except.ok b, s) ∈ ((fl4DecodeInvocation (Z := Unit) fp label args ci slot cap ex buf).run σ).1 := by + intro hm + unfold NonDetE.liftME at hm + rw [ExceptT.run_bind] at hm + obtain ⟨⟨r, s'⟩, hz, hm⟩ := hm + cases r with + | error e => + dsimp only at hm + have h := Set.mem_singleton_iff.1 hm; simp at h + | ok a => + have e := Set.mem_singleton_iff.1 hm + simp only [Prod.mk.injEq, Except.ok.injEq, Fl4InvocationV.base.injEq] at e + obtain ⟨rfl, rfl⟩ := e + exact hz + have thr : ∀ e : SyscallError, ((.ok (.base b), s) : Except SyscallError Fl4InvocationV × State Unit) ∉ + ((NonDetE.throwError e : SEMonad Unit Fl4InvocationV).run σ).1 := + fun e hm => (okr_throw (P := fun _ => False) e) σ _ s hm + unfold fl4DecodeInvocationV at h + dsimp only at h + split at h + · exact absurd h (vm _ (fun _ => by simp) _) + · exact absurd h (vm _ (fun _ => by simp) _) + · split at h + · exact absurd h (vm _ (fun _ => by simp) _) + split at h + · exact absurd h (thr _) + split at h + · have e := Set.mem_singleton_iff.1 h; simp at e + · exact base h + · split at h + · exact absurd h (vm _ (fun _ => by simp) _) + · exact base h + · split at h + · exact absurd h (vm _ (fun _ => by simp) _) + · exact base h + · split at h + · exact absurd h (thr _) + · exact base h + · exact base h + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinFamA.lean b/proofs/Fl4Proofs/P3/VmFinFamA.lean new file mode 100644 index 000000000..377b66f16 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamA.lean @@ -0,0 +1,133 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, first Lean check): the FaultTcbsS / NtfnQB-EpQB / IfLiveT +bundles across the VM report hooks `vmReportF` and `vcpuDetachF` (spec only). A `runningVM` thread becomes +`running`: the strong `FaultTcbsS` gives it no fault (`runningVM` is not a fault state), `liveSt` and `runnable` +include `runningVM`, so no new premise. +-/ +import Fl4Proofs.P3.SpecInvAll +import Fl4Proofs.P3.VmPerfRvm +import Fl4Proofs.P3.VirtNoRunF +import Fl4Proofs.P3.VmFinFamTac +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec + +noncomputable section + +set_option maxHeartbeats 1000000 + +open Fl4Proofs.P3 Fl4Proofs.P3.VirtNoRunF Fl4Proofs.P3.VirtReportEq + +variable {L K : List ObjRef} + +/-! ## The generic detach combinator -/ + +/-- `vcpuDetachF t` keeps `P` when the VCPU update keeps `P` and `vmReportF t 0` keeps `P` from a `P` state where `t` is +`runningVM` (the guard of the report). One use per invariant bundle. -/ +theorem ik_vcpuDetachF_gen {P : State Unit → Prop} (t : ObjRef) (hUpd : ∀ v g, IK P (vcpuUpdate (Z := Unit) v g)) + (hRep : ∀ σ, P σ → (∃ x, σ.kheap t = some (.tcb x) ∧ x.tcbState = .runningVM) → + ∀ y ∈ (vmReportF (Z := Unit) t 0 σ).1, P y.2) : IK P (vcpuDetachF (Z := Unit) t) := by + intro σ hσ y hy + unfold vcpuDetachF at hy + rw [nd_bind_one (getTcbVcpu_eq σ t)] at hy + cases hv : tcbVcpuOf σ t with + | none => + rw [hv] at hy + dsimp only at hy + obtain rfl := Set.mem_singleton_iff.1 hy + exact hσ + | some v => + rw [hv] at hy + dsimp only at hy + obtain ⟨z, hz, hy⟩ := hy + obtain ⟨e2, xt, hk, e1⟩ := PPS.getThreadState_mem hz + dsimp only at hy + obtain ⟨w, hw, hy⟩ := hy + rw [e2] at hw + have hP : P w.2 := by + unfold NonDet.when at hw + by_cases hs : z.1 = .runningVM + · rw [if_pos hs] at hw + exact hRep σ hσ ⟨xt, hk, e1.trans hs⟩ w hw + · rw [if_neg hs] at hw + obtain rfl := Set.mem_singleton_iff.1 hw + exact hσ + exact hUpd v _ w.2 hP y hy + +/-! ## FaultTcbsS -/ + +theorem ft_getVcpuV (v : ObjRef) : FTK L (getVcpu (Z := Unit) v) := + Fl4Proofs.P3.ik_of_keepSt (Fl4Proofs.P3.keepSt_getVcpu v) _ +macro_rules | `(tactic| ft_leaf) => `(tactic| with_reducible exact ft_getVcpuV _) + +theorem ft_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : FTK L (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu; ftl_auto +macro_rules | `(tactic| ft_leaf) => `(tactic| with_reducible exact ft_vcpuUpdateV _ _) + +/-- `set_thread_state` of a thread `t ∈ L` (known to have no fault). -/ +macro_rules | `(tactic| ft_leaf) => `(tactic| ((with_reducible refine ft_setThreadState _ _ (Or.inr ?_)); assumption)) + +theorem ft_vmReportF (t : ObjRef) (b : Word) (h : t ∈ L) : FTK L (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + ftl_auto + +theorem ik_fts_of_ftl {α : Type} {m : NonDet (State Unit) α} (h : FTK [] m) : IK FaultTcbsS m := + fun σ hσ x hx => (FTL.nil_iff _).1 (h σ ((FTL.nil_iff σ).2 hσ) x hx) + +/-- **`FaultTcbsS` across `vcpuDetachF`**: a `runningVM` thread has no fault (it is no fault state), so the report's +`Running` keeps the invariant. -/ +theorem fts_vcpuDetachF (t : ObjRef) : IK FaultTcbsS (vcpuDetachF (Z := Unit) t) := by + refine ik_vcpuDetachF_gen t (fun v g => ik_fts_of_ftl (ft_vcpuUpdateV v g)) fun σ hσ ⟨x, hx, hs⟩ y hy => ?_ + have hL : FTL [t] σ := fun p x' hp => ⟨hσ p x' hp, fun hm => by + obtain rfl := List.mem_singleton.1 hm + obtain rfl : x' = x := by rw [hx] at hp; exact (KernelObject.tcb.inj (Option.some.inj hp)).symm + by_contra hf + have := hσ p x' hx hf + rw [hs] at this + exact this⟩ + have h2 := ft_vmReportF (L := [t]) t 0 (List.mem_singleton_self t) σ hL y hy + exact fun p x' hp hf => (h2 p x' hp).1 hf + +/-! ## NtfnQB / EpQB -/ + +theorem nq_getVcpuV (v : ObjRef) : NQK K (getVcpu (Z := Unit) v) := + Fl4Proofs.P3.ik_of_keepSt (Fl4Proofs.P3.keepSt_getVcpu v) _ +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_getVcpuV _) + +theorem nq_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : NQK K (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu; nq_auto +macro_rules | `(tactic| nq_leaf) => `(tactic| with_reducible exact nq_vcpuUpdateV _ _) + +theorem nq_vmReportF (t : ObjRef) (b : Word) (h : t ∈ K) : NQK K (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + nq_auto + +/-- **`NQ K` across `vcpuDetachF`**: a `runningVM` thread is in no queue (not blocked), so it joins `K`. -/ +theorem nq_vcpuDetachF (t : ObjRef) : NQK K (vcpuDetachF (Z := Unit) t) := by + refine ik_vcpuDetachF_gen t (fun v g => nq_vcpuUpdateV v g) fun σ hσ ⟨x, hx, hs⟩ y hy => ?_ + exact nq_tail (nq_vmReportF (K := t :: K) t 0 (List.mem_cons_self ..) σ + (nq_consR hσ hx (by rw [hs]; simp [isBoN]) (by rw [hs]; simp [epBlocked])) y hy) + +/-! ## IfLiveT -/ + +theorem il_getVcpuV (v : ObjRef) : ILK K (getVcpu (Z := Unit) v) := + Fl4Proofs.P3.ik_of_keepSt (Fl4Proofs.P3.keepSt_getVcpu v) _ +macro_rules | `(tactic| il_leaf) => `(tactic| with_reducible exact il_getVcpuV _) + +theorem il_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : ILK K (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu; il_auto +macro_rules | `(tactic| il_leaf) => `(tactic| with_reducible exact il_vcpuUpdateV _ _) + +theorem il_vmReportF (t : ObjRef) (b : Word) (h : t ∈ K) : ILK K (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + il_auto + +/-- **`IL K` across `vcpuDetachF`**: `runningVM` is live, so the thread holds a thread cap and joins `K`. -/ +theorem il_vcpuDetachF (t : ObjRef) : ILK K (vcpuDetachF (Z := Unit) t) := by + refine ik_vcpuDetachF_gen t (fun v g => il_vcpuUpdateV v g) fun σ hσ ⟨x, hx, hs⟩ y hy => ?_ + exact ikp_ilcons (K := K) hx (by rw [hs]; trivial) (il_vmReportF (K := t :: K) t 0 (List.mem_cons_self ..)) hσ y hy + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinFamB.lean b/proofs/Fl4Proofs/P3/VmFinFamB.lean new file mode 100644 index 000000000..cf87c6599 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamB.lean @@ -0,0 +1,159 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): the `B L E K C` bundle (SpecInvFaultTcbsN: `FaultTcbsS` up to `E`, +`NQB`, …; `B [] [] [] ⊤ ↔ FaultTcbsS ∧ NQB`) across the VM hooks of `finaliseCapVF` (spec only): an architecture +object write, the VCPU update, `vmReportF` and `vcpuDetachF`. A `runningVM` thread has no fault under `B` (its state is +neither a fault state nor active), so the report's `Running` keeps the bundle. No named premise. +-/ +import Fl4Proofs.P3.VmFinFamA +import Fl4Proofs.P3.SpecInvFaultTcbsH7 +import Fl4Proofs.P3.SpecInvNtfnQP2 +import Fl4Proofs.P3.SpecInvNtfnQC2 +import Fl4Proofs.P3.SpecInvUtZK3 +import Fl4Proofs.P3.VmFinGenPj +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec + +noncomputable section + +set_option maxHeartbeats 1000000 + +open Fl4Proofs.P3 + +variable {L E K : List ObjRef} {C : ObjRef → Prop} + +theorem b_setArchV (p : ObjRef) (a : ArchKernelObj) : BK L E K C (setObject (Z := Unit) p (.archObj a)) := + b_of_gf fun _ _ => fg_setObject p _ trivial + +theorem b_getVcpuV (v : ObjRef) : BK L E K C (getVcpu (Z := Unit) v) := + ik_of_keepSt (keepSt_getVcpu v) _ +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_getVcpuV _) + +theorem b_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : BK L E K C (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu + refine ik_bind (b_getVcpuV v) fun _ => b_setArchV v _ +macro_rules | `(tactic| b_leaf) => `(tactic| with_reducible exact b_vcpuUpdateV _ _) + +theorem b_vmReportF (t : ObjRef) (b : Word) (h : t ∈ L) : BK L E K C (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + b_auto + +/-- Under `B`, a `runningVM` thread has no fault: it joins `L`. -/ +theorem b_cons_runningVM {σ : State Unit} (hσ : B L E K C σ) {t : ObjRef} {x : Tcb} + (hx : σ.kheap t = some (.tcb x)) (hs : x.tcbState = .runningVM) : B (t :: L) E K C σ := by + obtain ⟨⟨h1, h1c⟩, h2, h3⟩ := hσ + refine ⟨⟨fun p o hp => ?_, h1c⟩, h2, h3⟩ + have := h1 p o hp + cases o with + | tcb x' => + refine ⟨this.1, fun hl => ?_, this.2.2⟩ + rcases List.mem_cons.1 hl with rfl | hl + · rw [hx] at hp + cases hp + by_contra hf + rcases this.1 hf with h | ⟨-, h⟩ <;> rw [hs] at h <;> exact h + · exact this.2.1 hl + | _ => exact this + +/-- **`B` across `vcpuDetachF`.** -/ +theorem b_vcpuDetachF (t : ObjRef) : BK L E K C (vcpuDetachF (Z := Unit) t) := by + refine ik_vcpuDetachF_gen t (fun v g => b_vcpuUpdateV v g) fun σ hσ ⟨x, hx, hs⟩ y hy => ?_ + exact b_mono (b_vmReportF (L := t :: L) t 0 (List.mem_cons_self ..) σ (b_cons_runningVM hσ hx hs) y hy) + (fun u hu => List.mem_cons_of_mem _ hu) (fun u hu => hu) id + +/-! ## `PJ` (the threads of `L` not IPC-blocked) -/ + +variable {Lp : List ObjRef} + +theorem pj_setArchV (p : ObjRef) (a : ArchKernelObj) : PJK Lp (setObject (Z := Unit) p (.archObj a)) := + tp_setObject p _ fun _ h => by cases h + +theorem pj_getVcpuV (v : ObjRef) : PJK Lp (getVcpu (Z := Unit) v) := + ik_of_keepSt (keepSt_getVcpu v) _ +macro_rules | `(tactic| pj_leaf) => `(tactic| with_reducible exact pj_getVcpuV _) + +theorem pj_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : PJK Lp (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu + exact ik_bind (pj_getVcpuV v) fun _ => pj_setArchV v _ +macro_rules | `(tactic| pj_leaf) => `(tactic| with_reducible exact pj_vcpuUpdateV _ _) + +theorem pj_vmReportF (t : ObjRef) (b : Word) : PJK Lp (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + pj_auto + +theorem pj_vcpuDetachF (t : ObjRef) : PJK Lp (vcpuDetachF (Z := Unit) t) := + ik_vcpuDetachF_gen t (fun v g => pj_vcpuUpdateV v g) fun σ hσ _ y hy => pj_vmReportF t 0 σ hσ y hy + +/-- `cap_revoke` over a deletion `capDeleteG Fn` (the native family has no revoke lemma). -/ +theorem pj_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, PJK Lp (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, PJK Lp (f k)) (sl : CslotPtr) : PJKE Lp (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, PJKE Lp (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + pj_auto + all_goals first | exact hrec' _ | exact pj_capDeleteG hFn _ | apply pj_capDeleteG hFn + +theorem pj_capRevokeG {Fn : FinFn} (hFn : ∀ c b, PJK Lp (Fn c b)) + (sl : CslotPtr) : PJKE Lp (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => pj_capRevokeFG hFn hf k) sl + +/-! ## `CTK` (the current thread) -/ + +variable {cq : ObjRef} + +theorem ct_getVcpuV (v : ObjRef) : CTK cq (getVcpu (Z := Unit) v) := + ik_of_keepSt (keepSt_getVcpu v) _ +macro_rules | `(tactic| ct_leaf) => `(tactic| with_reducible exact ct_getVcpuV _) + +theorem ct_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : CTK cq (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu + exact ik_bind (ct_getVcpuV v) fun _ => ct_setObject v _ +macro_rules | `(tactic| ct_leaf) => `(tactic| with_reducible exact ct_vcpuUpdateV _ _) + +theorem ct_vmReportF (t : ObjRef) (b : Word) : CTK cq (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + ct_auto + +theorem ct_vcpuDetachF (t : ObjRef) : CTK cq (vcpuDetachF (Z := Unit) t) := + ik_vcpuDetachF_gen t (fun v g => ct_vcpuUpdateV v g) fun σ hσ _ y hy => ct_vmReportF t 0 σ hσ y hy + +end + +end Fl4Proofs.P3.SInv + +/-! ## `YP` (data pages and zeroed memory) -/ + +namespace Fl4Proofs.P3.SInvUZ +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv + +noncomputable section + +set_option maxHeartbeats 1000000 + +variable {PD : PgSet} {FS : Set Nat} + +open Lean in +/-- SpecInvUtZK1's `yk_auto` (P3/IpcYK, imported here, defines another). -/ +macro "ykz_auto" : tactic => pure ⟨(mkNode ``Fl4Proofs.P3.SInvUZ.tacticYk_auto #[mkAtom "yk_auto"]).raw⟩ + +theorem yk_getVcpuV (v : ObjRef) : YK PD FS (getVcpu (Z := Unit) v) := + ik_of_keepSt (keepSt_getVcpu v) _ +macro_rules (kind := Fl4Proofs.P3.SInvUZ.tacticYk_leaf) | `(tactic| yk_leaf) => `(tactic| with_reducible exact yk_getVcpuV _) + +theorem yk_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : YK PD FS (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu + exact ik_bind (yk_getVcpuV v) fun _ => yk_setObject v _ +macro_rules (kind := Fl4Proofs.P3.SInvUZ.tacticYk_leaf) | `(tactic| yk_leaf) => `(tactic| with_reducible exact yk_vcpuUpdateV _ _) + +theorem yk_vmReportF (t : ObjRef) (b : Word) : YK PD FS (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + ykz_auto + +theorem yk_vcpuDetachF (t : ObjRef) : YK PD FS (vcpuDetachF (Z := Unit) t) := + ik_vcpuDetachF_gen t (fun v g => yk_vcpuUpdateV v g) fun σ hσ _ y hy => yk_vmReportF t 0 σ hσ y hy + +end + +end Fl4Proofs.P3.SInvUZ diff --git a/proofs/Fl4Proofs/P3/VmFinFamDl.lean b/proofs/Fl4Proofs/P3/VmFinFamDl.lean new file mode 100644 index 000000000..367c0eced --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamDl.lean @@ -0,0 +1,55 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): `DomListOK` (`DP`, via the `DL` frame: the domain list, its start and its +index) across the VM report hooks and `finaliseCapVF` (spec only). The VF-only ops write TCB words, MRs, the thread +state and VCPU / EPT objects: pure frames for `dlV`. `finaliseCapVF` is `relk_finaliseCapVF`'s case split over the +native `dl_finaliseCap`. +-/ +import Fl4Proofs.P3.VmPerfCur +import Fl4Proofs.P3.SpecInvDom +open Classical + +namespace Fl4Proofs.P3.SInvDom +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Vs + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem dl_getVcpuV (v : ObjRef) : DL (getVcpu (Z := Unit) v) := fk_of_keepSt (keepSt_getVcpu v) +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_getVcpuV _) + +theorem dl_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : DL (vcpuUpdate (Z := Unit) v g) := + fk_vcpuUpdate (fun p _ => dl_setObject p _) v g +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_vcpuUpdateV _ _) + +theorem dl_vmReportF (t : ObjRef) (badge : Word) : DL (vmReportF (Z := Unit) t badge) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_vmReportF _ _) + +theorem dl_vcpuDetachF (t : ObjRef) : DL (vcpuDetachF (Z := Unit) t) := by + unfold vcpuDetachF getTcbVcpu + dl_auto +macro_rules | `(tactic| dl_leaf) => `(tactic| with_reducible exact dl_vcpuDetachF _) + +theorem dl_guestUnmapV (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + DL (guestUnmap (Z := Unit) asid gpa ptr level table) := + fk_guestUnmap (fun p _ => dl_setObject p _) asid gpa ptr level table + +theorem dl_ioPortRevokeV (c : ArchCap) : DL (ioPortRevoke (Z := Unit) c) := + fk_ioPortRevoke (fun p _ => dl_setObject p _) c + +/-- **`finaliseCapVF` keeps the domain list, its start and index.** -/ +theorem dl_finaliseCapVF (c : Cap) (b : Bool) : DL (finaliseCapVF (Z := Unit) c b) := + relk_finaliseCapVF (R := fun σ σ' => dlV σ' = dlV σ) (fun _ => rfl) (fun _ _ _ h1 h2 => h2.trans h1) + dl_finaliseCap dl_vcpuDetachF dl_getVcpuV dl_guestUnmapV dl_ioPortRevokeV c b + +/-- **`finaliseCapVF` keeps `DomListOK`.** -/ +theorem dp_finaliseCapVF (c : Cap) (b : Bool) : DP (finaliseCapVF (Z := Unit) c b) := + dp_of_dl (dl_finaliseCapVF c b) + +theorem dp_vcpuDetachF (t : ObjRef) : DP (vcpuDetachF (Z := Unit) t) := dp_of_dl (dl_vcpuDetachF t) + +end + +end Fl4Proofs.P3.SInvDom diff --git a/proofs/Fl4Proofs/P3/VmFinFamFn.lean b/proofs/Fl4Proofs/P3/VmFinFamFn.lean new file mode 100644 index 000000000..b89a97797 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamFn.lean @@ -0,0 +1,24 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): `FN K` (IfLiveT with `K`, NtfnQB/EpQB, and the FPU owner holding a thread +cap, SpecInvFoA) across `vcpuDetachF`, the hook of `finaliseCapVF`. The pieces: P3/VmFinFamA (`IL`, `NQ`) and +P3/VmFinFamFpu (`OIK`), assembled by SpecInvFoA's `fo_ikn`. +-/ +import Fl4Proofs.P3.VmFinFamA +import Fl4Proofs.P3.VmFinFamFpu + +open Classical + +namespace Fl4Proofs.P3.SInvF +open Fl4Spec Fl4Proofs.P3.SInv + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-- **`FN K` across `vcpuDetachF`.** -/ +theorem fn_vcpuDetachF {K : List ObjRef} (t : ObjRef) : IK (FN K) (vcpuDetachF (Z := Unit) t) := + fo_ikn (fun L => iln_ik (il_vcpuDetachF t) (nq_vcpuDetachF t)) fun _ => oif_vcpuDetachF t + +end + +end Fl4Proofs.P3.SInvF diff --git a/proofs/Fl4Proofs/P3/VmFinFamFpu.lean b/proofs/Fl4Proofs/P3/VmFinFamFpu.lean new file mode 100644 index 000000000..013cc6fa6 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamFpu.lean @@ -0,0 +1,39 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): the FPU-owner frame `OIK q` (SpecInvFoG1: the owner is `q` and a TCB) +across the VM report hooks `vmReportF` and `vcpuDetachF` (spec only). The hooks write TCB words, MRs, one VCPU +object and the thread state; none writes `x64CurrentFpuOwner`. Own module: it imports no file that redefines +`oi_leaf` / `oi_auto` (SpecInvAllO1). +-/ +import Fl4Proofs.P3.SpecInvFoA +import Fl4Spec.Ext.VirtReport + +open Classical + +namespace Fl4Proofs.P3.SInvF +open Fl4Spec Fl4Proofs.P3.SInv + +noncomputable section + +set_option maxHeartbeats 1000000 + +variable {Qo : Option ObjRef} + +theorem oif_getVcpu (v : ObjRef) : OIK Qo (getVcpu (Z := Unit) v) := by + unfold getVcpu; oi_auto +macro_rules | `(tactic| oi_leaf) => `(tactic| with_reducible exact oif_getVcpu _) + +theorem oif_vcpuUpdate (v : ObjRef) (g : VcpuState → VcpuState) : OIK Qo (vcpuUpdate (Z := Unit) v g) := by + unfold vcpuUpdate setVcpu; oi_auto +macro_rules | `(tactic| oi_leaf) => `(tactic| with_reducible exact oif_vcpuUpdate _ _) + +theorem oif_vmReportF (t : ObjRef) (b : Word) : OIK Qo (vmReportF (Z := Unit) t b) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + oi_auto + +theorem oif_vcpuDetachF (t : ObjRef) : OIK Qo (vcpuDetachF (Z := Unit) t) := by + unfold vcpuDetachF getTcbVcpu + oi_auto + +end + +end Fl4Proofs.P3.SInvF diff --git a/proofs/Fl4Proofs/P3/VmFinFamHook.lean b/proofs/Fl4Proofs/P3/VmFinFamHook.lean new file mode 100644 index 000000000..4b2efd313 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamHook.lean @@ -0,0 +1,235 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): the shape of `finaliseCapVF` for the families generalised over the +finaliser (spec only). Every outcome of `finaliseCapVF c b` is, after at most one hook step (`HookStep`), either an +outcome of the native `finaliseCap c b` or `(nullCap, nullCap)` at the hooked state with `c` a cap of no object +(`DirectNull`). The hooks keep the caps (`CKeep`) and the untyped view (`uV`). No named premise. +-/ +import Fl4Proofs.P3.VmDelFinSpec +import Fl4Proofs.P3.SpecInvUtF2 +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-! ## `CKeep` across the two remaining hooks -/ + +theorem nkc_of_keepSt {α : Type} {m : NonDet (State Unit) α} (h : KeepSt m) : NKC m := + fun σ x hx => by rw [h σ x hx]; exact CKeep.refl σ + +theorem nkc_guestUnmap (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + NKC (guestUnmap (Z := Unit) asid gpa ptr level table) := by + unfold guestUnmap + refine nkc_bind (nkc_of_keepSt (keepSt_findGuestRoot _)) fun r => ?_ + rcases r with _ | root + · exact nkc_pure _ + refine nkc_bind (nkc_of_keepSt (keepSt_guestLookupSlot _ _ _)) fun t => ?_ + rcases t with _ | tbl + · exact nkc_pure _ + refine nkc_bind (nkc_of_keepSt (keepSt_getEpt _)) fun e => ?_ + obtain ⟨_, es⟩ := e + dsimp only + refine nkc_unless ?_ + unfold storeGuestEntry + exact nkc_bind (nkc_of_keepSt (keepSt_getEpt _)) fun ⟨_, _⟩ => + nkc_setObject (fun _ _ h => by cases h) (fun _ h => by cases h) + +theorem nkc_ioPortRevoke (c : ArchCap) : NKC (ioPortRevoke (Z := Unit) c) := by + unfold ioPortRevoke + split + · exact nkc_bind (nkc_gets _) fun _ => nkc_when (nkc_vcpuUpdateV _ _) + · exact nkc_pure _ + +/-! ## The untyped view across the hooks -/ + +theorem uk_getVcpuV {v : UView} (p : ObjRef) : UK v (getVcpu (Z := Unit) p) := ik_of_keepSt (keepSt_getVcpu p) _ + +theorem uk_setArch {v : UView} (p : ObjRef) (a : ArchKernelObj) : + UK v (setObject (Z := Unit) p (.archObj a)) := by + refine uk_setObject _ _ fun old h => ?_ + cases old <;> first + | rfl + | (cases h; done) + | (simp only [aType] at h; (split at h) <;> cases h) + +theorem uk_vcpuUpdateV {v : UView} (p : ObjRef) (g : VcpuState → VcpuState) : + UK v (vcpuUpdate (Z := Unit) p g) := by + unfold vcpuUpdate setVcpu + exact ik_bind (uk_getVcpuV p) fun _ => uk_setArch _ _ + +theorem uk_vmReportF {v : UView} (t : ObjRef) (badge : Word) : UK v (vmReportF (Z := Unit) t badge) := by + unfold vmReportF getTcbVcpu vmFullReply vmFaultIpNext + repeat' (first + | (with_reducible exact uk_vcpuUpdateV _ _) | (with_reducible exact uk_getVcpuV _) + | (with_reducible exact uk_setThreadState _ _) | (with_reducible exact uk_setMrs _ _ _) + | (with_reducible exact uk_asUser _ _) | uk_leaf | ik_step0 | ik_unfold | (unfold NonDet.when) + | (unfold NonDet.unless)) + +theorem uk_vcpuDetachF {v : UView} (t : ObjRef) : UK v (vcpuDetachF (Z := Unit) t) := by + unfold vcpuDetachF getTcbVcpu + repeat' (first + | (with_reducible exact uk_vmReportF _ _) | (with_reducible exact uk_vcpuUpdateV _ _) + | uk_leaf | ik_step0 | ik_unfold | (unfold NonDet.when) | (unfold NonDet.unless)) + +theorem ik_unlessV {P : State Unit → Prop} (c : Prop) [Decidable c] {m : NonDet (State Unit) Unit} (h : IK P m) : + IK P (NonDet.unless c m) := by + unfold NonDet.unless + split + · intro σ hσ x hx + obtain rfl : x = ((), σ) := hx + exact hσ + · exact h + +theorem uk_guestUnmap {v : UView} (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + UK v (guestUnmap (Z := Unit) asid gpa ptr level table) := by + unfold guestUnmap + refine ik_bind (ik_of_keepSt (keepSt_findGuestRoot _) _) fun r => ?_ + rcases r with _ | root + · exact ik_pure _ + refine ik_bind (ik_of_keepSt (keepSt_guestLookupSlot _ _ _) _) fun t => ?_ + rcases t with _ | tbl + · exact ik_pure _ + refine ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun e => ?_ + obtain ⟨_, es⟩ := e + dsimp only + have hs : UK v (storeGuestEntry (Z := Unit) tbl (guestIndex gpa level) 0) := by + unfold storeGuestEntry + refine ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun e => ?_ + obtain ⟨_, _⟩ := e + exact uk_setArch _ _ + exact ik_unlessV _ hs + +theorem uk_ioPortRevoke {v : UView} (c : ArchCap) : UK v (ioPortRevoke (Z := Unit) c) := by + unfold ioPortRevoke + split + · refine ik_bind (ik_of_keepSt (fun σ x hx => by rw [Set.mem_singleton_iff.1 hx]) _) fun _ => ?_ + unfold NonDet.when + split + · exact uk_vcpuUpdateV _ _ + · exact ik_pure _ + · exact ik_pure _ + +/-! ## The case split -/ + +/-- At most one hook of `finaliseCapVF`. -/ +def HookStep (σ σ' : State Unit) : Prop := + σ' = σ ∨ (∃ t, ((), σ') ∈ (vcpuDetachF (Z := Unit) t σ).1) ∨ + (∃ a g p l tb, ((), σ') ∈ (guestUnmap (Z := Unit) a g p l tb σ).1) ∨ + (∃ c, ((), σ') ∈ (ioPortRevoke (Z := Unit) c σ).1) + +/-- A predicate kept by the three hooks is kept by a `HookStep`. -/ +theorem HookStep.keep {P : State Unit → Prop} (hd : ∀ t, IK P (vcpuDetachF (Z := Unit) t)) + (hg : ∀ a g p l tb, IK P (guestUnmap (Z := Unit) a g p l tb)) (hi : ∀ c, IK P (ioPortRevoke (Z := Unit) c)) + {σ σ' : State Unit} (h : HookStep σ σ') (hP : P σ) : P σ' := by + rcases h with rfl | ⟨t, h⟩ | ⟨a, g, p, l, tb, h⟩ | ⟨c, h⟩ + · exact hP + · exact hd t σ hP _ h + · exact hg a g p l tb σ hP _ h + · exact hi c σ hP _ h + +theorem HookStep.ckeep {σ σ' : State Unit} (h : HookStep σ σ') : CKeep σ σ' := by + rcases h with rfl | ⟨t, h⟩ | ⟨a, g, p, l, tb, h⟩ | ⟨c, h⟩ + · exact CKeep.refl _ + · exact nkc_vcpuDetachF t σ _ h + · exact nkc_guestUnmap a g p l tb σ _ h + · exact nkc_ioPortRevoke c σ _ h + +theorem HookStep.uv {σ σ' : State Unit} (h : HookStep σ σ') : uV σ' = uV σ := + h.keep (P := fun s => uV s = uV σ) (fun t => uk_vcpuDetachF t) (fun _ _ _ _ _ => uk_guestUnmap _ _ _ _ _) + (fun c => uk_ioPortRevoke c) rfl + +theorem HookStep.capAt {σ σ' : State Unit} (h : HookStep σ σ') : SInvU.capAt σ' = SInvU.capAt σ := + congrArg (fun w : UView => w.2.1) h.uv + +/-- The caps `finaliseCapVF` answers with `nullCap` itself: they reference no object. -/ +def DirectNull (c : Cap) : Prop := + (∃ v, c = .archObjectCap (.vcpuCap v)) ∨ (∃ p l a g, c = .archObjectCap (.eptCap p l (some (a, g)))) ∨ + (∃ d p r sz a g, c = .archObjectCap (.pageCap d p r .vmEptMap sz (some (a, g)))) + +/-- The shape of a finaliser's outcomes (module docstring). -/ +def FinShape (Fn : Cap → Bool → NonDet (State Unit) (Cap × Cap)) : Prop := + ∀ c b σ x, x ∈ (Fn c b σ).1 → ∃ σ1, HookStep σ σ1 ∧ + (x ∈ (finaliseCap (Z := Unit) c b σ1).1 ∨ (x = ((.nullCap, .nullCap), σ1) ∧ DirectNull c)) + +theorem finShape_native : FinShape (finaliseCap (Z := Unit)) := + fun _ _ σ _ hx => ⟨σ, Or.inl rfl, Or.inl hx⟩ + +theorem mem_pure_eq {α : Type} {a : α} {σ : State Unit} {x : α × State Unit} + (h : x ∈ ((pure a : NonDet (State Unit) α) σ).1) : x = (a, σ) := Set.mem_singleton_iff.1 h + +/-- **`finaliseCapVF`'s outcomes** (module docstring). -/ +theorem finaliseCapVF_cases : FinShape (finaliseCapVF (Z := Unit)) := by + intro c b σ x hx + by_cases hh : FinaliseHooked c + swap + · rw [VirtF13.finaliseCapVF_eq c b hh] at hx + exact ⟨σ, Or.inl rfl, Or.inl hx⟩ + cases c with + | threadCap t => + change x ∈ ((NonDet.when b (vcpuDetachF t) >>= fun _ => finaliseCap (.threadCap t) b) σ).1 at hx + obtain ⟨y, hy, hx⟩ := hx + refine ⟨y.2, ?_, Or.inl hx⟩ + unfold NonDet.when at hy + split at hy + · exact Or.inr (Or.inl ⟨t, hy⟩) + · rw [mem_pure_eq hy]; exact Or.inl rfl + | archObjectCap a => + cases a with + | vcpuCap v => + change x ∈ ((if b then (do + let st ← getVcpu v + match st.tcb with + | some t => vcpuDetachF t + | none => pure () + return (Cap.nullCap, Cap.nullCap)) else return (Cap.nullCap, Cap.nullCap)) σ).1 at hx + have hd : DirectNull (.archObjectCap (.vcpuCap v)) := Or.inl ⟨v, rfl⟩ + split at hx + · obtain ⟨y, hy, hx⟩ := hx + have e := keepSt_getVcpu v σ y hy + cases htcb : y.1.tcb with + | none => + simp only [htcb] at hx + exact ⟨y.2, Or.inl e, Or.inr ⟨mem_pure_eq hx, hd⟩⟩ + | some t => + simp only [htcb] at hx + obtain ⟨z, hz, hx⟩ := hx + rw [e] at hz + exact ⟨z.2, Or.inr (Or.inl ⟨t, hz⟩), Or.inr ⟨mem_pure_eq hx, hd⟩⟩ + · exact ⟨σ, Or.inl rfl, Or.inr ⟨mem_pure_eq hx, hd⟩⟩ + | eptCap p l m => + rcases m with _ | ⟨a, g⟩ + · cases hh + · change x ∈ ((if b ∧ l ≠ 0 then (do + guestUnmap a g p l true + return (Cap.nullCap, Cap.nullCap)) else finaliseCap (.archObjectCap (.eptCap p l (some (a, g)))) b) σ).1 + at hx + split at hx + · obtain ⟨y, hy, hx⟩ := hx + exact ⟨y.2, Or.inr (Or.inr (Or.inl ⟨_, _, _, _, _, hy⟩)), + Or.inr ⟨mem_pure_eq hx, Or.inr (Or.inl ⟨_, _, _, _, rfl⟩)⟩⟩ + · exact ⟨σ, Or.inl rfl, Or.inl hx⟩ + | pageCap d p ri mt sz m => + rcases m with _ | ⟨a, g⟩ <;> cases mt <;> try cases hh + change x ∈ ((do + NonDet.when (a ≠ 0) <| guestUnmap a g p (pageBitsForSize sz) false + return (Cap.nullCap, Cap.nullCap)) σ).1 at hx + obtain ⟨y, hy, hx⟩ := hx + refine ⟨y.2, ?_, Or.inr ⟨mem_pure_eq hx, Or.inr (Or.inr ⟨_, _, _, _, _, _, rfl⟩)⟩⟩ + unfold NonDet.when at hy + split at hy + · exact Or.inr (Or.inr (Or.inl ⟨_, _, _, _, _, hy⟩)) + · rw [mem_pure_eq hy]; exact Or.inl rfl + | ioPortCap f l o => + change x ∈ ((ioPortRevoke (.ioPortCap f l o) >>= fun _ => finaliseCap (.archObjectCap (.ioPortCap f l o)) b) + σ).1 at hx + obtain ⟨y, hy, hx⟩ := hx + exact ⟨y.2, Or.inr (Or.inr (Or.inr ⟨_, hy⟩)), Or.inl hx⟩ + | _ => cases hh + | _ => cases hh + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinFamTac.lean b/proofs/Fl4Proofs/P3/VmFinFamTac.lean new file mode 100644 index 000000000..17df08055 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFamTac.lean @@ -0,0 +1,13 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): the `FaultTcbsS` tactic of SpecInvFaultTcbsF1 under a name no other file +defines. This module imports no file that redefines `ft_auto` (XTopJointFinVsSpec) or `ik_step0` (ScSchedIdle), so the +quotation resolves to SpecInvNativeScopeK's `ik_step0` and SpecInvFaultTcbsF1's `ft_leaf`. +-/ +import Fl4Proofs.P3.SpecInvFaultTcbsF3 + +namespace Fl4Proofs.P3.SInv + +/-- SpecInvFaultTcbsF1's `ft_auto`. -/ +macro "ftl_auto" : tactic => `(tactic| repeat' (first | ft_leaf | ik_step0 | ik_unfold | (unfold NonDetE.whenE) | (unfold NonDetE.unlessE) | (unfold NonDet.when) | (unfold NonDet.unless))) + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinFbV.lean b/proofs/Fl4Proofs/P3/VmFinFbV.lean new file mode 100644 index 000000000..e93a7c44b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFbV.lean @@ -0,0 +1,485 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c)): the frame batch at the extended dispatcher with slot facts. +`fbDelXV slot` is `cap_delete` of a frame slot written out with the VM-aware finaliser (`finaliseCapVF`: the live guest +unmap of an EPT-mapped frame, else `arch_finalise_cap`) then `empty_slot slot Null`; `capDeleteVF_subXV`: at a slot +holding a frame cap every outcome of `capDeleteVF` is one of `fbDelXV` (from `capDeleteVF_frame_mem`). +`fbEntryXV` / `fbRunXV` / `performFrameBatchXV` are SpecFbX's `fbEntryX` / `fbRunX` / `performFrameBatchX` with `fbDelXV` +for the DELETE entry's deletion (the copy-map rollback keeps the native `fbDelX`, as `fbEntryWith` keeps `capDelete`), +generated from SpecFbX by `handoff/tools/gen_fbv.py` (statement-level renames only; the proofs are SpecFbX's). +`performFrameBatchVF_subXV` / `ik_performFrameBatchVF_ofXV`: outcome inclusion and the family combinator; +`il_performFrameBatchVF`: the batch keeps `IL K` (the DELETE's slot holds a frame cap, so it holds no thread cap). No +named premise. +-/ +import Fl4Proofs.P3.VmFinArmIl +import Fl4Proofs.P3.SpecInvNtfnQLA +import Fl4Proofs.P3.VirtFrameBatchConservative +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv + +noncomputable section + +set_option maxHeartbeats 4000000 + +/-- `get_cap` of a slot holding `c` is deterministic and total. -/ +theorem getCap_det {sl : CslotPtr} {σ : State Unit} {c : Cap} (hc : SInvU.capAt σ sl = some c) : + getCap (Z := Unit) sl σ = ({(c, σ)}, False) := by + obtain ⟨o, r⟩ := sl + unfold SInvU.capAt at hc + revert hc + cases hk : σ.kheap o with + | none => intro hc; cases hc + | some obj => + intro hc + unfold getCap + show (getObject o >>= _) σ = _ + rw [nd_bind_single (getObject_det hk)] + cases obj with + | cnode sz cn => + dsimp only at hc ⊢ + rw [capView_cnode] at hc + by_cases hwf : wellFormedCnodeN sz cn + · rw [if_pos hwf] at hc + have h1 : (NonDet.assert (σ := State Unit) (wellFormedCnodeN sz cn)) σ = ({((), σ)}, False) := by + simp [NonDet.assert, hwf] + rw [nd_bind_single h1] + simp only [nd_pure_bind] + simp [NonDet.assertOpt, hc]; rfl + · rw [if_neg hwf] at hc; cases hc + | tcb t => + dsimp only at hc ⊢ + rw [capView_tcb] at hc + simp only [nd_pure_bind] + simp [NonDet.assertOpt, hc]; rfl + | _ => simp [capView] at hc + +/-- `cap_delete` of a frame, written out with the VM-aware finaliser. -/ +def fbDelXV (slot : CslotPtr) : NonDetE (State Unit) Unit Unit := + NonDetE.liftE (do + let c ← getCap (Z := Unit) slot + match c with + | .archObjectCap (.pageCap dev p R mt sz m) => do + let fin ← isFinalCap (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) + let _ ← finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin + emptySlot (Z := Unit) slot .nullCap + | _ => NonDet.fail) + +/-- **`capDeleteVF` of a slot holding a frame cap** is `fbDelXV`'s round. -/ +theorem capDeleteVF_subXV {sl : CslotPtr} {σ : State Unit} {dev : Bool} {p : ObjRef} {R : CapRights} + {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt σ sl = some (.archObjectCap (.pageCap dev p R mt sz m))) : + ∀ y ∈ ((capDeleteVF (Z := Unit) sl).run σ).1, y ∈ ((fbDelXV sl).run σ).1 := by + intro y hy + obtain ⟨r, t⟩ := y + obtain ⟨rfl, u, hfin, hslot⟩ := (Fl4Proofs.P3.capDeleteVF_frame_mem (getCap_det hc)).1 hy + unfold fbDelXV NonDetE.liftE + rw [ExceptT.run_lift] + refine ⟨((), t), ?_, Set.mem_singleton _⟩ + refine ⟨(.archObjectCap (.pageCap dev p R mt sz m), σ), fbx_getCap_of_capAt hc, ?_⟩ + refine ⟨(decide (isFinalCap' (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) σ), σ), rfl, ?_⟩ + exact ⟨((.nullCap, .nullCap), u), hfin, hslot⟩ + +/-! ## The batch with `fbDelXV` -/ + +/-- `fbDelete` with `fbDelXV`. -/ +def fbDeleteXV (slot : CslotPtr) : NonDetE (State Unit) Data Unit := + NonDetE.handleE (fbDelXV slot) fun _ => NonDetE.throwError (msgFromSyscallError .illegalOperation).1 + +/-- `fbEntry` with `fbDelXV` for `capDelete`. -/ +def fbEntryXV (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : NonDetE (State Unit) Data Bool := + match b.op with + | .copyMap => do + let dest ← fbCode (Z := Unit) <| lookupTargetSlot b.destCnode (dataToCptr w.2.1) b.depth + fbCode (Z := Unit) <| ensureEmpty dest + let src ← fbCode (Z := Unit) <| lookupSourceSlot b.cnode (dataToCptr w.1) b.depth + let srcCap ← NonDetE.liftE <| getCap (Z := Unit) src + fbCode (Z := Unit) <| NonDetE.unlessE (isPageCap srcCap) <| NonDetE.throwError (.invalidCapability 0) + let newCap ← fbCode (Z := Unit) <| deriveCap src (maskCap (dataToRights b.capRights) srcCap) + NonDetE.liftE <| capInsert (Z := Unit) newCap src dest + NonDetE.handleE (fbCode (Z := Unit) <| fbMap b dest w.2.2) fun c => do + NonDetE.liftE <| NonDetE.catch (fbDelX dest) fun _ => NonDet.pure () + NonDetE.throwError c + NonDetE.returnOk false + | .remap => do + let slot ← fbCode (Z := Unit) <| lookupSourceSlot b.cnode (dataToCptr w.1) b.depth + fbCode (Z := Unit) <| fbMap b slot w.2.1 + NonDetE.returnOk false + | .delete => do + let slot ← fbCode (Z := Unit) <| lookupTargetSlot b.cnode (dataToCptr w.1) b.depth + let c ← NonDetE.liftE <| getCap (Z := Unit) slot + match c with + | .nullCap => NonDetE.returnOk false + | .archObjectCap (.pageCap dev p R mt sz m) => do + let cleared ← match m with + | some (asid', vaddr) => + if b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid then + NonDetE.liftE (do + let r ← unmapPageUnflushed (Z := Unit) sz b.vspace vaddr p + setCap (Z := Unit) (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) slot + return r) + else NonDetE.returnOk false + | none => NonDetE.returnOk false + fbDeleteXV slot + NonDetE.returnOk cleared + | _ => NonDetE.throwError (msgFromSyscallError .illegalOperation).1 + +/-- `fbRun` with `fbEntryXV`. -/ +def fbRunXV (b : Fl4FrameBatchInvocation) : List (Data × Data × Data) → Nat → Bool → NonDet (State Unit) (Nat × Data × Bool) + | [], done, d => return (done, 0, d) + | w :: ws, done, d => do + let r ← (fbEntryXV b w).run + match r with + | .ok d' => fbRunXV b ws (done + 1) (d || d') + | .error c => return (done, c, d) + +/-- `performFrameBatch` with `fbRunXV`. -/ +def performFrameBatchXV (b : Fl4FrameBatchInvocation) : NonDet (State Unit) (List Data) := do + let (done, err, d) ← fbRunXV b b.entries 0 false + NonDet.when (d = true) <| flushAll (Z := Unit) b.vspace b.asid + return [BitVec.ofNat 64 done, err] + +/-- **One entry**: every outcome of `fbEntry` is one of `fbEntryXV`. -/ +theorem fbEntryVF_subXV (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) (s : State Unit) : + ∀ y ∈ ((fbEntryWith capDeleteVF b w).run s).1, y ∈ ((fbEntryXV b w).run s).1 := by + rcases b with ⟨vs, asid, op, depth, mr, cr, attr, dd, cn, dcn, es⟩ + cases op + case remap => exact fun y hy => hy + case copyMap => + unfold fbEntryWith fbDeferDelete fbEntryXV + dsimp only + refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun dest t0 _ => ?_ + refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun _ t1 _ => ?_ + refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun src t2 _ => ?_ + refine fbx_sub_bindE (fun c t => t = t2 ∧ capAt t src = some c) (fun c t h => ?_) fun srcCap t3 h3 => ?_ + · obtain ⟨⟨c', t'⟩, hx, hxe⟩ := fbx_mem_liftE h + simp only [Prod.mk.injEq, Except.ok.injEq] at hxe + obtain ⟨rfl, rfl⟩ := hxe + obtain ⟨e3, e4⟩ := getCap_capAt hx + dsimp only at e3 e4 + exact ⟨e3, by rw [e3]; exact e4⟩ + refine fbx_sub_bindE (fun _ t => isPageCap srcCap = true ∧ t = t3) (fun _ t h => + fbx_unlessE_ok (fbx_fbCode_ok h)) fun _ t4 ⟨hpg, et4⟩ => ?_ + subst et4 + obtain ⟨dev, p, R, mt, sz, m, rfl⟩ := fbx_frame_of_isPageCap hpg + refine fbx_sub_bindE (fun c t => IsFrameCap c) (fun c t h => ?_) fun newCap t5 h5 => ?_ + · have h' := fbx_fbCode_ok h + simp only [maskCap, capRightsUpdate, acapRightsUpdate, deriveCap, archDeriveCap] at h' + obtain ⟨e1, -⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 h') + simp only [Except.ok.injEq] at e1 + subst e1; exact trivial + refine fbx_sub_bindE (fun _ t => capAt t dest = some newCap) (fun _ t h => ?_) fun _ t6 h6 => ?_ + · obtain ⟨x, hx, hxe⟩ := fbx_mem_liftE h + obtain ⟨-, rfl⟩ := Prod.mk.inj hxe + exact fbx_capInsert_at hx + refine fbx_sub_bindE2 (fbx_sub_handleE2 fun c t7 h7 => ?_) (fun _ _ => True) (fun _ _ _ => trivial) + fun _ _ _ => fun y hy => hy + obtain ⟨e, he⟩ := fbx_fbCode_err h7 + have hca : capAt t7 dest = some newCap := by rw [fbx_fbMap_err he]; exact h6 + revert hca + cases newCap + case archObjectCap a => + cases a + case pageCap dev' p' R' mt' sz' m' => + intro hca + exact fbx_sub_bindE2 (fbx_sub_liftE (fbx_sub_catch (capDelete_subX hca))) (fun _ _ => True) + (fun _ _ _ => trivial) fun _ _ _ => fun y hy => hy + all_goals exact (h5 : False).elim + all_goals exact (h5 : False).elim + case delete => + unfold fbEntryWith fbDeferDelete fbEntryXV + dsimp only + refine fbx_sub_bindE (fun _ _ => True) (fun _ _ _ => trivial) fun slot t0 _ => ?_ + refine fbx_sub_bindE (fun c t => capAt t slot = some c) (fun c t h => ?_) fun c t1 h1 => ?_ + · obtain ⟨⟨c', t'⟩, hx, hxe⟩ := fbx_mem_liftE h + simp only [Prod.mk.injEq, Except.ok.injEq] at hxe + obtain ⟨rfl, rfl⟩ := hxe + obtain ⟨e3, e4⟩ := getCap_capAt hx + dsimp only at e3 e4 + rw [e3]; exact e4 + cases c + case archObjectCap a => + cases a + case pageCap dev p R mt sz m => + have hdel : ∀ t2, capAt t2 slot = some (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) ∨ + (t2 = t1) → ∀ cl : Bool, ∀ y ∈ ((fbDeleteWith capDeleteVF slot >>= fun _ => + (NonDetE.returnOk cl : NonDetE (State Unit) Data Bool)).run t2).1, + y ∈ ((fbDeleteXV slot >>= fun _ => + (NonDetE.returnOk cl : NonDetE (State Unit) Data Bool)).run t2).1 := by + intro t2 h2 cl + have hc2 : ∃ dev p R mt sz m, capAt t2 slot = some (.archObjectCap (.pageCap dev p R mt sz m)) := by + rcases h2 with h2 | rfl + · exact ⟨_, _, _, _, _, _, h2⟩ + · exact ⟨_, _, _, _, _, _, h1⟩ + obtain ⟨_, _, _, _, _, _, hc2⟩ := hc2 + exact fbx_sub_bindE2 (by unfold fbDeleteWith fbDeleteXV; exact fbx_sub_handleE (capDeleteVF_subXV hc2)) + (fun _ _ => True) (fun _ _ _ => trivial) fun _ _ _ => fun y hy => hy + dsimp only + rcases m with _ | ⟨asid', vaddr⟩ + · dsimp only + refine fbx_sub_bindE2 (fun y hy => hy) (fun _ t => t = t1) (fun _ t h => ?_) fun cl t2 h2 => + hdel t2 (Or.inr h2) cl + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + · dsimp only + split + · refine fbx_sub_bindE2 (fun y hy => hy) + (fun _ t => capAt t slot = some (.archObjectCap (.pageCap dev p R .vmNoMap sz none))) + (fun _ t h => ?_) fun cl t2 h2 => hdel t2 (Or.inl h2) cl + obtain ⟨x, hx, hxe⟩ := fbx_mem_liftE h + obtain ⟨-, rfl⟩ := Prod.mk.inj hxe + obtain ⟨z1, -, hx⟩ := hx + obtain ⟨z2, hz2, hx⟩ := hx + rw [Set.mem_singleton_iff.1 hx] + exact setCap_at hz2 + · refine fbx_sub_bindE2 (fun y hy => hy) (fun _ t => t = t1) (fun _ t h => ?_) fun cl t2 h2 => + hdel t2 (Or.inr h2) cl + exact (Prod.mk.inj (Set.mem_singleton_iff.1 h)).2 + all_goals exact fun y hy => hy + all_goals exact fun y hy => hy + +/-- **The loop**. -/ +theorem fbRunVF_subXV (b : Fl4FrameBatchInvocation) : + ∀ (ws : List (Data × Data × Data)) (n : Nat) (d : Bool) (s : State Unit), + ∀ y ∈ (fbRunWith capDeleteVF b ws n d s).1, y ∈ (fbRunXV b ws n d s).1 + | [], n, d, s => fun y hy => hy + | w :: ws, n, d, s => by + unfold fbRunWith fbRunXV + refine fbx_sub_bind (fbEntryVF_subXV b w s) fun r t _ => ?_ + cases r with + | ok d' => exact fbRunVF_subXV b ws (n + 1) (d || d') t + | error c => exact fun y hy => hy + +/-- **The batch**. -/ +theorem performFrameBatchVF_subXV (b : Fl4FrameBatchInvocation) (s : State Unit) : + ∀ y ∈ (performFrameBatchVF b s).1, y ∈ (performFrameBatchXV b s).1 := by + unfold performFrameBatchVF performFrameBatchWith performFrameBatchXV + exact fbx_sub_bind (fbRunVF_subXV b _ _ _ s) fun _ _ _ => fun y hy => hy + +theorem ike_fbEntryVF_ofXV {P : State Unit → Prop} {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} + (h : IKE P (fbEntryXV b w)) : IKE P (fbEntryWith capDeleteVF b w) := + fun s hs y hy => h s hs y (fbEntryVF_subXV b w s y hy) + +theorem ik_performFrameBatchVF_ofXV {P : State Unit → Prop} {b : Fl4FrameBatchInvocation} + (h : IK P (performFrameBatchXV b)) : IK P (performFrameBatchVF b) := + fun s hs y hy => h s hs y (performFrameBatchVF_subXV b s y hy) + +/-- `IK P` of `fbRunXV` from `IK P` of one `fbEntryXV`. -/ +theorem ik_fbRunXV {P : State Unit → Prop} {b : Fl4FrameBatchInvocation} (h : ∀ w, IKE P (fbEntryXV b w)) : + ∀ (ws : List (Data × Data × Data)) (n : Nat) (d : Bool), IK P (fbRunXV b ws n d) + | [], n, d => ik_pure _ + | w :: ws, n, d => by + unfold fbRunXV + refine ik_bind (h w) fun r => ?_ + cases r with + | ok d' => exact ik_fbRunXV h ws (n + 1) (d || d') + | error c => exact ik_pure _ + +/-- **`IK P` of the batch** from `IK P` of one `fbEntryXV` and of the final flush. -/ +theorem ik_performFrameBatchXV {P : State Unit → Prop} {b : Fl4FrameBatchInvocation} + (h : ∀ w, IKE P (fbEntryXV b w)) (hf : IK P (flushAll (Z := Unit) b.vspace b.asid)) : + IK P (performFrameBatchVF b) := by + refine ik_performFrameBatchVF_ofXV ?_ + unfold performFrameBatchXV + refine ik_bind (ik_fbRunXV h _ _ _) fun r => ?_ + obtain ⟨done, err, d⟩ := r + exact ik_bind (ik_when _ hf) fun _ => ik_pure _ + +/-- **`IKE P` of one entry** from `IKE P` / `IK P` of its steps (a cap inserted or written by an entry is a +frame cap). A family proves each step with its own `*_auto`; no step unfolds the whole entry. -/ +theorem ik_fbEntryXV_of {P : State Unit → Prop} (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) + (hlt : ∀ cn c d, IKE P (lookupTargetSlot (Z := Unit) cn c d)) + (hls : ∀ cn c d, IKE P (lookupSourceSlot (Z := Unit) cn c d)) + (hee : ∀ sl, IKE P (ensureEmpty (Z := Unit) sl)) + (hdc : ∀ sl c, IKE P (deriveCap (Z := Unit) sl c)) + (hci : ∀ s c src dest c0, P s → IsFrameCap c → + (.ok dest, s) ∈ ((lookupTargetSlot (Z := Unit) b.destCnode (dataToCptr w.2.1) b.depth).run s).1 → + capAt s dest = some .nullCap → capAt s src = some c0 → isPageCap c0 = true → + (.ok c, s) ∈ ((deriveCap (Z := Unit) src (maskCap (dataToRights b.capRights) c0)).run s).1 → + ∀ x ∈ (capInsert (Z := Unit) c src dest s).1, P x.2) + (hmap : ∀ sl v, IKE P (fbMap (Z := Unit) b sl v)) + (hdelN : ∀ sl, IKE P (fbDelX sl)) + (hdel : ∀ sl, IKE P (fbDelXV sl)) + (hun : ∀ sz vs va p, IK P (unmapPageUnflushed (Z := Unit) sz vs va p)) + (hset : ∀ s dev p R mt sz m sl, P s → capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m)) → + ∀ x ∈ (setCap (Z := Unit) (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) sl s).1, P x.2) : + IKE P (fbEntryXV b w) := by + have hcode : ∀ {Q : State Unit → Prop} {α : Type} {m : SEMonad Unit α}, IKE Q m → IKE Q (fbCode (Z := Unit) m) := + fun h => ike_handleE h fun _ => ike_throw _ + rcases b with ⟨vs, asid, op, depth, mr, cr, attr, dd, cn, dcn, es⟩ + cases op + case remap => + unfold fbEntryXV; dsimp only + exact ike_bind (hcode (hls _ _ _)) fun sl => ike_bind (hcode (hmap _ _)) fun _ => ike_returnOk _ + case copyMap => + unfold fbEntryXV; dsimp only + intro s hs + refine fbx_ikp_bind (fun dest t => t = s ∧ + (.ok dest, s) ∈ ((lookupTargetSlot (Z := Unit) dcn (dataToCptr w.2.1) depth).run s).1) + (hcode (hlt _ _ _) s hs) (fun a t h => ?_) fun dest t ⟨e, hdest⟩ => ?_ + · have e : t = s := hcode (fbx_lookupTargetSlot (P := (· = s)) _ _ _) s rfl _ h + subst t; exact ⟨rfl, fbx_fbCode_ok h⟩ + subst t + refine fbx_ikp_bind (fun _ t => t = s ∧ capAt s dest = some .nullCap) (hcode (hee _) s hs) (fun a t h => ?_) + fun _ t ⟨e, hnull⟩ => ?_ + · have e : t = s := hcode (fbx_ensureEmpty (P := (· = s)) _) s rfl _ h + subst t; exact ⟨rfl, fbx_ensureEmpty_null (fbx_fbCode_ok h)⟩ + subst t + refine fbx_ikp_bind (fun _ t => t = s) (hcode (hls _ _ _) s hs) + (fun a t h => hcode (fbx_lookupSourceSlot (P := (· = s)) _ _ _) s rfl _ h) fun src t e => ?_ + subst t + refine fbx_ikp_bind (fun c0 t => t = s ∧ capAt s src = some c0) (ike_liftE (fbx_ik_getCap _) s hs) + (fun a t h => ?_) fun c0 t ⟨e, hc0⟩ => ?_ + · obtain ⟨x, hx, hxe⟩ := fbx_mem_liftE h + simp only [Prod.mk.injEq, Except.ok.injEq] at hxe + obtain ⟨rfl, rfl⟩ := hxe + exact getCap_capAt hx + subst t + refine fbx_ikp_bind (fun _ t => isPageCap c0 = true ∧ t = s) (hcode (ike_unlessE _ (ike_throw _)) s hs) + (fun a t h => fbx_unlessE_ok (fbx_fbCode_ok h)) fun _ t ⟨hpg, e⟩ => ?_ + subst t + refine fbx_ikp_bind (fun c t => t = s ∧ + (.ok c, s) ∈ ((deriveCap (Z := Unit) src (maskCap (dataToRights cr) c0)).run s).1) + (hcode (hdc _ _) s hs) (fun a t h => ?_) fun c t ⟨e, hder⟩ => ?_ + · have e : t = s := hcode (fbx_deriveCap (P := (· = s)) _ _) s rfl _ h + subst t; exact ⟨rfl, fbx_fbCode_ok h⟩ + subst t + have hfc : IsFrameCap c := by + obtain ⟨dev, p, R, mt, sz, m, rfl⟩ := fbx_frame_of_isPageCap hpg + have hder' := hder + simp only [maskCap, capRightsUpdate, acapRightsUpdate, deriveCap, archDeriveCap] at hder' + obtain ⟨e1, -⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hder') + simp only [Except.ok.injEq] at e1 + subst e1; exact trivial + have hci' : ∀ y ∈ ((NonDetE.liftE (ε := Data) (capInsert (Z := Unit) c src dest)).run s).1, P y.2 := by + intro y hy + obtain ⟨x, hx, rfl⟩ := fbx_mem_liftE hy + exact hci s c src dest c0 hs hfc hdest hnull hc0 hpg hder x hx + refine fbx_ikp_bind (fun _ t => P t) hci' (fun a t h => hci' _ h) fun _ t ht => ?_ + exact (ike_bind (ike_handleE (hcode (hmap _ _)) fun c => + ike_bind (ike_liftE (ik_catch (hdelN _) fun _ => ik_pure' _)) fun _ => ike_throw _) + fun _ => ike_returnOk _ : IKE P _) t ht + case delete => + unfold fbEntryXV; dsimp only + refine ike_bind (hcode (hlt _ _ _)) fun slot => ?_ + have hd : ∀ cl : Bool, IKE P (fbDeleteXV slot >>= fun _ => (NonDetE.returnOk cl : NonDetE (State Unit) Data Bool)) := + fun cl => ike_bind (by unfold fbDeleteXV; exact ike_handleE (hdel _) fun _ => ike_throw _) fun _ => ike_returnOk _ + intro s hs y hy + obtain ⟨z, hz, hr⟩ := fbx_mem_bindE hy + obtain ⟨x, hx, rfl⟩ := fbx_mem_liftE hz + obtain ⟨e1, hcap⟩ := getCap_capAt hx + rcases hr with ⟨c, hc, hy⟩ | ⟨e, he, -⟩ + swap; · cases he + simp only [Except.ok.injEq] at hc + subst hc + obtain ⟨c, t⟩ := x + try dsimp only at e1 hcap hy + subst s + cases c + case nullCap => exact ike_returnOk _ t hs y hy + case archObjectCap a => + cases a + case pageCap dev p R mt sz m => + rcases m with _ | ⟨asid', vaddr⟩ + · try dsimp only at hy + first | exact hd _ t hs y hy | exact ike_returnOk_bind (hd _) t hs y hy | exact ike_bind (ike_returnOk _) (fun cl => hd cl) t hs y hy + · try dsimp only at hy + by_cases hcond : (dd = true ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = asid) + · rw [if_pos hcond] at hy + obtain ⟨z2, hz2, hr2⟩ := fbx_mem_bindE hy + obtain ⟨x2, hx2, rfl⟩ := fbx_mem_liftE hz2 + rcases hr2 with ⟨cl, -, hy⟩ | ⟨e, he, -⟩ + swap; · cases he + obtain ⟨u1, hu1, hx2⟩ := hx2 + obtain ⟨u2, hu2, hx2⟩ := hx2 + rw [Set.mem_singleton_iff.1 hx2] at hy + have hP1 := hun _ _ _ _ t hs u1 hu1 + have hv1 : uV u1.2 = uV t := uk_unmapPageUnflushed (uvw := uV t) _ _ _ _ t rfl u1 hu1 + have hc1 : capAt u1.2 slot = some (.archObjectCap (.pageCap dev p R mt sz (some (asid', vaddr)))) := by + have := congrArg (fun w : UView => w.2.1 slot) hv1 + simp only [uV] at this + rw [this]; exact hcap + have hP2 := hset u1.2 dev p R mt sz (some (asid', vaddr)) slot hP1 hc1 u2 hu2 + exact hd _ _ hP2 y hy + · rw [if_neg hcond] at hy + first | exact hd _ t hs y hy | exact ike_returnOk_bind (hd _) t hs y hy | exact ike_bind (ike_returnOk _) (fun cl => hd cl) t hs y hy + all_goals exact ike_throw _ t hs y hy + all_goals exact ike_throw _ t hs y hy + + + +/-! ## `IL K` across the batch -/ + +theorem ik_fbDelXVF {P : State Unit → Prop} (sl : CslotPtr) + (hf : ∀ c, IK P (isFinalCap (Z := Unit) c)) + (ha : ∀ dev p R mt sz m fin, IK P (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin)) + (hu : ∀ dev p R mt sz m fin (v : UView), UK v (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin)) + (he : ∀ s dev p R mt sz m, P s → SInvU.capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m)) → + ∀ x ∈ (emptySlot (Z := Unit) sl .nullCap s).1, P x.2) : + IKE P (fbDelXV sl) := by + intro σ hσ y hy + unfold fbDelXV at hy + obtain ⟨x, hx, rfl⟩ := fbx_mem_liftE hy + obtain ⟨z, hz, hx⟩ := hx + obtain ⟨e1, hcap⟩ := getCap_capAt hz + obtain ⟨c, s1⟩ := z + simp only at e1 hcap; subst e1 + dsimp only at hx + split at hx + · rename_i dev p R mt sz m + obtain ⟨z1, hz1, hx⟩ := hx + have hv1 : uV z1.2 = uV s1 := uk_isFinalCap (uvw := uV s1) _ s1 rfl z1 hz1 + have hP1 := hf _ s1 hσ z1 hz1 + obtain ⟨z2, hz2, hx⟩ := hx + have hv2 : uV z2.2 = uV z1.2 := hu dev p R mt sz m _ (uV z1.2) z1.2 rfl z2 hz2 + have hP2 := ha _ _ _ _ _ _ _ z1.2 hP1 z2 hz2 + have hc2 : SInvU.capAt z2.2 sl = some (.archObjectCap (.pageCap dev p R mt sz m)) := by + have := congrArg (fun w : UView => w.2.1 sl) (hv2.trans hv1) + simp only [uV] at this + rw [this]; exact hcap + exact he z2.2 dev p R mt sz m hP2 hc2 x hx + · exact hx.elim + +theorem uk_finaliseCapVF_frame {v : UView} (dev : Bool) (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) + (m : Option (Asid × VSpaceRef)) (fin : Bool) : + UK v (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin) := by + rcases m with _ | ⟨a, gpa⟩ <;> cases mt <;> first + | exact uk_archFinaliseCap _ _ + | (simp only [finaliseCapVF] + refine ik_bind ?_ fun _ => ik_pure _ + unfold NonDet.when + split + · exact Fl4Proofs.P3.VmFin.uk_guestUnmap _ _ _ _ _ + · exact ik_pure _) + +theorem il_finaliseCapVF_frame {K : List ObjRef} (dev : Bool) (p : ObjRef) (R : CapRights) (mt : VMMapType) + (sz : VMPageSize) (m : Option (Asid × VSpaceRef)) (fin : Bool) : + ILK K (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin) := by + rcases m with _ | ⟨a, gpa⟩ <;> cases mt <;> first + | exact il_archFinaliseCap _ _ + | (simp only [finaliseCapVF] + refine ik_bind ?_ fun _ => ik_pure _ + unfold NonDet.when + split + · exact Fl4Proofs.P3.VmFin.ik_guestUnmapV (fun p a => Fl4Proofs.P3.VmFin.il_setArchV K p a) _ _ _ _ _ + · exact ik_pure _) +/-- **The extended dispatcher's frame batch keeps `IL K`** (the native `il_performFrameBatch`'s statement). -/ +theorem il_performFrameBatchVF {K : List ObjRef} (b : Fl4FrameBatchInvocation) : + ILK K (performFrameBatchVF (Z := Unit) b) := by + refine ik_performFrameBatchXV (fun w => ik_fbEntryXV_of b w fbx_lookupTargetSlot fbx_lookupSourceSlot fbx_ensureEmpty + fbx_deriveCap (fun s c src dest _ hs _ _ _ _ _ _ => il_capInsert c src dest s hs) + (fun sl v => ik_fbMapF b sl v (fun _ _ _ _ _ _ => il_decodePageInvocation _ _ _ _ _) + (fun s pi hs hok x hx => il_pageInv pi s hs hok x hx)) + (fun sl => ik_fbDelXF sl (fun _ => ik_gets _) (fun c x => il_archFinaliseCap c x) + (fun s _ _ _ _ _ _ hs hc x hx => il_emptySlot_p sl .nullCap s (fun t h => by rw [hc] at h; simp at h) + (by simp only [postCapDeletion]; il_auto) hs x hx)) + (fun sl => ik_fbDelXVF sl (fun _ => ik_gets _) (fun dev p R mt sz m fin => il_finaliseCapVF_frame dev p R mt sz m fin) + (fun dev p R mt sz m fin v => uk_finaliseCapVF_frame dev p R mt sz m fin) + (fun s _ _ _ _ _ _ hs hc x hx => il_emptySlot_p sl .nullCap s (fun t h => by rw [hc] at h; simp at h) + (by simp only [postCapDeletion]; il_auto) hs x hx)) + (fun _ _ _ _ => by unfold unmapPageUnflushed; il_auto) + (fun s _ _ _ _ _ _ sl hs hc0 x hx => il_setCap_nt _ sl s (fun t h => by rw [hc0] at h; simp at h) hs x hx)) + (il_flushAll _ _) + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinFrontS3.lean b/proofs/Fl4Proofs/P3/VmFinFrontS3.lean new file mode 100644 index 000000000..517784d40 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinFrontS3.lean @@ -0,0 +1,855 @@ +import Fl4Proofs.P3.SpecInvDom +import Fl4Proofs.P3.SpecInvFoS +import Fl4Proofs.P3.VmFinArmDl +import Fl4Proofs.P3.VmFinArmFoTop +import Fl4Proofs.P3.VmFinArmVgC +import Fl4Proofs.P3.VmFinArmIlVm +import Fl4Proofs.P3.VmFinDecode +import Fl4Proofs.P3.VmHiRvm +import Fl4Proofs.P3.VmRecvFin +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmMidRunQ + +/-! +P3 (line mmuAD, Stage 3, step (a)): **the finish facts at every outcome of the extended syscall front** (spec only): +from the entry facts, every outcome `x` of `specFrontVF onCore remoteCur c σ` (budget stop, every arm of +`fl4HandleSyscallVF`, the preemption path) has `DomListOK`, `FpuOwnOK`, `IdleVtS`, and no thread becomes `runningVM`. + +* `preemptionPathVF` keeps all of them (`dl`, FPU-owner frame, untyped view, idle thread, `runningVM`); +* `fin_hiVF`: the extended invocation from `JF N ct ∧ VG ∧ DomListOK`; at both outcomes the three facts, and `VG` + after a first-phase decode (what the following receive reads). SetEPTRoot (not first phase) keeps `IdleVtS` by its + delete and its insert into slot 5; +* the receive and the yield by `recvFinFrames` / `yieldFinFrames`. + +No named premise. [P] `numDomains = 1`. New file. +-/ + +open Classical + +namespace Fl4Proofs.P3.FinFront +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU Fl4Proofs.P3.SInvF Fl4Proofs.P3.SInvDom + Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P3.MidRun + +noncomputable section + +set_option maxHeartbeats 4000000 + +/-! ## The extended decode -/ + +theorem rd_decodeSetEptRoot (t : ObjRef) (ex : List (Cap × CslotPtr)) : + FKE (fun σ : State Unit => σ) (decodeSetEptRoot (Z := Unit) t ex) := by + unfold decodeSetEptRoot deriveCap archDeriveCap + repeat' (first | dk_step0 | split) + +/-- The extended decode keeps what the native decode keeps (its VM branches only read). -/ +theorem ike_decodeV {P : State Unit → Prop} (fp : Bool) (label : Data) (args : List Data) (ci : CapRef) + (sl : CslotPtr) (cap : Cap) (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) + (hb : IKE P (fl4DecodeInvocation (Z := Unit) fp label args ci sl cap ex buf)) : + IKE P (fl4DecodeInvocationV (Z := Unit) fp label args ci sl cap ex buf) := by + have hr : ∀ {α : Type} {m : NonDetE (State Unit) SyscallError α}, FKE (fun σ : State Unit => σ) m → IKE P m := + fun h σ hσ y hy => by rw [show y.2 = σ from h σ y hy]; exact hσ + have hb' : IKE P (NonDetE.liftME Fl4InvocationV.base + (fl4DecodeInvocation (Z := Unit) fp label args ci sl cap ex buf)) := ike_liftME _ hb + unfold fl4DecodeInvocationV + dsimp only + split + · exact hr (dke_liftME _ (rd_decodeVcpuInvocation _ _ _ _)) + · exact hr (dke_liftME _ (rd_decodeEptInvocation _ _ _ _ _)) + · split + · exact hr (dke_liftME _ (rd_decodePageMapEpt _ _ _ _)) + · split + · exact ike_throw _ + · split + · exact ike_returnOk _ + · exact hb' + · split + · exact hr (dke_liftME _ (rd_decodeEptAsidPoolAssign _ _ _)) + · exact hb' + · split + · exact hr (dke_liftME _ (rd_decodeSetEptRoot _ _)) + · exact hb' + · split + · exact ike_throw _ + · exact hb' + · exact hb' + +/-- A first-phase decode gives no SetEPTRoot. -/ +theorem okr_noSetEpt (label : Data) (args : List Data) (ci : CapRef) (sl : CslotPtr) (cap : Cap) + (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) : + VmFin.OkR (fun b => ∀ i, b ≠ .setEptRoot i) + (fl4DecodeInvocationV (Z := Unit) true label args ci sl cap ex buf) := by + have lift : ∀ {α : Type} (f : α → Fl4InvocationV) (m : SEMonad Unit α), (∀ a i, f a ≠ .setEptRoot i) → + VmFin.OkR (fun b => ∀ i, b ≠ .setEptRoot i) (NonDetE.liftME f m) := + fun f m h => VmFin.okr_liftME fun _ a _ _ => h a + unfold fl4DecodeInvocationV + dsimp only + split + · exact lift _ _ fun _ _ h => by cases h + · exact lift _ _ fun _ _ h => by cases h + · split + · exact lift _ _ fun _ _ h => by cases h + · split + · exact VmFin.okr_throw _ + · split + · exact VmFin.okr_returnOk fun _ h => by cases h + · exact lift _ _ fun _ _ h => by cases h + · split + · exact lift _ _ fun _ _ h => by cases h + · exact lift _ _ fun _ _ h => by cases h + · rw [if_neg (fun h => h.1 rfl)]; exact lift _ _ fun _ _ h => by cases h + · split + · exact VmFin.okr_throw _ + · exact lift _ _ fun _ _ h => by cases h + · exact lift _ _ fun _ _ h => by cases h + +/-! ## The domain list across the extended dispatcher and the preemption path -/ + +theorem dpe_performVF (onCore remoteCur : ObjRef → Bool) (blk call don : Bool) (inv : Fl4InvocationV) + (σ : State Unit) (hs : DomListOK σ) (hi : ∀ b, inv = .base b → DomInvOK σ b) : + ∀ x ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur blk call don inv).run σ).1, DomListOK x.2 := by + intro x hx + by_cases hd : ∃ d, inv = .base (.invokeDomain d) + · obtain ⟨d, rfl⟩ := hd + exact dpe_perform blk call don (.invokeDomain d) σ x hs (hi _ rfl) hx + · exact dpe_of_dle (dle_fl4PerformVF onCore remoteCur blk call don inv fun d e => hd ⟨d, e⟩) σ x hs hx + +theorem dl_preemptionPathVF (onCore remoteCur : ObjRef → Bool) : + DL (preemptionPathVF (Z := Unit) onCore remoteCur) := by + unfold preemptionPathVF maybeHandleInterruptVF handleInterruptVF + dl_auto + +/-! ## The three facts -/ + +/-- The finish facts' spec half. -/ +def F3 (σ : State Unit) : Prop := DomListOK σ ∧ FpuOwnOK σ ∧ SInvIV.IdleVtS σ + +/-- `F3`, with `VG` after a first phase. -/ +def FQ (fp : Bool) (σ : State Unit) : Prop := F3 σ ∧ (fp = true → VmFin.VG σ) + +theorem idleVtS_view {σ σ' : State Unit} (eu : uV σ' = uV σ) (ei : σ'.idleThread = σ.idleThread) + (h : SInvIV.IdleVtS σ) : SInvIV.IdleVtS σ' := by + unfold SInvIV.IdleVtS at h ⊢ + rw [eu, ei]; exact h + +theorem fpu_oi {α : Type} {m : NonDet (State Unit) α} (h : ∀ q, SInvF.OIK q m) : IK FpuOwnOK m := by + intro σ hσ y hy + obtain ⟨e, h'⟩ := h _ σ ⟨rfl, hσ⟩ y hy + exact fun o ho => h' o (e.symm.trans ho) + +theorem f3_keep {α : Type} {m : NonDet (State Unit) α} (hd : DL m) (ho : ∀ q, SInvF.OIK q m) (hu : ∀ v, UK v m) + (hi : ∀ t, SInvG.IdK t m) : IK F3 m := fun σ h y hy => + ⟨domListOK_of_dlV (hd σ y hy) h.1, fpu_oi ho σ h.2.1 y hy, + idleVtS_view (hu _ σ rfl y hy) (hi _ σ rfl y hy) h.2.2⟩ + +theorem vg_view {α : Type} {m : NonDet (State Unit) α} (hu : ∀ v, UK v m) (hn : ∀ N, SInvU.NK N m) + (hi : ∀ t, SInvG.IdK t m) : IK VmFin.VG m := + VmFin.vg_step (fun _ _ => SInvIV.iw_of_uk hu) (fun _ _ => SInvG.s_of_uk hu) hn hi + +theorem fq_keep {fp : Bool} {α : Type} {m : NonDet (State Unit) α} (hd : DL m) (ho : ∀ q, SInvF.OIK q m) + (hu : ∀ v, UK v m) (hn : ∀ N, SInvU.NK N m) (hi : ∀ t, SInvG.IdK t m) : IK (FQ fp) m := fun σ h y hy => + ⟨f3_keep hd ho hu hi σ h.1 y hy, fun e => vg_view hu hn hi σ (h.2 e) y hy⟩ + +/-! ## The preemption path -/ + +theorem nk_sendSignalVFF {N : Irq → ObjRef} (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + SInvU.NK N (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + unfold sendSignalVF vmSignalCompletes Fl4Spec.vmFullReply; nk_auto + +theorem idk_sendSignalVFF {K : ObjRef} (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (bd : Badge) : + SInvG.IdK K (sendSignalVF (Z := Unit) onCore remoteCur n bd) := by + unfold sendSignalVF vmSignalCompletes Fl4Spec.vmFullReply; idk_auto + +/-- The extended handler's leaves compose it. -/ +theorem ik_hiG3 {P : State Unit → Prop} (sig : ObjRef → Badge → SMonad Unit Unit) (i : Irq) + (h1 : SInv.IK P (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (h2 : SInv.IK P (getIrqState (Z := Unit) i)) (h3 : SInv.IK P (getIrqSlot (Z := Unit) i)) + (h4 : ∀ sl, SInv.IK P (getCap (Z := Unit) sl)) (h5 : ∀ n b, SInv.IK P (sig n b)) + (h6 : SInv.IK P (archMaskIrqSignal (Z := Unit) i)) (h7 : SInv.IK P (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (h8 : SInv.IK P (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (h9 : SInv.IK P (NonDet.fail : NonDet (State Unit) Unit)) (h10 : SInv.IK P (handleReservedIrq (Z := Unit) i)) + (h11 : SInv.IK P (doMachineOp (Z := Unit) (ackInterrupt i))) : SInv.IK P (hiS sig i) := by + unfold hiS + repeat' (first + | with_reducible exact h1 | with_reducible exact h2 | with_reducible exact h3 | with_reducible exact h4 _ + | with_reducible exact h5 _ _ | with_reducible exact h6 | with_reducible exact h7 | with_reducible exact h8 + | with_reducible exact h9 | with_reducible exact h10 | with_reducible exact h11 + | with_reducible exact SInv.ik_pure _ | with_reducible exact SInv.ik_pure' _ + | (with_reducible refine SInv.ik_bind ?_ (fun _ => ?_)) + | (unfold NonDet.when) | split | dsimp only) + +/-- The extended preemption path's leaves compose it. -/ +theorem ik_preG3 {P : State Unit → Prop} (onCore remoteCur : ObjRef → Bool) + (g : ∀ (α : Type) (f : State Unit → α), SInv.IK P (NonDet.gets f)) + (h1 : SInv.IK P (checkBudget (Z := Unit))) (h2 : ∀ c b, SInv.IK P (chargeBudget (Z := Unit) c b)) + (h3 : ∀ sc, SInv.IK P (getScActive (Z := Unit) sc)) + (h4 : SInv.IK P (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (h5 : SInv.IK P (doMachineOp (Z := Unit) (getActiveIRQ true))) (h6 : SInv.IK P (handleSpuriousIrq (Z := Unit))) + (h7 : ∀ i, SInv.IK P (hiS (sendSignalVF (Z := Unit) onCore remoteCur) i)) : + SInv.IK P (preemptionPathVF (Z := Unit) onCore remoteCur) := by + have hm : SInv.IK P (maybeHandleInterruptVF (Z := Unit) onCore remoteCur true) := by + unfold maybeHandleInterruptVF + simp only [handleInterruptVF_hiS] + refine SInv.ik_bind h5 (fun o => ?_) + cases o + · exact h6 + · exact h7 _ + rw [preemptionPathVF_ppG] + generalize maybeHandleInterruptVF (Z := Unit) onCore remoteCur true = tl at hm ⊢ + unfold ppG + repeat' (first + | with_reducible exact g _ _ | with_reducible exact h1 | with_reducible exact h2 _ _ + | with_reducible exact h3 _ | with_reducible exact h4 | with_reducible exact hm + | with_reducible exact SInv.ik_pure _ | with_reducible exact SInv.ik_pure' _ + | (with_reducible refine SInv.ik_bind ?_ (fun _ => ?_)) + | split | dsimp only) + +theorem oif_preemptionPathVF {Qo : Option ObjRef} (onCore remoteCur : ObjRef → Bool) : + SInvF.OIK Qo (preemptionPathVF (Z := Unit) onCore remoteCur) := + ik_preG3 onCore remoteCur (fun _ f => (by oi_auto : SInvF.OIK Qo (NonDet.gets f))) + SInvF.oi_checkBudget + (fun c b => SInvF.oi_chargeBudget c b) + (fun sc => SInvF.oi_getScActive sc) + (by oi_auto : SInvF.OIK Qo (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (by oi_auto : SInvF.OIK Qo (doMachineOp (Z := Unit) (getActiveIRQ true))) + (by oi_auto : SInvF.OIK Qo (handleSpuriousIrq (Z := Unit))) + (fun i => ik_hiG3 _ i + (by oi_auto : SInvF.OIK Qo (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (by oi_auto : SInvF.OIK Qo (getIrqState (Z := Unit) i)) + (by oi_auto : SInvF.OIK Qo (getIrqSlot (Z := Unit) i)) + (fun sl => (by oi_auto : SInvF.OIK Qo (getCap (Z := Unit) sl))) + (fun _ _ => oif_sendSignalVF _ _ _ _) + (by oi_auto : SInvF.OIK Qo (archMaskIrqSignal (Z := Unit) i)) + (by oi_auto : SInvF.OIK Qo (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (by oi_auto : SInvF.OIK Qo (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (by oi_auto : SInvF.OIK Qo (NonDet.fail : NonDet (State Unit) Unit)) + (by oi_auto : SInvF.OIK Qo (handleReservedIrq (Z := Unit) i)) + (by oi_auto : SInvF.OIK Qo (doMachineOp (Z := Unit) (ackInterrupt i)))) + +theorem iw_preemptionPathVF {N : Irq → ObjRef} {t : ObjRef} (onCore remoteCur : ObjRef → Bool) : + SInv.IK (SInvIV.SI N t) (preemptionPathVF (Z := Unit) onCore remoteCur) := + ik_preG3 onCore remoteCur (fun _ f => (by iw_auto : SInv.IK (SInvIV.SI N t) (NonDet.gets f))) + (by iw_auto : SInv.IK (SInvIV.SI N t) (checkBudget (Z := Unit))) + (fun c b => (by iw_auto : SInv.IK (SInvIV.SI N t) (chargeBudget (Z := Unit) c b))) + (fun sc => (by iw_auto : SInv.IK (SInvIV.SI N t) (getScActive (Z := Unit) sc))) + (by iw_auto : SInv.IK (SInvIV.SI N t) (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (by iw_auto : SInv.IK (SInvIV.SI N t) (doMachineOp (Z := Unit) (getActiveIRQ true))) + (by iw_auto : SInv.IK (SInvIV.SI N t) (handleSpuriousIrq (Z := Unit))) + (fun i => ik_hiG3 _ i + (by iw_auto : SInv.IK (SInvIV.SI N t) (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (by iw_auto : SInv.IK (SInvIV.SI N t) (getIrqState (Z := Unit) i)) + (by iw_auto : SInv.IK (SInvIV.SI N t) (getIrqSlot (Z := Unit) i)) + (fun sl => (by iw_auto : SInv.IK (SInvIV.SI N t) (getCap (Z := Unit) sl))) + (fun _ _ => SInvIV.iw_of_uk (fun _ => uk_sendSignalVF _ _ _ _)) + (by iw_auto : SInv.IK (SInvIV.SI N t) (archMaskIrqSignal (Z := Unit) i)) + (by iw_auto : SInv.IK (SInvIV.SI N t) (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (by iw_auto : SInv.IK (SInvIV.SI N t) (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (by iw_auto : SInv.IK (SInvIV.SI N t) (NonDet.fail : NonDet (State Unit) Unit)) + (by iw_auto : SInv.IK (SInvIV.SI N t) (handleReservedIrq (Z := Unit) i)) + (by iw_auto : SInv.IK (SInvIV.SI N t) (doMachineOp (Z := Unit) (ackInterrupt i)))) + +theorem sg_preemptionPathVF {N M : Irq → ObjRef} (onCore remoteCur : ObjRef → Bool) : + SInv.IK (SInvG.S N M) (preemptionPathVF (Z := Unit) onCore remoteCur) := + ik_preG3 onCore remoteCur (fun _ f => (by sg_auto : SInv.IK (SInvG.S N M) (NonDet.gets f))) + (by sg_auto : SInv.IK (SInvG.S N M) (checkBudget (Z := Unit))) + (fun c b => (by sg_auto : SInv.IK (SInvG.S N M) (chargeBudget (Z := Unit) c b))) + (fun sc => (by sg_auto : SInv.IK (SInvG.S N M) (getScActive (Z := Unit) sc))) + (by sg_auto : SInv.IK (SInvG.S N M) (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (by sg_auto : SInv.IK (SInvG.S N M) (doMachineOp (Z := Unit) (getActiveIRQ true))) + (by sg_auto : SInv.IK (SInvG.S N M) (handleSpuriousIrq (Z := Unit))) + (fun i => ik_hiG3 _ i + (by sg_auto : SInv.IK (SInvG.S N M) (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (by sg_auto : SInv.IK (SInvG.S N M) (getIrqState (Z := Unit) i)) + (by sg_auto : SInv.IK (SInvG.S N M) (getIrqSlot (Z := Unit) i)) + (fun sl => (by sg_auto : SInv.IK (SInvG.S N M) (getCap (Z := Unit) sl))) + (fun _ _ => SInvG.s_of_uk (fun _ => uk_sendSignalVF _ _ _ _)) + (by sg_auto : SInv.IK (SInvG.S N M) (archMaskIrqSignal (Z := Unit) i)) + (by sg_auto : SInv.IK (SInvG.S N M) (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (by sg_auto : SInv.IK (SInvG.S N M) (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (by sg_auto : SInv.IK (SInvG.S N M) (NonDet.fail : NonDet (State Unit) Unit)) + (by sg_auto : SInv.IK (SInvG.S N M) (handleReservedIrq (Z := Unit) i)) + (by sg_auto : SInv.IK (SInvG.S N M) (doMachineOp (Z := Unit) (ackInterrupt i)))) + +theorem nk_preemptionPathVF {N : Irq → ObjRef} (onCore remoteCur : ObjRef → Bool) : + SInvU.NK N (preemptionPathVF (Z := Unit) onCore remoteCur) := + ik_preG3 onCore remoteCur (fun _ f => (by nk_auto : SInvU.NK N (NonDet.gets f))) + (by nk_auto : SInvU.NK N (checkBudget (Z := Unit))) + (fun c b => (by nk_auto : SInvU.NK N (chargeBudget (Z := Unit) c b))) + (fun sc => (by nk_auto : SInvU.NK N (getScActive (Z := Unit) sc))) + (by nk_auto : SInvU.NK N (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (by nk_auto : SInvU.NK N (doMachineOp (Z := Unit) (getActiveIRQ true))) + (by nk_auto : SInvU.NK N (handleSpuriousIrq (Z := Unit))) + (fun i => ik_hiG3 _ i + (by nk_auto : SInvU.NK N (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (by nk_auto : SInvU.NK N (getIrqState (Z := Unit) i)) + (by nk_auto : SInvU.NK N (getIrqSlot (Z := Unit) i)) + (fun sl => (by nk_auto : SInvU.NK N (getCap (Z := Unit) sl))) + (fun _ _ => nk_sendSignalVFF _ _ _ _) + (by nk_auto : SInvU.NK N (archMaskIrqSignal (Z := Unit) i)) + (by nk_auto : SInvU.NK N (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (by nk_auto : SInvU.NK N (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (by nk_auto : SInvU.NK N (NonDet.fail : NonDet (State Unit) Unit)) + (by nk_auto : SInvU.NK N (handleReservedIrq (Z := Unit) i)) + (by nk_auto : SInvU.NK N (doMachineOp (Z := Unit) (ackInterrupt i)))) + +theorem idk_preemptionPathVF {K : ObjRef} (onCore remoteCur : ObjRef → Bool) : + SInvG.IdK K (preemptionPathVF (Z := Unit) onCore remoteCur) := + ik_preG3 onCore remoteCur (fun _ f => (by idk_auto : SInvG.IdK K (NonDet.gets f))) + (by idk_auto : SInvG.IdK K (checkBudget (Z := Unit))) + (fun c b => (by idk_auto : SInvG.IdK K (chargeBudget (Z := Unit) c b))) + (fun sc => (by idk_auto : SInvG.IdK K (getScActive (Z := Unit) sc))) + (by idk_auto : SInvG.IdK K (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (by idk_auto : SInvG.IdK K (doMachineOp (Z := Unit) (getActiveIRQ true))) + (by idk_auto : SInvG.IdK K (handleSpuriousIrq (Z := Unit))) + (fun i => ik_hiG3 _ i + (by idk_auto : SInvG.IdK K (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (by idk_auto : SInvG.IdK K (getIrqState (Z := Unit) i)) + (by idk_auto : SInvG.IdK K (getIrqSlot (Z := Unit) i)) + (fun sl => (by idk_auto : SInvG.IdK K (getCap (Z := Unit) sl))) + (fun _ _ => idk_sendSignalVFF _ _ _ _) + (by idk_auto : SInvG.IdK K (archMaskIrqSignal (Z := Unit) i)) + (by idk_auto : SInvG.IdK K (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (by idk_auto : SInvG.IdK K (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (by idk_auto : SInvG.IdK K (NonDet.fail : NonDet (State Unit) Unit)) + (by idk_auto : SInvG.IdK K (handleReservedIrq (Z := Unit) i)) + (by idk_auto : SInvG.IdK K (doMachineOp (Z := Unit) (ackInterrupt i)))) + +theorem vg_preemptionPathVF (onCore remoteCur : ObjRef → Bool) : + IK VmFin.VG (preemptionPathVF (Z := Unit) onCore remoteCur) := + VmFin.vg_step (fun _ _ => iw_preemptionPathVF _ _) (fun _ _ => sg_preemptionPathVF _ _) + (fun _ => nk_preemptionPathVF _ _) (fun _ => idk_preemptionPathVF _ _) + +theorem f3_preemptionPathVF (onCore remoteCur : ObjRef → Bool) (σ : State Unit) (h : F3 σ) (hG : VmFin.VG σ) + (y : Unit × State Unit) (hy : y ∈ (preemptionPathVF (Z := Unit) onCore remoteCur σ).1) : F3 y.2 := + ⟨domListOK_of_dlV (dl_preemptionPathVF _ _ σ y hy) h.1, + fpu_oi (fun _ => oif_preemptionPathVF _ _) σ h.2.1 y hy, (vg_preemptionPathVF _ _ σ hG y hy).2.2.2⟩ + +/-- A failed (preempted) SetEPTRoot keeps `VG` (it stops inside the delete). -/ +theorem vg_setEptRootErr (i : SetEptRootInvocation) (σ : State Unit) (hG : VmFin.VG σ) + (y : Except Unit Unit × State Unit) (hy : y ∈ ((invokeSetEptRootF (Z := Unit) i).run σ).1) + (he : y.1 = .error ()) : VmFin.VG y.2 := by + unfold invokeSetEptRootF at hy + obtain ⟨z, hz, ⟨a, -, hy⟩ | ⟨e, -, rfl⟩⟩ := fbx_mem_bindE hy + · obtain ⟨x, -, rfl⟩ := fbx_mem_liftE hy + cases he + · exact VmFin.vg_capDeleteVF (i.target, tcbCnodeIndex 5) σ hG z hz + +/-- The finish facts' spec half at an outcome, with `VG` at a preempted one (what the preemption path reads). -/ +def FR (y : Except Unit Unit × State Unit) : Prop := F3 y.2 ∧ (y.1 = .error () → VmFin.VG y.2) + +theorem fr_ok {w : Unit × State Unit} (h : F3 w.2) : FR (.ok w.1, w.2) := ⟨h, fun e => by cases e⟩ + +/-! ## SetEPTRoot keeps the idle vtable fact -/ + +theorem idleVtS_setEptRoot {N : Irq → ObjRef} (i : SetEptRootInvocation) (σ : State Unit) (hG : VmFin.VG σ) + (y : Except Unit Unit × State Unit) (hy : y ∈ ((invokeSetEptRootF (Z := Unit) i).run σ).1) : + SInvIV.IdleVtS y.2 := by + obtain ⟨hS, hI, hU, hV⟩ := hG + have hsi := SInvIV.si_at hS hI hU hV + have hdel : ∀ z ∈ ((capDeleteVF (Z := Unit) (i.target, tcbCnodeIndex 5)).run σ).1, + SInvIV.IdleVtS z.2 ∧ z.2.idleThread = σ.idleThread := by + intro z hz + have h1 := iw_capDeleteVF (N := σ.interruptIrqNode) (t := σ.idleThread) _ σ hsi z hz + have h2 : z.2.idleThread = σ.idleThread := idk_capDeleteVF (K := σ.idleThread) _ σ rfl z hz + refine ⟨?_, h2⟩ + unfold SInvIV.IdleVtS; rw [h2]; exact h1.2.2 + unfold invokeSetEptRootF at hy + obtain ⟨z, hz, ⟨a, -, hy⟩ | ⟨e, -, rfl⟩⟩ := fbx_mem_bindE hy + swap + · exact (hdel z hz).1 + obtain ⟨h1, h2⟩ := hdel z hz + obtain ⟨x, hx, rfl⟩ := fbx_mem_liftE hy + have ei : x.2.idleThread = z.2.idleThread := by + have hk : SInvG.IdK z.2.idleThread (capInsert (Z := Unit) i.newCap i.srcSlot (i.target, tcbCnodeIndex 5)) := by + idk_auto + exact hk z.2 rfl x hx + have hv := SInvIV.iv_capInsert (t := z.2.idleThread) i.newCap i.srcSlot (i.target, tcbCnodeIndex 5) z.2 + ⟨h1, Or.inl (fun e => by + have h5 : tcbCnodeIndex 5 = tcbCnodeIndex 1 := congrArg Prod.snd e + exact absurd h5 (by decide))⟩ x hx + unfold SInvIV.IdleVtS; rw [ei]; exact hv + +/-! ## The invocation -/ + +/-- The post of the extended invocation at an outcome. -/ +def FP3 {α : Type} (fp : Bool) (y : Except Unit α × State Unit) : Prop := FQ fp y.2 ∧ (y.1 = .error () → VmFin.VG y.2) + +theorem fp3_ok {α : Type} {fp : Bool} {w : α × State Unit} (h : FQ fp w.2) : FP3 fp (.ok w.1, w.2) := + ⟨h, fun e => by cases e⟩ + +/-- SpecInvDom's `syscall_post'` with a post on the outcome. -/ +theorem syscall_postR {α β : Type} {mF : FMonad Unit α} {hF : Fault → SMonad Unit Unit} + {mE : α → SEMonad Unit β} {hE : SyscallError → SMonad Unit Unit} {mFin : β → PMonad Unit Unit} + {σ : State Unit} {I : State Unit → Prop} {Q : Except Unit Unit × State Unit → Prop} + (h1 : ∀ w ∈ ((ExceptT.run mF) σ).1, I w.2) + (h2 : ∀ f s, I s → ∀ v ∈ (hF f s).1, Q (.ok v.1, v.2)) + (h3 : ∀ a s, (.ok a, s) ∈ ((ExceptT.run mF) σ).1 → ∀ w ∈ ((ExceptT.run (mE a)) s).1, I w.2) + (h4 : ∀ e s, I s → ∀ v ∈ (hE e s).1, Q (.ok v.1, v.2)) + (h5 : ∀ a s b s', (.ok a, s) ∈ ((ExceptT.run mF) σ).1 → (.ok b, s') ∈ ((ExceptT.run (mE a)) s).1 → + I s' → ∀ y ∈ ((mFin b).run s').1, Q y) : + ∀ y ∈ ((syscall mF hF mE hE mFin).run σ).1, Q y := by + intro y hy + unfold syscall withoutPreemption at hy + obtain ⟨z, hz, ⟨r, hr, hy⟩ | ⟨e, he, -⟩⟩ := SInvDom.mem_bindE_inv' hy + · obtain ⟨w, hw, rfl⟩ := SInvDom.mem_liftE_inv' hz + simp only [Except.ok.injEq] at hr + subst hr + obtain ⟨r, s⟩ := w + cases r with + | error f => + obtain ⟨v, hv, rfl⟩ := SInvDom.mem_liftE_inv' hy + exact h2 f s (h1 _ hw) v hv + | ok a => + obtain ⟨z, hz, ⟨r, hr, hy⟩ | ⟨e, he, -⟩⟩ := SInvDom.mem_bindE_inv' hy + · obtain ⟨w, hw', rfl⟩ := SInvDom.mem_liftE_inv' hz + simp only [Except.ok.injEq] at hr + subst hr + obtain ⟨r, s'⟩ := w + cases r with + | error e => + obtain ⟨v, hv, rfl⟩ := SInvDom.mem_liftE_inv' hy + exact h4 e s' (h3 a s hw _ hw') v hv + | ok b => exact h5 a s b s' hw hw' (h3 a s hw _ hw') y hy + · obtain ⟨w, -, rfl⟩ := SInvDom.mem_liftE_inv' hz + cases he + · obtain ⟨w, -, rfl⟩ := SInvDom.mem_liftE_inv' hz + cases he + +theorem ik_tailS3 {P : State Unit → Prop} (t : ObjRef) (calling : Bool) (rp : List Data) + (h1 : IK P (getThreadState (Z := Unit) t)) (h2 : IK P (replyFromKernel (Z := Unit) t (0, rp))) + (h3 : IK P (setThreadState (Z := Unit) t .running)) : + IKE P (withoutPreemption (Z := Unit) do + let state ← getThreadState t + match state with + | .restart => do + NonDet.when calling <| + replyFromKernel t (0, rp) + setThreadState t .running + | _ => return ()) := by + refine ike_liftE (SInv.ik_bind h1 fun st => ?_) + split + · refine SInv.ik_bind ?_ fun _ => h3 + unfold NonDet.when + split + · exact h2 + · exact SInv.ik_pure _ + · exact SInv.ik_pure _ + +/-- The invariant the front of the extended invocation keeps. -/ +def FI (N : Irq → ObjRef) (ct : ObjRef) (s : State Unit) : Prop := JF N ct s ∧ VmFin.VG s ∧ DomListOK s + +theorem fq_of_fi {N : Irq → ObjRef} {ct : ObjRef} {fp : Bool} {s : State Unit} (h : FI N ct s) : FQ fp s := + ⟨⟨h.2.2, fpuOwnOK_of_ft h.1.2, h.2.1.2.2.2⟩, fun _ => h.2.1⟩ + +theorem fi_keep {N : Irq → ObjRef} {ct : ObjRef} {ε α : Type} {m : NonDetE (State Unit) ε α} + (hJ : IKE (JF N ct) m) (hd : DLE m) (hu : ∀ v, UKE v m) (hn : ∀ N, SInvU.NKE N m) + (hi : ∀ t, SInvG.IdKE t m) : IKE (FI N ct) m := fun σ h y hy => + ⟨hJ σ h.1 y hy, vg_view (m := ExceptT.run m) hu hn hi σ h.2.1 y hy, domListOK_of_dlV (hd σ y hy) h.2.2⟩ + +set_option maxHeartbeats 8000000 in +/-- **The extended invocation** (module docstring). -/ +theorem fin_hiVF (hnd : numDomains = 1) (N : Irq → ObjRef) (ct : ObjRef) (onCore remoteCur : ObjRef → Bool) + (rep calling blocking don fp : Bool) (cptr : CapRef) : + ∀ σ, FI N ct σ → ∀ y ∈ ((fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep calling blocking don fp cptr).run σ).1, + FP3 fp y := by + intro σ hσ y1 hy1 + rw [fl4HandleInvocationVF_hiG] at hy1 + unfold hiG at hy1 + obtain ⟨z1, hz1, ⟨th, hth, hy1⟩ | ⟨e, he, -⟩⟩ := ft_mem_bindE hy1 + swap + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz1; rw [h] at he; cases he + obtain ⟨w1, hw1, rfl⟩ := ft_mem_liftE hz1 + simp only [Except.ok.injEq] at hth; subst hth + rw [mem_gets hw1] at hy1 + have hc : σ.curThread = ct := hσ.1.1.2.2.1 + simp only [hc, withoutPreemption] at hy1 + obtain ⟨z2, hz2, ⟨info0, hinfo, hy0⟩ | ⟨e, he, -⟩⟩ := ft_mem_bindE hy1 + swap + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz2; rw [h] at he; cases he + obtain ⟨⟨info, σ'⟩, hw2, rfl⟩ := ft_mem_liftE hz2 + simp only [Except.ok.injEq] at hinfo; subst hinfo + have hσ' : FI N ct σ' := by + have hJ : IK (JF N ct) (getMessageInfo (Z := Unit) ct) := by juf_auto + exact ⟨hJ σ hσ.1 _ hw2, vg_view (fun _ => by uk_auto) (fun _ => by nk_auto) (fun _ => by idk_auto) σ hσ.2.1 _ hw2, + domListOK_of_dlV (dl_getMessageInfo _ σ _ hw2) hσ.2.2⟩ + have hF : ∀ v, UKE v (do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot (Z := Unit) ct cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false ct + let extracaps ← lookupExtraCaps ct buffer info + NonDetE.returnOk (slot, cap, extracaps, buffer)) := fun _ => by uk_auto + have hFI : IKE (FI N ct) (do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot (Z := Unit) ct cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false ct + let extracaps ← lookupExtraCaps ct buffer info + NonDetE.returnOk (slot, cap, extracaps, buffer)) := + fi_keep (by juf_auto) (by dl_auto) hF (fun _ => by nk_auto) (fun _ => by idk_auto) + have hOk : OkV (uV σ') (fun r : CslotPtr × Cap × List (Cap × CslotPtr) × Option ObjRef => + (uV σ').2.1 r.1 = some r.2.1 ∧ ∀ p ∈ r.2.2.1, (uV σ').2.1 p.2 = some p.1 ∧ DestRefV (uV σ') p.2) (do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot (Z := Unit) ct cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false ct + let extracaps ← lookupExtraCaps ct buffer info + NonDetE.returnOk (slot, cap, extracaps, buffer)) := by + refine okv_bind (fun _ => uk_capFaultOnFailure _ _ _ (uk_lookupCapAndSlot _ _)) + (okv_capFaultOnFailure _ _ (okv_lookupCapAndSlot _ _)) fun x hx => ?_ + obtain ⟨cap, slot⟩ := x + dsimp only + refine okv_bind_uk (fun _ => ike_liftE (uk_lookupIpcBuffer _ _)) fun buf => ?_ + refine okv_bind (fun _ => uk_lookupExtraCaps _ _ _) (okv_lookupExtraCapsN _ _ _) fun ex hex => okv_returnOk ?_ + exact ⟨hx, hex⟩ + refine syscall_postR (I := FI N ct) (Q := FP3 fp) (fun w hw => hFI σ' hσ' w hw) ?_ ?_ ?_ ?_ y1 hy0 + · intro f s hs v hv + refine fp3_ok ?_ + unfold NonDet.when at hv + split at hv + · refine ⟨⟨domListOK_of_dlV (dl_handleFault ct f s v hv) hs.2.2, + fpu_oi (fun _ => oi_handleFault _ _) s (fpuOwnOK_of_ft hs.1.2) v hv, ?_⟩, fun _ => ?_⟩ + all_goals + have hg : VmFin.VG v.2 := VmFin.vg_step (fun _ _ => SInvIV.iw_handleFault _ _) + (fun _ _ => SInvG.sg_handleFault _ _) (fun _ => SInvU.nk_handleFault _ _) + (fun _ => SInvG.idk_handleFault _ _) s hs.2.1 v hv + · exact hg.2.2.2 + · exact hg + · rw [Set.mem_singleton_iff.1 hv]; exact fq_of_fi hs + · intro a s has w hw + have hs : FI N ct s := hFI σ' hσ' _ has + obtain ⟨sl, cap, ex, buf⟩ := a + have h : IKE (FI N ct) (do + let args ← NonDetE.liftE <| getMrs ct buf info + fl4DecodeInvocationV (Z := Unit) fp info.miLabel args cptr sl cap ex buf) := + ike_bind (fi_keep (by juf_auto) (by dl_auto) (fun _ => by uk_auto) (fun _ => by nk_auto) (fun _ => by idk_auto)) + fun args => ike_decodeV _ _ _ _ _ _ _ _ + (fi_keep (by juf_auto) (dle_fl4Decode _ _ _ _ _ _ _ _) (fun _ => by uk_auto) (fun _ => by nk_auto) + (fun _ => by idk_auto)) + exact h s hs w hw + · intro e s hs v hv + refine fp3_ok ?_ + have h : IK (FQ fp) (NonDet.when calling (fl4ReplyFromKernelError (Z := Unit) rep ct (msgFromSyscallError e))) := by + unfold NonDet.when + split + · exact fq_keep (dl_fl4ReplyFromKernelError _ _ _) (fun _ => oi_fl4ReplyFromKernelError _ _ _) + (fun _ => uk_fl4ReplyFromKernelError _ _ _) (fun _ => nk_fl4ReplyFromKernelError _ _ _) + (fun _ => SInvG.idk_fl4ReplyFromKernelError _ _ _) + · exact SInv.ik_pure _ + exact h s (fq_of_fi hs) v hv + · intro a s b s' has hbs hI y hy + obtain ⟨sl, cap, ex, buf⟩ := a + have hvs : uV s = uV σ' := hF _ σ' rfl _ has + obtain ⟨hcap, hex⟩ := hOk σ' rfl _ s has + obtain ⟨z0, hz0, ⟨args, ha0, hb0⟩ | ⟨e, -, he⟩⟩ := mem_bindE' hbs + swap + · simp at he + obtain ⟨x0, hx0, e0⟩ := mem_liftE' hz0 + subst e0 + simp only [Except.ok.injEq] at ha0 + subst ha0 + have hvx : uV x0.2 = uV σ' := (uk_getMrs (uvw := uV s) _ _ _ s rfl x0 hx0).trans hvs + have hv' : uV s' = uV σ' := by + have hd : IKE (fun t => uV t = uV σ') (fl4DecodeInvocationV (Z := Unit) fp info.miLabel x0.1 cptr sl cap ex buf) := + ike_decodeV _ _ _ _ _ _ _ _ (uk_fl4DecodeInvocation _ _ _ _ _ _ _ _) + exact hd _ hvx _ hb0 + have hbase : ∀ b0, b = .base b0 → (∀ t ∈ invRT b0, HasTC s' t) ∧ (fp = true → NoDelI b0) ∧ + InvOKN (uV σ') b0 ∧ DecodedFrom (uV σ') b0 ∧ DomInvOK s' b0 := by + intro b0 e0 + subst e0 + have hn := VmFin.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ _ _ _ hb0 + have hdec : InvOKN (uV σ') b0 := + okv_fl4DecodeN fp info.miLabel x0.1 cptr sl cap ex buf hcap hex x0.2 hvx b0 s' hn + have htg : TgI cap b0 := okv_tgI fp info.miLabel x0.1 cptr sl cap ex buf x0.2 hvx b0 s' hn + exact ⟨hasTC_of_tg hv' hcap htg, + okv_fpI fp info.miLabel x0.1 cptr sl cap ex buf x0.2 hvx b0 s' hn, + hdec, ⟨fp, info.miLabel, x0.1, cptr, sl, cap, ex, buf, x0.2, s', hvx, hcap, hex, hn⟩, + okd_fl4Decode hnd _ _ _ _ _ _ _ _ x0.2 b0 s' hn⟩ + have hdm : VmFin.VmDec cap sl ex b := VmFin.fl4DecodeInvocationV_vmDec _ _ _ _ _ _ _ _ _ _ _ hb0 + have hno : fp = true → ∀ i, b ≠ .setEptRoot i := by + intro e; subst e + exact okr_noSetEpt _ _ _ _ _ _ _ _ _ _ hb0 + obtain ⟨z, hz, ⟨u, hu, hy⟩ | ⟨e, hee, -⟩⟩ := ft_mem_bindE hy + · obtain ⟨w, hw, rfl⟩ := ft_mem_liftE hz + have hsts : FI N ct w.2 := ⟨juf_sts hI.1 hw, + vg_view (fun _ => uk_setThreadState _ _) (fun _ => by nk_auto) (fun _ => by idk_auto) s' hI.2.1 w hw, + domListOK_of_dlV (dl_setThreadState _ _ s' w hw) hI.2.2⟩ + have hv1 : uV w.2 = uV σ' := (uk_setThreadState (v := uV s') ct .restart s' rfl w hw).trans hv' + have hdd : ∀ b0, b = .base b0 → DomInvOK w.2 b0 := fun b0 e0 => + domInvOK_of_dlV (dl_setThreadState _ _ s' w hw) (hbase b0 e0).2.2.2.2 + obtain ⟨r, s1⟩ := w + dsimp only at hsts hv1 hdd + have hcaps : ∀ p ∈ (cap, sl) :: ex, SInvU.capAt s1 p.2 = some p.1 := by + intro p hp + show (uV s1).2.1 p.2 = some p.1 + rw [hv1] + rcases List.mem_cons.1 hp with rfl | hp + · exact hcap + · exact (hex p hp).1 + have hT1 : ∀ b0, b = .base b0 → ∀ t ∈ invRT b0, HasTC s1 t := by + intro b0 e0 + have h := (hbase b0 e0).1 + intro t ht + have e1 : uV s1 = uV s' := hv1.trans hv'.symm + subst e0 + have hn := VmFin.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ _ _ _ hb0 + have htg : TgI cap b0 := okv_tgI fp info.miLabel x0.1 cptr sl cap ex buf x0.2 hvx b0 s' hn + exact hasTC_of_tg hv1 hcap htg t ht + have hP : FP N [ct] s1 := + ⟨⟨⟨hsts.1.1.1, nq_weak_nil _ hsts.1.1.2.1⟩, hsts.1.2⟩, hsts.1.1.2.2.2.1⟩ + have hct1 : s1.curThread ∈ [ct] := by rw [hsts.1.1.2.2.1]; simp + have hperf : ∀ z2 ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur blocking calling don b).run s1).1, + FP3 fp z2 := by + intro z2 hz2 + have hD := dpe_performVF onCore remoteCur blocking calling don b s1 hsts.2.2 hdd z2 hz2 + have hFo := (fo_fl4PerformInvocationVF N onCore remoteCur blocking calling don fp b s1 [ct] hP hct1 + (fun b0 e0 => ⟨hT1 b0 e0, (hbase b0 e0).2.1, by rw [hv1]; exact (hbase b0 e0).2.2.1, + by rw [hv1]; exact (hbase b0 e0).2.2.2.1⟩) + (fun i e0 => by subst e0; exact VmFin.nt_eptInvSlot hdm hcaps) + (fun i e0 => by + subst e0 + obtain ⟨v, -, hd⟩ := hdm + exact VmFin.nt_vcpuInv hd hcaps) + hno z2 hz2).1 + have hG : (∀ i, b ≠ .setEptRoot i) → VmFin.VG z2.2 := fun h => + vg_fl4PerformInvocationVF onCore remoteCur blocking calling don b s1 hsts.2.1 + (fun b0 e0 => ⟨by rw [hv1]; exact (hbase b0 e0).2.2.1, by rw [hv1]; exact (hbase b0 e0).2.2.2.1⟩) + (fun i e0 => by subst e0; exact ⟨cap, sl, ex, hdm, hcaps⟩) + (fun i e0 => by + subst e0 + obtain ⟨v, -, hd⟩ := hdm + exact ⟨v, cap, sl, ex, hd, hcaps⟩) + (fun i e0 => absurd e0 (h i)) z2 hz2 + have hV : SInvIV.IdleVtS z2.2 := by + by_cases he : ∃ i, b = .setEptRoot i + · obtain ⟨i, rfl⟩ := he + simp only [fl4PerformInvocationVF] at hz2 + obtain ⟨q, hq, eq⟩ := mem_ret_bindE hz2 + rw [eq] + exact idleVtS_setEptRoot (N := N) i s1 hsts.2.1 q hq + · exact (hG fun i e => he ⟨i, e⟩).2.2.2 + refine ⟨⟨⟨hD, fpuOwnOK_of_ft hFo.2, hV⟩, fun e => hG (hno e)⟩, fun hE => ?_⟩ + by_cases he : ∃ i, b = .setEptRoot i + · obtain ⟨i, rfl⟩ := he + simp only [fl4PerformInvocationVF] at hz2 + obtain ⟨q, hq, ⟨u, hu, hr⟩ | ⟨e, he', rfl⟩⟩ := ft_mem_bindE hz2 + · rw [Set.mem_singleton_iff.1 hr] at hE; cases hE + · exact vg_setEptRootErr i s1 hsts.2.1 q hq he' + · exact hG fun i e => he ⟨i, e⟩ + obtain ⟨z2, hz2, ⟨rp, hr, hy⟩ | ⟨e, he2, rfl⟩⟩ := ft_mem_bindE hy + · have hs2 := (hperf z2 hz2).1 + obtain ⟨r2, s2⟩ := z2 + simp only at hr; subst hr + obtain ⟨q, -, eq⟩ := ft_mem_liftE hy + refine ⟨?_, fun h => by rw [eq] at h; cases h⟩ + exact ik_tailS3 (P := FQ fp) ct calling rp + (fq_keep (dl_getThreadState _) (fun _ => oi_getThreadState _) (fun _ => uk_getThreadState _) + (fun _ => nk_getThreadState _) (fun _ => SInvG.idk_getThreadState _)) + (fq_keep (dl_replyFromKernel _ _) (fun _ => oi_replyFromKernel _ _) (fun _ => uk_replyFromKernel _ _) + (fun _ => nk_replyFromKernel _ _) (fun _ => SInvG.idk_replyFromKernel _ _)) + (fq_keep (dl_setThreadState _ _) (fun _ => oi_setThreadState _ _) (fun _ => uk_setThreadState _ _) + (fun _ => by nk_auto) (fun _ => by idk_auto)) s2 hs2 y hy + · have h := hperf z2 hz2 + exact ⟨h.1, fun _ => h.2 (by rw [he2])⟩ + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz + rw [h] at hee; cases hee + +/-! ## The syscall arms and the front -/ + +theorem f3_recv {σ : State Unit} (h : DomListOK σ ∧ FpuOwnOK σ ∧ VmFin.VG σ) (b c : Bool) + (z : Unit × State Unit) (hz : z ∈ (handleRecv (Z := Unit) b c σ).1) : F3 z.2 := by + obtain ⟨hD, hF, -, hV⟩ := recvFinFrames b c hz h.1 h.2.1 h.2.2 + exact ⟨hD, hF, hV⟩ + +theorem f3_recvW {P : State Unit → Prop} (hP : ∀ σ, P σ → DomListOK σ ∧ FpuOwnOK σ ∧ VmFin.VG σ) (b c : Bool) + (σ : State Unit) (hσ : P σ) (y : Except Unit Unit × State Unit) + (hy : y ∈ ((withoutPreemption (Z := Unit) (handleRecv b c)).run σ).1) : FR y := by + obtain ⟨w, hw, rfl⟩ := ft_mem_liftE hy + exact fr_ok (f3_recv (hP σ hσ) b c w hw) + +theorem fi_recvPre {N : Irq → ObjRef} {ct : ObjRef} {σ : State Unit} (h : FI N ct σ) : + DomListOK σ ∧ FpuOwnOK σ ∧ VmFin.VG σ := ⟨h.2.2, fpuOwnOK_of_ft h.1.2, h.2.1⟩ + +theorem fi_capReg {N : Irq → ObjRef} {ct : ObjRef} (r : Register) : + IKE (FI N ct) (NonDetE.liftE (ε := Unit) (getCapReg (Z := Unit) r)) := + fi_keep (by juf_auto) (by dl_auto) (fun _ => by uk_auto) (fun _ => by nk_auto) (fun _ => by idk_auto) + +set_option maxHeartbeats 8000000 in +theorem f3_syscallVF (hnd : numDomains = 1) (N : Irq → ObjRef) (ct : ObjRef) (onCore remoteCur : ObjRef → Bool) + (rep : Bool) (c : Syscall) (σ : State Unit) (hσ : FI N ct σ) (y : Except Unit Unit × State Unit) + (hy : y ∈ ((fl4HandleSyscallVF (Z := Unit) onCore remoteCur rep c).run σ).1) : FR y := by + have hCap : ∀ (r : Register) (z : Except Unit CapRef × State Unit), + z ∈ ((NonDetE.liftE (ε := Unit) (getCapReg (Z := Unit) r)).run σ).1 → FI N ct z.2 := + fun r z hz => fi_capReg r σ hσ z hz + have hInv : ∀ (cl bl dn : Bool) (r : Register), ∀ y ∈ ((do + let cptr ← NonDetE.liftE <| getCapReg (Z := Unit) r + fl4HandleInvocationVF onCore remoteCur rep cl bl dn false cptr : PMonad Unit Unit).run σ).1, FR y := by + intro cl bl dn r y hy + obtain ⟨z, hz, ⟨a, -, hy⟩ | ⟨e, he, -⟩⟩ := ft_mem_bindE hy + · have h := fin_hiVF hnd N ct onCore remoteCur rep cl bl dn false _ _ (hCap r z hz) y hy + exact ⟨h.1.1, h.2⟩ + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz; rw [h] at he; cases he + have hSR : ∀ (r : Register) (cl bl dn b c : Bool), ∀ y ∈ ((do + let cptr ← withoutPreemption (Z := Unit) (getCapReg r) + fl4HandleInvocationVF onCore remoteCur rep cl bl dn true cptr + withoutPreemption (handleRecv b c) : PMonad Unit Unit).run σ).1, FR y := by + intro r cl bl dn b c y hy + obtain ⟨z, hz, ⟨a, -, hy⟩ | ⟨e, he, -⟩⟩ := ft_mem_bindE hy + swap + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz; rw [h] at he; cases he + have h1 : FI N ct z.2 := hCap r z hz + obtain ⟨z2, hz2, ⟨a2, -, hy⟩ | ⟨e, he, rfl⟩⟩ := ft_mem_bindE hy + · have h2 := (fin_hiVF hnd N ct onCore remoteCur rep cl bl dn true _ _ h1 z2 hz2).1 + exact f3_recvW (P := fun s => FQ true s) (fun s h => ⟨h.1.1, h.1.2.1, h.2 rfl⟩) b c _ h2 y hy + · have h2 := fin_hiVF hnd N ct onCore remoteCur rep cl bl dn true _ _ h1 z2 hz2 + exact ⟨h2.1.1, fun _ => h2.2 (by rw [he])⟩ + cases c + case sysSend => simp only [fl4HandleSyscallVF] at hy; exact hInv _ _ _ _ y hy + case sysNBSend => simp only [fl4HandleSyscallVF] at hy; exact hInv _ _ _ _ y hy + case sysCall => simp only [fl4HandleSyscallVF] at hy; exact hInv _ _ _ _ y hy + case sysReplyRecv => simp only [fl4HandleSyscallVF] at hy; exact hSR _ _ _ _ _ _ y hy + case sysNBSendRecv => simp only [fl4HandleSyscallVF] at hy; exact hSR _ _ _ _ _ _ y hy + case sysNBSendWait => simp only [fl4HandleSyscallVF] at hy; exact hSR _ _ _ _ _ _ y hy + case sysYield => + simp only [fl4HandleSyscallVF] at hy + obtain ⟨w, hw, rfl⟩ := ft_mem_liftE hy + have h := fi_recvPre hσ + obtain ⟨hD, hF, -, hV, -⟩ := yieldFinFrames hw h.1 h.2.1 h.2.2 + exact fr_ok ⟨hD, hF, hV⟩ + all_goals + simp only [fl4HandleSyscallVF] at hy + exact f3_recvW (fun _ h => fi_recvPre h) _ _ σ hσ y hy + +theorem f3_eventVF (hnd : numDomains = 1) (N : Irq → ObjRef) (ct : ObjRef) (onCore remoteCur : ObjRef → Bool) + (rep : Bool) (c : Syscall) (σ : State Unit) (hσ : FI N ct σ) (y : Except Unit Unit × State Unit) + (hy : y ∈ ((fl4HandleEventVF (Z := Unit) onCore remoteCur rep (.syscallEvent c)).run σ).1) : FR y := by + simp only [fl4HandleEventVF] at hy + obtain ⟨z, hz, ⟨a, -, hy⟩ | ⟨e, he, -⟩⟩ := ft_mem_bindE hy + swap + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz; rw [h] at he; cases he + have h1 : FI N ct z.2 := + fi_keep (N := N) (ct := ct) (m := NonDetE.liftE (ε := Unit) (updateTimeStamp (Z := Unit))) (by juf_auto) + (by dl_auto) (fun _ => by uk_auto) (fun _ => by nk_auto) (fun _ => by idk_auto) σ hσ z hz + obtain ⟨z2, hz2, ⟨r, hr, hy⟩ | ⟨e, he, -⟩⟩ := ft_mem_bindE hy + swap + · obtain ⟨w, -, h⟩ := ft_mem_liftE hz2; rw [h] at he; cases he + obtain ⟨w, hw, rfl⟩ := ft_mem_liftE hz2 + simp only [Except.ok.injEq] at hr + subst hr + unfold NonDetE.whenE at hy + split at hy + · rename_i hr + rw [SInv.checkBudgetRestart_true hw hr] at hy + exact f3_syscallVF hnd N ct onCore remoteCur rep c _ h1 y hy + · rw [Set.mem_singleton_iff.1 hy] + have hg : VmFin.VG w.2 := VmFin.vg_step (fun _ _ => SInvIV.iw_checkBudgetRestart) + (fun _ _ => SInvG.sg_checkBudgetRestart) (fun _ => SInvU.nk_checkBudgetRestart) + (fun _ => SInvG.idk_checkBudgetRestart) _ h1.2.1 w hw + exact ⟨⟨domListOK_of_dlV (dl_checkBudgetRestart _ w hw) h1.2.2, + fpu_oi (fun _ => SInvF.oi_checkBudgetRestart) _ (fpuOwnOK_of_ft h1.1.2) w hw, hg.2.2.2⟩, fun e => by cases e⟩ + +theorem rvmOi_preemptionPathVF {N : SInvAll.OIx} (onCore remoteCur : ObjRef → Bool) : + SInvAll.OIK N (preemptionPathVF (Z := Unit) onCore remoteCur) := + ik_preG3 onCore remoteCur (fun _ f => (by orv_auto : SInvAll.OIK N (NonDet.gets f))) + (by orv_auto : SInvAll.OIK N (checkBudget (Z := Unit))) + (fun c b => (by orv_auto : SInvAll.OIK N (chargeBudget (Z := Unit) c b))) + (fun sc => (by orv_auto : SInvAll.OIK N (getScActive (Z := Unit) sc))) + (by orv_auto : SInvAll.OIK N (NonDet.modify fun s : State Unit => { s with consumedTime := 0 })) + (by orv_auto : SInvAll.OIK N (doMachineOp (Z := Unit) (getActiveIRQ true))) + (by orv_auto : SInvAll.OIK N (handleSpuriousIrq (Z := Unit))) + (fun i => ik_hiG3 _ i + (by orv_auto : SInvAll.OIK N (doMachineOp (Z := Unit) (do maskInterrupt true i; ackInterrupt i))) + (by orv_auto : SInvAll.OIK N (getIrqState (Z := Unit) i)) + (by orv_auto : SInvAll.OIK N (getIrqSlot (Z := Unit) i)) + (fun sl => (by orv_auto : SInvAll.OIK N (getCap (Z := Unit) sl))) + (fun _ _ => SInvAll.oi_sendSignalVF _ _ _ _) + (by orv_auto : SInvAll.OIK N (archMaskIrqSignal (Z := Unit) i)) + (by orv_auto : SInvAll.OIK N (doMachineOp (Z := Unit) ackDeadlineIRQ)) + (by orv_auto : SInvAll.OIK N (NonDet.modify fun s : State Unit => { s with reprogramTimer := true })) + (by orv_auto : SInvAll.OIK N (NonDet.fail : NonDet (State Unit) Unit)) + (by orv_auto : SInvAll.OIK N (handleReservedIrq (Z := Unit) i)) + (by orv_auto : SInvAll.OIK N (doMachineOp (Z := Unit) (ackInterrupt i)))) + +theorem rvmOi_syscallVF {N : SInvAll.OIx} (onCore remoteCur : ObjRef → Bool) (rep : Bool) (c : Syscall) : + SInvAll.OIKE N (fl4HandleSyscallVF (Z := Unit) onCore remoteCur rep c) := by + have hI := fun cl bl dn fp cptr => SInvAll.oi_fl4HandleInvocationVF (N := N) onCore remoteCur rep cl bl dn fp cptr + have hC : ∀ r, SInvAll.OIKE N (NonDetE.liftE (ε := Unit) (getCapReg (Z := Unit) r)) := + fun r => SInv.ike_liftE (SInvAll.oi_getCapReg r) + have hR : ∀ b c, SInvAll.OIKE N (withoutPreemption (Z := Unit) (handleRecv b c)) := + fun b c => SInv.ike_liftE (SInvAll.oi_handleRecv b c) + have hSR : ∀ r cl bl dn b c, SInvAll.OIKE N (do + let cptr ← withoutPreemption (Z := Unit) (getCapReg r) + fl4HandleInvocationVF onCore remoteCur rep cl bl dn true cptr + withoutPreemption (handleRecv b c)) := fun r cl bl dn b c => + SInv.ike_bind (hC r) fun _ => SInv.ike_bind (hI _ _ _ _ _) fun _ => hR b c + cases c <;> simp only [fl4HandleSyscallVF] + case sysYield => exact SInv.ike_liftE SInvAll.oi_handleYield + case sysSend => exact SInv.ike_bind (hC _) fun _ => hI _ _ _ _ _ + case sysNBSend => exact SInv.ike_bind (hC _) fun _ => hI _ _ _ _ _ + case sysCall => exact SInv.ike_bind (hC _) fun _ => hI _ _ _ _ _ + case sysReplyRecv => exact hSR _ _ _ _ _ _ + case sysNBSendRecv => exact hSR _ _ _ _ _ _ + case sysNBSendWait => exact hSR _ _ _ _ _ _ + all_goals exact hR _ _ + +theorem rvmOi_eventVF {N : SInvAll.OIx} (onCore remoteCur : ObjRef → Bool) (rep : Bool) (c : Syscall) : + SInvAll.OIKE N (fl4HandleEventVF (Z := Unit) onCore remoteCur rep (.syscallEvent c)) := by + simp only [fl4HandleEventVF] + refine SInv.ike_bind (SInv.ike_liftE SInvAll.oi_updateTimeStamp) fun _ => + SInv.ike_bind (SInv.ike_liftE SInvAll.oi_checkBudgetRestart) fun r => ?_ + unfold NonDetE.whenE + split + · exact rvmOi_syscallVF _ _ _ _ + · first | exact SInv.ike_returnOk _ | exact SInv.ike_pure _ + +/-- **The finish facts at every outcome of the extended syscall front** (module docstring). The entry facts are the +native carried ones and `IdleVtS`. -/ +theorem specFrontVF_f3 (hnd : numDomains = 1) (onCore remoteCur : ObjRef → Bool) (c : Syscall) {σ : State Unit} + (ha : ∃ x, σ.kheap σ.curThread = some (.tcb x) ∧ activeSt x.tcbState) + (hN : NtfnQB σ ∧ EpQB σ) (hU : UtInv3 σ) (hL : IfLiveT σ) (hF : FT σ) + (hS : SInvG.SpecGlobS σ) (hI : SInvG.IdleOutV σ) (hV : SInvIV.IdleVtS σ) (hD : DomListOK σ) : + ∀ y ∈ (specFrontVF onCore remoteCur c σ).1, F3 y.2 := by + obtain ⟨x0, hx0, ha0⟩ := ha + intro y ⟨y2, h2, hy⟩ + have hNQ : NQ [σ.curThread] σ := nq_cons ((nq_nil_iff σ).2 hN) fun x hx => by + rw [hx0] at hx; cases hx; exact ok_of_active ha0 + have hJ : JF σ.interruptIrqNode σ.curThread σ := + ⟨⟨il_consT ((il_nil_iff σ).2 hL) (hL _ x0 hx0 (live_of_active ha0)), hNQ, rfl, hU, rfl⟩, hF⟩ + have h0 : FI σ.interruptIrqNode σ.curThread σ := ⟨hJ, ⟨hS, hI, hU, hV⟩, hD⟩ + have h1 : FI σ.interruptIrqNode σ.curThread y2.2 := by + have hJF : IK (JF σ.interruptIrqNode σ.curThread) (fl4SyscallFaultIP (Z := Unit) σ.curThread) := + jufK_of (ju_ofK (SInv.il_fl4SyscallFaultIP _) (SInv.nq_fl4SyscallFaultIP _) (SInv.ct_fl4SyscallFaultIP _) + (fun _ => SInvU.uk_fl4SyscallFaultIP _) (SInvU.nk_fl4SyscallFaultIP _)) + (fun _ => SInv.il_fl4SyscallFaultIP _) (fun _ => SInvF.oi_fl4SyscallFaultIP _) + exact ⟨hJF σ hJ y2 h2, + vg_view (fun _ => SInvU.uk_fl4SyscallFaultIP _) (fun _ => SInvU.nk_fl4SyscallFaultIP _) + (fun _ => SInvG.idk_fl4SyscallFaultIP _) σ h0.2.1 y2 h2, + domListOK_of_dlV (dl_fl4SyscallFaultIP _ σ y2 h2) hD⟩ + unfold NonDetE.handleE at hy + obtain ⟨z, hz, hy⟩ := hy + have hz' : FR z := f3_eventVF hnd _ _ onCore remoteCur true c _ h1 z hz + obtain ⟨r, s⟩ := z + cases r with + | ok a => rw [Set.mem_singleton_iff.1 hy]; exact hz'.1 + | error e => + obtain ⟨w, hw, rfl⟩ := ft_mem_liftE (m := preemptionPathVF (Z := Unit) onCore remoteCur) hy + exact f3_preemptionPathVF _ _ s hz'.1 (hz'.2 rfl) w hw + +/-- **No thread becomes `runningVM` in the extended syscall front.** -/ +theorem specFrontVF_rvm (onCore remoteCur : ObjRef → Bool) (c : Syscall) (σ : State Unit) : + ∀ y ∈ (specFrontVF onCore remoteCur c σ).1, RvmSub σ y.2 := by + have h : ∀ N : SInvAll.OIx, SInvAll.OIK N (fl4SyscallFaultIP (Z := Unit) σ.curThread >>= fun _ => + ExceptT.run (NonDetE.handleE (fl4HandleEventVF (Z := Unit) onCore remoteCur true (.syscallEvent c)) + (fun _ => withoutPreemption (preemptionPathVF onCore remoteCur)))) := by + intro N + refine SInv.ik_bind (SInvAll.oi_fl4SyscallFaultIP _) fun _ => ?_ + intro s hs y hy + unfold NonDetE.handleE at hy + obtain ⟨z, hz, hy⟩ := hy + have hz' := rvmOi_eventVF onCore remoteCur true c s hs z hz + obtain ⟨r, s1⟩ := z + cases r with + | ok a => rw [Set.mem_singleton_iff.1 hy]; exact hz' + | error e => + obtain ⟨w, hw, rfl⟩ := ft_mem_liftE (m := preemptionPathVF (Z := Unit) onCore remoteCur) hy + exact rvmOi_preemptionPathVF _ _ s1 hz' w hw + intro y hy + exact h ⟨.runningVM, fun o => ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM, Or.inr rfl⟩ σ + (fun _ x hp hs => ⟨x, hp, hs⟩) y hy + +end + +end Fl4Proofs.P3.FinFront diff --git a/proofs/Fl4Proofs/P3/VmFinGenB.lean b/proofs/Fl4Proofs/P3/VmFinGenB.lean new file mode 100644 index 000000000..aa4ac6bc6 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenB.lean @@ -0,0 +1,54 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvFaultTcbsH7's deletion-loop lemmas (`b_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvFaultTcbsH7 +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec +noncomputable section +set_option maxHeartbeats 1000000 +variable {L E K : List ObjRef} {C : ObjRef → Prop} + +theorem b_recDelFG {Fn : FinFn} (hFn : ∀ c b, BK L E K C (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, BK L E K C (f k)) (k : RecDelCall) : BKE L E K C (recDelFG Fn f k) := by + have hrec' : ∀ k, BKE L E K C (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; b_autoE; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; b_autoE; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> b_autoE + all_goals first | exact hrec' _ | apply hFn + +theorem b_recDelG {Fn : FinFn} (hFn : ∀ c b, BK L E K C (Fn c b)) + (c : RecDelCall) : BKE L E K C (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => b_recDelFG hFn hf k) c + +theorem b_capDeleteG {Fn : FinFn} (hFn : ∀ c b, BK L E K C (Fn c b)) + (slot : CslotPtr) : BKE L E K C (capDeleteG Fn slot) := by + unfold capDeleteG; b_autoE + all_goals first | exact b_recDelG hFn _ | apply b_recDelG hFn + +theorem b_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, BK L E K C (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, BK L E K C (f k)) (sl : CslotPtr) : BKE L E K C (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, BKE L E K C (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + b_autoE + all_goals first | exact hrec' _ | exact b_capDeleteG hFn _ | apply b_capDeleteG hFn + +theorem b_capRevokeG {Fn : FinFn} (hFn : ∀ c b, BK L E K C (Fn c b)) + (sl : CslotPtr) : BKE L E K C (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => b_capRevokeFG hFn hf k) sl + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinGenBase.lean b/proofs/Fl4Proofs/P3/VmFinGenBase.lean new file mode 100644 index 000000000..bd617bf40 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenBase.lean @@ -0,0 +1,35 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): what the long `rec_del` step lemmas read from the finaliser's outcomes +(`FinFacts`: the finalised slot keeps its cap, the remainder covers no more, and `fin_step`'s untyped facts), proved +for `finaliseCap` (the `finaliseCapVF` instance is in P3/VmFinGenVF). Native imports only: the VM cone redefines +`ik_step0`, which the generalised proofs quote. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtO1 +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 1000000 + +structure FinFacts (Fn : FinFn) : Prop where + keep : ∀ {σ : State Unit} {s : CslotPtr} {c : Cap}, SInvU.capAt σ s = some c → ∀ (b : Bool), + ∀ y ∈ (Fn c b σ).1, SInvU.capAt y.2 s = some c + cov : ∀ (c : Cap) (b : Bool), OV (fun p : Cap × Cap => covR p.1 ⊆ covR c) (Fn c b) + step : ∀ {σ : State Unit}, UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + cap ≠ .nullCap → ∀ {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + UtInv'' y.2 ∧ SInvU.capAt y.2 s = some cap ∧ (capRemoveable y.1.1 s → SafeDelV (uV y.2) s cap) ∧ + (¬ capRemoveable y.1.1 s → UtInv''V (vCap s y.1.1 (uV y.2))) + +theorem finFacts_native : FinFacts (finaliseCap (Z := Unit)) where + keep h b := finaliseCap_keepAt h b + cov := finaliseCap_cov + step hI _ _ hc hnn _ hy := SInvU.fin_step hI hc hnn hy + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinGenCt.lean b/proofs/Fl4Proofs/P3/VmFinGenCt.lean new file mode 100644 index 000000000..581a19de0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenCt.lean @@ -0,0 +1,55 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvNtfnQC2's deletion-loop lemmas (`ct_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvNtfnQC2 +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 +variable {cq : ObjRef} + +theorem ct_recDelFG {Fn : FinFn} (hFn : ∀ c b, CTK cq (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, CTK cq (f k)) (k : RecDelCall) : CTKE cq (recDelFG Fn f k) := by + have hrec' : ∀ k, CTKE cq (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; ct_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; ct_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> ct_auto + all_goals first | exact hrec' _ | apply hFn + +theorem ct_recDelG {Fn : FinFn} (hFn : ∀ c b, CTK cq (Fn c b)) + (c : RecDelCall) : CTKE cq (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => ct_recDelFG hFn hf k) c + +theorem ct_capDeleteG {Fn : FinFn} (hFn : ∀ c b, CTK cq (Fn c b)) + (slot : CslotPtr) : CTKE cq (capDeleteG Fn slot) := by + unfold capDeleteG; ct_auto + all_goals first | exact ct_recDelG hFn _ | apply ct_recDelG hFn + +theorem ct_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, CTK cq (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, CTK cq (f k)) (sl : CslotPtr) : CTKE cq (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, CTKE cq (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + ct_auto + all_goals first | exact hrec' _ | exact ct_capDeleteG hFn _ | apply ct_capDeleteG hFn + +theorem ct_capRevokeG {Fn : FinFn} (hFn : ∀ c b, CTK cq (Fn c b)) + (sl : CslotPtr) : CTKE cq (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => ct_capRevokeFG hFn hf k) sl + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinGenDl.lean b/proofs/Fl4Proofs/P3/VmFinGenDl.lean new file mode 100644 index 000000000..556c79506 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenDl.lean @@ -0,0 +1,61 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvDomG's deletion-loop lemmas (`dl_recDelF`, `dle_recDel`, +`dl_capRevokeF`, `dle_capRevoke`, `dl_capDelete`) over a finaliser `Fn` (`recDelFG` / `capDeleteG` / `capRevokeG`): +the domain list is kept. The proofs are the native ones with the finaliser's frame as a hypothesis. No named premise. +-/ +import Fl4Proofs.P3.SpecInvDomG +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInvDom +open Fl4Spec + +noncomputable section + +set_option maxHeartbeats 4000000 + +theorem dl_recDelFG {Fn : FinFn} (hFn : ∀ c b, DL (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, DL (f k)) (k : RecDelCall) : DLE (recDelFG Fn f k) := by + have hrec' : ∀ k, DLE (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; dl_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; dl_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> dl_auto + all_goals first | exact hrec' _ | apply hFn + +theorem dle_recDelG {Fn : FinFn} (hFn : ∀ c b, DL (Fn c b)) (c : RecDelCall) : DLE (recDelG Fn c) := by + intro σ x hx + let f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap)) := fun _ t => + ({y | dlV y.2 = dlV t}, True) + have hf : ∀ k t, (fun r c => recDelFG Fn r c) f k t ≤ f k t := fun k t => + ⟨fun y hy => dl_recDelFG hFn (f := f) (fun _ _ _ hz => hz) k t y hy, fun _ => trivial⟩ + exact (Fl4Proofs.P3.SInvDomL.lfp_le_prefixed' _ f hf c σ).1 hx + +theorem dl_capDeleteG {Fn : FinFn} (hFn : ∀ c b, DL (Fn c b)) (sl : CslotPtr) : DLE (capDeleteG Fn sl) := by + unfold capDeleteG; dl_auto0 + all_goals first | exact dle_recDelG hFn _ | apply dle_recDelG hFn + +theorem dl_capRevokeFG {Dl : CslotPtr → PMonad Unit Unit} (hDl : ∀ sl, DLE (Dl sl)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, DL (f k)) (sl : CslotPtr) : DLE (capRevokeFG Dl f sl) := by + have hrec' : ∀ k, DLE (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + dl_auto + all_goals first | exact hrec' _ | exact hDl _ | apply hDl + +theorem dle_capRevokeG {Dl : CslotPtr → PMonad Unit Unit} (hDl : ∀ sl, DLE (Dl sl)) (sl : CslotPtr) : + DLE (capRevokeG Dl sl) := by + intro σ x hx + let f : CslotPtr → NonDet (State Unit) (Except Unit Unit) := fun _ t => + ({y | dlV y.2 = dlV t}, True) + have hf : ∀ k t, (fun r c => capRevokeFG Dl r c) f k t ≤ f k t := fun k t => + ⟨fun y hy => dl_capRevokeFG hDl (f := f) (fun _ _ _ hz => hz) k t y hy, fun _ => trivial⟩ + exact (Fl4Proofs.P3.SInvDomL.lfp_le_prefixed' _ f hf sl σ).1 hx + +end + +end Fl4Proofs.P3.SInvDom diff --git a/proofs/Fl4Proofs/P3/VmFinGenHu.lean b/proofs/Fl4Proofs/P3/VmFinGenHu.lean new file mode 100644 index 000000000..84dbd3d37 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenHu.lean @@ -0,0 +1,66 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvUtZX's deletion-loop lemmas (`hu_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtZX +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInvUZ +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvV +noncomputable section +set_option maxHeartbeats 1000000 +variable {U : UtSet} + +set_option maxHeartbeats 4000000 in +theorem hu_recDelFG {Fn : FinFn} (hFn : ∀ c b, IKH (HU U) (Fn c b)) (hRv : ∀ c b, RV (fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, IKH (HU U) (f k)) (k : RecDelCall) : IKHE (ε := Unit) (HU U) (recDelFG Fn f k) := by + have hrec' : ∀ k, IKHE (ε := Unit) (HU U) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; hv_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => + unfold recDelFG withoutPreemption + refine ike_bind (ike_liftE (hv_getCap _)) fun cap => ?_ + split + · exact ike_returnOk _ + refine ike_bind (ike_liftE (by hv_auto)) fun fin => ?_ + refine ike_liftE_bind_rv (hFn cap fin) (hRv cap fin) fun rc hrc => ?_ + obtain ⟨rem, cl⟩ := rc + have hrem : UtIn U rem := utIn_noUt hrc U + dsimp only + hv_auto + all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> hv_auto + all_goals first | exact hrec' _ | apply hFn + +theorem hu_recDelG {Fn : FinFn} (hFn : ∀ c b, IKH (HU U) (Fn c b)) (hRv : ∀ c b, RV (fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) (Fn c b)) + (c : RecDelCall) : IKHE (ε := Unit) (HU U) (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => hu_recDelFG hFn hRv hf k) c + +theorem hu_capDeleteG {Fn : FinFn} (hFn : ∀ c b, IKH (HU U) (Fn c b)) (hRv : ∀ c b, RV (fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) (Fn c b)) + (sl : CslotPtr) : IKHE (ε := Unit) (HU U) (capDeleteG Fn sl) := by + unfold capDeleteG; hv_auto + all_goals first | exact hu_recDelG hFn hRv _ | apply hu_recDelG hFn hRv + +theorem hu_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, IKH (HU U) (Fn c b)) (hRv : ∀ c b, RV (fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IKH (HU U) (f k)) (sl : CslotPtr) : IKHE (ε := Unit) (HU U) (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKHE (ε := Unit) (HU U) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + hv_auto + all_goals first | exact hrec' _ | exact hu_capDeleteG hFn hRv _ | apply hu_capDeleteG hFn hRv + +theorem hu_capRevokeG {Fn : FinFn} (hFn : ∀ c b, IKH (HU U) (Fn c b)) (hRv : ∀ c b, RV (fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) (Fn c b)) + (sl : CslotPtr) : IKHE (ε := Unit) (HU U) (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => hu_capRevokeFG hFn hRv hf k) sl + +end + +end Fl4Proofs.P3.SInvUZ diff --git a/proofs/Fl4Proofs/P3/VmFinGenIdk.lean b/proofs/Fl4Proofs/P3/VmFinGenIdk.lean new file mode 100644 index 000000000..e70bed681 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenIdk.lean @@ -0,0 +1,55 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvSgI3's deletion-loop lemmas (`idk_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvSgI3 +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInvG +open Fl4Spec Fl4Proofs.P3.SInv +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 +variable {N : ObjRef} + +theorem idk_recDelFG {Fn : FinFn} (hFn : ∀ c b, IdK N (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, IdK N (f k)) (k : RecDelCall) : IdKE N (recDelFG Fn f k) := by + have hrec' : ∀ k, IdKE N (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; idk_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; idk_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> idk_auto + all_goals first | exact hrec' _ | apply hFn + +theorem idk_recDelG {Fn : FinFn} (hFn : ∀ c b, IdK N (Fn c b)) + (c : RecDelCall) : IdKE N (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => idk_recDelFG hFn hf k) c + +theorem idk_capDeleteG {Fn : FinFn} (hFn : ∀ c b, IdK N (Fn c b)) + (slot : CslotPtr) : IdKE N (capDeleteG Fn slot) := by + unfold capDeleteG; idk_auto + all_goals first | exact idk_recDelG hFn _ | apply idk_recDelG hFn + +theorem idk_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, IdK N (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IdK N (f k)) (sl : CslotPtr) : IdKE N (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IdKE N (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + idk_auto + all_goals first | exact hrec' _ | exact idk_capDeleteG hFn _ | apply idk_capDeleteG hFn + +theorem idk_capRevokeG {Fn : FinFn} (hFn : ∀ c b, IdK N (Fn c b)) + (sl : CslotPtr) : IdKE N (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => idk_capRevokeFG hFn hf k) sl + +end + +end Fl4Proofs.P3.SInvG diff --git a/proofs/Fl4Proofs/P3/VmFinGenLFo.lean b/proofs/Fl4Proofs/P3/VmFinGenLFo.lean new file mode 100644 index 000000000..9ea30bac5 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenLFo.lean @@ -0,0 +1,308 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvFoD's `fo_step` and its tops over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`), the finaliser's facts as hypotheses. +Written by `handoff/tools/gen_finlong.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvFoD +import Fl4Proofs.P3.VmFinGenBase +import Fl4Proofs.P3.VmFinGenLRd +open Classical + +namespace Fl4Proofs.P3.SInvF +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU +noncomputable section +set_option maxHeartbeats 2000000 +set_option maxErrors 2000 + +open Fl4Proofs.P3.VmFin + +theorem fo_stepG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (FN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → + TcSafe (oL y.2 ++ []) y.2 s y.1.1) + (hTm : ∀ {K : List ObjRef} {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe K y.2 s .nullCap) + (f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) + (hrec : ∀ k, HR (FPre k) (FPost k) (f k)) (k : RecDelCall) : + HR (FPre k) (FPost k) (recDelFG Fn f k : NonDet (State Unit) (Except Unit (Bool × Cap))) := by + cases k with + | cTEDeleteCall s e => + intro σ hσ y hy + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩⟩ := mem_bindE' hy + · have hp := hrec (.finaliseSlotCall s e) σ hσ z hz + obtain ⟨b, info⟩ := a + have hp2 := hp.1.2 b info ha + try dsimp only at hy + obtain ⟨z2, hz2, ⟨u, hu, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz2 + rw [mem_ret_ok hy]; try dsimp only + unfold NonDet.when at hx + split at hx + · rename_i heb + have hbt : b = true := by + cases b + · obtain ⟨he, -⟩ := hp2.2 rfl + subst he + simp at heb + · rfl + subst hbt + have hsafe : ∀ c, SInvU.capAt z.2 s = some c → SafeDelV (uV z.2) s c := hp2.1 rfl + obtain ⟨h1, h2⟩ := hv_emptySlotN0 s info z.2 ⟨hp.1.1, hsafe⟩ x hx + have hts := hp.2.2 info (by rw [ha]) + have hF := fo_emptySlot_ts s info z.2 hts (fn_ilf hp.2.1) x hx + exact ⟨⟨h1, fun _ _ _ => Or.inl h2⟩, + ⟨⟨hF.1, nq_emptySlot s info z.2 hp.2.1.1.2 x hx⟩, hF.2⟩⟩ + · rename_i heb + obtain rfl := Set.mem_singleton_iff.1 hx + have hb : b = false := by + cases b + · rfl + · exact absurd (Or.inr rfl) heb + exact ⟨⟨hp.1.1, fun _ _ _ => Or.inr (hp2.2 hb).2⟩, hp.2.1⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + · have hp := hrec (.finaliseSlotCall s e) σ hσ z hz + exact ⟨⟨hp.inv, fun a ha _ => by cases ha⟩, hp.fn⟩ + | finaliseSlotCall s e => + intro σ hσ y hy + obtain ⟨hI, hL⟩ := hσ + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + obtain ⟨⟨cap, σ1⟩, hx, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy1⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨e1, hc⟩ := getCap_capAt hx + simp only at e1 hc + subst e1 + try dsimp only at hy1 + split at hy1 + · rename_i hn + subst hn + rw [mem_ret_ok hy1]; try dsimp only + refine ⟨⟨hI, fun b info h => ?_⟩, hL, fun _ _ => tcSafe_of_not fun t e => by rw [hc] at e; cases e⟩ + cases h + refine ⟨fun _ c hc' => ?_, fun h => by cases h⟩ + rw [hc] at hc'; cases hc' + exact safeDel_of_sRefs rfl + · rename_i hnn + obtain ⟨z, hz, H⟩ := mem_bindE' hy1 + obtain ⟨⟨fin, σ2⟩, hx2, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy2⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hx2) + try dsimp only at hy2 + obtain ⟨z, hz, H⟩ := mem_bindE' hy2 + obtain ⟨⟨rc, σ3⟩, hx3, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy3⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + have hL3 : FN [] σ3 := hP cap _ _ hL _ hx3 + have hrem := hTr (s := s) hx3 + have hrmv := hTm (K := oL σ3 ++ []) hI hc hx3 + obtain ⟨rem, cl⟩ := rc + obtain ⟨hI3, hc3, hB3, hC3⟩ := hF.step hI hc hnn hx3 + try dsimp only at hI3 + try dsimp only at hc3 + try dsimp only at hB3 + try dsimp only at hC3 + try dsimp only at hrem + try dsimp only at hrmv + try dsimp only at hy3 + split at hy3 + · rename_i hrm + rw [mem_ret_ok hy3]; try dsimp only + refine ⟨⟨hI3, fun b info h => ?_⟩, hL3, fun info h => ?_⟩ + · cases h + refine ⟨fun _ c hc' => ?_, fun h => by cases h⟩ + rw [hc3] at hc'; cases hc' + exact hB3 hrm + · exact hrmv hc3 hrm + · rename_i hrm + have hW := hC3 hrm + have hT : TcSafe (oL σ3 ++ []) σ3 s rem := hrem hc3 hrm + have hFs : ∀ σ4 u, (u, σ4) ∈ (setCap (Z := Unit) rem s σ3).1 → FN [] σ4 := fun σ4 u hx4 => + let hF := fo_setCap_p rem s σ3 hT (fn_ilf hL3) _ hx4 + ⟨⟨hF.1, nq_setCap rem s σ3 hL3.1.2 _ hx4⟩, hF.2⟩ + split at hy3 + · rename_i hcy + obtain ⟨z, hz, ⟨u, -, hy4⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy3 + · obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy4]; try dsimp only + have hv4 : uV σ4 = vCap s rem (uV σ3) := hv_setCap rem s (uV σ3) σ3 rfl _ hx4 + refine ⟨⟨by show UtInv''V (uV σ4); rw [hv4]; exact hW, fun b info h => ?_⟩, + hFs σ4 u' hx4, fun info h => by cases h⟩ + cases h + refine ⟨fun h => (by cases h), fun _ => ⟨by simpa using hcy.2, rem, ?_, ?_⟩⟩ + · show (uV σ4).2.1 s = _ + rw [hv4, vCap_cap, if_pos rfl] + · obtain ⟨hcz, -⟩ := hcy + cases rem with + | zombie p bb n => exact ⟨p, bb, n, rfl, hcz⟩ + | _ => exact False.elim hcz + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · rename_i hcy + obtain ⟨z, hz, H⟩ := mem_bindE' hy3 + obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + obtain ⟨a, -, hy4⟩ | ⟨er, her, -⟩ := H + swap + · cases her + have hv4 : uV σ4 = vCap s rem (uV σ3) := hv_setCap rem s (uV σ3) σ3 rfl _ hx4 + have hI4 : UtInv'' σ4 := by show UtInv''V (uV σ4); rw [hv4]; exact hW + have hL4 : FN [] σ4 := hFs σ4 u' hx4 + have hc4 : SInvU.capAt σ4 s = some rem := by + show (uV σ4).2.1 s = _ + rw [hv4, vCap_cap, if_pos rfl] + have hpre : FPre (.reduceZombieCall rem s e) σ4 := by + refine ⟨⟨hI4, hc4, hrm, fun hcz => ?_⟩, hL4⟩ + by_contra hne + exact hcy ⟨hcz, hne⟩ + obtain ⟨z, hz, H⟩ := mem_bindE' hy4 + have hP5 := hrec _ σ4 hpre z hz + obtain ⟨a, -, hy5⟩ | ⟨er, -, rfl⟩ := H + · obtain ⟨z6, hz6, H⟩ := mem_bindE' hy5 + have hv6 : uV z6.2 = uV z.2 := uk_preemptionPoint (uvw := uV z.2) z.2 rfl z6 hz6 + have hI6 : UtInv'' z6.2 := by show UtInv''V (uV z6.2); rw [hv6]; exact hP5.inv + have hL6 : FN [] z6.2 := fo_preemptionPoint z.2 hP5.fn z6 hz6 + obtain ⟨a, -, hy6⟩ | ⟨er, -, rfl⟩ := H + · exact hrec (.finaliseSlotCall s e) z6.2 ⟨hI6, hL6⟩ y hy6 + · exact ⟨⟨hI6, fun _ _ h => by cases h⟩, hL6, fun _ h => by cases h⟩ + · exact ⟨⟨hP5.inv, fun _ _ h => by cases h⟩, hP5.fn, fun _ h => by cases h⟩ + | reduceZombieCall c s e => + intro σ hσ y hy + obtain ⟨⟨hI, hc, hnr, hcy⟩, hL⟩ := hσ + cases c with + | zombie ptr bits n => + cases n with + | zero => exact (rz_failG rfl hy).elim + | succ n => + cases e with + | false => + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨cn, hcn, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hcn + try dsimp only at hy + obtain ⟨z, hz, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + obtain ⟨hne, rfl⟩ := hx + obtain ⟨z, hz, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy]; try dsimp only + refine ⟨?_, fo_capSwapForDelete _ s σ hL x hx⟩ + show UtInv''V (uV x.2) + refine hv_capSwapForDeleteN _ s σ ⟨hI, fun c1 c2 h1 h2 => ⟨fun _ => ?_, fun _ => ?_⟩⟩ x hx + · exact posOK_of_nn hI.2 hc (fun e => by cases e) + · exact posOK_firstSlot hI.2 hc + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | true => + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨es, hes, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hes + try dsimp only at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + have hp := hrec (.cTEDeleteCall (ptr, natToCref (zombieCteBits bits) n) false) σ ⟨hI, hL⟩ z hz + rcases H with ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩ + swap + · exact ⟨hp.1.1, hp.2⟩ + have hend := hp.1.2 a ha rfl + obtain ⟨z2, hz2, ⟨nc, hnc, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + swap + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + obtain ⟨⟨nc', σ5⟩, hx5, rfl⟩ := mem_liftE' hz2 + cases hnc + obtain ⟨e5, hc5⟩ := getCap_capAt hx5 + simp only at e5 hc5 + subst e5 + try dsimp only at hy + split at hy + · rename_i hnz + subst hnz + obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + rw [mem_ret_ok hy]; try dsimp only + have hv := hv_setCap (.zombie ptr bits n) s (uV z.2) z.2 rfl _ hx + have hF := fo_setCap_p (K := []) (.zombie ptr bits n) s z.2 + (tcSafe_of_not fun t e => by rw [hc5] at e; cases e) (fn_ilf hp.2) x hx + refine ⟨?_, ⟨hF.1, nq_setCap _ s z.2 hp.2.1.2 x hx⟩, hF.2⟩ + show UtInv''V (uV x.2) + rw [hv] + exact utN_shrink hp.1.1 hc5 hnr hend + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + obtain ⟨-, rfl⟩ := hx + rw [mem_ret_ok hy]; try dsimp only + exact ⟨hp.1.1, hp.2⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | _ => exact (rz_failG rfl hy).elim + +theorem fo_recDelG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (FN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → + TcSafe (oL y.2 ++ []) y.2 s y.1.1) + (hTm : ∀ {K : List ObjRef} {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe K y.2 s .nullCap) (k : RecDelCall) : HR (FPre k) (FPost k) (recDelG Fn k : NonDet (State Unit) (Except Unit (Bool × Cap))) := by + unfold recDelG + exact hr_lfp FPre FPost (fun f hf k => fo_stepG hF hP hTr hTm f hf k) k + +theorem fou_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (FN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → + TcSafe (oL y.2 ++ []) y.2 s y.1.1) + (hTm : ∀ {K : List ObjRef} {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe K y.2 s .nullCap) (slot : CslotPtr) : IKE FU (capDeleteG Fn slot) := by + unfold capDeleteG + exact ike_bind (fun σ h y hy => let hp := fo_recDelG hF hP hTr hTm (.cTEDeleteCall slot true) σ ⟨h.2, h.1⟩ y hy + ⟨hp.fn, hp.inv⟩) fun _ => ike_returnOk _ + +theorem fou_capRevokeFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (FN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → + TcSafe (oL y.2 ++ []) y.2 s y.1.1) + (hTm : ∀ {K : List ObjRef} {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe K y.2 s .nullCap) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IK FU (f k)) (sl : CslotPtr) : IKE FU (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKE FU (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + repeat' (first + | (with_reducible exact fou_capDeleteG hF hP hTr hTm _) | (with_reducible exact fou_preemptionPoint) + | (with_reducible exact hrec' _) | ik_step0 | ik_unfold | (unfold NonDetE.whenE)) + all_goals exact hrec' _ + +theorem fou_capRevokeG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (FN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → + TcSafe (oL y.2 ++ []) y.2 s y.1.1) + (hTm : ∀ {K : List ObjRef} {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe K y.2 s .nullCap) (sl : CslotPtr) : IKE FU (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => fou_capRevokeFG hF hP hTr hTm hf k) sl + +end + +end Fl4Proofs.P3.SInvF diff --git a/proofs/Fl4Proofs/P3/VmFinGenLIo.lean b/proofs/Fl4Proofs/P3/VmFinGenLIo.lean new file mode 100644 index 000000000..d38d19e78 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenLIo.lean @@ -0,0 +1,173 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvUtO1's `io_recDelF` and its tops over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`), the finaliser's facts as hypotheses. +Written by `handoff/tools/gen_finlong.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtO1 +import Fl4Proofs.P3.VmFinGenBase +import Fl4Proofs.P3.VmFinGenLRd +import Fl4Proofs.P3.SpecInvUtO3 +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv +noncomputable section +set_option maxHeartbeats 1000000 +variable {N : Irq → ObjRef} + +open Fl4Proofs.P3.VmFin + +theorem io_recDelFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (IO N) (Fn c b)) + (f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) + (hrec : ∀ k, IK (IO N) (f k)) (k : RecDelCall) : + IKE (IO N) (recDelFG Fn f k) := by + have hrec' : ∀ k, IKE (ε := Unit) (IO N) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall s e => + unfold recDelFG withoutPreemption; io0_auto + all_goals exact hrec' _ + | finaliseSlotCall sl e => + intro σ hσ y hy + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + obtain ⟨⟨cap, σ1⟩, hx, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy1⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨e1, hc⟩ := getCap_capAt hx + simp only at e1 hc + subst e1 + try dsimp only at hy1 + split at hy1 + · rw [mem_ret_ok hy1]; exact hσ + · obtain ⟨z, hz, H⟩ := mem_bindE' hy1 + obtain ⟨⟨fin, σ2⟩, hx2, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy2⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hx2) + try dsimp only at hy2 + obtain ⟨z, hz, H⟩ := mem_bindE' hy2 + obtain ⟨⟨rc, σ3⟩, hx3, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy3⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rem, cl⟩ := rc + have hO3 : IO N σ3 := hP cap _ _ hσ _ hx3 + have hc3 : SInvU.capAt σ3 sl = some cap := hF.keep hc _ _ hx3 + have hcov : covR rem ⊆ covR cap := hF.cov cap _ _ _ hx3 + have hO4 : IrqOutV N (vCap sl rem (uV σ3)) := irqOut_vCap hO3 (covBy_of hc3 hcov) sl + try dsimp only at hy3 + split at hy3 + · rw [mem_ret_ok hy3]; exact hO3 + · split at hy3 + · obtain ⟨z, hz, ⟨u, -, hy4⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy3 + · obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy4] + have hv4 : uV σ4 = vCap sl rem (uV σ3) := hv_setCap rem sl (uV σ3) σ3 rfl _ hx4 + show IrqOutV N (uV σ4); rw [hv4]; exact hO4 + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain ⟨z, hz, H⟩ := mem_bindE' hy3 + obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + obtain ⟨a, -, hy4⟩ | ⟨er, her, -⟩ := H + swap + · cases her + have hv4 : uV σ4 = vCap sl rem (uV σ3) := hv_setCap rem sl (uV σ3) σ3 rfl _ hx4 + have hO5 : IO N σ4 := by show IrqOutV N (uV σ4); rw [hv4]; exact hO4 + obtain ⟨z, hz, H⟩ := mem_bindE' hy4 + have hI5 : IO N z.2 := hrec _ σ4 hO5 z hz + obtain ⟨a, -, hy5⟩ | ⟨er, -, rfl⟩ := H + · obtain ⟨z6, hz6, H⟩ := mem_bindE' hy5 + have hv6 : uV z6.2 = uV z.2 := uk_preemptionPoint (uvw := uV z.2) z.2 rfl z6 hz6 + have hI6 : IO N z6.2 := by show IrqOutV N (uV z6.2); rw [hv6]; exact hI5 + obtain ⟨a, -, hy6⟩ | ⟨er, -, rfl⟩ := H + · exact hrec _ z6.2 hI6 y hy6 + · exact hI6 + · exact hI5 + | reduceZombieCall c s e => + cases c with + | zombie ptr bits n => + cases n with + | zero => intro σ _ y hy; exact (rz_failG rfl hy).elim + | succ n => + cases e with + | false => + simp only [recDelFG, withoutPreemption]; io0_auto + | true => + intro σ hσ y hy + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨es, hes, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hes + try dsimp only at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + have hp : IO N z.2 := hrec _ σ hσ z hz + rcases H with ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩ + swap + · exact hp + obtain ⟨z2, hz2, ⟨nc, hnc, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + swap + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + obtain ⟨⟨nc', σ5⟩, hx5, rfl⟩ := mem_liftE' hz2 + cases hnc + obtain ⟨e5, hc5⟩ := getCap_capAt hx5 + simp only at e5 hc5 + subst e5 + try dsimp only at hy + split at hy + · rename_i hnz + subst hnz + obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + rw [mem_ret_ok hy] + have hv := hv_setCap (.zombie ptr bits n) s (uV z.2) z.2 rfl _ hx + show IrqOutV N (uV x.2) + rw [hv] + exact irqOut_vCap hp (covBy_of hc5 (fun q hq => by simpa [covR, capRng, objRefs] using hq)) s + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + obtain ⟨-, rfl⟩ := hx + rw [mem_ret_ok hy] + exact hp + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | _ => intro σ _ y hy; exact (rz_failG rfl hy).elim + +theorem io_recDelG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (IO N) (Fn c b)) (c : RecDelCall) : IKE (IO N) (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => io_recDelFG hF hP f hf k) c + +theorem io_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (IO N) (Fn c b)) (slot : CslotPtr) : IKE (IO N) (capDeleteG Fn slot) := by + unfold capDeleteG + exact ike_bind (io_recDelG hF hP _) fun _ => ike_returnOk _ + +theorem p3_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (IO N) (Fn c b)) (slot : CslotPtr) : IKE (P3 N) (capDeleteG Fn slot) := + p3_of (ikN_capDeleteG hF slot) (io_capDeleteG hF hP slot) + +theorem un_capRevokeFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (IO N) (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IK (P3 N) (f k)) (sl : CslotPtr) : IKE (P3 N) (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKE (P3 N) (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + un_auto + all_goals first | exact hrec' _ | exact p3_capDeleteG hF hP _ | apply p3_capDeleteG hF hP + +theorem un_capRevokeG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (IO N) (Fn c b)) + (sl : CslotPtr) : IKE (P3 N) (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => un_capRevokeFG hF hP hf k) sl + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinGenLLd.lean b/proofs/Fl4Proofs/P3/VmFinGenLLd.lean new file mode 100644 index 000000000..2688f16b7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenLLd.lean @@ -0,0 +1,301 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvNtfnQLD's `ld_step` and its tops over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`), the finaliser's facts as hypotheses. +Written by `handoff/tools/gen_finlong.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvNtfnQLD +import Fl4Proofs.P3.VmFinGenBase +import Fl4Proofs.P3.VmFinGenLRd +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec Fl4Proofs.P3.SInvU +noncomputable section +set_option maxHeartbeats 2000000 +set_option maxErrors 2000 +variable {Kq : List ObjRef} + +open Fl4Proofs.P3.VmFin + +theorem ld_stepG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (ILN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → TcSafe [] y.2 s y.1.1) + (hTm : ∀ {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe [] y.2 s .nullCap) + (f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) + (hrec : ∀ k, HR (LPre k) (LPost k) (f k)) (k : RecDelCall) : + HR (LPre k) (LPost k) (recDelFG Fn f k : NonDet (State Unit) (Except Unit (Bool × Cap))) := by + cases k with + | cTEDeleteCall s e => + intro σ hσ y hy + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩⟩ := mem_bindE' hy + · have hp := hrec (.finaliseSlotCall s e) σ hσ z hz + obtain ⟨b, info⟩ := a + have hp2 := hp.1.2 b info ha + try dsimp only at hy + obtain ⟨z2, hz2, ⟨u, hu, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz2 + rw [mem_ret_ok hy]; try dsimp only + unfold NonDet.when at hx + split at hx + · rename_i heb + have hbt : b = true := by + cases b + · obtain ⟨he, -⟩ := hp2.2 rfl + subst he + simp at heb + · rfl + subst hbt + have hsafe : ∀ c, SInvU.capAt z.2 s = some c → SafeDelV (uV z.2) s c := hp2.1 rfl + obtain ⟨h1, h2⟩ := hv_emptySlotN0 s info z.2 ⟨hp.1.1, hsafe⟩ x hx + have hts := hp.2.2 info (by rw [ha]) + exact ⟨⟨h1, fun _ _ _ => Or.inl h2⟩, + il_emptySlot_ts s info z.2 hts (il_postCapDeletion _) hp.2.1.1 x hx, + nq_emptySlot s info z.2 hp.2.1.2 x hx⟩ + · rename_i heb + obtain rfl := Set.mem_singleton_iff.1 hx + have hb : b = false := by + cases b + · rfl + · exact absurd (Or.inr rfl) heb + exact ⟨⟨hp.1.1, fun _ _ _ => Or.inr (hp2.2 hb).2⟩, hp.2.1⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + · have hp := hrec (.finaliseSlotCall s e) σ hσ z hz + exact ⟨⟨hp.inv, fun a ha _ => by cases ha⟩, hp.il⟩ + | finaliseSlotCall s e => + intro σ hσ y hy + obtain ⟨hI, hL⟩ := hσ + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + obtain ⟨⟨cap, σ1⟩, hx, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy1⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨e1, hc⟩ := getCap_capAt hx + simp only at e1 hc + subst e1 + try dsimp only at hy1 + split at hy1 + · rename_i hn + subst hn + rw [mem_ret_ok hy1]; try dsimp only + refine ⟨⟨hI, fun b info h => ?_⟩, hL, fun _ _ => tcSafe_of_not fun t e => by rw [hc] at e; cases e⟩ + cases h + refine ⟨fun _ c hc' => ?_, fun h => by cases h⟩ + rw [hc] at hc'; cases hc' + exact safeDel_of_sRefs rfl + · rename_i hnn + obtain ⟨z, hz, H⟩ := mem_bindE' hy1 + obtain ⟨⟨fin, σ2⟩, hx2, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy2⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hx2) + try dsimp only at hy2 + obtain ⟨z, hz, H⟩ := mem_bindE' hy2 + obtain ⟨⟨rc, σ3⟩, hx3, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy3⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + have hL3 : ILN [] σ3 := hP cap _ _ hL _ hx3 + have hrem := hTr (s := s) hx3 + have hrmv := hTm hI hc hx3 + obtain ⟨rem, cl⟩ := rc + obtain ⟨hI3, hc3, hB3, hC3⟩ := hF.step hI hc hnn hx3 + try dsimp only at hI3 + try dsimp only at hc3 + try dsimp only at hB3 + try dsimp only at hC3 + try dsimp only at hrem + try dsimp only at hrmv + try dsimp only at hy3 + split at hy3 + · rename_i hrm + rw [mem_ret_ok hy3]; try dsimp only + refine ⟨⟨hI3, fun b info h => ?_⟩, hL3, fun info h => ?_⟩ + · cases h + refine ⟨fun _ c hc' => ?_, fun h => by cases h⟩ + rw [hc3] at hc'; cases hc' + exact hB3 hrm + · exact hrmv hc3 hrm + · rename_i hrm + have hW := hC3 hrm + have hT : TcSafe [] σ3 s rem := hrem hc3 hrm + split at hy3 + · rename_i hcy + obtain ⟨z, hz, ⟨u, -, hy4⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy3 + · obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy4]; try dsimp only + have hv4 : uV σ4 = vCap s rem (uV σ3) := hv_setCap rem s (uV σ3) σ3 rfl _ hx4 + refine ⟨⟨by show UtInv''V (uV σ4); rw [hv4]; exact hW, fun b info h => ?_⟩, + ⟨il_setCap_p rem s σ3 (fun _ => hT) hL3.1 _ hx4, nq_setCap rem s σ3 hL3.2 _ hx4⟩, + fun info h => by cases h⟩ + cases h + refine ⟨fun h => (by cases h), fun _ => ⟨by simpa using hcy.2, rem, ?_, ?_⟩⟩ + · show (uV σ4).2.1 s = _ + rw [hv4, vCap_cap, if_pos rfl] + · obtain ⟨hcz, -⟩ := hcy + cases rem with + | zombie p bb n => exact ⟨p, bb, n, rfl, hcz⟩ + | _ => exact False.elim hcz + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · rename_i hcy + obtain ⟨z, hz, H⟩ := mem_bindE' hy3 + obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + obtain ⟨a, -, hy4⟩ | ⟨er, her, -⟩ := H + swap + · cases her + have hv4 : uV σ4 = vCap s rem (uV σ3) := hv_setCap rem s (uV σ3) σ3 rfl _ hx4 + have hI4 : UtInv'' σ4 := by show UtInv''V (uV σ4); rw [hv4]; exact hW + have hL4 : ILN [] σ4 := ⟨il_setCap_p rem s σ3 (fun _ => hT) hL3.1 _ hx4, nq_setCap rem s σ3 hL3.2 _ hx4⟩ + have hc4 : SInvU.capAt σ4 s = some rem := by + show (uV σ4).2.1 s = _ + rw [hv4, vCap_cap, if_pos rfl] + have hpre : LPre (.reduceZombieCall rem s e) σ4 := by + refine ⟨⟨hI4, hc4, hrm, fun hcz => ?_⟩, hL4⟩ + by_contra hne + exact hcy ⟨hcz, hne⟩ + obtain ⟨z, hz, H⟩ := mem_bindE' hy4 + have hP5 := hrec _ σ4 hpre z hz + obtain ⟨a, -, hy5⟩ | ⟨er, -, rfl⟩ := H + · obtain ⟨z6, hz6, H⟩ := mem_bindE' hy5 + have hv6 : uV z6.2 = uV z.2 := uk_preemptionPoint (uvw := uV z.2) z.2 rfl z6 hz6 + have hI6 : UtInv'' z6.2 := by show UtInv''V (uV z6.2); rw [hv6]; exact hP5.inv + have hL6 : ILN [] z6.2 := iln_preemptionPoint z.2 hP5.il z6 hz6 + obtain ⟨a, -, hy6⟩ | ⟨er, -, rfl⟩ := H + · exact hrec (.finaliseSlotCall s e) z6.2 ⟨hI6, hL6⟩ y hy6 + · exact ⟨⟨hI6, fun _ _ h => by cases h⟩, hL6, fun _ h => by cases h⟩ + · exact ⟨⟨hP5.inv, fun _ _ h => by cases h⟩, hP5.il, fun _ h => by cases h⟩ + | reduceZombieCall c s e => + intro σ hσ y hy + obtain ⟨⟨hI, hc, hnr, hcy⟩, hL⟩ := hσ + cases c with + | zombie ptr bits n => + cases n with + | zero => exact (rz_failG rfl hy).elim + | succ n => + cases e with + | false => + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨cn, hcn, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hcn + try dsimp only at hy + obtain ⟨z, hz, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + obtain ⟨hne, rfl⟩ := hx + obtain ⟨z, hz, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy]; try dsimp only + refine ⟨?_, iln_capSwapForDelete _ s σ hL x hx⟩ + show UtInv''V (uV x.2) + refine hv_capSwapForDeleteN _ s σ ⟨hI, fun c1 c2 h1 h2 => ⟨fun _ => ?_, fun _ => ?_⟩⟩ x hx + · exact posOK_of_nn hI.2 hc (fun e => by cases e) + · exact posOK_firstSlot hI.2 hc + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | true => + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨es, hes, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hes + try dsimp only at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + have hp := hrec (.cTEDeleteCall (ptr, natToCref (zombieCteBits bits) n) false) σ ⟨hI, hL⟩ z hz + rcases H with ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩ + swap + · exact ⟨hp.1.1, hp.2⟩ + have hend := hp.1.2 a ha rfl + obtain ⟨z2, hz2, ⟨nc, hnc, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + swap + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + obtain ⟨⟨nc', σ5⟩, hx5, rfl⟩ := mem_liftE' hz2 + cases hnc + obtain ⟨e5, hc5⟩ := getCap_capAt hx5 + simp only at e5 hc5 + subst e5 + try dsimp only at hy + split at hy + · rename_i hnz + subst hnz + obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + rw [mem_ret_ok hy]; try dsimp only + have hv := hv_setCap (.zombie ptr bits n) s (uV z.2) z.2 rfl _ hx + refine ⟨?_, il_setCap_nt _ s z.2 (fun t e => by rw [hc5] at e; cases e) hp.2.1 x hx, + nq_setCap _ s z.2 hp.2.2 x hx⟩ + show UtInv''V (uV x.2) + rw [hv] + exact utN_shrink hp.1.1 hc5 hnr hend + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + obtain ⟨-, rfl⟩ := hx + rw [mem_ret_ok hy]; try dsimp only + exact ⟨hp.1.1, hp.2⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | _ => exact (rz_failG rfl hy).elim + +theorem ld_recDelG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (ILN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → TcSafe [] y.2 s y.1.1) + (hTm : ∀ {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe [] y.2 s .nullCap) (k : RecDelCall) : HR (LPre k) (LPost k) (recDelG Fn k : NonDet (State Unit) (Except Unit (Bool × Cap))) := by + unfold recDelG + exact hr_lfp LPre LPost (fun f hf k => ld_stepG hF hP hTr hTm f hf k) k + +theorem ilu_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (ILN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → TcSafe [] y.2 s y.1.1) + (hTm : ∀ {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe [] y.2 s .nullCap) (slot : CslotPtr) : IKE ILU (capDeleteG Fn slot) := by + unfold capDeleteG + exact ike_bind (fun σ h y hy => let hp := ld_recDelG hF hP hTr hTm (.cTEDeleteCall slot true) σ ⟨h.2, h.1⟩ y hy + ⟨hp.il, hp.inv⟩) fun _ => ike_returnOk _ + +theorem ilu_capRevokeFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (ILN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → TcSafe [] y.2 s y.1.1) + (hTm : ∀ {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe [] y.2 s .nullCap) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IK ILU (f k)) (sl : CslotPtr) : IKE ILU (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKE ILU (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + repeat' (first + | (with_reducible exact ilu_capDeleteG hF hP hTr hTm _) | (with_reducible exact ilu_preemptionPoint) + | (with_reducible exact hrec' _) | ik_step0 | ik_unfold | (unfold NonDetE.whenE)) + all_goals exact hrec' _ + +theorem ilu_capRevokeG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (ILN []) (Fn c b)) + (hTr : ∀ {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit}, + y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → ∀ {s : CslotPtr}, SInvU.capAt y.2 s = some cap → ¬ capRemoveable y.1.1 s → TcSafe [] y.2 s y.1.1) + (hTm : ∀ {σ : State Unit}, SInvU.UtInv'' σ → ∀ {s : CslotPtr} {cap : Cap}, SInvU.capAt σ s = some cap → + ∀ {y : (Cap × Cap) × State Unit}, y ∈ (Fn cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1 → + SInvU.capAt y.2 s = some cap → capRemoveable y.1.1 s → TcSafe [] y.2 s .nullCap) (sl : CslotPtr) : IKE ILU (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => ilu_capRevokeFG hF hP hTr hTm hf k) sl + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinGenLRd.lean b/proofs/Fl4Proofs/P3/VmFinGenLRd.lean new file mode 100644 index 000000000..7d92eafdb --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenLRd.lean @@ -0,0 +1,255 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvUtN5's `rd_step` and its tops over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`), the finaliser's facts as hypotheses. +Written by `handoff/tools/gen_finlong.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtN5 +import Fl4Proofs.P3.VmFinGenBase +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv +noncomputable section +set_option maxHeartbeats 2000000 + +open Fl4Proofs.P3.VmFin + +theorem rz_failG {Fn : FinFn} {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} {c : Cap} {s : CslotPtr} + {e : Bool} (h : (recDelFG Fn f (.reduceZombieCall c s e) : NonDet (State Unit) _) = + (NonDetE.liftE (ε := Unit) (NonDet.fail : NonDet (State Unit) (Bool × Cap)) : NonDet (State Unit) _)) + {σ : State Unit} {y : Except Unit (Bool × Cap) × State Unit} + (hy : y ∈ ((recDelFG Fn f (.reduceZombieCall c s e) : NonDet (State Unit) _) σ).1) : False := by + rw [h] at hy + obtain ⟨x, hx, -⟩ := mem_liftE' (m := (NonDet.fail : NonDet (State Unit) (Bool × Cap))) hy + exact hx + +theorem rd_stepG {Fn : FinFn} (hF : FinFacts Fn) + (f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) + (hrec : ∀ k, HR (RPre k) (RPost k) (f k)) (k : RecDelCall) : + HR (RPre k) (RPost k) (recDelFG Fn f k : NonDet (State Unit) (Except Unit (Bool × Cap))) := by + cases k with + | cTEDeleteCall s e => + intro σ hσ y hy + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩⟩ := mem_bindE' hy + · have hp := hrec (.finaliseSlotCall s e) σ hσ z hz + obtain ⟨b, info⟩ := a + have hp2 := hp.2 b info ha + try dsimp only at hy + obtain ⟨z2, hz2, ⟨u, hu, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz2 + rw [mem_ret_ok hy]; try dsimp only + unfold NonDet.when at hx + split at hx + · rename_i heb + have hsafe : ∀ c, SInvU.capAt z.2 s = some c → SafeDelV (uV z.2) s c := by + cases b + · obtain ⟨he, -⟩ := hp2.2 rfl + subst he + simp at heb + · exact hp2.1 rfl + obtain ⟨h1, h2⟩ := hv_emptySlotN0 s info z.2 ⟨hp.1, hsafe⟩ x hx + exact ⟨h1, fun _ _ _ => Or.inl h2⟩ + · rename_i heb + obtain rfl := Set.mem_singleton_iff.1 hx + have hb : b = false := by + cases b + · rfl + · exact absurd (Or.inr rfl) heb + exact ⟨hp.1, fun _ _ _ => Or.inr (hp2.2 hb).2⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + · exact ⟨(hrec (.finaliseSlotCall s e) σ hσ z hz).inv, fun a ha _ => by cases ha⟩ + | finaliseSlotCall s e => + intro σ hσ y hy + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + obtain ⟨⟨cap, σ1⟩, hx, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy1⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨e1, hc⟩ := getCap_capAt hx + simp only at e1 hc + subst e1 + try dsimp only at hy1 + split at hy1 + · rename_i hn + subst hn + rw [mem_ret_ok hy1]; try dsimp only + refine ⟨hσ, fun b info h => ?_⟩ + cases h + refine ⟨fun _ c hc' => ?_, fun h => by cases h⟩ + rw [hc] at hc'; cases hc' + exact safeDel_of_sRefs rfl + · rename_i hnn + obtain ⟨z, hz, H⟩ := mem_bindE' hy1 + obtain ⟨⟨fin, σ2⟩, hx2, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy2⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hx2) + try dsimp only at hy2 + obtain ⟨z, hz, H⟩ := mem_bindE' hy2 + obtain ⟨⟨rc, σ3⟩, hx3, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy3⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rem, cl⟩ := rc + obtain ⟨hI3, hc3, hB3, hC3⟩ := hF.step hσ hc hnn hx3 + try dsimp only at hy3 + try dsimp only at hI3 + try dsimp only at hc3 + try dsimp only at hB3 + try dsimp only at hC3 + split at hy3 + · rename_i hrm + rw [mem_ret_ok hy3]; try dsimp only + refine ⟨hI3, fun b info h => ?_⟩ + cases h + refine ⟨fun _ c hc' => ?_, fun h => by cases h⟩ + rw [hc3] at hc'; cases hc' + exact hB3 hrm + · rename_i hrm + have hW := hC3 hrm + split at hy3 + · rename_i hcy + obtain ⟨z, hz, ⟨u, -, hy4⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy3 + · obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy4]; try dsimp only + have hv4 : uV σ4 = vCap s rem (uV σ3) := hv_setCap rem s (uV σ3) σ3 rfl _ hx4 + refine ⟨by show UtInv''V (uV σ4); rw [hv4]; exact hW, fun b info h => ?_⟩ + cases h + refine ⟨fun h => (by cases h), fun _ => ⟨by simpa using hcy.2, rem, ?_, ?_⟩⟩ + · show (uV σ4).2.1 s = _ + rw [hv4, vCap_cap, if_pos rfl] + · obtain ⟨hcz, -⟩ := hcy + cases rem with + | zombie p bb n => exact ⟨p, bb, n, rfl, hcz⟩ + | _ => exact False.elim hcz + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · rename_i hcy + obtain ⟨z, hz, H⟩ := mem_bindE' hy3 + obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + obtain ⟨a, -, hy4⟩ | ⟨er, her, -⟩ := H + swap + · cases her + have hv4 : uV σ4 = vCap s rem (uV σ3) := hv_setCap rem s (uV σ3) σ3 rfl _ hx4 + have hI4 : UtInv'' σ4 := by show UtInv''V (uV σ4); rw [hv4]; exact hW + have hc4 : SInvU.capAt σ4 s = some rem := by + show (uV σ4).2.1 s = _ + rw [hv4, vCap_cap, if_pos rfl] + have hpre : RPre (.reduceZombieCall rem s e) σ4 := by + refine ⟨hI4, hc4, hrm, fun hcz => ?_⟩ + by_contra hne + exact hcy ⟨hcz, hne⟩ + obtain ⟨z, hz, H⟩ := mem_bindE' hy4 + have hI5 : UtInv'' z.2 := hrec _ σ4 hpre z hz + obtain ⟨a, -, hy5⟩ | ⟨er, -, rfl⟩ := H + · obtain ⟨z6, hz6, H⟩ := mem_bindE' hy5 + have hv6 : uV z6.2 = uV z.2 := uk_preemptionPoint (uvw := uV z.2) z.2 rfl z6 hz6 + have hI6 : UtInv'' z6.2 := by show UtInv''V (uV z6.2); rw [hv6]; exact hI5 + obtain ⟨a, -, hy6⟩ | ⟨er, -, rfl⟩ := H + · exact hrec (.finaliseSlotCall s e) z6.2 hI6 y hy6 + · exact ⟨hI6, fun _ _ h => by cases h⟩ + · exact ⟨hI5, fun _ _ h => by cases h⟩ + | reduceZombieCall c s e => + intro σ hσ y hy + obtain ⟨hI, hc, hnr, hcy⟩ := hσ + cases c with + | zombie ptr bits n => + cases n with + | zero => exact (rz_failG rfl hy).elim + | succ n => + cases e with + | false => + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨cn, hcn, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hcn + try dsimp only at hy + obtain ⟨z, hz, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + obtain ⟨hne, rfl⟩ := hx + obtain ⟨z, hz, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy]; try dsimp only + show UtInv''V (uV x.2) + refine hv_capSwapForDeleteN _ s σ ⟨hI, fun c1 c2 h1 h2 => ⟨fun _ => ?_, fun _ => ?_⟩⟩ x hx + · exact posOK_of_nn hI.2 hc (fun e => by cases e) + · exact posOK_firstSlot hI.2 hc + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | true => + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨es, hes, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hes + try dsimp only at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + have hp := hrec (.cTEDeleteCall (ptr, natToCref (zombieCteBits bits) n) false) σ hI z hz + rcases H with ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩ + swap + · exact hp.1 + have hend := hp.2 a ha rfl + obtain ⟨z2, hz2, ⟨nc, hnc, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + swap + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + obtain ⟨⟨nc', σ5⟩, hx5, rfl⟩ := mem_liftE' hz2 + cases hnc + obtain ⟨e5, hc5⟩ := getCap_capAt hx5 + simp only at e5 hc5 + subst e5 + try dsimp only at hy + split at hy + · rename_i hnz + subst hnz + obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + rw [mem_ret_ok hy]; try dsimp only + have hv := hv_setCap (.zombie ptr bits n) s (uV z.2) z.2 rfl _ hx + show UtInv''V (uV x.2) + rw [hv] + exact utN_shrink hp.1 hc5 hnr hend + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + obtain ⟨-, rfl⟩ := hx + rw [mem_ret_ok hy]; try dsimp only + exact hp.1 + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | _ => exact (rz_failG rfl hy).elim + +theorem hr_recDelG {Fn : FinFn} (hF : FinFacts Fn) (k : RecDelCall) : + HR (RPre k) (RPost k) (recDelG Fn k : NonDet (State Unit) (Except Unit (Bool × Cap))) := by + unfold recDelG + exact hr_lfp RPre RPost (fun f hf k => rd_stepG hF f hf k) k + +theorem ikN_recDel_cteG {Fn : FinFn} (hF : FinFacts Fn) (s : CslotPtr) (e : Bool) : + IKE UtInv'' (recDelG Fn (.cTEDeleteCall s e)) := + fun σ hI y hy => (hr_recDelG hF (.cTEDeleteCall s e) σ hI y hy).inv + +theorem ikN_recDel_finG {Fn : FinFn} (hF : FinFacts Fn) (s : CslotPtr) (e : Bool) : + IKE UtInv'' (recDelG Fn (.finaliseSlotCall s e)) := + fun σ hI y hy => (hr_recDelG hF (.finaliseSlotCall s e) σ hI y hy).inv + +theorem ikN_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (slot : CslotPtr) : IKE UtInv'' (capDeleteG Fn slot) := by + unfold capDeleteG + exact ike_bind (ikN_recDel_cteG hF slot true) fun _ => ike_returnOk _ + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinGenLSg.lean b/proofs/Fl4Proofs/P3/VmFinGenLSg.lean new file mode 100644 index 000000000..f5abf6f35 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenLSg.lean @@ -0,0 +1,204 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvSgA's `no_recDelF` and its tops over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`), the finaliser's facts as hypotheses. +Written by `handoff/tools/gen_finlong.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvSgA +import Fl4Proofs.P3.VmFinGenBase +import Fl4Proofs.P3.VmFinGenLIo +import Fl4Proofs.P3.SpecInvSgG +open Classical + +namespace Fl4Proofs.P3.SInvG +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU +noncomputable section +set_option maxHeartbeats 1000000 +variable {N M : Irq → ObjRef} + +open Fl4Proofs.P3.VmFin + +theorem no_recDelFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NO N) (Fn c b)) + (hR : ∀ {w : UView}, NOV N w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotNode N sl ∨ NtfnOrNull x.1.1) + (f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) + (hrec : ∀ k, HR (NPre N k) (fun _ => NO N) (f k)) (k : RecDelCall) : + HR (NPre N k) (fun _ => NO N) (recDelFG Fn f k) := by + have hfin : ∀ s e, IKE (ε := Unit) (NO N) (f (.finaliseSlotCall s e) : NonDetE (State Unit) Unit (Bool × Cap)) := + fun s e σ h y hy => hrec (.finaliseSlotCall s e) σ h y hy + have hcte : ∀ s e, IKE (ε := Unit) (NO N) (f (.cTEDeleteCall s e) : NonDetE (State Unit) Unit (Bool × Cap)) := + fun s e σ h y hy => hrec (.cTEDeleteCall s e) σ h y hy + cases k with + | cTEDeleteCall s e => + intro σ hσ y hy + replace hσ : NO N σ := hσ + refine (show IKE (NO N) (recDelFG Fn f (.cTEDeleteCall s e)) by + unfold recDelFG withoutPreemption; no0_auto + all_goals first | exact hfin _ _ | exact hcte _ _) σ hσ y hy + | finaliseSlotCall sl e => + intro σ hσ y hy + replace hσ : NO N σ := hσ + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + obtain ⟨⟨cap, σ1⟩, hx, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy1⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨e1, hc⟩ := getCap_capAt hx + simp only at e1 hc + subst e1 + try dsimp only at hy1 + split at hy1 + · rw [mem_ret_ok hy1]; exact hσ + · obtain ⟨z, hz, H⟩ := mem_bindE' hy1 + obtain ⟨⟨fin, σ2⟩, hx2, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy2⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hx2) + try dsimp only at hy2 + obtain ⟨z, hz, H⟩ := mem_bindE' hy2 + obtain ⟨⟨rc, σ3⟩, hx3, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy3⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rem, cl⟩ := rc + have hO3 : NO N σ3 := hP cap _ _ hσ _ hx3 + have hc3 : SInvU.capAt σ3 sl = some cap := hF.keep hc _ _ hx3 + have hcov : covR rem ⊆ covR cap := hF.cov cap _ _ _ hx3 + have hrem : NotNode N sl ∨ NtfnOrNull rem := hR hσ.2 hc hx3 + have hO4 : NO N σ3 → IrqOutV N (vCap sl rem (uV σ3)) ∧ NOV N (vCap sl rem (uV σ3)) := fun h => + ⟨irqOut_vCap h.1 (covBy_of hc3 hcov) sl, nov_vCap h.2 hrem⟩ + try dsimp only at hy3 + split at hy3 + · rw [mem_ret_ok hy3]; exact hO3 + · split at hy3 + · obtain ⟨z, hz, ⟨u, -, hy4⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy3 + · obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy4] + have hv4 : uV σ4 = vCap sl rem (uV σ3) := hv_setCap rem sl (uV σ3) σ3 rfl _ hx4 + show IrqOutV N (uV σ4) ∧ NOV N (uV σ4); rw [hv4]; exact hO4 hO3 + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain ⟨z, hz, H⟩ := mem_bindE' hy3 + obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + obtain ⟨a, -, hy4⟩ | ⟨er, her, -⟩ := H + swap + · cases her + have hv4 : uV σ4 = vCap sl rem (uV σ3) := hv_setCap rem sl (uV σ3) σ3 rfl _ hx4 + have hO5 : NO N σ4 := by show IrqOutV N (uV σ4) ∧ NOV N (uV σ4); rw [hv4]; exact hO4 hO3 + have hc4 : SInvU.capAt σ4 sl = some rem := by + show (uV σ4).2.1 sl = some rem; rw [hv4, vCap_cap, if_pos rfl] + obtain ⟨z, hz, H⟩ := mem_bindE' hy4 + have hI5 : NO N z.2 := hrec (.reduceZombieCall rem sl e) σ4 ⟨hO5, hc4⟩ z hz + obtain ⟨a, -, hy5⟩ | ⟨er, -, rfl⟩ := H + · obtain ⟨z6, hz6, H⟩ := mem_bindE' hy5 + have hv6 : uV z6.2 = uV z.2 := uk_preemptionPoint (uvw := uV z.2) z.2 rfl z6 hz6 + have hI6 : NO N z6.2 := by show IrqOutV N (uV z6.2) ∧ NOV N (uV z6.2); rw [hv6]; exact hI5 + obtain ⟨a, -, hy6⟩ | ⟨er, -, rfl⟩ := H + · exact hrec (.finaliseSlotCall sl e) z6.2 hI6 y hy6 + · exact hI6 + · exact hI5 + | reduceZombieCall c s e => + cases c with + | zombie ptr bits n => + cases n with + | zero => intro σ _ y hy; exact (rz_failG rfl hy).elim + | succ n => + cases e with + | false => + intro σ hσ y hy + obtain ⟨hσ, hcs⟩ := hσ + have h1 : NotNode N (firstCslotOf (.zombie ptr bits (n + 1))) := + notNode_of_cov (w := uV σ) hσ.1 hcs (by simp [firstCslotOf, covR, capRng, objRefs]) + have h2 : NotNode N s := notNode_of_held (w := uV σ) hσ.2 hcs (not_ntfnOrNull_zombie _ _ _) + refine (show IKE (NO N) (recDelFG Fn f (.reduceZombieCall (.zombie ptr bits (n + 1)) s false)) by + simp only [recDelFG, withoutPreemption] + repeat' (first | exact no_capSwapForDelete h1 h2 | ik_step0 | ik_unfold)) σ hσ y hy + | true => + intro σ hσ y hy + replace hσ := hσ.1 + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨es, hes, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hes + try dsimp only at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + have hp : NO N z.2 := hrec (.cTEDeleteCall _ _) σ hσ z hz + rcases H with ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩ + swap + · exact hp + obtain ⟨z2, hz2, ⟨nc, hnc, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + swap + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + obtain ⟨⟨nc', σ5⟩, hx5, rfl⟩ := mem_liftE' hz2 + cases hnc + obtain ⟨e5, hc5⟩ := getCap_capAt hx5 + simp only at e5 hc5 + subst e5 + try dsimp only at hy + split at hy + · rename_i hnz + subst hnz + obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + rw [mem_ret_ok hy] + have hv := hv_setCap (.zombie ptr bits n) s (uV z.2) z.2 rfl _ hx + show IrqOutV N (uV x.2) ∧ NOV N (uV x.2) + rw [hv] + exact ⟨irqOut_vCap hp.1 (covBy_of hc5 (fun q hq => by simpa [covR, capRng, objRefs] using hq)) s, + nov_vCap hp.2 (Or.inl (notNode_of_held (w := uV z.2) hp.2 hc5 (not_ntfnOrNull_zombie _ _ _)))⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + obtain ⟨-, rfl⟩ := hx + rw [mem_ret_ok hy] + exact hp + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | _ => intro σ _ y hy; exact (rz_failG rfl hy).elim + +theorem no_recDelG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NO N) (Fn c b)) + (hR : ∀ {w : UView}, NOV N w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotNode N sl ∨ NtfnOrNull x.1.1) (k : RecDelCall) : HR (NPre N k) (fun _ => NO N) (recDelG Fn k) := by + unfold recDelG + exact hr_lfp (NPre N) (fun _ _ => NO N) (fun f hf k => no_recDelFG hF hP hR f hf k) k + +theorem sg_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NO N) (Fn c b)) + (hR : ∀ {w : UView}, NOV N w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotNode N sl ∨ NtfnOrNull x.1.1) + (hIO : ∀ (K : Irq → ObjRef) c b, IK (IO K) (Fn c b)) {M : Irq → ObjRef} (slot : CslotPtr) : IKE (S N M) (capDeleteG Fn slot) := by + unfold capDeleteG + refine ike_bind (s_of (fun K => p3_of (ikN_recDel_cteG hF _ _) (io_recDelG hF (hIO K) _)) + fun σ h y hy => (no_recDelG hF hP hR (.cTEDeleteCall slot true) σ ⟨h.1.2, h.2.2⟩ y hy).2) fun _ => ike_returnOk _ + +theorem sg_capRevokeFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NO N) (Fn c b)) + (hR : ∀ {w : UView}, NOV N w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotNode N sl ∨ NtfnOrNull x.1.1) + (hIO : ∀ (K : Irq → ObjRef) c b, IK (IO K) (Fn c b)) {M : Irq → ObjRef} + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IK (S N M) (f k)) (sl : CslotPtr) : IKE (S N M) (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKE (S N M) (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + sg_auto + all_goals first | exact hrec' _ | exact sg_capDeleteG hF hP hR hIO _ | apply sg_capDeleteG hF hP hR hIO + +theorem sg_capRevokeG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NO N) (Fn c b)) + (hR : ∀ {w : UView}, NOV N w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotNode N sl ∨ NtfnOrNull x.1.1) + (hIO : ∀ (K : Irq → ObjRef) c b, IK (IO K) (Fn c b)) {M : Irq → ObjRef} + (sl : CslotPtr) : IKE (S N M) (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => sg_capRevokeFG hF hP hR hIO hf k) sl + +end + +end Fl4Proofs.P3.SInvG diff --git a/proofs/Fl4Proofs/P3/VmFinGenLSi.lean b/proofs/Fl4Proofs/P3/VmFinGenLSi.lean new file mode 100644 index 000000000..4a144fd88 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenLSi.lean @@ -0,0 +1,204 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvSiA's `ni_recDelF` and its tops over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`), the finaliser's facts as hypotheses. +Written by `handoff/tools/gen_finlong.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvSiA +import Fl4Proofs.P3.VmFinGenBase +import Fl4Proofs.P3.VmFinGenLIo +import Fl4Proofs.P3.SpecInvSiG +open Classical + +namespace Fl4Proofs.P3.SInvIV +open Fl4Spec Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU Fl4Proofs.P3.SInvG +noncomputable section +set_option maxHeartbeats 1000000 +variable {N : Irq → ObjRef} {t : ObjRef} + +open Fl4Proofs.P3.VmFin + +theorem ni_recDelFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NI t) (Fn c b)) + (hR : ∀ {w : UView}, IVV t w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotIV t sl ∨ x.1.1 = .nullCap) + (f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))) + (hrec : ∀ k, HR (IPre t k) (fun _ => NI t) (f k)) (k : RecDelCall) : + HR (IPre t k) (fun _ => NI t) (recDelFG Fn f k) := by + have hfin : ∀ s e, IKE (ε := Unit) (NI t) (f (.finaliseSlotCall s e) : NonDetE (State Unit) Unit (Bool × Cap)) := + fun s e σ h y hy => hrec (.finaliseSlotCall s e) σ h y hy + have hcte : ∀ s e, IKE (ε := Unit) (NI t) (f (.cTEDeleteCall s e) : NonDetE (State Unit) Unit (Bool × Cap)) := + fun s e σ h y hy => hrec (.cTEDeleteCall s e) σ h y hy + cases k with + | cTEDeleteCall s e => + intro σ hσ y hy + replace hσ : NI t σ := hσ + refine (show IKE (NI t) (recDelFG Fn f (.cTEDeleteCall s e)) by + unfold recDelFG withoutPreemption; ni0_auto + all_goals first | exact hfin _ _ | exact hcte _ _) σ hσ y hy + | finaliseSlotCall sl e => + intro σ hσ y hy + replace hσ : NI t σ := hσ + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + obtain ⟨⟨cap, σ1⟩, hx, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy1⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨e1, hc⟩ := getCap_capAt hx + simp only at e1 hc + subst e1 + try dsimp only at hy1 + split at hy1 + · rw [mem_ret_ok hy1]; exact hσ + · obtain ⟨z, hz, H⟩ := mem_bindE' hy1 + obtain ⟨⟨fin, σ2⟩, hx2, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy2⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rfl, rfl⟩ := Prod.mk.inj (Set.mem_singleton_iff.1 hx2) + try dsimp only at hy2 + obtain ⟨z, hz, H⟩ := mem_bindE' hy2 + obtain ⟨⟨rc, σ3⟩, hx3, rfl⟩ := mem_liftE' hz + obtain ⟨a, ha, hy3⟩ | ⟨er, her, -⟩ := H + swap + · cases her + injection ha with ha' + subst ha' + obtain ⟨rem, cl⟩ := rc + have hO3 : NI t σ3 := hP cap _ _ hσ _ hx3 + have hc3 : SInvU.capAt σ3 sl = some cap := hF.keep hc _ _ hx3 + have hcov : covR rem ⊆ covR cap := hF.cov cap _ _ _ hx3 + have hrem : NotIV t sl ∨ rem = .nullCap := hR hσ.2 hc hx3 + have hO4 : NI t σ3 → IrqOutV (fun _ => t) (vCap sl rem (uV σ3)) ∧ IVV t (vCap sl rem (uV σ3)) := fun h => + ⟨irqOut_vCap h.1 (covBy_of hc3 hcov) sl, iv_vCap h.2 hrem⟩ + try dsimp only at hy3 + split at hy3 + · rw [mem_ret_ok hy3]; exact hO3 + · split at hy3 + · obtain ⟨z, hz, ⟨u, -, hy4⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy3 + · obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + rw [mem_ret_ok hy4] + have hv4 : uV σ4 = vCap sl rem (uV σ3) := hv_setCap rem sl (uV σ3) σ3 rfl _ hx4 + show IrqOutV (fun _ => t) (uV σ4) ∧ IVV t (uV σ4); rw [hv4]; exact hO4 hO3 + · obtain ⟨x, -, hx⟩ := mem_liftE' hz + rw [hx] at her; cases her + · obtain ⟨z, hz, H⟩ := mem_bindE' hy3 + obtain ⟨⟨u', σ4⟩, hx4, rfl⟩ := mem_liftE' hz + obtain ⟨a, -, hy4⟩ | ⟨er, her, -⟩ := H + swap + · cases her + have hv4 : uV σ4 = vCap sl rem (uV σ3) := hv_setCap rem sl (uV σ3) σ3 rfl _ hx4 + have hO5 : NI t σ4 := by show IrqOutV (fun _ => t) (uV σ4) ∧ IVV t (uV σ4); rw [hv4]; exact hO4 hO3 + have hc4 : SInvU.capAt σ4 sl = some rem := by + show (uV σ4).2.1 sl = some rem; rw [hv4, vCap_cap, if_pos rfl] + obtain ⟨z, hz, H⟩ := mem_bindE' hy4 + have hI5 : NI t z.2 := hrec (.reduceZombieCall rem sl e) σ4 ⟨hO5, hc4⟩ z hz + obtain ⟨a, -, hy5⟩ | ⟨er, -, rfl⟩ := H + · obtain ⟨z6, hz6, H⟩ := mem_bindE' hy5 + have hv6 : uV z6.2 = uV z.2 := uk_preemptionPoint (uvw := uV z.2) z.2 rfl z6 hz6 + have hI6 : NI t z6.2 := by show IrqOutV (fun _ => t) (uV z6.2) ∧ IVV t (uV z6.2); rw [hv6]; exact hI5 + obtain ⟨a, -, hy6⟩ | ⟨er, -, rfl⟩ := H + · exact hrec (.finaliseSlotCall sl e) z6.2 hI6 y hy6 + · exact hI6 + · exact hI5 + | reduceZombieCall c s e => + cases c with + | zombie ptr bits n => + cases n with + | zero => intro σ _ y hy; exact (rz_failG rfl hy).elim + | succ n => + cases e with + | false => + intro σ hσ y hy + obtain ⟨hσ, hcs⟩ := hσ + have h1 : NotIV t (firstCslotOf (.zombie ptr bits (n + 1))) := + notIV_of_cov (w := uV σ) hσ.1 hcs (by simp [firstCslotOf, covR, capRng, objRefs]) + have h2 : NotIV t s := notIV_of_held (w := uV σ) hσ.2 hcs (fun e => by cases e) + refine (show IKE (NI t) (recDelFG Fn f (.reduceZombieCall (.zombie ptr bits (n + 1)) s false)) by + simp only [recDelFG, withoutPreemption] + repeat' (first | exact ni_capSwapForDelete h1 h2 | ik_step0 | ik_unfold)) σ hσ y hy + | true => + intro σ hσ y hy + replace hσ := hσ.1 + simp only [recDelFG, withoutPreemption] at hy + obtain ⟨z, hz, ⟨es, hes, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain rfl := mem_ret_ok hz + cases hes + try dsimp only at hy + obtain ⟨z, hz, H⟩ := mem_bindE' hy + have hp : NI t z.2 := hrec (.cTEDeleteCall _ _) σ hσ z hz + rcases H with ⟨a, ha, hy⟩ | ⟨er, -, rfl⟩ + swap + · exact hp + obtain ⟨z2, hz2, ⟨nc, hnc, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + swap + · obtain ⟨x, -, hx⟩ := mem_liftE' hz2 + rw [hx] at her; cases her + obtain ⟨⟨nc', σ5⟩, hx5, rfl⟩ := mem_liftE' hz2 + cases hnc + obtain ⟨e5, hc5⟩ := getCap_capAt hx5 + simp only at e5 hc5 + subst e5 + try dsimp only at hy + split at hy + · rename_i hnz + subst hnz + obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + rw [mem_ret_ok hy] + have hv := hv_setCap (.zombie ptr bits n) s (uV z.2) z.2 rfl _ hx + show IrqOutV (fun _ => t) (uV x.2) ∧ IVV t (uV x.2) + rw [hv] + exact ⟨irqOut_vCap hp.1 (covBy_of hc5 (fun q hq => by simpa [covR, capRng, objRefs] using hq)) s, + iv_vCap hp.2 (Or.inl (notIV_of_held (w := uV z.2) hp.2 hc5 (fun e => by cases e)))⟩ + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain ⟨z3, hz3, ⟨u, -, hy⟩ | ⟨er, her, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨x, hx, rfl⟩ := mem_liftE' hz3 + obtain ⟨-, rfl⟩ := hx + rw [mem_ret_ok hy] + exact hp + · obtain ⟨x, -, hx⟩ := mem_liftE' hz3 + rw [hx] at her; cases her + · obtain rfl := mem_ret_ok hz; cases her + | _ => intro σ _ y hy; exact (rz_failG rfl hy).elim + +theorem ni_recDelG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NI t) (Fn c b)) + (hR : ∀ {w : UView}, IVV t w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotIV t sl ∨ x.1.1 = .nullCap) (k : RecDelCall) : HR (IPre t k) (fun _ => NI t) (recDelG Fn k) := by + unfold recDelG + exact hr_lfp (IPre t) (fun _ _ => NI t) (fun f hf k => ni_recDelFG hF hP hR f hf k) k + +theorem iw_capDeleteG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NI t) (Fn c b)) + (hR : ∀ {w : UView}, IVV t w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotIV t sl ∨ x.1.1 = .nullCap) + (hIO : ∀ (K : Irq → ObjRef) c b, IK (IO K) (Fn c b)) (slot : CslotPtr) : IKE (SI N t) (capDeleteG Fn slot) := by + unfold capDeleteG + refine ike_bind (iw_of (fun K => p3_of (ikN_recDel_cteG hF _ _) (io_recDelG hF (hIO K) _)) + fun σ h y hy => (ni_recDelG hF hP hR (.cTEDeleteCall slot true) σ ⟨h.2.1, h.2.2⟩ y hy).2) fun _ => ike_returnOk _ + +theorem iw_capRevokeFG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NI t) (Fn c b)) + (hR : ∀ {w : UView}, IVV t w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotIV t sl ∨ x.1.1 = .nullCap) + (hIO : ∀ (K : Irq → ObjRef) c b, IK (IO K) (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IK (SI N t) (f k)) (sl : CslotPtr) : IKE (SI N t) (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKE (SI N t) (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + iw_auto + all_goals first | exact hrec' _ | exact iw_capDeleteG hF hP hR hIO _ | apply iw_capDeleteG hF hP hR hIO + +theorem iw_capRevokeG {Fn : FinFn} (hF : FinFacts Fn) (hP : ∀ c b, IK (NI t) (Fn c b)) + (hR : ∀ {w : UView}, IVV t w → ∀ {sl : CslotPtr} {cap : Cap}, w.2.1 sl = some cap → ∀ {σ : State Unit} + {x : (Cap × Cap) × State Unit} {fin : Bool}, x ∈ (Fn cap fin σ).1 → NotIV t sl ∨ x.1.1 = .nullCap) + (hIO : ∀ (K : Irq → ObjRef) c b, IK (IO K) (Fn c b)) + (sl : CslotPtr) : IKE (SI N t) (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => iw_capRevokeFG hF hP hR hIO hf k) sl + +end + +end Fl4Proofs.P3.SInvIV diff --git a/proofs/Fl4Proofs/P3/VmFinGenNk.lean b/proofs/Fl4Proofs/P3/VmFinGenNk.lean new file mode 100644 index 000000000..a98201955 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenNk.lean @@ -0,0 +1,55 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvUtNK3's deletion-loop lemmas (`nk_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtNK3 +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 +variable {N : Irq → ObjRef} + +theorem nk_recDelFG {Fn : FinFn} (hFn : ∀ c b, NK N (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, NK N (f k)) (k : RecDelCall) : NKE N (recDelFG Fn f k) := by + have hrec' : ∀ k, NKE N (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; nk_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; nk_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> nk_auto + all_goals first | exact hrec' _ | apply hFn + +theorem nk_recDelG {Fn : FinFn} (hFn : ∀ c b, NK N (Fn c b)) + (c : RecDelCall) : NKE N (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => nk_recDelFG hFn hf k) c + +theorem nk_capDeleteG {Fn : FinFn} (hFn : ∀ c b, NK N (Fn c b)) + (slot : CslotPtr) : NKE N (capDeleteG Fn slot) := by + unfold capDeleteG; nk_auto + all_goals first | exact nk_recDelG hFn _ | apply nk_recDelG hFn + +theorem nk_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, NK N (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, NK N (f k)) (sl : CslotPtr) : NKE N (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, NKE N (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + nk_auto + all_goals first | exact hrec' _ | exact nk_capDeleteG hFn _ | apply nk_capDeleteG hFn + +theorem nk_capRevokeG {Fn : FinFn} (hFn : ∀ c b, NK N (Fn c b)) + (sl : CslotPtr) : NKE N (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => nk_capRevokeFG hFn hf k) sl + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinGenNq.lean b/proofs/Fl4Proofs/P3/VmFinGenNq.lean new file mode 100644 index 000000000..d04a57162 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenNq.lean @@ -0,0 +1,55 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvNtfnQG's deletion-loop lemmas (`nq_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvNtfnQG +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 +variable {Kq : List ObjRef} + +theorem nq_recDelFG {Fn : FinFn} (hFn : ∀ c b, NQK Kq (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, NQK Kq (f k)) (k : RecDelCall) : NQKE Kq (recDelFG Fn f k) := by + have hrec' : ∀ k, NQKE Kq (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; nq_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; nq_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> nq_auto + all_goals first | exact hrec' _ | apply hFn + +theorem nq_recDelG {Fn : FinFn} (hFn : ∀ c b, NQK Kq (Fn c b)) + (c : RecDelCall) : NQKE Kq (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => nq_recDelFG hFn hf k) c + +theorem nq_capDeleteG {Fn : FinFn} (hFn : ∀ c b, NQK Kq (Fn c b)) + (slot : CslotPtr) : NQKE Kq (capDeleteG Fn slot) := by + unfold capDeleteG; nq2_auto + all_goals first | exact nq_recDelG hFn _ | apply nq_recDelG hFn + +theorem nq_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, NQK Kq (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, NQK Kq (f k)) (sl : CslotPtr) : NQKE Kq (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, NQKE Kq (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + nq_auto + all_goals first | exact hrec' _ | exact nq_capDeleteG hFn _ | apply nq_capDeleteG hFn + +theorem nq_capRevokeG {Fn : FinFn} (hFn : ∀ c b, NQK Kq (Fn c b)) + (sl : CslotPtr) : NQKE Kq (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => nq_capRevokeFG hFn hf k) sl + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinGenPj.lean b/proofs/Fl4Proofs/P3/VmFinGenPj.lean new file mode 100644 index 000000000..0f617028f --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenPj.lean @@ -0,0 +1,42 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvNtfnQP2's deletion-loop lemmas (`pj_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvNtfnQP2 +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 +variable {Lp : List ObjRef} + +theorem pj_recDelFG {Fn : FinFn} (hFn : ∀ c b, PJK Lp (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, PJK Lp (f k)) (k : RecDelCall) : PJKE Lp (recDelFG Fn f k) := by + have hrec' : ∀ k, PJKE Lp (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; pj_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; pj_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> pj_auto + all_goals first | exact hrec' _ | apply hFn + +theorem pj_recDelG {Fn : FinFn} (hFn : ∀ c b, PJK Lp (Fn c b)) + (c : RecDelCall) : PJKE Lp (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => pj_recDelFG hFn hf k) c + +theorem pj_capDeleteG {Fn : FinFn} (hFn : ∀ c b, PJK Lp (Fn c b)) + (slot : CslotPtr) : PJKE Lp (capDeleteG Fn slot) := by + unfold capDeleteG; pj_auto + all_goals first | exact pj_recDelG hFn _ | apply pj_recDelG hFn + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmFinGenUi.lean b/proofs/Fl4Proofs/P3/VmFinGenUi.lean new file mode 100644 index 000000000..c8f3b7009 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenUi.lean @@ -0,0 +1,72 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvUtG's deletion-loop lemmas (`ui_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtG +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 + +theorem ui_recDelFG {Fn : FinFn} (hFn : ∀ c b, IK UtInv' (Fn c b)) (hNu : ∀ c b, OV RemNU (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, IK UtInv' (f k)) (k : RecDelCall) : IKE UtInv' (recDelFG Fn f k) := by + have hrec' : ∀ k, IKE UtInv' (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; ui_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => + unfold recDelFG withoutPreemption + refine ike_bind (ike_liftE (by ui_auto)) fun cap => ?_ + refine ike_ite _ (ike_returnOk _) ?_ + refine ike_bind (ike_liftE (by ui_auto)) fun isFinal => ?_ + refine ike_bind_v (Q := RemNU) (ike_liftE (hFn _ _)) (fun σ x hx a ha => ?_) fun r hr => ?_ + · obtain ⟨y, hy, rfl⟩ := mem_liftE' hx + simp only at ha + injection ha with e + subst e + exact hNu _ _ σ y hy + obtain ⟨rem, cl⟩ := r + dsimp only + by_cases h1 : capRemoveable rem sl + · rw [if_pos h1]; exact ike_returnOk _ + · rw [if_neg h1] + have hnn : NNU rem := And.intro (fun e => h1 (by subst e; trivial)) hr + ui_auto + all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> ui_auto + all_goals first | exact hrec' _ | apply hFn + +theorem ui_recDelG {Fn : FinFn} (hFn : ∀ c b, IK UtInv' (Fn c b)) (hNu : ∀ c b, OV RemNU (Fn c b)) + (c : RecDelCall) : IKE UtInv' (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => ui_recDelFG hFn hNu hf k) c + +theorem ui_capDeleteG {Fn : FinFn} (hFn : ∀ c b, IK UtInv' (Fn c b)) (hNu : ∀ c b, OV RemNU (Fn c b)) + (slot : CslotPtr) : IKE UtInv' (capDeleteG Fn slot) := by + unfold capDeleteG; ui_auto + all_goals first | exact ui_recDelG hFn hNu _ | apply ui_recDelG hFn hNu + +theorem ui_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, IK UtInv' (Fn c b)) (hNu : ∀ c b, OV RemNU (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, IK UtInv' (f k)) (sl : CslotPtr) : IKE UtInv' (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, IKE UtInv' (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + ui_auto + all_goals first | exact hrec' _ | exact ui_capDeleteG hFn hNu _ | apply ui_capDeleteG hFn hNu + +theorem ui_capRevokeG {Fn : FinFn} (hFn : ∀ c b, IK UtInv' (Fn c b)) (hNu : ∀ c b, OV RemNU (Fn c b)) + (sl : CslotPtr) : IKE UtInv' (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => ui_capRevokeFG hFn hNu hf k) sl + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinGenVF.lean b/proofs/Fl4Proofs/P3/VmFinGenVF.lean new file mode 100644 index 000000000..2345b3efa --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenVF.lean @@ -0,0 +1,148 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): the long families' finaliser facts at `finaliseCapVF`, and the generic +transfer from `finaliseCap` to `finaliseCapVF` for a predicate the three hooks keep (spec only). No named premise. +-/ +import Fl4Proofs.P3.VmFinFamHook +import Fl4Proofs.P3.VmFinGenLSi +import Fl4Proofs.P3.VmFinGenLSg +import Fl4Proofs.P3.VmFinGenLLd +import Fl4Proofs.P3.VmFinGenLFo +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem isFinalCap'_capAt {σ σ' : State Unit} (h : SInvU.capAt σ' = SInvU.capAt σ) (c : Cap) : + isFinalCap' (Z := Unit) c σ' ↔ isFinalCap' (Z := Unit) c σ := by + have e : {cref | ∃ cap', ((getCap cref : SMonad Unit Cap) σ').1 = {(cap', σ')} ∧ + genObjRefs c ∩ genObjRefs cap' ≠ ∅} + = {cref | ∃ cap', ((getCap cref : SMonad Unit Cap) σ).1 = {(cap', σ)} ∧ + genObjRefs c ∩ genObjRefs cap' ≠ ∅} := by + ext x + simp only [Set.mem_setOf_eq, getCap_set_capAt, h] + unfold isFinalCap' + rw [e] + +theorem sRefs_directNull {c : Cap} (h : DirectNull c) : sRefs c = ∅ := by + rcases h with ⟨_, rfl⟩ | ⟨_, _, _, _, rfl⟩ | ⟨_, _, _, _, _, _, rfl⟩ <;> rfl + +theorem finFacts_VF : FinFacts (finaliseCapVF (Z := Unit)) where + keep := by + intro σ s c h b y hy + obtain ⟨σ1, hh, hy | ⟨rfl, -⟩⟩ := finaliseCapVF_cases c b σ y hy + · exact finaliseCap_keepAt (by rw [hh.capAt]; exact h) b y hy + · rw [hh.capAt]; exact h + cov := by + intro c b σ y hy + obtain ⟨σ1, -, hy | ⟨rfl, -⟩⟩ := finaliseCapVF_cases c b σ y hy + · exact finaliseCap_cov c b σ1 y hy + · show covR .nullCap ⊆ _ + rw [covR_null]; exact Set.empty_subset _ + step := by + intro σ hI s cap hc hnn y hy + obtain ⟨σ1, hh, hy | ⟨rfl, hd⟩⟩ := finaliseCapVF_cases cap _ σ y hy + all_goals + have hv := hh.uv + have hI1 : UtInv'' σ1 := by show UtInv''V (uV σ1); rw [hv]; exact hI + have hc1 : SInvU.capAt σ1 s = some cap := by rw [hh.capAt]; exact hc + · have e : decide (isFinalCap' (Z := Unit) cap σ) = decide (isFinalCap' (Z := Unit) cap σ1) := + decide_eq_decide.2 (isFinalCap'_capAt hh.capAt cap).symm + rw [e] at hy + exact SInvU.fin_step hI1 hc1 hnn hy + · exact ⟨hI1, hc1, fun _ => safeDel_of_sRefs (sRefs_directNull hd), fun h => absurd trivial h⟩ + +/-- A predicate kept by `finaliseCap` and the three hooks is kept by `finaliseCapVF`. -/ +theorem ik_finaliseCapVF {P : State Unit → Prop} (hd : ∀ t, IK P (vcpuDetachF (Z := Unit) t)) + (hg : ∀ a g p l tb, IK P (guestUnmap (Z := Unit) a g p l tb)) (hi : ∀ c, IK P (ioPortRevoke (Z := Unit) c)) + (hf : ∀ c b, IK P (finaliseCap (Z := Unit) c b)) (c : Cap) (b : Bool) : IK P (finaliseCapVF (Z := Unit) c b) := by + intro σ hσ y hy + obtain ⟨σ1, hh, hy | ⟨rfl, -⟩⟩ := finaliseCapVF_cases c b σ y hy + · exact hf c b σ1 (hh.keep hd hg hi hσ) y hy + · exact hh.keep hd hg hi hσ + +/-- A predicate of the untyped view is kept by `finaliseCapVF` when `finaliseCap` keeps it. -/ +theorem ik_finaliseCapVF_uv {P : UView → Prop} (hf : ∀ c b, IK (fun σ => P (uV σ)) (finaliseCap (Z := Unit) c b)) + (c : Cap) (b : Bool) : IK (fun σ => P (uV σ)) (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => ik_of_uk (fun _ _ e h => by rw [e]; exact h) fun _ => uk_vcpuDetachF t) + (fun _ _ _ _ _ => ik_of_uk (fun _ _ e h => by rw [e]; exact h) fun _ => uk_guestUnmap _ _ _ _ _) + (fun c => ik_of_uk (fun _ _ e h => by rw [e]; exact h) fun _ => uk_ioPortRevoke c) hf c b + +/-- A result fact of `finaliseCap` that `(nullCap, nullCap)` has. -/ +theorem ov_finaliseCapVF {Q : Cap × Cap → Prop} (hf : ∀ c b σ x, x ∈ (finaliseCap (Z := Unit) c b σ).1 → Q x.1) + (h0 : Q (.nullCap, .nullCap)) (c : Cap) (b : Bool) (σ : State Unit) (x : (Cap × Cap) × State Unit) + (hx : x ∈ (finaliseCapVF (Z := Unit) c b σ).1) : Q x.1 := by + obtain ⟨σ1, -, hx | ⟨rfl, -⟩⟩ := finaliseCapVF_cases c b σ x hx + · exact hf c b σ1 x hx + · exact h0 + +theorem iv_fin_remVF {t : ObjRef} {w : UView} (h : SInvIV.IVV t w) {sl : CslotPtr} {cap : Cap} + (hc : w.2.1 sl = some cap) {σ : State Unit} {x : (Cap × Cap) × State Unit} {fin : Bool} + (hx : x ∈ (finaliseCapVF (Z := Unit) cap fin σ).1) : SInvIV.NotIV t sl ∨ x.1.1 = .nullCap := by + obtain ⟨σ1, -, hx | ⟨rfl, -⟩⟩ := finaliseCapVF_cases cap fin σ x hx + · exact SInvIV.iv_fin_rem h hc hx + · exact Or.inr rfl + +theorem fin_remVF {N : Irq → ObjRef} {w : UView} (h : SInvG.NOV N w) {sl : CslotPtr} {cap : Cap} + (hc : w.2.1 sl = some cap) {σ : State Unit} {x : (Cap × Cap) × State Unit} {fin : Bool} + (hx : x ∈ (finaliseCapVF (Z := Unit) cap fin σ).1) : SInvG.NotNode N sl ∨ SInvG.NtfnOrNull x.1.1 := by + obtain ⟨σ1, -, hx | ⟨rfl, -⟩⟩ := finaliseCapVF_cases cap fin σ x hx + · exact SInvG.fin_rem h hc hx + · exact Or.inr (Or.inl rfl) + +/-- The finalisation at the hooked state, its `isFinal` flag read there. -/ +theorem finVF_at {σ : State Unit} {s : CslotPtr} {cap : Cap} (hc : SInvU.capAt σ s = some cap) + {y : (Cap × Cap) × State Unit} + (hy : y ∈ (finaliseCapVF (Z := Unit) cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1) : + ∃ σ1, HookStep σ σ1 ∧ SInvU.capAt σ1 s = some cap ∧ + (y ∈ (finaliseCap (Z := Unit) cap (decide (isFinalCap' (Z := Unit) cap σ1)) σ1).1 ∨ + (y = ((.nullCap, .nullCap), σ1) ∧ DirectNull cap)) := by + obtain ⟨σ1, hh, h⟩ := finaliseCapVF_cases _ _ σ y hy + refine ⟨σ1, hh, by rw [hh.capAt]; exact hc, h.imp_left fun h => ?_⟩ + rw [← decide_eq_decide.2 (isFinalCap'_capAt hh.capAt cap)] at h + exact h + +theorem directNull_not_thread {cap : Cap} (h : DirectNull cap) (t : ObjRef) : cap ≠ .threadCap t := by + rcases h with ⟨_, rfl⟩ | ⟨_, _, _, _, rfl⟩ | ⟨_, _, _, _, _, _, rfl⟩ <;> exact fun h => by cases h + +theorem tcs_remVF {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit} + (hy : y ∈ (finaliseCapVF (Z := Unit) cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1) {s : CslotPtr} + (hc3 : SInvU.capAt y.2 s = some cap) (hrm : ¬ capRemoveable y.1.1 s) : TcSafe [] y.2 s y.1.1 := by + obtain ⟨σ1, hh, h⟩ := finaliseCapVF_cases _ _ σ y hy + rcases h with h | ⟨rfl, -⟩ + · rw [← decide_eq_decide.2 (isFinalCap'_capAt hh.capAt cap)] at h + exact SInv.tcs_rem h hc3 hrm + · exact absurd trivial hrm + +theorem tcs_removableVF {σ : State Unit} (hI : UtInv'' σ) {s : CslotPtr} {cap : Cap} + (hc : SInvU.capAt σ s = some cap) {y : (Cap × Cap) × State Unit} + (hy : y ∈ (finaliseCapVF (Z := Unit) cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1) + (hc3 : SInvU.capAt y.2 s = some cap) (hrm : capRemoveable y.1.1 s) : TcSafe [] y.2 s .nullCap := by + obtain ⟨σ1, hh, hc1, h | ⟨rfl, hd⟩⟩ := finVF_at hc hy + · exact SInv.tcs_removable (by show UtInv''V (uV σ1); rw [hh.uv]; exact hI) hc1 h hc3 hrm + · exact tcSafe_of_not fun t e => directNull_not_thread hd t (Option.some.inj (hc3.symm.trans e)) + +theorem fo_tcs_remVF {σ : State Unit} {cap : Cap} {y : (Cap × Cap) × State Unit} + (hy : y ∈ (finaliseCapVF (Z := Unit) cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1) {s : CslotPtr} + (hc3 : SInvU.capAt y.2 s = some cap) (hrm : ¬ capRemoveable y.1.1 s) : + TcSafe (SInvF.oL y.2 ++ []) y.2 s y.1.1 := by + obtain ⟨σ1, hh, h⟩ := finaliseCapVF_cases _ _ σ y hy + rcases h with h | ⟨rfl, -⟩ + · rw [← decide_eq_decide.2 (isFinalCap'_capAt hh.capAt cap)] at h + exact SInvF.fo_tcs_rem h hc3 hrm + · exact absurd trivial hrm + +theorem fo_tcs_removableVF {K : List ObjRef} {σ : State Unit} (hI : UtInv'' σ) {s : CslotPtr} {cap : Cap} + (hc : SInvU.capAt σ s = some cap) {y : (Cap × Cap) × State Unit} + (hy : y ∈ (finaliseCapVF (Z := Unit) cap (decide (isFinalCap' (Z := Unit) cap σ)) σ).1) + (hc3 : SInvU.capAt y.2 s = some cap) (hrm : capRemoveable y.1.1 s) : TcSafe K y.2 s .nullCap := by + obtain ⟨σ1, hh, hc1, h | ⟨rfl, hd⟩⟩ := finVF_at hc hy + · exact SInvF.fo_tcs_removable (by show UtInv''V (uV σ1); rw [hh.uv]; exact hI) hc1 h hc3 hrm + · exact tcSafe_of_not fun t e => directNull_not_thread hd t (Option.some.inj (hc3.symm.trans e)) + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinGenYk.lean b/proofs/Fl4Proofs/P3/VmFinGenYk.lean new file mode 100644 index 000000000..1ac839513 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinGenYk.lean @@ -0,0 +1,55 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families): P3/SpecInvUtZK3's deletion-loop lemmas (`yk_*`) over a finaliser `Fn` +(`recDelFG` / `capDeleteG` / `capRevokeG`). The proofs are the native ones with `finaliseCap`'s facts as hypotheses. +Written by `handoff/tools/gen_fingen.py`. No named premise. +-/ +import Fl4Proofs.P3.SpecInvUtZK3 +import Fl4Proofs.P3.CnodeRevoke +import Fl4Proofs.P3.DelSpec +open Classical + +namespace Fl4Proofs.P3.SInvUZ +open Fl4Spec Fl4Proofs.P3.SInv +noncomputable section +set_option maxHeartbeats 1000000 +set_option maxErrors 2000 +variable {PD : PgSet} {FS : Set Nat} + +theorem yk_recDelFG {Fn : FinFn} (hFn : ∀ c b, YK PD FS (Fn c b)) + {f : RecDelCall → NonDet (State Unit) (Except Unit (Bool × Cap))} + (hrec : ∀ k, YK PD FS (f k)) (k : RecDelCall) : YKE PD FS (recDelFG Fn f k) := by + have hrec' : ∀ k, YKE PD FS (ε := Unit) (f k : NonDetE (State Unit) Unit (Bool × Cap)) := hrec + cases k with + | cTEDeleteCall sl e => unfold recDelFG withoutPreemption; yk_auto; all_goals first | exact hrec' _ | apply hFn + | finaliseSlotCall sl e => unfold recDelFG withoutPreemption; yk_auto; all_goals first | exact hrec' _ | apply hFn + | reduceZombieCall c sl e => + unfold recDelFG withoutPreemption + split <;> yk_auto + all_goals first | exact hrec' _ | apply hFn + +theorem yk_recDelG {Fn : FinFn} (hFn : ∀ c b, YK PD FS (Fn c b)) + (c : RecDelCall) : YKE PD FS (recDelG Fn c) := by + unfold recDelG + exact ik_lfp (fun f hf k => yk_recDelFG hFn hf k) c + +theorem yk_capDeleteG {Fn : FinFn} (hFn : ∀ c b, YK PD FS (Fn c b)) + (slot : CslotPtr) : YKE PD FS (capDeleteG Fn slot) := by + unfold capDeleteG; yk_auto + all_goals first | exact yk_recDelG hFn _ | apply yk_recDelG hFn + +theorem yk_capRevokeFG {Fn : FinFn} (hFn : ∀ c b, YK PD FS (Fn c b)) + {f : CslotPtr → NonDet (State Unit) (Except Unit Unit)} + (hrec : ∀ k, YK PD FS (f k)) (sl : CslotPtr) : YKE PD FS (capRevokeFG (capDeleteG Fn) f sl) := by + have hrec' : ∀ k, YKE PD FS (ε := Unit) (f k : NonDetE (State Unit) Unit Unit) := hrec + unfold capRevokeFG withoutPreemption selectExt + yk_auto + all_goals first | exact hrec' _ | exact yk_capDeleteG hFn _ | apply yk_capDeleteG hFn + +theorem yk_capRevokeG {Fn : FinFn} (hFn : ∀ c b, YK PD FS (Fn c b)) + (sl : CslotPtr) : YKE PD FS (capRevokeG (capDeleteG Fn) sl) := by + unfold capRevokeG + exact ik_lfp (fun f hf k => yk_capRevokeFG hFn hf k) sl + +end + +end Fl4Proofs.P3.SInvUZ diff --git a/proofs/Fl4Proofs/P3/VmFinVF.lean b/proofs/Fl4Proofs/P3/VmFinVF.lean new file mode 100644 index 000000000..7f23e8d1b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinVF.lean @@ -0,0 +1,150 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (b)): the generalised deletion families at `finaliseCapVF` +(`capDeleteVF`, `capRevokeVF`). `vg_capDeleteVF` / `vg_capRevokeVF`: the state bundle `VG` (`SpecGlobS`, `IdleOutV`, +`UtInv3`, `IdleVtS`) is kept; `fu_capDeleteVF` / `fu_capRevokeVF`: `FU` (`FN []`, `UtInv''`) is kept; +`ilu_capDeleteVF` / `ilu_capRevokeVF`: `ILU`. No named premise. +-/ +import Fl4Proofs.P3.VmFinGenVF +import Fl4Proofs.P3.VmFinGenNk +import Fl4Proofs.P3.VmFinGenIdk +import Fl4Proofs.P3.VmFinFamFn +import Fl4Proofs.P3.DelVF +import Fl4Proofs.P3.SpecInvSiE +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.SInvU Fl4Proofs.P3.SInvG Fl4Proofs.P3.SInvIV + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-! ## The untyped-view families -/ + +theorem io_VF (K : Irq → ObjRef) (c : Cap) (b : Bool) : IK (IO K) (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF_uv (P := IrqOutV K) (fun c b => io_finaliseCap c b) c b + +theorem ni_VF (t : ObjRef) (c : Cap) (b : Bool) : IK (NI t) (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF_uv (P := fun w => IrqOutV (fun _ => t) w ∧ IVV t w) (fun c b => ni_finaliseCap c b) c b + +theorem no_VF (N : Irq → ObjRef) (c : Cap) (b : Bool) : IK (SInvG.NO N) (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF_uv (P := fun w => IrqOutV N w ∧ NOV N w) (fun c b => no_finaliseCap c b) c b + +theorem nk_VF (N : Irq → ObjRef) (c : Cap) (b : Bool) : SInvU.NK N (finaliseCapVF (Z := Unit) c b) := + fun _ h y hy => ((gk_finaliseCapVF c b).inode hy).trans h + +theorem idk_VF (N : ObjRef) (c : Cap) (b : Bool) : SInvG.IdK N (finaliseCapVF (Z := Unit) c b) := + fun _ h y hy => ((gk_finaliseCapVF c b).idle hy).trans h + +/-- The family's global bundle. -/ +def VG (σ : State Unit) : Prop := SpecGlobS σ ∧ IdleOutV σ ∧ UtInv3 σ ∧ IdleVtS σ + +theorem vg_step {α : Type} {m : NonDet (State Unit) α} (hsi : ∀ N t, IK (SI N t) m) + (hs : ∀ N M : Irq → ObjRef, IK (S N M) m) (hn : ∀ N, SInvU.NK N m) (hi : ∀ t, SInvG.IdK t m) : IK VG m := by + intro σ ⟨hG, hI, hU, hV⟩ y hy + have h1 := hsi _ _ σ (si_at hG hI hU hV) y hy + have h2 := hs _ _ σ (S_of_specGlob hG hI hU) y hy + have en : y.2.interruptIrqNode = σ.interruptIrqNode := hn _ σ rfl y hy + have et : y.2.idleThread = σ.idleThread := hi _ σ rfl y hy + rw [← en, ← et] at h1 h2 + obtain ⟨a, b⟩ := specGlob_of_S h2 + exact ⟨a, b, h2.1, h1.2.2⟩ + +theorem vg_capDeleteVF (slot : CslotPtr) : IK VG (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf] + exact vg_step (fun N t => iw_capDeleteG finFacts_VF (ni_VF t) iv_fin_remVF io_VF slot) + (fun N M => sg_capDeleteG finFacts_VF (no_VF N) fin_remVF io_VF slot) + (fun N => nk_capDeleteG (nk_VF N) slot) (fun t => idk_capDeleteG (idk_VF t) slot) + +theorem vg_capRevokeVF (slot : CslotPtr) : IK VG (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf] + exact vg_step (fun N t => iw_capRevokeG finFacts_VF (ni_VF t) iv_fin_remVF io_VF slot) + (fun N M => sg_capRevokeG finFacts_VF (no_VF N) fin_remVF io_VF slot) + (fun N => nk_capRevokeG (nk_VF N) slot) (fun t => idk_capRevokeG (idk_VF t) slot) + +/-! ## `FN` / `ILN` across the two remaining hooks -/ + +theorem ik_guestUnmapV {P : State Unit → Prop} + (hs : ∀ p (a : ArchKernelObj), IK P (setObject (Z := Unit) p (.archObj a))) + (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + IK P (guestUnmap (Z := Unit) asid gpa ptr level table) := by + unfold guestUnmap + refine ik_bind (ik_of_keepSt (keepSt_findGuestRoot _) _) fun r => ?_ + rcases r with _ | root + · exact ik_pure _ + refine ik_bind (ik_of_keepSt (keepSt_guestLookupSlot _ _ _) _) fun t => ?_ + rcases t with _ | tbl + · exact ik_pure _ + refine ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun e => ?_ + obtain ⟨_, es⟩ := e + dsimp only + refine ik_unlessV _ ?_ + unfold storeGuestEntry + refine ik_bind (ik_of_keepSt (keepSt_getEpt _) _) fun e => ?_ + obtain ⟨_, _⟩ := e + exact hs _ _ + +theorem ik_ioPortRevokeV {P : State Unit → Prop} (hu : ∀ v g, IK P (vcpuUpdate (Z := Unit) v g)) (c : ArchCap) : + IK P (ioPortRevoke (Z := Unit) c) := by + unfold ioPortRevoke + split + · refine ik_bind (ik_of_keepSt (fun σ x hx => by rw [Set.mem_singleton_iff.1 hx]) _) fun _ => ?_ + unfold NonDet.when + split + · exact hu _ _ + · exact ik_pure _ + · exact ik_pure _ + +theorem il_setArchV (K : List ObjRef) (p : ObjRef) (a : ArchKernelObj) : + ILK K (setObject (Z := Unit) p (.archObj a)) := by + refine il_setObject p _ (fun _ h => by cases h) fun old h => ?_ + cases old <;> first + | rfl + | (cases h; done) + | (simp only [aType] at h; (split at h) <;> cases h) + +theorem nq_setArchV (K : List ObjRef) (p : ObjRef) (a : ArchKernelObj) : + NQK K (setObject (Z := Unit) p (.archObj a)) := + nq_setObject p _ (fun _ h => by cases h) (fun _ h => by cases h) (fun _ h => by cases h) + (fun _ h => by simp only [aType] at h; cases h) + +theorem iln_setArchV (K : List ObjRef) (p : ObjRef) (a : ArchKernelObj) : + IK (ILN K) (setObject (Z := Unit) p (.archObj a)) := + iln_ik (il_setArchV K p a) (nq_setArchV [] p a) + +theorem iln_vcpuUpdateV (K : List ObjRef) (v : ObjRef) (g : VcpuState → VcpuState) : + IK (ILN K) (vcpuUpdate (Z := Unit) v g) := + iln_ik (il_vcpuUpdateV v g) (nq_vcpuUpdateV v g) + +theorem iln_VF (c : Cap) (b : Bool) : IK (ILN []) (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => iln_ik (il_vcpuDetachF t) (nq_vcpuDetachF t)) + (fun _ _ _ _ _ => ik_guestUnmapV (iln_setArchV []) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (iln_vcpuUpdateV []) c) (fun c b => iln_finaliseCap c b) c b + +theorem fn_VF (c : Cap) (b : Bool) : IK (SInvF.FN []) (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => SInvF.fn_vcpuDetachF t) + (fun _ _ _ _ _ => ik_guestUnmapV (fun p a => SInvF.fo_ikn (fun L => iln_setArchV (L ++ []) p a) + fun _ => SInvF.oi_setObject _ _) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (fun v g => SInvF.fo_ikn (fun L => iln_vcpuUpdateV (L ++ []) v g) + fun _ => SInvF.oif_vcpuUpdate v g) c) + (fun c b => SInvF.fo_finaliseCap c b) c b + +theorem ilu_capDeleteVF (slot : CslotPtr) : IKE ILU (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf] + exact ilu_capDeleteG finFacts_VF iln_VF tcs_remVF tcs_removableVF slot + +theorem ilu_capRevokeVF (slot : CslotPtr) : IKE ILU (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf] + exact ilu_capRevokeG finFacts_VF iln_VF tcs_remVF tcs_removableVF slot + +theorem fu_capDeleteVF (slot : CslotPtr) : IKE SInvF.FU (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf] + exact SInvF.fou_capDeleteG finFacts_VF fn_VF fo_tcs_remVF fo_tcs_removableVF slot + +theorem fu_capRevokeVF (slot : CslotPtr) : IKE SInvF.FU (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf] + exact SInvF.fou_capRevokeG finFacts_VF fn_VF fo_tcs_remVF fo_tcs_removableVF slot + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinVF2.lean b/proofs/Fl4Proofs/P3/VmFinVF2.lean new file mode 100644 index 000000000..fbadd699b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinVF2.lean @@ -0,0 +1,151 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (b) continued): the generalised deletion families `b` (`B L E K C`; +`B [] [] [] ⊤ ↔ FaultTcbsS ∧ NQB`), `pj`, `yk`, `hu`, `ct` and `nq` at `finaliseCapVF` (`capDeleteVF`, `capRevokeVF`). +Each instance is the generalised family's `capDeleteG` / `capRevokeG` lemma with the finaliser fact from +`ik_finaliseCapVF` (the native `finaliseCap` lemma and the three hook lemmas). No named premise. +-/ +import Fl4Proofs.P3.VmFinVF +import Fl4Proofs.P3.VmFinFamB +import Fl4Proofs.P3.VmFinGenB +import Fl4Proofs.P3.VmFinGenPj +import Fl4Proofs.P3.VmFinGenYk +import Fl4Proofs.P3.VmFinGenHu +import Fl4Proofs.P3.VmFinGenCt +import Fl4Proofs.P3.VmFinGenNq +open Classical + +namespace Fl4Proofs.P3.VmFin +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-! ## `B` (`FaultTcbsS` with `NQB`) -/ + +section B +variable {L E K : List ObjRef} {C : ObjRef → Prop} + +theorem b_VF (c : Cap) (b : Bool) : BK L E K C (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => b_vcpuDetachF t) (fun _ _ _ _ _ => ik_guestUnmapV (fun p a => b_setArchV p a) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (fun v g => b_vcpuUpdateV v g) c) (fun c b => b_finaliseCap c b) c b + +theorem b_capDeleteVF (slot : CslotPtr) : BKE L E K C (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact b_capDeleteG b_VF slot + +theorem b_capRevokeVF (slot : CslotPtr) : BKE L E K C (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact b_capRevokeG b_VF slot + +end B + +/-! ## `PJ` -/ + +section PJ +variable {Lp : List ObjRef} + +theorem pj_VF (c : Cap) (b : Bool) : PJK Lp (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => pj_vcpuDetachF t) (fun _ _ _ _ _ => ik_guestUnmapV (fun p a => pj_setArchV p a) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (fun v g => pj_vcpuUpdateV v g) c) (fun c b => pj_finaliseCap c b) c b + +theorem pj_capDeleteVF (slot : CslotPtr) : PJKE Lp (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact pj_capDeleteG pj_VF slot + +theorem pj_capRevokeVF (slot : CslotPtr) : PJKE Lp (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact pj_capRevokeG pj_VF slot + +end PJ + +/-! ## The current thread -/ + +section CT +variable {cq : ObjRef} + +theorem ct_VF (c : Cap) (b : Bool) : CTK cq (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => ct_vcpuDetachF t) (fun _ _ _ _ _ => ik_guestUnmapV (fun p _ => ct_setObject p _) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (fun v g => ct_vcpuUpdateV v g) c) (fun c b => ct_finaliseCap c b) c b + +theorem ct_capDeleteVF (slot : CslotPtr) : CTKE cq (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact ct_capDeleteG ct_VF slot + +theorem ct_capRevokeVF (slot : CslotPtr) : CTKE cq (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact ct_capRevokeG ct_VF slot + +end CT + +/-! ## `NQ K` -/ + +section NQ +variable {Kq : List ObjRef} + +theorem nq_VF (c : Cap) (b : Bool) : NQK Kq (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => nq_vcpuDetachF t) (fun _ _ _ _ _ => ik_guestUnmapV (fun p a => nq_setArchV Kq p a) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (fun v g => nq_vcpuUpdateV v g) c) (fun c b => SInv.nq_finaliseCap c b) c b + +theorem nq_capDeleteVF (slot : CslotPtr) : NQKE Kq (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact nq_capDeleteG nq_VF slot + +theorem nq_capRevokeVF (slot : CslotPtr) : NQKE Kq (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact nq_capRevokeG nq_VF slot + +end NQ + +/-! ## `YP` -/ + +section YK +open SInvUZ +variable {PD : PgSet} {FS : Set Nat} + +theorem yk_VF (c : Cap) (b : Bool) : YK PD FS (finaliseCapVF (Z := Unit) c b) := + ik_finaliseCapVF (fun t => yk_vcpuDetachF t) (fun _ _ _ _ _ => ik_guestUnmapV (fun p _ => SInvUZ.yk_setObject p _) _ _ _ _ _) + (fun c => ik_ioPortRevokeV (fun v g => yk_vcpuUpdateV v g) c) (fun c b => SInvUZ.yk_finaliseCap c b) c b + +theorem yk_capDeleteVF (slot : CslotPtr) : YKE PD FS (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact yk_capDeleteG yk_VF slot + +theorem yk_capRevokeVF (slot : CslotPtr) : YKE PD FS (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact yk_capRevokeG yk_VF slot + +end YK + +/-! ## `HU` (the cap half of the untyped-zero invariant): a predicate of the untyped view -/ + +section HU +open SInvUZ SInvV +variable {U : UtSet} + +theorem capOpt_eq_capAt (σ : State Unit) (p : ObjRef) (cref : CnodeIndex) : + capOpt (σ.kheap p) cref = SInvU.capAt σ (p, cref) := by + unfold SInvU.capAt + rcases σ.kheap p with _ | o + · rfl + · cases o <;> first | rfl | (simp only [capOpt, SInvU.capView]; split <;> rfl) + +/-- `HU` read off the untyped view. -/ +def HUV (U : UtSet) (w : SInvU.UView) : Prop := ∀ p cref c, w.2.1 (p, cref) = some c → UtIn U c + +theorem hu_iff (σ : State Unit) : SInvUZ.HU U σ ↔ HUV U (SInvU.uV σ) := by + unfold SInvUZ.HU HUV + simp only [capOpt_eq_capAt] + rfl + +theorem hu_VF (c : Cap) (b : Bool) : IKH (SInvUZ.HU U) (finaliseCapVF (Z := Unit) c b) := by + have h := ik_finaliseCapVF_uv (P := HUV U) + (fun c b σ hσ y hy => (hu_iff y.2).1 (hu_finaliseCap c b σ ((hu_iff σ).2 hσ) y hy)) c b + exact fun σ hσ y hy => (hu_iff y.2).2 (h σ ((hu_iff σ).1 hσ) y hy) + +theorem rv_VF (c : Cap) (b : Bool) : + RV (fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) (finaliseCapVF (Z := Unit) c b) := + fun σ x hx => ov_finaliseCapVF (Q := fun r : Cap × Cap => ∀ x n f, r.1 ≠ .untypedCap false x n f) + (fun c b σ x hx => rv_finaliseCap_ut c b σ x hx) (fun _ _ _ h => by cases h) c b σ x hx + +theorem hu_capDeleteVF (slot : CslotPtr) : IKHE (ε := Unit) (SInvUZ.HU U) (capDeleteVF (Z := Unit) slot) := by + rw [← capDeleteG_vf]; exact hu_capDeleteG hu_VF rv_VF slot + +theorem hu_capRevokeVF (slot : CslotPtr) : IKHE (ε := Unit) (SInvUZ.HU U) (capRevokeVF (Z := Unit) slot) := by + rw [← capRevokeG_vf, ← capDeleteG_vf]; exact hu_capRevokeG hu_VF rv_VF slot + +end HU + +end + +end Fl4Proofs.P3.VmFin diff --git a/proofs/Fl4Proofs/P3/VmFinVgFb.lean b/proofs/Fl4Proofs/P3/VmFinVgFb.lean new file mode 100644 index 000000000..0891be53c --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinVgFb.lean @@ -0,0 +1,105 @@ +/- +P3 (line mmuAD, Wave 3 finish-fact families, step (c), VG arms): **the extended dispatcher's frame batch keeps `FbQ N b` +and a view predicate** (`fbq_runV`, the VF sibling of SpecFbP3's `fbq_run`: DELETE goes through `capDeleteVF`, written +out as `fbDelXV`, while the COPY_MAP rollback keeps the native `fbDelX`), and `p3_performFrameBatchVF`. Spec only. No +named premise. +-/ +import Fl4Proofs.P3.VmFinFbV +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv + +noncomputable section + +set_option maxHeartbeats 4000000 + +variable {N : Irq → ObjRef} + +/-- **The VM batch keeps `FbQ N b` and a view predicate `RV`** (`fbq_run`'s statement at `performFrameBatchVF`). -/ +theorem fbq_runV {RV : UView → Prop} (b : Fl4FrameBatchInvocation) + (hci : ∀ s c src dest c0, FbQ N b s → RV (uV s) → DestRefV (uV s) dest → capAt s dest = some .nullCap → + capAt s src = some c0 → isPageCap c0 = true → IsFrameCap c → ∀ x ∈ (capInsert (Z := Unit) c src dest s).1, + RV (uV x.2)) + (hpp : ∀ s pi sl dev p R mt sz m, FbQ N b s → RV (uV s) → ArchSpecOKN (uV s) (.invokePage pi) → + PgSl sl (.invokePage pi) → capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m)) → + ∀ x ∈ (performPageInvocation (Z := Unit) pi s).1, RV (uV x.2)) + (hes : ∀ s sl dev p R mt sz m, FbQ N b s → RV (uV s) → capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m)) → + ∀ x ∈ (emptySlot (Z := Unit) sl .nullCap s).1, RV (uV x.2)) + (hset : ∀ s dev p R mt sz m sl, FbQ N b s → RV (uV s) → + capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m)) → + ∀ x ∈ (setCap (Z := Unit) (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) sl s).1, RV (uV x.2)) : + ∀ σ, FbQ N b σ → RV (uV σ) → ∀ y ∈ (performFrameBatchVF (Z := Unit) b σ).1, FbQ N b y.2 ∧ RV (uV y.2) := by + intro σ hσ hr y hy + have hu : ∀ {α : Type} {m : NonDet (State Unit) α}, (∀ v, UK v m) → IK (fun s => FbQ N b s ∧ RV (uV s)) m := + fun h s hs x hx => by + have e := h (uV s) s rfl x hx + exact ⟨fbq_uk h s hs.1 x hx, by rw [e]; exact hs.2⟩ + have hdelE : ∀ sl s dev p R mt sz m, (FbQ N b s ∧ RV (uV s)) → + capAt s sl = some (.archObjectCap (.pageCap dev p R mt sz m)) → + ∀ x ∈ (emptySlot (Z := Unit) sl .nullCap s).1, FbQ N b x.2 ∧ RV (uV x.2) := by + intro sl s dev p R mt sz m ⟨hs, hrs⟩ hc x hx + refine ⟨⟨⟨hv_emptySlotN sl _ s ⟨hs.1.1, fun c hc' => ?_⟩ x hx, io_emptySlot sl _ s hs.1.2 x hx⟩, ?_⟩, + hes s sl dev p R mt sz m hs hrs hc x hx⟩ + · have : c = .archObjectCap (.pageCap dev p R mt sz m) := Option.some.inj (hc'.symm.trans hc) + subst this + exact safeDel_of_sRefs (by simp [sRefs]) + · exact fbq_root hs.2 hc (Or.inl trivial) (fun x' cx hx' => fbk_emptySlot hx') x hx + refine ik_performFrameBatchXV (P := fun s => FbQ N b s ∧ RV (uV s)) (fun w => ik_fbEntryXV_of b w + fbx_lookupTargetSlot fbx_lookupSourceSlot fbx_ensureEmpty fbx_deriveCap ?_ ?_ + (fun sl => ik_fbDelXF sl (fun _ => hu fun v => uk_isFinalCap _) (fun _ _ => hu fun v => uk_archFinaliseCap _ _) + (fun s dev p R mt sz m h hc => hdelE sl s dev p R mt sz m h hc)) + (fun sl => ik_fbDelXVF sl (fun _ => hu fun v => uk_isFinalCap _) + (fun dev p R mt sz m fin => hu fun v => uk_finaliseCapVF_frame dev p R mt sz m fin) + (fun dev p R mt sz m fin v => uk_finaliseCapVF_frame dev p R mt sz m fin) + (fun s dev p R mt sz m h hc => hdelE sl s dev p R mt sz m h hc)) + (fun _ _ _ _ => hu fun v => uk_unmapPageUnflushed _ _ _ _) ?_) (hu fun v => uk_flushAll _ _) σ ⟨hσ, hr⟩ y hy + · -- COPY_MAP's `cap_insert` + intro s c src dest c0 ⟨hs, hrs⟩ hfc hdest hnull hc0 hpg hder x hx + have hD : DestRefV (uV s) dest := + okv_lookupSlotForCnodeOpN false _ _ _ hs.2.2 s rfl dest s hdest + refine ⟨⟨?_, ?_⟩, hci s c src dest c0 hs hrs hD hnull hc0 hpg hfc x hx⟩ + · obtain ⟨dev, p, R, mt, sz, m, rfl⟩ := fbx_frame_of_isPageCap hpg + have hR := (fb_frame_refRel hder).1 + exact hv_capInsert3 c src dest s ⟨hs.1, ⟨_, hc0, Or.inr hR⟩, posOK_of_destRef hs.1.1.2 hD⟩ x hx + · have hnu : ∀ d b n f, c ≠ .untypedCap d b n f := fun d b n f e => by rw [e] at hfc; exact hfc + exact fbq_root hs.2 hnull (Or.inr rfl) (fun x' cx hx' => hv_capInsert_keep c hnu src dest x' hx' cx) x hx + · -- the map + intro sl v + refine ik_fbMapF2 b sl v (fun _ _ _ _ _ _ => hu fun v => uk_decodePageInvocation _ _ _ _ _) ?_ + intro s pi dev p R mt sz m ⟨hs, hrs⟩ hok hpg hcap x hx + refine ⟨⟨p3_performPageInvocation pi s ⟨hs.1, hok⟩ x hx, ?_⟩, hpp s pi sl dev p R mt sz m hs hrs hok hpg hcap x hx⟩ + cases pi with + | pageMap c sl' en vs => + simp only [PgSl] at hpg + subst hpg + exact fbq_root hs.2 hcap (Or.inl trivial) (fun x' cx hx' => fbk_pageMap hx') x hx + | pageUnmap a sl' => + have hsl : capAt s sl' = some (.archObjectCap a) := hok + obtain ⟨hcn, x0, hx0⟩ := hs.2 + cases a with + | pageCap dev' p' R' mt' sz' mp' => + have hne := fbk_slot_ne hcn hx0 hsl (Or.inl trivial) + refine ⟨hcn, x0, ?_⟩ + refine (?_ : IK (fun t => capAt t x0 = some b.destCnode) _) s hx0 x hx + cases mp' <;> simp only [performPageInvocation, performPageInvocationUnmap] <;> + repeat' (first | exact fbkI_setCap hne | exact fbkI_uk (fun v => uk_unmapPage _ _ _ _) | ik_step0 | ik_unfold) + | _ => all_goals exact (hx : False).elim + | pageGetAddr _ => exact ((fbq_uk (b := b) (N := N) fun v => by simp only [performPageInvocation]; uk_auto) s hs x hx).2 + · -- the deferred unmap's `set_cap` + intro s dev p R mt sz m sl ⟨hs, hrs⟩ hc0 x hx + exact ⟨⟨hv_setCapRE (c0 := .archObjectCap (.pageCap dev p R mt sz m)) (nnu_arch (.pageCap dev p R .vmNoMap sz none)) sl s + ⟨hs.1, hc0, refEq_arch rfl rfl⟩ x hx, + fbq_root hs.2 hc0 (Or.inl trivial) (fun x' cx hx' => fbk_setCap hx') x hx⟩, + hset s dev p R mt sz m sl hs hrs hc0 x hx⟩ + +/-- **The VM batch keeps `P3 N`** from its decode's `FbOKN`. -/ +theorem p3_performFrameBatchVF (b : Fl4FrameBatchInvocation) (σ : State Unit) (hI : P3 N σ) (hok : FbOKN (uV σ) b) : + ∀ y ∈ (performFrameBatchVF (Z := Unit) b σ).1, P3 N y.2 := fun y hy => + (fbq_runV (RV := fun _ => True) b (fun _ _ _ _ _ _ _ _ _ _ _ _ _ _ => trivial) + (fun _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => trivial) (fun _ _ _ _ _ _ _ _ _ _ _ _ _ => trivial) + (fun _ _ _ _ _ _ _ _ _ _ _ _ _ => trivial) σ ⟨hI, hok⟩ trivial y hy).1.1 + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmFinVgSlot5.lean b/proofs/Fl4Proofs/P3/VmFinVgSlot5.lean new file mode 100644 index 000000000..4d7048faf --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinVgSlot5.lean @@ -0,0 +1,47 @@ +/- +P3 (line mmuAD, Wave 3 step (c), VG arms): **SetEPTRoot's call-site facts from the concrete state** (the Architect's +standing ruling: "`(t,5)` is null or a level-0 EPT cap" is derived from `EptSlotOK` at the call site, an internal +hypothesis of `sg_/vg_fl4PerformInvocationVF`, not a premise). `VmEptState.nullOrEpt0`: from `EptCore.eslot` and the +slot-cap relation (`StateRel.abs_cap`), the spec's slot 5 of a TCB holds null or a level-0 EPT cap. +`srcNe_of_cnode`: a CNode slot is not a TCB's slot 5 (`cnode_slot_ne_tcb`). No named premise. +-/ +import Fl4Proofs.P3.EptSetRoot +import Fl4Proofs.P3.VmFinArmVgC +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 + +/-- **A TCB's spec slot 5 holds null or a level-0 EPT cap** at `VmEptState` (from `EptCore.eslot`). -/ +theorem VmEptState.nullOrEpt0 (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) : SInvU.NullOrEpt0 σ (t.bv, Fl4Spec.tcbCnodeIndex 5) := by + have hR := J.1.1.xtop.rel.2.1.1 + have hAd : AbsSlot Z.th (t.bv, Fl4Spec.tcbCnodeIndex 5) := ⟨t, rfl, Or.inr ⟨ht, 5, by decide, rfl⟩⟩ + obtain ⟨c0, a0, hc0, hca, hg⟩ := hR.abs_cap hAd + have hcap : SInvU.capAt σ (t.bv, Fl4Spec.tcbCnodeIndex 5) = some a0 := + (SInvU.getCap_capAt (x := (a0, σ)) (by rw [hg]; exact Set.mem_singleton _)).2 + rw [hca.abs] at hcap + rcases J.2.eslot t ht with h0 | ⟨p, a', v', h0⟩ + · rw [capAt_capV, h0] at hc0 + obtain rfl := eq_of_ok_eq hc0 + exact Or.inl hcap + · rw [capAt_capV, h0] at hc0 + obtain rfl := eq_of_ok_eq hc0 + exact Or.inr ⟨_, _, hcap⟩ + +end + +/-- **A CNode slot is no TCB's slot 5** (SetEPTRoot's `srcNe`). -/ +theorem srcNe_of_cnode {th : TypedHeap} (hP : Placed th) {ps t : U64} {rs : Nat} (hps : th ps = some (.cnode rs)) + {idxs : List Bool} (hls : idxs.length = rs) (ht : th t = some .tcb) : + ((ps.bv, idxs) : Fl4Spec.CslotPtr) ≠ (t.bv, Fl4Spec.tcbCnodeIndex 5) := + fun e => cnode_slot_ne_tcb hP hps hls ht 5 (by decide) (by rw [e]) + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmFinishR.lean b/proofs/Fl4Proofs/P3/VmFinishR.lean new file mode 100644 index 000000000..a1dad5519 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFinishR.lean @@ -0,0 +1,7244 @@ +/- +P3 (line mmuAD, Stage 3 step 3, generated by `handoff/tools/gen_vmr.py`): **`finish` from a pending-switch VM state** +(`VmEptStateR`): the VM records' transports, the VF signal, the timer fire loop, `schedule` and `finish`, copied from +the `VmEptState` forms; `schedule` ends in `VmEptState` (it leaves `CurInv`). No named premise. +-/ +import Fl4Proofs.P3.BufInv +import Fl4Proofs.P3.EptCore +import Fl4Proofs.P3.EptReport +import Fl4Proofs.P3.JAtZBDetach +import Fl4Proofs.P3.JAtZBSched +import Fl4Proofs.P3.JAtZBSched2 +import Fl4Proofs.P3.JAtZBVcpu +import Fl4Proofs.P3.JAtZBWr +import Fl4Proofs.P3.JAtZV3 +import Fl4Proofs.P3.JAtZV3MrJ +import Fl4Proofs.P3.JAtZV3Pend +import Fl4Proofs.P3.JAtZV3RegW +import Fl4Proofs.P3.JAtZV3RepBr0 +import Fl4Proofs.P3.JAtZV3RepBrA +import Fl4Proofs.P3.JAtZV3RepBrB +import Fl4Proofs.P3.JAtZV3RepBrC +import Fl4Proofs.P3.JAtZV3RepFin +import Fl4Proofs.P3.JAtZV3RepFoot +import Fl4Proofs.P3.JAtZV3RepLoop +import Fl4Proofs.P3.JAtZV3RepTail +import Fl4Proofs.P3.JAtZV3RepVal +import Fl4Proofs.P3.JAtZV3ReportJ3 +import Fl4Proofs.P3.JAtZV3RunVm +import Fl4Proofs.P3.JAtZV3SigComp +import Fl4Proofs.P3.JAtZV3SigDon +import Fl4Proofs.P3.JAtZV3SigTail +import Fl4Proofs.P3.JAtZV3SigVm +import Fl4Proofs.P3.JAtZV3SwitchC +import Fl4Proofs.P3.JAtZV3VRead +import Fl4Proofs.P3.JExtVLay +import Fl4Proofs.P3.ScFireJ +import Fl4Proofs.P3.VcpuWrJ +import Fl4Proofs.P3.VmFinish +import Fl4Proofs.P3.VmJAtZRBFin +import Fl4Proofs.P3.VmOracle +import Fl4Proofs.P3.VmPreempt +import Fl4Proofs.P3.VmSchedule +import Fl4Proofs.P3.VmSignal +import Fl4Proofs.P3.VmState +import Fl4Proofs.P3.XTopJointData +import Fl4Proofs.P3.XTopJointFinThread +import Fl4Proofs.P3.XTopJointFinVs +import Fl4Proofs.P3.XTopJointFpu +import Fl4Proofs.P3.XTopJointReadFpu +import Fl4Proofs.P3.XTopJointSendRecv +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +open fl4_core in +/-- `VmEptStateR` with a pending reschedule (or `CurInv`) is `VmEptState`. -/ +theorem VmEptStateR.toV {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {V X : Set U64} {XL : U64 → Nat} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} + {σ : Fl4Spec.State Unit} (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (h : σ.schedulerAction = .chooseNewThread ∨ Sched.CurInv σ) : + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ := + ⟨⟨J.1.1.toB h, J.1.2⟩, J.2⟩ + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **`reprogram_timer`'s write is a timer word** (`TmW`: the current core's `REPROGRAM` word; mmuAD gen 42b18, for +`reprogramJ`'s `ext` rebuild). -/ +theorem JAtZRB.reprogram_w {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + P1.WritesIn st s (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) (TmW (L := L) Z.T Cs Z.c s) := by + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hN : P1.NodePlaced st Z.T s Z.c := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk Z.c hcC + rw [hc.cur] + exact (P1.writesIn_wr (P1.nodeAt_aligned hN (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]))).mono + fun _ hb => Or.inr hb + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 4000000 in +/-- (mmuAD gen 42b72) **The TCB timers' loop over any record `P` and spec signal `sig`**: the proof of gen 42b18's +`fire_loopE` with `P` giving the `JAtZRB` the loop reads (`hB`), kept by the timer-word writes (`hTm`, given the +`JAtZRB` after them) and by the `REPROGRAM` write (`hRep`), and the signal given (`hSig`: from `P` at a notification of +`N`, against `sig`, deterministic, the domain time kept). `fire_loopE` is it at `sig := sendSignal`, +`P := JAtZRB … ∧ E`. -/ +theorem fire_loopGR {P : S → A → Fl4Spec.State Unit → Prop} + {sig : Fl4Spec.ObjRef → Fl4Spec.Badge → Fl4Spec.SMonad Unit Unit} + (hB : ∀ {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit}, P s1 a1 σ1 → + ∃ (V1 : XVs) (Ac1 : Set U64) (Lst1 : U64 → List U64) (D1 : Set U64), + JAtZRB (L := L) arI AR (Z.mv Z.G V1) H Cs Ac1 Lst1 D1 s1 a1 σ1) + (hTm : ∀ {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} {s2 : S} {V1 : XVs} {Ac1 : Set U64} + {Lst1 : U64 → List U64} {D1 : Set U64}, P s1 a1 σ1 → + JAtZRB (L := L) arI AR (Z.mv Z.G V1) H Cs Ac1 Lst1 D1 s2 a1 σ1 → + P1.WritesIn st s1 s2 (TmW (L := L) Z.T Cs Z.c s1) → P s2 a1 σ1) + (hRep : ∀ {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit}, P s1 a1 σ1 → + P (L.wr s1 (L.nodeAt s1 (L.cpu s1) layout.node.REPROGRAM) 1#u64) a1 { σ1 with reprogramTimer := true }) + (hSig : ∀ {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} (n b : U64), P s1 a1 σ1 → n ∈ Z.N → + notification.send_signal st arI s1 a1 n b ⦃ r => ∃ σ' : Fl4Spec.State Unit, + sig n.bv b.bv σ1 = ({((), σ')}, False) ∧ σ'.domainTime = σ1.domainTime ∧ P r.1 r.2 σ' ⦄) + {σ : Fl4Spec.State Unit} (s : S) (a : A) (now i : U64) (h : FireInvG P sig σ s a) : + timer.fire_loop st arI s a now i ⦃ r => FireInvG P sig σ r.1 r.2 ⦄ := by + unfold timer.fire_loop + apply loop.spec_decr_nat (measure := fun x => 32 - x.2.2.val) + (inv := fun x => FireInvG P sig σ x.1 x.2.1) + · rintro ⟨s1, a1, i1⟩ h1 + try simp only at h1 + obtain ⟨σ1, hF1, hd1, hP1⟩ := id h1 + obtain ⟨V1, Ac1, Lst1, D1, J1⟩ := hB hP1 + have hTe : Z.T = P1.Heap.tcbs H := J1.tcbs_eq.1 + unfold timer.fire_loop.body + dsimp only + split + · rename_i hi + have hi32 : i1.val < 32 := by simpa using hi + step with P1.LawfulStore.cur_cpu_spec as ⟨c, ec⟩ + step with P1.obj.node_get.spec as ⟨t, et⟩ + step with P1.obj.Tcb.is_null.spec as ⟨b, eb⟩ + split + · exact (WP.spec_ok _).2 h1 + rename_i hb + have ht0 : t ≠ 0#u64 := fun e => hb (by rw [eb, e]; rfl) + obtain ⟨⟨Q1, R1, h1q⟩, -, hX1, -, -⟩ := id J1.blk + have hTm1 := hX1.2.2.tm + have htT : t ∈ P1.Heap.tcbs H := by + have := hTm1.min _ h1q.cur + rw [← ec] at this; rw [← et] at this + rcases this with e | e + · exact absurd e ht0 + · exact e + step with P1.obj.Tcb.timer_deadline.val as ⟨d, ed⟩ + split + · exact (WP.spec_ok _).2 h1 + step with P1.obj.Tcb.timer_badge.val as ⟨bd, ebd⟩ + have hI1 := P1.BlkOK.tinv J1.blk + refine WP.spec_bind (WP.spec_and (P1.timer.remove.tf (s0 := s1) s1 t (P1.TF.start hI1) htT) + (P1.timer.remove.rb s1 t hI1.tcbs htT hI1.rb hI1.nodes)) ?_ + rintro s2 ⟨⟨h2, -, -, -, hw2⟩, -, -, -, hrb2⟩ + -- a dead (unarmed) TCB stays unarmed + have hTd2 : ∀ q ∈ D1, H q = some .tcb → L.mem s1 (P1.tdA q) = 0#u64 → L.mem s2 (P1.tdA q) = 0#u64 := + fun q _ hH h0 => remove_td hI1.tcbs hI1.nodes hrb2 (show q ∈ P1.Heap.tcbs H from hH) h0 + -- `timer::remove` keeps `JAtZ` and the spec state + have hw2' : P1.WritesIn st s1 s2 (TmW (L := L) Z.T Cs Z.c s1) := + hw2.mono fun b hb => Or.inl (by rw [hTe]; exact hb) + have hR2 : L.mem s2 (L.nodeAt s1 Z.c layout.node.REPROGRAM) = L.mem s1 (L.nodeAt s1 Z.c layout.node.REPROGRAM) := by + obtain ⟨Q, R0, hc, -⟩ := J1.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := by have := J1.kern.cpuCs; rw [hc.cur] at this; exact this + have hN : P1.NodePlaced st Z.T s1 Z.c := by rw [hTe]; exact P1.TInv.nodes J1.blk Z.c hcC + refine hw2.mem_eq (P1.nodeAt_aligned hN (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM])) + fun hb => ?_ + have hb' : P1.TW st Z.T Cs s1 (L.nodeAt s1 Z.c layout.node.REPROGRAM) := by rw [hTe]; exact hb + have hv := P1.nodeAt_val hN (off := layout.node.REPROGRAM) (by simp [layout.node.REPROGRAM]) + have h56 : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + rcases J1.tw_word hb' with ⟨y, hy, g1, g2⟩ | ⟨c', hc', o, ho, e⟩ + · have := J1.kern.hnd y _ (J1.zH.1 y hy) Z.c hcC + simp only [P1.HKind.bits] at this; omega + · have hnb : ∀ c, L.nodeBase s1 c = (L.ksb s1).val + 13568 + c.val * 4288 := fun _ => rfl + simp only [hnb] at hv e + by_cases hcc : c' = Z.c + · subst hcc; omega + · have : c'.val ≠ Z.c.val := fun h => hcc (UScalar.eq_of_val_eq h) + rcases Nat.lt_or_gt_of_ne this with h | h + · have : c'.val * 4288 + 4288 ≤ Z.c.val * 4288 := by nlinarith + omega + · have : Z.c.val * 4288 + 4288 ≤ c'.val * 4288 := by nlinarith + omega + obtain ⟨r2, J2⟩ := J1.tmw_step hw2' (Or.inl hR2) h2 hTd2 + have e2 : (σ1.reprogramTimer || r2) = σ1.reprogramTimer := by + have g1 : (σ1.reprogramTimer || r2) = decide (Sched.nodeW st s2 Z.c layout.node.REPROGRAM ≠ 0#u64) := + J2.xtop.fin.1.1.1.repr + have g0 : σ1.reprogramTimer = decide (Sched.nodeW st s1 Z.c layout.node.REPROGRAM ≠ 0#u64) := + J1.xtop.fin.1.1.1.repr + rw [g1, g0]; unfold Sched.nodeW + rw [P1.nodeAt_of_ksb hw2.1, hR2] + rw [e2] at J2 + have J2' : JAtZRB (L := L) arI AR (Z.mv Z.G V1) H Cs Ac1 Lst1 D1 s2 a1 σ1 := J2 + have hP2 : P s2 a1 σ1 := hTm hP1 J2' hw2' + step with P1.obj.Tcb.bound_ntfn.val as ⟨n, en⟩ + refine WP.spec_bind (Pₘ := fun r : S × A => ∃ σ2 : Fl4Spec.State Unit, + (σ2 = σ1 ∨ ∃ n b, sig n b σ1 = ({((), σ2)}, False)) ∧ σ2.domainTime = σ1.domainTime ∧ P r.1 r.2 σ2) ?_ ?_ + · split + · rename_i hn0 + have hn0' : L.mem s2 (P1.boundA t) ≠ 0#u64 := by rw [← en]; exact P1.bne_ne hn0 + have hnH : n ∈ P1.Heap.ntfns H := by rw [en]; exact h2.2.2.1.2.2.bnd t htT hn0' + have hnN : n ∈ Z.N := J2'.hset.2.1 n hnH + refine WP.spec_mono (hSig n bd hP2 hnN) ?_ + rintro r ⟨σ2, hss, hd2, hP3⟩ + exact ⟨σ2, Or.inr ⟨n.bv, bd.bv, hss⟩, hd2, hP3⟩ + · exact (WP.spec_ok _).2 ⟨σ1, Or.inl rfl, rfl, hP2⟩ + rintro ⟨s3, a3⟩ ⟨σ2, hsig, hd2, hP3⟩ + try simp only at hP3 + try simp only [WP.uncurry'_pair] + step with P1.LawfulStore.cur_cpu_spec as ⟨c3, ec3⟩ + step with P1.obj.node_set.spec as ⟨s4, es4⟩ + step as ⟨i4, ei4⟩ + try simp only [WP.spec_ok] + subst es4 ec3 + refine ⟨⟨{ σ2 with reprogramTimer := true }, + Relation.ReflTransGen.tail hF1 ⟨σ2, hsig, rfl⟩, (hd2.trans hd1 : σ2.domainTime = σ.domainTime), + hRep hP3⟩, by omega⟩ + · exact (WP.spec_ok _).2 h1 + · exact h + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **A VSpace frame is an `other` object of `H` of at least a page**: a +spec arch object (`VsRel.pool`, `PtRel`), so `H`'s (`SpecCover.arch`). -/ +theorem JAtZRB.vsFr_other {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {q : U64} (hq : VsFr Z q) : ∃ n, 12 ≤ n ∧ H q = some (.other n) := by + have hF := J.xtop.fin + have hk : ∃ ao, σ.kheap q.bv = some (.archObj ao) := by + rcases hq with hq | hq | hq | hq | hq + · obtain ⟨f, hf, -⟩ := hF.2.1.pool q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l4 q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l3 q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l2 q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l1 q hq; exact ⟨_, hf⟩ + obtain ⟨ao, hao⟩ := hk + obtain ⟨n, hn, hH⟩ := J.cover.arch _ ao hao + exact ⟨n, hn, hH⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **`JKern` across an arch step** (`kern_keep` with the global PML4 kept a +PML4 rather than every arch object kept). -/ +theorem JAtZRB.kern_keepA {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hS' : Sched.SchedRel st Z.N Z.T Z.C Z.c s' σ') (F : ArFr σ σ') + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + JKern (L := L) arI H Cs D Z.th s' σ' := by + have K := J.kern + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + have ecw : L.curW s' = L.curW s := by + apply P1.eq_of_bv + have h1 := hs'.curThread; have h2 := hs.curThread + unfold P1.LawfulStore.curW + rw [hcpu, hna, hc.cur] + unfold nodeW at h1 h2 + rw [hna] at h1 + rw [← h1, ← h2, F.ct] + have ecs : L.cscW s' = L.cscW s := by + apply P1.eq_of_bv + have h1 := hs'.curSc; have h2 := hs.curSc + unfold P1.LawfulStore.cscW P1.LawfulStore.nodeA + rw [hcpu, hna, hc.cur] + unfold nodeW at h1 h2 + rw [hna] at h1 + rw [← h1, ← h2, F.csc] + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp only [P1.LawfulStore.nodeBase, hks] + refine ⟨fun sl h => K.orig sl (by rw [← F.orig]; exact h), by rw [ecw, F.ct]; exact K.cur, + by rw [ecw]; exact K.curT, by rw [hcpu, hna]; exact K.act, by rw [hna]; exact K.dwAl, + by rw [hna]; exact K.dwObj, by rw [hna, hna, hcpu, ecw]; exact K.dwNe, ?_, + by rw [F.gpml4]; exact F.hasPm K.gpm, by rw [F.gpml4]; exact K.ga, by rw [F.gpml4]; exact K.gf, + K.heap, K.h0, by rw [hks]; exact K.hks, by rw [hks]; exact K.hksIn, fun p k hp c hc => by rw [hnb]; exact K.hnd p k hp c hc, + by rw [hks]; exact K.ksbFit, by rw [hks]; exact K.ksbAl, by rw [hks, hcpu]; exact K.nodeFit, + by rw [hcpu]; exact K.cpuCs, by rw [ecw]; exact K.curH, by rw [ecs]; exact K.cscH, + K.csLt, K.cs0, K.csN, K.tdx.frame F.plat⟩ + rw [hna, hDbg]; exact K.head + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`JAtZ` across an `ArFr` spec step with FL4's store kept** (XTopJointFinVs's +`finVsJb` with no word written): `XTopAt` after given (at any arch state). -/ +theorem JAtZRB.arFrS (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {a' : A} {σ' : Fl4Spec.State Unit} + (F : ArFr σ σ') (hX : XTopAt (L := L) AR Z s a' σ') : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a' σ' := by + have hSS : SlotsSame Z.th (L.mem s) (L.mem s) := SlotsSame.refl _ _ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have k1 : ∀ t ∈ Z.T, L.mem s (mcpA t) = L.mem s (mcpA t) ∧ L.mem s (flagsA t) = L.mem s (flagsA t) := fun _ _ => ⟨rfl, rfl⟩ + have k2 : ∀ t ∈ Z.T, L.mem s (P1.yieldA t) = L.mem s (P1.yieldA t) := fun _ _ => rfl + have kb : ∀ t ∈ Z.T, L.mem s (tIpcBufA t) = L.mem s (tIpcBufA t) := fun _ _ => rfl + have k3 : ∀ q ∈ Z.C, L.mem s (P1.scReplyA q) = L.mem s (P1.scReplyA q) := fun _ _ => rfl + have k4 : ∀ r ∈ Z.R, L.mem s (P1.rPrevA r) = L.mem s (P1.rPrevA r) ∧ + L.mem s (P1.rNextA r) = L.mem s (P1.rNextA r) := fun _ _ => ⟨rfl, rfl⟩ + have ck : Sched.CKeep σ σ' := ⟨fun q y n hq => ⟨y, F.sc.2 hq, rfl⟩, + fun q x hq => ⟨x, (F.look (fun _ e => by cases e)).2 hq, rfl, rfl⟩, fun p x n hp => ⟨x, F.sc.1 hp⟩⟩ + have yk : YKeep σ σ' := ⟨fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩, + fun p x n hp => ⟨x, F.sc.2 hp, rfl⟩, fun p y n hp => ⟨y, F.sc.1 hp, rfl⟩⟩ + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ rfl ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 F.khrb + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s σ' := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => F.szKeep q.bv y n hq) rfl (fun _ _ => rfl) rfl⟩ + have hkern := J.kern_keepA rfl rfl hX.fin.1.1.1 F rfl + have hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have U := J.utc sl p sb fi dev hsl hcap hdv + refine ⟨U.pal, U.pfit, U.fiLe, U.high, U.actZ, U.free, U.zero, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [F.cdt] at h0; exact h0), ?_, U.dwReg, fun g => by rw [F.gpml4]; exact U.gReg g⟩ + intro g h0 hf0 + rw [F.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst g h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← F.orig]; exact h), r3⟩ + have R : JRestR (L := L) arI { Z with G := Z.G } H Cs Ac Lst D s σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 yk, J.mcp.frame k1 (fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩), + J.buf.frame kb (fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩), hRP', + J.apart.sc_frame rfl F.szKeep, hjpl, J.jcov, hkern, hutc, J.nsc.ar F, J.inv.ar F, (fun e => (J.cur (by rw [← F.act]; exact e)).ar F), + J.own.ar F, J.off.ar F, J.act.ar F, J.bsa.ar F, J.isorted.ar F, J.curRun.ar F, J.blk⟩ + exact J.cs_frameK (hvn := J.carry.vsNamed) rfl hX F.kback F.ipcFr (J.carry.frame subset_rfl F.ipcFr F.ksz) (JCs.sameBR J hSS F.ksz) R (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) + hSS (P1.DbgKeep.refl s) F.idle + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A timer word (`P1.TW`) is not the VCPU-list head word** (gen 42b46, P3/NatFoot's `NatW`): a TCB's timer fields +or a core's timer heads (`JAtZRB.tw_word`), both off the kernel-state block's head. -/ +theorem JAtZRB.tw_nh (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (hb : P1.TW st Z.T Cs s b) : + b ≠ P1.vHeadA st s := by + intro e + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + rcases J.tw_word hb with ⟨y, hy, g1, g2⟩ | ⟨c', hc', o, ho, g⟩ + · have d := J.kern.hks y _ (J.zH.1 y hy) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + rw [e] at g1 g2 + omega + · have hnb : L.nodeBase s c' = (L.ksb s).val + 13568 + c'.val * 4288 := rfl + rw [e] at g + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- (mmuAD gen 42b73) **Timer-word writes are a native step** (`TmW`: the TCBs' timer fields, every core's timer +heads, `REPROGRAM` of `Z.c`; the spec state kept): moved out of `JAtZB.tmRemoveJ` (statement unchanged) for the +fire loop at `VmEptStateR` (P3/VmOracle). -/ +theorem JAtZRB.tmwN (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s2 : S} + (hw2' : P1.WritesIn st s s2 (TmW (L := L) Z.T Cs Z.c s)) : NatFoot (L := L) Z s σ s2 σ := by + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := by have := J.kern.cpuCs; rw [hc.cur] at this; exact this + have hN : P1.NodePlaced st Z.T s Z.c := by rw [hTe]; exact P1.TInv.nodes J.blk Z.c hcC + have hv := P1.nodeAt_val hN (off := layout.node.REPROGRAM) (by simp [layout.node.REPROGRAM]) + have h56 : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + refine NatFoot.ofWS (J.tmw_slots hw2') hw2' (fun b hb => ?_) + (fun b hb u hu => J.tmw_ne_tcb hb hu (Sched.tVcpuA_val J.ip.e.tcb hu) (by omega)) (fun b hb => by + rcases hb with hb | rfl + · exact J.tw_nh hb + · intro e + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + rw [e] at hv + omega) (NatK.of_kheap rfl) + rcases hb with hb | rfl + · rcases J.tw_word hb with ⟨y, hy, g1, g2⟩ | ⟨c', hc', o, ho, e⟩ + · exact Or.inl (Or.inr (Or.inr (Or.inl ⟨y, hy, by omega, by omega⟩))) + · exact Or.inr ⟨c', J.kern.csLt c' hc', by rcases ho with rfl | rfl; exacts [Or.inl e, Or.inr e]⟩ + · exact Or.inl (Or.inl ⟨by rw [hv]; omega, by rw [hv]; omega⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Fl4Proofs.P1 in +set_option maxHeartbeats 16000000 in +/-- **One IPC-buffer word written at `JAtZRB`** (XTopJointData's `JAtZ.userWrJ`, generated copy). -/ +theorem JAtZRB.userWrJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ) {wp : U64 × Fl4Spec.Word} (hwB : wp ∈ Z.B) + (hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → wp.1.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ wp.1.val) + (hObj : ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ wp.1.val ∧ wp.1.val < o.toNat + 2 ^ Fl4Spec.objBits ko) + (v : U64) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s wp.1 v) a + (Endp.setUm σ (Endp.storeMem σ.machineState.underlyingMemory wp.2 v.bv)) := by + set s1 := L.wr s wp.1 v with hs1 + set σ1 := Endp.setUm σ (Endp.storeMem σ.machineState.underlyingMemory wp.2 v.bv) with hσ1 + have F : ArFr σ σ1 := arFr_setUm σ _ + have hP := J.xtop.fin.1.2 + have hal : P1.Aligned wp.1 := (hP.ep.balign wp hwB).1 + have hW : P1.WritesIn st s s1 (· = wp.1) := P1.writesIn_wr hal + have hkb : L.ksb s1 = L.ksb s := hW.1 + have hcpu : L.cpu s1 = L.cpu s := hW.2.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s1) := + slotsSame_wr hP.pl s v hal fun x hx => BufPlaced.slot hP hwB hx + have hX1 : XTopAt (L := L) AR Z s1 a σ1 := + J.xtop.ofRInv (bufw_inv s a σ J.xtop.toRInv hBP hwB v) hkb F.szKeep + -- the frame words + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hT := hcq.tcbs + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hHk := J.kern.heap + have objK : ∀ p k, H p = some k → (∀ z, k ≠ .other z) → ∀ w : U64, P1.Aligned w → p.val ≤ w.val → + w.val < p.val + 2 ^ k.bits → L.mem s1 w = L.mem s w := fun p k hp hk w hal' g1 g2 => + hW.mem_eq hal' fun hb => by subst hb; rcases hAp p k hp hk with e | e <;> omega + have tcbK : ∀ t ∈ Z.T, ∀ w : U64, P1.Aligned w → t.val ≤ w.val → w.val < t.val + 4096 → + L.mem s1 w = L.mem s w := fun t ht w hal' g1 g2 => + objK t .tcb (J.zH.1 t ht) (fun _ e => by cases e) w hal' g1 (by simpa [P1.HKind.bits] using g2) + have k1 : ∀ t ∈ Z.T, L.mem s1 (mcpA t) = L.mem s (mcpA t) ∧ L.mem s1 (flagsA t) = L.mem s (flagsA t) := fun t ht => + ⟨tcbK t ht _ (mcpA_aligned hT ht) (by rw [mcpA_val hT ht]; omega) (by rw [mcpA_val hT ht]; omega), + tcbK t ht _ (flagsA_aligned hT ht) (by rw [flagsA_val hT ht]; omega) (by rw [flagsA_val hT ht]; omega)⟩ + have k2 : ∀ t ∈ Z.T, L.mem s1 (P1.yieldA t) = L.mem s (P1.yieldA t) := fun t ht => + tcbK t ht _ (yieldA_aligned hT ht) (by rw [yieldA_val hT ht]; omega) (by rw [yieldA_val hT ht]; omega) + have kb : ∀ t ∈ Z.T, L.mem s1 (tIpcBufA t) = L.mem s (tIpcBufA t) := fun t ht => + tcbK t ht _ (tIpcBufA_aligned (hT t ht)) (by rw [tIpcBufA_val (hT t ht)]; omega) + (by rw [tIpcBufA_val (hT t ht)]; omega) + have scw : ∀ q z, H q = some (.sc z) → ∀ w : U64, P1.Aligned w → q.val ≤ w.val → w.val < q.val + 2 ^ z → + L.mem s1 w = L.mem s w := fun q z hq w hal' g1 g2 => + objK q _ hq (fun _ e => by cases e) w hal' g1 (by simpa only [P1.HKind.bits] using g2) + have k3 : ∀ q ∈ Z.C, L.mem s1 (P1.scReplyA q) = L.mem s (P1.scReplyA q) := fun q hq => by + obtain ⟨xq, nq, hkq, hin⟩ := screply_obj J.xtop.fin.1.1.1 hq + have hHq := (hJ.sc q xq nq (J.c0 q hq) hkq).2 + exact scw q _ hHq _ (sc_reply_word J.xtop.fin.1.1.1 hq).1 hin.1 hin.2 + have k4 : ∀ r ∈ Z.R, L.mem s1 (P1.rPrevA r) = L.mem s (P1.rPrevA r) ∧ + L.mem s1 (P1.rNextA r) = L.mem s (P1.rNextA r) := by + intro r hr + have hal' := J.rp.align r hr + have hHr : H r = some .reply := J.zH.2.2.1 r hr + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s1 a' = L.mem s a' := fun a' ha => + objK r _ hHr (fun _ e => by cases e) a' (J.rp.aligned hr ha) (by omega) + (by simp only [P1.HKind.bits]; omega) + exact ⟨k _ (Or.inl (rPrevA_val hal')), k _ (Or.inr (rNextA_val hal'))⟩ + have hOut : ∀ q z, H q = some (.sc z) → ∀ K : U64, K.val % 8 = 0 → K.val < 128 → + L.mem s1 (scW q K) = L.mem s (scW q K) := by + intro q z hq K hK8 hK + have hal' := hHk.align q _ hq + have hfit := hHk.fits q _ hq + have hz := hHk.scmin q z hq + simp only [P1.HKind.bits] at hal' hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal'] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + exact scw q z hq _ (by show (scW q K).val % 8 = 0; rw [hv]; omega) (by omega) (by omega) + -- the debug word and the IRQ node + have hN0 : P1.NodePlaced st Z.T s 0#u64 := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk _ J.kern.cs0 + have hDbg : L.mem s1 (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = + L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by + have hdv := P1.nodeAt_val hN0 (off := layout.node.DEBUG_TCBS) (by simp [layout.node.DEBUG_TCBS]) + have hdo : layout.node.DEBUG_TCBS.val < 4288 := by simp [layout.node.DEBUG_TCBS] + exact hW.mem_eq J.kern.dwAl fun hb => by + have := J.nodeB wp hwB 0#u64 (by simp) + rw [hb] at hdv; omega + have hnbc : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + have hirq : ∀ i : U64, i.val < 128 → capV (L.mem s1) (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := + irqNode_keep hW J.kern.ksbFit J.kern.ksbAl fun b hb => by + subst hb; have := hBP.far wp hwB; omega + -- the dead stay dead + have hAI : ActIdle σ1 := J.act.ar F + have yk : YKeep σ σ1 := ⟨fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩, + fun p x n hp => ⟨x, F.sc.2 hp, rfl⟩, fun p y n hp => ⟨y, F.sc.1 hp, rfl⟩⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s1 q := fun q hq => + J.deadP_keep hX1.fin.1.1.1 hX1.ipc + (fun x hx _ => ⟨x, (F.look (fun _ e => by cases e)).2 hx, rfl, rfl, Or.inl rfl⟩) + (fun x hx => (F.look (fun _ e => by cases e)).2 hx) + (fun x hx => ⟨x, F.ntfn.2 hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, (F.look (fun _ e => by cases e)).2 hx, rfl⟩) + (fun x n hx => ⟨x, F.sc.2 hx, Or.inl rfl, Or.inl rfl⟩) (k3 q) (fun z hz _ => hOut q z hz) F.idle hAI.1 + (J.yld.frame k2 yk) (yk.dk q.bv) (J.jcov.dead q hq) + -- P1's `BlkOK` + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s1 := by + refine (P1.blk_of_untracked J.blk hW fun b hb => ⟨⟨by rw [hb]; exact hal, fun t ht => ?_, fun c hc => ?_⟩, + fun e he => ?_, fun hf => ?_⟩).1 + · subst hb + rcases hAp t .tcb ht (fun _ e => by cases e) with e | e + · left; omega + · right; simpa [P1.HKind.bits] using e + · subst hb + rcases J.nodeB wp hwB c (J.kern.csLt c hc) with e | e <;> omega + · subst hb + rcases he with he | he + · rcases hAp e _ he (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · rcases hAp e _ he (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · subst hb + obtain ⟨-, p, k, hp, hk, hin⟩ := hf + have hko : ∀ z, k ≠ .other z := by + rcases hk with rfl | ⟨z, rfl⟩ | ⟨rfl, -⟩ <;> intro z e <;> cases e + obtain ⟨g1, g2⟩ := hin + rcases hAp p k hp hko with e | e <;> omega + -- the untyped caps' facts + have hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s1) (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s1 σ1 Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hna : ∀ c o, L.nodeAt s1 c o = L.nodeAt s c o := P1.nodeAt_of_ksb hkb + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, + U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [F.cdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [F.gpml4]; exact U.gReg (B g)⟩ + · intro g w hal' h1 h2 + rw [hW.mem_eq hal' fun hb => ?_] + · exact U.zero (B g) w hal' h1 h2 + subst hb + obtain ⟨o, ko, hko, g1, g2⟩ := hObj + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) o ko hko + omega + · intro g h0 hf0 + rw [F.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← F.orig]; exact h), by rw [hna, hDbg]; exact r3⟩ + -- the spec's frames + have ck : Sched.CKeep σ σ1 := ⟨fun q y n hq => ⟨y, F.sc.2 hq, rfl⟩, + fun q x hq => ⟨x, (F.look (fun _ e => by cases e)).2 hq, rfl, rfl⟩, fun p x n hp => ⟨x, F.sc.1 hp⟩⟩ + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hkb ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 F.khrb + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s1 σ1 := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => F.szKeep q.bv y n hq) hkb hirq (J.idleW_keep hX1.fin.1.1.1 F.idle F.isc).2⟩ + have hkern := J.kern_keepA hkb hcpu hX1.fin.1.1.1 F hDbg + have hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s1 := dbgKeep_of_writes J.blk hW fun b hb => by + subst hb + refine J.notDbg (fun t ht => ?_) (fun c hc => ?_) + · rcases hAp t .tcb ht (fun _ e => by cases e) with e | e + · left; omega + · right; simpa [P1.HKind.bits] using e + · rcases J.nodeB wp hwB c (J.kern.csLt c hc) with e | e <;> omega + have hTd : Sched.TdKeep (L := L) H s s1 := fun q hq => + tcbK q (J.tcbZ hq) _ (P1.field_aligned (hT q (J.tcbZ hq)) (K := 3528#u64) (by simp) (by simp)) + (by rw [J.tdA_val' (J.tcbZ hq)]; omega) (by rw [J.tdA_val' (J.tcbZ hq)]; omega) + have R : JRestR (L := L) arI { Z with G := Z.G } H Cs Ac Lst D s1 σ1 := + ⟨hCR', hRF', hRR', J.yld.frame k2 yk, J.mcp.frame k1 (fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩), + J.buf.frame kb (fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩), hRP', + J.apart.sc_frame hkb F.szKeep, hjpl, J.jcov_keep hkb hSS hdead (J.idleW_keep hX1.fin.1.1.1 F.idle F.isc) + (DXKeep.of_dbg hKd hTd), hkern, + hutc, J.nsc.ar F, J.inv.ar F, (fun e => (J.cur (by rw [← F.act]; exact e)).ar F), J.own.ar F, + J.off.ar F, hAI, J.bsa.ar F, J.isorted.ar F, J.curRun.ar F, hblk⟩ + exact J.cs_frameK (hvn := J.vsSS (G' := Z.G) hX1 hkb hSS (Vs.ArchShr.of_keep rfl fun _ _ _ => rfl)) hkb hX1 F.kback F.ipcFr (J.carry.frame subset_rfl F.ipcFr F.ksz) (JCs.sameBR J hSS F.ksz) R (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) + hSS hKd F.idle + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- (mmuAD gen 42b66) **The IPC-buffer word `userWrJ` writes is a native-footprint word**: an `OObj` buffer word, +not the VCPU-list head (`BufPlaced.far`), no TCB's VCPU word (`hAp`). -/ +theorem JAtZRB.userW_nat (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ) {wp : U64 × Fl4Spec.Word} (hwB : wp ∈ Z.B) + (hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → wp.1.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ wp.1.val) : + Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ wp.1 ∧ wp.1 ≠ P1.vHeadA st s ∧ ∀ t ∈ Z.T, wp.1 ≠ tVcpuA t := by + refine ⟨Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨wp, hwB, le_refl _, by omega⟩)))))), fun e => ?_, + fun t ht e => ?_⟩ + · have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + have hf := hBP.far wp hwB + rw [e, hvh, hnb] at hf + omega + · have hT := J.xtop.fin.1.1.1.placed.tcb + have hv : wp.1.val = t.val + 3664 := by rw [e]; exact tfield_val (hT t ht) (K := 3664#u64) (by simp) + rcases hAp t .tcb (J.zH.1 t ht) (fun _ e => by cases e) with e' | e' + · omega + · simp only [P1.HKind.bits] at e'; omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The SCs of `Z.C` are 8-aligned (`H`'s SC objects, `JPlaced.sc`, `HeapOK.align`). -/ +theorem JAtZRB.scAlign8 (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ sc ∈ Z.C, ∀ x n, σ.kheap sc.bv = some (.schedContext x n) → sc.val % 8 = 0 := by + intro sc hsc x n hk + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have hal := J.kern.heap.align sc _ hq + simp only [P1.HKind.bits] at hal + have hd : 8 ∣ 2 ^ (n + Fl4Spec.minSchedContextBits) := + Nat.pow_dvd_pow 2 (by simp only [Fl4Spec.minSchedContextBits]; omega : 3 ≤ n + Fl4Spec.minSchedContextBits) + rw [← Nat.mod_mod_of_dvd sc.val hd, hal] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`BufPlaced` at a `JAtZ`**, the buffer words off the nodes below core +`Z.c`. -/ +theorem JAtZRB.bufPlacedLow (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hlow : ∀ wp ∈ Z.B, wp.1.val < L.nodeBase s 0#u64 ∨ L.nodeBase s Z.c ≤ wp.1.val) : + Vs.BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ := + bufPlaced_of J.xtop J.ks J.ip J.scAlign8 hlow + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`BufPlaced` at every `JAtZ`** (no premise): `hlow` from `JAtZ.nodeB` +at the node slots below core `Z.c` (`Z.c ∈ Cs`, `JKern.csLt`). -/ +theorem JAtZRB.bufPlacedJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + Vs.BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ := by + refine J.bufPlacedLow fun wp hw => ?_ + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hc64 := J.kern.csLt Z.c hcC + have hn0 : L.nodeBase s 0#u64 = (L.ksb s).val + 13568 := by rw [nodeBase_val]; simp + rw [hn0, nodeBase_val] + by_contra hne + have h1 : (L.ksb s).val + 13568 ≤ wp.1.val := by omega + have h2 : wp.1.val < (L.ksb s).val + 13568 + Z.c.val * 4288 := by omega + set k := (wp.1.val - ((L.ksb s).val + 13568)) / 4288 with hk + have hkc : k < Z.c.val := by + rw [hk, Nat.div_lt_iff_lt_mul (by norm_num)]; rw [Nat.mul_comm] at h2; omega + obtain ⟨c', hc'⟩ : ∃ c' : U64, c'.val = k := ⟨⟨BitVec.ofNat 64 k⟩, by + show (BitVec.ofNat 64 _).toNat = _; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega)⟩ + have e := J.nodeB wp hw c' (by omega) + rw [nodeBase_val, hc'] at e + have hlo : k * 4288 ≤ wp.1.val - ((L.ksb s).val + 13568) := Nat.div_mul_le_self _ _ + have hhi : wp.1.val - ((L.ksb s).val + 13568) < k * 4288 + 4288 := by + rw [hk]; exact Nat.lt_div_mul_add (by norm_num) + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Fl4Proofs.P1 in +set_option maxHeartbeats 16000000 in +/-- **A frame word that is not an IPC-buffer word, written at `J`** (module +docstring; the spec state kept). -/ +theorem JAtZRB.frameWrJCore (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {w : U64} + (hal : P1.Aligned w) + (hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → w.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ w.val) + (hObj : ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ w.val ∧ w.val < o.toNat + 2 ^ Fl4Spec.objBits ko) + (hKn : ∀ c ∈ Cs, w.val + 8 ≤ L.nodeBase s c ∨ L.nodeBase s c + 4288 ≤ w.val) + (hKi : w.val < (L.ksb s).val + 1248 ∨ (L.ksb s).val + 5344 ≤ w.val) (v : U64) + (hSS : SlotsSame Z.th (L.mem s) (L.mem (L.wr s w v))) + (hX1 : XTopAt (L := L) AR Z (L.wr s w v) a σ) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s w v) a σ := by + set s1 := L.wr s w v with hs1 + have F : ArFr σ σ := ⟨fun _ => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have hW : P1.WritesIn st s s1 (· = w) := P1.writesIn_wr hal + have hkb : L.ksb s1 = L.ksb s := hW.1 + have hcpu : L.cpu s1 = L.cpu s := hW.2.1 + -- the frame words + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hT := hcq.tcbs + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hHk := J.kern.heap + have objK : ∀ p k, H p = some k → (∀ z, k ≠ .other z) → ∀ w' : U64, P1.Aligned w' → p.val ≤ w'.val → + w'.val < p.val + 2 ^ k.bits → L.mem s1 w' = L.mem s w' := fun p k hp hk w' hal' g1 g2 => + hW.mem_eq hal' fun hb => by rw [hb] at g1 g2; rcases hAp p k hp hk with e | e <;> omega + have tcbK : ∀ t ∈ Z.T, ∀ w' : U64, P1.Aligned w' → t.val ≤ w'.val → w'.val < t.val + 4096 → + L.mem s1 w' = L.mem s w' := fun t ht w' hal' g1 g2 => + objK t .tcb (J.zH.1 t ht) (fun _ e => by cases e) w' hal' g1 (by simpa [P1.HKind.bits] using g2) + have k1 : ∀ t ∈ Z.T, L.mem s1 (mcpA t) = L.mem s (mcpA t) ∧ L.mem s1 (flagsA t) = L.mem s (flagsA t) := fun t ht => + ⟨tcbK t ht _ (mcpA_aligned hT ht) (by rw [mcpA_val hT ht]; omega) (by rw [mcpA_val hT ht]; omega), + tcbK t ht _ (flagsA_aligned hT ht) (by rw [flagsA_val hT ht]; omega) (by rw [flagsA_val hT ht]; omega)⟩ + have k2 : ∀ t ∈ Z.T, L.mem s1 (P1.yieldA t) = L.mem s (P1.yieldA t) := fun t ht => + tcbK t ht _ (yieldA_aligned hT ht) (by rw [yieldA_val hT ht]; omega) (by rw [yieldA_val hT ht]; omega) + have kb : ∀ t ∈ Z.T, L.mem s1 (tIpcBufA t) = L.mem s (tIpcBufA t) := fun t ht => + tcbK t ht _ (tIpcBufA_aligned (hT t ht)) (by rw [tIpcBufA_val (hT t ht)]; omega) + (by rw [tIpcBufA_val (hT t ht)]; omega) + have scw : ∀ q z, H q = some (.sc z) → ∀ w' : U64, P1.Aligned w' → q.val ≤ w'.val → w'.val < q.val + 2 ^ z → + L.mem s1 w' = L.mem s w' := fun q z hq w' hal' g1 g2 => + objK q _ hq (fun _ e => by cases e) w' hal' g1 (by simpa only [P1.HKind.bits] using g2) + have k3 : ∀ q ∈ Z.C, L.mem s1 (P1.scReplyA q) = L.mem s (P1.scReplyA q) := fun q hq => by + obtain ⟨xq, nq, hkq, hin⟩ := screply_obj J.xtop.fin.1.1.1 hq + have hHq := (hJ.sc q xq nq (J.c0 q hq) hkq).2 + exact scw q _ hHq _ (sc_reply_word J.xtop.fin.1.1.1 hq).1 hin.1 hin.2 + have k4 : ∀ r ∈ Z.R, L.mem s1 (P1.rPrevA r) = L.mem s (P1.rPrevA r) ∧ + L.mem s1 (P1.rNextA r) = L.mem s (P1.rNextA r) := by + intro r hr + have hal' := J.rp.align r hr + have hHr : H r = some .reply := J.zH.2.2.1 r hr + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s1 a' = L.mem s a' := fun a' ha => + objK r _ hHr (fun _ e => by cases e) a' (J.rp.aligned hr ha) (by omega) + (by simp only [P1.HKind.bits]; omega) + exact ⟨k _ (Or.inl (rPrevA_val hal')), k _ (Or.inr (rNextA_val hal'))⟩ + have hOut : ∀ q z, H q = some (.sc z) → ∀ K : U64, K.val % 8 = 0 → K.val < 128 → + L.mem s1 (scW q K) = L.mem s (scW q K) := by + intro q z hq K hK8 hK + have hal' := hHk.align q _ hq + have hfit := hHk.fits q _ hq + have hz := hHk.scmin q z hq + simp only [P1.HKind.bits] at hal' hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal'] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + exact scw q z hq _ (by show (scW q K).val % 8 = 0; rw [hv]; omega) (by omega) (by omega) + -- the debug word and the IRQ node: in the kernel-global data, which the word misses + have hN0 : P1.NodePlaced st Z.T s 0#u64 := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk _ J.kern.cs0 + have hDbg : L.mem s1 (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = + L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by + have hdv := P1.nodeAt_val hN0 (off := layout.node.DEBUG_TCBS) (by simp [layout.node.DEBUG_TCBS]) + have hdo : layout.node.DEBUG_TCBS.val < 4288 := by simp [layout.node.DEBUG_TCBS] + exact hW.mem_eq J.kern.dwAl fun hb => by + rw [hb] at hdv + rcases hKn 0#u64 J.kern.cs0 with e | e <;> omega + have hirq : ∀ i : U64, i.val < 128 → capV (L.mem s1) (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := + irqNode_keep hW J.kern.ksbFit J.kern.ksbAl fun b hb => hb.symm ▸ hKi + -- the dead stay dead + have hAI : ActIdle σ := J.act.ar F + have yk : YKeep σ σ := ⟨fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩, + fun p x n hp => ⟨x, F.sc.2 hp, rfl⟩, fun p y n hp => ⟨y, F.sc.1 hp, rfl⟩⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s1 q := fun q hq => + J.deadP_keep hX1.fin.1.1.1 hX1.ipc + (fun x hx _ => ⟨x, (F.look (fun _ e => by cases e)).2 hx, rfl, rfl, Or.inl rfl⟩) + (fun x hx => (F.look (fun _ e => by cases e)).2 hx) + (fun x hx => ⟨x, F.ntfn.2 hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, (F.look (fun _ e => by cases e)).2 hx, rfl⟩) + (fun x n hx => ⟨x, F.sc.2 hx, Or.inl rfl, Or.inl rfl⟩) (k3 q) (fun z hz _ => hOut q z hz) F.idle hAI.1 + (J.yld.frame k2 yk) (yk.dk q.bv) (J.jcov.dead q hq) + -- P1's `BlkOK` + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s1 := by + refine (P1.blk_of_untracked J.blk hW fun b hb => ⟨⟨by rw [hb]; exact hal, fun t ht => ?_, fun c hc => ?_⟩, + fun e he => ?_, fun hf => ?_⟩).1 + · subst hb + rcases hAp t .tcb ht (fun _ e => by cases e) with e | e + · left; omega + · right; simpa [P1.HKind.bits] using e + · subst hb + exact (hKn c hc).imp (fun h => by omega) id + · subst hb + rcases he with he | he + · rcases hAp e _ he (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · rcases hAp e _ he (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · subst hb + obtain ⟨-, p, k, hp, hk, hin⟩ := hf + have hko : ∀ z, k ≠ .other z := by + rcases hk with rfl | ⟨z, rfl⟩ | ⟨rfl, -⟩ <;> intro z e <;> cases e + obtain ⟨g1, g2⟩ := hin + rcases hAp p k hp hko with e | e <;> omega + -- the untyped caps' facts + have hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s1) (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s1 σ Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hna : ∀ c o, L.nodeAt s1 c o = L.nodeAt s c o := P1.nodeAt_of_ksb hkb + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, + U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [F.cdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [F.gpml4]; exact U.gReg (B g)⟩ + · intro g w' hal' h1 h2 + rw [hW.mem_eq hal' fun hb => ?_] + · exact U.zero (B g) w' hal' h1 h2 + rw [hb] at h1 h2 + obtain ⟨o, ko, hko, g1, g2⟩ := hObj + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) o ko hko + omega + · intro g h0 hf0 + rw [F.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← F.orig]; exact h), by rw [hna, hDbg]; exact r3⟩ + -- the spec's frames + have ck : Sched.CKeep σ σ := ⟨fun q y n hq => ⟨y, F.sc.2 hq, rfl⟩, + fun q x hq => ⟨x, (F.look (fun _ e => by cases e)).2 hq, rfl, rfl⟩, fun p x n hp => ⟨x, F.sc.1 hp⟩⟩ + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hkb ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 F.khrb + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s1 σ := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => F.szKeep q.bv y n hq) hkb hirq (J.idleW_keep hX1.fin.1.1.1 F.idle F.isc).2⟩ + have hkern := J.kern_keepA hkb hcpu hX1.fin.1.1.1 F hDbg + have hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s1 := dbgKeep_of_writes J.blk hW fun b hb => by + subst hb + refine J.notDbg (fun t ht => ?_) hKn + rcases hAp t .tcb ht (fun _ e => by cases e) with e | e + · left; omega + · right; simpa [P1.HKind.bits] using e + have hTd : Sched.TdKeep (L := L) H s s1 := fun q hq => + tcbK q (J.tcbZ hq) _ (P1.field_aligned (J.ip.e.tcb q (J.tcbZ hq)) (K := 3528#u64) (by simp) (by simp)) + (by rw [J.tdA_val' (J.tcbZ hq)]; omega) (by rw [J.tdA_val' (J.tcbZ hq)]; omega) + have R : JRestR (L := L) arI { Z with G := Z.G } H Cs Ac Lst D s1 σ := + ⟨hCR', hRF', hRR', J.yld.frame k2 yk, J.mcp.frame k1 (fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩), + J.buf.frame kb (fun p x hp => ⟨x, (F.look (fun _ e => by cases e)).2 hp, rfl⟩), hRP', + J.apart.sc_frame hkb F.szKeep, hjpl, J.jcov_keep hkb hSS hdead (J.idleW_keep hX1.fin.1.1.1 F.idle F.isc) (DXKeep.of_dbg hKd hTd), hkern, + hutc, J.nsc.ar F, J.inv.ar F, (fun e => (J.cur (by rw [← F.act]; exact e)).ar F), J.own.ar F, + J.off.ar F, hAI, J.bsa.ar F, J.isorted.ar F, J.curRun.ar F, hblk⟩ + exact J.cs_frameK (hvn := J.vsSS (G' := Z.G) hX1 hkb hSS (Vs.ArchShr.refl _)) hkb hX1 F.kback F.ipcFr (J.carry.frame subset_rfl F.ipcFr F.ksz) (JCs.sameBR J hSS F.ksz) R (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) + hSS hKd F.idle + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Fl4Proofs.P1 in +/-- A write to a frame word outside the IPC buffers. -/ +theorem JAtZRB.frameWrJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {w : U64} + (hu : UserW (L := L) Z s σ w) (hnB : ∀ wp ∈ Z.B, wp.1 ≠ w) + (hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → w.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ w.val) + (hObj : ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ w.val ∧ w.val < o.toNat + 2 ^ Fl4Spec.objBits ko) + (hks : w.val + 8 ≤ (L.ksb s).val ∨ (L.ksb s).val + 304384 ≤ w.val) (v : U64) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s w v) a σ := by + have hKn : ∀ c ∈ Cs, w.val + 8 ≤ L.nodeBase s c ∨ L.nodeBase s c + 4288 ≤ w.val := by + intro c hc + have hcl := J.kern.csLt c hc + have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + omega + have hP := J.xtop.fin.1.2 + have hSS := slotsSame_wr (st := st) hP.pl s v hu.al fun _ hx ⟨h1, h2⟩ => + hu.vobj (vobj_of_slot hP.pl Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.ksb s) hx h1 h2) + exact J.frameWrJCore hu.al hAp hObj hKn (by omega) v hSS (J.xtop.userWr hu hnB v) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- **`unblock_check` at `J`, the SC's thread in no release queue** (module +docstring). (mmuAD gen 42b67) Over `JAtZRB` in place, the post extended by the domain time, the spec frame `ScTKw` +and FL4's `TFoot` writes (P3/JAtZV3SigTail's `JAtZRB.unblockJR`, now its corollary). -/ +theorem JAtZRB.unblockRJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hnr : t.bv ∉ σ.releaseQueue) + {p : U64} (hp : p ∈ Z.C) (hxsc : x.tcbSchedContext = some p.bv) (hpc : p.bv ≠ σ.curSc) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap p.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) : + sporadic.unblock_check st s p ⦃ s' => ∃ σ', Fl4Spec.refillUnblockCheck (Z := Unit) p.bv σ = ({((), σ')}, False) ∧ + (∀ q, q ≠ p.bv → σ'.kheap q = σ.kheap q) ∧ σ'.curThread = σ.curThread ∧ + σ'.idleThread = σ.idleThread ∧ σ'.releaseQueue = σ.releaseQueue ∧ + (∃ z, σ'.kheap p.bv = some (.schedContext z n) ∧ z.scRefillMax = y.scRefillMax ∧ z.scTcb = y.scTcb) ∧ + σ'.domainTime = σ.domainTime ∧ ScTKw (fun _ => False) σ σ' ∧ P1.WritesIn st s s' (TFoot st Z.T Z.N Z.C Z.c s σ) ∧ + ∃ Lst' : U64 → List U64, JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s' a σ' ⦄ := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR : NtfnStateRel Z.N Z.T (L.mem s) σ := hX.fin.1.1.2.2.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨xt, hkt, -, -, hsct, -⟩ := hs.tcb t ht + rw [hk] at hkt; cases hkt + have h0 : L.mem s (scA t) ≠ 0#u64 := (Notif.optRef_some (hsct.symm.trans hxsc)).1 + have hst : L.mem s (scA t) = p := P1.eq_of_bv (Notif.optRef_some (hsct.symm.trans hxsc)).2 + obtain ⟨y', n', hky', hy'⟩ := hs.scs p hp + rw [hky] at hky'; cases hky' + have hmx : (L.rmax s p).val ≠ 0 := hy'.rmax_ne hya + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + obtain ⟨⟨Q1, R1, hq1⟩, hB, hXi, hSc, hSg⟩ := J.blk + obtain ⟨hiS, -, -⟩ := hXi.2.2.bound t htH h0 + rw [hst] at hiS + have hiA : p ∈ Ac := (hSc.act _ hiS).2 (fun e => hmx (by rw [e]; rfl)) + have hp0 : p ≠ 0#u64 := fun e => h0 (by rw [hst]; exact e) + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (unblock_check_refines s σ hS hp hmx) + (unblock_check_kfoot hS hp hmx)) + (Fl4Proofs.P1.sporadic.unblock_check.all s t p hq1 hB hXi hSc hSg htH hst.symm hp0 hiA)) + (Fl4Proofs.P1.sporadic.unblock_check.kw (Es := P1.EH H) s t p hq1 hB hXi hSc hSg htH hst.symm hp0 hiA)) ?_ + rintro s' ⟨⟨⟨⟨σ', he, hfe, hkv, hS'⟩, hw⟩, hq2, hB2, hX2, hS2, hSg2, -⟩, hfoot⟩ + have mem : ((), σ') ∈ (Fl4Spec.refillUnblockCheck (Z := Unit) p.bv σ).1 := by rw [he]; exact Set.mem_singleton _ + have hne : y.scRefills ≠ [] := J.inv.ne _ y n hky hya + have hF : (∀ q, q ≠ p.bv → σ'.kheap q = σ.kheap q) ∧ σ'.curThread = σ.curThread ∧ σ'.curSc = σ.curSc ∧ + σ'.curTime = σ.curTime ∧ σ'.consumedTime = σ.consumedTime ∧ σ'.releaseQueue = σ.releaseQueue ∧ + σ'.idleThread = σ.idleThread ∧ σ'.schedulerAction = σ.schedulerAction ∧ + ∃ z, σ'.kheap p.bv = some (.schedContext z n) ∧ z.scRefillMax = y.scRefillMax ∧ z.scTcb = y.scTcb := by + rcases unblock_result hky hne mem with ⟨rfl, -⟩ | ⟨z, rfl, -, -, hz, -⟩ + · exact ⟨fun _ _ => rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, y, hky, rfl, rfl⟩ + · exact ⟨fun q hq => by simp [updK, hq], rfl, rfl, rfl, rfl, rfl, rfl, rfl, z, by simp [updK], + by rw [hz], by rw [hz]⟩ + obtain ⟨hkp, e_ct, e_csc, e_tm, e_cons, e_rel, e_idle, e_act, z, hkz, hzm, hzt⟩ := hF + have e_dt : σ'.domainTime = σ.domainTime := by + rcases unblock_result hky hne mem with ⟨rfl, -⟩ | ⟨z, rfl, -⟩ + · rfl + · rfl + refine ⟨σ', he, hkp, e_ct, e_idle, e_rel, ⟨z, hkz, hzm, hzt⟩, e_dt, ?_⟩ + -- the spec frame + have hK0 : ScTKw (fun _ => False) σ σ' := nkt_refillUnblockCheck p.bv σ _ mem + have hK : ScTKw (fun q => q = t.bv) σ σ' := hK0.ofFalse + have hL : LK (fun _ => False) σ σ' := lkm_refillUnblockCheck p.bv σ _ mem + have hFK : FK σ σ' := ⟨e_csc, e_idle, hK0.isc⟩ + have hsok : SOK (true, true) σ σ' := pres_refillUnblockCheck (f := (true, true)) p.bv σ _ mem + have hBK : BK σ σ' := bkm_of (nkt_refillUnblockCheck _) (MaxK.mk_refillUnblockCheck' _) σ _ mem + have hwT := WritesIn.tfoot_of_kfoot hw + have hR' : NtfnStateRel Z.N Z.T (L.mem s') σ' := hR.frame hS.placed hfe hkv + have hX' : XTopAt (L := L) AR Z s' a σ' := xtop_sc_step hX J.ip J.nk hwT hK hS' hR' id + have hN' : NSC Z.N Z.C σ' := nsc_of_lk J.nsc hK hL + -- the invariant: the only thread bound to `p` is `t`, in no release queue + have hI' : Inv σ' := by + refine J.inv.unblock hky hne (fun _ _ u hu xu hku hsu => ?_) mem + obtain ⟨y1, n1, hk1, hyt1⟩ := J.inv.back u xu p.bv hku hsu + obtain ⟨y2, n2, hk2, hyt2⟩ := J.inv.back t.bv x p.bv hk hxsc + rw [hk1] at hk2; cases hk2 + have hut : u = t.bv := Option.some.inj (hyt1.symm.trans hyt2) + subst hut + exact hnr hu + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree ⟨⟨hS', hR', hN', hI'⟩, hw, hsok⟩ + have hkcs : σ'.kheap σ'.curSc = σ.kheap σ.curSc := by rw [e_csc]; exact hkp _ (Ne.symm hpc) + have htk : ∀ q xq, σ'.kheap q = some (.tcb xq) → σ.kheap q = some (.tcb xq) := by + intro q xq h + by_cases e : q = p.bv + · rw [e, hkz] at h; cases h + · rwa [hkp q e] at h + have htk' : ∀ q xq, σ.kheap q = some (.tcb xq) → σ'.kheap q = some (.tcb xq) := by + intro q xq h + by_cases e : q = p.bv + · rw [e, hky] at h; cases h + · rwa [hkp q e] + have hC' : CurInvR σ' := by + intro er + cases (trivial : True) + · have hC : CurInv σ := J.cur (by rw [← e_act]; exact er) + obtain ⟨xc, hkc, hcs, hnb⟩ := hC.tcb + refine ⟨⟨xc, by rw [e_ct]; exact htk' _ _ hkc, by rw [e_csc]; exact hcs, hnb⟩, ?_, ?_, ?_⟩ + · rw [hkcs]; exact hC.sc + · intro hne' y2 n2 hy; rw [hkcs] at hy; rw [e_csc, hK0.isc] at hne'; exact hC.active hne' y2 n2 hy + · rw [e_ct, e_rel]; exact hC.notRel + have hown' : CurOwnW Z.T Z.C σ' := by + rcases J.own with ⟨u, hu, xu, hku, hxs, hown, hnr'⟩ | ⟨hcs, hun⟩ + · exact Or.inl ⟨u, hu, xu, htk' _ _ hku, by rw [e_csc]; exact hxs, + fun y1 n1 hy => hown y1 n1 (by rw [← hkcs]; exact hy), by rw [e_cons, e_rel]; exact hnr'⟩ + · exact Or.inr ⟨by rw [e_csc]; exact hcs, fun y1 n1 hy => hun y1 n1 (by rw [← hkcs]; exact hy)⟩ + have hoff : OffOK σ' := by + intro h0' y1 n1 hy ha + rw [e_cons] at h0' ⊢; rw [e_tm]; rw [hkcs] at hy; rw [e_csc, hK0.isc] at ha + exact J.off h0' y1 n1 hy ha + have hAI : ActIdle σ' := J.act.unblock hky hne mem + have hcr : CurRunM σ' := by + rcases J.curRun with h | h + · left; rw [e_act]; exact h + · right + rcases h with h | h + · left; intro y1 hy; rw [e_ct] at hy; exact h y1 (htk _ _ hy) + · right; rw [e_ct, e_idle]; exact h + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_stepQ hX' hwT hK0 (fun _ hp => hp.elim) hL hFK hAI.1 hY' ((hsok.ykeep rfl).dk q.bv) (fun h => h) (J.jcov.dead q hq) + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + exact ⟨hK0, hwT, Lst', J.sc_frame hwT hK hX' hCR' hRF' hY' hM' (J.sc_buf hwT hK) hRP' (J.sc_apart hwT hK) + (J.sc_jpl hwT hK (J.idleW_keep hX'.fin.1.1.1 hFK.2.1 hFK.2.2).2) (J.sc_jcov hwT hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hFK.2.1 hFK.2.2) hKd subset_rfl) + (J.sc_kern hwT hK hFK hX' hDbg fun _ h => h) (J.sc_utc hwT hK hDbg) hN' hI' hC' hown' hoff hAI + (J.bsa.bk hBK hsok) hcr ⟨⟨Q1, R1, hq2⟩, hB2, hX2, hS2, hSg2⟩ hKd (J.idleW_keep hX'.fin.1.1.1 hFK.2.1 hFK.2.2).1 J.zD⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **The five side facts of a VCPU word** (module docstring; mmuAD gen 42b18: out of `vcpuWrJ`'s proof, so that +`JAtZRB.vcpuWrJ` and `JAtZ.vcpuWrJ` are both `frameWrJ` after it): `UserW`, apart from `H`'s non-`other` objects, +inside the spec VCPU, off the kernel-global area. -/ +theorem JAtZRB.vcpuWr_side (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p w : U64} + (hσ : ∃ x, σ.kheap p.bv = some (.archObj (.vcpu x))) (hH : H p = some (.other 15)) + (hks : p.val + 32768 ≤ (L.ksb s).val ∨ (L.ksb s).val + 304384 ≤ p.val) + (hal : P1.Aligned w) (g1 : p.val ≤ w.val) (g2 : w.val < p.val + 32768) + (hnB : ∀ wp ∈ Z.B, wp.1 ≠ w) : + UserW (L := L) Z s σ w ∧ + (∀ q k, H q = some k → (∀ z, k ≠ .other z) → w.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ w.val) ∧ + (∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ w.val ∧ w.val < o.toNat + 2 ^ Fl4Spec.objBits ko) ∧ + (w.val + 8 ≤ (L.ksb s).val ∨ (L.ksb s).val + 304384 ≤ w.val) := by + obtain ⟨x, hσx⟩ := hσ + have hHO := J.kern.heap + have h15 : (2 : Nat) ^ 15 = 32768 := by norm_num + have hw8 : w.val % 8 = 0 := hal + have hpa : p.val % 32768 = 0 := by + have := hHO.align p _ hH; simpa [P1.HKind.bits, h15] using this + -- apart from every other `H` object + have wap : ∀ q k', H q = some k' → q ≠ p → w.val + 8 ≤ q.val ∨ q.val + 2 ^ k'.bits ≤ w.val := by + intro q k' hq hne + rcases hHO.disj p _ q k' hH hq (Ne.symm hne) with h | h + · simp only [P1.HKind.bits, h15] at h; left; omega + · right; omega + have hneK : ∀ q k', H q = some k' → (∀ z, k' ≠ .other z) → q ≠ p := fun q k' hq hk' e => by + subst e; rw [hH] at hq; exact hk' 15 (Option.some.inj hq).symm + have hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → w.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ w.val := + fun q k hq hk => wap q k hq (hneK q k hq hk) + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hR := J.xtop.rel.2.1.1 + -- the typed heap's CNodes are spec CNodes, so not the VCPU + have hcn : ∀ q r, Z.th q = some (.cnode r) → w.val + 8 ≤ q.val ∨ q.val + 32 * 2 ^ r ≤ w.val := by + intro q r hq + have hne : q ≠ p := fun e => by + subst e; obtain ⟨cs, hc, -⟩ := hR.cnode q r hq; rw [hc] at hσx; cases hσx + have e32 : 2 ^ (r + 5) = 32 * 2 ^ r := by rw [pow_add]; norm_num; ring + have := wap q _ (hJ.cnode q r hq) hne + simp only [P1.HKind.bits] at this; rw [e32] at this; exact this + have htc : ∀ t, Z.th t = some .tcb → w.val + 8 ≤ t.val ∨ t.val + 4096 ≤ w.val := by + intro t ht + have hHt := (hJ.tcb t).1 ht + have := hAp t _ hHt fun _ e => by cases e + simpa [P1.HKind.bits] using this + -- the sets' tables and pools are spec tables / pools, so not the VCPU + have hF := J.xtop.fin + have hvs : ∀ q', (q' ∈ Z.P ∨ q' ∈ Z.L4 ∨ q' ∈ Z.L3 ∨ q' ∈ Z.L2 ∨ q' ∈ Z.L1) → + w.val + 8 ≤ q'.val ∨ q'.val + 4096 ≤ w.val := by + intro q' hq' + obtain ⟨n', hn', hH'⟩ := J.vsFr_other hq' + have hne : q' ≠ p := by + intro e; subst e + rcases hq' with hv | hv | hv | hv | hv + · obtain ⟨f, hf, -⟩ := hF.2.1.pool q' hv; rw [hf] at hσx; cases hσx + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l4 q' hv; rw [hf] at hσx; cases hσx + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l3 q' hv; rw [hf] at hσx; cases hσx + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l2 q' hv; rw [hf] at hσx; cases hσx + · obtain ⟨f, hf, -⟩ := hF.2.2.1.l1 q' hv; rw [hf] at hσx; cases hσx + have h4 : 2 ^ 12 ≤ 2 ^ n' := Nat.pow_le_pow_right (by decide) hn' + rcases wap q' _ hH' hne with h | h + · exact Or.inl h + · simp only [P1.HKind.bits] at h; right; omega + -- core `Z.c`'s node lies below the ASID-table area + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hcq.cur ▸ J.kern.cpuCs + have hc64 := J.kern.csLt Z.c hcC + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + have hksw : w.val + 8 ≤ (L.ksb s).val ∨ (L.ksb s).val + 304384 ≤ w.val := by omega + have hu : UserW (L := L) Z s σ w := + { al := hal + far := ⟨by rw [hnb]; omega, + fun t ht => by + have := hAp t _ (J.zH.1 t ht) fun _ e => by cases e + simpa [P1.HKind.bits] using this, + fun sc hsc y n hky => by + have hHs := (hJ.sc sc y n (J.c0 sc hsc) hky).2 + have := hAp sc _ hHs fun _ e => by cases e + simpa [P1.HKind.bits] using this⟩ + vobj := fun hv => by + rcases hv with ⟨q, r, hq, c1, c2⟩ | ⟨t, ht, c1, c2⟩ | ⟨q', hq', c1, c2⟩ | h + · have := hcn q r hq; omega + · have := htc t ht; omega + · have := hvs q' hq'; omega + · omega + ep := fun e he => by + have := hAp e _ (J.zH.2.1 e he) fun _ e => by cases e + simpa [P1.HKind.bits] using this + rep := fun r hr => by + have := hAp r _ (J.zH.2.2.1 r hr) fun _ e => by cases e + simpa [P1.HKind.bits] using this + ntfn := fun n hn => by + have := hAp n _ (J.zH.2.2.2 n hn) fun _ e => by cases e + simpa [P1.HKind.bits] using this } + have hObj : ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ w.val ∧ w.val < o.toNat + 2 ^ Fl4Spec.objBits ko := + ⟨p.bv, _, hσx, g1, by + show w.val < p.val + 2 ^ Fl4Spec.archKobjSize (.vcpu x) + simp only [Fl4Spec.archKobjSize, h15]; exact g2⟩ + exact ⟨hu, hAp, hObj, hksw⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A word of a VCPU written at `J`** (module docstring; the spec state kept). -/ +theorem JAtZRB.vcpuWrJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {p w : U64} + (hσ : ∃ x, σ.kheap p.bv = some (.archObj (.vcpu x))) (hH : H p = some (.other 15)) + (hks : p.val + 32768 ≤ (L.ksb s).val ∨ (L.ksb s).val + 304384 ≤ p.val) + (hal : P1.Aligned w) (g1 : p.val ≤ w.val) (g2 : w.val < p.val + 32768) + (hnB : ∀ wp ∈ Z.B, wp.1 ≠ w) (v : U64) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s w v) a σ := by + obtain ⟨hu, hAp, hObj, hksw⟩ := J.vcpuWr_side hσ hH hks hal g1 g2 hnB + exact J.frameWrJ hu hnB hAp hObj hksw v + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- A typed-heap object is an `H` object of the same size. -/ +theorem JAtZRB.thH (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ p k, Z.th p = some k → ∃ hk, H p = some hk ∧ 2 ^ hk.bits = objSize k := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + intro p k hp + rcases J.thk p k hp with rfl | ⟨r, rfl⟩ + · exact ⟨_, (hJ.tcb p).1 hp, by simp [P1.HKind.bits, objSize]⟩ + · exact ⟨_, hJ.cnode p r hp, by simp only [P1.HKind.bits, objSize]; rw [pow_add]; ring⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- **The layout record at a joint state** (module docstring). -/ +theorem JAtZRB.jvLay (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) : + JVLay V Z.th (BufU Z σ) := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH := J.kern.heap + have hSR := J.xtop.rel.2.1.1 + have h15 : (2 : Nat) ^ 15 = 32768 := by norm_num + have hvn : ∀ v ∈ V, Z.th v = none := by + intro v hv + obtain ⟨x, hx⟩ := Jv.spec v hv + cases h : Z.th v with + | none => rfl + | some k => + exfalso + rcases J.thk v k h with rfl | ⟨r, rfl⟩ + · obtain ⟨c, hc, -⟩ := hSR.tcb v h + rw [hc] at hx; cases hx + · obtain ⟨c, hc, -⟩ := hSR.cnode v r h + rw [hc] at hx; cases hx + refine ⟨J.xtop.rel.1, J.thk, fun t ht => J.ip.e.tcb t (J.thT t ht), fun v hv => ?_, fun v hv p k hp => ?_, + fun v hv w hw hne => ?_, fun b hb => (J.bufU_rbuf hb).1.1, fun b hb y hy => J.bufU_noSlot y hy b hb, + fun b hb t ht => ?_, fun b hb v hv => ?_⟩ + · have h1 := hH.align v _ (Jv.heap v hv); have h2 := hH.fits v _ (Jv.heap v hv) + simp only [P1.HKind.bits, h15] at h1 h2; exact ⟨h1, h2⟩ + · obtain ⟨hk, hpk, he⟩ := J.thH p k hp + have hne : p ≠ v := fun e => by rw [e, hvn v hv] at hp; cases hp + rcases hH.disj p hk v _ hpk (Jv.heap v hv) hne with e | e + · left; rw [← he]; exact e + · right; simpa [P1.HKind.bits, h15] using e + · rcases hH.disj v _ w _ (Jv.heap v hv) (Jv.heap w hw) hne with e | e <;> + simp only [P1.HKind.bits, h15] at e + · exact Or.inl e + · exact Or.inr e + · have h12 : (2 : Nat) ^ 12 = 4096 := by norm_num + have := (J.bufU_rbuf hb).1.2.1 t ((hJ.tcb t).1 ht) + rw [h12] at this; exact this + · obtain ⟨wp, hwp, rfl⟩ := J.bufU_B hb + obtain ⟨q, vms, hq, g1, g2⟩ := J.carry.bufPg wp hwp + obtain ⟨x, hx⟩ := Jv.spec v hv + have hne : q ≠ v.bv := fun e => by rw [e, hx] at hq; cases hq + rcases Jv.sep v hv q _ hq hne with e | e + · left + have eb : Fl4Spec.objBits (.archObj (.dataPage false vms)) = Fl4Spec.pageBitsForSize vms := by + simp [Fl4Spec.objBits, Fl4Spec.archKobjSize] + rw [eb] at e; omega + · right; omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- **`JWPl` at a joint state.** -/ +theorem JAtZRB.jwPl (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) : + JWPl V Z.th := (J.jvLay Jv).jwPl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- **`DetPl` at a joint state**, at the IPC-buffer words `BufU Z σ`. -/ +theorem JAtZRB.detPl (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) : + DetPl V Z.th (fun _ => BufU Z σ) := (J.jvLay Jv).detPl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- The current core's node is placed (`JKern`'s `ksbAl` / `nodeFit` / `hnd`). -/ +theorem JAtZRB.nodePl (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + P1.NodePlaced st (P1.Heap.tcbs H) s (L.cpu s) where + align := by unfold P1.LawfulStore.nodeBase; have := J.kern.ksbAl; omega + fits := by unfold P1.LawfulStore.nodeBase; have := J.kern.nodeFit; omega + disj t ht := by + have := J.kern.hnd t .tcb ht (L.cpu s) J.kern.cpuCs + simpa [P1.HKind.bits] using this + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- A word of the current core's node is none of `JW` / `SW`. -/ +theorem JAtZRB.nodeW (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {b : U64} + (h1 : L.nodeBase s (L.cpu s) ≤ b.val) (h2 : b.val < L.nodeBase s (L.cpu s) + 4288) : + ¬ JW V Z.th b ∧ ¬ SW Z.th b := by + have h15 : (2 : Nat) ^ 15 = 32768 := by norm_num + refine (J.jvLay Jv).nJS (fun p k hp g1 g2 => ?_) (fun v hv => ?_) + · exfalso + obtain ⟨hk, hpk, he⟩ := J.thH p k hp + rcases J.kern.hnd p hk hpk (L.cpu s) J.kern.cpuCs with e | e <;> (try rw [he] at e) <;> omega + · have := ((J.jvLay Jv).vfit v hv).2 + rcases J.kern.hnd v _ (Jv.heap v hv) (L.cpu s) J.kern.cpuCs with e | e <;> + (try simp only [P1.HKind.bits, h15] at e) <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- A TCB word at an offset outside the register range, other than the VCPU / state words, is no binding word +and no detach word `CW` (what the IPC-buffer lookup's three words need). -/ +theorem JAtZRB.tw_nBC (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {t : U64} + (ht : Z.th t = some .tcb) {w : U64} {K : Nat} (hw : w.val = t.val + K) (hK : K < 4096) + (hK2 : K < 256 ∨ 3328 < K) (hK3 : K ≠ 3664) (hK8 : K % 8 = 0) : + P1.Aligned w ∧ ¬ (BW V Z.th w ∨ CW st V Z.th (fun _ => BufU Z σ) s w) := by + have Ly := J.jvLay Jv + have hta := Ly.tA t ht + have ha := hta.align + have dT : ∀ t', Z.th t' = some .tcb → t' ≠ t → t'.val + 4096 ≤ t.val ∨ t.val + 4096 ≤ t'.val := by + intro t' ht' hne + have h1 := (Ly.tA t' ht').align + have : t'.val ≠ t.val := fun e => hne (UScalar.eq_of_val_eq e) + omega + have hN := J.nodePl + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have nodeT := hN.disj t ((hJ.tcb t).1 ht) + have h12 : (2 : Nat) ^ 12 = 4096 := by norm_num + rw [h12] at nodeT + refine ⟨by unfold P1.Aligned; rw [hw]; omega, ?_⟩ + rintro ((⟨t', ht', rfl⟩ | ⟨v, hv, rfl⟩) | (e | e | ⟨t', ht', h | e⟩ | ⟨v, hv, e | e⟩)) + · rw [lay_tVcpu_val (Ly.tA t' ht')] at hw + by_cases e : t' = t + · subst e; omega + · rcases dT t' ht' e with e' | e' <;> omega + · rw [lay_vTcb_val (Ly.vfit v hv).2] at hw + rcases Ly.vap v hv t .tcb ht with e | e <;> (try rw [show objSize ObjKind.tcb = 4096 from rfl] at e) <;> omega + · rw [e] at hw + have := P1.nodeAt_val hN (off := layout.node.IPI_PENDING) (by simp [layout.node.IPI_PENDING]) + have h4 : layout.node.IPI_PENDING.val < 4288 := by simp [layout.node.IPI_PENDING] + unfold ipiA at hw; rw [this] at hw + rcases nodeT with e' | e' <;> omega + · rw [e] at hw + have := P1.nodeAt_val hN (off := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + have h4 : layout.node.SCHED_ACTION.val < 4288 := by simp [layout.node.SCHED_ACTION] + unfold actA at hw; rw [this] at hw + rcases nodeT with e' | e' <;> omega + · rcases h with ⟨K', g1, g2, g3, rfl⟩ | hb + · rw [lay_tw_val (Ly.tA t' ht') (by omega)] at hw + by_cases e : t' = t + · subst e; omega + · rcases dT t' ht' e with e' | e' <;> omega + · rcases Ly.ut w hb t ht with e' | e' <;> omega + · rw [e, lay_stA_val (Ly.tA t' ht')] at hw + by_cases e' : t' = t + · subst e'; omega + · rcases dT t' ht' e' with e'' | e'' <;> omega + · rw [e, lay_pend_val (Ly.vfit v hv).2] at hw + rcases Ly.vap v hv t .tcb ht with e' | e' <;> (try rw [show objSize ObjKind.tcb = 4096 from rfl] at e') <;> omega + · rw [e, lay_svm_val (Ly.vfit v hv).2] at hw + rcases Ly.vap v hv t .tcb ht with e' | e' <;> (try rw [show objSize ObjKind.tcb = 4096 from rfl] at e') <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- **`DetAtB` at a joint state** (P3/JExtVStepsB): the node placed (`JAtZ.nodePl`), its IPI / action words +outside the extension (`JAtZ.nodeW`), and the buffer lookup kept along binding / `CW` writes (it reads only the +TCB's `IPC_BUFFER` word and buffer-slot words, `P1.lookup_ipc_buffer_same`), landing in `BufU` (`JAtZ.bufOK`). -/ +theorem JAtZRB.detAtB (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) : + DetAtB st V Z.th (fun _ => BufU Z σ) s where + node := ⟨_, J.nodePl⟩ + nd b hb := by + have hN := J.nodePl + rcases hb with rfl | rfl + · have e := P1.nodeAt_val hN (off := layout.node.IPI_PENDING) (by simp [layout.node.IPI_PENDING]) + have h4 : layout.node.IPI_PENDING.val < 4288 := by simp [layout.node.IPI_PENDING] + unfold ipiA; exact J.nodeW Jv (by rw [e]; omega) (by rw [e]; omega) + · have e := P1.nodeAt_val hN (off := layout.node.SCHED_ACTION) (by simp [layout.node.SCHED_ACTION]) + have h4 : layout.node.SCHED_ACTION.val < 4288 := by simp [layout.node.SCHED_ACTION] + unfold actA; exact J.nodeW Jv (by rw [e]; omega) (by rw [e]; omega) + buf s1 h t ht := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hta := (J.jvLay Jv).tA t ht + have e1 : (P1.ibWordA t).val = t.val + 3512 := by + unfold P1.ibWordA; rw [lay_tw_val hta (by simp)]; simp + have e2 : (P1.sbWordA t).val = t.val + 64 := by + rw [P1.sbWordA_eq, lay_tw_val hta (by simp)]; simp + have e3 : (core.num.U64.wrapping_add (P1.sbWordA t) 8#u64).val = t.val + 72 := by + rw [P1.sbWordA8_eq, lay_tw_val hta (by simp)]; simp + have k1 := J.tw_nBC Jv ht e1 (by omega) (by omega) (by omega) (by omega) + have k2 := J.tw_nBC Jv ht e2 (by omega) (by omega) (by omega) (by omega) + have k3 := J.tw_nBC Jv ht e3 (by omega) (by omega) (by omega) (by omega) + have hI : P1.IbSame (L := L) s s1 t := ⟨h.mem_eq k1.1 k1.2, h.mem_eq k2.1 k2.2, h.mem_eq k3.1 k3.2⟩ + rw [P1.lookup_ipc_buffer_same hI] + exact J.bufOK ((hJ.tcb t).1 ht) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open fl4_core Classical + +/-- **Any word of a VCPU of `V` written at `JAtZRB`** (gen 42b43: `vcpuWrV`'s `JAtZRB` half, out of its proof, so that +the VCPU-list link writes of `vcpu::finalise` use it; no offset excluded). -/ +theorem JAtZRB.vcpuWrB (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {p w : U64} + (hp : p ∈ V) (hal : P1.Aligned w) (g1 : p.val ≤ w.val) (g2 : w.val < p.val + 32768) (x : U64) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s w x) a σ := by + -- no IPC-buffer word lies in the VCPU + have hnB : ∀ wp ∈ Z.B, wp.1 ≠ w := by + intro wp hwp e + obtain ⟨q, vms, hq, q1, q2⟩ := B.carry.bufPg wp hwp + obtain ⟨y, hy⟩ := Jv.spec p hp + have hne : q ≠ p.bv := fun e' => by rw [e', hy] at hq; cases hq + have eb : Fl4Spec.objBits (.archObj (.dataPage false vms)) = Fl4Spec.pageBitsForSize vms := by + simp [Fl4Spec.objBits, Fl4Spec.archKobjSize] + have ev : wp.1.val = w.val := by rw [e] + rcases Jv.sep p hp q _ hq hne with e1 | e1 + · rw [eb] at e1; omega + · omega + exact B.vcpuWrJ (Jv.spec p hp) (Jv.heap p hp) (Jv.ks p hp) hal g1 g2 hnB x + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open fl4_core Classical + +set_option maxHeartbeats 4000000 in +/-- **A VCPU word written at `JAtZRB`, with `RunVmB`, `JVm` and `VList`** (gen 42b42: `JAtZV.vcpuWr`'s proof +without its `JExtV` step, shared with P3/JAtZV3's `VmBaseR.vcpuWr`; `JAtZV.vcpuWr` is its corollary). -/ +theorem JAtZRB.vcpuWrV (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) + (R : RunVmB (L := L) Z.th s) (Vl : VList st V Z.th s) {p w : U64} (hp : p ∈ V) + (hal : P1.Aligned w) (g1 : p.val ≤ w.val) (g2 : w.val < p.val + 32768) + (hw : w.val ≠ p.val + 20480 ∧ w.val ≠ p.val + 20488 ∧ w.val ≠ p.val + 20496) (x : U64) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s w x) a σ ∧ RunVmB (L := L) Z.th (L.wr s w x) ∧ + JVm (L := L) V H (L.wr s w x) σ ∧ VList st V Z.th (L.wr s w x) := by + have Ly := B.jvLay Jv + have hW : P1.WritesIn st s (L.wr s w x) (· = w) := P1.writesIn_wr hal + have B' := B.vcpuWrB Jv hp hal g1 g2 x + have hHO := B.kern.heap + have apT : ∀ t, Z.th t = some .tcb → t.val + 4096 ≤ p.val ∨ p.val + 32768 ≤ t.val := by + intro t ht + have htH : H t = some .tcb := B.zH.1 t (B.thT t ht) + have hne : t ≠ p := fun e => by rw [e, Jv.heap p hp] at htH; cases htH + have d := hHO.disj t _ p _ htH (Jv.heap p hp) hne + simp only [P1.HKind.bits, Nat.reducePow] at d + exact d + have kT : ∀ t, Z.th t = some .tcb → L.mem (L.wr s w x) (P1.stA t) = L.mem s (P1.stA t) ∧ + L.mem (L.wr s w x) (tVcpuA t) = L.mem s (tVcpuA t) := by + intro t ht + have hta := B.ip.e.tcb t (B.thT t ht) + have e1 := lay_stA_val hta + have e2 := lay_tVcpu_val hta + have ha := hta.align + have d := apT t ht + refine ⟨hW.mem_eq (by unfold P1.Aligned; omega) fun e => ?_, hW.mem_eq (by unfold P1.Aligned; omega) fun e => ?_⟩ + · have := congrArg UScalar.val e; omega + · have := congrArg UScalar.val e; omega + have R' : RunVmB (L := L) Z.th (L.wr s w x) := by + intro t ht h8 + rw [(kT t ht).2] + exact R t ht (by rw [← (kT t ht).1]; exact h8) + have hv : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val B.kern.ksbFit + have Vl' : VList st V Z.th (L.wr s w x) := Vl.writes hW fun b hb => by + refine ⟨Vl.lw_al Ly.vfit hb, fun e => ?_⟩ + have ew := congrArg UScalar.val e + rcases hb with rfl | ⟨u, hu, rfl | rfl⟩ + · rcases Jv.ks p hp with h | h <;> omega + · rw [lay_next_val (Ly.vfit u hu).2] at ew + by_cases hup : u = p + · subst hup; exact hw.2.1 ew.symm + · rcases Ly.vv u hu p hp hup with h | h <;> omega + · rw [lay_prev_val (Ly.vfit u hu).2] at ew + by_cases hup : u = p + · subst hup; exact hw.2.2 ew.symm + · rcases Ly.vv u hu p hp hup with h | h <;> omega + have Jv' : JVm (L := L) V H (L.wr s w x) σ := + ⟨Jv.spec, Jv.heap, fun v hv => by rw [hW.1]; exact Jv.ks v hv, Jv.sep⟩ + exact ⟨B', R', Jv', Vl'⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched + +/-- `OObj` words miss every VCPU of `V` (gen 42b42: `JAtZV.oobj_v`'s proof, moved here from P3/VmStateR; `JAtZV.oobj_v` its corollary). -/ +theorem JAtZRB.oobjV (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {v : U64} + (hv : v ∈ V) {b : U64} (hb : OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) : + b.val < v.val ∨ v.val + 32768 ≤ b.val := by + have hvH := Jv.heap v hv + have dH : ∀ p k, H p = some k → k ≠ .other 15 → p.val + 2 ^ k.bits ≤ v.val ∨ v.val + 32768 ≤ p.val := by + intro p k hp hk + have hne : p ≠ v := fun e => by subst e; rw [hvH] at hp; cases hp; exact hk rfl + rcases B.kern.heap.disj p k v _ hp hvH hne with h | h + · exact Or.inl h + · simp only [P1.HKind.bits, Nat.reducePow] at h; exact Or.inr h + have hks := Jv.ks v hv + rcases hb with ⟨g1, g2⟩ | ⟨g1, g2, -⟩ | ⟨t, ht, g1, g2⟩ | ⟨e, he, g1, g2⟩ | ⟨r, hr, g1, g2⟩ | ⟨n, hn, g1, g2⟩ | + ⟨sc, hsc, x, n, hk, hso⟩ | ⟨wp, hwp, g1, g2⟩ + · obtain ⟨Q, R0, hcq, -⟩ := B.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hcq.cur ▸ B.kern.cpuCs + have hc64 := B.kern.csLt Z.c hcC + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + rw [hnb] at g1 g2 + omega + · omega + · have d := dH t _ (B.zH.1 t ht) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · have d := dH e _ (B.zH.2.1 e he) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · have d := dH r _ (B.zH.2.2.1 r hr) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · have d := dH n _ (B.zH.2.2.2 n hn) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hH := (hJ.sc sc x n (B.c0 sc hsc) hk).2 + have d := dH sc _ hH (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + unfold Sched.ScObj at hso + omega + · obtain ⟨q, vms, hq, q1, q2⟩ := B.carry.bufPg wp hwp + obtain ⟨y, hy⟩ := Jv.spec v hv + have hne : q ≠ v.bv := fun e' => by rw [e', hy] at hq; cases hq + have eb : Fl4Spec.objBits (.archObj (.dataPage false vms)) = Fl4Spec.pageBitsForSize vms := by + simp [Fl4Spec.objBits, Fl4Spec.archKobjSize] + rcases Jv.sep v hv q _ hq hne with e1 | e1 + · rw [eb] at e1; omega + · omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open fl4_core Classical + +/-- **A spec VCPU-object step at `JAtZRB` and `JVm`** (gen 42b42: `JAtZV.vcpuSpec`'s proof without the unchanged +extension parts, shared with P3/JAtZV3's `VmBaseR.vcpuSpec`; `JAtZV.vcpuSpec` is its corollary). -/ +theorem JAtZRB.vcpuSpecV (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {p : U64} + (hp : p ∈ V) {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (vs' : Fl4Spec.VcpuState) + {a' : A} (hX : XTopAt (L := L) AR Z s a' (Notif.updK σ p.bv (.archObj (.vcpu vs')))) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a' (Notif.updK σ p.bv (.archObj (.vcpu vs'))) ∧ + JVm (L := L) V H s (Notif.updK σ p.bv (.archObj (.vcpu vs'))) := by + have F := arFr_vcpu hk vs' + have B' := B.arFrS F hX + have Jv' : JVm (L := L) V H s (Notif.updK σ p.bv (.archObj (.vcpu vs'))) := by + refine Jv.step (fun _ h => h) (fun _ _ => rfl) rfl (fun v hv => ?_) (fun o ko' ho => ?_) + · by_cases e : v.bv = p.bv + · exact ⟨vs', by simp [Notif.updK, e]⟩ + · obtain ⟨x, hx⟩ := Jv.spec v hv + exact ⟨x, by simp [Notif.updK, e, hx]⟩ + · by_cases e : o = p.bv + · subst e + simp only [Notif.updK, Function.update_self, Option.some.injEq] at ho + subst ho + exact Or.inl ⟨_, hk, rfl⟩ + · exact Or.inl ⟨ko', by simpa [Notif.updK, e] using ho, rfl⟩ + exact ⟨B', Jv'⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- Writes missing every VCPU image of `V` keep every VCPU's `TCB` word (gen 42b46: `JAtZB.kVv`'s proof with the +writes' VCPU apartness `nV` as the premise; `kVv` its corollary). -/ +theorem JAtZRB.kVn (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {s' : S} + {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val) : + ∀ u ∈ V, L.mem s' (vTcbA u) = L.mem s (vTcbA u) := by + intro u hu + have hf := (B.jvLay Jv).vfit u hu + have e := lay_vTcb_val hf.2 + exact hw.mem_eq (by unfold P1.Aligned; rw [e]; omega) fun hb => by + rcases nV u hu _ hb with h | h <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +/-- `JAtZRB` at another arch state related to the same CR3. -/ +theorem JAtZRB.archV (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {a' : A} + (hAR : AR a' σ.archState.x64CurrentCr3) : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a' σ := + J.arFrS (arFr_fpuOwner σ σ.machineState σ.archState.x64CurrentFpuOwner) (J.xtop.arch hAR) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **The shared record of the report chain** (gen 42b42, ruling (B); module docstring): `JAtZV3`'s facts without +the cap typing of `X` and the IO-port owner clause — the base, the VCPU half `JExtVcpu` of the extension, and the +seven fields the chain reads. Both `JAtZV3` (`JAtZV3.toB`) and `VmEptStateR` (`VmEptStateR.toB`) give it. -/ +structure VmBaseR (V : Set U64) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop where + base : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ + extv : JExtVcpu (L := L) V Z.th s + rvm : RunVmB (L := L) Z.th s + vm : JVm (L := L) V H s σ + vl : VList st V Z.th s + vb : VBind V σ (L.mem s) + vo : VOnly V σ + vr : VRel V σ (L.mem s) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **`VmBaseR` with the footprint of the run from `(sA, σA)`** (the report chain's pre- and post-states). -/ +structure VmBaseFR (sA : S) (σA : Fl4Spec.State Unit) (V : Set U64) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) + (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : + Prop extends VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ, VmFoot (L := L) V Z sA σA s σ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- A `VmBaseR` is a `VmBaseFR` of the empty run. -/ +theorem VmBaseR.toF (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) : + VmBaseFR (L := L) arI s σ V AR Z H Cs Ac Lst D s a σ := + ⟨J, VmFoot.refl s σ⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **Writes inside the images of `V` or past the slots of a TCB keep every slot.** -/ +theorem JAtZRB.ssV (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {s' : S} + {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hW : ∀ b, W b → (∃ v ∈ V, v.val ≤ b.val ∧ b.val < v.val + 32768) ∨ + ∃ t, Z.th t = some .tcb ∧ t.val + 192 ≤ b.val ∧ b.val < t.val + 4096) : + SlotsSame Z.th (L.mem s) (L.mem s') := by + have Ly := B.jvLay Jv + intro y hy + have hal := hy.align Ly.pl + have hfit := hy.fit Ly.pl + obtain ⟨p, k, hk, g1, g2, gt, -⟩ := lay_slot_in Ly.pl hy + have one : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → L.mem s' (wordOf y o) = L.mem s (wordOf y o) := by + intro o ho ho8 + have hv : (wordOf y o).val = y.val + o.val := wordOf_val (by omega) + refine hw.mem_eq (by unfold P1.Aligned; rw [hv]; omega) fun h => ?_ + rcases hW _ h with ⟨v, hvV, b1, b2⟩ | ⟨t, ht, b1, b2⟩ + · rw [hv] at b1 b2 + have d := Ly.vap v hvV p k hk + omega + · rw [hv] at b1 b2 + by_cases e : t = p + · subst e + rw [hk] at ht + cases ht + have := gt rfl + omega + · have d := Ly.pl.disj t p _ _ ht hk e + rw [show objSize ObjKind.tcb = 4096 from rfl] at d + omega + have h0 : wordOf y 0#u64 = y := by + apply UScalar.eq_of_val_eq; rw [wordOf_val (by simp; omega)]; simp + refine ⟨?_, one 8#u64 (by decide) (by decide), one 16#u64 (by decide) (by decide), + one 24#u64 (by decide) (by decide)⟩ + have := one 0#u64 (by decide) (by decide) + rwa [h0] at this + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **`VmBaseR` across a native step that may change the spec heap, from the slots kept and the writes' VCPU +apartness** (gen 42b46: `VmBaseR.ofBSR`'s proof with `hSS` / `nV` for `hO`, the end state at any `Z.mv Z.G V'`, for +P3/NatFoot's `TmNode` words; `VmBaseR.ofBSR` its corollary). -/ +theorem VmBaseR.ofBSSCoreR (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hRun : RunVmB (L := L) Z.th s') + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VmBaseR (L := L) arI V AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ' := by + have B := J.base + have Ly := B.jvLay J.vm + have kV := B.kVn J.vm hw nV + have E' : JExtVcpu (L := L) V Z.th s' := + (J.extv.toV2u.of_eq (fun y hy => hSS.capV hy) hv kV).vcpu + have Jv' : JVm (L := L) V H s' σ' := + J.vm.step (fun _ h => h) (fun _ _ => rfl) hw.1 (fun u hu => by rw [hvk u hu]; exact J.vm.spec u hu) + (fun o ko' ho => Or.inl (hsz o ko' ho)) + have Vl' : VList st V Z.th s' := J.vl.writes hw fun b hb => by + refine ⟨J.vl.lw_al Ly.vfit hb, fun hW => ?_⟩ + rcases hb with rfl | ⟨u, hu, rfl | rfl⟩ + · exact hh _ hW rfl + · have := lay_next_val (Ly.vfit u hu).2 + rcases nV u hu _ hW with h | h <;> omega + · have := lay_prev_val (Ly.vfit u hu).2 + rcases nV u hu _ hW with h | h <;> omega + exact ⟨B', E', hRun, Jv', Vl', J.vb.ofV hvk kV, J.vo.frame hnv, + J.vr.frame (fun u _ vs hu => hnv _ vs hu) (v3_agreeW (L := L) hw (fun u hu => (Ly.vfit u hu).2) nV)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- A native step uses the old binding of every surviving VM thread. -/ +theorem VmBaseR.ofBSSR (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VmBaseR (L := L) arI V AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ' := by + have R' : RunVmB (L := L) Z.th s' := by + intro t ht h8 + rw [hv t ht] + refine J.rvm t ht ?_ + obtain ⟨x', hk', -⟩ := B'.xtop.rel.2.1.1.tcb t ht + have w' := (B'.xtop.fin.1.1.2.2.1.at_tcb (B'.thT t ht) hk').st + rw [Sched.stA_tStA] at h8 ⊢ + rw [h8] at w' + have hx' : x'.tcbState = .runningVM := by + cases hx : x'.tcbState <;> rw [hx] at w' <;> simp_all [Notif.tsCode] + obtain ⟨x, hk, hx⟩ := hrv _ x' hk' hx' + have w0 := (J.base.xtop.fin.1.1.2.2.1.at_tcb (J.base.thT t ht) hk).st + rw [hx] at w0 + apply UScalar.eq_of_val_eq + rw [w0]; simp [Notif.tsCode] + exact J.ofBSSCoreR B' hw hSS nV hh hv hvk hsz R' hnv + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **`VmBaseR` across a native step that may change the spec heap** (gen 42b42: P3/VmState's `VcpuCore.ofBSR` proof, +moved here; `VcpuCore.ofBSR` and `JAtZV3.ofBSR` are its corollaries; gen 42b46: `VmBaseR.ofBSSR`'s corollary). -/ +theorem VmBaseR.ofBSR (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hO : ∀ b, W b → OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VmBaseR (L := L) arI V AR Z H Cs Ac' Lst' D' s' a' σ' := + J.ofBSSR (V' := Z.vs) B' hw (J.base.xtop.slotsSame_of hw hO) (fun u hu b hb => J.base.oobjV J.vm hu (hO b hb)) hh hv + hvk hsz hrv hnv + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **`VmBaseR` across a native step keeping the spec heap** (as `VcpuCore.ofBR`). -/ +theorem VmBaseR.ofBR (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hO : ∀ b, W b → OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') (hke : σ'.kheap = σ.kheap) : + VmBaseR (L := L) arI V AR Z H Cs Ac' Lst' D' s' a' σ' := + J.ofBSR B' hw hO hh hv (fun u _ => by rw [hke]) (fun o ko' ho => ⟨ko', by rw [← hke]; exact ho, rfl⟩) + (fun o x' ho hx => ⟨x', by rw [← hke]; exact ho, hx⟩) (fun p vs hp => by rw [← hke]; exact hp) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **`VmBaseFR` across a native step that may change the spec heap**: `VmBaseR.ofBSR` with the step's `VmKStep`. -/ +theorem VmBaseFR.ofBSR (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hO : ∀ b, W b → OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) (hK : VmKStep σ σ') : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac' Lst' D' s' a' σ' := + ⟨J.toVmBaseR.ofBSR B' hw hO hh hv hvk hK.sz hrv hnv, + J.toVmFoot.step hw (fun b hb => Or.inl (hO b hb)) (J.base.xtop.slotsSame_of hw hO) hK⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **`VmBaseFR` across a native step keeping the spec heap.** -/ +theorem VmBaseFR.ofBR (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hO : ∀ b, W b → OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') (hke : σ'.kheap = σ.kheap) : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac' Lst' D' s' a' σ' := + ⟨J.toVmBaseR.ofBR B' hw hO hh hv hke, + J.toVmFoot.step hw (fun b hb => Or.inl (hO b hb)) (J.base.xtop.slotsSame_of hw hO) (VmKStep.ofEq hke)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **FL4's write of a non-link VCPU word paired with the spec's VCPU update** keeps `VmBaseFR`, given the new +spec VCPU related to the new image (`hR`: the paired spec update, the caller's obligation). -/ +theorem VmBaseFR.vcpuStep (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p w : U64} (hp : p ∈ V) + (hal : P1.Aligned w) (g1 : p.val ≤ w.val) (g2 : w.val < p.val + 32768) + (hw : w.val ≠ p.val + 20480 ∧ w.val ≠ p.val + 20488 ∧ w.val ≠ p.val + 20496) (x : U64) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (vs' : Fl4Spec.VcpuState) + (ht : Fl4Spec.VcpuState.tcb vs' = Fl4Spec.VcpuState.tcb vs) + (hR : Fl4Spec.VcpuRel Fl4Spec.virtKind (vmemW (L.mem (L.wr s w x))) p.bv vs') : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s w x) a (Notif.updK σ p.bv (.archObj (.vcpu vs'))) := by + have B := J.base + have Ly := B.jvLay J.vm + have hW : P1.WritesIn st s (L.wr s w x) (· = w) := P1.writesIn_wr hal + have nJ := (Ly.nJS_v hp g1 g2 hw.1).1 + obtain ⟨B1, R', Jv1, Vl'⟩ := B.vcpuWrV J.vm J.rvm J.vl hp hal g1 g2 hw x + obtain ⟨B', Jv'⟩ := B1.vcpuSpecV Jv1 hp hk vs' (B1.xtop.vcpuSpec hk vs') + have hb : ∀ v ∈ V, L.mem (L.wr s w x) (vTcbA v) = L.mem s (vTcbA v) := fun v hv => by + have hf := Ly.vfit v hv + exact hW.mem_eq (by unfold P1.Aligned; rw [lay_vTcb_val hf.2]; omega) fun e => + nJ (Or.inr (Or.inr ⟨v, hv, e.symm⟩)) + refine ⟨⟨B', (J.extv.toV2u.of_writes (B.jwPl J.vm) hW fun b hb => by subst hb; exact nJ).vcpu, R', Jv', Vl', + (J.vb.ofKheap rfl hb).set vs' (fun _ _ _ => rfl) ?_, J.vo.updV hp _, J.vr.upd vs' hR fun u hu e => ?_⟩, + J.toVmFoot.step hW (vfoot_wr hp g1 g2) (B.ssV J.vm hW fun b hb => by subst hb; exact Or.inl ⟨p, hp, g1, g2⟩) + (VmKStep.vcpu hk vs')⟩ + · obtain ⟨vs0, h1, h2⟩ := J.vb p hp + rw [hk] at h1 + cases h1 + rw [ht, hb p hp]; exact h2 + · refine v3_agreeAll (Ly.vfit u hu).2 fun b hb b1 b2 => hW.mem_eq hb fun e' => ?_ + rw [e'] at b1 b2 + rcases Ly.vv u hu p hp e with h | h <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **A spec VCPU bound to `t` is the `V` VCPU named by `t`'s VCPU word** (`JAtZV2.specBound` at `VmBaseR`). -/ +theorem VmBaseR.specBound (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) (hO : VOnly V σ) {t : U64} + (ht : Z.th t = some .tcb) {w : Fl4Spec.ObjRef} {ws : Fl4Spec.VcpuState} + (hw : σ.kheap w = some (.archObj (.vcpu ws))) (hwt : ws.tcb = some t.bv) : + ∃ u ∈ V, w = u.bv ∧ L.mem s (tVcpuA t) = u ∧ L.mem s (vTcbA u) = t := + J.vb.specBound J.extv.back hO hw hwt + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +set_option maxHeartbeats 4000000 in +/-- **`VcpuCore` across a native step that may change the spec heap** (module docstring; the arguments of +`JAtZV.ofBSR`, `JAtZV2.ofBSR`, `JAtZV3.ofBSR`; gen 42b42: P3/JAtZV3's `VmBaseR.ofBSR` and the four other fields; gen 42b46: +the slots kept and the writes' VCPU apartness for `hO`, the end at any `Z.mv Z.G V'` — `VmBaseR.ofBSSR`; `VcpuCore.ofBSR` +its corollary). -/ +theorem VcpuCore.ofBSSR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : VcpuCore (L := L) V Z H s σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VcpuCore (L := L) V (Z.mv Z.G V') H s' σ' := by + have J' := VmBaseR.ofBSSR ⟨B, C.extv, C.rvm, C.vm, C.vl, C.vb, C.vo, C.vr⟩ B' hw hSS nV hh hv hvk hsz hrv hnv + have kV := B.kVn C.vm hw nV + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := B.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact ⟨J'.extv, J'.vo, J'.vr, + VirtRngOne.rngOne_congr C.rngOne fun z hz _ => by rw [hSS.capV (hsl z hz)]; exact NoA.refl _, + J'.rvm, J'.vm, J'.vl, J'.vb, fun v hv => by + obtain ⟨y, hy, hc⟩ := C.vcap v hv + exact ⟨y, hy, (hSS.capV hy).trans hc⟩, + C.tcap.of_caps (fun v hv _ => ⟨hv, kV v hv⟩) fun y hy _ hc => ⟨y, hy, (hSS.capV hy).trans hc⟩⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +set_option maxHeartbeats 4000000 in +/-- **`VcpuCore` across a native step that may change the spec heap** (module docstring; the arguments of +`JAtZV.ofBSR`, `JAtZV2.ofBSR`, `JAtZV3.ofBSR`; gen 42b42: P3/JAtZV3's `VmBaseR.ofBSR` and the four other fields). -/ +theorem VcpuCore.ofBSR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : VcpuCore (L := L) V Z H s σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hO : ∀ b, W b → OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VcpuCore (L := L) V Z H s' σ' := + C.ofBSSR (V' := Z.vs) B B' hw (B.xtop.slotsSame_of hw hO) (fun u hu b hb => B.oobjV C.vm hu (hO b hb)) hh hv hvk hsz + hrv hnv + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- **`VcpuCore` across an SC-code step** (`TFoot` writes, `ScTKw`; as `JAtZV2.ofTKR` / `JAtZV3.ofTKR`). -/ +theorem VcpuCore.ofTKR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : VcpuCore (L := L) V Z H s σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw (fun _ => False) σ σ') : + VcpuCore (L := L) V Z H s' σ' := by + have hS := B.xtop.fin.1.1.1 + obtain ⟨h1, h2, h3⟩ := zv_tk hK + exact C.ofBSR B B' hw (fun b hb => Sched.tfoot_oobj hS hb) (fun b hb => B.tfoot_nh hb) (VKeep.of_tfoot hS B.thT hw) + (fun u hu => by obtain ⟨y, hy⟩ := C.vm.spec u hu; exact h1 _ _ hy) h2 h3 (zv_tk_v hK) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- `VmStateR` across an SC-code step (`VcpuCore.ofTKR`). -/ +theorem VmStateR.ofTKR (J : VmStateR (L := L) arI V AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw (fun _ => False) σ σ') : + VmStateR (L := L) arI V AR Z H Cs Ac' Lst' D' s' a' σ' := + ⟨B', J.2.ofTKR J.1 B' hw hK⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- `VmStateR` with another arch state relating to the same CR3 (`JAtZRB.archV`). -/ +theorem VmStateR.archV (J : VmStateR (L := L) arI V AR Z H Cs Ac Lst D s a σ) {a' : A} + (hAR : AR a' σ.archState.x64CurrentCr3) : VmStateR (L := L) arI V AR Z H Cs Ac Lst D s a' σ := + ⟨J.1.archV hAR, J.2⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- **A register write of a thread at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.regWr (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (r : Fl4Spec.Register) (v : U64) : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s (Endp.regA t (regIdx r)) v) a + (Notif.updK σ t.bv (.tcb (Notif.setReg x r v.bv))) := by + have B := J.base + have hS := B.xtop.fin.1.1.1 + have hX2 := reg_wr_irelN B.ip (B.xtop.irelK B.ks B.buf) ht hk r v + obtain ⟨hwF, B'⟩ := B.regWrJZ ht hk r v hX2 + have hA : QKY σ (Notif.updK σ t.bv (.tcb (Notif.setReg x r v.bv))) := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun p => ?_⟩ + by_cases hp : p = t.bv + · subst hp; exact Or.inr (Or.inl ⟨x, Notif.setReg x r v.bv, hk, by simp [Notif.updK], rfl⟩) + · exact Or.inl (by simp [Notif.updK, hp]) + obtain ⟨h1, -, h3, h4⟩ := zv_qky hA + exact J.ofBSR B' hwF (fun b hb => Ipc.FinW.oobj hS hb) (fun b hb => B.finW_nh hb) + (B.vkeep_of hwF fun b hb => Ipc.FinW.nv hS hb) + (fun u hu => by obtain ⟨vu, hvu⟩ := J.vm.spec u hu; exact h1 _ _ hvu) h3 h4 (zv_qky_k hA) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- **An IPC-buffer word written at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.userWr (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + (hBP : BufPlaced (L := L) Z.N Z.C Z.B Z.c s σ) {wp : U64 × Fl4Spec.Word} (hwB : wp ∈ Z.B) + (hAp : ∀ q k, H q = some k → (∀ z, k ≠ .other z) → wp.1.val + 8 ≤ q.val ∨ q.val + 2 ^ k.bits ≤ wp.1.val) + (hObj : ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ wp.1.val ∧ wp.1.val < o.toNat + 2 ^ Fl4Spec.objBits ko) + (v : U64) : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s wp.1 v) a + (Endp.setUm σ (Endp.storeMem σ.machineState.underlyingMemory wp.2 v.bv)) := by + have B := J.base + have hP := B.xtop.fin.1.2 + have hal : P1.Aligned wp.1 := (hP.ep.balign wp hwB).1 + have hW : P1.WritesIn st s (L.wr s wp.1 v) (· = wp.1) := P1.writesIn_wr hal + obtain ⟨hO, hh, hv⟩ := B.userW_nat hBP hwB hAp + exact J.ofBR (B.userWrJ hBP hwB hAp hObj v) hW (fun b hb => by subst hb; exact hO) (fun b hb => by subst hb; exact hh) + (B.vkeep_of hW fun b hb => by subst hb; exact hv) rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {rcv : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} + +/-- **A message register written (appended or overwritten) at `JAtZV3`.** -/ +theorem VmBaseFR.mrReg {ms : List Fl4Spec.Word} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 rcv.bv x0 pb ms)) + (hr : rcv ∈ Z.T) (k : Nat) (hk4 : k < 4) (hk : k ≤ ms.length) (v : U64) : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s (Endp.regA rcv (regIdx (Endp.mrIdx k))) v) a + (jMrSt σ0 rcv.bv x0 pb (mrUpd ms k v.bv)) := by + have J2 := J.regWr hr (jMrSt_rcv σ0 rcv.bv x0 pb ms) (Endp.mrIdx k) v + unfold mrUpd + by_cases hlt : k < ms.length + · rw [if_pos hlt, ← v3_jMrSt_set_lt σ0 rcv.bv x0 pb ms v.bv hlt hk4] + exact J2 + · rw [if_neg hlt] + have e : Notif.updK (jMrSt σ0 rcv.bv x0 pb ms) rcv.bv (.tcb (Notif.setReg (Endp.withRegs x0 + (Endp.mrRegs x0.tcbArch.tcbContext.userRegs ms)) (Endp.mrIdx k) v.bv)) = + jMrSt σ0 rcv.bv x0 pb (ms ++ [v.bv]) := by + cases pb with + | none => exact Endp.mrStateN_snoc_lt _ _ _ _ _ k (by omega) (by omega) + | some p => exact Endp.mrState_snoc_lt _ _ _ _ _ _ k (by omega) (by omega) + rw [← e] + exact J2 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {rcv : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} + +set_option maxHeartbeats 8000000 in +/-- **A buffer word of the message written (appended or overwritten) at `JAtZV3`** (word `k`, `4 ≤ k ≤ |ms| < +120`, FL4 buffer index `i1 = k + 1`). -/ +theorem VmBaseFR.mrBuf {ms : List Fl4Spec.Word} {p : Fl4Spec.ObjRef} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 rcv.bv x0 (some p) ms)) + (hr : rcv ∈ Z.T) (hq : Endp.ipcBufOf true x0 = some p) {rw : U64} (hmr : Endp.BufMap Z.B rw p) + (hrw : rw.bv = p) (i1 : U64) (k : Nat) (hi1 : i1.val = k + 1) (h4 : 4 ≤ k) (hk : k ≤ ms.length) + (h120 : ms.length < 120) (v : U64) : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s (Endp.fl4BufA rw i1) v) a + (jMrSt σ0 rcv.bv x0 (some p) (mrUpd ms k v.bv)) := by + have B := J.base + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hrT : Z.th rcv = some .tcb := (hJ.tcb rcv).2 (B.zH.1 rcv hr) + have hwB := hmr i1 (by omega) + have hk' := jMrSt_rcv σ0 rcv.bv x0 (some p) ms + obtain ⟨hAp, hObj⟩ := B.bufWordG B.bok hrT hk' (isR := true) hq i1.val (by omega) (b := Endp.fl4BufA rw i1) + (by rw [fl4BufA_bv, hrw]) + have J2 := J.userWr B.bufPlacedJ hwB hAp hObj v + unfold mrUpd + by_cases hlt : k < ms.length + · rw [if_pos hlt] + show VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D _ a (Endp.mrState σ0 rcv.bv x0 p (ms.set k v.bv)) + rw [← Endp.v3_mrState_set_ge _ _ _ _ _ _ h4 hlt (by omega), ← hi1] + exact J2 + · rw [if_neg hlt] + have hkl : k = ms.length := by omega + subst hkl + show VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D _ a (Endp.mrState σ0 rcv.bv x0 p (ms ++ [v.bv])) + rw [← Endp.mrState_snoc_ge _ _ _ _ _ _ h4 h120, ← hi1] + exact J2 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {rcv : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} + +set_option maxHeartbeats 8000000 in +/-- **FL4's `set_mr` of word `i ≤ |ms|` at `JAtZV3`**, appended or overwritten, into a receive buffer or without +one (module docstring). -/ +theorem VmBaseFR.setMr {ms : List Fl4Spec.Word} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 rcv.bv x0 pb ms)) + (hr : rcv ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) {rw : U64} (hb : JBufR Z.B rw pb) + (i : U64) (hi : i.val ≤ ms.length) (h120 : ms.length < 120) (v : U64) : + msg.set_mr st s rcv rw i v ⦃ r => + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D r.2 a (jMrSt σ0 rcv.bv x0 pb (mrUpd ms i.val v.bv)) ⦄ := by + have hN : msg.N_MSG_REGISTERS.val = 4 := by unfold msg.N_MSG_REGISTERS; rfl + unfold msg.set_mr + by_cases h4 : i < msg.N_MSG_REGISTERS + · have hi4 : i.val < 4 := by have := (P1.gt_iff_val).1 h4; omega + simp only [h4, ↓reduceIte] + refine WP.spec_bind (Endp.msg_register_spec i hi4) ?_ + rintro r rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s rcv _ v) ?_ + rintro s1 rfl + unfold lift + apply ok_bind_spec + simp only [WP.spec_ok] + exact J.mrReg hr i.val hi4 hi v + · have hi4 : 4 ≤ i.val := by + have : ¬ i.val < 4 := fun h' => h4 ((P1.gt_iff_val).2 (by rw [hN]; exact h')); omega + rw [if_neg h4] + cases pb with + | none => + have hb' : rw = 0#u64 := hb + subst hb' + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + rw [jMrSt_mrUpdN hi4 hi] + exact J + | some p => + obtain ⟨hrw0, hmr, hrw⟩ := hb + have hb1 : (rw != 0#u64) = true := by simp [hrw0] + simp only [hb1, ↓reduceIte] + unfold lift + apply ok_bind_spec + refine WP.spec_bind (Endp.msg.buf_set.spec s rw _ v) ?_ + rintro s1 rfl + apply ok_bind_spec + simp only [WP.spec_ok] + 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 + exact J.mrBuf hr (hq p rfl) hmr hrw _ i.val hiv hi4 hi h120 v + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +set_option maxHeartbeats 16000000 in +/-- **`set_thread_state Running` of a `RunningVM` thread at `JAtZRB`** (XTopJointReply's `JAtZ.runJD`, same proof; +the post adds the `TFoot` footprint). -/ +theorem JAtZRB.setRunnableVmJD (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .running ∨ x.tcbState = .runningVM) + {ts : Fl4Spec.ThreadState} {raw : U64} (hts : ts = .running ∨ ts = .runningVM) + (hcode : raw.val = Notif.tsCode ts) : + sched.set_thread_state st s t raw ⦃ s6 => ∃ (Lst' : U64 → List U64), + JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := ts })) t.bv) ∧ + SlotsSame Z.th (L.mem s) (L.mem s6) ∧ P1.DbgKeep st (P1.Heap.tcbs H) Cs s s6 ∧ + P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rcases hrs with hrs | hrs <;> rw [hst, hrs] <;> decide, + by rcases hrs with hrs | hrs <;> rw [hst, hrs] <;> decide⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + have rawRun (v : U64) (ts : Fl4Spec.ThreadState) (hr : ts = .running ∨ ts = .runningVM) + (hcode : v.val = tsCode ts) : P1.Runnable v := by + rcases hr with rfl | rfl + · exact Or.inl (UScalar.eq_of_val_eq (by unfold sched.TS_RUNNING tsCode at *; exact hcode)) + · exact Or.inr (Or.inr (UScalar.eq_of_val_eq (by unfold sched.TS_RUNNING_VM tsCode at *; exact hcode))) + have hOld : P1.Runnable (L.mem s (P1.stA t)) := + rawRun _ _ hrs (by rw [P1.stA_eq, ← tStA_eq]; exact hst) + have hNew := rawRun _ _ hts hcode + have hNewSpec : Fl4Spec.runnable ts = true := by rcases hts with rfl | rfl <;> rfl + have hNewFree : IpcFree ts := by + rcases hts with rfl | rfl <;> exact ⟨rfl, rfl, fun _ e => by cases e⟩ + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (set_thread_state_cVM hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by rcases hrs with hrs | hrs <;> rw [hrs] <;> decide) ts + (hts.imp (fun h => by rw [h]; decide) id) raw hcode) + (set_thread_state_sfoot hS ht _)) (set_thread_state_yw hS ht _)) + (P1.sched.set_thread_state.runnable_blk (Es := P1.EH H) s t J.blk htH hOld hNew)) ?_ + rintro s6 ⟨⟨⟨⟨hR6, hS6r⟩, hw6⟩, hy6⟩, hblk, hfoot⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + set x6 : Fl4Spec.Tcb := { x with tcbState := ts } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ p, p ≠ t.bv → σ6.kheap p = σ.kheap p := by + intro p hp; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hp] + have hact6 : σ6.schedulerAction = .chooseNewThread ∨ σ6.schedulerAction = σ.schedulerAction := + stsAct_action σ1 t.bv + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_csc : σ6.curSc = σ.curSc := by rw [hrec]; rfl + have e_cons : σ6.consumedTime = σ.consumedTime := by rw [hrec]; rfl + have e_tm : σ6.curTime = σ.curTime := by rw [hrec]; rfl + have e_idle : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have e_rel : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := by + rcases hrs with hrs | hrs <;> rw [hrs] <;> exact ⟨rfl, rfl, fun _ e => by cases e⟩ + have hK : ScTKw (fun p => p = t.bv) σ σ6 := by + refine ⟨fun p => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, hNewFree⟩⟩) + · rw [hkp p hp]; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + -- `Inv` + have hI1 : Inv σ1 := by + refine J.inv.step (NtfnStep.tcb hk ts x.tcbArch) (fun u x0 y h1 h2 _ hr => ?_) + (fun u x0 y h1 h2 hc => ?_) + · by_cases e : u = t.bv + · subst e; rw [hk] at h1; cases h1; simp [hσ1, updK] at h2; subst h2; exact hNewSpec + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + rw [h1] at h2'; cases h2'; exact hr + · by_cases e : u = t.bv + · subst e; exact J.inv.rd _ x hk (Or.inl (by rcases hrs with hrs | hrs <;> rw [hrs] <;> rfl)) + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + exact J.inv.rd u y h2' hc + have hInv : Inv σ6 := hI1.qchange (qchange_stsAct σ1 t.bv) + -- the yield words + have hY6 : YieldRel Z.T Z.C (L.mem s6) σ6 := by + have h1 := J.yld.frame (m' := L.mem s6) (σ' := σ1) + (fun y hy => hy6.mem_eq (yieldA_aligned hT hy) fun h => h ⟨y, hy, rfl⟩) + (YK.YKS.updTcb (y := x6) hk rfl).ykeep + exact h1.frame (m' := L.mem s6) (σ' := σ6) (fun _ _ => rfl) (YKeep.of_kheap (stsAct_kheap _ _)) + have hYK6 : YKeep σ σ6 := (YK.YKS.updTcb (y := x6) hk rfl).ykeep.trans (YKeep.of_kheap (stsAct_kheap _ _)) + have hsp : Fl4Spec.setThreadState (Z := Unit) t.bv ts σ = ({((), σ6)}, False) := + Notif.setThreadState_eq ts hk + have hsok : SOK (false, true) σ σ6 := Pres.of_eq (pres_setThreadState _ _ _) hsp + have hNSC : NSC Z.N Z.C σ6 := by + intro n hn y p hy hp + by_cases e : n.bv = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.nsc n hn y p hy hp + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hX6.2.2.1, hX6.1.toN, hNSC, hInv⟩ hY6 (hW.mono fun _ h => Or.inl h) + hsok rfl + -- the scheduler facts + have hsched : ∀ u, Fl4Spec.schedulable u σ = true → Fl4Spec.schedulable u σ6 = true := by + intro u hs + obtain ⟨y, scp, z, n, hky, hsc, hrun, hkz, hac, hnr⟩ := schedulable_inv hs + have hzt : scp ≠ t.bv := by rintro rfl; rw [hk] at hkz; cases hkz + have hkz6 : σ6.kheap scp = some (.schedContext z n) := by rw [hkp _ hzt]; exact hkz + have hnr6 : u ∉ σ6.releaseQueue := by rw [e_rel]; exact hnr + by_cases e : u = t.bv + · subst e; rw [hk] at hky; cases hky + exact schedulable_of hk6 hsc hNewSpec hkz6 hac hnr6 + · exact schedulable_of (by rw [hkp _ e]; exact hky) hsc hrun hkz6 hac hnr6 + have hidt : σ.idleThread ≠ t.bv := by + intro e + obtain ⟨y, h1, -, h3, -⟩ := J.act.2 + rw [e, hk] at h1; cases h1; rcases hrs with hrs | hrs <;> rw [hrs] at h3 <;> cases h3 + have hAI : ActIdle σ6 := by + refine ⟨fun u hu => ?_, ?_⟩ + · have hu0 : σ.schedulerAction = .switchThread u := by + rcases hact6 with e | e + · rw [e] at hu; cases hu + · rw [← e]; exact hu + exact hsched u (J.act.1 u hu0) + · obtain ⟨y, h1, h2, h3, h4⟩ := J.act.2 + exact ⟨y, by rw [e_idle, hkp _ hidt]; exact h1, by rw [e_isc]; exact h2, h3, by rw [e_idle, e_rel]; exact h4⟩ + have hoff : OffOK σ6 := by + intro h0 y n hy ha + rw [e_cons] at h0 ⊢; rw [e_tm]; rw [e_csc] at hy; rw [e_csc, e_isc] at ha + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.off h0 y n hy ha + have hcur6 : CurInvR σ6 := by + intro er + cases (trivial : True) + · have hC : CurInv σ := J.cur (hact6.elim (fun e' => by rw [e'] at er; cases er) (fun e' => by rw [← e']; exact er)) + obtain ⟨xt, hkc, hcs, hnb⟩ := hC.tcb + obtain ⟨y1, n1, hk1'⟩ := hC.sc + have hne : σ.curSc ≠ t.bv := by intro e; rw [e, hk] at hk1'; cases hk1' + refine ⟨?_, ⟨y1, n1, by rw [e_csc, hkp _ hne]; exact hk1'⟩, fun hne' y n hy => ?_, + fun hm => hC.notRel (by rw [e_ct, e_rel] at hm; exact hm)⟩ + · by_cases hct : σ.curThread = t.bv + · rw [hct, hk] at hkc; cases hkc + have hnb6 := NotIpcBlk.of_runnable hNewSpec + exact ⟨x6, by rw [e_ct, hct]; exact hk6, by rw [e_csc]; exact hcs, hnb6⟩ + · exact ⟨xt, by rw [e_ct, hkp _ hct]; exact hkc, by rw [e_csc]; exact hcs, hnb⟩ + · rw [e_csc, e_isc] at hne'; rw [e_csc] at hy + rw [hkp _ hne] at hy + exact hC.active hne' y n hy + have hown6 : CurOwnW Z.T Z.C σ6 := by + have hsc6 : ∀ y n, σ6.kheap σ6.curSc = some (.schedContext y n) → σ.kheap σ.curSc = some (.schedContext y n) := by + intro y n hy + rw [e_csc] at hy + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact hy + rcases J.own with ⟨u, hu, xt, hkt, hxs, hown, hnr⟩ | ⟨hcs, hun⟩ + · left + by_cases hut : u.bv = t.bv + · rw [hut, hk] at hkt; cases hkt + exact ⟨u, hu, x6, by rw [hut]; exact hk6, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact ⟨u, hu, xt, by rw [hkp _ hut]; exact hkt, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact Or.inr ⟨by rw [e_csc]; exact hcs, fun y n hy => hun y n (hsc6 y n hy)⟩ + have hcr6 : CurRunM σ6 := by + by_cases hnr : σ6.schedulerAction ≠ .resumeCurThread + · exact Or.inl hnr + have hr' : σ6.schedulerAction = .resumeCurThread := not_not.1 hnr + have ha : σ6.schedulerAction = σ.schedulerAction := hact6.resolve_left (by rw [hr']; intro e; cases e) + have h0 : ¬ σ.schedulerAction ≠ .resumeCurThread := fun e => e (by rw [← ha]; exact hr') + right + rcases J.curRun.resolve_left h0 with h | h + · left + intro y hy + rw [e_ct] at hy + by_cases hct : σ.curThread = t.bv + · rw [hct, hk6] at hy; cases hy; exact hNewSpec + · rw [hkp _ hct] at hy; exact h y hy + · right; rw [e_ct, e_idle]; exact h + have hlk : LK (fun _ => False) σ σ6 := fun p => Or.inr (by + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show LObj _ _ + refine ⟨?_, ?_, ?_⟩ + · intro y ey; cases ey; exact ⟨_, rfl, Or.inl rfl⟩ + · intro y n ey; cases ey + · intro y ey; cases ey + · rw [hkp p hp]; exact OL.refl _) + have hfk : FK σ σ6 := ⟨e_csc, e_idle, e_isc⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s6 q := fun q hq => + J.deadP_sc_stepG hX' hW hK (fun p x' hp hk' h0 y hy => by + subst hp; rw [hk] at hk'; cases hk'; rcases hrs with hrs | hrs <;> rw [hrs] at h0 <;> simp [tsCode] at h0) + hlk hfk hAI.1 hY' (hYK6.dk q.bv) (fun h => h) (J.jcov.dead q hq) + exact ⟨Lst', J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).2) (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2) hKd subset_rfl) (J.sc_kern hW hK hfk hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hNSC hInv hcur6 hown6 hoff hAI (BlockedScActive.tkw J.bsa hK fun p y n hp ha => by + by_cases e : p = t.bv + · rw [e, hk6] at hp; cases hp + · rw [hkp _ e] at hp; exact ⟨y, hp, ha, fun _ h => h, id⟩) hcr6 hblk hKd (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).1 J.zD, + J.xtop.slotsSame_of hW (fun b hb => tfoot_oobj hS hb), P1.Foot.dbgKeep J.blk (fun _ h => h) hfoot, hW⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- **`set_thread_state Running` of a `RunningVM` thread at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.reportRunningJ (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .running ∨ x.tcbState = .runningVM) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s6 => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) ⦄ := by + have B := J.base + have hS := B.xtop.fin.1.1.1 + refine WP.spec_mono (B.setRunnableVmJD ht hk hrs (Or.inl rfl) (raw := sched.TS_RUNNING) (by unfold sched.TS_RUNNING tsCode; rfl)) fun s6 h6 => ?_ + obtain ⟨Lst', B', -, -, hW⟩ := h6 + refine ⟨Lst', ?_⟩ + have hk7 : (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv).kheap t.bv = + some (.tcb { x with tcbState := .running }) := by rw [stsAct_kheap]; simp [updK] + have hkp7 : ∀ p, p ≠ t.bv → (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv).kheap p = + σ.kheap p := by intro p hp; rw [stsAct_kheap]; simp [updK, hp] + refine J.ofBSR B' hW (fun b hb => tfoot_oobj hS hb) (fun b hb => B.tfoot_nh hb) (VKeep.of_tfoot hS B.thT hW) + (fun u hu => ?_) (fun o x' ho hx => ?_) (fun o vs ho => ?_) (fun o => ?_) + · obtain ⟨vu, hvu⟩ := J.vm.spec u hu + refine hkp7 _ fun e => ?_ + rw [e, hk] at hvu; cases hvu + · by_cases e : o = t.bv + · subst e + rw [hk7] at ho; cases ho + cases hx + · rw [hkp7 _ e] at ho; exact ⟨x', ho, hx⟩ + · by_cases e : o = t.bv + · subst e + rw [hk7] at ho; cases ho + · rw [hkp7 _ e] at ho; exact ho + · by_cases e : o = t.bv + · subst e + exact Or.inr ⟨_, _, hk, hk7, Or.inl ⟨_, _, rfl, rfl⟩⟩ + · exact Or.inl (hkp7 _ e) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.vfitV (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) : + p.val % 32768 = 0 ∧ p.val + 32768 ≤ 2 ^ 64 := + (J.base.jvLay J.vm).vfit p hp + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- The word at offset `o` of a VCPU of `V`, as the relation's view (`v3_rdw` at a `JAtZV3` state). -/ +theorem VmBaseFR.rdw (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) {o : U64} + (ho : o.val < 32768) (ha : o.val % 8 = 0) : + (L.mem s (core.num.U64.wrapping_add p o)).bv = vmemW (L.mem s) (p.bv + o.bv) := + v3_rdw _ (J.vfitV hp) ho ha + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdPend (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) : + (L.mem s (core.num.U64.wrapping_add p vcpu.PENDING) != 0#u64) = vs.exit.pending := by + have hC := v3_relC (J.vr p hp vs hk) + have e : vcpu.PENDING.bv = Fl4Spec.VcpuLay.PENDING := by unfold vcpu.PENDING; rfl + have hv : vcpu.PENDING.val = 20520 := by unfold vcpu.PENDING; rfl + rw [v3_bne0, J.rdw hp (by rw [hv]; omega) (by rw [hv]), e, hC.pending] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdValid (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) : + (L.mem s (core.num.U64.wrapping_add p vcpu.VALID) != 0#u64) = vs.exit.valid := by + have hC := v3_relC (J.vr p hp vs hk) + have e : vcpu.VALID.bv = Fl4Spec.VcpuLay.VALID := by unfold vcpu.VALID; rfl + have hv : vcpu.VALID.val = 20528 := by unfold vcpu.VALID; rfl + rw [v3_bne0, J.rdw hp (by rw [hv]; omega) (by rw [hv]), e, hC.valid] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- `VALID` read as zero exactly when the spec has no saved exit. -/ +theorem VmBaseFR.rdValid0 (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) : + L.mem s (core.num.U64.wrapping_add p vcpu.VALID) = 0#u64 ↔ vs.exit.valid = false := by + rw [← J.rdValid hp hk] + simp + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdExit (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (o : U64) (j : Fin 30) + (ho : o.val = 20992 + 8 * j.val) : + (L.mem s (core.num.U64.wrapping_add p o)).bv = vs.exit.mrs j := by + have hC := v3_relC (J.vr p hp vs hk) + have e : o.bv = Fl4Spec.exitOff j := BitVec.eq_of_toNat_eq (by rw [v3_exit_val]; exact ho) + rw [J.rdw hp (by have := j.isLt; omega) (by omega), e, hC.mrs] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdGpr (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (o : U64) (j : Fin 15) + (ho : o.val = 20736 + 8 * j.val) : + (L.mem s (core.num.U64.wrapping_add p o)).bv = vs.get Fl4Spec.virtKind (.gpr j) := by + have hC := v3_relC (J.vr p hp vs hk) + have e : o.bv = Fl4Spec.gprOff j := BitVec.eq_of_toNat_eq (by rw [v3_gpr_val]; exact ho) + rw [J.rdw hp (by have := j.isLt; omega) (by omega), e, ← hC.gpr] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdVmcs (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hvk : Fl4Spec.virtKind = .vmx) + (f : U64) (hf : Fl4Spec.Vmx.readable f.bv = true) : + (L.mem s ⟨p.bv + Fl4Spec.vmcsOff f.bv⟩).bv = vs.get Fl4Spec.virtKind (.vmcs f.bv) := by + have R0 := J.vr p hp vs hk + rw [hvk] at R0 + have R : Fl4Spec.VcpuRelVmx (vmemW (L.mem s)) p.bv vs := R0 + have hfv := J.vfitV hp + have e1 := v3_toNat_add hfv.2 (VirtVcpuRel.vmcsOff_lt' f.bv) + have hov := v3_vmcsOff_val f.bv + have hal : (p.bv + Fl4Spec.vmcsOff f.bv).toNat % 8 = 0 := by + have : p.val % 32768 = 0 := hfv.1 + rw [e1, hov]; omega + have hw : vmemW (L.mem s) (p.bv + Fl4Spec.vmcsOff f.bv) = (L.mem s ⟨p.bv + Fl4Spec.vmcsOff f.bv⟩).bv := by + unfold vmemW; rw [if_pos hal] + rw [← hw, ← R.vmcs f.bv (Or.inr hf), hvk] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdCr2Vmx (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hvk : Fl4Spec.virtKind = .vmx) : + (L.mem s (core.num.U64.wrapping_add p vcpu.CR2)).bv = vs.get Fl4Spec.virtKind (.msr Fl4Spec.msrCr2) := by + have R0 := J.vr p hp vs hk + rw [hvk] at R0 + have R : Fl4Spec.VcpuRelVmx (vmemW (L.mem s)) p.bv vs := R0 + have e : vcpu.CR2.bv = Fl4Spec.VcpuLay.CR2 := by unfold vcpu.CR2; rfl + have hv : vcpu.CR2.val = 20512 := by unfold vcpu.CR2; rfl + rw [J.rdw hp (by rw [hv]; omega) (by rw [hv]), e, ← R.cr2, hvk] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdVmcb (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hvk : Fl4Spec.virtKind = .svm) + (o : U64) (hr : Fl4Spec.Svm.readable o.bv = true) (hh : Fl4Spec.Svm.hiddenRead o.bv = false) + (hx : o.bv ≠ Fl4Spec.Svm.rax) (ho : o.val < 20480) : + (L.mem s (core.num.U64.wrapping_add p o)).bv = vs.get Fl4Spec.virtKind (.vmcb o.bv) := by + have R0 := J.vr p hp vs hk + rw [hvk] at R0 + have R : Fl4Spec.VcpuRelSvm (vmemW (L.mem s)) p.bv vs := R0 + have ha : o.val % 8 = 0 := by + have h7 : o.bv &&& 7 = 0 := by + unfold Fl4Spec.Svm.readable at hr; simp only [Bool.and_eq_true, decide_eq_true_eq] at hr; exact hr.1 + have := congrArg BitVec.toNat h7 + rw [BitVec.toNat_and] at this + have e7 : (7 : BitVec 64).toNat = 2 ^ 3 - 1 := rfl + rw [e7, Nat.and_two_pow_sub_one_eq_mod] at this + exact this + rw [J.rdw hp (by omega) ha, ← R.vmcb o.bv hr hh hx, hvk] + show _ = vs.fields (Fl4Spec.VField.canon .svm (.vmcb o.bv)) + simp [Fl4Spec.VField.canon, hx] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdCr2Svm (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hvk : Fl4Spec.virtKind = .svm) : + (L.mem s (core.num.U64.wrapping_add p 1600#u64)).bv = vs.get Fl4Spec.virtKind (.msr Fl4Spec.msrCr2) := by + have e : (1600#u64 : U64) = ⟨Fl4Spec.Svm.cr2⟩ := + UScalar.eq_of_val_eq (by show (1600#u64 : U64).val = (Fl4Spec.Svm.cr2).toNat; rw [v3_cr2_val]; rfl) + have ho : (⟨Fl4Spec.Svm.cr2⟩ : U64).val < 20480 := by show (Fl4Spec.Svm.cr2).toNat < 20480; rw [v3_cr2_val]; omega + rw [e, J.rdVmcb hp hk hvk ⟨Fl4Spec.Svm.cr2⟩ v3_cr2_rd v3_cr2_hid v3_cr2_rax ho, hvk] + show vs.fields (Fl4Spec.VField.canon .svm (.vmcb Fl4Spec.Svm.cr2)) = + vs.fields (Fl4Spec.VField.canon .svm (.msr Fl4Spec.msrCr2)) + rw [v3_cr2_canon, v3_cr2_canon'] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {t : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +set_option maxHeartbeats 8000000 in +/-- **`report_loop0` at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.repLoop0 (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) + (hb : JBufR Z.B rw pb) {v : U64} {pending saved : Bool} {vs : Fl4Spec.VcpuState} + (hv : (pending || saved) = true → v ∈ V ∧ σ0.kheap v.bv = some (.archObj (.vcpu vs)) ∧ v.bv ≠ t.bv) + (i : U64) (hi : i.val ≤ 30) (s1 : S) + (J1 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s1 a + (jMrSt σ0 t.bv x0 pb ((List.range i.val).map (exitMrN (pending || saved) vs)))) : + vcpu.report_loop0 st s1 t v pending rw saved i ⦃ s2 => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s2 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (exitMrN (pending || saved) vs))) ⦄ := by + unfold vcpu.report_loop0 + have hE : vcpu.EXIT.val = 20992 := by unfold vcpu.EXIT; rfl + have hR : vcpu.REGS.val = 20736 := by unfold vcpu.REGS; rfl + apply loop.spec_decr_nat (measure := fun y : S × U64 => 30 - y.2.val) + (inv := fun y => y.2.val ≤ 30 ∧ VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D y.1 a + (jMrSt σ0 t.bv x0 pb ((List.range y.2.val).map (exitMrN (pending || saved) vs)))) + · rintro ⟨s2, j⟩ ⟨hj, J2⟩ + try simp only at hj J2 + unfold vcpu.report_loop0.body + dsimp only + split + · rename_i hlt + have hlt' : j.val < 30 := by + first | (simpa [vcpu.NUM_MRS] using hlt) | scalar_tac | (unfold vcpu.NUM_MRS at hlt; scalar_tac) + have hlen : ((List.range j.val).map (exitMrN (pending || saved) vs)).length = j.val := by simp + split + · rename_i hp + obtain ⟨hvV, hkv, hvt⟩ := hv (by rw [hp]; rfl) + step as ⟨i1, hi1⟩ + try simp only [vcpu.EXIT] + step as ⟨i2, hi2⟩ + rw [v3_get_eq, bind_ok] + have hw := J2.rdExit hvV (v3_kv hkv hvt) i2 ⟨j.val, hlt'⟩ (by first | omega | scalar_tac | (simp at hi2; omega) | (simp at hi1; omega) | (simp at hi2 hi1; omega) | (simp_all; omega)) + refine WP.spec_bind (J2.setMr hr hq hb j (by rw [hlen]) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + have e := v3_mrUpd_range (exitMrN (pending || saved) vs) j.val (L.mem s2 (core.num.U64.wrapping_add v i2)).bv + (by rw [hw]; simp [exitMrN, hlt', hp]) + rw [hj1', ← e]; exact J3 + · rename_i hp + split + · rename_i hs + obtain ⟨hvV, hkv, hvt⟩ := hv (by rw [hs]; simp) + step as ⟨i1, hi1⟩ + try simp only [vcpu.EXIT] + step as ⟨i2, hi2⟩ + rw [v3_get_eq, bind_ok] + have hw := J2.rdExit hvV (v3_kv hkv hvt) i2 ⟨j.val, hlt'⟩ (by first | omega | scalar_tac | (simp at hi2; omega) | (simp at hi1; omega) | (simp at hi2 hi1; omega) | (simp_all; omega)) + refine WP.spec_bind (J2.setMr hr hq hb j (by rw [hlen]) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + have e := v3_mrUpd_range (exitMrN (pending || saved) vs) j.val (L.mem s2 (core.num.U64.wrapping_add v i2)).bv + (by rw [hw]; simp [exitMrN, hlt', hs]) + rw [hj1', ← e]; exact J3 + · rename_i hs + refine WP.spec_bind (J2.setMr hr hq hb j (by rw [hlen]) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + have e := v3_mrUpd_range (exitMrN (pending || saved) vs) j.val (0#u64 : U64).bv + (by simp [exitMrN, hlt', hs, hp] <;> rfl) + rw [hj1', ← e]; exact J3 + · rename_i hge + have h30 : j.val = 30 := by + have : ¬ j.val < 30 := by + intro h; apply hge + first | (simpa [vcpu.NUM_MRS] using h) | scalar_tac | (unfold vcpu.NUM_MRS; scalar_tac) + omega + rw [h30] at J2 + exact (WP.spec_ok _).2 J2 + · exact ⟨hi, J1⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {t : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +set_option maxHeartbeats 8000000 in +/-- **`report_loop2` at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.repLoop2 (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) + (hb : JBufR Z.B rw pb) {v : U64} {pending saved : Bool} {vs : Fl4Spec.VcpuState} + (hv : (pending || saved) = true → v ∈ V ∧ σ0.kheap v.bv = some (.archObj (.vcpu vs)) ∧ v.bv ≠ t.bv) + (i : U64) (hi : i.val ≤ 30) (s1 : S) + (J1 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s1 a + (jMrSt σ0 t.bv x0 pb ((List.range i.val).map (exitMrN (pending || saved) vs)))) : + vcpu.report_loop2 st s1 t v pending rw saved i ⦃ s2 => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s2 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (exitMrN (pending || saved) vs))) ⦄ := by + unfold vcpu.report_loop2 + have hE : vcpu.EXIT.val = 20992 := by unfold vcpu.EXIT; rfl + have hR : vcpu.REGS.val = 20736 := by unfold vcpu.REGS; rfl + apply loop.spec_decr_nat (measure := fun y : S × U64 => 30 - y.2.val) + (inv := fun y => y.2.val ≤ 30 ∧ VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D y.1 a + (jMrSt σ0 t.bv x0 pb ((List.range y.2.val).map (exitMrN (pending || saved) vs)))) + · rintro ⟨s2, j⟩ ⟨hj, J2⟩ + try simp only at hj J2 + unfold vcpu.report_loop2.body + dsimp only + split + · rename_i hlt + have hlt' : j.val < 30 := by + first | (simpa [vcpu.NUM_MRS] using hlt) | scalar_tac | (unfold vcpu.NUM_MRS at hlt; scalar_tac) + have hlen : ((List.range j.val).map (exitMrN (pending || saved) vs)).length = j.val := by simp + split + · rename_i hp + obtain ⟨hvV, hkv, hvt⟩ := hv (by rw [hp]; rfl) + step as ⟨i1, hi1⟩ + try simp only [vcpu.EXIT] + step as ⟨i2, hi2⟩ + rw [v3_get_eq, bind_ok] + have hw := J2.rdExit hvV (v3_kv hkv hvt) i2 ⟨j.val, hlt'⟩ (by first | omega | scalar_tac | (simp at hi2; omega) | (simp at hi1; omega) | (simp at hi2 hi1; omega) | (simp_all; omega)) + refine WP.spec_bind (J2.setMr hr hq hb j (by rw [hlen]) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + have e := v3_mrUpd_range (exitMrN (pending || saved) vs) j.val (L.mem s2 (core.num.U64.wrapping_add v i2)).bv + (by rw [hw]; simp [exitMrN, hlt', hp]) + rw [hj1', ← e]; exact J3 + · rename_i hp + split + · rename_i hs + obtain ⟨hvV, hkv, hvt⟩ := hv (by rw [hs]; simp) + step as ⟨i1, hi1⟩ + try simp only [vcpu.EXIT] + step as ⟨i2, hi2⟩ + rw [v3_get_eq, bind_ok] + have hw := J2.rdExit hvV (v3_kv hkv hvt) i2 ⟨j.val, hlt'⟩ (by first | omega | scalar_tac | (simp at hi2; omega) | (simp at hi1; omega) | (simp at hi2 hi1; omega) | (simp_all; omega)) + refine WP.spec_bind (J2.setMr hr hq hb j (by rw [hlen]) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + have e := v3_mrUpd_range (exitMrN (pending || saved) vs) j.val (L.mem s2 (core.num.U64.wrapping_add v i2)).bv + (by rw [hw]; simp [exitMrN, hlt', hs]) + rw [hj1', ← e]; exact J3 + · rename_i hs + refine WP.spec_bind (J2.setMr hr hq hb j (by rw [hlen]) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + have e := v3_mrUpd_range (exitMrN (pending || saved) vs) j.val (0#u64 : U64).bv + (by simp [exitMrN, hlt', hs, hp] <;> rfl) + rw [hj1', ← e]; exact J3 + · rename_i hge + have h30 : j.val = 30 := by + have : ¬ j.val < 30 := by + intro h; apply hge + first | (simpa [vcpu.NUM_MRS] using h) | scalar_tac | (unfold vcpu.NUM_MRS; scalar_tac) + omega + rw [h30] at J2 + exact (WP.spec_ok _).2 J2 + · exact ⟨hi, J1⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {t : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +set_option maxHeartbeats 8000000 in +/-- **`report_loop1` at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.repLoop1 (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) + (hb : JBufR Z.B rw pb) {v : U64} {vs : Fl4Spec.VcpuState} (hvV : v ∈ V) + (hkv : σ0.kheap v.bv = some (.archObj (.vcpu vs))) (hvt : v.bv ≠ t.bv) (F0 : Nat → Fl4Spec.Word) + (i : U64) (hi : i.val ≤ 15) (s1 : S) + (J1 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s1 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs F0 i.val)))) : + vcpu.report_loop1 st s1 t v rw i ⦃ s2 => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s2 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs F0 15))) ⦄ := by + unfold vcpu.report_loop1 + have hE : vcpu.EXIT.val = 20992 := by unfold vcpu.EXIT; rfl + have hR : vcpu.REGS.val = 20736 := by unfold vcpu.REGS; rfl + apply loop.spec_decr_nat (measure := fun y : S × U64 => 15 - y.2.val) + (inv := fun y => y.2.val ≤ 15 ∧ VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D y.1 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs F0 y.2.val)))) + · rintro ⟨s2, j⟩ ⟨hj, J2⟩ + try simp only at hj J2 + unfold vcpu.report_loop1.body + dsimp only + split + · rename_i hlt + have hlt' : j.val < 15 := by first | scalar_tac | (simpa using hlt) + have hlen : ((List.range 30).map (gprOv vs F0 j.val)).length = 30 := by simp + step as ⟨k, hk⟩ + step as ⟨i1, hi1⟩ + try simp only [vcpu.REGS] + step as ⟨i2, hi2⟩ + rw [v3_get_eq, bind_ok] + have hk' : k.val = 10 + j.val := by first | omega | (simp at hk; omega) + have hw := J2.rdGpr hvV (v3_kv hkv hvt) i2 ⟨j.val, hlt'⟩ (by first | omega | scalar_tac | (simp at hi2; omega) | (simp at hi1; omega) | (simp at hi2 hi1; omega) | (simp_all; omega)) + refine WP.spec_bind (J2.setMr hr hq hb k (by rw [hlen]; omega) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + rw [v3_mrUpd_set _ (by omega : k.val < 30), hk'] at J3 + have e : (List.range 30).map (fun m => if m = 10 + j.val then (L.mem s2 (core.num.U64.wrapping_add v i2)).bv + else gprOv vs F0 j.val m) = (List.range 30).map (gprOv vs F0 (j.val + 1)) := + v3_map_congr 30 fun m _ => v3_gprOv_succ vs F0 hlt' m _ hw + rw [hj1', ← e] + exact J3 + · rename_i hge + have h15 : j.val = 15 := by + have : ¬ j.val < 15 := by intro h; apply hge; first | scalar_tac | simpa using h + omega + rw [h15] at J2 + exact (WP.spec_ok _).2 J2 + · exact ⟨hi, J1⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {t : U64} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +set_option maxHeartbeats 8000000 in +/-- **`report_loop3` at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.repLoop3 (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) + (hb : JBufR Z.B rw pb) {v : U64} {vs : Fl4Spec.VcpuState} (hvV : v ∈ V) + (hkv : σ0.kheap v.bv = some (.archObj (.vcpu vs))) (hvt : v.bv ≠ t.bv) (F0 : Nat → Fl4Spec.Word) + (i : U64) (hi : i.val ≤ 15) (s1 : S) + (J1 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s1 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs F0 i.val)))) : + vcpu.report_loop3 st s1 t v rw i ⦃ s2 => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s2 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs F0 15))) ⦄ := by + unfold vcpu.report_loop3 + have hE : vcpu.EXIT.val = 20992 := by unfold vcpu.EXIT; rfl + have hR : vcpu.REGS.val = 20736 := by unfold vcpu.REGS; rfl + apply loop.spec_decr_nat (measure := fun y : S × U64 => 15 - y.2.val) + (inv := fun y => y.2.val ≤ 15 ∧ VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D y.1 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs F0 y.2.val)))) + · rintro ⟨s2, j⟩ ⟨hj, J2⟩ + try simp only at hj J2 + unfold vcpu.report_loop3.body + dsimp only + split + · rename_i hlt + have hlt' : j.val < 15 := by first | scalar_tac | (simpa using hlt) + have hlen : ((List.range 30).map (gprOv vs F0 j.val)).length = 30 := by simp + step as ⟨k, hk⟩ + step as ⟨i1, hi1⟩ + try simp only [vcpu.REGS] + step as ⟨i2, hi2⟩ + rw [v3_get_eq, bind_ok] + have hk' : k.val = 10 + j.val := by first | omega | (simp at hk; omega) + have hw := J2.rdGpr hvV (v3_kv hkv hvt) i2 ⟨j.val, hlt'⟩ (by first | omega | scalar_tac | (simp at hi2; omega) | (simp at hi1; omega) | (simp at hi2 hi1; omega) | (simp_all; omega)) + refine WP.spec_bind (J2.setMr hr hq hb k (by rw [hlen]; omega) (by rw [hlen]; omega) _) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + step as ⟨j1, hj1⟩ + have hj1' : j1.val = j.val + 1 := by first | omega | (simp at hj1; omega) + refine ⟨by omega, ?_, by omega⟩ + rw [v3_mrUpd_set _ (by omega : k.val < 30), hk'] at J3 + have e : (List.range 30).map (fun m => if m = 10 + j.val then (L.mem s2 (core.num.U64.wrapping_add v i2)).bv + else gprOv vs F0 j.val m) = (List.range 30).map (gprOv vs F0 (j.val + 1)) := + v3_map_congr 30 fun m _ => v3_gprOv_succ vs F0 hlt' m _ hw + rw [hj1', ← e] + exact J3 + · rename_i hge + have h15 : j.val = 15 := by + have : ¬ j.val < 15 := by intro h; apply hge; first | scalar_tac | simpa using h + omega + rw [h15] at J2 + exact (WP.spec_ok _).2 J2 + · exact ⟨hi, J1⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +private theorem VmBaseFR.clearExitWord + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p w : U64} + (hp : p ∈ V) {o : Fl4Spec.Word} (hoff : o.toNat = 20520 ∨ o.toNat = 20528) + (hwp : w.val = p.val + o.toNat) + {vs vs' : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) + (ht : vs'.tcb = vs.tcb) + (hClear : ∀ {m m' : Fl4Spec.Word → Fl4Spec.Word}, + (∀ o', o' < Fl4Spec.VcpuLay.SIZE → o' ≠ o → m' (p.bv + o') = m (p.bv + o')) → + m' (p.bv + o) = 0 → Fl4Spec.VcpuRel Fl4Spec.virtKind m p.bv vs → + Fl4Spec.VcpuRel Fl4Spec.virtKind m' p.bv vs') : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s w 0#u64) a + (Notif.updK σ p.bv (.archObj (.vcpu vs'))) := by + obtain ⟨hAlignment, hFit⟩ := (J.base.jvLay J.vm).vfit p hp + have hNat : o.toNat < 32768 := by omega + have hl : o < Fl4Spec.VcpuLay.SIZE := by + rw [BitVec.lt_def] + exact hNat + have hal : P1.Aligned w := by unfold P1.Aligned; omega + have hw := P1.writesIn_wr (st := st) (s := s) (v := 0#u64) hal + have eAdd := v3_toNat_add hFit hl + have h0 : vmemW (L.mem (L.wr s w 0#u64)) (p.bv + o) = 0 := by + have hAlign : (p.bv + o).toNat % 8 = 0 := by rw [eAdd]; omega + unfold vmemW + rw [if_pos hAlign] + have hAddr : (⟨p.bv + o⟩ : U64) = w := UScalar.eq_of_val_eq (eAdd.trans hwp.symm) + rw [hAddr, L.mem_wr s w 0#u64 w hal hal, if_pos rfl] + rfl + have hAgree := v3_agree (o0 := o) hFit + (m := L.mem s) (m' := L.mem (L.wr s w 0#u64)) + (fun b hb _ _ hne => hw.mem_eq hb (fun e => hne (by rw [e, hwp]))) + exact J.vcpuStep hp hal (by omega) (by omega) ⟨by omega, by omega, by omega⟩ 0#u64 hk vs' ht + (hClear hAgree h0 (J.vr p hp vs hk)) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- The report clears a pending exit. -/ +theorem VmBaseFR.pendClr (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p w : U64} (hp : p ∈ V) + (hwp : w.val = p.val + 20520) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) : + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D (L.wr s w 0#u64) a + (Notif.updK σ p.bv (.archObj (.vcpu vs.reported))) := + J.clearExitWord hp (Or.inl v3_fix_vals.2.2.1) + (by rw [v3_fix_vals.2.2.1]; exact hwp) hk rfl fun hm h0 h => v3_pendR hm h0 h + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **Message info, badge, FaultIP, `Running`** (module docstring). -/ +theorem VmBaseFR.repFinRunning (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x1 : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x1)) (hrs : x1.tcbState = .running ∨ x1.tcbState = .runningVM) (mi bd : U64) : + (do + let s4 ← obj.Tcb.set_reg st t s layout.reg.MSG_INFO mi + let s5 ← obj.Tcb.set_reg st t s4 layout.reg.BADGE bd + let next ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP next + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a (repTail σ t.bv x1 mi.bv bd.bv) ⦄ := by + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec s t _ mi) ?_ + rintro s4 rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec _ t _ bd) ?_ + rintro s5 rfl + refine WP.spec_bind (Endp.obj.Tcb.reg.spec _ t _) ?_ + rintro nx rfl + refine WP.spec_bind (Endp.obj.Tcb.set_reg.spec _ t _ _) ?_ + rintro s6 rfl + have J4 := J.regWr ht hk Fl4Spec.msg_info_register mi + rw [Endp.regIdx_msgInfo] at J4 + have hk4 : (Notif.updK σ t.bv (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv))).kheap t.bv = + some (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv)) := by simp [Notif.updK] + have J5 := J4.regWr ht hk4 Fl4Spec.badge_register bd + rw [Endp.regIdx_badge] at J5 + have hk5 : (Notif.updK (Notif.updK σ t.bv (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv))) t.bv + (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv) Fl4Spec.badge_register bd.bv))).kheap t.bv = + some (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv) Fl4Spec.badge_register bd.bv)) := by + simp [Notif.updK] + have B5 := J5.base + have hnx := ((B5.xtop.irelK B5.ks B5.buf).1.toE.at_tcb ht hk5).regs .nextIP + rw [show regIdx .nextIP = layout.reg.NEXT_IP by unfold layout.reg.NEXT_IP; rfl] at hnx + have J6 := J5.regWr ht hk5 .faultIP (L.mem (L.wr (L.wr s (Endp.regA t layout.reg.MSG_INFO) mi) + (Endp.regA t layout.reg.BADGE) bd) (Endp.regA t layout.reg.NEXT_IP)) + rw [← fault_ip_idx] at J6 + have hk6 := (by simp [Notif.updK] : (Notif.updK (Notif.updK (Notif.updK σ t.bv + (.tcb (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv))) t.bv + (.tcb (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv) Fl4Spec.badge_register bd.bv))) t.bv + (.tcb (Notif.setReg (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv) Fl4Spec.badge_register bd.bv) + .faultIP (L.mem (L.wr (L.wr s (Endp.regA t layout.reg.MSG_INFO) mi) + (Endp.regA t layout.reg.BADGE) bd) (Endp.regA t layout.reg.NEXT_IP)).bv))).kheap t.bv = + some (.tcb (Notif.setReg (Notif.setReg (Notif.setReg x1 Fl4Spec.msg_info_register mi.bv) Fl4Spec.badge_register bd.bv) + .faultIP (L.mem (L.wr (L.wr s (Endp.regA t layout.reg.MSG_INFO) mi) + (Endp.regA t layout.reg.BADGE) bd) (Endp.regA t layout.reg.NEXT_IP)).bv))) + refine WP.spec_mono (J6.reportRunningJ ht hk6 hrs) fun s7 h7 => ?_ + obtain ⟨Lst', J7⟩ := h7 + refine ⟨Lst', ?_⟩ + unfold repTail + rw [v3_stsAct_setReg] + simp only [zv_updK_updK] at J7 ⊢ + rw [← hnx] + exact J7 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- The VM-exit instance of the shared report proof. -/ +theorem VmBaseFR.repFin (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x1 : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x1)) (hrs : x1.tcbState = .runningVM) (mi bd : U64) : + (do + let s4 ← obj.Tcb.set_reg st t s layout.reg.MSG_INFO mi + let s5 ← obj.Tcb.set_reg st t s4 layout.reg.BADGE bd + let next ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP next + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a (repTail σ t.bv x1 mi.bv bd.bv) ⦄ := + J.repFinRunning ht hk (Or.inr hrs) mi bd + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`PENDING := 0`, message info 1, badge 0, FaultIP, `Running`** (module docstring). -/ +theorem VmBaseFR.repFinPRunning (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x1 : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x1)) (hrs : x1.tcbState = .running ∨ x1.tcbState = .runningVM) {v : U64} (hv : v ∈ V) + {vs : Fl4Spec.VcpuState} (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) : + (do + let s8 ← vcpu.set st s v vcpu.PENDING 0#u64 + let s9 ← obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING) ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a + (repTail (Notif.updK σ v.bv (.archObj (.vcpu vs.reported))) t.bv x1 (1#u64 : U64).bv (0#u64 : U64).bv) ⦄ := by + have hvt : v.bv ≠ t.bv := fun e => by rw [e, hk] at hkv; cases hkv + have hf := J.vfitV hv + unfold vcpu.set + simp only [lift, bind_ok] + refine WP.spec_bind (P1.LawfulStore.write_spec s _ _) fun s8 e8 => ?_ + subst e8 + have hwp : (core.num.U64.wrapping_add v vcpu.PENDING).val = v.val + 20520 := by + have hv' : vcpu.PENDING.val = 20520 := by unfold vcpu.PENDING; rfl + have hs : UScalar.size UScalarTy.U64 = 2 ^ 64 := by simp [U64.size_def, U64.numBits] + rw [core.num.U64.wrapping_add_val_eq, hv'] + first | omega | (rw [hs]; exact Nat.mod_eq_of_lt (by omega)) | (simp only [hs]; omega) + have J8 := J.pendClr hv hwp hkv + have hk8 : (Notif.updK σ v.bv (.archObj (.vcpu vs.reported))).kheap t.bv = some (.tcb x1) := by + simp only [Notif.updK_kheap]; rw [Function.update_of_ne (Ne.symm hvt)]; exact hk + exact J8.repFinRunning ht hk8 hrs 1#u64 0#u64 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +/-- **`set_mr` of MR `n < 30` of a 30-word message** (`setMrJ3`, overwrite form). -/ +theorem VmBaseFR.setMrF {t : U64} {F : Nat → Fl4Spec.Word} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B rw pb) + (i : U64) (n : Nat) (hn : i.val = n) (h30 : n < 30) (w : U64) : + msg.set_mr st s t rw i w ⦃ r => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D r.2 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (fun k => if k = n then w.bv else F k))) ⦄ := + WP.spec_mono (J.setMr hr hq hb i (by simp; omega) (by simp) w) fun r h => by + rw [hn, v3_mrUpd_set F h30] at h; exact h + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +set_option maxHeartbeats 16000000 in +/-- `finish_notification`'s writes after the reason word `r` is chosen (module docstring). -/ +theorem VmBaseFR.repFinN0Running {t : U64} {F : Nat → Fl4Spec.Word} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B rw pb) + (hrs : x0.tcbState = .running ∨ x0.tcbState = .runningVM) (badge : U64) (vec : Bool) (r : U64) + (hrv : r.bv = Fl4Spec.vmNotifyReason Fl4Spec.virtKind) : + (do + let (_, s1) ← msg.set_mr st s t rw 3#u64 r + let (_, s2) ← msg.set_mr st s1 t rw 4#u64 0#u64 + let s3 ← + if vec + then ok s2 + else + do + let (_, s4) ← msg.set_mr st s2 t rw 5#u64 0#u64 + ok s4 + let (_, s4) ← msg.set_mr st s3 t rw 6#u64 0#u64 + let (_, s5) ← msg.set_mr st s4 t rw 25#u64 0#u64 + let (_, s6) ← msg.set_mr st s5 t rw 26#u64 0#u64 + let s7 ← obj.Tcb.set_reg st t s6 layout.reg.MSG_INFO 0#u64 + let s8 ← obj.Tcb.set_reg st t s7 layout.reg.BADGE badge + let next ← obj.Tcb.reg st t s8 layout.reg.NEXT_IP + let s9 ← obj.Tcb.set_reg st t s8 layout.reg.FAULT_IP next + sched.set_thread_state st s9 t sched.TS_RUNNING) ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a + (repTail (jMrSt σ0 t.bv x0 pb ((List.range 30).map (repFinF vec F))) t.bv + (repX x0 ((List.range 30).map (repFinF vec F))) (0#u64 : U64).bv badge.bv) ⦄ := by + have h0 : (0#u64 : U64).bv = 0 := rfl + refine WP.spec_bind (J.setMrF hr hq hb 3#u64 3 (by simp) (by omega) r) ?_ + rintro ⟨_, s1⟩ J1 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J1.setMrF hr hq hb 4#u64 4 (by simp) (by omega) 0#u64) ?_ + rintro ⟨_, s2⟩ J2 + try simp only [uncurry_apply_pair] + -- the tail after MR5, for either `vec` + have tail : ∀ (s3 : S) (G : Nat → Fl4Spec.Word), + (∀ k < 30, (if k = 26 then (0#u64 : U64).bv else if k = 25 then (0#u64 : U64).bv else + if k = 6 then (0#u64 : U64).bv else G k) = repFinF vec F k) → + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s3 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map G)) → + (do + let (_, s4) ← msg.set_mr st s3 t rw 6#u64 0#u64 + let (_, s5) ← msg.set_mr st s4 t rw 25#u64 0#u64 + let (_, s6) ← msg.set_mr st s5 t rw 26#u64 0#u64 + let s7 ← obj.Tcb.set_reg st t s6 layout.reg.MSG_INFO 0#u64 + let s8 ← obj.Tcb.set_reg st t s7 layout.reg.BADGE badge + let next ← obj.Tcb.reg st t s8 layout.reg.NEXT_IP + let s9 ← obj.Tcb.set_reg st t s8 layout.reg.FAULT_IP next + sched.set_thread_state st s9 t sched.TS_RUNNING) ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a + (repTail (jMrSt σ0 t.bv x0 pb ((List.range 30).map (repFinF vec F))) t.bv + (repX x0 ((List.range 30).map (repFinF vec F))) (0#u64 : U64).bv badge.bv) ⦄ := by + intro s3 G hG J3 + refine WP.spec_bind (J3.setMrF hr hq hb 6#u64 6 (by simp) (by omega) 0#u64) ?_ + rintro ⟨_, s4⟩ J4 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J4.setMrF hr hq hb 25#u64 25 (by simp) (by omega) 0#u64) ?_ + rintro ⟨_, s5⟩ J5 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J5.setMrF hr hq hb 26#u64 26 (by simp) (by omega) 0#u64) ?_ + rintro ⟨_, s6⟩ J6 + try simp only [uncurry_apply_pair] + rw [v3_map_congr 30 hG] at J6 + exact J6.repFinRunning hr (jMrSt_rcv _ _ _ _ _) hrs 0#u64 badge + cases vec with + | true => + simp only [↓reduceIte, bind_ok] + refine tail s2 _ (fun k hk => ?_) J2 + unfold repFinF + rw [h0, hrv] + by_cases e3 : k = 3 <;> by_cases e4 : k = 4 <;> by_cases e6 : k = 6 <;> by_cases e25 : k = 25 <;> + by_cases e26 : k = 26 <;> simp_all + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun s3 => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s3 a (jMrSt σ0 t.bv x0 pb + ((List.range 30).map (fun k => if k = 5 then (0#u64 : U64).bv else + if k = 4 then (0#u64 : U64).bv else if k = 3 then r.bv else F k)))) ?_ ?_ + · refine WP.spec_bind (J2.setMrF hr hq hb 5#u64 5 (by simp) (by omega) 0#u64) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + exact (WP.spec_ok _).2 J3 + intro s3 J3 + refine tail s3 _ (fun k hk => ?_) J3 + unfold repFinF + rw [h0, hrv] + by_cases e3 : k = 3 <;> by_cases e4 : k = 4 <;> by_cases e5 : k = 5 <;> by_cases e6 : k = 6 <;> + by_cases e25 : k = 25 <;> by_cases e26 : k = 26 <;> simp_all + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +/-- **`finish_notification` at `JAtZV3`** (module docstring). -/ +theorem VmBaseFR.repFinNRunning (hVA : ZvVirtAgree arI) {t : U64} {F : Nat → Fl4Spec.Word} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B rw pb) + (hrs : x0.tcbState = .running ∨ x0.tcbState = .runningVM) (badge : U64) (vec : Bool) : + vcpu.finish_notification st arI s a t rw badge vec ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a + (repTail (jMrSt σ0 t.bv x0 pb ((List.range 30).map (repFinF vec F))) t.bv + (repX x0 ((List.range 30).map (repFinF vec F))) (0#u64 : U64).bv badge.bv) ⦄ := by + obtain ⟨i, hvi, hi, -⟩ := hVA a + unfold vcpu.finish_notification + rw [hvi, bind_ok] + by_cases h2 : i = 2#u64 + · rw [if_pos h2] + exact J.repFinN0Running hr hq hb hrs badge vec 96#u64 (by + rw [Fl4Spec.vmNotifyReason, if_pos (hi.1 h2)]; rfl) + · rw [if_neg h2] + exact J.repFinN0Running hr hq hb hrs badge vec 1#u64 (by + rw [Fl4Spec.vmNotifyReason, if_neg (fun h => h2 (hi.2 h))]; rfl) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {rw : U64} + +/-- The VM-exit instance of the shared report proof. -/ +theorem VmBaseFR.repFinN (hVA : ZvVirtAgree arI) {t : U64} {F : Nat → Fl4Spec.Word} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B rw pb) + (hrs : x0.tcbState = .runningVM) (badge : U64) (vec : Bool) : + vcpu.finish_notification st arI s a t rw badge vec ⦃ s' => ∃ Lst' : U64 → List U64, + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a + (repTail (jMrSt σ0 t.bv x0 pb ((List.range 30).map (repFinF vec F))) t.bv + (repX x0 ((List.range 30).map (repFinF vec F))) (0#u64 : U64).bv badge.bv) ⦄ := + J.repFinNRunning hVA hr hq hb (Or.inr hrs) badge vec + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rdSvmW (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hvk : Fl4Spec.virtKind = .svm) + (o : U64) (w : Fl4Spec.Word) (he : o.val = w.toNat) + (hw : Fl4Spec.Svm.readable w = true ∧ Fl4Spec.Svm.hiddenRead w = false ∧ w ≠ Fl4Spec.Svm.rax ∧ w.toNat < 20480) : + (L.mem s (core.num.U64.wrapping_add p o)).bv = vs.get Fl4Spec.virtKind (.vmcb w) := by + have e : o = ⟨w⟩ := UScalar.eq_of_val_eq he + subst e + exact J.rdVmcb hp hk hvk ⟨w⟩ hw.1 hw.2.1 hw.2.2.1 hw.2.2.2 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.kindV (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) : + Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm := v3_kind (J.vr p hp vs hk) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvIp (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then do + let i2 ← vcpu.svm.RIP + vcpu.get st s p i2 + else vcpu.read_vmcs st s p vcpu.vmx.GUEST_RIP) ⦃ r => r.bv = vs.entryIp Fl4Spec.virtKind ⦄ := by + unfold Fl4Spec.VcpuState.entryIp + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs), if_pos hs] + have hR : vcpu.svm.RIP = ok 1400#u64 := by unfold vcpu.svm.RIP vcpu.svm.SAVE; rfl + rw [hR, bind_ok, v3_get_eq, WP.spec_ok] + obtain ⟨h1, h2, h3, h4⟩ := v3_rip_ok + exact J.rdSvmW hp hk hs 1400#u64 _ (by rw [h4]; rfl) ⟨h1, h2, h3, by omega⟩ + · rw [if_neg (fun h => hs (hi.1 h)), if_neg hs, v3_read_vmcs_eq, WP.spec_ok] + have hvk := (J.kindV hp hk).resolve_right hs + exact (J.rdVmcs hp hk hvk vcpu.vmx.GUEST_RIP (by rw [v3_vmx_ids.1]; exact v3_vmx_rd.1)).trans (by rw [v3_vmx_ids.1]) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvCtl (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then do + let i2 ← vcpu.get st s p vcpu.svm.INTERCEPT_EXC + i2 >>> 32#i32 + else vcpu.read_vmcs st s p vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS) ⦃ r => + r.bv = vs.entryCtl Fl4Spec.virtKind ⦄ := by + unfold Fl4Spec.VcpuState.entryCtl + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs), if_pos hs, v3_get_eq, bind_ok, v3_shr32, WP.spec_ok] + obtain ⟨h1, h2, h3, h4⟩ := v3_iexc_ok + show (L.mem s (core.num.U64.wrapping_add p vcpu.svm.INTERCEPT_EXC)).bv >>> 32 = _ + rw [J.rdSvmW hp hk hs vcpu.svm.INTERCEPT_EXC _ (by rw [h4]; unfold vcpu.svm.INTERCEPT_EXC; rfl) + ⟨h1, h2, h3, by omega⟩] + · rw [if_neg (fun h => hs (hi.1 h)), if_neg hs, v3_read_vmcs_eq, WP.spec_ok] + have hvk := (J.kindV hp hk).resolve_right hs + exact (J.rdVmcs hp hk hvk vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS (by rw [v3_vmx_ids.2.1]; exact v3_vmx_rd.2.1)).trans (by rw [v3_vmx_ids.2.1]) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvIntr (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then vcpu.get st s p vcpu.svm.EVENT_INJ + else vcpu.read_vmcs st s p vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO) ⦃ r => + r.bv = vs.entryIntr Fl4Spec.virtKind ⦄ := by + unfold Fl4Spec.VcpuState.entryIntr + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs), if_pos hs, v3_get_eq, WP.spec_ok] + obtain ⟨h1, h2, h3, h4⟩ := v3_einj_ok + exact J.rdSvmW hp hk hs vcpu.svm.EVENT_INJ _ (by rw [h4]; unfold vcpu.svm.EVENT_INJ; rfl) ⟨h1, h2, h3, by omega⟩ + · rw [if_neg (fun h => hs (hi.1 h)), if_neg hs, v3_read_vmcs_eq, WP.spec_ok] + have hvk := (J.kindV hp hk).resolve_right hs + exact (J.rdVmcs hp hk hvk vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO (by rw [v3_vmx_ids.2.2.1]; exact v3_vmx_rd.2.2.1)).trans (by rw [v3_vmx_ids.2.2.1]) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvFlags (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s p i4 + else vcpu.read_vmcs st s p vcpu.vmx.GUEST_RFLAGS) ⦃ r => + r.bv = (if Fl4Spec.virtKind = .svm then vs.get Fl4Spec.virtKind (.vmcb Fl4Spec.Svm.rflags) + else vs.get Fl4Spec.virtKind (.vmcs Fl4Spec.Vmx.guestRflags)) ⦄ := by + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs), if_pos hs] + have hR : vcpu.svm.RFLAGS = ok 1392#u64 := by unfold vcpu.svm.RFLAGS vcpu.svm.SAVE; rfl + rw [hR, bind_ok, v3_get_eq, WP.spec_ok] + obtain ⟨h1, h2, h3, h4⟩ := v3_rfl_ok + exact J.rdSvmW hp hk hs 1392#u64 _ (by rw [h4]; rfl) ⟨h1, h2, h3, by omega⟩ + · rw [if_neg (fun h => hs (hi.1 h)), if_neg hs, v3_read_vmcs_eq, WP.spec_ok] + have hvk := (J.kindV hp hk).resolve_right hs + exact (J.rdVmcs hp hk hvk vcpu.vmx.GUEST_RFLAGS (by rw [v3_vmx_ids.2.2.2.1]; exact v3_vmx_rd.2.2.2.1)).trans (by rw [v3_vmx_ids.2.2.2.1]) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvInt (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then vcpu.get st s p vcpu.svm.INT_STATE + else vcpu.read_vmcs st s p vcpu.vmx.GUEST_INTERRUPTABILITY) ⦃ r => + r.bv = (if Fl4Spec.virtKind = .svm then vs.get Fl4Spec.virtKind (.vmcb Fl4Spec.Svm.intState) + else vs.get Fl4Spec.virtKind (.vmcs Fl4Spec.Vmx.guestInterruptability)) ⦄ := by + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs), if_pos hs, v3_get_eq, WP.spec_ok] + obtain ⟨h1, h2, h3, h4⟩ := v3_ist_ok + exact J.rdSvmW hp hk hs vcpu.svm.INT_STATE _ (by rw [h4]; unfold vcpu.svm.INT_STATE; rfl) ⟨h1, h2, h3, by omega⟩ + · rw [if_neg (fun h => hs (hi.1 h)), if_neg hs, v3_read_vmcs_eq, WP.spec_ok] + have hvk := (J.kindV hp hk).resolve_right hs + exact (J.rdVmcs hp hk hvk vcpu.vmx.GUEST_INTERRUPTABILITY (by rw [v3_vmx_ids.2.2.2.2]; exact v3_vmx_rd.2.2.2.2)).trans (by rw [v3_vmx_ids.2.2.2.2]) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvCr3 (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then do + let i4 ← vcpu.svm.CR3 + vcpu.get st s p i4 + else ok 0#u64) ⦃ r => + r.bv = (if Fl4Spec.virtKind = .svm then vs.get Fl4Spec.virtKind (.vmcb Fl4Spec.Svm.cr3) else 0) ⦄ := by + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs), if_pos hs] + have hR : vcpu.svm.CR3 = ok 1360#u64 := by unfold vcpu.svm.CR3 vcpu.svm.SAVE; rfl + rw [hR, bind_ok, v3_get_eq, WP.spec_ok] + obtain ⟨h1, h2, h3, h4⟩ := v3_cr3_ok + exact J.rdSvmW hp hk hs 1360#u64 _ (by rw [h4]; rfl) ⟨h1, h2, h3, by omega⟩ + · rw [if_neg (fun h => hs (hi.1 h)), if_neg hs, WP.spec_ok] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} + +theorem VmBaseFR.rvCr2 (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) (hp : p ∈ V) + (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 then do + let i4 ← vcpu.svm.CR2 + vcpu.get st s p i4 + else vcpu.get st s p vcpu.CR2) ⦃ r => r.bv = vs.get Fl4Spec.virtKind (.msr Fl4Spec.msrCr2) ⦄ := by + by_cases hs : Fl4Spec.virtKind = .svm + · rw [if_pos (hi.2 hs)] + have hR : vcpu.svm.CR2 = ok 1600#u64 := by unfold vcpu.svm.CR2 vcpu.svm.SAVE; rfl + rw [hR, bind_ok, v3_get_eq, WP.spec_ok] + exact J.rdCr2Svm hp hk hs + · rw [if_neg (fun h => hs (hi.1 h)), v3_get_eq, WP.spec_ok] + exact J.rdCr2Vmx hp hk ((J.kindV hp hk).resolve_right hs) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {p : U64} {vs : Fl4Spec.VcpuState} {i : U64} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} + +set_option maxHeartbeats 16000000 in +/-- **The `VALID = 0` block: MR7/8/9/29** (module docstring). -/ +theorem VmBaseFR.repCr (hVA : ZvVirtAgree arI) {t b : U64} {F : Nat → Fl4Spec.Word} {s5 : S} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s5 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B b pb) + {v : U64} (hv : v ∈ V) (hkv : σ0.kheap v.bv = some (.archObj (.vcpu vs))) (hvt : v.bv ≠ t.bv) : + (do + let i3 ← arI.virtualization a + let flags ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s5 v i4 + else vcpu.read_vmcs st s5 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i3 = 2#u64 + then vcpu.get st s5 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s5 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR3 + vcpu.get st s5 v i4 + else ok 0#u64 + let cr2 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR2 + vcpu.get st s5 v i4 + else vcpu.get st s5 v vcpu.CR2 + let (_, s7) ← msg.set_mr st s5 t b 7#u64 flags + let (_, s8) ← msg.set_mr st s7 t b 8#u64 int + let (_, s9) ← msg.set_mr st s8 t b 9#u64 cr3 + let (_, s10) ← msg.set_mr st s9 t b 29#u64 cr2 + ok s10) ⦃ s' => VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s' a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (crF vs F))) ⦄ := by + obtain ⟨i, hvi, hi, -⟩ := hVA a + have hk := v3_kv (ms := (List.range 30).map F) (x0 := x0) (pb := pb) hkv hvt + rw [hvi, bind_ok] + refine WP.spec_bind (J.rvFlags hv hk hi) fun fl hfl => ?_ + refine WP.spec_bind (J.rvInt hv hk hi) fun it hit => ?_ + refine WP.spec_bind (J.rvCr3 hv hk hi) fun c3 hc3 => ?_ + refine WP.spec_bind (J.rvCr2 hv hk hi) fun c2 hc2 => ?_ + refine WP.spec_bind (J.setMrF hr hq hb 7#u64 7 (by simp) (by omega) fl) ?_ + rintro ⟨_, s7⟩ J7 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J7.setMrF hr hq hb 8#u64 8 (by simp) (by omega) it) ?_ + rintro ⟨_, s8⟩ J8 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J8.setMrF hr hq hb 9#u64 9 (by simp) (by omega) c3) ?_ + rintro ⟨_, s9⟩ J9 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J9.setMrF hr hq hb 29#u64 29 (by simp) (by omega) c2) ?_ + rintro ⟨_, s10⟩ J10 + try simp only [uncurry_apply_pair] + rw [WP.spec_ok] + rw [v3_map_congr 30 (G := crF vs F) (fun k _ => by + unfold crF + rw [hfl, hit, hc3, hc2] + all_goals (by_cases e29 : k = 29 <;> by_cases e9 : k = 9 <;> by_cases e8 : k = 8 <;> by_cases e7 : k = 7 <;> + simp_all))] at J10 + exact J10 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {a : A} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {vs : Fl4Spec.VcpuState} + +set_option maxHeartbeats 16000000 in +/-- **The `if full …` block** (module docstring). -/ +theorem VmBaseFR.repFullRunning (hVA : ZvVirtAgree arI) {t b v : U64} {F : Nat → Fl4Spec.Word} {s6 : S} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s6 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B b pb) + (hrs : x0.tcbState = .running ∨ x0.tcbState = .runningVM) (hv : v ∈ V) (hkv : σ0.kheap v.bv = some (.archObj (.vcpu vs))) + (hvt : v.bv ≠ t.bv) (pending saved full : Bool) (hsaved : saved = vs.exit.valid) + (hpf : pending = false → full = true) (badge : U64) : + (do + if full + then + let i3 ← msg.syscall_arg st s6 t b 2#u64 + let descriptor ← lift (i3 &&& 4294967295#u64) + let (_, s7) ← msg.set_mr st s6 t b 2#u64 descriptor + if pending + then + let s8 ← vcpu.set st s7 v vcpu.PENDING 0#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← + obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i4 ← 27#u64 * 8#u64 + let i5 ← vcpu.EXIT + i4 + let i6 ← vcpu.get st s7 v i5 + let i7 ← 1#u64 <<< 31#i32 + let i8 ← lift (i6 &&& i7) + ok (i8 != 0#u64) + else ok false + vcpu.finish_notification st arI s7 a t b + badge vectoring + else + let s7 ← vcpu.set st s6 v vcpu.PENDING 0#u64 + let s8 ← + obj.Tcb.set_reg st t s7 layout.reg.MSG_INFO 1#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s9 layout.reg.NEXT_IP + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s10 t sched.TS_RUNNING) ⦃ s' => ∃ Lst' : U64 → List U64, VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a + (repEnd σ0 t x0 pb v vs pending saved full badge F) ⦄ := by + have hk := v3_kv (ms := (List.range 30).map F) (x0 := x0) (pb := pb) hkv hvt + have hkt := jMrSt_rcv σ0 t.bv x0 pb ((List.range 30).map F) + have hE : vcpu.EXIT.val = 20992 := by unfold vcpu.EXIT; rfl + cases full with + | false => + have hp : pending = true := by + cases pending + · exact absurd (hpf rfl) (by simp) + · rfl + subst hp + simp only [Bool.false_eq_true, ↓reduceIte] + have e : (List.range 30).map (rpFullF false F) = (List.range 30).map F := + v3_map_congr 30 (fun k _ => by simp [rpFullF]) + simp only [repEnd, ↓reduceIte, e] + exact J.repFinPRunning hr hkt hrs hv hk + | true => + simp only [↓reduceIte] + refine WP.spec_bind (v3_syscall_arg2 s6 t b) fun i3 hi3 => ?_ + simp only [lift, bind_ok] + have hv2 : i3.bv = F 2 := by + rw [hi3] + have B := J.base + have h := ((B.xtop.irelK B.ks B.buf).1.toE.at_tcb hr hkt).regs (Endp.mrIdx 2) + rw [h] + show Endp.mrRegs x0.tcbArch.tcbContext.userRegs ((List.range 30).map F) (Endp.mrIdx 2) = F 2 + rw [Endp.mrRegs_mr _ _ (by omega : 2 < 4)] + simp + refine WP.spec_bind (J.setMrF hr hq hb 2#u64 2 (by simp) (by omega) (i3 &&& 4294967295#u64)) ?_ + rintro ⟨_, s7⟩ J7 + try simp only [uncurry_apply_pair] + have hm : ((i3 &&& 4294967295#u64).bv : Fl4Spec.Word) = F 2 &&& (0xffffffff : Fl4Spec.Word) := by + simp only [UScalar.bv_and, hv2]; rfl + rw [v3_map_congr 30 (G := rpFullF true F) (fun k _ => by + unfold rpFullF + by_cases e2 : k = 2 + · subst e2; simp [hm] + · simp [e2])] at J7 + have hk7 := v3_kv (ms := (List.range 30).map (rpFullF true F)) (x0 := x0) (pb := pb) hkv hvt + cases pending with + | true => + simp only [↓reduceIte] + simp only [repEnd, ↓reduceIte] + exact J7.repFinPRunning hr (jMrSt_rcv _ _ _ _ _) hrs hv hk7 + | false => + simp only [Bool.false_eq_true, ↓reduceIte] + simp only [repEnd, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (Pₘ := fun vec => vec = rpVecOf saved vs) ?_ ?_ + · cases saved with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + simp [rpVecOf] + | true => + simp only [↓reduceIte] + step as ⟨i4, hi4⟩ + step as ⟨i5, hi5⟩ + rw [v3_get_eq, bind_ok, VirtSanEq.shl_i32 _ _ (by decide) (by decide), bind_ok] + simp only [lift, bind_ok, WP.spec_ok] + have hw := J7.rdExit hv hk7 i5 27 (by + have : ((27 : Fin 30) : Nat) = 27 := rfl + first | omega | (simp at hi4 hi5; omega) | (simp_all; omega)) + rw [v3_bne0] + simp only [UScalar.bv_and, hw] + have h1 : ((1#u64 : U64).bv <<< (31#i32 : I32).toNat : Fl4Spec.Word) = (1 : Fl4Spec.Word) <<< 31 := by + decide + first | rw [h1] | simp only [h1] + simp [rpVecOf, ← hsaved] + · intro vec hvec + subst hvec + exact J7.repFinNRunning hVA hr hq hb hrs badge _ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {a : A} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {vs : Fl4Spec.VcpuState} + +set_option maxHeartbeats 32000000 in +/-- **The tail of the report after its first loop, with a VCPU** (module docstring). -/ +theorem VmBaseFR.repTailRunning (hVA : ZvVirtAgree arI) {t b v : U64} {F0 : Nat → Fl4Spec.Word} {s1 : S} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s1 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F0))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B b pb) + (hrs : x0.tcbState = .running ∨ x0.tcbState = .runningVM) (hv : v ∈ V) (hkv : σ0.kheap v.bv = some (.archObj (.vcpu vs))) + (hvt : v.bv ≠ t.bv) (pending saved full : Bool) (hsaved : saved = vs.exit.valid) + (hpf : pending = false → full = true) (badge : U64) (lp : S → Result S) + (R : S → Fl4Spec.State Unit → Prop) (hR : R sA σA) + (hlp : ∀ s4, VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s4 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (rpEntF vs F0))) → + lp s4 ⦃ s5 => ∃ (sB : S) (σB : Fl4Spec.State Unit), R sB σB ∧ VmBaseFR (L := L) arI sB σB V AR Z H Cs Ac Lst D + s5 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs (rpEntF vs F0) 15))) ⦄) : + (do + let i ← vcpu.get st s1 v vcpu.VALID + if i = 0#u64 + then + let i1 ← arI.virtualization a + let ip ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.RIP + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RIP + let ctl ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.get st s1 v vcpu.svm.INTERCEPT_EXC + i2 >>> 32#i32 + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let intr ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.EVENT_INJ + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + let (_, s2) ← msg.set_mr st s1 t b 0#u64 ip + let (_, s3) ← msg.set_mr st s2 t b 1#u64 ctl + let (_, s4) ← msg.set_mr st s3 t b 2#u64 intr + let s5 ← lp s4 + let i2 ← vcpu.get st s5 v vcpu.VALID + let s6 ← + if i2 = 0#u64 + then + do + let i3 ← arI.virtualization a + let flags ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s5 v i4 + else vcpu.read_vmcs st s5 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i3 = 2#u64 + then vcpu.get st s5 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s5 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR3 + vcpu.get st s5 v i4 + else ok 0#u64 + let cr2 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR2 + vcpu.get st s5 v i4 + else vcpu.get st s5 v vcpu.CR2 + let (_, s7) ← msg.set_mr st s5 t b 7#u64 flags + let (_, s8) ← msg.set_mr st s7 t b 8#u64 int + let (_, s9) ← msg.set_mr st s8 t b 9#u64 cr3 + let (_, s10) ← msg.set_mr st s9 t b 29#u64 cr2 + ok s10 + else ok s5 + if full + then + let i3 ← msg.syscall_arg st s6 t b 2#u64 + let descriptor ← lift (i3 &&& 4294967295#u64) + let (_, s7) ← msg.set_mr st s6 t b 2#u64 descriptor + if pending + then + let s8 ← vcpu.set st s7 v vcpu.PENDING 0#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← + obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i4 ← 27#u64 * 8#u64 + let i5 ← vcpu.EXIT + i4 + let i6 ← vcpu.get st s7 v i5 + let i7 ← 1#u64 <<< 31#i32 + let i8 ← lift (i6 &&& i7) + ok (i8 != 0#u64) + else ok false + vcpu.finish_notification st arI s7 a t b + badge vectoring + else + let s7 ← vcpu.set st s6 v vcpu.PENDING 0#u64 + let s8 ← + obj.Tcb.set_reg st t s7 layout.reg.MSG_INFO 1#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s9 layout.reg.NEXT_IP + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s10 t sched.TS_RUNNING + else + let s2 ← + if i = 0#u64 + then + do + let i1 ← arI.virtualization a + let flags ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.RFLAGS + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s1 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.CR3 + vcpu.get st s1 v i2 + else ok 0#u64 + let cr2 ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.CR2 + vcpu.get st s1 v i2 + else vcpu.get st s1 v vcpu.CR2 + let (_, s3) ← msg.set_mr st s1 t b 7#u64 flags + let (_, s4) ← msg.set_mr st s3 t b 8#u64 int + let (_, s5) ← msg.set_mr st s4 t b 9#u64 cr3 + let (_, s6) ← msg.set_mr st s5 t b 29#u64 cr2 + ok s6 + else ok s1 + if full + then + let i1 ← msg.syscall_arg st s2 t b 2#u64 + let descriptor ← lift (i1 &&& 4294967295#u64) + let (_, s3) ← msg.set_mr st s2 t b 2#u64 descriptor + if pending + then + let s4 ← vcpu.set st s3 v vcpu.PENDING 0#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.MSG_INFO 1#u64 + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s6 layout.reg.NEXT_IP + let s7 ← + obj.Tcb.set_reg st t s6 layout.reg.FAULT_IP ip + sched.set_thread_state st s7 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i2 ← 27#u64 * 8#u64 + let i3 ← vcpu.EXIT + i2 + let i4 ← vcpu.get st s3 v i3 + let i5 ← 1#u64 <<< 31#i32 + let i6 ← lift (i4 &&& i5) + ok (i6 != 0#u64) + else ok false + vcpu.finish_notification st arI s3 a t b + badge vectoring + else + let s3 ← vcpu.set st s2 v vcpu.PENDING 0#u64 + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 1#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (sB : S) (σB : Fl4Spec.State Unit), R sB σB ∧ + ∃ Lst' : U64 → List U64, VmBaseFR (L := L) arI sB σB V AR Z H Cs Ac Lst' D s' a + (repEnd σ0 t x0 pb v vs pending saved full badge (rpTailF vs F0)) ⦄ := by + have hk := v3_kv (ms := (List.range 30).map F0) (x0 := x0) (pb := pb) hkv hvt + rw [v3_get_eq, bind_ok] + by_cases hval : vs.exit.valid = true + · have hi0 : ¬ L.mem s1 (core.num.U64.wrapping_add v vcpu.VALID) = 0#u64 := fun h => by + rw [(J.rdValid0 hv hk).1 h] at hval; cases hval + rw [if_neg hi0, if_neg hi0, bind_ok] + unfold rpTailF + rw [if_pos hval] + exact WP.spec_mono (J.repFullRunning hVA hr hq hb hrs hv hkv hvt pending saved full hsaved hpf badge) + fun _ h => ⟨sA, σA, hR, h⟩ + · have hval' : vs.exit.valid = false := by simpa using hval + have hi0 : L.mem s1 (core.num.U64.wrapping_add v vcpu.VALID) = 0#u64 := (J.rdValid0 hv hk).2 hval' + rw [if_pos hi0] + obtain ⟨i1, hvi, hi, -⟩ := hVA a + refine WP.spec_bind (Pₘ := fun i => i = i1) (by rw [hvi, WP.spec_ok]) fun i1' hi1' => ?_ + subst hi1' + refine WP.spec_bind (J.rvIp hv hk hi) fun ip hip => ?_ + refine WP.spec_bind (J.rvCtl hv hk hi) fun ctl hctl => ?_ + refine WP.spec_bind (J.rvIntr hv hk hi) fun intr hintr => ?_ + refine WP.spec_bind (J.setMrF hr hq hb 0#u64 0 (by simp) (by omega) ip) ?_ + rintro ⟨_, s2⟩ J2 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J2.setMrF hr hq hb 1#u64 1 (by simp) (by omega) ctl) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J3.setMrF hr hq hb 2#u64 2 (by simp) (by omega) intr) ?_ + rintro ⟨_, s4⟩ J4 + try simp only [uncurry_apply_pair] + rw [v3_map_congr 30 (G := rpEntF vs F0) (fun k _ => by + unfold rpEntF + simp only [hip, hctl, hintr] + all_goals (by_cases e0 : k = 0 <;> by_cases e1 : k = 1 <;> by_cases e2 : k = 2 <;> simp_all))] at J4 + refine WP.spec_bind (hlp s4 J4) fun s5 ⟨sB, σB, hRB, J5⟩ => ?_ + have hk5 := v3_kv (ms := (List.range 30).map (gprOv vs (rpEntF vs F0) 15)) (x0 := x0) (pb := pb) hkv hvt + rw [v3_get_eq, bind_ok] + have hi0' : L.mem s5 (core.num.U64.wrapping_add v vcpu.VALID) = 0#u64 := (J5.rdValid0 hv hk5).2 hval' + rw [if_pos hi0'] + refine WP.spec_bind (J5.repCr hVA hr hq hb hv hkv hvt) fun s6 J6 => ?_ + unfold rpTailF + rw [if_neg hval] + exact WP.spec_mono (J6.repFullRunning hVA hr hq hb hrs hv hkv hvt pending saved full hsaved hpf badge) + fun _ h => ⟨sB, σB, hRB, h⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {a : A} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} +variable {σ0 : Fl4Spec.State Unit} {x0 : Fl4Spec.Tcb} {pb : Option Fl4Spec.ObjRef} {vs : Fl4Spec.VcpuState} + +/-- The VM-exit instance of the shared report proof. -/ +theorem VmBaseFR.repTail (hVA : ZvVirtAgree arI) {t b v : U64} {F0 : Nat → Fl4Spec.Word} {s1 : S} + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s1 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map F0))) + (hr : t ∈ Z.T) (hq : ∀ p, pb = some p → Endp.ipcBufOf true x0 = some p) (hb : JBufR Z.B b pb) + (hrs : x0.tcbState = .runningVM) (hv : v ∈ V) (hkv : σ0.kheap v.bv = some (.archObj (.vcpu vs))) + (hvt : v.bv ≠ t.bv) (pending saved full : Bool) (hsaved : saved = vs.exit.valid) + (hpf : pending = false → full = true) (badge : U64) (lp : S → Result S) + (R : S → Fl4Spec.State Unit → Prop) (hR : R sA σA) + (hlp : ∀ s4, VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s4 a + (jMrSt σ0 t.bv x0 pb ((List.range 30).map (rpEntF vs F0))) → + lp s4 ⦃ s5 => ∃ (sB : S) (σB : Fl4Spec.State Unit), R sB σB ∧ VmBaseFR (L := L) arI sB σB V AR Z H Cs Ac Lst D + s5 a (jMrSt σ0 t.bv x0 pb ((List.range 30).map (gprOv vs (rpEntF vs F0) 15))) ⦄) : + (do + let i ← vcpu.get st s1 v vcpu.VALID + if i = 0#u64 + then + let i1 ← arI.virtualization a + let ip ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.RIP + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RIP + let ctl ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.get st s1 v vcpu.svm.INTERCEPT_EXC + i2 >>> 32#i32 + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let intr ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.EVENT_INJ + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + let (_, s2) ← msg.set_mr st s1 t b 0#u64 ip + let (_, s3) ← msg.set_mr st s2 t b 1#u64 ctl + let (_, s4) ← msg.set_mr st s3 t b 2#u64 intr + let s5 ← lp s4 + let i2 ← vcpu.get st s5 v vcpu.VALID + let s6 ← + if i2 = 0#u64 + then + do + let i3 ← arI.virtualization a + let flags ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s5 v i4 + else vcpu.read_vmcs st s5 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i3 = 2#u64 + then vcpu.get st s5 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s5 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR3 + vcpu.get st s5 v i4 + else ok 0#u64 + let cr2 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR2 + vcpu.get st s5 v i4 + else vcpu.get st s5 v vcpu.CR2 + let (_, s7) ← msg.set_mr st s5 t b 7#u64 flags + let (_, s8) ← msg.set_mr st s7 t b 8#u64 int + let (_, s9) ← msg.set_mr st s8 t b 9#u64 cr3 + let (_, s10) ← msg.set_mr st s9 t b 29#u64 cr2 + ok s10 + else ok s5 + if full + then + let i3 ← msg.syscall_arg st s6 t b 2#u64 + let descriptor ← lift (i3 &&& 4294967295#u64) + let (_, s7) ← msg.set_mr st s6 t b 2#u64 descriptor + if pending + then + let s8 ← vcpu.set st s7 v vcpu.PENDING 0#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← + obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i4 ← 27#u64 * 8#u64 + let i5 ← vcpu.EXIT + i4 + let i6 ← vcpu.get st s7 v i5 + let i7 ← 1#u64 <<< 31#i32 + let i8 ← lift (i6 &&& i7) + ok (i8 != 0#u64) + else ok false + vcpu.finish_notification st arI s7 a t b + badge vectoring + else + let s7 ← vcpu.set st s6 v vcpu.PENDING 0#u64 + let s8 ← + obj.Tcb.set_reg st t s7 layout.reg.MSG_INFO 1#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s9 layout.reg.NEXT_IP + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s10 t sched.TS_RUNNING + else + let s2 ← + if i = 0#u64 + then + do + let i1 ← arI.virtualization a + let flags ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.RFLAGS + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s1 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.CR3 + vcpu.get st s1 v i2 + else ok 0#u64 + let cr2 ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.CR2 + vcpu.get st s1 v i2 + else vcpu.get st s1 v vcpu.CR2 + let (_, s3) ← msg.set_mr st s1 t b 7#u64 flags + let (_, s4) ← msg.set_mr st s3 t b 8#u64 int + let (_, s5) ← msg.set_mr st s4 t b 9#u64 cr3 + let (_, s6) ← msg.set_mr st s5 t b 29#u64 cr2 + ok s6 + else ok s1 + if full + then + let i1 ← msg.syscall_arg st s2 t b 2#u64 + let descriptor ← lift (i1 &&& 4294967295#u64) + let (_, s3) ← msg.set_mr st s2 t b 2#u64 descriptor + if pending + then + let s4 ← vcpu.set st s3 v vcpu.PENDING 0#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.MSG_INFO 1#u64 + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s6 layout.reg.NEXT_IP + let s7 ← + obj.Tcb.set_reg st t s6 layout.reg.FAULT_IP ip + sched.set_thread_state st s7 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i2 ← 27#u64 * 8#u64 + let i3 ← vcpu.EXIT + i2 + let i4 ← vcpu.get st s3 v i3 + let i5 ← 1#u64 <<< 31#i32 + let i6 ← lift (i4 &&& i5) + ok (i6 != 0#u64) + else ok false + vcpu.finish_notification st arI s3 a t b + badge vectoring + else + let s3 ← vcpu.set st s2 v vcpu.PENDING 0#u64 + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 1#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (sB : S) (σB : Fl4Spec.State Unit), R sB σB ∧ + ∃ Lst' : U64 → List U64, VmBaseFR (L := L) arI sB σB V AR Z H Cs Ac Lst' D s' a + (repEnd σ0 t x0 pb v vs pending saved full badge (rpTailF vs F0)) ⦄ := + J.repTailRunning hVA hr hq hb (Or.inr hrs) hv hkv hvt pending saved full hsaved hpf badge lp R hR hlp + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseFR.rvTriple (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {p : U64} (hp : p ∈ V) + {vs : Fl4Spec.VcpuState} (hk : σ.kheap p.bv = some (.archObj (.vcpu vs))) {i : U64} + (hi : i = 2#u64 ↔ Fl4Spec.virtKind = .svm) : + (if i = 2#u64 + then + do + let i1 ← vcpu.svm.RIP + let i2 ← vcpu.get st s p i1 + let i3 ← vcpu.get st s p vcpu.svm.INTERCEPT_EXC + let i4 ← i3 >>> 32#i32 + let i5 ← vcpu.get st s p vcpu.svm.EVENT_INJ + ok (i2, i4, i5) + else + do + let i1 ← vcpu.read_vmcs st s p vcpu.vmx.GUEST_RIP + let i2 ← + vcpu.read_vmcs st s p + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let i3 ← + vcpu.read_vmcs st s p + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + ok (i1, i2, i3)) ⦃ r => r.1.bv = vs.entryIp Fl4Spec.virtKind ∧ r.2.1.bv = vs.entryCtl Fl4Spec.virtKind ∧ + r.2.2.bv = vs.entryIntr Fl4Spec.virtKind ⦄ := by + have hR : vcpu.svm.RIP = ok 1400#u64 := by unfold vcpu.svm.RIP vcpu.svm.SAVE; rfl + by_cases hs : Fl4Spec.virtKind = .svm + · have h2 := hi.2 hs + have e1 := J.rvIp hp hk hi + rw [if_pos h2, hR, bind_ok, v3_get_eq, WP.spec_ok] at e1 + have e2 := J.rvCtl hp hk hi + rw [if_pos h2, v3_get_eq, bind_ok, v3_shr32, WP.spec_ok] at e2 + have e3 := J.rvIntr hp hk hi + rw [if_pos h2, v3_get_eq, WP.spec_ok] at e3 + rw [if_pos h2, hR, bind_ok, v3_get_eq, bind_ok, v3_get_eq, bind_ok, v3_shr32, bind_ok, v3_get_eq, bind_ok, + WP.spec_ok] + exact ⟨e1, e2, e3⟩ + · have h2 : ¬ i = 2#u64 := fun h => hs (hi.1 h) + have e1 := J.rvIp hp hk hi + rw [if_neg h2, v3_read_vmcs_eq, WP.spec_ok] at e1 + have e2 := J.rvCtl hp hk hi + rw [if_neg h2, v3_read_vmcs_eq, WP.spec_ok] at e2 + have e3 := J.rvIntr hp hk hi + rw [if_neg h2, v3_read_vmcs_eq, WP.spec_ok] at e3 + rw [if_neg h2, v3_read_vmcs_eq, bind_ok, v3_read_vmcs_eq, bind_ok, v3_read_vmcs_eq, bind_ok, WP.spec_ok] + exact ⟨e1, e2, e3⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +theorem VmBaseR.fullReply (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) : + vcpu.full_reply st s t ⦃ r => r = repFull x ⦄ := by + have B := J.base + unfold vcpu.full_reply + refine WP.spec_bind (Endp.obj.Tcb.reg.spec s t _) ?_ + rintro m rfl + rw [WP.spec_ok] + have h := ((B.xtop.irelK B.ks B.buf).1.toE.at_tcb ht hk).regs Fl4Spec.msg_info_register + rw [Endp.regIdx_msgInfo] at h + unfold repFull vcpu.NUM_MRS + rw [← h] + apply decide_eq_decide.2 + constructor + · intro e; rw [e]; rfl + · intro e + apply UScalar.eq_of_val_eq + show (L.mem s (Endp.regA t layout.reg.MSG_INFO)).bv.toNat = (30#u64 : U64).bv.toNat + rw [e]; rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **Branch: a pending exit** (module docstring). -/ +theorem VmBaseFR.repBrB (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {t : U64} (htT : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .runningVM) {b : U64} (hb : JBufR Z.B b (Endp.ipcBufOf true x)) + (hal : ∀ ms : List Fl4Spec.Word, ∀ p, Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ) + {v : U64} (hv0 : v ≠ 0#u64) (hvV : v ∈ V) {vs : Fl4Spec.VcpuState} + (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) (hvt' : v.bv ≠ t.bv) + (hTV : Fl4Spec.tcbVcpuOf σ t.bv = some v.bv) + {pending full : Bool} (hpend : pending = vs.exit.pending) (hp : pending = true) (hfull : full = Fl4Proofs.P3.repFull x) + (badge : U64) : + (do + let saved ← + if v != 0#u64 + then + do + let i ← vcpu.get st s v vcpu.VALID + ok (i != 0#u64) + else ok false + let s1 ← vcpu.report_loop0 st s t v pending b saved 0#u64 + if v = 0#u64 + then + vcpu.finish_notification st arI s1 a t b badge + false + else + let i ← vcpu.get st s1 v vcpu.VALID + if i = 0#u64 + then + let i1 ← arI.virtualization a + let ip ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.RIP + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RIP + let ctl ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.get st s1 v vcpu.svm.INTERCEPT_EXC + i2 >>> 32#i32 + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let intr ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.EVENT_INJ + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + let (_, s2) ← msg.set_mr st s1 t b 0#u64 ip + let (_, s3) ← msg.set_mr st s2 t b 1#u64 ctl + let (_, s4) ← msg.set_mr st s3 t b 2#u64 intr + let s5 ← vcpu.report_loop1 st s4 t v b 0#u64 + let i2 ← vcpu.get st s5 v vcpu.VALID + let s6 ← + if i2 = 0#u64 + then + do + let i3 ← arI.virtualization a + let flags ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s5 v i4 + else vcpu.read_vmcs st s5 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i3 = 2#u64 + then vcpu.get st s5 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s5 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR3 + vcpu.get st s5 v i4 + else ok 0#u64 + let cr2 ← + if i3 = 2#u64 + then do + let i4 ← vcpu.svm.CR2 + vcpu.get st s5 v i4 + else vcpu.get st s5 v vcpu.CR2 + let (_, s7) ← msg.set_mr st s5 t b 7#u64 flags + let (_, s8) ← msg.set_mr st s7 t b 8#u64 int + let (_, s9) ← msg.set_mr st s8 t b 9#u64 cr3 + let (_, s10) ← msg.set_mr st s9 t b 29#u64 cr2 + ok s10 + else ok s5 + if full + then + let i3 ← msg.syscall_arg st s6 t b 2#u64 + let descriptor ← lift (i3 &&& 4294967295#u64) + let (_, s7) ← msg.set_mr st s6 t b 2#u64 descriptor + if pending + then + let s8 ← vcpu.set st s7 v vcpu.PENDING 0#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← + obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i4 ← 27#u64 * 8#u64 + let i5 ← vcpu.EXIT + i4 + let i6 ← vcpu.get st s7 v i5 + let i7 ← 1#u64 <<< 31#i32 + let i8 ← lift (i6 &&& i7) + ok (i8 != 0#u64) + else ok false + vcpu.finish_notification st arI s7 a t b + badge vectoring + else + let s7 ← vcpu.set st s6 v vcpu.PENDING 0#u64 + let s8 ← + obj.Tcb.set_reg st t s7 layout.reg.MSG_INFO 1#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s9 layout.reg.NEXT_IP + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s10 t sched.TS_RUNNING + else + let s2 ← + if i = 0#u64 + then + do + let i1 ← arI.virtualization a + let flags ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.RFLAGS + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s1 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.CR3 + vcpu.get st s1 v i2 + else ok 0#u64 + let cr2 ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.CR2 + vcpu.get st s1 v i2 + else vcpu.get st s1 v vcpu.CR2 + let (_, s3) ← msg.set_mr st s1 t b 7#u64 flags + let (_, s4) ← msg.set_mr st s3 t b 8#u64 int + let (_, s5) ← msg.set_mr st s4 t b 9#u64 cr3 + let (_, s6) ← msg.set_mr st s5 t b 29#u64 cr2 + ok s6 + else ok s1 + if full + then + let i1 ← msg.syscall_arg st s2 t b 2#u64 + let descriptor ← lift (i1 &&& 4294967295#u64) + let (_, s3) ← msg.set_mr st s2 t b 2#u64 descriptor + if pending + then + let s4 ← vcpu.set st s3 v vcpu.PENDING 0#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.MSG_INFO 1#u64 + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s6 layout.reg.NEXT_IP + let s7 ← + obj.Tcb.set_reg st t s6 layout.reg.FAULT_IP ip + sched.set_thread_state st s7 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i2 ← 27#u64 * 8#u64 + let i3 ← vcpu.EXIT + i2 + let i4 ← vcpu.get st s3 v i3 + let i5 ← 1#u64 <<< 31#i32 + let i6 ← lift (i4 &&& i5) + ok (i6 != 0#u64) + else ok false + vcpu.finish_notification st arI s3 a t b + badge vectoring + else + let s3 ← vcpu.set st s2 v vcpu.PENDING 0#u64 + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 1#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ := by + subst hp + have hq : ∀ p, Endp.ipcBufOf true x = some p → Endp.ipcBufOf true x = some p := fun _ h => h + have J0 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) []) := by + rw [jMrSt_nil hk]; exact J + have hne : (v != 0#u64) = true := by simp [hv0] + refine WP.spec_bind (Pₘ := fun p => p = vs.exit.valid) (by + rw [if_pos hne, v3_get_eq, bind_ok, WP.spec_ok]; exact J.rdValid hvV hkv) fun saved hsaved => ?_ + have J0' : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) + ((List.range (0#u64 : U64).val).map (exitMrN (true || saved) vs))) := by + simpa using J0 + refine WP.spec_bind (VmBaseFR.repLoop0 htT hq hb (v := v) (pending := true) (saved := saved) (vs := vs) + (fun _ => ⟨hvV, hkv, hvt'⟩) 0#u64 (by simp) s J0') fun s1 J1 => ?_ + refine v3_ite_spec (fun h => absurd h hv0) (fun _ => ?_) + refine WP.spec_mono (J1.repTail hVA htT hq hb hrs hvV hkv hvt' true saved full hsaved (fun h => by cases h) badge + (fun s4 => vcpu.report_loop1 st s4 t v b 0#u64) (fun sB σB => sB = sA ∧ σB = σA) ⟨rfl, rfl⟩ + (fun s4 J4 => WP.spec_mono (VmBaseFR.repLoop1 htT hq hb hvV hkv hvt' _ 0#u64 + (by simp) s4 (by rw [v3_gpr0]; exact J4)) fun _ J5 => ⟨sA, σA, ⟨rfl, rfl⟩, J5⟩)) fun s' h' => ?_ + obtain ⟨_, _, ⟨rfl, rfl⟩, Lst', J'⟩ := h' + refine ⟨_, Lst', ?_, J'⟩ + rw [v3_vmReportF_some hk hTV hkv badge.bv (hal _), v3_repR_pend x vs badge.bv hpend.symm, hfull, hsaved] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **Branch: no pending exit, full-state caller, with a VCPU** (module docstring). -/ +theorem VmBaseFR.repBrC2Running (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {t : U64} (htT : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .running ∨ x.tcbState = .runningVM) {b : U64} (hb : JBufR Z.B b (Endp.ipcBufOf true x)) + (hal : ∀ ms : List Fl4Spec.Word, ∀ p, Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ) + {v : U64} (hv0 : v ≠ 0#u64) (hvV : v ∈ V) {vs : Fl4Spec.VcpuState} + (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) (hvt' : v.bv ≠ t.bv) + (hTV : Fl4Spec.tcbVcpuOf σ t.bv = some v.bv) + {pending full : Bool} (hpend : pending = vs.exit.pending) (hp : pending = false) (hf : full = true) + (hfull : full = Fl4Proofs.P3.repFull x) (badge : U64) : + (do + let saved ← + if v != 0#u64 + then + do + let i ← vcpu.get st s v vcpu.VALID + ok (i != 0#u64) + else ok false + let s1 ← vcpu.report_loop2 st s t v pending b saved 0#u64 + if v = 0#u64 + then + vcpu.finish_notification st arI s1 a t b + badge false + else + let i ← vcpu.get st s1 v vcpu.VALID + if i = 0#u64 + then + let i1 ← arI.virtualization a + let ip ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.RIP + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RIP + let ctl ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.get st s1 v vcpu.svm.INTERCEPT_EXC + i2 >>> 32#i32 + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let intr ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.EVENT_INJ + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + let (_, s2) ← msg.set_mr st s1 t b 0#u64 ip + let (_, s3) ← msg.set_mr st s2 t b 1#u64 ctl + let (_, s4) ← msg.set_mr st s3 t b 2#u64 intr + let s5 ← vcpu.report_loop3 st s4 t v b 0#u64 + let i2 ← vcpu.get st s5 v vcpu.VALID + let s6 ← + if i2 = 0#u64 + then + do + let i3 ← arI.virtualization a + let flags ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s5 v i4 + else vcpu.read_vmcs st s5 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i3 = 2#u64 + then vcpu.get st s5 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s5 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.CR3 + vcpu.get st s5 v i4 + else ok 0#u64 + let cr2 ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.CR2 + vcpu.get st s5 v i4 + else vcpu.get st s5 v vcpu.CR2 + let (_, s7) ← msg.set_mr st s5 t b 7#u64 flags + let (_, s8) ← msg.set_mr st s7 t b 8#u64 int + let (_, s9) ← msg.set_mr st s8 t b 9#u64 cr3 + let (_, s10) ← msg.set_mr st s9 t b 29#u64 cr2 + ok s10 + else ok s5 + if full + then + let i3 ← msg.syscall_arg st s6 t b 2#u64 + let descriptor ← lift (i3 &&& 4294967295#u64) + let (_, s7) ← msg.set_mr st s6 t b 2#u64 descriptor + if pending + then + let s8 ← vcpu.set st s7 v vcpu.PENDING 0#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← + obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i4 ← 27#u64 * 8#u64 + let i5 ← vcpu.EXIT + i4 + let i6 ← vcpu.get st s7 v i5 + let i7 ← 1#u64 <<< 31#i32 + let i8 ← lift (i6 &&& i7) + ok (i8 != 0#u64) + else ok false + vcpu.finish_notification st arI s7 a t + b badge vectoring + else + let s7 ← vcpu.set st s6 v vcpu.PENDING 0#u64 + let s8 ← + obj.Tcb.set_reg st t s7 layout.reg.MSG_INFO 1#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s9 layout.reg.NEXT_IP + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s10 t sched.TS_RUNNING + else + let s2 ← + if i = 0#u64 + then + do + let i1 ← arI.virtualization a + let flags ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.RFLAGS + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s1 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.CR3 + vcpu.get st s1 v i2 + else ok 0#u64 + let cr2 ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.CR2 + vcpu.get st s1 v i2 + else vcpu.get st s1 v vcpu.CR2 + let (_, s3) ← msg.set_mr st s1 t b 7#u64 flags + let (_, s4) ← msg.set_mr st s3 t b 8#u64 int + let (_, s5) ← msg.set_mr st s4 t b 9#u64 cr3 + let (_, s6) ← msg.set_mr st s5 t b 29#u64 cr2 + ok s6 + else ok s1 + if full + then + let i1 ← msg.syscall_arg st s2 t b 2#u64 + let descriptor ← lift (i1 &&& 4294967295#u64) + let (_, s3) ← msg.set_mr st s2 t b 2#u64 descriptor + if pending + then + let s4 ← vcpu.set st s3 v vcpu.PENDING 0#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.MSG_INFO 1#u64 + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s6 layout.reg.NEXT_IP + let s7 ← + obj.Tcb.set_reg st t s6 layout.reg.FAULT_IP ip + sched.set_thread_state st s7 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i2 ← 27#u64 * 8#u64 + let i3 ← vcpu.EXIT + i2 + let i4 ← vcpu.get st s3 v i3 + let i5 ← 1#u64 <<< 31#i32 + let i6 ← lift (i4 &&& i5) + ok (i6 != 0#u64) + else ok false + vcpu.finish_notification st arI s3 a t + b badge vectoring + else + let s3 ← vcpu.set st s2 v vcpu.PENDING 0#u64 + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 1#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ := by + subst hp hf + have hq : ∀ p, Endp.ipcBufOf true x = some p → Endp.ipcBufOf true x = some p := fun _ h => h + have J0 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) []) := by + rw [jMrSt_nil hk]; exact J + have hne : (v != 0#u64) = true := by simp [hv0] + refine WP.spec_bind (Pₘ := fun p => p = vs.exit.valid) (by + rw [if_pos hne, v3_get_eq, bind_ok, WP.spec_ok]; exact J.rdValid hvV hkv) fun saved hsaved => ?_ + have J0' : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) + ((List.range (0#u64 : U64).val).map (exitMrN (false || saved) vs))) := by + simpa using J0 + refine WP.spec_bind (VmBaseFR.repLoop2 htT hq hb (v := v) (pending := false) (saved := saved) (vs := vs) + (fun _ => ⟨hvV, hkv, hvt'⟩) 0#u64 (by simp) s J0') fun s1 J1 => ?_ + refine v3_ite_spec (fun h => absurd h hv0) (fun _ => ?_) + refine WP.spec_mono (J1.repTailRunning hVA htT hq hb hrs hvV hkv hvt' false saved true hsaved (fun _ => rfl) badge + (fun s4 => vcpu.report_loop3 st s4 t v b 0#u64) (fun sB σB => sB = sA ∧ σB = σA) ⟨rfl, rfl⟩ + (fun s4 J4 => WP.spec_mono (VmBaseFR.repLoop3 htT hq hb hvV hkv hvt' _ 0#u64 + (by simp) s4 (by rw [v3_gpr0]; exact J4)) fun _ J5 => ⟨sA, σA, ⟨rfl, rfl⟩, J5⟩)) fun s' h' => ?_ + obtain ⟨_, _, ⟨rfl, rfl⟩, Lst', J'⟩ := h' + refine ⟨_, Lst', ?_, J'⟩ + rw [v3_vmReportF_some hk hTV hkv badge.bv (hal _), v3_repR_full x vs badge.bv hpend.symm hfull.symm, hsaved] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **Branch: no pending exit, full-state caller, without VCPU** (module docstring). -/ +theorem VmBaseFR.repBrC1 (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {t : U64} (htT : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .runningVM) {b : U64} (hb : JBufR Z.B b (Endp.ipcBufOf true x)) + (hal : ∀ ms : List Fl4Spec.Word, ∀ p, Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ) + {v : U64} (hv0 : v = 0#u64) (hTV : Fl4Spec.tcbVcpuOf σ t.bv = none) + {pending full : Bool} (hp : pending = false) (hf : full = true) (hfull : full = Fl4Proofs.P3.repFull x) (badge : U64) : + (do + let saved ← + if v != 0#u64 + then + do + let i ← vcpu.get st s v vcpu.VALID + ok (i != 0#u64) + else ok false + let s1 ← vcpu.report_loop2 st s t v pending b saved 0#u64 + if v = 0#u64 + then + vcpu.finish_notification st arI s1 a t b + badge false + else + let i ← vcpu.get st s1 v vcpu.VALID + if i = 0#u64 + then + let i1 ← arI.virtualization a + let ip ← + if i1 = 2#u64 + then do + let i2 ← vcpu.svm.RIP + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RIP + let ctl ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.get st s1 v vcpu.svm.INTERCEPT_EXC + i2 >>> 32#i32 + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let intr ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.EVENT_INJ + else + vcpu.read_vmcs st s1 v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + let (_, s2) ← msg.set_mr st s1 t b 0#u64 ip + let (_, s3) ← msg.set_mr st s2 t b 1#u64 ctl + let (_, s4) ← msg.set_mr st s3 t b 2#u64 intr + let s5 ← vcpu.report_loop3 st s4 t v b 0#u64 + let i2 ← vcpu.get st s5 v vcpu.VALID + let s6 ← + if i2 = 0#u64 + then + do + let i3 ← arI.virtualization a + let flags ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.RFLAGS + vcpu.get st s5 v i4 + else vcpu.read_vmcs st s5 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i3 = 2#u64 + then vcpu.get st s5 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s5 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.CR3 + vcpu.get st s5 v i4 + else ok 0#u64 + let cr2 ← + if i3 = 2#u64 + then + do + let i4 ← vcpu.svm.CR2 + vcpu.get st s5 v i4 + else vcpu.get st s5 v vcpu.CR2 + let (_, s7) ← msg.set_mr st s5 t b 7#u64 flags + let (_, s8) ← msg.set_mr st s7 t b 8#u64 int + let (_, s9) ← msg.set_mr st s8 t b 9#u64 cr3 + let (_, s10) ← msg.set_mr st s9 t b 29#u64 cr2 + ok s10 + else ok s5 + if full + then + let i3 ← msg.syscall_arg st s6 t b 2#u64 + let descriptor ← lift (i3 &&& 4294967295#u64) + let (_, s7) ← msg.set_mr st s6 t b 2#u64 descriptor + if pending + then + let s8 ← vcpu.set st s7 v vcpu.PENDING 0#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.MSG_INFO 1#u64 + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s10 layout.reg.NEXT_IP + let s11 ← + obj.Tcb.set_reg st t s10 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s11 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i4 ← 27#u64 * 8#u64 + let i5 ← vcpu.EXIT + i4 + let i6 ← vcpu.get st s7 v i5 + let i7 ← 1#u64 <<< 31#i32 + let i8 ← lift (i6 &&& i7) + ok (i8 != 0#u64) + else ok false + vcpu.finish_notification st arI s7 a t + b badge vectoring + else + let s7 ← vcpu.set st s6 v vcpu.PENDING 0#u64 + let s8 ← + obj.Tcb.set_reg st t s7 layout.reg.MSG_INFO 1#u64 + let s9 ← + obj.Tcb.set_reg st t s8 layout.reg.BADGE 0#u64 + let ip1 ← obj.Tcb.reg st t s9 layout.reg.NEXT_IP + let s10 ← + obj.Tcb.set_reg st t s9 layout.reg.FAULT_IP ip1 + sched.set_thread_state st s10 t sched.TS_RUNNING + else + let s2 ← + if i = 0#u64 + then + do + let i1 ← arI.virtualization a + let flags ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.RFLAGS + vcpu.get st s1 v i2 + else vcpu.read_vmcs st s1 v vcpu.vmx.GUEST_RFLAGS + let int ← + if i1 = 2#u64 + then vcpu.get st s1 v vcpu.svm.INT_STATE + else + vcpu.read_vmcs st s1 v + vcpu.vmx.GUEST_INTERRUPTABILITY + let cr3 ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.CR3 + vcpu.get st s1 v i2 + else ok 0#u64 + let cr2 ← + if i1 = 2#u64 + then + do + let i2 ← vcpu.svm.CR2 + vcpu.get st s1 v i2 + else vcpu.get st s1 v vcpu.CR2 + let (_, s3) ← msg.set_mr st s1 t b 7#u64 flags + let (_, s4) ← msg.set_mr st s3 t b 8#u64 int + let (_, s5) ← msg.set_mr st s4 t b 9#u64 cr3 + let (_, s6) ← msg.set_mr st s5 t b 29#u64 cr2 + ok s6 + else ok s1 + if full + then + let i1 ← msg.syscall_arg st s2 t b 2#u64 + let descriptor ← lift (i1 &&& 4294967295#u64) + let (_, s3) ← msg.set_mr st s2 t b 2#u64 descriptor + if pending + then + let s4 ← vcpu.set st s3 v vcpu.PENDING 0#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.MSG_INFO 1#u64 + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s6 layout.reg.NEXT_IP + let s7 ← + obj.Tcb.set_reg st t s6 layout.reg.FAULT_IP ip + sched.set_thread_state st s7 t sched.TS_RUNNING + else + let vectoring ← + if saved + then + do + let i2 ← 27#u64 * 8#u64 + let i3 ← vcpu.EXIT + i2 + let i4 ← vcpu.get st s3 v i3 + let i5 ← 1#u64 <<< 31#i32 + let i6 ← lift (i4 &&& i5) + ok (i6 != 0#u64) + else ok false + vcpu.finish_notification st arI s3 a t + b badge vectoring + else + let s3 ← vcpu.set st s2 v vcpu.PENDING 0#u64 + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 1#u64 + let s5 ← + obj.Tcb.set_reg st t s4 layout.reg.BADGE 0#u64 + let ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ := by + subst hp hf + have hq : ∀ p, Endp.ipcBufOf true x = some p → Endp.ipcBufOf true x = some p := fun _ h => h + have J0 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) []) := by + rw [jMrSt_nil hk]; exact J + refine WP.spec_bind (Pₘ := fun p => p = false) (by + rw [if_neg (by simp [hv0])]; exact (WP.spec_ok _).2 rfl) fun saved hsaved => ?_ + subst hsaved + have J0' : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) + ((List.range (0#u64 : U64).val).map (exitMrN (false || false) default))) := by + simpa using J0 + refine WP.spec_bind (VmBaseFR.repLoop2 htT hq hb (v := v) (pending := false) (saved := false) (vs := default) + (fun h => absurd h (by simp)) 0#u64 (by simp) s J0') fun s1 J1 => ?_ + refine v3_ite_spec (fun _ => ?_) (fun h => absurd hv0 h) + refine WP.spec_mono (J1.repFinN hVA htT hq hb hrs badge false) fun s' h' => ?_ + obtain ⟨Lst', J'⟩ := h' + refine ⟨_, Lst', ?_, J'⟩ + rw [v3_vmReportF_none hk hTV badge.bv (hal _), v3_repR_noneFull x badge.bv hfull.symm] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **Branch: no pending exit, not full, with a VCPU** (module docstring). -/ +theorem VmBaseFR.repBrA2 (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {t : U64} (htT : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .runningVM) {b : U64} (hb : JBufR Z.B b (Endp.ipcBufOf true x)) + (hal : ∀ ms : List Fl4Spec.Word, ∀ p, Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ) + {v : U64} (hv0 : v ≠ 0#u64) (hvV : v ∈ V) {vs : Fl4Spec.VcpuState} + (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) (hvt' : v.bv ≠ t.bv) + (hTV : Fl4Spec.tcbVcpuOf σ t.bv = some v.bv) + {pending full : Bool} (hpend : pending = vs.exit.pending) (hp : pending = false) (hf : full = false) + (hfull : full = Fl4Proofs.P3.repFull x) (badge : U64) : + (do + let i ← arI.virtualization a + let (ip, ctl, intr) ← + if v = 0#u64 + then ok (0#u64, 0#u64, 0#u64) + else + if i = 2#u64 + then + do + let i1 ← vcpu.svm.RIP + let i2 ← vcpu.get st s v i1 + let i3 ← vcpu.get st s v vcpu.svm.INTERCEPT_EXC + let i4 ← i3 >>> 32#i32 + let i5 ← vcpu.get st s v vcpu.svm.EVENT_INJ + ok (i2, i4, i5) + else + do + let i1 ← vcpu.read_vmcs st s v vcpu.vmx.GUEST_RIP + let i2 ← + vcpu.read_vmcs st s v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let i3 ← + vcpu.read_vmcs st s v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + ok (i1, i2, i3) + let (_, s1) ← msg.set_mr st s t b 0#u64 ip + let (_, s2) ← msg.set_mr st s1 t b 1#u64 ctl + let (_, s3) ← msg.set_mr st s2 t b 2#u64 intr + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 0#u64 + let s5 ← obj.Tcb.set_reg st t s4 layout.reg.BADGE badge + let next_ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP next_ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ := by + subst hp hf + have hq : ∀ p, Endp.ipcBufOf true x = some p → Endp.ipcBufOf true x = some p := fun _ h => h + have J0 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) []) := by + rw [jMrSt_nil hk]; exact J + obtain ⟨i, hvi, hi, -⟩ := hVA a + refine WP.spec_bind (Pₘ := fun i' => i' = i) (by rw [hvi, WP.spec_ok]) fun i' hi' => ?_ + subst hi' + refine WP.spec_bind (Pₘ := fun r : U64 × U64 × U64 => r.1.bv = vs.entryIp Fl4Spec.virtKind ∧ + r.2.1.bv = vs.entryCtl Fl4Spec.virtKind ∧ r.2.2.bv = vs.entryIntr Fl4Spec.virtKind) + (v3_ite_spec (fun h => absurd h hv0) (fun _ => J.rvTriple hvV hkv hi)) ?_ + rintro ⟨ip, ctl, intr⟩ ⟨h1, h2, h3⟩ + simp only at h1 h2 h3 + refine WP.spec_bind (J0.setMr htT hq hb 0#u64 (by simp) (by simp) ip) ?_ + rintro ⟨_, s1⟩ J1 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J1.setMr htT hq hb 1#u64 (by simp [mrUpd]) (by simp [mrUpd]) ctl) ?_ + rintro ⟨_, s2⟩ J2 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J2.setMr htT hq hb 2#u64 (by simp [mrUpd]) (by simp [mrUpd]) intr) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + rw [v3_list3, h1, h2, h3] at J3 + refine WP.spec_mono (J3.repFin htT (jMrSt_rcv _ _ _ _ _) hrs 0#u64 badge) fun s' h' => ?_ + obtain ⟨Lst', J'⟩ := h' + refine ⟨_, Lst', ?_, J'⟩ + rw [v3_vmReportF_some hk hTV hkv badge.bv (hal _), v3_repR_short x vs badge.bv hpend.symm hfull.symm] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **Branch: no pending exit, not full, without VCPU** (module docstring). -/ +theorem VmBaseFR.repBrA1 (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) + {t : U64} (htT : t ∈ Z.T) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .runningVM) {b : U64} (hb : JBufR Z.B b (Endp.ipcBufOf true x)) + (hal : ∀ ms : List Fl4Spec.Word, ∀ p, Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ) + {v : U64} (hv0 : v = 0#u64) (hTV : Fl4Spec.tcbVcpuOf σ t.bv = none) + {pending full : Bool} (hp : pending = false) (hf : full = false) (hfull : full = Fl4Proofs.P3.repFull x) (badge : U64) : + (do + let i ← arI.virtualization a + let (ip, ctl, intr) ← + if v = 0#u64 + then ok (0#u64, 0#u64, 0#u64) + else + if i = 2#u64 + then + do + let i1 ← vcpu.svm.RIP + let i2 ← vcpu.get st s v i1 + let i3 ← vcpu.get st s v vcpu.svm.INTERCEPT_EXC + let i4 ← i3 >>> 32#i32 + let i5 ← vcpu.get st s v vcpu.svm.EVENT_INJ + ok (i2, i4, i5) + else + do + let i1 ← vcpu.read_vmcs st s v vcpu.vmx.GUEST_RIP + let i2 ← + vcpu.read_vmcs st s v + vcpu.vmx.CONTROL_PRIMARY_PROCESSOR_CONTROLS + let i3 ← + vcpu.read_vmcs st s v + vcpu.vmx.CONTROL_ENTRY_INTERRUPTION_INFO + ok (i1, i2, i3) + let (_, s1) ← msg.set_mr st s t b 0#u64 ip + let (_, s2) ← msg.set_mr st s1 t b 1#u64 ctl + let (_, s3) ← msg.set_mr st s2 t b 2#u64 intr + let s4 ← + obj.Tcb.set_reg st t s3 layout.reg.MSG_INFO 0#u64 + let s5 ← obj.Tcb.set_reg st t s4 layout.reg.BADGE badge + let next_ip ← obj.Tcb.reg st t s5 layout.reg.NEXT_IP + let s6 ← + obj.Tcb.set_reg st t s5 layout.reg.FAULT_IP next_ip + sched.set_thread_state st s6 t sched.TS_RUNNING) ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ := by + subst hp hf + have hq : ∀ p, Endp.ipcBufOf true x = some p → Endp.ipcBufOf true x = some p := fun _ h => h + have J0 : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a (jMrSt σ t.bv x (Endp.ipcBufOf true x) []) := by + rw [jMrSt_nil hk]; exact J + obtain ⟨i, hvi, hi, -⟩ := hVA a + refine WP.spec_bind (Pₘ := fun i' => i' = i) (by rw [hvi, WP.spec_ok]) fun i' hi' => ?_ + subst hi' + refine WP.spec_bind (Pₘ := fun r : U64 × U64 × U64 => r = (0#u64, 0#u64, 0#u64)) + (v3_ite_spec (fun _ => (WP.spec_ok _).2 rfl) (fun h => absurd hv0 h)) ?_ + rintro ⟨ip, ctl, intr⟩ hr + simp only [Prod.mk.injEq] at hr + obtain ⟨rfl, rfl, rfl⟩ := hr + refine WP.spec_bind (J0.setMr htT hq hb 0#u64 (by simp) (by simp) 0#u64) ?_ + rintro ⟨_, s1⟩ J1 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J1.setMr htT hq hb 1#u64 (by simp [mrUpd]) (by simp [mrUpd]) 0#u64) ?_ + rintro ⟨_, s2⟩ J2 + try simp only [uncurry_apply_pair] + refine WP.spec_bind (J2.setMr htT hq hb 2#u64 (by simp [mrUpd]) (by simp [mrUpd]) 0#u64) ?_ + rintro ⟨_, s3⟩ J3 + try simp only [uncurry_apply_pair] + rw [v3_list3] at J3 + refine WP.spec_mono (J3.repFin htT (jMrSt_rcv _ _ _ _ _) hrs 0#u64 badge) fun s' h' => ?_ + obtain ⟨Lst', J'⟩ := h' + refine ⟨_, Lst', ?_, J'⟩ + rw [v3_vmReportF_none hk hTV badge.bv (hal _), v3_repR_noneShort x badge.bv hfull.symm] + rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- **`vcpu.report` writes no `JW` word and leaves the thread out of `TS_RUNNING_VM`** (module docstring). -/ +theorem VmBaseFR.reportFoot (hA : P0.ArchTotal arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) (badge : U64) : + vcpu.report st arI s a t badge ⦃ s' => L.mem s' (P1.stA t) ≠ 8#u64 ∧ ∀ b, JW V Z.th b → L.mem s' b = L.mem s b ⦄ := by + have Jz := J.toVmBaseR + have B := Jz.base + have Dp := B.detPl Jz.vm + have Da := B.detAtB Jz.vm + have P := Dp.pl + have hta := Dp.ta t ht + have hfr : ∀ s1, P1.WritesIn st s s1 (fun b => ¬ JW V Z.th b) → ∀ b, JW V Z.th b → L.mem s1 b = L.mem s b := + fun s1 h b hb => h.mem_eq (P.jw b hb) fun h' => h' hb + refine WP.spec_mono (P1.vcpu.report.gen (Bok := P1.BufOK (BufU Z σ)) (vOk := fun v => v ∈ V) + (P := fun s1 => P1.WritesIn st s s1 (fun b => ¬ JW V Z.th b)) + (Q := fun s1 => L.mem s1 (P1.stA t) ≠ 8#u64 ∧ P1.WritesIn st s s1 (fun b => ¬ JW V Z.th b)) hA + ⟨fun s1 b i v hb h1 hi => WP.spec_mono (set_mr_w s1 hta hb (Dp.ua t ht) (by omega)) fun r hr => + (h1.trans hr).mono fun b' hb' => hb'.elim id fun h => (Dp.rep t ht b' h).1, + fun s1 r v h1 hr => WP.spec_mono (P1.obj.Tcb.set_reg.wrS s1 t r v hta (by + rcases hr with rfl | rfl | rfl <;> + simp [layout.reg.MSG_INFO, layout.reg.BADGE, layout.reg.FAULT_IP])) + fun s' ⟨K, hK, e⟩ => by + subst e + have hr' : r.val < 384 := by + rcases hr with rfl | rfl | rfl <;> + simp [layout.reg.MSG_INFO, layout.reg.BADGE, layout.reg.FAULT_IP] + exact (h1.trans (P1.writesIn_wr (P1.field_aligned hta (by omega) (by omega)))).mono fun b hb => + hb.elim id fun e => (Dp.rep t ht b (Or.inl ⟨K, by omega, by omega, by omega, e⟩)).1, + fun s1 v h1 hv => by + unfold vcpu.set + simp only [lift, bind_ok] + exact WP.spec_mono (P1.LawfulStore.write_spec s1 _ _) fun s' e => by + subst e + exact (h1.trans (P1.writesIn_wr (Dp.vca v hv).1)).mono fun b hb => + hb.elim id fun e => (Dp.vc v hv b (Or.inl e)).1⟩ + (fun s1 h1 => WP.spec_mono (sts_w s1 t sched.TS_RUNNING (P.sw _ ⟨t, ht, rfl⟩) (by rw [actA_of h1]; exact Da.actA_al) + (by rw [actA_of h1]; exact fun e => (Da.nd _ (Or.inr rfl)).2 ⟨t, ht, e⟩)) fun s' ⟨w, e⟩ => ⟨by + rw [e]; simp [sched.TS_RUNNING], by + rw [actA_of h1] at w + exact (h1.trans w).mono fun b hb => hb.elim id fun h => h.elim (fun e => P.swjw b ⟨t, ht, e⟩) + fun e => (Da.nd b (Or.inr e)).1⟩) + (P1.WritesIn.refl _ _) (Da.buf s (P1.WritesIn.refl _ _) t ht) (fun h0 _ => by + have h0' : L.mem s (tVcpuA t) ≠ 0#u64 := h0 + exact ((Jz.extv.bnd t ht).resolve_left h0').1)) fun s' ⟨h8, hw⟩ => ⟨h8, hfr s' hw⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **The refinement part of `ReportJ3`** (module docstring). -/ +theorem VmBaseFR.reportRunnableJ (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .runningVM ∨ + (x.tcbState = .running ∧ Fl4Proofs.P3.repFull x = true ∧ ∃ v vs, + Fl4Spec.tcbVcpuOf σ t.bv = some v ∧ σ.kheap v = some (.archObj (.vcpu vs)) ∧ + vs.exit.pending = false)) (badge : U64) : + vcpu.report st arI s a t badge ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ := by + have Jz := J.toVmBaseR + have B := Jz.base + have hO := J.vo + have htT := B.thT t ht + have hOK := B.bok + have hBC := bufCover_of_frameCover (T := Z.T) B.bok B.fcov + obtain ⟨xr', hkr', hwr⟩ := B.buf t htT + rw [hk] at hkr'; cases hkr' + have hEP := B.xtop.fin.1.2.ep + have hbuf : ∀ b : U64, ((b = 0#u64 ↔ Endp.ipcBufOf true x = none) ∧ + ((L.mem s (tIpcBufA t)).bv = x.tcbIpcBuffer → ∀ p', Endp.ipcBufOf true x = some p' → b.bv = p')) → + JBufR Z.B b (Endp.ipcBufOf true x) := by + rintro b ⟨hrw0, hrwp⟩ + cases hrb : Endp.ipcBufOf true x with + | none => exact hrw0.2 hrb + | some rp => + have hrwv : b.bv = rp := hrwp hwr rp hrb + exact ⟨(fun e => by have := hrw0.1 e; rw [hrb] at this; cases this), hBC t htT x true rp hk hrb b hrwv, hrwv⟩ + have halF : ∀ b : U64, JBufR Z.B b (Endp.ipcBufOf true x) → ∀ ms : List Fl4Spec.Word, ∀ p, + Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ := by + intro b hb ms p hp q hq + have := (List.of_mem_zip hq).1 + simp at this + have hb' := hb + rw [hp] at hb' + exact ⟨Endp.bufAddr_aligned hEP hb'.2.1 _, buf_frame_of_ok hOK hk hp _ (by omega)⟩ + unfold vcpu.report + refine WP.spec_bind (v3_bound s t) fun v ev' => ?_ + by_cases hv0 : v = 0#u64 + · have h0 : L.mem s (tVcpuA t) = 0#u64 := ev'.symm.trans hv0 + have hTV : Fl4Spec.tcbVcpuOf σ t.bv = none := zv_tcbVcpuOf_none fun w ws hw hwt => by + obtain ⟨u, hu, -, h1, -⟩ := J.specBound hO ht hw hwt + exact Jz.extv.nz u hu (h1.symm.trans h0) + have hrs : x.tcbState = .runningVM := hrs.resolve_right (by + rintro ⟨_, _, v, vs, hv, _, _⟩ + rw [hTV] at hv + cases hv) + refine WP.spec_bind (Pₘ := fun p => p = false) (by + rw [if_neg (by simp [hv0])]; exact (WP.spec_ok _).2 rfl) fun pending hpend => ?_ + subst hpend + refine WP.spec_bind (lookup_buf_full s σ B.xtop.rel.2.1.1 B.xtop.rel.2.1.2.1 ht hk hOK true) fun b hb0 => ?_ + have hb := hbuf b hb0 + refine WP.spec_bind (J.fullReply htT hk) fun full hfull => ?_ + refine v3_ite_spec (fun h => absurd h (by simp)) (fun _ => ?_) + refine v3_ite_spec (fun hf => ?_) (fun hf => ?_) + · exact J.repBrC1 hVA htT hk hrs hb (halF b hb) hv0 hTV rfl hf hfull badge + · exact J.repBrA1 hVA htT hk hrs hb (halF b hb) hv0 hTV rfl (by simpa using hf) hfull badge + · have hne0 : L.mem s (tVcpuA t) ≠ 0#u64 := fun h => hv0 (ev'.trans h) + obtain ⟨hvV, hvt⟩ := (Jz.extv.bnd t ht).resolve_left hne0 + rw [← ev'] at hvV hvt + obtain ⟨vs, hkv, hbv⟩ := J.vb v hvV + have hvs : Fl4Spec.VcpuState.tcb vs = some t.bv := by + rcases hbv with ⟨-, h0⟩ | ⟨t', -, h1, h2⟩ + · have e : t = 0#u64 := hvt.symm.trans h0 + subst e + exact absurd ht (B.detPl Jz.vm).h0 + · have e : t' = t := h2.symm.trans hvt + rw [h1, e] + have hTV : Fl4Spec.tcbVcpuOf σ t.bv = some v.bv := zv_tcbVcpuOf_eq hkv hvs fun w ws hw hwt => by + obtain ⟨u, hu, rfl, h1, -⟩ := J.specBound hO ht hw hwt + rw [← h1, ← ev'] + have hvt' : v.bv ≠ t.bv := fun e => by rw [e, hk] at hkv; cases hkv + have hne : (v != 0#u64) = true := by simp [hv0] + refine WP.spec_bind (Pₘ := fun p => p = vs.exit.pending) (by + rw [if_pos hne, v3_get_eq, bind_ok, WP.spec_ok]; exact J.rdPend hvV hkv) fun pending hpend => ?_ + refine WP.spec_bind (lookup_buf_full s σ B.xtop.rel.2.1.1 B.xtop.rel.2.1.2.1 ht hk hOK true) fun b hb0 => ?_ + have hb := hbuf b hb0 + refine WP.spec_bind (J.fullReply htT hk) fun full hfull => ?_ + have hRun : x.tcbState = .running ∨ x.tcbState = .runningVM := by + rcases hrs with he | ⟨he, -⟩ + · exact Or.inr he + · exact Or.inl he + have hHost : x.tcbState ≠ .runningVM → pending = false ∧ full = true := by + intro hNotVM + obtain ⟨_, hf, v', vs', hv', hk', hp'⟩ := hrs.resolve_left hNotVM + rw [hTV, Option.some.injEq] at hv' + subst v' + rw [hkv, Option.some.injEq, Fl4Spec.KernelObject.archObj.injEq, + Fl4Spec.ArchKernelObj.vcpu.injEq] at hk' + subst vs' + exact ⟨hpend.trans hp', hfull.trans hf⟩ + refine v3_ite_spec (fun hp => ?_) (fun hp => ?_) + · have hrs' : x.tcbState = .runningVM := by + by_contra hNotVM + rw [(hHost hNotVM).1] at hp + cases hp + exact J.repBrB hVA htT hk hrs' hb (halF b hb) hv0 hvV hkv hvt' hTV hpend hp hfull badge + · have hp' : pending = false := by simpa using hp + refine v3_ite_spec (fun hf => ?_) (fun hf => ?_) + · exact J.repBrC2Running hVA htT hk hRun hb (halF b hb) hv0 hvV hkv hvt' hTV hpend hp' hf hfull badge + · have hrs' : x.tcbState = .runningVM := by + by_contra hNotVM + exact hf (hHost hNotVM).2 + exact J.repBrA2 hVA htT hk hrs' hb (halF b hb) hv0 hvV hkv hvt' hTV hpend hp' (by simpa using hf) hfull badge + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- Report after a VCPU has exited to FL4. -/ +theorem VmBaseFR.reportJ3M (hVA : ZvVirtAgree arI) (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) + (hrs : x.tcbState = .runningVM) (badge : U64) : + vcpu.report st arI s a t badge ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst' D s' a σr ⦄ + := + J.reportRunnableJ hVA ht hk (Or.inl hrs) badge + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {sA : S} {σA : Fl4Spec.State Unit} + +/-- **`ReportJ3`'s post at every `VmBaseFR` state, for a `runningVM` thread** (`JAtZV3.reportJ3` over `VmBaseFR`, +gen 42b42; P3/JAtZV3Report's `ReportJ3` with `VmBaseFR` for `JAtZV3`). -/ +theorem VmBaseFR.reportJ3 (hA : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (J : VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : Z.th t = some .tcb) + (hrun : ∃ x, σ.kheap t.bv = some (.tcb x) ∧ x.tcbState = .runningVM) (badge : U64) : + vcpu.report st arI s a t badge ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmBaseFR (L := L) arI sA σA V AR Z H Cs Ac' Lst' D s' a σr ∧ L.mem s' (P1.stA t) ≠ 8#u64 ∧ + ∀ b, JW V Z.th b → L.mem s' b = L.mem s b ⦄ := by + obtain ⟨x, hk, hrs⟩ := hrun + refine WP.spec_mono (WP.spec_and (J.reportJ3M hVA ht hk hrs badge) (J.reportFoot hA ht badge)) ?_ + rintro s' ⟨⟨σr, Lst', h1, h2⟩, h3, h4⟩ + exact ⟨σr, Ac, Lst', h1, h2, h3, h4⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} + +/-- A table of `X` is `H`'s `.other 12` (`SpecCover.arch` + `ArchFitHyp`). -/ +theorem JAtZRB.eptHeap (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (C : EptCore (L := L) X XL Z.th s σ) {t : U64} (ht : t ∈ X) : H t = some (.other 12) := by + obtain ⟨es, hk, -⟩ := C.rel.obj t ht + obtain ⟨n, hn, hH⟩ := B.cover.arch t.bv _ hk + have hf := B.carry.archFit t.bv _ n hk hH + rw [ept_bits] at hf + have e : n = 12 := by omega + subst e + exact hH + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} + +/-- **Every other object of `H` misses a table of `X`.** -/ +theorem JAtZRB.eptApartH (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (C : EptCore (L := L) X XL Z.th s σ) {t : U64} (ht : t ∈ X) : + ∀ p k, H p = some k → p ≠ t → p.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ p.val := by + intro p k hp hne + rcases B.kern.heap.disj p k t _ hp (B.eptHeap C ht) hne with h | h + · exact Or.inl h + · simp only [P1.HKind.bits, Nat.reducePow] at h; exact Or.inr h + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched + +/-- **`OObj` words miss every table of `X`** (the argument of `JAtZRB.oobjV`, with `EptCore.ks` / `sep`). -/ +theorem JAtZRB.oobjX (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : EptCore (L := L) X XL Z.th s σ) + {t : U64} (ht : t ∈ X) {b : U64} (hb : OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) : + b.val < t.val ∨ t.val + 4096 ≤ b.val := by + have dH : ∀ p k, H p = some k → k ≠ .other 12 → p.val + 2 ^ k.bits ≤ t.val ∨ t.val + 4096 ≤ p.val := by + intro p k hp hk + have hne : p ≠ t := fun e => by subst e; rw [B.eptHeap C ht] at hp; cases hp; exact hk rfl + exact B.eptApartH C ht p k hp hne + have hks := C.ks t ht + rcases hb with ⟨g1, g2⟩ | ⟨g1, g2, -⟩ | ⟨u, hu, g1, g2⟩ | ⟨e, he, g1, g2⟩ | ⟨r, hr, g1, g2⟩ | ⟨n, hn, g1, g2⟩ | + ⟨sc, hsc, x, n, hk, hso⟩ | ⟨wp, hwp, g1, g2⟩ + · obtain ⟨Q, R0, hcq, -⟩ := B.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hcq.cur ▸ B.kern.cpuCs + have hc64 := B.kern.csLt Z.c hcC + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + rw [hnb] at g1 g2 + omega + · omega + · have d := dH u _ (B.zH.1 u hu) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · have d := dH e _ (B.zH.2.1 e he) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · have d := dH r _ (B.zH.2.2.1 r hr) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · have d := dH n _ (B.zH.2.2.2 n hn) (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d; omega + · obtain ⟨Tr, Cq, hJ⟩ := B.jpl + have hH := (hJ.sc sc x n (B.c0 sc hsc) hk).2 + have d := dH sc _ hH (by intro h; cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + unfold Sched.ScObj at hso + omega + · obtain ⟨q, vms, hq, q1, q2⟩ := B.carry.bufPg wp hwp + obtain ⟨es, hk, -⟩ := C.rel.obj t ht + have hne : q ≠ t.bv := fun e' => by rw [e', hk] at hq; cases hq + have eb : Fl4Spec.objBits (.archObj (.dataPage false vms)) = Fl4Spec.pageBitsForSize vms := by + simp [Fl4Spec.objBits, Fl4Spec.archKobjSize] + rcases C.sep t ht q _ hq hne with e1 | e1 + · rw [eb] at e1; omega + · omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} + +/-- **`EptCore` across a native step** (`OObj` writes; the spec's tables at `X` kept, no new spec guest table, no new +pool entry, sizes kept — the shape of `VcpuCore.ofBSR`). -/ +theorem EptCore.ofBSR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : EptCore (L := L) X XL Z.th s σ) + {s' : S} {σ' : Fl4Spec.State Unit} {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hO : ∀ b, W b → Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) + (hxk : ∀ t ∈ X, σ'.kheap t.bv = σ.kheap t.bv) + (hne : ∀ r l es, σ'.kheap r = some (.archObj (.ept l es)) → + ∃ l' es', σ.kheap r = some (.archObj (.ept l' es'))) + (hge : ∀ e, GuestEntry σ' e → GuestEntry σ e) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hnm : EptNamed Z.th (L.mem s') σ') : + EptCore (L := L) X XL Z.th s' σ' := by + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := B.xtop.slotsSame_of hw hO + exact C.frame hw (fun b hb t ht => B.oobjX C ht (hO b hb)) + (fun y hy p l a v mm hc => ⟨hy, by rw [← hSS.capV hy]; exact hc⟩) + (C.exte.of_eq fun y hy => hSS.capV hy) hxk hne hge hsz (EptSlotStep.ofSS hSS) hnm + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} + +/-- **`EptCore` across an SC-code step** (`TFoot` writes, `ScTKw`; as `VcpuCore.ofTKR`). -/ +theorem EptCore.ofTKR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : EptCore (L := L) X XL Z.th s σ) + {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw (fun _ => False) σ σ') : + EptCore (L := L) X XL Z.th s' σ' := by + have hS := B.xtop.fin.1.1.1 + obtain ⟨h1, h2, -⟩ := zv_tk hK + have hA := ept_tk_arch hK + refine C.ofBSR B hw (fun b hb => Sched.tfoot_oobj hS hb) (fun t ht => ?_) (fun r l es hk => ⟨l, es, hA _ _ hk⟩) + (fun e ⟨hi, p, f, lo, g1, g2, g3⟩ => ⟨hi, p, f, lo, by rw [← hK.arch]; exact g1, hA _ _ g2, g3⟩) h2 + (C.named.ofSS (B.xtop.slotsSame_of hw (fun b hb => Sched.tfoot_oobj hS hb)) (by rw [hK.arch]) hA) + obtain ⟨es, hk, -⟩ := C.rel.obj t ht + exact h1 _ _ hk + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} + +/-- `VmEptStateR` across an SC-code step. -/ +theorem VmEptStateR.ofTKR (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw (fun _ => False) σ σ') : + VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D' s' a' σ' := + ⟨J.1.ofTKR B' hw hK, J.2.ofTKR J.1.1 hw hK⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + {XL : U64 → Nat} + +/-- `VmEptStateR` with another arch state relating to the same CR3. -/ +theorem VmEptStateR.archV (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {a' : A} + (hAR : AR a' σ.archState.x64CurrentCr3) : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a' σ := + ⟨J.1.archV hAR, J.2⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- A `VmEptStateR` gives the shared record (its base and `VcpuCore` fields). -/ +theorem VmEptStateR.toB (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ := + ⟨J.1.1, J.1.2.extv, J.1.2.rvm, J.1.2.vm, J.1.2.vl, J.1.2.vb, J.1.2.vo, J.1.2.vr⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- A `VmEptStateR` as the start of a run. -/ +theorem VmEptStateR.toF (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + VmBaseFR (L := L) arI s σ V AR Z H Cs Ac Lst D s a σ := + J.toB.toF + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **A step whose writes miss the ASID-table words keeps the ASID table** (gen 42b46: `JAtZRB.asidFoot`'s proof for +any footprint `W`, the end state at any `Z.mv Z.G V'`; `asidFoot` its corollary). -/ +theorem JAtZRB.asidW (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hn : ∀ hi : U64, hi.val < 2048 → ¬ W (atA (L.ksb s) hi)) : + σ'.archState.x64AsidTable = σ.archState.x64AsidTable := by + have hR := B.xtop.fin.2.1 + have hR' := B'.xtop.fin.2.1 + have hVP := B.xtop.fin.1.2.vs + have hk : L.ksb s' = L.ksb s := hw.1 + funext x + obtain ⟨hi, hh, hx⟩ : ∃ hi : U64, hi.val < 2048 ∧ Fl4Spec.ucast 11 hi.bv = x := by + have hlt : x.toNat < 2048 := x.isLt + refine ⟨⟨BitVec.ofNat 64 x.toNat⟩, ?_, ?_⟩ + · show (BitVec.ofNat 64 x.toNat).toNat < 2048 + rw [BitVec.toNat_ofNat]; omega + · rw [ucast_ofNat] + apply BitVec.eq_of_toNat_eq + show (BitVec.ofNat 11 (BitVec.ofNat 64 x.toNat).toNat).toNat = x.toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat]; omega + have hv := atA_val hVP.ksb_fit hh + have hm : L.mem s' (atA (L.ksb s) hi) = L.mem s (atA (L.ksb s) hi) := + hw.mem_eq (by unfold P1.Aligned; have := hVP.ksb_al; rw [hv]; omega) (hn hi hh) + rw [← hx, hR'.table hi hh, hk, hm, ← hR.table hi hh] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **A run keeps the ASID table** (module docstring; gen 42b46: `JAtZRB.asidW`'s corollary). -/ +theorem JAtZRB.asidFoot (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') (F : VmFoot (L := L) V Z s σ s' σ') : + σ'.archState.x64AsidTable = σ.archState.x64AsidTable := + B.asidW (V' := Z.vs) B' F.fw fun hi hh h => by + have hv := atA_val B.xtop.fin.1.2.vs.ksb_fit hh + rcases h with hO | ⟨v, hvV, g1, g2⟩ + · exact B.xtop.fin.1.2.disj _ hO (Or.inr (Or.inr (Or.inr ⟨by omega, by omega⟩))) + · rcases Jv.ks v hvV with e | e <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **`EptCore` at the end of a run** (module docstring). -/ +theorem EptCore.ofFootR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) + (C : EptCore (L := L) X XL Z.th s σ) {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') + (F : VmFoot (L := L) V Z s σ s' σ') : EptCore (L := L) X XL Z.th s' σ' := by + have hS := F.ss + have hat := B.asidFoot Jv B' F + have hAo : ∀ p ao, σ'.kheap p = some (.archObj ao) → (∀ vs, ao ≠ .vcpu vs) → + σ.kheap p = some (.archObj ao) := by + intro p ao hp hn + rcases F.fk p with e | ⟨ko, ko', h1, h2, hc⟩ + · rw [← e]; exact hp + · rw [hp] at h2 + cases h2 + rcases hc with ⟨_, _, -, e⟩ | ⟨_, _, _, -, e⟩ | ⟨_, _, -, e⟩ | ⟨_, _, -, e⟩ | ⟨_, y, -, e⟩ <;> cases e + exact absurd rfl (hn y) + refine C.frame F.fw (fun b hb t ht => ?_) (fun y hy p l a v mm hc => ⟨hy, by rw [← hS.capV hy]; exact hc⟩) + (C.exte.of_eq fun y hy => hS.capV hy) (fun t ht => ?_) + (fun r l es hk => ⟨l, es, hAo _ _ hk (fun _ h => by cases h)⟩) + (fun e ⟨hi, p, f, lo, g1, g2, g3⟩ => ⟨hi, p, f, lo, by rw [← hat]; exact g1, hAo _ _ g2 (fun _ h => by cases h), g3⟩) + F.fk.sz (EptSlotStep.ofSS hS) ?_ + · rcases hb with hO | ⟨v, hv, g1, g2⟩ + · exact B.oobjX C ht hO + · rcases C.apartV Jv t ht v hv with h | h <;> omega + · obtain ⟨es, hk, -⟩ := C.rel.obj t ht + rcases F.fk t.bv with e | ⟨ko, ko', h1, h2, hc⟩ + · exact e + · rw [hk] at h1 + cases h1 + rcases hc with ⟨_, _, e, -⟩ | ⟨_, _, _, e, -⟩ | ⟨_, _, e, -⟩ | ⟨_, _, e, -⟩ | ⟨_, _, e, -⟩ <;> cases e + · exact C.named.frameA hat + (fun _ _ _ ⟨p, f, h1, h2, h3⟩ _ => ⟨p, f, by rw [← hat]; exact h1, hAo _ _ h2 (fun _ h => by cases h), h3⟩) + (fun r l es h => hAo _ _ h fun _ h => by cases h) + (fun y p lv a v hy hc _ => ⟨y, hy, by rw [hS.capV hy]; exact hc⟩) + (fun y p lv mm a v hy hc => ⟨y, hy, by rw [← hS.capV hy]; exact hc⟩) + (fun _ _ _ _ _ _ y _ _ _ _ hy _ hl hc => ⟨y, _, _, _, hy, hl, by rw [hS.capV hy]; exact hc⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- Rebuild the VM relation after a run with a witness for each bound TCB. -/ +theorem VmEptStateR.ofFTCap (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {Ac' : Set U64} + {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (J' : VmBaseFR (L := L) arI s σ V AR Z H Cs Ac' Lst' D' s' a' σ') + (htc : TCappedM V Z.th (L.mem s')) : + VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D' s' a' σ' := by + have hS := J'.ss + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.1.1.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact ⟨⟨J'.base, J'.extv, J'.vo, J'.vr, + VirtRngOne.rngOne_congr J.1.2.rngOne fun z hz _ => by rw [hS.capV (hsl z hz)]; exact NoA.refl _, + J'.rvm, J'.vm, J'.vl, J'.vb, fun v hv => by + obtain ⟨y, hy, hc⟩ := J.1.2.vcap v hv + exact ⟨y, hy, (hS.capV hy).trans hc⟩, + htc⟩, + J.2.ofFootR J.1.1 J.1.2.vm J'.base J'.toVmFoot⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **`VmEptStateR` at the end of a run from a `VmEptStateR` state** (module docstring): `htc`, no bound VCPU's `TCB` +word changed. -/ +theorem VmEptStateR.ofF (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {Ac' : Set U64} + {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (J' : VmBaseFR (L := L) arI s σ V AR Z H Cs Ac' Lst' D' s' a' σ') + (htc : ∀ v ∈ V, L.mem s' (vTcbA v) ≠ 0#u64 → L.mem s' (vTcbA v) = L.mem s (vTcbA v)) : + VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D' s' a' σ' := + J.ofFTCap J' (J.1.2.tcap.of_caps (fun v hv h => ⟨hv, htc v hv h⟩) fun y hy _ hc => + ⟨y, hy, (J'.ss.capV hy).trans hc⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **A native step's words miss every VCPU image of `V`** (module docstring): an `OObj` word by `JAtZRB.oobjV`, a +timer head (below `ksb + 288000`) by `JVm.ks`. -/ +theorem JAtZRB.natV (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (Jv : JVm (L := L) V H s σ) {b : U64} + (h : NatW (L := L) Z s σ b) : ∀ u ∈ V, b.val < u.val ∨ u.val + 32768 ≤ b.val := by + intro u hu + rcases h.1 with hO | ⟨c, hc, hb⟩ + · exact B.oobjV Jv hu hO + · have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + rcases Jv.ks u hu with e | e <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **A native step keeps the ASID table** (`JAtZRB.asidW`: an `OObj` word is no ASID-table word, `TopPlaced.disj`; a +timer head lies below `ksb + 288000`). -/ +theorem JAtZRB.asidNat (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') (F : NatFoot (L := L) Z s σ s' σ') : + σ'.archState.x64AsidTable = σ.archState.x64AsidTable := + B.asidW B' F.fw fun hi hh h => by + have hv := atA_val B.xtop.fin.1.2.vs.ksb_fit hh + rcases h.1 with hO | ⟨c, hc, hb⟩ + · exact B.xtop.fin.1.2.disj _ hO (Or.inr (Or.inr (Or.inr ⟨by omega, by omega⟩))) + · have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **`EptCore` after a native step** (module docstring). -/ +theorem EptCore.ofNatR (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : EptCore (L := L) X XL Z.th s σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') (F : NatFoot (L := L) Z s σ s' σ') : + EptCore (L := L) X XL Z.th s' σ' := by + have hS := F.ss + have hat := B.asidNat B' F + have hAo : ∀ p ao, σ'.kheap p = some (.archObj ao) → σ.kheap p = some (.archObj ao) := F.fk.2 + refine C.frame F.fw (fun b hb t ht => ?_) (fun y hy p l a v mm hc => ⟨hy, by rw [← hS.capV hy]; exact hc⟩) + (C.exte.of_eq fun y hy => hS.capV hy) (fun t ht => ?_) + (fun r l es hk => ⟨l, es, hAo _ _ hk⟩) + (fun e ⟨hi, p, f, lo, g1, g2, g3⟩ => ⟨hi, p, f, lo, by rw [← hat]; exact g1, hAo _ _ g2, g3⟩) + F.fk.1.2.1 (EptSlotStep.ofSS hS) ?_ + · rcases hb.1 with hO | ⟨c, hc, hb'⟩ + · exact B.oobjX C ht hO + · have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + rcases C.ks t ht with e | e <;> omega + · obtain ⟨es, hk, -⟩ := C.rel.obj t ht + exact F.fk.1.1 _ _ hk + · exact C.named.frameA hat + (fun _ _ _ ⟨p, f, h1, h2, h3⟩ _ => ⟨p, f, by rw [← hat]; exact h1, hAo _ _ h2, h3⟩) + (fun r l es h => hAo _ _ h) + (fun y p lv a v hy hc _ => ⟨y, hy, by rw [hS.capV hy]; exact hc⟩) + (fun y p lv mm a v hy hc => ⟨y, hy, by rw [← hS.capV hy]; exact hc⟩) + (fun _ _ _ _ _ _ y _ _ _ _ hy _ hl hc => ⟨y, _, _, _, hy, hl, by rw [hS.capV hy]; exact hc⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **`VmEptStateR` after a native step** (module docstring; P3/NatFoot's `NatFoot` from a `VmEptStateR` state to a +`JAtZRB` state at any `Z.mv Z.G V'`). -/ +theorem VmEptStateR.ofNatR (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') (F : NatFoot (L := L) Z s σ s' σ') : + VmEptStateR (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ' := + ⟨⟨B', J.1.2.ofBSSR J.1.1 B' F.fw F.ss (fun u hu b hb => J.1.1.natV J.1.2.vm hb u hu) (fun b hb => hb.2.2) + (J.1.1.vkeep_of F.fw fun _ h => h.2.1) + (fun u hu => by obtain ⟨y, hy⟩ := J.1.2.vm.spec u hu; exact F.fk.1.1 _ _ hy) + F.fk.1.2.1 F.fk.1.2.2.1 F.fk.1.2.2.2⟩, + J.2.ofNatR J.1.1 B' F⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **FL4's `vcpu.report` at `VmEptStateR`, for a `runningVM` thread** (`VmBaseFR.reportJ3`; module docstring). -/ +theorem VmEptStateR.reportJ3 (hA : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : Z.th t = some .tcb) + (hrun : ∃ x, σ.kheap t.bv = some (.tcb x) ∧ x.tcbState = .runningVM) (badge : U64) : + vcpu.report st arI s a t badge ⦃ s' => ∃ (σr : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64), + Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ = ({((), σr)}, False) ∧ + VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a σr ∧ L.mem s' (P1.stA t) ≠ 8#u64 ∧ + ∀ b, JW V Z.th b → L.mem s' b = L.mem s b ⦄ := + WP.spec_mono (J.toF.reportJ3 hA hVA ht hrun badge) fun _ ⟨σr, Ac', Lst', h1, J', h2, h3⟩ => + ⟨σr, Ac', Lst', h1, J.ofF J' fun v hv _ => h3 _ (Or.inr (Or.inr ⟨v, hv, rfl⟩)), h2, h3⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **The spec's report changes only the thread and a VCPU object; the thread ends `running`** (module docstring). +(mmuAD gen 42b71) At a `JAtZRB` state (only the base is read); `JAtZV3.repKh` its corollary. -/ +theorem JAtZRB.repKh (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) {b : Fl4Spec.Word} + {σr : Fl4Spec.State Unit} (hr : Fl4Spec.vmReportF (Z := Unit) t.bv b σ = ({((), σr)}, False)) : + (∀ o, o ≠ t.bv → (∀ vs, σ.kheap o ≠ some (.archObj (.vcpu vs))) → σr.kheap o = σ.kheap o) ∧ + (∃ y, σr.kheap t.bv = some (.tcb y) ∧ y.tcbState = .running) ∧ σr.domainTime = σ.domainTime := by + have htT := B.thT t ht + have hOK := B.bok + have hBC := bufCover_of_frameCover (T := Z.T) B.bok B.fcov + have hEP := B.xtop.fin.1.2.ep + have halG : ∀ ms : List Fl4Spec.Word, ∀ p, Endp.ipcBufOf true x = some p → ∀ q ∈ Endp.mrPairs ms, + (Endp.bufAddr p q.1).toNat % 8 = 0 ∧ Fl4Spec.inUserFrame (Endp.bufAddr p q.1) σ := by + intro ms p hp q hq + have := (List.of_mem_zip hq).1 + simp at this + have hm : Endp.BufMap Z.B ({ bv := p } : U64) p := hBC t htT x true p hk hp { bv := p } rfl + exact ⟨Endp.bufAddr_aligned hEP hm _, buf_frame_of_ok hOK hk hp _ (by omega)⟩ + cases hvo : Fl4Spec.tcbVcpuOf σ t.bv with + | none => + rw [v3_vmReportF_none hk hvo b (halG _)] at hr + have e := congrArg Prod.snd (v3_out_eq hr) + dsimp only at e + rw [← e] + exact ⟨fun o ho _ => by rw [v3_repTail_kh _ _ _ _ _ ho, v3_jMrSt_kh _ _ _ _ _ ho], v3_repTail_kt _ _ _ _ _, + by rw [v3_repTail_dt, v3_jMrSt_dt]⟩ + | some v => + obtain ⟨vs, hkv⟩ := v3_tcbVcpuOf_some hvo + rw [v3_vmReportF_some hk hvo hkv b (halG _)] at hr + have e := congrArg Prod.snd (v3_out_eq hr) + dsimp only at e + rw [← e] + refine ⟨fun o ho hnv => ?_, v3_repTail_kt _ _ _ _ _, ?_⟩ + swap + · rw [v3_repTail_dt] + split + · show (jMrSt σ t.bv x _ _).domainTime = _ + exact v3_jMrSt_dt _ _ _ _ _ + · exact v3_jMrSt_dt _ _ _ _ _ + rw [v3_repTail_kh _ _ _ _ _ ho] + have hov : o ≠ v := fun e => hnv vs (by rw [e]; exact hkv) + split + · simp only [updK_kheap, Function.update_of_ne hov] + exact v3_jMrSt_kh _ _ _ _ _ ho + · exact v3_jMrSt_kh _ _ _ _ _ ho + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **FL4's `signal_completes` is the spec's `vmSignalCompletes` with the oracles read off FL4's state** (module +docstring). (mmuAD gen 42b71) At a `VmBaseR` state (the fields read are the shared record's); `JAtZV3.sigCompR` its +corollary. -/ +theorem VmBaseR.sigCompR (J : VmBaseR (L := L) arI V AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) : + vcpu.signal_completes st s t ⦃ b => + Fl4Spec.vmSignalCompletes (Z := Unit) (v3OnCore st s) (v3RemCur st s) t.bv σ = ({(b, σ)}, False) ⦄ := by + have B := J.base + have hO := J.vo + have htT := B.thT t ht + unfold vcpu.signal_completes + refine WP.spec_bind (v3_bound s t) fun v ev' => ?_ + refine WP.spec_bind (P1.obj.Tcb.affinity.spec s t) fun cpu ecpu => ?_ + by_cases hv0 : v = 0#u64 + · have h0 : L.mem s (tVcpuA t) = 0#u64 := ev'.symm.trans hv0 + have hTV : Fl4Spec.tcbVcpuOf σ t.bv = none := zv_tcbVcpuOf_none fun w ws hw hwt => by + obtain ⟨u, hu, -, h1, -⟩ := J.specBound hO ht hw hwt + exact J.extv.nz u hu (h1.symm.trans h0) + rw [if_neg (by simp [hv0])] + exact (WP.spec_ok _).2 (v3_sigComp_none _ _ hTV) + have hne0 : L.mem s (tVcpuA t) ≠ 0#u64 := fun h => hv0 (ev'.trans h) + obtain ⟨hvV, hvt⟩ := (J.extv.bnd t ht).resolve_left hne0 + rw [← ev'] at hvV hvt + obtain ⟨vs, hkv, hbv⟩ := J.vb v hvV + have hvs : Fl4Spec.VcpuState.tcb vs = some t.bv := by + rcases hbv with ⟨-, h0⟩ | ⟨t', -, h1, h2⟩ + · have e : t = 0#u64 := hvt.symm.trans h0 + subst e + exact absurd ht (B.detPl J.vm).h0 + · have e : t' = t := h2.symm.trans hvt + rw [h1, e] + have hTV : Fl4Spec.tcbVcpuOf σ t.bv = some v.bv := zv_tcbVcpuOf_eq hkv hvs fun w ws hw hwt => by + obtain ⟨u, hu, rfl, h1, -⟩ := J.specBound hO ht hw hwt + rw [← h1, ← ev'] + have hS := v3_sigComp_some (v3OnCore st s) (v3RemCur st s) hk hTV hkv + rw [if_pos (by simp [hv0]), v3_get_eq, bind_ok] + have hpd := J.toF.rdPend hvV hkv + by_cases hp : vs.exit.pending = true + · have hi : ¬ L.mem s (core.num.U64.wrapping_add v vcpu.PENDING) = 0#u64 := by + intro e; rw [e, hp] at hpd; simp at hpd + rw [if_neg hi] + refine (WP.spec_ok _).2 ?_ + rw [hS] + simp [hp] + have hp' : vs.exit.pending = false := by simpa using hp + have hi : L.mem s (core.num.U64.wrapping_add v vcpu.PENDING) = 0#u64 := by + by_contra e + have : (L.mem s (core.num.U64.wrapping_add v vcpu.PENDING) != 0#u64) = true := by simpa using e + rw [hpd, hp'] at this; cases this + rw [if_pos hi] + refine WP.spec_bind (P1.LawfulStore.cur_cpu_spec s) fun c ec => ?_ + have hon : v3OnCore st s t.bv = decide (cpu = c) := by + unfold v3OnCore; rw [ecpu, ec]; try rfl + by_cases hcc : cpu = c + · rw [if_pos hcc] + refine (WP.spec_ok _).2 ?_ + rw [hS] + simp [hp', hon, hcc] + rw [if_neg hcc] + refine WP.spec_bind (J.fullReply htT hk) fun full hfull => ?_ + subst hfull + cases hf : Fl4Proofs.P3.repFull x with + | false => + rw [if_neg Bool.false_ne_true] + refine (WP.spec_ok _).2 ?_ + rw [hS] + have hf' : decide (x.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register = 30) = false := hf + have hf2 := of_decide_eq_false hf' + simp [hp', hon, hcc, hf2] + try exact fun h => absurd h hf2 + | true => + rw [if_pos rfl] + refine WP.spec_bind (P1.obj.node_get.spec s cpu _) fun i2 ei2 => ?_ + refine (WP.spec_ok _).2 ?_ + have hf' : decide (x.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register = 30) = true := hf + have hrc0 : v3RemCur st s t.bv = decide (i2 = t) := by + unfold v3RemCur; rw [ei2, ecpu]; try rfl + have key : decide (vs.exit.pending = false ∧ (v3OnCore st s t.bv = true ∨ + (decide (x.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register = 30) = true ∧ + v3RemCur st s t.bv = false))) = (i2 != t) := by + rw [hp', hon, hf', hrc0, decide_eq_false hcc] + by_cases e : i2 = t + · subst e; simp + · rw [decide_eq_false e, show (i2 != t) = true from bne_iff_ne.mpr e]; decide + rw [hS, key] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +set_option maxHeartbeats 16000000 in +/-- **FL4's walk of `send_signal` to the `RunningVM` branch** (module docstring): completing, the whole outcome; +not completing, the activation as `SingleRelI` with `AR` kept. (mmuAD gen 42b71, ruling (c2) item 2) Over any VM-epoch +record `P` that gives `VmBaseR` (`hB`), is kept by FL4's report (`hRep`) and by the tail (`hTl`, `SigVmTailJ3`'s +statement at `P`); `JAtZV3.signalVmW` is it at `JAtZV3`, P3/VmSignal's `VmEptStateR.signalVmW` at `VmEptStateR`. +(gen 42b73) `hRep` / `hTl` only for the bound thread `t`, the report and the tail into records `Pm` / `Pf` of their +own, and the not-completing outcome records FL4's `signal_completes` result (`ok false`), for P3/VmSignal's +`VmEptStateR.signalVmK`. -/ +theorem signalVmWGR {P Pm Pf : Set U64 → (U64 → List U64) → S → A → Fl4Spec.State Unit → Prop} {t : U64} + (hB : ∀ {Ac' : Set U64} {Lst' : U64 → List U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit}, + P Ac' Lst' s' a' σ' → VmBaseR (L := L) arI V AR Z H Cs Ac' Lst' D s' a' σ') + (hRep : ∀ {Ac' : Set U64} {Lst' : U64 → List U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit}, + P Ac' Lst' s' a' σ' → Z.th t = some .tcb → + (∃ x, σ'.kheap t.bv = some (.tcb x) ∧ x.tcbState = .runningVM) → ∀ badge : U64, + vcpu.report st arI s' a' t badge ⦃ s'' => ∃ (σr : Fl4Spec.State Unit) (Ac'' : Set U64) + (Lst'' : U64 → List U64), Fl4Spec.vmReportF (Z := Unit) t.bv badge.bv σ' = ({((), σr)}, False) ∧ + Pm Ac'' Lst'' s'' a' σr ⦄) + (hTl : ∀ (Ac' : Set U64) (Lst' : U64 → List U64) (s1 : S) (a' : A) (σr : Fl4Spec.State Unit) (n : U64), + Pm Ac' Lst' s1 a' σr → Z.th t = some .tcb → n ∈ Z.N → + (∃ x, σr.kheap t.bv = some (.tcb x) ∧ x.tcbState = .running) → + (∃ y, σr.kheap n.bv = some (.notification y) ∧ y.ntfnObj = .idleNtfn ∧ y.ntfnBoundTcb = some t.bv) → + sigVmTailF st arI s1 a' t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac'' : Set U64) (Lst'' : U64 → List U64), + sigVmTailS n.bv t.bv σr = ({((), σ')}, False) ∧ σ'.domainTime = σr.domainTime ∧ Pf Ac'' Lst'' r.1 r.2 σ' ⦄) + (hAT : P0.ArchTotal arI) (hK : ARKeep arI AR) + (J : P Ac Lst s a σ) {n : U64} (hn : n ∈ Z.N) + {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + (hb : x.ntfnBoundTcb = some t.bv) {y : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb y)) + (hrs : y.tcbState = .runningVM) (badge : U64) : + notification.send_signal st arI s a n badge ⦃ r => + (∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64), + Fl4Spec.sendSignalVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) n.bv badge.bv σ = ({((), σ')}, False) ∧ + σ'.domainTime = σ.domainTime ∧ Pf Ac' Lst' r.1 r.2 σ') ∨ + (Fl4Spec.sendSignalVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) n.bv badge.bv σ = + Fl4Spec.sendSignal n.bv badge.bv σ ∧ AR r.2 σ.archState.x64CurrentCr3 ∧ + SingleRelI (st := st) Z.N Z.T Z.E Z.R Z.B s σ (Fl4Spec.sendSignal n.bv badge.bv σ) r.1 ∧ + vcpu.signal_completes st s t = ok false) ⦄ := by + have B := (hB J).base + have hI := B.xtop.irelK B.ks B.buf + have hR := hI.1 + have hAR : AR a σ.archState.x64CurrentCr3 := B.xtop.fin.1.1.2.2.2.2.2.2 + have hx := hR.toN.at_ntfn hn hk + obtain ⟨hb0, hbt⟩ := Notif.optRef_some (hx.bound.symm.trans hb) + have hbw : L.mem s (nBoundA n) = t := P1.eq_of_bv hbt + have hbT : t ∈ Z.T := hbw ▸ hx.bound_mem hb0 + have ht0 : t ≠ 0#u64 := hbw ▸ hb0 + have hth : Z.th t = some .tcb := B.thTcb hbT + have hy := hR.toN.at_tcb hbT hkt + have h8 : L.mem s (tStA t) = sched.TS_RUNNING_VM := by + apply UScalar.eq_of_val_eq + rw [hy.st, hrs] + simp [tsCode, sched.TS_RUNNING_VM] + have hns : L.mem s (tStA t) ≠ sched.TS_BLOCKED_ON_RECV := by + rw [h8]; unfold sched.TS_RUNNING_VM sched.TS_BLOCKED_ON_RECV; decide + unfold notification.send_signal + refine WP.spec_bind (P1.obj.Ntfn.state.spec s n) fun nst enst => ?_ + rw [if_pos (by rw [enst, epState_eq]; exact stOf_idle hx.obj hidle)] + refine WP.spec_bind (Notif.obj.Ntfn.bound_tcb.spec s n) fun i ei => ?_ + rw [hbw] at ei + rw [ei] + refine WP.spec_bind (P1.obj.Tcb.is_null.spec t) fun b eb => ?_ + have hb' : b = false := by rw [eb]; exact decide_eq_false ht0 + rw [hb'] + rw [if_neg Bool.false_ne_true] + refine WP.spec_bind (Notif.obj.Tcb.state.spec s t) fun i1 ei1 => ?_ + have hns2 : i1 ≠ sched.TS_BLOCKED_ON_RECV := by rw [ei1]; exact hns + have hvm2 : i1 = sched.TS_RUNNING_VM := by rw [ei1]; exact h8 + rw [if_neg hns2, if_neg Bool.false_ne_true, if_pos hvm2] + refine WP.spec_bind (WP.spec_and ((hB J).sigCompR hth hkt) (spec_self ((hB J).sigCompR hth hkt))) fun c ⟨hc, ec⟩ => ?_ + cases c with + | true => + rw [if_pos rfl] + refine WP.spec_bind (hRep J hth ⟨y, hkt, hrs⟩ badge) ?_ + rintro s1 ⟨σr, Ac1, Lst1, hr, J1⟩ + obtain ⟨hkh, hrun, hdt⟩ := B.repKh hth hkt hr + have hnt : n.bv ≠ t.bv := by intro e; rw [e, hkt] at hk; cases hk + have hkn : σr.kheap n.bv = some (.notification x) := by + rw [hkh n.bv hnt (fun vs e => by rw [hk] at e; cases e)]; exact hk + refine WP.spec_mono (hTl Ac1 Lst1 s1 a σr n J1 hth hn hrun ⟨x, hkn, hidle, hb⟩) ?_ + rintro r ⟨σ', Ac', Lst', h1, h2, J'⟩ + refine Or.inl ⟨σ', Ac', Lst', ?_, h2.trans hdt, J'⟩ + rw [v3_ssVF_comp _ _ _ hk hidle hb hkt hc hrs hr] + exact h1 + | false => + rw [if_neg Bool.false_ne_true] + have hnc := v3_ssVF_nc (v3OnCore st s) (v3RemCur st s) badge.bv hk hidle hb hkt hc (fun h => Bool.false_ne_true h.2) + refine WP.spec_bind (Notif.notification.set_active.spec s n badge) fun s1 s1_post => ?_ + subst s1_post + rw [if_neg Bool.false_ne_true] + obtain ⟨hR', hW⟩ := set_active_relI B.ip hR hn hk hidle badge + have hst' : L.mem (L.wr (L.wr s (P1.epTSA n) + ((L.mem s (P1.epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) (nBadgeA n) badge) + (tStA t) = L.mem s (tStA t) := by + have h1 := nwr_wr (L := L) B.ip.n (K := 8#u64) hn (by simp) (by simp) s + ((L.mem s (P1.epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64)) + rw [← epTSA_eq] at h1 + have h2 := nwr_wr (L := L) B.ip.n (K := 16#u64) hn (by simp) (by simp) + (L.wr s (P1.epTSA n) ((L.mem s (P1.epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) badge + rw [← nBadgeA_eq] at h2 + exact ((h1.trans h2).tcb _ hbT).1.st + have hrb' : Fl4Spec.receiveBlocked y.tcbState = false := by rw [hrs]; rfl + have hSR : SingleRelI (st := st) Z.N Z.T Z.E Z.R Z.B s σ (Fl4Spec.sendSignal n.bv badge.bv σ) + (L.wr (L.wr s (P1.epTSA n) ((L.mem s (P1.epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_ACTIVE &&& 3#u64))) + (nBadgeA n) badge) := + ⟨_, sendSignal_activate hk hidle (Or.inr ⟨t.bv, y, hb, hkt, hrb'⟩) badge.bv, hR', + NtfnStep.ntfn hk rfl rfl, hW, NoBlock.ntfnUpd _ _ _⟩ + refine WP.spec_bind (Notif.obj.Tcb.state.spec _ t) fun vm_state hvs => ?_ + rw [if_pos (by rw [hvs, hst', h8])] + refine WP.spec_mono (v3_kick hAT hK _ a t hAR) ?_ + rintro r ⟨e1, hA1⟩ + rw [e1] + exact Or.inr ⟨hnc, hA1, hSR, ec⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +set_option maxHeartbeats 16000000 in +/-- **`possible_switch_to` at `JAtZRB` of a schedulable thread, possibly the current or idle one** (module +docstring). -/ +theorem JAtZRB.switchJc (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (hs : Fl4Spec.schedulable t.bv σ = true) : + sched.possible_switch_to st s t ⦃ s' => ∃ σ', Fl4Spec.possibleSwitchTo (Z := Unit) t.bv σ = ({((), σ')}, False) ∧ + ∃ Lst' : U64 → List U64, JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s' a σ' ⦄ := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hwd := (schedulable_eq hR hS ht).trans hs + obtain ⟨hrw, -, hmx, -⟩ := of_decide_eq_true hwd + have hrun : P1.Runnable (L.mem s (P1.stA t)) := by rw [stA_tStA]; exact hrw + refine WP.spec_mono (WP.spec_and (WP.spec_and (possible_switch_to_refines s σ hR hS J.inv.actReady t ht) + (possible_switch_to_sfoot hR hS J.inv.actReady ht)) + (P1.sched.possible_switch_to.blk (Es := P1.EH H) s t J.blk htH hrun (fun _ e => by + rw [e] at hmx; simp at hmx))) ?_ + rintro s' ⟨⟨⟨σ', he, hke, hfe, hS'⟩, hw⟩, hblk, hfoot⟩ + have mem : ((), σ') ∈ (Fl4Spec.possibleSwitchTo (Z := Unit) t.bv σ).1 := by rw [he]; exact Set.mem_singleton _ + have hR' : NtfnStateRel Z.N Z.T (L.mem s') σ' := hR.frame hS.placed hfe (KView.of_kheap hke) + have hwK : P1.WritesIn st s s' (KFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.kfoot_of_sfoot hw + have hwT := WritesIn.tfoot_of_kfoot hwK + have hK : ScTKw (fun _ => False) σ σ' := nkt_possibleSwitchTo t.bv σ _ mem + have hX' : XTopAt (L := L) AR Z s' a σ' := xtop_sc_step hX J.ip J.nk hwT (hK.ofFalse (P := fun p => p = t.bv)) hS' hR' id + have hq := possibleSwitchTo_qchange J.inv hs he + have hc := possibleSwitchTo_calm J.inv hs he + have hI' : Inv σ' := J.inv.qchange hq + have hL : LK (fun _ => False) σ σ' := lkm_possibleSwitchTo t.bv σ _ mem + have hN' : NSC Z.N Z.C σ' := nsc_of_lk J.nsc hK hL + have hsok : SOK (true, true) σ σ' := pres_possibleSwitchTo (f := (true, true)) t.bv σ _ mem + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree ⟨⟨hS', hR', hN', hI'⟩, hwK, hsok⟩ + have hF : FK σ σ' := ⟨hc.2.2.1, hc.2.2.2.2.2.1, hK.isc⟩ + have hMK := mkn_possibleSwitchToC _ mem + have hAI : ActIdle σ' := J.act.possibleSwitchTo J.inv hs he + have hBK : BK σ σ' := bkm_of (nkt_possibleSwitchTo t.bv) (MaxK.mk_possibleSwitchTo' t.bv) σ _ mem + have hbsa : BlockedScActive σ' := J.bsa.bk hBK hsok + have hcur : CurInvR σ' := by + intro er + have h0 : σ.schedulerAction = .resumeCurThread := by + by_contra h + exact possibleSwitchTo_nr J.inv hs he h er + exact (hMK.mid3 (J.cur h0) J.off J.inv ⟨L.curW s, J.thT _ J.kern.curT, J.kern.cur.symm⟩).1.resolve_left + (by rw [er]; intro e; cases e) + have hown : CurOwnW Z.T Z.C σ' := J.own.sub hc (fun u hu => by rw [hq.2.1] at hu; exact hu) + have hoff : OffOK σ' := by + intro h0 y n hy ha + rw [hc.2.2.2.2.1] at h0 ⊢ + rw [hc.2.2.2.1] + rw [hc.1, hc.2.2.1] at hy + rw [hc.2.2.1, hc.2.2.2.2.2.2.2.2] at ha + exact J.off h0 y n hy ha + have hcr : CurRunM σ' := by + by_cases hr : σ'.schedulerAction = .resumeCurThread + · have h0 : σ.schedulerAction = .resumeCurThread := by + by_contra h + exact possibleSwitchTo_nr J.inv hs he h hr + right + rcases J.curRun.resolve_left (fun h => h h0) with h | h + · left + intro y hy + rw [hc.2.1, hc.1] at hy + exact h y hy + · right; rw [hc.2.1, hc.2.2.2.2.2.1]; exact h + · exact Or.inl hr + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_stepQ hX' hwT hK (fun _ hp => hp.elim) hL hF hAI.1 hY' ((hsok.ykeep rfl).dk q.bv) (fun h => h) (J.jcov.dead q hq) + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + exact ⟨σ', he, Lst', J.sc_frame hwT hK hX' hCR' hRF' hY' hM' (J.sc_buf hwT hK) hRP' (J.sc_apart hwT hK) + (J.sc_jpl hwT hK (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) (J.sc_jcov hwT hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) hKd subset_rfl) + (J.sc_kern hwT hK hF hX' hDbg fun _ h => h) (J.sc_utc hwT hK hDbg) hN' hI' hcur hown hoff hAI hbsa hcr hblk hKd (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 J.zD⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`unblock_check` at `JAtZRB` for a thread in no release queue** (module docstring; P3/XTopJointUnblock's +`JAtZ.unblockJ` with `t ∉ releaseQueue` for `Inactive`, the post extended). (mmuAD gen 42b67) A corollary of +P3/XTopJointSendRecv's `JAtZRB.unblockRJ` (its twin, restated there over `JAtZRB` with this post's facts). -/ +theorem JAtZRB.unblockJR (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hnq : t.bv ∉ σ.releaseQueue) + {p : U64} (hp : p ∈ Z.C) (hxsc : x.tcbSchedContext = some p.bv) (hpc : p.bv ≠ σ.curSc) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap p.bv = some (.schedContext y n)) + (hya : Fl4Spec.scActive y = true) : + sporadic.unblock_check st s p ⦃ s' => ∃ σ', Fl4Spec.refillUnblockCheck (Z := Unit) p.bv σ = ({((), σ')}, False) ∧ + (∀ q, q ≠ p.bv → σ'.kheap q = σ.kheap q) ∧ σ'.domainTime = σ.domainTime ∧ + ScTKw (fun _ => False) σ σ' ∧ P1.WritesIn st s s' (TFoot st Z.T Z.N Z.C Z.c s σ) ∧ + ∃ Lst' : U64 → List U64, JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s' a σ' ⦄ := + WP.spec_mono (J.unblockRJ ht hk hnq hp hxsc hpc hky hya) + fun _ ⟨σ', he, hkp, _, _, _, _, hdt, hK, hw, Lst', B⟩ => ⟨σ', he, hkp, hdt, hK, hw, Lst', B⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} +variable {V X : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **FL4's sporadic check refines `sigVmSporS` at `JAtZV3`** (module docstring). (mmuAD gen 42b71, ruling (c2) item 2) +Over any VM-epoch record `P` with a `JAtZRB` base (`hB0`) kept by an SC-code step (`hTK`); `JAtZV3.sigSporJ` is it at +`JAtZV3`. -/ +theorem sigSporGR {P : Set U64 → (U64 → List U64) → S → A → Fl4Spec.State Unit → Prop} + (hB0 : ∀ {Ac' : Set U64} {Lst' : U64 → List U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit}, + P Ac' Lst' s' a' σ' → JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a' σ') + (hTK : ∀ {Ac1 : Set U64} {Lst1 : U64 → List U64} {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} {Ac2 : Set U64} + {Lst2 : U64 → List U64} {s2 : S} {a2 : A} {σ2 : Fl4Spec.State Unit}, + P Ac1 Lst1 s1 a1 σ1 → JAtZRB (L := L) arI AR Z H Cs Ac2 Lst2 D s2 a2 σ2 → + P1.WritesIn st s1 s2 (TFoot st Z.T Z.N Z.C Z.c s1 σ1) → ScTKw (fun _ => False) σ1 σ2 → P Ac2 Lst2 s2 a2 σ2) + (J : P Ac Lst s a σ) {t n : U64} (ht : t ∈ Z.T) + (hn : n ∈ Z.N) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) {y : Fl4Spec.Notification} + (hkn : σ.kheap n.bv = some (.notification y)) : + sigVmSporF st s a t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Lst' : U64 → List U64), + sigVmSporS n.bv t.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + P Ac Lst' r.1 r.2 σ' ⦄ := by + have B := hB0 J + have hS := B.xtop.fin.1.1.1 + have hR : NtfnStateRel Z.N Z.T (L.mem s) σ := B.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨xt, hkt, -, -, hsct, -⟩ := hs.tcb t ht + rw [hk] at hkt; cases hkt + obtain ⟨y', hkn', hyr⟩ := hR.ntfn n hn + rw [hkn] at hkn'; cases hkn' + have hysc := hyr.sc + unfold sigVmSporF + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s t) fun i2 hi2 => ?_ + subst hi2 + refine WP.spec_bind (Sched.obj.Sc.is_null.spec' _) fun b3 hb3 => ?_ + by_cases h0 : L.mem s (scA t) = 0#u64 + · obtain rfl : b3 = true := by rw [hb3]; exact decide_eq_true h0 + rw [if_pos rfl] + exact (WP.spec_ok _).2 ⟨σ, Lst, stl_spor_none hk (by rw [hsct, h0, Notif.optRef_zero]), rfl, J⟩ + obtain rfl : b3 = false := by rw [hb3]; exact decide_eq_false h0 + rw [if_neg Bool.false_ne_true] + have hscC : L.mem s (scA t) ∈ Z.C := hs.scMem t ht h0 + obtain ⟨yc, k, hky, hy⟩ := hs.scs _ hscC + have hxsc : x.tcbSchedContext = some (L.mem s (scA t)).bv := by rw [hsct, Notif.optRef_ne h0] + have hSp := stl_spor_some (n := n.bv) hk hxsc hky hkn + refine WP.spec_bind (Sched.sporadic.sc_active.spec' s _) fun b4 hb4 => ?_ + by_cases ha : Fl4Spec.scActive yc = true + swap + · obtain rfl : b4 = false := by + rw [hb4]; refine decide_eq_false fun h => ha (hy.active_of_rmax ?_) + have := P1.gt_iff_val.1 h; simp at this; omega + rw [if_neg Bool.false_ne_true] + exact (WP.spec_ok _).2 ⟨σ, Lst, by rw [hSp, if_neg ha], rfl, J⟩ + obtain rfl : b4 = true := by + rw [hb4]; refine decide_eq_true (P1.gt_iff_val.2 ?_) + have := hy.rmax_ne ha; simp; omega + rw [if_pos rfl] + rw [if_pos ha] at hSp + refine WP.spec_bind (Notif.obj.Ntfn.sc.spec s n) fun i3 hi3 => ?_ + subst hi3 + by_cases heq : L.mem s (scA t) = L.mem s (nScA n) + swap + · rw [if_neg heq] + refine (WP.spec_ok _).2 ⟨σ, Lst, ?_, rfl, J⟩ + rw [hSp, if_neg] + rintro ⟨h1, -⟩ + rw [hxsc, hysc] at h1 + exact heq (P1.eq_of_bv (Notif.optRef_some h1.symm).2).symm + rw [if_pos heq] + have hcond1 : x.tcbSchedContext = y.ntfnSc := by rw [hxsc, hysc, ← heq, Notif.optRef_ne h0] + refine WP.spec_bind (Sched.sporadic.sc_sporadic.spec' s _) fun b5 hb5 => ?_ + have hsp : yc.scSporadic = true ↔ L.mem s (scW (L.mem s (scA t)) 64#u64) ≠ 0#u64 := by + rw [hy.sporadic]; simp + by_cases hsw : L.mem s (scW (L.mem s (scA t)) 64#u64) ≠ 0#u64 + swap + · obtain rfl : b5 = false := by rw [hb5]; exact decide_eq_false fun h => hsw h.2 + rw [if_neg Bool.false_ne_true] + exact (WP.spec_ok _).2 ⟨σ, Lst, by rw [hSp, if_neg fun h => hsw (hsp.1 h.2.1)], rfl, J⟩ + obtain rfl : b5 = true := by rw [hb5]; exact decide_eq_true ⟨h0, hsw⟩ + rw [if_pos rfl] + refine WP.spec_bind (Sched.sched.cur_sc.spec' s) fun s4 hs4 => ?_ + rw [hc.cur] at hs4 + subst hs4 + rw [Sched.sc_ne_eq, bind_ok] + by_cases hcur : L.mem s (scA t) = Sched.nodeW st s Z.c layout.node.CUR_SC + · rw [if_neg fun h => absurd hcur (of_decide_eq_true h)] + exact (WP.spec_ok _).2 ⟨σ, Lst, by rw [hSp, if_neg fun h => h.2.2.1 (by rw [hs.curSc, hcur])], rfl, J⟩ + rw [if_pos (decide_eq_true hcur)] + have hpc : (L.mem s (scA t)).bv ≠ σ.curSc := by rw [hs.curSc]; exact Sched.bv_ne hcur + refine WP.spec_bind (Fl4Proofs.P1.sched.in_release_queue.spec s t) fun b7 hb7 => ?_ + have hrq : t.bv ∈ σ.releaseQueue ↔ (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 := by + rw [hs.rel, Sched.mem_map_bv]; exact (hc.released t ht).symm + by_cases hin : (L.mem s (inqA t) &&& sched.Q_RELEASE) ≠ 0#u64 + · obtain rfl : b7 = true := by rw [hb7]; exact decide_eq_true hin + rw [if_pos rfl] + refine (WP.spec_ok _).2 ⟨σ, Lst, ?_, rfl, J⟩ + rw [hSp, if_neg] + intro h + have h4 := h.2.2.2 + simp [Fl4Spec.inReleaseQueue, hrq.2 hin] at h4 + obtain rfl : b7 = false := by rw [hb7]; exact decide_eq_false hin + rw [if_neg Bool.false_ne_true] + have hnq : t.bv ∉ σ.releaseQueue := fun h => hin (hrq.1 h) + refine WP.spec_bind (B.unblockJR ht hk hnq hscC hxsc hpc hky ha) ?_ + rintro s5 ⟨σ5, he5, -, hdt5, hK5, hW5, Lst5, B5⟩ + refine (WP.spec_ok _).2 ⟨σ5, Lst5, ?_, hdt5, hTK J B5 hW5 hK5⟩ + rw [hSp, if_pos ⟨hcond1, hsp.2 hsw, hpc, by simp [Fl4Spec.inReleaseQueue, hnq]⟩, he5] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} +variable {V X : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **FL4's tail after `maybe_donate_sched_context` refines `sigVmRestS` at `JAtZV3`** (module docstring). (mmuAD gen +42b71) Over `P` as `sigSporGR`, the switch by `JAtZRB.switchJc` with its `TFoot` writes and `ScTKw` step; `JAtZV3.sigRestJ` +is it at `JAtZV3`. -/ +theorem sigRestGR {P : Set U64 → (U64 → List U64) → S → A → Fl4Spec.State Unit → Prop} + (hB0 : ∀ {Ac' : Set U64} {Lst' : U64 → List U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit}, + P Ac' Lst' s' a' σ' → JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a' σ') + (hTK : ∀ {Ac1 : Set U64} {Lst1 : U64 → List U64} {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} {Ac2 : Set U64} + {Lst2 : U64 → List U64} {s2 : S} {a2 : A} {σ2 : Fl4Spec.State Unit}, + P Ac1 Lst1 s1 a1 σ1 → JAtZRB (L := L) arI AR Z H Cs Ac2 Lst2 D s2 a2 σ2 → + P1.WritesIn st s1 s2 (TFoot st Z.T Z.N Z.C Z.c s1 σ1) → ScTKw (fun _ => False) σ1 σ2 → P Ac2 Lst2 s2 a2 σ2) + (J : P Ac Lst s a σ) {t n : U64} (ht : t ∈ Z.T) + (hn : n ∈ Z.N) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) {y : Fl4Spec.Notification} + (hkn : σ.kheap n.bv = some (.notification y)) : + sigVmRestF st s a t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Lst' : U64 → List U64), + sigVmRestS n.bv t.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + P Ac Lst' r.1 r.2 σ' ⦄ := by + have B := hB0 J + have hS := B.xtop.fin.1.1.1 + have hR : NtfnStateRel Z.N Z.T (L.mem s) σ := B.xtop.fin.1.1.2.2.1 + unfold sigVmRestF + refine WP.spec_bind (Sched.sched.is_schedulable.spec' s t) fun b2 hb2 => ?_ + have hsch : b2 = Fl4Spec.schedulable t.bv σ := hb2.trans (Sched.schedulable_eq hR hS ht) + cases hsv : Fl4Spec.schedulable t.bv σ with + | false => + rw [hsch, hsv, if_neg Bool.false_ne_true, bind_ok] + refine WP.spec_mono (sigSporGR hB0 hTK J ht hn hk hkn) ?_ + rintro r ⟨σ', Lst', h1, h2, J'⟩ + refine ⟨σ', Lst', ?_, h2, J'⟩ + unfold sigVmRestS + rw [nd_gets_bind, hsv] + unfold Fl4Spec.NonDet.when + rw [if_neg Bool.false_ne_true] + exact (nd_bind_one (m := (Fl4Spec.NonDet.pure () : Fl4Spec.NonDet (Fl4Spec.State Unit) Unit)) rfl _).trans h1 + | true => + rw [hsch, hsv, if_pos rfl] + refine WP.spec_bind (WP.spec_and (B.switchJc ht hsv) (possible_switch_to_sfoot hR hS B.inv.actReady ht)) ?_ + rintro s3 ⟨⟨σ3, he3, Lst3, B3⟩, hw3⟩ + have J3 := hTK J B3 (WritesIn.tfoot_of_kfoot (WritesIn.kfoot_of_sfoot hw3)) + (nkt_possibleSwitchTo t.bv σ _ (by rw [he3]; exact Set.mem_singleton _)) + obtain ⟨σq, heq, rq, act, hq⟩ := Sched.pst_q hk B.inv.actReady + have e3 : σ3 = σq := by + have h := congrArg Prod.fst (he3.symm.trans heq) + simpa only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] using h + have hk3 : σ3.kheap t.bv = some (.tcb x) := by rw [e3, hq]; exact hk + have hkn3 : σ3.kheap n.bv = some (.notification y) := by rw [e3, hq]; exact hkn + have hdt3 : σ3.domainTime = σ.domainTime := by rw [e3, hq] + refine WP.spec_mono (sigSporGR hB0 hTK J3 ht hn hk3 hkn3) ?_ + rintro r ⟨σ', Lst', h1, h2, J'⟩ + refine ⟨σ', Lst', ?_, h2.trans hdt3, J'⟩ + unfold sigVmRestS + rw [nd_gets_bind, hsv] + unfold Fl4Spec.NonDet.when + rw [if_pos rfl, nd_bind_one he3] + exact h1 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **Gen 35's [J] `SigVmTailJ3` from the narrower `SigVmDonJ3`** (module docstring). (mmuAD gen 42b71, ruling (c2) +item 2) Over `P` as `sigSporGR`, the donation as `hDon` (`SigVmDonJ3`'s statement at `P`); `sigVmTailJ3_of` is it at +`JAtZV3`. -/ +theorem sigVmTailGR {P : Set U64 → (U64 → List U64) → S → A → Fl4Spec.State Unit → Prop} + (hB0 : ∀ {Ac' : Set U64} {Lst' : U64 → List U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit}, + P Ac' Lst' s' a' σ' → JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a' σ') + (hTK : ∀ {Ac1 : Set U64} {Lst1 : U64 → List U64} {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} {Ac2 : Set U64} + {Lst2 : U64 → List U64} {s2 : S} {a2 : A} {σ2 : Fl4Spec.State Unit}, + P Ac1 Lst1 s1 a1 σ1 → JAtZRB (L := L) arI AR Z H Cs Ac2 Lst2 D s2 a2 σ2 → + P1.WritesIn st s1 s2 (TFoot st Z.T Z.N Z.C Z.c s1 σ1) → ScTKw (fun _ => False) σ1 σ2 → P Ac2 Lst2 s2 a2 σ2) + (hDon : ∀ (Ac : Set U64) (Lst : U64 → List U64) (s1 : S) (a : A) (σr : Fl4Spec.State Unit) (t n : U64), + P Ac Lst s1 a σr → Z.th t = some .tcb → n ∈ Z.N → + (∃ x, σr.kheap t.bv = some (.tcb x) ∧ x.tcbState = .running ∧ x.tcbSchedContext = none) → + (∃ y p yc k, σr.kheap n.bv = some (.notification y) ∧ y.ntfnObj = .idleNtfn ∧ y.ntfnBoundTcb = some t.bv ∧ + y.ntfnSc = some p ∧ σr.kheap p = some (.schedContext yc k) ∧ yc.scTcb = none) → + notification.maybe_donate_sched_context st arI s1 a t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64), Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σr = ({((), σ')}, False) ∧ + σ'.domainTime = σr.domainTime ∧ P Ac' Lst' r.1 r.2 σ' ⦄) : + ∀ (Ac : Set U64) (Lst : U64 → List U64) (s1 : S) (a : A) (σr : Fl4Spec.State Unit) (t n : U64), + P Ac Lst s1 a σr → Z.th t = some .tcb → n ∈ Z.N → + (∃ x, σr.kheap t.bv = some (.tcb x) ∧ x.tcbState = .running) → + (∃ y, σr.kheap n.bv = some (.notification y) ∧ y.ntfnObj = .idleNtfn ∧ y.ntfnBoundTcb = some t.bv) → + sigVmTailF st arI s1 a t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64), + sigVmTailS n.bv t.bv σr = ({((), σ')}, False) ∧ σ'.domainTime = σr.domainTime ∧ P Ac' Lst' r.1 r.2 σ' ⦄ := by + intro Ac Lst s1 a σr t n J hth hn hrun hntf + obtain ⟨x, hk, hxr⟩ := hrun + obtain ⟨y, hkn, hidle, hb⟩ := hntf + have B := hB0 J + have ht : t ∈ Z.T := B.thT t hth + have hS := B.xtop.fin.1.1.1 + have hR : NtfnStateRel Z.N Z.T (L.mem s1) σr := B.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨xt, hkt, -, -, hsct, -⟩ := hs.tcb t ht + rw [hk] at hkt; cases hkt + obtain ⟨y', hkn', hyr⟩ := hR.ntfn n hn + rw [hkn] at hkn'; cases hkn' + have hysc := hyr.sc + have rest : ∀ (s2 : S) (a2 : A) (σ2 : Fl4Spec.State Unit) (Ac2 : Set U64) (Lst2 : U64 → List U64), + Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σr = ({((), σ2)}, False) → σ2.domainTime = σr.domainTime → + P Ac2 Lst2 s2 a2 σ2 → + sigVmRestF st s2 a2 t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64), + sigVmTailS n.bv t.bv σr = ({((), σ')}, False) ∧ σ'.domainTime = σr.domainTime ∧ + P Ac' Lst' r.1 r.2 σ' ⦄ := by + intro s2 a2 σ2 Ac2 Lst2 he2 hdt2 J2 + have B2 := hB0 J2 + obtain ⟨Q2, R2, hc2, hs2⟩ := B2.xtop.fin.1.1.1.lists + obtain ⟨x2, hk2, -⟩ := hs2.tcb t ht + have hR2 : NtfnStateRel Z.N Z.T (L.mem s2) σ2 := B2.xtop.fin.1.1.2.2.1 + obtain ⟨y2, hkn2, -⟩ := hR2.ntfn n hn + refine WP.spec_mono (sigRestGR hB0 hTK J2 ht hn hk2 hkn2) ?_ + rintro r ⟨σ', Lst', h1, h2, J'⟩ + exact ⟨σ', Ac2, Lst', by rw [sigVmTailS_eq, nd_bind_one he2]; exact h1, h2.trans hdt2, J'⟩ + rw [sigVmTailF_eq] + by_cases hdon : L.mem s1 (scA t) = 0#u64 ∧ L.mem s1 (nScA n) ≠ 0#u64 ∧ + L.mem s1 (scTcbA (L.mem s1 (nScA n))) = 0#u64 + · obtain ⟨h0, h1, h2⟩ := hdon + have hysc' : y.ntfnSc = some (L.mem s1 (nScA n)).bv := by rw [hysc, Notif.optRef_ne h1] + obtain ⟨c, hcC, hcb⟩ := B.nsc n hn y _ hkn hysc' + have hc' : c = L.mem s1 (nScA n) := P1.eq_of_bv hcb + subst hc' + obtain ⟨yc, k, hky, hy⟩ := hs.scs _ hcC + have hyt : yc.scTcb = none := by rw [hy.tcb, Sched.scW_tcb, h2, Notif.optRef_zero] + refine WP.spec_bind (hDon Ac Lst s1 a σr t n J hth hn ⟨x, hk, hxr, by rw [hsct, h0, Notif.optRef_zero]⟩ + ⟨y, _, yc, k, hkn, hidle, hb, hysc', hky, hyt⟩) ?_ + rintro ⟨s2, a2⟩ ⟨σ2, Ac2, Lst2, he2, hdt2, J2⟩ + exact rest s2 a2 σ2 Ac2 Lst2 he2 hdt2 J2 + have hmd : notification.maybe_donate_sched_context st arI s1 a t n = ok (s1, a) := by + unfold notification.maybe_donate_sched_context + rw [spec_eq_ok (Fl4Proofs.P1.obj.Tcb.sc.spec s1 t), bind_ok] + by_cases h0 : L.mem s1 (scA t) = 0#u64 + · rw [if_pos h0, spec_eq_ok (Notif.obj.Ntfn.sc.spec s1 n), bind_ok, spec_eq_ok (Sched.obj.Sc.is_null.spec' _), + bind_ok] + by_cases h1 : L.mem s1 (nScA n) = 0#u64 + · rw [if_pos (decide_eq_true h1)] + · rw [if_neg (show ¬ (decide (L.mem s1 (nScA n) = 0#u64) = true) from fun h => h1 (of_decide_eq_true h)), + spec_eq_ok (Fl4Proofs.P1.obj.Sc.tcb.spec s1 _), bind_ok, if_neg (fun e => hdon ⟨h0, h1, e⟩)] + · rw [if_neg h0] + have hsp : Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σr = ({((), σr)}, False) := by + by_cases h0 : L.mem s1 (scA t) = 0#u64 + · have hx0 : x.tcbSchedContext = none := by rw [hsct, h0, Notif.optRef_zero] + by_cases h1 : L.mem s1 (nScA n) = 0#u64 + · exact stl_mds_nn hk hx0 hkn (by rw [hysc, h1, Notif.optRef_zero]) + · have h2 : L.mem s1 (scTcbA (L.mem s1 (nScA n))) ≠ 0#u64 := fun e => hdon ⟨h0, h1, e⟩ + have hysc' : y.ntfnSc = some (L.mem s1 (nScA n)).bv := by rw [hysc, Notif.optRef_ne h1] + obtain ⟨c, hcC, hcb⟩ := B.nsc n hn y _ hkn hysc' + have hc' : c = L.mem s1 (nScA n) := P1.eq_of_bv hcb + subst hc' + obtain ⟨yc, k, hky, hy⟩ := hs.scs _ hcC + exact stl_mds_bound hk hx0 hkn hysc' hky (by rw [hy.tcb, Sched.scW_tcb, Notif.optRef_ne h2]; simp) + · exact stl_mds_some hk (by rw [hsct, Notif.optRef_ne h0]; simp) + rw [hmd, bind_ok] + exact rest s1 a σr Ac Lst hsp rfl J + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +set_option maxHeartbeats 4000000 in +/-- **`maybe_donate_sched_context` to a running thread without an SC keeps `JAtZRB`** (module docstring). -/ +theorem JAtZRB.donJ (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n : U64} (ht : t ∈ Z.T) (hn : n ∈ Z.N) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hxr : x.tcbState = .running) + (hx0 : x.tcbSchedContext = none) : + notification.maybe_donate_sched_context st arI s a t n ⦃ r => r.2 = a ∧ ∃ σ' : Fl4Spec.State Unit, + Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + ScTKw (fun _ => False) σ σ' ∧ P1.WritesIn st s r.1 (TFoot st Z.T Z.N Z.C Z.c s σ) ∧ + JAtZRB (L := L) arI AR Z H Cs Ac Lst D r.1 a σ' ⦄ := by + have hS := J.xtop.fin.1.1.1 + have hR : NtfnStateRel Z.N Z.T (L.mem s) σ := J.xtop.fin.1.1.2.2.1 + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨xt, hkt, -, -, hsct, -⟩ := hs.tcb t ht + rw [hk] at hkt; cases hkt + have h0 : L.mem s (P1.scA t) = 0#u64 := optRef_none (hsct.symm.trans hx0) + obtain ⟨xn, hkn, -⟩ := hR.ntfn n hn + -- the spec: `t` in no queue + have hrq : ∀ d p, t.bv ∉ σ.readyQueues d p := fun d p hm => by + obtain ⟨x', scp, -, -, h1, h2, -⟩ := J.inv.rq d p t.bv hm + rw [hk] at h1; cases h1; rw [hx0] at h2; cases h2 + have hnr : t.bv ∉ σ.releaseQueue := J.inv.notRel_of_noSc hk hx0 + have hnqT : NotQueued σ t.bv := ⟨hrq, hnr⟩ + -- P1's side conditions + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hnH : n ∈ P1.Heap.ntfns H := J.zH.2.2.2 n hn + obtain ⟨⟨Q1, R1, h1q⟩, hB1, hX1, hS1, hSg1⟩ := J.blk + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hv : (L.mem s (P1.stA t)).val = tsCode x.tcbState := by + rw [P1.stA_eq, ← tStA_eq]; exact hTR.st + obtain ⟨hnb, hnrp⟩ := P1.run_word (w := L.mem s (P1.stA t)) + (by rw [hv]; exact runnable_code (by rw [hxr]; rfl)) + have hnS : ¬ P1.Sch st s t := by + rintro (e | e | e | e | ⟨e1, e2⟩ | e) + · exact hB1.q t htH e h0 + · exact hB1.r t htH e h0 + · exact hB1.act (by rw [e]; exact htH) (by rw [e]; exact h0) + · exact hB1.idle (by rw [e]; exact h0) + · exact hB1.resume e1 (by rw [e2]; exact h0) + · rcases e with e | e + · exact hnb e + · exact hnrp e + have p1 := Fl4Proofs.P1.notification.maybe_donate_sched_context.tinvF (Es := P1.EH H) hAT s a t n Q1 R1 h1q + hB1 hX1 hS1 hSg1 htH (Classical.byContradiction fun e => hB1.q t htH e h0) + (Classical.byContradiction fun e => hB1.r t htH e h0) + (fun e => hB1.act (by rw [e]; exact htH) (by rw [e]; exact h0)) (fun e => hB1.idle (by rw [e]; exact h0)) + (fun hn0 _ _ => by + obtain ⟨c1, -⟩ := hX1.2.2.nsc n hnH hn0 + exact ⟨c1, hX1.2.2.core _ c1, fun h => absurd h hnS⟩) + -- FL4's refinement + have hDon := maybe_donate_refinesA s a σ hR hS J.inv.relOK (hmb a) t n ht hn + (fun h => NSC.mem J.nsc hR hn h) (fun _ y hy => by rw [hk] at hy; cases hy; exact hrq _ _) + refine WP.spec_mono (WP.spec_and hDon p1) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ1, he, hf, hkv, hS', hw, -⟩, hp1⟩ + try simp only [WP.uncurry'_pair] at hp1 + obtain ⟨hblk, -, -, hfoot⟩ := hp1 + simp only at ha + subst ha + have hK0 : ScTKw (fun _ => False) σ σ1 := NKT.of_eq (nkt_maybeDonateSc t.bv n.bv) he + have hK : ScTKw (fun p => p = σ.curThread) σ σ1 := hK0.ofFalse + have hN1 : NtfnStateRel Z.N Z.T (L.mem s') σ1 := hR.frame hS.placed hf hkv + have hIR := tk_lift J.ip hI (hw.mono fun _ h => Or.inl h) hK hS' hN1 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw (fun b hb => tfoot_oobj hS (Or.inl hb)) + have hFS : FinStep (st := st) Z.T Z.N Z.C Z.c s σ s' σ1 := ⟨hw.mono fun b hb => Or.inl hb, SzKeepC.of hK.szKeep⟩ + have hw' : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hFS.1.mono fun b h => Or.inl h + -- the spec's frame + have hnipc : ∀ x', σ.kheap t.bv = some (.tcb x') → Fl4Spec.ipcQueuedThreadState x'.tcbState = false := + fun x' hx' => by rw [hk] at hx'; cases hx'; rw [hxr]; rfl + obtain ⟨Fd, had, -, -, Bd⟩ := donate_dfr (r := n.bv) J.inv hk hkn hnqT J.off J.bsa hnipc he + obtain ⟨Id, -⟩ := Inv.donate J.inv hk hkn (by rw [hxr]; rfl) hnqT he + have hAI : ActIdle σ1 := ⟨fun u hu => Fd.sched (J.act.1 u (by rw [← had]; exact hu)), Fd.idleOK J.act.2⟩ + have hcur1 : CurInvR σ1 := fun e => Fd.curInv (J.cur (by rw [← had]; exact e)) + have hrun1 : CurRunM σ1 := J.curRun.of_act Fd.curRunW fun e => by rw [had]; exact e + have hRR : ∀ q ∈ Z.R, ∀ xr, σ.kheap q.bv = some (.reply xr) → xr.replyTcb = some t.bv → + Detached Z.C Lst q ∧ L.mem s (rNextA q) = 0#u64 ∧ L.mem s (rPrevA q) = 0#u64 := by + intro q _ xr hq hxt + exfalso + obtain ⟨x', hk', hs'⟩ := J.sv.replyTcb q.bv xr t.bv hq hxt + rw [hk] at hk'; cases hk' + rcases hs' with e | ⟨ep, dd, e⟩ <;> rw [hxr] at e <;> cases e + have htD := J.not_dead_of_st ht hk (by rw [hxr]; simp [tsCode]) + exact ⟨rfl, σ1, he, Fd.sk.dt, hK0, WritesIn.tfoot_of_kfoot hw, + J.wake_postD hIR hF hw' hblk (hfoot (J.donCore hn)) Fd Id Bd hAI hcur1 hrun1 hRR htD ⟨t, ht, rfl⟩⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +/-- (mmuAD gen 42b71, ruling (c2) item 2) **The donation over any VM-epoch record `P`** with a `JAtZRB` base (`hB0`) +kept by an SC-code step (`hTK`): `SigVmDonJ3`'s statement at `P`, by `JAtZRB.donJ`. -/ +theorem sigVmDonGR {P : Set U64 → (U64 → List U64) → S → A → Fl4Spec.State Unit → Prop} + (hB0 : ∀ {Ac' : Set U64} {Lst' : U64 → List U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit}, + P Ac' Lst' s' a' σ' → JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a' σ') + (hTK : ∀ {Ac1 : Set U64} {Lst1 : U64 → List U64} {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} {Ac2 : Set U64} + {Lst2 : U64 → List U64} {s2 : S} {a2 : A} {σ2 : Fl4Spec.State Unit}, + P Ac1 Lst1 s1 a1 σ1 → JAtZRB (L := L) arI AR Z H Cs Ac2 Lst2 D s2 a2 σ2 → + P1.WritesIn st s1 s2 (Sched.TFoot st Z.T Z.N Z.C Z.c s1 σ1) → Sched.ScTKw (fun _ => False) σ1 σ2 → + P Ac2 Lst2 s2 a2 σ2) + (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) : + ∀ (Ac : Set U64) (Lst : U64 → List U64) (s1 : S) (a : A) (σr : Fl4Spec.State Unit) (t n : U64), + P Ac Lst s1 a σr → Z.th t = some .tcb → n ∈ Z.N → + (∃ x, σr.kheap t.bv = some (.tcb x) ∧ x.tcbState = .running ∧ x.tcbSchedContext = none) → + (∃ y p yc k, σr.kheap n.bv = some (.notification y) ∧ y.ntfnObj = .idleNtfn ∧ y.ntfnBoundTcb = some t.bv ∧ + y.ntfnSc = some p ∧ σr.kheap p = some (.schedContext yc k) ∧ yc.scTcb = none) → + notification.maybe_donate_sched_context st arI s1 a t n ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64), Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σr = ({((), σ')}, False) ∧ + σ'.domainTime = σr.domainTime ∧ P Ac' Lst' r.1 r.2 σ' ⦄ := by + intro Ac Lst s1 a σr t n J hth hn hrun _ + obtain ⟨x, hk, hxr, hx0⟩ := hrun + refine WP.spec_mono ((hB0 J).donJ hAT hmb ((hB0 J).thT t hth) hn hk hxr hx0) ?_ + rintro r ⟨ha, σ', he, hdt, hK, hW, B'⟩ + rw [ha] + exact ⟨σ', Ac, Lst, he, hdt, hTK J B' hW hK⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- (gen 42b73) **`VmEptStateR.signalVm` with the `runningVM` frame**: no thread becomes `runningVM` (`RvmSub`), and +the bound thread stays `runningVM` only when FL4's `signal_completes` refused it (the report and the tail by +`JAtZRB.repKh` and `natK_tkw`; the activation is a native step). -/ +theorem VmEptStateR.signalVmK (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) + {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) + {t : U64} (hb : x.ntfnBoundTcb = some t.bv) {y : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb y)) + (hrs : y.tcbState = .runningVM) (badge : U64) : + notification.send_signal st arI s a n badge ⦃ r => + ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64), + Fl4Spec.sendSignalVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) n.bv badge.bv σ = ({((), σ')}, False) ∧ + σ'.domainTime = σ.domainTime ∧ VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D r.1 r.2 σ' ∧ + RvmSub σ σ' ∧ ((∃ x', σ'.kheap t.bv = some (.tcb x') ∧ x'.tcbState = .runningVM) → + vcpu.signal_completes st s t = ok false) ⦄ := by + have B := J.1.1 + have hNt : ∀ m ∈ P1.Heap.ntfns H, H m = some .ntfn := fun _ h => h + have hnH : n ∈ P1.Heap.ntfns H := B.zH.2.2.2 n hn + have hP1 := P1.notification.send_signal.tinv hAT s a n badge B.blk hnH (hNt n hnH) B.bufAtJ.1 B.bufAtJ.2 + have hTK : ∀ {Ac1 : Set U64} {Lst1 : U64 → List U64} {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit} + {Ac2 : Set U64} {Lst2 : U64 → List U64} {s2 : S} {a2 : A} {σ2 : Fl4Spec.State Unit}, + (VmEptStateR (L := L) arI V X XL AR Z H Cs Ac1 Lst1 D s1 a1 σ1 ∧ RvmSub σ σ1 ∧ + ∀ x', σ1.kheap t.bv = some (.tcb x') → x'.tcbState ≠ .runningVM) → + JAtZRB (L := L) arI AR Z H Cs Ac2 Lst2 D s2 a2 σ2 → + P1.WritesIn st s1 s2 (Sched.TFoot st Z.T Z.N Z.C Z.c s1 σ1) → Sched.ScTKw (fun _ => False) σ1 σ2 → + VmEptStateR (L := L) arI V X XL AR Z H Cs Ac2 Lst2 D s2 a2 σ2 ∧ RvmSub σ σ2 ∧ + ∀ x', σ2.kheap t.bv = some (.tcb x') → x'.tcbState ≠ .runningVM := + fun ⟨J1, R1, N1⟩ B2 hw hK' => + have R12 : RvmSub _ _ := ZvK.rvm (natK_tkw hK' fun _ h => h.elim).1 + ⟨J1.ofTKR B2 hw hK', R1.trans R12, fun x' hx' hr => by + obtain ⟨x1, hx1, hr1⟩ := R12 _ x' hx' hr + exact N1 x1 hx1 hr1⟩ + have hW := signalVmWGR (t := t) + (P := fun Ac' Lst' s' a' σ' => VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a' σ' ∧ RvmSub σ σ') + (Pm := fun Ac' Lst' s' a' σ' => VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a' σ' ∧ RvmSub σ σ' ∧ + ∀ x', σ'.kheap t.bv = some (.tcb x') → x'.tcbState ≠ .runningVM) + (Pf := fun Ac' Lst' s' a' σ' => VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a' σ' ∧ RvmSub σ σ' ∧ + ∀ x', σ'.kheap t.bv = some (.tcb x') → x'.tcbState ≠ .runningVM) + (fun J' => J'.1.toB) + (@fun _ _ _ _ σ1 J' ht hr b => WP.spec_mono (J'.1.reportJ3 hAT hVA ht hr b) fun _ ⟨σr, Ac', Lst', h1, J'', _, _⟩ => by + obtain ⟨x1, hk1, -⟩ := hr + obtain ⟨hkh, ⟨yr, hkr, hyr⟩, -⟩ := J'.1.1.1.repKh ht hk1 h1 + refine ⟨σr, Ac', Lst', h1, J'', fun o x' ho hx' => ?_, fun x' hx' => ?_⟩ + · obtain ⟨u, hu, rfl⟩ := J''.1.1.obj.tcb o x' ho + have hot : u.bv ≠ t.bv := fun e => by + rw [e, hkr] at ho; cases ho; rw [hyr] at hx'; cases hx' + obtain ⟨x0, hk0, -⟩ := J'.1.1.1.specTcb (J'.1.1.1.thTcb hu) + have e : σr.kheap u.bv = σ1.kheap u.bv := hkh u.bv hot (fun vs e => by rw [hk0] at e; cases e) + exact J'.2 u.bv x' (by rw [← e]; exact ho) hx' + · rw [hkr] at hx'; cases hx'; rw [hyr]; exact fun h => by cases h) + (sigVmTailGR + (P := fun Ac' Lst' s' a' σ' => VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a' σ' ∧ RvmSub σ σ' ∧ + ∀ x', σ'.kheap t.bv = some (.tcb x') → x'.tcbState ≠ .runningVM) + (fun J' => J'.1.1.1) hTK (sigVmDonGR + (P := fun Ac' Lst' s' a' σ' => VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D s' a' σ' ∧ RvmSub σ σ' ∧ + ∀ x', σ'.kheap t.bv = some (.tcb x') → x'.tcbState ≠ .runningVM) + (fun J' => J'.1.1.1) hTK hAT hmb) · · · · · t) + hAT hK ⟨J, RvmSub.refl σ⟩ hn hk hidle hb hkt hrs badge + refine WP.spec_mono (WP.spec_and hW hP1) ?_ + rintro ⟨s', a'⟩ ⟨h1, hbk⟩ + try simp only [WP.uncurry'_pair] at hbk + rcases h1 with ⟨σ', Ac', Lst', h1, h2, J', R', N'⟩ | ⟨hnc, hA1, hSR, hcf⟩ + · exact ⟨σ', Ac', Lst', h1, h2, J', R', fun ⟨x', hx', hr'⟩ => absurd hr' (N' x' hx')⟩ + · have hspec := sendSignal_activate hk hidle (Or.inr ⟨t.bv, y, hb, hkt, by rw [hrs]; rfl⟩) badge.bv + obtain ⟨σ1, h1, -, B1⟩ := B.signal_badge_post (y := Fl4Spec.ntfnObjUpdate (fun _ => .activeNtfn badge.bv) x) hn hk + (by rw [hidle]; intro l e; cases e) (fun l e => by simp [Fl4Spec.ntfnObjUpdate] at e) rfl rfl hspec hSR hbk + have e : σ1 = updK σ n.bv (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .activeNtfn badge.bv) x)) := by + have h3 := (Prod.mk.inj (h1.symm.trans hspec)).1 + exact congrArg Prod.snd (Set.singleton_injective h3) + subst e + have F := B.badgeN hk (hspec ▸ hSR) + exact ⟨_, Ac, Lst, hnc.trans hspec, rfl, (J.ofNatR (V' := Z.vs) B1 F).archV hA1, ZvK.rvm F.fk.1, fun _ => hcf⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- (gen 42b73) **`VmEptStateR.signalVF` with the `runningVM` frame** (`signalVmK`; the other arms are native +steps, `ZvK`): no thread becomes `runningVM`, and the thread bound to an idle notification stays `runningVM` only when +FL4's `signal_completes` refused it. -/ +theorem VmEptStateR.signalVFK (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) (badge : U64) : + notification.send_signal st arI s a n badge ⦃ r => + ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignalVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) n.bv badge.bv σ = ({((), σ')}, False) ∧ + σ'.domainTime = σ.domainTime ∧ VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ + RvmSub σ σ' ∧ ∀ (x : Fl4Spec.Notification) (t : U64), σ.kheap n.bv = some (.notification x) → + x.ntfnObj = .idleNtfn → x.ntfnBoundTcb = some t.bv → + (∃ x', σ'.kheap t.bv = some (.tcb x') ∧ x'.tcbState = .runningVM) → + vcpu.signal_completes st s t = ok false ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨x, hk, hx⟩ := B.xtop.ipc.toN.ntfn n hn + by_cases hvm : x.ntfnObj = .idleNtfn ∧ ∃ t y, x.ntfnBoundTcb = some t ∧ σ.kheap t = some (.tcb y) ∧ + y.tcbState = .runningVM + · obtain ⟨hidle, t, y, hb, hkt, hrs⟩ := hvm + obtain ⟨-, hbt⟩ := Notif.optRef_some (hx.bound ▸ hb) + refine WP.spec_mono (J.signalVmK hAT hVA hK hmb hn hk hidle (t := L.mem s (nBoundA n)) (by rw [hbt]; exact hb) + (by rw [hbt]; exact hkt) hrs badge) fun _ ⟨σ', Ac', Lst', h1, h2, J', R', hc⟩ => + ⟨σ', Ac', Lst', D, h1, h2, J', R', fun x1 t1 hk1 _ hb1 hr1 => ?_⟩ + rw [hk] at hk1; cases hk1 + have e : L.mem s (nBoundA n) = t1 := P1.eq_of_bv (hbt.trans (Option.some.inj (hb.symm.trans hb1))) + subst e + exact hc hr1 + · have heq : Fl4Spec.sendSignalVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) n.bv badge.bv σ = + Fl4Spec.sendSignal n.bv badge.bv σ := v3_ssVF_nvm _ _ _ hk fun ho t hb => by + obtain ⟨hb0, hbt⟩ := Notif.optRef_some (hx.bound ▸ hb) + obtain ⟨tcb, hkt, -⟩ := hI.1.tcb _ (hx.bound_mem hb0) + rw [hbt] at hkt + exact ⟨tcb, hkt, fun hr => hvm ⟨ho, t, tcb, hb, hkt, hr⟩⟩ + refine WP.spec_mono (B.signal_atNV hAT hmb n badge hn fun x' t tcb hk' ho hb hkt hr => ?_) + fun _ ⟨σ', Ac', Lst', D', h1, h2, B', F⟩ => ⟨σ', Ac', Lst', D', heq.trans h1, h2, J.ofNatR (V' := Z.vs) B' F, + ZvK.rvm F.fk.1, fun x1 t1 hk1 ho1 hb1 ⟨x', hx', hr'⟩ => by + obtain ⟨y0, hy0, hr0⟩ := ZvK.rvm F.fk.1 _ x' hx' hr' + rw [hk] at hk1; cases hk1 + exact absurd ⟨ho1, t1.bv, y0, hb1, hy0, hr0⟩ hvm⟩ + rw [hk] at hk' + cases hk' + exact hvm ⟨ho, t, tcb, hb, hkt, hr⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- (gen 42b73, ruling 17:14Z option (i)) **`send_signal` keeps `VmOrc`** (module docstring): FL4's side writes the +`AFFINITY` of the thread it wakes only (P2/FootN8's `send_signal.ffG` at `Kp := ∅`, `Ks := ∅` and `Il` every thread +but the woken one: `FFoot.affK`), no other core's `CUR_THREAD` (`FFoot.curK`), keeps the core; the spec side makes no +thread `runningVM` (`VmEptStateR.signalVFK`'s `RvmSub`); a woken thread blocked at the start is no `runningVM` thread, +and the bound thread `signal_completes` accepts is `runningVM` after only if FL4's read refused it. -/ +theorem VmEptStateR.signalVFk (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) (badge : U64) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) : + notification.send_signal st arI s a n badge ⦃ r => + ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignalVF (Z := Unit) on rc n.bv badge.bv σ = ({((), σ')}, False) ∧ + σ'.domainTime = σ.domainTime ∧ VmEptStateR (L := L) arI V X XL AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ + VmOrc st on rc r.1 σ' ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨x, hk, hx⟩ := B.xtop.ipc.toN.ntfn n hn + have heq : Fl4Spec.sendSignalVF (Z := Unit) on rc n.bv badge.bv σ = + Fl4Spec.sendSignalVF (Z := Unit) (v3OnCore st s) (v3RemCur st s) n.bv badge.bv σ := + sendSignalVF_congr hk _ (fun t hb => by + obtain ⟨hb0, hbt⟩ := Notif.optRef_some (hx.bound ▸ hb) + obtain ⟨tcb, hkt, -⟩ := hI.1.tcb _ (hx.bound_mem hb0) + rw [hbt] at hkt + exact ⟨tcb, hkt⟩) hO + have hnH : n ∈ P1.Heap.ntfns H := B.zH.2.2.2 n hn + have hB0 := B.blk + obtain ⟨⟨Q, R, h0⟩, -, hX, -, -⟩ := hB0 + have hT := h0.tcbs + -- the threads `send_signal` may wake + let W0 : U64 → Prop := fun y => + (L.epState s n = obj.NTFN_IDLE ∧ L.mem s (P1.ntfnBtA n) = y ∧ + (L.mem s (P1.stA y) = sched.TS_BLOCKED_ON_RECV ∨ vcpu.signal_completes st s y = ok true)) ∨ + (L.epState s n = 1#u64 ∧ (L.epQ s n).head = y) + have hF := Fl4Proofs.P2.Stall.notification.send_signal.ffG (Kp := ∅) (Il := {y | ¬ W0 y}) (Ks := ∅) hAT s a n + badge B.blk hnH hnH B.bufAtJ.1 B.bufAtJ.2 (fun _ h => ((Set.mem_empty_iff_false _).1 h).elim) + (fun _ h => ((Set.mem_empty_iff_false _).1 h).elim) (fun _ h => ((Set.mem_empty_iff_false _).1 h).elim) + (fun i hi hb hst => ⟨(Set.mem_empty_iff_false _).1, fun h => h (Or.inl ⟨hi, hb, Or.inl hst⟩)⟩) + (fun i hi hb _ => WP.spec_mono (spec_self (P1.vcpu.signal_completes.blk s i)) fun v e hv => + ⟨(Set.mem_empty_iff_false _).1, fun h => h (Or.inl ⟨hi, hb, Or.inr (hv ▸ e)⟩)⟩) + (fun hw => ⟨(Set.mem_empty_iff_false _).1, fun h => h (Or.inr ⟨hw, rfl⟩)⟩) + refine WP.spec_mono (WP.spec_and (J.signalVFK hAT hVA hK hmb hn badge) hF) ?_ + rintro ⟨s', a'⟩ ⟨⟨σ', Ac', Lst', D', h1, h2, J', R', BT⟩, F⟩ + refine ⟨σ', Ac', Lst', D', heq.trans h1, h2, J', hO.keep R' F.2.1 (fun t y' hk' hr' => ?_) (fun t y' hk' hr' hne => ?_)⟩ + · obtain ⟨u, hu, rfl⟩ := J'.1.1.obj.tcb t y' hk' + have huT : u ∈ P1.Heap.tcbs H := B.zH.1 u hu + obtain ⟨y0, hy0, hr0⟩ := R' _ y' hk' hr' + obtain ⟨x0, hk0, hw0⟩ := B.specTcb (B.thTcb hu) + rw [hy0] at hk0; cases hk0 + have h8 : (L.mem s (P1.stA u)).val = 8 := by rw [hw0, hr0]; rfl + show L.mem s' (P1.affA u) = L.mem s (P1.affA u) + by_cases hW : W0 u + · exfalso + rcases hW with ⟨hid, hbu, hrv | hcv⟩ | ⟨hw1, hhd⟩ + · rw [hrv] at h8; simp [sched.TS_BLOCKED_ON_RECV] at h8 + · have hidle : x.ntfnObj = .idleNtfn := by + have ho := hx.obj + rw [Notif.epState_eq] at hid + cases e : x.ntfnObj with + | idleNtfn => rfl + | activeNtfn b => + rw [e] at ho; exact absurd (hid.symm.trans ho.1) (by unfold obj.NTFN_IDLE; decide) + | waitingNtfn q => + rw [e] at ho; exact absurd (hid.symm.trans ho.1) (by unfold obj.NTFN_IDLE; decide) + have hu0 : u ≠ 0#u64 := fun e => (hT u huT).ne0 (by rw [e]; rfl) + have e2 : L.mem s (nBoundA n) = u := hbu + have hbnd : x.ntfnBoundTcb = some u.bv := by rw [hx.bound, e2]; simp [Notif.optRef, hu0] + have hf := BT x u hk hidle hbnd ⟨y', hk', hr'⟩ + rw [hcv] at hf; simp at hf + · obtain ⟨-, -, hds⟩ := P1.ntfn_head h0 hnH hw1 + rw [hhd] at hds; rw [hds] at h8; simp [sched.TS_BLOCKED_ON_NTFN] at h8 + · exact Fl4Proofs.P2.Stall.FFoot.affK h0 hX.1 F huT (Or.inr hW) + · obtain ⟨u, hu, rfl⟩ := J'.1.1.obj.tcb t y' hk' + exact Fl4Proofs.P2.Stall.FFoot.curK h0 hX.1 F (h0.aff u (B.zH.1 u hu)) hne + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- (gen 42b73) **`VmOrc` across timer-word writes** (`TmW`: the TCBs' timer fields, every core's timer heads, this +core's `REPROGRAM`) with the spec heap kept: no `AFFINITY` word and no other core's `CUR_THREAD` is a timer word. -/ +theorem VmOrc.tmwR (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {on rc : Fl4Spec.ObjRef → Bool} + (hO : VmOrc st on rc s σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (TmW (L := L) Z.T Cs Z.c s)) (hk : σ'.kheap = σ.kheap) : VmOrc st on rc s' σ' := by + obtain ⟨⟨Q, R, h0⟩, -⟩ := J.blk + refine hO.keep (fun t y' h hr => ⟨y', by rw [← hk]; exact h, hr⟩) hw.2.1 (fun t y' h hr => ?_) + (fun t y' h hr hne => ?_) + · rw [hk] at h + obtain ⟨u, hu, rfl⟩ := J.obj.tcb t y' h + have hta := h0.tcbs u (J.zH.1 u hu) + have hv : (P1.affA u).val = u.val + 3520 := by + rw [show P1.affA u = core.num.U64.wrapping_add u 3520#u64 from rfl, P1.field_val hta (by simp)]; simp + exact hw.mem_eq (P1.field_aligned hta (by simp) (by simp)) fun hb => J.tmw_ne_tcb hb hu hv (Or.inl (by omega)) rfl + · rw [hk] at h + obtain ⟨u, hu, rfl⟩ := J.obj.tcb t y' h + have hc : L.mem s (P1.affA u) ∈ Cs := h0.aff u (J.zH.1 u hu) + have hN : P1.NodePlaced st Z.T s (L.mem s (P1.affA u)) := by rw [J.tcbs_eq.1]; exact h0.nodes _ hc + have h0v : layout.node.CUR_THREAD.val = 0 := by simp [layout.node.CUR_THREAD] + show L.mem s' (L.nodeAt s' (L.mem s (P1.affA u)) layout.node.CUR_THREAD) = + L.mem s (L.nodeAt s (L.mem s (P1.affA u)) layout.node.CUR_THREAD) + rw [P1.nodeAt_of_ksb hw.1] + exact hw.mem_eq (P1.nodeAt_aligned hN (by omega) (by omega)) fun hb => + J.tmw_ne_node hb hc (by omega) (by omega) (by omega) (by omega) rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- (gen 42b73) The fire loop's record at `VmEptStateR` (fresh VSpace sets) with `VmOrc`. -/ +abbrev VmFirePR (V X : Set U64) (XL : U64 → Nat) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (on rc : Fl4Spec.ObjRef → Bool) + (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) : Prop := + ∃ (V1 : XVs) (Ac1 : Set U64) (Lst1 : U64 → List U64) (D1 : Set U64), + VmEptStateR (L := L) arI V X XL AR (Z.mv Z.G V1) H Cs Ac1 Lst1 D1 s1 a1 σ1 ∧ VmOrc st on rc s1 σ1 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- (gen 42b73, ruling 17:14Z item 2) **FL4's TCB timers at `VmEptStateR` against the extended spec's +`fl4TimerFireVF`** (ScFireJ's `fire_loopGR` at `P := VmFirePR`, `sig := sendSignalVF on rc`): the timer-word and +`REPROGRAM` writes are native steps (`JAtZRB.tmwN`, `JAtZRB.reprogramN`) keeping `VmOrc` (`VmOrc.tmwR`), each signal is +`VmEptStateR.signalVFk`. -/ +theorem VmEptStateR.fire_loopVF (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) {on rc : Fl4Spec.ObjRef → Bool} + (s : S) (a : A) (now i : U64) + (h : FireInvG (VmFirePR (st := st) (arI := arI) (AR := AR) V X XL Z H Cs on rc) (Fl4Spec.sendSignalVF (Z := Unit) on rc) σ s a) : + timer.fire_loop st arI s a now i ⦃ r => + FireInvG (VmFirePR (st := st) (arI := arI) (AR := AR) V X XL Z H Cs on rc) (Fl4Spec.sendSignalVF (Z := Unit) on rc) σ r.1 r.2 ⦄ := + fire_loopGR (Z := Z) (H := H) (Cs := Cs) (AR := AR) + (fun ⟨V1, Ac1, Lst1, D1, J1, _⟩ => ⟨V1, Ac1, Lst1, D1, J1.1.1⟩) + (@fun _ _ _ _ V1 Ac1 Lst1 D1 ⟨_, _, _, _, J0, O0⟩ B2 hw => + ⟨V1, Ac1, Lst1, D1, J0.ofNatR (V' := V1) B2 (J0.1.1.tmwN hw), VmOrc.tmwR J0.1.1 O0 hw rfl⟩) + (fun ⟨V1, Ac1, Lst1, D1, J1, O1⟩ => ⟨V1, Ac1, Lst1, D1, + J1.ofNatR (V' := V1) J1.1.1.reprogramJ J1.1.1.reprogramN, VmOrc.tmwR J1.1.1 O1 J1.1.1.reprogram_w rfl⟩) + (fun n b ⟨V1, _, _, _, J1, O1⟩ hn => WP.spec_mono (J1.signalVFk hAT hVA hK hmb hn b O1) + fun _ ⟨σ', Ac', Lst', D', h1, h2, J', O'⟩ => ⟨σ', h1, h2, V1, Ac', Lst', D', J', O'⟩) + s a now i h + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- (ruling 18:47Z item 3) **FL4's TCB timers at `VmEptStateR` against `fl4AwakenVF`** (module docstring): the +outcome is `fl4AwakenVF on rc`'s, `VmEptStateR` (fresh VSpace sets) and `VmOrc` after. -/ +theorem VmEptStateR.fireVF (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) {on rc : Fl4Spec.ObjRef → Bool} + {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hO : VmOrc st on rc s σ) : + timer.fire st arI s a ⦃ r => ∃ σ1 : Fl4Spec.State Unit, ((), σ1) ∈ (Fl4Spec.fl4AwakenVF (Z := Unit) on rc σ).1 ∧ + σ1.domainTime = σ.domainTime ∧ VmFirePR (st := st) (arI := arI) (AR := AR) V X XL Z H Cs on rc r.1 r.2 σ1 ⦄ := by + unfold timer.fire + refine WP.spec_bind (L.cur_cpu_spec s) fun c _ => ?_ + refine WP.spec_bind (P1.obj.node_get.spec s c _) fun now _ => ?_ + exact WP.spec_mono (VmEptStateR.fire_loopVF (V := V) (X := X) (XL := XL) (Z := Z) (H := H) (Cs := Cs) (AR := AR) + hAT hVA hK hmb s a now 0#u64 ⟨σ, Relation.ReflTransGen.refl, rfl, Z.vs, Ac, Lst, D, J, hO⟩) + fun _ ⟨σ1, h1, h2, hP⟩ => ⟨σ1, h1, h2, hP⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- **FL4's FPU-aware `schedule` at `VmEptStateR`**, after `fl4AwakenVF` (module docstring). +The timer outcome supplies the facts for `jat_scheduleFRGF` and its native footprint carries the VM relations. -/ +theorem VmEptStateR.scheduleVF (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) {on rc : Fl4Spec.ObjRef → Bool} + {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hO : VmOrc st on rc s σ) (hd : σ.domainTime ≠ 0 ∨ NdOK (L := L) s σ) (hnd : Fl4Spec.numDomains = 1) + (hsni : SniHyp st arI Z.N Z.T Z.C Z.c) (hsend : SendHyp st arI Z.N Z.T Z.C Z.c) (hT : TimeAgree arI) + (hRef : ArchRefines arI AR) (hAF : ArchSwF st arI Z.N Z.T Z.C Z.c) (hAN : ArchNdF st Z.N Z.T Z.C Z.c) + (hFO : FpuOwnOK σ) (hIV : IdleVt σ) : + sched.schedule st arI s a ⦃ r => ∃ σ1 σ' : Fl4Spec.State Unit, + ((), σ1) ∈ (Fl4Spec.fl4AwakenVF (Z := Unit) on rc σ).1 ∧ + Fl4Spec.schedule (Z := Unit) σ1 = ({((), σ')}, False) ∧ + (∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.1 r.2 σ') ∧ CurInv σ' ∧ CurRunW σ' ⦄ := by + have B := J.1.1 + have hNt : ∀ n ∈ P1.Heap.ntfns H, H n = some .ntfn := fun _ h => h + refine WP.spec_mono (B.jat_scheduleFRGF hd hnd hAT hK hRef + (F := fun σ σ1 => ((), σ1) ∈ (Fl4Spec.fl4AwakenVF (Z := Unit) on rc σ).1) + (Pf := fun r σ1 => VmFirePR (st := st) (arI := arI) (AR := AR) V X XL Z H Cs on rc r.1 r.2 σ1) + (WP.spec_mono (P1.spec_eq' (WP.spec_and (J.fireVF hAT hVA hK hmb hO) + (P1.timer.fire.kw hAT (P1.ibKeep_holds hAT) hNt s a B.blk B.bufAtJ))) fun r h => by + obtain ⟨hfr, ⟨σ1, h1, hdt, hP⟩, hkw⟩ := h + obtain ⟨V1, Ac1, Lst1, D1, J1, -⟩ := id hP + obtain ⟨s1, a1⟩ := r + simp only [WP.uncurry'_pair] at hkw + have hdt1 : σ1.domainTime ≠ 0 ∨ NdOK (L := L) s1 σ1 := + hd.imp (fun h => by rw [hdt]; exact h) + (fun h => B.ndOK_awakenVF h hkw (fun b hb o ho h8 => (B.bufAtJ.1 b hb).ks o ho h8) h1) + exact ⟨σ1, h1, hdt1, ⟨V1, Ac1, Lst1, D1, J1.1.1⟩, + B.fpuOwnOK_awakenVF h1 J1.1.1.xtop.fin.1.1.2.2.1 hFO, B.idleVt_awakenVF hAT hIV hfr h1 J1.1.1, hP⟩) + hAF hAN hsni hsend hT) ?_ + rintro r ⟨σ1, σ', h1, h2, ⟨V', Ac', Lst', D', B'⟩, hC, hR, rf, ⟨V1, Ac1, Lst1, D1, J1, -⟩, NF⟩ + exact ⟨σ1, σ', h1, h2, ⟨V', Ac', Lst', D', (J1.ofNatR B' NF.cast).toV (Or.inr hC)⟩, hC, hR⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +/-- **FL4's `finish` at `VmEptStateR` against `fl4KernelExitVF`** (module docstring). -/ +theorem VmEptStateR.finishVF (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hK : ARKeep arI AR) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) {on rc : Fl4Spec.ObjRef → Bool} + (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hO : VmOrc st on rc s σ) (hd : σ.domainTime ≠ 0 ∨ NdOK (L := L) s σ) (hnd : Fl4Spec.numDomains = 1) + (hsni : SniHyp st arI Z.N Z.T Z.C Z.c) (hsend : SendHyp st arI Z.N Z.T Z.C Z.c) (hT : TimeAgree arI) + (hRef : ArchRefines arI AR) (hAF : ArchSwF st arI Z.N Z.T Z.C Z.c) (hAN : ArchNdF st Z.N Z.T Z.C Z.c) + (hFO : FpuOwnOK σ) (hIV : IdleVt σ) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : + syscall.finish st arI s a ⦃ r => ∃ σ' : Fl4Spec.State Unit, + ((), σ') ∈ (Fl4Spec.fl4KernelExitVF (Z := Unit) on rc σ).1 ∧ + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.1 r.2 σ' ⦄ := by + unfold syscall.finish + refine WP.spec_bind (J.scheduleVF hAT hVA hK hmb hO hd hnd hsni hsend hT hRef hAF hAN hFO hIV) ?_ + rintro ⟨s1, a1⟩ ⟨σ1, σ2, h1, h2, ⟨V2, Ac2, Lst2, D2, J2⟩, -, hR⟩ + refine WP.spec_bind (J2.activateV hAT hmax (htu a1) hR) ?_ + rintro ⟨s3, a3⟩ ⟨σ3, h3, V3, Ac3, Lst3, J3⟩ + step with Sched.sched.cur_thread.spec' as ⟨ct, ect⟩ + have hct : sched.cur_thread st s3 = ok ct := by rw [ect]; exact cur_thread_eq s3 + refine WP.spec_mono (J3.checkCur hAT hVA hct) ?_ + rintro r ⟨σ4, Ac4, Lst4, h4, J4, -⟩ + refine ⟨σ4, ?_, V3, Ac4, Lst4, D2, ?_⟩ + swap + · (first | exact ⟨⟨(J4).1.1, (J4).1.2⟩, (J4).2⟩ | exact ⟨(J4).1.1, (J4).1.2, (J4).2⟩) + unfold Fl4Spec.fl4KernelExitVF + exact nd_mem_bind h1 (nd_mem_bind (by rw [h2]; exact Set.mem_singleton _) + (nd_mem_bind (by rw [h3]; exact Set.mem_singleton _) + (nd_mem_bind (Set.mem_singleton _) (by rw [h4]; exact Set.mem_singleton _)))) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmFrameBatchRestart.lean b/proofs/Fl4Proofs/P3/VmFrameBatchRestart.lean new file mode 100644 index 000000000..24292d0e7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmFrameBatchRestart.lean @@ -0,0 +1,219 @@ +import Fl4Proofs.P3.SpecFbRestart +import Fl4Proofs.P3.VirtFrameBatchConservative + +/-! The VM FrameBatch match before and after the spec's caller Restart. +Each entry replays its actual lookup and deferred-unmap outcomes. At a +reached frame cap, finalisation and slot emptying replay the same native +or guest operation. The schedulability test uses the batch's state frame. -/ + +namespace Fl4Proofs.P3.SInv +open Fl4Spec Classical + +noncomputable section +variable {ct : ObjRef} + +private theorem fbGetCap_eq {sl : CslotPtr} {s : State Unit} {c : Cap} + (hc : SInvU.capAt s sl = some c) : getCap (Z := Unit) sl s = ({(c, s)}, False) := by + obtain ⟨p, idx⟩ := sl + cases hk : s.kheap p with + | none => simp [SInvU.capAt, hk] at hc + | some obj => + cases obj + case cnode r cs => + simp only [SInvU.capAt, hk, SInvU.capView_cnode] at hc + split at hc + · exact getCap_cnode hk ‹wellFormedCnodeN r cs› hc + · cases hc + case tcb x => + simp only [SInvU.capAt, hk, SInvU.capView_tcb] at hc + show (getObject (Z := Unit) p >>= _) s = _ + rw [Notif.nd_bind_one (Notif.getObject_eq hk)] + show ((pure (tcbCnodeMap x) : SMonad Unit CnodeContents) >>= _) s = _ + rw [Notif.nd_pure_bind'] + change NonDet.assertOpt (tcbCnodeMap x idx) s = _ + rw [hc] + rfl + all_goals simp [SInvU.capAt, hk, SInvU.capView] at hc + + +private theorem cm_getEpt (t : ObjRef) : CM ct (getEpt (Z := Unit) t) := by + unfold getEpt + cm_auto + +private theorem cm_storeGuestEntry (t : ObjRef) (i : BitVec 9) (e : Word) : + CM ct (storeGuestEntry (Z := Unit) t i e) := by + unfold storeGuestEntry + refine cm_bind (cm_getEpt _) ?_ + rintro ⟨l, es⟩ + exact cm_setObject _ _ (fun _ h => by cases h) + +private theorem cme_findGuestRoot (asid : Asid) : CME ct (findGuestRoot (Z := Unit) asid) := by + unfold findGuestRoot + cm_auto + +private theorem cme_guestWalk (gpa : Word) (level : Nat) (start : ObjRef) (shifts : List Nat) : + CME ct (guestLookupSlot.go (Z := Unit) gpa level start shifts) := by + induction shifts generalizing start with + | nil => exact cme_returnOk _ + | cons shift shifts ih => + rw [guestLookupSlot.go] + by_cases hs : shift > level + · rw [ite_eq_left hs] + refine cme_bind (cme_liftE (cm_getEpt _)) ?_ + rintro ⟨_, es⟩ + exact cme_ite _ (cme_throw _) (ih _) + · rw [ite_eq_right hs] + exact cme_returnOk _ + +theorem cm_guestUnmapFb (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + CM ct (guestUnmap (Z := Unit) asid gpa ptr level table) := by + unfold guestUnmap + refine cm_bind (cme_findGuestRoot _) ?_ + intro r + cases r with + | error _ => exact cm_pure _ + | ok root => + refine cm_bind (cme_guestWalk gpa level root _) ?_ + intro t + cases t with + | error _ => exact cm_pure _ + | ok tbl => + refine cm_bind (cm_getEpt _) ?_ + rintro ⟨_, es⟩ + exact cm_unless _ (cm_storeGuestEntry _ _ _) + +private theorem cm_fbFinaliseVF (d : Bool) (p : ObjRef) (R : CapRights) (mt : VMMapType) + (sz : VMPageSize) (m : Option (Asid × VSpaceRef)) (fin : Bool) : + CM ct (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap d p R mt sz m)) fin) := by + cases mt <;> cases m <;> try exact cm_archFinaliseCap_frame _ _ _ _ _ _ _ + case vmEptMap.some mapping => + obtain ⟨asid, va⟩ := mapping + unfold finaliseCapVF + exact cm_bind (cm_when _ (cm_guestUnmapFb _ _ _ _ _)) fun _ => cm_pure _ + +theorem fbDeleteVF_fbsM {s t : State Unit} {sl : CslotPtr} {d : Bool} {p : ObjRef} {R : CapRights} + {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt s sl = some (.archObjectCap (.pageCap d p R mt sz m))) + (ts : ThreadState) (sa : SchedulerAction) {r : Except Unit Unit} + (h : (r, t) ∈ ((capDeleteVF (Z := Unit) sl).run s).1) : + (r, fbsM ct ts sa t) ∈ ((capDeleteVF (Z := Unit) sl).run (fbsM ct ts sa s)).1 := by + have hg := fbGetCap_eq hc + have hg' := fbGetCap_eq (by rw [capAt_fbsM]; exact hc : + SInvU.capAt (fbsM ct ts sa s) sl = some (.archObjectCap (.pageCap d p R mt sz m))) + obtain ⟨rfl, u, hf, he⟩ := (capDeleteVF_frame_mem hg).1 h + apply (capDeleteVF_frame_mem hg').2 + refine ⟨rfl, fbsM ct ts sa u, ?_, cm_emptySlot sl .nullCap ts sa u ((), t) he⟩ + let c := Cap.archObjectCap (.pageCap d p R mt sz m) + have heq : decide (isFinalCap' (Z := Unit) c (fbsM ct ts sa s)) = decide (isFinalCap' (Z := Unit) c s) := + decide_eq_decide.2 (fbs_isFinalCap' c ts sa s) + change ((.nullCap, .nullCap), fbsM ct ts sa u) ∈ + (finaliseCapVF c (decide (isFinalCap' c (fbsM ct ts sa s))) (fbsM ct ts sa s)).1 + rw [heq] + exact cm_fbFinaliseVF d p R mt sz m _ ts sa s ((.nullCap, .nullCap), u) hf + +private theorem cme_fbDeferDelete (b : Fl4FrameBatchInvocation) (sl : CslotPtr) (d : Bool) + (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) (m : Option (Asid × VSpaceRef)) : + CME ct (fbDeferDelete (Z := Unit) b sl d p R mt sz m) := by + rcases m with _ | ⟨asid, va⟩ + · exact cme_returnOk _ + · unfold fbDeferDelete + cm_auto + +private theorem cme_fbDeleteWithVF_at {sl : CslotPtr} {s : State Unit} {d : Bool} {p : ObjRef} {R : CapRights} + {mt : VMMapType} {sz : VMPageSize} {m : Option (Asid × VSpaceRef)} + (hc : SInvU.capAt s sl = some (.archObjectCap (.pageCap d p R mt sz m))) + (ts : ThreadState) (sa : SchedulerAction) (x : Except Data Unit × State Unit) + (hx : x ∈ ((fbDeleteWith capDeleteVF sl).run s).1) : + (x.1, fbsM ct ts sa x.2) ∈ ((fbDeleteWith capDeleteVF sl).run (fbsM ct ts sa s)).1 := by + unfold fbDeleteWith NonDetE.handleE at hx ⊢ + rw [ExceptT.run_mk] at hx ⊢ + obtain ⟨⟨r, t⟩, ht, hx⟩ := hx + refine ⟨(r, fbsM ct ts sa t), fbDeleteVF_fbsM hc ts sa ht, ?_⟩ + cases r <;> exact cm_pure _ ts sa t x hx + +set_option maxHeartbeats 4000000 in +theorem cme_fbEntryVF (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : + CME ct (fbEntryWith capDeleteVF b w) := by + by_cases hop : b.op = .delete + swap + · have e : fbEntryWith capDeleteVF b w = fbEntry (Z := Unit) b w := by + change fbEntryWith capDeleteVF b w = fbEntryWith capDelete b w + cases h : b.op + · simp only [fbEntryWith, h] + · simp only [fbEntryWith, h] + · exact absurd h hop + rw [e] + intro ts sa s y hy + exact fbEntry_supX b w _ _ (cme_fbEntryX b w ts sa s y (fbEntry_subX b w s y hy)) + simp only [fbEntryWith, hop] + refine cme_bind (cme_fbCode (cme_lookupTargetSlot _ _ _)) ?_ + intro sl ts sa s x hx + rw [ExceptT.run_bind] at hx ⊢ + obtain ⟨y, hy, hx⟩ := hx + refine ⟨(y.1, fbsM ct ts sa y.2), cme_liftE (cm_getCap sl) ts sa s y hy, ?_⟩ + obtain ⟨⟨c, u⟩, hc, rfl⟩ := fbx_mem_liftE hy + have hstate : u = s := (SInvU.getCap_capAt hc).1 + have hcap : SInvU.capAt u sl = some c := by rw [hstate]; exact (SInvU.getCap_capAt hc).2 + change x ∈ _ at hx + cases c <;> try exact cm_pure _ ts sa u x hx + case archObjectCap a => + cases a <;> try exact cm_pure _ ts sa u x hx + case pageCap d p R mt sz m => + change x ∈ ((fbDeferDelete b sl d p R mt sz m >>= fun cleared => + fbDeleteWith capDeleteVF sl >>= fun _ => NonDetE.returnOk cleared).run u).1 at hx + change _ ∈ ((fbDeferDelete b sl d p R mt sz m >>= fun cleared => + fbDeleteWith capDeleteVF sl >>= fun _ => NonDetE.returnOk cleared).run (fbsM ct ts sa u)).1 + rw [ExceptT.run_bind] at hx ⊢ + obtain ⟨⟨r, v⟩, hp, hx⟩ := hx + refine ⟨(r, fbsM ct ts sa v), cme_fbDeferDelete b sl d p R mt sz m ts sa u (r,v) hp, ?_⟩ + cases r with + | error e => exact cm_pure _ ts sa v x hx + | ok cleared => + obtain ⟨d', p', R', mt', sz', m', hc'⟩ := fbDeferDelete_frame hcap hp + change x ∈ ((fbDeleteWith capDeleteVF sl >>= fun _ => NonDetE.returnOk cleared).run v).1 at hx + change _ ∈ ((fbDeleteWith capDeleteVF sl >>= fun _ => NonDetE.returnOk cleared).run (fbsM ct ts sa v)).1 + rw [ExceptT.run_bind] at hx ⊢ + obtain ⟨⟨rr, t⟩, hd, hs⟩ := hx + refine ⟨(rr, fbsM ct ts sa t), cme_fbDeleteWithVF_at hc' ts sa (rr,t) hd, ?_⟩ + cases rr <;> exact cm_pure _ ts sa t x hs + +private theorem cm_fbRunVF (b : Fl4FrameBatchInvocation) : + ∀ (ws : List (Data × Data × Data)) (n : Nat) (d : Bool), CM ct (fbRunWith capDeleteVF b ws n d) + | [], _, _ => cm_pure _ + | w :: ws, n, d => by + unfold fbRunWith + refine cm_bind (cme_fbEntryVF b w) fun r => ?_ + cases r with + | ok d' => exact cm_fbRunVF b ws (n + 1) (d || d') + | error _ => exact cm_pure _ + +/-- Every VM batch outcome replays across the caller's Restart. -/ +theorem cm_performFrameBatchVF (b : Fl4FrameBatchInvocation) : + CM ct (performFrameBatchVF b) := by + unfold performFrameBatchVF performFrameBatchWith + refine cm_bind (cm_fbRunVF b _ _ _) ?_ + rintro ⟨done, err, d⟩ + exact cm_bind (cm_when _ (by unfold flushAll; exact cm_doMachineOp _)) fun _ => cm_pure _ + + +/-- The matched batch's state footprint supplies the scheduler fields that +determine the action taken by Restart. -/ +theorem performFrameBatchVF_restart (b : Fl4FrameBatchInvocation) {σ τ : State Unit} {ct : ObjRef} {x : Tcb} + (hk : σ.kheap ct = some (.tcb x)) {r : List Data} + (hx : (r, τ) ∈ (performFrameBatchVF b σ).1) (hKeep : FbKeep σ τ) : + ∃ y, τ.kheap ct = some (.tcb y) ∧ y.tcbSchedContext = x.tcbSchedContext ∧ + (r, Notif.stsAct (Notif.updK τ ct (.tcb { y with tcbState := .restart })) ct) ∈ + (performFrameBatchVF b (Notif.stsAct (Notif.updK σ ct (.tcb { x with tcbState := .restart })) ct)).1 := by + refine frameBatch_restart_of hk hx (cm_performFrameBatchVF b) ?_ (by + unfold fbsF + rw [hKeep.act, hKeep.ct, hKeep.rel]) + intro q + rcases hKeep.kh q with h | ⟨a, a', h, h', -, -⟩ | ⟨rad, cs, cs', h, h'⟩ | ⟨x, y, h, h', hxy⟩ + · rw [h] + · simp only [h, h', SVK.sv] + · simp only [h, h', SVK.sv] + · simp only [h, h', SVK.sv, hxy.2.2.2.2.2.1] + +end + +end Fl4Proofs.P3.SInv diff --git a/proofs/Fl4Proofs/P3/VmHandleTop.lean b/proofs/Fl4Proofs/P3/VmHandleTop.lean index f949067fa..00bf89081 100644 --- a/proofs/Fl4Proofs/P3/VmHandleTop.lean +++ b/proofs/Fl4Proofs/P3/VmHandleTop.lean @@ -93,9 +93,8 @@ theorem VDispPostVE.toG (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4 rw [if_pos h1]; exact ⟨V, X, XL, hP⟩ set_option maxHeartbeats 4000000 in -/-- **`handle_invocation` at `VmEptState` refines `fl4HandleInvocationVF onCore remoteCur true`** (module -docstring): `VEAny` after, some `VmEptPre` after a preemption. -/ -theorem VmEptState.handleInvocation (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) +/-- The decoder receives the successful lookup equation for the invoked cap and slot. -/ +theorem VmEptState.handleInvocationAtLookup (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) (hM : MachPlatC arI MR) (hPA : PPPlatA arI AR) (onCore remoteCur : Fl4Spec.ObjRef → Bool) {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {ar : A} {σ : Fl4Spec.State Unit} @@ -118,7 +117,9 @@ theorem VmEptState.handleInvocation (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI (hDisp : ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) → capAt (L.mem s) (cslotAddr sl) = ok c → - Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → r2 = .Ok e → er = .ok exs → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → + (Fl4Spec.lookupCapAndSlot (Z := Unit) ct.bv (Fl4Spec.toBl cptr.bv)).run σ = + ({(.ok (absCap c, sl), σ)}, False) → r2 = .Ok e → er = .ok exs → syscall.decode_invocation st arI s ar info.label (msgLen info buf) (cslotAddr sl) c blocking is_call can_donate first_phase buf e ⦃ res => let m := specDecV first_phase mi.miLabel args (Fl4Spec.toBl cptr.bv) sl c exs buffer @@ -135,7 +136,7 @@ theorem VmEptState.handleInvocation (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI obtain ⟨hcur, -⟩ := J.1.1.curEq hct have hX := J.1.1.xtop have htcb := (hX.curTcb hcur).1 - refine handle_invocation_refinesMXPG arI Fl4Spec.fl4DecodeInvocationV + refine handle_invocation_refinesMXPG_atLookup arI Fl4Spec.fl4DecodeInvocationV (Fl4Spec.fl4PerformInvocationVF (Z := Unit) onCore remoteCur) (VEAny (L := L) arI AR) (fun s a σ => ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s a σ) true s ar σ Z.th Z.G hX.rel.1 hX.rel.2.1 hX.rel.2.2.1 hX.rel.2.2.2.1 hX.rel.2.2.2.2.1 (J.any arI) ct hct hcur htcb @@ -154,13 +155,56 @@ theorem VmEptState.handleInvocation (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI (show ((), σ1) ∈ (Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ).1 by rw [hst]; exact Set.mem_singleton _) case dec => - intro sl c e exs hsl hc hget he hexs - obtain ⟨res, hr, hp⟩ := P1.spec_ex_fd (hDisp sl c e exs hsl hc hget he hexs) + intro sl c e exs hsl hc hget hlook he hexs + obtain ⟨res, hr, hp⟩ := P1.spec_ex_fd (hDisp sl c e exs hsl hc hget hlook he hexs) unfold XCapPG DecOKDMXPG DecOKRMXG rcases hp with h | h · exact Or.inl (by rw [hr, WP.spec_ok]; exact h.toG arI hAK hA hnd hM hPA) · exact Or.inr (by rw [hr, WP.spec_ok]; exact h) +/-- **`handle_invocation` at `VmEptState` refines `fl4HandleInvocationVF onCore remoteCur true`** (module +docstring): `VEAny` after, some `VmEptPre` after a preemption. -/ +theorem VmEptState.handleInvocation (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hM : MachPlatC arI MR) (hPA : PPPlatA arI AR) (onCore remoteCur : Fl4Spec.ObjRef → Bool) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) + -- the front end's reads (`HIRead`) + (info : msginfo.MessageInfo) (hinfo : msg.msg_info st s ct = ok info) + (mi : Fl4Spec.MessageInfo) (hmi : Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False)) + (buf : U64) (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) + (buffer : Option Fl4Spec.ObjRef) + (hbuffer : Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False)) + (r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault) + (hr2 : ipc.lookup_extra_caps st s ct buf info = ok r2) + (er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))) + (her : (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False)) + (args : List Fl4Spec.Data) (hmrs : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False)) + -- the decode and invocation of the looked-up cap, in the dispatcher post's form + (hDisp : ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) → + capAt (L.mem s) (cslotAddr sl) = ok c → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → r2 = .Ok e → er = .ok exs → + syscall.decode_invocation st arI s ar info.label (msgLen info buf) (cslotAddr sl) c blocking is_call + can_donate first_phase buf e ⦃ res => + let m := specDecV first_phase mi.miLabel args (Fl4Spec.toBl cptr.bv) sl c exs buffer + VDispPostVE (L := L) arI AR MR onCore remoteCur σ blocking is_call can_donate m s res ∨ + VDispReplyVE (L := L) arI (AR := AR) onCore remoteCur σ blocking is_call can_donate m ct s res ⦄) : + syscall.handle_invocation st arI s ar is_call blocking can_donate first_phase cptr ⦃ res => + ∃ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) onCore remoteCur true is_call blocking can_donate + first_phase (Fl4Spec.toBl cptr.bv)).run σ).1, + ((res.1 = .Ok () ∧ y.1 = .ok ()) ∨ (res.1 = .Err .Preempted ∧ y.1 = .error ())) ∧ + RelTopM (L := L) res.2.1 y.2 ∧ + (if res.1 = .Err .Preempted then + ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR res.2.1 res.2.2 y.2 + else VEAny (L := L) arI AR res.2.1 res.2.2 y.2) ⦄ := + J.handleInvocationAtLookup arI hAK hA hnd hM hPA onCore remoteCur hni hct hnq hvfh + is_call blocking can_donate first_phase cptr info hinfo mi hmi buf hbuf buffer hbuffer r2 hr2 er her args hmrs + (fun sl c e exs hsl hc hget _ he hexs => hDisp sl c e exs hsl hc hget he hexs) + end end diff --git a/proofs/Fl4Proofs/P3/VmHandleTopX.lean b/proofs/Fl4Proofs/P3/VmHandleTopX.lean new file mode 100644 index 000000000..f6b3f55b5 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmHandleTopX.lean @@ -0,0 +1,173 @@ +import Fl4Proofs.P3.VmHandleTop +import Fl4Proofs.P3.VmHandleX + +/-! +`…X` siblings of `VmHandleTop`'s dispatcher-chain statements: the same results concluding `VEAnyX` (`VEAny` plus the ghost +fact `VxE`), asking `[VxE Z]` of the entry state. The merged statements are unchanged. Generated by +`handoff/tools/gen_vxsib.py`. The siblings of the merged theorems below the family are in `VmVxLow`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 Fl4Proofs.P3.Endp + +/-- `…X` sibling of `VEAny.relTop` (`VEAnyX`; module docstring). -/ +theorem VEAnyX.relTop {s : S} {a : A} {σ : Fl4Spec.State Unit} (h : VEAnyX (L := L) arI AR s a σ) : + RelTopM (L := L) s σ := + match h with + | ⟨_, _, _, _, _, _, _, _, _, _, J⟩ => J.1.1.xtop.relTop + +/-- `…X` sibling of `VDispPostVE.toG` (`VEAnyX`; module docstring). -/ +theorem VDispPostVEX.toGX (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hM : MachPlatC arI MR) (hPA : PPPlatA arI AR) {onCore remoteCur : Fl4Spec.ObjRef → Bool} + {σ : Fl4Spec.State Unit} {blk call don : Bool} {m : Fl4Spec.SEMonad Unit Fl4Spec.Fl4InvocationV} {s : S} + {res : core.result.Result Unit error.Exc × S × A} + (h : VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blk call don m s res) : + TopPostDMXPG (L := L) (Fl4Spec.fl4PerformInvocationVF (Z := Unit) onCore remoteCur) (VEAnyX (L := L) arI AR) + (fun s a σ => ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s a σ) + σ blk call don m s res := by + rcases h with ⟨e, h1, h2, ⟨e', h3, h3'⟩, h4⟩ | ⟨inv, hd, σ1, hst, y, hy, ⟨h1, hy1, hX⟩ | ⟨h1, hy1, V, X, XL, hP⟩⟩ + · have hne : res.1 ≠ .Err .Preempted := by + rw [h1]; intro he; cases he; exact ErrRel.not_preempted h3' + refine Or.inl ⟨e, h1, h2, ⟨e', h3, h3'⟩, ?_⟩ + try dsimp only + rw [if_neg hne]; exact h4 + · have hne : res.1 ≠ .Err .Preempted := by rw [h1]; intro he; cases he + obtain ⟨th, G, hR⟩ := hX.relTop arI + refine Or.inr ⟨inv, hd, σ1, hst, y, hy, ⟨th, G, Or.inl ⟨h1, hy1⟩, hR⟩, ?_⟩ + try dsimp only + rw [if_neg hne]; exact hX + · obtain ⟨th, G, hR⟩ := hP.relTopM arI hAK hA hnd hM hPA + refine Or.inr ⟨inv, hd, σ1, hst, y, hy, ⟨th, G, Or.inr ⟨h1, hy1⟩, hR⟩, ?_⟩ + try dsimp only + rw [if_pos h1]; exact ⟨V, X, XL, hP⟩ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.handleInvocationAtLookup` (`VEAnyX`; module docstring). -/ +theorem VmEptState.handleInvocationAtLookupX (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hM : MachPlatC arI MR) (hPA : PPPlatA arI AR) (onCore remoteCur : Fl4Spec.ObjRef → Bool) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) + -- the front end's reads (`HIRead`) + (info : msginfo.MessageInfo) (hinfo : msg.msg_info st s ct = ok info) + (mi : Fl4Spec.MessageInfo) (hmi : Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False)) + (buf : U64) (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) + (buffer : Option Fl4Spec.ObjRef) + (hbuffer : Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False)) + (r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault) + (hr2 : ipc.lookup_extra_caps st s ct buf info = ok r2) + (er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))) + (her : (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False)) + (args : List Fl4Spec.Data) (hmrs : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False)) + -- the decode and invocation of the looked-up cap, in the dispatcher post's form + (hDisp : ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) → + capAt (L.mem s) (cslotAddr sl) = ok c → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → + (Fl4Spec.lookupCapAndSlot (Z := Unit) ct.bv (Fl4Spec.toBl cptr.bv)).run σ = + ({(.ok (absCap c, sl), σ)}, False) → r2 = .Ok e → er = .ok exs → + syscall.decode_invocation st arI s ar info.label (msgLen info buf) (cslotAddr sl) c blocking is_call + can_donate first_phase buf e ⦃ res => + let m := specDecV first_phase mi.miLabel args (Fl4Spec.toBl cptr.bv) sl c exs buffer + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blocking is_call can_donate m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) onCore remoteCur σ blocking is_call can_donate m ct s res ⦄) : + syscall.handle_invocation st arI s ar is_call blocking can_donate first_phase cptr ⦃ res => + ∃ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) onCore remoteCur true is_call blocking can_donate + first_phase (Fl4Spec.toBl cptr.bv)).run σ).1, + ((res.1 = .Ok () ∧ y.1 = .ok ()) ∨ (res.1 = .Err .Preempted ∧ y.1 = .error ())) ∧ + RelTopM (L := L) res.2.1 y.2 ∧ + (if res.1 = .Err .Preempted then + ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR res.2.1 res.2.2 y.2 + else VEAnyX (L := L) arI AR res.2.1 res.2.2 y.2) ⦄ := by + obtain ⟨hcur, -⟩ := J.1.1.curEq hct + have hX := J.1.1.xtop + have htcb := (hX.curTcb hcur).1 + refine handle_invocation_refinesMXPG_atLookup arI Fl4Spec.fl4DecodeInvocationV + (Fl4Spec.fl4PerformInvocationVF (Z := Unit) onCore remoteCur) (VEAnyX (L := L) arI AR) + (fun s a σ => ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s a σ) true s ar σ + Z.th Z.G hX.rel.1 hX.rel.2.1 hX.rel.2.2.1 hX.rel.2.2.2.1 hX.rel.2.2.2.2.1 (J.anyX arI) ct hct hcur htcb + is_call blocking can_donate first_phase cptr info hinfo mi hmi buf hbuf buffer hbuffer r2 hr2 er her ?rr + args hmrs ?fl ?repS ?repF ?pk ?dec + case rr => + exact (J.extraV arI hct hinfo hmi hbuf hbuffer hr2 her).imp id fun ⟨f, f', h1, h2, hf⟩ => + ⟨f, f', h1, h2, fun _ => J.handleFaultVX arI hA hni hct hnq hvfh hf⟩ + case fl => exact fun _ _ _ hl => J.handleFaultVX arI hA hni hct hnq hvfh (capFaultCorr_lookup cptr hl) + case repS => exact fun s1 a1 σ' hR hX1 hc => VEAnyX.repS arI ct is_call s1 a1 σ' hR hX1 hc + case repF => exact fun a1 e e' hX1 he => VEAnyX.repF arI hcur is_call a1 e e' hX1 he + case pk => + intro inv σ1 y hst hy + rw [perfCurKeepVF onCore remoteCur _ _ _ inv σ1 y hy, ← hcur] + exact (dk_setThreadState' _ _).ct + (show ((), σ1) ∈ (Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ).1 by + rw [hst]; exact Set.mem_singleton _) + case dec => + intro sl c e exs hsl hc hget hlook he hexs + obtain ⟨res, hr, hp⟩ := P1.spec_ex_fd (hDisp sl c e exs hsl hc hget hlook he hexs) + unfold XCapPG DecOKDMXPG DecOKRMXG + rcases hp with h | h + · exact Or.inl (by rw [hr, WP.spec_ok]; exact h.toGX arI hAK hA hnd hM hPA) + · exact Or.inr (by rw [hr, WP.spec_ok]; exact h) + +/-- `…X` sibling of `VmEptState.handleInvocation` (`VEAnyX`; module docstring). -/ +theorem VmEptState.handleInvocationX (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hM : MachPlatC arI MR) (hPA : PPPlatA arI AR) (onCore remoteCur : Fl4Spec.ObjRef → Bool) + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} {s : S} {ar : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) (hni : Entered σ) + {ct : U64} (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) + -- the front end's reads (`HIRead`) + (info : msginfo.MessageInfo) (hinfo : msg.msg_info st s ct = ok info) + (mi : Fl4Spec.MessageInfo) (hmi : Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False)) + (buf : U64) (hbuf : msg.lookup_ipc_buffer st s false ct = ok buf) + (buffer : Option Fl4Spec.ObjRef) + (hbuffer : Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False)) + (r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault) + (hr2 : ipc.lookup_extra_caps st s ct buf info = ok r2) + (er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))) + (her : (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False)) + (args : List Fl4Spec.Data) (hmrs : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False)) + -- the decode and invocation of the looked-up cap, in the dispatcher post's form + (hDisp : ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) → + capAt (L.mem s) (cslotAddr sl) = ok c → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → r2 = .Ok e → er = .ok exs → + syscall.decode_invocation st arI s ar info.label (msgLen info buf) (cslotAddr sl) c blocking is_call + can_donate first_phase buf e ⦃ res => + let m := specDecV first_phase mi.miLabel args (Fl4Spec.toBl cptr.bv) sl c exs buffer + VDispPostVEX (L := L) arI AR MR onCore remoteCur σ blocking is_call can_donate m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) onCore remoteCur σ blocking is_call can_donate m ct s res ⦄) : + syscall.handle_invocation st arI s ar is_call blocking can_donate first_phase cptr ⦃ res => + ∃ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) onCore remoteCur true is_call blocking can_donate + first_phase (Fl4Spec.toBl cptr.bv)).run σ).1, + ((res.1 = .Ok () ∧ y.1 = .ok ()) ∨ (res.1 = .Err .Preempted ∧ y.1 = .error ())) ∧ + RelTopM (L := L) res.2.1 y.2 ∧ + (if res.1 = .Err .Preempted then + ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR res.2.1 res.2.2 y.2 + else VEAnyX (L := L) arI AR res.2.1 res.2.2 y.2) ⦄ := + J.handleInvocationAtLookupX arI hAK hA hnd hM hPA onCore remoteCur hni hct hnq hvfh + is_call blocking can_donate first_phase cptr info hinfo mi hmi buf hbuf buffer hbuffer r2 hr2 er her args hmrs + (fun sl c e exs hsl hc hget _ he hexs => hDisp sl c e exs hsl hc hget he hexs) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmHandleX.lean b/proofs/Fl4Proofs/P3/VmHandleX.lean new file mode 100644 index 000000000..e63548df8 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmHandleX.lean @@ -0,0 +1,88 @@ +import Fl4Proofs.P3.VmHandle +import Fl4Proofs.P3.VmDispX + +/-! +`…X` siblings of `VmHandle`'s dispatcher-chain statements: the same results concluding `VEAnyX` (`VEAny` plus the ghost +fact `VxE`), asking `[VxE Z]` of the entry state. The merged statements are unchanged. Generated by +`handoff/tools/gen_vxsib.py`. The siblings of the merged theorems below the family are in `VmVxLow`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Endp + +/-- `…X` sibling of `VmEptState.handleFaultV` (`VEAnyX`; module docstring). -/ +theorem VmEptState.handleFaultVX (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hvfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : CapFaultCorr f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.1.1.faultHypJZ hAT hni hct hnq hvfh hf) fun _ ⟨z, hz, hR, _, _, _, _, B', F⟩ => + ⟨z, hz, hR, (J.ofNat B' F).anyX arI⟩ + +/-- `…X` sibling of `VmEptState.flTailV` (`VEAnyX`; module docstring). -/ +theorem VmEptState.flTailVX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {ct : U64} + (hcur : σ.curThread = ct.bv) (call : Bool) : + flTail (st := st) ct call s a ⦃ res => res.1 = .Ok () ∧ + ∃ z ∈ (specTail call ct.bv [] σ).1, RelTopM (L := L) res.2.1 z.2 ∧ VEAnyX (L := L) arI AR res.2.1 res.2.2 z.2 ⦄ := + WP.spec_mono (J.1.1.flTailJ hcur call) fun _ ⟨h1, z, hz, hR, _, _, B', F⟩ => + ⟨h1, z, hz, hR, (J.ofNat B' F).anyX arI⟩ + +/-- `…X` sibling of `VmEptState.replyFailV` (`VEAnyX`; module docstring). -/ +theorem VmEptState.replyFailVX [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {ct : U64} + (hcur : σ.curThread = ct.bv) (is_call : Bool) (e : error.SysError) (e' : Fl4Spec.SyscallError) + (he : ErrRel (.Sys e) e') : + ((if is_call then do + let s2 ← syscall.reply_error st s ct e + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a) + else ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s, a)) : + Result ((core.result.Result Unit error.Exc) × S × A)) ⦃ res => + res.1 = .Ok () ∧ ∃ z ∈ ((Fl4Spec.NonDet.when is_call <| + Fl4Spec.fl4ReplyFromKernelError true ct.bv (Fl4Spec.msgFromSyscallError e')) σ).1, + RelTopM (L := L) res.2.1 z.2 ∧ VEAnyX (L := L) arI AR res.2.1 res.2.2 z.2 ⦄ := + WP.spec_mono (J.1.1.replyFailJ hcur is_call e e' he) fun _ ⟨h1, z, hz, hR, _, B', F⟩ => + ⟨h1, z, hz, hR, (J.ofNat B' F).anyX arI⟩ + +/-- `…X` sibling of `VEAny.repS` (`VEAnyX`; module docstring). -/ +theorem VEAnyX.repS (ct : U64) (is_call : Bool) (s1 : S) (a1 : A) (σ' : Fl4Spec.State Unit) + (_ : RelTopM (L := L) s1 σ') (hX : VEAnyX (L := L) arI AR s1 a1 σ') (hcur : σ'.curThread = ct.bv) : + flTail (st := st) ct is_call s1 a1 ⦃ res => res.1 = .Ok () ∧ + ∃ z ∈ (specTail is_call ct.bv [] σ').1, RelTopM (L := L) res.2.1 z.2 ∧ + VEAnyX (L := L) arI AR res.2.1 res.2.2 z.2 ⦄ := + match hX with + | ⟨_, _, _, _, _, _, _, _, _, _, J⟩ => J.flTailVX arI hcur is_call + +/-- `…X` sibling of `VEAny.repF` (`VEAnyX`; module docstring). -/ +theorem VEAnyX.repF {ct : U64} (hcur : σ.curThread = ct.bv) (is_call : Bool) (a1 : A) (e : error.SysError) + (e' : Fl4Spec.SyscallError) (hX : VEAnyX (L := L) arI AR s a1 σ) (he : ErrRel (.Sys e) e') : + ((if is_call then do + let s2 ← syscall.reply_error st s ct e + ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s2, a1) + else ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s, a1)) : + Result ((core.result.Result Unit error.Exc) × S × A)) ⦃ res => + res.1 = .Ok () ∧ ∃ z ∈ ((Fl4Spec.NonDet.when is_call <| + Fl4Spec.fl4ReplyFromKernelError true ct.bv (Fl4Spec.msgFromSyscallError e')) σ).1, + RelTopM (L := L) res.2.1 z.2 ∧ VEAnyX (L := L) arI AR res.2.1 res.2.2 z.2 ⦄ := + match hX with + | ⟨_, _, _, _, _, _, _, _, _, _, J⟩ => J.replyFailVX arI hcur is_call e e' he + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmHiRvm.lean b/proofs/Fl4Proofs/P3/VmHiRvm.lean new file mode 100644 index 000000000..b3c664141 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmHiRvm.lean @@ -0,0 +1,122 @@ +import Fl4Proofs.P3.VmHiErr +import Fl4Proofs.P3.VmPerfRvm + +/-! +P3 (line mmuAD, Stage 3, 3.3 oracle): **no thread becomes `runningVM` in the extended invocation handler** +(`rvmSub_hiVF`): every outcome `y` (ok or error) of `fl4HandleInvocationVF onCore remoteCur rep …` from `σ` has +`RvmSub σ y.2`. Spec only; the sibling of P3/VmHiErr's `hiVF_err_nrvm`, over the `OI` family (`oi_fl4DecodeInvocationV` +for the extended decoder, `oi_fl4PerformInvocationVF`, then `hiG`'s shell). The oracle at a preempted outcome is +`JAtZB.vmOrcAcross` of this fact (P3/VmEntryS3Oracle). + +No named premise. New file. +-/ +open Classical + +namespace Fl4Proofs.P3.SInvAll +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInv Fl4Proofs.P3.Vs + +noncomputable section + +set_option maxHeartbeats 2000000 + +variable {N : OIx} + +theorem oi_getEptV (p : ObjRef) : OIK N (getEpt (Z := Unit) p) := ik_of_keepSt (keepSt_getEpt p) _ +macro_rules (kind := Fl4Proofs.P3.SInvAll.tacticOi_leaf) | `(tactic| oi_leaf) => `(tactic| with_reducible exact oi_getEptV _) + +theorem oi_findGuestRootV (asid : Asid) : OIKE N (findGuestRoot (Z := Unit) asid) := + ik_of_keepSt (keepSt_findGuestRoot asid) _ +macro_rules (kind := Fl4Proofs.P3.SInvAll.tacticOi_leaf) | `(tactic| oi_leaf) => `(tactic| with_reducible exact oi_findGuestRootV _) + +theorem oi_guestLookupSlotV (root : ObjRef) (gpa : Word) (level : Nat) : + OIKE N (guestLookupSlot (Z := Unit) root gpa level) := + ik_of_keepSt (keepSt_guestLookupSlot root gpa level) _ +macro_rules (kind := Fl4Proofs.P3.SInvAll.tacticOi_leaf) | `(tactic| oi_leaf) => `(tactic| with_reducible exact oi_guestLookupSlotV _ _ _) + +theorem oi_decodeVcpuInvocationV (label : Data) (args : List Data) (v : ObjRef) (ex : List (Cap × CslotPtr)) : + OIKE N (decodeVcpuInvocation (Z := Unit) label args v ex) := by + unfold decodeVcpuInvocation; orv_auto + +theorem oi_decodeEptInvocationV (label : Data) (args : List Data) (slot : CslotPtr) (c : ArchCap) + (ex : List (Cap × CslotPtr)) : OIKE N (decodeEptInvocation (Z := Unit) label args slot c ex) := by + unfold decodeEptInvocation eptCapRoot guestSlot; orv_auto + +theorem oi_decodePageMapEptV (args : List Data) (slot : CslotPtr) (c : ArchCap) (ex : List (Cap × CslotPtr)) : + OIKE N (decodePageMapEpt (Z := Unit) args slot c ex) := by + unfold decodePageMapEpt eptCapRoot guestSlot; orv_auto + +theorem oi_decodeEptAsidPoolAssignV (p : ObjRef) (b : Asid) (x : CslotPtr) : + OIKE N (decodeEptAsidPoolAssign (Z := Unit) p b x) := by + unfold decodeEptAsidPoolAssign; orv_auto + +theorem oi_decodeSetEptRootV (t : ObjRef) (ex : List (Cap × CslotPtr)) : + OIKE N (decodeSetEptRoot (Z := Unit) t ex) := by + unfold decodeSetEptRoot; orv_auto + +/-- **The extended decode keeps `OIS`** (as `ct_fl4DecodeInvocationV` keeps the current thread). -/ +theorem oi_fl4DecodeInvocationV (fp : Bool) (label : Data) (args : List Data) (ci : CapRef) (slot : CslotPtr) + (cap : Cap) (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) : + OIKE N (fl4DecodeInvocationV (Z := Unit) fp label args ci slot cap ex buf) := by + have hb : OIKE N (NonDetE.liftME Fl4InvocationV.base + (fl4DecodeInvocation (Z := Unit) fp label args ci slot cap ex buf)) := + ike_liftME _ (oi_fl4DecodeInvocation _ _ _ _ _ _ _ _) + unfold fl4DecodeInvocationV + split + · exact ike_liftME _ (oi_decodeVcpuInvocationV _ _ _ _) + · exact ike_liftME _ (oi_decodeEptInvocationV _ _ _ _ _) + · split + · exact ike_liftME _ (oi_decodePageMapEptV _ _ _ _) + · split + · exact ike_throw _ + · split + · exact ike_returnOk _ + · exact hb + · split + · exact ike_liftME _ (oi_decodeEptAsidPoolAssignV _ _ _) + · exact hb + · split + · exact ike_liftME _ (oi_decodeSetEptRootV _ _) + · exact hb + · split + · exact ike_throw _ + · exact hb + · exact hb + + +/-- `hiG` keeps `OIS` for a decoder and a dispatcher that do. -/ +theorem oi_hiG {ι : Type} + {dec : Bool → Data → List Data → CapRef → CslotPtr → Cap → List (Cap × CslotPtr) → Option ObjRef → + SEMonad Unit ι} + {perf : Bool → Bool → Bool → ι → PMonad Unit (List Data)} + (hdec : ∀ fp l args cptr sl cap ex buf, OIKE N (dec fp l args cptr sl cap ex buf)) + (hperf : ∀ blk call don inv, OIKE N (perf blk call don inv)) + (rep calling blocking canDonate firstPhase : Bool) (cptr : CapRef) : + OIKE N (hiG (Z := Unit) dec perf rep calling blocking canDonate firstPhase cptr) := by + unfold hiG; orv_auto + all_goals first | exact hdec _ _ _ _ _ _ _ _ | exact hperf _ _ _ _ + +/-- **The extended invocation handler keeps `OIS`** at any `N`. -/ +theorem oi_fl4HandleInvocationVF (onCore remoteCur : ObjRef → Bool) (rep calling blocking canDonate firstPhase : Bool) + (cptr : CapRef) : + OIKE N (fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep calling blocking canDonate firstPhase cptr) := by + rw [fl4HandleInvocationVF_hiG] + exact oi_hiG (fun _ _ _ _ _ _ _ _ => oi_fl4DecodeInvocationV _ _ _ _ _ _ _ _) + (fun _ _ _ _ => oi_fl4PerformInvocationVF onCore remoteCur _ _ _ _) rep calling blocking canDonate firstPhase cptr + +end + +end Fl4Proofs.P3.SInvAll + +namespace Fl4Proofs.P3 +open Fl4Spec + +/-- **No thread becomes `runningVM` in the extended invocation handler** (module docstring). -/ +theorem rvmSub_hiVF (onCore remoteCur : ObjRef → Bool) (rep calling blocking canDonate firstPhase : Bool) + (cptr : CapRef) (σ : State Unit) (y : Except Unit Unit × State Unit) + (hy : y ∈ ((fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep calling blocking canDonate firstPhase + cptr).run σ).1) : RvmSub σ y.2 := + SInvAll.oi_fl4HandleInvocationVF + (N := ⟨.runningVM, fun o => ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM, Or.inr rfl⟩) + onCore remoteCur rep calling blocking canDonate firstPhase cptr σ (fun _ x hp hs => ⟨x, hp, hs⟩) y hy + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmInvAfter.lean b/proofs/Fl4Proofs/P3/VmInvAfter.lean index c018800eb..c317a0815 100644 --- a/proofs/Fl4Proofs/P3/VmInvAfter.lean +++ b/proofs/Fl4Proofs/P3/VmInvAfter.lean @@ -71,8 +71,8 @@ abbrev VDispAt := VDispAtFor (L := L) arI AR MR (fun _ => True) abbrev VDispNativeAt := VDispAtFor (L := L) arI AR MR Vs.NativeCap set_option maxHeartbeats 4000000 in -/-- Assemble the invocation dispatcher from the native-cap premise and the VM/EPT producers. -/ -theorem VmEptState.dispatchAt [PlainStore st] +/-- Decode the VCPU, EPT, assigned I/O and EPT-mapped frame caps outside `NativeCap`. -/ +theorem VmEptState.dispatchAtNonNative [PlainStore st] {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) @@ -80,12 +80,9 @@ theorem VmEptState.dispatchAt [PlainStore st] (hK : ARKeep arI AR) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hVal : PortInValAt arI a σ) (on rc : Fl4Spec.ObjRef → Bool) {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) - (call blk don fp : Bool) (cptr : U64) - (hNative : VDispNativeAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr) : - VDispAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr := by - intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he _ - by_cases hN : Vs.NativeCap c - · exact hNative info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he hN + (call blk don fp : Bool) (cptr : U64) : + VDispAtFor (L := L) arI AR MR (fun c => ¬ Vs.NativeCap c) on rc Z s a σ ct call blk don fp cptr := by + intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he hN have B := J.1.1 have hX := B.xferH obtain ⟨hlbl, hExtra⟩ := B.frontJ hX.1 hX.2.1 hct hinfo hmi hbuf hbufS hr2 her @@ -152,6 +149,25 @@ theorem VmEptState.dispatchAt [PlainStore st] exact WP.spec_mono (hd.frameOther hct hFL hm hu hg ht hs p rt asid va (msgLen info buf) buf sz dev blk call don fp (pc.bv, idx) ex args (Fl4Spec.toBl cptr.bv) exs buffer) fun _ h => Or.inl h +set_option maxHeartbeats 4000000 in +/-- Assemble the invocation dispatcher from the native-cap premise and the VM/EPT producers. -/ +theorem VmEptState.dispatchAt [PlainStore st] + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hK : ARKeep arI AR) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hVal : PortInValAt arI a σ) + (on rc : Fl4Spec.ObjRef → Bool) {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (call blk don fp : Bool) (cptr : U64) + (hNative : VDispNativeAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr) : + VDispAt (L := L) arI AR MR on rc Z s a σ ct call blk don fp cptr := by + intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he _ + by_cases hN : Vs.NativeCap c + · exact hNative info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he hN + exact (J.dispatchAtNonNative arI AR MR hni hRef hVA hK hSy hCap hAT hAM hVal on rc hct hRun call blk don fp cptr) + info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he hN + /-- `VmEptState.handleInvocation`'s post. -/ def HIPostVE (on rc : Fl4Spec.ObjRef → Bool) (σ : Fl4Spec.State Unit) (is_call blocking can_donate first_phase : Bool) (cptr : U64) (res : core.result.Result Unit error.Exc × S × A) : Prop := diff --git a/proofs/Fl4Proofs/P3/VmInvAfterX.lean b/proofs/Fl4Proofs/P3/VmInvAfterX.lean new file mode 100644 index 000000000..a4caeae84 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmInvAfterX.lean @@ -0,0 +1,191 @@ +import Fl4Proofs.P3.VmInvAfter +import Fl4Proofs.P3.VmHandleTopX + +/-! +`…X` siblings of `VmInvAfter`'s dispatcher-chain statements: the same results concluding `VEAnyX` (`VEAny` plus the ghost +fact `VxE`), asking `[VxE Z]` of the entry state. The merged statements are unchanged. Generated by +`handoff/tools/gen_vxsib.py`. The siblings of the merged theorems below the family are in `VmVxLow`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 Fl4Proofs.P3.Endp + +/-- `…X` sibling of `VDispAtFor` (`VEAnyX`; module docstring). -/ +def VDispAtForX (scope : capability.Cap → Prop) (on rc : Fl4Spec.ObjRef → Bool) (Z : XSets) (s : S) (ar : A) (σ : Fl4Spec.State Unit) (ct : U64) + (is_call blocking can_donate first_phase : Bool) (cptr : U64) : Prop := + ∀ (info : msginfo.MessageInfo) (mi : Fl4Spec.MessageInfo) (buf : U64) (buffer : Option Fl4Spec.ObjRef) + (r2 : core.result.Result (Aeneas.Std.Array obj.Slot 3#usize) error.Fault) + (er : Except Fl4Spec.Fault (List (Fl4Spec.Cap × Fl4Spec.CslotPtr))) (args : List Fl4Spec.Data), + msg.msg_info st s ct = ok info → Fl4Spec.getMessageInfo (Z := Unit) ct.bv σ = ({(mi, σ)}, False) → + msg.lookup_ipc_buffer st s false ct = ok buf → + Fl4Spec.lookupIpcBuffer (Z := Unit) false ct.bv σ = ({(buffer, σ)}, False) → + ipc.lookup_extra_caps st s ct buf info = ok r2 → + (Fl4Spec.lookupExtraCaps (Z := Unit) ct.bv buffer mi).run σ = ({(er, σ)}, False) → + Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(args, σ)}, False) → + ∀ (sl : Fl4Spec.CslotPtr) (c : capability.Cap) (e : Aeneas.Std.Array obj.Slot 3#usize) + (exs : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)), (∃ p, sl.1 = p.bv ∧ Z.th p = some (.cnode sl.2.length)) → + capAt (L.mem s) (cslotAddr sl) = ok c → + Fl4Spec.getCap (Z := Unit) sl σ = ({(absCap c, σ)}, False) → r2 = .Ok e → er = .ok exs → scope c → + syscall.decode_invocation st arI s ar info.label (msgLen info buf) (cslotAddr sl) c blocking is_call + can_donate first_phase buf e ⦃ res => + let m := specDecV first_phase mi.miLabel args (Fl4Spec.toBl cptr.bv) sl c exs buffer + VDispPostVEX (L := L) arI AR MR on rc σ blocking is_call can_donate m s res ∨ + VDispReplyVEX (L := L) arI (AR := AR) on rc σ blocking is_call can_donate m ct s res ⦄ + +/-- `…X` sibling of `VDispAt` (`VEAnyX`; module docstring). -/ +abbrev VDispAtX := VDispAtForX (L := L) arI AR MR (fun _ => True) + +/-- `…X` sibling of `VDispNativeAt` (`VEAnyX`; module docstring). -/ +abbrev VDispNativeAtX := VDispAtForX (L := L) arI AR MR Vs.NativeCap + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.dispatchAtNonNative` (`VEAnyX`; module docstring). -/ +theorem VmEptState.dispatchAtNonNativeX [PlainStore st] + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) + (hRef : ArchRefines arI AR) (hVA : ZvVirtAgree arI) + (hK : ARKeep arI AR) (hSy : ZvSyncKeep arI AR) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (hAT : P0.ArchTotal arI) (hAM : ARMask arI AR) (hVal : PortInValAt arI a σ) + (on rc : Fl4Spec.ObjRef → Bool) {ct : U64} (hct : sched.cur_thread st s = ok ct) (hRun : ZvCtRun σ ct) + (call blk don fp : Bool) (cptr : U64) : + VDispAtForX (L := L) arI AR MR (fun c => ¬ Vs.NativeCap c) on rc Z s a σ ct call blk don fp cptr := by + intro info mi buf buffer r2 er args hinfo hmi hbuf hbufS hr2 her hmrs sl c ex exs hsl hc hcs hr he hN + have B := J.1.1 + have hX := B.xferH + obtain ⟨hlbl, hExtra⟩ := B.frontJ hX.1 hX.2.1 hct hinfo hmi hbuf hbufS hr2 her + have heX := hExtra ex exs hr he + rw [← hlbl] + have hPl := B.xtop.rel.1 + obtain ⟨pc, hp, hpc⟩ := hsl + rcases sl with ⟨sp, idx⟩ + dsimp only at hp hpc + subst sp + have hAs : AbsSlot Z.th (pc.bv, idx) := ⟨pc, rfl, Or.inl hpc⟩ + have hV := B.vargs hct hinfo hmi hbuf hbufS hmrs + have hv' : VArgs st s (L.curW s) buf (msgLen info buf) args := by + have e : ct = L.curW s := ok_inj (hct.symm.trans (cur_thread_eq (L := L) s)) + exact e ▸ hV + let words := args.map fun x => (⟨x⟩ : U64) + have hmrs' : Fl4Spec.getMrs (Z := Unit) ct.bv buffer mi σ = ({(words.map UScalar.bv, σ)}, False) := by + rw [show words.map UScalar.bv = args from zv_map_mk args] + exact hmrs + obtain ⟨hlen, hargs, -⟩ := B.argsJ hX.1 hX.2.1 hct hinfo hmi hbuf hbufS hmrs' + have hd := VmEptState.decodeDispX arI (MR := MR) hRef hVA J hni on rc + cases c <;> try exact (hN trivial).elim + case Vcpu p => + have hcap := capV_of_capAt hc + have hpv := J.1.2.extv.cap (cslotAddr (pc.bv, idx)) hAs.isSlot + rw [hcap] at hpv + exact hd.vcpu hK hSy hCap hAT hpv (capV_vcpu_canonical hcap) hct hRun blk call don fp + (B.bufOut J.1.2.vm hct hbuf) ex args hV exs heX (Fl4Spec.toBl cptr.bv) (pc.bv, idx) buffer + case Ept p level m asid va => + exact WP.spec_mono (hd.ept blk call don fp buf ex hpc rfl hc args hv' exs (ExtraOK.relS hPl heX) fp + (Fl4Spec.toBl cptr.bv) buffer) fun _ h => Or.inl h + case IoPort first last owner => + rw [← zv_map_mk args] + exact hd.ioPort hAM hAT hct hRun hVal words hlen hargs (pc.bv, idx) hAs (capV_of_capAt hc) + blk call don fp ex (Fl4Spec.toBl cptr.bv) exs buffer + case Frame p sz rt dev m ep asid va => + have hmep : m = true ∧ ep = true := Classical.byContradiction hN + obtain ⟨rfl, rfl⟩ := hmep + by_cases hE : info.label = fl4_abi.label.X86PageMapEPT + · have hEL : info.label.val = Fl4Spec.fl4PageMapEptLabel := by + rw [hE] + simp [fl4_abi.label.X86PageMapEPT, Fl4Spec.fl4PageMapEptLabel] + exact WP.spec_mono (hd.frameEpt hEL blk call don fp buf ex hpc rfl hc args hv' exs (ExtraOK.relS hPl heX) fp + (Fl4Spec.toBl cptr.bv) buffer) fun _ h => Or.inl h + have hFL := frame_label info.label hE + by_cases hu : info.label = fl4_abi.label.X86PageUnmap + · exact WP.spec_mono (hd.frameUnmap hu blk call don fp buf ex hpc rfl hc args exs fp + (Fl4Spec.toBl cptr.bv) buffer) fun _ h => Or.inl h + by_cases hm : info.label = fl4_abi.label.X86PageMap + · rw [hm] + exact WP.spec_mono (hd.frameMapReject hct p rt asid va (msgLen info buf) buf sz dev blk call don fp + (pc.bv, idx) ex args (Fl4Spec.toBl cptr.bv) exs buffer) fun _ h => Or.inl h + by_cases hg : info.label = fl4_abi.label.X86PageGetAddress + · exact WP.spec_mono (hd.frameGetAddr hct hRun hFL hg sz dev blk call don fp + (pc.bv, idx) ex args (Fl4Spec.toBl cptr.bv) exs buffer) fun _ h => Or.inr h + by_cases ht : info.label = fl4_abi.label.X86PageTestAndClearAccessed + · rw [ht] + exact WP.spec_mono (hd.frameTac hct hRun sz dev blk call don fp (pc.bv, idx) hAs (capV_of_capAt hc) + ex args (Fl4Spec.toBl cptr.bv) exs buffer) fun _ h => Or.inr h + by_cases hs : info.label = fl4_abi.label.X86PageDeriveSubframe + · rw [hs, ← zv_map_mk args] + exact WP.spec_mono (hd.frameDerive hpc hct hc words hlen hargs blk call don fp ex + (Fl4Spec.toBl cptr.bv) exs (ExtraOK.vsExtra hPl heX) buffer) fun _ h => Or.inl h + exact WP.spec_mono (hd.frameOther hct hFL hm hu hg ht hs p rt asid va (msgLen info buf) buf + sz dev blk call don fp (pc.bv, idx) ex args (Fl4Spec.toBl cptr.bv) exs buffer) fun _ h => Or.inl h + +/-- `…X` sibling of `HIPostVE` (`VEAnyX`; module docstring). -/ +def HIPostVEX (on rc : Fl4Spec.ObjRef → Bool) (σ : Fl4Spec.State Unit) (is_call blocking can_donate first_phase : Bool) + (cptr : U64) (res : core.result.Result Unit error.Exc × S × A) : Prop := + ∃ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true is_call blocking can_donate + first_phase (Fl4Spec.toBl cptr.bv)).run σ).1, + ((res.1 = .Ok () ∧ y.1 = .ok ()) ∨ (res.1 = .Err .Preempted ∧ y.1 = .error ())) ∧ + RelTopM (L := L) res.2.1 y.2 ∧ + (if res.1 = .Err .Preempted then + ∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR res.2.1 res.2.2 y.2 + else VEAnyX (L := L) arI AR res.2.1 res.2.2 y.2) + +/-- `…X` sibling of `InvAfterVE` (`VEAnyX`; module docstring). -/ +def InvAfterVEX (on rc : Fl4Spec.ObjRef → Bool) (m : Fl4Spec.PMonad Unit Unit) (σ : Fl4Spec.State Unit) + (r : Bool × S × A) : Prop := + ∃ y ∈ (m.run σ).1, + (r.1 = true ∧ y.1 = .ok () ∧ VEAnyX (L := L) arI AR r.2.1 r.2.2 y.2) ∨ + (r.1 = false ∧ y.1 = .error () ∧ + ∃ z ∈ (Fl4Spec.preemptionPathVF (Z := Unit) on rc y.2).1, VEAnyX (L := L) arI AR r.2.1 r.2.2 z.2) + +variable {arI AR MR} + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.afterV` (`VEAnyX`; module docstring). -/ +theorem VmEptState.afterVX (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {on rc : Fl4Spec.ObjRef → Bool} {σ : Fl4Spec.State Unit} {is_call blocking can_donate first_phase : Bool} + {cptr : U64} {res : core.result.Result Unit error.Exc × S × A} + (h : HIPostVEX (L := L) arI AR MR on rc σ is_call blocking can_donate first_phase cptr res) + (hO : ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true is_call blocking can_donate first_phase + (Fl4Spec.toBl cptr.bv)).run σ).1, y.1 = .error () → + ∀ (s1 : S) (a1 : A), (∃ (V X : Set U64) (XL : U64 → Nat), VmEptPre (L := L) arI V X XL AR MR s1 a1 y.2) → + VmOrc st on rc s1 y.2) : + syscall.after_invocation st arI res.2.1 res.2.2 res.1 ⦃ r => + InvAfterVEX (L := L) arI AR on rc (Fl4Spec.fl4HandleInvocationVF (Z := Unit) on rc true is_call blocking + can_donate first_phase (Fl4Spec.toBl cptr.bv)) σ r ⦄ := by + obtain ⟨y, hy, hres, -, hX⟩ := h + rcases hres with ⟨h1, hy1⟩ | ⟨h1, hy1⟩ + · rw [if_neg (by rw [h1]; intro e; cases e)] at hX + rw [h1] + unfold syscall.after_invocation + rw [WP.spec_ok] + exact ⟨y, hy, Or.inl ⟨rfl, hy1, hX⟩⟩ + · rw [if_pos h1] at hX + obtain ⟨V, X, XL, hP⟩ := hX + rw [h1] + refine WP.spec_mono (hP.preemptPathVFkX hARI hAT hVA hARI.toARKeep hnd hM hMK hPA hIA hmrt + (fun _ => hiVF_err_nrvm on rc hy hy1) (fun _ => ⟨hmax, htu⟩) (hO y hy hy1 _ _ ⟨V, X, XL, hP⟩) .Preempted) ?_ + rintro r ⟨hr1, z, hz, ⟨Z', H', Cs', Ac', Lst', D', hv, J'⟩, -, -⟩ + exact ⟨y, hy, Or.inr ⟨hr1, hy1, z, hz, V, X, XL, Z', H', Cs', Ac', Lst', D', hv, J'⟩⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmJAtZRB.lean b/proofs/Fl4Proofs/P3/VmJAtZRB.lean new file mode 100644 index 000000000..4aedccb76 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmJAtZRB.lean @@ -0,0 +1,548 @@ +/- +P3 (line mmuAD, Stage 3 step 3 (R layer, route (a)), generated by `handoff/tools/gen_rstate.py`): **the VM R-mirror prerequisites**. +`JAtZRB` is `JAtZB` with `cur : CurInvR σ`; `JAtZB.toR`, `JAtZB.sc_frameR`, `JAtZB.cs_frame{VD,V,B,I}R` are the `JAtZB` frames +ending in it (proofs verbatim); `VmStateR` / `VmEptStateR` / `VEAnyR`; `VmEptState.ofNatR` is `ofNat` with an R target +(`VcpuCore.ofBSSX`, `EptCore.ofNatX`: the end state read only through its `XTopAt` and `thT`). No named premise. +-/ +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.VmDisp +import Fl4Proofs.P3.EptReport +import Fl4Proofs.P3.ScJointFrame +import Fl4Proofs.P3.XTopJointJAtZRA +import Fl4Proofs.P3.XTopJointStep +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **`JAtZB` with `CurInv` asked only under a resumed action** (`JAtZR` without `ext`). -/ +structure JAtZRB (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop where + -- 1a: the refinement relation + xtop : XTopAt (L := L) AR Z s a σ + call : Sched.CallRel (L.mem s) Z.R Z.C σ Lst + rfree : Sched.RFree (L.mem s) Z.R Z.C σ Lst + /-- A reply bound to a thread blocked receiving has null links (ScRecvLinks; + l4v `valid_replies`). -/ + rrecv : Sched.RRecv (L.mem s) Z.R σ + yld : Sched.YieldRel Z.T Z.C (L.mem s) σ + mcp : Sched.McpRel Z.T (L.mem s) σ + buf : BufWordRel Z.T (L.mem s) σ + -- 1b: placement + ks : KsApart Z.N Z.T Z.E Z.R Z.B (L.ksb s) + thT : ∀ t, Z.th t = some .tcb → t ∈ Z.T + /-- [carried] `Z.th` holds TCBs and CNodes only (its one producer, the + retype's `ThX`, adds `thKindN`'s kinds; deletions only drop entries). -/ + thk : ∀ q k, Z.th q = some k → k = .tcb ∨ ∃ r, k = .cnode r + ip : IPlaced Z.N Z.T Z.E Z.R Z.B + rp : Sched.RPlaced st Z.N Z.T Z.C Z.R Z.c s σ + apart : Sched.SlotsApart (st := st) Z.th Z.T Z.N Z.C Z.R Z.c s σ + nk : ∀ n ∈ Z.N, n.val + 40 ≤ (L.ksb s).val ∨ (L.ksb s).val + 13568 ≤ n.val + /-- [carried] The IPC-buffer words miss every node slot of the kernel-state + block (all 64 cores', not only `Cs`'s: the buffer words are user frames; + kept by every step, `Z.B` grows only by a frame retype from an untyped + region, which misses the block). -/ + nodeB : ∀ wp ∈ Z.B, ∀ c : U64, c.val < 64 → wp.1.val + 8 ≤ L.nodeBase s c ∨ L.nodeBase s c + 4288 ≤ wp.1.val + jpl : ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s σ + cover : Sched.SpecCover H σ + obj : Sched.ObjCover Z.N Z.T Z.E Z.R Z.C σ + /-- [carried] The sets' objects are `H`'s (derivable: `IpcRel`/`SchedRel` + and `SpecCover`, `JPlaced`). -/ + zH : (∀ t ∈ Z.T, H t = some .tcb) ∧ (∀ e ∈ Z.E, H e = some .ep) ∧ (∀ r ∈ Z.R, H r = some .reply) ∧ + (∀ n ∈ Z.N, H n = some .ntfn) + sfree : Sched.SpecFree (L.mem s) Z.G σ + /-- [carried] `H`'s endpoints, notifications and replies are the sets' + (the converse of `zH`; for the `SchedValid` rebuild). -/ + hset : (∀ e, H e = some .ep → e ∈ Z.E) ∧ (∀ n, H n = some .ntfn → n ∈ Z.N) ∧ + (∀ r, H r = some .reply → r ∈ Z.R) + /-- [carried] `H`'s SCs are `Z.C`'s (the converse of `JPlaced.sc` with + `ObjCover.sc`; l4v: an SC object of the heap is one the scheduler + relation covers). Kept by every step at `J` (each keeps `H` and `Z.C`; + a retype adds its new SCs to both). The idle SC, when the spec has one at + a non-null address, is in both (`JPlaced.sc`, `ObjCover.sc`); a cap never + names it (`CapObjH`). With `JPlaced.cobj` it gives an SC cap's spec SC + (`JAtZ.scCap`). -/ + hscC : ∀ c z, H c = some (.sc z) → c ∈ Z.C + jcov : Sched.JCovD (L := L) Z.th H Z.G D Cs s + kern : JKern (L := L) arI H Cs D Z.th s σ + -- 1c: CSpace + del : DelInv Z.th (L.mem s) + ut : UtInv (L.mem s) Z.G + bound : DelBound Z.th + /-- [carried] `FinM` at every tree of the slots (`FinInv` gives it at + `Z.G`; `DelE` reads every `G`). -/ + finM : ∀ G, MdbLinks Z.th (L.mem s) G → FinM (L.mem s) G + /-- [carried] The RAM untyped caps' facts (`UtCapP3`: l4v `valid_untyped`, + `valid_cap`, the kernel's placement). Not asked of device untypeds: since + losfair/FL4#34 a device untyped's pointer `paddr + PPTR_BASE` may lie in + the kernel image, so its region may hold the kernel-state block with the + IRQ node's CNodes (`IntLayout.node`, objects of `Z.th` with MDB slots) and + the preemption point's word: `free`, `nostr`, `nsFree`, `ns` and `pp` are + false of such a cap at boot. -/ + utc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s σ Z.G sl p sb fi + -- 1d: the scheduler + nsc : Sched.NSC Z.N Z.C σ + inv : Sched.Inv σ + /-- `CurInv` unless a reschedule is pending (mid-entry, as `ScEntry`). -/ + cur : CurInvR σ + /-- The current SC's owner, or the current SC unowned (`CurOwnW`, what + `schedule` from a mid-entry state reads; unowned after the current thread + deleted itself or unbound its SC; `curOwn_of_curInv` where `CurInv` + holds). -/ + own : Sched.CurOwnW Z.T Z.C σ + off : Sched.OffOK σ + act : Sched.ActIdle σ + c0 : ∀ q ∈ Z.C, q ≠ 0#u64 + bsa : Sched.BlockedScActive σ + sv : Sched.SchedValid σ + /-- The spec's IPC queues sorted by priority (l4v `sorted_ipc_queues`). -/ + isorted : Sched.IpcSorted σ + /-- The spec's SC, TCB and notification links (per-op; for the + `SchedValid` rebuild). -/ + slink : Sched.ScLinkHyp σ + /-- The current thread runnable, or the idle thread current (`CurRunW`: + at kernel exit `schedule` may have chosen the idle thread), unless a + reschedule or switch is pending (`CurRunM`: mid-entry after a blocking + step, or after suspending the current thread with a switch pending). -/ + curRun : CurRunM σ + -- 1e: P1 + blk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s + /-- [carried] The interrupt state and node layout (`IntLayout`, the boot's + [P]; kept by every step: `Z.th` and the kernel-state base are kept). -/ + intL : IntLayout Z.th (L.ksb s) + /-- [carried] **The VSpace caps placed in the relation's own sets**, weakened + (VsCapW's `VsCapsW`: l4v `valid_cap` for the arch caps with + `valid_table_caps`), at every slot of the typed heap: an unmapped page-table/directory/PDPT cap's table in + its set, a PML4 or ASID pool cap's object in its set, the ASID and + alignment facts; a mapped table may lie outside the sets (pruned by a + detype). Kept by every step (`VsSlotsW.of_eq` for slots kept, + `VsSlotsT` for the slot steps, `VCapW.transfer` for a copy), the sets kept + or grown. -/ + vsc : Vs.VsSlotsW Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s) + /-- [carried] **Every IPC frame cap valid** (l4v `valid_ipc_buffer_cap`, + from `valid_objs`; FL4's form `Endp.IpcBufCapOK`: the buffer's words in a + user frame, so an alias inside a large page from `Page_DeriveSubframe` + counts). Kept by every step not touching an IPC frame or a data page + (`IpcFrKeep`), set by SetIPCBuffer/Configure (`IpcBufCapOK.setFrame`, the + cap checked), dropped TCBs' frames by `IpcBufCapOK.sdrop`. -/ + bok : Endp.IpcBufCapOK σ + /-- [carried] **`B` covers every word of every non-device user frame** + (`FrameCover`; FL4's ghost `B` relates FL4 memory and the spec's + `underlyingMemory` there, l4v's shared `machine_state`). Kept by every step + keeping the data pages (`FrameCover.ipcFr`), grown with `B` at the frame + retype, shrunk with `B` at the detype (`FrameCover.sdrop`). -/ + fcov : FrameCover Z.B σ + /-- [carried] **Every non-device frame cap names user frames** (l4v + `valid_cap` for frame caps: each byte of a slot's frame cap range lies in a + non-device data page). Kept by every step keeping the data pages and moving + frame caps within old ranges (`Vs.VsSlotsT`'s `FrSlots` part: copy, mint, + move, swap, DeriveSubframe's sub-range), set at the frame retype for the new + pages, released at the detype by `FrameOffJ`. -/ + fcap : Vs.FrSlots Z.th (UFr σ) (L.mem s) + /-- [carried] **The liveness bundle at the ghosts** (KENTRY-PLAN g2i; INV-PLAN + E15, the former `JEntryAt.live` / the gen-33 joint entry theorem's `hLv` [J]): `LiveJ` + (l4v `if_unsafe_then_cap`, `if_live_then_nonz_cap`, `zombies_final`; the idle + thread and the kernel-state block exempt, KENTRY-PLAN g2e), `DbgWF` (FL4's + debug lists) and `NullOrig` (l4v `valid_nullcaps`) at `Z.th`, `H`, `Cs`, + `Z.G`. Each construction proves it where its frames are visible + (`JAtZ.liveX` for a step keeping the slots). -/ + live : LiveDN (L := L) Z.th H Cs Z.G s σ + /-- [carried] **The TCB zombies' TCBs are in the dead set** (so a frame step's + dead-set carry keeps `LiveJ.zdead`; every TCB zombie's TCB is `DeadP` by + `live`, so `D ∪ ZTcb` is always a dead set: `JAtZ.addDeadSet`). -/ + zD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D + /-- [carried] **The `JCarry` facts** (KENTRY-PLAN g7b-4, jc1): the IPC-buffer + words in non-device data pages (`BufPgHyp`, C21 / C24 bufObj) and `H`'s arch + objects no larger than their spec objects (`ArchFitHyp`, the first half of C16 / + C22 / C24 heapH); jc2: the device untyped caps' apartness (`DevApart`, C25) with + RAM/device range separation (`DevSep`). A frame step keeps the σ part by + `JCarry.frame` (a `JCarryS`) and the device part by `JCarry.devStep` (`JCarryS.withP`). -/ + carry : JCarry (L := L) Z H Cs s σ + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- `JAtZB` gives `JAtZRB`. -/ +theorem JAtZB.toR (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ where + xtop := J.xtop + call := J.call + rfree := J.rfree + rrecv := J.rrecv + yld := J.yld + mcp := J.mcp + buf := J.buf + ks := J.ks + thT := J.thT + thk := J.thk + ip := J.ip + rp := J.rp + apart := J.apart + nk := J.nk + nodeB := J.nodeB + jpl := J.jpl + cover := J.cover + obj := J.obj + zH := J.zH + sfree := J.sfree + hset := J.hset + hscC := J.hscC + jcov := J.jcov + kern := J.kern + del := J.del + ut := J.ut + bound := J.bound + finM := J.finM + utc := J.utc + nsc := J.nsc + inv := J.inv + cur := curInvR_of J.cur + own := J.own + off := J.off + act := J.act + c0 := J.c0 + bsa := J.bsa + sv := J.sv + isorted := J.isorted + slink := J.slink + curRun := J.curRun + blk := J.blk + intL := J.intL + vsc := J.vsc + bok := J.bok + fcov := J.fcov + fcap := J.fcap + live := J.live + zD := J.zD + carry := J.carry + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 1000000 in +/-- **`JAtZB.sc_frame` with `CurInvR` after.** -/ +theorem JAtZB.sc_frameR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') + (hX : XTopAt (L := L) AR Z s' a' σ') + (hcall : Sched.CallRel (L.mem s') Z.R Z.C σ' Lst') (hrf : Sched.RFree (L.mem s') Z.R Z.C σ' Lst') + (hyl : Sched.YieldRel Z.T Z.C (L.mem s') σ') (hmcp : Sched.McpRel Z.T (L.mem s') σ') + (hbuf : BufWordRel Z.T (L.mem s') σ') + (hrp : Sched.RPlaced st Z.N Z.T Z.C Z.R Z.c s' σ') (hap : Sched.SlotsApart (st := st) Z.th Z.T Z.N Z.C Z.R Z.c s' σ') + (hjpl : ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s' σ') (hjc : Sched.JCovD (L := L) Z.th H Z.G D' Cs s') + (hkern : JKern (L := L) arI H Cs D' Z.th s' σ') + (hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi) + (hnsc : Sched.NSC Z.N Z.C σ') (hinv : Sched.Inv σ') (hcur : CurInvR σ') + (hown : Sched.CurOwnW Z.T Z.C σ') (hoff : Sched.OffOK σ') + (hact : Sched.ActIdle σ') (hbsa : Sched.BlockedScActive σ') (hcr : CurRunM σ') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (hidl : L.idleW s' = L.idleW s) (hDD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D') : + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ' := by + have hks : L.ksb s' = L.ksb s := hw.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl' : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hsl : Sched.ScLinkHyp σ' := scLink_rebuild hX J.tcbs_eq.1 (J.obj.tkw hK) hblk J.hset.2.1 hjpl J.c0 + have hsv : Sched.SchedValid σ' := schedValid_rebuild hX J.tcbs_eq.1 J.tcbs_eq.2 (J.obj.tkw hK) hblk J.hset.1 + J.hset.2.1 hcall hyl J.c0 hinv hsl + refine ⟨hX, hcall, hrf, + J.rrecv.frame (Sched.TFoot.keep J.xtop.fin.1.1.1 J.rp hw).2.2 (Sched.KhRB.of_sctkw hK), hyl, hmcp, hbuf, + by rw [hks]; exact J.ks, J.thT, J.thk, J.ip, hrp, hap, + fun n hn => by rw [hks]; exact J.nk n hn, + fun wp hw c hc => by simp only [P1.LawfulStore.nodeBase, hks]; exact J.nodeB wp hw c hc, hjpl, J.cover.tkw hK, J.obj.tkw hK, J.zH, ?_, J.hset, J.hscC, hjc, hkern, + J.del.of_same hSS, UtInv.of_same hM J.ut hSS, J.bound, fun G hG => ?_, hutc, hnsc, hinv, hcur, hown, hoff, hact, + J.c0, hbsa, hsv, J.isorted.tkw hK, hsl, hcr, hblk, by rw [hks]; exact J.intL, + J.vsc.of_eq fun y hy => hSS.capV hy, + J.bok.keep (IpcFrKeep.of_otk hK.kh), J.fcov.ipcFr (IpcFrKeep.of_otk hK.kh), + J.fcap.keepU (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) (IpcFrKeep.of_otk hK.kh).page, + J.liveX hX rfl rfl rfl hSS hKd hK.orig hidl (fun p hp => hjc.dead p (hDD p hp)), J.zDX hSS hDD, + (J.carry.frame subset_rfl (IpcFrKeep.of_otk hK.kh) hK.ksz).withP + (J.carry.devKeep J.xtop.rel.2.1.2.2.1 hks hSS hX.rel.2.1.2.2.1 (idleH_of hX (J.obj.tkw hK) J.zH.1 hjpl hinv hact)) + (J.carry.vsSame hSS hks (ArchFwd.of_ipcFr (IpcFrKeep.of_otk hK.kh))) + (J.carry.tabKeep J.xtop.rel.2.1.2.2.1 hSS hX.rel.2.1.2.2.1 hK.ksz) + (J.vsW hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h)⟩ + · refine SpecFree.frame J.sfree (fun x' hx' p sb fi' d hc => ⟨x', hx', fi', ?_, le_rfl⟩) hK.ksz + rw [← hSS.capV (hsl' x' hx')]; exact hc + · have hG0 : MdbLinks Z.th (L.mem s) G := hG.of_same_symm hSS + have hsl' : ∀ x ∈ G.chains.flatten, IsSlot Z.th x := fun x h => ((hG0.members x).1 h).1 + exact (J.finM G hG0).of_capV fun x hx => hSS.capV (hsl' x hx) + +set_option maxHeartbeats 1000000 in +/-- `JAtZB.cs_frameVD` ending in `JAtZRB`. -/ +theorem JAtZB.cs_frameVDR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : Set U64 × Set U64 × Set U64 × Set U64 × Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' V') s' a' σ') (hkb : KBack σ σ') (hbf : JBuf (L := L) Z s' σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hvc : Vs.VsSlotsW Z.th V'.1 V'.2.1 V'.2.2.1 V'.2.2.2.1 V'.2.2.2.2 (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hcy : JCarryS Z.B H σ') (hdv : IdleH (L := L) H s' → DevApart (L := L) Z.th H G' Cs s' ∧ DevSep G' (L.mem s')) + (hvy : VsTyp (L := L) (Z.mv G' V') s' σ') (htb : DevTab G' (L.mem s') σ') + (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR (Z.mv G' V') H Cs Ac' Lst' D' s' a' σ' := + have hsl : Sched.ScLinkHyp σ' := scLink_rebuild hX J.tcbs_eq.1 (J.obj.kback hkb) R.blk J.hset.2.1 R.jpl J.c0 + ⟨hX, R.call, R.rfree, R.rrecv, R.yld, R.mcp, R.buf, by rw [hks]; exact J.ks, J.thT, J.thk, J.ip, R.rp, R.apart, + fun n hn => by rw [hks]; exact J.nk n hn, + fun wp hw c hc => by simp only [P1.LawfulStore.nodeBase, hks]; exact J.nodeB wp hw c hc, R.jpl, J.cover.kback hkb, J.obj.kback hkb, J.zH, hC.sfree, J.hset, J.hscC, R.jcov, + R.kern, hC.del, hC.ut, J.bound, hC.finM, R.utc, R.nsc, R.inv, R.cur, R.own, R.off, R.act, J.c0, R.bsa, + schedValid_rebuild hX J.tcbs_eq.1 J.tcbs_eq.2 (J.obj.kback hkb) R.blk J.hset.1 J.hset.2.1 R.call R.yld J.c0 R.inv + hsl, R.isorted, hsl, R.curRun, R.blk, by rw [hks]; exact J.intL, hvc, hbf.1, hbf.2.1, hbf.2.2, hlv, hzD, + hcy.withP (hdv (idleH_of hX (J.obj.kback hkb) J.zH.1 R.jpl R.inv R.act)) hvy htb hvn⟩ + +set_option maxHeartbeats 1000000 in +/-- `JAtZB.cs_frameV` ending in `JAtZRB`. -/ +theorem JAtZB.cs_frameVR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : Set U64 × Set U64 × Set U64 × Set U64 × Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' V') s' a' σ') (hkb : KBack σ σ') (hbf : JBuf (L := L) Z s' σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hvc : Vs.VsSlotsW Z.th V'.1 V'.2.1 V'.2.2.1 V'.2.2.2.1 V'.2.2.2.2 (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hcy : JCarryS Z.B H σ') (hdu : DevFrom Z.G G' (L.mem s) (L.mem s') ∧ Sched.UtFrom Z.G G' (L.mem s) (L.mem s')) + (hvy : VsTyp (L := L) (Z.mv G' V') s' σ') (hAF : ArchFwd σ σ') (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR (Z.mv G' V') H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameVDR (hvn := hvn) hks hX hkb hbf hC R hvc hlv hzD hcy (fun hI => J.carry.devStep hks hdu hI) hvy + (J.carry.devTab.frame hdu.1.sub (tabBack_of hkb hAF)) + +set_option maxHeartbeats 1000000 in +/-- `JAtZB.cs_frameB` ending in `JAtZRB`. -/ +theorem JAtZB.cs_frameBR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : XVs} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' Z.vs) s' a' σ') (hkb : KBack σ σ') (hbf : JBuf (L := L) Z s' σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hvc : Vs.VsSlotsW Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hcy : JCarryS Z.B H σ') (hdu : DevFrom Z.G G' (L.mem s) (L.mem s') ∧ Sched.UtFrom Z.G G' (L.mem s) (L.mem s')) + (hvy : VsTyp (L := L) (Z.mv G' Z.vs) s' σ') (hAF : ArchFwd σ σ') (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameVR (hvn := hvn) hks hX hkb hbf ⟨hC.del, hC.ut, hC.finM, hC.sfree⟩ + ⟨R.call, R.rfree, R.rrecv, R.yld, R.mcp, R.buf, R.rp, R.apart, R.jpl, R.jcov, R.kern, R.utc, R.nsc, R.inv, + R.cur, R.own, R.off, R.act, R.bsa, R.isorted, R.curRun, R.blk⟩ hvc hlv hzD hcy hdu hvy hAF + +set_option maxHeartbeats 1000000 in +/-- `JAtZB.cs_frameI` ending in `JAtZRB`. -/ +theorem JAtZB.cs_frameIR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : XVs} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' V') s' a' σ') {W : U64 → Prop} (hW : P1.WritesIn st s s' W) + (hWO : ∀ b, W b → Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hWV : ∀ b, W b → ∀ t ∈ Z.T, b ≠ tVcpuA t) + (har : σ'.archState = σ.archState) + (hao : ∀ q ao, σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) (hsz : Sched.SzKeep σ σ') + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hkb : KBack σ σ') (hfk : IpcFrKeep σ σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hK : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hidl : L.idleW s' = L.idleW s) (hDD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D') : + JAtZRB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameBR (hvn := (J.vsW hW hWO)) hks (J.xtop.vs_backO hX hW hWO har hao hsz) hkb (J.bfK hfk (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy)) + hC R (J.vsc.of_eq fun y hy => hSS.capV hy) + (J.liveX hX rfl rfl rfl hSS hK horig hidl fun p hp => R.jcov.dead p (hDD p hp)) (J.zDX hSS hDD) (J.carry.of_ao hkb hao) + (devFrom_same J.xtop.rel.2.1.2.2.1 hX.rel.2.1.2.2.1 hSS) (J.carry.vsSame hSS hks (ArchFwd.of_eq hao)) + (ArchFwd.of_eq hao) + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- `JAtZB.asidW` from the end state's `XTopAt` alone. -/ +theorem JAtZB.asidWX (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (hX' : XTopAt (L := L) AR (Z.mv Z.G V') s' a' σ') {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hn : ∀ hi : U64, hi.val < 2048 → ¬ W (atA (L.ksb s) hi)) : + σ'.archState.x64AsidTable = σ.archState.x64AsidTable := by + have hR := B.xtop.fin.2.1 + have hR' := hX'.fin.2.1 + have hVP := B.xtop.fin.1.2.vs + have hk : L.ksb s' = L.ksb s := hw.1 + funext x + obtain ⟨hi, hh, hx⟩ : ∃ hi : U64, hi.val < 2048 ∧ Fl4Spec.ucast 11 hi.bv = x := by + have hlt : x.toNat < 2048 := x.isLt + refine ⟨⟨BitVec.ofNat 64 x.toNat⟩, ?_, ?_⟩ + · show (BitVec.ofNat 64 x.toNat).toNat < 2048 + rw [BitVec.toNat_ofNat]; omega + · rw [ucast_ofNat] + apply BitVec.eq_of_toNat_eq + show (BitVec.ofNat 11 (BitVec.ofNat 64 x.toNat).toNat).toNat = x.toNat + rw [BitVec.toNat_ofNat, BitVec.toNat_ofNat]; omega + have hv := atA_val hVP.ksb_fit hh + have hm : L.mem s' (atA (L.ksb s) hi) = L.mem s (atA (L.ksb s) hi) := + hw.mem_eq (by unfold P1.Aligned; have := hVP.ksb_al; rw [hv]; omega) (hn hi hh) + rw [← hx, hR'.table hi hh, hk, hm, ← hR.table hi hh] + +set_option maxHeartbeats 4000000 in +/-- **`VcpuCore.ofBSS` from the end state's `XTopAt` and `thT` alone** (no end `JAtZB`: the end may be an R state). -/ +theorem VcpuCore.ofBSSX (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : VcpuCore (L := L) V Z H s σ) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (hX' : XTopAt (L := L) AR (Z.mv Z.G V') s' a' σ') (hT' : ∀ t, Z.th t = some .tcb → t ∈ Z.T) + {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (nV : ∀ u ∈ V, ∀ b, W b → b.val < u.val ∨ u.val + 32768 ≤ b.val) (hh : ∀ b, W b → b ≠ P1.vHeadA st s) + (hv : VKeep (L := L) Z.th s s') + (hvk : ∀ u ∈ V, σ'.kheap u.bv = σ.kheap u.bv) + (hsz : ∀ o ko', σ'.kheap o = some ko' → ∃ ko, σ.kheap o = some ko ∧ Fl4Spec.objBits ko' = Fl4Spec.objBits ko) + (hrv : ∀ o x', σ'.kheap o = some (.tcb x') → x'.tcbState = .runningVM → + ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM) + (hnv : ∀ p (vs : Fl4Spec.VcpuState), σ'.kheap p = some (.archObj (.vcpu vs)) → + σ.kheap p = some (.archObj (.vcpu vs))) : + VcpuCore (L := L) V (Z.mv Z.G V') H s' σ' := by + have R' : RunVmB (L := L) Z.th s' := by + intro t ht h8 + rw [hv t ht] + refine C.rvm t ht ?_ + obtain ⟨x', hk', -⟩ := hX'.rel.2.1.1.tcb t ht + have w' := (hX'.fin.1.1.2.2.1.at_tcb (hT' t ht) hk').st + rw [Sched.stA_tStA] at h8 ⊢ + rw [h8] at w' + have hx' : x'.tcbState = .runningVM := by + cases hx : x'.tcbState <;> rw [hx] at w' <;> simp_all [Notif.tsCode] + obtain ⟨x, hk, hx⟩ := hrv _ x' hk' hx' + have w0 := (B.xtop.fin.1.1.2.2.1.at_tcb (B.thT t ht) hk).st + rw [hx] at w0 + apply UScalar.eq_of_val_eq + rw [w0]; simp [Notif.tsCode] + have Ly := B.jvLay C.vm + have kV := B.kVn C.vm hw nV + have E' : JExtVcpu (L := L) V Z.th s' := + (C.extv.toV2u.of_eq (fun y hy => hSS.capV hy) hv kV).vcpu + have Jv' : JVm (L := L) V H s' σ' := + C.vm.step (fun _ h => h) (fun _ _ => rfl) hw.1 (fun u hu => by rw [hvk u hu]; exact C.vm.spec u hu) + (fun o ko' ho => Or.inl (hsz o ko' ho)) + have Vl' : VList st V Z.th s' := C.vl.writes hw fun b hb => by + refine ⟨C.vl.lw_al Ly.vfit hb, fun hW => ?_⟩ + rcases hb with rfl | ⟨u, hu, rfl | rfl⟩ + · exact hh _ hW rfl + · have := lay_next_val (Ly.vfit u hu).2 + rcases nV u hu _ hW with h | h <;> omega + · have := lay_prev_val (Ly.vfit u hu).2 + rcases nV u hu _ hW with h | h <;> omega + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := B.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact ⟨E', C.vo.frame hnv, + C.vr.frame (fun u _ vs hu => hnv _ vs hu) (v3_agreeW (L := L) hw (fun u hu => (Ly.vfit u hu).2) nV), + VirtRngOne.rngOne_congr C.rngOne fun z hz _ => by rw [hSS.capV (hsl z hz)]; exact NoA.refl _, + R', Jv', Vl', C.vb.ofV hvk kV, fun v hv => by + obtain ⟨y, hy, hc⟩ := C.vcap v hv + exact ⟨y, hy, (hSS.capV hy).trans hc⟩, + C.tcap.of_caps (fun v hv _ => ⟨hv, kV v hv⟩) fun y hy _ hc => ⟨y, hy, (hSS.capV hy).trans hc⟩⟩ + +/-- `EptCore.ofNat` from the end state's `XTopAt` alone. -/ +theorem EptCore.ofNatX (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (C : EptCore (L := L) X XL Z.th s σ) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (hX' : XTopAt (L := L) AR (Z.mv Z.G V') s' a' σ') (F : NatFoot (L := L) Z s σ s' σ') : + EptCore (L := L) X XL Z.th s' σ' := by + have hS := F.ss + have hat : σ'.archState.x64AsidTable = σ.archState.x64AsidTable := + B.asidWX hX' F.fw fun hi hh h => by + have hv := atA_val B.xtop.fin.1.2.vs.ksb_fit hh + rcases h.1 with hO | ⟨c, hc, hb⟩ + · exact B.xtop.fin.1.2.disj _ hO (Or.inr (Or.inr (Or.inr ⟨by omega, by omega⟩))) + · have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + omega + have hAo : ∀ p ao, σ'.kheap p = some (.archObj ao) → σ.kheap p = some (.archObj ao) := F.fk.2 + refine C.frame F.fw (fun b hb t ht => ?_) (fun y hy p l a v mm hc => ⟨hy, by rw [← hS.capV hy]; exact hc⟩) + (C.exte.of_eq fun y hy => hS.capV hy) (fun t ht => ?_) + (fun r l es hk => ⟨l, es, hAo _ _ hk⟩) + (fun e ⟨hi, p, f, lo, g1, g2, g3⟩ => ⟨hi, p, f, lo, by rw [← hat]; exact g1, hAo _ _ g2, g3⟩) + F.fk.1.2.1 (EptSlotStep.ofSS hS) ?_ + · rcases hb.1 with hO | ⟨c, hc, hb'⟩ + · exact B.oobjX C ht hO + · have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + rcases C.ks t ht with e | e <;> omega + · obtain ⟨es, hk, -⟩ := C.rel.obj t ht + exact F.fk.1.1 _ _ hk + · exact C.named.frameA hat + (fun _ _ _ ⟨p, f, h1, h2, h3⟩ _ => ⟨p, f, by rw [← hat]; exact h1, hAo _ _ h2, h3⟩) + (fun r l es h => hAo _ _ h) + (fun y p lv a v hy hc _ => ⟨y, hy, by rw [hS.capV hy]; exact hc⟩) + (fun y p lv mm a v hy hc => ⟨y, hy, by rw [← hS.capV hy]; exact hc⟩) + (fun _ _ _ _ _ _ y _ _ _ _ hy _ hl hc => ⟨y, _, _, _, hy, hl, by rw [hS.capV hy]; exact hc⟩) + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- `VmState` with `JAtZRB` as its base. -/ +abbrev VmStateR (V : Set U64) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ ∧ VcpuCore (L := L) V Z H s σ + +/-- `VmEptState` with `JAtZRB` as its base. -/ +abbrev VmEptStateR (V X : Set U64) (XL : U64 → Nat) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) + (Cs Ac : Set U64) (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + VmStateR (L := L) arI V AR Z H Cs Ac Lst D s a σ ∧ EptCore (L := L) X XL Z.th s σ + + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- A `VmEptState` is a `VmEptStateR`. -/ +theorem VmEptState.toRState (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ := + ⟨⟨J.1.1.toR, J.1.2⟩, J.2⟩ + +/-- **`VmEptState.ofNat` with an R target.** -/ +theorem VmEptState.ofNatR (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {V' : XVs} + (B' : JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ') (F : NatFoot (L := L) Z s σ s' σ') : + VmEptStateR (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D' s' a' σ' := + ⟨⟨B', J.1.2.ofBSSX J.1.1 B'.xtop B'.thT F.fw F.ss (fun u hu b hb => J.1.1.natV J.1.2.vm hb u hu) (fun b hb => hb.2.2) + (J.1.1.vkeep_of F.fw fun _ h => h.2.1) + (fun u hu => by obtain ⟨y, hy⟩ := J.1.2.vm.spec u hu; exact F.fk.1.1 _ _ hy) + F.fk.1.2.1 F.fk.1.2.2.1 F.fk.1.2.2.2⟩, + J.2.ofNatX J.1.1 B'.xtop F⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **Some `VmEptStateR`** (the R sibling of `VEAnyX`). -/ +def VEAnyR (AR : A → Fl4Spec.Cr3 → Prop) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + ∃ (V X : Set U64) (XL : U64 → Nat) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) + (D : Set U64), VxE Z ∧ VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ + + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +theorem VmEptStateR.any [VxE Z] (J : VmEptStateR (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + VEAnyR (L := L) arI AR s a σ := ⟨_, _, _, _, _, _, _, _, _, inferInstance, J⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmJAtZRBFin.lean b/proofs/Fl4Proofs/P3/VmJAtZRBFin.lean new file mode 100644 index 000000000..1972e0ca0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmJAtZRBFin.lean @@ -0,0 +1,5482 @@ +/- +P3 (line mmuAD, Stage 3 step 3, generated by `handoff/tools/gen_rclo2.py`): **the VM finish cone at `JAtZRB`**: the +`JAtZB` lemmas reached from VmPreempt / VmOracle / VmSignal / VmSchedule / VmFinish / VmCheck, renamed to `JAtZRB` +(proofs verbatim except the `.cur` readers). No named premise. +-/ +import Fl4Proofs.P3.BufAtJ +import Fl4Proofs.P3.DeadKeep +import Fl4Proofs.P3.IpcTcbJAll +import Fl4Proofs.P3.JAtZBDetach +import Fl4Proofs.P3.JProducers +import Fl4Proofs.P3.JointAt +import Fl4Proofs.P3.LiveFrame +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.ScDeadSch +import Fl4Proofs.P3.ScFireJ +import Fl4Proofs.P3.ScJoint +import Fl4Proofs.P3.ScJointSchFrame +import Fl4Proofs.P3.ScJointSchTail +import Fl4Proofs.P3.ScSignalBoundJ +import Fl4Proofs.P3.ScSignalDonJ +import Fl4Proofs.P3.ScSignalJ +import Fl4Proofs.P3.ScSignalJN +import Fl4Proofs.P3.VmJAtZRBRet +import Fl4Proofs.P3.VmOracle +import Fl4Proofs.P3.VmSchedule +import Fl4Proofs.P3.VmSwitchR +import Fl4Proofs.P3.XTopJoint +import Fl4Proofs.P3.XTopJointCopyRegs +import Fl4Proofs.P3.XTopJointFin +import Fl4Proofs.P3.XTopJointFinW +import Fl4Proofs.P3.XTopJointIrqZR +import Fl4Proofs.P3.XTopJointJAtZRS +import Fl4Proofs.P3.XTopJointMRPath +import Fl4Proofs.P3.XTopJointPP +import Fl4Proofs.P3.XTopJointRegFrame +import Fl4Proofs.P3.XTopJointReply +import Fl4Proofs.P3.XTopJointRest +import Fl4Proofs.P3.XTopJointStep +import Fl4Proofs.P3.XTopJointYield +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `FinW` word is not the VCPU-list head word** (moved here from P3/JAtZV3Sched in gen 42b46). -/ +theorem JAtZRB.finW_nh (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} + (h : Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ b) : b ≠ P1.vHeadA st s := by + rcases h with hk | ⟨y, hy, K, h1, h2, -, -, -, e⟩ + · exact J.tfoot_nh (Or.inl hk) + · intro eb + subst eb + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + have d := J.kern.hks y _ (J.zH.1 y hy) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71) **A badge-only signal is a native step** (IpcNtfnSend's `SingleRelI`: FL4's writes in `NWrite` +⊆ `FinW`, the notification rewritten in place). -/ +theorem JAtZRB.badgeN {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {n : U64} {x y : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) {s' : S} + (hrel : SingleRelI (st := st) Z.N Z.T Z.E Z.R Z.B s σ ({((), updK σ n.bv (.notification y))}, False) s') : + NatFoot (L := L) Z s σ s' (updK σ n.bv (.notification y)) := by + obtain ⟨-, -, -, -, hW, -⟩ := hrel + exact NatFoot.ofW J.xtop (hW.mono fun b hb => NWrite.finW J.xtop.fin.1.1.1.placed.tcb hb) + (fun _ hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) (fun _ hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) + (fun _ hb => J.finW_nh hb) (natK_ntfnUpd hk) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **Every TCB's receive buffer in `BufU`** (module docstring). -/ +theorem JAtZRB.bufOK {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {y : U64} (hy : y ∈ P1.Heap.tcbs H) : + msg.lookup_ipc_buffer st s true y ⦃ w => P1.BufOK (BufU Z σ) w ⦄ := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hth : Z.th y = some .tcb := (hJ.tcb y).2 hy + have hyT := J.thT y hth + obtain ⟨xd, hk, hws⟩ := J.buf y hyT + refine WP.spec_mono (lookup_buf_full s σ J.xtop.rel.2.1.1 J.xtop.rel.2.1.2.1 hth hk J.bok true) ?_ + rintro w ⟨h0, hp⟩ hw0 i hi + cases hb : Endp.ipcBufOf true xd with + | none => exact absurd (h0.2 hb) hw0 + | some p' => + have e := hp hws p' hb + subst e + exact ⟨y, hyT, xd, hk, _, hb, i, hi, rfl⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem JAtZRB.bufU_B {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : b ∈ BufU Z σ) : ∃ wp ∈ Z.B, wp.1 = b := by + obtain ⟨y, hy, xd, hk, rp, hrp, i, hi, rfl⟩ := hb + have hBC := bufCover_of_frameCover (T := Z.T) J.bok J.fcov + exact ⟨_, hBC y hy xd true rp hk hrp ⟨rp⟩ rfl i hi, rfl⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **A word of a TCB's IPC buffer at `J`** (for `JAtZ.userWrJ`): apart from +every non-`other` object of `H` (the TCB's IPC-frame slot holds a frame cap, +`StateRel.bufCap`, whose range meets no such object, `JPlaced.cobj`'s +`FrApartH`) and inside a spec object (the frame's data page, `IpcBufCapOK`). -/ +theorem JAtZRB.bufWordG (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hOK : Endp.IpcBufCapOK σ) {t : U64} + (ht : Z.th t = some .tcb) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) {q : Fl4Spec.ObjRef} + {isR : Bool} (hq : Endp.ipcBufOf isR x = some q) (k : Nat) (hk128 : k < 128) {b : U64} + (hb : b.bv = Endp.bufAddr q k) : + (∀ p k', H p = some k' → (∀ z, k' ≠ .other z) → b.val + 8 ≤ p.val ∨ p.val + 2 ^ k'.bits ≤ b.val) ∧ + ∃ o ko, σ.kheap o = some ko ∧ o.toNat ≤ b.val ∧ b.val < o.toNat + 2 ^ Fl4Spec.objBits ko := by + have hbv : b.val = (Endp.bufAddr q k).toNat := by rw [← hb]; rfl + unfold Endp.ipcBufOf at hq + split at hq + · rename_i dev pb R mt vms md he + split_ifs at hq with hc + cases hq + have hro : Fl4Spec.vmReadOnly ⊆ R := by + rcases hc with h | ⟨h, -⟩ + · exact fun z hz => h (by unfold Fl4Spec.vmReadOnly Fl4Spec.vmReadWrite at *; simp at hz ⊢; simp [hz]) + · exact h + obtain ⟨-, -, hd, ha, hpa⟩ := hOK t.bv x hk dev pb R mt vms md he hro + obtain ⟨r1, r2, r3⟩ := buf_frame_rng vms pb x.tcbIpcBuffer k hk128 hpa ha + rw [← hbv] at r1 r2 + refine ⟨?_, ?_⟩ + swap + · obtain ⟨sz', hsz'⟩ := hd k hk128 + have hn := Endp.and_not_mask_toNat (Endp.bufAddr (pb + (x.tcbIpcBuffer &&& + Fl4Spec.mask (Fl4Spec.pageBitsForSize vms))) k) _ (Endp.pageBits_mem sz') + have hpos : 0 < 2 ^ Fl4Spec.pageBitsForSize sz' := by positivity + have hlo := Nat.div_mul_le_self (Endp.bufAddr (pb + (x.tcbIpcBuffer &&& + Fl4Spec.mask (Fl4Spec.pageBitsForSize vms))) k).toNat (2 ^ Fl4Spec.pageBitsForSize sz') + have hhi := Nat.lt_div_mul_add (a := (Endp.bufAddr (pb + (x.tcbIpcBuffer &&& + Fl4Spec.mask (Fl4Spec.pageBitsForSize vms))) k).toNat) hpos + refine ⟨_, _, hsz', ?_, ?_⟩ + · rw [hn, hbv]; exact hlo + · show b.val < _ + 2 ^ Fl4Spec.pageBitsForSize sz' + rw [hn, hbv]; exact hhi + intro p k' hp hk' + obtain ⟨x', c, hkx, hcap, hcr⟩ := J.xtop.rel.2.1.1.bufCap ht + rw [hk] at hkx; cases hkx + rw [he] at hcr + obtain ⟨-, h2⟩ := hcr + cases c <;> try (simp [absCap] at h2; done) + case Frame p0 sz r0 d0 m0 ep0 as0 va0 => + simp only [absCap] at h2 + injection h2 with h2 + injection h2 with _ hp0 _ _ hsz _ + have hsl : IsSlot Z.th (slotOf t 2) := ⟨t, 2, Or.inr ⟨ht, by simp [tcbSlots]⟩, rfl⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hcv : capV (L.mem s) (slotOf t 2) = .Frame p0 sz r0 d0 m0 ep0 as0 va0 := capV_of_capAt hcap + have hFA : Sched.FrApartH H (.Frame p0 sz r0 d0 m0 ep0 as0 va0) := by + have := hJ.cobj _ hsl; rw [hcv] at this; exact this + obtain ⟨bs, ts⟩ := frame_range p0 sz r0 d0 m0 as0 va0 ep0 + have hm := Endp.pageBits_mem vms + have hbits : (fbitsF sz).val = Fl4Spec.pageBitsForSize vms := by rw [fbitsF_val, hsz] + have hmk : (maskF (fbitsF sz)).val = 2 ^ Fl4Spec.pageBitsForSize vms - 1 := by + rw [maskF_val (by rw [hbits]; simp at hm; omega), hbits] + have hpv : p0.val = pb.toNat := by rw [← hp0]; rfl + have hpos : 0 < 2 ^ Fl4Spec.pageBitsForSize vms := by positivity + have htop : topF (.Frame p0 sz r0 d0 m0 ep0 as0 va0) = p0.val + 2 ^ Fl4Spec.pageBitsForSize vms - 1 := by + rw [ts] + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, UScalarTy.U64_numBits_eq, U64.numBits, hmk] + omega + rcases hFA p k' hp hk' with e | e + · rw [bs] at e; right; omega + · rw [htop] at e; left; omega + · cases hq + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- The receiver's buffer words at `J` are untracked for P1 (`JAtZ.bufWord`, +`JAtZ`'s `nodeB`). -/ +theorem JAtZRB.rbufU (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {d : U64} + (hdT : d ∈ Z.T) (hdth : Z.th d = some .tcb) {xd : Fl4Spec.Tcb} (hkd : σ.kheap d.bv = some (.tcb xd)) : + ∀ b, RcvBufW xd b → P1.OutsideObjs st (P1.Heap.tcbs H) Cs s b ∧ + (∀ e, (H e = some .ep ∨ H e = some .ntfn) → b.val + 8 ≤ e.val ∨ e.val + 16 ≤ b.val) ∧ b ∉ P1.Heap.frame H := by + rintro b ⟨rp, hrp, i, hi, rfl⟩ + have hOK := J.bok + have hBC := bufCover_of_frameCover (T := Z.T) J.bok J.fcov + have hwB := hBC d hdT xd true rp hkd hrp ⟨rp⟩ rfl i hi + have hal := (J.xtop.fin.1.2.ep.balign _ hwB).1 + obtain ⟨hAp, -⟩ := J.bufWordG hOK hdth hkd hrp i.val hi (b := Endp.fl4BufA ⟨rp⟩ i) (by rw [fl4BufA_bv]) + refine ⟨⟨hal, fun t ht => ?_, fun c hc => ?_⟩, fun e he => ?_, fun hf => ?_⟩ + · rcases hAp t .tcb ht (fun _ e => by cases e) with e | e + · left; omega + · right; simpa [P1.HKind.bits] using e + · have e := J.nodeB _ hwB c (J.kern.csLt c hc) + dsimp only at e + rcases e with e | e <;> omega + · rcases he with he | he + · rcases hAp e _ he (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · rcases hAp e _ he (fun _ e => by cases e) with e | e + · left; omega + · right; simp only [P1.HKind.bits] at e; omega + · obtain ⟨-, p, k, hp, hk, hin⟩ := hf + have hko : ∀ z, k ≠ .other z := by + rcases hk with rfl | ⟨z, rfl⟩ | ⟨rfl, -⟩ <;> intro z e <;> cases e + obtain ⟨g1, g2⟩ := hin + rcases hAp p k hp hko with e | e <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A thread of `Z.T` is a TCB of the typed heap. -/ +theorem JAtZRB.thTcb (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) : + Z.th t = some .tcb := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact (hJ.tcb t).2 (J.zH.1 t ht) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem JAtZRB.bufU_rbuf {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : b ∈ BufU Z σ) : P1.OutsideObjs st (P1.Heap.tcbs H) Cs s b ∧ + (∀ e, (H e = some .ep ∨ H e = some .ntfn) → b.val + 8 ≤ e.val ∨ e.val + 16 ≤ b.val) ∧ + b ∉ P1.Heap.frame H := by + obtain ⟨y, hy, xd, hk, hb'⟩ := hb + exact J.rbufU hy (J.thTcb hy) hk b hb' + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **An IPC-buffer word is a user word** (`P1.UsrW`, int6): outside the +TCBs and nodes, the endpoints and notifications and the frame words +(`JAtZ.bufU_rbuf`), off the kernel-state block (`KsApart.buf`), in no SC. -/ +theorem JAtZRB.usrWJ {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : b ∈ BufU Z σ) : + P1.UsrW st (P1.Heap.tcbs H) Cs H (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) s b := by + obtain ⟨ho, he, hf⟩ := J.bufU_rbuf hb + refine ⟨ho, he, hf, fun o h1 h2 e => ?_, fun c hc hin => ?_⟩ + · obtain ⟨wp, hwp, e'⟩ := J.bufU_B hb + have h0 := J.ks.buf wp hwp + have hv : (core.num.U64.wrapping_add (L.ksb s) o).val = (L.ksb s).val + o.val := by + have := J.kern.ksbFit + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + rw [e', ← e, hv] at h0; omega + · obtain ⟨z, hz⟩ := hc + have hz' : P1.Heap.scBits H c = z := by simp [P1.Heap.scBits, hz] + rw [hz'] at hin + exact hf ⟨ho.1, c, .sc z, hz, Or.inr (Or.inl ⟨z, rfl⟩), hin⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The int6 core's placement carrier at `J`** (`P1.BufAt`): the IPC-buffer +words `BufU Z σ` are user words and every TCB's buffer is among them. -/ +theorem JAtZRB.bufAtJ {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + P1.BufAt st (P1.Heap.tcbs H) Cs H (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (BufU Z σ) s := + ⟨fun _ hb => J.usrWJ hb, fun _ hy => J.bufOK hy⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **No IPC-buffer word in a slot** (`BufPlaced.slot`). -/ +theorem JAtZRB.bufU_noSlot {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ x, IsSlot Z.th x → ∀ b ∈ BufU Z σ, ¬ (x.val ≤ b.val ∧ b.val < x.val + 32) := by + intro x hx b hb + obtain ⟨wp, hwp, e⟩ := J.bufU_B hb + subst e + exact BufPlaced.slot J.xtop.fin.1.2 hwp hx + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`cancel_yield_to` of a yielder at `J`** (P3/XTopJointYield's `JAtZ.cancelYieldJD` over `JAtZRB`; the post +also names the spec step's `ScTKw` and FL4's `TFoot` footprint). -/ +theorem JAtZRB.cancelYieldJD (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {y p : U64} (hy : y ∈ Z.T) + (hp : L.mem s (yieldA y) = p) (hp0 : p ≠ 0#u64) : + schedcontext.cancel_yield_to st s y ⦃ s' => ∃ σ', + Fl4Spec.schedContextCancelYieldTo (Z := Unit) y.bv σ = ({((), σ')}, False) ∧ + ScTKw (fun _ => False) σ σ' ∧ P1.WritesIn st s s' (TFoot st Z.T Z.N Z.C Z.c s σ) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a σ' ⦄ := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hT := hc.tcbs + have hy0 : y ≠ 0#u64 := fun e => (hc.tcbs y hy).ne0 (by rw [e]; rfl) + obtain ⟨xy, hky, eyt⟩ := J.yld.tcb y hy + obtain ⟨hpC, z, n, hkz, eyf⟩ := J.yld.toSc y hy (by rw [hp]; exact hp0) + rw [hp] at hpC hkz eyt + have hsw56 := sw_hdr hS hpC (K := 56#u64) (by simp) (by simp) (by decide) + have hal56 : Aligned (scW p 56#u64) := by + obtain ⟨x0, n0, -, hx0⟩ := hs.scs p hpC; exact (hx0.hdr (K := 56#u64) (by simp) (by simp)).1 + have hyT : y ∈ P1.Heap.tcbs H := by rw [← J.tcbs_eq.1]; exact hy + have hcode : schedcontext.cancel_yield_to st s y ⦃ s' => + s' = L.wr (L.wr s (scYieldFromA p) 0#u64) (yieldA y) 0#u64 ⦄ := by + unfold schedcontext.cancel_yield_to + step with obj.Tcb.is_null.spec as ⟨b2, hb2⟩ + have hb2' : b2 = false := by rw [hb2]; exact decide_eq_false hy0 + rw [hb2'] + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.yield_to.spec as ⟨i2, hi2⟩ + rw [hp] at hi2 + rw [hi2] + have hb' : (p != 0#u64) = true := by simp [hp0] + simp only [hb', ↓reduceIte] + exact WP.spec_bind (obj.Sc.set_yield_from.spec s _ 0#u64) fun s3 h3 => h3 ▸ obj.Tcb.set_yield_to.spec _ y 0#u64 + refine WP.spec_mono (WP.spec_and (P1.schedcontext.cancel_yield_to.blk s y J.blk hyT) hcode) ?_ + rintro s' ⟨hP, rfl⟩ + obtain ⟨hS3, hf3, hK3, hw3⟩ := hS.set_sc_yf hpC hkz 0#u64 + rw [Notif.optRef_zero] at hS3 hK3 + have hpy : p.bv ≠ y.bv := fun e => by rw [e, hky] at hkz; cases hkz + have hkay : (updSc σ p.bv { z with scYieldFrom := none } n).kheap y.bv = some (.tcb xy) := by + simp [updK, Ne.symm hpy, hky] + obtain ⟨hS4, hf4, hK4⟩ := hS3.set_yt hy hkay 0#u64 + rw [Notif.optRef_zero] at hS4 hK4 + have hm4 : ∀ u ∈ Z.T, u ≠ y → L.mem (L.wr (L.wr s (scYieldFromA p) 0#u64) (yieldA y) 0#u64) (yieldA u) = + L.mem s (yieldA u) := by + intro u hu hne + rw [L.mem_wr_ne (yieldA_aligned hT hy) (yieldA_aligned hT hu) + (fun e => hne (yieldA_inj hT hy hu (UScalar.eq_of_val_eq e.symm)))] + exact WritesIn.yield_keep hT hw3 u hu + have hmy : L.mem (L.wr (L.wr s (scYieldFromA p) 0#u64) (yieldA y) 0#u64) (yieldA y) = 0#u64 := + L.mem_wr_same (yieldA_aligned hT hy) + have hY' := J.yld.clear hy hp hp0 hky hkz hm4 hmy + have hN' : NSC Z.N Z.C (updK (updSc σ p.bv { z with scYieldFrom := none } n) y.bv + (.tcb { xy with tcbYieldTo := none })) := J.nsc.updSc.updTcb + have hI' := (J.inv.scFields (y' := { z with scYieldFrom := none }) hkz rfl rfl rfl rfl).ytUpd hkay none + have hR' := (hR.frame hS.placed hf3 hK3).frame hS3.placed hf4 hK4 + have he := cancelYieldTo_some hky (by rw [eyt, Notif.optRef_ne hp0]) hkz hpy + have hm : ((), updK (updSc σ p.bv { z with scYieldFrom := none } n) y.bv (.tcb { xy with tcbYieldTo := none })) ∈ + (Fl4Spec.schedContextCancelYieldTo (Z := Unit) y.bv σ).1 := by rw [he]; exact Set.mem_singleton _ + have hK : ScTKw (fun _ => False) σ _ := nkt_schedContextCancelYieldTo y.bv σ _ hm + have hA : QKY σ (updK (updSc σ p.bv { z with scYieldFrom := none } n) y.bv (.tcb { xy with tcbYieldTo := none })) := + QKY.upd2 hkz hky rfl rfl + have hW : P1.WritesIn st s (L.wr (L.wr s (scYieldFromA p) 0#u64) (yieldA y) 0#u64) (TFoot st Z.T Z.N Z.C Z.c s σ) := + ((writesIn_wr (st := st) (s := s) (v := 0#u64) + (show Aligned (scYieldFromA p) by rw [scYieldFromA_eq]; exact hal56)).trans + (writesIn_wr (st := st) (s := L.wr s (scYieldFromA p) 0#u64) (v := 0#u64) (yieldA_aligned hT hy))).mono + fun b hb => by + rcases hb with rfl | rfl + · exact Or.inl (KFoot.of_sw (by rw [scYieldFromA_eq]; exact hsw56)) + · exact Or.inr ⟨y, hy, rfl⟩ + have hX' : XTopAt (L := L) AR Z _ a _ := + xtop_sc_step (ct := σ.curThread) J.xtop J.ip J.nk hW (hK.mono fun _ h => h.elim) hS4 hR' id + have hall := ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hS4, hR', hN', hI'⟩ hY' (hW.mono fun b hb => Or.inl hb) + ((pres_schedContextCancelYieldTo y.bv).of_eq he) rfl + obtain ⟨-, -, hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := hall + have hAI := hA.actIdle J.act + have hYD : ∀ r, YDeadKeep σ (updK (updSc σ p.bv { z with scYieldFrom := none } n) y.bv + (.tcb { xy with tcbYieldTo := none })) r := fun r => + YDeadKeep.upd (p := y.bv) (o := .tcb { xy with tcbYieldTo := none }) + (YDeadKeep.upd (p := p.bv) (o := .schedContext { z with scYieldFrom := none } n) (YDeadKeep.of_at rfl) + (fun _ e => by cases e) (fun _ _ e => by cases e; rfl)) + (fun _ e => by cases e; rfl) (fun _ _ e => by cases e) + have hdead : ∀ q ∈ D, DeadP (L := L) H (L.wr (L.wr s (scYieldFromA p) 0#u64) (yieldA y) 0#u64) q := fun q hq => + J.deadP_sc_step hX' hW hK (fun _ _ h => h.elim) (hA.lk (W := fun _ => False)) hA.fk hAI.1 hY' (hYD q.bv) + (fun h => h) (J.jcov.dead q hq) + have hDbg := J.kw_dbg (hP.2.1.mono (Set.empty_subset _)) + have hKd := J.kw_dbgKeep (hP.2.1.mono (Set.empty_subset _)) + exact ⟨_, he, hK, hW, Ac, Lst', J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hA.fk.2.1 hA.fk.2.2).2) (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hA.fk.2.1 hA.fk.2.2) hKd subset_rfl) (J.sc_kern hW hK hA.fk hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hN' hI' (fun e => hA.curInv (J.cur (by rw [← hA.act]; exact e))) (hA.own J.own) (hA.off J.off) hAI (hA.bsa J.bsa) + (hA.curRunM J.curRun) hP.1 hKd (J.idleW_keep hX'.fin.1.1.1 hA.fk.2.1 hA.fk.2.2).1 J.zD⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- `NatFoot.trans` from a `JAtZRB` start (its SCs, `SchedRel`). -/ +theorem JAtZRB.natTrans (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s1 s2 : S} {σ1 σ2 : Fl4Spec.State Unit} + (F1 : NatFoot (L := L) Z s σ s1 σ1) (F2 : NatFoot (L := L) Z s1 σ1 s2 σ2) : NatFoot (L := L) Z s σ s2 σ2 := + F1.trans (fun q hq => by + obtain ⟨Q, R0, -, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨y, n, hk, -⟩ := hs.scs q hq + exact ⟨y, n, hk⟩) F2 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- `JAtZ.bok`/`fcov`/`fcap` across an `IpcFrKeep` step (`B` kept) whose +slots step by `VsSlotsT`. -/ +theorem JAtZRB.bfK {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} (hfk : IpcFrKeep σ σ') + (hT : Vs.VsSlotsT Z.th (L.mem s) (L.mem s')) : JBuf (L := L) Z s' σ' := + ⟨J.bok.keep hfk, J.fcov.ipcFr hfk, J.fcap.keepU hT hfk.page⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 1000000 in +/-- **`JAtZ` across a CSpace-shaped step** (module docstring). -/ +theorem JAtZRB.cs_frameVD {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : Set U64 × Set U64 × Set U64 × Set U64 × Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' V') s' a' σ') (hkb : KBack σ σ') (hbf : JBuf (L := L) Z s' σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hvc : Vs.VsSlotsW Z.th V'.1 V'.2.1 V'.2.2.1 V'.2.2.2.1 V'.2.2.2.2 (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hcy : JCarryS Z.B H σ') (hdv : IdleH (L := L) H s' → DevApart (L := L) Z.th H G' Cs s' ∧ DevSep G' (L.mem s')) + (hvy : VsTyp (L := L) (Z.mv G' V') s' σ') (htb : DevTab G' (L.mem s') σ') + (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR (Z.mv G' V') H Cs Ac' Lst' D' s' a' σ' := + have hsl : Sched.ScLinkHyp σ' := scLink_rebuild hX J.tcbs_eq.1 (J.obj.kback hkb) R.blk J.hset.2.1 R.jpl J.c0 + ⟨hX, R.call, R.rfree, R.rrecv, R.yld, R.mcp, R.buf, by rw [hks]; exact J.ks, J.thT, J.thk, J.ip, R.rp, R.apart, + fun n hn => by rw [hks]; exact J.nk n hn, + fun wp hw c hc => by simp only [P1.LawfulStore.nodeBase, hks]; exact J.nodeB wp hw c hc, R.jpl, J.cover.kback hkb, J.obj.kback hkb, J.zH, hC.sfree, J.hset, J.hscC, R.jcov, + R.kern, hC.del, hC.ut, J.bound, hC.finM, R.utc, R.nsc, R.inv, R.cur, R.own, R.off, R.act, J.c0, R.bsa, + schedValid_rebuild hX J.tcbs_eq.1 J.tcbs_eq.2 (J.obj.kback hkb) R.blk J.hset.1 J.hset.2.1 R.call R.yld J.c0 R.inv + hsl, R.isorted, hsl, R.curRun, R.blk, by rw [hks]; exact J.intL, hvc, hbf.1, hbf.2.1, hbf.2.2, hlv, hzD, + hcy.withP (hdv (idleH_of hX (J.obj.kback hkb) J.zH.1 R.jpl R.inv R.act)) hvy htb hvn⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- `cs_frameVD` from the untyped caps' `DevFrom`/`UtFrom`. -/ +theorem JAtZRB.cs_frameV {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : Set U64 × Set U64 × Set U64 × Set U64 × Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' V') s' a' σ') (hkb : KBack σ σ') (hbf : JBuf (L := L) Z s' σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hvc : Vs.VsSlotsW Z.th V'.1 V'.2.1 V'.2.2.1 V'.2.2.2.1 V'.2.2.2.2 (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hcy : JCarryS Z.B H σ') (hdu : DevFrom Z.G G' (L.mem s) (L.mem s') ∧ Sched.UtFrom Z.G G' (L.mem s) (L.mem s')) + (hvy : VsTyp (L := L) (Z.mv G' V') s' σ') (hAF : ArchFwd σ σ') (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR (Z.mv G' V') H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameVD (hvn := hvn) hks hX hkb hbf hC R hvc hlv hzD hcy (fun hI => J.carry.devStep hks hdu hI) hvy + (J.carry.devTab.frame hdu.1.sub (tabBack_of hkb hAF)) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`cs_frameV` with the VSpace sets put back** (`XTopAt` at `Z`'s sets +after; the CSpace fields and `JRestR` from a post at any sets `V'`, which they +do not mention). -/ +theorem JAtZRB.cs_frameB {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : XVs} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' Z.vs) s' a' σ') (hkb : KBack σ σ') (hbf : JBuf (L := L) Z s' σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hvc : Vs.VsSlotsW Z.th Z.P Z.L4 Z.L3 Z.L2 Z.L1 (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hcy : JCarryS Z.B H σ') (hdu : DevFrom Z.G G' (L.mem s) (L.mem s') ∧ Sched.UtFrom Z.G G' (L.mem s) (L.mem s')) + (hvy : VsTyp (L := L) (Z.mv G' Z.vs) s' σ') (hAF : ArchFwd σ σ') (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameV (hvn := hvn) hks hX hkb hbf ⟨hC.del, hC.ut, hC.finM, hC.sfree⟩ + ⟨R.call, R.rfree, R.rrecv, R.yld, R.mcp, R.buf, R.rp, R.apart, R.jpl, R.jcov, R.kern, R.utc, R.nsc, R.inv, + R.cur, R.own, R.off, R.act, R.bsa, R.isorted, R.curRun, R.blk⟩ hvc hlv hzD hcy hdu hvy hAF + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`cs_frameV` after IPC work** (`IRel` after, VSpace sets `V'`): the +sets put back to `Z`'s (`XTopAt.vs_backO`: FL4 wrote only `OObj` words, the +spec kept the arch state, arch objects and SC sizes) and the slots kept +(`SlotsSame`, so `JAtZ.vsc` carries over). -/ +theorem JAtZRB.cs_frameI {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {V' : XVs} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR (Z.mv G' V') s' a' σ') {W : U64 → Prop} (hW : P1.WritesIn st s s' W) + (hWO : ∀ b, W b → Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) (hWV : ∀ b, W b → ∀ t ∈ Z.T, b ≠ tVcpuA t) + (har : σ'.archState = σ.archState) + (hao : ∀ q ao, σ.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) (hsz : Sched.SzKeep σ σ') + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hkb : KBack σ σ') (hfk : IpcFrKeep σ σ') + (hC : JCs (L := L) (Z.mv G' V') s' σ') (R : JRestR (L := L) arI (Z.mv G' V') H Cs Ac' Lst' D' s' σ') + (hK : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hidl : L.idleW s' = L.idleW s) (hDD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D') : + JAtZRB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameB (hvn := (J.vsW hW hWO)) hks (J.xtop.vs_backO hX hW hWO har hao hsz) hkb (J.bfK hfk (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy)) + hC R (J.vsc.of_eq fun y hy => hSS.capV hy) + (J.liveX hX rfl rfl rfl hSS hK horig hidl fun p hp => R.jcov.dead p (hDD p hp)) (J.zDX hSS hDD) (J.carry.of_ao hkb hao) + (devFrom_same J.xtop.rel.2.1.2.2.1 hX.rel.2.1.2.2.1 hSS) (J.carry.vsSame hSS hks (ArchFwd.of_eq hao)) + (ArchFwd.of_eq hao) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead** (module docstring). -/ +theorem JAtZRB.deadP_keep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hS' : Sched.SchedRel st Z.N Z.T Z.C Z.c s' σ') (hI' : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s') σ') + {q : U64} + (hT : ∀ x, σ.kheap q.bv = some (.tcb x) → tsCode x.tcbState = 0 → ∃ y, σ'.kheap q.bv = some (.tcb y) ∧ + y.tcbState = x.tcbState ∧ y.tcbBoundNotification = x.tcbBoundNotification ∧ + (y.tcbSchedContext = x.tcbSchedContext ∨ y.tcbSchedContext = none)) + (hE : ∀ x, σ.kheap q.bv = some (.endpoint x) → σ'.kheap q.bv = some (.endpoint x)) + (hN : ∀ x, σ.kheap q.bv = some (.notification x) → ∃ y, σ'.kheap q.bv = some (.notification y) ∧ + (y.ntfnObj = x.ntfnObj ∨ ∀ l, y.ntfnObj ≠ .waitingNtfn l) ∧ y.ntfnBoundTcb = x.ntfnBoundTcb ∧ + (y.ntfnSc = x.ntfnSc ∨ y.ntfnSc = none)) + (hRp : ∀ x, σ.kheap q.bv = some (.reply x) → ∃ y, σ'.kheap q.bv = some (.reply y) ∧ y.replyTcb = x.replyTcb) + (hSc : ∀ x n, σ.kheap q.bv = some (.schedContext x n) → ∃ y, σ'.kheap q.bv = some (.schedContext y n) ∧ + (y.scTcb = x.scTcb ∨ y.scTcb = none) ∧ (y.scNtfn = x.scNtfn ∨ y.scNtfn = none)) + (hRW : q ∈ Z.C → L.mem s' (P1.scReplyA q) = L.mem s (P1.scReplyA q)) + (hOut : ∀ z, H q = some (.sc z) → q ∉ Z.C → ∀ K : U64, K.val % 8 = 0 → K.val < 128 → + L.mem s' (scW q K) = L.mem s (scW q K)) + (hidle : σ'.idleThread = σ.idleThread) (hAS : ActSched σ') + (hY' : YieldRel Z.T Z.C (L.mem s') σ') (hYD : YDeadKeep σ σ' q.bv) + (hd : DeadP (L := L) H s q) : DeadP (L := L) H s' q := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + have hI := hX.ipc + refine ⟨fun hq => ?_, fun hq => ?_, fun hq => ?_, fun hq => ?_, fun z hq => ?_⟩ + · -- a TCB + obtain ⟨d1, d2, d3, d4, d5, d6⟩ := hd.1 hq + have hqT : q ∈ Z.T := by rw [J.tcbs_eq.1]; exact hq + obtain ⟨x, hk, hR⟩ := hI.tcb q hqT + obtain ⟨x', hk', hR'⟩ := hI'.tcb q hqT + have hst0 : tsCode x.tcbState = 0 := by + have := hR.st; rw [← stA_tStA, d1] at this; unfold sched.TS_INACTIVE at this; simpa using this.symm + obtain ⟨y, hy, hstate, hso, e⟩ := hT x hk hst0 + rw [hk'] at hy; cases hy + have hxs : x.tcbSchedContext = none := by rw [hR.sc, d2, optRef_zero] + have hxs' : x'.tcbSchedContext = none := by + rcases e with e | e + · rw [e, hxs] + · exact e + refine ⟨?_, ?_, ?_, ?_, ?_, J.deadY_tcb hY' hYD hqT d6⟩ + · have h1 : (L.mem s' (tStA q)).val = (L.mem s (tStA q)).val := by rw [hR'.st, hR.st, hstate] + rw [stA_tStA, UScalar.eq_of_val_eq h1, ← stA_tStA, d1] + · exact optRef_eq_none (by rw [← hR'.sc, hxs']) + · have hb : x'.tcbBoundNotification = x.tcbBoundNotification := hso + have h1 := hR.bound + rw [show tBoundA q = P1.boundA q from rfl, d3, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.boundA q = tBoundA q from rfl, ← hR'.bound, hb, h1]) + · intro e + have hact := hS'.act + have ea : L.actionW s' = nodeW st s' Z.c layout.node.SCHED_ACTION := by + unfold P1.LawfulStore.actionW nodeW; rw [hc'.cur] + rw [← ea, ← e] at hact + revert hact + cases hsa : σ'.schedulerAction with + | resumeCurThread => + intro h; simp only [ActRel] at h; subst h + rw [J.kern.h0] at hq; cases hq + | chooseNewThread => + intro h; simp only [ActRel] at h; subst h + have := J.kern.heap.align _ _ hq + simp [P1.HKind.bits] at this + | switchThread t => + intro h; simp only [ActRel] at h + obtain ⟨-, -, ht⟩ := h + obtain ⟨x2, scp, -, -, hk2, hsc2, -⟩ := schedulable_inv (hAS t hsa) + rw [← ht, hk'] at hk2; cases hk2 + rw [hxs'] at hsc2; cases hsc2 + · have ei : L.idleW s' = L.idleW s := by + unfold P1.LawfulStore.idleW + apply P1.eq_of_bv + have h1 := hs'.idleTh; have h2 := hs.idleTh + unfold nodeW at h1 h2 + rw [hc'.cur, hc.cur, ← h1, ← h2, hidle] + rw [ei]; exact d5 + · -- an endpoint + have hqE : q ∈ Z.E := J.hset.1 q hq + obtain ⟨x, hk, hR⟩ := hI.ep q hqE + obtain ⟨x', hk', hR'⟩ := hI'.ep q hqE + rw [hE x hk] at hk'; cases hk' + rw [epState_eq, stOf_ep hR hR', ← epState_eq]; exact hd.2.1 hq + · -- a notification + obtain ⟨d1, d2, d3⟩ := hd.2.2.1 hq + have hqN : q ∈ Z.N := J.hset.2.1 q hq + obtain ⟨x, hk, hR⟩ := hI.ntfn q hqN + obtain ⟨x', hk', hR'⟩ := hI'.ntfn q hqN + obtain ⟨y, hy, ho, hb, e⟩ := hN x hk + rw [hk'] at hy; cases hy + have hxs : x.ntfnSc = none := by rw [hR.sc, show nScA q = P1.ntfnScA q from rfl, d3, optRef_zero] + have hxs' : x'.ntfnSc = none := by + rcases e with e | e + · rw [e, hxs] + · exact e + refine ⟨?_, ?_, ?_⟩ + · rw [epState_eq] + have hO' := hR'.obj + rcases ho with ho | ho + · rw [ho] at hO' + rw [stOf_ntfn hR.obj hO', ← epState_eq]; exact d1 + · revert hO' + cases hx' : x'.ntfnObj with + | waitingNtfn l => exact absurd hx' (ho l) + | idleNtfn => intro hO'; rw [hO'.1]; decide + | activeNtfn b => intro hO'; rw [hO'.1]; decide + · have h1 := hR.bound + rw [show nBoundA q = P1.ntfnBtA q from rfl, d2, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.ntfnBtA q = nBoundA q from rfl, ← hR'.bound, hb, h1]) + · exact optRef_eq_none (by rw [show P1.ntfnScA q = nScA q from rfl, ← hR'.sc, hxs']) + · -- a reply + have hqR : q ∈ Z.R := J.hset.2.2 q hq + obtain ⟨x, hk, hR⟩ := hI.reply q hqR + obtain ⟨x', hk', hR'⟩ := hI'.reply q hqR + obtain ⟨y, hy, e⟩ := hRp x hk + rw [hk'] at hy; cases hy + have h1 := hR + rw [show Endp.rTcbA q = P1.rTcbA q from rfl, hd.2.2.2.1 hq, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.rTcbA q = Endp.rTcbA q from rfl, ← hR', e, h1]) + · -- an SC + obtain ⟨d1, d2, d3, d4⟩ := hd.2.2.2.2 z hq + by_cases hqC : q ∈ Z.C + · obtain ⟨x, n, hk, hR⟩ := hs.scs q hqC + obtain ⟨x', n', hk', hR'⟩ := hs'.scs q hqC + obtain ⟨y, hy, e1, e2⟩ := hSc x n hk + rw [hk'] at hy; cases hy + have hxt : x.scTcb = none := by rw [hR.tcb, scW_tcb, d1, optRef_zero] + have hxn : x.scNtfn = none := by rw [hR.ntfn, scW_ntfn, d3, optRef_zero] + refine ⟨?_, ?_, ?_, ?_⟩ + · rw [← scW_tcb]; refine optRef_eq_none ?_; rw [← hR'.tcb] + rcases e1 with e | e + · rw [e, hxt] + · exact e + · rw [hRW hqC]; exact d2 + · rw [← scW_ntfn]; refine optRef_eq_none ?_; rw [← hR'.ntfn] + rcases e2 with e | e + · rw [e, hxn] + · exact e + · rw [scYieldFromA_eq]; refine optRef_eq_none ?_; rw [← hR'.yieldFrom] + exact hYD.2 x _ hk (by rw [hR.yieldFrom, ← scYieldFromA_eq, d4, optRef_zero]) x' _ hk' + · refine ⟨?_, ?_, ?_, ?_⟩ + · rw [← scW_tcb, hOut z hq hqC _ (by decide) (by decide), scW_tcb]; exact d1 + · rw [scReplyA_eq', hOut z hq hqC _ (by decide) (by decide), ← scReplyA_eq']; exact d2 + · rw [← scW_ntfn, hOut z hq hqC _ (by decide) (by decide), scW_ntfn]; exact d3 + · rw [scYieldFromA_eq, hOut z hq hqC _ (by decide) (by decide), ← scYieldFromA_eq]; exact d4 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The header words of `H`'s SCs outside `Z.C` are kept** by a +finalisation (`sc_out_keep`'s argument). -/ +theorem JAtZRB.finW_scOut {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ)) {q : U64} {z : Nat} + (hq : H q = some (.sc z)) (hqC : q ∉ Z.C) {K : U64} (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + L.mem s' (scW q K) = L.mem s (scW q K) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hS.lists + have hH := J.kern.heap + have hal := hH.align q _ hq + have hfit := hH.fits q _ hq + have hz := hH.scmin q z hq + simp only [P1.HKind.bits] at hal hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + refine hw.mem_eq (by show (scW q K).val % 8 = 0; rw [hv]; omega) fun hb => ?_ + rcases finW_cases hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hHy : H y = some .tcb := J.zH.1 y hy + have hne : q ≠ y := fun e => by rw [e, hHy] at hq; cases hq + have := hH.disj q _ y _ hq hHy hne + simp only [P1.HKind.bits] at this + omega + · have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have := J.kern.hnd q _ hq Z.c hcC + simp only [P1.HKind.bits] at this + omega + · have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + have hne : q ≠ n := fun e => by rw [e, hHn] at hq; cases hq + have := hH.disj q _ n _ hq hHn hne + simp only [P1.HKind.bits] at this + omega + · have hHs := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have hne : q ≠ sc := fun e => hqC (e ▸ hsc) + have := hH.disj q _ sc _ hq hHs hne + simp only [P1.HKind.bits] at this + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The dead stay dead across a quiet step** (`deadP_keep`). -/ +theorem JAtZRB.deadP_qk (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s3 : S} {σ3 : Fl4Spec.State Unit} + (hA : QKY σ σ3) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx _ => ?_) + (fun x hx => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 hx) + (fun x hx => ⟨x, (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 hx, rfl⟩) + (fun x n hx => ?_) (fun hqC => k3 q hqC) + (fun z hz hqC K hK8 hK => J.finW_scOut hw hz hqC hK8 hK) hA.idle hAS hY3 (hYK.dk q.bv) hd + · obtain ⟨y, hy, e⟩ := hA.tcb hx + exact ⟨y, hy, by rw [e], by rw [e], Or.inl (by rw [e])⟩ + · obtain ⟨w, hw, e⟩ := hA.sc hx + exact ⟨w, hw, Or.inl (by rw [e]), Or.inl (by rw [e])⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The IRQ node's caps are kept** by a finalisation (its words lie in the +kernel-state block; the TCBs, notifications and SCs outside it, the node +after it). -/ +theorem JAtZRB.finW_irq {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ)) (i : U64) (hi : i.val < 128) : + capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64 := J.kern.ksbFit + refine irqNode_keep hw hkf J.kern.ksbAl (fun b hb => ?_) i hi + rcases finW_cases hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have := hJ.ks y _ (J.zH.1 y hy) (fun _ e => by cases e); simp only [P1.HKind.bits] at this; omega + · simp only [P1.LawfulStore.nodeBase] at g1; omega + · have := J.nk n hn; omega + · have := (hS.scOut sc hsc x n hk b ⟨g1, g2⟩).2.2.2; omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The frame words a finalisation keeps**: the TCBs' `MCP`, `YIELD_TO` and +IPC-buffer words, the SCs' `REPLY` words, the replies' links. -/ +theorem JAtZRB.finW_keeps {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ)) : + (∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (tIpcBufA t) = L.mem s (tIpcBufA t)) ∧ + (∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q)) ∧ + (∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r)) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hT := hc.tcbs + have tsw : ∀ b, Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ b → TSW Z.T Z.C b := fun b hb => Ipc.FinW.tsw hS hb + refine ⟨fun t ht => ⟨hw.mem_eq (mcpA_aligned hT ht) fun hb => ((tsw _ hb).1 t ht).1 (mcpA_val hT ht), + hw.mem_eq (flagsA_aligned hT ht) fun hb => Ipc.FinW.nf hS hb t ht rfl⟩, + fun t ht => hw.mem_eq (yieldA_aligned hT ht) fun hb => ((tsw _ hb).1 t ht).2.1 (yieldA_val hT ht), + fun t ht => hw.mem_eq (tIpcBufA_aligned (hT t ht)) fun hb => ((tsw _ hb).1 t ht).2.2 (tIpcBufA_val (hT t ht)), + fun q hq => hw.mem_eq (sc_reply_word hS hq).1 fun hb => (tsw _ hb).2 q hq rfl, fun r hr => ?_⟩ + have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + hw.mem_eq (J.rp.aligned hr ha) fun hb => by + rcases hb with hb | ⟨y, hy, K, -, hK, -, -, -, e⟩ + · exact hb.ne_rword hS J.rp hr ha rfl + · have := J.rp.tcb r hr y hy; omega + exact ⟨k _ (Or.inl h8), k _ (Or.inr h16)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 1000000 in +/-- **The untyped caps' facts after a finalisation**: the caps kept (the slots +same), the free parts zero (no `FinW` word there: the TCBs are typed, the +notifications and SCs are spec objects outside it, `SpecFree`, the node is +outside every untyped cap, `JCovD`), the rest by the kept kernel state, CDT, +original bits and the node-0 `DEBUG_TCBS` word (`hDbg`). -/ +theorem JAtZRB.finW_utc {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ)) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (harch : σ'.archState = σ.archState) + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hfi := U.fiLe + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [hcdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [harch]; exact U.gReg (B g)⟩ + · intro g w hal h1 h2 + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases finW_cases hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hth : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y .tcb hth + simp only [objSize] at this; omega + · have := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).2.1 Z.c hcC + omega + · obtain ⟨x, hx, -⟩ := hR.ntfn n hn + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) n.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.ntfnBits, UScalar.bv_toNat] at this + omega + · have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) sc.bv _ hk + simp only [Fl4Spec.objBits, UScalar.bv_toNat] at this + rw [Nat.add_comm Fl4Spec.minSchedContextBits n] at this + omega + · intro g h0 hf0 + rw [hcdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← horig]; exact h), by rw [hna, hDbg]; exact r3⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`JCovD` across a step keeping every slot and the dead dead** +(`JCovD.same`, the MDB kept). -/ +theorem JAtZRB.jcov_keep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hks : L.ksb s' = L.ksb s) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hdead : ∀ q ∈ D, DeadP (L := L) H s' q) + (hidl : Sched.IdleKeep (L := L) s s') (hDX : Sched.DXKeep (L := L) H Cs s s') : + JCovD (L := L) Z.th H Z.G D Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + refine J.jcov.same (fun y hy _ _ => ⟨hy, hSS.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hSS.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hSS.capV (hsl x hx)]; exact hc⟩) + (fun q _ h => h.keep hDX) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`JKern` across a step** keeping the kernel-state base, the core, the +current thread and SC, the original bits (new ones only at `AbsSlot`s), the +global PML4 and the arch +objects (ScJointRest's `sc_kern` for any write set). -/ +theorem JAtZRB.kern_keep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + {D' : Set U64} + (hks : L.ksb s' = L.ksb s) (hcpu : L.cpu s' = L.cpu s) + (hS' : Sched.SchedRel st Z.N Z.T Z.C Z.c s' σ') + (hct : σ'.curThread = σ.curThread) (hcsc : σ'.curSc = σ.curSc) + (horig : ∀ sl, σ'.isOriginalCap sl = true → σ.isOriginalCap sl = true ∨ AbsSlot Z.th sl) + (hgp : (σ'.archState.x64GlobalPml4, σ'.archState.x64Plat) = (σ.archState.x64GlobalPml4, σ.archState.x64Plat)) + (hao : ∀ p ao, σ.kheap p = some (.archObj ao) → σ'.kheap p = some (.archObj ao)) + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + JKern (L := L) arI H Cs D' Z.th s' σ' := by + have hpl : σ'.archState.x64Plat = σ.archState.x64Plat := congrArg Prod.snd hgp + replace hgp : σ'.archState.x64GlobalPml4 = σ.archState.x64GlobalPml4 := congrArg Prod.fst hgp + have K := J.kern + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + have ecw : L.curW s' = L.curW s := by + apply P1.eq_of_bv + have h1 := hs'.curThread; have h2 := hs.curThread + unfold P1.LawfulStore.curW + rw [hcpu, hna, hc.cur] + unfold nodeW at h1 h2 + rw [hna] at h1 + rw [← h1, ← h2, hct] + have ecs : L.cscW s' = L.cscW s := by + apply P1.eq_of_bv + have h1 := hs'.curSc; have h2 := hs.curSc + unfold P1.LawfulStore.cscW P1.LawfulStore.nodeA + rw [hcpu, hna, hc.cur] + unfold nodeW at h1 h2 + rw [hna] at h1 + rw [← h1, ← h2, hcsc] + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp only [P1.LawfulStore.nodeBase, hks] + obtain ⟨pt, hpt⟩ := K.gpm + refine ⟨fun sl h => (horig sl h).elim (K.orig sl) id, by rw [ecw, hct]; exact K.cur, + by rw [ecw]; exact K.curT, by rw [hcpu, hna]; exact K.act, by rw [hna]; exact K.dwAl, + by rw [hna]; exact K.dwObj, by rw [hna, hna, hcpu, ecw]; exact K.dwNe, ?_, + ⟨pt, by rw [hgp]; exact hao _ _ hpt⟩, by rw [hgp]; exact K.ga, by rw [hgp]; exact K.gf, + K.heap, K.h0, by rw [hks]; exact K.hks, by rw [hks]; exact K.hksIn, fun p k hp c hc => by rw [hnb]; exact K.hnd p k hp c hc, + by rw [hks]; exact K.ksbFit, by rw [hks]; exact K.ksbAl, by rw [hks, hcpu]; exact K.nodeFit, + by rw [hcpu]; exact K.cpuCs, by rw [ecw]; exact K.curH, by rw [ecs]; exact K.cscH, + K.csLt, K.cs0, K.csN, K.tdx.frame hpl⟩ + rw [hna, hDbg]; exact K.head + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`TdKeep` from a `WritesIn` footprint missing every TCB's deadline word.** -/ +theorem JAtZRB.tdkeep_of (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {W : U64 → Prop} + (hW : P1.WritesIn st s s' W) (hWV : ∀ b, W b → ∀ t ∈ Z.T, b ≠ P1.tdA t) : Sched.TdKeep (L := L) H s s' := + Sched.TdKeep.of_writes J.ip.e.tcb (fun _ ht => J.tcbZ ht) hW hWV + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The CSpace fields across a step keeping every slot and the objects' +sizes** (module docstring). -/ +theorem JCs.sameBR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hk : KSz σ σ') : JCs (L := L) Z s' σ' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + refine ⟨J.del.of_same hSS, UtInv.of_same hM J.ut hSS, fun G hG => ?_, ?_⟩ + · have hG0 : MdbLinks Z.th (L.mem s) G := hG.of_same_symm hSS + have hsl' : ∀ x ∈ G.chains.flatten, IsSlot Z.th x := fun x h => ((hG0.members x).1 h).1 + exact (J.finM G hG0).of_capV fun x hx => hSS.capV (hsl' x hx) + · refine SpecFree.frame J.sfree (fun x' hx' p sb fi' d hc => ⟨x', hx', fi', ?_, le_rfl⟩) hk + rw [← hSS.capV (hsl x' hx')]; exact hc + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`reg_frameF` at `Z` itself, with the footprint**: the witness of `reg_frameF` is `Z.vs`, so the +post-state `JAtZRB` is at `Z`; also names the `FinW` footprint (gen 31, for the VM-epoch lift). -/ +theorem JAtZRB.reg_frameFZ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} + (hI3 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hw : P1.WritesIn st s s' (RegFW Z.T Z.C)) (hA : QKY σ σ') (hyk : YKeep σ σ') (hInv : Inv σ') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) ∅ s)) : + P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) ∧ + JAtZRB (L := L) arI AR Z H Cs Ac' Lst D s' a σ' := by + have hI := J.xtop.irelK J.ks J.buf + have hT : TcbPlaced Z.T := J.ip.e.tcb + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw fun b hb => by + rcases hb with (hb | ⟨q, hq, rfl⟩) | ⟨y, hy, hb⟩ + · obtain ⟨y, hy, h1, h2⟩ := hb + exact Or.inr (Or.inr (Or.inl ⟨y, hy, h1, by omega⟩)) + · obtain ⟨x0, n0, hk0, hx0⟩ := hs.scs q hq + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl + ⟨q, hq, x0, n0, hk0, hx0.in_obj (hx0.hdr (K := 8#u64) (by simp) (by simp)).2⟩)))))) + · exact Or.inr (Or.inr (Or.inl ⟨y, hy, by omega, by omega⟩)) + have hwF : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := hw.mono fun _ hb => regFW_finW hS hb + have hck : Fl4Proofs.P3.CKeep σ σ' := hA.ckeepP + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3 hAR hF.1 hck.cdt hck.orig hck.ks hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.finW_keeps hwF + have ck := hA.callKeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hA.khrb + have hDbg := J.kw_dbg (hfoot.mono fun b hb => kw_empty_mono hb _) + have hKd := J.kw_dbgKeep (hfoot.mono fun b hb => kw_empty_mono hb _) + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hI3.2.2.1 + have hAI3 : ActIdle σ' := hA.actIdle J.act + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_qk hA hS3 hI3.1 hwF k3 hAI3.1 (J.yld.frame k2 hyk) hyk + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hA.szKeep q.bv y n hq) hks (J.finW_irq hwF) + (J.idleW_keep hS3 hA.idle hA.isc).2⟩ + have hkern := J.kern_keep hks hwF.2.1 hS3 hA.ct hA.csc (fun sl h => Or.inl (by rw [← hA.orig]; exact h)) + (by rw [hA.arch]) + (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hDbg + (D' := D) + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac' Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 hyk, J.mcp.frame k1 hA.mcpKeep, J.buf.frame kb hA.bufKeep, hRP', + J.apart.sc_frame hks hA.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hA.idle hA.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hwF fun _ hb => Ipc.FinW.ntd J.xtop.fin.1.1.1 hb)), hkern, + J.finW_utc hwF hF.1 hA.cdt hA.orig hA.arch hDbg, hA.nsc J.nsc, hInv, (fun e => hA.curInv (J.cur (by rw [← hA.act]; exact e))), hA.own J.own, hA.off J.off, + hAI3, hA.bsa J.bsa, hA.isorted J.isorted, hA.curRunM J.curRun, hblk⟩ + have hC0 := JCs.sameBR J hF.1 hA.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have JZ := J.cs_frameI hks hX3 hwF (fun b hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) hA.arch (fun q ao h => (hA.oth (fun _ e => by cases e) (fun _ _ e => by cases e)).2 h) hA.szKeep hF.1 hA.kback hA.ipcFr hC R hKd hck.orig (J.idleW_keep hS3 hA.idle hA.isc).1 J.zD + exact ⟨hwF, JZ⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **`setConsumedJ` at `Z`, with the step's footprint and spec shape** (`QKY`): gen 31, for the +VM-epoch lift. -/ +theorem JAtZRB.setConsumedJV (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) {p y : U64} (hpC : p ∈ Z.C) + (hy : y ∈ Z.T) : + schedcontext.set_consumed st arI s a p y true ⦃ s1 => ∃ σ1, + Fl4Spec.setConsumed (Z := Unit) p.bv y.bv σ = ({((), σ1)}, False) ∧ YKeep σ σ1 ∧ + QKY σ σ1 ∧ P1.WritesIn st s s1 (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) ∧ + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s1 a σ1 ⦄ := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨z, n, hkz, -⟩ := hs.scs p hpC + obtain ⟨x, hkx, -⟩ := hs.tcb y hy + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hpS : p ∈ P1.Heap.scs H := ⟨_, (hJ.sc p z n (J.c0 p hpC) hkz).2⟩ + have hyT : y ∈ P1.Heap.tcbs H := by rw [← J.tcbs_eq.1]; exact hy + have hta := hc.tcbs y hy + refine WP.spec_mono (WP.spec_and (set_consumed_irel (ar := arI) J.ip s a σ hI hpC hkz hy hkx hmax htu) + (P1.schedcontext.set_consumed.blk hAT s a p y true J.blk hpS hyT)) ?_ + rintro s1 ⟨⟨σ1, he, hI1, hw⟩, hP1⟩ + obtain ⟨v, w, he'⟩ := Sched.setConsumed_shape hkz hkx + have e := the_result he' he + subst e + have hA := QKY.upd2 (w := Fl4Spec.scConsumedUpdate (fun _ => v) z) (x' := Endp.replyTcb x w) hkz hkx rfl rfl + have hyk := ykeep_upd2 (w := Fl4Spec.scConsumedUpdate (fun _ => v) z) (x' := Endp.replyTcb x w) hkz hkx rfl rfl + have hpy : y.bv ≠ p.bv := fun e => by rw [e, hkz] at hkx; cases hkx + have hk1 : (updSc σ p.bv (Fl4Spec.scConsumedUpdate (fun _ => v) z) n).kheap y.bv = some (.tcb x) := by + simp only [updSc, updK, Function.update_of_ne hpy]; exact hkx + have hInv := (archStep_updTcb (x' := Endp.replyTcb x w) hk1 rfl).inv (J.inv.scFields hkz rfl rfl rfl rfl) + have hw' : P1.WritesIn st s _ (RegCW Z.T Z.C) := hw.mono fun b hb => by + rcases hb with rfl | ⟨r, rfl⟩ + · exact Or.inr ⟨p, hpC, rfl⟩ + · have hv := regA_val hta (regIdx_lt r) + have := regIdx_lt r + exact Or.inl ⟨y, hy, by rw [hv]; omega, by rw [hv]; omega⟩ + obtain ⟨hwF, J1⟩ := J.reg_frameFZ hI1 (hw'.mono fun _ h => Or.inl h) hA hyk hInv hP1.1 hP1.2.1 + exact ⟨_, he', hyk, hA, hwF, J1⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **The yielder's `complete_yield_to` at `J`** (module docstring), the dead set kept, with the native footprint +(gen 42b45, P3/NatFoot: `setConsumedJV`'s `FinW` and `QKY`, then `cancelYieldJD`'s `TFoot` and `ScTKw`). -/ +theorem JAtZRB.completeYieldJDN (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) {y : U64} (hy : y ∈ Z.T) : + schedcontext.complete_yield_to st arI s a y ⦃ r => ∃ σ', + Fl4Spec.completeYieldTo (Z := Unit) y.bv σ = ({((), σ')}, False) ∧ + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64), + JAtZRB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D r.1 r.2 σ' ∧ + NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + obtain ⟨Q, R0, hc, hs⟩ := J.xtop.fin.1.1.1.lists + have hy0 : y ≠ 0#u64 := fun e => (hc.tcbs y hy).ne0 (by rw [e]; rfl) + obtain ⟨xy, hky, eyt⟩ := J.yld.tcb y hy + unfold schedcontext.complete_yield_to + step with obj.Tcb.is_null.spec as ⟨b, hb⟩ + have hb2' : b = false := by rw [hb]; exact decide_eq_false hy0 + rw [hb2'] + simp only [Bool.false_eq_true, ↓reduceIte] + step with obj.Tcb.yield_to.spec as ⟨i, hi⟩ + subst hi + by_cases hi0 : L.mem s (yieldA y) = 0#u64 + · have : (L.mem s (yieldA y) != 0#u64) = false := by simp [hi0] + simp only [this, Bool.false_eq_true, ↓reduceIte, WP.spec_ok] + exact ⟨σ, completeYieldTo_none hky (by rw [eyt, hi0, Notif.optRef_zero]), Z.vs, Ac, Lst, J, NatFoot.refl Z s σ⟩ + obtain ⟨p, hp⟩ : ∃ p, L.mem s (yieldA y) = p := ⟨_, rfl⟩ + rw [hp] at hi0 eyt ⊢ + have hb' : (p != 0#u64) = true := by simp [hi0] + simp only [hb', ↓reduceIte] + obtain ⟨hpC, -⟩ := J.yld.toSc y hy (by rw [hp]; exact hi0) + rw [hp] at hpC + refine WP.spec_bind (J.setConsumedJV hAT hmax htu hpC hy) ?_ + rintro s1 ⟨σ1, he1, hyk, hq1, hwF1, J1⟩ + have hm1 : L.mem s1 (yieldA y) = p := by + obtain ⟨x1, hk1, e1⟩ := J1.yld.tcb y hy + obtain ⟨x1', hk1', e1'⟩ := hyk.1 _ xy hky + rw [hk1] at hk1'; cases hk1' + rw [eyt, Notif.optRef_ne hi0] at e1' + rw [e1'] at e1 + exact P1.eq_of_bv (Notif.optRef_some e1.symm).2 + refine WP.spec_bind (J1.cancelYieldJD hy hm1 hi0) ?_ + rintro s2 ⟨σ2, he2, hk2, hw2, Ac2, Lst2, J2⟩ + refine (WP.spec_ok _).2 ⟨σ2, ?_, Z.vs, Ac2, Lst2, J2, + J.natTrans (NatFoot.ofW J.xtop hwF1 (fun _ hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) + (fun _ hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) (fun _ hb => J.finW_nh hb) (natK_qky hq1)) + (NatFoot.ofT J1.xtop hw2 (fun _ h => J1.tfoot_nh h) (natK_tk hk2))⟩ + rw [completeYieldTo_some hky (by rw [eyt, Notif.optRef_ne hi0]), nd_bind_one he1] + exact he2 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- The current thread of a `JAtZ` state: the node's `CUR_THREAD` word, the +spec's, a TCB. -/ +theorem JAtZRB.curEq {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {ct : U64} (hct : sched.cur_thread st s = ok ct) : + σ.curThread = ct.bv ∧ ∃ x, σ.kheap ct.bv = some (.tcb x) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R', hc, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hc.cur] at h; exact h + have hcur : σ.curThread = ct.bv := by rw [hs.curThread, hct'] + obtain ⟨-, x, hk⟩ := J.xtop.curTcb hcur + exact ⟨hcur, x, hk⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead across a scheduler step** (`deadP_sc_step` for a +`SchStep`, the idle thread by `ActIdle` after). -/ +theorem JAtZRB.deadP_sch_step {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) + (hK0 : SchStep (fun _ => False) σ σ') + {W : Fl4Spec.ObjRef → Prop} (hL : LK W σ σ') (hAI : ActIdle σ') + {q : U64} (hYm : YMemDead (L := L) H Z.C s s' q) (hW : ¬ W q.bv) (hd : DeadP (L := L) H s q) : + DeadP (L := L) H s' q := by + have hK := hK0.toTK + have hAS := hAI.1 + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hS' := hX'.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + have hI := hX.ipc + have hI' := hX'.ipc + refine ⟨fun hq => ?_, fun hq => ?_, fun hq => ?_, fun hq => ?_, fun z hq => ?_⟩ + · -- a TCB + obtain ⟨d1, d2, d3, d4, d5, d6⟩ := hd.1 hq + have hqT : q ∈ Z.T := by rw [J.tcbs_eq.1]; exact hq + obtain ⟨x, hk, hR⟩ := hI.tcb q hqT + obtain ⟨x', hk', hR'⟩ := hI'.tcb q hqT + obtain ⟨y, hy, hso, hst⟩ := hK.tcb hk + replace hy : σ'.kheap q.bv = some (.tcb y) := hy + rw [hk'] at hy; cases hy + have hst0 : tsCode x.tcbState = 0 := by + have := hR.st; rw [← stA_tStA, d1] at this; unfold sched.TS_INACTIVE at this; simpa using this.symm + have hstate : x'.tcbState = x.tcbState := by + rcases hst with e | ⟨hp, -, -⟩ + · exact e + · exact hp.elim + have hxs : x.tcbSchedContext = none := by rw [hR.sc, d2, optRef_zero] + have hxs' : x'.tcbSchedContext = none := by + obtain ⟨y2, hy2, e⟩ := hL.tcb hW hk + rw [hk'] at hy2; cases hy2 + rcases e with e | e + · rw [e, hxs] + · exact e + refine ⟨?_, ?_, ?_, ?_, ?_, hYm.1 hq d6⟩ + · have h1 : (L.mem s' (tStA q)).val = (L.mem s (tStA q)).val := by rw [hR'.st, hR.st, hstate] + rw [stA_tStA, UScalar.eq_of_val_eq h1, ← stA_tStA, d1] + · exact optRef_eq_none (by rw [← hR'.sc, hxs']) + · have hb : x'.tcbBoundNotification = x.tcbBoundNotification := by rw [hso] + have h1 := hR.bound + rw [show tBoundA q = P1.boundA q from rfl, d3, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.boundA q = tBoundA q from rfl, ← hR'.bound, hb, h1]) + · intro e + have hact := hS'.act + have ea : L.actionW s' = Sched.nodeW st s' Z.c layout.node.SCHED_ACTION := by + unfold P1.LawfulStore.actionW Sched.nodeW; rw [hc'.cur] + rw [← ea, ← e] at hact + revert hact + cases hsa : σ'.schedulerAction with + | resumeCurThread => + intro h; simp only [ActRel] at h; subst h + rw [J.kern.h0] at hq; cases hq + | chooseNewThread => + intro h; simp only [ActRel] at h; subst h + have := J.kern.heap.align _ _ hq + simp [P1.HKind.bits] at this + | switchThread t => + intro h; simp only [ActRel] at h + obtain ⟨-, -, ht⟩ := h + obtain ⟨x2, scp, -, -, hk2, hsc2, -⟩ := schedulable_inv (hAS t hsa) + rw [← ht, hk'] at hk2; cases hk2 + rw [hxs'] at hsc2; cases hsc2 + · intro e + obtain ⟨xi, hki, -, hsti, -⟩ := hAI.2 + have h1 := hs'.idleTh + have ei : σ'.idleThread = q.bv := by + rw [h1, e]; unfold P1.LawfulStore.idleW Sched.nodeW; rw [hc'.cur] + rw [ei, hk'] at hki; cases hki + rw [hstate] at hsti + rw [hsti] at hst0 + simp [tsCode] at hst0 + · -- an endpoint + have hqE : q ∈ Z.E := J.hset.1 q hq + obtain ⟨x, hk, hR⟩ := hI.ep q hqE + obtain ⟨x', hk', hR'⟩ := hI'.ep q hqE + have hke : σ'.kheap q.bv = some (.endpoint x) := hK.ep hk + rw [hke] at hk'; cases hk' + rw [epState_eq, stOf_ep hR hR', ← epState_eq]; exact hd.2.1 hq + · -- a notification + obtain ⟨d1, d2, d3⟩ := hd.2.2.1 hq + have hqN : q ∈ Z.N := J.hset.2.1 q hq + obtain ⟨x, hk, hR⟩ := hI.ntfn q hqN + obtain ⟨x', hk', hR'⟩ := hI'.ntfn q hqN + obtain ⟨y, hy, ho, hb⟩ := hK.ntfn' hk + replace hy : σ'.kheap q.bv = some (.notification y) := hy + rw [hk'] at hy; cases hy + have hxs : x.ntfnSc = none := by rw [hR.sc, show nScA q = P1.ntfnScA q from rfl, d3, optRef_zero] + have hxs' : x'.ntfnSc = none := by + obtain ⟨y2, hy2, e⟩ := hL.ntfn hW hk + rw [hk'] at hy2; cases hy2 + rcases e with e | e + · rw [e, hxs] + · exact e + refine ⟨?_, ?_, ?_⟩ + · rw [epState_eq] + have hO' := hR'.obj + rw [ho] at hO' + rw [stOf_ntfn hR.obj hO', ← epState_eq]; exact d1 + · have h1 := hR.bound + rw [show nBoundA q = P1.ntfnBtA q from rfl, d2, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.ntfnBtA q = nBoundA q from rfl, ← hR'.bound, hb, h1]) + · exact optRef_eq_none (by rw [show P1.ntfnScA q = nScA q from rfl, ← hR'.sc, hxs']) + · -- a reply + have hqR : q ∈ Z.R := J.hset.2.2 q hq + obtain ⟨x, hk, hR⟩ := hI.reply q hqR + obtain ⟨x', hk', hR'⟩ := hI'.reply q hqR + obtain ⟨y, hy, e⟩ := hK.reply hk + replace hy : σ'.kheap q.bv = some (.reply y) := hy + rw [hk'] at hy; cases hy + have h1 := hR + rw [show Endp.rTcbA q = P1.rTcbA q from rfl, hd.2.2.2.1 hq, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.rTcbA q = Endp.rTcbA q from rfl, ← hR', e, h1]) + · -- an SC + obtain ⟨d1, d2, d3, d4⟩ := hd.2.2.2.2 z hq + by_cases hqC : q ∈ Z.C + · obtain ⟨x, n, hk, hR⟩ := hs.scs q hqC + obtain ⟨x', n', hk', hR'⟩ := hs'.scs q hqC + obtain ⟨y, hy, e1, e2⟩ := hL.sc hW hk + rw [hk'] at hy; cases hy + have hxt : x.scTcb = none := by rw [hR.tcb, scW_tcb, d1, optRef_zero] + have hxn : x.scNtfn = none := by rw [hR.ntfn, scW_ntfn, d3, optRef_zero] + refine ⟨?_, ?_, ?_, hYm.2 z hq hqC d4⟩ + · rw [← scW_tcb]; refine optRef_eq_none ?_; rw [← hR'.tcb] + rcases e1 with e | e + · rw [e, hxt] + · exact e + · rw [(TFoot.keep hS J.rp hw).2.1 q hqC]; exact d2 + · rw [← scW_ntfn]; refine optRef_eq_none ?_; rw [← hR'.ntfn] + rcases e2 with e | e + · rw [e, hxn] + · exact e + · refine ⟨?_, ?_, ?_, ?_⟩ + · rw [← scW_tcb, J.sc_out_keep hw hq hqC (by decide) (by decide), scW_tcb]; exact d1 + · rw [scReplyA_eq', J.sc_out_keep hw hq hqC (by decide) (by decide), ← scReplyA_eq']; exact d2 + · rw [← scW_ntfn, J.sc_out_keep hw hq hqC (by decide) (by decide), scW_ntfn]; exact d3 + · rw [scYieldFromA_eq, J.sc_out_keep hw hq hqC (by decide) (by decide), ← scYieldFromA_eq]; exact d4 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +/-- FL4's timers keep the kernel-global words below 256 (`JKern.fire_ks`). -/ +theorem JAtZRB.fire_ks {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s1 : S} {U : Set U64} + (hf : P1.FootDU st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) U s s1) + (hU : ∀ b ∈ U, ∀ o : U64, o.val < 13568 → o.val % 8 = 0 → core.num.U64.wrapping_add (L.ksb s) o ≠ b) : + ∀ o : U64, o.val < 256 → o.val % 8 = 0 → L.mem s1 (ksW (L.ksb s) o) = L.mem s (ksW (L.ksb s) o) := + J.kern.fire_ks (fun c hc => J.blk.2.2.1.2.2.tm.npl c hc) hf hU + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +/-- `FpuOwnOK` after `fl4AwakenVF` from `FpuOwnOK` before (`fpuOwnOK_of_own`, the owner by `Vs.owF_awakenVF`). -/ +theorem JAtZRB.fpuOwnOK_awakenVF {on rc : Fl4Spec.ObjRef → Bool} {s : S} {a : A} {σ σ1 : Fl4Spec.State Unit} + {m : U64 → U64} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hF : ((), σ1) ∈ (Fl4Spec.fl4AwakenVF (Z := Unit) on rc σ).1) (hN : NtfnStateRel Z.N Z.T m σ1) + (hfo : FpuOwnOK σ) : FpuOwnOK σ1 := + fpuOwnOK_of_own J.obj hN (congrArg Prod.fst (Vs.owF_awakenVF hF)) hfo + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- `cs_frameV` at the same VSpace sets. -/ +theorem JAtZRB.cs_frame {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR { Z with G := G' } s' a' σ') (hkb : KBack σ σ') (hfk : IpcFrKeep σ σ') (hcy : JCarryS Z.B H σ') + (hdu : DevFrom Z.G G' (L.mem s) (L.mem s') ∧ Sched.UtFrom Z.G G' (L.mem s) (L.mem s')) + (hC : JCs (L := L) { Z with G := G' } s' σ') (R : JRestR (L := L) arI { Z with G := G' } H Cs Ac' Lst' D' s' σ') + (hvT : Vs.VsSlotsT Z.th (L.mem s) (L.mem s')) + (hlv : LiveDN (L := L) Z.th H Cs G' s' σ') (hzD : ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D') + (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR { Z with G := G' } H Cs Ac' Lst' D' s' a' σ' := + J.cs_frameV (V' := Z.vs) (hvn := hvn) hks hX hkb (J.bfK hfk hvT) hC R (J.vsc.step hvT) hlv hzD hcy hdu + (J.carry.vsStep rfl hvT subset_rfl subset_rfl subset_rfl subset_rfl rfl hks (ArchFwd.of_ipcFr hfk)) (ArchFwd.of_ipcFr hfk) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`cs_frame` for a step keeping every slot and the dead set** (KENTRY-PLAN +g2i): `JAtZ.live` by `liveX` (the original bits by `orig_eq_of_slots`, the idle +word from the spec's idle thread kept), `zD` by `zDX`; the step owes P1's +`DbgKeep`. -/ +theorem JAtZRB.cs_frameK {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} (hks : L.ksb s' = L.ksb s) + (hX : XTopAt (L := L) AR Z s' a' σ') (hkb : KBack σ σ') (hfk : IpcFrKeep σ σ') (hcy : JCarryS Z.B H σ') + (hC : JCs (L := L) Z s' σ') (R : JRestR (L := L) arI Z H Cs Ac' Lst' D s' σ') + (hvT : Vs.VsSlotsT Z.th (L.mem s) (L.mem s')) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hK : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') + (hid : σ'.idleThread = σ.idleThread) (hvn : (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s'))) : + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a' σ' := + J.cs_frame (G' := Z.G) (hvn := hvn) hks hX hkb hfk hcy + (devFrom_same J.xtop.rel.2.1.2.2.1 hX.rel.2.1.2.2.1 hSS) hC R hvT + (J.liveX hX rfl rfl rfl hSS hK + (orig_eq_of_slots J.xtop.rel.2.1.2.2.2.1 hX.rel.2.1.2.2.2.1 J.kern.orig R.kern.orig hSS) + (XTopAt.idleW_eqS J.xtop hX hid) fun p hp => R.jcov.dead p (J.zD p hp)) + (J.zDX hSS J.zD) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + +/-- **A word outside the TCBs and off the nodes' head words is no debug word.** -/ +theorem JAtZRB.notDbg' {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hobj : ∀ p, H p = some .tcb → b.val < p.val ∨ p.val + 2 ^ P1.HKind.bits .tcb ≤ b.val) + (hnode : ∀ c ∈ Cs, b.val ≠ L.nodeBase s c + 88) : + ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := by + obtain ⟨⟨Q, R, hq⟩, -⟩ := J.blk + have hT := hq.tcbs + have e12 : (2 : Nat) ^ P1.HKind.bits .tcb = 4096 := rfl + rintro (⟨y, hy, e | e⟩ | ⟨c, hc, e⟩) + · have h1 := hobj y hy + rw [e, show P1.dnA y = core.num.U64.wrapping_add y 3576#u64 from rfl, P1.field_val (hT y hy) (by simp), e12] + at h1 + have : (3576#u64 : U64).val = 3576 := by simp + omega + · have h1 := hobj y hy + rw [e, show P1.dpA y = core.num.U64.wrapping_add y 3584#u64 from rfl, P1.field_val (hT y hy) (by simp), e12] + at h1 + have : (3584#u64 : U64).val = 3584 := by simp + omega + · have h1 := hnode c hc + rw [e, P1.nodeAt_val (hq.nodes c hc) (by simp [P1.debug_val]), P1.debug_val] at h1 + exact h1 rfl + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + +/-- **A scheduler step's word is no debug word** (`SchW`: the TCBs' queue +words, the current node but its debug head, the domain words). -/ +theorem JAtZRB.schW_notDbg {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : SchW (L := L) Z.T Z.c s b) : ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := by + have hS := J.xtop.fin.1.1.1 + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + obtain ⟨⟨Q, R, hq⟩, -⟩ := J.blk + have hnb : ∀ c, L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := fun _ => rfl + rcases hb with ⟨y, hy, e | e | e⟩ | hn | hd + · subst e; exact P1.notDbgW_field hq.tcbs hq.nodes (hTe ▸ hy) (by simp) (by simp) + · subst e; exact P1.notDbgW_field hq.tcbs hq.nodes (hTe ▸ hy) (by simp) (by simp) + · subst e; exact P1.notDbgW_field hq.tcbs hq.nodes (hTe ▸ hy) (by simp) (by simp) + · have hk : Sched.KFoot st Z.T Z.N Z.C Z.c s σ b := Sched.KFoot.of_sfoot (Or.inr ⟨hn.1, hn.2.1⟩) + rintro (⟨y, hy, e | e⟩ | ⟨c, hc, e⟩) + · exact Sched.KFoot.ne_tcbw hS hk (by rw [hTe]; exact hy) (K := 3576#u64) (by simp) + (by unfold Sched.SOff Sched.SOffL; decide) e + · exact Sched.KFoot.ne_tcbw hS hk (by rw [hTe]; exact hy) (K := 3584#u64) (by simp) + (by unfold Sched.SOff Sched.SOffL; decide) e + · obtain ⟨h1, h2, h3⟩ := hn + have h4 := congrArg UScalar.val e + rw [P1.nodeAt_val (hq.nodes c hc) (by simp [P1.debug_val]), P1.debug_val] at h4 + rw [hnb] at h1 h2 h3 h4 + omega + · have hv := domKW_val hd + refine J.notDbg' (fun p hp => ?_) (fun c _ => ?_) + · have e12 : (2 : Nat) ^ P1.HKind.bits .tcb = 4096 := rfl + rw [e12]; rcases hq.ksOut p hp with h | h <;> omega + · rw [hnb]; omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The dead stay dead across a heap-keeping step** (`deadP_keep` with +every object but the CNodes kept, `HStep`). -/ +theorem JAtZRB.deadP_hstep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {G' : MdbGhost} (hX' : XTopAt (L := L) AR { Z with G := G' } s' a' σ') (hC : HStep σ σ') + (hRW : ∀ q ∈ Z.C, L.mem s' (P1.scReplyA q) = L.mem s (P1.scReplyA q)) + (hOut : ∀ q z, H q = some (.sc z) → ∀ K : U64, K.val % 8 = 0 → K.val < 128 → + L.mem s' (scW q K) = L.mem s (scW q K)) (hAS : ActSched σ') + (hY' : YieldRel Z.T Z.C (L.mem s') σ') (hYK : YKeep σ σ') + {q : U64} (hd : DeadP (L := L) H s q) : DeadP (L := L) H s' q := + J.deadP_keep hX'.fin.1.1.1 hX'.ipc (fun x hx _ => let ⟨y, hy, hc⟩ := hC.tcbF hx; ⟨y, hy, hc.st, hc.ntfn, Or.inl hc.sc⟩) + (fun x hx => (hC.look (fun _ _ e => by cases e) (fun _ e => by cases e)).2 hx) + (fun x hx => ⟨x, hC.ntfn.2 hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, (hC.look (fun _ _ e => by cases e) (fun _ e => by cases e)).2 hx, rfl⟩) + (fun x n hx => ⟨x, hC.sc.2 hx, Or.inl rfl, Or.inl rfl⟩) (hRW q) (fun z hz _ => hOut q z hz) hC.idle hAS hY' + (hYK.dk q.bv) hd + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **`JPlaced` across a step** keeping every slot, the kernel-state base, +the SCs' sizes and the IRQ node's caps (`JPlaced.same`). -/ +theorem JAtZRB.jpl_keep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hkh : σ'.kheap = σ.kheap) (hks : L.ksb s' = L.ksb s) + (hI : ∀ i : U64, i.val < 128 → capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i)) + (hiw : Sched.idleScW (L := L) s' = Sched.idleScW (L := L) s) : + ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hSS (fun c y n _ hk => ⟨y, by rw [← hkh]; exact hk⟩) hks hI hiw⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem JAtZRB.tdA_val' (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) : + (P1.tdA t).val = t.val + 3528 := + Notif.tfield_val (J.ip.e.tcb t ht) (K := 3528#u64) (by simp) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A kernel-state block word is no TCB's deadline word.** -/ +theorem JAtZRB.ks_ntd (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (h1 : (L.ksb s).val ≤ b.val) + (h2 : b.val < (L.ksb s).val + 13568) : ∀ t ∈ Z.T, b ≠ P1.tdA t := by + intro t ht e + obtain ⟨⟨Q, R, hq⟩, -⟩ := J.blk + have hv := J.tdA_val' ht + rw [e] at h1 h2 + rcases hq.ksOut t (J.zH.1 t ht) with h | h <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable {arI} in +/-- A scheduler-step word (`SchW`) is no TCB's deadline word (KENTRY-PLAN jc6e). -/ +theorem JAtZRB.schW_ntd {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (h : SchW (L := L) Z.T Z.c s b) : + ∀ t ∈ Z.T, b ≠ P1.tdA t := by + have hT : P1.TcbPlaced Z.T := J.ip.e.tcb + intro u hu + rcases h with ⟨y, hy, rfl | rfl | rfl⟩ | h | h + · exact Sched.tdA_ne (K := 3480) hT hy hu (tfield_val (hT y hy) (by simp)) (by omega) (by omega) + · exact Sched.tdA_ne (K := 3488) hT hy hu (tfield_val (hT y hy) (by simp)) (by omega) (by omega) + · exact Sched.tdA_ne (K := 3592) hT hy hu (tfield_val (hT y hy) (by simp)) (by omega) (by omega) + · exact Sched.KFoot.ne_td J.xtop.fin.1.1.1 (Sched.KFoot.of_sfoot (Or.inr ⟨h.1, h.2.1⟩)) hu + · have := domKW_val h + exact J.ks_ntd (by omega) (by omega) u hu + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **An SC of `H`'s words**: within its object, aligned. -/ +theorem JAtZRB.scW_in {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {q : U64} {z : Nat} (hq : H q = some (.sc z)) {K : U64} + (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + P1.Aligned (scW q K) ∧ (scW q K).val = q.val + K.val ∧ q.val + K.val + 8 ≤ q.val + 2 ^ z := by + have hH := J.kern.heap + have hal := hH.align q _ hq + have hfit := hH.fits q _ hq + have hz := hH.scmin q z hq + simp only [P1.HKind.bits] at hal hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + exact ⟨by show (scW q K).val % 8 = 0; rw [hv]; omega, hv, by omega⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`H`'s SC words are kept by scheduler writes** (`H`'s objects disjoint +from the TCBs, apart from the node and the kernel-state block). -/ +theorem JAtZRB.schW_scKeep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hw : P1.WritesIn st s s' (SchW (L := L) Z.T Z.c s)) + {q : U64} {z : Nat} (hq : H q = some (.sc z)) {K : U64} (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + L.mem s' (scW q K) = L.mem s (scW q K) := by + obtain ⟨hal, hv, hf⟩ := J.scW_in hq hK8 hK + have hS := J.xtop.fin.1.1.1 + have hP := J.xtop.fin.1.2 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64 := by have := hP.vs.ksb_fit; omega + have hks := J.kern.hks q _ hq (fun _ e => by cases e) + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hnd := J.kern.hnd q _ hq Z.c hcC + simp only [P1.HKind.bits] at hks hnd + refine hw.mem_eq hal fun hb => ?_ + rcases schW_val hc.tcbs hkf hb with ⟨y, hy, e⟩ | ⟨g1, g2⟩ | ⟨g1, g2, -⟩ + · have hHy : H y = some .tcb := J.zH.1 y hy + have hne : q ≠ y := fun e' => by rw [e', hHy] at hq; cases hq + have := J.kern.heap.disj q _ y _ hq hHy hne + simp only [P1.HKind.bits] at this + omega + · omega + · omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **The scheduler's words a scheduler step keeps** (as `inSlot_keeps`, for +`SchW`: the TCBs' queue words, the node, the domain words). -/ +theorem schW_keepsBR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hw : P1.WritesIn st s s' (SchW (L := L) Z.T Z.c s)) : + (∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t)) ∧ (∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (tIpcBufA t) = L.mem s (tIpcBufA t)) ∧ + (∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q)) ∧ + (∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r)) := by + have hS := J.xtop.fin.1.1.1 + have hP := J.xtop.fin.1.2 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hT := hc.tcbs + have hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64 := by have := hP.vs.ksb_fit; omega + have hko : ∀ t ∈ Z.T, t.val + 4096 ≤ (L.ksb s).val ∨ (L.ksb s).val + 13568 ≤ t.val := fun t ht => by + rcases hc.ksOut t ht with h | h <;> omega + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + have hnd := hc.node.disj + -- a word apart from the TCBs' queue words, the node and the domain words + have keep : ∀ b : U64, P1.Aligned b → (∀ y ∈ Z.T, b.val ≠ y.val + 3480 ∧ b.val ≠ y.val + 3488 ∧ b.val ≠ y.val + 3592) → + (b.val < L.nodeBase s Z.c ∨ L.nodeBase s Z.c + 4288 ≤ b.val) → + (b.val + 8 ≤ (L.ksb s).val + 16 ∨ (L.ksb s).val + 216 < b.val) → L.mem s' b = L.mem s b := by + intro b hb h1 h2 h3 + refine hw.mem_eq hb fun hb' => ?_ + rcases schW_val hT hkf hb' with ⟨y, hy, e | e | e⟩ | ⟨g1, g2⟩ | ⟨g1, g2, -⟩ + · exact (h1 y hy).1 e + · exact (h1 y hy).2.1 e + · exact (h1 y hy).2.2 e + · omega + · omega + have tw : ∀ t ∈ Z.T, ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → K.val ≠ 3480 → K.val ≠ 3488 → K.val ≠ 3592 → + L.mem s' (core.num.U64.wrapping_add t K) = L.mem s (core.num.U64.wrapping_add t K) := by + intro t ht K h1 h8 n1 n2 n3 + have hv := Notif.tfield_val (hT t ht) h1 (K := K) + refine keep _ (P1.field_aligned (hT t ht) h1 h8) (fun y hy => ?_) ?_ ?_ + · by_cases e : y = t + · subst e; omega + · have := tcb_disj hP.ep ht hy e; omega + · have := hnd t ht; omega + · rcases hko t ht with h | h <;> omega + refine ⟨fun t ht => ⟨tw t ht 3440#u64 (by simp) (by simp) (by simp) (by simp) (by simp), + tw t ht 3456#u64 (by simp) (by simp) (by simp) (by simp) (by simp)⟩, + fun t ht => tw t ht 3472#u64 (by simp) (by simp) (by simp) (by simp) (by simp), + fun t ht => tw t ht 3512#u64 (by simp) (by simp) (by simp) (by simp) (by simp), fun q hq => ?_, fun r hr => ?_⟩ + · obtain ⟨hal, hv, -⟩ := sc_reply_word hS hq + obtain ⟨-, o1, o2, o3⟩ := hS.scOutIn hq (a := scReplyA q) ⟨by rw [hv]; omega, by rw [hv]; omega⟩ + refine keep _ hal (fun y hy => ?_) (by omega) (by omega) + rcases o2 y hy with h | h <;> omega + · have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have hk := J.rp.ksb r hr + have ht := J.rp.tcb r hr + refine ⟨keep _ (J.rp.aligned hr (Or.inl h8)) (fun y hy => ?_) (by omega) (by omega), + keep _ (J.rp.aligned hr (Or.inr h16)) (fun y hy => ?_) (by omega) (by omega)⟩ + · rcases ht y hy with h | h <;> omega + · rcases ht y hy with h | h <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +set_option maxHeartbeats 4000000 in +/-- **`JStepHyps.sched`**, with no premise: FL4 writes the scheduler's words +(`schW_keeps`), the spec keeps the heap (`SchedK`, so `HStep`); `Inv`, the +current thread's invariant and `ActIdle` after (`SchedQOK`: the ready queues +gain schedulable threads only, the action kept or `chooseNewThread`); P1's +`BlkOK` after (`BlkKeep`); the node-0 debug word and the untyped caps' +facts kept (the footprint `SchW` misses them). -/ +theorem jowes_schedDBR {AR : A → Fl4Spec.Cr3 → Prop} {th : TypedHeap} (s s' : S) (a : A) (σ σ' : Fl4Spec.State Unit) + (hS : SchedStep (L := L) th s σ s' σ') : + ∀ (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) (D : Set U64), Z.th = th → + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ → XTopAt (L := L) AR Z s' a σ' → + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JRestR (L := L) arI Z H Cs Ac' Lst' D s' σ' := by + intro Z H Cs Ac Lst D hth J hX' + subst hth + obtain ⟨hK, hS2, hQ, hBK⟩ := hS + have hw := (hS2 Z.N Z.T Z.C Z.c J.thT J.xtop.fin.1.1.1 J.xtop.fin.1.1.2.2.1).2.1 + have hC : HStep σ σ' := ⟨fun p => Or.inl (by rw [hK.kh]), hK.rel, hK.ct, hK.csc, hK.ctime, hK.cons, hK.idle, hK.isc⟩ + obtain ⟨k1, k2, kb, k3, k4⟩ := schW_keepsBR arI J hw + have ck : Sched.CKeep σ σ' := ⟨fun q y n hq => ⟨y, hC.sc.2 hq, rfl⟩, fun q x hq => ⟨x, (hC.look (fun _ _ e => by cases e) (fun _ e => by cases e)).2 hq, + rfl, rfl⟩, fun p x n hp => ⟨x, hC.sc.1 hp⟩⟩ + have yk : YKeep σ σ' := ⟨fun p x hp => let ⟨y, hy, hc⟩ := hC.tcbF hp; ⟨y, hy, hc.yld⟩, fun p x n hp => ⟨x, hC.sc.2 hp, rfl⟩, + fun p y n hp => ⟨y, hC.sc.1 hp, rfl⟩⟩ + have mk : McpKeep σ σ' := fun p x hp => let ⟨y, hy, hc⟩ := hC.tcbF hp; ⟨y, hy, hc.mcpF⟩ + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hw.1 ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hC.khrb + -- the spec's scheduler invariants after + have hI' : Inv σ' := J.inv.congr' hK.kh (fun d p u hu => by + rcases hQ.1 d p u hu with ⟨d', p', h'⟩ | hs + · exact J.inv.rq d' p' u h' + · obtain ⟨x, scp, y, n, h1, h2, h3, h4, h5, -⟩ := schedulable_inv hs + exact ⟨x, scp, y, n, h1, h2, h3, h4, h5⟩) hK.rel hK.ctime + have hcur' : CurInvR σ' := by + intro er + rcases hQ.2 with e | e + · rw [e] at er; cases er + · exact (J.cur (by rw [← e]; exact er)).congr hK.kh hK.ct hK.csc hK.rel hK.isc + have hA' : ActIdle σ' := by + refine ⟨fun t ht => ?_, ?_⟩ + · rcases hQ.2 with e | e + · rw [e] at ht; cases ht + · rw [e] at ht; rw [schedulable_heap hK.kh hK.rel]; exact J.act.1 t ht + · obtain ⟨x, h1, h2, h3, h4⟩ := J.act.2 + exact ⟨x, by rw [hK.kh, hK.idle]; exact h1, by rw [hK.isc]; exact h2, h3, by rw [hK.idle, hK.rel]; exact h4⟩ + have hblk := hBK _ _ _ H _ _ _ _ _ Ac (fun t ht => J.zH.1 t (J.thT t ht)) J.blk + have hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64 := J.kern.ksbFit + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + have hko : ∀ t ∈ Z.T, t.val + 4096 ≤ (L.ksb s).val ∨ (L.ksb s).val + 13568 ≤ t.val := fun t ht => by + rcases hcq.ksOut t ht with h | h <;> omega + have h88 : (layout.node.DEBUG_TCBS).val = 88 := by simp [layout.node.DEBUG_TCBS] + -- the node-0 debug word: no TCB queue word (the TCBs miss the kernel-state + -- block), not core `c`'s node but its own debug word, no domain word + have hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = + L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by + have hf := J.kern.nodeFit + have hv : (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS).val = (L.ksb s).val + 13568 + 88 := by + rw [nodeAt_val' (st := st) (s := s) (c := 0#u64) (off := layout.node.DEBUG_TCBS) (by simp; omega) + (by simp [layout.node.DEBUG_TCBS])] + simp [layout.node.DEBUG_TCBS] + refine hw.mem_eq J.kern.dwAl fun hb => ?_ + rcases hb with hb' | ⟨g1, g2, g3⟩ | hb' + · obtain ⟨y, hy, e⟩ := hb' + have e' := tfield3_val (hcq.tcbs y hy) e + rw [hv] at e' + rcases hko y hy with h | h <;> omega + · rw [hv, hnb] at g1 g2 g3 + omega + · have := domKW_val hb' + rw [hv] at this; omega + -- the untyped caps' facts: the caps, the CDT and the original bits kept, the + -- free parts off the TCBs, the node and the kernel-state block + have hSS0 : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b hb => schW_oobj J.xtop.fin.1.1.1.placed.tcb hb + have hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS0.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + have hfi := U.fiLe + have hcZ : Z.c ∈ Cs := by rw [← hcq.cur]; exact J.kern.cpuCs + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, fun g w hal h1 h2 => ?_, U.sb4, + U.nostr, U.nsFree, fun h0 => U.ns (by rw [hK.cdt] at h0; exact h0), + fun g h0 hf0 => ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [hK.arch]; exact U.gReg (B g)⟩ + · have hapart := J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g)) + have hnd := hapart.2.1 Z.c hcZ + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases schW_val hcq.tcbs hkf hb with ⟨y, hy, e⟩ | ⟨g1, g2⟩ | ⟨g1, g2, -⟩ + · have hty : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y _ hty + simp only [objSize] at this + rcases e with e | e | e <;> omega + · omega + · have := hapart.1; omega + · rw [hK.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← hK.orig]; exact h), by rw [hna, hDbg]; exact r3⟩ + have hI : ∀ i : U64, i.val < 128 → capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := + irqNode_keep hw hkf J.kern.ksbAl fun b hb => by + rcases schW_val hcq.tcbs hkf hb with ⟨y, hy, e | e | e⟩ | ⟨g1, g2⟩ | ⟨g1, g2, -⟩ + all_goals first + | omega + | (rcases hko y hy with h | h <;> omega) + have hkern := J.kern_keep hw.1 hw.2.1 hX'.fin.1.1.1 hK.ct hK.csc (fun sl h => Or.inl (by rw [← hK.orig]; exact h)) (by rw [hK.arch]) + (fun p ao h => by rw [hK.kh]; exact h) hDbg (D' := D) + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b hb => schW_oobj J.xtop.fin.1.1.1.placed.tcb hb + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_hstep hX' hC k3 (fun q z hq K h8 hK => J.schW_scKeep hw hq h8 hK) hA'.1 (J.yld.frame k2 yk) yk + (J.jcov.dead q hq) + exact ⟨Ac, Lst, ⟨hCR', hRF', hRR', J.yld.frame k2 yk, J.mcp.frame k1 mk, + J.buf.frame kb (fun p x hp => let ⟨y, hy, hc⟩ := hC.tcbF hp; ⟨y, hy, hc.buf⟩), hRP', J.apart.sc_frame hw.1 (SzKeep.of_kheap hK.kh), + J.jpl_keep arI hSS hK.kh hw.1 hI (J.idleW_keep hX'.fin.1.1.1 hC.idle hC.isc).2, J.jcov_keep hw.1 hSS hdead (J.idleW_keep hX'.fin.1.1.1 hC.idle hC.isc) + (DXKeep.of_dbg (dbgKeep_of_writes J.blk hw fun _ hb => J.schW_notDbg hb) (J.tdkeep_of hw fun _ hb => J.schW_ntd hb)), + hkern, hutc, J.nsc.cn hC, hI', hcur', J.own.cn hC, J.off.cn hC, hA', J.bsa.cn hC, J.isorted.cn hC, + J.curRun.cn hC hQ.2, hblk⟩⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + {s : S} {ar : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **A scheduler step at `J`** (`stepClosed_jat`'s scheduler case, with +XTopJointRest's `jowes_sched`, no premise; (mmuAD gen 42b70) over `JAtZRB` in place, by `jowes_schedDBR`). -/ +theorem JAtZRB.schedJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s ar σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hS : SchedStep (L := L) Z.th s σ s' σ') : + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' ar σ' := by + have hX := J.xtop + have hX1 := hX.schedStep J.ks J.thT hS + have hW := (hS.2.1 Z.N Z.T Z.C Z.c J.thT hX.fin.1.1.1 hX.fin.1.1.2.2.1).2.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + hX.slotsSame_of hW fun b hb => schW_oobj hX.fin.1.1.1.placed.tcb hb + obtain ⟨Ac', Lst', R⟩ := jowes_schedDBR arI s s' ar σ σ' hS Z H Cs Ac Lst D rfl J hX1 + exact ⟨Ac', Lst', D, J.cs_frameK (hvn := J.vsW hW fun b hb => schW_oobj hX.fin.1.1.1.placed.tcb hb) hW.1 hX1 (KBack.of_kheap hS.1.kh) (IpcFrKeep.of_kheap hS.1.kh) (J.carry.frame subset_rfl (IpcFrKeep.of_kheap hS.1.kh) ((KSz.of_kheap hS.1.kh))) (JCs.sameBR J hSS (KSz.of_kheap hS.1.kh)) R (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) + hSS (dbgKeep_of_writes J.blk hW fun _ hb => J.schW_notDbg hb) hS.1.idle⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71) **The `ARMED` write at `JAtZRB`** (`JAtZRB.schedJ`; `JAtZ.armedJ`'s `JAtZRB` form). -/ +theorem JAtZRB.armedJ {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {c : U64} (hc : L.cpu s = c) (v : U64) : + ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' (L.wr s (L.nodeAt s c layout.node.ARMED) v) a σ := + J.schedJ (armed_schedStep hc v) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71) **The `ARMED` write is a native step** (a word of the current core's node, `SFoot`; the spec's +heap kept): `JAtZRB.armedJ`'s footprint. -/ +theorem JAtZRB.armedN {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {c : U64} (hc : L.cpu s = c) (v : U64) : + NatFoot (L := L) Z s σ (L.wr s (L.nodeAt s c layout.node.ARMED) v) σ := by + subst hc + obtain ⟨Q, R0, hcc, -⟩ := J.xtop.fin.1.1.1.lists + rw [hcc.cur] + exact NatFoot.ofT J.xtop (WritesIn.tfoot_of_kfoot (WritesIn.kfoot_of_sfoot + (sfoot_wr_node hcc.node (o := layout.node.ARMED) (by simp [layout.node.ARMED]) (by simp [layout.node.ARMED]) v))) + (fun _ h => J.tfoot_nh h) (NatK.of_kheap rfl) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **`JCovD` across a step keeping every slot**, the dead set's `DeadX` +carried by the caller (`JAtZ.jcov_keep` with `hdx` in place of a +`DXKeep`: the timer steps write deadline words). -/ +theorem JAtZRB.jcov_keepX {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} (hks : L.ksb s' = L.ksb s) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hdead : ∀ q ∈ D, DeadP (L := L) H s' q) + (hidl : Sched.IdleKeep (L := L) s s') + (hdx : ∀ q ∈ D, Sched.DeadX (L := L) H Cs s q → Sched.DeadX (L := L) H Cs s' q) : + JCovD (L := L) Z.th H Z.G D Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact J.jcov.same (fun y hy _ _ => ⟨hy, hSS.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hSS.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hSS.capV (hsl x hx)]; exact hc⟩) hdx + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + +/-- **A word apart from the TCBs' debug links and the nodes' debug heads is no +debug word** (the links at TCB offsets 3576 and 3584). -/ +theorem JAtZRB.notDbg_tw {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (htcb : ∀ y ∈ Z.T, ∀ (w : U64) (K : Nat), w.val = y.val + K → 3576 ≤ K → K < 4096 → b ≠ w) + (hnode : ∀ c ∈ Cs, b ≠ L.nodeAt s c layout.node.DEBUG_TCBS) : + ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := by + obtain ⟨⟨Q, R, hq⟩, -⟩ := J.blk + have hT := hq.tcbs + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + rintro (⟨y, hy, e | e⟩ | ⟨c, hc, e⟩) + · have h3 : (3576#u64 : U64).val = 3576 := by simp + exact htcb y (by rw [hTe]; exact hy) _ 3576 + (by rw [show P1.dnA y = core.num.U64.wrapping_add y 3576#u64 from rfl, P1.field_val (hT y hy) (by simp), h3]) + le_rfl (by norm_num) e + · have h3 : (3584#u64 : U64).val = 3584 := by simp + exact htcb y (by rw [hTe]; exact hy) _ 3584 + (by rw [show P1.dpA y = core.num.U64.wrapping_add y 3584#u64 from rfl, P1.field_val (hT y hy) (by simp), h3]) + (by norm_num) (by norm_num) e + · exact hnode c hc e + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **The other cores' timer words on `JAt`** (`OtherTmOK`): each other +core's node misses `H`'s objects (`JKern.hnd`), which hold the slots' CNodes +and TCBs (`JPlaced`), the sets' objects (`zH`), the SCs (`JPlaced.sc`) and the +arch objects (`SpecCover.arch`); it lies below the ASID table (`JKern.csLt`) +and misses the IPC-buffer words (`nodeB`). -/ +theorem JAtZRB.otherTmOK {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + OtherTmOK (L := L) Z Cs s σ := by + intro c' hc' e + have hX := J.xtop + have K := J.kern + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hTe : Z.T = P1.Heap.tcbs H := + Set.ext fun t => ⟨fun h => J.zH.1 t h, fun h => J.thT t ((hJ.tcb t).2 h)⟩ + have hNP : P1.NodePlaced st Z.T s c' := by rw [hTe]; exact P1.TInv.nodes J.blk c' hc' + have hlt := K.csLt c' hc' + have hnb : L.nodeBase s c' = (L.ksb s).val + 13568 + c'.val * 4288 := rfl + have hout : ∀ p k, H p = some k → p.val + 2 ^ k.bits ≤ L.nodeBase s c' ∨ L.nodeBase s c' + 4288 ≤ p.val := + fun p k hp => K.hnd p k hp c' hc' + refine ⟨hNP, fun o ho => ?_⟩ + have hv := P1.nodeAt_val hNP (off := o) (by omega) + have hb : L.nodeBase s c' ≤ (L.nodeAt s c' o).val ∧ (L.nodeAt s c' o).val + 8 ≤ L.nodeBase s c' + 4288 := by + rw [hv]; omega + -- a word in an object of `H` of `2 ^ k.bits` bytes is not in the node + have miss : ∀ p k, H p = some k → ¬ (p.val ≤ (L.nodeAt s c' o).val ∧ (L.nodeAt s c' o).val < p.val + 2 ^ k.bits) := + fun p k hp ⟨h1, h2⟩ => by rcases hout p k hp with h | h <;> omega + have hVs := hX.fin.2.1 + have hPt := hX.fin.2.2.1 + have arch : ∀ q : U64, (∃ ao, σ.kheap q.bv = some (.archObj ao)) → + ¬ (q.val ≤ (L.nodeAt s c' o).val ∧ (L.nodeAt s c' o).val < q.val + 4096) := by + rintro q ⟨ao, hq⟩ ⟨h1, h2⟩ + obtain ⟨n, hn, hH⟩ := J.cover.arch _ ao hq + have : (4096 : Nat) ≤ 2 ^ n := by + have := Nat.pow_le_pow_right (show 0 < 2 by norm_num) hn; simpa using this + exact miss q _ hH ⟨h1, by simp only [P1.HKind.bits]; omega⟩ + refine ⟨?_, ?_, ?_, ?_⟩ + · -- no VSpace object + rintro (⟨q, r, hq, h1, h2⟩ | ⟨t, ht, h1, h2⟩ | ⟨q, hq, h1, h2⟩ | ⟨h1, h2⟩) + · exact miss q _ (hJ.cnode q r hq) ⟨h1, by simp only [P1.HKind.bits]; rw [pow_add]; omega⟩ + · exact miss t _ ((hJ.tcb t).1 ht) ⟨h1, by simp only [P1.HKind.bits]; omega⟩ + · refine arch q ?_ ⟨h1, h2⟩ + rcases hq with hq | hq | hq | hq | hq + · obtain ⟨f, hf, -⟩ := hVs.pool q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l4 q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l3 q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l2 q hq; exact ⟨_, hf⟩ + · obtain ⟨f, hf, -⟩ := hPt.l1 q hq; exact ⟨_, hf⟩ + · rw [hnb] at hb; omega + · -- no endpoint-relation word + rintro (⟨e, he, h⟩ | ⟨r, hr, h⟩ | ⟨t, ht, h | h⟩ | ⟨wp, hwp, h⟩) + · exact miss e _ (J.zH.2.1 e he) ⟨by omega, by simp only [P1.HKind.bits]; omega⟩ + · exact miss r _ (J.zH.2.2.1 r hr) ⟨by omega, by simp only [P1.HKind.bits]; omega⟩ + · obtain ⟨h1, h2⟩ := h; unfold Endp.TOffN at h2 + exact miss t _ (J.zH.1 t ht) ⟨h1, by simp only [P1.HKind.bits]; omega⟩ + · unfold Endp.TL at h + exact miss t _ (J.zH.1 t ht) ⟨by omega, by simp only [P1.HKind.bits]; omega⟩ + · have := J.nodeB wp hwp c' (J.kern.csLt c' hc'); rw [← h] at this; omega + · -- no notification-relation word + rintro (⟨n, hn, h1, h2⟩ | ⟨t, ht, h⟩) + · exact miss n _ (J.zH.2.2.2 n hn) ⟨h1, by simp only [P1.HKind.bits]; omega⟩ + · have hv' := nfootT_val (hX.fin.1.1.1.placed.tcb t ht) h + exact miss t _ (J.zH.1 t ht) ⟨by omega, by simp only [P1.HKind.bits]; omega⟩ + · -- no SC word + intro sc hsc x n hk ⟨h1, h2⟩ + obtain ⟨-, hH⟩ := hJ.sc sc x n (J.c0 sc hsc) hk + exact miss sc _ hH ⟨h1, by simp only [P1.HKind.bits]; omega⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **A timer word, by value**: a TCB's timer fields (offsets 3528..3576), or +a timer-tree or `REPROGRAM` word of a core's node. -/ +theorem JAtZRB.tw_word {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : P1.TW st Z.T Cs s b) : + (∃ y ∈ Z.T, y.val + 3528 ≤ b.val ∧ b.val < y.val + 3576) ∨ + (∃ c ∈ Cs, ∃ o : Nat, (o = 104 ∨ o = 112) ∧ b.val = L.nodeBase s c + o) := by + have hNP : ∀ c ∈ Cs, P1.NodePlaced st Z.T s c := fun c hc => by + rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk c hc + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hT := hcq.tcbs + rcases hb with ⟨y, hy, K, h1, h2, rfl⟩ | ⟨c', hc', o, ho, rfl⟩ + · left + exact ⟨y, hy, by rw [Notif.tfield_val (hT y hy) (by omega)]; omega, + by rw [Notif.tfield_val (hT y hy) (by omega)]; omega⟩ + · right; exact ⟨c', hc', o.val, ho, P1.nodeAt_val (hNP c' hc') (by omega)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +theorem JAtZRB.tmw_word {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : TmW (L := L) Z.T Cs Z.c s b) : + (∃ y ∈ Z.T, y.val + 3528 ≤ b.val ∧ b.val < y.val + 3576) ∨ + (∃ c ∈ Cs, ∃ o : Nat, (o = 104 ∨ o = 112 ∨ o = 56) ∧ b.val = L.nodeBase s c + o) := by + rcases hb with hb | rfl + · rcases J.tw_word hb with h | ⟨c, hc, o, ho, e⟩ + · exact Or.inl h + · exact Or.inr ⟨c, hc, o, by omega, e⟩ + · right + obtain ⟨Q, R0, hcq, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hcq.cur ▸ J.kern.cpuCs + have hN : P1.NodePlaced st Z.T s Z.c := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk Z.c hcC + have h56 : layout.node.REPROGRAM.val = 56 := by simp [layout.node.REPROGRAM] + exact ⟨Z.c, hcC, 56, by omega, by rw [P1.nodeAt_val hN (by omega), h56]⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- A timer word is no node word off the timer-tree and `REPROGRAM` words. -/ +theorem JAtZRB.tmw_ne_node {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : TmW (L := L) Z.T Cs Z.c s b) {c : U64} (hc : c ∈ Cs) {o : U64} (ho : o.val < 4288) + (h1 : o.val ≠ 104) (h2 : o.val ≠ 112) (h3 : o.val ≠ 56) : b ≠ L.nodeAt s c o := fun e => by + have hN : P1.NodePlaced st Z.T s c := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk c hc + have hv := P1.nodeAt_val hN ho + have hnb : ∀ c, L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := fun _ => rfl + rw [← e] at hv + rcases J.tmw_word hb with ⟨y, hy, g1, g2⟩ | ⟨c', hc', o', ho', e'⟩ + · have := J.kern.hnd y _ (J.zH.1 y hy) c hc + simp only [P1.HKind.bits] at this; omega + · simp only [hnb] at hv e' + by_cases hcc : c' = c + · subst hcc; omega + · have : c'.val ≠ c.val := fun h => hcc (UScalar.eq_of_val_eq h) + rcases Nat.lt_or_gt_of_ne this with h | h + · have : c'.val * 4288 + 4288 ≤ c.val * 4288 := by nlinarith + omega + · have : c.val * 4288 + 4288 ≤ c'.val * 4288 := by nlinarith + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- **A timer word lies in no object of `H`** but a TCB's timer fields. -/ +theorem JAtZRB.tmw_obj {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : TmW (L := L) Z.T Cs Z.c s b) {p : U64} {k : P1.HKind} (hp : H p = some k) + (hin : p.val ≤ b.val ∧ b.val < p.val + 2 ^ k.bits) : + k = .tcb ∧ p ∈ Z.T ∧ p.val + 3528 ≤ b.val ∧ b.val < p.val + 3576 := by + have hH := J.kern.heap + rcases J.tmw_word hb with ⟨y, hy, g1, g2⟩ | ⟨c, hc, o, ho, e⟩ + · have hHy : H y = some .tcb := J.zH.1 y hy + by_cases hne : p = y + · subst hne; rw [hHy] at hp; cases hp; exact ⟨rfl, hy, g1, g2⟩ + · have := hH.disj p _ y _ hp hHy hne + have e12 : (2 : Nat) ^ P1.HKind.bits .tcb = 4096 := rfl + rw [e12] at this; omega + · have := J.kern.hnd p k hp c hc + rcases ho with rfl | rfl | rfl <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- A timer word is no word of a non-TCB object of `H`. -/ +theorem JAtZRB.tmw_ne_obj {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : TmW (L := L) Z.T Cs Z.c s b) {p : U64} {k : P1.HKind} (hp : H p = some k) (hk : k ≠ .tcb) + {w : U64} (hw : p.val ≤ w.val ∧ w.val < p.val + 2 ^ k.bits) : b ≠ w := fun e => by + subst e; exact hk (J.tmw_obj hb hp hw).1 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- A timer word is no TCB word off the timer fields. -/ +theorem JAtZRB.tmw_ne_tcb {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : TmW (L := L) Z.T Cs Z.c s b) {t : U64} (ht : t ∈ Z.T) {w : U64} {K : Nat} + (hwv : w.val = t.val + K) (hK : K < 3528 ∨ (3576 ≤ K ∧ K < 4096)) : b ≠ w := fun e => by + subst e + have := J.tmw_obj hb (J.zH.1 t ht) (by simp only [P1.HKind.bits]; omega) + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- `SlotsSame` across timer-word writes: the slots lie in `Z.th`'s objects, +which are `H`'s (`JPlaced`), at a TCB's cap slots (below offset 256). -/ +theorem JAtZRB.tmw_slots {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} (hw : P1.WritesIn st s s' (TmW (L := L) Z.T Cs Z.c s)) : SlotsSame Z.th (L.mem s) (L.mem s') := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hPl := hJ.pl + intro x hx + have hf := hx.fit hPl + have hal := hx.align hPl + obtain ⟨p, i, hi, rfl⟩ := id hx + have hv := hi.val hPl + -- the slot's 32 bytes lie in `p`'s object of `H`, below 256 if a TCB + have hin : ∃ k, H p = some k ∧ p.val + 32 * (i + 1) ≤ p.val + 2 ^ k.bits ∧ (k = .tcb → 32 * (i + 1) ≤ 256) := by + rcases hi with ⟨r, hp, hr⟩ | ⟨hp, hr⟩ + · refine ⟨_, hJ.cnode p r hp, ?_, fun e => by cases e⟩ + simp only [P1.HKind.bits] + have : 2 ^ (r + 5) = 32 * 2 ^ r := by rw [pow_add]; ring + rw [this] + have : i + 1 ≤ 2 ^ r := hr + nlinarith + · refine ⟨_, (hJ.tcb p).1 hp, ?_, fun _ => ?_⟩ + · simp only [P1.HKind.bits]; simp [tcbSlots] at hr; omega + · simp [tcbSlots] at hr; omega + obtain ⟨k, hHk, hsz, htcb⟩ := hin + have w : ∀ kk : U64, kk.val < 32 → kk.val % 8 = 0 → L.mem s' (wordOf (slotOf p i) kk) = L.mem s (wordOf (slotOf p i) kk) := by + intro kk hk hk8 + have hv' := wordOf_val (a := slotOf p i) (k := kk) (by omega) + refine hw.2.2 _ (by unfold P1.Aligned; rw [hv']; omega) fun hb => ?_ + have g := J.tmw_obj hb hHk (by rw [hv', hv]; omega) + have g2 := htcb g.1 + rw [hv', hv] at g + omega + refine ⟨?_, w 8#u64 (by decide) (by decide), w 16#u64 (by decide) (by decide), w 24#u64 (by decide) (by decide)⟩ + have := w 0#u64 (by decide) (by decide) + rwa [wordOf_zero] at this + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 2000000 in +/-- **The untyped caps' facts across timer-word writes** (`finW_utc`'s +argument: no timer word in an untyped cap's free part, the TCBs being typed +and the nodes outside every untyped region). -/ +theorem JAtZRB.tmw_utc {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (TmW (L := L) Z.T Cs Z.c s)) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (harch : σ'.archState = σ.archState) + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hfi := U.fiLe + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [hcdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [harch]; exact U.gReg (B g)⟩ + · intro g w hal h1 h2 + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases J.tmw_word hb with ⟨y, hy, g1, g2⟩ | ⟨c, hc, o, ho, e⟩ + · have hth : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y .tcb hth + simp only [objSize] at this; omega + · have := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).2.1 c hc + omega + · intro g h0 hf0 + rw [hcdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← horig]; exact h), by rw [hna, hDbg]; exact r3⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- (F1 stage 5, item 4) **`vsNamed` across a step with `VsKeep` and `ArchShr`**, `XTopAt` after at +`Z`'s VSpace sets (`XTopAt.vsNamedShr`). -/ +theorem JAtZRB.vsShr (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} (hX : XTopAt (L := L) AR (Z.mv G' Z.vs) s' a' σ') (hks : L.ksb s' = L.ksb s) + {th' : TypedHeap} (hK : Vs.VsKeep Z.th th' (L.mem s) (L.mem s')) (hsh : Vs.ArchShr σ σ') : + Z.vx = ∅ → Vs.VsNamed th' (L.mem s') (L.ksb s') := + fun h => J.xtop.vsNamedShr hX (J.carry.vsNamed h) J.vsc hks hK hsh + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- `vsShr` with the slots kept. -/ +theorem JAtZRB.vsSS (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {G' : MdbGhost} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} (hX : XTopAt (L := L) AR (Z.mv G' Z.vs) s' a' σ') (hks : L.ksb s' = L.ksb s) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hsh : Vs.ArchShr σ σ') : + Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s') := + J.vsShr hX hks (Vs.VsKeep.of_eq fun y hy => hSS.capV hy) hsh + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **`JAtZ` across timer-word writes** (module docstring): the timer words +written, `REPROGRAM` kept or set to 1, P1's `BlkOK` after given; a dead TCB's +deadline word stays 0 (`hTd`). -/ +theorem JAtZRB.tmw_step {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} (hw : P1.WritesIn st s s' (TmW (L := L) Z.T Cs Z.c s)) + (hR : L.mem s' (L.nodeAt s Z.c layout.node.REPROGRAM) = L.mem s (L.nodeAt s Z.c layout.node.REPROGRAM) ∨ + L.mem s' (L.nodeAt s Z.c layout.node.REPROGRAM) = 1#u64) + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s') + (hTd : ∀ q ∈ D, H q = some .tcb → L.mem s (P1.tdA q) = 0#u64 → L.mem s' (P1.tdA q) = 0#u64) : + ∃ r : Bool, JAtZRB (L := L) arI AR Z H Cs Ac Lst D s' a { σ with reprogramTimer := σ.reprogramTimer || r } := by + obtain ⟨r, hX'⟩ := J.xtop.tmStep J.otherTmOK hw hR + refine ⟨r, ?_⟩ + set σ' : Fl4Spec.State Unit := { σ with reprogramTimer := σ.reprogramTimer || r } with hσ' + have F : Sched.NFr true σ.curThread σ σ' := Sched.NFr.rt _ σ _ + have hS := J.xtop.fin.1.1.1 + have hS' : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hX'.fin.1.1.1 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hT := hc.tcbs + have hks : L.ksb s' = L.ksb s := hw.1 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH := J.kern.heap + -- the frame words + have k1 : ∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t) := fun t ht => + ⟨hw.mem_eq (mcpA_aligned hT ht) fun hb => J.tmw_ne_tcb hb ht (mcpA_val hT ht) (by omega) rfl, + hw.mem_eq (flagsA_aligned hT ht) fun hb => J.tmw_ne_tcb hb ht (flagsA_val hT ht) (by omega) rfl⟩ + have k2 : ∀ t ∈ Z.T, L.mem s' (P1.yieldA t) = L.mem s (P1.yieldA t) := fun t ht => + hw.mem_eq (yieldA_aligned hT ht) fun hb => J.tmw_ne_tcb hb ht (yieldA_val hT ht) (by omega) rfl + have kb : ∀ t ∈ Z.T, L.mem s' (tIpcBufA t) = L.mem s (tIpcBufA t) := fun t ht => + hw.mem_eq (tIpcBufA_aligned (hT t ht)) fun hb => J.tmw_ne_tcb hb ht (tIpcBufA_val (hT t ht)) (by omega) rfl + -- a word of an SC of `H` + have scw : ∀ q z, H q = some (.sc z) → ∀ w : U64, P1.Aligned w → q.val ≤ w.val → w.val < q.val + 2 ^ z → + L.mem s' w = L.mem s w := fun q z hq w hal h1 h2 => + hw.mem_eq hal fun hb => J.tmw_ne_obj hb hq (fun e => by cases e) (by simp only [P1.HKind.bits]; omega) rfl + have k3 : ∀ q ∈ Z.C, L.mem s' (P1.scReplyA q) = L.mem s (P1.scReplyA q) := fun q hq => by + obtain ⟨xq, nq, hkq, hin⟩ := screply_obj hS hq + have hHq := (hJ.sc q xq nq (J.c0 q hq) hkq).2 + exact scw q _ hHq _ (sc_reply_word hS hq).1 hin.1 hin.2 + have k4 : ∀ r ∈ Z.R, L.mem s' (P1.rPrevA r) = L.mem s (P1.rPrevA r) ∧ L.mem s' (P1.rNextA r) = L.mem s (P1.rNextA r) := by + intro r hr + have hal := J.rp.align r hr + have hHr : H r = some .reply := J.zH.2.2.1 r hr + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + hw.mem_eq (J.rp.aligned hr ha) fun hb => J.tmw_ne_obj hb hHr (fun e => by cases e) + (by simp only [P1.HKind.bits]; omega) rfl + exact ⟨k _ (Or.inl (rPrevA_val hal)), k _ (Or.inr (rNextA_val hal))⟩ + have hOut : ∀ q z, H q = some (.sc z) → ∀ K : U64, K.val % 8 = 0 → K.val < 128 → + L.mem s' (scW q K) = L.mem s (scW q K) := by + intro q z hq K hK8 hK + have hal := hH.align q _ hq + have hfit := hH.fits q _ hq + have hz := hH.scmin q z hq + simp only [P1.HKind.bits] at hal hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + exact scw q z hq _ (by show (scW q K).val % 8 = 0; rw [hv]; omega) (by omega) (by omega) + -- the debug word and the IRQ node + have hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := + hw.mem_eq J.kern.dwAl fun hb => J.tmw_ne_node hb J.kern.cs0 (by simp) + (by simp) (by simp) (by simp) rfl + have hirq : ∀ i : U64, i.val < 128 → capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := + irqNode_keep hw J.kern.ksbFit J.kern.ksbAl fun b hb => by + rcases J.tmw_word hb with ⟨y, hy, g1, g2⟩ | ⟨c, hc, o, ho, e⟩ + · have := hJ.ks y _ (J.zH.1 y hy) (fun _ e => by cases e); simp only [P1.HKind.bits] at this; omega + · simp only [P1.LawfulStore.nodeBase] at e; omega + have hSS := J.tmw_slots hw + -- the dead stay dead + have hAI : ActIdle σ' := F.actIdle J.act + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_hstep (G' := Z.G) hX' ⟨fun p => Or.inl rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ (fun q hq => k3 q hq) hOut + hAI.1 (J.yld.frame k2 F.ykeep) F.ykeep (J.jcov.dead q hq) + have ck := F.ckeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 F.khrb + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => F.szKeep q.bv y n hq) hks hirq (J.idleW_keep hS' F.sk.idle F.sk.isc).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS' F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D) + have hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := dbgKeep_of_writes J.blk hw fun b hb => + J.notDbg_tw (fun y hy w K hwv h1 h2 => J.tmw_ne_tcb hb hy hwv (Or.inr ⟨h1, h2⟩)) + (fun c hc => J.tmw_ne_node hb hc (by simp [P1.debug_val]) (by simp [P1.debug_val]) (by simp [P1.debug_val]) + (by simp [P1.debug_val])) + have R : JRestR (L := L) arI { Z with G := Z.G } H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, J.jcov_keepX hks hSS hdead (J.idleW_keep hS' F.sk.idle F.sk.isc) + (fun q hq h => h.keepT hKd (hTd q hq)), hkern, + J.tmw_utc hw hSS F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, J.inv.rt _, + (fun e => F.curInv (J.cur (by rw [← (F.act rfl).1]; exact e))), F.own J.own, F.off J.off, + hAI, F.bsa J.bsa, F.isorted J.isorted, + J.curRun.imp (fun e => by rw [(F.act rfl).1]; exact e) F.curRunW, hblk⟩ + exact J.cs_frameK (hvn := J.vsSS (G' := Z.G) hX' hks hSS (Vs.ArchShr.of_ao F.sk.arch fun q ao h => + F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e))) hks hX' F.kback F.ipcFr (J.carry.frame subset_rfl F.ipcFr F.ksz) (JCs.sameBR J hSS F.ksz) R (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) + hSS hKd F.sk.idle + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **The `REPROGRAM` write** at the end of a fired timer: `JAtZ` after, +`reprogram_timer` set. -/ +theorem JAtZRB.reprogramJ {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D + (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) a { σ with reprogramTimer := true } := by + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hN : P1.NodePlaced st Z.T s Z.c := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk Z.c hcC + have hal : P1.Aligned (L.nodeAt s Z.c layout.node.REPROGRAM) := + P1.nodeAt_aligned hN (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) + rw [hc.cur] + have hw : P1.WritesIn st s (L.wr s (L.nodeAt s Z.c layout.node.REPROGRAM) 1#u64) (TmW (L := L) Z.T Cs Z.c s) := + (P1.writesIn_wr hal).mono fun b hb => Or.inr hb + have hblk := P1.reprogram.tinv (v := 1#u64) (P1.BlkOK.tinv J.blk) + rw [hc.cur] at hblk + have hTd : ∀ q ∈ D, H q = some .tcb → L.mem s (P1.tdA q) = 0#u64 → + L.mem (L.wr s (L.nodeAt s Z.c layout.node.REPROGRAM) 1#u64) (P1.tdA q) = 0#u64 := fun q _ hH h0 => by + have hqT : q ∈ Z.T := by rw [J.tcbs_eq.1]; exact hH + have hT := hc.tcbs + exact ((P1.writesIn_wr hal).mem_eq (P1.field_aligned (hT q hqT) (K := 3528#u64) (by simp) (by simp)) + fun hb => P1.node_ne_field hN (o := layout.node.REPROGRAM) (by simp [layout.node.REPROGRAM]) hqT (hT q hqT) + (K := 3528#u64) (by simp) hb.symm).trans h0 + obtain ⟨r, J'⟩ := J.tmw_step hw (Or.inr (P1.LawfulStore.mem_wr_same hal)) hblk hTd + have hrp := J'.xtop.fin.1.1.1.repr + have e : (σ.reprogramTimer || r) = true := by + have e1 : (σ.reprogramTimer || r) = decide (Sched.nodeW st (L.wr s (L.nodeAt s Z.c layout.node.REPROGRAM) 1#u64) + Z.c layout.node.REPROGRAM ≠ 0#u64) := hrp + rw [e1]; unfold Sched.nodeW + rw [P1.nodeAt_of_ksb (hw.1) Z.c layout.node.REPROGRAM, P1.LawfulStore.mem_wr_same hal]; simp + rw [e] at J' + exact J' + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b70) **The `REPROGRAM` write is a native step** (a word of the current core's node, `SFoot`; the +spec's heap kept): `JAtZRB.reprogramJ`'s footprint (moved here from P3/ScChargeT in gen 42b71). -/ +theorem JAtZRB.reprogramN {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + NatFoot (L := L) Z s σ (L.wr s (L.nodeAt s (L.cpu s) layout.node.REPROGRAM) 1#u64) + { σ with reprogramTimer := true } := by + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + rw [hc.cur] + exact NatFoot.ofT J.xtop (WritesIn.tfoot_of_kfoot (WritesIn.kfoot_of_sfoot + (sfoot_wr_node hc.node (o := layout.node.REPROGRAM) (by simp) (by simp) 1#u64))) + (fun _ h => J.tfoot_nh h) (NatK.of_kheap rfl) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +open Fl4Proofs.P3.Notif (inClass inClassR released) + +/-- A notification cap's object in a slot is one of `JAtZ`'s notifications, +not at address 0 (`JPlaced.typed`, `JKern.h0`). (mmuAD gen 42b71) Over `JAtZRB` in place; `JAtZ.capN` its +corollary. -/ +theorem JAtZRB.capN {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {q : U64} + (hq : ∃ x b w r, IsSlot Z.th x ∧ capV (L.mem s) x = .Notification q b w r) : q ∈ Z.N ∧ q ≠ 0#u64 := by + obtain ⟨x, b, w, r, hx, hc⟩ := hq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH : H q = some .ntfn := ((hJ.typed x hx).2.1 q b w r hc).2 + refine ⟨J.hset.2.1 q hH, fun e => ?_⟩ + rw [e, J.kern.h0] at hH; cases hH + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P1 Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc + Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A notification cap in a slot names one of the notifications, not 0. (mmuAD gen 42b71) Over `JAtZRB` in place. -/ +theorem JAtZRB.slotN (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {sl : Fl4Spec.CslotPtr} + (hA : AbsSlot Z.th sl) {p b : U64} {w r : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Notification p b w r)) : p ∈ Z.N ∧ p ≠ 0#u64 := + J.capN ⟨cslotAddr sl, b, w, r, hA.isSlot, capV_of_capAt hc⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b75) **The timer line's two writes**: `REPROGRAM := 1` and `ARMED := 0` of the executing core's +node, the only words `handle_interrupt`'s timer arm writes. -/ +theorem JAtZRB.tmArm_w {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + P1.WritesIn st 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 s) layout.node.ARMED) 0#u64) + (fun b => b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM ∨ b = L.nodeAt s (L.cpu s) layout.node.ARMED) := by + obtain ⟨Q, R0, hc, -⟩ := J.xtop.fin.1.1.1.lists + have hN := hc.node + rw [hc.cur] + have hA1 : P1.Aligned (L.nodeAt s Z.c layout.node.REPROGRAM) := + P1.nodeAt_aligned hN (by simp [layout.node.REPROGRAM]) (by simp [layout.node.REPROGRAM]) + have hA2 : P1.Aligned (L.nodeAt s Z.c layout.node.ARMED) := + P1.nodeAt_aligned hN (by simp [layout.node.ARMED]) (by simp [layout.node.ARMED]) + rw [P1.nodeAt_of_ksb (L.ksb_wr _ _ _)] + exact (P1.writesIn_wr (s := s) hA1).trans + (P1.writesIn_wr (s := L.wr s (L.nodeAt s Z.c layout.node.REPROGRAM) 1#u64) hA2) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 16000000 in +/-- (mmuAD gen 42b72) **`handle_interrupt` from a `JAtZRB` state, over any spec signal `sig` and any post `Q`**: the +proof of gen 42b71's `JAtZRB.handleIrqJB` with the signal arm given (`hSigQ`: FL4's `send_signal` at the start against +`sig`, deterministic, `Q` after) and the native arms closed into `Q` (`hNQ`: a native run `JAtNR` from the start; +`hQms`: a machine-state change of the same memory and an `AR` arch state; `hQAR`: `Q` reads `AR`). The spec handler is +`hiS sig` (XTopJointIrq). `JAtZRB.handleIrqJB` is it at `sig := sendSignal`, `Q := JAtNR …` (P3/VmOracle's +`VmEptState.handleIrqVF` at `sendSignalVF`, `Q := VmEptState`). (gen 42b75) `hNQ` also gets the native arm's writes +(none, or the timer line's `REPROGRAM` / `ARMED`, `JAtZRB.tmArm_w`) and the spec heap kept (P3/VmOracle's +`VmEptState.handleIrqVFk` keeps `VmOrc` with them). -/ +theorem JAtZRB.handleIrqJG {MR : A → Fl4Spec.MachineState → Prop} + {sig : Fl4Spec.ObjRef → Fl4Spec.Badge → Fl4Spec.SMonad Unit Unit} {Q : S → A → Fl4Spec.State Unit → Prop} + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hR : MR a σ.machineState) + (hNQ : ∀ {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit}, JAtNR (L := L) arI AR Z H Cs s σ s1 a1 σ1 → + P1.WritesIn st s s1 (fun b => b = L.nodeAt s (L.cpu s) layout.node.REPROGRAM ∨ + b = L.nodeAt s (L.cpu s) layout.node.ARMED) → σ1.kheap = σ.kheap → Q s1 a1 σ1) + (hQms : ∀ {s1 : S} {a1 a2 : A} {σ1 : Fl4Spec.State Unit} (ms : Fl4Spec.MachineState), Q s1 a1 σ1 → + ms.underlyingMemory = σ1.machineState.underlyingMemory → AR a2 σ1.archState.x64CurrentCr3 → + Q s1 a2 { σ1 with machineState := ms }) + (hQAR : ∀ {s1 : S} {a1 : A} {σ1 : Fl4Spec.State Unit}, Q s1 a1 σ1 → AR a1 σ1.archState.x64CurrentCr3) + (hSigQ : ∀ p b : U64, p ∈ Z.N → notification.send_signal st arI s a p b ⦃ r => + ∃ σ' : Fl4Spec.State Unit, sig p.bv b.bv σ = ({((), σ')}, False) ∧ Q r.1 r.2 σ' ⦄) + (w : U64) (i : Fl4Spec.Irq) (hwi : w.val = i.toNat) (h126 : w.val ≠ 126) (h127 : w.val ≠ 127) + (hackAR : ∀ (a1 : A) (c : Fl4Spec.Cr3), AR a1 c → arI.ack_irq a1 w ⦃ a2 => AR a2 c ⦄) + (hMmask : ∀ (a0 a1 : A) (σ0 : Fl4Spec.State Unit), MR a0 σ0.machineState → arI.mask_irq a0 w true = ok a1 → + MR a1 (maskMs σ0 i true).machineState) + (hMack : ∀ (a1 : A) (ms : Fl4Spec.MachineState), MR a1 ms → arI.ack_irq a1 w ⦃ a2 => MR a2 ms ⦄) + (hMsig : ∀ (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) (n b : U64), MR a1 σ1.machineState → + notification.send_signal st arI s1 a1 n b ⦃ r => ∀ σ', ((), σ') ∈ (sig n.bv b.bv σ1).1 → + MR r.2 σ'.machineState ⦄) : + interrupt.handle_interrupt st arI s a w ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (hiS sig i σ).2) ∨ + ∃ z ∈ (hiS sig i σ).1, Q r.1 r.2 z.2 ∧ MR r.2 z.2.machineState ⦄ := by + have hX := J.xtop + have hIR : IrqRel (L.mem s) (L.ksb s) σ := hX.fin.1.1.2.2.2.1 + have hARa : AR a σ.archState.x64CurrentCr3 := hX.fin.1.1.2.2.2.2.2.2 + have hiw : (BitVec.ofNat 8 w.val : Fl4Spec.Irq) = i := by + apply BitVec.eq_of_toNat_eq; simp [hwi] + -- the acknowledgement keeps `JAtNR` and `MR` + have hack : ∀ (s1 : S) (a1 : A) (z : Unit × Fl4Spec.State Unit), + z ∈ (hiS sig i σ).1 → Q s1 a1 z.2 → + MR a1 z.2.machineState → + (do let a2 ← arI.ack_irq a1 w; ok (s1, a2)) ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (hiS sig i σ).2) ∨ + ∃ z ∈ (hiS sig i σ).1, Q r.1 r.2 z.2 ∧ + MR r.2 z.2.machineState ⦄ := by + intro s1 a1 z hz hJ1 hm1 + refine WP.spec_bind (WP.spec_and (hackAR a1 _ (hQAR hJ1)) (hMack a1 _ hm1)) ?_ + rintro a2 ⟨hA2, hM2⟩ + exact (WP.spec_ok _).2 (Or.inr ⟨z, hz, hQms z.2.machineState hJ1 rfl hA2, hM2⟩) + have J0 : Q s a σ := hNQ (JAtNR.of J (NatFoot.refl Z s σ)) (P1.WritesIn.refl s _) rfl + unfold interrupt.handle_interrupt + rw [irq_max_eq] + simp only [bind_ok] + by_cases hbig : w > 127#u64 + · -- above FL4's maximum + rw [if_pos hbig] + have hgt : i > Fl4Spec.maxIRQ := by + have : 127 < w.val := hbig + show (125 : BitVec 8) < i + rw [BitVec.lt_def]; simp; omega + obtain ⟨a1, ha1⟩ := maskIrq_total arI hAT a w true + rw [ha1] + simp only [bind_ok] + have hA1 : AR a1 σ.archState.x64CurrentCr3 := hARI.mask _ _ _ hARa (Or.inr (Or.inl ⟨w, true, ha1⟩)) + exact hack s a1 _ (mem_hiS_big sig hgt σ) (hQms _ J0 rfl hA1) (hMmask a a1 σ hR ha1) + · rw [if_neg hbig] + have hw : w.val ≤ 125 := by + have : ¬ 127 < w.val := hbig + omega + have hw128 : w.val < 128 := by omega + have hle : ¬ i > Fl4Spec.maxIRQ := by + show ¬ (125 : BitVec 8) < i + rw [BitVec.lt_def]; simp; omega + have hst : σ.interruptStates i = irqSt (L.mem s (irqStA (L.ksb s) w)) := by + rw [← hiw]; exact hIR.st w hw128 + rw [irq_state_eq'] + simp only [bind_ok, irqSig_eq, irqTimer_eq, irqIpi_eq, irqInactive_eq] + set v := L.mem s (irqStA (L.ksb s) w) with hv + -- the branches before the acknowledgement + have key : ∀ m : Result (S × A), m ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (hiS sig i σ).2) ∨ + ∃ z ∈ (hiS sig i σ).1, Q r.1 r.2 z.2 ∧ + MR r.2 z.2.machineState ⦄ → + (do let (s1, a1) ← m; let a2 ← arI.ack_irq a1 w; ok (s1, a2)) ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (hiS sig i σ).2) ∨ + ∃ z ∈ (hiS sig i σ).1, Q r.1 r.2 z.2 ∧ + MR r.2 z.2.machineState ⦄ := by + intro m hm + refine WP.spec_bind hm ?_ + rintro ⟨s1, a1⟩ (hf | ⟨z, hz, hJ1, hm1⟩) + · refine WP.spec_bind (hAT.ack_irq a1 w) ?_ + intro a2 _ + exact (WP.spec_ok _).2 (Or.inl hf) + · exact hack s1 a1 z hz hJ1 hm1 + refine key _ ?_ + by_cases h1 : v = 1#u64 + · -- a signal line + rw [if_pos h1] + have hsig : σ.interruptStates i = .irqSignal := by rw [hst, h1]; rfl + have hL : IntLayout Z.th (L.ksb s) := J.intL + have hnode := hL.node w hw128 + obtain ⟨cs, hkc, hwf, hsl⟩ := hX.rel.2.1.1.cnode _ 0 hnode + obtain ⟨c, ac, hcap, hcs, hCR⟩ := hsl [] rfl + rw [show slotAddr (nodeA (L.ksb s) w).bv (Fl4Spec.ofBl []) = nodeA (L.ksb s) w from + cslotAddr_nil _] at hcap + have hgc : Fl4Spec.getCap (Z := Unit) (σ.interruptIrqNode i, []) σ = ({(ac, σ)}, False) := by + rw [← hiw, hIR.node w hw128]; exact getCap_cnode hkc hwf hcs + have hac : ac = absCap c := hCR.abs + rw [irq_slot_eq] + simp only [bind_ok] + rw [slot_cap_eq, hcap] + simp only [bind_ok] + -- the mask after the cap's case + have hmask : ∀ (s3 : S) (a2 : A) (σ1 : Fl4Spec.State Unit), + ((), σ1) ∈ (Fl4Spec.NonDet.when (Fl4Spec.isNtfnCap ac ∧ Fl4Spec.AllowSend ∈ Fl4Spec.capRights ac) + (sig (Fl4Spec.objRefOf ac) (Fl4Spec.capEpBadge ac)) σ).1 → + Q s3 a2 σ1 → MR a2 σ1.machineState → + (do let a3 ← arI.mask_irq a2 w true; ok (s3, a3)) ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (hiS sig i σ).2) ∨ + ∃ z ∈ (hiS sig i σ).1, Q r.1 r.2 z.2 ∧ + MR r.2 z.2.machineState ⦄ := by + intro s3 a2 σ1 h1' hJ1 hm2 + obtain ⟨a3, ha3⟩ := maskIrq_total arI hAT a2 w true + rw [ha3] + simp only [bind_ok, WP.spec_ok] + have hA3 := hARI.mask _ _ _ (hQAR hJ1) (Or.inr (Or.inl ⟨w, true, ha3⟩)) + exact Or.inr ⟨((), maskMs σ1 i true), mem_hiS_signal sig hle hsig hgc h1', + hQms _ hJ1 rfl hA3, hMmask a2 a3 σ1 hm2 ha3⟩ + have hnone : ∀ (hP : ¬ (Fl4Spec.isNtfnCap ac ∧ Fl4Spec.AllowSend ∈ Fl4Spec.capRights ac)), + (do let a3 ← arI.mask_irq a w true; ok (s, a3)) ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (hiS sig i σ).2) ∨ + ∃ z ∈ (hiS sig i σ).1, Q r.1 r.2 z.2 ∧ + MR r.2 z.2.machineState ⦄ := + fun hP => hmask s a σ (mem_when_false hP _ σ) J0 hR + cases c with + | Notification p b sd rv => + cases sd with + | false => + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + exact hnone (by rw [hac]; simp [absCap, Fl4Spec.isNtfnCap, Fl4Spec.capRights, Fl4Spec.AllowSend, rightsOf]) + | true => + simp only [↓reduceIte] + have hP : Fl4Spec.isNtfnCap ac ∧ Fl4Spec.AllowSend ∈ Fl4Spec.capRights ac := by + rw [hac]; exact ⟨rfl, rfl⟩ + have hobj : Fl4Spec.objRefOf ac = p.bv := by rw [hac]; rfl + have hbd : Fl4Spec.capEpBadge ac = b.bv := by rw [hac]; rfl + have hwhen : ∀ σ1, ((), σ1) ∈ (sig p.bv b.bv σ).1 → + ((), σ1) ∈ (Fl4Spec.NonDet.when (Fl4Spec.isNtfnCap ac ∧ Fl4Spec.AllowSend ∈ Fl4Spec.capRights ac) + (sig (Fl4Spec.objRefOf ac) (Fl4Spec.capEpBadge ac)) σ).1 := by + intro σ1 h; unfold Fl4Spec.NonDet.when; rw [if_pos hP, hobj, hbd]; exact h + have hA : AbsSlot Z.th ((nodeA (L.ksb s) w).bv, []) := ⟨_, rfl, Or.inl hnode⟩ + have hpN := (J.slotN hA (by rw [cslotAddr_nil]; exact hcap)).1 + refine WP.spec_bind (WP.spec_and (hSigQ p b hpN) (hMsig s a σ p b hR)) ?_ + rintro ⟨s2, a2⟩ ⟨⟨σ', hsp, hQ'⟩, hm2⟩ + have hmem : ((), σ') ∈ (sig p.bv b.bv σ).1 := by rw [hsp]; rfl + exact hmask s2 a2 σ' (hwhen σ' hmem) hQ' (hm2 σ' hmem) + | _ => + simp only [bind_ok] + exact hnone (by rw [hac]; simp [absCap, Fl4Spec.isNtfnCap]) + rw [if_neg h1] + by_cases h2 : v = 2#u64 + · -- the timer line + rw [if_pos h2] + have htm : σ.interruptStates i = .irqTimer := by rw [hst, h2]; rfl + step with P1.LawfulStore.cur_cpu_spec as ⟨cc, ecc⟩ + step with P1.obj.node_set.spec as ⟨s2, es2⟩ + step with P1.obj.node_set.spec as ⟨s3, es3⟩ + subst es2 es3 ecc + have J2 := J.reprogramJ + obtain ⟨Ac3, Lst3, D3, J3⟩ := J2.armedJ (L.cpu_wr _ _ _) 0#u64 + try simp only [WP.spec_ok] + exact Or.inr ⟨_, mem_hiS_timer sig hle htm, + hNQ (JAtNR.of J3 (J.natTrans J.reprogramN (J2.armedN (L.cpu_wr _ _ _) 0#u64))) J.tmArm_w rfl, hR⟩ + rw [if_neg h2] + by_cases h3 : v = 3#u64 + · -- an IPI state on a line other than the IPI lines: reserved, nothing + rw [if_pos h3] + have hres : σ.interruptStates i = .irqReserved := by rw [hst, h3]; rfl + unfold smp.handle_ipi + rw [irq_resched_eq] + have hne : ¬ w = 127#u64 := fun e => h127 (by rw [e]; rfl) + simp only [bind_ok, hne, ↓reduceIte, WP.spec_ok] + exact Or.inr ⟨_, mem_hiS_reserved sig hle hres, J0, hR⟩ + rw [if_neg h3] + by_cases h0 : v = 0#u64 + · -- an inactive line: the spec fails + have hin : σ.interruptStates i = .irqInactive := by rw [hst, h0]; rfl + obtain ⟨a1, ha1⟩ := maskIrq_total arI hAT a w true + simp only [h0, ↓reduceIte, ha1, bind_ok, WP.spec_ok] + exact Or.inl ⟨hin, fail_hiS_inactive sig hle hin⟩ + · have hres : σ.interruptStates i = .irqReserved := by + rw [hst]; unfold irqSt; simp [h0, h1, h2] + simp only [h0, ↓reduceIte, bind_ok, WP.spec_ok] + exact Or.inr ⟨_, mem_hiS_reserved sig hle hres, J0, hR⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 2000000 in +/-- `IdleVt` after FL4's timers, the spec's by `fl4AwakenVF` (`idleVt_ofSlots`, the idle thread by +`Vs.crF_awakenVF`). -/ +theorem JAtZRB.idleVt_awakenVF (hAT : P0.ArchTotal arI) {on rc : Fl4Spec.ObjRef → Bool} {s : S} {a : A} + {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hIV : IdleVt σ) {s1 : S} {a1 : A} + (hfr : timer.fire st arI s a = ok (s1, a1)) {σ1 : Fl4Spec.State Unit} + (hF : ((), σ1) ∈ (Fl4Spec.fl4AwakenVF (Z := Unit) on rc σ).1) + {V1 : XVs} {Ac1 : Set U64} {Lst1 : U64 → List U64} {D1 : Set U64} + (J1 : JAtZRB (L := L) arI AR (Z.mv Z.G V1) H Cs Ac1 Lst1 D1 s1 a1 σ1) : IdleVt σ1 := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hNt : ∀ n ∈ P1.Heap.ntfns H, H n = some .ntfn := fun _ h => h + have hfk := spec_ok_post (P1.timer.fire.kw hAT (P1.ibKeep_holds hAT) hNt s a J.blk J.bufAtJ) hfr + simp only [WP.uncurry'_pair] at hfk + obtain ⟨⟨Q', R', hq⟩, -⟩ := J.blk + exact idleVt_ofSlots J.xtop J.jpl J.zH.1 J1.xtop + (slotsSame_of_footDU J.xtop.rel.1 (hJ.kwOutBlk J.blk) hq.tcbs hq.nodes hfk J.bufU_noSlot) + (congrArg (fun q => q.2.2.2.1) (Vs.crF_awakenVF hF)) hIV + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead across a step with the spec frame `NFr`** and the +footprint `FinW` (`deadP_keep`'s facts: a dead TCB's state and null bound +notification kept, a dead notification not waiting and unbound). -/ +theorem JAtZRB.deadP_nfr {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {qq : Bool} {r : Fl4Spec.ObjRef} {s3 : S} + {σ3 : Fl4Spec.State Unit} (F : Sched.NFr qq r σ σ3) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx h0 => ?_) + (fun x hx => F.othF hx (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)) + (fun x hx => ?_) + (fun x hx => ⟨x, F.othF hx (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e), rfl⟩) + (fun x n hx => ?_) (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.finW_scOut hw hz hqC hK8 hK) + F.sk.idle hAS hY3 (hYK.dk q.bv) hd + · -- a dead TCB: its state and (null) bound notification kept + obtain ⟨t, ht, e⟩ := J.obj.tcb q.bv x hx + have htq : t = q := P1.eq_of_bv e + subst htq + obtain ⟨-, -, d3, -, -⟩ := hd.1 (J.zH.1 t ht) + obtain ⟨x', hk', hR⟩ := J.xtop.ipc.tcb t ht + rw [hx] at hk'; cases hk' + have hbn : x.tcbBoundNotification = none := by + rw [hR.bound, show tBoundA t = P1.boundA t from rfl, d3, optRef_zero] + obtain ⟨st', bn, ar, hk3, hs, hbb⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, ?_, Or.inl rfl⟩ + · show st' = x.tcbState + rcases hs with ⟨e, -⟩ | ⟨-, e, -⟩ + · exact e + · rw [e] at h0; simp [tsCode] at h0 + · show bn = x.tcbBoundNotification + rcases hbb with e | e + · exact e + · rw [e, hbn] + · -- a dead notification: not waiting, unbound + obtain ⟨n', hn', e⟩ := J.obj.ntfn q.bv x hx + have hnq : n' = q := P1.eq_of_bv e + subst hnq + obtain ⟨d1, d2, -⟩ := hd.2.2.1 (J.zH.2.2.2 n' hn') + obtain ⟨x', hk', hR⟩ := J.xtop.ipc.toN.ntfn n' hn' + rw [hx] at hk'; cases hk' + obtain ⟨y, hy, -, hobj, hbd, hsc'⟩ := F.ntfnF hx + have hbn : x.ntfnBoundTcb = none := by + rw [hR.bound, show nBoundA n' = P1.ntfnBtA n' from rfl, d2, optRef_zero] + have hnw : ∀ l, x.ntfnObj ≠ .waitingNtfn l := by + intro l e + have hO := hR.obj + rw [e] at hO + exact d1 (by rw [epState_eq]; exact hO.1) + refine ⟨y, hy, (hobj hnw).imp id And.right, ?_, hsc'⟩ + rcases hbd with e | e + · exact e + · rw [e, hbn] + · -- an SC: its thread kept, its notification kept or cut + obtain ⟨rf, nt, hk3, -, hn⟩ := F.scF hx + exact ⟨_, hk3, Or.inl rfl, hn⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + +/-- **A word outside the TCBs and the nodes is no debug word.** -/ +theorem JAtZRB.notDbg {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hobj : ∀ p, H p = some .tcb → b.val < p.val ∨ p.val + 2 ^ P1.HKind.bits .tcb ≤ b.val) + (hnode : ∀ c ∈ Cs, b.val + 8 ≤ L.nodeBase s c ∨ L.nodeBase s c + 4288 ≤ b.val) : + ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := + J.notDbg' hobj fun c hc => by rcases hnode c hc with h | h <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + +/-- **A notification step's word (`NWrite`) is no debug word.** -/ +theorem JAtZRB.nwrite_notDbg {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (hb : Notif.NWrite Z.N Z.T b) : + ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := by + have hT : P1.TcbPlaced Z.T := J.ip.e.tcb + have hH := J.kern.heap + rcases hb with ⟨n, hn, g1, g2⟩ | ⟨t, ht, h⟩ + · have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + refine J.notDbg (fun p hp => ?_) (fun c hc => ?_) + · have hpn : p ≠ n := fun e => by rw [e, hHn] at hp; cases hp + have e := hH.disj p _ n _ hp hHn hpn + simp only [P1.HKind.bits] at e ⊢ + omega + · have := J.kern.hnd n _ hHn c hc + simp only [P1.HKind.bits] at this + omega + · have hv := nfootT_val (hT t ht) (b := b) (by + rcases h 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.inr (Or.inr (Or.inr (Or.inl h)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h))))) + refine J.notDbg_tw (fun y hy w K hwv h1 h2 => ?_) (fun c hc => ?_) + · rcases hv with e | e | e | e | e | e <;> exact tcbOff_ne hT ht hy e hwv (by omega) (by omega) (by omega) + · intro e + have hN : P1.NodePlaced st Z.T s c := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk c hc + have hnv := P1.nodeAt_val hN (off := layout.node.DEBUG_TCBS) (by simp) + have := J.kern.hnd t _ (J.zH.1 t ht) c hc + simp only [P1.HKind.bits] at this + rw [← e] at hnv + have hd88 : layout.node.DEBUG_TCBS.val = 88 := P1.debug_val + rcases hv with e' | e' | e' | e' | e' | e' <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **A badge-only signal keeps `JAtZ`** (module docstring): the spec's +outcome `updK σ n (.notification y)`, `y` the notification with a non-waiting +object (it was non-waiting), FL4's side `SingleRelI` at that outcome. -/ +theorem JAtZRB.signal_badge_post {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) + {x y : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hxw : ∀ l, x.ntfnObj ≠ .waitingNtfn l) (hyw : ∀ l, y.ntfnObj ≠ .waitingNtfn l) + (hyb : y.ntfnBoundTcb = x.ntfnBoundTcb) (hys : y.ntfnSc = x.ntfnSc) + {spec : Set (Unit × Fl4Spec.State Unit) × Prop} + (hspec : spec = ({((), updK σ n.bv (.notification y))}, False)) + {s' : S} (hrel : SingleRelI (st := st) Z.N Z.T Z.E Z.R Z.B s σ spec s') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s') : + ∃ σ' : Fl4Spec.State Unit, spec = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s' a σ' := by + obtain ⟨σ3, hsp, hIR3, hNS, hW, -⟩ := hrel + have e3 : σ3 = updK σ n.bv (.notification y) := by + have h1 := (Prod.mk.inj (hsp.symm.trans hspec)).1 + exact congrArg Prod.snd (Set.singleton_injective h1) + subst e3 + set σ3 := updK σ n.bv (.notification y) with hσ3 + -- the spec's frame + have hN : NObj x.ntfnObj y.ntfnObj := Or.inr ⟨hxw, hyw⟩ + have F : NFr true n.bv σ σ3 := NFr.upd hk (Or.inr (Or.inr ⟨x, y, rfl, rfl, rfl, hN.queue, fun _ => hN, + Or.inl hys, fun _ => hN, Or.inl hyb⟩)) + have hI3 : Inv σ3 := J.inv.updNtfn hk fun p hp => J.inv.ntfnSc n.bv x p hk (hys ▸ hp) + -- FL4's side: `IRel` after + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT := hS.placed.tcb + have h := hI.2 + have hSRT : SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c) s' σ3 := + ⟨h.1.stableN h.2.1.placed hW hNS, h.2.1.stable hW hNS, h.2.2.mono hW.1 (SzKeep.ntfnStep hNS)⟩ + have hI3' : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ3 := ⟨hIR3, hSRT⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ3 := hSRT.2.1 + have hks : L.ksb s' = L.ksb s := hW.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + slotsSame_of_iw J.xtop.rel.1 J.xtop.rel.2.2.2.2.2.1 J.xtop.rel.2.2.2.2.2.2 hW + fun b hb => Or.inl (NWrite.iwords hT hb) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hI3' hAR hSS F.sk.cdt F.sk.orig F.ksub hks + have hw : P1.WritesIn st s s' (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) := hW.mono fun b hb => NWrite.finW hT hb + obtain ⟨k1, k2, kb, k3, k4⟩ := J.finW_keeps hw + have ck := F.ckeep + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hks ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 F.khrb + -- core 0's debug word: no notification or TCB word + have hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by + have hN0 : P1.NodePlaced st Z.T s 0#u64 := by rw [J.tcbs_eq.1]; exact P1.TInv.nodes J.blk 0#u64 J.kern.cs0 + have hv := P1.nodeAt_val hN0 (off := layout.node.DEBUG_TCBS) (by simp) + have hd88 : layout.node.DEBUG_TCBS.val = 88 := P1.debug_val + refine hW.mem_eq J.kern.dwAl fun hb => ?_ + rcases NWrite.iwords (E := Z.E) (R := Z.R) (B := Z.B) hT hb with ⟨t, ht, g1, g2⟩ | ⟨e, he, g1, g2⟩ | + ⟨r, hr, g1, g2⟩ | ⟨m, hm, g1, g2⟩ | ⟨wp, hwp, g1, g2⟩ + · have := J.kern.hnd t _ (J.zH.1 t ht) 0#u64 J.kern.cs0 + simp only [P1.HKind.bits] at this; omega + · have := J.kern.hnd e _ (J.zH.2.1 e he) 0#u64 J.kern.cs0 + simp only [P1.HKind.bits] at this; omega + · have := J.kern.hnd r _ (J.zH.2.2.1 r hr) 0#u64 J.kern.cs0 + simp only [P1.HKind.bits] at this; omega + · have := J.kern.hnd m _ (J.zH.2.2.2 m hm) 0#u64 J.kern.cs0 + simp only [P1.HKind.bits] at this; omega + · have := J.nodeB wp hwp 0#u64 (by simp); omega + -- the scheduler fields + have hAI3 : ActIdle σ3 := F.actIdle J.act + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_nfr F hS3 hIR3 hw k3 hAI3.1 (J.yld.frame k2 F.ykeep) F.ykeep + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ3 := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.finW_irq hw) (J.idleW_keep hS3 F.sk.idle F.sk.isc).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D) + have hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := + dbgKeep_of_writes J.blk hW fun b hb => J.nwrite_notDbg hb + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ3 := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, J.jcov_keep hks hSS hdead (J.idleW_keep hS3 F.sk.idle F.sk.isc) + (DXKeep.of_dbg hKd (J.tdkeep_of hw fun _ hb => Ipc.FinW.ntd J.xtop.fin.1.1.1 hb)), hkern, + J.finW_utc hw hSS F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, hI3, + (fun e => F.curInv (J.cur (by rw [← (F.act rfl).1]; exact e))), F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa, F.isorted J.isorted, + J.curRun.imp (fun e => by rw [(F.act rfl).1]; exact e) F.curRunW, hblk⟩ + have hC0 := JCs.sameBR J hSS F.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ3 := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact ⟨σ3, hspec, rfl, J.cs_frameI hks hX3 hw (fun b hb => Ipc.FinW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => Ipc.FinW.nv J.xtop.fin.1.1.1 hb) F.sk.arch (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)) F.szKeep hSS F.kback F.ipcFr hC R + hKd F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) J.zD⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `CnW` word is not the VCPU-list head word** (moved here from P3/JAtZV3FaultBlk in gen 42b46). -/ +theorem JAtZRB.cnW_nh (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} + (h : CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b) : b ≠ P1.vHeadA st s := by + rcases h with hf | ⟨e, he, hb⟩ | ⟨r, hr, hb⟩ + · exact J.finW_nh hf + · intro eb + subst eb + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + have d := J.kern.hks e _ (J.zH.2.1 e he) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + unfold Endp.EW at hb + omega + · intro eb + subst eb + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + have d := J.kern.hks r _ (J.zH.2.2.1 r hr) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `CnX` word is in no other object of `H`** (module docstring): not in +an object `p` of `H` other than an endpoint or reply, nor in a reply's words +other than its `TCB` word. -/ +theorem JAtZRB.cnX_not (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (hb : CnX Z.E Z.R b) + {p : U64} {k : P1.HKind} (hp : H p = some k) (h1 : p.val ≤ b.val) (h2 : b.val < p.val + 2 ^ k.bits) + (hk : (k ≠ .ep ∧ k ≠ .reply) ∨ (k = .reply ∧ b.val ≠ p.val)) : False := by + have hH := J.kern.heap + obtain ⟨q, kq, hq, g1, g2, hkq⟩ : ∃ q kq, H q = some kq ∧ q.val ≤ b.val ∧ b.val < q.val + 2 ^ kq.bits ∧ + (kq = .ep ∨ (kq = .reply ∧ b.val = q.val)) := by + rcases hb with ⟨e, he, h⟩ | ⟨r, hr, h⟩ + · refine ⟨e, .ep, J.zH.2.1 e he, ?_, ?_, Or.inl rfl⟩ <;> (unfold EW at h; (try simp only [P1.HKind.bits]); omega) + · exact ⟨r, .reply, J.zH.2.2.1 r hr, by omega, by simp only [P1.HKind.bits]; omega, Or.inr ⟨rfl, h⟩⟩ + by_cases e : p = q + · subst e + rw [hp] at hq; cases hq + rcases hk with ⟨k1, k2⟩ | ⟨k1, k2⟩ <;> rcases hkq with k3 | ⟨k3, k4⟩ + · exact k1 k3 + · exact k2 k3 + · rw [k1] at k3; cases k3 + · exact k2 k4 + · have := hH.disj p k q kq hp hq e + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `CnW` word is no TCB's VCPU word** (KENTRY-PLAN g2b). -/ +theorem JAtZRB.cnW_nv (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} + (hb : CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b) : ∀ t ∈ Z.T, b ≠ tVcpuA t := by + have hS := J.xtop.fin.1.1.1 + intro t ht + rcases hb with hb | hx + · exact FinW.nv hS hb t ht + · rintro rfl + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hv : (tVcpuA t).val = t.val + 3664 := tfield_val (hc.tcbs t ht) (K := 3664#u64) (by simp) + exact J.cnX_not hx (J.zH.1 t ht) (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inl ⟨fun e => (by cases e), fun e => (by cases e)⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The threads all sit on the notification's SC's core** (P1's +`DonCore`): the TCBs on core `Z.c` (`SchedCore.aff`), the notification's SC, +when it has one, in `Z.C` (`NSC`) and so on core `Z.c` (`SchedRel.core`). A +wake donation then moves no thread across cores. -/ +theorem JAtZRB.donCore {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) : + P1.DonCore st (P1.Heap.tcbs H) (P1.Heap.scs H) n s := by + intro x _ h0 e y hy + have hSR := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hSR.lists + have hyT : y ∈ Z.T := by rw [J.tcbs_eq.1]; exact hy + have hxC : x ∈ Z.C := by + have h0' : L.mem s (nScA n) ≠ 0#u64 := by rw [show nScA n = P1.ntfnScA n from rfl, e]; exact h0 + have := NSC.mem J.nsc J.xtop.fin.1.1.2.2.1 hn h0' + rwa [show nScA n = P1.ntfnScA n from rfl, e] at this + rw [hc.aff y hyT, Fl4Proofs.P3.Sched.scCoreA_eq, hSR.core x hxC] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A thread in a state other than `inactive` is not dead. -/ +theorem JAtZRB.not_dead_of_st (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (htT : t ∈ Z.T) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hst : tsCode xt.tcbState ≠ 0) : + ∀ q ∈ D, q.bv ≠ t.bv := by + intro q hq eq + have hqt : q = t := P1.eq_of_bv eq + subst hqt + have d1 := ((J.jcov.dead q hq).1 (J.zH.1 q htT)).1 + have hI := J.xtop.irelK J.ks J.buf + have h3 := (hI.1.toN.at_tcb htT hkt).st + rw [P1.stA_eq, ← tStA_eq] at d1 + rw [d1] at h3 + unfold sched.TS_INACTIVE at h3 + exact hst (by rw [← h3]; rfl) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- `RecvReplyHyp`'s conclusion at one `JAtZRB` state (gen 42b45: moved here from P3/JAtZV3FaultRecvB). -/ +theorem JAtZRB.recvReplyB (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∀ d ∈ Z.R, ∀ xr t xt, σ.kheap d.bv = some (.reply xr) → + xr.replyTcb = some t → σ.kheap t = some (.tcb xt) → Fl4Spec.receiveBlocked xt.tcbState = true → + Detached Z.C Lst d ∧ L.mem s (rNextA d) = 0#u64 ∧ L.mem s (rPrevA d) = 0#u64 := by + intro d hd xr t xt hx ht hxt hrb + obtain ⟨hn, hp⟩ := J.rrecv d hd ⟨xr, t, xt, hx, ht, hxt, hrb⟩ + exact ⟨detached_of_next0 J.call (fun r hr => (J.rp.align r hr).2.1) hn, hn, hp⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The IRQ node's caps are kept** by a `CnW` step. -/ +theorem JAtZRB.cnW_irq (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) (i : U64) (hi : i.val < 128) : + capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hkf : (L.ksb s).val + 13568 ≤ 2 ^ 64 := J.kern.ksbFit + refine irqNode_keep hw hkf J.kern.ksbAl (fun b hb => ?_) i hi + rcases hb with hb | hb + · rcases finW_cases hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have := hJ.ks y _ (J.zH.1 y hy) (fun _ e => by cases e); simp only [P1.HKind.bits] at this; omega + · simp only [P1.LawfulStore.nodeBase] at g1; omega + · have := J.nk n hn; omega + · have := (hS.scOut sc hsc x n hk b ⟨g1, g2⟩).2.2.2; omega + · rcases hb with ⟨e, he, h⟩ | ⟨r, hr, h⟩ + · have := hJ.ks e _ (J.zH.2.1 e he) (fun _ e => by cases e); simp only [P1.HKind.bits] at this; unfold EW at h; omega + · have := hJ.ks r _ (J.zH.2.2.1 r hr) (fun _ e => by cases e); simp only [P1.HKind.bits] at this; omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `CnW` word is no TCB's flags word** (KENTRY-PLAN g5d). -/ +theorem JAtZRB.cnW_nf (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} + (hb : CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b) : ∀ t ∈ Z.T, b ≠ flagsA t := by + have hS := J.xtop.fin.1.1.1 + intro t ht + rcases hb with hb | hx + · exact FinW.nf hS hb t ht + · rintro rfl + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hv : (flagsA t).val = t.val + 3456 := tfield_val (hc.tcbs t ht) (K := 3456#u64) (by simp) + exact J.cnX_not hx (J.zH.1 t ht) (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inl ⟨fun e => (by cases e), fun e => (by cases e)⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The frame words a `CnW` step keeps** (`finW_keeps`). -/ +theorem JAtZRB.cnW_keeps (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) : + (∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (tIpcBufA t) = L.mem s (tIpcBufA t)) ∧ + (∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q)) ∧ + (∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r)) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hT := hc.tcbs + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have tsw : ∀ b, FinW (st := st) Z.T Z.N Z.C Z.c s σ b → TSW Z.T Z.C b := fun b hb => FinW.tsw hS hb + have tcbX : ∀ t ∈ Z.T, ∀ b : U64, b.val = t.val + 3440 ∨ b.val = t.val + 3472 ∨ b.val = t.val + 3512 → + ¬ CnX Z.E Z.R b := fun t ht b hv hx => + J.cnX_not hx (J.zH.1 t ht) (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inl ⟨fun e => (by cases e), fun e => (by cases e)⟩) + refine ⟨fun t ht => ⟨hw.mem_eq (mcpA_aligned hT ht) fun hb => ?_, + hw.mem_eq (flagsA_aligned hT ht) fun hb => J.cnW_nf hb t ht rfl⟩, + fun t ht => hw.mem_eq (yieldA_aligned hT ht) fun hb => ?_, + fun t ht => hw.mem_eq (tIpcBufA_aligned (hT t ht)) fun hb => ?_, + fun q hq => hw.mem_eq (sc_reply_word hS hq).1 fun hb => ?_, fun r hr => ?_⟩ + · rcases hb with hb | hb + · exact ((tsw _ hb).1 t ht).1 (mcpA_val hT ht) + · exact tcbX t ht _ (Or.inl (mcpA_val hT ht)) hb + · rcases hb with hb | hb + · exact ((tsw _ hb).1 t ht).2.1 (yieldA_val hT ht) + · exact tcbX t ht _ (Or.inr (Or.inl (yieldA_val hT ht))) hb + · rcases hb with hb | hb + · exact ((tsw _ hb).1 t ht).2.2 (tIpcBufA_val (hT t ht)) + · exact tcbX t ht _ (Or.inr (Or.inr (tIpcBufA_val (hT t ht)))) hb + · rcases hb with hb | hb + · exact (tsw _ hb).2 q hq rfl + · obtain ⟨Q', R', -, hs⟩ := hS.lists + obtain ⟨y, n, hky, -⟩ := hs.scs q hq + have hHq := (hJ.sc q y n (J.c0 q hq) hky).2 + have hz := J.kern.heap.scmin q _ hHq + have h128 : 2 ^ 7 ≤ 2 ^ (n + Fl4Spec.minSchedContextBits) := Nat.pow_le_pow_right (by norm_num) hz + have hv := (sc_reply_word hS hq).2.1 + exact J.cnX_not hb hHq (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inl ⟨fun e => (by cases e), fun e => (by cases e)⟩) + · have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have k : ∀ a' : U64, (a'.val = r.val + 8 ∨ a'.val = r.val + 16) → L.mem s' a' = L.mem s a' := fun a' ha => + hw.mem_eq (J.rp.aligned hr ha) fun hb => by + rcases hb with (hb | ⟨y, hy, K, -, hK, -, -, -, e⟩) | hb + · exact hb.ne_rword hS J.rp hr ha rfl + · have := J.rp.tcb r hr y hy; omega + · exact J.cnX_not hb (J.zH.2.2.1 r hr) (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inr ⟨rfl, by omega⟩) + exact ⟨k _ (Or.inl h8), k _ (Or.inr h16)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `CnW` word is no TCB's deadline word** (KENTRY-PLAN jc6e). -/ +theorem JAtZRB.cnW_ntd (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} + (hb : CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b) : ∀ t ∈ Z.T, b ≠ P1.tdA t := by + have hS := J.xtop.fin.1.1.1 + intro t ht + rcases hb with hb | hx + · exact FinW.ntd hS hb t ht + · rintro rfl + obtain ⟨Q, R0, hc, -⟩ := hS.lists + have hv : (P1.tdA t).val = t.val + 3528 := tfield_val (hc.tcbs t ht) (K := 3528#u64) (by simp) + exact J.cnX_not hx (J.zH.1 t ht) (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inl ⟨fun e => (by cases e), fun e => (by cases e)⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A `CnW` step keeps every deadline word** (KENTRY-PLAN jc6e). -/ +theorem JAtZRB.cnW_td (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) : Sched.TdKeep (L := L) H s s' := + J.tdkeep_of hw fun _ hb => J.cnW_ntd hb + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 1000000 in +/-- **The untyped caps' facts after a `CnW` step** (`finW_utc`; the endpoints +and replies are spec objects, outside the free parts, `SpecFree`). -/ +theorem JAtZRB.cnW_utc (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (hcdt : σ'.cdt = σ.cdt) (horig : σ'.isOriginalCap = σ.isOriginalCap) + (harch : σ'.archState = σ.archState) + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hfi := U.fiLe + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [hcdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [harch]; exact U.gReg (B g)⟩ + · intro g w hal h1 h2 + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases hb with hb | hb + · rcases finW_cases hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hth : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y .tcb hth + simp only [objSize] at this; omega + · have := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).2.1 Z.c hcC + omega + · obtain ⟨x, hx, -⟩ := hR.ntfn n hn + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) n.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.ntfnBits, UScalar.bv_toNat] at this + omega + · have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) sc.bv _ hk + simp only [Fl4Spec.objBits, UScalar.bv_toNat] at this + rw [Nat.add_comm Fl4Spec.minSchedContextBits n] at this + omega + · rcases hb with ⟨e, he, h⟩ | ⟨r, hr, h⟩ + · obtain ⟨x, hx, -⟩ := J.xtop.ipc.toE.ep e he + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) e.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.endpointBits, UScalar.bv_toNat] at this + unfold EW at h + omega + · obtain ⟨x, hx, -⟩ := J.xtop.ipc.toE.reply r hr + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) r.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.replyBits, UScalar.bv_toNat] at this + omega + · intro g h0 hf0 + rw [hcdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← horig]; exact h), by rw [hna, hDbg]; exact r3⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **The header words of `H`'s SCs outside `Z.C` are kept** by a `CnW` step. -/ +theorem JAtZRB.cnW_scOut (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) {q : U64} {z : Nat} + (hq : H q = some (.sc z)) (hqC : q ∉ Z.C) {K : U64} (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + L.mem s' (scW q K) = L.mem s (scW q K) := by + have hH := J.kern.heap + have hal := hH.align q _ hq + have hz := hH.scmin q z hq + simp only [P1.HKind.bits] at hal + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal] + have hfit := hH.fits q _ hq + simp only [P1.HKind.bits] at hfit + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hS.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + refine hw.mem_eq (by show (scW q K).val % 8 = 0; rw [hv]; omega) fun hb => ?_ + rcases hb with hb | hb + · rcases finW_cases hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hHy : H y = some .tcb := J.zH.1 y hy + have hne : q ≠ y := fun e => by rw [e, hHy] at hq; cases hq + have := hH.disj q _ y _ hq hHy hne + simp only [P1.HKind.bits] at this + omega + · have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have := J.kern.hnd q _ hq Z.c hcC + simp only [P1.HKind.bits] at this + omega + · have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + have hne : q ≠ n := fun e => by rw [e, hHn] at hq; cases hq + have := hH.disj q _ n _ hq hHn hne + simp only [P1.HKind.bits] at this + omega + · have hHs := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have hne : q ≠ sc := fun e => hqC (e ▸ hsc) + have := hH.disj q _ sc _ hq hHs hne + simp only [P1.HKind.bits] at this + omega + · exact J.cnX_not hb hq (by omega) (by simp only [P1.HKind.bits]; omega) + (Or.inl ⟨fun e => (by cases e), fun e => (by cases e)⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **A notification's SC is bound back to it** (P1's `ObjsOK.nsc` through +the notification and SC relations). -/ +theorem JAtZRB.ntfn_link {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) : + ∀ xn q, σ.kheap n.bv = some (.notification xn) → xn.ntfnSc = some q → + ∃ y m, σ.kheap q = some (.schedContext y m) ∧ y.scNtfn = some n.bv := by + intro xn q hk hq + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hx := J.xtop.ipc.toN.at_ntfn hn hk + obtain ⟨sc, hscC, hscv⟩ := J.nsc n hn xn q hk hq + rw [hx.sc] at hq + have h0 : L.mem s (nScA n) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hq; cases hq + have hqv : q = (L.mem s (nScA n)).bv := by rw [Sched.optRef_ne h0] at hq; exact (Option.some.inj hq).symm + have hsc : sc = L.mem s (nScA n) := P1.eq_of_bv (hscv.trans hqv) + obtain ⟨-, hback⟩ := J.blk.2.2.1.2.2.nsc n (J.zH.2.2.2 n hn) h0 + obtain ⟨x, m, hkx, hR⟩ := hs.scs sc hscC + refine ⟨x, m, by rw [← hscv]; exact hkx, ?_⟩ + rw [hR.ntfn, scW_ntfn, hsc] + have hback' : L.mem s (scNtfnA (L.mem s (nScA n))) = n := hback + have hn0 : n ≠ 0#u64 := fun e => by have := J.zH.2.2.2 n hn; rw [e, J.kern.h0] at this; cases this + rw [hback', Sched.optRef_ne hn0] + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A notification's SC is not dead (bound back to the notification, +`JAtZ.ntfn_link`; a dead SC's notification word is `0`). -/ +theorem JAtZRB.ntfnSc_not_dead (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {q : U64} {y : Fl4Spec.SchedContext} + {k : Nat} (hq : σ.kheap q.bv = some (.schedContext y k)) {o : Fl4Spec.ObjRef} {xo : Fl4Spec.Notification} + (ho : σ.kheap o = some (.notification xo)) (hsc : xo.ntfnSc = some q.bv) : ¬ DeadP (L := L) H s q := by + intro hd + obtain ⟨n', hn', en⟩ := J.obj.ntfn o xo ho + subst en + obtain ⟨y', m', hky, hyn⟩ := J.ntfn_link hn' xo q.bv ho hsc + rw [hq] at hky; cases hky + obtain ⟨c', hc', ec⟩ := J.obj.sc q.bv y k hq + have hcq : c' = q := P1.eq_of_bv ec + subst hcq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + obtain ⟨-, hH⟩ := hJ.sc c' y k (J.c0 c' hc') hq + obtain ⟨-, -, d3, -⟩ := hd.2.2.2.2 _ hH + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨x, m, hkx, hR⟩ := hs.scs c' hc' + rw [hq] at hkx; cases hkx + have h1 := hR.ntfn + rw [scW_ntfn] at h1 + rw [show L.mem s (scNtfnA c') = 0#u64 from d3, optRef_zero, hyn] at h1 + cases h1 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead across a `DWFr` step** with the footprint `CnW` +(`deadP_cfr`; `t` was blocked, so not dead; an SC whose thread changes is a +notification's SC, so not dead). -/ +theorem JAtZRB.deadP_dfr (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {r t : Fl4Spec.ObjRef} {s3 : S} + {σ3 : Fl4Spec.State Unit} (F : DWFr r t σ σ3) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) + (ht : ∀ q ∈ D, q.bv ≠ t) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx h0 => ?_) (fun x hx => ?_) (fun x hx => ?_) (fun x hx => ?_) + (fun x n hx => ?_) (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hw hz hqC hK8 hK) + F.sk.idle hAS hY3 (hYK.dk q.bv) hd + · -- a dead TCB: its state, (null) bound notification and SC kept + obtain ⟨t', ht', e⟩ := J.obj.tcb q.bv x hx + have htq : t' = q := P1.eq_of_bv e + subst htq + obtain ⟨-, -, d3, -, -⟩ := hd.1 (J.zH.1 t' ht') + obtain ⟨x', hk', hR⟩ := J.xtop.ipc.tcb t' ht' + rw [hx] at hk'; cases hk' + have hbn : x.tcbBoundNotification = none := by + rw [hR.bound, show tBoundA t' = P1.boundA t' from rfl, d3, optRef_zero] + obtain ⟨st', bn, fl, ar, sc, hk3, hs⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, ?_, ?_⟩ + · show st' = x.tcbState + rcases hs with ⟨⟨e, -⟩ | ⟨-, e, -⟩, -, -⟩ | ⟨-, e, -⟩ + · exact e + · rw [e] at h0; simp [tsCode] at h0 + · exact absurd e (ht t' hq) + · show bn = x.tcbBoundNotification + rcases hs with ⟨-, -, e | e⟩ | ⟨-, e, -⟩ + · exact e + · rw [e, hbn] + · exact absurd e (ht t' hq) + · show sc = x.tcbSchedContext ∨ sc = none + by_cases e : sc = x.tcbSchedContext + · exact Or.inl e + · exact absurd (F.tsc _ x _ hx hk3 e).1 (ht t' hq) + · -- a dead endpoint: idle, so holding nobody + obtain ⟨y, hy, hc⟩ := F.epF hx + rcases hc with rfl | ⟨-, hm⟩ + · exact hy + · exfalso + obtain ⟨e', he', e⟩ := J.obj.ep q.bv x hx + have heq : e' = q := P1.eq_of_bv e + subst heq + have d2 := hd.2.1 (J.zH.2.1 e' he') + obtain ⟨x', hk', hO⟩ := J.xtop.ipc.toE.ep e' he' + rw [hx] at hk'; cases hk' + rw [epState_eq] at d2 + cases x with + | idleEP => simp [Fl4Spec.Endpoint.epQueue] at hm + | sendEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + | recvEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + · -- a dead notification: not waiting, unbound + obtain ⟨n', hn', e⟩ := J.obj.ntfn q.bv x hx + have hnq : n' = q := P1.eq_of_bv e + subst hnq + obtain ⟨d1, d2, -⟩ := hd.2.2.1 (J.zH.2.2.2 n' hn') + obtain ⟨x', hk', hR⟩ := J.xtop.ipc.toN.ntfn n' hn' + rw [hx] at hk'; cases hk' + obtain ⟨y, hy, -, hobj, hbd, hsc'⟩ := F.ntfnF hx + have hbn : x.ntfnBoundTcb = none := by + rw [hR.bound, show nBoundA n' = P1.ntfnBtA n' from rfl, d2, optRef_zero] + have hnw : ∀ l, x.ntfnObj ≠ .waitingNtfn l := by + intro l e + have hO := hR.obj + rw [e] at hO + exact d1 (by rw [epState_eq]; exact hO.1) + refine ⟨y, hy, (hobj hnw).imp id And.right, ?_, hsc'⟩ + rcases hbd with e | e + · exact e + · rw [e, hbn] + · -- a dead reply: naming nobody + obtain ⟨y, hy, -, hc⟩ := F.replyF hx + refine ⟨y, hy, ?_⟩ + rcases hc with e | ⟨hxt, -⟩ + · exact e + · exfalso + obtain ⟨r', hr', e⟩ := J.obj.reply q.bv x hx + have heq : r' = q := P1.eq_of_bv e + subst heq + have d4 := hd.2.2.2.1 (J.zH.2.2.1 r' hr') + obtain ⟨x', hk', hx'⟩ := J.xtop.ipc.toE.reply r' hr' + rw [hx] at hk'; cases hk' + have d4' : L.mem s (Endp.rTcbA r') = 0#u64 := d4 + rw [hx', d4', optRef_zero] at hxt + cases hxt + · -- an SC: its thread kept (a changed one is a notification's SC, not dead), its notification kept or cut + obtain ⟨rf, nt, tc, hk3, -, hn⟩ := F.scF hx + refine ⟨_, hk3, ?_, hn⟩ + show tc = x.scTcb ∨ tc = none + by_cases e : tc = x.scTcb + · exact Or.inl e + · obtain ⟨-, o, xo, ho, hsc⟩ := F.scb q.bv x n _ n hx hk3 e + exact absurd hd (J.ntfnSc_not_dead hx ho hsc) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **A wake with or without a donation keeps `JAtZRB`** (`wake_postB` in the +frame `DWFr`; P3/ScSignalDonJ's `JAtZ.wake_postD` with the post `JAtZRB Z`, module docstring). -/ +theorem JAtZRB.wake_postD (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n t : Fl4Spec.ObjRef} + {s' : S} {σ' : Fl4Spec.State Unit} + (hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hF : IFrame (L := L) Z.th s s') (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s)) + (F : DWFr n t σ σ') (I3 : Inv σ') (B3 : BlockedScActive σ') (hAI3 : ActIdle σ') + (hcur3 : CurInvR σ') (hrun3 : CurRunM σ') + (hRR : ∀ d ∈ Z.R, ∀ xr, σ.kheap d.bv = some (.reply xr) → xr.replyTcb = some t → + Detached Z.C Lst d ∧ L.mem s (rNextA d) = 0#u64 ∧ L.mem s (rPrevA d) = 0#u64) + (ht : ∀ q ∈ D, q.bv ≠ t) (htT : ∃ u ∈ Z.T, u.bv = t) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s' a σ' := by + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hw + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨x, hx, -, hc⟩ := F.replyB hy + obtain ⟨e1, e2⟩ := k4 d hd + rcases hc with e | ⟨hxt, -⟩ + · obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨x, hx, e ▸ hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + · obtain ⟨h1, h2, h3⟩ := hRR d hd x hx hxt + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_dfr F hS3 hIR.1 hw k3 hAI3.1 (J.yld.frame k2 F.ykeep) F.ykeep ht + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hw) + (J.idleW_keep hS3 F.sk.idle F.sk.isc).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (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)) hDbg + (D' := D) + have hRR' := J.rrecv.frame k4 F.khrb + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 F.sk.idle F.sk.isc) + (DXKeep.of_dbg hKd (J.cnW_td hw)), hkern, + J.cnW_utc hw hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own htT J.own, F.off J.off, + hAI3, B3, F.isorted J.isorted, hrun3, hblk⟩ + have hC0 := JCs.sameBR J hF.1 F.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact J.cs_frameI hks hX3 hw (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch (fun q ao h => F.othF h (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)) F.szKeep hF.1 F.kback F.ipcFr hC R hKd F.sk.orig (J.idleW_keep hS3 F.sk.idle F.sk.isc).1 J.zD + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **The idle notification's bound receiver woken, donating or not, keeps +`JAtZ`** (module docstring; `JAtZ.signal_boundJ` with `wake_specG`). (mmuAD gen 42b71) Over `JAtZRB` in place, as +`JAtZRB.signal_waitDJ`; `JAtZ.signal_boundDJ` its corollary. -/ +theorem JAtZRB.signal_boundDJ (hAT : P0.ArchTotal arI) (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} + (hk : σ.kheap n.bv = some (.notification x)) (hidle : x.ntfnObj = .idleNtfn) {t : U64} {tcb : Fl4Spec.Tcb} + (hb : x.ntfnBoundTcb = some t.bv) (hkt : σ.kheap t.bv = some (.tcb tcb)) + (hrb : Fl4Spec.receiveBlocked tcb.tcbState = true) (badge : U64) : + notification.send_signal st arI s a n badge ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignal (Z := Unit) n.bv badge.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hNt : ∀ m ∈ P1.Heap.ntfns H, H m = some .ntfn := fun _ h => h + have hnH : n ∈ P1.Heap.ntfns H := J.zH.2.2.2 n hn + have hx := J.xtop.ipc.toN.at_ntfn hn hk + obtain ⟨hb0, hbt⟩ := Notif.optRef_some (hx.bound ▸ hb) + have htU : L.mem s (nBoundA n) = t := P1.eq_of_bv hbt + have htT : t ∈ Z.T := htU ▸ hx.bound_mem hb0 + obtain ⟨e, ro, d, hst⟩ : ∃ e ro d, tcb.tcbState = .blockedOnReceive e ro d := by + cases h : tcb.tcbState <;> simp [h, Fl4Spec.receiveBlocked] at hrb + exact ⟨_, _, _, rfl⟩ + have hCR : ∀ t', t' = t → CancelRecvHyp Z.E (L.mem s) σ t' tcb := fun t' et => et ▸ + cancelRecvHyp_of hI.1.toE J.sv J.obj J.inv htT hkt hrb + have hCR0 := hCR t rfl + obtain ⟨xe, hke, hqe⟩ := hCR0.ok.2.1 _ ro d hst + have hro : ∀ rp, ro = some rp → ∃ xr, σ.kheap rp = some (.reply xr) ∧ xr.replyTcb = some t.bv := fun rp er => by + subst er; exact hCR0.ok.2.2.1 _ rp d hst + have hrbI : RbIn tcb.tcbState := Or.inl ⟨_, _, _, hst⟩ + have post1 : ∀ σ1 x1, Fl4Spec.cancelIpc (Z := Unit) t.bv σ = ({((), σ1)}, False) → + σ1.kheap t.bv = some (.tcb x1) → x1.tcbState = .inactive → RelIfBound σ1 t.bv → + CFr n.bv t.bv σ σ1 ∧ Inv σ1 ∧ ((), σ1) ∈ (Fl4Spec.cancelIpc (Z := Unit) t.bv σ).1 := by + intro σ1 x1 hc hk1 hin hrib + have hm : ((), σ1) ∈ (Fl4Spec.cancelIpc (Z := Unit) t.bv σ).1 := by rw [hc]; exact Set.mem_singleton _ + obtain ⟨F1, -, -⟩ := cancelIpc_recv_cfr (r := n.bv) hkt hst hke hqe hro hm + exact ⟨F1, cancel_recv_inv J.inv hkt hrbI hm hrib, hm⟩ + have hA3 : ∀ σ1 x1, Fl4Spec.cancelIpc (Z := Unit) t.bv σ = ({((), σ1)}, False) → + σ1.kheap t.bv = some (.tcb x1) → x1.tcbState = .inactive → RelIfBound σ1 t.bv → + Inv (wakeSt3 σ1 t.bv x1 badge.bv) ∧ NSC Z.N Z.C σ1 ∧ ∀ dd pp, t.bv ∉ σ1.readyQueues dd pp := by + intro σ1 x1 hc hk1 hin hrib + obtain ⟨F1, hI1, -⟩ := post1 σ1 x1 hc hk1 hin hrib + exact ⟨wake_inv3B hI1 hk1 hin hrib badge.bv, F1.nsc J.nsc, (hI1.notQueued hk1 (by rw [hin]; rfl)).1⟩ + have hNV : P1.NoVmSig st n s := Ipc.noVm_of_recv hI hn hk hb hkt hrb + refine WP.spec_mono (WP.spec_and (send_signal_bound_irelD J.ip hmb s a σ hI hn hk hidle hb hkt hrb badge hCR hA3) + (P1.notification.send_signal.tinvF hAT s a n badge J.blk hnH (hNt n hnH) J.bufAtJ.1 J.bufAtJ.2)) ?_ + rintro ⟨s', a'⟩ ⟨⟨ea, s1, σ1, σd, σ', x1, hc, hk1, hin, hrib, hX1, hF1, hW1, hdon, hsend, hmw, hIR, hF2, hFS⟩, + hbk⟩ + try simp only [WP.uncurry'_pair] at hbk + obtain ⟨hbk, -, -, hfoot⟩ := hbk + have hfoot := (hfoot hNV).2 (J.donCore hn) + subst ea + obtain ⟨F1, hI1, hm1⟩ := post1 σ1 x1 hc hk1 hin hrib + obtain ⟨-, hk1', hact1⟩ := cancelIpc_recv_cfr (r := n.bv) hkt hst hke hqe hro hm1 + have hB1 := F1.bsa J.bsa + have hAI1 : ActIdle σ1 := by + refine ⟨fun u hu => ?_, F1.idleOK J.act.2⟩ + rcases hact1 with e1 | e1 + · exact F1.sched (J.act.1 u (by rw [← e1]; exact hu)) + · rw [e1] at hu; cases hu + have hcn : σ.schedulerAction = .chooseNewThread → σ1.schedulerAction = .chooseNewThread := fun e0 => by + rcases hact1 with e1 | e1 + · rw [e1, e0] + · exact e1 + have hcur1 : CurInvR σ1 := fun e => F1.curInv (J.cur + (hact1.elim (fun e1 => by rw [← e1]; exact e) (fun e1 => by rw [e1] at e; cases e))) + have hrun1 : CurRunM σ1 := J.curRun.of_cn F1.curRunW hact1.symm + obtain ⟨xn1, hkn1, -⟩ := F1.ntfnF hk + have hoff1 : OffOK σ1 := F1.off J.off + have hsp1 : ∀ c y k, σ1.kheap c = some (.schedContext y k) → y.scSporadic = true → Fl4Spec.scActive y = true := by + intro c y k hc + obtain ⟨y0, rf, nt, hy0, rfl, -⟩ := F1.scB hc + exact J.sv.scSpor c y0 k hy0 + obtain ⟨I', B', F2, AI', cur', run'⟩ := wake_specGR hI1 (wake_inv3B hI1 hk1 hin hrib badge.bv) hB1 hAI1 hcur1 + hrun1 hk1 (by rw [hin]; rfl) (wake_f3B hk1 hin badge.bv) hkn1 hoff1 hsp1 hdon hmw + have FF : DWFr n.bv t.bv σ σ' := (DWFr.ofCFr F1).trans F2 + have hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + (hW1.trans (hFS.1.mono fun b hb => FinW.back hW1.1 (SzKeepC.of F1.szKeep) hb)).mono + fun b h => h.elim id Or.inl + have hRR : ∀ q ∈ Z.R, ∀ xr, σ.kheap q.bv = some (.reply xr) → xr.replyTcb = some t.bv → + Detached Z.C Lst q ∧ L.mem s (rNextA q) = 0#u64 ∧ L.mem s (rPrevA q) = 0#u64 := + fun q hq xr hx' hxt => J.recvReplyB q hq xr t.bv tcb hx' hxt hkt hrb + have htD := J.not_dead_of_st htT hkt (by rw [hst]; simp [tsCode]) + exact ⟨σ', Ac, Lst, D, hsend, FF.sk.dt, + J.wake_postD hIR (hF1.trans hF2) hw hbk hfoot FF I' B' AI' cur' run' hRR htD ⟨t, htT, rfl⟩, + NatFoot.ofW J.xtop hw (fun _ hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun _ hb => J.cnW_nv hb) + (fun _ hb => J.cnW_nh hb) (natK_dwfr FF)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **A waiting notification signalled, donating or not, keeps `JAtZ`** +(module docstring). (mmuAD gen 42b71) Over `JAtZRB` in place, the post at `Z` with the native footprint `NatFoot` (the +wake's `CnW` writes, `natK_dwfr`); `JAtZ.signal_waitDJ` its corollary. -/ +theorem JAtZRB.signal_waitDJ (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} + (hk : σ.kheap n.bv = some (.notification x)) {l : List Fl4Spec.ObjRef} (ho : x.ntfnObj = .waitingNtfn l) + (badge : U64) : + notification.send_signal st arI s a n badge ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignal (Z := Unit) n.bv badge.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hNt : ∀ m ∈ P1.Heap.ntfns H, H m = some .ntfn := fun _ h => h + have hnH : n ∈ P1.Heap.ntfns H := J.zH.2.2.2 n hn + have hbl := (J.sv.ntfnQ n.bv x hk).2 + have hNV : P1.NoVmSig st n s := Ipc.noVm_of_ntfn hI hn hk (by rw [ho]; intro e; cases e) + refine WP.spec_mono (WP.spec_and (send_signal_wait_irelD J.ip hmb s a σ hI J.inv J.nsc hn hk ho hbl badge) + (P1.notification.send_signal.tinvF hAT s a n badge J.blk hnH (hNt n hnH) J.bufAtJ.1 J.bufAtJ.2)) ?_ + rintro ⟨s', a'⟩ ⟨⟨ea, d, xd, σd, σ', hmq, hkd, hxs, hdon, hsend, hmw, hIR, hF, hFS⟩, hb⟩ + try simp only [WP.uncurry'_pair] at hb + obtain ⟨hb, -, -, hfoot⟩ := hb + have hfoot := (hfoot hNV).2 (J.donCore hn) + subst ea + have hdn : d.bv ≠ n.bv := by intro e; rw [e, hk] at hkd; cases hkd + set σ1 := updK σ n.bv (.notification (afterDequeue x d)) with hσ1 + have hk1 : σ1.kheap d.bv = some (.tcb xd) := by simp [hσ1, updK, hdn, hkd] + have hkn1 : σ1.kheap n.bv = some (.notification (afterDequeue x d)) := by simp [hσ1, updK] + have hkp1 : ∀ p, p ≠ n.bv → σ1.kheap p = σ.kheap p := fun p hp => by simp [hσ1, updK, hp] + have hsub : (afterDequeue x d).ntfnObj.ntfnQueue.Sublist x.ntfnObj.ntfnQueue := by + show (match Fl4Spec.removeAll d.bv (Fl4Spec.ntfnQueueOf x) with + | [] => Fl4Spec.Ntfn.idleNtfn + | _ => Fl4Spec.Ntfn.waitingNtfn (Fl4Spec.removeAll d.bv (Fl4Spec.ntfnQueueOf x))).ntfnQueue.Sublist + (Fl4Spec.ntfnQueueOf x) + split + · exact List.nil_sublist _ + · unfold Fl4Spec.removeAll; apply List.filter_sublist + have F1 : NFr false n.bv σ σ1 := + NFr.upd hk (Or.inr (Or.inr ⟨x, afterDequeue x d, rfl, rfl, rfl, hsub, (fun h => by cases h), Or.inl rfl, + fun hw => absurd ho (hw l), Or.inl rfl⟩)) + have hI1 : Inv σ1 := J.inv.updNtfn hk (fun p hp => J.inv.ntfnSc n.bv x p hk hp) + have hAI1 : ActIdle σ1 := ⟨fun u hu => F1.sched (J.act.1 u hu), F1.idleOK J.act.2⟩ + have hcur1 : CurInvR σ1 := fun e => F1.curInv (J.cur e) + have hrun1 : CurRunM σ1 := Or.imp id F1.curRunW J.curRun + have hoff1 : OffOK σ1 := F1.off J.off + have hsp1 : ∀ c y k, σ1.kheap c = some (.schedContext y k) → y.scSporadic = true → Fl4Spec.scActive y = true := by + intro c y k hc + have hcn : c ≠ n.bv := by rintro rfl; rw [hkn1] at hc; cases hc + rw [hkp1 c hcn] at hc + exact J.sv.scSpor c y k hc + obtain ⟨I', B', F2, AI', cur', run'⟩ := wake_specGR hI1 (wake_inv3 hI1 hk1 hxs badge.bv) (F1.bsa J.bsa) hAI1 + hcur1 hrun1 hk1 (by rw [hxs]; rfl) (CFr.ofNFr (wake_f3 hk1 hxs badge.bv)) hkn1 hoff1 hsp1 hdon hmw + have FF : DWFr n.bv d.bv σ σ' := (DWFr.ofNFr F1).trans F2 + have hdT : d ∈ Z.T := by + obtain ⟨t', ht', e⟩ := J.obj.tcb d.bv xd hkd + rw [← P1.eq_of_bv e]; exact ht' + have hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hFS.1.mono fun b h => Or.inl h + have hRR : ∀ q ∈ Z.R, ∀ xr, σ.kheap q.bv = some (.reply xr) → xr.replyTcb = some d.bv → + Detached Z.C Lst q ∧ L.mem s (rNextA q) = 0#u64 ∧ L.mem s (rPrevA q) = 0#u64 := by + intro q _ xr hq hxt + exfalso + obtain ⟨x', hk', hs'⟩ := J.sv.replyTcb q.bv xr d.bv hq hxt + rw [hkd] at hk'; cases hk' + rcases hs' with e | ⟨ep, dd, e⟩ <;> rw [hxs] at e <;> cases e + have htD := J.not_dead_of_st hdT hkd (by rw [hxs]; simp [tsCode]) + exact ⟨σ', Ac, Lst, D, hsend, FF.sk.dt, J.wake_postD hIR hF hw hb hfoot FF I' B' AI' cur' run' hRR htD ⟨d, hdT, rfl⟩, + NatFoot.ofW J.xtop hw (fun _ hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun _ hb => J.cnW_nv hb) + (fun _ hb => J.cnW_nh hb) (natK_dwfr FF)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **ScSignalJ's `signalJHyp_of` at one `JAtZ` state with `NativeScope`** (g3b): +the `RunningVM` bound-thread case is absurd there, so no `SignalRestJHyp`. (mmuAD gen 42b71) Over `JAtZRB` in place, the +post at `Z` with the native footprint `NatFoot` (module docstring), the `RunningVM` case excluded for this notification +only (`hVM`); `JAtZRB.signal_atN` (`NativeScope`) and `JAtZ.signal_atN` its corollaries. -/ +theorem JAtZRB.signal_atNV (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) {s : S} {a : A} + {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (n badge : U64) (hn : n ∈ Z.N) + (hVM : ∀ (x : Fl4Spec.Notification) (t : Fl4Spec.ObjRef) (tcb : Fl4Spec.Tcb), + σ.kheap n.bv = some (.notification x) → x.ntfnObj = .idleNtfn → x.ntfnBoundTcb = some t → + σ.kheap t = some (.tcb tcb) → tcb.tcbState ≠ .runningVM) : + notification.send_signal st arI s a n badge ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignal (Z := Unit) n.bv badge.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + obtain ⟨x, hk, hx⟩ := J.xtop.ipc.toN.ntfn n hn + have hNt : ∀ m ∈ P1.Heap.ntfns H, H m = some .ntfn := fun _ h => h + have hnH : n ∈ P1.Heap.ntfns H := J.zH.2.2.2 n hn + have hP1 := P1.notification.send_signal.tinv hAT s a n badge J.blk hnH (hNt n hnH) J.bufAtJ.1 J.bufAtJ.2 + -- the badge-only branches + have post : ∀ (y : Fl4Spec.Notification), (∀ l, x.ntfnObj ≠ .waitingNtfn l) → (∀ l, y.ntfnObj ≠ .waitingNtfn l) → + y.ntfnBoundTcb = x.ntfnBoundTcb → y.ntfnSc = x.ntfnSc → + Fl4Spec.sendSignal (Z := Unit) n.bv badge.bv σ = ({((), updK σ n.bv (.notification y))}, False) → + notification.send_signal st arI s a n badge ⦃ r => r.2 = a ∧ + SingleRelI (st := st) Z.N Z.T Z.E Z.R Z.B s σ (Fl4Spec.sendSignal n.bv badge.bv σ) r.1 ⦄ → + notification.send_signal st arI s a n badge ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignal (Z := Unit) n.bv badge.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := by + intro y hxw hyw hyb hys hspec hI + refine WP.spec_mono (WP.spec_and hI hP1) ?_ + rintro ⟨s', a'⟩ ⟨⟨ea, hrel⟩, hb⟩ + try simp only [WP.uncurry'_pair] at hb + subst ea + obtain ⟨σ', h1, h2, J'⟩ := J.signal_badge_post hn hk hxw hyw hyb hys hspec hrel hb + have e : σ' = updK σ n.bv (.notification y) := by + have h3 := (Prod.mk.inj (h1.symm.trans hspec)).1 + exact congrArg Prod.snd (Set.singleton_injective h3) + subst e + exact ⟨_, Ac, Lst, D, hspec, h2, J', J.badgeN hk (hspec ▸ hrel)⟩ + have hI := J.xtop.irelK J.ks J.buf + cases ho : x.ntfnObj with + | activeNtfn b0 => + exact post (Fl4Spec.ntfnObjUpdate (fun _ => .activeNtfn (Fl4Spec.combineNtfnBadges badge.bv b0)) x) + (by rw [ho]; intro l e; cases e) (fun l e => by simp [Fl4Spec.ntfnObjUpdate] at e) rfl rfl + (sendSignal_active_eq hk ho badge.bv) (send_signal_activeI (ar := arI) J.ip s a σ hI.1 hn hk ho badge) + | waitingNtfn l => + exact J.signal_waitDJ hAT hmb hn hk ho badge + | idleNtfn => + cases hb : x.ntfnBoundTcb with + | none => + exact post (Fl4Spec.ntfnObjUpdate (fun _ => .activeNtfn badge.bv) x) + (by rw [ho]; intro l e; cases e) (fun l e => by simp [Fl4Spec.ntfnObjUpdate] at e) rfl rfl + (sendSignal_activate hk ho (Or.inl hb) badge.bv) + (send_signal_idle_unboundI (ar := arI) J.ip s a σ hI.1 hn hk ho hb badge) + | some t => + obtain ⟨hb0, hbt⟩ := Notif.optRef_some (hx.bound ▸ hb) + obtain ⟨tcb, hkt, -⟩ := hI.1.tcb _ (hx.bound_mem hb0) + rw [hbt] at hkt + by_cases hrb : Fl4Spec.receiveBlocked tcb.tcbState = true + · exact J.signal_boundDJ hAT hmb hn hk ho (t := L.mem s (nBoundA n)) (by rw [hbt]; exact hb) + (by rw [hbt]; exact hkt) hrb + badge + by_cases hvm : tcb.tcbState = .runningVM + · exact absurd hvm (hVM x t tcb hk ho hb hkt) + have hrb' : Fl4Spec.receiveBlocked tcb.tcbState = false := by simpa using hrb + exact post (Fl4Spec.ntfnObjUpdate (fun _ => .activeNtfn badge.bv) x) + (by rw [ho]; intro l e; cases e) (fun l e => by simp [Fl4Spec.ntfnObjUpdate] at e) rfl rfl + (sendSignal_activate hk ho (Or.inr ⟨t, tcb, hb, hkt, hrb'⟩) badge.bv) + (send_signal_idle_bound_idleI (ar := arI) J.ip s a σ hI.1 hn hk ho hb hkt hrb' hvm badge) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71) `JAtZRB.signal_atNV` in the native scope. -/ +theorem JAtZRB.signal_atN (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) {s : S} {a : A} + {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hN : NativeScope σ) + (n badge : U64) (hn : n ∈ Z.N) : + notification.send_signal st arI s a n badge ⦃ r => ∃ (σ' : Fl4Spec.State Unit) (Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + Fl4Spec.sendSignal (Z := Unit) n.bv badge.bv σ = ({((), σ')}, False) ∧ σ'.domainTime = σ.domainTime ∧ + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' r.1 r.2 σ' ∧ NatFoot (L := L) Z s σ r.1 σ' ⦄ := + J.signal_atNV hAT hmb n badge hn fun _ t tcb _ _ _ hkt => hN t tcb hkt + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 8000000 in +/-- **A `JAtZRB` state at another machine state (same memory) and arch state keeping `AR`**, a native step +(`JAtZB.msArJ` proved directly, `JHole` asking `CurInv` or a reschedule). -/ +theorem JAtZRB.msArJ {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (ms : Fl4Spec.MachineState) (a' : A) + (hum : ms.underlyingMemory = σ.machineState.underlyingMemory) + (hAR : AR a σ.archState.x64CurrentCr3 → AR a' σ.archState.x64CurrentCr3) : + ∃ Lst' : U64 → List U64, JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s a' { σ with machineState := ms } ∧ + NatFoot (L := L) Z s σ s { σ with machineState := ms } := by + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + have hARh : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hK : ScTKw (fun _ => False) σ { σ with machineState := ms } := + ⟨fun p => OTK.refl _ _, hum, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + have hsok : SOK (true, true) σ { σ with machineState := ms } := ⟨fun p => ORel.refl _ _, rfl, rfl, rfl, rfl, rfl⟩ + have hL : LK (fun _ => False) σ { σ with machineState := ms } := fun p => Or.inr (OL.refl _) + have hF : FK σ { σ with machineState := ms } := ⟨rfl, rfl, rfl⟩ + have hS2 : SchedRel st Z.N Z.T Z.C Z.c s { σ with machineState := ms } := + hS.spec_congr rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl rfl + have hR2 : NtfnStateRel Z.N Z.T (L.mem s) { σ with machineState := ms } := ⟨hR.ntfn, hR.tcb⟩ + have hI : Inv { σ with machineState := ms } := J.inv.congr rfl rfl rfl rfl + have hwT : P1.WritesIn st s s (TFoot st Z.T Z.N Z.C Z.c s σ) := P1.WritesIn.refl s _ + have hX' : XTopAt (L := L) AR Z s a' { σ with machineState := ms } := + xtop_sc_step J.xtop J.ip J.nk hwT (ct := σ.curThread) (hK.mono fun _ h => h.elim) hS2 hR2 + fun _ => hAR hARh + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree ⟨⟨hS2, hR2, J.nsc, hI⟩, P1.WritesIn.refl s _, hsok⟩ + have hDead : ∀ q ∈ D, DeadP (L := L) H s q := fun q hq => + J.deadP_sc_step hX' hwT hK (fun _ _ h => h.elim) hL hF J.act.1 hY' ((hsok.ykeep rfl).dk q.bv) (fun h => h) + (J.jcov.dead q hq) + have hidW := J.idleW_keep hS hF.2.1 hF.2.2 + exact ⟨Lst', J.sc_frame hwT hK hX' hCR' hRF' hY' hM' (J.sc_buf hwT hK) hRP' (J.sc_apart hwT hK) + (J.sc_jpl hwT hK hidW.2) (J.sc_jcov hwT hDead (fun q hq hn => absurd hq hn) hidW (P1.DbgKeep.refl s) subset_rfl) + (J.sc_kern hwT hK hF hX' rfl fun _ h => h) + (J.sc_utc hwT hK rfl) J.nsc hI (fun e => (J.cur e).congr rfl rfl rfl rfl) J.own J.off J.act + (J.bsa.tkw hK fun p y n h hy => ⟨y, h, hy, fun _ h => h, id⟩) J.curRun J.blk (P1.DbgKeep.refl s) hidW.1 J.zD, + NatFoot.ofT J.xtop hwT (fun _ h => J.tfoot_nh h) (natK_tk hK)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71, ruling (c2) item 1) **`handle_interrupt` from a `JAtZRB` state in the native scope, every branch +a native step** (module docstring): the post `JAtNR` from the start (no `JExt`), with a machine-state predicate `MR` +carried by the given facts of the mask (`hMmask`), the acknowledgement (`hMack`) and the signal (`hMsig`); the +`AR` fact of the acknowledgement is `hackAR` (`IrqPlat.ackAR` / `IrqPlatA.ackAR`). The `JAtZ` handlers +`handleIrqJJN`, `handleIrqJIN` (XTopJointIrqZI), `handleIrqJIAN` (XTopJointMRPath), `handleIrqJIM` +(XTopJointKEntryEv2), `handleIrqJIMSt` (StPostIrqLeaf) and `handleIrqJIANM` (XTopJointKEntry38) are its corollaries. +(gen 42b72) `JAtZRB.handleIrqJG` at `sig := sendSignal`, `Q := JAtNR …` (the signal by `JAtZRB.signal_atN`). -/ +theorem JAtZRB.handleIrqJB {MR : A → Fl4Spec.MachineState → Prop} (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hN : NativeScope σ) (hR : MR a σ.machineState) + (w : U64) (i : Fl4Spec.Irq) (hwi : w.val = i.toNat) (h126 : w.val ≠ 126) (h127 : w.val ≠ 127) + (hackAR : ∀ (a1 : A) (c : Fl4Spec.Cr3), AR a1 c → arI.ack_irq a1 w ⦃ a2 => AR a2 c ⦄) + (hMmask : ∀ (a0 a1 : A) (σ0 : Fl4Spec.State Unit), MR a0 σ0.machineState → arI.mask_irq a0 w true = ok a1 → + MR a1 (maskMs σ0 i true).machineState) + (hMack : ∀ (a1 : A) (ms : Fl4Spec.MachineState), MR a1 ms → arI.ack_irq a1 w ⦃ a2 => MR a2 ms ⦄) + (hMsig : ∀ (s1 : S) (a1 : A) (σ1 : Fl4Spec.State Unit) (n b : U64), MR a1 σ1.machineState → + notification.send_signal st arI s1 a1 n b ⦃ r => ∀ σ', ((), σ') ∈ (Fl4Spec.sendSignal (Z := Unit) n.bv b.bv σ1).1 → + MR r.2 σ'.machineState ⦄) : + interrupt.handle_interrupt st arI s a w ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (Fl4Spec.handleInterrupt (Z := Unit) i σ).2) ∨ + ∃ z ∈ (Fl4Spec.handleInterrupt (Z := Unit) i σ).1, JAtNR (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ∧ + MR r.2 z.2.machineState ⦄ := + J.handleIrqJG (sig := Fl4Spec.sendSignal) (Q := JAtNR (L := L) arI AR Z H Cs s σ) hARI hAT hR (fun h _ _ => h) + (fun ms ⟨V, Ac1, Lst1, D1, B1, F1⟩ hum hA => by + obtain ⟨Lst2, B2, F2⟩ := B1.msArJ ms _ hum (fun _ => hA) + exact ⟨V, Ac1, Lst2, D1, B2, J.natTrans F1 F2.cast⟩) + (fun ⟨_, _, _, _, B1, _⟩ => B1.xtop.fin.1.1.2.2.2.2.2.2) + (fun p b hp => WP.spec_mono (J.signal_atN hAT hmb hN p b hp) fun _ ⟨σ', _, _, _, hsp, _, J', F'⟩ => + ⟨σ', hsp, JAtNR.of J' F'⟩) + w i hwi h126 h127 hackAR hMmask hMack hMsig + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71) `JAtZRB.handleIrqJB` without a machine-state predicate (`MR := True`). -/ +theorem JAtZRB.handleIrqJBT (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hN : NativeScope σ) + (w : U64) (i : Fl4Spec.Irq) (hwi : w.val = i.toNat) (h126 : w.val ≠ 126) (h127 : w.val ≠ 127) + (hackAR : ∀ (a1 : A) (c : Fl4Spec.Cr3), AR a1 c → arI.ack_irq a1 w ⦃ a2 => AR a2 c ⦄) : + interrupt.handle_interrupt st arI s a w ⦃ r => + (σ.interruptStates i = .irqInactive ∧ (Fl4Spec.handleInterrupt (Z := Unit) i σ).2) ∨ + ∃ z ∈ (Fl4Spec.handleInterrupt (Z := Unit) i σ).1, JAtNR (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.handleIrqJB (MR := fun _ _ => True) hARI hAT hmb hN trivial w i hwi h126 h127 hackAR + (fun _ _ _ _ _ => trivial) (fun a1 _ _ => WP.spec_mono (hAT.ack_irq a1 w) fun _ _ => trivial) + (fun s1 a1 _ n b _ => WP.spec_mono (Fl4Proofs.P0.notification.send_signal.p0 st arI s1 a1 n b L.total hAT) + fun _ _ _ _ => trivial)) + fun _ h => h.imp id fun ⟨z, hz, hN', _⟩ => ⟨z, hz, hN'⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +/-- (mmuAD gen 42b71) **`JAtNR` composed**: a native run from a `JAtZRB` base `(s0, σ0)` to `(s1, σ1)`, then a native +run at `Z.mv Z.G V` from `(s1, σ1)` (`JAtZRB.natTrans`, `NatFoot.cast`). -/ +theorem JAtNR.trans {s0 : S} {a0 : A} {σ0 : Fl4Spec.State Unit} (J0 : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s0 a0 σ0) + {V : XVs} {s1 : S} {σ1 : Fl4Spec.State Unit} (F1 : NatFoot (L := L) Z s0 σ0 s1 σ1) {s2 : S} {a2 : A} + {σ2 : Fl4Spec.State Unit} (h : JAtNR (L := L) arI AR (Z.mv Z.G V) H Cs s1 σ1 s2 a2 σ2) : + JAtNR (L := L) arI AR Z H Cs s0 σ0 s2 a2 σ2 := + match h with + | ⟨V', Ac', Lst', D', B', F2⟩ => ⟨V', Ac', Lst', D', B', J0.natTrans F1 F2.cast⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {MR : A → Fl4Spec.MachineState → Prop} + +set_option maxHeartbeats 8000000 in +/-- (mmuAD gen 42b71, ruling (c2) item 4) **The IRQ take and handler of the preemption path from a native run**: after +the MCS part, at `JAtNR` from a `JAtZRB` base `(s0, σ0)`, with `MR`, `ValidIrqMasks` and `NativeScope` there, FL4's +`take_pending_irq` and `check_interrupt` end at an outcome of the spec's `preemption_path` (through `hm`) with `JAtNR` +from the base (`JAtZRB.msArJ`, `JAtZRB.handleIrqJBT`, `JAtNR.trans`). -/ +theorem JAtZRB.irqTailB {s0 : S} {a0 : A} {σ0 : Fl4Spec.State Unit} + (J0 : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s0 a0 σ0) {s1 : S} {a1 : A} {σ σ' : Fl4Spec.State Unit} + (hm : ∀ y, y ∈ (Fl4Spec.maybeHandleInterrupt (Z := Unit) true σ').1 → y ∈ (Fl4Spec.preemptionPath (Z := Unit) σ).1) + (hJ : JAtNR (L := L) arI AR Z H Cs s0 σ0 s1 a1 σ') (hR1 : MR a1 σ'.machineState) (hVI : ValidIrqMasks σ') + (hN' : NativeScope σ') + (hM : MachPlat arI MR) (hIP : IrqPlatA arI AR) (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) + (hmbA : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) : + (do + let (irq, a2) ← arI.take_pending_irq a1 + let (s2, a3) ← syscall.check_interrupt st arI s1 a2 irq + ok (false, s2, a3)) ⦃ r => r.1 = false ∧ + ∃ z ∈ (Fl4Spec.preemptionPath (Z := Unit) σ).1, JAtNR (L := L) arI AR Z H Cs s0 σ0 r.2.1 r.2.2 z.2 ⦄ := by + obtain ⟨V1, Ac1, Lst1, D1, B1, F1⟩ := hJ + have hAR1 : AR a1 σ'.archState.x64CurrentCr3 := B1.xtop.fin.1.1.2.2.2.2.2.2 + refine WP.spec_bind (WP.spec_and (WP.spec_and (hM.takeAt hR1) (hIP.takeAR a1 _ hAR1)) (hM.noIpi a1 _ hR1)) ?_ + rintro ⟨w, a2⟩ ⟨⟨⟨irq, ms, hIq, hnone, hsome, hum, -⟩, hAR2⟩, h126, h127⟩ + obtain ⟨Lst2, B2, F2⟩ := B1.msArJ ms a2 hum (fun _ => hAR2) + have F12 := J0.natTrans F1 F2.cast + unfold syscall.check_interrupt + by_cases hw : w = syscall.NO_IRQ + · have hirq : irq = none := hnone.1 hw + subst hirq + have e : (w != syscall.NO_IRQ) = false := by simp [hw] + show (do + let (s2, a3) ← (if (w != syscall.NO_IRQ) = true then interrupt.handle_interrupt st arI s1 a2 w else ok (s1, a2)) + ok (false, s2, a3)) ⦃ _ ⦄ + rw [e] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + exact (WP.spec_ok _).2 ⟨rfl, ((), { σ' with machineState := ms }), hm _ (mem_maybeHandle_none hIq), + V1, Ac1, Lst2, D1, B2, F12⟩ + · obtain ⟨i, rfl⟩ : ∃ i, irq = some i := by + cases irq with + | none => exact absurd (hnone.2 rfl) hw + | some i => exact ⟨i, rfl⟩ + obtain ⟨hwi, -⟩ := hsome i rfl + have e : (w != syscall.NO_IRQ) = true := by simpa using hw + show (do + let (s2, a3) ← (if (w != syscall.NO_IRQ) = true then interrupt.handle_interrupt st arI s1 a2 w else ok (s1, a2)) + ok (false, s2, a3)) ⦃ _ ⦄ + rw [e] + simp only [↓reduceIte] + refine WP.spec_bind (B2.handleIrqJBT hARI hAT hmbA hN' w i hwi h126 h127 fun a c h => hIP.ackAR a w c h) ?_ + rintro ⟨s2, a3⟩ (hf | ⟨z, hz, hJ2⟩) + · exact absurd hf.1 (gai_active hVI hIq i rfl) + · exact (WP.spec_ok _).2 ⟨rfl, z, hm _ (mem_maybeHandle_some hIq hz), JAtNR.trans J0 F12 hJ2⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} + {D : Set U64} + +/-- A kernel-state word (`KsW`) is no debug word: below the nodes, outside +the TCBs (`ksOut`). -/ +theorem JAtZRB.ksW_notDbg {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {b : U64} (hb : KsW (L.ksb s) b) : ¬ P1.DbgW st (P1.Heap.tcbs H) Cs s b := by + obtain ⟨K, h1, h2, -, e, -⟩ := hb + refine J.notDbg' (fun p hp => ?_) (fun c _ => ?_) + · obtain ⟨⟨Q, R, hq⟩, -⟩ := J.blk + have e12 : (2 : Nat) ^ P1.HKind.bits .tcb = 4096 := rfl + rw [e12] + rcases hq.ksOut p hp with h | h <;> omega + · have hnb : L.nodeBase s c = (L.ksb s).val + 13568 + c.val * 4288 := rfl + rw [hnb]; omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **A kernel-state block word is no TCB's VCPU word** (P1's `ksOut`). -/ +theorem JAtZRB.ks_nv (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (h1 : (L.ksb s).val ≤ b.val) + (h2 : b.val < (L.ksb s).val + 13568) : ∀ t ∈ Z.T, b ≠ tVcpuA t := by + intro t ht e + obtain ⟨⟨Q, R, hq⟩, -⟩ := J.blk + have hv := Sched.tVcpuA_val J.ip.e.tcb ht + rw [e] at h1 h2 + rcases hq.ksOut t (J.zH.1 t ht) with h | h <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem JAtZRB.ksW_nv (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (hb : KsW (L.ksb s) b) : + ∀ t ∈ Z.T, b ≠ tVcpuA t := by + obtain ⟨K, h1, h2, -, e, -⟩ := hb + exact J.ks_nv (by omega) (by omega) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The VCPU words kept by a step whose writes miss them** (KENTRY-PLAN +g2b): `VKeep` from a `WritesIn` footprint with no TCB's VCPU word. -/ +theorem JAtZRB.vkeep_of {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {W : U64 → Prop} (hW : P1.WritesIn st s s' W) + (hWV : ∀ b, W b → ∀ t ∈ Z.T, b ≠ tVcpuA t) : VKeep (L := L) Z.th s s' := + VKeep.of_writes J.ip.e.tcb J.thT hW hWV + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- (F1 stage 5, item 4) `vsNamed` across a kernel-state block write (`KsW`). -/ +theorem JAtZRB.vsKs {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hw : P1.WritesIn st s s' (KsW (L.ksb s))) : + (Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s')) := + J.vsW hw fun b hb => by obtain ⟨K, h1, h2, -, e⟩ := hb; exact Or.inr (Or.inl ⟨by omega, by omega, by omega⟩) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {arI} {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem JAtZRB.ksW_ntd (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} (hb : KsW (L.ksb s) b) : + ∀ t ∈ Z.T, b ≠ P1.tdA t := by + obtain ⟨K, h1, h2, -, e, -⟩ := hb + exact J.ks_ntd (by omega) (by omega) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`H`'s SC words are kept by kernel-state writes** (`H` off the block). -/ +theorem JAtZRB.ksW_scKeep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hw : P1.WritesIn st s s' (KsW (L.ksb s))) + {q : U64} {z : Nat} (hq : H q = some (.sc z)) {K : U64} (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + L.mem s' (scW q K) = L.mem s (scW q K) := by + obtain ⟨hal, hv, hf⟩ := J.scW_in hq hK8 hK + have := J.kern.hks q _ hq (fun _ e => by cases e) + simp only [P1.HKind.bits] at this + refine hw.mem_eq hal fun h => ?_ + obtain ⟨K', h1, h2, -, e⟩ := h + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **The scheduler's words a kernel-state step keeps** (as `inSlot_keeps`): +the TCBs, the SCs and the replies lie outside the kernel-state block. -/ +theorem ksW_keepsBR {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hw : P1.WritesIn st s s' (KsW (L.ksb s))) : + (∀ t ∈ Z.T, L.mem s' (mcpA t) = L.mem s (mcpA t) ∧ L.mem s' (flagsA t) = L.mem s (flagsA t)) ∧ (∀ t ∈ Z.T, L.mem s' (yieldA t) = L.mem s (yieldA t)) ∧ + (∀ t ∈ Z.T, L.mem s' (tIpcBufA t) = L.mem s (tIpcBufA t)) ∧ + (∀ q ∈ Z.C, L.mem s' (scReplyA q) = L.mem s (scReplyA q)) ∧ + (∀ r ∈ Z.R, L.mem s' (rPrevA r) = L.mem s (rPrevA r) ∧ L.mem s' (rNextA r) = L.mem s (rNextA r)) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + have hT := hc.tcbs + have hko : ∀ t ∈ Z.T, t.val + 4096 ≤ (L.ksb s).val ∨ (L.ksb s).val + 13568 ≤ t.val := fun t ht => by + rcases hc.ksOut t ht with h | h <;> omega + have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + refine ⟨fun t ht => ⟨ksW_keep hw (mcpA_aligned hT ht) (by rw [mcpA_val hT ht]; rcases hko t ht with h | h <;> omega), + ksW_keep hw (flagsA_aligned hT ht) (by rw [flagsA_val hT ht]; rcases hko t ht with h | h <;> omega)⟩, + fun t ht => ksW_keep hw (yieldA_aligned hT ht) (by rw [yieldA_val hT ht]; rcases hko t ht with h | h <;> omega), + fun t ht => ksW_keep hw (tIpcBufA_aligned (hT t ht)) (by + rw [tIpcBufA_val (hT t ht)]; rcases hko t ht with h | h <;> omega), fun q hq => ?_, fun r hr => ?_⟩ + · obtain ⟨hal, hv, hfit⟩ := sc_reply_word hS hq + obtain ⟨x, n, hk, hx⟩ := hs.scs q hq + have := (hS.scOutIn hq (a := scReplyA q) ⟨by rw [hv]; omega, by rw [hv]; omega⟩).2.2.2 + exact ksW_keep hw hal (Sched.ksb_word_out hal J.kern.ksbAl this) + · have hal := J.rp.align r hr + have h8 := rPrevA_val hal + have h16 := rNextA_val hal + have hk := J.rp.ksb r hr + exact ⟨ksW_keep hw (J.rp.aligned hr (Or.inl h8)) (by omega), ksW_keep hw (J.rp.aligned hr (Or.inr h16)) (by omega)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +set_option maxHeartbeats 4000000 in +/-- **`JStepHyps.ks`**, with no premise, the dead set kept: FL4 writes kernel-state words only +(`ksW_keeps`), the spec keeps the heap and the scheduler's fields (`KsStep`, +so `CnStep`); the untyped caps' facts kept (the caps and their free parts lie +outside the kernel-state block); P1's `BlkOK` kept (`blk_of_capw`: the +kernel-state words are in `Ω` and none is an SC word, `SgOK`). -/ +theorem jowes_ksDBR {AR : A → Fl4Spec.Cr3 → Prop} {th : TypedHeap} (s s' : S) (a a' : A) (σ σ' : Fl4Spec.State Unit) + (hw : P1.WritesIn st s s' (KsW (L.ksb s))) (hO : KsStep σ σ') : + ∀ (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) (Lst : U64 → List U64) (D : Set U64), Z.th = th → + JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ → XTopAt (L := L) AR Z s' a' σ' → + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JRestR (L := L) arI Z H Cs Ac' Lst' D s' σ' := by + intro Z H Cs Ac Lst D hth J hX' + have hC : CnStep σ σ' := ⟨⟨fun p => Or.inl (by rw [hO.kh]), hO.rel, hO.ct, hO.csc, hO.ctime, hO.cons, hO.idle, hO.isc⟩, + hO.rq, hO.act⟩ + obtain ⟨k1, k2, kb, k3, k4⟩ := ksW_keepsBR arI J hw + have ck : Sched.CKeep σ σ' := ⟨fun q y n hq => ⟨y, hC.sc.2 hq, rfl⟩, fun q x hq => ⟨x, (hC.look (fun _ _ e => by cases e) (fun _ e => by cases e)).2 hq, + rfl, rfl⟩, fun p x n hp => ⟨x, hC.sc.1 hp⟩⟩ + have yk : YKeep σ σ' := ⟨fun p x hp => let ⟨y, hy, hc⟩ := hC.tcbF hp; ⟨y, hy, hc.yld⟩, fun p x n hp => ⟨x, hC.sc.2 hp, rfl⟩, + fun p y n hp => ⟨y, hC.sc.1 hp, rfl⟩⟩ + have mk : McpKeep σ σ' := fun p x hp => let ⟨y, hy, hc⟩ := hC.tcbF hp; ⟨y, hy, hc.mcpF⟩ + obtain ⟨hCR', hRP'⟩ := CallRel.carry J.call J.rp ⟨k3, k4⟩ hw.1 ck + have hRF' := RFree.carry J.rfree J.call k4 ck + have hRR' := J.rrecv.frame k4 hC.toHStep.khrb + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := by + have hw' : P1.WritesIn st s s' (fun b => KsW (L.ksb s) b ∧ P1.Aligned b) := + ⟨hw.1, hw.2.1, fun b hb hn => hw.2.2 b hb fun h => hn ⟨h, hb⟩⟩ + have hSg := J.blk.2.2.2.2 + have h0 := J.blk.1.choose_spec.choose_spec + refine (P1.blk_of_capw J.blk hw' fun b hb => ?_).1 + obtain ⟨⟨K, h1, h2, -, e, -⟩, hal⟩ := hb + refine Or.inr ⟨?_, fun hm => ?_⟩ + · have hK8 : K.val % 8 = 0 := by + have := J.kern.ksbAl; have hb8 : b.val % 8 = 0 := hal; omega + have eb : b = core.num.U64.wrapping_add (L.ksb s) K := + UScalar.eq_of_val_eq (by rw [P1.ks_val h0 h2]; exact e) + rw [eb]; exact hSg.ksΩ K h2 hK8 + · have := hSg.ks b hm; omega + have hI : ∀ i : U64, i.val < 128 → capV (L.mem s') (nodeA (L.ksb s) i) = capV (L.mem s) (nodeA (L.ksb s) i) := + irqNode_keep hw J.kern.ksbFit J.kern.ksbAl fun b hb => by + obtain ⟨K, -, -, -, e, h⟩ := hb; omega + have hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by + have hf := J.kern.nodeFit + have hv := nodeAt_val' (st := st) (s := s) (c := 0#u64) (off := layout.node.DEBUG_TCBS) (by simp; omega) + (by simp [layout.node.DEBUG_TCBS]) + exact ksW_keep hw J.kern.dwAl (by rw [hv]; omega) + have hkern := J.kern_keep hw.1 hw.2.1 hX'.fin.1.1.1 hO.ct hO.csc (fun sl h => Or.inl (by rw [← hO.orig]; exact h)) (by rw [hO.gpml4, hO.huge]) (fun p ao h => by rw [hO.kh]; exact h) + hDbg (D' := D) + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b hb => by obtain ⟨K, h1, h2, -, e⟩ := hb; exact Or.inr (Or.inl ⟨by omega, by omega⟩) + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_hstep hX' hC.toHStep k3 (fun q z hq K h8 hK => J.ksW_scKeep hw hq h8 hK) (J.act.cn hC).1 + (J.yld.frame k2 yk) yk (J.jcov.dead q hq) + -- the untyped caps' facts: the caps kept, the free parts outside the kernel-state block + have hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + have hfi := U.fiLe + have hk8 := J.kern.ksbAl + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, fun g w hal h1 h2 => ?_, U.sb4, + U.nostr, U.nsFree, fun h0 => U.ns (by rw [hO.cdt] at h0; exact h0), + fun g h0 hf0 => ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [hO.gpml4]; exact U.gReg (B g)⟩ + · have hw8 : w.val % 8 = 0 := hal + have hapart := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).1 + rw [ksW_keep hw hal (by omega)] + exact U.zero (B g) w hal h1 h2 + · rw [hO.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← hO.orig]; exact h), by rw [hna, hDbg]; exact r3⟩ + exact ⟨Ac, Lst, ⟨hCR', hRF', hRR', J.yld.frame k2 yk, J.mcp.frame k1 mk, + J.buf.frame kb (fun p x hp => let ⟨y, hy, hc⟩ := hC.tcbF hp; ⟨y, hy, hc.buf⟩), hRP', J.apart.sc_frame hw.1 (SzKeep.of_kheap hO.kh), + J.jpl_keep arI hSS hO.kh hw.1 hI (J.idleW_keep hX'.fin.1.1.1 hC.idle hC.isc).2, J.jcov_keep hw.1 hSS hdead (J.idleW_keep hX'.fin.1.1.1 hC.idle hC.isc) + (DXKeep.of_dbg (dbgKeep_of_writes J.blk hw fun _ hb => J.ksW_notDbg hb) (J.tdkeep_of hw fun _ hb => J.ksW_ntd hb)), + hkern, hutc, J.nsc.cn hC.toHStep, J.inv.cn hC, (fun e => (J.cur (by rw [← hC.act]; exact e)).cn hC.toHStep), J.own.cn hC.toHStep, J.off.cn hC.toHStep, + J.act.cn hC, + J.bsa.cn hC.toHStep, J.isorted.cn hC.toHStep, J.curRun.cn hC.toHStep (Or.inr hC.act), hblk⟩⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} + +set_option maxHeartbeats 4000000 in +/-- **A write of `WORK_UNITS` keeps `JAtZ`** (module docstring), with its write set (gen 42b52). -/ +theorem JAtZRB.workUnitsJD_ws {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (v : U64) : + P1.WritesIn st s (L.wr s (core.num.U64.wrapping_add (L.ksb s) layout.ks.WORK_UNITS) v) + (· = core.num.U64.wrapping_add (L.ksb s) layout.ks.WORK_UNITS) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D + (L.wr s (core.num.U64.wrapping_add (L.ksb s) layout.ks.WORK_UNITS) v) a σ ∧ + (JExt (L := L) Z.th s → JExt (L := L) Z.th (L.wr s (core.num.U64.wrapping_add (L.ksb s) layout.ks.WORK_UNITS) v)) := by + have hX := J.xtop + have hf := J.kern.ksbFit + have hal := J.kern.ksbAl + have hv := workUnits_val hf + set w := core.num.U64.wrapping_add (L.ksb s) layout.ks.WORK_UNITS with hwd + have ha : P1.Aligned w := by unfold P1.Aligned; rw [hv]; omega + set s' := L.wr s w v with hs' + have hw : P1.WritesIn st s s' (KsW (L.ksb s)) := + (P1.writesIn_wr ha).mono fun b hb => ⟨40#u64, by decide, by decide, by decide, by rw [hb, hv]; rfl, + Or.inl (by decide)⟩ + have hne : ∀ b : U64, P1.Aligned b → b.val ≠ (L.ksb s).val + 40 → L.mem s' b = L.mem s b := fun b hb hb' => + P1.LawfulStore.mem_wr_ne ha hb (by rw [hv]; omega) + have hirq : IrqRel (L.mem s') (L.ksb s) σ := by + have hR := hX.fin.1.1.2.2.2.1 + refine ⟨fun i hi => ?_, hR.node⟩ + have e := irqStA_val hf hi + rw [hne _ (by unfold P1.Aligned; rw [e]; omega) (by rw [e]; omega)] + exact hR.st i hi + have hio : IoRel (L.mem s') (L.ksb s) σ := by + have hR := hX.fin.1.1.2.2.2.2.1 + intro p hp + have hw' : (wI p).val < 1024 := by rw [wI_val hp]; omega + have e := ioA_val hf hw' + unfold bitOf + rw [hne _ (by unfold P1.Aligned; rw [e]; omega) (by rw [e]; omega)] + exact hR p hp + have hdk : DK4 (L := L) s s' := ⟨by rw [hs', P1.LawfulStore.ksb_wr], fun o ho => by + obtain ⟨h1, h2⟩ := domOffs0_lt ho + have e : (core.num.U64.wrapping_add (L.ksb s) o).val = (L.ksb s).val + o.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + have h40 : o.val ≠ 40 := by + simp only [domOffs0, List.mem_cons, List.mem_nil_iff, or_false] at ho + rcases ho with rfl | rfl | rfl | rfl | rfl | rfl <;> decide + exact hne _ (by unfold P1.Aligned; rw [e]; omega) (by rw [e]; omega)⟩ + have hD := hX.rel.2.2.2.1 + have hAR := hX.fin.1.1.2.2.2.2.2.2 + have hX1 : XTopAt (L := L) AR Z s' a σ := XTopAt.ksStep hX J.ks hw (KsStep.refl σ) hirq hio + (hD.frame (fun o ho => hdk.2 o ho) rfl rfl) hAR + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + hX.slotsSame_of hw fun b hb => by obtain ⟨K, h1, h2, -, e⟩ := hb; exact Or.inr (Or.inl ⟨by omega, by omega⟩) + obtain ⟨Ac', Lst', R⟩ := jowes_ksDBR arI s s' a a σ σ hw (KsStep.refl σ) Z H Cs Ac Lst D rfl J hX1 + refine ⟨P1.writesIn_wr ha, ?_⟩ + exact ⟨Ac', Lst', J.cs_frameK (hvn := J.vsKs hw) hw.1 hX1 (KBack.of_kheap rfl) (IpcFrKeep.of_kheap rfl) (J.carry.frame subset_rfl (IpcFrKeep.of_kheap rfl) ((KSz.of_kheap rfl))) (JCs.sameBR J hSS (KSz.of_kheap rfl)) R (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) + hSS (dbgKeep_of_writes J.blk hw fun _ hb => J.ksW_notDbg hb) rfl, fun E => E.ss hSS (J.vkeep_of hw fun _ hb => J.ksW_nv hb)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +private theorem JAtZRB.ndFBase (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (v16 v24 v40 : U64) (di : Nat) (h24 : v24 = 0#u64) : + ∃ (Ac0 : Set U64) (Lst0 : U64 → List U64) (D0 : Set U64), + JAtZRB (L := L) arI AR Z H Cs Ac0 Lst0 D0 (ndF (L := L) s (L.ksb s) v16 v24 v40) a (ndFSt σ v16.bv di) := by + have hdom := schedStep_dom2 Z.th s σ v16 v24 di rfl J.kern.ksbFit J.kern.ksbAl h24 + obtain ⟨Ac1, Lst1, D1, J1⟩ := J.schedJ hdom + obtain ⟨Ac2, Lst2, J2, -⟩ := (J1.workUnitsJD_ws arI v40).2 + have e : core.num.U64.wrapping_add (L.ksb (ndF2 (L := L) s (L.ksb s) v16 v24)) layout.ks.WORK_UNITS = + ksW (L.ksb s) 40#u64 := by + simp only [ndF2, P1.LawfulStore.ksb_wr] + unfold layout.ks.WORK_UNITS + rfl + rw [e] at J2 + exact ⟨Ac2, Lst2, D1, J2⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +private theorem NdW.val_bounds {kb b : U64} (h : NdW kb b) (hk : kb.val + 13568 ≤ 2 ^ 64) : + kb.val + 16 ≤ b.val ∧ b.val ≤ kb.val + 40 := by + rcases h with rfl | rfl | rfl + · rw [ksW_val (by change kb.val + 16 < 2 ^ 64; omega : kb.val + (16#u64 : U64).val < 2 ^ 64)]; simp <;> omega + · rw [ksW_val (by change kb.val + 24 < 2 ^ 64; omega : kb.val + (24#u64 : U64).val < 2 ^ 64)]; simp <;> omega + · rw [ksW_val (by change kb.val + 40 < 2 ^ 64; omega : kb.val + (40#u64 : U64).val < 2 ^ 64)]; simp <;> omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +private theorem JAtZRB.ndFDbg (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (v16 v24 v40 : U64) : P1.DbgKeep st (P1.Heap.tcbs H) Cs + (ndF (L := L) s (L.ksb s) v16 v24 v40) s := by + have hks := ndF_ksb (L := L) s (L.ksb s) v16 v24 v40 + refine ⟨hks.symm, fun b hb => ?_⟩ + have hb' := P1.DbgW.ksb hks hb + refine (ndF_mem_ne s v16 v24 v40 J.kern.ksbFit J.kern.ksbAl (P1.dbgW_aligned J.blk hb') ?_).symm + intro hnd + have hval := hnd.val_bounds J.kern.ksbFit + exact J.notDbg' (fun p hp => by + obtain ⟨⟨_, _, hq⟩, -⟩ := J.blk + have hOut := hq.ksOut p hp + change b.val < p.val ∨ p.val + 4096 ≤ b.val + rcases hOut with h | h <;> omega) + (fun c _ => by + change b.val ≠ (L.ksb s).val + 13568 + c.val * 4288 + 88 + omega) hb' + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +/-- `RootFr` at a `JAtZRB` state, given `IdleVt` (`rootFr_of`). -/ +theorem JAtZRB.rootFr {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hIV : IdleVt σ) : RootFr (L := L) AR Z.T Z.P Z.c s a σ := + rootFr_of J.xtop J.kern J.nk J.jpl J.zH.1 J.vsc hIV + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`SlotsApart` after a scheduler step.** -/ +theorem JAtZRB.sch_apart {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') : + Sched.SlotsApart (st := st) Z.th Z.T Z.N Z.C Z.R Z.c s' σ' := by + have hz0 := hK.toTK.szKeep + have hz : Sched.SzKeep σ σ' := hz0 + exact J.apart.sc_frame hw.1 hz + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`BufWordRel` after a scheduler step** (`sc_buf` through `toTK`). -/ +theorem JAtZRB.sch_buf {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') : + BufWordRel Z.T (L.mem s') σ' := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hS.lists + have hb0 := hK.toTK.bufKeep + have hb : BufKeep σ σ' := hb0 + exact J.buf.frame (fun t ht => hw.mem_eq (tIpcBufA_aligned (hc.tcbs t ht)) fun hb => hb.ne_buf hS ht rfl) hb + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`JPlaced` after a scheduler step.** -/ +theorem JAtZRB.sch_jpl {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') + (hiw : Sched.idleScW (L := L) s' = Sched.idleScW (L := L) s) : + ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s' σ' := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hz0 := hK.toTK.szKeep + have hz : Sched.SzKeep σ σ' := hz0 + have hwU : P1.WritesIn st s s' (UFoot st Z.T Z.N Z.C Z.R Z.c s σ) := hw.mono fun b hb => Or.inl hb + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := slotsSame_of_ufoot hJ.pl hS J.rp J.apart hwU + have hIA := hJ.irqApart (c := Z.c) J.zH.1 J.zH.2.2.2 J.c0 J.zH.2.2.1 J.kern.ksbFit + exact ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => hz q.bv y n hq) hw.1 (irq_of_ufoot hS J.rp hIA hwU) hiw⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **The new current SC after a scheduler step**: `H`'s (the spec's SC, +`JPlaced`) and not dead (bound to the current thread, `CurInv`: its `TCB` +word is not 0, as a dead SC's is). -/ +theorem JAtZRB.sch_csc {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') + (hS' : SchedRel st Z.N Z.T Z.C Z.c s' σ') (hI' : Inv σ') (hC' : CurInv σ') + (hdead : ∀ q ∈ D, Sched.DeadP (L := L) H s' q) : H (L.cscW s') ≠ none ∧ L.cscW s' ∉ D := by + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + have ecs : L.cscW s' = Sched.nodeW st s' Z.c layout.node.CUR_SC := by + unfold P1.LawfulStore.cscW P1.LawfulStore.nodeA Sched.nodeW; rw [hc'.cur] + have hcv0 : σ'.curSc = (L.cscW s').bv := by rw [ecs]; exact hs'.curSc + obtain ⟨x, n, hx⟩ := hC'.sc + have hobj0 := J.obj.tkw hK.toTK + obtain ⟨c, hcC, hcv⟩ := hobj0.sc _ _ _ hx + have hcw : c = L.cscW s' := P1.eq_of_bv (hcv.trans hcv0) + rw [← hcw] + obtain ⟨Tr, Cq, hJ⟩ := J.sch_jpl hw hK (Sched.idleScW_keep_of J.xtop.fin.1.1.1 hS' hK.isc) + have hxc : σ'.kheap c.bv = some (.schedContext x n) := by rw [hcv]; exact hx + have hHc := (hJ.sc c x n (J.c0 c hcC) hxc).2 + refine ⟨(by rw [hHc]; exact fun h => by cases h), fun hD => ?_⟩ + have d := ((hdead c hD).2.2.2.2 _ hHc).1 + obtain ⟨x', n', hk', hR⟩ := hs'.scs c hcC + rw [hxc] at hk'; cases hk' + obtain ⟨u, -, xt, -, -, hown, -⟩ := curOwn_of_curInv hS' hI' hC' + have h1 := hR.tcb + rw [hown x n hx, show scW c 24#u64 = P1.scTcbA c from scW_tcb c, d, optRef_zero] at h1 + cases h1 + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 1000000 in +/-- **`JAtZ` across a scheduler step** (module docstring). -/ +theorem JAtZRB.sch_frame {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') + (hX : XTopAt (L := L) AR Z s' a' σ') + (hcall : Sched.CallRel (L.mem s') Z.R Z.C σ' Lst') (hrf : Sched.RFree (L.mem s') Z.R Z.C σ' Lst') + (hyl : Sched.YieldRel Z.T Z.C (L.mem s') σ') (hmcp : Sched.McpRel Z.T (L.mem s') σ') + (hbuf : BufWordRel Z.T (L.mem s') σ') + (hrp : Sched.RPlaced st Z.N Z.T Z.C Z.R Z.c s' σ') (hap : Sched.SlotsApart (st := st) Z.th Z.T Z.N Z.C Z.R Z.c s' σ') + (hjpl : ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s' σ') (hjc : Sched.JCovD (L := L) Z.th H Z.G D' Cs s') + (hkern : JKern (L := L) arI H Cs D' Z.th s' σ') + (hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi) + (hnsc : Sched.NSC Z.N Z.C σ') (hinv : Sched.Inv σ') (hcur : σ'.schedulerAction = .chooseNewThread ∨ Sched.CurInv σ') + (hown : Sched.CurOwnW Z.T Z.C σ') (hoff : Sched.OffOK σ') + (hact : Sched.ActIdle σ') (hbsa : Sched.BlockedScActive σ') (hcr : CurRunM σ') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (hidl : L.idleW s' = L.idleW s) (hDD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D') : + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ' := by + have hT := hK.toTK + have hobj0 := J.obj.tkw hT + have hobj : Sched.ObjCover Z.N Z.T Z.E Z.R Z.C σ' := + ⟨hobj0.tcb, hobj0.ntfn, hobj0.ep, hobj0.reply, hobj0.sc⟩ + have hcov0 := J.cover.tkw hT + have hcov : Sched.SpecCover H σ' := ⟨hcov0.ntfn, hcov0.ep, hcov0.reply, hcov0.sc0, hcov0.arch⟩ + have hsrt0 := J.isorted.tkw hT + have hsrt : Sched.IpcSorted σ' := hsrt0 + have hksz0 := hT.ksz + have hksz : KSz σ σ' := hksz0 + have hks : L.ksb s' = L.ksb s := hw.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl' : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hsl : Sched.ScLinkHyp σ' := scLink_rebuild hX J.tcbs_eq.1 (hobj) hblk J.hset.2.1 hjpl J.c0 + have hsv : Sched.SchedValid σ' := schedValid_rebuild hX J.tcbs_eq.1 J.tcbs_eq.2 (hobj) hblk J.hset.1 + J.hset.2.1 hcall hyl J.c0 hinv hsl + refine ⟨hX, hcall, hrf, + J.rrecv.frame (Sched.TFoot.keep J.xtop.fin.1.1.1 J.rp hw).2.2 (Sched.KhRB.of_otk hK.kh), hyl, hmcp, hbuf, + by rw [hks]; exact J.ks, J.thT, J.thk, J.ip, hrp, hap, + fun n hn => by rw [hks]; exact J.nk n hn, + fun wp hw c hc => by simp only [P1.LawfulStore.nodeBase, hks]; exact J.nodeB wp hw c hc, hjpl, hcov, hobj, J.zH, ?_, J.hset, J.hscC, hjc, hkern, + J.del.of_same hSS, UtInv.of_same hM J.ut hSS, J.bound, fun G hG => ?_, hutc, hnsc, hinv, curInvR_of hcur, hown, hoff, hact, + J.c0, hbsa, hsv, hsrt, hsl, hcr, hblk, by rw [hks]; exact J.intL, + J.vsc.of_eq fun y hy => hSS.capV hy, + J.bok.keep (IpcFrKeep.of_otk hK.kh), J.fcov.ipcFr (IpcFrKeep.of_otk hK.kh), + J.fcap.keepU (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) (IpcFrKeep.of_otk hK.kh).page, + J.liveX hX rfl rfl rfl hSS hKd hK.orig hidl (fun p hp => hjc.dead p (hDD p hp)), J.zDX hSS hDD, + (J.carry.frame subset_rfl (IpcFrKeep.of_otk hK.kh) hksz).withP + (J.carry.devKeep J.xtop.rel.2.1.2.2.1 hks hSS hX.rel.2.1.2.2.1 (idleH_of hX hobj J.zH.1 hjpl hinv hact)) + (J.carry.vsSame hSS hks (ArchFwd.of_ipcFr (IpcFrKeep.of_otk hK.kh))) + (J.carry.tabKeep J.xtop.rel.2.1.2.2.1 hSS hX.rel.2.1.2.2.1 hksz) + (J.vsW hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h)⟩ + · refine SpecFree.frame J.sfree (fun x' hx' p sb fi' d hc => ⟨x', hx', fi', ?_, le_rfl⟩) hksz + rw [← hSS.capV (hsl' x' hx')]; exact hc + · have hG0 : MdbLinks Z.th (L.mem s) G := hG.of_same_symm hSS + have hsl' : ∀ x ∈ G.chains.flatten, IsSlot Z.th x := fun x h => ((hG0.members x).1 h).1 + exact (J.finM G hG0).of_capV fun x hx => hSS.capV (hsl' x hx) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +/-- **`JKern` across a scheduler step** (module docstring): the current SC +word's heap object (`hcscH`), the node-0 +`DEBUG_TCBS` word kept and apart from the new thread's state word (`hDbg`, +`hdw`) given. -/ +theorem JAtZRB.sch_kern {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') + (hS' : SchedRel st Z.N Z.T Z.C Z.c s' σ') + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) + (hdw : (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS).val ≠ (P1.stA (L.curW s')).val) + (hcscH : H (L.cscW s') ≠ none) : JKern (L := L) arI H Cs D Z.th s' σ' := by + have K := J.kern + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have ecw : L.curW s' = Sched.nodeW st s' Z.c layout.node.CUR_THREAD := by + unfold P1.LawfulStore.curW Sched.nodeW; rw [hc'.cur] + have hcT : L.curW s' ∈ Z.T := ecw ▸ hc'.curT + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp only [P1.LawfulStore.nodeBase, hks] + have hT := hK.toTK + obtain ⟨pt, hpt⟩ := K.gpm + refine ⟨fun sl h => K.orig sl (by rw [← hK.orig]; exact h), by rw [ecw]; exact hs'.curThread, + (hJ.tcb _).2 (J.zH.1 _ hcT), by rw [hcpu, hna]; exact K.act, by rw [hna]; exact K.dwAl, + by rw [hna]; exact K.dwObj, by rw [hna, hna, hcpu]; exact ⟨hdw, K.dwNe.2⟩, ?_, + ⟨pt, by rw [hK.gpml4]; exact hT.archObj hpt⟩, by rw [hK.gpml4]; exact K.ga, by rw [hK.gpml4]; exact K.gf, + K.heap, K.h0, by rw [hks]; exact K.hks, by rw [hks]; exact K.hksIn, fun p k hp c hc => by rw [hnb]; exact K.hnd p k hp c hc, + by rw [hks]; exact K.ksbFit, by rw [hks]; exact K.ksbAl, by rw [hks, hcpu]; exact K.nodeFit, + by rw [hcpu]; exact K.cpuCs, J.zH.1 _ hcT, hcscH, K.csLt, K.cs0, K.csN, K.tdx.frame hK.huge⟩ + rw [hna, hDbg]; exact K.head + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +set_option maxHeartbeats 1000000 in +/-- **The untyped caps' facts after a scheduler step** (`sc_utc`'s proof; the +global PML4 kept by `SchStep`): the caps kept (slots +apart), the free parts zero (no SC-step word there: the TCBs are typed, the +notifications and SCs are spec objects outside it, `SpecFree`, the node is +outside every untyped cap, `JCovD`), the rest by the kept kernel state and +the node-0 `DEBUG_TCBS` word (`hDbg`, P1-supplied). -/ +theorem JAtZRB.sch_utc {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.SchStep P σ σ') + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b h => Sched.tfoot_oobj hS h + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hfi := U.fiLe + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [hK.cdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [hK.gpml4]; exact U.gReg (B g)⟩ + · intro g w hal h1 h2 + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases tfoot_tcb hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hth : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y .tcb hth + simp only [objSize] at this; omega + · have := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).2.1 Z.c hcC + omega + · obtain ⟨x, hx, -⟩ := hR.ntfn n hn + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) n.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.ntfnBits, UScalar.bv_toNat] at this + omega + · have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) sc.bv _ hk + simp only [Fl4Spec.objBits, UScalar.bv_toNat] at this + rw [Nat.add_comm Fl4Spec.minSchedContextBits n] at this + omega + · intro g h0 hf0 + rw [hK.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← hK.orig]; exact h), by rw [hna, hDbg]; exact r3⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +private theorem JAtZRB.schTailF (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} {Ac' : Set U64} + (hSch : SchS σ σ') (hik : σ'.idleThread = σ.idleThread ∧ σ'.idleSc = σ.idleSc) + (hw : P1.WritesIn st s s' (KFoot st Z.T Z.N Z.C Z.c s σ)) + (hS : SchedRel st Z.N Z.T Z.C Z.c s' σ') (hR : NtfnStateRel Z.N Z.T (L.mem s') σ') + (hN : NSC Z.N Z.C σ') (hI : Inv σ') (hC : CurInv σ') (hA : ActIdle σ') + (hcr : CurRunW σ') (hoff : Sched.OffOK σ') (hbsa : Sched.BlockedScActive σ') + (hT : SchTailHyps (L := L) AR Z H Cs D s s' a' σ' Ac') : + ∃ Lst' : U64 → List U64, JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a' σ' := by + have hK := hSch.1 + have hwT := WritesIn.tfoot_of_kfoot hw + have hX := xtop_sched_step J.xtop J.ip J.nk hwT (ct := σ.curThread) + { hK with kh := fun p => (hK.kh p).mono False.elim } hS hR hT.curOK hT.ar + obtain ⟨-, hY, hM, ⟨Lst', hCR, hRF⟩, hRP, -⟩ := + ScAll.of_k J.xtop.fin.1.1.1 J.rp J.yld J.mcp J.call J.rfree ⟨⟨hS, hR, hN, hI⟩, hw, hSch.2⟩ + have hIdle := J.idleW_keep hS hik.1 hik.2 + have hCov := J.sc_jcov (D' := D) hwT hT.dead (fun _ h hnot => (hnot h).elim) hIdle hT.dbgK subset_rfl + have hKern := J.sch_kern hwT hK hS hT.dbg hT.dwNe (J.sch_csc hwT hK hS hI hC hT.dead).1 + exact ⟨Lst', J.sch_frame hwT hK hX hCR hRF hY hM (J.sch_buf hwT hK) hRP (J.sch_apart hwT hK) + (J.sch_jpl hwT hK hIdle.2) hCov hKern (J.sch_utc hwT hK hT.dbg) hN hI (Or.inr hC) + (Or.inl (curOwn_of_curInv hS hI hC)) hoff hA hbsa (Or.inr hcr) hT.blk hT.dbgK hIdle.1 J.zD⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +private theorem JAtZRB.scheduleFootN (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {s' : S} {σ' : Fl4Spec.State Unit} + (hw : P1.WritesIn st s s' (KFootN st Z.T Z.N Z.C Z.c s σ)) + (hK : SchStep (fun _ => False) σ σ') : NatFoot (L := L) Z s σ s' σ' := by + have hS := J.xtop.fin.1.1.1 + have hb := fun b (h : NdW (L.ksb s) b) => h.val_bounds J.kern.ksbFit + refine NatFoot.ofW J.xtop hw (fun b h => ?_) (fun b h => ?_) (fun b h => ?_) + (natK_otk hK.kh fun _ h => h.elim) + · rcases h with h | h + · exact tfoot_oobj hS (Or.inl h) + · obtain ⟨hlo, hhi⟩ := hb b h + exact Or.inr (Or.inl ⟨by omega, by omega, Or.inl (by omega)⟩) + · rcases h with h | h + · exact tfoot_nv hS (Or.inl h) + · obtain ⟨hlo, hhi⟩ := hb b h + exact J.ks_nv (by omega) (by omega) + · rcases h with h | h + · exact J.tfoot_nh (Or.inl h) + · obtain ⟨-, hhi⟩ := hb b h + intro e + rw [e] at hhi + change (core.num.U64.wrapping_add (L.ksb s) layout.ks.VCPU_HEAD).val ≤ (L.ksb s).val + 40 at hhi + rw [vHead_val J.kern.ksbFit] at hhi + omega + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +/-- `VsRootOK` at a `JAtZRB` state (`vsRootOK_ofJ`). -/ +theorem JAtZRB.vsRootOK {s : S} {a : A} {σ : Fl4Spec.State Unit} (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + VsRootOK Z.T σ := + vsRootOK_ofJ J.xtop J.jpl J.zH.1 J.vsc + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +set_option maxHeartbeats 4000000 in +set_option maxRecDepth 2000 in +/-- The FPU-aware and domain-switching sibling of `JAtZB.jat_scheduleFRG`. -/ +theorem JAtZRB.jat_scheduleFRGF (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hd : σ.domainTime ≠ 0 ∨ NdOK (L := L) s σ) (hnd : Fl4Spec.numDomains = 1) + (hAT : P0.ArchTotal arI) (hAK : ARKeep arI AR) (hRef : ArchRefines arI AR) + {F : Fl4Spec.State Unit → Fl4Spec.State Unit → Prop} {Pf : S × A → Fl4Spec.State Unit → Prop} + (hfJ : timer.fire st arI s a ⦃ r => ∃ σ1, F σ σ1 ∧ (σ1.domainTime ≠ 0 ∨ NdOK (L := L) r.1 σ1) ∧ + (∃ (V1 : XVs) (Ac1 : Set U64) (Lst1 : U64 → List U64) (D1 : Set U64), + JAtZRB (L := L) arI AR (Z.mv Z.G V1) H Cs Ac1 Lst1 D1 r.1 r.2 σ1) ∧ + FpuOwnOK σ1 ∧ IdleVt σ1 ∧ Pf r σ1 ⦄) + (hAF : ArchSwF st arI Z.N Z.T Z.C Z.c) (hAN : ArchNdF st Z.N Z.T Z.C Z.c) + (hsni : SniHyp st arI Z.N Z.T Z.C Z.c) (hsend : SendHyp st arI Z.N Z.T Z.C Z.c) (hT : TimeAgree arI) : + sched.schedule st arI s a ⦃ r => ∃ σ1 σ', F σ σ1 ∧ + Fl4Spec.schedule (Z := Unit) σ1 = ({((), σ')}, False) ∧ + (∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZRB (L := L) arI AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.1 r.2 σ') ∧ CurInv σ' ∧ CurRunW σ' ∧ + ∃ rf, Pf rf σ1 ∧ NatFoot (L := L) Z rf.1 σ1 r.1 σ' ⦄ := by + have hB := J.blk + obtain ⟨⟨Q, R, hq⟩, hBd, hXi, hSc, hSg⟩ := id hB + have hNt : ∀ n ∈ P1.Heap.ntfns H, H n = some .ntfn := fun _ h => h + obtain ⟨rf, hfr, σ1, hF, hdt, ⟨V1, Ac1, Lst1, D1, J1⟩, hFO, hIV, hPf⟩ := spec_exists hfJ + have hkf : L.ksb rf.1 = L.ksb s := by + obtain ⟨s1, a1⟩ := rf + exact (spec_ok_post (P1.timer.fire.kw hAT (P1.ibKeep_holds hAT) hNt s a hB J.bufAtJ) hfr).1 + have hfire : timer.fire st arI s a ⦃ r => ∃ τ, F σ τ ∧ SchedRel st Z.N Z.T Z.C Z.c r.1 τ ∧ + NtfnStateRel Z.N Z.T (L.mem r.1) τ ∧ NSC Z.N Z.C τ ∧ SchedPreW0R Z.T Z.C τ ∧ + ((τ.domainTime ≠ 0 ∨ NdOK (L := L) r.1 τ) ∧ + (∀ n ∈ Z.N, n.val + 40 ≤ (L.ksb r.1).val ∨ (L.ksb r.1).val + 13568 ≤ n.val) ∧ + (r = rf ∧ τ = σ1)) ⦄ := by + rw [hfr, WP.spec_ok] + exact ⟨σ1, hF, J1.xtop.fin.1.1.1, J1.xtop.fin.1.1.2.2.1, J1.nsc, + ⟨J1.inv, J1.cur, J1.off, J1.act, J1.own⟩, hdt, J1.nk, rfl, rfl⟩ + have hSched := schedule_refinesWP0FRA (P := (Z.mv Z.G V1).P) hRef L.total hAT hAK s a σ J.xtop.fin.1.1.1 + J.xtop.fin.1.1.2.2.1 J.nsc hnd hfire + (fun _ _ h => h.2.symm ▸ hFO) (fun _ _ h => h.2.symm ▸ J1.vsRootOK) + (fun _ _ h => by + rcases h with ⟨rfl, rfl⟩ + exact ⟨J1.rootFr hIV, J1.xtop.fin.1.1.2.2.2.2.2.2⟩) + hAF hAN hsni hsend hT + have hIdx := P1.sched.schedule.idx hAT (P1.ibKeep_holds hAT) hNt s a hB J.kern.ksbFit J.kern.ksbAl + (J.kern.kwApartJ (s' := s) rfl (fun c hc => J.blk.2.2.1.2.2.tm.npl c hc)) J.bufAtJ + (fun s1 hf => J.fire_ks hf (fun b hb o ho h8 => (J.bufAtJ.1 b hb).ks o ho h8)) + (idxI_of J.xtop.rel.2.2.2.1 J.xtop.fin.1.1.1 hd) + refine WP.spec_mono (WP.spec_and hSched + (WP.spec_and (P1.sched.schedule.kw hAT (P1.ibKeep_holds hAT) hNt s a hB J.bufAtJ) + (WP.spec_and (P1.sched.schedule.tinv hAT (P1.ibKeep_holds hAT) hNt s a Q R hq hBd hSc hXi hSg J.bufAtJ) hIdx))) ?_ + rintro ⟨s', a'⟩ ⟨⟨τ, σ', -, hsc, hS', hR', hN', hI', hC', hA', rf1, ⟨hRf, hτ⟩, hw, σ3, ho, hr, he, hAR⟩, + hkw, htinv, hidx⟩ + subst rf1 + subst τ + simp only [WP.uncurry'_pair] at hkw htinv + try simp only [WP.uncurry'_pair] at hidx + obtain ⟨rf', hrf', -, hkw⟩ := hkw + have er : rf = rf' := P1.ok_inj_ff (hfr.symm.trans hrf') + subst rf' + obtain ⟨⟨Q', R', hq'⟩, hBd', hXi', hSc', -, -, -, -⟩ := htinv + let v16 := L.mem s' (ksW (L.ksb rf.1) 16#u64) + let v24 := L.mem s' (ksW (L.ksb rf.1) 24#u64) + let v40 := L.mem s' (ksW (L.ksb rf.1) 40#u64) + let s0 := ndF (L := L) rf.1 (L.ksb rf.1) v16 v24 v40 + let σ0 := ndFSt σ1 v16.bv σ'.domainIndex + have hv24 : v24 = 0#u64 := by dsimp [v24]; rw [hkf]; exact hidx + obtain ⟨Ac0, Lst0, D0, J0⟩ := J1.ndFBase v16 v24 v40 σ'.domainIndex hv24 + have hw0 : P1.WritesIn st s0 s' (KFoot st Z.T Z.N Z.C Z.c s0 σ0) := + ndF_writes σ'.domainIndex J1.kern.ksbFit J1.kern.ksbAl hw + have hdbgK := (J1.ndFDbg v16 v24 v40).trans (P1.dbgKeep_schedW J1.blk J1.kern.ksbFit hkw) + have hdbg := hdbgK.2 _ (Or.inr ⟨0#u64, J0.kern.cs0, rfl⟩) + have hdw : (L.nodeAt s0 0#u64 layout.node.DEBUG_TCBS).val ≠ (P1.stA (L.curW s')).val := by + rw [← P1.nodeAt_of_ksb hdbgK.1] + have hNode := hq'.nodes 0#u64 J0.kern.cs0 + have ht : L.curW s' ∈ P1.Heap.tcbs H := hq'.curT + have hv := P1.field_val (hq'.tcbs _ ht) (K := 3328#u64) (by decide) + have hStateVal : (P1.stA (L.curW s')).val = (L.curW s').val + 3328 := by + rw [P1.stA_eq, hv] + rfl + rw [P1.nodeAt_val hNode (off := layout.node.DEBUG_TCBS) (by simp), P1.debug_val, hStateVal] + have := hNode.disj _ ht + simp only [Nat.reducePow] at this + omega + have hSch : SchS σ1 σ' := schs_schedule.of_eq hsc + have hSch0 : SchS σ0 σ' := SchS.ndF hSch v16.bv σ'.domainIndex + have hwT := WritesIn.tfoot_of_kfoot hw0 + have hSS := slotsSame_of_iw J0.xtop.rel.1 J0.xtop.rel.2.2.2.2.2.1 J0.xtop.rel.2.2.2.2.2.2 hwT + (fun b hb => tfoot_iw J0.xtop.fin.1.1.1 hb) + obtain ⟨_, _, hc, hcT⟩ := hS'.lists + let t := Sched.nodeW st s' Z.c layout.node.CUR_THREAD + have hth : Z.th t = some .tcb := J0.thTcb hc.curT + have hSlot : IsSlot Z.th (slotOf t 1) := ⟨t, 1, Or.inr ⟨hth, by decide⟩, rfl⟩ + have hCur : Vs.CurOK st Z.th s' σ' := by + refine ⟨t, hcT.curThread.symm, hth, fun p asid h => ?_⟩ + rw [hSS.capAt hSlot] at h + have hV := J0.vsc _ hSlot + rw [capV_of_capAt h] at hV + exact (hV.2.2 rfl).1 + have hX := xtop_sched_step J0.xtop J0.ip J0.nk hwT (ct := σ0.curThread) + { hSch0.1 with kh := fun p => (hSch0.1.kh p).mono False.elim } hS' hR' hCur hAR + have hY := (ScAll.of_k J0.xtop.fin.1.1.1 J0.rp J0.yld J0.mcp J0.call J0.rfree + ⟨⟨hS', hR', hN', hI'⟩, hw0, hSch0.2⟩).2.1 + have hL : LK (fun _ => False) σ0 σ' := lkm_schedule σ1 ((), σ') (by rw [hsc]; exact Set.mem_singleton _) + have hDead : ∀ q ∈ D0, Sched.DeadP (L := L) H s' q := fun q hq => + J0.deadP_sch_step hX hwT hSch0.1 hL hA' (J0.ymd hS' hY ((hSch0.2.ykeep rfl).dk q.bv)) + (fun h => h) (J0.jcov.dead q hq) + have hIdle : σ'.idleThread = σ0.idleThread ∧ σ'.idleSc = σ0.idleSc := + ik_schedule σ1 ((), σ') (by rw [hsc]; exact Set.mem_singleton _) + have hRun := RunW.scAndTimer (hr J1.curRun) he + have hTail : SchTailHyps (L := L) AR (Z.mv Z.G V1) H Cs D0 s0 s' a' σ' Ac := + ⟨hCur, hAR, hdbg, hdw, hDead, + ⟨⟨Q', R', hq'⟩, hBd', hXi', hSc', hSg.ksb' (hkw.1.trans hkf)⟩, hdbgK⟩ + obtain ⟨Lst', J'⟩ := J0.schTailF hSch0 hIdle hw0 hS' hR' hN' hI' hC' hA' hRun + (OffOK.scAndTimer ho he) (bsa_schedule J1.bsa hsc) hTail + exact ⟨σ1, σ', hF, hsc, ⟨V1, Ac, Lst', D0, J'⟩, hC', hRun, rf, hPf, (J1.scheduleFootN hw hSch.1).cast⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +/-- `NdOK` after FL4's timers, the spec's by `fl4AwakenVF` (`JKern.ndOK_fire`, the domain schedule by +`Vs.awakenVF_dom`). -/ +theorem JAtZRB.ndOK_awakenVF {on rc : Fl4Spec.ObjRef → Bool} {s : S} {a : A} {σ σ1 : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hNd : NdOK (L := L) s σ) {s1 : S} {U : Set U64} + (hf : P1.FootDU st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) U s s1) + (hU : ∀ b ∈ U, ∀ o : U64, o.val < 13568 → o.val % 8 = 0 → core.num.U64.wrapping_add (L.ksb s) o ≠ b) + (hF : ((), σ1) ∈ (Fl4Spec.fl4AwakenVF (Z := Unit) on rc σ).1) : NdOK (L := L) s1 σ1 := by + obtain ⟨hdl, hdi, hds⟩ := Vs.awakenVF_dom hF + exact J.kern.ndOK_fire (fun c hc => J.blk.2.2.1.2.2.tm.npl c hc) hNd hf hU hdl hdi hds + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **One register write of a thread at `JAtZRB`** (XTopJointReply's `JAtZ.regWrJ`, generated copy; post at `Z` with +the `FinW` footprint via `JAtZRB.reg_frameFZ`). -/ +theorem JAtZRB.regWrJZ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (r : Fl4Spec.Register) (v : U64) + (hX2 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c (L.wr s (Endp.regA t (regIdx r)) v) + (updK σ t.bv (.tcb (setReg x r v.bv)))) : + P1.WritesIn st s (L.wr s (Endp.regA t (regIdx r)) v) (Ipc.FinW (st := st) Z.T Z.N Z.C Z.c s σ) ∧ + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s (Endp.regA t (regIdx r)) v) a + (updK σ t.bv (.tcb (setReg x r v.bv))) := by + have hP0 := J.ip + have hT : TcbPlaced Z.T := hP0.e.tcb + have hta := hT t ht + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hT + have hil := regIdx_lt r + set K : U64 := ⟨BitVec.ofNat 64 (256 + 8 * (regIdx r).val)⟩ with hKdef + have hKv : K.val = 256 + 8 * (regIdx r).val := by + show (BitVec.ofNat 64 (256 + 8 * (regIdx r).val)).toNat = _ + rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by omega) + have hra : (Endp.regA t (regIdx r)).val = t.val + 256 + 8 * (regIdx r).val := regA_val hta hil + have hKt : (core.num.U64.wrapping_add t K).val = t.val + K.val := tfield_val hta (by rw [hKv]; omega) + have hreq : Endp.regA t (regIdx r) = core.num.U64.wrapping_add t K := + UScalar.eq_of_val_eq (by rw [hra, hKt, hKv]; omega) + have hwr : P1.WritesIn st s (L.wr s (Endp.regA t (regIdx r)) v) (· = Endp.regA t (regIdx r)) := + writesIn_wr (a_reg hta r).1 + have hblk := (blk_rf J.blk (hwr.mono fun b hb => Or.inl ⟨t, htH, K, by rw [hKv]; omega, + by rw [hKv]; omega, by rw [hb, hreq]⟩)).1 + have hfoot : P1.WritesIn st s (L.wr s (Endp.regA t (regIdx r)) v) (P1.KW st (P1.Heap.tcbs H) Cs + (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) ∅ s) := by + rw [hreq] + exact foot_field hTH htH (by rw [hKv]; omega) (by rw [hKv]; omega) (by rw [hKv]; omega) + ⟨by rw [hKv]; omega, by rw [hKv]; omega⟩ + have hA : QKY σ (updK σ t.bv (.tcb (setReg x r v.bv))) := by + refine ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, fun p => ?_⟩ + by_cases hp : p = t.bv + · subst hp; exact Or.inr (Or.inl ⟨x, setReg x r v.bv, hk, by simp [updK], rfl⟩) + · exact Or.inl (by simp [updK, hp]) + exact J.reg_frameFZ hX2 (hwr.mono fun b hb => Or.inl (Or.inl ⟨t, ht, by rw [hb, hra]; omega, + by rw [hb, hra]; omega⟩)) hA (YK.YKS.updTcb (y := setReg x r v.bv) hk rfl).ykeep + (J.inv.updSame (y := setReg x r v.bv) hk rfl rfl (Or.inl rfl)) hblk hfoot + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- (mmuAD gen 42b66) **One register write of a thread at `JAtZRB`, with the native footprint** (`regWrJZ`; the +spec step one TCB's registers, `natK_tcbW`). -/ +theorem JAtZRB.regWrJFixed (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (r : Fl4Spec.Register) (v : U64) + (hX2 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c (L.wr s (Endp.regA t (regIdx r)) v) + (updK σ t.bv (.tcb (setReg x r v.bv)))) : + JAtZRB (L := L) arI AR Z H Cs Ac Lst D (L.wr s (Endp.regA t (regIdx r)) v) a + (updK σ t.bv (.tcb (setReg x r v.bv))) ∧ + NatFoot (L := L) Z s σ (L.wr s (Endp.regA t (regIdx r)) v) (updK σ t.bv (.tcb (setReg x r v.bv))) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨hwF, B'⟩ := J.regWrJZ ht hk r v hX2 + exact ⟨B', NatFoot.ofW J.xtop hwF (fun b hb => Ipc.FinW.oobj hS hb) (fun b hb => Ipc.FinW.nv hS hb) + (fun b hb => J.finW_nh hb) (natK_tcbW hk rfl fun h => h)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- (mmuAD gen 42b66) **One register write of a thread at `JAtZRB`, with the native footprint** (`regWrJZ`; the +spec step one TCB's registers, `natK_tcbW`). -/ +theorem JAtZRB.regWrJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (r : Fl4Spec.Register) (v : U64) + (hX2 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c (L.wr s (Endp.regA t (regIdx r)) v) + (updK σ t.bv (.tcb (setReg x r v.bv)))) : + ∃ V, JAtZRB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (L.wr s (Endp.regA t (regIdx r)) v) a + (updK σ t.bv (.tcb (setReg x r v.bv))) ∧ + NatFoot (L := L) Z s σ (L.wr s (Endp.regA t (regIdx r)) v) (updK σ t.bv (.tcb (setReg x r v.bv))) := + let ⟨B', F⟩ := J.regWrJFixed ht hk r v hX2 + ⟨Z.vs, B', F⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- **`set_thread_state Running` of a `Restart` thread at `JAtZRB`** (XTopJointReply's `JAtZ.runJD` over `JAtZRB`, +verbatim as P3/JAtZV3RunVm's `JAtZB.runVmJD`; the post adds the `TFoot` footprint). -/ +theorem JAtZRB.runJDB (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .restart) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s6 => ∃ (Lst' : U64 → List U64), + JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) ∧ + SlotsSame Z.th (L.mem s) (L.mem s6) ∧ P1.DbgKeep st (P1.Heap.tcbs H) Cs s s6 ∧ + P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) ⦄ := by + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rw [hst, hrs]; decide, by rw [hst, hrs]; decide⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + have hr0 : L.mem s (P1.stA t) = sched.TS_RESTART := by + apply UScalar.eq_of_val_eq + rw [P1.stA_eq, ← tStA_eq, hst, hrs]; unfold sched.TS_RESTART; rfl + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by rw [hrs]; decide) .running (by decide) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot hS ht _)) (set_thread_state_yw hS ht _)) + (P1.sched.set_thread_state.running_blk (Es := P1.EH H) s t J.blk htH hr0)) ?_ + rintro s6 ⟨⟨⟨⟨hR6, hS6r⟩, hw6⟩, hy6⟩, hblk, hfoot⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + set x6 : Fl4Spec.Tcb := { x with tcbState := .running } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ p, p ≠ t.bv → σ6.kheap p = σ.kheap p := by + intro p hp; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hp] + have hact6 : σ6.schedulerAction = .chooseNewThread ∨ σ6.schedulerAction = σ.schedulerAction := + stsAct_action σ1 t.bv + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_csc : σ6.curSc = σ.curSc := by rw [hrec]; rfl + have e_cons : σ6.consumedTime = σ.consumedTime := by rw [hrec]; rfl + have e_tm : σ6.curTime = σ.curTime := by rw [hrec]; rfl + have e_idle : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have e_rel : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := by + rw [hrs]; exact ⟨rfl, rfl, fun _ e => by cases e⟩ + have hK : ScTKw (fun p => p = t.bv) σ σ6 := by + refine ⟨fun p => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, rfl, rfl, fun _ e => by cases e⟩⟩) + · rw [hkp p hp]; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + -- `Inv` + have hI1 : Inv σ1 := by + refine J.inv.step (NtfnStep.tcb hk .running x.tcbArch) (fun u x0 y h1 h2 _ hr => ?_) + (fun u x0 y h1 h2 hc => ?_) + · by_cases e : u = t.bv + · subst e; rw [hk] at h1; cases h1; simp [hσ1, updK] at h2; subst h2; rfl + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + rw [h1] at h2'; cases h2'; exact hr + · by_cases e : u = t.bv + · subst e; exact J.inv.rd _ x hk (Or.inl (by rw [hrs]; rfl)) + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + exact J.inv.rd u y h2' hc + have hInv : Inv σ6 := hI1.qchange (qchange_stsAct σ1 t.bv) + -- the yield words + have hY6 : YieldRel Z.T Z.C (L.mem s6) σ6 := by + have h1 := J.yld.frame (m' := L.mem s6) (σ' := σ1) + (fun y hy => hy6.mem_eq (yieldA_aligned hT hy) fun h => h ⟨y, hy, rfl⟩) + (YK.YKS.updTcb (y := x6) hk rfl).ykeep + exact h1.frame (m' := L.mem s6) (σ' := σ6) (fun _ _ => rfl) (YKeep.of_kheap (stsAct_kheap _ _)) + have hYK6 : YKeep σ σ6 := (YK.YKS.updTcb (y := x6) hk rfl).ykeep.trans (YKeep.of_kheap (stsAct_kheap _ _)) + have hsp : Fl4Spec.setThreadState (Z := Unit) t.bv .running σ = ({((), σ6)}, False) := + Notif.setThreadState_eq .running hk + have hsok : SOK (false, true) σ σ6 := Pres.of_eq (pres_setThreadState _ _ _) hsp + have hNSC : NSC Z.N Z.C σ6 := by + intro n hn y p hy hp + by_cases e : n.bv = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.nsc n hn y p hy hp + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hX6.2.2.1, hX6.1.toN, hNSC, hInv⟩ hY6 (hW.mono fun _ h => Or.inl h) + hsok rfl + -- the scheduler facts + have hsched : ∀ u, Fl4Spec.schedulable u σ = true → Fl4Spec.schedulable u σ6 = true := by + intro u hs + obtain ⟨y, scp, z, n, hky, hsc, hrun, hkz, hac, hnr⟩ := schedulable_inv hs + have hzt : scp ≠ t.bv := by rintro rfl; rw [hk] at hkz; cases hkz + have hkz6 : σ6.kheap scp = some (.schedContext z n) := by rw [hkp _ hzt]; exact hkz + have hnr6 : u ∉ σ6.releaseQueue := by rw [e_rel]; exact hnr + by_cases e : u = t.bv + · subst e; rw [hk] at hky; cases hky + exact schedulable_of hk6 hsc rfl hkz6 hac hnr6 + · exact schedulable_of (by rw [hkp _ e]; exact hky) hsc hrun hkz6 hac hnr6 + have hidt : σ.idleThread ≠ t.bv := by + intro e + obtain ⟨y, h1, -, h3, -⟩ := J.act.2 + rw [e, hk] at h1; cases h1; rw [hrs] at h3; cases h3 + have hAI : ActIdle σ6 := by + refine ⟨fun u hu => ?_, ?_⟩ + · have hu0 : σ.schedulerAction = .switchThread u := by + rcases hact6 with e | e + · rw [e] at hu; cases hu + · rw [← e]; exact hu + exact hsched u (J.act.1 u hu0) + · obtain ⟨y, h1, h2, h3, h4⟩ := J.act.2 + exact ⟨y, by rw [e_idle, hkp _ hidt]; exact h1, by rw [e_isc]; exact h2, h3, by rw [e_idle, e_rel]; exact h4⟩ + have hoff : OffOK σ6 := by + intro h0 y n hy ha + rw [e_cons] at h0 ⊢; rw [e_tm]; rw [e_csc] at hy; rw [e_csc, e_isc] at ha + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.off h0 y n hy ha + have hcur6 : CurInvR σ6 := by + intro er + cases (trivial : True) + · have hC : CurInv σ := J.cur (hact6.elim (fun e' => by rw [e'] at er; cases er) (fun e' => by rw [← e']; exact er)) + obtain ⟨xt, hkc, hcs, hnb⟩ := hC.tcb + obtain ⟨y1, n1, hk1'⟩ := hC.sc + have hne : σ.curSc ≠ t.bv := by intro e; rw [e, hk] at hk1'; cases hk1' + refine ⟨?_, ⟨y1, n1, by rw [e_csc, hkp _ hne]; exact hk1'⟩, fun hne' y n hy => ?_, + fun hm => hC.notRel (by rw [e_ct, e_rel] at hm; exact hm)⟩ + · by_cases hct : σ.curThread = t.bv + · rw [hct, hk] at hkc; cases hkc + have hnb6 : NotIpcBlk Fl4Spec.ThreadState.running := by + refine ⟨?_, ?_, ?_⟩ + · intro _ e; cases e + · intro _ _ e; cases e + · intro _ _ _ e; cases e + exact ⟨x6, by rw [e_ct, hct]; exact hk6, by rw [e_csc]; exact hcs, hnb6⟩ + · exact ⟨xt, by rw [e_ct, hkp _ hct]; exact hkc, by rw [e_csc]; exact hcs, hnb⟩ + · rw [e_csc, e_isc] at hne'; rw [e_csc] at hy + rw [hkp _ hne] at hy + exact hC.active hne' y n hy + have hown6 : CurOwnW Z.T Z.C σ6 := by + have hsc6 : ∀ y n, σ6.kheap σ6.curSc = some (.schedContext y n) → σ.kheap σ.curSc = some (.schedContext y n) := by + intro y n hy + rw [e_csc] at hy + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact hy + rcases J.own with ⟨u, hu, xt, hkt, hxs, hown, hnr⟩ | ⟨hcs, hun⟩ + · left + by_cases hut : u.bv = t.bv + · rw [hut, hk] at hkt; cases hkt + exact ⟨u, hu, x6, by rw [hut]; exact hk6, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact ⟨u, hu, xt, by rw [hkp _ hut]; exact hkt, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact Or.inr ⟨by rw [e_csc]; exact hcs, fun y n hy => hun y n (hsc6 y n hy)⟩ + have hcr6 : CurRunM σ6 := by + by_cases hnr : σ6.schedulerAction ≠ .resumeCurThread + · exact Or.inl hnr + have hr' : σ6.schedulerAction = .resumeCurThread := not_not.1 hnr + have ha : σ6.schedulerAction = σ.schedulerAction := hact6.resolve_left (by rw [hr']; intro e; cases e) + have h0 : ¬ σ.schedulerAction ≠ .resumeCurThread := fun e => e (by rw [← ha]; exact hr') + right + rcases J.curRun.resolve_left h0 with h | h + · left + intro y hy + rw [e_ct] at hy + by_cases hct : σ.curThread = t.bv + · rw [hct, hk6] at hy; cases hy; rfl + · rw [hkp _ hct] at hy; exact h y hy + · right; rw [e_ct, e_idle]; exact h + have hlk : LK (fun _ => False) σ σ6 := fun p => Or.inr (by + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show LObj _ _ + refine ⟨?_, ?_, ?_⟩ + · intro y ey; cases ey; exact ⟨_, rfl, Or.inl rfl⟩ + · intro y n ey; cases ey + · intro y ey; cases ey + · rw [hkp p hp]; exact OL.refl _) + have hfk : FK σ σ6 := ⟨e_csc, e_idle, e_isc⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s6 q := fun q hq => + J.deadP_sc_stepG hX' hW hK (fun p x' hp hk' h0 y hy => by + subst hp; rw [hk] at hk'; cases hk'; rw [hrs] at h0; simp [tsCode] at h0) + hlk hfk hAI.1 hY' (hYK6.dk q.bv) (fun h => h) (J.jcov.dead q hq) + exact ⟨Lst', J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).2) (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2) hKd subset_rfl) (J.sc_kern hW hK hfk hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hNSC hInv hcur6 hown6 hoff hAI (BlockedScActive.tkw J.bsa hK fun p y n hp ha => by + by_cases e : p = t.bv + · rw [e, hk6] at hp; cases hp + · rw [hkp _ e] at hp; exact ⟨y, hp, ha, fun _ h => h, id⟩) hcr6 hblk hKd (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).1 J.zD, + J.xtop.slotsSame_of hW (fun b hb => tfoot_oobj hS hb), P1.Foot.dbgKeep J.blk (fun _ h => h) hfoot, hW⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- (mmuAD gen 42b66) **`set_thread_state Running` of a `Restart` thread at `JAtZRB`, with the native footprint** +(`runJDB`; the spec step one TCB's state, `natK_tcbW`). -/ +theorem JAtZRB.runJ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hrs : x.tcbState = .restart) : + sched.set_thread_state st s t sched.TS_RUNNING ⦃ s6 => ∃ (Lst' : U64 → List U64), + JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) ∧ + NatFoot (L := L) Z s σ s6 (stsAct (updK σ t.bv (.tcb { x with tcbState := .running })) t.bv) ⦄ := + WP.spec_mono (J.runJDB ht hk hrs) fun _ ⟨Lst', B', _, _, hW⟩ => + ⟨Lst', B', NatFoot.ofT J.xtop hW (fun _ h => J.tfoot_nh h) + (natK_tcbW (y := { x with tcbState := .running }) hk (by rw [stsAct_kheap]) fun h => by cases h)⟩ + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core Classical Fl4Proofs.P1 Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc + Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- An endpoint cap in a slot names one of the endpoints (`CapTyped`, `hset`). -/ +theorem JAtZRB.slotE (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {sl : Fl4Spec.CslotPtr} + (hA : AbsSlot Z.th sl) {e b : U64} {x1 x2 x3 x4 : Bool} + (hc : capAt (L.mem s) (cslotAddr sl) = ok (.Endpoint e b x1 x2 x3 x4)) : e ∈ Z.E := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact J.hset.1 e ((hJ.typed _ hA.isSlot).1 e b x1 x2 x3 x4 (capV_of_capAt hc)) + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} + +/-- A typed-heap TCB is a spec TCB whose state word is `tsCode` of its spec state. -/ +theorem JAtZRB.specTcb (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : Z.th t = some .tcb) : + ∃ x, σ.kheap t.bv = some (.tcb x) ∧ (L.mem s (P1.stA t)).val = Notif.tsCode x.tcbState := by + obtain ⟨x, hk, -⟩ := J.xtop.rel.2.1.1.tcb t ht + refine ⟨x, hk, ?_⟩ + rw [Sched.stA_tStA] + exact (J.xtop.fin.1.1.2.2.1.at_tcb (J.thT t ht) hk).st + +end + +section +open Aeneas Aeneas.Std Result +open fl4_core +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +/-- **`onCoreEff` / `remCurEff` are `VmOrc` at the state after the invocation** (module docstring). -/ +theorem JAtZRB.vmOrcEff (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) (s0 : S) : + VmOrc st (onCoreEff st s0 s) (remCurEff st s0 s) s σ := by + intro t y hk hr + obtain ⟨u, hu, rfl⟩ := J.obj.tcb t y hk + obtain ⟨x, hk', hw⟩ := J.specTcb (J.thTcb hu) + have e : x = y := by rw [hk] at hk'; simpa using hk'.symm + subst e + have h8 : L.mem s (P1.stA u) = 8#u64 := UScalar.eq_of_val_eq (by rw [hw, hr]; simp [Notif.tsCode]) + have hrv : isRunVMF st s u.bv = true := by + show decide (L.mem s (P1.stA u) = 8#u64) = true + exact decide_eq_true h8 + refine ⟨?_, fun _ => ?_⟩ <;> simp only [onCoreEff, remCurEff, hrv, ↓reduceIte] + +end + + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmJAtZRBRet.lean b/proofs/Fl4Proofs/P3/VmJAtZRBRet.lean new file mode 100644 index 000000000..56dd1a0e6 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmJAtZRBRet.lean @@ -0,0 +1,739 @@ +/- +P3 (line mmuAD, Stage 3 step 3, generated by `handoff/tools/gen_rclo.py`): **`maybe_return_sched_context` from a `JAtZRB` +state** (the intermediate state of the SC-returning blocking receives at a pending switch): the `JAtZB` lemmas it reads, +renamed to `JAtZRB` (proofs verbatim; `sc_frame` with `CurInvR`, the return by `maybeReturn_midR`, its post also says that +returning from the current thread leaves `chooseNewThread`), and `JAtZRB.toB`. No named premise. +-/ +import Fl4Proofs.P3.VmJAtZRB +import Fl4Proofs.P3.XTopJointJAtZRM +import Fl4Proofs.P3.XTopJointRecvSigRet +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem JAtZRB.sc_out_keep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) {q : U64} {z : Nat} + (hq : H q = some (.sc z)) (hqC : q ∉ Z.C) {K : U64} (hK8 : K.val % 8 = 0) (hK : K.val < 128) : + L.mem s' (scW q K) = L.mem s (scW q K) := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hS.lists + have hH := J.kern.heap + have hal := hH.align q _ hq + have hfit := hH.fits q _ hq + have hz := hH.scmin q z hq + simp only [P1.HKind.bits] at hal hfit + have h128 : 2 ^ 7 ≤ 2 ^ z := Nat.pow_le_pow_right (by norm_num) hz + have hq8 : q.val % 8 = 0 := by + have hd : 8 ∣ 2 ^ z := (Nat.pow_dvd_pow 2 (by omega : 3 ≤ z)) + rw [← Nat.mod_mod_of_dvd q.val hd, hal] + have hv : (scW q K).val = q.val + K.val := scW_val (by omega) + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + refine hw.mem_eq (by show (scW q K).val % 8 = 0; rw [hv]; omega) fun hb => ?_ + rcases tfoot_tcb hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hHy : H y = some .tcb := J.zH.1 y hy + have hne : q ≠ y := fun e => by rw [e, hHy] at hq; cases hq + have := hH.disj q _ y _ hq hHy hne + simp only [P1.HKind.bits] at this + omega + · have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have := J.kern.hnd q _ hq Z.c hcC + simp only [P1.HKind.bits] at this + omega + · have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + have hne : q ≠ n := fun e => by rw [e, hHn] at hq; cases hq + have := hH.disj q _ n _ hq hHn hne + simp only [P1.HKind.bits] at this + omega + · have hHs := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have hne : q ≠ sc := fun e => hqC (e ▸ hsc) + have := hH.disj q _ sc _ hq hHs hne + simp only [P1.HKind.bits] at this + omega + +theorem JAtZRB.tcbs_eq {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : Z.T = P1.Heap.tcbs H ∧ Z.R = P1.Heap.replies H := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Set.ext fun t => ⟨fun h => J.zH.1 t h, fun h => J.thT t ((hJ.tcb t).2 h)⟩, + Set.ext fun r => ⟨fun h => J.zH.2.2.1 r h, fun h => J.hset.2.2 r h⟩⟩ + +theorem JAtZRB.deadP_sc_stepQm {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) + {P : Fl4Spec.ObjRef → Prop} (hK : ScTKw P σ σ') {q : U64} + (hP : ∀ x, P q.bv → σ.kheap q.bv = some (.tcb x) → tsCode x.tcbState = 0 → + ∀ y, σ'.kheap q.bv = some (.tcb y) → y.tcbState = x.tcbState) + {W : Fl4Spec.ObjRef → Prop} (hL : LK W σ σ') (hF : FK σ σ') (hAS : ActSched σ') + (hYm : YMemDead (L := L) H Z.C s s' q) + (hW : ¬ W q.bv) (hd : DeadP (L := L) H s q) : DeadP (L := L) H s' q := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hS' := hX'.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + have hI := hX.ipc + have hI' := hX'.ipc + refine ⟨fun hq => ?_, fun hq => ?_, fun hq => ?_, fun hq => ?_, fun z hq => ?_⟩ + · -- a TCB + obtain ⟨d1, d2, d3, d4, d5, d6⟩ := hd.1 hq + have hqT : q ∈ Z.T := by rw [J.tcbs_eq.1]; exact hq + obtain ⟨x, hk, hR⟩ := hI.tcb q hqT + obtain ⟨x', hk', hR'⟩ := hI'.tcb q hqT + obtain ⟨y, hy, hso, hst⟩ := hK.tcb hk + rw [hk'] at hy; cases hy + have hst0 : tsCode x.tcbState = 0 := by + have := hR.st; rw [← stA_tStA, d1] at this; unfold sched.TS_INACTIVE at this; simpa using this.symm + have hstate : x'.tcbState = x.tcbState := by + rcases hst with e | ⟨hp, -, -⟩ + · exact e + · exact hP _ hp hk hst0 _ hk' + have hxs : x.tcbSchedContext = none := by rw [hR.sc, d2, optRef_zero] + have hxs' : x'.tcbSchedContext = none := by + obtain ⟨y2, hy2, e⟩ := hL.tcb hW hk + rw [hk'] at hy2; cases hy2 + rcases e with e | e + · rw [e, hxs] + · exact e + refine ⟨?_, ?_, ?_, ?_, ?_, hYm.1 hq d6⟩ + · have h1 : (L.mem s' (tStA q)).val = (L.mem s (tStA q)).val := by rw [hR'.st, hR.st, hstate] + rw [stA_tStA, UScalar.eq_of_val_eq h1, ← stA_tStA, d1] + · exact optRef_eq_none (by rw [← hR'.sc, hxs']) + · have hb : x'.tcbBoundNotification = x.tcbBoundNotification := by rw [hso] + have h1 := hR.bound + rw [show tBoundA q = P1.boundA q from rfl, d3, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.boundA q = tBoundA q from rfl, ← hR'.bound, hb, h1]) + · intro e + have hact := hS'.act + have ea : L.actionW s' = Sched.nodeW st s' Z.c layout.node.SCHED_ACTION := by + unfold P1.LawfulStore.actionW Sched.nodeW; rw [hc'.cur] + rw [← ea, ← e] at hact + revert hact + cases hsa : σ'.schedulerAction with + | resumeCurThread => + intro h; simp only [ActRel] at h; subst h + rw [J.kern.h0] at hq; cases hq + | chooseNewThread => + intro h; simp only [ActRel] at h; subst h + have := J.kern.heap.align _ _ hq + simp [P1.HKind.bits] at this + | switchThread t => + intro h; simp only [ActRel] at h + obtain ⟨-, -, ht⟩ := h + obtain ⟨x2, scp, -, -, hk2, hsc2, -⟩ := schedulable_inv (hAS t hsa) + rw [← ht, hk'] at hk2; cases hk2 + rw [hxs'] at hsc2; cases hsc2 + · have ei : L.idleW s' = L.idleW s := by + unfold P1.LawfulStore.idleW + apply P1.eq_of_bv + have h1 := hs'.idleTh; have h2 := hs.idleTh + unfold Sched.nodeW at h1 h2 + rw [hc'.cur, hc.cur, ← h1, ← h2, hF.2.1] + rw [ei]; exact d5 + · -- an endpoint + have hqE : q ∈ Z.E := J.hset.1 q hq + obtain ⟨x, hk, hR⟩ := hI.ep q hqE + obtain ⟨x', hk', hR'⟩ := hI'.ep q hqE + rw [hK.ep hk] at hk'; cases hk' + rw [epState_eq, stOf_ep hR hR', ← epState_eq]; exact hd.2.1 hq + · -- a notification + obtain ⟨d1, d2, d3⟩ := hd.2.2.1 hq + have hqN : q ∈ Z.N := J.hset.2.1 q hq + obtain ⟨x, hk, hR⟩ := hI.ntfn q hqN + obtain ⟨x', hk', hR'⟩ := hI'.ntfn q hqN + obtain ⟨y, hy, ho, hb⟩ := hK.ntfn' hk + rw [hk'] at hy; cases hy + have hxs : x.ntfnSc = none := by rw [hR.sc, show nScA q = P1.ntfnScA q from rfl, d3, optRef_zero] + have hxs' : x'.ntfnSc = none := by + obtain ⟨y2, hy2, e⟩ := hL.ntfn hW hk + rw [hk'] at hy2; cases hy2 + rcases e with e | e + · rw [e, hxs] + · exact e + refine ⟨?_, ?_, ?_⟩ + · rw [epState_eq] + have hO' := hR'.obj + rw [ho] at hO' + rw [stOf_ntfn hR.obj hO', ← epState_eq]; exact d1 + · have h1 := hR.bound + rw [show nBoundA q = P1.ntfnBtA q from rfl, d2, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.ntfnBtA q = nBoundA q from rfl, ← hR'.bound, hb, h1]) + · exact optRef_eq_none (by rw [show P1.ntfnScA q = nScA q from rfl, ← hR'.sc, hxs']) + · -- a reply + have hqR : q ∈ Z.R := J.hset.2.2 q hq + obtain ⟨x, hk, hR⟩ := hI.reply q hqR + obtain ⟨x', hk', hR'⟩ := hI'.reply q hqR + obtain ⟨y, hy, e⟩ := hK.reply hk + rw [hk'] at hy; cases hy + have h1 := hR + rw [show Endp.rTcbA q = P1.rTcbA q from rfl, hd.2.2.2.1 hq, optRef_zero] at h1 + exact optRef_eq_none (by rw [show P1.rTcbA q = Endp.rTcbA q from rfl, ← hR', e, h1]) + · -- an SC + obtain ⟨d1, d2, d3, d4⟩ := hd.2.2.2.2 z hq + by_cases hqC : q ∈ Z.C + · obtain ⟨x, n, hk, hR⟩ := hs.scs q hqC + obtain ⟨x', n', hk', hR'⟩ := hs'.scs q hqC + obtain ⟨y, hy, e1, e2⟩ := hL.sc hW hk + rw [hk'] at hy; cases hy + have hxt : x.scTcb = none := by rw [hR.tcb, scW_tcb, d1, optRef_zero] + have hxn : x.scNtfn = none := by rw [hR.ntfn, scW_ntfn, d3, optRef_zero] + refine ⟨?_, ?_, ?_, ?_⟩ + · rw [← scW_tcb]; refine optRef_eq_none ?_; rw [← hR'.tcb] + rcases e1 with e | e + · rw [e, hxt] + · exact e + · rw [(TFoot.keep hS J.rp hw).2.1 q hqC]; exact d2 + · rw [← scW_ntfn]; refine optRef_eq_none ?_; rw [← hR'.ntfn] + rcases e2 with e | e + · rw [e, hxn] + · exact e + · exact hYm.2 z hq hqC d4 + · refine ⟨?_, ?_, ?_, ?_⟩ + · rw [← scW_tcb, J.sc_out_keep hw hq hqC (by decide) (by decide), scW_tcb]; exact d1 + · rw [scReplyA_eq', J.sc_out_keep hw hq hqC (by decide) (by decide), ← scReplyA_eq']; exact d2 + · rw [← scW_ntfn, J.sc_out_keep hw hq hqC (by decide) (by decide), scW_ntfn]; exact d3 + · rw [scYieldFromA_eq, J.sc_out_keep hw hq hqC (by decide) (by decide), ← scYieldFromA_eq]; exact d4 + +theorem JAtZRB.deadY_tcb {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hY' : YieldRel Z.T Z.C (L.mem s') σ') {q : U64} (hYD : YDeadKeep σ σ' q.bv) (hqT : q ∈ Z.T) + (h0 : L.mem s (P1.yieldA q) = 0#u64) : L.mem s' (P1.yieldA q) = 0#u64 := by + obtain ⟨x, hx, e⟩ := J.yld.tcb q hqT + obtain ⟨y, hy, e'⟩ := hY'.tcb q hqT + exact optRef_eq_none (by rw [← e', hYD.1 x hx (by rw [e, h0, optRef_zero]) y hy]) + +theorem JAtZRB.ymd {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hS' : Sched.SchedRel st Z.N Z.T Z.C Z.c s' σ') (hY' : YieldRel Z.T Z.C (L.mem s') σ') {q : U64} + (hYD : YDeadKeep σ σ' q.bv) : YMemDead (L := L) H Z.C s s' q := by + refine ⟨fun hq h0 => J.deadY_tcb hY' hYD (by rw [J.tcbs_eq.1]; exact hq) h0, fun z hq hqC h0 => ?_⟩ + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + obtain ⟨x, n, hk, hR⟩ := hs.scs q hqC + obtain ⟨x', n', hk', hR'⟩ := hs'.scs q hqC + rw [scYieldFromA_eq]; refine optRef_eq_none ?_; rw [← hR'.yieldFrom] + exact hYD.2 x _ hk (by rw [hR.yieldFrom, ← scYieldFromA_eq, h0, optRef_zero]) x' _ hk' + +theorem JAtZRB.deadP_sc_stepQ {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) + {P : Fl4Spec.ObjRef → Prop} (hK : ScTKw P σ σ') {q : U64} + (hP : ∀ x, P q.bv → σ.kheap q.bv = some (.tcb x) → tsCode x.tcbState = 0 → + ∀ y, σ'.kheap q.bv = some (.tcb y) → y.tcbState = x.tcbState) + {W : Fl4Spec.ObjRef → Prop} (hL : LK W σ σ') (hF : FK σ σ') (hAS : ActSched σ') + (hY' : YieldRel Z.T Z.C (L.mem s') σ') (hYD : YDeadKeep σ σ' q.bv) + (hW : ¬ W q.bv) (hd : DeadP (L := L) H s q) : DeadP (L := L) H s' q := + J.deadP_sc_stepQm hX' hw hK hP hL hF hAS (J.ymd hX'.fin.1.1.1 hY' hYD) hW hd + +theorem JAtZRB.deadP_sc_stepG {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) + {P : Fl4Spec.ObjRef → Prop} (hK : ScTKw P σ σ') + (hP : ∀ p x, P p → σ.kheap p = some (.tcb x) → tsCode x.tcbState = 0 → + ∀ y, σ'.kheap p = some (.tcb y) → y.tcbState = x.tcbState) + {W : Fl4Spec.ObjRef → Prop} (hL : LK W σ σ') (hF : FK σ σ') (hAS : ActSched σ') + (hY' : YieldRel Z.T Z.C (L.mem s') σ') {q : U64} (hYD : YDeadKeep σ σ' q.bv) + (hW : ¬ W q.bv) (hd : DeadP (L := L) H s q) : DeadP (L := L) H s' q := + J.deadP_sc_stepQ hX' hw hK (fun x => hP _ x) hL hF hAS hY' hYD hW hd + +theorem JAtZRB.deadP_sc_step {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) + {P : Fl4Spec.ObjRef → Prop} (hK : ScTKw P σ σ') + (hP : ∀ p x, P p → σ.kheap p = some (.tcb x) → Fl4Spec.runnable x.tcbState = true) + {W : Fl4Spec.ObjRef → Prop} (hL : LK W σ σ') (hF : FK σ σ') (hAS : ActSched σ') + (hY' : YieldRel Z.T Z.C (L.mem s') σ') {q : U64} (hYD : YDeadKeep σ σ' q.bv) + (hW : ¬ W q.bv) (hd : DeadP (L := L) H s q) : DeadP (L := L) H s' q := + J.deadP_sc_stepG hX' hw hK (fun p x hp hk h0 => absurd h0 (tsCode_ne_of_runnable (hP p x hp hk))) hL hF hAS hY' + hYD hW hd + +theorem JAtZRB.idleW_keep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} + (hS' : Sched.SchedRel st Z.N Z.T Z.C Z.c s' σ') (hid : σ'.idleThread = σ.idleThread) + (hisc : σ'.idleSc = σ.idleSc) : Sched.IdleKeep (L := L) s s' := by + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Q', R', hc', hs'⟩ := hS'.lists + constructor + · unfold P1.LawfulStore.idleW + apply P1.eq_of_bv + have h1 := hs'.idleTh; have h2 := hs.idleTh + unfold nodeW at h1 h2 + rw [hc'.cur, hc.cur, ← h1, ← h2, hid] + · unfold Sched.idleScW + apply P1.eq_of_bv + have h1 := hs'.idleSc; have h2 := hs.idleSc + unfold nodeW at h1 h2 + rw [hc'.cur, hc.cur, h1, h2, hisc] + +theorem JAtZRB.kw_dbg {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s)) : + L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := + hw.mem_eq J.kern.dwAl fun hk => P1.dbg0_not_schedW J.blk J.kern.cs0 (Or.inl hk) + +theorem JAtZRB.kw_dbgKeep {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s)) : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := + P1.Foot.dbgKeep J.blk (fun _ h => h) hw + +theorem JAtZRB.sc_apart {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') : + Sched.SlotsApart (st := st) Z.th Z.T Z.N Z.C Z.R Z.c s' σ' := + J.apart.sc_frame hw.1 hK.szKeep + +theorem JAtZRB.sc_buf {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') : + BufWordRel Z.T (L.mem s') σ' := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, -⟩ := hS.lists + exact J.buf.frame (fun t ht => hw.mem_eq (tIpcBufA_aligned (hc.tcbs t ht)) fun hb => hb.ne_buf hS ht rfl) + hK.bufKeep + +theorem JAtZRB.liveX (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {Z' : XSets} {s' : S} {a' : A} + {σ' : Fl4Spec.State Unit} (hX : XTopAt (L := L) AR Z' s' a' σ') (hth : Z'.th = Z.th) (hT : Z'.T = Z.T) + (hc : Z'.c = Z.c) (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) + (hK : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (horig : σ'.isOriginalCap = σ.isOriginalCap) + (hidl : L.idleW s' = L.idleW s) (hdead : ∀ p ∈ ZTcb (L := L) Z.th s, Sched.DeadP (L := L) H s' p) : + LiveDN (L := L) Z.th H Cs Z'.G s' σ' := + liveDN_frame J.xtop J.live ⟨J.blk.2.2.1.2.2.tm.dn, J.blk.2.2.1.2.2.tm.dp, J.blk.2.2.1.2.2.tm.dh⟩ J.kern.cs0 + (by obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact Set.ext fun t => ⟨fun h => J.zH.1 t h, fun h => J.thT t ((hJ.tcb t).2 h)⟩) + hX hth hT hc hSS hK horig hidl hdead + +theorem JAtZRB.vsW (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {W : U64 → Prop} (hw : P1.WritesIn st s s' W) + (hWO : ∀ b, W b → Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b) : + Z.vx = ∅ → Vs.VsNamed Z.th (L.mem s') (L.ksb s') := + fun h => J.xtop.vsNamed_of (J.carry.vsNamed h) J.vsc hw hWO + +theorem JAtZRB.zDX (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {D' : Set U64} + (hSS : SlotsSame Z.th (L.mem s) (L.mem s')) (hDD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D') : + ∀ p ∈ ZTcb (L := L) Z.th s', p ∈ D' := fun p hp => hDD p (ZTcb.of_same hSS hp) + +theorem JAtZRB.sc_frame {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') + (hX : XTopAt (L := L) AR Z s' a' σ') + (hcall : Sched.CallRel (L.mem s') Z.R Z.C σ' Lst') (hrf : Sched.RFree (L.mem s') Z.R Z.C σ' Lst') + (hyl : Sched.YieldRel Z.T Z.C (L.mem s') σ') (hmcp : Sched.McpRel Z.T (L.mem s') σ') + (hbuf : BufWordRel Z.T (L.mem s') σ') + (hrp : Sched.RPlaced st Z.N Z.T Z.C Z.R Z.c s' σ') (hap : Sched.SlotsApart (st := st) Z.th Z.T Z.N Z.C Z.R Z.c s' σ') + (hjpl : ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s' σ') (hjc : Sched.JCovD (L := L) Z.th H Z.G D' Cs s') + (hkern : JKern (L := L) arI H Cs D' Z.th s' σ') + (hutc : ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi) + (hnsc : Sched.NSC Z.N Z.C σ') (hinv : Sched.Inv σ') (hcur : CurInvR σ') + (hown : Sched.CurOwnW Z.T Z.C σ') (hoff : Sched.OffOK σ') + (hact : Sched.ActIdle σ') (hbsa : Sched.BlockedScActive σ') (hcr : CurRunM σ') + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac' s') + (hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (hidl : L.idleW s' = L.idleW s) (hDD : ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D') : + JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ' := by + have hks : L.ksb s' = L.ksb s := hw.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl' : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hsl : Sched.ScLinkHyp σ' := scLink_rebuild hX J.tcbs_eq.1 (J.obj.tkw hK) hblk J.hset.2.1 hjpl J.c0 + have hsv : Sched.SchedValid σ' := schedValid_rebuild hX J.tcbs_eq.1 J.tcbs_eq.2 (J.obj.tkw hK) hblk J.hset.1 + J.hset.2.1 hcall hyl J.c0 hinv hsl + refine ⟨hX, hcall, hrf, + J.rrecv.frame (Sched.TFoot.keep J.xtop.fin.1.1.1 J.rp hw).2.2 (Sched.KhRB.of_sctkw hK), hyl, hmcp, hbuf, + by rw [hks]; exact J.ks, J.thT, J.thk, J.ip, hrp, hap, + fun n hn => by rw [hks]; exact J.nk n hn, + fun wp hw c hc => by simp only [P1.LawfulStore.nodeBase, hks]; exact J.nodeB wp hw c hc, hjpl, J.cover.tkw hK, J.obj.tkw hK, J.zH, ?_, J.hset, J.hscC, hjc, hkern, + J.del.of_same hSS, UtInv.of_same hM J.ut hSS, J.bound, fun G hG => ?_, hutc, hnsc, hinv, hcur, hown, hoff, hact, + J.c0, hbsa, hsv, J.isorted.tkw hK, hsl, hcr, hblk, by rw [hks]; exact J.intL, + J.vsc.of_eq fun y hy => hSS.capV hy, + J.bok.keep (IpcFrKeep.of_otk hK.kh), J.fcov.ipcFr (IpcFrKeep.of_otk hK.kh), + J.fcap.keepU (Vs.VsSlotsT.of_eq fun y hy => hSS.capV hy) (IpcFrKeep.of_otk hK.kh).page, + J.liveX hX rfl rfl rfl hSS hKd hK.orig hidl (fun p hp => hjc.dead p (hDD p hp)), J.zDX hSS hDD, + (J.carry.frame subset_rfl (IpcFrKeep.of_otk hK.kh) hK.ksz).withP + (J.carry.devKeep J.xtop.rel.2.1.2.2.1 hks hSS hX.rel.2.1.2.2.1 (idleH_of hX (J.obj.tkw hK) J.zH.1 hjpl hinv hact)) + (J.carry.vsSame hSS hks (ArchFwd.of_ipcFr (IpcFrKeep.of_otk hK.kh))) + (J.carry.tabKeep J.xtop.rel.2.1.2.2.1 hSS hX.rel.2.1.2.2.1 hK.ksz) + (J.vsW hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h)⟩ + · refine SpecFree.frame J.sfree (fun x' hx' p sb fi' d hc => ⟨x', hx', fi', ?_, le_rfl⟩) hK.ksz + rw [← hSS.capV (hsl' x' hx')]; exact hc + · have hG0 : MdbLinks Z.th (L.mem s) G := hG.of_same_symm hSS + have hsl' : ∀ x ∈ G.chains.flatten, IsSlot Z.th x := fun x h => ((hG0.members x).1 h).1 + exact (J.finM G hG0).of_capV fun x hx => hSS.capV (hsl' x hx) + +theorem JAtZRB.dxKeep (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hk : Sched.DXKeep (L := L) H Cs s s') : + ∀ q ∈ D, Sched.DeadX (L := L) H Cs s' q := J.jcov.dxKeep hk + +theorem JAtZRB.dxk (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (hTd : Sched.TdKeep (L := L) H s s') : + ∀ q ∈ D, Sched.DeadX (L := L) H Cs s' q := J.dxKeep (DXKeep.of_dbg hKd hTd) + +theorem JAtZRB.tcbZ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : H t = some .tcb) : t ∈ Z.T := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact J.thT t ((hJ.tcb t).2 ht) + +theorem JAtZRB.tfoot_td {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) : Sched.TdKeep (L := L) H s s' := + TdKeep.of_tfoot J.xtop.fin.1.1.1 (fun _ ht => J.tcbZ ht) hw + +theorem JAtZRB.sc_jcov {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {D' : Set U64} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) + (hdead : ∀ q ∈ D', Sched.DeadP (L := L) H s' q) + (hcov : ∀ q ∈ D, q ∉ D' → + (∀ k, Z.th q = some k → Sched.Covered (L.mem s') Z.G q.val (q.val + max (objSize k) 1)) ∧ + (∀ k, H q = some k → Sched.Covered (L.mem s') Z.G q.val (q.val + 2 ^ k.bits))) + (hidl : Sched.IdleKeep (L := L) s s') + (hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s') (hDD : D' ⊆ D) : + Sched.JCovD (L := L) Z.th H Z.G D' Cs s' := by + have hks : L.ksb s' = L.ksb s := hw.1 + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b h => Sched.tfoot_oobj J.xtop.fin.1.1.1 h + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hk : ∀ y ∈ Z.G.chains.flatten, physF (capV (L.mem s) y) = true → isUt (capV (L.mem s) y) = false → + y ∈ Z.G.chains.flatten ∧ capV (L.mem s') y = capV (L.mem s) y := + fun y hy _ _ => ⟨hy, hSS.capV (hsl y hy)⟩ + have JC := J.jcov + refine ⟨fun q k hq => ?_, fun q k hq => ?_, hdead, + JC.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hSS.capV (hsl x hx)]; exact hc⟩, + JC.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hSS.capV (hsl x hx)]; exact hc⟩, + fun q hq => J.dxk hKd (J.tfoot_td hw) q (hDD hq)⟩ + · rcases JC.th q k hq with hc | hd | hi + · exact Or.inl (Sched.covered_keep hc hk) + · by_cases h : q ∈ D' + · exact Or.inr (Or.inl h) + · exact Or.inl ((hcov q hd h).1 k hq) + · exact Or.inr (Or.inr ((Sched.exAt_of hidl hks).2 hi)) + · rcases JC.h q k hq with hc | hd | hi + · exact Or.inl (Sched.covered_keep hc hk) + · by_cases h : q ∈ D' + · exact Or.inr (Or.inl h) + · exact Or.inl ((hcov q hd h).2 k hq) + · exact Or.inr (Or.inr ((Sched.exAt_of hidl hks).2 hi)) + +theorem JAtZRB.sc_jpl {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') + (hiw : Sched.idleScW (L := L) s' = Sched.idleScW (L := L) s) : + ∃ Tr Cq, Sched.JPlaced (L := L) Z.th H Tr Cq s' σ' := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hwU : P1.WritesIn st s s' (UFoot st Z.T Z.N Z.C Z.R Z.c s σ) := hw.mono fun b hb => Or.inl hb + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := slotsSame_of_ufoot hJ.pl hS J.rp J.apart hwU + have hIA := hJ.irqApart (c := Z.c) J.zH.1 J.zH.2.2.2 J.c0 J.zH.2.2.1 J.kern.ksbFit + exact ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => hK.szKeep q.bv y n hq) hw.1 (irq_of_ufoot hS J.rp hIA hwU) hiw⟩ + +theorem JAtZRB.sc_kern {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + {P : Fl4Spec.ObjRef → Prop} {D' : Set U64} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') (hF : FK σ σ') + (hX' : XTopAt (L := L) AR Z s' a' σ') + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) + (hD : D' ⊆ D) : JKern (L := L) arI H Cs D' Z.th s' σ' := by + have K := J.kern + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Q', R', hc', hs'⟩ := hX'.fin.1.1.1.lists + have ecw : L.curW s' = L.curW s := by + apply P1.eq_of_bv + have h1 := hs'.curThread; have h2 := hs.curThread + unfold P1.LawfulStore.curW + rw [hcpu, hna, hc.cur] + unfold Sched.nodeW at h1 h2 + rw [hna] at h1 + rw [← h1, ← h2, hK.ct] + have ecs : L.cscW s' = L.cscW s := by + apply P1.eq_of_bv + have h1 := hs'.curSc; have h2 := hs.curSc + unfold P1.LawfulStore.cscW P1.LawfulStore.nodeA + rw [hcpu, hna, hc.cur] + unfold Sched.nodeW at h1 h2 + rw [hna] at h1 + rw [← h1, ← h2, hF.1] + have hnb : ∀ c, L.nodeBase s' c = L.nodeBase s c := fun c => by simp only [P1.LawfulStore.nodeBase, hks] + obtain ⟨pt, hpt⟩ := K.gpm + refine ⟨fun sl h => K.orig sl (by rw [← hK.orig]; exact h), by rw [ecw, hK.ct]; exact K.cur, + by rw [ecw]; exact K.curT, by rw [hcpu, hna]; exact K.act, by rw [hna]; exact K.dwAl, + by rw [hna]; exact K.dwObj, by rw [hna, hna, hcpu, ecw]; exact K.dwNe, ?_, + ⟨pt, by rw [hK.arch]; exact hK.archObj hpt⟩, by rw [hK.arch]; exact K.ga, by rw [hK.arch]; exact K.gf, + K.heap, K.h0, by rw [hks]; exact K.hks, by rw [hks]; exact K.hksIn, fun p k hp c hc => by rw [hnb]; exact K.hnd p k hp c hc, + by rw [hks]; exact K.ksbFit, by rw [hks]; exact K.ksbAl, by rw [hks, hcpu]; exact K.nodeFit, + by rw [hcpu]; exact K.cpuCs, by rw [ecw]; exact K.curH, by rw [ecs]; exact K.cscH, + K.csLt, K.cs0, K.csN, K.tdx.frame (by rw [hK.arch])⟩ + rw [hna, hDbg]; exact K.head + +theorem JAtZRB.sc_utc {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} {σ' : Fl4Spec.State Unit} {P : Fl4Spec.ObjRef → Prop} + (hw : P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ)) (hK : Sched.ScTKw P σ σ') + (hDbg : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS)) : + ∀ sl p sb fi dev, AbsSlot Z.th sl → capAt (L.mem s') (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := L) arI Z.th s' σ' Z.G sl p sb fi := by + intro sl p sb fi dev hsl hcap hdv + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hks : L.ksb s' = L.ksb s := hw.1 + have hcpu : L.cpu s' = L.cpu s := hw.2.1 + have hna : ∀ c o, L.nodeAt s' c o = L.nodeAt s c o := P1.nodeAt_of_ksb hks + have hSS : SlotsSame Z.th (L.mem s) (L.mem s') := + J.xtop.slotsSame_of hw fun b h => Sched.tfoot_oobj hS h + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hcv : capV (L.mem s) (cslotAddr sl) = .Untyped p sb fi dev := by + rw [← hSS.capV hsl.isSlot]; exact capV_of_capAt hcap + have hcap0 : capAt (L.mem s) (cslotAddr sl) = ok (.Untyped p sb fi dev) := by rw [capAt_capV, hcv] + have U := J.utc sl p sb fi dev hsl hcap0 hdv + have hxm : cslotAddr sl ∈ Z.G.chains.flatten := + (hM.members _).2 ⟨hsl.isSlot, _, hcap0, fun h => by cases h⟩ + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hcC : Z.c ∈ Cs := hc.cur ▸ J.kern.cpuCs + have hfi := U.fiLe + have B := notDev_back (capV_of_capAt hcap) (capV_of_capAt hcap0) + refine ⟨U.pal, U.pfit, U.fiLe, fun g => U.high (B g), fun g => by rw [hcpu, hna]; exact U.actZ (B g), U.free, ?_, U.sb4, U.nostr, U.nsFree, + fun h0 => U.ns (by rw [hK.cdt] at h0; exact h0), + ?_, fun g => by rw [hna]; exact U.dwReg (B g), + fun g => by rw [hK.arch]; exact U.gReg (B g)⟩ + · intro g w hal h1 h2 + rw [hw.mem_eq hal fun hb => ?_] + · exact U.zero (B g) w hal h1 h2 + rcases tfoot_tcb hS hb with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | ⟨sc, hsc, x, n, hk, g1, g2⟩ + · have hth : Z.th y = some .tcb := (hJ.tcb y).2 (J.zH.1 y hy) + have := U.free y .tcb hth + simp only [objSize] at this; omega + · have := (J.jcov.ks _ hxm p sb fi (ramCap_of_notDev hcv (B g))).2.1 Z.c hcC + omega + · obtain ⟨x, hx, -⟩ := hR.ntfn n hn + have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) n.bv _ hx + simp only [Fl4Spec.objBits, Fl4Spec.ntfnBits, UScalar.bv_toNat] at this + omega + · have := J.sfree _ hxm p sb fi (by rw [hcv, hdv]) sc.bv _ hk + simp only [Fl4Spec.objBits, UScalar.bv_toNat] at this + rw [Nat.add_comm Fl4Spec.minSchedContextBits n] at this + omega + · intro g h0 hf0 + rw [hK.cdt] at h0 + obtain ⟨r1, r3⟩ := U.rst (B g) h0 hf0 + exact ⟨fun sl' h => r1 sl' (by rw [← hK.orig]; exact h), by rw [hna, hDbg]; exact r3⟩ + +theorem JAtZRB.tfoot_nh (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {b : U64} + (h : Sched.TFoot st Z.T Z.N Z.C Z.c s σ b) : b ≠ P1.vHeadA st s := by + intro e + subst e + have hvh : (P1.vHeadA st s).val = (L.ksb s).val + 80 := vHead_val J.kern.ksbFit + rcases Sched.tfoot_tcb J.xtop.fin.1.1.1 h with ⟨y, hy, g1, g2⟩ | ⟨g1, g2⟩ | ⟨n, hn, g1, g2⟩ | + ⟨sc, hsc, x, n, hk, hso⟩ + · have d := J.kern.hks y _ (J.zH.1 y hy) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + omega + · have hnb : L.nodeBase s Z.c = (L.ksb s).val + 13568 + Z.c.val * 4288 := rfl + omega + · have d := J.kern.hks n _ (J.zH.2.2.2 n hn) (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + omega + · obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hH := (hJ.sc sc x n (J.c0 sc hsc) hk).2 + have d := J.kern.hks sc _ hH (fun _ h => by cases h) + simp only [P1.HKind.bits, Nat.reducePow] at d + unfold Sched.ScObj at hso + omega + +theorem JAtZRB.words_of_spec (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (hsa : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t.bv) (hid : t.bv ≠ σ.idleThread) : + L.actionW s ≠ t ∧ L.idleW s ≠ t := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have ea : L.actionW s = Sched.nodeW st s Z.c layout.node.SCHED_ACTION := by + unfold P1.LawfulStore.actionW Sched.nodeW; rw [hc.cur] + have ei : L.idleW s = Sched.nodeW st s Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW Sched.nodeW; rw [hc.cur] + have ta := hc.tcbs t ht + refine ⟨fun e => ?_, fun e => ?_⟩ + · have h := hS.act + rw [← ea, e] at h + cases ha : σ.schedulerAction with + | resumeCurThread => + rw [ha] at h + have h0 : t = 0#u64 := h + exact ta.ne0 (by rw [h0]; rfl) + | chooseNewThread => + rw [ha] at h + have h1 : t = 1#u64 := h + have hv : t.val = 1 := by rw [h1]; rfl + have := ta.align + rw [hv] at this; norm_num at this + | switchThread u => + rw [ha] at h + exact hsa u ha h.2.2.symm + · apply hid; rw [hs.idleTh, ← ei, e] + +theorem JAtZRB.maybeReturnBQ (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n i : U64} {x : Fl4Spec.Tcb} + {xn : Fl4Spec.Notification} (ht : t ∈ Z.T) (hn : n ∈ Z.N) (hiC : i ∈ Z.C) + (hk : σ.kheap t.bv = some (.tcb x)) (hkn : σ.kheap n.bv = some (.notification xn)) + (hsp : x.tcbSchedContext = some i.bv) (hxn : xn.ntfnSc = some i.bv) (hnq : NotQueued σ t.bv) + (hout : (L.mem s (P1.inqA t) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s (P1.inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + L.actionW s ≠ t) + (hsa : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t.bv) (hid : t.bv ≠ σ.idleThread) : + notification.maybe_return_sched_context st s n t ⦃ s' => + P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ) ∧ ∃ σ', + Fl4Spec.maybeReturnSc (Z := Unit) n.bv t.bv σ = ({((), σ')}, False) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' ∧ + (t.bv = σ.curThread → σ'.schedulerAction = .chooseNewThread) ⦄ := by + have hX := J.xtop + have hS := hX.fin.1.1.1 + have hR := hX.fin.1.1.2.2.1 + obtain ⟨y, m, hky, hyt⟩ := J.inv.back t.bv x i.bv hk hsp + obtain ⟨σ2, he, kt, ki, ko, -⟩ := maybeReturn_shape J.inv hkn hk hky hsp hxn hnq + have hm : ((), σ2) ∈ (Fl4Spec.maybeReturnSc (Z := Unit) n.bv t.bv σ).1 := by + rw [he]; exact Set.mem_singleton _ + obtain ⟨-, hidle⟩ := J.words_of_spec ht hsa hid + have htP : t ∈ P1.Heap.tcbs H := by rw [← J.tcbs_eq.1]; exact ht + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + refine WP.spec_mono (WP.spec_and (WP.spec_and (maybe_return_refines s σ hR hS J.inv.actReady t n ht hn) + (maybe_return_sfoot hS ht hiC hk hsp)) + (WP.spec_and (P1.notification.maybe_return_sched_context.inv s n t Q0 R0 h0 hB0 hX0 hS0 hSg0 htP hout hidle) + (P1.notification.maybe_return_sched_context.kw s n t Q0 R0 h0 hB0 hX0 hS0 hSg0 htP hout hidle))) ?_ + rintro s' ⟨⟨⟨-, σ0, hm0, hf, hv, hS'⟩, hw5⟩, hP1, hfoot⟩ + rw [he] at hm0 + have e0 : σ0 = σ2 := congrArg Prod.snd (Set.mem_singleton_iff.1 hm0) + rw [e0] at hS' hv + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := + ⟨hP1.1, hP1.2.1, hP1.2.2.1, hP1.2.2.2.1, hP1.2.2.2.2.1⟩ + have hR' : NtfnStateRel Z.N Z.T (L.mem s') σ2 := hR.frame hS.placed hf hv + have hN' : NSC Z.N Z.C σ2 := fun q hq x' p hx hp => J.nsc q hq x' p (by + by_cases e1 : q.bv = t.bv + · rw [e1, kt] at hx; cases hx + by_cases e3 : q.bv = i.bv + · rw [e3, ki] at hx; cases hx + rw [ko _ e1 e3] at hx; exact hx) hp + have hI' : Inv σ2 := Inv.retQ J.inv hk hkn hnq hm + have hWk : P1.WritesIn st s s' (KFoot st Z.T Z.N Z.C Z.c s σ) := + Sched.WritesIn.kfoot_of_sfoot_scw hS hiC (K := 24#u64) (by simp) (by simp) (by decide) hw5 + have hW : P1.WritesIn st s s' (TFoot st Z.T Z.N Z.C Z.c s σ) := Sched.WritesIn.tfoot_of_kfoot hWk + have hK : ScTKw (fun _ => False) σ σ2 := nkt_maybeReturnSc n.bv t.bv σ ((), σ2) hm + have hX' : XTopAt (L := L) AR Z s' a σ2 := + xtop_sc_step (ct := σ.curThread) hX J.ip J.nk hW (hK.mono fun _ h => h.elim) hS' hR' id + have hall := ScAll.of_k hS J.rp J.yld J.mcp J.call J.rfree + ⟨⟨hS', hR', hN', hI'⟩, hWk, (pres_maybeReturnSc (true, true) _ _).of_eq he⟩ + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := hall + obtain ⟨hcur', hown', hoff', hact', hcr', hF⟩ := maybeReturn_midR (T := Z.T) J.inv hky hyt hk hsp hkn hxn hnq + ⟨i, hiC, rfl⟩ hsa hid J.cur J.own J.off J.act J.curRun he + have hB' : BlockedScActive σ2 := + J.bsa.bk (bk_of_tk_sk hK (skm_maybeReturnSc _ _ σ _ hm)) ((pres_maybeReturnSc (true, true) _ _).of_eq he) + have hL : LK (fun _ => False) σ σ2 := lkm_maybeReturnSc (W := fun _ => False) n.bv t.bv σ _ hm + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => + J.deadP_sc_step hX' hW hK (fun _ _ h => h.elim) hL hF hact'.1 hY' + ((((pres_maybeReturnSc (true, true) _ _).of_eq he).ykeep rfl).dk q.bv) (fun h => h) (J.jcov.dead q hq) + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have B2 : JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s' a σ2 := + J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) + (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) hKd subset_rfl) + (J.sc_kern hW hK hF hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hN' hI' hcur' hown' hoff' hact' hB' hcr' hblk hKd (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 J.zD + have hO (b : U64) (h : Sched.TFoot st Z.T Z.N Z.C Z.c s σ b) : + Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b := Sched.tfoot_oobj hS h + exact ⟨hW, σ2, he, Ac, Lst', B2, NatFoot.ofWS (J.xtop.slotsSame_of hW hO) hW + (fun b hb => Or.inl (hO b hb)) (fun _ hb => tfoot_nv hS hb) + (fun _ hb => J.tfoot_nh hb) (natK_tk hK), (by + obtain ⟨σ3, e3, -, -, -, -, -, -, -, -, -, -, a1, -⟩ := maybeReturn_shape J.inv hkn hk hky hsp hxn hnq + have : σ3 = σ2 := by + have := (Prod.mk.inj (e3.symm.trans he)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 this)) + rw [← this]; exact a1)⟩ + +/-- `JAtZRB` with a pending reschedule (or `CurInv`) is `JAtZB`. -/ +theorem JAtZRB.toB (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (h : σ.schedulerAction = .chooseNewThread ∨ Sched.CurInv σ) : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ where + xtop := J.xtop + call := J.call + rfree := J.rfree + rrecv := J.rrecv + yld := J.yld + mcp := J.mcp + buf := J.buf + ks := J.ks + thT := J.thT + thk := J.thk + ip := J.ip + rp := J.rp + apart := J.apart + nk := J.nk + nodeB := J.nodeB + jpl := J.jpl + cover := J.cover + obj := J.obj + zH := J.zH + sfree := J.sfree + hset := J.hset + hscC := J.hscC + jcov := J.jcov + kern := J.kern + del := J.del + ut := J.ut + bound := J.bound + finM := J.finM + utc := J.utc + nsc := J.nsc + inv := J.inv + cur := h + own := J.own + off := J.off + act := J.act + c0 := J.c0 + bsa := J.bsa + sv := J.sv + isorted := J.isorted + slink := J.slink + curRun := J.curRun + blk := J.blk + intL := J.intL + vsc := J.vsc + bok := J.bok + fcov := J.fcov + fcap := J.fcap + live := J.live + zD := J.zD + carry := J.carry + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmKEntry.lean b/proofs/Fl4Proofs/P3/VmKEntry.lean index 239a3832f..98c921c0b 100644 --- a/proofs/Fl4Proofs/P3/VmKEntry.lean +++ b/proofs/Fl4Proofs/P3/VmKEntry.lean @@ -559,7 +559,7 @@ private theorem finishGuardedVF simp only [he, bind_ok, uncurry_apply_pair, pre] at h simpa only [he, bind_ok, uncurry_apply_pair, Fl4Spec.fl4CallKernelVF] using h -private theorem VmEptState.budgetRestartV +private theorem VmEptState.budgetRestartFramesV {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} @@ -577,7 +577,9 @@ private theorem VmEptState.budgetRestartV let cur ← Fl4Spec.NonDet.gets fun z : Fl4Spec.State Unit => z.curThread let ts ← Fl4Spec.getThreadState cur Fl4Spec.NonDet.when (¬r ∧ Fl4Spec.runnable ts) <| Fl4Spec.setThreadState cur .restart - pure r) σ).1 ∧ VEAny (L := L) arI AR res.2.1 res.2.2 σ' ⦄ := by + pure r) σ).1 ∧ ∃ (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D' res.2.1 res.2.2 σ' ∧ + NatFoot (L := L) Z s σ res.2.1 σ' ⦄ := by obtain ⟨ct, hct, -⟩ := spec_exists (sched.cur_thread.spec' (L := L) s) obtain ⟨hcur, x, hk⟩ := J.1.1.curEq hct have ht := J.1.1.thT ct (J.1.1.xtop.curTcb hcur).1 @@ -595,27 +597,51 @@ private theorem VmEptState.budgetRestartV · rw [if_pos hr] obtain ⟨s1, σ1, hst, -, hs, Ac1, Lst1, D1, B1, F1⟩ := J.1.1.restartRunJ hct hk hr rw [hs, bind_ok, WP.spec_ok] - refine ⟨σ1, ?_, (J.ofNat (V' := Z.vs) B1 F1).any arI⟩ + refine ⟨σ1, ?_, Ac1, Lst1, D1, J.ofNat (V' := Z.vs) B1 F1, F1⟩ unfold Fl4Spec.NonDet.when rw [if_pos (show ¬false = true ∧ Fl4Spec.runnable x.tcbState = true from ⟨by decide, hr⟩), Notif.nd_bind_one hst] exact Set.mem_singleton _ · rw [if_neg hr, WP.spec_ok] - refine ⟨σ, ?_, J.any arI⟩ + refine ⟨σ, ?_, Ac, Lst, D, J, NatFoot.refl Z s σ⟩ unfold Fl4Spec.NonDet.when rw [if_neg (show ¬ (¬false = true ∧ Fl4Spec.runnable x.tcbState = true) from fun h => hr h.2)] rw [Notif.nd_bind_one (rfl : Fl4Spec.NonDet.pure () σ = ({((), σ)}, False))] exact Set.mem_singleton _ · simp only [↓reduceIte, WP.spec_ok] - refine ⟨σ, ?_, J.any arI⟩ + refine ⟨σ, ?_, Ac, Lst, D, J, NatFoot.refl Z s σ⟩ unfold Fl4Spec.NonDet.when rw [if_neg (by simp)] rw [Notif.nd_bind_one (rfl : Fl4Spec.NonDet.pure () σ = ({((), σ)}, False))] exact Set.mem_singleton _ -/-- The VM budget guard, using the native entry's spec invariants -`ValidMachineTime` [J] and `Sched.OffReady` [J]. -/ -theorem VmEptState.budgetV +/-- The merged statement of `budgetRestartV`, from `budgetRestartFramesV`. -/ +private theorem VmEptState.budgetRestartV + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (r : Bool) : + (do + let ct ← sched.cur_thread st s + if r then ok (r, s, a) + else + let b ← sched.is_runnable st s ct + if b then + let s' ← sched.set_thread_state st s ct sched.TS_RESTART + ok (r, s', a) + else ok (r, s, a)) ⦃ res => + ∃ σ', (res.1, σ') ∈ ((do + let cur ← Fl4Spec.NonDet.gets fun z : Fl4Spec.State Unit => z.curThread + let ts ← Fl4Spec.getThreadState cur + Fl4Spec.NonDet.when (¬r ∧ Fl4Spec.runnable ts) <| Fl4Spec.setThreadState cur .restart + pure r) σ).1 ∧ VEAny (L := L) arI AR res.2.1 res.2.2 σ' ⦄ := + WP.spec_mono (J.budgetRestartFramesV r) fun _ ⟨σ', hm, _, _, _, J', _⟩ => ⟨σ', hm, J'.any arI⟩ + +/-- The VM budget guard with the native footprint at its matched outcome, +including the insufficient verdict and the possible restart write. +[J] `ValidMachineTime`, `Sched.OffReady`, `Entered`, and the current thread +outside the ready queues. -/ +theorem VmEptState.budgetFramesV {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} @@ -629,7 +655,10 @@ theorem VmEptState.budgetV (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : syscall.budget_ok st arI s a ⦃ r => - ∃ σ', (r.1, σ') ∈ (specBudget σ).1 ∧ VEAny (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := by + ∃ σ', (r.1, σ') ∈ (specBudget σ).1 ∧ + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.2.1 r.2.2 σ' ∧ + NatFoot (L := L) Z s σ r.2.1 σ' ⦄ := by have hC : CurInv σ := J.1.1.cur.resolve_left (by rw [hni.2]; intro e; cases e) have hB : KBudgetB arI a σ := ⟨hM.nowAgree hMR hVM, hC, hOff, Or.inr hnq⟩ obtain ⟨⟨su, au⟩, huEq, _⟩ := spec_exists (J.1.1.timestampJH hK hAT hnd hB.now) @@ -649,14 +678,34 @@ theorem VmEptState.budgetV ok (run, sr, a') else ok (run, s', a')) ⦃ _ ⦄ rw [hbEq, bind_ok] - refine WP.spec_mono ((J.ofNat (V' := V') J' F').budgetRestartV r) ?_ - rintro res ⟨σ', hr, hVE⟩ + refine WP.spec_mono ((J.ofNat (V' := V') J' F').budgetRestartFramesV r) ?_ + rintro res ⟨σ', hr, Ac2, Lst2, D2, J2, F2⟩ obtain ⟨u, hu, hb⟩ := mem_bind_elim hz unfold specBudget Fl4Spec.checkBudgetRestart - refine ⟨σ', mem_bind_nd hu (mem_bind_nd hb ?_), hVE⟩ + refine ⟨σ', mem_bind_nd hu (mem_bind_nd hb ?_), V', Ac2, Lst2, D2, J2, J.1.1.natTrans F' F2.cast⟩ rw [he] exact hr +/-- The VM budget guard, using the native entry's spec invariants +`ValidMachineTime` [J] and `Sched.OffReady` [J]. -/ +theorem VmEptState.budgetV + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) + (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : + syscall.budget_ok st arI s a ⦃ r => + ∃ σ', (r.1, σ') ∈ (specBudget σ).1 ∧ VEAny (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := + WP.spec_mono (J.budgetFramesV hni hnq hM hMR hVM hOff hK hAT hnd hmrt hmb hmax htu) + fun _ ⟨σ', hm, _, _, _, _, J', _⟩ => ⟨σ', hm, J'.any arI⟩ + section variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} @@ -871,9 +920,10 @@ private theorem JHole.idleBudgetClockFrame · simp only [↓reduceIte, is_cur_domain_expired_eq, bind_ok, WP.spec_ok] exact P1.WritesIn.refl s _ -/-- Carry the VM signal oracles through the timestamp and either budget -path, including a charge in the native idle case. -/ -private theorem VmEptState.entryBudgetOracles +/-- The timestamp and budget check preserve the VM signal oracles and +return the native footprint at the same matched state, including for an +idle-thread charge. -/ +theorem VmEptState.entryBudgetFrames {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} @@ -888,7 +938,9 @@ private theorem VmEptState.entryBudgetOracles (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : (do let (s1, a1) ← sched.update_timestamp st arI s a; sched.check_budget st arI s1 a1) ⦃ r => ∃ z ∈ ((Fl4Spec.updateTimeStamp (Z := Unit) >>= fun _ => Fl4Spec.checkBudget) σ).1, - VEAny (L := L) arI AR r.2.1 r.2.2 z.2 ∧ VmOrc st on rc r.2.1 z.2 ⦄ := by + ∃ (V' : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D' r.2.1 r.2.2 z.2 ∧ + NatFoot (L := L) Z s σ r.2.1 z.2 ∧ VmOrc st on rc r.2.1 z.2 ⦄ := by have B := J.1.1 have hc : CurInv σ := B.cur.resolve_left (by rw [hres]; intro e; cases e) have hb : KBudgetB arI a σ := ⟨hNow, hc, hOff, Or.inr hnq⟩ @@ -932,11 +984,32 @@ private theorem VmEptState.entryBudgetOracles P1.IpcFootN.trans (P1.FootN.ipc fp) f refine WP.spec_mono (WP.spec_and hCharge hConcrete) ?_ rintro r ⟨⟨z, hz, V', Ac', Lst', D', B', F'⟩, f⟩ - refine ⟨z, hz, (J.ofNat B' F').any arI, IpcFootN.vmOrc h0 hO f F'.fk.1.2.2.1 ?_⟩ + refine ⟨z, hz, V', Ac', Lst', D', J.ofNat B' F', F', IpcFootN.vmOrc h0 hO f F'.fk.1.2.2.1 ?_⟩ intro t y hy obtain ⟨u, hu, he⟩ := B'.obj.tcb t y hy exact ⟨u, B'.zH.1 u hu, he.symm⟩ +/-- Carry the VM signal oracles through the timestamp and either budget +path, including a charge in the native idle case. -/ +private theorem VmEptState.entryBudgetOracles + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) + (hNow : NowAgree arI a σ) (hOff : OffReady σ) + (hres : σ.schedulerAction = .resumeCurThread) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hAK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : + (do let (s1, a1) ← sched.update_timestamp st arI s a; sched.check_budget st arI s1 a1) ⦃ r => + ∃ z ∈ ((Fl4Spec.updateTimeStamp (Z := Unit) >>= fun _ => Fl4Spec.checkBudget) σ).1, + VEAny (L := L) arI AR r.2.1 r.2.2 z.2 ∧ VmOrc st on rc r.2.1 z.2 ⦄ := + WP.spec_mono (J.entryBudgetFrames hO hNow hOff hres hnq hAK hAT hnd hmrt hmb hmax htu) + fun _ ⟨z, hz, _, _, _, _, J', _, hO'⟩ => ⟨z, hz, J'.any arI, hO'⟩ + /-- The unstalled guest exit checks the VCPU between the timestamp and the budget charge, then delivers the saved IRQ before finishing. The timestamp uses `ValidMachineTime` [J] and the charge uses `Sched.OffReady` [J], as in @@ -1337,7 +1410,7 @@ private theorem vcpuAt_nonArch {σ : State Unit} {o : ObjRef} {ko : KernelObject /-- The active notification branch of VMEnter, after both saved-exit flags have been cleared. -/ -private theorem VmEptState.enterSignalTail (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) +private theorem VmEptState.enterSignalTailF (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {t n v : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hn : n ∈ Z.N) {x : Tcb} (hkt : σ.kheap t.bv = some (.tcb x)) (hrun : x.tcbState = .running) @@ -1354,7 +1427,7 @@ private theorem VmEptState.enterSignalTail (hAT : P0.ArchTotal arI) (hVA : ZvVir ok (s2, a1) else let s2 ← obj.Tcb.set_reg st t s1 layout.reg.MSG_INFO 0#u64 - ok (s2, a1)) ⦃ r => ∃ σ', + ok (s2, a1)) ⦃ r => ∃ σ' Ac' Lst', ((do completeSignal n.bv t.bv let full ← vmFullReply t.bv @@ -1362,7 +1435,7 @@ private theorem VmEptState.enterSignalTail (hAT : P0.ArchTotal arI) (hVA : ZvVir let b ← asUser t.bv (getRegister badge_register) vmReportF t.bv b else asUser t.bv (setRegister msg_info_register 0)) σ) = ({((), σ')}, False) ∧ - VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := by + VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D r.1 r.2 σ' ⦄ := by refine WP.spec_bind (J.completeEnterSignal hni ht hcur hn hkt hkn hact) ?_ rintro ⟨s1, a1⟩ ⟨σ1, hSig, ⟨Ac1, Lst1, J1⟩, e1⟩ rw [nd_bind_one hSig] @@ -1402,8 +1475,8 @@ private theorem VmEptState.enterSignalTail (hAT : P0.ArchTotal arI) (hVA : ZvVir (Or.inr ⟨hrun, hfX, v.bv, vs, hvo1, hkv1, hp⟩) (L.mem s1 (regA t layout.reg.BADGE)) refine WP.spec_bind (WP.spec_and hRep (J1.toF.reportFoot hAT (J1.1.1.thTcb ht) _)) ?_ rintro s2 ⟨⟨σ2, Lst2, hSpec, J2⟩, _, hFoot⟩ - refine (WP.spec_ok _).2 ⟨σ2, ?_, (J1.ofF J2 - (fun v hv _ => hFoot _ (Or.inr (Or.inr ⟨v, hv, rfl⟩)))).any arI⟩ + refine (WP.spec_ok _).2 ⟨σ2, Ac1, Lst2, ?_, J1.ofF J2 + (fun v hv _ => hFoot _ (Or.inr (Or.inr ⟨v, hv, rfl⟩)))⟩ rw [nd_bind_one (Endp.asUser_getRegister_eq hk1 badge_register), ← heB] exact hSpec · have hfX : Fl4Proofs.P3.repFull x1 ≠ true := fun h => hFull (eF.trans h) @@ -1418,29 +1491,60 @@ private theorem VmEptState.enterSignalTail (hAT : P0.ArchTotal arI) (hVA : ZvVir have ne : u.bv ≠ t.bv := fun e => by rw [e, hk1] at ho; cases ho simp [σ2, updK, ne, ho] at hn' simp [hn'] - exact (WP.spec_ok _).2 ⟨σ2, Notif.asUser_setRegister_eq _ _ hk1, JV.any arI⟩ + exact (WP.spec_ok _).2 ⟨σ2, Ac1, Lst1, Notif.asUser_setRegister_eq _ _ hk1, JV⟩ -private theorem VmEptState.enterTailJ (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) +/-- The merged statement of `enterSignalTail`, from `enterSignalTailF`. -/ +private theorem VmEptState.enterSignalTail (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {t n v : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hn : n ∈ Z.N) + {x : Tcb} (hkt : σ.kheap t.bv = some (.tcb x)) (hrun : x.tcbState = .running) + {y : Notification} (hkn : σ.kheap n.bv = some (.notification y)) + {badge : Badge} (hact : y.ntfnObj = .activeNtfn badge) + {vs : VcpuState} (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) + (hvo : tcbVcpuOf σ t.bv = some v.bv) (hp : vs.exit.pending = false) : + (do + let (s1, a1) ← notification.complete_signal st arI s a n t + let full ← vcpu.full_reply st s1 t + if full then + let b ← obj.Tcb.reg st t s1 layout.reg.BADGE + let s2 ← vcpu.report st arI s1 a1 t b + ok (s2, a1) + else + let s2 ← obj.Tcb.set_reg st t s1 layout.reg.MSG_INFO 0#u64 + ok (s2, a1)) ⦃ r => ∃ σ', + ((do + completeSignal n.bv t.bv + let full ← vmFullReply t.bv + if full then do + let b ← asUser t.bv (getRegister badge_register) + vmReportF t.bv b + else asUser t.bv (setRegister msg_info_register 0)) σ) = ({((), σ')}, False) ∧ + VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := + WP.spec_mono (J.enterSignalTailF hAT hVA hni ht hcur hn hkt hrun hkn hact hkv hvo hp) + fun _ ⟨σ', _, _, h, J'⟩ => ⟨σ', h, J'.any arI⟩ + +private theorem VmEptState.enterTailF (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {t v : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) {x : Tcb} (hkt : σ.kheap t.bv = some (.tcb x)) (hrun : x.tcbState = .running) {vs : VcpuState} (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) (hvo : tcbVcpuOf σ t.bv = some v.bv) (hp : vs.exit.pending = false) (hBound : L.mem s (tVcpuA t) ≠ 0#u64) : - vcpuEnterTail st arI s a t ⦃ r => ∃ σ', - enterSpecTail t.bv σ = ({((), σ')}, False) ∧ VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := by + vcpuEnterTail st arI s a t ⦃ r => ∃ σ' Ac' Lst', + enterSpecTail t.bv σ = ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D r.1 r.2 σ' ⦄ := by have B := J.1.1 have hI := B.xtop.irelK B.ks B.buf have hb := (hI.1.toE.at_tcb ht hkt).bound have hRun := J.enterRunning ht hkt hrun hBound have noAct : (do let s1 ← sched.set_thread_state st s t sched.TS_RUNNING_VM - ok (s1, a)) ⦃ r => ∃ σ', + ok (s1, a)) ⦃ r => ∃ σ' Lst', setThreadState (Z := Unit) t.bv .runningVM σ = ({((), σ')}, False) ∧ - VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := by + VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst' D r.1 r.2 σ' ⦄ := by refine WP.spec_bind hRun ?_ rintro s1 ⟨σ', Lst1, he, J1⟩ - exact (WP.spec_ok _).2 ⟨σ', he, J1.any arI⟩ + exact (WP.spec_ok _).2 ⟨σ', Lst1, he, J1⟩ unfold vcpuEnterTail refine WP.spec_bind (P1.obj.Tcb.bound_ntfn.val s t) fun n en => ?_ have en' : n = L.mem s (tBoundA t) := en @@ -1448,8 +1552,8 @@ private theorem VmEptState.enterTailJ (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgre · rw [if_neg (by simp [hn0])] have hN : x.tcbBoundNotification = none := by rw [hb, ← en', hn0] <;> rfl refine WP.spec_mono noAct ?_ - rintro r ⟨σ', hR, hVE⟩ - refine ⟨σ', ?_, hVE⟩ + rintro r ⟨σ', Lst', hR, J'⟩ + refine ⟨σ', Ac, Lst', ?_, J'⟩ unfold enterSpecTail rw [nd_bind_one (getTcbObjRef_eq _ hkt), hN] dsimp only @@ -1484,27 +1588,41 @@ private theorem VmEptState.enterTailJ (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgre cases hact : y.ntfnObj with | activeNtfn badge => rw [if_pos (ens.trans (stOf_active hNt.obj hact)), hTail, hact] - exact J.enterSignalTail hAT hVA hni ht hcur hnN hkt hrun hkn hact hkv hvo hp + exact J.enterSignalTailF hAT hVA hni ht hcur hnN hkt hrun hkn hact hkv hvo hp | idleNtfn => rw [if_neg (by rw [ens, stOf_idle hNt.obj hact]; unfold obj.NTFN_IDLE obj.NTFN_ACTIVE; decide), hTail, hact] - exact noAct + exact WP.spec_mono noAct fun _ ⟨σ', Lst', he, J'⟩ => ⟨σ', Ac, Lst', he, J'⟩ | waitingNtfn q => rw [if_neg (by rw [ens, stOf_waiting hNt.obj hact]; unfold obj.NTFN_WAITING obj.NTFN_ACTIVE; decide), hTail, hact] - exact noAct + exact WP.spec_mono noAct fun _ ⟨σ', Lst', he, J'⟩ => ⟨σ', Ac, Lst', he, J'⟩ + +/-- The merged statement of `enterTailJ`, from `enterTailF`. -/ +private theorem VmEptState.enterTailJ (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {t v : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) + {x : Tcb} (hkt : σ.kheap t.bv = some (.tcb x)) (hrun : x.tcbState = .running) + {vs : VcpuState} (hkv : σ.kheap v.bv = some (.archObj (.vcpu vs))) + (hvo : tcbVcpuOf σ t.bv = some v.bv) (hp : vs.exit.pending = false) + (hBound : L.mem s (tVcpuA t) ≠ 0#u64) : + vcpuEnterTail st arI s a t ⦃ r => ∃ σ', + enterSpecTail t.bv σ = ({((), σ')}, False) ∧ VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := + WP.spec_mono (J.enterTailF hAT hVA hni ht hcur hkt hrun hkv hvo hp hBound) + fun _ ⟨σ', _, _, h, J'⟩ => ⟨σ', h, J'.any arI⟩ /-- An accepted VMEnter against the extended spec, including either -the pending notification or the transition to RunningVM. -/ -private theorem VmEptState.vcpuEnterJ +the pending notification or the transition to RunningVM, retaining the +matched state with the same scheduler TCB set. -/ +theorem VmEptState.vcpuEnterStableJ (hK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) (hCap : ∀ a', VirtSanEq.CapAgree arI a') (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hni : Entered σ) {t : U64} (hcur : σ.curThread = t.bv) {x : Tcb} (hkt : σ.kheap t.bv = some (.tcb x)) (hrun : x.tcbState = .running) (hSome : (tcbVcpuOf σ t.bv).isSome) : - vcpu.enter st arI s a t ⦃ r => ∃ σ', vmEnterF t.bv σ = ({((), σ')}, False) ∧ - VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := by + vcpu.enter st arI s a t ⦃ r => ∃ σ' Ac' Lst', vmEnterF t.bv σ = ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D r.1 r.2 σ' ⦄ := by have B := J.1.1 have ht : t ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm have htyped := B.thTcb ht @@ -1549,8 +1667,8 @@ private theorem VmEptState.vcpuEnterJ let s0 ← Fl4Proofs.P2.enterBlk st arI s a1 v ip ctl intr i t let s2 ← vcpu.set st s0 v vcpu.VALID 0#u64 vcpu.set st s2 v vcpu.PENDING 0#u64) - vcpuEnterTail st arI s1 a1 t) ⦃ r => ∃ σ', vmEnterF t.bv σ = ({((), σ')}, False) ∧ - VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := by + vcpuEnterTail st arI s1 a1 t) ⦃ r => ∃ σ' Ac' Lst', vmEnterF t.bv σ = ({((), σ')}, False) ∧ + VmEptState (L := L) arI V X XL AR Z H Cs Ac' Lst' D r.1 r.2 σ' ⦄ := by refine WP.spec_bind hRes ?_ intro s3 J3 have hKeep : ∀ u ∈ V, ∀ old new, σ.kheap u.bv = some (.archObj (.vcpu old)) → @@ -1585,10 +1703,23 @@ private theorem VmEptState.vcpuEnterJ unfold vf VcpuState.enterF VcpuState.enter rfl rw [hF] - exact JR.enterTailJ hAT hVA hni ht hcur hkt' hrun hkv' hvo' hPend eBound + exact JR.enterTailF hAT hVA hni ht hcur hkt' hrun hkv' hvo' hPend eBound simpa only [Fl4Proofs.P2.enterFin, vcpuEnterTail, Std.bind_assoc] using hTail -private theorem vmEnterEventGuard {on rc : ObjRef → Bool} {σ : State Unit} {n : Nat} +/-- VMEnter's deterministic spec outcome retains a matching state. -/ +theorem VmEptState.vcpuEnterJ + (hK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) {t : U64} (hcur : σ.curThread = t.bv) + {x : Tcb} (hkt : σ.kheap t.bv = some (.tcb x)) (hrun : x.tcbState = .running) + (hSome : (tcbVcpuOf σ t.bv).isSome) : + vcpu.enter st arI s a t ⦃ r => ∃ σ', vmEnterF t.bv σ = ({((), σ')}, False) ∧ + VEAny (L := L) arI AR r.1 r.2 σ' ⦄ := + WP.spec_mono (J.vcpuEnterStableJ hK hAT hVA hCap hni hcur hkt hrun hSome) + fun _ ⟨σ', _, _, he, J'⟩ => ⟨σ', he, J'.any arI⟩ + +theorem vmEnterEventGuard {on rc : ObjRef → Bool} {σ : State Unit} {n : Nat} (h : isVmEnterCall n σ = true) : (fl4HandleEventVF (Z := Unit) on rc true (.unknownSyscall n)).run σ = (withoutPreemption (do @@ -1751,7 +1882,7 @@ private theorem VmEptState.unknownFaultAfterFip (B := brK (st := st) (arI := arI) num) (G := brKS num.val) hEv hBud (fun _ _ _ _ hb hv => unknownFaultArmVF hAT hnd (s := s) (a := a) hni hnq hvfh num hb hv) hFin -private theorem VmEptState.faultIPEntry +theorem VmEptState.faultIPEntry {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} diff --git a/proofs/Fl4Proofs/P3/VmKEntryX.lean b/proofs/Fl4Proofs/P3/VmKEntryX.lean new file mode 100644 index 000000000..16b7c2d2b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmKEntryX.lean @@ -0,0 +1,111 @@ +import Fl4Proofs.P3.VmKEntry +import Fl4Proofs.P3.VmInvAfterX + +/-! +`…X` siblings of `VmKEntry`'s dispatcher-chain statements: the same results concluding `VEAnyX` (`VEAny` plus the ghost +fact `VxE`), asking `[VxE Z]` of the entry state. The merged statements are unchanged. Generated by +`handoff/tools/gen_vxsib.py`. The siblings of the merged theorems below the family are in `VmVxLow`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section Spec + +open Fl4Spec + +end Spec + +section + +open Fl4Proofs.P1 Sched Ipc Notif Classical + +open Fl4Spec in + +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {MR : A → Fl4Spec.MachineState → Prop} + +variable {Z : XSets} {H : Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D V X : Set U64} + +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +variable (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +/-- `…X` sibling of `DispPostVE` (`VEAnyX`; module docstring). -/ +def DispPostVEX (on rc : Fl4Spec.ObjRef → Bool) (c : Fl4Spec.Syscall) (σ : Fl4Spec.State Unit) (r : S × A) : Prop := + ∃ y ∈ ((Fl4Spec.fl4HandleSyscallVF (Z := Unit) on rc true c).run σ).1, + (y.1 = .ok () ∧ VEAnyX (L := L) arI AR r.1 r.2 y.2) ∨ + (y.1 = .error () ∧ ∃ z ∈ (Fl4Spec.preemptionPathVF (Z := Unit) on rc y.2).1, VEAnyX (L := L) arI AR r.1 r.2 z.2) + +variable {arI AR MR} + +/-- `…X` sibling of `VmEptState.budgetV` (`VEAnyX`; module docstring). -/ +theorem VmEptState.budgetVX + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hM : MachPlat arI MR) (hMR : MR a σ.machineState) + (hVM : ValidMachineTime σ) (hOff : OffReady σ) + (hK : ARKeep arI AR) (hAT : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) + (hmrt : ∀ a, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) : + syscall.budget_ok st arI s a ⦃ r => + ∃ σ', (r.1, σ') ∈ (specBudget σ).1 ∧ VEAnyX (L := L) arI AR r.2.1 r.2.2 σ' ⦄ := + WP.spec_mono (J.budgetFramesV hni hnq hM hMR hVM hOff hK hAT hnd hmrt hmb hmax htu) + fun _ ⟨σ', hm, _, _, _, _, J', _⟩ => ⟨σ', hm, J'.anyX arI⟩ + +section + +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +end + +section + +open Fl4Spec + +end + +section + +variable {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +section + +open Fl4Spec + +end + +end + +section + +open Fl4Spec + +end + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmMidB.lean b/proofs/Fl4Proofs/P3/VmMidB.lean new file mode 100644 index 000000000..69c453ab5 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidB.lean @@ -0,0 +1,363 @@ +/- +P3 (line mmuAD, Stage 3, `VMidF` producer, plan part (ii); spec only): **the first-phase non-blocking non-calling +extended invocation keeps `CQ ∧ BI`** (`midRunB_shellVF`: XTopJointChooseB's `midRunB_shell` for +`fl4HandleInvocationVF`), hence **`jMidChooseFactVF`** (`CtOK`: `CurInv`, or the current thread holds no SC). + +* The virtualization steps write no SC field, no SC and no release queue (`SV`): the VMEnter report + (`sck_vmReportF`), every VCPU and EPT invocation, the frame batch (P3/VmMidFb). +* `pc_sendSignalVF`: the VM branch donates to a `runningVM` thread, hence not `ct` (`NotRvm`, P3/VmMidRunQ). +* The Untyped, IRQ handler set / clear and ASID control performs rest on the native premises [W] `JMidPerfHypQ` / + `JMidPerfHypB` (XTopJointChooseB; the VCPU initialisation writes no SC field): no new premise. + +Named premises: [W] `JMidPerfHypQ`, [W] `JMidPerfHypB`. New file. +-/ +import Fl4Proofs.P3.VmMidRunQ +import Fl4Proofs.P3.XTopJointChooseB +open Classical + +namespace Fl4Proofs.P3.MidRun +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Vs + +noncomputable section + +set_option maxRecDepth 20000 + +/-! ## The SC view across the virtualization steps -/ + +theorem sck_of_keepSt {α : Type} {m : NonDet (State Unit) α} (h : KeepSt m) : SV m := fk_of_keepSt h + +theorem sck_setArchObjV (p : ObjRef) (a : ArchKernelObj) : SV (setObject (Z := Unit) p (.archObj a)) := + sck_setObject_ns p _ (fun _ h => by cases h) (fun _ _ h => by cases h) (fun _ _ h => by cases h) + +theorem sck_vcpuUpdateV (v : ObjRef) (g : VcpuState → VcpuState) : SV (vcpuUpdate (Z := Unit) v g) := + fk_vcpuUpdate (fun p a => sck_setArchObjV p a) v g + +theorem sck_getTcbVcpu (t : ObjRef) : SV (getTcbVcpu (Z := Unit) t) := by + unfold getTcbVcpu; exact dk_gets _ + +theorem sck_vmFullReply (t : ObjRef) : SV (vmFullReply (Z := Unit) t) := by + unfold vmFullReply; exact dk_bind (sck_asUser _ _) fun _ => dk_pure _ + +theorem sck_getThreadStateV (t : ObjRef) : SV (getThreadState (Z := Unit) t) := by + unfold getThreadState threadGet; dk_auto0 + +theorem sck_vmReportF (t : ObjRef) (b : Word) : SV (vmReportF (Z := Unit) t b) := by + unfold vmReportF vmFaultIpNext + repeat' (first + | with_reducible exact sck_getTcbVcpu _ + | with_reducible exact sck_of_keepSt (keepSt_getVcpu _) + | with_reducible exact sck_vmFullReply _ + | with_reducible exact sck_lookupIpcBuffer _ _ + | with_reducible exact sck_setMrs' _ _ _ + | with_reducible exact sck_vcpuUpdateV _ _ + | with_reducible exact sck_asUser _ _ + | with_reducible exact sck_setThreadState _ _ + | dk_step0) + +theorem sck_vcpuDetachF (t : ObjRef) : SV (vcpuDetachF (Z := Unit) t) := by + unfold vcpuDetachF + refine dk_bind (sck_getTcbVcpu _) fun vo => ?_ + cases vo with + | none => exact dk_pure _ + | some v => + exact dk_bind (sck_getThreadStateV _) fun _ => dk_bind (dk_when _ (sck_vmReportF _ _)) fun _ => sck_vcpuUpdateV _ _ + +theorem sck_associateVcpuTcbF (v t : ObjRef) : SV (associateVcpuTcbF (Z := Unit) v t) := by + unfold associateVcpuTcbF + refine dk_bind (sck_vcpuDetachF _) fun _ => ?_ + refine dk_bind (sck_of_keepSt (keepSt_getVcpu v)) fun st => ?_ + cases st.tcb with + | none => + first + | (with_reducible exact sck_vcpuUpdateV _ _) + | (with_reducible exact dk_bind (dk_pure _) fun _ => sck_vcpuUpdateV _ _) + | some old => with_reducible exact dk_bind (sck_vcpuDetachF _) fun _ => sck_vcpuUpdateV _ _ + +theorem sck_performVcpuInvocationF (i : VcpuInvocation) : SV (performVcpuInvocationF (Z := Unit) i) := by + cases i with + | setTcb v t => + unfold performVcpuInvocationF + exact dk_bind (sck_associateVcpuTcbF _ _) fun _ => dk_pure _ + | _ => exact fk_performVcpuInvocationF (fun p a => sck_setArchObjV p a) (fun _ _ => sck_setCap _ _) _ trivial + +theorem sck_storeAsidPoolEntryV (p : ObjRef) (a : Asid) (b : Option ObjRef) : + SV (storeAsidPoolEntry (Z := Unit) p a b) := by + unfold storeAsidPoolEntry setAsidPool getAsidPool + repeat' (first | exact sck_setArchObjV _ _ | dk_step0) + +theorem sck_performEptInvocation (i : EptInvocation) : SV (performEptInvocation (Z := Unit) i) := + fk_performEptInvocation (fun p a => sck_setArchObjV p a) (fun _ _ => sck_setCap _ _) (fun _ => sck_getCap _) + (fun _ _ _ => sck_storeAsidPoolEntryV _ _ _) i + +set_option maxHeartbeats 4000000 in +theorem sck_performFrameBatchVF (bi : Fl4FrameBatchInvocation) : SV (performFrameBatchVF bi) := by + intro σ' x hx + have k : ∀ {α : Type} {m : NonDet (State Unit) α}, SV m → + Fl4Proofs.P3.SInv.IK (fun s => sV s = sV σ') m := + fun h s hs y hy => (h s y hy).trans hs + refine Fl4Proofs.P3.SInvU.ik_performFrameBatchVF_w (P := fun s => sV s = sV σ') bi + (fun _ _ _ _ => k (sck_capInsert _ _ _)) (fun _ => k (sck_performPageInvocation _)) + (fun _ _ => k ?_) (fun _ _ _ _ _ => k (fk_guestUnmap (fun p a => sck_setArchObjV p a) _ _ _ _ _)) + (fun _ => k ?_) (fun _ _ _ _ => k ?_) (fun _ _ _ => k (sck_setCap _ _)) (k (sck_flushAll _ _)) σ' rfl x hx + · simp only [archFinaliseCap]; sck_auto2 + · unfold emptySlot; sck_auto2 + · unfold unmapPageUnflushed; sck_auto2 + +theorem scke_midEV (t : ObjRef) (info : MessageInfo) (cptr : CapRef) (a) : SVE (midEV t info cptr a) := by + obtain ⟨sl, cap, ex, buf⟩ := a + unfold midEV + exact dke_bind (dke_liftE (sck_getMrs _ _ _)) fun _ => + fke_decodeFirstV _ _ _ _ _ _ _ (scke_decodeFirst _ _ _ _ _ _ _) + +/-! ## `send_signal` -/ + +section +variable {ct : ObjRef} + +set_option maxHeartbeats 4000000 in +theorem pc_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (b : Badge) : + ∀ σ x, CQ ct σ → BI ct σ → NQ ct σ → NotRvm ct σ → + x ∈ (sendSignalVF (Z := Unit) onCore remoteCur n b σ).1 → CQ ct x.2 ∧ BI ct x.2 := by + intro σ x hs hb hq hnr hx + unfold sendSignalVF at hx + obtain ⟨⟨nt, s1⟩, h1, hx⟩ := hx + obtain ⟨e1, hk⟩ := getSimpleKo_mem h1 + simp only at e1 hk + subst e1 + rcases hno : nt.ntfnObj with _ | q | b' <;> rcases hbt : nt.ntfnBoundTcb with _ | tcb <;> + simp only [hno, hbt] at hx + case idleNtfn.some => + obtain ⟨⟨st, s2⟩, h2, hx⟩ := hx + unfold getThreadState threadGet at h2 + obtain ⟨⟨xt, s0⟩, h20, h21⟩ := h2 + obtain ⟨e0, e0'⟩ := mem_getsThe' h20 + simp only at e0 e0' + subst e0' + have h21 := mem_pure' h21 + simp only [Prod.mk.injEq] at h21 + obtain ⟨rfl, rfl⟩ := h21 + obtain ⟨⟨comp, s3⟩, h3, hx⟩ := hx + have ev := fv_vmSignalCompletes (ct := ct) onCore remoteCur tcb _ _ h3 + simp only [fV, Prod.mk.injEq] at ev + have hs3 : CQ ct s3 := ⟨CtRs.of_ctV ev.1 hs.1, qk_of_qv (qv_vmSignalCompletes _ _ _) _ _ h3 hs.2⟩ + have hq3 : NQ ct s3 := NQ.of_nV ev.2 hq + have hb3 : BI ct s3 := by + refine BI.of_sV ?_ hb + have h : SV (vmSignalCompletes (Z := Unit) onCore remoteCur tcb) := by + unfold vmSignalCompletes + refine dk_bind (sck_getTcbVcpu _) fun vo => ?_ + cases vo with + | none => exact dk_pure _ + | some v => + exact dk_bind (sck_of_keepSt (keepSt_getVcpu v)) fun _ => dk_bind (sck_vmFullReply _) fun _ => dk_pure _ + exact h _ _ h3 + dsimp only at hx + split at hx + · next hc => + have hd : tcb ≠ ct := fun e => by + subst e + exact hnr _ (getTcb_some e0) hc.1 + refine (?_ : PC ct _) s3 x hs3 hb3 trivial hx + refine pcp_bind (pcp_of (pq_vmReportF _ _) (sck_vmReportF _ _)) fun _ => ?_ + refine pcp_bind (pc_maybeDonateSc _ _ hd) fun _ => ?_ + refine pcp_bind (by pc_step) fun _ => ?_ + refine pcp_bind (pcp_of (pq_when _ (pq_of (kf_possibleSwitchTo' _) (PPNQ.qk_possibleSwitchTo hd))) + (dk_when _ (sck_possibleSwitchTo _))) fun _ => ?_ + refine pcp_bind (pcp_of (pq_of (kf_getTcbObjRef' _ _) (PPNQ.qk_getTcbObjRef' _ _)) + (by unfold getTcbObjRef threadGet; dk_auto0)) fun scOpt => ?_ + cases scOpt with + | none => exact pcp_of (pq_of (dk_pure' _) (PPNQ.qk_pure' _)) (dk_pure' _) + | some scp => + show PC ct _ + unfold NonDet.maybeM + dsimp only + refine pcp_bind (pcp_of (pq_of (kf_getScActive' _) (by unfold getScActive; exact PPNQ.qk_getsThe _)) + (by unfold getScActive; exact dk_getsThe _)) fun _ => ?_ + refine pcp_of (pq_when _ (by pq_step)) (dk_when _ (by sck_auto2)) + · exact pc_sendSignal n b s3 x hs3 hb3 hq3 hx + all_goals exact pc_sendSignal n b _ x hs hb hq hx + +/-! ## The perform -/ + +/-- **The perform of a first-phase extended invocation keeps `CQ ∧ BI`**, `ct` not `runningVM`; the `PerfW` ones by +`hW`. -/ +theorem perf_firstBVF (onCore remoteCur : ObjRef → Bool) (don : Bool) (inv : Fl4InvocationV) (hf : FirstInvV inv) + {σ : State Unit} (hs : CQ ct σ) (hq : NQ ct σ) (hb : BI ct σ) (hnr : NotRvm ct σ) + (hW : ∀ b, inv = .base b → PerfW b → + ∀ y ∈ ((fl4PerformInvocation (Z := Unit) false false don b).run σ).1, CQ ct y.2 ∧ BI ct y.2) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur false false don inv).run σ).1, + CQ ct y.2 ∧ BI ct y.2 := by + cases inv with + | vcpu i => + simp only [fl4PerformInvocationVF] + exact post_liftE (Q := fun s => CQ ct s ∧ BI ct s) + (pc_post (pcp_of (pq_performVcpuInvocationF_all i) (sck_performVcpuInvocationF i)) hs hb) + | ept i => + simp only [fl4PerformInvocationVF] + exact post_liftE (Q := fun s => CQ ct s ∧ BI ct s) + (pc_post (pcp_of (pq_bind (pq_performEptInvocation i) fun _ => pq_pure _) + (dk_bind (sck_performEptInvocation i) fun _ => dk_pure _)) hs hb) + | setEptRoot _ => exact absurd hf id + | base b => + have nat : ∀ y ∈ ((fl4PerformInvocation (Z := Unit) false false don b).run σ).1, CQ ct y.2 ∧ BI ct y.2 := by + by_cases h : PerfW b + · exact hW b rfl h + · exact perf_firstB don b hf h hs hq hb + cases b + case invokeUntyped i => + intro y hy + simp only [fl4PerformInvocationVF] at hy + unfold fl4InvokeUntypedV at hy + have nat' : ∀ z ∈ ((fl4InvokeUntyped (Z := Unit) i).run σ).1, CQ ct z.2 ∧ BI ct z.2 := by + intro z hz + obtain ⟨r, s⟩ := z + cases r with + | error e => + refine nat (.error e, s) ?_ + simp only [fl4PerformInvocation] + rw [ExceptT.run_bind] + exact ⟨(.error e, s), hz, Set.mem_singleton _⟩ + | ok u => + refine nat (.ok [], s) ?_ + simp only [fl4PerformInvocation] + rw [ExceptT.run_bind] + exact ⟨(.ok u, s), hz, Set.mem_singleton _⟩ + have hV : ∀ z ∈ ((do + fl4InvokeUntyped (Z := Unit) i + match i with + | .retype _ _ _ retypeBase newType objSz slots _ => + NonDetE.liftE (NonDet.when (newType = ApiObjectType.archObject AObjectType.vcpuObj) + (NonDet.mapMx (fun v => setVcpu v (initVcpu virtKind)) + (retypeAddrs fl4ObjBitsApi retypeBase slots.length objSz newType))) + : PMonad Unit Unit).run σ).1, CQ ct z.2 ∧ BI ct z.2 := by + refine post_bindE (R := fun s => CQ ct s ∧ BI ct s) (Q := fun s => CQ ct s ∧ BI ct s) nat' (fun _ h => h) + (fun _ s' h z hz => ?_) + have h' : CQ ct s' ∧ BI ct s' := nat' _ h + cases i with + | retype _ _ _ retypeBase newType objSz slots _ => + refine post_liftE (Q := fun s => CQ ct s ∧ BI ct s) + (pc_post (pcp_of (pq_when _ ?_) (dk_when _ ?_)) h'.1 h'.2) z hz + · refine pq_of (dk_mapMx (fun v => ?_) _) (PPNQ.qk_mapMx (fun v => ?_) _) + · unfold setVcpu; exact kf_setArchObj _ _ + · unfold setVcpu; exact PPNQ.qk_setObject' _ _ + · refine dk_mapMx (fun v => ?_) _ + unfold setVcpu; exact sck_setArchObjV _ _ + refine post_bindE (R := fun s => CQ ct s ∧ BI ct s) (Q := fun s => CQ ct s ∧ BI ct s) hV (fun _ h => h) + (fun _ s' h z hz => ?_) y hy + rw [NonDetE.returnOk, ExceptT.run_pure] at hz + rw [Set.mem_singleton_iff.1 hz]; exact hV _ h + case invokeNotification ep badge => + simp only [fl4PerformInvocationVF, withoutPreemption] + have h1 := post_liftE (ε := Unit) (Q := fun s => CQ ct s ∧ BI ct s) + (fun x hx => pc_sendSignalVF onCore remoteCur ep badge σ x hs hb hq hnr hx) + refine post_bindE (R := fun s => CQ ct s ∧ BI ct s) (Q := fun s => CQ ct s ∧ BI ct s) h1 (fun _ h => h) + fun _ s' hm y hy => ?_ + rw [NonDetE.returnOk, ExceptT.run_pure] at hy + rw [Set.mem_singleton_iff.1 hy] + exact h1 _ hm + case invokeTCB _ => exact absurd hf id + case invokeCNode _ => exact absurd hf id + case invokeArchObject i => + cases i with + | frameBatch bi => + simp only [fl4PerformInvocationVF] + exact pce_post (Fl4Proofs.P3.Vs.dke_liftE (kf_performFrameBatchVF bi)) + (Fl4Proofs.P3.Vs.dke_liftE (qv_performFrameBatchVF bi)) + (Fl4Proofs.P3.Vs.dke_liftE (sck_performFrameBatchVF bi)) hs hb + | _ => simp only [fl4PerformInvocationVF]; exact nat + all_goals (simp only [fl4PerformInvocationVF]; exact nat) + +end + +/-! ## The shell and the fact -/ + +set_option maxHeartbeats 1000000 in +/-- **The extended shell with `CQ ∧ BI`**: from `CQ ∧ NQ ∧ BI` at the entry, every outcome of the first-phase +non-blocking non-calling extended invocation is `CQ ∧ BI`. -/ +theorem midRunB_shellVF (onCore remoteCur : ObjRef → Bool) (rep don : Bool) (cptr : CapRef) (σ : State Unit) + (hs : CQ σ.curThread σ) (hq : NQ σ.curThread σ) (hb : BI σ.curThread σ) (hW : JMidPerfHypQ don cptr σ) + (hWB : JMidPerfHypB don cptr σ) : + ∀ y ∈ ((fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep false false don true cptr).run σ).1, + CQ σ.curThread y.2 ∧ BI σ.curThread y.2 := by + intro y hy + unfold fl4HandleInvocationVF at hy + obtain ⟨z, hz, ⟨t, ht, hy⟩ | ⟨e, he, -⟩⟩ := mem_bindE_inv' hy + swap + · obtain ⟨w, -, rfl⟩ := mem_liftE_inv' hz; cases he + obtain ⟨w, hw, rfl⟩ := mem_liftE_inv' hz + obtain rfl : w = (σ.curThread, σ) := hw + simp only [Except.ok.injEq] at ht + subst ht + obtain ⟨z, hz, ⟨info, hi, hy⟩ | ⟨e, he, -⟩⟩ := mem_bindE_inv' hy + swap + · obtain ⟨w, -, rfl⟩ := mem_liftE_inv' hz; cases he + obtain ⟨⟨info', σa⟩, hia, rfl⟩ := mem_liftE_inv' hz + simp only [Except.ok.injEq] at hi + subst hi + have hIa := fqb_I (fv_getMessageInfo (ct := σ.curThread) _) (qv_getMessageInfo' _) (sck_getMessageInfo' _) + ⟨⟨hs, hq⟩, hb⟩ _ hia + refine syscall_post (mF := midF σ.curThread info' cptr) (mE := midEV σ.curThread info' cptr) + (I := fun s => (CQ σ.curThread s ∧ NQ σ.curThread s) ∧ BI σ.curThread s) + (Q := fun s => CQ σ.curThread s ∧ BI σ.curThread s) + (fun w hw => fqbe_I (fve_midF _ _ _) (qve_midF _ _ _) (scke_midF _ _ _) hIa w hw) ?_ + (fun a s has w hw => fqbe_I (fve_midEV _ _ _ a) (qve_midEV _ _ _ a) (scke_midEV _ _ _ a) + (fqbe_I (fve_midF _ _ _) (qve_midF _ _ _) (scke_midF _ _ _) hIa _ has) w hw) ?_ ?_ y hy + · intro f s hI v hv + simp only [NonDet.when, Bool.false_eq_true, ↓reduceIte] at hv + obtain rfl : v = ((), s) := hv + exact ⟨hI.1.1, hI.2⟩ + · intro e s hI v hv + simp only [NonDet.when, Bool.false_eq_true, ↓reduceIte] at hv + obtain rfl : v = ((), s) := hv + exact ⟨hI.1.1, hI.2⟩ + · intro a s b s' has hbs hI y hy + have hout := midEV_out hbs + refine post_bindE + (R := fun s => (CQ σ.curThread s ∧ NQ σ.curThread s) ∧ BI σ.curThread s) + (Q := fun s => CQ σ.curThread s ∧ BI σ.curThread s) ?_ (fun _ h => ⟨h.1.1, h.2⟩) ?_ y hy + · unfold withoutPreemption + exact post_liftE (Q := fun s => (CQ σ.curThread s ∧ NQ σ.curThread s) ∧ BI σ.curThread s) fun x hx => + ⟨⟨pq_setThreadState_run _ _ rfl s' x hI.1.1 hx, NQ.of_nV (nv_setThreadState _ _ s' x hx) hI.1.2⟩, + bi_setThreadState hx hI.2⟩ + intro u s1 h1 y hy + obtain ⟨⟨u', s1'⟩, h1', e1⟩ := mem_liftE_inv' h1 + simp only [Prod.mk.injEq] at e1 + obtain ⟨-, rfl⟩ := e1 + have hI1 : (CQ σ.curThread s1 ∧ NQ σ.curThread s1) ∧ BI σ.curThread s1 := + ⟨⟨pq_setThreadState_run _ _ rfl s' _ hI.1.1 h1', NQ.of_nV (nv_setThreadState _ _ s' _ h1') hI.1.2⟩, + bi_setThreadState h1' hI.2⟩ + have hnr : NotRvm σ.curThread s1 := sts_restart_notRvm h1' + have hP : ∀ z ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur false false don b).run s1).1, + CQ σ.curThread z.2 ∧ BI σ.curThread z.2 := + perf_firstBVF onCore remoteCur don b hout.1 hI1.1.1 hI1.1.2 hI1.2 hnr fun b0 e hw z hz => by + have hA : MidPerfAt cptr σ b0 s1 := ⟨info', σa, a, s, s', hia, has, hout.2 b0 e, h1'⟩ + exact ⟨hW b0 s1 hA hw z hz, hWB b0 s1 hA hw hI1.2 z hz⟩ + refine post_bindE (R := fun s => CQ σ.curThread s ∧ BI σ.curThread s) + (Q := fun s => CQ σ.curThread s ∧ BI σ.curThread s) hP (fun _ h => h) ?_ y hy + intro r s2 h2 y hy + have hs2 : CQ σ.curThread s2 ∧ BI σ.curThread s2 := hP (.ok r, s2) h2 + unfold withoutPreemption at hy + refine post_liftE (Q := fun s => CQ σ.curThread s ∧ BI σ.curThread s) ?_ y hy + refine pc_post ?_ hs2.1 hs2.2 + refine pcp_gts_bind _ fun ts _ => ?_ + split + · refine pcp_bind (by pc_step) fun _ => ?_ + exact pcp_of (fun σ x hs hx => pq_setThreadState_run _ _ rfl σ x hs hx) (sck_setThreadState _ _) + · pc_step + +/-- **The first-phase spec fact at the extended handler**: from the dispatch state, every outcome of the first-phase +extended invocation satisfies `CtOK` (`CurInv`, or the current thread holds no SC). -/ +theorem jMidChooseFactVF (onCore remoteCur : ObjRef → Bool) (rep don : Bool) (cptr : CapRef) (σ : State Unit) + (hs : CQ σ.curThread σ) (hq : NQ σ.curThread σ) (hC : Sched.CurInv σ) (hI : Sched.Inv σ) + (hW : JMidPerfHypQ don cptr σ) (hWB : JMidPerfHypB don cptr σ) : + ∀ y ∈ ((fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep false false don true cptr).run σ).1, + CtOK y.2 := by + intro y hy + obtain ⟨h1, h2⟩ := midRunB_shellVF onCore remoteCur rep don cptr σ hs hq (bi_of hC hI) hW hWB y hy + have e : y.2.curThread = σ.curThread := h1.1.1 + rw [← e] at h1 h2 + exact ctOK_of h1 h2 + +end + +end Fl4Proofs.P3.MidRun diff --git a/proofs/Fl4Proofs/P3/VmMidBlock.lean b/proofs/Fl4Proofs/P3/VmMidBlock.lean new file mode 100644 index 000000000..2b1ea6062 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidBlock.lean @@ -0,0 +1,85 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2): **P1's `CanBlock` of the current thread under `CurInv` at `JAtZB`**, the form +the receive leaves use at a resumed entry and at a mid state (`curScAcB`, native `JAtZ.curScAc`; `canBlockInvB`). +-/ +import Fl4Proofs.P3.VmHandle +import Fl4Proofs.P3.XTopJointRecvMid +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **The current SC is active and in the current thread's SC word** under `CurInv` (native `JAtZ.curScAc`). -/ +theorem JAtZB.curScAcB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hC : CurInv σ) + (hni : σ.curThread ≠ σ.idleThread) : + L.mem s (P1.scA (L.curW s)) = L.cscW s ∧ L.cscW s ∈ Ac := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R0, hc, hs⟩ := hS.lists + obtain ⟨hscC, hsc0, htw, hts, hact, hnr, hnb⟩ := CurInv.facts hS J.inv hC + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have ecur : L.curW s = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + show L.mem s (L.nodeAt s (L.cpu s) _) = _; rw [hc.cur]; rfl + have ecsc : L.cscW s = Sched.nodeW st s Z.c layout.node.CUR_SC := by + show L.mem s (L.nodeAt s (L.cpu s) _) = _; rw [hc.cur]; rfl + obtain ⟨xt, hkt, hxs, -⟩ := hC.tcb + have hcsI : σ.curSc ≠ σ.idleSc := by + intro e + obtain ⟨y, n, hky, hyt⟩ := J.inv.back _ xt _ hkt hxs + obtain ⟨xi, hki, hxi, -, -⟩ := J.act.2 + rw [← e] at hxi + obtain ⟨y', n', hky', hyt'⟩ := J.inv.back _ xi _ hki hxi + rw [hky] at hky'; cases hky' + exact hni (Option.some.inj (hyt.symm.trans hyt')) + have hpc : (Sched.nodeW st s Z.c layout.node.CUR_SC).bv = σ.curSc := hs.curSc.symm + obtain ⟨x, n, hk, hx⟩ := hs.scs _ hscC + have hxa : Fl4Spec.scActive x = true := hact (by rw [hpc]; exact hcsI) x n hk + have hscS : Sched.nodeW st s Z.c layout.node.CUR_SC ∈ P1.Heap.scs H := ⟨_, (hJ.sc _ x n hsc0 hk).2⟩ + refine ⟨by rw [ecur, ecsc, hts], ?_⟩ + rw [ecsc]; exact (J.blk.2.2.2.1.act _ hscS).2 (fun e => hx.rmax_ne hxa (by rw [e]; rfl)) + +set_option maxHeartbeats 8000000 in +/-- **P1's `CanBlock` of the current thread under `CurInv`**, at a resumed entry or a mid state: the thread +runnable, not queued, not the idle thread and not the pending switch target. -/ +theorem JAtZB.canBlockInvB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {ct : U64} (ht : ct ∈ Z.T) + (hcur : σ.curThread = ct.bv) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) (hC : CurInv σ) + (hni : σ.curThread ≠ σ.idleThread) (hrn : Fl4Spec.runnable x.tcbState = true) + (hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p) (hsw : ∀ v, σ.schedulerAction = .switchThread v → v ≠ ct.bv) : + P1.CanBlock st (P1.Heap.tcbs H) Ac s ct := by + have hnr : ct.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hS := J.xtop.fin.1.1.1 + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have htH : ct ∈ P1.Heap.tcbs H := J.zH.1 ct ht + obtain ⟨bq, br⟩ := J.curBits ht hnq' hnr + obtain ⟨hna, -⟩ := J.words_of_spec ht hsw (fun e => hni (by rw [hcur, e])) + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb ct ht + rw [hk] at hk0; cases hk0 + have hv : (L.mem s (P1.stA ct)).val = tsCode x.tcbState := by + rw [P1.stA_eq, ← tStA_eq]; exact hTR.st + obtain ⟨hnb, hnrp⟩ := P1.run_word (w := L.mem s (P1.stA ct)) (by rw [hv]; exact runnable_code hrn) + have hcw : L.curW s = ct := by + apply P1.eq_of_bv + rw [← hcur, hs.curThread] + unfold P1.LawfulStore.curW Sched.nodeW; rw [hcq.cur] + refine ⟨htH, hnb, hnrp, bq, br, hna, ?_, ?_⟩ + · intro h + have ei : L.idleW s = Sched.nodeW st s Z.c layout.node.IDLE_THREAD := by + unfold P1.LawfulStore.idleW Sched.nodeW; rw [hcq.cur] + exact hni (by rw [hcur, hs.idleTh, ← ei, h]) + · obtain ⟨e1, hA⟩ := J.curScAcB hC hni + rw [hcw] at e1 + intro _; rw [e1]; exact hA + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmMidFb.lean b/proofs/Fl4Proofs/P3/VmMidFb.lean new file mode 100644 index 000000000..9a3756cac --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidFb.lean @@ -0,0 +1,65 @@ +/- +P3 (line mmuAD, Stage 3, `VMidF` producer; spec only): **the extended dispatcher's frame batch from the leaves of one +entry.** `ik_performFrameBatchVF_w` is SpecFbX's `ik_performFrameBatchW` for `performFrameBatchVF` (P3/VmFinFbV's +`ik_performFrameBatchXV` / `ik_fbEntryXV_of`): the finaliser of a DELETE is `finaliseCapVF` of a frame cap, which is +`arch_finalise_cap` or the guest unmap of an EPT-mapped frame (`ik_finaliseCapVF_page`). + +No named premise. New file. +-/ +import Fl4Proofs.P3.VmFinFbV +open Classical + +namespace Fl4Proofs.P3.SInvU +open Fl4Spec Fl4Proofs.P3.SInv + +noncomputable section + +variable {P : State Unit → Prop} + +theorem ik_finaliseCapVF_page (dev : Bool) (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) + (m : Option (Asid × VSpaceRef)) (fin : Bool) + (haf : ∀ a b, IK P (archFinaliseCap (Z := Unit) a b)) + (hg : ∀ a gpa p' l t, IK P (guestUnmap (Z := Unit) a gpa p' l t)) : + IK P (finaliseCapVF (Z := Unit) (.archObjectCap (.pageCap dev p R mt sz m)) fin) := by + cases mt <;> rcases m with _ | ⟨a, gpa⟩ <;> first + | exact haf _ _ + | (unfold finaliseCapVF; exact ik_bind (ik_when _ (hg _ _ _ _ _)) fun _ => ik_pure _) + +/-- **`IK P` of the extended frame batch** from the leaves of the writes of one entry and of the final flush. -/ +theorem ik_performFrameBatchVF_w (b : Fl4FrameBatchInvocation) + (hci : ∀ c src dest, IsFrameCap c → IK P (capInsert (Z := Unit) c src dest)) + (hpp : ∀ pi, IK P (performPageInvocation (Z := Unit) pi)) + (haf : ∀ a b, IK P (archFinaliseCap (Z := Unit) a b)) + (hg : ∀ a gpa p' l t, IK P (guestUnmap (Z := Unit) a gpa p' l t)) + (hes : ∀ sl, IK P (emptySlot (Z := Unit) sl .nullCap)) + (hun : ∀ sz vs va p, IK P (unmapPageUnflushed (Z := Unit) sz vs va p)) + (hset : ∀ c sl, IsFrameCap c → IK P (setCap (Z := Unit) c sl)) + (hf : IK P (flushAll (Z := Unit) b.vspace b.asid)) : + IK P (performFrameBatchVF b) := by + refine ik_performFrameBatchXV (fun w => ?_) hf + refine ik_fbEntryXV_of b w fbx_lookupTargetSlot fbx_lookupSourceSlot fbx_ensureEmpty fbx_deriveCap + (fun s c src dest _ hs hc _ _ _ _ _ => hci c src dest hc s hs) (fun sl v => ?_) (fun sl => ?_) + (fun sl => ?_) hun + (fun s dev p R _ sz _ sl hs _ => hset (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) sl trivial s hs) + · unfold fbMap + refine ike_bind (ike_liftE (fbx_ik_getCap _)) fun c => ?_ + split + · refine ike_bind (ike_whenE _ (ike_throw _)) fun _ => ike_bind (fbx_decodePageInvocation _ _ _ _ _) fun iv => ?_ + split + · exact ike_bind (ike_liftE (hpp _)) fun _ => ike_returnOk _ + · exact ike_liftE ik_fail + · exact ike_throw _ + · unfold fbDelX + refine ike_liftE (ik_bind (fbx_ik_getCap _) fun c => ?_) + split + · exact ik_bind (fbx_isFinalCap _) fun _ => ik_bind (haf _ _) fun _ => hes _ + · exact ik_fail + · unfold fbDelXV + refine ike_liftE (ik_bind (fbx_ik_getCap _) fun c => ?_) + split + · exact ik_bind (fbx_isFinalCap _) fun _ => ik_bind (ik_finaliseCapVF_page _ _ _ _ _ _ _ haf hg) fun _ => hes _ + · exact ik_fail + +end + +end Fl4Proofs.P3.SInvU diff --git a/proofs/Fl4Proofs/P3/VmMidProd.lean b/proofs/Fl4Proofs/P3/VmMidProd.lean new file mode 100644 index 000000000..e90173a75 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidProd.lean @@ -0,0 +1,64 @@ +/- +P3 (line mmuAD, Stage 3, `VMidF` producer, plan part (iv); spec only): **the mid state after the first phase of +ReplyRecv / NBSendRecv / NBSendWait at the extended handler** (`vmidF_of_handleInvocationVF`: P3JMidSpec's +`midSpecAt_of` for `fl4HandleInvocationVF`, without `CKI`). + +Every `ok` outcome of `fl4HandleInvocationVF onCore remoteCur true false false true true cptr` from a dispatch state +`σ` is a `VMidF` state: +* `ni`, `nq`, `run`, `nsw`: `CQ` (P3/VmMidRunQ's `midRunQ_shellVF`) and the idle thread kept + (`idk_fl4HandleInvocationVF`); +* `vfh`: `vfh_handleInvocationVF` (P3/VmMidVfh); +* `cho`: P3/VmMidB's `jMidChooseFactVF`. + +The entry facts (`CQ`, `NQ`, `CurInv`, `Sched.Inv`, `VfC`, `UtInv''`, a non-idle current thread) are hypotheses: the +dispatch state's record of the Stage-3 start (step 3.4) supplies them. + +Named premises: [W] `JMidPerfHypQ`, [W] `JMidPerfHypB`. New file. +-/ +import Fl4Proofs.P3.VmMidB +import Fl4Proofs.P3.VmMidVfh +import Fl4Proofs.P3.VmRecvMid +open Classical + +namespace Fl4Proofs.P3 +open Fl4Spec Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched + +noncomputable section + +/-- **The `VMidF` producer** (module docstring). -/ +theorem vmidF_of_handleInvocationVF (onCore remoteCur : ObjRef → Bool) (cptr : Fl4Spec.CapRef) + (σ : Fl4Spec.State Unit) (hs : MidRun.CQ σ.curThread σ) (hq : MidRun.NQ σ.curThread σ) + (hni : σ.curThread ≠ σ.idleThread) (hC : CurInv σ) (hI : Sched.Inv σ) (hV : SInv.VfC σ) + (hU : SInvU.UtInv'' σ) (hW : MidRun.JMidPerfHypQ true cptr σ) (hWB : MidRun.JMidPerfHypB true cptr σ) : + ∀ y ∈ ((Fl4Spec.fl4HandleInvocationVF (Z := Unit) onCore remoteCur true false false true true cptr).run σ).1, + y.1 = .ok () → VMidF y.2 := by + intro y hy _ + obtain ⟨⟨hcy, hrs⟩, hrq, hsw⟩ := + MidRun.midRunQ_shellVF onCore remoteCur true true cptr σ hs hq hW y hy + have hid : y.2.idleThread = σ.idleThread := + SInvG.idk_fl4HandleInvocationVF (N := σ.idleThread) onCore remoteCur true false false true cptr σ rfl y hy + have hv : SInv.VfC y.2 := + SInv.vfh_handleInvocationVF onCore remoteCur true false false true cptr σ hV hU y hy + have hct : MidRun.CtOK y.2 := + MidRun.jMidChooseFactVF onCore remoteCur true true cptr σ hs hq hC hI hW hWB y hy + refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ + · rw [hcy, hid]; exact hni + · rw [hcy]; exact hrq + · intro x hx + exact (SInv.vfhV_iff _).1 hv.1 _ x hx + · intro x hx + rw [hcy] at hx + exact (hrs x hx).1 + · rw [hcy]; exact hsw + · intro _ + rcases hct with h | ⟨x, hk, h0⟩ + · exact Or.inl h + · right + intro x' hx' + rw [hk] at hx' + cases hx' + exact h0 + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmMidQ.lean b/proofs/Fl4Proofs/P3/VmMidQ.lean new file mode 100644 index 000000000..e66d25cf4 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidQ.lean @@ -0,0 +1,171 @@ +/- +P3 (line mmuAD, Stage 3, `VMidF` producer, plan part (i), first leaves; spec only): **the virtualization leaves of the +first-phase invocation keep `CQ`** (XTopJointMidRunQ: `ct` current and runnable without a fault, in no ready queue, no +switch to it pending). + +`qV` and `ctV ct` are kept by the VCPU reads and writes, the guest-table walks and stores, `guestUnmap`, +`ioPortRevoke`, every EPT invocation, and every VCPU invocation but SetTCB (whose detach reports to the old thread: +not here). + +No named premise. New file. +-/ +import Fl4Proofs.P3.XTopJointMidRunQ +import Fl4Proofs.P3.VmDelFinSpec +open Classical + +namespace Fl4Proofs.P3.MidRun +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Vs + +noncomputable section + +/-! ## Reads -/ + +theorem qv_getVcpu (v : ObjRef) : QV (getVcpu (Z := Unit) v) := fk_of_keepSt (keepSt_getVcpu v) + +theorem qv_getEpt (p : ObjRef) : QV (getEpt (Z := Unit) p) := fk_of_keepSt (keepSt_getEpt p) + +theorem kf_getVcpu {ct : ObjRef} (v : ObjRef) : KF ct (getVcpu (Z := Unit) v) := fk_of_keepSt (keepSt_getVcpu v) + +theorem kf_getEpt {ct : ObjRef} (p : ObjRef) : KF ct (getEpt (Z := Unit) p) := fk_of_keepSt (keepSt_getEpt p) + +/-! ## Arch object writes -/ + +theorem kf_setArchObj {ct : ObjRef} (p : ObjRef) (a : ArchKernelObj) : + KF ct (setObject (Z := Unit) p (.archObj a)) := + kf_setObject_nt p _ fun _ h => nomatch h + +theorem qv_vcpuUpdate (v : ObjRef) (g : VcpuState → VcpuState) : QV (vcpuUpdate (Z := Unit) v g) := + fk_vcpuUpdate (fun _ _ => qv_setObject _ _) v g + +theorem kf_vcpuUpdate {ct : ObjRef} (v : ObjRef) (g : VcpuState → VcpuState) : + KF ct (vcpuUpdate (Z := Unit) v g) := + fk_vcpuUpdate (fun p a => kf_setArchObj p a) v g + +theorem fk_storeGuestEntry {β : Type} {f : State Unit → β} + (hs : ∀ p (a : ArchKernelObj), FK f (setObject (Z := Unit) p (.archObj a))) + (p : ObjRef) (i : BitVec 9) (e : Word) : FK f (storeGuestEntry (Z := Unit) p i e) := by + unfold storeGuestEntry + exact dk_bind (fk_of_keepSt (keepSt_getEpt p)) fun _ => hs _ _ + +theorem qv_storeGuestEntry (p : ObjRef) (i : BitVec 9) (e : Word) : QV (storeGuestEntry (Z := Unit) p i e) := + fk_storeGuestEntry (fun _ _ => qv_setObject _ _) p i e + +theorem kf_storeGuestEntry {ct : ObjRef} (p : ObjRef) (i : BitVec 9) (e : Word) : + KF ct (storeGuestEntry (Z := Unit) p i e) := + fk_storeGuestEntry (fun p a => kf_setArchObj p a) p i e + +theorem qv_guestUnmap (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + QV (guestUnmap (Z := Unit) asid gpa ptr level table) := + fk_guestUnmap (fun _ _ => qv_setObject _ _) asid gpa ptr level table + +theorem kf_guestUnmap {ct : ObjRef} (asid : Asid) (gpa : Word) (ptr : ObjRef) (level : Nat) (table : Bool) : + KF ct (guestUnmap (Z := Unit) asid gpa ptr level table) := + fk_guestUnmap (fun p a => kf_setArchObj p a) asid gpa ptr level table + +theorem qv_ioPortRevoke (c : ArchCap) : QV (ioPortRevoke (Z := Unit) c) := + fk_ioPortRevoke (fun _ _ => qv_setObject _ _) c + +theorem kf_ioPortRevoke {ct : ObjRef} (c : ArchCap) : KF ct (ioPortRevoke (Z := Unit) c) := + fk_ioPortRevoke (fun p a => kf_setArchObj p a) c + +/-! ## The EPT and VCPU invocations -/ + +/-- An EPT invocation keeps what arch object writes, `set_cap` and the ASID pool store keep. -/ +theorem fk_performEptInvocation {β : Type} {f : State Unit → β} + (hs : ∀ p (a : ArchKernelObj), FK f (setObject (Z := Unit) p (.archObj a))) + (hc : ∀ c sl, FK f (setCap (Z := Unit) c sl)) (hg : ∀ sl, FK f (getCap (Z := Unit) sl)) + (hp : ∀ p a b, FK f (storeAsidPoolEntry (Z := Unit) p a b)) + (i : EptInvocation) : FK f (performEptInvocation (Z := Unit) i) := by + cases i with + | tableMap ptr level slot tbl i asid gpa => + unfold performEptInvocation + exact dk_bind (fk_storeGuestEntry hs _ _ _) fun _ => hc _ _ + | tableUnmap ptr level mapped slot => + unfold performEptInvocation + cases mapped with + | none => exact dk_pure _ + | some m => + obtain ⟨asid, gpa⟩ := m + exact dk_bind (fk_guestUnmap hs _ _ _ _ _) fun _ => + dk_bind (fk_of_keepSt (keepSt_getEpt _)) fun _ => dk_bind (hs _ _) fun _ => hc _ _ + | frameMap newCap slot tbl i entry => + unfold performEptInvocation + exact dk_bind (hc _ _) fun _ => fk_storeGuestEntry hs _ _ _ + | frameUnmap cap slot => + unfold performEptInvocation + cases cap + case pageCap dev p R _ sz m => + cases m with + | none => exact dk_pure _ + | some m => + obtain ⟨asid, gpa⟩ := m + exact dk_bind (dk_when _ (fk_guestUnmap hs _ _ _ _ _)) fun _ => hc _ _ + all_goals exact fun _ _ h => h.elim + | poolAssign asid pool slot => + unfold performEptInvocation + refine dk_bind (hg _) fun c => ?_ + split + · exact dk_bind (hc _ _) fun _ => hp _ _ _ + · exact fun _ _ h => h.elim + +theorem qv_performEptInvocation (i : EptInvocation) : QV (performEptInvocation (Z := Unit) i) := + fk_performEptInvocation (fun _ _ => qv_setObject _ _) (fun _ _ => qv_setCap _ _) (fun _ => qv_getCap _) + (fun _ _ _ => qv_storeAsidPoolEntry _ _ _) i + +theorem kf_performEptInvocation {ct : ObjRef} (i : EptInvocation) : + KF ct (performEptInvocation (Z := Unit) i) := + fk_performEptInvocation (fun p a => kf_setArchObj p a) (fun _ _ => by kf_auto) (fun _ => by kf_auto) + (fun _ _ _ => kf_storeAsidPoolEntry _ _ _) i + +/-- The VCPU invocation is not SetTCB. -/ +def VcpuNotSetTcb : VcpuInvocation → Prop + | .setTcb _ _ => False + | _ => True + +/-- A VCPU invocation but SetTCB keeps what arch object writes and `set_cap` keep. -/ +theorem fk_performVcpuInvocationF {β : Type} {f : State Unit → β} + (hs : ∀ p (a : ArchKernelObj), FK f (setObject (Z := Unit) p (.archObj a))) + (hc : ∀ c sl, FK f (setCap (Z := Unit) c sl)) + (i : VcpuInvocation) (hi : VcpuNotSetTcb i) : FK f (performVcpuInvocationF (Z := Unit) i) := by + have hu : ∀ v g, FK f (vcpuUpdate (Z := Unit) v g) := fk_vcpuUpdate hs + have hv : ∀ v, FK f (getVcpu (Z := Unit) v) := fun v => fk_of_keepSt (keepSt_getVcpu v) + cases i with + | setTcb v t => exact absurd hi id + | writeRegisters v vals => unfold performVcpuInvocationF; exact dk_bind (hu _ _) fun _ => dk_pure _ + | flushTlb v => unfold performVcpuInvocationF; exact dk_pure _ + | readVmcs v g => unfold performVcpuInvocationF; exact dk_bind (hv _) fun _ => dk_pure _ + | writeVmcs v g val => unfold performVcpuInvocationF; exact dk_bind (hu _ _) fun _ => dk_pure _ + | readVmcb v off n => unfold performVcpuInvocationF; exact dk_bind (hv _) fun _ => dk_pure _ + | writeVmcb v off val => unfold performVcpuInvocationF; exact dk_bind (hu _ _) fun _ => dk_pure _ + | readMsr v m => unfold performVcpuInvocationF; exact dk_bind (hv _) fun _ => dk_pure _ + | readVmxMisc => unfold performVcpuInvocationF; exact dk_pure _ + | writeMsr v m val => unfold performVcpuInvocationF; exact dk_bind (hu _ _) fun _ => dk_pure _ + | enableIoPort v lo hi c slot => + unfold performVcpuInvocationF ioPortSetOwner + refine dk_bind (fk_ioPortRevoke hs _) fun _ => dk_bind ?_ fun _ => dk_bind (hu _ _) fun _ => dk_pure _ + split + · exact hc _ _ + · exact dk_pure _ + | disableIoPort v lo hi => unfold performVcpuInvocationF; exact dk_bind (hu _ _) fun _ => dk_pure _ + +theorem qv_performVcpuInvocationF (i : VcpuInvocation) (hi : VcpuNotSetTcb i) : + QV (performVcpuInvocationF (Z := Unit) i) := + fk_performVcpuInvocationF (fun _ _ => qv_setObject _ _) (fun _ _ => qv_setCap _ _) i hi + +theorem kf_performVcpuInvocationF {ct : ObjRef} (i : VcpuInvocation) (hi : VcpuNotSetTcb i) : + KF ct (performVcpuInvocationF (Z := Unit) i) := + fk_performVcpuInvocationF (fun p a => kf_setArchObj p a) (fun _ _ => by kf_auto) i hi + +/-- **Every EPT invocation keeps `CQ`.** -/ +theorem pq_performEptInvocation {ct : ObjRef} (i : EptInvocation) : + PQ ct (performEptInvocation (Z := Unit) i) := + pq_of (kf_performEptInvocation i) (qk_of_qv (qv_performEptInvocation i)) + +/-- **Every VCPU invocation but SetTCB keeps `CQ`.** -/ +theorem pq_performVcpuInvocationF {ct : ObjRef} (i : VcpuInvocation) (hi : VcpuNotSetTcb i) : + PQ ct (performVcpuInvocationF (Z := Unit) i) := + pq_of (kf_performVcpuInvocationF i hi) (qk_of_qv (qv_performVcpuInvocationF i hi)) + +end + +end Fl4Proofs.P3.MidRun diff --git a/proofs/Fl4Proofs/P3/VmMidRunQ.lean b/proofs/Fl4Proofs/P3/VmMidRunQ.lean new file mode 100644 index 000000000..694cc0ac5 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidRunQ.lean @@ -0,0 +1,558 @@ +/- +P3 (line mmuAD, Stage 3, `VMidF` producer, plan part (i); spec only): **the first-phase non-blocking non-calling +extended invocation keeps `CQ`** (`midRunQ_shellVF`: XTopJointMidRunQ's `midRunQ_shell` for +`fl4HandleInvocationVF`). + +* `pq_vcpuDetachF`, `pq_associateVcpuTcbF`: the VMEnter report to ANY thread keeps `CQ`: its only state write is + `setThreadState t Running`, a runnable state (so no "`t ≠ ct`" is needed for SetTCB); every other step is a + P3/SpecPPRQ `QK` step keeping `ct`'s state. Hence **`pq_performVcpuInvocationF_all`**. +* `kf_performFrameBatchVF`, `qv_performFrameBatchVF` (P3/VmMidFb). +* `pq_sendSignalVF`: the VM branch wakes a `runningVM` thread, hence not `ct` when `ct` is not `runningVM`; at the + perform `ct` is `Restart` (`NotRvm`, from `setThreadState ct Restart`). +* The decode: `okr_decodeFirstV` (only `FirstInvV` invocations: no SetEPTRoot, no TCB / CNode invocation), + `fve_decodeFirstV`, `qve_decodeFirstV`. +* `perf_firstQVF`, **`midRunQ_shellVF`**. The Untyped, IRQ handler set / clear and ASID control performs rest on the + native premise [W] `JMidPerfHypQ` (XTopJointMidRunQ; the Untyped's VCPU initialisation after it writes no TCB and + no queue): no new premise. + +Named premise: [W] `JMidPerfHypQ`. New file. +-/ +import Fl4Proofs.P3.VmMidQ +import Fl4Proofs.P3.VmMidFb +import Fl4Proofs.P3.VmFinDecode +open Classical + +namespace Fl4Proofs.P3.MidRun +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Vs + +noncomputable section + +set_option maxRecDepth 20000 + +/-! ## The VMEnter report keeps `CQ` -/ + +section +variable {ct : ObjRef} + +theorem kf_of_keepSt {α : Type} {m : NonDet (State Unit) α} (h : KeepSt m) : KF ct m := fk_of_keepSt h + +theorem kf_getTcbVcpu (t : ObjRef) : KF ct (getTcbVcpu (Z := Unit) t) := by + unfold getTcbVcpu; exact dk_gets _ + +theorem kf_vmFullReply (t : ObjRef) : KF ct (vmFullReply (Z := Unit) t) := by + unfold vmFullReply; exact dk_bind (kf_asUser _ _) fun _ => dk_pure _ + +theorem kf_lookupIpcBufferV (b : Bool) (t : ObjRef) : KF ct (lookupIpcBuffer (Z := Unit) b t) := + fun σ x hx => congrArg Prod.fst (fv_lookupIpcBuffer (ct := ct) b t σ x hx) + +theorem pr_vmReportF (t : ObjRef) (b : Word) : PR ct (vmReportF (Z := Unit) t b) := by + unfold vmReportF vmFaultIpNext + repeat' (first + | with_reducible exact pr_of_kf (dk_pure _) + | with_reducible exact pr_of_kf (kf_getTcbVcpu _) + | with_reducible exact pr_of_kf (kf_getVcpu _) + | with_reducible exact pr_of_kf (kf_vmFullReply _) + | with_reducible exact pr_of_kf (kf_lookupIpcBufferV _ _) + | with_reducible exact pr_of_kf (kf_setMrs' _ _ _) + | with_reducible exact pr_of_kf (kf_vcpuUpdate _ _) + | with_reducible exact pr_of_kf (kf_asUser _ _) + | with_reducible exact pr_setThreadState_run _ _ rfl + | (with_reducible refine pr_bind ?_ fun _ => ?_) + | split + | dsimp only) + +theorem pq_vmReportF (t : ObjRef) (b : Word) : PQ ct (vmReportF (Z := Unit) t b) := + fun σ x hs hx => ⟨pr_vmReportF t b σ x hs.1 hx, qk_vmReportF t b σ x hx hs.2⟩ + +theorem pq_vcpuUpdate (v : ObjRef) (g : VcpuState → VcpuState) : PQ ct (vcpuUpdate (Z := Unit) v g) := + pq_of (kf_vcpuUpdate v g) (qk_of_qv (qv_vcpuUpdate v g)) + +theorem pq_of_keepSt {α : Type} {m : NonDet (State Unit) α} (h : KeepSt m) : PQ ct m := + fun σ x hs hx => by rw [h σ x hx]; exact hs + +theorem pq_gets {α : Type} (g : State Unit → α) : PQ ct (NonDet.gets g) := + pq_of (dk_gets _) (PPNQ.qk_gets _) + +theorem pq_pure {α : Type} (a : α) : PQ ct (pure a : NonDet (State Unit) α) := + pq_of (dk_pure _) (PPNQ.qk_pure _) + +theorem pq_when (P : Prop) [Decidable P] {m : NonDet (State Unit) Unit} (h : PQ ct m) : PQ ct (NonDet.when P m) := by + unfold NonDet.when + split + · exact h + · exact pq_of (dk_pure' _) (PPNQ.qk_pure' _) + +theorem pq_getTcbVcpu (t : ObjRef) : PQ ct (getTcbVcpu (Z := Unit) t) := by + unfold getTcbVcpu; exact pq_gets _ + +theorem pq_getThreadState (t : ObjRef) : PQ ct (getThreadState (Z := Unit) t) := + pq_of (by unfold getThreadState; exact kf_threadGet' _ _) (PPNQ.qk_getThreadState' _) + +theorem pq_vcpuDetachF (t : ObjRef) : PQ ct (vcpuDetachF (Z := Unit) t) := by + unfold vcpuDetachF + refine pq_bind (pq_getTcbVcpu _) fun vo => ?_ + cases vo with + | none => exact pq_pure _ + | some v => + exact pq_bind (pq_getThreadState _) fun _ => pq_bind (pq_when _ (pq_vmReportF _ _)) fun _ => pq_vcpuUpdate _ _ + +theorem pq_associateVcpuTcbF (v t : ObjRef) : PQ ct (associateVcpuTcbF (Z := Unit) v t) := by + unfold associateVcpuTcbF + refine pq_bind (pq_vcpuDetachF _) fun _ => ?_ + refine pq_bind (pq_of_keepSt (keepSt_getVcpu v)) fun st => ?_ + cases st.tcb with + | none => + first + | (with_reducible exact pq_vcpuUpdate _ _) + | (with_reducible exact pq_bind (pq_pure _) fun _ => pq_vcpuUpdate _ _) + | some old => with_reducible exact pq_bind (pq_vcpuDetachF _) fun _ => pq_vcpuUpdate _ _ + +/-- **Every VCPU invocation keeps `CQ`.** -/ +theorem pq_performVcpuInvocationF_all (i : VcpuInvocation) : PQ ct (performVcpuInvocationF (Z := Unit) i) := by + cases i with + | setTcb v t => + unfold performVcpuInvocationF + exact pq_bind (pq_associateVcpuTcbF _ _) fun _ => pq_pure _ + | _ => exact pq_performVcpuInvocationF _ trivial + +/-! ## The frame batch -/ + +theorem kf_performFrameBatchVF (bi : Fl4FrameBatchInvocation) : KF ct (performFrameBatchVF bi) := by + intro σ' x hx + have k : ∀ {α : Type} {m : NonDet (State Unit) α}, KF ct m → + Fl4Proofs.P3.SInv.IK (fun s => ctV ct s = ctV ct σ') m := + fun h s hs y hy => (h s y hy).trans hs + refine Fl4Proofs.P3.SInvU.ik_performFrameBatchVF_w (P := fun s => ctV ct s = ctV ct σ') bi + (fun _ _ _ _ => k (kf_capInsert _ _ _)) (fun _ => k (kf_performPageInvocation _)) + (fun _ _ => k (kf_archFinaliseCap' _ _)) (fun _ _ _ _ _ => k (kf_guestUnmap _ _ _ _ _)) + (fun _ => k (kf_emptySlot' _ _)) + (fun _ _ _ _ => k ?_) (fun _ _ _ => k (kf_setCap _ _)) (k (kf_flushAll _ _)) σ' rfl x hx + unfold unmapPageUnflushed; kf_auto + +set_option maxHeartbeats 4000000 in +theorem qv_performFrameBatchVF (bi : Fl4FrameBatchInvocation) : QV (performFrameBatchVF bi) := by + intro σ' x hx + have k : ∀ {α : Type} {m : NonDet (State Unit) α}, QV m → + Fl4Proofs.P3.SInv.IK (fun s => qV s = qV σ') m := + fun h s hs y hy => (h s y hy).trans hs + refine Fl4Proofs.P3.SInvU.ik_performFrameBatchVF_w (P := fun s => qV s = qV σ') bi + (fun _ _ _ _ => k (qv_capInsert _ _ _)) (fun _ => k (qv_performPageInvocation _)) + (fun _ _ => k ?_) (fun _ _ _ _ _ => k (qv_guestUnmap _ _ _ _ _)) (fun _ => k ?_) + (fun _ _ _ _ => k ?_) (fun _ _ _ => k (qv_setCap _ _)) (k (qv_flushAll _ _)) σ' rfl x hx + · simp only [archFinaliseCap]; qv_auto2 + · unfold emptySlot; qv_auto2 + · unfold unmapPageUnflushed; qv_auto2 + +/-! ## `send_signal` -/ + +/-- `ct`'s TCB (if any) is not `runningVM`. -/ +def NotRvm (ct : ObjRef) (σ : State Unit) : Prop := + ∀ x, σ.kheap ct = some (.tcb x) → x.tcbState ≠ .runningVM + +theorem NotRvm.of_ctV {σ σ' : State Unit} (e : ctV ct σ' = ctV ct σ) (h : NotRvm ct σ) : NotRvm ct σ' := by + intro x hx + have e2 := congrArg Prod.snd e + simp only [ctV] at e2 + rw [hx] at e2 + rcases hk : σ.kheap ct with _ | k + · rw [hk] at e2; simp [tsV] at e2 + · rw [hk] at e2 + cases k <;> simp [tsV] at e2 + rw [e2.1]; exact h _ hk + +theorem fv_vmSignalCompletes (onCore remoteCur : ObjRef → Bool) (t : ObjRef) : + FV ct (vmSignalCompletes (Z := Unit) onCore remoteCur t) := by + unfold vmSignalCompletes getTcbVcpu + refine dk_bind (dk_gets _) fun vo => ?_ + cases vo with + | none => exact dk_pure _ + | some v => + refine dk_bind (fk_of_keepSt (keepSt_getVcpu v)) fun _ => ?_ + refine dk_bind ?_ fun _ => dk_pure _ + unfold vmFullReply + exact dk_bind (fv_asUser _ _) fun _ => dk_pure _ + +theorem qv_vmSignalCompletes (onCore remoteCur : ObjRef → Bool) (t : ObjRef) : + QV (vmSignalCompletes (Z := Unit) onCore remoteCur t) := by + unfold vmSignalCompletes getTcbVcpu + refine dk_bind (dk_gets _) fun vo => ?_ + cases vo with + | none => exact dk_pure _ + | some v => + refine dk_bind (qv_getVcpu v) fun _ => ?_ + refine dk_bind ?_ fun _ => dk_pure _ + unfold vmFullReply + exact dk_bind (qv_asUser _ _) fun _ => dk_pure _ + +set_option maxHeartbeats 2000000 in +theorem pq_sendSignalVF (onCore remoteCur : ObjRef → Bool) (n : ObjRef) (b : Badge) : + ∀ σ x, CQ ct σ → NQ ct σ → NotRvm ct σ → x ∈ (sendSignalVF (Z := Unit) onCore remoteCur n b σ).1 → + CQ ct x.2 := by + intro σ x hs hq hnr hx + unfold sendSignalVF at hx + obtain ⟨⟨nt, s1⟩, h1, hx⟩ := hx + obtain ⟨e1, hk⟩ := getSimpleKo_mem h1 + simp only at e1 hk + subst e1 + rcases hno : nt.ntfnObj with _ | q | b' <;> rcases hbt : nt.ntfnBoundTcb with _ | tcb <;> + simp only [hno, hbt] at hx + case idleNtfn.some => + obtain ⟨⟨st, s2⟩, h2, hx⟩ := hx + unfold getThreadState threadGet at h2 + obtain ⟨⟨xt, s0⟩, h20, h21⟩ := h2 + obtain ⟨e0, e0'⟩ := mem_getsThe' h20 + simp only at e0 e0' + subst e0' + have h21 := mem_pure' h21 + simp only [Prod.mk.injEq] at h21 + obtain ⟨rfl, rfl⟩ := h21 + obtain ⟨⟨comp, s3⟩, h3, hx⟩ := hx + have ev := fv_vmSignalCompletes (ct := ct) onCore remoteCur tcb _ _ h3 + have eq' := qv_vmSignalCompletes onCore remoteCur tcb _ _ h3 + simp only [fV, Prod.mk.injEq] at ev + have hs3 : CQ ct s3 := ⟨CtRs.of_ctV ev.1 hs.1, qk_of_qv (qv_vmSignalCompletes _ _ _) _ _ h3 hs.2⟩ + have hq3 : NQ ct s3 := NQ.of_nV ev.2 hq + dsimp only at hx + split at hx + · next hc => + have hd : tcb ≠ ct := fun e => by + subst e + exact hnr _ (getTcb_some e0) hc.1 + refine (?_ : PQ ct _) s3 x hs3 hx + refine pq_bind (pq_vmReportF _ _) fun _ => ?_ + refine pq_bind (pq_of (kf_maybeDonateSc' _ _) (qk_maybeDonateScQ _ _)) fun _ => ?_ + refine pq_bind (pq_gets _) fun _ => ?_ + refine pq_bind (pq_when _ (pq_of (kf_possibleSwitchTo' _) (PPNQ.qk_possibleSwitchTo hd))) fun _ => ?_ + refine pq_bind (pq_of (kf_getTcbObjRef' _ _) (PPNQ.qk_getTcbObjRef' _ _)) fun scOpt => ?_ + cases scOpt with + | none => exact pq_of (dk_pure' _) (PPNQ.qk_pure' _) + | some scp => + show PQ ct _ + unfold NonDet.maybeM + dsimp only + refine pq_bind (pq_of (kf_getScActive' _) (by unfold getScActive; exact PPNQ.qk_getsThe _)) fun _ => ?_ + refine pq_when _ ?_ + pq_step + · exact pq_sendSignal n b s3 x hs3 hq3 hx + all_goals exact pq_sendSignal n b _ x hs hq hx + +/-! ## The first-phase decode -/ + +/-- The invocations a first-phase decode yields. -/ +def FirstInvV : Fl4InvocationV → Prop + | .base b => FirstInv b + | .setEptRoot _ => False + | _ => True + +theorem okr_decodeFirstV (label : Data) (args : List Data) (ci : CapRef) (sl : CslotPtr) (cap : Cap) + (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) (σ s : State Unit) (i : Fl4InvocationV) + (h : ((.ok i, s) : Except SyscallError Fl4InvocationV × State Unit) ∈ + ((fl4DecodeInvocationV (Z := Unit) true label args ci sl cap ex buf).run σ).1) : FirstInvV i := by + cases i with + | base b => + have h' := Fl4Proofs.P3.VmFin.fl4DecodeInvocationV_base true label args ci sl cap ex buf σ s b h + exact okAll_decodeFirst label args ci sl cap ex buf σ _ h' b rfl + | vcpu _ => trivial + | ept _ => trivial + | setEptRoot r => + have vm : ∀ {α : Type} (f : α → Fl4InvocationV) (_ : ∀ a, f a ≠ .setEptRoot r) (m : SEMonad Unit α), + ((.ok (.setEptRoot r), s) : Except SyscallError Fl4InvocationV × State Unit) ∉ ((NonDetE.liftME f m).run σ).1 := + fun f hf m hm => (Fl4Proofs.P3.VmFin.okr_liftME (P := fun x => x ≠ .setEptRoot r) (m := m) fun _ a _ _ => hf a) σ _ s hm rfl + have thr : ∀ e : SyscallError, ((.ok (.setEptRoot r), s) : Except SyscallError Fl4InvocationV × State Unit) ∉ + ((NonDetE.throwError e : SEMonad Unit Fl4InvocationV).run σ).1 := + fun e hm => (Fl4Proofs.P3.VmFin.okr_throw (P := fun _ => False) e) σ _ s hm + unfold fl4DecodeInvocationV at h + dsimp only at h + split at h + · exact absurd h (vm _ (fun _ => by simp) _) + · exact absurd h (vm _ (fun _ => by simp) _) + · split at h + · exact absurd h (vm _ (fun _ => by simp) _) + split at h + · exact absurd h (thr _) + split at h + · have e := Set.mem_singleton_iff.1 h; simp at e + · exact absurd h (vm _ (fun _ => by simp) _) + · split at h + · exact absurd h (vm _ (fun _ => by simp) _) + · exact absurd h (vm _ (fun _ => by simp) _) + · split at h + · next hc => exact absurd rfl hc.1 + · exact absurd h (vm _ (fun _ => by simp) _) + · split at h + · exact absurd h (thr _) + · exact absurd h (vm _ (fun _ => by simp) _) + · exact absurd h (vm _ (fun _ => by simp) _) + +/-! ### The VM decoders read only -/ + +theorem fke_of_id {β ε α : Type} {f : State Unit → β} {m : NonDetE (State Unit) ε α} + (h : FKE (fun σ : State Unit => σ) m) : FKE f m := fun σ x hx => congrArg f (h σ x hx) + +theorem rd_getVcpu (v : ObjRef) : FK (fun σ : State Unit => σ) (getVcpu (Z := Unit) v) := + fk_of_keepSt (keepSt_getVcpu v) + +theorem rd_getEpt (p : ObjRef) : FK (fun σ : State Unit => σ) (getEpt (Z := Unit) p) := fk_of_keepSt (keepSt_getEpt p) + +theorem rd_getAsidPool (p : ObjRef) : FK (fun σ : State Unit => σ) (getAsidPool (Z := Unit) p) := by + unfold getAsidPool; dk_auto0 + +theorem rd_isFinalCap (c : Cap) : FK (fun σ : State Unit => σ) (isFinalCap (Z := Unit) c) := by + unfold isFinalCap; exact dk_gets _ + +theorem rd_eptCapRoot (c : Cap) : FKE (fun σ : State Unit => σ) (eptCapRoot (Z := Unit) c) := by + unfold eptCapRoot + split + · exact dke_bind (dke_handleE (fk_of_keepSt (keepSt_findGuestRoot _)) fun _ => dke_throw _) fun _ => + dke_bind (dke_whenE _ (dke_throw _)) fun _ => dke_returnOk _ + · exact dke_throw _ + +theorem rd_guestSlot (r : ObjRef) (g : Word) (l : Nat) : FKE (fun σ : State Unit => σ) (guestSlot (Z := Unit) r g l) := by + unfold guestSlot + exact dke_handleE (fk_of_keepSt (keepSt_guestLookupSlot _ _ _)) fun _ => dke_throw _ + +theorem rd_decodeVcpuInvocation (label : Data) (args : List Data) (v : ObjRef) (ex : List (Cap × CslotPtr)) : + FKE (fun σ : State Unit => σ) (decodeVcpuInvocation (Z := Unit) label args v ex) := by + unfold decodeVcpuInvocation + repeat' (first | exact rd_getVcpu _ | dk_step0) + +theorem rd_decodeEptInvocation (label : Data) (args : List Data) (sl : CslotPtr) (c : ArchCap) + (ex : List (Cap × CslotPtr)) : FKE (fun σ : State Unit => σ) (decodeEptInvocation (Z := Unit) label args sl c ex) := by + unfold decodeEptInvocation + repeat' (first | exact rd_getEpt _ | exact rd_isFinalCap _ | exact rd_eptCapRoot _ | exact rd_guestSlot _ _ _ | dk_step0) + +theorem rd_decodePageMapEpt (args : List Data) (sl : CslotPtr) (c : ArchCap) (ex : List (Cap × CslotPtr)) : + FKE (fun σ : State Unit => σ) (decodePageMapEpt (Z := Unit) args sl c ex) := by + unfold decodePageMapEpt + repeat' (first | exact rd_getEpt _ | exact rd_eptCapRoot _ | exact rd_guestSlot _ _ _ | dk_step0) + +theorem rd_decodeEptAsidPoolAssign (p : ObjRef) (b : Asid) (x : CslotPtr) : + FKE (fun σ : State Unit => σ) (decodeEptAsidPoolAssign (Z := Unit) p b x) := by + unfold decodeEptAsidPoolAssign selectExt + repeat' (first | exact rd_getAsidPool _ | dk_step0) + +/-- The first-phase extended decode keeps what the first-phase native decode keeps. -/ +theorem fke_decodeFirstV {β : Type} {f : State Unit → β} (label : Data) (args : List Data) (ci : CapRef) + (sl : CslotPtr) (cap : Cap) (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) + (hb : FKE f (fl4DecodeInvocation (Z := Unit) true label args ci sl cap ex buf)) : + FKE f (fl4DecodeInvocationV (Z := Unit) true label args ci sl cap ex buf) := by + have hb' : FKE f (NonDetE.liftME Fl4InvocationV.base + (fl4DecodeInvocation (Z := Unit) true label args ci sl cap ex buf)) := dke_liftME _ hb + unfold fl4DecodeInvocationV + dsimp only + split + · exact dke_liftME _ (fke_of_id (rd_decodeVcpuInvocation _ _ _ _)) + · exact dke_liftME _ (fke_of_id (rd_decodeEptInvocation _ _ _ _ _)) + · refine dke_ite _ (dke_liftME _ (fke_of_id (rd_decodePageMapEpt _ _ _ _))) ?_ + exact dke_ite _ (dke_throw _) (dke_ite _ (dke_returnOk _) hb') + · exact dke_ite _ (dke_liftME _ (fke_of_id (rd_decodeEptAsidPoolAssign _ _ _))) hb' + · rw [if_neg (fun h => h.1 rfl)]; exact hb' + · exact dke_ite _ (dke_throw _) hb' + · exact hb' + +/-- The first-phase decode phase of the extended invocation. -/ +def midEV (thread : ObjRef) (info : MessageInfo) (cptr : CapRef) : + CslotPtr × Cap × List (Cap × CslotPtr) × Option ObjRef → SEMonad Unit Fl4InvocationV := + fun (slot, cap, extracaps, buffer) => do + let args ← NonDetE.liftE <| getMrs thread buffer info + fl4DecodeInvocationV true info.miLabel args cptr slot cap extracaps buffer + +theorem fve_midEV (t : ObjRef) (info : MessageInfo) (cptr : CapRef) (a) : FVE ct (midEV t info cptr a) := by + obtain ⟨sl, cap, ex, buf⟩ := a + unfold midEV + exact dke_bind (dke_liftE (fv_getMrs _ _ _)) fun _ => fke_decodeFirstV _ _ _ _ _ _ _ (fve_decodeFirst _ _ _ _ _ _ _) + +theorem qve_midEV (t : ObjRef) (info : MessageInfo) (cptr : CapRef) (a) : QVE (midEV t info cptr a) := by + obtain ⟨sl, cap, ex, buf⟩ := a + unfold midEV + exact dke_bind (dke_liftE (qv_getMrs _ _ _)) fun _ => fke_decodeFirstV _ _ _ _ _ _ _ (qve_decodeFirst _ _ _ _ _ _ _) + +/-- An outcome of the extended decode phase: `FirstInvV`, and a `.base` one is an outcome of the native phase. -/ +theorem midEV_out {t : ObjRef} {info : MessageInfo} {cptr : CapRef} {a} {σ s : State Unit} {i : Fl4InvocationV} + (h : ((.ok i, s) : Except SyscallError _ × State Unit) ∈ ((midEV t info cptr a).run σ).1) : + FirstInvV i ∧ ∀ b, i = .base b → + ((.ok b, s) : Except SyscallError _ × State Unit) ∈ ((midE t info cptr a).run σ).1 := by + obtain ⟨sl, cap, ex, buf⟩ := a + unfold midEV at h + obtain ⟨z, hz, ⟨args, ha, h⟩ | ⟨e, he, h'⟩⟩ := mem_bindE_inv' h + swap + · obtain ⟨w, -, rfl⟩ := mem_liftE_inv' hz; cases he + refine ⟨okr_decodeFirstV _ _ _ _ _ _ _ _ _ _ h, fun b e => ?_⟩ + subst e + have hn := Fl4Proofs.P3.VmFin.fl4DecodeInvocationV_base _ _ _ _ _ _ _ _ _ _ _ h + unfold midE + rw [ExceptT.run_bind] + refine ⟨z, hz, ?_⟩ + obtain ⟨r, s'⟩ := z + simp only at ha + subst ha + exact hn + +/-! ## The perform -/ + +/-- **The perform of a first-phase extended invocation keeps `CQ`**, `ct` not `runningVM`; the `PerfW` ones by +`hW`. -/ +theorem perf_firstQVF (onCore remoteCur : ObjRef → Bool) (don : Bool) (inv : Fl4InvocationV) (hf : FirstInvV inv) + {σ : State Unit} (hs : CQ ct σ) (hq : NQ ct σ) (hnr : NotRvm ct σ) + (hW : ∀ b, inv = .base b → PerfW b → + ∀ y ∈ ((fl4PerformInvocation (Z := Unit) false false don b).run σ).1, CQ ct y.2) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur false false don inv).run σ).1, CQ ct y.2 := by + cases inv with + | vcpu i => + simp only [fl4PerformInvocationVF] + exact post_liftE (pq_post (pq_performVcpuInvocationF_all i) hs) + | ept i => + simp only [fl4PerformInvocationVF] + exact post_liftE (pq_post (pq_bind (pq_performEptInvocation i) fun _ => pq_pure _) hs) + | setEptRoot _ => exact absurd hf id + | base b => + have gen : ¬ PerfW b → ∀ y ∈ ((fl4PerformInvocation (Z := Unit) false false don b).run σ).1, CQ ct y.2 := + fun h => perf_firstQ don b hf h hs hq + have nat : ∀ y ∈ ((fl4PerformInvocation (Z := Unit) false false don b).run σ).1, CQ ct y.2 := by + by_cases h : PerfW b + · exact hW b rfl h + · exact gen h + cases b + case invokeUntyped i => + intro y hy + simp only [fl4PerformInvocationVF] at hy + unfold fl4InvokeUntypedV at hy + have nat' : ∀ z ∈ ((fl4InvokeUntyped (Z := Unit) i).run σ).1, CQ ct z.2 := by + intro z hz + obtain ⟨r, s⟩ := z + cases r with + | error e => + refine nat (.error e, s) ?_ + simp only [fl4PerformInvocation] + rw [ExceptT.run_bind] + exact ⟨(.error e, s), hz, Set.mem_singleton _⟩ + | ok u => + refine nat (.ok [], s) ?_ + simp only [fl4PerformInvocation] + rw [ExceptT.run_bind] + exact ⟨(.ok u, s), hz, Set.mem_singleton _⟩ + have hV : ∀ z ∈ ((do + fl4InvokeUntyped (Z := Unit) i + match i with + | .retype _ _ _ retypeBase newType objSz slots _ => + NonDetE.liftE (NonDet.when (newType = ApiObjectType.archObject AObjectType.vcpuObj) + (NonDet.mapMx (fun v => setVcpu v (initVcpu virtKind)) + (retypeAddrs fl4ObjBitsApi retypeBase slots.length objSz newType))) + : PMonad Unit Unit).run σ).1, CQ ct z.2 := by + refine post_bindE (R := CQ ct) (Q := CQ ct) nat' (fun _ h => h) (fun _ s' h z hz => ?_) + have h' : CQ ct s' := nat' _ h + cases i with + | retype _ _ _ retypeBase newType objSz slots _ => + refine post_liftE (pq_post (pq_when _ ?_) h') z hz + refine pq_of (dk_mapMx (fun v => ?_) _) (PPNQ.qk_mapMx (fun v => ?_) _) + · unfold setVcpu; exact kf_setArchObj _ _ + · unfold setVcpu; exact PPNQ.qk_setObject' _ _ + refine post_bindE (R := CQ ct) (Q := CQ ct) hV (fun _ h => h) (fun _ s' h z hz => ?_) y hy + rw [NonDetE.returnOk, ExceptT.run_pure] at hz + rw [Set.mem_singleton_iff.1 hz]; exact hV _ h + case invokeNotification ep badge => + simp only [fl4PerformInvocationVF, withoutPreemption] + have h1 := post_liftE (ε := Unit) (fun x hx => pq_sendSignalVF onCore remoteCur ep badge σ x hs hq hnr hx) + refine post_bindE (R := CQ ct) h1 (fun _ h => h) fun _ s' hm y hy => ?_ + rw [NonDetE.returnOk, ExceptT.run_pure] at hy + rw [Set.mem_singleton_iff.1 hy] + exact h1 _ hm + case invokeTCB _ => exact absurd hf id + case invokeCNode _ => exact absurd hf id + case invokeArchObject i => + cases i with + | frameBatch bi => + simp only [fl4PerformInvocationVF] + exact pqe_post (Fl4Proofs.P3.Vs.dke_liftE (kf_performFrameBatchVF bi)) + (Fl4Proofs.P3.Vs.dke_liftE (qv_performFrameBatchVF bi)) hs + | _ => simp only [fl4PerformInvocationVF]; exact nat + all_goals (simp only [fl4PerformInvocationVF]; exact nat) + +/-! ## The shell -/ + +theorem sts_restart_notRvm {t : ObjRef} {σ : State Unit} {x : Unit × State Unit} + (hx : x ∈ (setThreadState (Z := Unit) t .restart σ).1) : NotRvm t x.2 := by + unfold setThreadState at hx + obtain ⟨u, hu, hx'⟩ := hx + obtain ⟨x0, -, hu2⟩ := SInv.mem_threadSet hu + have hk : x.2.kheap t = some (.tcb { x0 with tcbState := .restart }) := + SInv.setThreadStateAct_kheap t (P := fun k => k t = some (.tcb { x0 with tcbState := .restart })) u.2 + (by rw [hu2]; simp) x hx' + intro y hy + rw [hk] at hy + cases hy + simp + +end + +set_option maxHeartbeats 1000000 in +/-- **The extended shell with `CQ`**: from `CQ ∧ NQ` at the entry, every outcome of the first-phase non-blocking +non-calling extended invocation is `CQ`. -/ +theorem midRunQ_shellVF (onCore remoteCur : ObjRef → Bool) (rep don : Bool) (cptr : CapRef) (σ : State Unit) + (hs : CQ σ.curThread σ) (hq : NQ σ.curThread σ) (hW : JMidPerfHypQ don cptr σ) : + ∀ y ∈ ((fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep false false don true cptr).run σ).1, + CQ σ.curThread y.2 := by + intro y hy + unfold fl4HandleInvocationVF at hy + obtain ⟨z, hz, ⟨t, ht, hy⟩ | ⟨e, he, -⟩⟩ := mem_bindE_inv' hy + swap + · obtain ⟨w, -, rfl⟩ := mem_liftE_inv' hz; cases he + obtain ⟨w, hw, rfl⟩ := mem_liftE_inv' hz + obtain rfl : w = (σ.curThread, σ) := hw + simp only [Except.ok.injEq] at ht + subst ht + obtain ⟨z, hz, ⟨info, hi, hy⟩ | ⟨e, he, -⟩⟩ := mem_bindE_inv' hy + swap + · obtain ⟨w, -, rfl⟩ := mem_liftE_inv' hz; cases he + obtain ⟨⟨info', σa⟩, hia, rfl⟩ := mem_liftE_inv' hz + simp only [Except.ok.injEq] at hi + subst hi + have hIa := fq_I (fv_getMessageInfo (ct := σ.curThread) _) (qv_getMessageInfo' _) ⟨hs, hq⟩ _ hia + refine syscall_post (mF := midF σ.curThread info' cptr) (mE := midEV σ.curThread info' cptr) + (I := fun s => CQ σ.curThread s ∧ NQ σ.curThread s) (Q := CQ σ.curThread) + (fun w hw => fq_I (fve_midF _ _ _) (qve_midF _ _ _) hIa w hw) ?_ + (fun a s has w hw => fq_I (fve_midEV _ _ _ a) (qve_midEV _ _ _ a) + (fq_I (fve_midF _ _ _) (qve_midF _ _ _) hIa _ has) w hw) ?_ ?_ y hy + · intro f s hI v hv + simp only [NonDet.when, Bool.false_eq_true, ↓reduceIte] at hv + obtain rfl : v = ((), s) := hv + exact hI.1 + · intro e s hI v hv + simp only [NonDet.when, Bool.false_eq_true, ↓reduceIte] at hv + obtain rfl : v = ((), s) := hv + exact hI.1 + · intro a s b s' has hbs hI y hy + have hout := midEV_out hbs + refine post_bindE + (R := fun s => CQ σ.curThread s ∧ NQ σ.curThread s ∧ NotRvm σ.curThread s ∧ + ((), s) ∈ (setThreadState (Z := Unit) σ.curThread .restart s').1) ?_ (fun _ h => h.1) ?_ y hy + · unfold withoutPreemption + refine post_liftE (Q := fun s => CQ σ.curThread s ∧ NQ σ.curThread s ∧ NotRvm σ.curThread s ∧ + ((), s) ∈ (setThreadState (Z := Unit) σ.curThread .restart s').1) fun x hx => ?_ + exact ⟨pq_setThreadState_run _ _ rfl s' x hI.1 hx, NQ.of_nV (nv_setThreadState _ _ s' x hx) hI.2, + sts_restart_notRvm hx, hx⟩ + intro u s1 h1 y hy + obtain ⟨⟨u', s1'⟩, h1', e1⟩ := mem_liftE_inv' h1 + simp only [Prod.mk.injEq] at e1 + obtain ⟨-, rfl⟩ := e1 + have hI1 : CQ σ.curThread s1 ∧ NQ σ.curThread s1 := + ⟨pq_setThreadState_run _ _ rfl s' _ hI.1 h1', NQ.of_nV (nv_setThreadState _ _ s' _ h1') hI.2⟩ + have hnr : NotRvm σ.curThread s1 := sts_restart_notRvm h1' + have hP : ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur false false don b).run s1).1, + CQ σ.curThread y.2 := + perf_firstQVF onCore remoteCur don b hout.1 hI1.1 hI1.2 hnr fun b0 e hw => + hW b0 s1 ⟨info', σa, a, s, s', hia, has, hout.2 b0 e, h1'⟩ hw + refine post_bindE (R := CQ σ.curThread) hP (fun _ h => h) ?_ y hy + intro r s2 h2 y hy + have hs2 : CQ σ.curThread s2 := hP (.ok r, s2) h2 + unfold withoutPreemption at hy + refine post_liftE (pq_post ?_ hs2) y hy + refine pq_gts_bind _ fun ts _ => ?_ + split + · refine pq_bind (pq_of (dk_pure _) (PPNQ.qk_pure _)) fun _ => ?_ + exact pq_setThreadState_run _ _ rfl + · exact pq_of (dk_pure _) (PPNQ.qk_pure _) + +end + +end Fl4Proofs.P3.MidRun diff --git a/proofs/Fl4Proofs/P3/VmMidVfh.lean b/proofs/Fl4Proofs/P3/VmMidVfh.lean new file mode 100644 index 000000000..652114e4e --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmMidVfh.lean @@ -0,0 +1,422 @@ +/- +P3 (line mmuAD, Stage 3, `VMidF` producer, plan part (iii), `vfh`; spec only): **every TCB's fault handler stays +valid across the first-phase extended invocation** (`vfh_handleInvocationVF`: P3/SpecInvVfhE's +`vfh_handleInvocationR` for `fl4HandleInvocationVF` with `firstPhase`). + +* `vs_of_uk`: a step keeping the untyped view keeps `VS σ0` (`VfhV` and `CnDom` read caps only). +* The VM arms of the extended dispatcher: the VMEnter signal and every VCPU invocation but EnableIOPort keep the + untyped view; EnableIOPort and the EPT invocations write arch caps over slots holding arch caps (the VM decoders' + facts, P3/VmFinDecode), no fault-handler slot (`vs_performVcpuInvocationF`, `vs_performEptInvocation`). +* `vs_performFrameBatchVF` (the native `vs_performFrameBatch` over P3/VmFinFbV's entry). +* The base arms are the native `vfh_fl4Perform` (the Untyped's VCPU initialisation keeps the untyped view). + +No named premise. New file (spec only). +-/ +import Fl4Proofs.P3.VmMidRunQ +import Fl4Proofs.P3.VmFinArmVgC +import Fl4Proofs.P3.SpecInvVfhE +open Classical + +namespace Fl4Proofs.P3.SInv +open Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.SInvU Fl4Proofs.P3.Vs + +noncomputable section + +set_option maxHeartbeats 4000000 + +theorem vs_of_view {σ0 σ σ' : State Unit} (e : uV σ' = uV σ) (h : VS σ0 σ) : VS σ0 σ' := by + have hc : ∀ p l, capOpt (σ'.kheap p) l = capOpt (σ.kheap p) l := fun p l => by + have e1 := congrArg (fun w : UView => w.2.1 (p, l)) e + simp only [uV] at e1 + rw [vfh_capAt, vfh_capAt] at e1 + exact e1 + refine ⟨fun p c h3 h6 => ?_, fun p l hl => h.2.1 p l ?_, cnDom_of_caps hc h.2.2⟩ + · rw [hc] at h3 h6; exact h.1 p c h3 h6 + · rw [hc] at hl; exact hl + +theorem vs_of_uk {σ0 : State Unit} {α : Type} {m : NonDet (State Unit) α} (h : ∀ v, UK v m) : IK (VS σ0) m := + fun σ hσ y hy => vs_of_view (h (uV σ) σ rfl y hy) hσ + +theorem vfc_of_view {σ σ' : State Unit} (e : uV σ' = uV σ) (h : VfC σ) : VfC σ' := + (vs_of_view e (VS.refl h)).cur + +/-! ## The VM arms -/ + +theorem uk_performVcpuInvocationF_ne {v : UView} (i : VcpuInvocation) + (h : ∀ w lo hi c sl, i ≠ .enableIoPort w lo hi c sl) : UK v (performVcpuInvocationF (Z := Unit) i) := by + cases i with + | enableIoPort w lo hi c sl => exact absurd rfl (h w lo hi c sl) + | setTcb w t' => + unfold performVcpuInvocationF associateVcpuTcbF + dsimp only + refine ik_bind ?_ fun _ => ik_pure _ + refine ik_bind (Fl4Proofs.P3.VmFin.uk_vcpuDetachF _) fun _ => ?_ + refine ik_bind (Fl4Proofs.P3.VmFin.uk_getVcpuV _) fun st => ?_ + cases st.tcb with + | none => exact Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _ + | some old => exact ik_bind (Fl4Proofs.P3.VmFin.uk_vcpuDetachF _) fun _ => Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _ + | _ => + unfold performVcpuInvocationF + dsimp only + repeat' (first + | exact ik_pure _ + | exact Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _ + | exact Fl4Proofs.P3.VmFin.uk_getVcpuV _ + | refine ik_bind ?_ fun _ => ?_) + +theorem vs_performVcpuInvocationF (i : VcpuInvocation) (σ : State Unit) (hv : VfC σ) (h : VcpuOK σ i) : + ∀ y ∈ (performVcpuInvocationF (Z := Unit) i σ).1, VS σ y.2 := by + intro y hy + have h0 : VS σ σ := VS.refl hv + by_cases he : ∃ w lo hi c sl, i = .enableIoPort w lo hi c sl + · obtain ⟨w, lo, hi, c, slot, rfl⟩ := he + obtain ⟨_, cap, sl0, ex, hd, hl⟩ := h + obtain ⟨-, ⟨f, l, o, rfl⟩, rest, e⟩ := hd + have hc : SInvU.capAt σ slot = some (.archObjectCap (.ioPortCap f l o)) := + hl (_, _) (List.mem_cons_of_mem _ (by rw [e]; exact List.mem_cons_self ..)) + unfold performVcpuInvocationF at hy + dsimp only at hy + refine (?_ : IK (VS σ) (do + ioPortRevoke (Z := Unit) (.ioPortCap f l o) + ioPortSetOwner (.ioPortCap f l o) w slot + vcpuUpdate w (VcpuState.setPorts lo hi false) + pure [])) σ h0 y hy + refine ik_bind (vs_of_uk fun _ => Fl4Proofs.P3.VmFin.uk_ioPortRevoke _) fun _ => ?_ + refine ik_bind ?_ fun _ => ik_bind (vs_of_uk fun _ => Fl4Proofs.P3.VmFin.uk_vcpuUpdateV _ _) fun _ => ik_pure _ + simp only [ioPortSetOwner] + exact vs_setCap_nb (vfh_notBad_arch hv.1 hc) (vfh_arch_ok _) + · exact (vs_of_uk fun _ => uk_performVcpuInvocationF_ne i fun w lo hi c sl e => he ⟨w, lo, hi, c, sl, e⟩) σ h0 y hy + +theorem vs_performEptInvocation (i : EptInvocation) (σ : State Unit) (hv : VfC σ) (h : EptOK σ i) : + ∀ y ∈ (performEptInvocation (Z := Unit) i σ).1, VS σ y.2 := by + obtain ⟨cap, slot0, ex, hd, hl⟩ := h + have h0 := hl (cap, slot0) (List.mem_cons_self ..) + have h00 : VS σ σ := VS.refl hv + have hsge : ∀ p j e, IK (VS σ) (storeGuestEntry (Z := Unit) p j e) := fun _ _ _ => + vs_of_uk fun _ => Fl4Proofs.P3.VmFin.uk_storeGuestEntry _ _ _ + have hgu : ∀ a g p l t, IK (VS σ) (guestUnmap (Z := Unit) a g p l t) := fun _ _ _ _ _ => + vs_of_uk fun _ => Fl4Proofs.P3.VmFin.uk_guestUnmap _ _ _ _ _ + intro y hy + refine (?_ : IK (VS σ) (performEptInvocation (Z := Unit) i)) σ h00 y hy + cases i with + | tableMap ptr level slot tbl i asid gpa => + obtain ⟨rfl, rfl, -⟩ := hd + unfold performEptInvocation + exact ik_bind (hsge _ _ _) fun _ => vs_setCap_nb (vfh_notBad_arch hv.1 h0) (vfh_arch_ok _) + | tableUnmap ptr level mapped slot => + obtain ⟨rfl, rfl, -⟩ := hd + unfold performEptInvocation + cases mapped with + | none => exact ik_pure _ + | some p => + obtain ⟨asid, gpa⟩ := p + dsimp only + refine ik_bind (hgu _ _ _ _ _) fun _ => ?_ + refine ik_bind (vs_of_uk fun v => ik_of_keepSt (keepSt_getEpt ptr) _) fun e => ?_ + obtain ⟨_, _⟩ := e + dsimp only + refine ik_bind (vs_of_uk fun _ => Fl4Proofs.P3.VmFin.uk_setArch _ _) fun _ => ?_ + exact vs_setCap_nb (vfh_notBad_arch hv.1 h0) (vfh_arch_ok _) + | frameMap newCap slot tbl i entry => + obtain ⟨rfl, dev, p, R, mt, sz, mt', m', rfl, -⟩ := hd + unfold performEptInvocation + exact ik_bind (vs_setCap_nb (vfh_notBad_arch hv.1 h0) (vfh_arch_ok _)) fun _ => hsge _ _ _ + | frameUnmap c slot => + obtain ⟨rfl, rfl, dev, p, R, sz, m, rfl⟩ := hd + unfold performEptInvocation + dsimp only + cases m with + | none => exact ik_pure _ + | some q => + obtain ⟨asid, gpa⟩ := q + dsimp only + refine ik_bind ?_ fun _ => vs_setCap_nb (vfh_notBad_arch hv.1 h0) (vfh_arch_ok _) + unfold NonDet.when + split + · exact hgu _ _ _ _ _ + · exact ik_pure _ + | poolAssign asid pool slot => + obtain ⟨-, r, rest, e⟩ := hd + have hc : SInvU.capAt σ slot = some (.archObjectCap (.eptCap r 0 none)) := + hl (_, _) (List.mem_cons_of_mem _ (by rw [e]; exact List.mem_cons_self ..)) + unfold performEptInvocation + refine ik_bind (vs_of_uk fun _ => by uk_auto) fun c => ?_ + split + · exact ik_bind (vs_setCap_nb (vfh_notBad_arch hv.1 hc) (vfh_arch_ok _)) fun _ => + vs_of_uk fun _ => uk_storeAsidPoolEntry _ _ _ + · exact ik_fail + +/-- **The extended frame batch keeps `VS σ0`**, given the decode's `CnOk` of the destination CNode cap +(`vs_performFrameBatch` over `fbEntryXV`). -/ +theorem vs_performFrameBatchVF {σ0 : State Unit} (b : Fl4FrameBatchInvocation) (hcn : CnOk σ0 b.destCnode) : + IK (VS σ0) (performFrameBatchVF b) := + ik_performFrameBatchXV (fun w => ik_fbEntryXV_of b w fbx_lookupTargetSlot fbx_lookupSourceSlot fbx_ensureEmpty + fbx_deriveCap + (fun s c src dest _ hs hc hdest _ _ _ _ => by + have hf := (vfh_lookupSlotForCnodeOp s hs.2.2 false b.destCnode _ _ (hcn.mono hs.2.1) s rfl dest s + (by unfold lookupTargetSlot at hdest; exact hdest)).2 + exact vs_pin hs (vs_capInsert src dest (fun hb => (vfh_notBad_cnFull hf hb).elim) + (cnOk_of_nc fun p r g e => by subst e; exact hc))) + (fun sl v => ik_fbMapF2 b sl v (fun _ _ _ _ _ _ => fbx_decodePageInvocation _ _ _ _ _) + (fun s pi dev p R mt sz m hs hok hpg hcap x hx => vs_pin hs (vs_performPageInvocation pi + (fun c sl0 e v h => by + subst h + simp only [PgSl] at hpg; subst hpg + obtain ⟨c0, hc0, hre, -⟩ := hok + have hc0' : SInvU.capAt s sl0 = some c0 := hc0 + rw [hcap] at hc0'; cases hc0' + exact ⟨vfh_notBad_arch hs.1 hcap, vfh_cnOk_refEq_frame hre⟩) + (fun c sl0 h => by + subst h + exact vfh_notBad_arch hs.1 hok)) x hx)) + (fun sl => ik_fbDelXF sl (fun _ => fbx_isFinalCap _) (fun c x => by hv_auto) + (fun s _ _ _ _ _ _ hs _ x hx => vs_emptySlot sl .nullCap σ0 s hs x hx)) + (fun sl => ik_fbDelXVF sl (fun _ => fbx_isFinalCap _) + (fun _ _ _ _ _ _ _ => vs_of_uk fun _ => uk_finaliseCapVF_frame _ _ _ _ _ _ _) + (fun _ _ _ _ _ _ _ _ => uk_finaliseCapVF_frame _ _ _ _ _ _ _) + (fun s _ _ _ _ _ _ hs _ x hx => vs_emptySlot sl .nullCap σ0 s hs x hx)) + (fun _ _ _ _ => by unfold unmapPageUnflushed; hv_auto) + (fun s _ _ _ _ _ _ sl hs hc0 x hx => vs_pin hs (vs_setCap_nb (vfh_notBad_arch hs.1 hc0) (vfh_arch_ok _)) x hx)) + (by hv_auto) + +/-- **Every first-phase extended perform keeps `VfC`** from the decode's facts. -/ +theorem vfh_fl4PerformVF (onCore remoteCur : ObjRef → Bool) (blk call don : Bool) (inv : Fl4InvocationV) + (hf : Fl4Proofs.P3.MidRun.FirstInvV inv) (σ0 σ : State Unit) (hv : VfC σ) (hU : UtInv'' σ) + (hbase : ∀ b, inv = .base b → SInvV.hV σ = SInvV.hV σ0 ∧ InvOKV (uV σ) b ∧ VInv σ0 b) + (hept : ∀ i, inv = .ept i → EptOK σ i) (hvc : ∀ i, inv = .vcpu i → VcpuOK σ i) : + ∀ y ∈ ((fl4PerformInvocationVF (Z := Unit) onCore remoteCur blk call don inv).run σ).1, VfC y.2 := by + intro y hy + cases inv with + | vcpu i => + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + exact (vs_performVcpuInvocationF i σ hv (hvc i rfl) x hx).cur + | ept i => + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + obtain ⟨z, hz, hx⟩ := hx + rw [Set.mem_singleton_iff.1 hx] + exact (vs_performEptInvocation i σ hv (hept i rfl) z hz).cur + | setEptRoot _ => exact absurd hf id + | base b => + obtain ⟨e, hok, hi⟩ := hbase b rfl + have nat : ∀ y ∈ ((fl4PerformInvocation (Z := Unit) blk call don b).run σ).1, VfC y.2 := + vfh_fl4Perform blk call don b σ0 σ e hv.1 hv.2 hU hok hi + cases b + case invokeUntyped ui => + simp only [fl4PerformInvocationVF] at hy + unfold fl4InvokeUntypedV at hy + have nat' : ∀ z ∈ ((fl4InvokeUntyped (Z := Unit) ui).run σ).1, VfC z.2 := by + intro z hz + obtain ⟨r, s⟩ := z + cases r with + | error e' => + refine nat (.error e', s) ?_ + simp only [fl4PerformInvocation] + rw [ExceptT.run_bind] + exact ⟨(.error e', s), hz, Set.mem_singleton _⟩ + | ok u => + refine nat (.ok [], s) ?_ + simp only [fl4PerformInvocation] + rw [ExceptT.run_bind] + exact ⟨(.ok u, s), hz, Set.mem_singleton _⟩ + have hV : ∀ z ∈ ((do + fl4InvokeUntyped (Z := Unit) ui + match ui with + | .retype _ _ _ retypeBase newType objSz slots _ => + NonDetE.liftE (NonDet.when (newType = ApiObjectType.archObject AObjectType.vcpuObj) + (NonDet.mapMx (fun v => setVcpu v (initVcpu virtKind)) + (retypeAddrs fl4ObjBitsApi retypeBase slots.length objSz newType))) + : PMonad Unit Unit).run σ).1, VfC z.2 := by + refine Fl4Proofs.P3.MidRun.post_bindE (R := VfC) (Q := VfC) nat' (fun _ h => h) (fun _ s' h z hz => ?_) + have h' : VfC s' := nat' _ h + cases ui with + | retype _ _ _ retypeBase newType objSz slots _ => + obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + refine ((vs_of_uk fun _ => ?_) s' (VS.refl h') x hx).cur + unfold NonDet.when + split + · exact ik_mapMx (fun _ => uk_setVcpuV _ _) _ + · exact ik_pure _ + refine Fl4Proofs.P3.MidRun.post_bindE (R := VfC) (Q := VfC) hV (fun _ h => h) (fun _ s' h z hz => ?_) y hy + rw [NonDetE.returnOk, ExceptT.run_pure] at hz + rw [Set.mem_singleton_iff.1 hz]; exact hV _ h + case invokeNotification ep badge => + simp only [fl4PerformInvocationVF, withoutPreemption] at hy + obtain ⟨z, hz, e'⟩ := mem_ret_bindE hy + rw [e'] + obtain ⟨x, hx, rfl⟩ := mem_liftE' hz + exact ((vs_of_uk fun _ => uk_sendSignalVF onCore remoteCur ep badge) σ (VS.refl hv) x hx).cur + case invokeTCB _ => exact absurd hf id + case invokeCNode _ => exact absurd hf id + case invokeArchObject i => + cases i with + | frameBatch bi => + simp only [fl4PerformInvocationVF] at hy + obtain ⟨x, hx, rfl⟩ := mem_liftE' hy + have hcn : CnOk σ0 bi.destCnode := hi + have hcn' : CnOk σ bi.destCnode := hcn.mono (domK_view e) + exact (vs_performFrameBatchVF bi hcn' σ (VS.refl hv) x hx).cur + | _ => simp only [fl4PerformInvocationVF] at hy; exact nat y hy + all_goals (simp only [fl4PerformInvocationVF] at hy; exact nat y hy) + +/-! ## The shell -/ + +theorem fke_of_ike {β ε α : Type} {f : State Unit → β} {m : NonDetE (State Unit) ε α} + (h : ∀ v, IKE (fun s => f s = v) m) : FKE f m := fun σ x hx => h (f σ) σ rfl x hx + +theorem uk_midEV (t : ObjRef) (info : MessageInfo) (cptr : CapRef) (a) (v : UView) : + UKE v (Fl4Proofs.P3.MidRun.midEV t info cptr a) := by + obtain ⟨sl, cap, ex, buf⟩ := a + have h : FKE uV (Fl4Proofs.P3.MidRun.midEV t info cptr (sl, cap, ex, buf)) := by + unfold Fl4Proofs.P3.MidRun.midEV + refine dke_bind (fke_of_ike fun _ => ike_liftE (uk_getMrs _ _ _)) fun args => ?_ + exact Fl4Proofs.P3.MidRun.fke_decodeFirstV _ _ _ _ _ _ _ + (fke_of_ike fun _ => by uk_auto) + exact fun σ hσ y hy => (h σ y hy).trans hσ + +set_option maxHeartbeats 8000000 in +theorem vfh_handleInvocationVF (onCore remoteCur : ObjRef → Bool) (rep calling blocking don : Bool) + (cptr : CapRef) : + IKVE (fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep calling blocking don true cptr) := by + unfold fl4HandleInvocationVF + refine ikve_bind_uk (fun _ => by uk_auto) (by vfc_auto) fun thread => + ikve_bind_uk (fun _ => by uk_auto) (by vfc_auto) fun info => ?_ + intro σ hI hU y hy + have hF : ∀ v, UKE v (do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot (Z := Unit) thread cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false thread + let extracaps ← lookupExtraCaps thread buffer info + NonDetE.returnOk (slot, cap, extracaps, buffer)) := fun _ => by uk_auto + have hFv : IKE VfC (do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot (Z := Unit) thread cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false thread + let extracaps ← lookupExtraCaps thread buffer info + NonDetE.returnOk (slot, cap, extracaps, buffer)) := by vfc_auto + have hOk : OkV (uV σ) (fun r : CslotPtr × Cap × List (Cap × CslotPtr) × Option ObjRef => + (uV σ).2.1 r.1 = some r.2.1 ∧ ∀ p ∈ r.2.2.1, (uV σ).2.1 p.2 = some p.1) (do + let (cap, slot) ← capFaultOnFailure (ofBl cptr) false <| lookupCapAndSlot (Z := Unit) thread cptr + let buffer ← NonDetE.liftE <| lookupIpcBuffer false thread + let extracaps ← lookupExtraCaps thread buffer info + NonDetE.returnOk (slot, cap, extracaps, buffer)) := by + refine okv_bind (fun _ => uk_capFaultOnFailure _ _ _ (uk_lookupCapAndSlot _ _)) + (okv_capFaultOnFailure _ _ (okv_lookupCapAndSlot _ _)) fun x hx => ?_ + obtain ⟨cap, slot⟩ := x + dsimp only + refine okv_bind_uk (fun _ => ike_liftE (uk_lookupIpcBuffer _ _)) fun buf => ?_ + refine okv_bind (fun _ => uk_lookupExtraCaps _ _ _) (okv_lookupExtraCaps _ _ _) fun ex hex => okv_returnOk ?_ + exact ⟨hx, hex⟩ + refine syscall_postU (I := fun s => VfC s ∧ uV s = uV σ) (Q := VfC) + (fun w hw => ⟨hFv σ hI w hw, hF _ σ rfl w hw⟩) ?_ ?_ ?_ ?_ y hy + · intro f s hs v hv + exact (show IK VfC (NonDet.when blocking (handleFault thread f)) by vfc_auto) s hs.1 v hv + · intro a s has w hw + have hs : VfC s ∧ uV s = uV σ := ⟨hFv σ hI _ has, hF _ σ rfl _ has⟩ + have e : uV w.2 = uV s := uk_midEV thread info cptr a (uV s) s rfl w hw + exact ⟨vfc_of_view e hs.1, e.trans hs.2⟩ + · intro e s hs v hv + exact (by vfc_auto : IK VfC _) s hs.1 v hv + · intro a s b s' has hbs hI' y hy + obtain ⟨sl, cap, ex, buf⟩ := a + have hvs : uV s = uV σ := hF _ σ rfl _ has + have hs : VfC s := hFv σ hI _ has + obtain ⟨hc, hex⟩ := hOk σ rfl _ s has + have hcap : capOpt (s.kheap sl.1) sl.2 = some cap := by + rw [← vfh_capAt]; show (uV s).2.1 sl = some cap; rw [hvs]; exact hc + have hexs : ∀ p ∈ ex, capOpt (s.kheap p.2.1) p.2.2 = some p.1 := by + intro p hp + rw [← vfh_capAt]; show (uV s).2.1 p.2 = some p.1; rw [hvs]; exact hex p hp + have hss : uV s' = uV s := uk_midEV thread info cptr (sl, cap, ex, buf) (uV s) s rfl _ hbs + have hbs' : ((.ok b, s') : Except SyscallError _ × State Unit) ∈ + ((Fl4Proofs.P3.MidRun.midEV thread info cptr (sl, cap, ex, buf)).run s).1 := hbs + have hout := Fl4Proofs.P3.MidRun.midEV_out hbs' + have hbase : ∀ b0, b = .base b0 → SInvV.hV s' = SInvV.hV s ∧ InvOKV (uV σ) b0 ∧ VInv s b0 := by + intro b0 e0 + have hn := hout.2 b0 e0 + have hdec : InvOKV (uV σ) b0 := by + have := okv_bind_uk (P := InvOKV (uV σ)) (fun _ => ike_liftE (uk_getMrs _ _ _)) (fun args => + okv_fl4Decode (w := uV σ) true info.miLabel args cptr sl cap ex buf hc hex) s hvs b0 s' hn + exact this + have hvi : SInvV.hV s' = SInvV.hV s ∧ VInv s b0 := + SInvV.okv_bind (Q := fun _ => True) (SInvV.okv_frame fun v => by hv_auto) + (fun args _ => vfh_okv_fl4Decode s hs.1 hs.2 true info.miLabel args cptr sl cap ex buf hcap hexs) + s rfl b0 s' hn + exact ⟨hvi.1, hdec, hvi.2⟩ + have hdm : Fl4Proofs.P3.VmFin.VmDec cap sl ex b := by + have h := hbs' + unfold Fl4Proofs.P3.MidRun.midEV at h + obtain ⟨z, hz, ⟨args, ha, h⟩ | ⟨e, he, h'⟩⟩ := mem_bindE' h + · exact Fl4Proofs.P3.VmFin.fl4DecodeInvocationV_vmDec _ _ _ _ _ _ _ _ _ _ _ h + · first | (cases h'; done) | (subst h'; cases he) | (rw [h'] at he; cases he) + have hU' : UtInv'' s' := utInv''_of_view hI'.2 hU + have hTS : ∀ v, UKE v (withoutPreemption (Z := Unit) (setThreadState thread .restart)) := fun _ => by uk_auto + have hTSh : ∀ v, SInv.IKE (fun x => SInvV.hV x = v) (withoutPreemption (Z := Unit) (setThreadState thread .restart)) := + fun _ => by hv_auto + have hTSv : IKE VfC (withoutPreemption (Z := Unit) (setThreadState thread .restart)) := by vfc_auto + obtain ⟨z, hz, ⟨u, hu, hy⟩ | ⟨e, -, rfl⟩⟩ := mem_bindE' hy + · have hz' : uV z.2 = uV s' := hTS _ s' rfl z hz + have hzh : SInvV.hV z.2 = SInvV.hV s' := hTSh _ s' rfl z hz + have hzv : VfC z.2 := hTSv s' hI'.1 z hz + obtain ⟨r, s1⟩ := z + simp only at hu; subst hu + have hv1 : uV s1 = uV σ := hz'.trans hI'.2 + have hv1s : uV s1 = uV s := hz'.trans hss + have hcaps : ∀ p ∈ (cap, sl) :: ex, SInvU.capAt s1 p.2 = some p.1 := by + intro p hp + have e1 : SInvU.capAt s1 p.2 = SInvU.capAt s p.2 := congrArg (fun w : UView => w.2.1 p.2) hv1s + rw [e1, vfh_capAt] + rcases List.mem_cons.1 hp with rfl | hp + · exact hcap + · exact hexs p hp + have hperf := vfh_fl4PerformVF onCore remoteCur blocking calling don b hout.1 s s1 hzv + (utInv''_of_view hz' hU') + (fun b0 e0 => by + obtain ⟨h1, h2, h3⟩ := hbase b0 e0 + exact ⟨hzh.trans h1, by rw [hv1]; exact h2, h3⟩) + (fun i e0 => by + subst e0 + exact ⟨cap, sl, ex, hdm, hcaps⟩) + (fun i e0 => by + subst e0 + obtain ⟨v, -, hd⟩ := hdm + exact ⟨v, cap, sl, ex, hd, hcaps⟩) + obtain ⟨z2, hz2, ⟨rp, hr, hy⟩ | ⟨e, -, rfl⟩⟩ := mem_bindE' hy + · obtain ⟨r2, s2⟩ := z2 + simp only at hr; subst hr + exact (by vfc_auto : IKE VfC _) s2 (hperf _ hz2) y hy + · exact hperf _ hz2 + · exact hTSv s' hI'.1 z hz + +end + +end Fl4Proofs.P3.SInv + +namespace Fl4Proofs.P3.SInvG +open Fl4Spec Fl4Proofs.P3.SInv + +noncomputable section + +variable {N : ObjRef} + +theorem idk_midEV_decode (label : Data) (args : List Data) (ci : CapRef) (sl : CslotPtr) (cap : Cap) + (ex : List (Cap × CslotPtr)) (buf : Option ObjRef) : + IdKE N (fl4DecodeInvocationV (Z := Unit) true label args ci sl cap ex buf) := by + have h : Fl4Proofs.P3.Vs.FKE (fun s : State Unit => s.idleThread) + (fl4DecodeInvocationV (Z := Unit) true label args ci sl cap ex buf) := + Fl4Proofs.P3.MidRun.fke_decodeFirstV _ _ _ _ _ _ _ fun σ y hy => + idk_fl4DecodeInvocation (N := σ.idleThread) _ _ _ _ _ _ _ _ σ rfl y hy + exact fun σ hσ y hy => (h σ y hy).trans hσ +macro_rules | `(tactic| idk_leaf) => `(tactic| with_reducible exact idk_midEV_decode _ _ _ _ _ _ _) +macro_rules | `(tactic| idk_leaf) => `(tactic| with_reducible exact Fl4Proofs.P3.SInvU.idk_fl4PerformInvocationVF _ _ _ _ _ _) + +set_option maxHeartbeats 4000000 in +/-- **The extended first-phase invocation handler keeps the idle thread.** -/ +theorem idk_fl4HandleInvocationVF (onCore remoteCur : ObjRef → Bool) (rep calling blocking don : Bool) + (cptr : CapRef) : + IdKE N (fl4HandleInvocationVF (Z := Unit) onCore remoteCur rep calling blocking don true cptr) := by + unfold fl4HandleInvocationVF + idk_auto + +end + +end Fl4Proofs.P3.SInvG diff --git a/proofs/Fl4Proofs/P3/VmPerfCur.lean b/proofs/Fl4Proofs/P3/VmPerfCur.lean index 54ceb9633..4ddd6a1ab 100644 --- a/proofs/Fl4Proofs/P3/VmPerfCur.lean +++ b/proofs/Fl4Proofs/P3/VmPerfCur.lean @@ -23,6 +23,7 @@ No named premise. No existing statement changed. New file (spec only). import Fl4Proofs.P3.SpecPerfCur import Fl4Proofs.P3.VmDelFinSpec import Fl4Proofs.P3.DelVF +import Fl4Proofs.P3.SpecInvFbK open Aeneas Aeneas.Std Result namespace Fl4Proofs.P3.Vs @@ -176,6 +177,24 @@ theorem perfCurKeepVF (onCore remoteCur : ObjRef → Bool) (blk call don : Bool) dke_returnOk _) case invokeTCB i => exact DKE.ctK (dke_invokeTcbVF i) case invokeCNode i => exact DKE.ctK (dke_bind (dke_invokeCnodeVF i) fun _ => dke_returnOk _) + case invokeArchObject i => + cases i with + | frameBatch b => + apply DKE.ctK (dke_liftE ?_) + intro s y hy + have entry (w : Data × Data × Data) : SInv.IKE (fun t => schedF t = schedF s) + (fbEntryWith (Z := Unit) capDeleteVF b w) := + SInv.ik_fbEntryWith b w + (fun _ t ht x hx => (dk_fbEntry b w t x hx).trans ht) + (fun t ht x hx => (dk_lookupTargetSlot' _ _ _ t x hx).trans ht) + (fun sl t ht x hx => (dk_getCap sl t x hx).trans ht) + (fun sl dev p R mt sz m => SInv.ik_fbDeferDelete b sl dev p R mt sz m + (fun va t ht x hx => (dk_unmapPageUnflushed sz b.vspace va p t x hx).trans ht) + (fun t ht x hx => (dk_setCap _ sl t x hx).trans ht)) + (fun sl t ht x hx => (dk_capDeleteVF sl t x hx).trans ht) + exact SInv.ik_performFrameBatchWith entry + (fun t ht x hx => (dk_flushAll b.vspace b.asid t x hx).trans ht) s rfl y hy + | _ => exact fun σ x hx => perfCurKeep blk call don _ σ x hx all_goals exact fun σ x hx => perfCurKeep blk call don _ σ x hx | vcpu i => exact DKE.ctK (dke_liftE (dk_performVcpuInvocationF i)) | ept i => exact DKE.ctK (dke_liftE (dk_bind (dk_performEptInvocation i) fun _ => dk_pure _)) diff --git a/proofs/Fl4Proofs/P3/VmPerfRvm.lean b/proofs/Fl4Proofs/P3/VmPerfRvm.lean index f0f5ac06c..07b31a7e8 100644 --- a/proofs/Fl4Proofs/P3/VmPerfRvm.lean +++ b/proofs/Fl4Proofs/P3/VmPerfRvm.lean @@ -180,6 +180,15 @@ theorem oi_fl4PerformInvocationVF (onCore remoteCur : ObjRef → Bool) (blk call exact ike_bind (ike_liftE (oi_sendSignalVF onCore remoteCur n b)) fun _ => ike_returnOk _ case invokeTCB i => exact oi_invokeTcbVF i case invokeCNode i => exact ike_bind (oi_invokeCnodeVF i) fun _ => ike_returnOk _ + case invokeArchObject i => + cases i with + | frameBatch b => + apply ike_liftE (ik_performFrameBatchWith ?_ (oi_flushAll _ _)) + intro w + exact ik_fbEntryWith b w (fun _ => oi_fbEntry b w) (oi_lookupTargetSlot _ _ _) oi_getCap + (fun _ _ _ _ _ _ _ => ik_fbDeferDelete _ _ _ _ _ _ _ _ + (fun _ => by unfold unmapPageUnflushed; orv_auto) (by orv_auto)) oi_capDeleteVF + | _ => exact oi_fl4PerformInvocation _ _ _ _ all_goals exact oi_fl4PerformInvocation _ _ _ _ | vcpu i => exact ike_liftE (oi_performVcpuInvocationF i) | ept i => exact ike_liftE (ik_bind (oi_performEptInvocation i) fun _ => ik_pure _) diff --git a/proofs/Fl4Proofs/P3/VmRecvFin.lean b/proofs/Fl4Proofs/P3/VmRecvFin.lean new file mode 100644 index 000000000..f569017ad --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFin.lean @@ -0,0 +1,192 @@ +import Fl4Proofs.P3.VmEntryS3Fault +import Fl4Proofs.P3.VmRecvS +import Fl4Proofs.P3.VmFinVF +import Fl4Proofs.P3.VmFinishR +import Fl4Proofs.P3.SpecInvSiG +import Fl4Proofs.P3.SpecInvAllO6 +import Fl4Proofs.P3.SpecInvSgI6 +import Fl4Proofs.P3.SpecInvFoG6 +import Fl4Proofs.P3.XTopJointKEntryU13 + +/-! +P3 (line mmuAD, Stage 3, finish facts of `handle_recv`; spec side and the VM / R state records). + +* `recvFinFrames`: the spec frames of `handleRecv` the finish reads: `DomListOK`, `FpuOwnOK`, `RvmSub` (no thread becomes + `runningVM`), `IdleVtS` (the family bundle `VmFin.VG`) and the idle thread. +* `JAtZB.idleTcb`, `JAtZRB.idleTcb`, `JAtZRB.onCoreTcb`, `JAtZRB.vmOrcAcross`, `JAtZRB.kfinVE_ofFacts`: the R siblings of the + V finish-fact producers. +* `VmEptState.recvFinishFacts`: a `handleRecv` outcome with a `VEAnyS` post carries `FinishFactsS3`. +* `VEAnyS.finish`: FL4's `finish` at a `VEAnyS` state with its finish facts, against `fl4KernelExitVF`. + +No new named premise. [J] the entry-state finish facts (`FinishFactsS3`, `VmFin.VG`). [P] `P0.ArchTotal`, `FpuImplDet`, +`FpuWriteDet`, `SetDlDet`, the existing finish platform facts. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc + Fl4Proofs.P3.Sched Fl4Proofs.P3.SInvDom + +noncomputable section + +section Spec + +set_option maxHeartbeats 4000000 in +/-- **The spec frames of `handleRecv` the finish reads** (module docstring). -/ +theorem recvFinFrames (b c : Bool) {σ : State Unit} {z : Unit × State Unit} + (hz : z ∈ (handleRecv (Z := Unit) b c σ).1) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hG : VmFin.VG σ) : + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ RvmSub σ z.2 ∧ SInvIV.IdleVtS z.2 := by + refine ⟨domListOK_of_dlV (dl_handleRecv b c σ z hz) hDom, ?_, ?_, ?_⟩ + · obtain ⟨hOwn, hFpu'⟩ := SInvF.oi_handleRecv b c σ ⟨rfl, hFo⟩ z hz + exact fun o ho => hFpu' o (hOwn.symm.trans ho) + · exact SInvAll.oi_handleRecv + (N := ⟨.runningVM, fun o => ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM, Or.inr rfl⟩) + b c σ (fun _ x hp hs => ⟨x, hp, hs⟩) z hz + · exact SInvIV.idleVtS_step (SInvIV.iksi_of_ik fun N t => SInvIV.iw_handleRecv b c) + (fun K => SInvG.idk_handleRecv b c) hG.1 hG.2.1 hG.2.2.1 hG.2.2.2 z hz + +theorem recvFinIdle (b c : Bool) {σ : State Unit} {z : Unit × State Unit} + (hz : z ∈ (handleRecv (Z := Unit) b c σ).1) : z.2.idleThread = σ.idleThread := + SInvG.idk_handleRecv (N := σ.idleThread) b c σ rfl z hz + +set_option maxHeartbeats 4000000 in +/-- The spec frames of `handleYield` the finish reads (the `handleRecv` bundle's sibling). -/ +theorem yieldFinFrames {σ : State Unit} {z : Unit × State Unit} + (hz : z ∈ (handleYield (Z := Unit) σ).1) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hG : VmFin.VG σ) : + DomListOK z.2 ∧ FpuOwnOK z.2 ∧ RvmSub σ z.2 ∧ SInvIV.IdleVtS z.2 ∧ z.2.idleThread = σ.idleThread := by + refine ⟨domListOK_of_dlV (dl_handleYield σ z hz) hDom, ?_, ?_, ?_, ?_⟩ + · obtain ⟨hOwn, hFpu'⟩ := SInvF.oi_handleYield σ ⟨rfl, hFo⟩ z hz + exact fun o ho => hFpu' o (hOwn.symm.trans ho) + · exact SInvAll.oi_handleYield + (N := ⟨.runningVM, fun o => ∃ x, σ.kheap o = some (.tcb x) ∧ x.tcbState = .runningVM, Or.inr rfl⟩) + σ (fun _ x hp hs => ⟨x, hp, hs⟩) z hz + · exact SInvIV.idleVtS_step (SInvIV.iksi_of_ik fun N t => SInvIV.iw_handleYield) + (fun K => SInvG.idk_handleYield) hG.1 hG.2.1 hG.2.2.1 hG.2.2.2 z hz + · exact SInvG.idk_handleYield (N := σ.idleThread) σ rfl z hz + +end Spec + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] + {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} + {MR : A → Fl4Spec.MachineState → Prop} + {V X : Set U64} {XL : U64 → Nat} {Z : XSets} {H : P1.Heap} + {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} + {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The idle thread is a spec TCB at a `JAtZB` state. -/ +theorem JAtZB.idleTcb (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∃ x, σ.kheap σ.idleThread = some (.tcb x) := by + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hth := (hJ.tcb _).2 (J.zH.1 _ hc.idleT) + obtain ⟨x, hkx, -⟩ := J.xtop.rel.2.1.1.tcb _ hth + exact ⟨x, by rw [hs.idleTh]; exact hkx⟩ + +/-- The idle thread is a spec TCB at a `JAtZRB` state. -/ +theorem JAtZRB.idleTcb (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) : + ∃ x, σ.kheap σ.idleThread = some (.tcb x) := by + obtain ⟨Q, R, hc, hs⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hth := (hJ.tcb _).2 (J.zH.1 _ hc.idleT) + obtain ⟨x, hkx, -⟩ := J.xtop.rel.2.1.1.tcb _ hth + exact ⟨x, by rw [hs.idleTh]; exact hkx⟩ + +/-- A TCB covered by the joint scheduler relation is on the executing core (R state). -/ +theorem JAtZRB.onCoreTcb (B : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : Fl4Spec.ObjRef} {x : Fl4Spec.Tcb} (hk : σ.kheap t = some (.tcb x)) : + v3OnCore st s t = true := by + obtain ⟨u, hu, rfl⟩ := B.obj.tcb t x hk + obtain ⟨Q, R, hC, _⟩ := B.xtop.fin.1.1.1.lists + have e : ({ bv := u.bv } : U64) = u := by cases u; rfl + unfold v3OnCore + simp only [e, hC.aff u hu, hC.cur, decide_true] + +/-- `VmOrc` from a V state to an R state under `RvmSub`. -/ +theorem JAtZB.vmOrcAcrossR {Z' : XSets} {H' : P1.Heap} {Cs' Ac' D' : Set U64} {Lst' : U64 → List U64} + {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (B' : JAtZRB (L := L) arI AR Z' H' Cs' Ac' Lst' D' s' a' σ') + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) (hR : RvmSub σ σ') : + VmOrc st on rc s' σ' := by + intro t x hk hx + obtain ⟨y, hy, hry⟩ := hR t x hk hx + have h0 := (hO t y hy hry).1.trans (B.onCoreTcb hy) + exact ⟨h0.trans (B'.onCoreTcb hk).symm, fun h => by rw [h0] at h; cases h⟩ + +/-- The R sibling of `JAtZB.kfinVE_ofFacts`. -/ +theorem JAtZRB.kfinVE_ofFacts + (J : JAtZRB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hAT : P0.ArchTotal arI) (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hDom : DomListOK σ) (hFo : FpuOwnOK σ) (hIdle : IdleVt σ) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) : + KFinVE (L := L) arI on rc Z H Cs s σ := by + have hD : DomRel (L.mem s) (L.ksb s) σ := J.xtop.rel.2.2.2.1 + have hNd : NdOK (L := L) s σ := by + refine ⟨hD, hDom, ?_⟩ + rw [hD.2.2.2, hDom.idx0 hD.1]; rfl + exact ⟨Or.inr hNd, hO, ⟨archSwFT_of hFpu hFW hAT, archSwFI_of hAT⟩, + archNdF_of hFpu, hFo, hIdle⟩ + +variable (arI AR) + +/-- **The finish facts of a `handleRecv` outcome** (module docstring). -/ +theorem VmEptState.recvFinishFacts + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (b c : Bool) + {z : Unit × State Unit} (hz : z ∈ (handleRecv (Z := Unit) b c σ).1) + {s' : S} {a' : A} (hV' : VEAnyS (L := L) arI AR s' a' z.2) + {on rc : ObjRef → Bool} (hI : FinishFactsS3 (st := st) (L := L) on rc s σ) (hG : VmFin.VG σ) : + FinishFactsS3 (st := st) (L := L) on rc s' z.2 := by + obtain ⟨hO, hDom, hFo, -⟩ := hI + obtain ⟨hD', hF', hR, hV⟩ := recvFinFrames b c hz hDom hFo hG + rcases hV' with ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ | ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ + · exact ⟨J.1.1.vmOrcAcross J'.1.1 hO hR, hD', hF', SInvR.G5.idleVt_of J'.1.1.idleTcb hV⟩ + · exact ⟨J.1.1.vmOrcAcrossR J'.1.1 hO hR, hD', hF', SInvR.G5.idleVt_of J'.1.1.idleTcb hV⟩ + +/-- **The finish facts of a `handleYield` outcome.** -/ +theorem VmEptState.yieldFinishFacts + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {z : Unit × State Unit} (hz : z ∈ (handleYield (Z := Unit) σ).1) + {s' : S} {a' : A} (hV' : VEAnyX (L := L) arI AR s' a' z.2) + {on rc : ObjRef → Bool} (hI : FinishFactsS3 (st := st) on rc s σ) (hG : VmFin.VG σ) : + FinishFactsS3 (st := st) on rc s' z.2 := by + obtain ⟨hO, hDom, hFo, -⟩ := hI + obtain ⟨hD', hF', hR, hV, -⟩ := yieldFinFrames hz hDom hFo hG + obtain ⟨V', X', XL', Z', H', Cs', Ac', Lst', D', _, J'⟩ := hV' + exact ⟨J.1.1.vmOrcAcross J'.1.1 hO hR, hD', hF', SInvR.G5.idleVt_of J'.1.1.idleTcb hV⟩ + +end + +set_option maxHeartbeats 4000000 in +/-- **FL4's `finish` at a `VEAnyS` state with its finish facts**, against `fl4KernelExitVF` (module docstring). -/ +theorem VEAnyS.finish {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] + {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} + (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hnd : Fl4Spec.numDomains = 1) (hT : TimeAgree arI) (hRef : ArchRefines arI AR) (hDL : SetDlDet) + (hFpu : FpuImplDet) (hFW : FpuWriteDet) + (hmb : ∀ a, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ a v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {s : S} {a : A} {σ : Fl4Spec.State Unit} (hV : VEAnyS (L := L) arI AR s a σ) + {on rc : Fl4Spec.ObjRef → Bool} (hF : FinishFactsS3 (st := st) (L := L) on rc s σ) : + syscall.finish st arI s a ⦃ r => ∃ σ', ((), σ') ∈ (Fl4Spec.fl4KernelExitVF (Z := Unit) on rc σ).1 ∧ + VEAnyX (L := L) arI AR r.1 r.2 σ' ⦄ := by + obtain ⟨hO, hD, hFo, hI⟩ := hF + rcases hV with ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, _, J⟩ | ⟨V, X, XL, Z, H, Cs, Ac, Lst, D, _, J⟩ + · have F := J.1.1.kfinVE_ofFacts hAT hFpu hFW hD hFo hI hO + refine WP.spec_mono (J.finishVF hAT hVA hARI.toARKeep hmb F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, _, _, _, _, J'⟩ + exact ⟨σ', hmem, J'.anyX arI⟩ + · have F := J.1.1.kfinVE_ofFacts hAT hFpu hFW hD hFo hI hO + refine WP.spec_mono (J.finishVF hAT hVA hARI.toARKeep hmb F.orc F.hd hnd + (sniHyp_of hAT hDL) (sendHyp_of hAT) hT hRef F.arch F.archNd F.fo F.idle hmax htu) ?_ + rintro r ⟨σ', hmem, V', Ac', Lst', D', J'⟩ + exact ⟨σ', hmem, J'.anyX arI⟩ + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFoot0.lean b/proofs/Fl4Proofs/P3/VmRecvFoot0.lean new file mode 100644 index 000000000..ef6918fed --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFoot0.lean @@ -0,0 +1,175 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **the receive-from-sender frame as a native step at `JAtZB`**. + +* `zvk_rsfr` / `natK_rsfr`: an `RSFr` step (the sender woken, the endpoint queue shrunk, the receiver's context, SC refills) is a `NatK` step. +* `JAtZB.deadP_rsfr`, `JAtZB.rsfrStep`: XTopJointRecvSendStep's `JAtZ.deadP_rsfr` / `JAtZ.rsfrStep` with the post `JAtZB Z` (as + `JAtZB.wake_postD`), proofs verbatim. + +No named premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.XTopJointRecvSendStep +import Fl4Proofs.P3.ScSignalDonJ +import Fl4Proofs.P3.NatFoot +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 + +noncomputable section + +section +variable {σ1 σ2 : Fl4Spec.State Unit} + +theorem zvk_rsfr {t r : Fl4Spec.ObjRef} (F : RSFr t r σ1 σ2) : ZvK σ1 σ2 := by + refine ⟨fun p ao hp => ?_, fun o ko' ho => ?_, fun o x' ho hx => ?_, fun p vs hp => ?_⟩ + · rcases F.kh p with k | ⟨o1, o2, h1, -, hc⟩ + · exact k + · rw [hp] at h1 + cases h1 + rcases hc with ⟨_, _, _, e, -⟩ | ⟨_, _, _, e, -⟩ | ⟨_, _, e, -⟩ <;> cases e + · rcases F.kh o with k | ⟨o1, o2, h1, h2, hc⟩ + · exact ⟨ko', by rw [← k]; exact ho, rfl⟩ + · rw [ho] at h2 + cases h2 + rcases hc with ⟨_, _, _, rfl, rfl, -⟩ | ⟨_, _, _, rfl, rfl, -⟩ | ⟨_, _, rfl, rfl, -⟩ <;> exact ⟨_, h1, rfl⟩ + · rcases F.kh o with k | ⟨o1, o2, h1, h2, hc⟩ + · exact ⟨x', by rw [← k]; exact ho, hx⟩ + · rw [ho] at h2 + cases h2 + rcases hc with ⟨x, st, ar, rfl, e, htc⟩ | ⟨_, _, _, rfl, e, -⟩ | ⟨_, _, rfl, e, -⟩ + · cases e + refine ⟨x, h1, ?_⟩ + rcases htc.2 with e | ⟨-, -, e | e⟩ + · rw [← e]; exact hx + · rw [e] at hx; cases hx + · rw [e] at hx; cases hx + all_goals cases e + · rcases F.kh p with k | ⟨o1, o2, h1, h2, hc⟩ + · rw [← k]; exact hp + · rw [hp] at h2 + cases h2 + rcases hc with ⟨_, _, _, _, e, -⟩ | ⟨_, _, _, _, e, -⟩ | ⟨_, _, _, e, -⟩ <;> cases e + +theorem natK_rsfr {t r : Fl4Spec.ObjRef} (F : RSFr t r σ1 σ2) : NatK σ1 σ2 := + ⟨zvk_rsfr F, fun p ao hp => by + rcases F.kh p with k | ⟨o1, o2, h1, h2, hc⟩ + · rw [← k]; exact hp + · rw [hp] at h2 + cases h2 + rcases hc with ⟨_, _, _, _, e, -⟩ | ⟨_, _, _, _, e, -⟩ | ⟨_, _, _, e, -⟩ <;> cases e⟩ + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead across an `RSFr` step** with the footprint `CnW` +(module docstring). -/ +theorem JAtZB.deadP_rsfr (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t r : Fl4Spec.ObjRef} {s3 : S} + {σ3 : Fl4Spec.State Unit} (F : RSFr t r σ σ3) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) + (ht : ∀ q ∈ D, q.bv ≠ t) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx h0 => ?_) (fun x hx => ?_) (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) + (fun x n hx => by obtain ⟨rf, h1, -⟩ := F.scF hx; exact ⟨_, h1, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hw hz hqC hK8 hK) F.sk.idle hAS hY3 (hYK.dk q.bv) hd + · -- a dead TCB: its state kept (it is not `t`) + obtain ⟨st', ar, hk3, hs⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs.2 with e | ⟨e, -⟩ + · exact e + · exact absurd e (ht q hq) + · -- a dead endpoint: idle, so holding nobody + obtain ⟨y, hy, hc⟩ := F.epF hx + rcases hc with rfl | ⟨-, hm⟩ + · exact hy + · exfalso + obtain ⟨e', he', e⟩ := J.obj.ep q.bv x hx + have heq : e' = q := P1.eq_of_bv e + subst heq + have d2 := hd.2.1 (J.zH.2.1 e' he') + obtain ⟨x', hk', hO⟩ := J.xtop.ipc.toE.ep e' he' + rw [hx] at hk'; cases hk' + rw [epState_eq] at d2 + cases x with + | idleEP => simp [Fl4Spec.Endpoint.epQueue] at hm + | sendEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + | recvEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + +set_option maxHeartbeats 8000000 in +/-- **An `RSFr` step keeps `JAtZB`** (module docstring). -/ +theorem JAtZB.rsfrStep (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t r : Fl4Spec.ObjRef} + {s' : S} {σ' : Fl4Spec.State Unit} + (hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hF : IFrame (L := L) Z.th s s') (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s)) + (F : RSFr t r σ σ') (I3 : Inv σ') + (hact : σ'.schedulerAction = σ.schedulerAction ∨ σ'.schedulerAction = .chooseNewThread) + (ht : ∀ q ∈ D, q.bv ≠ t) : + JAtZB (L := L) arI AR Z H Cs Ac Lst D s' a σ' := by + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hw + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := by + refine ⟨fun u hu => ?_, F.idleOK J.act.2⟩ + rcases hact with e1 | e1 + · exact F.sched (J.act.1 u (by rw [← e1]; exact hu)) + · rw [e1] at hu; cases hu + have hcn : σ.schedulerAction = .chooseNewThread → σ'.schedulerAction = .chooseNewThread := fun e0 => by + rcases hact with e1 | e1 + · rw [e1, e0] + · exact e1 + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := J.cur.imp hcn F.curInv + have hrun3 : CurRunM σ' := J.curRun.of_cn F.curRunW hact.symm + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := J.deadP_rsfr F hS3 hIR.1 hw k3 hAI3.1 (J.yld.frame k2 F.ykeep) F.ykeep ht + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hw) + (J.idleW_keep hS3 F.sk.idle F.sk.isc).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D) + have hRR' := J.rrecv.frame k4 F.khrb + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 F.sk.idle F.sk.isc) (DXKeep.of_dbg hKd (J.cnW_td hw)), hkern, + J.cnW_utc hw hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa, F.isorted J.isorted, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact J.cs_frameI hks hX3 hw (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 + F.kback F.ipcFr hC R hKd F.sk.orig (J.idleW_keep hS3 F.sk.idle F.sk.isc).1 J.zD + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFoot1.lean b/proofs/Fl4Proofs/P3/VmRecvFoot1.lean new file mode 100644 index 000000000..2aa42ed21 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFoot1.lean @@ -0,0 +1,706 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **`JAtZB` twins of the receive-from-sender helpers** (generated by +`handoff/tools/gen_recvfoot1.py`): `JAtZB.undeadC`, `JAtZB.replyArg_ntcb`, `JAtZB.deadP_rsfrC`, `JAtZB.rsfr_postGR` (post `JAtZB Z`), +`JAtZB.pushDonRJW` (any current SC, as native; same outputs), `JAtZB.pushDonRJ` (with the `RmW` footprint and the `ScTKw` step, as `JAtZB.pushDonJ`), `JAtZB.tmoRecvBaseJ` (with `NatFoot`). +Proofs verbatim from the `JAtZ` originals (XTopJointRecvSendP / RD / T), with the edits noted. No named premise. New file. +-/ +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.XTopJointRecvSendRD +import Fl4Proofs.P3.XTopJointRecvSendPDD +import Fl4Proofs.P3.XTopJointRecvSendT +import Fl4Proofs.P3.XTopJointPushDon +import Fl4Proofs.P3.XTopJointFinScReply +import Fl4Proofs.P3.JAtZBUpdConsumed +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched Fl4Proofs.P1 + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **An object covered at the state leaves the dead set** (XTopJointRestart's +`JAtZB.undead` from the cover facts). -/ +theorem JAtZB.undeadC (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {e : U64} + (hth : ∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) + (hH : ∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits)) + (hez : Z.th e ≠ some .tcb) : + JAtZB (L := L) arI AR Z H Cs Ac Lst (D \ {e}) s a σ := by + have hw : P1.WritesIn st s s (TFoot st Z.T Z.N Z.C Z.c s σ) := P1.WritesIn.refl _ _ + have hK : ScTKw (fun _ => False) σ σ := ScTKw.refl _ σ + exact { J with + jcov := J.jcov.undead hth hH + kern := J.sc_kern hw hK (FK.refl σ) J.xtop rfl Set.sdiff_subset + zD := fun q hq => ⟨J.zD q hq, fun (eq : q = e) => hez (eq ▸ J.ztcb_tcb hq)⟩ } + +/-- The reply argument of a receive is no TCB of the typed heap (null: `H 0` +is empty; a reply: `H` holds a reply there). -/ +theorem JAtZB.replyArg_ntcb (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {m : U64 → U64} + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R m rc rc' rv) : + Z.th rv ≠ some .tcb := fun h => by + have hT := J.zH.1 rv (J.thT rv h) + rcases hrc with ⟨-, -, rfl⟩ | ⟨b, r', -, -, hr, -⟩ + · rw [J.kern.h0] at hT; cases hT + · rw [J.zH.2.2.1 rv hr] at hT; cases hT + +set_option maxHeartbeats 4000000 in +/-- **The dead stay dead across an `RSFr` step and a step keeping them** +(`JAtZB.deadP_rsfr` with the dead objects of the state after kept from the +middle state, `hkq`, as XTopJointSendCall's `JAtZB.deadP_dfrCI`). -/ +theorem JAtZB.deadP_rsfrC (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t r : Fl4Spec.ObjRef} {s3 : S} + {σa σ3 : Fl4Spec.State Unit} (F : RSFr t r σ σa) (hkq : ∀ q ∈ D, σ3.kheap q.bv = σa.kheap q.bv) + (hidl : σ3.idleThread = σ.idleThread) (hS3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3) + (hI3 : IpcRel Z.N Z.T Z.E Z.R Z.B (L.mem s3) σ3) + (hw : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (k3 : ∀ q ∈ Z.C, L.mem s3 (P1.scReplyA q) = L.mem s (P1.scReplyA q)) (hAS : ActSched σ3) + (hY3 : YieldRel Z.T Z.C (L.mem s3) σ3) (hYK : YKeep σ σ3) + (ht : ∀ q ∈ D, q.bv ≠ t) : + ∀ q ∈ D, DeadP (L := L) H s3 q := fun q hq => by + have hd := J.jcov.dead q hq + refine J.deadP_keep hS3 hI3 (fun x hx h0 => ?_) (fun x hx => ?_) + (fun x hx => by rw [hkq q hq]; exact ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => by rw [hkq q hq]; exact ⟨x, F.replyF hx, rfl⟩) + (fun x n hx => by + rw [hkq q hq]; obtain ⟨rf, h1, -⟩ := F.scF hx; exact ⟨_, h1, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hw hz hqC hK8 hK) hidl hAS hY3 (hYK.dk q.bv) hd + · rw [hkq q hq] + obtain ⟨st', ar, hk3, hs⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs.2 with e | ⟨e, -⟩ + · exact e + · exact absurd e (ht q hq) + · rw [hkq q hq] + obtain ⟨y, hy, hc⟩ := F.epF hx + rcases hc with rfl | ⟨-, hm⟩ + · exact hy + · exfalso + obtain ⟨e', he', e⟩ := J.obj.ep q.bv x hx + have heq : e' = q := P1.eq_of_bv e + subst heq + have d2 := hd.2.1 (J.zH.2.1 e' he') + obtain ⟨x', hk', hO⟩ := J.xtop.ipc.toE.ep e' he' + rw [hx] at hk'; cases hk' + rw [epState_eq] at d2 + cases x with + | idleEP => simp [Fl4Spec.Endpoint.epQueue] at hm + | sendEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + | recvEP l => have := hO.1; rw [d2] at this; exact absurd this (by decide) + +set_option maxHeartbeats 8000000 in +/-- **XTopJointSendPush's `JAtZB.wake_postGR` with an `RSFr` frame first** +(module docstring): the frame `σ → σa` by `RSFr` (replies kept), then a step +`σa → σ'` that may bind a free reply. -/ +theorem JAtZB.rsfr_postGR (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n t : Fl4Spec.ObjRef} + {s' : S} {σa σ' : Fl4Spec.State Unit} + (hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hF : IFrame (L := L) Z.th s s') (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s)) + (F : RSFr n t σ σa) (G : SkD σa σ') (hks1 : KSub σa σ') (hsz1 : SzKeep σa σ') (hyk1 : YKeep σa σ') + (hmk1 : McpKeep σa σ') + (hbk1 : ∀ p x, σa.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbIpcBuffer = x.tcbIpcBuffer) + (hrb1 : ∀ d ∈ Z.R, RecvB σ' d → RecvB σa d) (hkz1 : KSz σa σ') (hkb1 : KBack σa σ') (hfk1 : IpcFrKeep σa σ') + (hck1 : (∀ q y n, σa.kheap q = some (.schedContext y n) → ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ + y'.scReplies = y.scReplies) ∧ + (∀ q x, σa.kheap q = some (.reply x) → ∃ x', σ'.kheap q = some (.reply x') ∧ x'.replySc = x.replySc) ∧ + (∀ p x n, σ'.kheap p = some (.schedContext x n) → ∃ x0, σa.kheap p = some (.schedContext x0 n))) + (hfree1 : ∀ d ∈ Z.R, Free σ' d → Free σa d) + (hao1 : ∀ q ao, σa.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + (hkq : ∀ q ∈ D, σ'.kheap q.bv = σa.kheap q.bv) + (I3 : Inv σ') (B3 : BlockedScActive σ') (hAI3 : ActIdle σ') + (hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ') (hrun3 : CurRunM σ') + (hown3 : CurOwnW Z.T Z.C σ') (hoff3 : OffOK σ') (hnsc3 : NSC Z.N Z.C σ') (hiso3 : IpcSorted σ') + (ht : ∀ q ∈ D, q.bv ≠ n) : + JAtZB (L := L) arI AR Z H Cs Ac Lst D s' a σ' := by + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 (G.cdt.trans F.sk.cdt) (G.orig.trans F.sk.orig) + (F.ksub.trans hks1) hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hw + have ck0 := F.callKeep + have ck : (∀ q y n, σ.kheap q = some (.schedContext y n) → ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ + y'.scReplies = y.scReplies) ∧ + (∀ q x, σ.kheap q = some (.reply x) → ∃ x', σ'.kheap q = some (.reply x') ∧ x'.replySc = x.replySc) ∧ + (∀ p x n, σ'.kheap p = some (.schedContext x n) → ∃ x0, σ.kheap p = some (.schedContext x0 n)) := by + refine ⟨fun q y n hq => ?_, fun q x hq => ?_, fun p x n hp => ?_⟩ + · obtain ⟨y', h1, h2⟩ := ck0.1 q y n hq + obtain ⟨y'', h1', h2'⟩ := hck1.1 q y' n h1 + exact ⟨y'', h1', h2'.trans h2⟩ + · obtain ⟨x', h1, h2⟩ := ck0.2.1 q x hq + obtain ⟨x'', h1', h2'⟩ := hck1.2.1 q x' h1 + exact ⟨x'', h1', h2'.trans h2⟩ + · obtain ⟨x0, h0⟩ := hck1.2.2 p x n hp + exact ck0.2.2 p x0 n h0 + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy', hyt⟩ := hfree1 d hd hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy', hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hidl0 : σ'.idleThread = σ.idleThread := G.idle.trans F.sk.idle + have hisc0 : σ'.idleSc = σ.idleSc := G.isc.trans F.sk.isc + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := + J.deadP_rsfrC F hkq hidl0 hS3 hIR.1 hw k3 hAI3.1 + (J.yld.frame k2 (F.ykeep.trans hyk1)) (F.ykeep.trans hyk1) ht + have hsz : SzKeep σ σ' := fun p y n hp => by + obtain ⟨y0, hy0⟩ := hsz1 p y n hp + exact F.szKeep p y0 n hy0 + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hsz q.bv y n hq) hks (J.cnW_irq hw) + (J.idleW_keep hS3 hidl0 hisc0).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 (G.ct.trans F.sk.ct) (G.csc.trans F.sk.csc) + (fun sl h => Or.inl (by rw [← F.sk.orig, ← G.orig]; exact h)) + (by rw [G.arch, F.sk.arch]) + (fun q ao h => hao1 q ao (F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e))) + hDbg (D' := D) + have hRR' : RRecv (L.mem s') Z.R σ' := fun d hd hr => by + obtain ⟨h1, h2⟩ := J.rrecv d hd (F.khrb.recvB (hrb1 d hd hr)) + exact ⟨(k4 d hd).2.trans h1, (k4 d hd).1.trans h2⟩ + have hbuf : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbIpcBuffer = x.tcbIpcBuffer := + fun p x hp => by + obtain ⟨y, hy, e⟩ := F.bufKeep p x hp + obtain ⟨z, hz, e'⟩ := hbk1 p y hy + exact ⟨z, hz, e'.trans e⟩ + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 (F.ykeep.trans hyk1), + J.mcp.frame k1 (F.mcpKeep.trans hmk1), J.buf.frame kb hbuf, hRP', + J.apart.sc_frame hks hsz, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hidl0 hisc0) (DXKeep.of_dbg hKd (J.cnW_td hw)), hkern, + J.cnW_utc hw hF.1 (G.cdt.trans F.sk.cdt) (G.orig.trans F.sk.orig) (G.arch.trans F.sk.arch) hDbg, + hnsc3, I3, hcur3, hown3, hoff3, hAI3, B3, hiso3, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 (F.ksz.trans hkz1) + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact J.cs_frameI hks hX3 hw (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) (G.arch.trans F.sk.arch) + (fun q ao h => hao1 q ao (F.othF h (fun _ e => by cases e) (fun _ _ e => by cases e) (fun _ e => by cases e))) + hsz hF.1 (F.kback.trans' hkb1) (F.ipcFr.trans hfk1) hC R hKd (G.orig.trans F.sk.orig) (J.idleW_keep hS3 hidl0 hisc0).1 J.zD + +set_option maxHeartbeats 32000000 in +/-- **The donating part of `reply_push` at `J`, the donor not current** +(module docstring): `t`, blocked on the reply `w` (bound to it, unlinked, on +no call stack), donates its SC `sc` (active, released) to `d` (no SC, not +blocked in an IPC queue, not dead), both in no queue, a reschedule pending; +the current SC is another one, bound to no thread. -/ +theorem JAtZB.pushDonRJ (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t d w sc : U64} (ht : t ∈ Z.T) (hd : d ∈ Z.T) (hw : w ∈ Z.R) (hsC : sc ∈ Z.C) (htd : t ≠ d) + {xt xd : Fl4Spec.Tcb} {xw : Fl4Spec.Reply} {y : Fl4Spec.SchedContext} {n : Nat} + (hkt : σ.kheap t.bv = some (.tcb xt)) (hkd : σ.kheap d.bv = some (.tcb xd)) + (hkw : σ.kheap w.bv = some (.reply xw)) (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hst : xt.tcbState = .blockedOnReply w.bv) (hxt : xt.tcbSchedContext = some sc.bv) (hyt : y.scTcb = some t.bv) + (hxd : xd.tcbSchedContext = none) (hdq : Fl4Spec.ipcQueuedThreadState xd.tcbState = false) + (hidd : d.bv ≠ σ.idleThread) (hwt : xw.replyTcb = some t.bv) + (hcsC : ∃ c0 ∈ Z.C, c0.bv = σ.curSc) (hcsx : σ.curSc ≠ sc.bv) + {x0 : Fl4Spec.SchedContext} {n0 : Nat} (hk0 : σ.kheap σ.curSc = some (.schedContext x0 n0)) + (hcsF : x0.scTcb = none) (hrelD : released σ y) (hcn : σ.schedulerAction = .chooseNewThread) + (hnqt : NotQueued σ t.bv) (hnqd : NotQueued σ d.bv) (hya : Fl4Spec.scActive y = true) + (hdet : Detached Z.C Lst w) (hn0 : L.mem s (rNextA w) = 0#u64) (hp0 : L.mem s (rPrevA w) = 0#u64) + (hdD : ∀ q ∈ D, q.bv ≠ d.bv) : + (do + let i2 ← obj.Sc.reply st sc s + let s3 ← if i2 != 0#u64 then obj.Reply.set_next st i2 s w false else ok s + let s4 ← obj.Reply.set_prev st w s3 i2 + let s5 ← obj.Sc.set_reply st sc s4 w + let s6 ← obj.Reply.set_next st w s5 sc true + schedcontext.donate st arI s6 a sc d) ⦃ res => res.2 = a ∧ + (do + Fl4Spec.bindScReply (Z := Unit) sc.bv w.bv + Fl4Spec.schedContextDonate (Z := Unit) sc.bv d.bv) σ = + ({((), pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n)}, False) ∧ + ∃ Lst', JAtZB (L := L) arI AR Z H Cs Ac Lst' D res.1 a (pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n) ∧ + P1.WritesIn st s res.1 (RmW (st := st) Z.T Z.N Z.C Z.R Z.c s σ) ∧ + ScTKw (fun _ => False) σ (pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n) ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q0, R0, hc, hs⟩ := hS.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hsc0 : sc ≠ 0#u64 := J.c0 sc hsC + have hscH : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc y n hsc0 hky).2⟩ + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hdH : d ∈ P1.Heap.tcbs H := J.zH.1 d hd + have hwH : w ∈ P1.Heap.replies H := J.tcbs_eq.2 ▸ hw + -- the objects apart + have htd' : t.bv ≠ d.bv := bv_ne htd + have hst' : sc.bv ≠ t.bv := kheap_ne hky hkt (fun _ h1 h2 => by subst h1; cases h2) + have hsd' : sc.bv ≠ d.bv := kheap_ne hky hkd (fun _ h1 h2 => by subst h1; cases h2) + have hws : w.bv ≠ sc.bv := kheap_ne hkw hky (fun _ h1 h2 => by subst h1; cases h2) + have hwt' : w.bv ≠ t.bv := kheap_ne hkw hkt (fun _ h1 h2 => by subst h1; cases h2) + have hwd' : w.bv ≠ d.bv := kheap_ne hkw hkd (fun _ h1 h2 => by subst h1; cases h2) + -- the stack's old head + obtain ⟨⟨y0, n0, hk0, hyL⟩, hLR, -, hLw⟩ := J.call.stack sc hsC + rw [hky] at hk0; cases hk0 + have hh : ∀ h l, y.scReplies = h :: l → h ≠ sc.bv ∧ h ≠ w.bv ∧ ∃ x, σ.kheap h = some (.reply x) := by + intro h l hr + rw [hyL] at hr + cases e : Lst sc with + | nil => rw [e] at hr; cases hr + | cons h' l' => + rw [e] at hr + simp only [List.map_cons, List.cons.injEq] at hr + obtain ⟨rfl, -⟩ := hr + have hh' : h' ∈ Z.R := hLR h' (by rw [e]; exact List.mem_cons_self ..) + obtain ⟨x, hx, -⟩ := J.call.reply h' hh' + refine ⟨fun e' => ?_, fun e' => ?_, x, hx⟩ + · have h2 : σ.kheap sc.bv = some (.reply x) := by rw [← e']; exact hx + rw [hky] at h2; cases h2 + · exact hdet sc hsC (by rw [e, P1.eq_of_bv e']; exact List.mem_cons_self ..) + have hnsw : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u e => by rw [hcn] at e; cases e + have hpd := pd_eqN hkt hkd hky hkw hws hh hyt htd' hst' hsd' hwt' hwd' hcn hnqt + have hdn := bd_donate_eqN hkt hkd hky hkw hws hh hyt htd' hst' hsd' hcn hnqt + -- FL4's words at the start + have bits : ∀ u ∈ Z.T, NotQueued σ u.bv → + (L.mem s (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hc.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s (P1.prioA u))) + rw [hs.rq _ (hc.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hc.released u hu).1 h + apply hnu.2 + rw [hs.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits t ht hnqt + obtain ⟨hqbD, hrbD⟩ := bits d hd hnqd + have ha1 : L.actionW s = 1#u64 := by + have h := hS.act + rw [hcn] at h + unfold P1.LawfulStore.actionW; rw [hc.cur]; exact h + have hna : ∀ u ∈ Z.T, L.actionW s ≠ u := fun u hu h' => by + rw [ha1] at h'; exact P1.notT_small hc.tcbs (by simp) (h' ▸ hu) + have hiw : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s ≠ u := fun u hu hne h' => by + apply hne + rw [hs.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hc.cur] at h' + exact congrArg _ (show nodeW st s Z.c layout.node.IDLE_THREAD = u from h').symm + have hitd : t.bv ≠ σ.idleThread := by + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + intro e; rw [← e, hkt] at hkx; cases hkx; rw [hst] at hxs; cases hxs + have hsw : L.mem s (P1.scA t) = sc := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb t ht + rw [hkt] at hk'; cases hk' + exact optRef_some_eq (hscw.symm.trans hxt) + have hd0 : L.mem s (P1.scA d) = 0#u64 := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb d hd + rw [hkd] at hk'; cases hk' + exact Notif.optRef_none (by rw [← hscw]; exact hxd) + have hiA : sc ∈ Ac := by + have hrm := SchedRel.act_sc hS hsC hky hya + exact (J.blk.2.2.2.1.act _ hscH).2 (fun h => hrm (by rw [h]; rfl)) + have haff : L.mem s (P1.affA d) = L.mem s (P1.scCoreA sc) := by + rw [hc.aff d hd, Fl4Proofs.P3.Sched.scCoreA_eq, hS.core sc hsC] + have hR0 : ∀ r ∈ Z.R, r ≠ 0#u64 ∧ r.val % 2 = 0 := fun r hr => + ⟨(J.rp.align r hr).2.1, by have := (J.rp.align r hr).1; omega⟩ + have hiso : Isolated (L.mem s) Z.R w := J.call.isolated hR0 hw hdet hn0 hp0 + have hrel : inClassR xd.tcbState xd.tcbFault → Fl4Spec.scActive y = true → released σ y := fun _ _ => hrelD + have hnf : ¬ Free σ w := by + rintro ⟨x, hx, hn⟩; rw [hkw] at hx; cases hx; rw [hwt] at hn; cases hn + obtain ⟨⟨Q1, R1, h1⟩, hB1, hX1, hS1, hSg1⟩ := J.blk + refine WP.spec_mono (WP.spec_and + (push_donate_refinesW s a σ hS hR J.nsc J.inv J.call J.rp hsC hsc0 hky ht hyt hkt hd hkd hxd htd hrel hw hdet + hiso J.rfree hnf) + (P1.ipc.push_don.blk (Es := P1.EH H) hAT s a t d w sc Q1 R1 h1 hB1 hX1 hS1 hSg1 htH hqbT hrbT + (hna t ht) (hiw t ht hitd) hwH hdH (Ne.symm htd) hqbD hrbD (hna d hd) (hiw d hd hidd) hsw hsc0 hiA hd0 haff)) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ', hsp, hS', hR', hN', hI', hCR', hRP', hRF', hw0, hwd⟩, hP1⟩ + simp only [WP.uncurry'_pair] at hP1 ha + obtain ⟨hblk, hfoot⟩ := hP1 + subst a' + have e' : σ' = pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n := by + have h := hsp.symm.trans hpd + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + subst e' + refine ⟨rfl, hpd, Function.update Lst sc (w :: Lst sc), ?_⟩ + set σ' := pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n with hσ' + set σB := bdSt σ sc.bv w.bv y n xw with hσB + -- the spec's step and FL4's writes + have hK : ScTKw (fun _ => False) σ σ' := + NKT.of_eq (nkt_bind (nkt_bindScReply _ _) fun _ => nkt_schedContextDonate _ _) hpd + have hW : P1.WritesIn st s s' (fun b => TFoot st Z.T Z.N Z.C Z.c s σ b ∨ CallW' Z.R Z.C b) := + hw0.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl h)) Or.inr + have hX' : XTopAt (L := L) AR Z s' a σ' := + xtop_sc_stepCW (ct := σ.curThread) J.xtop J.ip J.nk J.ks.rep hW (hK.mono fun _ h => h.elim) hS' hR' id + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ' := + tk_liftCW (ct := σ.curThread) J.ip hI hW (ScTKw.ofFalse (P := fun p => p = σ.curThread) hK) hS' hR' + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw0 fun b hb => + hb.elim (fun h => tfoot_oobj hS (Or.inl h)) (fun h => callW'_oobj J.ip hS h) + have hSS := hF.1 + have hks : L.ksb s' = L.ksb s := hF.2 + have hwR : P1.WritesIn st s s' (RmW (st := st) Z.T Z.N Z.C Z.R Z.c s σ) := + J.rmW_of (hw0.mono fun b hb => hb.elim Or.inl (fun h => Or.inr (Or.inl h))) + obtain ⟨k1, k2, kb⟩ := J.rmW_keeps hwR + have hsok : SOK (true, false) σ σ' := + (bd_sok hky hkw hws hh).trans ((pres_schedContextDonate (true, false) _ _).of_eq hdn) + have hyk : YKeep σ σ' := hsok.ykeep rfl + have hmk : McpKeep σ σ' := hsok.mcpKeep + have hcn' : σ'.schedulerAction = .chooseNewThread := rfl + have hFK : FK σ σ' := ⟨pd_csc, pd_idle, pd_isc⟩ + have hIK := J.idleW_keep hS' pd_idle pd_isc + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + -- the bound-before objects at `σB` + have hktB : σB.kheap t.bv = some (.tcb xt) := bd_oth hky hkw hws hh (fun _ e => by cases e) (Ne.symm hst') hkt + have hkdB : σB.kheap d.bv = some (.tcb xd) := bd_oth hky hkw hws hh (fun _ e => by cases e) (Ne.symm hsd') hkd + have hkpB : σB.kheap sc.bv = some (.schedContext { y with scReplies := w.bv :: y.scReplies } n) := bd_kp hws + -- the current SC, kept + have hcs' : σ'.curSc = σ.curSc := @pd_csc σ t.bv d.bv sc.bv w.bv xt xd xw y n + have hcst : σ.curSc ≠ t.bv := kheap_ne hk0 hkt (fun _ h1 h2 => by subst h1; cases h2) + have hcsd : σ.curSc ≠ d.bv := kheap_ne hk0 hkd (fun _ h1 h2 => by subst h1; cases h2) + have hko_cs : σ'.kheap σ.curSc = some (.schedContext x0 n0) := by + rw [hσ', pd_ko hcst hcsd hcsx]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hcsx hk0 + -- the receivers' replies + have hrr : RRecv (L.mem s') Z.R σ' := by + intro d' hd' hrd + have hrd0 := (KhRB.of_sctkw hK).recvB hrd + obtain ⟨h1', h2'⟩ := J.rrecv d' hd' hrd0 + have hdw : d' ≠ w := by + rintro rfl + obtain ⟨x, t', xt', hx, ht', hxt', hrb⟩ := hrd0 + rw [hkw] at hx; cases hx; rw [hwt] at ht'; cases ht'; rw [hkt] at hxt'; cases hxt' + rw [hst] at hrb; cases hrb + have hdet' := detached_of_next0 J.call (fun r hr => (J.rp.align r hr).2.1) h1' + have hnh : (Lst sc).head? ≠ some d' := fun e => hdet' sc hsC (List.mem_of_mem_head? e) + obtain ⟨e1, e2⟩ := hwd d' hd' hdw hnh + exact ⟨e1.trans h1', e2.trans h2'⟩ + -- the dead + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => by + have hdq := J.jcov.dead q hq + have hqt : q.bv ≠ t.bv := J.notDead ht hkt (by rw [hst]; simp [tsCode]) q hq + have hqd : q.bv ≠ d.bv := hdD q hq + have hqs : q.bv ≠ sc.bv := fun e => J.scOwned_not_dead ht hky hyt (by rw [← P1.eq_of_bv e]; exact hdq) + have hko : σ'.kheap q.bv = σB.kheap q.bv := pd_ko hqt hqd hqs + refine J.deadP_keep hIR.2.2.1 hIR.1 (fun x0 hx0 _ => ?_) (fun x0 hx0 => ?_) (fun x0 hx0 => ?_) + (fun x0 hx0 => ?_) (fun x0 m hx0 => ?_) (fun hqC => ?_) + (fun z hz hqC K hK8 hK' => J.rmW_scOut hwR hz hqC hK8 hK') pd_idle + (fun u hu => by rw [hcn'] at hu; cases hu) (J.yld.frame k2 hyk) (hyk.dk q.bv) hdq + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, rfl, rfl, Or.inl rfl⟩ + · rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0 + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, Or.inl rfl, rfl, + Or.inl rfl⟩ + · obtain ⟨x', h1', h2'⟩ := bd_reply hky hkw hws hh hx0 + exact ⟨x', by rw [hko]; exact h1', h2'⟩ + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, Or.inl rfl, Or.inl rfl⟩ + · have hqsc : q ≠ sc := fun e => hqs (by rw [e]) + obtain ⟨-, -, -, hW1⟩ := J.call.stack q hqC + obtain ⟨-, -, -, hW2⟩ := hCR'.stack q hqC + rw [hW2.1, hW1.1, Function.update_of_ne hqsc] + -- the scheduler after + have hown : CurOwnW Z.T Z.C σ' := + Or.inr ⟨by rw [hcs']; exact hcsC, fun x n hx => by + rw [hcs', hko_cs] at hx; cases hx; exact hcsF⟩ + have hoff : OffOK σ' := by + intro h0 y' m hy ha + have hcons : σ'.consumedTime = σ.consumedTime := @bd_cons σ sc.bv w.bv y n xw + have htm : σ'.curTime = σ.curTime := @pd_tm σ t.bv d.bv sc.bv w.bv xt xd xw y n + have hisc : σ'.idleSc = σ.idleSc := @pd_isc σ t.bv d.bv sc.bv w.bv xt xd xw y n + rw [hcs', hko_cs] at hy; cases hy + rw [hcons] at h0 ⊢; rw [htm] + exact J.off h0 x0 n0 hk0 (fun h => ha (by rw [hcs', hisc]; exact h)) + have hact : ActIdle σ' := + ⟨(fun u hu => by rw [hcn'] at hu; cases hu), + dn_idleOK hktB hkdB hkpB htd' hst' hsd' (by rw [bd_idle]; exact Ne.symm hitd) + (by rw [bd_idle]; exact Ne.symm hidd) (bd_idleOK hky hkw hws hh J.act.2)⟩ + have hbsa : BlockedScActive σ' := + dn_bsa hktB hkdB hkpB htd' hst' hsd' hdq (bd_bsa hky hkw hws hh J.bsa hya) + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => hK.szKeep q.bv y n hq) hks (J.rmW_irq hwR) hIK.2⟩ + refine ⟨?_, hwR, hK⟩ + exact J.sc_frameS hks hSS (J.vkeep_of hwR fun b hb => J.rmW_nv hb) hK hX' hCR' hRF' hrr (J.yld.frame k2 hyk) (J.mcp.frame k1 hmk) + (J.buf.frame kb hK.bufKeep) hRP' (J.apart.sc_frame hks hK.szKeep) hjpl (J.jcov_keep hks hSS hdead hIK (DXKeep.of_dbg hKd (J.tdkeep_of hwR fun b hb => J.rmW_ntd hb))) + (J.sc_kernW hW hK hFK hX' hDbg (subset_refl D)) + (J.rmW_utc hwR hSS hK.cdt hK.orig pd_arch hDbg) hN' hI' (Or.inl hcn') hown hoff hact hbsa + (Or.inl (by rw [hcn']; intro h; cases h)) hblk hKd hIK.1 J.zD + +set_option maxHeartbeats 32000000 in +/-- **The donating part of `reply_push` at `J`, the donor not current** +(module docstring, sch29): `JAtZB.pushDonRJ` for any current SC (`JAtZB.own`); `t`, blocked on the reply `w` (bound to it, unlinked, on +no call stack), donates its SC `sc` (active, released) to `d` (no SC, not +blocked in an IPC queue, not dead), both in no queue, a reschedule pending. -/ +theorem JAtZB.pushDonRJW (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t d w sc : U64} (ht : t ∈ Z.T) (hd : d ∈ Z.T) (hw : w ∈ Z.R) (hsC : sc ∈ Z.C) (htd : t ≠ d) + {xt xd : Fl4Spec.Tcb} {xw : Fl4Spec.Reply} {y : Fl4Spec.SchedContext} {n : Nat} + (hkt : σ.kheap t.bv = some (.tcb xt)) (hkd : σ.kheap d.bv = some (.tcb xd)) + (hkw : σ.kheap w.bv = some (.reply xw)) (hky : σ.kheap sc.bv = some (.schedContext y n)) + (hst : xt.tcbState = .blockedOnReply w.bv) (hxt : xt.tcbSchedContext = some sc.bv) (hyt : y.scTcb = some t.bv) + (hxd : xd.tcbSchedContext = none) (hdq : Fl4Spec.ipcQueuedThreadState xd.tcbState = false) + (hidd : d.bv ≠ σ.idleThread) (hwt : xw.replyTcb = some t.bv) + (hrelD : released σ y) (hcn : σ.schedulerAction = .chooseNewThread) + (hnqt : NotQueued σ t.bv) (hnqd : NotQueued σ d.bv) (hya : Fl4Spec.scActive y = true) + (hdet : Detached Z.C Lst w) (hn0 : L.mem s (rNextA w) = 0#u64) (hp0 : L.mem s (rPrevA w) = 0#u64) + (hdD : ∀ q ∈ D, q.bv ≠ d.bv) : + (do + let i2 ← obj.Sc.reply st sc s + let s3 ← if i2 != 0#u64 then obj.Reply.set_next st i2 s w false else ok s + let s4 ← obj.Reply.set_prev st w s3 i2 + let s5 ← obj.Sc.set_reply st sc s4 w + let s6 ← obj.Reply.set_next st w s5 sc true + schedcontext.donate st arI s6 a sc d) ⦃ res => res.2 = a ∧ + (do + Fl4Spec.bindScReply (Z := Unit) sc.bv w.bv + Fl4Spec.schedContextDonate (Z := Unit) sc.bv d.bv) σ = + ({((), pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n)}, False) ∧ + ∃ Lst', JAtZB (L := L) arI AR Z H Cs Ac Lst' D res.1 a (pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n) ∧ + P1.WritesIn st s res.1 (RmW (st := st) Z.T Z.N Z.C Z.R Z.c s σ) ∧ + ScTKw (fun _ => False) σ (pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n) ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hR := J.xtop.fin.1.1.2.2.1 + obtain ⟨Q0, R0, hc, hs⟩ := hS.lists + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hsc0 : sc ≠ 0#u64 := J.c0 sc hsC + have hscH : sc ∈ P1.Heap.scs H := ⟨_, (hJ.sc sc y n hsc0 hky).2⟩ + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hdH : d ∈ P1.Heap.tcbs H := J.zH.1 d hd + have hwH : w ∈ P1.Heap.replies H := J.tcbs_eq.2 ▸ hw + -- the objects apart + have htd' : t.bv ≠ d.bv := bv_ne htd + have hst' : sc.bv ≠ t.bv := kheap_ne hky hkt (fun _ h1 h2 => by subst h1; cases h2) + have hsd' : sc.bv ≠ d.bv := kheap_ne hky hkd (fun _ h1 h2 => by subst h1; cases h2) + have hws : w.bv ≠ sc.bv := kheap_ne hkw hky (fun _ h1 h2 => by subst h1; cases h2) + have hwt' : w.bv ≠ t.bv := kheap_ne hkw hkt (fun _ h1 h2 => by subst h1; cases h2) + have hwd' : w.bv ≠ d.bv := kheap_ne hkw hkd (fun _ h1 h2 => by subst h1; cases h2) + -- the stack's old head + obtain ⟨⟨y0, n0, hk0, hyL⟩, hLR, -, hLw⟩ := J.call.stack sc hsC + rw [hky] at hk0; cases hk0 + have hh : ∀ h l, y.scReplies = h :: l → h ≠ sc.bv ∧ h ≠ w.bv ∧ ∃ x, σ.kheap h = some (.reply x) := by + intro h l hr + rw [hyL] at hr + cases e : Lst sc with + | nil => rw [e] at hr; cases hr + | cons h' l' => + rw [e] at hr + simp only [List.map_cons, List.cons.injEq] at hr + obtain ⟨rfl, -⟩ := hr + have hh' : h' ∈ Z.R := hLR h' (by rw [e]; exact List.mem_cons_self ..) + obtain ⟨x, hx, -⟩ := J.call.reply h' hh' + refine ⟨fun e' => ?_, fun e' => ?_, x, hx⟩ + · have h2 : σ.kheap sc.bv = some (.reply x) := by rw [← e']; exact hx + rw [hky] at h2; cases h2 + · exact hdet sc hsC (by rw [e, P1.eq_of_bv e']; exact List.mem_cons_self ..) + have hnsw : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u e => by rw [hcn] at e; cases e + have hpd := pd_eqN hkt hkd hky hkw hws hh hyt htd' hst' hsd' hwt' hwd' hcn hnqt + have hdn := bd_donate_eqN hkt hkd hky hkw hws hh hyt htd' hst' hsd' hcn hnqt + -- FL4's words at the start + have bits : ∀ u ∈ Z.T, NotQueued σ u.bv → + (L.mem s (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hnu + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hc.queued u hu).1 h + apply hnu.1 0 (specPrio (L.mem s (P1.prioA u))) + rw [hs.rq _ (hc.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hc.released u hu).1 h + apply hnu.2 + rw [hs.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbT, hrbT⟩ := bits t ht hnqt + obtain ⟨hqbD, hrbD⟩ := bits d hd hnqd + have ha1 : L.actionW s = 1#u64 := by + have h := hS.act + rw [hcn] at h + unfold P1.LawfulStore.actionW; rw [hc.cur]; exact h + have hna : ∀ u ∈ Z.T, L.actionW s ≠ u := fun u hu h' => by + rw [ha1] at h'; exact P1.notT_small hc.tcbs (by simp) (h' ▸ hu) + have hiw : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s ≠ u := fun u hu hne h' => by + apply hne + rw [hs.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hc.cur] at h' + exact congrArg _ (show nodeW st s Z.c layout.node.IDLE_THREAD = u from h').symm + have hitd : t.bv ≠ σ.idleThread := by + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + intro e; rw [← e, hkt] at hkx; cases hkx; rw [hst] at hxs; cases hxs + have hsw : L.mem s (P1.scA t) = sc := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb t ht + rw [hkt] at hk'; cases hk' + exact optRef_some_eq (hscw.symm.trans hxt) + have hd0 : L.mem s (P1.scA d) = 0#u64 := by + obtain ⟨x', hk', -, -, hscw, -⟩ := hs.tcb d hd + rw [hkd] at hk'; cases hk' + exact Notif.optRef_none (by rw [← hscw]; exact hxd) + have hiA : sc ∈ Ac := by + have hrm := SchedRel.act_sc hS hsC hky hya + exact (J.blk.2.2.2.1.act _ hscH).2 (fun h => hrm (by rw [h]; rfl)) + have haff : L.mem s (P1.affA d) = L.mem s (P1.scCoreA sc) := by + rw [hc.aff d hd, Fl4Proofs.P3.Sched.scCoreA_eq, hS.core sc hsC] + have hR0 : ∀ r ∈ Z.R, r ≠ 0#u64 ∧ r.val % 2 = 0 := fun r hr => + ⟨(J.rp.align r hr).2.1, by have := (J.rp.align r hr).1; omega⟩ + have hiso : Isolated (L.mem s) Z.R w := J.call.isolated hR0 hw hdet hn0 hp0 + have hrel : inClassR xd.tcbState xd.tcbFault → Fl4Spec.scActive y = true → released σ y := fun _ _ => hrelD + have hnf : ¬ Free σ w := by + rintro ⟨x, hx, hn⟩; rw [hkw] at hx; cases hx; rw [hwt] at hn; cases hn + obtain ⟨⟨Q1, R1, h1⟩, hB1, hX1, hS1, hSg1⟩ := J.blk + refine WP.spec_mono (WP.spec_and + (push_donate_refinesW s a σ hS hR J.nsc J.inv J.call J.rp hsC hsc0 hky ht hyt hkt hd hkd hxd htd hrel hw hdet + hiso J.rfree hnf) + (P1.ipc.push_don.blk (Es := P1.EH H) hAT s a t d w sc Q1 R1 h1 hB1 hX1 hS1 hSg1 htH hqbT hrbT + (hna t ht) (hiw t ht hitd) hwH hdH (Ne.symm htd) hqbD hrbD (hna d hd) (hiw d hd hidd) hsw hsc0 hiA hd0 haff)) ?_ + rintro ⟨s', a'⟩ ⟨⟨ha, σ', hsp, hS', hR', hN', hI', hCR', hRP', hRF', hw0, hwd⟩, hP1⟩ + simp only [WP.uncurry'_pair] at hP1 ha + obtain ⟨hblk, hfoot⟩ := hP1 + subst a' + have e' : σ' = pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n := by + have h := hsp.symm.trans hpd + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + subst e' + refine ⟨rfl, hpd, Function.update Lst sc (w :: Lst sc), ?_⟩ + set σ' := pdSt σ t.bv d.bv sc.bv w.bv xt xd xw y n with hσ' + set σB := bdSt σ sc.bv w.bv y n xw with hσB + -- the spec's step and FL4's writes + have hK : ScTKw (fun _ => False) σ σ' := + NKT.of_eq (nkt_bind (nkt_bindScReply _ _) fun _ => nkt_schedContextDonate _ _) hpd + have hW : P1.WritesIn st s s' (fun b => TFoot st Z.T Z.N Z.C Z.c s σ b ∨ CallW' Z.R Z.C b) := + hw0.mono fun b hb => hb.elim (fun h => Or.inl (Or.inl h)) Or.inr + have hX' : XTopAt (L := L) AR Z s' a σ' := + xtop_sc_stepCW (ct := σ.curThread) J.xtop J.ip J.nk J.ks.rep hW (hK.mono fun _ h => h.elim) hS' hR' id + have hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ' := + tk_liftCW (ct := σ.curThread) J.ip hI hW (ScTKw.ofFalse (P := fun p => p = σ.curThread) hK) hS' hR' + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hw0 fun b hb => + hb.elim (fun h => tfoot_oobj hS (Or.inl h)) (fun h => callW'_oobj J.ip hS h) + have hSS := hF.1 + have hks : L.ksb s' = L.ksb s := hF.2 + have hwR : P1.WritesIn st s s' (RmW (st := st) Z.T Z.N Z.C Z.R Z.c s σ) := + J.rmW_of (hw0.mono fun b hb => hb.elim Or.inl (fun h => Or.inr (Or.inl h))) + obtain ⟨k1, k2, kb⟩ := J.rmW_keeps hwR + have hsok : SOK (true, false) σ σ' := + (bd_sok hky hkw hws hh).trans ((pres_schedContextDonate (true, false) _ _).of_eq hdn) + have hyk : YKeep σ σ' := hsok.ykeep rfl + have hmk : McpKeep σ σ' := hsok.mcpKeep + have hcn' : σ'.schedulerAction = .chooseNewThread := rfl + have hFK : FK σ σ' := ⟨pd_csc, pd_idle, pd_isc⟩ + have hIK := J.idleW_keep hS' pd_idle pd_isc + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + -- the bound-before objects at `σB` + have hktB : σB.kheap t.bv = some (.tcb xt) := bd_oth hky hkw hws hh (fun _ e => by cases e) (Ne.symm hst') hkt + have hkdB : σB.kheap d.bv = some (.tcb xd) := bd_oth hky hkw hws hh (fun _ e => by cases e) (Ne.symm hsd') hkd + have hkpB : σB.kheap sc.bv = some (.schedContext { y with scReplies := w.bv :: y.scReplies } n) := bd_kp hws + -- the current SC (sch29: any owner, `J.own`) + have hcs' : σ'.curSc = σ.curSc := @pd_csc σ t.bv d.bv sc.bv w.bv xt xd xw y n + have hcons : σ'.consumedTime = σ.consumedTime := @bd_cons σ sc.bv w.bv y n xw + have htm : σ'.curTime = σ.curTime := @pd_tm σ t.bv d.bv sc.bv w.bv xt xd xw y n + have hisc : σ'.idleSc = σ.idleSc := @pd_isc σ t.bv d.bv sc.bv w.bv xt xd xw y n + have hrl' : σ'.releaseQueue = σ.releaseQueue := @pd_rel σ t.bv d.bv sc.bv w.bv xt xd xw y n + have hkpP : σ'.kheap sc.bv = + some (.schedContext { { y with scReplies := w.bv :: y.scReplies } with scTcb := some d.bv } n) := dn_kp hsd' + obtain ⟨x0, n0, hk0⟩ : ∃ x0 n0, σ.kheap σ.curSc = some (.schedContext x0 n0) := by + rcases J.own with ⟨u, -, xu, hku, hxu, -, -⟩ | ⟨⟨c0, hc0, e0⟩, -⟩ + · obtain ⟨x0, n0, h0, -⟩ := J.inv.back u.bv xu σ.curSc hku hxu + exact ⟨x0, n0, h0⟩ + · obtain ⟨x0, n0, h0, -⟩ := hs.scs c0 hc0 + exact ⟨x0, n0, by rw [← e0]; exact h0⟩ + have hcst : σ.curSc ≠ t.bv := kheap_ne hk0 hkt (fun _ h1 h2 => by subst h1; cases h2) + have hcsd : σ.curSc ≠ d.bv := kheap_ne hk0 hkd (fun _ h1 h2 => by subst h1; cases h2) + have hko_cs : σ.curSc ≠ sc.bv → σ'.kheap σ.curSc = some (.schedContext x0 n0) := fun hcx => by + rw [hσ', pd_ko hcst hcsd hcx]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hcx hk0 + -- the receivers' replies + have hrr : RRecv (L.mem s') Z.R σ' := by + intro d' hd' hrd + have hrd0 := (KhRB.of_sctkw hK).recvB hrd + obtain ⟨h1', h2'⟩ := J.rrecv d' hd' hrd0 + have hdw : d' ≠ w := by + rintro rfl + obtain ⟨x, t', xt', hx, ht', hxt', hrb⟩ := hrd0 + rw [hkw] at hx; cases hx; rw [hwt] at ht'; cases ht'; rw [hkt] at hxt'; cases hxt' + rw [hst] at hrb; cases hrb + have hdet' := detached_of_next0 J.call (fun r hr => (J.rp.align r hr).2.1) h1' + have hnh : (Lst sc).head? ≠ some d' := fun e => hdet' sc hsC (List.mem_of_mem_head? e) + obtain ⟨e1, e2⟩ := hwd d' hd' hdw hnh + exact ⟨e1.trans h1', e2.trans h2'⟩ + -- the dead + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := fun q hq => by + have hdq := J.jcov.dead q hq + have hqt : q.bv ≠ t.bv := J.notDead ht hkt (by rw [hst]; simp [tsCode]) q hq + have hqd : q.bv ≠ d.bv := hdD q hq + have hqs : q.bv ≠ sc.bv := fun e => J.scOwned_not_dead ht hky hyt (by rw [← P1.eq_of_bv e]; exact hdq) + have hko : σ'.kheap q.bv = σB.kheap q.bv := pd_ko hqt hqd hqs + refine J.deadP_keep hIR.2.2.1 hIR.1 (fun x0 hx0 _ => ?_) (fun x0 hx0 => ?_) (fun x0 hx0 => ?_) + (fun x0 hx0 => ?_) (fun x0 m hx0 => ?_) (fun hqC => ?_) + (fun z hz hqC K hK8 hK' => J.rmW_scOut hwR hz hqC hK8 hK') pd_idle + (fun u hu => by rw [hcn'] at hu; cases hu) (J.yld.frame k2 hyk) (hyk.dk q.bv) hdq + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, rfl, rfl, Or.inl rfl⟩ + · rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0 + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, Or.inl rfl, rfl, + Or.inl rfl⟩ + · obtain ⟨x', h1', h2'⟩ := bd_reply hky hkw hws hh hx0 + exact ⟨x', by rw [hko]; exact h1', h2'⟩ + · exact ⟨x0, by rw [hko]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hqs hx0, Or.inl rfl, Or.inl rfl⟩ + · have hqsc : q ≠ sc := fun e => hqs (by rw [e]) + obtain ⟨-, -, -, hW1⟩ := J.call.stack q hqC + obtain ⟨-, -, -, hW2⟩ := hCR'.stack q hqC + rw [hW2.1, hW1.1, Function.update_of_ne hqsc] + -- the scheduler after + have hown : CurOwnW Z.T Z.C σ' := by + by_cases hcx : σ.curSc = sc.bv + · refine Or.inl ⟨d, hd, _, pd_kd, by rw [hcs', hcx], fun x m hx => ?_, fun _ => by rw [hrl']; exact hnqd.2⟩ + rw [hcs', hcx, hkpP] at hx; cases hx; rfl + · rcases J.own with ⟨u, hu, xu, hku, hxu, hown0, hnr⟩ | ⟨hcsC, hun⟩ + · have hut : u.bv ≠ t.bv := fun e => by + rw [e, hkt] at hku; cases hku; rw [hxt] at hxu; exact hcx (Option.some.inj hxu).symm + have hud : u.bv ≠ d.bv := fun e => by + rw [e, hkd] at hku; cases hku; rw [hxd] at hxu; cases hxu + have hus : u.bv ≠ sc.bv := kheap_ne hku hky (fun _ h1 h2 => by subst h1; cases h2) + refine Or.inl ⟨u, hu, xu, ?_, by rw [hcs']; exact hxu, fun x m hx => ?_, fun h0 => ?_⟩ + · rw [hσ', pd_ko hut hud hus]; exact bd_oth hky hkw hws hh (fun _ e => by cases e) hus hku + · rw [hcs', hko_cs hcx] at hx; cases hx; exact hown0 x0 n0 hk0 + · rw [hrl']; exact hnr (by rw [← hcons]; exact h0) + · exact Or.inr ⟨by rw [hcs']; exact hcsC, fun x m hx => by + rw [hcs', hko_cs hcx] at hx; cases hx; exact hun x0 n0 hk0⟩ + have hoff : OffOK σ' := by + intro h0 y' m hy ha + rw [hcons] at h0 ⊢; rw [htm] + by_cases hcx : σ.curSc = sc.bv + · rw [hcs', hcx, hkpP] at hy; cases hy + exact J.off h0 y n (by rw [hcx]; exact hky) (fun h => ha (by rw [hcs', hisc]; exact h)) + · rw [hcs', hko_cs hcx] at hy; cases hy + exact J.off h0 x0 n0 hk0 (fun h => ha (by rw [hcs', hisc]; exact h)) + have hact : ActIdle σ' := + ⟨(fun u hu => by rw [hcn'] at hu; cases hu), + dn_idleOK hktB hkdB hkpB htd' hst' hsd' (by rw [bd_idle]; exact Ne.symm hitd) + (by rw [bd_idle]; exact Ne.symm hidd) (bd_idleOK hky hkw hws hh J.act.2)⟩ + have hbsa : BlockedScActive σ' := + dn_bsa hktB hkdB hkpB htd' hst' hsd' hdq (bd_bsa hky hkw hws hh J.bsa hya) + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := + ⟨Tr, Cq, hJ.same hSS (fun q y n _ hq => hK.szKeep q.bv y n hq) hks (J.rmW_irq hwR) hIK.2⟩ + refine ⟨?_, hwR, hK⟩ + exact J.sc_frameS hks hSS (J.vkeep_of hwR fun b hb => J.rmW_nv hb) hK hX' hCR' hRF' hrr (J.yld.frame k2 hyk) (J.mcp.frame k1 hmk) + (J.buf.frame kb hK.bufKeep) hRP' (J.apart.sc_frame hks hK.szKeep) hjpl (J.jcov_keep hks hSS hdead hIK (DXKeep.of_dbg hKd (J.tdkeep_of hwR fun b hb => J.rmW_ntd hb))) + (J.sc_kernW hW hK hFK hX' hDbg (subset_refl D)) + (J.rmW_utc hwR hSS hK.cdt hK.orig pd_arch hDbg) hN' hI' (Or.inl hcn') hown hoff hact hbsa + (Or.inl (by rw [hcn']; intro h; cases h)) hblk hKd hIK.1 J.zD + +set_option maxHeartbeats 8000000 in +/-- **The base `J` state of a receive from a timeout-faulted sender** +(module docstring): the SC `sc`'s consumed time cleared. -/ +theorem JAtZB.tmoRecvBaseJ (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {t : U64} (ht : t ∈ Z.T) {sc : U64} (hsc : sc ∈ Z.C) + {y : Fl4Spec.SchedContext} {n : Nat} (hky : σ.kheap sc.bv = some (.schedContext y n)) : + ∃ V, JAtZB (L := L) arI AR (Z.mv Z.G V) H Cs Ac Lst D (tmoS st s sc) a (Ipc.tmoSt σ sc y n) ∧ + NatFoot (L := L) Z s σ (tmoS st s sc) (Ipc.tmoSt σ sc y n) := by + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨r, -, ⟨σ2, c2, he2, V2, J2, F2⟩, hs2, -, he2', -⟩ := spec_exists (WP.spec_and + (J.updConsumedN hAT hmax htu hsc ht) (update_consumed_irel (ar := arI) J.ip _ a _ hI hsc hky hmax htu)) + have eσ : σ2 = Ipc.tmoSt σ sc y n := by + have e := (Prod.mk.inj (he2.symm.trans he2')).1 + exact congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e) + rw [eσ, hs2] at J2 F2 + exact ⟨V2, J2, F2⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFoot2.lean b/proofs/Fl4Proofs/P3/VmRecvFoot2.lean new file mode 100644 index 000000000..1035f1366 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFoot2.lean @@ -0,0 +1,47 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **the post of a receive from a waiting sender with its footprint**. + +`RecvFootPost` is `SendFootPost` (P3/P3JSendFootBase) with the base state's `Ac`, `Lst`, `D` existential (a receive that pushes a +reply first removes the reply from the dead set, `JAtZB.undeadC`, so its base `JAtZB` is at `D \ {rv}`). `VmEptState.xferBaseV'` +is `VmEptState.xferBaseV` for a base `JAtZB` at any `Ac' Lst' D'` (the no-insert case by `VmEptState.ofNat` across `NatFoot.refl`), +`VmEptState.recvFootV` is `VmEptState.sendFootV` for it. No named premise. New file; no existing statement changed. +-/ +import Fl4Proofs.P3.VmEntryS3Xfer +import Fl4Proofs.P3.VmEntryS3SendF +import Fl4Proofs.P3.VmRecvFoot2a +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P1 Vs Notif Endp Ipc Sched + +noncomputable section + +section +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D V X : Set U64} {Lst : U64 → List U64} +variable {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- **`VmEptState.xferBaseV` for a base `JAtZB` at any `Ac' Lst' D'`** (module docstring). -/ +theorem VmEptState.xferBaseV' (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {y : S} {σy : Fl4Spec.State Unit} {G' : MdbGhost} {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} + (Jy : JAtZB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' y a σy) + (hB : XferBase (L := L) Z s y σ σy G') (hcn : Ipc.CnK σ σy) : + VmEptState (L := L) arI V X XL AR (Z.mv G' Z.vs) H Cs Ac' Lst' D' y a σy := by + rcases hB with ⟨rfl, rfl, rfl⟩ | ⟨hmS, hw, hT, cv, c', x, d, hx, hd, hxd, hxv, hdv, hdc, hxc, hoth, hvt⟩ + · exact J.ofNat (V' := Z.vs) Jy (NatFoot.refl _ _ _) + · exact J.ofCn Jy hw (cnVtx_ins hx hd hxd hxv hdv hdc hxc hoth hvt) hmS + (eptSlotStep_of_tcb fun t ht => hT 5 (by decide) t ht) hcn + +/-- **`RecvFootPost` gives the record** (module docstring). -/ +theorem VmEptState.recvFootV [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {r : S × A} + {σ' : Fl4Spec.State Unit} (h : RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r σ') : + VEAnyX (L := L) arI AR r.1 r.2 σ' := by + obtain ⟨y, σy, G', Ac0, Lst0, D0, hB, hcn, Jy, V2, Ac2, Lst2, D2, J', F⟩ := h + exact ((J.xferBaseV' Jy hB hcn).ofNat (V' := V2) J' F).anyX arI + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFoot2a.lean b/proofs/Fl4Proofs/P3/VmRecvFoot2a.lean new file mode 100644 index 000000000..cb2b907ca --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFoot2a.lean @@ -0,0 +1,36 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **`RecvFootPost`**, the post of a receive from a waiting sender with its +footprint: `SendFootPost` (P3/P3JSendFootBase) with the base state's `Ac`, `Lst`, `D` existential (a receive that pushes a reply first +removes the reply from the dead set, `JAtZB.undeadC`, so its base `JAtZB` is at `D \ {rv}`). Native-only imports (the footprint twins +`P3/VmRecvFootB/C/D` import this file; the VM cone shadows native names). No named premise. New file. +-/ +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.NatFoot +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical +variable {S A : Type} {st : machine.Store S} [L : P1.LawfulStore st] +variable {arI : machine.Arch A} {AR : A → Fl4Spec.Cr3 → Prop} +variable {Z : XSets} {H : P1.Heap} {Cs Ac D : Set U64} {Lst : U64 → List U64} + +/-- **The post of a receive from a waiting sender with its footprint** (module docstring). -/ +def RecvFootPost (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs Ac : Set U64) + (Lst : U64 → List U64) (D : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) (r : S × A) + (σ' : Fl4Spec.State Unit) : Prop := + ∃ (y : S) (σy : Fl4Spec.State Unit) (G' : MdbGhost) (Ac0 : Set U64) (Lst0 : U64 → List U64) (D0 : Set U64), + XferBase (L := L) Z s y σ σy G' ∧ Ipc.CnK σ σy ∧ + JAtZB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac0 Lst0 D0 y a σy ∧ + ∃ (V2 : XVs) (Ac2 : Set U64) (Lst2 : U64 → List U64) (D2 : Set U64), + JAtZB (L := L) arI AR (Z.mv G' V2) H Cs Ac2 Lst2 D2 r.1 r.2 σ' ∧ + NatFoot (L := L) (Z.mv G' Z.vs) y σy r.1 σ' + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFootB.lean b/proofs/Fl4Proofs/P3/VmRecvFootB.lean new file mode 100644 index 000000000..fd1dbd00c --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFootB.lean @@ -0,0 +1,1957 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **footprint twins of the stable waiting-sender receives** +(generated by `handoff/tools/gen_recvfb.py` from P3JRecvStB/C/D: recvSendJGSt recvSendFJGSt recvSendJXSt recvSendFPJGSt recvSendPJGSt recvSendP0XSt). + +Each `…FB` theorem is its `…St` original with the same hypotheses and proof, except: the transfer step is +`JAtZB.xferStepGJXB` (its base step's data), the buffer rebase is `JAtZB.bufRebaseX` (its `NatFoot`), the receive-from-sender +frame step is `JAtZB.rsfrStep` (`natK_rsfr`), and the post is `RecvFootPost`. The `…St` statements are untouched. + +No named premise. New file. +-/ +import Fl4Proofs.P3.P3JRecvStB +import Fl4Proofs.P3.P3JRecvStC +import Fl4Proofs.P3.P3JRecvStD +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.VmRecvFoot1 +import Fl4Proofs.P3.VmRecvFoot2a +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from the head sender at `J`**, for any FL4 program `m` +reducing to the endpoint-state switch (`hm`; `JAtZ.recvSendJ0`, and after +`maybe_return_sched_context` XTopJointRecvSendRet's `JAtZB.recvSendRetJFB`). -/ +theorem JAtZB.recvSendJGFB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) (hfs : xs.tcbFault = none) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcall : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → dd.senderIsCall = false ∨ + ((dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rc = .Null)) (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.sendEP (sd'.bv :: q)) sd'.bv) hR1 hS1 hW1 F1 hsT ht (Ne.symm hts) + hks hkt hfs bdg (i != 0#u64) h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksb, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hksy : σy.kheap sd'.bv = some (.tcb xs) := hncn hks (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hks1y : σ1y.kheap sd'.bv = some (.tcb xs) := by simp [σ1y, hse, hksy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1y] + set σ2 := Notif.updK (Endp.setUm σ1y um2) t.bv (.tcb x2) with hσ2 + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1y] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1ey] + -- the spec frame from `σy` + have Fr1y : RSFr sd'.bv t.bv σy σ1y := RSFr.upd hky (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1y um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1y um2) hkt1y x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σy σ2 := (Fr1y.trans (RSFr.um _ _ σ1y um2)).trans Fr2c + have IK1y : IK sd'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hky + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : Ipc.SKeep σy σ1y := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans + (IK.updTcb (σ := Endp.setUm σ1y um2) hkt1y (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σy σ2 := SK1y.trans ((Fl4Proofs.P3.SKeep.um σ1y um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1y um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1y, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jy.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hfs]; unfold fl4_abi.consts.fault.NullFault; rfl + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + cases hcl : dd.senderIsCall with + | false => + simp only [hc, hcl, hft0, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + -- the sender runs + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 hsT hks3 hq3 + (by rw [hstd]; simp [tsCode]) .running (by simp [tsCode]) sched.TS_RUNNING (by unfold sched.TS_RUNNING; rfl)) + (set_thread_state_sfoot hIR3.2.2.1 hsT sched.TS_RUNNING)) + (WP.spec_and (P1.sts_all s3 sd' _ Q0 R0 h3 hB3 hX3 hS3' hSg3 hsH P1.not_blocked_running P1.running_ne + (Or.inl (Or.inr rfl)) hnr3 + ⟨fun _ _ _ _ _ => P1.runnable_running, fun _ _ _ => P1.runnable_running, fun _ => P1.runnable_running⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 sd' sched.TS_RUNNING h3 hsH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { xs with tcbState := .running } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σy.curThread; rw [hcty]; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + -- the footprints from the transfer's base + have hks2b : L.ksb s2 = L.ksb y := hW2.1 + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb y := hW3'.1 + have hW5' : P1.WritesIn st y s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW3'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3b).1 hb))))) + have hfoot := footW_trans (footW_trans F2 (footW_of fu3)) (footW_of fu5) + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr hXc)⟩, hB5, hX5, hS5', hSg5⟩ + -- the frame + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr5 : RSFr sd'.bv t.bv σy σ5 := Fr2.trans (Fr3.trans (RSFr.stsRun hks3 (Or.inl ⟨_, _, hstd⟩))) + -- `Inv` at the end + have hnqD : NotQueued σy sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σy σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σy σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd sd'.bv xs hksy (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => by rw [hfs] at h; cases h⟩)))) SK5' + have I5' : Inv σ5' := ((Jy.inv.toBut sd'.bv).ik hnqD IK5').1.inv fun _ _ _ => hrib + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqD.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- back to `JAtZ` + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) + Jy ht hth hkty hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hksy (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR5 := IpcRel.ofSRN hR5 hS5 + have J5 := Ju.rsfrStep (by rw [← hKy.2.1] at hIR5; exact hIR5) hF hWu hblk hFu (RSFr.ofUm Fr5 umu) I5 + (Or.inl act5) htD + -- `possible_switch_to` + have hk5 : σ5.kheap sd'.bv = some (.tcb x5) := by rw [hσ5, stsAct_kheap]; simp + have hnr5 : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqD.2 + have hact5 : ∀ p y' n, x5.tcbSchedContext = some p → σ5.kheap p = some (.schedContext y' n) → + Fl4Spec.scActive y' = true := by + intro p y' n hp hy + obtain ⟨y0, hy0, hyy, -⟩ := Fr5.scB hy + obtain ⟨y1, n1, hky1, hyt⟩ := Jy.inv.back _ _ _ hksy hp + rw [hy0] at hky1; cases hky1 + have h0 := Jy.bsa.tcb p y0 n sd'.bv xs hy0 hyt hksy (by rw [hstd]; rfl) + have em : y'.scRefillMax = y0.scRefillMax := by rw [hyy] + simp only [Fl4Spec.scActive] at h0 ⊢; rw [em]; exact h0 + have hti5 : sd'.bv ≠ σ5.idleThread := by + rw [Fr5.sk.idle] + intro h + obtain ⟨x, hkx, -, hxs', -⟩ := Jy.act.2 + rw [← h, hksy] at hkx; cases hkx; rw [hstd] at hxs'; cases hxs' + have htc5 : sd'.bv ≠ σ5.curThread := by rw [Fr5.sk.ct, hcty]; exact hsc + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hspec := receiveIpc_send_eqB b rights blk hsr hkt hbN hk hks hse hstd hcl e2' hA hks3 hfs + refine WP.spec_bind (J5.sendSwitchJ hsT hk5 rfl hnr5 hact5 hti5 htc5) ?_ + rintro s7 ⟨σ7, h7, Lst7, J7, F7⟩ + have Fy5 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (natK_rsfr (RSFr.ofUm Fr5 umu)) + refine (WP.spec_ok _).2 ⟨((), σ7), ?_, y, σy, Gy, _, _, _, hBase, hCnKy, Jy, _, _, _, _, J7, Jy.natTrans Fy5 F7.cast⟩ + rw [hspec, h7]; exact Set.mem_singleton _ + + | true => + have hno := (hcall _ _ hstd).resolve_left (fun h => by rw [hcl] at h; cases h) + have hnoS : ¬ ((dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none) := by + rintro ⟨hgg, hrn⟩ + rcases hno with ⟨hg0, hgr0⟩ | hnull + · rcases hgg with h | h + · rw [hg0] at h; cases h + · rw [hgr0] at h; cases h + · rcases hrc with ⟨-, -, hrv⟩ | ⟨_, _, hr, -⟩ + · exact hrn (by rw [hrv, optRef_zero]) + · rw [hnull] at hr; cases hr + suffices H : (do + let s4 ← sched.set_thread_state st s3 sd' sched.TS_INACTIVE + ok (s4, a)) ⦃ r => ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + rcases hno with ⟨hg0, hgr0⟩ | hnull + · simp only [hc, hcl, hg, hg0, hgr, hgr0, Bool.false_eq_true, ↓reduceIte] + exact H + · have hrv : rv = 0#u64 := by + rcases hrc with ⟨-, -, hrv⟩ | ⟨_, _, hr, -⟩ + · exact hrv + · rw [hnull] at hr; cases hr + subst hrv + simp only [hc, hcl, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact H + -- the sender `Inactive` + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + obtain ⟨hqb, hrb'⟩ := J.curBits hsT hnqS.1 hnqS.2 + have hna3 : L.actionW s3 ≠ sd' := by + refine actionW_ne_of hIR3.2.2.1 hsT (fun h => ?_) + have hsw := Jy.act.1 sd'.bv (show σy.schedulerAction = _ from act3.symm.trans h) + obtain ⟨x, -, -, -, hkx, -, hrx, -⟩ := schedulable_inv hsw + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 hsT hks3 hq3 + (by rw [hstd]; simp [tsCode]) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hIR3.2.2.1 hsT sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 sd' _ Q0 R0 h3 hB3 hX3 hS3' hSg3 hsH P1.not_blocked_inactive P1.inactive_ne + (Or.inl (Or.inr rfl)) hnr3 + ⟨fun c hc' p hp hm => absurd hm (h0.notQ hsH hqb hc' hp), fun c hc' hm => absurd hm (h0.notR hsH hrb' hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 sd' sched.TS_INACTIVE h3 hsH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σy.curThread; rw [hcty]; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + -- the footprints from the transfer's base + have hks2b : L.ksb s2 = L.ksb y := hW2.1 + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb y := hW3'.1 + have hW5' : P1.WritesIn st y s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW3'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3b).1 hb))))) + have hfoot := footW_trans (footW_trans F2 (footW_of fu3)) (footW_of fu5) + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr hXc)⟩, hB5, hX5, hS5', hSg5⟩ + -- the frame + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr5 : RSFr sd'.bv t.bv σy σ5 := Fr2.trans (Fr3.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩))) + -- `Inv` at the end + have hnqD : NotQueued σy sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σy σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σy σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd sd'.bv xs hksy (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => by rw [hfs] at h; cases h⟩)))) SK5' + have I5' : Inv σ5' := ((Jy.inv.toBut sd'.bv).ik hnqD IK5').1.inv fun _ _ _ => hrib + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqD.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- back to `JAtZ` + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) + Jy ht hth hkty hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hksy (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR5 := IpcRel.ofSRN hR5 hS5 + have J5 := Ju.rsfrStep (by rw [← hKy.2.1] at hIR5; exact hIR5) hF hWu hblk hFu (RSFr.ofUm Fr5 umu) I5 + (Or.inl act5) htD + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hspec := receiveIpc_sendCall_eq b rights blk hsr hkt hbN hk hks hse hstd hnoS e2' hA hks3 (Or.inl hcl) + have Fy5 := JAtZB.natFoot_post Jy Fsu Ju0 hWu (natK_rsfr (RSFr.ofUm Fr5 umu)) + refine (WP.spec_ok _).2 ⟨((), σ5), ?_, y, σy, Gy, _, _, _, hBase, hCnKy, Jy, _, _, _, _, J5, Fy5⟩ + rw [hspec]; exact Set.mem_singleton _ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a faulted head sender at `J`** (a timeout fault only +without an SC), no reply pushed +(module docstring), for any FL4 program `m` reducing to the endpoint-state +switch. -/ +theorem JAtZB.recvSendFJGFB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {f : Fl4Spec.Fault} + (hft : xs.tcbFault = some f) (hto : ∀ b, f = .timeout b → xs.tcbSchedContext = none) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hnp : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rc = .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hUt := J.rbufU ht hth hkt + have hok : FaultOK (L := L) s1 sd' xs f := by + cases f with + | timeout b0 => + have hsc := hto b0 rfl + obtain ⟨Q, R0, -, hsp⟩ := hS1.1.1.2.1.lists + obtain ⟨x, hk', -, -, hscw, -⟩ := hsp.tcb sd' hsT + rw [hks1] at hk'; cases hk' + exact ⟨hsc, optRef_none (hscw.symm.trans hsc)⟩ + | _ => trivial + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hok e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUt b hb).1.ksb hW1.1, (hUt b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) t.bv (.tcb x2) with hσ2 + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1e] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 ht hA2 (fun p hp => by simp [σ2, hp]) hkt1 hkt2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xt b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + -- the spec frame from `σ` + have Fr1 : RSFr sd'.bv t.bv σ σ1 := RSFr.upd hk (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1 um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1 um2) hkt1 x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σ σ2 := (Fr1.trans (RSFr.um _ _ σ1 um2)).trans Fr2c + have IK1 : IK sd'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hk + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans + (IK.updTcb (σ := Endp.setUm σ1 um2) hkt1 (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((Fl4Proofs.P3.SKeep.um σ1 um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1 um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => J.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + have hno := hnp _ _ hstd + have hnoS : ¬ ((dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none) := by + rintro ⟨hgg, hrn⟩ + rcases hno with ⟨hg0, hgr0⟩ | hnull + · rcases hgg with h | h + · rw [hg0] at h; cases h + · rw [hgr0] at h; cases h + · rcases hrc with ⟨-, -, hrv⟩ | ⟨_, _, hr, -⟩ + · exact hrn (by rw [hrv, optRef_zero]) + · rw [hnull] at hr; cases hr + suffices HH : (do + let s4 ← sched.set_thread_state st s3 sd' sched.TS_INACTIVE + ok (s4, a)) ⦃ r => ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + rcases hno with ⟨hg0, hgr0⟩ | hnull + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hg0, hgr, hgr0, Bool.false_eq_true, ↓reduceIte] + exact HH + · simp only [hci, hftB, hg, hg0, hgr, hgr0, Bool.false_eq_true, ↓reduceIte] + exact HH + · have hrv : rv = 0#u64 := by + rcases hrc with ⟨-, -, hrv⟩ | ⟨_, _, hr, -⟩ + · exact hrv + · rw [hnull] at hr; cases hr + subst hrv + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact HH + · simp only [hci, hftB, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact HH + -- the sender `Inactive` + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + obtain ⟨hqb, hrb'⟩ := J.curBits hsT hnqS.1 hnqS.2 + have hna3 : L.actionW s3 ≠ sd' := by + refine actionW_ne_of hIR3.2.2.1 hsT (fun h => ?_) + have hsw := J.act.1 sd'.bv (show σ.schedulerAction = _ from act3.symm.trans h) + obtain ⟨x, -, -, -, hkx, -, hrx, -⟩ := schedulable_inv hsw + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 hsT hks3 hq3 + (by rw [hstd]; simp [tsCode]) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hIR3.2.2.1 hsT sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 sd' _ Q0 R0 h3 hB3 hX3 hS3' hSg3 hsH P1.not_blocked_inactive P1.inactive_ne + (Or.inl (Or.inr rfl)) hnr3 + ⟨fun c hc' p hp hm => absurd hm (h0.notQ hsH hqb hc' hp), fun c hc' hm => absurd hm (h0.notR hsH hrb' hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 sd' sched.TS_INACTIVE h3 hsH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + -- the footprints + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW5' : P1.WritesIn st s s5 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW3'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3b).1 hb))))) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu5) + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr hXc)⟩, hB5, hX5, hS5', hSg5⟩ + -- the frame + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr5 : RSFr sd'.bv t.bv σ σ5 := Fr2.trans (Fr3.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩))) + -- `Inv` at the end + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σ σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σ σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := by + by_cases htf : ∃ b0, f = .timeout b0 + · obtain ⟨b0, rfl⟩ := htf + intro y scp z n hy hscp + rw [hk5'] at hy + simp only [Option.some.injEq, Fl4Spec.KernelObject.tcb.injEq] at hy + subst hy + rw [hx5] at hscp + simp [hto b0 rfl] at hscp + · have hnt : ∀ b', xs.tcbFault ≠ some (.timeout b') := fun b' h => + htf ⟨b', Option.some.inj (hft.symm.trans h)⟩ + exact Ipc.RelIfBound.of_keep (J.inv.rd sd'.bv xs hks (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => hnt b' h⟩)))) SK5' + have I5' : Inv σ5' := ((J.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- back to `JAtZ` + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := J.bufRebaseX ht hth hkt hW5' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hks (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s5 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR5 := IpcRel.ofSRN hR5 hS5 + have J5 := Ju.rsfrStep hIR5 hF hWu hblk hFu (RSFr.ofUm Fr5 umu) I5 (Or.inl act5) htD + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hspec := receiveIpc_sendCall_eq b rights blk hsr hkt hbN hk hks hse hstd hnoS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) + have Fy5 := JAtZB.natFoot_post J Fsu Ju0 hWu (natK_rsfr (RSFr.ofUm Fr5 umu)) + refine (WP.spec_ok _).2 ⟨((), σ5), ?_, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Z.vs, _, _, _, J5, Fy5.cast⟩ + rw [hspec]; exact Set.mem_singleton _ + +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from the head sender at `J`**, the cases of `SendOK`, for +any FL4 program `m` reducing to the endpoint-state switch. -/ +theorem JAtZB.recvSendJXFB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) (hok : SendOK xs rc) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + rcases hok with ⟨hfs, hcall⟩ | ⟨⟨f, hft, hto⟩, hnp⟩ + · exact J.recvSendJGFB ht hkt hrun hbN hct he hk hks hfs cb c1 c2 c3 c4 hrc hcall b rights + · exact J.recvSendFJGFB ht hkt hrun hbN hct he hk hks hft hto cb c1 c2 c3 c4 hrc hnp b rights + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a faulted head sender pushing a reply at `J`** +(module docstring): the sender holds a fault (a timeout only without an SC), +has the grant or grant-reply right, the receiver passes a reply object +(covered, `hcovR`), at a resumed entry; for any FL4 program `m` reducing to +the endpoint-state switch. -/ +theorem JAtZB.recvSendFPJGFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {f : Fl4Spec.Fault} + (hft : xs.tcbFault = some f) (hto : ∀ b, f = .timeout b → xs.tcbSchedContext = none) + (hAct : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue ∧ xs.tcbSchedContext = none)) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hUt := J.rbufU ht hth hkt + have hok : FaultOK (L := L) s1 sd' xs f := by + cases f with + | timeout b0 => + have hsc := hto b0 rfl + obtain ⟨Q, R0, -, hsp⟩ := hS1.1.1.2.1.lists + obtain ⟨x, hk', -, -, hscw, -⟩ := hsp.tcb sd' hsT + rw [hks1] at hk'; cases hk' + exact ⟨hsc, optRef_none (hscw.symm.trans hsc)⟩ + | _ => trivial + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hok e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUt b hb).1.ksb hW1.1, (hUt b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) t.bv (.tcb x2) with hσ2 + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1e] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 ht hA2 (fun p hp => by simp [σ2, hp]) hkt1 hkt2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xt b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + -- the spec frame from `σ` + have Fr1 : RSFr sd'.bv t.bv σ σ1 := RSFr.upd hk (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1 um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1 um2) hkt1 x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σ σ2 := (Fr1.trans (RSFr.um _ _ σ1 um2)).trans Fr2c + have IK1 : IK sd'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hk + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans + (IK.updTcb (σ := Endp.setUm σ1 um2) hkt1 (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((Fl4Proofs.P3.SKeep.um σ1 um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1 um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => J.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) -- the sender is in no ready or release queue + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, with the current SC (so no donation) + have hA0 := hAct + have hCI : σ.schedulerAction = .resumeCurThread → CurInv σ := fun hres => + J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hnd : xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none := by + rcases hA0 with hres | ⟨-, -, h⟩ + · obtain ⟨xc, hkc, hcs, -⟩ := (hCI hres).tcb + rw [hct, hkt] at hkc; cases hkc; exact Or.inr (by rw [hcs]; simp) + · exact Or.inl h + have hnrT : t.bv ∉ σ.releaseQueue := by + rcases hA0 with hres | ⟨-, h, -⟩ + · rw [← hct]; exact (hCI hres).notRel + · exact h + have hsw : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u h => by + rcases hA0 with h' | ⟨h', -⟩ <;> rw [h'] at h <;> cases h + have hnqT : ∀ dm p, t.bv ∉ σ.readyQueues dm p := by rw [← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σ σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σ.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkr + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σ.readyQueues dm p) → u.bv ∉ σ.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σ.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hni + have his : sd'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hks] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (J.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hscw : L.mem s3 (P1.scA sd') = 0#u64 ∨ L.mem s3 (P1.scA t) ≠ 0#u64 := by + rcases hnd with h | h + · exact Or.inl (optRef_none (hssc.symm.trans h)) + · exact Or.inr fun h0 => h (by rw [← hsc2, hdsc, h0, optRef_zero]) + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3'] + refine ok_bind_spec _ _ (HH _ (hscw.elim (fun h => ?_) Or.inr)) + rw [← hi3] at h; simp [h] at h3' + · rw [if_neg h3']; exact ok_bind_spec _ _ (HH _ (Or.inl rfl)) + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hrv0, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + intro cd hcd + refine WP.spec_mono (hPush cd hcd) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σ σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σ σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σ σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := by + by_cases htf : ∃ b0, f = .timeout b0 + · obtain ⟨b0, rfl⟩ := htf + intro y scp z n hy hscp + rw [hk5'] at hy + simp only [Option.some.injEq, Fl4Spec.KernelObject.tcb.injEq] at hy + subst hy + rw [hx5] at hscp + simp [hto b0 rfl] at hscp + · have hnt : ∀ b', xs.tcbFault ≠ some (.timeout b') := fun b' h => + htf ⟨b', Option.some.inj (hft.symm.trans h)⟩ + exact Ipc.RelIfBound.of_keep (J.inv.rd sd'.bv xs hks (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => hnt b' h⟩)))) SK5' + have I5' : Inv σ5' := ((J.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact J.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hks (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa J.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hactP] at hu; exact absurd hu (hsw u)), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK J.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (hactP.trans hcn) + have hCIa : CurInv σ5 := Fr5.curInv (hCI hres) + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (by rw [hactP, hcn]; intro h; cases h) + have hrun5 : CurRunW σ5 := Fr5.curRunW (J.curRun.resolve_left fun e0 => e0 hres) + exact Or.inr (by + rcases hrun5 with h | h + · refine Or.inl fun y hy => h y ?_ + rw [G'.ct, ps_ko hcne1 hcne2] at hy; exact hy + · exact Or.inr (by rw [G'.ct, G'.idle]; exact h)) + -- back to `JAtZ` + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := J.bufRebaseX ht hth hkt hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hks (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR hIR4 hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own J.own)) + (ps_off hkta hkwa hws (Fr5.off J.off)) (ps_nsc hkta hkwa hws (Fr5.nsc J.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted J.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → ∀ bd, Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r bd σ3 = + ({((), σ')}, False) := by + intro r hr bd + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [replyPush_don_irrel hks3 hkt3 (hnd.imp id fun h => by rw [hsc2]; exact h) bd cd, hpush, heqP] + have hspec := receiveIpc_sendPush_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) hPS + have Fy6 := JAtZB.natFoot_post J Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Z.vs, _, _, _, J6, Fy6.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a calling head sender (no fault) pushing a reply at +`J`** (module docstring; XTopJointRecvSend's `JAtZB.recvSendJGFB` up to the +transfer, its intermediate `JAtZ` `Jy` at `σy`): the grant or grant-reply +right, a reply object (covered, `hcovR`), at a resumed entry. -/ +theorem JAtZB.recvSendPJGFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) (hfs : xs.tcbFault = none) + (hAct : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue ∧ xs.tcbSchedContext = none)) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hcl0 : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → dd.senderIsCall = true) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.sendEP (sd'.bv :: q)) sd'.bv) hR1 hS1 hW1 F1 hsT ht (Ne.symm hts) + hks hkt hfs bdg (i != 0#u64) h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksb, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hksy : σy.kheap sd'.bv = some (.tcb xs) := hncn hks (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hks1y : σ1y.kheap sd'.bv = some (.tcb xs) := by simp [σ1y, hse, hksy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1y] + set σ2 := Notif.updK (Endp.setUm σ1y um2) t.bv (.tcb x2) with hσ2 + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1y] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1ey] + -- the spec frame from `σy` + have Fr1y : RSFr sd'.bv t.bv σy σ1y := RSFr.upd hky (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1y um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1y um2) hkt1y x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σy σ2 := (Fr1y.trans (RSFr.um _ _ σ1y um2)).trans Fr2c + have IK1y : IK sd'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hky + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : Ipc.SKeep σy σ1y := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans + (IK.updTcb (σ := Endp.setUm σ1y um2) hkt1y (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σy σ2 := SK1y.trans ((Fl4Proofs.P3.SKeep.um σ1y um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1y um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1y, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jy.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hfs]; unfold fl4_abi.consts.fault.NullFault; rfl + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + have hcl : dd.senderIsCall = true := hcl0 _ _ hstd + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + -- the sender is in no ready or release queue + have hnqS : NotQueued σy sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, with the current SC (so no donation) + have hA0 : σy.schedulerAction = .resumeCurThread ∨ + (σy.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue ∧ xs.tcbSchedContext = none) := by + rw [hsay]; exact hAct + have hcty' : σy.curThread = t.bv := hcty.trans hct + have hCI : σy.schedulerAction = .resumeCurThread → CurInv σy := fun hres => + Jy.cur.resolve_left (by rw [hres]; intro h; cases h) + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + have hnd : xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none := by + rcases hA0 with hres | ⟨-, -, h⟩ + · obtain ⟨xc, hkc, hcs, -⟩ := (hCI hres).tcb + rw [hcty', hkty] at hkc; cases hkc; exact Or.inr (by rw [hcs]; simp) + · exact Or.inl h + have hnrT : t.bv ∉ σy.releaseQueue := by + rcases hA0 with hres | ⟨-, h, -⟩ + · rw [← hcty']; exact (hCI hres).notRel + · rw [hrly]; exact h + have hsw : ∀ u, σy.schedulerAction ≠ .switchThread u := fun u h => by + rcases hA0 with h' | ⟨h', -⟩ <;> rw [h'] at h <;> cases h + have hnqT : ∀ dm p, t.bv ∉ σy.readyQueues dm p := by rw [hrqy, ← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σy σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σy.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkry : σy.kheap rv.bv = some (.reply xr) := hncn hkr (fun _ _ h0 => by cases h0) + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkry + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σy.readyQueues dm p) → u.bv ∉ σy.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σy.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [hidy, ← hct]; exact hni + have his : sd'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := Jy.act.2 + rw [← h, hksy] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := Jy.tcbs_eq.2 + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (Jy.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hscw : L.mem s3 (P1.scA sd') = 0#u64 ∨ L.mem s3 (P1.scA t) ≠ 0#u64 := by + rcases hnd with h | h + · exact Or.inl (optRef_none (hssc.symm.trans h)) + · exact Or.inr fun h0 => h (by rw [← hsc2, hdsc, h0, optRef_zero]) + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3'] + refine ok_bind_spec _ _ (HH _ (hscw.elim (fun h => ?_) Or.inr)) + rw [← hi3] at h; simp [h] at h3' + · rw [if_neg h3']; exact ok_bind_spec _ _ (HH _ (Or.inl rfl)) + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · simp only [hc, hcl, hg, hG, hrv0, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + simp only [hc, hcl, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + intro cd hcd + refine WP.spec_mono (hPush cd hcd) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb y := hW2.1 + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb y := hW3'.1 + have hW4' : P1.WritesIn st y s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans F2 (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σy.curThread; rw [hcty]; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σy σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σy σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σy σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd sd'.bv xs hksy (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => by rw [hfs] at h; cases h⟩)))) SK5' + have I5' : Inv σ5' := ((Jy.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact Jy.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hksy (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa Jy.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σy.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hactP] at hu; exact absurd hu (hsw u)), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK Jy.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (hactP.trans hcn) + have hCIa : CurInv σ5 := Fr5.curInv (hCI hres) + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (by rw [hactP, hcn]; intro h; cases h) + have hrun5 : CurRunW σ5 := Fr5.curRunW (Jy.curRun.resolve_left fun e0 => e0 hres) + exact Or.inr (by + rcases hrun5 with h | h + · refine Or.inl fun y hy => h y ?_ + rw [G'.ct, ps_ko hcne1 hcne2] at hy; exact hy + · exact Or.inr (by rw [G'.ct, G'.idle]; exact h)) + -- back to `JAtZ` + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) + Jy ht hth hkty hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hksy (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR (by rw [← hKy.2.1] at hIR4; exact hIR4) hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own Jy.own)) + (ps_off hkta hkwa hws (Fr5.off Jy.off)) (ps_nsc hkta hkwa hws (Fr5.nsc Jy.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted Jy.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → ∀ bd, Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r bd σ3 = + ({((), σ')}, False) := by + intro r hr bd + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [replyPush_don_irrel hks3 hkt3 (hnd.imp id fun h => by rw [hsc2]; exact h) bd cd, hpush, heqP] + have hspec := receiveIpc_sendPush_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inl hcl) hPS + have Fy6 := JAtZB.natFoot_post Jy Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, y, σy, Gy, _, _, _, hBase, hCnKy, Jy, _, _, _, _, J6, Fy6⟩ + +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from the head sender pushing a reply at `J`**, the cases +of `PushOK`, the receiver without a bound notification or with one not +active, polling or not lending its SC (XTopJointRecvSend's `JAtZ.recvPreN`). -/ +theorem JAtZB.recvSendP0XFB (hAT : P0.ArchTotal arI) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : Entered σ) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hok : PushOK xs rc) (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + obtain ⟨hrn, hyes, ⟨hfs, hcl⟩ | ⟨f, hft, hto⟩⟩ := hok + · exact J.recvSendPJGFB hAT ht hkt hrun hbN hct hni.1 hnq he hk hks hfs (Or.inl hni.2) cb c1 c2 c3 c4 hrc hcovR hcl hyes hrn + b rights + · exact J.recvSendFPJGFB hAT ht hkt hrun hbN hct hni.1 hnq he hk hks hft hto (Or.inl hni.2) cb c1 c2 c3 c4 hrc hcovR hyes hrn + b rights + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFootC.lean b/proofs/Fl4Proofs/P3/VmRecvFootC.lean new file mode 100644 index 000000000..d8825fdef --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFootC.lean @@ -0,0 +1,1174 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **footprint twins of the stable waiting-sender receives** +(generated by `handoff/tools/gen_recvfb.py` from P3JRecvStB/C/D: recvSendFPDJSt:C recvSendPDJSt:C). + +Each `…FB` theorem is its `…St` original with the same hypotheses and proof, except: the transfer step is +`JAtZB.xferStepGJXB` (its base step's data), the buffer rebase is `JAtZB.bufRebaseX` (its `NatFoot`), the receive-from-sender +frame step is `JAtZB.rsfrStep` (`natK_rsfr`), and the post is `RecvFootPost`. The `…St` statements are untouched. + +The timeout twins (`--tmo`, P3JRecvStD) have no transfer: their base state is the start state and the footprint runs from it. + +No named premise. New file. +-/ +import Fl4Proofs.P3.P3JRecvStB +import Fl4Proofs.P3.P3JRecvStC +import Fl4Proofs.P3.P3JRecvStD +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.VmRecvFoot1 +import Fl4Proofs.P3.VmRecvFoot2a +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.ScSignalBoundJ +import Fl4Proofs.P3.XTopJointRemove +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a faulted head sender with an SC pushing a reply +and donating the SC at `J`** (module docstring): XTopJointRecvSendP's +`JAtZB.recvSendFPJGFB` for a sender with an SC `p` and a fault other than a +timeout, the receiver (current) without an SC, a reschedule pending, the +current SC bound to no thread; the push donates `p` to the receiver +(`reply_push_iC`'s donation step: `JAtZ` at the block by `JAtZ.rsfr_postGR`, +then `JAtZ.pushDonRJ`). -/ +theorem JAtZB.recvSendFPDJFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {f : Fl4Spec.Fault} + (hft : xs.tcbFault = some f) (hnto : ∀ b, f ≠ .timeout b) + {p : U64} (hpC : p ∈ Z.C) (hxsp : xs.tcbSchedContext = some p.bv) (hxtn : xt.tcbSchedContext = none) + (hcn : σ.schedulerAction = .chooseNewThread) (hnrT0 : t.bv ∉ σ.releaseQueue) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hUt := J.rbufU ht hth hkt + have hok : FaultOK (L := L) s1 sd' xs f := by + cases f with + | timeout b0 => exact absurd rfl (hnto b0) + | _ => trivial + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hok e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUt b hb).1.ksb hW1.1, (hUt b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) t.bv (.tcb x2) with hσ2 + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1e] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 ht hA2 (fun p hp => by simp [σ2, hp]) hkt1 hkt2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xt b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + -- the spec frame from `σ` + have Fr1 : RSFr sd'.bv t.bv σ σ1 := RSFr.upd hk (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1 um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1 um2) hkt1 x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σ σ2 := (Fr1.trans (RSFr.um _ _ σ1 um2)).trans Fr2c + have IK1 : IK sd'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hk + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans + (IK.updTcb (σ := Endp.setUm σ1 um2) hkt1 (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((Fl4Proofs.P3.SKeep.um σ1 um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1 um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => J.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) -- the sender is in no ready or release queue + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, without an SC, a reschedule pending + have hsw : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u h => by rw [hcn] at h; cases h + have hnrT : t.bv ∉ σ.releaseQueue := hnrT0 + have hnqT : ∀ dm p, t.bv ∉ σ.readyQueues dm p := by rw [← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σ σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σ.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkr + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σ.readyQueues dm p) → u.bv ∉ σ.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σ.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hni + have his : sd'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hks] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (J.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + -- the donated SC `p`: the sender's + have hpw3 : L.mem s3 (P1.scA sd') = p := by + rw [hxsp] at hssc; exact (optRef_some_eq hssc.symm) + have hxd2 : x2.tcbSchedContext = none := hsc2.trans hxtn + -- the spec side: the sender `Inactive` (a spec state only), `Inv` there, as in `recvSendFPJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σ σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σ σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σ σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (J.inv.rd sd'.bv xs hks (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => hnto b' (Option.some.inj (hft.symm.trans h))⟩)))) SK5' + have I5' : Inv σ5' := ((J.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have hpsd : p.bv ≠ sd'.bv := fun h => by + obtain ⟨y0, m0, hky0, -⟩ := J.inv.back sd'.bv xs p.bv hks hxsp + rw [h, hks] at hky0; cases hky0 + -- `Inv` and the donated SC released at `σ5` + have IR5 : Inv σ5 ∧ ∀ y n, σ5.kheap p.bv = some (.schedContext y n) → Fl4Spec.scActive y = true → + released σ5 y := by + rcases hU3 with e3 | ⟨p', y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · have e5 : σ5 = σ5' := by rw [hσ5, stsAct_ne hsc3, e3] + rw [e5] + refine ⟨I5', fun y n hy ha => ?_⟩ + exact hrib x5 p.bv y n hk5' hxsp hy ha (fun hm => hnqS.2 (by rw [SK5'.1] at hm; exact hm)) + · have hpp : p' = p.bv := Option.some.inj (hxp.symm.trans hxsp) + subst hpp + have hkp' : σ5'.kheap p.bv = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p.bv z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine ⟨I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_), fun y1 n1 hy ha => ?_⟩ + · obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p.bv hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p.bv hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + · have hkz : (updSc { σ5' with reprogramTimer := true } p.bv z n).kheap p.bv = some (.schedContext z n) := by + simp [Notif.updK] + have hy' := hkz.symm.trans hy; cases hy' + have hya' : Fl4Spec.scActive y' = true := by rw [hz3] at ha; exact ha + exact ⟨ready_now _ _ hz2, sufficient_of_refOK (hz1 (I5'.refills _ y' n hkp' hya' hz6))⟩ + obtain ⟨I5, hrel5⟩ := IR5 + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the block (`pushSt`) from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact J.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hks (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa J.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hst' + have hactP : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hcnP : σ'.schedulerAction = .chooseNewThread := hactP.trans hcn + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hcnP] at hu; cases hu), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK J.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcnP + have hrun6 : CurRunM σ' := Or.inl (by rw [hcnP]; intro h; cases h) + -- the objects at `σ'` + set xP : Fl4Spec.Tcb := { x5 with tcbState := .blockedOnReply rv.bv } with hxP + set xr' : Fl4Spec.Reply := { xr with replyTcb := some sd'.bv } with hxr' + have hkP : σ'.kheap sd'.bv = some (.tcb xP) := ps_kt + have hkwP : σ'.kheap rv.bv = some (.reply xr') := ps_kw hws + have hkt5 : σ5.kheap t.bv = some (.tcb x2) := by rw [e5]; simp [Notif.updK, Ne.symm hst', hkt3] + have hkdP : σ'.kheap t.bv = some (.tcb x2) := by rw [hσ', ps_ko (Ne.symm hst') (Ne.symm hwt)]; exact hkt5 + have hxP : xP.tcbSchedContext = some p.bv := hxsp + obtain ⟨yP, nP, hkyP, hytP⟩ := I6.back sd'.bv xP p.bv hkP hxP + have hpr : p.bv ≠ rv.bv := fun h => by rw [h, hkwP] at hkyP; cases hkyP + have hptP : p.bv ≠ t.bv := fun h => by rw [h, hkdP] at hkyP; cases hkyP + have hky5 : σ5.kheap p.bv = some (.schedContext yP nP) := by + rw [← ps_ko (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5) hpsd hpr]; exact hkyP + have hyaP : Fl4Spec.scActive yP = true := hscA p.bv yP nP hky5 hytP + have hrelP : released σ' yP := by + have h := hrel5 yP nP hky5 hyaP + unfold released at h ⊢; rw [G'.tm]; exact h + have hnqP : NotQueued σ' sd'.bv := ⟨by rw [ps_rq]; exact hnqSa, by rw [ps_rel]; exact hnrSa⟩ + have hnqdP : NotQueued σ' t.bv := by + refine ⟨by rw [ps_rq, e5]; show ∀ dm p, t.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqT, ?_⟩ + rw [ps_rel, Fr5.sk.rel]; exact hnrT + -- the stack's head at `σ'` (from `J`) + have hfree0 : Free σ rv := ⟨xr, hkr, hxr0⟩ + obtain ⟨hdet, -, -⟩ := J.rfree rv hwR hfree0 + obtain ⟨y0, m0, hky0, -⟩ := J.inv.back sd'.bv xs p.bv hks hxsp + have hh0 := J.stackHead hpC hky0 hdet + have hrepl : yP.scReplies = y0.scReplies := by + obtain ⟨y1, hy1, e1⟩ := Fr5.callKeep.1 _ _ _ hky0 + obtain ⟨y2, hy2, e2⟩ := (ps_ck hkta hkwa hws).1 _ _ _ hy1 + rw [hkyP] at hy2; cases hy2; exact e2.trans e1 + have hhP : ∀ h l, yP.scReplies = h :: l → h ≠ p.bv ∧ h ≠ rv.bv ∧ ∃ x, σ'.kheap h = some (.reply x) := by + intro h l hr + rw [hrepl] at hr + obtain ⟨h1, h2, x, hx⟩ := hh0 h l hr + have hhs : h ≠ sd'.bv := fun e0 => by rw [e0, hks] at hx; cases hx + exact ⟨h1, h2, x, by rw [hσ', ps_ko hhs h2]; exact Fr5.replyF hx⟩ + -- the spec's donating push + set σD := pdSt σ' sd'.bv t.bv p.bv rv.bv xP x2 xr' yP nP with hσD + have hpushE : Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv true σ3 = ({((), σD)}, False) := by + rw [replyPush_don_eq hks3 hkt3 hkr3 hws hxsp hxd2, heqP] + exact pd_eqN hkP hkdP hkyP hkwP (Ne.symm hpr) hhP hytP hst' hpsd hptP hws hwt hcnP hnqP + have hneq : pushSt σ3 sd'.bv rv.bv xr xs ≠ σD := by + rw [heqP]; intro e0 + have h1 : σD.kheap t.bv = some (.tcb x2) := by rw [← e0]; exact hkdP + rw [show σD.kheap t.bv = _ from @pd_kd σ' sd'.bv t.bv p.bv rv.bv xP x2 xr' yP nP] at h1 + have h3 := congrArg (fun o => match o with | some (.tcb x) => x.tcbSchedContext | _ => none) h1 + simp only at h3; rw [hxd2] at h3; cases h3 + have hst2 : x2.tcbState = xt.tcbState := by rw [hx2] + have hdq : Fl4Spec.ipcQueuedThreadState x2.tcbState = false := by + rw [hst2]; exact runnable_notQ _ hrun + have hiddP : t.bv ≠ σ'.idleThread := by rw [G'.idle, Fr5.sk.idle]; exact hit + -- FL4: `can_donate` is true + have hftT : (ft != fl4_abi.consts.fault.Timeout) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.Timeout := fun e => by + obtain ⟨b0, hb0⟩ := hxs3.faultT.1 (by rw [e]; unfold fl4_abi.consts.fault.Timeout; first | rfl | decide) + exact hnto b0 (Option.some.inj (hft.symm.trans hb0)) + simpa using hne + suffices HH : ipc.reply_push st arI s3 a sd' t rv true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + have h3' : (i3 != 0#u64) = true := by + rw [hi3, hpw3]; simpa using J.c0 p hpC + rw [if_pos h3', hftT]; exact ok_bind_spec _ _ HH + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hrv0, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + -- the push: link and block (`J` again), then the donation at `J` + refine WP.spec_mono (reply_push_iC (don := true) (P := fun s' σ'' => σ'' = σD → + ∃ (Lst' : U64 → List U64), JAtZB (L := L) arI AR Z H Cs Ac Lst' (D \ {rv}) s' a σ'' ∧ + NatFoot (L := L) Z s σ s' σ'') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' _ _ e0 => absurd e0 hneq) ?_) ?_ + · intro _ s4 hsb hR4 hS4 _ _ _ _ + rw [hpw3, heqP] + have hsb4 : sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok s4 := hsb + -- FL4's words and P1 at the block + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hb4 := P1.sched.set_blocked_on_reply.ipc s3 sd' rv Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 + hsH hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 hcA + rw [hsb4, WP.spec_ok] at hb4 + obtain ⟨h4, hB4, hX4, hS4', hSg4, -, -, -, -, -, -⟩ := hb4 + have hO3 := hX3.1 + obtain ⟨hrΩ3, -⟩ := P1.objs_omega hO3 hSg3.sub + have f1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) + s3 (L.wr s3 (P1.rTcbA rv) sd') := + P1.foot_wr (h3.omega _ (hrΩ3 rv hwH).1).1 (P1.kw_sg (hO3.tcbW hwH)) + have q1 := (P1.omega_wr (v := sd') h3 (hrΩ3 rv hwH).1).1 + have f4 := P1.sched.set_blocked_on_reply.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) (Es := P1.EH H) _ sd' rv q1 hsH + rw [hsb4, WP.spec_ok] at f4 + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s4 := ⟨⟨Q0, R0, h4⟩, hB4, hX4, hS4', hSg4⟩ + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) + (footW_of f1)) (footW_of f4) + -- back to `JAtZ` at the block + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := J.bufRebaseX ht hth hkt hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hks (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR hIR4 hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own J.own)) + (ps_off hkta hkwa hws (Fr5.off J.off)) (ps_nsc hkta hkwa hws (Fr5.nsc J.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted J.isorted)) htD + -- the reply's links at the block + obtain ⟨-, hn0, hp0⟩ := Ju.rfree rv hwR hfree0 + obtain ⟨-, -, -, -, k4⟩ := Ju.cnW_keeps hWu + have htD6 := J6.not_dead_of_st ht hkdP (by rw [hst2]; exact runnable_tsc0 _ hrun) + refine WP.spec_mono (J6.pushDonRJW hAT hsT ht hwR hpC (Ne.symm hts) hkP hkdP hkwP hkyP rfl hxP hytP hxd2 hdq + hiddP rfl hrelP hcnP hnqP hnqdP hyaP hdet (by rw [(k4 rv hwR).2]; exact hn0) + (by rw [(k4 rv hwR).1]; exact hp0) htD6) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, hsp5, Lst5, J7, hwR5, hK5⟩ + simp only at ha5 + subst a5 + have Fy4 := JAtZB.natFoot_post J Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + have F45 := NatFoot.ofW J6.xtop hwR5 (fun b hb => RmW.oobj J6.xtop.fin.1.1.1 hb) + (fun b hb => J6.rmW_nv hb) (fun b hb => J6.rmW_nh hb) (natK_tk hK5) + exact ⟨rfl, _, hsp5, fun _ => ⟨Lst5, J7, J.natTrans Fy4 F45.cast⟩⟩ + rintro ⟨s5, a5⟩ ⟨ha5, σ5'', hpush, hPσ⟩ + simp only at ha5 hpush hPσ ⊢ + subst a5 + have e5'' : σ5'' = σD := by + have h := hpush.symm.trans hpushE + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + obtain ⟨Lst5, J5, Fp⟩ := hPσ e5'' + subst e5'' + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hspec := receiveIpc_sendPushD_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) (by rw [hxsp]; simp) + (by rw [hft]; cases f <;> simp [Fl4Spec.isTimeoutFault] <;> exact hnto _ rfl) + (fun r hr => by rw [Notif.optRef_ne hw00] at hr; cases hr; exact hpushE) + exact ⟨((), σD), by rw [hspec]; exact Set.mem_singleton _, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Z.vs, _, _, _, J5, Fp.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a calling head sender (no fault) with an SC pushing +a reply and donating the SC at `J`** (module docstring): XTopJointRecvSendP's +`JAtZB.recvSendPJGFB` with the donation of `JAtZB.recvSendFPDJFB`. -/ +theorem JAtZB.recvSendPDJFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) (hfs : xs.tcbFault = none) + {p : U64} (hpC : p ∈ Z.C) (hxsp : xs.tcbSchedContext = some p.bv) (hxtn : xt.tcbSchedContext = none) + (hcn : σ.schedulerAction = .chooseNewThread) (hnrT0 : t.bv ∉ σ.releaseQueue) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hcl0 : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → dd.senderIsCall = true) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.sendEP (sd'.bv :: q)) sd'.bv) hR1 hS1 hW1 F1 hsT ht (Ne.symm hts) + hks hkt hfs bdg (i != 0#u64) h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksb, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hksy : σy.kheap sd'.bv = some (.tcb xs) := hncn hks (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hks1y : σ1y.kheap sd'.bv = some (.tcb xs) := by simp [σ1y, hse, hksy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1y] + set σ2 := Notif.updK (Endp.setUm σ1y um2) t.bv (.tcb x2) with hσ2 + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1y] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1ey] + -- the spec frame from `σy` + have Fr1y : RSFr sd'.bv t.bv σy σ1y := RSFr.upd hky (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1y um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1y um2) hkt1y x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σy σ2 := (Fr1y.trans (RSFr.um _ _ σ1y um2)).trans Fr2c + have IK1y : IK sd'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hky + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : Ipc.SKeep σy σ1y := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans + (IK.updTcb (σ := Endp.setUm σ1y um2) hkt1y (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σy σ2 := SK1y.trans ((Fl4Proofs.P3.SKeep.um σ1y um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1y um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1y, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jy.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hfs]; unfold fl4_abi.consts.fault.NullFault; rfl + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + have hcl : dd.senderIsCall = true := hcl0 _ _ hstd + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + -- the sender is in no ready or release queue + have hnqS : NotQueued σy sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, without an SC, a reschedule pending + have hcty' : σy.curThread = t.bv := hcty.trans hct + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + have hnrT : t.bv ∉ σy.releaseQueue := by rw [hrly]; exact hnrT0 + have hsw : ∀ u, σy.schedulerAction ≠ .switchThread u := fun u h => by rw [hsay, hcn] at h; cases h + have hnqT : ∀ dm p, t.bv ∉ σy.readyQueues dm p := by rw [hrqy, ← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σy σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σy.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkry : σy.kheap rv.bv = some (.reply xr) := hncn hkr (fun _ _ h0 => by cases h0) + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkry + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σy.readyQueues dm p) → u.bv ∉ σy.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σy.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [hidy, ← hct]; exact hni + have his : sd'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := Jy.act.2 + rw [← h, hksy] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := Jy.tcbs_eq.2 + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (Jy.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + -- the donated SC `p`: the sender's + have hpw3 : L.mem s3 (P1.scA sd') = p := by + rw [hxsp] at hssc; exact (optRef_some_eq hssc.symm) + have hxd2 : x2.tcbSchedContext = none := hsc2.trans hxtn + -- the spec side: the sender `Inactive` (a spec state only), `Inv` there, as in `recvSendFPJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σy.curThread; rw [hcty]; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σy σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σy σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σy σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd sd'.bv xs hksy (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => by rw [hfs] at h; cases h⟩)))) SK5' + have I5' : Inv σ5' := ((Jy.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have hpsd : p.bv ≠ sd'.bv := fun h => by + obtain ⟨y0, m0, hky0, -⟩ := Jy.inv.back sd'.bv xs p.bv hksy hxsp + rw [h, hksy] at hky0; cases hky0 + -- `Inv` and the donated SC released at `σ5` + have IR5 : Inv σ5 ∧ ∀ y n, σ5.kheap p.bv = some (.schedContext y n) → Fl4Spec.scActive y = true → + released σ5 y := by + rcases hU3 with e3 | ⟨p', y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · have e5 : σ5 = σ5' := by rw [hσ5, stsAct_ne hsc3, e3] + rw [e5] + refine ⟨I5', fun y n hy ha => ?_⟩ + exact hrib x5 p.bv y n hk5' hxsp hy ha (fun hm => hnqS.2 (by rw [SK5'.1] at hm; exact hm)) + · have hpp : p' = p.bv := Option.some.inj (hxp.symm.trans hxsp) + subst hpp + have hkp' : σ5'.kheap p.bv = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p.bv z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine ⟨I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_), fun y1 n1 hy ha => ?_⟩ + · obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p.bv hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p.bv hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + · have hkz : (updSc { σ5' with reprogramTimer := true } p.bv z n).kheap p.bv = some (.schedContext z n) := by + simp [Notif.updK] + have hy' := hkz.symm.trans hy; cases hy' + have hya' : Fl4Spec.scActive y' = true := by rw [hz3] at ha; exact ha + exact ⟨ready_now _ _ hz2, sufficient_of_refOK (hz1 (I5'.refills _ y' n hkp' hya' hz6))⟩ + obtain ⟨I5, hrel5⟩ := IR5 + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the block (`pushSt`) from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact Jy.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hksy (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa Jy.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hst' + have hactP : σ'.schedulerAction = σy.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hcnP : σ'.schedulerAction = .chooseNewThread := hactP.trans (hsay.trans hcn) + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hcnP] at hu; cases hu), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK Jy.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcnP + have hrun6 : CurRunM σ' := Or.inl (by rw [hcnP]; intro h; cases h) + -- the objects at `σ'` + set xP : Fl4Spec.Tcb := { x5 with tcbState := .blockedOnReply rv.bv } with hxP + set xr' : Fl4Spec.Reply := { xr with replyTcb := some sd'.bv } with hxr' + have hkP : σ'.kheap sd'.bv = some (.tcb xP) := ps_kt + have hkwP : σ'.kheap rv.bv = some (.reply xr') := ps_kw hws + have hkt5 : σ5.kheap t.bv = some (.tcb x2) := by rw [e5]; simp [Notif.updK, Ne.symm hst', hkt3] + have hkdP : σ'.kheap t.bv = some (.tcb x2) := by rw [hσ', ps_ko (Ne.symm hst') (Ne.symm hwt)]; exact hkt5 + have hxP : xP.tcbSchedContext = some p.bv := hxsp + obtain ⟨yP, nP, hkyP, hytP⟩ := I6.back sd'.bv xP p.bv hkP hxP + have hpr : p.bv ≠ rv.bv := fun h => by rw [h, hkwP] at hkyP; cases hkyP + have hptP : p.bv ≠ t.bv := fun h => by rw [h, hkdP] at hkyP; cases hkyP + have hky5 : σ5.kheap p.bv = some (.schedContext yP nP) := by + rw [← ps_ko (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5) hpsd hpr]; exact hkyP + have hyaP : Fl4Spec.scActive yP = true := hscA p.bv yP nP hky5 hytP + have hrelP : released σ' yP := by + have h := hrel5 yP nP hky5 hyaP + unfold released at h ⊢; rw [G'.tm]; exact h + have hnqP : NotQueued σ' sd'.bv := ⟨by rw [ps_rq]; exact hnqSa, by rw [ps_rel]; exact hnrSa⟩ + have hnqdP : NotQueued σ' t.bv := by + refine ⟨by rw [ps_rq, e5]; show ∀ dm p, t.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqT, ?_⟩ + rw [ps_rel, Fr5.sk.rel]; exact hnrT + -- the stack's head at `σ'` (from `J`) + have hfree0 : Free σy rv := ⟨xr, hkry, hxr0⟩ + obtain ⟨hdet, -, -⟩ := Jy.rfree rv hwR hfree0 + obtain ⟨y0, m0, hky0, -⟩ := Jy.inv.back sd'.bv xs p.bv hksy hxsp + have hh0 := Jy.stackHead hpC hky0 hdet + have hrepl : yP.scReplies = y0.scReplies := by + obtain ⟨y1, hy1, e1⟩ := Fr5.callKeep.1 _ _ _ hky0 + obtain ⟨y2, hy2, e2⟩ := (ps_ck hkta hkwa hws).1 _ _ _ hy1 + rw [hkyP] at hy2; cases hy2; exact e2.trans e1 + have hhP : ∀ h l, yP.scReplies = h :: l → h ≠ p.bv ∧ h ≠ rv.bv ∧ ∃ x, σ'.kheap h = some (.reply x) := by + intro h l hr + rw [hrepl] at hr + obtain ⟨h1, h2, x, hx⟩ := hh0 h l hr + have hhs : h ≠ sd'.bv := fun e0 => by rw [e0, hksy] at hx; cases hx + exact ⟨h1, h2, x, by rw [hσ', ps_ko hhs h2]; exact Fr5.replyF hx⟩ + -- the spec's donating push + set σD := pdSt σ' sd'.bv t.bv p.bv rv.bv xP x2 xr' yP nP with hσD + have hpushE : Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv true σ3 = ({((), σD)}, False) := by + rw [replyPush_don_eq hks3 hkt3 hkr3 hws hxsp hxd2, heqP] + exact pd_eqN hkP hkdP hkyP hkwP (Ne.symm hpr) hhP hytP hst' hpsd hptP hws hwt hcnP hnqP + have hneq : pushSt σ3 sd'.bv rv.bv xr xs ≠ σD := by + rw [heqP]; intro e0 + have h1 : σD.kheap t.bv = some (.tcb x2) := by rw [← e0]; exact hkdP + rw [show σD.kheap t.bv = _ from @pd_kd σ' sd'.bv t.bv p.bv rv.bv xP x2 xr' yP nP] at h1 + have h3 := congrArg (fun o => match o with | some (.tcb x) => x.tcbSchedContext | _ => none) h1 + simp only at h3; rw [hxd2] at h3; cases h3 + have hst2 : x2.tcbState = xt.tcbState := by rw [hx2] + have hdq : Fl4Spec.ipcQueuedThreadState x2.tcbState = false := by + rw [hst2]; exact runnable_notQ _ hrun + have hiddP : t.bv ≠ σ'.idleThread := by rw [G'.idle, Fr5.sk.idle]; exact hit + -- FL4: `can_donate` is true + have hftT : (ft != fl4_abi.consts.fault.Timeout) = true := by + rw [hft, (hR3.at_tcb hsT hks3).fault0.2 hfs]; unfold fl4_abi.consts.fault.Timeout; decide + suffices HH : ipc.reply_push st arI s3 a sd' t rv true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + have h3' : (i3 != 0#u64) = true := by + rw [hi3, hpw3]; simpa using J.c0 p hpC + rw [if_pos h3', hftT]; exact ok_bind_spec _ _ HH + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · simp only [hc, hcl, hg, hG, hrv0, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + simp only [hc, hcl, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + -- the push: link and block (`J` again), then the donation at `J` + refine WP.spec_mono (reply_push_iC (don := true) (P := fun s' σ'' => σ'' = σD → + ∃ (Lst' : U64 → List U64), JAtZB (L := L) arI AR (Z.mv Gy Z.vs) H Cs Ac Lst' (D \ {rv}) s' a σ'' ∧ + NatFoot (L := L) (Z.mv Gy Z.vs) y σy s' σ'') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' _ _ e0 => absurd e0 hneq) ?_) ?_ + · intro _ s4 hsb hR4 hS4 _ _ _ _ + rw [hpw3, heqP] + have hsb4 : sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok s4 := hsb + -- FL4's words and P1 at the block + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hb4 := P1.sched.set_blocked_on_reply.ipc s3 sd' rv Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 + hsH hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 hcA + rw [hsb4, WP.spec_ok] at hb4 + obtain ⟨h4, hB4, hX4, hS4', hSg4, -, -, -, -, -, -⟩ := hb4 + have hO3 := hX3.1 + obtain ⟨hrΩ3, -⟩ := P1.objs_omega hO3 hSg3.sub + have f1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) + s3 (L.wr s3 (P1.rTcbA rv) sd') := + P1.foot_wr (h3.omega _ (hrΩ3 rv hwH).1).1 (P1.kw_sg (hO3.tcbW hwH)) + have q1 := (P1.omega_wr (v := sd') h3 (hrΩ3 rv hwH).1).1 + have f4 := P1.sched.set_blocked_on_reply.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) (Es := P1.EH H) _ sd' rv q1 hsH + rw [hsb4, WP.spec_ok] at f4 + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s4 := ⟨⟨Q0, R0, h4⟩, hB4, hX4, hS4', hSg4⟩ + have hks2b : L.ksb s2 = L.ksb y := hW2.1 + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb y := hW3'.1 + have hW4' : P1.WritesIn st y s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans (footW_trans F2 (footW_of fu3)) + (footW_of f1)) (footW_of f4) + -- back to `JAtZ` at the block + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) + Jy ht hth hkty hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hksy (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR (by rw [← hKy.2.1] at hIR4; exact hIR4) hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own Jy.own)) + (ps_off hkta hkwa hws (Fr5.off Jy.off)) (ps_nsc hkta hkwa hws (Fr5.nsc Jy.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted Jy.isorted)) htD + -- the reply's links at the block + obtain ⟨-, hn0, hp0⟩ := Ju.rfree rv hwR hfree0 + obtain ⟨-, -, -, -, k4⟩ := Ju.cnW_keeps hWu + have htD6 := J6.not_dead_of_st ht hkdP (by rw [hst2]; exact runnable_tsc0 _ hrun) + refine WP.spec_mono (J6.pushDonRJW hAT hsT ht hwR hpC (Ne.symm hts) hkP hkdP hkwP hkyP rfl hxP hytP hxd2 hdq + hiddP rfl hrelP hcnP hnqP hnqdP hyaP hdet (by rw [(k4 rv hwR).2]; exact hn0) + (by rw [(k4 rv hwR).1]; exact hp0) htD6) ?_ + rintro ⟨s5, a5⟩ ⟨ha5, hsp5, Lst5, J7, hwR5, hK5⟩ + simp only at ha5 + subst a5 + have Fy4 := JAtZB.natFoot_post Jy Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + have F45 := NatFoot.ofW J6.xtop hwR5 (fun b hb => RmW.oobj J6.xtop.fin.1.1.1 hb) + (fun b hb => J6.rmW_nv hb) (fun b hb => J6.rmW_nh hb) (natK_tk hK5) + exact ⟨rfl, _, hsp5, fun _ => ⟨Lst5, J7, Jy.natTrans Fy4 F45.cast⟩⟩ + rintro ⟨s5, a5⟩ ⟨ha5, σ5'', hpush, hPσ⟩ + simp only at ha5 hpush hPσ ⊢ + subst a5 + have e5'' : σ5'' = σD := by + have h := hpush.symm.trans hpushE + exact congrArg Prod.snd (Set.singleton_injective (congrArg Prod.fst h)) + obtain ⟨Lst5, J5, Fp⟩ := hPσ e5'' + subst e5'' + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hspec := receiveIpc_sendPushD_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inl hcl) (by rw [hxsp]; simp) (by rw [hfs]; simp) + (fun r hr => by rw [Notif.optRef_ne hw00] at hr; cases hr; exact hpushE) + exact ⟨((), σD), by rw [hspec]; exact Set.mem_singleton _, y, σy, Gy, _, _, _, hBase, hCnKy, Jy, _, _, _, _, J5, Fp⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFootD.lean b/proofs/Fl4Proofs/P3/VmRecvFootD.lean new file mode 100644 index 000000000..7115a4677 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFootD.lean @@ -0,0 +1,1014 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **footprint twins of the stable waiting-sender receives** +(generated by `handoff/tools/gen_recvfb.py` from P3JRecvStB/C/D: recvSendFTJSt:D recvSendFPTJSt:D). + +Each `…FB` theorem is its `…St` original with the same hypotheses and proof, except: the transfer step is +`JAtZB.xferStepGJXB` (its base step's data), the buffer rebase is `JAtZB.bufRebaseX` (its `NatFoot`), the receive-from-sender +frame step is `JAtZB.rsfrStep` (`natK_rsfr`), and the post is `RecvFootPost`. The `…St` statements are untouched. + +The timeout twins (`--tmo`, P3JRecvStD) have no transfer: their base state is the start state and the footprint runs from it. + +No named premise. New file. +-/ +import Fl4Proofs.P3.P3JRecvStB +import Fl4Proofs.P3.P3JRecvStC +import Fl4Proofs.P3.P3JRecvStD +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.VmRecvFoot1 +import Fl4Proofs.P3.VmRecvFoot2a +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.ScSignalBoundJ +import Fl4Proofs.P3.XTopJointRemove +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a head sender with a timeout fault and an SC at +`J`**, no reply pushed (module docstring), for any FL4 program `m` reducing +to the endpoint-state switch. -/ +theorem JAtZB.recvSendFTJFB (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {bb : Fl4Spec.Word} + (hft : xs.tcbFault = some (.timeout bb)) (hsn : xs.tcbSchedContext ≠ none) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hnp : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + (dd.senderCanGrant = false ∧ dd.senderCanGrantReply = false) ∨ rc = .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- the sender's SC and the base `J` state (its consumed time cleared, XTopJointFaultTmo's `tmoBaseJ`) + obtain ⟨sc0, hsc0⟩ := Option.ne_none_iff_exists'.1 hsn + obtain ⟨ys0, ns0, hky, -⟩ := J.inv.back sd'.bv xs sc0 hks hsc0 + obtain ⟨sc, hscC, rfl⟩ := J.obj.sc _ ys0 ns0 hky + obtain ⟨Vb, Jb, Fb⟩ := J.tmoRecvBaseJ hAT hmax htu hsT hscC hky + set σb := Ipc.tmoSt σ sc ys0 ns0 with hσb + have hscE : sc.bv ≠ e.bv := kheap_ne hky hk (fun _ h1 h2 => by subst h1; cases h2) + have hstS : sd'.bv ≠ sc.bv := kheap_ne hks hky (fun _ h1 h2 => by subst h1; cases h2) + have htS : t.bv ≠ sc.bv := kheap_ne hkt hky (fun _ h1 h2 => by subst h1; cases h2) + have hkb : ∀ q', q' ≠ sc.bv → σb.kheap q' = σ.kheap q' := fun q' hq' => by + simp [σb, Ipc.tmoSt, Sched.updSc, Notif.updK, hq'] + have hkbsc : σb.kheap sc.bv = some (.schedContext { ys0 with scConsumed := (0#u64 : U64).bv } ns0) := by + simp [σb, Ipc.tmoSt, Sched.updSc, Notif.updK] + have hksb : σb.kheap sd'.bv = some (.tcb xs) := by rw [hkb _ hstS]; exact hks + have hkbe : σb.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := by rw [hkb _ (Ne.symm hscE)]; exact hk + have hktb : σb.kheap t.bv = some (.tcb xt) := by rw [hkb _ htS]; exact hkt + have hzb : SzKeep σ σb := fun p y0 m hp => by + by_cases hps : p = sc.bv + · subst hps; rw [hkbsc] at hp; cases hp; exact ⟨_, hky⟩ + · exact ⟨y0, by rw [← hkb _ hps]; exact hp⟩ + have hz0 : SzKeepC Z.C σb σ := by + intro p _ y0 m hp + by_cases hps : p.bv = sc.bv + · rw [hps] at hp ⊢; rw [hky] at hp; cases hp; exact ⟨_, hkbsc⟩ + · exact ⟨y0, by rw [hkb _ hps]; exact hp⟩ + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hky1 : σ1.kheap sc.bv = some (.schedContext ys0 ns0) := by simp [σ1, hscE, hky] + have hsc1 : sc ∈ P1.Heap.scs H := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨_, (hJ.sc sc ys0 ns0 (J.c0 sc hscC) hky).2⟩ + -- the transfer: the timeout fault (XTopJointFaultTmo's `xferTmoStepJ`, on `IRel`) + refine WP.spec_bind (xferTmoStepJ hP0 (hH.epHyps hP0) hmax s1 a σ1 htu hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hsc0 hky1 e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 hsc1) ?_ + rintro s2 ⟨x2, e2, hx2, hR2, hS2', hA2, w2, ea2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Ipc.tmoSt σ1 sc ys0 ns0) t.bv (.tcb x2) with hσ2 + set σ1b := Notif.updK σb e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1b + have eσ2 : σ2 = Notif.updK σ1b t.bv (.tcb x2) := by + rw [hσ2, hσ1b, hσ1, hσb]; simp only [Ipc.tmoSt]; rw [updSc_updK_comm _ hscE] + have hkt1b : σ1b.kheap t.bv = some (.tcb xt) := by simp [σ1b, hte, hktb] + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by rw [eσ2]; simp + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by rw [eσ2]; simp [σ1b, hst', hse, hksb] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + rw [eσ2]; simp [σ1b, Ne.symm hte] + have hscw8 : SW Z.C σ (scW sc 8#u64) := sw_hdr hI.2.2.1 hscC (K := 8#u64) (by simp) (by simp) (by decide) + have hW2 : P1.WritesIn st s s2 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW1.trans1 (w2.mono fun b hb => by + rcases hb with ⟨eb, -⟩ | (⟨y0, hy, K, h1', h2', -, eb⟩ | hb) + · subst eb; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))) + · have hyT : y0 ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inr ⟨y0, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩) + rw [eb]; exact tfield_val (hP.tcb y0 hyT) (by omega) + · exact absurd hb (Set.notMem_empty _)) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ False) := + w2.mono fun b hb => by + rcases hb with ⟨-, c0, hc0, hin⟩ | (⟨y0, hy, K, h1', h2', h3', eb⟩ | hb) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨c0, hc0, hin⟩)))) + · exact Or.inl (Or.inl ⟨y0, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + -- the spec frame from the base `σb` + have Fr1 : RSFr sd'.bv t.bv σb σ1b := RSFr.upd hkbe (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv σ1b σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := σ1b) hkt1b x2.tcbArch + rw [← hx2, ← eσ2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σb σ2 := Fr1.trans Fr2c + have hz2 : SzKeepC Z.C σ σ2 := SzKeepC.of (fun p y m hp => by + obtain ⟨y1, h⟩ := Fr2.szKeep p y m hp; exact hzb p y1 m h) + have IK1 : IK sd'.bv σb σ1b := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hkbe + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σb σ1b := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σb σ2 := by + rw [eσ2]; exact IK1.trans (IK.updTcb (σ := σ1b) hkt1b (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2])) + have SK2 : Ipc.SKeep σb σ2 := by + rw [eσ2]; exact SK1.trans (Ipc.SKeep.updK (σ := σ1b) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1b, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h)) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jb.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + have hno := hnp _ _ hstd + have hnoS : ¬ ((dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none) := by + rintro ⟨hgg, hrn⟩ + rcases hno with ⟨hg0, hgr0⟩ | hnull + · rcases hgg with h | h + · rw [hg0] at h; cases h + · rw [hgr0] at h; cases h + · rcases hrc with ⟨-, -, hrv⟩ | ⟨_, _, hr, -⟩ + · exact hrn (by rw [hrv, optRef_zero]) + · rw [hnull] at hr; cases hr + suffices HH : (do + let s4 ← sched.set_thread_state st s3 sd' sched.TS_INACTIVE + ok (s4, a)) ⦃ r => ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + rcases hno with ⟨hg0, hgr0⟩ | hnull + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hg0, hgr, hgr0, Bool.false_eq_true, ↓reduceIte] + exact HH + · simp only [hci, hftB, hg, hg0, hgr, hgr0, Bool.false_eq_true, ↓reduceIte] + exact HH + · have hrv : rv = 0#u64 := by + rcases hrc with ⟨-, -, hrv⟩ | ⟨_, _, hr, -⟩ + · exact hrv + · rw [hnull] at hr; cases hr + subst hrv + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact HH + · simp only [hci, hftB, bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, ite_self] + exact HH + -- the sender `Inactive` + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqSb : NotQueued σb sd'.bv := hnqS + obtain ⟨hqb, hrb'⟩ := J.curBits hsT hnqS.1 hnqS.2 + have hna3 : L.actionW s3 ≠ sd' := by + refine actionW_ne_of hIR3.2.2.1 hsT (fun h => ?_) + have hsw := J.act.1 sd'.bv (show σ.schedulerAction = _ from act3.symm.trans h) + obtain ⟨x, -, -, -, hkx, -, hrx, -⟩ := schedulable_inv hsw + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + refine WP.spec_bind (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP s3 σ3 hR3 hS3 hsT hks3 hq3 + (by rw [hstd]; simp [tsCode]) .inactive (by simp [tsCode]) sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hIR3.2.2.1 hsT sched.TS_INACTIVE)) + (WP.spec_and (P1.sts_all s3 sd' _ Q0 R0 h3 hB3 hX3 hS3' hSg3 hsH P1.not_blocked_inactive P1.inactive_ne + (Or.inl (Or.inr rfl)) hnr3 + ⟨fun c hc' p hp hm => absurd hm (h0.notQ hsH hqb hc' hp), fun c hc' hm => absurd hm (h0.notR hsH hrb' hc'), + fun e0 => absurd e0 hna3⟩) + (P1.sched.set_thread_state.kw (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s3 sd' sched.TS_INACTIVE h3 hsH))) ?_ + rintro s5 ⟨⟨⟨hR5, hS5⟩, hW5⟩, ⟨h5, hB5, hX5, hS5', hSg5, -, -⟩, fu5⟩ + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + -- the footprints + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW2.trans1 (hW3.mono fun b hb => cnW_back hks2b hz2 (Or.inl (Or.inl hb))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW5' : P1.WritesIn st s s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW3'.trans1 (hW5.mono fun b hb => Or.inl (Or.inl (KFoot.of_sfoot ((SFoot.ksb hks3b).1 hb)))) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu5) + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s5 := + ⟨⟨Q0, R0, h5.congrX (P1.excT_congr hXc)⟩, hB5, hX5, hS5', hSg5⟩ + -- the frame + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr5 : RSFr sd'.bv t.bv σb σ5 := Fr2.trans (Fr3.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩))) + -- `Inv` at the end + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σb σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σb σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : ∀ x, σ5'.kheap sd'.bv = some (.tcb x) → inClassR x.tcbState x.tcbFault → RelIfBound σ5' sd'.bv := by + intro x hx hc + rw [hk5'] at hx; cases hx + exfalso + rcases hc with h | ⟨_, h⟩ | ⟨_, _, h, -⟩ | ⟨_, _, _, h⟩ <;> simp [hx5, Fl4Spec.runnable] at h + have I5' : Inv σ5' := ((Jb.inv.toBut sd'.bv).ik hnqSb IK5').1.inv hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqSb.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the footprints rebased onto the base (XTopJointFaultTmo's `writesIn_rebase`) + have hSσ := J.xtop.fin.1.1.1 + obtain ⟨Qs, Rs, hcs, hsps⟩ := hSσ.lists + obtain ⟨ysb, nsb, hkys, hys⟩ := hsps.scs sc hscC + rw [hky] at hkys; cases hkys + have hal8 : P1.Aligned (scW sc 8#u64) := (hys.hdr (K := 8#u64) (by simp) (by simp)).1 + have hw0 : P1.WritesIn st s (tmoS st s sc) (fun b => b = scW sc 8#u64) := writesIn_wr hal8 + have hWb : P1.WritesIn st (tmoS st s sc) s5 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c (tmoS st s sc) σb) := + (writesIn_rebase hW5' hw0 (fun b0 hb0 => by subst hb0; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))))).mono + fun b0 hb0 => CnW.back (s := tmoS st s sc) (s' := s) hw0.1.symm hz0 hb0 + have hfootb : P1.WritesIn st (tmoS st s sc) s5 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) (tmoS st s sc)) := + (writesIn_rebase hfoot hw0 (fun b0 hb0 => by + subst hb0; exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨sc, hsc1, scW8_inSC J.blk hsc1⟩)))))).mono + fun b0 hb0 => hb0.elim (P1.KW.ksb hw0.1.symm) False.elim + have hIb := Jb.xtop.irelK Jb.ks Jb.buf + have hF : IFrame (L := L) Z.th (tmoS st s sc) s5 := IRel.iframe_of hIb hWb fun b0 hb0 => CnW.oobj hIb.2.2.1 hb0 + have htD := Jb.not_dead_of_st hsT hksb (by rw [hstd]; simp [tsCode]) + have hIR5 := IpcRel.ofSRN hR5 hS5 + have J5 := Jb.rsfrStep hIR5 hF hWb hblk hfootb Fr5 I5 (Or.inl act5) htD + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hspec := receiveIpc_sendCall_eq b rights blk hsr hkt hbN hk hks hse hstd hnoS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) + have Fw := NatFoot.ofW Jb.xtop hWb (fun b0 hb0 => CnW.oobj hIb.2.2.1 hb0) (fun b0 hb0 => Jb.cnW_nv hb0) + (fun b0 hb0 => Jb.cnW_nh hb0) (natK_rsfr Fr5) + have Ft := J.natTrans Fb Fw.cast + refine (WP.spec_ok _).2 ⟨((), σ5), ?_, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Vb, _, _, _, J5, Ft.cast⟩ + rw [hspec]; exact Set.mem_singleton _ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a head sender with a timeout fault and an SC pushing +a reply at `J`** (module docstring; no donation), +has the grant or grant-reply right, the receiver passes a reply object +(covered, `hcovR`), at a resumed entry; for any FL4 program `m` reducing to +the endpoint-state switch. -/ +theorem JAtZB.recvSendFPTJFB (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {bb : Fl4Spec.Word} + (hft : xs.tcbFault = some (.timeout bb)) (hsn : xs.tcbSchedContext ≠ none) + (hAct : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue)) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- the sender's SC and the base `J` state (its consumed time cleared, XTopJointFaultTmo's `tmoBaseJ`) + obtain ⟨sc0, hsc0⟩ := Option.ne_none_iff_exists'.1 hsn + obtain ⟨ys0, ns0, hky, -⟩ := J.inv.back sd'.bv xs sc0 hks hsc0 + obtain ⟨sc, hscC, rfl⟩ := J.obj.sc _ ys0 ns0 hky + obtain ⟨Vb, Jb, Fb⟩ := J.tmoRecvBaseJ hAT hmax htu hsT hscC hky + set σb := Ipc.tmoSt σ sc ys0 ns0 with hσb + have hscE : sc.bv ≠ e.bv := kheap_ne hky hk (fun _ h1 h2 => by subst h1; cases h2) + have hstS : sd'.bv ≠ sc.bv := kheap_ne hks hky (fun _ h1 h2 => by subst h1; cases h2) + have htS : t.bv ≠ sc.bv := kheap_ne hkt hky (fun _ h1 h2 => by subst h1; cases h2) + have hkb : ∀ q', q' ≠ sc.bv → σb.kheap q' = σ.kheap q' := fun q' hq' => by + simp [σb, Ipc.tmoSt, Sched.updSc, Notif.updK, hq'] + have hkbsc : σb.kheap sc.bv = some (.schedContext { ys0 with scConsumed := (0#u64 : U64).bv } ns0) := by + simp [σb, Ipc.tmoSt, Sched.updSc, Notif.updK] + have hksb : σb.kheap sd'.bv = some (.tcb xs) := by rw [hkb _ hstS]; exact hks + have hkbe : σb.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := by rw [hkb _ (Ne.symm hscE)]; exact hk + have hktb : σb.kheap t.bv = some (.tcb xt) := by rw [hkb _ htS]; exact hkt + have hzb : SzKeep σ σb := fun p y0 m hp => by + by_cases hps : p = sc.bv + · subst hps; rw [hkbsc] at hp; cases hp; exact ⟨_, hky⟩ + · exact ⟨y0, by rw [← hkb _ hps]; exact hp⟩ + have hz0 : SzKeepC Z.C σb σ := by + intro p _ y0 m hp + by_cases hps : p.bv = sc.bv + · rw [hps] at hp ⊢; rw [hky] at hp; cases hp; exact ⟨_, hkbsc⟩ + · exact ⟨y0, by rw [hkb _ hps]; exact hp⟩ + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hky1 : σ1.kheap sc.bv = some (.schedContext ys0 ns0) := by simp [σ1, hscE, hky] + have hsc1 : sc ∈ P1.Heap.scs H := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨_, (hJ.sc sc ys0 ns0 (J.c0 sc hscC) hky).2⟩ + -- the transfer: the timeout fault (XTopJointFaultTmo's `xferTmoStepJ`, on `IRel`) + refine WP.spec_bind (xferTmoStepJ hP0 (hH.epHyps hP0) hmax s1 a σ1 htu hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hsc0 hky1 e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 hsc1) ?_ + rintro s2 ⟨x2, e2, hx2, hR2, hS2', hA2, w2, ea2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Ipc.tmoSt σ1 sc ys0 ns0) t.bv (.tcb x2) with hσ2 + set σ1b := Notif.updK σb e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1b + have eσ2 : σ2 = Notif.updK σ1b t.bv (.tcb x2) := by + rw [hσ2, hσ1b, hσ1, hσb]; simp only [Ipc.tmoSt]; rw [updSc_updK_comm _ hscE] + have hkt1b : σ1b.kheap t.bv = some (.tcb xt) := by simp [σ1b, hte, hktb] + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by rw [eσ2]; simp + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by rw [eσ2]; simp [σ1b, hst', hse, hksb] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + rw [eσ2]; simp [σ1b, Ne.symm hte] + have hscw8 : SW Z.C σ (scW sc 8#u64) := sw_hdr hI.2.2.1 hscC (K := 8#u64) (by simp) (by simp) (by decide) + have hW2 : P1.WritesIn st s s2 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW1.trans1 (w2.mono fun b hb => by + rcases hb with ⟨eb, -⟩ | (⟨y0, hy, K, h1', h2', -, eb⟩ | hb) + · subst eb; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))) + · have hyT : y0 ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inr ⟨y0, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩) + rw [eb]; exact tfield_val (hP.tcb y0 hyT) (by omega) + · exact absurd hb (Set.notMem_empty _)) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ False) := + w2.mono fun b hb => by + rcases hb with ⟨-, c0, hc0, hin⟩ | (⟨y0, hy, K, h1', h2', h3', eb⟩ | hb) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨c0, hc0, hin⟩)))) + · exact Or.inl (Or.inl ⟨y0, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + -- the spec frame from the base `σb` + have Fr1 : RSFr sd'.bv t.bv σb σ1b := RSFr.upd hkbe (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv σ1b σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := σ1b) hkt1b x2.tcbArch + rw [← hx2, ← eσ2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σb σ2 := Fr1.trans Fr2c + have hz2 : SzKeepC Z.C σ σ2 := SzKeepC.of (fun p y m hp => by + obtain ⟨y1, h⟩ := Fr2.szKeep p y m hp; exact hzb p y1 m h) + have IK1 : IK sd'.bv σb σ1b := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hkbe + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σb σ1b := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σb σ2 := by + rw [eσ2]; exact IK1.trans (IK.updTcb (σ := σ1b) hkt1b (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2])) + have SK2 : Ipc.SKeep σb σ2 := by + rw [eσ2]; exact SK1.trans (Ipc.SKeep.updK (σ := σ1b) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1b, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h)) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jb.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + have hty3 : L.mem s3 (tFaultA sd') = 5#u64 := by + have hfr := hxs3.faultR + unfold FaultRel at hfr; rw [hft] at hfr; exact hfr.1 + have hcdF : (ft != fl4_abi.consts.fault.Timeout) = false := by + rw [hft', hty3]; unfold fl4_abi.consts.fault.Timeout; decide + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) -- the sender is in no ready or release queue + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqSb : NotQueued σb sd'.bv := hnqS + -- the receiver: no donation (the sender's timeout fault) + have hA0 := hAct + have hCI : σ.schedulerAction = .resumeCurThread → CurInv σb := fun hres => + Jb.cur.resolve_left (fun h => by + have h' : σ.schedulerAction = .chooseNewThread := h + rw [hres] at h'; cases h') + have hnrT : t.bv ∉ σ.releaseQueue := by + rcases hA0 with hres | ⟨-, h⟩ + · rw [← hct]; exact (hCI hres).notRel + · exact h + have hsw : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u h => by + rcases hA0 with h' | ⟨h', -⟩ <;> rw [h'] at h <;> cases h + have hnqT : ∀ dm p, t.bv ∉ σ.readyQueues dm p := by rw [← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σb σ3 := Fr2.trans Fr3 + have hz23 : SzKeepC Z.C σ σ3 := SzKeepC.of (fun p y m hp => by + obtain ⟨y1, h⟩ := Fr23.szKeep p y m hp; exact hzb p y1 m h) + have hrq3 : σ3.readyQueues = σ.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkrb : σb.kheap rv.bv = some (.reply xr) := by + rw [hkb _ (kheap_ne hkr hky (fun _ h1 h2 => by subst h1; cases h2))]; exact hkr + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkrb + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σ.readyQueues dm p) → u.bv ∉ σ.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + show u.bv ∈ σb.releaseQueue + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σ.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hni + have his : sd'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hks] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (J.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ipc.reply_push st arI s3 a sd' t rv false ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 - + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3', hcdF]; exact ok_bind_spec _ _ HH + · rw [if_neg h3']; exact ok_bind_spec _ _ HH + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hrv0, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + refine WP.spec_mono (hPush false (Or.inl rfl)) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW2.trans1 (hW3.mono fun b hb => cnW_back hks2b hz2 (Or.inl (Or.inl hb))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW4' : P1.WritesIn st s s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW3'.trans1 (hW4.mono fun b hb => cnW_back hks3b hz23 hb) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σb σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σb σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σb σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : ∀ x, σ5'.kheap sd'.bv = some (.tcb x) → inClassR x.tcbState x.tcbFault → RelIfBound σ5' sd'.bv := by + intro x hx hc + rw [hk5'] at hx; cases hx + exfalso + rcases hc with h | ⟨_, h⟩ | ⟨_, _, h, -⟩ | ⟨_, _, _, h⟩ <;> simp [hx5, Fl4Spec.runnable] at h + have I5' : Inv σ5' := ((Jb.inv.toBut sd'.bv).ik hnqSb IK5').1.inv hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqSb.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact Jb.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hksb (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa Jb.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct]; show σ.curThread ≠ _; rw [hct]; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct]; show σ.curThread ≠ _; rw [hct]; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hactP] at hu; exact absurd hu (hsw u)), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK Jb.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (hactP.trans hcn) + have hCIa : CurInv σ5 := Fr5.curInv (hCI hres) + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (by rw [hactP, hcn]; intro h; cases h) + have hrun5 : CurRunW σ5 := Fr5.curRunW (Jb.curRun.resolve_left fun e0 => e0 hres) + exact Or.inr (by + rcases hrun5 with h | h + · refine Or.inl fun y hy => h y ?_ + rw [G'.ct, ps_ko hcne1 hcne2] at hy; exact hy + · exact Or.inr (by rw [G'.ct, G'.idle]; exact h)) + -- the footprints rebased onto the base (XTopJointFaultTmo's `writesIn_rebase`) + have hSσ := J.xtop.fin.1.1.1 + obtain ⟨Qs, Rs, hcs, hsps⟩ := hSσ.lists + obtain ⟨ysb, nsb, hkys, hys⟩ := hsps.scs sc hscC + rw [hky] at hkys; cases hkys + have hal8 : P1.Aligned (scW sc 8#u64) := (hys.hdr (K := 8#u64) (by simp) (by simp)).1 + have hw0 : P1.WritesIn st s (tmoS st s sc) (fun b => b = scW sc 8#u64) := writesIn_wr hal8 + have hWb : P1.WritesIn st (tmoS st s sc) s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c (tmoS st s sc) σb) := + (writesIn_rebase hW4' hw0 (fun b0 hb0 => by subst hb0; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))))).mono + fun b0 hb0 => CnW.back (s := tmoS st s sc) (s' := s) hw0.1.symm hz0 hb0 + have hfootb : P1.WritesIn st (tmoS st s sc) s4 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) (tmoS st s sc)) := + (writesIn_rebase hfoot hw0 (fun b0 hb0 => by + subst hb0; exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨sc, hsc1, scW8_inSC J.blk hsc1⟩)))))).mono + fun b0 hb0 => hb0.elim (P1.KW.ksb hw0.1.symm) False.elim + have hIb := Jb.xtop.irelK Jb.ks Jb.buf + have hF : IFrame (L := L) Z.th (tmoS st s sc) s4 := IRel.iframe_of hIb hWb fun b0 hb0 => CnW.oobj hIb.2.2.1 hb0 + have htD := Jb.not_dead_of_st hsT hksb (by rw [hstd]; simp [tsCode]) + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Jb.rsfr_postGR hIR4 hF hWb hblk hfootb Fr5 ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own Jb.own)) + (ps_off hkta hkwa hws (Fr5.off Jb.off)) (ps_nsc hkta hkwa hws (Fr5.nsc Jb.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted Jb.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r false σ3 = + ({((), σ')}, False) := by + intro r hr + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [hpush, heqP] + have hspec := receiveIpc_sendPushT_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) (by rw [hft]; rfl) hPS + have Fw := NatFoot.ofW Jb.xtop hWb (fun b0 hb0 => CnW.oobj hIb.2.2.1 hb0) (fun b0 hb0 => Jb.cnW_nv hb0) + (fun b0 hb0 => Jb.cnW_nh hb0) ((natK_rsfr Fr5).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + have Ft := J.natTrans Fb Fw.cast + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Vb, _, _, _, J6, Ft.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFootX.lean b/proofs/Fl4Proofs/P3/VmRecvFootX.lean new file mode 100644 index 000000000..ab0ce107b --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFootX.lean @@ -0,0 +1,2171 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **footprint twins of the stable waiting-sender receives** +(generated by `handoff/tools/gen_recvfb.py` from P3JRecvStB/C/D: recvSendPJGSt:B:C recvSendFPJGSt:B:C recvSendPJGSt:B:M recvSendFPJGSt:B:M). + +Each `…FB` theorem is its `…St` original with the same hypotheses and proof, except: the transfer step is +`JAtZB.xferStepGJXB` (its base step's data), the buffer rebase is `JAtZB.bufRebaseX` (its `NatFoot`), the receive-from-sender +frame step is `JAtZB.rsfrStep` (`natK_rsfr`), and the post is `RecvFootPost`. The `…St` statements are untouched. + +The timeout twins (`--tmo`, P3JRecvStD) have no transfer: their base state is the start state and the footprint runs from it. + +No named premise. New file. +-/ +import Fl4Proofs.P3.P3JRecvStB +import Fl4Proofs.P3.P3JRecvStC +import Fl4Proofs.P3.P3JRecvStD +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.VmRecvFoot1 +import Fl4Proofs.P3.VmRecvFoot2a +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.ScSignalBoundJ +import Fl4Proofs.P3.XTopJointRemove +import Fl4Proofs.P3.XTopJointRecvSendPM +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a calling head sender (no fault) pushing a reply at +`J`** (module docstring; XTopJointRecvSend's `JAtZB.recvSendJGFB` up to the +transfer, its intermediate `JAtZ` `Jy` at `σy`): the grant or grant-reply +right, a reply object (covered, `hcovR`), at a resumed entry. -/ +theorem JAtZB.recvSendPJGCFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) (hfs : xs.tcbFault = none) + (hAct : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue ∧ + (xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none))) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hcl0 : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → dd.senderIsCall = true) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.sendEP (sd'.bv :: q)) sd'.bv) hR1 hS1 hW1 F1 hsT ht (Ne.symm hts) + hks hkt hfs bdg (i != 0#u64) h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksb, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hksy : σy.kheap sd'.bv = some (.tcb xs) := hncn hks (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hks1y : σ1y.kheap sd'.bv = some (.tcb xs) := by simp [σ1y, hse, hksy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1y] + set σ2 := Notif.updK (Endp.setUm σ1y um2) t.bv (.tcb x2) with hσ2 + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1y] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1ey] + -- the spec frame from `σy` + have Fr1y : RSFr sd'.bv t.bv σy σ1y := RSFr.upd hky (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1y um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1y um2) hkt1y x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σy σ2 := (Fr1y.trans (RSFr.um _ _ σ1y um2)).trans Fr2c + have IK1y : IK sd'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hky + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : Ipc.SKeep σy σ1y := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans + (IK.updTcb (σ := Endp.setUm σ1y um2) hkt1y (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σy σ2 := SK1y.trans ((Fl4Proofs.P3.SKeep.um σ1y um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1y um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1y, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jy.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hfs]; unfold fl4_abi.consts.fault.NullFault; rfl + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + have hcl : dd.senderIsCall = true := hcl0 _ _ hstd + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + -- the sender is in no ready or release queue + have hnqS : NotQueued σy sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, with the current SC (so no donation) + have hA0 : σy.schedulerAction = .resumeCurThread ∨ + (σy.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue ∧ + (xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none)) := by + rw [hsay]; exact hAct + have hcty' : σy.curThread = t.bv := hcty.trans hct + have hCI : σy.schedulerAction = .resumeCurThread → CurInv σy := fun hres => + Jy.cur.resolve_left (by rw [hres]; intro h; cases h) + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + have hnd : xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none := by + rcases hA0 with hres | ⟨-, -, h⟩ + · obtain ⟨xc, hkc, hcs, -⟩ := (hCI hres).tcb + rw [hcty', hkty] at hkc; cases hkc; exact Or.inr (by rw [hcs]; simp) + · exact h + have hnrT : t.bv ∉ σy.releaseQueue := by + rcases hA0 with hres | ⟨-, h, -⟩ + · rw [← hcty']; exact (hCI hres).notRel + · rw [hrly]; exact h + have hsw : ∀ u, σy.schedulerAction ≠ .switchThread u := fun u h => by + rcases hA0 with h' | ⟨h', -⟩ <;> rw [h'] at h <;> cases h + have hnqT : ∀ dm p, t.bv ∉ σy.readyQueues dm p := by rw [hrqy, ← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σy σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σy.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkry : σy.kheap rv.bv = some (.reply xr) := hncn hkr (fun _ _ h0 => by cases h0) + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkry + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σy.readyQueues dm p) → u.bv ∉ σy.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σy.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [hidy, ← hct]; exact hni + have his : sd'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := Jy.act.2 + rw [← h, hksy] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := Jy.tcbs_eq.2 + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (Jy.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hscw : L.mem s3 (P1.scA sd') = 0#u64 ∨ L.mem s3 (P1.scA t) ≠ 0#u64 := by + rcases hnd with h | h + · exact Or.inl (optRef_none (hssc.symm.trans h)) + · exact Or.inr fun h0 => h (by rw [← hsc2, hdsc, h0, optRef_zero]) + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3'] + refine ok_bind_spec _ _ (HH _ (hscw.elim (fun h => ?_) Or.inr)) + rw [← hi3] at h; simp [h] at h3' + · rw [if_neg h3']; exact ok_bind_spec _ _ (HH _ (Or.inl rfl)) + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · simp only [hc, hcl, hg, hG, hrv0, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + simp only [hc, hcl, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + intro cd hcd + refine WP.spec_mono (hPush cd hcd) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb y := hW2.1 + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb y := hW3'.1 + have hW4' : P1.WritesIn st y s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans F2 (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σy.curThread; rw [hcty]; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σy σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σy σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σy σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd sd'.bv xs hksy (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => by rw [hfs] at h; cases h⟩)))) SK5' + have I5' : Inv σ5' := ((Jy.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact Jy.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hksy (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa Jy.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σy.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hactP] at hu; exact absurd hu (hsw u)), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK Jy.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (hactP.trans hcn) + have hCIa : CurInv σ5 := Fr5.curInv (hCI hres) + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (by rw [hactP, hcn]; intro h; cases h) + have hrun5 : CurRunW σ5 := Fr5.curRunW (Jy.curRun.resolve_left fun e0 => e0 hres) + exact Or.inr (by + rcases hrun5 with h | h + · refine Or.inl fun y hy => h y ?_ + rw [G'.ct, ps_ko hcne1 hcne2] at hy; exact hy + · exact Or.inr (by rw [G'.ct, G'.idle]; exact h)) + -- back to `JAtZ` + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) + Jy ht hth hkty hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hksy (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR (by rw [← hKy.2.1] at hIR4; exact hIR4) hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own Jy.own)) + (ps_off hkta hkwa hws (Fr5.off Jy.off)) (ps_nsc hkta hkwa hws (Fr5.nsc Jy.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted Jy.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → ∀ bd, Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r bd σ3 = + ({((), σ')}, False) := by + intro r hr bd + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [replyPush_don_irrel hks3 hkt3 (hnd.imp id fun h => by rw [hsc2]; exact h) bd cd, hpush, heqP] + have hspec := receiveIpc_sendPush_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inl hcl) hPS + have Fy6 := JAtZB.natFoot_post Jy Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, y, σy, Gy, _, _, _, hBase, hCnKy, Jy, _, _, _, _, J6, Fy6⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a faulted head sender pushing a reply at `J`** +(module docstring): the sender holds a fault (a timeout only without an SC), +has the grant or grant-reply right, the receiver passes a reply object +(covered, `hcovR`), at a resumed entry; for any FL4 program `m` reducing to +the endpoint-state switch. -/ +theorem JAtZB.recvSendFPJGCFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {f : Fl4Spec.Fault} + (hft : xs.tcbFault = some f) (hto : ∀ b, f = .timeout b → xs.tcbSchedContext = none) + (hAct : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ t.bv ∉ σ.releaseQueue ∧ + (xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none))) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hUt := J.rbufU ht hth hkt + have hok : FaultOK (L := L) s1 sd' xs f := by + cases f with + | timeout b0 => + have hsc := hto b0 rfl + obtain ⟨Q, R0, -, hsp⟩ := hS1.1.1.2.1.lists + obtain ⟨x, hk', -, -, hscw, -⟩ := hsp.tcb sd' hsT + rw [hks1] at hk'; cases hk' + exact ⟨hsc, optRef_none (hscw.symm.trans hsc)⟩ + | _ => trivial + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hok e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUt b hb).1.ksb hW1.1, (hUt b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) t.bv (.tcb x2) with hσ2 + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1e] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 ht hA2 (fun p hp => by simp [σ2, hp]) hkt1 hkt2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xt b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + -- the spec frame from `σ` + have Fr1 : RSFr sd'.bv t.bv σ σ1 := RSFr.upd hk (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1 um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1 um2) hkt1 x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σ σ2 := (Fr1.trans (RSFr.um _ _ σ1 um2)).trans Fr2c + have IK1 : IK sd'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hk + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans + (IK.updTcb (σ := Endp.setUm σ1 um2) hkt1 (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((Fl4Proofs.P3.SKeep.um σ1 um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1 um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => J.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) -- the sender is in no ready or release queue + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, with the current SC (so no donation) + have hA0 := hAct + have hCI : σ.schedulerAction = .resumeCurThread → CurInv σ := fun hres => + J.cur.resolve_left (by rw [hres]; intro h; cases h) + have hnd : xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none := by + rcases hA0 with hres | ⟨-, -, h⟩ + · obtain ⟨xc, hkc, hcs, -⟩ := (hCI hres).tcb + rw [hct, hkt] at hkc; cases hkc; exact Or.inr (by rw [hcs]; simp) + · exact h + have hnrT : t.bv ∉ σ.releaseQueue := by + rcases hA0 with hres | ⟨-, h, -⟩ + · rw [← hct]; exact (hCI hres).notRel + · exact h + have hsw : ∀ u, σ.schedulerAction ≠ .switchThread u := fun u h => by + rcases hA0 with h' | ⟨h', -⟩ <;> rw [h'] at h <;> cases h + have hnqT : ∀ dm p, t.bv ∉ σ.readyQueues dm p := by rw [← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σ σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σ.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkr + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σ.readyQueues dm p) → u.bv ∉ σ.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, L.actionW s3 ≠ u := fun u hu => + actionW_ne_of hSR3 hu (fun h => hsw _ (act3.symm.trans h)) + have hidl3 : σ3.idleThread = σ.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hni + have his : sd'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hks] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (J.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hscw : L.mem s3 (P1.scA sd') = 0#u64 ∨ L.mem s3 (P1.scA t) ≠ 0#u64 := by + rcases hnd with h | h + · exact Or.inl (optRef_none (hssc.symm.trans h)) + · exact Or.inr fun h0 => h (by rw [← hsc2, hdsc, h0, optRef_zero]) + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3'] + refine ok_bind_spec _ _ (HH _ (hscw.elim (fun h => ?_) Or.inr)) + rw [← hi3] at h; simp [h] at h3' + · rw [if_neg h3']; exact ok_bind_spec _ _ (HH _ (Or.inl rfl)) + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hrv0, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + intro cd hcd + refine WP.spec_mono (hPush cd hcd) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σ σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σ σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σ σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := by + by_cases htf : ∃ b0, f = .timeout b0 + · obtain ⟨b0, rfl⟩ := htf + intro y scp z n hy hscp + rw [hk5'] at hy + simp only [Option.some.injEq, Fl4Spec.KernelObject.tcb.injEq] at hy + subst hy + rw [hx5] at hscp + simp [hto b0 rfl] at hscp + · have hnt : ∀ b', xs.tcbFault ≠ some (.timeout b') := fun b' h => + htf ⟨b', Option.some.inj (hft.symm.trans h)⟩ + exact Ipc.RelIfBound.of_keep (J.inv.rd sd'.bv xs hks (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => hnt b' h⟩)))) SK5' + have I5' : Inv σ5' := ((J.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact J.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hks (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa J.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => (by rw [hactP] at hu; exact absurd hu (hsw u)), ps_idleOK hkta hkwa hws hidT (Fr5.idleOK J.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (hactP.trans hcn) + have hCIa : CurInv σ5 := Fr5.curInv (hCI hres) + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := by + rcases hA0 with hres | ⟨hcn, -⟩ + swap + · exact Or.inl (by rw [hactP, hcn]; intro h; cases h) + have hrun5 : CurRunW σ5 := Fr5.curRunW (J.curRun.resolve_left fun e0 => e0 hres) + exact Or.inr (by + rcases hrun5 with h | h + · refine Or.inl fun y hy => h y ?_ + rw [G'.ct, ps_ko hcne1 hcne2] at hy; exact hy + · exact Or.inr (by rw [G'.ct, G'.idle]; exact h)) + -- back to `JAtZ` + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := J.bufRebaseX ht hth hkt hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hks (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR hIR4 hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own J.own)) + (ps_off hkta hkwa hws (Fr5.off J.off)) (ps_nsc hkta hkwa hws (Fr5.nsc J.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted J.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → ∀ bd, Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r bd σ3 = + ({((), σ')}, False) := by + intro r hr bd + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [replyPush_don_irrel hks3 hkt3 (hnd.imp id fun h => by rw [hsc2]; exact h) bd cd, hpush, heqP] + have hspec := receiveIpc_sendPush_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) hPS + have Fy6 := JAtZB.natFoot_post J Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Z.vs, _, _, _, J6, Fy6.cast⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a calling head sender (no fault) pushing a reply at +`J`** (module docstring; XTopJointRecvSend's `JAtZB.recvSendJGFB` up to the +transfer, its intermediate `JAtZ` `Jy` at `σy`): the grant or grant-reply +right, a reply object (covered, `hcovR`), at a resumed entry. -/ +theorem JAtZB.recvSendPJGMFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) (hfs : xs.tcbFault = none) + (hAct : ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ t.bv) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hcl0 : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → dd.senderIsCall = true) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer + refine WP.spec_bind (J.xferStepGJXB hk (epDeq (.sendEP (sd'.bv :: q)) sd'.bv) hR1 hS1 hW1 F1 hsT ht (Ne.symm hts) + hks hkt hfs bdg (i != 0#u64) h1 hB1 hX1 hS1' hSg1) ?_ + rintro s2 ⟨x2, um2, y, σy, Gy, Jy, hBase, e2, hx2, hR2, hS2', hA2, hW2, F2, h2, hB2, hX2, hS2'', hSg2, f2, hksb, hKy, hCnKy, hyk⟩ + have hncn : ∀ {q' : Fl4Spec.ObjRef} {o' : Fl4Spec.KernelObject}, σ.kheap q' = some o' → + (∀ r cs, o' ≠ .cnode r cs) → σy.kheap q' = some o' := + fun hq ho => (hyk _ fun r cs h0 => ho r cs (by rw [hq] at h0; exact Option.some.inj h0)).trans hq + have hky : σy.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := hncn hk (fun _ _ h0 => by cases h0) + have hksy : σy.kheap sd'.bv = some (.tcb xs) := hncn hks (fun _ _ h0 => by cases h0) + have hkty : σy.kheap t.bv = some (.tcb xt) := hncn hkt (fun _ _ h0 => by cases h0) + have hcty : σy.curThread = σ.curThread := hKy.2.2.2.2 + set σ1y := Notif.updK σy e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1y + have hkt1y : σ1y.kheap t.bv = some (.tcb xt) := by simp [σ1y, hte, hkty] + have hks1y : σ1y.kheap sd'.bv = some (.tcb xs) := by simp [σ1y, hse, hksy] + have hk1ey : σ1y.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1y] + set σ2 := Notif.updK (Endp.setUm σ1y um2) t.bv (.tcb x2) with hσ2 + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1y] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1ey] + -- the spec frame from `σy` + have Fr1y : RSFr sd'.bv t.bv σy σ1y := RSFr.upd hky (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1y um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1y um2) hkt1y x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σy σ2 := (Fr1y.trans (RSFr.um _ _ σ1y um2)).trans Fr2c + have IK1y : IK sd'.bv σy σ1y := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hky + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1y : Ipc.SKeep σy σ1y := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σy σ2 := IK1y.trans ((IK.um _ σ1y um2).trans + (IK.updTcb (σ := Endp.setUm σ1y um2) hkt1y (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σy σ2 := SK1y.trans ((Fl4Proofs.P3.SKeep.um σ1y um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1y um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1y, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jy.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft + have hft0 : ft = fl4_abi.consts.fault.NullFault := by + rw [hft, hxs3.fault0.2 hfs]; unfold fl4_abi.consts.fault.NullFault; rfl + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) + have hcl : dd.senderIsCall = true := hcl0 _ _ hstd + have hsay : σy.schedulerAction = σ.schedulerAction := by + have e := congrArg Fl4Spec.AbstractState.schedulerAction hCnKy.1; exact e + have hidy : σy.idleThread = σ.idleThread := by + have e := congrArg Fl4Spec.AbstractState.idleThread hCnKy.1; exact e + have hrqy : σy.readyQueues = σ.readyQueues := by + have e := congrArg Fl4Spec.AbstractState.readyQueues hCnKy.1; exact e + -- the sender is in no ready or release queue + have hnqS : NotQueued σy sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rq dm p _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := Jy.inv.rel _ hm + rw [hksy] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, with the current SC (so no donation) + have hAy : ∃ u, σy.schedulerAction = .switchThread u ∧ u ≠ t.bv := by rw [hsay]; exact hAct + have hcty' : σy.curThread = t.bv := hcty.trans hct + have hCI : CurInv σy := Jy.curInv (by obtain ⟨u0, hu0, -⟩ := hAy; rw [hu0]; intro h; cases h) + have hrly : σy.releaseQueue = σ.releaseQueue := by + have e := congrArg Fl4Spec.AbstractState.releaseQueue hCnKy.1; exact e + have hnd : xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none := by + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hcty', hkty] at hkc; cases hkc; exact Or.inr (by rw [hcs]; simp) + have hnrT : t.bv ∉ σy.releaseQueue := by rw [← hcty']; exact hCI.notRel + have hswT : ∀ u, σy.schedulerAction = .switchThread u → u ≠ t.bv ∧ u ≠ sd'.bv := fun u hu => by + obtain ⟨u0, hu0, hne⟩ := hAy + rw [hu0] at hu; cases hu + refine ⟨hne, fun e0 => ?_⟩ + obtain ⟨x0, -, -, -, hk0, -, hr0, -, -, -⟩ := schedulable_inv (Jy.act.1 _ hu0) + rw [e0, hksy] at hk0; cases hk0; rw [hstd] at hr0; cases hr0 + have hnqT : ∀ dm p, t.bv ∉ σy.readyQueues dm p := by rw [hrqy, ← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σy σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σy.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkry : σy.kheap rv.bv = some (.reply xr) := hncn hkr (fun _ _ h0 => by cases h0) + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkry + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σy.readyQueues dm p) → u.bv ∉ σy.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, (u = sd' ∨ u = t) → L.actionW s3 ≠ u := fun u hu hut => + actionW_ne_of hSR3 hu (fun h => by + obtain ⟨h1, h2⟩ := hswT _ (act3.symm.trans h) + rcases hut with rfl | rfl + · exact h2 rfl + · exact h1 rfl) + have hidl3 : σ3.idleThread = σy.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σy.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σy.idleThread := by rw [hidy, ← hct]; exact hni + have his : sd'.bv ≠ σy.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := Jy.act.2 + rw [← h, hksy] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := Jy.tcbs_eq.2 + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (Jy.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hscw : L.mem s3 (P1.scA sd') = 0#u64 ∨ L.mem s3 (P1.scA t) ≠ 0#u64 := by + rcases hnd with h | h + · exact Or.inl (optRef_none (hssc.symm.trans h)) + · exact Or.inr fun h0 => h (by rw [← hsc2, hdsc, h0, optRef_zero]) + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT (Or.inl rfl)) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht (Or.inr rfl)) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT (Or.inl rfl)) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3'] + refine ok_bind_spec _ _ (HH _ (hscw.elim (fun h => ?_) Or.inr)) + rw [← hi3] at h; simp [h] at h3' + · rw [if_neg h3']; exact ok_bind_spec _ _ (HH _ (Or.inl rfl)) + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · simp only [hc, hcl, hg, hG, hrv0, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + simp only [hc, hcl, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + intro cd hcd + refine WP.spec_mono (hPush cd hcd) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb y := hW2.1 + have hW3' : P1.WritesIn st y s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb y := hW3'.1 + have hW4' : P1.WritesIn st y s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans F2 (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σy.curThread; rw [hcty]; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σy σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σy σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σy σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := + Ipc.RelIfBound.of_keep (Jy.inv.rd sd'.bv xs hksy (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => by rw [hfs] at h; cases h⟩)))) SK5' + have I5' : Inv σ5' := ((Jy.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σy.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact Jy.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hksy (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa Jy.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct, hcty']; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σy.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => by + rw [hactP] at hu + have hs5 := Fr5.sched (Jy.act.1 u hu) + have hur : u ≠ rv.bv := fun e0 => by + obtain ⟨x0, -, -, -, hk0, -⟩ := schedulable_inv hs5 + rw [e0, hkwa] at hk0; cases hk0 + exact ps_schedM hkta hkwa (hswT u hu).2 hur hs5, ps_idleOK hkta hkwa hws hidT (Fr5.idleOK Jy.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + have hCIa : CurInv σ5 := Fr5.curInv hCI + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := Or.inl (by + rw [hactP]; obtain ⟨u0, hu0, -⟩ := hAy; rw [hu0]; intro h; cases h) + -- back to `JAtZ` + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := JAtZB.bufRebaseX (Z := Z.mv Gy Z.vs) + Jy ht hth hkty hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hksy (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR (by rw [← hKy.2.1] at hIR4; exact hIR4) hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own Jy.own)) + (ps_off hkta hkwa hws (Fr5.off Jy.off)) (ps_nsc hkta hkwa hws (Fr5.nsc Jy.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted Jy.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → ∀ bd, Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r bd σ3 = + ({((), σ')}, False) := by + intro r hr bd + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [replyPush_don_irrel hks3 hkt3 (hnd.imp id fun h => by rw [hsc2]; exact h) bd cd, hpush, heqP] + have hspec := receiveIpc_sendPush_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inl hcl) hPS + have Fy6 := JAtZB.natFoot_post Jy Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, y, σy, Gy, _, _, _, hBase, hCnKy, Jy, _, _, _, _, J6, Fy6⟩ + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a faulted head sender pushing a reply at `J`** +(module docstring): the sender holds a fault (a timeout only without an SC), +has the grant or grant-reply right, the receiver passes a reply object +(covered, `hcovR`), at a resumed entry; for any FL4 program `m` reducing to +the endpoint-state switch. -/ +theorem JAtZB.recvSendFPJGMFB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {f : Fl4Spec.Fault} + (hft : xs.tcbFault = some f) (hto : ∀ b, f = .timeout b → xs.tcbSchedContext = none) + (hAct : ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ t.bv) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have hth : Z.th t = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := hS0.1.1 + exact Ipc.RelTopB.tcbT hB0' hkt + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hUt := J.rbufU ht hth hkt + have hok : FaultOK (L := L) s1 sd' xs f := by + cases f with + | timeout b0 => + have hsc := hto b0 rfl + obtain ⟨Q, R0, -, hsp⟩ := hS1.1.1.2.1.lists + obtain ⟨x, hk', -, -, hscw, -⟩ := hsp.tcb sd' hsT + rw [hks1] at hk'; cases hk' + exact ⟨hsc, optRef_none (hscw.symm.trans hsc)⟩ + | _ => trivial + refine WP.spec_bind (xferFaultStepGJ hP0 hP (hH.epHyps hP0) s1 a σ1 hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hok e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 (fun b hb => ⟨(hUt b hb).1.ksb hW1.1, (hUt b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Endp.setUm σ1 um2) t.bv (.tcb x2) with hσ2 + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by simp [σ2] + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by simp [σ2, hst', hks1] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + simp [σ2, Ne.symm hte, hk1e] + have hN2 := ntfn_lift_rcv hP0.toC hS1.2 hR2 ht hA2 (fun p hp => by simp [σ2, hp]) hkt1 hkt2 (by rw [hx2]) + have hS2' : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 σ2 := ⟨hS2.1, hN2⟩ + have hW2 : P1.WritesIn st s s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', -, eb⟩ | hb) | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩)) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ RcvBufW xt b) := + w2.mono fun b hb => by + rcases hb with (⟨y, hy, K, h1', h2', h3', eb⟩ | hb) | hb + · exact Or.inl (Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + · exact Or.inr hb + -- the spec frame from `σ` + have Fr1 : RSFr sd'.bv t.bv σ σ1 := RSFr.upd hk (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv (Endp.setUm σ1 um2) σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := Endp.setUm σ1 um2) hkt1 x2.tcbArch + rw [← hx2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σ σ2 := (Fr1.trans (RSFr.um _ _ σ1 um2)).trans Fr2c + have IK1 : IK sd'.bv σ σ1 := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hk + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σ σ1 := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σ σ2 := IK1.trans ((IK.um _ σ1 um2).trans + (IK.updTcb (σ := Endp.setUm σ1 um2) hkt1 (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2]))) + have SK2 : Ipc.SKeep σ σ2 := SK1.trans ((Fl4Proofs.P3.SKeep.um σ1 um2).trans (Ipc.SKeep.updK (σ := Endp.setUm σ1 um2) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h))) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => J.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) -- the sender is in no ready or release queue + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + -- the receiver: current, with the current SC (so no donation) + have hCI : CurInv σ := J.curInv (by obtain ⟨u0, hu0, -⟩ := hAct; rw [hu0]; intro h; cases h) + have hnd : xs.tcbSchedContext = none ∨ xt.tcbSchedContext ≠ none := by + obtain ⟨xc, hkc, hcs, -⟩ := hCI.tcb + rw [hct, hkt] at hkc; cases hkc; exact Or.inr (by rw [hcs]; simp) + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + have hswT : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t.bv ∧ u ≠ sd'.bv := fun u hu => by + obtain ⟨u0, hu0, hne⟩ := hAct + rw [hu0] at hu; cases hu + refine ⟨hne, fun e0 => ?_⟩ + obtain ⟨x0, -, -, -, hk0, -, hr0, -, -, -⟩ := schedulable_inv (J.act.1 _ hu0) + rw [e0, hks] at hk0; cases hk0; rw [hstd] at hr0; cases hr0 + have hnqT : ∀ dm p, t.bv ∉ σ.readyQueues dm p := by rw [← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σ σ3 := Fr2.trans Fr3 + have hrq3 : σ3.readyQueues = σ.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkr + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σ.readyQueues dm p) → u.bv ∉ σ.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, (u = sd' ∨ u = t) → L.actionW s3 ≠ u := fun u hu hut => + actionW_ne_of hSR3 hu (fun h => by + obtain ⟨h1, h2⟩ := hswT _ (act3.symm.trans h) + rcases hut with rfl | rfl + · exact h2 rfl + · exact h1 rfl) + have hidl3 : σ3.idleThread = σ.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hni + have his : sd'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hks] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (J.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hscw : L.mem s3 (P1.scA sd') = 0#u64 ∨ L.mem s3 (P1.scA t) ≠ 0#u64 := by + rcases hnd with h | h + · exact Or.inl (optRef_none (hssc.symm.trans h)) + · exact Or.inr fun h0 => h (by rw [← hsc2, hdsc, h0, optRef_zero]) + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT (Or.inl rfl)) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht (Or.inr rfl)) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT (Or.inl rfl)) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 hi3 + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3'] + refine ok_bind_spec _ _ (HH _ (hscw.elim (fun h => ?_) Or.inr)) + rw [← hi3] at h; simp [h] at h3' + · rw [if_neg h3']; exact ok_bind_spec _ _ (HH _ (Or.inl rfl)) + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hrv0, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + intro cd hcd + refine WP.spec_mono (hPush cd hcd) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW2.trans1 (hW3.mono fun b hb => Or.inl (cnW_back hks2b (SzKeepC.of Fr2.szKeep) (Or.inl (Or.inl hb)))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW4' : P1.WritesIn st s s4 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ b ∨ RcvBufW xt b) := + hW3'.trans1 (hW4.mono fun b hb => Or.inl (cnW_back hks3b (SzKeepC.of Fr23.szKeep) hb)) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σ σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σ σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σ σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : RelIfBound σ5' sd'.bv := by + by_cases htf : ∃ b0, f = .timeout b0 + · obtain ⟨b0, rfl⟩ := htf + intro y scp z n hy hscp + rw [hk5'] at hy + simp only [Option.some.injEq, Fl4Spec.KernelObject.tcb.injEq] at hy + subst hy + rw [hx5] at hscp + simp [hto b0 rfl] at hscp + · have hnt : ∀ b', xs.tcbFault ≠ some (.timeout b') := fun b' h => + htf ⟨b', Option.some.inj (hft.symm.trans h)⟩ + exact Ipc.RelIfBound.of_keep (J.inv.rd sd'.bv xs hks (Or.inr (Or.inr (Or.inl ⟨_, _, hstd, + fun b' h => hnt b' h⟩)))) SK5' + have I5' : Inv σ5' := ((J.inv.toBut sd'.bv).ik hnqS IK5').1.inv fun _ _ _ => hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqS.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact J.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hks (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa J.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct, hct]; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => by + rw [hactP] at hu + have hs5 := Fr5.sched (J.act.1 u hu) + have hur : u ≠ rv.bv := fun e0 => by + obtain ⟨x0, -, -, -, hk0, -⟩ := schedulable_inv hs5 + rw [e0, hkwa] at hk0; cases hk0 + exact ps_schedM hkta hkwa (hswT u hu).2 hur hs5, ps_idleOK hkta hkwa hws hidT (Fr5.idleOK J.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + have hCIa : CurInv σ5 := Fr5.curInv hCI + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := Or.inl (by + rw [hactP]; obtain ⟨u0, hu0, -⟩ := hAct; rw [hu0]; intro h; cases h) + -- back to `JAtZ` + obtain ⟨su, umu, Ju0, hWu, hFu, -, -, Fsu⟩ := J.bufRebaseX ht hth hkt hW4' hfoot + have Ju := Ju0 + have hIu := Ju.xtop.irelK Ju.ks Ju.buf + have htD := Ju.not_dead_of_st hsT hks (by rw [hstd]; simp [tsCode]) + have hF : IFrame (L := L) Z.th su s4 := IRel.iframe_of hIu hWu fun b hb => CnW.oobj hIu.2.2.1 hb + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Ju.rsfr_postGR hIR4 hF hWu hblk hFu (RSFr.ofUm Fr5 umu) ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own J.own)) + (ps_off hkta hkwa hws (Fr5.off J.off)) (ps_nsc hkta hkwa hws (Fr5.nsc J.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted J.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → ∀ bd, Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r bd σ3 = + ({((), σ')}, False) := by + intro r hr bd + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [replyPush_don_irrel hks3 hkt3 (hnd.imp id fun h => by rw [hsc2]; exact h) bd cd, hpush, heqP] + have hspec := receiveIpc_sendPush_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) hPS + have Fy6 := JAtZB.natFoot_post J Fsu Ju0 hWu ((natK_rsfr (RSFr.ofUm Fr5 umu)).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Z.vs, _, _, _, J6, Fy6.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvFootXT.lean b/proofs/Fl4Proofs/P3/VmRecvFootXT.lean new file mode 100644 index 000000000..152b638d0 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvFootXT.lean @@ -0,0 +1,618 @@ +/- +P3 (line mmuAD, Stage 3 step 3, `RecvSenderPrefixes` leaves): **footprint twins of the stable waiting-sender receives** +(generated by `handoff/tools/gen_recvfb.py` from P3JRecvStB/C/D: recvSendFPTJSt:D:M). + +Each `…FB` theorem is its `…St` original with the same hypotheses and proof, except: the transfer step is +`JAtZB.xferStepGJXB` (its base step's data), the buffer rebase is `JAtZB.bufRebaseX` (its `NatFoot`), the receive-from-sender +frame step is `JAtZB.rsfrStep` (`natK_rsfr`), and the post is `RecvFootPost`. The `…St` statements are untouched. + +The timeout twins (`--tmo`, P3JRecvStD) have no transfer: their base state is the start state and the footprint runs from it. + +No named premise. New file. +-/ +import Fl4Proofs.P3.P3JRecvStB +import Fl4Proofs.P3.P3JRecvStC +import Fl4Proofs.P3.P3JRecvStD +import Fl4Proofs.P3.P3JSendFootBase +import Fl4Proofs.P3.XTopJointXferGB +import Fl4Proofs.P3.VmRecvFoot0 +import Fl4Proofs.P3.VmRecvFoot1 +import Fl4Proofs.P3.VmRecvFoot2a +import Fl4Proofs.P3.NatFoot +import Fl4Proofs.P3.ScSignalBoundJ +import Fl4Proofs.P3.XTopJointRemove +import Fl4Proofs.P3.XTopJointRecvSendPM +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +noncomputable section + +section +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- (p3J gen 31; stable ghosts, `JStab`) **`receive_ipc` from a head sender with a timeout fault and an SC pushing +a reply at `J`** (module docstring; no donation), +has the grant or grant-reply right, the receiver passes a reply object +(covered, `hcovR`), at a resumed entry; for any FL4 program `m` reducing to +the endpoint-state switch. -/ +theorem JAtZB.recvSendFPTJMFB (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hrun : Fl4Spec.runnable xt.tcbState = true) {blk : Bool} + (hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false ∧ + (blk = true → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none))) + (hct : σ.curThread = t.bv) (hni : σ.curThread ≠ σ.idleThread) (hnq : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p) + {e sd : U64} (he : e ∈ Z.E) {q : List Fl4Spec.ObjRef} + (hk : σ.kheap e.bv = some (.endpoint (.sendEP (sd.bv :: q)))) + {xs : Fl4Spec.Tcb} (hks : σ.kheap sd.bv = some (.tcb xs)) {bb : Fl4Spec.Word} + (hft : xs.tcbFault = some (.timeout bb)) (hsn : xs.tcbSchedContext ≠ none) + (hAct : ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ t.bv) + (cb : U64) (c1 c2 c3 c4 : Bool) {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} + (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (hyes : ∀ e' dd, xs.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true) (hrn : rc ≠ .Null) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + : + endpoint.receive_from_sender st arI s a t e rv ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + have J := J.undeadC hcovR.1 hcovR.2 (J.replyArg_ntcb hrc) + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hR := hI.1.toE + have hS0 := IpcRel.toSRN hI.1 hI.2 + have hx := hR.at_ep he hk + have hxt := hR.at_tcb ht hkt + have hsr := replyArg_spec hP hR hrc + obtain ⟨hst1, xs0, hxs0, hq, hQ, hB⟩ := hx + 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 := eq_of_bv hd'.symm + subst hdd + have hsT : sd' ∈ Z.T := hQ.mem sd' (by simp) + have hsa := hP.tcb sd' hsT + have hxs := hR.at_tcb hsT hks + have hs4 : (L.mem s (tStA 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' = e.bv := 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 hnb : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + constructor + · intro h0; obtain ⟨_, _, _, h1⟩ := tsCode_three h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + · intro h0; obtain ⟨_, _, h1⟩ := tsCode_four h0; rw [h1] at hrun; simp [Fl4Spec.runnable] at hrun + have hts : t ≠ sd' := by intro h; subst h; exact hnb.2 (hxt.st.symm.trans hs4) + have hse : sd'.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hte : t.bv ≠ e.bv := kheap_ne hkt hk (fun _ h1 h2 => by subst h1; cases h2) + have hst' : sd'.bv ≠ t.bv := bv_ne (Ne.symm hts) + have hsc : sd'.bv ≠ σ.curThread := by rw [hct]; exact hst' + -- the sender's SC and the base `J` state (its consumed time cleared, XTopJointFaultTmo's `tmoBaseJ`) + obtain ⟨sc0, hsc0⟩ := Option.ne_none_iff_exists'.1 hsn + obtain ⟨ys0, ns0, hky, -⟩ := J.inv.back sd'.bv xs sc0 hks hsc0 + obtain ⟨sc, hscC, rfl⟩ := J.obj.sc _ ys0 ns0 hky + obtain ⟨Vb, Jb, Fb⟩ := J.tmoRecvBaseJ hAT hmax htu hsT hscC hky + set σb := Ipc.tmoSt σ sc ys0 ns0 with hσb + have hscE : sc.bv ≠ e.bv := kheap_ne hky hk (fun _ h1 h2 => by subst h1; cases h2) + have hstS : sd'.bv ≠ sc.bv := kheap_ne hks hky (fun _ h1 h2 => by subst h1; cases h2) + have htS : t.bv ≠ sc.bv := kheap_ne hkt hky (fun _ h1 h2 => by subst h1; cases h2) + have hkb : ∀ q', q' ≠ sc.bv → σb.kheap q' = σ.kheap q' := fun q' hq' => by + simp [σb, Ipc.tmoSt, Sched.updSc, Notif.updK, hq'] + have hkbsc : σb.kheap sc.bv = some (.schedContext { ys0 with scConsumed := (0#u64 : U64).bv } ns0) := by + simp [σb, Ipc.tmoSt, Sched.updSc, Notif.updK] + have hksb : σb.kheap sd'.bv = some (.tcb xs) := by rw [hkb _ hstS]; exact hks + have hkbe : σb.kheap e.bv = some (.endpoint (.sendEP (sd'.bv :: q))) := by rw [hkb _ (Ne.symm hscE)]; exact hk + have hktb : σb.kheap t.bv = some (.tcb xt) := by rw [hkb _ htS]; exact hkt + have hzb : SzKeep σ σb := fun p y0 m hp => by + by_cases hps : p = sc.bv + · subst hps; rw [hkbsc] at hp; cases hp; exact ⟨_, hky⟩ + · exact ⟨y0, by rw [← hkb _ hps]; exact hp⟩ + have hz0 : SzKeepC Z.C σb σ := by + intro p _ y0 m hp + by_cases hps : p.bv = sc.bv + · rw [hps] at hp ⊢; rw [hky] at hp; cases hp; exact ⟨_, hkbsc⟩ + · exact ⟨y0, by rw [hkb _ hps]; exact hp⟩ + -- P1's facts at the start + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hSc0, hSg0⟩ := J.blk + have hsH : sd' ∈ P1.Heap.tcbs H := J.zH.1 sd' hsT + have hHe : H e = some .ep := J.zH.2.1 e he + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h0.tcbs + have hsta := hTP sd' hsH + have hss : L.mem s (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [stA_tStA, tsVals.2.2.2.1]; exact UScalar.eq_of_val_eq hs4 + have hbe : L.mem s (P1.blockA sd') = e := by rw [P1.blockA_eq, ← tBlockA_eq]; exact hsb + have hdq := P1.endpoint.ep_dequeue.inv s sd' Q0 R0 h0 hsH (by simp) (Or.inl hss) + rw [hbe] at hdq + obtain ⟨E0, EQ0, hEW0⟩ := h0.eps + have heE0 : e ∈ E0 := (hEW0.2.2.2.2.2.2.2.1 e).2 (Or.inl hHe) + obtain ⟨hE0, -, hq0'⟩ := hEW0.1 e heE0 + have hdm : sd' ∈ EQ0 e := by + have := hEW0.2.2.2.2.1 sd' hsH (by simp) (Or.inl hss); rw [hbe] at this; exact this.2 + have pf := P1.endpoint.ep_dequeue.foot s e sd' (EQ0 e) hTP hE0 hq0' hdm + unfold endpoint.receive_from_sender + refine WP.spec_bind (Fl4Proofs.P1.endpoint.get_queue.spec s e) ?_ + rintro _ rfl + have hhead : (L.epQ s e).head = sd' := by rw [epQ_eq]; exact hQ.head + rw [hhead] + -- the dequeue + refine WP.spec_bind (WP.spec_and (WP.spec_and (ep_dequeue_c hH hP0 hP s σ hR hS0 he hk (d := sd') + (by simp [Fl4Spec.Endpoint.epQueue])) (ep_dequeue_full hP s he sd' _ hQ (by simp))) + (WP.spec_and hdq pf)) ?_ + rintro s1 ⟨⟨⟨-, hR1, hS1, hA1⟩, -, -, hQA, hWE, -, -⟩, ⟨h1, f1, o1, n1, k1, cc1, nc1⟩, w1⟩ + have hW1a : P1.WritesIn st s s1 (fun b => EW e b ∨ ∃ w ∈ (sd' :: xs'), TL w b) := + ⟨hWE.1, hWE.2.1, fun b hb hn => hQA b hb ⟨fun h => hn (Or.inl h), fun h => hn (Or.inr h)⟩⟩ + have hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := hW1a.mono fun b hb => by + rcases hb with h | ⟨w, hw, h | h⟩ + · exact Or.inr (Or.inl ⟨e, he, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3496, by omega, by omega, by omega, by omega, by omega, h⟩) + · exact Or.inl (Or.inr ⟨w, hQ.mem w hw, 3504, by omega, by omega, by omega, by omega, by omega, h⟩) + have F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1 := + P1.Foot.of w1 fun b hb => hb.kw hq0'.mem (Or.inl hHe) + obtain ⟨hB1, hX1, hS1', hSg1, -, -, -⟩ := P1.frame_bx h0 hB0 hX0 hSc0 hSg0 f1 o1 n1 k1 + (P1.TmFr.mk0 ⟨k1, fun y hy K a b c => f1 y hy K (by omega) c (by omega) (by omega), + fun c hc o ho => nc1 c hc o (by omega) (by omega), fun n' hn' => o1 _ (hSg0.sub _ (hX0.1.ntB n' hn').2)⟩) + -- the badge and the flags + 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⟩ + refine WP.spec_bind (Endp.obj.Tcb.block_badge.spec s1 sd') ?_ + rintro bdg hbdg + refine WP.spec_bind (Endp.obj.Tcb.block_flags.spec s1 sd') ?_ + rintro fl hfl + step as ⟨i, hi, hi'⟩ + step as ⟨i1, hi1, hi1'⟩ + step as ⟨i2, hi2, hi2'⟩ + have hg : (i != 0#u64) = dd.senderCanGrant := by + rw [← flags_grant dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi + have hgr : (i1 != 0#u64) = dd.senderCanGrantReply := by + rw [← flags_grantReply dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi1 + have hc : (i2 != 0#u64) = dd.senderIsCall := by + rw [← flags_call dd, ← hflags, ← hfw, ← hfl] + congr 1 + exact UScalar.eq_of_val_eq hi2 + have hbb : bdg.bv = dd.senderBadge := by rw [hbdg, hbw]; exact hbadge + -- the transfer: the fault (XTopJointXferF's `xferFaultStepGJ`, on `IRel`) + set σ1 := Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1 + have hkt1 : σ1.kheap t.bv = some (.tcb xt) := by simp [σ1, hte, hkt] + have hks1 : σ1.kheap sd'.bv = some (.tcb xs) := by simp [σ1, hse, hks] + have hk1e : σ1.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by simp [σ1] + have hOK1 : Endp.IpcBufCapOK σ1 := ipcBufCapOK_epStep (J.xferH).1 (EpStep.ep hk) + have htH : t ∈ P1.Heap.tcbs H := J.zH.1 t ht + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hky1 : σ1.kheap sc.bv = some (.schedContext ys0 ns0) := by simp [σ1, hscE, hky] + have hsc1 : sc ∈ P1.Heap.scs H := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨_, (hJ.sc sc ys0 ns0 (J.c0 sc hscC) hky).2⟩ + -- the transfer: the timeout fault (XTopJointFaultTmo's `xferTmoStepJ`, on `IRel`) + refine WP.spec_bind (xferTmoStepJ hP0 (hH.epHyps hP0) hmax s1 a σ1 htu hR1 hS1 hOK1 + (bufCover_epStep (J.xferH).2.1 (EpStep.ep hk)) hsT ht (Ne.symm hts) hks1 hkt1 hft hsc0 hky1 e bdg (i != 0#u64) + hTP htH h1 hB1 hX1 hS1' hSg1 hsc1) ?_ + rintro s2 ⟨x2, e2, hx2, hR2, hS2', hA2, w2, ea2, h2, hB2, hX2, hS2'', hSg2, f2⟩ + set σ2 := Notif.updK (Ipc.tmoSt σ1 sc ys0 ns0) t.bv (.tcb x2) with hσ2 + set σ1b := Notif.updK σb e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) with hσ1b + have eσ2 : σ2 = Notif.updK σ1b t.bv (.tcb x2) := by + rw [hσ2, hσ1b, hσ1, hσb]; simp only [Ipc.tmoSt]; rw [updSc_updK_comm _ hscE] + have hkt1b : σ1b.kheap t.bv = some (.tcb xt) := by simp [σ1b, hte, hktb] + have hkt2 : σ2.kheap t.bv = some (.tcb x2) := by rw [eσ2]; simp + have hks2 : σ2.kheap sd'.bv = some (.tcb xs) := by rw [eσ2]; simp [σ1b, hst', hse, hksb] + have hk2e : σ2.kheap e.bv = some (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) := by + rw [eσ2]; simp [σ1b, Ne.symm hte] + have hscw8 : SW Z.C σ (scW sc 8#u64) := sw_hdr hI.2.2.1 hscC (K := 8#u64) (by simp) (by simp) (by decide) + have hW2 : P1.WritesIn st s s2 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW1.trans1 (w2.mono fun b hb => by + rcases hb with ⟨eb, -⟩ | (⟨y0, hy, K, h1', h2', -, eb⟩ | hb) + · subst eb; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))) + · have hyT : y0 ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inr ⟨y0, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩) + rw [eb]; exact tfield_val (hP.tcb y0 hyT) (by omega) + · exact absurd hb (Set.notMem_empty _)) + have F2 : P1.WritesIn st s1 s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) s1 b ∨ False) := + w2.mono fun b hb => by + rcases hb with ⟨-, c0, hc0, hin⟩ | (⟨y0, hy, K, h1', h2', h3', eb⟩ | hb) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨c0, hc0, hin⟩)))) + · exact Or.inl (Or.inl ⟨y0, hy, K, by omega, by omega, by omega, by omega, eb⟩) + · exact absurd hb (Set.notMem_empty _) + -- the spec frame from the base `σb` + have Fr1 : RSFr sd'.bv t.bv σb σ1b := RSFr.upd hkbe (Or.inr (Or.inr ⟨_, _, rfl, rfl, + by rw [epDeq_queue']; unfold Fl4Spec.removeAll; exact List.filter_sublist, + by simp [Fl4Spec.Endpoint.epQueue]⟩)) + have Fr2c : RSFr sd'.bv t.bv σ1b σ2 := by + have h := RSFr.arch (t := sd'.bv) (σ := σ1b) hkt1b x2.tcbArch + rw [← hx2, ← eσ2] at h; exact h + have Fr2 : RSFr sd'.bv t.bv σb σ2 := Fr1.trans Fr2c + have hz2 : SzKeepC Z.C σ σ2 := SzKeepC.of (fun p y m hp => by + obtain ⟨y1, h⟩ := Fr2.szKeep p y m hp; exact hzb p y1 m h) + have IK1 : IK sd'.bv σb σ1b := IK.updOther (o' := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) hkbe + (fun o0 h => by + rcases h with rfl | rfl <;> exact ⟨fun _ h => (by cases h), fun _ _ h => (by cases h), fun _ h => (by cases h)⟩) + have SK1 : Ipc.SKeep σb σ1b := Ipc.SKeep.updK (p := e.bv) (o := .endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv)) + (fun y h => by cases h) (fun _ _ h => by cases h) + have IK2 : IK sd'.bv σb σ2 := by + rw [eσ2]; exact IK1.trans (IK.updTcb (σ := σ1b) hkt1b (by rw [hx2]) (by rw [hx2]) (fun _ => by rw [hx2])) + have SK2 : Ipc.SKeep σb σ2 := by + rw [eσ2]; exact SK1.trans (Ipc.SKeep.updK (σ := σ1b) (p := t.bv) + (o := .tcb x2) (fun y' h => ⟨xt, hkt1b, by cases h; rw [hx2]⟩) (fun _ _ h => by cases h)) + -- the sender's sporadic check, at the hole + have hne2 : ∀ p y' n, σ2.kheap p = some (.schedContext y' n) → Fl4Spec.scActive y' = true → y'.scRefills ≠ [] := + fun p y' n hp ha => Jb.inv.ne p y' n (SK2.2.2.2 p y' n hp) ha + refine WP.spec_bind (unblockHole hP0 (IpcRel.ofSRN hR2 hS2') hsT hks2 hne2 h2 hB2 hX2 hS2'' hSg2 hsH) ?_ + rintro s3 ⟨σ3, hA, hIR3, hW3, h3, hB3, hX3, hS3', hSg3, hT3, fu3, hU3⟩ + have hk3 : ∀ q' o', σ2.kheap q' = some o' → (∀ y' n, o' ≠ .schedContext y' n) → σ3.kheap q' = some o' := by + intro q' o' hq ho + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, -, rfl, -⟩ + · exact hq + · have hqp : q' ≠ p := fun h => by subst h; rw [hkp] at hq; cases hq; exact ho _ _ rfl + simp [Notif.updK, hqp, hq] + have hks3 : σ3.kheap sd'.bv = some (.tcb xs) := hk3 _ _ hks2 (fun _ _ h => by cases h) + have hk3e := hk3 _ _ hk2e (fun _ _ h => by cases h) + have ct3 : σ3.curThread = σ2.curThread := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have act3 : σ3.schedulerAction = σ2.schedulerAction := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h] + · rw [h]; rfl + have hR3 := hIR3.1.toE + have hS3 := IpcRel.toSRN hIR3.1 hIR3.2 + have hxs3 := hR3.at_tcb hsT hks3 + refine WP.spec_bind (Endp.obj.Tcb.fault_type.spec s3 sd') ?_ + rintro ft hft' + have hftB : (ft != fl4_abi.consts.fault.NullFault) = true := by + rw [hft'] + have hne : L.mem s3 (tFaultA sd') ≠ fl4_abi.consts.fault.NullFault := fun e => by + have := hxs3.fault0.1 (by rw [e]; unfold fl4_abi.consts.fault.NullFault; rfl); rw [hft] at this; cases this + simpa using hne + have hty3 : L.mem s3 (tFaultA sd') = 5#u64 := by + have hfr := hxs3.faultR + unfold FaultRel at hfr; rw [hft] at hfr; exact hfr.1 + have hcdF : (ft != fl4_abi.consts.fault.Timeout) = false := by + rw [hft', hty3]; unfold fl4_abi.consts.fault.Timeout; decide + -- the sender is in no queue + have eW : ∀ K : U64, K.val < 4096 → K.val % 8 = 0 → 3328 ≤ K.val → K.val ≠ 3496 → K.val ≠ 3504 → + L.mem s3 (core.num.U64.wrapping_add sd' K) = L.mem s (core.num.U64.wrapping_add sd' K) := by + intro K k1 k2 k3 k4 k5 + rw [hT3 sd' hsH K k1 k2, f2 sd' hsH K k3 k1 k2, f1 sd' hsH K k1 k2 k4 k5] + have hb3 : L.mem s3 (tBlockA sd') = e := by + rw [← blockA_tBlockA, P1.blockA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.blockA_eq] + exact hbe + have hst3 : L.mem s3 (P1.stA sd') = sched.TS_BLOCKED_ON_SEND := by + rw [P1.stA_eq, eW _ (by simp) (by simp) (by simp) (by simp) (by simp), ← P1.stA_eq] + exact hss + have hq3 := dequeued_not_queued hR3 he hb3 hk3e (epDeq_not_mem _) + have hnr3 : ∀ r ∈ P1.Heap.replies H, L.mem s3 (P1.rTcbA r) ≠ sd' := + hX3.noref hsta (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide) + (by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide) -- the sender is in no ready or release queue + have hnqS : NotQueued σ sd'.bv := by + refine ⟨fun dm p hm => ?_, fun hm => ?_⟩ + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rq dm p _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + · obtain ⟨x, -, -, -, hkx, -, hrx, -, -⟩ := J.inv.rel _ hm + rw [hks] at hkx; cases hkx; rw [hstd] at hrx; cases hrx + have hnqSb : NotQueued σb sd'.bv := hnqS + -- the receiver: no donation (the sender's timeout fault) + have hCI : CurInv σb := Jb.cur.resolve_left (fun h => by + have h' : σ.schedulerAction = .chooseNewThread := h + obtain ⟨u0, hu0, -⟩ := hAct + rw [hu0] at h'; cases h') + have hnrT : t.bv ∉ σ.releaseQueue := by rw [← hct]; exact hCI.notRel + have hswT : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t.bv ∧ u ≠ sd'.bv := fun u hu => by + obtain ⟨u0, hu0, hne⟩ := hAct + rw [hu0] at hu; cases hu + refine ⟨hne, fun e0 => ?_⟩ + obtain ⟨x0, -, -, -, hk0, -, hr0, -, -, -⟩ := schedulable_inv (J.act.1 _ hu0) + rw [e0, hks] at hk0; cases hk0; rw [hstd] at hr0; cases hr0 + have hnqT : ∀ dm p, t.bv ∉ σ.readyQueues dm p := by rw [← hct]; exact hnq + -- the frames to `σ3` + have Fr3 : RSFr sd'.bv t.bv σ2 σ3 := by + rcases hU3 with rfl | ⟨p, y', z, n, -, hkp, -, hpc, rfl, -, -, hz, -⟩ + · exact RSFr.refl _ _ _ + · exact (RSFr.reprog _ _ σ2).trans (RSFr.upd (σ := { σ2 with reprogramTimer := true }) hkp + (Or.inr (Or.inl ⟨y', n, z.scRefills, rfl, by rw [← hz], hpc⟩))) + have Fr23 : RSFr sd'.bv t.bv σb σ3 := Fr2.trans Fr3 + have hz23 : SzKeepC Z.C σ σ3 := SzKeepC.of (fun p y m hp => by + obtain ⟨y1, h⟩ := Fr23.szKeep p y m hp; exact hzb p y1 m h) + have hrq3 : σ3.readyQueues = σ.readyQueues := by + rcases hU3 with h | ⟨_, _, _, _, -, -, -, -, h, -⟩ + · rw [h]; exact IK2.2.1 + · rw [h]; exact IK2.2.1 + -- the reply + have hgg := hyes _ _ hstd + obtain ⟨wb, r', hrcw, hrc'w, hwR, hw0s⟩ : ∃ b r', rc = .Reply rv b ∧ rc' = .replyCap rv.bv r' ∧ rv ∈ Z.R ∧ + L.mem s (Endp.rTcbA rv) = 0#u64 := by + rcases hrc with ⟨h1, -, -⟩ | h + · exact absurd h1 hrn + · exact h + obtain ⟨xr, hkr, hxr⟩ := hR.reply rv hwR + have hxr0 : xr.replyTcb = none := by rw [hxr, hw0s, optRef_zero] + have hkrb : σb.kheap rv.bv = some (.reply xr) := by + rw [hkb _ (kheap_ne hkr hky (fun _ h1 h2 => by subst h1; cases h2))]; exact hkr + have hkr3 : σ3.kheap rv.bv = some (.reply xr) := Fr23.replyF hkrb + have hkt3 : σ3.kheap t.bv = some (.tcb x2) := hk3 _ _ hkt2 (fun _ _ h => by cases h) + have hsc2 : x2.tcbSchedContext = xt.tcbSchedContext := by rw [hx2] + have hws : rv.bv ≠ sd'.bv := kheap_ne hkr hks (fun _ h1 h2 => by subst h1; cases h2) + have hwt : rv.bv ≠ t.bv := kheap_ne hkr hkt (fun _ h1 h2 => by subst h1; cases h2) + -- P1's facts at `s3` + have hSR3 : SchedRel st Z.N Z.T Z.C Z.c s3 σ3 := hIR3.2.2.1 + obtain ⟨Q3, R3', hcq3, hs3⟩ := hSR3.lists + have bits3 : ∀ u ∈ Z.T, (∀ dm p, u.bv ∉ σ.readyQueues dm p) → u.bv ∉ σ.releaseQueue → + (L.mem s3 (P1.inqA u) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s3 (P1.inqA u) &&& sched.Q_RELEASE) = 0#u64 := by + intro u hu hq hr + refine ⟨Classical.byContradiction fun h => ?_, Classical.byContradiction fun h => ?_⟩ + · have hm := (hcq3.queued u hu).1 h + apply hq 0 (specPrio (L.mem s3 (P1.prioA u))) + rw [← hrq3, hs3.rq _ (hcq3.prio u hu)] + exact List.mem_map_of_mem hm + · have hm := (hcq3.released u hu).1 h + apply hr + show u.bv ∈ σb.releaseQueue + rw [← Fr23.sk.rel, hs3.rel] + exact List.mem_map_of_mem hm + obtain ⟨hqbS, hrbS⟩ := bits3 sd' hsT hnqS.1 hnqS.2 + obtain ⟨hqbT, hrbT⟩ := bits3 t ht hnqT hnrT + have hna3 : ∀ u ∈ Z.T, (u = sd' ∨ u = t) → L.actionW s3 ≠ u := fun u hu hut => + actionW_ne_of hSR3 hu (fun h => by + obtain ⟨h1, h2⟩ := hswT _ (act3.symm.trans h) + rcases hut with rfl | rfl + · exact h2 rfl + · exact h1 rfl) + have hidl3 : σ3.idleThread = σ.idleThread := Fr23.sk.idle + have hiw3 : ∀ u ∈ Z.T, u.bv ≠ σ.idleThread → L.idleW s3 ≠ u := fun u hu hne h' => by + apply hne + rw [← hidl3, hs3.idleTh] + unfold P1.LawfulStore.idleW at h'; rw [hcq3.cur] at h' + exact congrArg _ (show nodeW st s3 Z.c layout.node.IDLE_THREAD = u from h').symm + have hit : t.bv ≠ σ.idleThread := by rw [← hct]; exact hni + have his : sd'.bv ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs, -⟩ := J.act.2 + rw [← h, hks] at hkx; cases hkx; rw [hstd] at hxs; cases hxs + have hcA : L.mem s3 (P1.scA sd') ≠ 0#u64 → L.mem s3 (P1.scA sd') ∈ Ac := fun h0 => + hB3.sact sd' hsH (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl (Or.inl hst3))))))) h0 + have hcold : L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_REPLY ∧ L.mem s3 (P1.stA sd') ≠ sched.TS_BLOCKED_ON_RECV := + ⟨by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4]; unfold sched.TS_BLOCKED_ON_REPLY; decide, + by obtain ⟨-, -, r3, r4, -⟩ := P1.ts_vals; rw [hst3, r4, r3]; decide⟩ + have hRe : Z.R = P1.Heap.replies H := J.tcbs_eq.2 + have hwH : rv ∈ P1.Heap.replies H := hRe ▸ hwR + have hw00 : rv ≠ 0#u64 := (J.rp.align rv hwR).2.1 + have hz3 : L.mem s3 (P1.rTcbA rv) = 0#u64 := by + obtain ⟨xr', hkr', hrl⟩ := hR3.reply rv hwR + rw [hkr3] at hkr'; cases hkr' + exact optRef_none (hrl.symm.trans hxr0) + have hns3 : tsCode xs.tcbState ≠ 6 := by rw [hstd]; simp [tsCode] + have hSM := ScRelM.of hIR3.1 + obtain ⟨xd3, hkd3', hdsc⟩ := hSM t ht + rw [hkt3] at hkd3'; cases hkd3' + obtain ⟨xs3', hks3', hssc⟩ := hSM sd' hsT + rw [hks3] at hks3'; cases hks3' + have hw1 : P1.WritesIn st s3 (L.wr s3 (P1.rTcbA rv) sd') (fun b => b.val = rv.val) := by + obtain ⟨-, -, -, hA1⟩ := reply_write_rel (hH.epHyps hP0) hP s3 σ3 hR3 hS3.1 hwR hkr3 sd' + exact ⟨L.ksb_wr _ _ _, L.cpu_wr _ _ _, fun b hb hn => hA1 b hb hn⟩ + have hXc : P1.ExcT {y' | y' ∈ (∅ : Set U64) ∨ y' = sd'} sd' := fun y' hy' => by simpa using hy' + -- the push on `IRel`, at P1 + have hPush : ∀ cd : Bool, (cd = false ∨ L.mem s3 (P1.scA t) ≠ 0#u64) → ipc.reply_push st arI s3 a sd' t rv cd ⦃ r => r.2 = a ∧ + Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv rv.bv cd σ3 = ({((), pushSt σ3 sd'.bv rv.bv xr xs)}, False) ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem r.1) (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) r.1 (pushSt σ3 sd'.bv rv.bv xr xs) ∧ + P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac r.1 ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s3 r.1 ∧ + sched.set_blocked_on_reply st (L.wr s3 (P1.rTcbA rv) sd') sd' rv = ok r.1 ⦄ := by + intro cd hcd + refine WP.spec_mono (WP.spec_and (WP.spec_and + (reply_push_iC (don := cd) (P := fun s' σ' => σ' = pushSt σ3 sd'.bv rv.bv xr xs ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s') σ' ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s' σ') + hH hP0 hP s3 a σ3 hR3 hS3 hsT hks3 hkt3 hq3 hns3 hwR hkr3 (fun _ => ht) + (fun s' h1 h2 => ⟨rfl, h1, h2⟩) (fun hd _ _ _ _ _ _ _ h9 => hcd.elim (fun e => by rw [e] at hd; cases hd) + (fun h => (h (by rw [hdsc] at h9; exact optRef_none h9)).elim))) + (P1.ipc.reply_push.ipc hAT s3 a sd' t rv cd Q0 R0 h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT (Or.inl rfl)) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hqbT hrbT (hna3 t ht (Or.inr rfl)) + (hiw3 t ht hit) hcA)) + (P1.ipc.reply_push.nd (Es := P1.EH H) s3 a sd' t rv cd h3 hXc (Or.inl (Or.inr rfl)) hB3 hX3 hS3' hSg3 hsH + hqbS hrbS (hna3 sd' hsT (Or.inl rfl)) (hiw3 sd' hsT his) hcold hwH hw00 hz3 htH hts hcA hcd)) ?_ + rintro ⟨s4, a4⟩ ⟨⟨⟨ha, σ', hm, e4, hR4, hS4⟩, hb1, -, -⟩, -, hf, hsb⟩ + subst e4 + exact ⟨ha, hm, hR4, hS4, hb1, hf, hsb⟩ + have hrv0 : (rv != 0#u64) = true := by simpa using hw00 + suffices HH : ipc.reply_push st arI s3 a sd' t rv false ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ by + have HH' : (do + let i3 ← obj.Tcb.sc st sd' s3 + let can_donate ← if i3 != 0#u64 then ok (ft != fl4_abi.consts.fault.Timeout) else ok false + ipc.reply_push st arI s3 a sd' t rv can_donate) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + RecvFootPost (L := L) arI AR Z H Cs Ac Lst D s a σ r z.2 ⦄ := by + refine WP.spec_bind (Fl4Proofs.P1.obj.Tcb.sc.spec s3 sd') ?_ + rintro i3 - + by_cases h3' : (i3 != 0#u64) = true + · rw [if_pos h3', hcdF]; exact ok_bind_spec _ _ HH + · rw [if_neg h3']; exact ok_bind_spec _ _ HH + rcases Bool.eq_false_or_eq_true dd.senderCanGrant with hG | hG + · by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hrv0, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · have hGR : dd.senderCanGrantReply = true := hgg.resolve_left (by rw [hG]; decide) + by_cases hci : (i2 != 0#u64) = true + · simp only [hci, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + · simp only [hci, hftB, hg, hG, hgr, hGR, hrv0, Bool.false_eq_true, ↓reduceIte]; exact HH' + refine WP.spec_mono (hPush false (Or.inl rfl)) ?_ + rintro ⟨s4, a4⟩ ⟨ha4, hpush, hR4, hS4, hblk, fu4, hsb4⟩ + simp only at ha4 hpush hR4 hS4 hblk fu4 hsb4 ⊢ + subst ha4 + -- the footprints + have hW4 : P1.WritesIn st s3 s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s3 σ3) := by + have h := set_blocked_on_reply_cn (N := Z.N) (C := Z.C) (E := Z.E) hP hSR3 hsT hwR hw1 + rw [hsb4, WP.spec_ok] at h; exact h + have hks2b : L.ksb s2 = L.ksb s := hW2.1 + have hW3' : P1.WritesIn st s s3 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW2.trans1 (hW3.mono fun b hb => cnW_back hks2b hz2 (Or.inl (Or.inl hb))) + have hks3b : L.ksb s3 = L.ksb s := hW3'.1 + have hW4' : P1.WritesIn st s s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hW3'.trans1 (hW4.mono fun b hb => cnW_back hks3b hz23 hb) + have hfoot := footW_trans (footW_trans (footW_trans (footW_of F1) F2) (footW_of fu3)) (footW_of fu4) + -- the sender `Inactive` (a spec state only): `Inv` there, as in `recvSendFJG` + set x5 : Fl4Spec.Tcb := { xs with tcbState := .inactive } with hx5 + have hsc3 : sd'.bv ≠ (Notif.updK σ3 sd'.bv (.tcb x5)).curThread := by + show sd'.bv ≠ σ3.curThread; rw [ct3]; show sd'.bv ≠ σ.curThread; exact hsc + set σ5 := Notif.stsAct (Notif.updK σ3 sd'.bv (.tcb x5)) sd'.bv with hσ5 + have Fr5 : RSFr sd'.bv t.bv σb σ5 := Fr23.trans (Sched.RSFr.stsInact hks3 (Or.inl ⟨_, _, hstd⟩)) + set σ5' := Notif.updK σ2 sd'.bv (.tcb x5) with hσ5' + have IK5' : IK sd'.bv σb σ5' := IK2.trans (IK.updSelf (y := x5) hks2 rfl) + have SK5' : Ipc.SKeep σb σ5' := SK2.trans (Ipc.SKeep.updK (p := sd'.bv) (o := .tcb x5) + (fun y' h => ⟨xs, hks2, by cases h; rfl⟩) (fun _ _ h => by cases h)) + have hk5' : σ5'.kheap sd'.bv = some (.tcb x5) := by simp [σ5'] + have hrib : ∀ x, σ5'.kheap sd'.bv = some (.tcb x) → inClassR x.tcbState x.tcbFault → RelIfBound σ5' sd'.bv := by + intro x hx hc + rw [hk5'] at hx; cases hx + exfalso + rcases hc with h | ⟨_, h⟩ | ⟨_, _, h, -⟩ | ⟨_, _, _, h⟩ <;> simp [hx5, Fl4Spec.runnable] at h + have I5' : Inv σ5' := ((Jb.inv.toBut sd'.bv).ik hnqSb IK5').1.inv hrib + have I5 : Inv σ5 := by + rcases hU3 with e3 | ⟨p, y', z, n, hxp, hkp, hne', hpc, e3, hz1, hz2, hz3, hz5, hz6⟩ + · rw [hσ5, stsAct_ne hsc3, e3]; exact I5' + · have hpsd : p ≠ sd'.bv := fun h => by rw [h, hks2] at hkp; cases hkp + have hkp' : σ5'.kheap p = some (.schedContext y' n) := by simp [σ5', hpsd, hkp] + have e5 : σ5 = updSc { σ5' with reprogramTimer := true } p z n := by + rw [hσ5, stsAct_ne hsc3, e3, hσ5'] + rw [show ({ Notif.updK σ2 sd'.bv (.tcb x5) with reprogramTimer := true } : Fl4Spec.State Unit) = + Notif.updK { σ2 with reprogramTimer := true } sd'.bv (.tcb x5) from rfl, updSc_updK_comm _ hpsd] + rw [e5] + refine I5'.scRef hkp' hne' hz1 hz2 hz3 hz5 hz6 (fun u hu x hx hxu => ?_) + obtain ⟨y1, n1, hk1, ht1⟩ := I5'.back u x p hx hxu + obtain ⟨y2, n2, hk2, ht2⟩ := I5'.back sd'.bv x5 p hk5' hxp + rw [hk1] at hk2; cases hk2 + have hu' : u = sd'.bv := Option.some.inj (ht1.symm.trans ht2) + subst hu' + exact hnqSb.2 (by rw [← SK5'.1]; exact hu) + have act5 : σ5.schedulerAction = σ.schedulerAction := by + rw [hσ5, stsAct_ne hsc3]; exact act3 + -- the push from `σ5` + have e5 : σ5 = Notif.updK σ3 sd'.bv (.tcb x5) := stsAct_ne hsc3 + have hkta : σ5.kheap sd'.bv = some (.tcb x5) := by rw [e5]; simp [Notif.updK] + have hkwa : σ5.kheap rv.bv = some (.reply xr) := by rw [e5]; simp [Notif.updK, hws, hkr3] + have heqP : pushSt σ3 sd'.bv rv.bv xr xs = pushSt σ5 sd'.bv rv.bv xr x5 := by + rw [e5]; unfold pushSt; rw [updK3_eq σ3 sd'.bv rv.bv _ _ _ (Ne.symm hws)]; try rfl + set σ' := pushSt σ5 sd'.bv rv.bv xr x5 with hσ' + have G' := (ps_skD (σ := σ5) (t := sd'.bv) (w := rv.bv) (xr := xr) (xt := x5)) + have hnqSa : ∀ dm p, sd'.bv ∉ σ5.readyQueues dm p := by + rw [e5]; show ∀ dm p, sd'.bv ∉ σ3.readyQueues dm p; rw [hrq3]; exact hnqS.1 + have hnrSa : sd'.bv ∉ σ5.releaseQueue := by rw [Fr5.sk.rel]; exact hnqS.2 + have I6 : Inv σ' := ps_inv hkta hkwa hws I5 hnqSa hnrSa + have hscA : ∀ p y n, σ5.kheap p = some (.schedContext y n) → y.scTcb = some sd'.bv → + Fl4Spec.scActive y = true := by + intro p y n hp hyt + obtain ⟨y0, hy0, ey, -⟩ := Fr5.scB hp + rw [ey] + exact Jb.bsa.tcb p y0 n sd'.bv xs hy0 (by rw [ey] at hyt; exact hyt) hksb (by rw [hstd]; rfl) + have B6 : BlockedScActive σ' := ps_bsa hkta hkwa hws hscA (Fr5.bsa Jb.bsa) + have hcne1 : σ5.curThread ≠ sd'.bv := by rw [Fr5.sk.ct]; show σ.curThread ≠ _; rw [hct]; exact Ne.symm hst' + have hcne2 : σ5.curThread ≠ rv.bv := by rw [Fr5.sk.ct]; show σ.curThread ≠ _; rw [hct]; exact Ne.symm hwt + have hactP : σ'.schedulerAction = σ.schedulerAction := by + show (pushSt σ5 sd'.bv rv.bv xr x5).schedulerAction = _ + unfold pushSt + rw [stsAct_ne (by show sd'.bv ≠ σ5.curThread; exact Ne.symm hcne1)] + exact act5 + have hidT : σ5.idleThread ≠ sd'.bv := by rw [Fr5.sk.idle]; exact fun e0 => his e0.symm + have hAI6 : ActIdle σ' := + ⟨fun u hu => by + rw [hactP] at hu + have hs5 := Fr5.sched (Jb.act.1 u (show σb.schedulerAction = _ from hu)) + have hur : u ≠ rv.bv := fun e0 => by + obtain ⟨x0, -, -, -, hk0, -⟩ := schedulable_inv hs5 + rw [e0, hkwa] at hk0; cases hk0 + exact ps_schedM hkta hkwa (hswT u hu).2 hur hs5, ps_idleOK hkta hkwa hws hidT (Fr5.idleOK Jb.act.2)⟩ + have hcur6 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := by + have hCIa : CurInv σ5 := Fr5.curInv hCI + refine Or.inr ?_ + obtain ⟨⟨xc, hkc, hcs, hnb'⟩, ⟨y0, n0, hky0⟩, hact0, hnr0⟩ := hCIa + have hsne1 : σ5.curSc ≠ sd'.bv := kheap_ne hky0 hkta (fun _ h1 h2 => by subst h1; cases h2) + have hsne2 : σ5.curSc ≠ rv.bv := kheap_ne hky0 hkwa (fun _ h1 h2 => by subst h1; cases h2) + refine ⟨⟨xc, ?_, ?_, hnb'⟩, ⟨y0, n0, ?_⟩, ?_, ?_⟩ + · rw [G'.ct, ps_ko hcne1 hcne2]; exact hkc + · rw [G'.csc]; exact hcs + · rw [G'.csc, ps_ko hsne1 hsne2]; exact hky0 + · intro hne x n hx + rw [G'.csc, ps_ko hsne1 hsne2] at hx + rw [G'.csc, G'.isc] at hne + exact hact0 hne x n hx + · rw [G'.ct, ps_rel]; exact hnr0 + have hrun6 : CurRunM σ' := Or.inl (by + rw [hactP]; obtain ⟨u0, hu0, -⟩ := hAct; rw [hu0]; intro h; cases h) + -- the footprints rebased onto the base (XTopJointFaultTmo's `writesIn_rebase`) + have hSσ := J.xtop.fin.1.1.1 + obtain ⟨Qs, Rs, hcs, hsps⟩ := hSσ.lists + obtain ⟨ysb, nsb, hkys, hys⟩ := hsps.scs sc hscC + rw [hky] at hkys; cases hkys + have hal8 : P1.Aligned (scW sc 8#u64) := (hys.hdr (K := 8#u64) (by simp) (by simp)).1 + have hw0 : P1.WritesIn st s (tmoS st s sc) (fun b => b = scW sc 8#u64) := writesIn_wr hal8 + have hWb : P1.WritesIn st (tmoS st s sc) s4 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c (tmoS st s sc) σb) := + (writesIn_rebase hW4' hw0 (fun b0 hb0 => by subst hb0; exact Or.inl (Or.inl (Or.inr (Or.inr hscw8))))).mono + fun b0 hb0 => CnW.back (s := tmoS st s sc) (s' := s) hw0.1.symm hz0 hb0 + have hfootb : P1.WritesIn st (tmoS st s sc) s4 (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) (tmoS st s sc)) := + (writesIn_rebase hfoot hw0 (fun b0 hb0 => by + subst hb0; exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inl ⟨sc, hsc1, scW8_inSC J.blk hsc1⟩)))))).mono + fun b0 hb0 => hb0.elim (P1.KW.ksb hw0.1.symm) False.elim + have hIb := Jb.xtop.irelK Jb.ks Jb.buf + have hF : IFrame (L := L) Z.th (tmoS st s sc) s4 := IRel.iframe_of hIb hWb fun b0 hb0 => CnW.oobj hIb.2.2.1 hb0 + have htD := Jb.not_dead_of_st hsT hksb (by rw [hstd]; simp [tsCode]) + have hIR4 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s4 σ' := by + rw [← heqP]; exact IpcRel.ofSRN hR4 hS4 + have ho : ∀ {q : Fl4Spec.ObjRef} {o : Fl4Spec.KernelObject}, (∀ x, o ≠ .tcb x) → (∀ x, o ≠ .reply x) → + (σ'.kheap q = some o ↔ σ5.kheap q = some o) := fun h1 h2 => ps_oth hkta hkwa hws h1 h2 + have hkq : ∀ q ∈ D \ {rv}, σ'.kheap q.bv = σ5.kheap q.bv := fun q hq => + ps_ko (htD q hq) (fun e0 => hq.2 (P1.eq_of_bv e0)) + have J6 := Jb.rsfr_postGR hIR4 hF hWb hblk hfootb Fr5 ps_skD + (ps_ksub hkta hkwa hws) (ps_szKeep hkta hkwa hws) (ps_ykeep hkta hkwa hws) (ps_mcpKeep hkta hkwa hws) + (ps_bufKeep hkta hkwa hws) (fun d _ h => ps_recvB hkta hkwa hws h) (ps_ksz hkta hkwa hws) + (ps_kback hkta hkwa hws) (ps_ipcFr hkta hkwa hws) (ps_ck hkta hkwa hws) (fun d _ h => ps_free hkta hkwa hws h) + (fun q ao h => (ho (fun _ e => by cases e) (fun _ e => by cases e)).2 h) hkq + I6 B6 hAI6 hcur6 hrun6 (ps_own hkta hkwa hws (Fr5.own Jb.own)) + (ps_off hkta hkwa hws (Fr5.off Jb.off)) (ps_nsc hkta hkwa hws (Fr5.nsc Jb.nsc)) + (ps_isorted hkta hkwa hws (Fr5.isorted Jb.isorted)) htD + -- the spec's outcome + have e2' : Fl4Spec.doIpcTransfer (Z := Unit) sd'.bv (some e.bv) dd.senderBadge dd.senderCanGrant t.bv + (Notif.updK σ e.bv (.endpoint (epDeq (.sendEP (sd'.bv :: q)) sd'.bv))) = ({((), σ2)}, False) := by + rw [← hbb, ← hg]; exact e2 + have hyesS : (dd.senderCanGrant ∨ dd.senderCanGrantReply) ∧ optRef rv ≠ none := + ⟨by rcases hgg with h | h <;> simp [h], by rw [Notif.optRef_ne hw00]; simp⟩ + have hPS : ∀ r, optRef rv = some r → Fl4Spec.replyPush (Z := Unit) sd'.bv t.bv r false σ3 = + ({((), σ')}, False) := by + intro r hr + rw [Notif.optRef_ne hw00] at hr + cases hr + rw [hpush, heqP] + have hspec := receiveIpc_sendPushT_eq b rights blk hsr hkt hbN hk hks hse hstd hyesS e2' hA hks3 + (Or.inr (by rw [hft]; simp)) (by rw [hft]; rfl) hPS + have Fw := NatFoot.ofW Jb.xtop hWb (fun b0 hb0 => CnW.oobj hIb.2.2.1 hb0) (fun b0 hb0 => Jb.cnW_nv hb0) + (fun b0 hb0 => Jb.cnW_nh hb0) ((natK_rsfr Fr5).trans (natK_pushSt hkwa hkta (Ne.symm hws))) + have Ft := J.natTrans Fb Fw.cast + exact ⟨((), σ'), by rw [hspec]; exact Set.mem_singleton _, s, σ, Z.G, _, _, _, Or.inl ⟨rfl, rfl, rfl⟩, Ipc.CnK.refl σ, J, Vb, _, _, _, J6, Ft.cast⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvLeaves.lean b/proofs/Fl4Proofs/P3/VmRecvLeaves.lean new file mode 100644 index 000000000..127f58eb9 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvLeaves.lean @@ -0,0 +1,173 @@ +/- +P3 (line mmuAD, Stage 3 step 3.1, `RecvSenderPrefixes`): **the sender-transfer leaves at `VmEptState`**. + +* `VmEptState.recvLeafBasic`: `(SendOK · rc)` (no reply pushed): `JAtZB.recvSendJXFB` (P3/VmRecvFootB). +* `VmEptState.recvLeafPush`: `(PushOK · rc)`: at a resumed entry `JAtZB.recvSendP0XFB`; at a pending reschedule + `recvSendPJGFB`/`FPJGFB` (head sender without an SC) or `recvSendPDJFB`/`FPDJFB` (P3/VmRecvFootC; the sender donates its SC). +* `VmEptState.recvLeafTimeout`: `TmoOK`: `JAtZB.recvSendFPTJFB` (reply pushed) or `FTJFB` (P3/VmRecvFootD). +* `VmEptState.recvSenderLeaves` assembles the three, and `VmEptState.recvSenderPrefixes` is the producer of + `RecvSenderPrefixes`. + +[P] `P0.ArchTotal`, `maxTicksToUs` (`hmax`) and `ticks_to_us` (`htu`), as in the native twins. No other named premise. +-/ +import Fl4Proofs.P3.VmRecvFootB +import Fl4Proofs.P3.VmRecvFootC +import Fl4Proofs.P3.VmRecvFootD +import Fl4Proofs.P3.VmRecvFootX +import Fl4Proofs.P3.VmRecvFootXT +import Fl4Proofs.P3.VmEntryS3RecvEndpoint +import Fl4Proofs.P3.VmRecvFoot2 +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **The basic sender-transfer leaf** (module docstring). -/ +theorem VmEptState.recvLeafBasic [VxE Z] + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (ct e cb : U64) (sd g gr blocking : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) : + RecvFromSenderFor (L := L) arI AR Z s a σ ct e cb sd g gr blocking rc rc' (fun x => SendOK x rc) := by + intro r head queue sender receiver pl ht he hh hcur hkt hrun hact hnq hnrl hhead hk hks hss hnr hra hHd hHr hok + refine WP.spec_mono (JAtZB.recvSendJXFB (L := L) J.1.1 ht hkt hrun (hnr receiver hkt) hcur he hk hks cb sd true g gr + hra hok cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + +set_option maxHeartbeats 8000000 in +/-- **The pushed-reply sender-transfer leaf** (module docstring). -/ +theorem VmEptState.recvLeafPush [VxE Z] (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (ct e cb : U64) (sd g gr blocking : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) : + RecvFromSenderFor (L := L) arI AR Z s a σ ct e cb sd g gr blocking rc rc' (fun x => PushOK x rc) := by + intro r head queue sender receiver pl ht he hh hcur hkt hrun hact hnq hnrl hhead hk hks hss hnr hra hHd hHr hok + have hnqc : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p := by rw [hcur]; exact hnq + have hbN := hnr receiver hkt + obtain ⟨hrn, hyes, hcase⟩ := hok + have hrv0 : r ≠ 0#u64 := by + rcases hra with ⟨h1, -, -⟩ | ⟨_, _, _, _, hr, -⟩ + · exact absurd h1 hrn + · exact (J.1.1.rp.align r hr).2.1 + have hcov := (J.1.1.recvCov_ofW (P1.WritesIn.refl s _) he hHd hHr hra).2 hrv0 + have hti : σ.curThread ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs', -⟩ := J.1.1.act.2 + rw [← h, hcur, hkt] at hkx + cases hkx + rw [hxs'] at hrun + simp [Fl4Spec.runnable] at hrun + rcases hact with hres | hcn | ⟨u, hu, hne⟩ + · refine WP.spec_mono (JAtZB.recvSendP0XFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur ⟨hti, hres⟩ hnqc he hk hks cb sd true g gr + hra hcov ⟨hrn, hyes, hcase⟩ cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · by_cases hnd : sender.tcbSchedContext = none ∨ receiver.tcbSchedContext ≠ none + · have hAct : σ.schedulerAction = .resumeCurThread ∨ (σ.schedulerAction = .chooseNewThread ∧ ct.bv ∉ σ.releaseQueue ∧ + (sender.tcbSchedContext = none ∨ receiver.tcbSchedContext ≠ none)) := Or.inr ⟨hcn, hnrl, hnd⟩ + rcases hcase with ⟨hfs, hcl⟩ | ⟨f, hft, hto⟩ + · refine WP.spec_mono (JAtZB.recvSendPJGCFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur hti hnqc he hk hks hfs hAct + cb sd true g gr hra hcov hcl hyes hrn cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · refine WP.spec_mono (JAtZB.recvSendFPJGCFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur hti hnqc he hk hks hft hto hAct + cb sd true g gr hra hcov hyes hrn cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · rw [not_or, not_not] at hnd + obtain ⟨hsn, hxtn⟩ := hnd + obtain ⟨p0, hp0⟩ := Option.ne_none_iff_exists'.1 hsn + obtain ⟨y0, m0, hky, -⟩ := J.1.1.inv.back head.bv sender p0 hks hp0 + obtain ⟨p, hpC, rfl⟩ := J.1.1.obj.sc _ y0 m0 hky + rcases hcase with ⟨hfs, hcl⟩ | ⟨f, hft, hto⟩ + · refine WP.spec_mono (JAtZB.recvSendPDJFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur hti hnqc he hk hks hfs hpC hp0 + hxtn hcn hnrl cb sd true g gr hra hcov hcl hyes hrn cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · refine WP.spec_mono (JAtZB.recvSendFPDJFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur hti hnqc he hk hks hft + (fun b0 e0 => hsn (hto b0 e0)) hpC hp0 hxtn hcn hnrl cb sd true g gr hra hcov hyes hrn + cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · have hAct : ∃ u, σ.schedulerAction = .switchThread u ∧ u ≠ ct.bv := ⟨u, hu, hne⟩ + rcases hcase with ⟨hfs, hcl⟩ | ⟨f, hft, hto⟩ + · refine WP.spec_mono (JAtZB.recvSendPJGMFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur hti hnqc he hk hks hfs hAct + cb sd true g gr hra hcov hcl hyes hrn cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · refine WP.spec_mono (JAtZB.recvSendFPJGMFB (L := L) hAT J.1.1 ht hkt hrun hbN hcur hti hnqc he hk hks hft hto hAct + cb sd true g gr hra hcov hyes hrn cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + +set_option maxHeartbeats 8000000 in +/-- **The timeout-fault sender-transfer leaf** (module docstring). -/ +theorem VmEptState.recvLeafTimeout [VxE Z] (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (ct e cb : U64) (sd g gr blocking : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) : + RecvFromSenderFor (L := L) arI AR Z s a σ ct e cb sd g gr blocking rc rc' TmoOK := by + intro r head queue sender receiver pl ht he hh hcur hkt hrun hact0 hnq hnrl hhead hk hks hss hnr hra hHd hHr hok + obtain ⟨⟨bb, hft⟩, hsn⟩ := hok + have hbn := hnr receiver hkt + by_cases hp : rc ≠ .Null ∧ ∀ e' dd, sender.tcbState = .blockedOnSend e' dd → + dd.senderCanGrant = true ∨ dd.senderCanGrantReply = true + · have hrv0 : r ≠ 0#u64 := by + rcases hra with ⟨h1, -, -⟩ | ⟨_, _, _, _, hr, -⟩ + · exact absurd h1 hp.1 + · exact (J.1.1.rp.align r hr).2.1 + have hcov := (J.1.1.recvCov_ofW (P1.WritesIn.refl s _) he hHd hHr hra).2 hrv0 + have hnqc : ∀ dm p, σ.curThread ∉ σ.readyQueues dm p := by rw [hcur]; exact hnq + have hti : σ.curThread ≠ σ.idleThread := by + intro h + obtain ⟨x, hkx, -, hxs', -⟩ := J.1.1.act.2 + rw [← h, hcur, hkt] at hkx + cases hkx + rw [hxs'] at hrun + simp [Fl4Spec.runnable] at hrun + rcases hact0 with h | h | ⟨u, hu, hne⟩ + · refine WP.spec_mono (JAtZB.recvSendFPTJFB (L := L) hAT hmax htu J.1.1 ht hkt hrun hbn hcur hti hnqc he hk hks hft hsn + (Or.inl h) cb sd true g gr hra hcov hp.2 hp.1 cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · refine WP.spec_mono (JAtZB.recvSendFPTJFB (L := L) hAT hmax htu J.1.1 ht hkt hrun hbn hcur hti hnqc he hk hks hft hsn + (Or.inr ⟨h, hnrl⟩) cb sd true g gr hra hcov hp.2 hp.1 cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · refine WP.spec_mono (JAtZB.recvSendFPTJMFB (L := L) hAT hmax htu J.1.1 ht hkt hrun hbn hcur hti hnqc he hk hks hft hsn + ⟨u, hu, hne⟩ cb sd true g gr hra hcov hp.2 hp.1 cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + · refine WP.spec_mono (JAtZB.recvSendFTJFB (L := L) hAT hmax htu J.1.1 ht hkt hrun hbn hcur he hk hks hft hsn + cb sd true g gr hra (noPush_of hp) cb.bv (rightsOf sd true g gr)) ?_ + rintro out ⟨z, hz, hF⟩ + exact ⟨z, hz, J.recvFootV hF⟩ + +/-- **The three sender-transfer leaves** (module docstring). -/ +theorem VmEptState.recvSenderLeaves [VxE Z] (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (ct e cb : U64) (sd g gr blocking : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) : + RecvSenderLeaves (L := L) arI AR Z s a σ ct e cb sd g gr blocking rc rc' := + ⟨J.recvLeafBasic ct e cb sd g gr blocking rc rc', J.recvLeafPush hAT ct e cb sd g gr blocking rc rc', + J.recvLeafTimeout hAT hmax htu ct e cb sd g gr blocking rc rc'⟩ + +/-- **The producer of `RecvSenderPrefixes`** (module docstring): the leaves hold at any `VmEptState`. -/ +theorem recvSenderPrefixes (hAT : P0.ArchTotal arI) (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (H : P1.Heap) (Cs : Set U64) (s : S) (a : A) (σ : Fl4Spec.State Unit) (ct e cb : U64) (sd g gr blocking : Bool) + (rc : capability.Cap) (rc' : Fl4Spec.Cap) : + RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e cb sd g gr blocking rc rc' := + fun _ _ _ _ _ _ _ _ _ a' _ _ J => J.recvSenderLeaves hAT hmax (htu a') ct e cb sd g gr blocking rc rc' + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMid.lean b/proofs/Fl4Proofs/P3/VmRecvMid.lean new file mode 100644 index 000000000..a3480fbdc --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMid.lean @@ -0,0 +1,84 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2, matched-prefix `recvMid`, first part): **the receive at a mid state at +`VmEptState`**. + +* `VMidF σ`: the spec facts of the mid state after the first phase of ReplyRecv / NBSendRecv / NBSendWait (native + P3JMidCls `MidStF` without `cki`, plus at a pending reschedule `CurInv` or an SC-less current thread, native + `MidRun.CtOK`). **[J]** until its producer over `fl4HandleInvocationVF` is written. +* `JAtZB.canBlockMidB` (P3/VmMidBlock has `curScAcB` and `canBlockInvB`): P1's `CanBlock` of the current thread at any `VMidF` state (resumed, + pending switch, pending reschedule; native `JAtZ.canBlockMid`), at `JAtZB`. +* `VmEptState.handleRecvMidResumedV`: `handle_recv true cr` at a **resumed** mid state (native + `handle_recvMidSt`): the endpoint sender leaves are fed by `recvSenderPrefixes`. + +The pending switch / reschedule receive leaves are not here. + +[P] `P0.ArchTotal`, `maxTicksToUs` (`hmax`), `ticks_to_us` (`htu`). +-/ +import Fl4Proofs.P3.VmRecvLeaves +import Fl4Proofs.P3.XTopJointRecvMid +import Fl4Proofs.P3.XTopJointKEntry22M +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif + Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +/-- **[J] The spec facts of a mid state the receive reads** (module docstring). -/ +structure VMidF (σ : Fl4Spec.State Unit) : Prop where + ni : σ.curThread ≠ σ.idleThread + nq : ∀ d p, σ.curThread ∉ σ.readyQueues d p + vfh : ∀ x, σ.kheap σ.curThread = some (.tcb x) → Fl4Spec.validFaultHandler x.tcbFaultHandler + run : CurRun σ + nsw : σ.schedulerAction ≠ .switchThread σ.curThread + cho : σ.schedulerAction = .chooseNewThread → + CurInv σ ∨ ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 8000000 in +/-- **P1's `CanBlock` of the current thread at a mid state** (module docstring). -/ +theorem JAtZB.canBlockMidB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {ct : U64} (ht : ct ∈ Z.T) + (hcur : σ.curThread = ct.bv) {x : Fl4Spec.Tcb} (hk : σ.kheap ct.bv = some (.tcb x)) (hF : VMidF σ) : + P1.CanBlock st (P1.Heap.tcbs H) Ac s ct := by + have hk' : σ.kheap σ.curThread = some (.tcb x) := by rw [hcur]; exact hk + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x hk' + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hF.nq + have hsw : ∀ v, σ.schedulerAction = .switchThread v → v ≠ ct.bv := fun v hv e => by + exact hF.nsw (by rw [hv, e, hcur]) + have hgen : σ.schedulerAction = .resumeCurThread ∨ + (σ.schedulerAction = .chooseNewThread ∧ x.tcbSchedContext = none) → ct.bv ∉ σ.releaseQueue → + P1.CanBlock st (P1.Heap.tcbs H) Ac s ct := fun hsa hnr => + J.canBlockCurG ht hcur hk hrn hF.ni hnr hsa hF.nq + have hinv : CurInv σ → P1.CanBlock st (P1.Heap.tcbs H) Ac s ct := fun hC => + J.canBlockInvB ht hcur hk hC hF.ni hrn hnq' hsw + cases hsa : σ.schedulerAction with + | resumeCurThread => exact hinv (J.curInv (by rw [hsa]; intro e; cases e)) + | switchThread u => exact hinv (J.curInv (by rw [hsa]; intro e; cases e)) + | chooseNewThread => + rcases hF.cho hsa with hC | h0 + · exact hinv hC + · exact hgen (Or.inr ⟨hsa, h0 x hk'⟩) (by rw [← hcur]; exact J.inv.notRel_of_noSc hk' (h0 x hk')) + +set_option maxHeartbeats 8000000 in +/-- **`handle_recv` at a resumed mid state** (module docstring; native `handle_recvMidSt`). -/ +theorem VmEptState.handleRecvMidResumedV [VxE Z] (hAT : P0.ArchTotal arI) + (hmax : Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv) + (htu : ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hres : σ.schedulerAction = .resumeCurThread) {ct : U64} (hct : sched.cur_thread st s = ok ct) + (canReply : Bool) : + syscall.handle_recv st arI s a true canReply ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) true canReply σ).1, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := + J.handleRecvReachedV hAT ⟨hF.ni, hres⟩ hct hF.nq hF.vfh true canReply + (fun e b sd g gr rc rc' _ _ _ _ => + recvSenderPrefixes hAT hmax htu H Cs s a σ ct e b sd g gr true rc rc') + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidAsm.lean b/proofs/Fl4Proofs/P3/VmRecvMidAsm.lean new file mode 100644 index 000000000..ddce19455 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidAsm.lean @@ -0,0 +1,201 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2, first generated by `handoff/tools/gen_midasm.py`, now hand-edited): **the endpoint +receive assembled at a resumed or choose mid state** (`CurInv`, or an SC-less current thread at choose through the +donating cells of P3/VmRecvMidDonV): `recvIpcActCaseMVE`, `recvFreeEndpointMV`, `recvEndpointReachedMV`. +The pending-switch cells are not here. No named premise beyond `P0.ArchTotal` and the platform's `MIN_BUDGET`. +-/ +import Fl4Proofs.P3.VmRecvMidEp2 +import Fl4Proofs.P3.VmRecvMidDonV +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- Resolve an endpoint receive that completes its active bound notification, with a null or free reply. -/ +theorem VmEptState.recvIpcActCaseMVE (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hAct : RecvIpcActCase (L := L) ct s σ rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨⟨xt, np, xn, badge, hkt, hbn, hkn, hactive⟩, hrr⟩ := hAct + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + obtain ⟨rv, hra⟩ := B.recvReplyArg hRc hrr + rcases hF.inv_or_N B hsa with hC | ⟨hch, hN⟩ + swap + · rw [show absCap (.Endpoint p b sd true g gr) = .endpointCap p.bv b.bv (rightsOf sd true g gr) from rfl] + exact J.recvIpcActDMVE hAT hmb ht hcur hF hch hN hkt hn hkn hbn hactive blocking hke + b sd true g gr hra b.bv (rightsOf sd true g gr) + have hxt := hI.1.toE.at_tcb ht hkt + have hb : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hbn + have hb0 : L.mem s (tBoundA ct) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb; cases hb + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hb0] at hb + exact P1.eq_of_bv (Option.some.inj hb) + have hst : stOf (L.mem s) n = obj.NTFN_ACTIVE := + stOf_active (T := Z.T) (hactive ▸ (hI.1.toN.at_ntfn hn hkn).obj) rfl + let rights := rightsOf sd true g gr + rw [show absCap (.Endpoint p b sd true g gr) = .endpointCap p.bv b.bv rights from rfl, + receiveIpc_active b.bv rights blocking (replyArg_spec B.ip.e hI.1.toE hra) hkt hbn hke hkn hactive] + exact receive_ipc_preA s a ct p b sd true g gr blocking rc rc' rv hra hbound + (by rw [← hbound]; exact hb0) hst (J.recvActiveMidVE hAT ht hcur hC hF.ni hF.run hF.nsw hnq hn hkn hactive blocking b.bv rights).2 + +set_option maxHeartbeats 8000000 in +/-- The free-reply endpoint receive at a resumed or choose mid state keeping `CurInv`. -/ +theorem VmEptState.recvFreeEndpointMV [VxE Z] + (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (e cb : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap e.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hFree : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64) + (hSender : ∀ s' σ' Z' [VxE Z'] Ac' Lst', + RecvCtxReached (L := L) ct blocking s σ s' σ' → + VmEptState (L := L) arI V X XL AR Z' H Cs Ac' Lst' D s' a σ' → + RecvSenderLeaves (L := L) arI AR Z' s' a σ' ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hPoll : RecvIpcPollCase (L := L) ct blocking s σ e rc + · exact J.recvIpcPollCaseVE ht blocking e cb sd g gr rc rc' hEp hRc hPoll + by_cases hAct : RecvIpcActCase (L := L) ct s σ rc + · exact J.recvIpcActCaseMVE hAT hmb hF hsa hnq ht hcur blocking e cb sd g gr rc rc' hEp hRc hAct + by_cases hWait : RecvIpcBlockCase (L := L) ct blocking s σ e rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ e rc + · exact J.recvIpcWaitCaseMVE hF hsa hnq ht hcur blocking e cb sd g gr rc rc' hEp hRc hHd hHr hWait + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + have hrun := hF.run xt (by rw [hcur]; exact hkt) + have hq : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hrl : ct.bv ∉ σ.releaseQueue := by + rcases hF.inv_or_noSc B hsa (by rw [hcur]; exact hkt) with hC | h0 + · rw [← hcur]; exact hC.notRel + · rw [← hcur]; exact B.inv.notRel_of_noSc (by rw [hcur]; exact hkt) h0 + have hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false := by + cases hbn : xt.tcbBoundNotification with + | none => exact Or.inl rfl + | some n => + obtain ⟨xn, hkn⟩ := B.sv.tcbNtfn ct.bv xt n hkt hbn + refine Or.inr ⟨n, xn, rfl, hkn, ?_⟩ + cases hxo : xn.ntfnObj with + | activeNtfn b => exact (hAct ⟨⟨xt, n, xn, b, hkt, hbn, hkn, hxo⟩, hFree⟩).elim + | idleNtfn => simp [Fl4Spec.isActive, hxo] + | waitingNtfn q => simp [Fl4Spec.isActive, hxo] + have hkSend : ∃ queue, σ.kheap e.bv = some (.endpoint (.sendEP queue)) := by + by_contra hSend + have hn : ∀ x, σ.kheap e.bv = some (.endpoint x) → ∀ queue, x ≠ .sendEP queue := by + rintro x hx q rfl + exact hSend ⟨q, hx⟩ + cases blocking + · refine hPoll ⟨rfl, ?_, hn, hFree⟩ + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + exact hbN + · rcases hbN with hb | ⟨n, xn, hb, hkn, hna⟩ + · refine hWait (Or.inl ⟨rfl, ?_, hn, hFree⟩) + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + exact hb + · exact hWait (Or.inr ⟨rfl, ⟨xt, n, xn, hkt, hb, hkn, hna⟩, hn, hFree⟩) + obtain ⟨queue, hk⟩ := hkSend + obtain ⟨ee, he, eq⟩ := B.obj.ep _ (.sendEP queue) hk + rw [Sched.bv_inj eq] at he + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hFree + by_cases hReturn : blocking = true ∧ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none + · obtain ⟨rfl, np, xn, hb, hkn, hsc⟩ := hReturn + have hna : Fl4Spec.isActive xn = false := by + rcases hbN with h | ⟨n', xn', hb', hkn', h⟩ + · rw [hb] at h + cases h + · rw [hb] at hb' + cases hb' + rw [hkn] at hkn' + cases hkn' + exact h + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + have hC : CurInv σ := by + rcases hF.inv_or_noSc B hsa (by rw [hcur]; exact hkt) with hC | h0 + · exact hC + · exact absurd (hsc.1.trans h0) hsc.2 + exact J.recvSendingReturnMV ht he hcur hF hC hsa hnq hkt hn hkn hb hna hsc hk hra hHd hHr + (fun s' σ' Ac' Lst' hread hc hs J' => + hSender s' σ' (Z.mv Z.G Z.vs) Ac' Lst' (Or.inr ⟨n, rfl, hread, hc, hs⟩) J') + · have hno : RecvNoReturn ct blocking σ := by + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + rcases hbN with hb | ⟨n, xn, hb, hkn, hna⟩ + · exact Or.inl hb + · exact Or.inr ⟨n, xn, hb, hkn, hna, fun hbl hsc => hReturn ⟨hbl, n, xn, hb, hkn, hsc⟩⟩ + exact J.recvSendingNoReturnV ht he hcur hkt hrun (hsa.imp_right Or.inl) hq hrl hk hno + hra hHd hHr + (hSender s σ Z Ac Lst (Or.inl ⟨rfl, rfl⟩) J) + +set_option maxHeartbeats 8000000 in +/-- The endpoint leaf of receive at a resumed or choose mid state keeping `CurInv`. -/ +theorem VmEptState.recvEndpointReachedMV [VxE Z] + (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (e cb : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap e.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hSender : RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hFree : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 + · exact J.recvFreeEndpointMV hAT hmb ht hcur hF hsa hnq blocking e cb sd g gr rc rc' hEp hRc hHd hHr hFree + (fun s' σ' Z' [VxE Z'] Ac' Lst' hCtx J' => + hSender V X XL Z' Ac' Lst' D s' a σ' ⟨s, σ, Or.inl ⟨rfl, rfl, rfl⟩, hCtx⟩ J') + rcases hRc with ⟨hr, -⟩ | ⟨hcr, q, g', rfl, xr, hkr⟩ + · exact (hFree (Or.inl hr)).elim + · have hne : L.mem s (Endp.rTcbA q) ≠ 0#u64 := fun h => hFree (Or.inr ⟨q, g', rfl, h⟩) + refine J.recvIpcLinkedMVE ht hcur hF hsa hnq blocking e cb sd g gr q g' rc' hcr hEp hkr hHd (hHr q g' rfl) hne ?_ + intro i V' Ac' Lst' s' a' σ' hread hi0 hiNe hc hs J' hcur' hF' hsa' _hC' hnq' hEp' hx h0' hHd' hHr' + obtain ⟨xr', hkr', -⟩ := hx + exact J'.recvFreeEndpointMV hAT hmb ht hcur' hF' hsa' hnq' blocking e cb sd g gr (.Reply q g') rc' hEp' + (Or.inr ⟨hcr, q, g', rfl, xr', hkr'⟩) hHd' (fun _ _ _ => hHr') (Or.inr ⟨q, g', rfl, h0'⟩) + (fun s2 σ2 Z2 [VxE Z2] Ac2 Lst2 hCtx J2 => + hSender V X XL Z2 Ac2 Lst2 D s2 a' σ2 + ⟨s', σ', Or.inr ⟨q, g', i, rfl, hread, hi0, hiNe, hc, hs⟩, hCtx⟩ J2) + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidDon.lean b/proofs/Fl4Proofs/P3/VmRecvMidDon.lean new file mode 100644 index 000000000..9cde90597 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidDon.lean @@ -0,0 +1,370 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 1, generated by `handoff/tools/gen_middon.py`): **the active-notification +receive donating an SC to the SC-less running current thread at a choose mid state, at `JAtZB` with the native +footprint**: `JAtZB.recvSigDonMB` (native P3/XTopJointRecvSigDonD `JAtZ.recvSigDonJNQ`) and `JAtZB.recvIpcActDMB` +(P3/XTopJointRecvIpcActDonD `JAtZ.recvIpcActDJNQ`, the bound notification of an endpoint receive), donating or not; +proofs verbatim over `JAtZB` (`regWrJFixed`, `canBlockCurG`, `JAtZB.wake_postD`), the spec's `NatK` by +`nkt_maybeDonateSc` / `nkt_specUnblock`. No named premise beyond `P0.ArchTotal` and the platform's `MIN_BUDGET` +(`hmb`, [P]). +-/ +import Fl4Proofs.P3.VmRecvMidNtfn +import Fl4Proofs.P3.XTopJointRecvIpcActDonD +import Fl4Proofs.P3.JAtZV3SigDon +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The spec's wake-up tail changes only SC-code objects. -/ +theorem nkt_specUnblock (t : Fl4Spec.ObjRef) : NKT (specUnblock t) := by + unfold specUnblock + exact nkt_bind (nkt_threadGet _ _) fun o => nkt_ifCondRefillUnblockCheck o _ _ + +set_option maxHeartbeats 32000000 in +/-- **The notification receive from an active notification at a choose mid state of an SC-less current thread, donating +or not, at `JAtZB`** (module docstring). -/ +theorem JAtZB.recvSigDonMB (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) + (hF : VMidF σ) (hch : σ.schedulerAction = .chooseNewThread) + (hN : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blk : Bool) (cb : Fl4Spec.Badge) (cr : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n blk ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blk σ).1, + JAtZB (L := L) arI AR Z H Cs Ac Lst D r.1 r.2 z.2 ∧ NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hsc0 : xt.tcbSchedContext = none := hN xt (by rw [hcur]; exact hkt) + have hrn : Fl4Spec.runnable xt.tcbState = true := hF.run xt (by rw [hcur]; exact hkt) + have hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p := hF.nq + have hcb := J.canBlockCurG ht hcur hkt hrn hF.ni (J.inv.notRel_of_noSc hkt hsc0) (Or.inr ⟨hch, hsc0⟩) hnq + have hx := hI.1.toN.at_ntfn hn hk + have hst : stOf (L.mem s) n = obj.NTFN_ACTIVE := stOf_active (T := Z.T) (hact ▸ hx.obj) rfl + have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + -- P1's `BlkOK` after the whole receive + obtain ⟨⟨Q0, R0, h0'⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have p1 := P1.notification.receive_signal.ipc hAT s a t n blk Q0 R0 h0' hB0 hX0 hS0 hSg0 hcb + hHn hHn (by rw [epState_eq, hst, ntfn_consts.2.2]; decide) + -- the badge register at `J` + have hXb := badge_wr_irel J.ip hI ht hkt (L.mem s (nBadgeA n)) + rw [badgeRegA_eq_regA] at hXb + obtain ⟨J1, FR1⟩ := J.regWrJFixed ht hkt Fl4Spec.badge_register _ hXb + rw [hb0, regIdx_badge] at J1 FR1 + have hnt : n.bv ≠ t.bv := by intro e; rw [e, hkt] at hk; cases hk + set xt1 := setReg xt Fl4Spec.badge_register b0 with hxt1 + set σ1 := updK σ t.bv (.tcb xt1) with hσ1 + set s1 := L.wr s (regA t layout.reg.BADGE) (L.mem s (nBadgeA n)) with hs1 + have hk1 : σ1.kheap n.bv = some (.notification x) := by simp [hσ1, updK, hnt, hk] + have hkt1 : σ1.kheap t.bv = some (.tcb xt1) := by simp [hσ1, updK] + have hcur1 : σ1.curThread = t.bv := hcur + have hrn1 : Fl4Spec.runnable xt1.tcbState = true := hrn + have hsc1 : xt1.tcbSchedContext = none := hsc0 + have hch1 : σ1.schedulerAction = .chooseNewThread := hch + have hnq1 : ∀ d p, σ1.curThread ∉ σ1.readyQueues d p := hnq + have hnr1 : t.bv ∉ σ1.releaseQueue := J1.inv.notRel_of_noSc hkt1 hsc1 + have hcb1 := J1.canBlockCurG ht hcur1 hkt1 hrn1 hF.ni hnr1 (Or.inr ⟨hch1, hsc1⟩) hnq1 + -- the notification's state, FL4 on `IRel` from `J1` + have hI1 := J1.xtop.irelK J1.ks J1.buf + have hx1 := hI1.1.toN.at_ntfn hn hk1 + have hst1 : stOf (L.mem s1) n = obj.NTFN_ACTIVE := stOf_active (T := Z.T) (hact ▸ hx1.obj) rfl + obtain ⟨hR2, hW2, hS2⟩ := set_idle_relI J1.ip hI1.1 hn hk1 hact + set σ2 := updK σ1 n.bv (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x)) with hσ2 + set s2 := L.wr s1 (P1.epTSA n) ((L.mem s1 (P1.epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) with hs2 + have hP := J1.ip.e + have hSRT2 := hI1.2 + have hX2 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s2 σ2 := + ⟨hR2, hSRT2.1.stableN hSRT2.2.1.placed hW2 hS2, hSRT2.2.1.stable hW2 hS2, + hSRT2.2.2.mono hW2.1 (SzKeep.ntfnStep hS2)⟩ + have hSS2 : IFrame (L := L) Z.th s1 s2 := + IRel.iframe_of hI1 hW2 fun b hb => iwords_oobj (NWrite.iwords hP.tcb hb) + have hTW2 : FinStep (st := st) Z.T Z.N Z.C Z.c s1 σ1 s2 σ2 := + ⟨hW2.mono fun b hb => NWrite.finW hP.tcb hb, SzKeepC.of (SzKeep.ntfnStep hS2)⟩ + -- the spec at the state after the notification write + have hN0 : NObj x.ntfnObj (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x).ntfnObj := + Or.inr ⟨fun l h => (by rw [hact] at h; cases h), fun l h => (by cases h)⟩ + have F1 : NFr true n.bv σ1 σ2 := NFr.upd hk1 (Or.inr (Or.inr ⟨x, _, rfl, rfl, rfl, hN0.queue, fun _ => hN0, + Or.inl rfl, fun _ => hN0, Or.inl rfl⟩)) + have I2 : Inv σ2 := J1.inv.updNtfn hk1 fun p hp => J1.inv.ntfnSc n.bv x p hk1 hp + have hN2 : NSC Z.N Z.C σ2 := J1.nsc.step hS2 + have hkt2 : σ2.kheap t.bv = some (.tcb xt1) := by + simp only [hσ2, updK, Function.update_of_ne hnt.symm]; exact hkt1 + have hkn2 : σ2.kheap n.bv = some (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x)) := by + simp [hσ2, updK] + have hDon : DonOK st Z.C s2 σ2 t n := + ⟨I2.relOK, fun h0 => NSC.mem hN2 hR2.toN hn h0, fun _ y _ => by rw [← hcur]; exact hnq _ _, + I2.actReady⟩ + -- FL4: the run from `s` with the two P3 views of the tail + obtain ⟨⟨Q1, R1, h1q⟩, hB1, hX1, hS1, hSg1⟩ := J1.blk + have hfootT := P1.notification.recv_donate_tail.foot (Es := P1.EH H) hAT s1 a t n Q1 R1 + h1q hB1 hX1 hS1 hSg1 hcb1 hHn hHn (Or.inr hHn) + (by rw [epState_eq, hst1, ntfn_consts.2.2]) + have hirT : (do + let s2 ← obj.Ntfn.set_state st n s1 obj.NTFN_IDLE + rustRecvTail st arI s2 a t n) ⦃ r => r.2 = a ∧ ∃ σd σ', + Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ2 = ({((), σd)}, False) ∧ + specUnblock t.bv σd = ({((), σ')}, False) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c r.1 σ' ∧ + IFrame (L := L) Z.th s1 r.1 ∧ FinStep (st := st) Z.T Z.N Z.C Z.c s1 σ1 r.1 σ' ⦄ := by + refine WP.spec_bind (P1.obj.Ntfn.set_state.val _ n obj.NTFN_IDLE) ?_ + rintro s2' rfl + refine WP.spec_mono (recvTail_irelD J1.ip hmb s2 a σ2 hX2 ht hn hDon) ?_ + rintro r ⟨hra, σd, σ', hd, hu, hX', hSS', hTW'⟩ + exact ⟨hra, σd, σ', hd, hu, hX', hSS2.trans hSS', hTW2.trans hTW'⟩ + have hmain : notification.receive_signal st arI s a t n blk ⦃ r => r.2 = a ∧ (P1.DonCore st (P1.Heap.tcbs H) + (P1.Heap.scs H) n s1 → P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s1 r.1) ∧ ∃ σd σ', + Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ2 = ({((), σd)}, False) ∧ + specUnblock t.bv σd = ({((), σ')}, False) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c r.1 σ' ∧ + IFrame (L := L) Z.th s1 r.1 ∧ FinStep (st := st) Z.T Z.N Z.C Z.c s1 σ1 r.1 σ' ⦄ := by + unfold notification.receive_signal + refine WP.spec_bind (P1.obj.Ntfn.state.spec s n) ?_ + rintro v rfl + rw [epState_eq, hst, 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)] + refine WP.spec_bind (obj.Ntfn.badge.spec s n) ?_ + rintro bb rfl + refine WP.spec_bind (obj.Tcb.set_reg.spec s t layout.reg.BADGE _) ?_ + rintro s1' rfl + exact WP.spec_mono (WP.spec_and hfootT hirT) fun r ⟨hf, hra, hR⟩ => ⟨hra, hf, hR⟩ + refine WP.spec_mono (WP.spec_and hmain p1) ?_ + rintro ⟨s', a'⟩ ⟨⟨hra, hfoot, σd, σ', hd, hu, hIR, hFr, hFS⟩, hbk⟩ + simp only [WP.uncurry'_pair] at hbk hra hfoot hIR hFr hFS + subst hra + -- the spec's side + have hsp2 : ∀ c y k, σ2.kheap c = some (.schedContext y k) → y.scSporadic = true → Fl4Spec.scActive y = true := by + intro c y k hc + have hcn : c ≠ n.bv := by rintro rfl; rw [hkn2] at hc; cases hc + have hct : c ≠ t.bv := by rintro rfl; rw [hkt2] at hc; cases hc + have hc0 : σ.kheap c = some (.schedContext y k) := by + simpa [hσ2, hσ1, updK, hcn, hct] using hc + exact J.sv.scSpor c y k hc0 + have hnqT : NotQueued σ2 t.bv := ⟨fun d p => by rw [← hcur]; exact hnq d p, hnr1⟩ + obtain ⟨I', B', FF, AI', cur', run', -⟩ := recv_tailDN (r := n.bv) I2 (F1.bsa J1.bsa) + ⟨fun u hu => F1.sched (J1.act.1 u hu), F1.idleOK J1.act.2⟩ (Or.imp id F1.curInv J1.cur) + (Or.imp id F1.curRunW J1.curRun) hkt2 hrn1 hkn2 hnqT (F1.off J1.off) hsp2 hd + (by rw [hu]; exact Set.mem_singleton _) + -- `JAtZ` after, from `J1` + have hw : P1.WritesIn st s1 s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s1 σ1) := + hFS.1.mono fun b h => Or.inl h + have hRR : ∀ q ∈ Z.R, ∀ xr, σ1.kheap q.bv = some (.reply xr) → xr.replyTcb = some t.bv → + Detached Z.C Lst q ∧ L.mem s1 (P1.rNextA q) = 0#u64 ∧ L.mem s1 (P1.rPrevA q) = 0#u64 := by + intro q _ xr hq hxt + exfalso + obtain ⟨x', hk', hs'⟩ := J1.sv.replyTcb q.bv xr t.bv hq hxt + rw [hkt1] at hk'; cases hk' + rcases hs' with e | ⟨ep, dd, e⟩ <;> rw [e] at hrn1 <;> cases hrn1 + have htD := J1.not_dead_of_st ht hkt1 (by rcases runnable_code hrn1 with h | h | h <;> rw [h] <;> decide) + have J' := J1.wake_postD hIR hFr hw hbk (hfoot (J1.donCore hn)) ((DWFr.ofNFr F1.weak).trans FF) I' B' + AI' cur' run' hRR htD ⟨t, ht, rfl⟩ + -- the spec's outcome + have hspec : Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blk σ = + specRecvTail t.bv n.bv σ2 := by + rw [receiveSignal_cap hk] + simp only [hact] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt), nd_bind_one (setNotification_eq _ hk1)] + have hsT : specRecvTail t.bv n.bv σ2 = specUnblock t.bv σd := by + unfold specRecvTail; rw [nd_bind_one hd]; rfl + rw [hspec, hsT, hu] + have hK : NatK σ1 σ' := by + have m1 : ((), σd) ∈ (Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ2).1 := by rw [hd]; exact Set.mem_singleton _ + have m2 : ((), σ') ∈ (specUnblock t.bv σd).1 := by rw [hu]; exact Set.mem_singleton _ + exact (natK_ntfnUpd hk1).trans (natK_tk ((nkt_maybeDonateSc t.bv n.bv σ2 _ m1).trans (nkt_specUnblock t.bv σd _ m2))) + exact ⟨_, Set.mem_singleton _, J', + J.natTrans FR1 (NatFoot.ofW J1.xtop hFS.1 (fun _ hb => Ipc.FinW.oobj J1.xtop.fin.1.1.1 hb) + (fun _ hb => Ipc.FinW.nv J1.xtop.fin.1.1.1 hb) (fun _ hb => J1.finW_nh hb) hK)⟩ + +set_option maxHeartbeats 32000000 in +/-- **`receive_ipc` with an active bound notification at a choose mid state of an SC-less current thread, donating or +not, at `JAtZB`** (module docstring). -/ +theorem JAtZB.recvIpcActDMB (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) + (hF : VMidF σ) (hch : σ.schedulerAction = .chooseNewThread) + (hN : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hbn : xt.tcbBoundNotification = some n.bv) {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blk : Bool) {e : U64} {xe : Fl4Spec.Endpoint} (hke : σ.kheap e.bv = some (.endpoint xe)) + (cb : U64) (c1 c2 c3 c4 : Bool) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) blk rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + JAtZB (L := L) arI AR Z H Cs Ac Lst D r.1 r.2 z.2 ∧ NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hsc0 : xt.tcbSchedContext = none := hN xt (by rw [hcur]; exact hkt) + have hrn : Fl4Spec.runnable xt.tcbState = true := hF.run xt (by rw [hcur]; exact hkt) + have hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p := hF.nq + have hcb := J.canBlockCurG ht hcur hkt hrn hF.ni (J.inv.notRel_of_noSc hkt hsc0) (Or.inr ⟨hch, hsc0⟩) hnq + have hx := hI.1.toN.at_ntfn hn hk + have hst : stOf (L.mem s) n = obj.NTFN_ACTIVE := stOf_active (T := Z.T) (hact ▸ hx.obj) rfl + have hb0 : (L.mem s (nBadgeA n)).bv = b0 := by + have := hx.obj; rw [hact] at this; exact this.2.2 + have hHn : H n = some .ntfn := J.zH.2.2.2 n hn + -- P1's `BlkOK` after `complete_signal` + obtain ⟨⟨Q0, R0, h0'⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have p1 := P1.notification.complete_signal.ipcC hAT s a n t Q0 R0 h0' hB0 hX0 hS0 hSg0 hcb hHn hHn + -- the badge register at `J` + have hXb := badge_wr_irel J.ip hI ht hkt (L.mem s (nBadgeA n)) + rw [badgeRegA_eq_regA] at hXb + obtain ⟨J1, FR1⟩ := J.regWrJFixed ht hkt Fl4Spec.badge_register _ hXb + rw [hb0, regIdx_badge] at J1 FR1 + have hnt : n.bv ≠ t.bv := by intro e; rw [e, hkt] at hk; cases hk + set xt1 := setReg xt Fl4Spec.badge_register b0 with hxt1 + set σ1 := updK σ t.bv (.tcb xt1) with hσ1 + set s1 := L.wr s (regA t layout.reg.BADGE) (L.mem s (nBadgeA n)) with hs1 + have hk1 : σ1.kheap n.bv = some (.notification x) := by simp [hσ1, updK, hnt, hk] + have hkt1 : σ1.kheap t.bv = some (.tcb xt1) := by simp [hσ1, updK] + have hcur1 : σ1.curThread = t.bv := hcur + have hrn1 : Fl4Spec.runnable xt1.tcbState = true := hrn + have hsc1 : xt1.tcbSchedContext = none := hsc0 + have hch1 : σ1.schedulerAction = .chooseNewThread := hch + have hnq1 : ∀ d p, σ1.curThread ∉ σ1.readyQueues d p := hnq + have hnr1 : t.bv ∉ σ1.releaseQueue := J1.inv.notRel_of_noSc hkt1 hsc1 + have hcb1 := J1.canBlockCurG ht hcur1 hkt1 hrn1 hF.ni hnr1 (Or.inr ⟨hch1, hsc1⟩) hnq1 + -- the notification's state, FL4 on `IRel` from `J1` + have hI1 := J1.xtop.irelK J1.ks J1.buf + have hx1 := hI1.1.toN.at_ntfn hn hk1 + have hst1 : stOf (L.mem s1) n = obj.NTFN_ACTIVE := stOf_active (T := Z.T) (hact ▸ hx1.obj) rfl + obtain ⟨hR2, hW2, hS2⟩ := set_idle_relI J1.ip hI1.1 hn hk1 hact + set σ2 := updK σ1 n.bv (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x)) with hσ2 + set s2 := L.wr s1 (P1.epTSA n) ((L.mem s1 (P1.epTSA n) &&& ~~~3#u64) ||| (obj.NTFN_IDLE &&& 3#u64)) with hs2 + have hP := J1.ip.e + have hSRT2 := hI1.2 + have hX2 : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s2 σ2 := + ⟨hR2, hSRT2.1.stableN hSRT2.2.1.placed hW2 hS2, hSRT2.2.1.stable hW2 hS2, + hSRT2.2.2.mono hW2.1 (SzKeep.ntfnStep hS2)⟩ + have hSS2 : IFrame (L := L) Z.th s1 s2 := + IRel.iframe_of hI1 hW2 fun b hb => iwords_oobj (NWrite.iwords hP.tcb hb) + have hTW2 : FinStep (st := st) Z.T Z.N Z.C Z.c s1 σ1 s2 σ2 := + ⟨hW2.mono fun b hb => NWrite.finW hP.tcb hb, SzKeepC.of (SzKeep.ntfnStep hS2)⟩ + -- the spec at the state after the notification write + have hN0 : NObj x.ntfnObj (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x).ntfnObj := + Or.inr ⟨fun l h => (by rw [hact] at h; cases h), fun l h => (by cases h)⟩ + have F1 : NFr true n.bv σ1 σ2 := NFr.upd hk1 (Or.inr (Or.inr ⟨x, _, rfl, rfl, rfl, hN0.queue, fun _ => hN0, + Or.inl rfl, fun _ => hN0, Or.inl rfl⟩)) + have I2 : Inv σ2 := J1.inv.updNtfn hk1 fun p hp => J1.inv.ntfnSc n.bv x p hk1 hp + have hN2 : NSC Z.N Z.C σ2 := J1.nsc.step hS2 + have hkt2 : σ2.kheap t.bv = some (.tcb xt1) := by + simp only [hσ2, updK, Function.update_of_ne hnt.symm]; exact hkt1 + have hkn2 : σ2.kheap n.bv = some (.notification (Fl4Spec.ntfnObjUpdate (fun _ => .idleNtfn) x)) := by + simp [hσ2, updK] + have hnqT : NotQueued σ2 t.bv := ⟨fun d p => by rw [← hcur]; exact hnq d p, hnr1⟩ + have hDon : DonOK st Z.C s2 σ2 t n := + ⟨I2.relOK, fun h0 => NSC.mem hN2 hR2.toN hn h0, fun _ y _ => by rw [← hcur]; exact hnq _ _, + I2.actReady⟩ + -- FL4: the run from `s` with the two P3 views of the tail + obtain ⟨⟨Q1, R1, h1q⟩, hB1, hX1, hS1, hSg1⟩ := J1.blk + have hfootT := P1.notification.complete_donate_tail.foot (Es := P1.EH H) hAT s1 a t n Q1 R1 + h1q hB1 hX1 hS1 hSg1 hcb1 hHn hHn (Or.inr hHn) + (by rw [epState_eq, hst1, ntfn_consts.2.2]) + have hirT : (do + let s2 ← obj.Ntfn.set_state st n s1 obj.NTFN_IDLE + rustCompleteTail st arI s2 a t n) ⦃ r => r.2 = a ∧ ∃ σd σ', + Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ2 = ({((), σd)}, False) ∧ + specUnblock t.bv σd = ({((), σ')}, False) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c r.1 σ' ∧ + IFrame (L := L) Z.th s1 r.1 ∧ FinStep (st := st) Z.T Z.N Z.C Z.c s1 σ1 r.1 σ' ⦄ := by + refine WP.spec_bind (P1.obj.Ntfn.set_state.val _ n obj.NTFN_IDLE) ?_ + rintro s2' rfl + refine WP.spec_mono (completeTail_irelD J1.ip hmb s2 a σ2 hX2 ht hn hDon + (fun σd hd => donShape_of I2 hkt2 hkn2 hnqT hsc1 hd)) ?_ + rintro r ⟨hra, σd, σ', hd, hu, hX', hSS', hTW'⟩ + exact ⟨hra, σd, σ', hd, hu, hX', hSS2.trans hSS', hTW2.trans hTW'⟩ + have ht0 : t ≠ 0#u64 := fun e => by + obtain ⟨Q1, R1, hc1, -⟩ := J.xtop.fin.1.1.1.lists + exact (hc1.tcbs t ht).ne0 (by rw [e]; rfl) + have hmain : notification.complete_signal st arI s a n t ⦃ r => r.2 = a ∧ (P1.DonCore st (P1.Heap.tcbs H) + (P1.Heap.scs H) n s1 → P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s1 r.1) ∧ ∃ σd σ', + Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ2 = ({((), σd)}, False) ∧ + specUnblock t.bv σd = ({((), σ')}, False) ∧ + IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c r.1 σ' ∧ + IFrame (L := L) Z.th s1 r.1 ∧ FinStep (st := st) Z.T Z.N Z.C Z.c s1 σ1 r.1 σ' ⦄ := by + unfold notification.complete_signal + step with P1.obj.Tcb.is_null.spec as ⟨bn, hbn'⟩ + simp only [hbn', ht0, decide_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (P1.obj.Ntfn.state.spec s n) ?_ + rintro v rfl + rw [epState_eq, hst] + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (obj.Ntfn.badge.spec s n) ?_ + rintro bb rfl + refine WP.spec_bind (obj.Tcb.set_reg.spec s t layout.reg.BADGE _) ?_ + rintro s1' rfl + exact WP.spec_mono (WP.spec_and hfootT hirT) fun r ⟨hf, hra, hR⟩ => ⟨hra, hf, hR⟩ + -- `receive_ipc` is `complete_signal` + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + refine WP.spec_mono (receive_ipc_preA (ar := arI) s a t e cb c1 c2 c3 c4 blk rc rc' rv hrc hbound + (by rw [← hbound]; exact hbz) hst (WP.spec_and hmain p1)) ?_ + rintro ⟨s', a'⟩ ⟨⟨hra, hfoot, σd, σ', hd, hu, hIR, hFr, hFS⟩, hbk⟩ + simp only [WP.uncurry'_pair] at hbk hra hfoot hIR hFr hFS + subst hra + -- the spec's side + have hsp2 : ∀ c y k, σ2.kheap c = some (.schedContext y k) → y.scSporadic = true → Fl4Spec.scActive y = true := by + intro c y k hc + have hcn : c ≠ n.bv := by rintro rfl; rw [hkn2] at hc; cases hc + have hct : c ≠ t.bv := by rintro rfl; rw [hkt2] at hc; cases hc + have hc0 : σ.kheap c = some (.schedContext y k) := by + simpa [hσ2, hσ1, updK, hcn, hct] using hc + exact J.sv.scSpor c y k hc0 + obtain ⟨I', B', FF, AI', cur', run', -⟩ := recv_tailDN (r := n.bv) I2 (F1.bsa J1.bsa) + ⟨fun u hu => F1.sched (J1.act.1 u hu), F1.idleOK J1.act.2⟩ (Or.imp id F1.curInv J1.cur) + (Or.imp id F1.curRunW J1.curRun) hkt2 hrn1 hkn2 hnqT (F1.off J1.off) hsp2 hd + (by rw [hu]; exact Set.mem_singleton _) + -- `JAtZ` after, from `J1` + have hw : P1.WritesIn st s1 s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s1 σ1) := + hFS.1.mono fun b h => Or.inl h + have hRR : ∀ q ∈ Z.R, ∀ xr, σ1.kheap q.bv = some (.reply xr) → xr.replyTcb = some t.bv → + Detached Z.C Lst q ∧ L.mem s1 (P1.rNextA q) = 0#u64 ∧ L.mem s1 (P1.rPrevA q) = 0#u64 := by + intro q _ xr hq hxt + exfalso + obtain ⟨x', hk', hs'⟩ := J1.sv.replyTcb q.bv xr t.bv hq hxt + rw [hkt1] at hk'; cases hk' + rcases hs' with e | ⟨ep, dd, e⟩ <;> rw [e] at hrn1 <;> cases hrn1 + have htD := J1.not_dead_of_st ht hkt1 (by rcases runnable_code hrn1 with h | h | h <;> rw [h] <;> decide) + have J' := J1.wake_postD hIR hFr hw hbk (hfoot (J1.donCore hn)) ((DWFr.ofNFr F1.weak).trans FF) I' B' + AI' cur' run' hRR htD ⟨t, ht, rfl⟩ + -- the spec's outcome + have hspecC : Fl4Spec.completeSignal (Z := Unit) n.bv t.bv σ = specCompleteTail t.bv n.bv σ2 := by + unfold Fl4Spec.completeSignal + rw [nd_bind_one (getNotification_eq hk)] + simp only [hact] + rw [nd_bind_one (asUser_setRegister_eq _ _ hkt), nd_bind_one (setNotification_eq _ hk1)] + rfl + obtain ⟨xd, hkd⟩ := donate_tcb I2 hkt2 hkn2 hnqT hsc1 hd + have hsT : specCompleteTail t.bv n.bv σ2 = specUnblock t.bv σd := by + have e1 : specCompleteTail t.bv n.bv σ2 = specCompleteCheck t.bv n.bv σd := by + unfold specCompleteTail; rw [nd_bind_one hd]; rfl + rw [e1, specCompleteCheck_eqU (donShape_of I2 hkt2 hkn2 hnqT hsc1 hd) hkd] + rw [receiveIpc_active b rights blk (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hke hk hact, hspecC, hsT, hu] + have hK : NatK σ1 σ' := by + have m1 : ((), σd) ∈ (Fl4Spec.maybeDonateSc (Z := Unit) t.bv n.bv σ2).1 := by rw [hd]; exact Set.mem_singleton _ + have m2 : ((), σ') ∈ (specUnblock t.bv σd).1 := by rw [hu]; exact Set.mem_singleton _ + exact (natK_ntfnUpd hk1).trans (natK_tk ((nkt_maybeDonateSc t.bv n.bv σ2 _ m1).trans (nkt_specUnblock t.bv σd _ m2))) + exact ⟨_, Set.mem_singleton _, J', + J.natTrans FR1 (NatFoot.ofW J1.xtop hFS.1 (fun _ hb => Ipc.FinW.oobj J1.xtop.fin.1.1.1 hb) + (fun _ hb => Ipc.FinW.nv J1.xtop.fin.1.1.1 hb) (fun _ hb => J1.finW_nh hb) hK)⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidDonV.lean b/proofs/Fl4Proofs/P3/VmRecvMidDonV.lean new file mode 100644 index 000000000..a0bd4b328 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidDonV.lean @@ -0,0 +1,121 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 1): **the SC-donating active-notification cells at `VmEptState`** and the +notification arm of the receive over all of them. `VMidF.inv_or_N` (a resumed or choose mid state has `CurInv`, or is +a choose state with an SC-less current thread: the native `MidN`); `VmEptState.recvSigDonMVE` / `recvIpcActDMVE` +(P3/VmRecvMidDon's `JAtZB.recvSigDonMB` / `recvIpcActDMB` through `VmEptState.ofNat`, donating or not); +`VmEptState.recvNtfnMVE`: the notification arm at a resumed or choose mid state (SC return iff `ntfnSc = tcbSc ≠ none`), +no `CurInv` hypothesis any more. No named premise beyond `P0.ArchTotal` and the platform's `MIN_BUDGET` (`hmb`, [P]). +-/ +import Fl4Proofs.P3.VmRecvMidDon +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- A resumed or choose mid state has `CurInv`, or is a choose state with an SC-less current thread (native `MidN`). -/ +theorem VMidF.inv_or_N {σ : Fl4Spec.State Unit} (hF : VMidF σ) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) : + CurInv σ ∨ (σ.schedulerAction = .chooseNewThread ∧ + ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) := by + rcases hsa with h | h + · exact Or.inl (J.curInv (by rw [h]; intro e; cases e)) + · rcases hF.cho h with hC | h0 + · exact Or.inl hC + · exact Or.inr ⟨h, h0⟩ + +/-- **The active-notification receive donating to an SC-less current thread at a choose mid state** (`VmEptState`). -/ +theorem VmEptState.recvSigDonMVE (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) (hch : σ.schedulerAction = .chooseNewThread) + (hN : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blocking : Bool) (cb : Fl4Spec.Badge) (cr : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n blocking ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb cr) blocking σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.1.1.recvSigDonMB hAT hmb ht hcur hF hch hN hn hk hact blocking cb cr) + fun _ ⟨z, hz, B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +/-- **The endpoint receive completing an active bound notification, donating to an SC-less current thread at a choose +mid state** (`VmEptState`). -/ +theorem VmEptState.recvIpcActDMVE (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) (hch : σ.schedulerAction = .chooseNewThread) + (hN : ∀ x, σ.kheap σ.curThread = some (.tcb x) → x.tcbSchedContext = none) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hbn : xt.tcbBoundNotification = some n.bv) {b0 : Fl4Spec.Badge} (hact : x.ntfnObj = .activeNtfn b0) + (blk : Bool) {e : U64} {xe : Fl4Spec.Endpoint} (hke : σ.kheap e.bv = some (.endpoint xe)) + (cb : U64) (c1 c2 c3 c4 : Bool) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) blk rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) blk rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.1.1.recvIpcActDMB hAT hmb ht hcur hF hch hN hkt hn hk hbn hact blk hke cb c1 c2 c3 c4 hrc b rights) + fun _ ⟨z, hz, B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +set_option maxHeartbeats 8000000 in +/-- **The notification arm of the receive at a resumed or choose mid state.** The SC return is taken exactly when the +notification's SC is the receiver's own (`ntfnSc = tcbSc ≠ none`); an active notification is received keeping the +current SC (`CurInv`) or, for an SC-less current thread at choose, by the donating cell. -/ +theorem VmEptState.recvNtfnMVE (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (blocking : Bool) + (hcov : blocking = true → (∀ b, x.ntfnObj ≠ .activeNtfn b) → + (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n blocking ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) blocking σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + by_cases hs : ∃ badge, x.ntfnObj = .activeNtfn badge + · obtain ⟨badge, hactive⟩ := hs + rcases hF.inv_or_N B hsa with hC | ⟨hch, hN⟩ + · exact (J.recvActiveMidVE hAT ht hcur hC hF.ni hF.run hF.nsw hnq hn hk hactive blocking cb rt).1 + · exact J.recvSigDonMVE hAT hmb ht hcur hF hch hN hn hk hactive blocking cb rt + have hna : ∀ badge, x.ntfnObj ≠ .activeNtfn badge := fun badge h => hs ⟨badge, h⟩ + cases blocking + · exact J.recvSigPollVE ht hn hk hna cb rt + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + by_cases hret : x.ntfnSc = xt.tcbSchedContext ∧ x.ntfnSc ≠ none + · have hC : CurInv σ := by + rcases hF.inv_or_noSc B hsa hk' with hC | h0 + · exact hC + · exact absurd (hret.1.trans h0) hret.2 + have hsc : x.ntfnSc = some σ.curSc := by + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hk'] at hkc + cases hkc + exact hret.1.trans hscc + exact J.recvSigReturnMVE ht hcur hF hC hsa hnq hn hk hna hsc (hcov rfl hna) cb rt + · refine J.recvSigBlockMVE hAT ht hcur hF hsa hnq hn hk hna (fun xt' hkt' => ?_) (hcov rfl hna) cb rt + rw [hkt] at hkt' + cases hkt' + exact hret + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidEp.lean b/proofs/Fl4Proofs/P3/VmRecvMidEp.lean new file mode 100644 index 000000000..1daee44dc --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidEp.lean @@ -0,0 +1,359 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 2, generated by `handoff/tools/gen_midep.py`): **the endpoint-block leaves at a resumed or choose mid state at +`JAtZB`**: `JAtZB.recvIpcWaitMB`, `JAtZB.recvIpcBlockBoundMB`, `JAtZB.recvIpcRetMB` are `recvIpcWaitB` / `recvIpcBlockBoundB` / `recvIpcRetB` of +VmEntryS3RecvEndpoint with the entry facts (`hact`, `hscA`, `hcb`; for the return leaf `CurInv`) as hypotheses. Proofs verbatim except the +resumed-only steps. No named premise. +-/ +import Fl4Proofs.P3.VmRecvMidSig +import Fl4Proofs.P3.VmEntryS3RecvEndpoint +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcWaitMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hnrl : t.bv ∉ σ.releaseQueue) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : rv ≠ 0#u64 → (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + (hsp : Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ = + Fl4Spec.receiveIpcBlocked true t.bv e.bv (optRef rv) σ) : + (do + let s3 ← endpoint.wait_for_sender st s t e true rv + ok (s3, a)) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have htq : t.bv ∉ x.epQueue := Endp.not_queued hI.1.toE ht hkt hnb3 e he x hk + have hq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, hcn, hsrt, hcc⟩ := + recvIpcBlock_eq hkt hk hc htq hq hr (J.isorted.1 _ _ hk) b rights (replyArg_spec J.ip.e hI.1.toE hrc) hsp + -- the reply object (if any): in `R`, linked to no thread + have hrv : rv ≠ 0#u64 → rv ∈ Z.R ∧ L.mem s (Endp.rTcbA rv) = 0#u64 := fun h0 => replyArg_ne J.ip.e hrc h0 + have hcn' : σ'.schedulerAction = .chooseNewThread := by + rcases hact with h | h + · exact hcn hcur.symm h + · exact hcc h + -- P1's `BlkOK` after and footprint + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hHe : H e = some .ep := J.zH.2.1 e he + have hx := hI.1.toE.at_ep he hk + have hst2 : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => simp [Endp.CanBlock] at hc + have hst0 : L.epState s e = 0#u64 ∨ L.epState s e = 2#u64 := by rw [epState_eq]; exact hst2 + have hrH : rv ≠ 0#u64 → rv ∈ P1.Heap.replies H := fun h => J.zH.2.2.1 rv (hrv h).1 + have p1b := spec_lift_ok (P1.endpoint.wait_for_sender.inv s t e true rv Q0 R0 h0 hB0 hX0 hS0 hSg0 + (J.zH.1 t ht) (fun _ => hcb) hHe hst0 (fun h => ⟨hrH h, Or.inl (hrv h).2⟩)) a + have p1f := spec_lift_ok (P1.endpoint.wait_for_sender.footR (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) s t e rv h0 hcb hHe + (fun h => ⟨hSg0.sub _ (hX0.1.tcbW (hrH h)), hX0.1.tcbW (hrH h)⟩)) a + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (WP.spec_and (wait_for_sender_blockF J.ip s a σ hI ht hkt hnb3 hnn he hk hc + (hsort e (Or.inl he)) b rights hrc hsp) p1b) p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW⟩, hbk⟩, hfoot⟩ + simp only at ea hbk hfoot hIR hW + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB_free hy hyt, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := ⟨fun u hu => (by rw [hcn'] at hu; cases hu), F.idleOK J.act.2⟩ + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcn' + have hrun3 : CurRunM σ' := Or.inl (by rw [hcn']; intro h; cases h) + -- the dead: the thread is not dead (runnable); the endpoint and the reply leave the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + set D' : Set U64 := if rv = 0#u64 then D \ {e} else (D \ {e}) \ {rv} with hD' + have hD'D : ∀ q ∈ D', q ∈ D \ {e} ∧ Notif.optRef rv ≠ some q.bv := by + intro q hq + by_cases h0 : rv = 0#u64 + · rw [hD', if_pos h0] at hq + exact ⟨hq, by rw [h0, optRef_zero]; intro h; cases h⟩ + · rw [hD', if_neg h0] at hq + refine ⟨hq.1, fun h => hq.2 ?_⟩ + rw [Notif.optRef_ne h0] at h + exact (P1.eq_of_bv (Option.some.inj h)).symm + have hdead : ∀ q ∈ D', DeadP (L := L) H s' q := fun q hq' => by + obtain ⟨hq, hqr⟩ := hD'D q hq' + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqe : q.bv ≠ e.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF' hx hqr, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 + (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D') + -- no reply names the runnable thread (`SchedValid.replyTcb`) + have hnt : ∀ d ∈ Z.R, ∀ xr, σ.kheap d.bv = some (.reply xr) → xr.replyTcb ≠ some t.bv := by + intro d _ xr hkr hrt + obtain ⟨x', hk', hs'⟩ := J.sv.replyTcb _ xr _ hkr hrt + rw [hkt] at hk'; cases hk' + rcases hs' with hs' | ⟨ep, dd, hs'⟩ <;> rw [hs'] at hr <;> cases hr + -- the reply (if any) was free, so it has null links + have hrq : ∀ d ∈ Z.R, Notif.optRef rv = some d.bv → L.mem s' (rNextA d) = 0#u64 ∧ L.mem s' (rPrevA d) = 0#u64 := by + intro d hd hdr + have h0 : rv ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hdr; cases hdr + rw [Notif.optRef_ne h0] at hdr + obtain rfl : rv = d := P1.eq_of_bv (Option.some.inj hdr) + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply rv hd + obtain ⟨-, h2, h3⟩ := J.rfree rv hd ⟨xr, hkr, by rw [hxr, (hrv h0).2, optRef_zero]⟩ + obtain ⟨e1, e2⟩ := k4 rv hd + exact ⟨by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hRR' := J.rrecv.frameR k4 F hnt hrq + have hjcov : JCovD (L := L) Z.th H Z.G D' Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hcv : JCovD (L := L) Z.th H Z.G D' Cs s := by + by_cases h0 : rv = 0#u64 + · rw [hD', if_pos h0]; exact J.jcov.undead hcov.1 hcov.2 + · rw [hD', if_neg h0]; exact (J.jcov.undead hcov.1 hcov.2).undead (hcovR h0).1 (hcovR h0).2 + exact hcv.same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D' s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have B' : JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' s' a σ' := J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (fun p hp => by + have h1 := J.zD_diffE he p hp + have hT : H p = some .tcb := J.zH.1 p (J.thT p (J.ztcb_tcb hp)) + rw [hD'] + split_ifs with h0 + · exact h1 + · exact ⟨h1, fun e' => by rw [Set.mem_singleton_iff.1 e', J.zH.2.2.1 rv (hrv h0).1] at hT; cases hT⟩) + rw [heq] + have hK : NatK σ σ' := natK_kh (R := BChgR t.bv e.bv (optRef rv)) (by + intro p o o' h + rcases h with ⟨xt, e, d, rfl, rfl, -, -⟩ | ⟨x, y, rfl, rfl, -⟩ | ⟨x, rfl, rfl, -⟩ + · exact zvChg_tcb fun h => by cases h + · exact zvChg_ep + · exact zvChg_reply) F.kh + exact ⟨_, Set.mem_singleton _, D', B', NatFoot.ofWS hF.1 hW + (fun _ h => Or.inl (Ipc.CnW.oobj hS h)) (fun _ h => J.cnW_nv h) (fun _ h => J.cnW_nh h) hK⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcBlockBoundMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hnrl : t.bv ∉ σ.releaseQueue) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) (hnr : ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none)) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : rv ≠ 0#u64 → (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (cb : U64) (c1 c2 c3 c4 : Bool) (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) true rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hns : ∀ q, x ≠ .sendEP q := fun q h => by subst h; simp [Endp.CanBlock] at hc + have hx := hI.1.toE.at_ep he hk + have hst2 : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hwn : L.mem s (nScA n) = 0#u64 ∨ L.mem s (nScA n) ≠ L.mem s (P1.scA t) := by + have hsN : xn.ntfnSc = optRef (L.mem s (nScA n)) := hxn.sc + obtain ⟨Q0, R0, -, hs0⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨xt0, hkt0, -, -, hsct, -⟩ := hs0.tcb t ht + rw [hkt] at hkt0; cases hkt0 + by_cases h0 : L.mem s (nScA n) = 0#u64 + · exact Or.inl h0 + · refine Or.inr fun he' => hnr ⟨?_, ?_⟩ + · rw [hsN, he']; exact hsct.symm + · rw [hsN, Notif.optRef_ne h0]; simp + exact receive_ipc_blockN_red hrc hbound (by rw [← hbound]; exact hbz) hstN hwn hst2 + (J.recvIpcWaitMB ht hcur hti hnq hnrl hkt hr hact hscA hcb he hk hc hcov hrc hcovR b rights + (receiveIpc_boundNA b rights true (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hk hns hkn hna (fun _ => hnr))) + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcRetMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hC : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hr : Fl4Spec.runnable xt.tcbState = true) (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) (hlend : xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) {cb : U64} {sd g gr : Bool} + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) {rc : capability.Cap} + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb sd true g gr) true rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ Ac' Lst' D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac' Lst' D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hnrl : t.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hns : ∀ q, x ≠ .sendEP q := fun q h => by subst h; simp [Endp.CanBlock] at hc + -- the SC `i` lent by the notification, the thread's + obtain ⟨hl1, hl2⟩ := hlend + obtain ⟨i0, hi0⟩ := Option.ne_none_iff_exists'.1 hl2 + have hxs0 : xt.tcbSchedContext = some i0 := hl1.symm.trans hi0 + obtain ⟨y, m, hky, -⟩ := J.inv.back t.bv xt i0 hkt hxs0 + obtain ⟨i, hiC, rfl⟩ := J.obj.sc _ y m hky + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨σ2, he2, kt, -, ko, ct, -, -, -, hidl, -, rq, a1, -, hrq2⟩ := + maybeReturn_shape J.inv hkn hkt hky hxs0 hi0 hnqT + -- FL4's bound-notification words + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hsp1 := receiveIpc_boundR b rights (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hk hns hkn hna he2 + refine receive_ipc_blockR_red hrc hbound (by rw [← hbound]; exact hbz) hstN ?_ + refine WP.spec_mono (J.maybeReturnBQ ht hn hiC hkt hkn hxs0 hi0 hnqT ⟨hcb.nq, hcb.nr, hcb.na⟩ + (fun u hu => by rcases hact with h | h <;> rw [h] at hu <;> cases hu) (by rw [← hcur]; exact hti)) ?_ + rintro s' ⟨hW, σ', he', Ac', Lst', J2, F2⟩ + rw [the_result he2 he'] at J2 F2 + have hI2 := J2.xtop.irelK J2.ks J2.buf + -- the objects kept by `maybe_return_sc` + have hte : e.bv ≠ t.bv := fun h => by rw [h, hkt] at hk; cases hk + have hie : e.bv ≠ i.bv := fun h => by rw [h, hky] at hk; cases hk + have hk2 : σ2.kheap e.bv = some (.endpoint x) := by rw [ko _ hte hie]; exact hk + have hnt : n.bv ≠ t.bv := fun h => by rw [h, hkt] at hkn; cases hkn + have hni' : n.bv ≠ i.bv := fun h => by rw [h, hky] at hkn; cases hkn + have hkn2 : σ2.kheap n.bv = some (.notification xn) := by rw [ko _ hnt hni']; exact hkn + have hcur2 : σ2.curThread = t.bv := ct.trans hcur + have hti2 : σ2.curThread ≠ σ2.idleThread := by rw [ct, hidl]; exact hti + have hnq2 : ∀ d p, σ2.curThread ∉ σ2.readyQueues d p := by rw [hcur2]; exact hrq2 + -- the reply cap at the state after + have hrc2 : ReplyArg Z.R (L.mem s') rc rc' rv := by + rcases hrc with h | ⟨bb, r', h1, h2, hrR, h0⟩ + · exact Or.inl h + refine Or.inr ⟨bb, r', h1, h2, hrR, ?_⟩ + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply rv hrR + obtain ⟨xr2, hkr2, hxr2⟩ := hI2.1.toE.reply rv hrR + have hrt : rv.bv ≠ t.bv := fun h => by rw [h, hkt] at hkr; cases hkr + have hri : rv.bv ≠ i.bv := fun h => by rw [h, hky] at hkr; cases hkr + rw [ko _ hrt hri, hkr] at hkr2; cases hkr2 + rw [h0, optRef_zero] at hxr + exact optRef_none (hxr2.symm.trans hxr) + obtain ⟨hcov, hcovR⟩ := J.recvCov_ofW hW he hHd hHr hrc + have hx2 := hI2.1.toE.at_ep he hk2 + have hst2 : stOf (L.mem s') e = 0#u64 ∨ stOf (L.mem s') e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx2.1 + | recvEP q => exact Or.inr hx2.1 + | sendEP q => exact absurd rfl (hns q) + have hsp2 := receiveIpc_boundNA b rights true (replyArg_spec J2.ip.e hI2.1.toE hrc2) kt hbn hk2 hns hkn2 hna + (fun _ h => h.2 h.1) + have key := recvTail_wait (ar := arI) hst2 + (J2.recvIpcWaitB ht hcur2 hti2 hnq2 (fun h => hnrl (rq _ h)) kt hr (Or.inr ⟨a1 hcur.symm, rfl⟩) + he hk2 hc hcov hrc2 hcovR b rights hsp2) + rw [hsp1, ← hsp2] + exact WP.spec_mono key fun _ ⟨z, hz, D', B', F'⟩ => + ⟨z, hz, Ac', Lst', D', B', J.natTrans F2 F'⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidEp2.lean b/proofs/Fl4Proofs/P3/VmRecvMidEp2.lean new file mode 100644 index 000000000..aca65cda7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidEp2.lean @@ -0,0 +1,295 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 2, generated by `handoff/tools/gen_midep2.py`): `JAtZB.midBlkFacts` (the entry facts of a blocking leaf at a `VMidF` +state with the action resumed or choose: not in the release queue, every SC of the thread active, `CanBlock`) and `VmEptState.recvIpcWaitCaseMVE`, the +endpoint-block case of `handle_recv` at such a state (VmEntryS3RecvEndpoint's `recvIpcWaitCaseVE` over `recvIpcWaitMB` / `recvIpcBlockBoundMB` / `recvIpcRetMB`). No named premise. +-/ +import Fl4Proofs.P3.VmRecvMidEp +import Fl4Proofs.P3.VmRecvMidNtfn +import Fl4Proofs.P3.XTopJointRecvIpcLinkC +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- `VMidF` is kept by the spec's `cancel_ipc` of a linked thread other than the current one. -/ +theorem VMidF.cancel {σ σ' : Fl4Spec.State Unit} (hF : VMidF σ) (hsv : Sched.SchedValid σ) {i : Fl4Spec.ObjRef} + {x : Fl4Spec.Tcb} (hk : σ.kheap i = some (.tcb x)) {q : Fl4Spec.ObjRef} + (hst : x.tcbState = .blockedOnReply q ∨ ∃ e d, x.tcbState = .blockedOnReceive e (some q) d) + (hne : σ.curThread ≠ i) {xc : Fl4Spec.Tcb} (hkc : σ.kheap σ.curThread = some (.tcb xc)) + (hm : ((), σ') ∈ (Fl4Spec.cancelIpc (Z := Unit) i σ).1) : VMidF σ' := by + obtain ⟨hct, hid, hact, hrq, hkeep⟩ := cancelOther_keep hm hne + have hxc : σ'.kheap σ'.curThread = some (.tcb xc) := by + rw [hct]; exact hkeep σ.curThread xc (Ne.symm hne |>.symm |> fun h => h) hkc + refine ⟨by rw [hct, hid]; exact hF.ni, by rw [hct, hrq]; exact hF.nq, ?_, ?_, ?_, ?_⟩ + · intro x' hx'; rw [hxc] at hx'; cases hx'; exact hF.vfh xc hkc + · intro x' hx'; rw [hxc] at hx'; cases hx'; exact hF.run xc hkc + · rw [hact, hct]; exact hF.nsw + · intro hc + rw [hact] at hc + rcases hF.cho hc with hC | h0 + · exact Or.inl (cancelLink_curInv hsv hk hst hne hm hC) + · exact Or.inr (fun x' hx' => by rw [hxc] at hx'; cases hx'; exact h0 xc hkc) + +set_option maxHeartbeats 8000000 in +/-- The entry facts of a blocking leaf at a `VMidF` state (action resumed or pending reschedule). -/ +theorem JAtZB.midBlkFacts (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (hcur : σ.curThread = t.bv) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) : + t.bv ∉ σ.releaseQueue ∧ + (∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) ∧ + P1.CanBlock st (P1.Heap.tcbs H) Ac s t := by + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + have hcb := J.canBlockMidB ht hcur hkt hF + rcases hF.inv_or_noSc J hsa hk' with hC | h0 + · exact ⟨by rw [← hcur]; exact hC.notRel, J.blockScA hcur hC hF.ni hkt, hcb⟩ + · exact ⟨by rw [← hcur]; exact J.inv.notRel_of_noSc hk' h0, J.blockScA_noSc hkt h0, hcb⟩ + +set_option maxHeartbeats 16000000 in +theorem VmEptState.recvIpcWaitCaseMVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHeld : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hReply : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hWait : RecvIpcBlockCase (L := L) ct blocking s σ p rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ p rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he, h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he + have hb : blocking = true := hWait.elim (fun h => h.1) (fun h => h.1) + subst blocking + have hns : ∀ q, xe ≠ .sendEP q := hWait.elim (fun h => h.2.2.1 _ hke) (fun h => h.2.2.1 _ hke) + have hrr : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 := + hWait.elim (fun h => h.2.2.2) (fun h => h.2.2.2) + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hrr + have hcB : Endp.CanBlock true xe := by + cases xe with + | idleEP => trivial + | recvEP q => rfl + | sendEP q => exact absurd rfl (hns q) + obtain ⟨xt0, hkt0, -⟩ := hI.1.tcb ct ht + obtain ⟨hnrl, hscA, hcb⟩ := B.midBlkFacts ht hcur hF hsa hkt0 + obtain ⟨hcE, hcR⟩ := B.recvCov_ofW (P1.WritesIn.refl s _) he hHeld hReply hra + let rights := rightsOf sd true g gr + rcases hWait with ⟨-, hb, -, -⟩ | ⟨-, ⟨xt, np, xn, hkt, hb, hkn, hna⟩, -, -⟩ + · obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + have hrun := hF.run xt (by rw [hcur]; exact hkt) + have hx := hI.1.toE.at_ep he hke + have hst : stOf (L.mem s) p = 0#u64 ∨ stOf (L.mem s) p = 2#u64 := by + cases xe with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hb0 := optRef_none ((hI.1.toE.at_tcb ht hkt).bound.symm.trans (hb xt hkt)) + have hsp := receiveIpc_noSend b.bv rights true (replyArg_spec B.ip.e hI.1.toE hra) hkt (hb xt hkt) hke hns + exact receive_ipc_block_red hra hb0 hst (WP.spec_mono + (B.recvIpcWaitMB ht hcur hF.ni hnq hnrl hkt hrun hsa hscA hcb he hke hcB hcE hra hcR b.bv rights hsp) + fun _ ⟨z, hz, D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩) + · obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + have hrun := hF.run xt (by rw [hcur]; exact hkt) + by_cases hret : xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none + · have hCi : CurInv σ := (hF.inv_or_noSc B hsa (by rw [hcur]; exact hkt)).resolve_right + (fun h0 => hret.2 (hret.1.trans h0)) + exact WP.spec_mono (B.recvIpcRetMB ht hcur hCi hF.ni hsa hnq hkt hrun hcb hn hb hkn hna hret he hke hcB hHeld hReply hra + b.bv rights) fun _ ⟨z, hz, Ac', Lst', D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + · exact WP.spec_mono (B.recvIpcBlockBoundMB ht hcur hF.ni hnq hnrl hkt hrun hsa hscA hcb hn hb hkn hna hret + he hke hcB hcE hra hcR b sd true g gr b.bv rights) + fun _ ⟨z, hz, D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +set_option maxHeartbeats 6000000 in +theorem VmEptState.recvSendingReturnMV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct e cb r n : U64} {sd g gr : Bool} {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (ht : ct ∈ Z.T) (he : e ∈ Z.E) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) (hC : CurInv σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {recv : Fl4Spec.Tcb} (hkt : σ.kheap ct.bv = some (.tcb recv)) + (hn : n ∈ Z.N) {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hbn : recv.tcbBoundNotification = some n.bv) (hna : Fl4Spec.isActive xn = false) + (hlend : xn.ntfnSc = recv.tcbSchedContext ∧ xn.ntfnSc ≠ none) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap e.bv = some (.endpoint (.sendEP queue))) + (hra : ReplyArg Z.R (L.mem s) rc rc' r) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (afterReturn : ∀ s' σ' Ac' Lst', + L.mem s (tBoundA ct) = n → + notification.maybe_return_sched_context st s n ct = ok s' → + Fl4Spec.maybeReturnSc (Z := Unit) n.bv ct.bv σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR (Z.mv Z.G Z.vs) H Cs Ac' Lst' D s' a σ' → + RecvSenderLeaves (L := L) arI AR Z s' a σ' ct e cb sd g gr true rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) true rc ⦃ out => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) true rc' σ).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + have hnrl : ct.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hrun := hF.run recv (by rw [hcur]; exact hkt) + have hnqT : NotQueued σ ct.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨i0, hi0⟩ := Option.ne_none_iff_exists'.1 hlend.2 + have hxs0 : recv.tcbSchedContext = some i0 := hlend.1.symm.trans hi0 + obtain ⟨y, m, hky, -⟩ := B.inv.back ct.bv recv i0 hkt hxs0 + obtain ⟨i, hiC, rfl⟩ := B.obj.sc _ y m hky + obtain ⟨σ2, he2, kt, ki, ko, ct2, csc2, -, -, _, -, hrel, hact2, -, hnq2⟩ := + maybeReturn_shape B.inv hkn hkt hky hxs0 hi0 hnqT + have hxt := hI.1.toE.at_tcb ht hkt + have hb : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hbn + have hb0 : L.mem s (tBoundA ct) ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hb; cases hb + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hb0] at hb + exact P1.eq_of_bv (Option.some.inj hb) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hcb := B.canBlockMidB ht hcur hkt hF + refine receive_ipc_blockR_red hra hbound (by rw [← hbound]; exact hb0) hstN ?_ + refine WP.spec_mono (Vs.spec_eq_mp (B.maybeReturnBQ ht hn hiC hkt hkn hxs0 hi0 hnqT + ⟨hcb.nq, hcb.nr, hcb.na⟩ (fun u hu => by rcases hsa with h | h <;> rw [h] at hu <;> cases hu) + (by rw [← hcur]; exact hF.ni))) ?_ + rintro s' ⟨hconc, -, σ', he', Ac', Lst', B2, F2⟩ + rw [the_result he2 he'] at B2 F2 + have hI2 := B2.xtop.irelK B2.ks B2.buf + have het : e.bv ≠ ct.bv := fun h => by rw [h, hkt] at hk; cases hk + have hei : e.bv ≠ i.bv := fun h => by rw [h, hky] at hk; cases hk + have hk2 : σ2.kheap e.bv = some (.endpoint (.sendEP queue)) := by rw [ko _ het hei]; exact hk + have hnt : n.bv ≠ ct.bv := fun h => by rw [h, hkt] at hkn; cases hkn + have hnic : n.bv ≠ i.bv := fun h => by rw [h, hky] at hkn; cases hkn + have hkn2 : σ2.kheap n.bv = some (.notification xn) := by rw [ko _ hnt hnic]; exact hkn + have hra2 : ReplyArg Z.R (L.mem s') rc rc' r := by + rcases hra with h | ⟨bb, r', h1, h2, hrR, h0⟩ + · exact Or.inl h + refine Or.inr ⟨bb, r', h1, h2, hrR, ?_⟩ + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply r hrR + obtain ⟨xr2, hkr2, hxr2⟩ := hI2.1.toE.reply r hrR + have hrt : r.bv ≠ ct.bv := fun h => by rw [h, hkt] at hkr; cases hkr + have hri : r.bv ≠ i.bv := fun h => by rw [h, hky] at hkr; cases hkr + rw [ko _ hrt hri, hkr] at hkr2 + cases hkr2 + rw [h0, optRef_zero] at hxr + exact optRef_none (hxr2.symm.trans hxr) + have hnr2 : ¬ (xn.ntfnSc = (Fl4Spec.tcbSchedContextUpdate (fun _ => none) recv).tcbSchedContext ∧ + xn.ntfnSc ≠ none) := fun h => h.2 h.1 + have hno : RecvNoReturn ct true σ2 := by + intro xt hkt' + rw [kt] at hkt' + cases hkt' + exact Or.inr ⟨n.bv, xn, hbn, hkn2, hna, fun _ => hnr2⟩ + have hsp := receiveIpc_lend_eq cb.bv (rightsOf sd true g gr) (replyArg_spec B.ip.e hI.1.toE hra) + (replyArg_spec B2.ip.e hI2.1.toE hra2) hkt kt hbn hbn hk hk2 hkn hkn2 hna he2 hnr2 + rw [show absCap (.Endpoint e cb sd true g gr) = .endpointCap e.bv cb.bv (rightsOf sd true g gr) from rfl, hsp] + obtain ⟨head, sender, pl, tl, hh, l, hhead, hks, hss⟩ := B2.recvSenderHead he hk2 + rw [l] at hk2 + have hLeaf := (afterReturn s' σ2 Ac' Lst' hbound hconc he2 (J.ofNat (V' := Z.vs) B2 F2)).run + ht he hh (ct2.trans hcur) kt hrun (Or.inr (Or.inl (hact2 hcur.symm))) hnq2 (fun h => hnrl (hrel _ h)) + hhead hk2 hks hss hno hra2 (jHeld_of_same F2.ss hHd) (fun q g' h => jHeld_of_same F2.ss (hHr q g' h)) + unfold Endp.recvTail + exact recvAtSendHead true (hI2.1.toE.at_ep he hk2).1 hLeaf + +set_option maxHeartbeats 4000000 in +theorem VmEptState.recvIpcLinkedMVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (p b : U64) (sd g gr : Bool) (q : U64) (g' : Bool) + (rc' : Fl4Spec.Cap) (hcr : CapRel (.Reply q g') rc') + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + {xr : Fl4Spec.Reply} (hkr : σ.kheap q.bv = some (.reply xr)) + (hHd : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hHr : JHeld (L := L) Z.th s (.Reply q g')) (hi0 : L.mem s (rTcbA q) ≠ 0#u64) + (afterFree : ∀ i V' Ac' Lst' s' a' σ', + L.mem s (rTcbA q) = i → i ≠ 0#u64 → i ≠ ct → + ipc.cancel_ipc st arI s a i = ok (s', a') → + Fl4Spec.cancelIpc (Z := Unit) i.bv σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D s' a' σ' → + σ'.curThread = ct.bv → VMidF σ' → + (σ'.schedulerAction = .resumeCurThread ∨ σ'.schedulerAction = .chooseNewThread) → + (CurInv σ → CurInv σ') → + (∀ d p, σ'.curThread ∉ σ'.readyQueues d p) → + (∃ xe, σ'.kheap p.bv = some (.endpoint xe)) → + (∃ xr', σ'.kheap q.bv = some (.reply xr') ∧ xr'.replyTcb = none) → + L.mem s' (rTcbA q) = 0#u64 → + JHeld (L := L) Z.th s' (.Endpoint p b sd true g gr) → + JHeld (L := L) Z.th s' (.Reply q g') → + endpoint.receive_ipc st arI s' a' ct (.Endpoint p b sd true g gr) blocking (.Reply q g') ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ').1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking (.Reply q g') ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hR := (B.xtop.irelK B.ks B.buf).1.toE + obtain ⟨xc, hkc, -⟩ := (B.xtop.irelK B.ks B.buf).1.tcb ct ht + have hkc' : σ.kheap σ.curThread = some (.tcb xc) := by rw [hcur]; exact hkc + obtain ⟨q0, hq, hqe⟩ := B.obj.reply _ xr hkr + rw [Sched.bv_inj hqe] at hq + have hrt : xr.replyTcb = some (L.mem s (rTcbA q)).bv := (hR.at_reply hq hkr).trans (Notif.optRef_ne hi0) + obtain ⟨xi, hki, hsti⟩ := B.sv.replyTcb q.bv xr _ hkr hrt + obtain ⟨i, hiT, hie⟩ := B.obj.tcb _ xi hki + have hiq : L.mem s (rTcbA q) = i := (P1.eq_of_bv hie).symm + rw [hiq] at hrt hki hi0 + have hict : i ≠ ct := by + intro e + subst e + have hr := hF.run xi (by rw [hcur]; exact hki) + rcases hsti with h | ⟨_, _, h⟩ <;> rw [h] at hr <;> simp [Fl4Spec.runnable] at hr + have hne : σ.curThread ≠ i.bv := fun e => hict (P1.eq_of_bv (e.symm.trans hcur)) + refine receive_ipc_linked hiq hi0 hict ?_ + refine WP.spec_mono (Vs.spec_eq_mp (B.cancelLinkForRecv hiT hki hsti)) ?_ + rintro ⟨s1, a1⟩ ⟨hconc, ha, σ', he, hin, V', Ac', Lst', B', F⟩ + dsimp only at ha B' ⊢ + subst a1 + have hm : ((), σ') ∈ (Fl4Spec.cancelIpc (Z := Unit) i.bv σ).1 := by + rw [he] + exact Set.mem_singleton _ + obtain ⟨hct', hid', hact', hrq', -⟩ := cancelOther_keep hm hne + have hR' := (B'.xtop.irelK B'.ks B'.buf).1.toE + obtain ⟨xr', hkr', hrl⟩ := hR'.reply q hq + have hnone : xr'.replyTcb = none := cancelOther_reply B.sv B'.sv hm hkr hrt hin hkr' + have h0' : L.mem s1 (rTcbA q) = 0#u64 := optRef_none (hrl.symm.trans hnone) + refine ⟨h0', ?_⟩ + have hcr' : rc' = absCap (.Reply q g') := hcr.abs + subst rc' + have hsp : Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking + (absCap (.Reply q g')) σ = + Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking + (absCap (.Reply q g')) σ' := + receiveIpc_linked _ _ _ _ hkr hrt (fun e => hne (hcur.trans e.symm)) he hkr' hnone + rw [hsp] + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he', h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he' + obtain ⟨xe', hke', -⟩ := hR'.ep p he' + exact afterFree i V' Ac' Lst' s1 a σ' hiq hi0 hict hconc he (J.ofNat (V' := V') B' F) + (hct'.trans hcur) (VMidF.cancel hF B.sv hki hsti hne hkc' hm) (by rw [hact']; exact hsa) + (fun hC => cancelLink_curInv B.sv hki hsti hne hm hC) + (by rw [hct', hrq']; exact hnq) ⟨xe', hke'⟩ ⟨xr', hkr', hnone⟩ h0' + (jHeld_of_same F.ss hHd) (jHeld_of_same F.ss hHr) + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidNtfn.lean b/proofs/Fl4Proofs/P3/VmRecvMidNtfn.lean new file mode 100644 index 000000000..9934a2e14 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidNtfn.lean @@ -0,0 +1,111 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 2): **the blocking notification receive at a `VMidF` mid state, VEAnyX cells** at +`VmEptState`: `recvSigBlockMVE` (block without return; resumed, choose with `CurInv`, choose with an SC-less thread) and +`recvSigReturnMVE` (block with the bound SC returned; resumed or choose with `CurInv`). Both over `JAtZB.recvSigBlockMB` / +`recvSigBlockPreMB` (P3/VmRecvMidSig) with the entry facts from `VMidF` (`canBlockMidB`). The pending-switch cells need the R layer. +`recvNtfnMVE` (dispatching the notification arm) is in P3/VmRecvMidDonV. No named premise. +-/ +import Fl4Proofs.P3.VmRecvMidSig +import Fl4Proofs.P3.VmEntryS3Recv +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The current thread of a `VMidF` state at resume or choose either has `CurInv` or no SC. -/ +theorem VMidF.inv_or_noSc {σ : Fl4Spec.State Unit} (hF : VMidF σ) + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + {xt : Fl4Spec.Tcb} (hk' : σ.kheap σ.curThread = some (.tcb xt)) : + CurInv σ ∨ xt.tcbSchedContext = none := by + rcases hsa with h | h + · exact Or.inl (J.curInv (by rw [h]; intro e; cases e)) + · rcases hF.cho h with hC | h0 + · exact Or.inl hC + · exact Or.inr (h0 xt hk') + +set_option maxHeartbeats 8000000 in +/-- **The blocking notification receive without an SC return** at a resumed or choose mid state. -/ +theorem VmEptState.recvSigBlockMVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hnr : ∀ xt, σ.kheap t.bv = some (.tcb xt) → ¬ (x.ntfnSc = xt.tcbSchedContext ∧ x.ntfnSc ≠ none)) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + have hr := hF.run xt hk' + have hcb := B.canBlockMidB ht hcur hkt hF + obtain ⟨hnrel, hscA⟩ : t.bv ∉ σ.releaseQueue ∧ + (∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) := by + rcases hF.inv_or_noSc B hsa hk' with hC | h0 + · exact ⟨by rw [← hcur]; exact hC.notRel, B.blockScA hcur hC hF.ni hkt⟩ + · exact ⟨by rw [← hcur]; exact B.inv.notRel_of_noSc hk' h0, B.blockScA_noSc hkt h0⟩ + exact WP.spec_mono (B.recvSigBlockMB hAT ht hcur hkt hr hnrel hsa hscA hcb hnq hn hk hna (hnr xt hkt) hcov cb rt) + fun _ ⟨z, hz, B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +set_option maxHeartbeats 8000000 in +/-- **The blocking notification receive with the bound SC returned** at a resumed or choose mid state with `CurInv`. -/ +theorem VmEptState.recvSigReturnMVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) (hC : CurInv σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hsc : x.ntfnSc = some σ.curSc) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + have hr := hF.run xt hk' + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hcur, hkt] at hkc + cases hkc + obtain ⟨i, hiC, hib⟩ := B.nsc n hn x σ.curSc hk hsc + have hx := hI.1.toN.at_ntfn hn hk + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + have h := hx.obj + cases he : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := Z.T) (he ▸ h) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (he ▸ h) rfl) + | activeNtfn b => exact absurd he (hna b) + have hcb := B.canBlockMidB ht hcur hkt hF + refine receive_signal_split hst + (B.recvSigBlockPreMB hkt ht hcur hr hF.ni (by rw [← hcur]; exact hC.notRel) hsa + (B.blockScA hcur hC hF.ni hkt) hcb hnq hn hk hna hcov cb rt) ?_ + rintro s4 ⟨σ3, hspec, hnrw, hkt3, ⟨q, hkn3⟩, hact3, -, hid3, B3, F3⟩ + refine WP.spec_mono (B3.maybeReturnB ht hn hiC hkt3 hkn3 (by rw [hib]; exact hscc) (by rw [hib]; exact hsc) + (by rfl) hnrw (fun u hu => by rw [hact3] at hu; cases hu) (by rw [hid3, ← hcur]; exact hF.ni)) ?_ + rintro s5 ⟨σ5, he5, Ac', Lst', B5, F5⟩ + exact ⟨((), σ5), by rw [hspec, he5]; exact Set.mem_singleton _, + (J.ofNat (V' := Z.vs) B5 (B.natTrans F3 ⟨F5.ss, F5.fw, F5.fk⟩)).anyX arI⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidSig.lean b/proofs/Fl4Proofs/P3/VmRecvMidSig.lean new file mode 100644 index 000000000..e2aa2bc29 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidSig.lean @@ -0,0 +1,350 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 2, generated by `handoff/tools/gen_midsig.py`): **the blocking notification receive at a +resumed or `chooseNewThread` mid state at `JAtZB`**: `JAtZB.recvSigBlockMB` is XTopJointRecvSigBlock's `JAtZB.recvSigBlockB` with the +entry facts (`CanBlock`, the thread's state, no release queue, the current-SC activity, the SC-return test) as hypotheses; its proof is +verbatim except the resumed-only steps (the post's `chooseNewThread` also from a pending reschedule, `recvSigBlock_act`). +`JAtZB.blockScA`, `JAtZB.blockScA_noSc` give the activity from `CurInv` or an SC-less thread. No named premise. +-/ +import Fl4Proofs.P3.VmRecvMid +import Fl4Proofs.P3.XTopJointRecvSigBlock +import Fl4Proofs.P3.XTopJointRecvSigC +import Fl4Proofs.P3.XTopJointRecvSigN +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- **The blocking receive at `J`** (module docstring): the current thread +`t`, resumed and in no ready queue, blocks receiving on the idle or waiting +notification `n`, covered by a cap (`hcov`), whose SC is not the current SC +(`hnr`; otherwise `maybe_return_sched_context` gives it back). -/ +theorem JAtZB.recvSigBlockMB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hnrel : t.bv ∉ σ.releaseQueue) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hnr' : ¬ (x.ntfnSc = xt.tcbSchedContext ∧ x.ntfnSc ≠ none)) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have hqE : ∀ e ∈ Z.E, ∀ z, σ.kheap e.bv = some (.endpoint z) → t.bv ∉ z.epQueue := + fun e he z hz => Endp.not_queued hI.1.toE ht hkt hnb3 e he z hz + have hnqN : t.bv ∉ Fl4Spec.ntfnQueueOf x := not_member hI.1.toN ht hkt hnn n hn x hk + have hq : ∀ y ∈ Fl4Spec.ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.ntfnQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, hcn, hsrt⟩ := recvSigBlock_eq hkt hk hna hnqN hq hr (J.isorted.2 _ _ hk) hnr' cb rt + have hcn' : σ'.schedulerAction = .chooseNewThread := by + rcases hact with hres | hch + · exact hcn hcur.symm hres + · rw [recvSigBlock_act hkt hk hna hnqN hq hnr' cb rt heq (by rw [hch]; intro e; cases e)]; exact hch + -- P1's `BlkOK` after + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hx := hI.1.toN.at_ntfn hn hk + have hst0 : 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 := Z.T) (h ▸ this) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (h ▸ this) rfl) + | activeNtfn b => exact absurd h (hna b) + have h3 : L.epState s n ≠ 3#u64 := by + rw [epState_eq] + rcases hst0 with h | h + · rw [h, ntfn_consts.1]; decide + · rw [h, ntfn_consts.2.1]; decide + have p1 := P1.notification.receive_signal.ipc hAT s a t n true Q0 R0 h0 hB0 hX0 hS0 hSg0 hcb + (J.zH.2.2.2 n hn) (J.zH.2.2.2 n hn) h3 + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (receive_signal_blockF (ar := arI) J.ip s a σ hI ht hkt hnn hqE hn hk hna + (hsort n (Or.inr hn)) hnr' cb rt) p1) ?_ + rintro ⟨s', a'⟩ ⟨⟨ea, σ0, h0eq, hIR, hRB⟩, hbk⟩ + simp only [WP.uncurry'_pair] at ea hbk hIR hRB + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk := hbk + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hRB.mono fun b hb => RBW.cnW hS hb + have hDbg := J.rbw_dbg hRB + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := ⟨fun u hu => (by rw [hcn'] at hu; cases hu), F.idleOK J.act.2⟩ + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcn' + have hrun3 : CurRunM σ' := Or.inl (by rw [hcn']; intro h; cases h) + -- the dead: the thread is not dead (runnable); the notification leaves the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + have hdead : ∀ q ∈ D \ {n}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqn : q.bv ≠ n.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => F.epF hx) (fun x hx => ?_) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.ntfnF hx + rcases hc' with rfl | ⟨e1, -⟩ + · exact ⟨_, hy, Or.inl rfl, rfl, Or.inl rfl⟩ + · exact absurd e1 hqn + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {n}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {n}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg (J.rbw_dbgKeep hRB) (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrel⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {n}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have J3 : JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s' a σ' := + J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R + (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) + rw [heq] + exact ⟨_, Set.mem_singleton _, J3, NatFoot.ofWS hF.1 hW + (fun b hb => Or.inl (Ipc.CnW.oobj hS hb)) (fun b hb => J.cnW_nv hb) + (fun b hb => J.cnW_nh hb) F.natK⟩ + +set_option maxHeartbeats 16000000 in +/-- The blocking receive up to `maybe_return_sched_context` at a resumed or choose mid state (generalises `recvSigBlockPreB`). -/ +theorem JAtZB.recvSigBlockPreMB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hr : Fl4Spec.runnable xt.tcbState = true) + (hti : σ.curThread ≠ σ.idleThread) (hnrel : t.bv ∉ σ.releaseQueue) + (hact : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + Ipc.rsPre st s t n ⦃ s4 => ∃ σ3, + Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ = Fl4Spec.maybeReturnSc n.bv t.bv σ3 ∧ + ¬ P1.Runnable (L.mem s4 (P1.stA t)) ∧ + σ3.kheap t.bv = some (.tcb { xt with tcbState := .blockedOnNotification n.bv }) ∧ + (∃ q, σ3.kheap n.bv = some (.notification { x with ntfnObj := .waitingNtfn q })) ∧ + σ3.schedulerAction = .chooseNewThread ∧ σ3.curThread = σ.curThread ∧ σ3.idleThread = σ.idleThread ∧ + JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s4 a σ3 ∧ + NatFoot (L := L) Z s σ s4 σ3 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have hqE : ∀ e ∈ Z.E, ∀ z, σ.kheap e.bv = some (.endpoint z) → t.bv ∉ z.epQueue := + fun e he z hz => Endp.not_queued hI.1.toE ht hkt hnb3 e he z hz + have hnqN : t.bv ∉ Fl4Spec.ntfnQueueOf x := not_member hI.1.toN ht hkt hnn n hn x hk + have hq : ∀ y ∈ Fl4Spec.ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.ntfnQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, hcn, hsrt⟩ := recvSigBlockPre_eq hkt hk hna hnqN hq hr (J.isorted.2 _ _ hk) + have hcn' : σ'.schedulerAction = .chooseNewThread := by + rcases hact with hres | hch + · exact hcn hcur.symm hres + · rw [recvSigBlockPre_act hkt hk hnqN hq heq (by rw [hch]; intro e; cases e)]; exact hch + -- P1's `BlkOK` after + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hx := hI.1.toN.at_ntfn hn hk + have hst0 : 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 := Z.T) (h ▸ this) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (h ▸ this) rfl) + | activeNtfn b => exact absurd h (hna b) + have h3 : L.epState s n ≠ 3#u64 := by + rw [epState_eq] + rcases hst0 with h | h + · rw [h, ntfn_consts.1]; decide + · rw [h, ntfn_consts.2.1]; decide + have hidW : L.idleW s ≠ t := + (J.words_of_spec ht (fun u hu => by rcases hact with h | h <;> rw [h] at hu <;> cases hu) (by rw [← hcur]; exact hti)).2 + have h01 : L.epState s n = 0#u64 ∨ L.epState s n = 1#u64 := by + rw [epState_eq] + rcases hst0 with h | h + · exact Or.inl (by rw [h, ntfn_consts.1]) + · exact Or.inr (by rw [h, ntfn_consts.2.1]) + have p1 := P1.ntfn_blockPre s t n Q0 R0 h0 hB0 hX0 hS0 hSg0 hcb hidW (J.zH.2.2.2 n hn) h01 + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (receive_signal_preF J.ip s σ hI ht hkt hnn hqE hn hk hna + (hsort n (Or.inr hn)) cb rt) p1) ?_ + rintro s' ⟨⟨σ0, h0eq, hspec, hIR, hRB, hkn3, hkt3⟩, hbk⟩ + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := + ⟨hbk.1, hbk.2.1, hbk.2.2.1, hbk.2.2.2.1, hbk.2.2.2.2.1⟩ + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hRB.mono fun b hb => RBW.cnW hS hb + have hDbg := J.rbw_dbg hRB + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := ⟨fun u hu => (by rw [hcn'] at hu; cases hu), F.idleOK J.act.2⟩ + have hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ' := Or.inl hcn' + have hrun3 : CurRunM σ' := Or.inl (by rw [hcn']; intro h; cases h) + -- the dead: the thread is not dead (runnable); the notification leaves the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + have hdead : ∀ q ∈ D \ {n}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqn : q.bv ≠ n.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => F.epF hx) (fun x hx => ?_) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.ntfnF hx + rcases hc' with rfl | ⟨e1, -⟩ + · exact ⟨_, hy, Or.inl rfl, rfl, Or.inl rfl⟩ + · exact absurd e1 hqn + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {n}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {n}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg (J.rbw_dbgKeep hRB) (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrel⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {n}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have B' : JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s' a σ' := + J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R + (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) + exact ⟨σ', hspec, hbk.2.2.2.2.2.1, hkt3, hkn3, hcn', F.sk.ct, F.sk.idle, B', + NatFoot.ofWS hF.1 hW (fun _ hb => Or.inl (Ipc.CnW.oobj hS hb)) (fun _ hb => J.cnW_nv hb) + (fun _ hb => J.cnW_nh hb) F.natK⟩ + +/-- **Every thread-SC pair of a current thread under `CurInv` is active** (the `hscA` of `recvSigBlockMB`). -/ +theorem JAtZB.blockScA (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (hcur : σ.curThread = t.bv) + (hC : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) : + ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true := by + have hS := J.xtop.fin.1.1.1 + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hcur, hkt] at hkc; cases hkc + intro p y n hp hyt + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp).2⟩ + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp hyt + rw [hkt] at hku; cases hku + have e1 : σ.curSc = q.bv := Option.some.inj (hscc.symm.trans hxs) + rw [← e1] at hp + exact hC.active (J.curSc_ne_idle hC hti) y n hp + +/-- **The same for an SC-less current thread**: no SC points at it. -/ +theorem JAtZB.blockScA_noSc (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hsc0 : xt.tcbSchedContext = none) : + ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true := by + have hS := J.xtop.fin.1.1.1 + intro p y n hp hyt + obtain ⟨q, hqC, eq⟩ := J.obj.sc p y n hp + subst eq + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have hq0 := J.c0 q hqC + have hqS : q ∈ P1.Heap.scs H := ⟨_, (hJ.sc q y n hq0 hp).2⟩ + obtain ⟨u, -, -, xu, hku, hxs⟩ := scBack_of hS J.blk.2.2.1 J.tcbs_eq.1.symm hqC hqS hq0 hp hyt + rw [hkt] at hku; cases hku + rw [hsc0] at hxs; cases hxs + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvMidTop.lean b/proofs/Fl4Proofs/P3/VmRecvMidTop.lean new file mode 100644 index 000000000..c496dee8d --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvMidTop.lean @@ -0,0 +1,99 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2): **`handle_recv` at a resumed or choose mid state at `VmEptState`** +(`VmEptState.handleRecvMidV`, VmEntryS3Recv's `handleRecvV` over `VMidF`): the endpoint cells of P3/VmRecvMidAsm, the +notification cells of P3/VmRecvMidDonV, the cap fault by P3/VmFaultMid. The pending-switch state is not here. +No named premise beyond `P0.ArchTotal` and the platform's `MIN_BUDGET`. +-/ +import Fl4Proofs.P3.VmRecvMidAsm +import Fl4Proofs.P3.VmFaultMid +import Fl4Proofs.P3.VmEntryS3Recv +import Fl4Proofs.P3.VmRecvMid +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 4000000 in +/-- **`handle_recv` at a resumed or choose mid state.** -/ +theorem VmEptState.handleRecvMidV [VxE Z] + (hAT : P0.ArchTotal arI) + (hmb : ∀ a : A, sporadic.min_budget arI a ⦃ mb => mb.bv = Fl4Spec.MIN_BUDGET ⦄) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsa : σ.schedulerAction = .resumeCurThread ∨ σ.schedulerAction = .chooseNewThread) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (blocking canReply : Bool) + (hSender : ∀ e b sd g gr rc rc', + (∃ x, σ.kheap e.bv = some (.endpoint x)) → + ((rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) → + JHeld (L := L) Z.th s (.Endpoint e b sd true g gr) → + (∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) → + RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e b sd g gr blocking rc rc') : + syscall.handle_recv st arI s a blocking canReply ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) blocking canReply σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hnq := hF.nq + obtain ⟨hcur, -⟩ := B.curEq hct + obtain ⟨htcb, xs, hks⟩ := B.xtop.curTcb hcur + have hctT : ct ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm + have hI := B.xtop.irelK B.ks B.buf + have hxs := hI.1.toE.at_tcb hctT hks + have hR := B.xtop.rel.2.1.1 + have hV := B.xtop.rel.2.1.2.1 + refine handle_recv_refinesH (VEAnyX (L := L) arI AR) (JHeld (L := L) Z.th s) s a σ + blocking canReply ct hct hcur hks + (by rw [← regIdx_cap]; exact hxs.regs _) + (by rw [← regIdx_reply]; exact hxs.regs _) + (fun cptr => lookup_cap_refines Z.th s σ hR hV ct htcb cptr) + (fun cptr => WP.spec_mono (lookup_cap_held Z.th s σ hR hV ct htcb cptr) fun r hr c hc => ⟨?_, hr c hc⟩) + ⟨?_, ?_, recv_bound_leaf hI.1 B.obj, ?_⟩ + · obtain ⟨y, hy, hcv⟩ := hr c hc + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + cases c with + | Endpoint p b x1 x2 x3 x4 => + obtain ⟨xe, hke, -⟩ := hI.1.ep p (B.hset.1 p ((hJ.typed _ hy).1 p b x1 x2 x3 x4 hcv)) + exact ⟨xe, hke⟩ + | Reply p g => + obtain ⟨xr, hkr, -⟩ := hI.1.reply p (B.hset.2.2 p ((hJ.typed _ hy).2.2 p g hcv)) + exact ⟨xr, hkr⟩ + | Notification p b w r => + obtain ⟨xn, hkn, -⟩ := hI.1.ntfn p (B.capN ⟨y, b, w, r, hy, hcv⟩).1 + exact ⟨xn, hkn⟩ + | _ => trivial + · intro p b sd g gr rc rc' hEp hRc hHeld hReply + by_cases hPoll : RecvIpcPollCase (L := L) ct blocking s σ p rc + · exact J.recvIpcPollCaseVE hctT blocking p b sd g gr rc rc' hEp hRc hPoll + · by_cases hAct : RecvIpcActCase (L := L) ct s σ rc + · exact J.recvIpcActCaseMVE hAT hmb hF hsa hnq hctT hcur blocking p b sd g gr rc rc' hEp hRc hAct + · exact J.recvEndpointReachedMV hAT hmb hctT hcur hF hsa hnq blocking p b sd g gr rc rc' hEp hRc hHeld hReply + (hSender p b sd g gr rc rc' hEp hRc hHeld hReply) + · rintro p b w rd ac hcr ⟨x, hk⟩ hh + obtain ⟨n, hn, h⟩ := B.obj.ntfn _ x hk + rw [Sched.bv_inj h] at hn + rw [hcr.abs] + refine J.recvNtfnMVE hAT hmb hctT hcur hF hsa hnq hn hk blocking (fun _ _ => ?_) b.bv (rightsOf w rd false false) + obtain ⟨y, hy, hcv⟩ := hh + have hHn : H p = some .ntfn := B.zH.2.2.2 p hn + have hfit := B.kern.heap.fits p _ hHn + simp only [P1.HKind.bits] at hfit + exact B.cover_cap (P1.WritesIn.refl s _) hy (by rw [capAt_capV, hcv]) (fun h => by cases h) + (ok_inj (phys_eq _).symm) rfl (range_ntfn p b w rd hfit) fun k hk' => by + rw [hHn] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + · intro cptr lf lf' h + exact WP.spec_mono (J.handleFaultMVE hAT hF hsa hct (f := .Cap cptr true lf) + (f' := .capFault (Fl4Spec.ofBl (Fl4Spec.toBl cptr.bv)) true lf') + (FaultCorrG.cap ⟨cptr, true, lf, lf', rfl, by rw [ofBl_toBl64], h⟩)) + fun _ ⟨z, hz, _, hJ⟩ => ⟨z, hz, hJ⟩ + + +end +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmRecvS.lean b/proofs/Fl4Proofs/P3/VmRecvS.lean new file mode 100644 index 000000000..ba1368d4a --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmRecvS.lean @@ -0,0 +1,367 @@ +/- +P3 (line mmuAD, Stage 3 step 3.2 order step 2): **`handle_recv` at a pending-switch state at `VmEptState`** +(`VmEptState.handleRecvS`): the copy of VmRecvMidTop's `handleRecvMidV` and its endpoint / notification cells at +`switchThread u` (`CurInv` holds, so there are no N cells), with the post `VEAnyS = VEAnyX ∨ VEAnyR`: the V leaves +(P3/VmEntryS3Recv, VmRecvMidAsm's cells, `recvSigReturnSVE`, `recvIpcWaitReturnSVE`, `recvSendingReturnSV`, +`VmFaultSw`) post `VEAnyX`, the four R cells of P3/VmSwitchRTop post `VEAnyR`; the case hypotheses `hnoret` / "the handler +endpoint has no waiting receiver" are discharged by the case split against the V-post cells. No named premise beyond +`P0.ArchTotal`. +-/ +import Fl4Proofs.P3.VmRecvMidTop +import Fl4Proofs.P3.VmSwitchRTop +import Fl4Proofs.P3.VmFaultSw +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + +/-- **Some `VmEptState` or some `VmEptStateR`** (the post of the cells of a pending switch). -/ +def VEAnyS (AR : A → Fl4Spec.Cr3 → Prop) (s : S) (a : A) (σ : Fl4Spec.State Unit) : Prop := + VEAnyX (L := L) arI AR s a σ ∨ VEAnyR (L := L) arI AR s a σ + +end + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +theorem VEAny_toS {m : Result (S × A)} {Y : Set (Unit × Fl4Spec.State Unit)} + (h : m ⦃ r => ∃ z ∈ Y, VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄) : + m ⦃ r => ∃ z ∈ Y, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono h fun _ ⟨z, hz, h⟩ => ⟨z, hz, Or.inl h⟩ + +theorem VEAnyR_toS {m : Result (S × A)} {Y : Set (Unit × Fl4Spec.State Unit)} + (h : m ⦃ r => ∃ z ∈ Y, VEAnyR (L := L) arI AR r.1 r.2 z.2 ⦄) : + m ⦃ r => ∃ z ∈ Y, VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono h fun _ ⟨z, hz, h⟩ => ⟨z, hz, Or.inr h⟩ + +theorem VMidF.curInv_sw (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) : CurInv σ := + J.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + +/-- Resolve an endpoint receive that completes its active bound notification, with a null or free reply (pending switch). -/ +theorem VmEptState.recvIpcActCaseSVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hAct : RecvIpcActCase (L := L) ct s σ rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := by + apply VEAny_toS + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨⟨xt, np, xn, badge, hkt, hbn, hkn, hactive⟩, hrr⟩ := hAct + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + obtain ⟨rv, hra⟩ := B.recvReplyArg hRc hrr + have hC : CurInv σ := B.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + have hxt := hI.1.toE.at_tcb ht hkt + have hb : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hbn + have hb0 : L.mem s (tBoundA ct) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb; cases hb + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hb0] at hb + exact P1.eq_of_bv (Option.some.inj hb) + have hst : stOf (L.mem s) n = obj.NTFN_ACTIVE := + stOf_active (T := Z.T) (hactive ▸ (hI.1.toN.at_ntfn hn hkn).obj) rfl + let rights := rightsOf sd true g gr + rw [show absCap (.Endpoint p b sd true g gr) = .endpointCap p.bv b.bv rights from rfl, + receiveIpc_active b.bv rights blocking (replyArg_spec B.ip.e hI.1.toE hra) hkt hbn hke hkn hactive] + exact receive_ipc_preA s a ct p b sd true g gr blocking rc rc' rv hra hbound + (by rw [← hbound]; exact hb0) hst (J.recvActiveMidVE hAT ht hcur hC hF.ni hF.run hF.nsw hnq hn hkn hactive blocking b.bv rights).2 + +set_option maxHeartbeats 16000000 in +/-- The blocking endpoint receive at a pending switch: an SC return ends in `VEAnyX`, the others in `VEAnyR`. -/ +theorem VmEptState.recvIpcWaitCaseSV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHeld : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hReply : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hWait : RecvIpcBlockCase (L := L) ct blocking s σ p rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ p rc) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hret : ∃ xt np xn, σ.kheap ct.bv = some (.tcb xt) ∧ xt.tcbBoundNotification = some np ∧ + σ.kheap np = some (.notification xn) ∧ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none) + · obtain ⟨xt, np, xn, hkt, hb, hkn, hr⟩ := hret + rcases hWait with hBl | hBd + · exfalso + have := hBl.2.1 xt hkt + rw [this] at hb; cases hb + · refine VEAny_toS (J.recvIpcWaitReturnSVE hF hsw hnq ht hcur blocking p b sd g gr rc rc' hEp hRc hHeld hReply hBd + (fun xt' np' xn' h1 h2 h3 => ?_)) + rw [hkt] at h1; cases h1 + rw [hb] at h2; cases h2 + rw [hkn] at h3; cases h3 + exact hr + · exact VEAnyR_toS (J.recvIpcWaitCaseSVE hF hsw hnq ht hcur blocking p b sd g gr rc rc' hEp hRc hHeld hReply hWait + (fun xt np xn h1 h2 h3 h => hret ⟨xt, np, xn, h1, h2, h3, h⟩)) + +set_option maxHeartbeats 8000000 in +/-- The free-reply endpoint receive at a pending switch. -/ +theorem VmEptState.recvFreeEndpointSV [VxE Z] + (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (e cb : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap e.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hFree : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64) + (hSender : ∀ s' σ' Z' [VxE Z'] Ac' Lst', + RecvCtxReached (L := L) ct blocking s σ s' σ' → + VmEptState (L := L) arI V X XL AR Z' H Cs Ac' Lst' D s' a σ' → + RecvSenderLeaves (L := L) arI AR Z' s' a σ' ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hPoll : RecvIpcPollCase (L := L) ct blocking s σ e rc + · exact VEAny_toS (J.recvIpcPollCaseVE ht blocking e cb sd g gr rc rc' hEp hRc hPoll) + by_cases hAct : RecvIpcActCase (L := L) ct s σ rc + · exact J.recvIpcActCaseSVE hAT hF hsw hnq ht hcur blocking e cb sd g gr rc rc' hEp hRc hAct + by_cases hWait : RecvIpcBlockCase (L := L) ct blocking s σ e rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ e rc + · exact J.recvIpcWaitCaseSV hF hsw hnq ht hcur blocking e cb sd g gr rc rc' hEp hRc hHd hHr hWait + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + have hrun := hF.run xt (by rw [hcur]; exact hkt) + have hq : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hC : CurInv σ := B.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + have hrl : ct.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hbN : xt.tcbBoundNotification = none ∨ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ Fl4Spec.isActive xn = false := by + cases hbn : xt.tcbBoundNotification with + | none => exact Or.inl rfl + | some n => + obtain ⟨xn, hkn⟩ := B.sv.tcbNtfn ct.bv xt n hkt hbn + refine Or.inr ⟨n, xn, rfl, hkn, ?_⟩ + cases hxo : xn.ntfnObj with + | activeNtfn b => exact (hAct ⟨⟨xt, n, xn, b, hkt, hbn, hkn, hxo⟩, hFree⟩).elim + | idleNtfn => simp [Fl4Spec.isActive, hxo] + | waitingNtfn q => simp [Fl4Spec.isActive, hxo] + have hkSend : ∃ queue, σ.kheap e.bv = some (.endpoint (.sendEP queue)) := by + by_contra hSend + have hn : ∀ x, σ.kheap e.bv = some (.endpoint x) → ∀ queue, x ≠ .sendEP queue := by + rintro x hx q rfl + exact hSend ⟨q, hx⟩ + cases blocking + · refine hPoll ⟨rfl, ?_, hn, hFree⟩ + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + exact hbN + · rcases hbN with hb | ⟨n, xn, hb, hkn, hna⟩ + · refine hWait (Or.inl ⟨rfl, ?_, hn, hFree⟩) + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + exact hb + · exact hWait (Or.inr ⟨rfl, ⟨xt, n, xn, hkt, hb, hkn, hna⟩, hn, hFree⟩) + obtain ⟨queue, hk⟩ := hkSend + obtain ⟨ee, he, eq⟩ := B.obj.ep _ (.sendEP queue) hk + rw [Sched.bv_inj eq] at he + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hFree + by_cases hReturn : blocking = true ∧ ∃ n xn, xt.tcbBoundNotification = some n ∧ + σ.kheap n = some (.notification xn) ∧ xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none + · obtain ⟨rfl, np, xn, hb, hkn, hsc⟩ := hReturn + have hna : Fl4Spec.isActive xn = false := by + rcases hbN with h | ⟨n', xn', hb', hkn', h⟩ + · rw [hb] at h + cases h + · rw [hb] at hb' + cases hb' + rw [hkn] at hkn' + cases hkn' + exact h + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + exact VEAny_toS (J.recvSendingReturnSV ht he hcur hF hC hsw hnq hkt hn hkn hb hna hsc hk hra hHd hHr + (fun s' σ' Ac' Lst' hread hc hs J' => + hSender s' σ' (Z.mv Z.G Z.vs) Ac' Lst' (Or.inr ⟨n, rfl, hread, hc, hs⟩) J')) + · have hno : RecvNoReturn ct blocking σ := by + intro xt' hkt' + rw [hkt] at hkt' + cases hkt' + rcases hbN with hb | ⟨n, xn, hb, hkn, hna⟩ + · exact Or.inl hb + · exact Or.inr ⟨n, xn, hb, hkn, hna, fun hbl hsc => hReturn ⟨hbl, n, xn, hb, hkn, hsc⟩⟩ + obtain ⟨u, hu⟩ := hsw + exact VEAny_toS (J.recvSendingNoReturnV ht he hcur hkt hrun + (Or.inr (Or.inr ⟨u, hu, fun h => hF.nsw (by rw [hu, hcur, h])⟩)) hq hrl hk hno + hra hHd hHr + (hSender s σ Z Ac Lst (Or.inl ⟨rfl, rfl⟩) J)) + +set_option maxHeartbeats 8000000 in +/-- The endpoint leaf of receive at a pending switch. -/ +theorem VmEptState.recvEndpointReachedSV [VxE Z] + (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (e cb : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap e.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hSender : RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e cb sd g gr blocking rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) blocking rc' σ).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := by + by_cases hFree : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 + · exact J.recvFreeEndpointSV hAT ht hcur hF hsw hnq blocking e cb sd g gr rc rc' hEp hRc hHd hHr hFree + (fun s' σ' Z' [VxE Z'] Ac' Lst' hCtx J' => + hSender V X XL Z' Ac' Lst' D s' a σ' ⟨s, σ, Or.inl ⟨rfl, rfl, rfl⟩, hCtx⟩ J') + rcases hRc with ⟨hr, -⟩ | ⟨hcr, q, g', rfl, xr, hkr⟩ + · exact (hFree (Or.inl hr)).elim + · have hne : L.mem s (Endp.rTcbA q) ≠ 0#u64 := fun h => hFree (Or.inr ⟨q, g', rfl, h⟩) + refine J.recvIpcLinkedS (P := VEAnyS (L := L) arI AR) ht hcur hF hsw hnq blocking e cb sd g gr q g' rc' hcr hEp hkr hHd + (hHr q g' rfl) hne ?_ + intro i V' Ac' Lst' s' a' σ' hread hi0 hiNe hc hs J' hcur' hF' hsw' hnq' hEp' hx h0' hHd' hHr' + obtain ⟨xr', hkr', -⟩ := hx + exact J'.recvFreeEndpointSV hAT ht hcur' hF' hsw' hnq' blocking e cb sd g gr (.Reply q g') rc' hEp' + (Or.inr ⟨hcr, q, g', rfl, xr', hkr'⟩) hHd' (fun _ _ _ => hHr') (Or.inr ⟨q, g', rfl, h0'⟩) + (fun s2 σ2 Z2 [VxE Z2] Ac2 Lst2 hCtx J2 => + hSender V X XL Z2 Ac2 Lst2 D s2 a' σ2 + ⟨s', σ', Or.inr ⟨q, g', i, rfl, hread, hi0, hiNe, hc, hs⟩, hCtx⟩ J2) + +set_option maxHeartbeats 8000000 in +/-- **The notification arm of receive at a pending switch.** -/ +theorem VmEptState.recvNtfnSVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (blocking : Bool) + (hcov : blocking = true → (∀ b, x.ntfnObj ≠ .activeNtfn b) → + (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n blocking ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) blocking σ).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hC : CurInv σ := B.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + by_cases hs : ∃ badge, x.ntfnObj = .activeNtfn badge + · obtain ⟨badge, hactive⟩ := hs + exact VEAny_toS (J.recvActiveMidVE hAT ht hcur hC hF.ni hF.run hF.nsw hnq hn hk hactive blocking cb rt).1 + have hna : ∀ badge, x.ntfnObj ≠ .activeNtfn badge := fun badge h => hs ⟨badge, h⟩ + cases blocking + · exact VEAny_toS (J.recvSigPollVE ht hn hk hna cb rt) + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + by_cases hret : x.ntfnSc = xt.tcbSchedContext ∧ x.ntfnSc ≠ none + · have hsc : x.ntfnSc = some σ.curSc := by + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hk'] at hkc + cases hkc + exact hret.1.trans hscc + exact VEAny_toS (J.recvSigReturnSVE ht hcur hF hsw hnq hn hk hna hsc (hcov rfl hna) cb rt) + · exact VEAnyR_toS (J.recvSigBlockSVE hAT ht hcur hF hsw hnq hn hk hna (fun xt' hkt' => by + rw [hkt] at hkt' + cases hkt' + exact hret) (hcov rfl hna) cb rt) + +set_option maxHeartbeats 4000000 in +/-- **`handle_recv` at a pending-switch state.** -/ +theorem VmEptState.handleRecvS [VxE Z] + (hAT : P0.ArchTotal arI) + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + {ct : U64} (hct : sched.cur_thread st s = ok ct) + (blocking canReply : Bool) + (hSender : ∀ e b sd g gr rc rc', + (∃ x, σ.kheap e.bv = some (.endpoint x)) → + ((rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) → + JHeld (L := L) Z.th s (.Endpoint e b sd true g gr) → + (∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) → + RecvSenderPrefixes (L := L) arI AR H Cs s a σ ct e b sd g gr blocking rc rc') : + syscall.handle_recv st arI s a blocking canReply ⦃ r => + ∃ z ∈ (Fl4Spec.handleRecv (Z := Unit) blocking canReply σ).1, + VEAnyS (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hnq := hF.nq + obtain ⟨hcur, -⟩ := B.curEq hct + obtain ⟨htcb, xs, hks⟩ := B.xtop.curTcb hcur + have hctT : ct ∈ Z.T := TopSR.cur_mem B.xtop.fin.1 hcur.symm + have hI := B.xtop.irelK B.ks B.buf + have hxs := hI.1.toE.at_tcb hctT hks + have hR := B.xtop.rel.2.1.1 + have hV := B.xtop.rel.2.1.2.1 + refine handle_recv_refinesH (VEAnyS (L := L) arI AR) (JHeld (L := L) Z.th s) s a σ + blocking canReply ct hct hcur hks + (by rw [← regIdx_cap]; exact hxs.regs _) + (by rw [← regIdx_reply]; exact hxs.regs _) + (fun cptr => lookup_cap_refines Z.th s σ hR hV ct htcb cptr) + (fun cptr => WP.spec_mono (lookup_cap_held Z.th s σ hR hV ct htcb cptr) fun r hr c hc => ⟨?_, hr c hc⟩) + ⟨?_, ?_, recv_bound_leaf hI.1 B.obj, ?_⟩ + · obtain ⟨y, hy, hcv⟩ := hr c hc + obtain ⟨Tr, Cq, hJ⟩ := B.jpl + cases c with + | Endpoint p b x1 x2 x3 x4 => + obtain ⟨xe, hke, -⟩ := hI.1.ep p (B.hset.1 p ((hJ.typed _ hy).1 p b x1 x2 x3 x4 hcv)) + exact ⟨xe, hke⟩ + | Reply p g => + obtain ⟨xr, hkr, -⟩ := hI.1.reply p (B.hset.2.2 p ((hJ.typed _ hy).2.2 p g hcv)) + exact ⟨xr, hkr⟩ + | Notification p b w r => + obtain ⟨xn, hkn, -⟩ := hI.1.ntfn p (B.capN ⟨y, b, w, r, hy, hcv⟩).1 + exact ⟨xn, hkn⟩ + | _ => trivial + · intro p b sd g gr rc rc' hEp hRc hHeld hReply + by_cases hPoll : RecvIpcPollCase (L := L) ct blocking s σ p rc + · exact VEAny_toS (J.recvIpcPollCaseVE hctT blocking p b sd g gr rc rc' hEp hRc hPoll) + · by_cases hAct : RecvIpcActCase (L := L) ct s σ rc + · exact J.recvIpcActCaseSVE hAT hF hsw hnq hctT hcur blocking p b sd g gr rc rc' hEp hRc hAct + · exact J.recvEndpointReachedSV hAT hctT hcur hF hsw hnq blocking p b sd g gr rc rc' hEp hRc hHeld hReply + (hSender p b sd g gr rc rc' hEp hRc hHeld hReply) + · rintro p b w rd ac hcr ⟨x, hk⟩ hh + obtain ⟨n, hn, h⟩ := B.obj.ntfn _ x hk + rw [Sched.bv_inj h] at hn + rw [hcr.abs] + refine J.recvNtfnSVE hAT hctT hcur hF hsw hnq hn hk blocking (fun _ _ => ?_) b.bv (rightsOf w rd false false) + obtain ⟨y, hy, hcv⟩ := hh + have hHn : H p = some .ntfn := B.zH.2.2.2 p hn + have hfit := B.kern.heap.fits p _ hHn + simp only [P1.HKind.bits] at hfit + exact B.cover_cap (P1.WritesIn.refl s _) hy (by rw [capAt_capV, hcv]) (fun h => by cases h) + (ok_inj (phys_eq _).symm) rfl (range_ntfn p b w rd hfit) fun k hk' => by + rw [hHn] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + · intro cptr lf lf' h + obtain ⟨u0, hu0⟩ := hsw + exact WP.spec_mono (J.handleFaultSV hAT hF hu0 hct (fun e => hF.nsw (by rw [hu0, hcur, e])) (f := .Cap cptr true lf) + (f' := .capFault (Fl4Spec.ofBl (Fl4Spec.toBl cptr.bv)) true lf') + (FaultCorrG.cap ⟨cptr, true, lf, lf', rfl, by rw [ofBl_toBl64], h⟩)) + fun _ ⟨z, hz, _, hJ⟩ => ⟨z, hz, hJ⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmSwitchR.lean b/proofs/Fl4Proofs/P3/VmSwitchR.lean new file mode 100644 index 000000000..22dafab4a --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmSwitchR.lean @@ -0,0 +1,1128 @@ +/- +P3 (line mmuAD, Stage 3 step 3 (R layer), generated by `handoff/tools/gen_swR.py`): **the pending-switch (S) blocking leaves** +ending in `JAtZRB`: notification block, endpoint block (and with an idle bound notification), fault with a null handler or an +idle/sending handler endpoint. Each is the resumed/choose `…MB` leaf with the action kept (`switchThread u`, u ≠ ct). +No named premise beyond `P0.ArchTotal`. +-/ +import Fl4Proofs.P3.VmJAtZRB +import Fl4Proofs.P3.VmFaultMid +import Fl4Proofs.P3.XTopJointRecvSigM +import Fl4Proofs.P3.XTopJointRecvSigRetM +import Fl4Proofs.P3.XTopJointRecvIpcBlockM +import Fl4Proofs.P3.XTopJointFaultM +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +/-- The entry facts of a blocking leaf at a `VMidF` state with a pending switch (`CurInv` holds). -/ +theorem JAtZB.midBlkFactsS (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) + (hcur : σ.curThread = t.bv) (hF : VMidF σ) (hsw : ∃ u, σ.schedulerAction = .switchThread u) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) : + t.bv ∉ σ.releaseQueue ∧ + (∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) ∧ + P1.CanBlock st (P1.Heap.tcbs H) Ac s t := by + have hC : CurInv σ := J.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + exact ⟨by rw [← hcur]; exact hC.notRel, J.blockScA hcur hC hF.ni hkt, J.canBlockMidB ht hcur hkt hF⟩ + +/-- `JAtN` with `JAtZRB`. -/ +def JAtNR (arI : machine.Arch A) (AR : A → Fl4Spec.Cr3 → Prop) (Z : XSets) (H : P1.Heap) (Cs : Set U64) (s : S) + (σ : Fl4Spec.State Unit) (s' : S) (a' : A) (σ' : Fl4Spec.State Unit) : Prop := + ∃ (V : XVs) (Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + JAtZRB (L := L) arI AR (Z.mv Z.G V) H Cs Ac' Lst' D' s' a' σ' ∧ NatFoot (L := L) Z s σ s' σ' + +theorem JAtNR.of {Ac' : Set U64} {Lst' : U64 → List U64} {D' : Set U64} {s' : S} {a' : A} {σ' : Fl4Spec.State Unit} + (B' : JAtZRB (L := L) arI AR Z H Cs Ac' Lst' D' s' a' σ') (F : NatFoot (L := L) Z s σ s' σ') : + JAtNR (L := L) arI AR Z H Cs s σ s' a' σ' := + ⟨Z.vs, Ac', Lst', D', B', F⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvSigBlockSB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hnrel : t.bv ∉ σ.releaseQueue) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) + (hti : σ.curThread ≠ σ.idleThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hnr' : ¬ (x.ntfnSc = xt.tcbSchedContext ∧ x.ntfnSc ≠ none)) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hsnr : σ.schedulerAction ≠ .resumeCurThread := by + obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have hqE : ∀ e ∈ Z.E, ∀ z, σ.kheap e.bv = some (.endpoint z) → t.bv ∉ z.epQueue := + fun e he z hz => Endp.not_queued hI.1.toE ht hkt hnb3 e he z hz + have hnqN : t.bv ∉ Fl4Spec.ntfnQueueOf x := not_member hI.1.toN ht hkt hnn n hn x hk + have hq : ∀ y ∈ Fl4Spec.ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.ntfnQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, hcn, hsrt⟩ := recvSigBlock_eq hkt hk hna hnqN hq hr (J.isorted.2 _ _ hk) hnr' cb rt + have hact' : σ'.schedulerAction = σ.schedulerAction := + recvSigBlock_act hkt hk hna hnqN hq hnr' cb rt heq hsnr + -- P1's `BlkOK` after + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hx := hI.1.toN.at_ntfn hn hk + have hst0 : 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 := Z.T) (h ▸ this) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (h ▸ this) rfl) + | activeNtfn b => exact absurd h (hna b) + have h3 : L.epState s n ≠ 3#u64 := by + rw [epState_eq] + rcases hst0 with h | h + · rw [h, ntfn_consts.1]; decide + · rw [h, ntfn_consts.2.1]; decide + have p1 := P1.notification.receive_signal.ipc hAT s a t n true Q0 R0 h0 hB0 hX0 hS0 hSg0 hcb + (J.zH.2.2.2 n hn) (J.zH.2.2.2 n hn) h3 + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (receive_signal_blockF (ar := arI) J.ip s a σ hI ht hkt hnn hqE hn hk hna + (hsort n (Or.inr hn)) hnr' cb rt) p1) ?_ + rintro ⟨s', a'⟩ ⟨⟨ea, σ0, h0eq, hIR, hRB⟩, hbk⟩ + simp only [WP.uncurry'_pair] at ea hbk hIR hRB + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk := hbk + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hRB.mono fun b hb => RBW.cnW hS hb + have hDbg := J.rbw_dbg hRB + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := by + refine J.act.keepE (E := fun p => p = t.bv) ⟨fun p x0 hp hx => ?_, fun p y m hp => ⟨y, F.scF hp, rfl⟩⟩ + (fun u hu => Or.inl (by rw [F.sk.rel] at hu; exact hu)) (Or.inl hact') F.sk.idle + (fun u hu e => hnsw (by rw [hu, e, hcur])) (fun h => hti (hcur.trans h.symm)) F.sk.isc + obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + rcases hs' with e1 | ⟨e1, -⟩ + · rw [hk3, e1] + · exact absurd e1 hp + have hcur3 : CurInvR σ' := fun h => absurd (hact'.symm.trans h) hsnr + have hrun3 : CurRunM σ' := Or.inl (by rw [hact']; exact hsnr) + -- the dead: the thread is not dead (runnable); the notification leaves the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + have hdead : ∀ q ∈ D \ {n}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqn : q.bv ≠ n.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => F.epF hx) (fun x hx => ?_) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.ntfnF hx + rcases hc' with rfl | ⟨e1, -⟩ + · exact ⟨_, hy, Or.inl rfl, rfl, Or.inl rfl⟩ + · exact absurd e1 hqn + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {n}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {n}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg (J.rbw_dbgKeep hRB) (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrel⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {n}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have J3 : JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s' a σ' := + J.cs_frameIR hks hX3 hW (fun b hb => Ipc.CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R + (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) + rw [heq] + exact ⟨_, Set.mem_singleton _, J3, NatFoot.ofWS hF.1 hW + (fun b hb => Or.inl (Ipc.CnW.oobj hS hb)) (fun b hb => J.cnW_nv hb) + (fun b hb => J.cnW_nh hb) F.natK⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvSigBlockPreSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hr : Fl4Spec.runnable xt.tcbState = true) + (hti : σ.curThread ≠ σ.idleThread) (hnrel : t.bv ∉ σ.releaseQueue) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + Ipc.rsPre st s t n ⦃ s4 => ∃ σ3, + Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ = Fl4Spec.maybeReturnSc n.bv t.bv σ3 ∧ + ¬ P1.Runnable (L.mem s4 (P1.stA t)) ∧ + σ3.kheap t.bv = some (.tcb { xt with tcbState := .blockedOnNotification n.bv }) ∧ + (∃ q, σ3.kheap n.bv = some (.notification { x with ntfnObj := .waitingNtfn q })) ∧ + σ3.schedulerAction = σ.schedulerAction ∧ σ3.curThread = σ.curThread ∧ σ3.idleThread = σ.idleThread ∧ + JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s4 a σ3 ∧ + NatFoot (L := L) Z s σ s4 σ3 ⦄ := by + have hsnr : σ.schedulerAction ≠ .resumeCurThread := by + obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have hqE : ∀ e ∈ Z.E, ∀ z, σ.kheap e.bv = some (.endpoint z) → t.bv ∉ z.epQueue := + fun e he z hz => Endp.not_queued hI.1.toE ht hkt hnb3 e he z hz + have hnqN : t.bv ∉ Fl4Spec.ntfnQueueOf x := not_member hI.1.toN ht hkt hnn n hn x hk + have hq : ∀ y ∈ Fl4Spec.ntfnQueueOf x, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.ntfnQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, hcn, hsrt⟩ := recvSigBlockPre_eq hkt hk hna hnqN hq hr (J.isorted.2 _ _ hk) + have hact' : σ'.schedulerAction = σ.schedulerAction := + recvSigBlockPre_act hkt hk hnqN hq heq hsnr + -- P1's `BlkOK` after + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hx := hI.1.toN.at_ntfn hn hk + have hst0 : 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 := Z.T) (h ▸ this) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (h ▸ this) rfl) + | activeNtfn b => exact absurd h (hna b) + have h3 : L.epState s n ≠ 3#u64 := by + rw [epState_eq] + rcases hst0 with h | h + · rw [h, ntfn_consts.1]; decide + · rw [h, ntfn_consts.2.1]; decide + have hidW : L.idleW s ≠ t := + (J.words_of_spec ht (fun u hu e => hnsw (by rw [hu, e, hcur])) (by rw [← hcur]; exact hti)).2 + have h01 : L.epState s n = 0#u64 ∨ L.epState s n = 1#u64 := by + rw [epState_eq] + rcases hst0 with h | h + · exact Or.inl (by rw [h, ntfn_consts.1]) + · exact Or.inr (by rw [h, ntfn_consts.2.1]) + have p1 := P1.ntfn_blockPre s t n Q0 R0 h0 hB0 hX0 hS0 hSg0 hcb hidW (J.zH.2.2.2 n hn) h01 + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (receive_signal_preF J.ip s σ hI ht hkt hnn hqE hn hk hna + (hsort n (Or.inr hn)) cb rt) p1) ?_ + rintro s' ⟨⟨σ0, h0eq, hspec, hIR, hRB, hkn3, hkt3⟩, hbk⟩ + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := + ⟨hbk.1, hbk.2.1, hbk.2.2.1, hbk.2.2.2.1, hbk.2.2.2.2.1⟩ + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := + hRB.mono fun b hb => RBW.cnW hS hb + have hDbg := J.rbw_dbg hRB + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := by + refine J.act.keepE (E := fun p => p = t.bv) ⟨fun p x0 hp hx => ?_, fun p y m hp => ⟨y, F.scF hp, rfl⟩⟩ + (fun u hu => Or.inl (by rw [F.sk.rel] at hu; exact hu)) (Or.inl hact') F.sk.idle + (fun u hu e => hnsw (by rw [hu, e, hcur])) (fun h => hti (hcur.trans h.symm)) F.sk.isc + obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + rcases hs' with e1 | ⟨e1, -⟩ + · rw [hk3, e1] + · exact absurd e1 hp + have hcur3 : CurInvR σ' := fun h => absurd (hact'.symm.trans h) hsnr + have hrun3 : CurRunM σ' := Or.inl (by rw [hact']; exact hsnr) + -- the dead: the thread is not dead (runnable); the notification leaves the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + have hdead : ∀ q ∈ D \ {n}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqn : q.bv ≠ n.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => F.epF hx) (fun x hx => ?_) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.ntfnF hx + rcases hc' with rfl | ⟨e1, -⟩ + · exact ⟨_, hy, Or.inl rfl, rfl, Or.inl rfl⟩ + · exact absurd e1 hqn + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {n}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {n}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg (J.rbw_dbgKeep hRB) (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrel⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {n}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have B' : JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s' a σ' := + J.cs_frameIR hks hX3 hW (fun b hb => Ipc.CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R + (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) + exact ⟨σ', hspec, hbk.2.2.2.2.2.1, hkt3, hkn3, hact', F.sk.ct, F.sk.idle, B', + NatFoot.ofWS hF.1 hW (fun _ hb => Or.inl (Ipc.CnW.oobj hS hb)) (fun _ hb => J.cnW_nv hb) + (fun _ hb => J.cnW_nh hb) F.natK⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcRetSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hC : CurInv σ) (hti : σ.curThread ≠ σ.idleThread) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) + (hr : Fl4Spec.runnable xt.tcbState = true) (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) (hlend : xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) {cb : U64} {sd g gr : Bool} + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) {rc : capability.Cap} + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb sd true g gr) true rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ Ac' Lst' D', JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac' Lst' D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hnrl : t.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hns : ∀ q, x ≠ .sendEP q := fun q h => by subst h; simp [Endp.CanBlock] at hc + -- the SC `i` lent by the notification, the thread's + obtain ⟨hl1, hl2⟩ := hlend + obtain ⟨i0, hi0⟩ := Option.ne_none_iff_exists'.1 hl2 + have hxs0 : xt.tcbSchedContext = some i0 := hl1.symm.trans hi0 + obtain ⟨y, m, hky, -⟩ := J.inv.back t.bv xt i0 hkt hxs0 + obtain ⟨i, hiC, rfl⟩ := J.obj.sc _ y m hky + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨σ2, he2, kt, -, ko, ct, -, -, -, hidl, -, rq, a1, -, hrq2⟩ := + maybeReturn_shape J.inv hkn hkt hky hxs0 hi0 hnqT + -- FL4's bound-notification words + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hsp1 := receiveIpc_boundR b rights (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hk hns hkn hna he2 + refine receive_ipc_blockR_red hrc hbound (by rw [← hbound]; exact hbz) hstN ?_ + refine WP.spec_mono (J.maybeReturnBQ ht hn hiC hkt hkn hxs0 hi0 hnqT ⟨hcb.nq, hcb.nr, hcb.na⟩ + (fun u hu e => hnsw (by rw [hu, e, hcur])) (by rw [← hcur]; exact hti)) ?_ + rintro s' ⟨hW, σ', he', Ac', Lst', J2, F2⟩ + rw [the_result he2 he'] at J2 F2 + have hI2 := J2.xtop.irelK J2.ks J2.buf + -- the objects kept by `maybe_return_sc` + have hte : e.bv ≠ t.bv := fun h => by rw [h, hkt] at hk; cases hk + have hie : e.bv ≠ i.bv := fun h => by rw [h, hky] at hk; cases hk + have hk2 : σ2.kheap e.bv = some (.endpoint x) := by rw [ko _ hte hie]; exact hk + have hnt : n.bv ≠ t.bv := fun h => by rw [h, hkt] at hkn; cases hkn + have hni' : n.bv ≠ i.bv := fun h => by rw [h, hky] at hkn; cases hkn + have hkn2 : σ2.kheap n.bv = some (.notification xn) := by rw [ko _ hnt hni']; exact hkn + have hcur2 : σ2.curThread = t.bv := ct.trans hcur + have hti2 : σ2.curThread ≠ σ2.idleThread := by rw [ct, hidl]; exact hti + have hnq2 : ∀ d p, σ2.curThread ∉ σ2.readyQueues d p := by rw [hcur2]; exact hrq2 + -- the reply cap at the state after + have hrc2 : ReplyArg Z.R (L.mem s') rc rc' rv := by + rcases hrc with h | ⟨bb, r', h1, h2, hrR, h0⟩ + · exact Or.inl h + refine Or.inr ⟨bb, r', h1, h2, hrR, ?_⟩ + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply rv hrR + obtain ⟨xr2, hkr2, hxr2⟩ := hI2.1.toE.reply rv hrR + have hrt : rv.bv ≠ t.bv := fun h => by rw [h, hkt] at hkr; cases hkr + have hri : rv.bv ≠ i.bv := fun h => by rw [h, hky] at hkr; cases hkr + rw [ko _ hrt hri, hkr] at hkr2; cases hkr2 + rw [h0, optRef_zero] at hxr + exact optRef_none (hxr2.symm.trans hxr) + obtain ⟨hcov, hcovR⟩ := J.recvCov_ofW hW he hHd hHr hrc + have hx2 := hI2.1.toE.at_ep he hk2 + have hst2 : stOf (L.mem s') e = 0#u64 ∨ stOf (L.mem s') e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx2.1 + | recvEP q => exact Or.inr hx2.1 + | sendEP q => exact absurd rfl (hns q) + have hsp2 := receiveIpc_boundNA b rights true (replyArg_spec J2.ip.e hI2.1.toE hrc2) kt hbn hk2 hns hkn2 hna + (fun _ h => h.2 h.1) + have key := recvTail_wait (ar := arI) hst2 + (J2.recvIpcWaitB ht hcur2 hti2 hnq2 (fun h => hnrl (rq _ h)) kt hr (Or.inr ⟨a1 hcur.symm, rfl⟩) + he hk2 hc hcov hrc2 hcovR b rights hsp2) + rw [hsp1, ← hsp2] + exact WP.spec_mono key fun _ ⟨z, hz, D', B', F'⟩ => + ⟨z, hz, Ac', Lst', D', B', J.natTrans F2 F'⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcWaitSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hnrl : t.bv ∉ σ.releaseQueue) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : rv ≠ 0#u64 → (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) + (hsp : Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ = + Fl4Spec.receiveIpcBlocked true t.bv e.bv (optRef rv) σ) : + (do + let s3 ← endpoint.wait_for_sender st s t e true rv + ok (s3, a)) ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ D', JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hsnr : σ.schedulerAction ≠ .resumeCurThread := by + obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hnb3 : tsCode xt.tcbState ≠ 3 ∧ tsCode xt.tcbState ≠ 4 := by + rcases runnable_code hr with h | h | h <;> rw [h] <;> decide + have hnn : ∀ m, xt.tcbState ≠ .blockedOnNotification m := fun m h => by rw [h] at hr; cases hr + have htq : t.bv ∉ x.epQueue := Endp.not_queued hI.1.toE ht hkt hnb3 e he x hk + have hq : ∀ y ∈ x.epQueue, ∃ z, σ.kheap y = some (.tcb z) := fun y hy => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hk).2 y hy + exact ⟨z, hz⟩ + obtain ⟨σ', heq, F, hik, -, -, hsrt, -, hactK⟩ := + recvIpcBlock_eqM hkt hk hc htq hq hr (J.isorted.1 _ _ hk) b rights (replyArg_spec J.ip.e hI.1.toE hrc) hsp + -- the reply object (if any): in `R`, linked to no thread + have hrv : rv ≠ 0#u64 → rv ∈ Z.R ∧ L.mem s (Endp.rTcbA rv) = 0#u64 := fun h0 => replyArg_ne J.ip.e hrc h0 + have hact' : σ'.schedulerAction = σ.schedulerAction := hactK hsnr + -- P1's `BlkOK` after and footprint + obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk + have hHe : H e = some .ep := J.zH.2.1 e he + have hx := hI.1.toE.at_ep he hk + have hst2 : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => simp [Endp.CanBlock] at hc + have hst0 : L.epState s e = 0#u64 ∨ L.epState s e = 2#u64 := by rw [epState_eq]; exact hst2 + have hrH : rv ≠ 0#u64 → rv ∈ P1.Heap.replies H := fun h => J.zH.2.2.1 rv (hrv h).1 + have p1b := spec_lift_ok (P1.endpoint.wait_for_sender.inv s t e true rv Q0 R0 h0 hB0 hX0 hS0 hSg0 + (J.zH.1 t ht) (fun _ => hcb) hHe hst0 (fun h => ⟨hrH h, Or.inl (hrv h).2⟩)) a + have p1f := spec_lift_ok (P1.endpoint.wait_for_sender.footR (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) + (Z := P1.Heap.scBits H) s t e rv h0 hcb hHe + (fun h => ⟨hSg0.sub _ (hX0.1.tcbW (hrH h)), hX0.1.tcbW (hrH h)⟩)) a + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted + refine WP.spec_mono (WP.spec_and (WP.spec_and (wait_for_sender_blockF J.ip s a σ hI ht hkt hnb3 hnn he hk hc + (hsort e (Or.inl he)) b rights hrc hsp) p1b) p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW⟩, hbk⟩, hfoot⟩ + simp only at ea hbk hfoot hIR hW + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + -- the assembly (as XTopJointSendBlock's `JAtZ.sendBlockJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB_free hy hyt, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hAI3 : ActIdle σ' := by + refine J.act.keepE (E := fun p => p = t.bv) ⟨fun p x0 hp hx => ?_, fun p y m hp => ⟨y, F.scF hp, rfl⟩⟩ + (fun u hu => Or.inl (by rw [F.sk.rel] at hu; exact hu)) (Or.inl hact') F.sk.idle + (fun u hu e => hnsw (by rw [hu, e, hcur])) (fun h => hti (hcur.trans h.symm)) F.sk.isc + obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + rcases hs' with e1 | ⟨e1, -⟩ + · rw [hk3, e1] + · exact absurd e1 hp + have hcur3 : CurInvR σ' := fun h => absurd (hact'.symm.trans h) hsnr + have hrun3 : CurRunM σ' := Or.inl (by rw [hact']; exact hsnr) + -- the dead: the thread is not dead (runnable); the endpoint and the reply leave the dead set + have htD : ∀ q ∈ D, q.bv ≠ t.bv := J.notDead ht hkt (by rcases runnable_code hr with h | h | h <;> rw [h] <;> decide) + set D' : Set U64 := if rv = 0#u64 then D \ {e} else (D \ {e}) \ {rv} with hD' + have hD'D : ∀ q ∈ D', q ∈ D \ {e} ∧ Notif.optRef rv ≠ some q.bv := by + intro q hq + by_cases h0 : rv = 0#u64 + · rw [hD', if_pos h0] at hq + exact ⟨hq, by rw [h0, optRef_zero]; intro h; cases h⟩ + · rw [hD', if_neg h0] at hq + refine ⟨hq.1, fun h => hq.2 ?_⟩ + rw [Notif.optRef_ne h0] at h + exact (P1.eq_of_bv (Option.some.inj h)).symm + have hdead : ∀ q ∈ D', DeadP (L := L) H s' q := fun q hq' => by + obtain ⟨hq, hqr⟩ := hD'D q hq' + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ t.bv := htD q hq.1 + have hqe : q.bv ≠ e.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ?_) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF' hx hqr, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 + (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + · obtain ⟨st', hk3, hs'⟩ := F.tcbF hx + refine ⟨_, hk3, ?_, rfl, Or.inl rfl⟩ + show st' = x.tcbState + rcases hs' with e1 | ⟨e1, -⟩ + · exact e1 + · exact absurd e1 hqt + · obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D') + -- no reply names the runnable thread (`SchedValid.replyTcb`) + have hnt : ∀ d ∈ Z.R, ∀ xr, σ.kheap d.bv = some (.reply xr) → xr.replyTcb ≠ some t.bv := by + intro d _ xr hkr hrt + obtain ⟨x', hk', hs'⟩ := J.sv.replyTcb _ xr _ hkr hrt + rw [hkt] at hk'; cases hk' + rcases hs' with hs' | ⟨ep, dd, hs'⟩ <;> rw [hs'] at hr <;> cases hr + -- the reply (if any) was free, so it has null links + have hrq : ∀ d ∈ Z.R, Notif.optRef rv = some d.bv → L.mem s' (rNextA d) = 0#u64 ∧ L.mem s' (rPrevA d) = 0#u64 := by + intro d hd hdr + have h0 : rv ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hdr; cases hdr + rw [Notif.optRef_ne h0] at hdr + obtain rfl : rv = d := P1.eq_of_bv (Option.some.inj hdr) + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply rv hd + obtain ⟨-, h2, h3⟩ := J.rfree rv hd ⟨xr, hkr, by rw [hxr, (hrv h0).2, optRef_zero]⟩ + obtain ⟨e1, e2⟩ := k4 rv hd + exact ⟨by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hRR' := J.rrecv.frameR k4 F hnt hrq + have hjcov : JCovD (L := L) Z.th H Z.G D' Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + have hcv : JCovD (L := L) Z.th H Z.G D' Cs s := by + by_cases h0 : rv = 0#u64 + · rw [hD', if_pos h0]; exact J.jcov.undead hcov.1 hcov.2 + · rw [hD', if_neg h0]; exact (J.jcov.undead hcov.1 hcov.2).undead (hcovR h0).1 (hcovR h0).2 + exact hcv.same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the thread's `RelIfBound` from a runnable state) + have hnqT : NotQueued σ t.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut t.bv).ik hnqT hik + have hrib : RelIfBound σ' t.bv := Ipc.RelIfBound.of_keep (J.inv.rd t.bv xt hkt (Or.inl hr)) F.skeep + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D' s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have B' : JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' s' a σ' := J.cs_frameIR hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (fun p hp => by + have h1 := J.zD_diffE he p hp + have hT : H p = some .tcb := J.zH.1 p (J.thT p (J.ztcb_tcb hp)) + rw [hD'] + split_ifs with h0 + · exact h1 + · exact ⟨h1, fun e' => by rw [Set.mem_singleton_iff.1 e', J.zH.2.2.1 rv (hrv h0).1] at hT; cases hT⟩) + rw [heq] + have hK : NatK σ σ' := natK_kh (R := BChgR t.bv e.bv (optRef rv)) (by + intro p o o' h + rcases h with ⟨xt, e, d, rfl, rfl, -, -⟩ | ⟨x, y, rfl, rfl, -⟩ | ⟨x, rfl, rfl, -⟩ + · exact zvChg_tcb fun h => by cases h + · exact zvChg_ep + · exact zvChg_reply) F.kh + exact ⟨_, Set.mem_singleton _, D', B', NatFoot.ofWS hF.1 hW + (fun _ h => Or.inl (Ipc.CnW.oobj hS h)) (fun _ h => J.cnW_nv h) (fun _ h => J.cnW_nh h) hK⟩ + +set_option maxHeartbeats 16000000 in +theorem JAtZB.recvIpcBlockBoundSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hti : σ.curThread ≠ σ.idleThread) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) (hnrl : t.bv ∉ σ.releaseQueue) + {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (hr : Fl4Spec.runnable xt.tcbState = true) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) + (hscA : ∀ p y n, σ.kheap p = some (.schedContext y n) → y.scTcb = some t.bv → Fl4Spec.scActive y = true) + (hcb : P1.CanBlock st (P1.Heap.tcbs H) Ac s t) + {n : U64} (hn : n ∈ Z.N) (hbn : xt.tcbBoundNotification = some n.bv) + {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hna : Fl4Spec.isActive xn = false) (hnr : ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none)) + {e : U64} (he : e ∈ Z.E) {x : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint x)) + (hc : Endp.CanBlock true x) + (hcov : (∀ k, Z.th e = some k → Covered (L.mem s) Z.G e.val (e.val + max (objSize k) 1)) ∧ + (∀ k, H e = some k → Covered (L.mem s) Z.G e.val (e.val + 2 ^ k.bits))) + {rc : capability.Cap} {rc' : Fl4Spec.Cap} {rv : U64} (hrc : ReplyArg Z.R (L.mem s) rc rc' rv) + (hcovR : rv ≠ 0#u64 → (∀ k, Z.th rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + max (objSize k) 1)) ∧ + (∀ k, H rv = some k → Covered (L.mem s) Z.G rv.val (rv.val + 2 ^ k.bits))) + (cb : U64) (c1 c2 c3 c4 : Bool) (b : Fl4Spec.Badge) (rights : Fl4Spec.CapRights) : + endpoint.receive_ipc st arI s a t (.Endpoint e cb c1 c2 c3 c4) true rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) t.bv (.endpointCap e.bv b rights) true rc' σ).1, + ∃ D', JAtZRB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst D' r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hns : ∀ q, x ≠ .sendEP q := fun q h => by subst h; simp [Endp.CanBlock] at hc + have hx := hI.1.toE.at_ep he hk + have hst2 : stOf (L.mem s) e = 0#u64 ∨ stOf (L.mem s) e = 2#u64 := by + cases x with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hxt := hI.1.toE.at_tcb ht hkt + have hb1 : optRef (L.mem s (tBoundA t)) = some n.bv := hxt.bound.symm.trans hbn + have hbz : L.mem s (tBoundA t) ≠ 0#u64 := fun e => by rw [e, optRef_zero] at hb1; cases hb1 + have hbound : L.mem s (tBoundA t) = n := by + rw [Notif.optRef_ne hbz] at hb1; exact P1.eq_of_bv (Option.some.inj hb1) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b0 => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hwn : L.mem s (nScA n) = 0#u64 ∨ L.mem s (nScA n) ≠ L.mem s (P1.scA t) := by + have hsN : xn.ntfnSc = optRef (L.mem s (nScA n)) := hxn.sc + obtain ⟨Q0, R0, -, hs0⟩ := J.xtop.fin.1.1.1.lists + obtain ⟨xt0, hkt0, -, -, hsct, -⟩ := hs0.tcb t ht + rw [hkt] at hkt0; cases hkt0 + by_cases h0 : L.mem s (nScA n) = 0#u64 + · exact Or.inl h0 + · refine Or.inr fun he' => hnr ⟨?_, ?_⟩ + · rw [hsN, he']; exact hsct.symm + · rw [hsN, Notif.optRef_ne h0]; simp + exact receive_ipc_blockN_red hrc hbound (by rw [← hbound]; exact hbz) hstN hwn hst2 + (J.recvIpcWaitSB ht hcur hti hnq hnrl hkt hr hsw hnsw hscA hcb he hk hc hcov hrc hcovR b rights + (receiveIpc_boundNA b rights true (replyArg_spec J.ip.e hI.1.toE hrc) hkt hbn hk hns hkn hna (fun _ => hnr))) + +set_option maxHeartbeats 8000000 in +theorem JAtZB.stsCurSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hti : σ.curThread ≠ σ.idleThread) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) (hnsw : σ.schedulerAction ≠ .switchThread σ.curThread) {t : U64} (ht : t ∈ Z.T) + (hnr : t.bv ∉ σ.releaseQueue) (htD : t ∉ D) {x : Fl4Spec.Tcb} (hk : σ.kheap t.bv = some (.tcb x)) (hcur : σ.curThread = t.bv) + (hrn : Fl4Spec.runnable x.tcbState = true) (hnq : ∀ d p, t.bv ∉ σ.readyQueues d p) + (hB : sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s' => P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H + (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' ∧ + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s' ⦄) : + sched.set_thread_state st s t sched.TS_INACTIVE ⦃ s6 => ∃ (Lst' : U64 → List U64), + JAtZRB (L := L) arI AR Z H Cs Ac Lst' D s6 a + (stsAct (updK σ t.bv (.tcb { x with tcbState := .inactive })) t.bv) ∧ + P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) ⦄ := by + have hsnr : σ.schedulerAction ≠ .resumeCurThread := by + obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e + have hts : tsCode Fl4Spec.ThreadState.inactive < 3 := by decide + have hcode := runnable_code hrn + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + obtain ⟨x0, hk0, hTR⟩ := hI.1.tcb t ht + rw [hk] at hk0; cases hk0 + have hst : (L.mem s (tStA t)).val = tsCode x.tcbState := hTR.st + have hq := IpcRel.not_ep_queued hI.1 (t := t) ⟨by rw [hst]; omega, by rw [hst]; omega⟩ + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP0.e + have hSR := IpcRel.toSRN hI.1 hI.2 + refine WP.spec_mono (WP.spec_and (WP.spec_and (WP.spec_and (set_thread_state_c hH hP0 hP0.e s σ hI.1.toE hSR ht + hk hq (by omega) .inactive hts sched.TS_INACTIVE (by unfold sched.TS_INACTIVE; rfl)) + (set_thread_state_sfoot hS ht _)) (set_thread_state_yw hS ht _)) hB) ?_ + rintro s6 ⟨⟨⟨⟨hR6, hS6r⟩, hw6⟩, hy6⟩, hblk, hfoot⟩ + have hX6 := IpcRel.ofSRN hR6 hS6r + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + set x6 : Fl4Spec.Tcb := { x with tcbState := .inactive } with hx6 + set σ1 := updK σ t.bv (.tcb x6) with hσ1 + set σ6 := stsAct σ1 t.bv with hσ6 + have hrec : σ6 = { σ1 with schedulerAction := σ6.schedulerAction } := stsAct_rec σ1 t.bv + have hk1 : σ1.kheap t.bv = some (.tcb x6) := by simp [hσ1, updK] + have hk6 : σ6.kheap t.bv = some (.tcb x6) := by rw [hσ6, stsAct_kheap]; exact hk1 + have hkp : ∀ p, p ≠ t.bv → σ6.kheap p = σ.kheap p := by + intro p hp; rw [hσ6, stsAct_kheap, hσ1]; simp [updK, hp] + have hact6 : σ6.schedulerAction = σ.schedulerAction := by + rw [hσ6]; unfold stsAct + rw [if_neg (fun h' => hsnr h'.2.1)] + rfl + have e_ct : σ6.curThread = σ.curThread := by rw [hrec]; rfl + have e_csc : σ6.curSc = σ.curSc := by rw [hrec]; rfl + have e_cons : σ6.consumedTime = σ.consumedTime := by rw [hrec]; rfl + have e_tm : σ6.curTime = σ.curTime := by rw [hrec]; rfl + have e_idle : σ6.idleThread = σ.idleThread := by rw [hrec]; rfl + have e_isc : σ6.idleSc = σ.idleSc := by rw [hrec]; rfl + have e_rel : σ6.releaseQueue = σ.releaseQueue := by rw [hrec]; rfl + have hW : P1.WritesIn st s s6 (TFoot st Z.T Z.N Z.C Z.c s σ) := WritesIn.tfoot_sfoot (P1.WritesIn.refl _ _) hw6 + have hfx : IpcFree x.tcbState := runnable_free hrn + have hK : ScTKw (fun p => p = t.bv) σ σ6 := by + refine ⟨fun p => ?_, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, + by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, by rw [hrec]; rfl, e_ct, e_isc⟩ + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show TKObj _ _ _ + exact Or.inr (Or.inl ⟨x, x6, rfl, rfl, rfl, Or.inr ⟨rfl, hfx, tsLow_free hts⟩⟩) + · rw [hkp p hp]; exact OTK.refl _ _ + have hX' : XTopAt (L := L) AR Z s6 a σ6 := xtop_sc_step J.xtop hP0 J.nk hW hK hX6.2.2.1 hX6.1.toN id + -- `Inv` + have hI1 : Inv σ1 := by + refine J.inv.step (NtfnStep.tcb hk .inactive x.tcbArch) (fun u x0 y h1 h2 hqd hr => ?_) + (fun u x0 y h1 h2 hc => ?_) + · by_cases e : u = t.bv + · subst e + rcases hqd with ⟨d, p, hm⟩ | hm + · exact absurd hm (hnq d p) + · exact absurd hm hnr + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + rw [h1] at h2'; cases h2'; exact hr + · by_cases e : u = t.bv + · subst e; simp [hσ1, updK] at h2; subst h2 + exact absurd hc (by intro h; rcases h with h | ⟨_, h⟩ | ⟨_, _, h, -⟩ | ⟨_, _, _, h⟩ <;> cases h) + · have h2' : σ.kheap u = some (.tcb y) := by simpa [hσ1, updK, e] using h2 + exact J.inv.rd u y h2' hc + have hInv : Inv σ6 := hI1.qchange (qchange_stsAct σ1 t.bv) + -- the yield words + have hY6 : YieldRel Z.T Z.C (L.mem s6) σ6 := by + have h1 := J.yld.frame (m' := L.mem s6) (σ' := σ1) + (fun y hy => hy6.mem_eq (yieldA_aligned hT hy) fun h => h ⟨y, hy, rfl⟩) + (YK.YKS.updTcb (y := x6) hk rfl).ykeep + exact h1.frame (m' := L.mem s6) (σ' := σ6) (fun _ _ => rfl) (YKeep.of_kheap (stsAct_kheap _ _)) + have hYK6 : YKeep σ σ6 := (YK.YKS.updTcb (y := x6) hk rfl).ykeep.trans (YKeep.of_kheap (stsAct_kheap _ _)) + have hsp : Fl4Spec.setThreadState (Z := Unit) t.bv .inactive σ = ({((), σ6)}, False) := + Notif.setThreadState_eq .inactive hk + have hsok : SOK (false, true) σ σ6 := Pres.of_eq (pres_setThreadState _ _ _) hsp + have hNSC : NSC Z.N Z.C σ6 := by + intro n hn y p hy hp + by_cases e : n.bv = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.nsc n hn y p hy hp + obtain ⟨-, hY', hM', ⟨Lst', hCR', hRF'⟩, hRP', -⟩ := + ScAll.of_tr hS J.rp J.mcp J.call J.rfree ⟨hX6.2.2.1, hX6.1.toN, hNSC, hInv⟩ hY6 (hW.mono fun _ h => Or.inl h) + hsok rfl + -- the scheduler facts + have hidt : σ.idleThread ≠ t.bv := fun e => hti (by rw [hcur, e]) + have hAI : ActIdle σ6 := by + refine J.act.keepE (E := fun p => p = t.bv) ⟨fun p x0 hp hx => by rw [hkp p hp]; exact hx, + fun p y m hp => ⟨y, by rw [hkp p (fun e => by rw [e, hk] at hp; cases hp)]; exact hp, rfl⟩⟩ + (fun u hu => Or.inl (by rw [e_rel] at hu; exact hu)) (Or.inl hact6) e_idle + (fun u hu e => hnsw (by rw [hu, e, hcur])) hidt e_isc + have hoff : OffOK σ6 := by + intro h0 y n hy ha + rw [e_cons] at h0 ⊢; rw [e_tm]; rw [e_csc] at hy; rw [e_csc, e_isc] at ha + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact J.off h0 y n hy ha + have hcur6 : CurInvR σ6 := fun h => absurd (hact6.symm.trans h) hsnr + have hown6 : CurOwnW Z.T Z.C σ6 := by + have hsc6 : ∀ y n, σ6.kheap σ6.curSc = some (.schedContext y n) → σ.kheap σ.curSc = some (.schedContext y n) := by + intro y n hy + rw [e_csc] at hy + by_cases e : σ.curSc = t.bv + · rw [e, hk6] at hy; cases hy + · rw [hkp _ e] at hy; exact hy + rcases J.own with ⟨u, hu, xt, hkt, hxs, hown, hnr'⟩ | ⟨hcs, hun⟩ + · left + by_cases hut : u.bv = t.bv + · rw [hut, hk] at hkt; cases hkt + exact ⟨u, hu, x6, by rw [hut]; exact hk6, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr' (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact ⟨u, hu, xt, by rw [hkp _ hut]; exact hkt, by rw [e_csc]; exact hxs, fun y n hy => hown y n (hsc6 y n hy), + fun h0 hm => hnr' (by rw [← e_cons]; exact h0) (by rw [e_rel] at hm; exact hm)⟩ + · exact Or.inr ⟨by rw [e_csc]; exact hcs, fun y n hy => hun y n (hsc6 y n hy)⟩ + have hcr6 : CurRunM σ6 := Or.inl (by rw [hact6]; exact hsnr) + have hlk : LK (fun _ => False) σ σ6 := fun p => Or.inr (by + by_cases hp : p = t.bv + · subst hp; rw [hk, hk6] + show LObj _ _ + refine ⟨?_, ?_, ?_⟩ + · intro y ey; cases ey; exact ⟨_, rfl, Or.inl rfl⟩ + · intro y n ey; cases ey + · intro y ey; cases ey + · rw [hkp p hp]; exact OL.refl _) + have hfk : FK σ σ6 := ⟨e_csc, e_idle, e_isc⟩ + have hdead : ∀ q ∈ D, DeadP (L := L) H s6 q := fun q hq => + J.deadP_sc_stepQ hX' hW hK (fun _ hp => absurd (show q = t from P1.eq_of_bv hp) (fun e => htD (e ▸ hq))) + hlk hfk hAI.1 hY' (hYK6.dk q.bv) (fun h => h) (J.jcov.dead q hq) + exact ⟨Lst', J.sc_frameR hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).2) (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2) hKd subset_rfl) (J.sc_kern hW hK hfk hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hNSC hInv hcur6 hown6 hoff hAI (BlockedScActive.tkw J.bsa hK fun p y n hp ha => by + by_cases e : p = t.bv + · rw [e, hk6] at hp; cases hp + · rw [hkp _ e] at hp; exact ⟨y, hp, ha, fun _ h => h, id⟩) hcr6 hblk hKd (J.idleW_keep hX'.fin.1.1.1 hfk.2.1 hfk.2.2).1 J.zD, hW⟩ + +set_option maxHeartbeats 8000000 in +theorem JAtZB.noFaultSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hnull : capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok .Null) + (f : error.Fault) (f' : Fl4Spec.Fault) : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtNR (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x (by rw [hcur]; exact hk) + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have e1 : c1 = .Null := by rw [hnull] at hc1; simpa using hc1.symm + subst e1 + have hfh : x.tcbFaultHandler = .nullCap := by rw [← hcr.2]; rfl + -- the current thread among the TCBs + have hS := J.xtop.fin.1.1.1 + obtain ⟨Q, R, hc, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hc.cur] at h; exact h + have hT : ct ∈ Z.T := by rw [hct']; exact hc.curT + have hD : ct ∉ D := fun h => J.notDead hT hk (by have := runnable_code hrn; omega) ct h rfl + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + have hnr : ct.bv ∉ σ.releaseQueue := (J.midBlkFactsS hT hcur hF hsw hk).1 + obtain ⟨bq, br⟩ := J.curBits hT hnq' hnr + obtain ⟨hna, -⟩ := J.words_of_spec hT (fun u hu e => hF.nsw (by rw [hu, e, hcur])) + (fun e => hF.ni (by rw [hcur, e])) + obtain ⟨x0, hk0, hTR⟩ := (J.xtop.irelK J.ks J.buf).1.tcb ct hT + rw [hk] at hk0; cases hk0 + have hv : (L.mem s (P1.stA ct)).val = tsCode x.tcbState := by + rw [P1.stA_eq, ← tStA_eq]; exact hTR.st + obtain ⟨hnb, hnrp⟩ := P1.run_word (w := L.mem s (P1.stA ct)) (by rw [hv]; exact runnable_code hrn) + have hB := P1.sched.set_thread_state.inactive_blkR (Es := P1.EH H) s ct J.blk (J.zH.1 ct hT) hnb hnrp bq br hna + have hspec := handleFault_null hk hfh f' + unfold faults.handle_fault + rw [tcb_slot_fault] + apply ok_bind_spec + rw [slot_cap_eq, hnull] + apply ok_bind_spec + step with Fl4Proofs.P1.obj.Tcb.sc.spec as ⟨i, _hi⟩ + unfold faults.send_fault_ipc + dsimp only + simp only [bind_ok, Bool.false_eq_true, ↓reduceIte] + refine WP.spec_bind (J.stsCurSB hF.ni hsw hF.nsw hT hnr hD hk hcur hrn hnq' hB) ?_ + rintro s6 ⟨Lst', J6, hW⟩ + refine (WP.spec_ok _).2 ⟨_, by rw [hspec]; exact Set.mem_singleton _, + ⟨Z.th, Z.G, J6.xtop.rel.1, J6.xtop.rel.2.1, J6.xtop.rel.2.2.1, J6.xtop.rel.2.2.2.1, J6.xtop.rel.2.2.2.2.1⟩, + JAtNR.of J6 (NatFoot.ofT J.xtop hW (fun b hb => J.tfoot_nh hb) + (natK_tcbW hk (Notif.stsAct_kheap _ _) fun h => by simp at h))⟩ + +set_option maxHeartbeats 64000000 in +theorem JAtZB.faultBlockSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) {ct : U64} + (hct : sched.cur_thread st s = ok ct) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ptr bdg : U64} {sd rv g gr : Bool} + (hep : capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok (.Endpoint ptr bdg sd rv g gr)) + (hcb : ∀ xe, σ.kheap ptr.bv = some (.endpoint xe) → Endp.CanBlock false xe) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtNR (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + have hnsw := hF.nsw + have hsnr : σ.schedulerAction ≠ .resumeCurThread := by + obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hrn : Fl4Spec.runnable x.tcbState = true := hF.run x (by rw [hcur]; exact hk) + have hP0 := J.ip + have hI := J.xtop.irelK J.ks J.buf + have hS := J.xtop.fin.1.1.1 + have hT : TcbPlaced Z.T := hP0.e.tcb + have hTH : P1.TcbPlaced (P1.Heap.tcbs H) := by rw [← J.tcbs_eq.1]; exact hP0.e.tcb + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨Q, R, hcq, hs⟩ := hS.lists + have hct' : ct = Sched.nodeW st s Z.c layout.node.CUR_THREAD := by + have h := sched.cur_thread.spec' (L := L) s + rw [hct, WP.spec_ok, hcq.cur] at h; exact h + have ht : ct ∈ Z.T := by rw [hct']; exact hcq.curT + have htH : ct ∈ P1.Heap.tcbs H := J.zH.1 ct ht + have htD : ct ∉ D := fun h => J.notDead ht hk (by have := runnable_code hrn; omega) ct h rfl + have hnq' : ∀ d p, ct.bv ∉ σ.readyQueues d p := by rw [← hcur]; exact hnq + obtain ⟨hnr, hscAm, hcbm⟩ := J.midBlkFactsS ht hcur hF hsw hk + have hti : σ.curThread ≠ σ.idleThread := hF.ni + have hidt : σ.idleThread ≠ ct.bv := fun e => hti (by rw [hcur, e]) + -- the handler cap, its endpoint + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have e1 : c1 = .Endpoint ptr bdg sd rv g gr := by rw [hep] at hc1; simpa using hc1.symm + subst e1 + have habs := hcr.2 + simp only [absCap] at habs + have hfh : x.tcbFaultHandler = .endpointCap ptr.bv bdg.bv (rightsOf sd rv g gr) := habs.symm + have hAS : AbsSlot Z.th (ct.bv, Fl4Spec.tcbCnodeIndex 3) := ⟨ct, rfl, Or.inr ⟨htcb, 3, by decide, rfl⟩⟩ + have hpE : ptr ∈ Z.E := J.slotE hAS hep + obtain ⟨xe, hke, -⟩ := hI.1.ep ptr hpE + have hcbe : Endp.CanBlock false xe := hcb xe hke + have hpt : ptr.bv ≠ ct.bv := fun e => by rw [e, hk] at hke; cases hke + have hHe : H ptr = some .ep := J.zH.2.1 ptr hpE + have hfit := J.kern.heap.fits ptr _ hHe + simp only [P1.HKind.bits] at hfit + have hcov := J.cover_cap (P1.WritesIn.refl s _) hAS.isSlot hep (fun h => by cases h) (ok_inj (phys_eq _).symm) rfl + (range_ep ptr bdg sd rv g gr hfit) fun k hk' => by + rw [hHe] at hk'; cases hk' + exact ⟨by simp only [P1.HKind.bits]; omega, fun z h => by cases h⟩ + have hcbP := hcbm + have hdon := J.faultDonWord ht hk + refine handleFault_ep_wp hep ?_ + unfold faults.send_fault_ipc + dsimp only + -- `set_fault` + set x2 : Fl4Spec.Tcb := { x with tcbFault := some f' } with hx2 + set σf := updK σ ct.bv (.tcb x2) with hσf + refine WP.spec_bind (WP.spec_and (set_fault_G_top hP0 hI ht hk hf) + (WP.spec_and (P1.faults.set_fault.blk (U := ∅) s ct f (t0 := ct) J.blk htH + (fun _ h => by simp at h) hcbP) + (P1.faults.set_fault.kw (Cs := Cs) (Sg := P1.Heap.frame H) (Scs := P1.Heap.scs H) (Z := P1.Heap.scBits H) + (Es := P1.EH H) s ct f hTH htH))) ?_ + rintro s7 ⟨⟨⟨-, hI7⟩, hw7⟩, ⟨hblk7, -, hcb7⟩, hfoot7⟩ + have hkf : σf.kheap ct.bv = some (.tcb x2) := by simp [hσf, updK] + have hkfp : ∀ q, q ≠ ct.bv → σf.kheap q = σ.kheap q := by + intro q hq'; rw [hσf]; simp only [updK, Function.update_of_ne hq'] + have hkef : σf.kheap ptr.bv = some (.endpoint xe) := by rw [hkfp _ hpt]; exact hke + -- the FL4 step of `set_fault`: the seven words + have hC2 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ s7 σf := by + refine ⟨?_, SzKeepC.of (SzKeep.of_tcb (p := ct.bv) (x' := x2) (fun q hq' => by simp [hσf, updK, hq']) hkf)⟩ + refine hw7.mono fun w0 hw0 => ?_ + obtain ⟨K, k1, k2, k6⟩ := hw0 + exact Or.inl (Or.inr ⟨ct, ht, K, by omega, by omega, by omega, by omega, ⟨by omega, by omega⟩, k6⟩) + -- the spec's blocking send at `σf` + have hprf : ∀ u, Sched.prioOf σf u = Sched.prioOf σ u := by + intro u + by_cases hut : u = ct.bv + · subst hut; exact (prioOf_eq hkf).trans (prioOf_eq hk).symm + · unfold Sched.prioOf; rw [tcbsOf_eq, tcbsOf_eq, hkfp u hut] + have hnb0 : tsCode x.tcbState ≠ 3 ∧ tsCode x.tcbState ≠ 4 := by + have := runnable_code hrn; omega + have htq : ct.bv ∉ xe.epQueue := Endp.not_queued hI.1.toE ht hk hnb0 ptr hpE xe hke + have hqm : ∀ u ∈ xe.epQueue, ∃ z, σf.kheap u = some (.tcb z) := fun u hu => by + obtain ⟨z, hz, -⟩ := (J.sv.epQ _ _ hke).2 u hu + exact ⟨z, by rw [hkfp u (fun e => htq (e ▸ hu))]; exact hz⟩ + obtain ⟨σ', heq, F0, hik0, ⟨ds, hkt'⟩, -, -, -, hsrt, hactK⟩ := + sendIpcBlock_eqM hkf hkef hcbe htq hqm hrn ((J.isorted.1 _ _ hke).congr fun u _ => hprf u) + false bdg.bv g gr (L.mem s (scA ct) != 0#u64) + -- FL4's blocking send + have hIS : IpcSorted σf := QPK.QPS.sorted (QPK.qps_upd (ko := .tcb x2) hk rfl) J.isorted + obtain ⟨⟨Q0, R0, h0⟩, hB0', hX0, hS0, hSg0⟩ := hblk7 + have hst0 : L.epState s7 ptr = 0#u64 ∨ L.epState s7 ptr = 1#u64 := by + have hx := hI7.1.toE.at_ep hpE hkef + rw [epState_eq] + cases xe with + | idleEP => exact Or.inl hx.1 + | sendEP q => exact Or.inr hx.1 + | recvEP q => simp [Endp.CanBlock] at hcbe + have hnn : ∀ m, x2.tcbState ≠ .blockedOnNotification m := fun m h => by + have h' : tsCode x.tcbState = 6 := congrArg tsCode h + have := runnable_code hrn; omega + have p1f : endpoint.send_ipc st arI s7 a true false bdg g gr (L.mem s (scA ct) != 0#u64) ct ptr ⦃ s' _ => + P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s7 s' ⦄ := + P1.endpoint.send_ipc.blockFoot arI s7 a true false bdg g gr _ ct ptr h0 (fun _ => hcb7) hHe hst0 + refine WP.spec_bind (WP.spec_and (WP.spec_and (send_ipc_blockF (ar := arI) hP0 s7 a σf hI7 ht hkf hnb0 hnn hpE hkef + hcbe (Sched.qSorted_of_spec (L := L) hI7.1.toE hI7.1.toN hT hIS ptr (Or.inl hpE)) false bdg g gr + (L.mem s (scA ct) != 0#u64)) + (P1.endpoint.send_ipc.block arI s7 a true false bdg g gr _ ct ptr Q0 R0 h0 hB0' hX0 hS0 hSg0 + (fun _ => hcb7) hHe hst0)) + p1f) ?_ + rintro ⟨s', a'⟩ ⟨⟨⟨ea, σ0, h0eq, hIR, hW3⟩, hbk⟩, hfoot3⟩ + simp only [WP.uncurry'_pair] at ea hbk hfoot3 hIR hW3 + subst a' + obtain rfl : σ' = σ0 := by + have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 + exact (congrArg Prod.snd (Set.singleton_eq_singleton_iff.1 e1)).symm + apply (WP.spec_ok _).2 + refine ⟨rfl, ?_⟩ + -- the spec's outcome + have hz : ((), σ') ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv + (decide (Fl4Spec.Rights.allowGrant ∈ rightsOf sd rv g gr)) + (decide (Fl4Spec.Rights.allowGrantReply ∈ rightsOf sd rv g gr)) (decide (x.tcbSchedContext ≠ none)) ct.bv + ptr.bv (updK σ ct.bv (.tcb { x with tcbFault := some f' }))).1 := by + rw [rights_grant, rights_grantReply, ← hdon] + show ((), σ') ∈ (Fl4Spec.sendIpc (Z := Unit) true false bdg.bv g gr _ ct.bv ptr.bv σf).1 + rw [heq]; exact Set.mem_singleton _ + refine ⟨((), σ'), handleFault_ep_mem hk hfh _ hz, ?_⟩ + -- the frame from the start + have hkt3 : σ'.kheap ct.bv = + some (.tcb { x with tcbState := .blockedOnSend ptr.bv ds, tcbFault := some f' }) := hkt' + have hsk : Sk σ σ' := by + have h2 : Sk σ σf := ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩ + exact h2.trans F0.sk + have F : BFrT ct.bv ptr.bv σ σ' := by + refine ⟨hsk, fun q => ?_⟩ + by_cases hqt : q = ct.bv + · subst hqt + exact Or.inr ⟨_, _, hk, hkt3, Or.inl ⟨x, ptr.bv, ds, some f', rfl, rfl, rfl⟩⟩ + · rcases F0.kh q with e | ⟨o, o', h1, h2, hc⟩ + · exact Or.inl (e.trans (hkfp q hqt)) + · rw [hkfp q hqt] at h1 + rcases hc with ⟨_, _, _, _, _, e3, -⟩ | ⟨x1, y1, rfl, rfl, e3⟩ + · exact absurd e3 hqt + · exact Or.inr ⟨_, _, h1, h2, Or.inr ⟨x1, y1, rfl, rfl, e3⟩⟩ + -- the FL4 footprint from the start + have hC3 : Ipc.CnStep (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s7 σf s' σ' := ⟨hW3, SzKeepC.of F0.szKeep⟩ + have hW : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ) := (hC2.trans hC3).1 + have hfoot := P1.Foot.trans hfoot7 hfoot3 + have hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s' := ⟨⟨Q0, R0, hbk.1⟩, hbk.2⟩ + -- the assembly (as XTopJointSendBlockG's `JAtZ.runTmoJ`) + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + have hF : IFrame (L := L) Z.th s s' := IRel.iframe_of hI hW fun b hb => CnW.oobj hI.2.2.1 hb + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 F.sk.cdt F.sk.orig F.ksub hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hW + have ck := F.callKeep + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + obtain ⟨e1, e2⟩ := k4 d hd + obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨y, F.replyB hy, hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + -- the scheduler: the current thread blocked, a reschedule pending + have hact' : σ'.schedulerAction = σ.schedulerAction := hactK (by show σf.schedulerAction ≠ _; exact hsnr) + have hAI3 : ActIdle σ' := by + refine J.act.keepE (E := fun p => p = ct.bv) ⟨fun p x0 hp hx => F.tcbO hp hx, fun p y m hp => ⟨y, F.scF hp, rfl⟩⟩ + (fun u hu => Or.inl (by rw [F.sk.rel] at hu; exact hu)) (Or.inl hact') F.sk.idle + (fun u hu e => hnsw (by rw [hu, e, hcur])) hidt F.sk.isc + have hcur3 : CurInvR σ' := fun h => absurd (hact'.symm.trans h) hsnr + have hrun3 : CurRunM σ' := Or.inl (by rw [hact']; exact hsnr) + -- the dead: the thread is not dead; the endpoint leaves the dead set + have hdead : ∀ q ∈ D \ {ptr}, DeadP (L := L) H s' q := fun q hq => by + have hd := J.jcov.dead q hq.1 + have hqt : q.bv ≠ ct.bv := fun h => htD (by rw [← P1.eq_of_bv h]; exact hq.1) + have hqe : q.bv ≠ ptr.bv := fun h => hq.2 (P1.eq_of_bv h) + refine J.deadP_keep hS3 hIR.1 (fun x hx h0 => ⟨_, F.tcbO hqt hx, rfl, rfl, Or.inl rfl⟩) (fun x hx => ?_) + (fun x hx => ⟨x, F.ntfnF hx, Or.inl rfl, rfl, Or.inl rfl⟩) + (fun x hx => ⟨x, F.replyF hx, rfl⟩) (fun x n hx => ⟨x, F.scF hx, Or.inl rfl, Or.inl rfl⟩) + (fun hqC => k3 q hqC) (fun z hz hqC K hK8 hK => J.cnW_scOut hW hz hqC hK8 hK) F.sk.idle hAI3.1 + (J.yld.frame k2 F.ykeep) (F.ykeep.dk q.bv) hd + obtain ⟨y, hy, hc'⟩ := F.epF hx + rcases hc' with rfl | e1 + · exact hy + · exact absurd e1 hqe + have hidl := J.idleW_keep hS3 F.sk.idle F.sk.isc + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => F.szKeep q.bv y n hq) hks (J.cnW_irq hW) hidl.2⟩ + have hkern := J.kern_keep hks hW.2.1 hS3 F.sk.ct F.sk.csc (fun sl h => Or.inl (by rw [← F.sk.orig]; exact h)) + (by rw [F.sk.arch]) + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) hDbg + (D' := D \ {ptr}) + have hRR' := J.rrecv.frame k4 F.khrb + have hjcov : JCovD (L := L) Z.th H Z.G (D \ {ptr}) Cs s' := by + obtain ⟨-, ⟨-, -, hM, -, -⟩, -⟩ := J.xtop.rel + have hsl : ∀ x ∈ Z.G.chains.flatten, IsSlot Z.th x := fun x h => ((hM.members x).1 h).1 + exact (J.jcov.undead hcov.1 hcov.2).same (fun y hy _ _ => ⟨hy, hF.1.capV (hsl y hy)⟩) + (fun q hq _ => hdead q hq) + (J.jcov.ks.keep hks fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) hidl hks + (J.jcov.idleOut.keep hidl fun x hx p sb fi d hc => ⟨x, hx, fi, by rw [← hF.1.capV (hsl x hx)]; exact hc⟩) + (fun q _ h => h.keep (DXKeep.of_dbg hKd (J.cnW_td hW))) + -- `Inv` (`InvBut.ik`; the blocked sender's `RelIfBound` from the runnable thread's) + have hts6 : Fl4Spec.threadSet (Z := Unit) (fun tcb => { tcb with tcbFault := some f' }) ct.bv σ = + ({((), σf)}, False) := Notif.threadSet_eq _ hk + have hik : IK ct.bv σ σ' := + ((ikm_threadSet (t := ct.bv) (p := ct.bv) (g := fun tcb => { tcb with tcbFault := some f' }) + fun _ => ⟨rfl, fun h => absurd rfl h, fun h => absurd rfl h⟩).of_eq hts6).trans hik0 + have hnqT : NotQueued σ ct.bv := ⟨hnq', hnr⟩ + obtain ⟨hIB, -⟩ := (J.inv.toBut ct.bv).ik hnqT hik + have hskp : Ipc.SKeep σ σ' := ⟨F.sk.rel, F.sk.tm, fun p y hy => by + obtain ⟨x', hx', e, -⟩ := F.tcbScB hy; exact ⟨x', hx', e.symm⟩, fun p y n hy => F.scB hy⟩ + have hrib : RelIfBound σ' ct.bv := Ipc.RelIfBound.of_keep (J.inv.rd ct.bv x hk (Or.inl hrn)) hskp + have I3 : Inv σ' := hIB.inv fun _ _ _ => hrib + have hscA := hscAm + have R : JRestR (L := L) arI (Z.mv Z.G V') H Cs Ac Lst (D \ {ptr}) s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 F.ykeep, J.mcp.frame k1 F.mcpKeep, J.buf.frame kb F.bufKeep, hRP', + J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, + J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, + hAI3, F.bsa J.bsa hscA, F.isorted J.isorted fun y1 hy1 => (hsrt y1 hy1).congr fun u _ => (hprf u).symm, + hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 F.ksz + have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + have J' := J.cs_frameIR hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R hKd F.sk.orig hidl.1 (J.zD_diffE hpE) + exact ⟨⟨_, _, J'.xtop.rel.1, J'.xtop.rel.2.1, J'.xtop.rel.2.2.1, J'.xtop.rel.2.2.2.1, J'.xtop.rel.2.2.2.2.1⟩, + ⟨Z.vs, Ac, Lst, D \ {ptr}, J', NatFoot.ofW J.xtop hW (fun b hb => CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) + (fun b hb => J.cnW_nh hb) (natK_bfrt F)⟩⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmSwitchRTop.lean b/proofs/Fl4Proofs/P3/VmSwitchRTop.lean new file mode 100644 index 000000000..9abb9ff26 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmSwitchRTop.lean @@ -0,0 +1,407 @@ +/- +P3 (line mmuAD, Stage 3 step 3 (R layer), generated by `handoff/tools/gen_swRtop.py`): **the `VmEptState` R cells of a pending +switch**: notification block, endpoint block (no SC return), linked reply cap (cancel, then any leaf; post a parameter), and a +fault whose handler is null or an endpoint with no waiting receiver. No named premise beyond `P0.ArchTotal`. +-/ +import Fl4Proofs.P3.VmSwitchR +import Fl4Proofs.P3.VmRecvMidNtfn +import Fl4Proofs.P3.VmRecvMidTop +import Fl4Proofs.P3.VmJAtZRBRet +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} +variable {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} +variable {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +theorem VmEptState.recvIpcWaitReturnSVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHeld : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hReply : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hWait : RecvIpcBoundCase (L := L) ct blocking s σ p rc) + (hisret : ∀ xt np xn, σ.kheap ct.bv = some (.tcb xt) → xt.tcbBoundNotification = some np → + σ.kheap np = some (.notification xn) → xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he, h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he + have hb : blocking = true := hWait.1 + subst blocking + have hns : ∀ q, xe ≠ .sendEP q := hWait.2.2.1 _ hke + have hrr : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 := + hWait.2.2.2 + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hrr + have hcB : Endp.CanBlock true xe := by + cases xe with + | idleEP => trivial + | recvEP q => rfl + | sendEP q => exact absurd rfl (hns q) + obtain ⟨xt0, hkt0, -⟩ := hI.1.tcb ct ht + obtain ⟨hnrl, hscA, hcb⟩ := B.midBlkFactsS ht hcur hF hsw hkt0 + obtain ⟨hcE, hcR⟩ := B.recvCov_ofW (P1.WritesIn.refl s _) he hHeld hReply hra + let rights := rightsOf sd true g gr + obtain ⟨-, ⟨xt, np, xn, hkt, hb, hkn, hna⟩, -, -⟩ := hWait + obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + have hrun := hF.run xt (by rw [hcur]; exact hkt) + have hret := hisret xt _ xn hkt hb hkn + have hCi : CurInv σ := B.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + exact WP.spec_mono (B.recvIpcRetSB ht hcur hCi hF.ni hsw hF.nsw hnq hkt hrun hcb hn hb hkn hna hret he hke hcB hHeld hReply hra + b.bv rights) fun _ ⟨z, hz, Ac', Lst', D', B', F⟩ => ⟨z, hz, (J.ofNat (V' := Z.vs) B' F).anyX arI⟩ + +set_option maxHeartbeats 8000000 in +theorem VmEptState.recvSendingReturnSV + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct e cb r n : U64} {sd g gr : Bool} {rc : capability.Cap} {rc' : Fl4Spec.Cap} + (ht : ct ∈ Z.T) (he : e ∈ Z.E) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) (hC : CurInv σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {recv : Fl4Spec.Tcb} (hkt : σ.kheap ct.bv = some (.tcb recv)) + (hn : n ∈ Z.N) {xn : Fl4Spec.Notification} (hkn : σ.kheap n.bv = some (.notification xn)) + (hbn : recv.tcbBoundNotification = some n.bv) (hna : Fl4Spec.isActive xn = false) + (hlend : xn.ntfnSc = recv.tcbSchedContext ∧ xn.ntfnSc ≠ none) + {queue : List Fl4Spec.ObjRef} (hk : σ.kheap e.bv = some (.endpoint (.sendEP queue))) + (hra : ReplyArg Z.R (L.mem s) rc rc' r) + (hHd : JHeld (L := L) Z.th s (.Endpoint e cb sd true g gr)) + (hHr : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (afterReturn : ∀ s' σ' Ac' Lst', + L.mem s (tBoundA ct) = n → + notification.maybe_return_sched_context st s n ct = ok s' → + Fl4Spec.maybeReturnSc (Z := Unit) n.bv ct.bv σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR (Z.mv Z.G Z.vs) H Cs Ac' Lst' D s' a σ' → + RecvSenderLeaves (L := L) arI AR Z s' a σ' ct e cb sd g gr true rc rc') : + endpoint.receive_ipc st arI s a ct (.Endpoint e cb sd true g gr) true rc ⦃ out => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint e cb sd true g gr)) true rc' σ).1, + VEAnyX (L := L) arI AR out.1 out.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + have hnrl : ct.bv ∉ σ.releaseQueue := by rw [← hcur]; exact hC.notRel + have hrun := hF.run recv (by rw [hcur]; exact hkt) + have hnqT : NotQueued σ ct.bv := ⟨by rw [← hcur]; exact hnq, hnrl⟩ + obtain ⟨i0, hi0⟩ := Option.ne_none_iff_exists'.1 hlend.2 + have hxs0 : recv.tcbSchedContext = some i0 := hlend.1.symm.trans hi0 + obtain ⟨y, m, hky, -⟩ := B.inv.back ct.bv recv i0 hkt hxs0 + obtain ⟨i, hiC, rfl⟩ := B.obj.sc _ y m hky + obtain ⟨σ2, he2, kt, ki, ko, ct2, csc2, -, -, _, -, hrel, hact2, -, hnq2⟩ := + maybeReturn_shape B.inv hkn hkt hky hxs0 hi0 hnqT + have hxt := hI.1.toE.at_tcb ht hkt + have hb : optRef (L.mem s (tBoundA ct)) = some n.bv := hxt.bound.symm.trans hbn + have hb0 : L.mem s (tBoundA ct) ≠ 0#u64 := fun h => by rw [h, optRef_zero] at hb; cases hb + have hbound : L.mem s (tBoundA ct) = n := by + rw [Notif.optRef_ne hb0] at hb + exact P1.eq_of_bv (Option.some.inj hb) + have hxn := hI.1.toN.at_ntfn hn hkn + have hstN : stOf (L.mem s) n ≠ obj.NTFN_ACTIVE := by + have ho := hxn.obj + cases hxo : xn.ntfnObj with + | activeNtfn b => simp [Fl4Spec.isActive, hxo] at hna + | idleNtfn => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + | waitingNtfn q => rw [hxo] at ho; rw [ho.1, ntfn_consts.2.2]; decide + have hcb := B.canBlockMidB ht hcur hkt hF + refine receive_ipc_blockR_red hra hbound (by rw [← hbound]; exact hb0) hstN ?_ + refine WP.spec_mono (Vs.spec_eq_mp (B.maybeReturnBQ ht hn hiC hkt hkn hxs0 hi0 hnqT + ⟨hcb.nq, hcb.nr, hcb.na⟩ (fun u hu e => hF.nsw (by rw [hu, e, hcur])) + (by rw [← hcur]; exact hF.ni))) ?_ + rintro s' ⟨hconc, -, σ', he', Ac', Lst', B2, F2⟩ + rw [the_result he2 he'] at B2 F2 + have hI2 := B2.xtop.irelK B2.ks B2.buf + have het : e.bv ≠ ct.bv := fun h => by rw [h, hkt] at hk; cases hk + have hei : e.bv ≠ i.bv := fun h => by rw [h, hky] at hk; cases hk + have hk2 : σ2.kheap e.bv = some (.endpoint (.sendEP queue)) := by rw [ko _ het hei]; exact hk + have hnt : n.bv ≠ ct.bv := fun h => by rw [h, hkt] at hkn; cases hkn + have hnic : n.bv ≠ i.bv := fun h => by rw [h, hky] at hkn; cases hkn + have hkn2 : σ2.kheap n.bv = some (.notification xn) := by rw [ko _ hnt hnic]; exact hkn + have hra2 : ReplyArg Z.R (L.mem s') rc rc' r := by + rcases hra with h | ⟨bb, r', h1, h2, hrR, h0⟩ + · exact Or.inl h + refine Or.inr ⟨bb, r', h1, h2, hrR, ?_⟩ + obtain ⟨xr, hkr, hxr⟩ := hI.1.toE.reply r hrR + obtain ⟨xr2, hkr2, hxr2⟩ := hI2.1.toE.reply r hrR + have hrt : r.bv ≠ ct.bv := fun h => by rw [h, hkt] at hkr; cases hkr + have hri : r.bv ≠ i.bv := fun h => by rw [h, hky] at hkr; cases hkr + rw [ko _ hrt hri, hkr] at hkr2 + cases hkr2 + rw [h0, optRef_zero] at hxr + exact optRef_none (hxr2.symm.trans hxr) + have hnr2 : ¬ (xn.ntfnSc = (Fl4Spec.tcbSchedContextUpdate (fun _ => none) recv).tcbSchedContext ∧ + xn.ntfnSc ≠ none) := fun h => h.2 h.1 + have hno : RecvNoReturn ct true σ2 := by + intro xt hkt' + rw [kt] at hkt' + cases hkt' + exact Or.inr ⟨n.bv, xn, hbn, hkn2, hna, fun _ => hnr2⟩ + have hsp := receiveIpc_lend_eq cb.bv (rightsOf sd true g gr) (replyArg_spec B.ip.e hI.1.toE hra) + (replyArg_spec B2.ip.e hI2.1.toE hra2) hkt kt hbn hbn hk hk2 hkn hkn2 hna he2 hnr2 + rw [show absCap (.Endpoint e cb sd true g gr) = .endpointCap e.bv cb.bv (rightsOf sd true g gr) from rfl, hsp] + obtain ⟨head, sender, pl, tl, hh, l, hhead, hks, hss⟩ := B2.recvSenderHead he hk2 + rw [l] at hk2 + have hLeaf := (afterReturn s' σ2 Ac' Lst' hbound hconc he2 (J.ofNat (V' := Z.vs) B2 F2)).run + ht he hh (ct2.trans hcur) kt hrun (Or.inr (Or.inl (hact2 hcur.symm))) hnq2 (fun h => hnrl (hrel _ h)) + hhead hk2 hks hss hno hra2 (jHeld_of_same F2.ss hHd) (fun q g' h => jHeld_of_same F2.ss (hHr q g' h)) + unfold Endp.recvTail + exact recvAtSendHead true (hI2.1.toE.at_ep he hk2).1 hLeaf + +set_option maxHeartbeats 8000000 in +theorem VmEptState.recvSigReturnSVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hsc : x.ntfnSc = some σ.curSc) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + VEAnyX (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + have hr := hF.run xt hk' + have hC : CurInv σ := B.curInv (by obtain ⟨u, h⟩ := hsw; rw [h]; intro e; cases e) + obtain ⟨xc, hkc, hscc, -⟩ := hC.tcb + rw [hcur, hkt] at hkc + cases hkc + obtain ⟨i, hiC, hib⟩ := B.nsc n hn x σ.curSc hk hsc + have hx := hI.1.toN.at_ntfn hn hk + have hst : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by + have h := hx.obj + cases he : x.ntfnObj with + | idleNtfn => exact Or.inl (stOf_idle (T := Z.T) (he ▸ h) rfl) + | waitingNtfn q => exact Or.inr (stOf_waiting (T := Z.T) (he ▸ h) rfl) + | activeNtfn b => exact absurd he (hna b) + have hcb := B.canBlockMidB ht hcur hkt hF + refine receive_signal_split hst + (B.recvSigBlockPreSB hkt ht hcur hr hF.ni (by rw [← hcur]; exact hC.notRel) hsw hF.nsw + (B.blockScA hcur hC hF.ni hkt) hcb hnq hn hk hna hcov cb rt) ?_ + rintro s4 ⟨σ3, hspec, hnrw, hkt3, ⟨q, hkn3⟩, hact3, hct3, hid3, B3, F3⟩ + have htP : t ∈ P1.Heap.tcbs H := by rw [← B3.tcbs_eq.1]; exact ht + obtain ⟨⟨Q0, R0, h0⟩, -⟩ := B3.blk + refine WP.spec_mono (B3.maybeReturnBQ ht hn hiC hkt3 hkn3 (by rw [hib]; exact hscc) (by rw [hib]; exact hsc) + (B3.inv.notQueued hkt3 (by rfl)) (P1.blocked_out h0 htP hnrw) + (fun u hu e => hF.nsw (by rw [← hact3, hu, e, hcur])) (by rw [hid3, ← hcur]; exact hF.ni)) ?_ + rintro s5 ⟨-, σ5, he5, Ac', Lst', B5, F5, hcn5⟩ + exact ⟨((), σ5), by rw [hspec, he5]; exact Set.mem_singleton _, + (J.ofNat (V' := Z.vs) (B5.toB (Or.inl (hcn5 (by rw [hct3, hcur])))) (B.natTrans F3 ⟨F5.ss, F5.fw, F5.fk⟩)).anyX arI⟩ + +set_option maxHeartbeats 8000000 in +theorem VmEptState.recvSigBlockSVE (hAT : P0.ArchTotal arI) + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hnr : ∀ xt, σ.kheap t.bv = some (.tcb xt) → ¬ (x.ntfnSc = xt.tcbSchedContext ∧ x.ntfnSc ≠ none)) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + VEAnyR (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xt, hkt, -⟩ := hI.1.tcb t ht + have hk' : σ.kheap σ.curThread = some (.tcb xt) := by rw [hcur]; exact hkt + have hr := hF.run xt hk' + have hcb := B.canBlockMidB ht hcur hkt hF + obtain ⟨hnrel, hscA, -⟩ := B.midBlkFactsS ht hcur hF hsw hkt + exact WP.spec_mono (B.recvSigBlockSB hAT ht hcur hkt hr hnrel hsw hF.nsw hF.ni hscA hcb hnq hn hk hna (hnr xt hkt) hcov cb rt) + fun _ ⟨z, hz, B', F⟩ => ⟨z, hz, (J.ofNatR (V' := Z.vs) B' F).any⟩ + +set_option maxHeartbeats 16000000 in +theorem VmEptState.recvIpcWaitCaseSVE + [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + (hF : VMidF σ) (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (blocking : Bool) + (p b : U64) (sd g gr : Bool) (rc : capability.Cap) (rc' : Fl4Spec.Cap) + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + (hRc : (rc = .Null ∧ rc' = .nullCap) ∨ + (CapRel rc rc' ∧ ∃ q g', rc = .Reply q g' ∧ ∃ x, σ.kheap q.bv = some (.reply x))) + (hHeld : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hReply : ∀ q g', rc = .Reply q g' → JHeld (L := L) Z.th s rc) + (hWait : RecvIpcBlockCase (L := L) ct blocking s σ p rc ∨ RecvIpcBoundCase (L := L) ct blocking s σ p rc) + (hnoret : ∀ xt np xn, σ.kheap ct.bv = some (.tcb xt) → xt.tcbBoundNotification = some np → + σ.kheap np = some (.notification xn) → ¬ (xn.ntfnSc = xt.tcbSchedContext ∧ xn.ntfnSc ≠ none)) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking rc ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + VEAnyR (L := L) arI AR r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hI := B.xtop.irelK B.ks B.buf + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he, h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he + have hb : blocking = true := hWait.elim (fun h => h.1) (fun h => h.1) + subst blocking + have hns : ∀ q, xe ≠ .sendEP q := hWait.elim (fun h => h.2.2.1 _ hke) (fun h => h.2.2.1 _ hke) + have hrr : rc = .Null ∨ ∃ q g', rc = .Reply q g' ∧ L.mem s (Endp.rTcbA q) = 0#u64 := + hWait.elim (fun h => h.2.2.2) (fun h => h.2.2.2) + obtain ⟨rv, hra⟩ := B.checkedFreeArg hRc hrr + have hcB : Endp.CanBlock true xe := by + cases xe with + | idleEP => trivial + | recvEP q => rfl + | sendEP q => exact absurd rfl (hns q) + obtain ⟨xt0, hkt0, -⟩ := hI.1.tcb ct ht + obtain ⟨hnrl, hscA, hcb⟩ := B.midBlkFactsS ht hcur hF hsw hkt0 + obtain ⟨hcE, hcR⟩ := B.recvCov_ofW (P1.WritesIn.refl s _) he hHeld hReply hra + let rights := rightsOf sd true g gr + rcases hWait with ⟨-, hb, -, -⟩ | ⟨-, ⟨xt, np, xn, hkt, hb, hkn, hna⟩, -, -⟩ + · obtain ⟨xt, hkt, -⟩ := hI.1.tcb ct ht + have hrun := hF.run xt (by rw [hcur]; exact hkt) + have hx := hI.1.toE.at_ep he hke + have hst : stOf (L.mem s) p = 0#u64 ∨ stOf (L.mem s) p = 2#u64 := by + cases xe with + | idleEP => exact Or.inl hx.1 + | recvEP q => exact Or.inr hx.1 + | sendEP q => exact absurd rfl (hns q) + have hb0 := optRef_none ((hI.1.toE.at_tcb ht hkt).bound.symm.trans (hb xt hkt)) + have hsp := receiveIpc_noSend b.bv rights true (replyArg_spec B.ip.e hI.1.toE hra) hkt (hb xt hkt) hke hns + exact receive_ipc_block_red hra hb0 hst (WP.spec_mono + (B.recvIpcWaitSB ht hcur hF.ni hnq hnrl hkt hrun hsw hF.nsw hscA hcb he hke hcB hcE hra hcR b.bv rights hsp) + fun _ ⟨z, hz, D', B', F⟩ => ⟨z, hz, (J.ofNatR (V' := Z.vs) B' F).any⟩) + · obtain ⟨n, hn, rfl⟩ := B.obj.ntfn _ xn hkn + have hrun := hF.run xt (by rw [hcur]; exact hkt) + have hret := hnoret xt _ xn hkt hb hkn + exact WP.spec_mono (B.recvIpcBlockBoundSB ht hcur hF.ni hnq hnrl hkt hrun hsw hF.nsw hscA hcb hn hb hkn hna hret + he hke hcB hcE hra hcR b sd true g gr b.bv rights) + fun _ ⟨z, hz, D', B', F⟩ => ⟨z, hz, (J.ofNatR (V' := Z.vs) B' F).any⟩ + +set_option maxHeartbeats 8000000 in +/-- A linked reply cap: cancel first, then the leaf (post `P`). -/ +theorem VmEptState.recvIpcLinkedS + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {ct : U64} (ht : ct ∈ Z.T) (hcur : σ.curThread = ct.bv) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (blocking : Bool) (p b : U64) (sd g gr : Bool) (q : U64) (g' : Bool) + (rc' : Fl4Spec.Cap) (hcr : CapRel (.Reply q g') rc') + (hEp : ∃ x, σ.kheap p.bv = some (.endpoint x)) + {xr : Fl4Spec.Reply} (hkr : σ.kheap q.bv = some (.reply xr)) + (hHd : JHeld (L := L) Z.th s (.Endpoint p b sd true g gr)) + (hHr : JHeld (L := L) Z.th s (.Reply q g')) (hi0 : L.mem s (Endp.rTcbA q) ≠ 0#u64) + {P : S → A → Fl4Spec.State Unit → Prop} + (afterFree : ∀ i V' Ac' Lst' s' a' σ', + L.mem s (Endp.rTcbA q) = i → i ≠ 0#u64 → i ≠ ct → + ipc.cancel_ipc st arI s a i = ok (s', a') → + Fl4Spec.cancelIpc (Z := Unit) i.bv σ = ({((), σ')}, False) → + VmEptState (L := L) arI V X XL AR (Z.mv Z.G V') H Cs Ac' Lst' D s' a' σ' → + σ'.curThread = ct.bv → VMidF σ' → + (∃ u, σ'.schedulerAction = .switchThread u) → + (∀ d p, σ'.curThread ∉ σ'.readyQueues d p) → + (∃ xe, σ'.kheap p.bv = some (.endpoint xe)) → + (∃ xr', σ'.kheap q.bv = some (.reply xr') ∧ xr'.replyTcb = none) → + L.mem s' (Endp.rTcbA q) = 0#u64 → + JHeld (L := L) Z.th s' (.Endpoint p b sd true g gr) → + JHeld (L := L) Z.th s' (.Reply q g') → + endpoint.receive_ipc st arI s' a' ct (.Endpoint p b sd true g gr) blocking (.Reply q g') ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ').1, + P r.1 r.2 z.2 ⦄) : + endpoint.receive_ipc st arI s a ct (.Endpoint p b sd true g gr) blocking (.Reply q g') ⦃ r => + ∃ z ∈ (Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking rc' σ).1, + P r.1 r.2 z.2 ⦄ := by + have B := J.1.1 + have hR := (B.xtop.irelK B.ks B.buf).1.toE + obtain ⟨xc, hkc, -⟩ := (B.xtop.irelK B.ks B.buf).1.tcb ct ht + have hkc' : σ.kheap σ.curThread = some (.tcb xc) := by rw [hcur]; exact hkc + obtain ⟨q0, hq, hqe⟩ := B.obj.reply _ xr hkr + rw [Sched.bv_inj hqe] at hq + have hrt : xr.replyTcb = some (L.mem s (Endp.rTcbA q)).bv := (hR.at_reply hq hkr).trans (Notif.optRef_ne hi0) + obtain ⟨xi, hki, hsti⟩ := B.sv.replyTcb q.bv xr _ hkr hrt + obtain ⟨i, hiT, hie⟩ := B.obj.tcb _ xi hki + have hiq : L.mem s (Endp.rTcbA q) = i := (P1.eq_of_bv hie).symm + rw [hiq] at hrt hki hi0 + have hict : i ≠ ct := by + intro e + subst e + have hr := hF.run xi (by rw [hcur]; exact hki) + rcases hsti with h | ⟨_, _, h⟩ <;> rw [h] at hr <;> simp [Fl4Spec.runnable] at hr + have hne : σ.curThread ≠ i.bv := fun e => hict (P1.eq_of_bv (e.symm.trans hcur)) + refine receive_ipc_linked hiq hi0 hict ?_ + refine WP.spec_mono (Vs.spec_eq_mp (B.cancelLinkForRecv hiT hki hsti)) ?_ + rintro ⟨s1, a1⟩ ⟨hconc, ha, σ', he, hin, V', Ac', Lst', B', F⟩ + dsimp only at ha B' ⊢ + subst a1 + have hm : ((), σ') ∈ (Fl4Spec.cancelIpc (Z := Unit) i.bv σ).1 := by + rw [he] + exact Set.mem_singleton _ + obtain ⟨hct', hid', hact', hrq', -⟩ := cancelOther_keep hm hne + have hR' := (B'.xtop.irelK B'.ks B'.buf).1.toE + obtain ⟨xr', hkr', hrl⟩ := hR'.reply q hq + have hnone : xr'.replyTcb = none := cancelOther_reply B.sv B'.sv hm hkr hrt hin hkr' + have h0' : L.mem s1 (Endp.rTcbA q) = 0#u64 := optRef_none (hrl.symm.trans hnone) + refine ⟨h0', ?_⟩ + have hcr' : rc' = absCap (.Reply q g') := hcr.abs + subst rc' + have hsp : Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking + (absCap (.Reply q g')) σ = + Fl4Spec.receiveIpc (Z := Unit) ct.bv (absCap (.Endpoint p b sd true g gr)) blocking + (absCap (.Reply q g')) σ' := + receiveIpc_linked _ _ _ _ hkr hrt (fun e => hne (hcur.trans e.symm)) he hkr' hnone + rw [hsp] + obtain ⟨xe, hke⟩ := hEp + obtain ⟨e, he', h⟩ := B.obj.ep _ xe hke + rw [Sched.bv_inj h] at he' + obtain ⟨xe', hke', -⟩ := hR'.ep p he' + exact afterFree i V' Ac' Lst' s1 a σ' hiq hi0 hict hconc he (J.ofNat (V' := V') B' F) + (hct'.trans hcur) (VMidF.cancel hF B.sv hki hsti hne hkc' hm) (by rw [hact']; exact hsw) + (by rw [hct', hrq']; exact hnq) ⟨xe', hke'⟩ ⟨xr', hkr', hnone⟩ h0' + (jHeld_of_same F.ss hHd) (jHeld_of_same F.ss hHr) + +/-- **A fault with a null handler, or an endpoint handler with no waiting receiver**, at a pending switch (`JAtZB`). -/ +theorem JAtZB.faultHypSB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) {ct : U64} (hct : sched.cur_thread st s = ok ct) + (hnr : ∀ p b sd rv g gr, capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok (.Endpoint p b sd rv g gr) → + ∀ xe, σ.kheap p.bv = some (.endpoint xe) → Endp.CanBlock false xe) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ JAtNR (L := L) arI AR Z H Cs s σ r.1 r.2 z.2 ⦄ := by + obtain ⟨hcur, x, hk⟩ := J.curEq hct + have hnq := hF.nq + have htcb : Z.th ct = some .tcb := (J.xtop.curTcb hcur).1 + obtain ⟨x1, c1, hk1, hc1, hcr⟩ := fhCap_rel J.xtop.rel.2.1.1 htcb + rw [hk] at hk1; cases hk1 + have hv : Fl4Spec.validFaultHandler (absCap c1) := by + rw [hcr.2]; exact hF.vfh x (by rw [hcur]; exact hk) + by_cases hn : c1 = .Null + · subst hn + exact J.noFaultSB hF hsw hct hnq hc1 f f' + obtain ⟨p, b, sd, rv, g, gr, rfl⟩ := vfh_ep hv hn + exact J.faultBlockSB hF hsw hct hnq hc1 (hnr p b sd rv g gr hc1) hf + +/-- **The same at `VmEptState`**, ending in an R state. -/ +theorem VmEptState.handleFaultSVE [VxE Z] (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) (hF : VMidF σ) + (hsw : ∃ u, σ.schedulerAction = .switchThread u) {ct : U64} (hct : sched.cur_thread st s = ok ct) + (hnr : ∀ p b sd rv g gr, capAt (L.mem s) (cslotAddr (ct.bv, Fl4Spec.tcbCnodeIndex 3)) = ok (.Endpoint p b sd rv g gr) → + ∀ xe, σ.kheap p.bv = some (.endpoint xe) → Endp.CanBlock false xe) + {f : error.Fault} {f' : Fl4Spec.Fault} (hf : FaultCorrG f f') : + faults.handle_fault st arI s a ct f ⦃ r => ∃ z ∈ (Fl4Spec.handleFault (Z := Unit) ct.bv f' σ).1, + RelTopM (L := L) r.1 z.2 ∧ VEAnyR (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.1.1.faultHypSB hF hsw hct hnr hf) fun _ ⟨z, hz, hR, _, _, _, _, B', F⟩ => + ⟨z, hz, hR, (J.ofNatR B' F).any⟩ + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmVxLow.lean b/proofs/Fl4Proofs/P3/VmVxLow.lean new file mode 100644 index 000000000..66a4633a8 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmVxLow.lean @@ -0,0 +1,1002 @@ +import Fl4Proofs.P3.RetVmUtDec +import Fl4Proofs.P3.VmPreempt + +/-! +`…X` siblings of the merged theorems below the dispatcher family whose post hides the ghost `Z'` without `VxE Z'`: the +untyped retype cone (`VmUtPost`) and `VmEptPre.preemptPathVFk`. Each only adds `VxE Z'` to the post. The merged +statements are unchanged. Generated by `handoff/tools/gen_vxlow.py`. No new premise. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open Aeneas Aeneas.Std Result fl4_core + +/-- `VmEptState` with its `VxE` ghost fact. -/ +theorem VmEptState.withX {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} (arI : machine.Arch A) + {AR : A → Fl4Spec.Cr3 → Prop} {V X : Set U64} {XL : U64 → Nat} {Z : XSets} [VxE Z] {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) : + VxE Z ∧ VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ := ⟨inferInstance, J⟩ + +end Fl4Proofs.P3 + +-- ===== from RetVm + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +end + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +/-- `…X` sibling of `VmEptState.ofPost` (post carries `VxE`; module docstring). -/ +theorem VmEptState.ofPostX (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {nt : Nat} + {Xp : XSets → P1.Heap → Set U64 → S → Fl4Spec.State Unit → Prop} {W : Nat → Nat → U64 → Prop} + {O : Fl4Spec.ApiObjectType → Bool → Nat → Fl4Spec.ObjRef → Fl4Spec.KernelObject → Prop} + {src : Fl4Spec.CslotPtr} {rs : Bool} {pb rb : Fl4Spec.ObjRef} {t : Fl4Spec.ApiObjectType} {us : Nat} + {slots : List Fl4Spec.CslotPtr} {res : core.result.Result Unit error.Exc × S × A} + (h : UtBodyPostB (L := L) arI AR Z.th Z.G H s σ nt Xp W O (.retype src rs pb rb t us slots false) res) + (hAs : AbsSlot Z.th src) + (hFV : RetFootV (L := L) V Z.th s (W rb.toNat (rb.toNat + slots.length * 2 ^ zOf nt us))) + (hFE : RetFootE X (W rb.toNat (rb.toNat + slots.length * 2 ^ zOf nt us))) + (hty : objTypeOf nt = some t) (hn14 : nt ≠ 14) + (hob : t ≠ .untyped → ∀ q ko, O t false us q ko → Fl4Spec.objBits ko ≤ zOf nt us) : + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype src rs pb rb t us slots false)).run σ = + ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI V (RetTabs nt us rb slots.length ∪ X) (RetLvl nt us rb slots.length XL) AR Z' H' Cs' + Ac' Lst' D' res.2.1 res.2.2 σ' := by + obtain ⟨σ', hrun, hok, -, Z', H', Cs', Ac', Lst', D', B', hvx, -, -, hR⟩ := h + have R := hR _ _ _ _ _ _ _ _ rfl + obtain ⟨k, -, -, hk, -, -, -, -, -, -, -, -, -, -, -, -, -, ⟨p, sb, fi, hsrc, h1, h2⟩, -⟩ := id R + subst hk + have hcap : capAt (L.mem s) (cslotAddr src) = ok (.Untyped p sb fi false) := by rw [capAt_capV, hsrc] + have hfit := (J.1.1.utc _ p sb fi false hAs hcap rfl).pfit + have hW := J.1.1.winFree hAs hcap h1 h2 + exact ⟨σ', hrun, hok, Z', H', Cs', Ac', Lst', D', hvx, + J.ofRet B' R hFV hFE hty hn14 (J.1.1.winV J.1.2.vm hAs hcap h1 h2) (J.1.1.winX J.2 hAs hcap h1 h2) hW.2.2 hob + (by omega) (fun _ _ => hW.1) (fun _ _ => J.1.1.winKs hAs hcap h1 h2) (fun _ _ => J.1.1.winPa hAs hcap h1)⟩ + +end + +end Fl4Proofs.P3 + +-- ===== from RetVm14 + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +open VirtLoopSharp VirtRetypeLoop VirtRetypeInit VirtInitWalk VirtRetFrameV + +/-- `…X` sibling of `VmEptState.ofPost14` (post carries `VxE`; module docstring). -/ +theorem VmEptState.ofPost14X (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) + {src : Fl4Spec.CslotPtr} {rs : Bool} {pb rb : Fl4Spec.ObjRef} {us : Nat} + {slots : List Fl4Spec.CslotPtr} {res : core.result.Result Unit error.Exc × S × A} + (h : UtBodyPostB (L := L) arI AR Z.th Z.G H s σ 14 (VcpuX (L := L) V rb.toNat slots.length) + (RetWV V Z.th (P1.vHeadA st s) rb.toNat slots.length) VcpuObj + (.retype src rs pb rb (.archObject .vcpuObj) us slots false) res) + (hAs : AbsSlot Z.th src) (hvk : Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm) : + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype src rs pb rb (.archObject .vcpuObj) us slots false)).run σ = + ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI (VsI V rb.toNat slots.length) X XL AR Z' H' Cs' Ac' Lst' D' res.2.1 res.2.2 σ' := by + obtain ⟨σ', hrun, hok, -, Z', H', Cs', Ac', Lst', D', B', hvx, hX, -, hR⟩ := h + have R := hR _ _ _ _ _ _ _ _ rfl + have ez : (2 : Nat) ^ zOf 14 us = 32768 := by rw [zOf14]; norm_num + obtain ⟨k, -, -, hk, -, -, -, -, -, -, -, -, -, -, -, -, -, ⟨p, sb, fi, hsrc, h1, h2⟩, -⟩ := id R + rw [ez] at hk + subst hk + have hcap : capAt (L.mem s) (cslotAddr src) = ok (.Untyped p sb fi false) := by rw [capAt_capV, hsrc] + have hfit := (J.1.1.utc _ p sb fi false hAs hcap rfl).pfit + have hW := J.1.1.winFree hAs hcap h1 h2 + have hC := VcpuCore.ofRet14 J.1.1 J.1.2 B' R hX hvk (J.1.1.winV J.1.2.vm hAs hcap h1 h2) hW.1 hW.2.1 + (J.1.1.winKs hAs hcap h1 h2) (by omega) + have hty : objTypeOf 14 = some (.archObject .vcpuObj) := by simp [objTypeOf, Fl4Spec.archDataToObjType] + have hE := EptCore.ofRet J.1.1 J.2 R ((J.1.1.retFootE_crl J.2 J.1.2.vm hAs hcap h1 h2).union (J.1.1.retFootE_slot J.2)) + hty (by rw [ez]; exact J.1.1.winX J.2 hAs hcap h1 h2) + (fun _ q ko hko => by + unfold VcpuObj at hko; subst hko + rw [zOf14]; simp [Fl4Spec.objBits, Fl4Spec.archKobjSize]) + (by rw [ez]; omega) (fun h _ => absurd h (by norm_num)) (fun h _ => absurd h (by norm_num)) + (fun h _ => absurd h (by norm_num)) + rw [retTabs_none (Or.inl (by norm_num)), retLvl_none (Or.inl (by norm_num)), Set.empty_union] at hE + exact ⟨σ', hrun, hok, Z', H', Cs', Ac', Lst', D', hvx, ⟨B', hC⟩, hE⟩ + +end + +end + +end Fl4Proofs.P3 + +-- ===== from RetVmAll + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +/-- `…X` sibling of `VmEptState.ofPostD` (post carries `VxE`; module docstring). -/ +theorem VmEptState.ofPostDX (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s a σ) {nt : Nat} + {Xp : XSets → P1.Heap → Set U64 → S → Fl4Spec.State Unit → Prop} {W : Nat → Nat → U64 → Prop} + {O : Fl4Spec.ApiObjectType → Bool → Nat → Fl4Spec.ObjRef → Fl4Spec.KernelObject → Prop} + {src : Fl4Spec.CslotPtr} {rs : Bool} {pb rb : Fl4Spec.ObjRef} {t : Fl4Spec.ApiObjectType} {us : Nat} + {slots : List Fl4Spec.CslotPtr} {res : core.result.Result Unit error.Exc × S × A} + (h : UtBodyPostB (L := L) arI AR Z.th Z.G H s σ nt Xp W O (.retype src rs pb rb t us slots true) res) + (hAs : AbsSlot Z.th src) (hDA : DevApart (L := L) Z.th H Z.G Cs s) (hDF : DevFreeT (L.mem s) Z.G σ) + (hFV : RetFootV (L := L) V Z.th s (W rb.toNat (rb.toNat + slots.length * 2 ^ zOf nt us))) + (hFE : RetFootE X (W rb.toNat (rb.toNat + slots.length * 2 ^ zOf nt us))) + (hty : objTypeOf nt = some t) (hn13 : nt ≤ 13) + (hob : t ≠ .untyped → ∀ q ko, O t true us q ko → Fl4Spec.objBits ko ≤ zOf nt us) : + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype src rs pb rb t us slots true)).run σ = + ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI V (RetTabs nt us rb slots.length ∪ X) (RetLvl nt us rb slots.length XL) AR Z' H' Cs' + Ac' Lst' D' res.2.1 res.2.2 σ' := by + obtain ⟨σ', hrun, hok, -, Z', H', Cs', Ac', Lst', D', B', hvx, -, -, hR⟩ := h + have R := hR _ _ _ _ _ _ _ _ rfl + obtain ⟨k, -, -, hk, -, -, -, -, -, -, -, -, -, -, -, -, -, ⟨p, sb, fi, hsrc, h1, h2⟩, -⟩ := id R + subst hk + have hcap : capAt (L.mem s) (cslotAddr src) = ok (.Untyped p sb fi true) := by rw [capAt_capV, hsrc] + obtain ⟨hσ, hK, hfit⟩ := J.1.1.winDev hDA hDF hAs hcap h1 h2 + exact ⟨σ', hrun, hok, Z', H', Cs', Ac', Lst', D', hvx, + J.ofRet B' R hFV hFE hty (by omega) (J.1.2.vm.winOff hσ) (J.2.winOff hσ) hK hob (by omega) + (fun h _ => absurd h (by omega)) (fun h _ => absurd h (by omega)) (fun h _ => absurd h (by omega))⟩ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.retype` (post carries `VxE`; module docstring). -/ +theorem VmEptState.retypeX {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) {pp : U64} {rs : Nat} {idx : List Bool} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {p sb fi : U64} {dev : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev)) + {nt us dest off win : U64} {t : Fl4Spec.ApiObjectType} {r : Nat} {reset : Bool} {fiv : U64} + (hc0 : Z.c = 0#u64) + (hctx : RetCtx (L := L) Z.th s σ (pp.bv, idx) sb fi dev nt us dest off win t r reset fiv) (hfiv : fiv = fi) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) + (hRU : ∀ lo hi : Nat, p.val + fi.val ≤ lo → hi ≤ p.val + 2 ^ sb.val → + KeptRefsP3 (L := L) H (RegionD H lo hi) s ∧ DbgTimerKept (L := L) H (RegionD H lo hi) Cs s) + (hZ0 : dev = false → UtFreeZ σ (p.val + fi.val) (p.val + 2 ^ sb.val)) + (hKN : dev = false → UtNodesAt (L.ksb s) p sb) + (hDA : dev = true → DevApart (L := L) Z.th H Z.G Cs s) (hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (h14 : nt.val = 14 → P0.ArchTotal arI ∧ VirtSanEq.CapAgree arI ar ∧ VirtInitWalk.VirtCodeAgree arI ar ∧ + (Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm)) : + untyped.retype st arI s ar (cslotAddr (pp.bv, idx)) false + ⟨Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)⟩ nt us dest off win dev ⦃ res => + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype (pp.bv, idx) false p.bv + (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) t us.val (windowSlots dest r off.val win.val) + dev)).run σ = ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI + (if nt.val = 14 then VirtRetypeLoop.VsI V (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)).toNat + (windowSlots dest r off.val win.val).length else V) + (RetTabs nt.val us.val (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) + (windowSlots dest r off.val win.val).length ∪ X) + (RetLvl nt.val us.val (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) + (windowSlots dest r off.val win.val).length XL) AR Z' H' Cs' Ac' Lst' D' res.2.1 res.2.2 σ' ⦄ := by + subst fiv + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hty := hctx.ty + have hsc : nt.val = 5 → 7 ≤ us.val := fun h => by have := hctx.size.2.2.2.2; omega + have hD := defObj_bits σ hty hsc + have FVs := J.1.1.retFootV_slot J.1.2 + have FEs := J.1.1.retFootE_slot J.2 + cases dev + · -- a RAM source + by_cases e14 : nt.val = 14 + · -- the VCPU (`utVcpuBodyJ`, `ofPost14`) + obtain ⟨hA0, hcA, hkA, hvk⟩ := h14 e14 + have ht : t = .archObject .vcpuObj := by + have e := hty; rw [e14] at e; simpa [objTypeOf, Fl4Spec.archDataToObjType] using e.symm + subst ht + rw [if_pos e14, retTabs_none (nt := nt.val) (Or.inl (by omega)), retLvl_none (nt := nt.val) (Or.inl (by omega)), + Set.empty_union] + exact WP.spec_mono (J.1.1.utVcpuBodyJ J.1.2 hni hA0 hcA hkA hpp hidx hcap e14 hctx rfl hBO hRU) + fun res h => J.ofPost14X h hAs hvk + rw [if_neg e14] + have hn14 := e14 + have hZ := hZ0 rfl + have hN := hKN rfl + have hU := J.1.1.utc _ p sb fi false hAs hcap rfl + obtain ⟨h1, -, h2, -⟩ := retype_region p fi sb.val (zOf nt.val us.val) win.val (capAt_untyped_inv hcap) + (by have := hctx.size.2.1; omega) hU.pal hU.pfit hU.fiLe hctx.window.1 hctx.memory + have h2' : (Fl4Spec.alignUp (p.bv + fi.bv) (zOf nt.val us.val)).toNat + + (windowSlots dest r off.val win.val).length * 2 ^ zOf nt.val us.val ≤ p.val + 2 ^ sb.val := by + rw [show (windowSlots dest r off.val win.val).length = win.val by simp [windowSlots]]; exact h2 + have hcase : nt.val = 0 ∨ nt.val = 1 ∨ nt.val = 4 ∨ nt.val = 5 ∨ nt.val = 8 ∨ + (nt.val = 2 ∨ nt.val = 3 ∨ nt.val = 6 ∨ (ArchTab nt.val ∨ nt.val = 9 ∨ nt.val = 10 ∨ nt.val = 11)) := by + have hlt : nt.val < 19 := by + by_contra hc; rw [(objTypeOf_none _).2 (by omega)] at hty; cases hty + unfold ArchTab; omega + rcases hcase with e | e | e | e | e | e + · exact WP.spec_mono (J.1.1.utUtBodyJ hni hpp hidx hcap e hc0 hctx rfl hBO hAF hRU) fun res h => + J.ofPostX h hAs FVs FEs hty hn14 (hD false) + · exact WP.spec_mono (J.1.1.utTcbBodyJ hni hpp hidx hcap e hc0 hctx rfl hBO hAF hRU) fun res h => + J.ofPostX h hAs (J.1.1.retFootV_T J.1.2 hAs hcap h1 h2') (J.1.1.retFootE_T J.2 hAs hcap h1 h2') hty hn14 + (hD false) + · exact WP.spec_mono (J.1.1.utCnBodyJ hni hpp hidx hcap e hc0 hctx rfl hBO hAF hRU) fun res h => + J.ofPostX h hAs FVs FEs hty hn14 (hD false) + · exact WP.spec_mono (J.1.1.utScBodyJ hni hpp hidx hcap e hc0 hctx rfl hBO hAF hRU) fun res h => + J.ofPostX h hAs FVs FEs hty hn14 (hD false) + · exact WP.spec_mono (J.1.1.utPmBodyJ hni hpp hidx hcap e hc0 hctx rfl hBO hAF hRU) fun res h => + J.ofPostX h hAs (J.1.1.retFootV_W J.1.2 hAs hcap h1 h2') (J.1.1.retFootE_W J.2 hAs hcap h1 h2') hty hn14 + (by rw [e]; exact pmObj_bits) + · exact WP.spec_mono (J.1.1.utEpBodyJ hni hpp hidx hcap e hc0 hctx rfl hBO hAF hRU hZ hN) fun res h => + J.ofPostX h hAs FVs FEs hty hn14 (hD false) + · -- a device source: untypeds and frames only (`RetCtx.device`) + have hDA' := hDA rfl + have hDF' := hDF rfl + have hdv : nt.val = 0 ∨ nt.val = 9 ∨ nt.val = 10 ∨ nt.val = 11 := by + by_cases h1 : nt.val = 1 + · exfalso + have e : t = .tcbObject := by rw [h1] at hty; simpa [objTypeOf] using hty.symm + subst e + rcases hctx.device rfl with h | h + · simp [Fl4Spec.isFrameType] at h + · cases h + · obtain ⟨-, -, hF, hU, -⟩ := objTypeOf_some hty h1 us.val + rcases hctx.device rfl with h | h + · exact Or.inr (hF.1 h) + · exact Or.inl (hU.1 h) + rw [if_neg (show nt.val ≠ 14 by omega)] + rcases hdv with e | e + · exact WP.spec_mono (J.1.1.utUtDevBodyJ hni hpp hidx hcap e hc0 hctx rfl hDA' hDF' hBO hAF hRU) fun res h => + J.ofPostDX h hAs hDA' hDF' FVs FEs hty (by omega) (hD true) + · exact WP.spec_mono (J.1.1.utDevBodyJ hni hpp hidx hcap e hc0 hctx rfl hDA' hDF' hBO hAF hRU) fun res h => + J.ofPostDX h hAs hDA' hDF' FVs FEs hty (by omega) (hD true) + +end + +end + +end Fl4Proofs.P3 + +-- ===== from RetVmDec + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] + +open Classical Fl4Proofs.P3.Sched Fl4Proofs.P1 + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.retypeRG` (post carries `VxE`; module docstring). -/ +theorem VmEptState.retypeRGX {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) {ct : U64} (hct : sched.cur_thread st s = ok ct) {pp : U64} {rs : Nat} {idx : List Bool} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {p sb fi : U64} {dev : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev)) + {nt us dest off win : U64} {t : Fl4Spec.ApiObjectType} {r : Nat} {reset : Bool} {fiv : U64} (hc0 : Z.c = 0#u64) + (hctx : RetCtx (L := L) Z.th s σ (pp.bv, idx) sb fi dev nt us dest off win t r reset fiv) (hfiv : fiv = fi) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) (hRU : RegionUnrefAt (L := L) H Cs D s) + (hUZ : dev = false → UtZero (L.mem s) Z.G σ) (hUN : dev = false → UtNodesJ (L.mem s) Z.G (L.ksb s)) + (hDA : dev = true → DevApart (L := L) Z.th H Z.G Cs s) (hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (h14 : nt.val = 14 → P0.ArchTotal arI ∧ VirtSanEq.CapAgree arI ar ∧ VirtInitWalk.VirtCodeAgree arI ar ∧ + ZvVirtAgree arI ∧ ∃ v, arI.virtualization ar = ok v ∧ v ≠ 0#u64) : + (do + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st arI s2 ar (cslotAddr (pp.bv, idx)) false + ⟨Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)⟩ nt us dest off win dev) ⦃ res => + ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype (pp.bv, idx) false p.bv + (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) t us.val (windowSlots dest r off.val win.val) + dev)).run σ1 = ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI + (if nt.val = 14 then VirtRetypeLoop.VsI V (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)).toNat + (windowSlots dest r off.val win.val).length else V) + (RetTabs nt.val us.val (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) + (windowSlots dest r off.val win.val).length ∪ X) + (RetLvl nt.val us.val (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) + (windowSlots dest r off.val win.val).length XL) AR Z' H' Cs' Ac' Lst' D' res.2.1 res.2.2 σ' ⦄ := by + have hc2 : sched.cur_thread st s = ok (L.curW s) := cur_thread_eq s + have hctE : ct = L.curW s := by rw [hct] at hc2; exact eq_of_ok_eq hc2 + subst hctE + have B := J.1.1 + obtain ⟨hP, ⟨-, -, hM, -, -⟩, -⟩ := B.xtop.rel + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hxG : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hAs.isSlot, _, hcap, by simp⟩ + -- the restart (`VmEptState.restartK`) + obtain ⟨s1, σ1, Lst1, x, hcur, hk, hs1, hst1, J1, he1, hSS1, hks1, hni1⟩ := J.restartK hni + have hσ1k : ∀ o, o ≠ (L.curW s).bv → σ1.kheap o = σ.kheap o := fun o ho => by + rw [he1, Notif.stsAct_kheap, Notif.updK_kheap]; exact Function.update_of_ne ho _ _ + have hσ1c : σ1.kheap (L.curW s).bv = some (.tcb { x with tcbState := .restart }) := by + rw [he1, Notif.stsAct_kheap, Notif.updK_kheap]; simp + have hσ1cdt : σ1.cdt = σ.cdt := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hid1 : σ1.idleThread = σ.idleThread := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hcap1 : capAt (L.mem s1) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev) := + (hSS1.capAt hAs.isSlot).trans hcap + have hctx1 := hctx.restart hSS1 hσ1cdt + have hBO1 := hBO.restart hk hσ1k hσ1c + have hAF1 := hAF.restart hσ1k hσ1c + have h14' : nt.val = 14 → P0.ArchTotal arI ∧ VirtSanEq.CapAgree arI ar ∧ VirtInitWalk.VirtCodeAgree arI ar ∧ + (Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm) := fun e => by + obtain ⟨hA0, hcA, hkA, hVA, v, hv, hv0⟩ := h14 e + exact ⟨hA0, hcA, hkA, hVA.x86 hv hv0⟩ + rw [hs1]; simp only [bind_ok] + cases dev + · -- a RAM source: the region's objects of `H` dead (`free_dead`), the free part zero, the nodes outside + have hRU1 : ∀ lo hi : Nat, p.val + fi.val ≤ lo → hi ≤ p.val + 2 ^ sb.val → + KeptRefsP3 (L := L) H (RegionD H lo hi) s1 ∧ DbgTimerKept (L := L) H (RegionD H lo hi) Cs s1 := + fun lo hi h1 h2 => hRU s1 hs1 lo hi (B.free_dead hxG (capV_of_capAt hcap) h1 h2) + have hms1 : σ1.machineState = σ.machineState := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hZ1 : UtFreeZ σ1 (p.val + fi.val) (p.val + 2 ^ sb.val) := fun a h1 h2 => by + rw [hms1]; exact hUZ rfl _ hxG p sb fi (capV_of_capAt hcap) a h1 h2 + have hKN1 : UtNodesAt (L.ksb s1) p sb := by rw [hks1]; exact hUN rfl _ hxG p sb fi (capV_of_capAt hcap) + exact WP.spec_mono (J1.retypeX hni1 hpp hidx hcap1 hc0 hctx1 hfiv hBO1 hAF1 hRU1 (fun _ => hZ1) (fun _ => hKN1) + (fun h => absurd h (by simp)) (fun h => absurd h (by simp)) h14') fun res h => ⟨σ1, hst1, h⟩ + · -- a device source: the device facts after the restart, the region's free part dead (no `H` object meets it) + have hDA0 := hDA rfl + have hcvK : ∀ y ∈ Z.G.chains.flatten, capV (L.mem s1) y = capV (L.mem s) y := fun y hy => + capV_of_capAt ((hSS1.capAt ((hM.members y).1 hy).1).trans (capAt_capV _ _)) + have hidK : IdleKeep (L := L) s s1 := + B.idleW_keep J1.1.1.xtop.fin.1.1.1 hid1 (by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl) + have hRU1 : ∀ lo hi : Nat, p.val + fi.val ≤ lo → hi ≤ p.val + 2 ^ sb.val → + KeptRefsP3 (L := L) H (RegionD H lo hi) s1 ∧ DbgTimerKept (L := L) H (RegionD H lo hi) Cs s1 := by + intro lo hi h1 h2 + refine hRU s1 hs1 lo hi (fun q kd hq hr => ?_) + exfalso + unfold RegionD MeetsR at hr + rw [hq] at hr + simp only [Option.elim] at hr + exact meets_off hr h1 h2 ((hDA0 _ hxG p sb fi (capV_of_capAt hcap)).2.2.2.2.2.2.1 q kd hq |>.imp_right + (Or.imp_right fun h => ⟨h.1, h.2.1⟩)) + exact WP.spec_mono (J1.retypeX hni1 hpp hidx hcap1 hc0 hctx1 hfiv hBO1 hAF1 hRU1 (fun h => absurd h (by simp)) + (fun h => absurd h (by simp)) (fun _ => hDA0.restart hcvK hks1 hidK) + (fun _ => (hDF rfl).restart hcvK hk hσ1k hσ1c) h14') fun res h => ⟨σ1, hst1, h⟩ + +/-- `…X` sibling of `VmEptState.retypeR` (post carries `VxE`; module docstring). -/ +theorem VmEptState.retypeRX {s : S} {ar : A} {σ : Fl4Spec.State Unit} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) {ct : U64} (hct : sched.cur_thread st s = ok ct) {pp : U64} {rs : Nat} {idx : List Bool} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {p sb fi : U64} {dev : Bool} + (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev)) + {nt us dest off win : U64} {t : Fl4Spec.ApiObjectType} {r : Nat} {fiv : U64} (hc0 : Z.c = 0#u64) + (hctx : RetCtx (L := L) Z.th s σ (pp.bv, idx) sb fi dev nt us dest off win t r false fiv) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) (hRU : RegionUnrefAt (L := L) H Cs D s) + (hUZ : dev = false → UtZero (L.mem s) Z.G σ) (hUN : dev = false → UtNodesJ (L.mem s) Z.G (L.ksb s)) + (hDA : dev = true → DevApart (L := L) Z.th H Z.G Cs s) (hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (h14 : nt.val = 14 → P0.ArchTotal arI ∧ VirtSanEq.CapAgree arI ar ∧ VirtInitWalk.VirtCodeAgree arI ar ∧ + ZvVirtAgree arI ∧ ∃ v, arI.virtualization ar = ok v ∧ v ≠ 0#u64) : + (do + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st arI s2 ar (cslotAddr (pp.bv, idx)) false + ⟨Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)⟩ nt us dest off win dev) ⦃ res => + ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype (pp.bv, idx) false p.bv + (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) t us.val (windowSlots dest r off.val win.val) + dev)).run σ1 = ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI + (if nt.val = 14 then VirtRetypeLoop.VsI V (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)).toNat + (windowSlots dest r off.val win.val).length else V) + (RetTabs nt.val us.val (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) + (windowSlots dest r off.val win.val).length ∪ X) + (RetLvl nt.val us.val (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) + (windowSlots dest r off.val win.val).length XL) AR Z' H' Cs' Ac' Lst' D' res.2.1 res.2.2 σ' ⦄ := + J.retypeRGX hni hct hpp hidx hcap hc0 hctx (by rw [hctx.fidx]; rfl) hBO hAF hRU hUZ hUN hDA hDF h14 + +end + +end + +end Fl4Proofs.P3 + +-- ===== from RetVmRst + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section SpecV + +open Fl4Spec + +end SpecV + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} + +variable (arI : machine.Arch A) in + +/-- `…X` sibling of `VmUtPost` (post carries `VxE`; module docstring). -/ +def VmUtPostX (AR : A → Fl4Spec.Cr3 → Prop) (MR : A → Fl4Spec.MachineState → Prop) (σ : Fl4Spec.State Unit) + (inv : Fl4Spec.UntypedInvocation) (res : core.result.Result Unit error.Exc × S × A) : Prop := + ∃ σ1, Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False) ∧ + ((res.1 = .Ok () ∧ ∃ σ', (.ok (), σ') ∈ ((Fl4Spec.fl4InvokeUntypedV (Z := Unit) inv).run σ1).1 ∧ + ∃ (V' X' : Set U64) (XL' : U64 → Nat) (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI V' X' XL' AR Z' H' Cs' Ac' Lst' D' res.2.1 res.2.2 σ') ∨ + (res.1 = .Err .Preempted ∧ ∃ σ', (.error (), σ') ∈ ((Fl4Spec.fl4InvokeUntypedV (Z := Unit) inv).run σ1).1 ∧ + ∃ (V' X' : Set U64) (XL' : U64 → Nat), VmEptPre (L := L) arI V' X' XL' AR MR res.2.1 res.2.2 σ')) + +variable {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched Fl4Proofs.P1 + +/-- `…X` sibling of `VmUtPost.ofUnused` (post carries `VxE`; module docstring). -/ +theorem VmUtPostX.ofUnused {MR : A → Fl4Spec.MachineState → Prop} {σ σ1 : Fl4Spec.State Unit} + {sl : Fl4Spec.CslotPtr} {base rb : Fl4Spec.Word} {t : Fl4Spec.ApiObjectType} {us : Nat} + {slots : List Fl4Spec.CslotPtr} {dev d : Bool} {pu : Fl4Spec.ObjRef} {sz : Nat} + {res : core.result.Result Unit error.Exc × S × A} + (hst1 : Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1)}, False)) + (hg : Fl4Spec.getCap (Z := Unit) sl σ1 = ({(.untypedCap d pu sz 0, σ1)}, False)) + (h : ∃ σ1', Fl4Spec.setThreadState (Z := Unit) σ.curThread .restart σ = ({((), σ1')}, False) ∧ + ∃ σ', (Fl4Spec.fl4InvokeUntypedV (Z := Unit) (.retype sl false base rb t us slots dev)).run σ1' = + ({(.ok (), σ')}, False) ∧ + res.1 = .Ok () ∧ ∃ (V' X' : Set U64) (XL' : U64 → Nat) (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) + (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI V' X' XL' AR Z' H' Cs' Ac' Lst' D' res.2.1 res.2.2 σ') : + VmUtPostX (L := L) arI AR MR σ (.retype sl true base rb t us slots dev) res := by + obtain ⟨σ1', hst', σ', hrun, hok, hJ⟩ := h + have e : σ1' = σ1 := by + have h2 := hst'.symm.trans hst1 + simp only [Prod.mk.injEq, Set.singleton_eq_singleton_iff, true_and, and_true] at h2 + exact h2 + subst e + exact ⟨σ1', hst', Or.inl ⟨hok, σ', mem_of_run + ((VirtUtInvV.fl4InvokeUntypedV_reset_unused sl base rb t us slots dev σ1' hg).trans hrun), hJ⟩⟩ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.retypeRst` (post carries `VxE`; module docstring). -/ +theorem VmEptState.retypeRstX {s : S} {ar : A} {σ : Fl4Spec.State Unit} {MR : A → Fl4Spec.MachineState → Prop} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) {ct : U64} (hct : sched.cur_thread st s = ok ct) {pp : U64} {rs : Nat} {idx : List Bool} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {p sb fi : U64} + (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi false)) + {nt us dest off win : U64} {t : Fl4Spec.ApiObjectType} {r : Nat} {fiv : U64} (hc0 : Z.c = 0#u64) + (hctx : RetCtx (L := L) Z.th s σ (pp.bv, idx) sb fi false nt us dest off win t r true fiv) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) (hRU : RegionUnrefAt (L := L) H Cs D s) + (hAK : ARKeep arI AR) (hA : P0.ArchTotal arI) (hnd : Fl4Spec.numDomains = 1) (hMP : MachPlat arI MR) + (hPA : PPPlatA arI AR) (hrcb : Fl4Spec.resetChunkBits = 16) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → Fl4Spec.scActive y = true → + Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hVM : (∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx))) → ∀ s1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1)) + (hUZ : UtZero (L.mem s) Z.G σ) (hUN : UtNodesJ (L.mem s) Z.G (L.ksb s)) + (hMR : MR ar σ.machineState) (hVT : ValidMachineTime σ) (hVIM : ValidIrqMasks σ) + (h14 : nt.val = 14 → (∀ a', VirtSanEq.CapAgree arI a') ∧ ZvVirtAgree arI ∧ + ∃ v, arI.virtualization ar = ok v ∧ v ≠ 0#u64) : + (do + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st arI s2 ar (cslotAddr (pp.bv, idx)) true + ⟨Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)⟩ nt us dest off win false) ⦃ res => + VmUtPostX (L := L) arI AR MR σ (.retype (pp.bv, idx) true p.bv + (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) t us.val (windowSlots dest r off.val win.val) false) + res ⦄ := by + have hc2 : sched.cur_thread st s = ok (L.curW s) := cur_thread_eq s + have hctE : ct = L.curW s := by rw [hct] at hc2; exact eq_of_ok_eq hc2 + subst hctE + have B := J.1.1 + obtain ⟨hP, ⟨-, -, hM, hC, hI⟩, -⟩ := B.xtop.rel + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hxG : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hAs.isSlot, _, hcap, by simp⟩ + have hcv := capV_of_capAt hcap + have U := B.utc _ p sb fi false hAs hcap rfl + have hnc : ¬ ∃ y, σ.cdt y = some (pp.bv, idx) := by + have e := hctx.resetEq; exact of_decide_eq_true e.symm + have hfiv0 : fiv = 0#u64 := by rw [hctx.fidx]; rfl + have h14' : ∀ a', nt.val = 14 → P0.ArchTotal arI ∧ VirtSanEq.CapAgree arI a' ∧ + VirtInitWalk.VirtCodeAgree arI a' ∧ (Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm) := fun a' e => by + obtain ⟨hCA, hVA, v, hv, hv0⟩ := h14 e + exact ⟨hA, hCA a', hVA.code a', hVA.x86 hv hv0⟩ + -- the restart (`VmEptState.restartK`) + obtain ⟨s1, σ1, Lst1, x, hcur, hk, hs1, hst1, J1, he1, hSS1, hks1, hni1⟩ := J.restartK hni + have hσ1k : ∀ o, o ≠ (L.curW s).bv → σ1.kheap o = σ.kheap o := fun o ho => by + rw [he1, Notif.stsAct_kheap, Notif.updK_kheap]; exact Function.update_of_ne ho _ _ + have hσ1c : σ1.kheap (L.curW s).bv = some (.tcb { x with tcbState := .restart }) := by + rw [he1, Notif.stsAct_kheap, Notif.updK_kheap]; simp + have hσ1cdt : σ1.cdt = σ.cdt := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hcap1 : capAt (L.mem s1) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi false) := + (hSS1.capAt hAs.isSlot).trans hcap + have hg1 : Fl4Spec.getCap (Z := Unit) (pp.bv, idx) σ1 = + ({(.untypedCap false p.bv sb.val fi.val, σ1)}, False) := by + obtain ⟨c, a, hc, hca, hg⟩ := J1.1.1.xtop.rel.2.1.1.slot_cap (sl := (pp.bv, idx)) (p := pp) rfl + (by simpa [hidx] using hpp) + rw [hcap1] at hc; have := eq_of_ok_eq hc; subst this + rw [hg, hca.abs]; rfl + by_cases hf0 : fi.val = 0 + · -- an unused cap: the reset does nothing (`retype_reset_unused`), the retype by `retypeRG` + have hfi : fi = 0#u64 := UScalar.eq_of_val_eq (by rw [hf0]; rfl) + subst hfi + have hR := J.retypeRGX (ct := L.curW s) hni (cur_thread_eq s) hpp hidx hcap hc0 hctx hfiv0 hBO hAF hRU (fun _ => hUZ) + (fun _ => hUN) (fun h => absurd h (by simp)) (fun h => absurd h (by simp)) + (fun e => ⟨(h14' ar e).1, (h14' ar e).2.1, (h14' ar e).2.2.1, (h14 e).2⟩) + rw [hs1] at hR ⊢ + simp only [bind_ok] at hR ⊢ + rw [retype_reset_unused arI s1 ar _ _ nt us dest off win false hcap1] + exact WP.spec_mono hR fun res h => VmUtPostX.ofUnused hst1 hg1 + (let ⟨σ1', h1, σ', h2, h3, Z', H', Cs', Ac', Lst', D', hv, h4⟩ := h; ⟨σ1', h1, σ', h2, h3, _, _, _, Z', H', Cs', Ac', + Lst', D', hv, h4⟩) + -- a used cap: `reset_untyped` at the restart's state + have hctx1 := hctx.restart hSS1 hσ1cdt + have hBO1 := hBO.restart hk hσ1k hσ1c + have hAF1 := hAF.restart hσ1k hσ1c + have hnc1 : ¬ ∃ y, σ1.cdt y = some (pp.bv, idx) := by rw [hσ1cdt]; exact hnc + have hKN1 : UtNodesAt (L.ksb s1) p sb := by rw [hks1]; exact hUN _ hxG p sb fi hcv + have hnd' : ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx)) := nodesc_of_cdt hP hC hAs hnc + have hRD := (dead_childless B.jcov B.ut hI hxG hcv hnd').2 + obtain ⟨hKR, hDT⟩ := hRU s1 hs1 p.val (p.val + 2 ^ sb.val) (fun q k hq hr => + (hRD q k hq (by unfold RegionD MeetsR at hr; rw [hq] at hr; simpa using hr)).1) + have hres1 : σ1.schedulerAction ≠ .chooseNewThread := by rw [hni1.2]; simp + have hR0 : PPR0 σ := B.ppr0_start arI hni hRd0 hnq + have hR01 : PPR0 σ1 := by + rw [he1] + have h := hR0.of_ppk (PPK.updTcbSc (y := { x with tcbState := .restart }) hk rfl (hi := fun _ => Or.inr hcur.symm)) + unfold Notif.stsAct; split_ifs + all_goals first | exact h | exact ⟨⟨h.1.1, fun u e => (by cases e), h.1.2.2⟩, h.2⟩ + have hms1 : σ1.machineState = σ.machineState := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hcT1 : σ1.curTime = σ.curTime := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have his1 : σ1.interruptStates = σ.interruptStates := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hMR1 : MR ar σ1.machineState := hms1 ▸ hMR + have hVT1 : ValidMachineTime σ1 := by unfold ValidMachineTime; rw [hms1, hcT1]; exact hVT + have hVI1 : ValidIrqMasks σ1 := by unfold ValidIrqMasks; rw [hms1, his1]; exact hVIM + rw [hs1]; simp only [bind_ok] + refine retype_reset_wp arI s1 ar _ _ nt us dest off win false + (J1.reset_untyped hAK hA hnd hMP hPA hrcb hc0 hR01 hres1 hpp hidx hcap1 hnc1 hf0 hKR hDT hAF1 + J1.1.1.vsc.needR.1 J1.1.1.vsc.needR.2.1 J1.1.1.vsc.needR.2.2.1 J1.1.1.vsc.needR.2.2.2.1 + J1.1.1.vsc.needR.2.2.2.2 (hVM hnd' s1 hs1) Vs.needR_roots hMR1 hVT1 hVI1) ?_ ?_ + swap + · -- preempted: `ResetPostQ.vmPre` + intro e s2 a2 hpost + have hp : ((.Err e, s2, a2) : core.result.Result Unit error.Exc × S × A).1 = .Err .Preempted := by + rcases hpost with ⟨h, -⟩ | ⟨h, -⟩ + · simp at h + · exact h + obtain ⟨σ', hmem, hpre⟩ := ResetPostQ.vmPre hpost hp + exact ⟨σ1, hst1, Or.inr ⟨hp, σ', fl4InvokeUntypedV_reset_err _ _ _ _ _ _ _ hmem, _, _, _, hpre⟩⟩ + -- done: the entry facts at the reset's state (`retEntry_of_reset`), then `VmEptState.retype` + intro s2 a2 hpost + rcases hpost with ⟨-, σ2, Ac2, Lst2, D2, hmem, B2, ⟨C2, E2⟩, hcap2, hkb2, -, -, hnc2, -, hz2, -, hSl2⟩ | ⟨h, -⟩ + swap + · simp at h + have hxout := U.ns hnc _ hxG + have hppout : ¬ (p.val ≤ pp.val ∧ pp.val < p.val + 2 ^ sb.val) := by + obtain ⟨q, j, hj, hq, he'⟩ := hAs.at + have : pp = q := bv_inj hq + subst this + exact hj.obj_out hP U.nostr (by rw [← he']; exact hxout) + obtain ⟨hni2, hpp2, hctx2, hBO2, hAF2, hRU2⟩ := retEntry_of_reset J1.1.1.xtop.rel.1 J1.1.1.xtop.rel.2.1.2.2.1 + J1.1.1.ut J1.1.1.xtop.rel.2.1.2.2.2.2 hni1 hpp hidx hcap1 U.pfit U.fiLe hctx1 hppout U.nostr (U.ns hnc) hnd' + hBO1 (J1.1.1.sfree _ hxG p sb fi (capV_of_capAt hcap1)) hAF1 hg1 hf0 hmem hnc2 hSl2 + have hZ2 : UtFreeZ σ2 (p.val + (0#u64 : U64).val) (p.val + 2 ^ sb.val) := fun a h1 h2 => hz2 a (by + have e0 : (0#u64 : U64).val = 0 := rfl + omega) h2 + have hKN2 : UtNodesAt (L.ksb s2) p sb := by rw [hkb2]; exact hKN1 + have J2 : VmEptState (L := L) arI V (XOff X p sb) XL AR _ _ Cs Ac2 Lst2 D2 s2 a2 σ2 := ⟨⟨B2, C2⟩, E2⟩ + refine WP.spec_mono (J2.retypeX hni2 hpp2 hidx hcap2 hc0 hctx2 hfiv0 hBO2 hAF2 hRU2 (fun _ => hZ2) (fun _ => hKN2) + (fun h => absurd h (by simp)) (fun h => absurd h (by simp)) (h14' a2)) fun res h => ?_ + obtain ⟨σ', hrun, hok, Z3, H3, Cs3, Ac3, Lst3, D3, hv3, J3⟩ := h + exact ⟨σ1, hst1, Or.inl ⟨hok, σ', fl4InvokeUntypedV_reset_ok _ _ _ _ _ _ _ hmem (mem_of_run hrun), _, _, _, Z3, + H3, Cs3, Ac3, Lst3, D3, hv3, J3⟩⟩ + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.retypeRstD` (post carries `VxE`; module docstring). -/ +theorem VmEptState.retypeRstDX {s : S} {ar : A} {σ : Fl4Spec.State Unit} {MR : A → Fl4Spec.MachineState → Prop} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] + (hni : Entered σ) {ct : U64} (hct : sched.cur_thread st s = ok ct) {pp : U64} {rs : Nat} {idx : List Bool} + (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) {p sb fi : U64} + (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi true)) + {nt us dest off win : U64} {t : Fl4Spec.ApiObjectType} {r : Nat} {fiv : U64} (hc0 : Z.c = 0#u64) + (hctx : RetCtx (L := L) Z.th s σ (pp.bv, idx) sb fi true nt us dest off win t r true fiv) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) (hRU : RegionUnrefAt (L := L) H Cs D s) + (hDA : DevApart (L := L) Z.th H Z.G Cs s) (hDF : DevFreeT (L.mem s) Z.G σ) (hGP : DevGpm (L.mem s) Z.G σ) + (hA : P0.ArchTotal arI) (hrcb : Fl4Spec.resetChunkBits = 16) + (hVM : (∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx))) → ∀ s1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1)) + (h14 : nt.val = 14 → (∀ a', VirtSanEq.CapAgree arI a') ∧ ZvVirtAgree arI ∧ + ∃ v, arI.virtualization ar = ok v ∧ v ≠ 0#u64) : + (do + let s2 ← sched.set_thread_state st s ct sched.TS_RESTART + untyped.retype st arI s2 ar (cslotAddr (pp.bv, idx)) true + ⟨Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)⟩ nt us dest off win true) ⦃ res => + VmUtPostX (L := L) arI AR MR σ (.retype (pp.bv, idx) true p.bv + (Fl4Spec.alignUp (p.bv + fiv.bv) (zOf nt.val us.val)) t us.val (windowSlots dest r off.val win.val) true) + res ⦄ := by + have hc2 : sched.cur_thread st s = ok (L.curW s) := cur_thread_eq s + have hctE : ct = L.curW s := by rw [hct] at hc2; exact eq_of_ok_eq hc2 + subst hctE + have B := J.1.1 + obtain ⟨hP, ⟨-, -, hM, hC, hI⟩, -⟩ := B.xtop.rel + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + have hxG : cslotAddr (pp.bv, idx) ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hAs.isSlot, _, hcap, by simp⟩ + have hcv := capV_of_capAt hcap + have hsb : sb.val < 64 := capAt_untyped_inv hcap + obtain ⟨hpal, -, -, -, -, hrf, -, -, -⟩ := hDA _ hxG p sb fi hcv + have hfit : p.val + 2 ^ sb.val ≤ 2 ^ 64 := fit_of_al (show p.val < 2 ^ 64 from p.bv.isLt) hsb hpal + have hfib : fi.val ≤ 2 ^ sb.val := B.ut.fib _ hxG p sb fi true hcv + have hN : NoStraddle Z.th p.val (p.val + 2 ^ sb.val) := fun q k hq => + (hrf q k hq).elim (fun h => Or.inr (Or.inl h)) (fun h => Or.inr (Or.inr h)) + have hns : ∀ y ∈ Z.G.chains.flatten, y.val < p.val ∨ p.val + 2 ^ sb.val ≤ y.val := by + intro y hy + obtain ⟨q, kq, hq, h1, h2, -⟩ := slot_in_object hP ((hM.members y).1 hy).1 + rcases hrf q kq hq with e | e <;> omega + have hnc : ¬ ∃ y, σ.cdt y = some (pp.bv, idx) := by + have e := hctx.resetEq; exact of_decide_eq_true e.symm + have hfiv0 : fiv = 0#u64 := by rw [hctx.fidx]; rfl + have h14' : ∀ a', nt.val = 14 → P0.ArchTotal arI ∧ VirtSanEq.CapAgree arI a' ∧ + VirtInitWalk.VirtCodeAgree arI a' ∧ (Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm) := fun a' e => by + obtain ⟨hCA, hVA, v, hv, hv0⟩ := h14 e + exact ⟨hA, hCA a', hVA.code a', hVA.x86 hv hv0⟩ + -- the restart (`VmEptState.restartK`) + obtain ⟨s1, σ1, Lst1, x, hcur, hk, hs1, hst1, J1, he1, hSS1, hks1, hni1⟩ := J.restartK hni + have hσ1k : ∀ o, o ≠ (L.curW s).bv → σ1.kheap o = σ.kheap o := fun o ho => by + rw [he1, Notif.stsAct_kheap, Notif.updK_kheap]; exact Function.update_of_ne ho _ _ + have hσ1c : σ1.kheap (L.curW s).bv = some (.tcb { x with tcbState := .restart }) := by + rw [he1, Notif.stsAct_kheap, Notif.updK_kheap]; simp + have hσ1cdt : σ1.cdt = σ.cdt := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hid1 : σ1.idleThread = σ.idleThread := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hcap1 : capAt (L.mem s1) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi true) := + (hSS1.capAt hAs.isSlot).trans hcap + have hg1 : Fl4Spec.getCap (Z := Unit) (pp.bv, idx) σ1 = + ({(.untypedCap true p.bv sb.val fi.val, σ1)}, False) := by + obtain ⟨c, a, hc, hca, hg⟩ := J1.1.1.xtop.rel.2.1.1.slot_cap (sl := (pp.bv, idx)) (p := pp) rfl + (by simpa [hidx] using hpp) + rw [hcap1] at hc; have := eq_of_ok_eq hc; subst this + rw [hg, hca.abs]; rfl + by_cases hf0 : fi.val = 0 + · -- an unused cap: the reset does nothing (`retype_reset_unused`), the retype by `retypeRG` + have hfi : fi = 0#u64 := UScalar.eq_of_val_eq (by rw [hf0]; rfl) + subst hfi + have hR := J.retypeRGX (ct := L.curW s) hni (cur_thread_eq s) hpp hidx hcap hc0 hctx hfiv0 hBO hAF hRU + (fun h => absurd h (by simp)) (fun h => absurd h (by simp)) (fun _ => hDA) (fun _ => hDF) + (fun e => ⟨(h14' ar e).1, (h14' ar e).2.1, (h14' ar e).2.2.1, (h14 e).2⟩) + rw [hs1] at hR ⊢ + simp only [bind_ok] at hR ⊢ + rw [retype_reset_unused arI s1 ar _ _ nt us dest off win true hcap1] + exact WP.spec_mono hR fun res h => VmUtPostX.ofUnused hst1 hg1 + (let ⟨σ1', h1, σ', h2, h3, Z', H', Cs', Ac', Lst', D', hv, h4⟩ := h; ⟨σ1', h1, σ', h2, h3, _, _, _, Z', H', Cs', Ac', + Lst', D', hv, h4⟩) + -- a used cap: the device facts after the restart, then `reset_untypedD` + have hctx1 := hctx.restart hSS1 hσ1cdt + have hBO1 := hBO.restart hk hσ1k hσ1c + have hAF1 := hAF.restart hσ1k hσ1c + have hcvK : ∀ y ∈ Z.G.chains.flatten, capV (L.mem s1) y = capV (L.mem s) y := fun y hy => + capV_of_capAt ((hSS1.capAt ((hM.members y).1 hy).1).trans (capAt_capV _ _)) + have hidK : IdleKeep (L := L) s s1 := + B.idleW_keep J1.1.1.xtop.fin.1.1.1 hid1 (by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl) + have hDA1 := hDA.restart hcvK hks1 hidK + have hDF1 := hDF.restart hcvK hk hσ1k hσ1c + have hσ1a : σ1.archState = σ.archState := by rw [he1]; unfold Notif.stsAct; split_ifs <;> rfl + have hGP1 : DevGpm (L.mem s1) Z.G σ1 := by + intro y hy p0 sb0 fi0 hc + rw [hσ1a] + rw [hcvK y hy] at hc + exact hGP y hy p0 sb0 fi0 hc + have hnc1 : ¬ ∃ y, σ1.cdt y = some (pp.bv, idx) := by rw [hσ1cdt]; exact hnc + have hnd' : ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx)) := nodesc_of_cdt hP hC hAs hnc + obtain ⟨hKR, hDT⟩ := hRU s1 hs1 p.val (p.val + 2 ^ sb.val) (devRegion_dead B.jcov B.ut hI hDA hxG hcv hnd') + have hres1 : σ1.schedulerAction ≠ .chooseNewThread := by rw [hni1.2]; simp + rw [hs1]; simp only [bind_ok] + refine retype_reset_wp arI s1 ar _ _ nt us dest off win true + (J1.reset_untypedD hrcb hres1 hpp hidx hcap1 hnc1 hf0 hDA1 hDF1 hGP1 hKR hDT hAF1 + J1.1.1.vsc.needR.1 J1.1.1.vsc.needR.2.1 J1.1.1.vsc.needR.2.2.1 J1.1.1.vsc.needR.2.2.2.1 + J1.1.1.vsc.needR.2.2.2.2 (hVM hnd' s1 hs1) Vs.needR_roots) ?_ ?_ + swap + · intro e s2 a2 hpost + exact absurd hpost.1 (by simp) + intro s2 a2 hpost + obtain ⟨-, σ2, Ac2, Lst2, D2, hmem, B2, ⟨C2, E2⟩, hcap2, -, hnc2, hDA2, hDF2, -, hSl2⟩ := hpost + obtain ⟨hni2, hpp2, hctx2, hBO2, hAF2, hRU2⟩ := retEntry_of_reset J1.1.1.xtop.rel.1 J1.1.1.xtop.rel.2.1.2.2.1 + J1.1.1.ut J1.1.1.xtop.rel.2.1.2.2.2.2 hni1 hpp hidx hcap1 hfit hfib hctx1 + (by + have h0 : 0 < objSize (ObjKind.cnode rs) := by simp only [objSize]; positivity + rcases hrf pp _ hpp with h | h <;> omega) hN hns hnd' hBO1 + (hDF1 _ hxG p sb fi true (capV_of_capAt hcap1) ⟨rfl, trivial⟩) hAF1 hg1 hf0 hmem hnc2 hSl2 + have J2 : VmEptState (L := L) arI V (XOff X p sb) XL AR _ _ Cs Ac2 Lst2 D2 s2 a2 σ2 := ⟨⟨B2, C2⟩, E2⟩ + refine WP.spec_mono (J2.retypeX hni2 hpp2 hidx hcap2 hc0 hctx2 hfiv0 hBO2 hAF2 hRU2 (fun h => absurd h (by simp)) + (fun h => absurd h (by simp)) (fun _ => hDA2) (fun _ => hDF2) (h14' a2)) fun res h => ?_ + obtain ⟨σ', hrun, hok, Z3, H3, Cs3, Ac3, Lst3, D3, hv3, J3⟩ := h + exact ⟨σ1, hst1, Or.inl ⟨hok, σ', fl4InvokeUntypedV_reset_ok _ _ _ _ _ _ _ hmem (mem_of_run hrun), _, _, _, Z3, + H3, Cs3, Ac3, Lst3, D3, hv3, J3⟩⟩ + +end + +end + +end Fl4Proofs.P3 + +-- ===== from RetVmUtDec + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P3.Sched + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptState.decodeUt` (post carries `VxE`; module docstring). -/ +theorem VmEptState.decodeUtX [Fl4Proofs.P1.PlainStore st] {s : S} {ar : A} {σ : Fl4Spec.State Unit} + {MR : A → Fl4Spec.MachineState → Prop} + (J : VmEptState (L := L) arI V X XL AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) + (hA : P0.ArchTotal arI) (hag : ∀ r, arI.virtualization ar = ok r → r.val = Fl4Spec.virtKind.code) + (hVA : ZvVirtAgree arI) (hCap : ∀ a', VirtSanEq.CapAgree arI a') + (lbl : U64) (slbl : Fl4Spec.Data) + (hlbl : Fl4Spec.genInvocationType slbl = .untypedRetype ↔ lbl = fl4_abi.label.UntypedRetype) + (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) + {pp : U64} {rs : Nat} {idx : List Bool} (hpp : Z.th pp = some (.cnode rs)) (hidx : idx.length = rs) + {p sb fi : U64} {dev : Bool} (hcap : capAt (L.mem s) (cslotAddr (pp.bv, idx)) = ok (.Untyped p sb fi dev)) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (x0 : U64) (hx0 : Aeneas.Std.Array.index_usize extra 0#usize = ok x0) + (excaps : List Fl4Spec.Cap) (hex : ExtraRel Z.th (L.mem s) x0 excaps) + (hfan : Fl4Spec.retypeFanOutLimit = 256#64) (hc0 : Z.c = 0#u64) + (hBO : BufObjHyp Z.B σ) (hAF : ArchFitHyp H σ) (hRU : RegionUnrefAt (L := L) H Cs D s) + (hUZ : dev = false → UtZero (L.mem s) Z.G σ) (hUN : dev = false → UtNodesJ (L.mem s) Z.G (L.ksb s)) + (hDA : dev = true → DevApart (L := L) Z.th H Z.G Cs s) (hDF : dev = true → DevFreeT (L.mem s) Z.G σ) + (hGP : dev = true → DevGpm (L.mem s) Z.G σ) + (hAK : ARKeep arI AR) (hnd : Fl4Spec.numDomains = 1) (hMP : MachPlat arI MR) (hPA : PPPlatA arI AR) + (hrcb : Fl4Spec.resetChunkBits = 16) + (hRd0 : σ.consumedTime = 0 → ∀ y n, σ.kheap σ.curSc = some (.schedContext y n) → Fl4Spec.scActive y = true → + Fl4Spec.refillReady σ.curTime (Fl4Spec.refillHd y) = true) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + (hVM : (∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (pp.bv, idx))) → ∀ s1, + sched.set_thread_state st s ct sched.TS_RESTART = ok s1 → + Vs.VsReachMiss p.val (p.val + 2 ^ sb.val) (L.mem s1) (L.ksb s1) + (Vs.NeedR Z.th (L.mem s1) 5) (Vs.NeedR Z.th (L.mem s1) 4) (Vs.NeedR Z.th (L.mem s1) 3) + (Vs.NeedR Z.th (L.mem s1) 2) (Vs.NeedR Z.th (L.mem s1) 1)) + (hMR : MR ar σ.machineState) (hVT : ValidMachineTime σ) (hVIM : ValidIrqMasks σ) : + untyped.decode st arI s ar lbl length (cslotAddr (pp.bv, idx)) (.Untyped p sb fi dev) buf extra ⦃ res => + UPost σ (Fl4Spec.fl4DecodeUntypedInvocationV slbl (args.map UScalar.bv) (pp.bv, idx) + (absCap (.Untyped p sb fi dev)) excaps) s ar (VmUtPostX (L := L) arI AR MR σ) res ⦄ := by + have B := J.1.1 + obtain ⟨hP, ⟨hSR, hV, hM, hC, hI⟩, -⟩ := B.xtop.rel + have hAs : AbsSlot Z.th (pp.bv, idx) := ⟨pp, rfl, Or.inl (by simpa [hidx] using hpp)⟩ + refine untyped_decode_refinesVG arI hA Z.th s ar hag σ Z.G hSR hV hP hM hC hI lbl slbl hlbl length buf ct hct args + hlen hargs _ (pp.bv, idx) rfl hAs p sb fi dev hcap extra x0 hx0 excaps hex hfan _ ?_ + intro nt us dest off win t r reset fiv _ hctx hg + have hgate : nt.val = 14 → ∃ v, arI.virtualization ar = ok v ∧ v ≠ 0#u64 := fun e => hg (by omega) (by omega) + cases reset + · exact WP.spec_mono (J.retypeRX hni hct hpp hidx hcap hc0 hctx hBO hAF hRU hUZ hUN hDA hDF + fun e => ⟨hA, hCap ar, hVA.code ar, hVA, hgate e⟩) + fun res ⟨σ1, h1, σ', h2, h3, Z', H', Cs', Ac', Lst', D', hv, h4⟩ => + ⟨σ1, h1, Or.inl ⟨h3, σ', mem_of_run h2, _, _, _, Z', H', Cs', Ac', Lst', D', hv, h4⟩⟩ + · cases dev + · exact J.retypeRstX hni hct hpp hidx hcap hc0 hctx hBO hAF hRU hAK hA hnd hMP hPA hrcb hRd0 hnq hVM (hUZ rfl) + (hUN rfl) hMR hVT hVIM fun e => ⟨hCap, hVA, hgate e⟩ + · exact J.retypeRstDX hni hct hpp hidx hcap hc0 hctx hBO hAF hRU (hDA rfl) (hDF rfl) (hGP rfl) hA hrcb hVM + fun e => ⟨hCap, hVA, hgate e⟩ + +end + +end + +end Fl4Proofs.P3 + +-- ===== from VmPreempt + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 + +open fl4_core + +noncomputable section + +section Spec + +open Fl4Spec + +end Spec + +section + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +end + +section + +open Classical Fl4Proofs.P3.Vs Fl4Proofs.P1 Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {MR : A → Fl4Spec.MachineState → Prop} + +set_option maxHeartbeats 4000000 in +/-- `…X` sibling of `VmEptPre.preemptPathVFk` (post carries `VxE`; module docstring). -/ +theorem VmEptPre.preemptPathVFkX (hARI : ARIface arI AR) (hAT : P0.ArchTotal arI) (hVA : ZvVirtAgree arI) + (hAK : ARKeep arI AR) (hnd : Fl4Spec.numDomains = 1) (hM : MachPlat arI MR) (hMK : MRKeep arI MR) + (hPA : PPPlatA arI AR) (hIA : IrqPlatA arI AR) + (hmrt : ∀ a : A, sporadic.max_release_time arI a ⦃ mrt => mrt.bv = Fl4Spec.MAX_RELEASE_TIME ⦄) + {V X : Set U64} {XL : U64 → Nat} {s : S} {a : A} {σ : Fl4Spec.State Unit} + (h : VmEptPre (L := L) arI V X XL AR MR s a σ) (hnv : ¬ Sched.NoTmoSend σ → ¬ RunningVM σ) + (hTU : PPRecv σ → Fl4Spec.maxTicksToUs = core.num.U64.MAX.bv ∧ + ∀ (a : A) v, arI.ticks_to_us a v ⦃ r => r.bv = Fl4Spec.ticksToUs v.bv ⦄) + {on rc : Fl4Spec.ObjRef → Bool} (hO : VmOrc st on rc s σ) (e : error.Exc) : + syscall.after_invocation st arI s a (.Err e) ⦃ r => r.1 = false ∧ + ∃ z ∈ (Fl4Spec.preemptionPathVF (Z := Unit) on rc σ).1, + (∃ (Z' : XSets) (H' : P1.Heap) (Cs' Ac' : Set U64) (Lst' : U64 → List U64) (D' : Set U64), + VxE Z' ∧ VmEptState (L := L) arI V X XL AR Z' H' Cs' Ac' Lst' D' r.2.1 r.2.2 z.2) ∧ + VmOrc st on rc r.2.1 z.2 ∧ MR r.2.2 z.2.machineState ⦄ := by + obtain ⟨Z, H, Cs, Ac, Lst, D, s0, a0, σ0, ⟨J, hvx0, hns0, hp00, hR0, hV0, hI0⟩, hpp, hσ⟩ := h + haveI := hvx0 + have hw := J.1.1.ppJHM hAK hAT hnd hM hPA hR0 hV0 + rw [hpp, WP.spec_ok] at hw + obtain ⟨s1, Ac1, Lst1, Hh, -, F01, hR, -⟩ := hw rfl σ hσ + have hE := Sched.updateTimeStamp_eq' hnd (doMachineOp_single (getCurrentTime_eq σ0.machineState)) + have eσ : σ = _ := pp_err_eq hE (doMachineOp_single (getActiveIRQ_eq true _)) _ hσ rfl + have hTs : TShift σ0 σ := by rw [eσ]; exact ⟨vmt_le_gct hV0, rfl⟩ + have hVI : ValidIrqMasks σ := by rw [eσ]; exact fun i hi => hI0 i hi + -- the hole's spec facts (`jAtPreMV_holeQ`) + have hfr0 : CurScFree σ0 := curScFree_of J.1.1.inv J.1.1.own hp00 + have hni : σ.curThread ≠ σ.idleThread := by rw [eσ]; exact hp00.1.1 + have hns : Vs.NoSwitch σ := by rw [eσ]; exact hns0 + have hfr : CurScFree σ := by rw [eσ]; exact fun u xt hu hs => hfr0 u xt hu hs + have hct : CtOK σ := by rw [eσ]; exact hp00.1.2.2.1 + have hcg : Chg σ := by rw [eσ]; exact hp00.1.2.2.2.1 + have hnq : CtNQ σ := by rw [eσ]; exact hp00.1.2.2.2.2 + obtain ⟨⟨Qh, Rh, h0⟩, -⟩ := Hh.blk + unfold syscall.after_invocation + refine WP.spec_bind (Hh.mcsPPVF hTs hAT hnd (hmrt a) (hPA.minBudget a) hM hMK hR hVI hni hns hfr hct hcg hnq + hnv hTU) ?_ + rintro ⟨s2, a2⟩ ⟨⟨σ', hpp', ⟨V1, Ac2, Lst2, D2, B2, F12⟩, hR2, hVI2, -⟩, F⟩ + have J2 := J.ofNat B2 (J.1.1.natTrans F01 F12) + have hO2 : VmOrc st on rc s2 σ' := IpcFootN.vmOrc h0 hO F + (fun t y' hk' hr' => by + obtain ⟨y, hy, hry⟩ := ZvK.rvm F12.fk.1 t y' hk' hr' + exact ⟨y, by rw [Hh.sfacts.2.2.2.2.1]; exact hy, hry⟩) + (fun t y' hk' => by + obtain ⟨u, hu, rfl⟩ := B2.obj.tcb t y' hk' + exact ⟨u, J.1.1.zH.1 u hu, rfl⟩) + have hm : ∀ y, y ∈ (Fl4Spec.maybeHandleInterruptVF (Z := Unit) on rc true σ').1 → + y ∈ (Fl4Spec.preemptionPathVF (Z := Unit) on rc σ).1 := by + rw [preemptionPathVF_ppG]; exact (hpp' _).1 + -- the IRQ take and the handler (P3/XTopJointMRPath's `JAtZB.irqTailB` at `VmEptState`) + have hAR2 : AR a2 σ'.archState.x64CurrentCr3 := B2.xtop.fin.1.1.2.2.2.2.2.2 + refine WP.spec_bind (WP.spec_and (WP.spec_and (hM.takeAt hR2) (hIA.takeAR a2 _ hAR2)) (hM.noIpi a2 _ hR2)) ?_ + rintro ⟨w, a3⟩ ⟨⟨⟨irq, ms, hIq, hnone, hsome, hum, hR3⟩, hAR3⟩, h126, h127⟩ + obtain ⟨Lst3, B3, F3⟩ := B2.msArJ ms a3 hum (fun _ => hAR3) + have J3 := J2.ofNat (V' := V1) B3 F3 + unfold syscall.check_interrupt + by_cases hw : w = syscall.NO_IRQ + · have hirq : irq = none := hnone.1 hw + subst hirq + have e : (w != syscall.NO_IRQ) = false := by simp [hw] + show (do + let (s4, a4) ← (if (w != syscall.NO_IRQ) = true then interrupt.handle_interrupt st arI s2 a3 w else ok (s2, a3)) + ok (false, s4, a4)) ⦃ _ ⦄ + rw [e] + simp only [Bool.false_eq_true, ↓reduceIte, bind_ok] + exact (WP.spec_ok _).2 ⟨rfl, ((), { σ' with machineState := ms }), hm _ (mem_maybeHandleVF_none hIq), + ⟨_, _, _, _, _, _, J3.withX arI⟩, hO2, hR3⟩ + · obtain ⟨i, rfl⟩ : ∃ i, irq = some i := by + cases irq with + | none => exact absurd (hnone.2 rfl) hw + | some i => exact ⟨i, rfl⟩ + obtain ⟨hwi, -⟩ := hsome i rfl + have e : (w != syscall.NO_IRQ) = true := by simpa using hw + show (do + let (s4, a4) ← (if (w != syscall.NO_IRQ) = true then interrupt.handle_interrupt st arI s2 a3 w else ok (s2, a3)) + ok (false, s4, a4)) ⦃ _ ⦄ + rw [e] + simp only [↓reduceIte] + have hw256 : w.val < 256 := by rw [hwi]; exact i.isLt + have hwb : w.bv.setWidth 8 = i := setWidth8_eq hwi + refine WP.spec_bind (J3.handleIrqVFk hARI hAT hVA hAK hPA.minBudget (on := on) (rc := rc) hO2 hR3 w i hwi h126 + h127 (fun a1 c h => hIA.ackAR a1 w c h) (fun a0 a1 σ0 h0 he => ?_) (fun a1 ms h => hM.ack a1 w ms h) + (msig_sendSignalVF hAT hM hMK on rc)) ?_ + · have hmk := hM.mask a0 w true σ0.machineState h0 hw256 + rw [he, WP.spec_ok, hwb] at hmk + exact hmk + rintro ⟨s4, a4⟩ (⟨hin, -⟩ | ⟨z, hz, ⟨⟨V', Ac', Lst', D', J4⟩, O4⟩, hR4⟩) + · exact absurd hin (gai_active hVI2 hIq i rfl) + · exact (WP.spec_ok _).2 ⟨rfl, z, hm _ (mem_maybeHandleVF_some hIq hz), ⟨_, _, _, _, _, _, J4.withX arI⟩, O4, hR4⟩ + +end + +section + +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} + +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {V X : Set U64} {XL : U64 → Nat} + +end + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmWitBlk.lean b/proofs/Fl4Proofs/P3/VmWitBlk.lean new file mode 100644 index 000000000..1cc20e630 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitBlk.lean @@ -0,0 +1,492 @@ +/- +P3 (line mmuAD witness): **P1's `BlkOK` at `ww`** (`ww_blk`). Generated by `handoff/tools/gen_witblk.py` from +P3/JBootQ1Blk. + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitCs +import Fl4Proofs.P3.JBootQ1Blk +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P2.Wit Classical + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-! ## `Hww`'s sets -/ + +theorem curW_ww : wL.curW ww = rT := curT_ww +theorem idleW_ww : wL.idleW ww = idT := idleT_ww +theorem cscW_ww : wL.cscW ww = rS := curSc_ww +theorem idleScW_ww : idleScW (L := wL) ww = idS := idleSc_ww + +theorem yield_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (yieldA t) = 0#u64 := by + unfold yieldA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +theorem tcbs_Hww : Heap.tcbs Hww = Tw := by + ext p + simp only [Heap.tcbs, Set.mem_setOf_eq] + constructor + · intro h + rcases Hww_some h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, -, -, -, -, h'⟩ + · exact rT_mem + · exact gT_mem + · cases e + · cases e + · cases e + · have : p ∈ Heap.tcbs Hw := h' + rw [tcbs_Hw] at this; rw [memT0 this]; exact idT_mem + · intro h; rcases memTw h with rfl | rfl | rfl + · exact Hww_idT + · exact Hww_rT + · exact Hww_gT + +theorem scs_Hww : Heap.scs Hww = C1 := by + ext p + simp only [Heap.scs, Set.mem_setOf_eq] + constructor + · rintro ⟨z, h⟩ + rcases Hww_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, -, -, -, -, h'⟩ + · cases e + · cases e + · exact rS_mem + · cases e + · cases e + · have : p ∈ Heap.scs Hw := ⟨z, h'⟩ + rw [scs_Hw] at this; rw [memC0 this]; exact idS_mem + · intro h; rcases memC1 h with rfl | rfl + · exact ⟨7, Hww_idS⟩ + · exact ⟨7, Hww_rS⟩ + +theorem Hww_kind {p : U64} {k : HKind} (h : Hww p = some k) : k = .tcb ∨ k = .sc 7 ∨ ∃ z, k = .other z := by + rcases Hww_some h with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, -, -, -, -, h'⟩ + · exact Or.inl rfl + · exact Or.inl rfl + · exact Or.inr (Or.inl rfl) + · exact Or.inr (Or.inr ⟨8, rfl⟩) + · exact Or.inr (Or.inr ⟨15, rfl⟩) + · exact Hw_kind h' + +theorem replies_Hww : Heap.replies Hww = ∅ := by + ext p + simp only [Heap.replies, Set.mem_setOf_eq, Set.mem_empty_iff_false, iff_false] + intro h + rcases Hww_kind h with e | e | ⟨z, e⟩ <;> cases e + +theorem ntfns_Hww : Heap.ntfns Hww = ∅ := by + ext p + simp only [Heap.ntfns, Set.mem_setOf_eq, Set.mem_empty_iff_false, iff_false] + intro h + rcases Hww_kind h with e | e | ⟨z, e⟩ <;> cases e + +theorem scBits1 (c : U64) (hc : c ∈ C1) : Heap.scBits Hww c = 7 := by + unfold Heap.scBits + rcases memC1 hc with rfl | rfl + · rw [Hww_idS] + · rw [Hww_rS] + +/-- `Ω`'s words by position. -/ +theorem omega1_pos {b : U64} (h : b ∈ Heap.omega Hww) : b.val % 8 = 0 ∧ + ((18446744071562080256 ≤ b.val ∧ b.val < 18446744071562096768) ∨ + (18446744071562104832 ≤ b.val ∧ b.val < 18446744071562104832 + 128) ∨ + (18446744071562108928 ≤ b.val ∧ b.val < 18446744071562108928 + 256) ∨ + (18446744071563116544 ≤ b.val ∧ b.val < 18446744071563116544 + 32768) ∨ + (18446744071562133504 ≤ b.val ∧ b.val < 18446744071562147072)) := by + obtain ⟨hal, p, k, hp, hk, hin⟩ := h + refine ⟨hal, ?_⟩ + unfold InObj at hin + rcases Hww_pos hp with ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨rfl, e⟩ | + ⟨rfl, e⟩ | ⟨rfl, e⟩ | ⟨rfl, e⟩ + · rcases hk with e' | ⟨z, e'⟩ | ⟨z, e'⟩ | ⟨e', -⟩ <;> cases e' + · rcases hk with e' | ⟨z, e'⟩ | ⟨z, e'⟩ | ⟨e', -⟩ <;> cases e' + · simp only [HKind.bits, Nat.reducePow] at hin; omega + · simp only [HKind.bits, Nat.reducePow] at hin; omega + · simp only [HKind.bits, Nat.reducePow] at hin; omega + · rcases hk with e' | ⟨z, e'⟩ | ⟨z, e'⟩ | ⟨e', -⟩ <;> cases e' + all_goals simp only [HKind.bits, Nat.reducePow] at hin; omega + +/-- The frame words are the two SCs'. -/ +theorem frame1_pos {b : U64} (h : b ∈ Heap.frame Hww) : + b.val % 8 = 0 ∧ ((18446744071562096640 ≤ b.val ∧ b.val < 18446744071562096768) ∨ + (18446744071562104832 ≤ b.val ∧ b.val < 18446744071562104832 + 128)) := by + obtain ⟨hal, p, k, hp, hk, hin⟩ := h + refine ⟨hal, ?_⟩ + unfold InObj at hin + rcases Hww_pos hp with ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨rfl, -⟩ | + ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ + all_goals try (rcases hk with e' | ⟨z, e'⟩ | ⟨e', -⟩ <;> cases e'; done) + all_goals simp only [HKind.bits, Nat.reducePow] at hin; omega + +theorem frame1_of {c b : U64} (hc : c ∈ C1) (hb : b.val % 8 = 0) (h1 : c.val ≤ b.val) (h2 : b.val < c.val + 128) : + b ∈ Heap.frame Hww := by + rcases memC1 hc with rfl | rfl + · exact ⟨hb, idS, .sc 7, Hww_idS, Or.inr (Or.inl ⟨7, rfl⟩), h1, by simp only [HKind.bits, Nat.reducePow]; omega⟩ + · exact ⟨hb, rS, .sc 7, Hww_rS, Or.inr (Or.inl ⟨7, rfl⟩), h1, by simp only [HKind.bits, Nat.reducePow]; omega⟩ + +theorem scww_frame {c : U64} (hc : c ∈ C1) (K : U64) (hK : K.val < 128) (h8 : K.val % 8 = 0) : + core.num.U64.wrapping_add c K ∈ Heap.frame Hww := by + have hcv : c.val % 128 = 0 ∧ c.val + 128 < 2 ^ 64 := by + rcases memC1 hc with rfl | rfl <;> decide + have hv := wa_val (a := c) (b := K) (by omega) + exact frame1_of hc (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) + +theorem Hw_ne {p : U64} {k : HKind} (h : Hw p = some k) : p ≠ rT ∧ p ≠ gT ∧ p ≠ rS ∧ p ≠ rC ∧ p ≠ vB := by + have hp : p.val ≠ 18446744071562100736 ∧ p.val ≠ 18446744071562113024 ∧ p.val ≠ 18446744071562104832 ∧ p.val ≠ 18446744071562108928 ∧ p.val ≠ 18446744071563116544 := by + rcases Hw_pos h with ⟨e, -⟩ | ⟨e, -⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> omega + obtain ⟨h1, h2, h3, h4, h5⟩ := hp + exact ⟨fun e => h1 (by rw [e]; rfl), fun e => h2 (by rw [e]; rfl), fun e => h3 (by rw [e]; rfl), + fun e => h4 (by rw [e]; rfl), fun e => h5 (by rw [e]; rfl)⟩ + +theorem ks1_omega : ∀ o : U64, o.val < 13568 → o.val % 8 = 0 → + core.num.U64.wrapping_add (wL.ksb ww) o ∈ Heap.omega Hww := by + intro o ho h8 + obtain ⟨hal, p, k, hp, hk, hin⟩ := ks_omega o ho h8 + obtain ⟨n1, n2, n3, n4', n5'⟩ := Hw_ne hp + exact ⟨hal, p, k, by rw [Hww_old n1 n2 n3 n4' n5']; exact hp, hk, hin⟩ + +/-! ## Words of the image -/ + +theorem st_ww (t : U64) (ht : t ∈ Tw) : + (t = idT ∧ wL.mem ww (P1.stA t) = 7#u64) ∨ (t = rT ∧ wL.mem ww (P1.stA t) = 1#u64) ∨ + (t = gT ∧ wL.mem ww (P1.stA t) = 8#u64) := by + rcases memTw ht with rfl | rfl | rfl + · exact Or.inl ⟨rfl, by rw [P1.stA_eq, tcbW1_v _ (by decide) (by decide)]; decide⟩ + · exact Or.inr (Or.inl ⟨rfl, by rw [P1.stA_eq, rtW_v _ (by decide) (by decide)]; decide⟩) + · exact Or.inr (Or.inr ⟨rfl, by rw [P1.stA_eq, gtW_v _ (by decide) (by decide)]; decide⟩) + +theorem n1_4 : (1#u64) ≠ sched.TS_BLOCKED_ON_SEND := by unfold sched.TS_BLOCKED_ON_SEND; decide +theorem n1_3 : (1#u64) ≠ sched.TS_BLOCKED_ON_RECV := by unfold sched.TS_BLOCKED_ON_RECV; decide +theorem n1_6 : (1#u64) ≠ sched.TS_BLOCKED_ON_NTFN := by unfold sched.TS_BLOCKED_ON_NTFN; decide +theorem n1_5 : (1#u64) ≠ sched.TS_BLOCKED_ON_REPLY := by unfold sched.TS_BLOCKED_ON_REPLY; decide + +theorem nbl1 : ¬ Blocked (1#u64) := by + rintro (h | h | h) + · exact n1_4 h + · exact n1_3 h + · exact n1_6 h + +theorem nbr1 : ¬ BlockedR (1#u64) := by + rintro (h | h) + · exact nbl1 h + · exact n1_5 h + +theorem n8_4 : (8#u64) ≠ sched.TS_BLOCKED_ON_SEND := by unfold sched.TS_BLOCKED_ON_SEND; decide +theorem n8_3 : (8#u64) ≠ sched.TS_BLOCKED_ON_RECV := by unfold sched.TS_BLOCKED_ON_RECV; decide +theorem n8_6 : (8#u64) ≠ sched.TS_BLOCKED_ON_NTFN := by unfold sched.TS_BLOCKED_ON_NTFN; decide +theorem n8_5 : (8#u64) ≠ sched.TS_BLOCKED_ON_REPLY := by unfold sched.TS_BLOCKED_ON_REPLY; decide + +theorem nbr8 : ¬ BlockedR (8#u64) := by + rintro ((h | h | h) | h) + · exact n8_4 h + · exact n8_3 h + · exact n8_6 h + · exact n8_5 h + +theorem nblk_ww (t : U64) (ht : t ∈ Tw) : ¬ BlockedR (wL.mem ww (P1.stA t)) := by + rcases st_ww t ht with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] + · exact nbr7 + · exact nbr1 + · exact nbr8 + +theorem bound_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (boundA t) = 0#u64 := by + unfold boundA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem td_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (tdA t) = 0#u64 := by + unfold tdA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem tl_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (tlA t) = 0#u64 := by + unfold tlA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem tr_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (trA t) = 0#u64 := by + unfold trA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem tp_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (tpA t) = 0#u64 := by + unfold tpA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem dn_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (dnA t) = 0#u64 := by + unfold dnA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem dp_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (dpA t) = 0#u64 := by + unfold dpA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +/-- A zero word of either SC (not `TCB`, `REFILL_MAX`, the tail or the two refill words). -/ +theorem scZ1 {c : U64} (hc : c ∈ C1) (K : U64) (hK : K.val < 128) (h8 : K.val % 8 = 0) (h1 : K.val ≠ 24) + (h2 : K.val ≠ 72) (h3 : K.val ≠ 88) (h4 : K.val ≠ 104) (h5 : K.val ≠ 112) : + wL.mem ww (core.num.U64.wrapping_add c K) = 0#u64 := by + rcases memC1 hc with rfl | rfl + · rw [scW1_v K hK h8]; imgwz + · rw [rsW_v K hK h8]; imgwz + +theorem scTcb_ww {c : U64} (hc : c ∈ C1) : + (c = idS ∧ wL.mem ww (scTcbA c) = idT) ∨ (c = rS ∧ wL.mem ww (scTcbA c) = rT) := by + rcases memC1 hc with rfl | rfl + · exact Or.inl ⟨rfl, by unfold scTcbA layout.sc.TCB; rw [scW1_v _ (by decide) (by decide)]; decide⟩ + · exact Or.inr ⟨rfl, by unfold scTcbA layout.sc.TCB; rw [rsW_v _ (by decide) (by decide)]; decide⟩ + +theorem scCore_ww {c : U64} (hc : c ∈ C1) : wL.mem ww (scCoreA c) = 0#u64 := by + unfold scCoreA layout.sc.CORE + exact scZ1 hc _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem scReply1 {c : U64} (hc : c ∈ C1) : wL.mem ww (scReplyA c) = 0#u64 := by + unfold scReplyA layout.sc.REPLY + exact scZ1 hc _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem scNtfn1 {c : U64} (hc : c ∈ C1) : wL.mem ww (scNtfnA c) = 0#u64 := by + unfold scNtfnA layout.sc.NTFN + exact scZ1 hc _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem scYf1 {c : U64} (hc : c ∈ C1) : wL.mem ww (scYieldFromA c) = 0#u64 := by + unfold scYieldFromA layout.sc.YIELD_FROM + exact scZ1 hc _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +theorem rmax1 {c : U64} (hc : c ∈ C1) : wL.rmax ww c = 2#u64 := by + rcases memC1 hc with rfl | rfl + · exact rmax_idS1 + · exact rmax_rS + +theorem sc1_mem (t : U64) (ht : t ∈ Tw) (h : wL.mem ww (scA t) ≠ 0#u64) : + (t = idT ∧ wL.mem ww (scA t) = idS) ∨ (t = rT ∧ wL.mem ww (scA t) = rS) := by + rcases memTw ht with rfl | rfl | rfl + · exact Or.inl ⟨rfl, sc_idT1⟩ + · exact Or.inr ⟨rfl, sc_rT1⟩ + · exact absurd sc_gT h + +theorem sc1_in (t : U64) (ht : t ∈ Tw) (h : wL.mem ww (scA t) ≠ 0#u64) : wL.mem ww (scA t) ∈ C1 := by + rcases sc1_mem t ht h with ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] + · exact idS_mem + · exact rS_mem + +theorem sc_idne : wL.mem ww (scA idT) ≠ 0#u64 := by rw [sc_idT1]; decide +theorem sc_rne : wL.mem ww (scA rT) ≠ 0#u64 := by rw [sc_rT1]; decide + +theorem nodeZ1 (o : U64) (ho : o.val < 4288) (h8 : o.val % 8 = 0) (h0 : o.val ≠ 0) (h1 : o.val ≠ 8) + (h2 : o.val ≠ 24) (h3 : o.val ≠ 32) (h4 : o.val ≠ 48) : wL.mem ww (wL.nodeAt ww 0#u64 o) = 0#u64 := by + rw [show wL.mem ww (wL.nodeAt ww 0#u64 o) = nodeW wst ww 0#u64 o from rfl, nodeW1_v o ho h8]; imgwz + +theorem troot_ww : wL.mem ww (wL.nodeAt ww 0#u64 layout.node.TIMER_ROOT) = 0#u64 := by + unfold layout.node.TIMER_ROOT + exact nodeZ1 _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem tmin_ww : wL.mem ww (wL.nodeAt ww 0#u64 layout.node.TIMER_MIN) = 0#u64 := by + unfold layout.node.TIMER_MIN + exact nodeZ1 _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) +theorem dbg_ww : wL.mem ww (wL.nodeAt ww 0#u64 layout.node.DEBUG_TCBS) = 0#u64 := by + unfold layout.node.DEBUG_TCBS + exact nodeZ1 _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +theorem actW1 : wL.actionW ww = 0#u64 := act_ww + +/-! ## The scheduler queues -/ + +/-- **`SchedQueuesInv` at `ww`** (every queue empty). -/ +theorem ww_sqi : SchedQueuesInv wst Tw Csw (Heap.omega Hww) Hww ww (fun _ _ => []) (fun _ => []) where + tcbs := tcbPlaced_T1 + nodes c hc := by rw [memCs hc]; exact nodePlaced_ww + cur := rfl + aff t ht := by rw [aff_ww t ht]; rfl + prio := ww_schedCore.prio + ready c hc := by rw [memCs hc]; exact ww_schedCore.ready + release c hc := by rw [memCs hc]; exact ww_schedCore.release + inQ _ _ _ _ _ h := (List.not_mem_nil h).elim + inR _ _ _ h := (List.not_mem_nil h).elim + queued := ww_schedCore.queued + released := ww_schedCore.released + excl := ww_schedCore.excl + action := Or.inl actW1 + runQ _ _ _ _ _ h := (List.not_mem_nil h).elim + runR _ _ _ h := (List.not_mem_nil h).elim + actOK h := by rw [actW1] at h; rcases memTw h with e | e | e <;> exact absurd e (by decide) + eps := ⟨∅, fun _ => [], fun _ he => he.elim, fun _ he => he.elim, fun _ he => he.elim, fun _ he => he.elim, + fun t ht _ hb => absurd (Or.inl hb) (nblk_ww t ht), + fun t ht _ hs => by + rcases st_ww t ht with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] at hs + · rcases hs with h | h; exact absurd h n4; exact absurd h n3 + · rcases hs with h | h; exact absurd h n1_4; exact absurd h n1_3 + · rcases hs with h | h; exact absurd h n8_4; exact absurd h n8_3, + fun t ht _ hs => by + rcases st_ww t ht with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] at hs + · exact absurd hs n6 + · exact absurd hs n1_6 + · exact absurd hs n8_6, + fun e => ⟨fun he => he.elim, fun h => by + rcases h with h | h <;> rcases Hww_kind h with e | e | ⟨z, e⟩ <;> cases e⟩, + fun _ he => he.elim, fun _ he => he.elim, fun _ he => he.elim⟩ + curT := ww_schedCore.curT + idleT := ww_schedCore.idleT + ksOut := ww_schedCore.ksOut + omega a ha := by + obtain ⟨hal, hr⟩ := omega1_pos ha + refine ⟨hal, fun t ht => ?_, fun c hc => ?_⟩ + · rcases memTw ht with rfl | rfl | rfl + · rw [idT_val]; omega + · rw [rT_val]; omega + · rw [gT_val]; omega + · rw [memCs hc, nodeBase_ww]; omega + +/-- **`BoundInv` at `ww`.** -/ +theorem ww_bound : BoundInv wst Tw C1 ww where + q t ht h := absurd (by rw [inq_ww t ht]; simp) h + r t ht h := absurd (by rw [inq_ww t ht]; simp) h + act h := by rw [actW1] at h; rcases memTw h with e | e | e <;> exact absurd e (by decide) + idle := by rw [idleW_ww]; exact sc_idne + resume _ := by rw [curW_ww]; exact sc_rne + idleNB := by rw [idleW_ww]; exact nblk_ww idT idT_mem + sact t ht _ h := sc1_in t ht h + +/-! ## The objects -/ + +/-- **`ObjsOK` at `ww`** (two SCs, no reply, no notification). -/ +theorem ww_objs : ObjsOK wst (Heap.omega Hww) (Heap.frame Hww) ∅ C1 ∅ (Heap.scBits Hww) ww where + rp _ h := h.elim + sc c hc := by + rw [scBits1 c hc] + rcases memC1 hc with rfl | rfl + · exact ⟨⟨by decide, by decide, by decide⟩, le_refl _⟩ + · exact ⟨⟨by decide, by decide, by decide⟩, le_refl _⟩ + rpW _ h := h.elim + scW c hc b hb hin := by + rw [scBits1 c hc] at hin + exact Heap.frame_omega (frame1_of hc hb hin.1 (by have := hin.2; simp only [Nat.reducePow] at this; omega)) + fixW c hc := ⟨by unfold scTcbA layout.sc.TCB; exact scww_frame hc _ (by decide) (by decide), + by unfold scCoreA layout.sc.CORE; exact scww_frame hc _ (by decide) (by decide)⟩ + scDisj c hc c' hc' hne := by + rw [scBits1 c hc, scBits1 c' hc'] + rcases memC1 hc with rfl | rfl <;> rcases memC1 hc' with rfl | rfl + · exact absurd rfl hne + · rw [idS_val, rS_val]; decide + · rw [idS_val, rS_val]; decide + · exact absurd rfl hne + rpSc _ h := h.elim + repW c hc := by unfold scReplyA layout.sc.REPLY; exact scww_frame hc _ (by decide) (by decide) + ntW _ h := h.elim + ntRp _ h := h.elim + ntSc _ h := h.elim + ntAl _ h := h.elim + ntP _ h := h.elim + ntB _ h := h.elim + ntORp _ h := h.elim + ntOSc _ h := h.elim + snW c hc := by unfold scNtfnA layout.sc.NTFN; exact scww_frame hc _ (by decide) (by decide) + syW c hc := by unfold scYieldFromA layout.sc.YIELD_FROM; exact scww_frame hc _ (by decide) (by decide) + mxW c hc := by unfold maxA layout.sc.REFILL_MAX; exact scww_frame hc _ (by decide) (by decide) + nt0 _ h := h.elim + rp0 _ h := h.elim + sc0 c hc := by rcases memC1 hc with rfl | rfl <;> decide + +/-- **`ReplyInv` at `ww`.** -/ +theorem ww_reply : ReplyInv wst Tw ∅ C1 ww where + reply t ht h := by + rcases st_ww t ht with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] at h + · exact absurd h n5 + · exact absurd h n1_5 + · exact absurd h n8_5 + recv t ht h := by + rcases st_ww t ht with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] at h + · exact absurd h n3 + · exact absurd h n1_3 + · exact absurd h n8_3 + next _ h := h.elim + prev _ h := h.elim + back _ h := h.elim + screp c hc h := by rw [scReply1 hc] at h; exact absurd rfl h + idle c hc _ := by rw [scReply1 hc] + head _ h := h.elim + +/-- **P1's `ScExtra` at `ww`.** -/ +theorem ww_scx : P1.ScExtra wst Tw Csw C1 ∅ ww where + bound t ht h := by + refine ⟨sc1_in t ht h, ?_, by rw [scCore_ww (sc1_in t ht h)]; rfl⟩ + rcases sc1_mem t ht h with ⟨rfl, e⟩ | ⟨rfl, e⟩ <;> rw [e] + · rcases scTcb_ww idS_mem with ⟨-, e'⟩ | ⟨e'', -⟩ + · exact e' + · exact absurd e'' (by decide) + · rcases scTcb_ww rS_mem with ⟨e'', -⟩ | ⟨-, e'⟩ + · exact absurd e'' (by decide) + · exact e' + cur := by rw [cscW_ww]; exact rS_mem + core c hc := by rw [scCore_ww hc]; rfl + bnd t ht h := by rw [bound_ww t ht] at h; exact absurd rfl h + nsc _ h := h.elim + tm := { + npl := fun c hc => by rw [memCs hc]; exact nodePlaced_ww + lt := fun y hy => by rw [tl_ww y hy]; exact Or.inl rfl + rt := fun y hy => by rw [tr_ww y hy]; exact Or.inl rfl + pt := fun y hy => by rw [tp_ww y hy]; exact Or.inl rfl + root := fun c hc => by rw [memCs hc, troot_ww]; exact Or.inl rfl + min := fun c hc => by rw [memCs hc, tmin_ww]; exact Or.inl rfl + core := fun y hy h => by rw [td_ww y hy] at h; exact absurd rfl h + dn := fun y hy => by rw [dn_ww y hy]; exact Or.inl rfl + dp := fun y hy => by rw [dp_ww y hy]; exact Or.inl rfl + dh := fun c hc => by rw [memCs hc, dbg_ww]; exact Or.inl rfl } + ntb _ h := h.elim + rb := { + tree := fun c hc => by + rw [memCs hc] + exact ⟨.leaf, { + rep := TRB.Rep.leaf _ _ troot_ww + nodup := List.nodup_nil + inT := fun _ h => (List.not_mem_nil h).elim + armed := fun _ h => (List.not_mem_nil h).elim + core := fun _ h => (List.not_mem_nil h).elim + sorted := List.Pairwise.nil + bal := ⟨0, TRB.Bal.leaf⟩ + black := rfl + min := tmin_ww }⟩ + armed := fun y hy h => by rw [td_ww y hy] at h; exact absurd rfl h + idle := fun y hy _ => by rw [tl_ww y hy, tr_ww y hy, tp_ww y hy]; exact ⟨rfl, rfl, rfl⟩ } + scn c hc h := by rw [scNtfn1 hc] at h; exact absurd rfl h + yt := ⟨fun c hc _ => by + rcases scTcb_ww hc with ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] + · exact idT_mem + · exact rT_mem, + fun t ht h => by rw [yield_ww t ht] at h; exact absurd rfl h, + fun c hc h => by rw [scYf1 hc] at h; exact absurd rfl h⟩ + bs := { + tb := fun t ht h => by rw [bound_ww t ht] at h; exact absurd rfl h + nt := fun _ h => h.elim + sn := fun c hc _ h => by rw [scNtfn1 hc] at h; exact absurd rfl h } + tsc c hc _ _ := by + rcases scTcb_ww hc with ⟨rfl, e⟩ | ⟨rfl, e⟩ <;> rw [e] + · exact sc_idT1 + · exact sc_rT1 + aff t ht h := by rw [scCore_ww (sc1_in t ht h), aff_ww t ht] + +/-- **`SCsOK` at `ww`.** -/ +theorem ww_scsok : SCsOK wst Tw (Heap.omega Hww) (Heap.frame Hww) C1 (Heap.scBits Hww) C1 ww where + sub _ hb := Heap.frame_omega hb + ks b hb := by have := frame1_pos hb; left; rw [ksb_ww, fkb_val]; omega + ksΩ := ks1_omega + disj t ht h1 h2 b hb := by + rw [cscW_ww] at h2 ⊢ + rcases sc1_mem t ht h1 with ⟨rfl, e⟩ | ⟨rfl, e⟩ <;> rw [e] at h2 hb + · rw [scBits1 _ idS_mem] at hb + rw [scBits1 _ rS_mem] + unfold InSC at hb; unfold Outside + rw [idS_val] at hb; rw [rS_val]; simp only [Nat.reducePow] at hb ⊢; omega + · exact absurd rfl h2 + all c hc := by + rw [scBits1 c hc] + refine ⟨fun _ => ?_, by unfold maxA layout.sc.REFILL_MAX; exact scww_frame hc _ (by decide) (by decide), + fun b hb hin => frame1_of hc hb hin.1 (by have := hin.2; simp only [Nat.reducePow] at this; omega)⟩ + rcases memC1 hc with rfl | rfl + · exact wf_idS1 + · exact wf_rS + act c hc := ⟨fun _ => by rw [rmax1 hc]; decide, fun _ => hc⟩ + spor c hc _ := hc + spSg c hc := by unfold sporA layout.sc.SPORADIC; exact scww_frame hc _ (by decide) (by decide) + rep c hc _ := hc + repSg c hc := by unfold scReplyA layout.sc.REPLY; exact scww_frame hc _ (by decide) (by decide) + scKs c hc := by + rw [scBits1 c hc]; left; rw [ksb_ww, fkb_val] + rcases memC1 hc with rfl | rfl + · rw [idS_val]; decide + · rw [rS_val]; decide + +/-- **`SgOK` at `ww`.** -/ +theorem ww_sgok : SgOK wst (Heap.omega Hww) (Heap.frame Hww) ww where + sub _ hb := Heap.frame_omega hb + ks b hb := by have := frame1_pos hb; left; rw [ksb_ww, fkb_val]; omega + ksΩ := ks1_omega + +/-- **`JAtZ.blk` at `ww`: P1's `BlkOK`.** -/ +theorem ww_blk : P1.BlkOK wst (P1.Heap.tcbs Hww) Csw (P1.Heap.omega Hww) Hww (P1.Heap.frame Hww) + (P1.Heap.replies Hww) (P1.Heap.scs Hww) (P1.Heap.ntfns Hww) (P1.Heap.scBits Hww) C1 ww := by + rw [tcbs_Hww, replies_Hww, scs_Hww, ntfns_Hww] + exact ⟨⟨_, _, ww_sqi⟩, ww_bound, ⟨ww_objs, ww_reply, ww_scx⟩, ww_scsok, ww_sgok⟩ + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitCs.lean b/proofs/Fl4Proofs/P3/VmWitCs.lean new file mode 100644 index 000000000..0c49ea808 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitCs.lean @@ -0,0 +1,787 @@ +/- +P3 (line mmuAD witness): **the CSpace half at `ww` / `σw`** (P3/VmWitZ): the cap ranges, the cap relation `CRel` +and `FinInv` over the radix-3 root CNode and both TCBs, the untyped-cap facts (`UtCapP3` at the untyped cap of +`uB`), the MDB side invariants and the VSpace conjuncts. Generated by `handoff/tools/gen_witcs.py`. + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitZ +import Fl4Proofs.P3.JBootQ1Cs +import Fl4Proofs.P3.VirtLoopFrame +import Fl4Proofs.P3.MdbInsert +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif Fl4Proofs.P2.Wit Classical + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem uB_val : uB.val = 18446744071562117120 := rfl +theorem rS_toNat' : rS.bv.toNat = 18446744071562104832 := rfl + +/-! ## The spec objects' extents -/ + +/-- Every boot object lies below the root TCB or inside the kernel-state block. -/ +theorem finit_ext {p : ObjRef} {o : KernelObject} (h : fInitKheap p = some o) : + p.toNat + 2 ^ Fl4Spec.objBits o ≤ 18446744071562100736 ∨ + (18446744071562133504 ≤ p.toNat ∧ p.toNat + 2 ^ Fl4Spec.objBits o ≤ 18446744071562133504 + 13568) := by + unfold fInitKheap at h + simp only [Function.update_apply] at h + split_ifs at h with h1 h2 h3 h4 h5 h6 + · rw [initKheap_pd] at h; cases h; rw [h1]; left; decide + · rw [initKheap_pdpt] at h; cases h; rw [h2]; left; decide + · rw [initKheap_pml4] at h; cases h; rw [h3]; left; decide + · rw [initKheap_idleS] at h; cases h; rw [h4]; left; decide + · rw [initKheap_idleT] at h; cases h; rw [h5]; left; decide + · cases h + obtain ⟨irq, e⟩ := h6 + have e5 : Fl4Spec.objBits (.cnode 0 (emptyCnode 0)) = 5 := by decide + rw [e5, ← e] + rcases fIrqNode_cases irq with ⟨_, h2⟩ | ⟨_, _, h3⟩ + · right; omega + · left; omega + +/-- Every spec object of `σw` is the VCPU, or lies below the Untyped region or inside the kernel-state block. -/ +theorem obj_ext {p : ObjRef} {o : KernelObject} (h : σw.kheap p = some o) : + (p = vB.bv ∧ o = .archObj (.vcpu vcpuW)) ∨ p.toNat + 2 ^ Fl4Spec.objBits o ≤ 18446744071562117120 ∨ + (18446744071562133504 ≤ p.toNat ∧ p.toNat + 2 ^ Fl4Spec.objBits o ≤ 18446744071562133504 + 13568) := by + rw [σw_kheap] at h + rcases kh_some h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h5 | h' + · right; left; decide + · right; left; decide + · right; left + have e : Fl4Spec.objBits (.cnode 3 wCs) = 8 := by simp [Fl4Spec.objBits, Fl4Spec.cteLevelBits] + rw [e, rC_toNat]; decide + · right; left; decide + · exact Or.inl h5 + · rcases finit_ext h' with h1 | h1 + · right; left; omega + · exact Or.inr (Or.inr h1) + +/-! ## The caps' ranges -/ + +theorem physF_scC : physF scC = true := ok_inj (phys_eq _).symm +theorem physF_cnC : physF cnC = true := physF_cnode _ _ _ _ +theorem physF_thC : physF thC = true := physF_thread _ +theorem physF_gthC : physF gthC = true := physF_thread _ +theorem physF_utC : physF utC = true := ok_inj (phys_eq _).symm +theorem physF_vcC : physF vcC = true := ok_inj (phys_eq _).symm +theorem physF_sctlC : physF sctlC = false := ok_inj (phys_eq _).symm + +theorem sizeF_cnC : (sizeF cnC).val = 8 := by + rw [sizeF_of (c := cnC) (v := core.num.U64.wrapping_add 3#u64 fl4_abi.consts.bits.SLOT) rfl] + unfold fl4_abi.consts.bits.SLOT; decide +theorem sizeF_thC : (sizeF thC).val = 12 := by + rw [sizeF_of (c := thC) (v := fl4_abi.consts.bits.TCB) rfl] + unfold fl4_abi.consts.bits.TCB; decide +theorem sizeF_gthC : (sizeF gthC).val = 12 := by + rw [sizeF_of (c := gthC) (v := fl4_abi.consts.bits.TCB) rfl] + unfold fl4_abi.consts.bits.TCB; decide +theorem sizeF_scC : (sizeF scC).val = 7 := by + rw [sizeF_of (c := scC) (v := 7#u64) rfl]; rfl +theorem sizeF_utC : (sizeF utC).val = 12 := by + rw [show sizeF utC = 12#u64 from ok_inj (size_eq _).symm]; rfl +theorem sizeF_vcC : (sizeF vcC).val = 15 := by + rw [Fl4Proofs.P3.VirtLoopFrame.sizeF_vcpu]; rfl + +theorem ptrF_scC : ptrF scC = rS := ok_inj (ptr_eq _).symm +theorem ptrF_utC : ptrF utC = uB := ok_inj (ptr_eq _).symm +theorem ptrF_gthC : ptrF gthC = gT := ptrF_thread _ +theorem ptrF_vcC : ptrF vcC = vB := ok_inj (ptr_eq _).symm + +theorem topF_v {c : capability.Cap} {n : Nat} (hs : (sizeF c).val = n) (hn : n < 64) + (hf : (ptrF c).val + 2 ^ n ≤ 2 ^ 64) : topF c = (ptrF c).val + 2 ^ n - 1 := by + unfold topF + have hm := maskF_val (b := sizeF c) (by omega) + rw [hs] at hm + have h1 : 1 ≤ 2 ^ n := Nat.one_le_two_pow + rw [wa_val (by rw [hm]; omega), hm]; omega + +theorem base_cnC : baseF cnC = 18446744071562108928 := by unfold baseF; rw [ptrF_cnode]; rfl +theorem top_cnC : topF cnC = 18446744071562108928 + 255 := by + rw [topF_v sizeF_cnC (by omega) (by rw [ptrF_cnode]; decide), ptrF_cnode]; rfl +theorem base_thC : baseF thC = 18446744071562100736 := by unfold baseF; rw [ptrF_thread]; rfl +theorem top_thC : topF thC = 18446744071562100736 + 4095 := by + rw [topF_v sizeF_thC (by omega) (by rw [ptrF_thread]; decide), ptrF_thread]; rfl +theorem base_scC : baseF scC = 18446744071562104832 := by unfold baseF; rw [ptrF_scC]; rfl +theorem top_scC : topF scC = 18446744071562104832 + 127 := by + rw [topF_v sizeF_scC (by omega) (by rw [ptrF_scC]; decide), ptrF_scC]; rfl +theorem base_utC : baseF utC = 18446744071562117120 := by unfold baseF; rw [ptrF_utC]; rfl +theorem top_utC : topF utC = 18446744071562117120 + 4095 := by + rw [topF_v sizeF_utC (by omega) (by rw [ptrF_utC]; decide), ptrF_utC]; rfl +theorem base_gthC : baseF gthC = 18446744071562113024 := by unfold baseF; rw [ptrF_gthC]; rfl +theorem top_gthC : topF gthC = 18446744071562113024 + 4095 := by + rw [topF_v sizeF_gthC (by omega) (by rw [ptrF_gthC]; decide), ptrF_gthC]; rfl +theorem base_vcC : baseF vcC = 18446744071563116544 := by unfold baseF; rw [ptrF_vcC]; rfl +theorem top_vcC : topF vcC = 18446744071563116544 + 32767 := by + rw [topF_v sizeF_vcC (by omega) (by rw [ptrF_vcC]; decide), ptrF_vcC]; rfl + +theorem srB_cnC : srB cnC cnC = true := by + have e := srB_eq cnC cnC + rw [show objects.same_region_as cnC cnC = ok true by + unfold objects.same_region_as; simp [capability.Cap.ptr, objects.cap_size_bits, objects.cap_is_physical] <;> rfl] at e + exact (ok_inj e).symm +theorem srB_thC : srB thC thC = true := by + have e := srB_eq thC thC + rw [show objects.same_region_as thC thC = ok true by + unfold objects.same_region_as; simp [capability.Cap.ptr, objects.cap_size_bits, objects.cap_is_physical] <;> rfl] at e + exact (ok_inj e).symm +theorem srB_scC : srB scC scC = true := by + have e := srB_eq scC scC + rw [show objects.same_region_as scC scC = ok true by + unfold objects.same_region_as; simp [capability.Cap.ptr, objects.cap_size_bits, objects.cap_is_physical] <;> rfl] at e + exact (ok_inj e).symm +theorem srB_utC : srB utC utC = true := + (srB_ut_iff (a := utC) rfl utC).2 ⟨physF_utC, ⟨le_rfl, le_rfl⟩, by rw [base_utC, top_utC]; omega⟩ +theorem srB_sctlC : srB sctlC sctlC = true := by + have e := srB_eq sctlC sctlC + rw [show objects.same_region_as sctlC sctlC = ok true by + unfold objects.same_region_as; simp [capability.Cap.ptr, objects.cap_size_bits, objects.cap_is_physical] <;> rfl] at e + exact (ok_inj e).symm +theorem srB_gthC : srB gthC gthC = true := by + have e := srB_eq gthC gthC + rw [show objects.same_region_as gthC gthC = ok true by + unfold objects.same_region_as; simp [capability.Cap.ptr, objects.cap_size_bits, objects.cap_is_physical] <;> rfl] at e + exact (ok_inj e).symm +theorem srB_vcC : srB vcC vcC = true := by + have e := srB_eq vcC vcC + rw [show objects.same_region_as vcC vcC = ok true by + unfold objects.same_region_as; simp [capability.Cap.ptr, objects.cap_size_bits, objects.cap_is_physical] <;> rfl] at e + exact (ok_inj e).symm + +/-! ## The MDB members -/ + +theorem flatw {x : U64} (hx : x ∈ Gw.chains.flatten) : + (x = rT ∧ capV (wL.mem ww) x = cnC) ∨ (x = rC0 ∧ capV (wL.mem ww) x = thC) ∨ (x = rC1 ∧ capV (wL.mem ww) x = scC) ∨ (x = rC2 ∧ capV (wL.mem ww) x = utC) ∨ (x = rC3 ∧ capV (wL.mem ww) x = sctlC) ∨ (x = rC4 ∧ capV (wL.mem ww) x = gthC) ∨ (x = rC5 ∧ capV (wL.mem ww) x = vcC) := by + rcases Gw_flat.1 hx with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · exact Or.inl ⟨rfl, capV_rT⟩ + · exact Or.inr (Or.inl ⟨rfl, capV_rC0⟩) + · exact Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC1⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC2⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC3⟩)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC4⟩))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (⟨rfl, capV_rC5⟩)))))) + +theorem slot_of_flat {x : U64} (hx : x ∈ Gw.chains.flatten) : IsSlot thWw x := by + rcases Gw_flat.1 hx with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · exact isSlot_rT + · exact isSlot_rC0 + · exact isSlot_rC1 + · exact isSlot_rC2 + · exact isSlot_rC3 + · exact isSlot_rC4 + · exact isSlot_rC5 + +theorem links_mem {G : MdbGhost} (hG : MdbLinks thWw (wL.mem ww) G) {x : U64} (hx : x ∈ G.chains.flatten) : + (x = rT ∧ capV (wL.mem ww) x = cnC) ∨ (x = rC0 ∧ capV (wL.mem ww) x = thC) ∨ (x = rC1 ∧ capV (wL.mem ww) x = scC) ∨ (x = rC2 ∧ capV (wL.mem ww) x = utC) ∨ (x = rC3 ∧ capV (wL.mem ww) x = sctlC) ∨ (x = rC4 ∧ capV (wL.mem ww) x = gthC) ∨ (x = rC5 ∧ capV (wL.mem ww) x = vcC) := by + obtain ⟨hs, c, hc, hne⟩ := (hG.members x).1 hx + rw [capAt_capV] at hc + rcases slotw_cases hs with h0 | h1 | h2 | h3 | h4 | h5 | h6 | h + · exact Or.inl h0 + · exact Or.inr (Or.inl h1) + · exact Or.inr (Or.inr (Or.inl h2)) + · exact Or.inr (Or.inr (Or.inr (Or.inl h3))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h4)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h5))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (h6)))))) + · rw [h] at hc; exact absurd (ok_inj hc).symm hne + +theorem not_ovl_of {a b : Fl4Spec.Cap} (h : ∀ g, g ∈ genObjRefs a → g ∈ genObjRefs b → False) : ¬ Ovl a b := + fun h' => by obtain ⟨g, hg1, hg2⟩ := Set.nonempty_iff_ne_empty.2 h'; exact h g hg1 hg2 + +/-- Two caps naming different objects do not overlap. -/ +macro "novl" : tactic => `(tactic| exact not_ovl_of (fun g h1 h2 => by + simp [genObjRefs, objRefs, aobjRef, capIrqs, archGenRefs, archGenObjRefs, absCap, capIrqOpt, archCapSetMap] at h1 h2 + try rw [h1] at h2 + try simp [rT, rS, rC, gT, vB] at h2)) + +theorem ovl_same {x y : U64} + (hx : (x = rT ∧ capV (wL.mem ww) x = cnC) ∨ (x = rC0 ∧ capV (wL.mem ww) x = thC) ∨ (x = rC1 ∧ capV (wL.mem ww) x = scC) ∨ (x = rC2 ∧ capV (wL.mem ww) x = utC) ∨ (x = rC3 ∧ capV (wL.mem ww) x = sctlC) ∨ (x = rC4 ∧ capV (wL.mem ww) x = gthC) ∨ (x = rC5 ∧ capV (wL.mem ww) x = vcC)) + (hy : (y = rT ∧ capV (wL.mem ww) y = cnC) ∨ (y = rC0 ∧ capV (wL.mem ww) y = thC) ∨ (y = rC1 ∧ capV (wL.mem ww) y = scC) ∨ (y = rC2 ∧ capV (wL.mem ww) y = utC) ∨ (y = rC3 ∧ capV (wL.mem ww) y = sctlC) ∨ (y = rC4 ∧ capV (wL.mem ww) y = gthC) ∨ (y = rC5 ∧ capV (wL.mem ww) y = vcC)) + (h : Ovl (absCap (capV (wL.mem ww) x)) (absCap (capV (wL.mem ww) y))) : x = y := by + rcases hx with ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ <;> rcases hy with ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ <;> + simp only [ex, ey] at h <;> first | rfl | exact absurd h (by novl) + +theorem chain_uniq {L : List (List U64)} (h : L.flatten.Nodup) {a b : List U64} (ha : a ∈ L) (hb : b ∈ L) + {x : U64} (hxa : x ∈ a) (hxb : x ∈ b) : a = b := by + induction L with + | nil => cases ha + | cons c L ih => + rw [List.flatten_cons, List.nodup_append] at h + obtain ⟨-, h2, h3⟩ := h + rcases List.mem_cons.1 ha with rfl | ha' <;> rcases List.mem_cons.1 hb with rfl | hb' + · rfl + · exact absurd (List.mem_flatten.2 ⟨b, hb', hxb⟩) (fun hm => by + first | exact h3 x hxa hm | exact h3 x hxa x hm rfl | exact h3 hxa hm) + · exact absurd (List.mem_flatten.2 ⟨a, ha', hxa⟩) (fun hm => by + first | exact h3 x hxb hm | exact h3 x hxb x hm rfl | exact h3 hxb hm) + · exact ih h2 ha' hb' + +/-- **`FinM` at every tree of the slots.** -/ +theorem ww_finM : ∀ G, MdbLinks thWw (wL.mem ww) G → FinM (wL.mem ww) G := by + intro G hG + refine ⟨fun ch1 h1 ch2 h2 x hx y hy ho => ?_, fun x hx y hy hne ho _ _ _ => ?_, fun x hx y hy _ hf => ?_, + fun x hx y hy _ hz => ?_⟩ + · have hxf := List.mem_flatten.2 ⟨ch1, h1, hx⟩ + have hyf := List.mem_flatten.2 ⟨ch2, h2, hy⟩ + have e := ovl_same (links_mem hG hxf) (links_mem hG hyf) ho + subst e + exact chain_uniq hG.tree.nodup h1 h2 hx hy + · exact absurd (ovl_same (links_mem hG hx) (links_mem hG hy) ho) hne + · rcases links_mem hG hx with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] at hf <;> cases hf + · rcases links_mem hG hx with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] at hz <;> cases hz + +/-! ## `CRel` -/ + +theorem cs_rT0 : cslotAddr (rT.bv, tcbCnodeIndex 0) = rT := by decide +theorem cs_rC0 : cslotAddr (rC.bv, [false, false, false]) = rC0 := by decide +theorem cs_rC1 : cslotAddr (rC.bv, [false, false, true]) = rC1 := by decide +theorem cs_rC2 : cslotAddr (rC.bv, [false, true, false]) = rC2 := by decide +theorem cs_rC3 : cslotAddr (rC.bv, [false, true, true]) = rC3 := by decide +theorem cs_rC4 : cslotAddr (rC.bv, [true, false, false]) = rC4 := by decide +theorem cs_rC5 : cslotAddr (rC.bv, [true, false, true]) = rC5 := by decide +def rC6 : U64 := 18446744071562109120#u64 +def rC7 : U64 := 18446744071562109152#u64 +theorem cs_rC6 : cslotAddr (rC.bv, [true, true, false]) = rC6 := by decide +theorem cs_rC7 : cslotAddr (rC.bv, [true, true, true]) = rC7 := by decide + +theorem root_map (i : Nat) (hi : i < 6) : + tcbCnodeMap rootTcbW (tcbCnodeIndex i) = some (if i = 0 then cnW else .nullCap) := by + unfold tcbCnodeMap + rcases (by omega : i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 ∨ i = 4 ∨ i = 5) with rfl | rfl | rfl | rfl | rfl | rfl + · rw [Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_of_ne (by decide), + Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_self]; rfl + · rw [Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_of_ne (by decide), + Function.update_of_ne (by decide), Function.update_self]; rfl + · rw [Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_of_ne (by decide), + Function.update_self]; rfl + · rw [Function.update_of_ne (by decide), Function.update_of_ne (by decide), Function.update_self]; rfl + · rw [Function.update_of_ne (by decide), Function.update_self]; rfl + · rw [Function.update_self]; rfl + +theorem capRel_cnC : CapRel cnC cnW := ⟨⟨by decide, by decide, by decide⟩, rfl⟩ +theorem capRel_thC : CapRel thC (.threadCap rT.bv) := ⟨trivial, rfl⟩ +theorem capRel_scC : CapRel scC (.schedContextCap rS.bv 0) := ⟨show minSchedContextBits ≤ 7 by decide, rfl⟩ +theorem capRel_utC : CapRel utC utW := ⟨trivial, rfl⟩ +theorem capRel_sctlC : CapRel sctlC .schedControlCap := ⟨trivial, rfl⟩ +theorem capRel_gthC : CapRel gthC (.threadCap gT.bv) := ⟨trivial, rfl⟩ +theorem capRel_vcC : CapRel vcC vcW := ⟨trivial, rfl⟩ + +theorem slotOf_rT (i : Nat) (hi : i < 6) : (slotOf rT i).val = 18446744071562100736 + 32 * i := + slotOf_val (by rw [rT_val]; omega) + +theorem slotOf_gT (i : Nat) (hi : i < 6) : (slotOf gT i).val = 18446744071562113024 + 32 * i := + slotOf_val (by rw [gT_val]; omega) + +theorem slotOf_idT (i : Nat) (hi : i < 6) : (slotOf idT i).val = 18446744071562072064 + 32 * i := + slotOf_val (by rw [idT_val]; omega) + +theorem ne_of_val {a b : U64} (h : a.val ≠ b.val) : a ≠ b := fun e => h (by rw [e]) + +/-- A slot whose address is none of the seven is null. -/ +theorem capVw_val {x : U64} (h : IsSlot thWw x) (n1 : x.val ≠ 18446744071562100736) (n2 : x.val ≠ 18446744071562108928) (n3 : x.val ≠ 18446744071562108960) (n4 : x.val ≠ 18446744071562108992) (n5 : x.val ≠ 18446744071562109024) (n6 : x.val ≠ 18446744071562109056) (n7 : x.val ≠ 18446744071562109088) : + capV (wL.mem ww) x = .Null := + capVw_slot h (ne_of_val (by rw [rT_val]; exact n1)) (ne_of_val (by rw [rC0_val]; exact n2)) (ne_of_val (by rw [rC1_val]; exact n3)) (ne_of_val (by rw [rC2_val]; exact n4)) (ne_of_val (by rw [rC3_val]; exact n5)) (ne_of_val (by rw [rC4_val]; exact n6)) (ne_of_val (by rw [rC5_val]; exact n7)) + +theorem ww_stateRel : StateRel thWw (wL.mem ww) σw where + cnode p r hp := by + rcases thWw_some hp with ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, e⟩ | ⟨n1, n2, n3, hp'⟩ + · cases e + · cases e + · cases e + refine ⟨wCs, kh_rC, wCs_wf, fun idx hidx => ?_⟩ + match idx, hidx with + | [false, false, false], _ => exact ⟨thC, _, by rw [show slotAddr rC.bv (Fl4Spec.ofBl [false, false, false]) = rC0 from cs_rC0, capAt_capV, + capV_rC0], rfl, capRel_thC⟩ + | [false, false, true], _ => exact ⟨scC, _, by rw [show slotAddr rC.bv (Fl4Spec.ofBl [false, false, true]) = rC1 from cs_rC1, capAt_capV, + capV_rC1], rfl, capRel_scC⟩ + | [false, true, false], _ => exact ⟨utC, _, by rw [show slotAddr rC.bv (Fl4Spec.ofBl [false, true, false]) = rC2 from cs_rC2, capAt_capV, + capV_rC2], rfl, capRel_utC⟩ + | [false, true, true], _ => exact ⟨sctlC, _, by rw [show slotAddr rC.bv (Fl4Spec.ofBl [false, true, true]) = rC3 from cs_rC3, capAt_capV, + capV_rC3], rfl, capRel_sctlC⟩ + | [true, false, false], _ => exact ⟨gthC, _, by rw [show slotAddr rC.bv (Fl4Spec.ofBl [true, false, false]) = rC4 from cs_rC4, capAt_capV, + capV_rC4], rfl, capRel_gthC⟩ + | [true, false, true], _ => exact ⟨vcC, _, by rw [show slotAddr rC.bv (Fl4Spec.ofBl [true, false, true]) = rC5 from cs_rC5, capAt_capV, + capV_rC5], rfl, capRel_vcC⟩ + | [true, true, false], _ => + have hs : IsSlot thWw rC6 := ⟨rC, 6, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ + refine ⟨.Null, .nullCap, ?_, rfl, ⟨trivial, rfl⟩⟩ + rw [show slotAddr rC.bv (Fl4Spec.ofBl [true, true, false]) = rC6 from cs_rC6, capAt_capV, + capVw_val hs (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)] + | [true, true, true], _ => + have hs : IsSlot thWw rC7 := ⟨rC, 7, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ + refine ⟨.Null, .nullCap, ?_, rfl, ⟨trivial, rfl⟩⟩ + rw [show slotAddr rC.bv (Fl4Spec.ofBl [true, true, true]) = rC7 from cs_rC7, capAt_capV, + capVw_val hs (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)] + · rcases thW_some hp' with ⟨-, e⟩ | ⟨irq, e, ek⟩ + · cases e + cases ek + have hk : σw.kheap p.bv = some (.cnode 0 (emptyCnode 0)) := by + rw [σw_kheap, kh_old (fun h => n1 (UScalar.eq_of_val_eq (congrArg BitVec.toNat h))) + (fun h => (rS_irq irq) (e.trans h)) (fun h => n3 (UScalar.eq_of_val_eq (congrArg BitVec.toNat h))) + (fun h => n2 (UScalar.eq_of_val_eq (congrArg BitVec.toNat h))) (fun h => (vB_irq irq) (e.trans h))] + have h0 := fInitKheap_irq irq + rwa [e] at h0 + refine ⟨emptyCnode 0, hk, emptyCnode0_wf, fun idx hidx => ?_⟩ + have hnil : idx = [] := List.length_eq_zero_iff.1 hidx + subst hnil + have hs : IsSlot thWw (slotOf p 0) := ⟨p, 0, Or.inl ⟨0, hp, by norm_num⟩, rfl⟩ + have hv : (slotOf p 0).val = p.val := by rw [IsSlotAt.val placed_thWw (Or.inl ⟨0, hp, by norm_num⟩)]; rfl + have hne : p.val ≠ 18446744071562100736 ∧ p.val ≠ 18446744071562108928 ∧ p.val ≠ 18446744071562108960 ∧ p.val ≠ 18446744071562108992 ∧ p.val ≠ 18446744071562109024 ∧ p.val ≠ 18446744071562109056 ∧ p.val ≠ 18446744071562109088 := by + rcases thW_pos hp' with ⟨-, e'⟩ | ⟨-, ⟨j, hj, hpv⟩ | ⟨j, hj, hj2, hpv⟩⟩ + · cases e' + · omega + · omega + obtain ⟨h1, h2, h3, h4, h5, h6, h7⟩ := hne + refine ⟨.Null, .nullCap, ?_, by simp [emptyCnode], ⟨trivial, rfl⟩⟩ + rw [show slotAddr p.bv (Fl4Spec.ofBl []) = slotOf p 0 from rfl, capAt_capV, + capVw_val hs (by rw [hv]; exact h1) (by rw [hv]; exact h2) (by rw [hv]; exact h3) (by rw [hv]; exact h4) + (by rw [hv]; exact h5) (by rw [hv]; exact h6) (by rw [hv]; exact h7)] + tcb t ht := by + rcases thWw_some ht with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, -, -, ht'⟩ + · refine ⟨rootTcbW, kh_rT, fun i hi => ?_⟩ + have hi6 : i < 6 := by unfold tcbSlots at hi; omega + rw [root_map i hi6] + by_cases h0 : i = 0 + · subst h0 + exact ⟨cnC, cnW, by rw [show slotAddr rT.bv (BitVec.ofNat 64 0) = rT by decide, capAt_capV, capV_rT], + by rw [if_pos rfl], capRel_cnC⟩ + · have hs : IsSlot thWw (slotOf rT i) := ⟨rT, i, Or.inr ⟨thWw_rT, hi⟩, rfl⟩ + have hv := slotOf_rT i hi6 + refine ⟨.Null, .nullCap, ?_, by rw [if_neg h0], ⟨trivial, rfl⟩⟩ + rw [show slotAddr rT.bv (BitVec.ofNat 64 i) = slotOf rT i from rfl, capAt_capV, + capVw_val hs (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) + (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega)] + · refine ⟨guestTcbV, kh_gT, fun i hi => ?_⟩ + have hi6 : i < 6 := by unfold tcbSlots at hi; omega + have hs : IsSlot thWw (slotOf gT i) := ⟨gT, i, Or.inr ⟨thWw_gT, hi⟩, rfl⟩ + have hv := slotOf_gT i hi6 + refine ⟨.Null, .nullCap, ?_, idle_map i hi6, ⟨trivial, rfl⟩⟩ + rw [show slotAddr gT.bv (BitVec.ofNat 64 i) = slotOf gT i from rfl, capAt_capV, + capVw_val hs (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) + (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega)] + · cases e + · rcases thW_some ht' with ⟨rfl, -⟩ | ⟨_, _, e⟩ + · refine ⟨idleTcbV, σw_idleTcb, fun i hi => ?_⟩ + have hi6 : i < 6 := by unfold tcbSlots at hi; omega + have hs : IsSlot thWw (slotOf idT i) := ⟨idT, i, Or.inr ⟨thWw_idT, hi⟩, rfl⟩ + have hv := slotOf_idT i hi6 + refine ⟨.Null, .nullCap, ?_, idle_map i hi6, ⟨trivial, rfl⟩⟩ + rw [show slotAddr idT.bv (BitVec.ofNat 64 i) = slotOf idT i from rfl, capAt_capV, + capVw_val hs (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) + (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega)] + · cases e + +theorem capValidw {x : U64} (h : IsSlot thWw x) : CapValid thWw (capV (wL.mem ww) x) := by + rcases slotw_cases h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] + · exact ⟨thWw_rC, by decide⟩ + · exact thWw_rT + · show rS ≠ 0#u64; decide + · trivial + · trivial + · exact thWw_gT + · trivial + · trivial + +theorem ww_slotsValid : SlotsValid thWw (wL.mem ww) where + cnode p r hp i hi c h := by + have hs : IsSlot thWw (slotOf p i) := ⟨p, i, Or.inl ⟨r, hp, hi⟩, rfl⟩ + rw [show slotAddr p.bv (BitVec.ofNat 64 i) = slotOf p i from rfl, capAt_capV] at h + obtain rfl := ok_inj h; exact capValidw hs + tcb t ht i hi c h := by + have hs : IsSlot thWw (slotOf t i) := ⟨t, i, Or.inr ⟨ht, hi⟩, rfl⟩ + rw [show slotAddr t.bv (BitVec.ofNat 64 i) = slotOf t i from rfl, capAt_capV] at h + obtain rfl := ok_inj h; exact capValidw hs + +theorem segOK_single (x : U64) : Mdb.SegOK (fun _ => none : U64 → Option U64) [x] := by + intro pre y post h + refine ⟨[], post, rfl, fun z => ⟨fun hd => (desc_none hd).elim, fun hz => by cases hz⟩⟩ + +theorem ww_links : MdbLinks thWw (wL.mem ww) Gw where + tree := ⟨by simp [Gw]; decide, fun ch h => by + simp only [Gw, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact segOK_single _, fun a b h => by cases h⟩ + ne_nil ch h := by + simp only [Gw, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp + chain ch h := by + simp only [Gw, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact List.isChain_singleton _ + head ch h _ := by + simp only [Gw, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · exact prevOfw isSlot_rT + · exact prevOfw isSlot_rC0 + · exact prevOfw isSlot_rC1 + · exact prevOfw isSlot_rC2 + · exact prevOfw isSlot_rC3 + · exact prevOfw isSlot_rC4 + · exact prevOfw isSlot_rC5 + last ch h _ := by + simp only [Gw, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl + · exact nextOfw isSlot_rT + · exact nextOfw isSlot_rC0 + · exact nextOfw isSlot_rC1 + · exact nextOfw isSlot_rC2 + · exact nextOfw isSlot_rC3 + · exact nextOfw isSlot_rC4 + · exact nextOfw isSlot_rC5 + members a := by + constructor + · intro h + refine ⟨slot_of_flat h, ?_⟩ + rcases flatw h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> exact ⟨_, by rw [capAt_capV, e], fun h => by cases h⟩ + · rintro ⟨hs, c, hc, hne⟩ + rw [capAt_capV] at hc + rcases slotw_cases hs with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ | e + · exact Gw_flat.2 (Or.inl rfl) + · exact Gw_flat.2 (Or.inr (Or.inl rfl)) + · exact Gw_flat.2 (Or.inr (Or.inr (Or.inl rfl))) + · exact Gw_flat.2 (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) + · exact Gw_flat.2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl))))) + · exact Gw_flat.2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))) + · exact Gw_flat.2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (rfl))))))) + · rw [e] at hc; exact absurd (ok_inj hc).symm hne + +theorem σw_cdt (sl : CslotPtr) : σw.cdt sl = none := rfl + +theorem absSlot_ut : AbsSlot thWw (rC.bv, [false, true, false]) := ⟨rC, rfl, Or.inl thWw_rC⟩ + +theorem ww_cdtRel : CdtRel thWw (wL.mem ww) σw Gw where + cdt _ _ := rfl + cdt_slot sl q h := by cases h + orig sl h := by + rw [revOfw h.isSlot] + show decide (sl = (rC.bv, [false, true, false])) = decide (cslotAddr sl = rC2) + refine decide_eq_decide.2 ⟨fun e => by rw [e]; exact cs_rC2, fun e => ?_⟩ + exact cslotAddr_inj placed_thWw h absSlot_ut (e.trans cs_rC2.symm) + par_slot a b h := by cases h + +theorem ww_mdbInv : MdbInv (wL.mem ww) Gw where + orig c p h := by cases h + desc x y h := (desc_none h).elim + after y hy h := absurd (nextOfw (slot_of_flat hy)) h + chunk ch h := by + simp only [Gw, List.mem_cons, List.not_mem_nil, or_false] at h + rcases h with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> exact ⟨trivial, trivial⟩ + badges y hy h := absurd (nextOfw (slot_of_flat hy)) h + utrev x hx h := by + rw [revOfw (slot_of_flat hx), decide_eq_true_eq] + rcases flatw hx with ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ + · rw [e] at h; cases h + · rw [e] at h; cases h + · rw [e] at h; cases h + · exact ex + · rw [e] at h; cases h + · rw [e] at h; cases h + · rw [e] at h; cases h + self x hx _ := by + unfold srM + rcases flatw hx with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> rw [e] + · exact srB_cnC + · exact srB_thC + · exact srB_scC + · exact srB_utC + · exact srB_sctlC + · exact srB_gthC + · exact srB_vcC + +theorem ww_crel : CRel thWw (wL.mem ww) σw Gw := + ⟨ww_stateRel, ww_slotsValid, ww_links, ww_cdtRel, ww_mdbInv⟩ + +theorem ww_finInv : FinInv thWw (wL.mem ww) σw Gw := ⟨kTyped_thWw, ww_finM Gw ww_links⟩ + +/-! ## The untyped and overlap facts -/ + +theorem flatw_cap {x : U64} (hx : x ∈ Gw.chains.flatten) : + capV (wL.mem ww) x = cnC ∨ capV (wL.mem ww) x = thC ∨ capV (wL.mem ww) x = scC ∨ capV (wL.mem ww) x = utC ∨ capV (wL.mem ww) x = sctlC ∨ capV (wL.mem ww) x = gthC ∨ capV (wL.mem ww) x = vcC := by + rcases flatw hx with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ + · exact Or.inl 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 (Or.inl e)))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl e))))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (e)))))) + +/-- The only untyped member is the root CNode's slot 2. -/ +theorem flatw_ut {x : U64} (hx : x ∈ Gw.chains.flatten) {p sb fi : U64} {d : Bool} + (h : capV (wL.mem ww) x = .Untyped p sb fi d) : x = rC2 ∧ p = uB ∧ sb = 12#u64 ∧ fi = 0#u64 ∧ d = false := by + rcases flatw hx with ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ + · rw [e] at h; cases h + · rw [e] at h; cases h + · rw [e] at h; cases h + · rw [e] at h; cases h; exact ⟨ex, rfl, rfl, rfl, rfl⟩ + · rw [e] at h; cases h + · rw [e] at h; cases h + · rw [e] at h; cases h + +theorem rng_all : + baseF cnC = 18446744071562108928 ∧ topF cnC = 18446744071562109183 ∧ + baseF thC = 18446744071562100736 ∧ topF thC = 18446744071562104831 ∧ + baseF scC = 18446744071562104832 ∧ topF scC = 18446744071562104959 ∧ + baseF utC = 18446744071562117120 ∧ topF utC = 18446744071562121215 ∧ + baseF gthC = 18446744071562113024 ∧ topF gthC = 18446744071562117119 ∧ + baseF vcC = 18446744071563116544 ∧ topF vcC = 18446744071563149311 := + ⟨base_cnC, by rw [top_cnC], base_thC, by rw [top_thC], base_scC, by rw [top_scC], base_utC, by rw [top_utC], base_gthC, by rw [top_gthC], base_vcC, by rw [top_vcC]⟩ + +/-- **`UtInv` at `ww`.** -/ +theorem ww_utInv : UtInv (wL.mem ww) Gw where + reg x hx hp := by + rcases flatw_cap hx with e | e | e | e | e | e | e <;> rw [e] at hp ⊢ + · rw [base_cnC, top_cnC]; omega + · rw [base_thC, top_thC]; omega + · rw [base_scC, top_scC]; omega + · rw [base_utC, top_utC]; omega + · rw [physF_sctlC] at hp; cases hp + · rw [base_gthC, top_gthC]; omega + · rw [base_vcC, top_vcC]; omega + inc x hx _ _ _ _ _ y h := (desc_none h).elim + noov x hx p sb fi d h y hy hp := by + obtain ⟨ex, rfl, rfl, rfl, rfl⟩ := flatw_ut hx h + rcases flatw hy with ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ + · refine Or.inl ?_ + rw [e]; unfold DisjR; rw [base_cnC, top_cnC, uB_val] + have e0 : (0#u64).val = 0 := rfl + have e12 : (12#u64).val = 12 := rfl + rw [e0, e12]; omega + · refine Or.inl ?_ + rw [e]; unfold DisjR; rw [base_thC, top_thC, uB_val] + have e0 : (0#u64).val = 0 := rfl + have e12 : (12#u64).val = 12 := rfl + rw [e0, e12]; omega + · refine Or.inl ?_ + rw [e]; unfold DisjR; rw [base_scC, top_scC, uB_val] + have e0 : (0#u64).val = 0 := rfl + have e12 : (12#u64).val = 12 := rfl + rw [e0, e12]; omega + · refine Or.inr ⟨by rw [e]; rfl, ?_⟩ + have : y = x := ey.trans ex.symm + subst this; exact ⟨le_rfl, le_rfl⟩ + · rw [e, physF_sctlC] at hp; cases hp + · refine Or.inl ?_ + rw [e]; unfold DisjR; rw [base_gthC, top_gthC, uB_val] + have e0 : (0#u64).val = 0 := rfl + have e12 : (12#u64).val = 12 := rfl + rw [e0, e12]; omega + · refine Or.inl ?_ + rw [e]; unfold DisjR; rw [base_vcC, top_vcC, uB_val] + have e0 : (0#u64).val = 0 := rfl + have e12 : (12#u64).val = 12 := rfl + rw [e0, e12]; omega + excl x hx y hy hne p sb fi fi' d d' h h' := by + exact absurd ((flatw_ut hx h).1.trans (flatw_ut hy h').1.symm) hne + fib x hx p sb fi d h := by + obtain ⟨-, -, rfl, rfl, -⟩ := flatw_ut hx h + decide + drange x hx p sb fi d h y hy hne hp hd := by + obtain ⟨ex, -, -, -, -⟩ := flatw_ut hx h + have exv : capV (wL.mem ww) x = utC := by rw [ex]; exact capV_rC2 + refine absurd ?_ hd + rcases flatw hy with ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ | ⟨ey, e⟩ + · rw [e, exv]; refine disj_lt ?_ + rw [base_cnC, top_cnC, base_utC, top_utC]; omega + · rw [e, exv]; refine disj_lt ?_ + rw [base_thC, top_thC, base_utC, top_utC]; omega + · rw [e, exv]; refine disj_lt ?_ + rw [base_scC, top_scC, base_utC, top_utC]; omega + · exact absurd (ey.trans ex.symm) hne + · rw [e, physF_sctlC] at hp; cases hp + · rw [e, exv]; refine disj_lt ?_ + rw [base_gthC, top_gthC, base_utC, top_utC]; omega + · rw [e, exv]; refine disj_lt ?_ + rw [base_vcC, top_vcC, base_utC, top_utC]; omega + +theorem ww_noOverlap : NoOverlap (wL.mem ww) Gw := by + obtain ⟨r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11⟩ := rng_all + intro x hx y hy hpx hpy hux + rcases flatw hx with ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ | ⟨rfl, ex⟩ <;> rcases flatw hy with ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ <;> + first + | (rw [ex, physF_sctlC] at hpx; cases hpx) + | (rw [ey, physF_sctlC] at hpy; cases hpy) + | (rw [ex] at hux; cases hux; done) + | (rw [ex]; exact Or.inr (Or.inr (Or.inr ⟨rfl, rfl, rfl, rfl⟩))) + | (rw [ex, ey]; refine Or.inl (disj_lt ?_); omega) + +theorem ww_utWin : UtWin (wL.mem ww) Gw := by + intro x hx + rcases flatw_cap hx with e | e | e | e | e | e | e <;> rw [e] + · trivial + · trivial + · trivial + · refine ⟨fun _ => ?_, fun h => by cases h⟩ + unfold layout.PPTR_BASE; decide + · trivial + · trivial + · trivial + +theorem ww_utKsOut : Vs.UtKsOut (wL.ksb ww) (wL.mem ww) Gw := by + intro x hx p sb fi h + obtain ⟨-, rfl, rfl, -, -⟩ := flatw_ut hx h + left; rw [ksb_ww, fkb_val, uB_val]; decide + +/-- **`MdbOKV` at `ww`.** -/ +theorem ww_mdbOKV : MdbOKV (wL.ksb ww) (wL.mem ww) Gw := by + refine ⟨⟨fun x hx p r m ep a va h => ?_, ww_utInv, ww_noOverlap⟩, ww_utWin, + ⟨∅, ∅, ∅, ∅, ∅, fun x hx => ?_⟩, ww_utKsOut⟩ + · rcases flatw_cap hx with e | e | e | e | e | e | e <;> rw [e] at h <;> cases h + · rcases flatw_cap hx with e | e | e | e | e | e | e <;> rw [e] <;> trivial + +/-- **`DelInv` at `ww`.** -/ +theorem ww_delInv : DelInv thWw (wL.mem ww) where + zwf y hy p zt n h := by rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at h <;> cases h + zu y y' hy _ _ p zt n p' zt' n' h := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at h <;> cases h + nc y hy h := by + refine ⟨prevOfw hy, nextOfw hy⟩ + +theorem ww_delBound : DelBound thWw := DelBound.of_placed placed_thWw + +/-- No spec object lies in the untyped region. -/ +theorem ww_sfree : Sched.SpecFree (wL.mem ww) Gw σw := by + intro x hx p sb fi h o ko hk + obtain ⟨-, rfl, rfl, -, -⟩ := flatw_ut hx h + have e12 : (12#u64).val = 12 := rfl + rw [uB_val, e12] + left + rcases obj_ext hk with ⟨rfl, rfl⟩ | h1 | ⟨h1, -⟩ + · right; decide + · exact Or.inl h1 + · right; omega + +/-- **The Untyped cap's facts** (`JAtZ.utc`) at the cap of `uB` in the root CNode. -/ +theorem ww_utc : ∀ sl p sb fi dev, AbsSlot thWw sl → + capAt (wL.mem ww) (cslotAddr sl) = ok (.Untyped p sb fi dev) → dev = false → + Sched.UtCapP3 (L := wL) warI thWw ww σw Gw sl p sb fi := by + intro sl p sb fi dev hs h _ + rw [capAt_capV] at h + have h' := ok_inj h + have hx : cslotAddr sl = rC2 ∧ p = uB ∧ sb = 12#u64 ∧ fi = 0#u64 := by + rcases slotw_cases hs.isSlot with ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | ⟨ex, e⟩ | e + · rw [e] at h'; cases h' + · rw [e] at h'; cases h' + · rw [e] at h'; cases h' + · rw [e] at h'; cases h'; exact ⟨ex, rfl, rfl, rfl⟩ + · rw [e] at h'; cases h' + · rw [e] at h'; cases h' + · rw [e] at h'; cases h' + · rw [e] at h'; cases h' + obtain ⟨-, rfl, rfl, rfl⟩ := hx + have e12 : (12#u64).val = 12 := rfl + have e0 : (0#u64).val = 0 := rfl + have flat : ∀ a ∈ Gw.chains.flatten, a.val < 18446744071562117120 ∨ 18446744071562117120 + 4096 ≤ a.val := by + intro a ha + rcases Gw_flat.1 ha with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> decide + refine + { pal := by rw [uB_val, e12]; decide + pfit := by rw [uB_val, e12]; decide + fiLe := by rw [e0]; exact Nat.zero_le _ + high := fun _ => by rw [uB_val]; omega + actZ := fun _ => ?_ + free := ?_ + zero := fun _ a ha h1 h2 => ?_ + sb4 := by rw [e12]; omega + nostr := ?_ + nsFree := ?_ + ns := fun _ a ha => ?_ + rst := fun _ _ h => absurd e0 h + dwReg := fun _ => ?_ + gReg := fun _ => ?_ } + · have hv : (wL.nodeAt ww (wL.cpu ww) layout.node.SCHED_ACTION).val = 18446744071562147072 + 16 := by + rw [cpu_ww, nodeAt1_v _ (by unfold layout.node.SCHED_ACTION; decide)] + unfold layout.node.SCHED_ACTION; rfl + rw [hv, uB_val, e12]; omega + · intro q kd hq + rw [uB_val, e0, e12] + thcw hq <;> simp only [objSize, Nat.reducePow] <;> omega + · rw [uB_val, e0] at h1 + rw [uB_val, e12] at h2 + have h8 : a.val % 8 = 0 := ha + rw [memw_al rfl h8]; imgwz + · intro q kd hq + rw [uB_val, e12] + thcw hq <;> simp only [objSize, Nat.reducePow] <;> omega + · intro a ha + rw [uB_val, e0, e12] + have := flat a ha; omega + · rw [uB_val, e12]; exact flat a ha + · have hv : (wL.nodeAt ww 0#u64 layout.node.DEBUG_TCBS).val = + 18446744071562147072 + layout.node.DEBUG_TCBS.val := + nodeAt1_v _ (by unfold layout.node.DEBUG_TCBS; decide) + have hd : layout.node.DEBUG_TCBS.val < 4288 := by unfold layout.node.DEBUG_TCBS; decide + rw [hv, uB_val, e12]; omega + · rw [uB_val, e12]; decide + +/-! ## VSpace and frame conjuncts -/ + +theorem ww_vsc : Vs.VsSlotsW thWw ∅ ∅ ∅ ∅ ∅ (wL.mem ww) := by + intro y hy; rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] <;> trivial + +theorem ww_fcap : Vs.FrSlots thWw (UFr σw) (wL.mem ww) := by + intro y hy; rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] <;> trivial + +theorem ww_vsRel : Vs.VsRel ∅ (wL.mem ww) (wL.ksb ww) σw where + table hi h := by + have hk : fkb.val + 288000 + 16384 ≤ 2 ^ 64 := by rw [fkb_val]; norm_num + have hv := Vs.atA_val (ksb := fkb) (hi := hi) hk h + rw [fkb_val] at hv + have hz : imgw (18446744071562133504 + 288000 + 8 * hi.val) = 0#u64 := by imgwz + rw [ksb_ww, memw_al hv (by omega), hz, optRef_zero]; rfl + tmem hi h hne := by + have hk : fkb.val + 288000 + 16384 ≤ 2 ^ 64 := by rw [fkb_val]; norm_num + have hv := Vs.atA_val (ksb := fkb) (hi := hi) hk h + rw [fkb_val] at hv + have hz : imgw (18446744071562133504 + 288000 + 8 * hi.val) = 0#u64 := by imgwz + rw [ksb_ww, memw_al hv (by omega), hz] at hne + exact (hne rfl).elim + pool p hp := hp.elim + +theorem ww_ptRel : Vs.PtRel ∅ ∅ ∅ ∅ (wL.mem ww) σw := + ⟨fun p hp => hp.elim, fun p hp => hp.elim, fun p hp => hp.elim, fun p hp => hp.elim⟩ + +theorem ww_poolsIn : Vs.PoolsIn ∅ ∅ (wL.mem ww) := fun p hp => hp.elim + +/-- The root TCB current, its VSpace slot null. -/ +theorem ww_curOK : Vs.CurOK wst thWw ww σw := by + refine ⟨rT, rfl, thWw_rT, fun p asid h => ?_⟩ + have hs : IsSlot thWw (slotOf rT 1) := ⟨rT, 1, Or.inr ⟨thWw_rT, by unfold tcbSlots; omega⟩, rfl⟩ + have hv := slotOf_rT 1 (by omega) + rw [capAt_capV, capVw_val hs (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega) + (by rw [hv]; omega) (by rw [hv]; omega) (by rw [hv]; omega)] at h + cases ok_inj h + +theorem ww_orig : ∀ sl, σw.isOriginalCap sl = true → AbsSlot thWw sl := by + intro sl h + have e : sl = (rC.bv, [false, true, false]) := of_decide_eq_true h + rw [e]; exact absSlot_ut + +theorem ww_bok : Endp.IpcBufCapOK σw := by + intro t x h + intro dev p R mt vms md hc + rcases kh_tcb_of h with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hc + +theorem ww_fcov : FrameCover ∅ σw := by + intro q vms h + exfalso + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · rcases bootHeap_fInit.obj _ _ h' with h1 | h1 | e | e | e | e + · subst h1; rw [bootHeap_fInit.tcb] at h'; cases h' + · subst h1; rw [bootHeap_fInit.sc] at h'; cases h' + · cases e + · cases e + · cases e + · cases e + + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitJAt.lean b/proofs/Fl4Proofs/P3/VmWitJAt.lean new file mode 100644 index 000000000..c33874c65 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitJAt.lean @@ -0,0 +1,359 @@ +/- +P3 (line mmuAD witness): **`JAtZB` at the VM witness** `ww` / `σw` (**`jAtZB_ww`**): liveness, `JCarry`, the IPC, +yield and call relations, and the record. Generated by `handoff/tools/gen_witjat.py` from P3/JBootQ1{Live,Inv,JAt}. + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitLay +import Fl4Proofs.P3.VmWitSpecB +import Fl4Proofs.P3.JBootQ1JAt +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp + Fl4Proofs.P3.Ipc Fl4Proofs.P2.Wit Classical + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem dbgHead_ww : dbgHead (L := wL) ww = 0#u64 := by + show nodeW wst ww 0#u64 layout.node.DEBUG_TCBS = 0#u64 + rw [show layout.node.DEBUG_TCBS = 88#u64 by unfold layout.node.DEBUG_TCBS; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +/-- **`LiveJ` at `ww`.** -/ +theorem ww_liveJ : LiveJ (L := wL) thWw Hww Gw ww σw where + slots q i hs hu := by + left + have hsl : IsSlot thWw (slotOf q i) := ⟨q, i, hs, rfl⟩ + have hv := IsSlotAt.val placed_thWw hs + have key : ∀ (c : U64) (cv : Nat), c.val = cv → slotOf q i = c → + (cv = 18446744071562100736 → q = rT) ∧ (18446744071562108928 ≤ cv → cv < 18446744071562108928 + 256 → q = rC) := by + intro c cv hcv e + have := congrArg UScalar.val e + rw [hv, hcv] at this + rcases hs with ⟨r, hq, hi⟩ | ⟨hq, hi⟩ + · rcases thWw_pos hq with ⟨e1, e2⟩ | ⟨e1, e2⟩ | ⟨e1, e2⟩ | ⟨e1, e2⟩ | ⟨e2, ⟨j, hj, e1⟩ | ⟨j, hj, hj2, e1⟩⟩ + · cases e2 + · cases e2 + · cases e2 + · cases e2 + simp only [Nat.reducePow] at hi + exact ⟨fun h => by omega, fun _ _ => UScalar.eq_of_val_eq (by rw [rC_val]; omega)⟩ + · cases e2; rw [Nat.pow_zero] at hi; exact ⟨fun h => by omega, fun _ _ => by omega⟩ + · cases e2; rw [Nat.pow_zero] at hi; exact ⟨fun h => by omega, fun _ _ => by omega⟩ + · unfold tcbSlots at hi + rcases thWw_pos hq with ⟨e1, e2⟩ | ⟨e1, e2⟩ | ⟨e1, e2⟩ | ⟨e1, e2⟩ | ⟨e2, -⟩ + · exact ⟨fun h => by omega, fun _ _ => by omega⟩ + · exact ⟨fun h => UScalar.eq_of_val_eq (by rw [rT_val]; omega), fun _ _ => by omega⟩ + · exact ⟨fun h => by omega, fun _ _ => by omega⟩ + · cases e2 + · cases e2 + have inC : q = rC → ∃ y ∈ Gw.chains.flatten, RefsObj q (capV (wL.mem ww) y) := fun h => by + subst h; exact ⟨rT, Gw_flat.2 (Or.inl rfl), by rw [capV_rT]; rfl⟩ + rcases hu with h | ⟨x, hxe, hx⟩ + · rcases slotw_cases hsl with ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | e + · have hq := (key rT _ rT_val e).1 rfl + subst hq + exact ⟨rC0, Gw_flat.2 (Or.inr (Or.inl rfl)), by rw [capV_rC0]; rfl⟩ + · exact inC ((key rC0 _ rC0_val e).2 (by omega) (by omega)) + · exact inC ((key rC1 _ rC1_val e).2 (by omega) (by omega)) + · exact inC ((key rC2 _ rC2_val e).2 (by omega) (by omega)) + · exact inC ((key rC3 _ rC3_val e).2 (by omega) (by omega)) + · exact inC ((key rC4 _ rC4_val e).2 (by omega) (by omega)) + · exact inC ((key rC5 _ rC5_val e).2 (by omega) (by omega)) + · exact absurd e h + · have ex : x = (rC.bv, [false, true, false]) := of_decide_eq_true hx + rw [ex, cs_rC2] at hxe + exact inC ((key rC2 _ rC2_val hxe.symm).2 (by omega) (by omega)) + dbg n h := by rw [dbgHead_ww, dbgNth_zero] at h; exact absurd rfl h + zslots y hy p zt n h := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at h <;> cases h + zdead y hy p zt n h := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at h <;> cases h + +/-- **`DbgWF` at `ww`** (empty lists). -/ +theorem ww_dbgWF : DbgWF (Heap.tcbs Hww) Csw (wL.mem ww) (dbgHd (L := wL) ww) := by + refine ⟨fun _ => [], ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro c hc + rw [Csw, Set.mem_singleton_iff] at hc; subst hc + exact dbgHead_ww + · intro c _ x hx; cases hx + · intro c _ x hx; cases hx + · intro c _; exact List.nodup_nil + · intro c _ h; cases h + · intro c _ x hx; cases hx + · intro y hy _ + rw [tcbs_Hww] at hy + exact ⟨dn_ww y hy, dp_ww y hy⟩ + +/-- **`NullOrig` at `ww`.** -/ +theorem ww_nullOrig : NullOrig thWw (wL.mem ww) σw := fun sl _ h => by + show decide (sl = (rC.bv, [false, true, false])) = false + refine decide_eq_false (fun e => ?_) + rw [e, cs_rC2, capV_rC2] at h + cases h + +/-- **`JAtZ.live` at `ww`.** -/ +theorem ww_live : LiveDN (L := wL) thWw Hww Csw Gw ww σw := ⟨ww_liveJ, ww_dbgWF, ww_nullOrig⟩ + +/-! ## `JCarry` -/ + +theorem atA_ww (hi : U64) (h : hi.val < 2048) : wL.mem ww (Vs.atA fkb hi) = 0#u64 := by + have hv : (Vs.atA fkb hi).val = 18446744071562133504 + 288000 + 8 * hi.val := by + unfold Vs.atA + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, fkb_val] + omega + rw [memw_al hv (by omega)]; imgwz + +theorem vsPath1_none {k : Nat} {q : U64} {a v : Nat} (h : Vs.VsPath (wL.mem ww) fkb k q a v) : False := by + induction h with + | tbl hi hne => exact hne (atA_ww _ hi) + | pool _ _ _ _ ih => exact ih + | l4 _ _ _ ih => exact ih + | l3 _ _ _ _ ih => exact ih + | l2 _ _ _ _ ih => exact ih + +/-- **`VsNamed` at `ww`.** -/ +theorem ww_vsNamed : Vs.VsNamed thWw (wL.mem ww) fkb where + named k q a v h := (vsPath1_none h).elim + uref y y' hy _ _ hn := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at hn <;> exact absurd rfl hn + uun y y' hy _ _ ht := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at ht <;> cases ht + empty y hy ht := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at ht <;> cases ht + poolT y hy q b h := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at h <;> cases h + +/-- **`ArchFitHyp` at `ww`.** -/ +theorem ww_archFit : ArchFitHyp Hww σw := by + intro o ao n hk hH + rcases kh_some hk with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · have hv : (⟨vB.bv⟩ : U64) = vB := rfl + rw [hv, Hww_vB] at hH + cases hH; cases e + show 15 ≤ 15 + omega + · have hne : ∀ q : U64, fInitKheap q.bv = none → (⟨o⟩ : U64) ≠ q := fun q hq e => by + have : o = q.bv := congrArg UScalar.bv e + rw [this, hq] at h'; cases h' + have n1 := hne rT (fInitKheap_rT) + have n2 : (⟨o⟩ : U64) ≠ gT := hne gT (fInitKheap_none (by decide) (by decide) (by decide) (by decide) + (by decide) gT_irq) + have n3 := hne rS (fInitKheap_rS) + have n4 := hne rC (fInitKheap_rC) + have n5 : (⟨o⟩ : U64) ≠ vB := hne vB (fInitKheap_none (by decide) (by decide) (by decide) (by decide) + (by decide) vB_irq) + rw [Hww_old n1 n2 n3 n4 n5] at hH + exact w0_archFit o ao n h' hH + +/-- **`JAtZ.carry` at `ww`.** -/ +theorem ww_carry : JCarry (L := wL) Zw Hww Csw ww σw where + bufPg wp hw := hw.elim + archFit := ww_archFit + devApart x hx _ _ _ h := Bool.noConfusion (flatw_ut hx h).2.2.2.2 + devSep x hx y hy _ _ _ _ _ _ _ h := Bool.noConfusion (flatw_ut hy h).2.2.2.2 + vsTyp y hy := by + show Vs.TabR _ (capV (wL.mem ww) y) + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] <;> trivial + devTab x hx _ _ _ h := Bool.noConfusion (flatw_ut hx h).2.2.2.2 + vsNamed _ := ww_vsNamed + +theorem mcp_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (mcpA t) = 0#u64 := by + unfold mcpA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +theorem flags_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (flagsA t) = 0#u64 := by + unfold flagsA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +theorem ipcBuf_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (tIpcBufA t) = 0#u64 := by + unfold tIpcBufA; exact tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide) + +theorem scReply_ww (c : U64) (hc : c ∈ C1) : wL.mem ww (scReplyA c) = 0#u64 := by + unfold scReplyA layout.sc.REPLY + rcases memC1 hc with rfl | rfl + · rw [scW1_v _ (by decide) (by decide)]; decide + · rw [rsW_v _ (by decide) (by decide)]; decide + +/-- The TCBs of `Tw`: their spec objects. -/ +theorem tcbw {t : U64} (ht : t ∈ Tw) : + (t = idT ∧ σw.kheap t.bv = some (.tcb idleTcbV)) ∨ (t = rT ∧ σw.kheap t.bv = some (.tcb rootTcbW)) ∨ + (t = gT ∧ σw.kheap t.bv = some (.tcb guestTcbV)) := by + rcases memTw ht with rfl | rfl | rfl + · exact Or.inl ⟨rfl, σw_idleTcb⟩ + · exact Or.inr (Or.inl ⟨rfl, σw_rootTcb⟩) + · exact Or.inr (Or.inr ⟨rfl, σw_guestTcb⟩) + +/-- **`YieldRel` at `ww`** (no yield). -/ +theorem ww_yld : Sched.YieldRel Tw C1 (wL.mem ww) σw where + tcb t ht := by + rcases tcbw ht with ⟨rfl, hk⟩ | ⟨rfl, hk⟩ | ⟨rfl, hk⟩ + · exact ⟨idleTcbV, hk, by rw [yield_ww _ ht, optRef_zero]; rfl⟩ + · exact ⟨rootTcbW, hk, by rw [yield_ww _ ht, optRef_zero]; rfl⟩ + · exact ⟨guestTcbV, hk, by rw [yield_ww _ ht, optRef_zero]; rfl⟩ + toSc t ht h := absurd (yield_ww t ht) h + frm sc hsc y n hk p hp := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ <;> simp [rootScV, idleScV, defaultSchedContext] at hp + +/-- **`McpRel` at `ww`** (MCP 0, no flags). -/ +theorem ww_mcp : Sched.McpRel Tw (wL.mem ww) σw := by + intro t ht + rcases tcbw ht with ⟨rfl, hk⟩ | ⟨rfl, hk⟩ | ⟨rfl, hk⟩ + · refine ⟨idleTcbV, hk, by rw [mcp_ww _ ht]; rfl, ?_⟩ + rw [flags_ww _ ht]; ext f; simp [wordToTcbFlags, idleTcbV] + · refine ⟨rootTcbW, hk, by rw [mcp_ww _ ht]; rfl, ?_⟩ + rw [flags_ww _ ht]; ext f; simp [wordToTcbFlags, rootTcbW, rootTcbV, idleTcbV] + · refine ⟨guestTcbV, hk, by rw [mcp_ww _ ht]; rfl, ?_⟩ + rw [flags_ww _ ht]; ext f; simp [wordToTcbFlags, guestTcbV, idleTcbV] + +/-- **`BufWordRel` at `ww`** (IPC buffers 0). -/ +theorem ww_buf : BufWordRel Tw (wL.mem ww) σw := by + intro t ht + rcases tcbw ht with ⟨rfl, hk⟩ | ⟨rfl, hk⟩ | ⟨rfl, hk⟩ + · exact ⟨idleTcbV, hk, by rw [ipcBuf_ww _ ht]; rfl⟩ + · exact ⟨rootTcbW, hk, by rw [ipcBuf_ww _ ht]; rfl⟩ + · exact ⟨guestTcbV, hk, by rw [ipcBuf_ww _ ht]; rfl⟩ + +/-- **`CallRel` at `ww`** (no reply; both SCs' stacks empty). -/ +theorem ww_call : Sched.CallRel (wL.mem ww) ∅ C1 σw (fun _ => []) where + stack sc hsc := by + rcases memC1 hsc with rfl | rfl + · refine ⟨⟨idleScV, 0, σw_idleSc', rfl⟩, fun _ h => (List.not_mem_nil h).elim, List.nodup_nil, ?_, ?_⟩ + · rw [scReply_ww _ hsc]; rfl + · intro i h; exact absurd h (Nat.not_lt_zero _) + · refine ⟨⟨rootScV, 0, σw_rootSc, rfl⟩, fun _ h => (List.not_mem_nil h).elim, List.nodup_nil, ?_, ?_⟩ + · rw [scReply_ww _ hsc]; rfl + · intro i h; exact absurd h (Nat.not_lt_zero _) + excl _ _ _ _ _ h := (List.not_mem_nil h).elim + reply _ h := h.elim + det _ h := h.elim + +theorem ww_rfree : Sched.RFree (wL.mem ww) ∅ C1 σw (fun _ => []) := fun _ h => h.elim + +theorem ww_rrecv : Sched.RRecv (wL.mem ww) ∅ σw := fun _ h => h.elim + +/-- **`IpcRel` at `ww`** (`XTopAt.ipc`). -/ +theorem ww_ipc : IpcRel ∅ Tw ∅ ∅ ∅ (wL.mem ww) σw := + IpcRel.of ww_ntfnStateRel ww_epStateRel (sched_sc ww_schedRel) + +theorem efoot1_far {a : U64} + (h : (a.val < 18446744071562072064 ∨ 18446744071562072064 + 4096 ≤ a.val) ∧ + (a.val < 18446744071562100736 ∨ 18446744071562100736 + 4096 ≤ a.val) ∧ + (a.val < 18446744071562113024 ∨ 18446744071562113024 + 4096 ≤ a.val)) : + ¬ EFoot Tw ∅ ∅ ∅ a := by + rintro (⟨e, he, -⟩ | ⟨r, hr, -⟩ | ⟨t, ht, hf⟩ | ⟨wp, hw, -⟩) + · exact he + · exact hr + · rcases memTw ht with rfl | rfl | rfl <;> rcases hf with ⟨h1, h2⟩ | h3 + · rw [idT_val] at h1 h2; unfold TOffN at h2; omega + · unfold TL at h3; rw [idT_val] at h3; omega + · rw [rT_val] at h1 h2; unfold TOffN at h2; omega + · unfold TL at h3; rw [rT_val] at h3; omega + · rw [gT_val] at h1 h2; unfold TOffN at h2; omega + · unfold TL at h3; rw [gT_val] at h3; omega + · exact hw + +theorem ewrite1_far {a : U64} + (h : (a.val < 18446744071562072064 ∨ 18446744071562072064 + 4096 ≤ a.val) ∧ + (a.val < 18446744071562100736 ∨ 18446744071562100736 + 4096 ≤ a.val) ∧ + (a.val < 18446744071562113024 ∨ 18446744071562113024 + 4096 ≤ a.val)) : + ¬ EWrite Tw ∅ ∅ ∅ a := by + rintro (⟨e, he, -⟩ | ⟨r, hr, -⟩ | ⟨t, ht, hf⟩ | ⟨wp, hw, -⟩) + · exact he + · exact hr + · rcases memTw ht with rfl | rfl | rfl <;> rcases hf with ⟨h1, h2⟩ | h3 + · rw [idT_val] at h1 h2; unfold TWOffN at h2; omega + · unfold TL at h3; rw [idT_val] at h3; omega + · rw [rT_val] at h1 h2; unfold TWOffN at h2; omega + · unfold TL at h3; rw [rT_val] at h3; omega + · rw [gT_val] at h1 h2; unfold TWOffN at h2; omega + · unfold TL at h3; rw [gT_val] at h3; omega + · exact hw + +/-- **`EpSchedApart` at `ww`** (`XTopAt.esa`). -/ +theorem ww_esa : EpSchedApart wst Tw C1 ∅ ∅ ∅ 0#u64 ww σw where + node a h1 h2 := efoot1_far (by rw [nodeBase_ww] at h1; omega) + dom := ewrite1_far (by + unfold curDomA; rw [wa_val (by rw [ksb_ww, fkb_val]; decide), ksb_ww, fkb_val]; decide) + dt := ewrite1_far (by + unfold dtA; rw [wa_val (by rw [ksb_ww, fkb_val]; decide), ksb_ww, fkb_val]; decide) + sc sc hsc x n hk a ha := by + rcases memC1 hsc with rfl | rfl + · rw [σw_idleSc'] at hk; cases hk + obtain ⟨h1, h2⟩ := scObj_val ha + rw [idS_val] at h1 h2 + exact ewrite1_far (by omega) + · rw [σw_rootSc] at hk; cases hk + obtain ⟨h1, h2⟩ := scObj_val ha + rw [rS_val] at h1 h2 + exact ewrite1_far (by omega) + +/-! ## The record -/ + +/-- **The base invariant (without `JExt`) at the VM witness.** -/ +theorem jAtZB_ww (a : MachineState) : + JAtZB (L := wL) warI wAR Zw Hww Csw C1 (fun _ => []) Dw ww a σw where + xtop := + { fin := ⟨⟨ww_othersRel a, ww_topPlaced⟩, ww_vsRel, ww_ptRel, ww_poolsIn, ww_curOK⟩ + rel := ⟨placed_thWw, ww_crel, ww_finInv, ww_domRel, ww_mdbOKV, ww_ipcApart, ww_scApart⟩ + ipc := ww_ipc + esa := ww_esa } + call := ww_call + rfree := ww_rfree + rrecv := ww_rrecv + yld := ww_yld + mcp := ww_mcp + buf := ww_buf + ks := ww_ks + thT := thT_thWw + thk := thk_thWw + ip := ww_ip + rp := ww_rp + apart := ww_apart + nk := ww_nk + nodeB := ww_nodeB + jpl := ww_jpl + cover := ww_cover + obj := σw_obj + zH := ww_zH + sfree := ww_sfree + hset := ww_hset + hscC := ww_hscC + jcov := ww_jcov + kern := ww_jkern + del := ww_delInv + ut := ww_utInv + bound := ww_delBound + finM := ww_finM + utc := ww_utc + nsc := σw_nsc + inv := σw_inv + cur := Or.inr σw_curInv + own := σw_own + off := σw_off + act := σw_act + c0 := ww_c0 + bsa := σw_bsa + sv := σw_sv + isorted := σw_isorted + slink := σw_slink + curRun := σw_curRun + blk := ww_blk + intL := intL_thWw + vsc := ww_vsc + bok := ww_bok + fcov := ww_fcov + fcap := ww_fcap + live := ww_live + zD := ww_zD + carry := ww_carry + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitLay.lean b/proofs/Fl4Proofs/P3/VmWitLay.lean new file mode 100644 index 000000000..56c4b5286 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitLay.lean @@ -0,0 +1,588 @@ +/- +P3 (line mmuAD witness): **placement, apartness, P1's heap, coverage (`JCovD`) and `JKern` at `ww`**. Generated by +`handoff/tools/gen_witlay.py` from P3/JBootQ1Lay. + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitBlk +import Fl4Proofs.P3.JBootQ1Lay +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P2.Wit Classical +open Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-- Case split on `Hww`'s objects (`Hww_pos`), the kind substituted. -/ +macro "hwcw" h:term : tactic => + `(tactic| (rcases Hww_pos $h with ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | + ⟨e', e⟩ | ⟨e', e⟩ | ⟨e', e⟩ | ⟨e', e⟩ <;> cases e')) + +theorem dbgA_vw : (wL.nodeAt ww 0#u64 layout.node.DEBUG_TCBS).val = 18446744071562147072 + 88 := by + rw [nodeAt1_v _ (by rw [dbg_val]; omega), dbg_val] + +theorem actA_vw : (wL.nodeAt ww (wL.cpu ww) layout.node.SCHED_ACTION).val = 18446744071562147072 + 16 := by + rw [cpu_ww, nodeAt1_v _ (by rw [act_val]; omega), act_val] + +theorem atA_vw (hi : U64) (h : hi.val < 2048) : + (atA (wL.ksb ww) hi).val = 18446744071562133504 + 288000 + 8 * hi.val := by + unfold atA; rw [ksb_ww] + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, fkb_val] + omega + +theorem stA_rT : (Fl4Proofs.P1.stA rT).val = 18446744071562100736 + 3328 := by + rw [Fl4Proofs.P1.stA_eq]; exact (wa_val (a := rT) (b := 3328#u64) (by decide)).trans rfl + +/-! ## `TopPlaced` -/ + +theorem oobjw_nat {a : U64} (h : Vs.OObj wst ∅ Tw C1 ∅ ∅ ∅ 0#u64 ww σw a) : + (18446744071562147072 ≤ a.val ∧ a.val < 18446744071562147072 + 4288) ∨ + (18446744071562133504 ≤ a.val ∧ a.val < 18446744071562133504 + 13568 ∧ + (a.val < 18446744071562133504 + 1248 ∨ 18446744071562133504 + 5344 ≤ a.val)) ∨ + (18446744071562072064 + 256 ≤ a.val ∧ a.val < 18446744071562072064 + 4096) ∨ + (18446744071562100736 + 256 ≤ a.val ∧ a.val < 18446744071562100736 + 4096) ∨ + (18446744071562113024 + 256 ≤ a.val ∧ a.val < 18446744071562113024 + 4096) ∨ + (18446744071562096640 ≤ a.val ∧ a.val < 18446744071562096640 + 128) ∨ + (18446744071562104832 ≤ a.val ∧ a.val < 18446744071562104832 + 128) := by + unfold Vs.OObj at h + rw [nodeBase_ww, ksb_ww, fkb_val] at h + rcases h with h | h | ⟨t, ht, h⟩ | ⟨e, he, -⟩ | ⟨r, hr, -⟩ | ⟨n, hn, -⟩ | ⟨sc, hsc, x, n, hk, h⟩ | ⟨wp, hwp, -⟩ + · exact Or.inl h + · exact Or.inr (Or.inl h) + · rcases memTw ht with rfl | rfl | rfl + · rw [idT_val] at h; exact Or.inr (Or.inr (Or.inl h)) + · rw [rT_val] at h; exact Or.inr (Or.inr (Or.inr (Or.inl h))) + · rw [gT_val] at h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl h)))) + · simp at he + · simp at hr + · simp at hn + · rcases memC1 hsc with rfl | rfl + · rw [σw_idleSc'] at hk; cases hk + have := scObj_val h; rw [idS_val] at this; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl this))))) + · rw [σw_rootSc] at hk; cases hk + have := scObj_val h; rw [rS_val] at this; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr this))))) + · simp at hwp + +theorem vobjw_nat {a : U64} (h : Vs.VObj thWw ∅ ∅ ∅ ∅ ∅ (wL.ksb ww) a) : + SlotNatw a.val ∨ (18446744071562133504 + 288000 ≤ a.val ∧ a.val < 18446744071562133504 + 288000 + 16384) := by + unfold Vs.VObj at h + rw [ksb_ww, fkb_val] at h + unfold SlotNatw SlotNat + rcases h with ⟨q, r, hq, h1, h2⟩ | ⟨t, ht, h1, h2⟩ | ⟨q, hq, -⟩ | h + · rcases thWw_pos hq with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨e1, e⟩ | ⟨e, ⟨i, hi, e1⟩ | ⟨i, hi, hi2, e1⟩⟩ + · cases e + · cases e + · cases e + · cases e; simp only [Nat.reducePow] at h2; omega + · cases e; rw [Nat.pow_zero] at h2; omega + · cases e; rw [Nat.pow_zero] at h2; omega + · rcases thWw_pos ht with ⟨e1, -⟩ | ⟨e1, -⟩ | ⟨e1, -⟩ | ⟨-, e⟩ | ⟨e, -⟩ + · omega + · omega + · omega + · cases e + · cases e + · simp at hq + · exact Or.inr h + +theorem slot_endsw {x : U64} (h : IsSlot thWw x) : SlotNatw x.val ∧ SlotNatw (x.val + 31) := by + have h0 := (slot_natw h 0 (by omega)).1 + rw [Nat.add_zero] at h0 + exact ⟨h0, (slot_natw h 31 (by omega)).1⟩ + +theorem ePlacedw : Endp.EPlaced Tw ∅ ∅ ∅ := + ⟨tcbPlaced_T1, fun e he => by simp at he, fun r hr => by simp at hr, fun e he => by simp at he, + fun r hr => by simp at hr, fun e he => by simp at he, fun w hw => by simp at hw, fun w hw => by simp at hw, + fun w hw => by simp at hw, fun w hw => by simp at hw, fun w hw => by simp at hw⟩ + +/-- **`TopPlaced` at `ww`.** -/ +theorem ww_topPlaced : Vs.TopPlaced wst thWw ∅ ∅ ∅ ∅ ∅ ∅ Tw C1 ∅ ∅ ∅ 0#u64 ww σw where + ep := ePlacedw + vs := ⟨by rw [ksb_ww, fkb_val], by rw [ksb_ww, fkb_val]; omega, fun p hp => by simp at hp, + fun p hp => by simp at hp⟩ + pt := fun p hp => by simp at hp + pl := placed_thWw + disj a hO hV := by + have h1 := oobjw_nat hO + have h2 := vobjw_nat hV + unfold SlotNatw SlotNat at h2 + omega + nodeE a h1 h2 hE := by + rw [nodeBase_ww] at h1 h2 + unfold Endp.EFoot at hE + rcases hE with ⟨e, he, -⟩ | ⟨r, hr, -⟩ | ⟨t, ht, h⟩ | ⟨wp, hwp, -⟩ + · simp at he + · simp at hr + · rcases memTw ht with rfl | rfl | rfl + · unfold Endp.TF Endp.TOffN Endp.TL at h; rw [idT_val] at h; omega + · unfold Endp.TF Endp.TOffN Endp.TL at h; rw [rT_val] at h; omega + · unfold Endp.TF Endp.TOffN Endp.TL at h; rw [gT_val] at h; omega + · simp at hwp + po b hb := by obtain ⟨p, hp, -⟩ := hb; simp at hp + ps b hb := by obtain ⟨p, hp, -⟩ := hb; simp at hp + pv b hb := by obtain ⟨p, hp, -⟩ := hb; simp at hp + vo b hb := by + rcases hb with ⟨hi, hhi, rfl⟩ | ⟨p, hp, -⟩ + · have hv := atA_vw hi hhi + refine ⟨by show (atA _ hi).val % 8 = 0; omega, fun p k hp hin => ?_⟩ + rw [hv] at hin + thcw hp <;> simp only [objSize, Nat.reducePow] at hin <;> omega + · simp at hp + sd b hb hS := by + obtain ⟨x, hx, hb'⟩ := hS + have hx0 := (slot_natw hx 0 (by omega)).1 + have hx8 := (slot_natw hx 8 (by omega)).1 + unfold SlotNatw SlotNat at hx0 hx8 + have hbv : b.val = x.val ∨ b.val = x.val + 8 := by + rcases hb' with rfl | rfl + · exact Or.inl rfl + · exact Or.inr (wordOf_val (k := 8#u64) (by show x.val + 8 < 2 ^ 64; omega)) + rcases hb with ⟨hi, hhi, rfl⟩ | ⟨p, hp, -⟩ + · have hv := atA_vw hi hhi + omega + · simp at hp + tko t ht := by + left; rw [ksb_ww, fkb_val] + rcases thWw_pos ht with ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨-, e⟩ | ⟨e, -⟩ + · omega + · omega + · omega + · cases e + · cases e + +/-! ## The IPC and SC apartness -/ + +theorem ww_ipcApart : Ipc.IpcApart thWw ∅ Tw ∅ ∅ ∅ (wL.ksb ww) 0#u64 where + slot a ha x hx hin := by + obtain ⟨h0, h31⟩ := slot_endsw hx + unfold SlotNatw SlotNat at h0 h31 + unfold Ipc.IWords at ha + rcases ha with ⟨t, ht, h⟩ | ⟨e, he, -⟩ | ⟨r, hr, -⟩ | ⟨n, hn, -⟩ | ⟨wp, hwp, -⟩ + · rcases memTw ht with rfl | rfl | rfl + · rw [idT_val] at h; omega + · rw [rT_val] at h; omega + · rw [gT_val] at h; omega + · simp at he + · simp at hr + · simp at hn + · simp at hwp + node a h1 h2 x hx hin := by + obtain ⟨h0, h31⟩ := slot_endsw hx + unfold SlotNatw SlotNat at h0 h31 + have z : (0#u64).val = 0 := rfl + rw [ksb_ww, fkb_val, z] at h1 h2 + omega + dom a ha := by + rw [ksb_ww, fkb_val] + unfold Ipc.IWords at ha + rcases ha with ⟨t, ht, h⟩ | ⟨e, he, -⟩ | ⟨r, hr, -⟩ | ⟨n, hn, -⟩ | ⟨wp, hwp, -⟩ + · rcases memTw ht with rfl | rfl | rfl + · rw [idT_val] at h; omega + · rw [rT_val] at h; omega + · rw [gT_val] at h; omega + · simp at he + · simp at hr + · simp at hn + · simp at hwp + ks a h1 h2 h3 x hx hin := by + obtain ⟨h0, h31⟩ := slot_endsw hx + unfold SlotNatw SlotNat at h0 h31 + rw [ksb_ww, fkb_val] at h1 h2 h3 + omega + kfit := fkb_lt + kal := by rw [ksb_ww, fkb_val] + +theorem ww_scApart : Ipc.ScApart thWw C1 σw := by + intro sc hsc x n hk a ha y hy hin + obtain ⟨h0, h31⟩ := slot_endsw hy + unfold SlotNatw SlotNat at h0 h31 + rcases memC1 hsc with rfl | rfl + · rw [σw_idleSc'] at hk; cases hk + have := scObj_val ha; rw [idS_val] at this; omega + · rw [σw_rootSc] at hk; cases hk + have := scObj_val ha; rw [rS_val] at this; omega + +/-! ## The sets' placement -/ + +theorem ww_ks : KsApart ∅ Tw ∅ ∅ ∅ (wL.ksb ww) := + ⟨fun e he => by simp at he, fun r hr => by simp at hr, fun n hn => by simp at hn, fun w hw => by simp at hw⟩ + +theorem ww_ip : Ipc.IPlaced ∅ Tw ∅ ∅ ∅ := + ⟨⟨tcbPlaced_T1, fun n hn => by simp at hn, fun n hn => by simp at hn⟩, ePlacedw, + fun n hn => by simp at hn, fun n hn => by simp at hn, fun n hn => by simp at hn⟩ + +theorem ww_rp : Sched.RPlaced wst ∅ Tw C1 ∅ 0#u64 ww σw := + ⟨fun r hr => by simp at hr, fun r hr => by simp at hr, fun r hr => by simp at hr, fun r hr => by simp at hr, + fun r hr => by simp at hr⟩ + +theorem ww_apart : Sched.SlotsApart (st := wst) thWw Tw ∅ C1 ∅ 0#u64 ww σw := by + intro a ha + obtain ⟨h0, h31⟩ := slot_endsw ha + unfold SlotNatw SlotNat at h0 h31 + refine ⟨fun t ht => ?_, ?_, fun n hn => by simp at hn, fun sc hsc x n hk => ?_, fun r hr => by simp at hr⟩ + · rcases memTw ht with rfl | rfl | rfl + · rw [idT_val]; omega + · rw [rT_val]; omega + · rw [gT_val]; omega + · rw [nodeBase_ww]; omega + · rcases memC1 hsc with rfl | rfl + · rw [σw_idleSc'] at hk; cases hk; rw [e2sc, idS_val]; omega + · rw [σw_rootSc] at hk; cases hk; rw [e2sc, rS_val]; omega + +theorem ww_nk : ∀ n ∈ (∅ : Set U64), n.val + 40 ≤ (wL.ksb ww).val ∨ (wL.ksb ww).val + 13568 ≤ n.val := + fun n hn => by simp at hn + +theorem ww_nodeB : ∀ wp ∈ (∅ : Set (U64 × Fl4Spec.Word)), ∀ c : U64, c.val < 64 → + wp.1.val + 8 ≤ wL.nodeBase ww c ∨ wL.nodeBase ww c + 4288 ≤ wp.1.val := + fun wp hw => by simp at hw + +/-! ## P1's heap -/ + +theorem Hww_zero : Hww 0#u64 = none := by + rw [Hww_old (by decide) (by decide) (by decide) (by decide) (by decide)]; exact Hw_zero + +/-- **`Hww` is well formed.** -/ +theorem heapOK_Hww : HeapOK Hww where + align p k hp := by hwcw hp <;> simp only [HKind.bits, Nat.reducePow] <;> omega + fits p k hp := by hwcw hp <;> simp only [HKind.bits, Nat.reducePow] <;> omega + lt64 p k hp := by hwcw hp <;> simp [HKind.bits] + scmin p z hp := by hwcw hp <;> omega + disj p k q k' hp hq hne := by + have hv : p.val ≠ q.val := fun e => hne (UScalar.eq_of_val_eq e) + rcases Hww_pos hp with ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e', e⟩ | ⟨e', e⟩ | + ⟨e', e⟩ | ⟨e', e⟩ <;> cases e' <;> + rcases Hww_pos hq with ⟨f, f'⟩ | ⟨f, f'⟩ | ⟨f, f'⟩ | ⟨f, f'⟩ | ⟨f, f'⟩ | ⟨f, f'⟩ | ⟨f, f'⟩ | ⟨f', f⟩ | ⟨f', f⟩ | + ⟨f', f⟩ | ⟨f', f⟩ <;> cases f' <;> + simp only [HKind.bits, Nat.reducePow] <;> omega + +theorem capTypedw {x : U64} (h : IsSlot thWw x) : + CapTyped Hww (Heap.replies Hww) (Heap.ntfns Hww) (capV (wL.mem ww) x) := by + unfold CapTyped + rcases slotw_cases h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] <;> + exact ⟨fun _ _ _ _ _ _ h => (by cases h), fun _ _ _ _ h => (by cases h), fun _ _ h => (by cases h)⟩ + +theorem capVw_nodeA (i : U64) (hi : i.val < 128) : capV (wL.mem ww) (nodeA fkb i) = .Null := by + have hv := nodeA_val (kb := fkb) fkb_lt hi + rw [fkb_val] at hv + have e8 : (8#u64).val = 8 := rfl + exact capV_zero (by rw [memw_al hv (by omega)]; imgwz) + (by rw [memw_al (n := 18446744071562133504 + 1248 + 32 * i.val + 8) (by rw [wordOf_val (by omega), e8, hv]) + (by omega)]; imgwz) + +/-- **`JPlaced` at `ww`** (`Tr = Tw`, `C = C1`). -/ +theorem ww_jplaced : Sched.JPlaced (L := wL) thWw Hww Tw C1 ww σw where + heap := heapOK_Hww + pl := placed_thWw + tcb t := by + constructor + · intro h + rcases thWw_some h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, -, -, h'⟩ + · exact Hww_rT + · exact Hww_gT + · cases e + · rcases thW_some h' with ⟨rfl, -⟩ | ⟨_, _, e⟩ + · exact Hww_idT + · cases e + · intro h + rcases Hww_some h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, -, -, -, -, h'⟩ + · exact thWw_rT + · exact thWw_gT + · cases e + · cases e + · cases e + · rcases Hw_pos h' with ⟨e, -⟩ | ⟨-, e'⟩ | ⟨e', -⟩ | ⟨e', -⟩ | ⟨e', -⟩ | ⟨e', -⟩ + · rw [show t = idT from UScalar.eq_of_val_eq (by rw [e, idT_val])]; exact thWw_idT + all_goals cases e' + cnode p r h := by + rcases thWw_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, e⟩ | ⟨n1, n2, n3, h'⟩ + · cases e + · cases e + · cases e; exact Hww_rC + · have hirq : ∃ irq : Irq, fIrqNode irq = p.bv := by + unfold thW at h'; rw [if_neg (fun e => by subst e; cases (thW_idT.symm.trans (by unfold thW; exact h')))] at h' + split_ifs at h' with hx + exact hx + have hs : p ≠ rS := fun e => by + obtain ⟨irq, e'⟩ := hirq + exact rS_irq irq (e'.trans (congrArg UScalar.bv e)) + have hv : p ≠ vB := fun e => by + obtain ⟨irq, e'⟩ := hirq + exact vB_irq irq (e'.trans (congrArg UScalar.bv e)) + rw [Hww_old n1 n2 hs n3 hv]; exact w0_jplaced.cnode p r h' + tr := thT_thWw + sc c y n hc hk := by + rcases kh_sc_of hk with ⟨e, -, rfl⟩ | ⟨e, -, rfl⟩ + · have : c = rS := Fl4Proofs.P1.eq_of_bv (by rw [e]); subst this; exact ⟨rS_mem, Hww_rS⟩ + · have : c = idS := Fl4Proofs.P1.eq_of_bv (by rw [e, idS_bv]); subst this; exact ⟨idS_mem, Hww_idS⟩ + ks p k hp hk := by + rw [ksb_ww, fkb_val] + rcases Hww_pos hp with ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e', e⟩ | ⟨e', e⟩ | + ⟨e', e⟩ | ⟨e', e⟩ <;> cases e' <;> + first + | exact absurd rfl (hk _) + | (simp only [HKind.bits, Nat.reducePow]; omega) + ksIn p z hp := by + rw [ksb_ww, fkb_val] + hwcw hp <;> simp only [Nat.reducePow] <;> omega + typed a ha := capTypedw ha + irq i hi := by + rw [ksb_ww, capVw_nodeA i hi] + exact ⟨fun _ _ _ _ _ _ h => (by cases h), fun _ _ _ _ h => (by cases h), fun _ _ h => (by cases h)⟩ + fh t ht := by + have h : AbsSlot thWw (t.bv, Fl4Spec.tcbCnodeIndex 3) := by + unfold AbsSlot; exact ⟨t, rfl, Or.inr ⟨ht, 3, by decide, rfl⟩⟩ + left + have hs := AbsSlot.isSlot h + have hsv : (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 3)).val = t.val + 96 := + IsSlotAt.val placed_thWw (i := 3) (Or.inr ⟨ht, by unfold tcbSlots; omega⟩) + have ht' : t.val = 18446744071562072064 ∨ t.val = 18446744071562100736 ∨ t.val = 18446744071562113024 := by + rcases thWw_pos ht with ⟨e1, -⟩ | ⟨e1, -⟩ | ⟨e1, -⟩ | ⟨-, e2⟩ | ⟨e2, -⟩ + · exact Or.inl e1 + · exact Or.inr (Or.inl e1) + · exact Or.inr (Or.inr e1) + · cases e2 + · cases e2 + exact capVw_val hs (by rw [hsv]; omega) (by rw [hsv]; omega) (by rw [hsv]; omega) (by rw [hsv]; omega) + (by rw [hsv]; omega) (by rw [hsv]; omega) (by rw [hsv]; omega) + bfs t ht := by + have h : AbsSlot thWw (t.bv, Fl4Spec.tcbCnodeIndex 2) := by + unfold AbsSlot; exact ⟨t, rfl, Or.inr ⟨ht, 2, by decide, rfl⟩⟩ + have hs := AbsSlot.isSlot h + have hsv : (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 2)).val = t.val + 64 := + IsSlotAt.val placed_thWw (i := 2) (Or.inr ⟨ht, by unfold tcbSlots; omega⟩) + have ht' : t.val = 18446744071562072064 ∨ t.val = 18446744071562100736 ∨ t.val = 18446744071562113024 := by + rcases thWw_pos ht with ⟨e1, -⟩ | ⟨e1, -⟩ | ⟨e1, -⟩ | ⟨-, e2⟩ | ⟨e2, -⟩ + · exact Or.inl e1 + · exact Or.inr (Or.inl e1) + · exact Or.inr (Or.inr e1) + · cases e2 + · cases e2 + rw [capVw_val hs (by rw [hsv]; omega) (by rw [hsv]; omega) (by rw [hsv]; omega) (by rw [hsv]; omega) + (by rw [hsv]; omega) (by rw [hsv]; omega) (by rw [hsv]; omega)] + exact Or.inl rfl + cobj a ha := by + rcases slotw_cases ha with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] + · trivial + · trivial + · exact ⟨Hww_rS, by rw [idleScW_ww]; decide⟩ + · trivial + · trivial + · trivial + · trivial + · trivial + +theorem ww_jpl : ∃ Tr Cq, Sched.JPlaced (L := wL) thWw Hww Tr Cq ww σw := ⟨Tw, C1, ww_jplaced⟩ + +/-- **`SpecCover` at `ww`.** -/ +theorem ww_cover : Sched.SpecCover Hww σw where + ntfn o x h := (kh_no_ntfn h).elim + ep o x h := (kh_no_ep h).elim + reply o x h := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · rcases bootHeap_fInit.obj _ _ h' with h1 | h1 | e | e | e | e + · subst h1; rw [bootHeap_fInit.tcb] at h'; cases h' + · subst h1; rw [bootHeap_fInit.sc] at h'; cases h' + all_goals cases e + sc0 y k h := by + have h' : khW (0#u64 : U64).bv = some (.schedContext y k) := h + rcases kh_sc_of h' with ⟨e, -⟩ | ⟨e, -⟩ <;> exact absurd e (by decide) + arch o ao h := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, -⟩ | h' + · cases e + · cases e + · cases e + · cases e + · exact ⟨15, by omega, Hww_vB⟩ + · obtain ⟨n, hn, hH⟩ := w0_cover.arch o ao h' + refine ⟨n, hn, ?_⟩ + have hne : ∀ q : U64, fInitKheap q.bv = none → (⟨o⟩ : U64) ≠ q := fun q hq e => by + have : o = q.bv := congrArg UScalar.bv e + rw [this, hq] at h'; cases h' + have n1 := hne rT (fInitKheap_rT) + have n2 : (⟨o⟩ : U64) ≠ gT := hne gT (fInitKheap_none (by decide) (by decide) (by decide) (by decide) + (by decide) gT_irq) + have n3 := hne rS (fInitKheap_rS) + have n4 := hne rC (fInitKheap_rC) + have n5 : (⟨o⟩ : U64) ≠ vB := hne vB (fInitKheap_none (by decide) (by decide) (by decide) (by decide) + (by decide) vB_irq) + rw [Hww_old n1 n2 n3 n4 n5] + exact hH + +theorem ww_zH : (∀ t ∈ Tw, Hww t = some .tcb) ∧ (∀ e ∈ (∅ : Set U64), Hww e = some .ep) ∧ + (∀ r ∈ (∅ : Set U64), Hww r = some .reply) ∧ (∀ n ∈ (∅ : Set U64), Hww n = some .ntfn) := + ⟨fun t ht => by rcases memTw ht with rfl | rfl | rfl; exact Hww_idT; exact Hww_rT; exact Hww_gT, + fun e he => by simp at he, fun r hr => by simp at hr, fun n hn => by simp at hn⟩ + +theorem ww_hset : (∀ e, Hww e = some .ep → e ∈ (∅ : Set U64)) ∧ (∀ n, Hww n = some .ntfn → n ∈ (∅ : Set U64)) ∧ + (∀ r, Hww r = some .reply → r ∈ (∅ : Set U64)) := + ⟨fun e h => by hwcw h, fun n h => by hwcw h, fun r h => by hwcw h⟩ + +theorem ww_hscC : ∀ c z, Hww c = some (.sc z) → c ∈ C1 := by + intro c z h + rcases Hww_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, -, -, -, -, h'⟩ + · cases e + · cases e + · exact rS_mem + · cases e + · cases e + · have := w0_hscC c z h'; rw [Set.mem_singleton_iff.mp this]; exact idS_mem + +/-! ## The cover and the dead set -/ + +theorem ksObjw_blk {q : U64} (h1 : 18446744071562133504 ≤ q.val) (h2 : q.val < 18446744071562133504 + 13568) : + KsObj (L := wL) thWw Hww ww q := by + refine ⟨fun k hk => ?_, fun k hk => ?_⟩ <;> unfold KsIn <;> rw [ksb_ww, fkb_val] + · thcw hk <;> + simp only [objSize, Nat.pow_zero, Nat.mul_one, Nat.pow_one, show max 32 1 = 32 by decide, + show max 4096 1 = 4096 by decide, show max (32 * 2 ^ 3) 1 = 256 by decide, Nat.reducePow] <;> omega + · hwcw hk <;> simp only [HKind.bits, Nat.reducePow] <;> omega + +theorem Hww_D {q : U64} (hq : q ∈ Dw) : Hww q = some (.other 12) ∨ Hww q = some (.other 5) := by + have hq' := hq + simp only [Dw, Set.mem_ofPred_eq] at hq' + have ne : ∀ r : U64, q.val ≠ r.val → q ≠ r := fun r h e => h (by rw [e]) + rw [Hww_old (ne rT (by rw [rT_val]; omega)) (ne gT (by rw [gT_val]; omega)) (ne rS (by rw [rS_val]; omega)) + (ne rC (by rw [rC_val]; omega)) (ne vB (by rw [vB_val]; omega))] + exact Hw_D hq + +theorem cov_rT : Covered (wL.mem ww) Gw rT.val (rT.val + 4096) := + ⟨rC0, Gw_flat.2 (Or.inr (Or.inl rfl)), by rw [capV_rC0]; exact physF_thC, by rw [capV_rC0]; rfl, + by rw [capV_rC0, base_thC]; rfl, by rw [capV_rC0, top_thC]; rfl⟩ + +theorem cov_gT : Covered (wL.mem ww) Gw gT.val (gT.val + 4096) := + ⟨rC4, Gw_flat.2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl rfl)))))), by rw [capV_rC4]; exact physF_gthC, + by rw [capV_rC4]; rfl, by rw [capV_rC4, base_gthC]; rfl, by rw [capV_rC4, top_gthC]; rfl⟩ + +theorem cov_rS : Covered (wL.mem ww) Gw rS.val (rS.val + 128) := + ⟨rC1, Gw_flat.2 (Or.inr (Or.inr (Or.inl rfl))), by rw [capV_rC1]; exact physF_scC, by rw [capV_rC1]; rfl, + by rw [capV_rC1, base_scC]; rfl, by rw [capV_rC1, top_scC]; rfl⟩ + +theorem cov_rC : Covered (wL.mem ww) Gw rC.val (rC.val + 256) := + ⟨rT, Gw_flat.2 (Or.inl rfl), by rw [capV_rT]; exact physF_cnC, by rw [capV_rT]; rfl, + by rw [capV_rT, base_cnC]; rfl, by rw [capV_rT, top_cnC]; rfl⟩ + +theorem cov_vB : Covered (wL.mem ww) Gw vB.val (vB.val + 32768) := + ⟨rC5, Gw_flat.2 (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))))), by rw [capV_rC5]; exact physF_vcC, + by rw [capV_rC5]; rfl, by rw [capV_rC5, base_vcC]; rfl, by rw [capV_rC5, top_vcC]; rfl⟩ + +/-- **`JCovD` at `ww`** (module docstring). -/ +theorem ww_jcov : Sched.JCovD (L := wL) thWw Hww Gw Dw Csw ww where + th q k hq := by + rcases thWw_some hq with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, -, -, h'⟩ + · exact Or.inl cov_rT + · exact Or.inl cov_gT + · exact Or.inl cov_rC + · rcases thW_pos h' with ⟨e, -⟩ | ⟨-, ⟨i, hi, e⟩ | ⟨i, hi, hi2, e⟩⟩ + · have : q = idT := UScalar.eq_of_val_eq (by rw [e, idT_val]) + subst this + exact Or.inr (Or.inr (Or.inl (Or.inl idleW_ww.symm))) + · exact Or.inr (Or.inr (Or.inr (ksObjw_blk (by omega) (by omega)))) + · exact Or.inr (Or.inl (Or.inl ⟨by omega, by omega, by omega⟩)) + h q k hq := by + rcases Hww_some hq with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, -, -, -, -, h'⟩ + · exact Or.inl cov_rT + · exact Or.inl cov_gT + · exact Or.inl cov_rS + · exact Or.inl cov_rC + · exact Or.inl cov_vB + · rcases Hw_pos h' with ⟨e, -⟩ | ⟨e, -⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ + · have : q = idT := UScalar.eq_of_val_eq (by rw [e, idT_val]) + subst this + exact Or.inr (Or.inr (Or.inl (Or.inl idleW_ww.symm))) + · have : q = idS := UScalar.eq_of_val_eq (by rw [e, idS_val]) + subst this + exact Or.inr (Or.inr (Or.inl (Or.inr idleScW_ww.symm))) + · exact Or.inr (Or.inl (Or.inr e)) + · exact Or.inr (Or.inl (Or.inl e)) + · exact Or.inr (Or.inr (Or.inr (ksObjw_blk (by omega) (by omega)))) + · exact Or.inr (Or.inr (Or.inr (ksObjw_blk (by omega) (by omega)))) + dead q hq := by + refine ⟨fun h => ?_, fun h => ?_, fun h => ?_, fun h => ?_, fun z h => ?_⟩ <;> + rcases Hww_D hq with e | e <;> rw [e] at h <;> cases h + ks x hx p sb fi h := by + obtain ⟨-, rfl, rfl, -, -⟩ := flatw_ut hx h + have e12 : (12#u64).val = 12 := rfl + rw [uB_val, e12, ksb_ww, fkb_val] + refine ⟨by left; omega, fun c hc => ?_, by left; omega⟩ + rw [Csw, Set.mem_singleton_iff] at hc; subst hc + rw [nodeBase_ww]; omega + idleOut x hx p sb fi h q hq := by + obtain ⟨-, rfl, rfl, -, -⟩ := flatw_ut hx h + have e12 : (12#u64).val = 12 := rfl + rw [uB_val, e12] + have hq' : q = idT ∨ q = idS := by + rcases hq with e | e + · exact Or.inl (e.trans idleW_ww) + · exact Or.inr (e.trans idleScW_ww) + have hv : q.val = 18446744071562072064 ∨ q.val = 18446744071562096640 := by + rcases hq' with rfl | rfl + · exact Or.inl idT_val + · exact Or.inr idS_val + refine ⟨by omega, fun k hk => ?_, fun k hk => ?_⟩ + · thcw hk <;> simp only [objSize, Nat.reducePow] <;> omega + · hwcw hk <;> simp only [HKind.bits, Nat.reducePow] <;> omega + dx q hq h := by rcases Hww_D hq with e | e <;> rw [e] at h <;> cases h + +/-! ## `JKern` -/ + +/-- **`JKern` at `ww`.** -/ +theorem ww_jkern : JKern (L := wL) warI Hww Csw Dw thWw ww σw where + orig := ww_orig + cur := by rw [curW_ww]; rfl + curT := by rw [curW_ww]; exact thWw_rT + act := by + have hv := actA_vw + refine ⟨by show (wL.nodeAt ww (wL.cpu ww) layout.node.SCHED_ACTION).val % 8 = 0; omega, + fun p k hp hin => ?_⟩ + rw [hv] at hin + thcw hp <;> simp only [objSize, Nat.reducePow] at hin <;> omega + dwAl := by have := dbgA_vw; show (wL.nodeAt ww 0#u64 layout.node.DEBUG_TCBS).val % 8 = 0; omega + dwObj a ha hin := by + have hv := dbgA_vw + obtain ⟨h0, h31⟩ := slot_endsw ha + unfold SlotNatw SlotNat at h0 h31 + omega + dwNe := by + rw [dbgA_vw, curW_ww, stA_rT, actA_vw] + omega + head := Or.inl (by rw [memw_al dbgA_vw (by omega)]; imgwz) + gpm := ⟨_, by rw [σw_kheap, kh_old (by decide) (by decide) (by decide) (by decide) (by decide)]; exact bootHeap_fInit.pml4⟩ + ga := fInit_gpm_al.1 + gf := fInit_gpm_al.2 + heap := heapOK_Hww + h0 := Hww_zero + hks := ww_jplaced.ks + hksIn := ww_jplaced.ksIn + hnd p k hp c hc := by + rw [Csw, Set.mem_singleton_iff] at hc; subst hc + rw [nodeBase_ww] + hwcw hp <;> simp only [HKind.bits, Nat.reducePow] <;> omega + ksbFit := ww_jkernNum.1 + ksbAl := ww_jkernNum.2.1 + nodeFit := ww_jkernNum.2.2.1 + cpuCs := ww_jkernNum.2.2.2.1 + curH := by rw [curW_ww]; exact Hww_rT + cscH := by rw [cscW_ww, Hww_rS]; exact Option.some_ne_none _ + csLt := ww_jkernNum.2.2.2.2.1 + cs0 := ww_jkernNum.2.2.2.2.2.1 + csN := ww_jkernNum.2.2.2.2.2.2 + tdx := ww_tdx + +/-- **`JAtZ.zD` at `ww`**: no slot holds a zombie. -/ +theorem ww_zD : ∀ p ∈ ZTcb (L := wL) thWw ww, p ∈ Dw := by + intro p hp + obtain ⟨y, hy, zt, n, hc, -⟩ := hp + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] at hc <;> cases hc + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitMem.lean b/proofs/Fl4Proofs/P3/VmWitMem.lean new file mode 100644 index 000000000..3c70c00d1 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitMem.lean @@ -0,0 +1,917 @@ +/- +P3 (line mmuAD witness): **the VM witness's memory image `ww`** and the scheduler / IPC-state / kernel-global +relations at `ww` / `σw` (P3/VmWitSpec). Generated by `handoff/tools/gen_witmem.py` from P3/JBootQ1Mem. + +`ww` is `w0` with 24 words changed (`imgw`): core 0's current thread and SC; the root TCB, SC and radix-3 root +CNode (thread, SC, untyped, SchedControl, guest thread and VCPU caps; the untyped cap revocable); the guest TCB +(`STATE` = 8, `TS_RUNNING_VM`; `VCPU` = `vB`); the VCPU list head and the VCPU's `TCB` word; core 0's `CUR_TIME` +(`tWw`, the spec clock `tW`, P3/VmWitSpec). + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.JBootQ1Mem +import Fl4Proofs.P3.VmWitSpec +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif Fl4Proofs.P3.Vs + Fl4Proofs.P2.Wit + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-! ## The image -/ + +/-- The root CNode cap's first word (type 5, radix 3, pointer `rC`). -/ +def cnW3 : U64 := 2883429659276517376#u64 +/-- The root thread cap's first word. -/ +def thrW : U64 := 3459045986649800704#u64 +/-- The root SC cap's first word. -/ +def sccW : U64 := 6343320073003896832#u64 +/-- The untyped cap's first word (type 1, 4 KiB, pointer `uB`). -/ +def utWd : U64 := 580119924853227520#u64 +/-- The SchedControl cap's first word (type 12, core 0 in the second word). -/ +def sctlW : U64 := 6917529027641081856#u64 +/-- The guest thread cap's first word. -/ +def gthrW : U64 := 3459045986649812992#u64 +/-- The VCPU cap's first word (type 22, pointer `vB`). -/ +def vcWd : U64 := 12682418023505592320#u64 +/-- The untyped slot's MDB word: revocable. -/ +def revW : U64 := 281474976710656#u64 + +/-- The root TCB's capability register: the cptr of slot 3 (SchedControl). -/ +def cptrW : U64 := 6917529027641081856#u64 +/-- The root TCB's reply register: the cptr of slot 2 (Untyped). -/ +def rptrW : U64 := 4611686018427387904#u64 +/-- Core 0's current time: the spec witness's clock `tW` (P3/VmWitSpec). -/ +def tWw : U64 := ⟨tW⟩ + +/-- **The witness image** (module docstring). -/ +def imgw (a : Nat) : U64 := + if a = 18446744071562147072 then rT else + if a = 18446744071562147096 then rS else + if a = 18446744071562104064 then 1#u64 else + if a = 18446744071562104200 then rS else + if a = 18446744071562100736 then cnW3 else + if a = 18446744071562104856 then rT else + if a = 18446744071562104904 then 2#u64 else + if a = 18446744071562104920 then 1#u64 else + if a = 18446744071562104936 then mpW else + if a = 18446744071562104944 then mpW else + if a = 18446744071562108928 then thrW else + if a = 18446744071562108960 then sccW else + if a = 18446744071562108992 then utWd else + if a = 18446744071562109016 then revW else + if a = 18446744071562109024 then sctlW else + if a = 18446744071562109056 then gthrW else + if a = 18446744071562109088 then vcWd else + if a = 18446744071562116352 then 8#u64 else + if a = 18446744071562116688 then vB else + if a = 18446744071562133584 then vB else + if a = 18446744071563137024 then gT else + if a = 18446744071562100992 then cptrW else + if a = 18446744071562101032 then rptrW else + if a = 18446744071562147120 then tWw else img a + +/-- **The witness's FL4 store state**: the image, base `fkb`, CPU 0. -/ +def ww : WSt := { w := fun i => imgw (8 * i.val), kb := fkb, cpu := 0#u64 } + +theorem mem_ww (a : U64) : wL.mem ww a = imgw (8 * (a.val / 8)) := rfl + +theorem memw_al {a : U64} {n : Nat} (h : a.val = n) (h8 : n % 8 = 0) : wL.mem ww a = imgw n := by + rw [mem_ww, h]; congr 1; omega + +theorem ksb_ww : wL.ksb ww = fkb := rfl +theorem cpu_ww : wL.cpu ww = 0#u64 := rfl + +/-- `imgw` is `img` away from the changed words. -/ +theorem imgw_old {a : Nat} + (h1 : a ≠ 18446744071562147072) + (h2 : a ≠ 18446744071562147096) + (h3 : a ≠ 18446744071562104064) + (h4 : a ≠ 18446744071562104200) + (h5 : a ≠ 18446744071562100736) + (h6 : a ≠ 18446744071562104856) + (h7 : a ≠ 18446744071562104904) + (h8 : a ≠ 18446744071562104920) + (h9 : a ≠ 18446744071562104936) + (h10 : a ≠ 18446744071562104944) + (h11 : a ≠ 18446744071562108928) + (h12 : a ≠ 18446744071562108960) + (h13 : a ≠ 18446744071562108992) + (h14 : a ≠ 18446744071562109016) + (h15 : a ≠ 18446744071562109024) + (h16 : a ≠ 18446744071562109056) + (h17 : a ≠ 18446744071562109088) + (h18 : a ≠ 18446744071562116352) + (h19 : a ≠ 18446744071562116688) + (h20 : a ≠ 18446744071562133584) + (h21 : a ≠ 18446744071563137024) + (h22 : a ≠ 18446744071562100992) + (h23 : a ≠ 18446744071562101032) + (h24 : a ≠ 18446744071562147120) : + imgw a = img a := by + unfold imgw; 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, if_neg h9, if_neg h10, if_neg h11, if_neg h12, if_neg h13, if_neg h14, if_neg h15, if_neg h16, if_neg h17, if_neg h18, if_neg h19, if_neg h20, if_neg h21, if_neg h22, if_neg h23, if_neg h24] + +/-- `imgw` away from every nonzero word. -/ +theorem imgw_zero {a : Nat} + (h1 : a ≠ 18446744071562147072) + (h2 : a ≠ 18446744071562147096) + (h3 : a ≠ 18446744071562104064) + (h4 : a ≠ 18446744071562104200) + (h5 : a ≠ 18446744071562100736) + (h6 : a ≠ 18446744071562104856) + (h7 : a ≠ 18446744071562104904) + (h8 : a ≠ 18446744071562104920) + (h9 : a ≠ 18446744071562104936) + (h10 : a ≠ 18446744071562104944) + (h11 : a ≠ 18446744071562108928) + (h12 : a ≠ 18446744071562108960) + (h13 : a ≠ 18446744071562108992) + (h14 : a ≠ 18446744071562109016) + (h15 : a ≠ 18446744071562109024) + (h16 : a ≠ 18446744071562109056) + (h17 : a ≠ 18446744071562109088) + (h18 : a ≠ 18446744071562116352) + (h19 : a ≠ 18446744071562116688) + (h20 : a ≠ 18446744071562133584) + (h21 : a ≠ 18446744071563137024) + (h22 : a ≠ 18446744071562100992) + (h23 : a ≠ 18446744071562101032) + (h24 : a ≠ 18446744071562147120) + (g1 : a ≠ 18446744071562133504) + (g2 : a ≠ 18446744071562133520) + (g3 : a ≠ 18446744071562133552) + (g4 : a ≠ 18446744071562133576) + (g5 : a ≠ 18446744071562133704) + (g7 : a ≠ 18446744071562147080) + (g9 : a ≠ 18446744071562147104) + (g10 : a ≠ 18446744071562075392) + (g11 : a ≠ 18446744071562075528) + (g12 : a ≠ 18446744071562096664) + (g13 : a ≠ 18446744071562096712) + (g14 : a ≠ 18446744071562096728) + (g15 : a ≠ 18446744071562096744) + (g16 : a ≠ 18446744071562096752) + (g17 : a ≠ 18446744071562134728) + (g18 : a ≠ 18446744071562134736) + (g19 : a ≠ 18446744071562134744) : + imgw a = 0#u64 := by + rw [imgw_old h1 h2 h3 h4 h5 h6 h7 h8 h9 h10 h11 h12 h13 h14 h15 h16 h17 h18 h19 h20 h21 h22 h23 h24] + exact img_zero g1 g2 g3 g4 g5 h1 g7 h2 g9 g10 g11 g12 g13 g14 g15 g16 g17 g18 g19 + +/-- `imgw` away from its nonzero words, the side conditions by `omega`. -/ +macro "imgwz" : tactic => `(tactic| (apply imgw_zero <;> omega)) + +/-- **`ww` and `w0` agree below the root TCB**, away from the node words. -/ +theorem memw_old {a : U64} + (h : a.val < 18446744071562100736) + (hc : a.val / 8 ≠ 18446744071562147072 / 8) (hs : a.val / 8 ≠ 18446744071562147096 / 8) : + wL.mem ww a = wL.mem w0 a := by + rw [mem_ww, mem_w0]; apply imgw_old <;> omega + +/-! ## The interrupt, IO-port and kernel-root words -/ + +/-- **`IrqRel` at `ww`** (gen 15 re-proof: the three IRQ-state words of FL4's boot layout are not among +`imgw`'s twelve changed words, so `ww` reads `w0`'s there and `JBootQ.w0_irqRel` carries over). -/ +theorem ww_irqRel : IrqRel (wL.mem ww) (wL.ksb ww) σw := by + refine ⟨fun i hi => ?_, fun i hi => fInitASt_irqNode i hi⟩ + have hv := irqStA_val (kb := fkb) fkb_lt hi + rw [fkb_val] at hv + have h0 := w0_irqRel.st i hi + rw [ksb_w0, mem_al hv (by omega)] at h0 + rw [ksb_ww, memw_al hv (by omega), + show imgw (18446744071562133504 + 224 + 8 * i.val) = img (18446744071562133504 + 224 + 8 * i.val) by + apply imgw_old <;> omega] + exact h0 + +/-- **`IoRel` at `ww`.** -/ +theorem ww_ioRel : IoRel (wL.mem ww) (wL.ksb ww) σw := by + intro p hp + have hw : (wI p).val < 1024 := by rw [wI_val hp]; omega + have hv := ioA_val (kb := fkb) fkb_lt hw + rw [fkb_val] at hv + unfold bitOf + rw [ksb_ww, memw_al hv (by omega), show imgw (18446744071562133504 + 5344 + 8 * (wI p).val) = 0#u64 by imgwz] + simp [σw, σ1, fInitASt, initASt, initArchState] + +/-- **`KRootRel` at `ww`.** -/ +theorem ww_kRootRel : KRootRel (wL.mem ww) (wL.ksb ww) σw := by + refine ⟨?_, ?_⟩ + · unfold KRootW + have hv : (core.num.U64.wrapping_add fkb layout.ks.KERNEL_ROOT).val = 18446744071562133552 := by + unfold layout.ks.KERNEL_ROOT + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, fkb_val] + rw [ksb_ww, memw_al hv (by omega), show imgw 18446744071562133552 = kRootV by decide] + unfold globCr3 + show makeCr3 (addrFromPPtr kRootV.bv) 0 = makeCr3 (addrFromKPPtr initGlobalPml4) 0 + decide + · unfold HugeRel + have hv : (core.num.U64.wrapping_add fkb layout.ks.FEATURES).val = 18446744071562133576 := by + unfold layout.ks.FEATURES + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, fkb_val] + rw [ksb_ww, memw_al hv (by omega), show imgw 18446744071562133576 = 2#u64 by decide] + unfold layout.ks.FEATURE_HUGE_PAGES + decide + +/-- **`DomRel` at `ww`.** -/ +theorem ww_domRel : DomRel (wL.mem ww) (wL.ksb ww) σw := by + have ent : ∀ i : U64, i.val < 2 → (ksW fkb (entOff i)).val = 18446744071562133504 + 192 + 16 * i.val := by + intro i hi + unfold ksW entOff + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, fkb_val] + omega + have dur : ∀ i : U64, i.val < 2 → (ksW fkb (durOff i)).val = 18446744071562133504 + 200 + 16 * i.val := by + intro i hi + unfold ksW durOff entOff + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, fkb_val] + omega + have k32 : (ksW fkb 32#u64).val = 18446744071562133536 := by + rw [ksW_val (by rw [fkb_val]; decide), fkb_val]; rfl + have k24 : (ksW fkb 24#u64).val = 18446744071562133528 := by + rw [ksW_val (by rw [fkb_val]; decide), fkb_val]; rfl + have hl : σw.domainList = [((0 : BitVec 8), (-1 : BitVec 56)), (0, 0)] := rfl + refine ⟨rfl, fun i hi => ?_, ?_, ?_⟩ + · rw [ksb_ww, memw_al (ent i hi) (by omega), memw_al (dur i hi) (by omega), hl] + have h01 : i.val = 0 ∨ i.val = 1 := by omega + rcases h01 with h0 | h1 + · rw [h0, show imgw (18446744071562133504 + 192 + 16 * 0) = 0#u64 by decide, + show imgw (18446744071562133504 + 200 + 16 * 0) = 72057594037927935#u64 by decide] + exact ⟨rfl, by decide, by decide, by decide⟩ + · rw [h1, show imgw (18446744071562133504 + 192 + 16 * 1) = 0#u64 by decide, + show imgw (18446744071562133504 + 200 + 16 * 1) = 0#u64 by decide] + exact ⟨rfl, by decide, by decide, fun _ => rfl⟩ + · rw [ksb_ww, memw_al k32 (by omega), show imgw 18446744071562133536 = 0#u64 by decide]; rfl + · rw [ksb_ww, memw_al k24 (by omega), show imgw 18446744071562133528 = 0#u64 by decide] + +theorem ww_ar (a : MachineState) : wAR a σw.archState.x64CurrentCr3 := trivial + +/-- **`JKern`'s kernel-state facts at `ww`.** -/ +theorem ww_jkernNum : + (wL.ksb ww).val + 13568 ≤ 2 ^ 64 ∧ (wL.ksb ww).val % 8 = 0 ∧ + (wL.ksb ww).val + 13568 + ((wL.cpu ww).val + 1) * 4288 ≤ 2 ^ 64 ∧ wL.cpu ww ∈ ({0#u64} : Set U64) ∧ + (∀ c ∈ ({0#u64} : Set U64), c.val < 64) ∧ (0#u64 ∈ ({0#u64} : Set U64)) ∧ + ∃ n : Nat, ∀ c : U64, c ∈ ({0#u64} : Set U64) ↔ c.val < n := w0_jkernNum + +/-! ## Reading the words of the objects -/ + +theorem nodeAt1_v (o : U64) (ho : o.val < 4288) : (wL.nodeAt ww 0#u64 o).val = 18446744071562147072 + o.val := + nodeAt_v o ho + +theorem nodeW1_v (o : U64) (ho : o.val < 4288) (h8 : o.val % 8 = 0) : + nodeW wst ww 0#u64 o = imgw (18446744071562147072 + o.val) := + memw_al (nodeAt1_v o ho) (by omega) + +theorem nodeBase_ww : wL.nodeBase ww 0#u64 = 18446744071562147072 := nodeBase_w0 + +/-- A word of the idle TCB. -/ +theorem tcbW1_v (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) : + wL.mem ww (core.num.U64.wrapping_add idT K) = imgw (18446744071562072064 + K.val) := + memw_al (by rw [wa_val (by rw [idT_val]; omega), idT_val]) (by omega) + +/-- A word of the root TCB. -/ +theorem rtW_v (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) : + wL.mem ww (core.num.U64.wrapping_add rT K) = imgw (18446744071562100736 + K.val) := + memw_al (by rw [wa_val (by rw [rT_val]; omega), rT_val]) (by omega) + +/-- A word of the idle SC. -/ +theorem scW1_v (K : U64) (hK : K.val < 128) (h8 : K.val % 8 = 0) : + wL.mem ww (core.num.U64.wrapping_add idS K) = imgw (18446744071562096640 + K.val) := + memw_al (by rw [wa_val (by rw [idS_val]; omega), idS_val]) (by omega) + +/-- A word of the guest TCB. -/ +theorem gtW_v (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) : + wL.mem ww (core.num.U64.wrapping_add gT K) = imgw (18446744071562113024 + K.val) := + memw_al (by rw [wa_val (by rw [show gT.val = 18446744071562113024 from rfl]; omega), show gT.val = 18446744071562113024 from rfl]) (by omega) + +/-- A word of the root SC. -/ +theorem rsW_v (K : U64) (hK : K.val < 128) (h8 : K.val % 8 = 0) : + wL.mem ww (core.num.U64.wrapping_add rS K) = imgw (18446744071562104832 + K.val) := + memw_al (by rw [wa_val (by rw [rS_val]; omega), rS_val]) (by omega) + +theorem tcbZ1 (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hs : K.val ≠ 3328) (hc : K.val ≠ 3464) : + wL.mem ww (core.num.U64.wrapping_add idT K) = 0#u64 := by + rw [tcbW1_v K hK h8]; imgwz + +theorem rtZ (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hs : K.val ≠ 3328) (hc : K.val ≠ 3464) + (h0 : K.val ≠ 0) (hr0 : K.val ≠ 256 := by decide) (hr5 : K.val ≠ 296 := by decide) : + wL.mem ww (core.num.U64.wrapping_add rT K) = 0#u64 := by + rw [rtW_v K hK h8]; imgwz + +theorem gtZ (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hs : K.val ≠ 3328) (hv : K.val ≠ 3664) : + wL.mem ww (core.num.U64.wrapping_add gT K) = 0#u64 := by + rw [gtW_v K hK h8]; imgwz + +/-- The TCBs of `σw`. -/ +def Tw : Set U64 := {idT, rT, gT} + +theorem idT_mem : idT ∈ Tw := Or.inl rfl +theorem rT_mem : rT ∈ Tw := Or.inr (Or.inl rfl) +theorem gT_mem : gT ∈ Tw := Or.inr (Or.inr rfl) +theorem memTw {t : U64} (h : t ∈ Tw) : t = idT ∨ t = rT ∨ t = gT := h + +/-! ## The core part -/ + +theorem rq1_nil (p : U64) (hp : p.val < 256) : wL.rqQ ww 0#u64 p = ⟨0#u64, 0#u64⟩ := by + unfold LawfulStore.rqQ + have h1 : (rqOff p).val = 168 + 16 * p.val := by simp [rqOff]; omega + have h2 : (rqTailOff p).val = 176 + 16 * p.val := by simp [rqTailOff]; omega + rw [memw_al (nodeAt1_v _ (by omega)) (by omega), memw_al (nodeAt1_v _ (by omega)) (by omega), h1, h2] + congr 1 <;> imgwz + +theorem l1_zero1 : wL.l1W ww 0#u64 = 0#u64 := by + unfold LawfulStore.l1W + have h1 : (l1Off).val = 128 := by simp [l1Off] + rw [memw_al (nodeAt1_v _ (by omega)) (by omega), h1]; imgwz + +theorem l2_zero1 (g : Nat) (hg : g < 4) : wL.l2W ww 0#u64 g = 0#u64 := by + unfold LawfulStore.l2W + have h1 : (l2Off g).val = 136 + 8 * (3 - g) := by simp [l2Off]; omega + rw [memw_al (nodeAt1_v _ (by omega)) (by omega), h1]; imgwz + +theorem rel1_nil : wL.relQ ww 0#u64 = ⟨0#u64, 0#u64⟩ := by + unfold LawfulStore.relQ + have h1 : (layout.node.RELEASE_HEAD).val = 64 := by unfold layout.node.RELEASE_HEAD; rfl + have h2 : (core.num.U64.wrapping_add layout.node.RELEASE_HEAD 8#u64).val = 72 := by + unfold layout.node.RELEASE_HEAD; rfl + rw [memw_al (nodeAt1_v _ (by omega)) (by omega), memw_al (nodeAt1_v _ (by omega)) (by omega), h1, h2] + congr 1 <;> imgwz + +theorem curT_ww : nodeW wst ww 0#u64 layout.node.CUR_THREAD = rT := by + rw [show layout.node.CUR_THREAD = 0#u64 by unfold layout.node.CUR_THREAD; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +theorem idleT_ww : nodeW wst ww 0#u64 layout.node.IDLE_THREAD = idT := by + rw [show layout.node.IDLE_THREAD = 8#u64 by unfold layout.node.IDLE_THREAD; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +theorem curSc_ww : nodeW wst ww 0#u64 layout.node.CUR_SC = rS := by + rw [show layout.node.CUR_SC = 24#u64 by unfold layout.node.CUR_SC; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +theorem idleSc_ww : nodeW wst ww 0#u64 layout.node.IDLE_SC = idS := by + rw [show layout.node.IDLE_SC = 32#u64 by unfold layout.node.IDLE_SC; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +theorem act_ww : nodeW wst ww 0#u64 layout.node.SCHED_ACTION = 0#u64 := by + rw [show layout.node.SCHED_ACTION = 16#u64 by unfold layout.node.SCHED_ACTION; rfl, + nodeW1_v _ (by decide) (by decide)] + decide + +theorem repr_ww : nodeW wst ww 0#u64 layout.node.REPROGRAM = 0#u64 := by + rw [show layout.node.REPROGRAM = 56#u64 by unfold layout.node.REPROGRAM; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +theorem time_ww : nodeW wst ww 0#u64 layout.node.CUR_TIME = tWw := by + rw [show layout.node.CUR_TIME = 48#u64 by unfold layout.node.CUR_TIME; rfl, nodeW1_v _ (by decide) (by decide)] + unfold imgw + 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_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_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_neg (by decide), if_neg (by decide)] + exact if_pos (by decide) + +theorem cons_ww : nodeW wst ww 0#u64 layout.node.CONSUMED = 0#u64 := by + rw [show layout.node.CONSUMED = 40#u64 by unfold layout.node.CONSUMED; rfl, nodeW1_v _ (by decide) (by decide)] + decide + +/-- The TCB words P3 reads by name, at both TCBs: every one zero but `STATE` and `SC`. -/ +theorem inq_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (inqA t) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · unfold inqA; exact tcbZ1 _ (by decide) (by decide) (by decide) (by decide) + · unfold inqA; exact rtZ _ (by decide) (by decide) (by decide) (by decide) (by decide) + · unfold inqA; exact gtZ _ (by decide) (by decide) (by decide) (by decide) + +theorem aff_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (affA t) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · unfold affA; exact tcbZ1 _ (by decide) (by decide) (by decide) (by decide) + · unfold affA; exact rtZ _ (by decide) (by decide) (by decide) (by decide) (by decide) + · unfold affA; exact gtZ _ (by decide) (by decide) (by decide) (by decide) + +theorem prio_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (prioA t) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · unfold prioA; exact tcbZ1 _ (by decide) (by decide) (by decide) (by decide) + · unfold prioA; exact rtZ _ (by decide) (by decide) (by decide) (by decide) (by decide) + · unfold prioA; exact gtZ _ (by decide) (by decide) (by decide) (by decide) + +theorem dom_ww (t : U64) (ht : t ∈ Tw) : wL.mem ww (domA t) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · unfold domA; exact tcbZ1 _ (by decide) (by decide) (by decide) (by decide) + · unfold domA; exact rtZ _ (by decide) (by decide) (by decide) (by decide) (by decide) + · unfold domA; exact gtZ _ (by decide) (by decide) (by decide) (by decide) + +theorem sc_idT1 : wL.mem ww (scA idT) = idS := by + unfold scA; rw [tcbW1_v _ (by decide) (by decide)]; decide + +theorem sc_rT1 : wL.mem ww (scA rT) = rS := by + unfold scA; rw [rtW_v _ (by decide) (by decide)]; decide + +theorem sc_gT : wL.mem ww (scA gT) = 0#u64 := by + unfold scA; exact gtZ _ (by decide) (by decide) (by decide) (by decide) + +theorem memC1 {c : U64} (h : c ∈ C1) : c = idS ∨ c = rS := h + +theorem tcbPlaced_T1 : TcbPlaced Tw := by + intro t ht + rcases memTw ht with rfl | rfl | rfl + · exact tcb_addr + · exact ⟨by decide, by decide, by decide⟩ + · exact ⟨by decide, by decide, by decide⟩ + +theorem nodePlaced_ww : NodePlaced wst Tw ww 0#u64 := by + refine ⟨?_, ?_, ?_⟩ + · simp [LawfulStore.nodeBase, ksb_ww, fkb_val] + · simp [LawfulStore.nodeBase, ksb_ww, fkb_val] + · intro t ht + left + rcases memTw ht with rfl | rfl | rfl <;> simp [LawfulStore.nodeBase, ksb_ww, fkb_val, idT_val, rT_val, + show gT.val = 18446744071562113024 from rfl] + +/-- **`SchedCore` at `ww`.** -/ +theorem ww_schedCore : SchedCore wst Tw 0#u64 ww (fun _ => []) [] where + tcbs := tcbPlaced_T1 + node := nodePlaced_ww + cur := rfl + aff t ht := aff_ww t ht + prio t ht := by rw [prio_ww t ht]; decide + ready := ⟨⟨fun p hp => by rw [rq1_nil p hp]; exact QueueRel.nil, fun _ _ _ _ _ x hx => (List.not_mem_nil hx).elim⟩, + ⟨fun p hp => by + rw [l2_zero1 _ (by omega)] + simp, + fun i hi => by + rw [l1_zero1] + simp only [show (0#u64).bv.getLsbD i = false by simp, Bool.false_eq_true, false_iff, not_and, not_not] + intro h; rw [l2_zero1 i h]⟩⟩ + release := by rw [rel1_nil]; exact QueueRel.nil + inQ _ _ _ h := (List.not_mem_nil h).elim + queued t ht := by rw [inq_ww t ht]; simp + released t ht := by rw [inq_ww t ht]; simp + excl _ _ h := (List.not_mem_nil h).elim + curT := by rw [curT_ww]; exact rT_mem + idleT := by rw [idleT_ww]; exact idT_mem + ksOut t ht := by + left; rcases memTw ht with rfl | rfl | rfl <;> rw [ksb_ww, fkb_val] <;> decide + +/-! ## The root SC -/ + +theorem rmax_rS : wL.rmax ww rS = 2#u64 := by + unfold LawfulStore.rmax maxA layout.sc.REFILL_MAX; rw [rsW_v _ (by decide) (by decide)]; decide + +theorem rhead_rS : wL.rhead ww rS = 0#u64 := by + unfold LawfulStore.rhead headA layout.sc.REFILL_HEAD; rw [rsW_v _ (by decide) (by decide)]; decide + +theorem rtail_rS : wL.rtail ww rS = 1#u64 := by + unfold LawfulStore.rtail tailA layout.sc.REFILL_TAIL; rw [rsW_v _ (by decide) (by decide)]; decide + +theorem rperiod_rS : wL.rperiod ww rS = 0#u64 := by + unfold LawfulStore.rperiod periodA layout.sc.PERIOD; rw [rsW_v _ (by decide) (by decide)]; decide + +theorem imgw_rmp1 : imgw 18446744071562104936 = mpW := by + unfold imgw + 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_neg (by decide), if_pos rfl] + +theorem imgw_rmp2 : imgw 18446744071562104944 = mpW := by + unfold imgw + 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_neg (by decide), if_neg (by decide), if_pos rfl] + +theorem refills_rS : wL.refills ww rS = [⟨0#u64, mpW⟩, ⟨mpW, 0#u64⟩] := by + unfold LawfulStore.refills LawfulStore.size + rw [rmax_rS, rhead_rS, rtail_rS] + have e : ringN (2#u64).val (0#u64).val (1#u64).val = 2 := by decide + rw [e] + have r0 : wL.rf ww rS 0 = ⟨0#u64, mpW⟩ := by + unfold LawfulStore.rf LawfulStore.refill LawfulStore.ix + rw [rhead_rS, rmax_rS] + have a1 : (rtA rS (u64 (((0#u64).val + 0) % (2#u64).val))).val = 18446744071562104832 + 96 := by + unfold rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + have a2 : (raA rS (u64 (((0#u64).val + 0) % (2#u64).val))).val = 18446744071562104832 + 104 := by + unfold raA rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + rw [memw_al a1 (by omega), memw_al a2 (by omega), show 18446744071562104832 + 104 = 18446744071562104936 by rfl, + imgw_rmp1] + rw [show imgw (18446744071562104832 + 96) = 0#u64 by decide] + have r1 : wL.rf ww rS 1 = ⟨mpW, 0#u64⟩ := by + unfold LawfulStore.rf LawfulStore.refill LawfulStore.ix + rw [rhead_rS, rmax_rS] + have a1 : (rtA rS (u64 (((0#u64).val + 1) % (2#u64).val))).val = 18446744071562104832 + 112 := by + unfold rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + have a2 : (raA rS (u64 (((0#u64).val + 1) % (2#u64).val))).val = 18446744071562104832 + 120 := by + unfold raA rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + rw [memw_al a1 (by omega), memw_al a2 (by omega), show 18446744071562104832 + 112 = 18446744071562104944 by rfl, + imgw_rmp2] + rw [show imgw (18446744071562104832 + 120) = 0#u64 by decide] + show List.map (wL.rf ww rS) [0, 1] = _ + rw [List.map_cons, List.map_cons, List.map_nil, r0, r1] + +theorem placed_rS : P1.Placed rS (0 + minSchedContextBits) := ⟨by decide, by decide, by decide⟩ + +theorem wf_rS : RefillsWF wst ww rS (0 + minSchedContextBits) where + placed := placed_rS + min_le := by rw [rmax_rS]; unfold sporadic.MIN_REFILLS; decide + le_abs := by rw [rmax_rS]; decide + head_lt := by rw [rhead_rS, rmax_rS]; decide + tail_lt := by rw [rtail_rS, rmax_rS]; decide + +theorem rsw (K : U64) (hK : K.val < 128) (h8 : K.val % 8 = 0) : + wL.mem ww (scW rS K) = imgw (18446744071562104832 + K.val) := rsW_v K hK h8 + +theorem optRef_rT : optRef rT = some rT.bv := by unfold optRef; rw [if_neg (by decide)] +theorem optRef_rS : optRef rS = some rS.bv := by unfold optRef; rw [if_neg (by decide)] + +theorem ww_scRel1_rS : ScRel1 wst ww rS rootScV 0 := + ScRel1.ofWF wf_rS + (by rw [rperiod_rS]; rfl) + (by rw [rsw _ (by decide) (by decide)]; decide) + (by rw [rsw _ (by decide) (by decide), show imgw (18446744071562104832 + (24#u64).val) = rT by decide, + optRef_rT]; rfl) + (by rw [rsw _ (by decide) (by decide)]; decide) + (by rw [rsw _ (by decide) (by decide)]; decide) + (by rw [rsw _ (by decide) (by decide)]; decide) + (by rw [rsw _ (by decide) (by decide)]; decide) + (by rw [rmax_rS]; rfl) + (by rw [refills_rS]; rfl) + +theorem ww_scRelW_rS : ScRelW wst idleScPtr ww rS rootScV 0 where + pl := placed_rS + period := ww_scRel1_rS.period + consumed := ww_scRel1_rS.consumed + tcb := ww_scRel1_rS.tcb + ntfn := ww_scRel1_rS.ntfn + badge := ww_scRel1_rS.badge + yieldFrom := ww_scRel1_rS.yieldFrom + sporadic := ww_scRel1_rS.sporadic + spor _ := by rw [rmax_rS]; decide + idle _ := by rw [rmax_rS]; decide + max := ww_scRel1_rS.max + act _ := ww_scRel1_rS + +/-! ## The idle SC at `ww` -/ + +theorem scw1 (K : U64) (hK : K.val < 128) (h8 : K.val % 8 = 0) : + wL.mem ww (scW idS K) = imgw (18446744071562096640 + K.val) := scW1_v K hK h8 + +theorem mem1_idS {K : U64} (hK : K.val < 128) (h8 : K.val % 8 = 0) : + wL.mem ww (core.num.U64.wrapping_add idS K) = wL.mem w0 (core.num.U64.wrapping_add idS K) := by + rw [scW1_v K hK h8, scW_v K hK h8]; apply imgw_old <;> omega + +theorem rmax_idS1 : wL.rmax ww idS = 2#u64 := by + unfold LawfulStore.rmax maxA layout.sc.REFILL_MAX; rw [scW1_v _ (by decide) (by decide)]; decide + +theorem rhead_idS1 : wL.rhead ww idS = 0#u64 := by + unfold LawfulStore.rhead headA layout.sc.REFILL_HEAD; rw [scW1_v _ (by decide) (by decide)]; decide + +theorem rtail_idS1 : wL.rtail ww idS = 1#u64 := by + unfold LawfulStore.rtail tailA layout.sc.REFILL_TAIL; rw [scW1_v _ (by decide) (by decide)]; decide + +theorem rperiod_idS1 : wL.rperiod ww idS = 0#u64 := by + unfold LawfulStore.rperiod periodA layout.sc.PERIOD; rw [scW1_v _ (by decide) (by decide)]; decide + +theorem refills_idS1 : wL.refills ww idS = [⟨0#u64, mpW⟩, ⟨mpW, 0#u64⟩] := by + unfold LawfulStore.refills LawfulStore.size + rw [rmax_idS1, rhead_idS1, rtail_idS1] + have e : ringN (2#u64).val (0#u64).val (1#u64).val = 2 := by decide + rw [e] + have r0 : wL.rf ww idS 0 = ⟨0#u64, mpW⟩ := by + unfold LawfulStore.rf LawfulStore.refill LawfulStore.ix + rw [rhead_idS1, rmax_idS1] + have a1 : (rtA idS (u64 (((0#u64).val + 0) % (2#u64).val))).val = 18446744071562096640 + 96 := by + unfold rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + have a2 : (raA idS (u64 (((0#u64).val + 0) % (2#u64).val))).val = 18446744071562096640 + 104 := by + unfold raA rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + have e1 : imgw (18446744071562096640 + 96) = 0#u64 := by decide + have e2 : imgw (18446744071562096640 + 104) = mpW := by + rw [imgw_old (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]; exact img_mp1 + rw [memw_al a1 (by omega), memw_al a2 (by omega), e1, e2] + have r1 : wL.rf ww idS 1 = ⟨mpW, 0#u64⟩ := by + unfold LawfulStore.rf LawfulStore.refill LawfulStore.ix + rw [rhead_idS1, rmax_idS1] + have a1 : (rtA idS (u64 (((0#u64).val + 1) % (2#u64).val))).val = 18446744071562096640 + 112 := by + unfold rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + have a2 : (raA idS (u64 (((0#u64).val + 1) % (2#u64).val))).val = 18446744071562096640 + 120 := by + unfold raA rtA layout.sc.REFILLS layout.sc.REFILL_SIZE; decide + have e1 : imgw (18446744071562096640 + 120) = 0#u64 := by decide + have e2 : imgw (18446744071562096640 + 112) = mpW := by + rw [imgw_old (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega)]; exact img_mp2 + rw [memw_al a1 (by omega), memw_al a2 (by omega), e2, e1] + show List.map (wL.rf ww idS) [0, 1] = _ + rw [List.map_cons, List.map_cons, List.map_nil, r0, r1] + +theorem wf_idS1 : RefillsWF wst ww idS (0 + minSchedContextBits) where + placed := placed_idS + min_le := by rw [rmax_idS1]; unfold sporadic.MIN_REFILLS; decide + le_abs := by rw [rmax_idS1]; decide + head_lt := by rw [rhead_idS1, rmax_idS1]; decide + tail_lt := by rw [rtail_idS1, rmax_idS1]; decide + +theorem ww_scRel1_idS : ScRel1 wst ww idS idleScV 0 := + ScRel1.ofWF wf_idS1 + (by rw [rperiod_idS1]; rfl) + (by rw [scw1 _ (by decide) (by decide)]; decide) + (by rw [scw1 _ (by decide) (by decide), show imgw (18446744071562096640 + (24#u64).val) = idT by decide] + show some idleThreadPtr = optRef idT + unfold optRef; rw [if_neg (by decide), idT_bv]) + (by rw [scw1 _ (by decide) (by decide)]; decide) + (by rw [scw1 _ (by decide) (by decide)]; decide) + (by rw [scw1 _ (by decide) (by decide)]; decide) + (by rw [scw1 _ (by decide) (by decide)]; decide) + (by rw [rmax_idS1]; rfl) + (by rw [refills_idS1]; rfl) + +theorem ww_scRelW_idS : ScRelW wst idleScPtr ww idS idleScV 0 where + pl := placed_idS + period := ww_scRel1_idS.period + consumed := ww_scRel1_idS.consumed + tcb := ww_scRel1_idS.tcb + ntfn := ww_scRel1_idS.ntfn + badge := ww_scRel1_idS.badge + yieldFrom := ww_scRel1_idS.yieldFrom + sporadic := ww_scRel1_idS.sporadic + spor _ := by rw [rmax_idS1]; decide + idle _ := by rw [rmax_idS1]; decide + max := ww_scRel1_idS.max + act _ := ww_scRel1_idS + +/-! ## The spec side -/ + +theorem curDom_ww : wL.mem ww (curDomA wst ww) = 0#u64 := by + unfold curDomA + rw [memw_al (n := 18446744071562133512) (by rw [wa_val (by rw [ksb_ww, fkb_val]; decide), ksb_ww, fkb_val]; rfl) + (by norm_num)] + decide + +theorem dt_ww : wL.mem ww (dtA wst ww) = 15#u64 := by + unfold dtA + rw [memw_al (n := 18446744071562133520) (by rw [wa_val (by rw [ksb_ww, fkb_val]; decide), ksb_ww, fkb_val]; rfl) + (by norm_num)] + decide + +theorem σw_idleTcb : σw.kheap idT.bv = some (.tcb idleTcbV) := by rw [idT_bv]; exact kh_idleT +theorem σw_idleSc' : σw.kheap idS.bv = some (.schedContext idleScV 0) := by rw [idS_bv]; exact kh_idleS +theorem σw_rootTcb : σw.kheap rT.bv = some (.tcb rootTcbW) := kh_rT +theorem σw_guestTcb : σw.kheap gT.bv = some (.tcb guestTcbV) := kh_gT +theorem σw_rootSc : σw.kheap rS.bv = some (.schedContext rootScV 0) := kh_rS + +/-- **`SchedSpec` at `ww`.** -/ +theorem ww_schedSpec : SchedSpec wst Tw C1 0#u64 ww (fun _ => []) [] σw where + rq _ _ := rfl + rqDom _ _ _ := rfl + rel := rfl + curThread := by rw [curT_ww]; rfl + curSc := by rw [curSc_ww]; rfl + curTime := by rw [time_ww]; rfl + consumed := by rw [cons_ww]; rfl + curDom := ⟨rfl, curDom_ww⟩ + tcb t ht := by + rcases memTw ht with rfl | rfl | rfl + · exact ⟨idleTcbV, σw_idleTcb, rfl, dom_ww _ ht, by rw [sc_idT1, optRef_idS]; rfl, + by rw [prio_ww _ ht]; rfl⟩ + · exact ⟨rootTcbW, σw_rootTcb, rfl, dom_ww _ ht, by rw [sc_rT1, optRef_rS]; rfl, by rw [prio_ww _ ht]; rfl⟩ + · exact ⟨guestTcbV, σw_guestTcb, rfl, dom_ww _ ht, by rw [sc_gT, optRef_zero]; rfl, by rw [prio_ww _ ht]; rfl⟩ + scMem t ht h := by + rcases memTw ht with rfl | rfl | rfl + · rw [sc_idT1]; exact idS_mem + · rw [sc_rT1]; exact rS_mem + · exact absurd sc_gT h + scs sc hsc := by + rcases memC1 hsc with rfl | rfl + · exact ⟨idleScV, 0, σw_idleSc', ww_scRelW_idS⟩ + · exact ⟨rootScV, 0, σw_rootSc, ww_scRelW_rS⟩ + idleSc := by rw [idleSc_ww, idS_bv]; rfl + domTime := by rw [dt_ww]; rfl + idleTh := by rw [idleT_ww, idT_bv]; rfl + +/-! ## The relation -/ + +theorem tSt_rT : (tStA rT).val = 18446744071562100736 + 3328 := by + unfold tStA layout.tcb.STATE layout.tcb.F; decide +theorem tBlock_rT : (tBlockA rT).val = 18446744071562100736 + 3336 := by unfold tBlockA; decide +theorem badgeReg_rT : (badgeRegA rT).val = 18446744071562100736 + 256 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.RDI; decide +theorem nextEp_rT : (Fl4Proofs.P1.nextA .Ep rT).val = 18446744071562100736 + 3496 := by + unfold Fl4Proofs.P1.nextA nextOff; decide +theorem prevEp_rT : (Fl4Proofs.P1.prevA .Ep rT).val = 18446744071562100736 + 3504 := by + unfold Fl4Proofs.P1.prevA prevOff; decide + +/-- A word outside both TCBs is no notification write. -/ +theorem tSt_gT : (tStA gT).val = 18446744071562113024 + 3328 := by + unfold tStA layout.tcb.STATE layout.tcb.F; decide +theorem tBlock_gT : (tBlockA gT).val = 18446744071562113024 + 3336 := by unfold tBlockA; decide +theorem badgeReg_gT : (badgeRegA gT).val = 18446744071562113024 + 256 := by + unfold badgeRegA layout.tcb.REGS layout.reg.BADGE layout.reg.RDI; decide +theorem nextEp_gT : (Fl4Proofs.P1.nextA .Ep gT).val = 18446744071562113024 + 3496 := by + unfold Fl4Proofs.P1.nextA nextOff; decide +theorem prevEp_gT : (Fl4Proofs.P1.prevA .Ep gT).val = 18446744071562113024 + 3504 := by + unfold Fl4Proofs.P1.prevA prevOff; decide + +theorem nww_far {a : U64} + (h : (a.val < 18446744071562072064 ∨ 18446744071562072064 + 4096 ≤ a.val) ∧ + (a.val < 18446744071562100736 ∨ 18446744071562100736 + 4096 ≤ a.val) ∧ + (a.val < 18446744071562113024 ∨ 18446744071562113024 + 4096 ≤ a.val)) : + ¬ NWrite ∅ Tw a := by + rintro (⟨n, hn, -⟩ | ⟨t, ht, e⟩) + · exact hn + · rcases memTw ht with rfl | rfl | rfl <;> + rcases e with e | e | e | e | e <;> have := congrArg UScalar.val e + · rw [tSt_v] at this; omega + · rw [tBlock_v] at this; omega + · rw [badgeReg_v] at this; omega + · rw [nextEp_v] at this; omega + · rw [prevEp_v] at this; omega + · rw [tSt_rT] at this; omega + · rw [tBlock_rT] at this; omega + · rw [badgeReg_rT] at this; omega + · rw [nextEp_rT] at this; omega + · rw [prevEp_rT] at this; omega + · rw [tSt_gT] at this; omega + · rw [tBlock_gT] at this; omega + · rw [badgeReg_gT] at this; omega + · rw [nextEp_gT] at this; omega + · rw [prevEp_gT] at this; omega + +theorem scObj_val {sc : U64} {a : U64} (h : Sched.ScObj sc 0 a) : sc.val ≤ a.val ∧ a.val < sc.val + 128 := by + have e : (2 : Nat) ^ (0 + Fl4Spec.minSchedContextBits) = 128 := by decide + unfold Sched.ScObj at h; rw [e] at h; exact h + +/-- **`SchedRel` at `ww`** (`N = ∅`, `T = Tw`, `C = C1`, core 0). -/ +theorem ww_schedRel : SchedRel wst ∅ Tw C1 0#u64 ww σw where + placed := ⟨tcbPlaced_T1, fun _ hn => hn.elim, fun _ hn => hn.elim⟩ + nodeNW o ho := nww_far (by rw [nodeAt1_v o ho]; omega) + domNW := nww_far (by + unfold curDomA; rw [wa_val (by rw [ksb_ww, fkb_val]; decide), ksb_ww, fkb_val]; decide) + dtNW := nww_far (by + unfold dtA; rw [wa_val (by rw [ksb_ww, fkb_val]; decide), ksb_ww, fkb_val]; decide) + scOut sc hsc x n hk a ha := by + rcases memC1 hsc with rfl | rfl + · rw [σw_idleSc'] at hk; cases hk + obtain ⟨h1, h2⟩ := scObj_val ha + rw [idS_val] at h1 h2 + refine ⟨nww_far (by omega), ?_, ?_, ?_⟩ + · rw [nodeBase_ww]; omega + · intro t ht; rcases memTw ht with rfl | rfl | rfl <;> simp only [idT_val, rT_val, show gT.val = 18446744071562113024 from rfl] <;> omega + · rw [ksb_ww, fkb_val]; omega + · rw [σw_rootSc] at hk; cases hk + obtain ⟨h1, h2⟩ := scObj_val ha + rw [rS_val] at h1 h2 + refine ⟨nww_far (by omega), ?_, ?_, ?_⟩ + · rw [nodeBase_ww]; omega + · intro t ht; rcases memTw ht with rfl | rfl | rfl <;> simp only [idT_val, rT_val, show gT.val = 18446744071562113024 from rfl] <;> omega + · rw [ksb_ww, fkb_val]; omega + scDisj sc hsc sc' hsc' hne x n x' n' hk hk' a ha ha' := by + rcases memC1 hsc with rfl | rfl <;> rcases memC1 hsc' with rfl | rfl + · exact hne rfl + · rw [σw_idleSc'] at hk; cases hk; rw [σw_rootSc] at hk'; cases hk' + obtain ⟨a1, a2⟩ := scObj_val ha; obtain ⟨b1, b2⟩ := scObj_val ha' + rw [idS_val] at a1 a2; rw [rS_val] at b1 b2; omega + · rw [σw_rootSc] at hk; cases hk; rw [σw_idleSc'] at hk'; cases hk' + obtain ⟨a1, a2⟩ := scObj_val ha; obtain ⟨b1, b2⟩ := scObj_val ha' + rw [rS_val] at a1 a2; rw [idS_val] at b1 b2; omega + · exact hne rfl + actW := Or.inl act_ww + act := by rw [act_ww]; rfl + repr := by rw [repr_ww]; rfl + core sc hsc := by + rcases memC1 hsc with rfl | rfl + · rw [scw1 _ (by decide) (by decide)]; decide + · rw [rsw _ (by decide) (by decide)]; decide + lists := ⟨_, _, ww_schedCore, ww_schedSpec⟩ + +/-! ## The endpoint and notification relations' TCB part -/ + +theorem st_idT1 : wL.mem ww (tStA idT) = 7#u64 := by + unfold tStA layout.tcb.STATE layout.tcb.F; rw [tcbW1_v _ (by decide) (by decide)]; decide + +theorem st_rT1 : wL.mem ww (tStA rT) = 1#u64 := by + unfold tStA layout.tcb.STATE layout.tcb.F; rw [rtW_v _ (by decide) (by decide)]; decide + +theorem st_gT : wL.mem ww (tStA gT) = 8#u64 := by + unfold tStA layout.tcb.STATE layout.tcb.F; rw [gtW_v _ (by decide) (by decide)]; decide + +theorem reg_ww (t : U64) (ht : t ∈ Tw) (i : U64) (hi : i.val < 32) + (hr : t = rT → i.val ≠ 0 ∧ i.val ≠ 5) : wL.mem ww (Endp.regA t i) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · have hv : (Endp.regA idT i).val = 18446744071562072064 + 256 + 8 * i.val := by + unfold Endp.regA layout.tcb.REGS + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, idT_val] + omega + rw [memw_al hv (by omega)]; imgwz + · have hv : (Endp.regA rT i).val = 18446744071562100736 + 256 + 8 * i.val := by + unfold Endp.regA layout.tcb.REGS + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, rT_val] + omega + have h05 := hr rfl + rw [memw_al hv (by omega)]; imgwz + · have hv : (Endp.regA gT i).val = 18446744071562113024 + 256 + 8 * i.val := by + unfold Endp.regA layout.tcb.REGS + simp [core.num.U64.wrapping_add_val_eq, core.num.U64.wrapping_mul_val_eq, U64.size_def, U64.numBits, + show gT.val = 18446744071562113024 from rfl] + omega + rw [memw_al hv (by omega)]; imgwz + +theorem reg_rT0 : wL.mem ww (Endp.regA rT 0#u64) = cptrW := by + have hv : (Endp.regA rT 0#u64).val = 18446744071562100992 := by + unfold Endp.regA layout.tcb.REGS; decide + exact (memw_al hv (by decide)).trans (by decide) + +theorem reg_rT5 : wL.mem ww (Endp.regA rT 5#u64) = rptrW := by + have hv : (Endp.regA rT 5#u64).val = 18446744071562101032 := by + unfold Endp.regA layout.tcb.REGS; decide + exact (memw_al hv (by decide)).trans (by decide) + +theorem badge_ww (t : U64) (ht : t ∈ Tw) (ne : t ≠ rT) : wL.mem ww (badgeRegA t) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · rw [memw_al badgeReg_v (by omega)]; decide + · exact absurd rfl ne + · rw [memw_al badgeReg_gT (by omega)]; decide + +theorem badge_rT : wL.mem ww (badgeRegA rT) = cptrW := by + rw [Endp.badgeRegA_eq_regA]; exact reg_rT0 + +/-- A zero word of every TCB (not `STATE`, `SC`, `VCPU` or the CSpace-root slot's first word). -/ +theorem tZ1 (t : U64) (ht : t ∈ Tw) (K : U64) (hK : K.val < 4096) (h8 : K.val % 8 = 0) (hs : K.val ≠ 3328) + (hc : K.val ≠ 3464) (h0 : K.val ≠ 0) (hv : K.val ≠ 3664) + (hr0 : K.val ≠ 256 := by decide) (hr5 : K.val ≠ 296 := by decide) : + wL.mem ww (core.num.U64.wrapping_add t K) = 0#u64 := by + rcases memTw ht with rfl | rfl | rfl + · exact tcbZ1 K hK h8 hs hc + · exact rtZ K hK h8 hs hc h0 hr0 hr5 + · exact gtZ K hK h8 hs hv + +/-- **The TCBs' endpoint relation** (`TcbRelE`). -/ +theorem ww_tcbRelE (t : U64) (ht : t ∈ Tw) (x : Tcb) + (hp : (t = rT ∧ x = rootTcbW) ∨ (t ≠ rT ∧ (x = idleTcbV ∨ x = guestTcbV))) + (hst : (wL.mem ww (tStA t)).val = tsCode x.tcbState) : Endp.TcbRelE ∅ (wL.mem ww) t x := by + have hx : x = idleTcbV ∨ x = rootTcbW ∨ x = guestTcbV := by + rcases hp with ⟨-, h⟩ | ⟨-, h | h⟩ <;> simp [h] + have hregs : ∀ r, (wL.mem ww (Endp.regA t (Endp.regIdx r))).bv = x.tcbArch.tcbContext.userRegs r := by + intro r + rcases hp with ⟨e1, e2⟩ | ⟨ne, h⟩ + · subst e1 e2 + by_cases h0 : r = .rdi + · subst h0 + show (wL.mem ww (Endp.regA rT 0#u64)).bv = _ + rw [reg_rT0]; rfl + by_cases h5 : r = .r12 + · subst h5 + show (wL.mem ww (Endp.regA rT 5#u64)).bv = _ + rw [reg_rT5]; rfl + rw [reg_ww rT ht _ (by cases r <;> decide) + (fun _ => by cases r <;> first | exact absurd rfl h0 | exact absurd rfl h5 | decide)] + cases r <;> first | exact absurd rfl h0 | exact absurd rfl h5 | rfl + · rw [reg_ww t ht _ (by cases r <;> decide) (fun e => absurd e ne)] + rcases h with rfl | rfl <;> rfl + refine + { st := hst + blk _ h := by rcases hx with rfl | rfl | rfl <;> cases h + send _ _ h := by rcases hx with rfl | rfl | rfl <;> cases h + recv _ _ _ h := by rcases hx with rfl | rfl | rfl <;> cases h + reply _ h := by rcases hx with rfl | rfl | rfl <;> cases h + regs := hregs + prio := by rw [prio_ww t ht]; rcases hx with rfl | rfl | rfl <;> rfl + fault0 := by + unfold Endp.tFaultA; rw [tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)] + rcases hx with rfl | rfl | rfl <;> simp [idleTcbV, rootTcbW, rootTcbV, guestTcbV] + faultT := by + unfold Endp.tFaultA; rw [tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)] + rcases hx with rfl | rfl | rfl <;> simp [idleTcbV, rootTcbW, rootTcbV, guestTcbV] + bound := by + unfold Endp.tBoundA; rw [tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide), optRef_zero] + rcases hx with rfl | rfl | rfl <;> rfl + faultR := by + unfold Endp.FaultRel Endp.FaultRel' Endp.tFaultA + rw [tZ1 t ht _ (by decide) (by decide) (by decide) (by decide) (by decide) (by decide)] + rcases hx with rfl | rfl | rfl <;> exact rfl } + +/-- **`EpStateRel` at `ww`.** -/ +theorem ww_epStateRel : Endp.EpStateRel Tw ∅ ∅ ∅ (wL.mem ww) σw where + ep _ h := h.elim + reply _ h := h.elim + tcb t ht := by + rcases memTw ht with rfl | rfl | rfl + · exact ⟨idleTcbV, σw_idleTcb, ww_tcbRelE _ ht _ (Or.inr ⟨by decide, Or.inl rfl⟩) (by rw [st_idT1]; rfl)⟩ + · exact ⟨rootTcbW, σw_rootTcb, ww_tcbRelE _ ht _ (Or.inl ⟨rfl, rfl⟩) (by rw [st_rT1]; rfl)⟩ + · exact ⟨guestTcbV, σw_guestTcb, ww_tcbRelE _ ht _ (Or.inr ⟨by decide, Or.inr rfl⟩) (by rw [st_gT]; rfl)⟩ + buf _ h := h.elim + +/-- **`NtfnStateRel` at `ww`.** -/ +theorem ww_ntfnStateRel : NtfnStateRel ∅ Tw (wL.mem ww) σw where + ntfn _ h := h.elim + tcb t ht := by + rcases memTw ht with rfl | rfl | rfl + · exact ⟨idleTcbV, σw_idleTcb, ⟨(by rw [st_idT1]; rfl), (fun _ h => by cases h), + (by rw [badge_ww _ ht (by decide)]; rfl), (by rw [prio_ww _ ht]; rfl)⟩⟩ + · exact ⟨rootTcbW, σw_rootTcb, ⟨(by rw [st_rT1]; rfl), (fun _ h => by cases h), + (by rw [badge_rT]; rfl), (by rw [prio_ww _ ht]; rfl)⟩⟩ + · exact ⟨guestTcbV, σw_guestTcb, ⟨(by rw [st_gT]; rfl), (fun _ h => by cases h), + (by rw [badge_ww _ ht (by decide)]; rfl), (by rw [prio_ww _ ht]; rfl)⟩⟩ + +theorem ww_tdx : TdxRel warI σw := ⟨fun _ => rfl, fun _ _ _ => rfl⟩ + +theorem ww_c0 : ∀ q ∈ C1, q ≠ 0#u64 := by + intro q hq; rcases memC1 hq with rfl | rfl <;> decide + +/-- **`OthersRel` at `ww`.** -/ +theorem ww_othersRel (a : MachineState) : + Vs.OthersRel wst ∅ Tw C1 ∅ ∅ ∅ 0#u64 wAR ww a σw := + ⟨ww_schedRel, ww_epStateRel, ww_ntfnStateRel, ww_irqRel, ww_ioRel, ww_kRootRel, ww_ar a⟩ + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitOuter.lean b/proofs/Fl4Proofs/P3/VmWitOuter.lean new file mode 100644 index 000000000..844c25dba --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitOuter.lean @@ -0,0 +1,115 @@ +/- +P3 (line mmuAD witness): **`VEAnyX.outerS3` applies at the VM witness** `ww` / `σw` (P3/VmWit*), for the system call +entries that invoke the SchedControl cap (`SysCall`) and the Untyped cap (`SysNBSendWait`): every platform argument of +`outerS3` is instantiated at the witness platform (`wst`, `warV`, `wAR`, `ipiMR`) except the named premises below, +which are properties of opaque `Fl4Spec` constants, not of the arch. + +* `warV_*`: the arch-side arguments at `warV` (`warI1` with the platform's `virtualization` code and VMX capability + words): `ARIface`, `ZvVirtAgree` (under [P] `virtKind`), `ZvSyncKeep`, `CapAgree`, `PortInValMR` (`OpaqueWK.pin`), + `MRKeepD` at `wAR` and at `ipiMR`, `MachPlat` / `MRKeep` at `ipiMR`, `PPPlatA`, `IrqPlatA`, `TimeAgree` + (`OpaqueWK`), `ArchRefines`; the store's `StoreExt` (`wst_ext`). Each is the one `JBootIoa.u34_w0I1` proves at + `warI1` (the two arches agree on every operation these read). +* **`outer_schedControl`**, **`outer_untyped`**: `outerS3`'s conclusion at the witness for the two entries. + +Named premises: [P] `OpaqueWK` (one domain, `maxTicksToUs`, the kernel WCET and timer unit, the period bound, +`setDeadlineImpl` determinism, a port-read value), [P] `virtKind = .vmx ∨ virtKind = .svm`, [P] +`retypeFanOutLimit = 256`, [P] `resetChunkBits = 16`, [P] `FpuImplDet`, [P] `FpuWriteDet` (all opaque `Fl4Spec` +constants; none mentions the arch). +No sorry. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitS3 +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp + Fl4Proofs.P2.Wit Classical + +noncomputable section + +theorem warV_arIface : ARIface warV wAR := + ⟨⟨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, fun _ _ _ _ => (WP.spec_ok _).2 trivial⟩, + fun _ _ _ _ _ => trivial⟩ + +theorem warV_virtAgree (hk : virtKind = .vmx ∨ virtKind = .svm) : ZvVirtAgree warV := by + intro _ + refine ⟨⟨BitVec.ofNat 64 virtKind.code⟩, rfl, ?_⟩ + rcases hk with h | h <;> rw [h] <;> decide + +theorem warV_syncKeep : ZvSyncKeep warV wAR := fun _ _ _ _ _ => (WP.spec_ok _).2 trivial + +theorem warV_capAgree (a : MachineState) : VirtSanEq.CapAgree warV a := fun _ => rfl + +theorem warV_portIn (O : OpaqueWK) : PortInValMR warV ipiMR := by + intro a a' p z v hv σ _ + have e : (ok (pinF p z, a) : Result (U64 × Fl4Spec.MachineState)) = ok (v, a') := hv + have ev : pinF p z = v := (Prod.mk.inj (ok_inj e)).1 + rw [← ev] + exact pinF_ok O.pin p z σ + +theorem warV_mrKeepD {C : Type} (AR : MachineState → C → Prop) : MRKeepD warV AR := + ⟨fun a _ _ h => (WP.spec_ok _).2 h, fun a _ _ h => (WP.spec_ok _).2 h, + fun a _ _ h => (WP.spec_ok _).2 h, fun a _ _ h => (WP.spec_ok _).2 h, fun a _ _ h => (WP.spec_ok _).2 h, + fun a _ _ h => (WP.spec_ok _).2 h, fun a _ _ h => (WP.spec_ok _).2 h, fun a _ h => (WP.spec_ok _).2 h, + fun a _ _ h => (WP.spec_ok _).2 h, fun a _ _ _ h => (WP.spec_ok _).2 h, + fun a _ _ _ h => (WP.spec_ok _).2 h, fun a _ _ h => (WP.spec_ok _).2 h, fun a _ _ h => (WP.spec_ok _).2 h, + fun a _ _ _ h => (WP.spec_ok _).2 h, fun a _ _ _ _ h => (WP.spec_ok _).2 h, + fun a _ _ h => (WP.spec_ok _).2 h, + fun a _ _ _ _ _ _ _ _ _ _ h => (WP.spec_ok _).2 h, fun a _ _ _ _ _ _ _ _ h => (WP.spec_ok _).2 h, + fun a _ _ _ h => (WP.spec_ok _).2 h⟩ + +theorem warV_machPlat : MachPlat warV ipiMR := + MachPlatF.toT ⟨modelArch_machPlatF.now, modelArch_machPlatF.irqPending, modelArch_machPlatF.take, + modelArch_machPlatF.mask1, modelArch_machPlatF.mask0, modelArch_machPlatF.ack, modelArch_machPlatF.frame⟩ + modelArch_ipiF + +theorem warV_ppPlatA : PPPlatA warV wAR := ⟨fun _ _ _ => (WP.spec_ok _).2 trivial, warV_mb⟩ + +theorem warV_irqPlatA : IrqPlatA warV wAR := + ⟨fun _ _ _ => (WP.spec_ok _).2 trivial, fun _ _ _ _ => (WP.spec_ok _).2 trivial⟩ + +theorem warV_timeAgree (O : OpaqueWK) : TimeAgree warV where + maxPeriod a := warI1_max_periodK O a + usToTicks _ _ _ := (WP.spec_ok _).2 rfl + kernelWcet _ := (WP.spec_ok _).2 rfl + wcetUs := O.wcet + timerUnit := O.unit + +theorem warV_archRefines : Vs.ArchRefines warV wAR := + ⟨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⟩ + +/-- **`outerS3` at the witness, the SchedControl invocation** (module docstring's premises). -/ +theorem outer_schedControl (O : OpaqueWK) (hk : virtKind = .vmx ∨ virtKind = .svm) + (hfan : retypeFanOutLimit = 256#64) (hrcb : resetChunkBits = 16) (hFpu : Sched.FpuImplDet) + (hFW : FpuWriteDet) : + (S3Ev.sys (A := MachineState) fl4_abi.syscall.SysCall .sysCall).run wst warV ww σw.machineState ⦃ r => + ∃ σ', S3Ev.Out wst (S3Ev.sys (A := MachineState) fl4_abi.syscall.SysCall .sysCall) ww σw σ' ∧ + VEAnyX (L := wL) warV wAR r.1 r.2 σ' ⦄ := + VEAnyX.outerS3 (MR := ipiMR) warV_arIface warV_total (warV_virtAgree hk) warV_syncKeep warV_capAgree + (warV_portIn O) (warV_mrKeepD _) (warV_mrKeepD _) O.nd warV_machPlat (warV_mrKeepD _).toK warV_ppPlatA + warV_irqPlatA (fun a => (warV_timeAgree O).mrt a) warV_mb O.max (fun _ _ => (WP.spec_ok _).2 rfl) warV_virt + hfan hrcb (warV_timeAgree O) warV_archRefines O.dl hFpu hFW wst_ext (veAnyX_ww hk _) _ + (witness_schedControl hk O.nd).2 + +/-- **`outerS3` at the witness, the Untyped invocation** (module docstring's premises). -/ +theorem outer_untyped (O : OpaqueWK) (hk : virtKind = .vmx ∨ virtKind = .svm) + (hfan : retypeFanOutLimit = 256#64) (hrcb : resetChunkBits = 16) (hFpu : Sched.FpuImplDet) + (hFW : FpuWriteDet) : + (S3Ev.sys (A := MachineState) fl4_abi.syscall.SysNBSendWait .sysNBSendWait).run wst warV ww σw.machineState ⦃ r => + ∃ σ', S3Ev.Out wst (S3Ev.sys (A := MachineState) fl4_abi.syscall.SysNBSendWait .sysNBSendWait) ww σw σ' ∧ + VEAnyX (L := wL) warV wAR r.1 r.2 σ' ⦄ := + VEAnyX.outerS3 (MR := ipiMR) warV_arIface warV_total (warV_virtAgree hk) warV_syncKeep warV_capAgree + (warV_portIn O) (warV_mrKeepD _) (warV_mrKeepD _) O.nd warV_machPlat (warV_mrKeepD _).toK warV_ppPlatA + warV_irqPlatA (fun a => (warV_timeAgree O).mrt a) warV_mb O.max (fun _ _ => (WP.spec_ok _).2 rfl) warV_virt + hfan hrcb (warV_timeAgree O) warV_archRefines O.dl hFpu hFW wst_ext (veAnyX_ww hk _) _ + (witness_untyped hk O.nd).2 + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitPlatform.lean b/proofs/Fl4Proofs/P3/VmWitPlatform.lean new file mode 100644 index 000000000..17994e3b5 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitPlatform.lean @@ -0,0 +1,34 @@ +/- +Non-vacuity witness line (x86 VM Stage 3), finding 1: **a VM state with a running guest forces a virtualization +backend**. `virtKind` is an opaque platform constant (`Fl4Spec/Ext/VirtBackend`), and `VcpuRel` is `False` at +`.none`; so `VmState` (hence `VmEptState`, `VEAnyX`) with a TCB in `TS_RUNNING_VM` is only satisfiable where +`virtKind` is `.vmx` or `.svm`. A witness of such a state therefore needs that as a premise [P]. + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmState +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} {V : Set U64} + +/-- A `VmState` with a TCB in `TS_RUNNING_VM` has the VMX or the SVM backend (module docstring). -/ +theorem VmState.virtKind_of_runningVM (J : VmState (L := L) arI V AR Z H Cs Ac Lst D s a σ) {t : U64} + (ht : Z.th t = some .tcb) (hr : L.mem s (P1.stA t) = 8#u64) : + Fl4Spec.virtKind = .vmx ∨ Fl4Spec.virtKind = .svm := by + rcases J.2.extv.bnd t ht with h | ⟨hv, -⟩ + · exact absurd h (J.2.rvm t ht hr) + · obtain ⟨x, hx⟩ := J.2.vm.spec _ hv + have R := J.2.vr _ hv x hx + unfold Fl4Spec.VcpuRel at R + revert R + cases Fl4Spec.virtKind <;> simp + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Proofs/P3/VmWitS3.lean b/proofs/Fl4Proofs/P3/VmWitS3.lean new file mode 100644 index 000000000..784e25a75 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitS3.lean @@ -0,0 +1,460 @@ +/- +P3 (line mmuAD witness): **`VEAnyX ∧ S3Pre` holds at the VM witness** `ww` / `σw` (P3/VmWit*), for the syscall +event invoking the SchedControl cap (`SysCall`, capability register: slot 3) and for the one invoking the Untyped cap +(`SysNBSendWait`, reply register: slot 2). The state has a `runningVM` TCB with its VCPU. `StartS3.core` is not vacuous +in either case (`w_look_sc`, `w_look_ut`: at every dispatch pair the invoked slot is looked up and decodes to the +cap), and dispatch pairs exist: `w_budget_ok` / `w_budgetAt` (the FL4 `budget_ok` and the spec budget check pass: +the witness clock is at `getCurrentTime`'s cap, P3/VmWitSpec, so no time is consumed and the root SC's head +`MAX_PERIOD ≥ MIN_BUDGET` suffices), `w_dispatch`, and `w_core_exercised` (both invocations: at a concrete dispatch +pair `LookAt`, `capAt` and `SelectedCoreScope` hold, the SchedControl cap's core being the executing core 0). The +machine relation is `ipiMR` (`modelMR` with the IPI lines masked), the one `outerS3`'s `MachPlat` holds at +(P3/VmWitOuter). + +Named premises: [P] `virtKind = .vmx ∨ virtKind = .svm` (P3/VmWitPlatform); [P] `numDomains = 1` (opaque +configuration; as `VEAnyX.outerS3`'s and `OpaqueWK`'s). + +No sorry. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitVcpu +import Fl4Proofs.P3.VmEntryS3Outer +import Fl4Proofs.P3.XTopJointKEntry33 +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp + Fl4Proofs.P2.Wit Classical + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem cur_ww : sched.cur_thread wst ww = ok rT := by + obtain ⟨v, hv, hvv⟩ := spec_exists (cur_thread_spec' (L := wL) ww) + rw [hv, hvv] + exact congrArg ok curT_ww + +theorem w_zvCt : ∀ ct, sched.cur_thread wst ww = ok ct → ZvCtRun σw ct := by + intro ct h x hx + rw [cur_ww] at h + cases ok_inj h + rw [σw_kheap, kh_rT] at hx + cases hx; rfl + +theorem w_nativeSys : NativeSys fl4_abi.syscall.SysCall .sysCall := + Or.inl ⟨true, true, true, Or.inr (Or.inr ⟨rfl, rfl, rfl, rfl⟩), rfl⟩ + +theorem w_flags : ∀ ct v, sched.cur_thread wst ww = ok ct → + obj.Tcb.flags wst ct (wL.wr ww (Endp.regA ct layout.reg.FAULT_IP) v) + ⦃ f => (f &&& fl4_abi.consts.tcb_flag.TRAP_SYSCALLS) = 0#u64 ⦄ := by + intro ct v h + rw [cur_ww] at h + cases ok_inj h + have eF : layout.tcb.FLAGS = ok 3456#u64 := by + unfold layout.tcb.FLAGS layout.tcb.F; rfl + have ea : (core.num.U64.wrapping_add rT 3456#u64).val = 18446744071562104192 := by + rw [wa_val (by rw [rT_val]; decide), rT_val]; rfl + have er : (Endp.regA rT layout.reg.FAULT_IP).val % 8 = 0 ∧ (Endp.regA rT layout.reg.FAULT_IP).val ≠ 18446744071562104192 := by + unfold Endp.regA layout.tcb.REGS layout.reg.FAULT_IP + decide + have em : wL.mem ww (core.num.U64.wrapping_add rT 3456#u64) = 0#u64 := + (memw_al ea (by decide)).trans (by imgwz) + unfold obj.Tcb.flags + simp only [eF, lift, bind_ok] + rw [wL.read_eq, WP.spec_ok, wL.mem_wr _ _ _ _ er.1 (by show _ % 8 = 0; rw [ea]), + if_neg (fun e => er.2 (by rw [e, ea])), em] + exact UScalar.eq_of_val_eq (by simp) + +/-- **`StartS3.air` at the witness**: the architecture's IOAPICs are the spec state's. -/ +theorem w_air (s2 : WSt) (a2 : MachineState) (σb : State Unit) (hnd : numDomains = 1) (hBA : BudgetAt σw σb true) : + ArchIrqRel warV a2 σb := by + have ha : σb.archState = σw.archState := by + have hk : σw.kheap σw.curThread = some (.tcb rootTcbW) := kh_rT + obtain ⟨⟨σu, hu, ebu⟩, -, -⟩ := BudgetAt.shape27 hnd hk hBA + have hE := Sched.updateTimeStamp_eq' hnd + (doMachineOp_single (getCurrentTime_eq (faultIPSt σw σw.curThread rootTcbW).machineState)) + rw [hE] at hu + have e : σu = _ := congrArg Prod.snd (Set.mem_singleton_iff.1 hu) + rw [ebu, e]; rfl + have hn : σb.archState.x64NumIoapics = 1 := by rw [ha]; rfl + have hi : σb.archState.x64IoapicNirqs = fun _ => ucast 8 ioapicIRQLines := by rw [ha]; rfl + refine ⟨⟨1#u64, rfl, by rw [hn]; rfl⟩, fun n h i hlt => ?_, + fun a n i p l po v np _ _ _ _ _ _ => ⟨a, rfl⟩, fun a n i p l po v h np _ _ _ _ _ _ _ => ⟨a, rfl⟩, + fun a irq b => ?_⟩ + · cases ok_inj h + have h1 : i.val < 1 := hlt + refine ⟨24#u64, ?_, ?_, by decide⟩ + · show ok (if i.val < 1 then 24#u64 else 0#u64) = ok 24#u64 + rw [if_pos h1] + · rw [hi]; show (24#u64 : U64).val = (ucast 8 ioapicIRQLines).toNat; decide + · exact spec_exists (warI1_total.mask_irq a irq b) |>.imp fun _ h => h.1 + +/-! ## The dispatch pair's memory -/ + +theorem warV_total : P0.ArchTotal warV := + { warI1_total with + virtualization := fun _ => (WP.spec_ok _).2 trivial + vcpu_capability := fun _ _ => (WP.spec_ok _).2 trivial } + + +theorem w_arKeep : ARKeep (C := Fl4Spec.Cr3) warV wAR where + now a _ _ := WP.spec_mono (warV_total.now a) (fun _ _ => trivial) + set_deadline a d _ _ := WP.spec_mono (warV_total.set_deadline a d) (fun _ _ => trivial) + send_reschedule a m _ _ := WP.spec_mono (warV_total.send_reschedule a m) (fun _ _ => trivial) + remote_stall a cpu _ _ := WP.spec_mono (warV_total.remote_stall a cpu) (fun _ _ => trivial) + fpu_flush a t _ _ := WP.spec_mono (warV_total.fpu_flush a t) (fun _ _ => trivial) + fpu_release a t _ _ := WP.spec_mono (warV_total.fpu_release a t) (fun _ _ => trivial) + fpu_sanitise a t _ _ := WP.spec_mono (warV_total.fpu_sanitise a t) (fun _ _ => trivial) + fpu_reload a t _ _ := WP.spec_mono (warV_total.fpu_reload a t) (fun _ _ => trivial) + vcpu_barrier a _ _ := WP.spec_mono (warV_total.vcpu_barrier a) (fun _ _ => trivial) + vcpu_release a v _ _ := WP.spec_mono (warV_total.vcpu_release a v) (fun _ _ => trivial) + +theorem fip_val : (Endp.regA rT layout.reg.FAULT_IP).val = 18446744071562101128 := by + unfold Endp.regA layout.tcb.REGS layout.reg.FAULT_IP; decide + +/-- At every dispatch pair, a word below the node block other than the root TCB's `FAULT_IP` register is the entry's, +and so is the current thread. -/ +theorem w_front {s2 : WSt} {a2 : MachineState} + (h : KFrontB (L := wL) warV ww σw.machineState s2 a2) : + wL.cpu s2 = 0#u64 ∧ sched.cur_thread wst s2 = ok rT ∧ + ∀ b : U64, b.val % 8 = 0 → b.val < 18446744071562147072 → b.val ≠ 18446744071562101128 → + wL.mem s2 b = wL.mem ww b := by + obtain ⟨ct, v, hct, hb⟩ := h + rw [cur_ww] at hct + cases ok_inj hct + have hN : P1.NodePlaced wst Tw (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) + (wL.cpu (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v)) := by + rw [wL.cpu_wr]; exact nodePlaced_ww.wr + have hf := budget_ok_frT (arI := warV) (AR := wAR) w_arKeep warV_total + (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) σw.machineState (c := Fl4Spec.Cr3.cr3 0 0) trivial hN + rw [hb, WP.spec_ok] at hf + obtain ⟨hk, hc, hm⟩ := hf rfl + have hc0 : wL.cpu (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) = 0#u64 := by rw [wL.cpu_wr]; rfl + have hcpu : wL.cpu s2 = 0#u64 := hc.trans hc0 + have hbase : wL.nodeBase (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) 0#u64 = 18446744071562147072 := by + rw [show wL.nodeBase (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) 0#u64 = wL.nodeBase ww 0#u64 by + simp [LawfulStore.nodeBase]] + exact nodeBase_ww + have hN0 : P1.NodePlaced wst Tw (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) 0#u64 := nodePlaced_ww.wr + have nv : ∀ o : U64, o.val < 4288 → + (wL.nodeAt (wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) v) 0#u64 o).val = 18446744071562147072 + o.val := + fun o ho => by rw [P1.nodeAt_val hN0 ho, hbase] + have eT : layout.node.CUR_TIME.val < 4288 ∧ layout.node.CUR_TIME.val ≠ 0 := by + unfold layout.node.CUR_TIME; decide + have eC : layout.node.CONSUMED.val < 4288 ∧ layout.node.CONSUMED.val ≠ 0 := by + unfold layout.node.CONSUMED; decide + have key : ∀ b : U64, b.val % 8 = 0 → b.val ≠ 18446744071562101128 → + b.val ≠ 18446744071562147072 + layout.node.CUR_TIME.val → + b.val ≠ 18446744071562147072 + layout.node.CONSUMED.val → wL.mem s2 b = wL.mem ww b := by + intro b h8 h1 h2 h3 + rw [hm b h8, wL.mem_wr _ _ _ _ (by show _ % 8 = 0; rw [fip_val]) h8, + if_neg (fun e => h1 (by rw [← e, fip_val]))] + rw [hc0] + rintro (e | e) + · exact h2 (by rw [e, nv _ eT.1]) + · exact h3 (by rw [e, nv _ eC.1]) + refine ⟨hcpu, ?_, fun b h8 hlt h1 => key b h8 h1 (by omega) (by omega)⟩ + obtain ⟨x, hx, hxv⟩ := spec_exists (cur_thread_spec' (L := wL) s2) + rw [hx, hxv, hcpu] + have e0 : layout.node.CUR_THREAD.val = 0 := by unfold layout.node.CUR_THREAD; rfl + have en : wL.nodeAt s2 0#u64 layout.node.CUR_THREAD = wL.nodeAt ww 0#u64 layout.node.CUR_THREAD := by + have ek : wL.ksb s2 = wL.ksb ww := hk.trans (wL.ksb_wr _ _ _) + simp only [LawfulStore.nodeAt, ek] + have ev : (wL.nodeAt ww 0#u64 layout.node.CUR_THREAD).val = 18446744071562147072 := by + rw [nodeAt1_v _ (by rw [e0]; omega), e0] + rw [en, key _ (by rw [ev]) (by rw [ev]; omega) (by rw [ev]; omega) (by rw [ev]; omega)] + exact congrArg ok curT_ww + +theorem w_slotsSame {s2 : WSt} {a2 : MachineState} + (h : KFrontB (L := wL) warV ww σw.machineState s2 a2) : SlotsSame thWw (wL.mem ww) (wL.mem s2) := by + obtain ⟨-, -, hm⟩ := w_front h + intro y hy + have k : ∀ (o : U64) (n : Nat), o.val = n → n < 32 → n % 8 = 0 → wL.mem s2 (wordOf y o) = wL.mem ww (wordOf y o) := by + intro o n ho hn h8 + obtain ⟨hs, h32⟩ := slot_natw hy n hn + have hb : y.val + n < 2 ^ 64 := by unfold SlotNatw SlotNat at hs; omega + have e : (wordOf y o).val = y.val + n := by + unfold wordOf; rw [wa_val (a := y) (b := o) (by rw [ho]; exact hb), ho] + unfold SlotNatw SlotNat at hs + exact hm _ (by rw [e]; omega) (by rw [e]; omega) (by rw [e]; omega) + have h0 : wL.mem s2 y = wL.mem ww y := by + obtain ⟨hs, h32⟩ := slot_natw hy 0 (by omega) + unfold SlotNatw SlotNat at hs + exact hm _ (by omega) (by omega) (by omega) + exact ⟨h0, k 8#u64 8 rfl (by omega) rfl, k 16#u64 16 rfl (by omega) rfl, k 24#u64 24 rfl (by omega) rfl⟩ + +theorem w_reg {s2 : WSt} {a2 : MachineState} (h : KFrontB (L := wL) warV ww σw.machineState s2 a2) : + wL.mem s2 (Endp.regA rT 0#u64) = cptrW ∧ wL.mem s2 (Endp.regA rT 5#u64) = rptrW := by + obtain ⟨-, -, hm⟩ := w_front h + have e0 : (Endp.regA rT 0#u64).val = 18446744071562100992 := by unfold Endp.regA layout.tcb.REGS; decide + have e5 : (Endp.regA rT 5#u64).val = 18446744071562101032 := by unfold Endp.regA layout.tcb.REGS; decide + exact ⟨(hm _ (by rw [e0]) (by rw [e0]; omega) (by rw [e0]; omega)).trans reg_rT0, + (hm _ (by rw [e5]) (by rw [e5]; omega) (by rw [e5]; omega)).trans reg_rT5⟩ + +/-! ## The lookups -/ + +/-- The lookup of a cptr whose top three bits are `idx`, at a spec state with the root TCB's CSpace. -/ +theorem w_lookup {σb : State Unit} {x : Tcb} (hk : σb.kheap rT.bv = some (.tcb x)) (hx : x.tcbCtable = cnW) + (cref : List Bool) (hl : cref.length = 64) (idx : List Bool) (hi : cref.take 3 = idx) (cap : Cap) + (hc : capOpt (σb.kheap rC.bv) idx = some cap) (hn : isCnodeCap cap = false) : + (lookupCapAndSlot (Z := Unit) rT.bv cref).run σb = ({(.ok (cap, (rC.bv, idx)), σb)}, False) := by + have hg : getCap (Z := Unit) (rC.bv, idx) σb = ({(cap, σb)}, False) := (getCap_pair_iff σb _ _ _).2 hc + have hne : cref.drop 3 ≠ [] := fun h => by + have := congrArg List.length h + simp [hl] at this + have hr : (lookupSlotForThread (Z := Unit) rT.bv cref).run σb = + ({(.ok ((rC.bv, idx), cref.drop 3), σb)}, False) := by + have e3 : (cref.drop ([] : List Bool).length).take 3 = idx := hi + have h := rab_next (Z := Unit) rC.bv 3 [] cref σb cap (by decide) List.nil_prefix + (by rw [hl]; decide) hne (by rw [e3]; exact hg) + rw [lookupSlotForThread_eq hk, hx] + refine h.trans ?_ + rw [if_neg (by rw [hn]; decide), e3] + rfl + unfold lookupCapAndSlot + rw [run_bind_ok hr] + exact (run_bind_ok (run_liftE hg) _).trans rfl + +/-- The CSpace at a dispatch state. -/ +theorem w_disp (hnd : numDomains = 1) {σb : State Unit} (hBA : BudgetAt σw σb true) : + σb.curThread = rT.bv ∧ (∃ x, σb.kheap rT.bv = some (.tcb x) ∧ x.tcbCtable = cnW) ∧ + σb.kheap rC.bv = some (.cnode 3 wCs) := by + have hk : σw.kheap σw.curThread = some (.tcb rootTcbW) := kh_rT + obtain ⟨-, ek, ect, -⟩ := BudgetAt.shape27 hnd hk hBA + have ec : σw.curThread = rT.bv := rfl + have ek' : σb.kheap = Function.update σw.kheap rT.bv + (some (.tcb (Notif.setReg rootTcbW .faultIP (rootTcbW.tcbArch.tcbContext.userRegs .nextIP - 2)))) := ek + refine ⟨ect.trans ec, ⟨_, by rw [ek', Function.update_self], rfl⟩, ?_⟩ + rw [ek', Function.update_of_ne ne_TC.symm, σw_kheap, kh_rC] + +theorem w_look_slot (hnd : numDomains = 1) {σb : State Unit} (hBA : BudgetAt σw σb true) (cptr : U64) + (idx : List Bool) (hi : (toBl cptr.bv).take 3 = idx) (c : capability.Cap) + (hc : capOpt (some (.cnode 3 wCs)) idx = some (absCap c)) (hn : isCnodeCap (absCap c) = false) : + LookAt rT cptr σb (rC.bv, idx) c := by + obtain ⟨-, ⟨x, hk, hx⟩, hC⟩ := w_disp hnd hBA + exact w_lookup hk hx _ (by simp [toBl]) _ hi _ (by rw [hC]; exact hc) hn + +/-- **The SchedControl invocation's premise of `SelectedCoreScope` holds** at every dispatch pair. -/ +theorem w_look_sc (hnd : numDomains = 1) {s2 : WSt} {a2 : MachineState} {σb : State Unit} + (h : KFrontB (L := wL) warV ww σw.machineState s2 a2) (hBA : BudgetAt σw σb true) : + LookAt rT (invokedCptr wst s2 rT fl4_abi.syscall.SysCall) σb (rC.bv, [false, true, true]) sctlC ∧ + capAt (wL.mem s2) (cslotAddr (rC.bv, [false, true, true])) = ok sctlC := by + have e : invokedCptr wst s2 rT fl4_abi.syscall.SysCall = cptrW := by + have e0 : layout.reg.CAP = 0#u64 := by unfold layout.reg.CAP layout.reg.RDI; rfl + unfold invokedCptr + rw [if_neg (by unfold fl4_abi.syscall.SysCall fl4_abi.syscall.SysReplyRecv fl4_abi.syscall.SysNBSendWait; decide), + if_neg (by unfold fl4_abi.syscall.SysCall fl4_abi.syscall.SysNBSendRecv; decide), e0] + exact (w_reg h).1 + refine ⟨?_, ?_⟩ + · rw [e] + exact w_look_slot hnd hBA _ _ (by decide) _ (by simp [capOpt, wCs_wf, wCs, absCap]) (by simp [absCap, isCnodeCap]) + · rw [cs_rC3, (w_slotsSame h).capAt isSlot_rC3, capAt_capV, capV_rC3] + +/-- **The Untyped invocation's premise of `SelectedCoreScope` holds** at every dispatch pair. -/ +theorem w_look_ut (hnd : numDomains = 1) {s2 : WSt} {a2 : MachineState} {σb : State Unit} + (h : KFrontB (L := wL) warV ww σw.machineState s2 a2) (hBA : BudgetAt σw σb true) : + LookAt rT (invokedCptr wst s2 rT fl4_abi.syscall.SysNBSendWait) σb (rC.bv, [false, true, false]) utC ∧ + capAt (wL.mem s2) (cslotAddr (rC.bv, [false, true, false])) = ok utC := by + have e : invokedCptr wst s2 rT fl4_abi.syscall.SysNBSendWait = rptrW := by + have e0 : layout.reg.REPLY = 5#u64 := by unfold layout.reg.REPLY layout.reg.R12; rfl + unfold invokedCptr + rw [if_pos (Or.inr rfl), e0] + exact (w_reg h).2 + refine ⟨?_, ?_⟩ + · rw [e] + exact w_look_slot hnd hBA _ _ (by decide) _ (by simp [capOpt, wCs_wf, wCs]; rfl) (by simp [absCap, isCnodeCap]) + · rw [cs_rC2, (w_slotsSame h).capAt isSlot_rC2, capAt_capV, capV_rC2] + +/-! ## A dispatch pair exists -/ + +/-- **The witness clock reads `tW`** whatever `timeOracle` gives (`getCurrentTime` saturates at its cap). -/ +theorem gctVal_w : gctVal σw.machineState = tW := by + have e : gctNat σw.machineState = tW.toNat := by + unfold gctNat + show min tW.toNat (tW.toNat + _) = tW.toNat + exact Nat.min_eq_left (Nat.le_add_right _ _) + unfold gctVal + rw [e] + apply BitVec.eq_of_toNat_eq + rw [BitVec.toNat_ofNat] + exact Nat.mod_eq_of_lt tW.isLt + +/-- The root SC's head (`MAX_PERIOD`) is sufficient for no consumed time (`RVs`: `MIN_BUDGET ≤ MAX_PERIOD`). -/ +theorem suff_root : refillSufficient 0 (refillHd rootScV) = true := by + have h : MIN_BUDGET ≤ (refillHd idleScV).rAmount := idleSc_rvs.2.1 + have h0 : ¬ (refillHd idleScV).rAmount < 0 := fun hl => by simp [BitVec.lt_def] at hl + show decide (MIN_BUDGET ≤ (if (refillHd idleScV).rAmount < 0 then 0 else (refillHd idleScV).rAmount - 0)) = true + have e : (refillHd idleScV).rAmount - 0 = (refillHd idleScV).rAmount := by simp + rw [if_neg h0, e] + exact decide_eq_true h + +theorem w_consumed : σw.consumedTime + (gctVal σw.machineState - σw.curTime) = 0 := by + rw [gctVal_w] + show (0 : Word) + (tW - tW) = 0 + rw [BitVec.sub_self, BitVec.add_zero] + +/-- **The spec side: the budget check passes after the `FaultIP` write** (one domain). -/ +theorem w_budgetAt (hnd : numDomains = 1) : ∃ σb, BudgetAt σw σb true := by + have hk : σw.kheap σw.curThread = some (.tcb rootTcbW) := kh_rT + let xT : Tcb := Notif.setReg rootTcbW .faultIP (rootTcbW.tcbArch.tcbContext.userRegs .nextIP - 2) + set σ0 := faultIPSt σw σw.curThread rootTcbW with hσ0 + have hU := Sched.updateTimeStamp_eq' hnd (doMachineOp_single (getCurrentTime_eq σ0.machineState)) + set σu : State Unit := { { { σ0 with machineState := gctState σ0.machineState } with + curTime := gctVal σ0.machineState } with + consumedTime := σ0.consumedTime + (gctVal σ0.machineState - σ0.curTime) } with hσu + have hS : σu.kheap rS.bv = some (.schedContext rootScV 0) := by + show Function.update σw.kheap σw.curThread (some (.tcb xT)) rS.bv = _ + rw [Function.update_of_ne (show rS.bv ≠ σw.curThread from ne_TS.symm)]; exact kh_rS + have hb : getScRefillSufficient (Z := Unit) σu.curSc σu.consumedTime σu = ({(true, σu)}, False) := by + have e1 : σu.curSc = rS.bv := rfl + have e2 : σu.consumedTime = 0 := w_consumed + rw [e1, e2, getScRefillSufficient_eq hS (by simp [rootScV, idleScV]) 0, suff_root] + have hkt : σu.kheap σu.curThread = some (.tcb xT) := by + show Function.update σw.kheap σw.curThread (some (.tcb xT)) σw.curThread = _ + exact Function.update_self _ _ _ + unfold BudgetAt + refine ⟨σu, σ0, by rw [hσ0, fl4SyscallFaultIP_eq hk]; exact Set.mem_singleton _, ?_⟩ + show (true, σu) ∈ ((updateTimeStamp (Z := Unit) >>= fun _ => checkBudgetRestart) σ0).1 + rw [Fl4Proofs.P3.nd_bind_one hU, checkBudgetRestart_suff hnd hb hkt] + exact Set.mem_singleton _ + +/-- The `FaultIP` write of the register's own value leaves `ww` as it is. -/ +theorem w_wr_self : wL.wr ww (Endp.regA rT layout.reg.FAULT_IP) (wL.mem ww (Endp.regA rT layout.reg.FAULT_IP)) = ww := by + show wwr ww _ (wmem ww _) = ww + unfold wwr wmem + rw [Function.update_eq_self] + +theorem warV_mb (a : MachineState) : sporadic.min_budget warV a ⦃ mb => mb.bv = MIN_BUDGET ⦄ := by + unfold sporadic.min_budget + refine WP.spec_bind (Pₘ := fun v : U64 => v.bv = kernelWCETTicks) ((WP.spec_ok _).2 rfl) ?_ + intro i hi + rw [WP.spec_ok] + unfold MIN_BUDGET + rw [← hi] + simp [BitVec.mul_comm] + +/-- **The FL4 side: `budget_ok` passes at `ww`** (one domain). -/ +theorem w_budget_ok (hnd : numDomains = 1) : + syscall.budget_ok wst warV ww σw.machineState ⦃ r => r.1 = true ⦄ := by + have hnow : warV.now σw.machineState ⦃ r => ∃ ms, doMachineOp (Z := Unit) getCurrentTime σw = + ({(r.1.bv, { σw with machineState := ms })}, False) ∧ σw.curTime ≤ r.1.bv ⦄ := by + refine (WP.spec_ok ((⟨gctVal σw.machineState⟩ : U64), gctState σw.machineState)).2 + ⟨_, doMachineOp_single (getCurrentTime_eq _), ?_⟩ + show tW ≤ gctVal σw.machineState + rw [gctVal_w]; exact BitVec.le_refl _ + unfold syscall.budget_ok + refine WP.spec_bind (update_timestamp_refines ww σw.machineState σw ww_schedRel ww_ntfnStateRel σw_nsc hnd hnow) ?_ + rintro ⟨s1, a1⟩ ⟨σ', hU, hS1, -, -, -, -, -, hnw, -⟩ + rw [Sched.updateTimeStamp_eq' hnd (doMachineOp_single (getCurrentTime_eq σw.machineState))] at hU + have e := (Prod.mk.inj (Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst hU))).2 + subst e + have esc : nodeW wst s1 0#u64 layout.node.CUR_SC = rS := + (hnw _ (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) (by simp [layout.node.CUR_SC]) + (by simp [layout.node.CUR_SC])).trans curSc_ww + unfold sched.check_budget_restart + refine WP.spec_bind (check_budget_suffT warV_total s1 a1 _ hS1 (by rw [esc]; exact rS_mem) ?_ (warV_mb a1) ?_) ?_ + · intro _ x n hx + rw [esc] at hx + have hx' : khW rS.bv = some (.schedContext x n) := hx + rw [kh_rS] at hx'; cases hx' + simp [scActive, activeSc, rootScV, idleScV, MIN_REFILLS] + · intro y n hy + have hy' : khW rS.bv = some (.schedContext y n) := hy + rw [kh_rS] at hy'; cases hy' + show refillSufficient (σw.consumedTime + (gctVal σw.machineState - σw.curTime)) _ = true + rw [w_consumed]; exact suff_root + · rintro ⟨r, s2, a2⟩ hr + simp only at hr + subst hr + step with sched.cur_thread.spec' as ⟨ct, _⟩ + rw [if_pos trivial] + exact (WP.spec_ok _).2 rfl + +/-- **A dispatch pair of the witness entry exists** (one domain): the `FaultIP` write and a passing `budget_ok` on +the FL4 side, a passing budget check on the spec side. -/ +theorem w_dispatch (hnd : numDomains = 1) : ∃ (s2 : WSt) (a2 : MachineState) (σb : State Unit), + KFrontB (L := wL) warV ww σw.machineState s2 a2 ∧ BudgetAt σw σb true := by + obtain ⟨⟨b, s2, a2⟩, hb, hbt⟩ := spec_exists (w_budget_ok hnd) + simp only at hbt + subst hbt + obtain ⟨σb, hσb⟩ := w_budgetAt hnd + exact ⟨s2, a2, σb, ⟨rT, wL.mem ww (Endp.regA rT layout.reg.FAULT_IP), cur_ww, by rw [w_wr_self]; exact hb⟩, hσb⟩ + +/-! ## `S3Pre` -/ + +/-- `StartS3.core` from the invoked slot's lookup, whose decoded cap meets the core restriction. -/ +theorem w_core {n : I64} {s2 : WSt} {a2 : MachineState} {σb : State Unit} + (h : KFrontB (L := wL) warV ww σw.machineState s2 a2) {sl : CslotPtr} {c : capability.Cap} + (hl : LookAt rT (invokedCptr wst s2 rT n) σb sl c ∧ capAt (wL.mem s2) (cslotAddr sl) = ok c) + (hcore : c = sctlC ∨ c = utC) : + ∀ ct, sched.cur_thread wst s2 = ok ct → + SelectedCoreScope (wL.cpu s2) (wL.mem s2) σb ct (invokedCptr wst s2 ct n) := by + intro ct hct sl' c' hl' hc' + obtain ⟨hcpu, hcur, -⟩ := w_front h + rw [hcur] at hct + cases ok_inj hct + have e := hl.1.symm.trans hl' + have e2 := Set.singleton_eq_singleton_iff.1 (congrArg Prod.fst e) + have esl : sl = sl' := by + have := congrArg Prod.fst e2 + simp only [Except.ok.injEq, Prod.mk.injEq] at this + exact this.2 + subst esl + have ec : c' = c := ok_inj (hc'.symm.trans hl.2) + subst ec + rcases hcore with rfl | rfl + · exact hcpu.symm + · exact hcpu + +/-- **`S3Pre` at the witness, given `StartS3.core`.** -/ +theorem s3Pre_ww_of_core (hnd : numDomains = 1) (n : I64) (c : Fl4Spec.Syscall) (hn : NativeSys n c) + (core : ∀ (s2 : WSt) (a2 : MachineState) (σb : State Unit), + KFrontB (L := wL) warV ww σw.machineState s2 a2 → BudgetAt σw σb true → InvokesCap n → + ∀ ct, sched.cur_thread wst s2 = ok ct → + SelectedCoreScope (wL.cpu s2) (wL.mem s2) σb ct (invokedCptr wst s2 ct n)) : + S3Pre (L := wL) wst warV wAR ipiMR (.sys n c) ww σw.machineState σw := + ⟨⟨⟨rfl, rfl, rfl, rfl⟩, rfl, rfl⟩, w_zvCt, hn, w_flags, + { X := w_sinvXV, ct := w_nonIdle, ports := w_ports, uz := w_utZ, df := w_devRange, dp := w_gpmOut, + ut := w_utInv'', rv := w_rv, air := fun s2 a2 σb _ h => w_air s2 a2 σb hnd h, core := core, + idleVt := w_idleVtS }⟩ + +/-- **Non-vacuity, the SchedControl invocation** ([P] the backend, one domain). -/ +theorem witness_schedControl (hk : virtKind = .vmx ∨ virtKind = .svm) (hnd : numDomains = 1) : + VEAnyX (L := wL) warV wAR ww σw.machineState σw ∧ + S3Pre (L := wL) wst warV wAR ipiMR (.sys fl4_abi.syscall.SysCall .sysCall) ww σw.machineState σw := + ⟨veAnyX_ww hk _, s3Pre_ww_of_core hnd _ _ w_nativeSys fun _ _ _ h hBA _ => + w_core h (w_look_sc hnd h hBA) (Or.inl rfl)⟩ + +/-- **Non-vacuity, the Untyped invocation** ([P] the backend, one domain). -/ +theorem witness_untyped (hk : virtKind = .vmx ∨ virtKind = .svm) (hnd : numDomains = 1) : + VEAnyX (L := wL) warV wAR ww σw.machineState σw ∧ + S3Pre (L := wL) wst warV wAR ipiMR (.sys fl4_abi.syscall.SysNBSendWait .sysNBSendWait) ww σw.machineState σw := + ⟨veAnyX_ww hk _, s3Pre_ww_of_core hnd _ _ (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨rfl, rfl⟩))))) fun _ _ _ h hBA _ => + w_core h (w_look_ut hnd h hBA) (Or.inr rfl)⟩ + +/-- **`core` is exercised at the witness, for both invocations** (one domain): at a concrete dispatch pair the current +thread is the root thread on core 0, the SchedControl slot (`SysCall`) and the Untyped slot (`SysNBSendWait`) are +looked up and decode to their caps (`LookAt`, `capAt`), and `SelectedCoreScope` holds there, its premise met: +`sctlC`'s core is the executing core, and the Untyped invocation runs on core 0. -/ +theorem w_core_exercised (hnd : numDomains = 1) : ∃ (s2 : WSt) (a2 : MachineState) (σb : State Unit), + KFrontB (L := wL) warV ww σw.machineState s2 a2 ∧ BudgetAt σw σb true ∧ + sched.cur_thread wst s2 = ok rT ∧ wL.cpu s2 = 0#u64 ∧ + (InvokesCap fl4_abi.syscall.SysCall ∧ + LookAt rT (invokedCptr wst s2 rT fl4_abi.syscall.SysCall) σb (rC.bv, [false, true, true]) sctlC ∧ + capAt (wL.mem s2) (cslotAddr (rC.bv, [false, true, true])) = ok sctlC ∧ + SelectedCoreScope (wL.cpu s2) (wL.mem s2) σb rT (invokedCptr wst s2 rT fl4_abi.syscall.SysCall)) ∧ + (InvokesCap fl4_abi.syscall.SysNBSendWait ∧ + LookAt rT (invokedCptr wst s2 rT fl4_abi.syscall.SysNBSendWait) σb (rC.bv, [false, true, false]) utC ∧ + capAt (wL.mem s2) (cslotAddr (rC.bv, [false, true, false])) = ok utC ∧ + SelectedCoreScope (wL.cpu s2) (wL.mem s2) σb rT (invokedCptr wst s2 rT fl4_abi.syscall.SysNBSendWait)) := by + obtain ⟨s2, a2, σb, h, hBA⟩ := w_dispatch hnd + obtain ⟨hcpu, hcur, -⟩ := w_front h + have lsc := w_look_sc hnd h hBA + have lut := w_look_ut hnd h hBA + exact ⟨s2, a2, σb, h, hBA, hcur, hcpu, + ⟨Or.inr (Or.inr (Or.inl rfl)), lsc.1, lsc.2, w_core h lsc (Or.inl rfl) rT hcur⟩, + ⟨Or.inr (Or.inr (Or.inr (Or.inr (Or.inr rfl)))), lut.1, lut.2, w_core h lut (Or.inr rfl) rT hcur⟩⟩ + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitSpec.lean b/proofs/Fl4Proofs/P3/VmWitSpec.lean new file mode 100644 index 000000000..5170b728a --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitSpec.lean @@ -0,0 +1,731 @@ +import Fl4Proofs.P3.VmEntryS3Start +import Fl4Proofs.P3.JBootQ1Spec + +/-! +P3 (line mmuAD witness): **the spec half of the VM witness state** (generated by `handoff/tools/gen_witspec.py` +from `SInvXVWit`). `σw` of `SInvXVWit` plus a VCPU object at `vB`, bound to the `runningVM` guest, and its cap in +the root CNode's slot `[true,false,true]`. Every spec field of `SInvXVWit.startSpec_witness` holds with it +(`startSpec_witness`), `UtInv''` included (`w_utInv''`). + +The clock: the current time and the last machine time are `tW`, the cap of `getCurrentTime` +(`-(getCurrentTime_buffer + 1)`, l4v `getCurrentTime` saturates there), so the entry's time read returns `tW` +whatever the opaque `timeOracle` gives, the consumed time stays 0 and the root SC's head (`MAX_PERIOD`) passes the +budget check (P3/VmWitS3 `w_dispatch`). With the clock at 0 the check would pass only if `timeOracle 1` were at most +`MAX_PERIOD - MIN_BUDGET`, which the opaque oracle does not settle. `ValidMachineTime`, `OffReady` and `IdleHd` hold +at `tW` (the refill release times are 0). +No premise. New file. +-/ + +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-- The guest TCB's address `kernelBase + 0xB000`. -/ +def gT : U64 := 18446744071562113024#u64 +/-- The Untyped region's base `kernelBase + 0xC000`. -/ +def uB : U64 := 18446744071562117120#u64 + +theorem gT_toNat : gT.bv.toNat = 18446744071562113024 := rfl +theorem uB_toNat : uB.bv.toNat = 18446744071562117120 := rfl +/-- The guest VCPU's address `kernelBase + 0x100000` (32 KiB aligned, above the kernel-state block). -/ +def vB : U64 := 18446744071563116544#u64 +theorem vB_toNat : vB.bv.toNat = 18446744071563116544 := rfl +abbrev vcW : Cap := .archObjectCap (.vcpuCap vB.bv) +def vcpuW : VcpuState := { VcpuState.blank with tcb := some gT.bv } +theorem vB_irq (irq : Irq) : fIrqNode irq ≠ vB.bv := by + intro e + have h := congrArg BitVec.toNat e + have hs := sh_toNat irq + have := irq.isLt + have e1 : fkb.bv.toNat = 18446744071562133504 := rfl + have e2 : (1248#64 : BitVec 64).toNat = 1248 := rfl + have e3 : initIrqNodePtr.toNat = 18446744071562076160 := by decide + rw [vB_toNat] at h + unfold fIrqNode at h + split at h + · rw [BitVec.toNat_add, BitVec.toNat_add, hs, e1, e2] at h; omega + · rw [BitVec.toNat_add, hs, e3] at h; omega + +abbrev cnW : Cap := .cnodeCap rC.bv 3 [] +abbrev utW : Cap := .untypedCap false uB.bv 12 0 + +/-- The root TCB's registers: the idle TCB's with the capability register naming the SchedControl slot (3) and the +reply register naming the Untyped slot (2). -/ +def rootRegs : Register → Fl4Spec.Word := + Function.update (Function.update rootTcbV.tcbArch.tcbContext.userRegs capRegister 0x6000000000000000) + replyRegister 0x4000000000000000 +def rootTcbW : Tcb := + { rootTcbV with + tcbCtable := cnW + tcbArch := { rootTcbV.tcbArch with + tcbContext := { rootTcbV.tcbArch.tcbContext with userRegs := rootRegs } } } +def guestTcbV : Tcb := { idleTcbV with tcbState := .runningVM, tcbSchedContext := none } + +def wCs : CnodeContents := fun l => + if l = [false, false, false] then some (.threadCap rT.bv) + else if l = [false, false, true] then some (.schedContextCap rS.bv 0) + else if l = [false, true, false] then some utW + else if l = [false, true, true] then some .schedControlCap + else if l = [true, false, false] then some (.threadCap gT.bv) + else if l = [true, false, true] then some vcW + else if l.length = 3 then some .nullCap else none + +def khW : KHeap := + Function.update (Function.update (Function.update (Function.update (Function.update fInitKheap rT.bv (some (.tcb rootTcbW))) + rS.bv (some (.schedContext rootScV 0))) rC.bv (some (.cnode 3 wCs))) gT.bv (some (.tcb guestTcbV))) + vB.bv (some (.archObj (.vcpu vcpuW))) + +/-- **The witness's clock**: the cap of `getCurrentTime` (module docstring). -/ +def tW : Word := -(getCurrentTimeBuffer + 1) + +def σw : State Unit := + { σ1 with + kheap := khW + isOriginalCap := fun sl => decide (sl = (rC.bv, [false, true, false])) + curTime := tW + machineState := { σ1.machineState with lastMachineTime := tW } } + +theorem σw_kheap : σw.kheap = khW := rfl + +/-! ## The heap -/ + +theorem ne_TS : rT.bv ≠ rS.bv := by decide +theorem ne_TC : rT.bv ≠ rC.bv := by decide +theorem ne_TG : rT.bv ≠ gT.bv := by decide +theorem ne_SC : rS.bv ≠ rC.bv := by decide +theorem ne_SG : rS.bv ≠ gT.bv := by decide +theorem ne_CG : rC.bv ≠ gT.bv := by decide +theorem ne_TV : rT.bv ≠ vB.bv := by decide +theorem ne_SV : rS.bv ≠ vB.bv := by decide +theorem ne_CV : rC.bv ≠ vB.bv := by decide +theorem ne_GV : gT.bv ≠ vB.bv := by decide + +theorem kh_rT : khW rT.bv = some (.tcb rootTcbW) := by + unfold khW + rw [Function.update_of_ne ne_TV, Function.update_of_ne ne_TG, Function.update_of_ne ne_TC, Function.update_of_ne ne_TS, Function.update_self] +theorem kh_rS : khW rS.bv = some (.schedContext rootScV 0) := by + unfold khW; rw [Function.update_of_ne ne_SV, Function.update_of_ne ne_SG, Function.update_of_ne ne_SC, Function.update_self] +theorem kh_rC : khW rC.bv = some (.cnode 3 wCs) := by + unfold khW; rw [Function.update_of_ne ne_CV, Function.update_of_ne ne_CG, Function.update_self] +theorem kh_gT : khW gT.bv = some (.tcb guestTcbV) := by + unfold khW; rw [Function.update_of_ne ne_GV, Function.update_self] +theorem kh_vB : khW vB.bv = some (.archObj (.vcpu vcpuW)) := by + unfold khW; rw [Function.update_self] +theorem kh_old {x : ObjRef} (h1 : x ≠ rT.bv) (h2 : x ≠ rS.bv) (h3 : x ≠ rC.bv) (h4 : x ≠ gT.bv) + (h5 : x ≠ vB.bv) : + khW x = fInitKheap x := by + unfold khW + rw [Function.update_of_ne h5, Function.update_of_ne h4, Function.update_of_ne h3, Function.update_of_ne h2, Function.update_of_ne h1] + +theorem kh_some {p : ObjRef} {o : KernelObject} (h : khW p = some o) : + (p = rT.bv ∧ o = .tcb rootTcbW) ∨ (p = rS.bv ∧ o = .schedContext rootScV 0) ∨ + (p = rC.bv ∧ o = .cnode 3 wCs) ∨ (p = gT.bv ∧ o = .tcb guestTcbV) ∨ + (p = vB.bv ∧ o = .archObj (.vcpu vcpuW)) ∨ fInitKheap p = some o := by + by_cases e1 : p = rT.bv + · subst e1; rw [kh_rT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + by_cases e2 : p = rS.bv + · subst e2; rw [kh_rS] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + by_cases e3 : p = rC.bv + · subst e3; rw [kh_rC] at h; cases h; exact Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)) + by_cases e4 : p = gT.bv + · subst e4; rw [kh_gT] at h; cases h; exact Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩))) + by_cases e5 : p = vB.bv + · subst e5; rw [kh_vB] at h; cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)))) + rw [kh_old e1 e2 e3 e4 e5] at h + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr h)))) + +theorem kh_tcb_of {p : ObjRef} {x : Tcb} (h : khW p = some (.tcb x)) : + (p = rT.bv ∧ x = rootTcbW) ∨ (p = gT.bv ∧ x = guestTcbV) ∨ (p = idleThreadPtr ∧ x = idleTcbV) := by + rcases kh_some h with ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, e⟩ | ⟨-, e⟩ | h' + · cases e; exact Or.inl ⟨rfl, rfl⟩ + · cases e + · cases e + · cases e; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · cases e + · exact Or.inr (Or.inr (bootHeap_fInit.tcb_of h')) + +theorem kh_sc_of {p : ObjRef} {y : SchedContext} {n : Nat} (h : khW p = some (.schedContext y n)) : + (p = rS.bv ∧ y = rootScV ∧ n = 0) ∨ (p = idleScPtr ∧ y = idleScV ∧ n = 0) := by + rcases kh_some h with ⟨-, e⟩ | ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e; exact Or.inl ⟨rfl, rfl, rfl⟩ + · cases e + · cases e + · cases e + · exact Or.inr (bootHeap_fInit.sc_of h') + +theorem kh_no_ep {p : ObjRef} {x : Endpoint} (h : khW p = some (.endpoint x)) : False := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · exact bootHeap_fInit.no_ep h' + +theorem kh_no_ntfn {p : ObjRef} {x : Notification} (h : khW p = some (.notification x)) : False := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · exact bootHeap_fInit.no_ntfn h' + +theorem kh_idleT : khW idleThreadPtr = some (.tcb idleTcbV) := by + rw [kh_old (by decide) (by decide) (by decide) (by decide) (by decide)]; exact bootHeap_fInit.tcb +theorem kh_idleS : khW idleScPtr = some (.schedContext idleScV 0) := by + rw [kh_old (by decide) (by decide) (by decide) (by decide) (by decide)]; exact bootHeap_fInit.sc + +/-! ## The caps -/ + +theorem wCs_wf : wellFormedCnodeN 3 wCs := by + unfold wellFormedCnodeN dom + ext l + simp only [Set.mem_setOf_eq] + constructor + · intro h + unfold wCs at h + split_ifs at h with h1 h2 h3 h4 h5 h6 h7 + · subst h1; rfl + · subst h2; rfl + · subst h3; rfl + · subst h4; rfl + · subst h5; rfl + · subst h6; rfl + · exact h7 + · exact absurd h (by simp) + · intro hl + unfold wCs + split_ifs <;> simp + +theorem capView_wC : SInvU.capView (.cnode 3 wCs) = wCs := by simp [SInvU.capView, wCs_wf] +theorem capOpt_wC (l : CnodeIndex) : capOpt (some (.cnode 3 wCs)) l = wCs l := by simp [capOpt, wCs_wf] + +theorem wCs_cases {l : CnodeIndex} {c : Cap} (h : wCs l = some c) : + (c = .nullCap ∧ l.length = 3) ∨ (l = [false, false, false] ∧ c = .threadCap rT.bv) ∨ + (l = [false, false, true] ∧ c = .schedContextCap rS.bv 0) ∨ (l = [false, true, false] ∧ c = utW) ∨ + (l = [false, true, true] ∧ c = .schedControlCap) ∨ (l = [true, false, false] ∧ c = .threadCap gT.bv) ∨ + (l = [true, false, true] ∧ c = vcW) := by + unfold wCs at h + split_ifs at h with h1 h2 h3 h4 h5 h6 h7 + · cases h; exact Or.inr (Or.inl ⟨h1, rfl⟩) + · cases h; exact Or.inr (Or.inr (Or.inl ⟨h2, rfl⟩)) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inl ⟨h3, rfl⟩))) + · cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨h4, rfl⟩)))) + · cases h; exact Or.inr (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.inr ⟨h6, rfl⟩))))) + · cases h; exact Or.inl ⟨rfl, h7⟩ + +theorem rootW_map {l : CnodeIndex} {c : Cap} (h : tcbCnodeMap rootTcbW l = some c) : + (l = tcbCnodeIndex 0 ∧ c = cnW) ∨ c = .nullCap := by + unfold tcbCnodeMap at h + simp only [Function.update_apply] at h + split_ifs at h with h1 h2 h3 h4 h5 h6 <;> (try cases h) <;> (try exact Or.inr rfl) + exact Or.inl ⟨h6, rfl⟩ + +theorem guest_map {l : CnodeIndex} {c : Cap} (h : tcbCnodeMap guestTcbV l = some c) : c = .nullCap := by + unfold tcbCnodeMap at h + simp only [Function.update_apply] at h + split_ifs at h <;> (try cases h) <;> rfl + +/-- **The spec caps of `σw` by slot.** -/ +theorem capAtW {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) : + c = .nullCap ∨ (sl = (rT.bv, tcbCnodeIndex 0) ∧ c = cnW) ∨ + (sl = (rC.bv, [false, false, false]) ∧ c = .threadCap rT.bv) ∨ + (sl = (rC.bv, [false, false, true]) ∧ c = .schedContextCap rS.bv 0) ∨ + (sl = (rC.bv, [false, true, false]) ∧ c = utW) ∨ + (sl = (rC.bv, [false, true, true]) ∧ c = .schedControlCap) ∨ + (sl = (rC.bv, [true, false, false]) ∧ c = .threadCap gT.bv) ∨ + (sl = (rC.bv, [true, false, true]) ∧ c = vcW) := by + obtain ⟨p, i⟩ := sl + unfold SInvU.capAt at h + rw [σw_kheap] at h + split at h + · rename_i o ho + rcases kh_some ho with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h' + · rcases rootW_map h with ⟨rfl, rfl⟩ | rfl + · exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inl rfl + · cases h + · rw [capView_wC] at h + rcases wCs_cases h with ⟨rfl, -⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact Or.inl rfl + all_goals simp + · exact Or.inl (guest_map h) + · cases h + · left + have hb := bootHeap_fInit.capAt (N := fIrqNode) (sl := (p, i)) (c := c) + apply hb + unfold SInvU.capAt + show (match fInitKheap p with | some o => SInvU.capView o i | none => none) = some c + rw [h']; exact h + · cases h + +theorem capW {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) : + c = .nullCap ∨ c = cnW ∨ c = .threadCap rT.bv ∨ c = .schedContextCap rS.bv 0 ∨ c = utW ∨ + c = .schedControlCap ∨ c = .threadCap gT.bv ∨ c = vcW := by + rcases capAtW h with e | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> simp [e] + +theorem capOptW {p : ObjRef} {cref : CnodeIndex} {c : Cap} (h : capOpt (σw.kheap p) cref = some c) : + SInvU.capAt σw (p, cref) = some c := (SInvUZ.capAt_capOpt σw (p, cref)).trans h + +theorem capAt_slot {l : CnodeIndex} {c : Cap} (h : wCs l = some c) : SInvU.capAt σw (rC.bv, l) = some c := by + have e : SInvU.capAt σw (rC.bv, l) = SInvU.capView (.cnode 3 wCs) l := by + unfold SInvU.capAt; rw [σw_kheap, kh_rC] + rw [e, capView_wC]; exact h + +/-- The only untyped cap. -/ +theorem ut_only {sl : CslotPtr} {d : Bool} {b : ObjRef} {n f : Nat} + (h : SInvU.capAt σw sl = some (.untypedCap d b n f)) : + sl = (rC.bv, [false, true, false]) ∧ d = false ∧ b = uB.bv ∧ n = 12 ∧ f = 0 := by + rcases capAtW h with e | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨hs, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> cases e + exact ⟨hs, rfl, rfl, rfl, rfl⟩ + +/-! ## The Untyped region -/ + +theorem delR_toNat {x : ObjRef} (h : x ∈ SInvU.delR uB.bv 12) : + 18446744071562117120 ≤ x.toNat ∧ x.toNat ≤ 18446744071562121215 := by + obtain ⟨h1, h2⟩ := h + have e : (uB.bv + 2 ^ 12 - 1 : BitVec 64).toNat = 18446744071562121215 := by decide + rw [BitVec.le_def] at h1 h2 + rw [e] at h2 + rw [uB_toNat] at h1 + exact ⟨h1, h2⟩ + +theorem irq_not_region {x : ObjRef} (hx : x ∈ SInvU.delR uB.bv 12) (irq : Irq) : fIrqNode irq ≠ x := + fIrqNode_ne (by have := delR_toNat hx; omega) irq + +theorem not_region {x : ObjRef} (h : x.toNat < 18446744071562117120) : x ∉ SInvU.delR uB.bv 12 := + fun hx => by have := delR_toNat hx; omega + +/-- Every object of `σw` lies outside the Untyped region's range of addresses. -/ +theorem obj_out {p : ObjRef} {o : KernelObject} (h : khW p = some o) : + p.toNat < 18446744071562117120 ∨ 18446744071562121215 < p.toNat := by + by_contra hc + have hx : 18446744071562117120 ≤ p.toNat ∧ p.toNat ≤ 18446744071562121215 := by omega + have ne : ∀ q : ObjRef, q.toNat < 18446744071562117120 → p ≠ q := fun q hq e => by subst e; omega + have h1 := ne rT.bv (by rw [rT_toNat]; omega) + have h2 := ne rS.bv (by rw [rS_toNat]; omega) + have h3 := ne rC.bv (by rw [rC_toNat]; omega) + have h4 := ne gT.bv (by rw [gT_toNat]; omega) + have h5 : p ≠ vB.bv := fun e => by subst e; rw [vB_toNat] at hx; omega + rw [kh_old h1 h2 h3 h4 h5, fInitKheap_none (ne _ (by decide)) (ne _ (by decide)) (ne _ (by decide)) + (ne _ (by decide)) (ne _ (by decide)) (fIrqNode_ne (by omega))] at h + cases h + +theorem cov_cases {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) {r : ObjRef} (hr : r ∈ SInvU.covR c) : + r = rT.bv ∨ r = rS.bv ∨ r = rC.bv ∨ r = gT.bv ∨ r = vB.bv ∨ r ∈ SInvU.delR uB.bv 12 := by + rcases capW h with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl + all_goals first + | exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr hr)))) + | (simp [SInvU.covR, SInvU.capRng, SInvU.acRng, objRefs, aobjRef] at hr <;> simp [hr]) + +theorem objRefsW {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) {r : ObjRef} (hr : r ∈ objRefs c) : + r = rT.bv ∨ r = rS.bv ∨ r = rC.bv ∨ r = gT.bv ∨ r = vB.bv := by + rcases capW h with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [objRefs, aobjRef] at hr <;> simp [hr] + +theorem irqOutW (N : Irq → ObjRef) + (hN : ∀ irq, N irq ≠ rT.bv ∧ N irq ≠ rS.bv ∧ N irq ≠ rC.bv ∧ N irq ≠ gT.bv ∧ N irq ≠ vB.bv ∧ + N irq ∉ SInvU.delR uB.bv 12) : + SInvU.IrqOutV N (SInvU.uV σw) := by + intro irq s c hs hr + rcases cov_cases hs hr with e | e | e | e | e | e + · exact (hN irq).1 e + · exact (hN irq).2.1 e + · exact (hN irq).2.2.1 e + · exact (hN irq).2.2.2.1 e + · exact (hN irq).2.2.2.2.1 e + · exact (hN irq).2.2.2.2.2 e + +theorem gT_irq (irq : Irq) : fIrqNode irq ≠ gT.bv := fIrqNode_ne (by rw [gT_toNat]; omega) irq + +theorem irq_out (irq : Irq) : fIrqNode irq ≠ rT.bv ∧ fIrqNode irq ≠ rS.bv ∧ fIrqNode irq ≠ rC.bv ∧ + fIrqNode irq ≠ gT.bv ∧ fIrqNode irq ≠ vB.bv ∧ fIrqNode irq ∉ SInvU.delR uB.bv 12 := + ⟨rT_irq irq, rS_irq irq, rC_irq irq, gT_irq irq, vB_irq irq, fun h => irq_not_region h irq rfl⟩ + +theorem idleT_out : idleThreadPtr ≠ rT.bv ∧ idleThreadPtr ≠ rS.bv ∧ idleThreadPtr ≠ rC.bv ∧ + idleThreadPtr ≠ gT.bv ∧ idleThreadPtr ≠ vB.bv ∧ idleThreadPtr ∉ SInvU.delR uB.bv 12 := + ⟨by decide, by decide, by decide, by decide, by decide, not_region (by decide)⟩ + +/-! ## `SInvAV` -/ + +theorem tiW {Q : Tcb → Prop} (h0 : SInv.TI Q (initASt (Z := Unit))) (h1 : Q rootTcbW) (h2 : Q guestTcbV) : + SInv.TI Q σw := by + intro p x hp + rcases kh_tcb_of hp with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ + · exact h1 + · exact h2 + · exact h0 _ _ initA_idleT + +theorem w_vfh : SInv.VfhAll σw := tiW SInv.boot_vfhAll (Or.inr rfl) (Or.inr rfl) + +theorem w_cnDom : SInv.CnDom σw := by + intro sl c hc p r g e + subst e + rcases capAtW (capOptW hc) with e | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ <;> cases e + intro l hl + rw [σw_kheap, kh_rC, capOpt_wC] + unfold wCs + split_ifs <;> simp + +theorem w_fts : SInv.FaultTcbsS σw := tiW SInv.boot_faultTcbsS (fun h => absurd rfl h) (fun h => absurd rfl h) + +theorem w_nqb : SInv.NQB σw := by + rintro t ⟨n, x, hn, -⟩ + exact (kh_no_ntfn hn).elim + +theorem w_specGlob : SInvG.SpecGlobS σw ∧ SInvG.IdleOutV σw := by + refine ⟨⟨fun irq a ha => ?_, fun p cref a ha _ irq hr => ?_, fun p cref a ha _ hr => ?_⟩, ?_⟩ + · left + rcases capAtW ha with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact e + · exact absurd (congrArg Prod.fst e) (rT_irq irq) + all_goals exact absurd (congrArg Prod.fst e) (rC_irq irq) + · rcases objRefsW ha hr with e | e | e | e | e + · exact rT_irq irq e + · exact rS_irq irq e + · exact rC_irq irq e + · exact gT_irq irq e + · exact vB_irq irq e + · rcases objRefsW ha hr with e | e | e | e | e <;> exact absurd e (by decide) + · exact irqOutW _ (fun _ => idleT_out) + +theorem w_dom : DomListOK σw := SInvDom.domListOK_of_dlV (σ := σ1) (σ' := σw) rfl σ1_dom +theorem w_vim : SInvV.ValidIrqMasks σw := fun _ _ => rfl + +theorem w_vih : SInvV.ValidIrqHandlers σw := by + intro p cref irq hc + rcases capW (capOptW hc) with e | e | e | e | e | e | e | e <;> cases e + +theorem w_vmt : SInvV.ValidMachineTime σw := + ⟨BitVec.le_refl _, by show tW.toNat ≤ tW.toNat; exact Nat.le_refl _⟩ + +theorem w_cdt (x : CslotPtr) : (SInvU.uV σw).2.2.1 x = none := by + simp [SInvU.uV, σw, σ1, fInitASt, initASt, initCdt] + +theorem w_utInv : SInvU.UtInv σw := by + refine ⟨?_, ?_, ?_, ?_, ?_⟩ + · intro sl d b n f hc p hp hm + obtain ⟨-, -, rfl, rfl, rfl⟩ := ut_only hc + have hs : (khW p).isSome = true := hp + obtain ⟨o, ho⟩ := Option.isSome_iff_exists.1 hs + have hm' : uB.bv.toNat + 0 ≤ p.toNat ∧ p.toNat < uB.bv.toNat + 2 ^ 12 := hm + rw [uB_toNat] at hm' + rcases obj_out ho with h | h <;> omega + · intro sl d b n f hc + obtain ⟨-, -, rfl, rfl, rfl⟩ := ut_only hc + refine ⟨by decide, by rw [uB_toNat], by omega⟩ + · intro s1 s2 d1 b1 n1 f1 d2 b2 n2 f2 hne h1 h2 + exact absurd ((ut_only h1).1.trans (ut_only h2).1.symm) hne + · intro s1 s2 d1 b1 n1 f1 d2 b2 n2 f2 hne h1 h2 + exact absurd ((ut_only h1).1.trans (ut_only h2).1.symm) hne + · intro sl d b n f hc + obtain ⟨rfl, -⟩ := ut_only hc + show decide _ = true + simp + +theorem w_utInv' : SInvU.UtInv' σw := by + refine ⟨w_utInv, fun c p h => ?_, fun x hx => ?_⟩ + · rw [w_cdt] at h; cases h + · obtain ⟨c, hc⟩ := SInvU.tg_first hx + rw [w_cdt] at hc; cases hc + +/-- No cap's object references lie in the Untyped region. -/ +theorem rng_out {sl : CslotPtr} {c : Cap} (h : SInvU.capAt σw sl = some c) {r : ObjRef} (hr : r ∈ SInvU.capRng c) : + r.toNat < 18446744071562117120 ∨ 18446744071562121215 < r.toNat := by + rcases capW h with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.capRng, SInvU.acRng, objRefs, aobjRef] at hr <;> subst hr <;> + first | (left; decide) | (right; decide) + +theorem w_utNV : SInvU.UtNV (SInvU.uV σw) := by + have hc : ∀ sl c, (SInvU.uV σw).2.1 sl = some c → SInvU.capAt σw sl = some c := fun _ _ h => h + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ + · intro u dev b n fi hu s c r hs hr hd + obtain ⟨-, -, rfl, rfl, -⟩ := ut_only (hc _ _ hu) + have h1 := delR_toNat hd + have h2 := rng_out (hc _ _ hs) hr + omega + · rintro p c hp hne ⟨u, dev, b, n, fi, hu, hm⟩ + obtain ⟨-, -, rfl, rfl, -⟩ := ut_only (hc _ _ hu) + rcases capAtW (hc _ _ hp) with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + all_goals + subst e + exact absurd hm (not_region (by dsimp only; decide)) + · intro u dev b n fi hu s c r hs hr hm + obtain ⟨-, -, rfl, rfl, rfl⟩ := ut_only (hc _ _ hu) + have hm' : uB.bv.toNat + 0 ≤ r.toNat ∧ r.toNat < uB.bv.toNat + 2 ^ 12 := hm + rw [uB_toNat] at hm' + have := rng_out (hc _ _ hs) hr + omega + · intro s c s' c' r hs hs' hr hr' + rcases capW (hc _ _ hs) with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + rcases capW (hc _ _ hs') with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> + simp [SInvU.capRng, SInvU.acRng, SInvU.sRefs, objRefs, aobjRef] at hr hr' <;> (try subst hr) <;> + first | rfl | (exact absurd hr' (by decide)) | (exact absurd hr'.symm (by decide)) | + (rw [hr] at hr'; exact absurd hr' (by decide)) + · intro s c r hs hr i c' hc' hne + rcases capW (hc _ _ hs) with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.sRefs] at hr <;> subst hr + · rcases capAtW (hc _ _ hc') with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + · exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + all_goals cases e; rfl + · rcases capAtW (hc _ _ hc') with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + · cases e; exact ⟨0, by decide, rfl⟩ + all_goals exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + · rcases capAtW (hc _ _ hc') with e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact absurd e hne + all_goals exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + · intro s r b n hs; rcases capW (hc _ _ hs) with e | e | e | e | e | e | e | e <;> cases e + · intro s c hs b hb + rcases capW (hc _ _ hs) with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.shp] at hb + omega + +theorem w_utInv'' : SInvU.UtInv'' σw := ⟨w_utInv', w_utNV⟩ + +theorem w_utInv3 : SInvU.UtInv3 σw := ⟨w_utInv'', irqOutW _ irq_out⟩ + +theorem w_ntfnQB : SInv.NtfnQB σw := fun _ _ _ hn _ => (kh_no_ntfn hn).elim +theorem w_epQB : SInv.EpQB σw := fun _ _ _ hn _ => (kh_no_ep hn).elim + +theorem w_thr : SInvU.capAt σw (rC.bv, [false, false, false]) = some (.threadCap rT.bv) := capAt_slot rfl +theorem w_scc : SInvU.capAt σw (rC.bv, [false, false, true]) = some (.schedContextCap rS.bv 0) := capAt_slot rfl +theorem w_ut : SInvU.capAt σw (rC.bv, [false, true, false]) = some utW := capAt_slot rfl +theorem w_sctl : SInvU.capAt σw (rC.bv, [false, true, true]) = some .schedControlCap := capAt_slot rfl +theorem w_gthr : SInvU.capAt σw (rC.bv, [true, false, false]) = some (.threadCap gT.bv) := capAt_slot rfl +theorem w_vc : SInvU.capAt σw (rC.bv, [true, false, true]) = some vcW := capAt_slot rfl + +theorem w_ifLiveT : SInv.IfLiveT σw := by + intro t y ht hl + rcases kh_tcb_of ht with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, rfl⟩ + · exact ⟨_, w_thr⟩ + · exact ⟨_, w_gthr⟩ + · exact False.elim hl + +theorem w_validIdle : SInvI.ValidIdle σw := by + unfold SInvI.ValidIdle SInvI.VI SInvI.VIT + refine ⟨rfl, rfl, fun e => absurd e Bool.false_ne_true, ⟨_, kh_idleT⟩, ⟨_, 0, kh_idleS⟩, ?_⟩ + intro p o hp + change SInvI.ObjOK idleThreadPtr idleScPtr idleThreadPtr p o + rcases kh_some hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h' + · refine ⟨fun e => absurd e (by decide), fun _ => ?_, fun e => absurd e (by decide)⟩ + simp only [rootTcbW, rootTcbV, ne_eq, Option.some.injEq]; decide + · refine ⟨fun e => absurd e (by decide), fun _ => ?_⟩ + simp only [rootScV, ne_eq, Option.some.injEq]; decide + · trivial + · refine ⟨fun e => absurd e (by decide), fun _ => ?_, fun e => absurd e (by decide)⟩ + simp [guestTcbV] + · trivial + · exact (BootHeap.validIdle (N := fIrqNode) bootHeap_fInit).2.2.2.2.2 _ _ h' + +theorem w_ft : SInvF.FT σw := fun t ht => by + simp [σw, σ1, fInitASt, initASt, initArchState] at ht + +theorem w_scLiveT : SInvL.ScLiveT (SInvL.idleP σw) σw := by + intro p o hp hl h1 h2 + rcases kh_some hp with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | h' + · exact ⟨_, _, w_thr, rfl⟩ + · exact ⟨_, _, w_scc, rfl⟩ + · exact hl.elim + · exact absurd rfl hl + · exact hl.elim + · exfalso + rcases bootHeap_fInit.obj _ _ h' with rfl | rfl | rfl | rfl | rfl | rfl + · exact h1 rfl + · exact h2 rfl + all_goals exact hl + +theorem w_idleScOutV : SInvL.IdleScOutV σw := + irqOutW _ (fun _ => ⟨by show idleScPtr ≠ _; decide, by show idleScPtr ≠ _; decide, by show idleScPtr ≠ _; decide, + by show idleScPtr ≠ _; decide, by show idleScPtr ≠ _; decide, not_region (by show idleScPtr.toNat < _; decide)⟩) + +theorem w_ntfnScB : SInvL.NtfnScB σw := fun _ _ _ hn _ => (kh_no_ntfn hn).elim + +theorem w_scInv : SInvB.ScInv σw := by + refine ⟨⟨fun u xu c hu hc => ?_, fun p y n u hp hu => ?_⟩, fun p y n hp => ?_⟩ + · rcases kh_tcb_of hu with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · cases hc; exact ⟨rootScV, 0, kh_rS, rfl, idleSc_rne⟩ + · cases hc + · cases hc; exact ⟨idleScV, 0, kh_idleS, rfl, idleSc_rne⟩ + · rcases kh_sc_of hp with ⟨rfl, rfl, -⟩ | ⟨rfl, rfl, -⟩ + · cases hu; exact ⟨rootTcbW, kh_rT, rfl⟩ + · cases hu; exact ⟨idleTcbV, kh_idleT, rfl⟩ + · rcases kh_sc_of hp with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_rne + · exact idleSc_rne + +theorem w_scKindR : SInvL.ScKindR σw := by + intro s r b s' c' h1 h2 hr + have h1' : SInvU.capAt σw s = some (.schedContextCap r b) := h1 + rcases capW h1' with e | e | e | e | e | e | e | e <;> cases e + rcases capW h2 with rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> simp [SInvU.capRng, SInvU.acRng, objRefs, aobjRef] at hr + all_goals first | exact ⟨0, rfl⟩ | exact absurd hr (by decide) + +theorem w_curScT : SInvQ.CurScT σw := by + intro x hx + have hx' : khW rT.bv = some (.tcb x) := hx + rw [kh_rT] at hx'; cases hx'; rfl + +theorem w_sinvAV : SInvAll.SInvAV σw where + vfh := w_vfh + cnDom := w_cnDom + fts := w_fts + nqb := w_nqb + sg := w_specGlob.1 + idleOut := w_specGlob.2 + dom := w_dom + vim := w_vim + vih := w_vih + vmt := w_vmt + ut := w_utInv3 + nq := w_ntfnQB + epq := w_epQB + live := w_ifLiveT + vi := w_validIdle + ft := w_ft + sl := ⟨⟨w_scLiveT, w_idleScOutV, w_ntfnScB, w_scInv⟩, w_scKindR⟩ + cs := w_curScT + +/-! ## The rest of `SInvXV` -/ + +theorem w_ctExit : SInvAll.CtExit σw := ⟨_, kh_rT, Or.inl rfl⟩ + +theorem w_onlyIdle : SInvAll.OnlyIdle σw := by + intro t x ht hs + rcases kh_tcb_of ht with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · cases hs + · cases hs + · rfl + +theorem w_ex : SInvQ.EX σw := + ⟨⟨⟨fun _ _ _ h => (List.not_mem_nil h).elim, trivial⟩, + ⟨fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, + fun _ _ _ h => List.not_mem_nil h⟩⟩, + ⟨fun _ _ h => by simp [σw, σ1, fInitASt, initASt] at h, + ⟨fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, w_curScT⟩⟩⟩ + +theorem w_rb (k : Bool) : SInvQ.RBk k σw := by + refine ⟨⟨⟨fun _ _ _ h => by simp [σw, σ1, fInitASt, initASt] at h, + fun _ h => by simp [σw, σ1, fInitASt, initASt] at h⟩, + fun _ => ⟨fun _ _ h => by simp [σw, σ1, fInitASt, initASt] at h, + ⟨fun h => by simp [σw, σ1, fInitASt, initASt] at h, fun h => by simp [σw, σ1, fInitASt, initASt] at h⟩⟩, + fun _ h => by simp [σw, σ1, fInitASt, initASt] at h⟩, + fun _ => Or.inl ⟨rootTcbW, kh_rT, rfl⟩⟩ + +theorem w_t4 : SInvQ.T4 σw := + ⟨SInvQ.boot_t4.1, rfl, tiW SInvQ.boot_t4.2.2 + (by have h := SInvQ.boot_t4.2.2 idleThreadPtr idleTcbV initA_idleT; exact h) + (by have h := SInvQ.boot_t4.2.2 idleThreadPtr idleTcbV initA_idleT; exact h)⟩ + +theorem w_bsa : Sched.BlockedScActive σw := by + refine ⟨fun p y n t x hp hyt hx hq => ?_, fun p y n hp hr => ?_⟩ + · rcases kh_sc_of hp with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_active + · exact idleSc_active + · rcases kh_sc_of hp with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_active + · exact idleSc_active + +theorem w_vb : SInvR.B8n.VB σw := by + intro t hs hne _ + obtain ⟨x, c, -, -, hx, hc, hr, -, -, -⟩ := Sched.schedulable_inv hs + rcases kh_tcb_of hx with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd rfl hne + · simp [guestTcbV] at hc + · simp [idleTcbV, runnable] at hr + +theorem w_rdy : SInvR.Rdy σw := by + refine ⟨fun d p t h => by simp [σw, σ1, fInitASt, initASt] at h, + fun t h => by simp [σw, σ1, fInitASt, initASt] at h, + fun t x c hx hb _ => ?_, fun p y n hy ha => ?_, fun t h => by simp [σw, σ1, fInitASt, initASt] at h, + fun y n hy => ?_⟩ + · exfalso + rcases kh_tcb_of hx with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ + · have e : rootTcbW.tcbState = .running := rfl + rcases hb with ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ <;> rw [e] at h <;> cases h + · have e : guestTcbV.tcbState = .runningVM := rfl + rcases hb with ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ <;> rw [e] at h <;> cases h + · have e : idleTcbV.tcbState = .idleThreadState := rfl + rcases hb with ⟨_, h⟩ | ⟨_, _, h, _⟩ | ⟨_, _, _, h⟩ <;> rw [e] at h <;> cases h + · rcases kh_sc_of hy with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_rvs + · exact idleSc_rvs + · have hy' : khW idleScPtr = some (.schedContext y n) := hy + rw [kh_idleS] at hy'; cases hy' + exact ⟨(SInvR.boot_rdy.2.2.2.2.2 _ _ initA_idleS).1, by show (0 : Nat) ≤ _; exact Nat.zero_le _⟩ + +theorem w_offReady : Sched.OffReady σw := by + intro y n hy + have hy' : khW rS.bv = some (.schedContext y n) := hy + rw [kh_rS] at hy'; cases hy' + show (0 : Nat) + 0 ≤ _ + exact Nat.zero_le _ + +theorem w_cAx : SInvR.CAx σw := fun _ => ⟨rootTcbW, rS.bv, rootScV, 0, kh_rT, rfl, kh_rS, idleSc_active⟩ + +theorem w_rpos : SInvE.RPos σw := by + intro p y n hy + rcases kh_sc_of hy with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact SInvE.boot_rpos idleScPtr idleScV 0 initA_idleS + · exact SInvE.boot_rpos idleScPtr idleScV 0 initA_idleS + +theorem w_sinvXV : SInvR.G4.SInvXV σw := + ⟨⟨⟨⟨⟨⟨⟨⟨w_sinvAV, w_ctExit⟩, w_onlyIdle⟩, w_ex, w_rb true, w_t4⟩, rfl⟩, w_bsa⟩, w_vb⟩, + w_rdy, w_offReady, w_cAx⟩, w_rpos⟩ + +/-! ## The other spec fields of `StartS3` -/ + +theorem w_ports : SpecIoPorts σw := by + intro sl f l o h + obtain ⟨p, i⟩ := sl + have hc := capOptW ((getCap_pair_iff σw p i _).1 h) + rcases capW hc with e | e | e | e | e | e | e | e <;> cases e + +theorem w_utZ : SInvR.G5.SpecUtZ σw := fun _ _ _ _ _ _ _ _ => rfl + +theorem w_devRange : SInvDV.DevRangeS σw := by + intro p cref b n f hc + cases (ut_only (capOptW hc)).2.1 + +theorem w_gpmOut : SInvGp.GpmOutV σw := + irqOutW _ (fun _ => ⟨by show initGlobalPml4 ≠ _; decide, by show initGlobalPml4 ≠ _; decide, + by show initGlobalPml4 ≠ _; decide, by show initGlobalPml4 ≠ _; decide, by show initGlobalPml4 ≠ _; decide, + not_region (by show initGlobalPml4.toNat < _; decide)⟩) + +theorem w_rv : SInvR.RV σw := fun p y n hy ha => w_rdy.2.2.2.1 p y n hy ha + +theorem w_idleVtS : SInvIV.IdleVtS σw := by + intro a ha + rcases capAtW (sl := (σw.idleThread, tcbCnodeIndex 1)) ha with + e | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ | ⟨e, -⟩ + · exact e + all_goals exact absurd (congrArg Prod.fst e) (by dsimp only; decide) + +theorem w_nonIdle : σw.curThread ≠ σw.idleThread := σ1_nonIdle + +/-- **The spec fields of `StartS3` hold together at a state with an Untyped cap and a SchedControl cap in the current +thread's CSpace and a `runningVM` TCB** (module docstring). -/ +theorem startSpec_witness : + (SInvR.G4.SInvXV σw ∧ σw.curThread ≠ σw.idleThread ∧ SpecIoPorts σw ∧ SInvR.G5.SpecUtZ σw ∧ + SInvDV.DevRangeS σw ∧ SInvGp.GpmOutV σw ∧ SInvU.UtInv'' σw ∧ SInvR.RV σw ∧ SInvIV.IdleVtS σw) ∧ + (∃ x, σw.kheap σw.curThread = some (.tcb x) ∧ x.tcbCtable = cnW) ∧ + SInvU.capAt σw (rC.bv, [false, true, false]) = some utW ∧ + SInvU.capAt σw (rC.bv, [false, true, true]) = some .schedControlCap ∧ + (∃ t x, σw.kheap t = some (.tcb x) ∧ x.tcbState = .runningVM) := + ⟨⟨w_sinvXV, w_nonIdle, w_ports, w_utZ, w_devRange, w_gpmOut, w_utInv'', w_rv, w_idleVtS⟩, + ⟨rootTcbW, kh_rT, rfl⟩, w_ut, w_sctl, ⟨gT.bv, guestTcbV, kh_gT, rfl⟩⟩ + +/-- The native bundle is false at `σw`: it carries `NativeScopeS`. -/ +theorem σw_not_sinvX : ¬ SInvR.G4.SInvX σw := + fun h => h.sb.1.1.ns gT.bv guestTcbV kh_gT rfl + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitSpecB.lean b/proofs/Fl4Proofs/P3/VmWitSpecB.lean new file mode 100644 index 000000000..45818e573 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitSpecB.lean @@ -0,0 +1,163 @@ +/- +P3 (line mmuAD witness): **the scheduler spec facts of `JAtZB` at `σw`** (P3/VmWitSpec), on the plan of +P3/JBootQ1Spec's last part. + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitMem +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem rS_memw : rS ∈ C1 := Or.inr rfl + +theorem σw_nsc : Sched.NSC ∅ C1 σw := fun _ hn => hn.elim + +theorem root_releasedw : released σw rootScV := by + have e : (refillHd rootScV).rTime = 0 := rfl + refine ⟨?_, Sched.suff0_of idleSc_rvs.2.1⟩ + simp [refillReady', e, σw, σ1, fInitASt, initASt] <;> simp [BitVec.le_def] + +theorem σw_inv : Sched.Inv σw where + rq _ _ _ h := (List.not_mem_nil h).elim + rel _ h := (List.not_mem_nil h).elim + sorted := List.Pairwise.nil + rd u x hk hc := by + rcases kh_tcb_of hk with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, rfl⟩ + · intro x' scp y n hx hs hy _ _ + rw [σw_kheap, kh_rT] at hx; cases hx; cases hs + rw [σw_kheap, kh_rS] at hy; cases hy + exact root_releasedw + · intro x' scp y n hx hs _ _ _ + rw [σw_kheap, kh_gT] at hx; cases hx; cases hs + · exfalso + rcases hc with h | ⟨n, h⟩ | ⟨e, d, h, -⟩ | ⟨e, r, d, h⟩ + · exact absurd h (by decide) + all_goals simp [idleTcbV] at h + refills p y n hk _ hper := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact absurd rfl hper + · exact absurd rfl hper + back u x p hk hp := by + rcases kh_tcb_of hk with ⟨rfl, rfl⟩ | ⟨-, rfl⟩ | ⟨rfl, rfl⟩ + · cases hp; exact ⟨rootScV, 0, kh_rS, rfl⟩ + · cases hp + · cases hp; exact ⟨idleScV, 0, kh_idleS, rfl⟩ + ntfnSc _ _ _ h _ := (kh_no_ntfn h).elim + ne p y n hk _ := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ <;> simp [rootScV, idleScV] + rr p y n hk _ _ := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · exact idleSc_rrok + · exact idleSc_rrok + +theorem rootW_notIpcBlk : NotIpcBlk rootTcbW.tcbState := by + unfold NotIpcBlk + refine ⟨fun q h => ?_, fun e d h => ?_, fun e r d h => ?_⟩ <;> simp [rootTcbW, rootTcbV] at h + +theorem σw_curInv : Sched.CurInv σw where + tcb := ⟨rootTcbW, kh_rT, rfl, rootW_notIpcBlk⟩ + sc := ⟨rootScV, 0, kh_rS⟩ + active _ x n hk := by + have hk' : khW rS.bv = some (.schedContext x n) := hk + rw [kh_rS] at hk'; cases hk'; exact idleSc_active + notRel := List.not_mem_nil + +theorem σw_own : Sched.CurOwnW Tw C1 σw := by + refine Or.inl ⟨rT, rT_mem, rootTcbW, kh_rT, rfl, fun x n hk => ?_, fun h => absurd h (by decide)⟩ + have hk' : khW rS.bv = some (.schedContext x n) := hk + rw [kh_rS] at hk'; cases hk'; rfl + +theorem σw_off : Sched.OffOK σw := fun h => absurd h (by decide) + +theorem σw_act : Sched.ActIdle σw := by + unfold ActIdle ActSched IdleOK + exact ⟨fun _ h => by simp [σw, σ1, fInitASt, initASt] at h, ⟨idleTcbV, kh_idleT, rfl, rfl, List.not_mem_nil⟩⟩ + +theorem σw_sv : Sched.SchedValid σw where + epQ _ _ h := (kh_no_ep h).elim + ntfnQ _ _ h := (kh_no_ntfn h).elim + recvQ t x ep ro d hk hs := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hs + sendQ t x ep pl hk hs := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hs + ntfnW t x n hk hs := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hs + recvReply t x ep r d hk hs := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hs + onReply t x r hk hs := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hs + replyTcb r x t h _ := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · rcases bootHeap_fInit.obj _ _ h' with h1 | h1 | e | e | e | e + · subst h1; rw [bootHeap_fInit.tcb] at h'; cases h' + · subst h1; rw [bootHeap_fInit.sc] at h'; cases h' + all_goals cases e + replies sc y k r hk hr := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ <;> simp [rootScV, idleScV, defaultSchedContext] at hr + scTcb sc y k f hk hf := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ + · cases hf; exact ⟨rootTcbW, kh_rT⟩ + · cases hf; exact ⟨idleTcbV, kh_idleT⟩ + scNtfn sc y k n hk hf := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ <;> simp [rootScV, idleScV, defaultSchedContext] at hf + scYieldFrom sc y k t hk hf := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ <;> simp [rootScV, idleScV, defaultSchedContext] at hf + tcbYieldTo t x p hk hy := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hy + tcbNtfn t x n hk hn := by + rcases kh_tcb_of hk with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ <;> simp [rootTcbW, rootTcbV, guestTcbV, idleTcbV] at hn + ntfnTcb _ _ _ h _ := (kh_no_ntfn h).elim + scSpor sc y k hk hs := by + rcases kh_sc_of hk with ⟨-, rfl, -⟩ | ⟨-, rfl, -⟩ <;> simp [rootScV, idleScV, defaultSchedContext] at hs + +theorem σw_bsa : Sched.BlockedScActive σw := w_bsa + +theorem σw_isorted : Sched.IpcSorted σw := + ⟨fun _ _ h => (kh_no_ep h).elim, fun _ _ h => (kh_no_ntfn h).elim⟩ + +theorem σw_slink : Sched.ScLinkHyp σw := σw_sv.toLink + +theorem σw_curRun : CurRunM σw := by + refine Or.inr (Or.inl fun x hx => ?_) + have hx' : khW rT.bv = some (.tcb x) := hx + rw [kh_rT] at hx'; cases hx'; rfl + +theorem σw_obj : Sched.ObjCover ∅ Tw ∅ ∅ C1 σw where + tcb o x h := by + rcases kh_tcb_of h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact ⟨rT, rT_mem, rfl⟩ + · exact ⟨gT, gT_mem, rfl⟩ + · exact ⟨idT, idT_mem, idT_bv⟩ + ntfn _ _ h := (kh_no_ntfn h).elim + ep _ _ h := (kh_no_ep h).elim + reply r x h := by + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · rcases bootHeap_fInit.obj _ _ h' with h1 | h1 | e | e | e | e + · subst h1; rw [bootHeap_fInit.tcb] at h'; cases h' + · subst h1; rw [bootHeap_fInit.sc] at h'; cases h' + all_goals cases e + sc o y k h := by + rcases kh_sc_of h with ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact ⟨rS, rS_memw, rfl⟩ + · exact ⟨idS, idS_mem, idS_bv⟩ + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitVcpu.lean b/proofs/Fl4Proofs/P3/VmWitVcpu.lean new file mode 100644 index 000000000..aab38c1ba --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitVcpu.lean @@ -0,0 +1,415 @@ +/- +P3 (line mmuAD witness): **`VEAnyX` at the VM witness** `ww` / `σw`: the VCPU record `VcpuCore` at `{vB}`, the empty +guest-table record `EptCore ∅` and the witness architecture `warV`, which returns the platform's `virtualization` +code. + +Named premise: [P] `virtKind = .vmx ∨ virtKind = .svm` (a running guest forces a backend, P3/VmWitPlatform). + +No sorry. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitJAt +import Fl4Proofs.P3.VmWitPlatform +import Fl4Proofs.P3.VmDispX +import Fl4Proofs.P3.JBootIoa +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P3.Notif Fl4Proofs.P2.Wit Classical + +noncomputable section + +set_option maxHeartbeats 1000000 + +/-! ## The VCPU's words -/ + +/-- Every word of the VCPU other than its `TCB` word is zero. -/ +theorem memw_vcpu {a : U64} (h1 : vB.val ≤ a.val) (h2 : a.val / 8 ≠ 18446744071563137024 / 8) : + wL.mem ww a = 0#u64 := by + have hv : vB.val = 18446744071563116544 := vB_val + rw [mem_ww] + imgwz + +theorem vmemW_vcpu (o : BitVec 64) (ho : o.toNat < 32768) (h : o.toNat / 8 ≠ 20480 / 8) : + vmemW (wL.mem ww) (vB.bv + o) = 0 := by + have hv : (vB.bv + o).toNat = 18446744071563116544 + o.toNat := by + rw [BitVec.toNat_add, vB_toNat]; omega + unfold vmemW + split + · have hv' : (⟨vB.bv + o⟩ : U64).val = 18446744071563116544 + o.toNat := hv + rw [memw_vcpu (a := ⟨vB.bv + o⟩) (by rw [hv', vB_val]; omega) (by rw [hv']; omega)] + rfl + · rfl + +theorem vmemW_vtcb : vmemW (wL.mem ww) (vB.bv + VcpuLay.TCB) = gT.bv := by + have hv : (vB.bv + VcpuLay.TCB).toNat = 18446744071563137024 := by decide + unfold vmemW + rw [if_pos (by rw [hv]), memw_al (a := ⟨vB.bv + VcpuLay.TCB⟩) (n := 18446744071563137024) hv (by decide)] + rfl + +theorem vmcsIdx_lt (f : BitVec 64) : (vmcsIdx f).toNat < 2048 := by + unfold vmcsIdx + simp only [BitVec.toNat_or] + refine Nat.or_lt_two_pow (n := 11) (Nat.or_lt_two_pow ?_ ?_) ?_ + · have : (f &&& 0xff).toNat ≤ 255 := by rw [BitVec.toNat_and]; exact Nat.and_le_right + rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow]; omega + · have : ((f >>> 10) &&& 3).toNat ≤ 3 := by rw [BitVec.toNat_and]; exact Nat.and_le_right + rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq]; omega + · have : ((f >>> 13) &&& 3).toNat ≤ 3 := by rw [BitVec.toNat_and]; exact Nat.and_le_right + rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq]; omega + +theorem msrSlot_le {m i : BitVec 64} (h : msrSlot m = some i) : i.toNat ≤ 4 := by + unfold msrSlot at h + split_ifs at h <;> cases h <;> decide + +theorem vrelCommon_w : VcpuRelCommon (vmemW (wL.mem ww)) vB.bv vcpuW where + tcb := by + rw [vmemW_vtcb] + show some gT.bv = vcpuTcbRef gT.bv + unfold vcpuTcbRef + rw [if_neg (by decide)] + valid := by rw [vmemW_vcpu VcpuLay.VALID (by decide) (by decide)]; simp [vcpuW, VcpuState.blank] + pending := by rw [vmemW_vcpu VcpuLay.PENDING (by decide) (by decide)]; simp [vcpuW, VcpuState.blank] + mrs i := by + have hi := i.isLt + have e : (exitOff i).toNat = 20992 + 8 * i.val := by + unfold exitOff VcpuLay.EXIT + simp [BitVec.toNat_add, BitVec.toNat_mul]; omega + unfold exitOff at e ⊢ + rw [vmemW_vcpu _ (by rw [e]; omega) (by rw [e]; omega)]; rfl + gpr i := by + have hi := i.isLt + have e : (gprOff i).toNat = 20736 + 8 * i.val := by + unfold gprOff VcpuLay.REGS + simp [BitVec.toNat_add, BitVec.toNat_mul]; omega + rw [vmemW_vcpu _ (by rw [e]; omega) (by rw [e]; omega)]; rfl + xcr0 := by rw [vmemW_vcpu VcpuLay.XCR0 (by decide) (by decide)]; rfl + ibpb := by rw [vmemW_vcpu VcpuLay.VM_IBPB_DEFER (by decide) (by decide)]; rfl + +theorem vrelVmx_w : VcpuRelVmx (vmemW (wL.mem ww)) vB.bv vcpuW where + toVcpuRelCommon := vrelCommon_w + vmcs f _ := by + have h := vmcsIdx_lt f + have e : (vmcsOff f).toNat = 4096 + (vmcsIdx f).toNat * 8 := by + unfold vmcsOff VcpuLay.SHADOW + simp [BitVec.toNat_add, BitVec.toNat_mul]; omega + rw [vmemW_vcpu _ (by rw [e]; omega) (by rw [e]; omega)]; rfl + msr m i hm := by + have h := msrSlot_le hm + have e : (VcpuLay.MSRS + i * 8).toNat = 20864 + i.toNat * 8 := by + unfold VcpuLay.MSRS + simp [BitVec.toNat_add, BitVec.toNat_mul]; omega + rw [vmemW_vcpu _ (by rw [e]; omega) (by rw [e]; omega)]; rfl + cr2 := by rw [vmemW_vcpu VcpuLay.CR2 (by decide) (by decide)]; rfl + io p := by + have h := p.isLt + have e : (vcpuIoOff p).toNat = 24576 + p.toNat / 64 * 8 := by + unfold vcpuIoOff VcpuLay.IO + simp [BitVec.toNat_add, BitVec.toNat_mul]; omega + rw [vmemW_vcpu _ (by rw [e]; omega) (by rw [e]; omega)] + simp [vcpuW, VcpuState.blank] + +theorem vrelSvm_w : VcpuRelSvm (vmemW (wL.mem ww)) vB.bv vcpuW where + toVcpuRelCommon := vrelCommon_w + vmcb off h _ _ := by + have e : off.toNat < 1768 := by + unfold Svm.readable at h + rw [Bool.and_eq_true, decide_eq_true_eq, decide_eq_true_eq] at h + have h2 := BitVec.lt_def.1 h.2 + have e2 : Svm.svmEnd.toNat = 1768 := by decide + omega + rw [vmemW_vcpu off (by omega) (by omega)]; rfl + +/-! ## The VCPU record -/ + +theorem tVcpuA_val_w {t : U64} (h : t.val + 3664 < 2 ^ 64) : (tVcpuA t).val = t.val + 3664 := by + unfold tVcpuA; simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + +theorem vcpu_tcbOff : vcpu.TCB.val = 20480 := by unfold vcpu.TCB; rfl +theorem vcpu_prevOff : vcpu.PREV.val = 20496 := by unfold vcpu.PREV; rfl +theorem vcpu_nextOff : vcpu.NEXT.val = 20488 := by unfold vcpu.NEXT; rfl + +theorem vTcbA_vB : (vTcbA vB).val = 18446744071563137024 := by + unfold vTcbA + rw [wa_val (by rw [vB_val, vcpu_tcbOff]; decide), vB_val, vcpu_tcbOff] + +theorem mem_vTcb : wL.mem ww (vTcbA vB) = gT := + (memw_al vTcbA_vB (by decide)).trans (by decide) + +theorem mem_vPrev : wL.mem ww (vPrevA vB) = 0#u64 := by + have e : (vPrevA vB).val = 18446744071563137040 := by + unfold vPrevA + rw [wa_val (by rw [vB_val, vcpu_prevOff]; decide), vB_val, vcpu_prevOff] + exact memw_vcpu (by rw [e, vB_val]; omega) (by rw [e]; decide) + +theorem mem_vNext : wL.mem ww (P1.vNextA vB) = 0#u64 := by + have e : (P1.vNextA vB).val = 18446744071563137032 := by + unfold P1.vNextA + rw [wa_val (by rw [vB_val, vcpu_nextOff]; decide), vB_val, vcpu_nextOff] + exact memw_vcpu (by rw [e, vB_val]; omega) (by rw [e]; decide) + +theorem mem_vcpu_rT : wL.mem ww (tVcpuA rT) = 0#u64 := by + have e : (tVcpuA rT).val = 18446744071562104400 := by + rw [tVcpuA_val_w (by rw [rT_val]; decide), rT_val] + exact (memw_al e (by decide)).trans (by decide) + +theorem mem_vcpu_gT : wL.mem ww (tVcpuA gT) = vB := by + have e : (tVcpuA gT).val = 18446744071562116688 := by + rw [tVcpuA_val_w (by rw [gT_val]; decide), gT_val] + exact (memw_al e (by decide)).trans (by decide) + +theorem mem_vcpu_idT : wL.mem ww (tVcpuA idT) = 0#u64 := by + have e : (tVcpuA idT).val = 18446744071562075728 := by + rw [tVcpuA_val_w (by rw [idT_val]; decide), idT_val] + exact (memw_al e (by decide)).trans (by decide) + +theorem vHead_val : (P1.vHeadA wst ww).val = 18446744071562133584 := by + unfold P1.vHeadA + rw [ksb_ww]; unfold layout.ks.VCPU_HEAD + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits, fkb_val] + +theorem mem_vHead : wL.mem ww (P1.vHeadA wst ww) = vB := + (memw_al vHead_val (by decide)).trans (by decide) + +theorem thWw_tcb {t : U64} (h : thWw t = some .tcb) : t = idT ∨ t = rT ∨ t = gT := memTw (thT_thWw t h) + +theorem w_extv : JExtVcpu (L := wL) {vB} thWw ww where + cap y hy := by + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] <;> + first | trivial + bnd t ht := by + rcases thWw_tcb ht with rfl | rfl | rfl + · exact Or.inl mem_vcpu_idT + · exact Or.inl mem_vcpu_rT + · exact Or.inr ⟨by rw [mem_vcpu_gT]; exact Set.mem_singleton _, by rw [mem_vcpu_gT, mem_vTcb]⟩ + back v hv := by + rw [Set.mem_singleton_iff] at hv; subst hv + exact Or.inr ⟨by rw [mem_vTcb]; exact thWw_gT, by rw [mem_vTcb, mem_vcpu_gT]⟩ + nz v hv := by + rw [Set.mem_singleton_iff] at hv; subst hv; decide + +/-- A boot object is no VCPU. -/ +theorem finit_not_vcpu {p : ObjRef} {vs : VcpuState} (h : fInitKheap p = some (.archObj (.vcpu vs))) : False := by + unfold fInitKheap at h + simp only [Function.update_apply] at h + split_ifs at h + · rw [initKheap_pd] at h; cases h + · rw [initKheap_pdpt] at h; cases h + · rw [initKheap_pml4] at h; cases h + · rw [initKheap_idleS] at h; cases h + · rw [initKheap_idleT] at h; cases h + · cases h + +theorem σw_vcpu_at {p : ObjRef} {vs : VcpuState} (h : σw.kheap p = some (.archObj (.vcpu vs))) : + p = vB.bv ∧ vs = vcpuW := by + rw [σw_kheap] at h + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨e1, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e; exact ⟨e1, rfl⟩ + · exact (finit_not_vcpu h').elim + +theorem σw_vB : σw.kheap vB.bv = some (.archObj (.vcpu vcpuW)) := by rw [σw_kheap]; exact kh_vB + +theorem w_vo : VOnly {vB} σw := fun _ _ h => Or.inl ⟨vB, Set.mem_singleton _, (σw_vcpu_at h).1⟩ + +theorem w_vr (hk : virtKind = .vmx ∨ virtKind = .svm) : VRel {vB} σw (wL.mem ww) := by + intro v hv vs h + rw [Set.mem_singleton_iff] at hv; subst hv + rw [(σw_vcpu_at h).2] + unfold VcpuRel + rcases hk with e | e <;> rw [e] + exacts [vrelVmx_w, vrelSvm_w] + +theorem w_rngOne : VirtRngOne.RngOne (wL.mem ww) Gw := by + intro y hy y' hy' k k' hb _ + have e : y = y' := by + rcases flatw hy with ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ | ⟨rfl, ey⟩ <;> + rcases flatw hy' with ⟨rfl, ey'⟩ | ⟨rfl, ey'⟩ | ⟨rfl, ey'⟩ | ⟨rfl, ey'⟩ | ⟨rfl, ey'⟩ | ⟨rfl, ey'⟩ | ⟨rfl, ey'⟩ <;> + first + | rfl + | (exfalso; rw [ey] at k hb; rw [ey'] at k' hb + first + | exact absurd k.2.1 (by decide) + | exact absurd k'.2.1 (by decide) + | exact absurd (k.1.symm.trans physF_sctlC) (by decide) + | exact absurd (k'.1.symm.trans physF_sctlC) (by decide) + | (simp only [base_cnC, base_thC, base_scC, base_gthC, base_vcC] at hb; omega)) + subst e + obtain ⟨ch, hch, h⟩ := List.mem_flatten.1 hy + exact ⟨ch, hch, h, h⟩ + +theorem w_rvm : RunVmB (L := wL) thWw ww := by + intro t ht h + rcases st_ww t (thT_thWw t ht) with ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, -⟩ + · rw [e] at h; exact absurd h (by decide) + · rw [e] at h; exact absurd h (by decide) + · rw [mem_vcpu_gT]; decide + +theorem w_jvm : JVm (L := wL) {vB} Hww ww σw where + spec v hv := by + rw [Set.mem_singleton_iff] at hv; subst hv; exact ⟨_, σw_vB⟩ + heap v hv := by + rw [Set.mem_singleton_iff] at hv; subst hv; exact Hww_vB + ks v hv := by + rw [Set.mem_singleton_iff] at hv; subst hv + right; rw [ksb_ww, fkb_val, vB_val]; omega + sep v hv o ko h hne := by + rw [Set.mem_singleton_iff] at hv; subst hv + unfold VcpuApart + rw [vB_val] + rcases obj_ext h with ⟨e, -⟩ | h1 | ⟨-, h1⟩ + · exact absurd e hne + · left; omega + · left; omega + +theorem w_vl : VList wst {vB} thWw ww where + lst := by + refine ⟨[vB], List.nodup_singleton _, fun w => by simp, mem_vHead, ?_⟩ + exact ⟨by decide, mem_vPrev, mem_vNext, trivial⟩ + hd := by + have hv := vHead_val + refine ⟨by show _ % 8 = 0; rw [hv], ?_, ?_⟩ + · rintro (⟨y, hy, e | e⟩ | ⟨t, ht, e⟩ | ⟨v, hvv, e⟩) + · have h := (slot_natw hy 0 (by omega)).1 + rw [← e, hv] at h + unfold SlotNatw SlotNat at h; omega + · obtain ⟨h, h32⟩ := slot_natw hy 8 (by omega) + have e8 : (wordOf y 8#u64).val = y.val + 8 := by + have hb : y.val + 8 < 2 ^ 64 := by + unfold SlotNatw SlotNat at h; omega + unfold wordOf + exact wa_val (a := y) (b := 8#u64) hb + rw [← e, hv] at e8 + omega + · have ht' : t.val + 3664 < 2 ^ 64 ∧ t.val + 3664 ≠ 18446744071562133584 := by + rcases thWw_tcb ht with rfl | rfl | rfl + · rw [idT_val]; omega + · rw [rT_val]; omega + · rw [gT_val]; omega + have h := tVcpuA_val_w ht'.1 + rw [← e, hv] at h + exact ht'.2 h.symm + · rw [Set.mem_singleton_iff] at hvv; subst hvv + have h := vTcbA_vB + rw [← e, hv] at h; omega + · rintro ⟨t, ht, e⟩ + have h : (P1.stA t).val ≠ 18446744071562133584 := by + have k : ∀ u : U64, u.val + 3328 < 2 ^ 64 → (P1.stA u).val = u.val + 3328 := fun u hu => by + rw [P1.stA_eq]; exact wa_val (a := u) (b := 3328#u64) hu + rcases thWw_tcb ht with rfl | rfl | rfl + · rw [k _ (by rw [idT_val]; omega), idT_val]; omega + · rw [k _ (by rw [rT_val]; omega), rT_val]; omega + · rw [k _ (by rw [gT_val]; omega), gT_val]; omega + exact h (by rw [← e, hv]) + +theorem w_vbind : VBind {vB} σw (wL.mem ww) := by + intro v hv + rw [Set.mem_singleton_iff] at hv; subst hv + exact ⟨vcpuW, σw_vB, Or.inr ⟨gT, by decide, rfl, mem_vTcb⟩⟩ + +theorem w_vcap : VirtVCapped.VCappedM {vB} thWw (wL.mem ww) := by + intro v hv + rw [Set.mem_singleton_iff] at hv; subst hv + exact ⟨rC5, isSlot_rC5, capV_rC5⟩ + +theorem w_tcap : TCappedM {vB} thWw (wL.mem ww) := by + intro v hv _ + rw [Set.mem_singleton_iff] at hv; subst hv + exact ⟨rC4, isSlot_rC4, by rw [capV_rC4, mem_vTcb]⟩ + +/-- **`VcpuCore {vB}` at the witness** ([P] the backend). -/ +theorem vcpuCore_ww (hk : virtKind = .vmx ∨ virtKind = .svm) : VcpuCore (L := wL) {vB} Zw Hww ww σw := + ⟨w_extv, w_vo, w_vr hk, w_rngOne, w_rvm, w_jvm, w_vl, w_vbind, w_vcap, w_tcap⟩ + +/-! ## No guest tables -/ + +theorem σw_noGuest : NoGuestTag σw := by + rintro e ⟨hi, p, f, lo, h, -⟩ + simp [σw, σ1, fInitASt, initASt, initArchState] at h + +theorem σw_noEpt (r : ObjRef) (l : Nat) (es) : σw.kheap r ≠ some (.archObj (.ept l es)) := by + intro h + rw [σw_kheap] at h + rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e + · cases e + · cases e + · unfold fInitKheap at h' + simp only [Function.update_apply] at h' + split_ifs at h' + · rw [initKheap_pd] at h'; cases h' + · rw [initKheap_pdpt] at h'; cases h' + · rw [initKheap_pml4] at h'; cases h' + · rw [initKheap_idleS] at h'; cases h' + · rw [initKheap_idleT] at h'; cases h' + · cases h' + +theorem w_jextEpt : JExtEpt (L := wL) ∅ thWw ww := by + have k : ∀ y, IsSlot thWw y → (∀ p l m a v, capV (wL.mem ww) y ≠ .Ept p l m a v) ∧ + (∀ p sz r d a v, capV (wL.mem ww) y ≠ .Frame p sz r d true true a v) := by + intro y hy + rcases slotw_cases hy with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | e <;> rw [e] <;> + exact ⟨fun _ _ _ _ _ h => (by cases h), fun _ _ _ _ _ _ h => (by cases h)⟩ + refine ⟨fun y hy => ?_, ⟨fun y hy p l a v h => ((k y hy).1 _ _ _ _ _ h).elim, + fun y hy p sz r d a v h => ((k y hy).2 _ _ _ _ _ _ h).elim⟩, fun y hy p l a v h => ((k y hy).1 _ _ _ _ _ h).elim⟩ + cases hc : capV (wL.mem ww) y with + | Ept p l m a v => exact ((k y hy).1 _ _ _ _ _ hc).elim + | _ => trivial + +/-- **`EptCore ∅` at the witness.** -/ +theorem eptCore_ww (XL : U64 → Nat) : EptCore (L := wL) ∅ XL thWw ww σw := by + refine eptCore_empty σw_noEpt σw_noGuest w_jextEpt (fun t ht => ?_) (fun hi hi' p h _ => ?_) + · have h : cslotAddr (t.bv, tcbCnodeIndex 5) = slotOf t 5 := by + rcases thWw_tcb ht with rfl | rfl | rfl <;> decide + have hs : IsSlot thWw (slotOf t 5) := ⟨t, 5, Or.inr ⟨ht, by decide⟩, rfl⟩ + rw [h] + have hv : (slotOf t 5).val = 18446744071562072064 + 160 ∨ (slotOf t 5).val = 18446744071562100736 + 160 ∨ + (slotOf t 5).val = 18446744071562113024 + 160 := by + rcases thWw_tcb ht with rfl | rfl | rfl + · exact Or.inl (slotOf_idT 5 (by omega)) + · exact Or.inr (Or.inl (slotOf_rT 5 (by omega))) + · exact Or.inr (Or.inr (slotOf_gT 5 (by omega))) + exact capVw_val hs (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) (by omega) + · simp [σw, σ1, fInitASt, initASt, initArchState] at h + +/-! ## The witness architecture and `VEAnyX` -/ + +/-- The witness architecture: `warI1` (one IOAPIC of 24 pins, as the spec state's), returning the platform's +virtualization code and the platform's VMX capability words (`vcpuCapability`, so `CapAgree` holds). -/ +def warV : machine.Arch Fl4Spec.MachineState := + { warI1 with + virtualization := fun _ => ok ⟨BitVec.ofNat 64 virtKind.code⟩ + vcpu_capability := fun _ g => ok ⟨vcpuCapability g.bv⟩ } + +theorem warV_virt (a : Fl4Spec.MachineState) : ∀ i, warV.virtualization a = ok i → i.val = virtKind.code := by + intro i h + have e := ok_inj h + subst e + show (BitVec.ofNat 64 virtKind.code).toNat = _ + rw [BitVec.toNat_ofNat] + cases virtKind <;> decide + +/-- `JAtZB` at the witness architecture (`kern` reads only `tdx_active` / `dma_ram`; `utc` names no field). -/ +theorem jAtZB_wv (a : MachineState) : + JAtZB (L := wL) warV wAR Zw Hww Csw C1 (fun _ => []) Dw ww a σw := + { jAtZB_ww a with kern := { (jAtZB_ww a).kern with }, utc := fun sl p sb fi dev hs h hd => { (jAtZB_ww a).utc sl p sb fi dev hs h hd with } } + +/-- **The VM-epoch state with guest tables at the witness** ([P] the backend). -/ +theorem vmEptState_ww (hk : virtKind = .vmx ∨ virtKind = .svm) (a : MachineState) : + VmEptState (L := wL) warV {vB} ∅ (fun _ => 0) wAR Zw Hww Csw C1 (fun _ => []) Dw ww a σw := + ⟨⟨jAtZB_wv a, vcpuCore_ww hk⟩, eptCore_ww _⟩ + +/-- **`VEAnyX` at the witness** ([P] the backend). -/ +theorem veAnyX_ww (hk : virtKind = .vmx ∨ virtKind = .svm) (a : MachineState) : + VEAnyX (L := wL) warV wAR ww a σw := + haveI := Zw_vx + (vmEptState_ww hk a).anyX warV + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/VmWitZ.lean b/proofs/Fl4Proofs/P3/VmWitZ.lean new file mode 100644 index 000000000..c56b332b1 --- /dev/null +++ b/proofs/Fl4Proofs/P3/VmWitZ.lean @@ -0,0 +1,377 @@ +/- +P3 (line mmuAD witness): **the VM witness's ghosts** at `ww` / `σw` (P3/VmWitMem) and the slot facts. +Generated by `handoff/tools/gen_witz.py` on the plan of P3/JBootQ1Z. + +* `thWw` (`Z.th`): `thW` plus the root and guest TCBs and the radix-3 root CNode. +* The slot caps: the root TCB's slot 0 the CNode cap of `rC`; the root CNode's slots 0..5 the root thread, SC, + untyped, SchedControl, guest thread and VCPU caps; every other slot null. Every MDB word is zero but the + untyped slot's, which is revocable. +* `Gw` (the MDB ghost): the seven cap slots, each its own chain; no parent. +* `Hww` (P1's heap): `Hw` plus the two TCBs, the root SC, the root CNode and the VCPU (`other 15`). + +No sorry; no premises. New module; no existing statement changed. +-/ +import Fl4Proofs.P3.VmWitMem +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3.JBootQ.VWit +open Fl4Proofs.P3.JBoot +open fl4_core Fl4Spec Fl4Proofs.P1 Fl4Proofs.P3 Fl4Proofs.P3.Sched Fl4Proofs.P2.Wit Classical + +noncomputable section + +set_option maxHeartbeats 1000000 + +theorem gT_val : gT.val = 18446744071562113024 := rfl +theorem vB_val : vB.val = 18446744071563116544 := rfl + +/-! ## The typed heap -/ + +def thWw : TypedHeap := fun p => + if p = rT then some .tcb else if p = gT then some .tcb else if p = rC then some (.cnode 3) else thW p + +theorem thWw_rT : thWw rT = some .tcb := by unfold thWw; rw [if_pos rfl] +theorem thWw_gT : thWw gT = some .tcb := by unfold thWw; rw [if_neg (by decide), if_pos rfl] +theorem thWw_rC : thWw rC = some (.cnode 3) := by + unfold thWw; rw [if_neg (by decide), if_neg (by decide), if_pos rfl] +theorem thWw_old {p : U64} (h1 : p ≠ rT) (h2 : p ≠ gT) (h3 : p ≠ rC) : thWw p = thW p := by + unfold thWw; rw [if_neg h1, if_neg h2, if_neg h3] +theorem thWw_idT : thWw idT = some .tcb := by + rw [thWw_old (by decide) (by decide) (by decide)]; exact thW_idT + +theorem thWw_some {p : U64} {k : ObjKind} (h : thWw p = some k) : + (p = rT ∧ k = .tcb) ∨ (p = gT ∧ k = .tcb) ∨ (p = rC ∧ k = .cnode 3) ∨ + (p ≠ rT ∧ p ≠ gT ∧ p ≠ rC ∧ thW p = some k) := by + by_cases e1 : p = rT + · subst e1; rw [thWw_rT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + by_cases e2 : p = gT + · subst e2; rw [thWw_gT] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + by_cases e3 : p = rC + · subst e3; rw [thWw_rC] at h; cases h; exact Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)) + rw [thWw_old e1 e2 e3] at h + exact Or.inr (Or.inr (Or.inr ⟨e1, e2, e3, h⟩)) + +theorem thWw_pos {p : U64} {k : ObjKind} (h : thWw p = some k) : + (p.val = 18446744071562072064 ∧ k = .tcb) ∨ (p.val = 18446744071562100736 ∧ k = .tcb) ∨ (p.val = 18446744071562113024 ∧ k = .tcb) ∨ + (p.val = 18446744071562108928 ∧ k = .cnode 3) ∨ + (k = .cnode 0 ∧ ((∃ i, i < 128 ∧ p.val = 18446744071562134752 + 32 * i) ∨ + (∃ i, 128 ≤ i ∧ i < 256 ∧ p.val = 18446744071562076160 + 32 * i))) := by + rcases thWw_some h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, -, -, h'⟩ + · exact Or.inr (Or.inl ⟨rfl, rfl⟩) + · exact Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)) + · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩))) + · rcases thW_pos h' with h1 | h1 + · exact Or.inl h1 + · exact Or.inr (Or.inr (Or.inr (Or.inr h1))) + +/-- Case split on `thWw`'s objects, the kind substituted. -/ +macro "thcw" h:term : tactic => + `(tactic| (rcases thWw_pos $h with ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e, e'⟩ | ⟨e', ⟨i, hi, e⟩ | ⟨i, hi, hi2, e⟩⟩ <;> + cases e')) + +theorem placed_thWw : Placed thWw where + align p k h := by thcw h <;> omega + fits p k h := by thcw h <;> simp only [objSize, Nat.reducePow] <;> omega + disj p q kp kq hp hq hne := by + have hv : p.val ≠ q.val := fun e => hne (UScalar.eq_of_val_eq e) + rcases thWw_pos hp with ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨e, rfl⟩ | ⟨rfl, ⟨i, hi, e⟩ | ⟨i, hi, hi2, e⟩⟩ <;> + rcases thWw_pos hq with ⟨e', rfl⟩ | ⟨e', rfl⟩ | ⟨e', rfl⟩ | ⟨e', rfl⟩ | + ⟨rfl, ⟨j, hj, e'⟩ | ⟨j, hj, hj2, e'⟩⟩ <;> + simp only [objSize, Nat.reducePow] <;> omega + high p k h := by thcw h <;> omega + +theorem kTyped_thWw : KTyped thWw σw := by + intro o + refine ⟨fun r cs h _ => ?_, fun t h => ?_⟩ + · rcases kh_some h with ⟨-, e⟩ | ⟨-, e⟩ | ⟨rfl, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | h' + · cases e + · cases e + · cases e; exact thWw_rC + · cases e + · cases e + · obtain ⟨⟨irq, rfl⟩, rfl, -⟩ := fInitKheap_cnode h' + have h1 : (⟨fIrqNode irq⟩ : U64) ≠ rT := fun e => rT_irq irq (congrArg UScalar.bv e) + have h2 : (⟨fIrqNode irq⟩ : U64) ≠ gT := fun e => gT_irq irq (congrArg UScalar.bv e) + have h3 : (⟨fIrqNode irq⟩ : U64) ≠ rC := fun e => rC_irq irq (congrArg UScalar.bv e) + exact (thWw_old h1 h2 h3).trans (thW_irq irq) + · rcases kh_tcb_of h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact thWw_rT + · exact thWw_gT + · rw [← idT_bv]; exact thWw_idT + +theorem thT_thWw : ∀ t, thWw t = some .tcb → t ∈ Tw := by + intro t h + rcases thWw_some h with ⟨rfl, -⟩ | ⟨rfl, -⟩ | ⟨-, e⟩ | ⟨-, -, -, h'⟩ + · exact rT_mem + · exact gT_mem + · cases e + · rw [thT_thW t h']; exact idT_mem + +theorem thk_thWw : ∀ q k, thWw q = some k → k = .tcb ∨ ∃ r, k = .cnode r := by + intro q k h + rcases thWw_some h with ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, rfl⟩ | ⟨-, -, -, h'⟩ + · exact Or.inl rfl + · exact Or.inl rfl + · exact Or.inr ⟨3, rfl⟩ + · exact thk_thW q k h' + +theorem intL_thWw : IntLayout thWw fkb where + fit := fkb_lt + align := fkb_fit.2.2 + st_out i hi := by + have hv := irqStA_val (kb := fkb) fkb_lt hi + rw [fkb_val] at hv + refine ⟨by show (irqStA fkb i).val % 8 = 0; omega, fun p k hp => ?_⟩ + rw [hv] + thcw hp <;> simp only [objSize, Nat.reducePow] <;> omega + node i hi := by + have hv := nodeA_val (kb := fkb) fkb_lt hi + rw [fkb_val] at hv + rw [thWw_old (fun e => by rw [e, rT_val] at hv; omega) (fun e => by rw [e, gT_val] at hv; omega) + (fun e => by rw [e, rC_val] at hv; omega)] + exact intL_thW.node i hi + +/-! ## The slots -/ + +def SlotNatw (n : Nat) : Prop := + SlotNat n ∨ (18446744071562100736 ≤ n ∧ n < 18446744071562100736 + 192) ∨ (18446744071562113024 ≤ n ∧ n < 18446744071562113024 + 192) ∨ (18446744071562108928 ≤ n ∧ n < 18446744071562108928 + 256) + +theorem slot_natw {x : U64} (h : IsSlot thWw x) (k : Nat) (hk : k < 32) : + SlotNatw (x.val + k) ∧ x.val % 32 = 0 := by + obtain ⟨p, i, hs, rfl⟩ := h + rw [IsSlotAt.val placed_thWw hs] + unfold SlotNatw SlotNat + rcases hs with ⟨r, hp, hi⟩ | ⟨hp, hi⟩ + · rcases thWw_pos hp with ⟨-, e⟩ | ⟨-, e⟩ | ⟨-, e⟩ | ⟨hv, e⟩ | ⟨e, ⟨j, hj, hv⟩ | ⟨j, hj, hj2, hv⟩⟩ + · cases e + · cases e + · cases e + · cases e; simp only [Nat.reducePow] at hi; omega + · cases e; rw [Nat.pow_zero] at hi; omega + · cases e; rw [Nat.pow_zero] at hi; omega + · unfold tcbSlots at hi + rcases thWw_pos hp with ⟨hv, -⟩ | ⟨hv, -⟩ | ⟨hv, -⟩ | ⟨-, e⟩ | ⟨e, -⟩ + · omega + · omega + · omega + · cases e + · cases e + +/-- The root CNode's slot 0. -/ +def rC0 : U64 := 18446744071562108928#u64 +/-- The root CNode's slot 1. -/ +def rC1 : U64 := 18446744071562108960#u64 +/-- The root CNode's slot 2. -/ +def rC2 : U64 := 18446744071562108992#u64 +/-- The root CNode's slot 3. -/ +def rC3 : U64 := 18446744071562109024#u64 +/-- The root CNode's slot 4. -/ +def rC4 : U64 := 18446744071562109056#u64 +/-- The root CNode's slot 5. -/ +def rC5 : U64 := 18446744071562109088#u64 + +theorem rC0_val : rC0.val = 18446744071562108928 := rfl +theorem rC1_val : rC1.val = 18446744071562108960 := rfl +theorem rC2_val : rC2.val = 18446744071562108992 := rfl +theorem rC3_val : rC3.val = 18446744071562109024 := rfl +theorem rC4_val : rC4.val = 18446744071562109056 := rfl +theorem rC5_val : rC5.val = 18446744071562109088 := rfl + +abbrev cnC : capability.Cap := .CNode rC 3#u64 0#u64 0#u64 +abbrev thC : capability.Cap := .Thread rT +abbrev scC : capability.Cap := .SchedContext rS 7#u64 +abbrev utC : capability.Cap := .Untyped uB 12#u64 0#u64 false +abbrev sctlC : capability.Cap := .SchedControl 0#u64 +abbrev gthC : capability.Cap := .Thread gT +abbrev vcC : capability.Cap := .Vcpu vB + +/-- **A slot word at offset 8, 16 or 24 is zero**, but the untyped slot's revocable word. -/ +theorem memw_slotw {x : U64} (h : IsSlot thWw x) {k : U64} (hk0 : 8 ≤ k.val) (hk : k.val < 32) (hk8 : k.val % 8 = 0) + (hu : x.val + k.val ≠ 18446744071562109016) : wL.mem ww (wordOf x k) = 0#u64 := by + obtain ⟨h1, ha⟩ := slot_natw h k.val hk + have hv : (wordOf x k).val = x.val + k.val := wordOf_val (by unfold SlotNatw SlotNat at h1; omega) + rw [memw_al hv (by omega)] + unfold SlotNatw SlotNat at h1 + imgwz + +theorem memw_slot8 {x : U64} (h : IsSlot thWw x) {k : U64} (hk : k.val = 8 ∨ k.val = 16) : + wL.mem ww (wordOf x k) = 0#u64 := by + have ha := (slot_natw h 0 (by omega)).2 + exact memw_slotw h (by omega) (by omega) (by omega) (by omega) + +theorem w8_rT : wL.mem ww (wordOf rT 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562100744) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rT : capV (wL.mem ww) rT = cnC := by + unfold capV; rw [w8_rT, memw_al (n := 18446744071562100736) rfl (by norm_num)]; rfl +theorem w8_rC0 : wL.mem ww (wordOf rC0 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562108936) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rC0 : capV (wL.mem ww) rC0 = thC := by + unfold capV; rw [w8_rC0, memw_al (n := 18446744071562108928) rfl (by norm_num)]; rfl +theorem w8_rC1 : wL.mem ww (wordOf rC1 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562108968) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rC1 : capV (wL.mem ww) rC1 = scC := by + unfold capV; rw [w8_rC1, memw_al (n := 18446744071562108960) rfl (by norm_num)]; rfl +theorem w8_rC2 : wL.mem ww (wordOf rC2 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562109000) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rC2 : capV (wL.mem ww) rC2 = utC := by + unfold capV; rw [w8_rC2, memw_al (n := 18446744071562108992) rfl (by norm_num)]; rfl +theorem w8_rC3 : wL.mem ww (wordOf rC3 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562109032) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rC3 : capV (wL.mem ww) rC3 = sctlC := by + unfold capV; rw [w8_rC3, memw_al (n := 18446744071562109024) rfl (by norm_num)]; rfl +theorem w8_rC4 : wL.mem ww (wordOf rC4 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562109064) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rC4 : capV (wL.mem ww) rC4 = gthC := by + unfold capV; rw [w8_rC4, memw_al (n := 18446744071562109056) rfl (by norm_num)]; rfl +theorem w8_rC5 : wL.mem ww (wordOf rC5 8#u64) = 0#u64 := by + rw [memw_al (n := 18446744071562109096) (wordOf_val (by decide)) (by norm_num)]; decide +theorem capV_rC5 : capV (wL.mem ww) rC5 = vcC := by + unfold capV; rw [w8_rC5, memw_al (n := 18446744071562109088) rfl (by norm_num)]; rfl + +/-- **The caps of `thWw`'s slots**: the seven cap slots, every other slot null. -/ +theorem capVw_slot {x : U64} (h : IsSlot thWw x) (h1 : x ≠ rT) (h2 : x ≠ rC0) (h3 : x ≠ rC1) (h4 : x ≠ rC2) (h5 : x ≠ rC3) (h6 : x ≠ rC4) (h7 : x ≠ rC5) : + capV (wL.mem ww) x = .Null := by + have n1 : x.val ≠ 18446744071562100736 := fun e => h1 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + have n2 : x.val ≠ 18446744071562108928 := fun e => h2 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + have n3 : x.val ≠ 18446744071562108960 := fun e => h3 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + have n4 : x.val ≠ 18446744071562108992 := fun e => h4 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + have n5 : x.val ≠ 18446744071562109024 := fun e => h5 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + have n6 : x.val ≠ 18446744071562109056 := fun e => h6 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + have n7 : x.val ≠ 18446744071562109088 := fun e => h7 (UScalar.eq_of_val_eq (by rw [e]; rfl)) + obtain ⟨hs, ha⟩ := slot_natw h 0 (by omega) + rw [Nat.add_zero] at hs + apply capV_zero _ (memw_slot8 h (k := 8#u64) (Or.inl rfl)) + rw [memw_al rfl (by omega)] + unfold SlotNatw SlotNat at hs + imgwz + +theorem isSlot_rT : IsSlot thWw rT := ⟨rT, 0, Or.inr ⟨thWw_rT, by unfold tcbSlots; omega⟩, by decide⟩ +theorem isSlot_rC0 : IsSlot thWw rC0 := ⟨rC, 0, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ +theorem isSlot_rC1 : IsSlot thWw rC1 := ⟨rC, 1, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ +theorem isSlot_rC2 : IsSlot thWw rC2 := ⟨rC, 2, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ +theorem isSlot_rC3 : IsSlot thWw rC3 := ⟨rC, 3, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ +theorem isSlot_rC4 : IsSlot thWw rC4 := ⟨rC, 4, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ +theorem isSlot_rC5 : IsSlot thWw rC5 := ⟨rC, 5, Or.inl ⟨3, thWw_rC, by norm_num⟩, by decide⟩ + +theorem prevOfw {x : U64} (h : IsSlot thWw x) : prevOf (wL.mem ww) x = 0#u64 := + memw_slot8 h (k := 16#u64) (Or.inr rfl) + +/-- The untyped slot's MDB word. -/ +theorem revW_ww : wL.mem ww (wordOf rC2 24#u64) = revW := by + rw [memw_al (n := 18446744071562109016) (wordOf_val (by decide)) (by norm_num)]; decide + +theorem w24_other {x : U64} (h : IsSlot thWw x) (hx : x ≠ rC2) : wL.mem ww (wordOf x 24#u64) = 0#u64 := by + have ha := (slot_natw h 0 (by omega)).2 + refine memw_slotw h (by decide) (by decide) (by decide) ?_ + intro e + have e24 : (24#u64).val = 24 := rfl + exact hx (UScalar.eq_of_val_eq (by rw [rC2_val]; omega)) + +theorem nextOfw {x : U64} (h : IsSlot thWw x) : nextOf (wL.mem ww) x = 0#u64 := by + by_cases e : x = rC2 + · subst e; unfold nextOf; rw [revW_ww]; decide + · unfold nextOf; rw [w24_other h e]; decide + +theorem revOfw {x : U64} (h : IsSlot thWw x) : revOf (wL.mem ww) x = decide (x = rC2) := by + by_cases e : x = rC2 + · subst e; unfold revOf; rw [revW_ww]; decide + · unfold revOf; rw [w24_other h e, decide_eq_false e]; decide + +theorem fbOfw {x : U64} (h : IsSlot thWw x) : fbOf (wL.mem ww) x = false := by + by_cases e : x = rC2 + · subst e; unfold fbOf; rw [revW_ww]; decide + · unfold fbOf; rw [w24_other h e]; decide + +/-- The non-null slots of `thWw` are the seven cap slots. -/ +theorem slotw_cases {x : U64} (h : IsSlot thWw x) : + (x = rT ∧ capV (wL.mem ww) x = cnC) ∨ (x = rC0 ∧ capV (wL.mem ww) x = thC) ∨ (x = rC1 ∧ capV (wL.mem ww) x = scC) ∨ (x = rC2 ∧ capV (wL.mem ww) x = utC) ∨ (x = rC3 ∧ capV (wL.mem ww) x = sctlC) ∨ (x = rC4 ∧ capV (wL.mem ww) x = gthC) ∨ (x = rC5 ∧ capV (wL.mem ww) x = vcC) ∨ + capV (wL.mem ww) x = .Null := by + by_cases e1 : x = rT + · subst e1; exact Or.inl ⟨rfl, capV_rT⟩ + by_cases e2 : x = rC0 + · subst e2; exact Or.inr (Or.inl ⟨rfl, capV_rC0⟩) + by_cases e3 : x = rC1 + · subst e3; exact Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC1⟩)) + by_cases e4 : x = rC2 + · subst e4; exact Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC2⟩))) + by_cases e5 : x = rC3 + · subst e5; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC3⟩)))) + by_cases e6 : x = rC4 + · subst e6; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC4⟩))))) + by_cases e7 : x = rC5 + · subst e7; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, capV_rC5⟩)))))) + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (Or.inr (capVw_slot h e1 e2 e3 e4 e5 e6 e7))))))) + +/-! ## The other ghosts -/ + +/-- **The MDB ghost**: each cap slot its own chain; no parent. -/ +def Gw : MdbGhost := ⟨[[rT], [rC0], [rC1], [rC2], [rC3], [rC4], [rC5]], fun _ => none⟩ + +theorem Gw_flat {x : U64} : x ∈ Gw.chains.flatten ↔ + x = rT ∨ x = rC0 ∨ x = rC1 ∨ x = rC2 ∨ x = rC3 ∨ x = rC4 ∨ x = rC5 := by + simp [Gw] + +/-- **P1's heap**. -/ +def Hww : Heap := fun p => + if p = rT then some .tcb else if p = gT then some .tcb else if p = rS then some (.sc 7) + else if p = rC then some (.other 8) else if p = vB then some (.other 15) else Hw p + +theorem Hww_rT : Hww rT = some .tcb := by unfold Hww; rw [if_pos rfl] +theorem Hww_gT : Hww gT = some .tcb := by unfold Hww; rw [if_neg (by decide), if_pos rfl] +theorem Hww_rS : Hww rS = some (.sc 7) := by + unfold Hww; rw [if_neg (by decide), if_neg (by decide), if_pos rfl] +theorem Hww_rC : Hww rC = some (.other 8) := by + unfold Hww; rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_pos rfl] +theorem Hww_vB : Hww vB = some (.other 15) := by + unfold Hww; rw [if_neg (by decide), if_neg (by decide), if_neg (by decide), if_neg (by decide), if_pos rfl] +theorem Hww_old {p : U64} (h1 : p ≠ rT) (h2 : p ≠ gT) (h3 : p ≠ rS) (h4 : p ≠ rC) (h5 : p ≠ vB) : Hww p = Hw p := by + unfold Hww; rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5] +theorem Hww_idT : Hww idT = some .tcb := by + rw [Hww_old (by decide) (by decide) (by decide) (by decide) (by decide)]; exact Hw_idT +theorem Hww_idS : Hww idS = some (.sc 7) := by + rw [Hww_old (by decide) (by decide) (by decide) (by decide) (by decide)]; exact Hw_idS + +theorem Hww_some {p : U64} {k : HKind} (h : Hww p = some k) : + (p = rT ∧ k = .tcb) ∨ (p = gT ∧ k = .tcb) ∨ (p = rS ∧ k = .sc 7) ∨ (p = rC ∧ k = .other 8) ∨ + (p = vB ∧ k = .other 15) ∨ (p ≠ rT ∧ p ≠ gT ∧ p ≠ rS ∧ p ≠ rC ∧ p ≠ vB ∧ Hw p = some k) := by + by_cases e1 : p = rT + · subst e1; rw [Hww_rT] at h; cases h; exact Or.inl ⟨rfl, rfl⟩ + by_cases e2 : p = gT + · subst e2; rw [Hww_gT] at h; cases h; exact Or.inr (Or.inl ⟨rfl, rfl⟩) + by_cases e3 : p = rS + · subst e3; rw [Hww_rS] at h; cases h; exact Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)) + by_cases e4 : p = rC + · subst e4; rw [Hww_rC] at h; cases h; exact Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩))) + by_cases e5 : p = vB + · subst e5; rw [Hww_vB] at h; cases h; exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)))) + rw [Hww_old e1 e2 e3 e4 e5] at h + exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨e1, e2, e3, e4, e5, h⟩)))) + +theorem Hww_pos {p : U64} {k : HKind} (h : Hww p = some k) : + (p.val = 18446744071562100736 ∧ k = .tcb) ∨ (p.val = 18446744071562113024 ∧ k = .tcb) ∨ (p.val = 18446744071562104832 ∧ k = .sc 7) ∨ + (p.val = 18446744071562108928 ∧ k = .other 8) ∨ (p.val = 18446744071563116544 ∧ k = .other 15) ∨ + (p.val = 18446744071562072064 ∧ k = .tcb) ∨ (p.val = 18446744071562096640 ∧ k = .sc 7) ∨ + (k = .other 12 ∧ (p.val = 18446744071562084352 ∨ p.val = 18446744071562088448 ∨ + p.val = 18446744071562092544)) ∨ + (k = .other 5 ∧ 18446744071562080256 ≤ p.val ∧ p.val < 18446744071562084352 ∧ p.val % 32 = 0) ∨ + (k = .other 5 ∧ 18446744071562133504 + 1248 ≤ p.val ∧ p.val < 18446744071562133504 + 5344 ∧ + (p.val - 18446744071562133504) % 32 = 0) ∨ + (k = .other 3 ∧ 18446744071562133504 ≤ p.val ∧ p.val < 18446744071562133504 + 13568 ∧ + (p.val - 18446744071562133504) % 8 = 0 ∧ + ¬ (18446744071562133504 + 1248 ≤ p.val ∧ p.val < 18446744071562133504 + 5344)) := by + rcases Hww_some h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨-, -, -, -, -, h'⟩ + · 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 (Or.inl ⟨rfl, rfl⟩))) + · exact Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)))) + · right; right; right; right; right; exact Hw_pos h' + +/-- **The witness's ghost sets**. -/ +def Zw : XSets := ⟨thWw, Gw, ∅, ∅, ∅, ∅, ∅, ∅, Tw, C1, ∅, ∅, ∅, 0#u64, ∅⟩ + +theorem Zw_vx : VxE Zw := ⟨rfl⟩ + +end + +end Fl4Proofs.P3.JBootQ.VWit diff --git a/proofs/Fl4Proofs/P3/XTopJointFbDelFrame.lean b/proofs/Fl4Proofs/P3/XTopJointFbDelFrame.lean index d520cc014..b51ab7aef 100644 --- a/proofs/Fl4Proofs/P3/XTopJointFbDelFrame.lean +++ b/proofs/Fl4Proofs/P3/XTopJointFbDelFrame.lean @@ -56,12 +56,10 @@ theorem cdt_delete_frame_ok (hT : P0.ArchTotal arI) {s : S} {a : A} {x : obj.Slo WP.spec_mono (MKD.cdt_delete_frame (AR := fun (_ : A) (_ : Unit) => True) (c₀ := ()) L.total hT (mrKeepD_true hT) hc hf trivial) fun _ h => h.2 -/-- **`FbFootK` from the relations at both states**: every TCB's registers and every mapped -IPC-buffer word are read off the spec's state by `JAtZ` (`EpStateRel`'s `regs` and `buf`), so a -step keeping the TCBs' arch contexts (`FbKeep`'s `CapsOnly`) and the underlying memory keeps them. -/ -theorem FbFootK.of_rel {Z1 Z2 : XSets} {H1 H2 : P1.Heap} {Cs1 Cs2 Ac1 Ac2 : Set U64} +/-- Equal registers and buffer words follow from the shared state relation and the batch frame. -/ +theorem FbFootK.of_relB {Z1 Z2 : XSets} {H1 H2 : P1.Heap} {Cs1 Cs2 Ac1 Ac2 : Set U64} {Lst1 Lst2 : U64 → List U64} {D1 D2 : Set U64} {s s' : S} {a a' : A} {τ τ' : Fl4Spec.State Unit} - (J1 : JAtZ (L := L) arI AR Z1 H1 Cs1 Ac1 Lst1 D1 s a τ) (J2 : JAtZ (L := L) arI AR Z2 H2 Cs2 Ac2 Lst2 D2 s' a' τ') + (J1 : JAtZB (L := L) arI AR Z1 H1 Cs1 Ac1 Lst1 D1 s a τ) (J2 : JAtZB (L := L) arI AR Z2 H2 Cs2 Ac2 Lst2 D2 s' a' τ') (hT : Z2.T = Z1.T) (hB : Z2.B = Z1.B) (hK : FbKeep τ τ') (hum : τ'.machineState.underlyingMemory = τ.machineState.underlyingMemory) : FbFootK (L := L) Z1 s s' := by have hR1 := (J1.xtop.irelK J1.ks J1.buf).1.toE @@ -80,6 +78,16 @@ theorem FbFootK.of_rel {Z1 Z2 : XSets} {H1 H2 : P1.Heap} {Cs1 Cs2 Ac1 Ac2 : Set apply P1.eq_of_bv rw [b1, b2, hum] +/-- **`FbFootK` from the relations at both states**: every TCB's registers and every mapped +IPC-buffer word are read off the spec's state by `JAtZ` (`EpStateRel`'s `regs` and `buf`), so a +step keeping the TCBs' arch contexts (`FbKeep`'s `CapsOnly`) and the underlying memory keeps them. -/ +theorem FbFootK.of_rel {Z1 Z2 : XSets} {H1 H2 : P1.Heap} {Cs1 Cs2 Ac1 Ac2 : Set U64} + {Lst1 Lst2 : U64 → List U64} {D1 D2 : Set U64} {s s' : S} {a a' : A} {τ τ' : Fl4Spec.State Unit} + (J1 : JAtZ (L := L) arI AR Z1 H1 Cs1 Ac1 Lst1 D1 s a τ) (J2 : JAtZ (L := L) arI AR Z2 H2 Cs2 Ac2 Lst2 D2 s' a' τ') + (hT : Z2.T = Z1.T) (hB : Z2.B = Z1.B) (hK : FbKeep τ τ') + (hum : τ'.machineState.underlyingMemory = τ.machineState.underlyingMemory) : FbFootK (L := L) Z1 s s' := + FbFootK.of_relB arI J1.toB J2.toB hT hB hK hum + set_option maxHeartbeats 8000000 in /-- **The delete of a frame-cap slot at `J`** (module docstring). -/ theorem JAtZ.fbDelFrameJL [Fl4Proofs.P1.PlainStore st] {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} diff --git a/proofs/Fl4Proofs/P3/XTopJointFbRemap.lean b/proofs/Fl4Proofs/P3/XTopJointFbRemap.lean index cb54f1694..9a467a521 100644 --- a/proofs/Fl4Proofs/P3/XTopJointFbRemap.lean +++ b/proofs/Fl4Proofs/P3/XTopJointFbRemap.lean @@ -55,7 +55,7 @@ theorem fbEntry_remap {b : Fl4FrameBatchInvocation} (h : b.op = .remap) (w : Dat let slot ← fbCode <| lookupSourceSlot b.cnode (dataToCptr w.1) b.depth fbCode <| fbMap b slot w.2.1 NonDetE.returnOk false) := by - unfold fbEntry; rw [h] + unfold fbEntry fbEntryWith fbDeferDelete; rw [h] theorem fbCode_err {α : Type} {m : SEMonad Unit α} {σ : State Unit} {e : SyscallError} (h : ExceptT.run m σ = ({(.error e, σ)}, False)) : diff --git a/proofs/Fl4Proofs/P3/XTopJointGrant.lean b/proofs/Fl4Proofs/P3/XTopJointGrant.lean index dd1ac729b..f834d863f 100644 --- a/proofs/Fl4Proofs/P3/XTopJointGrant.lean +++ b/proofs/Fl4Proofs/P3/XTopJointGrant.lean @@ -265,7 +265,7 @@ set_option maxHeartbeats 16000000 in /-- **`cdt::insert` of a derived cap between two CNode slots at `J`** (module docstring): the spec's `cap_insert`, `KeepCV`, FL4's slots after (`insert_trace`), and `JAtZ` after at a new MDB ghost. -/ -theorem JAtZ.insDJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) +theorem JAtZ.insDJX (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {ps pd : U64} {rs rd : Nat} (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) {idxs idxd : List Bool} (hls : idxs.length = rs) (hld : idxd.length = rd) {srcC : capability.Cap} (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok srcC) @@ -286,7 +286,10 @@ theorem JAtZ.insDJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) capV (L.mem s') y = capV (L.mem s) y) ∧ capV (L.mem s') (cslotAddr (pd.bv, idxd)) = cc ∧ capV (L.mem s') (cslotAddr (ps.bv, idxs)) = (fullF srcC cc).getD srcC ∧ - ∃ G' : MdbGhost, JAtZ (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D s' a σ' ⦄ := by + ∃ G' : MdbGhost, JAtZ (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D s' a σ' ∧ + MdbStep (L.mem s) Z.G (L.mem s') G' ∧ P1.WritesIn st s s' (Sched.SlotWd Z.th) ∧ + (∀ n, n < tcbSlots → ∀ t, Z.th t = some .tcb → + capAt (L.mem s') (slotOf t n) = capAt (L.mem s) (slotOf t n)) ⦄ := by have hX := J.xtop obtain ⟨hPl, ⟨hSt, hV, hM, hC, hI⟩, hFI, -, hMO, -, -⟩ := hX.rel have hAs : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ @@ -406,14 +409,41 @@ theorem JAtZ.insDJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) · subst eS exact ⟨_, hAd.isSlot, by rw [hdc]; exact hcnn, by rw [hdc, hsv]; exact hsh⟩ · exact ⟨y0, hy0, by rw [hoth y0 hy0 eS eD]; exact hn, by rw [hoth y0 hy0 eS eD]⟩) - exact ⟨σ', hci, hcn.keepCV hkc, hcn, hw.1, hoth, hdc, hsc, G', + refine ⟨σ', hci, hcn.keepCV hkc, hcn, hw.1, hoth, hdc, hsc, G', ?_, hstep huc, hw', hTS⟩ + exact J.cs_frameV (hvn := J.vsShr hX1' hw.1 (Vs.vsKeep_insert hAs.isSlot hoth hdc (by rw [hsv]; exact hsc) (by rw [hdv]; rfl) (by rw [hsv]; exact hkr) hnt) (Vs.ArchShr.of_objCn hcn.2 (by have e := congrArg Fl4Spec.AbstractState.archState hcn.1; exact e))) hw.1 hX1' (KBack.of_keepCT hkcT) (J.bfK (J.slot2Fr hkcT hX1'.rel.2.1.1 fun q hq => Or.inl (hTS 2 (by decide) q hq)) hvT) hJC R (J.vsc.step hvT) (J.ext.step hvT (J.vkeep_slot hw)) hLv' (fun p hp => J.zD p (ZTcb.of_zombies hZf hp)) (J.carry.of_objCn hkcT) - (devFrom_kept J.mdbL hM' J.ut hCU.2) (J.carry.vsStep rfl hvT subset_rfl subset_rfl subset_rfl subset_rfl rfl hw.1 (ArchFwd.of_objCn hkcT)) (ArchFwd.of_objCn hkcT)⟩ + (devFrom_kept J.mdbL hM' J.ut hCU.2) (J.carry.vsStep rfl hvT subset_rfl subset_rfl subset_rfl subset_rfl rfl hw.1 (ArchFwd.of_objCn hkcT)) (ArchFwd.of_objCn hkcT) + +/-- `JAtZ.insDJX` without the step data (the statement of the original `JAtZ.insDJ`). -/ +theorem JAtZ.insDJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + {ps pd : U64} {rs rd : Nat} (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) + {idxs idxd : List Bool} (hls : idxs.length = rs) (hld : idxd.length = rd) + {srcC : capability.Cap} (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok srcC) + (hsnn : srcC ≠ .Null) + (hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + (cc : capability.Cap) (ac : Fl4Spec.Cap) (hca : CapRel cc ac) (hcv : CapValid Z.th cc) (he : EncWf cc) + (hcnn : cc ≠ .Null) (hsh : shape cc = shape srcC) (hbd : badgeF cc = badgeF srcC ∨ badgeF srcC = 0#u64) + (hut : isUt srcC = true → ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (ps.bv, idxs))) (hzb : isZb srcC = false) + (hkey : okey cc = okey srcC) + (hv : ∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 srcC → Vs.VCapOK P L4 L3 L2 L1 cc) + (htu : Vs.TabUn cc → Vs.TabUn srcC) (hkr : Vs.Keyed2 cc srcC → cc = srcC) (hnt : ¬ Vs.TabUn cc) + (huc : isUt srcC = true → cc = srcC) (hnat : Vs.NativeCap srcC → Vs.NativeCap cc) + (hDs : DestCap Z.th (L.mem s) ps) (hDd : DestCap Z.th (L.mem s) pd) : + cdt.insert st s cc (cslotAddr (ps.bv, idxs)) (cslotAddr (pd.bv, idxd)) ⦃ s' => ∃ σ', + Fl4Spec.capInsert (Z := Unit) ac (ps.bv, idxs) (pd.bv, idxd) σ = ({((), σ')}, False) ∧ + KeepCV σ σ' ∧ Ipc.CnK σ σ' ∧ L.ksb s' = L.ksb s ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr (ps.bv, idxs) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) ∧ + capV (L.mem s') (cslotAddr (pd.bv, idxd)) = cc ∧ + capV (L.mem s') (cslotAddr (ps.bv, idxs)) = (fullF srcC cc).getD srcC ∧ + ∃ G' : MdbGhost, JAtZ (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D s' a σ' ⦄ := + WP.spec_mono (J.insDJX hps hpd hls hld hsrcC hsnn hdst cc ac hca hcv he hcnn hsh hbd hut hzb hkey hv htu hkr hnt + huc hnat hDs hDd) fun _ ⟨σ', h1, h2, h3, h4, h5, h6, h7, G', J'⟩ => ⟨σ', h1, h2, h3, h4, h5, h6, h7, G', J'.1⟩ /-! ## The transfer loop at `J` -/ diff --git a/proofs/Fl4Proofs/P3/XTopJointRecvSigBlock.lean b/proofs/Fl4Proofs/P3/XTopJointRecvSigBlock.lean index 01d3fe977..9c7d5bf72 100644 --- a/proofs/Fl4Proofs/P3/XTopJointRecvSigBlock.lean +++ b/proofs/Fl4Proofs/P3/XTopJointRecvSigBlock.lean @@ -2,8 +2,8 @@ P3 (DESIGN.md §9), IPC: **`receive_signal`'s blocking receive at `J`** (KENTRY-PLAN.md item 5, the former `JRecvSigBlockHyp` of XTopJointRecvSig): the current thread, runnable, resumed and in no ready queue, blocks receiving on -an idle or waiting notification (`JAtZ.recvSigBlockJ`), from `JAtZ` before to -`JAtZ` after. The notification analogue of XTopJointSendBlock's +an idle or waiting notification (`JAtZB.recvSigBlockB`), with the shared joint relation and native footprint. +`JAtZ.recvSigBlockJ` gives the same receive at the native relation. The notification analogue of XTopJointSendBlock's `JAtZ.sendBlockJ`. * FL4's side: `receive_signal_blockF`, IpcNtfnBlock's `receive_signal_blockI` @@ -26,6 +26,7 @@ an idle or waiting notification (`JAtZ.recvSigBlockJ`), from `JAtZ` before to * The blocking receive that gives the SC back, `jRecvSigHyp_of` and `dispatch_recvJS` are in XTopJointRecvSigRet. -/ +import Fl4Proofs.P3.NatFoot import Fl4Proofs.P3.XTopJointRecvSig import Fl4Proofs.P3.XTopJointSendBlock open Aeneas Aeneas.Std Result @@ -703,7 +704,7 @@ variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Se /-- `RBW` misses core 0's debug word: no TCB or notification word, and the action word is another node word (`JKern.dwNe`). -/ -theorem JAtZ.rbw_dbg (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} +theorem JAtZB.rbw_dbg (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hW : P1.WritesIn st s s' (RBW st Z.N Z.T s)) : L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := by have hT := J.xtop.fin.1.1.1.placed.tcb @@ -728,7 +729,7 @@ theorem JAtZ.rbw_dbg (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} /-- **`RBW` keeps every debug word** (KENTRY-PLAN g2i; `rbw_dbg`'s `DbgKeep`): a notification step's word (`nwrite_notDbg`) or the current node's action word (apart from the TCBs, `JKern.hnd`; another offset than the debug head). -/ -theorem JAtZ.rbw_dbgKeep (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} +theorem JAtZB.rbw_dbgKeep (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} (hW : P1.WritesIn st s s' (RBW st Z.N Z.T s)) : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := dbgKeep_of_writes J.blk hW fun b hb => by rcases hb with hb | rfl @@ -748,12 +749,40 @@ theorem JAtZ.rbw_dbgKeep (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : rw [hv, hnb, hnb] omega +/-- The native instance of the core-zero debug-word frame. -/ +theorem JAtZ.rbw_dbg (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hW : P1.WritesIn st s s' (RBW st Z.N Z.T s)) : + L.mem s' (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) = L.mem s (L.nodeAt s 0#u64 layout.node.DEBUG_TCBS) := + J.toB.rbw_dbg hW + +/-- The native instance of the debug-word frame. -/ +theorem JAtZ.rbw_dbgKeep (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {s' : S} + (hW : P1.WritesIn st s s' (RBW st Z.N Z.T s)) : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := + J.toB.rbw_dbgKeep hW + +/-- Removing the notification from the dead set keeps every TCB zombie in the dead set. -/ +theorem JAtZB.zD_diffN (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n : U64} (hn : n ∈ Z.N) : + ∀ p ∈ ZTcb (L := L) Z.th s, p ∈ D \ {n} := fun p hp => + ⟨J.zD p hp, fun e => by + have h : H p = some .tcb := J.zH.1 p (J.thT p (J.ztcb_tcb hp)) + rw [Set.mem_singleton_iff.1 e, J.zH.2.2.2 n hn] at h + cases h⟩ + +/-- The blocking receive touches only the notification and the thread that enters its wait queue. -/ +theorem _root_.Fl4Proofs.P3.Sched.BFrN.natK {t n : Fl4Spec.ObjRef} {σ σ' : Fl4Spec.State Unit} + (F : BFrN t n σ σ') : NatK σ σ' := + natK_kh (R := BChgN t n) (by + intro p o o' h + rcases h with ⟨x, m, rfl, rfl, -, -⟩ | ⟨x, y, rfl, rfl, -, -, -⟩ + · exact zvChg_tcb fun h => by cases h + · exact zvChg_ntfn) F.kh + set_option maxHeartbeats 16000000 in /-- **The blocking receive at `J`** (module docstring): the current thread `t`, resumed and in no ready queue, blocks receiving on the idle or waiting notification `n`, covered by a cap (`hcov`), whose SC is not the current SC (`hnr`; otherwise `maybe_return_sched_context` gives it back). -/ -theorem JAtZ.recvSigBlockJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] +theorem JAtZB.recvSigBlockB (hAT : P0.ArchTotal arI) (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) @@ -763,7 +792,8 @@ theorem JAtZ.recvSigBlockJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : notification.receive_signal st arI s a t n true ⦃ r => ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, - RJ (L := L) arI AR r.1 r.2 z.2 ⦄ := by + JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) r.1 r.2 z.2 ∧ + NatFoot (L := L) Z s σ r.1 z.2 ⦄ := by have hI := J.xtop.irelK J.ks J.buf have hS := J.xtop.fin.1.1.1 have hC : CurInv σ := J.curInv (by rw [hni.2]; intro e; cases e) @@ -786,7 +816,7 @@ theorem JAtZ.recvSigBlockJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z obtain ⟨σ', heq, F, hik, -, hcn, hsrt⟩ := recvSigBlock_eq hkt hk hna hnqN hq hr (J.isorted.2 _ _ hk) hnr' cb rt have hcn' : σ'.schedulerAction = .chooseNewThread := hcn hcur.symm hres -- P1's `BlkOK` after - have hcb := J.canBlockCur ht hcur hkt hr hni hnq + have hcb := (J.faultEntryBlock hni ht hcur hkt hr (by rw [← hcur]; exact hnq)).1 obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk have hx := hI.1.toN.at_ntfn hn hk have hst0 : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by @@ -802,8 +832,9 @@ theorem JAtZ.recvSigBlockJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z · rw [h, ntfn_consts.2.1]; decide have p1 := P1.notification.receive_signal.ipc hAT s a t n true Q0 R0 h0 hB0 hX0 hS0 hSg0 hcb (J.zH.2.2.2 n hn) (J.zH.2.2.2 n hn) h3 + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted refine WP.spec_mono (WP.spec_and (receive_signal_blockF (ar := arI) J.ip s a σ hI ht hkt hnn hqE hn hk hna - (J.qsortedN n hn) hnr' cb rt) p1) ?_ + (hsort n (Or.inr hn)) hnr' cb rt) p1) ?_ rintro ⟨s', a'⟩ ⟨⟨ea, σ0, h0eq, hIR, hRB⟩, hbk⟩ simp only [WP.uncurry'_pair] at ea hbk hIR hRB subst a' @@ -892,13 +923,32 @@ theorem JAtZ.recvSigBlockJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ - have hC0 := JCs.same J hF.1 F.ksz + have hC0 := JCs.sameB J hF.1 F.ksz have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ - have J3 := J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch - (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R - (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) + have J3 : JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s' a σ' := + J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R + (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) rw [heq] - exact ⟨_, Set.mem_singleton _, J3.xtop.relTop, _, _, _, _, _, _, J3, inferInstance⟩ + exact ⟨_, Set.mem_singleton _, J3, NatFoot.ofWS hF.1 hW + (fun b hb => Or.inl (Ipc.CnW.oobj hS hb)) (fun b hb => J.cnW_nv hb) + (fun b hb => J.cnW_nh hb) F.natK⟩ + +/-- The blocking notification receive at the native relation. -/ +theorem JAtZ.recvSigBlockJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) [VxE Z] + {t : U64} (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) (hnr : x.ntfnSc ≠ some σ.curSc) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + notification.receive_signal st arI s a t n true ⦃ r => + ∃ z ∈ (Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ).1, + RJ (L := L) arI AR r.1 r.2 z.2 ⦄ := + WP.spec_mono (J.toB.recvSigBlockB hAT ht hcur hni hnq hn hk hna hnr hcov cb rt) fun _ ⟨z, hz, B', F⟩ => by + have J' := B'.toJN J F + exact ⟨z, hz, J'.xtop.relTop, _, _, _, _, _, _, J', inferInstance⟩ end diff --git a/proofs/Fl4Proofs/P3/XTopJointRecvSigRet.lean b/proofs/Fl4Proofs/P3/XTopJointRecvSigRet.lean index c677c50c7..9bd59ecba 100644 --- a/proofs/Fl4Proofs/P3/XTopJointRecvSigRet.lean +++ b/proofs/Fl4Proofs/P3/XTopJointRecvSigRet.lean @@ -27,6 +27,7 @@ server waiting again on the notification that lent it its SC). `maybeReturnJ`; `jRecvSigHyp_of` (every case, no premise) and `dispatch_recvJS` (moved here from XTopJointRecvSigBlock). -/ +import Fl4Proofs.P3.NatFoot import Fl4Proofs.P3.XTopJointRecvSigBlock import Fl4Proofs.P3.XTopJointFinThreadSc open Aeneas Aeneas.Std Result @@ -724,7 +725,7 @@ set_option maxHeartbeats 8000000 in `t` in no queue (spec `hnq`, FL4 `hout`: not runnable, or the running current thread of a receive, XTopJointRecvIpcRet), neither the idle thread nor the switch target, whose SC `i` is the notification `n`'s. -/ -theorem JAtZ.maybeReturnJQ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n i : U64} {x : Fl4Spec.Tcb} +theorem JAtZB.maybeReturnBQ (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n i : U64} {x : Fl4Spec.Tcb} {xn : Fl4Spec.Notification} (ht : t ∈ Z.T) (hn : n ∈ Z.N) (hiC : i ∈ Z.C) (hk : σ.kheap t.bv = some (.tcb x)) (hkn : σ.kheap n.bv = some (.notification xn)) (hsp : x.tcbSchedContext = some i.bv) (hxn : xn.ntfnSc = some i.bv) (hnq : NotQueued σ t.bv) @@ -734,7 +735,7 @@ theorem JAtZ.maybeReturnJQ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {t notification.maybe_return_sched_context st s n t ⦃ s' => P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ) ∧ ∃ σ', Fl4Spec.maybeReturnSc (Z := Unit) n.bv t.bv σ = ({((), σ')}, False) ∧ - ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JAtZ (L := L) arI AR Z H Cs Ac' Lst' D s' a σ' ⦄ := by + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JAtZB (L := L) arI AR Z H Cs Ac' Lst' D s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' ⦄ := by have hX := J.xtop have hS := hX.fin.1.1.1 have hR := hX.fin.1.1.2.2.1 @@ -783,11 +784,48 @@ theorem JAtZ.maybeReturnJQ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {t ((((pres_maybeReturnSc (true, true) _ _).of_eq he).ykeep rfl).dk q.bv) (fun h => h) (J.jcov.dead q hq) have hDbg := J.kw_dbg hfoot have hKd := J.kw_dbgKeep hfoot - exact ⟨hW, σ2, he, Ac, Lst', J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) - (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) - (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) hKd subset_rfl) - (J.sc_kern hW hK hF hX' hDbg fun _ h => h) - (J.sc_utc hW hK hDbg) hN' hI' hcur' hown' hoff' hact' hB' hcr' hblk hKd (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 J.zD⟩ + have B2 : JAtZB (L := L) arI AR Z H Cs Ac Lst' D s' a σ2 := + J.sc_frame hW hK hX' hCR' hRF' hY' hM' (J.sc_buf hW hK) hRP' (J.sc_apart hW hK) + (J.sc_jpl hW hK (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).2) + (J.sc_jcov hW hdead (fun q hq hn => absurd hq hn) (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2) hKd subset_rfl) + (J.sc_kern hW hK hF hX' hDbg fun _ h => h) + (J.sc_utc hW hK hDbg) hN' hI' hcur' hown' hoff' hact' hB' hcr' hblk hKd (J.idleW_keep hX'.fin.1.1.1 hF.2.1 hF.2.2).1 J.zD + have hO (b : U64) (h : Sched.TFoot st Z.T Z.N Z.C Z.c s σ b) : + Vs.OObj st Z.N Z.T Z.C Z.E Z.R Z.B Z.c s σ b := Sched.tfoot_oobj hS h + exact ⟨hW, σ2, he, Ac, Lst', B2, NatFoot.ofWS (J.xtop.slotsSame_of hW hO) hW + (fun b hb => Or.inl (hO b hb)) (fun _ hb => tfoot_nv hS hb) + (fun _ hb => J.tfoot_nh hb) (natK_tk hK)⟩ + +/-- Native stable-ghost return with its concrete footprint. -/ +theorem JAtZ.maybeReturnJQ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n i : U64} {x : Fl4Spec.Tcb} + {xn : Fl4Spec.Notification} (ht : t ∈ Z.T) (hn : n ∈ Z.N) (hiC : i ∈ Z.C) + (hk : σ.kheap t.bv = some (.tcb x)) (hkn : σ.kheap n.bv = some (.notification xn)) + (hsp : x.tcbSchedContext = some i.bv) (hxn : xn.ntfnSc = some i.bv) (hnq : NotQueued σ t.bv) + (hout : (L.mem s (P1.inqA t) &&& sched.Q_QUEUED) = 0#u64 ∧ (L.mem s (P1.inqA t) &&& sched.Q_RELEASE) = 0#u64 ∧ + L.actionW s ≠ t) + (hsa : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t.bv) (hid : t.bv ≠ σ.idleThread) : + notification.maybe_return_sched_context st s n t ⦃ s' => + P1.WritesIn st s s' (Sched.TFoot st Z.T Z.N Z.C Z.c s σ) ∧ ∃ σ', + Fl4Spec.maybeReturnSc (Z := Unit) n.bv t.bv σ = ({((), σ')}, False) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), JAtZ (L := L) arI AR Z H Cs Ac' Lst' D s' a σ' ⦄ := + WP.spec_mono (J.toB.maybeReturnBQ ht hn hiC hk hkn hsp hxn hnq hout hsa hid) + fun _ ⟨hW, σ', he, Ac', Lst', B', F⟩ => ⟨hW, σ', he, Ac', Lst', B'.toJN J F⟩ + +/-- Return the matching scheduling context of a blocked receiver at the shared joint relation. -/ +theorem JAtZB.maybeReturnB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n i : U64} {x : Fl4Spec.Tcb} + {xn : Fl4Spec.Notification} (ht : t ∈ Z.T) (hn : n ∈ Z.N) (hiC : i ∈ Z.C) + (hk : σ.kheap t.bv = some (.tcb x)) (hkn : σ.kheap n.bv = some (.notification xn)) + (hsp : x.tcbSchedContext = some i.bv) (hxn : xn.ntfnSc = some i.bv) (hnr : Fl4Spec.runnable x.tcbState = false) + (hnrw : ¬ P1.Runnable (L.mem s (P1.stA t))) + (hsa : ∀ u, σ.schedulerAction = .switchThread u → u ≠ t.bv) (hid : t.bv ≠ σ.idleThread) : + notification.maybe_return_sched_context st s n t ⦃ s' => ∃ σ', + Fl4Spec.maybeReturnSc (Z := Unit) n.bv t.bv σ = ({((), σ')}, False) ∧ + ∃ (Ac' : Set U64) (Lst' : U64 → List U64), + JAtZB (L := L) arI AR Z H Cs Ac' Lst' D s' a σ' ∧ NatFoot (L := L) Z s σ s' σ' ⦄ := by + have htP : t ∈ P1.Heap.tcbs H := by rw [← J.tcbs_eq.1]; exact ht + obtain ⟨⟨Q0, R0, h0⟩, -⟩ := J.blk + exact WP.spec_mono (J.maybeReturnBQ ht hn hiC hk hkn hsp hxn (J.inv.notQueued hk hnr) (P1.blocked_out h0 htP hnrw) + hsa hid) fun _ h => h.2 /-- **`maybe_return_sched_context` at `J`** (module docstring): a thread `t`, not runnable, neither the idle thread nor the switch target, whose SC @@ -807,10 +845,10 @@ theorem JAtZ.maybeReturnJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {t n hsa hid) fun _ h => h.2 set_option maxHeartbeats 16000000 in -/-- **The blocking receive at `J` up to `maybe_return_sched_context`** (as -`JAtZ.recvSigBlockJ`, for any SC of the notification): `JAtZ` at the state -after `rsPre`, the thread blocked, a reschedule pending. -/ -theorem JAtZ.recvSigBlockPreJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) +/-- The blocking receive up to `maybe_return_sched_context` for any notification +scheduling context. The thread is blocked, with a reschedule pending at the +shared joint state after `rsPre`. -/ +theorem JAtZB.recvSigBlockPreB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {t : U64} {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hni : Entered σ) (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) @@ -824,7 +862,8 @@ theorem JAtZ.recvSigBlockPreJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR σ3.kheap t.bv = some (.tcb { xt with tcbState := .blockedOnNotification n.bv }) ∧ (∃ q, σ3.kheap n.bv = some (.notification { x with ntfnObj := .waitingNtfn q })) ∧ σ3.schedulerAction = .chooseNewThread ∧ σ3.curThread = σ.curThread ∧ σ3.idleThread = σ.idleThread ∧ - ∃ V', JAtZ (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst (D \ {n}) s4 a σ3 ⦄ := by + JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s4 a σ3 ∧ + NatFoot (L := L) Z s σ s4 σ3 ⦄ := by have hI := J.xtop.irelK J.ks J.buf have hS := J.xtop.fin.1.1.1 have hC : CurInv σ := J.curInv (by rw [hni.2]; intro e; cases e) @@ -845,7 +884,7 @@ theorem JAtZ.recvSigBlockPreJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR obtain ⟨σ', heq, F, hik, -, hcn, hsrt⟩ := recvSigBlockPre_eq hkt hk hna hnqN hq hr (J.isorted.2 _ _ hk) have hcn' : σ'.schedulerAction = .chooseNewThread := hcn hcur.symm hres -- P1's `BlkOK` after - have hcb := J.canBlockCur ht hcur hkt hr hni hnq + have hcb := (J.faultEntryBlock hni ht hcur hkt hr (by rw [← hcur]; exact hnq)).1 obtain ⟨⟨Q0, R0, h0⟩, hB0, hX0, hS0, hSg0⟩ := J.blk have hx := hI.1.toN.at_ntfn hn hk have hst0 : stOf (L.mem s) n = obj.NTFN_IDLE ∨ stOf (L.mem s) n = obj.NTFN_WAITING := by @@ -867,8 +906,9 @@ theorem JAtZ.recvSigBlockPreJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR · exact Or.inl (by rw [h, ntfn_consts.1]) · exact Or.inr (by rw [h, ntfn_consts.2.1]) have p1 := P1.ntfn_blockPre s t n Q0 R0 h0 hB0 hX0 hS0 hSg0 hcb hidW (J.zH.2.2.2 n hn) h01 + have hsort := Sched.qSorted_of_spec (L := L) hI.1.toE hI.1.toN J.ip.e.tcb J.isorted refine WP.spec_mono (WP.spec_and (receive_signal_preF J.ip s σ hI ht hkt hnn hqE hn hk hna - (J.qsortedN n hn) cb rt) p1) ?_ + (hsort n (Or.inr hn)) cb rt) p1) ?_ rintro s' ⟨⟨σ0, h0eq, hspec, hIR, hRB, hkn3, hkt3⟩, hbk⟩ obtain rfl : σ' = σ0 := by have e1 := (Prod.mk.inj (h0eq.symm.trans heq)).1 @@ -957,14 +997,36 @@ theorem JAtZ.recvSigBlockPreJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR J.apart.sc_frame hks F.szKeep, hjpl, hjcov, hkern, J.cnW_utc hW hF.1 F.sk.cdt F.sk.orig F.sk.arch hDbg, F.nsc J.nsc, I3, hcur3, F.own J.own, F.off J.off, hAI3, F.bsa J.bsa hscA, F.isorted J.isorted hsrt, hrun3, hblk⟩ - have hC0 := JCs.same J hF.1 F.ksz + have hC0 := JCs.sameB J hF.1 F.ksz have hCs : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ - have J3 := J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) F.sk.arch - (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R - (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) - exact ⟨σ', hspec, hbk.2.2.2.2.2.1, hkt3, hkn3, hcn', F.sk.ct, F.sk.idle, _, J3⟩ + have B' : JAtZB (L := L) arI AR (Z.mv Z.G Z.vs) H Cs Ac Lst (D \ {n}) s' a σ' := + J.cs_frameI hks hX3 hW (fun b hb => Ipc.CnW.oobj hS hb) (fun b hb => J.cnW_nv hb) F.sk.arch + (fun q ao h => F.othF h (fun _ e => by cases e) (fun _ e => by cases e)) F.szKeep hF.1 F.kback F.ipcFr hCs R + (J.rbw_dbgKeep hRB) F.sk.orig (XTopAt.idleW_eqS J.xtop hX3 F.sk.idle) (J.zD_diffN hn) + exact ⟨σ', hspec, hbk.2.2.2.2.2.1, hkt3, hkn3, hcn', F.sk.ct, F.sk.idle, B', + NatFoot.ofWS hF.1 hW (fun _ hb => Or.inl (Ipc.CnW.oobj hS hb)) (fun _ hb => J.cnW_nv hb) + (fun _ hb => J.cnW_nh hb) F.natK⟩ +/-- Native blocking receive prefix, ending immediately before the possible SC return. -/ +theorem JAtZ.recvSigBlockPreJ (hAT : P0.ArchTotal arI) (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + {t : U64} {xt : Fl4Spec.Tcb} (hkt : σ.kheap t.bv = some (.tcb xt)) (ht : t ∈ Z.T) (hcur : σ.curThread = t.bv) (hni : Entered σ) + (hnq : ∀ d p, σ.curThread ∉ σ.readyQueues d p) + {n : U64} (hn : n ∈ Z.N) {x : Fl4Spec.Notification} (hk : σ.kheap n.bv = some (.notification x)) + (hna : ∀ b, x.ntfnObj ≠ .activeNtfn b) + (hcov : (∀ k, Z.th n = some k → Covered (L.mem s) Z.G n.val (n.val + max (objSize k) 1)) ∧ + (∀ k, H n = some k → Covered (L.mem s) Z.G n.val (n.val + 2 ^ k.bits))) + (cb : Fl4Spec.Badge) (rt : Fl4Spec.CapRights) : + Ipc.rsPre st s t n ⦃ s4 => ∃ σ3, + Fl4Spec.receiveSignal (Z := Unit) t.bv (.notificationCap n.bv cb rt) true σ = Fl4Spec.maybeReturnSc n.bv t.bv σ3 ∧ + ¬ P1.Runnable (L.mem s4 (P1.stA t)) ∧ + σ3.kheap t.bv = some (.tcb { xt with tcbState := .blockedOnNotification n.bv }) ∧ + (∃ q, σ3.kheap n.bv = some (.notification { x with ntfnObj := .waitingNtfn q })) ∧ + σ3.schedulerAction = .chooseNewThread ∧ σ3.curThread = σ.curThread ∧ σ3.idleThread = σ.idleThread ∧ + ∃ V', JAtZ (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst (D \ {n}) s4 a σ3 ⦄ := + WP.spec_mono (J.toB.recvSigBlockPreB hkt ht hcur hni hnq hn hk hna hcov cb rt) + fun _ ⟨σ', he, hnr, hkT, hkN, hact, hc, hi, B', F⟩ => ⟨σ', he, hnr, hkT, hkN, hact, hc, hi, _, B'.toJN J F⟩ + set_option maxHeartbeats 4000000 in /-- **The blocking receive at `J` that gives the SC back** (the former `JRecvSigRetHyp` [W]): the current thread blocks on an idle or waiting diff --git a/proofs/Fl4Proofs/P3/XTopJointRest.lean b/proofs/Fl4Proofs/P3/XTopJointRest.lean index f1892a665..fa7b82cf3 100644 --- a/proofs/Fl4Proofs/P3/XTopJointRest.lean +++ b/proofs/Fl4Proofs/P3/XTopJointRest.lean @@ -924,29 +924,28 @@ theorem jowes_slotOf {AR : A → Fl4Spec.Cr3 → Prop} {th : TypedHeap} {G' : Md subst hth exact J.toB.slotOfCs arI hcn hkc hw hFI hMO hkz hTK hT3 hT2 hDl hX' (hR Z H Cs Ac Lst D rfl J hX') -/-- **`JStepHyps.slot`**, with no premise (an insertion between two CNodes' -slots: `CnK`, and `KeepC` from the two CNodes, `capInsert_nko`; the -destination's cap the only one changed, from `Null` and to no untyped cap: -`CoverKept`, `UtKept`; its original bit the only one set, at an `AbsSlot`; -the untyped caps' facts by `ins_utc`, the destination's CNode live). -/ -theorem jowes_ins {AR : A → Fl4Spec.Cr3 → Prop} {th : TypedHeap} {G' : MdbGhost} (s s' : S) (a : A) - (σ σ' : Fl4Spec.State Unit) (c : Fl4Spec.Cap) (ps pd : U64) (rs rd : Nat) (idxs idxd : List Bool) - (hps : th ps = some (.cnode rs)) (hpd : th pd = some (.cnode rd)) +/-- The joint base after insertion into an empty CNode slot. -/ +theorem JAtZB.insCs {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {G' : MdbGhost} + {s s' : S} {a : A} {σ σ' : Fl4Spec.State Unit} + (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + (c : Fl4Spec.Cap) (ps pd : U64) (rs rd : Nat) (idxs idxd : List Bool) + (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) (hci : Fl4Spec.capInsert (Z := Unit) c (ps.bv, idxs) (pd.bv, idxd) σ = ({((), σ')}, False)) (hld : idxd.length = rd) - (hcap : ∀ y, IsSlot th y → y ≠ cslotAddr (pd.bv, idxd) → capV (L.mem s') y = capV (L.mem s) y) + (hcap : ∀ y, IsSlot Z.th y → y ≠ cslotAddr (pd.bv, idxd) → capV (L.mem s') y = capV (L.mem s) y) (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) (hnu : ∀ p sb fi d, capV (L.mem s') (cslotAddr (pd.bv, idxd)) ≠ .Untyped p sb fi d) - (hw : P1.WritesIn st s s' (InSlot th)) (hFI : FinInv th (L.mem s') σ' G') (hMO : MdbOKV (L.ksb s) (L.mem s') G') - (hTK : TypedKept th (L.mem s) (L.mem s')) - (hT3 : ∀ t, th t = some .tcb → capAt (L.mem s') (slotOf t 3) = capAt (L.mem s) (slotOf t 3)) - (hDL : DestLive th (L.mem s) (L.ksb s) pd) (hDl : DelInv th (L.mem s) → DelInv th (L.mem s')) : - JOwesCsD (L := L) arI AR th G' s a σ s' a σ' := by - intro Z H Cs Ac Lst D hth J hX' + (hw : P1.WritesIn st s s' (InSlot Z.th)) (hFI : FinInv Z.th (L.mem s') σ' G') (hMO : MdbOKV (L.ksb s) (L.mem s') G') + (hTK : TypedKept Z.th (L.mem s) (L.mem s')) + (hT3 : ∀ t, Z.th t = some .tcb → capAt (L.mem s') (slotOf t 3) = capAt (L.mem s) (slotOf t 3)) + (hDL : DestLive Z.th (L.mem s) (L.ksb s) pd) (hDl : DelInv Z.th (L.mem s) → DelInv Z.th (L.mem s')) + (hX' : XTopAt (L := L) AR { Z with G := G' } s' a σ') : + JCs (L := L) { Z with G := G' } s' σ' ∧ + ∃ Ac' Lst', JRest (L := L) arI { Z with G := G' } H Cs Ac' Lst' D s' σ' := by have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) c (ps.bv, idxs) (pd.bv, idxd) σ).1 := by rw [hci]; rfl have hcn := capInsert_nkn c _ _ σ _ hmem have hSt := J.xtop.rel.2.1.1 - subst hth have hkc : KeepC σ σ' := keepC_of hcn (capInsert_nko c _ _ σ _ hmem) (by rintro p (rfl | hp) · obtain ⟨cs, h, -⟩ := hSt.cnode ps rs hps; exact ⟨rs, cs, h⟩ @@ -956,26 +955,46 @@ theorem jowes_ins {AR : A → Fl4Spec.Cr3 → Prop} {th : TypedHeap} {G' : MdbGh have hs : IsSlot Z.th (slotOf t 2) := ⟨t, 2, Or.inr ⟨ht, by decide⟩, rfl⟩ rw [capAt_capV, capAt_capV, hcap _ hs fun e => cnode_slot_ne_tcb J.xtop.rel.1 hpd hld ht 2 (by decide) (by rw [cslot_tcb2]; exact e.symm)] - refine jowes_slotOf arI s s' a σ σ' hcn hkc hw hFI hMO (capInsert_nkz c _ _ σ _ hmem) hTK hT3 hT2 hDl - (fun Z2 H2 Cs2 Ac2 Lst2 D2 hth2 J2 hX2 => ?_) Z H Cs Ac Lst D rfl J hX' - have hcap' : ∀ y, IsSlot Z2.th y → y ≠ cslotAddr (pd.bv, idxd) → capV (L.mem s') y = capV (L.mem s) y := - fun y h => hcap y (hth2 ▸ h) - obtain ⟨-, ⟨-, -, hM2', -, -⟩, -⟩ := hX2.rel - have hutc := J2.ins_utc arI (G' := G') (hth2 ▸ hpd) hld hcap' hnull hnu (hth2 ▸ hw) + refine J.slotOfCs arI hcn hkc hw hFI hMO (capInsert_nkz c _ _ σ _ hmem) hTK hT3 hT2 hDl hX' ?_ + obtain ⟨-, ⟨-, -, hM2', -, -⟩, -⟩ := hX'.rel + have hutc := J.ins_utcG arI (G' := G') ⟨pd, rfl, Or.inl (by rw [hld]; exact hpd)⟩ hcap hnull hnu hw (capInsert_cdt c _ _ σ _ hmem) (fun sl e => capInsert_orig c _ _ σ ((), σ') hmem sl e) - (by have e := congrArg (fun z => z.archState.x64GlobalPml4) hcn.1; exact e) hM2' (hth2 ▸ hDL) + (by have e := congrArg (fun z => z.archState.x64GlobalPml4) hcn.1; exact e) hM2' + (fun _ _ _ _ _ hsl hcv hdv hnc => J.live_slot arI hsl hcv hdv hnc ⟨pd, rd, rfl, hpd, hld, hDL⟩) have hn0 : capV (L.mem s) (cslotAddr (pd.bv, idxd)) = .Null := by have h := capAt_capV (L.mem s) (cslotAddr (pd.bv, idxd)); rw [hnull] at h; exact (eq_of_ok_eq h).symm refine ⟨fun y hy hp hu => ?_, fun x hx p sb fi d hc => ?_, fun sl h => ?_, hutc⟩ · have hyd : y ≠ cslotAddr (pd.bv, idxd) := fun e => by rw [e, hn0, physF_null] at hp; cases hp - have e := hcap' y hy hyd + have e := hcap y hy hyd exact ⟨y, hy, by rw [e]; exact hp, by rw [e]; exact hu, by rw [e], by rw [e]⟩ · have hxd : x ≠ cslotAddr (pd.bv, idxd) := fun e => hnu p sb fi d (e ▸ hc) - exact ⟨x, hx, fi, by rw [← hcap' x hx hxd]; exact hc, Or.inl le_rfl⟩ + exact ⟨x, hx, fi, by rw [← hcap x hx hxd]; exact hc, Or.inl le_rfl⟩ · by_cases e : sl = (pd.bv, idxd) - · right; subst e; exact ⟨pd, rfl, Or.inl (by rw [hld]; exact hth2 ▸ hpd)⟩ + · right; subst e; exact ⟨pd, rfl, Or.inl (by rw [hld]; exact hpd)⟩ · left; rw [← capInsert_orig c _ _ σ ((), σ') hmem sl e]; exact h +/-- **`JStepHyps.slot`**, with no premise (an insertion between two CNodes' +slots: `CnK`, and `KeepC` from the two CNodes, `capInsert_nko`; the +destination's cap the only one changed, from `Null` and to no untyped cap: +`CoverKept`, `UtKept`; its original bit the only one set, at an `AbsSlot`; +the untyped caps' facts by `ins_utc`, the destination's CNode live). -/ +theorem jowes_ins {AR : A → Fl4Spec.Cr3 → Prop} {th : TypedHeap} {G' : MdbGhost} (s s' : S) (a : A) + (σ σ' : Fl4Spec.State Unit) (c : Fl4Spec.Cap) (ps pd : U64) (rs rd : Nat) (idxs idxd : List Bool) + (hps : th ps = some (.cnode rs)) (hpd : th pd = some (.cnode rd)) + (hci : Fl4Spec.capInsert (Z := Unit) c (ps.bv, idxs) (pd.bv, idxd) σ = ({((), σ')}, False)) + (hld : idxd.length = rd) + (hcap : ∀ y, IsSlot th y → y ≠ cslotAddr (pd.bv, idxd) → capV (L.mem s') y = capV (L.mem s) y) + (hnull : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + (hnu : ∀ p sb fi d, capV (L.mem s') (cslotAddr (pd.bv, idxd)) ≠ .Untyped p sb fi d) + (hw : P1.WritesIn st s s' (InSlot th)) (hFI : FinInv th (L.mem s') σ' G') (hMO : MdbOKV (L.ksb s) (L.mem s') G') + (hTK : TypedKept th (L.mem s) (L.mem s')) + (hT3 : ∀ t, th t = some .tcb → capAt (L.mem s') (slotOf t 3) = capAt (L.mem s) (slotOf t 3)) + (hDL : DestLive th (L.mem s) (L.ksb s) pd) (hDl : DelInv th (L.mem s) → DelInv th (L.mem s')) : + JOwesCsD (L := L) arI AR th G' s a σ s' a σ' := by + intro Z H Cs Ac Lst D hth J hX' + subst hth + exact J.toB.insCs arI c ps pd rs rd idxs idxd hps hpd hci hld hcap hnull hnu hw hFI hMO hTK hT3 hDL hDl hX' + /-- **A CNode-only step's `JCs` / `JRest` at `JAtZB`** (mmuAD gen 42b63, the Architect's ruling 08:57Z item 2: `jowes_cn`'s proof moved in place; `jowes_cn` is its `JAtZ.toB` instance). -/ theorem JAtZB.cnCs {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} diff --git a/proofs/Fl4Proofs/P3/XTopJointTdx.lean b/proofs/Fl4Proofs/P3/XTopJointTdx.lean index a9aca5376..29c9141d7 100644 --- a/proofs/Fl4Proofs/P3/XTopJointTdx.lean +++ b/proofs/Fl4Proofs/P3/XTopJointTdx.lean @@ -245,54 +245,39 @@ theorem tdxTail_jat [Fl4Proofs.P1.PlainStore st] (hAM : ARMask arI AR) {s : S} { exact hRep Z' H Cs Ac' Lst' D' s1 a1 σ1 rfl J' ct _ call v hcurY.symm hkY rfl s3 w heq hw hXT set_option maxHeartbeats 16000000 in -/-- **The TDX control cap at `JAt`, every label** (module docstring). -/ -theorem decode_invocation_tdxJ [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.State Unit) - (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) - (hARI : ARIface arI AR) (hT : P0.ArchTotal arI) (hZB : ZNoBlk Z.th (L.mem s) (L.ksb s)) +/-- Decode the three TDCALLs, using the supplied relation on the two outcomes. -/ +theorem decodeTdxOfTail [P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (X : S → A → Fl4Spec.State Unit → Prop) (hX : X s ar σ) + (htdx : TdxRel arI σ) (hT : P0.ArchTotal arI) {th : TypedHeap} (lbl 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) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) - (hex : ExtraRel Z.th (L.mem s) (exAt extra 0) (excaps.map Prod.fst)) + (hex : ExtraRel th (L.mem s) (exAt extra 0) (excaps.map Prod.fst)) (slot : U64) (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) - (buffer : Option Fl4Spec.ObjRef) : - XCap (L := L) arI (JAt (L := L) arI AR) s ar σ lbl length slot .TdxControl blk call don fp buf extra lbl.bv - (args.map UScalar.bv) ci sl excaps buffer ct := by - have hJ0 : JAt (L := L) arI AR s ar σ := ⟨Z, H, Cs, Ac, Lst, D, J, inferInstance⟩ - obtain ⟨hcur, -⟩ := J.curEq hct - have hC := J.curRun.run hni - obtain ⟨htcb, x, hk⟩ := J.xtop.curTcb hcur - have hst : Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = - ({((), Notif.stsAct (Notif.updK σ ct.bv (.tcb { x with tcbState := .restart })) ct.bv)}, False) := - Notif.setThreadState_eq .restart hk - -- the errors: the spec decode's one outcome - have herr : ∀ (e : error.Exc) (e' : Fl4Spec.SyscallError), ErrRel e e' → - (Fl4Spec.decodeTdxInvocation (Z := Unit) lbl.bv (args.map UScalar.bv) excaps).run σ = - ({(.error e', σ)}, False) → - TopPostRMX (L := L) (JAt (L := L) arI AR) σ blk call don - (specDec fp lbl.bv (args.map UScalar.bv) ci sl .TdxControl excaps buffer) ct s (.Err e, s, ar) := - fun e e' hE hd => Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err hd), hE⟩, hJ0⟩ - -- a success: the TDCALL's tail - have hsucc : ∀ (inv : Fl4Spec.Fl4TdxInvocation) (v : U64) (a1 : A), TdxArchStep arI ar a1 → + (buffer : Option Fl4Spec.ObjRef) + (hTail : ∀ (inv : Fl4Spec.Fl4TdxInvocation) (v : U64) (a1 : A), TdxArchStep arI ar a1 → (Fl4Spec.decodeTdxInvocation (Z := Unit) lbl.bv (args.map UScalar.bv) excaps).run σ = ({(.ok inv, σ)}, False) → (do let s2 ← sched.set_thread_state st s ct sched.TS_RESTART let s3 ← vspace.reply1 st s2 ct call v ok ((core.result.Result.Ok () : core.result.Result Unit error.Exc), s3, a1)) ⦃ res => - TopPostRMX (L := L) (JAt (L := L) arI AR) σ blk call don - (specDec fp lbl.bv (args.map UScalar.bv) ci sl .TdxControl excaps buffer) ct s res ⦄ := by - intro inv v a1 hop hd - refine WP.spec_mono (tdxTail_jat arI hARI.toMask J hZB hC hcur call v a1 hop inv) ?_ - rintro res ⟨hres, hne, hall⟩ - obtain ⟨rr, σy, hy, z, hz, hRT, hXT⟩ := hall _ hst - exact Or.inr ⟨hres, hne, .invokeArchObject (.tdx inv), DecOut.of_eq (run_liftME_ok (run_liftME_ok hd)), _, - by rw [hcur]; exact hst, rr, σy, hy, z, hz, hRT, hXT⟩ - refine Or.inr ?_ + TopPostRMX (L := L) X σ blk call don + (specDec fp lbl.bv (args.map UScalar.bv) ci sl .TdxControl excaps buffer) ct s res ⦄) : + DecOKRMX (L := L) arI X s ar σ lbl length slot .TdxControl blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci sl excaps buffer ct := by + have herr : ∀ (e : error.Exc) (e' : Fl4Spec.SyscallError), ErrRel e e' → + (Fl4Spec.decodeTdxInvocation (Z := Unit) lbl.bv (args.map UScalar.bv) excaps).run σ = + ({(.error e', σ)}, False) → + TopPostRMX (L := L) X σ blk call don + (specDec fp lbl.bv (args.map UScalar.bv) ci sl .TdxControl excaps buffer) ct s (.Err e, s, ar) := + fun e e' hE hd => Or.inl ⟨e, rfl, rfl, ⟨e', run_liftME_err (run_liftME_err hd), hE⟩, hX⟩ + have hsucc := hTail unfold DecOKRMX syscall.decode_invocation rw [hct]; simp only [bind_ok] unfold tdx.decode - rw [J.kern.tdx.1 ar]; simp only [bind_ok] + rw [htdx.1 ar]; simp only [bind_ok] by_cases hact : σ.archState.x64Plat.tdxActive = true swap · rw [if_neg hact]; simp only [error.illegal, bind_ok, WP.spec_ok] @@ -438,7 +423,7 @@ theorem decode_invocation_tdxJ [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ · rw [hlenB] at h; exact h1 (by scalar_tac) · cases h have hfr : (Fl4Spec.the (e0 :: es)[0]?).1 = absCap c0 := by rw [hca0.2, ← he0] <;> rfl - rcases quote_frame_cases arI J.kern.tdx.2 ar c0 with ⟨hn, hsn⟩ | ⟨p, n, hp, hsp, hdm⟩ + rcases quote_frame_cases arI htdx.2 ar c0 with ⟨hn, hsn⟩ | ⟨p, n, hp, hsp, hdm⟩ · rw [hn]; simp only [bind_ok, error.invalid_cap, WP.spec_ok] refine herr _ (.invalidCapability 1) (Or.inr ⟨1#u64, rfl, rfl⟩) ?_ unfold Fl4Spec.decodeTdxInvocation; rw [tdx_dec_gets] @@ -479,6 +464,37 @@ theorem decode_invocation_tdxJ [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ rw [hsq, tdx_unlessE_neg hl] exact Sched.run_throw_bind _ _ _ + +set_option maxHeartbeats 16000000 in +/-- **The TDX control cap at `JAt`, every label** (module docstring). -/ +theorem decode_invocation_tdxJ [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) + (hARI : ARIface arI AR) (hT : P0.ArchTotal arI) (hZB : ZNoBlk Z.th (L.mem s) (L.ksb s)) + (lbl 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) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hex : ExtraRel Z.th (L.mem s) (exAt extra 0) (excaps.map Prod.fst)) + (slot : U64) (blk call don fp : Bool) (ci : Fl4Spec.CapRef) (sl : Fl4Spec.CslotPtr) + (buffer : Option Fl4Spec.ObjRef) : + XCap (L := L) arI (JAt (L := L) arI AR) s ar σ lbl length slot .TdxControl blk call don fp buf extra lbl.bv + (args.map UScalar.bv) ci sl excaps buffer ct := by + have hJ0 : JAt (L := L) arI AR s ar σ := ⟨Z, H, Cs, Ac, Lst, D, J, inferInstance⟩ + obtain ⟨hcur, -⟩ := J.curEq hct + have hC := J.curRun.run hni + obtain ⟨htcb, x, hk⟩ := J.xtop.curTcb hcur + have hst : Fl4Spec.setThreadState (Z := Unit) ct.bv .restart σ = + ({((), Notif.stsAct (Notif.updK σ ct.bv (.tcb { x with tcbState := .restart })) ct.bv)}, False) := + Notif.setThreadState_eq .restart hk + refine Or.inr (decodeTdxOfTail arI s ar σ (JAt (L := L) arI AR) hJ0 J.kern.tdx hT lbl length buf ct hct + args hlen hargs extra excaps hex slot blk call don fp ci sl buffer ?_) + intro inv v a1 hop hd + refine WP.spec_mono (tdxTail_jat arI hARI.toMask J hZB hC hcur call v a1 hop inv) ?_ + rintro res ⟨hres, hne, hall⟩ + obtain ⟨rr, σy, hy, z, hz, hRT, hXT⟩ := hall _ hst + exact Or.inr ⟨hres, hne, .invokeArchObject (.tdx inv), DecOut.of_eq (run_liftME_ok (run_liftME_ok hd)), _, + by rw [hcur]; exact hst, rr, σy, hy, z, hz, hRT, hXT⟩ + end end diff --git a/proofs/Fl4Proofs/P3/XTopJointVsFb.lean b/proofs/Fl4Proofs/P3/XTopJointVsFb.lean index b8e60a82f..e4cd659eb 100644 --- a/proofs/Fl4Proofs/P3/XTopJointVsFb.lean +++ b/proofs/Fl4Proofs/P3/XTopJointVsFb.lean @@ -41,21 +41,40 @@ noncomputable section section SpecLoop open Fl4Spec +/-- The reply of an empty suffix does not depend on its deleter. -/ +theorem fbRunWith_nil_mem (del : CslotPtr → PMonad Unit Unit) + (b : Fl4FrameBatchInvocation) (n : Nat) (d : Bool) (σ : State Unit) : + ((n, (0 : Data), d), σ) ∈ (fbRunWith del b [] n d σ).1 := rfl + theorem fbRun_nil_mem (b : Fl4FrameBatchInvocation) (n : Nat) (d : Bool) (σ : State Unit) : - ((n, (0 : Data), d), σ) ∈ (fbRun (Z := Unit) b [] n d σ).1 := rfl + ((n, (0 : Data), d), σ) ∈ (fbRun (Z := Unit) b [] n d σ).1 := + fbRunWith_nil_mem capDelete b n d σ + +theorem fbRunWith_cons_err {del : CslotPtr → PMonad Unit Unit} + {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {ws : List (Data × Data × Data)} + {n : Nat} {d : Bool} {σ τ : State Unit} {c : Data} (h : (.error c, τ) ∈ ((fbEntryWith del b w).run σ).1) : + ((n, c, d), τ) ∈ (fbRunWith del b (w :: ws) n d σ).1 := by + unfold fbRunWith + exact ⟨_, h, rfl⟩ theorem fbRun_cons_err {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {ws : List (Data × Data × Data)} {n : Nat} {d : Bool} {σ τ : State Unit} {c : Data} (h : (.error c, τ) ∈ ((fbEntry (Z := Unit) b w).run σ).1) : - ((n, c, d), τ) ∈ (fbRun (Z := Unit) b (w :: ws) n d σ).1 := by - unfold fbRun - exact ⟨_, h, rfl⟩ + ((n, c, d), τ) ∈ (fbRun (Z := Unit) b (w :: ws) n d σ).1 := + fbRunWith_cons_err h + +theorem fbRunWith_cons_ok {del : CslotPtr → PMonad Unit Unit} + {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {ws : List (Data × Data × Data)} + {n : Nat} {d d' : Bool} {σ τ : State Unit} {y : (Nat × Data × Bool) × State Unit} + (h : (.ok d', τ) ∈ ((fbEntryWith del b w).run σ).1) (h2 : y ∈ (fbRunWith del b ws (n + 1) (d || d') τ).1) : + y ∈ (fbRunWith del b (w :: ws) n d σ).1 := by + unfold fbRunWith + exact ⟨_, h, h2⟩ theorem fbRun_cons_ok {b : Fl4FrameBatchInvocation} {w : Data × Data × Data} {ws : List (Data × Data × Data)} {n : Nat} {d d' : Bool} {σ τ : State Unit} {y : (Nat × Data × Bool) × State Unit} (h : (.ok d', τ) ∈ ((fbEntry (Z := Unit) b w).run σ).1) (h2 : y ∈ (fbRun (Z := Unit) b ws (n + 1) (d || d') τ).1) : - y ∈ (fbRun (Z := Unit) b (w :: ws) n d σ).1 := by - unfold fbRun - exact ⟨_, h, h2⟩ + y ∈ (fbRun (Z := Unit) b (w :: ws) n d σ).1 := + fbRunWith_cons_ok h h2 end SpecLoop @@ -215,66 +234,56 @@ theorem fb_entryJ [Fl4Proofs.P1.PlainStore st] (hA : ArchRefines arI AR) (hT : P · exact fb_entry_remapJ arI hA hT C h1 h2 x a τ hM w w0 w1 w2 hw0 hw1 hw2 df · exact fb_entry_delJ arI hA hT hDP hnd hARI C h1 h2 x a τ hM w w0 w1 w2 hw0 hw1 hw2 df -set_option maxHeartbeats 8000000 in -/-- **The batch's loop at `J`** (the decode's checks passed): FL4's loop has an outcome of the spec's -`fbRun` over the batch's entries from `σ` (entries done, the failing entry's code, whether a deferred -delete cleared an entry), with `JAtZ` after, the current thread and its thread state kept. OPEN (the -one FrameBatch stub left; KENTRY-PLAN fb3k). -/ -theorem fb_loopI [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.State Unit) - (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hA : ArchRefines arI AR) - (hT : P0.ArchTotal arI) - (ct : U64) (hct : sched.cur_thread st s = ok ct) - (p asid : U64) (h0 : asid ≠ 0#u64) (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 ct buf i = ok x) - (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) - (hXO : ExtraOK Z.th (L.mem s) extra excaps) (hXN : ExtraNamed Z.th (L.mem s) extra) - (hf : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) asid.bv) σ = ({(.ok p.bv, σ)}, False)) - (b : Fl4Spec.Fl4FrameBatchInvocation) - (hb : fbDecP p.bv asid.bv (args.map UScalar.bv) excaps = .ok b) - (a0 : U64) (ha0 : args[0]? = some a0) - (n words op depth mr cr attr : U64) (dd : Bool) (cnode dcnode : capability.Cap) - (hop : op = a0 &&& vspace.frame_batch.OP) (hw : words.val = b.op.words) - (hn : n = (⟨a0.bv >>> 8⟩ : U64) &&& 255#u64) (hdp : depth = (⟨a0.bv >>> 16⟩ : U64) &&& 127#u64) - (hmr : mr = (⟨a0.bv >>> 24⟩ : U64) &&& 15#u64) (hcr : cr = (⟨a0.bv >>> 28⟩ : U64) &&& 15#u64) - (hat : attr = (⟨a0.bv >>> 32⟩ : U64) &&& 31#u64) - (hdd : ∃ d : U64, d = 1099511627776#u64 ∧ dd = (a0 &&& d != 0#u64)) - (hcn : ∃ x, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok cnode) - (hdcn : dcnode = cnode ∨ ∃ x, vspace.extra extra 1#usize = ok (some x) ∧ capAt (L.mem s) x = ok dcnode) - (hbc : b.cnode = absCap cnode) (hbd : b.destCnode = absCap dcnode) - (hcnC : ∃ a b d e, cnode = .CNode a b d e) (hdc6 : Fl4Spec.isCnodeCap (absCap dcnode) = true) +-- Keep the error-code contract of FbErrOK for any batch deleter. +def FbErrOKWith (del : Fl4Spec.CslotPtr → Fl4Spec.PMonad Unit Unit) + (b : Fl4Spec.Fl4FrameBatchInvocation) (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) + (τ τ1 : Fl4Spec.State Unit) : error.Exc → Prop + | .Sys e1 => error.SysError.code e1 ⦃ c => (.error c.bv, τ1) ∈ ((Fl4Spec.fbEntryWith del b w).run τ).1 ⦄ + | _ => (.error fl4_abi.consts.error.IllegalOperation.bv, τ1) ∈ ((Fl4Spec.fbEntryWith del b w).run τ).1 + +/-- The matched state and outcome of one concrete batch entry. -/ +def FbEntryWithPost (del : Fl4Spec.CslotPtr → Fl4Spec.PMonad Unit Unit) + (Inv : S → A → Fl4Spec.State Unit → Prop) + (b : Fl4Spec.Fl4FrameBatchInvocation) (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) + (τ : Fl4Spec.State Unit) (df : Bool) (r : core.result.Result Unit error.Exc × S × A × Bool) : Prop := + ∃ τ1, Inv r.2.1 r.2.2.1 τ1 ∧ + ((r.1 = .Ok () ∧ ∃ d', (.ok d', τ1) ∈ ((Fl4Spec.fbEntryWith del b w).run τ).1 ∧ + r.2.2.2 = (df || d')) ∨ + (∃ e, r.1 = .Err e ∧ r.2.2.2 = df ∧ FbErrOKWith del b w τ τ1 e)) + +set_option maxHeartbeats 4000000 in +/-- Run the concrete batch loop against `fbRunWith`, composing the actual entry +outcomes in order and stopping at the first failure with its error code. -/ +theorem fb_loopWith (del : Fl4Spec.CslotPtr → Fl4Spec.PMonad Unit Unit) + (Inv : S → A → Fl4Spec.State Unit → Prop) (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (ct buf n words : U64) (args : List U64) (bt : vspace.Batch) + (b : Fl4Spec.Fl4FrameBatchInvocation) (hInv : Inv s ar σ) + (hw : words.val = b.op.words) (hn255 : n.val ≤ 255) (hbe : b.entries = Fl4Spec.fbEntries (args.map UScalar.bv) b.op.words n.val) (hln : 1 + words.val * n.val ≤ args.length) - (hbv : b.vspace = p.bv) (hba : b.asid = asid.bv) - (C : FbCtx σ Z.th args { root := p, asid := asid, op := op, depth := depth, map_rights := mr, cap_rights := cr, attr := attr, defer_delete := dd, cnode := cnode, dest_cnode := dcnode } b) - (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) - (hLv : LiveDN (L := L) Z.th H Cs Z.G s σ) (hsg : SpecGlob σ) (hp0 : PPR0 σ) - (hDP : JDelParts (L := L) arI AR Z.th) (hnd : Fl4Spec.numDomains = 1) (hARI : ARIface arI AR) - (hRt0 : (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = cnode) ∧ (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = dcnode)) - {MC : Type} {MR : A → MC → Prop} (hKD : MRKeepD arI MR) {m0 : MC} (hmr0 : MR ar m0) : - vspace.decode_frame_batch_loop st arI s ar buf ct n words p asid op depth mr cr attr dd cnode dcnode - 0#u64 false ⦃ r => ∃ (τ : Fl4Spec.State Unit) (Z1 : XSets) (H1 : P1.Heap) (Cs1 Ac1 : Set U64) - (Lst1 : U64 → List U64) (D1 : Set U64), - ((r.2.2.1.val, r.2.2.2.1.bv, r.2.2.2.2), τ) ∈ (Fl4Spec.fbRun (Z := Unit) b b.entries 0 false σ).1 ∧ - JAtZ (L := L) arI AR Z1 H1 Cs1 Ac1 Lst1 D1 r.1 r.2.1 τ ∧ VxE Z1 ∧ τ.curThread = σ.curThread ∧ - (∀ x : Fl4Spec.Tcb, σ.kheap ct.bv = some (.tcb x) → - ∃ y : Fl4Spec.Tcb, τ.kheap ct.bv = some (.tcb y) ∧ y.tcbState = x.tcbState) ∧ MR r.2.1 m0 ⦄ := by + (hargs : ∀ x a τ, Inv x a τ → ∀ (i : U64) v, + args[i.val]? = some v → msg.syscall_arg st x ct buf i = ok v) + (hEntry : ∀ x a τ, Inv x a τ → ∀ (w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data) + (w0 w1 w2 : U64), w0.bv = w.1 → w1.bv = w.2.1 → w2.bv = w.2.2 → + ∀ df, vspace.frame_batch_entry st arI x a bt w0 w1 w2 df ⦃ r => FbEntryWithPost del Inv b w τ df r ⦄) : + vspace.decode_frame_batch_loop st arI s ar buf ct n words bt.root bt.asid bt.op bt.depth + bt.map_rights bt.cap_rights bt.attr bt.defer_delete bt.cnode bt.dest_cnode 0#u64 false ⦃ r => + ∃ τ, ((r.2.2.1.val, r.2.2.2.1.bv, r.2.2.2.2), τ) ∈ (Fl4Spec.fbRunWith del b b.entries 0 false σ).1 ∧ + Inv r.1 r.2.1 τ ⦄ := by have hw3 : words.val ≤ 3 := by rw [hw]; cases b.op <;> decide - have hn255 : n.val ≤ 255 := by rw [hn, UScalar.val_and]; exact Nat.le_trans Nat.and_le_right (by decide) unfold vspace.decode_frame_batch_loop apply loop.spec_decr_nat (measure := fun x => match x with | (_, _, d, _) => n.val - d.val) - (inv := fun x => (∃ τ, FbMidJ (L := L) (AR := AR) arI σ Z.th ct buf args x.1 x.2.1 τ ∧ x.2.2.1.val ≤ n.val ∧ - ∀ y ∈ (Fl4Spec.fbRun (Z := Unit) b (b.entries.drop x.2.2.1.val) x.2.2.1.val x.2.2.2 τ).1, - y ∈ (Fl4Spec.fbRun (Z := Unit) b b.entries 0 false σ).1) ∧ MR x.2.1 m0 ∧ - ((∃ y, IsSlot Z.th y ∧ capV (L.mem x.1) y = cnode) ∧ (∃ y, IsSlot Z.th y ∧ capV (L.mem x.1) y = dcnode))) - · rintro ⟨x0, x1, x2, x3⟩ ⟨⟨τ, hM, hle, hpre⟩, hmq, hRt⟩ + (inv := fun x => ∃ τ, Inv x.1 x.2.1 τ ∧ x.2.2.1.val ≤ n.val ∧ + ∀ y ∈ (Fl4Spec.fbRunWith del b (b.entries.drop x.2.2.1.val) x.2.2.1.val x.2.2.2 τ).1, + y ∈ (Fl4Spec.fbRunWith del b b.entries 0 false σ).1) + · rintro ⟨x0, x1, x2, x3⟩ ⟨τ, hM, hle, hpre⟩ dsimp only at hM hle hpre unfold vspace.decode_frame_batch_loop.body dsimp only have harg : ∀ k : U64, (hk : k.val < args.length) → msg.syscall_arg st x0 ct buf k = ok args[k.val] := - fun k hk => hM.2.1 k _ (List.getElem?_eq_getElem hk) + fun k hk => hargs x0 x1 τ hM k _ (List.getElem?_eq_getElem hk) by_cases hd : x2 < n - · rw [if_pos hd] + · rw [ite_eq_left hd] have hx : x2.val < n.val := hd have hm : words.val * x2.val ≤ 3 * 255 := Nat.mul_le_mul hw3 (by omega) have hw1' : 1 ≤ words.val := by rw [hw]; cases b.op <;> decide @@ -283,105 +292,162 @@ theorem fb_loopI [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.Sta step as ⟨i3, hi3⟩ step as ⟨i4, hi4⟩ have hi4' : i4.val = 1 + b.op.words * x2.val := by rw [hi4, hi3, hw] - rw [harg i4 (by rw [Nat.mul_succ] at hk; omega)] + have hi4Bound : i4.val < args.length := by rw [Nat.mul_succ] at hk; omega + rw [harg i4 hi4Bound] simp only [bind_ok] refine WP.spec_bind (Pₘ := fun w1 : U64 => w1.bv = (if b.op.words > 1 then Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val + 1]? else 0)) ?_ ?_ · by_cases h1 : words > 1#u64 · have h1' : b.op.words > 1 := by rw [← hw]; scalar_tac - rw [if_pos h1, if_pos h1'] + rw [ite_eq_left h1, ite_eq_left h1'] step as ⟨i5, hi5⟩ rw [harg i5 (by rw [Nat.mul_succ] at hk; omega)] refine (WP.spec_ok _).2 ?_ rw [fb_the_arg (i := 1 + b.op.words * x2.val + 1) (by rw [Nat.mul_succ] at hk; rw [← hw]; omega)] simp only [hi5, hi4'] · have h1' : ¬ b.op.words > 1 := by rw [← hw]; scalar_tac - rw [if_neg h1, if_neg h1'] + rw [ite_eq_right h1, ite_eq_right h1'] exact (WP.spec_ok _).2 rfl intro w1 hw1 refine WP.spec_bind (Pₘ := fun w2 : U64 => w2.bv = (if b.op.words > 2 then Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val + 2]? else 0)) ?_ ?_ · by_cases h2 : words > 2#u64 · have h2' : b.op.words > 2 := by rw [← hw]; scalar_tac - rw [if_pos h2, if_pos h2'] + rw [ite_eq_left h2, ite_eq_left h2'] step as ⟨i5, hi5⟩ rw [harg i5 (by rw [Nat.mul_succ] at hk; omega)] refine (WP.spec_ok _).2 ?_ rw [fb_the_arg (i := 1 + b.op.words * x2.val + 2) (by rw [Nat.mul_succ] at hk; rw [← hw]; omega)] simp only [hi5, hi4'] · have h2' : ¬ b.op.words > 2 := by rw [← hw]; scalar_tac - rw [if_neg h2, if_neg h2'] + rw [ite_eq_right h2, ite_eq_right h2'] exact (WP.spec_ok _).2 rfl intro w2 hw2 have hdrop := fbEntries_drop (A := (args.map UScalar.bv : List Fl4Spec.Data)) (w := b.op.words) hx - refine WP.spec_bind (WP.spec_and (fb_entryJ arI hA hT hDP hnd hARI C x0 x1 τ hM hRt + let w : Fl4Spec.Data × Fl4Spec.Data × Fl4Spec.Data := (Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val]?, if b.op.words > 1 then Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val + 1]? else 0, if b.op.words > 2 then Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val + 2]? else 0) - _ w1 w2 (by rw [fb_the_arg (i := 1 + b.op.words * x2.val) (by rw [Nat.mul_succ] at hk; rw [← hw]; omega)]; simp only [hi4']) hw1 hw2 x3) - (MKD.vspace_frame_batch_entry L.total hT hKD (FbMidJ.delOK arI C hM _ _) hmq)) ?_ - rintro ⟨r, s1, a1, d1⟩ ⟨⟨⟨τ1, hM1, hres⟩, hCK⟩, hmr1⟩ - have hRt1 : (∃ y, IsSlot Z.th y ∧ capV (L.mem s1) y = cnode) ∧ (∃ y, IsSlot Z.th y ∧ capV (L.mem s1) y = dcnode) := - ⟨hCK.held hcnC hRt.1, hCK.held C.hdcC hRt.2⟩ + have hw0 : (args[i4.val]'hi4Bound).bv = w.1 := by + change _ = Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val]? + rw [fb_the_arg (i := 1 + b.op.words * x2.val) (by rw [Nat.mul_succ] at hk; rw [← hw]; omega)] + simp only [hi4'] + refine WP.spec_bind (hEntry x0 x1 τ hM w _ w1 w2 hw0 hw1 hw2 x3) ?_ + rintro ⟨r, s1, a1, d1⟩ ⟨τ1, hM1, hres⟩ fbj_red rcases hres with ⟨hr, d', hmem, hd1⟩ | ⟨e, hr, hd1, hE⟩ · try dsimp only at hr hd1 subst hr fbj_red step as ⟨done2, hd2⟩ - try simp only [WP.spec_ok] - try dsimp only - first - | refine ⟨⟨⟨τ1, hM1, by omega, ?_⟩, hmr1, hRt1⟩, by omega⟩ - | refine ⟨⟨τ1, hM1, by omega, ?_⟩, hmr1, hRt1, by omega⟩ - | refine ⟨⟨τ1, hM1, by omega, ?_⟩, hmr1, hRt1.1, hRt1.2, by omega⟩ + refine ⟨⟨τ1, hM1, by omega, ?_⟩, by omega⟩ intro y hy apply hpre rw [hbe] at hy ⊢ rw [hdrop] - rw [hd2] at hy - try dsimp only at hd1 - rw [hd1] at hy - exact fbRun_cons_ok hmem hy + rw [hd2, hd1] at hy + exact fbRunWith_cons_ok hmem hy · try dsimp only at hr hd1 subst hr fbj_red - have hpost : ∀ c : U64, (.error c.bv, τ1) ∈ ((Fl4Spec.fbEntry (Z := Unit) b - (Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val]?, - if b.op.words > 1 then Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val + 1]? else 0, - if b.op.words > 2 then Fl4Spec.the (args.map UScalar.bv : List Fl4Spec.Data)[1 + b.op.words * x2.val + 2]? else 0)).run τ).1 → - ∃ (τ : Fl4Spec.State Unit) (Z1 : XSets) (H1 : P1.Heap) (Cs1 Ac1 : Set U64) (Lst1 : U64 → List U64) (D1 : Set U64), - ((x2.val, c.bv, d1), τ) ∈ (Fl4Spec.fbRun (Z := Unit) b b.entries 0 false σ).1 ∧ - JAtZ (L := L) arI AR Z1 H1 Cs1 Ac1 Lst1 D1 s1 a1 τ ∧ VxE Z1 ∧ τ.curThread = σ.curThread ∧ - (∀ x : Fl4Spec.Tcb, σ.kheap ct.bv = some (.tcb x) → - ∃ y : Fl4Spec.Tcb, τ.kheap ct.bv = some (.tcb y) ∧ y.tcbState = x.tcbState) ∧ MR a1 m0 := by + have hpost : ∀ c : U64, (.error c.bv, τ1) ∈ ((Fl4Spec.fbEntryWith del b w).run τ).1 → + ∃ τ', ((x2.val, c.bv, d1), τ') ∈ (Fl4Spec.fbRunWith del b b.entries 0 false σ).1 ∧ Inv s1 a1 τ' := by intro c hc - obtain ⟨⟨Z1, H1, Cs1, Ac1, Lst1, D1, -, -, -, J1, -, hvx1⟩, -, hK1, -⟩ := hM1 - refine ⟨τ1, Z1, H1, Cs1, Ac1, Lst1, D1, hpre _ ?_, J1, hvx1, hK1.ct, fun x hx => ?_, hmr1⟩ - swap - · obtain ⟨y, hy, hxy⟩ := hK1.tcb hx; exact ⟨y, hy, hxy.1⟩ + refine ⟨τ1, hpre _ ?_, hM1⟩ rw [hbe, hdrop, hd1] - exact fbRun_cons_err hc + exact fbRunWith_cons_err hc cases e with | Sys e1 => refine WP.spec_bind hE ?_ intro c hc exact (WP.spec_ok _).2 (hpost c hc) | _ => exact (WP.spec_ok _).2 (hpost _ hE) - · rw [if_neg hd] + · rw [ite_eq_right hd] have hxn : x2.val = n.val := by have : ¬ x2.val < n.val := hd; omega - refine (WP.spec_ok _).2 ?_ - obtain ⟨⟨Z1, H1, Cs1, Ac1, Lst1, D1, -, -, -, J1, -, hvx1⟩, -, hK1, -⟩ := hM - refine ⟨τ, Z1, H1, Cs1, Ac1, Lst1, D1, hpre _ ?_, J1, hvx1, hK1.ct, fun x hx => ?_, hmq⟩ - swap - · obtain ⟨y, hy, hxy⟩ := hK1.tcb hx; exact ⟨y, hy, hxy.1⟩ + refine (WP.spec_ok _).2 ⟨τ, hpre _ ?_, hM⟩ rw [hbe, hxn, fbEntries_drop_n, ← hxn] - exact fbRun_nil_mem b _ _ _ - · have hctT : ct ∈ Z.T := J.thT ct (J.xtop.curTcb (J.curEq hct).1).1 - have hns : Vs.NoSwitch σ := fun u e => by rw [hni.2] at e; cases e - refine ⟨⟨σ, ⟨⟨Z, H, Cs, Ac, Lst, D, rfl, hctT, hbm, J, hLv, inferInstance⟩, hargs, FbKeep.refl σ, hns, hsg, hp0⟩, by simp, - fun y hy => ?_⟩, hmr0, hRt0⟩ - simpa using hy + exact fbRunWith_nil_mem del b _ _ _ + · exact ⟨σ, hInv, by simp, fun _ hy => by simpa using hy⟩ + +set_option maxHeartbeats 4000000 in +set_option linter.unusedVariables false in +/-- **The batch's loop at `J`** (the decode's checks passed): FL4's loop has an outcome of the spec's +`fbRun` over the batch's entries from `σ` (entries done, the failing entry's code, whether a deferred +delete cleared an entry), with `JAtZ` after, the current thread and its thread state kept. OPEN (the +one FrameBatch stub left; KENTRY-PLAN fb3k). -/ +theorem fb_loopI [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.State Unit) + (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s ar σ) [VxE Z] (hni : Entered σ) (hA : ArchRefines arI AR) + (hT : P0.ArchTotal arI) + (ct : U64) (hct : sched.cur_thread st s = ok ct) + (p asid : U64) (h0 : asid ≠ 0#u64) (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 ct buf i = ok x) + (extra : Aeneas.Std.Array obj.Slot 3#usize) (excaps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (hXO : ExtraOK Z.th (L.mem s) extra excaps) (hXN : ExtraNamed Z.th (L.mem s) extra) + (hf : ExceptT.run (Fl4Spec.findVspaceForAsid (Z := Unit) asid.bv) σ = ({(.ok p.bv, σ)}, False)) + (b : Fl4Spec.Fl4FrameBatchInvocation) + (hb : fbDecP p.bv asid.bv (args.map UScalar.bv) excaps = .ok b) + (a0 : U64) (ha0 : args[0]? = some a0) + (n words op depth mr cr attr : U64) (dd : Bool) (cnode dcnode : capability.Cap) + (hop : op = a0 &&& vspace.frame_batch.OP) (hw : words.val = b.op.words) + (hn : n = (⟨a0.bv >>> 8⟩ : U64) &&& 255#u64) (hdp : depth = (⟨a0.bv >>> 16⟩ : U64) &&& 127#u64) + (hmr : mr = (⟨a0.bv >>> 24⟩ : U64) &&& 15#u64) (hcr : cr = (⟨a0.bv >>> 28⟩ : U64) &&& 15#u64) + (hat : attr = (⟨a0.bv >>> 32⟩ : U64) &&& 31#u64) + (hdd : ∃ d : U64, d = 1099511627776#u64 ∧ dd = (a0 &&& d != 0#u64)) + (hcn : ∃ x, vspace.extra extra 0#usize = ok (some x) ∧ capAt (L.mem s) x = ok cnode) + (hdcn : dcnode = cnode ∨ ∃ x, vspace.extra extra 1#usize = ok (some x) ∧ capAt (L.mem s) x = ok dcnode) + (hbc : b.cnode = absCap cnode) (hbd : b.destCnode = absCap dcnode) + (hcnC : ∃ a b d e, cnode = .CNode a b d e) (hdc6 : Fl4Spec.isCnodeCap (absCap dcnode) = true) + (hbe : b.entries = Fl4Spec.fbEntries (args.map UScalar.bv) b.op.words n.val) + (hln : 1 + words.val * n.val ≤ args.length) + (hbv : b.vspace = p.bv) (hba : b.asid = asid.bv) + (C : FbCtx σ Z.th args { root := p, asid := asid, op := op, depth := depth, map_rights := mr, cap_rights := cr, attr := attr, defer_delete := dd, cnode := cnode, dest_cnode := dcnode } b) + (hbm : buf ≠ 0#u64 → ∃ sp, BufMap Z.B buf sp) + (hLv : LiveDN (L := L) Z.th H Cs Z.G s σ) (hsg : SpecGlob σ) (hp0 : PPR0 σ) + (hDP : JDelParts (L := L) arI AR Z.th) (hnd : Fl4Spec.numDomains = 1) (hARI : ARIface arI AR) + (hRt0 : (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = cnode) ∧ (∃ y, IsSlot Z.th y ∧ capV (L.mem s) y = dcnode)) + {MC : Type} {MR : A → MC → Prop} (hKD : MRKeepD arI MR) {m0 : MC} (hmr0 : MR ar m0) : + vspace.decode_frame_batch_loop st arI s ar buf ct n words p asid op depth mr cr attr dd cnode dcnode + 0#u64 false ⦃ r => ∃ (τ : Fl4Spec.State Unit) (Z1 : XSets) (H1 : P1.Heap) (Cs1 Ac1 : Set U64) + (Lst1 : U64 → List U64) (D1 : Set U64), + ((r.2.2.1.val, r.2.2.2.1.bv, r.2.2.2.2), τ) ∈ (Fl4Spec.fbRun (Z := Unit) b b.entries 0 false σ).1 ∧ + JAtZ (L := L) arI AR Z1 H1 Cs1 Ac1 Lst1 D1 r.1 r.2.1 τ ∧ VxE Z1 ∧ τ.curThread = σ.curThread ∧ + (∀ x : Fl4Spec.Tcb, σ.kheap ct.bv = some (.tcb x) → + ∃ y : Fl4Spec.Tcb, τ.kheap ct.bv = some (.tcb y) ∧ y.tcbState = x.tcbState) ∧ MR r.2.1 m0 ⦄ := by + let bt : vspace.Batch := { + root := p + asid := asid + op := op + depth := depth + map_rights := mr + cap_rights := cr + attr := attr + defer_delete := dd + cnode := cnode + dest_cnode := dcnode + } + let Inv := fun (x : S) (a : A) (τ : Fl4Spec.State Unit) => + FbMidJ (L := L) (AR := AR) arI σ Z.th ct buf args x a τ ∧ MR a m0 ∧ + ((∃ y, IsSlot Z.th y ∧ capV (L.mem x) y = cnode) ∧ (∃ y, IsSlot Z.th y ∧ capV (L.mem x) y = dcnode)) + have hn255 : n.val ≤ 255 := by rw [hn, UScalar.val_and]; exact Nat.le_trans Nat.and_le_right (by decide) + have hctT : ct ∈ Z.T := J.thT ct (J.xtop.curTcb (J.curEq hct).1).1 + have hns : Vs.NoSwitch σ := fun u e => by rw [hni.2] at e; cases e + have hInit : Inv s ar σ := ⟨⟨⟨Z, H, Cs, Ac, Lst, D, rfl, hctT, hbm, J, hLv, inferInstance⟩, + hargs, FbKeep.refl σ, hns, hsg, hp0⟩, hmr0, hRt0⟩ + refine WP.spec_mono (fb_loopWith arI Fl4Spec.capDelete Inv s ar σ ct buf n words args bt b hInit + hw hn255 hbe hln (fun _ _ _ h => h.1.2.1) ?_) ?_ + · intro x a τ h w w0 w1 w2 hw0 hw1 hw2 df + obtain ⟨hM, hmq, hRt⟩ := h + refine WP.spec_mono (WP.spec_and (fb_entryJ arI hA hT hDP hnd hARI C x a τ hM hRt + w w0 w1 w2 hw0 hw1 hw2 df) (MKD.vspace_frame_batch_entry L.total hT hKD (FbMidJ.delOK arI C hM _ _) hmq)) ?_ + rintro r ⟨⟨⟨τ1, hM1, hres⟩, hCK⟩, hmr1⟩ + exact ⟨τ1, ⟨hM1, hmr1, hCK.held hcnC hRt.1, hCK.held C.hdcC hRt.2⟩, hres⟩ + · rintro r ⟨τ, hmem, hM, hmr, -⟩ + obtain ⟨⟨Z1, H1, Cs1, Ac1, Lst1, D1, -, -, -, J1, -, hvx1⟩, -, hK1, -⟩ := hM + refine ⟨τ, Z1, H1, Cs1, Ac1, Lst1, D1, hmem, J1, hvx1, hK1.ct, ?_, hmr⟩ + intro x hx + obtain ⟨y, hy, hxy⟩ := hK1.tcb hx + exact ⟨y, hy, hxy.1⟩ /-- Reassociating a bind under a spec. -/ theorem fb_assoc_spec {α β γ : Type} {m : Result α} {g : α → Result β} {k : β → Result γ} {P : γ → Prop} @@ -479,7 +545,7 @@ theorem fb_loopJ [Fl4Proofs.P1.PlainStore st] (s : S) (ar : A) (σ : Fl4Spec.Sta apply BitVec.eq_of_toNat_eq; rw [BitVec.toNat_ofNat]; exact Nat.mod_eq_of_lt (by scalar_tac) have hperf : ([d1.bv, e1.bv], τ') ∈ (Fl4Spec.performFrameBatch (Z := Unit) b σ).1 := by rw [← hd] - unfold Fl4Spec.performFrameBatch + unfold Fl4Spec.performFrameBatch Fl4Spec.performFrameBatchWith exact ⟨_, hrun1, _, hτ', rfl⟩ obtain ⟨y', hky', -, hperf1⟩ := SInv.performFrameBatch_restart b hk hperf have hky2 : τ'.kheap ct.bv = some (.tcb y) := by rw [hkh]; exact hky diff --git a/proofs/Fl4Proofs/P3/XTopJointXferG.lean b/proofs/Fl4Proofs/P3/XTopJointXferG.lean index 33bf3a236..f9464c873 100644 --- a/proofs/Fl4Proofs/P3/XTopJointXferG.lean +++ b/proofs/Fl4Proofs/P3/XTopJointXferG.lean @@ -21,6 +21,7 @@ of a send to a waiting receiver, with the insert as a trace (IpcGrantTr). import Fl4Proofs.P3.XTopJointXferB import Fl4Proofs.P3.XTopJointGrant import Fl4Proofs.P3.IpcGrantTr +import Fl4Proofs.P3.IpcGrantTrV import Fl4Proofs.P3.BufJ open Aeneas Aeneas.Std Result @@ -315,6 +316,260 @@ theorem do_ipc_transfer_grantG (hH : IpcCore st N T E R B (SRT st th cr N T C E · obtain ⟨pd, h1, h2⟩ := hDL d hd exact ⟨pd, h1, h2.same (SlotsSame.symm' hS1)⟩ + +set_option maxHeartbeats 8000000 in +/-- **`do_ipc_transfer` with a receive buffer, caps granted, with the insert +trace** (module docstring), the insert record carrying `VtOK`. -/ +theorem do_ipc_transfer_grantGV (hH : IpcCore st N T E R B (SRT st th cr N T C E R B c (SRE st N T C E R B c))) + (hBf : EpBufHyps st T E R B (fun s σ => (SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c)) s σ ∧ + IpcBufCapOK σ) ∧ BufCover T B σ)) + (hBm : EpBufMapHyps st T E R B (fun s σ => (SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c)) s σ ∧ + IpcBufCapOK σ) ∧ BufCover T B σ)) + (hP0 : IPlaced N T E R B) (hP : EPlaced T E R B) + (s : S) (a : A) (σ : Fl4Spec.State Unit) (hR : EpStateRel T E R B (L.mem s) σ) + (hSR : SRN st N T (SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c))) s σ) + {snd rcv : U64} (hs : snd ∈ T) (hr : rcv ∈ T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) (hf : xs.tcbFault = none) + {rp : Fl4Spec.ObjRef} (hrb : ipcBufOf true xr = some rp) (hOK : IpcBufCapOK σ) (hBC : BufCover T B σ) (ep badge : U64) (grant : Bool) + {T1 : Set U64} (hT1 : P1.TcbPlaced T1) (hr1 : rcv ∈ T1) : + ipc.do_ipc_transfer st ar s a snd ep badge grant rcv ⦃ s' => ∃ (σ' : Fl4Spec.State Unit) (x' : Fl4Spec.Tcb), + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv (some ep.bv) badge.bv grant rcv.bv σ = ({((), σ')}, False) ∧ + EpStateRel T E R B (L.mem s') σ' ∧ + SRN st N T (SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c))) s' σ' ∧ + Agree (L.mem s) (L.mem s') (fun b => AllTF T b ∧ ¬ RcvReg rcv b ∧ ¬ BufW B b) ∧ + KeepNC σ σ' rcv.bv ∧ σ'.kheap rcv.bv = some (.tcb x') ∧ x' = { xr with tcbArch := x'.tcbArch } ∧ + WritesIn st s s' (fun b => XferRegW T1 ∅ b ∨ RBufW ⟨rp⟩ b ∨ Sched.SlotWd th b) ∧ + ((∃ um, σ' = Notif.updK (Endp.setUm σ um) rcv.bv (.tcb x') ∧ SlotsSame th (L.mem s) (L.mem s')) ∨ + ∃ (s1 : S) (x1 : Fl4Spec.Tcb) (um0 : Fl4Spec.Word → Fl4Spec.Word8) (caps : List (Fl4Spec.Cap × Fl4Spec.CslotPtr)) + (slots : List Fl4Spec.CslotPtr) (sk' : S) (σc : Fl4Spec.State Unit) (um : Fl4Spec.Word → Fl4Spec.Word8), + SlotsSame th (L.mem s) (L.mem s1) ∧ L.ksb s1 = L.ksb s ∧ + InsRecV (L := L) th caps slots s1 (Notif.updK (Endp.setUm σ um0) rcv.bv (.tcb x1)) sk' σc ∧ + σ' = Notif.updK (Endp.setUm σc um) rcv.bv (.tcb x') ∧ SlotsSame th (L.mem sk') (L.mem s') ∧ + (∀ j (hj : j < caps.length), ∃ ps, (caps[j]).2.1 = ps.bv ∧ DestCap th (L.mem s) ps) ∧ + (∀ d ∈ slots, ∃ pd, d.1 = pd.bv ∧ DestCap th (L.mem s) pd)) ⦄ := by + have hra := hP.tcb rcv hr + have hxs := hR.at_tcb hs hks + have hwmi : (L.mem s (regA snd layout.reg.MSG_INFO)).bv = xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register := by + rw [← regIdx_msgInfo]; exact hxs.regs _ + have hIR := (IpcRel.ofSRN hR hSR).1 + obtain ⟨⟨⟨G, hB⟩, hBw⟩, -⟩ := hSR.1.1 + have hEH := hH.epHyps hP0 + have hSRb : (SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c)) s σ ∧ IpcBufCapOK σ) ∧ + BufCover T B σ := ⟨⟨hSR.1, hOK⟩, hBC⟩ + have hu := hBm.frame s σ rcv xr rp hR hSRb hr hkr hrb + unfold ipc.do_ipc_transfer + have hlb := lookup_buf_full s σ hB.2.1.1 hB.2.1.2.1 (RelTopB.tcbT hB hkr) hkr hOK true + obtain ⟨x0, hk0, hbw⟩ := hBw.bw rcv hr + rw [hkr] at hk0; cases hk0 + refine WP.spec_bind (WP.spec_and (hBm.lookupMap s σ rcv xr true rp hR hSRb hr hkr hrb) hlb) ?_ + rintro rw ⟨⟨hrw0, hmr⟩, hwl⟩ + have hrwe : rw = ⟨rp⟩ := by + have hwb : rw.bv = rp := hwl.2 hbw rp hrb + rw [← hwb] + subst hrwe + have hal : ∀ i : U64, i.val < 128 → P1.Aligned (fl4BufA ⟨rp⟩ i) := fun i hi => (hP.balign _ (hmr i hi)).1 + step with Fl4Proofs.P3.Endp.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 + simp only [hft0, ↓reduceIte] + refine WP.spec_bind (lookup_send_buf hBf hBm s σ hR hSRb hs hks) ?_ + rintro sw hsw + unfold ipc.do_normal_transfer + refine WP.spec_bind (msg_info_spec (L := L) s snd) ?_ + rintro tag ⟨hlab, hlen, hext, hle⟩ + set w := xs.tcbArch.tcbContext.userRegs Fl4Spec.msg_info_register with hwdef + have hn3 : tag.extra_caps.val ≤ 3 := by + show tag.extra_caps.bv.toNat ≤ 3; rw [hext, hwmi]; exact extra_le3 w + have hsb' : sw = 0#u64 ↔ ipcBufOf false xs = none := by + rcases hsw with ⟨h1, h2⟩ | ⟨sp, h1, h2, -⟩ + · exact ⟨fun _ => h1, fun _ => h2⟩ + · rw [h1]; exact ⟨fun h => absurd h h2, fun h => by cases h⟩ + have hsm : ∀ sb, ipcBufOf false xs = some sb → BufMap B sw sb := by + intro sb h + rcases hsw with ⟨h1, -⟩ | ⟨sp, h1, -, h3⟩ + · rw [h1] at h; cases h + · rw [h1] at h; cases h; exact h3 + 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) ⦃ extra => ∃ caps, + (if grant = true then Fl4Spec.NonDetE.catch (Fl4Spec.lookupExtraCaps (Z := Unit) snd.bv (ipcBufOf false xs) + (Fl4Spec.dataToMessageInfo w)) (fun _ => pure []) else pure []) σ = ({(caps, σ)}, False) ∧ + ExtraOK th (L.mem s) extra caps ∧ ExtraLive th (L.mem s) (L.ksb s) extra ⦄ := by + have hnl : ExtraLive th (L.mem s) (L.ksb s) ipc.NO_EXTRA_CAPS := + fun j hj hne => absurd (Ipc.exAt_none j hj) hne + cases grant with + | false => + rw [if_neg (by decide), WP.spec_ok] + exact ⟨[], rfl, extraOK_none th _, hnl⟩ + | true => + rw [if_pos rfl] + refine WP.spec_bind (WP.spec_and (lookup_extra_top hP0 hB hIR (hB.tcbT hks) sw (ipcBufOf false xs) hsb' hsm + tag (Fl4Spec.dataToMessageInfo w) (by show tag.extra_caps.bv.toNat = _; rw [hext, hwmi]) hn3) + (lookup_extra_live th s σ hB.2.1.1 hB.2.1.2.1 (hB.tcbT hks) sw tag)) ?_ + rintro r ⟨⟨caps, hc, hok⟩, hl⟩ + cases r with + | Ok e => simp only [WP.spec_ok]; exact ⟨caps, by simp only [↓reduceIte]; exact hc, hok, hl _ rfl⟩ + | Err _ => simp only [WP.spec_ok]; exact ⟨caps, by simp only [↓reduceIte]; exact hc, hok, hnl⟩ + refine WP.spec_bind hex ?_ + rintro extra ⟨caps, hlk, hok, hlive⟩ + refine WP.spec_bind (WP.spec_and (copy_mrs_rel hEH hP s σ hR hSR.1 hs hr hne hks hkr hmr hrw0 hu hsw tag.length hle) + (copy_mrs_rgB hT1 hr1 hrw0 hal sw tag.length (snd := snd))) ?_ + rintro ⟨n, s1⟩ ⟨⟨σc, hcopy, hkcf, hRc, hSc, hAc, hn, hWc, hEc⟩, hF1⟩ + simp only at hcopy hkcf hRc hSc hAc hn hWc hEc hF1 + set x1 := copyRegsTcb xr xs.tcbArch.tcbContext.userRegs (Fl4Spec.msg_registers.take tag.length.val) with hx1 + have hkc : σc.kheap rcv.bv = some (.tcb x1) := by rw [hkcf]; simp + have hkco : ∀ p, p ≠ rcv.bv → σc.kheap p = σ.kheap p := fun p hp => by + rw [hkcf]; simp [Function.update_of_ne hp] + have hB1 : RelTopB (L := L) th G N T C E R B c s1 σc := hB.stableE hWc hEc + have hN1 : NtfnStateRel N T (L.mem s1) σc := + ntfn_lift_rcv hP0.toC hSR.2 hRc hr (hAc.mono fun b hb => ⟨fun ⟨r, _, h'⟩ => hb.1 ⟨r, h'⟩, hb.2⟩) hkco hkr hkc + (copyRegsTcb_state _ _ _) + have hIR1 : IpcRel N T E R B (L.mem s1) σc := IpcRel.of hN1 hRc hSc.2.all + have hSE1 : SRE st N T C E R B c s1 σc := hSc.1.2 + have hScb : (SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c)) s1 σc ∧ IpcBufCapOK σc) ∧ + BufCover T B σc := ⟨⟨hSc, Fl4Proofs.P3.ipcBufCapOK_epStep hOK hEc⟩, Fl4Proofs.P3.bufCover_epStep hBC hEc⟩ + have hS1 : SlotsSame th (L.mem s) (L.mem s1) := + slotsSame_of_iwords hB.1 hB.2.2.2.2.2.1 hWc (fun _ ha => EWrite.iwords ha) + have hok1 := hok.of_same hS1 + have hrb1 : ipcBufOf true x1 = some rp := by rw [ipcBufOf_copyRegs]; exact hrb + have hlenw : tag.length.bv = (Fl4Spec.dataToMessageInfo w).miLength := by rw [hlen, hwmi] + have hlabw : tag.label.bv = (Fl4Spec.dataToMessageInfo w).miLabel := by + rw [hlab, hwmi]; exact (label_mask w).symm + refine WP.spec_bind (transfer_caps_topGV hP0 hB1 hIR1 hSE1 tag (Fl4Spec.dataToMessageInfo w) hlenw hlabw hok1.1 + hok1.2.1 hok1.2.2 ep rcv ⟨rp⟩ (some rp) ⟨fun h => absurd h hrw0, fun h => by cases h⟩ + (fun rb h => by cases h; exact ⟨hmr, hBm.frame s1 σc rcv x1 rp hRc hScb hr hkc hrb1⟩) + (fun rb h => by cases h; exact hBm.recvSlots s1 σc rcv x1 rp hRc hScb hr hkc hrb1) + (fun rb h => by cases h; exact get_receive_slot_top hP0 hB1 hIR1.toE (hB1.tcbT hkc) hmr hrw0 caps extra) + (fun rb h => by cases h; exact get_receive_slot_live th s1 σc hB1.2.1.1 hB1.2.1.2.1 (hB1.tcbT hkc) ⟨rp⟩)) ?_ + rintro ⟨out, s2⟩ ⟨σt, mf, htc, hMi, hexc, hIRt, hcu, he3, hlbt, hSEt, ⟨hKC, hWt⟩, ⟨G', hBt⟩, htr, hWr⟩ + simp only at hMi hexc hIRt hcu he3 hSEt hWt hBt htr hWr + 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) ⦃ _ ⦄ + refine WP.spec_bind (set_msg_info_specG (L := L) s2 rcv { out with length := n } (by show n.val < 128; omega) + hcu (by show out.extra_caps.val < 4; omega)) ?_ + rintro s3 ⟨v, hs3, hv⟩ + step with Fl4Proofs.P3.Endp.obj.Tcb.set_reg.spec as ⟨s4, hs4⟩ + subst hs3 hs4 + have hkt : σt.kheap rcv.bv = some (.tcb x1) := by + rcases hKC.1 rcv.bv with e | ⟨r, cs, cs', h1, -⟩ + · rw [e, hkc] + · rw [hkc] at h1; cases h1 + have hcopy' : Fl4Spec.copyMrs (Z := Unit) snd.bv (ipcBufOf false xs) rcv.bv (some rp) + (Fl4Spec.dataToMessageInfo w).miLength σ = ({(n.bv, σc)}, False) := by + rw [← hlenw]; exact hcopy + have hspec := doIpcTransfer_grant hks hkr hf hrb (some ep.bv) badge.bv grant hlk hcopy' htc hkt + have hvx : v.bv = Fl4Spec.messageInfoToData + ⟨n.bv, mf.miExtraCaps, mf.miCapsUnwrapped, (Fl4Spec.dataToMessageInfo w).miLabel⟩ := by + rw [hv] + show Fl4Spec.messageInfoToData ⟨n.bv, out.extra_caps.bv, out.caps_unwrapped.bv, + out.label.bv &&& Fl4Spec.mask Fl4Spec.msgLabelBits⟩ = _ + rw [hexc, hMi.2.1, hMi.2.2, hlbt] + congr 2 + show ((w >>> 12) &&& Fl4Spec.mask Fl4Spec.msgLabelBits) &&& Fl4Spec.mask Fl4Spec.msgLabelBits = + (w >>> 12) &&& Fl4Spec.mask Fl4Spec.msgLabelBits + rw [BitVec.and_assoc, BitVec.and_self] + -- the two register writes + obtain ⟨hR2, hW2, hS2⟩ := write_reg_rel hP s2 _ hIRt.toE hr hkt Fl4Spec.msg_info_register v + rw [regIdx_msgInfo] at hR2 hW2 + have hkt2 : (updK σt rcv.bv (.tcb (setReg x1 Fl4Spec.msg_info_register v.bv))).kheap rcv.bv = + some (.tcb (setReg x1 Fl4Spec.msg_info_register v.bv)) := by simp + obtain ⟨hR3, hW3, hS3⟩ := write_reg_rel hP _ _ hR2 hr hkt2 Fl4Spec.badge_register badge + rw [regIdx_badge] at hR3 hW3 + have hfin : updK (updK σt rcv.bv (.tcb (setReg x1 Fl4Spec.msg_info_register v.bv))) rcv.bv + (.tcb (setReg (setReg x1 Fl4Spec.msg_info_register v.bv) Fl4Spec.badge_register badge.bv)) = + updK σt rcv.bv (.tcb (setReg (setReg x1 Fl4Spec.msg_info_register (Fl4Spec.messageInfoToData + ⟨n.bv, mf.miExtraCaps, mf.miCapsUnwrapped, (Fl4Spec.dataToMessageInfo w).miLabel⟩)) + Fl4Spec.badge_register badge.bv)) := by + simp only [updK, Function.update_idem]; rw [hvx] + rw [hfin] at hR3 + have hSRS2 : SRS st T (SRT st th cr N T C E R B c (SRE st N T C E R B c)) s2 σt := + ⟨⟨RelTopI.of_B (hSR.1.1.1.stableE hWc hEc) hBt hWt (fun b hb => by + rcases hb with h | ⟨wp, hw, rfl⟩ + · exact Or.inr (Or.inr (Or.inr h)) + · exact Or.inl (Or.inr (Or.inr (Or.inr (Or.inr ⟨wp, hw, le_refl _, by omega⟩))))) + (fun b hb => by + have hP := hSR.1.1.1.eplaced + rcases hb with h | ⟨wp, hw, rfl⟩ + · exact Or.inr (slotWd_nobuf hB.2.2.2.2.2.1 hB.1 hP.tcb h) + · exact Or.inr (NoBufW.out hP.tcb fun t ht => by have := hP.bt wp hw t ht; omega)) + hKC.vsKeep, hSEt⟩, ScRelM.of hIRt⟩ + have hSf := hEH.stable _ _ _ _ (hEH.stable _ _ _ _ hSRS2 hW2 hS2) hW3 hS3 + rw [hfin] at hSf + have hA2 : Agree (L.mem s2) (L.mem (L.wr (L.wr s2 (regA rcv layout.reg.MSG_INFO) v) (regA rcv layout.reg.BADGE) + badge)) (fun b => ¬ RcvReg rcv b) := by + intro b hb hn' + rw [← regIdx_msgInfo, ← regIdx_badge, + rd_wr_ne (a_reg hra Fl4Spec.badge_register).1 hb (fun h => hn' ⟨Fl4Spec.badge_register, by rw [← h]⟩), + rd_wr_ne (a_reg hra Fl4Spec.msg_info_register).1 hb (fun h => hn' ⟨Fl4Spec.msg_info_register, by rw [← h]⟩)] + have hNf := ntfn_lift_rcv hP0.toC hIRt.toN hR3 hr (hA2.mono fun _ h => h.1) + (fun p hp => by simp [updK, Function.update_of_ne hp]) hkt + (by simp only [updK_kheap, Function.update_self]; rfl) (by rw [setReg_state, setReg_state]) + -- the register writes keep the slots and lie in the receiver's registers + have hW24 : WritesIn st s2 (L.wr (L.wr s2 (regA rcv layout.reg.MSG_INFO) v) (regA rcv layout.reg.BADGE) badge) + (XferRegW T1 ∅) := + ((wr_reg_rg hT1 hr1 s2 (by simp [layout.reg.MSG_INFO, layout.reg.RSI]) v).trans + (wr_reg_rg hT1 hr1 _ (by simp [layout.reg.BADGE, layout.reg.RDI]) badge)).mono fun b hb => hb.elim id id + have hS24 : SlotsSame th (L.mem s2) + (L.mem (L.wr (L.wr s2 (regA rcv layout.reg.MSG_INFO) v) (regA rcv layout.reg.BADGE) badge)) := by + have hm1 : P1.Aligned (regA rcv layout.reg.MSG_INFO) := by + rw [← regIdx_msgInfo]; exact (a_reg hra _).1 + have hm2 : P1.Aligned (regA rcv layout.reg.BADGE) := by + rw [← regIdx_badge]; exact (a_reg hra _).1 + refine slotsSame_of_iwords hBt.1 hBt.2.2.2.2.2.1 ((writesIn_wr (v := v) hm1).trans (writesIn_wr (v := badge) hm2)) + fun b hb => Or.inl ⟨rcv, hr, ?_⟩ + rcases hb with rfl | rfl + · rw [← regIdx_msgInfo, regA_val hra (regIdx_lt _)]; have := regIdx_lt Fl4Spec.msg_info_register; omega + · rw [← regIdx_badge, regA_val hra (regIdx_lt _)]; have := regIdx_lt Fl4Spec.badge_register; omega + obtain ⟨um0, hσc⟩ : ∃ um, σc = Notif.updK (Endp.setUm σ um) rcv.bv (.tcb x1) := ⟨_, Fl4Proofs.P3.epStep_form hEc hkcf⟩ + refine ⟨_, _, hspec, hR3, ⟨hSf, hNf⟩, ?_, ?_, by simp only [updK_kheap, Function.update_self]; rfl, + Fl4Proofs.P3.setReg2_eqA xr _ _ _ _ _ _, ?_, ?_⟩ + · rintro b hb ⟨⟨y, hy, hty⟩, hnr, hnb⟩ + rw [hA2 b hb hnr] + have h12 : L.mem s2 b = L.mem s1 b := by + refine hWt.mem_eq hb fun hfw => ?_ + rcases hfw with hsw' | hbw + · obtain ⟨y', hy', g1, g2⟩ := slotWd_range hB.1 hsw' + have := hty.range + exact hB.2.2.2.2.2.1.slot b (Or.inl ⟨y, hy, by omega, by omega⟩) y' hy' ⟨g1, g2⟩ + · exact hnb hbw + rw [h12] + exact hAc b hb ⟨fun ⟨r, _, h'⟩ => hnr ⟨r, h'⟩, hnb⟩ + · intro p hp + simp only [updK_kheap, Function.update_of_ne hp] + rcases hKC.1 p with e | ⟨r, cs, cs', h1, h2⟩ + · left; rw [e, hkco p hp] + · right; left; exact ⟨r, cs, cs', by rw [← hkco p hp]; exact h1, h2⟩ + · refine ((hF1.trans hWr).trans hW24).mono fun b hb => ?_ + rcases hb with (h | h) | h + · rcases h with h | h + · exact Or.inl h + · exact Or.inr (Or.inl h) + · rcases h with h | h + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl h) + · exact Or.inl h + · rcases htr with ⟨⟨um, hσt⟩, hSS⟩ | ⟨rb, slots, hrb', hsl, ⟨sk', σcI, hrec, ⟨um, hσt⟩, hSSk⟩, hDL⟩ + · left + refine ⟨um, ?_, SlotsSame.trans' (SlotsSame.trans' hS1 hSS) hS24⟩ + rw [hσt, hσc] + exact updK_um_updK σ rcv.bv _ _ um um0 + · right + have hks1 : L.ksb s1 = L.ksb s := hWc.1 + refine ⟨s1, x1, um0, caps, slots, sk', σcI, um, hS1, hks1, hσc ▸ hrec, by rw [hσt], SlotsSame.trans' hSSk hS24, + fun j hj => ?_, fun d hd => ?_⟩ + · obtain ⟨ps, rs, idx, cv, hps, hls, hsl', hxa, -⟩ := hok.2.1 j hj + have hA : AbsSlot th (ps.bv, idx) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hne0 : exAt extra j ≠ 0#u64 := by rw [hxa]; exact hA.isSlot.ne_zero hB.1 + obtain ⟨ps', idx', he', hth', hD'⟩ := hlive j (by have := hok.1; omega) hne0 + have e := cslotAddr_inj hB.1 hA (⟨ps', rfl, Or.inl hth'⟩ : AbsSlot th (ps'.bv, idx')) (hxa.symm.trans he') + exact ⟨ps', by rw [hsl']; exact congrArg Prod.fst e, hD'⟩ + · obtain ⟨pd, h1, h2⟩ := hDL d hd + exact ⟨pd, h1, h2.same (SlotsSame.symm' hS1)⟩ + end GrantG end @@ -374,9 +629,20 @@ variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {a variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} +/-- **The base step of `xferStepGJX`** (Stage 3, the IPC-transfer bridge): `y` is `s` (no insert), or FL4's one +cap insert from the source slot `x` into the empty receive slot `d`, with its `MdbStep`, its slot-word writes, the +TCBs' own slots kept, and the inserted cap's VT-x view (`VtOK`, from `derive_cap`). -/ +def XferBase (Z : XSets) (s y : S) (σ σy : Fl4Spec.State Unit) (G' : MdbGhost) : Prop := + (y = s ∧ σy = σ ∧ G' = Z.G) ∨ + (MdbStep (L.mem s) Z.G (L.mem y) G' ∧ P1.WritesIn st s y (Sched.SlotWd Z.th) ∧ + (∀ n, n < tcbSlots → ∀ t, Z.th t = some .tcb → capAt (L.mem y) (slotOf t n) = capAt (L.mem s) (slotOf t n)) ∧ + ∃ cv c' x d, IsSlot Z.th x ∧ IsSlot Z.th d ∧ x ≠ d ∧ capV (L.mem s) x = cv ∧ capV (L.mem s) d = .Null ∧ + capV (L.mem y) d = c' ∧ capV (L.mem y) x = (fullF cv c').getD cv ∧ + (∀ z, IsSlot Z.th z → z ≠ x → z ≠ d → capV (L.mem y) z = capV (L.mem s) z) ∧ VtOK cv c') + set_option maxHeartbeats 64000000 in /-- **The transfer step of `send_ipc` to a waiting receiver at `J`, any -grant** (module docstring): on `IRel` inside the dequeued interval +grant, with the base step's data (`XferBase`)** (module docstring): on `IRel` inside the dequeued interval (`xferStepJ` without a receive buffer, `do_ipc_transfer_grantG` with one), P1's hole by `xfer_all`/`blkX_bufw` (an insert's slot words first by `capw_bx`, split by `mixS`); and a base `(y, σy)` for the way back to `JAtZ`: @@ -384,7 +650,7 @@ P1's hole by `xfer_all`/`blkX_bufw` (an insert's slot words first by same slot words as the actual insert's by `insWords_cong`) and the spec's `cap_insert` at `σ` (the actual one's outcome patched, `capInsert_patch`), from which the writes are again `CnW`/`KW` and the receiver's buffer words. -/ -theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) +theorem JAtZ.xferStepGJX (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) {e : U64} {ep0 : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint ep0)) (o : Fl4Spec.Endpoint) {s1 : S} (hR1 : EpStateRel Z.T Z.E Z.R Z.B (L.mem s1) (Notif.updK σ e.bv (.endpoint o))) (hS1 : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c @@ -403,6 +669,7 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) ipc.do_ipc_transfer st arI s1 a snd e badge grant rcv ⦃ s2 => ∃ (x' : Fl4Spec.Tcb) (um : Fl4Spec.Word → Fl4Spec.Word8) (y : S) (σy : Fl4Spec.State Unit) (G' : MdbGhost), JAtZ (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D y a σy ∧ + XferBase (L := L) Z s y σ σy G' ∧ Fl4Spec.doIpcTransfer (Z := Unit) snd.bv (some e.bv) badge.bv grant rcv.bv (Notif.updK σ e.bv (.endpoint o)) = ({((), Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x'))}, False) ∧ x' = { xr with tcbArch := x'.tcbArch } ∧ @@ -467,7 +734,7 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hSc2, hSg2, f2⟩ have hN2 := ntfn_lift_rcv (x' := x2) hP0.toC hS1.2 hR2 hr hA2 (fun p hp => by simp [hp]) hkr1 (by simp) (by rw [hx2]) - exact ⟨x2, um2, s, σ, Z.G, J, e2, hx2, hR2, ⟨hS2.1, hN2⟩, hA2.mono fun b hb => ⟨hb.2.1, hb.2.2⟩, + exact ⟨x2, um2, s, σ, Z.G, J, Or.inl ⟨rfl, rfl, rfl⟩, e2, hx2, hR2, ⟨hS2.1, hN2⟩, hA2.mono fun b hb => ⟨hb.2.1, hb.2.2⟩, (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => hb.imp_left cXr), footW_trans (footW_of F1) (w2.mono fun b hb => hb.imp_left kXr), h2, hB2, hX2, hSc2, hSg2, f2, rfl, KeepCV.refl σ, CnK.refl σ, fun q _ => rfl⟩ @@ -476,7 +743,7 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) (∀ e, (H e = some .ep ∨ H e = some .ntfn) → b.val + 8 ≤ e.val ∨ e.val + 16 ≤ b.val) ∧ b ∉ P1.Heap.frame H := fun b hb => ⟨(hUd b ⟨rp, hrb, mem_rbList.1 hb⟩).1.ksb hW1.1, (hUd b ⟨rp, hrb, mem_rbList.1 hb⟩).2⟩ - refine WP.spec_mono (do_ipc_transfer_grantG hH (epBufHyps_bufC Z.T) (epBufMapHyps_bufC hP0) hP0 hP s1 a _ hR1 hS1 + refine WP.spec_mono (do_ipc_transfer_grantGV hH (epBufHyps_bufC Z.T) (epBufMapHyps_bufC hP0) hP0 hP s1 a _ hR1 hS1 hs hr hne hks1 hkr1 hf hrb hOK1 hBC1 e badge grant hTP hr1) ?_ rintro s2 ⟨σ', x', hspec, hR2, hS2, hA2, -, -, hx', hWg, htr⟩ rcases htr with ⟨um, rfl, hSS⟩ | ⟨s1', x1, um0, caps, slots, sk', σc, um, hS1', hks1', hrec, rfl, hSSk, hsrcL, hDL⟩ @@ -489,13 +756,13 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) · exact hWg.2.2 b hb fun h' => h'.elim (fun h' => hn (Or.inl h')) fun h' => h'.elim (fun h' => hn (Or.inr h')) hsw obtain ⟨h2, hB2, hX2, hSc2, hSg2, -, -, -, -, f2⟩ := blkX_bufw (l := rbList ⟨rp⟩) h hB hX hSc hSg hU' (hWg'.mono fun b hb => hb.imp_right mem_rbList.2) - exact ⟨x', um, s, σ, Z.G, J, hspec, hx', hR2, hS2, hA2, + exact ⟨x', um, s, σ, Z.G, J, Or.inl ⟨rfl, rfl, rfl⟩, hspec, hx', hR2, hS2, hA2, (hW1.mono fun b hb => Or.inl hb).trans1 (hWg'.mono fun b hb => hb.imp cXr fun h' => ⟨rp, hrb, h'⟩), footW_trans (footW_of F1) (hWg'.mono fun b hb => hb.imp kXr fun h' => ⟨rp, hrb, h'⟩), h2, hB2, hX2, hSc2, hSg2, f2, rfl, KeepCV.refl σ, CnK.refl σ, fun q _ => rfl⟩ · -- one insert: the base `(y, σy)` obtain ⟨cv, c', a', ps, pd, rs, rd, idx, idxd, sk, σk, hps, hpd, hls, hld, ⟨j, hj, hsl⟩, hslots, hsrcC1, hsnn, - hdst1, hca, hcv, he', hcnn, hsh, hbd, hnu, hzb, hkey, hv, htu, hkr2, hnt2, huc, hnat, hSk, hiw, -, ⟨um1, hσk⟩, hci⟩ := hrec + hdst1, hca, hcv, he', hcnn, hsh, hbd, hnu, hzb, hkey, hv, htu, hkr2, hnt2, huc, hnat, hvt, hSk, hiw, -, ⟨um1, hσk⟩, hci⟩ := hrec have hX0 := J.xtop obtain ⟨hPl, ⟨hSt, hV, hM, hC, hI'⟩, -, -, -, -, -⟩ := hX0.rel have hAs : AbsSlot Z.th (ps.bv, idx) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ @@ -531,8 +798,8 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) obtain ⟨pd', hpd1, hDd⟩ := hDL (pd.bv, idxd) (by rw [hslots]; exact List.mem_singleton_self _) have epd : pd' = pd := P1.eq_of_bv hpd1.symm rw [epd] at hDd - obtain ⟨y, -, ⟨⟨σy, hci', hKCV, hCnK, hksy, -, -, -, G', Jy⟩, hiwy⟩, hwy⟩ := spec_exists - (WP.spec_and (WP.spec_and (J.insDJ hps hpd hls hld hsrcC hsnn hdst c' a' hca hcv he' hcnn hsh (Or.inl hbd) hut + obtain ⟨y, -, ⟨⟨σy, hci', hKCV, hCnK, hksy, hothy, hdcy, hscy, G', Jy, hmSy, hwSy, hTSy⟩, hiwy⟩, hwy⟩ := spec_exists + (WP.spec_and (WP.spec_and (J.insDJX hps hpd hls hld hsrcC hsnn hdst c' a' hca hcv he' hcnn hsh (Or.inl hbd) hut hzb hkey hv htu hkr2 hnt2 huc hnat (hDs.same (SlotsSame.symm' hIF.1)) (hDd.same (SlotsSame.symm' hIF.1))) (insert_mem hPl s c' _ _ hAs.isSlot hAd.isSlot hsd cv hsrcC hnxS)) (insert_ws hPl s c' _ _ hAs.isSlot hAd.isSlot cv hsrcC hnxW)) have hSy2 : SlotsSame Z.th (L.mem y) (L.mem s2) := @@ -632,7 +899,8 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) fun h' => h'.elim (fun h' => hn (Or.inr (mem_rbList.2 h'))) hsw obtain ⟨h2, hB2, hX2, hSc2, hSg2, -, -, -, -, f2⟩ := blkX_bufw (l := rbList ⟨rp⟩) hu1 hBu hXu hScu hSgu (fun b hb => ⟨(hU' b hb).1.ksb (mixS_ksb s1 s2 lI), (hU' b hb).2⟩) hwu - refine ⟨x', um, y, σ'', G', Jy, hspec, hx', hR2, hS2, hA2, ⟨?_, ?_, fun b hb hn => ?_⟩, + refine ⟨x', um, y, σ'', G', Jy, Or.inr ⟨hmSy, hwSy, hTSy, cv, c', _, _, hAs.isSlot, hAd.isSlot, hsd, + capV_of_capAt hsrcC, capV_of_capAt hdst, hdcy, hscy, hothy, hvt⟩, hspec, hx', hR2, hS2, hA2, ⟨?_, ?_, fun b hb hn => ?_⟩, ⟨?_, ?_, fun b hb hn => ?_⟩, h2, hB2, hX2, hSc2, hSg2, fun y' hy' K h1' h2' h3' => (f2 y' hy' K h1' h2' h3').trans (fu y' hy' K (by omega) h2' h3'), hksy, hKCV, hCnK, hyk⟩ @@ -659,6 +927,51 @@ theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) · exact hn (Or.inr ⟨rp, hrb, h'⟩) · exact hsw h' +/-- `JAtZ.xferStepGJX` without the base step's data (the statement of the original `JAtZ.xferStepGJ`). -/ +theorem JAtZ.xferStepGJ (J : JAtZ (L := L) arI AR Z H Cs Ac Lst D s a σ) + {e : U64} {ep0 : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint ep0)) (o : Fl4Spec.Endpoint) + {s1 : S} (hR1 : EpStateRel Z.T Z.E Z.R Z.B (L.mem s1) (Notif.updK σ e.bv (.endpoint o))) + (hS1 : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s1 (Notif.updK σ e.bv (.endpoint o))) + (hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) (hf : xs.tcbFault = none) + (badge : U64) (grant : Bool) {X : Set U64} {Q : U64 → U64 → List U64} {Rq : U64 → List U64} + (h : P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X s1 Q Rq) + (hB : P1.BoundInv st (P1.Heap.tcbs H) Ac s1) + (hX : P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s1) + (hSc : P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s1) + (hSg : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s1) : + ipc.do_ipc_transfer st arI s1 a snd e badge grant rcv ⦃ s2 => ∃ (x' : Fl4Spec.Tcb) + (um : Fl4Spec.Word → Fl4Spec.Word8) (y : S) (σy : Fl4Spec.State Unit) (G' : MdbGhost), + JAtZ (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D y a σy ∧ + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv (some e.bv) badge.bv grant rcv.bv (Notif.updK σ e.bv (.endpoint o)) = + ({((), Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x'))}, False) ∧ + x' = { xr with tcbArch := x'.tcbArch } ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s2) + (Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x')) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 + (Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x')) ∧ + Agree (L.mem s1) (L.mem s2) (fun b => AllTF Z.T b ∧ ¬ RcvReg rcv b ∧ ¬ BufW Z.B b) ∧ + P1.WritesIn st y s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xr b) ∧ + P1.WritesIn st y s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) y b ∨ RcvBufW xr b) ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X s2 Q Rq ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s2 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s2 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s2 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s2 ∧ + (∀ y' ∈ P1.Heap.tcbs H, ∀ K : U64, 3328 ≤ K.val → K.val < 4096 → K.val % 8 = 0 → + L.mem s2 (core.num.U64.wrapping_add y' K) = L.mem s1 (core.num.U64.wrapping_add y' K)) ∧ + L.ksb y = L.ksb s ∧ KeepCV σ σy ∧ CnK σ σy ∧ + (∀ q, (∀ r cs, σ.kheap q ≠ some (.cnode r cs)) → σy.kheap q = σ.kheap q) ⦄ := + WP.spec_mono (J.xferStepGJX hk o hR1 hS1 hW1 F1 hs hr hne hks hkr hf badge grant h hB hX hSc hSg) + fun _ ⟨x', um, y, σy, G', Jy, hb, hrest⟩ => ⟨x', um, y, σy, G', Jy, hrest⟩ + end XferG end diff --git a/proofs/Fl4Proofs/P3/XTopJointXferGB.lean b/proofs/Fl4Proofs/P3/XTopJointXferGB.lean new file mode 100644 index 000000000..a7b8a4cd7 --- /dev/null +++ b/proofs/Fl4Proofs/P3/XTopJointXferGB.lean @@ -0,0 +1,583 @@ +/- +P3 (line mmuAD, Stage 3 Wave 2, IPC-transfer bridge): **the send's transfer base step at `JAtZB`**. + +`JAtZB.insDJXB` and `JAtZB.xferStepGJXB` are `JAtZ.insDJX` (P3/XTopJointGrant) and `JAtZ.xferStepGJX` +(P3/XTopJointXferG) at the `ext`-free part of the joint relation (a `VmEptState`'s `JAtZB`): the same statements +and proofs, with `JAtZ` read as `JAtZB` and the `JExt` step dropped (generated from them by textual edits). + +No named premise. New file. +-/ +import Fl4Proofs.P3.XTopJointXferG +import Fl4Proofs.P3.XTopJointSendDon +open Aeneas Aeneas.Std Result + +namespace Fl4Proofs.P3 +open fl4_core Classical Fl4Proofs.P1 Fl4Proofs.P3.Vs Fl4Proofs.P3.Notif Fl4Proofs.P3.Endp Fl4Proofs.P3.Ipc Fl4Proofs.P3.Sched + +noncomputable section + +section +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 16000000 in +/-- **`cdt::insert` of a derived cap between two CNode slots at `J`** +(module docstring, at `JAtZB`: `JAtZ.insDJX` without `ext`): the spec's `cap_insert`, `KeepCV`, FL4's slots after +(`insert_trace`), and `JAtZ` after at a new MDB ghost. -/ +theorem JAtZB.insDJXB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {ps pd : U64} {rs rd : Nat} (hps : Z.th ps = some (.cnode rs)) (hpd : Z.th pd = some (.cnode rd)) + {idxs idxd : List Bool} (hls : idxs.length = rs) (hld : idxd.length = rd) + {srcC : capability.Cap} (hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idxs)) = ok srcC) + (hsnn : srcC ≠ .Null) + (hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null) + (cc : capability.Cap) (ac : Fl4Spec.Cap) (hca : CapRel cc ac) (hcv : CapValid Z.th cc) (he : EncWf cc) + (hcnn : cc ≠ .Null) (hsh : shape cc = shape srcC) (hbd : badgeF cc = badgeF srcC ∨ badgeF srcC = 0#u64) + (hut : isUt srcC = true → ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (ps.bv, idxs))) (hzb : isZb srcC = false) + (hkey : okey cc = okey srcC) + (hv : ∀ P L4 L3 L2 L1 : Set U64, Vs.VCapOK P L4 L3 L2 L1 srcC → Vs.VCapOK P L4 L3 L2 L1 cc) + (htu : Vs.TabUn cc → Vs.TabUn srcC) (hkr : Vs.Keyed2 cc srcC → cc = srcC) (hnt : ¬ Vs.TabUn cc) + (huc : isUt srcC = true → cc = srcC) (hnat : Vs.NativeCap srcC → Vs.NativeCap cc) + (hDs : DestCap Z.th (L.mem s) ps) (hDd : DestCap Z.th (L.mem s) pd) : + cdt.insert st s cc (cslotAddr (ps.bv, idxs)) (cslotAddr (pd.bv, idxd)) ⦃ s' => ∃ σ', + Fl4Spec.capInsert (Z := Unit) ac (ps.bv, idxs) (pd.bv, idxd) σ = ({((), σ')}, False) ∧ + KeepCV σ σ' ∧ Ipc.CnK σ σ' ∧ L.ksb s' = L.ksb s ∧ + (∀ y, IsSlot Z.th y → y ≠ cslotAddr (ps.bv, idxs) → y ≠ cslotAddr (pd.bv, idxd) → + capV (L.mem s') y = capV (L.mem s) y) ∧ + capV (L.mem s') (cslotAddr (pd.bv, idxd)) = cc ∧ + capV (L.mem s') (cslotAddr (ps.bv, idxs)) = (fullF srcC cc).getD srcC ∧ + ∃ G' : MdbGhost, JAtZB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D s' a σ' ∧ + MdbStep (L.mem s) Z.G (L.mem s') G' ∧ P1.WritesIn st s s' (Sched.SlotWd Z.th) ∧ + (∀ n, n < tcbSlots → ∀ t, Z.th t = some .tcb → + capAt (L.mem s') (slotOf t n) = capAt (L.mem s) (slotOf t n)) ⦄ := by + have hX := J.xtop + obtain ⟨hPl, ⟨hSt, hV, hM, hC, hI⟩, hFI, -, hMO, -, -⟩ := hX.rel + have hAs : AbsSlot Z.th (ps.bv, idxs) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hsm : cslotAddr (ps.bv, idxs) ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hAs.isSlot, srcC, hsrcC, hsnn⟩ + have hdnm : cslotAddr (pd.bv, idxd) ∉ Z.G.chains.flatten := fun h => by + obtain ⟨-, c0, h0, hn⟩ := (hM.members _).1 h; rw [hdst] at h0; exact hn (eq_of_ok_eq h0).symm + have hsd : cslotAddr (ps.bv, idxs) ≠ cslotAddr (pd.bv, idxd) := fun e => by + rw [e, hdst] at hsrcC; exact hsnn (eq_of_ok_eq hsrcC).symm + refine WP.spec_mono (WP.spec_and (WP.spec_and (insert_full_finG Z.th s σ Z.G hSt hV hPl hM hC hI hFI hps hpd hls + hld hsrcC hsnn hdst cc ac hca hcv he hcnn hsh hbd hut hzb hkey hv) + (Vs.insert_trace hPl s Z.G hM hsm hAd.isSlot hdnm hsrcC cc he)) + (insert_delinv_imp hPl s Z.G hM hsm hAd.isSlot hdnm hsrcC cc he hcnn (by rw [isZb_of_shape hsh]; exact hzb))) ?_ + rintro s' ⟨⟨⟨σ', G', hci, hSt', hV', hM', hC', hI', hFI', hstep, hG'⟩, hF, hoth, hdc, hsc⟩, hdi⟩ + have hmem : ((), σ') ∈ (Fl4Spec.capInsert (Z := Unit) ac (ps.bv, idxs) (pd.bv, idxd) σ).1 := by rw [hci]; rfl + have hcn := capInsert_nkn ac _ _ σ _ hmem + have hkc : KeepC σ σ' := keepC_of hcn (capInsert_nko ac _ _ σ _ hmem) (by + rintro p (rfl | hp) + · obtain ⟨cs, h, -⟩ := hSt.cnode ps rs hps; exact ⟨rs, cs, h⟩ + · rw [Set.mem_singleton_iff.1 hp]; obtain ⟨cs, h, -⟩ := hSt.cnode pd rd hpd; exact ⟨rd, cs, h⟩) + have hMO' : MdbOKV (L.ksb s) (L.mem s') G' := (hstep huc).okv hMO + have hw : P1.WritesIn st s s' (InSlot Z.th) := by + refine ⟨hF.1, hF.2.1, fun b hb hn => hF.2.2 b hb fun ⟨y, k, hy, hk', e⟩ => hn ⟨y, hy, ?_⟩⟩ + have hf := hy.fit hPl + have := hk'.lt + have hv := wordOf_val (a := y) (k := k) (by omega) + rw [e, hv]; omega + have hw' : P1.WritesIn st s s' (Sched.SlotWd Z.th) := + ⟨hw.1, hw.2.1, fun b hb hn => hw.2.2 b hb fun h => hn (Sched.slotSpan_wd hPl hb h)⟩ + have hsv : capV (L.mem s) (cslotAddr (ps.bv, idxs)) = srcC := capV_of_capAt hsrcC + have hdv : capV (L.mem s) (cslotAddr (pd.bv, idxd)) = .Null := capV_of_capAt hdst + have hsb : ∀ p sb fi d, srcC = .Untyped p sb fi d → sb.val < 64 := fun _ _ _ _ e => by + have h := hsrcC; rw [capAt_words, e] at h; exact (decode_encWf h).2 + have hTS : ∀ n, n < tcbSlots → ∀ t, Z.th t = some .tcb → + capAt (L.mem s') (slotOf t n) = capAt (L.mem s) (slotOf t n) := by + intro n hn t ht + have hi : IsSlotAt Z.th t n := Or.inr ⟨ht, hn⟩ + rw [capAt_capV, capAt_capV, hoth _ ⟨t, n, hi, rfl⟩ (slotOf_ne_cn hPl hi ht hps hls) + (slotOf_ne_cn hPl hi ht hpd hld)] + have hX1 := XTopAt.slotStep hX hw' hcn hkc ⟨hSt', hV', hM', hC', hI'⟩ hFI' hMO' (hTS 1 (by decide)) + have hCU := coverUt_ins hAs.isSlot hsd hsv hdv hdc hsc hoth hsh huc hsb + have horig : ∀ sl, σ'.isOriginalCap sl = true → σ.isOriginalCap sl = true ∨ AbsSlot Z.th sl := by + intro sl h + by_cases e : sl = (pd.bv, idxd) + · right; rw [e]; exact hAd + · left; rw [← capInsert_orig ac _ _ σ ((), σ') hmem sl e]; exact h + have hUS : UtSrc Z.th (L.mem s) (L.mem s') σ σ' := + utSrc_ins hPl hM hC hC' hAs hsv hdv hdc hsc hoth hsh huc hut hsb hG' + (fun hu => by rw [huc hu]; exact isPF_ut hI hsm hsv hu _) + have hW : ∀ sl ∈ [(ps.bv, idxs), (pd.bv, idxd)], ∃ p r, sl.1 = p.bv ∧ Z.th p = some (.cnode r) ∧ + sl.2.length = r ∧ DestCap Z.th (L.mem s) p := by + intro sl hsl + simp only [List.mem_cons, List.not_mem_nil, or_false] at hsl + rcases hsl with rfl | rfl + exacts [⟨ps, rs, rfl, hps, hls, hDs⟩, ⟨pd, rd, rfl, hpd, hld, hDd⟩] + have hWC := hW + replace hW : ∀ sl ∈ [(ps.bv, idxs), (pd.bv, idxd)], ∃ p r, sl.1 = p.bv ∧ Z.th p = some (.cnode r) ∧ + sl.2.length = r ∧ DestLive Z.th (L.mem s) (L.ksb s) p := fun sl hsl => by + obtain ⟨p, r, h1, h2, h3, h4⟩ := hWC sl hsl + exact ⟨p, r, h1, h2, h3, h4.live _⟩ + have hcW : ∀ y, IsSlot Z.th y → (∀ sl ∈ [(ps.bv, idxs), (pd.bv, idxd)], y ≠ cslotAddr sl) → + capV (L.mem s') y = capV (L.mem s) y := + fun y hy hne => hoth y hy (hne _ (by simp)) (hne _ (by simp)) + have hoW : ∀ sl, σ'.isOriginalCap sl = true → σ.isOriginalCap sl = true ∨ sl ∈ [(ps.bv, idxs), (pd.bv, idxd)] := by + intro sl h + by_cases e : sl = (pd.bv, idxd) + · right; rw [e]; simp + · left; rw [← capInsert_orig ac _ _ σ ((), σ') hmem sl e]; exact h + have hkcT : KeepCT σ σ' := KeepCT.of_cnK hcn + have hX1' : XTopAt (L := L) AR (Z.mv G' Z.vs) s' a σ' := hX1 + have hgp : σ'.archState.x64GlobalPml4 = σ.archState.x64GlobalPml4 := by + have e := congrArg (fun z => z.archState.x64GlobalPml4) hcn.1; exact e + have hutc := J.cn_utc arI [(ps.bv, idxs), (pd.bv, idxd)] hW hcW hoW hUS hw hgp hM' + have hKd : P1.DbgKeep st (P1.Heap.tcbs H) Cs s s' := dbgKeep_of_writes J.blk hw fun _ hb => J.inSlot_notDbg hb + have hjc := J.jcov_cover hw.1 hM' hCU.1 hCU.2 + (fun q hq => J.deadP_slot hX1' hcn hkcT hw (J.jcov.dead q hq)) + (J.idleW_keep hX1'.fin.1.1.1 (CnStep.of_cnK hcn).idle (CnStep.of_cnK hcn).isc) + (DXKeep.of_dbg hKd (J.tdkeep_slot hw)) + have hfh' : ∀ t, Z.th t = some .tcb → capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 3)) = .Null ∨ + ∃ ep b0 b1 b2 b3 b4, capV (L.mem s') (cslotAddr (t.bv, Fl4Spec.tcbCnodeIndex 3)) = + .Endpoint ep b0 b1 b2 b3 b4 := by + intro t ht + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + have e := hTS 3 (by decide) t ht + rw [capAt_capV, capAt_capV] at e + rw [cslot_tcb3, eq_of_ok_eq e, ← cslot_tcb3]; exact hJ.fh t ht + obtain ⟨hJC, R⟩ := jowes_slotDFB J hX1' hcn hkcT hw hFI' hMO' (capInsert_nkz ac _ _ σ _ hmem) + (typedKept_ins hAs.isSlot hsv hoth hdc hsc hsh) hfh' + (let ⟨_, _, hJ⟩ := J.jpl; hJ.bfsT fun t ht => Or.inl (hTS 2 (by decide) t ht)) (hdi J.del) hCU.2 hjc horig hutc + have hvT := vsSlotsT_insert hAs.isSlot hoth hdc (by rw [hsv]; exact hsc) (by rw [hsv]; exact hv) + (by rw [hsv]; exact htu) (by rw [hsv]; exact hkey) (by rw [hsv]; exact hnat) + -- `LiveDN` after (LiveFrame's `wstepC`: both written slots in CNodes named by CNode caps) + have hsA : CapSimL ((fullF srcC cc).getD srcC) srcC := capSimL_fullF srcC cc + have hZf : ∀ y, IsSlot Z.th y → ∀ p zt n, capV (L.mem s') y = .Zombie p zt n → + ∃ y0, IsSlot Z.th y0 ∧ capV (L.mem s) y0 = .Zombie p zt n := fun y hy p zt n h => by + by_cases eD : y = cslotAddr (pd.bv, idxd) + · subst eD; rw [hdc] at h; subst h + have := isZb_of_shape hsh; rw [hzb] at this; simp [isZb] at this + by_cases eS : y = cslotAddr (ps.bv, idxs) + · subst eS; rw [hsc] at h + exact ⟨_, hAs.isSlot, by rw [hsv]; exact (hsA.zombie p zt n).1 h⟩ + · exact ⟨y, hy, by rw [← hoth y hy eS eD]; exact h⟩ + have hLv' := LiveDN.wstepCB J J.live hX1' + (fun q hq => J.deadP_slot hX1' hcn hkcT hw (J.jcov.dead q (J.zD q hq))) hcn.idle hw + [(ps.bv, idxs), (pd.bv, idxd)] hWC hcW hoW + (fun sl hsl h => by + exfalso + simp only [List.mem_cons, List.not_mem_nil, or_false] at hsl + rcases hsl with rfl | rfl + · rw [hsc] at h; exact hsnn ((hsA.null).1 h) + · rw [hdc] at h; exact hcnn h) + hZf + (fun y0 hy0 hn => by + by_cases eD : y0 = cslotAddr (pd.bv, idxd) + · subst eD; exact absurd hdv hn + by_cases eS : y0 = cslotAddr (ps.bv, idxs) + · subst eS + exact ⟨_, hAd.isSlot, by rw [hdc]; exact hcnn, by rw [hdc, hsv]; exact hsh⟩ + · exact ⟨y0, hy0, by rw [hoth y0 hy0 eS eD]; exact hn, by rw [hoth y0 hy0 eS eD]⟩) + refine ⟨σ', hci, hcn.keepCV hkc, hcn, hw.1, hoth, hdc, hsc, G', ?_, hstep huc, hw', hTS⟩ + exact + J.cs_frameV (hvn := J.vsShr hX1' hw.1 + (Vs.vsKeep_insert hAs.isSlot hoth hdc (by rw [hsv]; exact hsc) (by rw [hdv]; rfl) (by rw [hsv]; exact hkr) hnt) + (Vs.ArchShr.of_objCn hcn.2 (by have e := congrArg Fl4Spec.AbstractState.archState hcn.1; exact e))) + hw.1 hX1' (KBack.of_keepCT hkcT) + (J.bfK (J.slot2Fr hkcT hX1'.rel.2.1.1 fun q hq => Or.inl (hTS 2 (by decide) q hq)) hvT) hJC R + (J.vsc.step hvT) hLv' (fun p hp => J.zD p (ZTcb.of_zombies hZf hp)) (J.carry.of_objCn hkcT) + (devFrom_kept J.mdbL hM' J.ut hCU.2) (J.carry.vsStep rfl hvT subset_rfl subset_rfl subset_rfl subset_rfl rfl hw.1 (ArchFwd.of_objCn hkcT)) (ArchFwd.of_objCn hkcT) + +set_option maxHeartbeats 8000000 in +/-- **A wake in the frame `DWFr` up to `σa`, then a spec step to `σ'`, keeps +`JAtZ`** (XTopJointSendCall's `JAtZ.wake_postCI` for any step `σa → σ'` +with its frame facts; the scheduler invariants after given). -/ +theorem JAtZB.wake_postGB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) {n t : Fl4Spec.ObjRef} + {s' : S} {σa σ' : Fl4Spec.State Unit} + (hIR : IRel st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c s' σ') + (hF : IFrame (L := L) Z.th s s') (hw : P1.WritesIn st s s' (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (hblk : P1.BlkOK st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H (P1.Heap.frame H) (P1.Heap.replies H) + (P1.Heap.scs H) (P1.Heap.ntfns H) (P1.Heap.scBits H) Ac s') + (hfoot : P1.WritesIn st s s' (P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) + (P1.EH H) s)) + (F : DWFr n t σ σa) (G : SkD σa σ') (hks1 : KSub σa σ') (hsz1 : SzKeep σa σ') (hyk1 : YKeep σa σ') + (hmk1 : McpKeep σa σ') + (hbk1 : ∀ p x, σa.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbIpcBuffer = x.tcbIpcBuffer) + (hrb1 : KhRB σa σ') (hkz1 : KSz σa σ') (hkb1 : KBack σa σ') (hfk1 : IpcFrKeep σa σ') + (hck1 : (∀ q y n, σa.kheap q = some (.schedContext y n) → ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ + y'.scReplies = y.scReplies) ∧ + (∀ q x, σa.kheap q = some (.reply x) → ∃ x', σ'.kheap q = some (.reply x') ∧ x'.replySc = x.replySc) ∧ + (∀ p x n, σ'.kheap p = some (.schedContext x n) → ∃ x0, σa.kheap p = some (.schedContext x0 n))) + (hrep1 : ∀ q x, σ'.kheap q = some (.reply x) → σa.kheap q = some (.reply x)) + (hao1 : ∀ q ao, σa.kheap q = some (.archObj ao) → σ'.kheap q = some (.archObj ao)) + (hkq : ∀ q ∈ D, σ'.kheap q.bv = σa.kheap q.bv) + (I3 : Inv σ') (B3 : BlockedScActive σ') (hAI3 : ActIdle σ') + (hcur3 : σ'.schedulerAction = .chooseNewThread ∨ CurInv σ') (hrun3 : CurRunM σ') + (hown3 : CurOwnW Z.T Z.C σ') (hoff3 : OffOK σ') (hnsc3 : NSC Z.N Z.C σ') (hiso3 : IpcSorted σ') + (hRR : ∀ d ∈ Z.R, ∀ xr, σ.kheap d.bv = some (.reply xr) → xr.replyTcb = some t → + Detached Z.C Lst d ∧ L.mem s (rNextA d) = 0#u64 ∧ L.mem s (rPrevA d) = 0#u64) + (ht : ∀ q ∈ D, q.bv ≠ t) : + ∃ V' : XVs, JAtZB (L := L) arI AR (Z.mv Z.G V') H Cs Ac Lst D s' a σ' := by + have hAR : AR a σ.archState.x64CurrentCr3 := J.xtop.fin.1.1.2.2.2.2.2.2 + obtain ⟨V', hX3⟩ := XTopAt.irel_same J.xtop hIR hAR hF.1 (G.cdt.trans F.sk.cdt) (G.orig.trans F.sk.orig) + (F.ksub.trans hks1) hF.2 + have hks : L.ksb s' = L.ksb s := hF.2 + obtain ⟨k1, k2, kb, k3, k4⟩ := J.cnW_keeps hw + have ck0 := F.callKeep + have ck : (∀ q y n, σ.kheap q = some (.schedContext y n) → ∃ y', σ'.kheap q = some (.schedContext y' n) ∧ + y'.scReplies = y.scReplies) ∧ + (∀ q x, σ.kheap q = some (.reply x) → ∃ x', σ'.kheap q = some (.reply x') ∧ x'.replySc = x.replySc) ∧ + (∀ p x n, σ'.kheap p = some (.schedContext x n) → ∃ x0, σ.kheap p = some (.schedContext x0 n)) := by + refine ⟨fun q y n hq => ?_, fun q x hq => ?_, fun p x n hp => ?_⟩ + · obtain ⟨y', h1, h2⟩ := ck0.1 q y n hq + obtain ⟨y'', h1', h2'⟩ := hck1.1 q y' n h1 + exact ⟨y'', h1', h2'.trans h2⟩ + · obtain ⟨x', h1, h2⟩ := ck0.2.1 q x hq + obtain ⟨x'', h1', h2'⟩ := hck1.2.1 q x' h1 + exact ⟨x'', h1', h2'.trans h2⟩ + · obtain ⟨x0, h0⟩ := hck1.2.2 p x n hp + exact ck0.2.2 p x0 n h0 + have hCR' : CallRel (L.mem s') Z.R Z.C σ' Lst := + J.call.frame k3 k4 (fun q _ y n hy => ck.1 _ y n hy) (fun d _ x hx => ck.2.1 _ x hx) + have hRP' := J.rp.frame hks ck.2.2 + have hRF' : RFree (L.mem s') Z.R Z.C σ' Lst := by + intro d hd hfd + obtain ⟨y, hy, hyt⟩ := hfd + have hy' : σa.kheap d.bv = some (.reply y) := hrep1 _ _ hy + obtain ⟨x, hx, -, hc⟩ := F.replyB hy' + obtain ⟨e1, e2⟩ := k4 d hd + rcases hc with e | ⟨hxt, -⟩ + · obtain ⟨h1, h2, h3⟩ := J.rfree d hd ⟨x, hx, e ▸ hyt⟩ + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + · obtain ⟨h1, h2, h3⟩ := hRR d hd x hx hxt + exact ⟨h1, by rw [e2]; exact h2, by rw [e1]; exact h3⟩ + have hDbg := J.kw_dbg hfoot + have hKd := J.kw_dbgKeep hfoot + have hS3 : SchedRel st Z.N Z.T Z.C Z.c s' σ' := hIR.2.2.1 + have hidl0 : σ'.idleThread = σ.idleThread := G.idle.trans F.sk.idle + have hisc0 : σ'.idleSc = σ.idleSc := G.isc.trans F.sk.isc + have hdead : ∀ q ∈ D, DeadP (L := L) H s' q := + J.deadP_dfrCI F hkq hidl0 hS3 hIR.1 hw k3 hAI3.1 + (J.yld.frame k2 (F.ykeep.trans hyk1)) (F.ykeep.trans hyk1) ht + have hsz : SzKeep σ σ' := fun p y n hp => by + obtain ⟨y0, hy0⟩ := hsz1 p y n hp + exact F.szKeep p y0 n hy0 + have hjpl : ∃ Tr Cq, JPlaced (L := L) Z.th H Tr Cq s' σ' := by + obtain ⟨Tr, Cq, hJ⟩ := J.jpl + exact ⟨Tr, Cq, hJ.same hF.1 (fun q y n _ hq => hsz q.bv y n hq) hks (J.cnW_irq hw) + (J.idleW_keep hS3 hidl0 hisc0).2⟩ + have hkern := J.kern_keep hks hw.2.1 hS3 (G.ct.trans F.sk.ct) (G.csc.trans F.sk.csc) + (fun sl h => Or.inl (by rw [← F.sk.orig, ← G.orig]; exact h)) + (by rw [G.arch, F.sk.arch]) + (fun q ao h => hao1 q ao (F.othF h (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))) hDbg + (D' := D) + have hRR' := J.rrecv.frame k4 (F.khrb.trans hrb1) + have hbuf : ∀ p x, σ.kheap p = some (.tcb x) → ∃ y, σ'.kheap p = some (.tcb y) ∧ y.tcbIpcBuffer = x.tcbIpcBuffer := + fun p x hp => by + obtain ⟨y, hy, e⟩ := F.bufKeep p x hp + obtain ⟨z, hz, e'⟩ := hbk1 p y hy + exact ⟨z, hz, e'.trans e⟩ + have R : JRest (L := L) arI (Z.mv Z.G V') H Cs Ac Lst D s' σ' := + ⟨hCR', hRF', hRR', J.yld.frame k2 (F.ykeep.trans hyk1), + J.mcp.frame k1 (F.mcpKeep.trans hmk1), J.buf.frame kb hbuf, hRP', + J.apart.sc_frame hks hsz, hjpl, J.jcov_keep hks hF.1 hdead (J.idleW_keep hS3 hidl0 hisc0) + (DXKeep.of_dbg hKd (J.cnW_td hw)), hkern, + J.cnW_utc hw hF.1 (G.cdt.trans F.sk.cdt) (G.orig.trans F.sk.orig) (G.arch.trans F.sk.arch) hDbg, + hnsc3, I3, hcur3, hown3, hoff3, hAI3, B3, hiso3, hrun3, hblk⟩ + have hC0 := JCs.sameB J hF.1 (F.ksz.trans hkz1) + have hC : JCs (L := L) (Z.mv Z.G V') s' σ' := ⟨hC0.del, hC0.ut, hC0.finM, hC0.sfree⟩ + exact ⟨Z.vs, J.cs_frameI hks hX3 hw (fun b hb => Ipc.CnW.oobj J.xtop.fin.1.1.1 hb) (fun b hb => J.cnW_nv hb) (G.arch.trans F.sk.arch) (fun q ao h => hao1 q ao (F.othF h (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))) hsz hF.1 (F.kback.trans' hkb1) (F.ipcFr.trans hfk1) hC R hKd (G.orig.trans F.sk.orig) (J.idleW_keep hS3 hidl0 hisc0).1 J.zD⟩ + +end + +section XferG +open Fl4Proofs.P1 +variable {S : Type} {st : machine.Store S} [L : P1.LawfulStore st] {A : Type} {arI : machine.Arch A} +variable {AR : A → Fl4Spec.Cr3 → Prop} {Z : XSets} {H : P1.Heap} {Cs Ac : Set U64} + {Lst : U64 → List U64} {D : Set U64} {s : S} {a : A} {σ : Fl4Spec.State Unit} + +set_option maxHeartbeats 64000000 in +/-- **The transfer step of `send_ipc` to a waiting receiver at `J`, any +grant, with the base step's data (`XferBase`)** (module docstring): on `IRel` inside the dequeued interval +(`xferStepJ` without a receive buffer, `do_ipc_transfer_grantG` with one), +P1's hole by `xfer_all`/`blkX_bufw` (an insert's slot words first by +`capw_bx`, split by `mixS`); and a base `(y, σy)` for the way back to `JAtZ`: +`(s, σ)` without an insert, else FL4's insert redone at `s` (`JAtZ.insDJ`, the +same slot words as the actual insert's by `insWords_cong`) and the spec's +`cap_insert` at `σ` (the actual one's outcome patched, `capInsert_patch`), +from which the writes are again `CnW`/`KW` and the receiver's buffer words. -/ +theorem JAtZB.xferStepGJXB (J : JAtZB (L := L) arI AR Z H Cs Ac Lst D s a σ) + {e : U64} {ep0 : Fl4Spec.Endpoint} (hk : σ.kheap e.bv = some (.endpoint ep0)) (o : Fl4Spec.Endpoint) + {s1 : S} (hR1 : EpStateRel Z.T Z.E Z.R Z.B (L.mem s1) (Notif.updK σ e.bv (.endpoint o))) + (hS1 : SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s1 (Notif.updK σ e.bv (.endpoint o))) + (hW1 : P1.WritesIn st s s1 (CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s σ)) + (F1 : P1.Foot st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s s1) + {snd rcv : U64} (hs : snd ∈ Z.T) (hr : rcv ∈ Z.T) (hne : snd ≠ rcv) {xs xr : Fl4Spec.Tcb} + (hks : σ.kheap snd.bv = some (.tcb xs)) (hkr : σ.kheap rcv.bv = some (.tcb xr)) (hf : xs.tcbFault = none) + (badge : U64) (grant : Bool) {X : Set U64} {Q : U64 → U64 → List U64} {Rq : U64 → List U64} + (h : P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X s1 Q Rq) + (hB : P1.BoundInv st (P1.Heap.tcbs H) Ac s1) + (hX : P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s1) + (hSc : P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s1) + (hSg : P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s1) : + ipc.do_ipc_transfer st arI s1 a snd e badge grant rcv ⦃ s2 => ∃ (x' : Fl4Spec.Tcb) + (um : Fl4Spec.Word → Fl4Spec.Word8) (y : S) (σy : Fl4Spec.State Unit) (G' : MdbGhost), + JAtZB (L := L) arI AR (Z.mv G' Z.vs) H Cs Ac Lst D y a σy ∧ + XferBase (L := L) Z s y σ σy G' ∧ + Fl4Spec.doIpcTransfer (Z := Unit) snd.bv (some e.bv) badge.bv grant rcv.bv (Notif.updK σ e.bv (.endpoint o)) = + ({((), Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x'))}, False) ∧ + x' = { xr with tcbArch := x'.tcbArch } ∧ + EpStateRel Z.T Z.E Z.R Z.B (L.mem s2) + (Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x')) ∧ + SRN st Z.N Z.T (SRS st Z.T (SRT st Z.th σ.archState.x64CurrentCr3 Z.N Z.T Z.C Z.E Z.R Z.B Z.c + (SRE st Z.N Z.T Z.C Z.E Z.R Z.B Z.c))) s2 + (Notif.updK (Endp.setUm (Notif.updK σy e.bv (.endpoint o)) um) rcv.bv (.tcb x')) ∧ + Agree (L.mem s1) (L.mem s2) (fun b => AllTF Z.T b ∧ ¬ RcvReg rcv b ∧ ¬ BufW Z.B b) ∧ + P1.WritesIn st y s2 (fun b => CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c y σy b ∨ RcvBufW xr b) ∧ + P1.WritesIn st y s2 (fun b => P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) (P1.Heap.scs H) + (P1.Heap.scBits H) (P1.EH H) y b ∨ RcvBufW xr b) ∧ + P1.SchedQueuesInvX st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) H X s2 Q Rq ∧ + P1.BoundInv st (P1.Heap.tcbs H) Ac s2 ∧ + P1.XInv st (P1.Heap.tcbs H) Cs (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.replies H) (P1.Heap.scs H) + (P1.Heap.ntfns H) (P1.Heap.scBits H) s2 ∧ + P1.SCsOK st (P1.Heap.tcbs H) (P1.Heap.omega H) (P1.Heap.frame H) (P1.Heap.scs H) (P1.Heap.scBits H) Ac s2 ∧ + P1.SgOK st (P1.Heap.omega H) (P1.Heap.frame H) s2 ∧ + (∀ y' ∈ P1.Heap.tcbs H, ∀ K : U64, 3328 ≤ K.val → K.val < 4096 → K.val % 8 = 0 → + L.mem s2 (core.num.U64.wrapping_add y' K) = L.mem s1 (core.num.U64.wrapping_add y' K)) ∧ + L.ksb y = L.ksb s ∧ KeepCV σ σy ∧ CnK σ σy ∧ + (∀ q, (∀ r cs, σ.kheap q ≠ some (.cnode r cs)) → σy.kheap q = σ.kheap q) ⦄ := by + have hI := J.xtop.irelK J.ks J.buf + have hP0 := J.ip + have hP := hP0.e + have hH := ipcCore_top (st := st) (th := Z.th) (cr := σ.archState.x64CurrentCr3) (N := Z.N) (C := Z.C) + (c := Z.c) hP + have hTe : Z.T = P1.Heap.tcbs H := J.tcbs_eq.1 + have hTP : P1.TcbPlaced (P1.Heap.tcbs H) := h.tcbs + have hr1 : rcv ∈ P1.Heap.tcbs H := J.zH.1 rcv hr + have hse : snd.bv ≠ e.bv := kheap_ne hks hk (fun _ h1 h2 => by subst h1; cases h2) + have hre : rcv.bv ≠ e.bv := kheap_ne hkr hk (fun _ h1 h2 => by subst h1; cases h2) + have hks1 : (Notif.updK σ e.bv (.endpoint o)).kheap snd.bv = some (.tcb xs) := by simp [hse, hks] + have hkr1 : (Notif.updK σ e.bv (.endpoint o)).kheap rcv.bv = some (.tcb xr) := by simp [hre, hkr] + obtain ⟨hOK, hBC, -⟩ := J.xferH + have hOK1 : Endp.IpcBufCapOK (Notif.updK σ e.bv (.endpoint o)) := ipcBufCapOK_epStep hOK (EpStep.ep hk) + have hBC1 : Endp.BufCover Z.T Z.B (Notif.updK σ e.bv (.endpoint o)) := bufCover_epStep hBC (EpStep.ep hk) + have hdth : Z.th rcv = some .tcb := by + obtain ⟨⟨⟨G0, hB0'⟩, -⟩, -⟩ := (IpcRel.toSRN hI.1 hI.2).1.1 + exact Ipc.RelTopB.tcbT hB0' hkr + have hUd := J.rbufU hr hdth hkr + have hIF : IFrame (L := L) Z.th s s1 := IRel.iframe_of hI hW1 fun b hb => CnW.oobj hI.2.2.1 hb + have cXr : ∀ {s' : S} {σ' : Fl4Spec.State Unit} {b : U64}, XferRegW (P1.Heap.tcbs H) ∅ b → + CnW (st := st) Z.T Z.N Z.C Z.E Z.R Z.c s' σ' b := by + intro s' σ' b hb + rcases hb with ⟨y, hy, K, h1', h2', -, eb⟩ | hb + · have hyT : y ∈ Z.T := by rw [hTe]; exact hy + refine Or.inl (Or.inr ⟨y, hyT, K.val, h1', by omega, by omega, by omega, by omega, ?_⟩) + rw [eb]; exact tfield_val (hP.tcb y hyT) (by omega) + · exact absurd hb (Set.notMem_empty _) + have kXr : ∀ {s' : S} {b : U64}, XferRegW (P1.Heap.tcbs H) ∅ b → P1.KW st (P1.Heap.tcbs H) Cs (P1.Heap.frame H) + (P1.Heap.scs H) (P1.Heap.scBits H) (P1.EH H) s' b := by + intro s' b hb + rcases hb with ⟨y, hy, K, h1', h2', h3', eb⟩ | hb + · exact Or.inl ⟨y, hy, K, by omega, by omega, by omega, by omega, eb⟩ + · exact absurd hb (Set.notMem_empty _) + cases hrb : Endp.ipcBufOf true xr with + | none => + refine WP.spec_mono (xferStepJ hP0 hP (hH.epHyps hP0) s1 a _ hR1 hS1 hOK1 hBC1 hs hr hne hks1 hkr1 hf e badge + grant (Or.inr (Or.inr (Or.inr hrb))) hTP hr1 h hB hX hSc hSg + (fun b hb => ⟨(hUd b hb).1.ksb hW1.1, (hUd b hb).2⟩)) ?_ + rintro s2 ⟨x2, um2, e2, hx2, hR2, hS2, hA2, w2, h2, hB2, hX2, hSc2, hSg2, f2⟩ + have hN2 := ntfn_lift_rcv (x' := x2) hP0.toC hS1.2 hR2 hr hA2 (fun p hp => by simp [hp]) hkr1 (by simp) + (by rw [hx2]) + exact ⟨x2, um2, s, σ, Z.G, J, Or.inl ⟨rfl, rfl, rfl⟩, e2, hx2, hR2, ⟨hS2.1, hN2⟩, hA2.mono fun b hb => ⟨hb.2.1, hb.2.2⟩, + (hW1.mono fun b hb => Or.inl hb).trans1 (w2.mono fun b hb => hb.imp_left cXr), + footW_trans (footW_of F1) (w2.mono fun b hb => hb.imp_left kXr), h2, hB2, hX2, hSc2, hSg2, f2, rfl, + KeepCV.refl σ, CnK.refl σ, fun q _ => rfl⟩ + | some rp => + have hU' : ∀ b ∈ rbList ⟨rp⟩, P1.OutsideObjs st (P1.Heap.tcbs H) Cs s1 b ∧ + (∀ e, (H e = some .ep ∨ H e = some .ntfn) → b.val + 8 ≤ e.val ∨ e.val + 16 ≤ b.val) ∧ + b ∉ P1.Heap.frame H := fun b hb => + ⟨(hUd b ⟨rp, hrb, mem_rbList.1 hb⟩).1.ksb hW1.1, (hUd b ⟨rp, hrb, mem_rbList.1 hb⟩).2⟩ + refine WP.spec_mono (do_ipc_transfer_grantGV hH (epBufHyps_bufC Z.T) (epBufMapHyps_bufC hP0) hP0 hP s1 a _ hR1 hS1 + hs hr hne hks1 hkr1 hf hrb hOK1 hBC1 e badge grant hTP hr1) ?_ + rintro s2 ⟨σ', x', hspec, hR2, hS2, hA2, -, -, hx', hWg, htr⟩ + rcases htr with ⟨um, rfl, hSS⟩ | ⟨s1', x1, um0, caps, slots, sk', σc, um, hS1', hks1', hrec, rfl, hSSk, hsrcL, hDL⟩ + · -- no insert + have hWg' : P1.WritesIn st s1 s2 (fun b => XferRegW (P1.Heap.tcbs H) ∅ b ∨ RBufW ⟨rp⟩ b) := by + refine ⟨hWg.1, hWg.2.1, fun b hb hn => ?_⟩ + by_cases hsw : SlotWd Z.th b + · obtain ⟨a', ha', k, hk', rfl⟩ := hsw + exact SlotsSame.wd hSS ha' hk' + · exact hWg.2.2 b hb fun h' => h'.elim (fun h' => hn (Or.inl h')) fun h' => h'.elim (fun h' => hn (Or.inr h')) hsw + obtain ⟨h2, hB2, hX2, hSc2, hSg2, -, -, -, -, f2⟩ := blkX_bufw (l := rbList ⟨rp⟩) h hB hX hSc hSg hU' + (hWg'.mono fun b hb => hb.imp_right mem_rbList.2) + exact ⟨x', um, s, σ, Z.G, J, Or.inl ⟨rfl, rfl, rfl⟩, hspec, hx', hR2, hS2, hA2, + (hW1.mono fun b hb => Or.inl hb).trans1 (hWg'.mono fun b hb => hb.imp cXr fun h' => ⟨rp, hrb, h'⟩), + footW_trans (footW_of F1) (hWg'.mono fun b hb => hb.imp kXr fun h' => ⟨rp, hrb, h'⟩), h2, hB2, hX2, hSc2, + hSg2, f2, rfl, KeepCV.refl σ, CnK.refl σ, fun q _ => rfl⟩ + · -- one insert: the base `(y, σy)` + obtain ⟨cv, c', a', ps, pd, rs, rd, idx, idxd, sk, σk, hps, hpd, hls, hld, ⟨j, hj, hsl⟩, hslots, hsrcC1, hsnn, + hdst1, hca, hcv, he', hcnn, hsh, hbd, hnu, hzb, hkey, hv, htu, hkr2, hnt2, huc, hnat, hvt, hSk, hiw, -, ⟨um1, hσk⟩, hci⟩ := hrec + have hX0 := J.xtop + obtain ⟨hPl, ⟨hSt, hV, hM, hC, hI'⟩, -, -, -, -, -⟩ := hX0.rel + have hAs : AbsSlot Z.th (ps.bv, idx) := ⟨ps, rfl, Or.inl (by simpa [hls] using hps)⟩ + have hAd : AbsSlot Z.th (pd.bv, idxd) := ⟨pd, rfl, Or.inl (by simpa [hld] using hpd)⟩ + have hSs1 : SlotsSame Z.th (L.mem s) (L.mem s1') := SlotsSame.trans' hIF.1 hS1' + have hSsk : SlotsSame Z.th (L.mem s) (L.mem sk) := SlotsSame.trans' hSs1 hSk + have hsrcC : capAt (L.mem s) (cslotAddr (ps.bv, idx)) = ok cv := by + rw [← SlotsSame.capAt hSs1 hAs.isSlot]; exact hsrcC1 + have hdst : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok .Null := by + rw [← SlotsSame.capAt hSs1 hAd.isSlot]; exact hdst1 + have hsd : cslotAddr (ps.bv, idx) ≠ cslotAddr (pd.bv, idxd) := fun e0 => by + rw [e0, hdst] at hsrcC; exact hsnn (eq_of_ok_eq hsrcC).symm + have hsm : cslotAddr (ps.bv, idx) ∈ Z.G.chains.flatten := (hM.members _).2 ⟨hAs.isSlot, cv, hsrcC, hsnn⟩ + have hdnm : cslotAddr (pd.bv, idxd) ∉ Z.G.chains.flatten := fun h0 => by + obtain ⟨-, c0, h0', hn0⟩ := (hM.members _).1 h0 + rw [hdst] at h0'; exact hn0 (eq_of_ok_eq h0').symm + have hnxS : nextOf (L.mem s) (cslotAddr (ps.bv, idx)) = 0#u64 ∨ + (IsSlot Z.th (nextOf (L.mem s) (cslotAddr (ps.bv, idx))) ∧ + nextOf (L.mem s) (cslotAddr (ps.bv, idx)) ≠ cslotAddr (pd.bv, idxd) ∧ + nextOf (L.mem s) (cslotAddr (ps.bv, idx)) ≠ cslotAddr (ps.bv, idx)) := by + rcases hM.next_mem hsm with h0 | ⟨h1', h2'⟩ + · exact Or.inl h0 + · exact Or.inr ⟨((hM.members _).1 h1').1, fun e0 => hdnm (e0 ▸ h1'), h2'⟩ + have hnxW : nextOf (L.mem s) (cslotAddr (ps.bv, idx)) = 0#u64 ∨ + IsSlot Z.th (nextOf (L.mem s) (cslotAddr (ps.bv, idx))) := hnxS.imp_right And.left + have hut : isUt cv = true → ∀ y, ¬ Mdb.Desc Z.G.par y (cslotAddr (ps.bv, idx)) := by + intro hu0 y hy + obtain ⟨z, hz⟩ := desc_tail_child hy + exact hnu hu0 ((hC.has_child_iff hPl hAs).2 ⟨z, hz⟩) + obtain ⟨ps', hps1, hDs⟩ := hsrcL j hj + have eps : ps' = ps := P1.eq_of_bv (by rw [← hps1, hsl]) + rw [eps] at hDs + obtain ⟨pd', hpd1, hDd⟩ := hDL (pd.bv, idxd) (by rw [hslots]; exact List.mem_singleton_self _) + have epd : pd' = pd := P1.eq_of_bv hpd1.symm + rw [epd] at hDd + obtain ⟨y, -, ⟨⟨σy, hci', hKCV, hCnK, hksy, hothy, hdcy, hscy, G', Jy, hmSy, hwSy, hTSy⟩, hiwy⟩, hwy⟩ := spec_exists + (WP.spec_and (WP.spec_and (J.insDJXB hps hpd hls hld hsrcC hsnn hdst c' a' hca hcv he' hcnn hsh (Or.inl hbd) hut + hzb hkey hv htu hkr2 hnt2 huc hnat (hDs.same (SlotsSame.symm' hIF.1)) (hDd.same (SlotsSame.symm' hIF.1))) (insert_mem hPl s c' _ _ hAs.isSlot hAd.isSlot hsd cv hsrcC hnxS)) + (insert_ws hPl s c' _ _ hAs.isSlot hAd.isSlot cv hsrcC hnxW)) + have hSy2 : SlotsSame Z.th (L.mem y) (L.mem s2) := + SlotsSame.trans' (insWords_cong hSsk hAs.isSlot hAd.isSlot hiwy hiw) hSSk + -- the spec: the actual insert's outcome is `σy` patched + obtain ⟨css, hkps, hwfs, hslS⟩ := hSt.cnode ps rs hps + obtain ⟨csd, hkpd, hwfd, hslD⟩ := hSt.cnode pd rd hpd + obtain ⟨c0, srcA, hc0, hcs0, -⟩ := hslS idx hls + obtain ⟨c1, ac1, hc1, hcs1, hr1'⟩ := hslD idxd hld + have hc1n : c1 = .Null := by + have : capAt (L.mem s) (cslotAddr (pd.bv, idxd)) = ok c1 := hc1 + rw [hdst] at this; exact (eq_of_ok_eq this).symm + subst hc1n + have hac1 : ac1 = .nullCap := by rw [← hr1'.2]; rfl + subst hac1 + have hne' : (ps.bv, idx) ≠ (pd.bv, idxd) := fun e0 => hsd (congrArg cslotAddr e0) + have hpse : ps.bv ≠ e.bv := kheap_ne hkps hk (fun _ h1 h2 => by subst h1; cases h2) + have hpde : pd.bv ≠ e.bv := kheap_ne hkpd hk (fun _ h1 h2 => by subst h1; cases h2) + have hpsr : ps.bv ≠ rcv.bv := kheap_ne hkps hkr (fun _ h1 h2 => by subst h1; cases h2) + have hpdr : pd.bv ≠ rcv.bv := kheap_ne hkpd hkr (fun _ h1 h2 => by subst h1; cases h2) + have hτs : σk.kheap ps.bv = σ.kheap ps.bv := by rw [hσk]; simp [hpse, hpsr] + have hτd : σk.kheap pd.bv = σ.kheap pd.bv := by rw [hσk]; simp [hpde, hpdr] + have hτc : σk.cdt = σ.cdt := by rw [hσk]; rfl + have hτo : σk.isOriginalCap = σ.isOriginalCap := by rw [hσk]; rfl + obtain ⟨σ'', h1'', h2''⟩ := capInsert_patch a' hkps hwfs hls hcs0 hkpd hwfd hld hcs1 hne' hτs hτd hτc hτo + obtain ⟨σ3, h13, h23⟩ := capInsert_patch a' hkps hwfs hls hcs0 hkpd hwfd hld hcs1 hne' rfl rfl rfl rfl + have e1 : σ'' = σy := by + rw [hci'] at h1''; have := congrArg Prod.fst h1'' + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at this; exact this.symm + have e3 : σ3 = σy := by + rw [hci'] at h13; have := congrArg Prod.fst h13 + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at this; exact this.symm + subst e3 e1 + have hself : patchK (fun q => q = ps.bv ∨ q = pd.bv) σ'' σ = σ'' := patchK_self_of h13 h23 + have hσc : σc = patchK (fun q => q = ps.bv ∨ q = pd.bv) σ'' σk := by + rw [hci] at h2''; have := congrArg Prod.fst h2'' + simp only [Set.singleton_eq_singleton_iff, Prod.mk.injEq, true_and] at this; exact this + have hrP : ¬ (rcv.bv = ps.bv ∨ rcv.bv = pd.bv) := fun h0 => h0.elim (fun h0 => hpsr h0.symm) fun h0 => hpdr h0.symm + have heP : ¬ (e.bv = ps.bv ∨ e.bv = pd.bv) := fun h0 => h0.elim (fun h0 => hpse h0.symm) fun h0 => hpde h0.symm + have hfin : Notif.updK (Endp.setUm σc um) rcv.bv (.tcb x') = + Notif.updK (Endp.setUm (Notif.updK σ'' e.bv (.endpoint o)) um) rcv.bv (.tcb x') := by + rw [hσc, hσk, patchK_setUm, patchK_updK _ _ hrP, patchK_setUm, patchK_updK _ _ heP, hself, + Endp.setUm_setUm, updK_um_updK] + rw [hfin] at hspec hR2 hS2 + have hksy' : L.ksb s = L.ksb y := hksy.symm + have hzC : SzKeepC Z.C σ'' σ := fun sc _ x n hx => + ⟨x, hCnK.other hx (fun r cs h0 => by cases h0) (fun x h0 => by cases h0)⟩ + have hyk : ∀ q, (∀ r cs, σ.kheap q ≠ some (.cnode r cs)) → σ''.kheap q = σ.kheap q := by + intro q hq + rw [← hself, patchK_kheap] + by_cases hq' : q = ps.bv ∨ q = pd.bv + · rcases hq' with rfl | rfl + · exact absurd hkps (hq _ _) + · exact absurd hkpd (hq _ _) + · rw [if_neg hq'] + -- P1: the insert's slot words first, by `capw_bx` + set lI := insWl (L.mem sk) (cslotAddr (ps.bv, idx)) (cslotAddr (pd.bv, idxd)) with hlI + have hnxk : nextOf (L.mem sk) (cslotAddr (ps.bv, idx)) = nextOf (L.mem s) (cslotAddr (ps.bv, idx)) := by + unfold nextOf; rw [SlotsSame.wd hSsk hAs.isSlot off24] + have hlW : ∀ b ∈ lI, SlotWd Z.th b := by + intro b hb + simp only [hlI, insWl, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hb + rcases hb with (rfl | rfl | rfl | rfl | rfl | rfl | rfl) | hb + · exact ⟨_, hAd.isSlot, _, off0, rfl⟩ + · exact ⟨_, hAd.isSlot, _, off8, rfl⟩ + · exact ⟨_, hAd.isSlot, _, off16, rfl⟩ + · exact ⟨_, hAd.isSlot, _, off24, rfl⟩ + · exact ⟨_, hAs.isSlot, _, off0, rfl⟩ + · exact ⟨_, hAs.isSlot, _, off8, rfl⟩ + · exact ⟨_, hAs.isSlot, _, off24, rfl⟩ + · split at hb + · simp at hb + · rename_i h0 + simp only [List.mem_singleton] at hb + subst hb + rw [hnxk] at h0 ⊢ + exact ⟨_, hnxW.resolve_left h0, _, off16, rfl⟩ + have halI : ∀ b ∈ lI, P1.Aligned b := fun b hb => by + obtain ⟨a', ha', k, hk', rfl⟩ := hlW b hb + exact slot_word_aligned hPl ha' hk' + have hSC := J.slotCapW (fun t ht K hK => P1.tlow_slots t ht K hK) + obtain ⟨hu1, hBu, hXu, hScu, hSgu, -, -, -, -, fu, -⟩ := P1.capw_bx h hB hX hSc hSg + (mixS_from (s := s1) (t := s2) halI) fun b hb => hSC b (hlW b hb) + have hS1k : SlotsSame Z.th (L.mem s1) (L.mem sk) := SlotsSame.trans' hS1' hSk + have hwu : P1.WritesIn st (mixS (L := L) s1 s2 lI) s2 + (fun b => XferRegW (P1.Heap.tcbs H) ∅ b ∨ b ∈ rbList ⟨rp⟩) := by + refine ⟨by rw [mixS_ksb]; exact hWg.1, by rw [mixS_cpu]; exact hWg.2.1, fun b hb hn => ?_⟩ + rw [mixS_mem s1 s2 lI halI hb] + split + · rfl + · rename_i hbl + by_cases hsw : SlotWd Z.th b + · obtain ⟨a', ha', k, hk', rfl⟩ := hsw + rw [SlotsSame.wd hSSk ha' hk', insWords_out hiw ha' hk' hbl] + exact SlotsSame.wd hS1k ha' hk' + · exact hWg.2.2 b hb fun h' => h'.elim (fun h' => hn (Or.inl h')) + fun h' => h'.elim (fun h' => hn (Or.inr (mem_rbList.2 h'))) hsw + obtain ⟨h2, hB2, hX2, hSc2, hSg2, -, -, -, -, f2⟩ := blkX_bufw (l := rbList ⟨rp⟩) hu1 hBu hXu hScu hSgu + (fun b hb => ⟨(hU' b hb).1.ksb (mixS_ksb s1 s2 lI), (hU' b hb).2⟩) hwu + refine ⟨x', um, y, σ'', G', Jy, Or.inr ⟨hmSy, hwSy, hTSy, cv, c', _, _, hAs.isSlot, hAd.isSlot, hsd, + capV_of_capAt hsrcC, capV_of_capAt hdst, hdcy, hscy, hothy, hvt⟩, hspec, hx', hR2, hS2, hA2, ⟨?_, ?_, fun b hb hn => ?_⟩, + ⟨?_, ?_, fun b hb hn => ?_⟩, h2, hB2, hX2, hSc2, hSg2, + fun y' hy' K h1' h2' h3' => (f2 y' hy' K h1' h2' h3').trans (fu y' hy' K (by omega) h2' h3'), hksy, hKCV, hCnK, + hyk⟩ + · exact hWg.1.trans (hW1.1.trans hksy') + · exact hWg.2.1.trans (hW1.2.1.trans hwy.2.1.symm) + · by_cases hsw : SlotWd Z.th b + · obtain ⟨a', ha', k, hk', rfl⟩ := hsw + exact SlotsSame.wd hSy2 ha' hk' + · rw [hwy.2.2 b hb hsw, hWg.2.2 b hb ?_] + · exact hW1.2.2 b hb fun hc => hn (Or.inl (cnW_back hksy' hzC hc)) + · rintro (h' | h' | h') + · exact hn (Or.inl (cXr h')) + · exact hn (Or.inr ⟨rp, hrb, h'⟩) + · exact hsw h' + · exact hWg.1.trans (hW1.1.trans hksy') + · exact hWg.2.1.trans (hW1.2.1.trans hwy.2.1.symm) + · by_cases hsw : SlotWd Z.th b + · obtain ⟨a', ha', k, hk', rfl⟩ := hsw + exact SlotsSame.wd hSy2 ha' hk' + · rw [hwy.2.2 b hb hsw, hWg.2.2 b hb ?_] + · exact F1.2.2 b hb fun hc => hn (Or.inl (P1.KW.ksb hksy' hc)) + · rintro (h' | h' | h') + · exact hn (Or.inl (kXr h')) + · exact hn (Or.inr ⟨rp, hrb, h'⟩) + · exact hsw h' + +end XferG + +end + +end Fl4Proofs.P3 diff --git a/proofs/Fl4Spec/Ext/FrameBatch.lean b/proofs/Fl4Spec/Ext/FrameBatch.lean index 038a78fb5..73a021c8d 100644 --- a/proofs/Fl4Spec/Ext/FrameBatch.lean +++ b/proofs/Fl4Spec/Ext/FrameBatch.lean @@ -188,12 +188,31 @@ def fbCode {α : Type} (m : SEMonad Z α) : NonDetE (State Z) Data α := /-- `cteDelete(slot, true)` in an entry: a preemption (never reached for a frame cap) is FL4's `Err(_)`, `IllegalOperation`. -/ -def fbDelete (slot : CslotPtr) : NonDetE (State Z) Data Unit := - NonDetE.handleE (capDelete slot) fun _ => NonDetE.throwError (msgFromSyscallError .illegalOperation).1 +def fbDeleteWith (del : CslotPtr → PMonad Z Unit) (slot : CslotPtr) : NonDetE (State Z) Data Unit := + NonDetE.handleE (del slot) fun _ => NonDetE.throwError (msgFromSyscallError .illegalOperation).1 + +/-- The native DELETE finaliser with the batch error convention. -/ +abbrev fbDelete (slot : CslotPtr) : NonDetE (State Z) Data Unit := + fbDeleteWith capDelete slot + +/-- The optional native unmap before deleting a frame slot. -/ +abbrev fbDeferDelete (b : Fl4FrameBatchInvocation) (slot : CslotPtr) (dev : Bool) + (p : ObjRef) (R : CapRights) (mt : VMMapType) (sz : VMPageSize) + (m : Option (Asid × VSpaceRef)) : NonDetE (State Z) Data Bool := + match m with + | some (asid', vaddr) => + if b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid then + NonDetE.liftE (do + let r ← unmapPageUnflushed sz b.vspace vaddr p + setCap (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) slot + return r) + else NonDetE.returnOk false + | none => NonDetE.returnOk false /-- **One entry** (`frame_batch_entry`), its error the reply's error code; `true` when a deferred delete cleared an entry. -/ -def fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : NonDetE (State Z) Data Bool := +def fbEntryWith (del : CslotPtr → PMonad Z Unit) (b : Fl4FrameBatchInvocation) + (w : Data × Data × Data) : NonDetE (State Z) Data Bool := match b.op with | .copyMap => do let dest ← fbCode <| lookupTargetSlot b.destCnode (dataToCptr w.2.1) b.depth @@ -218,39 +237,45 @@ def fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : NonDetE ( match c with | .nullCap => NonDetE.returnOk false | .archObjectCap (.pageCap dev p R mt sz m) => do - let cleared ← match m with - | some (asid', vaddr) => - if b.deferDelete ∧ mt = .vmVSpaceMap ∧ dev = false ∧ asid' = b.asid then - NonDetE.liftE (do - let r ← unmapPageUnflushed sz b.vspace vaddr p - setCap (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) slot - return r) - else NonDetE.returnOk false - | none => NonDetE.returnOk false - fbDelete slot + let cleared ← fbDeferDelete b slot dev p R mt sz m + fbDeleteWith del slot NonDetE.returnOk cleared | _ => NonDetE.throwError (msgFromSyscallError .illegalOperation).1 +/-- One entry of the native batch. -/ +abbrev fbEntry (b : Fl4FrameBatchInvocation) (w : Data × Data × Data) : NonDetE (State Z) Data Bool := + fbEntryWith capDelete b w + /-- The loop (`decode_frame_batch`'s `while`): the entries in order, up to the first that fails; (entries done, error code or 0, whether a deferred delete cleared an entry). -/ -def fbRun (b : Fl4FrameBatchInvocation) : List (Data × Data × Data) → Nat → Bool → SMonad Z (Nat × Data × Bool) +def fbRunWith (del : CslotPtr → PMonad Z Unit) (b : Fl4FrameBatchInvocation) : + List (Data × Data × Data) → Nat → Bool → SMonad Z (Nat × Data × Bool) | [], done, d => return (done, 0, d) | w :: ws, done, d => do - let r ← (fbEntry b w).run + let r ← (fbEntryWith del b w).run match r with - | .ok d' => fbRun b ws (done + 1) (d || d') + | .ok d' => fbRunWith del b ws (done + 1) (d || d') | .error c => return (done, c, d) +/-- The native batch loop. -/ +abbrev fbRun (b : Fl4FrameBatchInvocation) : List (Data × Data × Data) → Nat → Bool → SMonad Z (Nat × Data × Bool) := + fbRunWith capDelete b + /-- **The batch**: the loop, then one invalidation of the address space if a deferred delete cleared an entry (FL4's `invalidate_asid`, l4v's `flush_all`, as `performPml4Flush`); the reply is (entries done, error code or 0). -/ -def performFrameBatch (b : Fl4FrameBatchInvocation) : SMonad Z (List Data) := do - let (done, err, d) ← fbRun b b.entries 0 false +def performFrameBatchWith (del : CslotPtr → PMonad Z Unit) (b : Fl4FrameBatchInvocation) : + SMonad Z (List Data) := do + let (done, err, d) ← fbRunWith del b b.entries 0 false NonDet.when (d = true) <| flushAll b.vspace b.asid return [BitVec.ofNat 64 done, err] +/-- The native batch uses the ported cap deletion. -/ +abbrev performFrameBatch (b : Fl4FrameBatchInvocation) : SMonad Z (List Data) := + performFrameBatchWith capDelete b + end end diff --git a/proofs/Fl4Spec/Ext/VirtDispatch.lean b/proofs/Fl4Spec/Ext/VirtDispatch.lean index 88384dd27..7cf088f4c 100644 --- a/proofs/Fl4Spec/Ext/VirtDispatch.lean +++ b/proofs/Fl4Spec/Ext/VirtDispatch.lean @@ -22,7 +22,8 @@ and everything else to `fl4DecodeInvocation`. `fl4PerformInvocationV` performs the new invocations and, for the ported ones, substitutes the virtualization-aware operations: `fl4InvokeUntypedV` (VCPU init), `sendSignalV` (notification send), `invokeTcbV` and `invokeCnodeV` -(finalisation hooks); the rest is `fl4PerformInvocation`. +(finalisation hooks), and `performFrameBatchWith capDeleteV` for +FrameBatch. The remaining cases use `fl4PerformInvocation`. `fl4HandleInvocationV` is `fl4HandleInvocationR` with this dispatcher. The ported modules are not changed. @@ -98,6 +99,8 @@ def fl4PerformInvocationV (onCore : ObjRef → Bool) : Bool → Bool → Bool | _, _, _, .base (.invokeCNode i) => do invokeCnodeV i NonDetE.returnOk [] + | _, _, _, .base (.invokeArchObject (.frameBatch i)) => + NonDetE.liftE <| performFrameBatchWith capDeleteV i | block, call, canDonate, .base i => fl4PerformInvocation block call canDonate i | _, _, _, .vcpu i => NonDetE.liftE <| performVcpuInvocation i | _, _, _, .ept i => NonDetE.liftE (do diff --git a/proofs/Fl4Spec/Ext/VirtEptInv.lean b/proofs/Fl4Spec/Ext/VirtEptInv.lean index ca2d62404..61f240261 100644 --- a/proofs/Fl4Spec/Ext/VirtEptInv.lean +++ b/proofs/Fl4Spec/Ext/VirtEptInv.lean @@ -228,8 +228,8 @@ def performEptInvocation : EptInvocation → SMonad Z Unit -- seL4 clears an unmapped table (FL4 `s.zero(ptr, 4096)`). let (l, _) ← getEpt ptr setObject ptr (.archObj (.ept l fun _ => 0)) - | none => return () - setCap (.archObjectCap (.eptCap ptr level none)) slot + setCap (.archObjectCap (.eptCap ptr level none)) slot + | none => pure () -- the skipped `setCap` is a verified no-op (every unmapped EPT cap has asid 0, vaddr 0) against Rust `ept.rs:262-282` | .frameMap newCap slot tbl i entry => do setCap (.archObjectCap newCap) slot storeGuestEntry tbl i entry @@ -237,9 +237,10 @@ def performEptInvocation : EptInvocation → SMonad Z Unit match cap with | .pageCap dev p R _ sz m => do match m with - | some (asid, gpa) => NonDet.when (asid ≠ 0) <| guestUnmap asid gpa p (pageBitsForSize sz) false - | none => return () - setCap (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) slot + | some (asid, gpa) => do + NonDet.when (asid ≠ 0) <| guestUnmap asid gpa p (pageBitsForSize sz) false + setCap (.archObjectCap (.pageCap dev p R .vmNoMap sz none)) slot + | none => pure () -- unreachable from decode (`.frameUnmap` is built only for a mapped frame); the Rust unmapped-frame write is the same cap | _ => NonDet.fail | .poolAssign asid pool slot => do let c ← getCap slot diff --git a/proofs/Fl4Spec/Ext/VirtKernelF.lean b/proofs/Fl4Spec/Ext/VirtKernelF.lean index 9b0f29ba5..0b02427a7 100644 --- a/proofs/Fl4Spec/Ext/VirtKernelF.lean +++ b/proofs/Fl4Spec/Ext/VirtKernelF.lean @@ -8,7 +8,8 @@ ev` (`Ext/VirtKernel`) with every VMEnter completion as FL4 main `vmEnterF` for `SysVMEnter`, `vcpuDetachF` in finalisation (VCPU caps, final thread caps) and SetTCB (`associateVcpuTcbF`), and `sendSignalVF` in every signal (interrupts, the notification invocation, the TCB -timers). `remoteCur t` (FL4 `node_get(cpu, CUR_THREAD) == t` for `t`'s +timers). FrameBatch uses the same VM-aware frame deletion as +`fl4CallKernelV`. `remoteCur t` (FL4 `node_get(cpu, CUR_THREAD) == t` for `t`'s core `cpu`) is, like `onCore`, a per-core fact the uniprocessor spec does not have, fixed for the entry. @@ -150,6 +151,10 @@ def capDeleteVF (slot : CslotPtr) : PMonad Z Unit := do let _ ← recDelVF (.cTEDeleteCall slot true) NonDetE.returnOk () +/-- FrameBatch DELETE finalises frames through the VM-aware cap delete. -/ +def performFrameBatchVF (b : Fl4FrameBatchInvocation) : SMonad Z (List Data) := + performFrameBatchWith capDeleteVF b + /-- `capRevokeFV` (`Ext/VirtFinalise`) with FL4's VMEnter report (`Ext/VirtReport`). Its docstring: l4v `cap_revoke`'s equation (`capRevokeF`) with `capDeleteV`. -/ def capRevokeFVF (capRevoke : CslotPtr → PMonad Z Unit) (slot : CslotPtr) : PMonad Z Unit := do let cap ← withoutPreemption <| getCap slot @@ -286,6 +291,8 @@ def fl4PerformInvocationVF (onCore remoteCur : ObjRef → Bool) : Bool → Bool | _, _, _, .base (.invokeCNode i) => do invokeCnodeVF i NonDetE.returnOk [] + | _, _, _, .base (.invokeArchObject (.frameBatch i)) => + NonDetE.liftE <| performFrameBatchVF i | block, call, canDonate, .base i => fl4PerformInvocation block call canDonate i | _, _, _, .vcpu i => NonDetE.liftE <| performVcpuInvocationF i | _, _, _, .ept i => NonDetE.liftE (do diff --git a/proofs/Fl4Spec/Ext/VirtP3x.lean b/proofs/Fl4Spec/Ext/VirtP3x.lean index ca8f07416..e5ba492cc 100644 --- a/proofs/Fl4Spec/Ext/VirtP3x.lean +++ b/proofs/Fl4Spec/Ext/VirtP3x.lean @@ -22,8 +22,8 @@ every ported operation's preservation of `NoVirtObjects`) is VIRT-PLAN.md the two unknown labels; the ported retype even creates the VT-x objects). These are the exact exceptions of P3x (VIRT-PLAN.md §10). 2. **Perform** (`fl4PerformInvocationV_base`, proved): every ported - invocation but the four substituted ones (untyped, notification send, - TCB, CNode) is performed by the ported code. + invocation outside the substituted routes (untyped, notification send, + TCB, CNode, and FrameBatch) uses the native performer. 3. **Hooks** (proved): `finaliseCapV` is `finaliseCap` on every cap but the hooked ones (`finaliseCapV_eq`); on a state without virtualization objects `vcpuDetach` and a thread cap's hook do nothing @@ -135,6 +135,7 @@ def PerformSubst : Fl4Invocation → Prop | .invokeNotification _ _ => True | .invokeTCB _ => True | .invokeCNode _ => True + | .invokeArchObject (.frameBatch _) => True | _ => False /-- **P3x, perform** (module docstring, 2). -/ @@ -147,6 +148,8 @@ theorem fl4PerformInvocationV_base (onCore : ObjRef → Bool) (block call canDon | invokeNotification _ _ => exact absurd trivial h | invokeTCB _ => exact absurd trivial h | invokeCNode _ => exact absurd trivial h + | invokeArchObject i => + cases i <;> first | exact absurd trivial h | rfl | _ => rfl /-- The caps `finaliseCapV` hooks. -/ diff --git a/proofs/Fl4Spec/PORTING.md b/proofs/Fl4Spec/PORTING.md index e524405b2..b892bf2c5 100644 --- a/proofs/Fl4Spec/PORTING.md +++ b/proofs/Fl4Spec/PORTING.md @@ -22,6 +22,10 @@ executable-spec types these import (`spec/design/skel`, `spec/haskell`). IO page tables (`IOPageTable`, `IOSpace`, VT-d contexts), no VCPUs or EPT, no non-MCS definitions. Keep IO ports. Where an omission removes a case of a datatype, say so in a comment at the datatype. +- **No early `return`.** Never write a non-tail `return ()` inside a `do` + `match`/`if`; always use `pure ()`. Lean's `return` exits the whole `do` + block and so skips what follows, while in the seL4 source (pinned + `c98d70ad`) leaving such a branch continues with the next statement. - **FL4 differences** (per-core state, l4h extensions such as TCB timers, trapSyscalls, subframes) never go into ported modules. They go into `Fl4Spec/Ext/` later, on top of the port. @@ -487,6 +491,18 @@ details. Where the owner decided (2026-11-21) that FL4 should follow seL4 and the kernel does not yet, the spec states seL4's behaviour and the place is marked `-- FL4 code differs until V1/V2/V3` (VIRT-PLAN.md §12). +- **VM FrameBatch DELETE** (`Ext/FrameBatch`, `Ext/VirtDispatch`, + `Ext/VirtKernelF`). A native PML4 selects the batch, and each + DELETE resolves its own frame slot. FL4's `frame_batch_entry` uses + `cdt::delete`, which invokes `ept::unmap` for a mapped EPT frame + with a nonzero ASID. The shared batch uses `capDelete` for native + dispatch, `capDeleteV` for V and `capDeleteVF` for VF. The deferred + native unmap and COPY_MAP rollback retain their existing semantics. + The VM batch agrees with the native batch when both unmaps succeed + without changing the state at every reached mapped-EPT DELETE. + If a stale EPT cap's ASID now names a native root, the native unmap + can clear a matching native entry. The equality also compares the + abstract failure flag. - **Saved host IRQs at VCPU exit** (`Ext/VirtExitIrqF`). The x86 exit path captures or polls the host vector before taking the gate (`crates/fl4-kernel/src/entry.rs:774–812`). By the time the