1;; @file
2;   This is the assembly code for page fault handler hook.
3;
4; Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5;
6; This program and the accompanying materials
7; are licensed and made available under the terms and conditions of the BSD License
8; which accompanies this distribution.  The full text of the license may be found at
9; http://opensource.org/licenses/bsd-license.php
10;
11; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13;
14;;
15
16EXTERN PageFaultHandler:PROC
17
18    .code
19
20PageFaultHandlerHook PROC
21    add     rsp, -10h
22    ; save rax
23    mov     [rsp + 08h], rax
24
25    ;push    rax                         ; save all volatile registers
26    push    rcx
27    push    rdx
28    push    r8
29    push    r9
30    push    r10
31    push    r11
32    ; save volatile fp registers
33    ; 68h + 08h(for alignment)
34    add     rsp, -70h
35    stmxcsr [rsp + 60h]
36    movdqa  [rsp + 0h], xmm0
37    movdqa  [rsp + 10h], xmm1
38    movdqa  [rsp + 20h], xmm2
39    movdqa  [rsp + 30h], xmm3
40    movdqa  [rsp + 40h], xmm4
41    movdqa  [rsp + 50h], xmm5
42
43    add     rsp, -20h
44    call    PageFaultHandler
45    add     rsp, 20h
46
47    ; load volatile fp registers
48    ldmxcsr [rsp + 60h]
49    movdqa  xmm0,  [rsp + 0h]
50    movdqa  xmm1,  [rsp + 10h]
51    movdqa  xmm2,  [rsp + 20h]
52    movdqa  xmm3,  [rsp + 30h]
53    movdqa  xmm4,  [rsp + 40h]
54    movdqa  xmm5,  [rsp + 50h]
55    add     rsp, 70h
56
57    pop     r11
58    pop     r10
59    pop     r9
60    pop     r8
61    pop     rdx
62    pop     rcx
63    ;pop     rax                         ; restore all volatile registers
64
65    add     rsp, 10h
66
67    ; rax returned from PageFaultHandler is NULL or OriginalHandler address
68    ; NULL if the page fault is handled by PageFaultHandler
69    ; OriginalHandler address if the page fault is not handled by PageFaultHandler
70    test    rax, rax
71
72    ; save OriginalHandler address
73    mov     [rsp - 10h], rax
74    ; restore rax
75    mov     rax, [rsp - 08h]
76
77    jz      @F
78
79    ; jump to OriginalHandler
80    jmp     qword ptr [rsp - 10h]
81
82@@:
83    add     rsp, 08h                    ; skip error code for PF
84    iretq
85PageFaultHandlerHook ENDP
86
87    END
88