diff options
| -rw-r--r-- | miasm2/analysis/machine.py | 2 | ||||
| -rw-r--r-- | miasm2/arch/mips32/arch.py | 14 | ||||
| -rw-r--r-- | miasm2/arch/mips32/jit.py | 37 | ||||
| -rw-r--r-- | miasm2/arch/mips32/regs.py | 7 | ||||
| -rw-r--r-- | miasm2/arch/mips32/sem.py | 8 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_arm.c | 27 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_mips32.c | 624 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_mips32.h | 171 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_msp430.c | 31 | ||||
| -rw-r--r-- | miasm2/jitter/arch/JitCore_x86.c | 36 | ||||
| -rw-r--r-- | miasm2/jitter/jitcore_llvm.py | 13 | ||||
| -rw-r--r-- | miasm2/jitter/jitcore_tcc.py | 1 | ||||
| -rw-r--r-- | miasm2/jitter/jitload.py | 33 | ||||
| -rw-r--r-- | miasm2/jitter/vm_mngr_py.c | 21 | ||||
| -rwxr-xr-x | setup.py | 40 | ||||
| -rw-r--r-- | test/arch/mips32/arch.py | 2 |
16 files changed, 963 insertions, 104 deletions
diff --git a/miasm2/analysis/machine.py b/miasm2/analysis/machine.py index 55d7668c..bf433554 100644 --- a/miasm2/analysis/machine.py +++ b/miasm2/analysis/machine.py @@ -63,10 +63,12 @@ class Machine(object): from miasm2.arch.mips32.disasm import dis_mips32b as dis_engine from miasm2.arch.mips32.arch import mn_mips32 as mn from miasm2.arch.mips32.ira import ir_a_mips32 as ira + #from miasm2.arch.mips32.jit import jitter_mips32 as jitter elif machine_name == "mips32l": from miasm2.arch.mips32.disasm import dis_mips32l as dis_engine from miasm2.arch.mips32.arch import mn_mips32 as mn from miasm2.arch.mips32.ira import ir_a_mips32 as ira + from miasm2.arch.mips32.jit import jitter_mips32 as jitter else: raise ValueError('Unknown machine: %s' % machine_name) diff --git a/miasm2/arch/mips32/arch.py b/miasm2/arch/mips32/arch.py index 447669ef..ff3c90ec 100644 --- a/miasm2/arch/mips32/arch.py +++ b/miasm2/arch/mips32/arch.py @@ -96,7 +96,7 @@ class instruction_mips32(instruction): return i def dstflow2label(self, symbol_pool): - if self.name == "J": + if self.name in ["J", 'JAL']: e = self.args[0].arg ad = (self.offset & (0xFFFFFFFF ^ ((1<< 28)-1))) + e l = symbol_pool.getby_offset_create(ad) @@ -188,8 +188,8 @@ class mn_mips32(cls_mn): all_mn_mode = defaultdict(list) all_mn_name = defaultdict(list) all_mn_inst = defaultdict(list) - pc = PC - sp = SP + pc = {'l':PC, 'b':PC} + sp = {'l':SP, 'b':SP} instruction = instruction_mips32 max_instruction_len = 4 @@ -252,7 +252,13 @@ class mn_mips32(cls_mn): def value(self, mode): v = super(mn_mips32, self).value(mode) - return [x for x in v] + if mode == 'l': + return [x[::-1] for x in v] + elif mode == 'b': + return [x for x in v] + else: + raise NotImplementedError('bad attrib') + def mips32op(name, fields, args=None, alias=False): diff --git a/miasm2/arch/mips32/jit.py b/miasm2/arch/mips32/jit.py new file mode 100644 index 00000000..70e05380 --- /dev/null +++ b/miasm2/arch/mips32/jit.py @@ -0,0 +1,37 @@ +from miasm2.jitter.jitload import jitter +from miasm2.core import asmbloc +from miasm2.core.utils import * +from miasm2.arch.mips32.sem import ir_mips32 + +import logging + +log = logging.getLogger('jit_mips32') +hnd = logging.StreamHandler() +hnd.setFormatter(logging.Formatter("[%(levelname)s]: %(message)s")) +log.addHandler(hnd) +log.setLevel(logging.CRITICAL) + +class jitter_mips32(jitter): + + def __init__(self, *args, **kwargs): + sp = asmbloc.asm_symbol_pool() + jitter.__init__(self, ir_mips32(sp), *args, **kwargs) + self.my_ir.jit_pc = self.my_ir.arch.regs.PC + self.my_ir.attrib = 'l' + + def vm_push_uint32_t(self, v): + self.cpu.SP -= 4 + self.vm.vm_set_mem(self.cpu.SP, pck32(v)) + + def vm_pop_uint32_t(self): + x = upck32(self.vm.vm_get_mem(self.cpu.SP, 4)) + self.cpu.SP += 4 + return x + + def get_stack_arg(self, n): + x = upck32(self.vm.vm_get_mem(self.cpu.SP + 4 * n, 4)) + return x + + def init_run(self, *args, **kwargs): + jitter.init_run(self, *args, **kwargs) + self.cpu.PC = self.pc diff --git a/miasm2/arch/mips32/regs.py b/miasm2/arch/mips32/regs.py index 2667f482..bf4926a8 100644 --- a/miasm2/arch/mips32/regs.py +++ b/miasm2/arch/mips32/regs.py @@ -9,6 +9,8 @@ gen_reg('PC', globals()) gen_reg('R_LO', globals()) gen_reg('R_HI', globals()) +PC_init = ExprId("PC_init") + regs32_str = ["ZERO", 'AT', 'V0', 'V1'] +\ ['A%d'%i for i in xrange(4)] +\ ['T%d'%i for i in xrange(8)] +\ @@ -42,9 +44,10 @@ regs_flt_expr, regs_flt_init, fltregs = gen_regs(regs_flt_str, globals()) regs_fcc_expr, regs_fcc_init, fccregs = gen_regs(regs_fcc_str, globals()) -all_regs_ids = gpregs_expr + regs_flt_expr + regs_fcc_expr +all_regs_ids = [PC] + gpregs_expr + regs_flt_expr + regs_fcc_expr all_regs_ids_byname = dict([(x.name, x) for x in all_regs_ids]) -all_regs_ids_init = gpregs_init + regs_flt_init + regs_fcc_init +all_regs_ids_init = [PC_init] + gpregs_init + regs_flt_init + regs_fcc_init +all_regs_ids_no_alias = all_regs_ids[:] regs_init = {} for i, r in enumerate(all_regs_ids): diff --git a/miasm2/arch/mips32/sem.py b/miasm2/arch/mips32/sem.py index ab2f1c62..41f38b3d 100644 --- a/miasm2/arch/mips32/sem.py +++ b/miasm2/arch/mips32/sem.py @@ -59,7 +59,7 @@ def lhu(ir, instr, a, b): def beq(ir, instr, a, b, c): e = [] n = ExprId(ir.get_next_break_label(instr)) - dst_o = ExprCond(a-b, c, n) + dst_o = ExprCond(a-b, n, c) e = [ExprAff(PC, dst_o)] return dst_o, e, [] @@ -73,7 +73,7 @@ def bgez(ir, instr, a, b): def bne(ir, instr, a, b, c): e = [] n = ExprId(ir.get_next_break_label(instr)) - dst_o = ExprCond(a-b, n, c) + dst_o = ExprCond(a-b, c, n) e = [ExprAff(PC, dst_o)] return dst_o, e, [] @@ -486,3 +486,7 @@ class ir_mips32(ir): {self.pc: ExprInt32(instr.offset + 4)})) irs[i] = x return dst, instr_ir, extra_ir + + def get_next_break_label(self, instr): + l = self.symbol_pool.getby_offset_create(instr.offset + 8) + return l diff --git a/miasm2/jitter/arch/JitCore_arm.c b/miasm2/jitter/arch/JitCore_arm.c index c8077b84..2b3bc6e0 100644 --- a/miasm2/jitter/arch/JitCore_arm.c +++ b/miasm2/jitter/arch/JitCore_arm.c @@ -65,7 +65,7 @@ typedef struct { -PyObject* vm_get_gpreg(JitCpu* self) +PyObject* cpu_get_gpreg(JitCpu* self) { PyObject *dict = PyDict_New(); PyObject *o; @@ -128,7 +128,7 @@ PyObject* _vm_set_gpreg(JitCpu* self, PyObject *dict) return NULL; } -PyObject* vm_set_gpreg(JitCpu* self, PyObject *args) +PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) { PyObject* dict; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -139,7 +139,7 @@ PyObject* vm_set_gpreg(JitCpu* self, PyObject *args) } -PyObject* vm_set_exception(JitCpu* self, PyObject* args) +PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; uint64_t i; @@ -154,13 +154,13 @@ PyObject* vm_set_exception(JitCpu* self, PyObject* args) return Py_None; } -PyObject* vm_get_exception(JitCpu* self, PyObject* args) +PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { return PyLong_FromUnsignedLongLong((uint64_t)self->vmcpu.exception_flags); } -PyObject * vm_init_regs(JitCpu* self) +PyObject * cpu_init_regs(JitCpu* self) { memset(&self->vmcpu, 0, sizeof(vm_cpu_t)); @@ -184,7 +184,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) } -PyObject * vm_dump_gpregs(JitCpu* self, PyObject* args) +PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { vm_cpu_t* vmcpu; @@ -230,17 +230,17 @@ static PyMemberDef JitCpu_members[] = { }; static PyMethodDef JitCpu_methods[] = { - {"vm_init_regs", (PyCFunction)vm_init_regs, METH_NOARGS, + {"vm_init_regs", (PyCFunction)cpu_init_regs, METH_NOARGS, "X"}, - {"vm_dump_gpregs", (PyCFunction)vm_dump_gpregs, METH_NOARGS, + {"vm_dump_gpregs", (PyCFunction)cpu_dump_gpregs, METH_NOARGS, "X"}, - {"vm_get_gpreg", (PyCFunction)vm_get_gpreg, METH_NOARGS, + {"vm_get_gpreg", (PyCFunction)cpu_get_gpreg, METH_NOARGS, "X"}, - {"vm_set_gpreg", (PyCFunction)vm_set_gpreg, METH_VARARGS, + {"vm_set_gpreg", (PyCFunction)cpu_set_gpreg, METH_VARARGS, "X"}, - {"vm_get_exception", (PyCFunction)vm_get_exception, METH_VARARGS, + {"vm_get_exception", (PyCFunction)cpu_get_exception, METH_VARARGS, "X"}, - {"vm_set_exception", (PyCFunction)vm_set_exception, METH_VARARGS, + {"vm_set_exception", (PyCFunction)cpu_set_exception, METH_VARARGS, "X"}, {NULL} /* Sentinel */ }; @@ -534,5 +534,8 @@ initJitCore_arm(void) Py_INCREF(&JitCpuType); PyModule_AddObject(m, "JitCpu", (PyObject *)&JitCpuType); + /* init vm */ + init_vm_mngr(m); + } diff --git a/miasm2/jitter/arch/JitCore_mips32.c b/miasm2/jitter/arch/JitCore_mips32.c new file mode 100644 index 00000000..7a62a5fc --- /dev/null +++ b/miasm2/jitter/arch/JitCore_mips32.c @@ -0,0 +1,624 @@ +#include <Python.h> +#include "JitCore.h" +#include "structmember.h" +#include <stdint.h> +#include <inttypes.h> +#include "JitCore_mips32.h" + +#define RAISE(errtype, msg) {PyObject* p; p = PyErr_Format( errtype, msg ); return p;} + +typedef struct _reg_dict{ + char* name; + unsigned long offset; +} reg_dict; + + +#define PyGetInt(item, value) \ + if (PyInt_Check(item)){ \ + value = (uint64_t)PyInt_AsLong(item); \ + } \ + else if (PyLong_Check(item)){ \ + value = (uint64_t)PyLong_AsUnsignedLongLong(item); \ + } \ + else{ \ + RAISE(PyExc_TypeError,"arg must be int"); \ + } \ + + + +reg_dict gpreg_dict[] = { {.name = "ZERO", .offset = offsetof(vm_cpu_t, ZERO)}, + {.name = "AT", .offset = offsetof(vm_cpu_t, AT)}, + {.name = "V0", .offset = offsetof(vm_cpu_t, V0)}, + {.name = "V1", .offset = offsetof(vm_cpu_t, V1)}, + {.name = "A0", .offset = offsetof(vm_cpu_t, A0)}, + {.name = "A1", .offset = offsetof(vm_cpu_t, A1)}, + {.name = "A2", .offset = offsetof(vm_cpu_t, A2)}, + {.name = "A3", .offset = offsetof(vm_cpu_t, A3)}, + {.name = "T0", .offset = offsetof(vm_cpu_t, T0)}, + {.name = "T1", .offset = offsetof(vm_cpu_t, T1)}, + {.name = "T2", .offset = offsetof(vm_cpu_t, T2)}, + {.name = "T3", .offset = offsetof(vm_cpu_t, T3)}, + {.name = "T4", .offset = offsetof(vm_cpu_t, T4)}, + {.name = "T5", .offset = offsetof(vm_cpu_t, T5)}, + {.name = "T6", .offset = offsetof(vm_cpu_t, T6)}, + {.name = "T7", .offset = offsetof(vm_cpu_t, T7)}, + {.name = "S0", .offset = offsetof(vm_cpu_t, S0)}, + {.name = "S1", .offset = offsetof(vm_cpu_t, S1)}, + {.name = "S2", .offset = offsetof(vm_cpu_t, S2)}, + {.name = "S3", .offset = offsetof(vm_cpu_t, S3)}, + {.name = "S4", .offset = offsetof(vm_cpu_t, S4)}, + {.name = "S5", .offset = offsetof(vm_cpu_t, S5)}, + {.name = "S6", .offset = offsetof(vm_cpu_t, S6)}, + {.name = "S7", .offset = offsetof(vm_cpu_t, S7)}, + {.name = "T8", .offset = offsetof(vm_cpu_t, T8)}, + {.name = "T9", .offset = offsetof(vm_cpu_t, T9)}, + {.name = "K0", .offset = offsetof(vm_cpu_t, K0)}, + {.name = "K1", .offset = offsetof(vm_cpu_t, K1)}, + {.name = "GP", .offset = offsetof(vm_cpu_t, GP)}, + {.name = "SP", .offset = offsetof(vm_cpu_t, SP)}, + {.name = "FP", .offset = offsetof(vm_cpu_t, FP)}, + {.name = "RA", .offset = offsetof(vm_cpu_t, RA)}, + {.name = "PC", .offset = offsetof(vm_cpu_t, PC)}, +}; + +/************************** JitCpu object **************************/ + +typedef struct { + PyObject_HEAD + PyObject *cpu; /* cpu */ + vm_cpu_t vmcpu; +} JitCpu; + + + +#define get_reg(reg) do { \ + o = PyLong_FromUnsignedLongLong((uint32_t)self->vmcpu.reg); \ + PyDict_SetItemString(dict, #reg, o); \ + Py_DECREF(o); \ + } while(0); + + + +PyObject* cpu_get_gpreg(JitCpu* self) +{ + PyObject *dict = PyDict_New(); + PyObject *o; + + get_reg(ZERO); + get_reg(AT); + get_reg(V0); + get_reg(V1); + get_reg(A0); + get_reg(A1); + get_reg(A2); + get_reg(A3); + get_reg(T0); + get_reg(T1); + get_reg(T2); + get_reg(T3); + get_reg(T4); + get_reg(T5); + get_reg(T6); + get_reg(T7); + get_reg(S0); + get_reg(S1); + get_reg(S2); + get_reg(S3); + get_reg(S4); + get_reg(S5); + get_reg(S6); + get_reg(S7); + get_reg(T8); + get_reg(T9); + get_reg(K0); + get_reg(K1); + get_reg(GP); + get_reg(SP); + get_reg(FP); + get_reg(RA); + get_reg(PC); + + return dict; +} + +PyObject* _vm_set_gpreg(JitCpu* self, PyObject *dict) +{ + PyObject *d_key, *d_value = NULL; + Py_ssize_t pos = 0; + uint64_t val; + unsigned int i, found; + + if(!PyDict_Check(dict)) + RAISE(PyExc_TypeError, "arg must be dict"); + while(PyDict_Next(dict, &pos, &d_key, &d_value)){ + if(!PyString_Check(d_key)) + RAISE(PyExc_TypeError, "key must be str"); + + PyGetInt(d_value, val); + + + found = 0; + for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){ + if (strcmp(PyString_AsString(d_key), gpreg_dict[i].name)) + continue; + *((uint32_t*)(((char*)&(self->vmcpu)) + gpreg_dict[i].offset)) = val; + found = 1; + break; + } + + if (found) + continue; + fprintf(stderr, "unkown key: %s\n", PyString_AsString(d_key)); + RAISE(PyExc_ValueError, "unkown reg"); + } + return NULL; +} + +PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) +{ + PyObject* dict; + if (!PyArg_ParseTuple(args, "O", &dict)) + return NULL; + _vm_set_gpreg(self, dict); + Py_INCREF(Py_None); + return Py_None; +} + + +PyObject* cpu_set_exception(JitCpu* self, PyObject* args) +{ + PyObject *item1; + uint64_t i; + + if (!PyArg_ParseTuple(args, "O", &item1)) + return NULL; + + PyGetInt(item1, i); + + self->vmcpu.exception_flags = i; + Py_INCREF(Py_None); + return Py_None; +} + +PyObject* cpu_get_exception(JitCpu* self, PyObject* args) +{ + return PyLong_FromUnsignedLongLong((uint32_t)self->vmcpu.exception_flags); +} + + +PyObject * cpu_init_regs(JitCpu* self) +{ + memset(&self->vmcpu, 0, sizeof(vm_cpu_t)); + + Py_INCREF(Py_None); + return Py_None; + +} + +void dump_gpregs(vm_cpu_t* vmcpu) +{ + + printf("ZR %.8"PRIX32" AT %.8"PRIX32" V0 %.8"PRIX32" V1 %.8"PRIX32" ", + vmcpu->ZERO, vmcpu->AT, vmcpu->V0, vmcpu->V1); + printf("A0 %.8"PRIX32" A1 %.8"PRIX32" A2 %.8"PRIX32" A3 %.8"PRIX32" ", + vmcpu->A0, vmcpu->A1, vmcpu->A2, vmcpu->A3); + printf("T0 %.8"PRIX32" T1 %.8"PRIX32" T2 %.8"PRIX32" T3 %.8"PRIX32" ", + vmcpu->T0, vmcpu->T1, vmcpu->T2, vmcpu->T3); + printf("T4 %.8"PRIX32" T5 %.8"PRIX32" T6 %.8"PRIX32" T7 %.8"PRIX32"\n", + vmcpu->T4, vmcpu->T5, vmcpu->T6, vmcpu->T7); + printf("S0 %.8"PRIX32" S1 %.8"PRIX32" S2 %.8"PRIX32" S3 %.8"PRIX32" ", + vmcpu->S0, vmcpu->S1, vmcpu->S2, vmcpu->S3); + printf("S4 %.8"PRIX32" S5 %.8"PRIX32" S6 %.8"PRIX32" S7 %.8"PRIX32" ", + vmcpu->S4, vmcpu->S5, vmcpu->S6, vmcpu->S7); + printf("T8 %.8"PRIX32" T9 %.8"PRIX32" K0 %.8"PRIX32" K1 %.8"PRIX32" ", + vmcpu->T8, vmcpu->T9, vmcpu->K0, vmcpu->K1); + printf("GP %.8"PRIX32" SP %.8"PRIX32" FP %.8"PRIX32" RA %.8"PRIX32"\n", + vmcpu->GP, vmcpu->SP, vmcpu->FP, vmcpu->RA); + printf("PC %.8"PRIX32"\n", + vmcpu->PC); +} + + +PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) +{ + vm_cpu_t* vmcpu; + + vmcpu = &self->vmcpu; + dump_gpregs(vmcpu); + Py_INCREF(Py_None); + return Py_None; +} + + + +static void +JitCpu_dealloc(JitCpu* self) +{ + self->ob_type->tp_free((PyObject*)self); +} + + +static PyObject * +JitCpu_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + JitCpu *self; + + self = (JitCpu *)type->tp_alloc(type, 0); + return (PyObject *)self; +} + +static PyObject * +JitCpu_get_cpu(JitCpu *self, void *closure) +{ + return PyLong_FromUnsignedLongLong((uint64_t)&(self->vmcpu)); +} + +static int +JitCpu_set_cpu(JitCpu *self, PyObject *value, void *closure) +{ + PyErr_SetString(PyExc_TypeError, "immutable cpu"); + return -1; +} + +static PyMemberDef JitCpu_members[] = { + {NULL} /* Sentinel */ +}; + +static PyMethodDef JitCpu_methods[] = { + {"vm_init_regs", (PyCFunction)cpu_init_regs, METH_NOARGS, + "X"}, + {"vm_dump_gpregs", (PyCFunction)cpu_dump_gpregs, METH_NOARGS, + "X"}, + {"vm_get_gpreg", (PyCFunction)cpu_get_gpreg, METH_NOARGS, + "X"}, + {"vm_set_gpreg", (PyCFunction)cpu_set_gpreg, METH_VARARGS, + "X"}, + {"vm_get_exception", (PyCFunction)cpu_get_exception, METH_VARARGS, + "X"}, + {"vm_set_exception", (PyCFunction)cpu_set_exception, METH_VARARGS, + "X"}, + {NULL} /* Sentinel */ +}; + +static int +JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds) +{ + return 0; +} + +getset_reg_u32(ZERO); +getset_reg_u32(AT); +getset_reg_u32(V0); +getset_reg_u32(V1); +getset_reg_u32(A0); +getset_reg_u32(A1); +getset_reg_u32(A2); +getset_reg_u32(A3); +getset_reg_u32(T0); +getset_reg_u32(T1); +getset_reg_u32(T2); +getset_reg_u32(T3); +getset_reg_u32(T4); +getset_reg_u32(T5); +getset_reg_u32(T6); +getset_reg_u32(T7); +getset_reg_u32(S0); +getset_reg_u32(S1); +getset_reg_u32(S2); +getset_reg_u32(S3); +getset_reg_u32(S4); +getset_reg_u32(S5); +getset_reg_u32(S6); +getset_reg_u32(S7); +getset_reg_u32(T8); +getset_reg_u32(T9); +getset_reg_u32(K0); +getset_reg_u32(K1); +getset_reg_u32(GP); +getset_reg_u32(SP); +getset_reg_u32(FP); +getset_reg_u32(RA); +getset_reg_u32(PC); + + + +#define get_reg_off(reg) do { \ + o = PyLong_FromUnsignedLongLong((uint64_t)offsetof(vm_cpu_t, reg)); \ + PyDict_SetItemString(dict, #reg, o); \ + Py_DECREF(o); \ + } while(0); + +PyObject* get_gpreg_offset_all(void) +{ + PyObject *dict = PyDict_New(); + PyObject *o; + + get_reg_off(exception_flags); + get_reg_off(exception_flags_new); + + + get_reg_off(ZERO); + get_reg_off(AT); + get_reg_off(V0); + get_reg_off(V1); + get_reg_off(A0); + get_reg_off(A1); + get_reg_off(A2); + get_reg_off(A3); + get_reg_off(T0); + get_reg_off(T1); + get_reg_off(T2); + get_reg_off(T3); + get_reg_off(T4); + get_reg_off(T5); + get_reg_off(T6); + get_reg_off(T7); + get_reg_off(S0); + get_reg_off(S1); + get_reg_off(S2); + get_reg_off(S3); + get_reg_off(S4); + get_reg_off(S5); + get_reg_off(S6); + get_reg_off(S7); + get_reg_off(T8); + get_reg_off(T9); + get_reg_off(K0); + get_reg_off(K1); + get_reg_off(GP); + get_reg_off(SP); + get_reg_off(FP); + get_reg_off(RA); + get_reg_off(PC); + + get_reg_off(ZERO_new); + get_reg_off(AT_new); + get_reg_off(V0_new); + get_reg_off(V1_new); + get_reg_off(A0_new); + get_reg_off(A1_new); + get_reg_off(A2_new); + get_reg_off(A3_new); + get_reg_off(T0_new); + get_reg_off(T1_new); + get_reg_off(T2_new); + get_reg_off(T3_new); + get_reg_off(T4_new); + get_reg_off(T5_new); + get_reg_off(T6_new); + get_reg_off(T7_new); + get_reg_off(S0_new); + get_reg_off(S1_new); + get_reg_off(S2_new); + get_reg_off(S3_new); + get_reg_off(S4_new); + get_reg_off(S5_new); + get_reg_off(S6_new); + get_reg_off(S7_new); + get_reg_off(T8_new); + get_reg_off(T9_new); + get_reg_off(K0_new); + get_reg_off(K1_new); + get_reg_off(GP_new); + get_reg_off(SP_new); + get_reg_off(FP_new); + get_reg_off(RA_new); + get_reg_off(PC_new); + + + + get_reg_off(pfmem08_0); + get_reg_off(pfmem08_1); + get_reg_off(pfmem08_2); + get_reg_off(pfmem08_3); + get_reg_off(pfmem08_4); + get_reg_off(pfmem08_5); + get_reg_off(pfmem08_6); + get_reg_off(pfmem08_7); + get_reg_off(pfmem08_8); + get_reg_off(pfmem08_9); + get_reg_off(pfmem08_10); + get_reg_off(pfmem08_11); + get_reg_off(pfmem08_12); + get_reg_off(pfmem08_13); + get_reg_off(pfmem08_14); + get_reg_off(pfmem08_15); + get_reg_off(pfmem08_16); + get_reg_off(pfmem08_17); + get_reg_off(pfmem08_18); + get_reg_off(pfmem08_19); + + + get_reg_off(pfmem16_0); + get_reg_off(pfmem16_1); + get_reg_off(pfmem16_2); + get_reg_off(pfmem16_3); + get_reg_off(pfmem16_4); + get_reg_off(pfmem16_5); + get_reg_off(pfmem16_6); + get_reg_off(pfmem16_7); + get_reg_off(pfmem16_8); + get_reg_off(pfmem16_9); + get_reg_off(pfmem16_10); + get_reg_off(pfmem16_11); + get_reg_off(pfmem16_12); + get_reg_off(pfmem16_13); + get_reg_off(pfmem16_14); + get_reg_off(pfmem16_15); + get_reg_off(pfmem16_16); + get_reg_off(pfmem16_17); + get_reg_off(pfmem16_18); + get_reg_off(pfmem16_19); + + + get_reg_off(pfmem32_0); + get_reg_off(pfmem32_1); + get_reg_off(pfmem32_2); + get_reg_off(pfmem32_3); + get_reg_off(pfmem32_4); + get_reg_off(pfmem32_5); + get_reg_off(pfmem32_6); + get_reg_off(pfmem32_7); + get_reg_off(pfmem32_8); + get_reg_off(pfmem32_9); + get_reg_off(pfmem32_10); + get_reg_off(pfmem32_11); + get_reg_off(pfmem32_12); + get_reg_off(pfmem32_13); + get_reg_off(pfmem32_14); + get_reg_off(pfmem32_15); + get_reg_off(pfmem32_16); + get_reg_off(pfmem32_17); + get_reg_off(pfmem32_18); + get_reg_off(pfmem32_19); + + + get_reg_off(pfmem64_0); + get_reg_off(pfmem64_1); + get_reg_off(pfmem64_2); + get_reg_off(pfmem64_3); + get_reg_off(pfmem64_4); + get_reg_off(pfmem64_5); + get_reg_off(pfmem64_6); + get_reg_off(pfmem64_7); + get_reg_off(pfmem64_8); + get_reg_off(pfmem64_9); + get_reg_off(pfmem64_10); + get_reg_off(pfmem64_11); + get_reg_off(pfmem64_12); + get_reg_off(pfmem64_13); + get_reg_off(pfmem64_14); + get_reg_off(pfmem64_15); + get_reg_off(pfmem64_16); + get_reg_off(pfmem64_17); + get_reg_off(pfmem64_18); + get_reg_off(pfmem64_19); + + return dict; +} + + +static PyGetSetDef JitCpu_getseters[] = { + {"cpu", + (getter)JitCpu_get_cpu, (setter)JitCpu_set_cpu, + "first name", + NULL}, + + {"ZERO" , (getter)JitCpu_get_ZERO , (setter)JitCpu_set_ZERO , "ZERO" , NULL}, + {"AT" , (getter)JitCpu_get_AT , (setter)JitCpu_set_AT , "AT" , NULL}, + {"V0" , (getter)JitCpu_get_V0 , (setter)JitCpu_set_V0 , "V0" , NULL}, + {"V1" , (getter)JitCpu_get_V1 , (setter)JitCpu_set_V1 , "V1" , NULL}, + {"A0" , (getter)JitCpu_get_A0 , (setter)JitCpu_set_A0 , "A0" , NULL}, + {"A1" , (getter)JitCpu_get_A1 , (setter)JitCpu_set_A1 , "A1" , NULL}, + {"A2" , (getter)JitCpu_get_A2 , (setter)JitCpu_set_A2 , "A2" , NULL}, + {"A3" , (getter)JitCpu_get_A3 , (setter)JitCpu_set_A3 , "A3" , NULL}, + {"T0" , (getter)JitCpu_get_T0 , (setter)JitCpu_set_T0 , "T0" , NULL}, + {"T1" , (getter)JitCpu_get_T1 , (setter)JitCpu_set_T1 , "T1" , NULL}, + {"T2" , (getter)JitCpu_get_T2 , (setter)JitCpu_set_T2 , "T2" , NULL}, + {"T3" , (getter)JitCpu_get_T3 , (setter)JitCpu_set_T3 , "T3" , NULL}, + {"T4" , (getter)JitCpu_get_T4 , (setter)JitCpu_set_T4 , "T4" , NULL}, + {"T5" , (getter)JitCpu_get_T5 , (setter)JitCpu_set_T5 , "T5" , NULL}, + {"T6" , (getter)JitCpu_get_T6 , (setter)JitCpu_set_T6 , "T6" , NULL}, + {"T7" , (getter)JitCpu_get_T7 , (setter)JitCpu_set_T7 , "T7" , NULL}, + {"S0" , (getter)JitCpu_get_S0 , (setter)JitCpu_set_S0 , "S0" , NULL}, + {"S1" , (getter)JitCpu_get_S1 , (setter)JitCpu_set_S1 , "S1" , NULL}, + {"S2" , (getter)JitCpu_get_S2 , (setter)JitCpu_set_S2 , "S2" , NULL}, + {"S3" , (getter)JitCpu_get_S3 , (setter)JitCpu_set_S3 , "S3" , NULL}, + {"S4" , (getter)JitCpu_get_S4 , (setter)JitCpu_set_S4 , "S4" , NULL}, + {"S5" , (getter)JitCpu_get_S5 , (setter)JitCpu_set_S5 , "S5" , NULL}, + {"S6" , (getter)JitCpu_get_S6 , (setter)JitCpu_set_S6 , "S6" , NULL}, + {"S7" , (getter)JitCpu_get_S7 , (setter)JitCpu_set_S7 , "S7" , NULL}, + {"T8" , (getter)JitCpu_get_T8 , (setter)JitCpu_set_T8 , "T8" , NULL}, + {"T9" , (getter)JitCpu_get_T9 , (setter)JitCpu_set_T9 , "T9" , NULL}, + {"K0" , (getter)JitCpu_get_K0 , (setter)JitCpu_set_K0 , "K0" , NULL}, + {"K1" , (getter)JitCpu_get_K1 , (setter)JitCpu_set_K1 , "K1" , NULL}, + {"GP" , (getter)JitCpu_get_GP , (setter)JitCpu_set_GP , "GP" , NULL}, + {"SP" , (getter)JitCpu_get_SP , (setter)JitCpu_set_SP , "SP" , NULL}, + {"FP" , (getter)JitCpu_get_FP , (setter)JitCpu_set_FP , "FP" , NULL}, + {"RA" , (getter)JitCpu_get_RA , (setter)JitCpu_set_RA , "RA" , NULL}, + {"PC" , (getter)JitCpu_get_PC , (setter)JitCpu_set_PC , "PC" , NULL}, + + {NULL} /* Sentinel */ +}; + + +static PyTypeObject JitCpuType = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "JitCore_mips32.JitCpu", /*tp_name*/ + sizeof(JitCpu), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)JitCpu_dealloc,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "JitCpu objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + JitCpu_methods, /* tp_methods */ + JitCpu_members, /* tp_members */ + JitCpu_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)JitCpu_init, /* tp_init */ + 0, /* tp_alloc */ + JitCpu_new, /* tp_new */ +}; + + + +static PyMethodDef JitCore_mips32_Methods[] = { + + /* + + */ + {"get_gpreg_offset_all", (PyCFunction)get_gpreg_offset_all, METH_NOARGS}, + {NULL, NULL, 0, NULL} /* Sentinel */ + +}; + +static PyObject *JitCore_mips32_Error; + +PyMODINIT_FUNC +initJitCore_mips32(void) +{ + PyObject *m; + + if (PyType_Ready(&JitCpuType) < 0) + return; + + m = Py_InitModule("JitCore_mips32", JitCore_mips32_Methods); + if (m == NULL) + return; + + JitCore_mips32_Error = PyErr_NewException("JitCore_mips32.error", NULL, NULL); + Py_INCREF(JitCore_mips32_Error); + PyModule_AddObject(m, "error", JitCore_mips32_Error); + + Py_INCREF(&JitCpuType); + PyModule_AddObject(m, "JitCpu", (PyObject *)&JitCpuType); + + /* init vm */ + init_vm_mngr(m); + +} + diff --git a/miasm2/jitter/arch/JitCore_mips32.h b/miasm2/jitter/arch/JitCore_mips32.h new file mode 100644 index 00000000..d8fe6f0a --- /dev/null +++ b/miasm2/jitter/arch/JitCore_mips32.h @@ -0,0 +1,171 @@ + +typedef struct { + uint32_t exception_flags; + uint32_t exception_flags_new; + + /* gpregs */ + + uint32_t ZERO; + uint32_t AT; + uint32_t V0; + uint32_t V1; + uint32_t A0; + uint32_t A1; + uint32_t A2; + uint32_t A3; + uint32_t T0; + uint32_t T1; + uint32_t T2; + uint32_t T3; + uint32_t T4; + uint32_t T5; + uint32_t T6; + uint32_t T7; + uint32_t S0; + uint32_t S1; + uint32_t S2; + uint32_t S3; + uint32_t S4; + uint32_t S5; + uint32_t S6; + uint32_t S7; + uint32_t T8; + uint32_t T9; + uint32_t K0; + uint32_t K1; + uint32_t GP; + uint32_t SP; + uint32_t FP; + uint32_t RA; + uint32_t PC; + + uint32_t ZERO_new; + uint32_t AT_new; + uint32_t V0_new; + uint32_t V1_new; + uint32_t A0_new; + uint32_t A1_new; + uint32_t A2_new; + uint32_t A3_new; + uint32_t T0_new; + uint32_t T1_new; + uint32_t T2_new; + uint32_t T3_new; + uint32_t T4_new; + uint32_t T5_new; + uint32_t T6_new; + uint32_t T7_new; + uint32_t S0_new; + uint32_t S1_new; + uint32_t S2_new; + uint32_t S3_new; + uint32_t S4_new; + uint32_t S5_new; + uint32_t S6_new; + uint32_t S7_new; + uint32_t T8_new; + uint32_t T9_new; + uint32_t K0_new; + uint32_t K1_new; + uint32_t GP_new; + uint32_t SP_new; + uint32_t FP_new; + uint32_t RA_new; + uint32_t PC_new; + + + + uint8_t pfmem08_0; + uint8_t pfmem08_1; + uint8_t pfmem08_2; + uint8_t pfmem08_3; + uint8_t pfmem08_4; + uint8_t pfmem08_5; + uint8_t pfmem08_6; + uint8_t pfmem08_7; + uint8_t pfmem08_8; + uint8_t pfmem08_9; + uint8_t pfmem08_10; + uint8_t pfmem08_11; + uint8_t pfmem08_12; + uint8_t pfmem08_13; + uint8_t pfmem08_14; + uint8_t pfmem08_15; + uint8_t pfmem08_16; + uint8_t pfmem08_17; + uint8_t pfmem08_18; + uint8_t pfmem08_19; + + + uint16_t pfmem16_0; + uint16_t pfmem16_1; + uint16_t pfmem16_2; + uint16_t pfmem16_3; + uint16_t pfmem16_4; + uint16_t pfmem16_5; + uint16_t pfmem16_6; + uint16_t pfmem16_7; + uint16_t pfmem16_8; + uint16_t pfmem16_9; + uint16_t pfmem16_10; + uint16_t pfmem16_11; + uint16_t pfmem16_12; + uint16_t pfmem16_13; + uint16_t pfmem16_14; + uint16_t pfmem16_15; + uint16_t pfmem16_16; + uint16_t pfmem16_17; + uint16_t pfmem16_18; + uint16_t pfmem16_19; + + + uint32_t pfmem32_0; + uint32_t pfmem32_1; + uint32_t pfmem32_2; + uint32_t pfmem32_3; + uint32_t pfmem32_4; + uint32_t pfmem32_5; + uint32_t pfmem32_6; + uint32_t pfmem32_7; + uint32_t pfmem32_8; + uint32_t pfmem32_9; + uint32_t pfmem32_10; + uint32_t pfmem32_11; + uint32_t pfmem32_12; + uint32_t pfmem32_13; + uint32_t pfmem32_14; + uint32_t pfmem32_15; + uint32_t pfmem32_16; + uint32_t pfmem32_17; + uint32_t pfmem32_18; + uint32_t pfmem32_19; + + + uint64_t pfmem64_0; + uint64_t pfmem64_1; + uint64_t pfmem64_2; + uint64_t pfmem64_3; + uint64_t pfmem64_4; + uint64_t pfmem64_5; + uint64_t pfmem64_6; + uint64_t pfmem64_7; + uint64_t pfmem64_8; + uint64_t pfmem64_9; + uint64_t pfmem64_10; + uint64_t pfmem64_11; + uint64_t pfmem64_12; + uint64_t pfmem64_13; + uint64_t pfmem64_14; + uint64_t pfmem64_15; + uint64_t pfmem64_16; + uint64_t pfmem64_17; + uint64_t pfmem64_18; + uint64_t pfmem64_19; + + + +}vm_cpu_t; + + + +#define RETURN_PC return PyLong_FromUnsignedLongLong(vmcpu->PC); diff --git a/miasm2/jitter/arch/JitCore_msp430.c b/miasm2/jitter/arch/JitCore_msp430.c index e5f7ffab..7dc2c935 100644 --- a/miasm2/jitter/arch/JitCore_msp430.c +++ b/miasm2/jitter/arch/JitCore_msp430.c @@ -107,7 +107,7 @@ typedef struct { -PyObject* vm_get_gpreg(JitCpu* self) +PyObject* cpu_get_gpreg(JitCpu* self) { PyObject *dict = PyDict_New(); PyObject *o; @@ -325,7 +325,7 @@ PyObject* _vm_set_gpreg(JitCpu* self, PyObject *dict) } return NULL; } - +/* uint8_t const bcd2bin_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0, 0, 0, 0, 0, 0, @@ -418,9 +418,9 @@ uint16_t bcd2hex_16(uint16_t a) { return bcd2bin_data[a % 100] | (bcd2bin_data[(a / 100)] << 8); } +*/ - -PyObject* vm_set_gpreg(JitCpu* self, PyObject *args) +PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) { PyObject* dict; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -431,7 +431,7 @@ PyObject* vm_set_gpreg(JitCpu* self, PyObject *args) } -PyObject* vm_set_exception(JitCpu* self, PyObject* args) +PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; uint64_t i; @@ -446,13 +446,13 @@ PyObject* vm_set_exception(JitCpu* self, PyObject* args) return Py_None; } -PyObject* vm_get_exception(JitCpu* self, PyObject* args) +PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { return PyLong_FromUnsignedLongLong((uint64_t)self->vmcpu.exception_flags); } -PyObject * vm_init_regs(JitCpu* self) +PyObject * cpu_init_regs(JitCpu* self) { memset(&self->vmcpu, 0, sizeof(vm_cpu_t)); @@ -477,7 +477,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) } -PyObject * vm_dump_gpregs(JitCpu* self, PyObject* args) +PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { vm_cpu_t* vmcpu; @@ -523,17 +523,17 @@ static PyMemberDef JitCpu_members[] = { }; static PyMethodDef JitCpu_methods[] = { - {"vm_init_regs", (PyCFunction)vm_init_regs, METH_NOARGS, + {"vm_init_regs", (PyCFunction)cpu_init_regs, METH_NOARGS, "X"}, - {"vm_dump_gpregs", (PyCFunction)vm_dump_gpregs, METH_NOARGS, + {"vm_dump_gpregs", (PyCFunction)cpu_dump_gpregs, METH_NOARGS, "X"}, - {"vm_get_gpreg", (PyCFunction)vm_get_gpreg, METH_NOARGS, + {"vm_get_gpreg", (PyCFunction)cpu_get_gpreg, METH_NOARGS, "X"}, - {"vm_set_gpreg", (PyCFunction)vm_set_gpreg, METH_VARARGS, + {"vm_set_gpreg", (PyCFunction)cpu_set_gpreg, METH_VARARGS, "X"}, - {"vm_get_exception", (PyCFunction)vm_get_exception, METH_VARARGS, + {"vm_get_exception", (PyCFunction)cpu_get_exception, METH_VARARGS, "X"}, - {"vm_set_exception", (PyCFunction)vm_set_exception, METH_VARARGS, + {"vm_set_exception", (PyCFunction)cpu_set_exception, METH_VARARGS, "X"}, {NULL} /* Sentinel */ }; @@ -686,5 +686,8 @@ initJitCore_msp430(void) Py_INCREF(&JitCpuType); PyModule_AddObject(m, "JitCpu", (PyObject *)&JitCpuType); + /* init vm */ + init_vm_mngr(m); + } diff --git a/miasm2/jitter/arch/JitCore_x86.c b/miasm2/jitter/arch/JitCore_x86.c index a09e39b1..bd5f57a8 100644 --- a/miasm2/jitter/arch/JitCore_x86.c +++ b/miasm2/jitter/arch/JitCore_x86.c @@ -72,7 +72,7 @@ typedef struct { } while(0); -PyObject* vm_get_gpreg(JitCpu* self) +PyObject* cpu_get_gpreg(JitCpu* self) { PyObject *dict = PyDict_New(); PyObject *o; @@ -151,7 +151,7 @@ PyObject* _vm_set_gpreg(JitCpu* self, PyObject *dict) return NULL; } -PyObject* vm_set_gpreg(JitCpu* self, PyObject *args) +PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args) { PyObject* dict; if (!PyArg_ParseTuple(args, "O", &dict)) @@ -162,7 +162,7 @@ PyObject* vm_set_gpreg(JitCpu* self, PyObject *args) } -PyObject * vm_init_regs(JitCpu* self) +PyObject * cpu_init_regs(JitCpu* self) { memset(&self->vmcpu, 0, sizeof(vm_cpu_t)); @@ -189,7 +189,7 @@ void dump_gpregs(vm_cpu_t* vmcpu) } -PyObject * vm_dump_gpregs(JitCpu* self, PyObject* args) +PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args) { vm_cpu_t* vmcpu; @@ -200,7 +200,7 @@ PyObject * vm_dump_gpregs(JitCpu* self, PyObject* args) } -PyObject* vm_set_segm_base(JitCpu* self, PyObject* args) +PyObject* cpu_set_segm_base(JitCpu* self, PyObject* args) { PyObject *item1, *item2; uint64_t segm_num, segm_base; @@ -216,7 +216,7 @@ PyObject* vm_set_segm_base(JitCpu* self, PyObject* args) return Py_None; } -PyObject* vm_get_segm_base(JitCpu* self, PyObject* args) +PyObject* cpu_get_segm_base(JitCpu* self, PyObject* args) { PyObject *item1; uint64_t segm_num; @@ -235,7 +235,7 @@ uint64_t segm2addr(vm_cpu_t* vmcpu, uint64_t segm, uint64_t addr) } -PyObject* vm_set_exception(JitCpu* self, PyObject* args) +PyObject* cpu_set_exception(JitCpu* self, PyObject* args) { PyObject *item1; uint64_t i; @@ -250,7 +250,7 @@ PyObject* vm_set_exception(JitCpu* self, PyObject* args) return Py_None; } -PyObject* vm_get_exception(JitCpu* self, PyObject* args) +PyObject* cpu_get_exception(JitCpu* self, PyObject* args) { return PyLong_FromUnsignedLongLong((uint64_t)self->vmcpu.exception_flags); } @@ -362,21 +362,21 @@ static PyMemberDef JitCpu_members[] = { }; static PyMethodDef JitCpu_methods[] = { - {"vm_init_regs", (PyCFunction)vm_init_regs, METH_NOARGS, + {"vm_init_regs", (PyCFunction)cpu_init_regs, METH_NOARGS, "X"}, - {"vm_dump_gpregs", (PyCFunction)vm_dump_gpregs, METH_NOARGS, + {"vm_dump_gpregs", (PyCFunction)cpu_dump_gpregs, METH_NOARGS, "X"}, - {"vm_get_gpreg", (PyCFunction)vm_get_gpreg, METH_NOARGS, + {"vm_get_gpreg", (PyCFunction)cpu_get_gpreg, METH_NOARGS, "X"}, - {"vm_set_gpreg", (PyCFunction)vm_set_gpreg, METH_VARARGS, + {"vm_set_gpreg", (PyCFunction)cpu_set_gpreg, METH_VARARGS, "X"}, - {"vm_get_segm_base", (PyCFunction)vm_get_segm_base, METH_VARARGS, + {"vm_get_segm_base", (PyCFunction)cpu_get_segm_base, METH_VARARGS, "X"}, - {"vm_set_segm_base", (PyCFunction)vm_set_segm_base, METH_VARARGS, + {"vm_set_segm_base", (PyCFunction)cpu_set_segm_base, METH_VARARGS, "X"}, - {"vm_get_exception", (PyCFunction)vm_get_exception, METH_VARARGS, + {"vm_get_exception", (PyCFunction)cpu_get_exception, METH_VARARGS, "X"}, - {"vm_set_exception", (PyCFunction)vm_set_exception, METH_VARARGS, + {"vm_set_exception", (PyCFunction)cpu_set_exception, METH_VARARGS, "X"}, {NULL} /* Sentinel */ }; @@ -815,6 +815,7 @@ static PyMethodDef JitCore_x86_Methods[] = { static PyObject *JitCore_x86_Error; +extern int init_vm_mngr(PyObject* m); PyMODINIT_FUNC initJitCore_x86(void) { @@ -834,6 +835,9 @@ initJitCore_x86(void) Py_INCREF(&JitCpuType); PyModule_AddObject(m, "JitCpu", (PyObject *)&JitCpuType); + /* init vm */ + init_vm_mngr(m); + } diff --git a/miasm2/jitter/jitcore_llvm.py b/miasm2/jitter/jitcore_llvm.py index 712b8a2f..03bfb90b 100644 --- a/miasm2/jitter/jitcore_llvm.py +++ b/miasm2/jitter/jitcore_llvm.py @@ -14,8 +14,10 @@ class JitCore_LLVM(jitcore.JitCore): "JiT management, using LLVM as backend" # Architecture dependant libraries - arch_dependent_libs = {"x86": "arch/JitCore_x86.so", - "arm": "arch/JitCore_arm.so"} + arch_dependent_libs = {"x86": "JitCore_x86.so", + "arm": "JitCore_arm.so", + "msp430": "JitCore_msp430.so", + "mips32": "JitCore_mips32.so"} def __init__(self, my_ir, bs=None): super(JitCore_LLVM, self).__init__(my_ir, bs) @@ -35,12 +37,9 @@ class JitCore_LLVM(jitcore.JitCore): # Library to load within Jit context libs_to_load = [] - # Get the vm_mngr librairy - lib_dir = os.path.dirname(os.path.realpath(__file__)) - vm_mngr_path = os.path.join(lib_dir, 'vm_mngr.so') - libs_to_load.append(vm_mngr_path) - # Get architecture dependant Jitcore library (if any) + lib_dir = os.path.dirname(os.path.realpath(__file__)) + lib_dir = os.path.join(lib_dir, 'arch') try: jit_lib = os.path.join( lib_dir, self.arch_dependent_libs[arch.name]) diff --git a/miasm2/jitter/jitcore_tcc.py b/miasm2/jitter/jitcore_tcc.py index ee33bcd0..856bffc0 100644 --- a/miasm2/jitter/jitcore_tcc.py +++ b/miasm2/jitter/jitcore_tcc.py @@ -99,7 +99,6 @@ class JitCore_Tcc(jitcore.JitCore): # os.path.join(os.path.dirname(os.path.realpath(__file__)), "jitter") lib_dir = os.path.dirname(os.path.realpath(__file__)) libs = [] - libs.append(os.path.join(lib_dir, 'vm_mngr.so')) libs.append(os.path.join(lib_dir, 'arch/JitCore_%s.so' % (arch.name))) libs = ';'.join(libs) jittcc_path = Jittcc.__file__ diff --git a/miasm2/jitter/jitload.py b/miasm2/jitter/jitload.py index 959c9d4a..591d7a6b 100644 --- a/miasm2/jitter/jitload.py +++ b/miasm2/jitter/jitload.py @@ -7,8 +7,6 @@ import struct from elfesteem import pe from elfesteem import cstruct from elfesteem import * -from vm_mngr import * -from vm_mngr import VmMngr from csts import * from miasm2.core.utils import * @@ -28,14 +26,6 @@ hnd.setFormatter(logging.Formatter("[%(levelname)s]: %(message)s")) log.addHandler(hnd) log.setLevel(logging.CRITICAL) -""" -name2jit = {'x86':JitCore_x86, - 'arm':JitCore_arm, - 'msp430':JitCore_msp430, - } -""" - - def whoami(): return inspect.stack()[2][3] @@ -430,25 +420,6 @@ def vm_load_elf(vm, fname, **kargs): data += (((len(data) + 0xFFF) & ~0xFFF) - len(data)) * "\x00" vm.vm_add_memory_page(r_vaddr, PAGE_READ | PAGE_WRITE, data) return e -""" -def init_jitter(arch, attrib): - jitarch = name2jit[(arch.name, attrib)] - jitarch.vm_init_regs() - init_memory_page_pool() - init_code_bloc_pool() - init_memory_breakpoint() - jit_tcc_init(arch, attrib) - -def init_stack(arch, attrib, stack_size = 0x10000, stack_base = 0x1230000, **kargs): - jitarch = name2jit[(arch.name, attrib)] - - vm_add_memory_page(stack_base, PAGE_READ|PAGE_WRITE, "\x00"*stack_size) - regs = jitarch.vm_get_gpreg() - regs[arch.sp[attrib].name] = stack_base+stack_size - jitarch.vm_set_gpreg(regs) - regs = jitarch.vm_get_gpreg() -""" - def vm_load_pe_lib(fname_in, libs, lib_path_base, patch_vm_imp, **kargs): fname = os.path.join(lib_path_base, fname_in) @@ -588,11 +559,13 @@ class jitter: from arch import JitCore_arm as jcore elif arch_name == "msp430": from arch import JitCore_msp430 as jcore + elif arch_name == "mips32": + from arch import JitCore_mips32 as jcore else: raise ValueError("unsupported jit arch!") self.cpu = jcore.JitCpu() - self.vm = VmMngr() + self.vm = jcore.VmMngr() self.bs = bin_stream_vm(self.vm) self.my_ir = my_ir init_arch_C(self.arch) diff --git a/miasm2/jitter/vm_mngr_py.c b/miasm2/jitter/vm_mngr_py.c index a65ea549..18f2fd51 100644 --- a/miasm2/jitter/vm_mngr_py.c +++ b/miasm2/jitter/vm_mngr_py.c @@ -934,7 +934,7 @@ static PyMethodDef Vm_Mngr_Methods[] = { }; - +/* PyMODINIT_FUNC initvm_mngr(void) { @@ -955,4 +955,23 @@ initvm_mngr(void) PyModule_AddObject(m, "VmMngr", (PyObject *)&VmMngrType); } +*/ + +/* + return + 0 on success + -1 on error +*/ +int init_vm_mngr(PyObject* m) +{ + if (PyType_Ready(&VmMngrType) < 0) + return -1; + Vm_Mngr_Error = PyErr_NewException("vm_mngr_.error", NULL, NULL); + Py_INCREF(Vm_Mngr_Error); + PyModule_AddObject(m, "error", Vm_Mngr_Error); + + Py_INCREF(&VmMngrType); + PyModule_AddObject(m, "VmMngr", (PyObject *)&VmMngrType); + +} diff --git a/setup.py b/setup.py index fcc8f7a6..05e247ec 100755 --- a/setup.py +++ b/setup.py @@ -21,31 +21,44 @@ def buil_all(): 'miasm2/jitter/arch', 'miasm2/jitter/os_dep', ] - ext_modules_no_tcc = [ - Extension("miasm2.jitter.vm_mngr", - ["miasm2/jitter/vm_mngr.c", - "miasm2/jitter/vm_mngr_py.c"]), Extension("miasm2.jitter.arch.JitCore_x86", - ["miasm2/jitter/arch/JitCore_x86.c"]), + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_x86.c"]), Extension("miasm2.jitter.arch.JitCore_arm", - ["miasm2/jitter/arch/JitCore_arm.c"]), + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_arm.c"]), Extension("miasm2.jitter.arch.JitCore_msp430", - ["miasm2/jitter/arch/JitCore_msp430.c"]), + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_msp430.c"]), + Extension("miasm2.jitter.arch.JitCore_mips32", + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_mips32.c"]), Extension("miasm2.jitter.Jitllvm", ["miasm2/jitter/Jitllvm.c"]), ] ext_modules_all = [ - Extension("miasm2.jitter.vm_mngr", - ["miasm2/jitter/vm_mngr.c", - "miasm2/jitter/vm_mngr_py.c"]), Extension("miasm2.jitter.arch.JitCore_x86", - ["miasm2/jitter/arch/JitCore_x86.c"]), + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_x86.c"]), Extension("miasm2.jitter.arch.JitCore_arm", - ["miasm2/jitter/arch/JitCore_arm.c"]), + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_arm.c"]), Extension("miasm2.jitter.arch.JitCore_msp430", - ["miasm2/jitter/arch/JitCore_msp430.c"]), + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_msp430.c"]), + Extension("miasm2.jitter.arch.JitCore_mips32", + ["miasm2/jitter/vm_mngr.c", + "miasm2/jitter/vm_mngr_py.c", + "miasm2/jitter/arch/JitCore_mips32.c"]), Extension("miasm2.jitter.Jitllvm", ["miasm2/jitter/Jitllvm.c"]), Extension("miasm2.jitter.Jittcc", @@ -53,7 +66,6 @@ def buil_all(): libraries=["tcc"]) ] - print 'building' build_ok = False for name, ext_modules in [('all', ext_modules_all), diff --git a/test/arch/mips32/arch.py b/test/arch/mips32/arch.py index 2ec6d2a2..fadffec5 100644 --- a/test/arch/mips32/arch.py +++ b/test/arch/mips32/arch.py @@ -231,7 +231,7 @@ for s, l in reg_tests_mips32: assert(str(mn) == s) # print hex(b) # print [str(x.get()) for x in mn.args] - l = mn_mips32.fromstring(s) + l = mn_mips32.fromstring(s, 'b') # print l assert(str(l) == s) a = mn_mips32.asm(l, 'b') |