/*-1* Copyright (c) 2001 Takanori Watanabe <[email protected]>2* Copyright (c) 2001 Mitsuru IWASAKI <[email protected]>3* Copyright (c) 2003 Peter Wemm4* Copyright (c) 2008-2012 Jung-uk Kim <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <machine/asmacros.h>30#include <machine/specialreg.h>31#include <x86/ppireg.h>32#include <x86/timerreg.h>3334#include "assym.inc"3536/*37* Resume entry point for real mode.38*39* If XFirmwareWakingVector is zero and FirmwareWakingVector is non-zero40* in FACS, the BIOS enters here in real mode after POST with CS set to41* (FirmwareWakingVector >> 4) and IP set to (FirmwareWakingVector & 0xf).42* Depending on the previous sleep state, we may need to initialize more43* of the system (i.e., S3 suspend-to-RAM vs. S4 suspend-to-disk).44*45* Note: If XFirmwareWakingVector is non-zero, it should disable address46* translation/paging and interrupts, load all segment registers with47* a flat 4 GB address space, and set EFLAGS.IF to zero. Currently48* this mode is not supported by this code.49*/5051.data /* So we can modify it */5253ALIGN_TEXT54.code1655wakeup_start:56/*57* Set up segment registers for real mode, a small stack for58* any calls we make, and clear any flags.59*/60cli /* make sure no interrupts */61mov %cs, %ax /* copy %cs to %ds. Remember these */62mov %ax, %ds /* are offsets rather than selectors */63mov %ax, %ss64movw $PAGE_SIZE, %sp65xorw %ax, %ax66pushw %ax67popfw6869/* To debug resume hangs, beep the speaker if the user requested. */70testb $~0, resume_beep - wakeup_start71jz 1f72movb $0, resume_beep - wakeup_start7374/* Set PIC timer2 to beep. */75movb $(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT), %al76outb %al, $TIMER_MODE7778/* Turn on speaker. */79inb $IO_PPI, %al80orb $PIT_SPKR, %al81outb %al, $IO_PPI8283/* Set frequency. */84movw $0x4c0, %ax85outb %al, $TIMER_CNTR286shrw $8, %ax87outb %al, $TIMER_CNTR2881:8990/* Re-initialize video BIOS if the reset_video tunable is set. */91testb $~0, reset_video - wakeup_start92jz 1f93movb $0, reset_video - wakeup_start94lcall $0xc000, $39596/* When we reach here, int 0x10 should be ready. Hide cursor. */97movb $0x01, %ah98movb $0x20, %ch99int $0x10100101/* Re-start in case the previous BIOS call clobbers them. */102jmp wakeup_start1031:104105/*106* Find relocation base and patch the gdt descript and ljmp targets107*/108xorl %ebx, %ebx109mov %cs, %bx110sall $4, %ebx /* %ebx is now our relocation base */111112/*113* Load the descriptor table pointer. We'll need it when running114* in 16-bit protected mode.115*/116lgdtl bootgdtdesc - wakeup_start117118/* Enable protected mode */119movl $CR0_PE, %eax120mov %eax, %cr0121122/*123* Now execute a far jump to turn on protected mode. This124* causes the segment registers to turn into selectors and causes125* %cs to be loaded from the gdt.126*127* The following instruction is:128* ljmpl $bootcode32 - bootgdt, $wakeup_32 - wakeup_start129* but gas cannot assemble that. And besides, we patch the targets130* in early startup and its a little clearer what we are patching.131*/132wakeup_sw32:133.byte 0x66 /* size override to 32 bits */134.byte 0xea /* opcode for far jump */135.long wakeup_32 - wakeup_start /* offset in segment */136.word bootcode32 - bootgdt /* index in gdt for 32 bit code */137138/*139* At this point, we are running in 32 bit legacy protected mode.140*/141ALIGN_TEXT142.code32143wakeup_32:144145mov $bootdata32 - bootgdt, %eax146mov %ax, %ds147148/*149* Turn on the PAE bit and optionally the LA57 bit for when paging150* is later enabled.151*/152mov %cr4, %eax153orl $CR4_PAE, %eax154leal wakeup_pagetables - wakeup_start(%ebx), %ecx155movl (%ecx), %ecx156testl $0x1, %ecx157je 1f158orl $CR4_LA57, %eax1591: mov %eax, %cr4160161/*162* Enable EFER.LME so that we get long mode when all the prereqs are163* in place. In this case, it turns on when CR0_PG is finally enabled.164* Also it picks up a few other EFER bits that we'll use need we're165* here, like SYSCALL and NX enable.166*/167movl $MSR_EFER, %ecx168movl wakeup_efer - wakeup_start(%ebx), %eax169movl wakeup_efer + 4 - wakeup_start(%ebx), %edx170wrmsr171172/*173* Point to the embedded page tables for startup. Note that this174* only gets accessed after we're actually in 64 bit mode, however175* we can only set the bottom 32 bits of %cr3 in this state. This176* means we are required to use a temporary page table that is below177* the 4GB limit. %ebx is still our relocation base. We could just178* subtract 3 * PAGE_SIZE, but that would be too easy.179*/180leal wakeup_pagetables - wakeup_start(%ebx), %eax181movl (%eax), %eax182andl $~0x1, %eax183mov %eax, %cr3184185/*186* Finally, switch to long bit mode by enabling paging. We have187* to be very careful here because all the segmentation disappears188* out from underneath us. The spec says we can depend on the189* subsequent pipelined branch to execute, but *only if* everything190* is still identity mapped. If any mappings change, the pipeline191* will flush.192*/193mov %cr0, %eax194orl $CR0_PG, %eax195mov %eax, %cr0196197/*198* At this point paging is enabled, and we are in "compatibility" mode.199* We do another far jump to reload %cs with the 64 bit selector.200* %cr3 points to a 4-level page table page.201* We cannot yet jump all the way to the kernel because we can only202* specify a 32 bit linear address. So, yet another trampoline.203*204* The following instruction is:205* ljmp $bootcode64 - bootgdt, $wakeup_64 - wakeup_start206* but gas cannot assemble that. And besides, we patch the targets207* in early startup and its a little clearer what we are patching.208*/209wakeup_sw64:210.byte 0xea /* opcode for far jump */211.long wakeup_64 - wakeup_start /* offset in segment */212.word bootcode64 - bootgdt /* index in gdt for 64 bit code */213214/*215* Yeehar! We're running in 64-bit mode! We can mostly ignore our216* segment registers, and get on with it.217* Note that we are running at the correct virtual address, but with218* a 1:1 1GB mirrored mapping over entire address space. We had better219* switch to a real %cr3 promptly so that we can get to the direct map220* space. Remember that jmp is relative and that we've been relocated,221* so use an indirect jump.222*/223ALIGN_TEXT224.code64225wakeup_64:226mov $bootdata64 - bootgdt, %eax227mov %ax, %ds228229/* Restore arguments. */230movq wakeup_pcb - wakeup_start(%rbx), %rdi231movq wakeup_ret - wakeup_start(%rbx), %rax232233/* Restore GDT. */234lgdt wakeup_gdt - wakeup_start(%rbx)235236/* Jump to return address. */237jmp *%rax238239.data240241resume_beep:242.byte 0243reset_video:244.byte 0245246ALIGN_DATA247bootgdt:248.long 0x00000000249.long 0x00000000250.long 0x00000000251.long 0x00000000252.long 0x00000000253.long 0x00000000254.long 0x00000000255.long 0x00000000256257bootcode64:258.long 0x0000ffff259.long 0x00af9b00260261bootdata64:262.long 0x0000ffff263.long 0x00af9300264265bootcode32:266.long 0x0000ffff267.long 0x00cf9b00268269bootdata32:270.long 0x0000ffff271.long 0x00cf9300272bootgdtend:273274wakeup_pagetables:275.long 0276277bootgdtdesc:278.word bootgdtend - bootgdt /* Length */279.long bootgdt - wakeup_start /* Offset plus %ds << 4 */280281ALIGN_DATA282wakeup_pcb:283.quad 0284wakeup_ret:285.quad 0286wakeup_efer:287.quad 0288wakeup_gdt:289.word 0290.quad 0291dummy:292293294