[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

icedtea zero M68K atomic patch



Greetings!

Here is a patch for the M68K zero port to deal with missing atomic operations in M68K hardware, here implemented using a processor specific CAS instruction found on most 680X0 CPUs. It will priobably work on 68020 68030 68040 and 68060. Suggestions are welcome how to make improve it to work on all M68K cpus compatible with Linux/M68K like the ColdFire who lacks the CAS instruction.

I have included a small testprogram as well that can be build with g++ to test the zero atomic headerfile without compiling the whole hotspot.
xerxes@aranym-m68k:~/test$ g++ atomictest.cpp
xerxes@aranym-m68k:~/test$ ./a.out
start of test
testing Atomic::xchg
testing Atomic::xchg_ptr
a=C0FFEE b=CAFEBABE
Atomic::xchg() works!
c=EFE6A938 &b=EFE6A938 d=EFE6A93C &a=EFE6A93C
Atomic::xchg_ptr() works!

Have a great day!
Xerxes
diff -r 8b0e11483252 ports/hotspot/src/os_cpu/linux_zero/vm/atomic_linux_zero.inline.hpp
--- a/ports/hotspot/src/os_cpu/linux_zero/vm/atomic_linux_zero.inline.hpp	Thu Sep 11 20:47:50 2008 +0200
+++ b/ports/hotspot/src/os_cpu/linux_zero/vm/atomic_linux_zero.inline.hpp	Fri Sep 12 15:51:35 2008 +0200
@@ -24,6 +24,83 @@
  */
 
 // Implementation of class atomic
+
+#ifdef M68K
+
+/*
+ * __m68k_cmpxchg
+ *
+ * Atomically store newval in *ptr if *ptr is equal to oldval for user space.
+ * Returns newval on success and oldval if no exchange happened. 
+ * This implementation is processor specific and works on 
+ * 68020 68030 68040 and 68060.
+ *
+ * It will not work on ColdFire, 68000 and 68010 since they lack the CAS
+ * instruction. 
+ * Using a kernelhelper would be better for arch complete implementation.
+ *
+ */
+ 
+static inline int __m68k_cmpxchg(int oldval,
+                                int newval,
+                                volatile int *ptr)
+{ 
+  int ret;
+  __asm __volatile ("cas%.l %0,%2,%1"
+                   : "=d" (ret), "+m" (*(ptr))
+                   : "d" (newval), "0" (oldval));
+  return ret;
+}
+
+/* Perform an atomic compare and swap: if the current value of `*PTR'
+   is OLDVAL, then write NEWVAL into `*PTR'.  Return the contents of
+   `*PTR' before the operation.*/
+static inline int m68k_compare_and_swap(volatile int *ptr,
+                                       int oldval,
+                                       int newval) 
+{
+  for (;;)
+    {
+      int prev = *ptr;
+      if (prev != oldval)
+	return prev;
+
+      if (__m68k_cmpxchg (prev, newval, ptr) == newval)
+	// Success.
+	return prev;
+
+      // We failed even though prev == oldval.  Try again.
+    }
+}
+
+/* Atomically add an int to memory.  */
+static inline int m68k_add_and_fetch(volatile int *ptr, int add_value)
+{
+  for (;;)
+    {
+      // Loop until a __kernel_cmpxchg succeeds.
+
+      int prev = *ptr;
+
+      if (__m68k_cmpxchg (prev, prev + add_value, ptr) == prev + add_value)
+	return prev + add_value;
+    }
+}
+
+/* Atomically write VALUE into `*PTR' and returns the previous
+   contents of `*PTR'.  */
+static inline int m68k_lock_test_and_set(volatile int *ptr, int newval)
+{
+  for (;;)
+    {
+      // Loop until a __kernel_cmpxchg succeeds.
+      int prev = *ptr;
+
+      if (__m68k_cmpxchg (prev, newval, ptr) == prev)
+	return prev;
+    }
+}
+#endif // M68K
 
 #ifdef ARM
 
@@ -107,7 +184,11 @@
 #ifdef ARM
   return arm_add_and_fetch(dest, add_value);
 #else
+#ifdef M68K
+  return m68k_add_and_fetch(dest, add_value);
+#else
   return __sync_add_and_fetch(dest, add_value);
+#endif // M68K
 #endif // ARM
 }
 
@@ -116,7 +197,11 @@
 #ifdef ARM
   return arm_add_and_fetch(dest, add_value);
 #else
