From 0a7914e3f71093399ce7f62d77d79ba685b04264 Mon Sep 17 00:00:00 2001 From: sys64738 Date: Sat, 29 Jan 2022 03:24:18 +0100 Subject: [PATCH] linux stuff --- jazelle.c | 36 ++++++++++++++++++++++++++++++++---- rpi/.gitignore | 3 +++ rpi/Makefile | 6 ++++++ rpi/linux.c | 8 ++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 rpi/.gitignore create mode 100644 rpi/linux.c diff --git a/jazelle.c b/jazelle.c index ccde2fe..679fec0 100644 --- a/jazelle.c +++ b/jazelle.c @@ -13,9 +13,11 @@ #if defined(__linux__) && !defined(__KERNEL__) +// FIXME: (on musl) seems to crash upon entry before reaching main() #include #include #include +#include #define iprintf(fmt, ...) printf((fmt), ##__VA_ARGS__) #elif defined(__KERNEL__) @@ -174,7 +176,7 @@ static void DC_InvalidateAll(void) { DC_FlushAll(); } __attribute__((__naked__, __no_inline__)) -static void IC_InvalidateAll(void) { +void IC_InvalidateAll(void) { asm volatile( ".fill 65536, 4, 0\n" // 64k nop instructions because eh "bx lr\n" @@ -329,6 +331,7 @@ static void handler_0xfe(void) { "bxj r12\n" // FIXME: r12 can be modified by jazelle so it should be restored to something //: //:[br5]"m"(backup_r5) + ".pool\n" ); } @@ -426,7 +429,7 @@ static void handler_noexec() { ); } -static uint8_t bytecode_testh[] = { +static uint8_t bytecode_testh_base[] = { 0x06, 0x06, 0x07, 0x07, 0x05, 0x05, @@ -439,6 +442,22 @@ static uint8_t bytecode_testh[] = { }; static void jazelle_test_handlers(uint8_t hflags[256/8]) { +#ifdef __linux__ +#ifdef __KERNEL__ +#error "TODO: kernel: alloc rwx page" +#else + uint8_t* bytecode_testh = mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC, + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (!bytecode_testh || !~(uint32_t)bytecode_testh) { + printf("Can't map a page RWX, need it for instruction enumeration.\n"); + return; + } + memcpy(bytecode_testh, bytecode_testh_base, sizeof bytecode_testh_base); +#endif +#else + #define bytecode_testh bytecode_testh_base +#endif + memset(hflags, 0, 256/8); for (int i = 0x00; i < 0xff /* bytecode 0xff is hardwired to bkpt #0 */; ++i) { if (i == 0xba) { @@ -490,9 +509,19 @@ static void jazelle_test_handlers(uint8_t hflags[256/8]) { jazelle_block.handlers[i] = NULL; } +#ifdef __linux__ +#ifdef __KERNEL__ +#error "TODO: kernel: alloc rwx page" +#else + munmap(bytecode_testh, 4096); +#endif +#else + #undef bytecode_testh +#endif } // https://github.com/SonoSooS/libjz/wiki/Java-instruction-set +__attribute__((__section__(".text"), __align__(4))) static uint8_t bytecode_test1[] = { 0x06, // iconst_3 0x07, // iconst_4 @@ -503,6 +532,7 @@ static uint8_t bytecode_test1[] = { 0x60, // iadd 0x60, // iadd 0xAC, // ireturn + 0x00, 0x00, 0x00 // alignment to keep gcc happy }; // returns 6 if idiv implemented natively, else 10 @@ -551,7 +581,5 @@ void jazelle_main(void) { } printf("\r\n"); } - - while (true) ; } diff --git a/rpi/.gitignore b/rpi/.gitignore new file mode 100644 index 0000000..02138fb --- /dev/null +++ b/rpi/.gitignore @@ -0,0 +1,3 @@ +*.o +*.elf +jazlinux diff --git a/rpi/Makefile b/rpi/Makefile index 078709a..f5796c3 100644 --- a/rpi/Makefile +++ b/rpi/Makefile @@ -11,6 +11,9 @@ GDB ?= $(PREFIX)gdb PYTHON3 ?= python3 NC ?= nc +LNXPREFIX ?= arm-linux-gnueabihf- +LNXCC = $(LNXPREFIX)gcc + default: all all: rpi.elf @@ -27,6 +30,9 @@ rpi.elf: rpi.o jazelle.o: ../jazelle.c $(CC) $(CFLAGS) -c -o "$@" "$<" +jazlinux: linux.c ../jazelle.c + $(LNXCC) $(CLFAGS) -o "$@" $^ + openocd-launch: openocd -f interface/cmsis-dap.cfg -c "transport select jtag" \ -c "adapter speed 50" -f target/bcm2835.cfg \ diff --git a/rpi/linux.c b/rpi/linux.c new file mode 100644 index 0000000..05a1264 --- /dev/null +++ b/rpi/linux.c @@ -0,0 +1,8 @@ + +void jazelle_main(void); + +int main() { + jazelle_main(); + return 0; +} +