Mono on Android - Success At Last!

Last week, I blogged about my attempts to get Mono running on Android. I was running into a nasty Illegal Instruction issue, which I didn't look into further until today. And after implementing a quick hack/fix I got it working!

After running strace on Mono, I was able to determine that the Illegal Instruction was occuring in mini-arm.c in the mono_arch_flush_icache function. I wasn't able to decipher whether the inline assembly was correct or not; but I got the jist of what it was trying to do: it was calling cacheflush, but failing with the Illegal Instruction in there. I looked at the mini-x86.c and saw that the equivalent mono_arch_flush_icache was empty; the comment indicating it was "not needed". I don't know if this is some vestigial code or what, but I figured it would be relatively benign to not flush the instruction cache and just see what happens. And the grand results:

root@localhost:~/Desktop# mono test.exe

Hello World

This would not be possible without the help of my co-worker, Kevin Eustice, and my abhorrence towards Java.

Expect to see some performance comparisons between Mono vs Dalvik shortly! Though I don't expect it to even be a fair competition, because Dalvik does not JIT. Lame.

9 comments:

Anonymous said...

It would be interesting to see that speed comparison whenever you get a chance.

Anonymous said...

Would you mind posting the instructions that you followed to build Mono for the iPhone?

Koush said...

Unfortunately, soon after posting, I discovered I'm still having issues running Mono on the actual phone.
The first problem was that SYSVIPC was disabled in the Dream kernel, but not in the emulator. As a result, one of the system calls Mono was making was failing with a Not Implemented error. I ended up rebuilding the kernel and deployed it to the phone. Now Mono makes it past that point, but throws a OutOfMemoryException, followed by a StackOverflowException, and finishes off with a NullReferenceException. All that for a program that simply starts and exits (doesn't even print hello world). I don't think it is memory related at all though, because I can run it on the emulator with 96MB ram. And the device has plenty of RAM free (Mono doesn't have a 58MB footprint).

I know I'm really close; much closer than I was last week... I'll be looking into this more when I get a chance.

mdi said...

I believe you can disable the SysV requirement by exporting the MONO_DISABLE_SHM=1 environment variable.

Koush said...

Hi Miguel, is that a variable I set during compile time to produce a binary without that dependency or is it a runtime check?

Unknown said...

For the cache bug, see:

https://bugzilla.novell.com/show_bug.cgi?id=462587

Seems like the asm is not compatible with EABI. This should fix it.

Koush said...

Awesome, thanks for the patch!

Koush said...

Yessss!!! Mark's change fixed the issue on the phone, and it works now! (So commenting that code out was a *bad* thing)

Performance comparisons coming this weekend. :)

Jim said...

Congratulations!! This is so cool!
Can you do this on a regular G1 or does it need to be "jailbroken" somehow? obviously I don't have a G1, so please forgive my ignorance.
Thanks!