forked from AeonLucid/AndroidNativeEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SVC 0 support, implemented few syscalls and VFP support.
- Loading branch information
Showing
21 changed files
with
429 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PR_SET_VMA = 0x53564d41 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CLOCK_REALTIME = 0 | ||
CLOCK_MONOTONIC = 1 | ||
CLOCK_PROCESS_CPUTIME_ID = 2 | ||
CLOCK_THREAD_CPUTIME_ID = 3 | ||
CLOCK_MONOTONIC_RAW = 4 | ||
CLOCK_REALTIME_COARSE = 5 | ||
CLOCK_MONOTONIC_COARSE = 6 | ||
CLOCK_BOOTTIME = 7 | ||
CLOCK_REALTIME_ALARM = 8 | ||
CLOCK_BOOTTIME_ALARM = 9 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import logging | ||
|
||
from unicorn import * | ||
from unicorn.arm_const import * | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class InterruptHandler: | ||
|
||
""" | ||
:type mu Uc | ||
""" | ||
def __init__(self, mu): | ||
self._mu = mu | ||
self._mu.hook_add(UC_HOOK_INTR, self._hook_interrupt) | ||
self._handlers = dict() | ||
|
||
def _hook_interrupt(self, uc, intno, data): | ||
if intno in self._handlers: | ||
self._handlers[intno](uc) | ||
else: | ||
logger.error("Unhandled interrupt %d at %x, stopping emulation" % (intno, self._mu.reg_read(UC_ARM_REG_PC))) | ||
self._mu.emu_stop() | ||
|
||
def set_handler(self, intno, handler): | ||
self._handlers[intno] = handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class SyscallHandler: | ||
|
||
def __init__(self, idx, name, arg_count, callback): | ||
self.idx = idx | ||
self.name = name | ||
self.arg_count = arg_count | ||
self.callback = callback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
|
||
from unicorn import * | ||
from unicorn.arm_const import * | ||
|
||
from androidemu.cpu.interrupt_handler import InterruptHandler | ||
from androidemu.cpu.syscall_handler import SyscallHandler | ||
from androidemu.utils import memory_helpers | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SyscallHandlers: | ||
|
||
""" | ||
:type interrupt_handler InterruptHandler | ||
""" | ||
def __init__(self, interrupt_handler): | ||
self._handlers = dict() | ||
interrupt_handler.set_handler(2, self._handle_syscall) | ||
|
||
def set_handler(self, idx, name, arg_count, callback): | ||
self._handlers[idx] = SyscallHandler(idx, name, arg_count, callback) | ||
|
||
def _handle_syscall(self, mu): | ||
idx = mu.reg_read(UC_ARM_REG_R7) | ||
args = [mu.reg_read(reg_idx) for reg_idx in range(UC_ARM_REG_R0, UC_ARM_REG_R6 + 1)] | ||
|
||
if idx in self._handlers: | ||
handler = self._handlers[idx] | ||
args = args[:handler.arg_count] | ||
args_formatted = ", ".join(["%08x" % arg for arg in args]) | ||
logger.info("Executing syscall %s(%s)" % (handler.name, args_formatted)) | ||
|
||
try: | ||
result = handler.callback(mu, *args) | ||
except: | ||
logger.error("An error occured during in %x syscall hander, stopping emulation" % idx) | ||
mu.emu_stop() | ||
raise | ||
|
||
if result is not None: | ||
mu.reg_write(UC_ARM_REG_R0, result) | ||
else: | ||
logger.error("Unhandled syscall 0x%x at 0x%x, stopping emulation" % (idx, mu.reg_read(UC_ARM_REG_PC))) | ||
mu.emu_stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from unicorn import Uc | ||
|
||
from androidemu.const.android import PR_SET_VMA | ||
from androidemu.const.linux import CLOCK_MONOTONIC_COARSE | ||
from androidemu.cpu.syscall_handlers import SyscallHandlers | ||
from androidemu.utils import memory_helpers | ||
|
||
|
||
class SyscallHooks: | ||
|
||
""" | ||
:type mu Uc | ||
:type syscall_handler SyscallHandlers | ||
""" | ||
def __init__(self, mu, syscall_handler): | ||
self._mu = mu | ||
self._syscall_handler = syscall_handler | ||
self._syscall_handler.set_handler(0xAC, "prctl", 5, self._handle_prctl) | ||
self._syscall_handler.set_handler(0x107, "clock_gettime", 2, self._handle_clock_gettime) | ||
|
||
def _handle_prctl(self, mu, option, arg2, arg3, arg4, arg5): | ||
""" | ||
int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5); | ||
See: | ||
- https://linux.die.net/man/2/prctl | ||
- https://github.com/torvalds/linux/blob/master/include/uapi/linux/prctl.h | ||
For PR_SET_VMA: | ||
- https://android.googlesource.com/platform/bionic/+/263325d/libc/include/sys/prctl.h | ||
- https://sourceforge.net/p/strace/mailman/message/34329772/ | ||
""" | ||
|
||
if option == PR_SET_VMA: | ||
# arg5 contains ptr to a name. | ||
return 0 | ||
else: | ||
raise NotImplementedError("Unsupported prctl option %d (0x%x)" % (option, option)) | ||
|
||
def _handle_clock_gettime(self, mu, clk_id, tp_ptr): | ||
""" | ||
The functions clock_gettime() retrieve the time of the specified clock clk_id. | ||
The clk_id argument is the identifier of the particular clock on which to act. A clock may be system-wide and | ||
hence visible for all processes, or per-process if it measures time only within a single process. | ||
clock_gettime(), clock_settime() and clock_getres() return 0 for success, or -1 for failure (in which case | ||
errno is set appropriately). | ||
""" | ||
|
||
if clk_id == CLOCK_MONOTONIC_COARSE: | ||
# TODO: Actually write time. | ||
return 0 | ||
else: | ||
raise NotImplementedError("Unsupported clk_id: %d (%x)" % (clk_id, clk_id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.