Library mcertikos.mm.PTInitGen


This file provide the contextual refinement proof between MPTKern layer and MPTInit layer
Require Import Coqlib.
Require Import Errors.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Op.
Require Import Asm.
Require Import Events.
Require Import Globalenvs.
Require Import Smallstep.
Require Import Values.
Require Import Memory.
Require Import Maps.
Require Import CommonTactic.
Require Import AuxLemma.
Require Import FlatMemory.
Require Import AuxStateDataType.
Require Import Constant.
Require Import GlobIdent.
Require Import RealParams.
Require Import LoadStoreSem2.
Require Import AsmImplLemma.
Require Import LAsm.
Require Import RefinementTactic.
Require Import PrimSemantics.

Require Import MPTKern.
Require Import MPTInit.
Require Import AbstractDataType.

Require Import liblayers.logic.PTreeModules.
Require Import liblayers.logic.LayerLogicImpl.
Require Import liblayers.compcertx.Stencil.
Require Import liblayers.compcertx.MakeProgram.
Require Import liblayers.compat.CompatLayers.
Require Import liblayers.compat.CompatGenSem.
Require Import compcert.cfrontend.Ctypes.
Require Import PTInitGenSpec.

Require Import LayerCalculusLemma.

Require Import XOmega.

Notation of the refinement relation

