Yinghai Lu | f0fc4af | 2008-09-04 20:09:00 -0700 | [diff] [blame] | 1 | #include <linux/bootmem.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 2 | #include <linux/linkage.h> |
Yinghai Lu | f0fc4af | 2008-09-04 20:09:00 -0700 | [diff] [blame] | 3 | #include <linux/bitops.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 4 | #include <linux/kernel.h> |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 5 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/percpu.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 7 | #include <linux/string.h> |
Borislav Petkov | ee098e1 | 2015-06-01 12:06:57 +0200 | [diff] [blame] | 8 | #include <linux/ctype.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 9 | #include <linux/delay.h> |
Ingo Molnar | 68e21be | 2017-02-01 19:08:20 +0100 | [diff] [blame] | 10 | #include <linux/sched/mm.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 11 | #include <linux/sched/clock.h> |
Ingo Molnar | 9164bb4 | 2017-02-04 01:20:53 +0100 | [diff] [blame] | 12 | #include <linux/sched/task.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 13 | #include <linux/init.h> |
Masami Hiramatsu | 0f46efeb | 2014-04-17 17:17:12 +0900 | [diff] [blame] | 14 | #include <linux/kprobes.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 15 | #include <linux/kgdb.h> |
| 16 | #include <linux/smp.h> |
| 17 | #include <linux/io.h> |
Laura Abbott | b51ef52 | 2015-07-20 14:47:58 -0700 | [diff] [blame] | 18 | #include <linux/syscore_ops.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 19 | |
| 20 | #include <asm/stackprotector.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 21 | #include <asm/perf_event.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/mmu_context.h> |
H. Peter Anvin | 49d859d | 2011-07-31 14:02:19 -0700 | [diff] [blame] | 23 | #include <asm/archrandom.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 24 | #include <asm/hypervisor.h> |
| 25 | #include <asm/processor.h> |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 26 | #include <asm/tlbflush.h> |
Paul Gortmaker | f649e93 | 2012-01-20 16:24:09 -0500 | [diff] [blame] | 27 | #include <asm/debugreg.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 28 | #include <asm/sections.h> |
Andy Lutomirski | f40c330 | 2014-05-05 12:19:36 -0700 | [diff] [blame] | 29 | #include <asm/vsyscall.h> |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 30 | #include <linux/topology.h> |
| 31 | #include <linux/cpumask.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 32 | #include <asm/pgtable.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 33 | #include <linux/atomic.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 34 | #include <asm/proto.h> |
| 35 | #include <asm/setup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/apic.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 37 | #include <asm/desc.h> |
Ingo Molnar | 78f7f1e | 2015-04-24 02:54:44 +0200 | [diff] [blame] | 38 | #include <asm/fpu/internal.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 39 | #include <asm/mtrr.h> |
Grzegorz Andrejczuk | 0274f95 | 2017-01-20 14:22:34 +0100 | [diff] [blame] | 40 | #include <asm/hwcap2.h> |
Alan Cox | 8bdbd96 | 2009-07-04 00:35:45 +0100 | [diff] [blame] | 41 | #include <linux/numa.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 42 | #include <asm/asm.h> |
Dave Hansen | 0f6ff2b | 2016-05-12 15:04:00 -0700 | [diff] [blame] | 43 | #include <asm/bugs.h> |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 44 | #include <asm/cpu.h> |
| 45 | #include <asm/mce.h> |
| 46 | #include <asm/msr.h> |
| 47 | #include <asm/pat.h> |
Fenghua Yu | d288e1c | 2012-12-20 23:44:23 -0800 | [diff] [blame] | 48 | #include <asm/microcode.h> |
| 49 | #include <asm/microcode_intel.h> |
Ingo Molnar | e641f5f | 2009-02-17 14:02:01 +0100 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_X86_LOCAL_APIC |
Tejun Heo | bdbcdd4 | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 52 | #include <asm/uv/uv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | #include "cpu.h" |
| 56 | |
Grzegorz Andrejczuk | 0274f95 | 2017-01-20 14:22:34 +0100 | [diff] [blame] | 57 | u32 elf_hwcap2 __read_mostly; |
| 58 | |
Mike Travis | c2d1cec | 2009-01-04 05:18:03 -0800 | [diff] [blame] | 59 | /* all of these masks are initialized in setup_cpu_local_masks() */ |
Mike Travis | c2d1cec | 2009-01-04 05:18:03 -0800 | [diff] [blame] | 60 | cpumask_var_t cpu_initialized_mask; |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 61 | cpumask_var_t cpu_callout_mask; |
| 62 | cpumask_var_t cpu_callin_mask; |
Mike Travis | c2d1cec | 2009-01-04 05:18:03 -0800 | [diff] [blame] | 63 | |
| 64 | /* representing cpus for which sibling maps can be computed */ |
| 65 | cpumask_var_t cpu_sibling_setup_mask; |
| 66 | |
Brian Gerst | 2f2f52b | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 67 | /* correctly size the local cpu masks */ |
Ingo Molnar | 4369f1f | 2009-01-27 12:03:24 +0100 | [diff] [blame] | 68 | void __init setup_cpu_local_masks(void) |
Brian Gerst | 2f2f52b | 2009-01-27 12:56:47 +0900 | [diff] [blame] | 69 | { |
| 70 | alloc_bootmem_cpumask_var(&cpu_initialized_mask); |
| 71 | alloc_bootmem_cpumask_var(&cpu_callin_mask); |
| 72 | alloc_bootmem_cpumask_var(&cpu_callout_mask); |
| 73 | alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask); |
| 74 | } |
| 75 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 76 | static void default_init(struct cpuinfo_x86 *c) |
Ondrej Zary | e805513 | 2009-08-11 20:00:11 +0200 | [diff] [blame] | 77 | { |
| 78 | #ifdef CONFIG_X86_64 |
Borislav Petkov | 27c13ec | 2009-11-21 14:01:45 +0100 | [diff] [blame] | 79 | cpu_detect_cache_sizes(c); |
Ondrej Zary | e805513 | 2009-08-11 20:00:11 +0200 | [diff] [blame] | 80 | #else |
| 81 | /* Not much we can do here... */ |
| 82 | /* Check if at least it has cpuid */ |
| 83 | if (c->cpuid_level == -1) { |
| 84 | /* No cpuid. It must be an ancient CPU */ |
| 85 | if (c->x86 == 4) |
| 86 | strcpy(c->x86_model_id, "486"); |
| 87 | else if (c->x86 == 3) |
| 88 | strcpy(c->x86_model_id, "386"); |
| 89 | } |
| 90 | #endif |
| 91 | } |
| 92 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 93 | static const struct cpu_dev default_cpu = { |
Ondrej Zary | e805513 | 2009-08-11 20:00:11 +0200 | [diff] [blame] | 94 | .c_init = default_init, |
| 95 | .c_vendor = "Unknown", |
| 96 | .c_x86_vendor = X86_VENDOR_UNKNOWN, |
| 97 | }; |
| 98 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 99 | static const struct cpu_dev *this_cpu = &default_cpu; |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 100 | |
Brian Gerst | 06deef8 | 2009-01-21 17:26:05 +0900 | [diff] [blame] | 101 | DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = { |
Yinghai Lu | 950ad7f | 2008-09-04 20:09:01 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_X86_64 |
Brian Gerst | 06deef8 | 2009-01-21 17:26:05 +0900 | [diff] [blame] | 103 | /* |
| 104 | * We need valid kernel segments for data and code in long mode too |
| 105 | * IRET will check the segment types kkeil 2000/10/28 |
| 106 | * Also sysret mandates a special GDT layout |
| 107 | * |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 108 | * TLS descriptors are currently at a different place compared to i386. |
Brian Gerst | 06deef8 | 2009-01-21 17:26:05 +0900 | [diff] [blame] | 109 | * Hopefully nobody expects them at a fixed place (Wine?) |
| 110 | */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 111 | [GDT_ENTRY_KERNEL32_CS] = GDT_ENTRY_INIT(0xc09b, 0, 0xfffff), |
| 112 | [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xa09b, 0, 0xfffff), |
| 113 | [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), |
| 114 | [GDT_ENTRY_DEFAULT_USER32_CS] = GDT_ENTRY_INIT(0xc0fb, 0, 0xfffff), |
| 115 | [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f3, 0, 0xfffff), |
| 116 | [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xa0fb, 0, 0xfffff), |
Yinghai Lu | 950ad7f | 2008-09-04 20:09:01 -0700 | [diff] [blame] | 117 | #else |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 118 | [GDT_ENTRY_KERNEL_CS] = GDT_ENTRY_INIT(0xc09a, 0, 0xfffff), |
| 119 | [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), |
| 120 | [GDT_ENTRY_DEFAULT_USER_CS] = GDT_ENTRY_INIT(0xc0fa, 0, 0xfffff), |
| 121 | [GDT_ENTRY_DEFAULT_USER_DS] = GDT_ENTRY_INIT(0xc0f2, 0, 0xfffff), |
Rusty Russell | bf504672 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 122 | /* |
| 123 | * Segments used for calling PnP BIOS have byte granularity. |
| 124 | * They code segments and data segments have fixed 64k limits, |
| 125 | * the transfer segment sizes are set at run time. |
| 126 | */ |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 127 | /* 32-bit code */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 128 | [GDT_ENTRY_PNPBIOS_CS32] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 129 | /* 16-bit code */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 130 | [GDT_ENTRY_PNPBIOS_CS16] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 131 | /* 16-bit data */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 132 | [GDT_ENTRY_PNPBIOS_DS] = GDT_ENTRY_INIT(0x0092, 0, 0xffff), |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 133 | /* 16-bit data */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 134 | [GDT_ENTRY_PNPBIOS_TS1] = GDT_ENTRY_INIT(0x0092, 0, 0), |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 135 | /* 16-bit data */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 136 | [GDT_ENTRY_PNPBIOS_TS2] = GDT_ENTRY_INIT(0x0092, 0, 0), |
Rusty Russell | bf504672 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 137 | /* |
| 138 | * The APM segments have byte granularity and their bases |
| 139 | * are set at run time. All have 64k limits. |
| 140 | */ |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 141 | /* 32-bit code */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 142 | [GDT_ENTRY_APMBIOS_BASE] = GDT_ENTRY_INIT(0x409a, 0, 0xffff), |
Rusty Russell | bf504672 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 143 | /* 16-bit code */ |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 144 | [GDT_ENTRY_APMBIOS_BASE+1] = GDT_ENTRY_INIT(0x009a, 0, 0xffff), |
Glauber de Oliveira Costa | 6842ef0 | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 145 | /* data */ |
Ingo Molnar | 72c4d85 | 2009-08-03 08:47:07 +0200 | [diff] [blame] | 146 | [GDT_ENTRY_APMBIOS_BASE+2] = GDT_ENTRY_INIT(0x4092, 0, 0xffff), |
Rusty Russell | bf504672 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 147 | |
Akinobu Mita | 1e5de18 | 2009-07-19 00:12:20 +0900 | [diff] [blame] | 148 | [GDT_ENTRY_ESPFIX_SS] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), |
| 149 | [GDT_ENTRY_PERCPU] = GDT_ENTRY_INIT(0xc092, 0, 0xfffff), |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 150 | GDT_STACK_CANARY_INIT |
Yinghai Lu | 950ad7f | 2008-09-04 20:09:01 -0700 | [diff] [blame] | 151 | #endif |
Brian Gerst | 06deef8 | 2009-01-21 17:26:05 +0900 | [diff] [blame] | 152 | } }; |
Jeremy Fitzhardinge | 7a61d35 | 2007-05-02 19:27:15 +0200 | [diff] [blame] | 153 | EXPORT_PER_CPU_SYMBOL_GPL(gdt_page); |
Rusty Russell | ae1ee11 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 154 | |
Dave Hansen | 8c3641e | 2015-06-07 11:37:02 -0700 | [diff] [blame] | 155 | static int __init x86_mpx_setup(char *s) |
Suresh Siddha | 0c752a9 | 2009-05-22 12:17:45 -0700 | [diff] [blame] | 156 | { |
Dave Hansen | 8c3641e | 2015-06-07 11:37:02 -0700 | [diff] [blame] | 157 | /* require an exact match without trailing characters */ |
Dave Hansen | 2cd3949 | 2014-11-11 14:01:33 -0800 | [diff] [blame] | 158 | if (strlen(s)) |
| 159 | return 0; |
Suresh Siddha | 0c752a9 | 2009-05-22 12:17:45 -0700 | [diff] [blame] | 160 | |
Dave Hansen | 8c3641e | 2015-06-07 11:37:02 -0700 | [diff] [blame] | 161 | /* do not emit a message if the feature is not present */ |
| 162 | if (!boot_cpu_has(X86_FEATURE_MPX)) |
| 163 | return 1; |
Suresh Siddha | 6bad06b | 2010-07-19 16:05:52 -0700 | [diff] [blame] | 164 | |
Dave Hansen | 8c3641e | 2015-06-07 11:37:02 -0700 | [diff] [blame] | 165 | setup_clear_cpu_cap(X86_FEATURE_MPX); |
| 166 | pr_info("nompx: Intel Memory Protection Extensions (MPX) disabled\n"); |
Fenghua Yu | b6f42a4 | 2014-05-29 11:12:31 -0700 | [diff] [blame] | 167 | return 1; |
| 168 | } |
Dave Hansen | 8c3641e | 2015-06-07 11:37:02 -0700 | [diff] [blame] | 169 | __setup("nompx", x86_mpx_setup); |
Fenghua Yu | b6f42a4 | 2014-05-29 11:12:31 -0700 | [diff] [blame] | 170 | |
Andy Lutomirski | 0790c9a | 2017-06-29 08:53:20 -0700 | [diff] [blame] | 171 | #ifdef CONFIG_X86_64 |
Andy Lutomirski | c7ad5ad | 2017-09-10 17:48:27 -0700 | [diff] [blame] | 172 | static int __init x86_nopcid_setup(char *s) |
Andy Lutomirski | 0790c9a | 2017-06-29 08:53:20 -0700 | [diff] [blame] | 173 | { |
Andy Lutomirski | c7ad5ad | 2017-09-10 17:48:27 -0700 | [diff] [blame] | 174 | /* nopcid doesn't accept parameters */ |
| 175 | if (s) |
| 176 | return -EINVAL; |
Andy Lutomirski | 0790c9a | 2017-06-29 08:53:20 -0700 | [diff] [blame] | 177 | |
| 178 | /* do not emit a message if the feature is not present */ |
| 179 | if (!boot_cpu_has(X86_FEATURE_PCID)) |
Andy Lutomirski | c7ad5ad | 2017-09-10 17:48:27 -0700 | [diff] [blame] | 180 | return 0; |
Andy Lutomirski | 0790c9a | 2017-06-29 08:53:20 -0700 | [diff] [blame] | 181 | |
| 182 | setup_clear_cpu_cap(X86_FEATURE_PCID); |
| 183 | pr_info("nopcid: PCID feature disabled\n"); |
Andy Lutomirski | c7ad5ad | 2017-09-10 17:48:27 -0700 | [diff] [blame] | 184 | return 0; |
Andy Lutomirski | 0790c9a | 2017-06-29 08:53:20 -0700 | [diff] [blame] | 185 | } |
Andy Lutomirski | c7ad5ad | 2017-09-10 17:48:27 -0700 | [diff] [blame] | 186 | early_param("nopcid", x86_nopcid_setup); |
Andy Lutomirski | 0790c9a | 2017-06-29 08:53:20 -0700 | [diff] [blame] | 187 | #endif |
| 188 | |
Andy Lutomirski | d12a72b | 2016-01-29 11:42:58 -0800 | [diff] [blame] | 189 | static int __init x86_noinvpcid_setup(char *s) |
| 190 | { |
| 191 | /* noinvpcid doesn't accept parameters */ |
| 192 | if (s) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | /* do not emit a message if the feature is not present */ |
| 196 | if (!boot_cpu_has(X86_FEATURE_INVPCID)) |
| 197 | return 0; |
| 198 | |
| 199 | setup_clear_cpu_cap(X86_FEATURE_INVPCID); |
| 200 | pr_info("noinvpcid: INVPCID feature disabled\n"); |
| 201 | return 0; |
| 202 | } |
| 203 | early_param("noinvpcid", x86_noinvpcid_setup); |
| 204 | |
Yinghai Lu | ba51dce | 2008-09-04 20:09:02 -0700 | [diff] [blame] | 205 | #ifdef CONFIG_X86_32 |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 206 | static int cachesize_override = -1; |
| 207 | static int disable_x86_serial_nr = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | static int __init cachesize_setup(char *str) |
| 210 | { |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 211 | get_option(&str, &cachesize_override); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | return 1; |
| 213 | } |
| 214 | __setup("cachesize=", cachesize_setup); |
| 215 | |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 216 | static int __init x86_sep_setup(char *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Andi Kleen | 1353025 | 2008-01-30 13:33:20 +0100 | [diff] [blame] | 218 | setup_clear_cpu_cap(X86_FEATURE_SEP); |
Chuck Ebbert | 4f88651 | 2006-03-23 02:59:34 -0800 | [diff] [blame] | 219 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
Chuck Ebbert | 4f88651 | 2006-03-23 02:59:34 -0800 | [diff] [blame] | 221 | __setup("nosep", x86_sep_setup); |
| 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* Standard macro to see if a specific flag is changeable */ |
| 224 | static inline int flag_is_changeable_p(u32 flag) |
| 225 | { |
| 226 | u32 f1, f2; |
| 227 | |
Krzysztof Helt | 94f6bac | 2008-09-30 23:17:51 +0200 | [diff] [blame] | 228 | /* |
| 229 | * Cyrix and IDT cpus allow disabling of CPUID |
| 230 | * so the code below may return different results |
| 231 | * when it is executed before and after enabling |
| 232 | * the CPUID. Add "volatile" to not allow gcc to |
| 233 | * optimize the subsequent calls to this function. |
| 234 | */ |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 235 | asm volatile ("pushfl \n\t" |
| 236 | "pushfl \n\t" |
| 237 | "popl %0 \n\t" |
| 238 | "movl %0, %1 \n\t" |
| 239 | "xorl %2, %0 \n\t" |
| 240 | "pushl %0 \n\t" |
| 241 | "popfl \n\t" |
| 242 | "pushfl \n\t" |
| 243 | "popl %0 \n\t" |
| 244 | "popfl \n\t" |
| 245 | |
Krzysztof Helt | 94f6bac | 2008-09-30 23:17:51 +0200 | [diff] [blame] | 246 | : "=&r" (f1), "=&r" (f2) |
| 247 | : "ir" (flag)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | return ((f1^f2) & flag) != 0; |
| 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* Probe for the CPUID instruction */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 253 | int have_cpuid_p(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
| 255 | return flag_is_changeable_p(X86_EFLAGS_ID); |
| 256 | } |
| 257 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 258 | static void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 259 | { |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 260 | unsigned long lo, hi; |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 261 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 262 | if (!cpu_has(c, X86_FEATURE_PN) || !disable_x86_serial_nr) |
| 263 | return; |
| 264 | |
| 265 | /* Disable processor serial number: */ |
| 266 | |
| 267 | rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi); |
| 268 | lo |= 0x200000; |
| 269 | wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi); |
| 270 | |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 271 | pr_notice("CPU serial number disabled.\n"); |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 272 | clear_cpu_cap(c, X86_FEATURE_PN); |
| 273 | |
| 274 | /* Disabling the serial number may affect the cpuid level */ |
| 275 | c->cpuid_level = cpuid_eax(0); |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static int __init x86_serial_nr_setup(char *s) |
| 279 | { |
| 280 | disable_x86_serial_nr = 0; |
| 281 | return 1; |
| 282 | } |
| 283 | __setup("serialnumber", x86_serial_nr_setup); |
Yinghai Lu | ba51dce | 2008-09-04 20:09:02 -0700 | [diff] [blame] | 284 | #else |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 285 | static inline int flag_is_changeable_p(u32 flag) |
| 286 | { |
| 287 | return 1; |
| 288 | } |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 289 | static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c) |
| 290 | { |
| 291 | } |
Yinghai Lu | ba51dce | 2008-09-04 20:09:02 -0700 | [diff] [blame] | 292 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Fenghua Yu | de5397a | 2011-05-11 16:51:05 -0700 | [diff] [blame] | 294 | static __init int setup_disable_smep(char *arg) |
| 295 | { |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 296 | setup_clear_cpu_cap(X86_FEATURE_SMEP); |
Dave Hansen | 0f6ff2b | 2016-05-12 15:04:00 -0700 | [diff] [blame] | 297 | /* Check for things that depend on SMEP being enabled: */ |
| 298 | check_mpx_erratum(&boot_cpu_data); |
Fenghua Yu | de5397a | 2011-05-11 16:51:05 -0700 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | __setup("nosmep", setup_disable_smep); |
| 302 | |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 303 | static __always_inline void setup_smep(struct cpuinfo_x86 *c) |
Fenghua Yu | de5397a | 2011-05-11 16:51:05 -0700 | [diff] [blame] | 304 | { |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 305 | if (cpu_has(c, X86_FEATURE_SMEP)) |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 306 | cr4_set_bits(X86_CR4_SMEP); |
Fenghua Yu | de5397a | 2011-05-11 16:51:05 -0700 | [diff] [blame] | 307 | } |
| 308 | |
H. Peter Anvin | 52b6179 | 2012-09-21 12:43:13 -0700 | [diff] [blame] | 309 | static __init int setup_disable_smap(char *arg) |
| 310 | { |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 311 | setup_clear_cpu_cap(X86_FEATURE_SMAP); |
H. Peter Anvin | 52b6179 | 2012-09-21 12:43:13 -0700 | [diff] [blame] | 312 | return 1; |
| 313 | } |
| 314 | __setup("nosmap", setup_disable_smap); |
| 315 | |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 316 | static __always_inline void setup_smap(struct cpuinfo_x86 *c) |
H. Peter Anvin | 52b6179 | 2012-09-21 12:43:13 -0700 | [diff] [blame] | 317 | { |
Andrew Cooper | 581b7f15 | 2015-06-03 10:31:14 +0100 | [diff] [blame] | 318 | unsigned long eflags = native_save_fl(); |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 319 | |
| 320 | /* This should have been cleared long ago */ |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 321 | BUG_ON(eflags & X86_EFLAGS_AC); |
| 322 | |
H. Peter Anvin | 03bbd59 | 2014-02-13 07:34:30 -0800 | [diff] [blame] | 323 | if (cpu_has(c, X86_FEATURE_SMAP)) { |
| 324 | #ifdef CONFIG_X86_SMAP |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 325 | cr4_set_bits(X86_CR4_SMAP); |
H. Peter Anvin | 03bbd59 | 2014-02-13 07:34:30 -0800 | [diff] [blame] | 326 | #else |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 327 | cr4_clear_bits(X86_CR4_SMAP); |
H. Peter Anvin | 03bbd59 | 2014-02-13 07:34:30 -0800 | [diff] [blame] | 328 | #endif |
| 329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Ricardo Neri | aa35f89 | 2017-11-05 18:27:54 -0800 | [diff] [blame] | 332 | static __always_inline void setup_umip(struct cpuinfo_x86 *c) |
| 333 | { |
| 334 | /* Check the boot processor, plus build option for UMIP. */ |
| 335 | if (!cpu_feature_enabled(X86_FEATURE_UMIP)) |
| 336 | goto out; |
| 337 | |
| 338 | /* Check the current processor's cpuid bits. */ |
| 339 | if (!cpu_has(c, X86_FEATURE_UMIP)) |
| 340 | goto out; |
| 341 | |
| 342 | cr4_set_bits(X86_CR4_UMIP); |
| 343 | |
Ricardo Neri | 770c775 | 2017-11-13 22:29:43 -0800 | [diff] [blame] | 344 | pr_info("x86/cpu: Activated the Intel User Mode Instruction Prevention (UMIP) CPU feature\n"); |
| 345 | |
Ricardo Neri | aa35f89 | 2017-11-05 18:27:54 -0800 | [diff] [blame] | 346 | return; |
| 347 | |
| 348 | out: |
| 349 | /* |
| 350 | * Make sure UMIP is disabled in case it was enabled in a |
| 351 | * previous boot (e.g., via kexec). |
| 352 | */ |
| 353 | cr4_clear_bits(X86_CR4_UMIP); |
| 354 | } |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* |
Dave Hansen | 0697694 | 2016-02-12 13:02:29 -0800 | [diff] [blame] | 357 | * Protection Keys are not available in 32-bit mode. |
| 358 | */ |
| 359 | static bool pku_disabled; |
| 360 | |
| 361 | static __always_inline void setup_pku(struct cpuinfo_x86 *c) |
| 362 | { |
Dave Hansen | e8df1a95 | 2016-05-13 15:13:28 -0700 | [diff] [blame] | 363 | /* check the boot processor, plus compile options for PKU: */ |
| 364 | if (!cpu_feature_enabled(X86_FEATURE_PKU)) |
| 365 | return; |
| 366 | /* checks the actual processor's cpuid bits: */ |
Dave Hansen | 0697694 | 2016-02-12 13:02:29 -0800 | [diff] [blame] | 367 | if (!cpu_has(c, X86_FEATURE_PKU)) |
| 368 | return; |
| 369 | if (pku_disabled) |
| 370 | return; |
| 371 | |
| 372 | cr4_set_bits(X86_CR4_PKE); |
| 373 | /* |
| 374 | * Seting X86_CR4_PKE will cause the X86_FEATURE_OSPKE |
| 375 | * cpuid bit to be set. We need to ensure that we |
| 376 | * update that bit in this CPU's "cpu_info". |
| 377 | */ |
| 378 | get_cpu_cap(c); |
| 379 | } |
| 380 | |
| 381 | #ifdef CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS |
| 382 | static __init int setup_disable_pku(char *arg) |
| 383 | { |
| 384 | /* |
| 385 | * Do not clear the X86_FEATURE_PKU bit. All of the |
| 386 | * runtime checks are against OSPKE so clearing the |
| 387 | * bit does nothing. |
| 388 | * |
| 389 | * This way, we will see "pku" in cpuinfo, but not |
| 390 | * "ospke", which is exactly what we want. It shows |
| 391 | * that the CPU has PKU, but the OS has not enabled it. |
| 392 | * This happens to be exactly how a system would look |
| 393 | * if we disabled the config option. |
| 394 | */ |
| 395 | pr_info("x86: 'nopku' specified, disabling Memory Protection Keys\n"); |
| 396 | pku_disabled = true; |
| 397 | return 1; |
| 398 | } |
| 399 | __setup("nopku", setup_disable_pku); |
| 400 | #endif /* CONFIG_X86_64 */ |
| 401 | |
| 402 | /* |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 403 | * Some CPU features depend on higher CPUID levels, which may not always |
| 404 | * be available due to CPUID level capping or broken virtualization |
| 405 | * software. Add those features to this table to auto-disable them. |
| 406 | */ |
| 407 | struct cpuid_dependent_feature { |
| 408 | u32 feature; |
| 409 | u32 level; |
| 410 | }; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 411 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 412 | static const struct cpuid_dependent_feature |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 413 | cpuid_dependent_features[] = { |
| 414 | { X86_FEATURE_MWAIT, 0x00000005 }, |
| 415 | { X86_FEATURE_DCA, 0x00000009 }, |
| 416 | { X86_FEATURE_XSAVE, 0x0000000d }, |
| 417 | { 0, 0 } |
| 418 | }; |
| 419 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 420 | static void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 421 | { |
| 422 | const struct cpuid_dependent_feature *df; |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 423 | |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 424 | for (df = cpuid_dependent_features; df->feature; df++) { |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 425 | |
| 426 | if (!cpu_has(c, df->feature)) |
| 427 | continue; |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 428 | /* |
| 429 | * Note: cpuid_level is set to -1 if unavailable, but |
| 430 | * extended_extended_level is set to 0 if unavailable |
| 431 | * and the legitimate extended levels are all negative |
| 432 | * when signed; hence the weird messing around with |
| 433 | * signs here... |
| 434 | */ |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 435 | if (!((s32)df->level < 0 ? |
Yinghai Lu | f6db44d | 2009-02-14 23:59:18 -0800 | [diff] [blame] | 436 | (u32)df->level > (u32)c->extended_cpuid_level : |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 437 | (s32)df->level > (s32)c->cpuid_level)) |
| 438 | continue; |
| 439 | |
| 440 | clear_cpu_cap(c, df->feature); |
| 441 | if (!warn) |
| 442 | continue; |
| 443 | |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 444 | pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n", |
| 445 | x86_cap_flag(df->feature), df->level); |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 446 | } |
Yinghai Lu | f6db44d | 2009-02-14 23:59:18 -0800 | [diff] [blame] | 447 | } |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 448 | |
| 449 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | * Naming convention should be: <Name> [(<Codename>)] |
| 451 | * This table only is used unless init_<vendor>() below doesn't set it; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 452 | * in particular, if CPUID levels 0x80000002..4 are supported, this |
| 453 | * isn't used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | */ |
| 455 | |
| 456 | /* Look up CPU names by table lookup. */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 457 | static const char *table_lookup_model(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Jan Beulich | 09dc68d | 2013-10-21 09:35:20 +0100 | [diff] [blame] | 459 | #ifdef CONFIG_X86_32 |
| 460 | const struct legacy_cpu_model_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | if (c->x86_model >= 16) |
| 463 | return NULL; /* Range check */ |
| 464 | |
| 465 | if (!this_cpu) |
| 466 | return NULL; |
| 467 | |
Jan Beulich | 09dc68d | 2013-10-21 09:35:20 +0100 | [diff] [blame] | 468 | info = this_cpu->legacy_models; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Jan Beulich | 09dc68d | 2013-10-21 09:35:20 +0100 | [diff] [blame] | 470 | while (info->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (info->family == c->x86) |
| 472 | return info->model_names[c->x86_model]; |
| 473 | info++; |
| 474 | } |
Jan Beulich | 09dc68d | 2013-10-21 09:35:20 +0100 | [diff] [blame] | 475 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | return NULL; /* Not found */ |
| 477 | } |
| 478 | |
Thomas Gleixner | 6cbd217 | 2017-12-04 15:07:32 +0100 | [diff] [blame] | 479 | __u32 cpu_caps_cleared[NCAPINTS + NBUGINTS]; |
| 480 | __u32 cpu_caps_set[NCAPINTS + NBUGINTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Jeremy Fitzhardinge | 11e3a84 | 2009-01-30 17:47:54 +0900 | [diff] [blame] | 482 | void load_percpu_segment(int cpu) |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 483 | { |
Yinghai Lu | fab334c | 2008-09-04 20:09:05 -0700 | [diff] [blame] | 484 | #ifdef CONFIG_X86_32 |
Brian Gerst | 2697fbd | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 485 | loadsegment(fs, __KERNEL_PERCPU); |
| 486 | #else |
Andy Lutomirski | 45e876f | 2016-04-26 12:23:26 -0700 | [diff] [blame] | 487 | __loadsegment_simple(gs, 0); |
Brian Gerst | 2697fbd | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 488 | wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu)); |
Yinghai Lu | fab334c | 2008-09-04 20:09:05 -0700 | [diff] [blame] | 489 | #endif |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 490 | load_stack_canary_segment(); |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 493 | #ifdef CONFIG_X86_32 |
| 494 | /* The 32-bit entry code needs to find cpu_entry_area. */ |
| 495 | DEFINE_PER_CPU(struct cpu_entry_area *, cpu_entry_area); |
| 496 | #endif |
| 497 | |
Andy Lutomirski | 40e7f94 | 2017-12-04 15:07:26 +0100 | [diff] [blame] | 498 | #ifdef CONFIG_X86_64 |
| 499 | /* |
| 500 | * Special IST stacks which the CPU switches to when it calls |
| 501 | * an IST-marked descriptor entry. Up to 7 stacks (hardware |
| 502 | * limit), all of them are 4K, except the debug stack which |
| 503 | * is 8K. |
| 504 | */ |
| 505 | static const unsigned int exception_stack_sizes[N_EXCEPTION_STACKS] = { |
| 506 | [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ, |
| 507 | [DEBUG_STACK - 1] = DEBUG_STKSZ |
| 508 | }; |
Andy Lutomirski | 40e7f94 | 2017-12-04 15:07:26 +0100 | [diff] [blame] | 509 | #endif |
| 510 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 511 | /* Load the original GDT from the per-cpu structure */ |
| 512 | void load_direct_gdt(int cpu) |
| 513 | { |
| 514 | struct desc_ptr gdt_descr; |
| 515 | |
| 516 | gdt_descr.address = (long)get_cpu_gdt_rw(cpu); |
| 517 | gdt_descr.size = GDT_SIZE - 1; |
| 518 | load_gdt(&gdt_descr); |
| 519 | } |
| 520 | EXPORT_SYMBOL_GPL(load_direct_gdt); |
| 521 | |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 522 | /* Load a fixmap remapping of the per-cpu GDT */ |
| 523 | void load_fixmap_gdt(int cpu) |
| 524 | { |
| 525 | struct desc_ptr gdt_descr; |
| 526 | |
| 527 | gdt_descr.address = (long)get_cpu_gdt_ro(cpu); |
| 528 | gdt_descr.size = GDT_SIZE - 1; |
| 529 | load_gdt(&gdt_descr); |
| 530 | } |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 531 | EXPORT_SYMBOL_GPL(load_fixmap_gdt); |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 532 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 533 | /* |
| 534 | * Current gdt points %fs at the "master" per-cpu area: after this, |
| 535 | * it's on the real one. |
| 536 | */ |
Brian Gerst | 552be87 | 2009-01-30 17:47:53 +0900 | [diff] [blame] | 537 | void switch_to_new_gdt(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 539 | /* Load the original GDT */ |
| 540 | load_direct_gdt(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /* Reload the per-cpu base */ |
Jeremy Fitzhardinge | 11e3a84 | 2009-01-30 17:47:54 +0900 | [diff] [blame] | 542 | load_percpu_segment(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 545 | static const struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 547 | static void get_model_name(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | { |
| 549 | unsigned int *v; |
Borislav Petkov | ee098e1 | 2015-06-01 12:06:57 +0200 | [diff] [blame] | 550 | char *p, *q, *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 552 | if (c->extended_cpuid_level < 0x80000004) |
Yinghai Lu | 1b05d60 | 2008-09-06 01:52:27 -0700 | [diff] [blame] | 553 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 555 | v = (unsigned int *)c->x86_model_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]); |
| 557 | cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]); |
| 558 | cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]); |
| 559 | c->x86_model_id[48] = 0; |
| 560 | |
Borislav Petkov | ee098e1 | 2015-06-01 12:06:57 +0200 | [diff] [blame] | 561 | /* Trim whitespace */ |
| 562 | p = q = s = &c->x86_model_id[0]; |
| 563 | |
| 564 | while (*p == ' ') |
| 565 | p++; |
| 566 | |
| 567 | while (*p) { |
| 568 | /* Note the last non-whitespace index */ |
| 569 | if (!isspace(*p)) |
| 570 | s = q; |
| 571 | |
| 572 | *q++ = *p++; |
| 573 | } |
| 574 | |
| 575 | *(s + 1) = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 578 | void cpu_detect_cache_sizes(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 580 | unsigned int n, dummy, ebx, ecx, edx, l2size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 582 | n = c->extended_cpuid_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | if (n >= 0x80000005) { |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 585 | cpuid(0x80000005, &dummy, &ebx, &ecx, &edx); |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 586 | c->x86_cache_size = (ecx>>24) + (edx>>24); |
Yinghai Lu | 140fc72 | 2008-09-04 20:09:07 -0700 | [diff] [blame] | 587 | #ifdef CONFIG_X86_64 |
| 588 | /* On K8 L1 TLB is inclusive, so don't count it */ |
| 589 | c->x86_tlbsize = 0; |
| 590 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (n < 0x80000006) /* Some chips just has a large L1. */ |
| 594 | return; |
| 595 | |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 596 | cpuid(0x80000006, &dummy, &ebx, &ecx, &edx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | l2size = ecx >> 16; |
| 598 | |
Yinghai Lu | 140fc72 | 2008-09-04 20:09:07 -0700 | [diff] [blame] | 599 | #ifdef CONFIG_X86_64 |
| 600 | c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff); |
| 601 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | /* do processor-specific cache resizing */ |
Jan Beulich | 09dc68d | 2013-10-21 09:35:20 +0100 | [diff] [blame] | 603 | if (this_cpu->legacy_cache_size) |
| 604 | l2size = this_cpu->legacy_cache_size(c, l2size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | /* Allow user to override all this if necessary. */ |
| 607 | if (cachesize_override != -1) |
| 608 | l2size = cachesize_override; |
| 609 | |
| 610 | if (l2size == 0) |
| 611 | return; /* Again, no L2 cache is possible */ |
Yinghai Lu | 140fc72 | 2008-09-04 20:09:07 -0700 | [diff] [blame] | 612 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | c->x86_cache_size = l2size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Alex Shi | e0ba94f | 2012-06-28 09:02:16 +0800 | [diff] [blame] | 617 | u16 __read_mostly tlb_lli_4k[NR_INFO]; |
| 618 | u16 __read_mostly tlb_lli_2m[NR_INFO]; |
| 619 | u16 __read_mostly tlb_lli_4m[NR_INFO]; |
| 620 | u16 __read_mostly tlb_lld_4k[NR_INFO]; |
| 621 | u16 __read_mostly tlb_lld_2m[NR_INFO]; |
| 622 | u16 __read_mostly tlb_lld_4m[NR_INFO]; |
Kirill A. Shutemov | dd36039 | 2013-12-23 14:16:58 +0200 | [diff] [blame] | 623 | u16 __read_mostly tlb_lld_1g[NR_INFO]; |
Alex Shi | e0ba94f | 2012-06-28 09:02:16 +0800 | [diff] [blame] | 624 | |
Steven Honeyman | f94fe11 | 2014-11-05 22:52:18 +0000 | [diff] [blame] | 625 | static void cpu_detect_tlb(struct cpuinfo_x86 *c) |
Alex Shi | e0ba94f | 2012-06-28 09:02:16 +0800 | [diff] [blame] | 626 | { |
| 627 | if (this_cpu->c_detect_tlb) |
| 628 | this_cpu->c_detect_tlb(c); |
| 629 | |
Steven Honeyman | f94fe11 | 2014-11-05 22:52:18 +0000 | [diff] [blame] | 630 | pr_info("Last level iTLB entries: 4KB %d, 2MB %d, 4MB %d\n", |
Alex Shi | e0ba94f | 2012-06-28 09:02:16 +0800 | [diff] [blame] | 631 | tlb_lli_4k[ENTRIES], tlb_lli_2m[ENTRIES], |
Steven Honeyman | f94fe11 | 2014-11-05 22:52:18 +0000 | [diff] [blame] | 632 | tlb_lli_4m[ENTRIES]); |
| 633 | |
| 634 | pr_info("Last level dTLB entries: 4KB %d, 2MB %d, 4MB %d, 1GB %d\n", |
| 635 | tlb_lld_4k[ENTRIES], tlb_lld_2m[ENTRIES], |
| 636 | tlb_lld_4m[ENTRIES], tlb_lld_1g[ENTRIES]); |
Alex Shi | e0ba94f | 2012-06-28 09:02:16 +0800 | [diff] [blame] | 637 | } |
| 638 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 639 | void detect_ht(struct cpuinfo_x86 *c) |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 640 | { |
Borislav Petkov | c8e56d2 | 2015-06-04 18:55:25 +0200 | [diff] [blame] | 641 | #ifdef CONFIG_SMP |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 642 | u32 eax, ebx, ecx, edx; |
| 643 | int index_msb, core_bits; |
Mike Travis | 2eaad1f | 2009-12-10 17:19:36 -0800 | [diff] [blame] | 644 | static bool printed; |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 645 | |
| 646 | if (!cpu_has(c, X86_FEATURE_HT)) |
| 647 | return; |
| 648 | |
| 649 | if (cpu_has(c, X86_FEATURE_CMP_LEGACY)) |
| 650 | goto out; |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 651 | |
Yinghai Lu | 1cd7877 | 2008-09-04 20:09:08 -0700 | [diff] [blame] | 652 | if (cpu_has(c, X86_FEATURE_XTOPOLOGY)) |
| 653 | return; |
| 654 | |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 655 | cpuid(1, &eax, &ebx, &ecx, &edx); |
| 656 | |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 657 | smp_num_siblings = (ebx & 0xff0000) >> 16; |
| 658 | |
| 659 | if (smp_num_siblings == 1) { |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 660 | pr_info_once("CPU0: Hyper-Threading is disabled\n"); |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 661 | goto out; |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 662 | } |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 663 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 664 | if (smp_num_siblings <= 1) |
| 665 | goto out; |
| 666 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 667 | index_msb = get_count_order(smp_num_siblings); |
| 668 | c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb); |
| 669 | |
| 670 | smp_num_siblings = smp_num_siblings / c->x86_max_cores; |
| 671 | |
| 672 | index_msb = get_count_order(smp_num_siblings); |
| 673 | |
| 674 | core_bits = get_count_order(c->x86_max_cores); |
| 675 | |
| 676 | c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) & |
| 677 | ((1 << core_bits) - 1); |
| 678 | |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 679 | out: |
Mike Travis | 2eaad1f | 2009-12-10 17:19:36 -0800 | [diff] [blame] | 680 | if (!printed && (c->x86_max_cores * smp_num_siblings) > 1) { |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 681 | pr_info("CPU: Physical Processor ID: %d\n", |
| 682 | c->phys_proc_id); |
| 683 | pr_info("CPU: Processor Core ID: %d\n", |
| 684 | c->cpu_core_id); |
Mike Travis | 2eaad1f | 2009-12-10 17:19:36 -0800 | [diff] [blame] | 685 | printed = 1; |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 686 | } |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 687 | #endif |
Yinghai Lu | 97e4db7 | 2008-09-04 20:08:59 -0700 | [diff] [blame] | 688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 690 | static void get_cpu_vendor(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | char *v = c->x86_vendor_id; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 693 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | for (i = 0; i < X86_VENDOR_NUM; i++) { |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 696 | if (!cpu_devs[i]) |
| 697 | break; |
| 698 | |
| 699 | if (!strcmp(v, cpu_devs[i]->c_ident[0]) || |
| 700 | (cpu_devs[i]->c_ident[1] && |
| 701 | !strcmp(v, cpu_devs[i]->c_ident[1]))) { |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 702 | |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 703 | this_cpu = cpu_devs[i]; |
| 704 | c->x86_vendor = this_cpu->c_x86_vendor; |
| 705 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
| 707 | } |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 708 | |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 709 | pr_err_once("CPU: vendor_id '%s' unknown, using generic init.\n" \ |
| 710 | "CPU: Your system may be unstable.\n", v); |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 711 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | c->x86_vendor = X86_VENDOR_UNKNOWN; |
| 713 | this_cpu = &default_cpu; |
| 714 | } |
| 715 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 716 | void cpu_detect(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | /* Get vendor name */ |
Harvey Harrison | 4a14851 | 2008-02-01 17:49:43 +0100 | [diff] [blame] | 719 | cpuid(0x00000000, (unsigned int *)&c->cpuid_level, |
| 720 | (unsigned int *)&c->x86_vendor_id[0], |
| 721 | (unsigned int *)&c->x86_vendor_id[8], |
| 722 | (unsigned int *)&c->x86_vendor_id[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | c->x86 = 4; |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 725 | /* Intel-defined flags: level 0x00000001 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | if (c->cpuid_level >= 0x00000001) { |
| 727 | u32 junk, tfms, cap0, misc; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | cpuid(0x00000001, &tfms, &misc, &junk, &cap0); |
Borislav Petkov | 99f925c | 2015-11-23 11:12:21 +0100 | [diff] [blame] | 730 | c->x86 = x86_family(tfms); |
| 731 | c->x86_model = x86_model(tfms); |
| 732 | c->x86_mask = x86_stepping(tfms); |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 733 | |
Huang, Ying | d4387bd | 2008-01-31 22:05:45 +0100 | [diff] [blame] | 734 | if (cap0 & (1<<19)) { |
Huang, Ying | d4387bd | 2008-01-31 22:05:45 +0100 | [diff] [blame] | 735 | c->x86_clflush_size = ((misc >> 8) & 0xff) * 8; |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 736 | c->x86_cache_alignment = c->x86_clflush_size; |
Huang, Ying | d4387bd | 2008-01-31 22:05:45 +0100 | [diff] [blame] | 737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | } |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 740 | |
Andy Lutomirski | 8bf1ebc | 2017-01-18 11:15:38 -0800 | [diff] [blame] | 741 | static void apply_forced_caps(struct cpuinfo_x86 *c) |
| 742 | { |
| 743 | int i; |
| 744 | |
Thomas Gleixner | 6cbd217 | 2017-12-04 15:07:32 +0100 | [diff] [blame] | 745 | for (i = 0; i < NCAPINTS + NBUGINTS; i++) { |
Andy Lutomirski | 8bf1ebc | 2017-01-18 11:15:38 -0800 | [diff] [blame] | 746 | c->x86_capability[i] &= ~cpu_caps_cleared[i]; |
| 747 | c->x86_capability[i] |= cpu_caps_set[i]; |
| 748 | } |
| 749 | } |
| 750 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 751 | void get_cpu_cap(struct cpuinfo_x86 *c) |
Yinghai Lu | 093af8d | 2008-01-30 13:33:32 +0100 | [diff] [blame] | 752 | { |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 753 | u32 eax, ebx, ecx, edx; |
Yinghai Lu | 093af8d | 2008-01-30 13:33:32 +0100 | [diff] [blame] | 754 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 755 | /* Intel-defined flags: level 0x00000001 */ |
| 756 | if (c->cpuid_level >= 0x00000001) { |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 757 | cpuid(0x00000001, &eax, &ebx, &ecx, &edx); |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 758 | |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 759 | c->x86_capability[CPUID_1_ECX] = ecx; |
| 760 | c->x86_capability[CPUID_1_EDX] = edx; |
Yinghai Lu | 093af8d | 2008-01-30 13:33:32 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Andy Lutomirski | 3df8d920 | 2016-12-15 10:14:42 -0800 | [diff] [blame] | 763 | /* Thermal and Power Management Leaf: level 0x00000006 (eax) */ |
| 764 | if (c->cpuid_level >= 0x00000006) |
| 765 | c->x86_capability[CPUID_6_EAX] = cpuid_eax(0x00000006); |
| 766 | |
H. Peter Anvin | bdc802d | 2010-07-07 17:29:18 -0700 | [diff] [blame] | 767 | /* Additional Intel-defined flags: level 0x00000007 */ |
| 768 | if (c->cpuid_level >= 0x00000007) { |
H. Peter Anvin | bdc802d | 2010-07-07 17:29:18 -0700 | [diff] [blame] | 769 | cpuid_count(0x00000007, 0, &eax, &ebx, &ecx, &edx); |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 770 | c->x86_capability[CPUID_7_0_EBX] = ebx; |
Dave Hansen | dfb4a70 | 2016-02-12 13:02:01 -0800 | [diff] [blame] | 771 | c->x86_capability[CPUID_7_ECX] = ecx; |
H. Peter Anvin | bdc802d | 2010-07-07 17:29:18 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Fenghua Yu | 6229ad2 | 2014-05-29 11:12:30 -0700 | [diff] [blame] | 774 | /* Extended state features: level 0x0000000d */ |
| 775 | if (c->cpuid_level >= 0x0000000d) { |
Fenghua Yu | 6229ad2 | 2014-05-29 11:12:30 -0700 | [diff] [blame] | 776 | cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx); |
| 777 | |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 778 | c->x86_capability[CPUID_D_1_EAX] = eax; |
Fenghua Yu | 6229ad2 | 2014-05-29 11:12:30 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Peter P Waskiewicz Jr | cbc82b1 | 2015-01-23 18:45:43 +0000 | [diff] [blame] | 781 | /* Additional Intel-defined flags: level 0x0000000F */ |
| 782 | if (c->cpuid_level >= 0x0000000F) { |
Peter P Waskiewicz Jr | cbc82b1 | 2015-01-23 18:45:43 +0000 | [diff] [blame] | 783 | |
| 784 | /* QoS sub-leaf, EAX=0Fh, ECX=0 */ |
| 785 | cpuid_count(0x0000000F, 0, &eax, &ebx, &ecx, &edx); |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 786 | c->x86_capability[CPUID_F_0_EDX] = edx; |
| 787 | |
Peter P Waskiewicz Jr | cbc82b1 | 2015-01-23 18:45:43 +0000 | [diff] [blame] | 788 | if (cpu_has(c, X86_FEATURE_CQM_LLC)) { |
| 789 | /* will be overridden if occupancy monitoring exists */ |
| 790 | c->x86_cache_max_rmid = ebx; |
| 791 | |
| 792 | /* QoS sub-leaf, EAX=0Fh, ECX=1 */ |
| 793 | cpuid_count(0x0000000F, 1, &eax, &ebx, &ecx, &edx); |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 794 | c->x86_capability[CPUID_F_1_EDX] = edx; |
| 795 | |
Vikas Shivappa | 33c3cc7 | 2016-03-10 15:32:09 -0800 | [diff] [blame] | 796 | if ((cpu_has(c, X86_FEATURE_CQM_OCCUP_LLC)) || |
| 797 | ((cpu_has(c, X86_FEATURE_CQM_MBM_TOTAL)) || |
| 798 | (cpu_has(c, X86_FEATURE_CQM_MBM_LOCAL)))) { |
Peter P Waskiewicz Jr | cbc82b1 | 2015-01-23 18:45:43 +0000 | [diff] [blame] | 799 | c->x86_cache_max_rmid = ecx; |
| 800 | c->x86_cache_occ_scale = ebx; |
| 801 | } |
| 802 | } else { |
| 803 | c->x86_cache_max_rmid = -1; |
| 804 | c->x86_cache_occ_scale = -1; |
| 805 | } |
| 806 | } |
| 807 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 808 | /* AMD-defined flags: level 0x80000001 */ |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 809 | eax = cpuid_eax(0x80000000); |
| 810 | c->extended_cpuid_level = eax; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 811 | |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 812 | if ((eax & 0xffff0000) == 0x80000000) { |
| 813 | if (eax >= 0x80000001) { |
| 814 | cpuid(0x80000001, &eax, &ebx, &ecx, &edx); |
| 815 | |
| 816 | c->x86_capability[CPUID_8000_0001_ECX] = ecx; |
| 817 | c->x86_capability[CPUID_8000_0001_EDX] = edx; |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 818 | } |
| 819 | } |
Yinghai Lu | 5122c89 | 2008-09-04 20:09:09 -0700 | [diff] [blame] | 820 | |
Yazen Ghannam | 71faad4 | 2016-05-11 14:58:26 +0200 | [diff] [blame] | 821 | if (c->extended_cpuid_level >= 0x80000007) { |
| 822 | cpuid(0x80000007, &eax, &ebx, &ecx, &edx); |
| 823 | |
| 824 | c->x86_capability[CPUID_8000_0007_EBX] = ebx; |
| 825 | c->x86_power = edx; |
| 826 | } |
| 827 | |
Yinghai Lu | 5122c89 | 2008-09-04 20:09:09 -0700 | [diff] [blame] | 828 | if (c->extended_cpuid_level >= 0x80000008) { |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 829 | cpuid(0x80000008, &eax, &ebx, &ecx, &edx); |
Yinghai Lu | 5122c89 | 2008-09-04 20:09:09 -0700 | [diff] [blame] | 830 | |
| 831 | c->x86_virt_bits = (eax >> 8) & 0xff; |
| 832 | c->x86_phys_bits = eax & 0xff; |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 833 | c->x86_capability[CPUID_8000_0008_EBX] = ebx; |
Yinghai Lu | 5122c89 | 2008-09-04 20:09:09 -0700 | [diff] [blame] | 834 | } |
Jan Beulich | 13c6c53 | 2009-03-12 12:37:34 +0000 | [diff] [blame] | 835 | #ifdef CONFIG_X86_32 |
| 836 | else if (cpu_has(c, X86_FEATURE_PAE) || cpu_has(c, X86_FEATURE_PSE36)) |
| 837 | c->x86_phys_bits = 36; |
Yinghai Lu | 5122c89 | 2008-09-04 20:09:09 -0700 | [diff] [blame] | 838 | #endif |
Yinghai Lu | e322423 | 2008-09-06 01:52:28 -0700 | [diff] [blame] | 839 | |
Borislav Petkov | 2ccd71f | 2015-12-07 10:39:39 +0100 | [diff] [blame] | 840 | if (c->extended_cpuid_level >= 0x8000000a) |
Borislav Petkov | 39c06df | 2015-12-07 10:39:40 +0100 | [diff] [blame] | 841 | c->x86_capability[CPUID_8000_000A_EDX] = cpuid_edx(0x8000000a); |
Borislav Petkov | 2ccd71f | 2015-12-07 10:39:39 +0100 | [diff] [blame] | 842 | |
Jacob Pan | 1dedefd | 2010-05-19 12:01:23 -0700 | [diff] [blame] | 843 | init_scattered_cpuid_features(c); |
Andy Lutomirski | 60d3450 | 2017-01-18 11:15:39 -0800 | [diff] [blame] | 844 | |
| 845 | /* |
| 846 | * Clear/Set all flags overridden by options, after probe. |
| 847 | * This needs to happen each time we re-probe, which may happen |
| 848 | * several times during CPU initialization. |
| 849 | */ |
| 850 | apply_forced_caps(c); |
Yinghai Lu | 093af8d | 2008-01-30 13:33:32 +0100 | [diff] [blame] | 851 | } |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 852 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 853 | static void identify_cpu_without_cpuid(struct cpuinfo_x86 *c) |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 854 | { |
| 855 | #ifdef CONFIG_X86_32 |
| 856 | int i; |
| 857 | |
| 858 | /* |
| 859 | * First of all, decide if this is a 486 or higher |
| 860 | * It's a 486 if we can modify the AC flag |
| 861 | */ |
| 862 | if (flag_is_changeable_p(X86_EFLAGS_AC)) |
| 863 | c->x86 = 4; |
| 864 | else |
| 865 | c->x86 = 3; |
| 866 | |
| 867 | for (i = 0; i < X86_VENDOR_NUM; i++) |
| 868 | if (cpu_devs[i] && cpu_devs[i]->c_identify) { |
| 869 | c->x86_vendor_id[0] = 0; |
| 870 | cpu_devs[i]->c_identify(c); |
| 871 | if (c->x86_vendor_id[0]) { |
| 872 | get_cpu_vendor(c); |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 879 | /* |
| 880 | * Do minimum CPU detection early. |
| 881 | * Fields really needed: vendor, cpuid_level, family, model, mask, |
| 882 | * cache alignment. |
| 883 | * The others are not touched to avoid unwanted side effects. |
| 884 | * |
Jean Delvare | a1652bb | 2017-10-03 11:47:27 +0200 | [diff] [blame] | 885 | * WARNING: this function is only called on the boot CPU. Don't add code |
| 886 | * here that is supposed to run on all CPUs. |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 887 | */ |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 888 | static void __init early_identify_cpu(struct cpuinfo_x86 *c) |
Rusty Russell | d7cd561 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 889 | { |
Yinghai Lu | 6627d24 | 2008-09-04 20:09:10 -0700 | [diff] [blame] | 890 | #ifdef CONFIG_X86_64 |
| 891 | c->x86_clflush_size = 64; |
Jan Beulich | 13c6c53 | 2009-03-12 12:37:34 +0000 | [diff] [blame] | 892 | c->x86_phys_bits = 36; |
| 893 | c->x86_virt_bits = 48; |
Yinghai Lu | 6627d24 | 2008-09-04 20:09:10 -0700 | [diff] [blame] | 894 | #else |
Huang, Ying | d4387bd | 2008-01-31 22:05:45 +0100 | [diff] [blame] | 895 | c->x86_clflush_size = 32; |
Jan Beulich | 13c6c53 | 2009-03-12 12:37:34 +0000 | [diff] [blame] | 896 | c->x86_phys_bits = 32; |
| 897 | c->x86_virt_bits = 32; |
Yinghai Lu | 6627d24 | 2008-09-04 20:09:10 -0700 | [diff] [blame] | 898 | #endif |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 899 | c->x86_cache_alignment = c->x86_clflush_size; |
Rusty Russell | d7cd561 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 900 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 901 | memset(&c->x86_capability, 0, sizeof c->x86_capability); |
Yinghai Lu | 0a488a5 | 2008-09-04 21:09:47 +0200 | [diff] [blame] | 902 | c->extended_cpuid_level = 0; |
| 903 | |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 904 | /* cyrix could have cpuid enabled via c_identify()*/ |
Andy Lutomirski | 05fb3c1 | 2016-09-28 16:06:33 -0700 | [diff] [blame] | 905 | if (have_cpuid_p()) { |
| 906 | cpu_detect(c); |
| 907 | get_cpu_vendor(c); |
| 908 | get_cpu_cap(c); |
Borislav Petkov | 78d1b2968 | 2017-01-18 11:15:37 -0800 | [diff] [blame] | 909 | setup_force_cpu_cap(X86_FEATURE_CPUID); |
Rusty Russell | d7cd561 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 910 | |
Andy Lutomirski | 05fb3c1 | 2016-09-28 16:06:33 -0700 | [diff] [blame] | 911 | if (this_cpu->c_early_init) |
| 912 | this_cpu->c_early_init(c); |
Krzysztof Helt | 12cf105 | 2008-09-04 21:09:43 +0200 | [diff] [blame] | 913 | |
Andy Lutomirski | 05fb3c1 | 2016-09-28 16:06:33 -0700 | [diff] [blame] | 914 | c->cpu_index = 0; |
| 915 | filter_cpuid_features(c, false); |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 916 | |
Andy Lutomirski | 05fb3c1 | 2016-09-28 16:06:33 -0700 | [diff] [blame] | 917 | if (this_cpu->c_bsp_init) |
| 918 | this_cpu->c_bsp_init(c); |
Borislav Petkov | 78d1b2968 | 2017-01-18 11:15:37 -0800 | [diff] [blame] | 919 | } else { |
| 920 | identify_cpu_without_cpuid(c); |
| 921 | setup_clear_cpu_cap(X86_FEATURE_CPUID); |
Andy Lutomirski | 05fb3c1 | 2016-09-28 16:06:33 -0700 | [diff] [blame] | 922 | } |
Borislav Petkov | c3b8359 | 2013-06-09 12:07:30 +0200 | [diff] [blame] | 923 | |
| 924 | setup_force_cpu_cap(X86_FEATURE_ALWAYS); |
Thomas Gleixner | a89f040 | 2017-12-04 15:07:33 +0100 | [diff] [blame] | 925 | |
Tom Lendacky | 694d99d | 2017-12-26 23:43:54 -0600 | [diff] [blame] | 926 | if (c->x86_vendor != X86_VENDOR_AMD) |
Thomas Gleixner | de79182 | 2018-01-05 15:27:34 +0100 | [diff] [blame] | 927 | setup_force_cpu_bug(X86_BUG_CPU_MELTDOWN); |
Thomas Gleixner | a89f040 | 2017-12-04 15:07:33 +0100 | [diff] [blame] | 928 | |
Ingo Molnar | db52ef7 | 2015-06-27 10:25:14 +0200 | [diff] [blame] | 929 | fpu__init_system(c); |
Andy Lutomirski | b8b7aba | 2017-09-17 09:03:50 -0700 | [diff] [blame] | 930 | |
| 931 | #ifdef CONFIG_X86_32 |
| 932 | /* |
| 933 | * Regardless of whether PCID is enumerated, the SDM says |
| 934 | * that it can't be enabled in 32-bit mode. |
| 935 | */ |
| 936 | setup_clear_cpu_cap(X86_FEATURE_PCID); |
| 937 | #endif |
Rusty Russell | d7cd561 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 940 | void __init early_cpu_init(void) |
| 941 | { |
Jan Beulich | 02dde8b | 2009-03-12 12:08:49 +0000 | [diff] [blame] | 942 | const struct cpu_dev *const *cdev; |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 943 | int count = 0; |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 944 | |
Jan Beulich | ac23f25 | 2011-03-04 15:52:35 +0000 | [diff] [blame] | 945 | #ifdef CONFIG_PROCESSOR_SELECT |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 946 | pr_info("KERNEL supported cpus:\n"); |
Ingo Molnar | 31c997c | 2009-11-14 10:34:41 +0100 | [diff] [blame] | 947 | #endif |
| 948 | |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 949 | for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) { |
Jan Beulich | 02dde8b | 2009-03-12 12:08:49 +0000 | [diff] [blame] | 950 | const struct cpu_dev *cpudev = *cdev; |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 951 | |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 952 | if (count >= X86_VENDOR_NUM) |
| 953 | break; |
| 954 | cpu_devs[count] = cpudev; |
| 955 | count++; |
| 956 | |
Jan Beulich | ac23f25 | 2011-03-04 15:52:35 +0000 | [diff] [blame] | 957 | #ifdef CONFIG_PROCESSOR_SELECT |
Ingo Molnar | 31c997c | 2009-11-14 10:34:41 +0100 | [diff] [blame] | 958 | { |
| 959 | unsigned int j; |
Yinghai Lu | 10a434f | 2008-09-04 21:09:45 +0200 | [diff] [blame] | 960 | |
Ingo Molnar | 31c997c | 2009-11-14 10:34:41 +0100 | [diff] [blame] | 961 | for (j = 0; j < 2; j++) { |
| 962 | if (!cpudev->c_ident[j]) |
| 963 | continue; |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 964 | pr_info(" %s %s\n", cpudev->c_vendor, |
Ingo Molnar | 31c997c | 2009-11-14 10:34:41 +0100 | [diff] [blame] | 965 | cpudev->c_ident[j]); |
| 966 | } |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 967 | } |
Dave Jones | 0388423 | 2009-11-13 15:30:00 -0500 | [diff] [blame] | 968 | #endif |
Ingo Molnar | 31c997c | 2009-11-14 10:34:41 +0100 | [diff] [blame] | 969 | } |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 970 | early_identify_cpu(&boot_cpu_data); |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 971 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
H. Peter Anvin | b6734c3 | 2008-08-18 17:39:32 -0700 | [diff] [blame] | 973 | /* |
Borislav Petkov | 366d4a4 | 2010-10-04 09:31:27 +0200 | [diff] [blame] | 974 | * The NOPL instruction is supposed to exist on all CPUs of family >= 6; |
| 975 | * unfortunately, that's not true in practice because of early VIA |
| 976 | * chips and (more importantly) broken virtualizers that are not easy |
| 977 | * to detect. In the latter case it doesn't even *fail* reliably, so |
| 978 | * probing for it doesn't even work. Disable it completely on 32-bit |
H. Peter Anvin | ba0593b | 2008-09-16 09:29:40 -0700 | [diff] [blame] | 979 | * unless we can find a reliable way to detect all the broken cases. |
Borislav Petkov | 366d4a4 | 2010-10-04 09:31:27 +0200 | [diff] [blame] | 980 | * Enable it explicitly on 64-bit for non-constant inputs of cpu_has(). |
H. Peter Anvin | b6734c3 | 2008-08-18 17:39:32 -0700 | [diff] [blame] | 981 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 982 | static void detect_nopl(struct cpuinfo_x86 *c) |
H. Peter Anvin | b6734c3 | 2008-08-18 17:39:32 -0700 | [diff] [blame] | 983 | { |
Borislav Petkov | 366d4a4 | 2010-10-04 09:31:27 +0200 | [diff] [blame] | 984 | #ifdef CONFIG_X86_32 |
H. Peter Anvin | b6734c3 | 2008-08-18 17:39:32 -0700 | [diff] [blame] | 985 | clear_cpu_cap(c, X86_FEATURE_NOPL); |
Borislav Petkov | 366d4a4 | 2010-10-04 09:31:27 +0200 | [diff] [blame] | 986 | #else |
| 987 | set_cpu_cap(c, X86_FEATURE_NOPL); |
| 988 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Andy Lutomirski | 7a5d6704 | 2016-04-07 17:31:46 -0700 | [diff] [blame] | 991 | static void detect_null_seg_behavior(struct cpuinfo_x86 *c) |
| 992 | { |
| 993 | #ifdef CONFIG_X86_64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | /* |
Andy Lutomirski | 7a5d6704 | 2016-04-07 17:31:46 -0700 | [diff] [blame] | 995 | * Empirically, writing zero to a segment selector on AMD does |
| 996 | * not clear the base, whereas writing zero to a segment |
| 997 | * selector on Intel does clear the base. Intel's behavior |
| 998 | * allows slightly faster context switches in the common case |
| 999 | * where GS is unused by the prev and next threads. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | * |
Andy Lutomirski | 7a5d6704 | 2016-04-07 17:31:46 -0700 | [diff] [blame] | 1001 | * Since neither vendor documents this anywhere that I can see, |
| 1002 | * detect it directly instead of hardcoding the choice by |
| 1003 | * vendor. |
| 1004 | * |
| 1005 | * I've designated AMD's behavior as the "bug" because it's |
| 1006 | * counterintuitive and less friendly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | */ |
Andy Lutomirski | 7a5d6704 | 2016-04-07 17:31:46 -0700 | [diff] [blame] | 1008 | |
| 1009 | unsigned long old_base, tmp; |
| 1010 | rdmsrl(MSR_FS_BASE, old_base); |
| 1011 | wrmsrl(MSR_FS_BASE, 1); |
| 1012 | loadsegment(fs, 0); |
| 1013 | rdmsrl(MSR_FS_BASE, tmp); |
| 1014 | if (tmp != 0) |
| 1015 | set_cpu_bug(c, X86_BUG_NULL_SEG); |
| 1016 | wrmsrl(MSR_FS_BASE, old_base); |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1017 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 1019 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 1020 | static void generic_identify(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | { |
| 1022 | c->extended_cpuid_level = 0; |
| 1023 | |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 1024 | if (!have_cpuid_p()) |
| 1025 | identify_cpu_without_cpuid(c); |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 1026 | |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 1027 | /* cyrix could have cpuid enabled via c_identify()*/ |
Ingo Molnar | a9853dd | 2008-09-14 14:46:58 +0200 | [diff] [blame] | 1028 | if (!have_cpuid_p()) |
Yinghai Lu | aef93c8 | 2008-09-14 02:33:15 -0700 | [diff] [blame] | 1029 | return; |
| 1030 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1031 | cpu_detect(c); |
| 1032 | |
| 1033 | get_cpu_vendor(c); |
| 1034 | |
| 1035 | get_cpu_cap(c); |
| 1036 | |
| 1037 | if (c->cpuid_level >= 0x00000001) { |
| 1038 | c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF; |
Yinghai Lu | b89d3b3 | 2008-09-04 20:09:12 -0700 | [diff] [blame] | 1039 | #ifdef CONFIG_X86_32 |
Borislav Petkov | c8e56d2 | 2015-06-04 18:55:25 +0200 | [diff] [blame] | 1040 | # ifdef CONFIG_SMP |
Ingo Molnar | cb8cc44 | 2009-01-28 13:24:54 +0100 | [diff] [blame] | 1041 | c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); |
Yinghai Lu | b89d3b3 | 2008-09-04 20:09:12 -0700 | [diff] [blame] | 1042 | # else |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1043 | c->apicid = c->initial_apicid; |
Yinghai Lu | b89d3b3 | 2008-09-04 20:09:12 -0700 | [diff] [blame] | 1044 | # endif |
Siddha, Suresh B | 1e9f28f | 2006-03-27 01:15:22 -0800 | [diff] [blame] | 1045 | #endif |
Yinghai Lu | b89d3b3 | 2008-09-04 20:09:12 -0700 | [diff] [blame] | 1046 | c->phys_proc_id = c->initial_apicid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1048 | |
Yinghai Lu | 1b05d60 | 2008-09-06 01:52:27 -0700 | [diff] [blame] | 1049 | get_model_name(c); /* Default name */ |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1050 | |
Yinghai Lu | 3da99c9 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1051 | detect_nopl(c); |
Andy Lutomirski | 7a5d6704 | 2016-04-07 17:31:46 -0700 | [diff] [blame] | 1052 | |
| 1053 | detect_null_seg_behavior(c); |
Andy Lutomirski | 0230bb0 | 2016-04-07 17:31:48 -0700 | [diff] [blame] | 1054 | |
| 1055 | /* |
| 1056 | * ESPFIX is a strange bug. All real CPUs have it. Paravirt |
| 1057 | * systems that run Linux at CPL > 0 may or may not have the |
| 1058 | * issue, but, even if they have the issue, there's absolutely |
| 1059 | * nothing we can do about it because we can't use the real IRET |
| 1060 | * instruction. |
| 1061 | * |
| 1062 | * NB: For the time being, only 32-bit kernels support |
| 1063 | * X86_BUG_ESPFIX as such. 64-bit kernels directly choose |
| 1064 | * whether to apply espfix using paravirt hooks. If any |
| 1065 | * non-paravirt system ever shows up that does *not* have the |
| 1066 | * ESPFIX issue, we can change this. |
| 1067 | */ |
| 1068 | #ifdef CONFIG_X86_32 |
| 1069 | # ifdef CONFIG_PARAVIRT |
| 1070 | do { |
| 1071 | extern void native_iret(void); |
| 1072 | if (pv_cpu_ops.iret == native_iret) |
| 1073 | set_cpu_bug(c, X86_BUG_ESPFIX); |
| 1074 | } while (0); |
| 1075 | # else |
| 1076 | set_cpu_bug(c, X86_BUG_ESPFIX); |
| 1077 | # endif |
| 1078 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Peter P Waskiewicz Jr | cbc82b1 | 2015-01-23 18:45:43 +0000 | [diff] [blame] | 1081 | static void x86_init_cache_qos(struct cpuinfo_x86 *c) |
| 1082 | { |
| 1083 | /* |
| 1084 | * The heavy lifting of max_rmid and cache_occ_scale are handled |
| 1085 | * in get_cpu_cap(). Here we just set the max_rmid for the boot_cpu |
| 1086 | * in case CQM bits really aren't there in this CPU. |
| 1087 | */ |
| 1088 | if (c != &boot_cpu_data) { |
| 1089 | boot_cpu_data.x86_cache_max_rmid = |
| 1090 | min(boot_cpu_data.x86_cache_max_rmid, |
| 1091 | c->x86_cache_max_rmid); |
| 1092 | } |
| 1093 | } |
| 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | /* |
Thomas Gleixner | 9d85eb9 | 2016-12-12 11:04:53 +0100 | [diff] [blame] | 1096 | * Validate that ACPI/mptables have the same information about the |
| 1097 | * effective APIC id and update the package map. |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1098 | */ |
Thomas Gleixner | 9d85eb9 | 2016-12-12 11:04:53 +0100 | [diff] [blame] | 1099 | static void validate_apic_and_package_id(struct cpuinfo_x86 *c) |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1100 | { |
| 1101 | #ifdef CONFIG_SMP |
Thomas Gleixner | 9d85eb9 | 2016-12-12 11:04:53 +0100 | [diff] [blame] | 1102 | unsigned int apicid, cpu = smp_processor_id(); |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1103 | |
| 1104 | apicid = apic->cpu_present_to_apicid(cpu); |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1105 | |
Thomas Gleixner | 9d85eb9 | 2016-12-12 11:04:53 +0100 | [diff] [blame] | 1106 | if (apicid != c->apicid) { |
| 1107 | pr_err(FW_BUG "CPU%u: APIC id mismatch. Firmware: %x APIC: %x\n", |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1108 | cpu, apicid, c->initial_apicid); |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1109 | } |
Thomas Gleixner | 9d85eb9 | 2016-12-12 11:04:53 +0100 | [diff] [blame] | 1110 | BUG_ON(topology_update_package_map(c->phys_proc_id, cpu)); |
Thomas Gleixner | d49597f | 2016-11-09 16:35:51 +0100 | [diff] [blame] | 1111 | #else |
| 1112 | c->logical_proc_id = 0; |
| 1113 | #endif |
| 1114 | } |
| 1115 | |
| 1116 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | * This does the hard work of actually picking apart the CPU stuff... |
| 1118 | */ |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 1119 | static void identify_cpu(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
| 1121 | int i; |
| 1122 | |
| 1123 | c->loops_per_jiffy = loops_per_jiffy; |
| 1124 | c->x86_cache_size = -1; |
| 1125 | c->x86_vendor = X86_VENDOR_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | c->x86_model = c->x86_mask = 0; /* So far unknown... */ |
| 1127 | c->x86_vendor_id[0] = '\0'; /* Unset */ |
| 1128 | c->x86_model_id[0] = '\0'; /* Unset */ |
Siddha, Suresh B | 94605ef | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 1129 | c->x86_max_cores = 1; |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1130 | c->x86_coreid_bits = 0; |
Borislav Petkov | 79a8b9a | 2017-02-05 11:50:21 +0100 | [diff] [blame] | 1131 | c->cu_id = 0xff; |
Yinghai Lu | 11fdd25 | 2008-09-07 17:58:50 -0700 | [diff] [blame] | 1132 | #ifdef CONFIG_X86_64 |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1133 | c->x86_clflush_size = 64; |
Jan Beulich | 13c6c53 | 2009-03-12 12:37:34 +0000 | [diff] [blame] | 1134 | c->x86_phys_bits = 36; |
| 1135 | c->x86_virt_bits = 48; |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1136 | #else |
| 1137 | c->cpuid_level = -1; /* CPUID not detected */ |
Andi Kleen | 770d132 | 2006-12-07 02:14:05 +0100 | [diff] [blame] | 1138 | c->x86_clflush_size = 32; |
Jan Beulich | 13c6c53 | 2009-03-12 12:37:34 +0000 | [diff] [blame] | 1139 | c->x86_phys_bits = 32; |
| 1140 | c->x86_virt_bits = 32; |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1141 | #endif |
| 1142 | c->x86_cache_alignment = c->x86_clflush_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | memset(&c->x86_capability, 0, sizeof c->x86_capability); |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | generic_identify(c); |
| 1146 | |
Andi Kleen | 3898534 | 2008-01-30 13:32:49 +0100 | [diff] [blame] | 1147 | if (this_cpu->c_identify) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | this_cpu->c_identify(c); |
| 1149 | |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 1150 | /* Clear/Set all flags overridden by options, after probe */ |
Andy Lutomirski | 8bf1ebc | 2017-01-18 11:15:38 -0800 | [diff] [blame] | 1151 | apply_forced_caps(c); |
Yinghai Lu | 2759c32 | 2009-05-15 13:05:16 -0700 | [diff] [blame] | 1152 | |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1153 | #ifdef CONFIG_X86_64 |
Ingo Molnar | cb8cc44 | 2009-01-28 13:24:54 +0100 | [diff] [blame] | 1154 | c->apicid = apic->phys_pkg_id(c->initial_apicid, 0); |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1155 | #endif |
| 1156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | /* |
| 1158 | * Vendor-specific initialization. In this section we |
| 1159 | * canonicalize the feature flags, meaning if there are |
| 1160 | * features a certain CPU supports which CPUID doesn't |
| 1161 | * tell us, CPUID claiming incorrect flags, or other bugs, |
| 1162 | * we handle them here. |
| 1163 | * |
| 1164 | * At the end of this section, c->x86_capability better |
| 1165 | * indicate the features this CPU genuinely supports! |
| 1166 | */ |
| 1167 | if (this_cpu->c_init) |
| 1168 | this_cpu->c_init(c); |
| 1169 | |
| 1170 | /* Disable the PN if appropriate */ |
| 1171 | squash_the_stupid_serial_number(c); |
| 1172 | |
Ricardo Neri | aa35f89 | 2017-11-05 18:27:54 -0800 | [diff] [blame] | 1173 | /* Set up SMEP/SMAP/UMIP */ |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 1174 | setup_smep(c); |
| 1175 | setup_smap(c); |
Ricardo Neri | aa35f89 | 2017-11-05 18:27:54 -0800 | [diff] [blame] | 1176 | setup_umip(c); |
H. Peter Anvin | b2cc2a0 | 2012-09-26 18:02:28 -0700 | [diff] [blame] | 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | /* |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1179 | * The vendor-specific functions might have changed features. |
| 1180 | * Now we do "generic changes." |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | */ |
| 1182 | |
H. Peter Anvin | b38b066 | 2009-01-23 17:20:50 -0800 | [diff] [blame] | 1183 | /* Filter out anything that depends on CPUID levels we don't have */ |
| 1184 | filter_cpuid_features(c, true); |
| 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | /* If the model name is still unset, do table lookup. */ |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 1187 | if (!c->x86_model_id[0]) { |
Jan Beulich | 02dde8b | 2009-03-12 12:08:49 +0000 | [diff] [blame] | 1188 | const char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | p = table_lookup_model(c); |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 1190 | if (p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | strcpy(c->x86_model_id, p); |
| 1192 | else |
| 1193 | /* Last resort... */ |
| 1194 | sprintf(c->x86_model_id, "%02x/%02x", |
Chuck Ebbert | 54a20f8 | 2006-03-23 02:59:36 -0800 | [diff] [blame] | 1195 | c->x86, c->x86_model); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1198 | #ifdef CONFIG_X86_64 |
| 1199 | detect_ht(c); |
| 1200 | #endif |
| 1201 | |
H. Peter Anvin | 49d859d | 2011-07-31 14:02:19 -0700 | [diff] [blame] | 1202 | x86_init_rdrand(c); |
Peter P Waskiewicz Jr | cbc82b1 | 2015-01-23 18:45:43 +0000 | [diff] [blame] | 1203 | x86_init_cache_qos(c); |
Dave Hansen | 0697694 | 2016-02-12 13:02:29 -0800 | [diff] [blame] | 1204 | setup_pku(c); |
Yinghai Lu | 3e0c373 | 2009-05-09 23:47:42 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 1207 | * Clear/Set all flags overridden by options, need do it |
Yinghai Lu | 3e0c373 | 2009-05-09 23:47:42 -0700 | [diff] [blame] | 1208 | * before following smp all cpus cap AND. |
| 1209 | */ |
Andy Lutomirski | 8bf1ebc | 2017-01-18 11:15:38 -0800 | [diff] [blame] | 1210 | apply_forced_caps(c); |
Yinghai Lu | 3e0c373 | 2009-05-09 23:47:42 -0700 | [diff] [blame] | 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | /* |
| 1213 | * On SMP, boot_cpu_data holds the common feature set between |
| 1214 | * all CPUs; so make sure that we indicate which features are |
| 1215 | * common between the CPUs. The first time this routine gets |
| 1216 | * executed, c == &boot_cpu_data. |
| 1217 | */ |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 1218 | if (c != &boot_cpu_data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | /* AND the already accumulated flags with these */ |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1220 | for (i = 0; i < NCAPINTS; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | boot_cpu_data.x86_capability[i] &= c->x86_capability[i]; |
Borislav Petkov | 65fc985 | 2013-03-20 15:07:23 +0100 | [diff] [blame] | 1222 | |
| 1223 | /* OR, i.e. replicate the bug flags */ |
| 1224 | for (i = NCAPINTS; i < NCAPINTS + NBUGINTS; i++) |
| 1225 | c->x86_capability[i] |= boot_cpu_data.x86_capability[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /* Init Machine Check Exception if available. */ |
Borislav Petkov | 5e09954 | 2009-10-16 12:31:32 +0200 | [diff] [blame] | 1229 | mcheck_cpu_init(c); |
Andi Kleen | 30d432d | 2008-01-30 13:33:16 +0100 | [diff] [blame] | 1230 | |
| 1231 | select_idle_routine(c); |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1232 | |
Tejun Heo | de2d944 | 2011-01-23 14:37:41 +0100 | [diff] [blame] | 1233 | #ifdef CONFIG_NUMA |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1234 | numa_add_cpu(smp_processor_id()); |
| 1235 | #endif |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1236 | } |
Shaohua Li | 31ab269 | 2005-11-07 00:58:42 -0800 | [diff] [blame] | 1237 | |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1238 | /* |
| 1239 | * Set up the CPU state needed to execute SYSENTER/SYSEXIT instructions |
| 1240 | * on 32-bit kernels: |
| 1241 | */ |
Andy Lutomirski | cfda7bb | 2014-05-05 12:19:33 -0700 | [diff] [blame] | 1242 | #ifdef CONFIG_X86_32 |
| 1243 | void enable_sep_cpu(void) |
| 1244 | { |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1245 | struct tss_struct *tss; |
| 1246 | int cpu; |
Andy Lutomirski | cfda7bb | 2014-05-05 12:19:33 -0700 | [diff] [blame] | 1247 | |
Borislav Petkov | b3edfda | 2016-03-16 13:19:29 +0100 | [diff] [blame] | 1248 | if (!boot_cpu_has(X86_FEATURE_SEP)) |
| 1249 | return; |
| 1250 | |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1251 | cpu = get_cpu(); |
Andy Lutomirski | c482fee | 2017-12-04 15:07:29 +0100 | [diff] [blame] | 1252 | tss = &per_cpu(cpu_tss_rw, cpu); |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1253 | |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1254 | /* |
Andy Lutomirski | cf9328c | 2015-04-02 12:41:45 -0700 | [diff] [blame] | 1255 | * We cache MSR_IA32_SYSENTER_CS's value in the TSS's ss1 field -- |
| 1256 | * see the big comment in struct x86_hw_tss's definition. |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1257 | */ |
Andy Lutomirski | cfda7bb | 2014-05-05 12:19:33 -0700 | [diff] [blame] | 1258 | |
| 1259 | tss->x86_tss.ss1 = __KERNEL_CS; |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1260 | wrmsr(MSR_IA32_SYSENTER_CS, tss->x86_tss.ss1, 0); |
Dave Hansen | 4fe2d8b | 2017-12-04 17:25:07 -0800 | [diff] [blame] | 1261 | wrmsr(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1), 0); |
Ingo Molnar | 4c8cd0c | 2015-06-08 08:33:56 +0200 | [diff] [blame] | 1262 | wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long)entry_SYSENTER_32, 0); |
Ingo Molnar | 8b6c0ab | 2015-03-16 10:32:20 +0100 | [diff] [blame] | 1263 | |
Andy Lutomirski | cfda7bb | 2014-05-05 12:19:33 -0700 | [diff] [blame] | 1264 | put_cpu(); |
| 1265 | } |
Glauber Costa | e04d645 | 2008-09-22 14:35:08 -0300 | [diff] [blame] | 1266 | #endif |
| 1267 | |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1268 | void __init identify_boot_cpu(void) |
| 1269 | { |
| 1270 | identify_cpu(&boot_cpu_data); |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1271 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1272 | sysenter_setup(); |
Li Shaohua | 6fe940d | 2005-06-25 14:54:53 -0700 | [diff] [blame] | 1273 | enable_sep_cpu(); |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1274 | #endif |
Borislav Petkov | 5b556332 | 2012-08-06 19:00:37 +0200 | [diff] [blame] | 1275 | cpu_detect_tlb(&boot_cpu_data); |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1276 | } |
Shaohua Li | 3b520b2 | 2005-07-07 17:56:38 -0700 | [diff] [blame] | 1277 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 1278 | void identify_secondary_cpu(struct cpuinfo_x86 *c) |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1279 | { |
| 1280 | BUG_ON(c == &boot_cpu_data); |
| 1281 | identify_cpu(c); |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1282 | #ifdef CONFIG_X86_32 |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1283 | enable_sep_cpu(); |
Yinghai Lu | 102bbe3a | 2008-09-04 20:09:13 -0700 | [diff] [blame] | 1284 | #endif |
Jeremy Fitzhardinge | a6c4e07 | 2007-05-02 19:27:12 +0200 | [diff] [blame] | 1285 | mtrr_ap_init(); |
Thomas Gleixner | 9d85eb9 | 2016-12-12 11:04:53 +0100 | [diff] [blame] | 1286 | validate_apic_and_package_id(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Andi Kleen | 191679f | 2008-01-30 13:33:21 +0100 | [diff] [blame] | 1289 | static __init int setup_noclflush(char *arg) |
| 1290 | { |
H. Peter Anvin | 840d283 | 2014-02-27 08:31:30 -0800 | [diff] [blame] | 1291 | setup_clear_cpu_cap(X86_FEATURE_CLFLUSH); |
H. Peter Anvin | da4aaa7 | 2014-02-27 08:36:31 -0800 | [diff] [blame] | 1292 | setup_clear_cpu_cap(X86_FEATURE_CLFLUSHOPT); |
Andi Kleen | 191679f | 2008-01-30 13:33:21 +0100 | [diff] [blame] | 1293 | return 1; |
| 1294 | } |
| 1295 | __setup("noclflush", setup_noclflush); |
| 1296 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 1297 | void print_cpu_info(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | { |
Jan Beulich | 02dde8b | 2009-03-12 12:08:49 +0000 | [diff] [blame] | 1299 | const char *vendor = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1301 | if (c->x86_vendor < X86_VENDOR_NUM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | vendor = this_cpu->c_vendor; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1303 | } else { |
| 1304 | if (c->cpuid_level >= 0) |
| 1305 | vendor = c->x86_vendor_id; |
| 1306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
Yinghai Lu | bd32a8cf | 2008-09-19 18:41:16 -0700 | [diff] [blame] | 1308 | if (vendor && !strstr(c->x86_model_id, vendor)) |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1309 | pr_cont("%s ", vendor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
Yinghai Lu | 9d31d35 | 2008-09-04 21:09:44 +0200 | [diff] [blame] | 1311 | if (c->x86_model_id[0]) |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1312 | pr_cont("%s", c->x86_model_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | else |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1314 | pr_cont("%d86", c->x86); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1316 | pr_cont(" (family: 0x%x, model: 0x%x", c->x86, c->x86_model); |
Borislav Petkov | 924e101 | 2012-09-14 18:37:46 +0200 | [diff] [blame] | 1317 | |
Paolo Ciarrocchi | 34048c9 | 2008-02-24 11:58:13 +0100 | [diff] [blame] | 1318 | if (c->x86_mask || c->cpuid_level >= 0) |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1319 | pr_cont(", stepping: 0x%x)\n", c->x86_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | else |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1321 | pr_cont(")\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Andi Kleen | 0c2a391 | 2017-10-13 14:56:43 -0700 | [diff] [blame] | 1324 | /* |
| 1325 | * clearcpuid= was already parsed in fpu__init_parse_early_param. |
| 1326 | * But we need to keep a dummy __setup around otherwise it would |
| 1327 | * show up as an environment variable for init. |
| 1328 | */ |
| 1329 | static __init int setup_clearcpuid(char *arg) |
Andi Kleen | ac72e78 | 2008-01-30 13:33:21 +0100 | [diff] [blame] | 1330 | { |
Andi Kleen | ac72e78 | 2008-01-30 13:33:21 +0100 | [diff] [blame] | 1331 | return 1; |
| 1332 | } |
Andi Kleen | 0c2a391 | 2017-10-13 14:56:43 -0700 | [diff] [blame] | 1333 | __setup("clearcpuid=", setup_clearcpuid); |
Andi Kleen | ac72e78 | 2008-01-30 13:33:21 +0100 | [diff] [blame] | 1334 | |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1335 | #ifdef CONFIG_X86_64 |
Brian Gerst | 947e76c | 2009-01-19 12:21:28 +0900 | [diff] [blame] | 1336 | DEFINE_PER_CPU_FIRST(union irq_stack_union, |
Andi Kleen | 277d5b4 | 2013-08-05 15:02:43 -0700 | [diff] [blame] | 1337 | irq_stack_union) __aligned(PAGE_SIZE) __visible; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1338 | |
Tejun Heo | bdf977b | 2009-08-03 14:12:19 +0900 | [diff] [blame] | 1339 | /* |
Andy Lutomirski | a7fcf28 | 2015-03-06 17:50:19 -0800 | [diff] [blame] | 1340 | * The following percpu variables are hot. Align current_task to |
| 1341 | * cacheline size such that they fall in the same cacheline. |
Tejun Heo | bdf977b | 2009-08-03 14:12:19 +0900 | [diff] [blame] | 1342 | */ |
| 1343 | DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = |
| 1344 | &init_task; |
| 1345 | EXPORT_PER_CPU_SYMBOL(current_task); |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1346 | |
Tejun Heo | bdf977b | 2009-08-03 14:12:19 +0900 | [diff] [blame] | 1347 | DEFINE_PER_CPU(char *, irq_stack_ptr) = |
Josh Poimboeuf | 4950d6d | 2016-08-18 10:59:08 -0500 | [diff] [blame] | 1348 | init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE; |
Tejun Heo | bdf977b | 2009-08-03 14:12:19 +0900 | [diff] [blame] | 1349 | |
Andi Kleen | 277d5b4 | 2013-08-05 15:02:43 -0700 | [diff] [blame] | 1350 | DEFINE_PER_CPU(unsigned int, irq_count) __visible = -1; |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1351 | |
Peter Zijlstra | c2daa3b | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 1352 | DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; |
| 1353 | EXPORT_PER_CPU_SYMBOL(__preempt_count); |
| 1354 | |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1355 | /* May not be marked __init: used by software suspend */ |
| 1356 | void syscall_init(void) |
| 1357 | { |
Andy Lutomirski | 3386bc8 | 2017-12-04 15:07:25 +0100 | [diff] [blame] | 1358 | extern char _entry_trampoline[]; |
| 1359 | extern char entry_SYSCALL_64_trampoline[]; |
| 1360 | |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 1361 | int cpu = smp_processor_id(); |
Andy Lutomirski | 3386bc8 | 2017-12-04 15:07:25 +0100 | [diff] [blame] | 1362 | unsigned long SYSCALL64_entry_trampoline = |
| 1363 | (unsigned long)get_cpu_entry_area(cpu)->entry_trampoline + |
| 1364 | (entry_SYSCALL_64_trampoline - _entry_trampoline); |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 1365 | |
Borislav Petkov | 31ac34c | 2015-11-23 11:12:25 +0100 | [diff] [blame] | 1366 | wrmsr(MSR_STAR, 0, (__USER32_CS << 16) | __KERNEL_CS); |
Thomas Gleixner | 8d4b067 | 2017-12-04 15:07:43 +0100 | [diff] [blame] | 1367 | if (static_cpu_has(X86_FEATURE_PTI)) |
| 1368 | wrmsrl(MSR_LSTAR, SYSCALL64_entry_trampoline); |
| 1369 | else |
| 1370 | wrmsrl(MSR_LSTAR, (unsigned long)entry_SYSCALL_64); |
Ingo Molnar | d56fe4b | 2015-03-24 14:41:37 +0100 | [diff] [blame] | 1371 | |
| 1372 | #ifdef CONFIG_IA32_EMULATION |
Andy Lutomirski | 47edb65 | 2015-07-23 12:14:40 -0700 | [diff] [blame] | 1373 | wrmsrl(MSR_CSTAR, (unsigned long)entry_SYSCALL_compat); |
Denys Vlasenko | a76c7f4 | 2015-03-22 20:48:14 +0100 | [diff] [blame] | 1374 | /* |
Denys Vlasenko | 487d1ed | 2015-03-27 11:59:16 +0100 | [diff] [blame] | 1375 | * This only works on Intel CPUs. |
| 1376 | * On AMD CPUs these MSRs are 32-bit, CPU truncates MSR_IA32_SYSENTER_EIP. |
| 1377 | * This does not cause SYSENTER to jump to the wrong location, because |
| 1378 | * AMD doesn't allow SYSENTER in long mode (either 32- or 64-bit). |
Denys Vlasenko | a76c7f4 | 2015-03-22 20:48:14 +0100 | [diff] [blame] | 1379 | */ |
| 1380 | wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS); |
Dave Hansen | 4fe2d8b | 2017-12-04 17:25:07 -0800 | [diff] [blame] | 1381 | wrmsrl_safe(MSR_IA32_SYSENTER_ESP, (unsigned long)(cpu_entry_stack(cpu) + 1)); |
Ingo Molnar | 4c8cd0c | 2015-06-08 08:33:56 +0200 | [diff] [blame] | 1382 | wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)entry_SYSENTER_compat); |
Ingo Molnar | d56fe4b | 2015-03-24 14:41:37 +0100 | [diff] [blame] | 1383 | #else |
Andy Lutomirski | 47edb65 | 2015-07-23 12:14:40 -0700 | [diff] [blame] | 1384 | wrmsrl(MSR_CSTAR, (unsigned long)ignore_sysret); |
Borislav Petkov | 6b51311 | 2015-04-03 14:25:28 +0200 | [diff] [blame] | 1385 | wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)GDT_ENTRY_INVALID_SEG); |
Ingo Molnar | d56fe4b | 2015-03-24 14:41:37 +0100 | [diff] [blame] | 1386 | wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL); |
| 1387 | wrmsrl_safe(MSR_IA32_SYSENTER_EIP, 0ULL); |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1388 | #endif |
| 1389 | |
| 1390 | /* Flags to clear on syscall */ |
| 1391 | wrmsrl(MSR_SYSCALL_MASK, |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 1392 | X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF| |
Andy Lutomirski | 8c7aa69 | 2014-10-01 11:49:04 -0700 | [diff] [blame] | 1393 | X86_EFLAGS_IOPL|X86_EFLAGS_AC|X86_EFLAGS_NT); |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1396 | /* |
| 1397 | * Copies of the original ist values from the tss are only accessed during |
| 1398 | * debugging, no special alignment required. |
| 1399 | */ |
| 1400 | DEFINE_PER_CPU(struct orig_ist, orig_ist); |
| 1401 | |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1402 | static DEFINE_PER_CPU(unsigned long, debug_stack_addr); |
Steven Rostedt | 4218118 | 2011-12-16 11:43:02 -0500 | [diff] [blame] | 1403 | DEFINE_PER_CPU(int, debug_stack_usage); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1404 | |
| 1405 | int is_debug_stack(unsigned long addr) |
| 1406 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1407 | return __this_cpu_read(debug_stack_usage) || |
| 1408 | (addr <= __this_cpu_read(debug_stack_addr) && |
| 1409 | addr > (__this_cpu_read(debug_stack_addr) - DEBUG_STKSZ)); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1410 | } |
Masami Hiramatsu | 0f46efeb | 2014-04-17 17:17:12 +0900 | [diff] [blame] | 1411 | NOKPROBE_SYMBOL(is_debug_stack); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1412 | |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 1413 | DEFINE_PER_CPU(u32, debug_idt_ctr); |
Steven Rostedt | f898817 | 2012-05-30 11:47:00 -0400 | [diff] [blame] | 1414 | |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1415 | void debug_stack_set_zero(void) |
| 1416 | { |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 1417 | this_cpu_inc(debug_idt_ctr); |
| 1418 | load_current_idt(); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1419 | } |
Masami Hiramatsu | 0f46efeb | 2014-04-17 17:17:12 +0900 | [diff] [blame] | 1420 | NOKPROBE_SYMBOL(debug_stack_set_zero); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1421 | |
| 1422 | void debug_stack_reset(void) |
| 1423 | { |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 1424 | if (WARN_ON(!this_cpu_read(debug_idt_ctr))) |
Steven Rostedt | f898817 | 2012-05-30 11:47:00 -0400 | [diff] [blame] | 1425 | return; |
Seiji Aguchi | 629f4f9 | 2013-06-20 11:45:44 -0400 | [diff] [blame] | 1426 | if (this_cpu_dec_return(debug_idt_ctr) == 0) |
| 1427 | load_current_idt(); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1428 | } |
Masami Hiramatsu | 0f46efeb | 2014-04-17 17:17:12 +0900 | [diff] [blame] | 1429 | NOKPROBE_SYMBOL(debug_stack_reset); |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1430 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1431 | #else /* CONFIG_X86_64 */ |
Yinghai Lu | d5494d4 | 2008-09-04 20:09:03 -0700 | [diff] [blame] | 1432 | |
Tejun Heo | bdf977b | 2009-08-03 14:12:19 +0900 | [diff] [blame] | 1433 | DEFINE_PER_CPU(struct task_struct *, current_task) = &init_task; |
| 1434 | EXPORT_PER_CPU_SYMBOL(current_task); |
Peter Zijlstra | c2daa3b | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 1435 | DEFINE_PER_CPU(int, __preempt_count) = INIT_PREEMPT_COUNT; |
| 1436 | EXPORT_PER_CPU_SYMBOL(__preempt_count); |
Tejun Heo | bdf977b | 2009-08-03 14:12:19 +0900 | [diff] [blame] | 1437 | |
Andy Lutomirski | a7fcf28 | 2015-03-06 17:50:19 -0800 | [diff] [blame] | 1438 | /* |
| 1439 | * On x86_32, vm86 modifies tss.sp0, so sp0 isn't a reliable way to find |
| 1440 | * the top of the kernel stack. Use an extra percpu variable to track the |
| 1441 | * top of the kernel stack directly. |
| 1442 | */ |
| 1443 | DEFINE_PER_CPU(unsigned long, cpu_current_top_of_stack) = |
| 1444 | (unsigned long)&init_thread_union + THREAD_SIZE; |
| 1445 | EXPORT_PER_CPU_SYMBOL(cpu_current_top_of_stack); |
| 1446 | |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 1447 | #ifdef CONFIG_CC_STACKPROTECTOR |
Jeremy Fitzhardinge | 53f8245 | 2009-09-03 14:31:44 -0700 | [diff] [blame] | 1448 | DEFINE_PER_CPU_ALIGNED(struct stack_canary, stack_canary); |
Tejun Heo | 60a5317 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 1449 | #endif |
| 1450 | |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1451 | #endif /* CONFIG_X86_64 */ |
Jeremy Fitzhardinge | c5413fb | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 1452 | |
Rusty Russell | d2cbcc4 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 1453 | /* |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 1454 | * Clear all 6 debug registers: |
| 1455 | */ |
| 1456 | static void clear_all_debug_regs(void) |
| 1457 | { |
| 1458 | int i; |
| 1459 | |
| 1460 | for (i = 0; i < 8; i++) { |
| 1461 | /* Ignore db4, db5 */ |
| 1462 | if ((i == 4) || (i == 5)) |
| 1463 | continue; |
| 1464 | |
| 1465 | set_debugreg(0, i); |
| 1466 | } |
| 1467 | } |
Jeremy Fitzhardinge | f95d47c | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 1468 | |
Jason Wessel | 0bb9fef | 2010-05-20 21:04:30 -0500 | [diff] [blame] | 1469 | #ifdef CONFIG_KGDB |
| 1470 | /* |
| 1471 | * Restore debug regs if using kgdbwait and you have a kernel debugger |
| 1472 | * connection established. |
| 1473 | */ |
| 1474 | static void dbg_restore_debug_regs(void) |
| 1475 | { |
| 1476 | if (unlikely(kgdb_connected && arch_kgdb_ops.correct_hw_break)) |
| 1477 | arch_kgdb_ops.correct_hw_break(); |
| 1478 | } |
| 1479 | #else /* ! CONFIG_KGDB */ |
| 1480 | #define dbg_restore_debug_regs() |
| 1481 | #endif /* ! CONFIG_KGDB */ |
| 1482 | |
Igor Mammedov | ce4b1b1 | 2014-06-20 14:23:11 +0200 | [diff] [blame] | 1483 | static void wait_for_master_cpu(int cpu) |
| 1484 | { |
| 1485 | #ifdef CONFIG_SMP |
| 1486 | /* |
| 1487 | * wait for ACK from master CPU before continuing |
| 1488 | * with AP initialization |
| 1489 | */ |
| 1490 | WARN_ON(cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)); |
| 1491 | while (!cpumask_test_cpu(cpu, cpu_callout_mask)) |
| 1492 | cpu_relax(); |
| 1493 | #endif |
| 1494 | } |
| 1495 | |
Rusty Russell | d2cbcc4 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 1496 | /* |
| 1497 | * cpu_init() initializes state that is per-CPU. Some data is already |
| 1498 | * initialized (naturally) in the bootstrap process, such as the GDT |
| 1499 | * and IDT. We reload them nevertheless, this function acts as a |
| 1500 | * 'CPU state barrier', nothing should get across. |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1501 | * A lot of state is already set up in PDA init for 64 bit |
Rusty Russell | d2cbcc4 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 1502 | */ |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1503 | #ifdef CONFIG_X86_64 |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1504 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 1505 | void cpu_init(void) |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1506 | { |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1507 | struct orig_ist *oist; |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1508 | struct task_struct *me; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1509 | struct tss_struct *t; |
| 1510 | unsigned long v; |
Andy Lutomirski | fb59831 | 2016-07-14 13:22:58 -0700 | [diff] [blame] | 1511 | int cpu = raw_smp_processor_id(); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1512 | int i; |
| 1513 | |
Igor Mammedov | ce4b1b1 | 2014-06-20 14:23:11 +0200 | [diff] [blame] | 1514 | wait_for_master_cpu(cpu); |
| 1515 | |
Fenghua Yu | e6ebf5d | 2012-12-20 23:44:24 -0800 | [diff] [blame] | 1516 | /* |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 1517 | * Initialize the CR4 shadow before doing anything that could |
| 1518 | * try to read it. |
| 1519 | */ |
| 1520 | cr4_init_shadow(); |
| 1521 | |
Borislav Petkov | 777284b | 2016-10-25 11:55:11 +0200 | [diff] [blame] | 1522 | if (cpu) |
| 1523 | load_ucode_ap(); |
Fenghua Yu | e6ebf5d | 2012-12-20 23:44:24 -0800 | [diff] [blame] | 1524 | |
Andy Lutomirski | c482fee | 2017-12-04 15:07:29 +0100 | [diff] [blame] | 1525 | t = &per_cpu(cpu_tss_rw, cpu); |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1526 | oist = &per_cpu(orig_ist, cpu); |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1527 | |
Brian Gerst | e7a22c1 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 1528 | #ifdef CONFIG_NUMA |
Fenghua Yu | 27fd185 | 2012-11-13 11:32:47 -0800 | [diff] [blame] | 1529 | if (this_cpu_read(numa_node) == 0 && |
Lee Schermerhorn | e534c7c | 2010-05-26 14:44:58 -0700 | [diff] [blame] | 1530 | early_cpu_to_node(cpu) != NUMA_NO_NODE) |
| 1531 | set_numa_node(early_cpu_to_node(cpu)); |
Brian Gerst | e7a22c1 | 2009-01-19 00:38:59 +0900 | [diff] [blame] | 1532 | #endif |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1533 | |
| 1534 | me = current; |
| 1535 | |
Mike Travis | 2eaad1f | 2009-12-10 17:19:36 -0800 | [diff] [blame] | 1536 | pr_debug("Initializing CPU#%d\n", cpu); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1537 | |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 1538 | cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1539 | |
| 1540 | /* |
| 1541 | * Initialize the per-CPU GDT with the boot GDT, |
| 1542 | * and set up the GDT descriptor: |
| 1543 | */ |
| 1544 | |
Brian Gerst | 552be87 | 2009-01-30 17:47:53 +0900 | [diff] [blame] | 1545 | switch_to_new_gdt(cpu); |
Brian Gerst | 2697fbd | 2009-01-27 12:56:48 +0900 | [diff] [blame] | 1546 | loadsegment(fs, 0); |
| 1547 | |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 1548 | load_current_idt(); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1549 | |
| 1550 | memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8); |
| 1551 | syscall_init(); |
| 1552 | |
| 1553 | wrmsrl(MSR_FS_BASE, 0); |
| 1554 | wrmsrl(MSR_KERNEL_GS_BASE, 0); |
| 1555 | barrier(); |
| 1556 | |
H. Peter Anvin | 4763ed4 | 2009-11-13 15:28:16 -0800 | [diff] [blame] | 1557 | x86_configure_nx(); |
Thomas Gleixner | 659006b | 2015-01-15 21:22:26 +0000 | [diff] [blame] | 1558 | x2apic_setup(); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1559 | |
| 1560 | /* |
| 1561 | * set up and load the per-CPU TSS |
| 1562 | */ |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1563 | if (!oist->ist[0]) { |
Andy Lutomirski | 40e7f94 | 2017-12-04 15:07:26 +0100 | [diff] [blame] | 1564 | char *estacks = get_cpu_entry_area(cpu)->exception_stacks; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1565 | |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1566 | for (v = 0; v < N_EXCEPTION_STACKS; v++) { |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1567 | estacks += exception_stack_sizes[v]; |
Tejun Heo | 0fe1e00 | 2009-10-29 22:34:14 +0900 | [diff] [blame] | 1568 | oist->ist[v] = t->x86_tss.ist[v] = |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1569 | (unsigned long)estacks; |
Steven Rostedt | 228bdaa | 2011-12-09 03:02:19 -0500 | [diff] [blame] | 1570 | if (v == DEBUG_STACK-1) |
| 1571 | per_cpu(debug_stack_addr, cpu) = (unsigned long)estacks; |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
Andy Lutomirski | 7fb983b | 2017-12-04 15:07:17 +0100 | [diff] [blame] | 1575 | t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET; |
Ingo Molnar | 0f3fa48 | 2009-03-14 08:46:17 +0100 | [diff] [blame] | 1576 | |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1577 | /* |
| 1578 | * <= is required because the CPU will access up to |
| 1579 | * 8 bits beyond the end of the IO permission bitmap. |
| 1580 | */ |
| 1581 | for (i = 0; i <= IO_BITMAP_LONGS; i++) |
| 1582 | t->io_bitmap[i] = ~0UL; |
| 1583 | |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 1584 | mmgrab(&init_mm); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1585 | me->active_mm = &init_mm; |
Stoyan Gaydarov | 8c5dfd2 | 2009-03-10 00:10:32 -0500 | [diff] [blame] | 1586 | BUG_ON(me->mm); |
Andy Lutomirski | 72c0098 | 2017-09-06 19:54:53 -0700 | [diff] [blame] | 1587 | initialize_tlbstate_and_flush(); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1588 | enter_lazy_tlb(&init_mm, me); |
| 1589 | |
Andy Lutomirski | 20bb834 | 2017-11-02 00:59:13 -0700 | [diff] [blame] | 1590 | /* |
Andy Lutomirski | 7f2590a | 2017-12-04 15:07:23 +0100 | [diff] [blame] | 1591 | * Initialize the TSS. sp0 points to the entry trampoline stack |
| 1592 | * regardless of what task is running. |
Andy Lutomirski | 20bb834 | 2017-11-02 00:59:13 -0700 | [diff] [blame] | 1593 | */ |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 1594 | set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1595 | load_TR_desc(); |
Dave Hansen | 4fe2d8b | 2017-12-04 17:25:07 -0800 | [diff] [blame] | 1596 | load_sp0((unsigned long)(cpu_entry_stack(cpu) + 1)); |
Andy Lutomirski | 20bb834 | 2017-11-02 00:59:13 -0700 | [diff] [blame] | 1597 | |
Andy Lutomirski | 37868fe | 2015-07-30 14:31:32 -0700 | [diff] [blame] | 1598 | load_mm_ldt(&init_mm); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1599 | |
Jason Wessel | 0bb9fef | 2010-05-20 21:04:30 -0500 | [diff] [blame] | 1600 | clear_all_debug_regs(); |
| 1601 | dbg_restore_debug_regs(); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1602 | |
Ingo Molnar | 21c4cd1 | 2015-04-26 14:27:17 +0200 | [diff] [blame] | 1603 | fpu__init_cpu(); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1604 | |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1605 | if (is_uv_system()) |
| 1606 | uv_cpu_init(); |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 1607 | |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 1608 | load_fixmap_gdt(cpu); |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | #else |
| 1612 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 1613 | void cpu_init(void) |
James Bottomley | 9ee79a3 | 2007-01-22 09:18:31 -0600 | [diff] [blame] | 1614 | { |
Rusty Russell | d2cbcc4 | 2007-05-02 19:27:10 +0200 | [diff] [blame] | 1615 | int cpu = smp_processor_id(); |
| 1616 | struct task_struct *curr = current; |
Andy Lutomirski | c482fee | 2017-12-04 15:07:29 +0100 | [diff] [blame] | 1617 | struct tss_struct *t = &per_cpu(cpu_tss_rw, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
Igor Mammedov | ce4b1b1 | 2014-06-20 14:23:11 +0200 | [diff] [blame] | 1619 | wait_for_master_cpu(cpu); |
Fenghua Yu | e6ebf5d | 2012-12-20 23:44:24 -0800 | [diff] [blame] | 1620 | |
Steven Rostedt | 5b2bdbc | 2015-02-27 14:50:19 -0500 | [diff] [blame] | 1621 | /* |
| 1622 | * Initialize the CR4 shadow before doing anything that could |
| 1623 | * try to read it. |
| 1624 | */ |
| 1625 | cr4_init_shadow(); |
| 1626 | |
Igor Mammedov | ce4b1b1 | 2014-06-20 14:23:11 +0200 | [diff] [blame] | 1627 | show_ucode_info_early(); |
Jeremy Fitzhardinge | 6211119 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 1628 | |
Chen Yucong | 1b74dde | 2016-02-02 11:45:02 +0800 | [diff] [blame] | 1629 | pr_info("Initializing CPU#%d\n", cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Borislav Petkov | 362f924 | 2015-12-07 10:39:41 +0100 | [diff] [blame] | 1631 | if (cpu_feature_enabled(X86_FEATURE_VME) || |
Borislav Petkov | 59e21e3 | 2016-04-04 22:24:59 +0200 | [diff] [blame] | 1632 | boot_cpu_has(X86_FEATURE_TSC) || |
Borislav Petkov | 362f924 | 2015-12-07 10:39:41 +0100 | [diff] [blame] | 1633 | boot_cpu_has(X86_FEATURE_DE)) |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 1634 | cr4_clear_bits(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | |
Seiji Aguchi | cf910e8 | 2013-06-20 11:46:53 -0400 | [diff] [blame] | 1636 | load_current_idt(); |
Brian Gerst | 552be87 | 2009-01-30 17:47:53 +0900 | [diff] [blame] | 1637 | switch_to_new_gdt(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
| 1639 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | * Set up and load the per-CPU TSS and LDT |
| 1641 | */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 1642 | mmgrab(&init_mm); |
Jeremy Fitzhardinge | 6211119 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 1643 | curr->active_mm = &init_mm; |
Stoyan Gaydarov | 8c5dfd2 | 2009-03-10 00:10:32 -0500 | [diff] [blame] | 1644 | BUG_ON(curr->mm); |
Andy Lutomirski | 72c0098 | 2017-09-06 19:54:53 -0700 | [diff] [blame] | 1645 | initialize_tlbstate_and_flush(); |
Jeremy Fitzhardinge | 6211119 | 2006-12-07 02:14:02 +0100 | [diff] [blame] | 1646 | enter_lazy_tlb(&init_mm, curr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
Andy Lutomirski | 20bb834 | 2017-11-02 00:59:13 -0700 | [diff] [blame] | 1648 | /* |
| 1649 | * Initialize the TSS. Don't bother initializing sp0, as the initial |
| 1650 | * task never enters user mode. |
| 1651 | */ |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 1652 | set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | load_TR_desc(); |
Andy Lutomirski | 20bb834 | 2017-11-02 00:59:13 -0700 | [diff] [blame] | 1654 | |
Andy Lutomirski | 37868fe | 2015-07-30 14:31:32 -0700 | [diff] [blame] | 1655 | load_mm_ldt(&init_mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
Andy Lutomirski | 7fb983b | 2017-12-04 15:07:17 +0100 | [diff] [blame] | 1657 | t->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET; |
Thomas Gleixner | f9a196b | 2009-05-01 20:59:25 +0200 | [diff] [blame] | 1658 | |
Matt Mackall | 22c4e30 | 2006-01-08 01:05:24 -0800 | [diff] [blame] | 1659 | #ifdef CONFIG_DOUBLEFAULT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | /* Set up doublefault TSS pointer in the GDT */ |
| 1661 | __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss); |
Matt Mackall | 22c4e30 | 2006-01-08 01:05:24 -0800 | [diff] [blame] | 1662 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Jaswinder Singh Rajput | 9766cdb | 2009-03-14 11:19:49 +0530 | [diff] [blame] | 1664 | clear_all_debug_regs(); |
Jason Wessel | 0bb9fef | 2010-05-20 21:04:30 -0500 | [diff] [blame] | 1665 | dbg_restore_debug_regs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
Ingo Molnar | 21c4cd1 | 2015-04-26 14:27:17 +0200 | [diff] [blame] | 1667 | fpu__init_cpu(); |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 1668 | |
Thomas Garnier | 69218e4 | 2017-03-14 10:05:07 -0700 | [diff] [blame] | 1669 | load_fixmap_gdt(cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | } |
Yinghai Lu | 1ba7658 | 2008-09-04 20:09:04 -0700 | [diff] [blame] | 1671 | #endif |
Borislav Petkov | 5700f74 | 2013-06-09 12:07:32 +0200 | [diff] [blame] | 1672 | |
Laura Abbott | b51ef52 | 2015-07-20 14:47:58 -0700 | [diff] [blame] | 1673 | static void bsp_resume(void) |
| 1674 | { |
| 1675 | if (this_cpu->c_bsp_resume) |
| 1676 | this_cpu->c_bsp_resume(&boot_cpu_data); |
| 1677 | } |
| 1678 | |
| 1679 | static struct syscore_ops cpu_syscore_ops = { |
| 1680 | .resume = bsp_resume, |
| 1681 | }; |
| 1682 | |
| 1683 | static int __init init_cpu_syscore(void) |
| 1684 | { |
| 1685 | register_syscore_ops(&cpu_syscore_ops); |
| 1686 | return 0; |
| 1687 | } |
| 1688 | core_initcall(init_cpu_syscore); |