about summary refs log tree commit diff stats
path: root/miasm/jitter/arch/JitCore_arm.c
diff options
context:
space:
mode:
Diffstat (limited to 'miasm/jitter/arch/JitCore_arm.c')
-rw-r--r--miasm/jitter/arch/JitCore_arm.c133
1 files changed, 58 insertions, 75 deletions
diff --git a/miasm/jitter/arch/JitCore_arm.c b/miasm/jitter/arch/JitCore_arm.c
index 64f30cf4..13dcb3f4 100644
--- a/miasm/jitter/arch/JitCore_arm.c
+++ b/miasm/jitter/arch/JitCore_arm.c
@@ -14,35 +14,35 @@
 
 
 reg_dict gpreg_dict[] = {
-			 {.name = "R0", .offset = offsetof(vm_cpu_t, R0), .size = 32},
-			 {.name = "R1", .offset = offsetof(vm_cpu_t, R1), .size = 32},
-			 {.name = "R2", .offset = offsetof(vm_cpu_t, R2), .size = 32},
-			 {.name = "R3", .offset = offsetof(vm_cpu_t, R3), .size = 32},
-			 {.name = "R4", .offset = offsetof(vm_cpu_t, R4), .size = 32},
-			 {.name = "R5", .offset = offsetof(vm_cpu_t, R5), .size = 32},
-			 {.name = "R6", .offset = offsetof(vm_cpu_t, R6), .size = 32},
-			 {.name = "R7", .offset = offsetof(vm_cpu_t, R7), .size = 32},
-			 {.name = "R8", .offset = offsetof(vm_cpu_t, R8), .size = 32},
-			 {.name = "R9", .offset = offsetof(vm_cpu_t, R9), .size = 32},
-			 {.name = "R10", .offset = offsetof(vm_cpu_t, R10), .size = 32},
-			 {.name = "R11", .offset = offsetof(vm_cpu_t, R11), .size = 32},
-			 {.name = "R12", .offset = offsetof(vm_cpu_t, R12), .size = 32},
-			 {.name = "SP", .offset = offsetof(vm_cpu_t, SP), .size = 32},
-			 {.name = "LR", .offset = offsetof(vm_cpu_t, LR), .size = 32},
-			 {.name = "PC", .offset = offsetof(vm_cpu_t, PC), .size = 32},
-
-			 {.name = "zf", .offset = offsetof(vm_cpu_t, zf), .size = 8},
-			 {.name = "nf", .offset = offsetof(vm_cpu_t, nf), .size = 8},
-			 {.name = "of", .offset = offsetof(vm_cpu_t, of), .size = 8},
-			 {.name = "cf", .offset = offsetof(vm_cpu_t, cf), .size = 8},
-
-			 {.name = "ge0", .offset = offsetof(vm_cpu_t, ge0), .size = 8},
-			 {.name = "ge1", .offset = offsetof(vm_cpu_t, ge1), .size = 8},
-			 {.name = "ge2", .offset = offsetof(vm_cpu_t, ge2), .size = 8},
-			 {.name = "ge3", .offset = offsetof(vm_cpu_t, ge3), .size = 8},
-
-			 {.name = "exception_flags", .offset = offsetof(vm_cpu_t, exception_flags), .size = 32},
-			 {.name = "interrupt_num", .offset = offsetof(vm_cpu_t, interrupt_num), .size = 32},
+			 {.name = "R0", .offset = offsetof(struct vm_cpu, R0), .size = 32},
+			 {.name = "R1", .offset = offsetof(struct vm_cpu, R1), .size = 32},
+			 {.name = "R2", .offset = offsetof(struct vm_cpu, R2), .size = 32},
+			 {.name = "R3", .offset = offsetof(struct vm_cpu, R3), .size = 32},
+			 {.name = "R4", .offset = offsetof(struct vm_cpu, R4), .size = 32},
+			 {.name = "R5", .offset = offsetof(struct vm_cpu, R5), .size = 32},
+			 {.name = "R6", .offset = offsetof(struct vm_cpu, R6), .size = 32},
+			 {.name = "R7", .offset = offsetof(struct vm_cpu, R7), .size = 32},
+			 {.name = "R8", .offset = offsetof(struct vm_cpu, R8), .size = 32},
+			 {.name = "R9", .offset = offsetof(struct vm_cpu, R9), .size = 32},
+			 {.name = "R10", .offset = offsetof(struct vm_cpu, R10), .size = 32},
+			 {.name = "R11", .offset = offsetof(struct vm_cpu, R11), .size = 32},
+			 {.name = "R12", .offset = offsetof(struct vm_cpu, R12), .size = 32},
+			 {.name = "SP", .offset = offsetof(struct vm_cpu, SP), .size = 32},
+			 {.name = "LR", .offset = offsetof(struct vm_cpu, LR), .size = 32},
+			 {.name = "PC", .offset = offsetof(struct vm_cpu, PC), .size = 32},
+
+			 {.name = "zf", .offset = offsetof(struct vm_cpu, zf), .size = 8},
+			 {.name = "nf", .offset = offsetof(struct vm_cpu, nf), .size = 8},
+			 {.name = "of", .offset = offsetof(struct vm_cpu, of), .size = 8},
+			 {.name = "cf", .offset = offsetof(struct vm_cpu, cf), .size = 8},
+
+			 {.name = "ge0", .offset = offsetof(struct vm_cpu, ge0), .size = 8},
+			 {.name = "ge1", .offset = offsetof(struct vm_cpu, ge1), .size = 8},
+			 {.name = "ge2", .offset = offsetof(struct vm_cpu, ge2), .size = 8},
+			 {.name = "ge3", .offset = offsetof(struct vm_cpu, ge3), .size = 8},
+
+			 {.name = "exception_flags", .offset = offsetof(struct vm_cpu, exception_flags), .size = 32},
+			 {.name = "interrupt_num", .offset = offsetof(struct vm_cpu, interrupt_num), .size = 32},
 };
 
 /************************** JitCpu object **************************/
