Library mcertikos.mm.PTBitGen

***********************************************************************
*                                                                     *
*            The CertiKOS Certified Kit Operating System              *
*                                                                     *
*                   The FLINT Group, Yale University                  *
*                                                                     *
*  Copyright The FLINT Group, Yale University.  All rights reserved.  *
*  This file is distributed under the terms of the Yale University    *
*  Non-Commercial License Agreement.                                  *
*                                                                     *
*********************************************************************** 


This file provide the contextual refinement proof between MPTInit layer and MPTBit 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 GenSem.
Require Import RefinementTactic.
Require Import PrimSemantics.
Require Import XOmega.

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

Require Import PTBitGenSpec.
Require Import MPTBit.
Require Import LayerCalculusLemma.

Definition 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}.

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

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

  Section WITHMEM.

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

Definition the refinement relation: relate_RData + match_RData

    Section REFINEMENT_REL.

Relation between each entry of the page table bit map and the underline memory
        Inductive match_PT_Bit: PTBitvalProp :=
        | MATCH_PBUNDEF:
             v, match_PT_Bit PTBUndef v
        | MATCH_PTBIT:
             pb v,
              ZtoPTBit v = Some pb
              → match_PT_Bit pb (Vint (Int.repr v)).

Relation between page table bit map and the underline memory
        Inductive match_PTP: stencilPTBitMapmemmeminjProp :=
        | MATCH_PTP: ptb m b f s,
                       ( ofs, 0 ofs < num_proc
                                    ( v, Mem.load Mint32 m b (ofs×4) = Some v
                                               Mem.valid_access m Mint32 b (ofs×4) Writable
                                               match_PT_Bit (ZMap.get ofs ptb) v))
                       → find_symbol s PTP_LOC = Some b
                       → match_PTP s ptb m f.

Relation between the new raw data at the higher layer with the mememory at lower layer
        Inductive match_RData: stencilHDATAmemmeminjProp :=
        | MATCH_RDATA:
             hadt m f s,
              match_PTP s (pb hadt) m f
              → match_RData s hadt m f.

Relation between raw data at two layers
        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));
              LAT_re: LAT ladt = LAT hadt;
              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;
              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
            }.

        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 := PTP_LOC :: nil
          }.

    End REFINEMENT_REL.

