Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / arch / x86 / kernel / cpu / common.c
index 62590aa..9988caf 100644 (file)
@@ -303,6 +303,48 @@ static __always_inline void setup_smap(struct cpuinfo_x86 *c)
        }
 }
 
+/*
+ * Protection Keys are not available in 32-bit mode.
+ */
+static bool pku_disabled;
+
+static __always_inline void setup_pku(struct cpuinfo_x86 *c)
+{
+       if (!cpu_has(c, X86_FEATURE_PKU))
+               return;
+       if (pku_disabled)
+               return;
+
+       cr4_set_bits(X86_CR4_PKE);
+       /*
+        * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE
+        * cpuid bit to be set.  We need to ensure that we
+        * update that bit in this CPU's "cpu_info".
+        */
+       get_cpu_cap(c);
+}
+
+#ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS
+static __init int setup_disable_pku(char *arg)
+{
+       /*
+        * Do not clear the X86_FEATURE_PKU bit.  All of the
+        * runtime checks are against OSPKE so clearing the
+        * bit does nothing.
+        *
+        * This way, we will see "pku" in cpuinfo, but not
+        * "ospke", which is exactly what we want.  It shows
+        * that the CPU has PKU, but the OS has not enabled it.
+        * This happens to be exactly how a system would look
+        * if we disabled the config option.
+        */
+       pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n");
+       pku_disabled = true;
+       return 1;
+}
+__setup("nopku", setup_disable_pku);
+#endif /* CONFIG_X86_64 */
+
 /*
  * Some CPU features depend on higher CPUID levels, which may not always
  * be available due to CPUID level capping or broken virtualization
@@ -625,6 +667,7 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
                c->x86_capability[CPUID_7_0_EBX] = ebx;
 
                c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006);
+               c->x86_capability[CPUID_7_ECX] = ecx;
        }
 
        /* Extended state features: level 0x0000000d */
@@ -982,6 +1025,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
        init_hypervisor(c);
        x86_init_rdrand(c);
        x86_init_cache_qos(c);
+       setup_pku(c);
 
        /*
         * Clear/Set all flags overridden by options, need do it