fd2893
diff -up openssl-1.0.2a/engines/e_padlock.c.padlock64 openssl-1.0.2a/engines/e_padlock.c
fd2893
--- openssl-1.0.2a/engines/e_padlock.c.padlock64	2015-03-19 14:19:00.000000000 +0100
fd2893
+++ openssl-1.0.2a/engines/e_padlock.c	2015-04-22 16:23:44.105617468 +0200
fd2893
@@ -101,7 +101,10 @@
fd2893
  */
fd2893
 #  undef COMPILE_HW_PADLOCK
fd2893
 #  if !defined(I386_ONLY) && !defined(OPENSSL_NO_INLINE_ASM)
fd2893
-#   if (defined(__GNUC__) && (defined(__i386__) || defined(__i386))) || \
fd2893
+#  if (defined(__GNUC__) && __GNUC__>=2 && \
fd2893
+       (defined(__i386__) || defined(__i386) || \
fd2893
+        defined(__x86_64__) || defined(__x86_64)) \
fd2893
+     ) || \
fd2893
      (defined(_MSC_VER) && defined(_M_IX86))
fd2893
 #    define COMPILE_HW_PADLOCK
fd2893
 #   endif
fd2893
@@ -140,7 +143,7 @@ void ENGINE_load_padlock(void)
fd2893
 #    endif
fd2893
 #   elif defined(__GNUC__)
fd2893
 #    ifndef alloca
fd2893
-#     define alloca(s) __builtin_alloca(s)
fd2893
+#     define alloca(s) __builtin_alloca((s))
fd2893
 #    endif
fd2893
 #   endif
fd2893
 
fd2893
@@ -303,6 +306,7 @@ static volatile struct padlock_cipher_da
fd2893
  * =======================================================
fd2893
  */
fd2893
 #   if defined(__GNUC__) && __GNUC__>=2
