From 88d76b0228144c77be31d945393650b2a99c75cd Mon Sep 17 00:00:00 2001 From: CommaBot Date: Tue, 1 Dec 2026 11:31:55 +0900 Subject: [PATCH] =?UTF-8?q?P2=20(AArch64):=20the=20fl4-sync=20gate=20and?= =?UTF-8?q?=20locks=20refine=20the=20gate=20model,=20with=20the=20CDT=20an?= =?UTF-8?q?d=20IPC-buffer=20history=20(DESIGN=20=C2=A79=20item=204)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DESIGN §9 item 4, both halves, for AArch64. One commit on `main` @ `c98a08b1c` (after #146 and #147): 34 files, +9216 / −199. No Rust changes (nothing under `crates/`). No x86 statement changes. **Half 1 (the history along an execution).** A replay record `InvR` beside `InvH`; P1's MDB tree facts hold of the replayed history (`MdbSH`) along every execution of the local-path family, every slot's edit being one of P1's (`SlotWireP`). The top family is `fl4LocPBL` (`DelLkA`): the Delete-with-PTE start needs the CDT facts only at its own locked slots (`CdtAtL`), discharged from the history, so the all-slot `MdbLk` / `NullLk` [J] is gone. The IPC-buffer words are gate-owned, so `BufSH` is kept along an execution and gives the PTE slots' `bufU`. Boot corollaries at w0/w1 (`w0_serial_mdbL`, `w1_serial_mdbL`). **Half 2 (the gate and the locks themselves).** `proofs/Fl4P2Base/GateImpl.lean` hand-transcribes fl4-sync's gate (`try_enter_local`, `leave_local`, `acquire`, the serial body, `release`) and fl4-store's lock table (`try_lock_object`, `unlock_held`), plus hardware writes, as `ICfg` / `IStepG` / `IStepL` / `IStepHw` (listed in `TRUSTED` with its modelling choices). `impl_sim` matches every runtime step by zero or one gate-model step (`StepR`: a `Serial.Step`, or a racy speculative read returning another value; on gate- or core-class words a value of the current class), keeping `Rel` and the lock invariant `LkInv`; hardware writes during a pending serial body are deferred and replayed after the model's `serial` at the release's decisive store. Racy reads are covered by weak safety `SafeW` from the same per-path certificates. `impl_execPA` / `serializable_implPA` (`GateExecA`): every runtime execution from boot is an FL4 execution, and where no local path runs and no body is pending its memory is the serial run of the completed entries; `w0_serializable_impl` / `w1_serializable_impl` (`GateBootA`) at the boot witnesses for `fl4LocPBL`. **Premises and hypotheses.** - Named premises added: [P] MM-SB, MM-DRF, MM-RACY, IPI (the transcription's step granularity, stated in `GateImpl`'s docstring; no Lean hypothesis carries them). `IHwPA` is `HwStepPA`'s existing [P] moved to the runtime. No [W]. - Theorem hypotheses left to the serial-kernel side: `hSer` / `hSerB` (serial entries keep `MdbSH` / `BufSH`; AArch64 P1 / S1A) and `BufSH` at w0/w1 (S1A / Boot). - Not yet proved: the Half 1 history facts along the weak executions `ExecPAR` (proved along `ExecPA`). **Merge with #146.** Text conflicts only: `proofs/Fl4ProofsA64.lean` (union of imports, no duplicates) and `DESIGN.md` §9 status (main's `w1_sysL` kept; this PR's item-4 bullets added; the "Not yet proved" bullet reconciled: `SysL` at w1 is no longer listed). No proof conflicts. Existing declarations: 0 statement changes against the pre-rebase branch. **Checks.** - `Fl4ProofsA64` root build on this tree: 0 errors, 0 sorry, "Build completed successfully (5755 jobs)". Module builds of every changed module and its users (the gate files, the A64 P2 files of this PR, and the x86 `Fl4Proofs.P2` re-export modules): 0 errors, 0 sorry (5548 jobs). - Axioms: sweep of all 18882 constants under `Fl4ProofsA64.P2` and `Fl4Proofs.P2` (`Fl4P2Base`): 0 outside propext / Classical.choice / Quot.sound; plus 109 `#print axioms` on the gate files' and `GateBootA`'s theorems: 68 `[propext, Classical.choice, Quot.sound]`, 21 `[propext]`, 3 `[propext, Quot.sound]`, 17 none, 0 `sorryAx`. - No sorry / admit / native_decide / bv_decide / axiom / maxHeartbeats in the diff; every new module is imported from `Fl4ProofsA64`. - `python3 xtask/scripts/port_p2_a64.py --check proofs`: exit 0. - No x86 root build: the x86 users of `Fl4P2Base` are the unchanged re-export modules `Fl4Proofs.P2.Serial` / `SerialHw` / `SafeCert` / `SerialSafe`, built as modules. Co-Authored-By: Claude Opus 5.5 --- DESIGN.md | 10 +- README.md | 10 +- TRUSTED | 29 +- proofs/Fl4P2Base/GateImpl.lean | 1401 ++++++++++++++++++++ proofs/Fl4P2Base/GateLock.lean | 430 ++++++ proofs/Fl4P2Base/SafeW.lean | 455 +++++++ proofs/Fl4P2Base/SerialD.lean | 61 + proofs/Fl4P2Base/SerialR.lean | 574 ++++++++ proofs/Fl4P2Base/SerialW.lean | 139 ++ proofs/Fl4Proofs.lean | 4 + proofs/Fl4ProofsA64.lean | 13 + proofs/Fl4ProofsA64/P2/BufWireA.lean | 291 ++++ proofs/Fl4ProofsA64/P2/CutNbwA.lean | 4 +- proofs/Fl4ProofsA64/P2/DelLkA.lean | 295 +++++ proofs/Fl4ProofsA64/P2/E2EPStartA.lean | 75 +- proofs/Fl4ProofsA64/P2/E2EPTopA.lean | 175 ++- proofs/Fl4ProofsA64/P2/GateBootA.lean | 158 +++ proofs/Fl4ProofsA64/P2/GateExecA.lean | 603 +++++++++ proofs/Fl4ProofsA64/P2/GlueAdeqA.lean | 259 ++++ proofs/Fl4ProofsA64/P2/GlueRepA.lean | 376 ++++++ proofs/Fl4ProofsA64/P2/InvDelPteAccPA.lean | 312 ++++- proofs/Fl4ProofsA64/P2/InvDelPteLPA.lean | 248 +++- proofs/Fl4ProofsA64/P2/InvPmPdw.lean | 7 +- proofs/Fl4ProofsA64/P2/InvPmRunA.lean | 48 +- proofs/Fl4ProofsA64/P2/InvPmWalkA.lean | 6 +- proofs/Fl4ProofsA64/P2/MdbBootP.lean | 78 ++ proofs/Fl4ProofsA64/P2/MdbEditA.lean | 299 +++++ proofs/Fl4ProofsA64/P2/MdbKeepA.lean | 1198 +++++++++++++++++ proofs/Fl4ProofsA64/P2/MdbKeepDelA.lean | 391 ++++++ proofs/Fl4ProofsA64/P2/MdbKeepPmA.lean | 457 +++++++ proofs/Fl4ProofsA64/P2/MdbWireA.lean | 471 +++++++ proofs/Fl4ProofsA64/P2/MdbWirePDA.lean | 517 ++++++++ proofs/Fl4ProofsA64/P2/ViewSafeIoAcc.lean | 6 +- xtask/scripts/port_p2_a64.py | 15 +- 34 files changed, 9216 insertions(+), 199 deletions(-) create mode 100644 proofs/Fl4P2Base/GateImpl.lean create mode 100644 proofs/Fl4P2Base/GateLock.lean create mode 100644 proofs/Fl4P2Base/SafeW.lean create mode 100644 proofs/Fl4P2Base/SerialD.lean create mode 100644 proofs/Fl4P2Base/SerialR.lean create mode 100644 proofs/Fl4P2Base/SerialW.lean create mode 100644 proofs/Fl4ProofsA64/P2/BufWireA.lean create mode 100644 proofs/Fl4ProofsA64/P2/DelLkA.lean create mode 100644 proofs/Fl4ProofsA64/P2/GateBootA.lean create mode 100644 proofs/Fl4ProofsA64/P2/GateExecA.lean create mode 100644 proofs/Fl4ProofsA64/P2/GlueAdeqA.lean create mode 100644 proofs/Fl4ProofsA64/P2/GlueRepA.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbBootP.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbEditA.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbKeepA.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbKeepDelA.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbKeepPmA.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbWireA.lean create mode 100644 proofs/Fl4ProofsA64/P2/MdbWirePDA.lean diff --git a/DESIGN.md b/DESIGN.md index 6f9604b0b..cf68d478f 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -432,7 +432,11 @@ The proof is staged so each level is useful by itself and later levels build on - Its named premises beyond the first form's: [J] `EpochAP` at boot and kept by every serial entry; for the two PTE slots [J] `hbuf` (IPC-buffer placement at every entry state, as the timer interrupt's `ub`) and, for Delete, [J] `hcdt` (CDT link well-formedness at the start), [P] the shared `LkKernelReadsA`, [P] `cs` (the invocation's cores are below 128) and [P] `CurUnbound` at the start (the invoking thread has no bound VCPU); no [W]. The PageMap and Delete-with-PTE slots' user-write obligation is proved, not assumed. - AArch64 boot (`Fl4ProofsA64/P1/Boot*`, `Fl4ProofsA64/P2/Boot*`): two boot witnesses, `w0` (the kernel's boot state, no page tables) and `w1` (the root task's: its CNode, TCB, SC, one VSpace with a 512-entry leaf table). `EpochAP` holds at both (`boot_epochAP`, `w1_epochAP`; at `w1` the PTE set is the 512 words of the leaf table and equals `ptesR`, non-vacuously), so `w0_serial` / `w1_serial` are `serializable_fl4_localVP` with the epoch premise discharged, and `w1_vreStart_ex` starts the `arm_vm_entry_ready` slot at the concrete `w1` with no premise left. P1's `SysL` holds at `w0` (`boot_sysL`) and the MDB tree facts at `w1` (`w1_treeA`, `w1_mdbLk`). - P1's `SysL` also holds at `w1` (`w1_sysL`, `BootSysL1`). - - Not yet proved on AArch64: `hbuf` and `hcdt` from AArch64 P1 invariants; the [J] premises of the other slots from P1; and the gate and lock implementations themselves (§9, "From serial to concurrent", (4)). + - §9 item 4, Half 1 (the CDT and the IPC buffer along an execution, `Fl4ProofsA64/P2/Mdb*`, `BufWireA`, `DelLkA`): the replay record `InvR` beside `InvH` gives a history memory `H` whose entries replayed serially explain the run; P1's MDB tree facts hold of it (`MdbSH`) along every execution of `fl4LocPB` (`serializable_fl4_localVP_mdb`), every slot's edit being one of P1's (`SlotWireP`). The top family is now **`fl4LocPBL`** (`DelLkA`): `fl4LocPB` with the Delete-with-PTE slot started at `CdtAtL`, the CDT facts at the slots the path itself locks, discharged from the execution (`delSlotPL_startExec`), so the Delete start's all-slot `MdbLk` / `NullLk` [J] is gone (`hcdt` from `MdbSH` of the history, under `hSer`). The IPC-buffer words are gate-owned, so no local path or hardware step writes them and `BufSH` (`bufU` from the memory) is kept along an execution (`serializable_fl4_localVPL_buf`, under `hSerB` and `BufSH` at the start; the PTE slots' `hbuf` from it, `delSlotPL_startAtB`). From the boot witnesses: `w0_serial_mdbL`, `w1_serial_mdbL`. + - Its remaining obligations, theorem hypotheses: `hSer` / `hSerB`, that serial entries keep `MdbSH` / `BufSH` (the serial kernel's AArch64 P1 / S1A obligations, at the top-level P1–P2 composition), and `BufSH` at `w0` / `w1` (S1A / Boot). + - §9 item 4, Half 2 (the gate and the locks themselves, `Fl4P2Base/GateImpl`, `GateLock`, `SerialW`, `SafeW`, `Fl4ProofsA64/P2/GateExecA`, `GateBootA`): `GateImpl.lean` is a hand transcription of `fl4-sync`'s gate (`try_enter_local`, `leave_local`, `acquire`, the serial body, `release`) and `fl4-store`'s lock table (`try_lock_object`, `unlock_held`) as a runtime configuration and steps (`ICfg`, `IStepG`, `IStepL`, and the guest's or MMU's hardware write `IStepHw`); its fidelity is reviewed, not proved, and it is listed in `TRUSTED`. `impl_sim` matches every runtime step by zero or one step of the gate model (`StepR`: `Serial.Step`, or a racy speculative read returning another value of the word's coherence order: any value, or a value of the current class where the gate or the core owns the class), keeping a refinement relation `Rel` and the lock table's invariant `LkInv`; a hardware write while a serial body is pending release is deferred and replayed after the model's `serial` at the release's decisive store. Racy reads are covered by weak safety `SafeW`, derived from the same per-path certificates (`LocalPath.safeW`). `impl_execPA` / `serializable_implPA` (`GateExecA`): every runtime execution from boot, under the per-step premises `IFl4StepPA` (`Fl4StepPA`'s glue `arm` / `start` [P] and the serial entries' layout [J], now stated at the serial body) and the hardware write's `HwStepPA` [P], is an FL4 execution `ExecPAR`, and where no local path is in progress and no body pending its memory is the serial run of the completed entries. At the boot witnesses, for `fl4LocPBL` under `EpochAP`: `w0_serializable_impl`, `w1_serializable_impl`. + - Half 2's named premises, the transcription's step granularity (in `GateImpl`'s docstring; no Lean hypothesis carries them): **[P] MM-SB** (the Dekker store→load order of the active flag and `closed`), **[P] MM-DRF** (plain accesses to lock- or gate-owned words read the latest write), **[P] MM-RACY** (a racy aligned 64-bit read returns some value of the word's coherence order), **[P] IPI** (a remote call runs inside the requesting serial body; `settle_resume_point` is sequenced at the tail of the core's previous local entry). No [W]. + - Not yet proved on AArch64: `hSer` / `hSerB` and `BufSH` at the witnesses (above); the [J] premises of the other slots from P1; the Half 1 history facts (`MdbSH`, `BufSH`) along the weak executions `ExecPAR` (they are proved along `ExecPA`). - **P3**, in `proofs/Fl4Proofs/P3`, against `Fl4Spec` (the l4v rt abstract spec ported to Lean, up to `call_kernel`; KernelInit_A is dead in l4v and left out). The top-level theorem `handle_invocation_refinesM` relates the decode and invocation of each capability to the spec, from per-capability results and named hypotheses, and carries the CDT invariant `MdbOK` (frame invariant, untyped bookkeeping, no overlap) through a per-step relation `MdbStep`: - CSpace: lookups, every CNode invocation, delete and revoke (zombies, preemption), untyped retype and reset, the IRQ and IO port caps. - TCBs: native TCB invocations, including cap installs. The dispatcher excludes the new SetEPTRoot label. @@ -455,11 +459,11 @@ Decided: we port seL4's Isabelle abstract specification (the MCS version, from ` The argument -\(1\) Under the serial gate an entry runs alone on memory, so it is one serial kernel entry, which P0 to P3 are about. (2) A local path that runs ends in the state the general path, a serial entry, would (refinement), and writes only its footprint (frame). (3) The gate holder waits for active local paths and a local path starts only on an open gate (the Dekker handshake of §7), so serial entries and local paths never overlap; two local paths hold disjoint footprints, by their locks and their cores, so they commute. Together, any execution equals running its entries one after another, in the order serial entries take the gate and local paths finish. (1) is P0–P3; (2) and (3) are P2. (4) What is left unproven is that `fl4-store`, `fl4-sync` and the IPI paths implement the gate and the locks correctly on real memory. The simulator tests exactly that, and checks (2) on every local path it runs. +\(1\) Under the serial gate an entry runs alone on memory, so it is one serial kernel entry, which P0 to P3 are about. (2) A local path that runs ends in the state the general path, a serial entry, would (refinement), and writes only its footprint (frame). (3) The gate holder waits for active local paths and a local path starts only on an open gate (the Dekker handshake of §7), so serial entries and local paths never overlap; two local paths hold disjoint footprints, by their locks and their cores, so they commute. Together, any execution equals running its entries one after another, in the order serial entries take the gate and local paths finish. (1) is P0–P3; (2) and (3) are P2. (4) That `fl4-store`, `fl4-sync` and the IPI paths implement the gate and the locks: on AArch64, P2 proves that a hand transcription of their protocol core refines the gate model (§9 item 4, Half 2: `GateImpl`, `impl_execPA`), from four named memory-model and IPI premises; the transcription's fidelity to the Rust and those premises are trusted. The simulator tests the implementation on real memory, and checks (2) on every local path it runs. ### Trusted base -The proof assumes: Rust, LLVM and the linker, and the kernels' linker scripts; Charon, Aeneas and the Lean kernel; the abstract spec; entry assembly and boot code; `fl4-store` and `fl4-sync`; hardware backends; the totality of `Store` and `Arch` (for each extraction); and the processor's memory model. fl4-core's AArch64 slice has P0 only. Everything trusted is listed in one file in the repo, with its size tracked in CI. +The proof assumes: Rust, LLVM and the linker, and the kernels' linker scripts; Charon, Aeneas and the Lean kernel; the abstract spec; entry assembly and boot code; `fl4-store` and `fl4-sync`, and the Lean transcription of their protocol core (`proofs/Fl4P2Base/GateImpl.lean`); hardware backends; the totality of `Store` and `Arch` (for each extraction); and the processor's memory model. fl4-core's AArch64 slice has P0 only. Everything trusted is listed in one file in the repo, with its size tracked in CI. ## 10. Deterministic simulation diff --git a/README.md b/README.md index bb8e7c96f..8a8305c61 100644 --- a/README.md +++ b/README.md @@ -205,7 +205,15 @@ the VM-timer interrupt and `arm_vm_entry_ready`, from named premises page-table entries (`KLayoutP` / `ownP`), adding the frame-cap PageMap and the Delete of a mapped frame cap, under the epoch contract `EpochAP`, which is proved at the two boot witnesses `w0` and `w1` (`w0_serial`, -`w1_serial`). `cargo xtask gate` checks the x86_64 +`w1_serial`). The top family is `fl4LocPBL` (the Delete start checked +only at its own locked slots), with the CDT tree facts of the replayed +history kept along every execution (`w0_serial_mdbL`, `w1_serial_mdbL`), +and the runtime itself is tied to it: a hand transcription of `fl4-sync`'s +gate and `fl4-store`'s locks (`proofs/Fl4P2Base/GateImpl.lean`, in +`TRUSTED`) refines the gate model step by step, racy reads and the +guest's hardware writes included, so the runtime's executions from `w0` / +`w1` are serializable (`w0_serializable_impl`, `w1_serializable_impl`), +from four memory-model and IPI premises. `cargo xtask gate` checks the x86_64 extraction; the AArch64 one is extracted and checked on its own (`lake build Fl4ProofsA64`). P3 also covers the x86_64 VCPU/EPT extension on a joint VM state (`VmEptState`): retype and untyped, the EPT diff --git a/TRUSTED b/TRUSTED index 519c9c948..0fb369be0 100644 --- a/TRUSTED +++ b/TRUSTED @@ -34,6 +34,26 @@ a64-core: crates/fl4-core/src/arch/aarch64 runtime: crates/fl4-store/src runtime: crates/fl4-sync/src +# The hand transcription of that runtime's protocol core into Lean +# (DESIGN.md §9 item 4, Half 2): fl4-sync's gate (try_enter_local, +# leave_local, acquire, serial, release) and fl4-store's lock table +# (try_lock_object, unlock_held) as the configuration ICfg and the steps +# IStepG / IStepL / IStepHw that P2 proves refine Serial.Step. Its fidelity +# to the Rust is reviewed, not proved. Modelling choices (stated in the +# module docstring): one atomic Mem operation, one plain access or the +# serial body per step, justified by the named premises [P] MM-SB (the +# Dekker store->load order), MM-DRF (plain accesses to lock- or +# gate-owned words), MM-RACY (a racy aligned read returns some value of the +# word's coherence order) and IPI (remote calls run inside the requesting +# serial body); tickets as naturals (at most MAX_CPUS outstanding); +# `closed` as the three-valued Gate; the flag scan as one phase of +# per-core loads; fences as no-ops under SC except the SeqCst one; +# `owner`, `local_hit` and the policy word not modelled; the serial and +# local programs fixed at the ticket and at the first open load; ghost +# state (programs, taken objects, written words, ghost maps) beside the +# runtime words. GateLock.lean (the lock side) only proves facts about it. +transcription: proofs/Fl4P2Base/GateImpl.lean + # The interface between the verified core and the trusted world: what P0 # assumes the Store and Arch implementations do (that they are total), for # each extraction. The x86_64 Arch methods are machine.rs and the macro it @@ -46,8 +66,9 @@ interface: proofs/Fl4Core/FunsExternal.lean interface: proofs/Fl4ProofsA64/P0/Totality.lean interface: proofs/Fl4CoreA64/FunsExternal.lean -# The abstract model of the transactional runtime P2 was proved about, -# superseded with it (DESIGN.md §9). Nothing is proved about the serial -# gate and local paths that replaced it (P2 not started): the runtime -# group above is trusted and tested by fl4-sim. +# The abstract model of the transactional runtime P2 was first proved +# about, superseded with it (DESIGN.md §9). The serial gate and local paths +# that replaced it are related to P2's Serial model through the +# transcription above; the runtime group itself stays trusted and tested by +# fl4-sim. model: proofs/Fl4Proofs/P2/Runtime.lean diff --git a/proofs/Fl4P2Base/GateImpl.lean b/proofs/Fl4P2Base/GateImpl.lean new file mode 100644 index 000000000..aababf177 --- /dev/null +++ b/proofs/Fl4P2Base/GateImpl.lean @@ -0,0 +1,1401 @@ +/- +# P2 (sync, gate gen 1): **the `fl4-sync` gate and locks, transcribed**, and the gate side of its refinement to `Serial.Step` (DESIGN §9 item 4, Half 2) + +`Serial.lean` proves serializability of an abstract interleaving model of the serial gate and the local paths +(`Serial.Step`). This module ties that model to the runtime that implements it, `crates/fl4-sync/src/lib.rs` and +`crates/fl4-store/src/lock.rs`, by a **hand transcription** of the protocol core into an implementation +configuration `ICfg` and step relation `IStep` (one atomic `Mem` operation, one plain access, or the serial body per +step, under sequentially consistent atomics), and a refinement relation `Rel : ICfg → Serial.Cfg → Prop`. +Nothing in `Serial.lean` or any other existing file changed; `SerialW.lean` adds the weak racy-read step `StepR`. + +## The transcription (TRUSTED: fidelity to the Rust is reviewed, not proved) + +| Rust (file:line) | Lean | +|---|---| +| `SerialGate` fields `next`, `serving`, `closed`, `active`, `cores` (`lib.rs:77–92`); `TABLE` (`lock.rs:44`) | `ICfg` | +| `try_enter_local` (`lib.rs:117–131`) | `IStepG.tryShut` / `tryOpen` / `arm` / `enter` / `back` / `clear` | +| `leave_local` (`lib.rs:135–137`) | `IStepG.leave` / `leaveF` | +| `local_entry` (`lib.rs:519–565`): the wait loop, `local`, `leave_local` | `idle` → `tryOpen` …; the body `IPc.loc`; `unlk1` / `leave` | +| `acquire` (`lib.rs:143–186`) | `ticket` / `spin` / `turn` / `inherit` / `ldOpen` / `close1` / `fence` / `scanBusy` / `scanClr` / `close2` | +| `serial` (`lib.rs:991–1010`): the body under the gate | `IStepG.body` | +| `release` (`lib.rs:188–197`), called at kernel exit (`fl4-kernel/src/entry.rs:302`, `fl4-kernel-aarch64/src/entry.rs:96`) | `ldServing` / `ldNextEq` / `ldNextNe` / `open0` / `serve` | +| `try_lock_object` (`lock.rs:61–79`) | `IStepL.held` / `full` / `busy` / `free` / `casOk` / `casFail` | +| `unlock_held` (`lock.rs:82–87`) | `IStepG.unlk1` / `unlk` | +| `with_locks` (`lib.rs:851–863`) | `withLocksP` (the lock attempts it issues, as a `Prog`) | +| `lookup_slot_locked` (`lib.rs:603–628`) | `lookupLoopP` / `lookupSlotLockedP` | + +Transcription choices (part of what TRUSTED covers): +* Tickets are naturals: `u64` wrapping (`a_add`, `wrapping_add`) agrees with `ℕ` while fewer than `2^64` tickets + are outstanding, and at most `MAX_CPUS = 128` are. +* `closed` holds only 0, 1, 2 (`lib.rs:65–68`): it is `Serial.Gate` (`opn`, `draining`, `closed`). +* A lock word is 0 or 1 (`lock.rs:72`); `tbl` records which core set it (ghost owner). +* The flag scan (`lib.rs:157–179`) is one phase `scn p t pend`, `pend` the cores not yet seen clear: a load of a + flag of `d ∈ pend` either clears `d` (`scanClr`) or not (`scanBusy`); the Rust's first pass over `0..cores` and + its re-polls of `busy` are one order of these steps. `cores` (`set_cores`, before any core starts) is `ICfg.run`; + cores outside it never step. +* Fences are no-ops under SC; the SeqCst fence of `lib.rs:156` is its own step (`fence`), the acquire fence of + `lib.rs:182` is folded into the `closed := 2` store (`close2`). +* Diagnostic and policy words are not modelled: `owner` (`lib.rs:185`, `189`; read only by `owner()`, which no + kernel decision uses), `local_hit` (`lib.rs:537`, `555`; it only bounds how long a core waits, which is the + nondeterministic choice between `tryShut` and `ticket`), and `run_syscall`'s `policy` loads and stores. +* The serial body's program `p` is fixed when the core takes its ticket (`ticket p`): it is everything the core + does under the gate between `acquire` returning and `release` (the entry body, or nothing when a remote stall + cancelled it, `lib.rs:1005`). Racy reads a core makes outside any entry (`local_candidate`, `lib.rs:435–437`) + change no shared word and are not steps. +* The local path's program `q` is fixed when the core first finds the gate open (`tryOpen q`); its lock attempts + are `Prog.lock` nodes run by `try_lock_object` (`IStepL`), and its end is `unlock_held` then `leave_local`. + +## Named premises (all [P]; no [J], no [W]) + +* **[P] MM-SB** (Dekker store→load order): the store `active[cpu] := 1` (SeqCst, `lib.rs:125`) is ordered before + the following load of `closed` (SeqCst, `lib.rs:126`), and the holder's store `closed := 1` (SeqCst, `lib.rs:150`) + is ordered by the SeqCst fence (`lib.rs:156`) before its relaxed loads of the flags (`lib.rs:164`, `175`); so in + every execution at least one of "the local path's load sees `closed ≠ 0`" or "the holder's load sees the flag + set" holds (the SB litmus with these barriers is forbidden on x86_64 (locked store) and AArch64 (`STLR`/`LDAR`, + `DMB ISH`)). Used by `IStepG.arm` / `enter` / `back` / `close1` / `fence` / `scanClr` / `scanBusy` being atomic + interleaved steps. +* **[P] MM-DRF** (data-race freedom for the protected words): a plain (volatile) access to a kernel word owned + through the lock or gate chain reads the latest write in the interleaving. The chain is: lock CAS-acquire + (`lock.rs:72`) / store-release (`lock.rs:85`); `closed` acquire (`lib.rs:119`) / release (`lib.rs:194`); `serving` + acquire (`lib.rs:145`) / release (`lib.rs:196`); the holder's acquire fence after the scan (`lib.rs:182`) against + `leave_local`'s release (`lib.rs:136`). Used by `IStepG.rd` / `rdc` / `wr` and `body` being interleaved steps on + `ICfg.mem`. +* **[P] MM-RACY** (`SerialW.lean`): a racy aligned 64-bit `read_volatile` returns some value of the word's coherence + order (no tearing, no out-of-thin-air). Used by `IStepG.rdsR` (any value), matched by `StepR.rdsW`. + Gate gen 3 (the Architect's ruling of 19:10Z): a speculative read is split by the ghost maps `ICfg.gh` and the + untracked words `U` (user memory for FL4): `rds` on a word the path holds or the gate owns and not untracked reads + the current value (**MM-DRF**); `rdsR` on a racy word (not readable, or untracked) whose class neither the gate nor + this core owns may return any value (**MM-RACY**). A racy read of a word whose class the gate or this core owns is + no step of the transcription: `E2E.tr` translates such a read to `rdc`, so no started body issues it. Gate gen 4 + (the Architect's ruling of 19:58Z, gap 2, option (a)): fl4-sync's glue does read such words speculatively (the + CNode's `CAP0`, `ko = .gate`; the current thread's `STATE`, `ko = .core c`) before `.start`, so `rdsC` takes them: + on a racy word whose class the gate or this core owns, any value of the current value's class (**MM-RACY**, and + every write to the word in the epoch keeps its class), matched by `StepR.rdsC`. +* **[P] IPI**: a remote call (stall, reschedule) is serviced in a spin loop (`wait()` in `acquire`, + `service_remote` in `local_entry`) by a core in no local path, and writes only the serviced core's local state; + its effect is sequenced inside the requesting serial body, which waits for the acknowledgement. A guest's hardware + writes during a serial body touch only a VCPU the body does not access without a stall first. Used by + `IStepG.body` being one atomic `Run p` of the body. + **The kernel exit's `settle_resume_point`** (`fl4-kernel/src/entry.rs:840–872`, before `lock()`; gen 1's + finding 3) is no step of `GateImpl` and is not folded into the following serial body: if `lock()` services a + remote stall IPI, `!lock()` is false and the body is skipped, while the remote gate holder observes the + `FAULT_IP` write right after the stall acknowledgement (`fl4-kernel/src/entry.rs:856`, + `fl4-kernel-aarch64/src/entry.rs:73–90`). Nor is it a `SerialHw.HwWr` (that needs a word no core owns; + `ct.FAULT_IP` is `.core c`-owned). It is covered by this premise: `settle_resume_point` runs strictly before + `lock()` services any remote stall IPI and only reads and writes the `.core c`-owned `NEXT_IP` / `FAULT_IP` of + core `c`'s current thread, which no other local path reaches and no concurrent gate holder reaches until core + `c` acknowledges a `remote_tcb_stall` inside `lock()`; so it is sequenced at the tail of core `c`'s preceding + local entry, before the stall acknowledgement. + +## What is proved here + +* `Rel ι s` (below): the gate word is the model's gate; `take` is the `closed := 1` store (`close1`), or, with + `closed = 2` inherited from the predecessor's release, the `serving` acquire (`turn`); each flag load that finds + a core clear is `scan d`, the fence is the holder's own `scan c`, the `closed := 2` store is `drain`; the release's + `closed := 0` store (no successor queued) or `serving` store (one queued, gate kept closed) is `serial`; the + local path's active store is `arm`, its SeqCst load `enter` / `back`, its first unlock store (or, holding none, + the clearing store) `leave`. Locks: `lk o = some d ↔ o` is one of the objects `d`'s path took; a lock attempt on + an object the path already took is a stutter (`dedup`), one on an object whose lock word the path holds by a hash + collision is a successful `lock`, and a collision with another core's word or `MAX_HELD` overflow is `lock false` + (gen 2). Between the body and the release's decisive store the model's memory is the memory before the body + (`IPc.pend`). +* **`impl_sim_gate`**: every step of the gate protocol, of the local path's data accesses and of its end + (`IStepG`) is matched by zero or one `StepR` step keeping `Rel` (gen 3: outside a pending body the model's ghost + maps are the runtime's, `Rel.gh`; the release's `serial` installs the maps the body left). The lock attempts (`IStepL`), the whole + `impl_sim` and `impl_exec` are in `GateLock.lean` (gen 2). +* `rel_init`: `Rel` holds at the start (`SerialGate::new`, `lib.rs:95–104`). + +## Gate gen 4: hardware writes + +`IStepHw ι a x ι'`: the guest of a running VCPU (or the MMU) writes the kernel word `a := x`; only the memory +changes (which words it may write is the instance's premise: `GateExecA.IHwPA`, `HwStepPA`'s [P]). `Rel Q ι s` now +carries the hardware writes `Q` (`HwQ`: each with the memory it was made at) deferred since a pending serial body: +`memP` says the runtime's memory is the body's result `mb` after them (`HwChain mb Q ι.mem`), `qE` that `Q` is empty +with no body pending. With no body pending a hardware write is the model's (`Rel.hwFree`, to `hwCfg`); with one +pending it is deferred (`Rel.hwDefer`). At the release's decisive store (`IDec`) `impl_sim_gate` gives the model's +`serial` to the memory `hqBase Q ι.mem` the body left, after which the model replays `Q` (`hwRun`) and `Rel []` +holds; every other step keeps `Q`. Changed statements: `Rel` (the parameter `Q`, `memP`, `qE`), `Rel.upd` (to any +`Q'`, the new `hqE`), `impl_sim_gate` (the decisive store's replay disjunct), `rel_init` (`Rel []`). +-/ +import Fl4P2Base.SerialW +import Fl4P2Base.SerialSafe +import Fl4P2Base.SerialHw + +namespace Fl4Proofs.P2.GateImpl + +open Fl4Proofs.P2.Serial + +noncomputable section + +variable {A V C L E : Type} [DecidableEq A] [DecidableEq C] [DecidableEq L] [DecidableEq E] + +/-! ## The glue programs the runtime runs -/ + +/-- `with_locks` (`crates/fl4-sync/src/lib.rs:851–863`) as the program it runs: lock each object of the list once +(`lock_object`, `lib.rs:855`), skipping null ones (`o != 0`, here `isNull`) and ones already in `locked` +(`lib.rs:854`), attempting none after the first failure (`ok`); then `f` on the locked list if all were taken +(`lib.rs:860`), else `dflt` (`R::default()`). Its `unlock_held` (`lib.rs:861`) is the local path's end +(`IStepG.unlk1` / `unlk`). -/ +def withLocksP (isNull : L → Bool) (f : List (L × ℕ) → Prog A V L) (dflt : Prog A V L) : + List (L × ℕ) → List (L × ℕ) → Bool → Prog A V L + | [], locked, ok => if ok = true then f locked else dflt + | (o, b) :: r, locked, ok => + if ok = true ∧ isNull o = false ∧ o ∉ locked.map Prod.fst then + .lock o fun ok' => withLocksP isNull f dflt r (locked ++ [(o, b)]) ok' + else withLocksP isNull f dflt r locked ok + +/-- The loop of `lookup_slot_locked` (`lib.rs:613–627`), at most `n` levels: `cspace_step` (`cstep`, line 614; the +`cptr` is closed over), lock the slot (line 615, `None` if taken), read its cap (`readCap`, line 618), and go on +while it is a CNode with bits left (lines 619–623), else return the slot (line 624). -/ +def lookupLoopP {K : Type} (cstep : K → ℕ → Option (L × ℕ)) + (readCap : L → (K → Prog A V L) → Prog A V L) (isCNode : K → Bool) + (k : Option L → Prog A V L) : ℕ → K → ℕ → Prog A V L + | 0, _, _ => k none + | n + 1, cap, bits => + match cstep cap bits with + | none => k none + | some (slot, rest) => + .lock slot fun ok => + if ok = true then + readCap slot fun nx => + if isCNode nx = true ∧ rest ≠ 0 then lookupLoopP cstep readCap isCNode k n nx rest + else k (some slot) + else k none + +/-- `lookup_slot_locked` (`lib.rs:603–628`): read the CTable cap of the thread (`root`, line 611, a gate-owned +slot), then `lookupLoopP` for `maxHeld` levels (`lock::MAX_HELD`, line 613) from `wordBits` (line 612). -/ +def lookupSlotLockedP {K : Type} (cstep : K → ℕ → Option (L × ℕ)) + (readCap : L → (K → Prog A V L) → Prog A V L) (isCNode : K → Bool) (root : L) + (maxHeld wordBits : ℕ) (k : Option L → Prog A V L) : Prog A V L := + readCap root fun cap => lookupLoopP cstep readCap isCNode k maxHeld cap wordBits + +/-! ## Configurations and steps -/ + +/-- Where a core is in the runtime. Ghost parts: the programs `q0`/`q`/`p` (the entry's), the objects taken `T`, +the words written `W`, the ticket `t` and the memory `m0` before the serial body. -/ +inductive IPc (A V C L E : Type) where + /-- In no entry (or spinning in `local_entry`'s wait loop, `lib.rs:541–553`). -/ + | idle + /-- `try_enter_local`: loaded `closed = 0` (`lib.rs:119`), the active store next. -/ + | la (q : Prog A V L) + /-- `try_enter_local`: stored `active := 1` (`lib.rs:125`), the SeqCst load of `closed` next. -/ + | lb (q : Prog A V L) + /-- `try_enter_local`: loaded `closed ≠ 0` (`lib.rs:126`), the clearing store next (`lib.rs:129`). -/ + | lc + /-- Running the local path (`lib.rs:554`): `q` left of `q0`, objects taken `T`, words written `W`, lock words + held `hs` (`lock::Held`, most recent first). -/ + | loc (q0 q : Prog A V L) (T : Set L) (W : Set A) (hs : List E) + /-- `try_lock_object` of `o` (continuation `k`): loaded the lock word 0 (`lock.rs:71`), the CAS next. -/ + | cas (q0 : Prog A V L) (o : L) (k : Bool → Prog A V L) (T : Set L) (W : Set A) (hs : List E) + /-- `unlock_held` (`lock.rs:83–86`) after the first release: `hs` left, then `leave_local`. -/ + | fin (hs : List E) + /-- `acquire`: ticket `t` (`lib.rs:144`), spinning on `serving` (`lib.rs:145–148`). -/ + | tk (p : Prog A V L) (t : ℕ) + /-- `acquire`: `serving = t` seen, the load of `closed` next (`lib.rs:149`). -/ + | cl (p : Prog A V L) (t : ℕ) + /-- `acquire`: `closed ≠ 2` loaded, the store `closed := 1` next (`lib.rs:150`). -/ + | st1 (p : Prog A V L) (t : ℕ) + /-- `acquire`: `closed := 1` stored, the SeqCst fence next (`lib.rs:156`). -/ + | fen (p : Prog A V L) (t : ℕ) + /-- `acquire`: scanning the flags (`lib.rs:157–179`), `pend` not yet seen clear. -/ + | scn (p : Prog A V L) (t : ℕ) (pend : Set C) + /-- Holding the gate, the body next (`serial`, `lib.rs:1008`). -/ + | bd (p : Prog A V L) (t : ℕ) + /-- `release`: body done from `m0`, the load of `serving` next (`lib.rs:190`). -/ + | rl (p : Prog A V L) (t : ℕ) (m0 : A → V) + /-- `release`: `serving = s` loaded, the load of `next` next (`lib.rs:193`). -/ + | rn (p : Prog A V L) (m0 : A → V) (s : ℕ) + /-- `release`: no successor queued, the store `closed := 0` next (`lib.rs:194`). -/ + | rc (p : Prog A V L) (m0 : A → V) (s : ℕ) + /-- `release`: the store `serving := s + 1` next (`lib.rs:196`); `dn`: `closed := 0` was stored. -/ + | rs (p : Prog A V L) (m0 : A → V) (s : ℕ) (dn : Bool) + +/-- The runtime's state (`SerialGate`, `lib.rs:77–92`; the lock table, `lock.rs:44`) and kernel memory. -/ +structure ICfg (A V C L E : Type) where + mem : A → V + /-- `SerialGate::next` (ticket dispenser). -/ + next : ℕ + /-- `Head::serving`. -/ + serving : ℕ + /-- `Head::closed`: 0 `opn`, 1 `draining`, 2 `closed`. -/ + closed : Gate + /-- `SerialGate::active`. -/ + active : C → Bool + /-- `lock::TABLE`: `none` is 0, `some d` is 1 set by core `d` (ghost owner). -/ + tbl : E → Option C + pc : C → IPc A V C L E + /-- The cores the kernel runs (`set_cores`, `lib.rs:109–112`). -/ + run : Set C + /-- Ghost (gate gen 3): the ownership map, value classes and class owners (`Serial.Cfg.own` / `cls` / `ko`), which + only a serial body replaces (`IStepG.body`). They classify a speculative read as non-racy or racy (`IStepG.rds` / + `rdsR`). -/ + gh : ((A → V) → A → Owner C L) × (A → V → V) × (A → ClsOwner C) + +/-- The model's ghost maps, as a triple. -/ +def ghOf (s : Cfg A V C L) : ((A → V) → A → Owner C L) × (A → V → V) × (A → ClsOwner C) := (s.own, s.cls, s.ko) + +/-- One step of core `c` of the gate protocol, the local path's data accesses, and its end. `U`: the untracked words +(user memory for FL4, written by user threads beside the kernel). -/ +inductive IStepG (U : A → Prop) (ι : ICfg A V C L E) (c : C) : ICfg A V C L E → Prop + /-- `try_enter_local`'s first (acquire) load finds `closed ≠ 0` (`lib.rs:119–120`); the core spins on. -/ + | tryShut : ι.pc c = .idle → ι.closed ≠ .opn → IStepG U ι c ι + /-- `try_enter_local`'s first load finds `closed = 0` (`lib.rs:119`); `q` is the local path's program. -/ + | tryOpen (q : Prog A V L) : ι.pc c = .idle → ι.closed = .opn → + IStepG U ι c { ι with pc := Function.update ι.pc c (.la q) } + /-- `active[cpu] := 1`, SeqCst (`lib.rs:125`; **[P] MM-SB**). -/ + | arm {q : Prog A V L} : ι.pc c = .la q → + IStepG U ι c { ι with active := Function.update ι.active c true, + pc := Function.update ι.pc c (.lb q) } + /-- The SeqCst load of `closed` finds 0 (`lib.rs:126–127`; **[P] MM-SB**): the local path runs. -/ + | enter {q : Prog A V L} : ι.pc c = .lb q → ι.closed = .opn → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q q ∅ ∅ []) } + /-- The SeqCst load of `closed` finds it non-zero (`lib.rs:126`; **[P] MM-SB**). -/ + | back {q : Prog A V L} : ι.pc c = .lb q → ι.closed ≠ .opn → + IStepG U ι c { ι with pc := Function.update ι.pc c .lc } + /-- `active[cpu] := 0`, release (`lib.rs:129–130`). -/ + | clear : ι.pc c = .lc → + IStepG U ι c { ι with active := Function.update ι.active c false, + pc := Function.update ι.pc c .idle } + /-- A read of a word the path holds or the gate owns (`PhysStore::read`; **[P] MM-DRF**). -/ + | rd {q0 : Prog A V L} {a : A} {k : V → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.rd a k) T W hs → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q0 (k (ι.mem a)) T W hs) } + /-- A speculative read of a word the path holds or the gate owns, not untracked: the current value + (**[P] MM-DRF**; gate gen 3). -/ + | rds {q0 : Prog A V L} {a : A} {k : V → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.rds a k) T W hs → PO c (· ∈ T) (ι.gh.1 ι.mem a) → ¬ U a → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q0 (k (ι.mem a)) T W hs) } + /-- A speculative read of a racy word (one the path may not read, or untracked) whose value class neither the + gate nor this core owns: any value (**[P] MM-RACY**; gate gen 3). -/ + | rdsR {q0 : Prog A V L} {a : A} {k : V → Prog A V L} {T : Set L} {W : Set A} {hs : List E} (v : V) : + ι.pc c = .loc q0 (.rds a k) T W hs → (¬ PO c (· ∈ T) (ι.gh.1 ι.mem a) ∨ U a) → ι.gh.2.2 a ≠ .gate → + ι.gh.2.2 a ≠ .core c → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q0 (k v) T W hs) } + /-- A speculative read of a racy word whose value class the gate or this core owns: any value of the current + value's class (**[P] MM-RACY**, and every write to the word in the epoch keeps its class: its class owners are the + gate and this core; gate gen 4, the Architect's ruling of 19:58Z, gap 2). -/ + | rdsC {q0 : Prog A V L} {a : A} {k : V → Prog A V L} {T : Set L} {W : Set A} {hs : List E} (v : V) : + ι.pc c = .loc q0 (.rds a k) T W hs → (¬ PO c (· ∈ T) (ι.gh.1 ι.mem a) ∨ U a) → + (ι.gh.2.2 a = .gate ∨ ι.gh.2.2 a = .core c) → ι.gh.2.1 a v = ι.gh.2.1 a (ι.mem a) → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q0 (k v) T W hs) } + /-- A class read (**[P] MM-DRF**: class words change only under the gate or on this core). -/ + | rdc {q0 : Prog A V L} {a : A} {K : V → V → Prop} {k : V → Prog A V L} {T : Set L} {W : Set A} + {hs : List E} : ι.pc c = .loc q0 (.rdc a K k) T W hs → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q0 (k (ι.mem a)) T W hs) } + /-- A write (`PhysStore::write`; **[P] MM-DRF**). -/ + | wr {q0 : Prog A V L} {a : A} {v : V} {k : Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.wr a v k) T W hs → + IStepG U ι c { ι with mem := Function.update ι.mem a v, + pc := Function.update ι.pc c (.loc q0 k T (insert a W) hs) } + /-- The ghost marker where the glue hands over to the extracted body. -/ + | start {q0 : Prog A V L} {k : Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.start k) T W hs → + IStepG U ι c { ι with pc := Function.update ι.pc c (.loc q0 k T W hs) } + /-- The path's program is done: `unlock_held` releases the most recent lock word, store-release + (`lock.rs:84–85`, via `with_locks`, `lib.rs:861`). -/ + | unlk1 {q0 : Prog A V L} {T : Set L} {W : Set A} {e : E} {r : List E} : + ι.pc c = .loc q0 .done T W (e :: r) → + IStepG U ι c { ι with tbl := Function.update ι.tbl e none, pc := Function.update ι.pc c (.fin r) } + /-- The path's program is done holding no lock word: `leave_local` (`lib.rs:136`, via `lib.rs:557`). -/ + | leave {q0 : Prog A V L} {T : Set L} {W : Set A} : ι.pc c = .loc q0 .done T W [] → + IStepG U ι c { ι with active := Function.update ι.active c false, + pc := Function.update ι.pc c .idle } + /-- `unlock_held`'s next release (`lock.rs:83–85`). -/ + | unlk {e : E} {r : List E} : ι.pc c = .fin (e :: r) → + IStepG U ι c { ι with tbl := Function.update ι.tbl e none, pc := Function.update ι.pc c (.fin r) } + /-- `leave_local` after the releases (`lib.rs:136`). -/ + | leaveF : ι.pc c = .fin [] → + IStepG U ι c { ι with active := Function.update ι.active c false, + pc := Function.update ι.pc c .idle } + /-- `acquire`: `ticket = next.fetch_add(1)` (`lib.rs:144`); `p` is everything the core does under the gate. -/ + | ticket (p : Prog A V L) : ι.pc c = .idle → + IStepG U ι c { ι with next := ι.next + 1, pc := Function.update ι.pc c (.tk p ι.next) } + /-- `acquire`: the acquire load of `serving` is not the ticket (`lib.rs:145–147`). -/ + | spin {p : Prog A V L} {t : ℕ} : ι.pc c = .tk p t → ι.serving ≠ t → IStepG U ι c ι + /-- `acquire`: the acquire load of `serving` is the ticket (`lib.rs:145`). -/ + | turn {p : Prog A V L} {t : ℕ} : ι.pc c = .tk p t → ι.serving = t → + IStepG U ι c { ι with pc := Function.update ι.pc c (.cl p t) } + /-- `acquire`: the relaxed load of `closed` finds 2 (`lib.rs:149`): exclusion inherited. -/ + | inherit {p : Prog A V L} {t : ℕ} : ι.pc c = .cl p t → ι.closed = .closed → + IStepG U ι c { ι with pc := Function.update ι.pc c (.bd p t) } + /-- `acquire`: the relaxed load of `closed` does not find 2 (`lib.rs:149`). -/ + | ldOpen {p : Prog A V L} {t : ℕ} : ι.pc c = .cl p t → ι.closed ≠ .closed → + IStepG U ι c { ι with pc := Function.update ι.pc c (.st1 p t) } + /-- `acquire`: `closed := 1`, SeqCst (`lib.rs:150`; **[P] MM-SB**). -/ + | close1 {p : Prog A V L} {t : ℕ} : ι.pc c = .st1 p t → + IStepG U ι c { ι with closed := .draining, pc := Function.update ι.pc c (.fen p t) } + /-- `acquire`: the SeqCst fence (`lib.rs:156`; **[P] MM-SB**), then the scan of cores `0..cores` but this one + (`lib.rs:157–167`). -/ + | fence {p : Prog A V L} {t : ℕ} : ι.pc c = .fen p t → + IStepG U ι c { ι with pc := Function.update ι.pc c (.scn p t (ι.run \ {c})) } + /-- `acquire`: a relaxed load of `d`'s flag finds it set (`lib.rs:164–165`, `175`; **[P] MM-SB**). -/ + | scanBusy {p : Prog A V L} {t : ℕ} {pend : Set C} (d : C) : ι.pc c = .scn p t pend → d ∈ pend → + ι.active d = true → IStepG U ι c ι + /-- `acquire`: a relaxed load of `d`'s flag finds it clear (`lib.rs:164`, `175–176`; **[P] MM-SB**). -/ + | scanClr {p : Prog A V L} {t : ℕ} {pend : Set C} (d : C) : ι.pc c = .scn p t pend → d ∈ pend → + ι.active d = false → + IStepG U ι c { ι with pc := Function.update ι.pc c (.scn p t (pend \ {d})) } + /-- `acquire`: every flag seen clear; the acquire fence (`lib.rs:182`; **[P] MM-DRF**) and `closed := 2` + (`lib.rs:183`). -/ + | close2 {p : Prog A V L} {t : ℕ} {pend : Set C} : ι.pc c = .scn p t pend → pend = ∅ → + IStepG U ι c { ι with closed := .closed, pc := Function.update ι.pc c (.bd p t) } + /-- The serial body, alone (`lib.rs:1008`; **[P] IPI**, **[P] MM-DRF**); it installs the ghost maps `g'` it leaves + (retype, delete, migration and binding change them). -/ + | body {p : Prog A V L} {t : ℕ} (m' : A → V) (g' : ((A → V) → A → Owner C L) × (A → V → V) × (A → ClsOwner C)) : + ι.pc c = .bd p t → Run p ι.mem m' → + IStepG U ι c { ι with mem := m', gh := g', pc := Function.update ι.pc c (.rl p t ι.mem) } + /-- `release`: the relaxed load of `serving` (`lib.rs:190`). -/ + | ldServing {p : Prog A V L} {t : ℕ} {m0 : A → V} : ι.pc c = .rl p t m0 → + IStepG U ι c { ι with pc := Function.update ι.pc c (.rn p m0 ι.serving) } + /-- `release`: the relaxed load of `next` finds no successor (`lib.rs:193`). -/ + | ldNextEq {p : Prog A V L} {m0 : A → V} {s : ℕ} : ι.pc c = .rn p m0 s → ι.next = s + 1 → + IStepG U ι c { ι with pc := Function.update ι.pc c (.rc p m0 s) } + /-- `release`: the relaxed load of `next` finds a successor queued (`lib.rs:193`): the gate stays closed. -/ + | ldNextNe {p : Prog A V L} {m0 : A → V} {s : ℕ} : ι.pc c = .rn p m0 s → ι.next ≠ s + 1 → + IStepG U ι c { ι with pc := Function.update ι.pc c (.rs p m0 s false) } + /-- `release`: `closed := 0`, release (`lib.rs:194`). -/ + | open0 {p : Prog A V L} {m0 : A → V} {s : ℕ} : ι.pc c = .rc p m0 s → + IStepG U ι c { ι with closed := .opn, pc := Function.update ι.pc c (.rs p m0 s true) } + /-- `release`: `serving := s + 1`, release (`lib.rs:196`). -/ + | serve {p : Prog A V L} {m0 : A → V} {s : ℕ} {dn : Bool} : ι.pc c = .rs p m0 s dn → + IStepG U ι c { ι with serving := s + 1, pc := Function.update ι.pc c .idle } + +/-- One step of `try_lock_object` (`crates/fl4-store/src/lock.rs:61–79`) on the local path's next lock attempt, +for the hash `idx` (`index`, `lock.rs:50–57`) and `maxHeld` (`MAX_HELD`, `lock.rs:89`). -/ +inductive IStepL (idx : L → E) (maxHeld : ℕ) (ι : ICfg A V C L E) (c : C) : ICfg A V C L E → Prop + /-- The word is already this path's (`lock.rs:67–69`). -/ + | held {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.lock o k) T W hs → idx o ∈ hs → + IStepL idx maxHeld ι c { ι with pc := Function.update ι.pc c (.loc q0 (k true) (insert o T) W hs) } + /-- `held.n == MAX_HELD` (`lock.rs:70`). -/ + | full {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.lock o k) T W hs → idx o ∉ hs → hs.length = maxHeld → + IStepL idx maxHeld ι c { ι with pc := Function.update ι.pc c (.loc q0 (k false) T W hs) } + /-- The relaxed load of the word finds it set (`lock.rs:71`). -/ + | busy {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.lock o k) T W hs → idx o ∉ hs → hs.length ≠ maxHeld → ι.tbl (idx o) ≠ none → + IStepL idx maxHeld ι c { ι with pc := Function.update ι.pc c (.loc q0 (k false) T W hs) } + /-- The relaxed load of the word finds it clear (`lock.rs:71`): the CAS next. -/ + | free {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .loc q0 (.lock o k) T W hs → idx o ∉ hs → hs.length ≠ maxHeld → ι.tbl (idx o) = none → + IStepL idx maxHeld ι c { ι with pc := Function.update ι.pc c (.cas q0 o k T W hs) } + /-- The CAS 0 → 1 (acquire) succeeds (`lock.rs:72`, `76–78`). -/ + | casOk {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .cas q0 o k T W hs → ι.tbl (idx o) = none → + IStepL idx maxHeld ι c { ι with tbl := Function.update ι.tbl (idx o) (some c), + pc := Function.update ι.pc c (.loc q0 (k true) (insert o T) W (idx o :: hs)) } + /-- The CAS fails (`lock.rs:72–74`). -/ + | casFail {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} {hs : List E} : + ι.pc c = .cas q0 o k T W hs → ι.tbl (idx o) ≠ none → + IStepL idx maxHeld ι c { ι with pc := Function.update ι.pc c (.loc q0 (k false) T W hs) } + +/-- A step of the runtime: by a core the kernel runs, of the gate protocol or of a lock attempt. -/ +def IStep (U : A → Prop) (idx : L → E) (maxHeld : ℕ) (ι : ICfg A V C L E) (c : C) (ι' : ICfg A V C L E) : + Prop := + c ∈ ι.run ∧ (IStepG U ι c ι' ∨ IStepL idx maxHeld ι c ι') + +/-- The runtime at boot (`SerialGate::new`, `lib.rs:95–104`; `TABLE`, `lock.rs:44`): no ticket out, the gate open, +every core idle with its flag clear. -/ +structure IInit (ι : ICfg A V C L E) : Prop where + sv : ι.serving = ι.next + opn : ι.closed = .opn + idle : ∀ d, ι.pc d = .idle + act : ∀ d, ι.active d = false + +/-- **A hardware write** of `a := x` (gate gen 4, the Architect's ruling of 19:58Z, gap 1): the guest of a running +VCPU, or the MMU, writes a kernel word beside the cores (`SerialHw.HwWr`'s runtime side). Only the memory changes; +which words it may write is the instance's premise (for AArch64 `GateExecA.IHwPA`, `HwStepPA`'s [P]). -/ +def IStepHw (ι : ICfg A V C L E) (a : A) (x : V) (ι' : ICfg A V C L E) : Prop := + ι' = { ι with mem := Function.update ι.mem a x } + +/-! ## The refinement relation -/ + +/-- A lock attempt on an object the path already took is no attempt in the model (the glue model does not re-issue +it, as `held.e.contains(e)` succeeds at once, `lock.rs:67–69`). -/ +def dedup : Set L → Prog A V L → Prog A V L + | _, .done => .done + | T, .rd a k => .rd a fun v => dedup T (k v) + | T, .wr a v k => .wr a v (dedup T k) + | T, .lock l k => @ite _ (l ∈ T) (Classical.propDecidable _) (dedup T (k true)) + (.lock l fun b => dedup (if b = true then insert l T else T) (k b)) + | T, .rds a k => .rds a fun v => dedup T (k v) + | T, .start k => .start (dedup T k) + | T, .rdc a K k => .rdc a K fun v => dedup T (k v) + +namespace IPc + +/-- Running a local path (the model's `.loc`). -/ +def isLoc : IPc A V C L E → Prop + | .loc .. | .cas .. => True + | .idle | .la _ | .lb _ | .lc | .fin _ | .tk .. | .cl .. | .st1 .. | .fen .. | .scn .. | .bd .. | .rl .. + | .rn .. | .rc .. | .rs .. => False + +/-- The value of the core's active flag. -/ +def actv : IPc A V C L E → Bool + | .lb _ | .lc | .loc .. | .cas .. | .fin _ => true + | .idle | .la _ | .tk .. | .cl .. | .st1 .. | .fen .. | .scn .. | .bd .. | .rl .. | .rn .. | .rc .. | .rs .. => + false + +/-- The core's ticket (ghost; in the release phases, the `serving` it loaded). -/ +def tick : IPc A V C L E → Option ℕ + | .tk _ t | .cl _ t | .st1 _ t | .fen _ t | .scn _ t _ | .bd _ t | .rl _ t _ => some t + | .rn _ _ s | .rc _ _ s | .rs _ _ s _ => some s + | .idle | .la _ | .lb _ | .lc | .loc .. | .cas .. | .fin _ => none + +/-- Its ticket is being served. -/ +def atSv : IPc A V C L E → Prop + | .cl .. | .st1 .. | .fen .. | .scn .. | .bd .. | .rl .. | .rn .. | .rc .. | .rs .. => True + | .idle | .la _ | .lb _ | .lc | .loc .. | .cas .. | .fin _ | .tk .. => False + +/-- It is the model's gate holder, for the gate word `g`. -/ +def holds (g : Gate) : IPc A V C L E → Prop + | .cl _ _ => g = .closed + | .fen .. | .scn .. | .bd .. | .rl .. | .rn .. | .rc .. => True + | .rs _ _ _ dn => dn = false + | .idle | .la _ | .lb _ | .lc | .loc .. | .cas .. | .fin _ | .tk .. | .st1 .. => False + +/-- What the gate word is while the core is in this phase. -/ +def gOK : IPc A V C L E → Gate → Prop + | .st1 .., g => g ≠ .closed + | .fen .., g | .scn .., g => g = .draining + | .bd .., g | .rl .., g | .rn .., g | .rc .., g => g = .closed + | .rs _ _ _ dn, g => dn = false → g = .closed + | .idle, _ | .la _, _ | .lb _, _ | .lc, _ | .loc .., _ | .cas .., _ | .fin _, _ | .tk .., _ | .cl .., _ => True + +/-- A serial body done but not yet released in the model: its program and the memory before it. -/ +def pend : IPc A V C L E → Option (Prog A V L × (A → V)) + | .rl p _ m0 | .rn p m0 _ | .rc p m0 _ => some (p, m0) + | .rs p m0 _ dn => if dn = true then none else some (p, m0) + | .idle | .la _ | .lb _ | .lc | .loc .. | .cas .. | .fin _ | .tk .. | .cl .. | .st1 .. | .fen .. | .scn .. + | .bd .. => none + +/-- The objects the local path took. -/ +def taken : IPc A V C L E → Set L + | .loc _ _ T _ _ | .cas _ _ _ T _ _ => T + | .idle | .la _ | .lb _ | .lc | .fin _ | .tk .. | .cl .. | .st1 .. | .fen .. | .scn .. | .bd .. | .rl .. + | .rn .. | .rc .. | .rs .. => ∅ + +end IPc + +/-- The model's phase of a core, for the gate word `g`. -/ +def phOf (g : Gate) : IPc A V C L E → Phase A V L + | .lb q => .arm (dedup ∅ q) + | .loc q0 q T W _ => .loc (dedup ∅ q0) (dedup T q) W + | .cas q0 o k T W _ => .loc (dedup ∅ q0) (dedup T (.lock o k)) W + | .cl p _ => if g = .closed then .ser p else .idle + | .fen p _ | .scn p _ _ | .bd p _ | .rl p _ _ | .rn p _ _ | .rc p _ _ => .ser p + | .rs p _ _ dn => if dn = true then .idle else .ser p + | .idle | .la _ | .lc | .fin _ | .tk .. | .st1 .. => .idle + +/-! ## Deferred hardware writes (gate gen 4) -/ + +/-- Hardware writes deferred while a serial body is pending release: each with the memory it was made at, its word, +its value and the lock owning the word. -/ +abbrev HwQ (A V L : Type) := List ((A → V) × A × V × L) + +/-- `m'` is `m` after the writes of `q`, each made at the memory it records. -/ +inductive HwChain : (A → V) → HwQ A V L → (A → V) → Prop + | nil (m : A → V) : HwChain m [] m + | cons {m m' : A → V} {a : A} {x : V} {l : L} {r : HwQ A V L} : + HwChain (Function.update m a x) r m' → HwChain m ((m, a, x, l) :: r) m' + +/-- The memory before the writes of `q` (the first one's), or `m` if there are none. -/ +def hqBase : HwQ A V L → (A → V) → (A → V) + | [], m => m + | (m0, _) :: _, _ => m0 + +/-- The model replaying the hardware writes `q` (`SerialHw.hwCfg` for each, in order). -/ +def hwRun : Cfg A V C L → HwQ A V L → Cfg A V C L + | s, [] => s + | s, (_, a, x, _) :: r => hwRun (hwCfg s a x) r + +/-- **The refinement relation** between the runtime and the model (see the module docstring), with the hardware +writes `Q` deferred since the pending serial body (gate gen 4). The first nine fields are the runtime's own +invariants (flags, the Dekker exclusion, the gate word per phase, tickets); the rest map it to the model. -/ +structure Rel (Q : HwQ A V L) (ι : ICfg A V C L E) (s : Cfg A V C L) : Prop where + runI : ∀ d, d ∉ ι.run → ι.pc d = .idle + act : ∀ d, ι.active d = (ι.pc d).actv + /-- With `closed = 2` no local path runs. -/ + clo : ι.closed = .closed → ∀ d, ¬ (ι.pc d).isLoc + /-- A core the holder has seen clear runs no local path (the Dekker handshake). -/ + drn : ∀ c p t pend, ι.pc c = .scn p t pend → ∀ d ∈ ι.run, d ≠ c → d ∉ pend → ¬ (ι.pc d).isLoc + gph : ∀ d, (ι.pc d).gOK ι.closed + tlt : ∀ d t, (ι.pc d).tick = some t → t < ι.next + tdis : ∀ d e t, (ι.pc d).tick = some t → (ι.pc e).tick = some t → d = e + tsv : ∀ d t, (ι.pc d).tick = some t → (ι.pc d).atSv → t = ι.serving + gate : s.gate = ι.closed + hold : ∀ d, s.holder = some d ↔ (ι.pc d).holds ι.closed + ph : ∀ d, s.ph d = phOf ι.closed (ι.pc d) + seen : ∀ c p t pend, ι.pc c = .scn p t pend → + c ∈ s.seen ∧ ∀ d ∈ ι.run, d ≠ c → d ∉ pend → d ∈ s.seen + lk : ∀ o d, s.lk o = some d ↔ o ∈ (ι.pc d).taken + /-- Between the body and the release's decisive store, the model is still before the body; the runtime's memory is + the body's `mb` after the deferred hardware writes `Q` (gate gen 4). -/ + memP : ∀ d p m0, (ι.pc d).pend = some (p, m0) → s.mem = m0 ∧ ∃ mb, Run p m0 mb ∧ HwChain mb Q ι.mem + memE : (∀ d, (ι.pc d).pend = none) → s.mem = ι.mem + /-- Gate gen 3: outside a pending body the model's ghost maps are the runtime's. -/ + gh : (∀ d, (ι.pc d).pend = none) → ghOf s = ι.gh + /-- Gate gen 4: hardware writes are deferred only while a serial body is pending. -/ + qE : (∀ d, (ι.pc d).pend = none) → Q = [] + +/-! ## Facts about the phases -/ + +namespace IPc + +variable {X : IPc A V C L E} + +theorem atSv_tick (h : X.atSv) : ∃ t, X.tick = some t := by + cases X <;> simp_all [atSv, tick] + +theorem holds_atSv {g : Gate} (h : X.holds g) : X.atSv := by + cases X <;> simp_all [holds, atSv] + +theorem gOK_triv {g : Gate} (h : ¬ X.atSv) : X.gOK g := by + cases X <;> simp_all [gOK, atSv] + +theorem pend_atSv {pm : Prog A V L × (A → V)} (h : X.pend = some pm) : X.atSv := by + cases X <;> simp_all [pend, atSv] + +theorem pend_gOK {pm : Prog A V L × (A → V)} {g : Gate} (h : X.pend = some pm) (hg : X.gOK g) : + g = .closed := by + cases X <;> (try cases ‹Bool›) <;> simp_all [pend, gOK] + +theorem isLoc_actv (h : X.isLoc) : X.actv = true := by + cases X <;> simp_all [isLoc, actv] + +theorem isLoc_natSv (h : X.isLoc) : ¬ X.atSv := by + cases X <;> simp_all [isLoc, atSv] + +theorem isLoc_tick (h : X.isLoc) : X.tick = none := by + cases X <;> simp_all [isLoc, tick] + +theorem isLoc_pend (h : X.isLoc) : X.pend = none := by + cases X <;> simp_all [isLoc, pend] + +theorem isLoc_nscn (h : X.isLoc) (p : Prog A V L) (t : ℕ) (pend : Set C) : X ≠ .scn p t pend := by + rintro rfl; exact h + +end IPc + +open IPc + +theorem phOf_indep {X : IPc A V C L E} {g g' : Gate} (h : ¬ X.atSv) : phOf g X = phOf g' X := by + cases X <;> simp_all [atSv, phOf] + +/-- A core whose flag is clear is neither arming nor in a local path, in the model. -/ +theorem nActive {X : IPc A V C L E} {g : Gate} (h : X.actv = false) : ¬ (phOf g X).Active := by + cases X <;> simp [actv] at h <;> simp only [phOf] <;> (try split) <;> exact fun h => h + +theorem gate_take {g : Gate} (h : g ≠ .closed) : (if g = .opn then Gate.draining else g) = .draining := by + cases g <;> simp_all + +theorem release_ne (lk : L → Option C) (c : C) (o : L) : release lk c o ≠ some c := by + show (if lk o = some c then none else lk o) ≠ some c + by_cases h : lk o = some c + · rw [if_pos h]; exact fun h' => by cases h' + · rw [if_neg h]; exact h + +theorem release_other {lk : L → Option C} {c d : C} {o : L} (hd : d ≠ c) : + release lk c o = some d ↔ lk o = some d := by + show (if lk o = some c then none else lk o) = some d ↔ lk o = some d + by_cases h : lk o = some c + · rw [if_pos h, h] + exact ⟨fun h' => (by cases h'), fun h' => absurd (Option.some.inj h') (Ne.symm hd)⟩ + · rw [if_neg h] + +/-- Closes a side condition about fixed phases. -/ +local macro "gtriv" : tactic => `(tactic| first + | rfl + | exact Iff.rfl + | (intro _ _ _ h; cases h; done) + | (simp [IPc.isLoc, IPc.gOK, IPc.tick, IPc.atSv, IPc.holds, IPc.taken, IPc.pend, IPc.actv, phOf]; done)) + +/-! ## Consequences of `Rel` -/ + +section rel + +variable {ι ι' : ICfg A V C L E} {s s' : Cfg A V C L} {c : C} {X Y : IPc A V C L E} {Q Q' : HwQ A V L} + +/-- At most one core's ticket is being served. -/ +theorem Rel.uniq (R : Rel Q ι s) {a b : C} (ha : (ι.pc a).atSv) (hb : (ι.pc b).atSv) : a = b := by + obtain ⟨ta, hta⟩ := atSv_tick ha + obtain ⟨tb, htb⟩ := atSv_tick hb + have e1 := R.tsv a ta hta ha + have e2 := R.tsv b tb htb hb + subst e1; subst e2 + exact R.tdis a b _ hta htb + +theorem Rel.tick_serving (R : Rel Q ι s) (hs : (ι.pc c).atSv) : (ι.pc c).tick = some ι.serving := by + obtain ⟨t, ht⟩ := atSv_tick hs + rw [ht, R.tsv c t ht hs] + +theorem Rel.holder_none (R : Rel Q ι s) (ht : (ι.pc c).tick = some ι.serving) + (hnh : ¬ (ι.pc c).holds ι.closed) : s.holder = none := by + cases hh : s.holder with + | none => rfl + | some d => + have hd := (R.hold d).mp hh + have e := R.tdis d c ι.serving (R.tick_serving (holds_atSv hd)) ht + subst e; exact absurd hd hnh + +theorem Rel.scn_draining (R : Rel Q ι s) {e : C} {p : Prog A V L} {t : ℕ} {pend : Set C} + (he : ι.pc e = .scn p t pend) : ι.closed = .draining := by + have := R.gph e; rw [he] at this; exact this + +/-- While a local path runs, no serial body is pending. -/ +theorem Rel.nopend (R : Rel Q ι s) (hX : (ι.pc c).isLoc) (d : C) : (ι.pc d).pend = none := by + cases h : (ι.pc d).pend with + | none => rfl + | some pm => exact absurd hX (R.clo (pend_gOK h (R.gph d)) c) + +theorem Rel.pendOnly (R : Rel Q ι s) (hcs : (ι.pc c).atSv) (d : C) (hd : d ≠ c) : (ι.pc d).pend = none := by + cases h : (ι.pc d).pend with + | none => rfl + | some pm => exact absurd (R.uniq (pend_atSv h) hcs) hd + +theorem Rel.allNone (R : Rel Q ι s) (hc : ι.pc c = X) (hX : X.atSv) (hXp : X.pend = none) (d : C) : + (ι.pc d).pend = none := by + by_cases hd : d = c + · rw [hd, hc]; exact hXp + · exact R.pendOnly (by rw [hc]; exact hX) d hd + +theorem Rel.memP_same (R : Rel Q ι s) (hc : ι.pc c = X) (hpc : ι'.pc = Function.update ι.pc c Y) + (hpd : Y.pend = X.pend) (hm : ι'.mem = ι.mem) (hs : s'.mem = s.mem) : + ∀ d p m0, (ι'.pc d).pend = some (p, m0) → s'.mem = m0 ∧ ∃ mb, Run p m0 mb ∧ HwChain mb Q ι'.mem := by + intro d p m0 h + rw [hs, hm] + rw [hpc] at h + by_cases hd : d = c + · subst hd; rw [Function.update_self, hpd, ← hc] at h; exact R.memP _ _ _ h + · rw [Function.update_of_ne hd] at h; exact R.memP _ _ _ h + +theorem Rel.memE_same (R : Rel Q ι s) (hc : ι.pc c = X) (hpc : ι'.pc = Function.update ι.pc c Y) + (hpd : Y.pend = X.pend) (hm : ι'.mem = ι.mem) (hs : s'.mem = s.mem) : + (∀ d, (ι'.pc d).pend = none) → s'.mem = ι'.mem := by + intro h + rw [hs, hm] + apply R.memE + intro d + have h1 := h d + rw [hpc] at h1 + by_cases hd : d = c + · subst hd; rw [Function.update_self, hpd, ← hc] at h1; exact h1 + · rw [Function.update_of_ne hd] at h1; exact h1 + +/-- With no body pending after a step of `c` that keeps `c`'s pending body, none was pending before. -/ +theorem pendNone_prev (hc : ι.pc c = X) (hpc : ι'.pc = Function.update ι.pc c Y) (hpd : Y.pend = X.pend) + (h : ∀ d, (ι'.pc d).pend = none) (d : C) : (ι.pc d).pend = none := by + have h1 := h d + rw [hpc] at h1 + by_cases hd : d = c + · subst hd; rw [Function.update_self, hpd, ← hc] at h1; exact h1 + · rw [Function.update_of_ne hd] at h1; exact h1 + +theorem Rel.ghE_same (R : Rel Q ι s) (hc : ι.pc c = X) (hpc : ι'.pc = Function.update ι.pc c Y) + (hpd : Y.pend = X.pend) (hg : ι'.gh = ι.gh) (hs : ghOf s' = ghOf s) : + (∀ d, (ι'.pc d).pend = none) → ghOf s' = ι'.gh := fun h => by + rw [hs, hg]; exact R.gh (pendNone_prev hc hpc hpd h) + +theorem Rel.qE_same (R : Rel Q ι s) (hc : ι.pc c = X) (hpc : ι'.pc = Function.update ι.pc c Y) + (hpd : Y.pend = X.pend) : (∀ d, (ι'.pc d).pend = none) → Q = [] := fun h => + R.qE (pendNone_prev hc hpc hpd h) + +theorem Rel.hYt_of (R : Rel Q ι s) (hc : ι.pc c = X) (htk : Y.tick = X.tick) (hsvY : Y.atSv → X.atSv) + (hnx : ι.next ≤ ι'.next) (hsv : ι'.serving = ι.serving) : + ∀ t, Y.tick = some t → + t < ι'.next ∧ (∀ d, d ≠ c → (ι.pc d).tick ≠ some t) ∧ (Y.atSv → t = ι'.serving) := by + intro t h + have h1 : (ι.pc c).tick = some t := by rw [hc, ← htk]; exact h + exact ⟨lt_of_lt_of_le (R.tlt c t h1) hnx, fun d hd hd' => hd (R.tdis d c t hd' h1), + fun ha => by rw [hsv]; exact R.tsv c t h1 (by rw [hc]; exact hsvY ha)⟩ + +/-- **The frame lemma**: a step of core `c` from phase `X` to `Y` keeps `Rel`, given what it does to `c` and to +the global words; the other cores' fields follow from `Rel` itself (a change of the gate word or of `serving` is +made only by the core being served, so no other core's phase depends on it). -/ +theorem Rel.upd {Z : Phase A V L} + (R : Rel Q ι s) (hc : ι.pc c = X) (hcr : c ∈ ι.run) + (hpc : ι'.pc = Function.update ι.pc c Y) (hrn : ι'.run = ι.run) + (hA : ∀ d, d ≠ c → ι'.active d = ι.active d) (hAc : ι'.active c = Y.actv) + (hG : ι'.closed ≠ ι.closed → X.atSv) + (hclo : ι'.closed = .closed → (∀ d, d ≠ c → ¬ (ι.pc d).isLoc) ∧ ¬ Y.isLoc) + (hdrnY : ∀ e p t pend, e ≠ c → ι.pc e = .scn p t pend → c ∉ pend → ¬ Y.isLoc) + (hdrnc : ∀ p t pend, Y = .scn p t pend → ∀ d ∈ ι.run, d ≠ c → d ∉ pend → ¬ (ι.pc d).isLoc) + (hgY : Y.gOK ι'.closed) + (hnx : ι.next ≤ ι'.next) (hsv : ι'.serving ≠ ι.serving → X.atSv) + (hYt : ∀ t, Y.tick = some t → + t < ι'.next ∧ (∀ d, d ≠ c → (ι.pc d).tick ≠ some t) ∧ (Y.atSv → t = ι'.serving)) + (hgate : s'.gate = ι'.closed) + (hH : s'.holder = some c ↔ Y.holds ι'.closed) + (hHo : ∀ d, d ≠ c → (s'.holder = some d ↔ s.holder = some d)) + (hPf : s'.ph = Function.update s.ph c Z) (hZ : Z = phOf ι'.closed Y) + (hSo : ∀ e p t pend, e ≠ c → ι.pc e = .scn p t pend → s.seen ⊆ s'.seen) + (hSc : ∀ p t pend, Y = .scn p t pend → + c ∈ s'.seen ∧ ∀ d ∈ ι.run, d ≠ c → d ∉ pend → d ∈ s'.seen) + (hL : ∀ o, s'.lk o = some c ↔ o ∈ Y.taken) + (hLo : ∀ o d, d ≠ c → (s'.lk o = some d ↔ s.lk o = some d)) + (hmP : ∀ d p m0, (ι'.pc d).pend = some (p, m0) → s'.mem = m0 ∧ ∃ mb, Run p m0 mb ∧ HwChain mb Q' ι'.mem) + (hmE : (∀ d, (ι'.pc d).pend = none) → s'.mem = ι'.mem) + (hgE : (∀ d, (ι'.pc d).pend = none) → ghOf s' = ι'.gh) + (hqE : (∀ d, (ι'.pc d).pend = none) → Q' = []) : + Rel Q' ι' s' := by + have hne : ∀ d, d ≠ c → ι'.pc d = ι.pc d := fun d hd => by rw [hpc, Function.update_of_ne hd] + have hcY : ι'.pc c = Y := by rw [hpc, Function.update_self] + have hoth : ∀ d, d ≠ c → ι'.closed ≠ ι.closed → ¬ (ι.pc d).atSv := + fun d hd hg hs => hd (R.uniq hs (by rw [hc]; exact hG hg)) + have hoth' : ∀ d, d ≠ c → ι'.serving ≠ ι.serving → ¬ (ι.pc d).atSv := + fun d hd hg hs => hd (R.uniq hs (by rw [hc]; exact hsv hg)) + refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hgate, ?_, ?_, ?_, ?_, hmP, hmE, hgE, hqE⟩ + · intro d hd + have hdc : d ≠ c := fun h => hd (by rw [hrn, h]; exact hcr) + rw [hne d hdc]; exact R.runI d (by rw [← hrn]; exact hd) + · intro d + by_cases hd : d = c + · subst hd; rw [hAc, hcY] + · rw [hA d hd, hne d hd]; exact R.act d + · intro h d + by_cases hd : d = c + · subst hd; rw [hcY]; exact (hclo h).2 + · rw [hne d hd]; exact (hclo h).1 d hd + · intro e p t pend he d hd hde hdp + rw [hrn] at hd + by_cases hec : e = c + · subst hec + rw [hcY] at he + rw [hne d hde]; exact hdrnc p t pend he d hd hde hdp + · rw [hne e hec] at he + by_cases hdc : d = c + · subst hdc; rw [hcY]; exact hdrnY e p t pend hec he hdp + · rw [hne d hdc]; exact R.drn e p t pend he d hd hde hdp + · intro d + by_cases hd : d = c + · subst hd; rw [hcY]; exact hgY + · rw [hne d hd] + by_cases hg : ι'.closed = ι.closed + · rw [hg]; exact R.gph d + · exact gOK_triv (hoth d hd hg) + · intro d t h + by_cases hd : d = c + · subst hd; rw [hcY] at h; exact (hYt t h).1 + · rw [hne d hd] at h; exact lt_of_lt_of_le (R.tlt d t h) hnx + · intro d e t hd he + by_cases hdc : d = c <;> by_cases hec : e = c + · rw [hdc, hec] + · subst hdc; rw [hcY] at hd; rw [hne e hec] at he; exact absurd he ((hYt t hd).2.1 e hec) + · subst hec; rw [hcY] at he; rw [hne d hdc] at hd; exact absurd hd ((hYt t he).2.1 d hdc) + · rw [hne d hdc] at hd; rw [hne e hec] at he; exact R.tdis d e t hd he + · intro d t h hs + by_cases hd : d = c + · subst hd; rw [hcY] at h hs; exact (hYt t h).2.2 hs + · rw [hne d hd] at h hs + by_cases hsv' : ι'.serving = ι.serving + · rw [hsv']; exact R.tsv d t h hs + · exact absurd hs (hoth' d hd hsv') + · intro d + by_cases hd : d = c + · subst hd; rw [hcY]; exact hH + · rw [hHo d hd, R.hold d, hne d hd] + by_cases hg : ι'.closed = ι.closed + · simp only [hg] + · exact ⟨fun h => absurd (holds_atSv h) (hoth d hd hg), + fun h => absurd (holds_atSv h) (hoth d hd hg)⟩ + · intro d + by_cases hd : d = c + · subst hd; rw [hPf, Function.update_self, hcY]; exact hZ + · rw [hPf, Function.update_of_ne hd, R.ph d, hne d hd] + by_cases hg : ι'.closed = ι.closed + · rw [hg] + · exact phOf_indep (hoth d hd hg) + · intro e p t pend he + by_cases hec : e = c + · subst hec; rw [hcY] at he; rw [hrn]; exact hSc p t pend he + · rw [hne e hec] at he + obtain ⟨h1, h2⟩ := R.seen e p t pend he + have hsub := hSo e p t pend hec he + exact ⟨hsub h1, fun d hd hde hdp => hsub (h2 d (by rw [← hrn]; exact hd) hde hdp)⟩ + · intro o d + by_cases hd : d = c + · subst hd; rw [hcY]; exact hL o + · rw [hLo o d hd, R.lk o d, hne d hd] + +/-- `Rel.upd` for a step the model does not see (a stutter): only `c`'s phase (and the flag it sets) changes, in a +way the model cannot tell apart. -/ +theorem Rel.stut (R : Rel Q ι s) (hc : ι.pc c = X) (hcr : c ∈ ι.run) + (hpc : ι'.pc = Function.update ι.pc c Y) (hrn : ι'.run = ι.run) + (hAct : (ι'.active = ι.active ∧ Y.actv = X.actv) ∨ ι'.active = Function.update ι.active c Y.actv) + (hG : ι'.closed = ι.closed) (hnx : ι'.next = ι.next) (hsv : ι'.serving = ι.serving) + (hmem : ι'.mem = ι.mem) (hgh : ι'.gh = ι.gh) + (hloc : Y.isLoc → X.isLoc) (hscn : ∀ p t pend, Y ≠ .scn p t pend) + (hgO : X.gOK ι.closed → Y.gOK ι.closed) + (htk : ∀ t, Y.tick = some t → X.tick = some t ∧ (Y.atSv → X.atSv)) + (hho : Y.holds ι.closed ↔ X.holds ι.closed) (hph : phOf ι.closed Y = phOf ι.closed X) + (hlk : Y.taken = X.taken) (hpd : Y.pend = X.pend) : Rel Q ι' s := by + have hA : ∀ d, d ≠ c → ι'.active d = ι.active d := by + intro d hd + rcases hAct with ⟨h1, _⟩ | h1 + · rw [h1] + · rw [h1, Function.update_of_ne hd] + have hAc : ι'.active c = Y.actv := by + rcases hAct with ⟨h1, h2⟩ | h1 + · rw [h1, h2, R.act c, hc] + · rw [h1, Function.update_self] + have hXl : ¬ (ι.pc c).isLoc → ¬ Y.isLoc := fun h hy => h (by rw [hc]; exact hloc hy) + refine R.upd hc hcr hpc hrn hA hAc (fun h => absurd hG h) + (fun h => ⟨fun d _ => R.clo (hG.symm.trans h) d, hXl (R.clo (hG.symm.trans h) c)⟩) + (fun e p t pend he hpe hcp => hXl (R.drn e p t pend hpe c hcr (Ne.symm he) hcp)) + (fun p t pend h => absurd h (hscn p t pend)) + (by rw [hG]; exact hgO (by have := R.gph c; rw [hc] at this; exact this)) + (le_of_eq hnx.symm) (fun h => absurd hsv h) ?_ (by rw [hG]; exact R.gate) + (by rw [hG, R.hold c, hc]; exact hho.symm) (fun _ _ => Iff.rfl) + (Function.update_eq_self c s.ph).symm (by rw [R.ph c, hc, hG, hph]) + (fun _ _ _ _ _ _ _ hx => hx) (fun p t pend h => absurd h (hscn p t pend)) + (fun o => by rw [R.lk o c, hc, hlk]) (fun _ _ _ => Iff.rfl) + (R.memP_same hc hpc hpd hmem rfl) (R.memE_same hc hpc hpd hmem rfl) (R.ghE_same hc hpc hpd hgh rfl) + (R.qE_same hc hpc hpd) + intro t h + obtain ⟨h1, h2⟩ := htk t h + have h1' : (ι.pc c).tick = some t := by rw [hc]; exact h1 + exact ⟨(by rw [hnx]; exact R.tlt c t h1'), fun d hd hd' => hd (R.tdis d c t hd' h1'), + fun ha => by rw [hsv]; exact R.tsv c t h1' (by rw [hc]; exact h2 ha)⟩ + +/-- `Rel.upd` for a step inside a local path whose model step may take or keep locks (a data access or a lock +attempt, gen 2): the model's lock table for `c` is `Y`'s objects, the other cores' entries are kept. -/ +theorem Rel.lkStep {Z : Phase A V L} (R : Rel Q ι s) (hc : ι.pc c = X) (hcr : c ∈ ι.run) + (hX : X.isLoc) (hY : Y.isLoc) + (hpc : ι'.pc = Function.update ι.pc c Y) (hrn : ι'.run = ι.run) (hact : ι'.active = ι.active) + (hG : ι'.closed = ι.closed) (hnx : ι'.next = ι.next) (hsv : ι'.serving = ι.serving) + (hgate : s'.gate = s.gate) (hho : s'.holder = s.holder) (hPf : s'.ph = Function.update s.ph c Z) + (hZ : Z = phOf ι.closed Y) (hseen : s'.seen = s.seen) + (hL : ∀ o, s'.lk o = some c ↔ o ∈ Y.taken) + (hLo : ∀ o d, d ≠ c → (s'.lk o = some d ↔ s.lk o = some d)) (hm : s'.mem = ι'.mem) + (hgh : ι'.gh = ι.gh) (hgs : ghOf s' = ghOf s) : Rel Q ι' s' := by + have hXc : (ι.pc c).isLoc := by rw [hc]; exact hX + refine R.upd hc hcr hpc hrn (fun _ _ => by rw [hact]) + (by rw [hact, R.act c, hc, isLoc_actv hX, isLoc_actv hY]) (fun h => absurd hG h) + (fun h => absurd hXc (R.clo (hG.symm.trans h) c)) + (fun e p t pend he hpe hcp _ => R.drn e p t pend hpe c hcr (Ne.symm he) hcp hXc) + (fun p t pend h => absurd h (isLoc_nscn hY p t pend)) (gOK_triv (isLoc_natSv hY)) + (le_of_eq hnx.symm) (fun h => absurd hsv h) + (fun t h => by rw [isLoc_tick hY] at h; cases h) (by rw [hgate, hG]; exact R.gate) + (by rw [hho, R.hold c, hc, hG] + exact ⟨fun h => absurd (holds_atSv h) (isLoc_natSv hX), + fun h => absurd (holds_atSv h) (isLoc_natSv hY)⟩) + (fun d _ => by rw [hho]) hPf (by rw [hZ, hG]) + (fun _ _ _ _ _ _ x hx => by rw [hseen]; exact hx) (fun p t pend h => absurd h (isLoc_nscn hY p t pend)) + hL hLo ?_ (fun _ => hm) (fun _ => by rw [hgs, hgh]; exact R.gh (R.nopend hXc)) (fun _ => R.qE (R.nopend hXc)) + intro d p m0 h + by_cases hd : d = c + · subst hd; rw [hpc, Function.update_self, isLoc_pend hY] at h; cases h + · rw [hpc, Function.update_of_ne hd, R.nopend hXc d] at h; cases h + +/-- `Rel.upd` for a step inside a local path (a data access matched by the same model step). -/ +theorem Rel.locStep {Z : Phase A V L} (R : Rel Q ι s) (hc : ι.pc c = X) (hcr : c ∈ ι.run) + (hX : X.isLoc) (hY : Y.isLoc) + (hpc : ι'.pc = Function.update ι.pc c Y) (hrn : ι'.run = ι.run) (hact : ι'.active = ι.active) + (hG : ι'.closed = ι.closed) (hnx : ι'.next = ι.next) (hsv : ι'.serving = ι.serving) + (hgate : s'.gate = s.gate) (hho : s'.holder = s.holder) (hPf : s'.ph = Function.update s.ph c Z) + (hZ : Z = phOf ι.closed Y) (hseen : s'.seen = s.seen) (hlk : s'.lk = s.lk) + (htk : Y.taken = X.taken) (hm : s'.mem = ι'.mem) (hgh : ι'.gh = ι.gh) (hgs : ghOf s' = ghOf s) : + Rel Q ι' s' := + R.lkStep hc hcr hX hY hpc hrn hact hG hnx hsv hgate hho hPf hZ hseen + (fun o => by rw [hlk, R.lk o c, hc, htk]) (fun o d _ => by rw [hlk]) hm hgh hgs + +/-! ### Hardware writes (gate gen 4) -/ + +theorem HwChain.base {mb m : A → V} {q : HwQ A V L} (h : HwChain mb q m) : mb = hqBase q m := by + cases h <;> rfl + +theorem HwChain.snoc {mb m : A → V} {q : HwQ A V L} (h : HwChain mb q m) (a : A) (x : V) (l : L) : + HwChain mb (q ++ [(m, a, x, l)]) (Function.update m a x) := by + induction h with + | nil m => exact .cons (.nil _) + | cons _ ih => exact .cons ih + +theorem hqBase_snoc (q : HwQ A V L) (m m' : A → V) (a : A) (x : V) (l : L) : + hqBase (q ++ [(m, a, x, l)]) m' = hqBase q m := by + cases q <;> rfl + +/-- Replaying hardware writes changes only the memory and the log. -/ +theorem hwRun_fix (s : Cfg A V C L) (q : HwQ A V L) : + (hwRun s q).gate = s.gate ∧ (hwRun s q).holder = s.holder ∧ (hwRun s q).seen = s.seen ∧ + (hwRun s q).lk = s.lk ∧ (hwRun s q).ph = s.ph ∧ ghOf (hwRun s q) = ghOf s := by + induction q generalizing s with + | nil => exact ⟨rfl, rfl, rfl, rfl, rfl, rfl⟩ + | cons w r ih => + obtain ⟨m, a, x, l⟩ := w + exact ih (hwCfg s a x) + +theorem hwRun_mem {m m' : A → V} {q : HwQ A V L} (h : HwChain m q m') {s : Cfg A V C L} (hs : s.mem = m) : + (hwRun s q).mem = m' := by + induction h generalizing s with + | nil => exact hs + | cons _ ih => exact ih (by show Function.update s.mem _ _ = _; rw [hs]) + +/-- `Rel` depends on the model state only through its gate, holder, seen set, locks, phases, memory and ghost maps +(not its log). -/ +theorem Rel.of_eq {t : Cfg A V C L} (R : Rel Q ι s) (hg : t.gate = s.gate) (hh : t.holder = s.holder) + (hse : t.seen = s.seen) (hl : t.lk = s.lk) (hp : t.ph = s.ph) (hm : t.mem = s.mem) (hgh : ghOf t = ghOf s) : + Rel Q ι t := by + refine ⟨R.runI, R.act, R.clo, R.drn, R.gph, R.tlt, R.tdis, R.tsv, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, R.qE⟩ + · rw [hg]; exact R.gate + · intro d; rw [hh]; exact R.hold d + · intro d; rw [hp]; exact R.ph d + · intro c p t pend h; rw [hse]; exact R.seen c p t pend h + · intro o d; rw [hl]; exact R.lk o d + · intro d p m0 h; rw [hm]; exact R.memP d p m0 h + · intro h; rw [hm]; exact R.memE h + · intro h; rw [hgh]; exact R.gh h + +/-- The model's lock `l` is free when no core's path took it. -/ +theorem Rel.lk_free (R : Rel Q ι s) {l : L} (h : ∀ d, l ∉ (ι.pc d).taken) : s.lk l = none := by + cases hl : s.lk l with + | none => rfl + | some d => exact absurd ((R.lk l d).mp hl) (h d) + +/-- **A hardware write with no serial body pending** is the model's hardware write (`hwCfg`). -/ +theorem Rel.hwFree (R : Rel Q ι s) (hp : ∀ d, (ι.pc d).pend = none) (a : A) (x : V) : + Rel Q { ι with mem := Function.update ι.mem a x } (hwCfg s a x) := + ⟨R.runI, R.act, R.clo, R.drn, R.gph, R.tlt, R.tdis, R.tsv, R.gate, R.hold, R.ph, R.seen, R.lk, + fun d _ _ h => (by cases (hp d).symm.trans h), + fun _ => (by show Function.update s.mem a x = Function.update ι.mem a x; rw [R.memE hp]), + fun _ => R.gh hp, fun _ => R.qE hp⟩ + +/-- **A hardware write while a serial body is pending** is deferred: appended to `Q`, the model unchanged. -/ +theorem Rel.hwDefer (R : Rel Q ι s) (hp : ¬ ∀ d, (ι.pc d).pend = none) (a : A) (x : V) (l : L) : + Rel (Q ++ [(ι.mem, a, x, l)]) { ι with mem := Function.update ι.mem a x } s := + ⟨R.runI, R.act, R.clo, R.drn, R.gph, R.tlt, R.tdis, R.tsv, R.gate, R.hold, R.ph, R.seen, R.lk, + fun d p m0 h => (by + obtain ⟨h1, mb, hr, hch⟩ := R.memP d p m0 h + exact ⟨h1, mb, hr, hch.snoc a x l⟩), + fun h => absurd h hp, fun h => absurd h hp, fun h => absurd h hp⟩ + +end rel + +/-- **The decisive store of a release** (`release`'s `closed := 0`, `lib.rs:194`, or its `serving` store with a +successor queued, `lib.rs:196`): the core held the closed gate and is idle in the model after the step. Defined on +the runtime alone; it is the runtime side of the model's `serial`. -/ +def IDec (ι : ICfg A V C L E) (c : C) (ι' : ICfg A V C L E) : Prop := + (ι.pc c).holds ι.closed ∧ ι.closed = .closed ∧ phOf ι'.closed (ι'.pc c) = .idle + +/-- The model state after `c`'s serial step `p` ending at memory `m`, with gate `keep` and ghost maps `g` +(`Step.serial`'s). -/ +abbrev serCfg (s : Cfg A V C L) (c : C) (p : Prog A V L) (m : A → V) (keep : Bool) + (g : ((A → V) → A → Owner C L) × (A → V → V) × (A → ClsOwner C)) : Cfg A V C L := + { s with mem := m, holder := none, ph := Function.update s.ph c .idle, gate := if keep then .closed else .opn, + log := s.log ++ [p], own := g.1, cls := g.2.1, ko := g.2.2 } + +/-! ## The gate side of the simulation -/ + +/-- **Every gate-protocol step of the runtime is matched by zero or one model step** (`StepR`: a speculative read +may return a stale value, [P] MM-RACY), keeping `Rel`. `IStepG` covers `try_enter_local`, `leave_local`, +`acquire`, the serial body, `release`, the local path's data accesses and `unlock_held`; the lock attempts +(`IStepL`, `try_lock_object`) are gen 2. -/ +theorem impl_sim_gate {U : A → Prop} {ι ι' : ICfg A V C L E} {s : Cfg A V C L} {c : C} {Q : HwQ A V L} + (R : Rel Q ι s) (hcr : c ∈ ι.run) (h : IStepG U ι c ι') : + ∃ s1, (s1 = s ∨ StepR U s c s1) ∧ + (Rel Q ι' s1 ∨ (IDec ι c ι' ∧ s1.mem = hqBase Q ι.mem ∧ Rel [] ι' (hwRun s1 Q))) := by + have lift : (∃ s', (s' = s ∨ StepR U s c s') ∧ Rel Q ι' s') → + ∃ s1, (s1 = s ∨ StepR U s c s1) ∧ + (Rel Q ι' s1 ∨ (IDec ι c ι' ∧ s1.mem = hqBase Q ι.mem ∧ Rel [] ι' (hwRun s1 Q))) := + fun ⟨s', h1, h2⟩ => ⟨s', h1, Or.inl h2⟩ + cases h with + | tryShut _ _ => exact lift ⟨s, Or.inl rfl, R⟩ + | spin _ _ => exact lift ⟨s, Or.inl rfl, R⟩ + | scanBusy _ _ _ _ => exact lift ⟨s, Or.inl rfl, R⟩ + | tryOpen q hc _ => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (by gtriv) (by gtriv) (by gtriv) (by gtriv) rfl rfl⟩ + | @arm q hc => + have hph : s.ph c = .idle := by rw [R.ph c, hc]; rfl + refine lift ⟨_, Or.inr (.step (Step.arm (dedup ∅ q) hph)), R.upd hc hcr rfl rfl + (fun d hd => Function.update_of_ne hd _ _) (Function.update_self _ _ _) (fun h => absurd rfl h) + (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) (by gtriv) le_rfl + (fun h => absurd rfl h) (by gtriv) R.gate ((R.hold c).trans (by simp [hc, IPc.holds])) + (fun _ _ => Iff.rfl) rfl rfl (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) + (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) + (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + | @enter q hc hg => + refine lift ⟨_, Or.inr (.step (Step.enter (p := dedup ∅ q) (by rw [R.ph c, hc]; rfl) (R.gate.trans hg))), + R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => absurd (hg.symm.trans h) (by simp)) ?_ (by gtriv) (by gtriv) + le_rfl (fun h => absurd rfl h) (by gtriv) R.gate ((R.hold c).trans (by simp [hc, IPc.holds])) + (fun _ _ => Iff.rfl) rfl rfl (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) + (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) + (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + intro e _ _ _ _ he _ + exact absurd ((R.scn_draining he).symm.trans hg) (by simp) + | @back q hc hg => + refine lift ⟨_, Or.inr (.step (Step.back (p := dedup ∅ q) (by rw [R.ph c, hc]; rfl) + (by rw [R.gate]; exact hg))), + R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) + (by gtriv) le_rfl (fun h => absurd rfl h) (by gtriv) R.gate + ((R.hold c).trans (by simp [hc, IPc.holds])) (fun _ _ => Iff.rfl) rfl rfl + (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + | clear hc => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inr rfl) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (by gtriv) (by gtriv) (by gtriv) (by gtriv) rfl rfl⟩ + | @rd q0 a k T W hs hc => + have hph : s.ph c = .loc (dedup ∅ q0) (.rd a fun v => dedup T (k v)) W := by rw [R.ph c, hc]; rfl + have hm : s.mem = ι.mem := R.memE (R.nopend (by rw [hc]; trivial)) + exact lift ⟨_, Or.inr (.step (Step.rd hph)), R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl (by rw [hm]; rfl) rfl rfl rfl hm rfl rfl⟩ + | @rds q0 a k T W hs hc _ _ => + have hph : s.ph c = .loc (dedup ∅ q0) (.rds a fun v => dedup T (k v)) W := by rw [R.ph c, hc]; rfl + have hm : s.mem = ι.mem := R.memE (R.nopend (by rw [hc]; trivial)) + exact lift ⟨_, Or.inr (.step (Step.rds hph)), R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl (by rw [hm]; rfl) rfl rfl rfl hm rfl rfl⟩ + | @rdsR q0 a k T W hs v hc hp hk1 hk2 => + have hph : s.ph c = .loc (dedup ∅ q0) (.rds a fun v => dedup T (k v)) W := by rw [R.ph c, hc]; rfl + have hnp := R.nopend (by rw [hc]; trivial) + have hm : s.mem = ι.mem := R.memE hnp + have hg : ghOf s = ι.gh := R.gh hnp + have ho : s.own = ι.gh.1 := by rw [← hg]; rfl + have hko : s.ko = ι.gh.2.2 := by rw [← hg]; rfl + have hp' : ¬ Perm s c a ∨ U a := hp.imp_left fun h hP => h (by + rw [← ho, ← hm] + exact Or.imp_left (fun h' => (heldV_congr (fun l => (R.lk l c).trans (by simp only [hc, IPc.taken])) _).mp + (heldV_of_held h')) hP) + exact lift ⟨_, Or.inr (StepR.rdsW v hph hp' (by rw [hko]; exact hk1) (by rw [hko]; exact hk2)), + R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl rfl rfl rfl rfl hm rfl rfl⟩ + | @rdsC q0 a k T W hs v hc hp hk hcl => + have hph : s.ph c = .loc (dedup ∅ q0) (.rds a fun v => dedup T (k v)) W := by rw [R.ph c, hc]; rfl + have hnp := R.nopend (by rw [hc]; trivial) + have hm : s.mem = ι.mem := R.memE hnp + have hg : ghOf s = ι.gh := R.gh hnp + have ho : s.own = ι.gh.1 := by rw [← hg]; rfl + have hcs : s.cls = ι.gh.2.1 := by rw [← hg]; rfl + have hko : s.ko = ι.gh.2.2 := by rw [← hg]; rfl + have hp' : ¬ Perm s c a ∨ U a := hp.imp_left fun h hP => h (by + rw [← ho, ← hm] + exact Or.imp_left (fun h' => (heldV_congr (fun l => (R.lk l c).trans (by simp only [hc, IPc.taken])) _).mp + (heldV_of_held h')) hP) + exact lift ⟨_, Or.inr (StepR.rdsC v hph hp' (by rw [hko]; exact hk) (by rw [hcs, hm]; exact hcl)), + R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl rfl rfl rfl rfl hm rfl rfl⟩ + | @rdc q0 a K k T W hs hc => + have hph : s.ph c = .loc (dedup ∅ q0) (.rdc a K fun v => dedup T (k v)) W := by + rw [R.ph c, hc]; rfl + have hm : s.mem = ι.mem := R.memE (R.nopend (by rw [hc]; trivial)) + exact lift ⟨_, Or.inr (.step (Step.rdc hph)), R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl (by rw [hm]; rfl) rfl rfl rfl hm rfl rfl⟩ + | @wr q0 a v k T W hs hc => + have hph : s.ph c = .loc (dedup ∅ q0) (.wr a v (dedup T k)) W := by rw [R.ph c, hc]; rfl + have hm : s.mem = ι.mem := R.memE (R.nopend (by rw [hc]; trivial)) + exact lift ⟨_, Or.inr (.step (Step.wr hph)), R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl rfl rfl rfl rfl (by show Function.update s.mem a v = Function.update ι.mem a v; rw [hm]) rfl rfl⟩ + | @start q0 k T W hs hc => + have hph : s.ph c = .loc (dedup ∅ q0) (.start (dedup T k)) W := by rw [R.ph c, hc]; rfl + have hm : s.mem = ι.mem := R.memE (R.nopend (by rw [hc]; trivial)) + exact lift ⟨_, Or.inr (.step (Step.start hph)), R.locStep hc hcr (by trivial) (by trivial) rfl rfl rfl rfl rfl rfl + rfl rfl rfl rfl rfl rfl rfl hm rfl rfl⟩ + | @unlk1 q0 T W e r hc => + have hph : s.ph c = .loc (dedup ∅ q0) .done W := by rw [R.ph c, hc]; rfl + exact lift ⟨_, Or.inr (.step (Step.leave hph)), R.upd hc hcr rfl rfl (fun _ _ => rfl) + ((R.act c).trans (by simp [hc, IPc.actv])) (fun h => absurd rfl h) + (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) (by gtriv) le_rfl + (fun h => absurd rfl h) (by gtriv) R.gate ((R.hold c).trans (by simp [hc, IPc.holds])) + (fun _ _ => Iff.rfl) rfl rfl (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) + (fun o => ⟨fun h => absurd h (release_ne _ _ _), fun h => by simp [IPc.taken] at h⟩) + (fun o d hd => release_other hd) (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + | @leave q0 T W hc => + have hph : s.ph c = .loc (dedup ∅ q0) .done W := by rw [R.ph c, hc]; rfl + exact lift ⟨_, Or.inr (.step (Step.leave hph)), R.upd hc hcr rfl rfl + (fun d hd => Function.update_of_ne hd _ _) (Function.update_self _ _ _) (fun h => absurd rfl h) + (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) (by gtriv) le_rfl + (fun h => absurd rfl h) (by gtriv) R.gate ((R.hold c).trans (by simp [hc, IPc.holds])) + (fun _ _ => Iff.rfl) rfl rfl (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) + (fun o => ⟨fun h => absurd h (release_ne _ _ _), fun h => by simp [IPc.taken] at h⟩) + (fun o d hd => release_other hd) (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + | unlk hc => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (by gtriv) (by gtriv) (by gtriv) (by gtriv) rfl rfl⟩ + | leaveF hc => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inr rfl) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (by gtriv) (by gtriv) (by gtriv) (by gtriv) rfl rfl⟩ + | ticket p hc => + refine lift ⟨s, Or.inl rfl, R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) (by gtriv) + (Nat.le_succ _) (fun h => absurd rfl h) ?_ R.gate ((R.hold c).trans (by simp [hc, IPc.holds])) + (fun _ _ => Iff.rfl) (Function.update_eq_self c s.ph).symm (by rw [R.ph c, hc]; rfl) + (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + intro t h + simp only [IPc.tick, Option.some.injEq] at h + subst h + exact ⟨Nat.lt_succ_self _, fun d _ hd => lt_irrefl _ (R.tlt d _ hd), + fun h => by simp [IPc.atSv] at h⟩ + | @turn p t hc hs => + have hYt : ∀ t', (IPc.cl p t : IPc A V C L E).tick = some t' → + t' < ι.next ∧ (∀ d, d ≠ c → (ι.pc d).tick ≠ some t') ∧ + ((IPc.cl p t : IPc A V C L E).atSv → t' = ι.serving) := by + intro t' h + simp only [IPc.tick, Option.some.injEq] at h + subst h + exact ⟨R.tlt c t (by rw [hc]; rfl), fun d hd h' => hd (R.tdis d c t h' (by rw [hc]; rfl)), + fun _ => hs.symm⟩ + by_cases hg : ι.closed = .closed + · have hnone : s.holder = none := + R.holder_none (by rw [hc]; exact congrArg some hs.symm) (by simp [hc, IPc.holds]) + refine lift ⟨_, Or.inr (.step (Step.take p (by rw [R.ph c, hc]; rfl) hnone)), R.upd hc hcr rfl rfl + (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) (fun h => absurd rfl h) + (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) (by gtriv) le_rfl + (fun h => absurd rfl h) hYt ?_ ⟨fun _ => hg, fun _ => rfl⟩ ?_ rfl (by simp [phOf, hg]) ?_ + (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) + (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + · show (if s.gate = .opn then Gate.draining else s.gate) = ι.closed + simp [R.gate, hg] + · intro d hd; simp [hnone, Ne.symm hd] + · intro e _ _ _ _ he + exact absurd ((R.scn_draining he).symm.trans hg) (by simp) + · exact lift ⟨s, Or.inl rfl, R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) + (by gtriv) le_rfl (fun h => absurd rfl h) hYt R.gate + ((R.hold c).trans (by simp [hc, IPc.holds, hg])) (fun _ _ => Iff.rfl) + (Function.update_eq_self c s.ph).symm (by rw [R.ph c, hc]; simp [phOf, hg]) + (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + | @inherit p t hc hg => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (fun _ => hg) (by gtriv) (by simp [IPc.holds, hg]) (by simp [phOf, hg]) rfl rfl⟩ + | @ldOpen p t hc hg => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (fun _ => hg) (by gtriv) (by simp [IPc.holds, hg]) (by simp [phOf, hg]) rfl rfl⟩ + | @close1 p t hc => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hnone : s.holder = none := R.holder_none (R.tick_serving hsv0) (by simp [hc, IPc.holds]) + have hg : ι.closed ≠ .closed := by have := R.gph c; rw [hc] at this; exact this + refine lift ⟨_, Or.inr (.step (Step.take p (by rw [R.ph c, hc]; rfl) hnone)), R.upd hc hcr rfl rfl + (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) (fun _ => trivial) + (fun h => by simp at h) ?_ (by gtriv) rfl le_rfl (fun h => absurd rfl h) + (R.hYt_of hc rfl (fun _ => trivial) le_rfl rfl) ?_ ⟨fun _ => trivial, fun _ => rfl⟩ ?_ rfl rfl ?_ + (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) + (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + · exact fun e _ _ _ he hpe _ => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + · show (if s.gate = .opn then Gate.draining else s.gate) = Gate.draining + rw [R.gate]; exact gate_take hg + · intro d hd; simp [hnone, Ne.symm hd] + · exact fun e _ _ _ he hpe => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + | @fence p t hc => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hd0 : ι.closed = .draining := by have := R.gph c; rw [hc] at this; exact this + have hho : s.holder = some c := (R.hold c).mpr (by rw [hc]; trivial) + refine lift ⟨_, Or.inr (.step (Step.scan c hho (R.gate.trans hd0) (by rw [R.ph c, hc]; exact fun h => h))), + R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => absurd (hd0.symm.trans h) (by simp)) ?_ ?_ hd0 le_rfl + (fun h => absurd rfl h) (R.hYt_of hc rfl (fun _ => trivial) le_rfl rfl) R.gate + ⟨fun _ => trivial, fun _ => hho⟩ (fun _ _ => Iff.rfl) (Function.update_eq_self c s.ph).symm + (by rw [R.ph c, hc]; rfl) (fun _ _ _ _ _ _ => Set.subset_insert _ _) ?_ + (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) + (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + · exact fun e _ _ _ he hpe _ => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + · intro p' t' pend' hY d hd hdc hnp + injection hY with _ _ h3 + subst h3 + exact (hnp (Set.mem_diff_of_mem hd hdc)).elim + · intro p' t' pend' hY + injection hY with _ _ h3 + subst h3 + exact ⟨Set.mem_insert c _, fun d hd hdc hnp => (hnp (Set.mem_diff_of_mem hd hdc)).elim⟩ + | @scanClr p t pend d hc hdp ha => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hd0 : ι.closed = .draining := by have := R.gph c; rw [hc] at this; exact this + have hho : s.holder = some c := (R.hold c).mpr (by rw [hc]; trivial) + have hna : ¬ (s.ph d).Active := by rw [R.ph d]; exact nActive ((R.act d).symm.trans ha) + refine lift ⟨_, Or.inr (.step (Step.scan d hho (R.gate.trans hd0) hna)), + R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => absurd (hd0.symm.trans h) (by simp)) ?_ ?_ hd0 le_rfl + (fun h => absurd rfl h) (R.hYt_of hc rfl (fun _ => trivial) le_rfl rfl) R.gate + ⟨fun _ => trivial, fun _ => hho⟩ (fun _ _ => Iff.rfl) (Function.update_eq_self c s.ph).symm + (by rw [R.ph c, hc]; rfl) (fun _ _ _ _ _ _ => Set.subset_insert _ _) ?_ + (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) + (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + · exact fun e _ _ _ he hpe _ => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + · intro p' t' pend' hY e he hec hnp + injection hY with _ _ h3 + subst h3 + by_cases hed : e = d + · intro hl + rw [hed] at hl + have h1 := (R.act d).symm.trans ha + rw [isLoc_actv hl] at h1 + cases h1 + · exact R.drn c p t pend hc e he hec (fun hp => hnp (Set.mem_diff_of_mem hp hed)) + · intro p' t' pend' hY + injection hY with _ _ h3 + subst h3 + obtain ⟨h1, h2⟩ := R.seen c p t pend hc + refine ⟨Set.mem_insert_of_mem _ h1, fun e he hec hnp => ?_⟩ + by_cases hed : e = d + · rw [hed]; exact Set.mem_insert d _ + · exact Set.mem_insert_of_mem _ (h2 e he hec (fun hp => hnp (Set.mem_diff_of_mem hp hed))) + | @close2 p t pend hc hpe => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hd0 : ι.closed = .draining := by have := R.gph c; rw [hc] at this; exact this + have hho : s.holder = some c := (R.hold c).mpr (by rw [hc]; trivial) + have hall : ∀ d, d ∈ s.seen ∨ s.ph d = .idle := by + intro d + obtain ⟨h1, h2⟩ := R.seen c p t pend hc + by_cases hdr : d ∈ ι.run + · by_cases hdc : d = c + · rw [hdc]; exact Or.inl h1 + · exact Or.inl (h2 d hdr hdc (by rw [hpe]; exact fun h => h)) + · right; rw [R.ph d, R.runI d hdr]; rfl + refine lift ⟨_, Or.inr (.step (Step.drain hho (R.gate.trans hd0) hall)), + R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun _ => trivial) ?_ ?_ (by gtriv) rfl le_rfl (fun h => absurd rfl h) + (R.hYt_of hc rfl (fun _ => trivial) le_rfl rfl) rfl ⟨fun _ => trivial, fun _ => hho⟩ + (fun _ _ => Iff.rfl) (Function.update_eq_self c s.ph).symm (by rw [R.ph c, hc]; rfl) + (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) (R.memP_same hc rfl rfl rfl rfl) (R.memE_same hc rfl rfl rfl rfl) (R.ghE_same hc rfl rfl rfl rfl) (R.qE_same hc rfl rfl)⟩ + · intro _ + refine ⟨fun d hdc hl => ?_, by gtriv⟩ + by_cases hdr : d ∈ ι.run + · exact R.drn c p t pend hc d hdr hdc (by rw [hpe]; exact fun h => h) hl + · rw [R.runI d hdr] at hl; exact hl + · exact fun e _ _ _ he hpe _ => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + | @body p t m' g' hc hr => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hnp : ∀ d, (ι.pc d).pend = none := R.allNone hc trivial rfl + have hQ := R.qE hnp + subst hQ + refine lift ⟨s, Or.inl rfl, R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) ?_ le_rfl + (fun h => absurd rfl h) (R.hYt_of hc rfl (fun _ => trivial) le_rfl rfl) R.gate + ((R.hold c).trans (by simp [hc, IPc.holds])) (fun _ _ => Iff.rfl) (Function.update_eq_self c s.ph).symm + (by rw [R.ph c, hc]; rfl) (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) + (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) (fun _ _ _ => Iff.rfl) ?_ ?_ ?_ ?_⟩ + · have := R.gph c; rw [hc] at this; exact this + · intro d p' m0 h + by_cases hd : d = c + · subst hd + simp only [Function.update_self, IPc.pend, Option.some.injEq, Prod.mk.injEq] at h + obtain ⟨rfl, rfl⟩ := h + exact ⟨R.memE hnp, m', hr, .nil _⟩ + · simp only [Function.update_of_ne hd] at h + rw [hnp d] at h; cases h + · intro h + have h1 := h c + simp [IPc.pend] at h1 + · intro h + have h1 := h c + simp [IPc.pend] at h1 + · intro h + have h1 := h c + simp [IPc.pend] at h1 + | @ldServing p t m0 hc => + have ht : t = ι.serving := R.tsv c t (by rw [hc]; rfl) (by rw [hc]; trivial) + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (fun h => h) + (by + intro t' h + simp only [IPc.tick, Option.some.injEq] at h + subst h + exact ⟨by rw [ht]; rfl, fun _ => trivial⟩) + (by gtriv) rfl rfl rfl⟩ + | @ldNextEq p m0 sv hc _ => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (fun h => h) (by gtriv) (by gtriv) rfl rfl rfl⟩ + | @ldNextNe p m0 sv hc _ => + exact lift ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (by gtriv) + (by gtriv) (fun h _ => h) (by gtriv) (by gtriv) (by gtriv) rfl (by gtriv)⟩ + | @open0 p m0 sv hc => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hcl0 : ι.closed = .closed := by have := R.gph c; rw [hc] at this; exact this + have hho : s.holder = some c := (R.hold c).mpr (by rw [hc]; trivial) + obtain ⟨hm0, mb, hr, hch⟩ := R.memP c p m0 (by rw [hc]; rfl) + have hr' : Run p s.mem mb := by rw [hm0]; exact hr + have hst : StepR U s c (serCfg s c p mb false ι.gh) := + .step (Step.serial false ι.gh.1 ι.gh.2.1 ι.gh.2.2 hho (R.gate.trans hcl0) (by rw [R.ph c, hc]; rfl) hr') + have R1 : Rel [] { ι with closed := .opn, pc := Function.update ι.pc c (.rs p m0 sv true) } + (serCfg s c p ι.mem false ι.gh) := by + refine R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) (fun _ => trivial) + (fun h => by simp at h) ?_ (by gtriv) (by gtriv) le_rfl (fun h => absurd rfl h) + (R.hYt_of hc rfl (fun _ => trivial) le_rfl rfl) (by simp [serCfg]) (by simp [serCfg, IPc.holds]) ?_ rfl + (by gtriv) (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) ?_ (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) + · exact fun e _ _ _ he hpe _ => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + · intro d hd + constructor + · intro h; simp [serCfg] at h + · intro h; exact absurd (Option.some.inj (hho.symm.trans h)) (Ne.symm hd) + · intro d p' m0' h + by_cases hd : d = c + · subst hd; simp [IPc.pend] at h + · simp only [Function.update_of_ne hd] at h + rw [R.pendOnly hsv0 d hd] at h; cases h + have hf := hwRun_fix (serCfg s c p mb false ι.gh) Q + exact ⟨_, Or.inr hst, Or.inr ⟨⟨by rw [hc]; trivial, hcl0, by simp [phOf]⟩, hch.base, + R1.of_eq hf.1 hf.2.1 hf.2.2.1 hf.2.2.2.1 hf.2.2.2.2.1 (hwRun_mem hch rfl) hf.2.2.2.2.2⟩⟩ + | @serve p m0 sv dn hc => + cases dn with + | true => + exact lift ⟨s, Or.inl rfl, R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) (by gtriv) (by gtriv) + (by gtriv) le_rfl (fun _ => trivial) (by gtriv) R.gate ((R.hold c).trans (by simp [hc, IPc.holds])) + (fun _ _ => Iff.rfl) (Function.update_eq_self c s.ph).symm (by rw [R.ph c, hc]; rfl) + (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) (R.memP_same hc rfl (by simp [IPc.pend]) rfl rfl) + (R.memE_same hc rfl (by simp [IPc.pend]) rfl rfl) (R.ghE_same hc rfl (by simp [IPc.pend]) rfl rfl) + (R.qE_same hc rfl (by simp [IPc.pend]))⟩ + | false => + have hsv0 : (ι.pc c).atSv := by rw [hc]; trivial + have hcl0 : ι.closed = .closed := by have := R.gph c; rw [hc] at this; exact this rfl + have hho : s.holder = some c := (R.hold c).mpr (by rw [hc]; exact rfl) + obtain ⟨hm0, mb, hr, hch⟩ := R.memP c p m0 (by simp [hc, IPc.pend]) + have hr' : Run p s.mem mb := by rw [hm0]; exact hr + have hst : StepR U s c (serCfg s c p mb true ι.gh) := + .step (Step.serial true ι.gh.1 ι.gh.2.1 ι.gh.2.2 hho (R.gate.trans hcl0) (by rw [R.ph c, hc]; rfl) hr') + have R1 : Rel [] { ι with serving := sv + 1, pc := Function.update ι.pc c .idle } + (serCfg s c p ι.mem true ι.gh) := by + refine R.upd hc hcr rfl rfl (fun _ _ => rfl) ((R.act c).trans (by simp [hc, IPc.actv])) + (fun h => absurd rfl h) (fun h => ⟨fun d _ => R.clo h d, by gtriv⟩) ?_ (by gtriv) (by gtriv) + le_rfl (fun _ => trivial) (by gtriv) (by simp [serCfg, hcl0]) (by simp [serCfg, IPc.holds]) ?_ rfl rfl + (fun _ _ _ _ _ _ _ hx => hx) (by gtriv) (fun o => (R.lk o c).trans (by simp [hc, IPc.taken])) + (fun _ _ _ => Iff.rfl) ?_ (fun _ => rfl) (fun _ => rfl) (fun _ => rfl) + · exact fun e _ _ _ he hpe _ => absurd (R.uniq (by rw [hpe]; trivial) hsv0) he + · intro d hd + constructor + · intro h; simp [serCfg] at h + · intro h; exact absurd (Option.some.inj (hho.symm.trans h)) (Ne.symm hd) + · intro d p' m0' h + by_cases hd : d = c + · subst hd; simp [IPc.pend] at h + · simp only [Function.update_of_ne hd] at h + rw [R.pendOnly hsv0 d hd] at h; cases h + have hf := hwRun_fix (serCfg s c p mb true ι.gh) Q + exact ⟨_, Or.inr hst, Or.inr ⟨⟨by rw [hc]; exact rfl, hcl0, by simp [phOf]⟩, hch.base, + R1.of_eq hf.1 hf.2.1 hf.2.2.1 hf.2.2.2.1 hf.2.2.2.2.1 (hwRun_mem hch rfl) hf.2.2.2.2.2⟩⟩ + +/-- **`Rel` at boot**: the runtime of `SerialGate::new` and a model state with the gate open, no holder, every core +idle, no lock taken and the same memory. -/ +theorem rel_init {ι : ICfg A V C L E} {s : Cfg A V C L} (hι : IInit ι) (hg : s.gate = .opn) + (hh : s.holder = none) (hp : ∀ d, s.ph d = .idle) (hl : ∀ o, s.lk o = none) (hm : s.mem = ι.mem) + (hgh : ghOf s = ι.gh) : + Rel [] ι s where + runI d _ := hι.idle d + act d := by rw [hι.act d, hι.idle d]; rfl + clo h := by rw [hι.opn] at h; cases h + drn c p t pend h := by rw [hι.idle c] at h; cases h + gph d := by rw [hι.idle d]; trivial + tlt d t h := by rw [hι.idle d] at h; cases h + tdis d e t h := by rw [hι.idle d] at h; cases h + tsv d t h := by rw [hι.idle d] at h; cases h + gate := hg.trans hι.opn.symm + hold d := by + rw [hh, hι.idle d] + exact ⟨fun h => (by cases h), fun h => False.elim h⟩ + ph d := by rw [hp d, hι.idle d]; rfl + seen c p t pend h := by rw [hι.idle c] at h; cases h + lk o d := by + rw [hl o, hι.idle d] + exact ⟨fun h => (by cases h), fun h => False.elim h⟩ + memP d p m0 h := by rw [hι.idle d] at h; cases h + memE _ := hm + gh _ := hgh + qE _ := rfl + +end + +end Fl4Proofs.P2.GateImpl diff --git a/proofs/Fl4P2Base/GateLock.lean b/proofs/Fl4P2Base/GateLock.lean new file mode 100644 index 000000000..1ad5b1734 --- /dev/null +++ b/proofs/Fl4P2Base/GateLock.lean @@ -0,0 +1,430 @@ +/- +# P2 (sync, gate gen 2): **the lock side of the refinement**, the whole `impl_sim` and `impl_exec` (DESIGN §9 item 4, Half 2) + +`GateImpl.lean` transcribes `fl4-sync`'s gate and `fl4-store`'s lock table (`ICfg`, `IStepG`, `IStepL`) and proves +the gate side of the simulation (`impl_sim_gate`). This module adds the lock attempts (`try_lock_object`, +`crates/fl4-store/src/lock.rs:61–79`) and puts the two together. + +* **`LkInv idx ι`**, the lock table's invariant (runtime only, beside `Rel`): every object a local path took has + its word (`idx o`, the hash `index`, `lock.rs:50–57`) among the path's held words (`IPc.heldL`, `lock::Held`); the + table's word `e` is set by `d` exactly when `e` is among `d`'s held words (so held lists of different cores are + disjoint); held lists have no duplicates (`held.e.contains(e)` is checked before a CAS, `lock.rs:67`); a core + about to CAS (`IPc.cas`) does not hold the word. +* `LkInv.gate`: every `IStepG` step keeps it (`unlock_held`'s releases, `lock.rs:83–86`: `LkInv.unlock`). +* **`impl_sim_lock`**: every `IStepL` step is matched by zero or one `StepR` step keeping `Rel` and `LkInv`: + - `held` (`lock.rs:67–69`) on an object the path already took: a stutter (`dedup`); on another object (a hash + collision with the path's own word): `lock o true` (the model lock is free: `lk_none`); + - `full` (`lock.rs:70`), `busy` (`lock.rs:71`), `casFail` (`lock.rs:72–74`): `lock o false`; + - `free` (`lock.rs:71`): a stutter into `IPc.cas`; + - `casOk` (`lock.rs:72`, `76–78`): `lock o true`. +* **`impl_sim`** (`IStep` = `IStepG ∨ IStepL` by a running core) and **`impl_exec`**: from a runtime at boot + (`IInit`, the lock table clear, `lock.rs:44`) and a model state related by `rel_init`'s hypotheses, every runtime + execution (`IReach`) is matched by a model execution of weak steps (`ReachR`, `StepR` steps) ending in a state + related by `Rel`, the lock table satisfying `LkInv`. + +## Named premises + +The four [P]s of `GateImpl.lean` (MM-SB, MM-DRF, MM-RACY, IPI), as justifications of the transcription's step +granularity; no new one, no [J], no [W]. The lock CAS (`lock.rs:72`) is one atomic step (it is an `AtomicU64` +`compare_exchange`); the relaxed load before it (`lock.rs:71`) is its own step (`busy` / `free`), so the CAS may +fail after a load that found the word clear (`casFail`). + +## What `impl_exec` does not give, and where it is closed + +`ReachR` has no `Allowed` side condition: it is not `GReachR`. Gen 2's stop-first finding (a body's step after a +stale racy read need not be `Allowed` under `Safe`) is closed in gate gen 3 by weak safety (`SafeW.lean`) and the +connection to the AArch64 chain (`Fl4ProofsA64/P2/GateExecA.lean`: `impl_execPA`, `serializable_implPA`), for which +this module adds the pairing facts `holds_phOf`, `isLoc_of_phOf`, `pend_of_phOf_idle`, `IStep.pc_other` and +`stepR_ser_idle` (`IDec`, a release's decisive store on the runtime alone, moved to `GateImpl.lean` in gate gen 4). + +Gate gen 4: `impl_sim` carries `GateImpl`'s deferred hardware writes (`Rel Q`; at the decisive store the replay +disjunct); `impl_exec` concludes `Rel []`. New: `LkInv.hw`, `IBody` (the serial body's step, on the runtime alone), +`IStep.pend_cls` (a step is the body, the decisive store, or neither), `IStep.memgh`, `stepR_ser_lk`, `serStep_dec`. +-/ +import Fl4P2Base.GateImpl + +namespace Fl4Proofs.P2.GateImpl + +open Fl4Proofs.P2.Serial + +noncomputable section + +variable {A V C L E : Type} [DecidableEq A] [DecidableEq C] [DecidableEq L] [DecidableEq E] + +namespace IPc + +/-- The lock words the core holds (`lock::Held`, most recent first). -/ +def heldL : IPc A V C L E → List E + | .loc _ _ _ _ hs | .cas _ _ _ _ _ hs | .fin hs => hs + | .idle | .la _ | .lb _ | .lc | .tk .. | .cl .. | .st1 .. | .fen .. | .scn .. | .bd .. | .rl .. | .rn .. + | .rc .. | .rs .. => [] + +end IPc + +open IPc + +/-- **The lock table's invariant** (see the module docstring). -/ +structure LkInv (idx : L → E) (ι : ICfg A V C L E) : Prop where + tk : ∀ d o, o ∈ (ι.pc d).taken → idx o ∈ (ι.pc d).heldL + tbl : ∀ e d, ι.tbl e = some d ↔ e ∈ (ι.pc d).heldL + nd : ∀ d, ((ι.pc d).heldL).Nodup + cas : ∀ d (q0 : Prog A V L) o k T W hs, ι.pc d = .cas q0 o k T W hs → idx o ∉ hs + +/-- `dedup` of an attempt on an object already taken: no attempt. -/ +theorem dedup_lock_mem {T : Set L} {o : L} {k : Bool → Prog A V L} (h : o ∈ T) : + dedup T (.lock o k) = dedup T (k true) := by + rw [dedup, if_pos h] + +/-- `dedup` of an attempt on an object not yet taken: the attempt. -/ +theorem dedup_lock_nmem {T : Set L} {o : L} {k : Bool → Prog A V L} (h : o ∉ T) : + dedup T (.lock o k) = .lock o fun b => dedup (if b = true then insert o T else T) (k b) := by + rw [dedup, if_neg h] + +section lk + +variable {idx : L → E} {ι ι' : ICfg A V C L E} {s : Cfg A V C L} {c : C} {X Y : IPc A V C L E} {Q : HwQ A V L} + +/-- **Frame**: a step of core `c` from `X` to `Y` that keeps the table and the held words keeps `LkInv`, given +that what `Y` took is what `X` took or held. -/ +theorem LkInv.frame (I : LkInv idx ι) (hc : ι.pc c = X) (hpc : ι'.pc = Function.update ι.pc c Y) + (htb : ι'.tbl = ι.tbl) (hh : Y.heldL = X.heldL) + (ht : ∀ o, o ∈ Y.taken → o ∈ X.taken ∨ idx o ∈ Y.heldL) + (hcas : ∀ (q0 : Prog A V L) o k T W hs, Y = .cas q0 o k T W hs → idx o ∉ hs) : LkInv idx ι' := by + have hne : ∀ d, d ≠ c → ι'.pc d = ι.pc d := fun d hd => by rw [hpc, Function.update_of_ne hd] + have hcY : ι'.pc c = Y := by rw [hpc, Function.update_self] + refine ⟨fun d o h => ?_, fun e d => ?_, fun d => ?_, fun d q0 o k T W hs h => ?_⟩ + · by_cases hd : d = c + · rw [hd, hcY] at h ⊢ + rcases ht o h with h1 | h1 + · have := I.tk c o; rw [hc] at this; rw [hh]; exact this h1 + · exact h1 + · rw [hne d hd] at h ⊢; exact I.tk d o h + · rw [htb] + by_cases hd : d = c + · have := I.tbl e c; rw [hc] at this; rw [hd, hcY, hh]; exact this + · rw [hne d hd]; exact I.tbl e d + · by_cases hd : d = c + · have := I.nd c; rw [hc] at this; rw [hd, hcY, hh]; exact this + · rw [hne d hd]; exact I.nd d + · by_cases hd : d = c + · rw [hd, hcY] at h; exact hcas _ _ _ _ _ _ h + · rw [hne d hd] at h; exact I.cas d _ _ _ _ _ _ h + +/-- `unlock_held` releasing its most recent word `e` (`lock.rs:84–85`) keeps `LkInv`. -/ +theorem LkInv.unlock (I : LkInv idx ι) {e : E} {r : List E} (hc : ι.pc c = X) (hX : X.heldL = e :: r) + (hpc : ι'.pc = Function.update ι.pc c (.fin r)) (htb : ι'.tbl = Function.update ι.tbl e none) : + LkInv idx ι' := by + have hne : ∀ d, d ≠ c → ι'.pc d = ι.pc d := fun d hd => by rw [hpc, Function.update_of_ne hd] + have hcY : ι'.pc c = .fin r := by rw [hpc, Function.update_self] + have hXc : (ι.pc c).heldL = e :: r := by rw [hc]; exact hX + have hnd := I.nd c + rw [hXc] at hnd + have her : e ∉ r := (List.nodup_cons.mp hnd).1 + have hec : ι.tbl e = some c := (I.tbl e c).mpr (by rw [hXc]; simp) + refine ⟨fun d o h => ?_, fun e' d => ?_, fun d => ?_, fun d q0 o k T W hs h => ?_⟩ + · by_cases hd : d = c + · rw [hd, hcY] at h; simp [IPc.taken] at h + · rw [hne d hd] at h ⊢; exact I.tk d o h + · rw [htb] + by_cases he : e' = e + · rw [he, Function.update_self] + by_cases hd : d = c + · rw [hd, hcY]; simp [IPc.heldL, her] + · rw [hne d hd] + exact ⟨fun h => (by cases h), + fun h => absurd (Option.some.inj (((I.tbl e d).mpr h).symm.trans hec)) hd⟩ + · rw [Function.update_of_ne he] + by_cases hd : d = c + · rw [hd, hcY, I.tbl e' c, hXc]; simp [IPc.heldL, he] + · rw [hne d hd]; exact I.tbl e' d + · by_cases hd : d = c + · rw [hd, hcY]; exact (List.nodup_cons.mp hnd).2 + · rw [hne d hd]; exact I.nd d + · by_cases hd : d = c + · rw [hd, hcY] at h; cases h + · rw [hne d hd] at h; exact I.cas d q0 o k T W hs h + +/-- A successful CAS on `idx o` (`lock.rs:72`, `76–78`) keeps `LkInv`. -/ +theorem LkInv.acq (I : LkInv idx ι) {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} + {hs : List E} (hc : ι.pc c = .cas q0 o k T W hs) (htb : ι.tbl (idx o) = none) : + LkInv idx { ι with tbl := Function.update ι.tbl (idx o) (some c), + pc := Function.update ι.pc c (.loc q0 (k true) (insert o T) W (idx o :: hs)) } := by + have hne : ∀ d, d ≠ c → + Function.update ι.pc c (IPc.loc q0 (k true) (insert o T) W (idx o :: hs)) d = ι.pc d := + fun d hd => Function.update_of_ne hd _ _ + have hcY : Function.update ι.pc c (IPc.loc q0 (k true) (insert o T) W (idx o :: hs)) c = + .loc q0 (k true) (insert o T) W (idx o :: hs) := Function.update_self _ _ _ + have hn : idx o ∉ hs := I.cas c _ _ _ _ _ _ hc + have hhs : (ι.pc c).heldL = hs := by rw [hc]; rfl + refine ⟨fun d o' h => ?_, fun e d => ?_, fun d => ?_, fun d q0' o' k' T' W' hs' h => ?_⟩ + · dsimp only at h ⊢ + by_cases hd : d = c + · rw [hd, hcY] at h ⊢ + rcases Set.mem_insert_iff.mp h with h1 | h1 + · rw [h1]; exact List.mem_cons_self .. + · have := I.tk c o'; rw [hc] at this; exact List.mem_cons_of_mem _ (this h1) + · rw [hne d hd] at h ⊢; exact I.tk d o' h + · dsimp only + by_cases he : e = idx o + · rw [he, Function.update_self] + by_cases hd : d = c + · rw [hd, hcY]; simp [IPc.heldL] + · rw [hne d hd] + exact ⟨fun h => absurd (Option.some.inj h).symm hd, + fun h => by rw [(I.tbl _ d).mpr h] at htb; cases htb⟩ + · rw [Function.update_of_ne he] + by_cases hd : d = c + · rw [hd, hcY, I.tbl e c, hhs]; simp [IPc.heldL, he] + · rw [hne d hd]; exact I.tbl e d + · dsimp only + by_cases hd : d = c + · rw [hd, hcY]; exact List.nodup_cons.mpr ⟨hn, by have := I.nd c; rw [hhs] at this; exact this⟩ + · rw [hne d hd]; exact I.nd d + · dsimp only at h + by_cases hd : d = c + · rw [hd, hcY] at h; cases h + · rw [hne d hd] at h; exact I.cas d _ _ _ _ _ _ h + +/-- **Every gate-protocol step keeps `LkInv`.** -/ +theorem LkInv.gate {U : A → Prop} (I : LkInv idx ι) (h : IStepG U ι c ι') : LkInv idx ι' := by + cases h <;> first + | exact I + | exact I.unlock ‹ι.pc c = _› rfl rfl rfl + | exact I.frame ‹ι.pc c = _› rfl rfl (by exact rfl) + (fun o h => by first | exact Or.inl h | (simp [IPc.taken] at h)) + (fun _ _ _ _ _ _ h => by cases h) + +/-- No core holds `o` in the model when no core whose word `idx o` is set took `o`. -/ +theorem lk_none (R : Rel Q ι s) (I : LkInv idx ι) {o : L} + (h : ∀ d, ι.tbl (idx o) = some d → o ∉ (ι.pc d).taken) : s.lk o = none := by + cases hl : s.lk o with + | none => rfl + | some d => + have ho := (R.lk o d).mp hl + exact absurd ho (h d ((I.tbl _ d).mpr (I.tk d o ho))) + +/-- The model's phase at a lock attempt on an object not yet taken. -/ +theorem ph_lock (R : Rel Q ι s) {q0 : Prog A V L} {o : L} {k : Bool → Prog A V L} {T : Set L} {W : Set A} + (hX : phOf ι.closed (ι.pc c) = .loc (dedup ∅ q0) (dedup T (.lock o k)) W) (hoT : o ∉ T) : + s.ph c = .loc (dedup ∅ q0) (.lock o fun b => dedup (if b = true then insert o T else T) (k b)) W := by + rw [R.ph c, hX, dedup_lock_nmem hoT] + +/-- **A lock attempt matched by the model's `lock b`**, keeping `Rel`. -/ +theorem Rel.lockM {U : A → Prop} {p0 : Prog A V L} {o : L} {k' : Bool → Prog A V L} {W : Set A} (b : Bool) + (R : Rel Q ι s) (hc : ι.pc c = X) (hcr : c ∈ ι.run) (hX : X.isLoc) (hY : Y.isLoc) + (hph : s.ph c = .loc p0 (.lock o k') W) (hb : b = true → s.lk o = none) + (hpc : ι'.pc = Function.update ι.pc c Y) (hrn : ι'.run = ι.run) (hact : ι'.active = ι.active) + (hG : ι'.closed = ι.closed) (hnx : ι'.next = ι.next) (hsv : ι'.serving = ι.serving) + (hmem : ι'.mem = ι.mem) (hgh : ι'.gh = ι.gh) (hZ : Phase.loc p0 (k' b) W = phOf ι.closed Y) + (hL : ∀ o', o' ∈ Y.taken ↔ (b = true ∧ o' = o) ∨ o' ∈ X.taken) (hI : LkInv idx ι') : + ∃ s', (s' = s ∨ StepR U s c s') ∧ Rel Q ι' s' ∧ LkInv idx ι' := by + refine ⟨_, Or.inr (.step (Step.lock b hph hb)), R.lkStep hc hcr hX hY hpc hrn hact hG hnx hsv rfl rfl rfl hZ rfl + (fun o' => ?_) (fun o' d hd => ?_) ?_ hgh rfl, hI⟩ + · rw [hL o', ← hc, ← R.lk o' c] + cases b + · simp + · by_cases ho : o' = o + · subst ho; simp + · simp [ho, Function.update_of_ne ho] + · cases b + · simp + · by_cases ho : o' = o + · subst ho + rw [if_pos rfl] + dsimp only + rw [Function.update_self, hb rfl] + exact ⟨fun h => absurd (Option.some.inj h) (Ne.symm hd), fun h => by cases h⟩ + · simp [ho, Function.update_of_ne ho] + · show s.mem = ι'.mem + rw [hmem]; exact R.memE (R.nopend (by rw [hc]; exact hX)) + +/-- Closes a side condition about fixed phases. -/ +local macro "ltriv" : tactic => `(tactic| first + | rfl + | exact Iff.rfl + | (intro _ _ _ h; cases h; done) + | (intro _ h; cases h; done) + | (simp [IPc.isLoc, IPc.gOK, IPc.tick, IPc.atSv, IPc.holds, IPc.taken, IPc.pend, IPc.actv, IPc.heldL, + phOf]; done)) + +/-- **Every lock attempt of the runtime is matched by zero or one model step** (`try_lock_object`, +`lock.rs:61–79`), keeping `Rel` and `LkInv`. -/ +theorem impl_sim_lock {maxHeld : ℕ} {U : A → Prop} (R : Rel Q ι s) (I : LkInv idx ι) (hcr : c ∈ ι.run) + (h : IStepL idx maxHeld ι c ι') : ∃ s', (s' = s ∨ StepR U s c s') ∧ Rel Q ι' s' ∧ LkInv idx ι' := by + cases h with + | @held q0 o k T W hs hc hi => + have I' : LkInv idx { ι with pc := Function.update ι.pc c (.loc q0 (k true) (insert o T) W hs) } := + I.frame (Y := .loc q0 (k true) (insert o T) W hs) hc rfl rfl rfl + (fun o' h => (Set.mem_insert_iff.mp h).elim (fun e => Or.inr (by rw [e]; exact hi)) Or.inl) + (fun _ _ _ _ _ _ h => by cases h) + by_cases hoT : o ∈ T + · refine ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (fun _ => trivial) + (by ltriv) (fun _ => trivial) (by ltriv) Iff.rfl ?_ (Set.insert_eq_of_mem hoT) rfl, I'⟩ + show Phase.loc _ (dedup (insert o T) (k true)) W = Phase.loc _ (dedup T (.lock o k)) W + rw [dedup_lock_mem hoT, Set.insert_eq_of_mem hoT] + · have hfree : s.lk o = none := lk_none R I fun d hd hod => by + have hc' := (I.tbl _ c).mpr (by rw [hc]; exact hi) + rw [hd] at hc' + rw [Option.some.inj hc', hc] at hod + exact hoT hod + exact R.lockM true hc hcr (by trivial) (by trivial) + (ph_lock (q0 := q0) (k := k) (W := W) R (by rw [hc]; rfl) hoT) (fun _ => hfree) + rfl rfl rfl rfl rfl rfl rfl rfl rfl (fun o' => by simp [IPc.taken]) I' + | @full q0 o k T W hs hc hn _ => + have hoT : o ∉ T := fun h => hn (by have := I.tk c o; rw [hc] at this; exact this h) + exact R.lockM false hc hcr (by trivial) (by trivial) + (ph_lock (q0 := q0) (k := k) (W := W) R (by rw [hc]; rfl) hoT) (fun h => by cases h) + rfl rfl rfl rfl rfl rfl rfl rfl rfl (fun o' => by simp [IPc.taken]) + (I.frame (Y := .loc q0 (k false) T W hs) hc rfl rfl rfl (fun _ h => Or.inl h) + (fun _ _ _ _ _ _ h => by cases h)) + | @busy q0 o k T W hs hc hn _ _ => + have hoT : o ∉ T := fun h => hn (by have := I.tk c o; rw [hc] at this; exact this h) + exact R.lockM false hc hcr (by trivial) (by trivial) + (ph_lock (q0 := q0) (k := k) (W := W) R (by rw [hc]; rfl) hoT) (fun h => by cases h) + rfl rfl rfl rfl rfl rfl rfl rfl rfl (fun o' => by simp [IPc.taken]) + (I.frame (Y := .loc q0 (k false) T W hs) hc rfl rfl rfl (fun _ h => Or.inl h) + (fun _ _ _ _ _ _ h => by cases h)) + | @free q0 o k T W hs hc hn _ _ => + exact ⟨s, Or.inl rfl, R.stut hc hcr rfl rfl (Or.inl ⟨rfl, rfl⟩) rfl rfl rfl rfl rfl (fun _ => trivial) + (by ltriv) (fun _ => trivial) (by ltriv) Iff.rfl rfl rfl rfl, + I.frame (Y := .cas q0 o k T W hs) hc rfl rfl rfl (fun _ h => Or.inl h) + (fun _ _ _ _ _ _ h => by cases h; exact hn)⟩ + | @casOk q0 o k T W hs hc htb => + have hn : idx o ∉ hs := I.cas c _ _ _ _ _ _ hc + have hoT : o ∉ T := fun h => hn (by have := I.tk c o; rw [hc] at this; exact this h) + have hfree : s.lk o = none := lk_none R I fun d hd => by rw [htb] at hd; cases hd + exact R.lockM true hc hcr (by trivial) (by trivial) + (ph_lock (q0 := q0) (k := k) (W := W) R (by rw [hc]; rfl) hoT) (fun _ => hfree) + rfl rfl rfl rfl rfl rfl rfl rfl rfl (fun o' => by simp [IPc.taken]) (I.acq hc htb) + | @casFail q0 o k T W hs hc _ => + have hn : idx o ∉ hs := I.cas c _ _ _ _ _ _ hc + have hoT : o ∉ T := fun h => hn (by have := I.tk c o; rw [hc] at this; exact this h) + exact R.lockM false hc hcr (by trivial) (by trivial) + (ph_lock (q0 := q0) (k := k) (W := W) R (by rw [hc]; rfl) hoT) (fun h => by cases h) + rfl rfl rfl rfl rfl rfl rfl rfl rfl (fun o' => by simp [IPc.taken]) + (I.frame (Y := .loc q0 (k false) T W hs) hc rfl rfl rfl (fun _ h => Or.inl h) + (fun _ _ _ _ _ _ h => by cases h)) + +/-- **The simulation**: every step of the runtime (`IStep`: a gate-protocol step or a lock attempt, by a core the +kernel runs) is matched by zero or one model step (`StepR`), keeping `Rel` and `LkInv`; at a release's decisive +store the model's `serial` is followed by the deferred hardware writes `Q` (gate gen 4). -/ +theorem impl_sim {maxHeld : ℕ} {U : A → Prop} (R : Rel Q ι s) (I : LkInv idx ι) (h : IStep U idx maxHeld ι c ι') : + ∃ s1, (s1 = s ∨ StepR U s c s1) ∧ + (Rel Q ι' s1 ∨ (IDec ι c ι' ∧ s1.mem = hqBase Q ι.mem ∧ Rel [] ι' (hwRun s1 Q))) ∧ LkInv idx ι' := + h.2.elim (fun hg => let ⟨s1, h1, h2⟩ := impl_sim_gate R h.1 hg; ⟨s1, h1, h2, I.gate hg⟩) + (fun hl => let ⟨s1, h1, h2, h3⟩ := impl_sim_lock R I h.1 hl; ⟨s1, h1, Or.inl h2, h3⟩) + +/-- A hardware write keeps `LkInv` (it changes only the memory). -/ +theorem LkInv.hw (I : LkInv idx ι) (m : A → V) : LkInv idx { ι with mem := m } := + ⟨I.tk, I.tbl, I.nd, I.cas⟩ + +end lk + +/-! ## Executions -/ + +/-- Executions of the runtime (the untracked words `U` of each step its own). -/ +inductive IReach (idx : L → E) (maxHeld : ℕ) (ι0 : ICfg A V C L E) : ICfg A V C L E → Prop + | refl : IReach idx maxHeld ι0 ι0 + | step {ι ι' : ICfg A V C L E} {c : C} {U : A → Prop} : + IReach idx maxHeld ι0 ι → IStep U idx maxHeld ι c ι' → IReach idx maxHeld ι0 ι' + +/-- Executions of the model's weak steps (no `Allowed` side condition). -/ +inductive ReachR (s0 : Cfg A V C L) : Cfg A V C L → Prop + | refl : ReachR s0 s0 + | step {s s' : Cfg A V C L} {c : C} {U : A → Prop} : ReachR s0 s → StepR U s c s' → ReachR s0 s' + +/-- `LkInv` at boot: every core idle, the lock table clear (`TABLE`, `lock.rs:44`). -/ +theorem lkInv_init {idx : L → E} {ι : ICfg A V C L E} (hι : IInit ι) (htb : ∀ e, ι.tbl e = none) : + LkInv idx ι where + tk d o h := by rw [hι.idle d] at h; simp [IPc.taken] at h + tbl e d := by rw [htb e, hι.idle d]; simp [IPc.heldL] + nd d := by rw [hι.idle d]; exact List.nodup_nil + cas d _ _ _ _ _ _ h := by rw [hι.idle d] at h; cases h + +/-- **`impl_exec`**: from the runtime at boot (`IInit`, the lock table clear) and a model state with the gate open, +no holder, every core idle, no lock taken and the same memory, every execution of the runtime is matched by an +execution of the model's weak steps, ending in states related by `Rel`, the lock table satisfying `LkInv`. -/ +theorem impl_exec {idx : L → E} {maxHeld : ℕ} {ι0 ι : ICfg A V C L E} {s0 : Cfg A V C L} + (hι : IInit ι0) (htb : ∀ e, ι0.tbl e = none) (hg : s0.gate = .opn) (hh : s0.holder = none) + (hp : ∀ d, s0.ph d = .idle) (hl : ∀ o, s0.lk o = none) (hm : s0.mem = ι0.mem) (hgh : ghOf s0 = ι0.gh) + (h : IReach idx maxHeld ι0 ι) : ∃ s, ReachR s0 s ∧ Rel [] ι s ∧ LkInv idx ι := by + induction h with + | refl => exact ⟨s0, .refl, rel_init hι hg hh hp hl hm hgh, lkInv_init hι htb⟩ + | step _ hst ih => + obtain ⟨s, hr, R, I⟩ := ih + obtain ⟨s', h1, hR, I'⟩ := impl_sim R I hst + have R' : Rel [] _ s' := hR.elim id fun h => h.2.2 + rcases h1 with h1 | h1 + · subst h1; exact ⟨s', hr, R', I'⟩ + · exact ⟨s', .step hr h1, R', I'⟩ + +/-! ## Facts for pairing runtime and model steps (gate gen 3) -/ + +theorem holds_phOf {X : IPc A V C L E} {g : Gate} (h : X.holds g) (hg : g = .closed) : + ∃ p, phOf g X = .ser p := by + subst hg; cases X <;> simp_all [IPc.holds, phOf] + +theorem isLoc_of_phOf {X : IPc A V C L E} {g : Gate} (h : (phOf g X).IsLoc) : X.isLoc := by + cases X <;> simp only [phOf] at h <;> (try split at h) <;> simp_all [Phase.IsLoc, IPc.isLoc] + +theorem pend_of_phOf_idle {X : IPc A V C L E} {g : Gate} (h : phOf g X = .idle) : X.pend = none := by + cases X <;> simp only [phOf] at h <;> (try split at h) <;> simp_all [IPc.pend] + +/-- A runtime step of `c` leaves the other cores' phases. -/ +theorem IStep.pc_other {U : A → Prop} {idx : L → E} {maxHeld : ℕ} {ι ι' : ICfg A V C L E} {c d : C} + (h : IStep U idx maxHeld ι c ι') (hd : d ≠ c) : ι'.pc d = ι.pc d := by + rcases h with ⟨-, h | h⟩ <;> cases h <;> first | rfl | exact Function.update_of_ne hd _ _ + +/-- A model step of the holder of the closed gate is its serial body: it ends idle. -/ +theorem stepR_ser_idle {U : A → Prop} {s s' : Cfg A V C L} {c : C} {p : Prog A V L} (hp : s.ph c = .ser p) + (hg : s.gate = .closed) (h : StepR U s c s') : s'.ph c = .idle := by + cases h with + | rdsW _ hc => rw [hp] at hc; cases hc + | rdsC _ hc => rw [hp] at hc; cases hc + | step h => cases h <;> simp_all + +/-! ## Pending bodies and hardware writes (gate gen 4) -/ + +/-- **The serial body's step** (`IStepG.body`), on the runtime alone: the core's body becomes pending release. It is +where a runtime execution switches to the layout the body leaves (`GateExecA.IFl4StepPA.ser`). -/ +def IBody (ι : ICfg A V C L E) (c : C) (ι' : ICfg A V C L E) : Prop := + (ι.pc c).pend = none ∧ (ι'.pc c).pend ≠ none + +/-- A step of the runtime makes its core's body pending (the body), releases it (the decisive store), or neither. -/ +theorem IStep.pend_cls {U : A → Prop} {idx : L → E} {maxHeld : ℕ} {ι ι' : ICfg A V C L E} {c : C} + (h : IStep U idx maxHeld ι c ι') (hg : (ι.pc c).gOK ι.closed) : + IBody ι c ι' ∨ (IDec ι c ι' ∧ (ι.pc c).pend ≠ none ∧ (ι'.pc c).pend = none) ∨ + (¬ IBody ι c ι' ∧ ¬ IDec ι c ι' ∧ ((ι'.pc c).pend = none ↔ (ι.pc c).pend = none)) := by + rcases h with ⟨-, h | h⟩ + · cases h with + | @serve p m0 sv dn hc => + cases dn <;> simp_all [IBody, IDec, IPc.pend, IPc.holds, IPc.gOK, phOf] + | _ => simp_all [IBody, IDec, IPc.pend, IPc.holds, IPc.gOK, phOf] + · cases h <;> simp_all [IBody, IDec, IPc.pend, IPc.holds, phOf] + +/-- A step of the runtime leaves the memory and the ghost maps, unless it is a local path's or the serial body. -/ +theorem IStep.memgh {U : A → Prop} {idx : L → E} {maxHeld : ℕ} {ι ι' : ICfg A V C L E} {c : C} + (h : IStep U idx maxHeld ι c ι') : (ι'.mem = ι.mem ∧ ι'.gh = ι.gh) ∨ (ι.pc c).isLoc ∨ IBody ι c ι' := by + rcases h with ⟨-, h | h⟩ <;> cases h <;> simp_all [IBody, IPc.isLoc, IPc.pend] + +/-- A model step of the holder of the closed gate (its serial body) keeps the locks. -/ +theorem stepR_ser_lk {U : A → Prop} {s s' : Cfg A V C L} {c : C} {p : Prog A V L} (hp : s.ph c = .ser p) + (h : StepR U s c s') : s'.lk = s.lk := by + cases h with + | rdsW => rfl + | rdsC => rfl + | step h => cases h <;> simp_all + +/-- A model step of the holder of the closed gate matched to a runtime step is the release's decisive store. -/ +theorem serStep_dec {U : A → Prop} {Q Q' : HwQ A V L} {ι ι' : ICfg A V C L E} {s s1 : Cfg A V C L} {c : C} + (R : Rel Q ι s) (R1 : Rel Q' ι' s1) (h : StepR U s c s1) (hg : s.gate = .closed) (hh : s.holder = some c) : + IDec ι c ι' := by + have hho := (R.hold c).mp hh + have hcl := R.gate.symm.trans hg + obtain ⟨p, hp⟩ := holds_phOf hho hcl + rw [← R.ph c] at hp + exact ⟨hho, hcl, by rw [← R1.ph c]; exact stepR_ser_idle hp hg h⟩ + +end + +end Fl4Proofs.P2.GateImpl diff --git a/proofs/Fl4P2Base/SafeW.lean b/proofs/Fl4P2Base/SafeW.lean new file mode 100644 index 000000000..45f313076 --- /dev/null +++ b/proofs/Fl4P2Base/SafeW.lean @@ -0,0 +1,455 @@ +/- +# P2 (sync, gate gen 3): **weak safety** `SafeW`, for executions whose racy reads may return stale values (DESIGN §9 item 4, Half 2) + +`SerialSafe.Safe` lets a speculative read (`Prog.rds`) continue only with the value of a memory the other cores can +reach from the current one (`FReach`). The runtime's racy reads may return an older value of the word's coherence +order (**[P] MM-RACY**, `SerialW.StepR.rdsW`), so `Safe` does not cover them (gate gen 2's stop-first finding). The +Architect's ruling of 19:10Z, option (a): a weaker safety whose speculative-read continuation holds for every value +such a read may return, re-derived from the same per-path certificates. + +* `U : A → Prop`: the untracked words (FL4: the layout's user memory, `· ∈ K.user`, which user threads write beside + the kernel; `fun _ => False` if none). Under `OwnMap.own` / `ownP` a user word is the gate's, so `PO` holds of it: + being readable does not make its value the current one. +* **`RdW own cls ko U c hs μ a v`**: the values a speculative read of `a` from `μ` may return: the current one, or, + on a racy word (`¬ PO c hs (own μ a) ∨ U a`), any value of the current value's class if the gate or `c` owns the + word's class, and any value otherwise (gate gen 4, `StepR.rdsC`: the class clause is exactly what `DInvW.pinR`'s + `pinCls` asks). +* **`SafeW`**: `Safe` with `rds` continuing at every `RdW` value. `SafeW.foreign`: stable under another core's + `Foreign` write, as `Safe.foreign`. +* **`safe_of_runsW`**: `SafeCert.safe_of_runs` for `SafeW`, from certificates for every memory **`AdmW`**-admissible + (equal to the real one only on readable *tracked* words, `PO ∧ ¬ U`): a racy read pins its word at the value it + returned, whatever that is (`DInvW.pinR`). Needs the certificate's `rd` and written words tracked (`hRU`, `hHU`; + for FL4 they are non-user words). +* **`SInvW U s`** (every local path in its glue or `SafeW` from the current memory and locks), `allowed_of_sinvW` + (it gives `Allowed`), **`sinv_stepRW`** (kept by a `StepR U` step, given a started body `SafeW` and no local path + under a closed gate), `sinvW_hw` (kept by a hardware write), `sinvW_noLoc`. +* `XGReachR` (`GReachR` with hardware writes), `xgreachR_inv`, `no_overlapXR`, **`serializable_quiescentXR`**. + +No premises: everything is a hypothesis of the theorems. The [P]s MM-DRF / MM-RACY justify `StepR` (`SerialW.lean`) +and the runtime's `IStepG.rds` / `rdsR` / `rdsC` (`GateImpl.lean`); no [J], no [W]. Gate gen 4 widened `RdW` (a +`cls` argument and the class-preserving values on gate- or `c`-class words), so `SafeW` asks a speculative read's +continuation at more values; `safe_of_runsW` gives it unchanged (its `pinR` already asked the class condition). +-/ +import Fl4P2Base.SerialW +import Fl4P2Base.SafeCert +import Fl4P2Base.SerialHw + +namespace Fl4Proofs.P2.Serial + +noncomputable section + +variable {A V C L : Type} [DecidableEq A] [DecidableEq C] [DecidableEq L] + +/-- The values a speculative read of `a` from `μ` may return on core `c` (locks `hs`): the current value, or, on a +racy word (not readable, or untracked), any value of the current value's class where the gate or `c` owns the class +(gate gen 4), any value elsewhere. -/ +def RdW (own : (A → V) → A → Owner C L) (cls : A → V → V) (ko : A → ClsOwner C) (U : A → Prop) (c : C) + (hs : L → Prop) (μ : A → V) (a : A) (v : V) : Prop := + v = μ a ∨ ((¬ PO c hs (own μ a) ∨ U a) ∧ ((ko a = .gate ∨ ko a = .core c) → cls a v = cls a (μ a))) + +/-- **A body weakly safe from core `c`'s view** (`Safe` with `rds` continuing at every `RdW` value). -/ +inductive SafeW (own : (A → V) → A → Owner C L) (cls : A → V → V) (ko : A → ClsOwner C) (U : A → Prop) + (c : C) (hs : L → Prop) : (A → V) → Prog A V L → Prop + | done {μ : A → V} : SafeW own cls ko U c hs μ .done + | rd {μ : A → V} {a : A} {k : V → Prog A V L} : + (∀ μ', FReach own cls ko c hs μ μ' → PO c hs (own μ' a)) → + (∀ μ', FReach own cls ko c hs μ μ' → SafeW own cls ko U c hs μ' (k (μ' a))) → + SafeW own cls ko U c hs μ (.rd a k) + | rds {μ : A → V} {a : A} {k : V → Prog A V L} : + (∀ μ' v, FReach own cls ko c hs μ μ' → RdW own cls ko U c hs μ' a v → SafeW own cls ko U c hs μ' (k v)) → + SafeW own cls ko U c hs μ (.rds a k) + | rdc {μ : A → V} {a : A} {K : V → V → Prop} {k : V → Prog A V L} : + (ko a = .gate ∨ ko a = .core c) → (∀ u w, cls a u = cls a w → K u w) → + (∀ μ', FReach own cls ko c hs μ μ' → SafeW own cls ko U c hs μ' (k (μ' a))) → + SafeW own cls ko U c hs μ (.rdc a K k) + | wr {μ : A → V} {a : A} {v : V} {k : Prog A V L} : + (∀ μ', FReach own cls ko c hs μ μ' → HeldV c hs (own μ' a)) → + (∀ μ', FReach own cls ko c hs μ μ' → MoveV own c hs μ' (Function.update μ' a v)) → + (∀ μ', FReach own cls ko c hs μ μ' → + cls a v = cls a (μ' a) ∨ ko a = .core c ∨ ko a = .free) → + (∀ μ', FReach own cls ko c hs μ μ' → SafeW own cls ko U c hs (Function.update μ' a v) k) → + SafeW own cls ko U c hs μ (.wr a v k) + +variable {own : (A → V) → A → Owner C L} {cls : A → V → V} {ko : A → ClsOwner C} {U : A → Prop} {c : C} + {hs : L → Prop} + +/-- **Stability**: `SafeW` is kept by another core's `Foreign` write. -/ +theorem SafeW.foreign {μ : A → V} {q : Prog A V L} (hS : SafeW own cls ko U c hs μ q) {b : A} {v : V} + (hF : Foreign own cls ko c hs μ b v) : SafeW own cls ko U c hs (Function.update μ b v) q := by + cases hS with + | done => exact .done + | rd h1 h2 => exact .rd (fun μ' hR => h1 μ' (.step hF hR)) (fun μ' hR => h2 μ' (.step hF hR)) + | rds h => exact .rds fun μ' w hR => h μ' w (.step hF hR) + | rdc hko hK h => exact .rdc hko hK fun μ' hR => h μ' (.step hF hR) + | wr h1 h2 h3 h4 => + exact .wr (fun μ' hR => h1 μ' (.step hF hR)) (fun μ' hR => h2 μ' (.step hF hR)) + (fun μ' hR => h3 μ' (.step hF hR)) (fun μ' hR => h4 μ' (.step hF hR)) + +/-! ## From certificates -/ + +/-- `ν` is admissible for the real memory `μ` with pins `π`, weakly: equal to `μ` on the readable *tracked* words, +to the pins, and of `μ`'s class on class-tracked words. -/ +def AdmW (own : (A → V) → A → Owner C L) (cls : A → V → V) (ko : A → ClsOwner C) (U : A → Prop) (c : C) + (hs : L → Prop) (μ : A → V) (π : A → Option V) (ν : A → V) : Prop := + (∀ y, PO c hs (own μ y) → ¬ U y → ν y = μ y) ∧ (∀ y u, π y = some u → ν y = u) ∧ + (∀ y, (ko y = .gate ∨ ko y = .core c) → cls y (ν y) = cls y (μ y)) + +/-- `SafeCert.DInv` with `AdmW` (pins of readable words bind only tracked ones). -/ +structure DInvW (own : (A → V) → A → Owner C L) (cls : A → V → V) (ko : A → ClsOwner C) (U : A → Prop) (c : C) + (hs : L → Prop) (Rp Hp : A → Prop) (P : Set A) (q : Prog A V L) (μ : A → V) (π : A → Option V) : Prop where + run : ∀ ν, AdmW own cls ko U c hs μ π ν → + ∃ κ, Diff own c hs μ κ ∧ AltRun own cls ko c hs Rp Hp P q ν κ + pin : ∀ y u, π y = some u → y ∈ P + pinPO : ∀ y u, π y = some u → PO c hs (own μ y) → ¬ U y → μ y = u + pinCls : ∀ y u, π y = some u → (ko y = .gate ∨ ko y = .core c) → cls y u = cls y (μ y) + rinv : RInv own c hs Rp Hp μ + +variable {Rp Hp : A → Prop} + +theorem DInvW.adm {P : Set A} {q : Prog A V L} {μ : A → V} {π : A → Option V} + (I : DInvW own cls ko U c hs Rp Hp P q μ π) : AdmW own cls ko U c hs μ π (pick μ π) := by + refine ⟨fun y hy hu => ?_, fun y u hu => ?_, fun y hy => ?_⟩ <;> unfold pick + · cases hπ : π y with + | none => rfl + | some u => exact (I.pinPO y u hπ hy hu).symm + · rw [hu]; rfl + · cases hπ : π y with + | none => rfl + | some u => exact I.pinCls y u hπ hy + +theorem DInvW.freach {P : Set A} {q : Prog A V L} {μ μ' : A → V} {π : A → Option V} + (I : DInvW own cls ko U c hs Rp Hp P q μ π) (hR : FReach own cls ko c hs μ μ') : + DInvW own cls ko U c hs Rp Hp P q μ' π := by + obtain ⟨f1, f2, f3⟩ := freach_po hR + refine ⟨fun ν hν => ?_, I.pin, fun y u hu hy hU => ?_, fun y u hu hy => ?_, I.rinv.freach hR⟩ + · obtain ⟨κ, hd, h⟩ := I.run ν ⟨fun y hy hU => by rw [hν.1 y ((f1 y hy).1 ▸ hy) hU, (f1 y hy).2], + hν.2.1, fun y hy => by rw [hν.2.2 y hy, f3 y hy]⟩ + exact ⟨κ, hd.freach hR, h⟩ + · rw [(f1 y (f2 y hy)).2]; exact I.pinPO y u hu (f2 y hy) hU + · rw [f3 y hy]; exact I.pinCls y u hu hy + +/-- **A speculative or class read of `b` returning `v`**: pin `b` at `v`, which must be the real value if `b` is +readable and tracked, and of its class if `b` is class-tracked. -/ +theorem DInvW.pinR {P : Set A} {μ : A → V} {π : A → Option V} {b : A} {k : V → Prog A V L} {q : Prog A V L} + (v : V) (hq : ∀ ν κ, AltRun own cls ko c hs Rp Hp P q ν κ → + b ∉ P ∧ AltRun own cls ko c hs Rp Hp (insert b P) (k (ν b)) ν κ) + (hpo : PO c hs (own μ b) → ¬ U b → μ b = v) + (hcl : (ko b = .gate ∨ ko b = .core c) → cls b v = cls b (μ b)) + (I : DInvW own cls ko U c hs Rp Hp P q μ π) : + DInvW own cls ko U c hs Rp Hp (insert b P) (k v) μ (Function.update π b (some v)) := by + have hπb : π b = none := by + obtain ⟨κ, -, h⟩ := I.run _ I.adm + cases h' : π b with + | none => rfl + | some u => exact absurd (I.pin b u h') (hq _ _ h).1 + have ext : ∀ y u, π y = some u → Function.update π b (some v) y = some u := by + intro y u hu + have hyb : y ≠ b := by rintro rfl; rw [hπb] at hu; cases hu + rw [Function.update_of_ne hyb]; exact hu + refine ⟨fun ν hν => ?_, fun y u hu => ?_, fun y u hu hy hU => ?_, fun y u hu hy => ?_, I.rinv⟩ + · obtain ⟨κ, hd, h⟩ := I.run ν ⟨hν.1, fun y u hu => hν.2.1 y u (ext y u hu), hν.2.2⟩ + have h := (hq ν κ h).2 + rw [hν.2.1 b v (by rw [Function.update_self])] at h + exact ⟨κ, hd, h⟩ + all_goals by_cases hyb : y = b + · subst hyb; exact Set.mem_insert _ _ + · rw [Function.update_of_ne hyb] at hu; exact Set.mem_insert_of_mem _ (I.pin y u hu) + · subst hyb; rw [Function.update_self] at hu; rw [← Option.some.inj hu]; exact hpo hy hU + · rw [Function.update_of_ne hyb] at hu; exact I.pinPO y u hu hy hU + · subst hyb; rw [Function.update_self] at hu; rw [← Option.some.inj hu]; exact hcl hy + · rw [Function.update_of_ne hyb] at hu; exact I.pinCls y u hu hy + +/-- **`SafeW` from sequential runs** (`safe_of_runs` for `AdmW`): the run reading every weakly admissible memory +passes, its `rd` and written words tracked. -/ +theorem safe_of_runsW (hL : MoveHeld own) (hRU : ∀ x, Rp x → ¬ U x) (hHU : ∀ x, Hp x → ¬ U x) + (q : Prog A V L) : + ∀ (P : Set A) (μ : A → V) (π : A → Option V), DInvW own cls ko U c hs Rp Hp P q μ π → + SafeW own cls ko U c hs μ q := by + induction q with + | done => intros; exact .done + | lock l k _ => + intro _ _ _ I; obtain ⟨κ, -, h⟩ := I.run _ I.adm; simp only [AltRun] at h + | start k _ => + intro _ _ _ I; obtain ⟨κ, -, h⟩ := I.run _ I.adm; simp only [AltRun] at h + | rd a k ih => + intro P μ π I + have hRp : Rp a := by + obtain ⟨κ, -, h⟩ := I.run _ I.adm; simp only [AltRun] at h; exact h.1 + refine .rd (fun μ' hR => (I.freach hR).rinv.2 a hRp) (fun μ' hR => ?_) + have I' := I.freach hR + refine ih (μ' a) P μ' π ⟨fun ν hν => ?_, I'.pin, I'.pinPO, I'.pinCls, I'.rinv⟩ + obtain ⟨κ, hd, h⟩ := I'.run ν hν + simp only [AltRun] at h + rw [hν.1 a (I'.rinv.2 a hRp) (hRU a hRp)] at h + exact ⟨κ, hd, h.2⟩ + | rds b k ih => + intro P μ π I + refine .rds fun μ' v hR hv => ih v _ _ _ ((I.freach hR).pinR v + (fun ν κ h => by simp only [AltRun] at h; exact h) (fun hp hu => ?_) (fun hk => ?_)) + · rcases hv with rfl | ⟨h1, -⟩ + · rfl + · exact (h1.elim (fun n => n hp) hu).elim + · rcases hv with rfl | ⟨-, h2⟩ + · rfl + · exact h2 hk + | rdc b K k ih => + intro P μ π I + obtain ⟨κ, -, h0⟩ := I.run _ I.adm + simp only [AltRun] at h0 + refine .rdc h0.2.1 h0.2.2.1 fun μ' hR => ?_ + exact ih _ _ _ _ ((I.freach hR).pinR _ + (fun ν κ h => by simp only [AltRun] at h; exact ⟨h.1, h.2.2.2⟩) (fun _ _ => rfl) (fun _ => rfl)) + | wr a v k ih => + intro P μ π I + have hHp : Hp a := by + obtain ⟨κ, -, h⟩ := I.run _ I.adm; simp only [AltRun] at h; exact h.1 + have go : ∀ μ', FReach own cls ko c hs μ μ' → + HeldV c hs (own μ' a) ∧ MoveV own c hs μ' (Function.update μ' a v) ∧ + (cls a v = cls a (μ' a) ∨ ko a = .core c ∨ ko a = .free) := by + intro μ' hR + have I' := I.freach hR + obtain ⟨κ, hd, h0⟩ := I'.run _ I'.adm + simp only [AltRun] at h0 + obtain ⟨-, ⟨hM, hcl⟩, _⟩ := h0 + have ha := I'.rinv.1 a hHp + refine ⟨ha, moveV_transfer hL hd ha hM, ?_⟩ + rw [← hd.eq_of_po (Or.inl ha)]; exact hcl + refine .wr (fun μ' hR => (go μ' hR).1) (fun μ' hR => (go μ' hR).2.1) + (fun μ' hR => (go μ' hR).2.2) (fun μ' hR => ?_) + have I' := I.freach hR + obtain ⟨ha, hM, _⟩ := go μ' hR + obtain ⟨w1, w2, w3, _⟩ := write_po hM + have hpoa' : PO c hs (own (Function.update μ' a v) a) := Or.inl (w3 a ha) + refine ih P (Function.update μ' a v) (Function.update π a none) + ⟨fun ν'' hν => ?_, fun y u hu => ?_, fun y u hu hy hU => ?_, fun y u hu hy => ?_, + I'.rinv.write hM⟩ + · -- the certificate memory before the write: `ν''` with `a` at its value before + have hadm : AdmW own cls ko U c hs μ' π (Function.update ν'' a (μ' a)) := by + refine ⟨fun y hy hU => ?_, fun y u hu => ?_, fun y hy => ?_⟩ <;> by_cases hya : y = a + · subst hya; rw [Function.update_self] + · rw [Function.update_of_ne hya, hν.1 y (w1 y hy) hU, Function.update_of_ne hya] + · subst hya; rw [Function.update_self]; exact I'.pinPO y u hu (Or.inl ha) (hHU y hHp) + · rw [Function.update_of_ne hya]; exact hν.2.1 y u (by rw [Function.update_of_ne hya]; exact hu) + · subst hya; rw [Function.update_self] + · rw [Function.update_of_ne hya, hν.2.2 y hy, Function.update_of_ne hya] + obtain ⟨κ, hd, h⟩ := I'.run _ hadm + simp only [AltRun] at h + have hva : ν'' a = v := by rw [hν.1 a hpoa' (hHU a hHp), Function.update_self] + have e : Function.update (Function.update ν'' a (μ' a)) a v = ν'' := by + rw [Function.update_idem, ← hva, Function.update_eq_self] + rw [e] at h; exact ⟨_, hd.write hM, h.2.2⟩ + all_goals by_cases hya : y = a + · subst hya; rw [Function.update_self] at hu; cases hu + · rw [Function.update_of_ne hya] at hu; exact I'.pin y u hu + · subst hya; rw [Function.update_self] at hu; cases hu + · rw [Function.update_of_ne hya] at hu + rw [Function.update_of_ne hya]; exact I'.pinPO y u hu (w2 y hy) hU + · subst hya; rw [Function.update_self] at hu; cases hu + · rw [Function.update_of_ne hya] at hu + rw [Function.update_of_ne hya]; exact I'.pinCls y u hu hy + +/-! ## The invariant along weak executions -/ + +/-- Every local path in progress is in its glue, or in a body `SafeW` from the current memory with its locks. -/ +def SInvW (U : A → Prop) (s : Cfg A V C L) : Prop := + ∀ d p0 q W, s.ph d = .loc p0 q W → + Glue s.cls s.ko d q ∨ SafeW s.own s.cls s.ko U d (fun l => s.lk l = some d) s.mem q + +theorem sinvW_noLoc {s : Cfg A V C L} (h : NoLoc s) : SInvW U s := + fun d p0 q W hd => absurd (by rw [hd]; trivial) (h d) + +theorem allowed_of_sinvW {s : Cfg A V C L} (I : SInvW U s) (c : C) : Allowed s c := by + unfold Allowed + split + next p0 a k W hph => + rcases I c _ _ _ hph with hg | hS + · cases hg + · cases hS with | rd h _ => exact (h _ (.refl _)).imp_left held_of_heldV + next p0 a K k W hph => + rcases I c _ _ _ hph with hg | hS + · cases hg with | rdc hko hK _ => exact ⟨hko, hK⟩ + · cases hS with | rdc hko hK _ => exact ⟨hko, hK⟩ + next p0 a v k W hph => + rcases I c _ _ _ hph with hg | hS + · cases hg + · cases hS with + | wr h1 h2 h3 _ => + exact ⟨held_of_heldV (h1 _ (.refl _)), fun x hx => ⟨held_of_heldV (h2 _ (.refl _) x hx).1, + held_of_heldV (h2 _ (.refl _) x hx).2⟩, h3 _ (.refl _)⟩ + next => trivial + +/-- `SInvW` after a step of `c` that leaves the other cores' phases, the memory, the ghost maps and their locks as +they were, given `c`'s own path. -/ +theorem SInvW.other {s s' : Cfg A V C L} (I : SInvW U s) + (hph : ∀ d, d ≠ c → s'.ph d = s.ph d) (hm : s'.mem = s.mem) (ho : s'.own = s.own) (hcl : s'.cls = s.cls) + (hk : s'.ko = s.ko) (hlk : ∀ d, d ≠ c → ∀ l, (s'.lk l = some d ↔ s.lk l = some d)) + (hc : ∀ p0 q W, s'.ph c = .loc p0 q W → + Glue s'.cls s'.ko c q ∨ SafeW s'.own s'.cls s'.ko U c (fun l => s'.lk l = some c) s'.mem q) : + SInvW U s' := by + intro d p0 q W hd + by_cases hdc : d = c + · subst hdc; exact hc p0 q W hd + · rw [hph d hdc] at hd + have e : (fun l => s'.lk l = some d) = (fun l => s.lk l = some d) := funext fun l => propext (hlk d hdc l) + rw [hm, ho, hcl, hk, e]; exact I d p0 q W hd + +/-- What another core sees of an `Allowed` write: a `Foreign` one. -/ +theorem foreign_of_wr {s : Cfg A V C L} {d : C} {p0 : Prog A V L} {a : A} {v : V} {k : Prog A V L} {W : Set A} + (hc : s.ph c = .loc p0 (.wr a v k) W) (hal : Allowed s c) (hdc : d ≠ c) : + Foreign s.own s.cls s.ko d (fun l => s.lk l = some d) s.mem a v := by + simp only [Allowed, hc] at hal + obtain ⟨hb, hmv, hcl⟩ := hal + have hcd : c ≠ d := Ne.symm hdc + refine ⟨not_PO_of_held hcd hb, fun x hx => + ⟨not_PO_of_held hcd (hmv x hx).1, not_PO_of_held hcd (hmv x hx).2⟩, ?_⟩ + rcases hcl with h | h | h + · exact Or.inl h + · exact Or.inr ⟨(by rw [h]; exact fun e => by cases e), (by rw [h]; exact fun e => hcd (ClsOwner.core.inj e))⟩ + · exact Or.inr ⟨(by rw [h]; exact fun e => by cases e), (by rw [h]; exact fun e => by cases e)⟩ + +/-- **`SInvW` is kept by a weak step** `StepR U`, given that a body it starts is `SafeW` (`hB`), armed programs are +pure glue (`hA`), and no local path runs under a closed gate (`hno`). -/ +theorem sinv_stepRW {s s' : Cfg A V C L} (hno : s.gate = .closed → NoLoc s) (hA : ArmInv s) + (hB : ∀ p0 k W, s.ph c = .loc p0 (.start k) W → + SafeW s.own s.cls s.ko U c (fun l => s.lk l = some c) s.mem k) (I : SInvW U s) + (hal : Allowed s c) (hst : StepR U s c s') : SInvW U s' := by + have up : ∀ (X : Phase A V L) d, d ≠ c → Function.update s.ph c X d = s.ph d := + fun X d hd => Function.update_of_ne hd _ _ + have nl : ∀ {X : Phase A V L} {p0 q W}, ¬ X.IsLoc → Function.update s.ph c X c ≠ .loc p0 q W := + fun {X _ _ _} hX h => hX (by rw [← Function.update_self c X s.ph, h]; trivial) + have self : ∀ {X : Phase A V L} {p0 q W}, Function.update s.ph c X c = .loc p0 q W → X = .loc p0 q W := + fun h => (Function.update_self c _ s.ph).symm.trans h + cases hst with + | rdsW v hc hp hk1 hk2 => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h + rcases I c _ _ _ hc with hg | hS + · cases hg with | rds hk => exact Or.inl (hk v) + · cases hS with + | rds h => + refine Or.inr (h _ v (.refl _) (Or.inr ⟨hp.imp_left fun hn hP => hn ?_, fun hk => (hk.elim hk1 hk2).elim⟩)) + exact Or.imp_left held_of_heldV hP + | rdsC v hc hp hk hcl => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h + rcases I c _ _ _ hc with hg | hS + · cases hg with | rds hk => exact Or.inl (hk v) + · cases hS with + | rds h => + refine Or.inr (h _ v (.refl _) (Or.inr ⟨hp.imp_left fun hn hP => hn ?_, fun _ => hcl⟩)) + exact Or.imp_left held_of_heldV hP + | step hst => + cases hst with + | arm p hc => exact I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun _ _ _ h => absurd h (nl id) + | back hc _ => exact I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun _ _ _ h => absurd h (nl id) + | take p hc _ => exact I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun _ _ _ h => absurd h (nl id) + | scan _ _ _ _ => exact I + | drain _ _ _ => exact I + | @enter p hc _ => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h; exact Or.inl ((hA c _ hc).glue _ _ _) + | @rd p1 a k W1 hc => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h + rcases I c _ _ _ hc with hg | hS + · cases hg + · cases hS with | rd _ h => exact Or.inr (h _ (.refl _)) + | @rds p1 a k W1 hc => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h + rcases I c _ _ _ hc with hg | hS + · cases hg with | rds hk => exact Or.inl (hk _) + · cases hS with | rds h => exact Or.inr (h _ _ (.refl _) (Or.inl rfl)) + | @rdc p1 a K k W1 hc => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h + rcases I c _ _ _ hc with hg | hS + · cases hg with | rdc _ _ hk => exact Or.inl (hk _) + · cases hS with | rdc _ _ h => exact Or.inr (h _ (.refl _)) + | @start p1 k W1 hc => + refine I.other (up _) rfl rfl rfl rfl (fun _ _ _ => Iff.rfl) fun p0 q W h => ?_ + cases self h; exact Or.inr (hB _ _ _ hc) + | @lock p1 l k W1 b hc hb => + refine I.other (up _) rfl rfl rfl rfl (fun d hdc l' => ?_) fun p0 q W h => ?_ + · cases b with + | false => exact Iff.rfl + | true => + show Function.update s.lk l (some c) l' = some d ↔ s.lk l' = some d + by_cases hl : l' = l + · subst hl + rw [Function.update_self, hb rfl] + exact ⟨fun h => absurd (Option.some.inj h) (Ne.symm hdc), fun h => by cases h⟩ + · rw [Function.update_of_ne hl] + · cases self h + rcases I c _ _ _ hc with hg | hS + · cases hg with | lock hk => exact Or.inl (hk b) + · cases hS + | @leave p1 W1 hc => + refine I.other (up _) rfl rfl rfl rfl (fun d hdc l' => ?_) fun _ _ _ h => absurd h (nl id) + show (if s.lk l' = some c then none else s.lk l') = some d ↔ s.lk l' = some d + by_cases hl : s.lk l' = some c + · rw [if_pos hl, hl] + exact ⟨fun h => (by cases h), fun h => absurd (Option.some.inj h) (Ne.symm hdc)⟩ + · rw [if_neg hl] + | serial keep own' cls' ko' _ hg _ _ => + intro d p0 q W hd + by_cases hdc : d = c + · subst hdc; exact absurd hd (nl id) + · have hd' : s.ph d = .loc p0 q W := (up _ d hdc).symm.trans hd + exact absurd (by rw [hd']; trivial) (hno hg d) + | @wr p1 a v k W1 hc => + intro d p0 q W hd + by_cases hdc : d = c + · subst hdc + cases self hd + rcases I d _ _ _ hc with hg | hS + · cases hg + · cases hS with | wr _ _ _ h => exact Or.inr (h _ (.refl _)) + · have hd' : s.ph d = .loc p0 q W := (up _ d hdc).symm.trans hd + rcases I d p0 q W hd' with hg | hS + · exact Or.inl hg + · exact Or.inr (hS.foreign (foreign_of_wr hc hal hdc)) + +/-- **`SInvW` is kept by a hardware write**: to every in-progress local path it is `Foreign`. -/ +theorem sinvW_hw {s : Cfg A V C L} (I : SInvW U s) {a : A} {x : V} {l : L} (h : HwWr s a x l) : + SInvW U (hwCfg s a x) := by + intro d p0 q W hd + rcases I d p0 q W hd with hg | hS + · exact Or.inl hg + · refine Or.inr (hS.foreign ⟨?_, fun y hy => absurd (by rw [h.keep]) hy, ?_⟩) + · rw [h.own]; rintro (hl | hl) + · change s.lk l = some d at hl; rw [h.free] at hl; cases hl + · cases hl + · right; rw [h.ko]; exact ⟨fun e => (by cases e), fun e => (by cases e)⟩ + +/-! ## Weak executions with hardware writes -/ + +/-- Executions of weak steps whose local-path accesses respect ownership, with hardware writes. -/ +inductive XGReachR (s0 : Cfg A V C L) : Cfg A V C L → Prop + | refl : XGReachR s0 s0 + | step {s s' : Cfg A V C L} {c : C} {U : A → Prop} : + XGReachR s0 s → Allowed s c → StepR U s c s' → XGReachR s0 s' + | hw {s : Cfg A V C L} {a : A} {x : V} {l : L} : + XGReachR s0 s → HwWr s a x l → XGReachR s0 (hwCfg s a x) + +theorem xgreachR_inv {s0 s : Cfg A V C L} (h0 : Init s0) (h : XGReachR s0 s) : ∃ H, InvH s0.mem s H := by + induction h with + | refl => exact ⟨_, inv_init h0⟩ + | step _ hal hs ih => obtain ⟨H, I⟩ := ih; exact I.stepR hal hs + | hw _ hh ih => obtain ⟨H, I⟩ := ih; exact ⟨_, inv_hw I hh⟩ + +/-- `no_overlap` for weak executions with hardware writes. -/ +theorem no_overlapXR {s0 s : Cfg A V C L} (h0 : Init s0) (h : XGReachR s0 s) : s.gate = .closed → NoLoc s := + (xgreachR_inv h0 h).elim fun _ I => I.clo + +/-- **`serializable_quiescent` for weak executions with hardware writes**: with no local path in progress, memory +is the serial run of the completed entries (a speculative read returning any value, as in `Serial`). -/ +theorem serializable_quiescentXR {s0 s : Cfg A V C L} (h0 : Init s0) (h : XGReachR s0 s) (hq : NoLoc s) : + RunAll s.log s0.mem s.mem := by + obtain ⟨H, I⟩ := xgreachR_inv h0 h + have : s.mem = H := funext fun x => I.agree x (fun d q0 q W hd => absurd (by rw [hd]; trivial) (hq d)) + rw [this]; exact I.hist + +end + +end Fl4Proofs.P2.Serial diff --git a/proofs/Fl4P2Base/SerialD.lean b/proofs/Fl4P2Base/SerialD.lean new file mode 100644 index 000000000..04fa5ffd2 --- /dev/null +++ b/proofs/Fl4P2Base/SerialD.lean @@ -0,0 +1,61 @@ +/- +# P2 (sync, gen 1): **the deterministic sequential run** `RunD` (architecture-independent) + +`Serial.Run` lets a speculative read `rds` return any value, a class read `rdc` any `K`-related value and a lock +attempt either outcome. `RunD` is the run in which every read (`rd`, `rds`, `rdc`) returns the current memory's +value: the run of a body "agreeing with the store's reads". It is the left-hand side of G1 (adequacy of `tr`), +which each architecture proves against its own extracted store run (`Fl4Proofs/P2/GlueAdeq.lean` on x86_64, +`Fl4ProofsA64/P2/GlueAdeqA.lean` on AArch64). + +* `RunD.run`: a deterministic run is a `Run` (for `rdc`, when the class relation is reflexive at the value read). +* `RunD.det`: the deterministic run is unique. + +It is stated over `Serial.Prog` only (no `Fl4Core` import), so both architectures share it. The declarations keep +the namespace `Fl4Proofs.P2.Serial`. **Integration note:** `Fl4Proofs/P2/GlueAdeq.lean` (x86_64) still carries its +own copies of these three declarations under the same names; no module imports both today. Folding the x86 copies +into a re-export of this file is a pure deletion there (see `handoff/p2sync.md`). + +No premise: no [J], [P] or [W]. No existing statement changed. +-/ +import Fl4P2Base.Serial + +namespace Fl4Proofs.P2.Serial + +noncomputable section + +section rund +variable {A V L : Type} [DecidableEq A] + +/-- **The deterministic sequential run**: every read returns the current memory's value (module docstring). Bodies +take no locks and have no `start`, so there are no such steps. -/ +inductive RunD : Prog A V L → (A → V) → (A → V) → Prop + | done (m : A → V) : RunD .done m m + | rd {a : A} {k : V → Prog A V L} {m m' : A → V} : RunD (k (m a)) m m' → RunD (.rd a k) m m' + | wr {a : A} {v : V} {k : Prog A V L} {m m' : A → V} : + RunD k (Function.update m a v) m' → RunD (.wr a v k) m m' + | rds {a : A} {k : V → Prog A V L} {m m' : A → V} : RunD (k (m a)) m m' → RunD (.rds a k) m m' + | rdc {a : A} {K : V → V → Prop} {k : V → Prog A V L} {m m' : A → V} : + K (m a) (m a) → RunD (k (m a)) m m' → RunD (.rdc a K k) m m' + +/-- A deterministic run is a sequential run. -/ +theorem RunD.run {q : Prog A V L} {m m' : A → V} (h : RunD q m m') : Run q m m' := by + induction h with + | done m => exact .done m + | rd _ ih => exact .rd ih + | wr _ ih => exact .wr ih + | rds _ ih => exact .rds _ ih + | rdc hK _ ih => exact .rdc _ hK ih + +/-- **The deterministic run is unique.** -/ +theorem RunD.det {q : Prog A V L} {m m1 m2 : A → V} (h1 : RunD q m m1) (h2 : RunD q m m2) : m1 = m2 := by + induction h1 generalizing m2 with + | done m => cases h2; rfl + | rd _ ih => cases h2 with | rd h => exact ih h + | wr _ ih => cases h2 with | wr h => exact ih h + | rds _ ih => cases h2 with | rds h => exact ih h + | rdc _ _ ih => cases h2 with | rdc _ h => exact ih h + +end rund + +end +end Fl4Proofs.P2.Serial diff --git a/proofs/Fl4P2Base/SerialR.lean b/proofs/Fl4P2Base/SerialR.lean new file mode 100644 index 000000000..537f0b94c --- /dev/null +++ b/proofs/Fl4P2Base/SerialR.lean @@ -0,0 +1,574 @@ +/- +# P2 (sync, gen 2): **the replay record** beside `InvH` (architecture-independent; DESIGN §9 item 4, Half 1 (a)) + +`InvH` keeps, for each local path in progress, `LocInv.sim`: a `Run` of the logged program from the history. It +forgets which values the path's reads actually returned, so at `leave` nothing ties the memory the path wrote to +*one* sequential run of its body. This module adds, beside `InvH` and without changing it, a record per started +local path (`RecD`: the state `s0` where its body started, the body `b`, a read memory `ρ` and the set `P` of words +it has read speculatively) and the invariant `LocRec`: + +* `run`: for every memory `ν` **admissible** at the start (`AdmR`: equal to `s0.mem` on every word the core may + read, of its class on every class-tracked word) that agrees with `ρ` on `P`, a deterministic run (`RunD`) of what + is left of the body from `ν` with the path's own writes in place is a deterministic run of `b` from `ν`; +* `once`: along those runs each speculative or class read is of a word not yet in `P` (`OnceD`, read once along the + deterministic run; Risk 4 of gen 1: per run, not per tree); +* `adm`: `ρ` itself is admissible; `perm` / `ko` / `cls`: what the core may read and the class maps are the start's; + `stab` / `clsM`: the words the core may read and has not written keep their start value, the class-tracked ones + their start class. + +A speculative read of a fresh word `a` pins `ρ a` to the value read (`racyR`) when the core may not read `a`; a +read of a word the core may read returns its start value (`stab`), which every admissible `ν` has. So at `leave` +(`InvR.leave_run`) the whole body is **one** deterministic run from the admissible memory `ρ`, ending in the memory +the path left on its written words: `RunD b ρ (mix W s.mem ρ)`. + +* `InvR m0 s H R`: `InvH m0 s H` and every record of `R` holds (`RecOK`). +* **`InvR.step`**: every `Allowed` step keeps it (`H'` is `InvH.step`'s: the same `H` when the log does not grow, + `mix W s.mem H` at a `leave`), given at a `start` step the premise `StartOnce` (read-once from every admissible + memory). `RecFrom` says where the core's new record comes from. +* **`InvR.hw`**: a hardware write (`SerialHw.HwWr`) keeps it, the history taking the same write. + **Risk 3 (gen 1), checked here:** `HwWr.free` asks that no core hold the written word's lock, so the word is + `Perm` for no path in progress (`HwWr.ne_of_perm`) and never class-tracked (`HwWr.ko`). A path holding a VCPU + lock (AArch64's `ARMVCPURun` exit, which takes `lock v` before its body) therefore sees no hardware write of `v`'s + words at all (`HwWr.not_held`), and one of another lock's words is foreign to it: `LocRec.hw` covers both with + no case split. On AArch64 `HwStepPA.hwWr` builds `HwWr.free` from `HwStepVA.free : s.lk v = none`. +* `WrIn Q q ν`: every write of the deterministic run of `q` from `ν` is of a word in `Q`; `RunD.wrIn`: the run + changes nothing outside `Q`. + +No premise: no [J], [P] or [W] (`StartOnce` is a hypothesis of `InvR.step`, discharged on AArch64 by +`Fl4ProofsA64/P2/GlueRepA.lean`'s `LocalPath.startOnce` from the path's `ReadOnce` [W]). No existing statement +changed. +-/ +import Fl4P2Base.SerialHw +import Fl4P2Base.SerialD +import Fl4P2Base.SafeCert + +namespace Fl4Proofs.P2.Serial + +noncomputable section + +section onced +variable {A V L : Type} [DecidableEq A] + +/-- **Read once along the deterministic run** of `q` from `ν`: each speculative or class read is of a word not in +`P` (the words read that way so far), which it then joins; a class read's relation is reflexive at the value read; +no lock attempt, no `start`. -/ +def OnceD : Set A → Prog A V L → (A → V) → Prop + | _, .done, _ => True + | P, .rd a k, ν => OnceD P (k (ν a)) ν + | P, .rds a k, ν => a ∉ P ∧ OnceD (insert a P) (k (ν a)) ν + | P, .rdc a K k, ν => a ∉ P ∧ K (ν a) (ν a) ∧ OnceD (insert a P) (k (ν a)) ν + | P, .wr a v k, ν => OnceD P k (Function.update ν a v) + | _, .lock _ _, _ => False + | _, .start _, _ => False + +theorem onceD_done {P : Set A} {ν : A → V} : OnceD P (.done : Prog A V L) ν := trivial + +theorem onceD_rd {P : Set A} {a : A} {k : V → Prog A V L} {ν : A → V} : + OnceD P (.rd a k) ν ↔ OnceD P (k (ν a)) ν := Iff.rfl + +theorem onceD_rds {P : Set A} {a : A} {k : V → Prog A V L} {ν : A → V} : + OnceD P (.rds a k) ν ↔ a ∉ P ∧ OnceD (insert a P) (k (ν a)) ν := Iff.rfl + +theorem onceD_rdc {P : Set A} {a : A} {K : V → V → Prop} {k : V → Prog A V L} {ν : A → V} : + OnceD P (.rdc a K k) ν ↔ a ∉ P ∧ K (ν a) (ν a) ∧ OnceD (insert a P) (k (ν a)) ν := Iff.rfl + +theorem onceD_wr {P : Set A} {a : A} {v : V} {k : Prog A V L} {ν : A → V} : + OnceD P (.wr a v k) ν ↔ OnceD P k (Function.update ν a v) := Iff.rfl + +/-- Every write of the deterministic run of `q` from `ν` is of a word in `Q`. -/ +def WrIn (Q : A → Prop) : Prog A V L → (A → V) → Prop + | .done, _ => True + | .rd a k, ν => WrIn Q (k (ν a)) ν + | .rds a k, ν => WrIn Q (k (ν a)) ν + | .rdc a _ k, ν => WrIn Q (k (ν a)) ν + | .wr a v k, ν => Q a ∧ WrIn Q k (Function.update ν a v) + | .lock _ _, _ => True + | .start _, _ => True + +theorem wrIn_done {Q : A → Prop} {ν : A → V} : WrIn Q (.done : Prog A V L) ν := trivial + +theorem wrIn_wr {Q : A → Prop} {a : A} {v : V} {k : Prog A V L} {ν : A → V} : + WrIn Q (.wr a v k) ν ↔ Q a ∧ WrIn Q k (Function.update ν a v) := Iff.rfl + +/-- **A deterministic run writes only in `Q`** when `WrIn Q` holds along it. -/ +theorem RunD.wrIn {Q : A → Prop} {q : Prog A V L} {ν m' : A → V} (h : RunD q ν m') : + WrIn Q q ν → ∀ x, ¬ Q x → m' x = ν x := by + induction h with + | done m => intro _ _ _; rfl + | rd _ ih => exact ih + | @wr a v k m m' _ ih => + intro hw x hx + obtain ⟨h1, h2⟩ := wrIn_wr.mp hw + rw [ih h2 x hx, Function.update_of_ne (fun h => hx (by rw [h]; exact h1))] + | rds _ ih => exact ih + | rdc _ _ ih => exact ih + +end onced + +variable {A V C L : Type} [DecidableEq A] [DecidableEq C] [DecidableEq L] + +/-! ## Admissible memories and the record -/ + +/-- **Admissible memories** for core `d` at the start state `s0` (`SafeCert.Adm` with no pins: `admR_iff`): equal +to `s0.mem` on every word `d` may read, of its class on every word whose class the gate or `d` owns. -/ +def AdmR (s0 : Cfg A V C L) (d : C) (ν : A → V) : Prop := + (∀ x, Perm s0 d x → ν x = s0.mem x) ∧ + (∀ x, (s0.ko x = .gate ∨ s0.ko x = .core d) → s0.cls x (ν x) = s0.cls x (s0.mem x)) + +theorem admR_iff {s0 : Cfg A V C L} {d : C} {ν : A → V} : + AdmR s0 d ν ↔ Adm s0.own s0.cls s0.ko d (fun l => s0.lk l = some d) s0.mem (fun _ => none) ν := by + constructor + · rintro ⟨h1, h2⟩ + refine ⟨fun y hy => h1 y ?_, fun y u hu => by simp at hu, h2⟩ + rcases hy with hy | hy + · exact Or.inl (held_of_heldV hy) + · exact Or.inr hy + · rintro ⟨h1, -, h3⟩ + refine ⟨fun y hy => h1 y ?_, h3⟩ + rcases hy with hy | hy + · exact Or.inl (heldV_of_held hy) + · exact Or.inr hy + +/-- The record of a started local path: the state where its body started, the body, the read memory, the words +read speculatively. -/ +structure RecD (A V C L : Type) where + s0 : Cfg A V C L + b : Prog A V L + ρ : A → V + P : Set A + +/-- **The replay record** of core `d`'s local path, `q` left to run, having written `W` (module docstring). -/ +structure LocRec (s : Cfg A V C L) (d : C) (e : RecD A V C L) (q : Prog A V L) (W : Set A) : Prop where + run : ∀ ν, AdmR e.s0 d ν → (∀ x ∈ e.P, ν x = e.ρ x) → + ∀ m', RunD q (mix W s.mem ν) m' → RunD e.b ν m' + once : ∀ ν, AdmR e.s0 d ν → (∀ x ∈ e.P, ν x = e.ρ x) → OnceD e.P q (mix W s.mem ν) + adm : AdmR e.s0 d e.ρ + perm : ∀ x, Perm s d x ↔ Perm e.s0 d x + ko : s.ko = e.s0.ko + cls : s.cls = e.s0.cls + stab : ∀ x, x ∉ W → Perm s d x → s.mem x = e.s0.mem x + clsM : ∀ x, x ∉ W → (s.ko x = .gate ∨ s.ko x = .core d) → s.cls x (s.mem x) = s.cls x (e.s0.mem x) + +/-- Every record of `R` belongs to a local path in progress and holds. -/ +def RecOK (s : Cfg A V C L) (R : C → Option (RecD A V C L)) : Prop := + ∀ d e, R d = some e → ∃ p0 q W, s.ph d = .loc p0 q W ∧ LocRec s d e q W + +/-- **The invariant with replay records**: `InvH` and the records. -/ +structure InvR (m0 : A → V) (s : Cfg A V C L) (H : A → V) (R : C → Option (RecD A V C L)) : Prop where + inv : InvH m0 s H + recs : RecOK s R + +/-- The premise of a `start` step (`InvR.step`): the started body reads once along its deterministic run from +every admissible memory. -/ +def StartOnce (s : Cfg A V C L) (c : C) : Prop := + ∀ p0 k W, s.ph c = .loc p0 (.start k) W → ∀ ν, AdmR s c ν → OnceD ∅ k ν + +/-- Where core `c`'s record after a step comes from: its record before (same start, same body), or a `start` +step from `s` itself. -/ +def RecFrom (s : Cfg A V C L) (c : C) (R : C → Option (RecD A V C L)) (o : Option (RecD A V C L)) : Prop := + ∀ e', o = some e' → (∃ e, R c = some e ∧ e'.s0 = e.s0 ∧ e'.b = e.b) ∨ + (∃ p0 W, s.ph c = .loc p0 (.start e'.b) W ∧ e'.s0 = s) + +/-- The record a `start` step opens. -/ +def startR (s : Cfg A V C L) (k : Prog A V L) : RecD A V C L := ⟨s, k, s.mem, ∅⟩ + +open Classical in +/-- The record after a speculative or class read of `a`: `a` joins `P`; where the core may not read `a`, `ρ a` is +pinned to the value read. -/ +def racyR (s : Cfg A V C L) (c : C) (e : RecD A V C L) (a : A) : RecD A V C L := + { e with ρ := if Perm s c a then e.ρ else Function.update e.ρ a (s.mem a), P := insert a e.P } + +theorem racyR_pos {s : Cfg A V C L} {c : C} {e : RecD A V C L} {a : A} (hp : Perm s c a) : + (racyR s c e a).ρ = e.ρ := if_pos hp + +theorem racyR_neg {s : Cfg A V C L} {c : C} {e : RecD A V C L} {a : A} (hp : ¬ Perm s c a) : + (racyR s c e a).ρ = Function.update e.ρ a (s.mem a) := if_neg hp + +/-! ## Who may read what, across steps -/ + +theorem held_lock_iff {lk : L → Option C} {l : L} {c d : C} {b : Bool} (hdc : d ≠ c) + (hb : b = true → lk l = none) (o : Owner C L) : + Held (if b then Function.update lk l (some c) else lk) d o ↔ Held lk d o := by + refine ⟨fun h => ?_, held_lock hb⟩ + cases b with + | false => rw [if_neg (by decide)] at h; exact h + | true => + rw [if_pos rfl] at h + cases o with + | core e => exact h + | lock l' => + have h' : Function.update lk l (some c) l' = some d := h + by_cases hl : l' = l + · subst hl; rw [Function.update_self] at h'; exact absurd (Option.some.inj h').symm hdc + · rw [Function.update_of_ne hl] at h'; exact h' + | gate => exact h + +theorem held_release_iff {lk : L → Option C} {c d : C} (hdc : d ≠ c) (o : Owner C L) : + Held (release lk c) d o ↔ Held lk d o := by + refine ⟨fun h => ?_, held_release hdc⟩ + cases o with + | core e => exact h + | lock l => + have h' : (if lk l = some c then none else lk l) = some d := h + by_cases hl : lk l = some c + · rw [if_pos hl] at h'; cases h' + · rw [if_neg hl] at h'; exact h' + | gate => exact h + +/-- Another core's `Allowed` write leaves what `d` may read unchanged. -/ +theorem perm_wr_other {s : Cfg A V C L} {c d : C} (hdc : d ≠ c) {a : A} {v : V} + (hmv : ∀ x, s.own (Function.update s.mem a v) x ≠ s.own s.mem x → + Held s.lk c (s.own s.mem x) ∧ Held s.lk c (s.own (Function.update s.mem a v) x)) (x : A) : + (Held s.lk d (s.own (Function.update s.mem a v) x) ∨ s.own (Function.update s.mem a v) x = .gate) ↔ + Perm s d x := by + by_cases he : s.own (Function.update s.mem a v) x = s.own s.mem x + · rw [he]; exact Iff.rfl + · exact ⟨fun h => absurd h (fun h' => held_disj (Ne.symm hdc) (hmv x he).2 h'), + fun h => absurd h (fun h' => held_disj (Ne.symm hdc) (hmv x he).1 h')⟩ + +/-- The core's own `Allowed` write leaves what it may read unchanged (owners move between held ones). -/ +theorem perm_wr_own {s : Cfg A V C L} {d : C} {a : A} {v : V} + (hmv : ∀ x, s.own (Function.update s.mem a v) x ≠ s.own s.mem x → + Held s.lk d (s.own s.mem x) ∧ Held s.lk d (s.own (Function.update s.mem a v) x)) (x : A) : + (Held s.lk d (s.own (Function.update s.mem a v) x) ∨ s.own (Function.update s.mem a v) x = .gate) ↔ + Perm s d x := by + by_cases he : s.own (Function.update s.mem a v) x = s.own s.mem x + · rw [he]; exact Iff.rfl + · exact ⟨fun _ => Or.inl (hmv x he).1, fun _ => Or.inl (hmv x he).2⟩ + +/-- **Risk 3**: a hardware write of a word of lock `l` happens only while no core holds `l`. -/ +theorem HwWr.not_held {s : Cfg A V C L} {a : A} {x : V} {l : L} (h : HwWr s a x l) (d : C) : + s.lk l ≠ some d := by + rw [h.free]; exact fun h => by cases h + +/-! ## The record across steps -/ + +section rec +variable {s s' : Cfg A V C L} {c d : C} {e : RecD A V C L} {q : Prog A V L} {W : Set A} + +/-- **The frame**: a step that changes neither what `d` may read nor the class maps, and writes only words `d` may +not read, has not written, and (if class-tracked for `d`) in their class. -/ +theorem LocRec.frame (I : LocRec s d e q W) (hp : ∀ x, Perm s' d x ↔ Perm s d x) (hko : s'.ko = s.ko) + (hcl : s'.cls = s.cls) + (hm : ∀ x, s'.mem x ≠ s.mem x → ¬ Perm s d x ∧ x ∉ W ∧ + ((s.ko x = .gate ∨ s.ko x = .core d) → s.cls x (s'.mem x) = s.cls x (s.mem x))) : + LocRec s' d e q W := by + have hmix : ∀ ν : A → V, mix W s'.mem ν = mix W s.mem ν := by + intro ν; funext x + by_cases hx : x ∈ W + · rw [mix_mem hx, mix_mem hx] + by_contra hne; exact (hm x hne).2.1 hx + · rw [mix_nmem hx, mix_nmem hx] + refine ⟨fun ν h1 h2 m' hr => I.run ν h1 h2 m' (by rw [← hmix ν]; exact hr), + fun ν h1 h2 => by rw [hmix ν]; exact I.once ν h1 h2, I.adm, fun x => (hp x).trans (I.perm x), + hko.trans I.ko, hcl.trans I.cls, fun x hx hpx => ?_, fun x hx hk => ?_⟩ + · have hpx' := (hp x).mp hpx + have he : s'.mem x = s.mem x := by + by_contra hne; exact (hm x hne).1 hpx' + rw [he]; exact I.stab x hx hpx' + · rw [hko] at hk; rw [hcl] + by_cases hne : s'.mem x = s.mem x + · rw [hne]; exact I.clsM x hx hk + · rw [(hm x hne).2.2 hk]; exact I.clsM x hx hk + +/-- A word the core may read has, in every admissible `ν` with its writes in place, the value memory holds. -/ +theorem LocRec.val (I : LocRec s c e q W) {ν : A → V} (hν : AdmR e.s0 c ν) {a : A} (hp : Perm s c a) : + mix W s.mem ν a = s.mem a := by + by_cases haW : a ∈ W + · exact mix_mem haW + · rw [mix_nmem haW, hν.1 a ((I.perm a).mp hp), I.stab a haW hp] + +theorem racyR_P {a : A} (ha : a ∉ e.P) {ν : A → V} + (hρ : ∀ x ∈ (racyR s c e a).P, ν x = (racyR s c e a).ρ x) : ∀ x ∈ e.P, ν x = e.ρ x := by + intro x hx + have hxa : x ≠ a := by rintro rfl; exact ha hx + rw [hρ x (Set.mem_insert_of_mem a hx)] + by_cases hp : Perm s c a + · rw [racyR_pos hp] + · rw [racyR_neg hp, Function.update_of_ne hxa] + +theorem LocRec.racyR_val (I : LocRec s c e q W) (hW : ∀ x ∈ W, Perm s c x) {a : A} {ν : A → V} + (hν : AdmR e.s0 c ν) (hρ : ∀ x ∈ (racyR s c e a).P, ν x = (racyR s c e a).ρ x) : + mix W s.mem ν a = s.mem a := by + by_cases hp : Perm s c a + · exact I.val hν hp + · have haW : a ∉ W := fun h => hp (hW a h) + rw [mix_nmem haW, hρ a (Set.mem_insert a e.P), racyR_neg hp, Function.update_self] + +theorem LocRec.racyR_adm (I : LocRec s c e q W) (hW : ∀ x ∈ W, Perm s c x) (a : A) : + AdmR e.s0 c (racyR s c e a).ρ := by + by_cases hp : Perm s c a + · rw [racyR_pos hp]; exact I.adm + · rw [racyR_neg hp] + have haW : a ∉ W := fun h => hp (hW a h) + refine ⟨fun x hx => ?_, fun x hx => ?_⟩ + · have hxa : x ≠ a := by rintro rfl; exact hp ((I.perm x).mpr hx) + rw [Function.update_of_ne hxa]; exact I.adm.1 x hx + · by_cases hxa : x = a + · subst hxa + rw [Function.update_self] + have h1 := I.clsM x haW (by rw [I.ko]; exact hx) + rw [I.cls] at h1; exact h1 + · rw [Function.update_of_ne hxa]; exact I.adm.2 x hx + +variable {p0 : Prog A V L} {a : A} {k : V → Prog A V L} + +/-- The core's own read of a word it may read. -/ +theorem LocRec.rd (I : LocRec s c e (.rd a k) W) (hp : Perm s c a) : + LocRec { s with ph := Function.update s.ph c (.loc p0 (k (s.mem a)) W) } c e (k (s.mem a)) W := by + refine ⟨fun ν hν hρ m' hr => I.run ν hν hρ m' (.rd ?_), fun ν hν hρ => ?_, I.adm, I.perm, I.ko, I.cls, + I.stab, I.clsM⟩ + · change RunD (k (s.mem a)) (mix W s.mem ν) m' at hr + rw [I.val hν hp]; exact hr + · have h := onceD_rd.mp (I.once ν hν hρ) + rw [I.val hν hp] at h; exact h + +/-- The core's own speculative read. -/ +theorem LocRec.rds (I : LocRec s c e (.rds a k) W) (hW : ∀ x ∈ W, Perm s c x) : + LocRec { s with ph := Function.update s.ph c (.loc p0 (k (s.mem a)) W) } c (racyR s c e a) + (k (s.mem a)) W := by + have ha : a ∉ e.P := (onceD_rds.mp (I.once e.ρ I.adm (fun _ _ => rfl))).1 + refine ⟨fun ν hν hρ m' hr => I.run ν hν (racyR_P ha hρ) m' (.rds ?_), fun ν hν hρ => ?_, + I.racyR_adm hW a, I.perm, I.ko, I.cls, I.stab, I.clsM⟩ + · change RunD (k (s.mem a)) (mix W s.mem ν) m' at hr + rw [I.racyR_val hW hν hρ]; exact hr + · have h := (onceD_rds.mp (I.once ν hν (racyR_P ha hρ))).2 + rw [I.racyR_val hW hν hρ] at h; exact h + +/-- The core's own class read. -/ +theorem LocRec.rdc {K : V → V → Prop} (I : LocRec s c e (.rdc a K k) W) (hW : ∀ x ∈ W, Perm s c x) : + LocRec { s with ph := Function.update s.ph c (.loc p0 (k (s.mem a)) W) } c (racyR s c e a) + (k (s.mem a)) W := by + have ha : a ∉ e.P := (onceD_rdc.mp (I.once e.ρ I.adm (fun _ _ => rfl))).1 + refine ⟨fun ν hν hρ m' hr => ?_, fun ν hν hρ => ?_, + I.racyR_adm hW a, I.perm, I.ko, I.cls, I.stab, I.clsM⟩ + · have h := onceD_rdc.mp (I.once ν hν (racyR_P ha hρ)) + refine I.run ν hν (racyR_P ha hρ) m' (.rdc h.2.1 ?_) + change RunD (k (s.mem a)) (mix W s.mem ν) m' at hr + rw [I.racyR_val hW hν hρ]; exact hr + · have h := (onceD_rdc.mp (I.once ν hν (racyR_P ha hρ))).2.2 + rw [I.racyR_val hW hν hρ] at h; exact h + +/-- The core's own `Allowed` write. -/ +theorem LocRec.wr {v : V} {k : Prog A V L} (I : LocRec s c e (.wr a v k) W) + (hmv : ∀ x, s.own (Function.update s.mem a v) x ≠ s.own s.mem x → + Held s.lk c (s.own s.mem x) ∧ Held s.lk c (s.own (Function.update s.mem a v) x)) : + LocRec { s with mem := Function.update s.mem a v, + ph := Function.update s.ph c (.loc p0 k (insert a W)) } c e k (insert a W) := by + refine ⟨fun ν hν hρ m' hr => I.run ν hν hρ m' (.wr ?_), fun ν hν hρ => ?_, I.adm, + fun x => (perm_wr_own hmv x).trans (I.perm x), I.ko, I.cls, fun x hx hpx => ?_, fun x hx hk => ?_⟩ + · change RunD k (mix (insert a W) (Function.update s.mem a v) ν) m' at hr + rw [mix_upd]; exact hr + · have h := onceD_wr.mp (I.once ν hν hρ) + rw [mix_upd] at h; exact h + · have hxa : x ≠ a := fun h => hx (by rw [h]; exact Set.mem_insert a W) + have hxW : x ∉ W := fun h => hx (Set.mem_insert_of_mem a h) + change Function.update s.mem a v x = _ + rw [Function.update_of_ne hxa]; exact I.stab x hxW ((perm_wr_own hmv x).mp hpx) + · have hxa : x ≠ a := fun h => hx (by rw [h]; exact Set.mem_insert a W) + have hxW : x ∉ W := fun h => hx (Set.mem_insert_of_mem a h) + change s.cls x (Function.update s.mem a v x) = _ + rw [Function.update_of_ne hxa]; exact I.clsM x hxW hk + +/-- The record a `start` step opens holds. -/ +theorem LocRec.start {k : Prog A V L} (hW : ∀ x ∈ W, Perm s c x) (hO : ∀ ν, AdmR s c ν → OnceD ∅ k ν) : + LocRec { s with ph := Function.update s.ph c (.loc p0 k W) } c (startR s k) k W := by + have hmix : ∀ ν, AdmR s c ν → mix W s.mem ν = ν := fun ν hν => funext fun x => by + by_cases hx : x ∈ W + · rw [mix_mem hx, hν.1 x (hW x hx)] + · rw [mix_nmem hx] + refine ⟨fun ν hν _ m' hr => ?_, fun ν hν _ => ?_, ⟨fun _ _ => rfl, fun _ _ => rfl⟩, fun _ => Iff.rfl, + rfl, rfl, fun _ _ _ => rfl, fun _ _ _ => rfl⟩ + · change RunD k (mix W s.mem ν) m' at hr + rw [hmix ν hν] at hr; exact hr + · change OnceD ∅ k (mix W s.mem ν) + rw [hmix ν hν]; exact hO ν hν + +/-- **Another core's step** keeps `d`'s record (a serial step cannot happen while `d`'s path is in progress). -/ +theorem LocRec.other {m0 H : A → V} (I : InvH m0 s H) (hal : Allowed s c) (h : Step s c s') + (hdc : d ≠ c) (hd : s.ph d = .loc p0 q W) (R : LocRec s d e q W) : + s'.ph d = .loc p0 q W ∧ LocRec s' d e q W := by + have hU : ∀ X : Phase A V L, Function.update s.ph c X d = .loc p0 q W := fun X => by + rw [Function.update_of_ne hdc]; exact hd + have tr0 : ∀ s'' : Cfg A V C L, s''.mem = s.mem → s''.lk = s.lk → s''.own = s.own → + s''.ko = s.ko → s''.cls = s.cls → LocRec s'' d e q W := by + intro s'' h1 h2 h3 h4 h5 + refine R.frame (fun x => ?_) h4 h5 (fun x hx => absurd (by rw [h1]) hx) + unfold Perm; rw [h1, h2, h3] + cases h with + | arm _ _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | enter _ _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | back _ _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | rd _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | rds _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | rdc _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | start _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | take _ _ _ => exact ⟨hU _, tr0 _ rfl rfl rfl rfl rfl⟩ + | scan _ _ _ _ => exact ⟨hd, tr0 _ rfl rfl rfl rfl rfl⟩ + | drain _ _ _ => exact ⟨hd, tr0 _ rfl rfl rfl rfl rfl⟩ + | serial _ _ _ _ _ hg _ _ => exact absurd (by rw [hd]; trivial) (I.clo hg d) + | lock b _ hb => + exact ⟨hU _, R.frame (fun x => or_congr (held_lock_iff hdc hb _) Iff.rfl) rfl rfl + (fun x hx => absurd rfl hx)⟩ + | leave _ => + exact ⟨hU _, R.frame (fun x => or_congr (held_release_iff hdc _) Iff.rfl) rfl rfl + (fun x hx => absurd rfl hx)⟩ + | @wr p1 a v k W1 hc => + simp only [Allowed, hc] at hal + obtain ⟨hb, hmv, hcl⟩ := hal + refine ⟨hU _, R.frame (fun x => perm_wr_other hdc hmv x) rfl rfl (fun x hx => ?_)⟩ + have hxa : x = a := by + by_contra hne; exact hx (Function.update_of_ne hne _ _) + subst hxa + refine ⟨fun hp => held_disj (Ne.symm hdc) hb hp, + fun hxW => held_disj (Ne.symm hdc) hb (Or.inl ((I.loc d p0 q W hd).held x hxW)), fun hk => ?_⟩ + change s.cls x (Function.update s.mem x v x) = _ + rw [Function.update_self] + rcases hcl with h1 | h1 | h1 + · exact h1 + · exfalso + rcases hk with h2 | h2 <;> rw [h1] at h2 + · cases h2 + · exact hdc (ClsOwner.core.inj h2).symm + · exfalso + rcases hk with h2 | h2 <;> rw [h1] at h2 <;> cases h2 + +/-- **A hardware write** keeps every record (Risk 3: module docstring). -/ +theorem LocRec.hw {m0 H : A → V} (R : LocRec s d e q W) (I : InvH m0 s H) {a : A} {x : V} {l : L} + (h : HwWr s a x l) (hd : s.ph d = .loc p0 q W) : LocRec (hwCfg s a x) d e q W := by + refine R.frame (fun y => ?_) rfl rfl (fun y hy => ?_) + · change (Held s.lk d (s.own (Function.update s.mem a x) y) ∨ + s.own (Function.update s.mem a x) y = .gate) ↔ Perm s d y + rw [h.keep]; exact Iff.rfl + · have hya : y = a := by + by_contra hne; exact hy (Function.update_of_ne hne _ _) + subst hya + refine ⟨fun hp => h.ne_of_perm hp rfl, + fun hyW => h.ne_of_held ((I.loc d p0 q W hd).held y hyW) rfl, fun hk => ?_⟩ + rw [h.ko] at hk; rcases hk with hk | hk <;> cases hk + +end rec + +/-! ## The invariant -/ + +theorem invR_init {s0 : Cfg A V C L} (h0 : Init s0) : InvR s0.mem s0 s0.mem (fun _ => none) := + ⟨inv_init h0, fun _ _ h => by cases h⟩ + +/-- **Every `Allowed` step keeps `InvR`** (module docstring), the history as `InvH.step`'s. -/ +theorem InvR.step {m0 H : A → V} {s s' : Cfg A V C L} {R : C → Option (RecD A V C L)} {c : C} + (I : InvR m0 s H R) (hal : Allowed s c) (h : Step s c s') (hS : StartOnce s c) : + ∃ H' R', InvR m0 s' H' R' ∧ (s'.log = s.log → H' = H) ∧ + (∀ p0 W, s.ph c = .loc p0 .done W → s'.log ≠ s.log → H' = mix W s.mem H) ∧ + (∀ d, d ≠ c → R' d = R d) ∧ RecFrom s c R (R' c) := by + obtain ⟨IH, IR⟩ := I + have fin : ∀ (H' : A → V) (o : Option (RecD A V C L)), InvH m0 s' H' → + (∀ e', o = some e' → ∃ p0 q W, s'.ph c = .loc p0 q W ∧ LocRec s' c e' q W) → + (s'.log = s.log → H' = H) → + (∀ p0 W, s.ph c = .loc p0 .done W → s'.log ≠ s.log → H' = mix W s.mem H) → RecFrom s c R o → + ∃ H' R', InvR m0 s' H' R' ∧ (s'.log = s.log → H' = H) ∧ + (∀ p0 W, s.ph c = .loc p0 .done W → s'.log ≠ s.log → H' = mix W s.mem H) ∧ + (∀ d, d ≠ c → R' d = R d) ∧ RecFrom s c R (R' c) := by + intro H' o IH' ho h1 h2 h3 + refine ⟨H', Function.update R c o, ⟨IH', fun d e' hd => ?_⟩, h1, h2, + fun d hdc => Function.update_of_ne hdc _ _, by rw [Function.update_self]; exact h3⟩ + by_cases hdc : d = c + · rw [hdc, Function.update_self] at hd; rw [hdc]; exact ho e' hd + · rw [Function.update_of_ne hdc] at hd + obtain ⟨p0, q, W, hph, hr⟩ := IR d e' hd + obtain ⟨h1', h2'⟩ := LocRec.other IH hal h hdc hph hr + exact ⟨p0, q, W, h1', h2'⟩ + have nk : ∀ e', (none : Option (RecD A V C L)) = some e' → + ∃ p0 q W, s'.ph c = .loc p0 q W ∧ LocRec s' c e' q W := fun _ h => by cases h + have nf : RecFrom s c R none := fun _ h => by cases h + cases h with + | arm p hc => + exact fin H none (inv_phase IH (Phase.arm_not_loc p) (by rw [hc]; exact Phase.idle_not_loc)) nk + (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | enter hc hg => + exact fin H none (inv_enter IH hc hg) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | back hc _ => + exact fin H none (inv_phase IH Phase.idle_not_loc (by rw [hc]; exact Phase.arm_not_loc _)) nk + (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | @rd p0 a k W hc => + simp only [Allowed, hc] at hal + refine fin H (R c) (inv_rd IH hc hal) (fun e' he => ?_) (fun _ => rfl) + (fun _ _ _ hne => (hne rfl).elim) (fun e' he => Or.inl ⟨e', he, rfl, rfl⟩) + obtain ⟨p0', q', W', hph, hr⟩ := IR c e' he + rw [hc] at hph; cases hph + exact ⟨p0, k (s.mem a), W, Function.update_self .., hr.rd hal⟩ + | @rds p0 a k W hc => + have hW : ∀ x ∈ W, Perm s c x := fun x hx => Or.inl ((IH.loc c p0 _ W hc).held x hx) + cases hRc : R c with + | none => exact fin H none (inv_rds IH hc) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := IR c e hRc + rw [hc] at hph; cases hph + refine fin H (some (racyR s c e a)) (inv_rds IH hc) (fun e' he => ?_) (fun _ => rfl) + (fun _ _ _ hne => (hne rfl).elim) (fun e' he => Or.inl ⟨e, hRc, ?_, ?_⟩) + · cases he; exact ⟨p0, k (s.mem a), W, Function.update_self .., hr.rds hW⟩ + · cases he; rfl + · cases he; rfl + | @rdc p0 a K k W hc => + simp only [Allowed, hc] at hal + have hW : ∀ x ∈ W, Perm s c x := fun x hx => Or.inl ((IH.loc c p0 _ W hc).held x hx) + cases hRc : R c with + | none => + exact fin H none (inv_rdc IH hc hal.1 hal.2) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := IR c e hRc + rw [hc] at hph; cases hph + refine fin H (some (racyR s c e a)) (inv_rdc IH hc hal.1 hal.2) (fun e' he => ?_) (fun _ => rfl) + (fun _ _ _ hne => (hne rfl).elim) (fun e' he => Or.inl ⟨e, hRc, ?_, ?_⟩) + · cases he; exact ⟨p0, k (s.mem a), W, Function.update_self .., hr.rdc hW⟩ + · cases he; rfl + · cases he; rfl + | @start p0 k W hc => + have hW : ∀ x ∈ W, Perm s c x := fun x hx => Or.inl ((IH.loc c p0 _ W hc).held x hx) + exact fin H (some (startR s k)) (inv_start IH hc) + (fun e' he => by cases he; exact ⟨p0, k, W, Function.update_self .., LocRec.start hW (hS p0 k W hc)⟩) + (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) + (fun e' he => by cases he; exact Or.inr ⟨p0, W, hc, rfl⟩) + | @wr p0 a v k W hc => + simp only [Allowed, hc] at hal + obtain ⟨hb, hmv, hcl⟩ := hal + refine fin H (R c) (inv_wr IH hc hb hmv hcl) (fun e' he => ?_) (fun _ => rfl) + (fun _ _ _ hne => (hne rfl).elim) (fun e' he => Or.inl ⟨e', he, rfl, rfl⟩) + obtain ⟨p0', q', W', hph, hr⟩ := IR c e' he + rw [hc] at hph; cases hph + exact ⟨p0, k, insert a W, Function.update_self .., hr.wr hmv⟩ + | lock b hc hb => + exact fin H none (inv_lock IH hc b hb) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | @leave p0 W hc => + exact fin (mix W s.mem H) none (inv_leave IH hc) nk + (fun hl => absurd (congrArg List.length hl) (by simp)) + (fun p0' W' h' _ => by rw [hc] at h'; cases h'; rfl) nf + | take p hc _ => + exact fin H none (inv_take IH p hc) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | scan e _ _ hd => + exact fin H none (inv_scan IH e hd) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | drain _ hg hall => + exact fin H none (inv_drain IH hg hall) nk (fun _ => rfl) (fun _ _ _ hne => (hne rfl).elim) nf + | serial keep own' cls' ko' _ hg hc hr => + exact fin _ none (inv_serial IH keep own' cls' ko' hg hr) nk + (fun hl => absurd (congrArg List.length hl) (by simp)) + (fun p0' W' h' _ => by rw [hc] at h'; cases h') nf + +/-- **A hardware write keeps `InvR`**, the history taking the same write (Risk 3: module docstring). -/ +theorem InvR.hw {m0 H : A → V} {s : Cfg A V C L} {R : C → Option (RecD A V C L)} (I : InvR m0 s H R) + {a : A} {x : V} {l : L} (h : HwWr s a x l) : InvR m0 (hwCfg s a x) (Function.update H a x) R := by + refine ⟨inv_hw I.inv h, fun d e hd => ?_⟩ + obtain ⟨p0, q, W, hph, hr⟩ := I.recs d e hd + exact ⟨p0, q, W, hph, hr.hw I.inv h hph⟩ + +/-- **At `leave`, the body is one deterministic run** from the admissible read memory `ρ`, ending in what the path +left on its written words. -/ +theorem InvR.leave_run {m0 H : A → V} {s : Cfg A V C L} {R : C → Option (RecD A V C L)} {c : C} + {p0 : Prog A V L} {W : Set A} {e : RecD A V C L} (I : InvR m0 s H R) (hc : s.ph c = .loc p0 .done W) + (hR : R c = some e) : RunD e.b e.ρ (mix W s.mem e.ρ) ∧ AdmR e.s0 c e.ρ := by + obtain ⟨p0', q, W', hph, hr⟩ := I.recs c e hR + rw [hc] at hph; cases hph + exact ⟨hr.run e.ρ hr.adm (fun _ _ => rfl) _ (.done _), hr.adm⟩ + +end + +end Fl4Proofs.P2.Serial diff --git a/proofs/Fl4P2Base/SerialW.lean b/proofs/Fl4P2Base/SerialW.lean new file mode 100644 index 000000000..584eeed43 --- /dev/null +++ b/proofs/Fl4P2Base/SerialW.lean @@ -0,0 +1,139 @@ +/- +# P2 (sync, gate gen 1): **the weak racy-read step** `StepR` beside `Serial.Step` (DESIGN §9 item 4, Half 2) + +`Serial.Step.rds` reads the *current* memory: a speculative read (`Prog.rds`) of core `c` returns `s.mem a`. On +hardware such a read is racy (another core may be writing the word beside the gate, under a lock `c` does not hold, +or the holder of the word's owner may have written it without any ordering towards `c`), and it may return an older +value of the word's coherence order. `fl4-store`'s `PhysStore::read` is a `read_volatile` of an aligned 64-bit word +(`crates/fl4-store/src/lib.rs:31–34`). + +This module adds, **without changing `Serial.lean`**, the weak step relation + +* `StepR U s c s'`: a `Serial.Step`, or `rdsW v`, a speculative read that returns an **arbitrary** value `v` + (`s.ph c = .loc p0 (.rds a k) W` moves to `.loc p0 (k v) W`; nothing else changes), on a **racy** word only + (gate gen 3, the Architect's ruling of 19:10Z): one core `c` may not read (`¬ Perm s c a`) or an untracked word + (`U a`: user memory for FL4, which user threads write beside the kernel), whose value class neither the gate nor + `c` owns (`s.ko a ≠ .gate`, `s.ko a ≠ .core c`). A non-racy speculative read is `Step.rds` (the current value, + **[P] MM-DRF**). +* Gate gen 4 (the Architect's ruling of 19:58Z, gap 2): `rdsC v`, a speculative read of a racy word whose class the + gate or `c` owns (`s.ko a = .gate` or `.core c`), returning any `v` of the current value's class + (`s.cls a v = s.cls a (s.mem a)`): **[P] MM-RACY** gives some value of the word's coherence order, and every write + to the word in the epoch keeps its class (the class owners are the gate and `c`, `Allowed`'s `wr` clause). fl4-sync's + glue reads such words speculatively (the CNode's `CAP0`, `ko = .gate`; the current thread's `STATE`, + `ko = .core c`) before `.start`; `Glue.rds` takes every value, and `SafeW`'s read set `RdW` admits these values + (`SafeW.lean`). + +Any value over-approximates "some value of the word's coherence order", which is what the named premise below +gives; nothing about which value is needed, because `Run.rds` already lets a speculative read return any value in +the sequential meaning. That executions of these steps stay `Allowed` under the path obligations is `SafeW.lean`'s +(`SafeW`, the weak safety of a started body). + +* `inv_rdsW`: `InvH` is kept by `rdsW`, for every value (the twin of `inv_rds`, whose proof never used the value). +* `InvH.stepR`, `GReachR` (executions of `StepR` whose steps are `Allowed`), `greachR_inv`, and the three main + theorems for them: `no_overlapR`, `serializableR`, `serializable_quiescentR`. `GReach.toR` embeds the strong + executions. + +## Named premise (used by `GateImpl.lean`'s `IStepG.rds`, not by any theorem here) + +* **[P] MM-RACY**: a racy aligned 64-bit `read_volatile` of kernel memory (`PhysStore::read`, + `crates/fl4-store/src/lib.rs:31–34`) returns some value of that word's coherence order: it does not tear (an + aligned 8-byte single-copy-atomic access on x86_64 and AArch64) and does not return a value out of thin air. + Racing volatile accesses are undefined behaviour in the Rust abstract machine, so this is a trusted compiler and + hardware fact. `StepR.rdsW` lets the value be anything, so only "the read is one event with some 64-bit value" + is used. + +No [J], no [W]; nothing in `Serial.lean` or any other existing file changed. Gate gen 3 changed `StepR` (the +`U` parameter and `rdsW`'s racy-word condition) and `GReachR` (a `U` per step), with `InvH.stepR`; gate gen 4 added +the constructor `rdsC`. +-/ +import Fl4P2Base.Serial + +namespace Fl4Proofs.P2.Serial + +noncomputable section + +variable {A V C L : Type} [DecidableEq A] [DecidableEq C] [DecidableEq L] + +/-- One step of core `c` where a speculative read of a racy word may return any value (**[P] MM-RACY**: a racy +aligned word read returns some value of the word's coherence order; any value over-approximates that). `U`: the +untracked words (user memory for FL4; `fun _ => False` if none). -/ +inductive StepR (U : A → Prop) (s : Cfg A V C L) (c : C) : Cfg A V C L → Prop + /-- A step of `Serial.Step`. -/ + | step {s' : Cfg A V C L} : Step s c s' → StepR U s c s' + /-- A speculative read of a racy word returning `v`, which need not be `s.mem a`. -/ + | rdsW {p0 : Prog A V L} {a : A} {k : V → Prog A V L} {W : Set A} (v : V) : + s.ph c = .loc p0 (.rds a k) W → (¬ Perm s c a ∨ U a) → s.ko a ≠ .gate → s.ko a ≠ .core c → + StepR U s c { s with ph := Function.update s.ph c (.loc p0 (k v) W) } + /-- A speculative read of a racy word whose class the gate or `c` owns, returning a value of the current value's + class (gate gen 4). -/ + | rdsC {p0 : Prog A V L} {a : A} {k : V → Prog A V L} {W : Set A} (v : V) : + s.ph c = .loc p0 (.rds a k) W → (¬ Perm s c a ∨ U a) → (s.ko a = .gate ∨ s.ko a = .core c) → + s.cls a v = s.cls a (s.mem a) → + StepR U s c { s with ph := Function.update s.ph c (.loc p0 (k v) W) } + +/-- `inv_rds` for a value that need not be memory's: a speculative read keeps `InvH` whatever it returns. -/ +theorem inv_rdsW {m0 : A → V} {s : Cfg A V C L} {H : A → V} (I : InvH m0 s H) {c : C} + {p0 : Prog A V L} {a : A} {k : V → Prog A V L} {W : Set A} (v : V) + (hc : s.ph c = .loc p0 (.rds a k) W) : + InvH m0 { s with ph := Function.update s.ph c (.loc p0 (k v) W) } H := by + have Ic := I.loc c p0 _ W hc + refine I.locstep (p' := k v) (W' := W) hc rfl rfl rfl rfl (subset_refl _) + (fun _ _ => rfl) (fun _ _ _ h => h) (fun _ _ _ h => h) (fun _ _ _ _ _ _ _ _ => rfl) + rfl rfl (I.clsW hc) ?_ + exact { held := Ic.held, sim := fun n n' hn hcn hr => Ic.sim n n' hn hcn (Run.rds v hr) } + +/-- `InvH.step` for the weak steps. -/ +theorem InvH.stepR {m0 : A → V} {s s' : Cfg A V C L} {H : A → V} {c : C} {U : A → Prop} + (I : InvH m0 s H) (hal : Allowed s c) (h : StepR U s c s') : + ∃ H', InvH m0 s' H' := by + cases h with + | step h => exact I.step hal h + | rdsW v hc _ _ _ => exact ⟨H, inv_rdsW I v hc⟩ + | rdsC v hc _ _ _ => exact ⟨H, inv_rdsW I v hc⟩ + +/-- Executions of weak steps whose local-path accesses respect ownership (the untracked words `U` of each step its +own: for FL4 the layout's user memory, which serial entries may change). -/ +inductive GReachR (s0 : Cfg A V C L) : Cfg A V C L → Prop + | refl : GReachR s0 s0 + | step {s s' : Cfg A V C L} {c : C} {U : A → Prop} : + GReachR s0 s → Allowed s c → StepR U s c s' → GReachR s0 s' + +/-- Every strong execution is a weak one. -/ +theorem GReach.toR {s0 s : Cfg A V C L} (h : GReach s0 s) : GReachR s0 s := by + induction h with + | refl => exact .refl + | step _ hal hs ih => exact .step ih hal (.step (U := fun _ => False) hs) + +theorem greachR_inv {s0 s : Cfg A V C L} (h0 : Init s0) (h : GReachR s0 s) : + ∃ H, InvH s0.mem s H := by + induction h with + | refl => exact ⟨_, inv_init h0⟩ + | step _ hal hs ih => + obtain ⟨H, I⟩ := ih + exact I.stepR hal hs + +/-- `no_overlap` for executions whose speculative reads may return stale values. -/ +theorem no_overlapR {s0 s : Cfg A V C L} (h0 : Init s0) (h : GReachR s0 s) : + s.gate = .closed → NoLoc s := by + obtain ⟨H, I⟩ := greachR_inv h0 h + exact I.clo + +/-- `serializable` for executions whose speculative reads may return stale values: the serial order's `Run` lets a +speculative read return any value, so nothing changes. -/ +theorem serializableR {s0 s : Cfg A V C L} (h0 : Init s0) (h : GReachR s0 s) : + ∃ H, RunAll s.log s0.mem H ∧ + ∀ x, (∀ d p0 p W, s.ph d = .loc p0 p W → x ∉ W) → s.mem x = H x := by + obtain ⟨H, I⟩ := greachR_inv h0 h + exact ⟨H, I.hist, I.agree⟩ + +/-- `serializable_quiescent` for executions whose speculative reads may return stale values. -/ +theorem serializable_quiescentR {s0 s : Cfg A V C L} (h0 : Init s0) (h : GReachR s0 s) + (hq : NoLoc s) : RunAll s.log s0.mem s.mem := by + obtain ⟨H, I⟩ := greachR_inv h0 h + have : s.mem = H := funext fun x => + I.agree x (fun d q0 q U hd => absurd (by rw [hd]; trivial) (hq d)) + rw [this]; exact I.hist + +end + +end Fl4Proofs.P2.Serial diff --git a/proofs/Fl4Proofs.lean b/proofs/Fl4Proofs.lean index f98cc2ecd..c164ed5ea 100644 --- a/proofs/Fl4Proofs.lean +++ b/proofs/Fl4Proofs.lean @@ -2925,3 +2925,7 @@ import Fl4Proofs.P2.OwnXfer import Fl4Proofs.P2.OwnXferPm import Fl4Proofs.P2.V15PlatW import Fl4Proofs.P2.BootV15 +import Fl4P2Base.SerialW +import Fl4P2Base.GateImpl +import Fl4P2Base.GateLock +import Fl4P2Base.SafeW diff --git a/proofs/Fl4ProofsA64.lean b/proofs/Fl4ProofsA64.lean index a3292f2c3..e76240d57 100644 --- a/proofs/Fl4ProofsA64.lean +++ b/proofs/Fl4ProofsA64.lean @@ -968,9 +968,22 @@ import Fl4ProofsA64.P2.E2EPStartA import Fl4ProofsA64.P2.BootVsL import Fl4ProofsA64.P2.BootEpP import Fl4ProofsA64.P2.BootStartP +import Fl4ProofsA64.P2.MdbKeepA +import Fl4ProofsA64.P2.MdbKeepPmA +import Fl4ProofsA64.P2.MdbKeepDelA import Fl4ProofsA64.P1.BootSysL0 import Fl4ProofsA64.P1.BootMdb1 import Fl4ProofsA64.P1.BootSysL1 import Fl4ProofsA64.P1.BootScAp import Fl4ProofsA64.P2.BootSysA import Fl4ProofsA64.P1.BootW1Win +import Fl4ProofsA64.P2.GlueAdeqA +import Fl4ProofsA64.P2.GlueRepA +import Fl4ProofsA64.P2.MdbEditA +import Fl4ProofsA64.P2.MdbWireA +import Fl4ProofsA64.P2.MdbWirePDA +import Fl4ProofsA64.P2.MdbBootP +import Fl4ProofsA64.P2.BufWireA +import Fl4ProofsA64.P2.DelLkA +import Fl4ProofsA64.P2.GateExecA +import Fl4ProofsA64.P2.GateBootA diff --git a/proofs/Fl4ProofsA64/P2/BufWireA.lean b/proofs/Fl4ProofsA64/P2/BufWireA.lean new file mode 100644 index 000000000..763f7336f --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/BufWireA.lean @@ -0,0 +1,291 @@ +/- +# P2 (sync, gen 5): **the IPC-buffer placement `BufU` along an AArch64 execution** (DESIGN §9 item 4, Half 1 (e)) + +The PageMap and Delete-with-PTE starts (`pmSlotP_startAt`, `delSlotP_startAt`, `P2/E2EPStartA.lean`) take the [J] +start fact `BufU` (`P2/E2EPTopA.lean`): every thread's IPC buffer, as `lookup_ipc_buffer` finds it, lies in the user +words. `lookup_ipc_buffer` of a thread `t` reads three words (P1's `IbSame`): the two cap words of `t`'s buffer cap +slot (TCB offsets 64 and 72) and `t`'s `IPC_BUFFER` word (3512), the words **`BufWd`**. Under `ownP` all of them are +the gate's: the buffer cap slot is a TCB slot (rule 2: the cap words of a TCB slot are the gate's), and `IPC_BUFFER` +is a `GateField` (**`ownP_bufWd`**). Serial's `Allowed` lets a local path write only a word whose owner it holds, and a +hardware write is to a `lock` word (`HwWr.own`). So none of the eleven local paths of `fl4LocPB`, and no hardware +step, writes a `BufWd` word: only serial entries change them. This is the P2-side framing of `bufU` across all the +local paths, at the level of Serial's steps (no per-path lemma is needed). + +* **`BufSH st K m`**: some state of the store has `m`'s `BufWd` words and `BufU` at `K`. +* **`bufSH_step`** (a step that is no serial entry keeps `BufSH` of the memory), **`bufSH_hw`** (a hardware step), + **`bufSH_exec`** (induction over `ExecPA`; serial entries by the hypothesis `hSerB`). +* **`bufU_of_sh`**: `BufSH` of a memory gives `BufU` at every state agreeing with it on the aligned kernel words, + so at every start of a slot (**`bufU_start`**). +* **`serializable_fl4_localVP_buf`**: `bufSH_exec` for `fl4LocPB`. **`pmSlotP_startAtB`** / **`delSlotP_startAtB`**: + the two PTE starts in an execution, their `BufU` from `BufSH` of the memory. +* Layout helpers: `EpochA.vmLay` (the epoch's `VmLayA` at its user set), **`ownP_of_slot`**, **`ownP_of_tcb`** + (`ownP` at a slot or non-guest TCB word is `own`'s owner), `bufWd_reg`, `bufWd_nu`. + +Hypothesis `hSerB` of `bufSH_exec` / `serializable_fl4_localVP_buf` (serial entries keep `BufSH`): the `bufU` half +of the ruled serial-entry hypothesis `hSer` (Architect 16:45Z / 18:13Z: serial-kernel P1 / S1A work); the S1A / Boot +witness of `BufSH` at the start is those lines'. Otherwise no premise: no [J], [P] or [W]. New module; no existing +statement changes. +-/ +import Fl4ProofsA64.P2.MdbWirePDA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl +open Fl4ProofsA64.P2.OwnMap (KLayout region own InW TcbSlot slotOwner tcbOwner GateField) +open Fl4ProofsA64.P2.OwnMapV (KLayoutV GuestOff FpOff) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP ownP ownP_off) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA MapsPA GateEpPA Fl4StepPA HwStepPA ExecPA StartAtEP exec_safeRunPA) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv Held hwCfg xsafeRun_greach allowed_of_sinv) +open Fl4ProofsA64.P2.E2EP (PmQF PmEx PmRoot) + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section +open Classical + +/-! ## Layout -/ + +section lay +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The epoch's `VmLayA`** at its user set (`vxLay_Ub`). -/ +theorem EpochA.vmLay {K : KLayoutV} {e : U64 → U64} (h : EpochA st K e) : VmLayA K.toKLayout := by + obtain ⟨σ, H, Cs, OS, U, Ac, Pp, Tb, Vc, Ub, Lv, n, m, rfl, E, P, W, hcs, hn, -, -⟩ := h + exact (S1b.vxLay_Ub E P W hcs hn m).base + +theorem EpochAP.vmLay {K : KLayoutP} {e : U64 → U64} (h : EpochAP st K e) : VmLayA K.toKLayout := + EpochA.vmLay h.1 + +end lay + +section own +variable {K : KLayoutP} + +/-- **`ownP` at a slot word** is `own`'s slot owner (a slot word is no PTE word, no VCPU word, no TCB word). -/ +theorem ownP_of_slot (hK : RegionOKP K) {m : U64 → U64} {b q : U64} (hr : region K.toKLayout b = .slot q) : + ownP K m b = slotOwner K.toKLayout m q (b.val - q.val) := by + have hnp : b ∉ K.ptes := fun h => by + have h' := hK.none _ h + rw [hr] at h' + cases h' + rw [ownP_off hnp] + unfold OwnMapV.ownV + rw [dif_neg fun ⟨v, hv⟩ => by have h' := hK.base.none v b hv; rw [hr] at h'; cases h'] + unfold OwnMapV.ownT + simp only [hr, own] + +/-- **`ownP` at a TCB word off the rule-8 offsets** is `own`'s TCB owner. -/ +theorem ownP_of_tcb (hK : RegionOKP K) {m : U64 → U64} {b t : U64} (hr : region K.toKLayout b = .tcb t) + (hg : ¬ GuestOff (b.val - t.val)) : ownP K m b = tcbOwner m t (b.val - t.val) := by + have hnp : b ∉ K.ptes := fun h => by + have h' := hK.none _ h + rw [hr] at h' + cases h' + rw [ownP_off hnp] + unfold OwnMapV.ownV + rw [dif_neg (OwnMapV.not_vcpu_of_tcb hK.base hr), OwnMapV.ownT_tcb hr, if_neg fun h => hg h.2] + simp only [own, hr] + +end own + +/-! ## The words `lookup_ipc_buffer` reads -/ + +section buf +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The `lookup_ipc_buffer` words of the layout's threads** (module docstring): of each TCB `t`, its buffer cap +slot's two cap words (`t + 64`, `t + 72`) and its `IPC_BUFFER` word (`t + 3512`). -/ +def BufWd (K : KLayout) (b : U64) : Prop := + ∃ t, K.heap t = some .tcb ∧ (b = wa t 64#u64 ∨ b = wa t 72#u64 ∨ b = wa t 3512#u64) + +variable (st) in +/-- **`BufU` of a word memory** (module docstring): a state of the store with `m`'s `BufWd` words has `BufU`. -/ +def BufSH (K : KLayout) (m : U64 → U64) : Prop := + ∃ v : S, (∀ b, BufWd K b → L.mem v b = m b) ∧ BufU st K.heap.tcbs K.user v + +/-- **`BufU` at a state with the memory's `BufWd` words** (`lookup_ipc_buffer_same`). -/ +theorem bufU_of_sh {K : KLayout} {m : U64 → U64} (h : BufSH st K m) {σ : S} (hσ : ∀ b, BufWd K b → L.mem σ b = m b) : + BufU st K.heap.tcbs K.user σ := by + obtain ⟨v, hv, hB⟩ := h + intro t ht + have ag : ∀ b, BufWd K b → L.mem σ b = L.mem v b := fun b hb => (hσ b hb).trans (hv b hb).symm + have e : msg.lookup_ipc_buffer st σ true t = msg.lookup_ipc_buffer st v true t := + lookup_ipc_buffer_same (st := st) + ⟨ag _ ⟨t, ht, Or.inr (Or.inr rfl)⟩, by rw [sbWordA_eq]; exact ag _ ⟨t, ht, Or.inl rfl⟩, + by rw [sbWordA8_eq]; exact ag _ ⟨t, ht, Or.inr (Or.inl rfl)⟩⟩ + rw [e] + exact hB t ht + +/-- **Where a `BufWd` word lies**: aligned, and either the thread's `IPC_BUFFER` (TCB offset 3512) or a cap word +(offset below 16) of the thread's buffer cap slot, a TCB slot. -/ +theorem bufWd_reg {K : KLayout} (V : VmLayA K) {b : U64} (hb : BufWd K b) : + Aligned b ∧ ((∃ t, region K b = .tcb t ∧ b.val - t.val = 3512) ∨ + (∃ q, region K b = .slot q ∧ TcbSlot K q ∧ b.val - q.val < 16)) := by + obtain ⟨t, ht, hb⟩ := hb + have hT := V.tcbA t ht + have hq := V.tslot t ht layout.tcb.SLOT_BUFFER (sbWordA t) (by simp [obj.Tcb.slot, lift, sbWordA]) + (by simp [layout.tcb.SLOT_BUFFER]) + have q64 : (sbWordA t).val = t.val + 64 := by rw [sbWordA_eq, field_val hT (K := 64#u64) (by simp)]; simp + rcases hb with rfl | rfl | rfl + · have v : (wa t 64#u64).val = t.val + 64 := by rw [field_val hT (K := 64#u64) (by simp)]; simp + refine ⟨field_aligned hT (by simp) (by simp), Or.inr ⟨sbWordA t, ?_, hq.2, ?_⟩⟩ + · exact V.slotw _ hq.1 _ ⟨by omega, by omega⟩ + · omega + · have v : (wa t 72#u64).val = t.val + 72 := by rw [field_val hT (K := 72#u64) (by simp)]; simp + refine ⟨field_aligned hT (by simp) (by simp), Or.inr ⟨sbWordA t, ?_, hq.2, ?_⟩⟩ + · exact V.slotw _ hq.1 _ ⟨by omega, by omega⟩ + · omega + · have v : (wa t 3512#u64).val = t.val + 3512 := by rw [field_val hT (K := 3512#u64) (by simp)]; simp + exact ⟨field_aligned hT (by simp) (by simp), Or.inl ⟨t, V.tcbw t ht 3512#u64 (by simp) (by simp), by omega⟩⟩ + +/-- A `BufWd` word is no user word. -/ +theorem bufWd_nu {K : KLayout} (V : VmLayA K) {b : U64} (hb : BufWd K b) : b ∉ K.user := by + intro hu + have e := (V.user b hu).1 + rcases (bufWd_reg V hb).2 with ⟨t, hr, -⟩ | ⟨q, hr, -⟩ <;> rw [hr] at e <;> cases e + +/-- **Every `BufWd` word is the gate's under `ownP`** (rule 2 for the TCB slot's cap words, `GateField` for +`IPC_BUFFER`), whatever the memory. -/ +theorem ownP_bufWd {K : KLayoutP} (hK : RegionOKP K) (V : VmLayA K.toKLayout) {b : U64} (hb : BufWd K.toKLayout b) + (m : U64 → U64) : ownP K m b = .gate := by + rcases (bufWd_reg V hb).2 with ⟨t, hr, hd⟩ | ⟨q, hr, hT, hd⟩ + · rw [ownP_of_tcb hK hr (by unfold GuestOff FpOff; omega), hd] + unfold tcbOwner + rw [if_pos (by unfold GateField; omega)] + · rw [ownP_of_slot hK hr] + unfold slotOwner + rw [if_neg (by omega), if_pos hT] + +/-- `BufU` at a state agreeing with `m` on the aligned kernel words, from `BufSH` of `m`. -/ +theorem bufU_start {K : KLayout} (V : VmLayA K) {m : U64 → U64} (h : BufSH st K m) {σ : S} + (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = m x) : BufU st K.heap.tcbs K.user σ := + bufU_of_sh h fun b hb => hμ b (bufWd_reg V hb).1 (bufWd_nu V hb) + +end buf + +/-! ## Along an execution -/ + +section wire +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Q : KLayoutP → (U64 → U64) → Prop} {ι : Type} {LP : PathsPA ι} + +/-- **A core step that is no serial entry keeps `BufSH` of the memory**: a local write is to a word the core holds +(`Allowed`), never the gate's. -/ +theorem bufSH_step {K : KLayoutP} (hK : RegionOKP K) (V : VmLayA K.toKLayout) {s s' : Cfg U64 U64 U64 U64} + {c : U64} (hM : MapsPA K s) (hal : Fl4Proofs.P2.Serial.Allowed s c) (hst : Step s c s') + (hs : ¬ E2E.SerStep s c) (hB : BufSH st K.toKLayout s.mem) : BufSH st K.toKLayout s'.mem := by + cases hst with + | @wr p0 a v k W hc => + simp only [Fl4Proofs.P2.Serial.Allowed, hc] at hal + obtain ⟨hh, -, -⟩ := hal + obtain ⟨u, hu, hBu⟩ := hB + refine ⟨u, fun b hb => ?_, hBu⟩ + have hne : b ≠ a := by + rintro rfl + rw [hM.own, ownP_bufWd hK V hb] at hh + exact hh + show L.mem u b = Function.update s.mem a v b + rw [Function.update_of_ne hne, hu b hb] + | serial _ _ _ _ hh hg _ _ => exact absurd ⟨hg, hh⟩ hs + | arm => exact hB + | enter => exact hB + | back => exact hB + | rd => exact hB + | rds => exact hB + | rdc => exact hB + | start => exact hB + | lock => exact hB + | leave => exact hB + | take => exact hB + | scan => exact hB + | drain => exact hB + +/-- **A hardware step keeps `BufSH` of the memory**: its word is a `lock` word (`HwWr.own`). -/ +theorem bufSH_hw {K : KLayoutP} (hK : ROKP K) (V : VmLayA K.toKLayout) {s : Cfg U64 U64 U64 U64} {v a x : U64} + (hM : MapsPA K s) (h : HwStepPA K s v a x) (hB : BufSH st K.toKLayout s.mem) : + BufSH st K.toKLayout (hwCfg s a x).mem := by + have hw := h.hwWr hK hM + obtain ⟨u, hu, hBu⟩ := hB + refine ⟨u, fun b hb => ?_, hBu⟩ + have hne : b ≠ a := by + rintro rfl + have e := hw.own + rw [hM.own, ownP_bufWd hK.1 V hb] at e + cases e + show L.mem u b = Function.update s.mem a x b + rw [Function.update_of_ne hne, hu b hb] + +/-- **`BufSH` of the memory along an execution** (module docstring), given it at the start and `hSerB` (serial +entries keep it). -/ +theorem bufSH_exec (hV : ∀ K e, Q K e → VmLayA K.toKLayout) {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hK0 : ROKP K0) (hM0 : MapsPA K0 s0) (hQ0 : Q K0 s0.mem) + (hB0 : BufSH st K0.toKLayout s0.mem) + (hSerB : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, ExecPA Q LP s0 K0 s K → Step s c s' → + Fl4StepPA Q LP s K c s' K' → E2E.SerStep s c → BufSH st K.toKLayout s.mem → BufSH st K'.toKLayout s'.mem) + (h : ExecPA Q LP s0 K0 s K) : BufSH st K.toKLayout s.mem := by + induction h with + | refl => exact hB0 + | @step s s' K K' c hp hst hS ih => + by_cases hs : E2E.SerStep s c + · exact hSerB hp hst hS hs ih + · obtain ⟨hr, hK, hM, e, hq, -⟩ := exec_safeRunPA h0 hA0 hK0 hM0 hQ0 hp + obtain ⟨-, hSI, -⟩ := xsafeRun_greach h0 hA0 hr + have hKK := hS.keep hs + subst hKK + exact bufSH_step hK.1 (hV _ _ hq) hM (allowed_of_sinv hSI _) hst hs ih + | hw hp hh ih => + obtain ⟨-, hK, hM, e, hq, -⟩ := exec_safeRunPA h0 hA0 hK0 hM0 hQ0 hp + exact bufSH_hw hK (hV _ _ hq) hM hh ih + +end wire + +/-! ## `fl4LocPB` -/ + +section fl4 +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **`BufU` of the memory along an execution of `fl4LocPB`** (module docstring; `hSerB` as `bufSH_exec`). -/ +theorem serializable_fl4_localVP_buf {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (hBu0 : BufSH st K0.toKLayout s0.mem) + (hSerB : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, + ExecPA (EpochAP st) (fl4LocPB st ar hA hm) s0 K0 s K → Step s c s' → + Fl4StepPA (EpochAP st) (fl4LocPB st ar hA hm) s K c s' K' → E2E.SerStep s c → + BufSH st K.toKLayout s.mem → BufSH st K'.toKLayout s'.mem) + (h : ExecPA (EpochAP st) (fl4LocPB st ar hA hm) s0 K0 s K) : BufSH st K.toKLayout s.mem := + bufSH_exec (fun _ _ he => he.vmLay) h0 hA0 hB0.rok hM0 hB0 hBu0 hSerB h + +variable {st} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} {c : U64} {hs : U64 → Prop} {μ : U64 → U64} + +/-- **Starting the PageMap slot with `BufU` from `BufSH`** of the memory the start agrees with +(`pmSlotP_startAt` with `bufU_start`). -/ +theorem pmSlotP_startAtB {i : InvArgP S} (h : PmPremP st i K m) {e : U64 → U64} (he : EpochAP st K e) + (hBu : BufSH st K.toKLayout μ) (σ : S) (a : A) + (hJ : InvJA (L := L) K.toKLayout σ) (hQ : PmQF st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ) + (hub : CurUnbound K.vcpuOf (L.curW σ)) (hEx : PmEx st i.p σ) (hRt : PmRoot st i.p σ) + (hc : L.cpu σ = c) (hl : i.Locks σ = hs) (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = μ x) : + ∃ q, StartAtEP (EpochAP st) + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl i)))))))))) K m hK) + c hs μ q := + pmSlotP_startAt (ar := ar) (hA := hA) (hm := hm) (hK := hK) h σ a hJ hQ hub hEx hRt (bufU_start he.vmLay hBu hμ) hc hl hμ + +/-- **Starting the Delete-with-PTE slot with `BufU` from `BufSH`** (`delSlotP_startAt` with `bufU_start`). -/ +theorem delSlotP_startAtB {i : InvArgP S} (h : DelPremP st i K m) {e : U64 → U64} (he : EpochAP st K e) + (hBu : BufSH st K.toKLayout μ) (σ : S) (a : A) + (hJ : InvJA (L := L) K.toKLayout σ) (hQ : InvQFP st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K.toKLayout m σ) + (hub : CurUnbound K.vcpuOf (L.curW σ)) {G : List (List U64)} (hM : W7.R.ST.MdbLk st K.toKLayout.slots G σ) + (hN : W7.R.ST.NullLk st K.toKLayout.slots σ) + (hc : L.cpu σ = c) (hl : i.Locks σ = hs) (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = μ x) : + ∃ q, StartAtEP (EpochAP st) + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i)))))))))) K m hK) + c hs μ q := + delSlotP_startAt (ar := ar) (hA := hA) (hm := hm) (hK := hK) h σ a hJ hQ hub hM hN (bufU_start he.vmLay hBu hμ) hc hl hμ + +end fl4 + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/CutNbwA.lean b/proofs/Fl4ProofsA64/P2/CutNbwA.lean index 0987af475..58c8e8f68 100644 --- a/proofs/Fl4ProofsA64/P2/CutNbwA.lean +++ b/proofs/Fl4ProofsA64/P2/CutNbwA.lean @@ -87,8 +87,8 @@ theorem nbw_ioNarR (hVn : VcNone K Vc) (hR : RdCutA K Vc vcpuOf R) (P : IoPre st unfold RegOffA FpOffA; omega exact { nd := fun o h1 _ => ⟨rb_nvNodeR hR hVn B h1, rb_ncNode hVn B h1⟩ - cur := fun off h1 h2 => rb_nvTR hR hVn B (off := off) P.cT h1 h2 ct0 - curW := fun off h1 h2 _ => rb_ncT hVn B (off := off) P.cT h1 h2 ct0 + cur := fun off h1 h2 => rb_nvTR hR hVn B (off := off) P.cT (by omega) h2 ct0 + curW := fun off h1 h2 _ => rb_ncT hVn B (off := off) P.cT (by omega) h2 ct0 slot := fun sl h => nv_slotR hR hVn P.lay h ks := fun o ho h8 => rb_nvKsR hR B hO ho h8 csc := fun _ => rb_scCutR hR hVn B P.cscS diff --git a/proofs/Fl4ProofsA64/P2/DelLkA.lean b/proofs/Fl4ProofsA64/P2/DelLkA.lean new file mode 100644 index 000000000..54e60d106 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/DelLkA.lean @@ -0,0 +1,295 @@ +/- +# P2 (sync, gen 5): **the Delete-with-PTE start at the locked slots**, discharged from the replay history (DESIGN §9 +item 4, Half 1) + +`delSlotP_startAt` (`P2/E2EPStartA.lean`) takes P1's `MdbLk` / `NullLk` at **every** slot of the layout at the start +state σ ([J] start facts), from which the entry's `CdtAt` (`PrePA`'s CDT conjunct) follows. At a physical start +another core may be half-way through `cdt.empty_slot` or a PageMap on its own locked slots, so nothing gives them at +σ. What the path needs is less: `delEntP_of` reads `CdtAt` at the Delete target only, which the plan locks. This +module states the Delete-with-PTE slot at the parallel start fact `PrePAL` (`CdtAtL`: `CdtAt` at the slots the path +holds, `P2/InvDelPteAccPA.lean`; the path `inv_localPathPAL`, `P2/InvDelPteLPA.lean`; both new parallel records, +`PrePA` and `inv_localPathPA` unchanged) and discharges `CdtAtL` from the replay history: + +* **`delSlotPL`**: the slot (`DelPremP` unchanged), with `inv_localPathPAL` and `del_hufPAL`. +* **`delSlotPL_startAt`**: a start asserts `InvJA`, `InvQFP`, [P] `CurUnbound`, the [J] `bufU` and **`CdtAtL` at its + own locks** (in place of `MdbLk` / `NullLk` at every slot). +* **`cdtAtL_of_hist`**: `CdtAtL` at σ from `MdbSH` of a word memory `H` (P1's `MdbS` at some state with `H`'s slot + words, `cdtAt_of_mdb` there) and σ = `H` on the locked slots' `PREV` / `NEXT` words. +* **`ownP_slotLk`**: a slot's link words (offsets 16..31) are its lock's under `ownP` (rule 2). +* **`WireI.cdtAtL`**: at a step where core `c` starts a body (`.start k`), the wiring invariant (`P2/MdbWireA.lean`) + gives `CdtAtL` at every state σ agreeing with the memory on the aligned kernel words, at `c`'s locks: the locked + link words are `c`'s, so the history agrees with the memory there (`InvH.agree`: no other core wrote them; `c` + has written nothing yet, its glue has no record). +* **`serializable_cdtL_of`**: the same along an execution of any wiring family (`wire_exec`). +* **`fl4LocPBL`**: `fl4LocPB` with `delSlotPL` in the Delete-with-PTE place; **`delSlotPL_wire`** (its edit form, + `del_editL`), **`fl4LocPBL_wire`**, **`serializable_fl4_localVPL`** (`serializable_e2ePA`), + **`serializable_fl4_localVPL_mdb`** (`serializable_mdb_of`), and **`delSlotPL_startExec`**: in an execution of + `fl4LocPBL`, the Delete start's `CdtAtL` is the execution's, so the glue's `StartAtEP` of the slot needs only the + leaf's own start facts (`InvJA`, `InvQFP`, [P] `CurUnbound`, [J] `bufU`; `bufU` from `BufSH` by `bufU_start`). + +Hypothesis `hSer` of the top theorem (as `serializable_fl4_localVP_mdb`); otherwise no premise: no [J], [P] or [W] +beyond `delSlotPL_startAt`'s start facts, which are `delSlotP_startAt`'s with `MdbLk` / `NullLk` replaced by +`CdtAtL`. New module; no existing statement changes. +-/ +import Fl4ProofsA64.P2.BufWireA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA +open Fl4ProofsA64.P2.OwnMap (KLayout region own InW slotOwner) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP ownP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA MapsPA GateEpPA Fl4StepPA ExecPA StartAtEP exec_safeRunPA) +open Fl4ProofsA64.P2.E2EP Fl4ProofsA64.P2.AvoidWA +open Fl4ProofsA64.P2.S1b (KernDetA LkKernelReadsA) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv Perm Held held_disj) + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section +open Classical + +/-! ## The slot and its start -/ + +section slots +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The Delete-with-PTE slot at `PrePAL`** (`inv_localPathPAL`; `huf` by `del_hufPAL`), completed by the empty +path where its record fails. -/ +def delSlotPL : PathsPA (InvArgP S) := fun i K m hK => + if h : DelPremP st i K m then inv_localPathPAL ar hA (Cs := i.Cs) (Ω := i.Ω) (Sg := i.Sg) (Ac := i.Ac) + (Locks := i.Locks) i.number i.p hm K m hK.1 hK.2 (del_hufPAL h.ul h.cs (kd_of_kernelReadsU h.ul h.rd)) + else LocalPath.void (fl4DfltA st ar hm) (OwnMap.ko_aligned hK.1.base.base hK.2.base.base m) + +variable {st ar hA hm} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} {c : U64} {hs : U64 → Prop} {μ : U64 → U64} + +/-- **Starting the Delete-with-PTE slot at `PrePAL`** (module docstring): as `delSlotP_startAt`, with `CdtAt` asked +at the start's own locks (`CdtAtL`) in place of P1's `MdbLk` / `NullLk` at every slot. Start facts: `InvJA`, +`InvQFP`, [P] `CurUnbound`, [J] `bufU`, `CdtAtL`. -/ +theorem delSlotPL_startAt {i : InvArgP S} (h : DelPremP st i K m) (σ : S) (a : A) + (hJ : InvJA (L := L) K.toKLayout σ) (hQ : InvQFP st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K.toKLayout m σ) + (hub : CurUnbound K.vcpuOf (L.curW σ)) (hcd : CdtAtL st K.toKLayout (i.Locks σ) σ) + (hB : BufU st K.toKLayout.heap.tcbs K.toKLayout.user σ) + (hc : L.cpu σ = c) (hl : i.Locks σ = hs) (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = μ x) : + ∃ q, StartAtEP (EpochAP st) (delSlotPL st ar hA hm i K m hK) c hs μ q := by + have e : delSlotPL st ar hA hm i K m hK = inv_localPathPAL ar hA (Cs := i.Cs) (Ω := i.Ω) (Sg := i.Sg) (Ac := i.Ac) + (Locks := i.Locks) i.number i.p hm K m hK.1 hK.2 (del_hufPAL h.ul h.cs (kd_of_kernelReadsU h.ul h.rd)) := + dif_pos h + rw [e] + exact ⟨_, σ, a, fun G => prePAL_of_gateEpPA G hJ hQ hub hcd + (invBufJ8_of_bufU h.ul hQ.1.1.1.kb.symm hQ.1.1.1.cT hB), hc, hl, hμ, rfl⟩ + +end slots + +/-! ## `CdtAtL` from the history -/ + +section hist +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A slot's link words are its lock's** under `ownP` (rule 2: offsets 16..31). -/ +theorem ownP_slotLk {K : KLayoutP} (hK : RegionOKP K) (V : VmLayA K.toKLayout) {q o : U64} (hq : q ∈ K.slots) + (h16 : 16 ≤ o.val) (ho : o.val < 32) (m : U64 → U64) : ownP K m (wa q o) = .lock q := by + have hv := slot_word_val (V.slotf q hq) ho + rw [ownP_of_slot hK (V.slotw q hq _ ⟨by omega, by omega⟩)] + unfold slotOwner + rw [if_pos (by omega)] + +/-- **`CdtAtL` from the history** (module docstring): `MdbSH` of `H`, and σ = `H` on the link words of the locked +slots. -/ +theorem cdtAtL_of_hist {K : KLayout} (V : VmLayA K) {H : U64 → U64} (hM : MdbSH st K H) {Lk : U64 → Prop} {σ : S} + (hag : ∀ sl ∈ K.slots, Lk sl → L.mem σ (wa sl 16#u64) = H (wa sl 16#u64) ∧ + L.mem σ (wa sl 24#u64) = H (wa sl 24#u64)) : + CdtAtL st K Lk σ := by + obtain ⟨v, hv, G, hMl, -, -, hN, -⟩ := hM + have hC := cdtAt_of_mdb hMl hN + intro sl hsl hl + have hf := V.slotf sl hsl + have h8 := V.slot8 sl hsl + have wd : ∀ o : U64, o.val < 32 → o.val % 8 = 0 → Aligned (wa sl o) ∧ SlotWd K (wa sl o) := fun o ho h8o => by + have e := slot_word_val hf ho + exact ⟨by unfold Aligned; omega, ⟨sl, V.slotw sl hsl _ ⟨by omega, by omega⟩⟩⟩ + obtain ⟨a1, a2⟩ := hag sl hsl hl + have w16 := wd 16#u64 (by simp) (by simp) + have w24 := wd 24#u64 (by simp) (by simp) + have eP : L.mem σ (wa sl layout.cte.PREV) = L.mem v (wa sl layout.cte.PREV) := by + rw [cte_prev, a1, hv _ w16.1 w16.2] + have eN : obj.Slot.next st sl σ = obj.Slot.next st sl v := by + rw [W7.R.ST.next_eq, W7.R.ST.next_eq] + unfold W7.R.ST.NxA + rw [a2, hv _ w24.1 w24.2] + rw [eP, eN] + exact hC sl hsl + +end hist + +/-! ## At a start of the wiring -/ + +section wire +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Q : KLayoutP → (U64 → U64) → Prop} {ι : Type} {LP : PathsPA ι} + +/-- **`CdtAtL` at a start, from the wiring invariant** (module docstring). -/ +theorem WireI.cdtAtL {m0 : U64 → U64} {s : Cfg U64 U64 U64 U64} {K : KLayoutP} {H : U64 → U64} + {R : U64 → Option (Fl4Proofs.P2.Serial.RecD U64 U64 U64 U64)} (hW : WireI st Q LP m0 s K H R) + (hK : RegionOKP K) (V : VmLayA K.toKLayout) (hM : MapsPA K s) {c : U64} {p0 k : E2E.SP} {W : Set U64} + (hc : s.ph c = .loc p0 (.start k) W) {σ : S} (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = s.mem x) : + CdtAtL st K.toKLayout (fun l => s.lk l = some c) σ := by + have IH := hW.inv.inv + have hRc : R c = none := by + cases hR : R c with + | none => rfl + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hR + rw [hc] at hph; cases hph + exact False.elim (hr.once e.ρ hr.adm fun _ _ => rfl) + have hw0 := (hW.glue c _ _ _ hc hRc).2 + have hag : ∀ x, Perm s c x → s.mem x = H x := fun x hx => IH.agree x fun d q0 q U hd hxU => by + by_cases hdc : d = c + · subst hdc; rw [hc] at hd; cases hd; rw [hw0] at hxU; exact hxU + · exact held_disj hdc ((IH.loc d q0 q U hd).held x hxU) hx + have one : ∀ sl ∈ K.slots, s.lk sl = some c → ∀ o : U64, 16 ≤ o.val → o.val < 32 → o.val % 8 = 0 → + L.mem σ (wa sl o) = H (wa sl o) := fun sl hsl hl o h16 ho h8o => by + have e := slot_word_val (V.slotf sl hsl) ho + have hr := V.slotw sl hsl (wa sl o) ⟨by omega, by omega⟩ + have hnu : wa sl o ∉ K.user := fun hu => by + have e' := (V.user _ hu).1 + rw [hr] at e' + cases e' + have hal : Aligned (wa sl o) := by have := V.slot8 sl hsl; unfold Aligned; omega + rw [hμ _ hal hnu] + refine hag _ (Or.inl ?_) + rw [hM.own, ownP_slotLk hK V hsl h16 ho] + exact hl + exact cdtAtL_of_hist V hW.mdb fun sl hsl hl => + ⟨one sl hsl hl 16#u64 (by simp) (by simp) (by simp), one sl hsl hl 24#u64 (by simp) (by simp) (by simp)⟩ + +end wire + +/-! ## `fl4LocPBL`: `fl4LocPB` with the Delete-with-PTE slot at `PrePAL` -/ + +section gen +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Q : KLayoutP → (U64 → U64) → Prop} {ι : Type} {LP : PathsPA ι} + +/-- **`CdtAtL` at every start of an execution** of a wiring family (`wire_exec`, `WireI.cdtAtL`): where core `c` +starts a body at `s`, every state agreeing with `s`'s memory on the aligned kernel words has `CdtAt` at `c`'s locked +slots. -/ +theorem serializable_cdtL_of (hF : WireFam st Q LP) (hV : ∀ K e, Q K e → VmLayA K.toKLayout) + {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} (h0 : Init s0) (hA0 : ArmInv s0) (hK0 : ROKP K0) + (hM0 : MapsPA K0 s0) (hQ0 : Q K0 s0.mem) (hMd0 : MdbSH st K0.toKLayout s0.mem) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, ExecPA Q LP s0 K0 s K → Step s c s' → + Fl4StepPA Q LP s K c s' K' → E2E.SerStep s c → MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA Q LP s0 K0 s K) {c : U64} {p0 k : E2E.SP} {W : Set U64} (hc : s.ph c = .loc p0 (.start k) W) + {σ : S} (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = s.mem x) : + CdtAtL st K.toKLayout (fun l => s.lk l = some c) σ := by + obtain ⟨H, R, hW⟩ := wire_exec hF h0 hA0 hK0 hM0 hQ0 hMd0 hSer h + obtain ⟨-, hK, hM, e, he, -⟩ := exec_safeRunPA h0 hA0 hK0 hM0 hQ0 h + exact hW.cdtAtL hK.1 (hV K e he) hM hc hμ + +end gen + +section fl4 +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The Delete-with-PTE slot at `PrePAL` for the wiring** (`del_editL`; declines write the `IoDS` words). -/ +theorem delSlotPL_wire (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + SlotWireP st (EpochAP st) K (delSlotPL st ar hA hm i K m hK) := by + unfold delSlotPL + by_cases h : DelPremP st i K m + · rw [dif_pos h] + refine ⟨fun σ σ' a r hp _ hp' g' hag hr hac => ?_, fun s x hp hx => ?_⟩ + · obtain ⟨e0, he, -⟩ := g' + obtain ⟨b, s', a'⟩ := r + change b = true at hac + subst hac + change PrePAL st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ at hp + change PrePAL st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ' at hp' + change syscall.handle_invocation_local st ar σ' a i.number i.p = ok (true, s', a') at hr + exact del_editL hA hK he hp hp' hag hr + · have hp' : PrePAL st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s := hp + have N := io_narNS hp'.2.1.1.1.1 + rcases hx with rfl | rfl | rfl + · exact (N.reg hp'.2.1.1.1.1.tA (by simp [layout.reg.FAULT_IP])).2 + · exact (N.nd _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])).2 + · exact (N.nd _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])).2 + · rw [dif_neg h] + exact ⟨fun _ _ _ _ hp => hp.elim, fun _ _ hp => hp.elim⟩ + +/-- **`fl4LocPB` with the Delete-with-PTE slot at `PrePAL`** (`delSlotPL`; the other ten slots are `fl4LocPB`'s). -/ +def fl4LocPBL : PathsPA (LocIdxP S A) := fun i => + match i with + | .inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i))))))))) => delSlotPL st ar hA hm i + | i => fl4LocPB st ar hA hm i + +/-- **`fl4LocPBL` is a wiring family** (the ten other slots by `fl4LocPB_wire`, the Delete slot by `delSlotPL_wire`). -/ +theorem fl4LocPBL_wire : WireFam st (EpochAP st) (fl4LocPBL st ar hA hm) := by + have F := (fl4LocPB_wire st ar hA hm).2 + refine ⟨fun K e he b hb => he.slotNU hb, fun i K m hK hep => ?_⟩ + rcases i with i | i | i | i | i | i | j | j | j | i | i + · exact F (.inl i) K m hK hep + · exact F (.inr (.inl i)) K m hK hep + · exact F (.inr (.inr (.inl i))) K m hK hep + · exact F (.inr (.inr (.inr (.inl i)))) K m hK hep + · exact F (.inr (.inr (.inr (.inr (.inl i))))) K m hK hep + · exact F (.inr (.inr (.inr (.inr (.inr (.inl i)))))) K m hK hep + · exact F (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))) K m hK hep + · exact F (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j)))))))) K m hK hep + · exact F (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))))) K m hK hep + · exact F (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl i)))))))))) K m hK hep + · exact delSlotPL_wire st ar hA hm i K m hK + +/-- **FL4 (AArch64) end to end with the Delete-with-PTE slot at `PrePAL`** (`serializable_e2ePA`): as +`serializable_fl4_localVP`, for `fl4LocPBL`. -/ +theorem serializable_fl4_localVPL {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (h : ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K) (hq : Fl4Proofs.P2.Serial.NoLoc s) : + Fl4Proofs.P2.Serial.RunAll s.log s0.mem s.mem := + Fl4ProofsA64.P2.E2EPA.serializable_e2ePA _ h0 hA0 hB0.rok hM0 hB0 h hq + +/-- **`MdbS` of the history along an execution of `fl4LocPBL`** (`serializable_mdb_of` at `fl4LocPBL_wire`; `hSer` +as `serializable_fl4_localVP_mdb`). -/ +theorem serializable_fl4_localVPL_mdb {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (hMd0 : MdbSH st K0.toKLayout s0.mem) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, + ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K → Step s c s' → + Fl4StepPA (EpochAP st) (fl4LocPBL st ar hA hm) s K c s' K' → E2E.SerStep s c → + MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K) : + ∃ H R, Fl4Proofs.P2.Serial.InvR s0.mem s H R ∧ MdbSH st K.toKLayout H ∧ + (Fl4Proofs.P2.Serial.NoLoc s → H = s.mem) := + serializable_mdb_of (fl4LocPBL_wire st ar hA hm) h0 hA0 hB0.rok hM0 hB0 hMd0 hSer h + +/-- **The Delete-with-PTE start of `fl4LocPBL` inside an execution** (module docstring): where core `c` starts a +body at `s` (layout `K`), a state σ of the leaf with `s`'s memory on the aligned kernel words and `c`'s locks gives the +glue's `StartAtEP` from the leaf's own start facts (`InvJA`, `InvQFP`, [P] `CurUnbound`, [J] `bufU`): its `CdtAtL` is +the execution's (`serializable_cdtL_of`). `hSer` as `serializable_fl4_localVPL_mdb`. -/ +theorem delSlotPL_startExec {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (hMd0 : MdbSH st K0.toKLayout s0.mem) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, + ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K → Step s c s' → + Fl4StepPA (EpochAP st) (fl4LocPBL st ar hA hm) s K c s' K' → E2E.SerStep s c → + MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K) {c : U64} {p0 k : E2E.SP} {W : Set U64} + (hc0 : s.ph c = .loc p0 (.start k) W) (hK : ROKP K) {i : InvArgP S} (hD : DelPremP st i K s.mem) (σ : S) (a : A) + (hJ : InvJA (L := L) K.toKLayout σ) (hQ : InvQFP st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K.toKLayout s.mem σ) + (hub : CurUnbound K.vcpuOf (L.curW σ)) (hB : BufU st K.toKLayout.heap.tcbs K.toKLayout.user σ) + (hc : L.cpu σ = c) (hl : i.Locks σ = fun l => s.lk l = some c) + (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = s.mem x) : + ∃ q, StartAtEP (EpochAP st) + (fl4LocPBL st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i)))))))))) K s.mem hK) + c (fun l => s.lk l = some c) s.mem q := by + have hcd := serializable_cdtL_of (fl4LocPBL_wire st ar hA hm) (fun _ _ he => he.vmLay) h0 hA0 hB0.rok hM0 hB0 + hMd0 hSer h hc0 hμ + show ∃ q, StartAtEP (EpochAP st) (delSlotPL st ar hA hm i K s.mem hK) c (fun l => s.lk l = some c) s.mem q + exact delSlotPL_startAt hD σ a hJ hQ hub (by rw [hl]; exact hcd) hB hc hl hμ + +end fl4 + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/E2EPStartA.lean b/proofs/Fl4ProofsA64/P2/E2EPStartA.lean index 5699a745e..2032eb4d8 100644 --- a/proofs/Fl4ProofsA64/P2/E2EPStartA.lean +++ b/proofs/Fl4ProofsA64/P2/E2EPStartA.lean @@ -29,8 +29,22 @@ new slots following it in `LocIdxP`; nothing else changes. Gen 26: the two start lemmas' statements are unchanged; their proofs name the slots' new `huf` (`pm_hufPA` / `del_hufPA` from the records' `rd`, `cs` and `ep`, `P2/E2EPTopA.lean`). + +Gen 27 (Architect 13:04Z, option (b)): **`cdtAt_of_mdb`**: the entry's CDT links `CdtAt` (the new last conjunct of +A64I's `PrePA`) from P1's `MdbLk ∧ NullLk` at the layout's slot set (`P1/MdbA64.lean`, `P1/MdbNull.lean`; parts of +P1's `MdbST`, which Boot proves at w0 / w1). **Changed statement (flagged): `delSlotP_startAt`** gains the start facts +`hM : MdbLk st K.slots G σ` and `hN : NullLk st K.slots σ` (as it takes `InvJA` / `InvQFP`), in place of the record's +removed [J] `hcdt` (which asked `InvCdtJ` at every `PrePA` state). + +Gen 28 (Architect 13:39Z): **`PmPremP.hbuf` / `DelPremP.hbuf` discharged** (A64I's `PmPreG` / `PrePA` carry the entry's +`InvBufJ8`, last; `invBufJ8_pmPreG`, `invBufJ8_prePA`, `P2/E2EPTopA.lean`). **Changed statements (flagged): +`pmSlotP_startAt` and `delSlotP_startAt`** gain the start fact **`hB : BufU st K.heap.tcbs K.user σ`**, the [J] +**`bufU`** (A64-S1A: every thread's IPC buffer lies in the user words), from which the entry's `InvBufJ8` follows +(`invBufJ8_of_bufU`: the caller is a thread of the layout, `IoPre.cT`; user words are aligned, `UserLayA.usr`), in +place of the records' removed ∀-state [J] `hbuf`. `delSlotP_startAt`'s `hM` / `hN` (gen 27) are [J] start facts. -/ import Fl4ProofsA64.P2.E2EPTopA +import Fl4ProofsA64.P1.MdbNull open Aeneas Aeneas.Std Result set_option autoImplicit false @@ -42,7 +56,7 @@ open Fl4ProofsA64.P2.OwnMap (KLayout region fld own) open Fl4ProofsA64.P2.OwnMapV (KLayoutV ownV RegionOKV UserOKV InVcpu vmapV) open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP UserOKP ownP ownP_off ko_pte) open Fl4ProofsA64.P2.E2EPA (GateEpPA ROKP PathsPA StartAtEP SlotCPG CutOKG AvoidE AvoidP) -open Fl4ProofsA64.P2.E2EP (PmQF PmEx PmRoot pm_localPathA inv_localPathPA) +open Fl4ProofsA64.P2.E2EP (PmQF PmEx PmRoot pm_localPathA inv_localPathPA CdtAt invCdtJ_prePA invBufJ8_prePA) noncomputable section open Classical @@ -197,35 +211,80 @@ theorem vreSlotP_startAt {j : VreArgA S} (h : VrePremVB st j K.toKLayoutV m) (σ /-- **Starting the PageMap slot of `fl4LocPB`** (gen 24): a start asserts the parts of A64I's `PmPreG` the epoch does not give (`InvJA`, `PmQF`, `PmEx`, `PmRoot`, and [P] `CurUnbound`, A64I's `GateEp.ub`); `GateEp`, `PmVs`, `VcKsA` and -`VcTabA` come from the epoch data (`pmPreG_of_gateEpPA`). -/ +`VcTabA` come from the epoch data (`pmPreG_of_gateEpPA`). Gen 28 (changed statement): and the [J] start fact +`hB : BufU` (`bufU`), which gives the entry's `InvBufJ8` (`invBufJ8_of_bufU`). -/ theorem pmSlotP_startAt {i : InvArgP S} (h : PmPremP st i K m) (σ : S) (a : A) (hJ : InvJA (L := L) K.toKLayout σ) (hQ : PmQF st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ) (hub : CurUnbound K.vcpuOf (L.curW σ)) (hEx : PmEx st i.p σ) (hRt : PmRoot st i.p σ) + (hB : BufU st K.toKLayout.heap.tcbs K.toKLayout.user σ) (hc : L.cpu σ = c) (hl : i.Locks σ = hs) (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = μ x) : ∃ q, StartAtEP (EpochAP st) (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl i)))))))))) K m hK) c hs μ q := by - have e : pmSlotP st ar hA hm i K m hK = pm_localPathA ar hA hK.1 hK.2 i.number i.p hm h.hbuf + have e : pmSlotP st ar hA hm i K m hK = pm_localPathA ar hA hK.1 hK.2 i.number i.p hm + (invBufJ8_pmPreG (Cs := i.Cs) (Ω := i.Ω) (Sg := i.Sg) (Ac := i.Ac) (Locks := i.Locks)) (pm_hufPA h.ul hK.1 hK.2 h.cs (kd_of_kernelReadsU h.ul h.rd)) := dif_pos h show ∃ q, StartAtEP (EpochAP st) (pmSlotP st ar hA hm i K m hK) c hs μ q rw [e] - exact ⟨_, σ, a, fun G => pmPreG_of_gateEpPA G hJ hQ hub hEx hRt, hc, hl, hμ, rfl⟩ + exact ⟨_, σ, a, fun G => pmPreG_of_gateEpPA G hJ hQ hub hEx hRt + (invBufJ8_of_bufU h.ul hQ.1.1.1.kb.symm hQ.1.1.1.cT hB), hc, hl, hμ, rfl⟩ + +/-- (gen 27) **The entry's CDT links from P1's MDB facts** at the layout's slot set: by `MdbLk` a non-null slot is +a chain member, whose `PREV` word is the previous member (`0` at the head) and whose `NEXT` pointer is the next member +(`0` at the end); by `NullLk` a null slot's links are zero. -/ +theorem cdtAt_of_mdb {K : KLayout} {s : S} {G : List (List U64)} (hM : W7.R.ST.MdbLk st K.slots G s) + (hN : W7.R.ST.NullLk st K.slots s) : CdtAt st K s := by + intro sl hsl + have hP : core.num.U64.wrapping_add sl layout.cte.PREV = core.num.U64.wrapping_add sl 16#u64 := by + simp only [layout.cte.PREV] + have hnx : ∀ nx, obj.Slot.next st sl s = ok nx → nx = W7.R.ST.NxA st s sl := fun nx e => + (ok_inj_ff ((W7.R.ST.next_eq (st := st) s sl).symm.trans e)).symm + rw [hP] + by_cases hc : W7.R.ST.capA st s sl = .Null + · obtain ⟨h16, h24⟩ := hN sl hsl hc + exact ⟨fun h => absurd h16 h, fun nx e hne => absurd ((hnx nx e).trans h24) hne⟩ + · obtain ⟨ch, hch, hsc⟩ := List.mem_flatten.1 ((hM.members sl).2 ⟨hsl, hc⟩) + obtain ⟨i, hi, hgi⟩ := List.getElem_of_mem hsc + have hK : ∀ j a, ch[j]? = some a → a ∈ K.slots := fun j a e => + ((hM.members a).1 (List.mem_flatten.2 ⟨ch, hch, List.mem_of_getElem? e⟩)).1 + have hsi : ch[i]? = some sl := by rw [List.getElem?_eq_getElem hi, hgi] + refine ⟨fun h => ?_, fun nx e hne => ?_⟩ + · cases i with + | zero => exact absurd (hM.head ch hch sl (by rw [List.head?_eq_getElem?]; exact hsi)) h + | succ j => + have hj : j < ch.length := by omega + rw [(hM.chain ch hch j ch[j] sl (List.getElem?_eq_getElem hj) hsi).2] + exact hK j _ (List.getElem?_eq_getElem hj) + · obtain rfl := hnx nx e + by_cases hl : i + 1 < ch.length + · rw [(hM.chain ch hch i sl ch[i + 1] hsi (List.getElem?_eq_getElem hl)).1] + exact hK (i + 1) _ (List.getElem?_eq_getElem hl) + · have hlast : ch.getLast? = some sl := by + rw [List.getLast?_eq_getElem?, show ch.length - 1 = i by omega]; exact hsi + exact absurd (hM.last ch hch sl hlast) hne /-- **Starting the Delete-with-PTE slot of `fl4LocPB`** (gen 24): a start asserts `InvJA`, `InvQFP` and [P] -`CurUnbound`; the rest of A64I's `PrePA` comes from the epoch data (`prePA_of_gateEpPA`). -/ +`CurUnbound`; the rest of A64I's `PrePA` comes from the epoch data (`prePA_of_gateEpPA`). Gen 27 (changed +statement): and P1's `MdbLk` / `NullLk` at the slot set, which give the entry's `CdtAt` (`cdtAt_of_mdb`): **`hM` / +`hN` are [J] start facts** (P1 `MdbST` at the start state; Boot proves them at w0 / w1, their preservation along the +local paths is the glue's). Gen 28 (changed statement): and the [J] start fact `hB : BufU` (`bufU`), which gives the +entry's `InvBufJ8` (`invBufJ8_of_bufU`). -/ theorem delSlotP_startAt {i : InvArgP S} (h : DelPremP st i K m) (σ : S) (a : A) (hJ : InvJA (L := L) K.toKLayout σ) (hQ : InvQFP st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K.toKLayout m σ) - (hub : CurUnbound K.vcpuOf (L.curW σ)) + (hub : CurUnbound K.vcpuOf (L.curW σ)) {G : List (List U64)} (hM : W7.R.ST.MdbLk st K.toKLayout.slots G σ) + (hN : W7.R.ST.NullLk st K.toKLayout.slots σ) (hB : BufU st K.toKLayout.heap.tcbs K.toKLayout.user σ) (hc : L.cpu σ = c) (hl : i.Locks σ = hs) (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = μ x) : ∃ q, StartAtEP (EpochAP st) (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i)))))))))) K m hK) c hs μ q := by - have e : delSlotP st ar hA hm i K m hK = inv_localPathPA ar hA i.number i.p hm K m hK.1 hK.2 h.hcdt h.hbuf + have e : delSlotP st ar hA hm i K m hK = inv_localPathPA ar hA i.number i.p hm K m hK.1 hK.2 invCdtJ_prePA + (invBufJ8_prePA (Cs := i.Cs) (Ω := i.Ω) (Sg := i.Sg) (Ac := i.Ac) (Locks := i.Locks)) (del_hufPA h.ul h.cs (kd_of_kernelReadsU h.ul h.rd)) := dif_pos h show ∃ q, StartAtEP (EpochAP st) (delSlotP st ar hA hm i K m hK) c hs μ q rw [e] - exact ⟨_, σ, a, fun G => prePA_of_gateEpPA G hJ hQ hub, hc, hl, hμ, rfl⟩ + exact ⟨_, σ, a, fun G => prePA_of_gateEpPA G hJ hQ hub (cdtAt_of_mdb hM hN) + (invBufJ8_of_bufU h.ul hQ.1.1.1.kb.symm hQ.1.1.1.cT hB), hc, hl, hμ, rfl⟩ end slots diff --git a/proofs/Fl4ProofsA64/P2/E2EPTopA.lean b/proofs/Fl4ProofsA64/P2/E2EPTopA.lean index 5cd28970e..e549492d7 100644 --- a/proofs/Fl4ProofsA64/P2/E2EPTopA.lean +++ b/proofs/Fl4ProofsA64/P2/E2EPTopA.lean @@ -41,9 +41,17 @@ NBWait) by `SlotCPG` (`P2/E2EPCutVA.lean`) with their `*_cutOKP`, the three `vq` * (gen 26, Architect 11:50Z) **`huf` of the two PTE slots discharged**: `EpochA.userLay` (the epoch's user-word facts, `UserLayA`, `P2/HufPA.lean`), `pmVs_wrUA`, **`pm_hufPA`** (A64I's `PmHufG`), **`del_hufPA`** (`DelPteHufA`), from the records' [P] `rd` (`LkKernelReadsA`) and [P] `cs` (the index's cores below 128). The records `PmPremP` / - `DelPremP` are now structures: `hbuf` [J] (kept, the slot's own, uniform with `IrqPremVB.ub`), `hcdt` [J] (Delete, - kept, a start-state `MdbLk`-preservation obligation), `rd` [P], `cs` [P], `ep` (a pure layout-existence + `DelPremP` are now structures: `hbuf` [J] (discharged gen 28), `hcdt` [J] (Delete, discharged gen 27), `rd` [P], `cs` [P], `ep` (a pure layout-existence witness `∃ e, EpochAP st K e`: no new conjunct, no [J] debt, discharged at every start as `⟨e, hep⟩`). +* (gen 27, Architect 13:04Z) **`hcdt` of the Delete-with-PTE slot discharged**: A64I's `PrePA` carries the entry's + CDT links `CdtAt` (last conjunct), so `InvCdtJ` at `PrePA` is `invCdtJ_prePA`; the field is removed from + `DelPremP`. A user write keeps `CdtAt` (**`cdtAt_wrUA`**, in `del_hufPA`); a start derives it from P1's `MdbLk ∧ + NullLk` at the slot set (`P2/E2EPStartA.lean`, `cdtAt_of_mdb`), as Boot proves them at w0 / w1. +* (gen 28, Architect 13:39Z) **`hbuf` of the two PTE slots discharged**: A64I's `PmPreG` / `PrePA` carry the entry's + IPC-buffer placement `InvBufJ8` (last conjunct), so `hbuf` is **`invBufJ8_pmPreG`** / `invBufJ8_prePA`; the field is + removed from `PmPremP` and `DelPremP`, which now have **no [J] field**. A user write keeps it (**`invBufJ8_wrUA`**, + in `pm_hufPA` / `del_hufPA`); a start derives it (**`invBufJ8_of_bufU`**) from the new [J] start fact **`BufU`** + (`bufU`, A64-S1A: every thread's IPC buffer lies in the user words, P1's `BufOK`). * **`fl4LocPB`**: the eleven slots (gen 22's nine, gen 24's PageMap and Delete-with-PTE) as one `PathsPA` family; **`serializable_fl4_localVP`**: `serializable_e2ePA` at `EpochAP st`. `serializable_fl4_localVXb` (the `ownV` top) stays as it is. @@ -51,7 +59,8 @@ Premises of `serializable_fl4_localVP`: at the start `Init`, `ArmInv`, `EpochAP (`MapsPA`); per core step `Fl4StepPA` (`arm`, `start` [P], `ser` [J] with `EpochAP` at the serial state); per hardware step `HwStepPA` [P]; per slot its record's (`*PremVB`, `VxPremVB`, `VtPremVB`, `VrePremVB`, unchanged; gen 24: `PmPremP`, `DelPremP`; gen 26: their [J]s are `hbuf` and `hcdt` only, `huf` discharged, with [P] `rd`, [P] `cs` and -the layout-existence witness `ep`). **No [W].** Gen 24: `EpochAP` restated in place (two conjuncts added, last), `LocIdxP` / +the layout-existence witness `ep`; gen 27: `hcdt` discharged, the only [J] left is `hbuf`; gen 28: `hbuf` discharged, +no [J] field left; the starts take the [J] start fact `bufU`). **No [W].** Gen 24: `EpochAP` restated in place (two conjuncts added, last), `LocIdxP` / `fl4LocPB` extended; the statement text of `serializable_fl4_localVP` is unchanged. -/ import Fl4ProofsA64.P2.CutVxP @@ -75,7 +84,8 @@ open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP UserOKP ownP ownP_off ko_pte) open Fl4ProofsA64.P2.E2EPA (GateEpPA ROKP PathsPA MapsPA ExecPA SlotCPG CutOKG AvoidE AvoidP serializable_e2ePA) open Fl4Proofs.P2.Serial (Owner ClsOwner Cfg Init ArmInv NoLoc RunAll) open Fl4ProofsA64.P2.E2EP (ptesR VsLayA GateEp GateW gateW_of_region GateAgT ptesR_subT page_ne vsW_ptw vsW_atW PmVs - PmPreG PrePA VcKsA VcTabA PmQF PmEx PmRoot PmHufG DelPteHufA pm_localPathA inv_localPathPA + PmPreG PrePA VcKsA VcTabA PmQF PmEx PmRoot PmHufG DelPteHufA pm_localPathA inv_localPathPA CdtAt invCdtJ_prePA + invBufJ8_prePA PrePAL CdtAtL DelPteHufAL walk_wrUA pmEx_wrUA pmRoot_wrUA invPlanD_mapUA invPlanD_delUA ownP_wrUA gateEp_userWr PmRSU PmWSU PmRS PmWS InvRSPA InvWSPA IoRSPV IoRSPA IoWSPA) open Fl4ProofsA64.P2.S1b (KernDetA LkKernelReadsA) @@ -245,24 +255,38 @@ theorem pmVs_of_gateEpPA {K : KLayoutP} {s : S} (G : GateEpPA st (EpochAP st) K /-- **The PageMap path's `PmPreG` at a start with the epoch data** (Architect 10:46Z): A64I's `GateEp` by the bridge, `PmVs` by `pmVs_of_gateEpPA` (the entry's base from `IoPre.kb`), `VcKsA` / `VcTabA` from `EpochA`; the start gives -the rest (`InvJA`, `PmQF`, `PmEx`, `PmRoot`, and [P] `CurUnbound`). -/ +the rest (`InvJA`, `PmQF`, `PmEx`, `PmRoot`, and [P] `CurUnbound`; gen 28: the entry's buffer placement `InvBufJ8`, +`invBufJ8_of_bufU`). -/ theorem pmPreG_of_gateEpPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} {s : S} (G : GateEpPA st (EpochAP st) K s) (hJ : InvJA (L := L) K.toKLayout s) (hQ : PmQF st Cs Ω Sg Ac Locks p K m s) (hub : CurUnbound K.vcpuOf (L.curW s)) (hEx : PmEx st p s) - (hRt : PmRoot st p s) : PmPreG st Cs Ω Sg Ac Locks p K m s := + (hRt : PmRoot st p s) (hbj : InvBufJ8 st K.toKLayout s) : PmPreG st Cs Ω Sg Ac Locks p K m s := let ⟨_, hE, _⟩ := G ⟨⟨hJ, hQ, gateEp_of_gateEpPA (fun _ _ h => h.2.2.2.2.2.1) G hub⟩, hEx, hRt, pmVs_of_gateEpPA G hQ.1.1.1.kb, - hE.1.vcKs, hE.1.vcTab⟩ + hE.1.vcKs, hE.1.vcTab, hbj⟩ /-- **The Delete-with-PTE path's `PrePA` at a start with the epoch data**: as `pmPreG_of_gateEpPA`; the start gives -`InvJA`, `InvQFP` and [P] `CurUnbound`. -/ +`InvJA`, `InvQFP`, [P] `CurUnbound` and (gen 27) the entry's CDT links `CdtAt` (`cdtAt_of_mdb`, from P1's `MdbLk ∧ +NullLk`, `P2/E2EPStartA.lean`), (gen 28) the entry's buffer placement `InvBufJ8` (`invBufJ8_of_bufU`). -/ theorem prePA_of_gateEpPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} {s : S} (G : GateEpPA st (EpochAP st) K s) (hJ : InvJA (L := L) K.toKLayout s) - (hQ : InvQFP st Cs Ω Sg Ac Locks p K.toKLayout m s) (hub : CurUnbound K.vcpuOf (L.curW s)) : + (hQ : InvQFP st Cs Ω Sg Ac Locks p K.toKLayout m s) (hub : CurUnbound K.vcpuOf (L.curW s)) + (hcd : CdtAt st K.toKLayout s) (hbj : InvBufJ8 st K.toKLayout s) : PrePA st Cs Ω Sg Ac Locks p K m s := let ⟨_, hE, _⟩ := G ⟨hJ, hQ, gateEp_of_gateEpPA (fun _ _ h => h.2.2.2.2.2.1) G hub, pmVs_of_gateEpPA G hQ.1.1.1.kb, - hE.1.vcKs, hE.1.vcTab⟩ + hE.1.vcKs, hE.1.vcTab, hcd, hbj⟩ + +/-- (P2-Sync gen 5) **The Delete-with-PTE path's `PrePAL` at a start with the epoch data** (`prePA_of_gateEpPA` +with the entry's CDT links at the locked slots, `CdtAtL`). -/ +theorem prePAL_of_gateEpPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} + {m : U64 → U64} {s : S} (G : GateEpPA st (EpochAP st) K s) (hJ : InvJA (L := L) K.toKLayout s) + (hQ : InvQFP st Cs Ω Sg Ac Locks p K.toKLayout m s) (hub : CurUnbound K.vcpuOf (L.curW s)) + (hcd : CdtAtL st K.toKLayout (Locks s) s) (hbj : InvBufJ8 st K.toKLayout s) : + PrePAL st Cs Ω Sg Ac Locks p K m s := + let ⟨_, hE, _⟩ := G + ⟨hJ, hQ, gateEp_of_gateEpPA (fun _ _ h => h.2.2.2.2.2.1) G hub, pmVs_of_gateEpPA G hQ.1.1.1.kb, + hE.1.vcKs, hE.1.vcTab, hcd, hbj⟩ /-- A state of which `K` is the bridge layout has `K`'s kernel base. -/ theorem LayWbAt.ksb {Vc : Set U64} {K : KLayout} {x : S} (h : LayWbAt st Vc K x) : K.ksb = L.ksb x := by @@ -305,16 +329,56 @@ theorem pmVs_wrUA {K : KLayoutP} {Pp Tb : Set U64} {Lv : U64 → Nat} {s : S} {b (fun p hp o h1 h2 => hw p (Or.inl hp) o h1 h2) (fun p hp _ o h1 h2 => hw p (Or.inr hp) o h1 h2) ksb := by rw [L.ksb_wr]; exact hV.ksb } +/-- (gen 28) **`InvBufJ8` across a user write**: the current thread is kept (`cur_wrUA`), and the three words its +`lookup_ipc_buffer` reads (`IPC_BUFFER` at TCB+3512, the buffer cap slot at TCB+64 / +72) are TCB words, none of them +the written `UsrW` word (`usr_tcb`), so the lookup answers alike (`lookup_ipc_buffer_same`). -/ +theorem invBufJ8_wrUA {Cs Ω Sg Ac : Set U64} {K : KLayout} {s : S} {b : U64} (U : UserLayA st K) + (P : IoPre st K K.heap.tcbs Cs Ω K.heap Sg K.heap.replies K.heap.scs K.heap.ntfns K.heap.scBits Ac s) + (h : InvBufJ8 st K s) (hb : b ∈ K.user) (v : U64) : InvBufJ8 st K (L.wr s b v) := by + have u := U.usr b hb s P.kb.symm + have hI : IbSame (L := L) s (L.wr s b v) (L.curW s) := by + refine ⟨S1b.usr_tcb u P.tp P.cT (o := 3512#u64) (by simp) (by simp) v, ?_, ?_⟩ + · rw [sbWordA_eq]; exact S1b.usr_tcb u P.tp P.cT (o := 64#u64) (by simp) (by simp) v + · rw [sbWordA8_eq]; exact S1b.usr_tcb u P.tp P.cT (o := 72#u64) (by simp) (by simp) v + intro b' hl + rw [(cur_wrUA U P hb v).1, lookup_ipc_buffer_same hI] at hl + exact h b' hl + +variable (st) in +/-- **`bufU`** [J] (A64-S1A, a start fact; P2-A64X gen 28, Architect 13:39Z): the IPC buffer of every thread of `T` +lies in the user words `U`: for each `t ∈ T`, `lookup_ipc_buffer` (as the kernel calls it for a receiver) answers a +buffer whose 128 words are `U` words (P1's `BufOK`, the second half of P1's `BufAt`). A64-S1A discharges it on its own +branch (P1's `lookup_bufIn` places the buffer inside the frame its buffer cap names; that the frames named by the +`OS` buffer caps are user frames, `⊆ Ub`, is the S1A fact). Tagged [J]; not an `EpochAP` conjunct. -/ +def BufU (T U : Set U64) (s : S) : Prop := + ∀ t ∈ T, msg.lookup_ipc_buffer st s true t ⦃ b => BufOK U b ⦄ + +/-- (gen 28) **The entry's `InvBufJ8` from [J] `bufU`**: the caller is a thread of the layout, so its buffer's 128 +words are user words (`bufU`), and every user word is aligned (a P1 `UsrW` word, `UserLayA.usr`). -/ +theorem invBufJ8_of_bufU {K : KLayout} {s : S} (U : UserLayA st K) (hks : L.ksb s = K.ksb) + (hc : L.curW s ∈ K.heap.tcbs) (hB : BufU st K.heap.tcbs K.user s) : InvBufJ8 st K s := by + intro b hl hb0 i hi + have h := hB _ hc + rw [hl] at h + have hw := (WP.spec_ok _).1 h hb0 i hi + exact ⟨(U.usr _ hw s hks).out.1, hw⟩ + +/-- (gen 28) **`InvBufJ8` at `PmPreG`** (the former [J] `hbuf` of `PmPremP`): `PmPreG`'s last conjunct. -/ +theorem invBufJ8_pmPreG {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} + {m : U64 → U64} : ∀ s, PmPreG st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s := + fun _ hE => hE.2.2.2.2.2.2 + /-- **A64I's `PmHufG` discharged** (Architect 11:50Z): at a layout with the epoch's `UserLayA`, the index's cores below 128 ([P] `cs`) and the locks determined off the user words (from [P] `rd`, `kd_of_kernelReadsU`), a user write keeps `PmPreG` (`IoQ` by `ioQ_wrUA`, the plan by `invPlanD_mapUA`, the lookup by `invLk_wrUA`, `GateEp` by `gateEp_userWr`, `PmEx` / `PmRoot` / `PmVs` by `pmEx_wrUA` / `pmRoot_wrUA` / `pmVs_wrUA`; `InvJA`, `VcKsA`, `VcTabA` -are state-free), the locks, `PmRSU` / `PmWSU` (`ownP` reads no user word) and `IoDS`. -/ +are state-free; gen 28: `InvBufJ8` by `invBufJ8_wrUA`), the locks, `PmRSU` / `PmWSU` (`ownP` reads no user word) and +`IoDS`. -/ theorem pm_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {μ : U64 → U64} (U : UserLayA st K.toKLayout) (hK : RegionOKP K) (hU : UserOKP K) (hcs : ∀ c ∈ Cs, c.val < 128) (kd : ∀ s, KernDetA (L := L) K.user Locks s) : PmHufG st Cs Ω Sg Ac Locks p K μ := by intro s b v hE hb hba - obtain ⟨⟨hJ, ⟨⟨hio, hpd, harm, hlks⟩, hlk0⟩, hG⟩, hEx, hRt, ⟨Pp, Tb, Lv, hV, h0⟩, hks, hvt⟩ := hE + obtain ⟨⟨hJ, ⟨⟨hio, hpd, harm, hlks⟩, hlk0⟩, hG⟩, hEx, hRt, ⟨Pp, Tb, Lv, hV, h0⟩, hks, hvt, hbj⟩ := hE have hq : PmQF st Cs Ω Sg Ac Locks p K μ s := ⟨⟨hio, hpd, harm, hlks⟩, hlk0⟩ have P := hio.1 have el := (kd s).wr hb hba v @@ -325,7 +389,7 @@ theorem pm_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : local refine ⟨⟨⟨⟨hJ.regionOK, hJ.userOK, hJ.ioLayA, hJ.slots0⟩, ⟨⟨ioQ_wrUA U hcs hio hb v, invPlanD_mapUA U hq hEx hb hba v efind epe, ?_, ?_⟩, invLk_wrUA U P hlk0 hb v⟩, gateEp_userWr hG hb hba ecur⟩, pmEx_wrUA U P hEx hb v, pmRoot_wrUA P hEx hRt hb hba v efind, - ⟨Pp, Tb, Lv, pmVs_wrUA hV hb hba v, h0⟩, hks, hvt⟩, el, ?_, ?_, ?_⟩ + ⟨Pp, Tb, Lv, pmVs_wrUA hV hb hba v, h0⟩, hks, hvt, invBufJ8_wrUA U P hbj hb v⟩, el, ?_, ?_, ?_⟩ · intro t info ht hi rw [cur_thread_ok, ecur] at ht obtain rfl := (ok_inj_ff ht).symm @@ -338,14 +402,44 @@ theorem pm_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : local · funext y; simp only [PmWSU, PmWS, WPerm, eoP, el, L.cpu_wr] · funext y; simp only [IoDS, ecur, L.cpu_wr, nodeAt_wr] -/-- **A64I's `DelPteHufA` discharged** (Architect 11:50Z): as `pm_hufPA` for `PrePA` (the plan by -`invPlanD_delUA`: every `small_page_entry` is kept, `walk_wrUA`), the sets `InvRSPA` / `InvWSPA` (`own` reads no user -word, the current thread kept). -/ -theorem del_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} +/-- (P2-Sync gen 5) **`CdtAtL` across a user write** (gen 27's `cdtAt_wrUA` at the locked slots `Lk`): a slot's `PREV` / `NEXT` words are no user words +(`IoPre.slotW_nuUA`), so the write keeps them. -/ +theorem cdtAtL_wrUA {Lk : U64 → Prop} {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} {s : S} {b : U64} + (P : IoPre st K T Cs Ω H Sg Rp Scs Nt Z Ac s) (h : CdtAtL st K Lk s) (hb : b ∈ K.user) (hba : Aligned b) (v : U64) : + CdtAtL st K Lk (L.wr s b v) := by + intro sl hsl hl + have h8 := P.lay.slot8 sl hsl + have hf := P.lay.slotf sl hsl + have hvP : (core.num.U64.wrapping_add sl layout.cte.PREV).val = sl.val + 16 := by + simp [core.num.U64.wrapping_add_val_eq, layout.cte.PREV, U64.size_def, U64.numBits]; omega + have hvN : (core.num.U64.wrapping_add sl layout.cte.NEXT).val = sl.val + 24 := by + simp [core.num.U64.wrapping_add_val_eq, layout.cte.NEXT, U64.size_def, U64.numBits]; omega + have eP : L.mem (L.wr s b v) (core.num.U64.wrapping_add sl layout.cte.PREV) = + L.mem s (core.num.U64.wrapping_add sl layout.cte.PREV) := + mem_wr_ne' hba (by unfold Aligned; omega) (fun e => P.slotW_nuUA hsl ⟨by omega, by omega⟩ (e ▸ hb)) + have eN : obj.Slot.next st sl (L.wr s b v) = obj.Slot.next st sl s := by + have e : L.mem (L.wr s b v) (core.num.U64.wrapping_add sl layout.cte.NEXT) = + L.mem s (core.num.U64.wrapping_add sl layout.cte.NEXT) := + mem_wr_ne' hba (by unfold Aligned; omega) (fun e => P.slotW_nuUA hsl ⟨by omega, by omega⟩ (e ▸ hb)) + simp only [obj.Slot.next, obj.Slot.next_word, lift, bind_ok, L.read_eq, e] + rw [eP, eN]; exact h sl hsl hl + +/-- (gen 27) **`CdtAt` across a user write**: a slot's `PREV` / `NEXT` words are no user words +(`IoPre.slotW_nuUA`), so the write keeps them. -/ +theorem cdtAt_wrUA {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} {s : S} {b : U64} + (P : IoPre st K T Cs Ω H Sg Rp Scs Nt Z Ac s) (h : CdtAt st K s) (hb : b ∈ K.user) (hba : Aligned b) (v : U64) : + CdtAt st K (L.wr s b v) := + fun sl hsl => cdtAtL_wrUA (Lk := fun _ => True) P h.toL hb hba v sl hsl trivial + +/-- (P2-Sync gen 5) **`DelPteHufAL` discharged**, as `del_hufPA` (`CdtAtL` by `cdtAtL_wrUA`, the locks kept). +(Gen 26) **A64I's `DelPteHufA` discharged** (Architect 11:50Z): as `pm_hufPA` for `PrePA` (the plan by +`invPlanD_delUA`: every `small_page_entry` is kept, `walk_wrUA`; gen 27: `CdtAt` by `cdtAt_wrUA`; gen 28: `InvBufJ8` by +`invBufJ8_wrUA`), the sets `InvRSPA` / `InvWSPA` (`own` reads no user word, the current thread kept). -/ +theorem del_hufPAL {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {μ : U64 → U64} (U : UserLayA st K.toKLayout) (hcs : ∀ c ∈ Cs, c.val < 128) - (kd : ∀ s, KernDetA (L := L) K.user Locks s) : DelPteHufA st Cs Ω Sg Ac Locks p K μ := by + (kd : ∀ s, KernDetA (L := L) K.user Locks s) : DelPteHufAL st Cs Ω Sg Ac Locks p K μ := by intro s b v hE hb hba - obtain ⟨hJ, ⟨⟨hio, hpd, harm, hlks⟩, hlk0⟩, hG, ⟨Pp, Tb, Lv, hV, h0⟩, hks, hvt⟩ := hE + obtain ⟨hJ, ⟨⟨hio, hpd, harm, hlks⟩, hlk0⟩, hG, ⟨Pp, Tb, Lv, hV, h0⟩, hks, hvt, hcd, hbj⟩ := hE have hqf : InvQFP st Cs Ω Sg Ac Locks p K.toKLayout μ s := ⟨⟨hio, hpd, harm, hlks⟩, hlk0⟩ have P := hio.1 have el := (kd s).wr hb hba v @@ -356,7 +450,9 @@ theorem del_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : loca own_wr_user hJ.regionOK hJ.userOK hb hba refine ⟨⟨⟨hJ.regionOK, hJ.userOK, hJ.ioLayA, hJ.slots0⟩, ⟨⟨ioQ_wrUA U hcs hio hb v, invPlanD_delUA U hqf hb hba v espe, ?_, ?_⟩, invLk_wrUA U P hlk0 hb v⟩, - gateEp_userWr hG hb hba ecur, ⟨Pp, Tb, Lv, pmVs_wrUA hV hb hba v, h0⟩, hks, hvt⟩, el, ?_, ?_, ?_⟩ + gateEp_userWr hG hb hba ecur, ⟨Pp, Tb, Lv, pmVs_wrUA hV hb hba v, h0⟩, hks, hvt, (by rw [el]; exact cdtAtL_wrUA P hcd hb hba v), + invBufJ8_wrUA U P hbj hb v⟩, el, + ?_, ?_, ?_⟩ · intro t info ht hi rw [cur_thread_ok, ecur] at ht obtain rfl := (ok_inj_ff ht).symm @@ -369,6 +465,16 @@ theorem del_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : loca · funext y; simp only [InvWSPA, IoWSPA, IoWS, WPerm, eo, el, L.cpu_wr] · funext y; simp only [IoDS, ecur, L.cpu_wr, nodeAt_wr] +/-- **A64I's `DelPteHufA` discharged** (Architect 11:50Z): as `pm_hufPA` for `PrePA` (the plan by +`invPlanD_delUA`: every `small_page_entry` is kept, `walk_wrUA`; gen 27: `CdtAt` by `cdtAt_wrUA`; gen 28: `InvBufJ8` by +`invBufJ8_wrUA`), the sets `InvRSPA` / `InvWSPA` (`own` reads no user word, the current thread kept). -/ +theorem del_hufPA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} + {μ : U64 → U64} (U : UserLayA st K.toKLayout) (hcs : ∀ c ∈ Cs, c.val < 128) + (kd : ∀ s, KernDetA (L := L) K.user Locks s) : DelPteHufA st Cs Ω Sg Ac Locks p K μ := by + intro s b v hE hb hba + obtain ⟨⟨a1, a2, a3, a4, a5, a6, -, a8⟩, r⟩ := del_hufPAL U hcs kd s b v hE.toL hb hba + exact ⟨⟨a1, a2, a3, a4, a5, a6, cdtAt_wrUA hE.2.1.1.1.1 hE.2.2.2.2.2.2.1 hb hba v, a8⟩, r⟩ + end ep /-! ## Obligations needing a layout fact, at an entry condition asking it -/ @@ -560,12 +666,8 @@ structure InvArgP (S : Type) where p : localinv.Plan /-- **The PageMap slot's record** at `K` / `m` (gen 26, Architect 11:50Z: `huf` discharged by `pm_hufPA`; in place of -gen 24's `hbuf ∧ huf`). -/ +gen 24's `hbuf ∧ huf`; gen 28: `hbuf` discharged, `invBufJ8_pmPreG`, the field removed: **no [J] field**). -/ structure PmPremP (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) : Prop where - /-- **`hbuf`** [J] (the slot's own, kept as it is, uniform with `IrqPremVB.ub`): `InvBufJ8`, the IPC-buffer - placement, at every `PmPreG` state; to be discharged with `IrqPremVB.ub` once P3 / A64-S1A supplies the IPC-buffer - placement. It is not an `EpochAP` conjunct. -/ - hbuf : ∀ s, PmPreG st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s → InvBufJ8 st K.toKLayout s /-- **`rd`** [P] (the shared read-locality every slot carries, `LkKernelReadsA`, `P2/KernDetA.lean`): the lock map reads only aligned kernel words. -/ rd : LkKernelReadsA (L := L) K.toKLayout i.Locks @@ -579,16 +681,9 @@ structure PmPremP (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) : Prop where ep : ∃ e, EpochAP st K e /-- **The Delete-with-PTE slot's record** at `K` / `m` (gen 26: `huf` discharged by `del_hufPA`; in place of gen 24's -`hcdt ∧ hbuf ∧ huf`). -/ +`hcdt ∧ hbuf ∧ huf`; gen 27: `hcdt` discharged, `invCdtJ_prePA`, the field removed; gen 28: `hbuf` discharged, +`invBufJ8_prePA`, the field removed: **no [J] field**). -/ structure DelPremP (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) : Prop where - /-- **`hcdt`** [J] (a start-state fact, kept as it is): CDT link well-formedness at every `PrePA` state - (`InvCdtJ`), an A64 / P1 `MdbLk`-preservation obligation (`P1/MdbA64.lean`): the `PREV` / `NEXT` words are - lock-owned, so no epoch fact gives it. -/ - hcdt : InvCdtJ st K.toKLayout (PrePA st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m) - /-- **`hbuf`** [J] (the slot's own, kept as it is, uniform with `IrqPremVB.ub`): `InvBufJ8`, the IPC-buffer - placement, at every `PrePA` state; to be discharged with `IrqPremVB.ub` once P3 / A64-S1A supplies the IPC-buffer - placement. It is not an `EpochAP` conjunct. -/ - hbuf : ∀ s, PrePA st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s → InvBufJ8 st K.toKLayout s /-- **`rd`** [P] (as `PmPremP.rd`). -/ rd : LkKernelReadsA (L := L) K.toKLayout i.Locks /-- **`cs`** [P] (as `PmPremP.cs`). -/ @@ -610,16 +705,18 @@ theorem DelPremP.ul {i : InvArgP S} {K : KLayoutP} {m : U64 → U64} (h : DelPre h.ep.elim fun _ he => he.1.userLay /-- **The PageMap slot** (`pm_localPathA`, already an `ownP` path: no cut, no lift; `hK` / `hU` from `ROKP`; gen 26: -`huf` by `pm_hufPA`), completed by the empty path where its record fails. -/ +`huf` by `pm_hufPA`; gen 28: `hbuf` by `invBufJ8_pmPreG`), completed by the empty path where its record fails. -/ def pmSlotP : PathsPA (InvArgP S) := fun i K m hK => - if h : PmPremP st i K m then pm_localPathA ar hA hK.1 hK.2 i.number i.p hm h.hbuf + if h : PmPremP st i K m then pm_localPathA ar hA hK.1 hK.2 i.number i.p hm + (invBufJ8_pmPreG (Cs := i.Cs) (Ω := i.Ω) (Sg := i.Sg) (Ac := i.Ac) (Locks := i.Locks)) (pm_hufPA h.ul hK.1 hK.2 h.cs (kd_of_kernelReadsU h.ul h.rd)) else LocalPath.void (fl4DfltA st ar hm) (OwnMap.ko_aligned hK.1.base.base hK.2.base.base m) -/-- **The Delete-with-PTE slot** (`inv_localPathPA`, an `ownP` path; gen 26: `huf` by `del_hufPA`), completed by the -empty path. -/ +/-- **The Delete-with-PTE slot** (`inv_localPathPA`, an `ownP` path; gen 26: `huf` by `del_hufPA`; gen 27: `hcdt` by +`invCdtJ_prePA`; gen 28: `hbuf` by `invBufJ8_prePA`), completed by the empty path. -/ def delSlotP : PathsPA (InvArgP S) := fun i K m hK => - if h : DelPremP st i K m then inv_localPathPA ar hA i.number i.p hm K m hK.1 hK.2 h.hcdt h.hbuf + if h : DelPremP st i K m then inv_localPathPA ar hA i.number i.p hm K m hK.1 hK.2 invCdtJ_prePA + (invBufJ8_prePA (Cs := i.Cs) (Ω := i.Ω) (Sg := i.Sg) (Ac := i.Ac) (Locks := i.Locks)) (del_hufPA h.ul h.cs (kd_of_kernelReadsU h.ul h.rd)) else LocalPath.void (fl4DfltA st ar hm) (OwnMap.ko_aligned hK.1.base.base hK.2.base.base m) diff --git a/proofs/Fl4ProofsA64/P2/GateBootA.lean b/proofs/Fl4ProofsA64/P2/GateBootA.lean new file mode 100644 index 000000000..fb6231d5f --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/GateBootA.lean @@ -0,0 +1,158 @@ +/- +# P2 (sync, gate gen 4): **Half 1 and Half 2 on `fl4LocPBL`**, from the boot witnesses (DESIGN §9 item 4) + +The Architect's ruling of 19:58Z, item 3: P2-Sync gen 5's top family `fl4LocPBL` (`P2/DelLkA.lean`: `fl4LocPB` with +the Delete-with-PTE slot started at its own locks' `CdtAtL`, no all-slot `MdbLk` / `NullLk`) is the AArch64 top family +of both halves. This module restates on it what gen 5 stated on `fl4LocPB`, and wires the runtime (`GateExecA`) onto +it: + +* **`serializable_fl4_localVPL_buf`** (`serializable_fl4_localVP_buf` for `fl4LocPBL`: `BufSH` of the memory along an + execution, `hSerB` as `bufSH_exec`) and **`delSlotPL_startAtB`** (`delSlotPL_startAt` with the [J] start fact `bufU` + from `BufSH` of the memory the start agrees with, `bufU_start`). +* **`w0_serial_mdbL`** / **`w1_serial_mdbL`**: `w0_serial_mdb` / `w1_serial_mdb` (`P2/MdbBootP.lean`) for + `fl4LocPBL` (`serializable_fl4_localVPL_mdb`; `hSer` as there). +* **`w0_serializable_impl`** / **`w1_serializable_impl`**: `GateExecA.serializable_implPA` for `fl4LocPBL` under + `EpochAP` from a runtime booted at `w0` / `w1` (its memory the witness's, its ghost maps the witness layout's): + `ROKP` and the epoch predicate at boot by `boot_epochAP` / `w1_epochAP`. + +Hypotheses `hSer` / `hSerB` (serial entries keep `MdbSH` / `BufSH`: the serial kernel's P1 / S1A obligations, as +ruled); `BufSH` at `w0` / `w1` is S1A / Boot's (a hypothesis `hBu0` of `serializable_fl4_localVPL_buf`). No [J], [P] +or [W] beyond the restated theorems'. New module; no existing statement changes. +-/ +import Fl4ProofsA64.P2.DelLkA +import Fl4ProofsA64.P2.MdbBootP +import Fl4ProofsA64.P2.GateExecA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA +open Fl4ProofsA64.P2.OwnMap (KLayout region own InW slotOwner) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP ownP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA MapsPA GateEpPA Fl4StepPA ExecPA StartAtEP exec_safeRunPA) +open Fl4ProofsA64.P2.E2EP Fl4ProofsA64.P2.AvoidWA +open Fl4ProofsA64.P2.S1b (KernDetA LkKernelReadsA) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv Perm Held held_disj) + +noncomputable section +open Classical + +section fl4 +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **`BufU` of the memory along an execution of `fl4LocPBL`** (`serializable_fl4_localVP_buf` for `fl4LocPBL`; +`hSerB` as `bufSH_exec`). -/ +theorem serializable_fl4_localVPL_buf {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (hBu0 : BufSH st K0.toKLayout s0.mem) + (hSerB : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, + ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K → Step s c s' → + Fl4StepPA (EpochAP st) (fl4LocPBL st ar hA hm) s K c s' K' → E2E.SerStep s c → + BufSH st K.toKLayout s.mem → BufSH st K'.toKLayout s'.mem) + (h : ExecPA (EpochAP st) (fl4LocPBL st ar hA hm) s0 K0 s K) : BufSH st K.toKLayout s.mem := + bufSH_exec (fun _ _ he => he.vmLay) h0 hA0 hB0.rok hM0 hB0 hBu0 hSerB h + +variable {st ar hA hm} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} {c : U64} {hs : U64 → Prop} {μ : U64 → U64} + +/-- **Starting the Delete-with-PTE slot of `fl4LocPBL` with `BufU` from `BufSH`** (`delSlotPL_startAt` with +`bufU_start`). Start facts: `InvJA`, `InvQFP`, [P] `CurUnbound`, `CdtAtL` (from the execution: +`delSlotPL_startExec`), and `BufSH` of the memory the start agrees with. -/ +theorem delSlotPL_startAtB {i : InvArgP S} (h : DelPremP st i K m) {e : U64 → U64} (he : EpochAP st K e) + (hBu : BufSH st K.toKLayout μ) (σ : S) (a : A) + (hJ : InvJA (L := L) K.toKLayout σ) (hQ : InvQFP st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K.toKLayout m σ) + (hub : CurUnbound K.vcpuOf (L.curW σ)) (hcd : CdtAtL st K.toKLayout (i.Locks σ) σ) + (hc : L.cpu σ = c) (hl : i.Locks σ = hs) (hμ : ∀ x, Aligned x → x ∉ K.user → L.mem σ x = μ x) : + ∃ q, StartAtEP (EpochAP st) (delSlotPL st ar hA hm i K m hK) c hs μ q := + delSlotPL_startAt (ar := ar) (hA := hA) (hm := hm) (hK := hK) h σ a hJ hQ hub hcd + (bufU_start he.vmLay hBu hμ) hc hl hμ + +end fl4 + +end + +end Fl4ProofsA64.P2.E2EVA + +namespace Fl4ProofsA64.P2.GateBootA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit +open Fl4ProofsA64.P2.BootEpP (bootKP w1KP boot_epochAP w1_epochAP) +open Fl4ProofsA64.P2.E2EVA (EpochAP fl4LocPBL MdbSH serializable_fl4_localVPL_mdb) +open Fl4ProofsA64.P2.E2EPA (MapsPA ExecPA Fl4StepPA) +open Fl4ProofsA64.P2.GateExecA (ExecPAR IExecPA ghP bootCfg serializable_implPA) +open Fl4Proofs.P2.GateImpl (ICfg IInit IPc Rel) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv NoLoc InvR RunAll) + +noncomputable section + +variable {A : Type} {ar : machine.Arch A} {hA : P0.ArchTotal ar} {hm : Fl4ProofsA64.P2.E2E.MasksWr bst} + +section w0 +variable {kb ib r now mt : U64} {nc : Nat} (P : BootLay kb ib nc) +include P + +/-- **`MdbS` of the history from `w0`, for `fl4LocPBL`** (`w0_serial_mdb` at `serializable_fl4_localVPL_mdb`). -/ +theorem w0_serial_mdbL {s0 s : Cfg U64 U64 U64 U64} {K : Fl4ProofsA64.P2.OwnMapP.KLayoutP} (h0 : Init s0) + (hA0 : ArmInv s0) (hmem : s0.mem = (bootS kb ib nc r now mt).m) (hM0 : MapsPA (bootKP kb ib nc r now mt) s0) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : Fl4ProofsA64.P2.OwnMapP.KLayoutP} {c : U64}, + ExecPA (EpochAP bst) (fl4LocPBL bst ar hA hm) s0 (bootKP kb ib nc r now mt) s K → Step s c s' → + Fl4StepPA (EpochAP bst) (fl4LocPBL bst ar hA hm) s K c s' K' → Fl4ProofsA64.P2.E2E.SerStep s c → + MdbSH bst K.toKLayout s.mem → MdbSH bst K'.toKLayout s'.mem) + (h : ExecPA (EpochAP bst) (fl4LocPBL bst ar hA hm) s0 (bootKP kb ib nc r now mt) s K) : + ∃ H R, InvR s0.mem s H R ∧ MdbSH bst K.toKLayout H ∧ (NoLoc s → H = s.mem) := + serializable_fl4_localVPL_mdb bst ar hA hm h0 hA0 (hmem ▸ boot_epochAP P) hM0 + ⟨bootS kb ib nc r now mt, fun b _ _ => by rw [hmem]; rfl, (boot_mdbST (r := r) (now := now) (mt := mt) P).mdbS⟩ + hSer h + +/-- **The runtime booted at `w0` is serializable** (`serializable_implPA` for `fl4LocPBL` under `EpochAP`): its +memory the witness's, its ghost maps `bootKP`'s. -/ +theorem w0_serializable_impl {E : Type} [DecidableEq E] {idx : U64 → E} {maxHeld : ℕ} + {ι0 ι : ICfg U64 U64 U64 U64 E} {K : Fl4ProofsA64.P2.OwnMapP.KLayoutP} + (hι : IInit ι0) (htb : ∀ e, ι0.tbl e = none) (hmem : ι0.mem = (bootS kb ib nc r now mt).m) + (hgh : ι0.gh = ghP (bootKP kb ib nc r now mt) ι0.mem) + (h : IExecPA (EpochAP bst) (fl4LocPBL bst ar hA hm) idx maxHeld ι0 (bootKP kb ib nc r now mt) ι K) + (hq : ∀ d, ¬ (ι.pc d).isLoc) (hpd : ∀ d, (ι.pc d).pend = none) : + ∃ s : Cfg U64 U64 U64 U64, ExecPAR (EpochAP bst) (fl4LocPBL bst ar hA hm) (bootCfg ι0) + (bootKP kb ib nc r now mt) s K ∧ Rel [] ι s ∧ RunAll s.log ι0.mem ι.mem := + serializable_implPA _ hι htb (boot_epochAP (r := r) (now := now) (mt := mt) P).rok hgh + (hmem ▸ boot_epochAP P) h hq hpd + +end w0 + +section w1 +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} (P : BootLay1 kb ib nc) +include P + +/-- **`MdbS` of the history from `w1`, for `fl4LocPBL`** (`w1_serial_mdb` at `serializable_fl4_localVPL_mdb`). -/ +theorem w1_serial_mdbL {s0 s : Cfg U64 U64 U64 U64} {K : Fl4ProofsA64.P2.OwnMapP.KLayoutP} (h0 : Init s0) + (hA0 : ArmInv s0) (hmem : s0.mem = (w1S kb ib nc r now mt sl uw).m) + (hM0 : MapsPA (w1KP kb ib nc r now mt sl uw) s0) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : Fl4ProofsA64.P2.OwnMapP.KLayoutP} {c : U64}, + ExecPA (EpochAP bst) (fl4LocPBL bst ar hA hm) s0 (w1KP kb ib nc r now mt sl uw) s K → Step s c s' → + Fl4StepPA (EpochAP bst) (fl4LocPBL bst ar hA hm) s K c s' K' → Fl4ProofsA64.P2.E2E.SerStep s c → + MdbSH bst K.toKLayout s.mem → MdbSH bst K'.toKLayout s'.mem) + (h : ExecPA (EpochAP bst) (fl4LocPBL bst ar hA hm) s0 (w1KP kb ib nc r now mt sl uw) s K) : + ∃ H R, InvR s0.mem s H R ∧ MdbSH bst K.toKLayout H ∧ (NoLoc s → H = s.mem) := + serializable_fl4_localVPL_mdb bst ar hA hm h0 hA0 (hmem ▸ w1_epochAP P) hM0 + ⟨w1S kb ib nc r now mt sl uw, fun b _ _ => by rw [hmem]; rfl, + (w1_mdbST (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).mdbS⟩ + hSer h + +/-- **The runtime booted at `w1` is serializable** (`serializable_implPA` for `fl4LocPBL` under `EpochAP`): its +memory the witness's, its ghost maps `w1KP`'s. -/ +theorem w1_serializable_impl {E : Type} [DecidableEq E] {idx : U64 → E} {maxHeld : ℕ} + {ι0 ι : ICfg U64 U64 U64 U64 E} {K : Fl4ProofsA64.P2.OwnMapP.KLayoutP} + (hι : IInit ι0) (htb : ∀ e, ι0.tbl e = none) (hmem : ι0.mem = (w1S kb ib nc r now mt sl uw).m) + (hgh : ι0.gh = ghP (w1KP kb ib nc r now mt sl uw) ι0.mem) + (h : IExecPA (EpochAP bst) (fl4LocPBL bst ar hA hm) idx maxHeld ι0 (w1KP kb ib nc r now mt sl uw) ι K) + (hq : ∀ d, ¬ (ι.pc d).isLoc) (hpd : ∀ d, (ι.pc d).pend = none) : + ∃ s : Cfg U64 U64 U64 U64, ExecPAR (EpochAP bst) (fl4LocPBL bst ar hA hm) (bootCfg ι0) + (w1KP kb ib nc r now mt sl uw) s K ∧ Rel [] ι s ∧ RunAll s.log ι0.mem ι.mem := + serializable_implPA _ hι htb (w1_epochAP (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).rok hgh + (hmem ▸ w1_epochAP P) h hq hpd + +end w1 + +end + +end Fl4ProofsA64.P2.GateBootA diff --git a/proofs/Fl4ProofsA64/P2/GateExecA.lean b/proofs/Fl4ProofsA64/P2/GateExecA.lean new file mode 100644 index 000000000..01cc0cf4a --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/GateExecA.lean @@ -0,0 +1,603 @@ +/- +# P2 (sync, gate gen 3): **the runtime's executions are FL4 executions** (`serializable_implPA`, DESIGN §9 item 4, Half 2) + +`Fl4P2Base/GateImpl.lean` / `GateLock.lean` transcribe `fl4-sync`'s gate and `fl4-store`'s locks (`ICfg`, `IStep`) +and match every runtime step by zero or one weak model step (`impl_sim`, `StepR`). This module closes the +connection to the AArch64 end-to-end chain (`E2EPA.lean`'s `ExecPA`), with racy reads returning stale values +(**[P] MM-RACY**), following the Architect's ruling of 19:10Z (option (a), `SafeW`): + +* **`body_safeW`** / **`LocalPath.safeW`**: a started body is `SafeW` (`Fl4P2Base/SafeW.lean`) for the untracked words + `U := User` (`· ∈ K.user`), from the same per-path obligations as `E2E.body_safe`: its proof with `AdmW` for `Adm` + (the certificate's admissible memories pinned to the real one only on readable non-user words; `body_safe` uses + `Adm`'s first clause only on non-user words, through `RpOf` and the declined run's kernel words). +* **`ExecPAR`**: `ExecPA` over `StepR (· ∈ K.user)` (the same `Fl4StepPA` per core step, `HwStepPA` per hardware + step); **`exec_safeRunPAR`** (every step `Allowed`, `XGReachR`; `ROKP`, `MapsPA` and the epoch agreement kept) and + **`serializable_e2ePAR`** (`serializable_e2ePA` for it). +* **`IFl4StepPA`**: the premises of a *runtime* step, stated on the runtime alone (its phases through `phOf`, its + memory, ghost maps and taken objects): `arm` [P], `start` [P], `ser` [J] (at a release's decisive store, `IDec`, + the ghost maps the body installed are the new layout's), `keep`. **`IExecPA`**: runtime executions with them. +* **`impl_execPA`**: every `IExecPA` from boot is matched by an `ExecPAR` from the related model state, ending in + states related by `Rel` with `LkInv`. **`serializable_implPA`**: where the runtime has no local path in progress + and no serial body pending, its memory is the serial run of the completed entries from the boot memory. + +## Named premises + +* Per runtime step, `IFl4StepPA`: `arm` [P] (an armed program is pure glue: fl4-sync's local attempts), `start` [P] + (`StartAtEP`: the glue starts one of the paths on its own core's state after its `Pre`), `ser` [J] (a serial entry + leaves a layout with `ROKP`, its ghost maps that layout's, and the epoch predicate at its memory), `keep` (the + layout is ghost state only serial entries change). They are `Fl4StepPA`'s, moved to the runtime. +* Per path, `LocalPath`'s ([P]/[W], unchanged). +* The four [P]s of the transcription (`GateImpl.lean`): MM-SB, MM-DRF, MM-RACY, IPI. + +No new premise; no [W] added. + +## Gate gen 4 (the Architect's ruling of 19:58Z) + +* **Hardware writes** (gap 1): `IExecPA.hw` is a runtime hardware write (`GateImpl.IStepHw`) whose word's lock no + core's path took, with **`IHwPA`** (`HwStepPA`'s [P] on the memory alone) at the runtime's layout. With no serial + body pending it is one `ExecPAR.hw` step at once (`Rel.hwFree`); while one is pending it is deferred (`Rel.hwDefer`: + appended to `Rel`'s queue `Qh`, no model step), and at the release's decisive store the simulation takes + `1 + Qh.length` steps: the model's `serial`, then each deferred write as an `ExecPAR.hw` step at the memory it was + made at (`replay_execPAR`). No restriction on when guest or MMU writes happen. +* So that a deferred write is checked against the layout the body leaves, a runtime execution switches its layout at + the serial body (`IBody`), not at the release: `IFl4StepPA.ser` [J] is stated there (changed statement), and + **`PendPA`** records what the simulation keeps while a body is pending (the model at the old layout; the runtime's + layout with `ROKP`, the body's ghost maps and the epoch predicate at the memory the body left, and every deferred + write's `IHwPA`). `impl_execPA` concludes `∃ s Km Qh, ExecPAR … s Km ∧ Rel Qh ι s ∧ LkInv idx ι ∧ PendPA …`; + `serializable_implPA` concludes `Rel [] ι s` (changed statements). +* **Racy reads of gate- or core-class words** (gap 2, option (a)): `IStepG.rdsC` / `StepR.rdsC` (a value of the + current class), covered by `SafeW`'s widened `RdW`. +-/ +import Fl4P2Base.SafeW +import Fl4P2Base.GateLock +import Fl4ProofsA64.P2.E2EPA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P2.E2E +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2.PathObl +open Fl4Proofs.P2.Serial (Owner ClsOwner Prog HeldV PO MoveV SafeW MoveHeld AltRun AdmW Diff RInv WrOK) + +noncomputable section +open Classical + +section body +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} + {ko : U64 → ClsOwner U64} {User : U64 → Prop} + {S A X : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + {F : ∀ {A : Type}, machine.Arch A → machine.Store S → S → A → Result (X × S × A)} + {p : SProg X} {Acc : X → Prop} {Locks : S → U64 → Prop} {Pre : S → Prop} + {RS WS DS : S → U64 → Prop} + +/-- **A body is `SafeW` when it starts** (`body_safe` for weak safety, the untracked words being the user words): +the certificate for every memory equal to the real one on the readable non-user words. -/ +theorem body_safeW (hL : MoveHeld own) (hO : OwnDep own User) + (hp : IsProgOn (F ar) ar st (AltOf st Pre RS cls ko User) p) + (O : PathObl st (F ar) Acc own cls ko User Locks Pre RS WS DS) + (Sd : PathSide st (F ar) cls ko User Locks Pre RS WS DS) + (hN : ReadOnce st ar p cls ko User Pre RS) (hT : ∀ s a, ∃ r, F ar st s a = ok r) + (hmk : MasksWr st) {σ : S} {a : A} (hpre : Pre σ) {μ : U64 → U64} + (hμ : ∀ x, Aligned x → ¬ User x → L.mem σ x = μ x) : + SafeW own cls ko User (L.cpu σ) (Locks σ) μ + (tr (RS σ) User cls ko (L.cpu σ) (L.ksb σ) (masksOf st σ) ar p ∅ a) := by + have e0 : own (L.mem σ) = own μ := hO _ _ hμ + let Hp : U64 → Prop := fun x => ¬ User x ∧ (WS σ x ∨ DS σ x) + have hRI : RInv own (L.cpu σ) (Locks σ) (RpOf (RS σ) User Hp) Hp μ := by + have hH : ∀ x, Hp x → HeldV (L.cpu σ) (Locks σ) (own μ x) := fun x hx => by + rw [← e0]; exact heldBy_iff.mp (O.write_own σ x hpre hx.2 hx.1) + refine ⟨hH, fun x hx => ?_⟩ + rcases hx with ⟨hu, hr⟩ | hx + · rw [← e0] + exact (O.read_own σ x hpre hr hu).imp_left heldBy_iff.mp + · exact Or.inl (hH x hx) + refine Fl4Proofs.P2.Serial.safe_of_runsW hL (Rp := RpOf (RS σ) User Hp) (Hp := Hp) + (fun x hx => Or.elim hx And.left And.left) (fun x hx => And.left hx) _ ∅ μ (fun _ => none) + ⟨fun ν hν => ?_, fun y u hu => (by cases hu), fun y u hu => (by cases hu), + fun y u hu => (by cases hu), hRI⟩ + -- `hν.1` is used on non-user words only + have hνR : ∀ x, ¬ User x → RS σ x → ν x = μ x := fun x hu hr => hν.1 x (hRI.2 x (Or.inl ⟨hu, hr⟩)) hu + generalize hq : tr (RS σ) User cls ko (L.cpu σ) (L.ksb σ) (masksOf st σ) ar p ∅ a = q + let rl := (reads q ν).filter (fun x => decide (Aligned x)) + let ul := rl.filter (fun x => decide (User x)) + let kl := rl.filter (fun x => decide (¬ User x)) + have hul : ∀ x, x ∈ ul ↔ (x ∈ reads q ν ∧ Aligned x) ∧ User x := by + intro x; simp only [ul, rl, List.mem_filter, decide_eq_true_eq] + have hkl : ∀ x, x ∈ kl ↔ (x ∈ reads q ν ∧ Aligned x) ∧ ¬ User x := by + intro x; simp only [kl, rl, List.mem_filter, decide_eq_true_eq] + obtain ⟨σu, hσu⟩ : ∃ s, s = wrs st ν ul σ := ⟨_, rfl⟩ + obtain ⟨σν, hσν⟩ : ∃ s, s = wrs st ν kl σu := ⟨_, rfl⟩ + obtain ⟨uP, uL, uR, uW, uD⟩ := wrs_user st Sd (ν := ν) ul σ + (fun x hx => ⟨((hul x).mp hx).2, ((hul x).mp hx).1.2⟩) hpre + obtain ⟨uc, ub, um⟩ := wrs_const st (ν := ν) hmk ul σ + have uM := wrs_mem st (ν := ν) ul σ (fun x hx => ((hul x).mp hx).1.2) + rw [← hσu] at uP uL uR uW uD uc ub um uM + obtain ⟨kc, kb, km⟩ := wrs_const st (ν := ν) hmk kl σu + have kM := wrs_mem st (ν := ν) kl σu (fun x hx => ((hkl x).mp hx).1.2) + rw [← hσν] at kc kb km kM + have hmσ := masksOf_eq st σ + have uMk : ∀ x, Aligned x → ¬ User x → L.mem σu x = μ x := fun x hx hu => by + rw [uM x hx, if_neg (fun h => hu ((hul x).mp h).2)]; exact hμ x hx hu + obtain ⟨ru, hru⟩ := hT σu a + by_cases hacc : Acc ru.1 + · -- accepted at `σu`: checked at the real memory + refine ⟨μ, fun y hy => absurd rfl hy, ?_⟩ + have gu : AltOf st Pre RS cls ko User σu := AltOf.pre uP + have hW := O.acc σu a ru uP hru hacc + rw [hp _ (Narrow.guard _ _ _) σu gu a] at hW + have hA := Sd.read_aligned σu σu a ru uP (Alt.refl σu) hru + rw [hp _ (Narrow.guard _ _ _) σu gu a] at hA + have hM := O.move_steps hp uP gu hru + have hO1 := hN σu σu a ru uP (Alt.refl σu) (by rw [← hp _ (Narrow.self st) σu gu a]; exact hru) + have hW := (interp_guard p σu a ru hW).2 + rw [uR] at hW hO1; rw [uW] at hW; rw [uc, uL] at hM + rw [← hq] + refine sim (L := L) hO hmk (Rr := RS σ) (Wr := fun x => True ∧ WS σ x) ?_ p σu a ∅ ∅ ∅ ν μ + (within_and p σu a (interp_guard p σu a ru hA).2 hW) hM hO1 + (fun x hx => absurd hx (Set.notMem_empty x)) subset_rfl + (um.trans hmσ) ub uc (fun x hx hu => (uMk x hx hu).symm) ?_ + · exact fun x hx => ⟨O.aligned σ x hpre (Or.inl hx.2), fun hu => ⟨hu, Or.inl hx.2⟩⟩ + · rw [hq] + intro x hx hxa hxr _ + by_cases hu : User x + · rw [uM x hxa, if_pos ((hul x).mpr ⟨⟨hx, hxa⟩, hu⟩)] + · rw [uMk x hxa hu, hνR x hu hxr] + · -- declined at `σu`: checked at the alternative state's memory + have hAlt : Alt st (RS σu) cls ko User σu σν := by + refine ⟨kc, kb, km, fun x hx => ?_, fun x hx hu hko => ?_⟩ + · have hxa := Sd.rs_aligned σu x uP hx + rw [uR] at hx + rw [kM x hxa] + split_ifs with h + · rw [hνR x ((hkl x).mp h).2 hx, uMk x hxa ((hkl x).mp h).2] + · rfl + · have hxa : Aligned x := Sd.ko_aligned x (by rcases hko with h | h <;> rw [h] <;> simp) + rw [kM x hxa] + split_ifs with h + · rw [uc] at hko + rw [hν.2.2 x hko, uMk x hxa hu] + · rfl + obtain ⟨rν, hrν⟩ := hT σν a + have gν : AltOf st Pre RS cls ko User σν := ⟨σu, uP, hAlt⟩ + obtain ⟨-, hW, hM⟩ := O.dec_alt σu a ru uP hru hacc σν rν hAlt hrν + rw [hp _ (Narrow.guard _ _ _) σν gν a] at hW + rw [hp _ (Narrow.mguard _ _) σν gν a] at hM + have hA := Sd.read_aligned σu σν a rν uP hAlt hrν + rw [hp _ (Narrow.guard _ _ _) σν gν a] at hA + have hO1 := hN σu σν a rν uP hAlt (by rw [← hp _ (Narrow.self st) σν gν a]; exact hrν) + have hW := (interp_guard p σν a rν hW).2 + have hM := (interp_mguard p σν a rν hM).2 + rw [uR] at hO1; rw [uD] at hW; rw [uc, uL] at hM + refine ⟨fun x => if Aligned x ∧ ¬ User x then L.mem σν x else μ x, fun y hy hpo => hy ?_, ?_⟩ + · beta_reduce + split_ifs with h + · rw [kM y h.1, uMk y h.1 h.2] + split_ifs + · exact hν.1 y hpo h.2 + · rfl + · rfl + · rw [← hq] + refine sim (L := L) hO hmk (Rr := fun _ => True) (Wr := fun x => True ∧ DS σ x) ?_ p σν a + ∅ ∅ ∅ ν _ (within_and p σν a (interp_guard p σν a rν hA).2 hW) hM hO1 + (fun x hx => absurd hx (Set.notMem_empty x)) + subset_rfl (km.trans (um.trans hmσ)) (kb.trans ub) (kc.trans uc) + (fun x hx hu => if_pos ⟨hx, hu⟩) ?_ + · exact fun x hx => ⟨O.aligned σ x hpre (Or.inr hx.2), fun hu => ⟨hu, Or.inr hx.2⟩⟩ + · rw [hq] + intro x hx hxa _ _ + rw [kM x hxa] + by_cases hu : User x + · rw [if_neg (fun h => ((hkl x).mp h).2 hu), uM x hxa, if_pos ((hul x).mpr ⟨⟨hx, hxa⟩, hu⟩)] + · rw [if_pos ((hkl x).mpr ⟨⟨hx, hxa⟩, hu⟩)] + +end body + +/-- **A started local path is `SafeW`** (`LocalPath.safe` for weak safety). -/ +theorem LocalPath.safeW {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} + {ko : U64 → ClsOwner U64} {User : U64 → Prop} (P : LocalPath own cls ko User) (hL : MoveHeld own) + (hO : OwnDep own User) {c : U64} {hs : U64 → Prop} {μ : U64 → U64} {q : SP} + (h : StartAt P c hs μ q) : SafeW own cls ko User c hs μ q := by + obtain ⟨σ, a, hpre, rfl, rfl, hμ, rfl⟩ := h + let _ := P.L + exact body_safeW hL hO P.isProg P.obl P.side P.once P.total P.masks hpre hμ + +end +end Fl4ProofsA64.P2.E2E + +namespace Fl4ProofsA64.P2.GateExecA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2E Fl4ProofsA64.P2.E2EPA + Fl4ProofsA64.P2.OwnMapP +open Fl4Proofs.P2.Serial (Owner ClsOwner Cfg Step StepR Init ArmInv NoLoc RunAll PureGlue Allowed Phase + hwCfg SafeW SInvW XGReachR allowed_of_sinvW sinv_stepRW sinvW_hw sinvW_noLoc no_overlapXR + serializable_quiescentXR heldV_congr) +open Fl4Proofs.P2.GateImpl + +noncomputable section +open Classical + +/-! ## `ExecPA` with weak racy reads -/ + +/-- **An execution of FL4 whose racy reads may return stale values** (`ExecPA` over `StepR (· ∈ K.user)`, **[P] +MM-RACY**), from `s0` with layout `K0`, under the epoch predicate `Q`. -/ +inductive ExecPAR (Q : KLayoutP → (U64 → U64) → Prop) {J : Type} (LP : PathsPA J) (s0 : Cfg U64 U64 U64 U64) + (K0 : KLayoutP) : Cfg U64 U64 U64 U64 → KLayoutP → Prop + | refl : ExecPAR Q LP s0 K0 s0 K0 + | step {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64} : + ExecPAR Q LP s0 K0 s K → StepR (· ∈ K.user) s c s' → Fl4StepPA Q LP s K c s' K' → + ExecPAR Q LP s0 K0 s' K' + | hw {s : Cfg U64 U64 U64 U64} {K : KLayoutP} {v a x : U64} : + ExecPAR Q LP s0 K0 s K → HwStepPA K s v a x → ExecPAR Q LP s0 K0 (hwCfg s a x) K + +section exec +variable {K : KLayoutP} {U : U64 → Prop} + +theorem armInv_stepR {s s' : Cfg U64 U64 U64 U64} {c : U64} (I : ArmInv s) (hst : StepR U s c s') + (h : ∀ p, s'.ph c = .arm p → PureGlue p) : ArmInv s' := by + intro d p hd + by_cases hdc : d = c + · subst hdc; exact h p hd + · cases hst with + | step hst => rw [Fl4Proofs.P2.Serial.step_ph_other hst hdc] at hd; exact I d p hd + | rdsW _ _ _ _ _ => exact I d p ((Function.update_of_ne hdc _ _).symm.trans hd) + | rdsC _ _ _ _ _ => exact I d p ((Function.update_of_ne hdc _ _).symm.trans hd) + +/-- A weak step under a closed gate with no local path leaves no local path. -/ +theorem noLoc_stepR {s s' : Cfg U64 U64 U64 U64} {c : U64} (hq : NoLoc s) (hg : s.gate = .closed) + (hst : StepR U s c s') : NoLoc s' := by + have loc : ∀ {p0 q W}, s.ph c = .loc p0 q W → False := fun hc => hq c (by rw [hc]; trivial) + intro d hd + by_cases hdc : d = c + · subst hdc + cases hst with + | rdsW _ hc => exact loc hc + | rdsC _ hc => exact loc hc + | step hst => + cases hst with + | enter _ hgo => rw [hg] at hgo; cases hgo + | rd hc => exact loc hc + | rds hc => exact loc hc + | rdc hc => exact loc hc + | start hc => exact loc hc + | wr hc => exact loc hc + | lock _ hc _ => exact loc hc + | scan _ _ _ _ => exact hq d hd + | drain _ _ _ => exact hq d hd + | _ => simp [Phase.IsLoc] at hd + · cases hst with + | rdsW _ _ _ _ _ => exact hq d (by simpa [Function.update_of_ne hdc] using hd) + | rdsC _ _ _ _ _ => exact hq d (by simpa [Function.update_of_ne hdc] using hd) + | step hst => exact hq d (by rwa [Fl4Proofs.P2.Serial.step_ph_other hst hdc] at hd) + +/-- `maps_stepPA` for a weak step. -/ +theorem maps_stepPAR (hK : ROKP K) {s s' : Cfg U64 U64 U64 U64} {c : U64} (hM : MapsPA K s) + (hal : Allowed s c) (hst : StepR U s c s') (hn : ¬ SerStep s c) : MapsPA K s' := by + cases hst with + | step hst => exact maps_stepPA hK hM hal hst hn + | rdsW _ _ _ _ _ => exact ⟨hM.own, hM.cls, hM.ko⟩ + | rdsC _ _ _ _ _ => exact ⟨hM.own, hM.cls, hM.ko⟩ + +theorem epAgP_stepR {e : U64 → U64} {s s' : Cfg U64 U64 U64 U64} {c : U64} (hM : MapsPA K s) + (hal : Allowed s c) (hst : StepR U s c s') (hn : ¬ SerStep s c) (hE : EpAgP K e s.mem) : + EpAgP K e s'.mem := by + cases hst with + | step hst => exact epAgP_step hM hal hst hn hE + | rdsW _ _ _ _ _ => exact hE + | rdsC _ _ _ _ _ => exact hE + +/-- **Every step of an `ExecPAR` is `Allowed`** (`exec_safeRunPA` for weak steps): the started bodies are `SafeW` +(`LocalPath.safeW`), and `ROKP`, the ghost maps and the epoch agreement are kept. -/ +theorem exec_safeRunPAR {Q : KLayoutP → (U64 → U64) → Prop} {J : Type} {LP : PathsPA J} + {s0 s : Cfg U64 U64 U64 U64} {K0 : KLayoutP} (h0 : Init s0) (hA0 : ArmInv s0) (hK0 : ROKP K0) + (hM0 : MapsPA K0 s0) (hQ0 : Q K0 s0.mem) (h : ExecPAR Q LP s0 K0 s K) : + XGReachR s0 s ∧ SInvW (· ∈ K.user) s ∧ ArmInv s ∧ ROKP K ∧ MapsPA K s ∧ ∃ e, Q K e ∧ EpAgP K e s.mem := by + induction h with + | refl => exact ⟨.refl, sinvW_noLoc h0.2, hA0, hK0, hM0, s0.mem, hQ0, EpAgP.refl _ _⟩ + | @step s s' K K' c _ hst hF ih => + obtain ⟨hr, hS, hA, hK, hM, e, hq, hE⟩ := ih + have hal := allowed_of_sinvW hS c + have hno := no_overlapXR h0 hr + have hS' : SInvW (· ∈ K.user) s' := sinv_stepRW hno hA (fun p0 k W hph => by + obtain ⟨i, σ, a, hp, hc, hl, hμ, hk⟩ := hF.start p0 k W hph hK + rw [hM.own, hM.cls, hM.ko] + exact (LP i K s.mem hK).safeW (moveHeldP hK.1) (ownDepP hK.1 hK.2) + ⟨σ, a, hp ⟨e, hq, fun x hx ha hu => (hμ x ha hu).trans (hE x hx).1⟩, hc, hl, hμ, hk⟩) hS hal hst + refine ⟨.step hr hal hst, ?_, armInv_stepR hA hst hF.arm, ?_⟩ + · by_cases hs : SerStep s c + · exact sinvW_noLoc (noLoc_stepR (hno hs.1) hs.1 hst) + · rw [hF.keep hs]; exact hS' + · by_cases hs : SerStep s c + · obtain ⟨h1, h2, h3⟩ := hF.ser hs + exact ⟨h1, h2, s'.mem, h3, EpAgP.refl _ _⟩ + · rw [hF.keep hs] + exact ⟨hK, maps_stepPAR hK hM hal hst hs, e, hq, epAgP_stepR hM hal hst hs hE⟩ + | hw _ hh ih => + obtain ⟨hr, hS, hA, hK, hM, e, hq, hE⟩ := ih + have hw := hh.hwWr hK hM + exact ⟨.hw hr hw, sinvW_hw hS hw, hA, hK, maps_hwPA hK hM hh, e, hq, epAgP_hw hK hh hE⟩ + +/-- **End to end with stale racy reads** (`serializable_e2ePA` for `ExecPAR`): where an execution whose racy reads +may return any value of the word reaches a state with no local path in progress, memory is the serial run of the +completed entries, hardware writes included, in completion order. Premises: `serializable_e2ePA`'s. -/ +theorem serializable_e2ePAR {Q : KLayoutP → (U64 → U64) → Prop} {J : Type} (LP : PathsPA J) + {s0 s : Cfg U64 U64 U64 U64} {K0 : KLayoutP} (h0 : Init s0) (hA0 : ArmInv s0) (hK0 : ROKP K0) + (hM0 : MapsPA K0 s0) (hQ0 : Q K0 s0.mem) (h : ExecPAR Q LP s0 K0 s K) (hq : NoLoc s) : + RunAll s.log s0.mem s.mem := + serializable_quiescentXR h0 (exec_safeRunPAR h0 hA0 hK0 hM0 hQ0 h).1 hq + +end exec + +/-! ## Runtime executions -/ + +variable {E : Type} [DecidableEq E] + +/-- The model state at boot for the runtime `ι0`: gate open, no holder, every core idle, no lock taken, the +runtime's memory and ghost maps, nothing completed. -/ +def bootCfg (ι0 : ICfg U64 U64 U64 U64 E) : Cfg U64 U64 U64 U64 := + { mem := ι0.mem, gate := .opn, holder := none, seen := ∅, lk := fun _ => none, ph := fun _ => .idle, + own := ι0.gh.1, cls := ι0.gh.2.1, ko := ι0.gh.2.2, log := [] } + +/-- The ghost maps of the layout `K` at memory `m`. -/ +def ghP (K : KLayoutP) (m : U64 → U64) : + ((U64 → U64) → U64 → Owner U64 U64) × (U64 → U64 → U64) × (U64 → ClsOwner U64) := + (ownP K, OwnMap.cls K.toKLayout, OwnMap.ko K.toKLayout m) + +/-- **The premises of a runtime step** of core `c` from `ι` with layout `K` to `ι'` with layout `K'`, on the +runtime alone (`Fl4StepPA`'s, through the model's view `phOf` of the runtime's phases): +* `arm` [P]: an armed program is pure glue (fl4-sync's `run_syscall` / `run_vm_fault` / `run_timer_irq` before + the body); +* `start` [P]: a body the path starts is one of the paths `LP`, started on its own core's state with the objects + it took, after `Pre` (`StartAtEP`); +* `ser` [J]: at the serial body (`IBody`, gate gen 4: the runtime's layout switches where the body runs, so that the + hardware writes before its release are checked against the layout it leaves) the layout `K'` has `ROKP`, the + ghost maps the body installed are `K'`'s, and the epoch predicate holds at the memory the body left; +* `keep`: no other step changes the layout. -/ +structure IFl4StepPA (Q : KLayoutP → (U64 → U64) → Prop) {J : Type} (LP : PathsPA J) + (ι : ICfg U64 U64 U64 U64 E) (K : KLayoutP) (c : U64) (ι' : ICfg U64 U64 U64 U64 E) (K' : KLayoutP) : + Prop where + arm : ∀ p, phOf ι'.closed (ι'.pc c) = .arm p → PureGlue p + start : ∀ p0 k W, phOf ι.closed (ι.pc c) = .loc p0 (.start k) W → ∀ (hK : ROKP K), + ∃ i, StartAtEP Q (LP i K ι.mem hK) c (· ∈ (ι.pc c).taken) ι.mem k + ser : IBody ι c ι' → ROKP K' ∧ ι'.gh = ghP K' ι'.mem ∧ Q K' ι'.mem + keep : ¬ IBody ι c ι' → K' = K + +/-- **A runtime hardware write's premise** at memory `m` (gate gen 4): `HwStepPA`'s [P] (`E2EVA.HwStepVA` at the +layout's rule-7/8 part) on the memory alone: while `v`'s bound guest runs, the hardware writes the kernel word +`a := x`, a change `VmRunFrameA` allows. (`HwStepVA`'s fourth field, no core holding `lock v`, is +`IExecPA.hw`'s `∀ d, v ∉ taken`.) -/ +structure IHwPA (K : KLayoutP) (m : U64 → U64) (v a x : U64) : Prop where + frame : Fl4ProofsA64.P2.AvoidWA.VmRunFrameA K.toKLayoutV.toKLayout K.toKLayoutV.vcpus K.toKLayoutV.vcpuOf v m + (Function.update m a x) + kern : a ∉ K.toKLayoutV.toKLayout.user + chg : x ≠ m a + +theorem IHwPA.toStep {K : KLayoutP} {m : U64 → U64} {v a x : U64} (h : IHwPA K m v a x) + {s : Cfg U64 U64 U64 U64} (hm : s.mem = m) (hl : s.lk v = none) : HwStepPA K s v a x := by + subst hm; exact ⟨h.frame, h.kern, h.chg, hl⟩ + +/-- **Executions of the runtime** (`IStep` for the layout's user words) whose steps satisfy `IFl4StepPA`, with the +hardware's writes (`IStepHw`, gate gen 4) where no core's path took the word's lock and `IHwPA` holds at the layout +the runtime is in (after a serial body, the layout it leaves: `IFl4StepPA.ser`). -/ +inductive IExecPA (Q : KLayoutP → (U64 → U64) → Prop) {J : Type} (LP : PathsPA J) (idx : U64 → E) + (maxHeld : ℕ) (ι0 : ICfg U64 U64 U64 U64 E) (K0 : KLayoutP) : ICfg U64 U64 U64 U64 E → KLayoutP → Prop + | refl : IExecPA Q LP idx maxHeld ι0 K0 ι0 K0 + | step {ι ι' : ICfg U64 U64 U64 U64 E} {K K' : KLayoutP} {c : U64} : + IExecPA Q LP idx maxHeld ι0 K0 ι K → IStep (· ∈ K.user) idx maxHeld ι c ι' → + IFl4StepPA Q LP ι K c ι' K' → IExecPA Q LP idx maxHeld ι0 K0 ι' K' + | hw {ι ι' : ICfg U64 U64 U64 U64 E} {K : KLayoutP} {v a x : U64} : + IExecPA Q LP idx maxHeld ι0 K0 ι K → IStepHw ι a x ι' → (∀ d, v ∉ (ι.pc d).taken) → + IHwPA K ι.mem v a x → IExecPA Q LP idx maxHeld ι0 K0 ι' K + +/-- The model holds `ser p` for the holder of the closed gate. -/ +theorem ser_ph {Qh : HwQ U64 U64 U64} {ι : ICfg U64 U64 U64 U64 E} {s : Cfg U64 U64 U64 U64} {c : U64} + (R : Rel Qh ι s) (hs : SerStep s c) : ∃ p, s.ph c = .ser p := by + rw [R.ph c] + exact holds_phOf ((R.hold c).mp hs.2) (R.gate.symm.trans hs.1) + +/-- **What the simulation keeps about the layouts** (gate gen 4): with no serial body pending the model's layout `Km` +is the runtime's `K`; while one is pending, the model is still before the body (at the old layout) and the runtime's +layout `K` is the one the body left: `ROKP`, the body's ghost maps and the epoch predicate at the memory the body +left (`hqBase`), and every deferred hardware write's premise at it. -/ +structure PendPA (Q : KLayoutP → (U64 → U64) → Prop) (ι : ICfg U64 U64 U64 U64 E) (K : KLayoutP) + (Qh : HwQ U64 U64 U64) (Km : KLayoutP) : Prop where + nopend : (∀ d, (ι.pc d).pend = none) → Km = K + pending : ¬ (∀ d, (ι.pc d).pend = none) → ROKP K ∧ ι.gh = ghP K (hqBase Qh ι.mem) ∧ Q K (hqBase Qh ι.mem) ∧ + ∀ w ∈ Qh, IHwPA K w.1 w.2.2.2 w.2.1 w.2.2.1 + +/-- The model's step matched to a runtime step that is not a serial entry's: `Fl4StepPA` at the model's layout. -/ +theorem fl4Step_of {Q : KLayoutP → (U64 → U64) → Prop} {J : Type} {LP : PathsPA J} {Qh Qh' : HwQ U64 U64 U64} + {ι ι' : ICfg U64 U64 U64 U64 E} {K K' Km : KLayoutP} {c : U64} {s s1 : Cfg U64 U64 U64 U64} + (R : Rel Qh ι s) (R1 : Rel Qh' ι' s1) (hF : IFl4StepPA Q LP ι K c ι' K') + (hKm : (ι.pc c).isLoc → Km = K) (hn : ¬ SerStep s c) : Fl4StepPA Q LP s Km c s1 Km := by + refine ⟨fun p hp => hF.arm p (by rw [← R1.ph c]; exact hp), fun p0 k W hph hK => ?_, fun hs => absurd hs hn, + fun _ => rfl⟩ + have hph' := (R.ph c).symm.trans hph + have hl : (ι.pc c).isLoc := isLoc_of_phOf (by rw [hph']; trivial) + obtain rfl := hKm hl + have hm : s.mem = ι.mem := R.memE (R.nopend hl) + obtain ⟨i, hi⟩ := hF.start p0 k W hph' hK + have e : (fun l => s.lk l = some c) = (· ∈ (ι.pc c).taken) := funext fun l => propext (R.lk l c) + exact ⟨i, by rw [hm, e]; exact hi⟩ + +/-- **Replaying the deferred hardware writes** at the release: each is an `ExecPAR.hw` step at the layout the body +left, at the memory it was made at, no lock being held. -/ +theorem replay_execPAR {Q : KLayoutP → (U64 → U64) → Prop} {J : Type} {LP : PathsPA J} + {s0 : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} {m m' : U64 → U64} {Qh : HwQ U64 U64 U64} + (hch : HwChain m Qh m') (hW : ∀ w ∈ Qh, IHwPA K w.1 w.2.2.2 w.2.1 w.2.2.1) : + ∀ {s1 : Cfg U64 U64 U64 U64}, s1.mem = m → (∀ l, s1.lk l = none) → ExecPAR Q LP s0 K0 s1 K → + ExecPAR Q LP s0 K0 (hwRun s1 Qh) K := by + induction hch with + | nil => intro s1 _ _ hx; exact hx + | @cons m m' a x l r _ ih => + intro s1 hm hl hx + have hw := hW (m, a, x, l) List.mem_cons_self + exact ih (fun w hw' => hW w (List.mem_cons_of_mem _ hw')) + (by show Function.update s1.mem a x = _; rw [hm]) hl (.hw hx (hw.toStep hm (hl l))) + +/-- A weak step's untracked words matter only at a speculative read, which only a local path makes. -/ +theorem stepR_U {U U' : U64 → Prop} {s s1 : Cfg U64 U64 U64 U64} {c : U64} (h : StepR U s c s1) + (hl : (s.ph c).IsLoc → U = U') : StepR U' s c s1 := by + cases h with + | step h => exact .step h + | rdsW v hc hp hk1 hk2 => + obtain rfl := hl (by rw [hc]; trivial) + exact .rdsW v hc hp hk1 hk2 + | rdsC v hc hp hk hcl => + obtain rfl := hl (by rw [hc]; trivial) + exact .rdsC v hc hp hk hcl + +/-- **`impl_execPA`**: every runtime execution from boot satisfying the step premises is matched by an execution +of FL4 with stale racy reads (`ExecPAR`) from the related model state, ending in states related by `Rel` (with the +hardware writes deferred since a pending body, `Qh`) with `LkInv`, the layouts as `PendPA` says. A runtime hardware +write is an `ExecPAR.hw` step at once when no body is pending, else deferred; at the release's decisive store the +model takes `serial` and then replays the deferred writes (`1 + Qh.length` steps; gate gen 4). -/ +theorem impl_execPA {Q : KLayoutP → (U64 → U64) → Prop} {J : Type} {LP : PathsPA J} {idx : U64 → E} + {maxHeld : ℕ} {ι0 ι : ICfg U64 U64 U64 U64 E} {K0 K : KLayoutP} {s0 : Cfg U64 U64 U64 U64} + (hι : IInit ι0) (htb : ∀ e, ι0.tbl e = none) (hg : s0.gate = .opn) (hh : s0.holder = none) + (hp : ∀ d, s0.ph d = .idle) (hl : ∀ o, s0.lk o = none) (hm : s0.mem = ι0.mem) (hgh : ghOf s0 = ι0.gh) + (h : IExecPA Q LP idx maxHeld ι0 K0 ι K) : + ∃ s Km Qh, ExecPAR Q LP s0 K0 s Km ∧ Rel Qh ι s ∧ LkInv idx ι ∧ PendPA Q ι K Qh Km := by + induction h with + | refl => + exact ⟨s0, K0, [], .refl, rel_init hι hg hh hp hl hm hgh, lkInv_init hι htb, + ⟨fun _ => rfl, fun h => absurd (fun d => by rw [hι.idle d]; rfl) h⟩⟩ + | @hw ι ι' K v a x _ hst hfree hH ih => + obtain ⟨s, Km, Qh, hx, R, I, P⟩ := ih + subst hst + by_cases hpn : ∀ d, (ι.pc d).pend = none + · have hQ := R.qE hpn + subst hQ + have hK := P.nopend hpn + rw [← hK] at hH + exact ⟨hwCfg s a x, Km, [], .hw hx (hH.toStep (R.memE hpn) (R.lk_free hfree)), R.hwFree hpn a x, I.hw _, + ⟨fun _ => hK, fun h => absurd hpn h⟩⟩ + · refine ⟨s, Km, Qh ++ [(ι.mem, a, x, v)], hx, R.hwDefer hpn a x v, I.hw _, + ⟨fun h => absurd h hpn, fun _ => ?_⟩⟩ + obtain ⟨hK, hG, hQ, hW⟩ := P.pending hpn + refine ⟨hK, (by rw [hqBase_snoc]; exact hG), (by rw [hqBase_snoc]; exact hQ), fun w hw => ?_⟩ + rcases List.mem_append.mp hw with hw | hw + · exact hW w hw + · rw [List.mem_singleton.mp hw]; exact hH + | @step ι ι' K K' c _ hst hF ih => + obtain ⟨s, Km, Qh, hx, R, I, P⟩ := ih + obtain ⟨s1, h1, hR, I'⟩ := impl_sim R I hst + have hoth : ∀ d, d ≠ c → ι'.pc d = ι.pc d := fun d hd => hst.pc_other hd + rcases IStep.pend_cls hst (R.gph c) with hB | ⟨hD, hb, ha⟩ | ⟨hnB, hnD, hiff⟩ + · -- the serial body: the runtime switches to the layout it leaves; the model stays before the body + have R1 : Rel Qh ι' s1 := hR.elim id fun h => absurd (pend_of_phOf_idle h.1.2.2) hB.2 + have hall : ∀ d, (ι.pc d).pend = none := by + intro d + by_cases hdc : d = c + · rw [hdc]; exact hB.1 + · cases hpd : (ι.pc d).pend with + | none => rfl + | some pm => + have hd' : (ι'.pc d).pend = some pm := by rw [hoth d hdc]; exact hpd + obtain ⟨pc', hpc'⟩ := Option.ne_none_iff_exists'.mp hB.2 + exact absurd (R1.uniq (IPc.pend_atSv hd') (IPc.pend_atSv hpc')) hdc + have hQ := R.qE hall + subst hQ + have hK := P.nopend hall + obtain ⟨hK', hG, hQ'⟩ := hF.ser hB + have hx' : ExecPAR Q LP s0 K0 s1 Km := by + rcases h1 with rfl | h1 + · exact hx + · refine .step hx (stepR_U h1 fun _ => by rw [hK]) (fl4Step_of R R1 hF (fun _ => hK) fun hs => hB.2 ?_) + exact pend_of_phOf_idle (serStep_dec R R1 h1 hs.1 hs.2).2.2 + exact ⟨s1, Km, [], hx', R1, I', ⟨fun h => absurd (h c) hB.2, fun _ => ⟨hK', hG, hQ', fun w hw => by cases hw⟩⟩⟩ + · -- the release's decisive store: `serial`, then the deferred hardware writes + have hpn : ¬ ∀ d, (ι.pc d).pend = none := fun h => hb (h c) + obtain ⟨hK, hG, hQ, hW⟩ := P.pending hpn + have hKK : K' = K := hF.keep fun h => hb h.1 + have hmg : ι'.mem = ι.mem ∧ ι'.gh = ι.gh := by + rcases hst.memgh with h | h | h + · exact h + · exact absurd (IPc.isLoc_pend h) hb + · exact absurd h.1 hb + have hsv : (ι.pc c).atSv := IPc.holds_atSv hD.1 + have hall : ∀ d, (ι'.pc d).pend = none := by + intro d + by_cases hdc : d = c + · rw [hdc]; exact ha + · rw [hoth d hdc]; exact R.pendOnly hsv d hdc + obtain ⟨p, hsp⟩ : ∃ p, s.ph c = .ser p := by rw [R.ph c]; exact holds_phOf hD.1 hD.2.1 + -- the model: `serial` from `s` to `s1` at the memory the body left, then the writes + obtain ⟨hst1, hmb, R2⟩ : StepR (· ∈ K.user) s c s1 ∧ s1.mem = hqBase Qh ι.mem ∧ + Rel [] ι' (hwRun s1 Qh) := by + rcases hR with R1 | ⟨-, hmb, R2⟩ + · have hQ0 := R1.qE hall + subst hQ0 + rcases h1 with rfl | h1 + · have := R1.ph c; rw [hsp, hD.2.2] at this; cases this + · exact ⟨h1, (by rw [R1.memE hall, hmg.1]; rfl), R1⟩ + · rcases h1 with rfl | h1 + · have := R2.ph c; rw [(hwRun_fix _ Qh).2.2.2.2.1, hsp, hD.2.2] at this; cases this + · exact ⟨h1, hmb, R2⟩ + have hS : SerStep s c := ⟨R.gate.trans hD.2.1, (R.hold c).mpr hD.1⟩ + have hf := hwRun_fix s1 Qh + have hgs : ghOf s1 = ghP K (hqBase Qh ι.mem) := by rw [← hf.2.2.2.2.2, R2.gh hall, hmg.2, hG] + have hMs : MapsPA K s1 := + ⟨congrArg Prod.fst hgs, congrArg (fun g => g.2.1) hgs, by rw [hmb]; exact congrArg (fun g => g.2.2) hgs⟩ + have hF1 : Fl4StepPA Q LP s Km c s1 K := + ⟨fun p' hp' => (by rw [stepR_ser_idle hsp hS.1 hst1] at hp'; cases hp'), + fun p0 k W hph => (by rw [hsp] at hph; cases hph), + fun _ => ⟨hK, hMs, by rw [hmb]; exact hQ⟩, fun h => absurd hS h⟩ + obtain ⟨⟨p', m0⟩, hpm⟩ := Option.ne_none_iff_exists'.mp hb + obtain ⟨-, mb, -, hch⟩ := R.memP c p' m0 hpm + have htk : ∀ d l, l ∉ (ι.pc d).taken := fun d l hl => R.clo hD.2.1 d (by + revert hl; cases ι.pc d <;> simp [IPc.taken, IPc.isLoc]) + have hlk : ∀ l, s1.lk l = none := fun l => by + rw [stepR_ser_lk hsp hst1]; exact R.lk_free fun d => htk d l + exact ⟨hwRun s1 Qh, K, [], + replay_execPAR hch hW (hmb.trans hch.base.symm) hlk (.step hx (stepR_U hst1 fun h => by + rw [hsp] at h; exact (Phase.ser_not_loc _ h).elim) hF1), + R2, I', ⟨fun _ => hKK.symm, fun h => absurd hall h⟩⟩ + · -- neither: the layout is kept, the pending status too + have hKK : K' = K := hF.keep hnB + have R1 : Rel Qh ι' s1 := hR.elim id fun h => absurd h.1 hnD + have hsame : ∀ d, ((ι'.pc d).pend = none ↔ (ι.pc d).pend = none) := fun d => by + by_cases hdc : d = c + · rw [hdc]; exact hiff + · rw [hoth d hdc] + have hKm : (ι.pc c).isLoc → Km = K := fun hl => P.nopend (R.nopend hl) + have hx' : ExecPAR Q LP s0 K0 s1 Km := by + rcases h1 with rfl | h1 + · exact hx + · refine .step hx (stepR_U h1 fun hl => ?_) + (fl4Step_of R R1 hF hKm fun hs => hnD (serStep_dec R R1 h1 hs.1 hs.2)) + rw [hKm (isLoc_of_phOf (by rw [← R.ph c]; exact hl))] + refine ⟨s1, Km, Qh, hx', R1, I', ⟨fun h => ?_, fun h => ?_⟩⟩ + · rw [hKK]; exact P.nopend fun d => (hsame d).mp (h d) + · have hpn : ¬ ∀ d, (ι.pc d).pend = none := fun h' => h fun d => (hsame d).mpr (h' d) + obtain ⟨hK, hG, hQ, hW⟩ := P.pending hpn + have hmg : ι'.mem = ι.mem ∧ ι'.gh = ι.gh := by + rcases hst.memgh with h' | h' | h' + · exact h' + · exact absurd (R.nopend h') hpn + · exact absurd h' hnB + rw [hKK, hmg.1, hmg.2]; exact ⟨hK, hG, hQ, hW⟩ + +/-- **The runtime is serializable**: from boot (`IInit`, the lock table clear, ghost maps of a layout `K0` with +`ROKP` [J] and the epoch predicate at the boot memory), wherever a runtime execution satisfying the step premises +(`IExecPA`, hardware writes included) reaches a state with no local path in progress and no serial body pending, its +memory is what running the completed entries one after another, in completion order, produces from the boot memory +(`s.log`, the hardware writes included as entries of their own; a speculative read returning any value, as in +`Serial`). -/ +theorem serializable_implPA {Q : KLayoutP → (U64 → U64) → Prop} {J : Type} (LP : PathsPA J) {idx : U64 → E} + {maxHeld : ℕ} {ι0 ι : ICfg U64 U64 U64 U64 E} {K0 K : KLayoutP} + (hι : IInit ι0) (htb : ∀ e, ι0.tbl e = none) (hK0 : ROKP K0) (hgh : ι0.gh = ghP K0 ι0.mem) + (hQ0 : Q K0 ι0.mem) (h : IExecPA Q LP idx maxHeld ι0 K0 ι K) (hq : ∀ d, ¬ (ι.pc d).isLoc) + (hpd : ∀ d, (ι.pc d).pend = none) : + ∃ s : Cfg U64 U64 U64 U64, ExecPAR Q LP (bootCfg ι0) K0 s K ∧ Rel [] ι s ∧ RunAll s.log ι0.mem ι.mem := by + obtain ⟨s, Km, Qh, hx, R, -, P⟩ := impl_execPA (idx := idx) (s0 := bootCfg ι0) hι htb rfl rfl (fun _ => rfl) + (fun _ => rfl) rfl rfl h + have hQ := R.qE hpd + subst hQ + have hK : Km = K := P.nopend hpd + rw [hK] at hx + have hM0 : MapsPA K0 (bootCfg ι0) := + ⟨congrArg Prod.fst hgh, congrArg (fun g => g.2.1) hgh, congrArg (fun g => g.2.2) hgh⟩ + have hnl : NoLoc s := fun d hd => hq d (isLoc_of_phOf (by rw [← R.ph d]; exact hd)) + refine ⟨s, hx, R, ?_⟩ + rw [← R.memE hpd] + exact serializable_e2ePAR (s0 := bootCfg ι0) LP ⟨rfl, fun d h => h⟩ (fun d p hd => by cases hd) hK0 hM0 hQ0 hx hnl + +end +end Fl4ProofsA64.P2.GateExecA diff --git a/proofs/Fl4ProofsA64/P2/GlueAdeqA.lean b/proofs/Fl4ProofsA64/P2/GlueAdeqA.lean new file mode 100644 index 000000000..25d0f953b --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/GlueAdeqA.lean @@ -0,0 +1,259 @@ +/- +# P2 (sync, gen 1): **adequacy of `tr` on AArch64** — the deterministic sequential run of a translated body is the +store run of the path's `F` (gap G1, ported from `Fl4Proofs/P2/GlueAdeq.lean`) + +The AArch64 twin of x86_64's G1 (`Fl4Proofs/P2/GlueAdeq.lean`, P2 line glue gen 3), over `Fl4ProofsA64.P2.E2E`'s +`tr` / `LocalPath`, which are the x86_64 text re-checked over `Fl4CoreA64` (`port.py`). The proofs below are the +x86_64 proofs with the renaming only. + +* `Serial.RunD` (the deterministic run: every read returns the current memory's value), `RunD.run`, `RunD.det`: + shared with x86_64, from **`Fl4P2Base/SerialD.lean`** (architecture-independent: stated over `Serial.Prog`). +* **`E2E.runD_tr`**: if the tree `p` runs on the store from `s` to `r` (`interp … = ok r`) with aligned accesses and + `Once`, and `m` agrees with `s` on aligned words (user words: those not yet written), then `tr … p` has a + deterministic run from `m` to an `m'` that is `r`'s memory on aligned kernel words and `m` elsewhere. +* **`E2E.LocalPath.body_runD`** / **`E2E.LocalPath.runD_result`** / **`E2E.LocalPath.body_run`**: for a local path + `P` from a `Pre` state `σ`, the body `P.body σ a` has a deterministic run from any `m` agreeing with `σ` on aligned + words, and every deterministic run of it from such an `m` ends in `F`'s result on aligned kernel words. Only the + fields every `LocalPath` already carries are used (`isProg`, `obl`, `side`, `once`, `total`, `masks`). + +What G1 does **not** give (as on x86_64): the Serial history records an arbitrary `Run` of the logged program +(`LocInv.sim` is existential over the speculative values), and a completed body is replayed from the prefix memory +at completion, not at start (G3). `handoff/p2sync.md` (gen 1) proposes the history extension that uses this file. + +Not ported: x86_64's `Serial.veq_revert`. It is about `VEq`, which lives in x86_64's `Fl4Proofs/P2/SerialH.lean` +(not in `Fl4P2Base`); AArch64 has no `SerialH` / `VEq` and so no user for it. + +No premise: no [J], [P] or [W]. No existing statement changed. +-/ +import Fl4P2Base.SerialD +import Fl4ProofsA64.P2.E2E +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P2.E2E +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2.PathObl +open Fl4Proofs.P2.Serial (Owner ClsOwner Prog RunD) + +noncomputable section +open Classical + +section adeq +variable {S A X : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + {R User : U64 → Prop} {cls : U64 → U64 → U64} {ko : U64 → ClsOwner U64} {c kb : U64} {mk : U64 × U64} + +theorem runD_rdN {Wd : Set U64} {q : U64} {k : U64 → SP} {m m' : U64 → U64} + (h : RunD (k (m q)) m m') : RunD (rdN R User cls ko c Wd q k) m m' := by + unfold rdN + split_ifs <;> first | exact .rd h | exact .rds h | exact .rdc rfl h + +/-- **Adequacy of `tr`** (module docstring): the store run of the tree `p` is the deterministic sequential run of +its translation, on aligned kernel words. -/ +theorem runD_tr (hmk : MasksWr st) (p : SProg X) : + ∀ (s : S) (a : A) (Wd T : Set U64) (m : U64 → U64) (r : X × S × A), + interp st ar p s a = ok r → + Within st ar (fun b => Aligned b) (fun b => Aligned b) p s a → + Once st ar R User Wd T p s a → + st.frame_masks s = ok mk → L.ksb s = kb → L.cpu s = c → + (∀ y, Aligned y → (¬ User y ∨ y ∉ T) → m y = L.mem s y) → + ∃ m', RunD (tr R User cls ko c kb mk ar p Wd a) m m' ∧ + (∀ y, Aligned y → ¬ User y → m' y = L.mem r.2.1 y) ∧ + (∀ y, ¬ (Aligned y ∧ ¬ User y) → m' y = m y) := by + induction p with + | ret x => + intro s a Wd T m r h _ _ _ _ _ hK + simp only [interp] at h + obtain rfl := Result.ok_injective h + refine ⟨m, ?_, fun y hy hu => hK y hy (Or.inl hu), fun _ _ => rfl⟩ + simp only [tr]; exact .done m + | err e => + intro s a Wd T m r h + simp only [interp] at h + exact absurd h fail_not_ok + | zero q n k ih => + intro s a Wd T m r _ _ hN + simp only [Once] at hN + | clr q n k ih => + intro s a Wd T m r _ _ hN + simp only [Once] at hN + | rd q k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + obtain ⟨hal, hW⟩ := hW + obtain ⟨ho, hN⟩ := hN + have ev : v = L.mem s q := Result.ok_injective (hv.symm.trans (L.read_eq s q)) + subst ev + have hu : ¬ User q ∨ q ∉ T := by + by_cases hu : User q + · exact Or.inr (ho fun h => h.1 hu) + · exact Or.inl hu + have e : m q = L.mem s q := hK q hal hu + obtain ⟨m', h1, h2, h3⟩ := ih (L.mem s q) s a Wd _ m r hk' (hW _ hv) (hN _ hv) hm hk hc + (fun y hy hyu => hK y hy (hyu.imp_right fun h hT => h (tch_sub hT))) + refine ⟨m', ?_, h2, h3⟩ + simp only [tr] + have h1' : RunD (tr R User cls ko c kb mk ar (k (m q)) Wd a) m m' := by rw [e]; exact h1 + exact runD_rdN h1' + | rdp q k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + simp only [tr] + have hrp := L.read_pair_eq s q + generalize core.num.U64.wrapping_add q 8#u64 = q8 at * + obtain ⟨hal, hal8, hW⟩ := hW + obtain ⟨ho, ho8, hN⟩ := hN + have ev : v = (L.mem s q, L.mem s q8) := Result.ok_injective (hv.symm.trans hrp) + subst ev + have hu : ¬ User q ∨ q ∉ T := by + by_cases hu : User q + · exact Or.inr (ho fun h => h.1 hu) + · exact Or.inl hu + have hu8 : ¬ User q8 ∨ q8 ∉ T := by + by_cases hu : User q8 + · exact Or.inr fun h => ho8 (fun h => h.1 hu) (tch_sub h) + · exact Or.inl hu + have e : m q = L.mem s q := hK q hal hu + have e8 : m q8 = L.mem s q8 := hK q8 hal8 hu8 + obtain ⟨m', h1, h2, h3⟩ := ih _ s a Wd _ m r hk' (hW _ hv) (hN _ hv) hm hk hc + (fun y hy hyu => hK y hy (hyu.imp_right fun h hT => h (tch_sub (tch_sub hT)))) + refine ⟨m', ?_, h2, h3⟩ + have h1' : RunD (tr R User cls ko c kb mk ar (k (m q, m q8)) Wd a) m m' := by rw [e, e8]; exact h1 + exact runD_rdN (runD_rdN h1') + | wr q v k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨s', hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + obtain ⟨hal, hW⟩ := hW + have es : s' = L.wr s q v := Result.ok_injective (hv.symm.trans (L.write_eq s q v)) + subst es + have hm' : st.frame_masks (L.wr s q v) = ok mk := by rw [hmk]; exact hm + have hk2 : L.ksb (L.wr s q v) = kb := by rw [L.ksb_wr]; exact hk + have hc2 : L.cpu (L.wr s q v) = c := by rw [L.cpu_wr]; exact hc + have hN := hN _ hv + by_cases hu : User q + · simp only [if_pos hu] at hN + have hK' : ∀ y, Aligned y → (¬ User y ∨ y ∉ insert q T) → m y = L.mem (L.wr s q v) y := by + intro y hy hyu + have hne : q ≠ y := by + rintro rfl + rcases hyu with h | h + · exact h hu + · exact h (Set.mem_insert _ _) + rw [L.mem_wr s q v y hal hy, if_neg hne] + exact hK y hy (hyu.imp_right fun h hT => h (Set.mem_insert_of_mem _ hT)) + obtain ⟨m', h1, h2, h3⟩ := ih _ a Wd _ m r hk' (hW _ hv) hN hm' hk2 hc2 hK' + refine ⟨m', ?_, h2, h3⟩ + simp only [tr, if_pos hu]; exact h1 + · simp only [if_neg hu] at hN + have hK' : ∀ y, Aligned y → (¬ User y ∨ y ∉ T) → + Function.update m q v y = L.mem (L.wr s q v) y := by + intro y hy hyu + rw [L.mem_wr s q v y hal hy] + by_cases hne : q = y + · subst hne; rw [if_pos rfl, Function.update_self] + · rw [if_neg hne, Function.update_of_ne (Ne.symm hne)]; exact hK y hy hyu + obtain ⟨m', h1, h2, h3⟩ := ih _ a _ T (Function.update m q v) r hk' (hW _ hv) hN hm' hk2 hc2 hK' + refine ⟨m', ?_, h2, fun y hy => ?_⟩ + · simp only [tr, if_neg hu]; exact .wr h1 + · rw [h3 y hy, Function.update_of_ne] + rintro rfl; exact hy ⟨hal, hu⟩ + | ksb k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have ev : v = kb := Result.ok_injective (hv.symm.trans (by rw [← hk]; exact L.ks_base_eq s)) + subst ev + simp only [tr] + exact ih _ s a Wd T m r hk' (hW _ hv) (hN _ hv) hm hk hc hK + | cpu k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have ev : v = c := Result.ok_injective (hv.symm.trans (by rw [← hc]; exact L.cur_cpu_eq s)) + subst ev + simp only [tr] + exact ih _ s a Wd T m r hk' (hW _ hv) (hN _ hv) hm hk hc hK + | masks k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have ev : v = mk := Result.ok_injective (hv.symm.trans hm) + subst ev + simp only [tr] + exact ih _ s a Wd T m r hk' (hW _ hv) (hN _ hv) hm hk hc hK + | arch op k ih => + intro s a Wd T m r h hW hN hm hk hc hK + simp only [interp] at h + obtain ⟨⟨b, a'⟩, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have hca : ∃ r, callArch ar op a = ok r := ⟨_, hv⟩ + have e2 : hca.choose = (b, a') := Result.ok_injective (hca.choose_spec.symm.trans hv) + simp only [tr, dif_pos hca] + rw [e2] + exact ih b s a' Wd T m r hk' (hW _ _ hv) (hN _ _ hv) hm hk hc hK + +end adeq + +section path +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} + {ko : U64 → ClsOwner U64} {User : U64 → Prop} + +/-- **A started body's deterministic run is its store run** (module docstring): from a `Pre` state `σ`, a run of `F` +(accepted or declined) to `r`, and any `m` agreeing with `σ` on aligned words. -/ +theorem LocalPath.body_runD (P : LocalPath own cls ko User) {σ : P.S} {a : P.A} {r : P.X × P.S × P.A} + (hpre : P.Pre σ) (h : P.F P.ar P.st σ a = ok r) {m : U64 → U64} + (hm : ∀ y, Aligned y → m y = P.L.mem σ y) : + ∃ m', RunD (P.body σ a) m m' ∧ (∀ y, Aligned y → ¬ User y → m' y = P.L.mem r.2.1 y) ∧ + (∀ y, ¬ (Aligned y ∧ ¬ User y) → m' y = m y) := by + letI := P.L + have gu : AltOf P.st P.Pre P.RS cls ko User σ := AltOf.pre hpre + have hI : interp P.st P.ar P.p σ a = ok r := by + rw [← P.isProg _ (Narrow.self _) σ gu a]; exact h + have hA := P.side.read_aligned σ σ a r hpre (Alt.refl σ) h + rw [P.isProg _ (Narrow.guard _ _ _) σ gu a] at hA + have hA := (interp_guard P.p σ a r hA).2 + have hWr : Within P.st P.ar (fun _ => True) (fun b => Aligned b) P.p σ a := by + by_cases hacc : P.Acc r.1 + · exact Within.mono (fun _ _ => trivial) (fun b hb => P.obl.aligned σ b hpre (Or.inl hb)) P.p σ a + (P.obl.within P.isProg hpre gu h hacc) + · exact Within.mono (fun _ _ => trivial) (fun b hb => P.obl.aligned σ b hpre (Or.inr hb)) P.p σ a + (P.obl.within_dec P.isProg hpre gu h hacc) + have hW := Within.mono (fun b hb => hb.1) (fun b hb => hb.2) P.p σ a (within_and P.p σ a hA hWr) + have hO := P.once σ σ a r hpre (Alt.refl σ) hI + exact runD_tr P.masks P.p σ a ∅ ∅ m r hI hW hO (masksOf_eq P.st σ) rfl rfl (fun y hy _ => hm y hy) + +/-- **Every deterministic run of a started body ends in `F`'s result** on aligned kernel words (G1, module +docstring). -/ +theorem LocalPath.runD_result (P : LocalPath own cls ko User) {σ : P.S} {a : P.A} {r : P.X × P.S × P.A} + (hpre : P.Pre σ) (h : P.F P.ar P.st σ a = ok r) {m m' : U64 → U64} + (hm : ∀ y, Aligned y → m y = P.L.mem σ y) (hr : RunD (P.body σ a) m m') : + ∀ y, Aligned y → ¬ User y → m' y = P.L.mem r.2.1 y := by + obtain ⟨m'', h1, h2, -⟩ := P.body_runD hpre h hm + rw [hr.det h1]; exact h2 + +/-- **A started body has a run from `σ`'s memory ending in `F`'s result** (G1 with `Run`; the total body runs). -/ +theorem LocalPath.body_run (P : LocalPath own cls ko User) {σ : P.S} {a : P.A} (hpre : P.Pre σ) + {m : U64 → U64} (hm : ∀ y, Aligned y → m y = P.L.mem σ y) : + ∃ r, P.F P.ar P.st σ a = ok r ∧ ∃ m', Fl4Proofs.P2.Serial.Run (P.body σ a) m m' ∧ + ∀ y, Aligned y → ¬ User y → m' y = P.L.mem r.2.1 y := by + obtain ⟨r, h⟩ := P.total σ a + obtain ⟨m', h1, h2, -⟩ := P.body_runD hpre h hm + exact ⟨r, h, m', h1.run, h2⟩ + +end path + +end +end Fl4ProofsA64.P2.E2E diff --git a/proofs/Fl4ProofsA64/P2/GlueRepA.lean b/proofs/Fl4ProofsA64/P2/GlueRepA.lean new file mode 100644 index 000000000..3bd5c1b6b --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/GlueRepA.lean @@ -0,0 +1,376 @@ +/- +# P2 (sync, gen 2): **G1 from alternative states** on AArch64 — the replay record's start premise and the +accepted / declined write split (DESIGN §9 item 4, Half 1 (b)) + +`Fl4P2Base/SerialR.lean` records, for each started local path, one deterministic run (`RunD`) of its body from an +admissible memory `ν` (`AdmR`: the start memory on every word the core may read, its class on class-tracked words; +on the other words, what the path's speculative reads returned). This module ties such a run to the extracted body +`F` (`E2E.LocalPath`): + +* **`rep_tr`** (the induction, the twin of `GlueAdeqA.runD_tr` with agreement only on the words the run reads): if + the tree `p` runs on the store from `s` to `r` with aligned accesses, reads in `Rr`, writes in `Wr` and `Once`, and + `ν` agrees with `s` on the words the deterministic run of `tr … p` from `ν` reads in `Rr`, then that run reads once + (`OnceD`), writes only in `Wr` (`WrIn`), and ends in `r`'s memory on every aligned kernel word where `ν` and `s` + agree (in particular on every word it writes). +* **`LocalPath.rep`**: for a `Pre` state `σ`, the model memory `μ` agreeing with `σ` on aligned kernel words, and + every `ν` admissible for `μ` (`SafeCert.Adm` with no pins), the body `P.body σ a` reads once along its + deterministic run from `ν`, and there is a store state `σ'` of the path (`AltOf`) with `F σ' = ok r` such that + the run from `ν` ends in `r`'s memory where `ν` and `σ'` agree, and either + - **accepted**: `Acc r.1`, `σ'` is a `Pre` state equal to `μ` on aligned kernel words (`σ` with the user words the + run reads written), and every write is in `WS σ`; or + - **declined**: `¬ Acc r.1` (`σ'` an `Alt` state of such a `Pre` state, holding `ν` on the words read), and every + write is in `DS σ`. + The state is built as `E2E.body_safe`'s certificates are: the user words the run reads are written into `σ` + (`PathSide.user_free` keeps `Pre` and the sets), then, if that run declines, the kernel words (an `Alt` state; + `PathObl.dec_alt`). +* **`LocalPath.startOnce`**: a body the glue starts (`StartAt`), in a `Serial` state whose ghost maps are the path's, + satisfies `SerialR.StartOnce`'s read-once premise from every `AdmR` memory (`admR_iff`). + +Only `LocalPath`'s existing fields (`isProg`, `obl`, `side`, `once`, `total`, `masks`) and `OwnDep` (a hypothesis, +proved for `OwnMap.own` by `E2E.ownDep`) are used. No premise: no [J], [P] or [W]. No existing statement changed. +-/ +import Fl4P2Base.SerialR +import Fl4ProofsA64.P2.GlueAdeqA +open Aeneas Aeneas.Std Result + +namespace Fl4ProofsA64.P2.E2E +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2.PathObl +open Fl4Proofs.P2.Serial (Owner ClsOwner Prog RunD OnceD WrIn Adm AdmR Cfg admR_iff onceD_done onceD_rd + onceD_rds onceD_rdc onceD_wr wrIn_done wrIn_wr) + +noncomputable section +open Classical + +section rep +variable {S A X : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + {R User : U64 → Prop} {cls : U64 → U64 → U64} {ko : U64 → ClsOwner U64} {c kb : U64} {mk : U64 × U64} + +theorem runD_rdN_inv {Wd : Set U64} {q : U64} {k : U64 → SP} {m m' : U64 → U64} + (h : RunD (rdN R User cls ko c Wd q k) m m') : RunD (k (m q)) m m' := by + unfold rdN at h + split_ifs at h <;> cases h <;> assumption + +theorem wrIn_rdN {Q : U64 → Prop} {Wd : Set U64} {q : U64} {k : U64 → SP} {ν : U64 → U64} + (h : WrIn Q (k (ν q)) ν) : WrIn Q (rdN R User cls ko c Wd q k) ν := by + unfold rdN; split_ifs <;> exact h + +theorem onceD_rdN {Wd T P : Set U64} {q : U64} {k : U64 → SP} {ν : U64 → U64} + (hPT : P ⊆ T) (ho : Racy R User Wd q → q ∉ T) + (hk : ∀ P', P' ⊆ tch R User Wd q T → OnceD P' (k (ν q)) ν) : + OnceD P (rdN R User cls ko c Wd q k) ν := by + by_cases hr : Racy R User Wd q + · have hsub : insert q P ⊆ tch R User Wd q T := by + unfold tch; rw [if_pos hr]; exact Set.insert_subset_insert hPT + have hq : q ∉ P := fun h => ho hr (hPT h) + unfold rdN + rw [if_neg (not_not.mpr hr)] + split_ifs + · exact onceD_rdc.mpr ⟨hq, rfl, hk _ hsub⟩ + · exact onceD_rds.mpr ⟨hq, hk _ hsub⟩ + · unfold rdN; rw [if_pos hr] + exact onceD_rd.mpr (hk P (hPT.trans tch_sub)) + +/-- A racy read's word is a kernel word or not yet in `T`. -/ +theorem nuT {Wd T : Set U64} {q : U64} (ho : Racy R User Wd q → q ∉ T) : ¬ User q ∨ q ∉ T := by + by_cases hu : User q + · exact Or.inr (ho fun h => h.1 hu) + · exact Or.inl hu + +/-- **The deterministic run of a translated tree from `ν` follows its store run** (module docstring). -/ +theorem rep_tr (hmk : MasksWr st) {Rr Wr : U64 → Prop} (hWr : ∀ x, Wr x → Aligned x) (p : SProg X) : + ∀ (s : S) (a : A) (Wd T P : Set U64) (ν : U64 → U64) (r : X × S × A), + interp st ar p s a = ok r → Within st ar (fun b => Aligned b ∧ Rr b) Wr p s a → + Once st ar R User Wd T p s a → + st.frame_masks s = ok mk → L.ksb s = kb → L.cpu s = c → P ⊆ T → + (∀ y ∈ reads (tr R User cls ko c kb mk ar p Wd a) ν, Aligned y → Rr y → + (¬ User y ∨ y ∉ T) → L.mem s y = ν y) → + OnceD P (tr R User cls ko c kb mk ar p Wd a) ν ∧ WrIn Wr (tr R User cls ko c kb mk ar p Wd a) ν ∧ + ∀ m', RunD (tr R User cls ko c kb mk ar p Wd a) ν m' → + ∀ y, Aligned y → ¬ User y → ν y = L.mem s y → m' y = L.mem r.2.1 y := by + induction p with + | ret x => + intro s a Wd T P ν r h _ _ _ _ _ _ _ + simp only [interp] at h + obtain rfl := Result.ok_injective h + simp only [tr] + refine ⟨onceD_done, wrIn_done, fun m' hm y _ _ hy => ?_⟩ + cases hm; exact hy + | err e => + intro s a Wd T P ν r h + simp only [interp] at h + exact absurd h fail_not_ok + | zero q n k ih => + intro s a Wd T P ν r _ _ hN + simp only [Once] at hN + | clr q n k ih => + intro s a Wd T P ν r _ _ hN + simp only [Once] at hN + | rd q k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + simp only [tr] at hν ⊢ + rw [reads_rdN] at hν + obtain ⟨⟨hal, hr⟩, hW⟩ := hW + obtain ⟨ho, hN⟩ := hN + have e : L.mem s q = ν q := hν q (List.mem_cons_self ..) hal hr (nuT ho) + have ev : v = ν q := Result.ok_injective (hv.symm.trans (by rw [← e]; exact L.read_eq s q)) + subst ev + have IH := fun P' (hP' : P' ⊆ tch R User Wd q T) => + ih (ν q) s a Wd _ P' ν r hk' (hW _ hv) (hN _ hv) hm hk hc hP' + (fun y hy hya hyr hyu => hν y (List.mem_cons_of_mem _ hy) hya hyr + (hyu.imp_right fun h hT => h (tch_sub hT))) + have hP1 : P ⊆ tch R User Wd q T := hPT.trans tch_sub + exact ⟨onceD_rdN hPT ho fun P' hP' => (IH P' hP').1, wrIn_rdN (IH P hP1).2.1, + fun m' hm' => (IH P hP1).2.2 m' (runD_rdN_inv hm')⟩ + | rdp q k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + simp only [tr] at hν ⊢ + rw [reads_rdN, reads_rdN] at hν + have hrp := L.read_pair_eq s q + generalize core.num.U64.wrapping_add q 8#u64 = q8 at * + obtain ⟨⟨hal, hr⟩, ⟨hal8, hr8⟩, hW⟩ := hW + obtain ⟨ho, ho8, hN⟩ := hN + have e : L.mem s q = ν q := hν q (List.mem_cons_self ..) hal hr (nuT ho) + have e8 : L.mem s q8 = ν q8 := hν q8 (List.mem_cons_of_mem _ (List.mem_cons_self ..)) hal8 hr8 + ((nuT ho8).imp_right fun h hT => h (tch_sub hT)) + have ev : v = (ν q, ν q8) := Result.ok_injective (hv.symm.trans (by rw [hrp, e, e8])) + subst ev + have IH := fun P'' (hP'' : P'' ⊆ tch R User Wd q8 (tch R User Wd q T)) => + ih (ν q, ν q8) s a Wd _ P'' ν r hk' (hW _ hv) (hN _ hv) hm hk hc hP'' + (fun y hy hya hyr hyu => hν y (List.mem_cons_of_mem _ (List.mem_cons_of_mem _ hy)) hya hyr + (hyu.imp_right fun h hT => h (tch_sub (tch_sub hT)))) + have hP2 : P ⊆ tch R User Wd q8 (tch R User Wd q T) := hPT.trans (tch_sub.trans tch_sub) + exact ⟨onceD_rdN hPT ho fun P' hP' => onceD_rdN hP' ho8 fun P'' hP'' => (IH P'' hP'').1, + wrIn_rdN (wrIn_rdN (IH P hP2).2.1), + fun m' hm' => (IH P hP2).2.2 m' (runD_rdN_inv (runD_rdN_inv hm'))⟩ + | wr q v k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨s', hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + obtain ⟨hWq, hW⟩ := hW + have es : s' = L.wr s q v := Result.ok_injective (hv.symm.trans (L.write_eq s q v)) + subst es + have hal := hWr q hWq + have hm' : st.frame_masks (L.wr s q v) = ok mk := by rw [hmk]; exact hm + have hk2 : L.ksb (L.wr s q v) = kb := by rw [L.ksb_wr]; exact hk + have hc2 : L.cpu (L.wr s q v) = c := by rw [L.cpu_wr]; exact hc + have hN := hN _ hv + by_cases hu : User q + · simp only [if_pos hu] at hN + simp only [tr, if_pos hu] at hν ⊢ + have hν' : ∀ y ∈ reads (tr R User cls ko c kb mk ar k Wd a) ν, Aligned y → Rr y → + (¬ User y ∨ y ∉ insert q T) → L.mem (L.wr s q v) y = ν y := by + intro y hy hya hyr hyu + have hne : q ≠ y := by + rintro rfl + rcases hyu with h | h + · exact h hu + · exact h (Set.mem_insert _ _) + rw [L.mem_wr s q v y hal hya, if_neg hne] + exact hν y hy hya hyr (hyu.imp_right fun h hT => h (Set.mem_insert_of_mem _ hT)) + obtain ⟨h1, h2, h3⟩ := ih _ a Wd _ P ν r hk' (hW _ hv) hN hm' hk2 hc2 + (hPT.trans (Set.subset_insert _ _)) hν' + refine ⟨h1, h2, fun m' hr y hy hyu hyν => h3 m' hr y hy hyu ?_⟩ + have hne : q ≠ y := by rintro rfl; exact hyu hu + rw [L.mem_wr s q v y hal hy, if_neg hne]; exact hyν + · simp only [if_neg hu] at hN + simp only [tr, if_neg hu] at hν ⊢ + simp only [reads] at hν + have hν' : ∀ y ∈ reads (tr R User cls ko c kb mk ar k (insert q Wd) a) (Function.update ν q v), + Aligned y → Rr y → (¬ User y ∨ y ∉ T) → L.mem (L.wr s q v) y = Function.update ν q v y := by + intro y hy hya hyr hyu + rw [L.mem_wr s q v y hal hya] + by_cases hne : q = y + · subst hne; rw [if_pos rfl, Function.update_self] + · rw [if_neg hne, Function.update_of_ne (Ne.symm hne)]; exact hν y hy hya hyr hyu + obtain ⟨h1, h2, h3⟩ := ih _ a _ T P _ r hk' (hW _ hv) hN hm' hk2 hc2 hPT hν' + refine ⟨onceD_wr.mpr h1, wrIn_wr.mpr ⟨hWq, h2⟩, fun m' hr y hy hyu hyν => ?_⟩ + cases hr with + | wr hr => + refine h3 m' hr y hy hyu ?_ + rw [L.mem_wr s q v y hal hy] + by_cases hne : q = y + · subst hne; rw [if_pos rfl, Function.update_self] + · rw [if_neg hne, Function.update_of_ne (Ne.symm hne)]; exact hyν + | ksb k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have ev : v = kb := Result.ok_injective (hv.symm.trans (by rw [← hk]; exact L.ks_base_eq s)) + subst ev + simp only [tr] at hν ⊢ + exact ih _ s a Wd T P ν r hk' (hW _ hv) (hN _ hv) hm hk hc hPT hν + | cpu k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have ev : v = c := Result.ok_injective (hv.symm.trans (by rw [← hc]; exact L.cur_cpu_eq s)) + subst ev + simp only [tr] at hν ⊢ + exact ih _ s a Wd T P ν r hk' (hW _ hv) (hN _ hv) hm hk hc hPT hν + | masks k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨v, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have ev : v = mk := Result.ok_injective (hv.symm.trans hm) + subst ev + simp only [tr] at hν ⊢ + exact ih _ s a Wd T P ν r hk' (hW _ hv) (hN _ hv) hm hk hc hPT hν + | arch op k ih => + intro s a Wd T P ν r h hW hN hm hk hc hPT hν + simp only [interp] at h + obtain ⟨⟨b, a'⟩, hv, hk'⟩ := bok.mp h + simp only [Within] at hW + simp only [Once] at hN + have hca : ∃ r, callArch ar op a = ok r := ⟨_, hv⟩ + have e2 : hca.choose = (b, a') := Result.ok_injective (hca.choose_spec.symm.trans hv) + simp only [tr, dif_pos hca] at hν ⊢ + rw [e2] at hν ⊢ + exact ih b s a' Wd T P ν r hk' (hW _ _ hv) (hN _ _ hv) hm hk hc hPT hν + +end rep + +section path +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} + {ko : U64 → ClsOwner U64} {User : U64 → Prop} + +/-- **G1 from alternative states, with the accepted / declined write split** (module docstring). -/ +theorem LocalPath.rep (P : LocalPath own cls ko User) (hO : OwnDep own User) {σ : P.S} {a : P.A} + (hpre : P.Pre σ) {μ : U64 → U64} (hμ : ∀ x, Aligned x → ¬ User x → P.L.mem σ x = μ x) + {ν : U64 → U64} (hν : Adm own cls ko (P.L.cpu σ) (P.Locks σ) μ (fun _ => none) ν) : + OnceD ∅ (P.body σ a) ν ∧ ∃ (σ' : P.S) (r : P.X × P.S × P.A), P.F P.ar P.st σ' a = ok r ∧ + @AltOf P.S P.st P.L P.Pre P.RS cls ko User σ' ∧ + (∀ m', RunD (P.body σ a) ν m' → ∀ y, Aligned y → ¬ User y → ν y = P.L.mem σ' y → + m' y = P.L.mem r.2.1 y) ∧ + ((P.Acc r.1 ∧ P.Pre σ' ∧ (∀ x, Aligned x → ¬ User x → P.L.mem σ' x = μ x) ∧ + WrIn (P.WS σ) (P.body σ a) ν) ∨ + (¬ P.Acc r.1 ∧ WrIn (P.DS σ) (P.body σ a) ν)) := by + letI := P.L + have e0 : own (P.L.mem σ) = own μ := hO _ _ hμ + have hνR : ∀ x, ¬ User x → P.RS σ x → ν x = μ x := by + intro x hu hr + refine hν.1 x ?_ + rw [← e0] + rcases P.obl.read_own σ x hpre hr hu with h | h + · exact Or.inl (heldBy_iff.mp h) + · exact Or.inr h + -- the words the run from `ν` reads: the user ones go into a `Pre` state, the kernel ones into an `Alt` state + let l := reads (P.body σ a) ν + let ul := l.filter (fun x => decide (Aligned x ∧ User x)) + let kl := l.filter (fun x => decide (Aligned x ∧ ¬ User x)) + have hul : ∀ x, x ∈ ul ↔ x ∈ l ∧ Aligned x ∧ User x := by + intro x; simp only [ul, List.mem_filter, decide_eq_true_eq] + have hkl : ∀ x, x ∈ kl ↔ x ∈ l ∧ Aligned x ∧ ¬ User x := by + intro x; simp only [kl, List.mem_filter, decide_eq_true_eq] + obtain ⟨σu, hσu⟩ : ∃ t, t = wrs P.st ν ul σ := ⟨_, rfl⟩ + obtain ⟨uP, -, uR, uW, uD⟩ := wrs_user P.st P.side (ν := ν) ul σ + (fun x hx => ⟨((hul x).mp hx).2.2, ((hul x).mp hx).2.1⟩) hpre + obtain ⟨uc, ub, um⟩ := wrs_const P.st (ν := ν) P.masks ul σ + have uM := wrs_mem P.st (ν := ν) ul σ (fun x hx => ((hul x).mp hx).2.1) + rw [← hσu] at uP uR uW uD uc ub um uM + have uMk : ∀ x, Aligned x → ¬ User x → P.L.mem σu x = μ x := by + intro x hx hu + rw [uM x hx, if_neg (fun h => hu ((hul x).mp h).2.2)]; exact hμ x hx hu + have uMu : ∀ x ∈ l, Aligned x → User x → P.L.mem σu x = ν x := by + intro x hx hxa hu; rw [uM x hxa, if_pos ((hul x).mpr ⟨hx, hxa, hu⟩)] + have hmσ : P.st.frame_masks σu = ok (masksOf P.st σ) := um.trans (masksOf_eq P.st σ) + obtain ⟨ru, hru⟩ := P.total σu a + have gu : AltOf P.st P.Pre P.RS cls ko User σu := AltOf.pre uP + by_cases hacc : P.Acc ru.1 + · -- accepted at `σu`: the guarded run reads only `RS σ`, where `ν` is `μ` + have hW := P.obl.acc σu a ru uP hru hacc + rw [P.isProg _ (Narrow.guard _ _ _) σu gu a] at hW + have hW := (interp_guard P.p σu a ru hW).2 + have hA := P.side.read_aligned σu σu a ru uP (Alt.refl σu) hru + rw [P.isProg _ (Narrow.guard _ _ _) σu gu a] at hA + have hA := (interp_guard P.p σu a ru hA).2 + have hI : interp P.st P.ar P.p σu a = ok ru := by + rw [← P.isProg _ (Narrow.self _) σu gu a]; exact hru + have hN := P.once σu σu a ru uP (Alt.refl σu) hI + rw [uR] at hW hN; rw [uW] at hW + have hWA := Within.mono (fun _ h => h) (fun _ h => h.2) P.p σu a (within_and P.p σu a hA hW) + have hag : ∀ y ∈ l, Aligned y → P.RS σ y → (¬ User y ∨ y ∉ (∅ : Set U64)) → + P.L.mem σu y = ν y := by + intro y hy hya hyr _ + by_cases hu : User y + · exact uMu y hy hya hu + · rw [uMk y hya hu, hνR y hu hyr] + obtain ⟨h1, h2, h3⟩ := rep_tr (Rr := P.RS σ) (Wr := P.WS σ) P.masks + (fun x hx => P.obl.aligned σ x hpre (Or.inl hx)) P.p σu a ∅ ∅ ∅ ν ru hI hWA hN hmσ ub uc + subset_rfl hag + exact ⟨h1, σu, ru, hru, gu, h3, Or.inl ⟨hacc, uP, uMk, h2⟩⟩ + · -- declined at `σu`: write the kernel words read too, an alternative state + obtain ⟨σν, hσν⟩ : ∃ t, t = wrs P.st ν kl σu := ⟨_, rfl⟩ + obtain ⟨kc, kk, km⟩ := wrs_const P.st (ν := ν) P.masks kl σu + have kM := wrs_mem P.st (ν := ν) kl σu (fun x hx => ((hkl x).mp hx).2.1) + rw [← hσν] at kc kk km kM + have hAlt : Alt P.st (P.RS σu) cls ko User σu σν := by + refine ⟨kc, kk, km, fun x hx => ?_, fun x hx hu hko => ?_⟩ + · have hxa := P.side.rs_aligned σu x uP hx + rw [uR] at hx + rw [kM x hxa] + split_ifs with h + · rw [hνR x ((hkl x).mp h).2.2 hx, uMk x hxa ((hkl x).mp h).2.2] + · rfl + · have hxa : Aligned x := P.side.ko_aligned x (by rcases hko with h | h <;> rw [h] <;> simp) + rw [kM x hxa] + split_ifs with h + · rw [uc] at hko + rw [hν.2.2 x hko, uMk x hxa hu] + · rfl + obtain ⟨rν, hrν⟩ := P.total σν a + have gν : AltOf P.st P.Pre P.RS cls ko User σν := ⟨σu, uP, hAlt⟩ + obtain ⟨hdec, hW, -⟩ := P.obl.dec_alt σu a ru uP hru hacc σν rν hAlt hrν + rw [P.isProg _ (Narrow.guard _ _ _) σν gν a] at hW + have hW := (interp_guard P.p σν a rν hW).2 + have hA := P.side.read_aligned σu σν a rν uP hAlt hrν + rw [P.isProg _ (Narrow.guard _ _ _) σν gν a] at hA + have hA := (interp_guard P.p σν a rν hA).2 + have hI : interp P.st P.ar P.p σν a = ok rν := by + rw [← P.isProg _ (Narrow.self _) σν gν a]; exact hrν + have hN := P.once σu σν a rν uP hAlt hI + rw [uR] at hN; rw [uD] at hW + have hWA := Within.mono (fun _ h => h) (fun _ h => h.2) P.p σν a (within_and P.p σν a hA hW) + have hag : ∀ y ∈ l, Aligned y → True → (¬ User y ∨ y ∉ (∅ : Set U64)) → + P.L.mem σν y = ν y := by + intro y hy hya _ _ + rw [kM y hya] + by_cases hu : User y + · rw [if_neg (fun h => ((hkl y).mp h).2.2 hu)]; exact uMu y hy hya hu + · rw [if_pos ((hkl y).mpr ⟨hy, hya, hu⟩)] + obtain ⟨h1, h2, h3⟩ := rep_tr (Rr := fun _ => True) (Wr := P.DS σ) P.masks + (fun x hx => P.obl.aligned σ x hpre (Or.inr hx)) P.p σν a ∅ ∅ ∅ ν rν hI hWA hN (km.trans hmσ) + (kk.trans ub) (kc.trans uc) subset_rfl hag + exact ⟨h1, σν, rν, hrν, gν, h3, Or.inr ⟨hdec, h2⟩⟩ + +/-- **The start premise of `SerialR.InvR.step`** for a body the glue starts (`StartAt`) in a `Serial` state whose +ghost maps are the path's: read once from every admissible memory. -/ +theorem LocalPath.startOnce (P : LocalPath own cls ko User) (hO : OwnDep own User) + {s : Cfg U64 U64 U64 U64} {c : U64} {k : SP} (hown : s.own = own) (hcls : s.cls = cls) + (hko : s.ko = ko) (h : StartAt P c (fun l => s.lk l = some c) s.mem k) : + ∀ ν, AdmR s c ν → OnceD ∅ k ν := by + obtain ⟨σ, a, hpre, hc, hl, hμ, rfl⟩ := h + intro ν hν + have hA := admR_iff.mp hν + rw [hown, hcls, hko, ← hl, ← hc] at hA + exact (P.rep hO hpre hμ hA).1 + +end path + +end +end Fl4ProofsA64.P2.E2E diff --git a/proofs/Fl4ProofsA64/P2/InvDelPteAccPA.lean b/proofs/Fl4ProofsA64/P2/InvDelPteAccPA.lean index 84e8daa0a..3cb73868d 100644 --- a/proofs/Fl4ProofsA64/P2/InvDelPteAccPA.lean +++ b/proofs/Fl4ProofsA64/P2/InvDelPteAccPA.lean @@ -29,6 +29,14 @@ statements (gen 12): `InvRSPA`, `PrePA` (definitions), `ioRSPA_inv` → `ioRSPV_ Hypotheses of the lemmas: [J] `hK : RegionOKP K`, [J] `hU : UserOKP K`, [J] `hbuf` (`InvBufJ8` at the entry), [J] `hcdt` (`InvCdtJ` at `PrePA`), `hp` (the program premise, from the tree). New module. + +(P2-A64X gen 27, Architect 13:04Z) **`PrePA` gains a last conjunct `CdtAt`** (the entry's own CDT links, `InvCdtJ`'s +two clauses at that state), so `hcdt` is a lemma, **`invCdtJ_prePA`**; the lemmas keep their `hcdt` argument (statement +text unchanged). Re-indexed: `VcTabA` is now `hE.2.2.2.2.2.1`. + +(P2-A64X gen 28, Architect 13:39Z) **`PrePA` gains a last conjunct `InvBufJ8`** (the entry's own IPC-buffer +placement), so `hbuf` is a lemma, **`invBufJ8_prePA`**; the lemmas keep their `hbuf` argument (statement text +unchanged). Re-indexed: `CdtAt` is now `hE.2.2.2.2.2.2.1`. -/ import Fl4ProofsA64.P2.InvDelPtePA import Fl4ProofsA64.P2.InvDelPteAcc2X @@ -85,13 +93,62 @@ variable (st) in def InvWSPA (K : KLayoutP) (Locks : S → U64 → Prop) (s : S) (x : U64) : Prop := Aligned x ∧ (x ∈ K.user ∨ IoWSPA st K Locks s x) +variable (st) in +/-- (P2-A64X gen 27) **CDT link well-formedness at one state** (`InvCdtJ`'s two clauses at `s` alone): a +capability slot's non-zero `PREV` word and non-zero `NEXT` pointer are capability slots. x86_64 reads it off P3's +`SlJAt` at the entry (`invCdtJ_of`); here it is the entry's own fact, from P1's `MdbLk ∧ NullLk` at the start +(`P2/E2EPStartA.lean`, `cdtAt_of_mdb`). -/ +def CdtAt (K : KLayout) (s : S) : Prop := + ∀ sl, sl ∈ K.slots → + (L.mem s (core.num.U64.wrapping_add sl layout.cte.PREV) ≠ 0#u64 → + L.mem s (core.num.U64.wrapping_add sl layout.cte.PREV) ∈ K.slots) ∧ + (∀ nx, obj.Slot.next st sl s = ok nx → nx ≠ 0#u64 → nx ∈ K.slots) + variable (st) in /-- **The entry states of the AArch64 PTE-arm path** (module docstring; x86_64 `PreP`; gen 12: with the [J] VCPU -placements `VcKsA` / `VcTabA`, `GateEp` without `K.vcpus = ∅`). -/ +placements `VcKsA` / `VcTabA`, `GateEp` without `K.vcpus = ∅`; P2-A64X gen 27: with the entry's CDT links +`CdtAt`, so that `InvCdtJ` at `PrePA` is a lemma, `invCdtJ_prePA`; P2-A64X gen 28: with the entry's IPC-buffer +placement `InvBufJ8`, last, so that the former [J] `hbuf` at `PrePA` is a lemma, `invBufJ8_prePA`). -/ def PrePA (Cs Ω Sg Ac : Set U64) (Locks : S → U64 → Prop) (p : localinv.Plan) (K : KLayoutP) (m : U64 → U64) (s : S) : Prop := InvJA (L := L) K.toKLayout s ∧ InvQFP st Cs Ω Sg Ac Locks p K.toKLayout m s ∧ GateEp st K s ∧ PmVs st K s ∧ - VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV + VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV ∧ CdtAt st K.toKLayout s ∧ InvBufJ8 st K.toKLayout s + +/-- (P2-A64X gen 27) **`InvCdtJ` at `PrePA`** (the former [J] `hcdt` of `inv_localPathPA`): `PrePA`'s `CdtAt`. -/ +theorem invCdtJ_prePA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} + {m : U64 → U64} : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m) := + fun _ sl hE => hE.2.2.2.2.2.2.1 sl + +/-- (P2-A64X gen 28) **`InvBufJ8` at `PrePA`** (the former [J] `hbuf` of `DelPremP`): `PrePA`'s last conjunct. -/ +theorem invBufJ8_prePA {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} + {m : U64 → U64} : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s := + fun _ hE => hE.2.2.2.2.2.2.2 + +variable (st) in +/-- (P2-Sync gen 5) **`CdtAt` at the locked slots** (`Lk`): the two clauses of `CdtAt` for the slots the path holds +only. A start can honestly assert it from the replay history (`P2/DelLkA.lean`); `CdtAt` at every slot it cannot, +since another core may be half-way through an edit of its own locked slots. -/ +def CdtAtL (K : KLayout) (Lk : U64 → Prop) (s : S) : Prop := + ∀ sl, sl ∈ K.slots → Lk sl → + (L.mem s (core.num.U64.wrapping_add sl layout.cte.PREV) ≠ 0#u64 → + L.mem s (core.num.U64.wrapping_add sl layout.cte.PREV) ∈ K.slots) ∧ + (∀ nx, obj.Slot.next st sl s = ok nx → nx ≠ 0#u64 → nx ∈ K.slots) + +theorem CdtAt.toL {K : KLayout} {Lk : U64 → Prop} {s : S} (h : CdtAt st K s) : CdtAtL st K Lk s := + fun sl hsl _ => h sl hsl + +variable (st) in +/-- (P2-Sync gen 5) **The PTE-arm entry states with `CdtAt` at the locked slots only** (`PrePA` with its `CdtAt` +conjunct weakened to `CdtAtL` at the path's locks; a parallel record, `PrePA` is unchanged). -/ +def PrePAL (Cs Ω Sg Ac : Set U64) (Locks : S → U64 → Prop) (p : localinv.Plan) (K : KLayoutP) (m : U64 → U64) + (s : S) : Prop := + InvJA (L := L) K.toKLayout s ∧ InvQFP st Cs Ω Sg Ac Locks p K.toKLayout m s ∧ GateEp st K s ∧ PmVs st K s ∧ + VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV ∧ CdtAtL st K.toKLayout (Locks s) s ∧ InvBufJ8 st K.toKLayout s + +theorem PrePA.toL {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} + {m : U64 → U64} {s : S} (h : PrePA st Cs Ω Sg Ac Locks p K m s) : PrePAL st Cs Ω Sg Ac Locks p K m s := + let ⟨a, b, c, d, e, f, g, i⟩ := h + ⟨a, b, c, d, e, f, g.toL, i⟩ variable {K : KLayoutP} {Locks : S → U64 → Prop} {s : S} {x : U64} @@ -106,8 +163,19 @@ section ent variable {S : Type} {st : machine.Store S} [L : LawfulStore st] variable {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {μ : U64 → U64} {s : S} -/-- The seen words of the PTE arm's plan are `IoRSR` words (`dest`'s four words, `dest` locked). -/ -theorem invSeenR_ofA (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : +/-- (P2-Sync gen 5) **`DelEntP` at a `PrePAL` state**: `delEntP_of`'s argument, with `CdtAt` asked at the plan's +target only, which the plan holds (`del_coreP`). -/ +theorem delEntP_ofL (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) : DelEntP st K.toKLayout Cs Locks p s := by + obtain ⟨cp, cr, cg, cgb, dest, fp, rt, asid, va, e, C, hcap, -, htg, hdc, hCc, hcl, hC, hdK, hdO, hLd, hLp, + hLn, -, hA, he, he0, hle⟩ := del_coreP hE.2.1 + have hd0 : dest ≠ 0#u64 := fun e => hE.1.slots0 (e ▸ hdK) + obtain ⟨hcP, hcN⟩ := hE.2.2.2.2.2.2.1 dest hdK (hLd hd0) + exact ⟨cp, cr, cg, cgb, dest, fp, rt, asid, va, e, C, hcap, htg, hdc, ⟨hA, he, he0, hle⟩, hCc, hcl, hC, hdK, + hdO, hLd hd0, fun hp => ⟨hcP hp, hLp hp⟩, fun nx e hne => ⟨hcN nx e hne, hLn nx e hne⟩⟩ + +/-- (P2-Sync gen 5: stated at `PrePAL`) The seen words of the PTE arm's plan are `IoRSR` words (`dest`'s four +words, `dest` locked). -/ +theorem invSeenR_ofAL (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) : ∀ x, SeenW p x → IoRSR st K.toKLayout Locks s x := by intro x hx obtain ⟨-, -, -, -, dest, -, -, -, -, -, -, -, -, -, -, -, -, -, hdK, -, hLd, -, -, hSn, -⟩ := del_coreP hE.2.1 @@ -118,26 +186,47 @@ theorem invSeenR_ofA (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : rcases ho with rfl | rfl | rfl | rfl <;> simp [layout.cte.CAP0, layout.cte.CAP1, layout.cte.PREV, layout.cte.NEXT] exact hE.2.1.1.1.1.lockSlotR hdK (hLd fun e => hE.1.slots0 (e ▸ hdK)) h32 h8 -/-- The invoked slot's two cap words are `IoRSR` words. -/ -theorem invPsR_ofA (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : +/-- The seen words of the PTE arm's plan are `IoRSR` words (`invSeenR_ofAL`). -/ +theorem invSeenR_ofA (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : + ∀ x, SeenW p x → IoRSR st K.toKLayout Locks s x := + invSeenR_ofAL hE.toL + +/-- (P2-Sync gen 5: stated at `PrePAL`) The invoked slot's two cap words are `IoRSR` words. -/ +theorem invPsR_ofAL (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) : IoRSR st K.toKLayout Locks s p.slot ∧ IoRSR st K.toKLayout Locks s (core.num.U64.wrapping_add p.slot 8#u64) := by obtain ⟨cp, cr, cg, cgb, -, -, -, -, -, -, -, hcap, hsK, -⟩ := del_coreP hE.2.1 exact hE.2.1.1.1.1.slotR hsK (Or.inr (cap_not_nof hcap (Or.inl ⟨_, _, _, _, rfl⟩))) -/-- (gen 12) **The VSpace layout, the convention and the walk-word agreement at a `PrePA` state** (`PmVs`, -`GateEp.agT` at `VcTabA`; no `K.vcpus = ∅`). -/ -theorem PrePA.agT (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : +/-- The invoked slot's two cap words are `IoRSR` words (`invPsR_ofAL`). -/ +theorem invPsR_ofA (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : + IoRSR st K.toKLayout Locks s p.slot ∧ IoRSR st K.toKLayout Locks s (core.num.U64.wrapping_add p.slot 8#u64) := + invPsR_ofAL hE.toL + +/-- (P2-Sync gen 5: stated at `PrePAL`) The VSpace layout, the convention and the walk-word agreement. -/ +theorem PrePAL.agT (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) : ∃ (Pp Tb : Set U64) (Lv : U64 → Nat) (m1 : U64 → U64), VsLayA st K Pp Tb Lv s ∧ 0#u64 ∉ Tb ∧ K.ptes = ptesR K m1 ∧ GateAgT st K Pp Tb m1 s := by - obtain ⟨-, -, hG, ⟨Pp, Tb, Lv, hV, h0T⟩, -, hvt⟩ := hE + obtain ⟨-, -, hG, ⟨Pp, Tb, Lv, hV, h0T⟩, -, hvt, -⟩ := hE obtain ⟨m1, hc, hag⟩ := hG.agT hV hvt exact ⟨Pp, Tb, Lv, m1, hV, h0T, hc, hag⟩ +/-- (gen 12) **The VSpace layout, the convention and the walk-word agreement at a `PrePA` state** (`PmVs`, +`GateEp.agT` at `VcTabA`; no `K.vcpus = ∅`; P2-Sync gen 5: by `PrePAL.agT`). -/ +theorem PrePA.agT (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) : + ∃ (Pp Tb : Set U64) (Lv : U64 → Nat) (m1 : U64 → U64), VsLayA st K Pp Tb Lv s ∧ 0#u64 ∉ Tb ∧ + K.ptes = ptesR K m1 ∧ GateAgT st K Pp Tb m1 s := + hE.toL.agT + +/-- (P2-Sync gen 5: stated at `PrePAL`) Every `IoRSR` word is an `InvRSPA` word. -/ +theorem ioRSR_invL (hK : RegionOKP K) (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) {x : U64} + (h : IoRSR st K.toKLayout Locks s x) : InvRSPA st K Locks s x := + ioRSPV_inv (ioRSR_PV hE.2.2.1.2.choose_spec.1 hK.base hE.2.2.2.2.1 h) + /-- (gen 10 `ioRSR_inv`, moved here and given [J] `hK` in gen 12) **Every `IoRSR` word is an `InvRSPA` word** at a -`PrePA` state (`ioRSR_PV`: the convention, the rule-7 layout, `VcKsA`). -/ +`PrePA` state (`ioRSR_PV`: the convention, the rule-7 layout, `VcKsA`; P2-Sync gen 5: by `ioRSR_invL`). -/ theorem ioRSR_inv (hK : RegionOKP K) (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) {x : U64} (h : IoRSR st K.toKLayout Locks s x) : InvRSPA st K Locks s x := - ioRSPV_inv (ioRSR_PV hE.2.2.1.2.choose_spec.1 hK.base hE.2.2.2.2.1 h) + ioRSR_invL hK hE.toL h end ent @@ -147,12 +236,11 @@ section blk variable {S : Type} {st : machine.Store S} [L : LawfulStore st] /-- (P2-A64I gen 10) **Both arms of the PTE-arm decode block on `guard st (InvRSPA s) (InvWSPA s)`**, `FinOK` over -`IoRSR s` (module docstring). Hypotheses: [J] `hbuf`, [J] `hcdt`. -/ -theorem invBlkPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} +`IoRSR s` (module docstring; P2-Sync gen 5: at `PrePAL`). Hypothesis: [J] `hbuf`. -/ +theorem invBlkPAL {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hK : RegionOKP K) (hU : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - {s : S} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hbuf : InvBufJ8 st K.toKLayout s) + {s : S} (hE : PrePAL st Cs Ω Sg Ac Locks p K m s) (hbuf : InvBufJ8 st K.toKLayout s) {a : A} {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) @@ -173,7 +261,7 @@ theorem invBlkPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω rw [(hq.1.2.2.1 (L.curW s) info (cur_thread_ok s) emi).1] at hy ⊢ have hW : IoWr st (InvWSPA st K Locks s) K.heap.scBits s := (PE.ioWrDN (nsK_of hK.base) hG.ub).mono (fun _ h => ioWSPA_inv hU h) - have hRR : ∀ b, IoRSR st K.toKLayout Locks s b → InvRSPA st K Locks s b := fun _ h => ioRSR_inv hK hE h + have hRR : ∀ b, IoRSR st K.toKLayout Locks s b → InvRSPA st K Locks s b := fun _ h => ioRSR_invL hK hE h have hsw : InvWSPA st K Locks s (stA (L.curW s)) := ioWSPA_inv hU (Or.inl ⟨(PE.stw (Locks := Locks)).2, PE.st_nsN (K := K.toKLayoutV) (nsK_of hK.base) hG.ub⟩) by_cases hlen : length < 2#u64 @@ -185,13 +273,13 @@ theorem invBlkPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω grd_sub hRR exact truncFinR (gS := guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) hA PE hW (SWr.of_W hsw) hcs hsK hbuf C hcap hlen hy hz' - · have E := delEntP_of (P := PrePA st Cs Ω Sg Ac Locks p K m) hE hq hcdt hE.1.slots0 + · have E := delEntP_ofL hE obtain ⟨hdec, hrep⟩ := haveI : (GRd (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) st (IoRSPV st K Locks s)) := grd_sub (fun _ h => ioRSPV_inv h) haveI : (GWr (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) st (IoWSPA st K Locks s)) := gwr_sub (fun _ h => ioWSPA_inv hU h) delAccPA (gS := guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) hA hV h0T hag hc hE.2.2.2.2.1 - hE.2.2.2.2.2 hG.ub hK hU PE E hsK hlen C hcap hy + hE.2.2.2.2.2.1 hG.ub hK hU PE E hsK hlen C hcap hy refine ⟨hdec, fun z hz' => ⟨hrep z hz', ?_⟩⟩ haveI : GRd (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) st (IoRSR st K.toKLayout Locks s) := grd_sub hRR @@ -199,25 +287,57 @@ theorem invBlkPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω (fun _ _ _ asid va _ _ _ => ⟨spe_ne0T hV hag h0T asid va, vsRdT hV hag hc asid va⟩) (fun _ _ _ he => leafVsWT hV hag he) C hcap hy hz' +/-- (P2-A64I gen 10) **Both arms of the PTE-arm decode block on `guard st (InvRSPA s) (InvWSPA s)`**, `FinOK` over +`IoRSR s` (module docstring). Hypotheses: [J] `hbuf`, [J] `hcdt`. -/ +theorem invBlkPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hK : RegionOKP K) (hU : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + {s : S} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hbuf : InvBufJ8 st K.toKLayout s) + {a : A} {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} + {cap : capability.Cap} (emi : msg.msg_info st s (L.curW s) = ok info) + (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) (y : (core.result.Result Unit error.Exc) × S × A) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) : + syscall.decode_invocation (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) ar s2 a1 + info.label length p.slot cap true true true false buf p.extra = ok y ∧ + ∀ z, invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z → + invRep (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) (L.curW s) y.2.1 y.2.2 y.1 = ok z ∧ + z.2.2 = core.result.Result.Ok () ∧ + FinOK (L := L) (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) + (IoRSR st K.toKLayout Locks s) (InvWSPA st K Locks s) ar s z.1 z.2.1 := + invBlkPAL hA hK hU hE.toL hbuf emi C hcap y hy + /-- (P2-A64I gen 10) **`acc` of the AArch64 PTE-arm path**: an accepted run on `guard st (InvRSPA s) (InvWSPA s)` -ends as the real one. Hypotheses: [J] `hK`, [J] `hU`, [J] `hcdt`, [J] `hbuf`. -/ -theorem invAccPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} +ends as the real one. Hypotheses: [J] `hK`, [J] `hU` (P2-Sync gen 5: at `PrePAL`). -/ +theorem invAccPAL {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hK : RegionOKP K) (hU : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) - {s s' : S} {a a' : A} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) + {s s' : S} {a a' : A} (hE : PrePAL st Cs Ω Sg Ac Locks p K m s) (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : syscall.handle_invocation_local (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) ar s a number p = ok (true, s', a') := by have PE := hE.2.1.1.1.1 haveI : GRd (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) st (IoRSR st K.toKLayout Locks s) := - grd_sub (fun _ h => ioRSR_inv hK hE h) + grd_sub (fun _ h => ioRSR_invL hK hE h) exact inv_acc_genR (W := InvWSPA st K Locks s) hA PE ((PE.ioWrDN (nsK_of hK.base) hE.2.2.1.ub).mono (fun _ h => ioWSPA_inv hU h)) - (invSeenR_ofA hE) (invPsR_ofA hE) + (invSeenR_ofAL hE) (invPsR_ofAL hE) (fun info nx s1 s2 a1 buf length cap emi hc' _ hk y hy => - invBlkPA hA hK hU hcdt hE (hbuf s hE) emi hc' hk y hy) h + invBlkPAL hA hK hU hE hE.2.2.2.2.2.2.2 emi hc' hk y hy) h + +/-- (P2-A64I gen 10) **`acc` of the AArch64 PTE-arm path**: an accepted run on `guard st (InvRSPA s) (InvWSPA s)` +ends as the real one. Hypotheses: [J] `hK`, [J] `hU`, [J] `hcdt`, [J] `hbuf`. -/ +theorem invAccPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hK : RegionOKP K) (hU : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) + {s s' : S} {a a' : A} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + syscall.handle_invocation_local (guard st (InvRSPA st K Locks s) (InvWSPA st K Locks s)) ar s a number p = + ok (true, s', a') := + invAccPAL hA hK hU hE.toL h end blk @@ -227,15 +347,15 @@ section obl variable {S : Type} {st : machine.Store S} [L : LawfulStore st] variable {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} -/-- **`read_own` of the AArch64 PTE-arm path** (`ioRSPV_rperm`: the unbound caller from `PrePA`; no `K.vcpus = ∅`). -/ -theorem invRSPA_read_own : - ∀ s x, PrePA st Cs Ω Sg Ac Locks p K m s → InvRSPA st K Locks s x → ¬ x ∈ K.user → +/-- (P2-Sync gen 5: at `PrePAL`) **`read_own` of the AArch64 PTE-arm path** (`ioRSPV_rperm`: the unbound caller from `PrePA`; no `K.vcpus = ∅`). -/ +theorem invRSPA_read_ownL : + ∀ s x, PrePAL st Cs Ω Sg Ac Locks p K m s → InvRSPA st K Locks s x → ¬ x ∈ K.user → PathObl.RPerm (ownP K) (L.cpu s) (Locks s) (L.mem s) x := fun _ _ hE hr hu => ioRSPV_rperm hE.2.2.1.ub (hr.2.resolve_left hu) -/-- **`write_own` of the AArch64 PTE-arm path** (the declined words: `IoPre.ds_notSpecial`, no VCPU assumption). -/ -theorem invWSPA_write_own (hK : RegionOKP K) : - ∀ s x, PrePA st Cs Ω Sg Ac Locks p K m s → (InvWSPA st K Locks s x ∨ IoDS (st := st) s x) → ¬ x ∈ K.user → +/-- (P2-Sync gen 5: at `PrePAL`) **`write_own` of the AArch64 PTE-arm path** (the declined words: `IoPre.ds_notSpecial`, no VCPU assumption). -/ +theorem invWSPA_write_ownL (hK : RegionOKP K) : + ∀ s x, PrePAL st Cs Ω Sg Ac Locks p K m s → (InvWSPA st K Locks s x ∨ IoDS (st := st) s x) → ¬ x ∈ K.user → PathObl.WPerm (ownP K) (L.cpu s) (Locks s) (L.mem s) x := by intro s x hE hx hu rcases hx with hx | hx @@ -247,10 +367,27 @@ theorem invWSPA_write_own (hK : RegionOKP K) : · exact (PE.nd (Locks := Locks) _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])).2.2 · exact (PE.nd (Locks := Locks) _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])).2.2 +/-- (P2-Sync gen 5: at `PrePAL`) **`aligned` of the AArch64 PTE-arm path.** -/ +theorem invWSPA_alignedL : + ∀ s x, PrePAL st Cs Ω Sg Ac Locks p K m s → (InvWSPA st K Locks s x ∨ IoDS (st := st) s x) → Aligned x := + fun s _ hE hx => hx.elim (fun h => h.1) (fun h => (IoDS.safe hE.2.1.1.1.1 h).1) + +/-- **`read_own` of the AArch64 PTE-arm path** (`ioRSPV_rperm`: the unbound caller from `PrePA`; no `K.vcpus = ∅`). -/ +theorem invRSPA_read_own : + ∀ s x, PrePA st Cs Ω Sg Ac Locks p K m s → InvRSPA st K Locks s x → ¬ x ∈ K.user → + PathObl.RPerm (ownP K) (L.cpu s) (Locks s) (L.mem s) x := + fun s x hE => invRSPA_read_ownL s x hE.toL + +/-- **`write_own` of the AArch64 PTE-arm path** (the declined words: `IoPre.ds_notSpecial`, no VCPU assumption). -/ +theorem invWSPA_write_own (hK : RegionOKP K) : + ∀ s x, PrePA st Cs Ω Sg Ac Locks p K m s → (InvWSPA st K Locks s x ∨ IoDS (st := st) s x) → ¬ x ∈ K.user → + PathObl.WPerm (ownP K) (L.cpu s) (Locks s) (L.mem s) x := + fun s x hE => invWSPA_write_ownL hK s x hE.toL + /-- **`aligned` of the AArch64 PTE-arm path.** -/ theorem invWSPA_aligned : ∀ s x, PrePA st Cs Ω Sg Ac Locks p K m s → (InvWSPA st K Locks s x ∨ IoDS (st := st) s x) → Aligned x := - fun s _ hE hx => hx.elim (fun h => h.1) (fun h => (IoDS.safe hE.2.1.1.1.1 h).1) + fun s x hE => invWSPA_alignedL s x hE.toL end obl @@ -260,12 +397,11 @@ section mv variable {S : Type} {st : machine.Store S} [L : LawfulStore st] /-- (P2-A64I gen 10) **Both arms of the PTE-arm decode block on the checked store `gd st (InvRSPA s) (PWs …)`** -(module docstring). Hypotheses: [J] `hbuf`, [J] `hcdt`. -/ -theorem invBlkGdPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} +(module docstring; P2-Sync gen 5: at `PrePAL`). Hypothesis: [J] `hbuf`. -/ +theorem invBlkGdPAL {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hKP : RegionOKP K) (hUP : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - {s : S} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hbuf : InvBufJ8 st K.toKLayout s) + {s : S} (hE : PrePAL st Cs Ω Sg Ac Locks p K m s) (hbuf : InvBufJ8 st K.toKLayout s) {a : A} {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) @@ -297,7 +433,7 @@ theorem invBlkGdPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs SWr (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) st (L.curW s) v := fun v hv _ => OwnMap.gds_write_state (PE.hT _ PE.cT) hS0 (fun hq => absurd hq (nq_run hv)) - have hRR : ∀ b, IoRSR st K.toKLayout Locks s b → InvRSPA st K Locks s b := fun _ h => ioRSR_inv hKP hE h + have hRR : ∀ b, IoRSR st K.toKLayout Locks s b → InvRSPA st K Locks s b := fun _ h => ioRSR_invL hKP hE h by_cases hlen : length < 2#u64 · have hWu : ∀ b, UserW K.toKLayout b → WAll (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s)) b := @@ -309,7 +445,7 @@ theorem invBlkGdPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs haveI : GRd (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) st (IoRSR st K.toKLayout Locks s) := gdr_sub hRR exact truncFinR hA PE hW (hst _ (Or.inr rfl)) hcs hsK hbuf C hcap hlen hy hz' - · have E := delEntP_of (P := PrePA st Cs Ω Sg Ac Locks p K m) hE hq hcdt hJ.slots0 + · have E := delEntP_ofL hE have hwS : ∀ q o, q ∈ K.slots → Locks s q → o.val < 32 → o.val % 8 = 0 → 8 ≤ o.val → (16 ≤ o.val ∨ (¬ TcbSlot K.toKLayout q ∧ NullOrFrame (L.mem s q))) → WAll (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s)) (core.num.U64.wrapping_add q o) := by @@ -329,7 +465,7 @@ theorem invBlkGdPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs -- gen 12: the walk read set off the leaves and the VCPUs' words (`X := VsX K`, `vsJXV`; gen 10: `K.ptes`) obtain ⟨hdec, hrep⟩ := delAcc2XR (X := VsX K) (R := InvRSPA st K Locks s) (gS := gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) - hA PE hW hwS hw0 hst E hsK hlen (vsJXV hV hag hc h0T hE.2.2.2.2.2.atW) + hA PE hW hwS hw0 hst E hsK hlen (vsJXV hV hag hc h0T hE.2.2.2.2.2.1.atW) (fun b h => h.elim (fun hb => ⟨(hUP.kern b hb).1, hKP.none b hb, (hUP.kern b hb).2⟩) And.left) hRR (fun b hv hn => ioRSPV_inv ⟨⟨VsW.ioRS hv, fun h => absurd (Or.inl h) hn, noGuestO_none hv.2.1⟩, fun _ hvc => hn (Or.inr ⟨hv, hvc⟩)⟩) @@ -346,22 +482,45 @@ theorem invBlkGdPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs (fun _ _ _ asid va _ _ _ => ⟨spe_ne0T hV hag h0T asid va, vsRdT hV hag hc asid va⟩) (fun _ _ _ he => leafVsWT hV hag he) C hcap hy hz' +/-- (P2-A64I gen 10) **Both arms of the PTE-arm decode block on the checked store `gd st (InvRSPA s) (PWs …)`** +(module docstring). Hypotheses: [J] `hbuf`, [J] `hcdt`. -/ +theorem invBlkGdPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hKP : RegionOKP K) (hUP : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + {s : S} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hbuf : InvBufJ8 st K.toKLayout s) + {a : A} {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} + {cap : capability.Cap} (emi : msg.msg_info st s (L.curW s) = ok info) + (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) (y : (core.result.Result Unit error.Exc) × S × A) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) : + syscall.decode_invocation (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) + (L.mem s))) ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y ∧ + ∀ z, invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z → + invRep (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) + (L.curW s) y.2.1 y.2.2 y.1 = ok z ∧ + z.2.2 = core.result.Result.Ok () ∧ + FinOK (L := L) (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) + (IoRSR st K.toKLayout Locks s) (WAll (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) + ar s z.1 z.2.1 := + invBlkGdPAL hA hKP hUP hE.toL hbuf emi C hcap y hy + /-- (P2-A64I gen 10) **`move` of accepted AArch64 PTE-arm runs** (module docstring): no VCPU assumption — the `mguard` run for `own` lifts by `stepOKV_of_stepOK hK.base` and `stepOKP_of_stepOKV`. Hypotheses: [J] `hKP`, -[J] `hUP`, [J] `hcdt`, [J] `hbuf`, `hp` (the program premise). -/ -theorem invMovePA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} +[J] `hUP`, `hp` (the program premise); P2-Sync gen 5: at any `P` inside `PrePAL`. -/ +theorem invMovePAG {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hKP : RegionOKP K) (hUP : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) {q : SProg Bool} + {P : S → Prop} (hP : ∀ s, P s → PrePAL st Cs Ω Sg Ac Locks p K m s) {q : SProg Bool} (hp : IsProgOn (fun m s a => syscall.handle_invocation_local m ar s a number p) ar st - (AltOf st (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) (OwnMap.cls K.toKLayout) + (AltOf st P (InvRSPA st K Locks) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user)) q) - {s s' : S} {a a' : A} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) + {s s' : S} {a a' : A} (hE0 : P s) (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : syscall.handle_invocation_local (mguard st (fun x y => PathObl.StepOK (ownP K) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) (L.cpu s) (Locks s) (L.mem x) (L.mem y))) ar s a number p = ok (true, s', a') := by + have hE := hP s hE0 have hq := hE.2.1 have PE := hq.1.1.1 have hK := hE.1.regionOK @@ -369,15 +528,33 @@ theorem invMovePA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) ar s a number p = ok (true, s', a') := haveI : GRd (gd st (InvRSPA st K Locks s) (OwnMap.PWs (L := L) K.toKLayout (L.cpu s) (Locks s) (L.mem s))) st - (IoRSR st K.toKLayout Locks s) := gdr_sub (fun _ h => ioRSR_inv hKP hE h) - inv_acc_genR hA PE (mvWrS PE hK hE.1.ioLayA) (invSeenR_ofA hE) (invPsR_ofA hE) + (IoRSR st K.toKLayout Locks s) := gdr_sub (fun _ h => ioRSR_invL hKP hE h) + inv_acc_genR hA PE (mvWrS PE hK hE.1.ioLayA) (invSeenR_ofAL hE) (invPsR_ofAL hE) (fun info nx s1 s2 a1 buf length cap emi hc' _ hk y hy => - invBlkGdPA hA hKP hUP hcdt hE (hbuf s hE) emi hc' hk y hy) h - rw [inv_hp hp (Narrow.gd _ _ _) (AltOf.pre hE) a] at hg - rw [inv_hp hp (Narrow.mguard _ _) (AltOf.pre hE) a] + invBlkGdPAL hA hKP hUP hE hE.2.2.2.2.2.2.2 emi hc' hk y hy) h + rw [inv_hp hp (Narrow.gd _ _ _) (AltOf.pre hE0) a] at hg + rw [inv_hp hp (Narrow.mguard _ _) (AltOf.pre hE0) a] exact interp_mguard_mono (fun x y hs => stepOKP_of_stepOKV (stepOKV_of_stepOK hKP.base hs)) q s a _ (OwnMap.run_gds_mguard hK hE.1.userOK hq.1.1.2 q hg) +/-- (P2-A64I gen 10) **`move` of accepted AArch64 PTE-arm runs** (module docstring): no VCPU assumption — the +`mguard` run for `own` lifts by `stepOKV_of_stepOK hK.base` and `stepOKP_of_stepOKV`. Hypotheses: [J] `hKP`, +[J] `hUP`, [J] `hcdt`, [J] `hbuf`, `hp` (the program premise). -/ +theorem invMovePA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hKP : RegionOKP K) (hUP : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) {q : SProg Bool} + (hp : IsProgOn (fun m s a => syscall.handle_invocation_local m ar s a number p) ar st + (AltOf st (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) (OwnMap.cls K.toKLayout) + (OwnMap.ko K.toKLayout m) (· ∈ K.user)) q) + {s s' : S} {a a' : A} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + syscall.handle_invocation_local (mguard st (fun x y => PathObl.StepOK (ownP K) (OwnMap.cls K.toKLayout) + (OwnMap.ko K.toKLayout m) (· ∈ K.user) (L.cpu s) (Locks s) (L.mem x) (L.mem y))) ar s a number p = + ok (true, s', a') := + invMovePAG hA hKP hUP (fun _ h => h.toL) hp hE h + end mv /-! ## `dec_alt` -/ @@ -385,6 +562,29 @@ end mv section dec variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +/-- (P2-A64I gen 10) **`dec_alt` of the AArch64 PTE-arm path** (module docstring): no VCPU assumption; P2-Sync gen 5: at any `P` inside `PrePAL`. -/ +theorem invDecPAG {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hKP : RegionOKP K) {P : S → Prop} (hP : ∀ s, P s → PrePAL st Cs Ω Sg Ac Locks p K m s) {q : SProg Bool} + (hp : IsProgOn (fun m s a => syscall.handle_invocation_local m ar s a number p) ar st + (AltOf st P (InvRSPA st K Locks) (OwnMap.cls K.toKLayout) + (OwnMap.ko K.toKLayout m) (· ∈ K.user)) q) + {s s' : S} {a : A} {r r' : Bool × S × A} (hE0 : P s) + (h : syscall.handle_invocation_local st ar s a number p = ok r) (hr : r.1 ≠ true) + (Al : Alt st (InvRSPA st K Locks s) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) s s') + (h' : syscall.handle_invocation_local st ar s' a number p = ok r') : + r'.1 ≠ true ∧ + syscall.handle_invocation_local (guard st (fun _ => True) (IoDS (st := st) s)) ar s' a number p = ok r' ∧ + syscall.handle_invocation_local (mguard st (fun x y => PathObl.StepOK (ownP K) (OwnMap.cls K.toKLayout) + (OwnMap.ko K.toKLayout m) (· ∈ K.user) (L.cpu s) (Locks s) (L.mem x) (L.mem y))) ar s' a number p = + ok r' := by + have hE := hP s hE0 + obtain ⟨h1, h2, h3⟩ := inv_dec_altR hA hE.1.regionOK hE.1.ioLayA hE.2.1.1.1.1 (invSeenR_ofAL hE) h hr + (Al.anti (fun x hx => ioRSR_invL hKP hE hx)) h' + refine ⟨h1, h2, ?_⟩ + rw [inv_hp hp (Narrow.mguard _ _) ⟨s, hE0, Al⟩ a] at h3 ⊢ + exact interp_mguard_mono (fun x y hs => stepOKP_of_stepOKV (stepOKV_of_stepOK hKP.base hs)) q s' a _ h3 + /-- (P2-A64I gen 10) **`dec_alt` of the AArch64 PTE-arm path** (module docstring): no VCPU assumption. -/ theorem invDecPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} @@ -400,12 +600,8 @@ theorem invDecPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω syscall.handle_invocation_local (guard st (fun _ => True) (IoDS (st := st) s)) ar s' a number p = ok r' ∧ syscall.handle_invocation_local (mguard st (fun x y => PathObl.StepOK (ownP K) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) (L.cpu s) (Locks s) (L.mem x) (L.mem y))) ar s' a number p = - ok r' := by - obtain ⟨h1, h2, h3⟩ := inv_dec_altR hA hE.1.regionOK hE.1.ioLayA hE.2.1.1.1.1 (invSeenR_ofA hE) h hr - (Al.anti (fun x hx => ioRSR_inv hKP hE hx)) h' - refine ⟨h1, h2, ?_⟩ - rw [inv_hp hp (Narrow.mguard _ _) ⟨s, hE, Al⟩ a] at h3 ⊢ - exact interp_mguard_mono (fun x y hs => stepOKP_of_stepOKV (stepOKV_of_stepOK hKP.base hs)) q s' a _ h3 + ok r' := + invDecPAG hA hKP (fun _ h => h.toL) hp hE h hr Al h' end dec diff --git a/proofs/Fl4ProofsA64/P2/InvDelPteLPA.lean b/proofs/Fl4ProofsA64/P2/InvDelPteLPA.lean index 6b8e351bc..dda5922aa 100644 --- a/proofs/Fl4ProofsA64/P2/InvDelPteLPA.lean +++ b/proofs/Fl4ProofsA64/P2/InvDelPteLPA.lean @@ -44,69 +44,100 @@ variable {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) variable {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} +include hA in +/-- (P2-Sync gen 5) **`IsProgOn` of the AArch64 PTE-arm path** over the alternatives of any `P` inside `PrePAL`. +Hypothesis: [J] `hK`. -/ +theorem inv_isProgOnPAG {P : S → Prop} (hP : ∀ s, P s → PrePAL st Cs Ω Sg Ac Locks p K m s) (hK : RegionOKP K) : + IsProgOn (fun m s a => syscall.handle_invocation_local m ar s a number p) ar st + (AltOf st P (InvRSPA st K Locks) (OwnMap.cls K.toKLayout) + (OwnMap.ko K.toKLayout m) (· ∈ K.user)) (R.invBodyG number p) := by + have hSC : InvSlotCN st K.toKLayout p P := fun s hE => by + obtain ⟨cp, cr, cg, cgb, -, -, -, -, -, -, -, hcs, hsK, -⟩ := del_coreP (hP s hE).2.1 + exact ⟨hsK, cp, cr, cg, cgb, hcs⟩ + exact (inv_isProgOnR (Locks := Locks) (m0 := m) hA (P := P) + (fun _ h => (hP _ h).2.1.1.1.1) hSC (inv_isProg S number p)).mono + (fun _ ⟨s, hE, Al⟩ => ⟨s, hE, Al.anti (fun _ hx => ioRSR_invL hK (hP s hE) hx)⟩) + include hA in /-- (P2-A64I gen 10) **`IsProgOn` of the AArch64 PTE-arm path over `InvRSPA`'s alternatives.** Hypothesis (gen 12): [J] `hK` (`ioRSR_inv`). -/ theorem inv_isProgOnPA (hK : RegionOKP K) : IsProgOn (fun m s a => syscall.handle_invocation_local m ar s a number p) ar st (AltOf st (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) (OwnMap.cls K.toKLayout) - (OwnMap.ko K.toKLayout m) (· ∈ K.user)) (R.invBodyG number p) := by - have hSC : InvSlotCN st K.toKLayout p (PrePA st Cs Ω Sg Ac Locks p K m) := fun s hE => by - obtain ⟨cp, cr, cg, cgb, -, -, -, -, -, -, -, hcs, hsK, -⟩ := del_coreP hE.2.1 - exact ⟨hsK, cp, cr, cg, cgb, hcs⟩ - exact (inv_isProgOnR (Locks := Locks) (m0 := m) hA (P := PrePA st Cs Ω Sg Ac Locks p K m) - (fun _ h => h.2.1.1.1.1) hSC (inv_isProg S number p)).mono - (fun _ ⟨s, hE, Al⟩ => ⟨s, hE, Al.anti (fun _ hx => ioRSR_inv hK hE hx)⟩) + (OwnMap.ko K.toKLayout m) (· ∈ K.user)) (R.invBodyG number p) := + inv_isProgOnPAG hA (fun _ h => h.toL) hK include hA in -/-- (P2-A64I gen 10) **`PathObl` of the AArch64 PTE-arm path.** Hypotheses: [J] `hK`, [J] `hU`, [J] `hcdt`, -[J] `hbuf`. -/ -theorem invOblPA (hK : RegionOKP K) (hU : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) : +/-- (P2-Sync gen 5) **`PathObl` of the AArch64 PTE-arm path** at any `P` inside `PrePAL`. Hypotheses: [J] `hK`, +[J] `hU`. -/ +theorem invOblPAG {P : S → Prop} (hP : ∀ s, P s → PrePAL st Cs Ω Sg Ac Locks p K m s) (hK : RegionOKP K) + (hU : UserOKP K) : PathObl st (fun m s a => syscall.handle_invocation_local m ar s a number p) (fun b => b = true) (ownP K) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) Locks - (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) (InvWSPA st K Locks) (IoDS (st := st)) := by - have hp := inv_isProgOnPA (st := st) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (Locks := Locks) - (number := number) (p := p) (K := K) (m := m) hA hK - refine ⟨?_, ?_, invRSPA_read_own, invWSPA_write_own hK, invWSPA_aligned, ?_⟩ + P (InvRSPA st K Locks) (InvWSPA st K Locks) (IoDS (st := st)) := by + have hp := inv_isProgOnPAG (st := st) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (Locks := Locks) + (number := number) (p := p) (K := K) (m := m) hA hP hK + refine ⟨?_, ?_, fun s x hE => invRSPA_read_ownL s x (hP s hE), fun s x hE => invWSPA_write_ownL hK s x (hP s hE), + fun s x hE => invWSPA_alignedL s x (hP s hE), ?_⟩ · rintro s a ⟨b, s1, a1⟩ hE h rfl - exact invAccPA hA hK hU hcdt hbuf hE h + exact invAccPAL hA hK hU (hP s hE) h · intro s a r hE h hn s' r' Al h' - exact invDecPA hA hK hp hE h hn Al h' + exact invDecPAG hA hK hP hp hE h hn Al h' · rintro s a ⟨b, s1, a1⟩ hE h cases b - · exact (invDecPA hA hK hp hE h Bool.false_ne_true (Alt.refl s) h).2.2 - · exact invMovePA hA hK hU hcdt hbuf hp hE h + · exact (invDecPAG hA hK hP hp hE h Bool.false_ne_true (Alt.refl s) h).2.2 + · exact invMovePAG hA hK hU hP hp hE h include hA in -/-- (P2-A64I gen 10) **`read_aligned` of the AArch64 PTE-arm path.** Hypotheses as `invOblPA`, [P] `hm`. -/ -theorem invRaPA (hK : RegionOKP K) (hU : UserOKP K) +/-- (P2-A64I gen 10) **`PathObl` of the AArch64 PTE-arm path.** Hypotheses: [J] `hK`, [J] `hU`, [J] `hcdt`, +[J] `hbuf`. -/ +theorem invOblPA (hK : RegionOKP K) (hU : UserOKP K) (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) (hm : E2E.MasksWr st) : - ∀ s s' a r, PrePA st Cs Ω Sg Ac Locks p K m s → + (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) : + PathObl st (fun m s a => syscall.handle_invocation_local m ar s a number p) (fun b => b = true) + (ownP K) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) Locks + (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) (InvWSPA st K Locks) (IoDS (st := st)) := + invOblPAG hA (fun _ h => h.toL) hK hU + +include hA in +/-- (P2-A64I gen 10) **`read_aligned` of the AArch64 PTE-arm path.** Hypotheses as `invOblPAG`, [P] `hm`; P2-Sync gen 5: at any `P` inside `PrePAL`. -/ +theorem invRaPAG {P : S → Prop} (hP : ∀ s, P s → PrePAL st Cs Ω Sg Ac Locks p K m s) (hK : RegionOKP K) + (hU : UserOKP K) (hm : E2E.MasksWr st) : + ∀ s s' a r, P s → Alt st (InvRSPA st K Locks s) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) s s' → syscall.handle_invocation_local st ar s' a number p = ok r → syscall.handle_invocation_local (guard st (fun b => Aligned b) (fun _ => True)) ar s' a number p = ok r := by - intro s s' a r hE Al h - have hp := inv_isProgOnPA (st := st) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (Locks := Locks) - (number := number) (p := p) (K := K) (m := m) hA hK + intro s s' a r hE0 Al h + have hE := hP s hE0 + have hp := inv_isProgOnPAG (st := st) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (Locks := Locks) + (number := number) (p := p) (K := K) (m := m) hA hP hK have hz := inv_noZero number p by_cases hac : ∃ s1 a1, syscall.handle_invocation_local st ar s a number p = ok (true, s1, a1) · obtain ⟨s1, a1, h0⟩ := hac - have hg := invAccPA hA hK hU hcdt hbuf hE h0 - rw [inv_hp hp (Narrow.guard _ _ _) (AltOf.pre hE) a] at hg + have hg := invAccPAL hA hK hU hE h0 + rw [inv_hp hp (Narrow.guard _ _ _) (AltOf.pre hE0) a] at hg obtain ⟨s1', hg'⟩ := interp_agree hm (fun w hw => hw.1) (fun w hw => hw.1) _ hz s s' a true s1 a1 Al.ksb Al.cpu Al.masks Al.read hg have hs := interp_to (st := st) (st' := st) (fun _ _ _ => rfl) (fun _ _ _ _ => rfl) (fun _ _ _ _ => rfl) rfl rfl rfl _ hz s' a _ hg' show syscall.handle_invocation_local _ ar s' a number p = ok r - rw [inv_hp hp (Narrow.self st) ⟨s, hE, Al⟩ a] at h - rw [inv_hp hp (Narrow.guard _ _ _) ⟨s, hE, Al⟩ a, ← h, hs] + rw [inv_hp hp (Narrow.self st) ⟨s, hE0, Al⟩ a] at h + rw [inv_hp hp (Narrow.guard _ _ _) ⟨s, hE0, Al⟩ a, ← h, hs] exact interp_mono (fun _ hw => hw.1) (fun _ _ => trivial) _ hz s' a _ hg' · obtain ⟨⟨b, z, c⟩, h0⟩ := inv_total (st := st) hA number p s a have hr0 : b ≠ true := fun e => hac ⟨z, c, by rw [← e]; exact h0⟩ - exact inv_ra_decR hA hE.2.1.1.1.1 (invSeenR_ofA hE) (Al.anti (fun _ hx => ioRSR_inv hK hE hx)) h0 hr0 h + exact inv_ra_decR hA hE.2.1.1.1.1 (invSeenR_ofAL hE) (Al.anti (fun _ hx => ioRSR_invL hK hE hx)) h0 hr0 h + +include hA in +/-- (P2-A64I gen 10) **`read_aligned` of the AArch64 PTE-arm path.** Hypotheses as `invOblPA`, [P] `hm`. -/ +theorem invRaPA (hK : RegionOKP K) (hU : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) (hm : E2E.MasksWr st) : + ∀ s s' a r, PrePA st Cs Ω Sg Ac Locks p K m s → + Alt st (InvRSPA st K Locks s) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) s s' → + syscall.handle_invocation_local st ar s' a number p = ok r → + syscall.handle_invocation_local (guard st (fun b => Aligned b) (fun _ => True)) ar s' a number p = ok r := + invRaPAG hA (fun _ h => h.toL) hK hU hm variable (st) in /-- [J] (shape) **A user-word write keeps the AArch64 PTE-arm start fact and the path's sets** (the `huf` of @@ -118,6 +149,15 @@ def DelPteHufA (Cs Ω Sg Ac : Set U64) (Locks : S → U64 → Prop) (p : localin InvRSPA st K Locks (L.wr s b v) = InvRSPA st K Locks s ∧ InvWSPA st K Locks (L.wr s b v) = InvWSPA st K Locks s ∧ IoDS (st := st) (L.wr s b v) = IoDS (st := st) s +variable (st) in +/-- [J] (shape; P2-Sync gen 5) **`DelPteHufA` at `PrePAL`** (the `huf` of `inv_localPathPAL`). -/ +def DelPteHufAL (Cs Ω Sg Ac : Set U64) (Locks : S → U64 → Prop) (p : localinv.Plan) (K : KLayoutP) (m : U64 → U64) : + Prop := + ∀ s b v, PrePAL st Cs Ω Sg Ac Locks p K m s → b ∈ K.user → Aligned b → + PrePAL st Cs Ω Sg Ac Locks p K m (L.wr s b v) ∧ Locks (L.wr s b v) = Locks s ∧ + InvRSPA st K Locks (L.wr s b v) = InvRSPA st K Locks s ∧ InvWSPA st K Locks (L.wr s b v) = InvWSPA st K Locks s ∧ + IoDS (st := st) (L.wr s b v) = IoDS (st := st) s + end lp /-! ## `ReadOnce` -/ @@ -126,12 +166,11 @@ section ro variable {S : Type} {st : machine.Store S} [L : LawfulStore st] /-- (P2-A64I gen 10) **The `length ≥ 2` arm of the PTE-arm decode block on `guard st (IoRSPV s) (IoWSPA s)`** (no -user word read or written; gen 12: `IoRSPV`, no VCPU word). Hypothesis: [J] `hcdt`. -/ -theorem invBlkPLA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} +user word read or written; gen 12: `IoRSPV`, no VCPU word; P2-Sync gen 5: at `PrePAL`). -/ +theorem invBlkPLAL {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hK : RegionOKP K) (hU : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - {s : S} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hL : InvLen st s) + {s : S} (hE : PrePAL st Cs Ω Sg Ac Locks p K m s) (hL : InvLen st s) {a : A} {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) @@ -153,9 +192,9 @@ theorem invBlkPLA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω obtain ⟨cp, cr, cg, cgb, -, -, -, -, -, -, -, hcs, hsK, -⟩ := del_coreP hq rw [(hq.1.2.2.1 (L.curW s) info (cur_thread_ok s) emi).1] at hy ⊢ have hlen : ¬ length < 2#u64 := len_ge2 (hL (L.curW s) info (cur_thread_ok s) emi) hl - have E := delEntP_of (P := PrePA st Cs Ω Sg Ac Locks p K m) hE hq hcdt hE.1.slots0 + have E := delEntP_ofL hE obtain ⟨hdec, hrep⟩ := delAccPA (gS := guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) hA hV h0T hag hc - hE.2.2.2.2.1 hE.2.2.2.2.2 hG.ub hK hU PE E hsK hlen C hcap hy + hE.2.2.2.2.1 hE.2.2.2.2.2.1 hG.ub hK hU PE E hsK hlen C hcap hy refine ⟨hdec, fun z hz' => ⟨hrep z hz', ?_⟩⟩ haveI : GRd (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) st (IoRSR st K.toKLayout Locks s) := grd_sub (fun _ h => ioRSR_PV hc hK.base hE.2.2.2.2.1 h) @@ -164,68 +203,113 @@ theorem invBlkPLA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω (fun _ _ _ asid va _ _ _ => ⟨spe_ne0T hV hag h0T asid va, vsRdT hV hag hc asid va⟩) (fun _ _ _ he => leafVsWT hV hag he) C hcap hy hz' +/-- (P2-A64I gen 10) **The `length ≥ 2` arm of the PTE-arm decode block on `guard st (IoRSPV s) (IoWSPA s)`** (no +user word read or written; gen 12: `IoRSPV`, no VCPU word). Hypothesis: [J] `hcdt`. -/ +theorem invBlkPLA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hK : RegionOKP K) (hU : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + {s : S} (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hL : InvLen st s) + {a : A} {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} + {cap : capability.Cap} (emi : msg.msg_info st s (L.curW s) = ok info) + (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hl : (if info.length > msg.N_MSG_REGISTERS then (if buf = 0#u64 then ok msg.N_MSG_REGISTERS else ok info.length) + else ok info.length) = ok length) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) (y : (core.result.Result Unit error.Exc) × S × A) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) : + syscall.decode_invocation (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) ar s2 a1 + info.label length p.slot cap true true true false buf p.extra = ok y ∧ + ∀ z, invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z → + invRep (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) (L.curW s) y.2.1 y.2.2 y.1 = ok z ∧ + z.2.2 = core.result.Result.Ok () ∧ + FinOK (L := L) (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) + (IoRSR st K.toKLayout Locks s) (IoWSPA st K Locks s) ar s z.1 z.2.1 := + invBlkPLAL hA hK hU hE.toL hL emi C hl hcap y hy + /-- (P2-A64I gen 10) **An accepted PTE-arm run with `InvLen` runs on `guard st (IoRSPV s) (IoWSPA s)`.** -Hypothesis: [J] `hcdt`. -/ -theorem invAccPLA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} +P2-Sync gen 5: at `PrePAL`. -/ +theorem invAccPLAL {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hK : RegionOKP K) (hU : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) {s s' : S} {a a' : A} - (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hL : InvLen st s) + {s s' : S} {a a' : A} (hE : PrePAL st Cs Ω Sg Ac Locks p K m s) (hL : InvLen st s) (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : syscall.handle_invocation_local (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) ar s a number p = ok (true, s', a') := by have PE := hE.2.1.1.1.1 haveI : GRd (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) st (IoRSR st K.toKLayout Locks s) := grd_sub (fun _ h => ioRSR_PV hE.2.2.1.2.choose_spec.1 hK.base hE.2.2.2.2.1 h) - exact inv_acc_genR (W := IoWSPA st K Locks s) hA PE (PE.ioWrDN (nsK_of hK.base) hE.2.2.1.ub) (invSeenR_ofA hE) - (invPsR_ofA hE) - (fun info nx s1 s2 a1 buf length cap emi hc' hl hk y hy => invBlkPLA hA hK hU hcdt hE hL emi hc' hl hk y hy) h + exact inv_acc_genR (W := IoWSPA st K Locks s) hA PE (PE.ioWrDN (nsK_of hK.base) hE.2.2.1.ub) (invSeenR_ofAL hE) + (invPsR_ofAL hE) + (fun info nx s1 s2 a1 buf length cap emi hc' hl hk y hy => invBlkPLAL hA hK hU hE hL emi hc' hl hk y hy) h + +/-- (P2-A64I gen 10) **An accepted PTE-arm run with `InvLen` runs on `guard st (IoRSPV s) (IoWSPA s)`.** +Hypothesis: [J] `hcdt`. -/ +theorem invAccPLA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} + (hK : RegionOKP K) (hU : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) {s s' : S} {a a' : A} + (hE : PrePA st Cs Ω Sg Ac Locks p K m s) (hL : InvLen st s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + syscall.handle_invocation_local (guard st (IoRSPV st K Locks s) (IoWSPA st K Locks s)) ar s a number p = + ok (true, s', a') := + invAccPLAL hA hK hU hE.toL hL h /-- (P2-A64I gen 10) **`ReadOnce` of the AArch64 PTE-arm path** (module docstring). Hypotheses: [P] `hm`, [J] `hK`, -[J] `hU`, [J] `hcdt`, [J] `hbuf`. -/ -theorem invRoPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) +[J] `hU`; P2-Sync gen 5: at any `P` inside `PrePAL`. -/ +theorem invRoPAG {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} {m : U64 → U64} (hK : RegionOKP K) (hU : UserOKP K) - (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) - (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) : + {P : S → Prop} (hP : ∀ s, P s → PrePAL st Cs Ω Sg Ac Locks p K m s) : ReadOnce st ar (R.invBodyG number p) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) - (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) := by - intro s s' a r hE Al h - have hp := inv_isProgOnPA (st := st) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (Locks := Locks) - (number := number) (p := p) (K := K) (m := m) hA hK + P (InvRSPA st K Locks) := by + intro s s' a r hE0 Al h + have hE := hP s hE0 + have hp := inv_isProgOnPAG (st := st) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (Locks := Locks) + (number := number) (p := p) (K := K) (m := m) hA hP hK have hz := inv_noZero number p by_cases hac : ∃ s1 a1, syscall.handle_invocation_local st ar s a number p = ok (true, s1, a1) · obtain ⟨s1, a1, h0⟩ := hac - have hg := invAccPA hA hK hU hcdt hbuf hE h0 - rw [inv_hp hp (Narrow.guard _ _ _) (AltOf.pre hE) a] at hg + have hg := invAccPAL hA hK hU hE h0 + rw [inv_hp hp (Narrow.guard _ _ _) (AltOf.pre hE0) a] at hg have o : Once st ar (InvRSPA st K Locks s) (· ∈ K.user) ∅ ∅ (R.invBodyG number p) s a := by by_cases hL : InvLen st s - · have hg2 := invAccPLA hA hK hU hcdt hE hL h0 - rw [inv_hp hp (Narrow.guard _ _ _) (AltOf.pre hE) a] at hg2 + · have hg2 := invAccPLAL hA hK hU hE hL h0 + rw [inv_hp hp (Narrow.guard _ _ _) (AltOf.pre hE0) a] at hg2 exact onceU_monoR (fun _ h => ioRSPV_inv h) _ ∅ ∅ ∅ s a (once_of_guardG (fun w hw => hw.1.1.2.1) _ hz ∅ ∅ s a _ hg2) subset_rfl · have h0' : interp st ar (R.invBodyG number p) s a = ok (true, s1, a1) := by - rw [← inv_hp hp (Narrow.self st) (AltOf.pre hE) a]; exact h0 + rw [← inv_hp hp (Narrow.self st) (AltOf.pre hE0) a]; exact h0 have hent := invU_entOnce (Locks := Locks) hA - (P := fun s => PrePA st Cs Ω Sg Ac Locks p K m s ∧ ¬ InvLen st s) - (fun _ h => h.1.2.1.1.1.1) (fun s h x hx => (invSeenR_ofA h.1 x hx).1) - (fun s h => ⟨(invPsR_ofA h.1).1.1, (invPsR_ofA h.1).2.1⟩) + (P := fun s => P s ∧ ¬ InvLen st s) + (fun _ h => (hP _ h.1).2.1.1.1.1) (fun s h x hx => (invSeenR_ofAL (hP _ h.1) x hx).1) + (fun s h => ⟨(invPsR_ofAL (hP _ h.1)).1.1, (invPsR_ofAL (hP _ h.1)).2.1⟩) (invU_decOnceP (Cs := Cs) (Ω := Ω) (Sg := Sg) (Ac := Ac) (m0 := m) hA - (P := PrePA st Cs Ω Sg Ac Locks p K m) (fun _ h => ⟨h.1, h.2.1⟩) hbuf) - s a _ ⟨hE, hL⟩ ⟨s1, a1, h0⟩ h0' + (P := P) (fun _ h => ⟨(hP _ h).1, (hP _ h).2.1⟩) fun s h => (hP s h).2.2.2.2.2.2.2) + s a _ ⟨hE0, hL⟩ ⟨s1, a1, h0⟩ h0' exact once_meetG (G := InvRSPA st K Locks s) (W := InvWSPA st K Locks s) (R' := InvRSPA st K Locks s) (fun _ hg _ => hg) _ hz ∅ ∅ ∅ s a _ hg hent subset_rfl exact once_agree hm (fun w hw => hw.1) (fun w hw => hw.1) _ hz s s' a _ ∅ ∅ Al.ksb Al.cpu Al.masks Al.read hg o · obtain ⟨⟨b, z, c⟩, h0⟩ := inv_total (st := st) hA number p s a have hr0 : b ≠ true := fun e => hac ⟨z, c, by rw [← e]; exact h0⟩ have h' : syscall.handle_invocation_local st ar s' a number p = ok r := by - rw [inv_hp hp (Narrow.self st) ⟨s, hE, Al⟩ a]; exact h - have hg := inv_dec_guardRSR hA hE.2.1.1.1.1 (invSeenR_ofA hE) (Al.anti (fun _ hx => ioRSR_inv hK hE hx)) h0 hr0 + rw [inv_hp hp (Narrow.self st) ⟨s, hE0, Al⟩ a]; exact h + have hg := inv_dec_guardRSR hA hE.2.1.1.1.1 (invSeenR_ofAL hE) (Al.anti (fun _ hx => ioRSR_invL hK hE hx)) h0 hr0 h' - rw [inv_hp hp (Narrow.guard _ _ _) ⟨s, hE, Al⟩ a] at hg - exact onceU_monoR (fun _ h => ioRSR_inv hK hE h) _ ∅ ∅ ∅ s' a + rw [inv_hp hp (Narrow.guard _ _ _) ⟨s, hE0, Al⟩ a] at hg + exact onceU_monoR (fun _ h => ioRSR_invL hK hE h) _ ∅ ∅ ∅ s' a (once_of_guardG (fun w hw => hw.1.2.1) _ hz ∅ ∅ s' a _ hg) subset_rfl +/-- (P2-A64I gen 10) **`ReadOnce` of the AArch64 PTE-arm path** (module docstring). Hypotheses: [P] `hm`, [J] `hK`, +[J] `hU`, [J] `hcdt`, [J] `hbuf`. -/ +theorem invRoPA {A : Type} {ar : machine.Arch A} (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {number : I64} {p : localinv.Plan} {K : KLayoutP} + {m : U64 → U64} (hK : RegionOKP K) (hU : UserOKP K) + (hcdt : InvCdtJ st K.toKLayout (PrePA st Cs Ω Sg Ac Locks p K m)) + (hbuf : ∀ s, PrePA st Cs Ω Sg Ac Locks p K m s → InvBufJ8 st K.toKLayout s) : + ReadOnce st ar (R.invBodyG number p) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) + (PrePA st Cs Ω Sg Ac Locks p K m) (InvRSPA st K Locks) := + invRoPAG hA hm hK hU fun _ h => h.toL + end ro /-! ## The path -/ @@ -265,6 +349,36 @@ noncomputable def inv_localPathPA (ar : machine.Arch A) (hA : P0.ArchTotal ar) { total := fun s a => inv_total hA number p s a masks := hm +/-- (P2-Sync gen 5) **The AArch64 Delete PTE-arm local path at `PrePAL`** (`inv_localPathPA` with the start fact +`PrePAL`: `CdtAt` at the locked slots only; a parallel path, `inv_localPathPA` is unchanged). Premises: `hA`, [P] `hm`, +[J] `hK`, [J] `hU`, [J] `huf`. -/ +noncomputable def inv_localPathPAL (ar : machine.Arch A) (hA : P0.ArchTotal ar) {Cs Ω Sg Ac : Set U64} + {Locks : S → U64 → Prop} (number : I64) (p : localinv.Plan) (hm : E2E.MasksWr st) + (K : KLayoutP) (m : U64 → U64) (hK : RegionOKP K) (hU : UserOKP K) + (huf : DelPteHufAL st Cs Ω Sg Ac Locks p K m) : + E2E.LocalPath (ownP K) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user) where + S := S + A := A + X := Bool + st := st + L := L + ar := ar + F := invBody S number p + p := R.invBodyG number p + Acc := fun b => b = true + Locks := Locks + Pre := PrePAL st Cs Ω Sg Ac Locks p K m + RS := InvRSPA st K Locks + WS := InvWSPA st K Locks + DS := IoDS (st := st) + isProg := inv_isProgOnPAG hA (fun _ h => h) hK + obl := invOblPAG hA (fun _ h => h) hK hU + side := ⟨invRaPAG hA (fun _ h => h) hK hU hm, fun _ _ _ hx => hx.1, + fun x hx => Fl4ProofsA64.P2.ko_aligned hK.base.base hU.base.base x hx, huf⟩ + once := invRoPAG hA hm hK hU fun _ h => h + total := fun s a => inv_total hA number p s a + masks := hm + end path end Fl4ProofsA64.P2.E2EP diff --git a/proofs/Fl4ProofsA64/P2/InvPmPdw.lean b/proofs/Fl4ProofsA64/P2/InvPmPdw.lean index 77c907ddb..4302572b0 100644 --- a/proofs/Fl4ProofsA64/P2/InvPmPdw.lean +++ b/proofs/Fl4ProofsA64/P2/InvPmPdw.lean @@ -40,6 +40,7 @@ half (`pmEx_wr`, `vsAg_userWr`, `pageEntry_userWr`, `pmRoot_wr`, `planMap_userWr facts; the AArch64 path keeps `huf` [J]) and `LkOr0` / `lookup_extra_*` / `pmEx_of_plan` (P3 `Ipc.exAt` helpers). -/ import Fl4ProofsA64.P2.InvPmU +import Fl4ProofsA64.P2.InvTrunc8C -- a64: x86_64 P2.InvPD import Fl4ProofsA64.P2.PteReachV -- a64: x86_64 P3.IpcGrant open Aeneas Aeneas.Std Result @@ -119,11 +120,13 @@ def PmVs (K : KLayoutP) (s : S) : Prop := variable (st) in /-- **The PageMap path's start fact with `PmEx` and `PmRoot`** (module docstring; a64: and [J] `PmVs`; gen 12: and the -[J] VCPU placements `VcKsA` / `VcTabA`, `P2/InvPmUb.lean`, in place of `GateEp`'s `K.vcpus = ∅`). -/ +[J] VCPU placements `VcKsA` / `VcTabA`, `P2/InvPmUb.lean`, in place of `GateEp`'s `K.vcpus = ∅`; P2-A64X gen 28: and +the entry's IPC-buffer placement `InvBufJ8`, last, so that `hbuf` at `PmPreG` is a lemma, `invBufJ8_pmPreG`). -/ def PmPreG (Cs Ω Sg Ac : Set U64) (Locks : S → U64 → Prop) (p : localinv.Plan) (K : KLayoutP) (m : U64 → U64) (s : S) : Prop := PmPreF st Cs Ω Sg Ac Locks p K m s ∧ PmEx st p s ∧ PmRoot st p s ∧ PmVs st K s ∧ -- a64: [J] `PmVs` - VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV -- a64 (gen 12): [J] the VCPU placements + VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV ∧ -- a64 (gen 12): [J] the VCPU placements + InvBufJ8 st K.toKLayout s -- a64 (P2-A64X gen 28): the entry's IPC-buffer placement -- a64: not ported pm_pdwG diff --git a/proofs/Fl4ProofsA64/P2/InvPmRunA.lean b/proofs/Fl4ProofsA64/P2/InvPmRunA.lean index f23ef3846..ab3eb10be 100644 --- a/proofs/Fl4ProofsA64/P2/InvPmRunA.lean +++ b/proofs/Fl4ProofsA64/P2/InvPmRunA.lean @@ -23,6 +23,8 @@ that writes no store word). the error named: no error is a lookup failure when the leaf is a page's (`check_vspace` and the walk answer `Ok` at the accepted run's state; AArch64 answers another shift with `FailedLookup`), `shift_leafA`; `dpm_runA` is now its corollary (statement unchanged). +* (P2-A64X gen 32) `DpmOutA3`, `dpmOut_ofA3`, **`dpm_runA3`**: the same run with the slot's new cap named a `Frame` + cap (what P1's in-place `set_cap` lemmas need); `dpm_runA2` is now its corollary (statement unchanged). No named premise. New module; no existing statement changes. -/ @@ -155,6 +157,25 @@ def DpmOutA2 (x : S) (a : A) (t slot : U64) (lu : vspace.PtSlot) (y : (core.resu obj.Slot.set_cap st slot s1 c = ok s2 ∧ vspace.install_page st ar s2 a lu e' r' as' va' f' at' false = ok (y.2.1, y.2.2)) +variable (st) in +/-- (P2-A64X gen 32) `DpmOutA2` with the slot's new cap a `Frame` cap (the decode writes +`Frame f.ptr f.size f.rights f.is_device true false asid vaddr`). -/ +def DpmOutA3 (x : S) (a : A) (t slot : U64) (lu : vspace.PtSlot) (y : (core.result.Result Unit error.Exc) × S × A) : + Prop := + (y.2.1 = x ∧ y.2.2 = a ∧ ∃ se, y.1 = .Err (.Sys se) ∧ (lu.shift = vspace.PAGE_BITS → PmNoLk se)) ∨ + (y.1 = .Ok () ∧ ∃ s1 s2 fp fz fr fd fm fe fa fv e' r' as' va' f' at', + sched.set_thread_state st x t sched.TS_RESTART = ok s1 ∧ + obj.Slot.set_cap st slot s1 (.Frame fp fz fr fd fm fe fa fv) = ok s2 ∧ + vspace.install_page st ar s2 a lu e' r' as' va' f' at' false = ok (y.2.1, y.2.2)) + +/-- (P2-A64X gen 32) The `Frame`-cap outcomes are named-error outcomes. -/ +theorem dpmOut_ofA3 {x : S} {a : A} {t slot : U64} {lu : vspace.PtSlot} + {y : (core.result.Result Unit error.Exc) × S × A} (h : DpmOutA3 st ar x a t slot lu y) : + DpmOutA2 st ar x a t slot lu y := by + rcases h with h | ⟨h0, s1, s2, fp, fz, fr, fd, fm, fe, fa, fv, h⟩ + · exact Or.inl h + · exact Or.inr ⟨h0, s1, s2, _, h⟩ + /-- (gen 8) The named-error outcomes are outcomes (x86_64 `dpmOut_of2`). -/ theorem dpmOut_ofA2 {x : S} {a : A} {t slot : U64} {lu : vspace.PtSlot} {y : (core.result.Result Unit error.Exc) × S × A} (h : DpmOutA2 st ar x a t slot lu y) : @@ -191,7 +212,7 @@ local macro "pmtailA" : tactic => `(tactic| ( refine WP.spec_bind (spec_selfA (P0.obj.Slot.set_cap.p0 st slot _ _ hS)) fun _ h2 => ?_ refine WP.spec_bind (spec_selfA (P0.vspace.install_page.p0 st ar _ a _ _ _ _ _ _ _ _ hS hA)) ?_ rintro ⟨s3, a3⟩ h3 - exact (WP.spec_ok _).2 (Or.inr ⟨rfl, _, _, _, _, _, _, _, _, _, h1, h2, h3⟩))) + exact (WP.spec_ok _).2 (Or.inr ⟨rfl, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, h1, h2, h3⟩))) set_option hygiene false in /-- After `check_vspace`: the shift, the alignment, the frame cap's ASID / address or the range, then `pmtailA`. -/ @@ -214,9 +235,10 @@ local macro "pmbodyA" : tactic => `(tactic| ( try simp only [hl, bind_ok] pmtailA)) -/-- (gen 8) **The outcomes of the AArch64 PageMap decode on `st`, no error a lookup failure** (x86_64 `dpm_run2`): at -the accepted run's state `check_vspace` and the walk answer `Ok` with a page leaf, so every error is a pure check's. -/ -theorem dpm_runA2 (hA : P0.ArchTotal ar) {x : S} {a : A} {length buf : U64} {slot : obj.Slot} +/-- (P2-A64X gen 32; gen 8's `dpm_runA2` proof) **The outcomes of the AArch64 PageMap decode on `st`, no error a +lookup failure, the slot's new cap a `Frame` cap** (x86_64 `dpm_run2`): at the accepted run's state `check_vspace` and +the walk answer `Ok` with a page leaf, so every error is a pure check's. -/ +theorem dpm_runA3 (hA : P0.ArchTotal ar) {x : S} {a : A} {length buf : U64} {slot : obj.Slot} {ex : Array obj.Slot 3#usize} {fp rt asid va : U64} {mp : Bool} {t x0 root rasid v0 v1 v2 : U64} {lu : vspace.PtSlot} (hc : sched.cur_thread st x = ok t) (hx0 : vspace.extra ex 0#usize = ok (some x0)) @@ -227,7 +249,7 @@ theorem dpm_runA2 (hA : P0.ArchTotal ar) {x : S} {a : A} {length buf : U64} {slo (hl : vspace.lookup_pt_slot st x root v0 = ok lu) : vspace.decode_page_map st ar x a length slot { ptr := fp, size := .Small, rights := rt, is_device := false, mapped := mp, is_ept := false, asid, - vaddr := va } buf ex ⦃ y => DpmOutA2 st ar x a t slot lu y ⦄ := by + vaddr := va } buf ex ⦃ y => DpmOutA3 st ar x a t slot lu y ⦄ := by have hS : P0.StoreTotal st := L.total unfold vspace.decode_page_map simp only [hc, hx0, bind_ok] @@ -241,6 +263,22 @@ theorem dpm_runA2 (hA : P0.ArchTotal ar) {x : S} {a : A} {length buf : U64} {slo · pmbodyA · pmbodyA +/-- (gen 8) **The outcomes of the AArch64 PageMap decode on `st`, no error a lookup failure** (x86_64 `dpm_run2`; +P2-A64X gen 32: `dpm_runA3` with the cap forgotten, statement unchanged). -/ +theorem dpm_runA2 (hA : P0.ArchTotal ar) {x : S} {a : A} {length buf : U64} {slot : obj.Slot} + {ex : Array obj.Slot 3#usize} {fp rt asid va : U64} {mp : Bool} {t x0 root rasid v0 v1 v2 : U64} + {lu : vspace.PtSlot} + (hc : sched.cur_thread st x = ok t) (hx0 : vspace.extra ex 0#usize = ok (some x0)) + (ha0 : msg.syscall_arg st x t buf 0#u64 = ok v0) (ha1 : msg.syscall_arg st x t buf 1#u64 = ok v1) + (ha2 : msg.syscall_arg st x t buf 2#u64 = ok v2) + (hk : obj.Slot.cap st x0 x = ok (.Pml4 root true rasid)) + (hcv : vspace.check_vspace st x (.Pml4 root true rasid) 1#u64 = ok (.Ok (root, rasid))) + (hl : vspace.lookup_pt_slot st x root v0 = ok lu) : + vspace.decode_page_map st ar x a length slot + { ptr := fp, size := .Small, rights := rt, is_device := false, mapped := mp, is_ept := false, asid, + vaddr := va } buf ex ⦃ y => DpmOutA2 st ar x a t slot lu y ⦄ := + WP.spec_mono (dpm_runA3 ar hA hc hx0 ha0 ha1 ha2 hk hcv hl) fun _ h => dpmOut_ofA3 ar h + /-- **The outcomes of the AArch64 PageMap decode on `st`** (module docstring). -/ theorem dpm_runA (hA : P0.ArchTotal ar) {x : S} {a : A} {length buf : U64} {slot : obj.Slot} {ex : Array obj.Slot 3#usize} {fp rt asid va : U64} {mp : Bool} {t x0 root rasid v0 v1 v2 : U64} diff --git a/proofs/Fl4ProofsA64/P2/InvPmWalkA.lean b/proofs/Fl4ProofsA64/P2/InvPmWalkA.lean index b0832c33b..c9b5642b8 100644 --- a/proofs/Fl4ProofsA64/P2/InvPmWalkA.lean +++ b/proofs/Fl4ProofsA64/P2/InvPmWalkA.lean @@ -259,7 +259,7 @@ theorem pm_leafA {s : S} (hE : PmPreG st Cs Ω Sg Ac Locks p K μ s) : vspace.lookup_pt_slot st s root (L.mem s (regA (L.curW s) layout.reg.X2)) = ok { slot := e, shift := vspace.PAGE_BITS } ∧ e ∈ K.ptes ∧ e ≠ 0#u64 ∧ Locks s e ∧ PmRS st K Locks s e ∧ PmWS st K Locks s e := by - obtain ⟨⟨-, hq, hG⟩, hEx, hRt, ⟨Pp, Tb, Lv, hV, h0T⟩, -, hvt⟩ := hE + obtain ⟨⟨-, hq, hG⟩, hEx, hRt, ⟨Pp, Tb, Lv, hV, h0T⟩, -, hvt, -⟩ := hE have Pb := hq.1.1.1 obtain ⟨⟨-, ⟨t, info, ex, ht, hi, hpw⟩, harm, hlk⟩, -⟩ := hq have et : t = L.curW s := by rw [cur_thread_ok] at ht; exact (ok_inj_ff ht).symm @@ -309,7 +309,7 @@ theorem pm_walkS2 (hA : P0.ArchTotal ar) {s : S} {a : A} {nx : U64} {s1 s2 : S} (∀ asid, vspace.find_vspace_for_asid st s2 asid = vspace.find_vspace_for_asid st s asid) ∧ ∀ asid root va, vspace.find_vspace_for_asid st s asid = ok (.Ok root) → vspace.lookup_pt_slot st s2 root va = vspace.lookup_pt_slot st s root va := by - obtain ⟨⟨-, hq, hG⟩, -, -, ⟨Pp, Tb, Lv, hV, -⟩, -, hvt⟩ := hE + obtain ⟨⟨-, hq, hG⟩, -, -, ⟨Pp, Tb, Lv, hV, -⟩, -, hvt, -⟩ := hE have Pb := hq.1.1.1 obtain ⟨-, es1, eu, -, -, -, -⟩ := C have w0 := prefix_writes ar hA Pb.tp Pb.np Pb.cT es1 eu @@ -333,7 +333,7 @@ theorem pm_decWalkV (hA : P0.ArchTotal ar) {s : S} {R : U64 → Prop} {gS : mach ∀ asid root va, vspace.find_vspace_for_asid st s asid = ok (.Ok root) → vspace.lookup_pt_slot gS s2 root va = vspace.lookup_pt_slot st s root va ∧ vspace.lookup_pt_slot st s2 root va = vspace.lookup_pt_slot st s root va := by - obtain ⟨⟨-, hq, hG⟩, -, -, ⟨Pp, Tb, Lv, hV, -⟩, -, hvt⟩ := hE + obtain ⟨⟨-, hq, hG⟩, -, -, ⟨Pp, Tb, Lv, hV, -⟩, -, hvt, -⟩ := hE have Pb := hq.1.1.1 obtain ⟨-, es1, eu, -, -, -, -⟩ := C have w0 := prefix_writes ar hA Pb.tp Pb.np Pb.cT es1 eu diff --git a/proofs/Fl4ProofsA64/P2/MdbBootP.lean b/proofs/Fl4ProofsA64/P2/MdbBootP.lean new file mode 100644 index 000000000..c85780747 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbBootP.lean @@ -0,0 +1,78 @@ +/- +# P2 (sync, gen 5): **`MdbS` of the history along an execution started at the boot witnesses** (DESIGN §9 item 4, +Half 1) + +`P2/MdbWirePDA.lean`'s `serializable_fl4_localVP_mdb` takes the initial `MdbSH st K0.toKLayout s0.mem` and the epoch +premise `EpochAP st K0 s0.mem` [J]. At A64-Boot's witnesses both are proved: + +* **`w0_serial_mdb`**: from `w0` (`bootS`, layout `bootKP`): `EpochAP` by `boot_epochAP`, `MdbSH` from Boot's + `boot_mdbST` (`MdbST.mdbS`) at `bootS` itself (`bootKP`'s slot set is `bootOS`). +* **`w1_serial_mdb`**: from `w1` (`w1S`, layout `w1KP`): `EpochAP` by `w1_epochAP`, `MdbSH` from `w1_mdbST`. + +Each concludes `serializable_fl4_localVP_mdb`'s `∃ H R, InvR s0.mem s H R ∧ MdbSH st K.toKLayout H ∧ (NoLoc s → H = +s.mem)` for any glue start `s0` whose memory is the witness's (as `P2/BootStartP.lean`'s `w0_serial` / `w1_serial`). + +Hypothesis `hSer` (Architect 16:45Z / 18:13Z: serial entries keep `MdbSH`, the serial-kernel obligation); otherwise no +premise: no [J], [P] or [W]. New module; no existing statement changes. +-/ +import Fl4ProofsA64.P2.BootStartP +import Fl4ProofsA64.P2.MdbWirePDA +import Fl4ProofsA64.P1.BootMdb1 +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.BootMdbP +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P1.W7.R.ST Fl4ProofsA64.P1.W7.R.ST.BootWit +open Fl4ProofsA64.P2.BootEpP (bootKP w1KP boot_epochAP w1_epochAP) +open Fl4ProofsA64.P2.E2EVA (EpochAP fl4LocPB MdbSH serializable_fl4_localVP_mdb) +open Fl4ProofsA64.P2.E2EPA (MapsPA ExecPA Fl4StepPA) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv NoLoc InvR) + +noncomputable section + +variable {A : Type} {ar : machine.Arch A} {hA : P0.ArchTotal ar} {hm : Fl4ProofsA64.P2.E2E.MasksWr bst} + +section w0 +variable {kb ib r now mt : U64} {nc : Nat} (P : BootLay kb ib nc) +include P + +/-- **`MdbS` of the history from `w0`** (module docstring). -/ +theorem w0_serial_mdb {s0 s : Cfg U64 U64 U64 U64} {K : Fl4ProofsA64.P2.OwnMapP.KLayoutP} (h0 : Init s0) + (hA0 : ArmInv s0) (hmem : s0.mem = (bootS kb ib nc r now mt).m) (hM0 : MapsPA (bootKP kb ib nc r now mt) s0) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : Fl4ProofsA64.P2.OwnMapP.KLayoutP} {c : U64}, + ExecPA (EpochAP bst) (fl4LocPB bst ar hA hm) s0 (bootKP kb ib nc r now mt) s K → Step s c s' → + Fl4StepPA (EpochAP bst) (fl4LocPB bst ar hA hm) s K c s' K' → Fl4ProofsA64.P2.E2E.SerStep s c → + MdbSH bst K.toKLayout s.mem → MdbSH bst K'.toKLayout s'.mem) + (h : ExecPA (EpochAP bst) (fl4LocPB bst ar hA hm) s0 (bootKP kb ib nc r now mt) s K) : + ∃ H R, InvR s0.mem s H R ∧ MdbSH bst K.toKLayout H ∧ (NoLoc s → H = s.mem) := + serializable_fl4_localVP_mdb bst ar hA hm h0 hA0 (hmem ▸ boot_epochAP P) hM0 + ⟨bootS kb ib nc r now mt, fun b _ _ => by rw [hmem]; rfl, (boot_mdbST (r := r) (now := now) (mt := mt) P).mdbS⟩ + hSer h + +end w0 + +section w1 +variable {kb ib r now mt sl : U64} {nc : Nat} {uw : U64 → U64} (P : BootLay1 kb ib nc) +include P + +/-- **`MdbS` of the history from `w1`** (module docstring). -/ +theorem w1_serial_mdb {s0 s : Cfg U64 U64 U64 U64} {K : Fl4ProofsA64.P2.OwnMapP.KLayoutP} (h0 : Init s0) + (hA0 : ArmInv s0) (hmem : s0.mem = (w1S kb ib nc r now mt sl uw).m) + (hM0 : MapsPA (w1KP kb ib nc r now mt sl uw) s0) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : Fl4ProofsA64.P2.OwnMapP.KLayoutP} {c : U64}, + ExecPA (EpochAP bst) (fl4LocPB bst ar hA hm) s0 (w1KP kb ib nc r now mt sl uw) s K → Step s c s' → + Fl4StepPA (EpochAP bst) (fl4LocPB bst ar hA hm) s K c s' K' → Fl4ProofsA64.P2.E2E.SerStep s c → + MdbSH bst K.toKLayout s.mem → MdbSH bst K'.toKLayout s'.mem) + (h : ExecPA (EpochAP bst) (fl4LocPB bst ar hA hm) s0 (w1KP kb ib nc r now mt sl uw) s K) : + ∃ H R, InvR s0.mem s H R ∧ MdbSH bst K.toKLayout H ∧ (NoLoc s → H = s.mem) := + serializable_fl4_localVP_mdb bst ar hA hm h0 hA0 (hmem ▸ w1_epochAP P) hM0 + ⟨w1S kb ib nc r now mt sl uw, fun b _ _ => by rw [hmem]; rfl, + (w1_mdbST (r := r) (now := now) (mt := mt) (sl := sl) (uw := uw) P).mdbS⟩ + hSer h + +end w1 + +end + +end Fl4ProofsA64.P2.BootMdbP diff --git a/proofs/Fl4ProofsA64/P2/MdbEditA.lean b/proofs/Fl4ProofsA64/P2/MdbEditA.lean new file mode 100644 index 000000000..ff88ca7e9 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbEditA.lean @@ -0,0 +1,299 @@ +/- +# P2 (sync, gen 3): **the slot edit forms** — what a slot's run does to the capability slots, stated against any +memory carrying P1's MDB field (DESIGN §9 item 4, Half 1 (c)) + +The slot lemmas of `P2/MdbKeepA.lean` (and `MdbKeepPmA` / `MdbKeepDelA`) keep P1's `MdbS` from a `Pre` state to the +end of a run. The wiring (`P2/MdbWireA.lean`) cannot use them as they stand: at a physical start other cores may be +half-way through their own edits of their own locked slots, so `MdbS` need not hold at the start state. What does +hold is `MdbS` of the **history** `H` (the memory of the serial run of the completed entries, `SerialR.InvR`), on +its slot words. This module restates, per slot, what the run does to the slot words in a form that applies to any +memory carrying `MdbS` and agreeing with the start on the words the run edits: + +* **`MdbSH st K H`**: some state of the store `st` has `H`'s slot words (`SlotWd`, aligned) and P1's `MdbS` at the + layout's slot set. +* **`KeptD K P`**: the decline words `DS` of `P` are no slot words (each slot's no-slot fact for its declined runs, + exported). +* **`SlotEdX st0 K P G`** (the edit form): for `Pre` states `σ` (the start) and `σ'` (`σ` up to user words), both in + `G`, and an accepted run `F σ' = ok r`, there is a set `D` of **edited words**, each held by the path at `σ` + (`HeldBy`), such that every other slot word is kept by the run, and every state `v` of `st0` with `MdbS` that agrees + with `σ'` on `D` has a successor `v'` with `MdbS`, `r`'s values on `D` and `v`'s elsewhere. +* **`SlotWireP st0 Q K P`**: `SlotEdX` from the entry states with the epoch data (`GateEpPA Q K`), and `KeptD`. + +Frame slots (the nine that write no capability slot): **`slotEdX_of_kept`** (`D = ∅`, from `KeptPG`). Per slot of +`fl4LocPB`: `*SlotP_kept` (`KeptP` / `KeptPE`, the term inside the existing `*SlotP_mdb`) and `*SlotP_ds` (`KeptD`: +the declined-run halves of `vm_kept`, `rr_kept`, … exported); `P2/MdbWireA.lean`'s `fl4LocPB_wireFam` assembles +them. Generic: `SlotCPG.keptD`, `SlotVqP.keptD`. `EpochA.slotNU`: slot words are no user words (`vxLay_Ub`). + +No premise: no [J], [P] or [W]. New module; no existing statement changes. +-/ +import Fl4ProofsA64.P2.MdbKeepDelA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA +open Fl4ProofsA64.P2.OwnMap (KLayout region fld own InW RegionOK) +open Fl4ProofsA64.P2.OwnMapV (KLayoutV ownV vmapV) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP UserOKP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA SlotCPG GateEpPA) +open Fl4Proofs.P2.Serial (Owner ClsOwner) + +noncomputable section +open Classical + +/-! ## The history's MDB field and the edit form -/ + +section gen +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} {ko : U64 → ClsOwner U64} + {User : U64 → Prop} + +/-- **P1's `MdbS` of a word memory** (module docstring): a state of `st0` with `H`'s aligned slot-region words has +`MdbS` at the layout's slot set. -/ +def MdbSH {S0 : Type} (st0 : machine.Store S0) [L0 : LawfulStore st0] (K : KLayout) (H : U64 → U64) : Prop := + ∃ v : S0, (∀ b, Aligned b → SlotWd K b → L0.mem v b = H b) ∧ W7.R.ST.MdbS st0 K.slots v + +/-- **The decline words of `P` are no slot words.** -/ +def KeptD (K : KLayout) (P : E2E.LocalPath own cls ko User) : Prop := + ∀ s x, P.Pre s → P.DS s x → ¬ SlotWd K x + +/-- **The edit form of a path** (module docstring). -/ +def SlotEdX {S0 : Type} (st0 : machine.Store S0) [L0 : LawfulStore st0] (K : KLayout) + (P : E2E.LocalPath own cls ko User) (G : P.S → Prop) : Prop := + ∀ (σ σ' : P.S) (a : P.A) (r : P.X × P.S × P.A), P.Pre σ → G σ → P.Pre σ' → G σ' → + (∀ x, Aligned x → ¬ User x → P.L.mem σ' x = P.L.mem σ x) → P.F P.ar P.st σ' a = ok r → P.Acc r.1 → + ∃ D : U64 → Prop, (∀ b, D b → HeldBy (P.L.cpu σ) (P.Locks σ) (own (P.L.mem σ) b)) ∧ + (∀ b, Aligned b → SlotWd K b → ¬ D b → P.L.mem r.2.1 b = P.L.mem σ' b) ∧ + ∀ v : S0, W7.R.ST.MdbS st0 K.slots v → + (∀ b, Aligned b → SlotWd K b → D b → L0.mem v b = P.L.mem σ' b) → + ∃ v' : S0, W7.R.ST.MdbS st0 K.slots v' ∧ ∀ b, Aligned b → SlotWd K b → + (D b → L0.mem v' b = P.L.mem r.2.1 b) ∧ (¬ D b → L0.mem v' b = L0.mem v b) + +/-- **What the wiring asks of a path** at a rule-9 layout `K` under the epoch predicate `Q`: its edit form from the +entry states with the epoch data, and its decline words off the slots. -/ +structure SlotWireP {S0 : Type} (st0 : machine.Store S0) [L0 : LawfulStore st0] + (Q : KLayoutP → (U64 → U64) → Prop) (K : KLayoutP) (P : E2E.LocalPath own cls ko User) : Prop where + ed : SlotEdX st0 K.toKLayout P fun s => @GateEpPA P.S P.st P.L Q K s + ds : KeptD K.toKLayout P + +/-- **A path keeping every slot word is in edit form** with no edited word. -/ +theorem slotEdX_of_kept {S0 : Type} (st0 : machine.Store S0) [L0 : LawfulStore st0] {K : KLayout} + {P : E2E.LocalPath own cls ko User} {G : P.S → Prop} (h : KeptPG K P G) : SlotEdX st0 K P G := + fun _ σ' a r _ _ hp' g' _ hr _ => + ⟨fun _ => False, fun _ h => h.elim, fun b hb hs _ => h σ' a r hp' g' hr b hb hs, + fun v hM _ => ⟨v, hM, fun _ _ _ => ⟨fun h => h.elim, fun _ => rfl⟩⟩⟩ + +theorem KeptP.toG {K : KLayout} {P : E2E.LocalPath own cls ko User} (h : KeptP K P) (G : P.S → Prop) : + KeptPG K P G := + fun s a r hp _ hr => h s a r hp hr + +/-- **A frame path for the wiring**: kept slot words from the entry states with the epoch data, declines off the +slots. -/ +theorem SlotWireP.ofKept {S0 : Type} (st0 : machine.Store S0) [L0 : LawfulStore st0] + {Q : KLayoutP → (U64 → U64) → Prop} {K : KLayoutP} {P : E2E.LocalPath own cls ko User} + (h : KeptPE Q K P) (hd : KeptD K.toKLayout P) : SlotWireP st0 Q K P := + ⟨slotEdX_of_kept st0 h, hd⟩ + +end gen + +/-- **A lifted route-C slot's declines are off the slots** where its path's are. -/ +theorem SlotCPG.keptD {ι : Type} (D : DfltA) (Y : SlotCPG ι) {i : ι} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} + (hX : ∀ h : Y.X.Prem i K.toKLayoutV m, KeptD K.toKLayout (Y.X.path i K.toKLayoutV m hK.v h)) : + KeptD K.toKLayout (Y.lift D i K m hK) := by + by_cases h : Y.X.Prem i K.toKLayoutV m + · rw [SlotCPG.lift_pos D Y h] + intro s x hp hx + exact hX h s x hp.1 hx + · unfold SlotCPG.lift + rw [dif_neg h] + intro s x hp + exact hp.elim + +/-- **A lifted `vq` slot's declines are off the slots** where its path's are. -/ +theorem SlotVqP.keptD {ν : Type} (D : DfltA) (Y : SlotVqP ν) {j : ν} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} + (hX : ∀ h : Y.Prem j K.toKLayoutV m, KeptD K.toKLayout (Y.path j K m hK h)) : + KeptD K.toKLayout (Y.lift D j K m hK) := by + unfold SlotVqP.lift + by_cases h : Y.Prem j K.toKLayoutV m + · rw [dif_pos h] + intro s x hp hx + exact hX h s x hp.1 hx + · rw [dif_neg h] + intro s x hp + exact hp.elim + +/-! ## Slot words at an epoch layout -/ + +section ep +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **A slot word is no user word** at an epoch layout (the epoch's `VxLay` at its user set, `vxLay_Ub`: user +words have region `.none`). -/ +theorem EpochA.slotNU {K : KLayoutV} {e : U64 → U64} (h : EpochA st K e) {b : U64} (hb : SlotWd K.toKLayout b) : + b ∉ K.toKLayout.user := by + obtain ⟨σ, H, Cs, OS, U, Ac, Pp, Tb, Vc, Ub, Lv, n, m, rfl, E, P, W, hcs, hn, -, -⟩ := h + have V := (S1b.vxLay_Ub E P W hcs hn m).base + obtain ⟨q, hq⟩ := hb + intro hu + have e := (V.user b hu).1 + rw [hq] at e + cases e + +theorem EpochAP.slotNU {K : KLayoutP} {e : U64 → U64} (h : EpochAP st K e) {b : U64} (hb : SlotWd K.toKLayout b) : + b ∉ K.user := + EpochA.slotNU h.1 hb + +end ep + +/-! ## The nine frame slots of `fl4LocPB` -/ + +section slots +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- The VM-fault slot keeps the slot words (`vm_kept`). -/ +theorem vmFSlotP_kept (i : VmFArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inl i) K m hK) := + SlotCPG.kept _ _ fun h => + have h' := h.toA hA hm + vm_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) h'.base.lay hm h'.base.mA h'.base.uf + +/-- The VM-fault slot's declines (the node's two timestamp words, owned by the core) are off the slots. -/ +theorem vmFSlotP_ds (i : VmFArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inl i) K m hK) := + SlotCPG.keptD _ _ fun _ s x hp hx => by + have P : VmPre st ar K.toKLayout i.T i.Cs i.Ω i.U i.H i.Sg i.Rp i.Scs i.Nt i.Z i.Ac i.Locks s := hp + rcases (hx : VmDS (st := st) s x) with rfl | rfl + · exact not_slot_of_core (P.own_node _ (by simp [layout.node.CUR_TIME]) (L.mem s)) + · exact not_slot_of_core (P.own_node _ (by simp [layout.node.CONSUMED]) (L.mem s)) + +/-- The VM ReplyRecv slot keeps the slot words (`rr_kept`). -/ +theorem rrSlotP_kept (i : RrArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inr (.inl i)) K m hK) := + SlotCPG.kept _ _ fun h => + have h' := h.toA + rr_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) hm h'.hmA h'.huf + +/-- The VM ReplyRecv slot's declines (`FAULT_IP`, the node's timestamps: `VmWd` words) are off the slots. -/ +theorem rrSlotP_ds (i : RrArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inl i)) K m hK) := + SlotCPG.keptD _ _ fun _ s x hp hx => by + have hE : VmRRPre st ar K.toKLayout i.T i.Cs i.Ω i.H i.Sg i.Rp i.Scs i.Nt i.Z i.Ac i.Locks i.rptr i.ep s := hp + have P := hE.1.base + rcases (hx : RrDS (st := st) s x) with rfl | rfl | rfl + · exact not_slot_of_vmWd (P.wdReg P.cT (by simp [layout.reg.FAULT_IP])) + · exact not_slot_of_vmWd (P.wdNode (by simp [layout.node.CUR_TIME])) + · exact not_slot_of_vmWd (P.wdNode (by simp [layout.node.CONSUMED])) + +/-- The `TCB_SetTimer` slot keeps the slot words (`st_kept`). -/ +theorem stSlotP_kept (i : StArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inl i))) K m hK) := + SlotCPG.kept _ _ fun h => + have h' := h.toA + st_kept i.n hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) hm h'.base.eu h'.base.lk h'.base.nu + +/-- The `TCB_SetTimer` slot's declines (`StDec`, owned by the core) are off the slots. -/ +theorem stSlotP_ds (i : StArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inl i))) K m hK) := + SlotCPG.keptD _ _ fun _ s x hp hx => by + have hE : StEntK K.toKLayout st i.T i.Cs i.Ω i.H i.Sg i.Rp i.Scs i.Nt i.Z i.Ac i.target m s := hp + exact not_slot_of_core (hE.ent.dec hx).2.1 + +/-- The timer-interrupt slot keeps the slot words (`irq_kept`). -/ +theorem irqSlotP_kept (i : IrqArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inl i)))) K m hK) := + SlotCPG.kept _ _ fun h => + have h' := h.toA + irq_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) hm h'.eu h'.lk h'.pl h'.ol h'.ub h'.sl h'.vc.rve hS + +/-- The timer-interrupt slot's declines (`IrqDec`, owned by the core) are off the slots. -/ +theorem irqSlotP_ds (i : IrqArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inl i)))) K m hK) := + SlotCPG.keptD _ _ fun _ s x hp hx => by + have hE : IrqEntM K.toKLayout st i.T i.Cs i.Ω i.H i.Sg i.Rp i.Scs i.Nt i.Z i.Ac i.irq m s := hp + exact not_slot_of_core (hE.ent.dec hx).2.1 + +/-- The syscall_local slot keeps the slot words (`sl_kept`). -/ +theorem slSlotP_kept (i : SlArgA S A) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inl i))))) K m hK) := + SlotCPG.kept _ _ fun h => + have h' := h.toA + sl_kept ar h'.hSt hA (vmapV.base _ hK.v) h'.hX (vmapV.user _ hK.v) hm h'.hSe h'.hUu i.a0 (fun _ e => e.1) + (fun _ e => e.2.1) (fun _ e => e.2.2.1) (fun _ e => e.2.2.2) h'.huf h'.vc.nuk hS + +/-- The syscall_local slot's declines (the prefix words, by region) are off the slots. -/ +theorem slSlotP_ds (i : SlArgA S A) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inl i))))) K m hK) := + SlotCPG.keptD _ _ fun _ s x hp hx => by + have hE : SlEntA st ar i K.toKLayout m s := hp + rcases hE.2.1.dsR x hx with ⟨c, e⟩ | e + · exact nsl_of_region e (fun _ e => by cases e) + · exact nsl_of_region e (fun _ e => by cases e) + +/-- The NBWait slot keeps the slot words (`nbw_kept`). -/ +theorem nbwSlotP_kept (i : NbwArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inl i)))))) K m hK) := + SlotCPG.kept _ _ fun h => + have h' := h.toA + nbw_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) h'.hLA i.number hm (fun _ e => e) h'.hmA h'.huf + +/-- The NBWait slot's declines (`FAULT_IP`, the node's timestamps, by region) are off the slots. -/ +theorem nbwSlotP_ds (i : NbwArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inl i)))))) K m hK) := + SlotCPG.keptD _ _ fun _ s x hp hx => by + have E : NbwPre st K.toKLayout i.T i.Cs i.Ω i.H i.Sg i.Rp i.Scs i.Nt i.Z i.Ac i.Locks i.n s := hp + have B := AvoidWA.ioBase E.io + rcases (hx : IoDS (st := st) s x) with rfl | rfl | rfl + · have hf : layout.reg.FAULT_IP.val < 384 := by simp [layout.reg.FAULT_IP] + obtain ⟨k, hk, e⟩ := regA_eq (B.tp _ E.io.cT) hf + rw [e] + exact nsl_of_region (AvoidWA.rb_reg B (off := k) E.io.cT (by omega) (by omega)) (fun _ e => by cases e) + · exact nsl_of_region (E.io.lay.node _ _ (B.inNode _ (by simp [layout.node.CUR_TIME]))) (fun _ e => by cases e) + · exact nsl_of_region (E.io.lay.node _ _ (B.inNode _ (by simp [layout.node.CONSUMED]))) (fun _ e => by cases e) + +/-- The `ARMVCPURun` exit slot keeps the slot words from its entry states with the epoch data (`vx_kept`). -/ +theorem vxSlotP_kept (j : VxArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + KeptPE (EpochAP st) K (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))) K m hK) := + SlotVqP.keptE (Y := vqSlotP st ar hA hm) (j := .inl j) _ _ fun h => vx_kept st ar hA hm hK hS h + +/-- The `ARMVCPURun` exit slot's declines (the node's timestamps, by region) are off the slots. -/ +theorem vxSlotP_ds (j : VxArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))) K m hK) := + SlotVqP.keptD (Y := vqSlotP st ar hA hm) (j := .inl j) _ fun _ s x hp hx => by + have P : VxPreR st K.toKLayoutV j.T j.Cs j.Ω j.H j.Sg j.Rp j.Scs j.Nt j.Z j.Ac j.Locks j.vc s := hp.1.toVxPreR + rcases (hx : VmDS (st := st) s x) with rfl | rfl + · exact nsl_of_region (P.toVBase.nodeReg _ (by simp [layout.node.CUR_TIME])) (fun _ e => by cases e) + · exact nsl_of_region (P.toVBase.nodeReg _ (by simp [layout.node.CONSUMED])) (fun _ e => by cases e) + +/-- The VM-timer slot keeps the slot words (`vtP_kept`). -/ +theorem vtSlotP_kept (j : VtArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptP K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j)))))))) K m hK) := + SlotVqP.kept (Y := vqSlotP st ar hA hm) (j := .inr (.inl j)) _ fun h => vtP_kept st ar hA hm hK.1 hK.2.base h + +/-- The VM-timer slot's declines (the node's timestamps, by region) are off the slots. -/ +theorem vtSlotP_ds (j : VtArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j)))))))) K m hK) := + SlotVqP.keptD (Y := vqSlotP st ar hA hm) (j := .inr (.inl j)) _ fun h s x hp hx => by + obtain ⟨h1, h2, h3⟩ := hp + have E := h1.toAP (fun _ _ h => h.1) hK.1 h.lv h2 h3 + rcases (hx : VmDS (st := st) s x) with rfl | rfl + · exact nsl_of_region (E.toVBase.nodeReg _ (by simp [layout.node.CUR_TIME])) (fun _ e => by cases e) + · exact nsl_of_region (E.toVBase.nodeReg _ (by simp [layout.node.CONSUMED])) (fun _ e => by cases e) + +/-- The `arm_vm_entry_ready` slot writes nothing (`vre_kept`). -/ +theorem vreSlotP_kept (j : VreArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptP K.toKLayout + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))))) K m hK) := + SlotVqP.kept (Y := vqSlotP st ar hA hm) (j := .inr (.inr j)) _ fun h => vre_kept ar hm hK.1.base hK.2.base h + +/-- The `arm_vm_entry_ready` slot declines writing nothing. -/ +theorem vreSlotP_ds (j : VreArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + KeptD K.toKLayout + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))))) K m hK) := + SlotVqP.keptD (Y := vqSlotP st ar hA hm) (j := .inr (.inr j)) _ fun _ _ _ _ hx => False.elim hx + +end slots + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/MdbKeepA.lean b/proofs/Fl4ProofsA64/P2/MdbKeepA.lean new file mode 100644 index 000000000..7bd712c9a --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbKeepA.lean @@ -0,0 +1,1198 @@ +/- +# P2 (AArch64 integration, P2-A64X gens 29–31): **the slots of `fl4LocPB` keep P1's MDB field `MdbS`** + +Architect 13:57Z, item 1, and 14:29Z (gen 30, item 1: P1's 5-tuple): per slot of `fl4LocPB` (`P2/E2EPTopA.lean`), a +run of the slot's path from an entry state keeps P1's `MdbS` (`P1/MdbLoop.lean`: +`∃ G, MdbLk ∧ RngOneA64 ∧ KindA ∧ NullLk ∧ ClsA`) at the layout's slot set `K.slots`. +`MdbOK` / `UtInvA` (`MdbST`) are not carried. + +* **`MdbKeepP OS P`**: a run of `P` from a `P.Pre` state with `MdbS OS` ends with `MdbS OS`. +* **The frame slots** (the nine slots that write no capability slot, gen 27's audit table): `KeptP K P`, every + aligned word of a slot region (`SlotWd K`) is kept by the run; then every slot's four words are kept + (**`osKeep_of_kept`**, P1's `OSKeep`) and `MdbKeepP` (**`mdbKeep_of_kept`**: P1's `MdbS.keep`, the ghost kept). + - Generic: **`interp_frameW`** (a `NoZero` tree whose writes lie in `W` keeps every aligned word off `W`), + **`guard_frame`**, **`LocalPath.keptOf`** (a path keeps the slot words if its accepted runs run on a store + guarded to non-slot writes (`NSl`), and its decline words `DS` are no slot words; declines from `PathObl.dec`); + `not_slot_of_core` (a word owned by a core is no slot word: `slotOwner` is a lock or the gate); + `nsl_of_region` (a word of a non-slot region); (gen 30) **`nsl_of_held`** (a word held under a lock map of + non-slot words, `lkNS`), **`nsl_of_ownV_core`**. + - Per path: the path's accepted run on a store `gS` answering non-slot writes as `st` (its `*_accG` lemma, which + takes any `GWr gS st W` with `W` covering its footprint), or its own accepted-run lemma at a narrowed lock map; + its `DS` words owned by the core or in a node / TCB region. +* `SlotLayK K` (a slot's 32 bytes are its region, aligned, not wrapping): `EpochA.slotLay` from the epoch + (`vmLayA_of_P1`); each per-slot lemma takes it as a hypothesis `hS` (the wiring gives it as `EpochAP.slotLay` of + the serial state's epoch data). +* Per slot: gen 29 **`vmFSlotP_mdb`** (`vm_kept`: `vm_accG`, `VmWd` / `STATE` writes), **`rrSlotP_mdb`** + (`rr_kept`: `rr_vm_accG`, `VmWd` and the two threads' `STATE` / `BLOCK_OBJ`), **`stSlotP_mdb`** (`st_kept`: + `set_timer_acc_own` at the empty lock map, so every write is owned by the core; `StEnt.dec`), **`vreSlotP_mdb`** + (`vre_kept`: no write at all); gen 30 **`nbwSlotP_mdb`** (`nbw_kept`: `nbw_accN` at `IoNar` / `NbwNar` with the + written words off the slot regions, `io_narNS` / `nbw_narNS`, now that `IoNar.curW` starts at AArch64's 224), + **`irqSlotP_mdb`** (`irq_kept`: `irq_acc_own` / `irq_acc_fire_nw` / `irq_acc_fire_wk2` at the lock map `lkNS`, + `PlanLk` there from `irqPlan_nsl`), **`vtSlotP_mdb`** (`vt_kept` / `vtP_kept`: `vt_acc` at the empty lock map). + Gen 31 (Architect 14:49Z, item 1): **`vxSlotP_mdb`** (`vx_kept`: `vxa_accR` at the lock map `lkNS`, `VxPreR` + there by `vxPreR_nsl`, the ASID table and pool words region `.none` from the epoch data `GateEpPA`, + `gateEpPA_tabNS`), stated over the entry states with the epoch data (**`MdbKeepPE`**, which every start supplies: + `StartAtEP`; `MdbKeepP.toE` for the other slots); item 2: **`slSlotP_mdb`** (`sl_kept`: `sl_pathObl_e`'s `acc` at + `lkNS`, the entry records there by `slPreB_nsl` / `slEntJ_nsl` / `slAccK_nsl`; a queued thread's block object is a + heap endpoint or notification, `sl_qnsl`). The other two slots (PageMap, Delete-with-PTE) are not covered here + (handoff `p2a64x.md`, gen 31). + +**The wiring along the execution log is deferred** (Architect, option (C), gen 33) to DESIGN §9 item 4 +(`par/p2-sync`): carrying `MdbS` from Boot's `MdbST.mdbS` along `RunAll` needs the concrete run of each completed +entry, but `RunAll` / `Serial.Run` keep an arbitrary run of the logged program `p0`: glue `rds` / `rdc` return any +value and `lock` either result, so a logged `Run` need not be a run of `P.F` from a `Pre` state. The 11 slot lemmas +here (and `MdbKeepPmA` / `MdbKeepDelA`) stand as stated; they are the per-entry step that wiring will cite. The +Delete start's `MdbS` facts stay [J] until then. + +No new premise: no [J], [P] or [W]. Gen 31 split `osKeep_of_words` out of `osKeep_of_kept` (statement unchanged). +Gen 30 changed `MdbKeepP` / `mdbKeep_of_kept` (and so the four gen-29 slot +lemmas' meaning) from `MdbLk ∧ NullLk` to `MdbS`; the slot lemmas' text is unchanged. +-/ +import Fl4ProofsA64.P2.E2EPStartA +import Fl4ProofsA64.P1.MdbLoop +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA +open Fl4ProofsA64.P2.OwnMap (KLayout region fld own InW RegionOK oSTATE) +open Fl4ProofsA64.P2.OwnMapV (KLayoutV ownV vmapV) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA SlotCPG) +open Fl4Proofs.P2.Serial (Owner ClsOwner) + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section +open Classical + +/-! ## Slot words and the frame of a guarded run -/ + +/-- An aligned word of a slot region. -/ +def SlotWd (K : KLayout) (b : U64) : Prop := ∃ q, region K b = .slot q + +/-- An aligned word of no slot region. -/ +def NSl (K : KLayout) (b : U64) : Prop := Aligned b ∧ ¬ SlotWd K b + +/-- **A word owned by a core is no slot word** (`slotOwner` is a lock or the gate). -/ +theorem not_slot_of_core {K : KLayout} {m : U64 → U64} {b c : U64} (h : own K m b = .core c) : ¬ SlotWd K b := by + rintro ⟨q, hq⟩ + simp only [own, hq, OwnMap.slotOwner] at h + split_ifs at h <;> cases h + +section frame +variable {S A X : Type} {st : machine.Store S} [L : LawfulStore st] {ar : machine.Arch A} + +/-- **The frame of a run within `W`**: a `NoZero` tree whose writes lie in the aligned set `W` keeps every aligned +word off `W`. -/ +theorem interp_frameW {R W : U64 → Prop} (hWa : ∀ b, W b → Aligned b) (p : SProg X) (hz : NoZero p) : + ∀ (s : S) (a : A) (r : X × S × A), interp st ar p s a = ok r → Within st ar R W p s a → + ∀ b, Aligned b → ¬ W b → L.mem r.2.1 b = L.mem s b := by + induction p with + | ret x => + intro s a r h _ b _ _ + simp only [interp] at h + obtain rfl := ok_inj_ff h + rfl + | err e => intro s a r h; exact absurd h fail_not_ok + | rd q k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨v, hv, hk⟩ := bok.mp h + exact ih v (hz v) s a r hk (hW.2 v hv) b hb hn + | rdp q k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨v, hv, hk⟩ := bok.mp h + exact ih v (hz v) s a r hk (hW.2.2 v hv) b hb hn + | wr q v k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨s', hv, hk⟩ := bok.mp h + have e : s' = L.wr s q v := (ok_inj_ff ((L.write_eq s q v).symm.trans hv)).symm + rw [ih hz s' a r hk (hW.2 s' hv) b hb hn, e] + exact mem_wr_ne' (hWa q hW.1) hb (fun e => hn (e ▸ hW.1)) + | zero q n k ih => intro _ _ _ _; exact False.elim hz + | clr q n k ih => intro _ _ _ _; exact False.elim hz + | ksb k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨v, hv, hk⟩ := bok.mp h + exact ih v (hz v) s a r hk (hW v hv) b hb hn + | cpu k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨v, hv, hk⟩ := bok.mp h + exact ih v (hz v) s a r hk (hW v hv) b hb hn + | masks k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨v, hv, hk⟩ := bok.mp h + exact ih v (hz v) s a r hk (hW v hv) b hb hn + | arch op k ih => + intro s a r h hW b hb hn + simp only [interp] at h + simp only [Within] at hW + obtain ⟨⟨c, a'⟩, hv, hk⟩ := bok.mp h + exact ih c (hz c) s a' r hk (hW c a' hv) b hb hn + +/-- **The frame of a guarded run** (`interp_guard`, `interp_frameW`). -/ +theorem guard_frame {R W : U64 → Prop} (hWa : ∀ b, W b → Aligned b) {p : SProg X} (hz : NoZero p) {s : S} + {a : A} {r : X × S × A} (h : interp (guard st R W) ar p s a = ok r) : + ∀ b, Aligned b → ¬ W b → L.mem r.2.1 b = L.mem s b := + let ⟨h1, h2⟩ := interp_guard p s a r h + interp_frameW hWa p hz s a r h1 h2 + +end frame + +/-! ## Paths that keep the slot words, and `MdbLk ∧ NullLk` -/ + +section gen +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} {ko : U64 → ClsOwner U64} + {User : U64 → Prop} + +/-- **`P` keeps the slot words of `K`**: a run from an entry state keeps every aligned slot-region word. -/ +def KeptP (K : KLayout) (P : E2E.LocalPath own cls ko User) : Prop := + ∀ s a r, P.Pre s → P.F P.ar P.st s a = ok r → ∀ b, Aligned b → SlotWd K b → P.L.mem r.2.1 b = P.L.mem s b + +/-- **`P` keeps P1's MDB field `MdbS` at the slot set `OS`** (module docstring): `MdbLk ∧ RngOneA64 ∧ KindA ∧ +NullLk ∧ ClsA` over some ghost, `P1/MdbLoop.lean`. -/ +def MdbKeepP (OS : Set U64) (P : E2E.LocalPath own cls ko User) : Prop := + ∀ s a r, P.Pre s → P.F P.ar P.st s a = ok r → + @W7.R.ST.MdbS P.S P.st P.L OS s → @W7.R.ST.MdbS P.S P.st P.L OS r.2.1 + +/-- **A path keeps the slot words** (module docstring): its accepted runs run on a store guarded to non-slot writes, +and its decline words are no slot words. -/ +theorem LocalPath.keptOf (P : E2E.LocalPath own cls ko User) (K : KLayout) (hz : NoZero P.p) + (hA : ∀ s a r, P.Pre s → P.F P.ar P.st s a = ok r → P.Acc r.1 → + ∃ R W : U64 → Prop, (∀ b, W b → NSl K b) ∧ P.F P.ar (guard P.st R W) s a = ok r) + (hD : ∀ s x, P.Pre s → P.DS s x → ¬ SlotWd K x) : KeptP K P := by + letI := P.L + intro s a r hp h b hb hsl + by_cases hac : P.Acc r.1 + · obtain ⟨R, W, hW, hg⟩ := hA s a r hp h hac + rw [P.isProg _ (Narrow.guard _ _ _) s (AltOf.pre hp) a] at hg + exact guard_frame (fun b hb => (hW b hb).1) hz hg b hb (fun hw => (hW b hw).2 hsl) + · have hg := P.obl.dec hp h hac + rw [P.isProg _ (Narrow.guard _ _ _) s (AltOf.pre hp) a] at hg + exact guard_frame (fun b hb => P.obl.aligned s b hp (Or.inr hb)) hz hg b hb (fun hd => hD s b hp hd hsl) + +/-- **A path whose write sets hold no slot word keeps the slot words** (`LocalPath.keptOf` at its own `acc`). -/ +theorem LocalPath.keptOfWS (P : E2E.LocalPath own cls ko User) (K : KLayout) (hz : NoZero P.p) + (hW : ∀ s x, P.Pre s → P.WS s x → ¬ SlotWd K x) (hD : ∀ s x, P.Pre s → P.DS s x → ¬ SlotWd K x) : + KeptP K P := by + letI := P.L + exact LocalPath.keptOf P K hz (fun s a r hp h hac => ⟨P.RS s, P.WS s, + fun b hb => ⟨P.obl.aligned s b hp (Or.inl hb), hW s b hp hb⟩, P.obl.acc s a r hp h hac⟩) hD + +/-- **A layout's slots are slot regions** of 32 aligned, non-wrapping bytes. -/ +structure SlotLayK (K : KLayout) : Prop where + s8 : ∀ p ∈ K.slots, p.val % 8 = 0 + sf : ∀ p ∈ K.slots, p.val + 32 ≤ 2 ^ 64 + sw : ∀ p ∈ K.slots, ∀ b, InW p 32 b → region K b = .slot p + +/-- A slot's word at an aligned offset below 32 is an aligned slot-region word. -/ +theorem SlotLayK.word {K : KLayout} (h : SlotLayK K) {p : U64} (hp : p ∈ K.slots) {k : U64} (hk : k.val < 32) + (h8 : k.val % 8 = 0) : Aligned (wa p k) ∧ SlotWd K (wa p k) := by + have s8 := h.s8 p hp + have sf := h.sf p hp + have hv : (wa p k).val = p.val + k.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + exact ⟨by unfold Aligned; omega, p, h.sw p hp _ ⟨by omega, by omega⟩⟩ + +/-- A slot is an aligned slot-region word. -/ +theorem SlotLayK.base {K : KLayout} (h : SlotLayK K) {p : U64} (hp : p ∈ K.slots) : Aligned p ∧ SlotWd K p := + ⟨h.s8 p hp, p, h.sw p hp _ ⟨le_refl _, by have := h.sf p hp; omega⟩⟩ + +/-- **Two states agreeing on the aligned slot-region words agree on every slot's four words** (P1's `OSKeep`, +`SlotLayK`; gen 31: split out of `osKeep_of_kept`). -/ +theorem osKeep_of_words {S : Type} {st : machine.Store S} [L : LawfulStore st] {K : KLayout} (hS : SlotLayK K) + {s r : S} (k : ∀ b, Aligned b → SlotWd K b → L.mem r b = L.mem s b) : W7.R.ST.OSKeep st K.slots s r := by + have kw : ∀ x ∈ K.slots, ∀ k' : U64, k'.val < 32 → k'.val % 8 = 0 → + L.mem r (wa x k') = L.mem s (wa x k') := fun x hx k' h1 h2 => + let ⟨ha, hw⟩ := hS.word hx h1 h2 + k _ ha hw + intro x hx + exact ⟨let ⟨ha, hw⟩ := hS.base hx; k _ ha hw, kw x hx 8#u64 (by simp) (by simp), + kw x hx 16#u64 (by simp) (by simp), kw x hx 24#u64 (by simp) (by simp)⟩ + +/-- **A path keeping the slot words keeps every slot's four words** (P1's `OSKeep`, `SlotLayK`). -/ +theorem osKeep_of_kept {K : KLayout} (hS : SlotLayK K) {P : E2E.LocalPath own cls ko User} (h : KeptP K P) + {s : P.S} {a : P.A} {r : P.X × P.S × P.A} (hp : P.Pre s) (hr : P.F P.ar P.st s a = ok r) : + @W7.R.ST.OSKeep P.S P.st P.L K.slots s r.2.1 := + @osKeep_of_words P.S P.st P.L K hS s r.2.1 (h s a r hp hr) + +/-- **`MdbS` from the kept slot words** (P1's `MdbS.keep`, the ghost kept). -/ +theorem mdbKeep_of_kept {K : KLayout} (hS : SlotLayK K) {P : E2E.LocalPath own cls ko User} (h : KeptP K P) : + MdbKeepP K.slots P := by + letI := P.L + exact fun _ _ _ hp hr hM => W7.R.ST.MdbS.keep hM (osKeep_of_kept hS h hp hr) + +end gen + +/-! ## The slot layout from the epoch -/ + +section ep +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **`SlotLayK` at an epoch layout** (`vmLayA_of_P1`'s `slot8` / `slotf` / `slotw` at the epoch's typing state). -/ +theorem EpochA.slotLay {K : KLayoutV} {e : U64 → U64} (h : EpochA st K e) : SlotLayK K.toKLayout := by + obtain ⟨σ, H, Cs, OS, U, Ac, Pp, Tb, Vc, Ub, Lv, n, m, rfl, E, P, -, hcs, hn, -, -⟩ := h + have V := S1b.vmLayA_of_P1 E P hcs hn + exact ⟨V.slot8, V.slotf, V.slotw⟩ + +theorem EpochAP.slotLay {K : KLayoutP} {e : U64 → U64} (h : EpochAP st K e) : SlotLayK K.toKLayout := + EpochA.slotLay h.1 + +end ep + +/-! ## The route-C slots: a cut-and-lifted slot keeps what its path keeps -/ + +/-- **A lifted route-C slot keeps the slot words** where its path does (`cutLiftPG` keeps the body and narrows the +entry states; the empty path has none). -/ +theorem SlotCPG.kept {ι : Type} (D : DfltA) (Y : SlotCPG ι) {i : ι} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} + (hX : ∀ h : Y.X.Prem i K.toKLayoutV m, KeptP K.toKLayout (Y.X.path i K.toKLayoutV m hK.v h)) : + KeptP K.toKLayout (Y.lift D i K m hK) := by + by_cases h : Y.X.Prem i K.toKLayoutV m + · rw [SlotCPG.lift_pos D Y h] + intro s a r hp hr + exact hX h s a r hp.1 hr + · unfold SlotCPG.lift + rw [dif_neg h] + intro s a r hp + exact hp.elim + +/-! ## Non-slot words -/ + +/-- A `VmWd` word (node, object, TCB or SC region) is no slot word. -/ +theorem not_slot_of_vmWd {K : KLayout} {b : U64} (h : VmWd K b) : ¬ SlotWd K b := by + rintro ⟨q, hq⟩ + rcases h with ⟨c, hc⟩ | ⟨p, hp⟩ | ⟨t, ht, -⟩ | ⟨p, hp, -⟩ + · rw [hq] at hc; cases hc + · rw [hq] at hp; cases hp + · rw [hq] at ht; cases ht + · rw [hq] at hp; cases hp + +/-- **A TCB's `STATE` and `BLOCK_OBJ` words are non-slot words** (`RegionOK.tcb`). -/ +theorem tcbW_nsl {K : KLayout} (hK : RegionOK K) {d : U64} (hd : K.heap d = some .tcb) (ha : TcbAddr d) : + NSl K (stA d) ∧ NSl K (blockA d) := by + have e1 := (hK.tcb d hd oSTATE (Or.inr (Or.inl rfl))).1 + have e2 := (hK.tcb d hd OwnMap.oBLOCK_OBJ (Or.inr (Or.inr (Or.inl rfl)))).1 + rw [← OwnMap.stA_fld] at e1 + rw [← OwnMap.blockA_fld] at e2 + refine ⟨⟨?_, fun ⟨q, hq⟩ => by rw [hq] at e1; cases e1⟩, ⟨?_, fun ⟨q, hq⟩ => by rw [hq] at e2; cases e2⟩⟩ + · rw [stA_eq]; exact field_aligned ha (by simp) (by simp) + · exact field_aligned ha (by simp) (by simp) + +/-- **A lifted `vq` slot keeps the slot words** where its path does (`cutLiftP` of `restrictC` keeps the body and +narrows the entry states; the empty path has none). -/ +theorem SlotVqP.kept {ν : Type} (D : DfltA) (Y : SlotVqP ν) {j : ν} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} + (hX : ∀ h : Y.Prem j K.toKLayoutV m, KeptP K.toKLayout (Y.path j K m hK h)) : + KeptP K.toKLayout (Y.lift D j K m hK) := by + unfold SlotVqP.lift + by_cases h : Y.Prem j K.toKLayoutV m + · rw [dif_pos h] + intro s a r hp hr + exact hX h s a r hp.1 hr + · rw [dif_neg h] + intro s a r hp + exact hp.elim + +/-! ## The VM-fault slot -/ + +section vm +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T Cs Ω U Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} +variable {Locks : S → U64 → Prop} {m0 : U64 → U64} +variable {A : Type} {ar : machine.Arch A} + +/-- **The VM-fault path keeps the slot words**: an accepted run on a store answering the non-slot writes as `st` +(`vm_accG`: its writes are `VmWd` words, of node / object / TCB / SC regions, and `STATE` words); a declined one +writes the node's two timestamp words (owned by the core). -/ +theorem vm_kept (hA : P0.ArchTotal ar) (hK : RegionOK K) (hU : E2E.UserOK K) (hLay : VmLayA K) {addr code : U64} + (hm : E2E.MasksWr st) + (hmA : ∀ s, VmPre st ar K T Cs Ω U H Sg Rp Scs Nt Z Ac Locks s → + ∀ d, K.heap d = some .tcb → m0 (fld d OwnMap.oAFFINITY) = L.mem s (fld d OwnMap.oAFFINITY)) + (huf : ∀ s b v, VmPre st ar K T Cs Ω U H Sg Rp Scs Nt Z Ac Locks s → b ∈ K.user → Aligned b → + VmPre st ar K T Cs Ω U H Sg Rp Scs Nt Z Ac Locks (L.wr s b v) ∧ Locks (L.wr s b v) = Locks s ∧ + VmRS (st := st) K Locks (L.wr s b v) = VmRS (st := st) K Locks s ∧ + VmWS (st := st) K Locks (L.wr s b v) = VmWS (st := st) K Locks s ∧ + VmDS (st := st) (L.wr s b v) = VmDS (st := st) s) : + KeptP K (vm_localPath (T := T) (Cs := Cs) (Ω := Ω) (U := U) (H := H) (Sg := Sg) (Rp := Rp) (Scs := Scs) + (Nt := Nt) (Z := Z) (Ac := Ac) (Locks := Locks) (m0 := m0) ar hA hK hU hLay (addr := addr) (code := code) + hm hmA huf) := by + refine LocalPath.keptOf _ K (vm_noZero addr code) ?_ ?_ + · intro s a r hp h hr + change syscall.handle_vm_fault_local st ar s a addr code = ok r at h + have P : VmPre st ar K T Cs Ω U H Sg Rp Scs Nt Z Ac Locks s := hp + refine ⟨VmRS (st := st) K Locks s, NSl K, fun b hb => hb, ?_⟩ + change syscall.handle_vm_fault_local (guard st (VmRS (st := st) K Locks s) (NSl K)) ar s a addr code = ok r + refine vm_accG hA P (fun b hwd hws => ⟨hws.1, not_slot_of_vmWd hwd⟩) (fun d v hd _ _ _ => ?_) h hr + exact SWr.of_W (tcbW_nsl hK (P.hT d hd) (hLay.tcbA d (P.hT d hd))).1 + · intro s x hp hx + have P : VmPre st ar K T Cs Ω U H Sg Rp Scs Nt Z Ac Locks s := hp + rcases (hx : VmDS (st := st) s x) with rfl | rfl + · exact not_slot_of_core (P.own_node _ (by simp [layout.node.CUR_TIME]) (L.mem s)) + · exact not_slot_of_core (P.own_node _ (by simp [layout.node.CONSUMED]) (L.mem s)) + +end vm + +/-- A word held with no lock is owned by the core. -/ +theorem heldBy_bot {c : U64} {o : Owner U64 U64} (h : HeldBy c (fun _ => False) o) : o = .core c := by + cases o with + | core d => exact congrArg Owner.core h + | lock l => exact h.elim + | gate => exact h.elim + +/-! ## The `TCB_SetTimer` slot -/ + +section st +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} +variable {target : U64} {m : U64 → U64} {lk : S → U64 → Prop} +variable {A : Type} {ar : machine.Arch A} + +/-- **The `TCB_SetTimer` path keeps the slot words**: an accepted run runs on the store guarded to `MaxW` with no lock +held (`set_timer_acc_own` at the empty lock map: every write is owned by the core), a declined one writes `StDec` +(owned by the core, `StEnt.dec`). -/ +theorem st_kept (n : I64) (hA : P0.ArchTotal ar) (hK : RegionOK K) (hUO : E2E.UserOK K) (hm : E2E.MasksWr st) + (hEU : ∀ s b v, StEnt K st T Cs Ω H Sg Rp Scs Nt Z Ac target s → b ∈ K.user → Aligned b → + StEnt K st T Cs Ω H Sg Rp Scs Nt Z Ac target (L.wr s b v)) + (hlk : ∀ s b v, b ∈ K.user → lk (L.wr s b v) = lk s) + (hNU : ∀ s, StEnt K st T Cs Ω H Sg Rp Scs Nt Z Ac target s → ∀ o : U64, o.val < 4288 → + L.nodeAt s (L.cpu s) o ∉ K.user) : + KeptP K (set_timer_localPathG2 (T := T) (Cs := Cs) (Ω := Ω) (H := H) (Sg := Sg) (Rp := Rp) (Scs := Scs) + (Nt := Nt) (Z := Z) (Ac := Ac) (target := target) (m := m) (lk := lk) ar n hA hK hUO hm hEU hlk hNU) := by + refine LocalPath.keptOf _ K (st_noZero n target) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp h hr + change b = true at hr + subst hr + change localtimer.handle_set_timer_local st ar s a n target = ok (true, s', a') at h + have hE : StEntK K st T Cs Ω H Sg Rp Scs Nt Z Ac target m s := hp + refine ⟨MaxR K st (fun _ _ => False) s, MaxW K st (fun _ _ => False) s, + fun b hb => ⟨hb.2, not_slot_of_core (heldBy_bot hb.1)⟩, ?_⟩ + change localtimer.handle_set_timer_local (guard st (MaxR K st (fun _ _ => False) s) + (MaxW K st (fun _ _ => False) s)) ar s a n target = ok (true, s', a') + exact set_timer_acc_own ar hA hE.ent.inv hE.ent.tg hE.ent.lay hE.ent.aff h + · intro s x hp hx + have hE : StEntK K st T Cs Ω H Sg Rp Scs Nt Z Ac target m s := hp + exact not_slot_of_core (hE.ent.dec hx).2.1 + +end st + +/-! ## The VM ReplyRecv slot -/ + +section rr +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} +variable {Locks : S → U64 → Prop} {m0 : U64 → U64} +variable {A : Type} {ar : machine.Arch A} + +/-- The ReplyRecv `STATE` / `BLOCK_OBJ` writes pass the store guarded to non-slot writes. -/ +theorem rrSW_nsl (hK : RegionOK K) {R : U64 → Prop} {t ep u : U64} (ht : K.heap t = some .tcb) (hta : TcbAddr t) + (hu : K.heap u = some .tcb) (hua : TcbAddr u) : RrSW (st := st) (guard st R (NSl K)) t ep u := + have a := tcbW_nsl hK ht hta + have b := tcbW_nsl hK hu hua + ⟨SWr.of_W a.1, SWr.of_W a.1, fun _ _ => guard_write a.1, fun _ => guard_write a.2, SWr.of_W b.1, SWr.of_W b.1, + SWr.of_W b.1⟩ + +/-- **The VM ReplyRecv path keeps the slot words**: an accepted run on the store guarded to non-slot writes +(`rr_vm_accG`: `VmWd` words and the two threads' `STATE` / `BLOCK_OBJ`), a declined one writes the caller's +`FAULT_IP` and the node's timestamp words (`VmWd`). -/ +theorem rr_kept (hA : P0.ArchTotal ar) (hK : RegionOK K) (hU : E2E.UserOK K) {rptr ep : U64} + (hm : E2E.MasksWr st) + (hmA : ∀ s, VmRRPre st ar K T Cs Ω H Sg Rp Scs Nt Z Ac Locks rptr ep s → + ∀ d, K.heap d = some .tcb → m0 (fld d OwnMap.oAFFINITY) = L.mem s (fld d OwnMap.oAFFINITY)) + (huf : ∀ s b v, VmRRPre st ar K T Cs Ω H Sg Rp Scs Nt Z Ac Locks rptr ep s → b ∈ K.user → Aligned b → + VmRRPre st ar K T Cs Ω H Sg Rp Scs Nt Z Ac Locks rptr ep (L.wr s b v) ∧ Locks (L.wr s b v) = Locks s ∧ + VmRS (st := st) K Locks (L.wr s b v) = VmRS (st := st) K Locks s ∧ + VmWS (st := st) K Locks (L.wr s b v) = VmWS (st := st) K Locks s ∧ + RrDS (st := st) (L.wr s b v) = RrDS (st := st) s) : + KeptP K (rr_localPathG2 (T := T) (Cs := Cs) (Ω := Ω) (Sg := Sg) (Rp := Rp) (Scs := Scs) (Nt := Nt) (Ac := Ac) + (H := H) (Z := Z) (Locks := Locks) (m0 := m0) (rptr := rptr) (ep := ep) ar hA hK hU hm hmA huf) := by + refine LocalPath.keptOf _ K (rr_noZero rptr ep) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp h hr + change b = true at hr + subst hr + change syscall.handle_reply_recv_vm_local st ar s a (rptr, ep) = ok (true, s', a') at h + have hE : VmRRPre st ar K T Cs Ω H Sg Rp Scs Nt Z Ac Locks rptr ep s := hp + have P := hE.1.base + refine ⟨VmRS (st := st) K Locks s, NSl K, fun b hb => hb, ?_⟩ + change syscall.handle_reply_recv_vm_local (guard st (VmRS (st := st) K Locks s) (NSl K)) ar s a (rptr, ep) = + ok (true, s', a') + exact rr_vm_accG hA hE.1 hE.2.2.2 (fun b hwd hws => ⟨hws.1, not_slot_of_vmWd hwd⟩) + (fun u hu _ _ => rrSW_nsl hK (P.hT _ P.cT) P.tA (P.hT u hu) (P.lay.tcbA u (P.hT u hu))) h + · intro s x hp hx + have hE : VmRRPre st ar K T Cs Ω H Sg Rp Scs Nt Z Ac Locks rptr ep s := hp + have P := hE.1.base + rcases (hx : RrDS (st := st) s x) with rfl | rfl | rfl + · exact not_slot_of_vmWd (P.wdReg P.cT (by simp [layout.reg.FAULT_IP])) + · exact not_slot_of_vmWd (P.wdNode (by simp [layout.node.CUR_TIME])) + · exact not_slot_of_vmWd (P.wdNode (by simp [layout.node.CONSUMED])) + +end rr + +/-! ## The `arm_vm_entry_ready` slot -/ + +section vre +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hm : E2E.MasksWr st) + +/-- **`arm_vm_entry_ready` keeps the slot words**: it writes nothing (`WS = DS = ∅`). -/ +theorem vre_kept {j : VreArgA S} {K : KLayoutV} {m : U64 → U64} (hK : OwnMapV.RegionOKV K) + (hU : OwnMapV.UserOKV K) (h : VrePremVB st j K m) : KeptP K.toKLayout (vre_localPathX ar hm hK hU h) := + LocalPath.keptOfWS _ _ (vre_noZero j.n) (fun _ _ _ h => False.elim h) (fun _ _ _ h => False.elim h) + +end vre + +/-! ## The NBWait slot (gen 30) -/ + +/-- A word of a region that is no slot region is no slot word. -/ +theorem nsl_of_region {K : KLayout} {b : U64} {r : OwnMap.Region} (h : region K b = r) + (hr : ∀ q, r ≠ .slot q) : ¬ SlotWd K b := + fun ⟨q, hq⟩ => hr q (h.symm.trans hq) + +/-- A word of a notification's 64 bytes is no slot word. -/ +theorem nsl_ntfnW {K : KLayout} {p : U64} (hreg : ∀ b, InW p 64 b → region K b = .ntfn p) + (hfit : p.val + 64 ≤ 2 ^ 64) {o : U64} (ho : o.val < 64) : ¬ SlotWd K (wa p o) := by + have hv : (wa p o).val = p.val + o.val := by + simp [core.num.U64.wrapping_add_val_eq, U64.size_def, U64.numBits]; omega + exact nsl_of_region (hreg _ ⟨by rw [hv]; omega, by rw [hv]; omega⟩) (fun _ e => by cases e) + +section nbw +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} +variable {Locks : S → U64 → Prop} {m0 : U64 → U64} {n : U64} {s : S} +variable {A : Type} {ar : machine.Arch A} + +/-- **`IoNar` with every written word off the slot regions** (nothing asked of the read words): the core's node, +the caller's TCB words from offset 224 (`IoNar.curW` at AArch64's 224, gen 30), the current SC's ring and +`CONSUMED`, all by region. -/ +theorem io_narNS (P : IoPre st K T Cs Ω H Sg Rp Scs Nt Z Ac s) : + IoNar (L := L) K Z (fun _ => True) (fun b => ¬ SlotWd K b) s := by + have B := AvoidWA.ioBase P + obtain ⟨hP, hz⟩ := B.bxi.2.2.1.1.sc _ P.cscS + obtain ⟨-, h128, -⟩ := sc_lo hP hz + have hv := sc_word_val hP hz (o := layout.sc.CONSUMED) (by simp [layout.sc.CONSUMED]) + have hcv : layout.sc.CONSUMED.val = 8 := by simp [layout.sc.CONSUMED] + exact + { nd := fun o h1 _ => ⟨trivial, nsl_of_region (P.lay.node _ _ (B.inNode o h1)) (fun _ e => by cases e)⟩ + cur := fun _ _ _ => trivial + curW := fun off h1 h2 _ => nsl_of_region (AvoidWA.rb_reg B (off := off) P.cT h1 h2) (fun _ e => by cases e) + slot := fun _ _ => ⟨trivial, trivial⟩ + ks := fun _ _ _ => trivial + csc := fun _ => ⟨fun _ _ _ => trivial, + fun b _ hb => nsl_of_region (AvoidWA.rb_scReg B P.cscS hb.inSC) (fun _ e => by cases e), + nsl_of_region (AvoidWA.rb_scReg B P.cscS ⟨by rw [hv]; omega, by rw [hv, hcv]; omega⟩) + (fun _ e => by cases e)⟩ + td := fun _ => trivial + rs := fun _ => ⟨trivial, fun _ _ _ => trivial⟩ } + +/-- **`NbwNar` with the written word off the slot regions**: the notification's `TAIL_STATE`. -/ +theorem nbw_narNS (P : NbwPre st K T Cs Ω H Sg Rp Scs Nt Z Ac Locks n s) : + NbwNar (L := L) (fun _ => True) (fun b => ¬ SlotWd K b) n s := + ⟨trivial, fun _ => ⟨trivial, trivial, nsl_ntfnW P.nreg P.nfit.2 (by simp [layout.ntfn.TAIL_STATE]), trivial⟩⟩ + +/-- **The NBWait path keeps the slot words** (gen 30): an accepted run runs on the store guarded to `IoWS` off the +slot regions (`nbw_accN` at `io_narNS` / `nbw_narNS`); a declined one writes the caller's `FAULT_IP` (a TCB word +from offset 224) and the node's two timestamp words. -/ +theorem nbw_kept (hA : P0.ArchTotal ar) (hK : RegionOK K) (hU : E2E.UserOK K) (hLA : IoLayA K) (number : I64) + (hm : E2E.MasksWr st) {P : S → Prop} (hPE : ∀ s, P s → NbwPre st K T Cs Ω H Sg Rp Scs Nt Z Ac Locks n s) + (hmA : ∀ s, P s → ∀ d, K.heap d = some .tcb → + m0 (fld d OwnMap.oAFFINITY) = L.mem s (fld d OwnMap.oAFFINITY)) + (huf : ∀ s b v, P s → b ∈ K.user → Aligned b → + P (L.wr s b v) ∧ Locks (L.wr s b v) = Locks s ∧ + IoRS (st := st) K Locks (L.wr s b v) = IoRS (st := st) K Locks s ∧ + IoWS (st := st) K Locks (L.wr s b v) = IoWS (st := st) K Locks s ∧ + IoDS (st := st) (L.wr s b v) = IoDS (st := st) s) : + KeptP K (nbw_localPathG ar hA hK hU hLA number n hm hPE hmA huf) := by + refine LocalPath.keptOf _ K (nbw_noZero number n) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp h hr + change b = true at hr + subst hr + change syscall.handle_nbwait_local st ar s a number n = ok (true, s', a') at h + have E := hPE s hp + refine ⟨fun b => IoRS (st := st) K Locks s b ∧ True, fun b => IoWS (st := st) K Locks s b ∧ ¬ SlotWd K b, + fun b hb => ⟨hb.1.1, hb.2⟩, ?_⟩ + change syscall.handle_nbwait_local (guard st (fun b => IoRS (st := st) K Locks s b ∧ True) + (fun b => IoWS (st := st) K Locks s b ∧ ¬ SlotWd K b)) ar s a number n = ok (true, s', a') + exact nbw_accN hA E (io_narNS E.io) (nbw_narNS E) h + · intro s x hp hx + have E := hPE s hp + have B := AvoidWA.ioBase E.io + rcases (hx : IoDS (st := st) s x) with rfl | rfl | rfl + · have hf : layout.reg.FAULT_IP.val < 384 := by simp [layout.reg.FAULT_IP] + obtain ⟨k, hk, e⟩ := regA_eq (B.tp _ E.io.cT) hf + rw [e] + exact nsl_of_region (AvoidWA.rb_reg B (off := k) E.io.cT (by omega) (by omega)) (fun _ e => by cases e) + · exact nsl_of_region (E.io.lay.node _ _ (B.inNode _ (by simp [layout.node.CUR_TIME]))) (fun _ e => by cases e) + · exact nsl_of_region (E.io.lay.node _ _ (B.inNode _ (by simp [layout.node.CONSUMED]))) (fun _ e => by cases e) + +end nbw + +/-! ## The timer interrupt and VM-timer slots (gen 30) + +No new premise (Architect 14:29Z, item 2): the words these runs write are placed off the slot regions by region +facts the paths already carry. Rather than re-running the fire-case walks on a guard narrowed to `W ∧ ¬ SlotWd` +(`irq_acc_fire_wk2` alone is a 16M-heartbeat proof), the existing accepted-run lemmas are instantiated at a +**narrowed lock map** whose every word is a non-slot word: + +* a slot word is owned by its slot's lock (the slot itself) or the gate (`OwnMap.slotOwner`), never by a core; +* timer IRQ: `MaxW` at `lkNS K lk` (the held locks whose word is no slot word). Every write lemma (`irq_acc_own`, + `irq_acc_fire_nw`) holds for any lock map; `irq_acc_fire_wk2` needs only `PlanLk`, which holds at `lkNS` since + the plan's notification is the earliest armed thread's bound one, in `Nt` (`IrqLay.ntw`: region `.ntfn`), and + its endpoint / reply are P1 objects (`plan_pe_pr`, `ObjLay`: region `.obj`) — **`irqPlan_nsl`**; +* VM timer: `vt_acc` holds for any lock map, so at the empty one `VxWS` is user words (region `.none`, + `VmLayA.user`) or core-owned words (`ownV`: a VCPU word has region `.none`, `RegionOKV.none`; else `own`). -/ + +/-- **A word held under a lock map of non-slot words is no slot word** (`slotOwner` is the slot's lock or the +gate). -/ +theorem nsl_of_held {K : KLayout} (hS : SlotLayK K) {c : U64} {lk : U64 → Prop} + (hlk : ∀ l, lk l → ¬ SlotWd K l) {m : U64 → U64} {b : U64} (h : HeldBy c lk (own K m b)) : + ¬ SlotWd K b := by + rintro ⟨q, hq⟩ + have hq' := (OwnMap.region_slot_inv hq).1 + simp only [own, hq, OwnMap.slotOwner] at h + split_ifs at h <;> first | exact hlk q h (hS.base hq').2 | exact h + +/-- The held locks whose word is no slot word. -/ +def lkNS {S : Type} (K : KLayout) (lk : S → U64 → Prop) (s : S) (l : U64) : Prop := lk s l ∧ ¬ SlotWd K l + +section irq +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {K : KLayout} +variable {irq : U64} {m : U64 → U64} {pl : localtimer.TimerIrqPlan} {lk : S → U64 → Prop} +variable {A : Type} {ar : machine.Arch A} + +/-- **The plan objects of an accepted fire-case run are no slot words** (module section docstring): the +notification is in `Nt` (`IrqLay.ntw`), the endpoint and reply are P1 objects (`plan_pe_pr`, `ObjLay`). -/ +theorem irqPlan_nsl (hA : P0.ArchTotal ar) {s s' : S} {a a' : A} (hE : IrqEntK K st T Cs Ω H Sg Rp Scs Nt Z Ac m s) + (hY : IrqLay K st Nt irq s) (hOL : ObjLay K H Rp) (hq : IrqFire st ar s a) + (h : localtimer.handle_timer_irq_local st ar s a irq pl = ok (true, s', a')) : + (pl.ntfn ≠ 0#u64 → ¬ SlotWd K pl.ntfn) ∧ (pl.ep ≠ 0#u64 → ¬ SlotWd K pl.ep) ∧ + (pl.reply ≠ 0#u64 → ¬ SlotWd K pl.reply) := by + have hI := hE.inv + have h0 := hI.1.choose_spec.choose_spec + have hX := hI.2.2.1 + obtain ⟨mx, now, i2, tip, x, s1, s2, s3, a1, a2, a3, emx, hle, ei1, etip, eb, eok, eq, eu, enow, erel, ei2, eb4, + edue, efo, ecb, ehi⟩ := fire_eqs ar hA hq h + obtain ⟨hPE, hPR⟩ := plan_pe_pr hI etip eb + obtain ⟨hTM0, hpN⟩ := plan_ntfn etip eb efo + have hTMs := tmin_facts (BXI.blk hI) h0.cur hTM0 + refine ⟨fun hn => ?_, fun he => ?_, fun hr => ?_⟩ + · have hB0 : L.mem s (boundA (L.mem s (L.nodeAt s (L.cpu s) layout.node.TIMER_MIN))) ≠ 0#u64 := by + rw [← hpN]; exact hn + have r := hY.ntw _ (hX.2.2.bnd _ hTMs.1 hB0) 0#u64 (by simp) + rw [SFF.wadd0', ← hpN] at r + exact nsl_of_region r (fun _ e => by cases e) + · have r := hOL.1 _ (hPE he).1 0#u64 (by simp) + rw [SFF.wadd0'] at r + exact nsl_of_region r (fun _ e => by cases e) + · have r := hOL.2 _ (hPR hr).1 0#u64 (by simp) + rw [SFF.wadd0'] at r + exact nsl_of_region r (fun _ e => by cases e) + +/-- **The timer interrupt path keeps the slot words** (module section docstring): an accepted run runs on the store +guarded to `MaxW` at `lkNS K lk` (`irq_acc_own`, the fire case by `irq_acc_fire_nw` / `irq_acc_fire_wk2` with +`PlanLk` at `lkNS` from `irqPlan_nsl`); a declined one writes `IrqDec` (owned by the core). -/ +theorem irq_kept (hA : P0.ArchTotal ar) (hK : RegionOK K) (hUO : E2E.UserOK K) (hm : E2E.MasksWr st) + (hEU : ∀ s b v, IrqEnt K st T Cs Ω H Sg Rp Scs Nt Z Ac irq s → b ∈ K.user → Aligned b → + IrqEnt K st T Cs Ω H Sg Rp Scs Nt Z Ac irq (L.wr s b v)) + (hlk : ∀ s b v, b ∈ K.user → lk (L.wr s b v) = lk s) + (hPL : PlanLk lk pl) (hOL : ObjLay K H Rp) + (hUB : ∀ s, IrqEntM K st T Cs Ω H Sg Rp Scs Nt Z Ac irq m s → + ∃ U : Set U64, UserOK st T Cs Ω H U s ∧ + ∀ y ∈ T, msg.lookup_ipc_buffer st s true y ⦃ b => BufOK U b ⦄) + (hSL : ∀ s, IrqEntM K st T Cs Ω H Sg Rp Scs Nt Z Ac irq m s → SlotLay K st T s) + (hRVe : ∀ s, IrqEntM K st T Cs Ω H Sg Rp Scs Nt Z Ac irq m s → RunVcpuInv st T s) (hS : SlotLayK K) : + KeptP K (irq_localPathG4 ar hA hK hUO hm hEU hlk hPL hOL hUB hSL hRVe) := by + refine LocalPath.keptOf _ K (irq_noZero irq pl) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp h hr + change b = true at hr + subst hr + change localtimer.handle_timer_irq_local st ar s a irq pl = ok (true, s', a') at h + have hE : IrqEntM K st T Cs Ω H Sg Rp Scs Nt Z Ac irq m s := hp + refine ⟨MaxR K st (lkNS K lk) s, MaxW K st (lkNS K lk) s, + fun b hb => ⟨hb.2, nsl_of_held hS (fun _ h => h.2) hb.1⟩, ?_⟩ + change localtimer.handle_timer_irq_local (guard st (MaxR K st (lkNS K lk) s) (MaxW K st (lkNS K lk) s)) ar s a + irq pl = ok (true, s', a') + refine irq_acc_own ar hA hK hE.k hE.ent.ly hE.ent.aff (fun hf => ?_) h + by_cases hnw : NoWake st s + · exact irq_acc_fire_nw ar hA hK hE.k hE.ent.ly hE.ent.aff hE.ent.nth hf hnw h + · obtain ⟨n1, e1, r1⟩ := irqPlan_nsl hA hE.k hE.ent.ly hOL hf h + have hPL' : PlanLk (lkNS K lk) pl := fun x => + ⟨fun q => ⟨(hPL x).1 q, n1 q⟩, fun q => ⟨(hPL x).2.1 q, e1 q⟩, fun q => ⟨(hPL x).2.2 q, r1 q⟩⟩ + obtain ⟨U, hU, hb⟩ := hUB s hE + exact irq_acc_fire_wk2 ar hA hK hE.k hE.ent.ly hE.ent.aff hE.ent.nth hOL hPL' + (irq_bufAt_run ar hA hE.ent.inv hU hb hf h) (hSL s hE).v (hSL s hE).h + (irq_rvi_run ar hA hE.ent.inv (hRVe s hE) hf h) (hRVe s hE) hf hnw h + · intro s x hp hx + have hE : IrqEntM K st T Cs Ω H Sg Rp Scs Nt Z Ac irq m s := hp + exact not_slot_of_core (hE.ent.dec hx).2.1 + +end irq + +/-- **A word `ownV` gives a core is no slot word** (a VCPU word has region `.none`; otherwise `own`). -/ +theorem nsl_of_ownV_core {K : KLayoutV} (hK : OwnMapV.RegionOKV K) {m : U64 → U64} {b c : U64} + (h : ownV K m b = .core c) : ¬ SlotWd K.toKLayout b := by + rintro ⟨q, hq⟩ + unfold ownV at h + split_ifs at h with hv + · have e := hK.none _ _ (Classical.choose_spec hv) + rw [hq] at e + cases e + · have h' : own K.toKLayout m b = .core c := by + simp only [OwnMapV.ownT, hq] at h + exact h + exact not_slot_of_core h' ⟨q, hq⟩ + +section vt +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {K : KLayoutV} {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} + {Locks : S → U64 → Prop} +variable {A : Type} (ar : machine.Arch A) + +/-- **The VM-timer path keeps the slot words** (module section docstring): an accepted run runs on the store +guarded to `VxWS` at the empty lock map (`vt_acc`: user words, region `.none`, or core-owned words); a declined one +writes the node's two timestamp words. -/ +theorem vt_kept (hmk : E2E.MasksWr st) (hA : P0.ArchTotal ar) (hK : OwnMapV.RegionOKV K) + (hU : OwnMapV.UserOKV K) (hLay : VxLay K) (hNt : ∀ q ∈ Nt, H q = some .ntfn) (m0 : U64 → U64) (irq : U64) + (pl : localtimer.TimerIrqPlan) (n : U64) + (hmA : ∀ s, VtEntA st K T Cs Ω H Sg Rp Scs Nt Z Ac irq s → + ∀ d, K.heap d = some .tcb → m0 (fld d OwnMap.oAFFINITY) = L.mem s (fld d OwnMap.oAFFINITY)) + (hEU : ∀ s b v, VtEntA st K T Cs Ω H Sg Rp Scs Nt Z Ac irq s → b ∈ K.user → Aligned b → + VtEntA st K T Cs Ω H Sg Rp Scs Nt Z Ac irq (L.wr s b v)) + (hlk : ∀ s b v, b ∈ K.user → Aligned b → Locks (L.wr s b v) = Locks s) : + KeptP K.toKLayout (vt_localPathG (Locks := Locks) ar hmk hA hK hU hLay hNt m0 irq pl n hmA hEU hlk) := by + refine LocalPath.keptOf _ K.toKLayout (vti_noZero irq pl n) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp h hr + change b = true at hr + subst hr + change localvm.handle_vm_timer_irq_local st ar s a irq pl n = ok (true, s', a') at h + have E : VtEntA st K T Cs Ω H Sg Rp Scs Nt Z Ac irq s := hp + refine ⟨VxRS (st := st) K (fun _ _ => False) s, VxWS (st := st) K (fun _ _ => False) s, + fun b hb => ⟨hb.1, ?_⟩, ?_⟩ + · rcases hb.2 with hu | hw + · exact nsl_of_region (E.toVBase.lay.base.user b hu).1 (fun _ e => by cases e) + · exact nsl_of_ownV_core hK (heldBy_bot hw) + · change localvm.handle_vm_timer_irq_local (guard st (VxRS (st := st) K (fun _ _ => False) s) + (VxWS (st := st) K (fun _ _ => False) s)) ar s a irq pl n = ok (true, s', a') + exact vt_acc (Locks := fun _ _ => False) ar hA hK hNt E h + · intro s x hp hx + have E : VtEntA st K T Cs Ω H Sg Rp Scs Nt Z Ac irq s := hp + rcases (hx : VmDS (st := st) s x) with rfl | rfl + · exact nsl_of_region (E.toVBase.nodeReg _ (by simp [layout.node.CUR_TIME])) (fun _ e => by cases e) + · exact nsl_of_region (E.toVBase.nodeReg _ (by simp [layout.node.CONSUMED])) (fun _ e => by cases e) + +end vt + +/-! ## The slots of `fl4LocPB` -/ + +section slots +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The VM-fault slot of `fl4LocPB` keeps `MdbS`** (`vm_kept`; a frame slot). -/ +theorem vmFSlotP_mdb (i : VmFArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots (fl4LocPB st ar hA hm (.inl i) K m hK) := + mdbKeep_of_kept hS <| SlotCPG.kept _ _ fun h => + have h' := h.toA hA hm + vm_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) h'.base.lay hm h'.base.mA h'.base.uf + +/-- **The VM ReplyRecv slot of `fl4LocPB` keeps `MdbS`** (`rr_kept`; a frame slot). -/ +theorem rrSlotP_mdb (i : RrArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots (fl4LocPB st ar hA hm (.inr (.inl i)) K m hK) := + mdbKeep_of_kept hS <| SlotCPG.kept _ _ fun h => + have h' := h.toA + rr_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) hm h'.hmA h'.huf + +/-- **The `TCB_SetTimer` slot of `fl4LocPB` keeps `MdbS`** (`st_kept`; a frame slot). -/ +theorem stSlotP_mdb (i : StArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots (fl4LocPB st ar hA hm (.inr (.inr (.inl i))) K m hK) := + mdbKeep_of_kept hS <| SlotCPG.kept _ _ fun h => + have h' := h.toA + st_kept i.n hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) hm h'.base.eu h'.base.lk h'.base.nu + +/-- **The `arm_vm_entry_ready` slot of `fl4LocPB` keeps `MdbS`** (`vre_kept`: it writes nothing). -/ +theorem vreSlotP_mdb (j : VreArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))))) K m hK) := + mdbKeep_of_kept hS <| SlotVqP.kept (Y := vqSlotP st ar hA hm) (j := .inr (.inr j)) _ fun h => + vre_kept ar hm hK.1.base hK.2.base h + +/-- **The NBWait slot of `fl4LocPB` keeps `MdbS`** (`nbw_kept`; a frame slot, gen 30). -/ +theorem nbwSlotP_mdb (i : NbwArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inl i)))))) K m hK) := + mdbKeep_of_kept hS <| SlotCPG.kept _ _ fun h => + have h' := h.toA + nbw_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) h'.hLA i.number hm (fun _ e => e) h'.hmA h'.huf + +/-- **The VM-timer path at a PTE layout keeps the slot words** (`vt_kept`; `vt_localPathP` narrows the entry states +of `vt_localPathG` and keeps its body). -/ +theorem vtP_kept {j : VtArgA S} {Kp : KLayoutP} {m : U64 → U64} (hKp : OwnMapP.RegionOKP Kp) + (hU : OwnMapV.UserOKV Kp.toKLayoutV) (h : VtPremVB st j Kp.toKLayoutV m) : + KeptP Kp.toKLayout (vt_localPathP ar hm hA (EpochAP st) (fun _ _ h => h.1) hKp hU h) := by + intro s a r hp hr + obtain ⟨h1, h2, h3⟩ := hp + have F := h.lv.facts + exact vt_kept (T := j.x.T) (Cs := j.x.Cs) (Ω := j.x.Ω) (H := j.x.H) (Sg := j.x.Sg) (Rp := j.x.Rp) + (Scs := j.x.Scs) (Nt := j.x.Nt) (Z := j.x.Z) (Ac := j.x.Ac) (Locks := j.x.Locks) + ar hm hA hKp.base hU F.1 F.2.2.1 m j.irq j.pl j.n h.hmA (fun _ _ v E hb hba => h.lv.wrA E hb hba v) + (fun s _ v hb hba => (S1b.kd_of_kernelReads_Ub F.2.1 h.rd s).wr hb hba v) s a r + (h1.toAP (fun _ _ h => h.1) hKp h.lv h2 h3) hr + +/-- **The timer-interrupt slot of `fl4LocPB` keeps `MdbS`** (`irq_kept`, gen 30). -/ +theorem irqSlotP_mdb (i : IrqArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inl i)))) K m hK) := + mdbKeep_of_kept hS <| SlotCPG.kept _ _ fun h => + have h' := h.toA + irq_kept hA (vmapV.base _ hK.v) (vmapV.user _ hK.v) hm h'.eu h'.lk h'.pl h'.ol h'.ub h'.sl h'.vc.rve hS + +/-- **The VM-timer slot of `fl4LocPB` keeps `MdbS`** (`vt_kept`, gen 30). -/ +theorem vtSlotP_mdb (j : VtArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl j)))))))) K m hK) := + mdbKeep_of_kept hS <| SlotVqP.kept (Y := vqSlotP st ar hA hm) (j := .inr (.inl j)) _ fun h => + vtP_kept st ar hA hm hK.1 hK.2.base h + +end slots + +/-! ## Entry states with the epoch data (gen 31) + +A start of `fl4LocPB` is a `StartAtEP (EpochAP st)` (`Fl4StepPA.start`): its state is a path entry state as soon as +it carries the epoch data `GateEpPA (EpochAP st) K`, which the execution discharges at every start. The +`ARMVCPURun` exit's entry facts place the ASID pools only through the lock map (`VxPreR.tab`); their region comes +from the epoch data, so its slot lemma is stated over the entry states with `GateEpPA` (**`MdbKeepPE`**). Every +`MdbKeepP` gives `MdbKeepPE` (`MdbKeepP.toE`). -/ + +section genG +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} {ko : U64 → ClsOwner U64} + {User : U64 → Prop} + +/-- **`P` keeps the slot words of `K` from its entry states satisfying `G`.** -/ +def KeptPG (K : KLayout) (P : E2E.LocalPath own cls ko User) (G : P.S → Prop) : Prop := + ∀ s a r, P.Pre s → G s → P.F P.ar P.st s a = ok r → ∀ b, Aligned b → SlotWd K b → P.L.mem r.2.1 b = P.L.mem s b + +/-- **`P` keeps `MdbS` at `OS` from its entry states satisfying `G`.** -/ +def MdbKeepPG (OS : Set U64) (P : E2E.LocalPath own cls ko User) (G : P.S → Prop) : Prop := + ∀ s a r, P.Pre s → G s → P.F P.ar P.st s a = ok r → + @W7.R.ST.MdbS P.S P.st P.L OS s → @W7.R.ST.MdbS P.S P.st P.L OS r.2.1 + +/-- `KeptPG` at the entry states with the epoch data `GateEpPA Q K`. -/ +def KeptPE (Q : KLayoutP → (U64 → U64) → Prop) (K : KLayoutP) (P : E2E.LocalPath own cls ko User) : Prop := + KeptPG K.toKLayout P fun s => @E2EPA.GateEpPA P.S P.st P.L Q K s + +/-- **`MdbS` at `K`'s slots from the entry states with the epoch data `GateEpPA Q K`** (module section docstring). -/ +def MdbKeepPE (Q : KLayoutP → (U64 → U64) → Prop) (K : KLayoutP) (P : E2E.LocalPath own cls ko User) : Prop := + MdbKeepPG K.toKLayout.slots P fun s => @E2EPA.GateEpPA P.S P.st P.L Q K s + +theorem MdbKeepP.toE {Q : KLayoutP → (U64 → U64) → Prop} {K : KLayoutP} {P : E2E.LocalPath own cls ko User} + (h : MdbKeepP K.toKLayout.slots P) : MdbKeepPE Q K P := + fun s a r hp _ hr => h s a r hp hr + +/-- **`LocalPath.keptOf` from the entry states satisfying `G`.** -/ +theorem LocalPath.keptOfG (P : E2E.LocalPath own cls ko User) (K : KLayout) (G : P.S → Prop) (hz : NoZero P.p) + (hA : ∀ s a r, P.Pre s → G s → P.F P.ar P.st s a = ok r → P.Acc r.1 → + ∃ R W : U64 → Prop, (∀ b, W b → NSl K b) ∧ P.F P.ar (guard P.st R W) s a = ok r) + (hD : ∀ s x, P.Pre s → G s → P.DS s x → ¬ SlotWd K x) : KeptPG K P G := by + letI := P.L + intro s a r hp hg h b hb hsl + by_cases hac : P.Acc r.1 + · obtain ⟨R, W, hW, hgd⟩ := hA s a r hp hg h hac + rw [P.isProg _ (Narrow.guard _ _ _) s (AltOf.pre hp) a] at hgd + exact guard_frame (fun b hb => (hW b hb).1) hz hgd b hb (fun hw => (hW b hw).2 hsl) + · have hgd := P.obl.dec hp h hac + rw [P.isProg _ (Narrow.guard _ _ _) s (AltOf.pre hp) a] at hgd + exact guard_frame (fun b hb => P.obl.aligned s b hp (Or.inr hb)) hz hgd b hb (fun hd => hD s b hp hg hd hsl) + +/-- **`MdbS` from the kept slot words, from the entry states satisfying `G`** (P1's `MdbS.keep`). -/ +theorem mdbKeepG_of_kept {K : KLayout} (hS : SlotLayK K) {P : E2E.LocalPath own cls ko User} {G : P.S → Prop} + (h : KeptPG K P G) : MdbKeepPG K.slots P G := by + letI := P.L + exact fun s a r hp hg hr hM => W7.R.ST.MdbS.keep hM (@osKeep_of_words P.S P.st P.L K hS s r.2.1 (h s a r hp hg hr)) + +end genG + +/-- **A lifted `vq` slot keeps the slot words from the entry states with the epoch data** where its path does. -/ +theorem SlotVqP.keptE {ν : Type} (D : DfltA) (Y : SlotVqP ν) {j : ν} {K : KLayoutP} {m : U64 → U64} {hK : ROKP K} + (Q : KLayoutP → (U64 → U64) → Prop) (hX : ∀ h : Y.Prem j K.toKLayoutV m, KeptPE Q K (Y.path j K m hK h)) : + KeptPE Q K (Y.lift D j K m hK) := by + unfold SlotVqP.lift + by_cases h : Y.Prem j K.toKLayoutV m + · rw [dif_pos h] + intro s a r hp hg hr + exact hX h s a r hp.1 hg hr + · rw [dif_neg h] + intro s a r hp + exact hp.elim + +/-! ## The `ARMVCPURun` exit slot (gen 31) + +Architect 14:49Z, item 1: the smaller lock map again. `vxa_accR` holds at any lock map whose `VxPreR` holds, so it is +run at `lkNS K Locks` (the held locks whose word is no slot word). `VxPreR` at `lkNS` (**`vxPreR_nsl`**): the VCPU +lock word `vc` is a VCPU word (region `.none`, `RegionOKV.none`); the ASID table and pool words `TabRd` reads are +region `.none` (**`TabNS`**), so a lock owning one is a VCPU (`rperm_nsl`). `TabNS` at an entry state with the epoch +data (**`gateEpPA_tabNS`**): the table words by `EpochA.tabW`; the pool words through the table words' agreement with +the epoch memory (`GateEpPA`, as `tabNoEA_carry`) and S1b's `asidPool_of_P1` at the epoch's typing state +(**`EpochA.poolW`**). Every written word is then a user word (region `.none`) or held under `lkNS` (no slot word, +**`nsl_of_heldV`**); a declined run writes the node's two timestamp words. -/ + +section vx +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- A VCPU word of the layout is no slot word (region `.none`). -/ +theorem vc_nsl {K : KLayoutV} (hK : OwnMapV.RegionOKV K) {v : U64} (hv : v ∈ K.vcpus) : ¬ SlotWd K.toKLayout v := by + rintro ⟨q, hq⟩ + have e := hK.none v v ⟨hv, le_refl _, by omega⟩ + rw [hq] at e + cases e + +/-- **A read permission of a region-`.none` word survives dropping the slot locks**: its owner is the gate or a VCPU's +lock. -/ +theorem rperm_nsl {K : KLayoutV} (hK : OwnMapV.RegionOKV K) {c : U64} {lk : U64 → Prop} {m : U64 → U64} {x : U64} + (hx : region K.toKLayout x = .none) (h : RPerm (ownV K) c lk m x) : + RPerm (ownV K) c (fun l => lk l ∧ ¬ SlotWd K.toKLayout l) m x := by + have key : ∀ l, ownV K m x = .lock l → ¬ SlotWd K.toKLayout l := by + intro l e + unfold ownV at e + split_ifs at e with hv + · unfold OwnMapV.vcpuOwner at e + split_ifs at e + cases e + exact vc_nsl hK (Classical.choose_spec hv).1 + · simp only [OwnMapV.ownT, hx, OwnMap.own] at e + cases e + rcases h with h | h + · left + generalize ownV K m x = o at h key ⊢ + cases o with + | core d => exact h + | lock l => exact ⟨h, key l rfl⟩ + | gate => exact h.elim + · exact Or.inr h + +/-- **A word held under a lock map of non-slot words is no slot word**, for `ownV` (`nsl_of_held`; a VCPU word has +region `.none`). -/ +theorem nsl_of_heldV {K : KLayoutV} (hK : OwnMapV.RegionOKV K) (hS : SlotLayK K.toKLayout) {c : U64} + {lk : U64 → Prop} (hlk : ∀ l, lk l → ¬ SlotWd K.toKLayout l) {m : U64 → U64} {b : U64} + (h : HeldBy c lk (ownV K m b)) : ¬ SlotWd K.toKLayout b := by + rintro ⟨q, hq⟩ + unfold ownV at h + split_ifs at h with hv + · have e := hK.none _ _ (Classical.choose_spec hv) + rw [hq] at e + cases e + · have h' : HeldBy c lk (own K.toKLayout m b) := by + simp only [OwnMapV.ownT, hq] at h + exact h + exact nsl_of_held hS hlk h' ⟨q, hq⟩ + +variable (st) in +/-- **The ASID table and the pools it names are region-`.none` words** at `s`. -/ +def TabNS (K : KLayout) (s : S) : Prop := + ∀ hi : U64, hi.val < 128 → region K (atAF (L.ksb s) hi) = .none ∧ + (L.mem s (atAF (L.ksb s) hi) ≠ 0#u64 → ∀ lo : U64, lo.val < 512 → + region K (peAF (L.mem s (atAF (L.ksb s) hi)) lo) = .none) + +/-- **The pools an epoch's table names are region-`.none` words** (S1b's `asidPool_of_P1` at the epoch's typing +state, which agrees with the epoch memory on the table words). -/ +theorem EpochA.poolW {K : KLayoutV} {e : U64 → U64} (h : EpochA st K e) {hi : U64} (hhi : hi.val < 128) + (h0 : e (atAF K.ksb hi) ≠ 0#u64) {lo : U64} (hlo : lo.val < 512) : + region K.toKLayout (peAF (e (atAF K.ksb hi)) lo) = .none := by + obtain ⟨τ, H, Cs, OS, U, Ac, Pp, Tb, Vc, Ub, Lv, n, m, hKe, EI, PL, UB, hcs, hn, -, hag⟩ := h + have tA := S1b.asidTab_of_P1 (Sl := OS) (U := ∅) EI.nil PL.nil hcs hn fun _ h => h + have kb : K.ksb = L.ksb τ := by rw [hKe]; rfl + have et : e (atAF (L.ksb τ) hi) = L.mem τ (atAF (L.ksb τ) hi) := + (hag _ (tA hi hhi).2 (by rw [hKe]; exact S1b.ubTab EI PL UB hhi)).symm + rw [kb, et] at h0 ⊢ + have a := (S1b.asidPool_of_P1 (Sl := OS) (U := ∅) EI.nil PL.nil hcs hn (fun _ h => h) hi hhi h0 lo hlo).2.1 + rw [hKe] + exact a + +/-- **`TabNS` at an entry state with the epoch data** whose kernel base is the layout's (module section docstring). -/ +theorem gateEpPA_tabNS {K : KLayoutP} {s : S} (G : E2EPA.GateEpPA st (EpochAP st) K s) (hkb : K.ksb = L.ksb s) : + TabNS st K.toKLayout s := by + obtain ⟨e, ⟨hE, -, -, -, hT, -, -⟩, hg⟩ := G + intro hi hhi + rw [← hkb] + obtain ⟨ha, hu, hr, hv⟩ := hE.tabW hhi + have hp := (hT hi hhi).1 + have ag : L.mem s (atAF K.ksb hi) = e (atAF K.ksb hi) := by + refine hg _ ?_ ha hu + have hns : ¬ OwnMapV.Special K.toKLayoutV (atAF K.ksb hi) := by + rintro (hv' | ⟨t', hr', -⟩) + · exact hv hv' + · rw [hr] at hr'; cases hr' + show OwnMapP.ownP K e _ = .gate + rw [OwnMapP.ownP_off hp, OwnMapV.ownV_off hns] + simp only [OwnMap.own, hr] + refine ⟨hr, fun h0 lo hlo => ?_⟩ + rw [ag] at h0 ⊢ + exact hE.poolW hhi h0 hlo + +variable {K : KLayoutV} {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} + {Locks : S → U64 → Prop} {vc : U64} {s : S} + +/-- **The exit's entry facts at the lock map without slot locks** (module section docstring). -/ +theorem vxPreR_nsl (hK : OwnMapV.RegionOKV K) (P : VxPreR st K T Cs Ω H Sg Rp Scs Nt Z Ac Locks vc s) + (hT : TabNS st K.toKLayout s) : VxPreR st K T Cs Ω H Sg Rp Scs Nt Z Ac (lkNS K.toKLayout Locks) vc s where + toVxPre := + { toVBase := P.toVBase + vcg := P.vcg + lkv := fun h0 => ⟨P.lkv h0, vc_nsl hK (P.vcK h0)⟩ + vcK := P.vcK + vof := P.vof } + tab hi hhi := + ⟨⟨(P.tab hi hhi).1.1, (P.tab hi hhi).1.2.1, rperm_nsl hK (hT hi hhi).1 (P.tab hi hhi).1.2.2⟩, + fun lo hlo hne => ⟨((P.tab hi hhi).2 lo hlo hne).1, ((P.tab hi hhi).2 lo hlo hne).2.1, + rperm_nsl hK ((hT hi hhi).2 hne lo hlo) ((P.tab hi hhi).2 lo hlo hne).2.2⟩⟩ + asid := P.asid + asidSep := P.asidSep + hub := P.hub + vfit := P.vfit + gbind := P.gbind + buf := P.buf + +end vx + +section vxs +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The `ARMVCPURun` exit path at a PTE layout keeps the slot words** from its entry states with the epoch data +(module section docstring): an accepted run runs on the store guarded to `VxWS` at `lkNS K Locks` (`vxa_accR` at +`vxPreR_nsl`); a declined one writes the node's two timestamp words. -/ +theorem vx_kept {j : VxArgA S} {Kp : KLayoutP} {m : U64 → U64} (hK : ROKP Kp) (hS : SlotLayK Kp.toKLayout) + (h : VxPremVB st ar j Kp.toKLayoutV m) : + KeptPE (EpochAP st) Kp (vxa_localPathP st ar hA hm hK.v h.toA) := by + have hKV : OwnMapV.RegionOKV Kp.toKLayoutV := hK.1.base + refine LocalPath.keptOfG _ _ _ (vexitA_noZero j.vc j.stop j.w1 j.w2 j.w3 j.hvc) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp hG hr hb + change b = true at hb + subst hb + change localvm.handle_arm_vcpu_run_exit_local st ar s a j.vc j.stop j.w1 j.w2 j.w3 j.hvc = ok (true, s', a') at hr + have P : VxPreR st Kp.toKLayoutV j.T j.Cs j.Ω j.H j.Sg j.Rp j.Scs j.Nt j.Z j.Ac j.Locks j.vc s := hp.1.toVxPreR + have P' := vxPreR_nsl hKV P (gateEpPA_tabNS hG P.kb) + refine ⟨VxRS (st := st) Kp.toKLayoutV (lkNS Kp.toKLayout j.Locks) s, + VxWS (st := st) Kp.toKLayoutV (lkNS Kp.toKLayout j.Locks) s, fun b hb => ⟨hb.1, ?_⟩, ?_⟩ + · rcases hb.2 with hu | hw + · exact nsl_of_region (P.lay.base.user b hu).1 (fun _ e => by cases e) + · exact nsl_of_heldV hKV hS (fun _ h => h.2) hw + · change localvm.handle_arm_vcpu_run_exit_local (guard st + (VxRS (st := st) Kp.toKLayoutV (lkNS Kp.toKLayout j.Locks) s) + (VxWS (st := st) Kp.toKLayoutV (lkNS Kp.toKLayout j.Locks) s)) ar s a j.vc j.stop j.w1 j.w2 j.w3 j.hvc = + ok (true, s', a') + exact vxa_accR (st := st) (Locks := lkNS Kp.toKLayout j.Locks) hA h.hw hKV h.g0 s a _ P' hr rfl + · intro s x hp _ hx + have P : VxPreR st Kp.toKLayoutV j.T j.Cs j.Ω j.H j.Sg j.Rp j.Scs j.Nt j.Z j.Ac j.Locks j.vc s := hp.1.toVxPreR + rcases (hx : VmDS (st := st) s x) with rfl | rfl + · exact nsl_of_region (P.toVBase.nodeReg _ (by simp [layout.node.CUR_TIME])) (fun _ e => by cases e) + · exact nsl_of_region (P.toVBase.nodeReg _ (by simp [layout.node.CONSUMED])) (fun _ e => by cases e) + +/-- **The `ARMVCPURun` exit slot of `fl4LocPB` keeps `MdbS`** from its entry states with the epoch data (`vx_kept`, +gen 31). -/ +theorem vxSlotP_mdb (j : VxArgA S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepPE (EpochAP st) K + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inl j))))))) K m hK) := + mdbKeepG_of_kept hS <| SlotVqP.keptE (Y := vqSlotP st ar hA hm) (j := .inl j) _ _ fun h => + vx_kept st ar hA hm hK hS h + +end vxs + +/-! ## The syscall_local slot (gen 31) + +Architect 14:49Z, item 2: the same lock-map method. The path's accepted run is `sl_pathObl_e`'s `acc` at the lock +map `lkNS K Locks`, given the entry records `SlPreB` / `SlEntJ` / `SlAccK` there (`SlHereJ` has no lock map). Their +lock fields name the locks fl4-sync holds: the handler endpoint and the ReplyRecv endpoint (heap endpoints, region +`.obj`), the reply objects (heap replies, `frp_of`), and in `Safe0` the block object of a queued thread +(**`sl_qnsl`**: P1's `EpsWit` at the prefix state makes a blocked thread's block object a heap endpoint or +notification, and the prefix keeps the thread's `STATE` / `BLOCK_OBJ`); the `STATE` / `BLOCK_OBJ` words in `SlWS` +are their home core's (`own_state_home`). So every lock they name is no slot word, and every written word is a user +word (region `.none`, `RegionOKX.kern`) or held under `lkNS` (`nsl_of_held`); a declined run writes the prefix words +(node or current-TCB region, `SlEntJ.dsR`). -/ + +section sl +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable {K : KLayout} {Locks : S → U64 → Prop} {m0 : U64 → U64} {n : I64} {found : U64 × U64} + {T Cs Ω U Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} + +/-- A heap endpoint is no slot word. -/ +theorem ep_nsl (hX : RegionOKX K) {p : U64} (hp : K.heap p = some .ep) : ¬ SlotWd K p := by + have r := (hX.epW p hp 0#u64 (by simp) (by simp)).1 + rw [SFF.wadd0'] at r + exact nsl_of_region r (fun _ e => by cases e) + +/-- A heap reply is no slot word. -/ +theorem rp_nsl (hX : RegionOKX K) {p : U64} (hp : K.heap p = some .reply) : ¬ SlotWd K p := by + have r := (hX.rpW p hp 0#u64 (by simp) (by simp)).1 + rw [SFF.wadd0'] at r + exact nsl_of_region r (fun _ e => by cases e) + +/-- A heap notification is no slot word. -/ +theorem ntfn_nsl (hX : RegionOKX K) {p : U64} (hp : K.heap p = some .ntfn) : ¬ SlotWd K p := by + have r := (hX.ntW p hp 0#u64 (by simp) (by simp)).1 + rw [SFF.wadd0'] at r + exact nsl_of_region r (fun _ e => by cases e) + +/-- `Safe0` at the lock map without slot locks, given that a queued thread's block object is no slot word. -/ +theorem safe0_nsl {c : U64} {lk : U64 → Prop} {m : U64 → U64} {d : U64} (h : OwnMap.Safe0 c lk m d) + (hq : OwnMap.QueuedState (m (fld d oSTATE)) → ¬ SlotWd K (m (fld d OwnMap.oBLOCK_OBJ))) : + OwnMap.Safe0 c (fun l => lk l ∧ ¬ SlotWd K l) m d := + ⟨h.1, fun q => ⟨h.2 q, hq q⟩⟩ + +/-- A word of `SlWS` whose owner is no slot word's lock is in `SlWS` at `lkNS`. -/ +theorem slWS_nsl {s : S} {x : U64} (hx : ∀ l, own K (L.mem s) x = .lock l → ¬ SlotWd K l) + (h : SlWS (st := st) K Locks s x) : SlWS (st := st) K (lkNS K Locks) s x := by + refine ⟨h.1, h.2.imp id fun hw => ?_⟩ + unfold WPerm at hw ⊢ + revert hw hx + generalize own K (L.mem s) x = o + intro hx hw + cases o with + | core d => exact hw + | lock l => exact ⟨hw, hx l rfl⟩ + | gate => exact hw.elim + +/-- A TCB's `STATE` / `BLOCK_OBJ` word in `SlWS` is in `SlWS` at `lkNS` (its owner is its home core). -/ +theorem slWS_home_nsl (hK : RegionOK K) {s : S} {d : U64} (hd : K.heap d = some .tcb) {off : U64} + (hoff : off = oSTATE ∨ off = OwnMap.oBLOCK_OBJ) (h : SlWS (st := st) K Locks s (fld d off)) : + SlWS (st := st) K (lkNS K Locks) s (fld d off) := + slWS_nsl (fun l e => by rw [OwnMap.own_state_home hK hd hoff] at e; cases e) h + +/-- **A queued thread's block object is no slot word** (module section docstring): P1's `EpsWit` at the prefix +state, whose `STATE` / `BLOCK_OBJ` the prefix keeps. -/ +theorem sl_qnsl (hSt : P0.StoreTotal st) (hA : P0.ArchTotal ar) (hX : RegionOKX K) + (hSe : SlSets K T Rp Scs Nt H Z) (a0 : A) {s : S} + (E : SlPreB (st := st) ar K Locks m0 n found T Cs Ω U Sg Rp Scs Nt Ac H Z s) + (Hj : SlHereJ (st := st) K Z Scs found s) {d : U64} (hd : K.heap d = some .tcb) + (hq : OwnMap.QueuedState (L.mem s (fld d oSTATE))) : ¬ SlotWd K (L.mem s (fld d OwnMap.oBLOCK_OBJ)) := by + obtain ⟨X, a1, LP⟩ := lp_ex ar hSt hA E.hT E.hcT a0 + obtain ⟨⟨Q, R, hSQ⟩, -⟩ := E.hI a0 X a1 LP + obtain ⟨Ep, EQ, -, -, -, -, c5, -, -, k0, -⟩ := hSQ.eps + have hdT : d ∈ T := by + rw [hSe.tcbs] + show H d = some .tcb + rw [← hSe.heap] + exact hd + have hc : K.heap (L.curW s) = some .tcb := hSe.tcb E.hcT + have keep : ∀ o : U64, 224 ≤ o.val → o.val < 4096 → o.val % 8 = 0 → o.val ≠ 528 → + L.mem X (core.num.U64.wrapping_add d o) = L.mem s (core.num.U64.wrapping_add d o) := + fun o h1 h2 h3 h4 => lp_mem ar hA E.hT E.hN E.hcT LP (hX.tcbW d hd o h1 h2 h3).2.2 + (not_slDS_tcb hX E.hN E.hT E.hcT hc Hj.ksb Hj.cpu hd h1 h2 h3 h4) + have eS : L.mem X (stA d) = L.mem s (fld d oSTATE) := by + rw [OwnMap.stA_fld] + exact keep oSTATE (by rw [OwnMap.oSTATE_val]; omega) (by rw [OwnMap.oSTATE_val]; omega) + (by rw [OwnMap.oSTATE_val]) (by rw [OwnMap.oSTATE_val]; omega) + have eB : L.mem X (blockA d) = L.mem s (fld d OwnMap.oBLOCK_OBJ) := by + rw [OwnMap.blockA_fld] + exact keep OwnMap.oBLOCK_OBJ (by rw [OwnMap.oBOBJ_val]; omega) (by rw [OwnMap.oBOBJ_val]; omega) + (by rw [OwnMap.oBOBJ_val]) (by rw [OwnMap.oBOBJ_val]; omega) + have hB : Blocked (L.mem X (stA d)) := by + rw [eS] + rcases hq with h | h + · exact Or.inr (Or.inl (UScalar.eq_of_val_eq (by rw [h]; simp [sched.TS_BLOCKED_ON_RECV]))) + · exact Or.inr (Or.inr (UScalar.eq_of_val_eq (by rw [h]; simp [sched.TS_BLOCKED_ON_NTFN]))) + have hE := (c5 d hdT (Set.notMem_empty d) hB).1 + rw [eB] at hE + rcases (k0 _).1 hE with h | h + · exact ep_nsl hX (by rw [hSe.heap]; exact h) + · exact ntfn_nsl hX (by rw [hSe.heap]; exact h) + +/-- **`SlPreB` at the lock map without slot locks** (module section docstring). -/ +theorem slPreB_nsl (hSt : P0.StoreTotal st) (hA : P0.ArchTotal ar) (hK : RegionOK K) (hX : RegionOKX K) + (hSe : SlSets K T Rp Scs Nt H Z) (a0 : A) {s : S} + (E : SlPreB (st := st) ar K Locks m0 n found T Cs Ω U Sg Rp Scs Nt Ac H Z s) + (Hj : SlHereJ (st := st) K Z Scs found s) : + SlPreB (st := st) ar K (lkNS K Locks) m0 n found T Cs Ω U Sg Rp Scs Nt Ac H Z s := + have q := fun {d : U64} (hd : K.heap d = some .tcb) => sl_qnsl ar hSt hA hX hSe a0 E Hj hd + { E with + hSt := ⟨E.hSt.1, safe0_nsl E.hSt.2 (q E.hSt.1)⟩ + hSd := fun hS a X a1 LP {_p _b _d _r} hc => + have w := E.hSd hS a X a1 LP hc + have hcT := hSe.tcb E.hcT + ⟨w.1, safe0_nsl w.2.1 (q w.1), + by rw [OwnMap.stA_fld]; exact slWS_home_nsl hK hcT (Or.inl rfl) (by rw [← OwnMap.stA_fld]; exact w.2.2.1), + by rw [OwnMap.stA_fld]; exact slWS_home_nsl hK w.1 (Or.inl rfl) (by rw [← OwnMap.stA_fld]; exact w.2.2.2)⟩ + hSu := fun hS a X a1 LP u usc hc => + have w := E.hSu hS a X a1 LP u usc hc + have hcT := hSe.tcb E.hcT + ⟨w.1, safe0_nsl w.2.1 (q w.1), + by rw [OwnMap.stA_fld]; exact slWS_home_nsl hK hcT (Or.inl rfl) (by rw [← OwnMap.stA_fld]; exact w.2.2.1), + by rw [OwnMap.blockA_fld]; exact slWS_home_nsl hK hcT (Or.inr rfl) (by rw [← OwnMap.blockA_fld]; exact w.2.2.2.1), + by rw [OwnMap.stA_fld]; exact slWS_home_nsl hK w.1 (Or.inl rfl) (by rw [← OwnMap.stA_fld]; exact w.2.2.2.2)⟩ + hlk := fun hS => ⟨E.hlk hS, ep_nsl hX (Hj.ep hS)⟩ } + +/-- **`SlEntJ` at the lock map without slot locks** (the handler endpoint and the reply are heap objects). -/ +theorem slEntJ_nsl (hX : RegionOKX K) {s : S} (N : SlEntJ (st := st) K Locks found s) : + SlEntJ (st := st) K (lkNS K Locks) found s := + { N with + lep := fun hS hE => ⟨N.lep hS hE, ep_nsl hX (N.hep hS hE)⟩ + lrp := fun hS => ⟨N.lrp hS, rp_nsl hX (N.hrp hS)⟩ } + +/-- **`SlAccK` at the lock map without slot locks** (the receiver's reply is a heap reply, `frp_of`). -/ +theorem slAccK_nsl (hA : P0.ArchTotal ar) (hX : RegionOKX K) (hSe : SlSets K T Rp Scs Nt H Z) {s : S} + (E : SlPreB (st := st) ar K Locks m0 n found T Cs Ω U Sg Rp Scs Nt Ac H Z s) + (Ak : SlAccK (st := st) ar K Locks found s) : SlAccK (st := st) ar K (lkNS K Locks) found s := + { Ak with + frl := fun hS a X a1 LP {_p _b _d _r} hc => ⟨Ak.frl hS a X a1 LP hc, rp_nsl hX (frp_of ar hA hSe E hS LP hc)⟩ } + +/-- **The syscall_local path keeps the slot words** (module section docstring): an accepted run runs on the store +guarded to `SlWS` at `lkNS K Locks` (`sl_pathObl_e`'s `acc` at `slPreB_nsl` / `slEntJ_nsl` / `slAccK_nsl`); a +declined one writes the prefix words. -/ +theorem sl_kept (hSt : P0.StoreTotal st) (hA : P0.ArchTotal ar) (hK : RegionOK K) (hX : RegionOKX K) + (hU : E2E.UserOK K) (hm : E2E.MasksWr st) (hSe : SlSets K T Rp Scs Nt H Z) (hUu : ∀ b ∈ U, b ∈ K.user) (a0 : A) + {P : S → Prop} (hPE : ∀ s, P s → SlPreB (st := st) ar K Locks m0 n found T Cs Ω U Sg Rp Scs Nt Ac H Z s) + (hEj : ∀ s, P s → SlEntJ (st := st) K Locks found s) + (hHj : ∀ s, P s → SlHereJ (st := st) K Z Scs found s) + (hAk : ∀ s, P s → SlAccK (st := st) ar K Locks found s) + (huf : ∀ s b v, P s → b ∈ K.user → Aligned b → + P (L.wr s b v) ∧ Locks (L.wr s b v) = Locks s ∧ + SlRS (st := st) K Locks (L.wr s b v) = SlRS (st := st) K Locks s ∧ + SlWS (st := st) K Locks (L.wr s b v) = SlWS (st := st) K Locks s ∧ + SlDS (L := L) (L.wr s b v) = SlDS (L := L) s) + (hNk : ∀ s, P s → SlNUK (L := L) K s) (hS : SlotLayK K) : + KeptP K (sl_localPathG2 ar hSt hA hK hX hU hm hSe hUu a0 hPE hEj hHj hAk huf hNk) := by + refine LocalPath.keptOf _ K (sl_noZero n found) ?_ ?_ + · rintro s a ⟨b, s', a'⟩ hp h hr + change b = true at hr + subst hr + refine ⟨SlRS (st := st) K (lkNS K Locks) s, SlWS (st := st) K (lkNS K Locks) s, + fun b hb => ⟨hb.1, ?_⟩, ?_⟩ + · rcases hb.2 with hu | hw + · exact fun ⟨q, hq⟩ => hX.kern b (by rw [hq]; intro e; cases e) hu + · exact nsl_of_held hS (fun _ h => h.2) hw + · exact (sl_pathObl_e ar hSt hA hK hX hU (sl_isProg S n found) (sl_noZero n found) hm hSe hUu a0 (P := P) + (fun s hp => slPreB_nsl ar hSt hA hK hX hSe a0 (hPE s hp) (hHj s hp)) + (fun s hp => slEntJ_nsl hX (hEj s hp)) hHj + (fun s hp => slAccK_nsl ar hA hX hSe (hPE s hp) (hAk s hp))).acc s a _ hp h rfl + · intro s x hp hx + rcases (hEj s hp).dsR x hx with ⟨c, e⟩ | e + · exact nsl_of_region e (fun _ e => by cases e) + · exact nsl_of_region e (fun _ e => by cases e) + +end sl + +section sls +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The syscall_local slot of `fl4LocPB` keeps `MdbS`** (`sl_kept`; a frame slot, gen 31). -/ +theorem slSlotP_mdb (i : SlArgA S A) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) : + MdbKeepP K.toKLayout.slots (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inl i))))) K m hK) := + mdbKeep_of_kept hS <| SlotCPG.kept _ _ fun h => + have h' := h.toA + sl_kept ar h'.hSt hA (vmapV.base _ hK.v) h'.hX (vmapV.user _ hK.v) hm h'.hSe h'.hUu i.a0 (fun _ e => e.1) + (fun _ e => e.2.1) (fun _ e => e.2.2.1) (fun _ e => e.2.2.2) h'.huf h'.vc.nuk hS + +end sls + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/MdbKeepDelA.lean b/proofs/Fl4ProofsA64/P2/MdbKeepDelA.lean new file mode 100644 index 000000000..3512d5ba5 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbKeepDelA.lean @@ -0,0 +1,391 @@ +/- +# P2 (AArch64 integration, P2-A64X gen 32): **the Delete-with-PTE slot of `fl4LocPB` keeps P1's `MdbS`** + +Architect 15:13Z, item 2. The Delete-with-PTE slot (`delSlotP`, `inv_localPathPA`) deletes a slot holding a mapped +small `Frame` cap. An accepted run goes through `inv_accMdbG` (`P2/MdbKeepPmA.lean`: the prefix, `after_invocation` +and `finish` keep every slot word); its decode block (**`del_blkMdb`**): + +* a message shorter than two words: the decode returns `TruncatedMessage` at the post-prefix state, the reply writes + the caller's `IoTW` words and user words (`reply_error_ku8`), no slot word; `FinOK` from `truncFinR`; +* otherwise (`DelEntP`, `delEntP_of`): `set_thread_state` (the caller's `IoTW` words), then `cdt.delete` of the target + `dest`, which is **`unmap_page`** (`unmapLeaf` at the plan's leaf `e`, a PTE word, no slot word: `unmap_pte`, + `unmapLeaf_st`) then **`cdt.empty_slot dest .Null`** (**`del_runSt`**). `MdbS` is kept to `unmap_page`'s state + (`MdbS.keep`), and P1's **`cdt.empty_slot.mdbO`** carries it through `empty_slot`: the cleanup is `Null`, so + `post_cap_deletion` writes nothing (`pcd_null`, `Pw := False`). The reply (`invRep_ok_ku8` at the layout with no + user word, `SchedOK` from `cnDelP_keep`) writes `IoTW` words only. `FinOK` from `delFinR`. + +P2-Sync gen 4 (Architect 17:41Z, proofs only, every statement above unchanged): **`DelSl`** / **`del_blkSl`** (the +block's slot words: the entry's, or those of `empty_slot dest .Null` from a state with the entry's slot words; +`del_blkMdb` now derives `MdbS` from it) and **`del_accSl`**, for the edit form (`P2/MdbWirePDA.lean`). + +**`delSlotP_mdb`**: the slot of `fl4LocPB` keeps `MdbS` (`MdbKeepP`; `MdbKeepP.toE` gives `MdbKeepPE`). +**`EpochA.osok`** / **`EpochAP.osok`**: the `OSOK` both slot lemmas take holds at every epoch layout (`EntryInvA.os`), +so the wiring discharges it from the serial state's epoch data. + +Lemma hypotheses (no premise of any top-level theorem): `hS : SlotLayK` and the **state-free `hO : OSOK T Ω Sg +K.slots`** (`cdt.empty_slot.mdbO`'s slot-set facts, with `∀ y ∈ OS, y ≠ 0` as `OSOK.nz`), as the ruling allows. No new +[J], [P] or [W]; `EpochAP`, `ExecPA`, `fl4LocPB` and `serializable_fl4_localVP` are unchanged. +-/ +import Fl4ProofsA64.P2.MdbKeepPmA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA Fl4ProofsA64.P2.E2EP +open Fl4ProofsA64.P2.OwnMap (KLayout region fld own InW RegionOK) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP UserOKP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA) + +noncomputable section +open Classical + +section del +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {μ : U64 → U64} + +/-- A word of some slot's 32 bytes is a slot-region word (`SlotLayK.sw`). -/ +theorem SlotLayK.wd {Kl : KLayout} (hS : SlotLayK Kl) {b : U64} (h : SlotW Kl b) : SlotWd Kl b := + let ⟨q, hq, hb⟩ := h + ⟨q, hS.sw q hq b hb⟩ + +/-- A slot-region word is a word of some slot's 32 bytes (`region_slot_inv`). -/ +theorem SlotWd.w {Kl : KLayout} {b : U64} (h : SlotWd Kl b) : SlotW Kl b := + let ⟨_, hq⟩ := h + let ⟨h1, h2⟩ := OwnMap.region_slot_inv hq + ⟨_, h1, h2⟩ + +/-- **`cdt.delete` of a mapped small frame slot on `st`**: `unmap_page`, then `empty_slot` with a null cleanup. -/ +theorem del_runSt {x : S} {a : A} {dest fp rt asid va : U64} + (hc : obj.Slot.cap st dest x = ok (.Frame fp .Small rt false true false asid va)) (hA0 : asid ≠ 0#u64) + {y : core.result.Result Unit error.Exc × S × A} (h : cdt.delete st ar x a dest true = ok y) : + ∃ x1 a1, vspace.unmap_page st ar x a .Small asid va fp = ok (x1, a1) ∧ + cdt.empty_slot st ar x1 a1 dest .Null = ok (y.2.1, y.2.2) := by + unfold cdt.delete at h + simp only [↓reduceIte] at h + obtain ⟨y0, hf, h⟩ := bind_eq_ok h + obtain ⟨x1, a1, hu, rfl⟩ := finImm_frame_pte ar hc hA0 hf + refine ⟨x1, a1, hu, ?_⟩ + try simp only [uncurry_apply_pair] at h + try dsimp only at h + obtain ⟨cf, ecf, h⟩ := bind_eq_ok h + try simp only [core.result.Result.Insts.CoreOpsTry.branch] at ecf + rw [← ok_inj_ff ecf] at h + try dsimp only at h + obtain ⟨⟨x2, a2⟩, he, h⟩ := bind_eq_ok h + rw [← ok_inj_ff h] + exact he + +variable (st ar) in +/-- (P2-Sync gen 4) **The slot words of a Delete-with-PTE decode block's end `z`**: those of the entry `s`, or those +of `cdt.empty_slot dest .Null` (`dest` the target the CNode cap at the plan's slot finds at `s`, a slot of `Kl`) run +from a state `x1` with `s`'s slot words. -/ +def DelSl (Kl : KLayout) (p : localinv.Plan) (s z : S) : Prop := + (∀ b, Aligned b → SlotWd Kl b → L.mem z b = L.mem s b) ∨ + ∃ (cp cr cg cgb dest : U64) (x1 y1 : S) (a1 a2 : A), obj.Slot.cap st p.slot s = ok (.CNode cp cr cg cgb) ∧ + cspace.lookup_target_slot st s (.CNode cp cr cg cgb) (L.mem s (regA (L.curW s) layout.reg.X2)) + (L.mem s (regA (L.curW s) layout.reg.X3)) = ok (.Ok dest) ∧ dest ∈ Kl.slots ∧ + (∀ b, Aligned b → SlotWd Kl b → L.mem x1 b = L.mem s b) ∧ cdt.empty_slot st ar x1 a1 dest .Null = ok (y1, a2) ∧ + ∀ b, Aligned b → SlotWd Kl b → L.mem z b = L.mem y1 b + +/-- (P2-Sync gen 4) **The Delete-with-PTE decode block's slot words** (module docstring; gen 32's `del_blkMdb` proof +with the conclusion stated on the slot words; P2-Sync gen 5: at `PrePAL`). -/ +theorem del_blkSlL (hA : P0.ArchTotal ar) (hS : SlotLayK K.toKLayout) {s : S} {a : A} + (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) (hW : IoWr st (NSl K.toKLayout) K.heap.scBits s) + {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} + {y : (core.result.Result Unit error.Exc) × S × A} {z : S × A × core.result.Result Unit error.Exc} + (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) + (hz : invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z) : DelSl st ar K.toKLayout p s z.1 := by + have hq := hE.2.1 + have PE := hq.1.1.1 + have N := io_narNS PE + have nSt : NSl K.toKLayout (stA (L.curW s)) := + ⟨(PE.stw (Locks := Locks)).2.1, N.curW _ (by rw [st_val]; omega) (by rw [st_val]; omega) (by rw [st_val]; omega)⟩ + have nT : ∀ b, IoTW (L.curW s) b → ¬ SlotWd K.toKLayout b := by + rintro b (⟨r, hr, rfl⟩ | rfl) + · exact (N.reg PE.tA hr).2 + · exact nSt.2 + rw [(hq.1.2.2.1 (L.curW s) info (cur_thread_ok s) emi).1] at hy + obtain ⟨Pp, Tb, Lv, m1, hV, h0T, hc, hag⟩ := hE.agT + have E := delEntP_ofL hE + have hsK : p.slot ∈ K.toKLayout.slots := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, h, -⟩ := del_coreP hq; exact h + -- the post-prefix state + obtain ⟨-, hs1, hu, hi3, -, hb2, -⟩ := id C + have w0 := prefix_writes ar hA PE.tp PE.np PE.cT hs1 hu + have Hf := here_facts hb2 + have Ent := PE.entR (Locks := Locks) hW + have I := IoS2.of_ent hA Ent hs1 hu Hf.sc + have hct : L.curW s2 = L.curW s := I.cur + have ha2 : L.actionW s2 = sched.ACTION_RESUME_CURRENT := by + rw [eq_of_spec_fd (sched.action.spec (L := L) s2)] at hi3; exact ok_inj_ff hi3 + have hrun : L.mem s2 (stA (L.curW s)) = sched.TS_RESTART ∨ L.mem s2 (stA (L.curW s)) = sched.TS_RUNNING := by + rw [I.st]; exact Ent.run + have hK : SchedOK (L := L) s2 (L.curW s) := by + have h0 := SchedOK.of_sw I.F (by rw [hct]; exact Hf.sc) (by rw [hct]; exact Hf.nr) + rwa [hct] at h0 + have hL : Slot8 K.toKLayout := fun q hq' => ⟨PE.lay.slot8 q hq', PE.lay.slotf q hq'⟩ + have ag : ∀ b, SlotW K.toKLayout b → Aligned b → L.mem s2 b = L.mem s b := fun b ⟨q, hq', hbq⟩ hb => + PE.slot_keep w0 hq' hbq hb + have kP : ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem s2 b = L.mem s b := fun b hb hsl => ag b hsl.w hb + obtain ⟨cp, cr, cg, cgb, dest, fp, rt, asid, va, e, Cw, hcs, hlk, hdc, ⟨hA0, he, -, -⟩, hC, hcl, hWk, hd, -, -, + hpv, hnx⟩ := E + rw [cap_agree hL hsK ag, hcs] at hcap + cases ok_inj_ff hcap + rw [decode_cnode_eq ar] at hy + by_cases hlen : length < 2#u64 + · -- `TruncatedMessage`: the reply's error words + rw [cnode_decode_trunc ar st hlen] at hy + cases ok_inj_ff hy + simp only at hz + unfold invRep at hz + obtain ⟨q0, eq0, hz⟩ := bind_eq_ok hz + cases ok_inj_ff hz + obtain ⟨s6, es6, e6⟩ := bind_eq_ok eq0 + cases ok_inj_ff e6 + have O : UsrOK (L := L) K.toKLayout s2 (L.curW s) := by + refine ⟨fun o ho => ?_, fun k h1 h2 => PE.tcb_notUser (PE.hT _ PE.cT) h1 h2, ?_⟩ + · rw [I.cp, nodeAt_of_ksb I.k]; exact PE.node_notUser ho + · rw [← hct, I.F.i]; exact PE.sc_notUser (PE.hSc _ PE.cscS) I.F.q.2.2.2.1 + have tk : ∀ k : U64, k.val < 4096 → k.val % 8 = 0 → k.val ≠ 528 → + L.mem s2 (core.num.U64.wrapping_add (L.curW s) k) = L.mem s (core.num.U64.wrapping_add (L.curW s) k) := + fun k h1 h2 h3 => w0.2.2 _ (field_aligned PE.tA h1 h2) (preW_tcb PE.tA PE.np PE.cT h1 h3) + have hlb := lookup_buf_keep (st := st) PE.tA true tk + have kr := reply_error_ku8 hK O (KeepU.of_keepA (KeepA.refl ha2 hrun)) + (fun b eb hne i hi => hE.2.2.2.2.2.2.2 b (by rw [← hlb]; exact eb) hne i hi) es6 + refine Or.inl fun b hb hsl => ?_ + rw [kr.mem hb (fun ht' => nT b ht' hsl) (fun hu' => IoPre.user_nsl PE hu'.2 hsl), kP b hb hsl] + -- the target lookup at the post-prefix state + have rk : ∀ r : U64, r.val < 384 → r.val ≠ 34 → + L.mem s2 (regA (L.curW s) r) = L.mem s (regA (L.curW s) r) := fun r h1 h2 => by + have e1 := reg_keepK (st := st) PE.tA (r := r) h1 h2 fun k k1 k2 k3 => + w0.2.2 _ (field_aligned PE.tA k1 k2) (preW_tcb PE.tA PE.np PE.cT k1 k3) + rw [reg_ok, reg_ok] at e1; exact ok_inj_ff e1 + have hlk2 : cspace.lookup_target_slot st s2 (.CNode cp cr cg cgb) (L.mem s2 (regA (L.curW s) layout.reg.X2)) + (L.mem s2 (regA (L.curW s) layout.reg.X3)) = ok (.Ok dest) := by + rw [rk _ (by simp [layout.reg.X2]) (by simp [layout.reg.X2]), rk _ (by simp [layout.reg.X3]) + (by simp [layout.reg.X3])] + unfold cspace.lookup_target_slot cspace.lookup_slot_for_cnode_op at hlk ⊢ + simp only [bind_ok, ↓reduceIte] at hlk ⊢ + by_cases g1 : L.mem s (regA (L.curW s) layout.reg.X3) < 1#u64 + · rw [if_pos g1] at hlk; simp at hlk + rw [if_neg g1] at hlk ⊢ + by_cases g2 : L.mem s (regA (L.curW s) layout.reg.X3) > cspace.WORD_BITS + · rw [if_pos g2] at hlk; simp at hlk + rw [if_neg g2] at hlk ⊢ + unfold cspace.resolve_address_bits at hlk ⊢ + obtain ⟨res, eres, hres⟩ := bind_eq_ok hlk + rw [rab_loop_congr2 st Cw (fun q r hq m hm x => cap_agree hL (hWk q r m x hq hm).1 ag) hcl 66 _ _ 65#u64 res + (by simp) (fun _ _ _ _ e' => by cases e'; exact hC) eres, bind_ok] + exact hres + have hdc2 : obj.Slot.cap st dest s2 = ok (.Frame fp .Small rt false true false asid va) := by + rw [cap_agree hL hd ag]; exact hdc + have hpv2 : L.mem s2 (core.num.U64.wrapping_add dest layout.cte.PREV) ≠ 0#u64 → + L.mem s2 (core.num.U64.wrapping_add dest layout.cte.PREV) ∈ K.toKLayout.slots := by + rw [ag _ (slotW_of hL hd (by simp [layout.cte.PREV])) (slot_al hL hd (by simp [layout.cte.PREV]) + (by simp [layout.cte.PREV]))] + exact fun h0 => (hpv h0).1 + have hnx2 : ∀ nx, obj.Slot.next st dest s2 = ok nx → nx ≠ 0#u64 → nx ∈ K.toKLayout.slots := fun nx e1 h0 => + (hnx nx (by rw [← next_agree hL hd ag]; exact e1) h0).1 + -- the leaf + have hVe : VsW K.toKLayout e := leafVsWT hV hag he + have ne : ¬ SlotWd K.toKLayout e := fun h' => PE.vs_notSlot hVe h'.w + have hu3 : ∀ x3, sched.set_thread_state st s2 (L.curW s) sched.TS_RESTART = ok x3 → ∀ a' : A, + vspace.unmap_page st ar x3 a' .Small asid va fp = unmapLeaf st ar x3 a' e asid va fp (unmapHi st s asid) := by + intro x3 hs3 a' + have K3 := (KeepA.refl ha2 hrun).sts hK (Or.inl rfl) hs3 + refine unmap_pte ar (fun b hb => ?_) ?_ (vsRdT hV hag hc asid va) he a' + · rw [L.read_eq, L.read_eq, K3.mem hb.1 (PE.vs_notIoTW hb), w0.2.2 b hb.1 (PE.vs_notPreW hb)] + · rw [L.ks_base_eq, L.ks_base_eq, K3.1.1, w0.1] + have O' : SlotOKX (L := L) K.toKLayout (fun b => b = e) s2 (L.curW s) := by + have hX : ∀ b, (fun b => b = e) b → VsW K.toKLayout b := fun b hb => by subst hb; exact hVe + refine ⟨fun o ho => ?_, fun k h1 h2 => PE.tcb_notSlotX hX (PE.hT _ PE.cT) h1 h2, ?_⟩ + · rw [I.cp, nodeAt_of_ksb I.k]; exact PE.node_notSlotX hX ho + · rw [← hct, I.F.i]; exact PE.sc_notSlotX hX (PE.hSc _ PE.cscS) I.F.q.2.2.2.1 + obtain ⟨hy1, kx⟩ := cnDelP_keep ar hK O' ha2 hrun hL hlen hlk2 hd hdc2 hA0 hu3 rfl + (PE.vs_notSlot hVe) hVe.1 hpv2 hnx2 hy + -- the decode: the restart, `unmap_page`, `empty_slot` + unfold cnode.decode at hy + rw [if_neg lblD1, if_neg lblD2, if_neg hlen, cur_thread_ok, hct] at hy + simp only [bind_ok] at hy + rw [sarg0_reg, sarg1_reg, reg_ok, reg_ok] at hy + simp only [bind_ok] at hy + rw [hlk2] at hy + simp only [core.result.Result.Insts.CoreOpsTry.branch, bind_ok, lblD3, lblD4, ↓reduceIte] at hy + obtain ⟨s3, hs3, hy⟩ := bind_eq_ok hy + have K3 := (KeepA.refl ha2 hrun).sts hK (Or.inl rfl) hs3 + have k3 : ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem s3 b = L.mem s b := fun b hb hsl => + (K3.mem hb fun ht' => nT b ht' hsl).trans (kP b hb hsl) + have hc3 : obj.Slot.cap st dest s3 = ok (.Frame fp .Small rt false true false asid va) := by + rw [cap_agree hL hd (fun b hs hb => (k3 b hb (hS.wd hs)).trans (ag b hs hb).symm)]; exact hdc2 + obtain ⟨x1, a2, hu1, hes⟩ := del_runSt hc3 hA0 hy + rw [hu3 s3 hs3] at hu1 + have k1 : ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem x1 b = L.mem s b := fun b hb hsl => by + rcases unmapLeaf_st ar hu1 with ⟨rfl, -⟩ | ⟨rfl, -⟩ + · exact k3 b hb hsl + · rw [mem_wr_ne' hVe.1 hb (fun h' => ne (h' ▸ hsl))]; exact k3 b hb hsl + -- the reply + have Fx := kx.sched hK O' + obtain ⟨r, y2, a3⟩ := y + simp only at hy1 hz kx Fx hes + subst hy1 + obtain ⟨-, kz⟩ := invRep_ok_ku8 (K := { K.toKLayout with user := ∅ }) Fx ⟨fun _ _ hu' => hu'.2, + fun _ _ _ hu' => hu'.2, fun hu' => hu'.2⟩ (KeepU.of_keepA (KeepA.refl kx.2.1 kx.2.2)) hz + exact Or.inr ⟨cp, cr, cg, cgb, dest, x1, y2, a2, a3, hcs, hlk, hd, k1, hes, fun b hb hsl => + kz.mem hb (fun ht' => nT b ht' hsl) (fun hu' => hu'.2)⟩ + +/-- (P2-Sync gen 4) **The Delete-with-PTE decode block's slot words** (module docstring; gen 32's `del_blkMdb` proof +with the conclusion stated on the slot words). -/ +theorem del_blkSl (hA : P0.ArchTotal ar) (hS : SlotLayK K.toKLayout) {s : S} {a : A} + (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) (hW : IoWr st (NSl K.toKLayout) K.heap.scBits s) + {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} + {y : (core.result.Result Unit error.Exc) × S × A} {z : S × A × core.result.Result Unit error.Exc} + (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) + (hz : invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z) : DelSl st ar K.toKLayout p s z.1 := + del_blkSlL hA hS hE.toL hW emi C hcap hy hz + +/-- (P2-Sync gen 5) **`FinOK` of the Delete-with-PTE decode block on the store guarded to the non-slot words** at a +`PrePAL` state (`del_blkMdb`'s first half). -/ +theorem del_blkFinL (hA : P0.ArchTotal ar) {s : S} {a : A} + (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) (hW : IoWr st (NSl K.toKLayout) K.heap.scBits s) + {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} + {y : (core.result.Result Unit error.Exc) × S × A} {z : S × A × core.result.Result Unit error.Exc} + (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) + (hz : invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z) : + z.2.2 = .Ok () ∧ FinOK (L := L) (guard st (IoRSR st K.toKLayout Locks s) (NSl K.toKLayout)) + (IoRSR st K.toKLayout Locks s) (NSl K.toKLayout) ar s z.1 z.2.1 := by + have hq := hE.2.1 + have PE := hq.1.1.1 + have nSt : NSl K.toKLayout (stA (L.curW s)) := ⟨(PE.stw (Locks := Locks)).2.1, + (io_narNS PE).curW _ (by rw [st_val]; omega) (by rw [st_val]; omega) (by rw [st_val]; omega)⟩ + have hy' := hy + rw [(hq.1.2.2.1 (L.curW s) info (cur_thread_ok s) emi).1] at hy' + obtain ⟨Pp, Tb, Lv, m1, hV, h0T, hc, hag⟩ := hE.agT + have E := delEntP_ofL hE + have hsK : p.slot ∈ K.toKLayout.slots := by + obtain ⟨-, -, -, -, -, -, -, -, -, -, -, -, h, -⟩ := del_coreP hq; exact h + by_cases hlen : length < 2#u64 + · obtain ⟨cp, cr, cg, cgb, -, -, -, -, -, -, -, hcs, -⟩ := E + exact truncFinR (gS := guard st (IoRSR st K.toKLayout Locks s) (NSl K.toKLayout)) hA PE hW (SWr.of_W nSt) + hcs hsK hE.2.2.2.2.2.2.2 C hcap hlen hy' hz + · exact delFinR (gS := guard st (IoRSR st K.toKLayout Locks s) (NSl K.toKLayout)) hA PE hW nSt E hsK hlen + (fun _ _ _ asid va _ _ _ => ⟨spe_ne0T hV hag h0T asid va, vsRdT hV hag hc asid va⟩) + (fun _ _ _ he => leafVsWT hV hag he) C hcap hy' hz + +/-- **The Delete-with-PTE decode block keeps `MdbS`** (module docstring), with `FinOK` on the store guarded to the +non-slot words (P2-Sync gen 4: the slot words from `del_blkSl`, then `cdt.empty_slot.mdbO`). -/ +theorem del_blkMdb (hA : P0.ArchTotal ar) {T0 Ω0 Sg0 : Set U64} (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) + (hS : SlotLayK K.toKLayout) {s : S} {a : A} + (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) (hW : IoWr st (NSl K.toKLayout) K.heap.scBits s) + {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} + {y : (core.result.Result Unit error.Exc) × S × A} {z : S × A × core.result.Result Unit error.Exc} + (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) + (hz : invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z) : + (z.2.2 = .Ok () ∧ FinOK (L := L) (guard st (IoRSR st K.toKLayout Locks s) (NSl K.toKLayout)) + (IoRSR st K.toKLayout Locks s) (NSl K.toKLayout) ar s z.1 z.2.1) ∧ + (W7.R.ST.MdbS st K.toKLayout.slots s → W7.R.ST.MdbS st K.toKLayout.slots z.1) := by + refine ⟨?_, fun hM => ?_⟩ + · exact del_blkFinL hA hE.toL hW emi C hcap hy hz + rcases del_blkSl hA hS hE hW emi C hcap hy hz with k | ⟨cp, cr, cg, cgb, dest, x1, y1, a2, a3, -, -, hd, k1, hes, k2⟩ + · exact W7.R.ST.MdbS.keep hM (osKeep_of_words hS k) + obtain ⟨G, hM1, hRO, hKd, hN, hCl⟩ := W7.R.ST.MdbS.keep hM (osKeep_of_words hS k1) + have hMy : W7.R.ST.MdbS st K.toKLayout.slots y1 := post_of_ok (W7.R.ST.cdt.empty_slot.mdbO + (cl := .Null) (Pw := fun _ => False) hO hM1 hRO hKd hN hCl hd (fun _ h' => h'.elim) + fun s7 a7 _ => by rw [W7.R.ST.pcd_null]; exact (WP.spec_ok _).2 (WritesIn.refl _ _)) hes + exact W7.R.ST.MdbS.keep hMy (osKeep_of_words hS k2) + +/-- (P2-Sync gen 4) **An accepted Delete-with-PTE run's slot words** are those of its decode block's end +(`inv_accSl` at `del_blkSlL`; P2-Sync gen 5: at `PrePAL`). -/ +theorem del_accSlL (hA : P0.ArchTotal ar) + (hS : SlotLayK K.toKLayout) {number : I64} {s s' : S} {a a' : A} (hE : PrePAL st Cs Ω Sg Ac Locks p K μ s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + ∃ z1, DelSl st ar K.toKLayout p s z1 ∧ ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem s' b = L.mem z1 b := + inv_accSl hA hE.2.1.1.1.1 (fun o h1 h2 => (hE.2.1.1.1.1.ndR (Locks := Locks) o h1 h2).1) + (fun hW _ _ _ _ _ _ _ _ _ _ emi C hcap hy hz => ⟨del_blkFinL hA hE hW emi C hcap hy hz, + del_blkSlL hA hS hE hW emi C hcap hy hz⟩) h + +/-- (P2-Sync gen 4) **An accepted Delete-with-PTE run's slot words** are those of its decode block's end +(`inv_accSl` at `del_blkSl`). -/ +theorem del_accSl (hA : P0.ArchTotal ar) {T0 Ω0 Sg0 : Set U64} (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) + (hS : SlotLayK K.toKLayout) {number : I64} {s s' : S} {a a' : A} (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + ∃ z1, DelSl st ar K.toKLayout p s z1 ∧ ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem s' b = L.mem z1 b := + del_accSlL hA hS hE.toL h + +/-- **An accepted Delete-with-PTE run keeps `MdbS`** (`inv_accMdbG` at `del_blkMdb`). -/ +theorem del_accMdb (hA : P0.ArchTotal ar) {T0 Ω0 Sg0 : Set U64} (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) + (hS : SlotLayK K.toKLayout) {number : I64} {s s' : S} {a a' : A} (hE : PrePA st Cs Ω Sg Ac Locks p K μ s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) + (hM : W7.R.ST.MdbS st K.toKLayout.slots s) : W7.R.ST.MdbS st K.toKLayout.slots s' := + inv_accMdbG hA hS hE.2.1.1.1.1 (fun o h1 h2 => (hE.2.1.1.1.1.ndR (Locks := Locks) o h1 h2).1) + (fun hW _ _ _ _ _ _ _ _ _ _ emi C hcap hy hz => del_blkMdb hA hO hS hE hW emi C hcap hy hz) h hM + +end del + +/-! ## The slot-set facts at an epoch -/ + +section ep +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- (gen 32) **P1's slot-set facts at an epoch layout** (`EntryInvA.os` at the epoch's typing state): the state-free +`OSOK` that `pmSlotP_mdb` / `delSlotP_mdb` take. -/ +theorem EpochA.osok {K : OwnMapV.KLayoutV} {e : U64 → U64} (h : EpochA st K e) : + ∃ T Ω Sg : Set U64, W7.R.ST.OSOK T Ω Sg K.toKLayout.slots := by + obtain ⟨_, _, _, _, _, _, _, _, _, _, _, _, _, rfl, E, -⟩ := h + exact ⟨_, _, _, E.os⟩ + +theorem EpochAP.osok {K : KLayoutP} {e : U64 → U64} (h : EpochAP st K e) : + ∃ T Ω Sg : Set U64, W7.R.ST.OSOK T Ω Sg K.toKLayout.slots := + EpochA.osok h.1 + +end ep + +/-! ## The Delete-with-PTE slot -/ + +section slot +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The Delete-with-PTE slot of `fl4LocPB` keeps `MdbS`** (module docstring; `hO` is P1's state-free slot-set +fact). -/ +theorem delSlotP_mdb (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) + {T0 Ω0 Sg0 : Set U64} (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) : + MdbKeepP K.toKLayout.slots + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i)))))))))) K m hK) := by + show MdbKeepP K.toKLayout.slots (delSlotP st ar hA hm i K m hK) + unfold delSlotP + by_cases h : DelPremP st i K m + · rw [dif_pos h] + refine LocalPath.mdbOfAcc _ K.toKLayout hS (inv_noZero i.number i.p) ?_ ?_ + · intro s x hp hx + have hp' : PrePA st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s := hp + have N := io_narNS hp'.2.1.1.1.1 + rcases hx with rfl | rfl | rfl + · exact (N.reg hp'.2.1.1.1.1.tA (by simp [layout.reg.FAULT_IP])).2 + · exact (N.nd _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])).2 + · exact (N.nd _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])).2 + · intro s a r hp hr hac hM + obtain ⟨b, s', a'⟩ := r + change b = true at hac + subst hac + change syscall.handle_invocation_local st ar s a i.number i.p = ok (true, s', a') at hr + change W7.R.ST.MdbS st _ s at hM + change W7.R.ST.MdbS st _ s' + exact del_accMdb hA hO hS hp hr hM + · rw [dif_neg h] + intro s a r hp + exact hp.elim + +end slot + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/MdbKeepPmA.lean b/proofs/Fl4ProofsA64/P2/MdbKeepPmA.lean new file mode 100644 index 000000000..0a9fcf337 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbKeepPmA.lean @@ -0,0 +1,457 @@ +/- +# P2 (AArch64 integration, P2-A64X gen 32): **the PageMap slot of `fl4LocPB` keeps P1's `MdbS`** + +Architect 15:13Z, item 1. The PageMap slot (`pmSlotP`, `pm_localPathA`) writes one capability slot: the decode's +in-place `set_cap` of the invoked slot `p.slot`, a mapped small `Frame` cap over the slot's `Frame` cap (same object, +same kind; `CapRt.dec_frame_any`: the encoded cap decodes to a `Frame` cap). An accepted run is split as + +* the prefix (`prefix_writes`, `IoPre.slot_keep`: no slot word); +* the decode (`dpm_runA3`, `P2/InvPmRunA.lean`): an error leaves the state alone; `Ok` is `set_thread_state` (the + caller's `IoTW` words), **`set_cap` at `p.slot`** (two words, `set_cap_eq`) and `install_page` (the leaf `e`, a PTE + word: region `.none`, `RegionOKP.none`); +* the reply (`invRep`): `reply_error_ku8` (the caller's `IoTW` words and user words) or `invRep_ok_ku8` at the layout + with no user word (`IoTW` words only), `SchedOK` at the decode's end from `pmDecKeep`; +* `after_invocation` of `Ok ()` (no step) and `finish`, run on the store guarded to the non-slot words (`NSl`): + `pmFinG`'s `FinOK` there, `syscall.finish.guard_resume`, then `guard_frame` of `R.syscall.finish`. + +So every slot word but `p.slot`'s two cap words is kept, and those two hold the encoded `Frame` cap: the end state +agrees on the slot words with `s` after the two-word write (**`mem_wr2_congr`**). P1's five in-place `set_cap` +lemmas (`MdbLk` / `RngOneA64` / `KindA` / `NullLk` / `ClsA.set_cap`; Frame → Frame: non-null on both sides, neither a +range key, `isFrA`) give `MdbS` after the write, and `MdbS.keep` moves it to the end state. A declined run writes +the `IoDS` words (`FAULT_IP`, two node words: `io_narNS`), no slot word (**`LocalPath.mdbOfAcc`**). + +* **`invTail_rest`**: the accepted tail after the plan check, step by step (`invTail_inv` stops at the check). +* **`inv_accMdbG`**: an accepted `handle_invocation_local` run keeps `MdbS` given its decode block (`FinOK` on the + store guarded to `NSl`, and `MdbS` from the entry to the reply's end): the prefix, `after_invocation` and `finish` + are shared with the Delete-with-PTE slot (`P2/MdbKeepDelA.lean`). +* **`pm_blkMdb`**: the PageMap decode block; **`pm_accMdb`**: an accepted PageMap run keeps `MdbS`; + **`pmSlotP_mdb`**: the slot of `fl4LocPB` (`MdbKeepP`, so `MdbKeepP.toE` gives the `MdbKeepPE` form). + +P2-Sync gen 4 (Architect 17:41Z, proofs only, every statement above unchanged): **`inv_accSl`** (the accepted run's +slot words are its decode block's end's, for any predicate `Φ` of that end; `inv_accMdbG` is its `MdbS` instance), +**`PmSl`** / **`pm_blkSl`** (the PageMap block's slot words: the entry's, or the entry's after the two-word `Frame` +write; `pm_blkMdb` now derives `MdbS` from it) and **`pm_accSl`**, for the edit form (`P2/MdbWirePDA.lean`). + +Lemma hypotheses (no premise of any top-level theorem): `hS : SlotLayK` (from `EpochAP.slotLay`, as every slot) and, +as the ruling allows, the **state-free `hO : OSOK T Ω Sg K.slots`** (P1's slot-set facts the `set_cap` lemmas read: +alignment, fit, disjointness, no slot at 0). No new [J], [P] or [W]; `EpochAP`, `ExecPA`, `fl4LocPB` and +`serializable_fl4_localVP` are unchanged. +-/ +import Fl4ProofsA64.P2.MdbKeepA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA Fl4ProofsA64.P2.E2EP +open Fl4ProofsA64.P2.OwnMap (KLayout region fld own InW RegionOK) +open Fl4ProofsA64.P2.OwnMapV (KLayoutV) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP UserOKP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA) +open Fl4Proofs.P2.Serial (Owner ClsOwner) + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section +open Classical + +/-! ## Generic: declined runs off the slots, the two-word write -/ + +section gen +variable {own : (U64 → U64) → U64 → Owner U64 U64} {cls : U64 → U64 → U64} {ko : U64 → ClsOwner U64} + {User : U64 → Prop} + +/-- **`MdbS` along a path from its accepted runs**: a declined run writes the `DS` words (`PathObl.dec`), no slot +word, so it keeps every slot's words (`MdbS.keep`). -/ +theorem LocalPath.mdbOfAcc (P : E2E.LocalPath own cls ko User) (K : KLayout) (hS : SlotLayK K) (hz : NoZero P.p) + (hD : ∀ s x, P.Pre s → P.DS s x → ¬ SlotWd K x) + (hA : ∀ s a r, P.Pre s → P.F P.ar P.st s a = ok r → P.Acc r.1 → + @W7.R.ST.MdbS P.S P.st P.L K.slots s → @W7.R.ST.MdbS P.S P.st P.L K.slots r.2.1) : + MdbKeepP K.slots P := by + letI := P.L + intro s a r hp h hM + by_cases hac : P.Acc r.1 + · exact hA s a r hp h hac hM + have hg := P.obl.dec hp h hac + rw [P.isProg _ (Narrow.guard _ _ _) s (AltOf.pre hp) a] at hg + refine W7.R.ST.MdbS.keep hM (osKeep_of_words hS fun b hb hsl => ?_) + exact guard_frame (fun x hx => P.obl.aligned s x hp (Or.inr hx)) hz hg b hb fun hd => hD s b hp hd hsl + +end gen + +section wr2 +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **Agreement survives the same two-word write** (at `x0` and `x0 + 8`). -/ +theorem mem_wr2_congr {s x : S} {x0 w0 w1 b : U64} (h0 : Aligned x0) (h8 : Aligned (wa x0 8#u64)) + (hb : Aligned b) (e : L.mem x b = L.mem s b) : + L.mem (L.wr (L.wr x x0 w0) (wa x0 8#u64) w1) b = L.mem (L.wr (L.wr s x0 w0) (wa x0 8#u64) w1) b := by + by_cases e8 : wa x0 8#u64 = b + · subst e8; rw [L.mem_wr_same h8, L.mem_wr_same h8] + rw [mem_wr_ne' h8 hb e8, mem_wr_ne' h8 hb e8] + by_cases e0 : x0 = b + · subst e0; rw [L.mem_wr_same h0, L.mem_wr_same h0] + rw [mem_wr_ne' h0 hb e0, mem_wr_ne' h0 hb e0, e] + +/-- **`MdbS` after the in-place write of a `Frame` cap over a `Frame` cap** (P1's five `set_cap` lemmas). -/ +theorem MdbS.set_frame {OS T0 Ω0 Sg0 : Set U64} (hO : W7.R.ST.OSOK T0 Ω0 Sg0 OS) {s : S} (hM : W7.R.ST.MdbS st OS s) + {x0 : U64} (hx : x0 ∈ OS) {fp rt asid va : U64} {fz : capability.FrameSize} {fd fm fe : Bool} + (hc : W7.R.ST.capA st s x0 = .Frame fp fz rt fd fm fe asid va) (c : capability.Cap) + (hcf : ∃ p sz r d m e a v, c = .Frame p sz r d m e a v) : + W7.R.ST.MdbS st OS + (L.wr (L.wr s x0 ⟨(CapRt.encodeF c).1⟩) (wa x0 8#u64) ⟨(CapRt.encodeF c).2⟩) := by + obtain ⟨p, sz, r, d, m, e, a, v, rfl⟩ := hcf + obtain ⟨p', sz', r', d', m', e', a', v', hdf⟩ := CapRt.dec_frame_any p sz r d m e a v + have hd : capability.Cap.decode ⟨(CapRt.encodeF (.Frame p sz r d m e a v)).1⟩ + ⟨(CapRt.encodeF (.Frame p sz r d m e a v)).2⟩ = ok (.Frame p' sz' r' d' m' e' a' v') := by + rw [CapRt.decode_eq]; exact congrArg ok hdf + have nk : ∀ {c : capability.Cap}, W7.R.ST.isFrA c = true → ¬ W7.R.ST.RngKeyA c := fun h k => by + rw [k.2.2] at h; cases h + have k0 : ¬ W7.R.ST.RngKeyA (W7.R.ST.capA st s x0) := by rw [hc]; exact nk rfl + have kk : W7.R.ST.RngKeyA (.Frame p' sz' r' d' m' e' a' v') ↔ W7.R.ST.RngKeyA (W7.R.ST.capA st s x0) := + ⟨fun k => absurd k (nk rfl), fun k => absurd k k0⟩ + obtain ⟨G, hM', hRO, hKd, hN, hC⟩ := hM + have hG : ∀ y ∈ G.flatten, y ∈ OS := fun y hy => ((hM'.members y).1 hy).1 + have hnc : (capability.Cap.Frame p' sz' r' d' m' e' a' v') ≠ .Null := fun h => by cases h + have hc0 : W7.R.ST.capA st s x0 ≠ .Null := by rw [hc]; exact fun h => by cases h + exact ⟨G, hM'.set_cap hO hx hd hnc hc0, hRO.set_cap hO hG hx hd kk (fun k => absurd k k0), + hKd.set_cap hO hx hd kk (fun k => absurd k k0), hN.set_cap hO hx hd hnc, hC.set_cap hO hx hd kk + (fun k => absurd k k0)⟩ + +end wr2 + +/-! ## The accepted PageMap run -/ + +section pm +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {μ : U64 → U64} + +/-- **The accepted tail after the plan check** (the steps `invTail_inv` leaves inside the tail). -/ +theorem invTail_rest {s s' : S} {a a' : A} {t nx : U64} {info : msginfo.MessageInfo} {s1 s2 : S} {a1 : A} + (hb : localinv.is_local_label info.label = ok true) (C : InvChk st ar s a t p nx s1 s2 a1) + (h : invTail st ar s a t info p = ok (true, s', a')) : + ∃ buf length cap y z x s5 a4, msg.lookup_ipc_buffer st s2 false t = ok buf ∧ + (if info.length > msg.N_MSG_REGISTERS then (if buf = 0#u64 then ok msg.N_MSG_REGISTERS else ok info.length) + else ok info.length) = ok length ∧ + obj.Slot.cap st p.slot s2 = ok cap ∧ + syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y ∧ + invRep st t y.2.1 y.2.2 y.1 = ok z ∧ + syscall.after_invocation st ar z.1 z.2.1 z.2.2 = ok (x, s5, a4) ∧ syscall.finish st ar s5 a4 = ok (s', a') := by + obtain ⟨hnx, hs1, hu, hi3, hi4, hb2, hb3⟩ := C + unfold invTail at h + simp only [hb, hnx, lift, bind_ok, hs1, hu, uncurry_apply_pair, ↓reduceIte] at h + rw [eq_of_spec_fd (sched.cur_sc.spec (L := L) s2), bind_ok, hi3, bind_ok] at h + simp only [bne_self_eq_false, Bool.false_eq_true, ↓reduceIte, hi4, bind_ok] at h + rw [io_consumed_ok s2, bind_ok] at h + obtain ⟨b1, -, h⟩ := bind_eq_ok h + cases b1 + · simp at h + simp only [↓reduceIte, hb2, hb3, bind_ok] at h + obtain ⟨buf, ebuf, h⟩ := bind_eq_ok h + obtain ⟨length, elen, h⟩ := bind_eq_ok h + obtain ⟨cap, ecap, h⟩ := bind_eq_ok h + obtain ⟨⟨r, s3, a2⟩, ey, h⟩ := bind_eq_ok h + try simp only [uncurry_apply_pair] at h + try dsimp only at h + obtain ⟨⟨s4, a3, r1⟩, ez, h⟩ := bind_eq_ok h + try simp only [uncurry_apply_pair] at h + try dsimp only at h + obtain ⟨⟨x, s5, a4⟩, ex, h⟩ := bind_eq_ok h + try simp only [uncurry_apply_pair] at h + try dsimp only at h + obtain ⟨⟨s6, a5⟩, ef, h⟩ := bind_eq_ok h + try simp only [uncurry_apply_pair] at h + try dsimp only at h + have e := ok_inj_ff h + simp only [Prod.mk.injEq, true_and] at e + obtain ⟨rfl, rfl⟩ := e + exact ⟨buf, length, cap, (r, s3, a2), (s4, a3, r1), x, s5, a4, ebuf, elen, ecap, ey, ez, ex, ef⟩ + +/-- A user word is no slot word (`IoPre.region_kernel`). -/ +theorem IoPre.user_nsl {Kl : KLayout} {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} {s : S} + (P : IoPre st Kl T Cs Ω H Sg Rp Scs Nt Z Ac s) {b : U64} (hb : b ∈ Kl.user) : ¬ SlotWd Kl b := + fun ⟨_, hq⟩ => P.region_kernel (Or.inl (by rw [hq]; exact fun h => by cases h)) hb + +/-- (P2-Sync gen 4) **An accepted invocation run ends with its decode block's slot words** (module docstring): +the prefix's current SC gives the write set `NSl` (`IoWr`, `io_narNS`); the block `hblk` (the decode and the reply on +`st`) gives `FinOK` on the store guarded to `NSl` and `Φ` at the reply's end; `after_invocation` of `Ok ()` takes no +step and `finish` (`guard_resume`, `R.syscall.finish`) keeps every slot word. -/ +theorem inv_accSl (hA : P0.ArchTotal ar) {Kl : KLayout} {T Cs' Ω' Sg' Rp Scs Nt Ac' : Set U64} + {H : Heap} {Z : U64 → Nat} {number : I64} {s s' : S} {a a' : A} + (Pb : IoPre st Kl T Cs' Ω' H Sg' Rp Scs Nt Z Ac' s) {Rf : U64 → Prop} + (hRn : ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Rf (L.nodeAt s (L.cpu s) o)) {Φ : S → Prop} + (hblk : IoWr st (NSl Kl) Z s → ∀ info nx s1 s2 a1 buf length cap y + (z : S × A × core.result.Result Unit error.Exc), msg.msg_info st s (L.curW s) = ok info → + InvChk st ar s a (L.curW s) p nx s1 s2 a1 → obj.Slot.cap st p.slot s2 = ok cap → + syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y → + invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z → + (z.2.2 = .Ok () ∧ FinOK (L := L) (guard st Rf (NSl Kl)) Rf (NSl Kl) ar s z.1 z.2.1) ∧ Φ z.1) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + ∃ z1, Φ z1 ∧ ∀ b, Aligned b → SlotWd Kl b → L.mem s' b = L.mem z1 b := by + obtain ⟨info, emi, -, -, ht⟩ := handle_inv_inv h + obtain ⟨nx, s1, s2, a1, hb, hnx, hs1, hu, hi3, hi4, -, hb2, hb3⟩ := invTail_inv ht + have C : InvChk st ar s a (L.curW s) p nx s1 s2 a1 := ⟨hnx, hs1, hu, hi3, hi4, hb2, hb3⟩ + obtain ⟨buf, length, cap, y, z, x, s5, a4, -, -, hcap, hy, hz, hx, hf⟩ := invTail_rest hb C ht + have w0 := prefix_writes ar hA Pb.tp Pb.np Pb.cT hs1 hu + have hsc0 : L.mem s (scA (L.curW s)) ≠ 0#u64 := by + have e1 : L.mem s2 (scA (L.curW s)) = L.mem s (scA (L.curW s)) := + w0.2.2 _ (field_aligned Pb.tA (by simp) (by simp)) (preW_tcb Pb.tA Pb.np Pb.cT (K := 3464#u64) (by simp) (by simp)) + rw [← e1]; exact (here_facts hb2).sc + have hW : IoWr st (NSl Kl) Z s := (Pb.ioWrN (Locks := fun _ _ => True) (io_narNS Pb) hsc0).mono fun _ hb => ⟨hb.1.1, hb.2⟩ + obtain ⟨⟨hz2, F⟩, hMz⟩ := hblk hW info nx s1 s2 a1 buf length cap y z emi C hcap hy hz + rw [hz2, syscall.after_invocation.ok_eq] at hx + have ex := ok_inj_ff hx + simp only [Prod.mk.injEq] at ex + obtain ⟨-, rfl, rfl⟩ := ex + have hfg : syscall.finish (guard st Rf (NSl Kl)) ar z.1 z.2.1 = ok (s', a') := by + rw [syscall.finish.guard_resume ar z.1 z.2.1 (LT.nd_at (fun o h1 h2 => ⟨hRn o h1 h2, hW.nd o h1 h2⟩) F.k F.c) + F.nd F.td F.rs F.ha F.hsw F.hfr F.hact F.hvc] + exact hf + have hi : interp (guard st Rf (NSl Kl)) ar R.syscall.finish z.1 z.2.1 = ok ((), s', a') := by + rw [R.syscall.finish_eq, hfg, bind_tc_ok] + exact ⟨z.1, hMz, fun b hb hsl => guard_frame (fun _ hx => hx.1) R.syscall.finish_nz hi b hb fun hx => hx.2 hsl⟩ + +/-- (gen 32) **An accepted invocation run keeps `MdbS` from its decode block** (module docstring; P2-Sync gen 4: +`inv_accSl` at `Φ := (MdbS s → MdbS ·)`). -/ +theorem inv_accMdbG (hA : P0.ArchTotal ar) {Kl : KLayout} (hS : SlotLayK Kl) {T Cs' Ω' Sg' Rp Scs Nt Ac' : Set U64} + {H : Heap} {Z : U64 → Nat} {number : I64} {s s' : S} {a a' : A} + (Pb : IoPre st Kl T Cs' Ω' H Sg' Rp Scs Nt Z Ac' s) {Rf : U64 → Prop} + (hRn : ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → Rf (L.nodeAt s (L.cpu s) o)) + (hblk : IoWr st (NSl Kl) Z s → ∀ info nx s1 s2 a1 buf length cap y + (z : S × A × core.result.Result Unit error.Exc), msg.msg_info st s (L.curW s) = ok info → + InvChk st ar s a (L.curW s) p nx s1 s2 a1 → obj.Slot.cap st p.slot s2 = ok cap → + syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y → + invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z → + (z.2.2 = .Ok () ∧ FinOK (L := L) (guard st Rf (NSl Kl)) Rf (NSl Kl) ar s z.1 z.2.1) ∧ + (W7.R.ST.MdbS st Kl.slots s → W7.R.ST.MdbS st Kl.slots z.1)) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) + (hM : W7.R.ST.MdbS st Kl.slots s) : W7.R.ST.MdbS st Kl.slots s' := by + obtain ⟨z1, hz1, k⟩ := inv_accSl (Φ := fun z1 => W7.R.ST.MdbS st Kl.slots s → W7.R.ST.MdbS st Kl.slots z1) + hA Pb hRn hblk h + exact W7.R.ST.MdbS.keep (hz1 hM) (osKeep_of_words hS k) + +variable (st) in +/-- (P2-Sync gen 4) **The slot words of a PageMap decode block's end `z`**: those of the entry `s`, or those of `s` +after the in-place write of a `Frame` cap at `slot` (`MdbS.set_frame`'s two words). -/ +def PmSl (Kl : KLayout) (slot : U64) (s z : S) : Prop := + (∀ b, Aligned b → SlotWd Kl b → L.mem z b = L.mem s b) ∨ + ∃ c : capability.Cap, (∃ p' sz r d m e a v, c = .Frame p' sz r d m e a v) ∧ + ∀ b, Aligned b → SlotWd Kl b → + L.mem z b = L.mem (L.wr (L.wr s slot ⟨(CapRt.encodeF c).1⟩) (wa slot 8#u64) ⟨(CapRt.encodeF c).2⟩) b + +/-- (P2-Sync gen 4) **The PageMap decode block's slot words** (module docstring; gen 32's `pm_blkMdb` proof with the +conclusion stated on the slot words). -/ +theorem pm_blkSl (hA : P0.ArchTotal ar) (hKP : RegionOKP K) {T0 Ω0 Sg0 : Set U64} + (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) (hS : SlotLayK K.toKLayout) {s : S} {a : A} + (hE : PmPreG st Cs Ω Sg Ac Locks p K μ s) (hW : IoWr st (NSl K.toKLayout) K.heap.scBits s) + {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} + {y : (core.result.Result Unit error.Exc) × S × A} {z : S × A × core.result.Result Unit error.Exc} + (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) + (hz : invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z) : PmSl st K.toKLayout p.slot s z.1 := by + obtain ⟨-, hs1, hu, hi3, -, hb2, -⟩ := id C + obtain ⟨fp, rt, asid, va, mp, x0, root, rasid, e, hcs, hcs2, hx0, -, hcx2, hcv2, hpe2, hpt, -, hRS, -, -, hsl, + -, -⟩ := pm_s2A hA hE C + have hq := hE.1.2.1 + have hG := hE.1.2.2 + have Pb := hq.1.1.1 + have P := Pb.ptU hG + have N := io_narNS Pb + have w0 := prefix_writes ar hA Pb.tp Pb.np Pb.cT hs1 hu + have Hf := here_facts hb2 + -- the words a run writes that are no slot words + have nR : ∀ r : U64, r.val < 384 → ¬ SlotWd K.toKLayout (regA (L.curW s) r) := fun r hr => (N.reg Pb.tA hr).2 + have nSt : NSl K.toKLayout (stA (L.curW s)) := + ⟨(Pb.stw (Locks := Locks)).2.1, N.curW _ (by rw [st_val]; omega) (by rw [st_val]; omega) (by rw [st_val]; omega)⟩ + have nT : ∀ b, IoTW (L.curW s) b → ¬ SlotWd K.toKLayout b := by + rintro b (⟨r, hr, rfl⟩ | rfl) + · exact nR r hr + · exact nSt.2 + have ne : ¬ SlotWd K.toKLayout e := nsl_of_region (hKP.none e hpt) (fun _ h => by cases h) + have nU : ∀ b, UserW K.ptU b → ¬ SlotWd K.toKLayout b := fun b hb => by + rcases hb.2 with hu' | hp' + · exact IoPre.user_nsl Pb hu' + · exact nsl_of_region (hKP.none b hp') (fun _ h => by cases h) + have Ent := P.entR (Locks := Locks) hW + have I := IoS2.of_ent hA Ent hs1 hu Hf.sc + have hct : L.curW s2 = L.curW s := I.cur + have ha2 : L.actionW s2 = sched.ACTION_RESUME_CURRENT := by + rw [eq_of_spec_fd (sched.action.spec (L := L) s2)] at hi3; exact ok_inj_ff hi3 + have hrun : L.mem s2 (stA (L.curW s)) = sched.TS_RESTART ∨ L.mem s2 (stA (L.curW s)) = sched.TS_RUNNING := by + rw [I.st]; exact Ent.run + have hK0 := SchedOK.of_sw I.F (by rw [hct]; exact Hf.sc) (by rw [hct]; exact Hf.nr) + rw [hct] at hK0 + have hK : SchedOK (L := L) s2 (L.curW s) := hK0 + -- the prefix + have kP : ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem s2 b = L.mem s b := fun b hb ⟨_, hq'⟩ => + let ⟨h1, h2⟩ := OwnMap.region_slot_inv hq' + Pb.slot_keep w0 h1 h2 hb + -- the decode + have hcap' : cap = .Frame fp .Small rt false mp false asid va := by + rw [hcs2] at hcap; exact (ok_inj_ff hcap).symm + subst hcap' + obtain ⟨-, -, -, -, -, -, -, hlab, -, -⟩ := pm_core hq + rw [(hlab info emi).1, decInv_eq_Pm] at hy + unfold decPmG at hy + have hc2 : sched.cur_thread st s2 = ok (L.curW s2) := cur_thread_ok s2 + simp only [hc2, bind_ok, ↓reduceIte] at hy + have ha0 : msg.syscall_arg st s2 (L.curW s2) buf 0#u64 = ok (L.mem s2 (regA (L.curW s2) layout.reg.X2)) := by + rw [sarg0_reg, reg_ok] + have ha1 : msg.syscall_arg st s2 (L.curW s2) buf 1#u64 = ok (L.mem s2 (regA (L.curW s2) layout.reg.X3)) := by + rw [sarg1_reg, reg_ok] + have ha2' : msg.syscall_arg st s2 (L.curW s2) buf 2#u64 = ok (L.mem s2 (regA (L.curW s2) layout.reg.X4)) := by + rw [sarg2_reg, reg_ok] + have hpe : vspace.lookup_pt_slot st s2 root (L.mem s2 (regA (L.curW s2) layout.reg.X2)) = + ok { slot := e, shift := vspace.PAGE_BITS } := by rw [hct]; exact hpe2 + have hout := post_of_ok (dpm_runA3 ar hA hc2 hx0 ha0 ha1 ha2' hcx2 hcv2 hpe) hy + rw [hct] at hout + -- the user words of the reply + have tk : ∀ k : U64, k.val < 4096 → k.val % 8 = 0 → k.val ≠ 528 → + L.mem s2 (wa (L.curW s) k) = L.mem s (wa (L.curW s) k) := + fun k h1 h2 h3 => w0.2.2 _ (field_aligned Pb.tA h1 h2) (preW_tcb Pb.tA Pb.np Pb.cT h1 h3) + have hnd : ∀ o : U64, o.val < 4288 → ¬ UserW (KSU8 K.ptU) (L.nodeAt s2 (L.cpu s2) o) := fun o ho => by + rw [I.cp, nodeAt_of_ksb I.k]; exact not_usu8 (P.node_notUser ho) (P.node_notSlot ho) + have htf : ∀ k : U64, 224 ≤ k.val → k.val < 4096 → ¬ UserW (KSU8 K.ptU) (wa (L.curW s) k) := fun k h1 h2 => + not_usu8 (P.tcb_notUser (P.hT _ P.cT) h1 h2) (P.tcb_notSlot (P.hT _ P.cT) h1 h2) + have hmx : ¬ UserW (KSU8 K.ptU) (maxA (L.mem s2 (scA (L.curW s)))) := by + rw [← hct, I.F.i] + exact not_usu8 (P.sc_notUser (P.hSc _ P.cscS) I.F.q.2.2.2.1) (P.sc_notSlot (P.hSc _ P.cscS) I.F.q.2.2.2.1) + have O8 : UsrOK (L := L) (KSU8 K.ptU) s2 (L.curW s) := ⟨hnd, htf, hmx⟩ + -- the slot words along the run + rcases hout with ⟨hy2, -, se, hse, -⟩ | ⟨hok, x1, x2, cp, cz, cr, cd, cm, ce, ca, cv, e', r', as', va', f', at', + h1, h2, h3⟩ + · -- an error: the reply's error words + obtain ⟨r, y2, a2⟩ := y + simp only at hy2 hse hz + subst hse + rw [hy2] at hz + unfold invRep at hz + obtain ⟨q0, eq0, hz⟩ := bind_eq_ok hz + cases ok_inj_ff hz + obtain ⟨s6, es6, e6⟩ := bind_eq_ok eq0 + cases ok_inj_ff e6 + have O : UsrOK (L := L) K.ptU s2 (L.curW s) := ⟨fun o ho hu' => hnd o ho (usu_of_user8 hu'), + fun k h1 h2 hu' => htf k h1 h2 (usu_of_user8 hu'), fun hu' => hmx (usu_of_user8 hu')⟩ + have hlb : msg.lookup_ipc_buffer st s2 true (L.curW s) = msg.lookup_ipc_buffer st s true (L.curW s) := + lookup_buf_keep Pb.tA true tk + have kr := reply_error_ku8 hK O (KeepU.of_keepA (KeepA.refl ha2 hrun)) (fun b eb hne i hi => by + have hw := hE.2.2.2.2.2.2 b (by rw [← hlb]; exact eb) hne i hi + exact ⟨hw.1, Or.inl hw.2⟩) es6 + refine Or.inl fun b hb hsl => ?_ + rw [kr.mem hb (fun ht' => nT b ht' hsl) (fun hu' => nU b hu' hsl), kP b hb hsl] + · -- `Ok`: the restart, the cap's update, the leaf, the reply + have hL : Slot8 K.ptU := fun q hq' => ⟨P.lay.slot8 q hq', P.lay.slotf q hq'⟩ + have heU : UserW (KSU8 K.ptU) e := ⟨hRS.1, Or.inl (Or.inr hpt)⟩ + have kx := pmDecKeep ar hA hK O8 ha2 hrun hL hsl heU + (dpmOut_ofA2 ar (dpmOut_ofA3 ar (Or.inr ⟨hok, x1, x2, cp, cz, cr, cd, cm, ce, ca, cv, e', r', as', va', f', + at', h1, h2, h3⟩))) hok + have Fx := kx.sched hK O8 + obtain ⟨r, y2, a2⟩ := y + simp only at hok h3 hz kx Fx + subst hok + obtain ⟨-, kz⟩ := invRep_ok_ku8 (K := { K.toKLayout with user := ∅ }) Fx ⟨fun _ _ hu' => hu'.2, + fun _ _ _ hu' => hu'.2, fun hu' => hu'.2⟩ (KeepU.of_keepA (KeepA.refl kx.2.1 kx.2.2)) hz + have k1 := (KeepA.refl ha2 hrun).sts hK (Or.inl rfl) h1 + rw [W7.R.ST.set_cap_eq (CapRt.encode_eq _)] at h2 + cases ok_inj_ff h2 + obtain ⟨a'', ha''⟩ := post_of_ok (ip_st ar hA _ a1 _ e' r' as' va' f' at' false) h3 + have h0 := hO.al0 hsl + have h8 := hO.al hsl (k := 8#u64) (by simp) (by simp) + refine Or.inr ⟨.Frame cp cz cr cd cm ce ca cv, ⟨_, _, _, _, _, _, _, _, rfl⟩, fun b hb hsl => ?_⟩ + rw [kz.mem hb (fun ht' => nT b ht' hsl) (fun hu' => hu'.2)] + have hne : e ≠ b := fun h' => ne (h' ▸ hsl) + have hy2 : L.mem y2 b = L.mem (L.wr (L.wr x1 p.slot ⟨(CapRt.encodeF (.Frame cp cz cr cd cm ce ca cv)).1⟩) + (wa p.slot 8#u64) ⟨(CapRt.encodeF (.Frame cp cz cr cd cm ce ca cv)).2⟩) b := by + rcases ha'' with ha'' | ha'' <;> rw [Prod.mk.injEq] at ha'' <;> rw [ha''.1] + · exact mem_wr_ne' hRS.1 hb hne + · rw [mem_wr_ne' hRS.1 hb hne, mem_wr_ne' hRS.1 hb hne] + rw [hy2] + exact mem_wr2_congr h0 h8 hb ((k1.mem hb fun ht' => nT b ht' hsl).trans (kP b hb hsl)) + +/-- (gen 32) **The PageMap decode block keeps `MdbS`** (module docstring), with `FinOK` on the store guarded to the +non-slot words (P2-Sync gen 4: the slot words from `pm_blkSl`, then `MdbS.set_frame`). -/ +theorem pm_blkMdb (hA : P0.ArchTotal ar) (hKP : RegionOKP K) {T0 Ω0 Sg0 : Set U64} + (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) (hS : SlotLayK K.toKLayout) {s : S} {a : A} + (hE : PmPreG st Cs Ω Sg Ac Locks p K μ s) (hW : IoWr st (NSl K.toKLayout) K.heap.scBits s) + {info : msginfo.MessageInfo} {nx : U64} {s1 s2 : S} {a1 : A} {buf length : U64} {cap : capability.Cap} + {y : (core.result.Result Unit error.Exc) × S × A} {z : S × A × core.result.Result Unit error.Exc} + (emi : msg.msg_info st s (L.curW s) = ok info) (C : InvChk st ar s a (L.curW s) p nx s1 s2 a1) + (hcap : obj.Slot.cap st p.slot s2 = ok cap) + (hy : syscall.decode_invocation st ar s2 a1 info.label length p.slot cap true true true false buf p.extra = ok y) + (hz : invRep st (L.curW s) y.2.1 y.2.2 y.1 = ok z) : + (z.2.2 = .Ok () ∧ FinOK (L := L) (guard st (IoRSR st K.ptU Locks s) (NSl K.toKLayout)) (IoRSR st K.ptU Locks s) + (NSl K.toKLayout) ar s z.1 z.2.1) ∧ + (W7.R.ST.MdbS st K.toKLayout.slots s → W7.R.ST.MdbS st K.toKLayout.slots z.1) := by + refine ⟨?_, fun hM => ?_⟩ + · exact pmFinG (gS := guard st (IoRSR st K.ptU Locks s) (NSl K.toKLayout)) hA hE hW (SWr.of_W ⟨((hE.1.2.1.1.1.1).stw (Locks := Locks)).2.1, (io_narNS hE.1.2.1.1.1.1).curW _ + (by rw [st_val]; omega) (by rw [st_val]; omega) (by rw [st_val]; omega)⟩) hE.2.2.2.2.2.2 emi C hcap hy hz + obtain ⟨fp, rt, asid, va, mp, hcs, hsl, -, -, -⟩ := pm_core hE.1.2.1 + rcases pm_blkSl hA hKP hO hS hE hW emi C hcap hy hz with k | ⟨c, hcf, k⟩ + · exact W7.R.ST.MdbS.keep hM (osKeep_of_words hS k) + · exact W7.R.ST.MdbS.keep (MdbS.set_frame hO hM hsl + (ok_inj_ff ((W7.R.ST.capA_eq (st := st) s p.slot).symm.trans hcs)) c hcf) (osKeep_of_words hS k) + +/-- (P2-Sync gen 4) **An accepted PageMap run's slot words** are those of its decode block's end (`inv_accSl` at +`pm_blkSl`). -/ +theorem pm_accSl (hA : P0.ArchTotal ar) (hKP : RegionOKP K) {T0 Ω0 Sg0 : Set U64} + (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) (hS : SlotLayK K.toKLayout) {number : I64} {s s' : S} {a a' : A} + (hE : PmPreG st Cs Ω Sg Ac Locks p K μ s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) : + ∃ z1, PmSl st K.toKLayout p.slot s z1 ∧ ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem s' b = L.mem z1 b := + inv_accSl hA hE.1.2.1.1.1.1 (fun o h1 h2 => ((hE.1.2.1.1.1.1.ptU hE.1.2.2).ndR (Locks := Locks) o h1 h2).1) + (fun hW _ _ _ _ _ _ _ _ _ _ emi C hcap hy hz => ⟨(pm_blkMdb hA hKP hO hS hE hW emi C hcap hy hz).1, + pm_blkSl hA hKP hO hS hE hW emi C hcap hy hz⟩) h + +/-- **An accepted PageMap run keeps `MdbS`** (module docstring; gen 32: `inv_accMdbG` at `pm_blkMdb`). -/ +theorem pm_accMdb (hA : P0.ArchTotal ar) (hKP : RegionOKP K) {T0 Ω0 Sg0 : Set U64} + (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) (hS : SlotLayK K.toKLayout) {number : I64} {s s' : S} {a a' : A} + (hE : PmPreG st Cs Ω Sg Ac Locks p K μ s) + (h : syscall.handle_invocation_local st ar s a number p = ok (true, s', a')) + (hM : W7.R.ST.MdbS st K.toKLayout.slots s) : W7.R.ST.MdbS st K.toKLayout.slots s' := + inv_accMdbG hA hS hE.1.2.1.1.1.1 + (fun o h1 h2 => ((hE.1.2.1.1.1.1.ptU hE.1.2.2).ndR (Locks := Locks) o h1 h2).1) + (fun hW _ _ _ _ _ _ _ _ _ _ emi C hcap hy hz => pm_blkMdb hA hKP hO hS hE hW emi C hcap hy hz) h hM + +end pm + +/-! ## The PageMap slot -/ + +section slot +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The PageMap slot of `fl4LocPB` keeps `MdbS`** (module docstring; `hO` is P1's state-free slot-set fact). -/ +theorem pmSlotP_mdb (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) (hS : SlotLayK K.toKLayout) + {T0 Ω0 Sg0 : Set U64} (hO : W7.R.ST.OSOK T0 Ω0 Sg0 K.toKLayout.slots) : + MdbKeepP K.toKLayout.slots + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl i)))))))))) K m hK) := by + show MdbKeepP K.toKLayout.slots (pmSlotP st ar hA hm i K m hK) + unfold pmSlotP + by_cases h : PmPremP st i K m + · rw [dif_pos h] + refine LocalPath.mdbOfAcc _ K.toKLayout hS (pm_noZero i.number i.p) ?_ ?_ + · intro s x hp hx + have hp' : PmPreG st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s := hp + have N := io_narNS hp'.1.2.1.1.1.1 + rcases hx with rfl | rfl | rfl + · exact (N.reg hp'.1.2.1.1.1.1.tA (by simp [layout.reg.FAULT_IP])).2 + · exact (N.nd _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])).2 + · exact (N.nd _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])).2 + · intro s a r hp hr hac hM + obtain ⟨b, s', a'⟩ := r + change b = true at hac + subst hac + change syscall.handle_invocation_local st ar s a i.number i.p = ok (true, s', a') at hr + change W7.R.ST.MdbS st _ s at hM + change W7.R.ST.MdbS st _ s' + exact pm_accMdb hA hK.1 hO hS hp hr hM + · rw [dif_neg h] + intro s a r hp + exact hp.elim + +end slot + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/MdbWireA.lean b/proofs/Fl4ProofsA64/P2/MdbWireA.lean new file mode 100644 index 000000000..927dbbde2 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbWireA.lean @@ -0,0 +1,471 @@ +/- +# P2 (sync, gen 3): **`MdbS` of the history along an AArch64 execution** (DESIGN §9 item 4, Half 1 (d)) + +`SerialR.InvR` keeps, beside `InvH`'s history `H`, a replay record per started local path. This module carries P1's +MDB field of the history, `MdbSH st K.slots H` (`P2/MdbEditA.lean`), along an `ExecPA` (`P2/E2EPA.lean`): + +* **`WireI`**: `InvR`, `MdbSH` of `H`, every in-progress path without a record still in its glue (`PureGlue`, no + write yet), and per record (**`RecW`**): the history agrees with the record's start on every word the core may + read there (`agH`), the start's ghost maps and epoch data, and the glue's `StartAtEP` of the body at the start. +* **`leave_mdb`**: at a `leave`, the body is one deterministic run from the record's admissible memory + (`InvR.leave_run`'s argument), which `LocalPath.rep` (`P2/GlueRepA.lean`) ties to an `F` run from a state `σ'` that + is the start up to user words (accepted) or to its decline words (declined). The slot's `SlotWireP` + (`MdbEditA`) then gives `MdbSH` of `mix W s.mem H`: a slot word the core does not hold keeps `H`'s value (it is no + word the path wrote), a held one takes the run's (`agH`: `H` is the start there). +* **`wire_step`** (a core step; serial steps by the hypothesis `hser`, the others keep `H` but at `leave`), + **`wire_hw`** (a hardware step writes no slot word, `hw_nsl`), **`wire_exec`** (induction over `ExecPA`). +* **`serializable_mdb_of`**: for any family `LP` whose slots are `SlotWireP` at every layout with epoch data + (`WireFam`), an execution keeps `InvR` with `MdbSH` of its history, which is the memory at quiescence. +* `StartAtEP.toStart` (the start under the epoch data is a `StartAt`), `noLoc_closed`. + +Hypothesis `hSer` of `serializable_mdb_of` (Architect 16:45Z: serial entries keep `MdbSH`, the serial-entry +counterpart of `Fl4StepPA.ser` [J]); otherwise no premise: no [J], [P] or [W]. New module; no existing statement +changes. +-/ +import Fl4ProofsA64.P2.MdbEditA +import Fl4ProofsA64.P2.GlueRepA +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl +open Fl4ProofsA64.P2.OwnMap (KLayout) +open Fl4ProofsA64.P2.OwnMapV (KLayoutV vmapV) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP ownP ownDepP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA MapsPA GateEpPA EpAgP StartAtEP Fl4StepPA HwStepPA ExecPA + exec_safeRunPA) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv NoLoc Perm Held held_disj InvH InvR RecD LocRec AdmR StartOnce RunD + mix mix_mem mix_nmem mix_empty PureGlue hwCfg XSafeRun xsafeRun_greach allowed_of_sinv admR_iff startR racyR + invR_init step_ph_other held_of_heldV Phase) + +noncomputable section +open Classical + +section wire +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] +variable {Q : KLayoutP → (U64 → U64) → Prop} {ι : Type} {LP : PathsPA ι} + +/-- **A start under the epoch data is a `StartAt`**: the start state agrees with `μ` on the aligned kernel words, and +`μ` with the epoch memory on the gate's words, so the state has the epoch data its `Pre` asks. -/ +theorem StartAtEP.toStart {K : KLayoutP} {m : U64 → U64} + {P : E2E.LocalPath (ownP K) (OwnMap.cls K.toKLayout) (OwnMap.ko K.toKLayout m) (· ∈ K.user)} + {c : U64} {hs : U64 → Prop} {μ : U64 → U64} {q : E2E.SP} (h : StartAtEP Q P c hs μ q) {e : U64 → U64} + (hq : Q K e) (hE : EpAgP K e μ) : E2E.StartAt P c hs μ q := + let ⟨σ, a, hp, hc, hl, hμ, hk⟩ := h + ⟨σ, a, hp ⟨e, hq, fun x hx ha hu => (hμ x ha hu).trans (hE x hx).1⟩, hc, hl, hμ, hk⟩ + +/-- **A step from a quiescent state with the gate closed** starts no local path. -/ +theorem noLoc_closed {s s' : Cfg U64 U64 U64 U64} {c : U64} (hg : s.gate = .closed) (hn : NoLoc s) + (hst : Step s c s') : NoLoc s' := by + intro d hd + by_cases hdc : d = c + · subst hdc + cases hst with + | enter _ hg' => rw [hg] at hg'; cases hg' + | rd hc => exact hn d (by rw [hc]; trivial) + | rds hc => exact hn d (by rw [hc]; trivial) + | rdc hc => exact hn d (by rw [hc]; trivial) + | start hc => exact hn d (by rw [hc]; trivial) + | wr hc => exact hn d (by rw [hc]; trivial) + | lock _ hc _ => exact hn d (by rw [hc]; trivial) + | leave hc => exact hn d (by rw [hc]; trivial) + | scan _ _ _ _ => exact hn d hd + | drain _ _ _ => exact hn d hd + | _ => simp [Phase.IsLoc] at hd + · exact hn d (by rw [← step_ph_other hst hdc]; exact hd) + +variable (Q LP) in +/-- **What the wiring keeps of a started path's record** (module docstring). -/ +structure RecW (K : KLayoutP) (H : U64 → U64) (d : U64) (e : RecD U64 U64 U64 U64) : Prop where + agH : ∀ x, Perm e.s0 d x → H x = e.s0.mem x + maps : MapsPA K e.s0 + ep : ∃ ep, Q K ep ∧ EpAgP K ep e.s0.mem + start : ∃ (hK : ROKP K) (i : ι), StartAtEP Q (LP i K e.s0.mem hK) d (fun l => e.s0.lk l = some d) e.s0.mem e.b + +variable (st Q LP) in +/-- **The family's slots for the wiring**: slot words are no user words at the epoch layouts, and every slot is +`SlotWireP` at every layout with epoch data. -/ +def WireFam : Prop := + (∀ K e, Q K e → ∀ b, SlotWd K.toKLayout b → b ∉ K.user) ∧ + ∀ i (K : KLayoutP) m (hK : ROKP K), (∃ e, Q K e) → SlotWireP st Q K (LP i K m hK) + +variable (st Q LP) in +/-- **The wiring invariant** (module docstring). -/ +structure WireI (m0 : U64 → U64) (s : Cfg U64 U64 U64 U64) (K : KLayoutP) (H : U64 → U64) + (R : U64 → Option (RecD U64 U64 U64 U64)) : Prop where + inv : InvR m0 s H R + mdb : MdbSH st K.toKLayout H + glue : ∀ d p0 q W, s.ph d = .loc p0 q W → R d = none → PureGlue q ∧ W = ∅ + recW : ∀ d e, R d = some e → RecW Q LP K H d e + +/-- **`leave` keeps `MdbSH` of the history** (module docstring). -/ +theorem leave_mdb (hF : WireFam st Q LP) {m0 H : U64 → U64} {s : Cfg U64 U64 U64 U64} {c : U64} {p0 : E2E.SP} + {W : Set U64} {e : RecD U64 U64 U64 U64} {K : KLayoutP} (I : InvH m0 s H) + (hph : s.ph c = .loc p0 .done W) (Rc : LocRec s c e .done W) (hR : RecW Q LP K H c e) + (hM : MdbSH st K.toKLayout H) : MdbSH st K.toKLayout (mix W s.mem H) := by + have run : RunD e.b e.ρ (mix W s.mem e.ρ) := Rc.run e.ρ Rc.adm (fun _ _ => rfl) _ (.done _) + obtain ⟨hK, i, σ, a, hpG, hc, hl, hμ, hb⟩ := hR.start + obtain ⟨ep, hq, hE⟩ := hR.ep + have hNU := hF.1 K ep hq + have hW := hF.2 i K e.s0.mem hK ⟨ep, hq⟩ + have G : @GateEpPA (LP i K e.s0.mem hK).S (LP i K e.s0.mem hK).st (LP i K e.s0.mem hK).L Q K σ := + ⟨ep, hq, fun x hx ha hu => (hμ x ha hu).trans (hE x hx).1⟩ + have hpre := hpG G + have hA := admR_iff.mp Rc.adm + rw [hR.maps.own, hR.maps.cls, hR.maps.ko, ← hl, ← hc] at hA + obtain ⟨-, σ', r, hFr, -, hrun, hbr⟩ := (LP i K e.s0.mem hK).rep (ownDepP hK.1 hK.2) hpre hμ hA + rw [hb] at run + have hm' := hrun _ run + have hWP : ∀ x ∈ W, Perm e.s0 c x := fun x hx => (Rc.perm x).mp (Or.inl ((I.loc c p0 _ W hph).held x hx)) + have hρ : ∀ x, Perm e.s0 c x → e.ρ x = e.s0.mem x := Rc.adm.1 + obtain ⟨v, hv, hMv⟩ := hM + rcases hbr with ⟨hacc, hpre', hσ', -⟩ | ⟨-, hwr⟩ + · -- accepted: the edit form at `σ'` + have G' : @GateEpPA (LP i K e.s0.mem hK).S (LP i K e.s0.mem hK).st (LP i K e.s0.mem hK).L Q K σ' := + ⟨ep, hq, fun x hx ha hu => (hσ' x ha hu).trans (hE x hx).1⟩ + obtain ⟨D, hDh, hND, hed⟩ := hW.ed σ σ' a r hpre G hpre' G' + (fun x ha hu => (hσ' x ha hu).trans (hμ x ha hu).symm) hFr hacc + have hDP : ∀ b, D b → Perm e.s0 c b := by + intro b hD + have h1 := hDh b hD + rw [hc, hl, ownDepP hK.1 hK.2 _ _ hμ] at h1 + show Held e.s0.lk c (e.s0.own e.s0.mem b) ∨ _ + left + rw [hR.maps.own] + exact held_of_heldV (E2E.heldBy_iff.mp h1) + obtain ⟨v', hMv', hv'⟩ := hed v hMv fun b ha hs hD => by + rw [hv b ha hs, hR.agH b (hDP b hD), hσ' b ha (hNU b hs)] + refine ⟨v', fun b ha hs => ?_, hMv'⟩ + have hu := hNU b hs + by_cases hD : D b + · rw [(hv' b ha hs).1 hD] + have hp := hDP b hD + rw [← hm' b ha hu (by rw [hρ b hp, hσ' b ha hu])] + by_cases hbW : b ∈ W + · rw [mix_mem hbW, mix_mem hbW] + · rw [mix_nmem hbW, mix_nmem hbW, hρ b hp, hR.agH b hp] + · rw [(hv' b ha hs).2 hD, hv b ha hs] + by_cases hbW : b ∈ W + · have hp := hWP b hbW + have e1 := hm' b ha hu (by rw [hρ b hp, hσ' b ha hu]) + rw [mix_mem hbW] at e1 + rw [mix_mem hbW, e1, hND b ha hs hD, hσ' b ha hu, hR.agH b hp] + · rw [mix_nmem hbW] + · -- declined: no slot word written + have hk := run.wrIn hwr + refine ⟨v, fun b ha hs => ?_, hMv⟩ + rw [hv b ha hs] + by_cases hbW : b ∈ W + · have hp := hWP b hbW + have e1 := hk b (fun hd => hW.ds σ b hpre hd hs) + rw [mix_mem hbW] at e1 + rw [mix_mem hbW, e1, hρ b hp, hR.agH b hp] + · rw [mix_nmem hbW] + +/-- **A hardware step writes no slot word** (its word is a VCPU's or a guest register of a TCB). -/ +theorem hw_nsl {K : KLayoutP} (hK : ROKP K) {s : Cfg U64 U64 U64 U64} {v a x : U64} (h : HwStepPA K s v a x) : + ¬ SlotWd K.toKLayout a := by + obtain ⟨hv, -⟩ := HwStepVA.vcpu (M := vmapV) hK.v h + rintro ⟨q, hq⟩ + rcases hv with ⟨w, hw, hb⟩ | ⟨t, hr, -, -⟩ + · have e := hK.1.base.none w a ⟨hw, hb⟩ + rw [hq] at e + cases e + · have hr' : OwnMap.region K.toKLayout a = .tcb t := hr + rw [hq] at hr' + cases hr' + +/-- **A hardware step keeps the wiring invariant**, the history taking the same write. -/ +theorem wire_hw {m0 : U64 → U64} {s : Cfg U64 U64 U64 U64} {K : KLayoutP} {H : U64 → U64} + {R : U64 → Option (RecD U64 U64 U64 U64)} {v a x : U64} (hK : ROKP K) (hM : MapsPA K s) + (hW : WireI st Q LP m0 s K H R) (h : HwStepPA K s v a x) : + WireI st Q LP m0 (hwCfg s a x) K (Function.update H a x) R := by + have hw := h.hwWr hK hM + have hns := hw_nsl hK h + refine ⟨hW.inv.hw hw, ?_, fun d p0 q W hd hn => hW.glue d p0 q W hd hn, fun d e hd => ?_⟩ + · obtain ⟨u, hu, hMu⟩ := hW.mdb + exact ⟨u, fun b ha hs => by rw [hu b ha hs, Function.update_of_ne (by rintro rfl; exact hns hs)], hMu⟩ + · have r0 := hW.recW d e hd + obtain ⟨p0, q, W, -, hr⟩ := hW.inv.recs d e hd + exact ⟨fun y hy => by rw [Function.update_of_ne (hw.ne_of_perm ((hr.perm y).mpr hy))]; exact r0.agH y hy, + r0.maps, r0.ep, r0.start⟩ + +/-- **A core step keeps the wiring invariant** (module docstring); a serial step by `hser`. -/ +theorem wire_step (hF : WireFam st Q LP) {s0 s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64} + {H : U64 → U64} {R : U64 → Option (RecD U64 U64 U64 U64)} (h0 : Init s0) (hA0 : ArmInv s0) + (hr : XSafeRun s0 s) (hK : ROKP K) (hM : MapsPA K s) (hep : ∃ e, Q K e ∧ EpAgP K e s.mem) + (hW : WireI st Q LP s0.mem s K H R) (hst : Step s c s') (hS : Fl4StepPA Q LP s K c s' K') + (hser : E2E.SerStep s c → MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) : + ∃ H' R', WireI st Q LP s0.mem s' K' H' R' := by + obtain ⟨-, hSI, hAI⟩ := xsafeRun_greach h0 hA0 hr + have hal := allowed_of_sinv hSI c + obtain ⟨e0, hq0, hE0⟩ := hep + have hSO : StartOnce s c := fun p0 k W hph ν hν => by + obtain ⟨i, hsa⟩ := hS.start p0 k W hph hK + exact (LP i K s.mem hK).startOnce (ownDepP hK.1 hK.2) hM.own hM.cls hM.ko (StartAtEP.toStart hsa hq0 hE0) ν hν + obtain ⟨H', R', IR', hlog, hlv, hoth, -⟩ := hW.inv.step hal hst hSO + have IH := hW.inv.inv + by_cases hs : E2E.SerStep s c + · -- a serial step: no local path before or after + have hn : NoLoc s := IH.clo hs.1 + have hn' : NoLoc s' := noLoc_closed hs.1 hn hst + have e1 : H' = s'.mem := + funext fun x => (IR'.inv.agree x fun d _ _ _ hd => absurd (by rw [hd]; trivial) (hn' d)).symm + have e0 : s.mem = H := + funext fun x => IH.agree x fun d _ _ _ hd => absurd (by rw [hd]; trivial) (hn d) + refine ⟨H', fun _ => none, ⟨IR'.inv, fun _ _ h => by cases h⟩, ?_, + fun d _ _ _ hd => absurd (by rw [hd]; trivial) (hn' d), fun _ _ h => by cases h⟩ + rw [e1] + exact hser hs (by rw [e0]; exact hW.mdb) + have hKK := hS.keep hs + subst K' + have hph' : ∀ d, d ≠ c → s'.ph d = s.ph d := fun d h => step_ph_other hst h + have hRd : ∀ d, d ≠ c → ∀ e, R d = some e → ∃ p0 q W, s'.ph d = .loc p0 q W ∧ LocRec s' d e q W := + fun d hdc e hd => IR'.recs d e (by rw [hoth d hdc]; exact hd) + have hWc : ∀ p0 q W, s.ph c = .loc p0 q W → ∀ x ∈ W, Perm s c x := + fun p0 q W hc x hx => Or.inl ((IH.loc c p0 q W hc).held x hx) + have fin : ∀ (H'' : U64 → U64) (o : Option (RecD U64 U64 U64 U64)), InvH s0.mem s' H'' → + (∀ e', o = some e' → ∃ p0 q W, s'.ph c = .loc p0 q W ∧ LocRec s' c e' q W) → + (∀ p0 q W, s'.ph c = .loc p0 q W → o = none → PureGlue q ∧ W = ∅) → + (∀ e', o = some e' → RecW Q LP K H'' c e') → MdbSH st K.toKLayout H'' → + (∀ d e, d ≠ c → R d = some e → ∀ x, Perm e.s0 d x → H'' x = H x) → + ∃ H' R', WireI st Q LP s0.mem s' K H' R' := by + intro H'' o I'' ho hgo hro hmd hag + refine ⟨H'', Function.update R c o, ⟨I'', fun d e hd => ?_⟩, hmd, fun d p0 q W hd hn => ?_, + fun d e hd => ?_⟩ + · by_cases hdc : d = c + · subst hdc; rw [Function.update_self] at hd; exact ho e hd + · rw [Function.update_of_ne hdc] at hd; exact hRd d hdc e hd + · by_cases hdc : d = c + · subst hdc; rw [Function.update_self] at hn; exact hgo p0 q W hd hn + · rw [Function.update_of_ne hdc] at hn; rw [hph' d hdc] at hd; exact hW.glue d p0 q W hd hn + · by_cases hdc : d = c + · subst hdc; rw [Function.update_self] at hd; exact hro e hd + · rw [Function.update_of_ne hdc] at hd + have r0 := hW.recW d e hd + exact ⟨fun x hx => (hag d e hdc hd x hx).trans (r0.agH x hx), r0.maps, r0.ep, r0.start⟩ + have nk : ∀ e', (none : Option (RecD U64 U64 U64 U64)) = some e' → + ∃ p0 q W, s'.ph c = .loc p0 q W ∧ LocRec s' c e' q W := fun _ h => by cases h + have nr : ∀ e', (none : Option (RecD U64 U64 U64 U64)) = some e' → RecW Q LP K H c e' := fun _ h => by cases h + have sg : ∀ p0 q W, s'.ph c = .loc p0 q W → ∀ e : RecD U64 U64 U64 U64, (some e) = none → + PureGlue q ∧ W = ∅ := fun _ _ _ _ _ h => by cases h + have hag0 : ∀ d e, d ≠ c → R d = some e → ∀ x, Perm e.s0 d x → H x = H x := fun _ _ _ _ _ _ => rfl + cases hst with + | arm p hc => + have eH := hlog rfl + subst H' + exact fin _ none IR'.inv nk (fun _ _ _ hd _ => by simp at hd) nr hW.mdb hag0 + | enter hc hg => + have eH := hlog rfl + subst H' + refine fin _ none IR'.inv nk (fun p0 q W hd _ => ?_) nr hW.mdb hag0 + simp at hd + obtain ⟨rfl, rfl, rfl⟩ := hd + exact ⟨hAI c _ hc, rfl⟩ + | back hc _ => + have eH := hlog rfl + subst H' + exact fin _ none IR'.inv nk (fun _ _ _ hd _ => by simp at hd) nr hW.mdb hag0 + | @rd p0 a k W hc => + have eH := hlog rfl + subst H' + simp only [Fl4Proofs.P2.Serial.Allowed, hc] at hal + cases hRc : R c with + | none => exact absurd (hW.glue c _ _ _ hc hRc).1 (fun h => by cases h) + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + exact fin _ (some e) IR'.inv (fun e' he => by cases he; exact ⟨p0, _, W, Function.update_self .., hr.rd hal⟩) + (fun p0 q W hd => sg p0 q W hd e) (fun e' he => by cases he; exact hW.recW c e hRc) hW.mdb hag0 + | @rds p0 a k W hc => + have eH := hlog rfl + subst H' + cases hRc : R c with + | none => + obtain ⟨hg, hw0⟩ := hW.glue c _ _ _ hc hRc + cases hg with + | rds hk => + refine fin _ none IR'.inv nk (fun p0' q W' hd _ => ?_) nr hW.mdb hag0 + simp at hd + obtain ⟨rfl, rfl, rfl⟩ := hd + exact ⟨hk _, hw0⟩ + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + have r0 := hW.recW c e hRc + exact fin _ (some (racyR s c e a)) IR'.inv + (fun e' he => by cases he; exact ⟨p0, _, W, Function.update_self .., hr.rds (hWc _ _ _ hc)⟩) + (fun p0 q W hd => sg p0 q W hd _) + (fun e' he => by cases he; exact ⟨r0.agH, r0.maps, r0.ep, r0.start⟩) hW.mdb hag0 + | @rdc p0 a K' k W hc => + have eH := hlog rfl + subst H' + cases hRc : R c with + | none => exact absurd (hW.glue c _ _ _ hc hRc).1 (fun h => by cases h) + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + have r0 := hW.recW c e hRc + exact fin _ (some (racyR s c e a)) IR'.inv + (fun e' he => by cases he; exact ⟨p0, _, W, Function.update_self .., hr.rdc (hWc _ _ _ hc)⟩) + (fun p0 q W hd => sg p0 q W hd _) + (fun e' he => by cases he; exact ⟨r0.agH, r0.maps, r0.ep, r0.start⟩) hW.mdb hag0 + | @start p0 k W hc => + have eH := hlog rfl + subst H' + cases hRc : R c with + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + exact False.elim (hr.once e.ρ hr.adm fun _ _ => rfl) + | none => + have hw0 := (hW.glue c _ _ _ hc hRc).2 + have r1 : RecW Q LP K H c (startR s k) := by + obtain ⟨i, hsa⟩ := hS.start p0 k W hc hK + refine ⟨fun x hx => (IH.agree x fun d q0 q U hd hxU => ?_).symm, hM, ⟨e0, hq0, hE0⟩, hK, i, hsa⟩ + by_cases hdc : d = c + · subst hdc; rw [hc] at hd; cases hd; rw [hw0] at hxU; exact hxU + · exact held_disj hdc ((IH.loc d q0 q U hd).held x hxU) hx + exact fin _ (some (startR s k)) IR'.inv + (fun e' he => by + cases he + exact ⟨p0, k, W, Function.update_self .., LocRec.start (hWc _ _ _ hc) (hSO p0 k W hc)⟩) + (fun p0 q W hd => sg p0 q W hd _) (fun e' he => by cases he; exact r1) hW.mdb hag0 + | @wr p0 a v k W hc => + have eH := hlog rfl + subst H' + simp only [Fl4Proofs.P2.Serial.Allowed, hc] at hal + obtain ⟨-, hmv, -⟩ := hal + cases hRc : R c with + | none => exact absurd (hW.glue c _ _ _ hc hRc).1 (fun h => by cases h) + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + exact fin _ (some e) IR'.inv + (fun e' he => by cases he; exact ⟨p0, k, insert a W, Function.update_self .., hr.wr hmv⟩) + (fun p0 q W hd => sg p0 q W hd e) (fun e' he => by cases he; exact hW.recW c e hRc) hW.mdb hag0 + | @lock p0 l k W b hc hb => + have eH := hlog rfl + subst H' + cases hRc : R c with + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + exact False.elim (hr.once e.ρ hr.adm fun _ _ => rfl) + | none => + obtain ⟨hg, hw0⟩ := hW.glue c _ _ _ hc hRc + cases hg with + | lock hk => + refine fin _ none IR'.inv nk (fun p0' q W' hd _ => ?_) nr hW.mdb hag0 + simp at hd + obtain ⟨rfl, rfl, rfl⟩ := hd + exact ⟨hk _, hw0⟩ + | @leave p0 W hc => + obtain rfl := hlv p0 W hc (fun hl => absurd (congrArg List.length hl) (by simp)) + have hmd : MdbSH st K.toKLayout (mix W s.mem H) := by + cases hRc : R c with + | none => + rw [(hW.glue c _ _ _ hc hRc).2, mix_empty] + exact hW.mdb + | some e => + obtain ⟨p0', q', W', hph, hr⟩ := hW.inv.recs c e hRc + rw [hc] at hph; cases hph + exact leave_mdb hF IH hc hr (hW.recW c e hRc) hW.mdb + refine fin _ none IR'.inv nk (fun _ _ _ hd _ => by simp at hd) (fun _ h => by cases h) hmd + fun d e hdc hd x hx => ?_ + obtain ⟨_, _, _, _, hrd⟩ := hW.inv.recs d e hd + exact mix_nmem fun hxW => + held_disj (Ne.symm hdc) ((IH.loc c p0 _ W hc).held x hxW) ((LocRec.perm hrd x).mpr hx) + | take p hc _ => + have eH := hlog rfl + subst H' + exact fin _ none IR'.inv nk (fun _ _ _ hd _ => by simp at hd) nr hW.mdb hag0 + | scan d' hh hg hd' => + have eH := hlog rfl + subst H' + refine fin _ (R c) IR'.inv (fun e' he => ?_) (fun p0 q W hd hn => hW.glue c p0 q W hd hn) + (fun e' he => hW.recW c e' he) hW.mdb hag0 + obtain ⟨p0, q, W, hph, hr⟩ := hW.inv.recs c e' he + exact ⟨p0, q, W, hph, hr.frame (fun _ => Iff.rfl) rfl rfl (fun _ h => absurd rfl h)⟩ + | drain hh hg hall => + have eH := hlog rfl + subst H' + refine fin _ (R c) IR'.inv (fun e' he => ?_) (fun p0 q W hd hn => hW.glue c p0 q W hd hn) + (fun e' he => hW.recW c e' he) hW.mdb hag0 + obtain ⟨p0, q, W, hph, hr⟩ := hW.inv.recs c e' he + exact ⟨p0, q, W, hph, hr.frame (fun _ => Iff.rfl) rfl rfl (fun _ h => absurd rfl h)⟩ + | serial _ _ _ _ hh hg _ _ => exact absurd ⟨hg, hh⟩ hs + +/-- **The wiring invariant along an execution** (module docstring). -/ +theorem wire_exec (hF : WireFam st Q LP) {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} (h0 : Init s0) + (hA0 : ArmInv s0) (hK0 : ROKP K0) (hM0 : MapsPA K0 s0) (hQ0 : Q K0 s0.mem) + (hMd0 : MdbSH st K0.toKLayout s0.mem) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, ExecPA Q LP s0 K0 s K → Step s c s' → + Fl4StepPA Q LP s K c s' K' → E2E.SerStep s c → MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA Q LP s0 K0 s K) : ∃ H R, WireI st Q LP s0.mem s K H R := by + induction h with + | refl => + exact ⟨s0.mem, fun _ => none, invR_init h0, hMd0, + fun d _ _ _ hd _ => absurd (by rw [hd]; trivial) (h0.2 d), fun _ _ h => by cases h⟩ + | step hp hst hS ih => + obtain ⟨H, R, hW⟩ := ih + obtain ⟨hr, hK, hM, hep⟩ := exec_safeRunPA h0 hA0 hK0 hM0 hQ0 hp + exact wire_step hF h0 hA0 hr hK hM hep hW hst hS (hSer hp hst hS) + | hw hp hh ih => + obtain ⟨H, R, hW⟩ := ih + obtain ⟨-, hK, hM, -⟩ := exec_safeRunPA h0 hA0 hK0 hM0 hQ0 hp + exact ⟨_, _, wire_hw hK hM hW hh⟩ + +/-- **`MdbS` of the history along an execution**, for a family whose slots are in edit form (`WireFam`), given the +initial `MdbSH` and `hSer` (serial entries keep `MdbSH`): the replay invariant holds with a history carrying P1's MDB +field, and at quiescence the history is the memory. -/ +theorem serializable_mdb_of (hF : WireFam st Q LP) {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} (h0 : Init s0) + (hA0 : ArmInv s0) (hK0 : ROKP K0) (hM0 : MapsPA K0 s0) (hQ0 : Q K0 s0.mem) + (hMd0 : MdbSH st K0.toKLayout s0.mem) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, ExecPA Q LP s0 K0 s K → Step s c s' → + Fl4StepPA Q LP s K c s' K' → E2E.SerStep s c → MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA Q LP s0 K0 s K) : + ∃ H R, InvR s0.mem s H R ∧ MdbSH st K.toKLayout H ∧ (NoLoc s → H = s.mem) := by + obtain ⟨H, R, hW⟩ := wire_exec hF h0 hA0 hK0 hM0 hQ0 hMd0 hSer h + exact ⟨H, R, hW.inv, hW.mdb, fun hn => funext fun x => + (hW.inv.inv.agree x fun d _ _ _ hd => absurd (by rw [hd]; trivial) (hn d)).symm⟩ + +end wire + +/-! ## The slots of `fl4LocPB` -/ + +section fl4 +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **`fl4LocPB` for the wiring**, given the edit forms of its PageMap and Delete-with-PTE slots (`hPm`, `hDel`: +lemma hypotheses, the interface the two edit forms discharge): the nine frame slots by `SlotWireP.ofKept` (their +`*SlotP_kept` and `*SlotP_ds`, `P2/MdbEditA.lean`), slot words no user words by `EpochAP.slotNU`. -/ +theorem fl4LocPB_wireFam + (hPm : ∀ i (K : KLayoutP) m (hK : ROKP K), (∃ e, EpochAP st K e) → SlotWireP st (EpochAP st) K + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl i)))))))))) K m hK)) + (hDel : ∀ i (K : KLayoutP) m (hK : ROKP K), (∃ e, EpochAP st K e) → SlotWireP st (EpochAP st) K + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i)))))))))) K m hK)) : + WireFam st (EpochAP st) (fl4LocPB st ar hA hm) := by + refine ⟨fun K e he b hb => he.slotNU hb, fun i K m hK hep => ?_⟩ + obtain ⟨e, he⟩ := hep + have hS := he.slotLay + rcases i with i | i | i | i | i | i | j | j | j | i | i + · exact SlotWireP.ofKept st ((vmFSlotP_kept st ar hA hm i K m hK).toG _) (vmFSlotP_ds st ar hA hm i K m hK) + · exact SlotWireP.ofKept st ((rrSlotP_kept st ar hA hm i K m hK).toG _) (rrSlotP_ds st ar hA hm i K m hK) + · exact SlotWireP.ofKept st ((stSlotP_kept st ar hA hm i K m hK).toG _) (stSlotP_ds st ar hA hm i K m hK) + · exact SlotWireP.ofKept st ((irqSlotP_kept st ar hA hm i K m hK hS).toG _) (irqSlotP_ds st ar hA hm i K m hK) + · exact SlotWireP.ofKept st ((slSlotP_kept st ar hA hm i K m hK hS).toG _) (slSlotP_ds st ar hA hm i K m hK) + · exact SlotWireP.ofKept st ((nbwSlotP_kept st ar hA hm i K m hK).toG _) (nbwSlotP_ds st ar hA hm i K m hK) + · exact SlotWireP.ofKept st (vxSlotP_kept st ar hA hm j K m hK hS) (vxSlotP_ds st ar hA hm j K m hK) + · exact SlotWireP.ofKept st ((vtSlotP_kept st ar hA hm j K m hK).toG _) (vtSlotP_ds st ar hA hm j K m hK) + · exact SlotWireP.ofKept st ((vreSlotP_kept st ar hA hm j K m hK).toG _) (vreSlotP_ds st ar hA hm j K m hK) + · exact hPm i K m hK ⟨e, he⟩ + · exact hDel i K m hK ⟨e, he⟩ + +end fl4 + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/MdbWirePDA.lean b/proofs/Fl4ProofsA64/P2/MdbWirePDA.lean new file mode 100644 index 000000000..f460a12e3 --- /dev/null +++ b/proofs/Fl4ProofsA64/P2/MdbWirePDA.lean @@ -0,0 +1,517 @@ +/- +# P2 (sync, gen 4): **the PageMap and Delete-with-PTE edit forms**, and `MdbS` of the history along an AArch64 +execution of `fl4LocPB` (DESIGN §9 item 4, Half 1 (c)–(d)) + +`P2/MdbWireA.lean`'s `fl4LocPB_wireFam` takes the edit forms (`SlotWireP`, `P2/MdbEditA.lean`) of the two slots that +write capability slots as lemma hypotheses. This module proves them and states the wiring for `fl4LocPB` itself. + +* **`pmSlotP_wire`** (PageMap). An accepted run's slot words (`pm_accSl`, `P2/MdbKeepPmA.lean`) are either the + start's (no edited word) or the start's after the in-place write of a `Frame` cap at the plan's slot (`PmSl`). The + edited words are the slot's two cap words. They are held at the start σ: the plan locks the slot (`pm_core`), and + under `ownP` the cap words of a non-TCB slot holding a `Frame` cap are that slot's lock's (**`ownP_slotW`**; `ownP` + of σ is that of σ', `ownDepP`). The successor of an `MdbS` state `v` is `v` with the same two-word write + (`MdbS.set_frame`: `v`'s cap at the slot is σ''s, a `Frame` cap). +* **`delSlotP_wire`** (Delete-with-PTE). An accepted run's slot words (`del_accSl`) are the start's, or those of + `cdt.empty_slot dest .Null` run from a state `x1` with the start's slot words (`DelSl`). The edited words are + **`EsD`**: `dest`'s four words, the previous slot's `NEXT` word and the next slot's `PREV` and `NEXT` words. They are + held at σ because the plan holds `dest` and its non-null CDT neighbours (**`del_heldP`**, from `plan_with_delF`), and + `InvQP` gives every plan lock at σ. The successor of `v` is `empty_slot` run from `v`: it exists with `MdbS` by P1's + `cdt.empty_slot.mdbO`, and it makes the same writes on `EsD` (**`es_sim`**). That is the congruence of `empty_slot`: + two states equal on `EsD` stay equal there, and each keeps its own value elsewhere (`SimE`, via `set_next_eq`, + `set_prev_eq`, `set_fb_eq_*` and `clear_eq`). +* **`serializable_fl4_localVP_mdb`**: `serializable_mdb_of` at `fl4LocPB_wireFam` with the two edit forms. An + `ExecPA` of `fl4LocPB` keeps `InvR` with `MdbSH` of its history, which is the memory at quiescence. + **`serializable_fl4_localVP_mdbT`**: the same from Boot's `MdbST` at the start, concluded at quiescence. + +Hypothesis `hSer` of the top theorem (Architect 16:45Z / 17:41Z: per serial step, the serial-entry counterpart of +`Fl4StepPA.ser` [J]); otherwise no premise: no [J], [P] or [W]. New module; no existing statement changes. +-/ +import Fl4ProofsA64.P2.MdbWireA +import Fl4ProofsA64.P1.MdbTreeS +open Aeneas Aeneas.Std Result + +set_option autoImplicit false + +namespace Fl4ProofsA64.P2.E2EVA +open fl4_core_a64 Fl4ProofsA64.P1 Fl4ProofsA64.P2 Fl4ProofsA64.P2.PathObl Fl4ProofsA64.P2.E2EA Fl4ProofsA64.P2.E2EP +open Fl4ProofsA64.P2.OwnMap (KLayout region own InW TcbSlot NullOrFrame slotOwner) +open Fl4ProofsA64.P2.OwnMapP (KLayoutP RegionOKP ownP ownP_off ownDepP) +open Fl4ProofsA64.P2.E2EPA (ROKP PathsPA MapsPA GateEpPA Fl4StepPA ExecPA) +open Fl4Proofs.P2.Serial (Cfg Step Init ArmInv NoLoc InvR) + +local notation "wa" => core.num.U64.wrapping_add + +noncomputable section +open Classical + +/-! ## The congruence of `empty_slot` -/ + +section es +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} + +variable (st) in +/-- **Two runs side by side**: `x` and `v` are equal on `D`, and off `D` each still holds its start's value. -/ +def SimE (D : U64 → Prop) (x0 v0 x v : S) : Prop := + (∀ b, D b → L.mem v b = L.mem x b) ∧ ∀ b, Aligned b → ¬ D b → L.mem v b = L.mem v0 b ∧ L.mem x b = L.mem x0 b + +theorem SimE.wr {D : U64 → Prop} (hDa : ∀ b, D b → Aligned b) {x0 v0 x v : S} (h : SimE st D x0 v0 x v) {q : U64} + (hq : D q) (w : U64) : SimE st D x0 v0 (L.wr x q w) (L.wr v q w) := by + refine ⟨fun b hb => ?_, fun b hb hn => ?_⟩ + · rw [L.mem_wr _ _ _ _ (hDa q hq) (hDa b hb), L.mem_wr _ _ _ _ (hDa q hq) (hDa b hb), h.1 b hb] + · have hne : q ≠ b := fun e => hn (e ▸ hq) + rw [mem_wr_ne' (hDa q hq) hb hne, mem_wr_ne' (hDa q hq) hb hne] + exact h.2 b hb hn + +/-- A read-modify-write of one `D` word, run at both states. -/ +theorem SimE.rmw {D : U64 → Prop} (hDa : ∀ b, D b → Aligned b) {F : S → Result S} {q : U64} {g : U64 → U64} + (hF : ∀ s, F s = ok (L.wr s q (g (L.mem s q)))) (hq : D q) {x0 v0 x v x' v' : S} (h : SimE st D x0 v0 x v) + (ex : F x = ok x') (ev : F v = ok v') : SimE st D x0 v0 x' v' := by + rw [hF] at ex ev + cases ok_inj_ff ex + cases ok_inj_ff ev + rw [h.1 q hq] + exact h.wr hDa hq _ + +/-- `first_badged` reads the slot's `NEXT` word only. -/ +theorem fb_congr {q : U64} {s s' : S} (h : L.mem s' (wa q 24#u64) = L.mem s (wa q 24#u64)) : + obj.Slot.first_badged st q s' = obj.Slot.first_badged st q s := by + unfold obj.Slot.first_badged obj.Slot.next_word + simp only [lift, bind_ok, cte_next, L.read_eq, h] + +/-- The `NEXT` word `set_first_badged` writes. -/ +def fbG : Bool → U64 → U64 + | true, w => (w &&& ~~~ W7.R.ST.FB) ||| W7.R.ST.FB + | false, w => (w &&& ~~~ W7.R.ST.FB) ||| 0#u64 + +theorem set_fb_eqG (s : S) (q : U64) (bb : Bool) : + obj.Slot.set_first_badged st q s bb = ok (L.wr s (wa q 24#u64) (fbG bb (L.mem s (wa q 24#u64)))) := by + cases bb + · exact W7.R.ST.set_fb_eq_f s q + · exact W7.R.ST.set_fb_eq_t s q + +/-- **The words `empty_slot dest .Null` reads and writes** (with `dest`'s `PREV` word `pv` and `NEXT` pointer +`nx`): `dest`'s four words, `pv`'s `NEXT` word, `nx`'s `PREV` and `NEXT` words. -/ +def EsD (dest pv nx b : U64) : Prop := + b = dest ∨ b = wa dest 8#u64 ∨ b = wa dest 16#u64 ∨ b = wa dest 24#u64 ∨ (pv ≠ 0#u64 ∧ b = wa pv 24#u64) ∨ + (nx ≠ 0#u64 ∧ (b = wa nx 16#u64 ∨ b = wa nx 24#u64)) + +/-- **The congruence of `empty_slot dest .Null`** (module docstring): runs from two states equal on `EsD` end equal +there, and each keeps its start's value off `EsD`. -/ +theorem es_sim {dest pv nx : U64} (hDa : ∀ b, EsD dest pv nx b → Aligned b) {x v : S} {a : A} + (hpv : L.mem x (wa dest 16#u64) = pv) (hnx : W7.R.ST.NxA st x dest = nx) + (hag : ∀ b, EsD dest pv nx b → L.mem v b = L.mem x b) {y w : S × A} + (hx : cdt.empty_slot st ar x a dest .Null = ok y) (hv : cdt.empty_slot st ar v a dest .Null = ok w) : + SimE st (EsD dest pv nx) x v y.1 w.1 := by + have S0 : SimE st (EsD dest pv nx) x v x v := ⟨hag, fun _ _ _ => ⟨rfl, rfl⟩⟩ + have d0 : EsD dest pv nx dest := Or.inl rfl + have d8 : EsD dest pv nx (wa dest 8#u64) := Or.inr (Or.inl rfl) + have d16 : EsD dest pv nx (wa dest 16#u64) := Or.inr (Or.inr (Or.inl rfl)) + have d24 : EsD dest pv nx (wa dest 24#u64) := Or.inr (Or.inr (Or.inr (Or.inl rfl))) + have ec : W7.R.ST.capA st v dest = W7.R.ST.capA st x dest := by + unfold W7.R.ST.capA; rw [hag _ d0, hag _ d8] + have ep : L.mem v (wa dest 16#u64) = pv := (hag _ d16).trans hpv + have en : W7.R.ST.NxA st v dest = nx := by + unfold W7.R.ST.NxA; rw [hag _ d24]; exact hnx + unfold cdt.empty_slot at hx hv + rw [W7.R.ST.capA_eq, bind_ok] at hx + rw [W7.R.ST.capA_eq, bind_ok] at hv + rw [ec] at hv + obtain ⟨bn, ebn, hx⟩ := bind_eq_ok hx + rw [ebn, bind_ok] at hv + cases bn + swap + · simp only [↓reduceIte] at hx hv + cases ok_inj_ff hx + cases ok_inj_ff hv + exact S0 + simp only [Bool.false_eq_true, ↓reduceIte] at hx hv + rw [W7.R.ST.slot_prev_eq, bind_ok, W7.R.ST.next_eq, bind_ok] at hx + rw [W7.R.ST.slot_prev_eq, bind_ok, W7.R.ST.next_eq, bind_ok] at hv + rw [hpv, hnx] at hx + rw [ep, en, fb_congr (hag _ d24)] at hv + obtain ⟨fb, efb, hx⟩ := bind_eq_ok hx + rw [efb, bind_ok] at hv + obtain ⟨b1, eb1, hx⟩ := bind_eq_ok hx + rw [eb1, bind_ok] at hv + obtain ⟨x1, ex1, hx⟩ := bind_eq_ok hx + obtain ⟨v1, ev1, hv⟩ := bind_eq_ok hv + have S1 : SimE st (EsD dest pv nx) x v x1 v1 := by + cases b1 + · simp only [Bool.false_eq_true, ↓reduceIte] at ex1 ev1 + have hp0 : pv ≠ 0#u64 := by + unfold obj.Slot.is_null at eb1 + have h1 := ok_inj_ff eb1 + simpa using h1 + exact S0.rmw hDa (F := fun s => obj.Slot.set_next st pv s nx) + (g := fun w => (w &&& ~~~ obj.MDB_PTR_MASK) ||| (nx &&& obj.MDB_PTR_MASK)) + (fun s => W7.R.ST.set_next_eq s pv nx) (Or.inr (Or.inr (Or.inr (Or.inr (Or.inl ⟨hp0, rfl⟩))))) ex1 ev1 + · simp only [↓reduceIte] at ex1 ev1 + cases ok_inj_ff ex1 + cases ok_inj_ff ev1 + exact S0 + obtain ⟨b2, eb2, hx⟩ := bind_eq_ok hx + rw [eb2, bind_ok] at hv + obtain ⟨x2, ex2, hx⟩ := bind_eq_ok hx + obtain ⟨v2, ev2, hv⟩ := bind_eq_ok hv + have S2 : SimE st (EsD dest pv nx) x v x2 v2 := by + cases b2 + · simp only [Bool.false_eq_true, ↓reduceIte] at ex2 ev2 + have hn0 : nx ≠ 0#u64 := by + unfold obj.Slot.is_null at eb2 + have h1 := ok_inj_ff eb2 + simpa using h1 + have dn16 : EsD dest pv nx (wa nx 16#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨hn0, Or.inl rfl⟩)))) + have dn24 : EsD dest pv nx (wa nx 24#u64) := Or.inr (Or.inr (Or.inr (Or.inr (Or.inr ⟨hn0, Or.inr rfl⟩)))) + obtain ⟨x3, ex3, ex2⟩ := bind_eq_ok ex2 + obtain ⟨v3, ev3, ev2⟩ := bind_eq_ok ev2 + have S3 := S1.rmw hDa (F := fun s => obj.Slot.set_prev st nx s pv) (g := fun _ => pv) + (fun s => W7.R.ST.set_prev_eq s nx pv) dn16 ex3 ev3 + rw [fb_congr (S3.1 _ dn24)] at ev2 + obtain ⟨nfb, enfb, ex2⟩ := bind_eq_ok ex2 + rw [enfb, bind_ok] at ev2 + cases nfb + · simp only [Bool.false_eq_true, ↓reduceIte] at ex2 ev2 + exact S3.rmw hDa (F := fun s => obj.Slot.set_first_badged st nx s fb) (g := fbG fb) + (fun s => set_fb_eqG s nx fb) dn24 ex2 ev2 + · simp only [↓reduceIte] at ex2 ev2 + exact S3.rmw hDa (F := fun s => obj.Slot.set_first_badged st nx s true) (g := fbG true) + (fun s => set_fb_eqG s nx true) dn24 ex2 ev2 + · simp only [↓reduceIte] at ex2 ev2 + cases ok_inj_ff ex2 + cases ok_inj_ff ev2 + exact S1 + rw [W7.R.ST.clear_eq, bind_ok, W7.R.ST.pcd_null] at hx + rw [W7.R.ST.clear_eq, bind_ok, W7.R.ST.pcd_null] at hv + cases ok_inj_ff hx + cases ok_inj_ff hv + exact (((S2.wr hDa d0 _).wr hDa d8 _).wr hDa d16 _).wr hDa d24 _ + +end es + +/-! ## Slot words held at the start -/ + +section held +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] + +/-- **The owner of a capability slot's word under `ownP`**: the slot's lock, for the words from offset 16, and below +when the slot is no TCB's own and holds a Null / Frame cap (rule 2; not a PTE word, no `Special` word). -/ +theorem ownP_slotW {K : KLayoutP} (hK : RegionOKP K) {T Cs Ω Sg Rp Scs Nt Ac : Set U64} {H : Heap} {Z : U64 → Nat} + {s : S} (P : IoPre st K.toKLayout T Cs Ω H Sg Rp Scs Nt Z Ac s) {q o : U64} (hq : q ∈ K.toKLayout.slots) + (ho : o.val < 32) (hw : 16 ≤ o.val ∨ (¬ TcbSlot K.toKLayout q ∧ NullOrFrame (L.mem s q))) : + ownP K (L.mem s) (wa q o) = .lock q := by + have hL : Slot8 K.toKLayout := fun q hq => ⟨P.lay.slot8 q hq, P.lay.slotf q hq⟩ + have hv := slot_word_val (P.lay.slotf q hq) ho + have hr := P.lay.slotw q hq _ ⟨by rw [hv]; omega, by rw [hv]; omega⟩ + have hd : (wa q o).val - q.val = o.val := by rw [hv]; omega + have hnp : wa q o ∉ K.ptes := fun h => by + have h' := hK.none _ h + rw [hr] at h' + cases h' + rw [ownP_off hnp, OwnMapV.ownV_off (P.slotW_nsN (nsK_of hK.base) (slotW_of hL hq ho))] + have hown : own K.toKLayout (L.mem s) (wa q o) = slotOwner K.toKLayout (L.mem s) q o.val := by + simp only [own, hr, hd] + rw [hown] + unfold slotOwner + by_cases h16 : 16 ≤ o.val + · rw [if_pos h16] + · obtain ⟨hT, hn⟩ := hw.resolve_left h16 + rw [if_neg h16, if_neg hT, if_pos hn] + +/-- The target a CNode cap's lookup finds is determined by the cap at the slot. -/ +theorem lts_det {s : S} {sl r2 r3 cp cr cg cgb cp' cr' cg' cgb' d d' : U64} + (h1 : obj.Slot.cap st sl s = ok (.CNode cp cr cg cgb)) (h2 : obj.Slot.cap st sl s = ok (.CNode cp' cr' cg' cgb')) + (l1 : cspace.lookup_target_slot st s (.CNode cp cr cg cgb) r2 r3 = ok (.Ok d)) + (l2 : cspace.lookup_target_slot st s (.CNode cp' cr' cg' cgb') r2 r3 = ok (.Ok d')) : d = d' := by + rw [h1] at h2 + have e := ok_inj_ff h2 + simp only [capability.Cap.CNode.injEq] at e + obtain ⟨rfl, rfl, rfl, rfl⟩ := e + rw [l1] at l2 + have e2 := ok_inj_ff l2 + simp only [core.result.Result.Ok.injEq] at e2 + exact e2 + +/-- **The plan holds the Delete target and its CDT neighbours** (`plan_with_delF`'s `PHeld` facts at the entry). -/ +theorem del_heldP {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayout} {μ : U64 → U64} + {s : S} (hq : InvQFP st Cs Ω Sg Ac Locks p K μ s) : + ∃ cp cr cg cgb dest : U64, obj.Slot.cap st p.slot s = ok (.CNode cp cr cg cgb) ∧ + cspace.lookup_target_slot st s (.CNode cp cr cg cgb) (L.mem s (regA (L.curW s) layout.reg.X2)) + (L.mem s (regA (L.curW s) layout.reg.X3)) = ok (.Ok dest) ∧ + (dest ≠ 0#u64 → PHeld p dest) ∧ + (L.mem s (wa dest layout.cte.PREV) ≠ 0#u64 → PHeld p (L.mem s (wa dest layout.cte.PREV))) ∧ + (∀ nx, obj.Slot.next st dest s = ok nx → nx ≠ 0#u64 → PHeld p nx) := by + obtain ⟨⟨-, ⟨t, info, ex, et, ei, hpw⟩, harm, -⟩, -⟩ := hq + have ht : t = L.curW s := ok_inj_ff (et.symm.trans (cur_thread_ok s)) + subst ht + obtain ⟨hlbl, -⟩ := harm _ _ (cur_thread_ok s) ei + rw [hlbl] at hpw + obtain ⟨cp, cr, cg, cgb, dest, -, -, -, -, -, hcap, htg, -, hHd, hHp, hHn, -⟩ := plan_with_delF hpw + exact ⟨cp, cr, cg, cgb, dest, hcap, htg, hHd, hHp, hHn⟩ + +end held + +/-! ## The two edit forms -/ + +section edit +variable {S : Type} {st : machine.Store S} [L : LawfulStore st] {A : Type} {ar : machine.Arch A} +variable {Cs Ω Sg Ac : Set U64} {Locks : S → U64 → Prop} {p : localinv.Plan} {K : KLayoutP} {μ : U64 → U64} + +variable (st) in +/-- **`SlotEdX`'s conclusion at one accepted run** from `σ'` to `r` (start σ, core `c`, locks `Lk`), for `ownP K`. -/ +def EdAt (K : KLayoutP) (Lk : U64 → Prop) (c : U64) (σ σ' r : S) : Prop := + ∃ D : U64 → Prop, (∀ b, D b → HeldBy c Lk (ownP K (L.mem σ) b)) ∧ + (∀ b, Aligned b → SlotWd K.toKLayout b → ¬ D b → L.mem r b = L.mem σ' b) ∧ + ∀ v : S, W7.R.ST.MdbS st K.toKLayout.slots v → + (∀ b, Aligned b → SlotWd K.toKLayout b → D b → L.mem v b = L.mem σ' b) → + ∃ v' : S, W7.R.ST.MdbS st K.toKLayout.slots v' ∧ ∀ b, Aligned b → SlotWd K.toKLayout b → + (D b → L.mem v' b = L.mem r b) ∧ (¬ D b → L.mem v' b = L.mem v b) + +/-- A run keeping every slot word edits nothing. -/ +theorem edAt_kept {Lk : U64 → Prop} {c : U64} {σ σ' r : S} + (h : ∀ b, Aligned b → SlotWd K.toKLayout b → L.mem r b = L.mem σ' b) : EdAt st K Lk c σ σ' r := + ⟨fun _ => False, fun _ h => h.elim, fun b hb hs _ => h b hb hs, + fun v hM _ => ⟨v, hM, fun _ _ _ => ⟨fun h => h.elim, fun _ => rfl⟩⟩⟩ + +/-- **The PageMap edit form at one accepted run** (module docstring). -/ +theorem pm_edit (hA : P0.ArchTotal ar) (hK : ROKP K) {e : U64 → U64} (he : EpochAP st K e) {number : I64} + {σ σ' s' : S} {a a' : A} (hp : PmPreG st Cs Ω Sg Ac Locks p K μ σ) (hp' : PmPreG st Cs Ω Sg Ac Locks p K μ σ') + (hag : ∀ x, Aligned x → x ∉ K.user → L.mem σ' x = L.mem σ x) + (hr : syscall.handle_invocation_local st ar σ' a number p = ok (true, s', a')) : + EdAt st K (Locks σ) (L.cpu σ) σ σ' s' := by + obtain ⟨T0, Ω0, Sg0, hO⟩ := he.osok + have hS := he.slotLay + obtain ⟨z1, hz1, ks⟩ := pm_accSl hA hK.1 hO hS hp' hr + rcases hz1 with k0 | ⟨c, hcf, k1⟩ + · exact edAt_kept fun b hb hs => (ks b hb hs).trans (k0 b hb hs) + obtain ⟨fp, rt, asid, va, mp, hcs, hsl, -, -, -⟩ := pm_core hp'.1.2.1 + have h0 := hO.al0 hsl + have h8 := hO.al hsl (k := 8#u64) (by simp) (by simp) + have hq := hp'.1.2.1 + have Pb := hq.1.1.1 + have hOut : OutsideObjs st K.heap.tcbs Cs σ' p.slot := by + obtain ⟨x, hx, hxs⟩ := hq.2 + obtain ⟨Cw, hroot, hcl, hC⟩ := Pb.walk + obtain ⟨q0, r0, m0, y0, hq0, hm0, hs0⟩ := lookup_slot_cn hroot hcl hx + rw [← hxs, hs0] + exact (hC q0 r0 m0 y0 hq0 hm0).2.1 + have hlk : Locks σ p.slot := by + obtain ⟨-, -, -, -, -, -, -, -, hl, -⟩ := pm_core hp.1.2.1 + exact hl (hO.nz _ hsl) + have hown : ∀ o : U64, o.val < 32 → ownP K (L.mem σ) (wa p.slot o) = .lock p.slot := fun o ho => by + rw [← congrFun (ownDepP hK.1 hK.2 _ _ hag) _] + exact ownP_slotW hK.1 Pb hsl ho (Or.inr ⟨notTcbSlot_of hOut, nof_of_frame_slot hcs⟩) + refine ⟨fun b => b = p.slot ∨ b = wa p.slot 8#u64, ?_, ?_, ?_⟩ + · rintro b (h | h) <;> rw [h] + · have e0 := hown 0#u64 (by simp) + rw [wadd_zero (hO.fit _ hsl)] at e0 + rw [e0] + exact hlk + · rw [hown 8#u64 (by simp)] + exact hlk + · intro b hb hs hD + rw [ks b hb hs, k1 b hb hs, mem_wr_ne' h8 hb (fun e => hD (Or.inr e.symm)), + mem_wr_ne' h0 hb (fun e => hD (Or.inl e.symm))] + · intro v hM hv + have hc : W7.R.ST.capA st v p.slot = .Frame fp .Small rt false mp false asid va := by + have e1 := hv _ h0 (hS.base hsl).2 (Or.inl rfl) + have e2 := hv _ h8 (hS.word hsl (k := 8#u64) (by simp) (by simp)).2 (Or.inr rfl) + have e3 := ok_inj_ff ((W7.R.ST.capA_eq (st := st) σ' p.slot).symm.trans hcs) + unfold W7.R.ST.capA at e3 ⊢ + rw [e1, e2] + exact e3 + refine ⟨_, MdbS.set_frame hO hM hsl hc c hcf, fun b hb hs => ⟨fun hD => ?_, fun hD => ?_⟩⟩ + · rw [ks b hb hs, k1 b hb hs] + exact mem_wr2_congr h0 h8 hb (hv b hb hs hD) + · rw [mem_wr_ne' h8 hb (fun e => hD (Or.inr e.symm)), mem_wr_ne' h0 hb (fun e => hD (Or.inl e.symm))] + +/-- **The Delete-with-PTE edit form at one accepted run** (module docstring; P2-Sync gen 5: at `PrePAL`). -/ +theorem del_editL (hA : P0.ArchTotal ar) (hK : ROKP K) {e : U64 → U64} (he : EpochAP st K e) {number : I64} + {σ σ' s' : S} {a a' : A} (hp : PrePAL st Cs Ω Sg Ac Locks p K μ σ) (hp' : PrePAL st Cs Ω Sg Ac Locks p K μ σ') + (hag : ∀ x, Aligned x → x ∉ K.user → L.mem σ' x = L.mem σ x) + (hr : syscall.handle_invocation_local st ar σ' a number p = ok (true, s', a')) : + EdAt st K (Locks σ) (L.cpu σ) σ σ' s' := by + obtain ⟨T0, Ω0, Sg0, hO⟩ := he.osok + have hS := he.slotLay + obtain ⟨z1, hz1, ks⟩ := del_accSlL hA hS hp' hr + rcases hz1 with k0 | ⟨cp, cr, cg, cgb, dest, x1, y1, a1, a2, hcs, hlk, hd, k1, hes, k2⟩ + · exact edAt_kept fun b hb hs => (ks b hb hs).trans (k0 b hb hs) + -- the target's facts at σ' + obtain ⟨cp', cr', cg', cgb', dest', fp, rt, asid, va, e', Cw, hcs', hlk', hdc, -, -, -, -, -, hdO, -, hpv, hnx⟩ := + delEntP_ofL hp' + have ed := lts_det hcs hcs' hlk hlk' + rw [← ed] at hdc hdO hpv hnx + obtain ⟨cp'', cr'', cg'', cgb'', dest'', hcs'', hlk'', hHd, hHp, hHn⟩ := del_heldP hp'.2.1 + have ed' := lts_det hcs hcs'' hlk hlk'' + rw [← ed'] at hHd hHp hHn + obtain ⟨pv, hpvd⟩ : ∃ pv, L.mem σ' (wa dest 16#u64) = pv := ⟨_, rfl⟩ + obtain ⟨nx, hnxd⟩ : ∃ nx, W7.R.ST.NxA st σ' dest = nx := ⟨_, rfl⟩ + rw [cte_prev, hpvd] at hpv hHp + have hnx' : obj.Slot.next st dest σ' = ok nx := by rw [← hnxd]; exact W7.R.ST.next_eq σ' dest + have hpvK : pv ≠ 0#u64 → pv ∈ K.toKLayout.slots := fun h0 => (hpv h0).1 + have hnxK : nx ≠ 0#u64 → nx ∈ K.toKLayout.slots := fun h0 => (hnx nx hnx' h0).1 + -- the edited words + have hDw : ∀ b, EsD dest pv nx b → Aligned b ∧ SlotWd K.toKLayout b := by + rintro b (h | h | h | h | ⟨h0, h⟩ | ⟨h0, h | h⟩) <;> rw [h] + · exact hS.base hd + · exact hS.word hd (by simp) (by simp) + · exact hS.word hd (by simp) (by simp) + · exact hS.word hd (by simp) (by simp) + · exact hS.word (hpvK h0) (by simp) (by simp) + · exact hS.word (hnxK h0) (by simp) (by simp) + · exact hS.word (hnxK h0) (by simp) (by simp) + have hDa : ∀ b, EsD dest pv nx b → Aligned b := fun b hb => (hDw b hb).1 + have hpv1 : L.mem x1 (wa dest 16#u64) = pv := by + have hw := hDw _ (Or.inr (Or.inr (Or.inl rfl))) + rw [k1 _ hw.1 hw.2] + exact hpvd + have hnx1 : W7.R.ST.NxA st x1 dest = nx := by + have hw := hDw _ (Or.inr (Or.inr (Or.inr (Or.inl rfl)))) + unfold W7.R.ST.NxA + rw [k1 _ hw.1 hw.2] + exact hnxd + have Ex := es_sim hDa hpv1 hnx1 (fun _ _ => rfl) hes hes + refine ⟨EsD dest pv nx, ?_, ?_, ?_⟩ + · -- held at σ: the plan's locks, `ownP`'s slot owners + have PE' := hp'.2.1.1.1.1 + have held : ∀ y, PHeld p y → Locks σ y := fun y ⟨j, hj, hy⟩ => hp.2.1.1.2.2.2 j y hj hy + have hown : ∀ q o : U64, q ∈ K.toKLayout.slots → PHeld p q → o.val < 32 → + (16 ≤ o.val ∨ (¬ TcbSlot K.toKLayout q ∧ NullOrFrame (L.mem σ' q))) → + HeldBy (L.cpu σ) (Locks σ) (ownP K (L.mem σ) (wa q o)) := fun q o hq hh ho hw => by + rw [← congrFun (ownDepP hK.1 hK.2 _ _ hag) _, ownP_slotW hK.1 PE' hq ho hw] + exact held q hh + have hw0 : ¬ TcbSlot K.toKLayout dest ∧ NullOrFrame (L.mem σ' dest) := + ⟨notTcbSlot_of hdO, nof_of_frame_slot hdc⟩ + have hdH := hHd (hO.nz _ hd) + have hnH : nx ≠ 0#u64 → PHeld p nx := fun h0 => hHn nx hnx' h0 + rintro b (h | h | h | h | ⟨h0, h⟩ | ⟨h0, h | h⟩) <;> rw [h] + · have e0 := hown dest 0#u64 hd hdH (by simp) (Or.inr hw0) + rwa [wadd_zero (hO.fit _ hd)] at e0 + · exact hown dest 8#u64 hd hdH (by simp) (Or.inr hw0) + · exact hown dest 16#u64 hd hdH (by simp) (Or.inl (by simp)) + · exact hown dest 24#u64 hd hdH (by simp) (Or.inl (by simp)) + · exact hown pv 24#u64 (hpvK h0) (hHp h0) (by simp) (Or.inl (by simp)) + · exact hown nx 16#u64 (hnxK h0) (hnH h0) (by simp) (Or.inl (by simp)) + · exact hown nx 24#u64 (hnxK h0) (hnH h0) (by simp) (Or.inl (by simp)) + · intro b hb hs hD + rw [ks b hb hs, k2 b hb hs] + exact (Ex.2 b hb hD).2.trans (k1 b hb hs) + · intro v hMv hv + obtain ⟨G, hM1, hRO, hKd, hN, hCl⟩ := hMv + obtain ⟨w, hw, hMw⟩ := spec_ex_fd (W7.R.ST.cdt.empty_slot.mdbO (ar := ar) (a := a1) (cl := .Null) + (Pw := fun _ => False) hO hM1 hRO hKd hN hCl hd (fun _ h' => h'.elim) + fun s7 a7 _ => by rw [W7.R.ST.pcd_null]; exact (WP.spec_ok _).2 (WritesIn.refl _ _)) + have E := es_sim (v := v) hDa hpv1 hnx1 + (fun b hb => (hv b (hDw b hb).1 (hDw b hb).2 hb).trans (k1 b (hDw b hb).1 (hDw b hb).2).symm) hes hw + refine ⟨w.1, hMw, fun b hb hs => ⟨fun hD => ?_, fun hD => (E.2 b hb hD).1⟩⟩ + rw [ks b hb hs, k2 b hb hs] + exact E.1 b hD + +/-- **The Delete-with-PTE edit form at one accepted run** (module docstring). -/ +theorem del_edit (hA : P0.ArchTotal ar) (hK : ROKP K) {e : U64 → U64} (he : EpochAP st K e) {number : I64} + {σ σ' s' : S} {a a' : A} (hp : PrePA st Cs Ω Sg Ac Locks p K μ σ) (hp' : PrePA st Cs Ω Sg Ac Locks p K μ σ') + (hag : ∀ x, Aligned x → x ∉ K.user → L.mem σ' x = L.mem σ x) + (hr : syscall.handle_invocation_local st ar σ' a number p = ok (true, s', a')) : + EdAt st K (Locks σ) (L.cpu σ) σ σ' s' := + del_editL hA hK he hp.toL hp'.toL hag hr + +end edit + +/-! ## The two slots of `fl4LocPB` and the top theorem -/ + +section fl4 +variable {S : Type} (st : machine.Store S) [L : LawfulStore st] {A : Type} (ar : machine.Arch A) +variable (hA : P0.ArchTotal ar) (hm : E2E.MasksWr st) + +/-- **The PageMap slot of `fl4LocPB` for the wiring** (`pm_edit`; declines write the `IoDS` words, no slot word). -/ +theorem pmSlotP_wire (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + SlotWireP st (EpochAP st) K + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inl i)))))))))) K m hK) := by + show SlotWireP st (EpochAP st) K (pmSlotP st ar hA hm i K m hK) + unfold pmSlotP + by_cases h : PmPremP st i K m + · rw [dif_pos h] + refine ⟨fun σ σ' a r hp _ hp' g' hag hr hac => ?_, fun s x hp hx => ?_⟩ + · obtain ⟨e0, he, -⟩ := g' + obtain ⟨b, s', a'⟩ := r + change b = true at hac + subst hac + change PmPreG st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ at hp + change PmPreG st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ' at hp' + change syscall.handle_invocation_local st ar σ' a i.number i.p = ok (true, s', a') at hr + exact pm_edit hA hK he hp hp' hag hr + · have hp' : PmPreG st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s := hp + have N := io_narNS hp'.1.2.1.1.1.1 + rcases hx with rfl | rfl | rfl + · exact (N.reg hp'.1.2.1.1.1.1.tA (by simp [layout.reg.FAULT_IP])).2 + · exact (N.nd _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])).2 + · exact (N.nd _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])).2 + · rw [dif_neg h] + exact ⟨fun _ _ _ _ hp => hp.elim, fun _ _ hp => hp.elim⟩ + +/-- **The Delete-with-PTE slot of `fl4LocPB` for the wiring** (`del_edit`; declines write the `IoDS` words). -/ +theorem delSlotP_wire (i : InvArgP S) (K : KLayoutP) (m : U64 → U64) (hK : ROKP K) : + SlotWireP st (EpochAP st) K + (fl4LocPB st ar hA hm (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr (.inr i)))))))))) K m hK) := by + show SlotWireP st (EpochAP st) K (delSlotP st ar hA hm i K m hK) + unfold delSlotP + by_cases h : DelPremP st i K m + · rw [dif_pos h] + refine ⟨fun σ σ' a r hp _ hp' g' hag hr hac => ?_, fun s x hp hx => ?_⟩ + · obtain ⟨e0, he, -⟩ := g' + obtain ⟨b, s', a'⟩ := r + change b = true at hac + subst hac + change PrePA st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ at hp + change PrePA st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m σ' at hp' + change syscall.handle_invocation_local st ar σ' a i.number i.p = ok (true, s', a') at hr + exact del_edit hA hK he hp hp' hag hr + · have hp' : PrePA st i.Cs i.Ω i.Sg i.Ac i.Locks i.p K m s := hp + have N := io_narNS hp'.2.1.1.1.1 + rcases hx with rfl | rfl | rfl + · exact (N.reg hp'.2.1.1.1.1.tA (by simp [layout.reg.FAULT_IP])).2 + · exact (N.nd _ (by simp [layout.node.CUR_TIME]) (by simp [layout.node.CUR_TIME])).2 + · exact (N.nd _ (by simp [layout.node.CONSUMED]) (by simp [layout.node.CONSUMED])).2 + · rw [dif_neg h] + exact ⟨fun _ _ _ _ hp => hp.elim, fun _ _ hp => hp.elim⟩ + +/-- **`fl4LocPB` is a wiring family** (all eleven slots in edit form). -/ +theorem fl4LocPB_wire : WireFam st (EpochAP st) (fl4LocPB st ar hA hm) := + fl4LocPB_wireFam st ar hA hm (fun i K m hK _ => pmSlotP_wire st ar hA hm i K m hK) + (fun i K m hK _ => delSlotP_wire st ar hA hm i K m hK) + +/-- **P1's MDB field of the history along an execution of `fl4LocPB`** (module docstring; `hSer` is the ruled +per-serial-step hypothesis). -/ +theorem serializable_fl4_localVP_mdb {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (hMd0 : MdbSH st K0.toKLayout s0.mem) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, + ExecPA (EpochAP st) (fl4LocPB st ar hA hm) s0 K0 s K → Step s c s' → + Fl4StepPA (EpochAP st) (fl4LocPB st ar hA hm) s K c s' K' → E2E.SerStep s c → + MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA (EpochAP st) (fl4LocPB st ar hA hm) s0 K0 s K) : + ∃ H R, InvR s0.mem s H R ∧ MdbSH st K.toKLayout H ∧ (NoLoc s → H = s.mem) := + serializable_mdb_of (fl4LocPB_wire st ar hA hm) h0 hA0 hB0.rok hM0 hB0 hMd0 hSer h + +/-- **At quiescence, from Boot's `MdbST`**: a start state with the start memory's slot words and P1's `MdbST` gives +`MdbSH` of the memory at every quiescent point of the execution. -/ +theorem serializable_fl4_localVP_mdbT {s0 s : Cfg U64 U64 U64 U64} {K0 K : KLayoutP} + (h0 : Init s0) (hA0 : ArmInv s0) (hB0 : EpochAP st K0 s0.mem) (hM0 : MapsPA K0 s0) + (hT : ∃ v : S, (∀ b, Aligned b → SlotWd K0.toKLayout b → L.mem v b = s0.mem b) ∧ + W7.R.ST.MdbST st K0.toKLayout.slots v) + (hSer : ∀ {s s' : Cfg U64 U64 U64 U64} {K K' : KLayoutP} {c : U64}, + ExecPA (EpochAP st) (fl4LocPB st ar hA hm) s0 K0 s K → Step s c s' → + Fl4StepPA (EpochAP st) (fl4LocPB st ar hA hm) s K c s' K' → E2E.SerStep s c → + MdbSH st K.toKLayout s.mem → MdbSH st K'.toKLayout s'.mem) + (h : ExecPA (EpochAP st) (fl4LocPB st ar hA hm) s0 K0 s K) (hq : NoLoc s) : + MdbSH st K.toKLayout s.mem := by + obtain ⟨v, hv, hTv⟩ := hT + obtain ⟨H, R, -, hM, hH⟩ := + serializable_fl4_localVP_mdb st ar hA hm h0 hA0 hB0 hM0 ⟨v, hv, hTv.mdbS⟩ hSer h + rw [← hH hq] + exact hM + +end fl4 + +end + +end Fl4ProofsA64.P2.E2EVA diff --git a/proofs/Fl4ProofsA64/P2/ViewSafeIoAcc.lean b/proofs/Fl4ProofsA64/P2/ViewSafeIoAcc.lean index e4776578c..85bfc36b1 100644 --- a/proofs/Fl4ProofsA64/P2/ViewSafeIoAcc.lean +++ b/proofs/Fl4ProofsA64/P2/ViewSafeIoAcc.lean @@ -3,7 +3,7 @@ # P2 (line p3mc, gen 4): the I/O port path's accepted run on a narrowed guard (`io_accN`) `io_acc` with the read set `IoRS ∧ NN` and the write set `IoWS ∧ NW` for any two word predicates `NN`, `NW` that hold -on the objects the accepted run touches (`IoNar`): the core's node, the current thread's words from offset 192 on +on the objects the accepted run touches (`IoNar`): the core's node, the current thread's words from offset 224 on (written: all but `AFFINITY`), every capability slot, the kernel-global words, the current SC (its `ScAcc`, where the current thread has an SC), the earliest timer's `TIMER_DEADLINE`, the release head's `SC` field and its SC's words. `AccessIoEnt.IoAcc` / `AccessIoPath.IoMid` are generic in the sets, so `io_acc`'s proof goes through with @@ -29,8 +29,8 @@ variable {Locks : S → U64 → Prop} /-- **The words the I/O port path touches, in `NN` (read) and `NW` (written)** (module docstring). -/ structure IoNar (K : KLayout) (Z : U64 → Nat) (NN NW : U64 → Prop) (s : S) : Prop where nd : ∀ o : U64, o.val < 4288 → o.val % 8 = 0 → NN (L.nodeAt s (L.cpu s) o) ∧ NW (L.nodeAt s (L.cpu s) o) - cur : ∀ off : U64, 192 ≤ off.val → off.val < 4096 → NN (core.num.U64.wrapping_add (L.curW s) off) - curW : ∀ off : U64, 192 ≤ off.val → off.val < 4096 → off.val ≠ 3520 → + cur : ∀ off : U64, 224 ≤ off.val → off.val < 4096 → NN (core.num.U64.wrapping_add (L.curW s) off) + curW : ∀ off : U64, 224 ≤ off.val → off.val < 4096 → off.val ≠ 3520 → NW (core.num.U64.wrapping_add (L.curW s) off) slot : ∀ sl ∈ K.slots, NN sl ∧ NN (core.num.U64.wrapping_add sl 8#u64) ks : ∀ o : U64, o.val < 256 → o.val % 8 = 0 → NN (core.num.U64.wrapping_add (L.ksb s) o) diff --git a/xtask/scripts/port_p2_a64.py b/xtask/scripts/port_p2_a64.py index 9e2548ee0..efc597afe 100755 --- a/xtask/scripts/port_p2_a64.py +++ b/xtask/scripts/port_p2_a64.py @@ -3213,7 +3213,11 @@ def generate(root, mod): 'AccessIoRa': _S1B4_SLOTS, 'AccessIoRaU': _S1B4_SLOTS, 'IoOnceB': _S1B4_SLOTS, - 'ViewSafeIoAcc': _S1B4_SLOTS, + # (P2-A64X gen 30, Architect 14:29Z) `IoNar.cur` / `curW` at AArch64's 224 (TCB offsets 192–223 are slot 6), + # missed when `_S1B4_SLOTS` was applied here. + 'ViewSafeIoAcc': _S1B4_SLOTS + [('192 ≤ off.val', '224 ≤ off.val'), + ('the current thread\'s words from offset 192 on', + 'the current thread\'s words from offset 224 on')], 'AccessNbwOwn': [('(h1 : 192 ≤ k.val)', '(h1 : 224 ≤ k.val)')], 'InvBlkFin': [(' tf : ∀ k : U64, 192 ≤ k.val', ' tf : ∀ k : U64, 224 ≤ k.val'), ('the TCB fields from offset 192 and', 'the TCB fields from offset 224 (AArch64) and'), @@ -4568,18 +4572,21 @@ def PmVs (K : KLayoutP) (s : S) : Prop := variable (st) in /-- **The PageMap path's start fact with `PmEx` and `PmRoot`** (module docstring; a64: and [J] `PmVs`; gen 12: and the -[J] VCPU placements `VcKsA` / `VcTabA`, `P2/InvPmUb.lean`, in place of `GateEp`'s `K.vcpus = ∅`). -/ +[J] VCPU placements `VcKsA` / `VcTabA`, `P2/InvPmUb.lean`, in place of `GateEp`'s `K.vcpus = ∅`; P2-A64X gen 28: and +the entry's IPC-buffer placement `InvBufJ8`, last, so that `hbuf` at `PmPreG` is a lemma, `invBufJ8_pmPreG`). -/ """ MODULES.update({ 'InvPmPdw': { 'keep': ['PmEx', 'pmEx_slots', 'PmRoot', 'PmPreG'], - 'imports': {'Fl4Proofs.P2.InvPD': None, 'Fl4Proofs.P3.IpcGrant': 'Fl4ProofsA64.P2.PteReachV'}, + 'imports': {'Fl4Proofs.P2.InvPD': 'Fl4ProofsA64.P2.InvTrunc8C', + 'Fl4Proofs.P3.IpcGrant': 'Fl4ProofsA64.P2.PteReachV'}, 'text': [('\n-/\nimport ', _I7_PDW_NOTE + 'import '), ('variable (st) in\n/-- **The PageMap path\'s start fact with `PmEx` and `PmRoot`** (module docstring). -/\n', _I7_PMVS), (' PmPreF st Cs Ω Sg Ac Locks p K m s ∧ PmEx st p s ∧ PmRoot st p s\n', ' PmPreF st Cs Ω Sg Ac Locks p K m s ∧ PmEx st p s ∧ PmRoot st p s ∧ PmVs st K s ∧ -- a64: [J] `PmVs`\n' - ' VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV -- a64 (gen 12): [J] the VCPU placements\n')], + ' VcKsA K.toKLayoutV ∧ VcTabA K.toKLayoutV ∧ -- a64 (gen 12): [J] the VCPU placements\n' + ' InvBufJ8 st K.toKLayout s -- a64 (P2-A64X gen 28): the entry\'s IPC-buffer placement\n')], 're': [_I7_CR3], }, 'InvPmRoot': {