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    .code
25
26;------------------------------------------------------------------------------
27; VOID
28; EFIAPI
29; InternalX86DisablePaging64 (
30;   IN      UINT16                    Cs,
31;   IN      UINT32                    EntryPoint,
32;   IN      UINT32                    Context1,  OPTIONAL
33;   IN      UINT32                    Context2,  OPTIONAL
34;   IN      UINT32                    NewStack
35;   );
36;------------------------------------------------------------------------------
37InternalX86DisablePaging64    PROC
38    cli
39    lea     rsi, @F                     ; rsi <- The start address of transition code
40    mov     edi, [rsp + 28h]            ; rdi <- New stack
41    lea     rax, mTransitionEnd         ; rax <- end of transition code
42    sub     rax, rsi                    ; rax <- The size of transition piece code
43    add     rax, 4                      ; Round RAX up to the next 4 byte boundary
44    and     al, 0fch
45    sub     rdi, rax                    ; rdi <- Use stack to hold transition code
46    mov     r10d, edi                   ; r10 <- The start address of transicition code below 4G
47    push    rcx                         ; save rcx to stack
48    mov     rcx, rax                    ; rcx <- The size of transition piece code
49    rep     movsb                       ; copy transition code to top of new stack which must be below 4GB
50    pop     rcx                         ; restore rcx
51
52    mov     esi, r8d
53    mov     edi, r9d
54    mov     eax, r10d                   ; eax <- start of the transition code on the stack
55    sub     eax, 4                      ; eax <- One slot below transition code on the stack
56    push    rcx                         ; push Cs to stack
57    push    r10                         ; push address of tansition code on stack
58    DB      48h                         ; prefix to composite "retq" with next "retf"
59    retf                                ; Use far return to load CS register from stack
60
61; Start of transition code
62@@:
63    mov     esp, eax                    ; set up new stack
64    mov     rax, cr0
65    btr     eax, 31                     ; Clear CR0.PG
66    mov     cr0, rax                    ; disable paging and caches
67
68    mov     ebx, edx                    ; save EntryPoint to rbx, for rdmsr will overwrite rdx
69    mov     ecx, 0c0000080h
70    rdmsr
71    and     ah, NOT 1                   ; clear LME
72    wrmsr
73    mov     rax, cr4
74    and     al, NOT (1 SHL 5)           ; clear PAE
75    mov     cr4, rax
76    push    rdi                         ; push Context2
77    push    rsi                         ; push Context1
78    call    rbx                         ; transfer control to EntryPoint
79    hlt                                 ; no one should get here
80InternalX86DisablePaging64    ENDP
81
82mTransitionEnd LABEL    BYTE
83
84    END
85