LCOV - code coverage report
Current view: top level - x86_instruction_emulator - x86_emulate.c (source / functions) Hit Total Coverage
Test: trace.lcov_info_final Lines: 41 49 83.7 %
Date: 2017-04-07 10:24:39 Functions: 4 5 80.0 %

          Line data    Source code
       1             : #include "x86_emulate.h"
       2             : 
       3             : #include <sys/mman.h>
       4             : 
       5             : #define cpu_has_amd_erratum(nr) 0
       6             : #define mark_regs_dirty(r) ((void)(r))
       7             : #define cpu_has_mpx false
       8             : #define read_bndcfgu() 0
       9             : #define xstate_set_init(what)
      10             : 
      11             : /* For generic assembly code: use macros to define operation/operand sizes. */
      12             : #ifdef __i386__
      13             : # define r(name)       e ## name
      14             : # define __OS          "l"  /* Operation Suffix */
      15             : # define __OP          "e"  /* Operand Prefix */
      16             : #else
      17             : # define r(name)       r ## name
      18             : # define __OS          "q"  /* Operation Suffix */
      19             : # define __OP          "r"  /* Operand Prefix */
      20             : #endif
      21             : 
      22             : #define get_stub(stb) ({                         \
      23             :     assert(!(stb).addr);                         \
      24             :     (void *)((stb).addr = (uintptr_t)(stb).buf); \
      25             : })
      26             : #define put_stub(stb) ((stb).addr = 0)
      27             : 
      28             : uint32_t mxcsr_mask = 0x0000ffbf;
      29             : 
      30       45254 : bool emul_test_init(void)
      31             : {
      32             :     unsigned long sp;
      33             : 
      34       45254 :     if ( cpu_has_fxsr )
      35             :     {
      36             :         static union __attribute__((__aligned__(16))) {
      37             :             char x[464];
      38             :             struct {
      39             :                 uint32_t other[6];
      40             :                 uint32_t mxcsr;
      41             :                 uint32_t mxcsr_mask;
      42             :                 /* ... */
      43             :             };
      44             :         } fxs;
      45             : 
      46       45254 :         asm ( "fxsave %0" : "=m" (fxs) );
      47       45254 :         if ( fxs.mxcsr_mask )
      48       45254 :             mxcsr_mask = fxs.mxcsr_mask;
      49             :     }
      50             : 
      51             :     /*
      52             :      * Mark the entire stack executable so that the stub executions
      53             :      * don't fault
      54             :      */
      55             : #ifdef __x86_64__
      56       45254 :     asm ("movq %%rsp, %0" : "=g" (sp));
      57             : #else
      58             :     asm ("movl %%esp, %0" : "=g" (sp));
      59             : #endif
      60             : 
      61       45254 :     return mprotect((void *)(sp & -0x1000L) - (MMAP_SZ - 0x1000),
      62             :                     MMAP_SZ, PROT_READ|PROT_WRITE|PROT_EXEC) == 0;
      63             : }
      64             : 
      65      115337 : int emul_test_cpuid(
      66             :     uint32_t leaf,
      67             :     uint32_t subleaf,
      68             :     struct cpuid_leaf *res,
      69             :     struct x86_emulate_ctxt *ctxt)
      70             : {
      71      115337 :     asm ("cpuid"
      72             :          : "=a" (res->a), "=b" (res->b), "=c" (res->c), "=d" (res->d)
      73             :          : "a" (leaf), "c" (subleaf));
      74             : 
      75             :     /*
      76             :      * Some instructions and features can be emulated without specific
      77             :      * hardware support.  These features are unconditionally reported here,
      78             :      * for testing and fuzzing-coverage purposes.
      79             :      */
      80      115337 :     switch ( leaf )
      81             :     {
      82             :     case 1:
      83      100481 :         res->c |= 1U << 22; /* MOVBE */
      84             : 
      85      100481 :         res->d |= 1U << 19; /* CLFLUSH */
      86      100481 :         break;
      87             : 
      88             :     case 7:
      89        3507 :         switch ( subleaf )
      90             :         {
      91             :         case 0:
      92        3220 :             res->b |= 1U << 11; /* rtm */
      93        3220 :             res->b |= 1U << 19; /* ADCX/ADOX */
      94        3220 :             res->b |= 1U << 20; /* smap */
      95        3220 :             res->b |= 1U << 23; /* CLFLUSHOPT */
      96        3220 :             res->b |= 1U << 24; /* CLWB */
      97             : 
      98        3220 :             res->c |= 1U << 22; /* RDPID */
      99        3220 :             break;
     100             :         }
     101        3507 :         break;
     102             : 
     103             :     case 0x80000001:
     104       10588 :         res->c |= 1U << 0; /* LAHF/SAHF */
     105       10588 :         res->c |= 1U << 4; /* cr8_legacy */
     106       10588 :         res->c |= 1U << 5; /* LZCNT */
     107             : 
     108       10588 :         if ( ctxt->vendor == X86_VENDOR_AMD )
     109           0 :             res->c |= 1U << 7; /* misalignsse */
     110             : 
     111       10588 :         break;
     112             :     }
     113             : 
     114      115337 :     return X86EMUL_OKAY;
     115             : }
     116             : 
     117           0 : int emul_test_read_cr(
     118             :     unsigned int reg,
     119             :     unsigned long *val,
     120             :     struct x86_emulate_ctxt *ctxt)
     121             : {
     122             :     /* Fake just enough state for the emulator's _get_fpu() to be happy. */
     123           0 :     switch ( reg )
     124             :     {
     125             :     case 0:
     126           0 :         *val = 0x00000001; /* PE */
     127           0 :         return X86EMUL_OKAY;
     128             : 
     129             :     case 4:
     130             :         /* OSFXSR, OSXMMEXCPT, and maybe OSXSAVE */
     131           0 :         *val = 0x00000600 | (cpu_has_xsave ? 0x00040000 : 0);
     132           0 :         return X86EMUL_OKAY;
     133             :     }
     134             : 
     135           0 :     return X86EMUL_UNHANDLEABLE;
     136             : }
     137             : 
     138       23338 : int emul_test_get_fpu(
     139             :     void (*exception_callback)(void *, struct cpu_user_regs *),
     140             :     void *exception_callback_arg,
     141             :     enum x86_emulate_fpu_type type,
     142             :     struct x86_emulate_ctxt *ctxt)
     143             : {
     144       23338 :     switch ( type )
     145             :     {
     146             :     case X86EMUL_FPU_fpu:
     147       10435 :         break;
     148             :     case X86EMUL_FPU_mmx:
     149        5061 :         if ( cpu_has_mmx )
     150        5061 :             break;
     151             :     case X86EMUL_FPU_xmm:
     152        3985 :         if ( cpu_has_sse )
     153        3985 :             break;
     154             :     case X86EMUL_FPU_ymm:
     155        3846 :         if ( cpu_has_avx )
     156        3846 :             break;
     157             :     default:
     158          11 :         return X86EMUL_UNHANDLEABLE;
     159             :     }
     160       23327 :     return X86EMUL_OKAY;
     161             : }
     162             : 
     163       23319 : void emul_test_put_fpu(
     164             :     struct x86_emulate_ctxt *ctxt,
     165             :     enum x86_emulate_fpu_type backout,
     166             :     const struct x86_emul_fpu_aux *aux)
     167             : {
     168             :     /* TBD */
     169       23319 : }
     170             : 
     171             : #include "x86_emulate/x86_emulate.c"

Generated by: LCOV version 1.11