Library mcertikos.devdrivers.DHandlerIntro


This file defines the abstract data and the primitives for the ConsoleBufOp layer, which will introduce the primtives of console buffer
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 DevicePrimSemantics.
Require Import LAsm.
Require Import LoadStoreSem1.
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 INVLemmaDriver.

Require Import AbstractDataType.
Require Import FutureTactic.

Require Export DConsole.
Require Export ObjConsole.
Require Import DeviceStateDataType.
Require Export ObjInterruptManagement.
Require Import INVLemmaInterrupt.
Require Export ObjSerialDriver.
Require Import INVLemmaDriver.
Require Export ObjInterruptController.
Require Export ObjX86.
Require Export ObjQLock.

Abstract Data and Primitives at this layer

Section WITHMEM.

  Local Open Scope Z_scope.

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

Invariants at this layer

  Record high_level_invariant (abd: RData) :=
    mkInvariant {
      valid_cons_buf_rpos: 0 rpos (console abd) < CONSOLE_BUFFER_SIZE;
      valid_cons_buf_length: 0 Zlength (cons_buf (console abd)) < CONSOLE_BUFFER_SIZE;
      valid_ioapic_max_intr: 23 IoApicMaxIntr (s (ioapic abd)) < 32;
      valid_ioapic_enables: 23 Zlength (IoApicEnables (s (ioapic abd))) < 32;
      valid_ioapic_state:
        init abd = true
         n v, nth_error (IoApicIrqs (s (ioapic abd))) n = value v
                    ( v = Z.of_nat n + IRQ0
                       b, nth_error (IoApicEnables (s (ioapic abd))) n = value b);
      valid_curr_intr_num: 0 curr_intr_num abd < Z.of_nat MAX_IRQ_NUM;
      valid_tf_reg: tf_reg abd = Vint Int.zero
      }.

Definition of the abstract state ops

  Global Instance dhandlerintro_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; simpl; intros; auto; try inv H.
      vm_compute.
      split.
      intro contra; inv contra.
      reflexivity.
      omega.
    Qed.

Definition of the abstract state

    Global Instance dhandlerintro_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.

Dhandlerintro primitives
    Global Instance get_curr_intr_num_inv: PreservesInvariants get_curr_intr_num_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; auto.
    Qed.

    Global Instance intr_default_handler_inv: PreservesInvariants intr_default_handler_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; auto.
    Qed.