@@ -92,8 +92,8 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
     PyObject* dict;
     PyObject *d_key, *d_value = NULL;
     Py_ssize_t pos = 0;
-    char* d_key_name;
-    uint64_t val;
+    const char *d_key_name;
+    uint32_t val;
     unsigned int i, found;
 
     if (!PyArg_ParseTuple(args, "O", &dict))
@@ -102,7 +102,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
 	    RAISE(PyExc_TypeError, "arg must be dict");
     while(PyDict_Next(dict, &pos, &d_key, &d_value)){
 	    PyGetStr(d_key_name, d_key);
-	    PyGetInt(d_value, val);
+	    PyGetInt_uint32_t(d_value, val);
 
 	    found = 0;
 	    for (i=0; i < sizeof(gpreg_dict)/sizeof(reg_dict); i++){
@@ -115,7 +115,7 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
 
 	    if (found)
 		    continue;
-	    fprintf(stderr, "unknown key: %s\n", d_key);
+	    fprintf(stderr, "unknown key: %s\n", d_key_name);
 	    RAISE(PyExc_ValueError, "unknown reg");
     }
     Py_INCREF(Py_None);
@@ -125,13 +125,13 @@ PyObject* cpu_set_gpreg(JitCpu* self, PyObject *args)
 
 PyObject * cpu_init_regs(JitCpu* self)
 {
-	memset(self->cpu, 0, sizeof(vm_cpu_t));
+	memset(self->cpu, 0, sizeof(struct vm_cpu));
 
 	Py_INCREF(Py_None);
 	return Py_None;
 }
 
-void dump_gpregs(vm_cpu_t* vmcpu)
+void dump_gpregs(struct vm_cpu* vmcpu)
 {
 	printf("R0  %.8"PRIX32" R1  %.8"PRIX32" R2  %.8"PRIX32" R3  %.8"PRIX32" ",
 	       vmcpu->R0, vmcpu->R1, vmcpu->R2, vmcpu->R3);
@@ -148,7 +148,7 @@ void dump_gpregs(vm_cpu_t* vmcpu)
 
 PyObject * cpu_dump_gpregs(JitCpu* self, PyObject* args)
 {
-	vm_cpu_t* vmcpu;
+	struct vm_cpu* vmcpu;
 
 	vmcpu = self->cpu;
 	dump_gpregs(vmcpu);
@@ -167,60 +167,41 @@ PyObject * cpu_dump_gpregs_with_attrib(JitCpu* self, PyObject* args)
 PyObject* cpu_set_exception(JitCpu* self, PyObject* args)
 {
 	PyObject *item1;
-	uint64_t i;
+	uint32_t exception_flags;
 
 	if (!PyArg_ParseTuple(args, "O", &item1))
 		RAISE(PyExc_TypeError,"Cannot parse arguments");
 
-	PyGetInt(item1, i);
+	PyGetInt_uint32_t(item1, exception_flags);
 
-	((vm_cpu_t*)self->cpu)->exception_flags = i;
+	((struct vm_cpu*)self->cpu)->exception_flags = exception_flags;
 	Py_INCREF(Py_None);
 	return Py_None;
 }
 
 PyObject* cpu_get_exception(JitCpu* self, PyObject* args)
 {
-	return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->exception_flags));
-}
-
-
-
-
-
-void check_automod(JitCpu* jitcpu, uint64_t addr, uint64_t size)
-{
-	PyObject *result;
-
-	if (!(((VmMngr*)jitcpu->pyvm)->vm_mngr.exception_flags & EXCEPT_CODE_AUTOMOD))
-		return;
-	result = PyObject_CallMethod(jitcpu->jitter, "automod_cb", "LL", addr, size);
-	Py_DECREF(result);
-
+	return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->exception_flags));
 }
 
 void MEM_WRITE_08(JitCpu* jitcpu, uint64_t addr, uint8_t src)
 {
 	vm_MEM_WRITE_08(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src);
-	check_automod(jitcpu, addr, 8);
 }
 
 void MEM_WRITE_16(JitCpu* jitcpu, uint64_t addr, uint16_t src)
 {
 	vm_MEM_WRITE_16(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src);
-	check_automod(jitcpu, addr, 16);
 }
 
 void MEM_WRITE_32(JitCpu* jitcpu, uint64_t addr, uint32_t src)
 {
 	vm_MEM_WRITE_32(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src);
-	check_automod(jitcpu, addr, 32);
 }
 
 void MEM_WRITE_64(JitCpu* jitcpu, uint64_t addr, uint64_t src)
 {
 	vm_MEM_WRITE_64(&((VmMngr*)jitcpu->pyvm)->vm_mngr, addr, src);
-	check_automod(jitcpu, addr, 64);
 }
 
 PyObject* vm_set_mem(JitCpu *self, PyObject* args)