fd2893
+#    if defined(__i386__) || defined(__i386)
fd2893
 /*
fd2893
  * As for excessive "push %ebx"/"pop %ebx" found all over.
fd2893
  * When generating position-independent code GCC won't let
fd2893
@@ -379,22 +383,6 @@ static int padlock_available(void)
fd2893
     return padlock_use_ace + padlock_use_rng;
fd2893
 }
fd2893
 
fd2893
-#    ifndef OPENSSL_NO_AES
fd2893
-#     ifndef AES_ASM
fd2893
-/* Our own htonl()/ntohl() */
fd2893
-static inline void padlock_bswapl(AES_KEY *ks)
fd2893
-{
fd2893
-    size_t i = sizeof(ks->rd_key) / sizeof(ks->rd_key[0]);
fd2893
-    unsigned int *key = ks->rd_key;
fd2893
-
fd2893
-    while (i--) {
fd2893
-        asm volatile ("bswapl %0":"+r" (*key));
fd2893
-        key++;
fd2893
-    }
fd2893
-}
fd2893
-#     endif
fd2893
-#    endif
fd2893
-
fd2893
 /*
fd2893
  * Force key reload from memory to the CPU microcode. Loading EFLAGS from the
fd2893
  * stack clears EFLAGS[30] which does the trick.
fd2893
@@ -404,7 +392,7 @@ static inline void padlock_reload_key(vo
fd2893
     asm volatile ("pushfl; popfl");
fd2893
 }
fd2893
 
fd2893
-#    ifndef OPENSSL_NO_AES
fd2893
+#     ifndef OPENSSL_NO_AES
fd2893
 /*
fd2893
  * This is heuristic key context tracing. At first one
fd2893
  * believes that one should use atomic swap instructions,
fd2893
@@ -448,6 +436,101 @@ static inline void *name(size_t cnt,
fd2893
                 : "edx", "cc", "memory");       \
fd2893
         return iv;                              \
fd2893
 }
fd2893
+#     endif
fd2893
+
fd2893
+#    elif defined(__x86_64__) || defined(__x86_64)
fd2893
+
fd2893
+/* Load supported features of the CPU to see if
fd2893
+   the PadLock is available. */
fd2893
+static int padlock_available(void)
fd2893
+{
fd2893
+    char vendor_string[16];
fd2893
+    unsigned int eax, edx;
fd2893
+
fd2893
+    /* Are we running on the Centaur (VIA) CPU? */
fd2893
+    eax = 0x00000000;
fd2893
+    vendor_string[12] = 0;
fd2893
+    asm volatile ("cpuid\n"
fd2893
+                  "movl   %%ebx,(%1)\n"
fd2893
+                  "movl   %%edx,4(%1)\n"
fd2893
+                  "movl   %%ecx,8(%1)\n":"+a" (eax):"r"(vendor_string):"rbx",
fd2893
+                  "rcx", "rdx");
fd2893
+    if (strcmp(vendor_string, "CentaurHauls") != 0)
fd2893
+        return 0;
fd2893
+
fd2893
+    /* Check for Centaur Extended Feature Flags presence */
fd2893
+    eax = 0xC0000000;
fd2893
+    asm volatile ("cpuid":"+a" (eax)::"rbx", "rcx", "rdx");
fd2893
+    if (eax < 0xC0000001)
fd2893
+        return 0;
fd2893
+
fd2893
+    /* Read the Centaur Extended Feature Flags */
fd2893
+    eax = 0xC0000001;
fd2893
+    asm volatile ("cpuid":"+a" (eax), "=d"(edx)::"rbx", "rcx");
fd2893
+
fd2893
+    /* Fill up some flags */
fd2893
+    padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
fd2893
+    padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
fd2893
+
fd2893
+    return padlock_use_ace + padlock_use_rng;
fd2893
+}
fd2893
+
fd2893
+/* Force key reload from memory to the CPU microcode.
fd2893
+   Loading EFLAGS from the stack clears EFLAGS[30]
fd2893
+   which does the trick. */
fd2893
+static inline void padlock_reload_key(void)
fd2893
+{
fd2893
+    asm volatile ("pushfq; popfq");
fd2893
+}
fd2893
+
fd2893
+#     ifndef OPENSSL_NO_AES
fd2893
+/*
fd2893
+ * This is heuristic key context tracing. At first one
fd2893
+ * believes that one should use atomic swap instructions,
fd2893
+ * but it's not actually necessary. Point is that if
fd2893
+ * padlock_saved_context was changed by another thread
fd2893
+ * after we've read it and before we compare it with cdata,
fd2893
+ * our key *shall* be reloaded upon thread context switch
fd2893
+ * and we are therefore set in either case...
fd2893
+ */
fd2893
+static inline void padlock_verify_context(struct padlock_cipher_data *cdata)
fd2893
+{
fd2893
+    asm volatile ("pushfq\n"
fd2893
+                  "       btl     $30,(%%rsp)\n"
fd2893
+                  "       jnc     1f\n"
fd2893
+                  "       cmpq    %2,%1\n"
fd2893
+                  "       je      1f\n"
fd2893
+                  "       popfq\n"
fd2893
+                  "       subq    $8,%%rsp\n"
fd2893
+                  "1:     addq    $8,%%rsp\n"
fd2893
+                  "       movq    %2,%0":"+m" (padlock_saved_context)
fd2893
+                  :"r"(padlock_saved_context), "r"(cdata):"cc");
fd2893
+}
fd2893
+
fd2893
+/* Template for padlock_xcrypt_* modes */
fd2893
+/* BIG FAT WARNING:
fd2893
+ *      The offsets used with 'leal' instructions
fd2893
+ *      describe items of the 'padlock_cipher_data'
fd2893
+ *      structure.
fd2893
+ */
fd2893
+#      define PADLOCK_XCRYPT_ASM(name,rep_xcrypt)     \
fd2893
+static inline void *name(size_t cnt,            \
fd2893
+        struct padlock_cipher_data *cdata,      \
fd2893
+        void *out, const void *inp)             \
fd2893
+{       void *iv;                               \
fd2893
+        asm volatile ( "leaq    16(%0),%%rdx\n" \
fd2893
+                "       leaq    32(%0),%%rbx\n" \
fd2893
+                        rep_xcrypt "\n"         \
fd2893
+                : "=a"(iv), "=c"(cnt), "=D"(out), "=S"(inp) \
fd2893
+                : "0"(cdata), "1"(cnt), "2"(out), "3"(inp)  \
fd2893
+                : "rbx", "rdx", "cc", "memory");        \
fd2893
+        return iv;                              \
fd2893
+}
fd2893
+#     endif
fd2893
+
fd2893
+#    endif                      /* cpu */
fd2893
+
fd2893
+#    ifndef OPENSSL_NO_AES
fd2893
 
fd2893
 /* Generate all functions with appropriate opcodes */
fd2893
 /* rep xcryptecb */
fd2893
@@ -458,6 +541,20 @@ PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb, "
fd2893
     PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb, ".byte 0xf3,0x0f,0xa7,0xe0")
fd2893
 /* rep xcryptofb */
fd2893
     PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb, ".byte 0xf3,0x0f,0xa7,0xe8")
fd2893
+
fd2893
+#     ifndef AES_ASM
fd2893
+/* Our own htonl()/ntohl() */
fd2893
+static inline void padlock_bswapl(AES_KEY *ks)
fd2893
+{
fd2893
+    size_t i = sizeof(ks->rd_key) / sizeof(ks->rd_key[0]);
fd2893
+    unsigned int *key = ks->rd_key;
fd2893
+
fd2893
+    while (i--) {
fd2893
+        asm volatile ("bswapl %0":"+r" (*key));
fd2893
+        key++;
fd2893
+    }
fd2893
+}
fd2893
+#     endif
fd2893
 #    endif
fd2893
 /* The RNG call itself */
fd2893
 static inline unsigned int padlock_xstore(void *addr, unsigned int edx_in)
fd2893
@@ -485,8 +582,8 @@ static inline unsigned int padlock_xstor
fd2893
 static inline unsigned char *padlock_memcpy(void *dst, const void *src,
fd2893
                                             size_t n)
fd2893
 {
fd2893
-    long *d = dst;
fd2893
-    const long *s = src;
fd2893
+    size_t *d = dst;
fd2893
+    const size_t *s = src;
fd2893
 
fd2893
     n /= sizeof(*d);
fd2893
     do {