Library mcertikos.mm.MShareOp


This file defines the abstract data and the primitives for the PIPC layer, which will introduce the primtives of thread
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 LoadStoreSem2.
Require Import XOmega.

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

Require Import CalRealPTPool.
Require Import CalRealPT.
Require Import CalRealIDPDE.
Require Import CalRealInitPTE.
Require Import CalRealSMSPool.

Require Import INVLemmaContainer.
Require Import INVLemmaMemory.

Require Export MShareIntro.
Require Import AbstractDataType.

Require Import INVLemmaQLock.
Require Import INVLemmaInterrupt.
Require Import INVLemmaDriver.
Require Import DeviceStateDataType.
Require Import FutureTactic.

Abstract Data and Primitives at this layer

Section WITHMEM.

  Local Open Scope Z_scope.

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

  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 shared_mem_to_pending_inv:
      PreservesInvariants shared_mem_to_pending_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

    Global Instance shared_mem_to_dead_inv:
      PreservesInvariants shared_mem_to_dead_spec.
    Proof.
      preserves_invariants_simpl_auto.
    Qed.

    Global Instance shared_mem_to_ready_inv:
      PreservesInvariants shared_mem_to_ready_spec.
    Proof.
      preserves_invariants_simpl´; functional inversion H2; subst.
      - eapply ptResv2_low_level_inv; eauto.
      - exploit ptResv2_low_level_inv; eauto.
        intros HP; inv HP. constructor; trivial.
      - eapply ptResv2_high_level_inv; eauto.
      - exploit ptResv2_high_level_inv; eauto.
        intros HP; inv HP. constructor; trivial.
      - eapply ptResv2_kernel_mode; eauto.
      - exploit ptResv2_kernel_mode; eauto.
    Qed.

    Global Instance sharedmem_init_inv: PreservesInvariants sharedmem_init_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant.
      - apply real_nps_range.
      - apply AC_init_container_valid.
      - rewrite init_pperm; [|try assumption].
        apply real_pperm_log_valid.
      - eapply real_pt_PMap_valid; eauto.
      - apply real_pt_PMap_kern.
      - omega.
      - assumption.
      - apply real_idpde_init.
      - apply real_pt_weak_consistent_pmap.
      - apply real_pt_consistent_pmap_domain.
      - apply Lreal_at_consistent_lat_domain.
      - eapply LATCTable_log_real; eauto.
      - assumption.
      - assumption.
      - eapply real_valid_hlock_pool1; eauto.
      - assumption.
      - eapply real_valid_AT_log_pool_H; eauto.
    Qed.

  End INV.

Layer Definition

  Definition mshareop_fresh : compatlayer (cdata RData) :=
    shared_mem_to_pending gensem shared_mem_to_pending_spec
                           shared_mem_to_dead gensem shared_mem_to_dead_spec
                           shared_mem_to_ready gensem shared_mem_to_ready_spec
                           get_shared_mem_status_seen gensem get_shared_mem_status_seen_spec
                           shared_mem_init gensem sharedmem_init_spec.

  Definition mshareop_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
           palloc gensem palloc_spec
          
           set_pt gensem setPT_spec
           pt_read gensem ptRead_spec
           pt_resv gensem ptResv_spec
           pt_resv2 gensem ptResv2_spec
           pt_new gensem pt_new_spec
          
           get_shared_mem_state gensem get_shared_mem_state_spec
           get_shared_mem_seen gensem get_shared_mem_seen_spec
           set_shared_mem_seen gensem set_shared_mem_seen_spec

           pt_in primcall_general_compatsem´ ptin_spec (prim_ident:= pt_in)
           pt_out primcall_general_compatsem´ ptout_spec (prim_ident:= pt_out)
           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

           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 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 _ _ _ _ _ _ _ _ _ _ _ _ _ _ Hmwd);
                           exec_store := (@exec_storeex _ _ _ _ _ _ _ _ _ _ _ _ _ _ Hmwd) |}.

  Definition mshareop : compatlayer (cdata RData) := mshareop_fresh mshareop_passthrough.

End WITHMEM.

Section WITHPARAM.

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

  Local Open Scope Z_scope.

Implementation of primtives in VMCBOps
  Section Impl.

    Function ll_shared_mem_status_spec (pid1 pid2: Z) (adt: RData) : option (RData × Z) :=
      match (ikern adt, ihost adt, pg adt, ipt adt, shared_mem_arg pid1 pid2) with
        | (true, true, true, true, true)
          match ZMap.get pid2 (ZMap.get pid1 (smspool adt)) with
            | SHRDValid true _
              match get_shared_mem_status_seen_spec pid1 pid2 adt with
               | Some jSome (adt, j)
               | _None
              end
            | SHRDValid false vadr
              Some (adt {smspool: ZMap.set pid1 (ZMap.set pid2 (SHRDValid true vadr)
                                                          (ZMap.get pid1 (smspool adt)))
                                           (smspool adt)}, SharedMemInfo2Z )
            | _None
          end
        | _None
      end.

    Function ll_offer_shared_mem_spec (pid1 pid2 vadr: Z) (adt: RData) : option (RData × Z) :=
      match (ikern adt, ihost adt, pg adt, ipt adt, shared_mem_arg pid1 pid2) with
        | (true, true, true, true, true)
          match ZMap.get pid2 (ZMap.get pid1 (smspool adt)) with
            | SHRDValid st _ _
              if SharedMemInfo_dec st SHRDPEND then Some (adt, SHARED_MEM_PEND)
              else
                match ZMap.get pid1 (ZMap.get pid2 (smspool adt)) with
                  | SHRDValid st´ _ vadr´
                    if zle_lt 0 vadr´ adr_max then
                      if SharedMemInfo_dec st´ SHRDPEND then
                        match shared_mem_to_ready_spec pid1 pid2 vadr adt with
                          | Some (adt´, re)
                            if zeq re MagicNumber
                            then match shared_mem_to_dead_spec pid1 pid2 vadr adt´ with
                                   | Some adt´´Some (adt´´, SHARED_MEM_DEAD)
                                   | _None
                                 end
                            else Some (adt´, SHARED_MEM_READY)
                          | _None
                        end
                      else match shared_mem_to_pending_spec pid1 pid2 vadr adt with
                             | Some adt´´Some (adt´´, SHARED_MEM_PEND)
                             | _None
                           end
                    else None
                  | _None
                end
            | _None
          end
        | _None
      end.

  End Impl.

End WITHPARAM.