@@ -230,25 +211,27 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args)
        Py_ssize_t py_length;
 
        char * buffer;
-       uint64_t size;
+       Py_ssize_t pysize;
        uint64_t addr;
        int ret;
 
        if (!PyArg_ParseTuple(args, "OO", &py_addr, &py_buffer))
 	       RAISE(PyExc_TypeError,"Cannot parse arguments");
 
-       PyGetInt(py_addr, addr);
+       PyGetInt_uint64_t(py_addr, addr);
 
        if(!PyBytes_Check(py_buffer))
 	       RAISE(PyExc_TypeError,"arg must be bytes");
 
-       size = PyBytes_Size(py_buffer);
+       pysize = PyBytes_Size(py_buffer);
+       if (pysize < 0) {
+	       RAISE(PyExc_TypeError,"Python error");
+       }
        PyBytes_AsStringAndSize(py_buffer, &buffer, &py_length);
 
-       ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, size);
+       ret = vm_write_mem(&(((VmMngr*)self->pyvm)->vm_mngr), addr, buffer, pysize);
        if (ret < 0)
 	       RAISE(PyExc_TypeError,"arg must be str");
-       check_automod(self, addr, size*8);
 
        Py_INCREF(Py_None);
        return Py_None;
@@ -257,21 +240,21 @@ PyObject* vm_set_mem(JitCpu *self, PyObject* args)
 PyObject* cpu_set_interrupt_num(JitCpu* self, PyObject* args)
 {
 	PyObject *item1;
-	uint64_t i;
+	uint32_t exception_flags;
 
 	if (!PyArg_ParseTuple(args, "O", &item1))
 		RAISE(PyExc_TypeError,"Cannot parse arguments");
 
-	PyGetInt(item1, i);
+	PyGetInt_uint32_t(item1, exception_flags);
 
-	((vm_cpu_t*)self->cpu)->interrupt_num = i;
+	((struct vm_cpu*)self->cpu)->interrupt_num = exception_flags;
 	Py_INCREF(Py_None);
 	return Py_None;
 }
 
 PyObject* cpu_get_interrupt_num(JitCpu* self, PyObject* args)
 {
-	return PyLong_FromUnsignedLongLong((uint64_t)(((vm_cpu_t*)self->cpu)->interrupt_num));
+	return PyLong_FromUnsignedLongLong((uint64_t)(((struct vm_cpu*)self->cpu)->interrupt_num));
 }
 
 static PyMemberDef JitCpu_members[] = {
@@ -307,9 +290,9 @@ static PyMethodDef JitCpu_methods[] = {
 static int
 JitCpu_init(JitCpu *self, PyObject *args, PyObject *kwds)
 {
-	self->cpu = malloc(sizeof(vm_cpu_t));
+	self->cpu = malloc(sizeof(struct vm_cpu));
 	if (self->cpu == NULL) {
-		fprintf(stderr, "cannot alloc vm_cpu_t\n");
+		fprintf(stderr, "cannot alloc struct vm_cpu\n");
 		exit(EXIT_FAILURE);
 	}
 	return 0;
@@ -488,20 +471,20 @@ static PyMethodDef JitCore_arm_Methods[] = {
 
 MOD_INIT(JitCore_arm)
 {
-	PyObject *module;
+	PyObject *module = NULL;
 
 	MOD_DEF(module, "JitCore_arm", "JitCore_arm module", JitCore_arm_Methods);
 
 	if (module == NULL)
-		return NULL;
+		RET_MODULE;
 
 	if (PyType_Ready(&JitCpuType) < 0)
-		return NULL;
+		RET_MODULE;
 
 	Py_INCREF(&JitCpuType);
 	if (PyModule_AddObject(module, "JitCpu", (PyObject *)&JitCpuType) < 0)
-		return NULL;
+		RET_MODULE;
 
-	return module;
+	RET_MODULE;
 }