Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/x86/kernel/head_32.S
26451 views
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
*/
52
KERNEL_PAGES = LOWMEM_PAGES
53
54
INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE
55
RESERVE_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
__HEAD
65
SYM_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
108
1:
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
129
SYM_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
SYM_FUNC_START(startup_32_smp)
140
cld
141
movl $(__BOOT_DS),%eax
142
movl %eax,%ds
143
movl %eax,%es
144
movl %eax,%fs
145
movl %eax,%gs
146
movl pa(initial_stack),%ecx
147
movl %eax,%ss
148
leal -__PAGE_OFFSET(%ecx),%esp
149
150
.Ldefault_entry:
151
movl $(CR0_STATE & ~X86_CR0_PG),%eax
152
movl %eax,%cr0
153
154
/*
155
* We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave
156
* bits like NT set. This would confuse the debugger if this code is traced. So
157
* initialize them properly now before switching to protected mode. That means
158
* DF in particular (even though we have cleared it earlier after copying the
159
* command line) because GCC expects it.
160
*/
161
pushl $0
162
popfl
163
164
/*
165
* New page tables may be in 4Mbyte page mode and may be using the global pages.
166
*
167
* NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists
168
* if and only if CPUID exists and has flags other than the FPU flag set.
169
*/
170
movl $-1,pa(X86_CPUID) # preset CPUID level
171
movl $X86_EFLAGS_ID,%ecx
172
pushl %ecx
173
popfl # set EFLAGS=ID
174
pushfl
175
popl %eax # get EFLAGS
176
testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set?
177
jz .Lenable_paging # hw disallowed setting of ID bit
178
# which means no CPUID and no CR4
179
180
xorl %eax,%eax
181
cpuid
182
movl %eax,pa(X86_CPUID) # save largest std CPUID function
183
184
movl $1,%eax
185
cpuid
186
andl $~1,%edx # Ignore CPUID.FPU
187
jz .Lenable_paging # No flags or only CPUID.FPU = no CR4
188
189
movl pa(mmu_cr4_features),%eax
190
movl %eax,%cr4
191
192
testb $X86_CR4_PAE, %al # check if PAE is enabled
193
jz .Lenable_paging
194
195
/* Check if extended functions are implemented */
196
movl $0x80000000, %eax
197
cpuid
198
/* Value must be in the range 0x80000001 to 0x8000ffff */
199
subl $0x80000001, %eax
200
cmpl $(0x8000ffff-0x80000001), %eax
201
ja .Lenable_paging
202
203
/* Clear bogus XD_DISABLE bits */
204
call verify_cpu
205
206
mov $0x80000001, %eax
207
cpuid
208
/* Execute Disable bit supported? */
209
btl $(X86_FEATURE_NX & 31), %edx
210
jnc .Lenable_paging
211
212
/* Setup EFER (Extended Feature Enable Register) */
213
movl $MSR_EFER, %ecx
214
rdmsr
215
216
btsl $_EFER_NX, %eax
217
/* Make changes effective */
218
wrmsr
219
220
.Lenable_paging:
221
222
/*
223
* Enable paging
224
*/
225
movl $pa(initial_page_table), %eax
226
movl %eax,%cr3 /* set the page table pointer.. */
227
movl $CR0_STATE,%eax
228
movl %eax,%cr0 /* ..and set paging (PG) bit */
229
ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
230
1:
231
/* Shift the stack pointer to a virtual address */
232
addl $__PAGE_OFFSET, %esp
233
234
/*
235
* Check if it is 486
236
*/
237
movb $4,X86 # at least 486
238
cmpl $-1,X86_CPUID
239
je .Lis486
240
241
/* get vendor info */
242
xorl %eax,%eax # call CPUID with 0 -> return vendor ID
243
cpuid
244
movl %eax,X86_CPUID # save CPUID level
245
movl %ebx,X86_VENDOR_ID # lo 4 chars
246
movl %edx,X86_VENDOR_ID+4 # next 4 chars
247
movl %ecx,X86_VENDOR_ID+8 # last 4 chars
248
249
orl %eax,%eax # do we have processor info as well?
250
je .Lis486
251
252
movl $1,%eax # Use the CPUID instruction to get CPU type
253
cpuid
254
movb %al,%cl # save reg for future use
255
andb $0x0f,%ah # mask processor family
256
movb %ah,X86
257
andb $0xf0,%al # mask model
258
shrb $4,%al
259
movb %al,X86_MODEL
260
andb $0x0f,%cl # mask mask revision
261
movb %cl,X86_STEPPING
262
movl %edx,X86_CAPABILITY
263
264
.Lis486:
265
movl $0x50022,%ecx # set AM, WP, NE and MP
266
movl %cr0,%eax
267
andl $0x80000011,%eax # Save PG,PE,ET
268
orl %ecx,%eax
269
movl %eax,%cr0
270
271
lgdt early_gdt_descr
272
ljmp $(__KERNEL_CS),$1f
273
1: movl $(__KERNEL_DS),%eax # reload all the segment registers
274
movl %eax,%ss # after changing gdt.
275
276
movl $(__USER_DS),%eax # DS/ES contains default USER segment
277
movl %eax,%ds
278
movl %eax,%es
279
280
movl $(__KERNEL_PERCPU), %eax
281
movl %eax,%fs # set this cpu's percpu
282
283
xorl %eax,%eax
284
movl %eax,%gs # clear possible garbage in %gs
285
286
xorl %eax,%eax # Clear LDT
287
lldt %ax
288
289
call *(initial_code)
290
1: jmp 1b
291
SYM_FUNC_END(startup_32_smp)
292
293
#include "verify_cpu.S"
294
295
__INIT
296
SYM_FUNC_START(early_idt_handler_array)
297
# 36(%esp) %eflags
298
# 32(%esp) %cs
299
# 28(%esp) %eip
300
# 24(%rsp) error code
301
i = 0
302
.rept NUM_EXCEPTION_VECTORS
303
.if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
304
pushl $0 # Dummy error code, to make stack frame uniform
305
.endif
306
pushl $i # 20(%esp) Vector number
307
jmp early_idt_handler_common
308
i = i + 1
309
.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
310
.endr
311
SYM_FUNC_END(early_idt_handler_array)
312
313
SYM_CODE_START_LOCAL(early_idt_handler_common)
314
/*
315
* The stack is the hardware frame, an error code or zero, and the
316
* vector number.
317
*/
318
cld
319
320
incl %ss:early_recursion_flag
321
322
/* The vector number is in pt_regs->gs */
323
324
cld
325
pushl %fs /* pt_regs->fs (__fsh varies by model) */
326
pushl %es /* pt_regs->es (__esh varies by model) */
327
pushl %ds /* pt_regs->ds (__dsh varies by model) */
328
pushl %eax /* pt_regs->ax */
329
pushl %ebp /* pt_regs->bp */
330
pushl %edi /* pt_regs->di */
331
pushl %esi /* pt_regs->si */
332
pushl %edx /* pt_regs->dx */
333
pushl %ecx /* pt_regs->cx */
334
pushl %ebx /* pt_regs->bx */
335
336
/* Fix up DS and ES */
337
movl $(__KERNEL_DS), %ecx
338
movl %ecx, %ds
339
movl %ecx, %es
340
341
/* Load the vector number into EDX */
342
movl PT_GS(%esp), %edx
343
344
/* Load GS into pt_regs->gs (and maybe clobber __gsh) */
345
movw %gs, PT_GS(%esp)
346
347
movl %esp, %eax /* args are pt_regs (EAX), trapnr (EDX) */
348
call early_fixup_exception
349
350
popl %ebx /* pt_regs->bx */
351
popl %ecx /* pt_regs->cx */
352
popl %edx /* pt_regs->dx */
353
popl %esi /* pt_regs->si */
354
popl %edi /* pt_regs->di */
355
popl %ebp /* pt_regs->bp */
356
popl %eax /* pt_regs->ax */
357
popl %ds /* pt_regs->ds (always ignores __dsh) */
358
popl %es /* pt_regs->es (always ignores __esh) */
359
popl %fs /* pt_regs->fs (always ignores __fsh) */
360
popl %gs /* pt_regs->gs (always ignores __gsh) */
361
decl %ss:early_recursion_flag
362
addl $4, %esp /* pop pt_regs->orig_ax */
363
iret
364
SYM_CODE_END(early_idt_handler_common)
365
366
/* This is the default interrupt "handler" :-) */
367
SYM_FUNC_START(early_ignore_irq)
368
cld
369
#ifdef CONFIG_PRINTK
370
pushl %eax
371
pushl %ecx
372
pushl %edx
373
pushl %es
374
pushl %ds
375
movl $(__KERNEL_DS),%eax
376
movl %eax,%ds
377
movl %eax,%es
378
cmpl $2,early_recursion_flag
379
je hlt_loop
380
incl early_recursion_flag
381
pushl 16(%esp)
382
pushl 24(%esp)
383
pushl 32(%esp)
384
pushl 40(%esp)
385
pushl $int_msg
386
call _printk
387
388
call dump_stack
389
390
addl $(5*4),%esp
391
popl %ds
392
popl %es
393
popl %edx
394
popl %ecx
395
popl %eax
396
#endif
397
iret
398
399
hlt_loop:
400
hlt
401
jmp hlt_loop
402
SYM_FUNC_END(early_ignore_irq)
403
404
__INITDATA
405
.align 4
406
SYM_DATA(early_recursion_flag, .long 0)
407
408
__REFDATA
409
.align 4
410
SYM_DATA(initial_code, .long i386_start_kernel)
411
412
#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
413
#define PGD_ALIGN (2 * PAGE_SIZE)
414
#define PTI_USER_PGD_FILL 1024
415
#else
416
#define PGD_ALIGN (PAGE_SIZE)
417
#define PTI_USER_PGD_FILL 0
418
#endif
419
/*
420
* BSS section
421
*/
422
__PAGE_ALIGNED_BSS
423
.align PGD_ALIGN
424
#ifdef CONFIG_X86_PAE
425
.globl initial_pg_pmd
426
initial_pg_pmd:
427
.fill 1024*KPMDS,4,0
428
#else
429
.globl initial_page_table
430
initial_page_table:
431
.fill 1024,4,0
432
#endif
433
.align PGD_ALIGN
434
initial_pg_fixmap:
435
.fill 1024,4,0
436
.globl swapper_pg_dir
437
.align PGD_ALIGN
438
swapper_pg_dir:
439
.fill 1024,4,0
440
.fill PTI_USER_PGD_FILL,4,0
441
.globl empty_zero_page
442
empty_zero_page:
443
.fill 4096,1,0
444
EXPORT_SYMBOL(empty_zero_page)
445
446
/*
447
* This starts the data section.
448
*/
449
#ifdef CONFIG_X86_PAE
450
__PAGE_ALIGNED_DATA
451
/* Page-aligned for the benefit of paravirt? */
452
.align PGD_ALIGN
453
SYM_DATA_START(initial_page_table)
454
.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
455
# if KPMDS == 3
456
.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
457
.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
458
.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0
459
# elif KPMDS == 2
460
.long 0,0
461
.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
462
.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
463
# elif KPMDS == 1
464
.long 0,0
465
.long 0,0
466
.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
467
# else
468
# error "Kernel PMDs should be 1, 2 or 3"
469
# endif
470
.align PAGE_SIZE /* needs to be page-sized too */
471
472
#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
473
/*
474
* PTI needs another page so sync_initial_pagetable() works correctly
475
* and does not scribble over the data which is placed behind the
476
* actual initial_page_table. See clone_pgd_range().
477
*/
478
.fill 1024, 4, 0
479
#endif
480
481
SYM_DATA_END(initial_page_table)
482
#endif
483
484
.data
485
.balign 4
486
SYM_DATA(initial_stack, .long __top_init_kernel_stack)
487
488
__INITRODATA
489
int_msg:
490
.asciz "Unknown interrupt or fault at: %p %p %p\n"
491
492
#include "../xen/xen-head.S"
493
494
/*
495
* The IDT and GDT 'descriptors' are a strange 48-bit object
496
* only used by the lidt and lgdt instructions. They are not
497
* like usual segment descriptors - they consist of a 16-bit
498
* segment size, and 32-bit linear address value:
499
*/
500
501
.data
502
ALIGN
503
# early boot GDT descriptor (must use 1:1 address mapping)
504
.word 0 # 32 bit align gdt_desc.address
505
SYM_DATA_START_LOCAL(boot_gdt_descr)
506
.word __BOOT_DS+7
507
.long boot_gdt - __PAGE_OFFSET
508
SYM_DATA_END(boot_gdt_descr)
509
510
# boot GDT descriptor (later on used by CPU#0):
511
.word 0 # 32 bit align gdt_desc.address
512
SYM_DATA_START(early_gdt_descr)
513
.word GDT_ENTRIES*8-1
514
.long gdt_page /* Overwritten for secondary CPUs */
515
SYM_DATA_END(early_gdt_descr)
516
517
/*
518
* The boot_gdt must mirror the equivalent in setup.S and is
519
* used only for booting.
520
*/
521
.align L1_CACHE_BYTES
522
SYM_DATA_START(boot_gdt)
523
.fill GDT_ENTRY_BOOT_CS,8,0
524
.quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
525
.quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */
526
SYM_DATA_END(boot_gdt)
527
528