{"text": "Require Import Axioms CoqlibC Maps Iteration (* Errors *).\nRequire Import Integers Floats AST Linking.\nRequire Import ValuesC Memory Events Globalenvs Smallstep.\nRequire Import Machregs LocationsC Conventions Op LinearC.\nRequire Import Debugvar.\n(** newly added **)\nRequire Export Debugvarproof.\nRequire Import Simulation.\nRequire Import Skeleton Mod ModSem SimMod SimModSem SimSymb SimMem AsmregsC MatchSimModSem.\nRequire SimMemId.\nRequire SoundTop.\nRequire Import LiftDummy.\n\nSet Implicit Arguments.\n\n\n\nDefinition strong_wf_tgt (st_tgt0: Linear.state): Prop :=\n exists sg_init ls_init, last_option (LinearC.get_stack st_tgt0) = Some (Linear.dummy_stack sg_init ls_init).\n\nSection SIMMODSEM.\n\nVariable skenv_link: SkEnv.t.\nVariable sm_link: SimMem.t.\nVariables prog tprog: program.\nLet md_src: Mod.t := (LinearC.module prog).\nLet md_tgt: Mod.t := (LinearC.module tprog).\nHypothesis (INCLSRC: SkEnv.includes skenv_link (Mod.sk md_src)).\nHypothesis (INCLTGT: SkEnv.includes skenv_link (Mod.sk md_tgt)).\nHypothesis (WF: SkEnv.wf skenv_link).\nHypothesis TRANSL: match_prog prog tprog.\nLet ge := (SkEnv.revive (SkEnv.project skenv_link (Mod.sk md_src)) prog).\nLet tge := (SkEnv.revive (SkEnv.project skenv_link (Mod.sk md_tgt)) tprog).\nDefinition msp: ModSemPair.t := ModSemPair.mk (md_src skenv_link) (md_tgt skenv_link) (SimSymbId.mk md_src md_tgt) sm_link.\n\nInductive match_states\n (idx: nat) (st_src0: Linear.state) (st_tgt0: Linear.state) (sm0: SimMem.t): Prop :=\n| match_states_intro\n (MATCHST: Debugvarproof.match_states st_src0 st_tgt0)\n (MCOMPATSRC: (get_mem st_src0) = sm0.(SimMem.src))\n (MCOMPATTGT: (get_mem st_tgt0) = sm0.(SimMem.tgt))\n (DUMMYTGT: strong_wf_tgt st_tgt0).\n\nTheorem make_match_genvs :\n SimSymbId.sim_skenv (SkEnv.project skenv_link (Mod.sk md_src))\n (SkEnv.project skenv_link (Mod.sk md_tgt)) ->\n Genv.match_genvs (match_globdef (fun _ f tf => transf_fundef f = Errors.OK tf) eq prog) ge tge.\nProof. subst_locals. eapply SimSymbId.sim_skenv_revive; eauto. Qed.\n\nTheorem sim_modsem: ModSemPair.sim msp.\nProof.\n eapply match_states_sim with (match_states := match_states) (match_states_at := top4) (sound_state := SoundTop.sound_state);\n eauto; ii; ss.\n - instantiate (1:= Nat.lt). apply lt_wf.\n - eapply SoundTop.sound_state_local_preservation.\n - (* init bsim *)\n inv INITTGT. destruct sm_arg; ss. clarify.\n inv SIMARGS; ss. clarify. inv TYP.\n exploit (fill_arguments_progress ls_init (typify_list vs_tgt (sig_args (fn_sig fd)))\n (Conventions1.loc_arguments (fn_sig fd))); eauto.\n { apply (f_equal (@length _)) in TYP0. rewrite map_length in *. etrans; try apply TYP0; eauto. }\n intro P; des.\n hexploit (@fill_arguments_spec); et. intro Q; des.\n exploit make_match_genvs; eauto. { apply SIMSKENV. } intro SIMGE. des.\n eexists. eexists (SimMemId.mk _ _). esplits; ss; cycle 1.\n + econs; eauto; ss.\n * rpapply match_states_call.\n { econs; eauto.\n - instantiate (1:= (dummy_stack (fn_sig fd) ls_init)). unfold dummy_stack, dummy_function.\n rpapply match_stackframe_intro; eauto; swap 1 2.\n + econs; eauto.\n + econs; eauto. ss. econs; eauto. econs; eauto.\n + ss. f_equal; ss.\n * instantiate (1:= None). instantiate (1:= None). ss.\n * instantiate (1:= None). instantiate (1:= None). ss.\n - econs; et.\n }\n * rr. esplits; ss; eauto.\n + inv SAFESRC. folder. inv TYP.\n exploit (Genv.find_funct_transf_partial_genv SIMGE); eauto. intro FINDFSRC; des; clarify.\n unfold transf_fundef; ss. unfold Errors.bind in *. des_ifs.\n econs; swap 1 3.\n { folder; ss. eauto. }\n all: ss.\n unfold transf_function in *. des_ifs.\n - (* init progress *)\n des. inv SAFESRC. inv SIMARGS; ss.\n exploit make_match_genvs; eauto. { apply SIMSKENV. } intro SIMGE.\n exploit (Genv.find_funct_match_genv SIMGE); eauto. i; des. ss. clarify. folder.\n inv TYP. unfold Errors.bind in *. des_ifs.\n destruct sm_arg; ss. clarify.\n exploit (Genv.find_funct_match_genv SIMGE); eauto. i; des. ss. clarify.\n exploit (fill_arguments_progress (Locmap.init Vundef) (typify_list vs_tgt (sig_args (fn_sig fd)))\n (Conventions1.loc_arguments (fn_sig fd))); eauto.\n { apply (f_equal (@length _)) in TYP0. rewrite map_length in *. etrans; try apply TYP0; eauto. }\n intro P; des.\n hexploit (fill_arguments_spec); eauto. intro Q; des.\n assert(SGEQ: fn_sig fd = fn_sig f).\n { unfold transf_function in *; des_ifs. }\n esplits; eauto. econs; swap 1 3.\n { ss. folder. eauto. }\n all: ss; eauto with congruence.\n + econs; ss; eauto with congruence.\n - (* call wf *)\n inv MATCH; ss. destruct sm0; ss. clarify.\n inv CALLSRC. inv MATCHST; ss.\n - (* call fsim *)\n inv MATCH; ss. destruct sm0; ss. clarify.\n inv CALLSRC. inv MATCHST; ss.\n folder. esplits; eauto.\n + econs; eauto.\n * folder. des.\n r in TRANSL. r in TRANSL.\n exploit (SimSymbId.sim_skenv_revive TRANSL); eauto.\n { apply SIMSKENV. }\n intro GE.\n apply (fsim_external_funct_id GE); ss.\n + econs; ss; eauto.\n * instantiate (1:= SimMemId.mk _ _). ss.\n * ss.\n + ss.\n - (* after fsim *)\n inv AFTERSRC. inv SIMRET; ss. exists sm_ret. destruct sm_ret; ss. clarify.\n inv MATCH; ss. inv MATCHST; ss. esplits; eauto.\n + econs; eauto.\n + econs; ss; eauto. econs; eauto.\n { inv H5; ss. - econs; eauto. - des_ifs. econs; eauto. inv H. econs; eauto. }\n { clear - DUMMYTGT. unfold strong_wf_tgt in *. des. destruct ts; ss. unfold dummy_stack, dummy_function in *. des_ifs; ss; clarify; esplits; et. }\n - (* final fsim *)\n inv MATCH. inv FINALSRC; inv MATCHST; ss.\n inv H3; ss. inv H4; ss. rr in DUMMYTGT. des. ss. clarify. destruct sm0; ss. clarify.\n eexists (SimMemId.mk _ _). esplits; ss; eauto.\n econs; ss; eauto. inv H1; ss. inv H2; ss.\n - left; i. esplits; eauto.\n { apply modsem_receptive; et. }\n inv MATCH.\n ii. rr in STEPSRC. des. hexploit (@transf_step_correct prog skenv_link); eauto.\n { inv SIMSKENV. ss. }\n { apply make_match_genvs; eauto. apply SIMSKENV. }\n i; des.\n exploit (lift_plus Linear.step (fun st => get_stack st <> []) strong_wf_tgt); ss; et.\n { intros st X Y. rr in X. des. rewrite Y in *. ss. }\n { i. folder. unfold strong_wf_tgt in *. des. inv HSTEP; ss; eauto.\n - des_ifs; ss; eauto.\n - des_ifs; ss; eauto. right. ii. inv HSTEP0; ss. }\n { ii. unfold strong_wf_tgt in *; des. inv HSTEP; try inv STACKS; ss; clarify; et; des_ifs; et. }\n { intro T. inv H0; ss; clarify; try inv STACKS; ss; try inv H1; ss. }\n intro TT; des.\n esplits; eauto.\n * left. eapply ModSemProps.spread_dplus; eauto.\n { eapply modsem_determinate; eauto. }\n * instantiate (1:= SimMemId.mk _ _). econs; ss.\nUnshelve.\n all: ss; try (by econs).\nQed.\n\nEnd SIMMODSEM.\n\n\n\n\nSection SIMMOD.\n\nVariables prog tprog: program.\nHypothesis TRANSL: match_prog prog tprog.\nDefinition mp: ModPair.t := SimSymbId.mk_mp (LinearC.module prog) (LinearC.module tprog).\n\nTheorem sim_mod: ModPair.sim mp.\nProof.\n econs; ss.\n - r. eapply Sk.match_program_eq; eauto.\n ii. destruct f1; ss.\n + clarify. right. unfold Errors.bind, transf_function in *. des_ifs. esplits; eauto.\n + clarify. left. esplits; eauto.\n - ii. inv SIMSKENVLINK. eapply sim_modsem; eauto.\nQed.\n\nEnd SIMMOD.\n", "meta": {"author": "snu-sf", "repo": "CompCertM", "sha": "1bf2113b2381df604a3abcce7711af1f154d1620", "save_path": "github-repos/coq/snu-sf-CompCertM", "path": "github-repos/coq/snu-sf-CompCertM/CompCertM-1bf2113b2381df604a3abcce7711af1f154d1620/backend/DebugvarproofC.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. NO\n\n", "lm_q1_score": 0.523420348936324, "lm_q2_score": 0.1895210913296776, "lm_q1q2_score": 0.09919919575457278}}
{"text": "\n\n(* Mostly taken form https://github.com/sigurdschneider/lvc/blob/23b7fa8cd0640503ff370144fb407192632f9cc6/Infra/AutoIndTac.v *)\n\n(* fail 1 will break from the 'match H with', and indicate to\n the outer match that it should consider finding another \n hypothesis, see documentation on match goal and fail\n This tactic is a variation of Tobias Tebbi's revert_except_until *)\n\nLtac revert_all :=\n repeat match goal with [ H : _ |- _ ] => revert H end.\n\nTactic Notation \"revert\" \"all\" := revert_all.\n\nLtac revert_except i :=\n repeat match goal with [ H : _ |- _ ] => tryif unify H i then fail else revert H end.\n \nTactic Notation \"revert\" \"all\" \"except\" ident(i) := revert_except i.\n\nLtac clear_except i :=\n repeat match goal with [ H : _ |- _ ] => tryif unify H i then fail else clear H end.\n \nTactic Notation \"clear\" \"all\" \"except\" ident(i) := clear_except i.\n\nLtac clear_all :=\n repeat match goal with\n [H : _ |- _] => clear H\n end.\n\n\n(*\n(* succeed if H has a function type, fail otherwise *)\nLtac is_ftype H := \n let t := type of H in\n let t' := eval cbv in t in\n match t' with\n | _ -> _ => idtac\n end.\n*)\n(* match on the type of E and remember each of it's arguments\n that is not a variable by calling tac.\n tac needs to do a remember exactly if x is not a var, and \n fail otherwise. (We need to fail, otherwise the repeat will\n stop prematurely) \n try will silently ignore a fail 0, but will fail if a fail 1 or \n above occurs. Sequentialization makes sure fail 1 is executed \n if is_var is successful, hence try (is_var x; fail 1) will\n fail exactly when x is a var. *)\n\nLtac remember_arguments E :=\n let tac x := (try (is_var x; fail 1); (*try (is_ftype x; fail 1);*) remember (x)) in\n repeat (match type of E with\n | ?t ?x _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ _ => tac x\n | ?t ?x _ _ _ _ => tac x\n | ?t ?x _ _ _ => tac x\n | ?t ?x _ _ => tac x\n | ?t ?x _ => tac x\n | ?t ?x => tac x\n end).\n\n(* from Coq.Program.Tactics *)\nLtac clear_dup :=\n match goal with\n | [ H : ?X |- _ ] =>\n match goal with\n | [ H' : ?Y |- _ ] =>\n match H with\n | H' => fail 2\n | _ => unify X Y ; (clear H' || clear H)\n end\n end\n end.\n\nLtac inv_eqs :=\n repeat (match goal with\n | [ H : @eq _ ?x ?x |- _ ] => fail (* nothing to do on x = x *)\n | [ H : @eq _ ?x ?y |- _ ] => progress (inversion H; subst; try clear_dup)\n end).\n\n(* this is a standard tactic *)\nLtac clear_trivial_eqs :=\n repeat (progress (match goal with\n | [ H : @eq _ ?x ?x |- _ ] => clear H\n end)).\n\nTactic Notation \"general\" \"induction\" hyp(H) :=\n remember_arguments H; revert_except H; \n induction H; intros; (try inv_eqs); (try clear_trivial_eqs).\n\n(* Module Test. *)\n(* Require Import List. *)\n\n(* Inductive decreasing : list nat -> Prop := *)\n(* | base : decreasing nil *)\n(* | step m n L : decreasing (n::L) -> n <= m -> decreasing (m :: n :: L). *)\n\n(* Lemma all_zero_by_hand L *)\n(* : decreasing (0::L) -> forall x, In x L -> x = 0. *)\n(* Proof. *)\n(* intros. remember (0::L). *)\n(* revert dependent L. revert x. induction H; intros. *)\n(* inversion Heql. *)\n(* inversion Heql. subst. inversion H0; subst; firstorder. *)\n(* Qed. *)\n\n(* Lemma all_zero L *)\n(* : decreasing (0::L) -> forall x, In x L -> x = 0. *)\n(* Proof. *)\n(* intros. general induction H. *)\n(* inversion H0; subst; firstorder. *)\n(* Qed. *)\n(* End Test. *)\n", "meta": {"author": "uds-psl", "repo": "base-library", "sha": "d9f3b8abf379d4c12049dd25c8d1fdf1973dab48", "save_path": "github-repos/coq/uds-psl-base-library", "path": "github-repos/coq/uds-psl-base-library/base-library-d9f3b8abf379d4c12049dd25c8d1fdf1973dab48/Tactics/AutoIndTac.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.43782348444346736, "lm_q2_score": 0.22541662103868043, "lm_q1q2_score": 0.09869269047462768}}
{"text": "Require Export P04.\n\n\n\n(** **** Problem #2 : 1 star, optional (hd_opt_poly) *)\n(** Complete the definition of a polymorphic version of the\n [hd_opt] function from the last chapter. Be sure that it\n passes the unit tests below. *)\n\nDefinition hd_opt {X : Type} (l : list X) : option X :=\n match l with\n | nil => None\n | a :: l' => Some a\n end.\n \n\n(** Once again, to force the implicit arguments to be explicit,\n we can use [@] before the name of the function. *)\n\nCheck @hd_opt.\n\nExample test_hd_opt1 : hd_opt [1;2] = Some 1.\nProof. reflexivity. Qed.\n\nExample test_hd_opt2 : hd_opt [[1];[2]] = Some [1].\nProof. reflexivity. Qed.\n\n", "meta": {"author": "tinkerrobot", "repo": "Software_Foundations_Solutions2", "sha": "c88b2445a3c06bba27fb97f939a8070b0d2713e6", "save_path": "github-repos/coq/tinkerrobot-Software_Foundations_Solutions2", "path": "github-repos/coq/tinkerrobot-Software_Foundations_Solutions2/Software_Foundations_Solutions2-c88b2445a3c06bba27fb97f939a8070b0d2713e6/assignments/03/P05.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.4649015713733885, "lm_q2_score": 0.21206880435710534, "lm_q1q2_score": 0.09859112038489397}}
{"text": "Require Import SpecDeps.\nRequire Import RData.\nRequire Import EventReplay.\nRequire Import MoverTypes.\nRequire Import Constants.\nRequire Import CommonLib.\nRequire Import TableDataOpsIntro.Spec.\nRequire Import TableDataOpsRef1.Spec.\n\nLocal Open Scope Z_scope.\n\nSection Spec.\n\n Definition table_unmap2_spec (g_rd: Pointer) (map_addr: Z64) (level: Z64) (adt: RData) : option (RData * Z64) :=\n match map_addr, level with\n | VZ64 map_addr, VZ64 level =>\n rely is_int64 map_addr; rely (level >=? 3); rely GRANULE_ALIGNED map_addr; rely is_int64 level;\n let idx0 := __addr_to_idx map_addr 0 in\n let idx1 := __addr_to_idx map_addr 1 in\n let idx2 := __addr_to_idx map_addr 2 in\n let idx3 := __addr_to_idx map_addr 3 in\n let ret_idx := (if level =? 1 then idx0 else if level =? 2 then idx1 else if level =? 3 then idx2 else idx3) in\n rely (peq (base g_rd) ginfo_loc);\n rely prop_dec ((buffer (priv adt)) @ SLOT_RD = None);\n rely prop_dec ((buffer (priv adt)) @ SLOT_TABLE = None);\n let rd_gidx := (offset g_rd) in\n let grd := (gs (share adt)) @ rd_gidx in\n rely (g_tag (ginfo grd) =? GRANULE_STATE_RD);\n rely prop_dec (glock grd = Some CPU_ID);\n let root_gidx := (g_rtt (gnorm grd)) in\n rely is_gidx rd_gidx; rely is_gidx root_gidx;\n when adt == query_oracle adt;\n let adt := adt {log: EVT CPU_ID (RTT_WALK root_gidx map_addr 2) :: log adt} in\n (* hold root lock *)\n let groot := (gs (share adt)) @ root_gidx in\n rely (tbl_level (gaux groot) =? 0);\n rely prop_dec (glock groot = None);\n rely (g_tag (ginfo groot) =? GRANULE_STATE_TABLE);\n rely (gtype groot =? GRANULE_STATE_TABLE);\n (* walk deeper root *)\n let entry0 := (g_data (gnorm groot)) @ idx0 in\n rely is_int64 entry0;\n let phys0 := __entry_to_phys entry0 3 in\n let lv1_gidx := __addr_to_gidx phys0 in\n if (__entry_is_table entry0) && (GRANULE_ALIGNED phys0) && (is_gidx lv1_gidx) then\n (* level 1 valid, hold level 1 lock *)\n let glv1 := (gs (share adt)) @ lv1_gidx in\n rely prop_dec (glock glv1 = None);\n rely (tbl_level (gaux glv1) =? 1);\n (* walk deeper level 1 *)\n rely (g_tag (ginfo glv1) =? GRANULE_STATE_TABLE);\n rely (gtype glv1 =? GRANULE_STATE_TABLE);\n let entry1 := (g_data (gnorm glv1)) @ idx1 in\n rely is_int64 entry1;\n let phys1 := __entry_to_phys entry1 3 in\n let lv2_gidx := __addr_to_gidx phys1 in\n if (__entry_is_table entry1) && (GRANULE_ALIGNED phys1) && (is_gidx lv2_gidx) then\n (* level 2 valid, hold level 2 lock *)\n let glv2 := (gs (share adt)) @ lv2_gidx in\n rely (tbl_level (gaux glv2) =? 2);\n rely prop_dec (glock glv2 = None);\n if level =? 3 then\n (* walk until level 2 *)\n let adt := adt {priv: (priv adt) {wi_llt: lv2_gidx} {wi_index: idx2}} in\n unmap_table lv2_gidx idx2 level map_addr adt\n else\n (* walk deeper level 2 *)\n rely (g_tag (ginfo glv2) =? GRANULE_STATE_TABLE);\n rely (gtype glv2 =? GRANULE_STATE_TABLE);\n let entry2 := (g_data (gnorm glv2)) @ idx2 in\n rely is_int64 entry2;\n let phys2 := __entry_to_phys entry2 3 in\n let lv3_gidx := __addr_to_gidx phys2 in\n if (__entry_is_table entry2) && (GRANULE_ALIGNED phys2) && (is_gidx lv3_gidx) then\n (* level 2 valid, hold level 2 lock *)\n let adt := adt {log: EVT CPU_ID (REL lv2_gidx glv2 {glock: Some CPU_ID}) :: EVT CPU_ID (ACQ lv3_gidx) :: log adt} in\n let glv3 := (gs (share adt)) @ lv3_gidx in\n rely prop_dec (glock glv3 = None);\n rely (tbl_level (gaux glv3) =? 3);\n if level =? 4 then\n (* walk until level 3 *)\n let adt := adt {priv: (priv adt) {wi_llt: lv3_gidx} {wi_index: idx3}} in\n unmap_table lv3_gidx idx3 level map_addr adt\n else (* can't be other level *)\n None\n else\n (* level 3 invalid *)\n Some (adt {log: EVT CPU_ID (REL lv2_gidx glv2 {glock: Some CPU_ID}) :: log adt}\n {priv: (priv adt) {wi_llt: 0} {wi_index: ret_idx}}, VZ64 1)\n else\n (* level 2 invalid *)\n Some (adt {priv: (priv adt) {wi_llt: 0} {wi_index: ret_idx}}, VZ64 1)\n else\n (* level 1 invalid *)\n Some (adt {priv: (priv adt) {wi_llt: 0} {wi_index: ret_idx}}, VZ64 1)\n end.\n\nEnd Spec.\n\n", "meta": {"author": "columbia", "repo": "osdi-paper196-ae", "sha": "6df8f9e5b7b1e508a22327fd4a8c0c9be6c56496", "save_path": "github-repos/coq/columbia-osdi-paper196-ae", "path": "github-repos/coq/columbia-osdi-paper196-ae/osdi-paper196-ae-6df8f9e5b7b1e508a22327fd4a8c0c9be6c56496/proof/TableDataOpsRef2/Specs/table_unmap2.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. NO\n\n", "lm_q1_score": 0.5039061705290805, "lm_q2_score": 0.19436780635202988, "lm_q1q2_score": 0.09794313697298926}}
{"text": "(* SPDX-License-Identifier: GPL-2.0 *)\nRequire Import Coqlib.\nRequire Import Maps.\nRequire Import Integers.\nRequire Import Values.\nRequire Import GenSem.\nRequire Import Maps.\nRequire Import Integers.\nRequire Import Values.\nRequire Import RealParams.\nRequire Import GenSem.\nRequire Import Clight.\nRequire Import CDataTypes.\nRequire Import Ctypes.\nRequire Import PrimSemantics.\nRequire Import CompatClightSem.\nRequire Import liblayers.compcertx.Stencil.\nRequire Import liblayers.compat.CompatLayers.\nRequire Import liblayers.compat.CompatGenSem.\n\nRequire Import TrapHandlerRaw.Layer.\nRequire Import RData.\nRequire Import Constants.\nRequire Import HypsecCommLib.\nRequire Import AbstractMachine.Spec.\nRequire Import TrapHandlerRaw.Spec.\nRequire Import TrapDispatcher.Spec.\nRequire Import FaultHandler.Spec.\nRequire Import MemHandler.Spec.\nRequire Import MmioOps.Spec.\nRequire Import BootOps.Spec.\nRequire Import MemoryOps.Spec.\nRequire Import MemManager.Spec.\nRequire Import NPTWalk.Spec.\nRequire Import NPTOps.Spec.\nRequire Import MmioSPTWalk.Spec.\nRequire Import MmioSPTOps.Spec.\n\nLocal Open Scope Z_scope.\n\nSection HighPTSpec.\n\n Fixpoint local_mmap_loop_h (n: nat) (gfn: Z) (pfn: Z) (level: Z) (pte: Z) (pt': ZMap.t (Z*Z*Z)) :=\n match n with\n | O => (gfn, pfn, pt')\n | S m =>\n match local_mmap_loop_h m gfn pfn level pte pt' with\n | (gfn', pfn', pt0) => (gfn'+1, pfn'+1, ZMap.set gfn' (pfn', level, pte) pt0)\n end\n end.\n\n Definition local_mmap_h (vmid: Z) (addr: Z) (level: Z) (pte: Z) (npt: NPT) :=\n if level =? 2 then\n let gfn := addr / PAGE_SIZE / PTRS_PER_PMD * PTRS_PER_PMD in\n let pfn := (phys_page pte) / PAGE_SIZE in\n let pgd_next' := (if (pgd_index addr) @ (pgd_t npt) then (pt_pgd_next npt) else (pt_pgd_next npt) + PAGE_SIZE) in\n let pud_next' := (if (pud_index addr) @ ((pgd_index addr) @ (pud_t npt))\n then (pt_pud_next npt) else (pt_pud_next npt) + PAGE_SIZE) in\n if (pgd_next' <=? pud_start vmid) && (pud_next' <=? pmd_start vmid) then\n match local_mmap_loop_h (Z.to_nat PTRS_PER_PMD) gfn pfn 2 pte (pt npt) with\n | (_, _, pt') =>\n let npt' := npt {pt: pt'} {pgd_t: ZMap.set (pgd_index addr) true (pgd_t npt)}\n {pud_t: (pud_t npt) # (pgd_index addr) == (((pgd_index addr) @ (pud_t npt)) # (pud_index addr) == true)}\n {pmd_t: (pmd_t npt) # (pgd_index addr) ==\n (((pgd_index addr) @ (pmd_t npt)) # (pud_index addr) ==\n (((pud_index addr) @ ((pgd_index addr) @ (pmd_t npt))) # (pmd_index addr) == false))}\n {pt_pgd_next: pgd_next'} {pt_pud_next: pud_next'}\n in\n (false, npt')\n end\n else (true, npt)\n else\n let gfn := addr / PAGE_SIZE in\n let pfn := (phys_page pte) / PAGE_SIZE in\n match gfn @ (pt npt) with\n | (pfn0, level0, pte0) =>\n if level0 =? 2 then (true, npt)\n else\n let pgd_next' := (if (pgd_index addr) @ (pgd_t npt) then (pt_pgd_next npt) else (pt_pgd_next npt) + PAGE_SIZE) in\n let pud_next' := (if (pud_index addr) @ ((pgd_index addr) @ (pud_t npt))\n then (pt_pud_next npt) else (pt_pud_next npt) + PAGE_SIZE) in\n let pmd_next' := (if (pmd_index addr) @ ((pud_index addr) @ ((pgd_index addr) @ (pmd_t npt)))\n then (pt_pmd_next npt) else (pt_pmd_next npt) + PAGE_SIZE) in\n if (pgd_next' <=? pud_start vmid) && (pud_next' <=? pmd_start vmid) && (pmd_next' <=? pool_end vmid) then\n match local_mmap_loop_h 1 gfn pfn 3 pte (pt npt) with\n | (_, _, pt') =>\n let npt' := npt {pt: pt'}\n {pgd_t: (pgd_t npt) # (pgd_index addr) == true}\n {pud_t: (pud_t npt) # (pgd_index addr) ==\n (((pgd_index addr) @ (pud_t npt)) # (pud_index addr) == true)}\n {pmd_t: (pmd_t npt) # (pgd_index addr) ==\n (((pgd_index addr) @ (pmd_t npt)) # (pud_index addr) ==\n (((pud_index addr) @ ((pgd_index addr) @ (pmd_t npt))) # (pmd_index addr) == true))}\n {pt_pgd_next: pgd_next'} {pt_pud_next: pud_next'} {pt_pmd_next: pmd_next'}\n in\n (false, npt')\n end\n else(true, npt)\n end.\n\n Definition local_spt_map_h (cbndx: Z) (index: Z) (addr: Z) (pte: Z) (spt: SPT) :=\n let gfn := addr / PAGE_SIZE in\n let pfn := (phys_page pte) / PAGE_SIZE in\n let ttbr := SMMU_TTBR index cbndx in\n let pt := ttbr @ (spt_pt spt) in\n match gfn @ pt with\n | (pfn0, pte0) =>\n let pgd_next' := (if (stage2_pgd_index addr) @ (ttbr @ (spt_pgd_t spt)) then (spt_pgd_next spt) else (spt_pgd_next spt) + PAGE_SIZE) in\n let pmd_next' := (if (pmd_index addr) @ ((stage2_pgd_index addr) @ (ttbr @ (spt_pmd_t spt)))\n then (spt_pmd_next spt) else (spt_pmd_next spt) + PAGE_SIZE) in\n if (pgd_next' <=? SMMU_PMD_START) && (pmd_next' <=? SMMU_POOL_END) then\n let spt' := spt {spt_pt: (spt_pt spt) # ttbr == (pt # gfn == (pfn, pte))}\n {spt_pgd_t: (spt_pgd_t spt) # ttbr == ((ttbr @ (spt_pgd_t spt)) # (stage2_pgd_index addr) == true)}\n {spt_pmd_t: (spt_pmd_t spt) # ttbr ==\n ((ttbr @ (spt_pmd_t spt)) # (stage2_pgd_index addr) ==\n (((stage2_pgd_index addr) @ (ttbr @ (spt_pmd_t spt))) # (pmd_index addr) == true))}\n {spt_pgd_next: pgd_next'} {spt_pmd_next: pmd_next'}\n in\n (false, spt')\n else (true, spt)\n end.\n\n Lemma in_range_n gfn gfn0 n :\n {gfn <= gfn0 < gfn + n} + {~ gfn <= gfn0 < gfn + n}.\n Proof.\n destruct (zle gfn gfn0);\n destruct (zlt gfn0 (gfn + n));\n try omega; try (left; omega); try (right; red; intro; omega).\n Qed.\n\n Lemma local_mmap_level2:\n forall vmid addr pte npt npt',\n local_mmap_h vmid addr 2 pte npt = (false, npt') ->\n let gfn := (addr / PAGE_SIZE / PTRS_PER_PMD * PTRS_PER_PMD) in\n forall gfn0, if in_range_n gfn gfn0 512 then gfn0 @ (pt npt') = (phys_page pte / PAGE_SIZE + gfn0 - gfn, 2, pte)\n else gfn0 @ (pt npt') = gfn0 @ (pt npt).\n Proof.\n intros. unfold local_mmap_h in H. simpl_bool_eq.\n repeat simpl_hyp H; inv H. simpl.\n assert(Hloop: forall n gfn pfn level pte npt gfn' pfn' npt' (Hloop: local_mmap_loop_h n gfn pfn level pte npt = (gfn', pfn', npt')),\n gfn' = gfn + (Z.of_nat n) /\\ pfn' = pfn + (Z.of_nat n) /\\\n forall gfn0, if in_range_n gfn gfn0 (Z.of_nat n) then gfn0 @ npt' = (pfn + gfn0 - gfn, level, pte)\n else gfn0 @ npt' = gfn0 @ npt).\n { clear C0. induction n. intros. simpl in *. inv Hloop. split_and; try omega.\n intros. destruct in_range_n. omega. reflexivity.\n intros. simpl in Hloop. repeat simpl_hyp Hloop; inversion Hloop.\n rewrite Nat2Z.inj_succ, succ_plus_1. apply IHn in C0. destruct C0 as (? & ? & ?).\n split_and; try omega. intros. pose proof (H4 gfn2). repeat destruct in_range_n; try omega.\n assert_gso. omega. rewrite (ZMap.gso _ _ Hneq). assumption.\n assert(gfn2 = z1) by omega. rewrite H6. rewrite ZMap.gss.\n assert(z2 = pfn + z1 - gfn1) by omega. rewrite H7. reflexivity.\n assert(gfn2 <> z1) by omega. rewrite (ZMap.gso _ _ H6). assumption. }\n apply Hloop in C0. destruct C0 as (? & ? & ?). apply H1.\n Qed.\n\n Lemma local_mmap_level3:\n forall vmid addr pte npt npt',\n local_mmap_h vmid addr 3 pte npt = (false, npt') ->\n (pt npt') = (pt npt) # (addr / PAGE_SIZE) == (phys_page pte / PAGE_SIZE, 3, pte).\n Proof.\n intros. unfold local_mmap_h in H. replace (3 =? 2) with false in H by reflexivity.\n repeat simpl_hyp H; try inv H. simpl in C3. inv C3. reflexivity.\n Qed.\n\n Lemma local_spt_map_pt:\n forall cbndx index addr pte spt spt',\n local_spt_map_h cbndx index addr pte spt = (false, spt') ->\n (spt_pt spt') = (spt_pt spt) # (SMMU_TTBR index cbndx) == (((SMMU_TTBR index cbndx) @ (spt_pt spt)) # (addr / PAGE_SIZE) == (phys_page pte / PAGE_SIZE, pte)).\n Proof.\n intros. unfold local_spt_map_h in H.\n repeat simpl_hyp H; try inv H. reflexivity.\n Qed.\n\n Opaque local_mmap_h local_spt_map_h.\n\nEnd HighPTSpec.\n\nSection ConcurrentSpec.\n\n Definition local_map_host addr sdt :=\n let ptid := NPT_ID in\n let pfn := addr / PAGE_SIZE in\n let page := pfn @ (s2page sdt) in\n if (s2_owner page =? INVALID) || (s2_owner page =? HOSTVISOR) || (s2_count page >? 0) then\n let pte := (if s2_owner page =? INVALID then Z.lor (pfn * PAGE_SIZE) (Z.lor PAGE_S2_DEVICE S2_RDWR)\n else Z.lor (pfn * PAGE_SIZE) PAGE_S2_KERNEL) in\n match local_mmap_h HOSTVISOR addr 3 pte (HOSTVISOR @ (npts sdt)) with\n | (halt', npt') =>\n if halt' then\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'} {hlock: ((hlock sdt) # S2PAGE_ID == false) # ptid == false}, true,\n Some (s2page sdt), Some npt')\n else\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'}, false,\n Some (s2page sdt), Some npt')\n end\n else (sdt {hlock: (hlock sdt) # S2PAGE_ID == false}, true, Some (s2page sdt), None).\n\n Definition local_clear_vm_page vmid pfn sdt :=\n let s2p := s2page sdt in\n let fmem := flatmem sdt in\n let page := pfn @ s2p in\n let npt := HOSTVISOR @ (npts sdt) in\n if s2_owner page =? vmid then\n match pfn @ (pt npt) with\n | (pfn', level, pte) =>\n match (if pte =? 0 then (false, npt)\n else local_mmap_h HOSTVISOR (pfn * PAGE_SIZE) 3 PAGE_GUEST npt) with\n | (halt', npt') =>\n if halt' then\n (sdt {hlock: ((hlock sdt) # S2PAGE_ID == false) # NPT_ID == false} {npts: (npts sdt) # HOSTVISOR == npt'},\n true, Some s2p, Some npt', None)\n else\n let page' := mkS2Page HOSTVISOR 0 (pfn + SMMU_HOST_OFFSET) in\n let s2p' := s2p # pfn == page' in\n let fmem' := fmem # pfn == 0 in\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'} {flatmem: fmem'},\n false, Some s2p', Some npt', Some fmem')\n end\n end\n else\n (sdt, false, Some s2p, None, None).\n\n Definition local_assign_pfn_to_vm vmid gfn pfn (dorc: Z -> Z) logn sdt :=\n let s2p := s2page sdt in\n let npt := HOSTVISOR @ (npts sdt) in\n let fmem := flatmem sdt in\n let page := pfn @ s2p in\n if s2_owner page =? HOSTVISOR then\n if s2_count page =? 0 then\n let s2p' := s2p # pfn == (mkS2Page vmid 0 gfn) in\n let fmem' := fmem # pfn == (dorc logn) in\n match pfn @ (pt npt) with\n | (pfn', level, pte) =>\n match (if pte =? 0 then (false, npt)\n else local_mmap_h HOSTVISOR (pfn * PAGE_SIZE) 3 PAGE_GUEST npt) with\n | (halt', npt') =>\n if halt' then\n (sdt {hlock: ((hlock sdt) # S2PAGE_ID == false) # NPT_ID == false} {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'},\n true, logn, Some s2p', Some npt', None)\n else\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'} {flatmem: fmem'},\n false, logn + 1, Some s2p', Some npt', Some fmem')\n end\n end\n else\n (sdt {hlock: (hlock sdt) # S2PAGE_ID == false}, true, logn, Some s2p, None, None)\n else\n if (s2_owner page =? vmid) && ((s2_gfn page =? INVALID64) || (gfn =? s2_gfn page)) then\n let page' := page {s2_count: if s2_count page =? INVALID then 0 else s2_count page}\n {s2_gfn: if s2_gfn page =? INVALID64 then gfn else s2_gfn page}\n in\n let s2p' := s2p # pfn == page in\n (sdt {s2page: s2p'}, false, logn, Some s2p', None, None)\n else\n (sdt {hlock: (hlock sdt) # S2PAGE_ID == false}, true, logn, Some s2p, None, None).\n\n Definition local_map_pfn_vm vmid addr pte level sdt :=\n let npt := vmid @ (npts sdt) in\n let paddr := phys_page pte in\n let perm := PAGE_S2_KERNEL in\n let pte' := (if level =? 2 then Z.land (Z.lor paddr perm) NOT_PMD_TABLE_BIT else Z.lor paddr perm) in\n let level' := (if level =? 2 then 2 else 3) in\n let (halt', npt') := local_mmap_h vmid addr level' pte' npt in\n if halt' then\n (sdt {hlock: (hlock sdt) # (NPT_ID + vmid) == false} {npts: (npts sdt) # vmid == npt'}, true, Some npt')\n else\n (sdt {npts: (npts sdt) # vmid == npt'}, false, Some npt').\n\n Definition local_map_io vmid gpa pa sdt :=\n let info := vmid @ (vminfos sdt) in\n let state := vm_state (VS info) in\n if state =? READY then\n let s2p := s2page sdt in\n let npt := vmid @ (npts sdt) in\n let gfn := gpa / PAGE_SIZE in\n let pfn := pa / PAGE_SIZE in\n let pte := Z.lor (phys_page pa) (Z.lor PAGE_S2_DEVICE S2_RDWR) in\n let page := pfn @ s2p in\n if s2_owner page =? INVALID then\n let (halt', npt') := local_mmap_h vmid gpa 3 pte npt in\n if halt' then\n (sdt {hlock: (((hlock sdt) # (INFO_ID + vmid) == false) # S2PAGE_ID == false) # (NPT_ID + vmid) == false} {npts: (npts sdt) # vmid == npt'},\n true, Some info, Some s2p, Some npt')\n else\n (sdt {npts: (npts sdt) # vmid == npt'}, false, Some info, Some s2p, Some npt')\n else\n (sdt, false, Some info, Some s2p, None)\n else\n (sdt {hlock: (hlock sdt) # (INFO_ID + vmid) == false}, true, Some info, None, None).\n\n Definition local_grant_vm_page vmid pfn sdt :=\n let s2p := s2page sdt in\n let page := pfn @ s2p in\n let s2p' := (if (s2_owner page =? vmid) && (s2_count page MAX_SHARE_COUNT)\n then s2p # pfn == (page {s2_count: (s2_count page) + 1})\n else s2p) in\n (sdt {s2page: s2p'}, Some s2p').\n\n Definition local_revoke_vm_page vmid pfn (dorc: Z -> Z) logn sdt :=\n let s2p := s2page sdt in\n let npt := HOSTVISOR @ (npts sdt) in\n let fmem := flatmem sdt in\n let page := pfn @ s2p in\n if (s2_owner page =? vmid) && (s2_count page >? 0) then\n let s2p' := s2p # pfn == (page {s2_count: (s2_count page) - 1}) in\n if s2_count page =? 1 then\n let fmem' := fmem # pfn == (dorc logn) in\n match pfn @ (pt npt) with\n | (pfn', level, pte) =>\n match (if pte =? 0 then (false, npt)\n else local_mmap_h HOSTVISOR (pfn * PAGE_SIZE) 3 PAGE_GUEST npt) with\n | (halt', npt') =>\n if halt' then\n (sdt {hlock: ((hlock sdt) # S2PAGE_ID == false) # NPT_ID == false} {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'},\n true, logn, Some s2p', Some npt', None)\n else\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'} {flatmem: fmem'},\n false, logn + 1, Some s2p', Some npt', Some fmem')\n end\n end\n else\n (sdt {s2page: s2p'}, false, logn, Some s2p', None, None)\n else\n (sdt, false, logn, Some s2p, None, None).\n\n Definition local_set_vcpu_active vmid vcpuid sdt :=\n let info := vmid @ (vminfos sdt) in\n match vm_state (VS info), vcpuid @ (vm_vcpu_state (VS info)) with\n | vms, vcpus =>\n if (vms =? VERIFIED) && (vcpus =? READY) then\n let info' := info {vm_vcpu_state: (vm_vcpu_state (VS info)) # vcpuid == ACTIVE} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, false, Some info')\n else\n (sdt, true, Some info)\n end.\n\n Definition local_set_vcpu_inactive vmid vcpuid sdt :=\n let info := vmid @ (vminfos sdt) in\n let vcpus := vcpuid @ (vm_vcpu_state (VS info)) in\n if vcpus =? ACTIVE then\n let info' := info {vm_vcpu_state: (vm_vcpu_state (VS info)) # vcpuid == READY} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, false, Some info')\n else\n (sdt, true, Some info).\n\n Definition local_set_vm_poweroff vmid sdt :=\n let info := vmid @ (vminfos sdt) in\n let info' := info {vm_state: POWEROFF} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, Some info').\n\n Definition local_register_vcpu vmid vcpuid sdt :=\n let info := vmid @ (vminfos sdt) in\n match vm_state (VS info), vcpuid @ (vm_vcpu_state (VS info)) with\n | vms, vcpus =>\n if (vms =? READY) && (vcpus =? UNUSED) then\n let vcpu := shared_vcpu vmid vcpuid in\n let info' := info {vm_vcpu: (vm_vcpu (VS info)) # vcpuid == vcpu} {vm_vcpu_state: (vm_vcpu_state (VS info)) # vcpuid == READY} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, false, Some info')\n else\n (sdt, true, Some info)\n end.\n\n Definition local_gen_vmid sdt :=\n let vmid := next_vmid (core_data sdt) in\n if vmid COREVISOR then\n let core' := (core_data sdt) {next_vmid: vmid + 1} in\n (sdt {core_data: core'}, false, core')\n else\n (sdt, true, (core_data sdt)).\n\n Definition local_register_kvm vmid sdt :=\n let info := vmid @ (vminfos sdt) in\n let vms := vm_state (VS info) in\n if vms =? UNUSED then\n let kvm := shared_kvm vmid in\n let info' := info {vm_kvm: kvm} {vm_state: READY} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, false, Some info')\n else\n (sdt, true, Some info).\n\n Definition local_set_boot_info vmid load_addr size sdt :=\n let info := vmid @ (vminfos sdt) in\n let core := core_data sdt in\n let vms := vm_state (VS info) in\n if vms =? READY then\n let load_idx := vm_next_load_info (VB info) in\n if load_idx MAX_LOAD_INFO_NUM then\n let pgnum := (size + 4095) / PAGE_SIZE in\n let remap := next_remap_ptr core in\n if (0 pgnum) && (remap + pgnum * PAGE_SIZE REMAP_END) then\n let core' := core {next_remap_ptr : remap + pgnum * PAGE_SIZE} in\n let info' := info {vm_next_load_info: load_idx + 1}\n {vm_load_addr: (vm_load_addr (VB info)) # load_idx == load_addr}\n {vm_load_size: (vm_load_size (VB info)) # load_idx == size}\n {vm_remap_addr: (vm_remap_addr (VB info)) # load_idx == remap}\n {vm_mapped_pages: (vm_mapped_pages (VB info)) # load_idx == 0} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'} {core_data: core'}, false, Some info', Some core')\n else\n (sdt, true, Some info, Some core)\n else\n (sdt, false, Some info, None)\n else\n (sdt, true, Some info, None).\n\n Definition local_remap_vm_image vmid pfn load_idx sdt :=\n let info := vmid @ (vminfos sdt) in\n let npt := COREVISOR @ (npts sdt) in\n let vms := vm_state (VS info) in\n if vms =? READY then\n let load_cnt := vm_next_load_info (VB info) in\n if load_idx load_cnt then\n match load_idx @ (vm_load_size (VB info)), load_idx @ (vm_remap_addr (VB info)), load_idx @ (vm_mapped_pages (VB info)) with\n | size, remap, mapped =>\n let pgnum := (size + 4095) / PAGE_SIZE in\n let target := remap + mapped * PAGE_SIZE in\n if mapped pgnum then\n let (halt', npt') := local_mmap_h COREVISOR target 3 (Z.lor (pfn * PAGE_SIZE) PAGE_HYP) npt in\n if halt' then\n (sdt {hlock: ((hlock sdt) # (INFO_ID + vmid) == false) # (NPT_ID + COREVISOR) == false}\n {npts: (npts sdt) # COREVISOR == npt'}, true, Some info, Some npt')\n else\n let info' := info {vm_mapped_pages: (vm_mapped_pages (VB info)) # load_idx == (mapped + 1)} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}\n {npts: (npts sdt) # COREVISOR == npt'}, false, Some info', Some npt')\n else\n (sdt, false, Some info, None)\n end\n else\n (sdt, false, Some info, None)\n else\n (sdt {hlock: (hlock sdt) # (INFO_ID + vmid) == false}, true, Some info, None).\n\n Definition local_verify_vm vmid sdt :=\n let info := vmid @ (vminfos sdt) in\n let state := vm_state (VS info) in\n if state =? READY then\n let info' := info {vm_state: VERIFIED} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, false, Some info')\n else\n (sdt {hlock: (hlock sdt) # (INFO_ID + vmid) == false}, true, Some info).\n\n Definition local_free_smmu_pgd cbndx index sdt :=\n let vmid := (smmu_id index cbndx) @ (smmu_vmid sdt) in\n if vmid =? INVALID then\n (sdt, false, Some (smmu_vmid sdt), None)\n else\n let info := vmid @ (vminfos sdt) in\n if vm_state (VS info) =? POWEROFF then\n let smmu' := (smmu_vmid sdt) # (smmu_id index cbndx) == INVALID in\n (sdt {smmu_vmid: smmu'}, false, Some smmu', Some info)\n else\n (sdt {hlock: (hlock sdt) # SMMU_ID == false}, true, Some (smmu_vmid sdt), Some info).\n\n Definition local_alloc_smmu_pgd cbndx vmid index num sdt :=\n if cbndx num then\n let target := (smmu_id index cbndx) @ (smmu_vmid sdt) in\n if target =? INVALID then\n let info := vmid @ (vminfos sdt) in\n let state := vm_state (VS info) in\n if is_vm vmid && (state =? VERIFIED) then\n (sdt {hlock: ((hlock sdt) # (INFO_ID + vmid) == false) # SMMU_ID == false}, true, Some (smmu_vmid sdt), Some info, None)\n else\n let smmu' := (smmu_vmid sdt) # (smmu_id index cbndx) == vmid in\n let ttbr := SMMU_TTBR index cbndx in\n let spt := spts sdt in\n let spt' := spt {spt_pt: (spt_pt spt) # ttbr == (ZMap.init (0, 0))}\n {spt_pgd_t: (spt_pgd_t spt) # ttbr == (ZMap.init false)}\n {spt_pmd_t: (spt_pmd_t spt) # ttbr == (ZMap.init (ZMap.init false))} in\n (sdt {smmu_vmid: smmu'} {spts: spt'}, false, Some smmu', Some info, Some spt')\n else\n (sdt, false, Some (smmu_vmid sdt), None, None)\n else\n (sdt {hlock: (hlock sdt) # SMMU_ID == false}, true, Some (smmu_vmid sdt), None, None).\n\n Definition local_smmu_assign_page cbndx index pfn gfn sdt :=\n let smmu := smmu_vmid sdt in\n let vmid := (smmu_id index cbndx) @ (smmu_vmid sdt) in\n let info := vmid @ (vminfos sdt) in\n let s2p := s2page sdt in\n let npt := HOSTVISOR @ (npts sdt) in\n if vmid =? INVALID then\n (sdt, false, Some smmu, None, None, None)\n else\n let state := vm_state (VS info) in\n if is_vm vmid then\n if state =? VERIFIED then\n (sdt {hlock: ((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false}, true, Some smmu, Some info, None, None)\n else\n let page := pfn @ s2p in\n if s2_owner page =? HOSTVISOR then\n if s2_count page =? 0 then\n let s2p' := s2p # pfn == (mkS2Page vmid INVALID gfn) in\n match pfn @ (pt npt) with\n | (_, level, pte) =>\n match (if pte =? 0 then (false, npt)\n else local_mmap_h HOSTVISOR (pfn * PAGE_SIZE) 3 PAGE_GUEST npt) with\n | (halt', npt') =>\n if halt' then\n (sdt {hlock: ((((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false) # S2PAGE_ID == false) # NPT_ID == false}\n {npts: (npts sdt) # HOSTVISOR == npt'},\n true, Some smmu, Some info, Some s2p, Some npt')\n else\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'}, false, Some smmu, Some info, Some s2p', Some npt')\n end\n end\n else\n (sdt {hlock: (((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false) # S2PAGE_ID == false}, true, Some smmu, Some info, Some s2p, None)\n else\n if s2_owner page =? vmid then\n (sdt, false, Some smmu, Some info, Some s2p, None)\n else\n (sdt {hlock: (((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false) # S2PAGE_ID == false}, true, Some smmu, Some info, Some s2p, None)\n else\n (sdt, false, Some smmu, Some info, None, None).\n\n Definition local_smmu_map_page cbndx index iova pte sdt :=\n let smmu := smmu_vmid sdt in\n let vmid := (smmu_id index cbndx) @ smmu in\n let info := vmid @ (vminfos sdt) in\n let s2p := s2page sdt in\n let spt := spts sdt in\n if vmid =? INVALID then\n (sdt, false, Some smmu, None, None, None)\n else\n let state := vm_state (VS info) in\n if is_vm vmid && (state =? VERIFIED) then\n (sdt {hlock: ((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false}, true, Some smmu, Some info, None, None)\n else\n let pfn := phys_page pte / PAGE_SIZE in\n let gfn := iova / PAGE_SIZE in\n let page := pfn @ s2p in\n if (vmid =? s2_owner page) && (gfn =? s2_gfn page) then\n match local_spt_map_h cbndx index iova pte spt with\n | (halt', spt') =>\n if halt' then\n (sdt {hlock: ((((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false) # S2PAGE_ID == false) # SPT_ID == false}\n {spts: spt'}, true, Some smmu, Some info, Some s2p, Some spt')\n else\n let s2p' := (if (s2_owner page =? HOSTVISOR) && (s2_count page EL2_SMMU_CFG_SIZE)\n then s2p # pfn == (page {s2_count: (s2_count page) + 1}) else s2p) in\n (sdt {s2page: s2p'} {spts: spt'}, false, Some smmu, Some info, Some s2p', Some spt')\n end\n else\n (sdt {hlock: (((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false) # S2PAGE_ID == false},\n true, Some smmu, Some info, Some s2p, None).\n\n Definition local_smmu_clear iova cbndx index sdt :=\n let smmu := smmu_vmid sdt in\n let vmid := (smmu_id index cbndx) @ smmu in\n let info := vmid @ (vminfos sdt) in\n let s2p := s2page sdt in\n let spt := spts sdt in\n if vmid =? INVALID then\n (sdt, false, Some smmu, None, None, None)\n else\n let state := vm_state (VS info) in\n if is_vm vmid && (state >=? VERIFIED) then\n (sdt {hlock: ((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false}, true, Some smmu, Some info, None, None)\n else\n let ttbr := SMMU_TTBR index cbndx in\n let pt := ttbr @ (spt_pt spt) in\n let gfn := iova / PAGE_SIZE in\n let (pfn, pte) := gfn @ pt in\n let (halt', spt') := (if pte =? 0 then (false, spt) else local_spt_map_h cbndx index iova 0 spt) in\n if halt' then\n (sdt {hlock: ((((hlock sdt) # SMMU_ID == false) # (INFO_ID + vmid) == false) # S2PAGE_ID == false) # SPT_ID == false}\n {spts: spt'}, true, Some smmu, Some info, Some s2p, Some spt')\n else\n let pfn0 := phys_page pte / PAGE_SIZE in\n let page := pfn0 @ s2p in\n let s2p' := (if (s2_owner page =? HOSTVISOR) && (s2_count page >? 0)\n then s2p # pfn0 == (page {s2_count: (s2_count page) - 1}) else s2p) in\n (sdt {s2page: s2p'} {spts: spt'}, false, Some smmu, Some info, Some s2p', Some spt').\n\n Definition local_load_encryted_vcpu vmid vcpuid cregs cstates dorc logn sdt :=\n let info := vmid @ (vminfos sdt) in\n let vms := vm_state (VS info) in\n let vcpus := vcpuid @ (vm_vcpu_state (VS info)) in\n if (vms =? READY) && (vcpus =? READY) then\n match decrypt_gp_regs_specx cregs cstates dorc logn with\n | (cregs', cstates', logn') =>\n match decrypt_sys_regs_specx cregs' cstates' dorc logn' with\n | (cregs'', cstates'', logn'') =>\n let info' := info {vm_inc_exe: 1} in\n (sdt {vminfos: (vminfos sdt) # vmid == info'}, false, cregs'', cstates'', logn'', Some info')\n end\n end\n else\n (sdt {hlock: (hlock sdt) # (INFO_ID + vmid) == false}, true, cregs, cstates, logn, Some info).\n\n Definition local_save_encrypt_buf vmid inbuf outbuf dorc logn sdt :=\n let inpfn := inbuf / PAGE_SIZE in\n let outpfn := outbuf / PAGE_SIZE in\n let inpage := inpfn @ (s2page sdt) in\n let outpage := outpfn @ (s2page sdt) in\n if (s2_owner inpage =? vmid) && (s2_owner outpage =? HOSTVISOR) then\n let fmem' := (flatmem sdt) # outpfn == (dorc logn) in\n (sdt {flatmem: fmem'}, false, logn + 1, Some (s2page sdt), Some fmem')\n else\n (sdt {hlock: (hlock sdt) # S2PAGE_ID == false}, true, logn, Some (s2page sdt), None).\n\n Definition local_load_decrypt_buf vmid inbuf dorc logn sdt :=\n let pfn := inbuf / PAGE_SIZE in\n let info := vmid @ (vminfos sdt) in\n let s2p := s2page sdt in\n let npt := HOSTVISOR @ (npts sdt) in\n let vms := vm_state (VS info) in\n let page := pfn @ s2p in\n if vms =? READY then\n if (s2_owner page =? HOSTVISOR) && (s2_count page =? 0) then\n match pfn @ (pt npt) with\n | (pfn', level, pte) =>\n match (if pte =? 0 then (false, npt)\n else local_mmap_h HOSTVISOR (pfn * PAGE_SIZE) 3 PAGE_GUEST npt) with\n | (halt', npt') =>\n if halt' then\n (sdt {hlock: (((hlock sdt) # (INFO_ID + vmid) == false) # S2PAGE_ID == false) # NPT_ID == false}\n {npts: (npts sdt) # HOSTVISOR == npt'}, true, logn, Some info, Some s2p, Some npt', None)\n else\n let page' := mkS2Page vmid 0 INVALID64 in\n let s2p' := s2p # pfn == page' in\n let fmem' := (flatmem sdt) # pfn == (dorc logn) in\n (sdt {npts: (npts sdt) # HOSTVISOR == npt'} {s2page: s2p'} {flatmem: fmem'},\n false, logn + 1, Some info, Some s2p', Some npt', Some fmem')\n end\n end\n else\n (sdt {hlock: ((hlock sdt) # (INFO_ID + vmid) == false) # S2PAGE_ID == false},\n true, logn, Some info, Some s2p, None, None)\n else\n (sdt {hlock: (hlock sdt) # (INFO_ID + vmid) == false}, true, logn, Some info, None, None, None).\n\n Fixpoint CalStateH (slog: SingleLog) (sdt: Shared) :=\n match slog with\n | nil => sdt\n | (SEVENT _ e) :: slog' =>\n let sdt' := CalStateH slog' sdt in\n match e with\n | OMAP_HOST addr =>\n match local_map_host addr sdt with\n | (sdt', _, _, _) => sdt'\n end\n | _ => sdt\n end\n end.\n\n Definition query_oracle adt :=\n adt {slog: ((soracle adt) (curid adt) (slog adt)) ++ (slog adt)} {shared: CalStateH ((soracle adt) (curid adt) (slog adt)) (shared adt)}.\n\n Opaque query_oracle.\n\nEnd ConcurrentSpec.\n\nSection AuxSpec.\n\n Fixpoint reset_sys_regs_loopx n i vcpuid cregs cstates (dorc: Z -> Z) logn :=\n match n with\n | O => (cregs, cstates, logn, i)\n | S n' =>\n match reset_sys_regs_loopx n' i vcpuid cregs cstates dorc logn with\n | (cregs', cstates', logn', i') =>\n if i' =? MPIDR_EL1 then\n let mpidr := (Z.land vcpuid 15) + (Z.land (vcpuid / 16) 255) * 256 +\n (Z.land (vcpuid / 4096) 255) * 65536 in\n match set_shadow_ctxt_specx i' (mpidr + 2147483648) cregs' cstates' with\n | (cregs'', cstates'') => (cregs'', cstates'', logn', i' + 1)\n end\n else\n if i' =? ACTLR_EL1 then\n let val := dorc logn' in\n match set_shadow_ctxt_specx i' val cregs' cstates' with\n | (cregs'', cstates'') => (cregs'', cstates'', logn' + 1, i' + 1)\n end\n else\n let val := reg_desc i' in\n match set_shadow_ctxt_specx i' val cregs' cstates' with\n | (cregs'', cstates'') => (cregs'', cstates'', logn', i' + 1)\n end\n end\n end.\n\n Definition reset_sys_regs_specx vcpuid cregs cstates dorc logn :=\n match reset_sys_regs_loopx (Z.to_nat SHADOW_SYS_REGS_SIZE) SYSREGS_START vcpuid cregs cstates dorc logn with\n | (cregs', cstates', logn', i') => (cregs', cstates', logn')\n end.\n\n Fixpoint sync_dirty_to_shadow_loopx (n: nat) (i: Z) dirty cregs cstates dorc logn :=\n match n with\n | O => (i, cregs, cstates, logn)\n | S n' =>\n match sync_dirty_to_shadow_loopx n' i dirty cregs cstates dorc logn with\n | (i', cregs', cstates', logn') =>\n if (Z.land dirty (Z.shiftl 1 i')) =? 0 then\n (i' + 1, cregs', cstates', logn')\n else\n let reg := dorc logn in\n match set_shadow_ctxt_specx i' reg cregs' cstates' with\n | (cregs'', cstates'') => (i' + 1, cregs'', cstates'', logn' + 1)\n end\n end\n end.\n\n Definition sync_dirty_to_shadow_specx (vmid: Z) (dirty: Z) (cregs: CtxtRegs) (cstates: CtxtStates) (logn: Z) (dorc: Z -> Z) :=\n match sync_dirty_to_shadow_loopx 31%nat 0 dirty cregs cstates dorc logn with\n | (i', cregs', cstates', logn') => (cregs', cstates', logn')\n end.\n\n Definition update_exception_gp_regs_specx (cregs: CtxtRegs) (cstates: CtxtStates) :=\n let pstate0 := pstate cstates in\n let pc0 := pc cstates in\n let new_pc := (exception_vector (VZ64 pstate0)) in\n (cregs {elr_el1: pc0}, cstates {spsr_0: pstate0} {esr_el1: ESR_ELx_EC_UNKNOWN} {pstate: PSTATE_FAULT_BITS_64}).\n\n Fixpoint prot_and_map_vm_loop_h n vmid gfn pfn adt :=\n match n with\n | O => Some (gfn, pfn, adt)\n | S n' =>\n match prot_and_map_vm_loop_h n' vmid gfn pfn adt with\n | Some (gfn', pfn', adt') =>\n if halt adt' then Some (gfn' + 1, pfn' + 1, adt') else\n let adt0 := query_oracle adt' in\n let log' := SEVENT (curid adt0) (OASSIGN_VM vmid gfn pfn (doracle adt0) (vmid @ (data_log adt0))) :: (slog adt0) in\n rely S2PAGE_ID @ (hlock (shared adt0)); rely NPT_ID @ (hlock (shared adt0));\n match local_assign_pfn_to_vm vmid gfn' pfn' ((doracle adt0) vmid) (vmid @ (data_log adt0)) (shared adt0) with\n | (sdt', halt', logn', _, _, _) =>\n Some (gfn' + 1, pfn' + 1, adt0 {shared: sdt'} {halt: halt'} {slog: log'} {data_log: (data_log adt) # vmid == logn'})\n end\n | _ => None\n end\n end.\n\n Definition prot_and_map_vm_s2pt_spec_h vmid addr pte level adt :=\n if halt adt then Some adt else\n let target := phys_page pte in\n let pfn := target / PAGE_SIZE in\n let gfn := addr / PAGE_SIZE in\n if level =? 2 then\n let gfn' := gfn / PTRS_PER_PMD * PTRS_PER_PMD in\n let num := PMD_PAGE_NUM in\n match prot_and_map_vm_loop_h (Z.to_nat num) vmid gfn' pfn adt with\n | Some (gfn'', pfn'', adt') =>\n if halt adt' then Some adt' else\n let adt0 := query_oracle adt' in\n let log' := SEVENT (curid adt0) (OMAP_VM vmid addr pte 2) :: (slog adt0) in\n rely (NPT_ID + vmid) @ (hlock (shared adt0));\n match local_map_pfn_vm vmid addr pte 2 (shared adt0) with\n | (sdt', halt', _) =>\n Some adt0 {halt: halt'} {shared: sdt'} {slog: log'}\n end\n | _ => None\n end\n else\n let adt0 := query_oracle adt in\n let log' := SEVENT (curid adt0) (OASSIGN_VM vmid gfn pfn (doracle adt0) (vmid @ (data_log adt0))) :: (slog adt0) in\n rely S2PAGE_ID @ (hlock (shared adt0)); rely NPT_ID @ (hlock (shared adt0));\n match local_assign_pfn_to_vm vmid gfn pfn ((doracle adt0) vmid) (vmid @ (data_log adt0)) (shared adt0) with\n | (sdt', halt', logn', _, _, _) =>\n let adt'' := adt0 {shared: sdt'} {halt: halt'} {slog: log'} {data_log: (data_log adt) # vmid == logn'} in\n let adt0 := query_oracle adt'' in\n let log' := SEVENT (curid adt0) (OMAP_VM vmid addr pte 3) :: (slog adt0) in\n rely (NPT_ID + vmid) @ (hlock (shared adt0));\n match local_map_pfn_vm vmid addr pte 3 (shared adt0) with\n | (sdt', halt', _) =>\n Some adt0 {halt: halt'} {shared: sdt'} {slog: log'}\n end\n end.\n\n Fixpoint clear_vm_loop_h n pfn vmid adt :=\n match n with\n | O => Some (pfn, adt)\n | S n' =>\n match clear_vm_loop_h n' pfn vmid adt with\n | Some (pfn', adt') =>\n if halt adt' then Some (pfn' + 1, adt') else\n let adt1 := query_oracle adt' in\n rely S2PAGE_ID @ (hlock (shared adt1)); rely NPT_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OCLEAR_VM vmid pfn') :: (slog adt1) in\n match local_clear_vm_page vmid pfn' (shared adt1) with\n | (sdt', halt', _, _, _) =>\n Some (pfn' + 1, adt1 {shared: sdt'} {halt: halt'} {slog: log'})\n end\n | _ => None\n end\n end.\n\n Fixpoint unmap_and_load_loop_h n vmid start target remap adt :=\n match n with\n | O => Some (start, target, remap, adt)\n | S n' =>\n match unmap_and_load_loop_h n' vmid start target remap adt with\n | Some (start', target', remap', adt') =>\n if halt adt' then\n Some (start' + PMD_SIZE, start' + PMD_SIZE, remap' + (start' + PMD_SIZE - target'), adt')\n else\n let adt0 := query_oracle adt' in\n rely (NPT_ID + COREVISOR) @ (hlock (shared adt0));\n let adt'' := adt0 {slog: SEVENT (curid adt0) (OPT_GET COREVISOR) :: (slog adt0)} in\n match (remap' / PAGE_SIZE) @ (pt (COREVISOR @ (npts (shared adt'')))) with\n | (_, _, pte) =>\n let pfn := phys_page pte / PMD_SIZE * PTRS_PER_PMD in\n let gfn := start' / PAGE_SIZE in\n if pte =? 0 then\n Some (start' + PMD_SIZE, start' + PMD_SIZE, remap' + (start' + PMD_SIZE - target'), adt'' {halt: true})\n else\n match prot_and_map_vm_s2pt_spec_h vmid (gfn * PAGE_SIZE) (pfn * PAGE_SIZE) 2 adt'' with\n | Some adt1 =>\n Some (start' + PMD_SIZE, start' + PMD_SIZE, remap' + (start' + PMD_SIZE - target'), adt1)\n | _ => None\n end\n end\n | _ => None\n end\n end.\n\n Fixpoint verify_and_load_loop_h n vmid load_idx adt :=\n match n with\n | O => Some (load_idx, adt)\n | S n' =>\n match verify_and_load_loop_h n' vmid load_idx adt with\n | Some (idx', adt') =>\n let info := vmid @ (vminfos (shared adt')) in\n let load_addr := idx' @ (vm_load_addr (VB info)) in\n let remap_addr := idx' @ (vm_remap_addr (VB info)) in\n let mapped := idx' @ (vm_mapped_pages (VB info)) in\n let start := load_addr / PMD_SIZE * PMD_SIZE in\n let end' := load_addr + mapped * PAGE_SIZE in\n let num := (end' - start + 2097151) / PMD_SIZE in\n match unmap_and_load_loop_h (Z.to_nat num) vmid start load_addr remap_addr adt' with\n | Some (start', target', remap', adt'') =>\n Some (load_idx + 1, adt'')\n | _ => None\n end\n | _ => None\n end\n end.\n\n Fixpoint kvm_phys_addr_ioremap_loop_h n vmid gpa pa adt :=\n match n with\n | O => Some (gpa, pa, adt)\n | S n' =>\n match kvm_phys_addr_ioremap_loop_h n' vmid gpa pa adt with\n | Some (gpa', pa', adt') =>\n if halt adt' then\n Some (gpa' + PAGE_SIZE, pa' + PAGE_SIZE, adt')\n else\n let adt1 := query_oracle adt' in\n rely (INFO_ID + vmid) @ (hlock (shared adt1)); rely S2PAGE_ID @ (hlock (shared adt1));\n rely (NPT_ID + vmid) @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OMAP_IO vmid gpa' pa') :: (slog adt1) in\n match local_map_io vmid gpa' pa' (shared adt1) with\n | (sdt', halt', _, _, _) =>\n Some (gpa' + PAGE_SIZE, pa' + PAGE_SIZE, adt1 {shared: sdt'} {halt: halt'} {slog: log'})\n end\n | _ => None\n end\n end.\n\n Fixpoint is_smmu_range_loop_h n addr i res conf :=\n match n with\n | O => (i, res)\n | S n' =>\n match is_smmu_range_loop_h n' addr i res conf with\n | (i', res') =>\n if (i' @ (smmu_phys_base conf) <=? addr) && (addr i' @ (smmu_phys_base conf) + i' @ (smmu_size conf))\n then (i' + 1, i')\n else (i' + 1, res')\n end\n end.\n\n Definition handle_host_mmio_spec_h index fault_ipa len is_write rt cregs cstates rcregs dorc logn smmu :=\n if is_write =? 0 then\n let data := dorc logn in\n if len =? 8 then\n match set_shadow_ctxt_specx rt data cregs cstates with\n | (cregs', cstates') =>\n (false, logn + 1, cregs', cstates', rcregs {esr_el2: (elr_el2 rcregs) + 4})\n end\n else if len =? 4 then\n match set_shadow_ctxt_specx rt (Z.land data INVALID) cregs cstates with\n | (cregs', cstates') =>\n (false, logn + 1, cregs', cstates', rcregs {esr_el2: (elr_el2 rcregs) + 4})\n end\n else (true, logn, cregs, cstates, rcregs)\n else\n let data := get_shadow_ctxt_specx rt cregs cstates in\n let ret_state := (if len =? 8 then (false, logn, cregs, cstates, rcregs {esr_el2: (elr_el2 rcregs) + 4})\n else if len =? 4 then (false, logn, cregs, cstates, rcregs {esr_el2: (elr_el2 rcregs) + 4})\n else (true, logn, cregs, cstates, rcregs)) in\n let offset := Z.land fault_ipa SMMU_OFFSET_MASK in\n if offset Spec.SMMU_GLOBAL_BASE\n then\n let ret :=\n (if (offset >=? 0) && (offset <=? ARM_SMMU_GR1_BASE)\n then\n if offset =? ARM_SMMU_GR0_sCR0\n then let smmu_enable := Z.land (Z.shiftr data sCR0_SMCFCFG_SHIFT) 1 in if smmu_enable =? 0 then 0 else 1\n else if offset =? ARM_SMMU_GR0_sCR2 then if Z.land data 255 =? 0 then 1 else 0 else 1\n else\n if (offset >=? ARM_SMMU_GR1_BASE) && (offset ARM_SMMU_GR1_END)\n then\n let n := (offset - ARM_SMMU_GR1_BASE) / 4 in\n let vmid := (smmu_id index n) @ smmu in\n let type := Z.shiftr data CBAR_TYPE_SHIFT in\n let t_vmid := Z.land data CBAR_VMID_MASK in\n if vmid =? 0 then 1 else if (type =? CBAR_TYPE_S2_TRANS) && (vmid =? t_vmid) then 1 else 0\n else 1) in\n if ret =? 0 then (true, logn, cregs, cstates, rcregs)\n else ret_state\n else\n let off' := offset - Spec.SMMU_GLOBAL_BASE in\n let cb_offset := Z.land off' ARM_SMMU_PGSHIFT_MASK in\n if cb_offset =? ARM_SMMU_CB_TTBR0\n then\n let cbndx := Z.shiftr (offset - Spec.SMMU_GLOBAL_BASE) Spec.ARM_SMMU_PGSHIFT in\n ret_state\n else\n if cb_offset =? ARM_SMMU_CB_CONTEXTIDR\n then (true, logn, cregs, cstates, rcregs)\n else ret_state.\n\n Fixpoint search_load_info_loop_h (n: nat) (idx: Z) (addr: Z) (ret: Z) (binfo: BootInfo) :=\n match n with\n | O => (idx, ret)\n | S n' =>\n match search_load_info_loop_h n' idx addr ret binfo with\n | (idx', ret') =>\n match idx' @ (vm_load_addr binfo), idx' @ (vm_load_size binfo), idx' @ (vm_remap_addr binfo) with\n | base, size, remap =>\n if (addr >=? base) && (addr base + size) then\n (idx' + 1, (addr - base) + remap)\n else\n (idx' + 1, ret')\n end\n end\n end.\n\n Fixpoint grant_stage2_loop (n: nat) (vmid: Z) (addr: Z) (adt: RData) :=\n match n with\n | O => Some (addr, adt)\n | S n' =>\n match grant_stage2_loop n' vmid addr adt with\n | Some (addr', adt') =>\n let gfn' := addr' / PAGE_SIZE in\n let adt0 := query_oracle adt' in\n rely (NPT_ID + vmid) @ (hlock (shared adt0));\n let adt' := adt0 {slog: SEVENT (curid adt0) (OPT_GET vmid) :: (slog adt0)} in\n match gfn' @ (pt (vmid @ (npts (shared adt')))) with\n | (_, _, pte) =>\n let adt1 := query_oracle adt' in\n rely (NPT_ID + vmid) @ (hlock (shared adt1));\n let adt'' := adt1 {slog: SEVENT (curid adt1) (OPT_GET vmid) :: (slog adt1)} in\n match ZMap.get gfn' (pt (vmid @ (npts (shared adt'')))) with\n | (_, level, _) =>\n let pfn := phys_page pte / PAGE_SIZE in\n if pfn =? 0 then Some (addr' + PAGE_SIZE, adt'')\n else\n let pfn' := (if level =? 2 then pfn + Z.land gfn' 511 else pfn) in\n let adt2 := query_oracle adt'' in\n rely S2PAGE_ID @ (hlock (shared adt1));\n match local_grant_vm_page vmid pfn (shared adt2) with\n | (sdt', _) =>\n Some (addr' + PAGE_SIZE, adt2 {shared: sdt'} {slog: SEVENT (curid adt2) (OGRANT vmid pfn) :: (slog adt2)})\n end\n end\n end\n | _ => None\n end\n end.\n\n Fixpoint revoke_stage2_loop (n: nat) (vmid: Z) (addr: Z) (adt: RData) :=\n match n with\n | O => Some (addr, adt)\n | S n' =>\n match revoke_stage2_loop n' vmid addr adt with\n | Some (addr', adt') =>\n let gfn' := addr' / PAGE_SIZE in\n let adt0 := query_oracle adt' in\n rely (NPT_ID + vmid) @ (hlock (shared adt0));\n let adt' := adt0 {slog: SEVENT (curid adt0) (OPT_GET vmid) :: (slog adt0)} in\n match gfn' @ (pt (vmid @ (npts (shared adt')))) with\n | (_, _, pte) =>\n let adt1 := query_oracle adt' in\n rely (NPT_ID + vmid) @ (hlock (shared adt1));\n let adt'' := adt1 {slog: SEVENT (curid adt1) (OPT_GET vmid) :: (slog adt1)} in\n match ZMap.get gfn' (pt (vmid @ (npts (shared adt'')))) with\n | (_, level, _) =>\n let pfn := phys_page pte / PAGE_SIZE in\n if pfn =? 0 then Some (addr' + PAGE_SIZE, adt'')\n else\n let pfn' := (if level =? 2 then pfn + Z.land gfn' 511 else pfn) in\n let adt2 := query_oracle adt'' in\n rely S2PAGE_ID @ (hlock (shared adt1)); rely NPT_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt2) (OREVOKE vmid pfn (doracle adt2) (vmid @ (data_log adt2))) :: (slog adt2) in\n match local_revoke_vm_page vmid pfn ((doracle adt2) vmid) (vmid @ (data_log adt2)) (shared adt2) with\n | (sdt', halt', logn', _, _, _) =>\n Some (addr' + PAGE_SIZE, adt2 {halt: halt'} {shared: sdt'} {slog: log'} {data_log: (data_log adt2) # vmid == logn'})\n end\n end\n end\n | _ => None\n end\n end.\n\n Definition vm_exit_dispatcher_spec_h (vmid vcpuid: Z) (adt: RData) :=\n let ctxt := (ctxt_id vmid vcpuid) @ (shadow_ctxts adt) in\n let cregs := ctxt_regs ctxt in\n let esrel2 := esr_el2 cregs in\n let exit_type := (esrel2 / 67108864) mod 64 in\n if exit_type =? ESR_ELx_EC_SYS64 then\n let pc := elr_el2 (ctxt_regs (regs adt)) in\n Some (adt {regs : (regs adt) {ctxt_regs : (ctxt_regs (regs adt)) {esr_el2 : pc + 4}}}, 0)\n else\n if exit_type =? ESR_ELx_EC_HVC64 then\n let arg := x0 cregs in\n let addr := x2 cregs in\n let size := x3 cregs in\n if arg =? HVC_KVM_SET_DESC_PFN then\n let len := (size + 4095) / PAGE_SIZE in\n match grant_stage2_loop (Z.to_nat len) vmid addr adt with\n | Some (_, adt') => Some (adt', 0)\n | _ => None\n end\n else\n if arg =? HVC_KVM_UNSET_DESC_PFN then\n let len := (size + 4095) / PAGE_SIZE in\n match revoke_stage2_loop (Z.to_nat len) vmid addr adt with\n | Some (_, adt') => Some (adt', 0)\n | _ => None\n end\n else Some (adt, 1)\n else Some (adt {halt: true}, 0).\n\n Definition prep_wfx_specx (cstates: CtxtStates) :=\n cstates {dirty: DIRTY_PC_FLAG}.\n\n Definition prep_abort_specx (cregs: CtxtRegs) (cstates: CtxtStates) (logn: Z) (doc: Z -> Z) :=\n let esr := doc logn in\n let Rd := esr / 65536 mod 32 in\n let hpfar_el2 := hpfar_el2 cregs in\n let fault_ipa := (hpfar_el2 / 16) * 4096 in\n if fault_ipa MAX_MMIO_ADDR then\n let cst' := cstates {dirty: DIRTY_PC_FLAG} in\n if ((Z.land esr ESR_ELx_WNR) =? 0) && ((Z.land esr ESR_ELx_S1PTW) =? 0) then\n let cst'' := cstates {dirty: (Z.shiftl 1 Rd)} in\n (cst'', logn + 1)\n else (cst', logn + 1)\n else (cstates, logn + 1).\n\n Definition prep_hvc_spec_h (vmid vcpuid: Z) (adt: RData) :=\n let ctxtid := ctxt_id vmid vcpuid in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let psci_fn0 := x0 (ctxt_regs ctxt) in\n let psci_fn := Z.land psci_fn0 INVALID in\n if psci_fn =? PSCI_0_2_FN_SYSTEM_OFF then\n let adt0 := query_oracle adt in\n rely (INFO_ID + vmid) @ (hlock (shared adt0));\n let l' := SEVENT (curid adt) (OVM_POWEROFF vmid) :: (slog adt0) in\n match local_set_vm_poweroff vmid (shared adt0) with\n | (sdt', _) => Some adt0 {shared: sdt'} {slog: l'}\n end\n else Some adt.\n\n Lemma set_shadow_ctxt_dirty:\n forall index val cregs cstates cregs' cstates',\n set_shadow_ctxt_specx index val cregs cstates = (cregs', cstates') ->\n index <> DIRTY ->\n dirty cstates' = dirty cstates.\n Proof.\n intros. hsimpl_func H; try reflexivity.\n bool_rel; autounfold in *; omega.\n Qed.\n\nEnd AuxSpec.\n\nSection TrapHandlerSpec.\n\n Definition host_hvc_handler_spec (adt: RData) : option RData :=\n match (ihost adt, icore adt) with\n | (true, false) =>\n if halt adt then Some adt else\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let ctxt' := ctxt {ctxt_regs: host_to_core_specx (ctxt_regs ctxt) (ctxt_regs (regs adt))} in\n let adt' := adt {shadow_ctxts: (shadow_ctxts adt) # ctxtid == ctxt'} {icore: true} in\n let cregs' := ctxt_regs ctxt' in\n let cstates' := ctxt_states ctxt' in\n match x0 cregs', x1 cregs', x2 cregs', x3 cregs', x4 cregs', x5 cregs' with\n | arg, arg1, arg2, arg3, arg4, arg5 =>\n match (\n if arg =? HVC_TIMER_SET_CNTVOFF then\n Some adt'\n else\n if arg =? HVC_CLEAR_VM_S2_RANGE then\n if is_vm arg1 && is_paddr arg2 && is_paddr arg3 then\n let adt1 := query_oracle adt' in\n rely (NPT_ID + arg1) @ (hlock (shared adt1));\n let info := arg1 @ (vminfos (shared adt1)) in\n let adt'' := adt1 {slog: SEVENT (curid adt1) (OVM_GET vmid) :: (slog adt1)} in\n if vm_state (VS info) =? POWEROFF then\n let pfn := arg2 / PAGE_SIZE in\n let num := arg3 / PAGE_SIZE in\n match clear_vm_loop_h (Z.to_nat num) pfn arg1 adt'' with\n | Some (_, adt0) => Some adt0\n | _ => None\n end\n else Some adt''\n else Some adt' {halt: true}\n else\n if arg =? HVC_SET_BOOT_INFO then\n if is_vm arg1 && is_paddr arg2 && is_paddr arg1 && is_pfn (arg2 + arg3) then\n let adt1 := query_oracle adt' in\n rely (INFO_ID + arg1) @ (hlock (shared adt1)); rely CORE_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OBOOT_INFO arg1 arg2 arg3) :: (slog adt1) in\n match local_set_boot_info arg1 arg2 arg3 (shared adt1) with\n | (sdt', halt', _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_REMAP_VM_IMAGE then\n if is_vm arg1 && is_pfn arg3 && is_load_idx arg3 then\n let adt1 := query_oracle adt' in\n rely (INFO_ID + arg1) @ (hlock (shared adt1)); rely (NPT_ID + COREVISOR) @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OREMAP_IMAGE arg1 arg2 arg3) :: (slog adt1) in\n match local_remap_vm_image arg1 arg2 arg3 (shared adt1) with\n | (sdt', halt', _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_VERIFY_VM_IMAGE then\n if is_vm arg1 then\n let adt1 := query_oracle adt' in\n rely (INFO_ID + arg1) @ (hlock (shared adt1));\n let info := arg1 @ (vminfos (shared adt1)) in\n let state := vm_state (VS info) in\n let sdt' := (shared adt1) {hlock: (hlock (shared adt1)) # (INFO_ID + vmid) == false} in\n let adt'' := adt1 {slog: SEVENT (curid adt1) (OVERIFY arg1) :: (slog adt1)} {shared: sdt'} in\n if state =? READY then\n let cnt := vm_next_load_info (VB info) in\n match verify_and_load_loop_h (Z.to_nat cnt) arg1 0 adt'' with\n | Some (_, adt0) =>\n if halt adt0 then Some adt0 else\n let info' := info {vm_state: VERIFIED} in\n let hlock' := (hlock (shared adt0)) # (INFO_ID + arg1) == true in\n Some adt0 {shared: (shared adt0) {hlock: hlock'} {vminfos: (vminfos (shared adt0)) # arg1 == info'}}\n {slog: SEVENT (curid adt0) (OVERIFY arg1) :: (slog adt0)}\n | _ => None\n end\n else Some adt'' {halt: true}\n else Some adt' {halt: true}\n else\n if arg =? HVC_SMMU_FREE_PGD then\n if is_smmu_cfg arg1 && is_smmu arg2 then\n let adt1 := query_oracle adt' in\n rely SMMU_ID @ (hlock (shared adt1)); rely (INFO_ID + vmid) @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OSMMU_FREE arg1 arg2) :: (slog adt1) in\n match local_free_smmu_pgd arg1 arg2 (shared adt1) with\n | (sdt', halt', _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_SMMU_ALLOC_PGD then\n if is_smmu_cfg arg1 && (HOSTVISOR <=? arg2) && (arg2 COREVISOR) && is_smmu arg3 then\n let adt1 := query_oracle adt' in\n let num := arg3 @ (smmu_dev_context_banks (smmu_conf adt1)) in\n rely SMMU_ID @ (hlock (shared adt1)); rely (INFO_ID + vmid) @ (hlock (shared adt1));\n rely SPT_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OSMMU_ALLOC arg1 arg2 arg3 num) :: (slog adt1) in\n match local_alloc_smmu_pgd arg1 arg2 arg3 num (shared adt1) with\n | (sdt', halt', _, _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_SMMU_LPAE_MAP then\n if is_paddr arg1 && is_paddr arg2 && (0 <=? arg3) && (arg3 9223372036854775807) && (phys_page arg3 =? 0) && is_smmu_cfg arg4 && is_smmu arg5 then\n let pfn := arg2 / PAGE_SIZE in\n let gfn := arg1 / PAGE_SIZE in\n let pte := Z.lor arg2 (Z.lor arg3 PTE_AF_OR_SH_IS) in\n let adt1 := query_oracle adt' in\n rely SMMU_ID @ (hlock (shared adt1)); rely (INFO_ID + vmid) @ (hlock (shared adt1));\n rely S2PAGE_ID @ (hlock (shared adt1)); rely NPT_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OASSIGN_SMMU arg4 arg5 pfn gfn) :: (slog adt1) in\n match local_smmu_assign_page arg4 arg5 pfn gfn (shared adt1) with\n | (sdt', halt', _, _, _, _) =>\n let adt'' := adt1 {shared: sdt'} {halt: halt'} {slog: log'} in\n if halt adt'' then Some adt'' else\n let adt2 := query_oracle adt'' in\n let vmid := (smmu_id arg4 arg5) @ (smmu_vmid (shared adt2)) in\n rely SMMU_ID @ (hlock (shared adt2)); rely (INFO_ID + vmid) @ (hlock (shared adt2));\n rely SPT_ID @ (hlock (shared adt2)); rely SPT_ID @ (hlock (shared adt2));\n let log'' := SEVENT (curid adt2) (OSMMU_MAP arg4 arg5 arg1 pte) :: (slog adt2) in\n match local_smmu_map_page arg4 arg5 arg1 pte (shared adt2) with\n | (sdt'', halt'', _, _, _, _) =>\n Some adt2 {shared: sdt''} {halt: halt''} {slog: log''}\n end\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_SMMU_LPAE_IOVA_TO_PHYS then\n if is_paddr arg1 && is_smmu_cfg arg2 && is_smmu arg3 then\n let adt1 := query_oracle adt' in\n rely SPT_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OSMMU_GET) :: (slog adt1) in\n let ttbr := SMMU_TTBR arg3 arg2 in\n match (arg1 / PAGE_SIZE) @ (ttbr @ (spt_pt (spts (shared adt1)))) with\n | (pfn, pte) =>\n let ret := phys_page pte + (Z.land arg1 4095) in\n Some adt1 {shadow_ctxts: (shadow_ctxts adt') # ctxtid == (ctxt' {ctxt_regs: (ctxt_regs ctxt') {x0: ret}})}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_SMMU_CLEAR then\n if is_paddr arg1 && is_smmu_cfg arg2 && is_smmu arg3 then\n let adt1 := query_oracle adt' in\n let vmid := (smmu_id arg2 arg3) @ (smmu_vmid (shared adt1)) in\n rely SMMU_ID @ (hlock (shared adt1)); rely (INFO_ID + vmid) @ (hlock (shared adt1));\n rely S2PAGE_ID @ (hlock (shared adt1)); rely SPT_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OSMMU_CLEAR arg1 arg2 arg3) :: (slog adt1) in\n match local_smmu_clear arg1 arg2 arg3 (shared adt1) with\n | (sdt', halt', _, _, _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_PHYS_ADDR_IOREMAP then\n if is_vm arg1 && is_addr arg2 && is_paddr arg3 && is_paddr arg4 && is_addr (arg2 + ((arg4 + 4095) / PAGE_SIZE) * PAGE_SIZE) && \n\t\t\t is_paddr (arg3 + ((arg4 + 4095) / PAGE_SIZE) * PAGE_SIZE) && (0 <=? arg4) then\n let num := (arg4 + 4095) / PAGE_SIZE in\n match kvm_phys_addr_ioremap_loop_h (Z.to_nat num) arg1 arg2 arg3 adt' with\n | Some (_, _, adt'') => Some adt''\n | _ => None\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_REGISTER_KVM then\n let adt1 := query_oracle adt' in\n rely CORE_ID @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OGEN_VMID) :: (slog adt1) in\n let vmid := next_vmid (core_data (shared adt1)) in\n match local_gen_vmid (shared adt1) with\n | (sdt', halt', _) =>\n let adt'' := adt1 {shared: sdt'} {halt: halt'} {slog: log'} in\n if halt adt'' then Some adt'' else\n let adt2 := query_oracle adt'' in\n rely (INFO_ID + vmid) @ (hlock (shared adt2));\n let log'' := SEVENT (curid adt2) (OREG_KVM vmid) :: (slog adt2) in\n match local_register_kvm vmid (shared adt2) with\n | (sdt'', halt'', _) =>\n Some adt2 {shared: sdt''} {halt: halt''} {slog: log''}\n {shadow_ctxts: (shadow_ctxts adt') # ctxtid == (ctxt' {ctxt_regs: (ctxt_regs ctxt') {x0: vmid}})}\n end\n end\n else\n if arg =? HVC_REGISTER_VCPU then\n if is_vm arg1 && is_vcpu arg2 then\n let adt1 := query_oracle adt' in\n rely (INFO_ID + arg1) @ (hlock (shared adt1));\n let log' := SEVENT (curid adt1) (OREG_VCPU arg1 arg2) :: (slog adt1) in\n match local_register_vcpu arg1 arg2 (shared adt1) with\n | (sdt', halt', _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_ENCRYPT_BUF then\n if is_vm arg1 && is_paddr arg2 && is_paddr arg3 then\n let adt1 := query_oracle adt' in\n rely S2PAGE_ID @ (hlock (shared adt1));\n let dorc := (doracle adt1) HOSTVISOR in\n let logn := HOSTVISOR @ (data_log adt1) in\n let log' := SEVENT (curid adt1) (OSAVE_ENC_BUF arg1 arg2 arg3 dorc logn) :: (slog adt1) in\n match local_save_encrypt_buf arg1 arg2 arg3 dorc logn (shared adt1) with\n | (sdt', halt', logn', _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'} {data_log: (data_log adt1) # HOSTVISOR == logn'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_DECRYPT_BUF then\n if is_vm arg1 && is_paddr arg2 then\n let adt1 := query_oracle adt' in\n rely S2PAGE_ID @ (hlock (shared adt1)); rely (INFO_ID + arg1) @ (hlock (shared adt1));\n rely NPT_ID @ (hlock (shared adt1));\n let dorc := (doracle adt1) HOSTVISOR in\n let logn := HOSTVISOR @ (data_log adt1) in\n let log' := SEVENT (curid adt1) (OLOAD_ENC_BUF arg1 arg2 dorc logn) :: (slog adt1) in\n match local_load_decrypt_buf arg1 arg2 dorc logn (shared adt1) with\n | (sdt', halt', logn', _, _, _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'} {data_log: (data_log adt1) # HOSTVISOR == logn'}\n end\n else Some adt' {halt: true}\n else\n if arg =? HVC_SAVE_CRYPT_VCPU then\n if is_vm arg1 && is_vcpu arg2 then\n Some adt'\n else Some adt' {halt: true}\n else\n if arg =? HVC_LOAD_CRYPT_VCPU then\n if is_vm arg1 && is_vcpu arg2 then\n let adt1 := query_oracle adt' in\n rely (INFO_ID + arg1) @ (hlock (shared adt1));\n let dorc := (doracle adt1) HOSTVISOR in\n let logn := HOSTVISOR @ (data_log adt1) in\n let log' := SEVENT (curid adt1) (OLOAD_ENC_VCPU arg1 arg2 cregs' cstates' dorc logn) :: (slog adt1) in\n match local_load_encryted_vcpu arg1 arg2 cregs' cstates' dorc logn (shared adt1) with\n | (sdt', halt', cregs'', cstates'', logn', _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'} {data_log: (data_log adt1) # HOSTVISOR == logn'}\n {shadow_ctxts: (shadow_ctxts adt') # ctxtid == (ctxt' {ctxt_regs: cregs''} {ctxt_states: cstates''})}\n end\n else Some adt' {halt: true}\n else Some adt'\n )\n with\n | Some adt'' =>\n if halt adt'' then Some adt'' else\n let ctxt := ctxtid @ (shadow_ctxts adt'') in\n Some adt'' {regs: (regs adt'') {ctxt_regs: core_to_host_specx (ctxt_regs ctxt) (ctxt_regs (regs adt''))}}\n {icore: false}\n | _ => None\n end\n end\n | _ => None\n end.\n\n Definition host_npt_handler_spec (adt: RData) : option RData :=\n match (ihost adt, icore adt) with\n | (true, false) =>\n if halt adt then Some adt else\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let ctxt' := ctxt {ctxt_regs: host_to_core_specx (ctxt_regs ctxt) (ctxt_regs (regs adt))} in\n let adt' := adt {shadow_ctxts: (shadow_ctxts adt) # ctxtid == ctxt'} {icore: true} in\n let hpfar := hpfar_el2 (ctxt_regs (regs adt')) in\n let addr := Z.land hpfar HPFAR_MASK * 256 in\n if is_addr addr then\n let esr := esr_el2 (ctxt_regs (regs adt')) in\n match (\n let fault_ipa := Z.lor addr (Z.land (far_el2 (ctxt_regs (regs adt'))) 4095) in\n let len := host_dabt_get_as' esr in\n let is_write := host_dabt_is_write' esr in\n let rt := host_dabt_get_rd' esr in\n let adt0 := query_oracle adt' in\n let log' := SEVENT (curid adt) (OSMMU_GET) :: (slog adt0) in\n rely SMMU_ID @ (hlock (shared adt0));\n let conf := smmu_conf adt' in\n let num := smmu_num conf in\n let (_, res) := is_smmu_range_loop_h (Z.to_nat num) addr 0 INVALID conf in\n if res =? INVALID then\n let adt'' := adt0 {slog: log'} in\n let adt1 := query_oracle adt'' in\n let log' := SEVENT (curid adt1) (OMAP_HOST addr) :: (slog adt1) in\n rely S2PAGE_ID @ (hlock (shared adt1)); rely NPT_ID @ (hlock (shared adt1));\n match local_map_host addr (shared adt1) with\n | (sdt', halt', _, _) =>\n Some adt1 {shared: sdt'} {halt: halt'} {slog: log'}\n end\n else\n let smmu := smmu_vmid (shared adt0) in\n let logn := HOSTVISOR @ (data_log adt0) in\n match handle_host_mmio_spec_h res fault_ipa len is_write rt (ctxt_regs ctxt') (ctxt_states ctxt') (ctxt_regs (regs adt0)) ((doracle adt0) HOSTVISOR) logn smmu with\n | (halt', logn', cregs', cstates', rcregs') =>\n Some adt0 {halt: halt'} {slog: log'} {regs: (regs adt) {ctxt_regs: rcregs'}} {data_log: (data_log adt) # HOSTVISOR == logn'}\n {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt' {ctxt_regs: cregs'} {ctxt_states: cstates'})}\n end\n )\n with\n | Some adt'' =>\n if halt adt'' then Some adt'' else\n let ctxt := ctxtid @ (shadow_ctxts adt'') in\n Some adt'' {regs: (regs adt'') {ctxt_regs: core_to_host_specx (ctxt_regs ctxt) (ctxt_regs (regs adt''))}}\n {icore: false}\n | _ => None\n end\n else Some adt' {halt: true}\n | _ => None\n end.\n\n Definition vcpu_run_swith_to_core (adt: RData) :=\n let ctxtid := ctxt_id HOSTVISOR (cur_vcpuid adt) in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let regc := regs adt in\n let cregs1 := host_to_core_specx (ctxt_regs ctxt) (ctxt_regs regc) in\n let cregs2 := save_sysregs_specx cregs1 (ctxt_regs regc) in\n let cregs3 := fpsimd_save_state_specx cregs2 (ctxt_regs regc) in\n let vmid := x0 cregs3 in\n if is_vm vmid then\n adt {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt {ctxt_regs: cregs3})} {icore: true} {cur_vmid: vmid} {cur_vcpuid: -1}\n else\n adt {halt: true}.\n\n Definition vcpu_run_process (adt: RData) :=\n let vmid := cur_vmid adt in\n let clogn := vmid @ (core_data_log adt) in\n let vcpuid := (core_doracle adt) vmid clogn in\n let ctxtid' := ctxt_id vmid vcpuid in\n if is_vcpu vcpuid then\n let adt' := adt {cur_vcpuid: vcpuid} {core_data_log: (core_data_log adt) # vmid == (clogn + 1)} in\n let adt'' := query_oracle adt' in\n let l' := SEVENT (curid adt'') (OVM_ACTIVE vmid vcpuid) :: (slog adt'') in\n match local_set_vcpu_active vmid vcpuid (shared adt'') with\n | (sdt', halt', _) =>\n let adt0 := adt'' {halt: halt'} {shared: sdt'} {slog: l'} in\n if halt' then Some adt0 else\n let dirty0 := dirty (ctxt_states (ctxtid' @ (shadow_ctxts adt0))) in\n if dirty0 =? INVALID64 then\n let adt1 := query_oracle adt0 in\n rely (INFO_ID + vmid) @ (hlock (shared adt1));\n let l'' := SEVENT (curid adt1) (OVM_GET vmid) :: (slog adt1) in\n let inc_exe := vm_inc_exe (VB (vmid @ (vminfos (shared adt1)))) in\n if inc_exe =? 0 then\n let logn := vmid @ (data_log adt1) in\n let pc' := (doracle adt1) vmid logn in\n let adt2 := adt1 {slog: l''} {data_log: (data_log adt1) # vmid == (logn + 1)} in\n let adt3 := query_oracle adt2 in\n rely (INFO_ID + vmid) @ (hlock (shared adt3));\n let ll := SEVENT (curid adt3) (OVM_GET vmid) :: (slog adt3) in\n let info := vmid @ (vminfos (shared adt3)) in\n let num := vm_next_load_info (VB info) in\n let (_, ret) := search_load_info_loop_h (Z.to_nat num) 0 pc' 0 (VB info) in\n if ret =? 0 then Some adt3 {slog: ll} {halt: true}\n else\n let cregs' := clear_shadow_gp_regs_specx (ctxt_regs (ctxtid' @ (shadow_ctxts adt3))) in\n let logn' := vmid @ (data_log adt3) in\n let pstate' := (doracle adt3) vmid logn' in\n let cstates' := (ctxt_states (ctxtid' @ (shadow_ctxts adt3))) {pstate: pstate'} {pc: pc'} in\n let cregs'' := reset_fp_regs_specx cregs' in\n match reset_sys_regs_specx vcpuid cregs'' cstates' ((doracle adt3) vmid) (logn' + 1) with\n | (cregs''', cstates'', logn'') =>\n let cstats''' := cstates'' {dirty: 0} in\n Some adt0 {slog: ll} {data_log: (data_log adt3) # vmid == logn''}\n {shadow_ctxts: (shadow_ctxts adt3) # ctxtid' == ((ctxtid' @ (shadow_ctxts adt3)) {ctxt_regs: cregs'''} {ctxt_states: cstats'''})}\n end\n else\n let cstates' := (ctxt_states (ctxtid' @ (shadow_ctxts adt1))) {dirty: 0} in\n Some adt1 {slog: l''} {shadow_ctxts: (shadow_ctxts adt1) # ctxtid' == ((ctxtid' @ (shadow_ctxts adt1)) {ctxt_states: cstates'})}\n else\n let cregs := ctxt_regs (ctxtid' @ (shadow_ctxts adt0)) in\n let cstates := ctxt_states (ctxtid' @ (shadow_ctxts adt0)) in\n let ec0 := ec cregs in\n let logn := vmid @ (data_log adt0) in\n match (if ec0 =? ARM_EXCEPTION_TRAP\n then sync_dirty_to_shadow_specx vmid dirty0 cregs cstates logn ((doracle adt0) vmid)\n else (cregs, cstates, logn))\n with\n | (cregs', cstates', logn') =>\n match (if (Z.land dirty0 PENDING_EXCEPT_INJECT_FLAG) =? 0\n then update_exception_gp_regs_specx cregs' cstates'\n else (cregs', cstates'))\n with\n | (cregs'', cstates'') =>\n match (if (Z.land dirty0 DIRTY_PC_FLAG) =? 0\n then cstates''\n else (cstates'' {pc: (pc cstates'') + 4}))\n with\n | cstates3 =>\n let cstates4 := cstates3 {dirty: 0} in\n let cregs3 := cregs'' {far_el2: 0} in\n let clogn := vmid @ (core_data_log adt0) in\n let addr := (core_doracle adt0) vmid clogn in\n if (PAGE_SIZE <=? addr) && (addr KVM_ADDR_SPACE) then\n let pte := (core_doracle adt0) vmid (clogn + 1) in\n let level := (core_doracle adt0) vmid (clogn + 2) in\n let adt1 := adt0 {core_data_log: (core_data_log adt0) # vmid == (clogn + 3)}\n {data_log: (data_log adt0) # vmid == logn'}\n {shadow_ctxts: (shadow_ctxts adt0) # ctxtid' == ((ctxtid' @ (shadow_ctxts adt0))\n {ctxt_regs: cregs3} {ctxt_states: cstates4})}\n in prot_and_map_vm_s2pt_spec_h vmid addr pte level adt1\n else\n Some adt0 {core_data_log: (core_data_log adt0) # vmid == (clogn + 1)}\n {data_log: (data_log adt0) # vmid == logn'}\n {shadow_ctxts: (shadow_ctxts adt0) # ctxtid' == ((ctxtid' @ (shadow_ctxts adt0))\n {ctxt_regs: cregs3} {ctxt_states: cstates4})}\n end\n end\n end\n end\n else Some adt {halt: true}.\n\n Definition vcpu_run_switch_to_vm (adt: RData) :=\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid' := ctxt_id vmid vcpuid in\n let ctxt := ctxtid' @ (shadow_ctxts adt) in\n let creg := ctxt_regs ctxt in\n let regc := ctxt_regs (regs adt) in\n let csts := ctxt_states ctxt in\n let regt := trap_regs (regs adt) in\n let vttbr := pt_vttbr vmid in\n let regc' := vm_el2_restore_state_specx regc creg vttbr in\n let regt' := activate_traps_specx (timer_enable_traps_specx regt) in\n let regc'' := restore_sysregs_specx creg csts regc' in\n let regc3 := fpsimd_restore_state_specx creg regc'' in\n let regc4 := core_to_vm_specx creg regc3 in\n Some adt {regs: (regs adt) {ctxt_regs: regc4} {trap_regs: regt'}} {icore: false} {ihost: false}.\n\n Definition host_vcpu_run_handler_spec (adt: RData) : option RData :=\n match (ihost adt, icore adt) with\n | (true, false) =>\n if halt adt then Some adt else\n let adt' := vcpu_run_swith_to_core adt in\n if halt adt' then Some adt' else\n when adt'' == vcpu_run_process adt';\n if halt adt'' then Some adt'' else\n vcpu_run_switch_to_vm adt''\n | _ => None\n end.\n\n Definition prep_exit_vm (adt: RData) :=\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let cregs := ctxt_regs ctxt in\n let rcregs := ctxt_regs (regs adt) in\n let cregs1 := save_sysregs_specx cregs rcregs in\n let cregs2 := fpsimd_save_state_specx cregs1 rcregs in\n let rtregs1 := deactivate_traps_specx (trap_regs (regs adt)) in\n let rtregs2 := timer_enable_traps_specx rtregs1 in\n let ec' := ec cregs2 in\n match (\n if ec' =? ARM_EXCEPTION_TRAP then\n let hsr := esr_el2 cregs2 in\n let hsr_ec := Z.shiftr (Z.land hsr ESR_ELx_EC_MASK) ESR_ELx_EC_SHIFT in\n if hsr_ec =? ESR_ELx_EC_WFx then\n let cstates' := prep_wfx_specx (ctxt_states ctxt) in\n Some adt {regs: (regs adt) {trap_regs: rtregs2}}\n {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt {ctxt_regs: cregs2} {ctxt_states: cstates'})}\n else\n if hsr_ec =? ESR_ELx_EC_HVC64 then\n let adt' := adt {regs: (regs adt) {trap_regs: rtregs2}}\n {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt {ctxt_regs: cregs2})} in\n prep_hvc_spec_h vmid vcpuid adt'\n else\n if (hsr_ec =? ESR_ELx_EC_IABT_LOW) || (hsr_ec =? ESR_ELx_EC_DABT_LOW) then\n match prep_abort_specx cregs2 (ctxt_states ctxt) (vmid @ (data_log adt)) ((doracle adt) vmid) with\n | (cstates', logn') =>\n Some adt {regs: (regs adt) {trap_regs: rtregs2}} {data_log: (data_log adt) # vmid == logn'}\n {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt {ctxt_regs: cregs2} {ctxt_states: cstates'})}\n end\n else Some adt {halt: true}\n else\n Some adt {regs: (regs adt) {trap_regs: rtregs2}} {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt {ctxt_regs: cregs2})})\n with\n | Some adt1 =>\n if halt adt1 then Some adt1 else\n let adt2 := query_oracle adt1 in\n let l' := SEVENT (curid adt2) (OVM_INACTIVE vmid vcpuid) :: slog adt2 in\n match local_set_vcpu_inactive vmid vcpuid (shared adt2) with\n | (sdt', halt', _) =>\n Some adt2 {shared: sdt'} {halt: halt'} {slog: l'}\n end\n | _ => None\n end.\n\n Definition vm_exit_pre_process (adt: RData) :=\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let cregs := ctxt_regs ctxt in\n let rcregs := ctxt_regs (regs adt) in\n let cregs1 := vm_to_core_specx cregs rcregs in\n let cregs2 := exit_populate_fault_specx cregs1 rcregs in\n let adt' := adt {shadow_ctxts: (shadow_ctxts adt) # ctxtid == (ctxt {ctxt_regs: cregs2})} {icore: true} in\n let ec' := ec cregs2 in\n if ec' =? ARM_EXCEPTION_TRAP then\n match vm_exit_dispatcher_spec_h vmid vcpuid adt' with\n | Some (adt'', ret) =>\n if ret =? 0 then\n let ctxt := ctxtid @ (shadow_ctxts adt'') in\n Some (adt'' {regs: (regs adt'') {ctxt_regs: core_to_vm_specx (ctxt_regs ctxt) (ctxt_regs (regs adt''))}} {icore: false}, 0)\n else\n when adt3 == prep_exit_vm adt'';\n Some (adt3, 1)\n | _ => None\n end\n else\n if ec' =? ARM_EXCEPTION_INTERRUPT then\n when adt'' == prep_exit_vm adt';\n Some (adt'', 1)\n else Some (adt' {halt: true}, 1).\n\n Definition switch_to_host (adt: RData) :=\n let vmid := HOSTVISOR in\n let adt0 := adt {cur_vmid: vmid} {cur_vcpuid: -1} in\n let vcpuid := curid adt0 in\n let ctxtid := ctxt_id vmid vcpuid in\n let cregs := ctxt_regs ctxtid @ (shadow_ctxts adt0) in\n let cstates := ctxt_states ctxtid @ (shadow_ctxts adt0) in\n let rcregs := ctxt_regs (regs adt0) in\n let vttbr := pt_vttbr HOSTVISOR in\n let rcregs1 := host_el2_restore_state_specx rcregs vttbr in\n let rcregs2 := restore_sysregs_specx cregs cstates rcregs1 in\n let rcregs3 := fpsimd_restore_state_specx cregs rcregs2 in\n let rcregs4 := core_to_host_specx cregs rcregs3 in\n adt0 {regs: (regs adt0) {ctxt_regs: rcregs4}} {cur_vcpuid: vcpuid} {ihost: true} {icore: false}.\n\n Definition vm_exit_handler_spec (adt: RData) : option RData :=\n match (ihost adt, icore adt) with\n | (false, false) =>\n if halt adt then Some adt else\n when ret, adt' == vm_exit_pre_process adt;\n if halt adt' then Some adt' else\n if ret =? 0 then Some adt'\n else Some (switch_to_host adt')\n | _ => None\n end.\n\n Definition mem_load_spec (gfn: Z64) (reg: Z) (adt: RData) : option RData :=\n match gfn, icore adt with\n | VZ64 gfn, false =>\n let adt := query_oracle adt in\n let log' := (SEVENT (curid adt) (OPT_GET (cur_vmid adt))) :: (slog adt) in\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n match\n (if vmid =? HOSTVISOR then\n match gfn @ (pt (vmid @ (npts (shared adt)))) with\n | (pfn, level, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? HOSTVISOR\n then Some (pfn @ (flatmem (shared adt)))\n else Some ((doracle adt) vmid (vmid @ (data_log adt)))\n end\n else\n rely (vm_state (VS (cur_vmid adt) @ (vminfos (shared adt))) =? VERIFIED);\n match gfn @ (pt (vmid @ (npts (shared adt)))) with\n | (pfn, level, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? INVALID then None\n else\n if s2_count (pfn @ (s2page (shared adt))) =? 0\n then Some (pfn @ (flatmem (shared adt)))\n else Some ((doracle adt) vmid (vmid @ (data_log adt)))\n end\n )\n with\n | Some val =>\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n rely (reg X30);\n match set_shadow_ctxt_specx reg val (ctxt_regs ctxt) (ctxt_states ctxt) with\n | (cregs', cstates') =>\n let ctxt' := ctxt {ctxt_regs: cregs'} {ctxt_states: cstates'} in\n Some adt {shadow_ctxts: (shadow_ctxts adt) # ctxtid == ctxt'} {slog: log'}\n end\n | _ => None\n end\n | _, _ => None\n end.\n\n Definition mem_store_spec (gfn: Z64) (reg: Z) (adt: RData) : option RData :=\n match gfn, icore adt with\n | VZ64 gfn, false =>\n let adt := query_oracle adt in\n let log' := (SEVENT (curid adt) (OPT_GET (cur_vmid adt))) :: (slog adt) in\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let val := get_shadow_ctxt_specx reg (ctxt_regs ctxt) (ctxt_states ctxt) in\n if vmid =? HOSTVISOR then\n match gfn @ (pt (vmid @ (npts (shared adt)))) with\n | (pfn, level, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? HOSTVISOR\n then Some adt {shared: (shared adt) {flatmem: (flatmem (shared adt)) # pfn == val}} {slog: log'}\n else Some adt {slog: log'}\n end\n else\n rely (vm_state (VS (cur_vmid adt) @ (vminfos (shared adt))) =? VERIFIED);\n match gfn @ (pt (vmid @ (npts (shared adt)))) with\n | (pfn, level, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? INVALID then None\n else\n if s2_count (pfn @ (s2page (shared adt))) =? 0\n then Some adt {shared: (shared adt) {flatmem: (flatmem (shared adt)) # pfn == val}} {slog: log'}\n else Some adt {slog: log'}\n end\n | _, _ => None\n end.\n\n Definition dev_load_spec (gfn: Z64) (reg: Z) (cbndx: Z) (index: Z) (adt: RData) : option RData :=\n match gfn, icore adt with\n | VZ64 gfn, false =>\n rely is_smmu index; rely is_smmu_cfg cbndx;\n let adt := query_oracle adt in\n let log' := (SEVENT (curid adt) (OPT_GET (cur_vmid adt))) :: (slog adt) in\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let smmuid := smmu_id index cbndx in\n rely smmuid @ (smmu_vmid (shared adt)) =? vmid;\n let spt := (SMMU_TTBR index cbndx) @ (spt_pt (spts (shared adt))) in\n match\n (if vmid =? HOSTVISOR then\n match gfn @ spt with\n | (pfn, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? HOSTVISOR\n then Some (pfn @ (flatmem (shared adt)))\n else Some ((doracle adt) vmid (vmid @ (data_log adt)))\n end\n else\n rely (vm_state (VS (cur_vmid adt) @ (vminfos (shared adt))) =? VERIFIED);\n match gfn @ spt with\n | (pfn, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? INVALID then None\n else\n if s2_count (pfn @ (s2page (shared adt))) =? 0\n then Some (pfn @ (flatmem (shared adt)))\n else Some ((doracle adt) vmid (vmid @ (data_log adt)))\n end\n )\n with\n | Some val =>\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n rely (reg X30);\n match set_shadow_ctxt_specx reg val (ctxt_regs ctxt) (ctxt_states ctxt) with\n | (cregs', cstates') =>\n let ctxt' := ctxt {ctxt_regs: cregs'} {ctxt_states: cstates'} in\n Some adt {shadow_ctxts: (shadow_ctxts adt) # ctxtid == ctxt'} {slog: log'}\n end\n | _ => None\n end\n | _, _ => None\n end.\n\n Definition dev_store_spec (gfn: Z64) (reg: Z) (cbndx: Z) (index: Z) (adt: RData) : option RData :=\n match gfn, icore adt with\n | VZ64 gfn, false =>\n rely is_smmu index; rely is_smmu_cfg cbndx;\n let adt := query_oracle adt in\n let log' := (SEVENT (curid adt) (OPT_GET (cur_vmid adt))) :: (slog adt) in\n let vmid := cur_vmid adt in\n let vcpuid := cur_vcpuid adt in\n let ctxtid := ctxt_id vmid vcpuid in\n let smmuid := smmu_id index cbndx in\n rely smmuid @ (smmu_vmid (shared adt)) =? vmid;\n let spt := (SMMU_TTBR index cbndx) @ (spt_pt (spts (shared adt))) in\n let ctxt := ctxtid @ (shadow_ctxts adt) in\n let val := get_shadow_ctxt_specx reg (ctxt_regs ctxt) (ctxt_states ctxt) in\n if vmid =? HOSTVISOR then\n match gfn @ spt with\n | (pfn, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? HOSTVISOR\n then Some adt {shared: (shared adt) {flatmem: (flatmem (shared adt)) # pfn == val}} {slog: log'}\n else Some adt {slog: log'}\n end\n else\n rely (vm_state (VS (cur_vmid adt) @ (vminfos (shared adt))) =? VERIFIED);\n match gfn @ spt with\n | (pfn, pte) =>\n if pfn =? 0 then None else\n if s2_owner (pfn @ (s2page (shared adt))) =? INVALID then None\n else\n if s2_count (pfn @ (s2page (shared adt))) =? 0\n then Some adt {shared: (shared adt) {flatmem: (flatmem (shared adt)) # pfn == val}} {slog: log'}\n else Some adt {slog: log'}\n end\n | _, _ => None\n end.\n\nEnd TrapHandlerSpec.\n\nSection TrapHandlerSpecLow.\n\n Context `{real_params: RealParams}.\n\n Notation LDATA := RData.\n\n Notation LDATAOps := (cdata (cdata_ops := TrapHandlerRaw_ops) LDATA).\n\n Definition host_hvc_handler_spec0 (adt: RData) : option RData :=\n host_hvc_handler_raw_spec adt.\n\n Definition host_npt_handler_spec0 (adt: RData) : option RData :=\n host_npt_handler_raw_spec adt.\n\n Definition host_vcpu_run_handler_spec0 (adt: RData) : option RData :=\n host_vcpu_run_handler_raw_spec adt.\n\n Definition vm_exit_handler_spec0 (adt: RData) : option RData :=\n vm_exit_handler_raw_spec adt.\n\n Definition mem_load_spec0 (gfn: Z64) (reg: Z) (adt: RData) : option RData :=\n mem_load_ref_spec gfn reg adt.\n\n Definition mem_store_spec0 (gfn: Z64) (reg: Z) (adt: RData) : option RData :=\n mem_store_ref_spec gfn reg adt.\n\n Definition dev_load_spec0 (gfn: Z64) (reg: Z) (cbndx: Z) (index: Z) (adt: RData) : option RData :=\n dev_load_ref_spec gfn reg cbndx index adt.\n\n Definition dev_store_spec0 (gfn: Z64) (reg: Z) (cbndx: Z) (index: Z) (adt: RData) : option RData :=\n dev_load_ref_spec gfn reg cbndx index adt.\n\n Inductive host_hvc_handler_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | host_hvc_handler_spec_low_intro s (WB: _ -> Prop) m'0 labd labd'\n (Hinv: high_level_invariant labd)\n (Hspec: host_hvc_handler_spec labd = Some labd'):\n host_hvc_handler_spec_low_step s WB nil (m'0, labd) Vundef (m'0, labd').\n\n Inductive host_npt_handler_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | host_npt_handler_spec_low_intro s (WB: _ -> Prop) m'0 labd labd'\n (Hinv: high_level_invariant labd)\n (Hspec: host_npt_handler_spec labd = Some labd'):\n host_npt_handler_spec_low_step s WB nil (m'0, labd) Vundef (m'0, labd').\n\n Inductive host_vcpu_run_handler_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | host_vcpu_run_handler_spec_low_intro s (WB: _ -> Prop) m'0 labd labd'\n (Hinv: high_level_invariant labd)\n (Hspec: host_vcpu_run_handler_spec labd = Some labd'):\n host_vcpu_run_handler_spec_low_step s WB nil (m'0, labd) Vundef (m'0, labd').\n\n Inductive vm_exit_handler_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | vm_exit_handler_spec_low_intro s (WB: _ -> Prop) m'0 labd labd'\n (Hinv: high_level_invariant labd)\n (Hspec: vm_exit_handler_spec labd = Some labd'):\n vm_exit_handler_spec_low_step s WB nil (m'0, labd) Vundef (m'0, labd').\n\n Inductive mem_load_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | mem_load_spec_low_intro s (WB: _ -> Prop) m'0 labd labd' gfn reg\n (Hinv: high_level_invariant labd)\n (Hspec: mem_load_spec (VZ64 (Int64.unsigned gfn)) (Int.unsigned reg) labd = Some labd'):\n mem_load_spec_low_step s WB ((Vlong gfn)::(Vint reg)::nil) (m'0, labd) Vundef (m'0, labd').\n\n Inductive mem_store_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | mem_store_spec_low_intro s (WB: _ -> Prop) m'0 labd labd' gfn reg\n (Hinv: high_level_invariant labd)\n (Hspec: mem_store_spec (VZ64 (Int64.unsigned gfn)) (Int.unsigned reg) labd = Some labd'):\n mem_store_spec_low_step s WB ((Vlong gfn)::(Vint reg)::nil) (m'0, labd) Vundef (m'0, labd').\n\n Inductive dev_load_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | dev_load_spec_low_intro s (WB: _ -> Prop) m'0 labd labd' gfn reg cbndx index\n (Hinv: high_level_invariant labd)\n (Hspec: dev_load_spec (VZ64 (Int64.unsigned gfn)) (Int.unsigned reg) (Int.unsigned cbndx) (Int.unsigned index) labd = Some labd'):\n dev_load_spec_low_step s WB ((Vlong gfn)::(Vint reg)::(Vint cbndx)::(Vint index)::nil) (m'0, labd) Vundef (m'0, labd').\n\n Inductive dev_store_spec_low_step `{StencilOps} `{Mem.MemoryModelOps} `{UseMemWithData mem}:\n sextcall_sem (mem := mwd LDATAOps) :=\n | dev_store_spec_low_intro s (WB: _ -> Prop) m'0 labd labd' gfn reg cbndx index\n (Hinv: high_level_invariant labd)\n (Hspec: dev_store_spec (VZ64 (Int64.unsigned gfn)) (Int.unsigned reg) (Int.unsigned cbndx) (Int.unsigned index) labd = Some labd'):\n dev_store_spec_low_step s WB ((Vlong gfn)::(Vint reg)::(Vint cbndx)::(Vint index)::nil) (m'0, labd) Vundef (m'0, labd').\n\n Section WITHMEM.\n\n Context `{Hstencil: Stencil}.\n Context `{Hmem: Mem.MemoryModelX}.\n Context `{Hmwd: UseMemWithData mem}.\n\n Definition host_hvc_handler_spec_low: compatsem LDATAOps :=\n csem host_hvc_handler_spec_low_step (type_of_list_type nil) Tvoid.\n\n Definition host_npt_handler_spec_low: compatsem LDATAOps :=\n csem host_npt_handler_spec_low_step (type_of_list_type nil) Tvoid.\n\n Definition host_vcpu_run_handler_spec_low: compatsem LDATAOps :=\n csem host_vcpu_run_handler_spec_low_step (type_of_list_type nil) Tvoid.\n\n Definition vm_exit_handler_spec_low: compatsem LDATAOps :=\n csem vm_exit_handler_spec_low_step (type_of_list_type nil) Tvoid.\n\n Definition mem_load_spec_low: compatsem LDATAOps :=\n csem mem_load_spec_low_step (type_of_list_type (Tint64::Tint32::nil)) Tvoid.\n\n Definition mem_store_spec_low: compatsem LDATAOps :=\n csem mem_store_spec_low_step (type_of_list_type (Tint64::Tint32::nil)) Tvoid.\n\n Definition dev_load_spec_low: compatsem LDATAOps :=\n csem dev_load_spec_low_step (type_of_list_type (Tint64::Tint32::Tint32::Tint32::nil)) Tvoid.\n\n Definition dev_store_spec_low: compatsem LDATAOps :=\n csem dev_store_spec_low_step (type_of_list_type (Tint64::Tint32::Tint32::Tint32::nil)) Tvoid.\n\n End WITHMEM.\n\nEnd TrapHandlerSpecLow.\n\n", "meta": {"author": "VeriGu", "repo": "VRM-proof", "sha": "9e3c9751f31713a133a0a7e98f3d4c9600ca7bde", "save_path": "github-repos/coq/VeriGu-VRM-proof", "path": "github-repos/coq/VeriGu-VRM-proof/VRM-proof-9e3c9751f31713a133a0a7e98f3d4c9600ca7bde/sekvm/TrapHandler/Spec.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. NO", "lm_q1_score": 0.5156199157230156, "lm_q2_score": 0.18952109819626003, "lm_q1q2_score": 0.09772085267968895}}
{"text": "(** * ImpParser: Lexing and Parsing in Coq *)\n\n(** The development of the Imp language in [Imp.v] completely ignores\n issues of concrete syntax -- how an ascii string that a programmer\n might write gets translated into abstract syntax trees defined by\n the datatypes [aexp], [bexp], and [com]. In this chapter, we\n illustrate how the rest of the story can be filled in by building\n a simple lexical analyzer and parser using Coq's functional\n programming facilities.\n\n It is not important to understand all the details here (and\n accordingly, the explanations are fairly terse and there are no\n exercises). The main point is simply to demonstrate that it can\n be done. You are invited to look through the code -- most of it\n is not very complicated, though the parser relies on some\n \"monadic\" programming idioms that may require a little work to\n make out -- but most readers will probably want to just skim down\n to the Examples section at the very end to get the punchline. *)\n\n(* ####################################################### *)\n(** * Internals *)\n\nRequire Import Coq.Strings.String.\nRequire Import Coq.Strings.Ascii.\nRequire Import Coq.Arith.Arith.\nRequire Import Coq.Arith.EqNat.\nRequire Import Coq.Lists.List.\nImport ListNotations.\nRequire Import Maps.\nRequire Import Imp.\n\n(* ####################################################### *)\n(** ** Lexical Analysis *)\n\nDefinition isWhite (c : ascii) : bool :=\n let n := nat_of_ascii c in\n orb (orb (beq_nat n 32) (* space *)\n (beq_nat n 9)) (* tab *)\n (orb (beq_nat n 10) (* linefeed *)\n (beq_nat n 13)). (* Carriage return. *)\n\nNotation \"x '<=?' y\" := (leb x y)\n (at level 70, no associativity) : nat_scope.\n\nDefinition isLowerAlpha (c : ascii) : bool :=\n let n := nat_of_ascii c in\n andb (97 <=? n) (n <=? 122).\n\nDefinition isAlpha (c : ascii) : bool :=\n let n := nat_of_ascii c in\n orb (andb (65 <=? n) (n <=? 90))\n (andb (97 <=? n) (n <=? 122)).\n\nDefinition isDigit (c : ascii) : bool :=\n let n := nat_of_ascii c in\n andb (48 <=? n) (n <=? 57).\n\nInductive chartype := white | alpha | digit | other.\n\nDefinition classifyChar (c : ascii) : chartype :=\n if isWhite c then\n white\n else if isAlpha c then\n alpha\n else if isDigit c then\n digit\n else\n other.\n\nFixpoint list_of_string (s : string) : list ascii :=\n match s with\n | EmptyString => []\n | String c s => c :: (list_of_string s)\n end.\n\nFixpoint string_of_list (xs : list ascii) : string :=\n fold_right String EmptyString xs.\n\nDefinition token := string.\n\nFixpoint tokenize_helper (cls : chartype) (acc xs : list ascii)\n : list (list ascii) :=\n let tk := match acc with [] => [] | _::_ => [rev acc] end in\n match xs with\n | [] => tk\n | (x::xs') =>\n match cls, classifyChar x, x with\n | _, _, \"(\" =>\n tk ++ [\"(\"]::(tokenize_helper other [] xs')\n | _, _, \")\" =>\n tk ++ [\")\"]::(tokenize_helper other [] xs')\n | _, white, _ =>\n tk ++ (tokenize_helper white [] xs')\n | alpha,alpha,x =>\n tokenize_helper alpha (x::acc) xs'\n | digit,digit,x =>\n tokenize_helper digit (x::acc) xs'\n | other,other,x =>\n tokenize_helper other (x::acc) xs'\n | _,tp,x =>\n tk ++ (tokenize_helper tp [x] xs')\n end\n end %char.\n\nDefinition tokenize (s : string) : list string :=\n map string_of_list (tokenize_helper white [] (list_of_string s)).\n\nExample tokenize_ex1 :\n tokenize \"abc12==3 223*(3+(a+c))\" %string\n = [\"abc\"; \"12\"; \"==\"; \"3\"; \"223\";\n \"*\"; \"(\"; \"3\"; \"+\"; \"(\";\n \"a\"; \"+\"; \"c\"; \")\"; \")\"]%string.\nProof. reflexivity. Qed.\n\n(* ####################################################### *)\n(** ** Parsing *)\n\n(** *** Options With Errors *)\n\n(** An [option] type with error messages: *)\n\nInductive optionE (X:Type) : Type :=\n | SomeE : X -> optionE X\n | NoneE : string -> optionE X.\n\nImplicit Arguments SomeE [[X]].\nImplicit Arguments NoneE [[X]].\n\n(** Some syntactic sugar to make writing nested match-expressions on\n optionE more convenient. *)\n\nNotation \"'DO' ( x , y ) <== e1 ; e2\"\n := (match e1 with\n | SomeE (x,y) => e2\n | NoneE err => NoneE err\n end)\n (right associativity, at level 60).\n\nNotation \"'DO' ( x , y ) <-- e1 ; e2 'OR' e3\"\n := (match e1 with\n | SomeE (x,y) => e2\n | NoneE err => e3\n end)\n (right associativity, at level 60, e2 at next level).\n\n(** *** Symbol Table *)\n\n(** Build a mapping from [tokens] to [nats]. A real parser would do\n this incrementally as it encountered new symbols, but passing\n around the symbol table inside the parsing functions is a bit\n inconvenient, so instead we do it as a first pass. *)\n\nFixpoint build_symtable (xs : list token) (n : nat)\n : (token -> nat) :=\n match xs with\n | [] => (fun s => n)\n | x::xs =>\n if (forallb isLowerAlpha (list_of_string x))\n then (fun s => if string_dec s x then n\n else (build_symtable xs (S n) s))\n else build_symtable xs n\n end.\n\n(** *** Generic Combinators for Building Parsers *)\n\nOpen Scope string_scope.\n\nDefinition parser (T : Type) :=\n list token -> optionE (T * list token).\n\nFixpoint many_helper {T} (p : parser T) acc steps xs :=\n match steps, p xs with\n | 0, _ =>\n NoneE \"Too many recursive calls\"\n | _, NoneE _ =>\n SomeE ((rev acc), xs)\n | S steps', SomeE (t, xs') =>\n many_helper p (t::acc) steps' xs'\n end.\n\n(** A (step-indexed) parser that expects zero or more [p]s: *)\n\nFixpoint many {T} (p : parser T) (steps : nat) : parser (list T) :=\n many_helper p [] steps.\n\n(** A parser that expects a given token, followed by [p]: *)\n\nDefinition firstExpect {T} (t : token) (p : parser T)\n : parser T :=\n fun xs => match xs with\n | x::xs' =>\n if string_dec x t\n then p xs'\n else NoneE (\"expected '\" ++ t ++ \"'.\")\n | [] =>\n NoneE (\"expected '\" ++ t ++ \"'.\")\n end.\n\n(** A parser that expects a particular token: *)\n\nDefinition expect (t : token) : parser unit :=\n firstExpect t (fun xs => SomeE(tt, xs)).\n\n(** *** A Recursive-Descent Parser for Imp *)\n\n(** Identifiers: *)\n\nDefinition parseIdentifier (symtable :string->nat)\n (xs : list token)\n : optionE (id * list token) :=\nmatch xs with\n| [] => NoneE \"Expected identifier\"\n| x::xs' =>\n if forallb isLowerAlpha (list_of_string x) then\n SomeE (Id (symtable x), xs')\n else\n NoneE (\"Illegal identifier:'\" ++ x ++ \"'\")\nend.\n\n(** Numbers: *)\n\nDefinition parseNumber (xs : list token)\n : optionE (nat * list token) :=\nmatch xs with\n| [] => NoneE \"Expected number\"\n| x::xs' =>\n if forallb isDigit (list_of_string x) then\n SomeE (fold_left\n (fun n d =>\n 10 * n + (nat_of_ascii d -\n nat_of_ascii \"0\"%char))\n (list_of_string x)\n 0,\n xs')\n else\n NoneE \"Expected number\"\nend.\n\n(** Parse arithmetic expressions *)\n\nFixpoint parsePrimaryExp (steps:nat) symtable\n (xs : list token)\n : optionE (aexp * list token) :=\n match steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (i, rest) <-- parseIdentifier symtable xs ;\n SomeE (AId i, rest)\n OR DO (n, rest) <-- parseNumber xs ;\n SomeE (ANum n, rest)\n OR (DO (e, rest) <== firstExpect \"(\"\n (parseSumExp steps' symtable) xs;\n DO (u, rest') <== expect \")\" rest ;\n SomeE(e,rest'))\n end\n\nwith parseProductExp (steps:nat) symtable\n (xs : list token) :=\n match steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (e, rest) <==\n parsePrimaryExp steps' symtable xs ;\n DO (es, rest') <==\n many (firstExpect \"*\" (parsePrimaryExp steps' symtable))\n steps' rest;\n SomeE (fold_left AMult es e, rest')\n end\n\nwith parseSumExp (steps:nat) symtable (xs : list token) :=\n match steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (e, rest) <==\n parseProductExp steps' symtable xs ;\n DO (es, rest') <==\n many (fun xs =>\n DO (e,rest') <--\n firstExpect \"+\"\n (parseProductExp steps' symtable) xs;\n SomeE ( (true, e), rest')\n OR DO (e,rest') <==\n firstExpect \"-\"\n (parseProductExp steps' symtable) xs;\n SomeE ( (false, e), rest'))\n steps' rest;\n SomeE (fold_left (fun e0 term =>\n match term with\n (true, e) => APlus e0 e\n | (false, e) => AMinus e0 e\n end)\n es e,\n rest')\n end.\n\nDefinition parseAExp := parseSumExp.\n\n(** Parsing boolean expressions: *)\n\nFixpoint parseAtomicExp (steps:nat) (symtable : string->nat)\n (xs : list token) :=\nmatch steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (u,rest) <-- expect \"true\" xs;\n SomeE (BTrue,rest)\n OR DO (u,rest) <-- expect \"false\" xs;\n SomeE (BFalse,rest)\n OR DO (e,rest) <-- \n firstExpect \"not\" \n (parseAtomicExp steps' symtable) \n xs;\n SomeE (BNot e, rest)\n OR DO (e,rest) <-- \n firstExpect \"(\" \n (parseConjunctionExp steps' symtable) xs;\n (DO (u,rest') <== expect \")\" rest; \n SomeE (e, rest'))\n OR DO (e, rest) <== parseProductExp steps' symtable xs;\n (DO (e', rest') <--\n firstExpect \"==\" \n (parseAExp steps' symtable) rest;\n SomeE (BEq e e', rest')\n OR DO (e', rest') <--\n firstExpect \"<=\" \n (parseAExp steps' symtable) rest;\n SomeE (BLe e e', rest')\n OR\n NoneE \n \"Expected '==' or '<=' after arithmetic expression\")\nend\n\nwith parseConjunctionExp (steps:nat)\n (symtable : string->nat)\n (xs : list token) :=\n match steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (e, rest) <==\n parseAtomicExp steps' symtable xs ;\n DO (es, rest') <==\n many (firstExpect \"&&\"\n (parseAtomicExp steps' symtable))\n steps' rest;\n SomeE (fold_left BAnd es e, rest')\n end.\n\nDefinition parseBExp := parseConjunctionExp.\n\nCheck parseConjunctionExp.\n\nDefinition testParsing {X : Type}\n (p : nat -> (string -> nat) ->\n list token ->\n optionE (X * list token))\n (s : string) :=\n let t := tokenize s in \n p 100 (build_symtable t 0) t.\n\n(*\nEval compute in \n testParsing parseProductExp \"x*y*(x*x)*x\".\n\nEval compute in \n testParsing parseConjunctionExp \"not((x==x||x*x<=(x*x)*x)&&x==x\". \n*)\n\n(** Parsing commands: *)\n\nFixpoint parseSimpleCommand (steps:nat) \n (symtable:string->nat) \n (xs : list token) :=\n match steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (u, rest) <-- expect \"SKIP\" xs;\n SomeE (SKIP, rest)\n OR DO (e,rest) <--\n firstExpect \"IF\" (parseBExp steps' symtable) xs;\n DO (c,rest') <==\n firstExpect \"THEN\" \n (parseSequencedCommand steps' symtable) rest;\n DO (c',rest'') <==\n firstExpect \"ELSE\" \n (parseSequencedCommand steps' symtable) rest';\n DO (u,rest''') <==\n expect \"END\" rest'';\n SomeE(IFB e THEN c ELSE c' FI, rest''')\n OR DO (e,rest) <--\n firstExpect \"WHILE\" \n (parseBExp steps' symtable) xs;\n DO (c,rest') <==\n firstExpect \"DO\" \n (parseSequencedCommand steps' symtable) rest;\n DO (u,rest'') <==\n expect \"END\" rest';\n SomeE(WHILE e DO c END, rest'')\n OR DO (i, rest) <==\n parseIdentifier symtable xs;\n DO (e, rest') <==\n firstExpect \":=\" (parseAExp steps' symtable) rest;\n SomeE(i ::= e, rest')\n end\n\nwith parseSequencedCommand (steps:nat)\n (symtable:string->nat)\n (xs : list token) :=\n match steps with\n | 0 => NoneE \"Too many recursive calls\"\n | S steps' =>\n DO (c, rest) <==\n parseSimpleCommand steps' symtable xs;\n DO (c', rest') <--\n firstExpect \";;\" \n (parseSequencedCommand steps' symtable) rest;\n SomeE(c ;; c', rest')\n OR\n SomeE(c, rest)\n end.\n\nDefinition bignumber := 1000.\n\nDefinition parse (str : string) : optionE (com * list token) :=\n let tokens := tokenize str in\n parseSequencedCommand bignumber\n (build_symtable tokens 0) tokens.\n\n(* ####################################################### *)\n(** * Examples *)\n\n(*\nCompute parse \"\n IF x == y + 1 + 2 - y * 6 + 3 THEN\n x := x * 1;;\n y := 0\n ELSE\n SKIP\n END \".\n====>\n SomeE\n (IFB BEq (AId (Id 0))\n (APlus\n (AMinus (APlus (APlus (AId (Id 1)) (ANum 1)) (ANum 2))\n (AMult (AId (Id 1)) (ANum 6)))\n (ANum 3))\n THEN Id 0 ::= AMult (AId (Id 0)) (ANum 1);; Id 1 ::= ANum 0\n ELSE SKIP FI, [])\n*)\n\n(*\nCompute parse \"\n SKIP;;\n z:=x*y*(x*x);;\n WHILE x==x DO\n IF z <= z*z && not x == 2 THEN\n x := z;;\n y := z\n ELSE\n SKIP\n END;;\n SKIP\n END;;\n x:=z \".\n====>\n SomeE\n (SKIP;;\n Id 0 ::= AMult (AMult (AId (Id 1)) (AId (Id 2)))\n (AMult (AId (Id 1)) (AId (Id 1)));;\n WHILE BEq (AId (Id 1)) (AId (Id 1)) DO\n IFB BAnd (BLe (AId (Id 0)) (AMult (AId (Id 0)) (AId (Id 0))))\n (BNot (BEq (AId (Id 1)) (ANum 2)))\n THEN Id 1 ::= AId (Id 0);; Id 2 ::= AId (Id 0)\n ELSE SKIP FI;;\n SKIP\n END;;\n Id 1 ::= AId (Id 0),\n [])\n*)\n\n(*\nCompute parse \"\n SKIP;;\n z:=x*y*(x*x);;\n WHILE x==x DO\n IF z <= z*z && not x == 2 THEN\n x := z;;\n y := z\n ELSE\n SKIP\n END;;\n SKIP\n END;;\n x:=z \".\n=====>\n SomeE\n (SKIP;;\n Id 0 ::= AMult (AMult (AId (Id 1)) (AId (Id 2)))\n (AMult (AId (Id 1)) (AId (Id 1)));;\n WHILE BEq (AId (Id 1)) (AId (Id 1)) DO\n IFB BAnd (BLe (AId (Id 0)) (AMult (AId (Id 0)) (AId (Id 0))))\n (BNot (BEq (AId (Id 1)) (ANum 2)))\n THEN Id 1 ::= AId (Id 0);;\n Id 2 ::= AId (Id 0)\n ELSE SKIP\n FI;;\n SKIP\n END;;\n Id 1 ::= AId (Id 0),\n []).\n*)\n\n(** $Date: 2016-05-26 12:03:56 -0400 (Thu, 26 May 2016) $ *)\n", "meta": {"author": "colinmccabe", "repo": "software-foundations", "sha": "d581e89d47978ed9acc9e2a64ba6209a614fd9f1", "save_path": "github-repos/coq/colinmccabe-software-foundations", "path": "github-repos/coq/colinmccabe-software-foundations/software-foundations-d581e89d47978ed9acc9e2a64ba6209a614fd9f1/ImpParser.v", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.47657966593214324, "lm_q2_score": 0.2018132270610754, "lm_q1q2_score": 0.09618008033345508}}
{"text": "(*|\n###############################################\nUnderstanding how pattern matching works in Coq\n###############################################\n\n:Link: https://stackoverflow.com/q/72930287\n|*)\n\n(*|\nQuestion\n********\n\nI'm currently following the `Software Foundations\n