Library mcertikos.proc.VMXInitGen


Require Import Coqlib.
Require Import Errors.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Op.
Require Import Asm.
Require Import Events.
Require Import Globalenvs.
Require Import Smallstep.
Require Import Values.
Require Import Memory.
Require Import Maps.
Require Import CommonTactic.
Require Import AuxLemma.
Require Import FlatMemory.
Require Import AuxStateDataType.
Require Import Constant.
Require Import GlobIdent.
Require Import RealParams.
Require Import AsmImplLemma.
Require Import GenSem.
Require Import RefinementTactic.
Require Import PrimSemantics.
Require Import XOmega.

Require Import liblayers.logic.PTreeModules.
Require Import liblayers.logic.LayerLogicImpl.
Require Import liblayers.compcertx.Stencil.
Require Import liblayers.compcertx.MakeProgram.
Require Import liblayers.compat.CompatLayers.
Require Import liblayers.compat.CompatGenSem.
Require Import compcert.cfrontend.Ctypes.

Require Import LayerCalculusLemma.
Require Import AbstractDataType.

Require Import VVMXInit.
Require Import VMXInitGenSpec.
Require Import LoadStoreSem3.

Definition of the refinement relation

Section Refinement.

  Local Open Scope string_scope.
  Local Open Scope error_monad_scope.
  Local Open Scope Z_scope.

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



  Notation HDATA := RData.
  Notation LDATA := RData.

  Notation HDATAOps := (cdata (cdata_ops := pproc_data_ops) HDATA).
  Notation LDATAOps := (cdata (cdata_ops := pproc_data_ops) LDATA).

  Section WITHMEM.

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

Relation between raw data at two layers
    Record relate_RData (f: meminj) (hadt: HDATA) (ladt: LDATA) :=
      mkrelate_RData {
          flatmem_re: FlatMem.flatmem_inj (HP hadt) (HP ladt);
          vmxinfo_re: vmxinfo hadt = vmxinfo ladt;
          CR3_re: CR3 hadt = CR3 ladt;
          ikern_re: ikern hadt = ikern ladt;
          pg_re: pg hadt = pg ladt;
          ihost_re: ihost hadt = ihost ladt;
          AC_re: AC hadt = AC ladt;
          ti_fst_re: (fst (ti hadt)) = (fst (ti ladt));
          ti_snd_re: val_inject f (snd (ti hadt)) (snd (ti ladt));
          LAT_re: LAT hadt = LAT ladt;
          nps_re: nps hadt = nps ladt;
          init_re: init hadt = init ladt;

          pperm_re: pperm hadt = pperm ladt;
          PT_re: PT hadt = PT ladt;
          ptp_re: ptpool hadt = ptpool ladt;
          idpde_re: idpde hadt = idpde ladt;
          ipt_re: ipt hadt = ipt ladt;
          smspool_re: smspool hadt = smspool ladt;

          CPU_ID_re: CPU_ID hadt = CPU_ID ladt;
          cid_re: cid hadt = cid ladt;
          multi_oracle_re: (multi_oracle hadt) = (multi_oracle ladt);
          multi_log_re: (multi_log hadt) = (multi_log ladt);
          lock_re: lock hadt = lock ladt;

          com1_re: com1 hadt = com1 ladt;
          console_re: console hadt = console ladt;
          console_concrete_re: console_concrete hadt = console_concrete ladt;
          ioapic_re: ioapic ladt = ioapic hadt;
          lapic_re: lapic ladt = lapic hadt;
          intr_flag_re: intr_flag ladt = intr_flag hadt;
          curr_intr_num_re: curr_intr_num ladt = curr_intr_num hadt;
          in_intr_re: in_intr ladt = in_intr hadt;
          drv_serial_re: drv_serial hadt = drv_serial ladt;

          kctxt_re: kctxt_inj f num_proc (kctxt hadt) (kctxt ladt);
          uctxt_re: uctxt_inj f (uctxt hadt) (uctxt ladt);
          
          syncchpool_re: syncchpool hadt = syncchpool ladt;

          ept_re: ept hadt = ept ladt;

          vmcs_re: VMCSPool_inj f (vmcs hadt) (vmcs ladt);
          vmx_re: VMXPool_inj f (vmx hadt) (vmx ladt)
        }.

    Inductive match_RData: stencilHDATAmemmeminjProp :=
    | MATCH_RDATA: habd m f s, match_RData s habd m f.

    Local Hint Resolve MATCH_RDATA.

    Global Instance rel_ops: CompatRelOps HDATAOps LDATAOps :=
      {
        relate_AbData s f d1 d2 := relate_RData f d1 d2;
        match_AbData s d1 m f := match_RData s d1 m f;
        new_glbl := nil
      }.

