vx32.h (4059B)
1 // VX32 Virtual execution environment 2 3 #ifndef VX32_H 4 #define VX32_H 5 6 #include <inttypes.h> 7 #include <sys/types.h> 8 #include <sys/signal.h> 9 10 // VX memory access permission bits 11 #define VXPERM_READ 0x01 12 #define VXPERM_WRITE 0x02 13 #define VXPERM_EXEC 0x04 14 15 // VX trap code categories 16 #define VXTRAP_CPU 0x000 17 #define VXTRAP_IRQ 0x100 18 #define VXTRAP_SOFT 0x200 19 #define VXTRAP_SYSCALL 0x300 20 #define VXTRAP_SIGNAL 0x400 21 #define VXTRAP_SINGLESTEP 0x500 22 #define VXTRAP_CATEGORY 0xF00 23 24 #define VXIRQ_TIMER 0 25 26 // VX processor traps (same numbering as Intel) 27 #define VXTRAP_DIVIDE 0x000 // Integer divide by zero 28 #define VXTRAP_DEBUG 0x001 // Debug exception (single step) 29 #define VXTRAP_BREAKPOINT 0x003 // INT3 (breakpoint) instruction 30 #define VXTRAP_INVALID 0x006 // Invalid instruction opcode 31 #define VXTRAP_FPOFF 0x007 // Floating point unit needed 32 #define VXTRAP_PAGEFAULT 0x00e // Reference to inaccessible page 33 #define VXTRAP_ALIGN 0x011 // Misaligned memory reference 34 #define VXTRAP_FLOAT 0x013 // SIMD floating-point exception 35 36 // Intel 32-bit register numbers, in Intel order 37 #define EAX 0 38 #define ECX 1 39 #define EDX 2 40 #define EBX 3 41 #define ESP 4 42 #define EBP 5 43 #define ESI 6 44 #define EDI 7 45 46 // EFLAGS bits implemented in vx32 47 #define EF_CF_BIT 0 48 #define EF_PF_BIT 2 49 #define EF_ZF_BIT 6 50 #define EF_SF_BIT 7 51 #define EF_DF_BIT 10 52 #define EF_OF_BIT 11 53 #define EF_CF (1 << EF_CF_BIT) 54 #define EF_PF (1 << EF_PF_BIT) 55 #define EF_ZF (1 << EF_ZF_BIT) 56 #define EF_SF (1 << EF_SF_BIT) 57 #define EF_DF (1 << EF_DF_BIT) 58 #define EF_OF (1 << EF_OF_BIT) 59 60 // Signal handlers. 61 int vx32_siginit(void); 62 int vx32_sighandler(int, siginfo_t*, void*); 63 64 typedef struct vxcpu vxcpu; 65 typedef struct vxemu vxemu; 66 typedef struct vxmem vxmem; 67 typedef struct vxmmap vxmmap; 68 typedef struct vxproc vxproc; 69 70 // VX32 CPU state 71 struct vxcpu { 72 uint32_t reg[8]; 73 uint32_t eip; 74 uint32_t eflags; 75 76 uint32_t traperr; 77 uint32_t trapva; // cr2 78 }; 79 80 // Memory 81 #define VXMEMMAP_GODMODE 0x01 // Disable permission checking 82 83 struct vxmem { 84 ssize_t (*read)(vxmem*, void *data, uint32_t addr, uint32_t len); 85 ssize_t (*write)(vxmem*, const void *data, uint32_t addr, uint32_t len); 86 vxmmap* (*map)(vxmem*, uint32_t flags); 87 void (*unmap)(vxmem*, vxmmap*); 88 int (*checkperm)(vxmem*, uint32_t addr, uint32_t len, uint32_t perm, uint32_t *out_faultva); 89 int (*setperm)(vxmem*, uint32_t addr, uint32_t len, uint32_t perm); 90 int (*resize)(vxmem*, size_t); 91 void (*free)(vxmem*); 92 93 vxmmap *mapped; 94 vxmmap *mapped_godmode; 95 }; 96 int vxmem_read(vxmem*, void *data, uint32_t addr, uint32_t len); 97 int vxmem_write(vxmem*, const void *data, uint32_t addr, uint32_t len); 98 vxmmap *vxmem_map(vxmem*, uint32_t); 99 void vxmem_unmap(vxmem*, vxmmap*); 100 int vxmem_checkperm(vxmem*, uint32_t addr, uint32_t len, uint32_t perm, uint32_t *out_faultva); 101 int vxmem_setperm(vxmem*, uint32_t addr, uint32_t len, uint32_t perm); 102 void vxmem_free(vxmem*); 103 int vxmem_resize(vxmem*, size_t); 104 105 vxmem* vxmem_chunk_new(int); 106 vxmem* vxmem_chunk_copy(vxmem*); 107 108 // A single memory-mapped address space region. 109 struct vxmmap { 110 int ref; 111 void *base; 112 uint32_t size; 113 }; 114 115 116 // Process state. 117 struct vxproc { 118 vxemu *emu; // Emulation state 119 vxcpu *cpu; // Register contents (points into emu) 120 vxmem *mem; // Memory layout 121 int vxpno; 122 int allowfp; 123 }; 124 125 vxproc *vxproc_alloc(void); 126 void vxproc_free(vxproc*); 127 int vxproc_run(vxproc *proc); 128 void vxproc_flush(vxproc *proc); 129 130 // ELF loader 131 int vxproc_loadelffile(vxproc *p, const char *file, 132 const char *const *argv, const char *const *envp); 133 int vxproc_loadelfmem(vxproc *p, const void *mem, size_t nmem, 134 const char *const *argv, const char *const *envp); 135 136 // VX system call numbers 137 #define VXPC_EXIT 0x1000 138 #define VXPC_WRITE 0x3100 139 #define VXPC_READ 0x03 140 #define VXPC_SBRK 0x04 141 #define VXPC_SETPERM 0x01 142 143 // VX system call errors 144 #define VXTRAP_INVARG 0x301 // Invalid argument to system call 145 146 typedef int vxpcallhandler(vxproc *proc, void *pcalldata); 147 int vxproc_run_pcall(vxproc *proc, int(*)(vxproc*, void*), void*); 148 149 extern int vx32_debugxlate; // dump translations to stderr 150 151 #endif