Library mcertikos.conlib.conmtlib.SingleOracleDef

Require Import Coqlib.
Require Import Maps.
Require Import ASTExtra.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import MemoryX.
Require Import Events.
Require Import Globalenvs.
Require Import Smallstep.
Require Import Asm.
Require Import Conventions.
Require Import Machregs.
Require Import AuxLemma.

Require Import Values.
Require Import Maps.
Require Import Constant.
Require Import Maps.
Require Import ASTExtra.
Require Import Floats.
Require Import Values.
Require Import MemoryX.
Require Import Events.
Require Import Globalenvs.

Require Import liblayers.logic.PTreeModules.
Require Import liblayers.compcertx.MemWithData.
Require Import liblayers.compcertx.Stencil.
Require Import liblayers.compat.CompatData.
Require Import liblayers.compat.CompatPrimSem.
Require Import liblayers.compat.CompatLayers.
Require Import liblayers.compat.CompatExternalCalls.
Require Import Decision.
Require Import LAsm.
Require Import GlobIdent.

Require Import FlatMemory.

Require Import AlgebraicMem.

Inductive lval:=
| Lint (n: int)
| Lptr (b: block) (ofs: int).

Section VAL2LVAL.

  Inductive val2Lval: vallvalProp:=
  | VAL2LVALINT:
       n,
        val2Lval (Vint n) (Lint n)
  | VAL2LVALPTR:
       n ofs,
        val2Lval (Vptr n ofs) (Lptr n ofs).

  Inductive val2Lval_list: (list val) → (list lval) → Prop:=
  | VAL2LVALNIL:
      val2Lval_list nil nil
  | VAL2LVALCON:
       v l ,
        val2Lval v
        val2Lval_list l
        val2Lval_list (v::l) (::).

  Lemma val2Lval_lessdef:
     v gv,
      Val.lessdef v
      val2Lval v gv
      val2Lval gv.
  Proof.
    intros. inv H0; inv H; constructor.
  Qed.

  Lemma val2Lval_list_lessdef:
     v gv,
      Val.lessdef_list v
      val2Lval_list v gv
      val2Lval_list gv.
  Proof.
    induction v; intros.
    - inv H; inv H0. constructor.
    - inv H. inv H0. constructor; eauto.
      eapply val2Lval_lessdef; eauto.
  Qed.

End VAL2LVAL.

Require Import SingleAbstractDataType.
Require Import DeviceStateDataType.
Require Import AuxStateDataType.

Record privDataSnap :=
  mk_privDataSnap {
      tiSnap: trapinfo;
      
      ikernSnap: bool;
      ihostSnap: bool;
      
      
      HPSnap: flatmem;
      
      ACSnap : Container;
      
      uctxtSnap : UContext;
      
      ppermSnap: PPermT;
      
      PTSnap: Z;
      ptpoolSnap: PMapPool;
      iptSnap: bool;
      
      syncchpoolSnap : SyncChanPool;
      bufferSnap: PageBufferPool;
      
      
      eptSnap: EPT;
      vmcsSnap: VMCS;
      vmxSnap: VMX;
      
      com1Snap: SerialData;
      drv_serialSnap: SerialDriver;
      console_concreteSnap: ConsoleDriverConcrete;
      consoleSnap: ConsoleDriver;
      
      
      ioapicSnap: IoApicData;
      lapicSnap: LApicData;
      tfSnap: TrapFrames;
      tf_regSnap: val;
temporary place to save an intermediate tf regiter
      curr_intr_numSnap: Z;
      intr_flagSnap: bool;
      saved_intr_flagsSnap: list bool;
      
      in_intrSnap: bool;
      i_dev_serialSnap: bool
                      
    }.

Inductive LogEventUnit :=
| LogYield (n: positive)
| LogSleep (i: Z) (n: positive) (syncch : option AuxStateDataType.SyncChanPool)
| LogPrim (id: ident) (args: list lval) (choice : Z) (dprocSnap : privDataSnap)



| LogYieldBack.

Inductive LogEventType :=
| LogYieldTy
| LogOtherTy.

Inductive LogEvent :=
| LEvent : ZLogEventUnitLogEvent.


Function getLogEventUnitType (le : LogEventUnit) :=
  match le with
    | LogYield _LogYieldTy
    | LogSleep _ _ _LogYieldTy
    | _LogOtherTy
  end.

Function getLogEventType (le : LogEvent) :=
  match le with
    | LEvent _ tgetLogEventUnitType t
  end.

Function getLogEventUnitNB (le : LogEventUnit) :=
  match le with
    | LogYield nSome n
    | LogSleep _ n _Some n
    | _None
  end.

Function getLogEventNB (le : LogEvent) :=
  match le with
    | LEvent _ tgetLogEventUnitNB t
  end.

Function getThrdID (le : LogEvent) :=
  match le with
    | LEvent cid _cid
  end.

Definition Log := list LogEvent.

Section Auxiliary.

  Set Implicit Arguments.

  Variable A : Type.

  Fixpoint last_op (l : list A) : option A :=
    match l with
      | nilNone
      | hd::_Some hd
    end.

  Definition remove_hd (l : list A) : list A :=
    match l with
    | nilnil
    | ev::
    end.

  Lemma eq_lst_has_same_length:
     (l : list A),
      l = length l = length .
  Proof.
    induction l; intros; inv H; auto.
  Qed.

  Lemma all_lst_has_length:
     (l : list A), n, length l = n.
  Proof.
    induction l; intros; [ 0%nat |]; auto.
    destruct IHl; eauto.
  Qed.

  Lemma cons_lst_succ_length:
     (l: list A) (e: A) n, length l = nlength (e::l) = S n.
  Proof.
    destruct l; intros.
    - simpl; auto.
    - simpl; simpl in H; auto.
  Qed.

  Lemma cons_lst_eq_lst_contradiction:
     (l : list A) e, e::l = lFalse.
  Proof.
    intros; generalize (all_lst_has_length l); intros.
    destruct H0 as (n, H0).
    generalize H0; intros.
    eapply eq_lst_has_same_length in H.
    eapply cons_lst_succ_length with (e := e) in H1.
    rewrite H1, H0 in H; generalize H; clear.
    intros; induction n.
    - inv H.
    - inversion H; clear H.
      apply IHn; auto.
  Qed.

  Unset Implicit Arguments.

End Auxiliary.

Function lastEvType (lg : Log) : option LogEventType :=
  match (last_op lg) with
  | Some evSome (getLogEventType ev)
    | NoneNone
  end.


Definition ThreadIDSet := list Z.