Permalink
Browse files

Added support for ARM in linux today to plant the inception thought i…

…n fischers_mind_state map. Was nice to learn about arm assembly to add the inception code support. Felt much advanced than intel especially the multiple stores/load (stfdm) but not required for the inception code to work as the assembly I coded was simpler but learning curve was good for ARM.
  • Loading branch information...
1 parent 1c8e262 commit 378450e49696330dc9727aa6dbd2e6043a254887 @karthick18 committed Aug 7, 2010
Showing with 82 additions and 4 deletions.
  1. +5 −1 Makefile
  2. +16 −2 inception.c
  3. +61 −1 inception.h
View
6 Makefile
@@ -7,7 +7,11 @@ ARCH := $(shell uname -m | sed -e 's,i.86,i386,')
ifeq ($(ARCH),i386)
ARCH_FLAGS := -m32
else
- ARCH_FLAGS := -m64
+ ifeq ($(ARCH), x86_64)
+ ARCH_FLAGS := -m64
+ else
+ ARCH_FLAGS :=
+ endif
endif
CFLAGS := -g -Wall $(ARCH_FLAGS)
SRC_FILES := $(wildcard *.c)
View
18 inception.c
@@ -1714,7 +1714,9 @@ static void meet_all_others_in_level_1(struct dreamer_attr *dattr)
static void shared_dream_level_1(void *dreamer_attr)
{
struct dreamer_attr *dattr = dreamer_attr;
- void (*fischer_level1)(void) = &fischer_dream_level1;
+ void (*fischer_level1)(void) __attribute__((unused)); /*for ARM its not used*/
+
+ fischer_level1 = &fischer_dream_level1;
set_thread_priority(dattr, 1);
@@ -1759,11 +1761,23 @@ static void shared_dream_level_1(void *dreamer_attr)
fischers_mind_state = mmap(0, getpagesize(), PROT_READ| PROT_WRITE|PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(fischers_mind_state != MAP_FAILED);
- memset(fischers_mind_state, 0x90, getpagesize()); /*fill with x86 NOP opcodes*/
+ nop_fill(fischers_mind_state, getpagesize());
memcpy(fischers_mind_state, fischers_thoughts, sizeof(fischers_thoughts));
+#if defined(__i386__) || defined(__x86_64__)
+
asm("push %0\n"
"jmp *%1\n"
::"r"(fischers_mind_state),"m"(fischer_level1):"memory");
+#elif defined(__arm__)
+
+ asm("ldr lr, %0\n"
+ "b fischer_dream_level1\n"
+ ::"m"(fischers_mind_state):"memory","lr");
+#else
+
+#error "Unsupport architecture"
+
+#endif
/*
* IF we return back, it means INCEPTION has failed. Abort the process.
*/
View
62 inception.h
@@ -32,7 +32,7 @@ static unsigned char inception_thoughts[] = {
0x75, 0x61, 0x6c, 0x69, 0x74, 0x79, 0x0a,
};
-#else /* __x86_64__ */
+#elif defined(__x86_64__) /* __x86_64__ */
/*
* For 64 bit, find the inception buffer with the assembly equivalent using:
@@ -70,6 +70,35 @@ static unsigned char inception_thoughts[] = {
0x75 ,0x61 ,0x6c ,0x69 ,0x74 ,0x79 ,0x0a,
};
+#elif defined(__arm__)
+
+/*
+ * Here is the assembly equilavent for arm.
+ __asm__ __volatile__("mov r0, #1\n"
+ "add r1, pc, #12\n"
+ "mov r2, #4\n"
+ "swi 0x900004\n"
+ "mov r0, #1\n"
+ "swi 0x900001\n"
+ ".ascii");
+ */
+static unsigned char inception_thoughts[] = {
+ 0x01,0x00,0xa0,0xe3,0x0c,0x10,0x8f,0xe2
+ ,0x37,0x20,0xa0,0xe3,0x4,0x00,0x90,0xef
+ ,0x00,0x00,0xa0,0xe3,0x01,0x00,0x90,0xef
+ ,0x52,0x65,0x63,0x6f,0x6e,0x63,0x69,0x6c,0x65,0x20
+ ,0x77,0x69,0x74,0x68,0x20,0x6d,0x79,0x20
+ ,0x66,0x61,0x74,0x68,0x65,0x72,0x20,0x61
+ ,0x6e,0x64,0x20,0x68,0x61,0x76,0x65,0x20
+ ,0x6d,0x79,0x20,0x6f,0x77,0x6e,0x20,0x69
+ ,0x6e,0x64,0x69,0x76,0x69,0x64,0x75,0x61
+ ,0x6c,0x69,0x74,0x79,0x0a,
+};
+
+#else
+
+#error "Unsupported linux architecture. Force compilation errors"
+
#endif
/*
@@ -78,11 +107,37 @@ static unsigned char inception_thoughts[] = {
* Fischers dream
*/
+#if defined(__i386__) || defined(__x86_64__)
+
static unsigned char fischers_thoughts[] = { [ 0 ... sizeof(inception_thoughts)-1] = 0x90,
0xb8, 0x1, 0x00, 0x00,
0x00, 0xbb, 0x00, 0x00, 0x00, 0x00,0xcd, 0x80,
};
+static __inline__ void nop_fill(char *map, int len)
+{
+ memset(map, 0x90, len); /*fill it with the x86 nop opcode*/
+}
+
+#elif defined(__arm__)
+
+static unsigned char fischers_thoughts[] = { 0x00, 0x00, 0xa0, 0xe3, 0x01, 0x00, 0x90, 0xef };
+
+static __inline__ void nop_fill(char *map, int len)
+{
+#define NOP_WORD_FILL 0xe1a01001 /* mov r1, r1 or even a zero set with mov r0, r0 */
+ register int i;
+ for(i = 0; i < len >> 2; ++i)
+ ((int*)map)[i] = NOP_WORD_FILL;
+#undef NOP_WORD_FILL
+}
+
+#else
+
+#error "Unsupported linux arch. Force compilation error"
+
+#endif
+
#elif defined(__APPLE__)
static unsigned char inception_thoughts[] = {
@@ -106,6 +161,11 @@ static unsigned char fischers_thoughts[] = { [ 0 ... sizeof(inception_thoughts)-
,0x00, 0x00, 0x00, 0x50, 0xcd, 0x80,
};
+static __inline__ void nop_fill(char *map, int len)
+{
+ memset(map, 0x90, len); /*fill it with the x86 nop opcode*/
+}
+
#else
#error "Unsupported Arch. Force compilation errors"

0 comments on commit 378450e

Please sign in to comment.