Section Refinement.

  Local Open Scope string_scope.
  Local Open Scope error_monad_scope.
  Local Open Scope Z_scope.

  Context `{real_params: RealParams}.
  Context `{multi_oracle_prop: MultiOracleProp}.

  Notation HDATA := RData.
  Notation LDATA := RData.

  Notation HDATAOps := (cdata (cdata_ops := mptinit_data_ops) HDATA).
  Notation LDATAOps := (cdata (cdata_ops := mptintro_data_ops) LDATA).

  Section WITHMEM.

    Context `{Hstencil: Stencil}.
    Context `{Hmem: Mem.MemoryModelX}.
    Context `{Hmwd: UseMemWithData mem}.

Definition the refinement relation: relate_RData + match_RData


    Lemma real_relate_LATCTable:
       a1 a2,
        
        relate_LATCTable a1 a2
        relate_LATCTable (real_ATC a1) (real_LAT a2).
    Proof.
      intros.
      unfold real_ATC, real_LAT.
      generalize (Z.to_nat (real_nps - 1)).
      induction n.
      - simpl.
        intros i.
        destruct (zeq i 0) as [ → |];
          [ rewrite 2 ZMap.gss; auto using RELATE_VALID
          | rewrite 2 ZMap.gso; auto ].
      - simpl.
        remember (Z.pos (Pos.of_succ_nat n)) as .
        intros i;
          destruct (zeq i ) as [ → |];
          solve [ rewrite 2 ZMap.gss; auto using RELATE_VALID
                | rewrite 2 ZMap.gso; auto ].
    Qed.

    Record relate_RData (f: meminj) (hadt: HDATA) (ladt: LDATA) :=
      mkrelate_RData {
          flatmem_re: FlatMem.flatmem_inj (HP hadt) (HP ladt);
          vmxinfo_re: vmxinfo hadt = vmxinfo ladt;
          ikern_re: ikern ladt = ikern hadt;
          pg_re: pg ladt = pg hadt;
          ihost_re: ihost ladt = ihost hadt;
          AC_re: AC ladt = AC hadt;
          ti_fst_re: (fst (ti ladt)) = (fst (ti hadt));
          ti_snd_re: val_inject f (snd (ti hadt)) (snd (ti ladt));
          nps_re: nps ladt = nps hadt;
          PT_re: PT ladt = PT hadt;
          ptp_re: ptpool ladt = ptpool hadt;
          idpde_re: idpde ladt = idpde hadt;
          ipt_re: ipt ladt = ipt hadt;
          init_re: init ladt = init hadt;
          pperm_re: pperm ladt = pperm hadt;
          LAT_re: relate_LATCTable (ATC ladt) (LAT hadt);

          CPU_ID_re: CPU_ID hadt = CPU_ID ladt;
          cid_re: cid hadt = cid ladt;
          multi_oracle_re: multi_oracle hadt = multi_oracle ladt;
          multi_log_re: multi_log hadt = multi_log ladt;
          lock_re: lock hadt = lock ladt;

          com1_re: com1 hadt = com1 ladt;
          console_re: console hadt = console ladt;
          console_concrete_re: console_concrete hadt = console_concrete ladt;
          ioapic_re: ioapic ladt = ioapic hadt;
          lapic_re: lapic ladt = lapic hadt;
          intr_flag_re: intr_flag ladt = intr_flag hadt;
          curr_intr_num_re: curr_intr_num ladt = curr_intr_num hadt;
          in_intr_re: in_intr ladt = in_intr hadt;
          drv_serial_re: drv_serial hadt = drv_serial ladt

        }.

    Inductive match_RData: stencilHDATAmemmeminjProp :=
    | MATCH_RDATA: habd m f s, match_RData s habd m f.

    Local Hint Resolve MATCH_RDATA.

    Global Instance rel_ops: CompatRelOps HDATAOps LDATAOps :=
      {
        relate_AbData s f d1 d2 := relate_RData f d1 d2;
        match_AbData s d1 m f := match_RData s d1 m f;
        new_glbl := nil
      }.

Properties of relations

    Section Rel_Property.

Prove that after taking one step, the refinement relation still holds
      Lemma relate_incr:
         abd abd´ f ,
          relate_RData f abd abd´
          → inject_incr f
          → relate_RData abd abd´.
      Proof.
        inversion 1; subst; intros; inv H; constructor; eauto.
      Qed.

    End Rel_Property.

    Global Instance rel_prf: CompatRel HDATAOps LDATAOps.
    Proof.
      constructor; intros; simpl; trivial.
      eapply relate_incr; eauto.
    Qed.

Proofs the one-step forward simulations for the low level specifications

    Section OneStep_Forward_Relation.

The low level specifications exist

      Section Exists.

        Lemma pt_init_exist:
           habd habd´ labd i f,
            pt_init_spec i habd = ret habd´
            → relate_RData f habd labd
            → labd´, MPTKern.pt_init_spec i labd = Some labd´ relate_RData f habd´ labd´
                              kernel_mode labd.
        Proof.
          unfold MPTKern.pt_init_spec, pt_init_spec; intros until f.
          exist_simpl. eapply real_relate_LATCTable; trivial.
        Qed.

        Lemma ptin_exist:
           habd habd´ labd f,
            ptin_spec habd = Some habd´
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, ptin´_spec labd = Some labd´ relate_RData f habd´ labd´.
        Proof.
          unfold ptin_spec, ptin´_spec; intros until f.
          intros HP HR HINV. pose proof HR as HR´; inv HR. inv HINV.
          subrewrite´; subdestruct; simpl.
          rewrite e in ×.
          destruct (PMap_kern_dec (ZMap.get 0 (ptpool habd))).
          - refine_split´; eauto 1. inv HR´. inv HP. split; eauto 1.
          - elim n; auto.
        Qed.

        Lemma setPT_exist:
           habd habd´ labd i f,
            setPT_spec i habd = Some habd´
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, setPT´_spec i labd = Some labd´ relate_RData f habd´ labd´.
        Proof.
          unfold setPT_spec, setPT´_spec; intros until f.
          intros HP HR HINV. pose proof HR as HR´. inv HR. inv HINV.
          subrewrite´. subdestruct; simpl.
          destruct (PMap_valid_dec (ZMap.get i (ptpool habd))).
          - refine_split´; eauto 1. inv HR´. inv HP; split; eauto 1.
          - elim n. apply valid_PMap; auto; omega.
        Qed.

        Lemma vadr_PTX_range:
           vadr,
            0 vadr
            → vadr < 4294967296
            → 0 PTX vadr PTX vadr PTX Int.max_unsigned.
        Proof.
          unfold PTX; simpl.
          change ((Int.max_unsigned / 4096) mod 1024) with 1023.
          intros.
          assert (Hrange: 0 (vadr / 4096) mod 1024 < 1024) by
              (apply Z.mod_pos_bound; omega).
          omega.
        Qed.

        Lemma vadr_PDX_range:
           vadr,
            0 vadr
            → vadr < 4294967296
            → 0 PDX vadr PDX vadr PDX Int.max_unsigned.
        Proof.
          unfold PDX; simpl.
          change (Int.max_unsigned / 4194304) with 1023.
          intros. xomega.
        Qed.



        Lemma relate_LATCTable_gss:
           a la t1 t2 i
                 (Hre: relate_LATCTable a la)
                 (Hre_info: relate_LATCInfo t1 t2),
            relate_LATCTable (ZMap.set i t1 a)
                             (ZMap.set i t2 la).
        Proof.
          unfold relate_LATCTable in *; intros.
          destruct (zeq i i0); subst.
          - repeat rewrite ZMap.gss. eauto.
          - repeat rewrite ZMap.gso; eauto.
        Qed.

        Lemma relate_LATCTable_valid:
           a la l i,
            relate_LATCTable a la
            ZMap.get i la = LATCValid l
             n, ZMap.get i a = ATCValid n.
        Proof.
          intros. unfold relate_LATCTable in H.
          specialize (H i). inv H; try congruence.
          eauto.
        Qed.

        Lemma palloc_exist:
           habd habd´ labd i n f,
            palloc_spec n habd = Some (habd´, i)
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, lpalloc_spec n labd = Some (labd´, i) relate_RData f habd´ labd´.
        Proof.
          unfold palloc_spec, lpalloc_spec.
          intros until f; intros HP HR HH. pose proof HR as HR´.
          inv HR; revert HP.
          specialize (valid_AT_valid _ HH).
          rewrite (valid_pg_init habd) in *; eauto.
          subrewrite´; intros HV HQ.
          destruct (ikern habd); contra_inv.
          destruct (ihost habd); contra_inv.
          destruct (init habd); contra_inv.
          destruct (ipt habd); contra_inv.
          subdestruct; inv HQ.
          - assert (HATC: n, ZMap.get i (ATC labd) = ATCValid n).
            {
              specialize (HV refl_equal).
              exploit (HV i); eauto.
              - destruct a0 as (a0 & Hl). omega.
              - intros ( & Hl).
                eapply relate_LATCTable_valid; eauto.
            }
            destruct HATC as ( & ->).
            refine_split´; trivial.
            inv HR´; constructor; eauto; simpl.
            eapply relate_LATCTable_gss; eauto.
            constructor. trivial.
          - refine_split´; trivial.
            inv HR´; constructor; eauto; simpl.
          - refine_split´; trivial.
            inv HR´; constructor; eauto; simpl.
        Qed.

        Lemma ptInsertPTE_exist:
           habd habd´ labd n vadr padr perm f,
            ptInsertPTE0_spec n vadr padr perm habd = Some habd´
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, ptInsertPTE_spec n vadr padr perm labd = Some labd´
                              relate_RData f habd´ labd´.
        Proof.
          unfold ptInsertPTE_spec, ptInsertPTE0_spec, pt_Arg, pt_Arg´. intros until f.
          intros HP HR HINV; inv HR. inv HINV.
          rewrite valid_pg_init in ×.
          subrewrite´; subdestruct; simpl.
          - rewrite valid_PT_kern; trivial.
            rewrite zeq_false; [|omega].
            unfold consistent_pmap in ×.
            assert (HR1: 0 n < num_proc) by omega.
            assert (HR2: 0 vadr < adr_max) by omega.
            specialize (valid_pperm_pmap _ HR1 _ HR2).
            rewrite Hdestruct6 in valid_pperm_pmap.
            destruct (valid_pperm_pmap _ _ refl_equal) as (Hrange & HW1 & HW2).
            exploit valid_nps; eauto. intros nps_range.
            rewrite zlt_lt_true; [|rewrite_omega].
            generalize (LAT_re0 padr). subrewrite´.
            intros HRe. inv HRe.
            rewrite zle_lt_true; trivial.
            refine_split´; trivial.
            inv HP.
            constructor; eauto; simpl.
            apply relate_LATable_gss; eauto.
            constructor. Opaque Z.of_nat. simpl.
            rewrite Nat2Z.inj_succ.
            reflexivity.
          - rewrite valid_PT_kern; trivial.
            rewrite zeq_false; [|omega].
            unfold consistent_pmap in ×.
            assert (HR1: 0 n < num_proc) by omega.
            assert (HR2: 0 vadr < adr_max) by omega.
            specialize (valid_pperm_pmap _ HR1 _ HR2).
            rewrite Hdestruct6 in valid_pperm_pmap.
            destruct (valid_pperm_pmap _ _ refl_equal) as (Hrange & HW1 & HW2).
            exploit valid_nps; eauto. intros nps_range.
            rewrite zlt_lt_true; [|rewrite_omega].
            generalize (LAT_re0 padr). subrewrite´.
            intros HRe. inv HRe.
            rewrite zle_lt_true; trivial.
            refine_split´; trivial.
            inv HP.
            constructor; eauto; simpl.
            apply relate_LATable_gss; eauto.
            constructor. Opaque Z.of_nat. simpl.
            rewrite Nat2Z.inj_succ.
            reflexivity.
        Qed.

        Lemma ptAllocPDE_exist:
           habd habd´ labd n vadr v f,
            ptAllocPDE0_spec n vadr habd = Some (habd´, v)
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, ptAllocPDE_spec n vadr labd = Some (labd´, v)
                              relate_RData f habd´ labd´.
        Proof.
          unfold ptAllocPDE_spec, ptAllocPDE0_spec, pt_Arg, pt_Arg´. intros until f.
          intros HP HR HINV; pose proof HR as HR´; inv HR. pose proof HINV as HINV´. inv HINV.
          rewrite valid_pg_init in ×.
          subrewrite´; subdestruct; simpl; inv HP.
          - rewrite valid_PT_kern; trivial.
            rewrite zeq_false; [|omega].
            exploit palloc_exist; eauto.
            intros (labd´ & → & Hr). simpl.
            subst; refine_split´; trivial.
          - rewrite valid_PT_kern; trivial.
            rewrite zeq_false; [|omega].
            exploit palloc_exist; eauto.
            intros (labd´ & → & Hr).
            rewrite Hdestruct9.
            subst; refine_split´; trivial.
            inv Hr.
            constructor; try assumption; simpl.
            + eapply FlatMem.free_page_inj´; eauto.
            + rewrite ptp_re1. reflexivity.
            + rewrite pperm_re1. reflexivity.
        Qed.

        Lemma ptInsert_exist:
           habd habd´ labd n vadr padr perm v f,
            ptInsert0_spec n vadr padr perm habd = Some (habd´, v)
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, ptInsert_spec n vadr padr perm labd = Some (labd´, v)
                              relate_RData f habd´ labd´.
        Proof.
          unfold ptInsert_spec, ptInsert0_spec, pt_Arg´. intros until f.
          intros HP HR HINV; pose proof HR as HR´; inv HR.
          pose proof HINV as HINT´; inv HINV.
          rewrite valid_pg_init in ×.
          subrewrite´; subdestruct; simpl.
          - exploit ptInsertPTE_exist; eauto. inv HP.
            intros (labd´ & HP & HR).
            rewrite HP. refine_split´; trivial.
          - exploit ptAllocPDE_exist; eauto. inv HP.
            intros (labd´ & HP & HR).
            rewrite HP. rewrite zeq_true.
            refine_split´; trivial.
          - exploit ptAllocPDE_exist; eauto. inv HP.
            intros (labd´ & HP & HR).
            rewrite HP. rewrite zeq_false; trivial.
            exploit ptInsertPTE_exist; eauto.
            + eapply ptAllocPDE_high_level_inv; eauto.
            + intros (labd´´ & HP´ & HR´´).
              rewrite HP´. refine_split´; trivial.
        Qed.

        Lemma remove_notIn:
           a l,
            ¬ In a l
            Lremove a l = l.
        Proof.
          induction l; intros; trivial.
          - simpl in ×.
            destruct (LATOwner_dec a a0); subst.
            + elim H; left; trivial.
            + rewrite IHl; trivial. red; intros.
              elim H. right; trivial.
        Qed.

        Lemma remove_lenght_one:
           a l,
            count_occ LATOwner_dec l a = 1%nat
            Z.of_nat (length (Lremove a l)) =
            Z.of_nat (length l) - 1.
        Proof.
          induction l; intros.
          - inv H.
          - simpl in ×. destruct (LATOwner_dec a a0); subst.
            + destruct (LATOwner_dec a0 a0); try congruence.
              assert (notInQ: ¬ In a0 l).
              {
                red; intros.
                eapply (count_occ_In LATOwner_dec) in H0.
                inv H. rewrite H2 in H0.
                omega.
              }
              rewrite remove_notIn; eauto.
              rewrite Nat2Z.inj_succ. omega.
            + destruct (LATOwner_dec a0 a); try congruence.
              specialize (IHl H).
              simpl.
              repeat rewrite Nat2Z.inj_succ.
              rewrite IHl. omega.
        Qed.

        Lemma length_positive:
           {A: Type} (a: A) l,
            In a l
            0 < Z.of_nat (length l).
        Proof.
          induction l; intros.
          - inv H.
          - simpl. rewrite Nat2Z.inj_succ.
            omega.
        Qed.

        Lemma length_positive´:
           a l,
            count_occ LATOwner_dec l a = 1%nat
            0 < Z.of_nat (length l).
        Proof.
          intros. eapply (length_positive a); eauto.
          apply (count_occ_In LATOwner_dec). rewrite H.
          xomega.
        Qed.

        Lemma ptRmv_exist:
           habd habd´ labd n vadr z f,
            ptRmv0_spec n vadr habd = Some (habd´, z)
            → relate_RData f habd labd
            → high_level_invariant habd
            → labd´, ptRmv_spec n vadr labd = Some (labd´, z) relate_RData f habd´ labd´.
        Proof.
          unfold ptRmv_spec, ptRmv0_spec, pt_Arg´. intros until f.
          intros HP HR HINV; pose proof HR as HR´; inv HR.
          pose proof HINV as HINV´. inv HINV.
          rewrite valid_pg_init in ×.
          subrewrite´; subdestruct; simpl; inv HP.
          - rewrite valid_PT_kern; trivial.
            rewrite zeq_false; [|omega].
            unfold consistent_pmap_domain in ×.
            assert (HR1: 0 n < num_proc) by omega.
            assert (HR2: 0 vadr < adr_max) by omega.
            specialize (valid_pmap_domain _ HR1 _ HR2 _ _ Hdestruct5 _ _ Hdestruct7).
            destruct valid_pmap_domain as (HT1 & HT2 & HT3).
            exploit valid_nps; eauto. intros nps_range.
            rewrite zlt_lt_true; [|omega].
            generalize (LAT_re0 z). subrewrite´.
            intros HRe. inv HRe.
            rewrite Hdestruct8 in HT3.
            destruct HT3 as ( & HT31 & HT32).
            inv HT31.
            assert (Hlt: 0 < Z.of_nat (length ))
              by (eapply length_positive´; eauto).
            rewrite zlt_le_true; trivial.
            refine_split´; trivial.
            constructor; eauto; simpl.
            apply relate_LATable_gss; eauto.
            constructor.
            + rewrite remove_lenght_one; eauto.
          - rewrite valid_PT_kern; trivial.
            rewrite zeq_false; [|omega].
            refine_split´; trivial.
        Qed.

      End Exists.

      Lemma pt_init_spec_ref:
        compatsim (crel HDATA LDATA) (gensem pt_init_spec) pt_init_spec_low.
      Proof.
        compatsim_simpl (@match_AbData).
        exploit pt_init_exist; eauto 1.
        intros [labd´ [HP [HM Hkern]]].
        refine_split; try econstructor; try eassumption.
        constructor.
        apply inject_incr_refl.
      Qed.

      Section PASSTHROUGH_RPIM.

        Global Instance: (LoadStoreProp (hflatmem_store:= flatmem_store) (lflatmem_store:= flatmem_store)).
        Proof.
          accessor_prop_tac.
          - eapply flatmem_store_exists; eauto.
        Qed.


        Lemma passthrough_correct:
          sim (crel HDATA LDATA) mptinit_passthrough mptkern.
        Proof.
          sim_oplus.
          - apply fload_sim.
          - apply fstore_sim.
          - apply page_copy_sim.
          - apply page_copy_back_sim.
          - apply vmxinfo_get_sim.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit palloc_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit setPT_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.
          - apply ptRead_sim.
          - apply ptReadPDE_sim.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit ptInsert_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit ptRmv_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            inv match_extcall_states.
            exploit ptin_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.
          - apply ptout_sim.
          - apply container_get_parent_sim.
          - apply container_get_nchildren_sim.
          - apply container_get_quota_sim.
          - apply container_get_usage_sim.
          - apply container_can_consume_sim.
          - apply container_split_sim.
          - apply get_CPU_ID_sim.
          - apply get_curid_sim.
          - apply set_curid_sim.
          - apply set_curid_init_sim.
          - apply (release_lock_sim (valid_arg_imply:= Shared2ID1_imply)).
          - eapply acquire_lock_sim1; eauto.
            intros. inv H.
          - apply cli_sim.
          - apply sti_sim.
          - apply serial_intr_disable_sim.
          - apply serial_intr_enable_sim.
          - apply serial_putc_sim.
          - apply cons_buf_read_sim.
          - apply trapin_sim.
          - apply trapout_sim.
          - apply hostin_sim.
          - apply hostout_sim.
          - apply proc_create_postinit_sim.
          - apply trap_info_get_sim.
          - apply trap_info_ret_sim.
          - layer_sim_simpl.
            + eapply load_correct2.
            + eapply store_correct2.
        Qed.

      End PASSTHROUGH_RPIM.

    End OneStep_Forward_Relation.

  End WITHMEM.

End Refinement.