passthrough primitives

    Global Instance ticket_lock_init0_inv: PreservesInvariants ticket_lock_init0_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto.
      {
        assert (l1: IoApicMaxIntr (s (ioapic d)) < Zlength (IoApicIrqs (s (ioapic d)))) by omega.
        assert (l2: IoApicMaxIntr (s (ioapic d)) < Zlength (IoApicEnables (s (ioapic d)))) by omega.
        generalize l1 l2.
        replace (IoApicMaxIntr (s (ioapic d))) with (Z.of_nat (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1))) by (rewrite Z2Nat.id; omega).
        revert valid_ioapic_max_intr0.
        repeat match goal with [H: _ |- _] ⇒ clear H end.
        induction (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1)).
        simpl.
        intros.
        omega.
        Opaque Z.of_nat.
        intros.
        replace (Z.of_nat (S n) + 1 - 1) with (Z.of_nat (S n)) by omega.
        rewrite Nat2Z.id.
        simpl.
        replace (Z.of_nat n + 1 - 1) with (Z.of_nat n) in IHn by omega.
        rewrite Nat2Z.id in IHn.
        eapply IHn; eauto; xomega.
      }
      {
        assert (l1: IoApicMaxIntr (s (ioapic d)) < Zlength (IoApicIrqs (s (ioapic d)))) by omega.
        assert (l2: IoApicMaxIntr (s (ioapic d)) < Zlength (IoApicEnables (s (ioapic d)))) by omega.
        generalize l1 l2.
        generalize valid_ioapic_enables0.
        replace (IoApicMaxIntr (s (ioapic d))) with (Z.of_nat (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1))) by (rewrite Z2Nat.id; omega).
        repeat match goal with [H: _ |- _] ⇒ clear H end.
        induction (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1)).
        Transparent Z.of_nat.
        simpl.
        intros.
        rewrite replace_preserves_Zlength.
        assumption.
        Opaque Z.of_nat.
        intros.
        replace (Z.of_nat (S n) + 1 - 1) with (Z.of_nat (S n)) by omega.
        rewrite Nat2Z.id.
        simpl.
        replace (Z.of_nat n + 1 - 1) with (Z.of_nat n) in IHn by omega.
        rewrite Nat2Z.id in IHn.
        rewrite replace_preserves_Zlength.
        eapply IHn; eauto; xomega.
      }
      {
        assert (l1: IoApicMaxIntr (s (ioapic d)) < Zlength (IoApicIrqs (s (ioapic d)))) by omega.
        assert (l2: IoApicMaxIntr (s (ioapic d)) < Zlength (IoApicEnables (s (ioapic d)))) by omega.
        generalize l1 l2.
        assert(0 Z.of_nat n Z.of_nat (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1))).
        {
          split.
          xomega.
          eapply nth_error_range in H8.
          rewrite Z2Nat.id by xomega.
          assert (Z.of_nat (length
                     (IoApicIrqs
                        (ioapic_init_aux (s (ioapic d))
                                         (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1))))) = IoApicMaxIntr (s (ioapic d)) + 1).
          {
            rewrite <- ioapic_init_aux_preserves_ioapicirq_length.
            rewrite _x.
            rewrite length_to_zlength.
            reflexivity.
          }
          rewrite H9 in H8.
          omega.
        }
        generalize H8, H9.
        replace (IoApicMaxIntr (s (ioapic d))) with (Z.of_nat (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1))) by (rewrite Z2Nat.id; omega).
        generalize _x _x0.
        repeat match goal with [H: _ |- _] ⇒ clear H end.
        induction (Z.to_nat (IoApicMaxIntr (s (ioapic d)) + 1 - 1)).
        {
          simpl.
          intros.
          assert(Z.of_nat n = 0) by xomega.
          assert(n = O).
          {
            rewrite <- Nat2Z.id at 1.
            rewrite H.
            reflexivity.
          }
          rewrite H0 in H8.
          simpl in ×.
          subdestruct.
          inv H8.
          replace (Z.to_nat (Z.of_nat 0 + 1 - 1)) with O in × by xomega.
          simpl in ×.
          destruct (IoApicIrqs (s (ioapic d))).
          simpl in ×.
          inv Hdestruct.
          simpl in ×.
          inv Hdestruct.
          split.
          reflexivity.
          simpl.
          destruct (IoApicEnables (s (ioapic d))); simpl in ×.
          rewrite _x0 in _x.
          change (Zlength nil) with 0 in _x.
          generalize (Zlength_ge_1 z l); intro.
          omega.
          esplit; reflexivity.
        }
        {
          Opaque Z.of_nat Z.add.
          simpl.
          intros.
          simpl.
          assert(ncase: n = S n0 n S n0) by omega.
          case_eq ncase; intros.
          {
            subst.
            replace (Z.of_nat (S n0) + 1 - 1) with (Z.of_nat (S n0)) in × by omega.
            rewrite Z2Nat.id in × by omega.
            rewrite Nat2Z.id in × by omega.
            Opaque nth_error.
            simpl in ×.
            rewrite replace_gss.
            rewrite replace_gss in H8.
            split.
            inv H8.
            omega.
            esplit; reflexivity.
            rewrite <- ioapic_init_aux_preserves_ioapicirq_length.
            rewrite length_to_zlength.
            omega.
            rewrite <- ioapic_init_aux_preserves_ioapicenables_length.
            rewrite length_to_zlength.
            omega.
          }
          {
            Transparent nth_error.
            replace (Z.of_nat (S n0) + 1 - 1) with (Z.of_nat (S n0)) in × by omega.
            replace (Z.of_nat n0 + 1 - 1) with (Z.of_nat n0) in × by omega.
            rewrite Z2Nat.id in × by omega.
            rewrite Nat2Z.id in × by omega.
            simpl.
            assert(nth_error
                     (replace false (S n0)
                              (IoApicEnables (ioapic_init_aux (s (ioapic d)) n0))) n = nth_error (IoApicEnables (ioapic_init_aux (s (ioapic d)) n0)) n).
            {
              eapply ntheq; eauto.
            }
            rewrite H0.
            eapply IHn0; eauto.
            unfold disable_irq in H8.
            Opaque Z.add.
            simpl in H8.
            erewrite ntheq in H8; eauto.
            xomega.
            xomega.
            xomega.
          }
        }
      }
    Qed.


    Global Instance page_copy_inv: PreservesInvariants page_copy´_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto.
    Qed.

    Global Instance page_copy_back_inv: PreservesInvariants page_copy_back´_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto.
    Qed.

    Global Instance fstore_inv: PreservesInvariants fstore´_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; auto.
    Qed.

    Global Instance setPG_inv: PreservesInvariants setPG_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; 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 H H0.
    Qed.

    Global Instance trapout_inv: PrimInvariants trapout´_spec.
    Proof.
      PrimInvariants_simpl H H0.
    Qed.

    Global Instance hostin_inv: PrimInvariants hostin_spec.
    Proof.
      PrimInvariants_simpl H H0.
    Qed.

    Global Instance hostout_inv: PrimInvariants hostout´_spec.
    Proof.
      PrimInvariants_simpl H H0.
    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 low_level_invariant high_level_invariant; eauto 2.
    Qed.

    Global Instance ioapic_mask_inv:
      PreservesInvariants ioapic_mask_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto 2.
      rewrite replace_preserves_Zlength.
      assumption.
      eapply valid_ioapic_state0 in H5; eauto.
      destruct H5.
      destruct H6.
      assert (ncase: (Z.to_nat (Int.unsigned i)) = n (Z.to_nat (Int.unsigned i)) n) by omega.
      case_eq ncase; intros.
      rewrite e.
      split.
      assumption.
      rewrite replace_gss.
      esplit; reflexivity.
      apply (nth_error_range _ _ _ _ H6).
      split.
      assumption.
      rewrite nth_error_replace_gso by assumption.
      esplit; eassumption.
    Qed.

    Global Instance ioapic_unmask_inv:
      PreservesInvariants ioapic_unmask_spec.
    Proof.
      preserves_invariants_simpl low_level_invariant high_level_invariant; eauto 2.
      rewrite replace_preserves_Zlength.
      assumption.
      eapply valid_ioapic_state0 in H5; eauto.
      destruct H5.
      destruct H6.
      assert (ncase: (Z.to_nat (Int.unsigned i)) = n (Z.to_nat (Int.unsigned i)) n) by omega.
      case_eq ncase; intros.
      rewrite e.
      split.
      assumption.
      rewrite replace_gss.
      esplit; reflexivity.
      apply (nth_error_range _ _ _ _ H6).
      split.
      assumption.
      rewrite nth_error_replace_gso by assumption.
      esplit; eassumption.
    Qed.

    Global Instance serial_hw_intr_inv: PreservesInvariants serial_hw_intr_spec.
    Proof.
      preserves_invariants_nested low_level_invariant high_level_invariant; eauto 2;
      unfold ioapic_trans_intr in *;
      subdestruct; eauto.
      change (Z.of_nat 257%nat) with 257.
      omega.
    Qed.

    Global Instance ic_intr_inv: PreservesInvariants ic_intr_spec.
    Proof.
      preserves_invariants_nested low_level_invariant high_level_invariant; eauto 2;
      unfold ioapic_trans_intr in *;
      subdestruct; eauto.
      simpl in ×.
      eapply nth_error_valid_length in Hdestruct1; eauto.
      generalize valid_ioapic_enables0, Hdestruct1.
      repeat match goal with | [H: _ |- _] ⇒ clear H end.
      intros.
      assert (Z.of_nat (length IoApicEnables) > Int.unsigned i) by xomega.
      rewrite length_to_zlength in H.
      generalize (Int.unsigned_range i); intro.
      change (Z.of_nat 257%nat) with 257.
      omega.
    Qed.

    Global Instance save_context_inv: SaveContextInvariants save_context_spec.
    Proof.
      constructor; intros.
      -
        inv H0. unfold save_context_spec; simpl. inv H1.
        constructor; simpl; trivial.
        induction (tf d).
        simpl.
        constructor.
        constructor.
        simpl.
        apply inv_reg_wt.
        simpl.
        inv inv_inject_neutral.
        eapply inv_reg_inject_neutral; auto.
        constructor.
        constructor.
        inv tf_INJECT.
        assumption.
        eapply IHt; auto.
        inv tf_INJECT.
        assumption.
      -
        inv H0. unfold save_context_spec; simpl.
        constructor; simpl; trivial.
    Qed.

    Global Instance restore_context_inv: RestoreContextInvariants restore_context_spec.
    Proof.
      constructor; intros.
      -
        inv H0. functional inversion H; inv H1;
        constructor; simpl; trivial.
        rewrite H4 in ×.
        clear H4 H.
        generalize dependent _x.
        induction _x0.
        intros.
        simpl in ×.
        constructor.
        intros.
        simpl in ×.
        constructor.
        inv tf_INJECT.
        assumption.
        eapply IH_x0; auto.
        inv tf_INJECT; assumption.
      -
        inv H0. functional inversion H; subst;
        constructor; simpl; trivial.
    Qed.

    Global Instance cons_intr_inv:
      PreservesInvariants cons_intr_spec.
    Proof.
      preserves_invariants_nested low_level_invariant high_level_invariant; eauto;
      functional inversion H; subst; simpl in *; eauto.
    Qed.

    Global Instance iret_inv: PreservesInvariants iret_spec.
    Proof.
      preserves_invariants_direct low_level_invariant high_level_invariant; eauto 2.
      change (Z.of_nat 257%nat) with 257.
      omega.
    Qed.

    Global Instance set_curid_inv: PreservesInvariants set_curid_spec.
    Proof.
      preserves_invariants_simpl_auto; eauto 2.
    Qed.

    Global Instance set_curid_init_inv: PreservesInvariants set_curid_init_spec.
    Proof.
      preserves_invariants_simpl_auto; eauto 2.
    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.
    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.
    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.

Layer Definition

  Definition dhandlerintro_fresh : compatlayer (cdata RData) :=
    intr_default_handler gensem intr_default_handler_spec.

  Definition dhandlerintro_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
           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
           ticket_lock_init gensem ticket_lock_init0_spec
           serial_irq_check gensem serial_irq_check_spec
           iret gensem iret_spec
           cli gensem cli_spec
           sti gensem sti_spec
           serial_irq_current gensem serial_irq_current_spec
           ic_intr gensem ic_intr_spec
           save_context primcall_save_context_compatsem save_context_spec
           restore_context primcall_restore_context_compatsem restore_context_spec
           ioapic_mask gensem ioapic_mask_spec
           ioapic_unmask gensem ioapic_unmask_spec
           serial_putc gensem serial_putc_spec
           serial_hw_intr gensem serial_hw_intr_spec
serial device
           cons_buf_read gensem cons_buf_read_spec
           cons_intr gensem cons_intr_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 dhandlerintro : compatlayer (cdata RData) :=
    dhandlerintro_fresh dhandlerintro_passthrough.

End WITHMEM.