2012-05-09 02:22:28 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Trampoline.S Derived from Setup.S by Linus Torvalds
|
|
|
|
*
|
|
|
|
* 4 Jan 1997 Michael Chastain: changed to gnu as.
|
|
|
|
* 15 Sept 2005 Eric Biederman: 64bit PIC support
|
|
|
|
*
|
|
|
|
* Entry: CS:IP point to the start of our code, we are
|
|
|
|
* in real mode with no stack, but the rest of the
|
|
|
|
* trampoline page to make our stack and everything else
|
|
|
|
* is a mystery.
|
|
|
|
*
|
|
|
|
* On entry to trampoline_data, the processor is in real mode
|
|
|
|
* with 16-bit addressing and 16-bit data. CS has some value
|
|
|
|
* and IP is zero. Thus, data addresses need to be absolute
|
|
|
|
* (no relocation) and are taken with regard to r_base.
|
|
|
|
*
|
|
|
|
* With the addition of trampoline_level4_pgt this code can
|
|
|
|
* now enter a 64bit kernel that lives at arbitrary 64bit
|
|
|
|
* physical addresses.
|
|
|
|
*
|
|
|
|
* If you work on this file, check the object module with objdump
|
|
|
|
* --full-contents --reloc to make sure there are no relocation
|
|
|
|
* entries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/linkage.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <asm/pgtable_types.h>
|
|
|
|
#include <asm/page_types.h>
|
|
|
|
#include <asm/msr.h>
|
|
|
|
#include <asm/segment.h>
|
|
|
|
#include <asm/processor-flags.h>
|
2012-05-09 02:22:37 +08:00
|
|
|
#include "realmode.h"
|
2012-05-09 02:22:28 +08:00
|
|
|
|
|
|
|
.text
|
|
|
|
.balign PAGE_SIZE
|
|
|
|
.code16
|
|
|
|
|
|
|
|
ENTRY(trampoline_data)
|
|
|
|
cli # We should be safe anyway
|
|
|
|
wbinvd
|
|
|
|
|
2012-05-09 02:22:37 +08:00
|
|
|
LJMPW_RM(1f)
|
2012-05-09 02:22:28 +08:00
|
|
|
1:
|
|
|
|
mov %cs, %ax # Code and data in the same place
|
|
|
|
mov %ax, %ds
|
|
|
|
mov %ax, %es
|
|
|
|
mov %ax, %ss
|
|
|
|
|
|
|
|
movl $0xA5A5A5A5, trampoline_status
|
|
|
|
# write marker for master knows we're running
|
|
|
|
|
|
|
|
# Setup stack
|
2012-05-09 02:22:40 +08:00
|
|
|
movl $rm_stack_end, %esp
|
2012-05-09 02:22:28 +08:00
|
|
|
|
|
|
|
call verify_cpu # Verify the cpu supports long mode
|
|
|
|
testl %eax, %eax # Check for return code
|
|
|
|
jnz no_longmode
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GDT tables in non default location kernel can be beyond 16MB and
|
|
|
|
* lgdt will not be able to load the address as in real mode default
|
|
|
|
* operand size is 16bit. Use lgdtl instead to force operand size
|
|
|
|
* to 32 bit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lidtl tidt # load idt with 0, 0
|
|
|
|
lgdtl tgdt # load gdt with whatever is appropriate
|
|
|
|
|
2012-05-09 02:22:40 +08:00
|
|
|
movw $__KERNEL_DS, %dx # Data segment descriptor
|
|
|
|
|
|
|
|
# Enable protected mode
|
|
|
|
movl $X86_CR0_PE, %eax # protected mode (PE) bit
|
|
|
|
movl %eax, %cr0 # into protected mode
|
2012-05-09 02:22:28 +08:00
|
|
|
|
|
|
|
# flush prefetch and jump to startup_32
|
2012-05-09 02:22:35 +08:00
|
|
|
ljmpl $__KERNEL32_CS, $pa_startup_32
|
2012-05-09 02:22:28 +08:00
|
|
|
|
|
|
|
no_longmode:
|
|
|
|
hlt
|
|
|
|
jmp no_longmode
|
|
|
|
#include "../kernel/verify_cpu.S"
|
|
|
|
|
2012-05-09 02:22:33 +08:00
|
|
|
.section ".text32","ax"
|
2012-05-09 02:22:28 +08:00
|
|
|
.code32
|
|
|
|
.balign 4
|
|
|
|
ENTRY(startup_32)
|
2012-05-09 02:22:40 +08:00
|
|
|
movl %edx, %ss
|
|
|
|
addl $pa_real_mode_base, %esp
|
|
|
|
movl %edx, %ds
|
|
|
|
movl %edx, %es
|
|
|
|
movl %edx, %fs
|
|
|
|
movl %edx, %gs
|
2012-05-09 02:22:28 +08:00
|
|
|
|
|
|
|
movl $X86_CR4_PAE, %eax
|
|
|
|
movl %eax, %cr4 # Enable PAE mode
|
|
|
|
|
2012-05-09 02:22:40 +08:00
|
|
|
# Setup trampoline 4 level pagetables
|
|
|
|
movl $pa_level3_ident_pgt, %eax
|
2012-05-09 02:22:28 +08:00
|
|
|
movl %eax, %cr3
|
|
|
|
|
|
|
|
movl $MSR_EFER, %ecx
|
2012-05-09 02:22:40 +08:00
|
|
|
movl $((1 << _EFER_LME) | (1 << _EFER_NX)), %eax # Enable Long Mode
|
2012-05-09 02:22:28 +08:00
|
|
|
xorl %edx, %edx
|
|
|
|
wrmsr
|
|
|
|
|
|
|
|
# Enable paging and in turn activate Long Mode
|
2012-05-09 02:22:40 +08:00
|
|
|
movl $(X86_CR0_PG | X86_CR0_WP | X86_CR0_PE), %eax
|
2012-05-09 02:22:28 +08:00
|
|
|
movl %eax, %cr0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we're in long mode but in 32bit compatibility mode
|
|
|
|
* with EFER.LME = 1, CS.L = 0, CS.D = 1 (and in turn
|
|
|
|
* EFER.LMA = 1). Now we want to jump in 64bit mode, to do that we use
|
|
|
|
* the new gdt/idt that has __KERNEL_CS with CS.L = 1.
|
|
|
|
*/
|
2012-05-09 02:22:35 +08:00
|
|
|
ljmpl $__KERNEL_CS, $pa_startup_64
|
2012-05-09 02:22:28 +08:00
|
|
|
|
2012-05-09 02:22:33 +08:00
|
|
|
.section ".text64","ax"
|
2012-05-09 02:22:28 +08:00
|
|
|
.code64
|
|
|
|
.balign 4
|
|
|
|
ENTRY(startup_64)
|
|
|
|
# Now jump into the kernel using virtual addresses
|
2012-05-09 02:22:40 +08:00
|
|
|
jmpq *startup_64_smp(%rip)
|
2012-05-09 02:22:28 +08:00
|
|
|
|
2012-05-09 02:22:33 +08:00
|
|
|
.section ".rodata","a"
|
|
|
|
.balign 16
|
2012-05-09 02:22:28 +08:00
|
|
|
tidt:
|
|
|
|
.word 0 # idt limit = 0
|
|
|
|
.word 0, 0 # idt base = 0L
|
|
|
|
|
|
|
|
# Duplicate the global descriptor table
|
|
|
|
# so the kernel can live anywhere
|
2012-05-09 02:22:40 +08:00
|
|
|
.balign 16
|
2012-05-09 02:22:28 +08:00
|
|
|
.globl tgdt
|
|
|
|
tgdt:
|
2012-05-09 02:22:40 +08:00
|
|
|
.short tgdt_end - tgdt - 1 # gdt limit
|
2012-05-09 02:22:28 +08:00
|
|
|
.long pa_tgdt
|
|
|
|
.short 0
|
|
|
|
.quad 0x00cf9b000000ffff # __KERNEL32_CS
|
|
|
|
.quad 0x00af9b000000ffff # __KERNEL_CS
|
|
|
|
.quad 0x00cf93000000ffff # __KERNEL_DS
|
|
|
|
tgdt_end:
|
|
|
|
|
2012-05-09 02:22:40 +08:00
|
|
|
.bss
|
|
|
|
|
|
|
|
.balign PAGE_SIZE
|
|
|
|
GLOBAL(level3_ident_pgt) .space 511*8
|
|
|
|
GLOBAL(level3_kernel_pgt) .space 8
|
|
|
|
|
|
|
|
.balign 8
|
|
|
|
GLOBAL(startup_64_smp) .space 8
|
|
|
|
GLOBAL(trampoline_status) .space 4
|