+#ifdef M68K
+  return m68k_add_and_fetch(dest, add_value);
+#else
   return __sync_add_and_fetch(dest, add_value);
+#endif // M68K
 #endif // ARM
 }
 
@@ -160,11 +245,15 @@
 #ifdef ARM
   return arm_lock_test_and_set(dest, exchange_value);
 #else
+#ifdef M68K
+  return m68k_lock_test_and_set(dest, exchange_value);
+#else
   // __sync_lock_test_and_set is a bizarrely named atomic exchange
   // operation.  Note that some platforms only support this with the
   // limitation that the only valid value to store is the immediate
   // constant 1.  There is a test for this in JNI_CreateJavaVM().
   return __sync_lock_test_and_set (dest, exchange_value);
+#endif // M68K
 #endif // ARM
 }
 
@@ -173,7 +262,11 @@
 #ifdef ARM
   return arm_lock_test_and_set(dest, exchange_value);
 #else
+#ifdef M68K
+  return m68k_lock_test_and_set(dest, exchange_value);
+#else
   return __sync_lock_test_and_set (dest, exchange_value);
+#endif // M68K
 #endif // ARM
 }
 
@@ -189,7 +282,11 @@
 #ifdef ARM
   return arm_compare_and_swap(dest, compare_value, exchange_value);
 #else
+#ifdef M68K
+  return m68k_compare_and_swap(dest, compare_value, exchange_value);
+#else
   return __sync_val_compare_and_swap(dest, compare_value, exchange_value);
+#endif // M68K
 #endif // ARM
 }
 
@@ -206,7 +303,11 @@
 #ifdef ARM
   return arm_compare_and_swap(dest, compare_value, exchange_value);
 #else
+#ifdef M68K
+  return m68k_compare_and_swap(dest, compare_value, exchange_value);
+#else
   return __sync_val_compare_and_swap(dest, compare_value, exchange_value);
+#endif // M68K
 #endif // ARM
 }
 
#include <assert.h>
#include <stdio.h>

typedef int jint;
typedef int intptr_t;
typedef long jlong;

void Unimplemented(void)
{
  printf("Unimplemented");
}

class Atomic {
public: 
inline void store(jint store_value, volatile jint* dest);
inline void store_ptr(intptr_t store_value, intptr_t* dest);
inline jint add(jint add_value, volatile jint* dest);
inline intptr_t add_ptr(intptr_t add_value, volatile intptr_t* dest);
inline void* add_ptr(intptr_t add_value, volatile void* dest);
inline void inc(volatile jint* dest);
inline void inc_ptr(volatile intptr_t* dest);
inline void inc_ptr(volatile void* dest);
inline void dec(volatile jint* dest);
inline void dec_ptr(volatile intptr_t* dest);
inline void dec_ptr(volatile void* dest);
static inline jint xchg(jint exchange_value, volatile jint* dest);
static inline intptr_t xchg_ptr(intptr_t exchange_value,
                                 volatile intptr_t* dest);
static inline void* xchg_ptr(void* exchange_value, volatile void* dest);
inline jint cmpxchg(jint exchange_value,
                            volatile jint* dest,
                            jint compare_value);
inline jlong cmpxchg(jlong exchange_value,
                             volatile jlong* dest,
                             jlong compare_value);
inline intptr_t cmpxchg_ptr(intptr_t exchange_value,
                                    volatile intptr_t* dest,
                                    intptr_t compare_value);
inline void* cmpxchg_ptr(void* exchange_value,
                                 volatile void* dest,
                                 void* compare_value);
};

#define M68K
#include "atomic_linux_zero.inline.hpp"

int main (int argc, char* argv[])
{
   printf("start of test\n");
   jint a = 0xCAFEBABE;
   printf("testing Atomic::xchg\n"); 
   jint b = Atomic::xchg(0xC0FFEE, &a);
   void *c = &a;
   printf("testing Atomic::xchg_ptr\n"); 
   void *d = Atomic::xchg_ptr(&b, &c);

   printf("a=%X b=%X\n",a,b);
   assert(a == 0xC0FFEE && b == (jint) 0xCAFEBABE);
   printf( "Atomic::xchg() works!\n");

   printf("c=%X &b=%X d=%X &a=%X\n",c,&b,d,&a);
   assert(c == &b && d == &a);
   printf("Atomic::xchg_ptr() works!\n");
}

Reply to: