xref: /linux/arch/x86/kernel/head_32.S (revision 22bdd6e68bbe270a916233ec5f34a13ae5e80ed9)
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 *
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
11#include <linux/export.h>
12#include <linux/threads.h>
13#include <linux/init.h>
14#include <linux/linkage.h>
15#include <asm/segment.h>
16#include <asm/page_types.h>
17#include <asm/pgtable_types.h>
18#include <asm/cache.h>
19#include <asm/thread_info.h>
20#include <asm/asm-offsets.h>
21#include <asm/setup.h>
22#include <asm/processor-flags.h>
23#include <asm/msr-index.h>
24#include <asm/cpufeatures.h>
25#include <asm/percpu.h>
26#include <asm/nops.h>
27#include <asm/nospec-branch.h>
28#include <asm/bootparam.h>
29#include <asm/pgtable_32.h>
30
31/* Physical address */
32#define pa(X) ((X) - __PAGE_OFFSET)
33
34/*
35 * References to members of the new_cpu_data structure.
36 */
37
38#define X86		new_cpu_data+CPUINFO_x86
39#define X86_VENDOR	new_cpu_data+CPUINFO_x86_vendor
40#define X86_MODEL	new_cpu_data+CPUINFO_x86_model
41#define X86_STEPPING	new_cpu_data+CPUINFO_x86_stepping
42#define X86_HARD_MATH	new_cpu_data+CPUINFO_hard_math
43#define X86_CPUID	new_cpu_data+CPUINFO_cpuid_level
44#define X86_CAPABILITY	new_cpu_data+CPUINFO_x86_capability
45#define X86_VENDOR_ID	new_cpu_data+CPUINFO_x86_vendor_id
46
47/*
48 * Worst-case size of the kernel mapping we need to make:
49 * a relocatable kernel can live anywhere in lowmem, so we need to be able
50 * to map all of lowmem.
51 */
52KERNEL_PAGES = LOWMEM_PAGES
53
54INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE
55RESERVE_BRK(pagetables, INIT_MAP_SIZE)
56
57/*
58 * 32-bit kernel entrypoint; only used by the boot CPU.  On entry,
59 * %esi points to the real-mode code as a 32-bit pointer.
60 * CS and DS must be 4 GB flat segments, but we don't depend on
61 * any particular GDT layout, because we load our own as soon as we
62 * can.
63 */
64	__INIT
65SYM_CODE_START(startup_32)
66	movl pa(initial_stack),%ecx
67
68/*
69 * Set segments to known values.
70 */
71	lgdt pa(boot_gdt_descr)
72	movl $(__BOOT_DS),%eax
73	movl %eax,%ds
74	movl %eax,%es
75	movl %eax,%fs
76	movl %eax,%gs
77	movl %eax,%ss
78	leal -__PAGE_OFFSET(%ecx),%esp
79
80/*
81 * Clear BSS first so that there are no surprises...
82 */
83	cld
84	xorl %eax,%eax
85	movl $pa(__bss_start),%edi
86	movl $pa(__bss_stop),%ecx
87	subl %edi,%ecx
88	shrl $2,%ecx
89	rep stosl
90/*
91 * Copy bootup parameters out of the way.
92 * Note: %esi still has the pointer to the real-mode data.
93 * With the kexec as boot loader, parameter segment might be loaded beyond
94 * kernel image and might not even be addressable by early boot page tables.
95 * (kexec on panic case). Hence copy out the parameters before initializing
96 * page tables.
97 */
98	movl $pa(boot_params),%edi
99	movl $(PARAM_SIZE/4),%ecx
100	cld
101	rep movsl
102	movl pa(boot_params) + NEW_CL_POINTER,%esi
103	andl %esi,%esi
104	jz 1f			# No command line
105	movl $pa(boot_command_line),%edi
106	movl $(COMMAND_LINE_SIZE/4),%ecx
107	rep movsl
1081:
109
110#ifdef CONFIG_OLPC
111	/* save OFW's pgdir table for later use when calling into OFW */
112	movl %cr3, %eax
113	movl %eax, pa(olpc_ofw_pgd)
114#endif
115
116	/* Create early pagetables. */
117	call  mk_early_pgtbl_32
118
119	/* Do early initialization of the fixmap area */
120	movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
121#ifdef  CONFIG_X86_PAE
122#define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
123	movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
124#else
125	movl %eax,pa(initial_page_table+0xffc)
126#endif
127
128	jmp .Ldefault_entry
129SYM_CODE_END(startup_32)
130
131/*
132 * Non-boot CPU entry point; entered from trampoline.S
133 * We can't lgdt here, because lgdt itself uses a data segment, but
134 * we know the trampoline has already loaded the boot_gdt for us.
135 *
136 * If cpu hotplug is not supported then this code can go in init section
137 * which will be freed later
138 */
139#ifdef CONFIG_HOTPLUG_CPU
140       .text
141#endif
142SYM_FUNC_START(startup_32_smp)
143	cld
144	movl $(__BOOT_DS),%eax
145	movl %eax,%ds
146	movl %eax,%es
147	movl %eax,%fs
148	movl %eax,%gs
149	movl pa(initial_stack),%ecx
150	movl %eax,%ss
151	leal -__PAGE_OFFSET(%ecx),%esp
152
153.Ldefault_entry:
154	movl $(CR0_STATE & ~X86_CR0_PG),%eax
155	movl %eax,%cr0
156
157/*
158 * We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave
159 * bits like NT set. This would confuse the debugger if this code is traced. So
160 * initialize them properly now before switching to protected mode. That means
161 * DF in particular (even though we have cleared it earlier after copying the
162 * command line) because GCC expects it.
163 */
164	pushl $0
165	popfl
166
167/*
168 * New page tables may be in 4Mbyte page mode and may be using the global pages.
169 *
170 * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists
171 * if and only if CPUID exists and has flags other than the FPU flag set.
172 */
173	movl $-1,pa(X86_CPUID)		# preset CPUID level
174	movl $X86_EFLAGS_ID,%ecx
175	pushl %ecx
176	popfl				# set EFLAGS=ID
177	pushfl
178	popl %eax			# get EFLAGS
179	testl $X86_EFLAGS_ID,%eax	# did EFLAGS.ID remained set?
180	jz .Lenable_paging		# hw disallowed setting of ID bit
181					# which means no CPUID and no CR4
182
183	xorl %eax,%eax
184	cpuid
185	movl %eax,pa(X86_CPUID)		# save largest std CPUID function
186
187	movl $1,%eax
188	cpuid
189	andl $~1,%edx			# Ignore CPUID.FPU
190	jz .Lenable_paging		# No flags or only CPUID.FPU = no CR4
191
192	movl pa(mmu_cr4_features),%eax
193	movl %eax,%cr4
194
195	testb $X86_CR4_PAE, %al		# check if PAE is enabled
196	jz .Lenable_paging
197
198	/* Check if extended functions are implemented */
199	movl $0x80000000, %eax
200	cpuid
201	/* Value must be in the range 0x80000001 to 0x8000ffff */
202	subl $0x80000001, %eax
203	cmpl $(0x8000ffff-0x80000001), %eax
204	ja .Lenable_paging
205
206	/* Clear bogus XD_DISABLE bits */
207	call verify_cpu
208
209	mov $0x80000001, %eax
210	cpuid
211	/* Execute Disable bit supported? */
212	btl $(X86_FEATURE_NX & 31), %edx
213	jnc .Lenable_paging
214
215	/* Setup EFER (Extended Feature Enable Register) */
216	movl $MSR_EFER, %ecx
217	rdmsr
218
219	btsl $_EFER_NX, %eax
220	/* Make changes effective */
221	wrmsr
222
223.Lenable_paging:
224
225/*
226 * Enable paging
227 */
228	movl $pa(initial_page_table), %eax
229	movl %eax,%cr3		/* set the page table pointer.. */
230	movl $CR0_STATE,%eax
231	movl %eax,%cr0		/* ..and set paging (PG) bit */
232	ljmp $__BOOT_CS,$1f	/* Clear prefetch and normalize %eip */
2331:
234	/* Shift the stack pointer to a virtual address */
235	addl $__PAGE_OFFSET, %esp
236
237/*
238 * Check if it is 486
239 */
240	movb $4,X86			# at least 486
241	cmpl $-1,X86_CPUID
242	je .Lis486
243
244	/* get vendor info */
245	xorl %eax,%eax			# call CPUID with 0 -> return vendor ID
246	cpuid
247	movl %eax,X86_CPUID		# save CPUID level
248	movl %ebx,X86_VENDOR_ID		# lo 4 chars
249	movl %edx,X86_VENDOR_ID+4	# next 4 chars
250	movl %ecx,X86_VENDOR_ID+8	# last 4 chars
251
252	orl %eax,%eax			# do we have processor info as well?
253	je .Lis486
254
255	movl $1,%eax		# Use the CPUID instruction to get CPU type
256	cpuid
257	movb %al,%cl		# save reg for future use
258	andb $0x0f,%ah		# mask processor family
259	movb %ah,X86
260	andb $0xf0,%al		# mask model
261	shrb $4,%al
262	movb %al,X86_MODEL
263	andb $0x0f,%cl		# mask mask revision
264	movb %cl,X86_STEPPING
265	movl %edx,X86_CAPABILITY
266
267.Lis486:
268	movl $0x50022,%ecx	# set AM, WP, NE and MP
269	movl %cr0,%eax
270	andl $0x80000011,%eax	# Save PG,PE,ET
271	orl %ecx,%eax
272	movl %eax,%cr0
273
274	lgdt early_gdt_descr
275	ljmp $(__KERNEL_CS),$1f
2761:	movl $(__KERNEL_DS),%eax	# reload all the segment registers
277	movl %eax,%ss			# after changing gdt.
278
279	movl $(__USER_DS),%eax		# DS/ES contains default USER segment
280	movl %eax,%ds
281	movl %eax,%es
282
283	movl $(__KERNEL_PERCPU), %eax
284	movl %eax,%fs			# set this cpu's percpu
285
286	xorl %eax,%eax
287	movl %eax,%gs			# clear possible garbage in %gs
288
289	xorl %eax,%eax			# Clear LDT
290	lldt %ax
291
292	call *(initial_code)
2931:	jmp 1b
294SYM_FUNC_END(startup_32_smp)
295
296#include "verify_cpu.S"
297
298__INIT
299SYM_FUNC_START(early_idt_handler_array)
300	# 36(%esp) %eflags
301	# 32(%esp) %cs
302	# 28(%esp) %eip
303	# 24(%rsp) error code
304	i = 0
305	.rept NUM_EXCEPTION_VECTORS
306	.if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
307	pushl $0		# Dummy error code, to make stack frame uniform
308	.endif
309	pushl $i		# 20(%esp) Vector number
310	jmp early_idt_handler_common
311	i = i + 1
312	.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
313	.endr
314SYM_FUNC_END(early_idt_handler_array)
315
316SYM_CODE_START_LOCAL(early_idt_handler_common)
317	/*
318	 * The stack is the hardware frame, an error code or zero, and the
319	 * vector number.
320	 */
321	cld
322
323	incl %ss:early_recursion_flag
324
325	/* The vector number is in pt_regs->gs */
326
327	cld
328	pushl	%fs		/* pt_regs->fs (__fsh varies by model) */
329	pushl	%es		/* pt_regs->es (__esh varies by model) */
330	pushl	%ds		/* pt_regs->ds (__dsh varies by model) */
331	pushl	%eax		/* pt_regs->ax */
332	pushl	%ebp		/* pt_regs->bp */
333	pushl	%edi		/* pt_regs->di */
334	pushl	%esi		/* pt_regs->si */
335	pushl	%edx		/* pt_regs->dx */
336	pushl	%ecx		/* pt_regs->cx */
337	pushl	%ebx		/* pt_regs->bx */
338
339	/* Fix up DS and ES */
340	movl	$(__KERNEL_DS), %ecx
341	movl	%ecx, %ds
342	movl	%ecx, %es
343
344	/* Load the vector number into EDX */
345	movl	PT_GS(%esp), %edx
346
347	/* Load GS into pt_regs->gs (and maybe clobber __gsh) */
348	movw	%gs, PT_GS(%esp)
349
350	movl	%esp, %eax	/* args are pt_regs (EAX), trapnr (EDX) */
351	call	early_fixup_exception
352
353	popl	%ebx		/* pt_regs->bx */
354	popl	%ecx		/* pt_regs->cx */
355	popl	%edx		/* pt_regs->dx */
356	popl	%esi		/* pt_regs->si */
357	popl	%edi		/* pt_regs->di */
358	popl	%ebp		/* pt_regs->bp */
359	popl	%eax		/* pt_regs->ax */
360	popl	%ds		/* pt_regs->ds (always ignores __dsh) */
361	popl	%es		/* pt_regs->es (always ignores __esh) */
362	popl	%fs		/* pt_regs->fs (always ignores __fsh) */
363	popl	%gs		/* pt_regs->gs (always ignores __gsh) */
364	decl	%ss:early_recursion_flag
365	addl	$4, %esp	/* pop pt_regs->orig_ax */
366	iret
367SYM_CODE_END(early_idt_handler_common)
368
369/* This is the default interrupt "handler" :-) */
370SYM_FUNC_START(early_ignore_irq)
371	cld
372#ifdef CONFIG_PRINTK
373	pushl %eax
374	pushl %ecx
375	pushl %edx
376	pushl %es
377	pushl %ds
378	movl $(__KERNEL_DS),%eax
379	movl %eax,%ds
380	movl %eax,%es
381	cmpl $2,early_recursion_flag
382	je hlt_loop
383	incl early_recursion_flag
384	pushl 16(%esp)
385	pushl 24(%esp)
386	pushl 32(%esp)
387	pushl 40(%esp)
388	pushl $int_msg
389	call _printk
390
391	call dump_stack
392
393	addl $(5*4),%esp
394	popl %ds
395	popl %es
396	popl %edx
397	popl %ecx
398	popl %eax
399#endif
400	iret
401
402hlt_loop:
403	hlt
404	jmp hlt_loop
405SYM_FUNC_END(early_ignore_irq)
406
407__INITDATA
408	.align 4
409SYM_DATA(early_recursion_flag, .long 0)
410
411__REFDATA
412	.align 4
413SYM_DATA(initial_code,		.long i386_start_kernel)
414
415#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
416#define	PGD_ALIGN	(2 * PAGE_SIZE)
417#define PTI_USER_PGD_FILL	1024
418#else
419#define	PGD_ALIGN	(PAGE_SIZE)
420#define PTI_USER_PGD_FILL	0
421#endif
422/*
423 * BSS section
424 */
425__PAGE_ALIGNED_BSS
426	.align PGD_ALIGN
427#ifdef CONFIG_X86_PAE
428.globl initial_pg_pmd
429initial_pg_pmd:
430	.fill 1024*KPMDS,4,0
431#else
432.globl initial_page_table
433initial_page_table:
434	.fill 1024,4,0
435#endif
436	.align PGD_ALIGN
437initial_pg_fixmap:
438	.fill 1024,4,0
439.globl swapper_pg_dir
440	.align PGD_ALIGN
441swapper_pg_dir:
442	.fill 1024,4,0
443	.fill PTI_USER_PGD_FILL,4,0
444.globl empty_zero_page
445empty_zero_page:
446	.fill 4096,1,0
447EXPORT_SYMBOL(empty_zero_page)
448
449/*
450 * This starts the data section.
451 */
452#ifdef CONFIG_X86_PAE
453__PAGE_ALIGNED_DATA
454	/* Page-aligned for the benefit of paravirt? */
455	.align PGD_ALIGN
456SYM_DATA_START(initial_page_table)
457	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR),0	/* low identity map */
458# if KPMDS == 3
459	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR),0
460	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
461	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0
462# elif KPMDS == 2
463	.long	0,0
464	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR),0
465	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
466# elif KPMDS == 1
467	.long	0,0
468	.long	0,0
469	.long	pa(initial_pg_pmd+PGD_IDENT_ATTR),0
470# else
471#  error "Kernel PMDs should be 1, 2 or 3"
472# endif
473	.align PAGE_SIZE		/* needs to be page-sized too */
474
475#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
476	/*
477	 * PTI needs another page so sync_initial_pagetable() works correctly
478	 * and does not scribble over the data which is placed behind the
479	 * actual initial_page_table. See clone_pgd_range().
480	 */
481	.fill 1024, 4, 0
482#endif
483
484SYM_DATA_END(initial_page_table)
485#endif
486
487.data
488.balign 4
489SYM_DATA(initial_stack, .long __top_init_kernel_stack)
490
491__INITRODATA
492int_msg:
493	.asciz "Unknown interrupt or fault at: %p %p %p\n"
494
495#include "../xen/xen-head.S"
496
497/*
498 * The IDT and GDT 'descriptors' are a strange 48-bit object
499 * only used by the lidt and lgdt instructions. They are not
500 * like usual segment descriptors - they consist of a 16-bit
501 * segment size, and 32-bit linear address value:
502 */
503
504	.data
505	ALIGN
506# early boot GDT descriptor (must use 1:1 address mapping)
507	.word 0				# 32 bit align gdt_desc.address
508SYM_DATA_START_LOCAL(boot_gdt_descr)
509	.word __BOOT_DS+7
510	.long boot_gdt - __PAGE_OFFSET
511SYM_DATA_END(boot_gdt_descr)
512
513# boot GDT descriptor (later on used by CPU#0):
514	.word 0				# 32 bit align gdt_desc.address
515SYM_DATA_START(early_gdt_descr)
516	.word GDT_ENTRIES*8-1
517	.long gdt_page			/* Overwritten for secondary CPUs */
518SYM_DATA_END(early_gdt_descr)
519
520/*
521 * The boot_gdt must mirror the equivalent in setup.S and is
522 * used only for booting.
523 */
524	.align L1_CACHE_BYTES
525SYM_DATA_START(boot_gdt)
526	.fill GDT_ENTRY_BOOT_CS,8,0
527	.quad 0x00cf9a000000ffff	/* kernel 4GB code at 0x00000000 */
528	.quad 0x00cf92000000ffff	/* kernel 4GB data at 0x00000000 */
529SYM_DATA_END(boot_gdt)
530