Blame SOURCES/openssl-1.0.2a-padlock64.patch

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