2020-05-24 15:30:55 -05:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
//
|
|
|
|
// startup32.S contains the 32-bit startup code for both the BSP and APs.
|
|
|
|
// It initialises stacks, memory management, and exception handling, clears
|
|
|
|
// the BSS, completes relocation, and finally calls the main application.
|
2020-07-08 05:07:32 -05:00
|
|
|
// It supports the 32-bit Linux boot protocol and EFI boot for the first
|
|
|
|
// boot of the BSP.
|
2020-05-24 15:30:55 -05:00
|
|
|
//
|
2022-01-09 15:21:29 -06:00
|
|
|
// Copyright (C) 2020-2022 Martin Whitaker.
|
2020-05-24 15:30:55 -05:00
|
|
|
//
|
|
|
|
// Derived from memtest86+ head.S:
|
|
|
|
//
|
|
|
|
// linux/boot/head.S
|
|
|
|
// Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
// 1-Jan-96 Modified by Chris Brady for use as a boot/loader for MemTest-86.
|
|
|
|
// Set up the memory management for flat non-paged linear addressing.
|
|
|
|
// 17 May 2004 : Added X86_PWRCAP for AMD64 (Memtest86+ - Samuel D.)
|
|
|
|
|
|
|
|
#define __ASSEMBLY__
|
|
|
|
|
|
|
|
#include "boot.h"
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
#define INT64_CS 0x08
|
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
#define NUM_INT_VEC 20
|
|
|
|
|
|
|
|
.text
|
|
|
|
.code32
|
|
|
|
|
|
|
|
# The Linux 32-bit boot entry point.
|
|
|
|
|
|
|
|
.globl startup32
|
|
|
|
startup32:
|
|
|
|
cld
|
|
|
|
cli
|
2022-02-08 17:35:00 -06:00
|
|
|
|
|
|
|
# Jump to the shared 32-bit entry point with the boot params pointer
|
|
|
|
# in %esi and the startup address in %edi.
|
|
|
|
|
|
|
|
movl 0x214(%esi), %ebx # bootparams.code32_start
|
|
|
|
leal (startup - startup32)(%ebx), %edi
|
|
|
|
jmp startup
|
2020-07-08 05:07:32 -05:00
|
|
|
|
|
|
|
# The Linux 32-bit EFI handover point.
|
|
|
|
|
|
|
|
.org 0x10
|
|
|
|
.globl efi_handover
|
|
|
|
efi_handover:
|
|
|
|
popl %eax # the return address (discard)
|
|
|
|
popl %ecx # the EFI image handle
|
|
|
|
popl %edx # the EFI system table pointer
|
|
|
|
popl %esi # the boot params pointer
|
|
|
|
|
|
|
|
# Load the GOT pointer.
|
|
|
|
|
|
|
|
call 0f
|
|
|
|
0: popl %ebx
|
|
|
|
addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx
|
|
|
|
|
|
|
|
# Fill out the boot params structure.
|
|
|
|
|
|
|
|
subl $12, %esp # align the stack
|
|
|
|
andl $~0xf, %esp
|
|
|
|
addl $12, %esp
|
|
|
|
|
|
|
|
pushl %esi # the boot params pointer
|
|
|
|
pushl %edx # the EFI system table pointer
|
|
|
|
pushl %ecx # the EFI image handle
|
|
|
|
|
|
|
|
call efi_setup
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# Fall through to the shared 32-bit entry point with the boot params
|
|
|
|
# pointer in %esi and the startup address in %edi.
|
2020-07-08 05:07:32 -05:00
|
|
|
|
|
|
|
movl %eax, %esi
|
2022-01-31 15:54:24 -06:00
|
|
|
movl 0x214(%esi), %ebx # bootparams.code32_start
|
2022-04-23 07:17:59 -05:00
|
|
|
leal (startup - startup32)(%ebx), %edi
|
2022-01-31 15:54:24 -06:00
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
# The 32-bit entry point for AP boot and for restart after relocation.
|
|
|
|
|
|
|
|
.globl startup
|
|
|
|
startup:
|
2022-02-05 10:56:06 -06:00
|
|
|
# Some of the startup actions are not thread safe. Use a mutex
|
|
|
|
# to protect this section of code.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-05 10:56:06 -06:00
|
|
|
leal (startup_mutex - startup)(%edi), %eax
|
2022-02-02 06:52:44 -06:00
|
|
|
0: lock btsl $0, (%eax)
|
2022-01-31 15:54:24 -06:00
|
|
|
jc 0b
|
|
|
|
|
2022-02-05 10:56:06 -06:00
|
|
|
# Use the startup stack until we pick the correct one.
|
|
|
|
|
2022-01-31 15:54:24 -06:00
|
|
|
leal (startup_stack_top - startup)(%edi), %esp
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
# Load the GOT pointer.
|
|
|
|
|
|
|
|
call 0f
|
|
|
|
0: popl %ebx
|
|
|
|
addl $_GLOBAL_OFFSET_TABLE_+[.-0b], %ebx
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# If first boot, save the boot params pointer...
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
cmpl $1, first_boot@GOTOFF(%ebx)
|
2022-02-12 03:05:34 -06:00
|
|
|
jne 1f
|
2020-05-24 15:30:55 -05:00
|
|
|
movl %esi, boot_params_addr@GOTOFF(%ebx)
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# ...and check if the processor supports long mode.
|
|
|
|
|
|
|
|
movl $0x80000000, %eax # check if function 0x80000001 is available
|
2022-04-13 14:37:47 -05:00
|
|
|
pushl %ebx # ebx is overwritten by cpuid
|
2022-02-08 17:35:00 -06:00
|
|
|
cpuid
|
2022-04-13 14:37:47 -05:00
|
|
|
popl %ebx # restore ebx
|
2022-02-08 17:35:00 -06:00
|
|
|
cmpl $0x80000001, %eax
|
|
|
|
jb 1f
|
|
|
|
movl $0x80000001, %eax # test the LM flag
|
2022-04-13 14:37:47 -05:00
|
|
|
pushl %ebx # ebx is overwritten by cpuid
|
2022-02-08 17:35:00 -06:00
|
|
|
cpuid
|
|
|
|
popl %ebx # restore ebx
|
2022-04-13 14:37:47 -05:00
|
|
|
andl $0x20000000, %edx
|
2022-02-08 17:35:00 -06:00
|
|
|
jz 1f
|
|
|
|
movl $1, use_long_mode@GOTOFF(%ebx)
|
|
|
|
1:
|
2022-04-13 14:37:47 -05:00
|
|
|
|
2022-02-02 06:20:39 -06:00
|
|
|
# Pick the correct stack.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-01-31 16:59:14 -06:00
|
|
|
call smp_my_cpu_num
|
2022-01-29 08:16:51 -06:00
|
|
|
movl $AP_STACK_SIZE, %edx
|
2020-05-24 15:30:55 -05:00
|
|
|
mul %edx
|
2022-02-28 14:21:50 -06:00
|
|
|
addl $(BSP_STACK_SIZE - LOCALS_SIZE), %eax
|
2022-02-02 06:20:39 -06:00
|
|
|
leal _stacks@GOTOFF(%ebx), %esp
|
2022-01-29 08:16:51 -06:00
|
|
|
addl %eax, %esp
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
# Initialise the GDT descriptor.
|
|
|
|
|
|
|
|
leal gdt@GOTOFF(%ebx), %eax
|
|
|
|
movl %eax, 2 + gdt_descr@GOTOFF(%ebx)
|
|
|
|
|
|
|
|
# Load the GDT and the segment registers.
|
|
|
|
|
|
|
|
lgdt gdt_descr@GOTOFF(%ebx)
|
|
|
|
leal flush@GOTOFF(%ebx), %eax
|
|
|
|
movw $KERNEL_CS, -2(%esp)
|
|
|
|
movl %eax, -6(%esp)
|
|
|
|
ljmp *-6(%esp)
|
|
|
|
flush: movw $KERNEL_DS, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %es
|
|
|
|
movw %ax, %fs
|
|
|
|
movw %ax, %gs
|
|
|
|
movw %ax, %ss
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# Initialise the IDT. If we are going to operate in long mode, we need
|
|
|
|
# a 64-bit IDT, otherwise we need a 32-bit IDT.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
leal idt@GOTOFF(%ebx), %edi
|
2022-02-08 17:35:00 -06:00
|
|
|
cmpl $1, use_long_mode@GOTOFF(%ebx)
|
2022-02-12 03:05:34 -06:00
|
|
|
je init_idt64
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp init_idt32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
# Initialise the IDT descriptor.
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
init_idt_descr:
|
|
|
|
movw %ax, idt_descr@GOTOFF(%ebx)
|
2020-05-24 15:30:55 -05:00
|
|
|
leal idt@GOTOFF(%ebx), %eax
|
|
|
|
movl %eax, 2 + idt_descr@GOTOFF(%ebx)
|
|
|
|
|
|
|
|
# Load the IDT.
|
|
|
|
|
|
|
|
lidt idt_descr@GOTOFF(%ebx)
|
|
|
|
|
|
|
|
# Zero the BSS (if first boot).
|
|
|
|
|
|
|
|
cmpl $1, first_boot@GOTOFF(%ebx)
|
2022-02-12 03:05:34 -06:00
|
|
|
jne 1f
|
2020-05-24 15:30:55 -05:00
|
|
|
xorl %eax, %eax
|
|
|
|
leal _bss@GOTOFF(%ebx), %edi
|
|
|
|
leal _end@GOTOFF(%ebx), %ecx
|
|
|
|
subl %edi, %ecx
|
|
|
|
0: movl %eax, (%edi)
|
|
|
|
addl $4, %edi
|
|
|
|
subl $4, %ecx
|
|
|
|
jnz 0b
|
|
|
|
movl $0, first_boot@GOTOFF(%ebx)
|
|
|
|
1:
|
|
|
|
# Initialise the FPU.
|
|
|
|
|
|
|
|
finit
|
|
|
|
|
|
|
|
# Call the dynamic linker to fix up the addresses in the GOT.
|
|
|
|
|
|
|
|
call reloc
|
|
|
|
|
2020-07-08 05:07:32 -05:00
|
|
|
# Disable paging (needed during restart). Also disable write protect
|
|
|
|
# (in case set by EFI boot).
|
2020-05-30 04:52:35 -05:00
|
|
|
|
|
|
|
movl %cr0, %eax
|
2020-07-08 05:07:32 -05:00
|
|
|
andl $0x7ffeffff, %eax
|
2020-05-30 04:52:35 -05:00
|
|
|
movl %eax, %cr0
|
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
# Enable PAE if supported.
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
pushl %ebx # ebx is overwritten by cpuid
|
2020-05-24 15:30:55 -05:00
|
|
|
movl $0x00000001, %eax # test the PAE flag
|
|
|
|
cpuid
|
|
|
|
andl $0x00000040, %edx
|
2022-02-08 17:35:00 -06:00
|
|
|
popl %ebx # restore ebx
|
2020-05-24 15:30:55 -05:00
|
|
|
jz 1f # bail if not supported
|
|
|
|
|
|
|
|
movl %cr4, %eax # enable PAE
|
|
|
|
orl $0x00000020, %eax
|
|
|
|
movl %eax, %cr4
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
leal pdp@GOTOFF(%ebx), %eax # set the page directory base address
|
2020-05-24 15:30:55 -05:00
|
|
|
movl %eax, %cr3
|
|
|
|
|
|
|
|
# Enable long mode if supported.
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
cmpl $1, use_long_mode@GOTOFF(%ebx)
|
2022-02-12 03:05:34 -06:00
|
|
|
jne 0f
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2020-05-30 04:52:35 -05:00
|
|
|
movl $0xc0000080, %ecx # enable long mode
|
2020-05-24 15:30:55 -05:00
|
|
|
rdmsr
|
|
|
|
orl $0x00000100, %eax
|
|
|
|
wrmsr
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
leal pml4@GOTOFF(%ebx), %eax # set the page directory base address
|
2020-05-24 15:30:55 -05:00
|
|
|
movl %eax, %cr3
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# Enable paging and protection.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
0: movl %cr0, %eax
|
2022-02-08 17:35:00 -06:00
|
|
|
orl $0x80000001, %eax
|
2020-05-24 15:30:55 -05:00
|
|
|
movl %eax, %cr0
|
2022-02-08 17:35:00 -06:00
|
|
|
1:
|
2022-02-05 10:56:06 -06:00
|
|
|
# Release the startup mutex.
|
|
|
|
|
|
|
|
movl $0, startup_mutex@GOTOFF(%ebx)
|
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
# Run the application.
|
|
|
|
|
|
|
|
call main
|
|
|
|
|
|
|
|
# In case we return, simulate an exception.
|
|
|
|
|
|
|
|
pushfl
|
|
|
|
pushl %cs
|
|
|
|
call 0f
|
|
|
|
0: pushl $0 # error code
|
|
|
|
pushl $257 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2020-07-08 05:07:32 -05:00
|
|
|
# The EFI PE32 boot entry point.
|
|
|
|
|
|
|
|
.org 0x1e0
|
|
|
|
.globl efi_boot
|
|
|
|
efi_boot:
|
|
|
|
popl %eax # the return address (discard)
|
|
|
|
popl %ecx # the EFI image handle
|
|
|
|
popl %edx # the EFI system table pointer
|
|
|
|
|
|
|
|
pushl $0 # the boot params pointer (0 = not yet allocated)
|
|
|
|
pushl %edx # the EFI system table pointer
|
|
|
|
pushl %ecx # the EFI image handle
|
|
|
|
|
|
|
|
call efi_handover # never returns
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# Initialise the 64-bit IDT.
|
|
|
|
|
|
|
|
init_idt64:
|
|
|
|
leal vec64_0@GOTOFF(%ebx), %esi
|
|
|
|
movw $NUM_INT_VEC, %cx
|
|
|
|
0: movl %esi, %edx
|
|
|
|
movl $(INT64_CS << 16), %eax
|
|
|
|
movw %dx, %ax # selector = 0x0008 = long mode cs
|
|
|
|
movw $0x8E00, %dx # interrupt gate - dpl=0, present
|
|
|
|
movl %eax, (%edi)
|
|
|
|
movl %edx, 4(%edi)
|
|
|
|
addl $(vec64_1-vec64_0), %esi
|
|
|
|
addl $16, %edi
|
|
|
|
dec %cx
|
|
|
|
jnz 0b
|
|
|
|
|
|
|
|
movw $(NUM_INT_VEC*16 - 1), %ax
|
|
|
|
jmp init_idt_descr
|
|
|
|
|
|
|
|
# Initialise the 32-bit IDT.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
init_idt32:
|
|
|
|
leal vec32_0@GOTOFF(%ebx), %esi
|
|
|
|
movw $NUM_INT_VEC, %cx
|
|
|
|
0: movl %esi, %edx
|
|
|
|
movl $(KERNEL_CS << 16), %eax
|
|
|
|
movw %dx, %ax # selector = 0x0010 = cs
|
|
|
|
movw $0x8E00, %dx # interrupt gate - dpl=0, present
|
|
|
|
movl %eax, (%edi)
|
|
|
|
movl %edx, 4(%edi)
|
|
|
|
addl $(vec32_1-vec32_0), %esi
|
|
|
|
addl $8, %edi
|
|
|
|
dec %cx
|
|
|
|
jnz 0b
|
|
|
|
|
|
|
|
movw $(NUM_INT_VEC*8 - 1), %ax
|
|
|
|
jmp init_idt_descr
|
|
|
|
|
|
|
|
# Individual interrupt vector handlers for long mode. These need to be
|
|
|
|
# spaced equally, to allow the IDT initialisation loop above to work,
|
|
|
|
# so we use noops to pad out where required.
|
|
|
|
|
|
|
|
.code64
|
|
|
|
|
|
|
|
vec64_0:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $0 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_1:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $1 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_2:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $2 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_3:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $3 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_4:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $4 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_5:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $5 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_6:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $6 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_7:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $7 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_8:
|
|
|
|
nop;nop # error code already provided
|
|
|
|
pushq $8 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_9:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $9 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_10:
|
|
|
|
nop;nop # error code already provided
|
|
|
|
pushq $10 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_11:
|
|
|
|
nop;nop # error code already provided
|
|
|
|
pushq $11 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_12:
|
|
|
|
nop;nop # error code already provided
|
|
|
|
pushq $12 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_13:
|
|
|
|
nop;nop # error code already provided
|
|
|
|
pushq $13 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_14:
|
|
|
|
nop;nop # error code already provided
|
|
|
|
pushq $14 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_15:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $15 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_16:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $16 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_17:
|
|
|
|
nop;nop # error code
|
|
|
|
pushq $17 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_18:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $18 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
|
|
|
vec64_19:
|
|
|
|
pushq $0 # error code
|
|
|
|
pushq $19 # vector
|
|
|
|
jmp int_handler64
|
|
|
|
|
2022-02-11 17:39:46 -06:00
|
|
|
# The interrupt handler code for long mode. Pass the register state to the
|
|
|
|
# common interrupt handler. On entry this expects the stack to contain:
|
|
|
|
#
|
|
|
|
# rsp+30 ss
|
|
|
|
# rsp+28 rsp
|
|
|
|
# rsp+20 rflags
|
|
|
|
# rsp+18 cs
|
|
|
|
# rsp+10 rip
|
|
|
|
# rsp+08 error code
|
|
|
|
# rsp+00 vector number
|
|
|
|
#
|
|
|
|
# We create a new stack frame in the format expected by int_handler. We can
|
|
|
|
# reuse the space currently occupied by the vector number and the error code,
|
|
|
|
# as they are not needed on return.
|
2022-02-08 17:35:00 -06:00
|
|
|
|
|
|
|
int_handler64:
|
2022-02-11 17:39:46 -06:00
|
|
|
subq $16, %rsp
|
|
|
|
movl %ebp, 0x04(%rsp) # save the state of ebp
|
|
|
|
leal 0x48(%rsp), %ebp # save the state of esp before the interrupt
|
|
|
|
movl %ebp, 0x00(%rsp)
|
|
|
|
movl 0x10(%rsp), %ebp # save the vector number
|
|
|
|
movl %ebp, 0x08(%rsp)
|
|
|
|
movl 0x18(%rsp), %ebp # save the error code
|
|
|
|
movl %ebp, 0x0c(%rsp)
|
|
|
|
movl 0x20(%rsp), %ebp # save the state of eip
|
|
|
|
movl %ebp, 0x10(%rsp)
|
|
|
|
movl 0x28(%rsp), %ebp # save the state of cs
|
|
|
|
movl %ebp, 0x14(%rsp)
|
|
|
|
movl 0x30(%rsp), %ebp # save the state of eflags
|
|
|
|
movl %ebp, 0x18(%rsp)
|
|
|
|
leal int_handler(%rip), %ebp
|
|
|
|
movw $KERNEL_CS, -2(%rsp)
|
|
|
|
movl %ebp, -6(%rsp)
|
|
|
|
lcall *-6(%rsp)
|
|
|
|
movl 0x04(%rsp), %ebp # restore the saved state of ebp
|
|
|
|
addq $32, %rsp # discard the stack frame we created
|
2022-02-08 17:35:00 -06:00
|
|
|
iretq
|
|
|
|
|
|
|
|
# Individual interrupt vector handlers for protected mode. These need to be
|
|
|
|
# spaced equally, to allow the IDT initialisation loop above to work, so we
|
|
|
|
# use noops to pad out where required.
|
|
|
|
|
|
|
|
.code32
|
|
|
|
|
|
|
|
vec32_0:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $0 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
|
|
|
|
|
|
|
vec32_1:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $1 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_2:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $2 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_3:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $3 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_4:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $4 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_5:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $5 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_6:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $6 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_7:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $7 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_8:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code already provided
|
|
|
|
pushl $8 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_9:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $9 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_10:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code already provided
|
|
|
|
pushl $10 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_11:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code already provided
|
|
|
|
pushl $11 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_12:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code already provided
|
|
|
|
pushl $12 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_13:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code already provided
|
|
|
|
pushl $13 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_14:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code already provided
|
|
|
|
pushl $14 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_15:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $15 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_16:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $16 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_17:
|
2020-05-24 15:30:55 -05:00
|
|
|
nop;nop # error code
|
|
|
|
pushl $17 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_18:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $18 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
vec32_19:
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl $0 # error code
|
|
|
|
pushl $19 # vector
|
2022-02-08 17:35:00 -06:00
|
|
|
jmp int_handler32
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-11 17:39:46 -06:00
|
|
|
# The interrupt handler code for protected mode. Pass the register state to
|
|
|
|
# the common interrupt handler. On entry this expects the stack to contain:
|
|
|
|
#
|
|
|
|
# esp+10 eflags
|
|
|
|
# esp+0c cs
|
|
|
|
# esp+08 eip
|
|
|
|
# esp+04 error code
|
|
|
|
# esp+00 vector number
|
|
|
|
#
|
|
|
|
# It adds the additional state expected by int_handler to the bottom of the
|
|
|
|
# stack frame.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
int_handler32:
|
2022-02-11 17:39:46 -06:00
|
|
|
pushl %ebp # save the state of ebp
|
|
|
|
leal 24(%esp), %ebp # save the state of esp before the interrupt
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl %ebp
|
2022-02-11 17:39:46 -06:00
|
|
|
leal int_handler@GOTOFF(%ebx), %ebp
|
|
|
|
movw $KERNEL_CS, -2(%esp)
|
|
|
|
movl %ebp, -6(%esp)
|
|
|
|
lcall *-6(%esp)
|
|
|
|
popl %ebp # discard the saved state of esp
|
|
|
|
popl %ebp # restore the saved state of ebp
|
|
|
|
addl $8, %esp # discard the vector number and error code
|
|
|
|
iret
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-11 17:39:46 -06:00
|
|
|
# The common interrupt handler code. Pass the register state to the application
|
|
|
|
# interrupt handler. On entry this expects the stack to contain:
|
|
|
|
#
|
|
|
|
# esp+18 eflags
|
|
|
|
# esp+14 cs
|
|
|
|
# esp+10 eip
|
|
|
|
# esp+0c error code
|
|
|
|
# esp+08 vector number
|
|
|
|
# esp+04 ebp
|
|
|
|
# esp+00 esp
|
|
|
|
#
|
|
|
|
# It adds the additional state expected by the application to the bottom of the
|
|
|
|
# stack frame.
|
|
|
|
|
|
|
|
int_handler:
|
|
|
|
pushl %esi
|
|
|
|
pushl %edi
|
|
|
|
pushl %edx
|
|
|
|
pushl %ecx
|
|
|
|
pushl %ebx
|
2020-05-24 15:30:55 -05:00
|
|
|
pushl %eax
|
|
|
|
pushl %ss
|
2022-02-11 17:39:46 -06:00
|
|
|
pushl %es
|
|
|
|
pushl %ds
|
|
|
|
pushl %esp # pointer to trap regs struct on the stack
|
|
|
|
cld
|
2020-05-24 15:30:55 -05:00
|
|
|
call interrupt
|
2022-02-11 17:39:46 -06:00
|
|
|
addl $16, %esp
|
2020-05-24 15:30:55 -05:00
|
|
|
popl %eax
|
2022-02-11 17:39:46 -06:00
|
|
|
popl %ebx
|
|
|
|
popl %ecx
|
|
|
|
popl %edx
|
|
|
|
popl %edi
|
|
|
|
popl %esi
|
|
|
|
lret
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# The interrupt descriptor table, used for both long mode and protected mode.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
.align 4
|
|
|
|
.word 0 # for alignment
|
2022-02-08 17:35:00 -06:00
|
|
|
.globl idt_descr
|
2020-05-24 15:30:55 -05:00
|
|
|
idt_descr:
|
2022-02-08 17:35:00 -06:00
|
|
|
.word 0 # size: filled in at run time
|
|
|
|
.quad 0 # addr: filled in at run time
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
.align 8
|
|
|
|
.globl idt
|
2020-05-24 15:30:55 -05:00
|
|
|
idt:
|
2022-02-08 17:35:00 -06:00
|
|
|
.fill 2*NUM_INT_VEC, 8, 0 # filled in at run time
|
2020-05-24 15:30:55 -05:00
|
|
|
idt_end:
|
|
|
|
|
|
|
|
# The global descriptor table.
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
.align 4
|
2020-05-24 15:30:55 -05:00
|
|
|
.word 0 # for alignment
|
|
|
|
gdt_descr:
|
|
|
|
.word gdt_end - gdt - 1 # size
|
|
|
|
.long 0 # addr: filled in at run time
|
|
|
|
|
|
|
|
.align 4
|
|
|
|
.globl gdt
|
|
|
|
gdt:
|
|
|
|
.quad 0x0000000000000000 # NULL descriptor
|
2022-02-08 17:35:00 -06:00
|
|
|
.quad 0x00209a0000000000 # 0x08 64-bit code at 0x000000
|
|
|
|
.quad 0x00cf9a000000ffff # 0x10 main 4gb code at 0x000000
|
|
|
|
.quad 0x00cf92000000ffff # 0x18 main 4gb data at 0x000000
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
.globl gdt_end
|
|
|
|
gdt_end:
|
|
|
|
|
|
|
|
.data
|
|
|
|
|
|
|
|
.macro ptes64 start, count=64
|
|
|
|
.quad \start + 0x0000000 + 0x83
|
|
|
|
.quad \start + 0x0200000 + 0x83
|
|
|
|
.quad \start + 0x0400000 + 0x83
|
|
|
|
.quad \start + 0x0600000 + 0x83
|
|
|
|
.quad \start + 0x0800000 + 0x83
|
|
|
|
.quad \start + 0x0A00000 + 0x83
|
|
|
|
.quad \start + 0x0C00000 + 0x83
|
|
|
|
.quad \start + 0x0E00000 + 0x83
|
|
|
|
.if \count-1
|
|
|
|
ptes64 "(\start+0x01000000)",\count-1
|
|
|
|
.endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
.macro maxdepth depth=1
|
|
|
|
.if \depth-1
|
|
|
|
maxdepth \depth-1
|
|
|
|
.endif
|
|
|
|
.endm
|
|
|
|
|
|
|
|
maxdepth
|
|
|
|
|
|
|
|
# The long mode level 4 page map table.
|
|
|
|
|
|
|
|
.align 4096
|
|
|
|
.globl pml4
|
|
|
|
pml4:
|
|
|
|
.long pdp + 0x3 # relocated at run time
|
|
|
|
.long 0
|
|
|
|
|
|
|
|
# Page Directory Pointer Table:
|
|
|
|
# 4 Entries, pointing to the Page Directory Tables.
|
|
|
|
|
|
|
|
.align 4096
|
|
|
|
.globl pdp
|
|
|
|
pdp:
|
|
|
|
.long pd0 + 0x1 # relocated at run time
|
|
|
|
.long 0
|
|
|
|
.long pd1 + 0x1 # relocated at run time
|
|
|
|
.long 0
|
|
|
|
.long pd2 + 0x1 # relocated at run time
|
|
|
|
.long 0
|
|
|
|
.long pd3 + 0x1 # relocated at run time
|
|
|
|
.long 0
|
|
|
|
|
|
|
|
# Page Directory Tables:
|
|
|
|
# There are 4 tables. The first two map the first 2 GB of memory. The third
|
|
|
|
# is used with PAE to map the rest of memory in 1 GB segments. The fourth is
|
|
|
|
# reserved for mapping the video frame buffer. We use 2 MB pages so only the
|
|
|
|
# Page Directory Table is used (no page tables).
|
|
|
|
|
|
|
|
.align 4096
|
|
|
|
.globl pd0
|
|
|
|
pd0:
|
|
|
|
ptes64 0x0000000000000000
|
|
|
|
|
|
|
|
.align 4096
|
|
|
|
.globl pd1
|
|
|
|
pd1:
|
|
|
|
ptes64 0x0000000040000000
|
|
|
|
|
|
|
|
.align 4096
|
|
|
|
.globl pd2
|
|
|
|
pd2:
|
|
|
|
ptes64 0x0000000080000000
|
|
|
|
|
|
|
|
.align 4096
|
|
|
|
.globl pd3
|
|
|
|
pd3:
|
|
|
|
ptes64 0x00000000C0000000
|
|
|
|
|
|
|
|
.previous
|
|
|
|
|
|
|
|
# ap_trampoline is the entry point for CPUs other than the bootstrap
|
|
|
|
# CPU (BSP). It gets copied to a page in low memory, to enable the APs
|
|
|
|
# to boot when the main program has been loaded in high memory.
|
|
|
|
|
|
|
|
.code16
|
|
|
|
.align 4
|
|
|
|
|
|
|
|
.globl ap_trampoline
|
|
|
|
ap_trampoline:
|
|
|
|
movw %cs, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
|
2022-01-31 15:54:24 -06:00
|
|
|
# Load the startup address and use it to patch the jump address.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
2022-01-31 15:54:24 -06:00
|
|
|
movl (ap_startup_addr - ap_trampoline), %edi
|
|
|
|
movl %edi, (ap_jump - ap_trampoline + 2)
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
# Patch and load the GDT descriptor. It should point to the main
|
|
|
|
# GDT descriptor, which has already been initialised by the BSP.
|
|
|
|
|
2022-01-31 15:54:24 -06:00
|
|
|
movl %edi, %eax
|
2020-05-24 15:30:55 -05:00
|
|
|
addl $(gdt - startup), %eax
|
|
|
|
movl %eax, (ap_gdt_descr - ap_trampoline + 2)
|
2022-05-15 04:10:36 -05:00
|
|
|
data32 lgdt ap_gdt_descr - ap_trampoline
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
# Switch to protected mode and reload the segment registers.
|
|
|
|
|
|
|
|
movl %cr0, %eax
|
|
|
|
orl $1, %eax
|
|
|
|
movl %eax, %cr0
|
|
|
|
jmp ap_flush
|
|
|
|
ap_flush:
|
|
|
|
movw $KERNEL_DS, %ax
|
|
|
|
movw %ax, %ds
|
|
|
|
movw %ax, %es
|
|
|
|
movw %ax, %fs
|
|
|
|
movw %ax, %gs
|
|
|
|
movw %ax, %ss
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
# Jump to the main entry point with the startup address in %edi.
|
2020-05-24 15:30:55 -05:00
|
|
|
ap_jump:
|
|
|
|
data32 ljmp $KERNEL_CS, $0
|
|
|
|
|
|
|
|
.align 4
|
|
|
|
.word 0 # for alignment
|
|
|
|
ap_gdt_descr:
|
|
|
|
.word gdt_end - gdt - 1 # gdt limit
|
|
|
|
.long 0 # gdt base - filled in at run time
|
|
|
|
|
|
|
|
.globl ap_startup_addr
|
|
|
|
ap_startup_addr:
|
|
|
|
.long 0 # filled in at run time
|
|
|
|
|
|
|
|
.globl ap_trampoline_end
|
|
|
|
ap_trampoline_end:
|
|
|
|
|
|
|
|
.previous
|
|
|
|
|
|
|
|
# Variables.
|
|
|
|
|
|
|
|
.data
|
2022-01-31 15:54:24 -06:00
|
|
|
.align 4
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
.globl boot_params_addr
|
|
|
|
boot_params_addr:
|
|
|
|
.long 0
|
|
|
|
|
2022-02-05 10:56:06 -06:00
|
|
|
startup_mutex:
|
2022-01-31 15:54:24 -06:00
|
|
|
.long 0
|
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
first_boot:
|
|
|
|
.long 1
|
|
|
|
|
2022-02-08 17:35:00 -06:00
|
|
|
use_long_mode:
|
|
|
|
.long 0
|
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
.previous
|
|
|
|
|
2022-01-29 08:16:51 -06:00
|
|
|
# Startup stack.
|
2020-05-24 15:30:55 -05:00
|
|
|
|
|
|
|
.bss
|
|
|
|
.align 16
|
|
|
|
|
|
|
|
startup_stack_base:
|
|
|
|
. = . + 64
|
|
|
|
startup_stack_top:
|
|
|
|
|
|
|
|
.previous
|
2022-02-02 06:20:39 -06:00
|
|
|
|
|
|
|
# Main stack area.
|
|
|
|
|
|
|
|
.section "stacks", "aw", @progbits
|
2022-02-02 12:23:23 -06:00
|
|
|
.align 16
|
2022-02-02 06:20:39 -06:00
|
|
|
|
|
|
|
. = . + STACKS_SIZE
|
|
|
|
|
|
|
|
.previous
|