Library mcertikos.mm.ALGen


This file provide the contextual refinement proof between MALOp layer and MAL 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 LoadStoreSem1.
Require Import AsmImplLemma.
Require Import LAsm.
Require Import RefinementTactic.
Require Import PrimSemantics.

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 LayerCalculusLemma.

Require Import MALT.
Require Import MALOp.
Require Import ALGenSpec.

Require Import AbstractDataType.

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

Definition of the refinement relation

Section Refinement.

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

  Notation HDATAOps := (cdata (cdata_ops := malt_data_ops) RData).
  Notation LDATAOps := (cdata (cdata_ops := malop_data_ops) RData).

  Section WITHMEM.

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

Definition the refinement relation: relate_RData + match_RData

    Record relate_RData (f:meminj) (hadt: RData) (ladt: RData) :=
      mkrelate_RData {
          flatmem_re: FlatMem.flatmem_inj (HP hadt) (HP ladt);
          vmxinfo_re: vmxinfo hadt = vmxinfo ladt;
          CR3_re: CR3 hadt = CR3 ladt;
          ikern_re: ikern hadt = ikern ladt;
          pg_re: pg hadt = pg ladt;
          ihost_re: ihost hadt = ihost ladt;
          AC_re: AC hadt = AC ladt;
          ti_fst_re: (fst (ti hadt)) = (fst (ti ladt));
          ti_snd_re: val_inject f (snd (ti hadt)) (snd (ti ladt));
          AT_re: AT hadt = AT ladt;
          ATC_re: ATC hadt = ATC ladt;
          nps_re: nps hadt = nps ladt;
          init_re: init hadt = init ladt;

          buffer_re: buffer hadt = buffer ladt;

          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: stencilRDatamemmeminjProp :=
    | 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.

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

    End Rel_Property.

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

    Section OneStep_Forward_Relation.

The low level specifications exist

      Section Exists.

        Lemma pfree_exist:
           habd habd´ labd i f,
            ObjPMM.pfree´_spec i habd = Some habd´
            → relate_RData f habd labd
            → labd´, pfree_spec i labd = Some labd´ relate_RData f habd´ labd´
                              kernel_mode labd.
        Proof.
          unfold pfree_spec, ObjPMM.pfree´_spec; intros until f. exist_simpl.
        Qed.

        Lemma acquire_lock_AT_ac:
           labd labd´,
            acquire_lock_AT_spec labd = Some labd´
            AC labd´ = AC labd.
        Proof.
          intros.
          unfold acquire_lock_AT_spec in H.
          subdestruct;
          inv H; simpl; reflexivity.
        Qed.

        Lemma palloc_exist:
           habd habd´ labd i id s f,
            ObjPMM.palloc´_spec id habd = Some (habd´, i)
            → relate_AbData s f habd labd
            → labd´, palloc_spec id labd = Some (labd´, i) relate_AbData s f habd´ labd´
                              kernel_mode labd.
        Proof.
          unfold palloc_spec, ObjPMM.palloc´_spec, ObjPMM.palloc_aux_spec, palloc_aux_spec in *; intros.
          revert H. pose proof H0 as HR.
          inv H0. subrewrite. simpl.
          destruct (acquire_lock_AT_spec habd) eqn:Ha; contra_inv.
          exploit (acquire_lock_AT_exist s); eauto.
          intros (labd´ & Hacq & Hre).
          rewrite Hacq.
          assert(kern: ikern labd = true ihost labd = true).
          {
            unfold acquire_lock_AT_spec in Hacq.
            clear HQ.
            subdestruct; simpl; eauto.
          }
          destruct kern as [ikern ihost].
          generalize Hacq; intro Hacqtmp.
          eapply acquire_lock_AT_ac in Hacq.
          inv Hre.
          revert HQ; subrewrite.
          destruct (cused (ZMap.get id (AC labd))); contra_inv.
          destruct ((cusage (ZMap.get id (AC labd)) <? cquota (ZMap.get id (AC labd)))%Z); contra_inv.
          - subdestruct; inv HQ.
            exploit (release_lock_AT_exist
                       s _ _
                       (labd´ {AT : ZMap.set i (ATValid true ATNorm) (AT labd´)}
                              {ATC : ZMap.set i (ATCValid 0) (ATC labd´)}
                              {AC
                               : ZMap.set id
                                          {|
                                            cquota := cquota (ZMap.get id (AC labd));
                                            cusage := cusage (ZMap.get id (AC labd)) + 1;
                                            cparent := cparent (ZMap.get id (AC labd));
                                            cchildren := cchildren (ZMap.get id (AC labd));
                                            cused := true |} (AC labd)}) f Hdestruct5); eauto.
            constructor; eauto; simpl.
            intros (labd´´ & Hacq´ & Hre´).
            rewrite Hacq´.
            refine_split´; trivial.
            exploit (release_lock_AT_exist s _ _ labd´ f Hdestruct3); eauto.
            constructor; eauto.
            intros (labd´´ & Hacq´ & Hre´).
            rewrite Hacq´.
            refine_split´; trivial.
          - subdestruct; inv HQ.
            exploit (release_lock_AT_exist s _ _ labd´ f Hdestruct2); eauto.
            constructor; eauto.
            intros (labd´´ & Hacq´ & Hre´).
            rewrite Hacq´.
            refine_split´; trivial.
          - subdestruct; inv HQ.
        Qed.

        Lemma flatmem_store_exists:
           hadt ladt hadt´ t addr v f,
            flatmem_store hadt t addr v = Some hadt´
            → relate_RData f hadt ladt
            → val_inject f v
            → ladt´,
                 flatmem_store´ ladt t addr = Some ladt´
                  relate_RData f hadt´ ladt´.
        Proof.
          unfold flatmem_store´, flatmem_store. intros.
          revert H. inv H0. subrewrite. subdestruct.
          inv HQ; simpl. refine_split´; eauto.
          constructor; trivial; simpl.
          eapply (FlatMem.store_mapped_inj f); trivial;
          assumption.
        Qed.

        Lemma fstore_exist:
           habd habd´ labd i v f,
            fstore0_spec i v habd = Some habd´
            → relate_RData f habd labd
            → labd´, fstore´_spec i v labd = Some labd´ relate_RData f habd´ labd´.
        Proof.
          unfold fstore0_spec, fstore´_spec; intros.
          revert H. pose proof H0 as HR.
          inv H0. subrewrite. subdestruct.
          eapply flatmem_store_exists; eauto.
        Qed.


        Lemma page_copy_back_exist:
           habd habd´ labd i count to f,
            page_copy_back0_spec i count to habd = Some habd´
            → relate_RData f habd labd
            → labd´, page_copy_back´_spec i count to labd = Some labd´
                              relate_RData f habd´ labd´.
        Proof.
          unfold page_copy_back0_spec, page_copy_back´_spec; intros.
          revert H. pose proof H0 as HR.
          inv H0. subrewrite. subdestruct.
          exploit page_copy_back_aux_exists; eauto.
          intros (lh´ & HCopy & Hinj).
          rewrite HCopy. refine_split´; trivial.
          inv HR.
          inv HQ; constructor; trivial; simpl.
        Qed.

        Lemma page_copy_exist:
           habd habd´ labd i count from f,
            page_copy0_spec i count from habd = Some habd´
            → relate_RData f habd labd
            → labd´, page_copy´_spec i count from labd = Some labd´
                              relate_RData f habd´ labd´.
        Proof.
          unfold page_copy0_spec, page_copy´_spec; intros.
          revert H. pose proof H0 as HR.
          inv H0. subrewrite. subdestruct.
          exploit page_copy_aux_exists; eauto.
          intros HCopy. rewrite HCopy. refine_split´; trivial.
          inv HR.
          rewrite Hdestruct10.
          reflexivity.
          inv HQ; constructor; trivial; simpl; auto.
          rewrite Hdestruct; auto.
          rewrite Hdestruct0; auto.
        Qed.

        Lemma set_at_c_exist:
           habd habd´ labd i z f,
            set_at_c0_spec i z habd = Some habd´
            → relate_RData f habd labd
            → labd´, set_at_c_spec i z labd = Some labd´ relate_RData f habd´ labd´.
        Proof.
          unfold set_at_c0_spec, set_at_c_spec; intros until f; exist_simpl; inv HR´.
        Qed.

      End Exists.

      Section FRESH_PRIM.

        Lemma pfree_spec_ref:
          compatsim (crel RData RData) (gensem ObjPMM.pfree´_spec) pfree_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit pfree_exist; eauto 1.
          intros [labd´ [HP [HM Hkern]]].
          refine_split; try econstructor; eauto. constructor.
        Qed.

        Lemma palloc_spec_ref:
          compatsim (crel RData RData) (gensem ObjPMM.palloc´_spec) palloc_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit palloc_exist; eauto 1.
          intros [labd´ [HP [HM Hkern]]].
          refine_split; try econstructor; eauto. constructor.
        Qed.

      End FRESH_PRIM.

      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 RData RData) malt_passthrough malop.
        Proof.
          sim_oplus.
          - apply fload´_sim.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit fstore_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.

          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit page_copy_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit page_copy_back_exist; eauto 1; intros [labd´ [HP HM]].
            match_external_states_simpl.

          - apply vmxinfo_get_sim.
          - apply setPG0_sim.
          - apply setCR30_sim.
          - apply get_at_c_sim.
          -
            layer_sim_simpl; compatsim_simpl (@match_AbData); intros.
            exploit set_at_c_exist; eauto 1. intros [labd´ [HP HM]].
            match_external_states_simpl.
          - apply mem_init_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 trapout0_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_correct1.
            + eapply store_correct1.
        Qed.

      End PASSTHROUGH_RPIM.

    End OneStep_Forward_Relation.

  End WITHMEM.

End Refinement.