From 11fa5a9a6b864a7e02a4290153fdb08a69625f27 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Thu, 20 Nov 2008 16:22:28 +0000 Subject: [PATCH] towards adding support for 64-bit architectures: registering relative memory addresses with contiki instead of fetching absolute addresses to java --- .../examples/jni_test/level4/Level4.java | 10 +-- tools/cooja/examples/jni_test/level4/level4.c | 24 +++--- .../examples/jni_test/level5/Level5.java | 80 ++++++++++++------- tools/cooja/examples/jni_test/level5/level5.c | 46 +++++++---- 4 files changed, 103 insertions(+), 57 deletions(-) diff --git a/tools/cooja/examples/jni_test/level4/Level4.java b/tools/cooja/examples/jni_test/level4/Level4.java index 0005c808e..d1658261e 100644 --- a/tools/cooja/examples/jni_test/level4/Level4.java +++ b/tools/cooja/examples/jni_test/level4/Level4.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Level4.java,v 1.3 2007/09/18 11:35:11 fros4943 Exp $ + * $Id: Level4.java,v 1.4 2008/11/20 16:22:28 fros4943 Exp $ */ import java.io.*; @@ -45,7 +45,8 @@ public class Level4 { } private native void doCount(); - private native int getRefAddress(); + + public native void setReferenceAddress(int addr); public Level4() { // Configure logger @@ -152,12 +153,9 @@ public class Level4 { System.exit(1); } - int absRefAddress = getRefAddress(); - System.out.println("Absolute reference address: 0x" + Integer.toHexString(absRefAddress)); int relRefAddress = (Integer) addresses.get("ref_var"); System.out.println("Relative reference address: 0x" + Integer.toHexString(relRefAddress)); - int offsetRelToAbs = absRefAddress - relRefAddress; - System.out.println("Offset relative-absolute: 0x" + Integer.toHexString(offsetRelToAbs)); + setReferenceAddress(relRefAddress); doCount(); doCount(); diff --git a/tools/cooja/examples/jni_test/level4/level4.c b/tools/cooja/examples/jni_test/level4/level4.c index c48c6d50e..3adb921c2 100644 --- a/tools/cooja/examples/jni_test/level4/level4.c +++ b/tools/cooja/examples/jni_test/level4/level4.c @@ -26,16 +26,27 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: level4.c,v 1.2 2007/09/10 14:07:12 fros4943 Exp $ + * $Id: level4.c,v 1.3 2008/11/20 16:22:28 fros4943 Exp $ */ #include #include -int ref_var; +long ref_var; /* Placed somewhere in the BSS section */ -int initialized_counter=1; -int uninitialized_counter; +int initialized_counter=1; /* Variable in data section */ +int uninitialized_counter; /* Variable in BSS section */ + +JNIEXPORT void JNICALL +Java_Level4_setReferenceAddress(JNIEnv *env, jobject obj, jint addr) +{ + /*printf("relative reference address is %p\n", addr);*/ + /*printf("absolute reference address is %p\n", &ref_var);*/ + + ref_var = (((long)&ref_var) - ((long)addr)); + printf("Offset is 0x%p\n", ref_var); + fflush(stdout); +} JNIEXPORT void JNICALL Java_Level4_doCount(JNIEnv *env, jobject obj) @@ -43,8 +54,3 @@ Java_Level4_doCount(JNIEnv *env, jobject obj) printf(">> DATA_counter=\t%i\tBSS_counter=\t%i\n", initialized_counter++, uninitialized_counter++); fflush(stdout); } -JNIEXPORT jint JNICALL -Java_Level4_getRefAddress(JNIEnv *env, jobject obj) -{ - return (jint) &ref_var; -} diff --git a/tools/cooja/examples/jni_test/level5/Level5.java b/tools/cooja/examples/jni_test/level5/Level5.java index 7a796d435..8c624d63e 100644 --- a/tools/cooja/examples/jni_test/level5/Level5.java +++ b/tools/cooja/examples/jni_test/level5/Level5.java @@ -26,14 +26,14 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: Level5.java,v 1.4 2007/09/18 11:35:11 fros4943 Exp $ + * $Id: Level5.java,v 1.5 2008/11/20 16:22:28 fros4943 Exp $ */ import java.io.*; import java.util.Properties; import java.util.Vector; -import org.apache.log4j.xml.DOMConfigurator; +import org.apache.log4j.BasicConfigurator; import se.sics.cooja.GUI; import se.sics.cooja.SectionMoteMemory; import se.sics.cooja.contikimote.ContikiMoteType; @@ -46,9 +46,10 @@ public class Level5 { } private native void doCount(); - private native int getRefAddress(); - public native void getMemory(int start, int length, byte[] mem); - public native void setMemory(int start, int length, byte[] mem); + + public native void setReferenceAddress(int addr); + public native void getMemory(int addr, int length, byte[] mem); + public native void setMemory(int addr, int length, byte[] mem); private int javaDataCounter = 1; private int javaBssCounter = 0; @@ -56,7 +57,7 @@ public class Level5 { public Level5() { // Configure logger - DOMConfigurator.configure(GUI.class.getResource("/" + GUI.LOG_CONFIG_FILE)); + BasicConfigurator.configure(); // Load configuration System.out.println("Loading COOJA configuration"); @@ -126,16 +127,15 @@ public class Level5 { bssSectionSize = ContikiMoteType.loadBssSectionSize(mapData); } - int absRefAddress = getRefAddress(); int relRefAddress = (Integer) addresses.get("ref_var"); - int offsetRelToAbs = absRefAddress - relRefAddress; + setReferenceAddress(relRefAddress); System.out.println("Creating section memory"); byte[] initialDataSection = new byte[dataSectionSize]; byte[] initialBssSection = new byte[bssSectionSize]; - getMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, initialDataSection); - getMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection); + getMemory(relDataSectionAddr, dataSectionSize, initialDataSection); + getMemory(relBssSectionAddr, bssSectionSize, initialBssSection); SectionMoteMemory memory = new SectionMoteMemory(addresses); memory.setMemorySegment(relDataSectionAddr, initialDataSection); memory.setMemorySegment(relBssSectionAddr, initialBssSection); @@ -145,8 +145,14 @@ public class Level5 { System.out.print("Checking initial values: "); dataCounter = memory.getIntValueOf("initialized_counter"); bssCounter = memory.getIntValueOf("uninitialized_counter"); - if (dataCounter != javaDataCounter || bssCounter != javaBssCounter) { + if (dataCounter != javaDataCounter) { + System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")"); System.out.println("FAILED!"); + System.exit(1); + } else if (bssCounter != javaBssCounter) { + System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")"); + System.out.println("FAILED!"); + System.exit(1); } else { System.out.println("OK!"); } @@ -159,13 +165,18 @@ public class Level5 { doCount(); javaDataCounter++; javaBssCounter++; System.out.print("Checking increased values: "); - getMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, initialDataSection); - getMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection); + getMemory(relDataSectionAddr, dataSectionSize, initialDataSection); + getMemory(relBssSectionAddr, bssSectionSize, initialBssSection); memory.setMemorySegment(relDataSectionAddr, initialDataSection); memory.setMemorySegment(relBssSectionAddr, initialBssSection); dataCounter = memory.getIntValueOf("initialized_counter"); bssCounter = memory.getIntValueOf("uninitialized_counter"); - if (dataCounter != javaDataCounter || bssCounter != javaBssCounter) { + if (dataCounter != javaDataCounter) { + System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")"); + System.out.println("FAILED!"); + System.exit(1); + } else if (bssCounter != javaBssCounter) { + System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")"); System.out.println("FAILED!"); System.exit(1); } else { @@ -177,8 +188,8 @@ public class Level5 { byte[] savedBssSection = new byte[bssSectionSize]; int savedDataCounter = dataCounter; int savedBssCounter = bssCounter; - getMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, savedDataSection); - getMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, savedBssSection); + getMemory(relDataSectionAddr, dataSectionSize, savedDataSection); + getMemory(relBssSectionAddr, bssSectionSize, savedBssSection); System.out.println("Increasing counters 3 times"); doCount(); javaDataCounter++; javaBssCounter++; @@ -186,13 +197,18 @@ public class Level5 { doCount(); javaDataCounter++; javaBssCounter++; System.out.print("Checking increased values: "); - getMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, initialDataSection); - getMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection); + getMemory(relDataSectionAddr, dataSectionSize, initialDataSection); + getMemory(relBssSectionAddr, bssSectionSize, initialBssSection); memory.setMemorySegment(relDataSectionAddr, initialDataSection); memory.setMemorySegment(relBssSectionAddr, initialBssSection); dataCounter = memory.getIntValueOf("initialized_counter"); bssCounter = memory.getIntValueOf("uninitialized_counter"); - if (dataCounter != javaDataCounter || bssCounter != javaBssCounter) { + if (dataCounter != javaDataCounter) { + System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")"); + System.out.println("FAILED!"); + System.exit(1); + } else if (bssCounter != javaBssCounter) { + System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")"); System.out.println("FAILED!"); System.exit(1); } else { @@ -200,7 +216,7 @@ public class Level5 { } System.out.println("Restoring data segment"); - setMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, savedDataSection); + setMemory(relDataSectionAddr, dataSectionSize, savedDataSection); javaDataCounter = savedDataCounter; System.out.println("Increasing counters 3 times"); @@ -209,13 +225,18 @@ public class Level5 { doCount(); javaDataCounter++; javaBssCounter++; System.out.print("Checking reset data counter: "); - getMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, initialDataSection); - getMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection); + getMemory(relDataSectionAddr, dataSectionSize, initialDataSection); + getMemory(relBssSectionAddr, bssSectionSize, initialBssSection); memory.setMemorySegment(relDataSectionAddr, initialDataSection); memory.setMemorySegment(relBssSectionAddr, initialBssSection); dataCounter = memory.getIntValueOf("initialized_counter"); bssCounter = memory.getIntValueOf("uninitialized_counter"); - if (dataCounter != javaDataCounter || bssCounter != javaBssCounter) { + if (dataCounter != javaDataCounter) { + System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")"); + System.out.println("FAILED!"); + System.exit(1); + } else if (bssCounter != javaBssCounter) { + System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")"); System.out.println("FAILED!"); System.exit(1); } else { @@ -223,17 +244,22 @@ public class Level5 { } System.out.println("Restoring bss segment"); - setMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, savedBssSection); + setMemory(relBssSectionAddr, bssSectionSize, savedBssSection); javaBssCounter = savedBssCounter; System.out.print("Checking reset bss counter: "); - getMemory(relDataSectionAddr + offsetRelToAbs, dataSectionSize, initialDataSection); - getMemory(relBssSectionAddr + offsetRelToAbs, bssSectionSize, initialBssSection); + getMemory(relDataSectionAddr, dataSectionSize, initialDataSection); + getMemory(relBssSectionAddr, bssSectionSize, initialBssSection); memory.setMemorySegment(relDataSectionAddr, initialDataSection); memory.setMemorySegment(relBssSectionAddr, initialBssSection); dataCounter = memory.getIntValueOf("initialized_counter"); bssCounter = memory.getIntValueOf("uninitialized_counter"); - if (dataCounter != javaDataCounter || bssCounter != javaBssCounter) { + if (dataCounter != javaDataCounter) { + System.out.println("DATA mismatch (" + dataCounter + " != " + javaDataCounter + ")"); + System.out.println("FAILED!"); + System.exit(1); + } else if (bssCounter != javaBssCounter) { + System.out.println("BSS mismatch (" + bssCounter + " != " + javaBssCounter + ")"); System.out.println("FAILED!"); System.exit(1); } else { diff --git a/tools/cooja/examples/jni_test/level5/level5.c b/tools/cooja/examples/jni_test/level5/level5.c index d77956f64..c3328da19 100644 --- a/tools/cooja/examples/jni_test/level5/level5.c +++ b/tools/cooja/examples/jni_test/level5/level5.c @@ -26,17 +26,29 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: level5.c,v 1.3 2007/09/10 14:07:12 fros4943 Exp $ + * $Id: level5.c,v 1.4 2008/11/20 16:22:28 fros4943 Exp $ */ #include #include +#include #include -int ref_var; +long ref_var; /* Placed somewhere in the BSS section */ -int initialized_counter=1; -int uninitialized_counter; +int initialized_counter=1; /* Variable in data section */ +int uninitialized_counter; /* Variable in BSS section */ + +JNIEXPORT void JNICALL +Java_Level5_setReferenceAddress(JNIEnv *env, jobject obj, jint addr) +{ + /*printf("relative reference address is %p\n", addr);*/ + /*printf("absolute reference address is %p\n", &ref_var);*/ + + ref_var = (((long)&ref_var) - ((long)addr)); + printf("Offset is 0x%p\n", ref_var); + fflush(stdout); +} JNIEXPORT void JNICALL Java_Level5_doCount(JNIEnv *env, jobject obj) @@ -44,22 +56,26 @@ Java_Level5_doCount(JNIEnv *env, jobject obj) printf(">> DATA_counter=\t%i\tBSS_counter=\t%i\n", ++initialized_counter, ++uninitialized_counter); fflush(stdout); } -JNIEXPORT jint JNICALL -Java_Level5_getRefAddress(JNIEnv *env, jobject obj) + +JNIEXPORT void JNICALL +Java_Level5_getMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr) { - return (jint) &ref_var; + (*env)->SetByteArrayRegion( + env, + mem_arr, + 0, + (size_t) length, + (jbyte *) (((long)rel_addr) + ref_var) + ); } JNIEXPORT void JNICALL -Java_Level5_getMemory(JNIEnv *env, jobject obj, jint start, jint length, jbyteArray mem_arr) -{ - (*env)->SetByteArrayRegion(env, mem_arr, 0, (size_t) length, (jbyte *) start); -} - -JNIEXPORT void JNICALL -Java_Level5_setMemory(JNIEnv *env, jobject obj, jint start, jint length, jbyteArray mem_arr) +Java_Level5_setMemory(JNIEnv *env, jobject obj, jint rel_addr, jint length, jbyteArray mem_arr) { jbyte *mem = (*env)->GetByteArrayElements(env, mem_arr, 0); - memcpy((void *) start, mem, length); + memcpy( + (char*) (((long)rel_addr) + ref_var), + mem, + length); (*env)->ReleaseByteArrayElements(env, mem_arr, mem, 0); }