Library mcertikos.mm.MALT


This file defines the abstract data and the primitives for the MAL layer, which will introduce the palloc and pfree primitives
Require Import Coqlib.
Require Import Maps.
Require Import ASTExtra.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import Memory.
Require Import Events.
Require Import Stacklayout.
Require Import Globalenvs.
Require Import AsmX.
Require Import Smallstep.
Require Import AuxStateDataType.
Require Import Constant.
Require Import GlobIdent.
Require Import FlatMemory.
Require Import CommonTactic.
Require Import AuxLemma.
Require Import RealParams.
Require Import PrimSemantics.
Require Import LAsm.
Require Import LoadStoreSem1.

Require Import liblayers.logic.PTreeModules.
Require Import liblayers.logic.LayerLogicImpl.
Require Import liblayers.compat.CompatLayers.
Require Import liblayers.compat.CompatGenSem.

Require Import FutureTactic.

Require Import INVLemmaContainer.
Require Import INVLemmaMemory.
Require Import INVLemmaQLock.
Require Import INVLemmaInterrupt.
Require Import INVLemmaDriver.

Require Import AbstractDataType.
Require Import DeviceStateDataType.

Require Export ObjCPU.
Require Export ObjMM.
Require Export ObjFlatMem.
Require Export ObjContainer.
Require Export ObjMultiprocessor.
Require Export ObjQLock.
Require Export ObjPMM.

Require Export ObjInterruptManagement.
Require Export ObjInterruptController.
Require Export ObjConsole.
Require Export ObjSerialDriver.

Section WITHMEM.

  Local Open Scope Z_scope.

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

Invariants at this layer

  Record high_level_invariant (abd: RData) :=
    mkInvariant {
        CPU_ID_range: 0 (CPU_ID abd) < TOTAL_CPU;
        valid_curid: 0 ZMap.get (CPU_ID abd) (cid abd) < num_proc;

        valid_nps: init abd= truekern_low nps abd maxpage;
        valid_kern: ikern abd = falsepg abd = true init abd = true;
        valid_AT_kern: init abd = trueAT_kern (AT abd) (nps abd);
        valid_AT_usr: init abd = trueAT_usr (AT abd) (nps abd);
        valid_CR3: pg abd= trueCR3_valid (CR3 abd);
        valid_ihost: ihost abd = falsepg abd = true init abd = true ikern abd = true;
        valid_container: Container_valid (AC abd);

        valid_multi_oracle_pool_inv: valid_multi_oracle_pool_H (multi_oracle abd);
        valid_hlock_pool_inv: valid_hlock_pool (multi_log abd);
        valid_AT_oracle_pool_inv: valid_AT_oracle_pool (multi_oracle abd);
        valid_AT_log_pool_inv: init abd = truevalid_AT_log_pool (multi_log abd) (nps abd);

        valid_cons_buf_rpos: 0 rpos (console abd) < CONSOLE_BUFFER_SIZE;
        valid_cons_buf_length: 0 Zlength (cons_buf (console abd)) < CONSOLE_BUFFER_SIZE;

        
        init_pperm: init abd = false(pperm abd) = ZMap.init PGUndef;

        init_AT: init abd = falseAT abd = ZMap.init ATUndef
                                      multi_log abd = ZMap.init MultiUndef

      }.

Definition of the abstract state ops

  Global Instance malt_data_ops : CompatDataOps RData :=
    {
      empty_data := init_adt multi_oracle_init2;
      high_level_invariant := high_level_invariant;
      low_level_invariant := low_level_invariant;
      kernel_mode adt := ikern adt = true ihost adt = true
    }.

Proofs that the initial abstract_data should satisfy the invariants

  Section Property_Abstract_Data.

    Lemma empty_data_high_level_invariant:
      high_level_invariant (init_adt multi_oracle_init2).
    Proof.
      constructor; simpl; intros; auto; try inv H.
      - apply current_CPU_ID_range.
      - rewrite ZMap.gi; intuition.
      -
        apply empty_container_valid.
      - apply valid_ticket_oracle2.
      - apply valid_hlock_pool_init.
      - eapply valid_AT_oracle_pool2.
    Qed.

  End Property_Abstract_Data.