Properties of relations

    Section Rel_Property.

      Lemma inject_match_correct:
         s d1 m2 f m2´ j,
          match_RData s d1 m2 f
          Mem.inject j m2 m2´
          inject_incr (Mem.flat_inj (genv_next s)) j
          match_RData s d1 m2´ (compose_meminj f j).
      Proof.
        inversion 1; subst; intros.
        inv H0.
        assert (HFB0: j b = Some (b, 0)).
        {
          eapply stencil_find_symbol_inject´; eauto.
        }
        econstructor; eauto; intros.
        econstructor; eauto; intros.
        specialize (H3 _ H0).
        destruct H3 as [v[HL[HV HM]]].
        specialize (Mem.load_inject _ _ _ _ _ _ _ _ _ H1 HL HFB0).
        repeat rewrite Z.add_0_r; intros [[HLD´ HV´]].
        refine_split´; eauto.
        specialize(Mem.valid_access_inject _ _ _ _ _ _ _ _ _ HFB0 H1 HV).
        rewrite Z.add_0_r; trivial.
        inv HM. constructor.
        inv HV´. constructor; auto.
      Qed.

      Lemma store_match_correct:
         s abd m0 m0´ f b2 v chunk,
          match_RData s abd m0 f
          ( i b,
             In i new_glbl
             find_symbol s i = Some bb b2) →
          Mem.store chunk m0 b2 v = Some m0´
          match_RData s abd m0´ f.
      Proof.
        intros. inv H. inv H2.
        econstructor; eauto.
        econstructor; eauto.
        intros. specialize (H _ H2).
        destruct H as [v0[HL[HV HM]]].
        eapply H0 in H3; simpl; eauto.
        repeat rewrite (Mem.load_store_other _ _ _ _ _ _ H1); auto.
        refine_split´; eauto.
        eapply Mem.store_valid_access_1; eauto.
      Qed.

      Lemma storebytes_match_correct:
         s abd m0 m0´ f b2 v ,
          match_RData s abd m0 f
          ( i b,
             In i new_glbl
             find_symbol s i = Some bb b2) →
          Mem.storebytes m0 b2 v = Some m0´
          match_RData s abd m0´ f.
      Proof.
        intros. inv H. inv H2.
        econstructor; eauto.
        econstructor; eauto.
        intros. specialize (H _ H2).
        destruct H as [v0[HL[HV HM]]].
        eapply H0 in H3; simpl; eauto.
        repeat rewrite (Mem.load_storebytes_other _ _ _ _ _ H1); eauto.
        refine_split´; eauto.
        eapply Mem.storebytes_valid_access_1; eauto.
      Qed.

      Lemma free_match_correct:
         s abd m0 m0´ f ofs sz b2,
          match_RData s abd m0 f
          ( i b,
             In i new_glbl
             find_symbol s i = Some bb b2) →
          Mem.free m0 b2 ofs sz = Some m0´
          match_RData s abd m0´ f.
      Proof.
        intros; inv H; inv H2.
        econstructor; eauto.
        econstructor; eauto.
        intros. specialize (H _ H2).
        destruct H as [v1[HL1 [HV1 HM]]].
        eapply H0 in H3; simpl; eauto.
        repeat rewrite (Mem.load_free _ _ _ _ _ H1); auto.
        refine_split´; eauto.
        eapply Mem.valid_access_free_1; eauto.
      Qed.

      Lemma alloc_match_correct:
         s abd m´0 m´1 f ofs sz b0 b´1,
          match_RData s abd m´0 f
          Mem.alloc m´0 ofs sz = (m´1, b´1)
           b0 = Some (b´1, 0%Z)
          ( b : block, b b0 b = f b) →
          inject_incr f
          ( i b,
             In i new_glbl
             find_symbol s i = Some bb b0) →
          match_RData s abd m´1 .
      Proof.
        intros. rename H1 into HF1, H2 into HB. inv H; inv H1.
        econstructor; eauto.
        econstructor; eauto.
        intros. specialize (H _ H1).
        destruct H as [v1[HL1 [HV1 HM]]].
        refine_split´; eauto.
        apply (Mem.load_alloc_other _ _ _ _ _ H0); auto.
        eapply Mem.valid_access_alloc_other; eauto.
      Qed.

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.
        - apply inject_match_correct.
        - apply store_match_correct.
        - apply alloc_match_correct.
        - apply free_match_correct.
        - apply storebytes_match_correct.
        - intros. eapply relate_incr; eauto.
      Qed.

    End Rel_Property.

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

    Section OneStep_Forward_Relation.

      Ltac pattern2_refinement_simpl:=
        pattern2_refinement_simpl´ (@relate_AbData).

      Section FRESH_PRIM.

        Lemma isused_spec_ref:
          compatsim (crel HDATA LDATA) (gensem is_pt_used_spec) isused_spec_low.
        Proof.
          compatsim_simpl (@match_AbData). inv H.
          assert(HOS: kernel_mode d2 0 Int.unsigned i < num_proc).
          {
            simpl; inv match_related.
            functional inversion H2; repeat (split; trivial); congruence.
          }
          destruct HOS as [Hkern HOS].
          pose proof H0 as HBit.
          specialize (H0 _ HOS); destruct H0 as [v[HL[_ HM]]].
          assert (HP: v = Vint z).
          {
            functional inversion H2; subst; rewrite H6 in HM; inv HM;
            apply Z2PTBit_correct in H7; inv H7;
            rewrite <- Int.repr_unsigned with z; rewrite <- H; trivial.
          }
          refine_split; eauto; econstructor; eauto.
        Qed.

        Lemma setbit_spec_ref:
          compatsim (crel HDATA LDATA) (gensem set_pt_bit_spec) setbit_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          assert (Hkern: kernel_mode d2 0 Int.unsigned i < num_proc).
          {
            inv match_related. functional inversion H1; subst.
            repeat (split; eauto); try congruence.
          }
          destruct Hkern as [Hkern HOS].
          inv H. rename H0 into HMB; destruct (HMB _ HOS) as [v[HL[HV HM]]].
          specialize (Mem.valid_access_store _ _ _ _ (Vint i0) HV); intros [ HST].
          refine_split.
          - econstructor; eauto.
            lift_unfold. split; eauto.
          - constructor.
          - pose proof H1 as Hspec.
            functional inversion Hspec; subst.
            split; eauto; pattern2_refinement_simpl.
            econstructor; simpl; eauto.
            econstructor; eauto; intros.
            destruct (zeq ofs (Int.unsigned i)); subst.
            +
              refine_split´; eauto.
              eapply Mem.load_store_same; eauto.
              eapply Mem.store_valid_access_1; eauto.
              rewrite ZMap.gss.
              apply Z2PTBit_correct in H6.
              rewrite <- Int.repr_unsigned with i0.
              destruct ; try constructor; inv H6; trivial.

            +
              specialize (HMB _ H).
              destruct HMB as [[HL´[HV´ HM2]]].
              refine_split´; eauto.
              rewrite <- (Mem.load_store_other _ _ _ _ _ _ HST) in HL´; eauto.
              simpl; right. destruct (zlt ofs (Int.unsigned i)); [left; xomega|right; xomega].
              eapply Mem.store_valid_access_1; eauto.
              rewrite ZMap.gso; trivial.
          - apply inject_incr_refl.
        Qed.

      End FRESH_PRIM.

      Section PASSTHROUGH_PRIM.

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

        Lemma passthrough_correct:
          sim (crel HDATA LDATA) mptbit_passthrough mptinit.
        Proof.
          sim_oplus.
          - apply fload_sim.
          - apply fstore_sim.
          - apply flatmem_copy_sim.
          - apply vmxinfo_get_sim.
          - apply palloc_sim.
          - apply pfree_sim.
          - apply setPT_sim.
          - apply ptRead_sim.
          - apply ptReadPDE_sim.
          - apply ptFreePDE0_sim.
          - apply ptInsert0_sim.
          - apply ptRmv0_sim.
          - apply pt_init_sim.
          - apply ptin_sim.
          - apply ptout_sim.
          - apply container_get_parent_sim.
          - apply container_get_quota_sim.
          - apply container_get_usage_sim.
          - apply container_can_consume_sim.
          - apply container_split_sim.
          - apply cli_sim.
          - apply sti_sim.
          - apply cons_buf_read_sim.
          - apply serial_putc_sim.
          - apply serial_intr_disable_sim.
          - apply serial_intr_enable_sim.
          - apply trapin_sim.
          - apply trapout_sim.
          - apply hostin_sim.
          - apply hostout_sim.
          - apply trap_info_get_sim.
          - apply trap_info_ret_sim.
          - layer_sim_simpl.
            + eapply load_correct2.
            + eapply store_correct2.
        Qed.

      End PASSTHROUGH_PRIM.

    End OneStep_Forward_Relation.

  End WITHMEM.

End Refinement.