Properties of relations

    Section Rel_Property.

Prove that after taking one step, the refinement relation still holds
      Lemma relate_incr:
         abd abd´ f ,
          relate_RData f abd abd´
          → inject_incr f
          → relate_RData abd abd´.
      Proof.
        inversion 1; subst; intros; inv H; constructor; eauto.
        - eapply kctxt_inj_incr; eauto.
        - eapply uctxt_inj_incr; eauto.
        - unfold VMCSPool_inj.
          intros.
          eapply VMCS_inj_incr; eauto.
        - unfold VMXPool_inj.
          intros.
          eapply VMX_inj_incr; eauto.
      Qed.

    End Rel_Property.

    Global Instance rel_prf: CompatRel HDATAOps LDATAOps.
    Proof.
      constructor; intros; simpl; trivial.
      eapply relate_incr; eauto.
    Qed.

Proofs the one-step forward simulations for the low level specifications

    Section OneStep_Forward_Relation.

      Section FRESH_PRIM.

        Lemma vmx_return_from_guest_spec_ref:
          compatsim (crel HDATA LDATA)
                    (primcall_vmx_return_compatsem vmx_return_from_guest_spec)
                    vmx_return_from_guest_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          inv match_extcall_states.
          exploit vmx_return_from_guest_exist; eauto.

          intros (labd´ & HP & HM).
          intros.
          refine_split; try econstructor; eauto.
          - eapply reg_symbol_inject; eassumption.
          - specialize (match_reg ESP); unfold Pregmap.get in match_reg.
            inv ASM_INV.
            inv inv_inject_neutral.
            inv match_reg; try congruence.
          - intros.
            specialize (match_reg ESP); unfold Pregmap.get in match_reg.
            inv match_reg; try congruence.
            specialize (HESP_STACK _ _ (eq_sym H0)).
            replace b1 with b2 by congruence.
            split.
            × apply Ple_trans with b0;
              [ apply HESP_STACK | apply (match_inject_forward _ _ _ H2) ].
            × apply (Mem.valid_block_inject_2 _ _ _ _ _ _ H2 match_inject).
          - econstructor; eauto. constructor.
          - intros.
            destruct reg; simpl; try apply match_reg; eauto; try constructor.
            specialize (match_reg PC).
            unfold Pregmap.get in match_reg.
            rewrite HPC in match_reg.
            inv match_reg.
            inv ASM_INV.
            inv inv_inject_neutral.
            specialize (inv_reg_inject_neutral PC).
            rewrite <- H1 in inv_reg_inject_neutral.
            inv inv_reg_inject_neutral.
            generalize H2; intro tmp.
            eapply inject_forward_equal´ in tmp; eauto.
            inv tmp.
            econstructor; eauto.
            destruct i; simpl; try apply match_reg; eauto.
            destruct f; simpl; try apply match_reg; eauto.
            destruct c; simpl; try apply match_reg; eauto.
        Qed.

        Lemma vmx_run_vm_spec_ref:
          compatsim (crel HDATA LDATA)
                    (primcall_vmx_enter_compatsem vm_run_spec vmx_run_vm)
                    vmx_run_vm_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          inv match_extcall_states.
          set (rs02 := ((Pregmap.init Vundef) # EDI <- (rs2 EDI)) # EBP <- (rs2 EBP)).
          assert (HVAL: reg,
                          val_inject ι (Pregmap.get reg rs01) (Pregmap.get reg rs02)).
          {
            subst rs01 rs02.
            val_inject_simpl.
          }

          exploit vm_run_exist; try eassumption; eauto.

          intros r; destruct r as [| [] | [] | | [] |]; try constructor;
          try apply ASM_INV.

          intros r; destruct r as [| [] | [] | | [] |]; try constructor;
          try apply ASM_INV.
          eapply Hlow.

          intros (labd´ & r´0 & HP & HM & HReg & Hwt & Hinj_neutral).
          refine_split; try econstructor; eauto.
          - eapply reg_symbol_inject; eassumption.
          - specialize (match_reg ESP); unfold Pregmap.get in match_reg.
            inv ASM_INV.
            inv inv_inject_neutral.
            inv match_reg; try congruence.
          - intros.
            specialize (match_reg ESP); unfold Pregmap.get in match_reg.
            inv match_reg; try congruence.
            specialize (HESP_STACK _ _ (eq_sym H0)).
            replace b1 with b2 by congruence.
            split.
            × apply Ple_trans with b0;
              [ apply HESP_STACK | apply (match_inject_forward _ _ _ H2) ].
            × apply (Mem.valid_block_inject_2 _ _ _ _ _ _ H2 match_inject).
          - econstructor; eauto. constructor.
          - subst rs0.
            val_inject_simpl.
        Qed.

        Lemma vmx_get_reg_kernel_mode:
           d2 d2´ v1,
            vmx_get_reg_spec v1 d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_reg_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_reg_spec) vmx_get_reg_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_reg_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_reg_kernel_mode; eauto.
        Qed.

        Lemma vmx_set_mmap_kernel_mode:
           d2 d2´ v1 v2 v3,
            vmx_set_mmap_spec v1 v2 v3 d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros.
          functional inversion H; subst;
          functional inversion H1; subst;
          simpl; auto.
        Qed.

        Lemma vmx_set_mmap_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_set_mmap_spec) vmx_set_mmap_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_set_mmap_exist; eauto 1.
          intros (labd´ & HP & HM).
          refine_split; try econstructor; eauto.
          - eapply vmx_set_mmap_kernel_mode; eauto.
          - constructor.
        Qed.

        Lemma vmx_init_kernel_mode:
           d2 d2´ pml4ept_b stack_b idt_b msr_bitmap_b io_bitmap_b host_rip_b ,
            vmx_init_spec pml4ept_b stack_b idt_b msr_bitmap_b io_bitmap_b host_rip_b d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_init_spec_ref:
          compatsim (crel HDATA LDATA) (vmcs_set_defaults_compatsem vmx_init_spec) vmx_init_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_init_exist; eauto 1; intros.
          - exploit (stencil_find_symbol_inject´ s ι EPT_LOC pml4ept_b); eauto.
            intros HFB.
            econstructor; eauto. rewrite Int.add_zero; trivial.
          - exploit (stencil_find_symbol_inject´ s ι STACK_LOC stack_b); eauto.
            intros HFB.
            econstructor; eauto. rewrite Int.add_zero; trivial.
          - exploit (stencil_find_symbol_inject´ s ι idt_LOC idt_b); eauto.
            intros HFB.
            econstructor; eauto. rewrite Int.add_zero; trivial.
          - exploit (stencil_find_symbol_inject´ s ι msr_bitmap_LOC msr_bitmap_b); eauto.
            intros HFB.
            econstructor; eauto. rewrite Int.add_zero; trivial.
          - exploit (stencil_find_symbol_inject´ s ι io_bitmap_LOC io_bitmap_b); eauto.
            intros HFB.
            econstructor; eauto. rewrite Int.add_zero; trivial.
          - exploit (stencil_find_symbol_inject´ s ι vmx_return_from_guest host_rip_b); eauto.
            intros HFB.
            econstructor; eauto. rewrite Int.add_zero; trivial.
          - destruct H as (labd´ & HP & HM).
            refine_split; try econstructor; eauto.
            + eapply vmx_init_kernel_mode; eauto.
            + constructor.
        Qed.

        Lemma vmx_get_next_eip_kernel_mode:
           d2 d2´,
            vmx_get_next_eip_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_next_eip_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_next_eip_spec) vmx_get_next_eip_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_next_eip_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_next_eip_kernel_mode; eauto.
        Qed.

        Lemma vmx_get_exit_qualification_kernel_mode:
           d2 d2´,
            vmx_get_exit_qualification_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_exit_qualification_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_exit_qualification_spec) vmx_get_exit_qualification_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_exit_qualification_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_exit_qualification_kernel_mode; eauto.
        Qed.

        Lemma vmx_get_io_width_kernel_mode:
           d2 d2´,
            vmx_get_io_width_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_io_width_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_io_width_spec) vmx_get_io_width_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_io_width_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_io_width_kernel_mode; eauto.
        Qed.

        Lemma vmx_get_io_write_kernel_mode:
           d2 d2´,
            vmx_get_io_write_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_io_write_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_io_write_spec) vmx_get_io_write_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_io_write_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_io_write_kernel_mode; eauto.
        Qed.

        Lemma vmx_get_exit_io_rep_kernel_mode:
           d2 d2´,
            vmx_get_exit_io_rep_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_exit_io_rep_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_exit_io_rep_spec) vmx_get_exit_io_rep_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_exit_io_rep_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_exit_io_rep_kernel_mode; eauto.
        Qed.

        Lemma vmx_get_exit_io_str_kernel_mode:
           d2 d2´,
            vmx_get_exit_io_str_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_exit_io_str_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_exit_io_str_spec) vmx_get_exit_io_str_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_exit_io_str_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_exit_io_str_kernel_mode; eauto.
        Qed.

        Lemma vmx_get_exit_io_port_kernel_mode:
           d2 d2´,
            vmx_get_exit_io_port_spec d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_get_exit_io_port_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_get_exit_io_port_spec) vmx_get_exit_io_port_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_get_exit_io_port_exist; eauto 1.
          refine_split; try econstructor; eauto.
          eapply vmx_get_exit_io_port_kernel_mode; eauto.
        Qed.

        Lemma vmx_set_reg_kernel_mode:
           d2 d2´ v1 v2,
            vmx_set_reg_spec v1 v2 d2 = Some d2´
            → kernel_mode d2.
        Proof.
          intros. functional inversion H; subst;
          simpl; auto.
        Qed.

        Lemma vmx_set_reg_spec_ref:
          compatsim (crel HDATA LDATA) (gensem vmx_set_reg_spec) vmx_set_reg_spec_low.
        Proof.
          compatsim_simpl (@match_AbData).
          exploit vmx_set_reg_exist; eauto 1.
          intros (labd´ & HP & HM).
          refine_split; try econstructor; eauto.
          - eapply vmx_set_reg_kernel_mode; eauto.
          - constructor.
        Qed.

      End FRESH_PRIM.

      Global Instance cpu_id_range: high_level_invariant_impl_CPU_ID_range.
      Proof.
        econstructor; eauto.
        intros.
        inv H.
        assumption.
      Qed.

      Section PASSTHROUGH_PRIM.

        Global Instance: (LoadStoreProp (hflatmem_store:= flatmem_store) (lflatmem_store:= flatmem_store)).
        Proof.
          accessor_prop_tac.
          - eapply flatmem_store_exists; eauto.
        Qed.

        Lemma passthrough_correct:
          sim (crel HDATA LDATA) vmxinit_passthrough vmxintro.
        Proof.
          sim_oplus.
          - apply fload_sim.
          - apply fstore_sim.
          - apply vmxinfo_get_sim.
          - apply palloc_sim.
          - apply setPT_sim.
          - apply ptRead_sim.
          - apply ptResv_sim.
          - apply shared_mem_status_sim.
          - apply offer_shared_mem_sim.

          - apply biglow_thread_wakeup_sim.
          - apply biglow_thread_yield_sim.
          - apply biglow_thread_sleep_sim.
          - apply biglow_sched_init_sim.
          - apply uctx_get_sim.
          - apply uctx_set_sim.
          - apply biglow_proc_create_sim.

          - apply container_get_nchildren_sim.
          - apply container_get_quota_sim.
          - apply container_get_usage_sim.
          - apply container_can_consume_sim.

          - apply get_CPU_ID_sim.
          - apply get_curid_sim.

          - apply acquire_lock_SC_sim.
          - apply release_lock_SC_sim.
          - apply get_sync_chan_busy_sim.
          - apply set_sync_chan_busy_sim.
          - apply ipc_send_body_sim.
          - apply ipc_receive_body_sim.

          - apply rdmsr_sim.
          - apply wrmsr_sim.
          - apply vmx_set_intercept_intwin_sim.
          - apply vmx_set_desc1_sim.
          - apply vmx_set_desc2_sim.
          - apply vmx_inject_event_sim.
          - apply vmx_set_tsc_offset_sim.
          - apply vmx_get_tsc_offset_sim.
          - apply vmx_get_exit_reason_sim.
          - apply vmx_get_exit_fault_addr_sim.
          - apply vmx_check_pending_event_sim.
          - apply vmx_check_int_shadow_sim.

          - apply cli_sim.
          - apply sti_sim.
          - apply serial_intr_disable_sim.
          - apply serial_intr_enable_sim.
          - apply serial_putc_sim.
          - apply cons_buf_read_sim.

          - apply hostin_sim.
          - apply hostout_sim.
          - apply proc_create_postinit_sim.
          - apply trap_info_get_sim.
          - apply trap_info_ret_sim.
          - apply proc_start_user_sim.
            intros; inv H; auto.
          - apply proc_exit_user_sim.
          - apply proc_start_user_sim2.
            intros; inv H; auto.
          - apply proc_exit_user_sim2.
          - layer_sim_simpl.
            + eapply load_correct3.
            + eapply store_correct3.
        Qed.

      End PASSTHROUGH_PRIM.

    End OneStep_Forward_Relation.

  End WITHMEM.

End Refinement.