Definition of the abstract state

  Global Instance malt_data_prf : CompatData RData.
  Proof.
    constructor.
    - apply low_level_invariant_incr.
    - apply empty_data_low_level_invariant.
    - apply empty_data_high_level_invariant.
  Qed.

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

Proofs that the primitives satisfies the invariants at this layer

  Section INV.

    Global Instance release_lock_inv: ReleaseInvariants release_lock_spec1.
    Proof.
      constructor; unfold release_lock_spec; intros; subdestruct;
      inv H; inv H0; constructor; auto; simpl; intros.
      - eapply valid_hlock_pool_gss´; eauto.
      - eapply valid_AT_log_pool_gso; eauto.
        eapply Shared2ID1_neq; eauto.
      - exploit init_AT0; eauto.
        intros (_ & Hm). rewrite Hm in Hdestruct3.
        rewrite ZMap.gi in Hdestruct3. inv Hdestruct3.
    Qed.

    Global Instance acquire_lock_inv: AcquireInvariants acquire_lock_spec1.
    Proof.
      constructor; unfold acquire_lock_spec; intros; subdestruct;
      inv H; inv H0; constructor; auto; simpl; intros.
      - eapply valid_hlock_pool_gss´; eauto.
      - eapply valid_AT_log_pool_gso; eauto.
        eapply Shared2ID1_neq; eauto.
      - exploit init_AT0; eauto.
        intros (_ & Hm). rewrite Hm in Hdestruct4.
        rewrite ZMap.gi in Hdestruct4. inv Hdestruct4.
    Qed.


    Global Instance set_curid_inv: PreservesInvariants set_curid_spec.
    Proof.
      preserves_invariants_simpl_auto.
      - rewrite ZMap.gss. auto.
    Qed.

    Global Instance set_curid_init_inv: PreservesInvariants set_curid_init_spec.
    Proof.
      preserves_invariants_simpl_auto; eauto 2.
      case_eq (zeq (CPU_ID d) (Int.unsigned i)); intros; subst.
      - rewrite e; rewrite ZMap.gss; omega.
      - rewrite ZMap.gso; auto.
    Qed.

    Global Instance setCR3_inv: SetCR3Invariants setCR30_spec.
    Proof.
      constructor; intros; functional inversion H.
      - inv H0; constructor; trivial.
      - inv H0; constructor; auto.
      - assumption.
    Qed.

    Global Instance container_split_inv: PreservesInvariants container_split_spec.
    Proof.
      preserves_invariants_simpl_auto.
      rewrite <- H1 in H0.
      exploit split_container_valid; eauto.
    Qed.

    Global Instance container_alloc_inv: PreservesInvariants container_alloc_spec.
    Proof.
      preserves_invariants_simpl_auto.
      exploit alloc_container_valid; eauto.
    Qed.

    Global Instance trapin_inv: PrimInvariants trapin_spec.
    Proof.
      PrimInvariants_simpl_auto.
    Qed.

    Global Instance trapout_inv: PrimInvariants trapout0_spec.
    Proof.
      PrimInvariants_simpl_auto.
    Qed.

    Global Instance hostin_inv: PrimInvariants hostin_spec.
    Proof.
      PrimInvariants_simpl_auto.
    Qed.

    Global Instance hostout_inv: PrimInvariants hostout_spec.
    Proof.
      PrimInvariants_simpl_auto.
    Qed.

    Global Instance cli_inv: PreservesInvariants cli_spec.
    Proof.
      preserves_invariants_direct low_level_invariant high_level_invariant; eauto 2.
    Qed.

    Global Instance sti_inv: PreservesInvariants sti_spec.
    Proof.
      preserves_invariants_direct low_level_invariant high_level_invariant; eauto 2.
    Qed.

    Global Instance cons_buf_read_inv:
      PreservesInvariants cons_buf_read_spec.
    Proof.
      preserves_invariants_nested low_level_invariant high_level_invariant; eauto 2.
    Qed.

    Global Instance serial_putc_inv:
      PreservesInvariants serial_putc_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

    Global Instance serial_intr_disable_inv: PreservesInvariants serial_intr_disable_spec.
    Proof.
      constructor; simpl; intros; inv_generic_sem H.
      - inversion H0; econstructor; eauto 2 with serial_intr_disable_invariantdb.
        generalize (serial_intr_disable_preserves_tf _ _ H2); intro tmprw; rewrite <- tmprw; assumption.
      - inversion H0; econstructor; eauto 2 with serial_intr_disable_invariantdb.
        eapply serial_intr_disable_preserves_all in H2.
        blast H2. eauto.
      - eauto 2 with serial_intr_disable_invariantdb.
   Qed.

    Global Instance serial_intr_enable_inv:
      PreservesInvariants serial_intr_enable_spec.
    Proof.
      constructor; simpl; intros; inv_generic_sem H.
      - inversion H0; econstructor; eauto 2 with serial_intr_enable_invariantdb.
        generalize (serial_intr_enable_preserves_tf _ _ H2); intro tmprw; rewrite <- tmprw; assumption.
      - inversion H0; econstructor; eauto 2 with serial_intr_enable_invariantdb.
        eapply serial_intr_enable_preserves_all in H2.
        blast H2. eauto.
      - eauto 2 with serial_intr_enable_invariantdb.
    Qed.

    Global Instance mem_init_inv: PreservesInvariants mem_init_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant.
      - apply real_nps_range.
      - apply real_at_kern_valid.
      - apply real_at_usr_valid.
      - apply AC_init_container_valid.
      - assumption.
      - eapply real_valid_hlock_pool; eauto.
      - assumption.
      - eapply real_valid_AT_log_pool; eauto.
    Qed.

    Global Instance setPG_inv: PreservesInvariants setPG0_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

    Global Instance set_at_c_inv: PreservesInvariants set_at_c0_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

    Section PALLOC.

      Lemma acquire_lock_AT_high_level_inv:
         d ,
          acquire_lock_AT_spec d = Some
          high_level_invariant d
          high_level_invariant .
      Proof.
        intros. unfold acquire_lock_AT_spec in *; subdestruct; inv H.
        - inv H0. constructor; simpl; eauto; intros.
          + eapply valid_AT_log_imply; eauto.
          + eapply valid_AT_log_imply; eauto.
          + eapply valid_hlock_pool_gss´; eauto.
          + eapply valid_AT_log_pool_gss´; eauto.
            eapply valid_AT_log_pull; eauto.
            eapply valid_AT_log_TICKET_imply; eauto.
          + exploit init_AT0; eauto.
            intros (_ & Hm). rewrite Hm in Hdestruct2.
            rewrite ZMap.gi in Hdestruct2. inv Hdestruct2.
        - inv H0. constructor; simpl; eauto; intros.
          + eapply valid_hlock_pool_gss´; eauto.
          + eapply valid_AT_log_pool_gss´; eauto.
            eapply valid_AT_log_pull; eauto.
            eapply valid_AT_log_TICKET_imply; eauto.
          + exploit init_AT0; eauto.
            intros (_ & Hm). rewrite Hm in Hdestruct2.
            rewrite ZMap.gi in Hdestruct2. inv Hdestruct2.

      Qed.

      Lemma acquire_lock_AT_low_level_inv:
         d n,
          acquire_lock_AT_spec d = Some
          low_level_invariant n d
          low_level_invariant n .
      Proof.
        intros. unfold acquire_lock_AT_spec in *; subdestruct; inv H.
        - inv H0. constructor; simpl; eauto.
        - inv H0. constructor; simpl; eauto.
      Qed.

      Lemma acquire_lock_AT_kernel_mode:
         d ,
          acquire_lock_AT_spec d = Some
          kernel_mode d
          kernel_mode .
      Proof.
        intros. unfold acquire_lock_AT_spec in *; subdestruct; inv H.
        - inv H0; eauto.
        - inv H0; eauto.
      Qed.

      Lemma release_lock_AT_high_level_inv:
         d ,
          release_lock_AT_spec d = Some
          high_level_invariant d
          high_level_invariant .
      Proof.
        intros. unfold release_lock_AT_spec in *; subdestruct; inv H.
        - inv H0. constructor; simpl; eauto; intros.
          + eapply valid_hlock_pool_gss´; eauto.
          + eapply valid_AT_log_pool_TICKET; eauto.
          + exploit init_AT0; eauto.
            intros (_ & Hm). rewrite Hm in Hdestruct1.
            rewrite ZMap.gi in Hdestruct1. inv Hdestruct1.
      Qed.

      Lemma release_lock_AT_low_level_inv:
         d n,
          release_lock_AT_spec d = Some
          low_level_invariant n d
          low_level_invariant n .
      Proof.
        intros. unfold release_lock_AT_spec in *; subdestruct; inv H.
        - inv H0. constructor; simpl; eauto.
      Qed.

      Lemma release_lock_AT_kernel_mode:
         d ,
          release_lock_AT_spec d = Some
          kernel_mode d
          kernel_mode .
      Proof.
        intros. unfold release_lock_AT_spec in *; subdestruct; inv H.
        - inv H0; eauto.
      Qed.

      Lemma palloc_aux_high_level_inv:
         d i n,
          ObjPMM.palloc_aux_spec i d = Some (, n)
          high_level_invariant d
          high_level_invariant .
      Proof.
        unfold ObjPMM.palloc_aux_spec; intros.
        subdestruct; inv H; subst; eauto.
        inv H0. constructor; simpl; eauto.
        + intros; eapply AT_kern_norm´; eauto.
        + intros; eapply AT_usr_norm; eauto.
        + rewrite <- Hdestruct2.
          eapply alloc_container_valid´; eauto.
        + subst; simpl;
          intros; congruence.
        + intros; congruence.
      Qed.

      Lemma palloc´_high_level_inv:
         d i n,
          palloc´_spec i d = Some (, n)
          high_level_invariant d
          high_level_invariant .
      Proof.
        intros. functional inversion H; subst; eauto.
        eapply release_lock_AT_high_level_inv; eauto.
        eapply palloc_aux_high_level_inv; eauto.
        eapply acquire_lock_AT_high_level_inv; eauto.
      Qed.

      Lemma palloc_aux_low_level_inv:
         d i n ,
          ObjPMM.palloc_aux_spec i d = Some (, n)
          low_level_invariant d
          low_level_invariant .
      Proof.
        unfold ObjPMM.palloc_aux_spec; intros.
        subdestruct; inv H; subst; eauto.
        inv H0. constructor; eauto.
      Qed.

      Lemma palloc´_low_level_inv:
         d i n ,
          palloc´_spec i d = Some (, n)
          low_level_invariant d
          low_level_invariant .
      Proof.
        intros. functional inversion H; subst; eauto.
        eapply release_lock_AT_low_level_inv; eauto.
        eapply palloc_aux_low_level_inv; eauto.
        eapply acquire_lock_AT_low_level_inv; eauto.
      Qed.

      Lemma palloc´_kernel_mode:
         d i n,
          palloc´_spec i d = Some (, n)
          kernel_mode d
          kernel_mode .
      Proof.
        intros. functional inversion H; subst; eauto.
        unfold release_lock_AT_spec in ×.
        subdestruct; inv H5; simpl; eauto.
      Qed.

      Global Instance palloc´_inv: PreservesInvariants palloc´_spec.
      Proof.
        preserves_invariants_simpl´.
        - eapply palloc´_low_level_inv; eassumption.
        - eapply palloc´_high_level_inv; eassumption.
        - eapply palloc´_kernel_mode; eassumption.
      Qed.

    End PALLOC.

    Global Instance pfree_inv: PreservesInvariants pfree´_spec.
    Proof.
      preserves_invariants_simpl_auto.
      - intros; eapply AT_kern_norm; eauto.
      - intros; eapply AT_usr_norm; eauto.
    Qed.

    Global Instance proc_create_postinit_inv:
      PreservesInvariants proc_create_postinit_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto 2.
    Qed.

  End INV.

  Definition exec_loadex {F V} := exec_loadex1 (F := F) (V := V).

  Definition exec_storeex {F V} := exec_storeex1 (flatmem_store:= flatmem_store) (F := F) (V := V).


  Global Instance page_copy_inv: PreservesInvariants page_copy0_spec.
  Proof.
    preserves_invariants_simpl_auto.
    - eapply valid_hlock_pool_gss´; eauto.
    - eapply valid_AT_log_pool_gso; eauto.
      eapply Shared2ID1_neq; eauto.
      unfold Shared2ID1.
      reflexivity.
    - exploit init_AT0; eauto.
      intros (_ & Hm). rewrite Hm in H8.
      rewrite ZMap.gi in H8.
      inv H8.
  Qed.

  Global Instance page_copy_back_inv: PreservesInvariants page_copy_back0_spec.
  Proof.
    preserves_invariants_simpl_auto.
  Qed.

  Global Instance flatmem_store_inv: FlatmemStoreInvariant (flatmem_store:= flatmem_store).
  Proof.
    split; inversion 1; intros.
    - functional inversion H0; constructor; auto.
    - functional inversion H1; constructor; auto.
  Qed.

  Global Instance fstore_inv: PreservesInvariants fstore0_spec.
  Proof.
    preserves_invariants_simpl low_level_invariant high_level_invariant; trivial;
    functional inversion H; subst; auto.
  Qed.

  Global Instance trapinfo_set_inv: TrapinfoSetInvariant.
  Proof.
    split; inversion 1; intros; constructor; auto.
  Qed.

