Library mcertikos.proc.SleeperGen


This file provide the contextual refinement proof between PKContext layer and PKCtxtNew 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 FutureTactic.

Require Import AbstractDataType.
Require Import LayerCalculusLemma.

Require Import PSleeper.

Require Import SleeperGenSpec.

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}.
  Context `{multi_oracle_prop: MultiOracleProp}.

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

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

  Section WITHMEM.

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

Definition the refinement relation: relate_RData + match_RData

    Section REFINEMENT_REL.

      Inductive match_sleeper: stencilSleeperZmemmeminjProp :=
      | MATCH_SLEEPER:
           sleeper (cpu:Z) (v: int) m b f s,
            Mem.load Mint32 m b (cpu × 4) = Some (Vint v) →
            Mem.valid_access m Mint32 b (cpu × 4) Writable
            ZMap.get cpu sleeper = Int.unsigned v
            find_symbol s SLEEPER_LOC = Some b
            match_sleeper s sleeper cpu 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_sleeper s (sleeper hadt) (CPU_ID 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;
            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));
            LAT_re: LAT hadt = LAT ladt;
            nps_re: nps hadt = nps ladt;
            init_re: init hadt = init ladt;

            pperm_re: pperm hadt = pperm ladt;
            PT_re: PT hadt = PT ladt;
            ptp_re: ptpool hadt = ptpool ladt;
            idpde_re: idpde hadt = idpde ladt;
            ipt_re: ipt hadt = ipt ladt;
            smspool_re: smspool hadt = smspool 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;

            kctxt_re: kctxt_inj f num_proc (kctxt hadt) (kctxt ladt)

          }.

      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 := SLEEPER_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 (Mem.load_inject _ _ _ _ _ _ _ _ _ H1 H3 HFB0). calc.
        intros [v2 [Hv2 Hinj]].
        inv Hinj. assumption.
        specialize(Mem.valid_access_inject _ _ _ _ _ _ _ _ _ HFB0 H1 H4). calc. 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.
        eapply H0 in H5.
        rewrite (Mem.load_store_other _ _ _ _ _ _ H1); auto.
        econstructor. reflexivity.
        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.
        eapply H0 in H5; simpl; eauto.
        repeat rewrite (Mem.load_storebytes_other _ _ _ _ _ H1); 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.
        generalize H5; intros Hx.
        eapply H0 in H5; simpl; eauto.
        econstructor; eauto.
        repeat rewrite (Mem.load_free _ _ _ _ _ H1); auto.
        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.
        apply (Mem.load_alloc_other _ _ _ _ _ H0). eauto.
        eapply Mem.valid_access_alloc_other. eauto. 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.
        eapply kctxt_inj_incr; 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.

        Require Import CLemmas.
        Lemma sleeper_zzz_spec_ref:
          compatsim (crel HDATA LDATA) (gensem sleeper_zzz_spec) sleeper_zzz_spec_low.
        Proof.
          compatsim_simpl (@match_AbData). inv H.
          assert(HOS: kernel_mode d2).
          {
            simpl; inv match_related.
            functional inversion H2; repeat split; trivial; congruence.
          }
          refine_split; eauto.
          econstructor; eauto.
          assert (Hcpuid: CPU_ID d1´ = CPU_ID d2).
          {
            inversion match_related. assumption.
          }
          rewrite <- Hcpuid. eassumption.
          functional inversion H2; subst.
          - unfold cpu in ×. rewrite _x in H3.
            replace 0 with (Int.unsigned Int.zero) in H3.
            apply unsigned_inj in H3. rewrite <- H3. rewrite Int.eq_true.
            apply unsigned_inj. rewrite <- H. reflexivity. reflexivity.
          - unfold cpu in ×. unfold n in ×.
            rewrite Int.eq_false.
            apply unsigned_inj. rewrite <- H. reflexivity.
            intros Hf. apply _x. rewrite H3. rewrite Hf. reflexivity.
        Qed.

        Lemma sleeper_inc_spec_ref:
          compatsim (crel HDATA LDATA) (gensem sleeper_inc_spec) sleeper_inc_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          assert (Hkern: kernel_mode d2).
          {
            inv match_related. functional inversion H1; subst;
                                 repeat split; try congruence; eauto.
          }
          assert (Hcpuid: CPU_ID d1 = CPU_ID d2).
          {
            inversion match_related. assumption.
          }
          inv H.
          specialize (Mem.valid_access_store _ _ _ _ (Vint (Int.repr (Int.unsigned v + 1))) H2); intros [ HST].
          functional inversion H1; subst.
          {
            refine_split; unfold n in *; unfold cpu in ×.
            - econstructor; eauto.
              clear H7. rewrite H3 in _x. omega.
            - constructor.
            - split; eauto; pattern2_refinement_simpl.
              econstructor; simpl; eauto.
              rewrite H3.
              econstructor; eauto; intros.
              eapply Mem.load_store_same in HST; eauto.
              eapply Mem.store_valid_access_1; eauto.
              rewrite ZMap.gss. rewrite Int.unsigned_repr. reflexivity.
              clear H7. rewrite H3 in _x.
              generalize max_unsigned_val; intro muval. omega.
            - apply inject_incr_refl.
          }
        Qed.

        Lemma sleeper_dec_spec_ref:
          compatsim (crel HDATA LDATA) (gensem sleeper_dec_spec) sleeper_dec_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          assert (Hkern: kernel_mode d2).
          {
            inv match_related. functional inversion H1; subst;
                                 repeat split; try congruence; eauto.
          }
          assert (Hcpuid: CPU_ID d1 = CPU_ID d2).
          {
            inversion match_related. assumption.
          }
          inv H.
          specialize (Mem.valid_access_store _ _ _ _ (Vint (Int.repr (Int.unsigned v - 1))) H2); intros [ HST].
          functional inversion H1; subst.
          {
            refine_split; unfold n in *; unfold cpu in ×.
            - econstructor; eauto.
              clear H7. rewrite H3 in _x. omega.
            - constructor.
            - split; eauto; pattern2_refinement_simpl.
              econstructor; simpl; eauto.
              rewrite H3.
              econstructor; eauto; intros.
              eapply Mem.load_store_same in HST; eauto.
              eapply Mem.store_valid_access_1; eauto.
              rewrite ZMap.gss. rewrite Int.unsigned_repr. reflexivity.
              clear H7. rewrite H3 in _x.
              generalize max_unsigned_val; intro muval. omega.
            - 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.
          - econstructor. functional inversion H5. simpl. assumption.
        Qed.

        Lemma passthrough_correct:
          sim (crel HDATA LDATA) psleeper_passthrough pkcontextnew.
        Proof.
          sim_oplus.
          - apply fload_sim.
          - apply fstore_sim.
          - apply page_copy_sim.
          - apply page_copy_back_sim.
          - apply vmxinfo_get_sim.
          - apply palloc_sim.
          - apply setPT_sim.
          - apply ptRead_sim.
          - apply ptResv_sim.
          - apply sharedmem_init_sim.
          - apply shared_mem_status_sim.
          - apply offer_shared_mem_sim.
          - apply ptin_sim.
          - apply ptout_sim.
          - apply container_get_nchildren_sim.
          - apply container_get_quota_sim.
          - apply container_get_usage_sim.
          - apply container_can_consume_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; trivial. apply in_nil in H0. inversion H0.

          - 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 kctxt_new_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.
          - apply kctxt_switch_sim.
          - layer_sim_simpl.
            + eapply load_correct2.
            + eapply store_correct2.
        Qed.

      End PASSTHROUGH_PRIM.

    End OneStep_Forward_Relation.

  End WITHMEM.

End Refinement.