Library mcertikos.mm.MContainer


This file defines the raw abstract data and the primitives for the MBoot layer, which is also the bottom layer of memory management
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 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 ObjInterruptManagement.
Require Export ObjInterruptController.
Require Export ObjConsole.
Require Export ObjSerialDriver.

Section WITHMEM.

  Local Open Scope Z_scope.

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

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_multi_oracle_pool_inv: valid_multi_oracle_pool_H (multi_oracle abd);
        valid_hlock_pool_inv: valid_hlock_pool (multi_log abd);
        valid_container: Container_valid (AC abd);
        valid_preinit_container: init abd = false
                                  x, 0 x < num_proccused (ZMap.get x (AC abd)) true;
        

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

Definition of the abstract state ops

  Global Instance mcontainer_data_ops : CompatDataOps RData :=
    {
      empty_data := init_adt multi_oracle_init1;
      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_init1).
    Proof.
      constructor; trivial; simpl; try congruence; intros.
      - apply current_CPU_ID_range.
      - rewrite ZMap.gi; intuition.
      - apply valid_ticket_oracle1.
      - apply valid_hlock_pool_init.
      - eapply empty_container_valid.
      - intros; simpl. rewrite ZMap.gi. discriminate.
    Qed.

Definition of the abstract state

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

  End Property_Abstract_Data.

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

Proofs that the primitives satisfies the invariants at this layer

  Section INV.

MContainer primitives
    Global Instance container_init_inv: PreservesInvariants container_init_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant.
      - assumption.
      - eapply real_valid_hlock_pool; eauto.
      - apply AC_init_container_valid.
    Qed.

    Global Instance container_split_inv: PreservesInvariants container_split_spec.
    Proof.
      preserves_invariants_simpl_auto.
      - rewrite <- H1 in H0.
        exploit split_container_valid; eauto.
      - subst c. contradiction (valid_preinit_container0 H (Int.unsigned i)).
        inv valid_container0; auto.
    Qed.

    Fact remove_in {A} : (l : list A) eq a1 a2, a1 a2In a2 lIn a2 (remove eq a1 l).
    Proof.
      induction l; simpl; intros; auto.
      destruct H0; subst.
      destruct (eq a1 a2); simpl; auto; contradiction.
      destruct (eq a1 a); simpl; auto.
    Qed.

    Fact remove_in_rev {A} : (l : list A) eq a1 a2, In a2 (remove eq a1 l) → a1 a2 In a2 l.
    Proof.
      induction l; simpl; intros; auto.
      destruct (eq a1 a); subst.
      apply IHl in H; destruct H; auto.
      simpl in H; destruct H; subst; auto.
      apply IHl in H; destruct H; auto.
    Qed.

    Fact remove_nodup {A} : (l : list A) eq a, NoDup lNoDup (remove eq a l).
    Proof.
      induction l; simpl; intros.
      constructor.
      inv H; destruct (eq a0 a); subst; auto.
      constructor; auto.
      intro ; contradiction H2.
      apply remove_in_rev in ; destruct ; auto.
    Qed.

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

passthrough primitives
    Global Instance setPG_inv: PreservesInvariants setPG_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

    Global Instance fstore_inv: PreservesInvariants fstore´_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

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

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

    Global Instance trapout_inv: PrimInvariants trapout´_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 set_curid_inv: PreservesInvariants set_curid_spec.
    Proof.
      preserves_invariants_simpl_auto; eauto 2.
      - 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 acquire_lock_inv: AcquireInvariants acquire_lock_spec0.
    Proof.
      constructor; unfold acquire_lock_spec; intros; subdestruct;
      inv H; inv H0; constructor; auto; simpl; intros.
      - eapply valid_hlock_pool_gss´; eauto.
    Qed.

    Global Instance release_lock_inv: ReleaseInvariants release_lock_spec0.
    Proof.
      constructor; unfold release_lock_spec; intros; subdestruct;
      inv H; inv H0; constructor; auto; simpl; intros.
      - eapply valid_hlock_pool_gss´; eauto.
    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.
    Qed.

    Global Instance serial_putc_inv:
      PreservesInvariants serial_putc_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto 2.
    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.
      - 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.
      - eauto 2 with serial_intr_enable_invariantdb.
    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 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 trapinfo_set_inv: TrapinfoSetInvariant.
  Proof.
    split; inversion 1; intros; constructor; auto.
  Qed.


  Global Instance page_copy_inv: PreservesInvariants page_copy´_spec.
  Proof.
    preserves_invariants_simpl_auto.
    - eapply valid_hlock_pool_gss´; eauto.
  Qed.

  Global Instance page_copy_back_inv: PreservesInvariants page_copy_back´_spec.
  Proof.
    preserves_invariants_simpl_auto.
  Qed.

Layer Definition


  Definition mcontainer_fresh : compatlayer (cdata RData) :=
    container_init gensem container_init_spec
            container_split gensem container_split_spec.

Layer Definition passthrough

  Definition mcontainer_passthrough: compatlayer (cdata RData) :=
        fload gensem fload´_spec
           fstore gensem fstore´_spec
          
           page_copy gensem page_copy´_spec
           page_copy_back gensem page_copy_back´_spec

           vmxinfo_get gensem vmxinfo_get_spec
           set_pg gensem setPG_spec
           set_cr3 setCR3_compatsem setCR3_spec
           get_size gensem MMSize
           is_usable gensem is_mm_usable_spec
           get_mms gensem get_mm_s_spec
           get_mml gensem get_mm_l_spec
           container_can_consume gensem container_can_consume_spec
           container_alloc gensem container_alloc_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
           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_spec0
           acquire_lock primcall_acquire_lock_compatsem acquire_lock_spec0
          

           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 trapout´_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 |}.

  Definition mcontainer : compatlayer (cdata RData) := mcontainer_fresh mcontainer_passthrough.

End WITHMEM.