Layer Definition

Layer Definition newly introduced

  Definition malt_fresh: compatlayer (cdata RData) :=
    palloc gensem palloc´_spec
            pfree gensem pfree´_spec.

Layer Definition passthrough

  Definition malt_passthrough : compatlayer (cdata RData) :=
    fload gensem fload´_spec
           fstore gensem fstore0_spec
          
           page_copy gensem page_copy0_spec
           page_copy_back gensem page_copy_back0_spec

           vmxinfo_get gensem vmxinfo_get_spec
           set_pg gensem setPG0_spec
           set_cr3 setCR3_compatsem setCR30_spec
           at_get_c gensem get_at_c_spec
           at_set_c gensem set_at_c0_spec
           mem_init gensem mem_init_spec
           container_get_parent gensem container_get_parent_spec
           container_get_nchildren gensem container_get_nchildren_spec
           container_get_quota gensem container_get_quota_spec
           container_get_usage gensem container_get_usage_spec
           container_can_consume gensem container_can_consume_spec
           container_split gensem container_split_spec
           get_CPU_ID gensem get_CPU_ID_spec
           get_curid gensem get_curid_spec
           set_curid gensem set_curid_spec
           set_curid_init gensem set_curid_init_spec
           release_lock primcall_release_lock_compatsem release_lock release_lock_spec1
           acquire_lock primcall_acquire_lock_compatsem acquire_lock_spec1
          

           cli gensem cli_spec
           sti gensem sti_spec
           serial_intr_disable gensem serial_intr_disable_spec
           serial_intr_enable gensem serial_intr_enable_spec
           serial_putc gensem serial_putc_spec
           cons_buf_read gensem cons_buf_read_spec

           trap_in primcall_general_compatsem trapin_spec
           trap_out primcall_general_compatsem trapout0_spec
           host_in primcall_general_compatsem hostin_spec
           host_out primcall_general_compatsem hostout_spec
           proc_create_postinit gensem proc_create_postinit_spec
           trap_get primcall_trap_info_get_compatsem trap_info_get_spec
           trap_set primcall_trap_info_ret_compatsem trap_info_ret_spec
           accessors {| exec_load := @exec_loadex; exec_store := @exec_storeex |}.

Layer Definition

  Definition malt : compatlayer (cdata RData) := malt_fresh malt_passthrough.

End WITHMEM.