blob: abe6df15a8fbb798bf9cb8bfec5252494a943e63 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Enhanced CPU detection and feature setting code by Mike Jagdis
7 * and Martin Mares, November 1997.
8 */
9
10.text
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/threads.h>
Sam Ravnborg8b2f7ff2008-01-30 13:33:28 +010012#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/linkage.h>
14#include <asm/segment.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080015#include <asm/page_types.h>
16#include <asm/pgtable_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/cache.h>
18#include <asm/thread_info.h>
Sam Ravnborg86feeaa2005-09-09 19:28:28 +020019#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/setup.h>
Ian Campbell551889a62008-02-09 23:24:09 +010021#include <asm/processor-flags.h>
H. Peter Anvin8a50e512009-11-13 15:28:13 -080022#include <asm/msr-index.h>
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010023#include <asm/cpufeatures.h>
Tejun Heo60a53172009-02-09 22:17:40 +090024#include <asm/percpu.h>
H. Peter Anvin4c5023a2012-04-18 17:16:50 -070025#include <asm/nops.h>
Alexander Kuleshovfb148d82015-02-19 13:34:58 +060026#include <asm/bootparam.h>
Al Viro784d5692016-01-11 11:04:34 -050027#include <asm/export.h>
Boris Ostrovsky1e620f92016-12-08 11:44:31 -050028#include <asm/pgtable_32.h>
Ian Campbell551889a62008-02-09 23:24:09 +010029
30/* Physical address */
31#define pa(X) ((X) - __PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * References to members of the new_cpu_data structure.
35 */
36
37#define X86 new_cpu_data+CPUINFO_x86
38#define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
39#define X86_MODEL new_cpu_data+CPUINFO_x86_model
Jia Zhangb3991512018-01-01 09:52:10 +080040#define X86_STEPPING new_cpu_data+CPUINFO_x86_stepping
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
42#define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
43#define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
44#define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id
45
Jeremy Fitzhardinge9ce8c2e2007-05-02 19:27:16 +020046
Josh Poimboeuf22dc3912016-09-21 16:04:06 -050047#define SIZEOF_PTREGS 17*4
48
Borislav Petkov04c17342015-06-19 13:49:06 +020049/*
Jeremy Fitzhardingec090f532009-03-16 12:07:54 -070050 * Worst-case size of the kernel mapping we need to make:
H. Peter Anvin147dd562010-12-16 19:11:09 -080051 * a relocatable kernel can live anywhere in lowmem, so we need to be able
52 * to map all of lowmem.
Jeremy Fitzhardingec090f532009-03-16 12:07:54 -070053 */
H. Peter Anvin147dd562010-12-16 19:11:09 -080054KERNEL_PAGES = LOWMEM_PAGES
Jeremy Fitzhardingec090f532009-03-16 12:07:54 -070055
Stratos Psomadakis7bf04be2011-02-25 22:46:13 +020056INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE
Yinghai Lu2bd27532009-03-09 01:15:57 -070057RESERVE_BRK(pagetables, INIT_MAP_SIZE)
Jeremy Fitzhardinge796216a2009-03-12 16:09:49 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * 32-bit kernel entrypoint; only used by the boot CPU. On entry,
61 * %esi points to the real-mode code as a 32-bit pointer.
62 * CS and DS must be 4 GB flat segments, but we don't depend on
63 * any particular GDT layout, because we load our own as soon as we
64 * can.
65 */
Tim Abbott4ae59b92009-09-16 16:44:28 -040066__HEAD
Linus Torvalds1da177e2005-04-16 15:20:36 -070067ENTRY(startup_32)
Josh Poimboeufb32f96c2016-08-18 10:59:03 -050068 movl pa(initial_stack),%ecx
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -080069
Rusty Russella24e7852007-10-21 16:41:35 -070070 /* test KEEP_SEGMENTS flag to see if the bootloader is asking
71 us to not reload segments */
Alexander Kuleshovfb148d82015-02-19 13:34:58 +060072 testb $KEEP_SEGMENTS, BP_loadflags(%esi)
Rusty Russella24e7852007-10-21 16:41:35 -070073 jnz 2f
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Set segments to known values.
77 */
Ian Campbell551889a62008-02-09 23:24:09 +010078 lgdt pa(boot_gdt_descr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 movl $(__BOOT_DS),%eax
80 movl %eax,%ds
81 movl %eax,%es
82 movl %eax,%fs
83 movl %eax,%gs
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -080084 movl %eax,%ss
Rusty Russella24e7852007-10-21 16:41:35 -0700852:
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -080086 leal -__PAGE_OFFSET(%ecx),%esp
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88/*
89 * Clear BSS first so that there are no surprises...
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
Rusty Russella24e7852007-10-21 16:41:35 -070091 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 xorl %eax,%eax
Ian Campbell551889a62008-02-09 23:24:09 +010093 movl $pa(__bss_start),%edi
94 movl $pa(__bss_stop),%ecx
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 subl %edi,%ecx
96 shrl $2,%ecx
97 rep ; stosl
Vivek Goyal484b90c2005-09-03 15:56:31 -070098/*
99 * Copy bootup parameters out of the way.
100 * Note: %esi still has the pointer to the real-mode data.
101 * With the kexec as boot loader, parameter segment might be loaded beyond
102 * kernel image and might not even be addressable by early boot page tables.
103 * (kexec on panic case). Hence copy out the parameters before initializing
104 * page tables.
105 */
Ian Campbell551889a62008-02-09 23:24:09 +0100106 movl $pa(boot_params),%edi
Vivek Goyal484b90c2005-09-03 15:56:31 -0700107 movl $(PARAM_SIZE/4),%ecx
108 cld
109 rep
110 movsl
Ian Campbell551889a62008-02-09 23:24:09 +0100111 movl pa(boot_params) + NEW_CL_POINTER,%esi
Vivek Goyal484b90c2005-09-03 15:56:31 -0700112 andl %esi,%esi
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400113 jz 1f # No command line
Ian Campbell551889a62008-02-09 23:24:09 +0100114 movl $pa(boot_command_line),%edi
Vivek Goyal484b90c2005-09-03 15:56:31 -0700115 movl $(COMMAND_LINE_SIZE/4),%ecx
116 rep
117 movsl
1181:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Thomas Gleixnerdc3119e702011-02-23 10:08:31 +0100120#ifdef CONFIG_OLPC
Andres Salomonfd699c72010-06-18 17:46:53 -0400121 /* save OFW's pgdir table for later use when calling into OFW */
122 movl %cr3, %eax
123 movl %eax, pa(olpc_ofw_pgd)
124#endif
125
Borislav Petkovfe055892015-10-20 11:54:45 +0200126#ifdef CONFIG_MICROCODE
Fenghua Yu63b553c2012-12-20 23:44:29 -0800127 /* Early load ucode on BSP. */
128 call load_ucode_bsp
129#endif
130
Boris Ostrovsky1e620f92016-12-08 11:44:31 -0500131 /* Create early pagetables. */
132 call mk_early_pgtbl_32
Ian Campbell551889a62008-02-09 23:24:09 +0100133
Boris Ostrovsky1e620f92016-12-08 11:44:31 -0500134 /* Do early initialization of the fixmap area */
135 movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
136#ifdef CONFIG_X86_PAE
Joe Korty86b2b702008-06-02 17:21:06 -0400137#define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200138 movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
Boris Ostrovsky1e620f92016-12-08 11:44:31 -0500139#else
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200140 movl %eax,pa(initial_page_table+0xffc)
Ian Campbell551889a62008-02-09 23:24:09 +0100141#endif
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030142
143#ifdef CONFIG_PARAVIRT
144 /* This is can only trip for a broken bootloader... */
145 cmpw $0x207, pa(boot_params + BP_version)
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500146 jb .Ldefault_entry
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030147
148 /* Paravirt-compatible boot parameters. Look to see what architecture
149 we're booting under. */
150 movl pa(boot_params + BP_hardware_subarch), %eax
151 cmpl $num_subarch_entries, %eax
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500152 jae .Lbad_subarch
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030153
154 movl pa(subarch_entries)(,%eax,4), %eax
155 subl $__PAGE_OFFSET, %eax
156 jmp *%eax
157
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500158.Lbad_subarch:
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030159WEAK(xen_entry)
160 /* Unknown implementation; there's really
161 nothing we can do at this point. */
162 ud2a
163
164 __INITDATA
165
166subarch_entries:
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500167 .long .Ldefault_entry /* normal x86/PC */
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030168 .long xen_entry /* Xen hypervisor */
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500169 .long .Ldefault_entry /* Moorestown MID */
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030170num_subarch_entries = (. - subarch_entries) / 4
171.previous
172#else
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500173 jmp .Ldefault_entry
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030174#endif /* CONFIG_PARAVIRT */
175
Fenghua Yu3e2a0cc2012-11-13 11:32:45 -0800176#ifdef CONFIG_HOTPLUG_CPU
177/*
178 * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
179 * up already except stack. We just set up stack here. Then call
180 * start_secondary().
181 */
182ENTRY(start_cpu0)
Josh Poimboeufb32f96c2016-08-18 10:59:03 -0500183 movl initial_stack, %ecx
Fenghua Yu3e2a0cc2012-11-13 11:32:45 -0800184 movl %ecx, %esp
Josh Poimboeuf6616a142016-09-21 16:04:02 -0500185 call *(initial_code)
1861: jmp 1b
Fenghua Yu3e2a0cc2012-11-13 11:32:45 -0800187ENDPROC(start_cpu0)
188#endif
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * Non-boot CPU entry point; entered from trampoline.S
192 * We can't lgdt here, because lgdt itself uses a data segment, but
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200193 * we know the trampoline has already loaded the boot_gdt for us.
Vivek Goyalf8657e12007-02-13 13:26:22 +0100194 *
195 * If cpu hotplug is not supported then this code can go in init section
196 * which will be freed later
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198ENTRY(startup_32_smp)
199 cld
200 movl $(__BOOT_DS),%eax
201 movl %eax,%ds
202 movl %eax,%es
203 movl %eax,%fs
204 movl %eax,%gs
Josh Poimboeufb32f96c2016-08-18 10:59:03 -0500205 movl pa(initial_stack),%ecx
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -0800206 movl %eax,%ss
207 leal -__PAGE_OFFSET(%ecx),%esp
Jarkko Sakkinen48927bb2012-05-08 21:22:28 +0300208
Borislav Petkovfe055892015-10-20 11:54:45 +0200209#ifdef CONFIG_MICROCODE
Fenghua Yu63b553c2012-12-20 23:44:29 -0800210 /* Early load ucode on AP. */
211 call load_ucode_ap
212#endif
213
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500214.Ldefault_entry:
H. Peter Anvin021ef052013-01-19 10:29:37 -0800215 movl $(CR0_STATE & ~X86_CR0_PG),%eax
216 movl %eax,%cr0
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
Borislav Petkov9efb58d2013-02-11 15:22:16 +0100219 * We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave
220 * bits like NT set. This would confuse the debugger if this code is traced. So
221 * initialize them properly now before switching to protected mode. That means
222 * DF in particular (even though we have cleared it earlier after copying the
223 * command line) because GCC expects it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
H. Peter Anvin5a5a51d2012-09-24 16:05:48 -0700225 pushl $0
226 popfl
Borislav Petkov9efb58d2013-02-11 15:22:16 +0100227
228/*
229 * New page tables may be in 4Mbyte page mode and may be using the global pages.
230 *
231 * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists
232 * if and only if CPUID exists and has flags other than the FPU flag set.
233 */
234 movl $-1,pa(X86_CPUID) # preset CPUID level
235 movl $X86_EFLAGS_ID,%ecx
236 pushl %ecx
237 popfl # set EFLAGS=ID
H. Peter Anvin5a5a51d2012-09-24 16:05:48 -0700238 pushfl
Borislav Petkov9efb58d2013-02-11 15:22:16 +0100239 popl %eax # get EFLAGS
240 testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set?
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500241 jz .Lenable_paging # hw disallowed setting of ID bit
Borislav Petkov9efb58d2013-02-11 15:22:16 +0100242 # which means no CPUID and no CR4
243
244 xorl %eax,%eax
245 cpuid
246 movl %eax,pa(X86_CPUID) # save largest std CPUID function
H. Peter Anvin5a5a51d2012-09-24 16:05:48 -0700247
H. Peter Anvin6662c342012-11-27 08:54:36 -0800248 movl $1,%eax
249 cpuid
Borislav Petkov9efb58d2013-02-11 15:22:16 +0100250 andl $~1,%edx # Ignore CPUID.FPU
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500251 jz .Lenable_paging # No flags or only CPUID.FPU = no CR4
H. Peter Anvin6662c342012-11-27 08:54:36 -0800252
H. Peter Anvin5a5a51d2012-09-24 16:05:48 -0700253 movl pa(mmu_cr4_features),%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 movl %eax,%cr4
255
H. Peter Anvin8a50e512009-11-13 15:28:13 -0800256 testb $X86_CR4_PAE, %al # check if PAE is enabled
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500257 jz .Lenable_paging
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Check if extended functions are implemented */
260 movl $0x80000000, %eax
261 cpuid
H. Peter Anvin8a50e512009-11-13 15:28:13 -0800262 /* Value must be in the range 0x80000001 to 0x8000ffff */
263 subl $0x80000001, %eax
264 cmpl $(0x8000ffff-0x80000001), %eax
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500265 ja .Lenable_paging
Kees Cookebba6382010-11-10 10:35:53 -0800266
267 /* Clear bogus XD_DISABLE bits */
268 call verify_cpu
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 mov $0x80000001, %eax
271 cpuid
272 /* Execute Disable bit supported? */
H. Peter Anvin8a50e512009-11-13 15:28:13 -0800273 btl $(X86_FEATURE_NX & 31), %edx
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500274 jnc .Lenable_paging
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* Setup EFER (Extended Feature Enable Register) */
H. Peter Anvin8a50e512009-11-13 15:28:13 -0800277 movl $MSR_EFER, %ecx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 rdmsr
279
H. Peter Anvin8a50e512009-11-13 15:28:13 -0800280 btsl $_EFER_NX, %eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* Make changes effective */
282 wrmsr
283
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500284.Lenable_paging:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/*
287 * Enable paging
288 */
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200289 movl $pa(initial_page_table), %eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 movl %eax,%cr3 /* set the page table pointer.. */
H. Peter Anvin021ef052013-01-19 10:29:37 -0800291 movl $CR0_STATE,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 movl %eax,%cr0 /* ..and set paging (PG) bit */
293 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
2941:
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -0800295 /* Shift the stack pointer to a virtual address */
296 addl $__PAGE_OFFSET, %esp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * start system 32-bit setup. We need to re-do some of the things done
300 * in 16-bit mode for the "real" operations.
301 */
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700302 movl setup_once_ref,%eax
303 andl %eax,%eax
304 jz 1f # Did we do this already?
305 call *%eax
3061:
Borislav Petkov166df912013-02-11 15:22:15 +0100307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
Borislav Petkov166df912013-02-11 15:22:15 +0100309 * Check if it is 486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Wang YanQing237d1542013-06-28 22:45:16 +0800311 movb $4,X86 # at least 486
Borislav Petkovc3a22a22013-02-11 15:22:17 +0100312 cmpl $-1,X86_CPUID
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500313 je .Lis486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 /* get vendor info */
316 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
317 cpuid
318 movl %eax,X86_CPUID # save CPUID level
319 movl %ebx,X86_VENDOR_ID # lo 4 chars
320 movl %edx,X86_VENDOR_ID+4 # next 4 chars
321 movl %ecx,X86_VENDOR_ID+8 # last 4 chars
322
323 orl %eax,%eax # do we have processor info as well?
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500324 je .Lis486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 movl $1,%eax # Use the CPUID instruction to get CPU type
327 cpuid
328 movb %al,%cl # save reg for future use
329 andb $0x0f,%ah # mask processor family
330 movb %ah,X86
331 andb $0xf0,%al # mask model
332 shrb $4,%al
333 movb %al,X86_MODEL
334 andb $0x0f,%cl # mask mask revision
Jia Zhangb3991512018-01-01 09:52:10 +0800335 movb %cl,X86_STEPPING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 movl %edx,X86_CAPABILITY
337
Josh Poimboeuf1b002552016-09-21 16:03:59 -0500338.Lis486:
Borislav Petkovc3a22a22013-02-11 15:22:17 +0100339 movl $0x50022,%ecx # set AM, WP, NE and MP
Borislav Petkov166df912013-02-11 15:22:15 +0100340 movl %cr0,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 andl $0x80000011,%eax # Save PG,PE,ET
342 orl %ecx,%eax
343 movl %eax,%cr0
344
Rusty Russell2a57ff12007-02-13 13:26:26 +0100345 lgdt early_gdt_descr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ljmp $(__KERNEL_CS),$1f
3471: movl $(__KERNEL_DS),%eax # reload all the segment registers
348 movl %eax,%ss # after changing gdt.
349
350 movl $(__USER_DS),%eax # DS/ES contains default USER segment
351 movl %eax,%ds
352 movl %eax,%es
353
Brian Gerst0dd76d72009-01-21 17:26:05 +0900354 movl $(__KERNEL_PERCPU), %eax
355 movl %eax,%fs # set this cpu's percpu
356
Tejun Heo60a53172009-02-09 22:17:40 +0900357 movl $(__KERNEL_STACK_CANARY),%eax
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100358 movl %eax,%gs
Tejun Heo60a53172009-02-09 22:17:40 +0900359
360 xorl %eax,%eax # Clear LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 lldt %ax
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100362
Josh Poimboeuf6616a142016-09-21 16:04:02 -0500363 call *(initial_code)
3641: jmp 1b
365ENDPROC(startup_32_smp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700367#include "verify_cpu.S"
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700370 * setup_once
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700372 * The setup work we only want to run on the BSP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
374 * Warning: %esi is live across this function.
375 */
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700376__INIT
377setup_once:
Linus Torvalds050e9ba2018-06-14 12:21:18 +0900378#ifdef CONFIG_STACKPROTECTOR
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700379 /*
380 * Configure the stack canary. The linker can't handle this by
381 * relocation. Manually set base address in stack canary
382 * segment descriptor.
383 */
384 movl $gdt_page,%eax
385 movl $stack_canary,%ecx
386 movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax)
387 shrl $16, %ecx
388 movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax)
389 movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax)
390#endif
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200391
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700392 andl $0,setup_once_ref /* Once is enough, thanks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ret
394
Andy Lutomirski425be562015-05-22 16:15:47 -0700395ENTRY(early_idt_handler_array)
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700396 # 36(%esp) %eflags
397 # 32(%esp) %cs
398 # 28(%esp) %eip
399 # 24(%rsp) error code
400 i = 0
401 .rept NUM_EXCEPTION_VECTORS
Josh Poimboeuf82c62fa2017-10-20 11:21:35 -0500402 .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700403 pushl $0 # Dummy error code, to make stack frame uniform
404 .endif
405 pushl $i # 20(%esp) Vector number
Andy Lutomirski425be562015-05-22 16:15:47 -0700406 jmp early_idt_handler_common
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700407 i = i + 1
Andy Lutomirski425be562015-05-22 16:15:47 -0700408 .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700409 .endr
Andy Lutomirski425be562015-05-22 16:15:47 -0700410ENDPROC(early_idt_handler_array)
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700411
Andy Lutomirski425be562015-05-22 16:15:47 -0700412early_idt_handler_common:
413 /*
414 * The stack is the hardware frame, an error code or zero, and the
415 * vector number.
416 */
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200417 cld
H. Peter Anvin5fa10192014-03-07 15:05:20 -0800418
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700419 incl %ss:early_recursion_flag
420
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700421 /* The vector number is in pt_regs->gs */
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700422
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700423 cld
Andy Lutomirski630c1862017-07-28 06:00:31 -0700424 pushl %fs /* pt_regs->fs (__fsh varies by model) */
425 pushl %es /* pt_regs->es (__esh varies by model) */
426 pushl %ds /* pt_regs->ds (__dsh varies by model) */
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700427 pushl %eax /* pt_regs->ax */
428 pushl %ebp /* pt_regs->bp */
429 pushl %edi /* pt_regs->di */
430 pushl %esi /* pt_regs->si */
431 pushl %edx /* pt_regs->dx */
432 pushl %ecx /* pt_regs->cx */
433 pushl %ebx /* pt_regs->bx */
434
435 /* Fix up DS and ES */
436 movl $(__KERNEL_DS), %ecx
437 movl %ecx, %ds
438 movl %ecx, %es
439
440 /* Load the vector number into EDX */
441 movl PT_GS(%esp), %edx
442
Andy Lutomirski630c1862017-07-28 06:00:31 -0700443 /* Load GS into pt_regs->gs (and maybe clobber __gsh) */
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700444 movw %gs, PT_GS(%esp)
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700445
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700446 movl %esp, %eax /* args are pt_regs (EAX), trapnr (EDX) */
447 call early_fixup_exception
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700448
449 popl %ebx /* pt_regs->bx */
450 popl %ecx /* pt_regs->cx */
451 popl %edx /* pt_regs->dx */
452 popl %esi /* pt_regs->si */
453 popl %edi /* pt_regs->di */
454 popl %ebp /* pt_regs->bp */
455 popl %eax /* pt_regs->ax */
Andy Lutomirski630c1862017-07-28 06:00:31 -0700456 popl %ds /* pt_regs->ds (always ignores __dsh) */
457 popl %es /* pt_regs->es (always ignores __esh) */
458 popl %fs /* pt_regs->fs (always ignores __fsh) */
459 popl %gs /* pt_regs->gs (always ignores __gsh) */
Andy Lutomirski7bbcdb12016-04-02 07:01:32 -0700460 decl %ss:early_recursion_flag
461 addl $4, %esp /* pop pt_regs->orig_ax */
462 iret
Andy Lutomirski425be562015-05-22 16:15:47 -0700463ENDPROC(early_idt_handler_common)
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/* This is the default interrupt "handler" :-) */
Thomas Gleixner87e81782017-08-28 08:47:48 +0200466ENTRY(early_ignore_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 cld
Matt Mackalld59745c2005-05-01 08:59:02 -0700468#ifdef CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 pushl %eax
470 pushl %ecx
471 pushl %edx
472 pushl %es
473 pushl %ds
474 movl $(__KERNEL_DS),%eax
475 movl %eax,%ds
476 movl %eax,%es
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200477 cmpl $2,early_recursion_flag
478 je hlt_loop
479 incl early_recursion_flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 pushl 16(%esp)
481 pushl 24(%esp)
482 pushl 32(%esp)
483 pushl 40(%esp)
484 pushl $int_msg
485 call printk
Ingo Molnard5e397c2009-01-26 06:09:00 +0100486
487 call dump_stack
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 addl $(5*4),%esp
490 popl %ds
491 popl %es
492 popl %edx
493 popl %ecx
494 popl %eax
Matt Mackalld59745c2005-05-01 08:59:02 -0700495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 iret
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700497
498hlt_loop:
499 hlt
500 jmp hlt_loop
Thomas Gleixner87e81782017-08-28 08:47:48 +0200501ENDPROC(early_ignore_irq)
Jiri Slaby04b5de32017-08-31 14:16:53 +0200502
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700503__INITDATA
504 .align 4
Andy Lutomirski0e861fb2016-04-02 07:01:34 -0700505GLOBAL(early_recursion_flag)
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700506 .long 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700508__REFDATA
509 .align 4
Thomas Gleixner583323b2008-07-27 21:43:11 +0200510ENTRY(initial_code)
511 .long i386_start_kernel
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700512ENTRY(setup_once_ref)
513 .long setup_once
Thomas Gleixner583323b2008-07-27 21:43:11 +0200514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*
516 * BSS section
517 */
Tim Abbott02b7da32009-09-20 18:14:14 -0400518__PAGE_ALIGNED_BSS
Stratos Psomadakis7bf04be2011-02-25 22:46:13 +0200519 .align PAGE_SIZE
Ian Campbell551889a62008-02-09 23:24:09 +0100520#ifdef CONFIG_X86_PAE
Boris Ostrovsky1e620f92016-12-08 11:44:31 -0500521.globl initial_pg_pmd
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030522initial_pg_pmd:
Ian Campbell551889a62008-02-09 23:24:09 +0100523 .fill 1024*KPMDS,4,0
524#else
Arnd Bergmann553bbc12016-11-16 15:17:09 +0100525.globl initial_page_table
526initial_page_table:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .fill 1024,4,0
Ian Campbell551889a62008-02-09 23:24:09 +0100528#endif
Rusty Russelld50d8fe2011-01-04 17:20:54 +1030529initial_pg_fixmap:
Eric W. Bidermanb1c931e2007-07-15 23:37:28 -0700530 .fill 1024,4,0
Arnd Bergmann553bbc12016-11-16 15:17:09 +0100531.globl empty_zero_page
532empty_zero_page:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .fill 4096,1,0
Arnd Bergmann553bbc12016-11-16 15:17:09 +0100534.globl swapper_pg_dir
535swapper_pg_dir:
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200536 .fill 1024,4,0
Al Viro784d5692016-01-11 11:04:34 -0500537EXPORT_SYMBOL(empty_zero_page)
Yinghai Lu2bd27532009-03-09 01:15:57 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*
540 * This starts the data section.
541 */
Ian Campbell551889a62008-02-09 23:24:09 +0100542#ifdef CONFIG_X86_PAE
Tim Abbottabe1ee32009-09-20 18:14:15 -0400543__PAGE_ALIGNED_DATA
Ian Campbell551889a62008-02-09 23:24:09 +0100544 /* Page-aligned for the benefit of paravirt? */
Stratos Psomadakis7bf04be2011-02-25 22:46:13 +0200545 .align PAGE_SIZE
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200546ENTRY(initial_page_table)
547 .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
Ian Campbell551889a62008-02-09 23:24:09 +0100548# if KPMDS == 3
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200549 .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
550 .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
551 .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0
Ian Campbell551889a62008-02-09 23:24:09 +0100552# elif KPMDS == 2
553 .long 0,0
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200554 .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
555 .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
Ian Campbell551889a62008-02-09 23:24:09 +0100556# elif KPMDS == 1
557 .long 0,0
558 .long 0,0
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200559 .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
Ian Campbell551889a62008-02-09 23:24:09 +0100560# else
561# error "Kernel PMDs should be 1, 2 or 3"
562# endif
Stratos Psomadakis7bf04be2011-02-25 22:46:13 +0200563 .align PAGE_SIZE /* needs to be page-sized too */
Ian Campbell551889a62008-02-09 23:24:09 +0100564#endif
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566.data
H. Peter Anvin11d4c3f2011-02-04 16:14:11 -0800567.balign 4
Josh Poimboeufb32f96c2016-08-18 10:59:03 -0500568ENTRY(initial_stack)
Josh Poimboeuf22dc3912016-09-21 16:04:06 -0500569 /*
570 * The SIZEOF_PTREGS gap is a convention which helps the in-kernel
571 * unwinder reliably detect the end of the stack.
572 */
573 .long init_thread_union + THREAD_SIZE - SIZEOF_PTREGS - \
574 TOP_OF_KERNEL_STACK_PADDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700576__INITRODATA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577int_msg:
Ingo Molnard5e397c2009-01-26 06:09:00 +0100578 .asciz "Unknown interrupt or fault at: %p %p %p\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Thomas Gleixner97027852007-10-11 11:16:51 +0200580#include "../../x86/xen/xen-head.S"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * The IDT and GDT 'descriptors' are a strange 48-bit object
584 * only used by the lidt and lgdt instructions. They are not
585 * like usual segment descriptors - they consist of a 16-bit
586 * segment size, and 32-bit linear address value:
587 */
588
H. Peter Anvin4c5023a2012-04-18 17:16:50 -0700589 .data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590.globl boot_gdt_descr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 ALIGN
593# early boot GDT descriptor (must use 1:1 address mapping)
594 .word 0 # 32 bit align gdt_desc.address
595boot_gdt_descr:
596 .word __BOOT_DS+7
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200597 .long boot_gdt - __PAGE_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599# boot GDT descriptor (later on used by CPU#0):
600 .word 0 # 32 bit align gdt_desc.address
Rusty Russell2a57ff12007-02-13 13:26:26 +0100601ENTRY(early_gdt_descr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 .word GDT_ENTRIES*8-1
Rusty Russelldd17c8f2009-10-29 22:34:15 +0900603 .long gdt_page /* Overwritten for secondary CPUs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200606 * The boot_gdt must mirror the equivalent in setup.S and is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * used only for booting.
608 */
609 .align L1_CACHE_BYTES
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200610ENTRY(boot_gdt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 .fill GDT_ENTRY_BOOT_CS,8,0
612 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
613 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */