1;------------------------------------------------------------------------------
2;
3; Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>
4; This program and the accompanying materials
5; are licensed and made available under the terms and conditions of the BSD License
6; which accompanies this distribution.  The full text of the license may be found at
7; http://opensource.org/licenses/bsd-license.php.
8;
9; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11;
12; Module Name:
13;
14;   DisablePaging64.Asm
15;
16; Abstract:
17;
18;   AsmDisablePaging64 function
19;
20; Notes:
21;
22;------------------------------------------------------------------------------
23
24    DEFAULT REL
25    SECTION .text
26
27;------------------------------------------------------------------------------
28; VOID
29; EFIAPI
30; InternalX86DisablePaging64 (
31;   IN      UINT16                    Cs,
32;   IN      UINT32                    EntryPoint,
33;   IN      UINT32                    Context1,  OPTIONAL
34;   IN      UINT32                    Context2,  OPTIONAL
35;   IN      UINT32                    NewStack
36;   );
37;------------------------------------------------------------------------------
38global ASM_PFX(InternalX86DisablePaging64)
39ASM_PFX(InternalX86DisablePaging64):
40    cli
41    lea     rsi, [.0]                     ; rsi <- The start address of transition code
42    mov     edi, [rsp + 0x28]            ; rdi <- New stack
43    lea     rax, [mTransitionEnd]         ; rax <- end of transition code
44    sub     rax, rsi                    ; rax <- The size of transition piece code
45    add     rax, 4                      ; Round RAX up to the next 4 byte boundary
46    and     al, 0xfc
47    sub     rdi, rax                    ; rdi <- Use stack to hold transition code
48    mov     r10d, edi                   ; r10 <- The start address of transicition code below 4G
49    push    rcx                         ; save rcx to stack
50    mov     rcx, rax                    ; rcx <- The size of transition piece code
51    rep     movsb                       ; copy transition code to top of new stack which must be below 4GB
52    pop     rcx                         ; restore rcx
53
54    mov     esi, r8d
55    mov     edi, r9d
56    mov     eax, r10d                   ; eax <- start of the transition code on the stack
57    sub     eax, 4                      ; eax <- One slot below transition code on the stack
58    push    rcx                         ; push Cs to stack
59    push    r10                         ; push address of tansition code on stack
60    DB      0x48                         ; prefix to composite "retq" with next "retf"
61    retf                                ; Use far return to load CS register from stack
62
63; Start of transition code
64.0:
65    mov     esp, eax                    ; set up new stack
66    mov     rax, cr0
67    btr     eax, 31                     ; Clear CR0.PG
68    mov     cr0, rax                    ; disable paging and caches
69
70    mov     ebx, edx                    ; save EntryPoint to rbx, for rdmsr will overwrite rdx
71    mov     ecx, 0xc0000080
72    rdmsr
73    and     ah, ~ 1                   ; clear LME
74    wrmsr
75    mov     rax, cr4
76    and     al, ~ (1 << 5)           ; clear PAE
77    mov     cr4, rax
78    push    rdi                         ; push Context2
79    push    rsi                         ; push Context1
80    call    rbx                         ; transfer control to EntryPoint
81    hlt                                 ; no one should get here
82
83mTransitionEnd:
84
85