]> git.proxmox.com Git - mirror_edk2.git/commitdiff
1. Added comments to ASM files
authorbxing <bxing@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 10 Jan 2007 06:57:04 +0000 (06:57 +0000)
committerbxing <bxing@6f19259b-4bc3-4df7-8a09-765794883524>
Wed, 10 Jan 2007 06:57:04 +0000 (06:57 +0000)
2. Fixed a bug in 64-bit AsmDisablePaging64(), which may cause a #GP exception.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@2206 6f19259b-4bc3-4df7-8a09-765794883524

45 files changed:
MdePkg/Library/BaseLib/Ia32/CpuFlushTlb.asm
MdePkg/Library/BaseLib/Ia32/DivU64x32Remainder.asm
MdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.asm
MdePkg/Library/BaseLib/Ia32/EnablePaging32.asm
MdePkg/Library/BaseLib/Ia32/EnablePaging64.asm
MdePkg/Library/BaseLib/Ia32/FxRestore.asm
MdePkg/Library/BaseLib/Ia32/FxSave.asm
MdePkg/Library/BaseLib/Ia32/LongJump.asm
MdePkg/Library/BaseLib/Ia32/MultU64x64.asm
MdePkg/Library/BaseLib/Ia32/RRotU64.asm
MdePkg/Library/BaseLib/Ia32/RShiftU64.asm
MdePkg/Library/BaseLib/Ia32/ReadDr4.asm
MdePkg/Library/BaseLib/Ia32/ReadDr5.asm
MdePkg/Library/BaseLib/Ia32/SetJump.asm
MdePkg/Library/BaseLib/Ia32/SwapBytes64.asm
MdePkg/Library/BaseLib/Ia32/Thunk16.asm
MdePkg/Library/BaseLib/Ia32/WriteDr4.asm
MdePkg/Library/BaseLib/Ia32/WriteDr5.asm
MdePkg/Library/BaseLib/X64/DisablePaging64.asm
MdePkg/Library/BaseLib/X64/EnablePaging64.asm
MdePkg/Library/BaseLib/X64/LongJump.asm
MdePkg/Library/BaseLib/X64/Monitor.asm
MdePkg/Library/BaseLib/X64/Mwait.asm
MdePkg/Library/BaseLib/X64/ReadDr4.asm
MdePkg/Library/BaseLib/X64/ReadDr5.asm
MdePkg/Library/BaseLib/X64/ReadMm0.asm
MdePkg/Library/BaseLib/X64/ReadMm1.asm
MdePkg/Library/BaseLib/X64/ReadMm2.asm
MdePkg/Library/BaseLib/X64/ReadMm3.asm
MdePkg/Library/BaseLib/X64/ReadMm4.asm
MdePkg/Library/BaseLib/X64/ReadMm5.asm
MdePkg/Library/BaseLib/X64/ReadMm6.asm
MdePkg/Library/BaseLib/X64/ReadMm7.asm
MdePkg/Library/BaseLib/X64/Thunk16.asm
MdePkg/Library/BaseLib/X64/WriteDr4.asm
MdePkg/Library/BaseLib/X64/WriteDr5.asm
MdePkg/Library/BaseLib/X64/WriteMm0.asm
MdePkg/Library/BaseLib/X64/WriteMm1.asm
MdePkg/Library/BaseLib/X64/WriteMm2.asm
MdePkg/Library/BaseLib/X64/WriteMm3.asm
MdePkg/Library/BaseLib/X64/WriteMm4.asm
MdePkg/Library/BaseLib/X64/WriteMm5.asm
MdePkg/Library/BaseLib/X64/WriteMm6.asm
MdePkg/Library/BaseLib/X64/WriteMm7.asm
MdePkg/Library/BaseLib/X64/WriteMsr64.asm

index 367412403e1bdb3c448ade927854e55dfe32561f..4f0c4f9944b9326f465f550f788312af58717dd3 100644 (file)
@@ -33,7 +33,7 @@
 ;------------------------------------------------------------------------------\r
 CpuFlushTlb PROC\r
     mov     eax, cr3\r
-    mov     cr3, eax\r
+    mov     cr3, eax                    ; moving to CR3 flushes TLB\r
     ret\r
 CpuFlushTlb ENDP\r
 \r
index a836e22d0db671c83f9fae605fe4570c06b2e918..e025b22c2d278fe8869cbffdc15c8abba7090094 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalMathDivRemU64x32    PROC\r
-    mov     ecx, [esp + 12]\r
-    mov     eax, [esp + 8]\r
+    mov     ecx, [esp + 12]         ; ecx <- divisor\r
+    mov     eax, [esp + 8]          ; eax <- dividend[32..63]\r
     xor     edx, edx\r
-    div     ecx\r
+    div     ecx                     ; eax <- quotient[32..63], edx <- remainder\r
     push    eax\r
-    mov     eax, [esp + 8]\r
-    div     ecx\r
-    mov     ecx, [esp + 20]\r
+    mov     eax, [esp + 8]          ; eax <- dividend[0..31]\r
+    div     ecx                     ; eax <- quotient[0..31]\r
+    mov     ecx, [esp + 20]         ; ecx <- Remainder\r
     jecxz   @F                      ; abandon remainder if Remainder == NULL\r
     mov     [ecx], edx\r
 @@:\r
-    pop     edx\r
+    pop     edx                     ; edx <- quotient[32..63]\r
     ret\r
 InternalMathDivRemU64x32    ENDP\r
 \r
index 61a53d1e3a42f7d1d115322d910347ec41598e18..95e57efac14c32128e98a1e63164d8ed99a1c4ba 100644 (file)
@@ -36,13 +36,13 @@ EXTERN  InternalMathDivRemU64x32:PROC
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalMathDivRemU64x64    PROC\r
-    mov     ecx, [esp + 16]\r
+    mov     ecx, [esp + 16]             ; ecx <- divisor[32..63]\r
     test    ecx, ecx\r
     jnz     _@DivRemU64x64              ; call _@DivRemU64x64 if Divisor > 2^32\r
     mov     ecx, [esp + 20]\r
     jecxz   @F\r
-    and     dword ptr [ecx + 4], 0\r
-    mov     [esp + 16], ecx\r
+    and     dword ptr [ecx + 4], 0      ; zero high dword of remainder\r
+    mov     [esp + 16], ecx             ; set up stack frame to match DivRemU64x32\r
 @@:\r
     jmp     InternalMathDivRemU64x32\r
 InternalMathDivRemU64x64    ENDP\r
@@ -61,11 +61,11 @@ _@DivRemU64x64  PROC    USES    ebx esi edi
     jnz     @B\r
     div     ebx\r
     mov     ebx, eax                    ; ebx <- quotient\r
-    mov     ecx, [esp + 28]\r
-    mul     dword ptr [esp + 24]\r
-    imul    ecx, ebx\r
-    add     edx, ecx\r
-    mov     ecx, dword ptr [esp + 32]\r
+    mov     ecx, [esp + 28]             ; ecx <- high dword of divisor\r
+    mul     dword ptr [esp + 24]        ; edx:eax <- quotient * divisor[0..31]\r
+    imul    ecx, ebx                    ; ecx <- quotient * divisor[32..63]\r
+    add     edx, ecx                    ; edx <- (quotient * divisor)[32..63]\r
+    mov     ecx, dword ptr [esp + 32]   ; ecx <- addr for Remainder\r
     jc      @TooLarge                   ; product > 2^64\r
     cmp     edi, edx                    ; compare high 32 bits\r
     ja      @Correct\r
@@ -76,7 +76,7 @@ _@DivRemU64x64  PROC    USES    ebx esi edi
     dec     ebx                         ; adjust quotient by -1\r
     jecxz   @Return                     ; return if Remainder == NULL\r
     sub     eax, dword ptr [esp + 24]\r
-    sbb     edx, dword ptr [esp + 28]\r
+    sbb     edx, dword ptr [esp + 28]   ; edx:eax <- (quotient - 1) * divisor\r
 @Correct:\r
     jecxz   @Return\r
     sub     esi, eax\r
@@ -85,7 +85,7 @@ _@DivRemU64x64  PROC    USES    ebx esi edi
     mov     [ecx + 4], edi\r
 @Return:\r
     mov     eax, ebx                    ; eax <- quotient\r
-    xor     edx, edx\r
+    xor     edx, edx                    ; quotient is 32 bits long\r
     ret\r
 _@DivRemU64x64  ENDP\r
 \r
index cf6f2fe458ede509c6223b8c07a499f93c6c6208..ae98b93285e83c0c5cdabd09f09ce2551299286c 100644 (file)
@@ -40,14 +40,14 @@ InternalX86EnablePaging32 PROC
     mov     ecx, [esp + 8]\r
     mov     edx, [esp + 12]\r
     pushfd\r
-    pop     edi\r
+    pop     edi                         ; save flags in edi\r
     cli\r
     mov     eax, cr0\r
     bts     eax, 31\r
     mov     esp, [esp + 16]\r
     mov     cr0, eax\r
     push    edi\r
-    popfd\r
+    popfd                               ; restore flags\r
     push    edx\r
     push    ecx\r
     call    ebx\r
index 354d1fe60340c1a707adb6cfa689327f3dceafa5..435bb391058fabcd55d4d28e5592987272469574 100644 (file)
@@ -47,11 +47,11 @@ InternalX86EnablePaging64 PROC
     or      ah, 1                       ; set LME\r
     wrmsr\r
     mov     eax, cr0\r
-    bts     eax, 31\r
+    bts     eax, 31                     ; set PG\r
     mov     cr0, eax                    ; enable paging\r
-    retf\r
+    retf                                ; topmost 2 dwords hold the address\r
 @@:                                     ; long mode starts here\r
-    DB      67h, 48h\r
+    DB      67h, 48h                    ; 32-bit address size, 64-bit operand size\r
     mov     ebx, [esp]                  ; mov rbx, [esp]\r
     DB      67h, 48h\r
     mov     ecx, [esp + 8]              ; mov rcx, [esp + 8]\r
@@ -62,7 +62,7 @@ InternalX86EnablePaging64 PROC
     DB      48h\r
     add     esp, -20h                   ; add rsp, -20h\r
     call    ebx                         ; call rbx\r
-    jmp     $\r
+    hlt                                 ; no one should get here\r
 InternalX86EnablePaging64 ENDP\r
 \r
     END\r
index 9bcc6ac885c1abf281931b2aa7ae8155c20ad79b..5def31170032f6ee9ac7e58b30b5204d448cc5f4 100644 (file)
@@ -34,7 +34,7 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalX86FxRestore  PROC\r
-    mov     eax, [esp + 4]\r
+    mov     eax, [esp + 4]              ; Buffer must be 16-byte aligned\r
     fxrstor [eax]\r
     ret\r
 InternalX86FxRestore  ENDP\r
index 7d5ea45ffb79ea0897c6170eef2026d5ebd7f634..d3a9f9cd4809b17137e198bd326067bb3825e2ca 100644 (file)
@@ -34,7 +34,7 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalX86FxSave PROC\r
-    mov     eax, [esp + 4]\r
+    mov     eax, [esp + 4]              ; Buffer must be 16-byte aligned\r
     fxsave  [eax]\r
     ret\r
 InternalX86FxSave ENDP\r
index a634c4599c41cfa16d1d774021f5fa74fc05a5be..0309f6ae7157c8a62c22d891128ed6647ed5daf7 100644 (file)
@@ -40,7 +40,7 @@ InternalLongJump    PROC
     mov     edi, [edx + 8]\r
     mov     ebp, [edx + 12]\r
     mov     esp, [edx + 16]\r
-    jmp     dword ptr [edx + 20]\r
+    jmp     dword ptr [edx + 20]        ; restore "eip"\r
 InternalLongJump    ENDP\r
 \r
     END\r
index 377aa5197227fc6a335fb46273e51bfa098ace26..f3679548f15226fd1deb830c339218b1992780b6 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalMathMultU64x64  PROC    USES    ebx\r
-    mov     ebx, [esp + 8]\r
-    mov     edx, [esp + 16]\r
+    mov     ebx, [esp + 8]              ; ebx <- M1[0..31]\r
+    mov     edx, [esp + 16]             ; edx <- M2[0..31]\r
     mov     ecx, ebx\r
     mov     eax, edx\r
-    imul    ebx, [esp + 20]\r
-    imul    edx, [esp + 12]\r
-    add     ebx, edx\r
-    mul     ecx\r
-    add     edx, ebx\r
+    imul    ebx, [esp + 20]             ; ebx <- M1[0..31] * M2[32..63]\r
+    imul    edx, [esp + 12]             ; edx <- M1[32..63] * M2[0..31]\r
+    add     ebx, edx                    ; carries are abandoned\r
+    mul     ecx                         ; edx:eax <- M1[0..31] * M2[0..31]\r
+    add     edx, ebx                    ; carries are abandoned\r
     ret\r
 InternalMathMultU64x64  ENDP\r
 \r
index 4c58878ace0d11f6f81fc53843378d3e2287b176..fe13abc0d574f6c7fdd25e135c66201328e03437 100644 (file)
@@ -40,7 +40,7 @@ InternalMathRRotU64 PROC    USES    ebx
     rol     ebx, cl\r
     shrd    edx, ebx, cl\r
     test    cl, 32                      ; Count >= 32?\r
-    cmovnz  ecx, eax\r
+    cmovnz  ecx, eax                    ; switch eax & edx if Count >= 32\r
     cmovnz  eax, edx\r
     cmovnz  edx, ecx\r
     ret\r
index 793a8f988fdf28a7996ade12fc18ef24b38d4d87..faa4e9677112ed17be200d3b60ec396c3e6d7e38 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalMathRShiftU64   PROC\r
-    mov     cl, [esp + 12]\r
+    mov     cl, [esp + 12]              ; cl <- Count\r
     xor     edx, edx\r
     mov     eax, [esp + 8]\r
-    test    cl, 32\r
+    test    cl, 32                      ; Count >= 32?\r
     cmovz   edx, eax\r
     cmovz   eax, [esp + 4]\r
     shrd    eax, edx, cl\r
index d1fffe4ec0792aa46e898e74fc66e41c023f60a1..606f26fe2abb59bb3e2e0c784ab92b15fb822ccb 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadDr4  PROC\r
+    ;\r
+    ; DR4 is alias to DR6 only if DE (in CR4) is cleared. Otherwise, reading\r
+    ; this register will cause a #UD exception.\r
+    ;\r
+    ; MS assembler doesn't support this instruction since no one would use it\r
+    ; under normal circustances. Here opcode is used.\r
+    ;\r
     DB      0fh, 21h, 0e0h\r
     ret\r
 AsmReadDr4  ENDP\r
index bba8b602ba196abead08448782f6b1c50564b3ec..091792dab9260688bbb7db048635c6959546f476 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadDr5  PROC\r
+    ;\r
+    ; DR5 is alias to DR7 only if DE (in CR4) is cleared. Otherwise, reading\r
+    ; this register will cause a #UD exception.\r
+    ;\r
+    ; MS assembler doesn't support this instruction since no one would use it\r
+    ; under normal circustances. Here opcode is used.\r
+    ;\r
     DB      0fh, 21h, 0e8h\r
     ret\r
 AsmReadDr5  ENDP\r
index 4d745e29ac9898f227b8dbee8e32cb125db6e922..4b7b40c47a4b2faf89ec972c2e4f60d153eba3ce 100644 (file)
@@ -34,16 +34,16 @@ InternalAssertJumpBuffer    PROTO   C
 ;------------------------------------------------------------------------------\r
 SetJump     PROC\r
     push    [esp + 4]\r
-    call    InternalAssertJumpBuffer\r
-    pop     ecx\r
+    call    InternalAssertJumpBuffer    ; To validate JumpBuffer\r
     pop     ecx\r
+    pop     ecx                         ; ecx <- return address\r
     mov     edx, [esp]\r
     mov     [edx], ebx\r
     mov     [edx + 4], esi\r
     mov     [edx + 8], edi\r
     mov     [edx + 12], ebp\r
     mov     [edx + 16], esp\r
-    mov     [edx + 20], ecx\r
+    mov     [edx + 20], ecx             ; eip value to restore in LongJump\r
     xor     eax, eax\r
     jmp     ecx\r
 SetJump     ENDP\r
index 186d094eee0fad84bc2d351ff4a2a2d5611acc7d..7647880202c47691637ce59a6f6d83d529667ab0 100644 (file)
@@ -33,8 +33,8 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalMathSwapBytes64 PROC\r
-    mov     eax, [esp + 8]\r
-    mov     edx, [esp + 4]\r
+    mov     eax, [esp + 8]              ; eax <- upper 32 bits\r
+    mov     edx, [esp + 4]              ; edx <- lower 32 bits\r
     bswap   eax\r
     bswap   edx\r
     ret\r
index 5e357991271d07efb15e95075027833d4075512a..5c9bc81a2bc3e6a1a624bddf7b65e1829a73846a 100644 (file)
@@ -75,6 +75,11 @@ SavedGdt    LABEL   FWORD
 ; by user code. It will be shadowed to somewhere in memory below 1MB.\r
 ;------------------------------------------------------------------------------\r
 _BackFromUserCode   PROC\r
+    ;\r
+    ; The order of saved registers on the stack matches the order they appears\r
+    ; in IA32_REGS structure. This facilitates wrapper function to extract them\r
+    ; into that structure.\r
+    ;\r
     push    ss\r
     push    cs\r
     DB      66h\r
@@ -104,6 +109,11 @@ _ThunkAttr  DD      ?
     mov     eax, ss\r
     DB      67h\r
     lea     bp, [esp + sizeof (IA32_REGS)]\r
+    ;\r
+    ; esi's in the following 2 instructions are indeed bp in 16-bit code. Fact\r
+    ; is "esi" in 32-bit addressing mode has the same encoding of "bp" in 16-\r
+    ; bit addressing mode.\r
+    ;\r
     mov     word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp\r
     mov     ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP\r
     shl     ax, 4                       ; shl eax, 4\r
@@ -167,7 +177,7 @@ _ToUserCode PROC
     pop     fs\r
     pop     gs\r
     popf                                ; popfd\r
-    DB      66h\r
+    DB      66h                         ; Use 32-bit addressing for "retf" below\r
     retf                                ; transfer control to user code\r
 _ToUserCode ENDP\r
 \r
@@ -197,7 +207,7 @@ GdtEnd          LABEL   QWORD
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalAsmThunk16  PROC    USES    ebp ebx esi edi ds  es  fs  gs\r
-    mov     esi, [esp + 36]             ; esi <- RegSet\r
+    mov     esi, [esp + 36]             ; esi <- RegSet, the 1st parameter\r
     movzx   edx, (IA32_REGS ptr [esi])._SS\r
     mov     edi, (IA32_REGS ptr [esi])._ESP\r
     add     edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space\r
@@ -227,11 +237,11 @@ InternalAsmThunk16  PROC    USES    ebp ebx esi edi ds  es  fs  gs
     push    10h\r
     pop     ecx                         ; ecx <- selector for data segments\r
     lgdt    fword ptr [edx + (_16Gdtr - SavedCr0)]\r
-    pushfd\r
+    pushfd                              ; Save df/if indeed\r
     call    fword ptr [edx + (_EntryPoint - SavedCr0)]\r
     popfd\r
     lidt    fword ptr [esp + 36]        ; restore protected mode IDTR\r
-    lea     eax, [ebp - sizeof (IA32_REGS)]\r
+    lea     eax, [ebp - sizeof (IA32_REGS)] ; eax <- the address of IA32_REGS\r
     ret\r
 InternalAsmThunk16  ENDP\r
 \r
index ca9a839f7028f8a053348640221f8d373e5f89dd..9c10305b4da7079c6124977395a4d9c0388754b0 100644 (file)
 ;------------------------------------------------------------------------------\r
 AsmWriteDr4 PROC\r
     mov     eax, [esp + 4]\r
+    ;\r
+    ; DR4 is alias to DR6 only if DE (in CR4) is cleared. Otherwise, writing to\r
+    ; this register will cause a #UD exception.\r
+    ;\r
+    ; MS assembler doesn't support this instruction since no one would use it\r
+    ; under normal circustances. Here opcode is used.\r
+    ;\r
     DB      0fh, 23h, 0e0h\r
     ret\r
 AsmWriteDr4 ENDP\r
index c6def6820cde14de5ddfef07199a90ac3bcf217e..e10434ef3d389662b0bde2436cc191d7e5aa39a1 100644 (file)
 ;------------------------------------------------------------------------------\r
 AsmWriteDr5 PROC\r
     mov     eax, [esp + 4]\r
+    ;\r
+    ; DR5 is alias to DR7 only if DE (in CR4) is cleared. Otherwise, writing to\r
+    ; this register will cause a #UD exception.\r
+    ;\r
+    ; MS assembler doesn't support this instruction since no one would use it\r
+    ; under normal circustances. Here opcode is used.\r
+    ;\r
     DB      0fh, 23h, 0e8h\r
     ret\r
 AsmWriteDr5 ENDP\r
index 2609c941aa0a0aa2d715ba14249342396e00320d..518137ddc9ee3f8777024bec5b18795f6aac3cf6 100644 (file)
 ;------------------------------------------------------------------------------\r
 InternalX86DisablePaging64    PROC\r
     cli\r
-    shl     rcx, 32\r
+    shl     rcx, 32                     ; rcx[32..47] <- Cs\r
     lea     eax, @F\r
-    mov     ecx, eax\r
-    push    rcx\r
-    mov     ebx, edx\r
     mov     esi, r8d\r
+    or      rcx, rax                    ; rcx[0..47] <- Cs:@F\r
     mov     edi, r9d\r
-    mov     eax, [rsp + 28h]\r
-    retf\r
+    mov     eax, [rsp + 28h]            ; eax <- New Stack\r
+    push    rcx\r
+    retf                                ; switch to compatibility mode\r
 @@:\r
     mov     esp, eax                    ; set up new stack\r
     mov     rax, cr0\r
@@ -57,10 +56,10 @@ InternalX86DisablePaging64    PROC
     mov     rax, cr4\r
     and     al, NOT (1 SHL 5)           ; clear PAE\r
     mov     cr4, rax\r
-    push    rdi\r
-    push    rsi\r
-    call    rbx\r
-    jmp     $\r
+    push    rdi                         ; push Context2\r
+    push    rsi                         ; push Context1\r
+    call    rdx                         ; transfer control to EntryPoint\r
+    hlt                                 ; no one should get here\r
 InternalX86DisablePaging64    ENDP\r
 \r
     END\r
index 459120a886cce71005364a05b89af7ce8394cb55..c54843008a4554b19f770861afe28b9a3520cdae 100644 (file)
@@ -36,7 +36,7 @@
 ;------------------------------------------------------------------------------\r
 InternalX86EnablePaging64 PROC\r
     cli\r
-    pop     rax\r
+    pop     rax                         ; skip the return address\r
     call    @Base\r
 @Base:\r
     add     dword ptr [rsp], @F - @Base ; offset for far retf, seg is the 1st arg\r
@@ -58,7 +58,7 @@ InternalX86EnablePaging64 PROC
     mov     rsp, [esp + 18h]\r
     add     rsp, -20h\r
     call    rbx\r
-    jmp     $                           ; halt processor if EntryPoint() returned\r
+    hlt                                 ; halt processor if EntryPoint() returned\r
 InternalX86EnablePaging64 ENDP\r
 \r
     END\r
index 6ef5f964e0e5d78b1d2e093d806501ba7b2cf43c..38e560608131a7a1339d48541a6f531aa73991a0 100644 (file)
@@ -39,7 +39,7 @@ InternalLongJump    PROC
     mov     r13, [rcx + 30h]\r
     mov     r14, [rcx + 38h]\r
     mov     r15, [rcx + 40h]\r
-    mov     rax, rdx\r
+    mov     rax, rdx                    ; set return value\r
     jmp     qword ptr [rcx + 48h]\r
 InternalLongJump    ENDP\r
 \r
index 0140494d30b15ee50a2c6ae48c8202d3cc66b580..ec9c3f8d030cb121e21ac4ebe9c39c9f4e0ffc39 100644 (file)
@@ -36,7 +36,7 @@ AsmMonitor  PROC
     mov     eax, ecx\r
     mov     ecx, edx\r
     mov     edx, r8d\r
-    DB      0fh, 1, 0c8h\r
+    DB      0fh, 1, 0c8h                ; monitor\r
     ret\r
 AsmMonitor  ENDP\r
 \r
index 8f76d944f14b043681ef3cfd2c6ac767176c035b..aae1d4275809b8cdf92df685bfa136e0b607125d 100644 (file)
@@ -34,7 +34,7 @@
 AsmMwait    PROC\r
     mov     eax, ecx\r
     mov     ecx, edx\r
-    DB      0fh, 1, 0c9h\r
+    DB      0fh, 1, 0c9h                ; mwait\r
     ret\r
 AsmMwait    ENDP\r
 \r
index 97740016b3494a4ac5f7764a829d26c9105ffc0a..2bed12928b8a38a007f24ec53346893c9aa82baa 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadDr4  PROC\r
+    ;\r
+    ; There's no obvious reason to access this register, since it's aliased to\r
+    ; DR7 when DE=0 or an exception generated when DE=1\r
+    ;\r
     DB      0fh, 21h, 0e0h\r
     ret\r
 AsmReadDr4  ENDP\r
index 0582502220803c163dc35af5ae4ab88ce8f337e0..3bee3a56ec4410786a1e723638a1b724c2be72f1 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadDr5  PROC\r
+    ;\r
+    ; There's no obvious reason to access this register, since it's aliased to\r
+    ; DR7 when DE=0 or an exception generated when DE=1\r
+    ;\r
     DB      0fh, 21h, 0e8h\r
     ret\r
 AsmReadDr5  ENDP\r
index a81d20829863ea3578c6455cf95c249618f79bbb..0ed5c8f8aeb0d38765411b93f3406168720f3647 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm0  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0c0h\r
     ret\r
 AsmReadMm0  ENDP\r
index 5da8c4f9e58bd1d16f854e50a57a35386ab7ee09..e24fcb5f9dbb23f2c62187b40df6bd6c3131768d 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm1  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0c8h\r
     ret\r
 AsmReadMm1  ENDP\r
index 3b2b1e3af3e02de5a268b38c319e830793ced602..860326b37539be4b1faa869192529c865d184551 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm2  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0d0h\r
     ret\r
 AsmReadMm2  ENDP\r
index 6283c30285fa1dd7d8e4044f0424419fbc838526..aca6f3d2df0529528f929ce58ed92f989018f182 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm3  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0d8h\r
     ret\r
 AsmReadMm3  ENDP\r
index a65f130283e50be3a7e722fdfe214d197e006513..dd52f99436b17d329324bd70714d989e99b4abf6 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm4  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0e0h\r
     ret\r
 AsmReadMm4  ENDP\r
index 5295c38ae01fb4ea074d2864863c019a33486c9c..1f30e4bed47121812312674de1d1fed02b3230a2 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm5  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0e8h\r
     ret\r
 AsmReadMm5  ENDP\r
index acdc99b888adddd14229cebf2944d4ed79de6e0a..4e1f2a824bb992a4650bf0512645bf5f2d98d188 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm6  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0f0h\r
     ret\r
 AsmReadMm6  ENDP\r
index 7a1b4e6111abefcd2e30ed33f5eb5b2c968b4894..d871a9f8dcd79763e6d274208472d46156f3e830 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmReadMm7  PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 7eh, 0f8h\r
     ret\r
 AsmReadMm7  ENDP\r
index 642310869b6fe6ec6574f56ed387f054dd9cab03..556a8b47e37d0d0820f14c176bcf46dfd3dc30a9 100644 (file)
@@ -66,6 +66,14 @@ SavedGdt    LABEL   FWORD
 ; by user code. It will be shadowed to somewhere in memory below 1MB.\r
 ;------------------------------------------------------------------------------\r
 _BackFromUserCode   PROC\r
+    ;\r
+    ; The order of saved registers on the stack matches the order they appears\r
+    ; in IA32_REGS structure. This facilitates wrapper function to extract them\r
+    ; into that structure.\r
+    ;\r
+    ; Some instructions for manipulation of segment registers have to be written\r
+    ; in opcode since 64-bit MASM prevents accesses to those registers.\r
+    ;\r
     DB      16h                         ; push ss\r
     DB      0eh                         ; push cs\r
     DB      66h\r
@@ -115,7 +123,7 @@ SavedCr4    DD      ?
     ;\r
     ; rdi in the instruction below is indeed bx in 16-bit code\r
     ;\r
-    DB      66h, 2eh\r
+    DB      66h, 2eh                    ; 2eh is "cs:" segment override\r
     lgdt    fword ptr [rdi + (SavedGdt - @Base)]\r
     DB      66h\r
     mov     ecx, 0c0000080h\r
@@ -129,9 +137,8 @@ SavedCr0    DD      ?
 @64Eip      DD      ?\r
 SavedCs     DW      ?\r
 @64BitCode:\r
-    DB      48h, 0b8h                   ; mov rax, imm64\r
-SavedRip    DQ      ?\r
-    jmp     rax                         ; return to caller\r
+    mov     rsp, r8                     ; restore stack\r
+    ret\r
 _BackFromUserCode   ENDP\r
 \r
 _EntryPoint DD      _ToUserCode - m16Start\r
@@ -160,14 +167,14 @@ _ToUserCode PROC
     mov     cr4, rbp\r
     mov     ss, esi                     ; set up 16-bit stack segment\r
     mov     sp, bx                      ; set up 16-bit stack pointer\r
-    DB      66h\r
+    DB      66h                         ; make the following call 32-bit\r
     call    @Base                       ; push eip\r
 @Base:\r
     pop     bp                          ; ebp <- address of @Base\r
     push    [esp + sizeof (IA32_REGS) + 2]\r
-    lea     eax, [rsi + (@RealMode - @Base)]\r
+    lea     eax, [rsi + (@RealMode - @Base)]    ; rsi is "bp" in 16-bit code\r
     push    rax\r
-    retf\r
+    retf                                ; execution begins at next instruction\r
 @RealMode:\r
     DB      66h, 2eh                    ; CS and operand size override\r
     lidt    fword ptr [rsi + (_16Idtr - @Base)]\r
@@ -178,7 +185,7 @@ _ToUserCode PROC
     pop     gs\r
     popf                                ; popfd\r
     lea     sp, [esp + 4]               ; skip high order 32 bits of EFlags\r
-    DB      66h\r
+    DB      66h                         ; make the following retf 32-bit\r
     retf                                ; transfer control to user code\r
 _ToUserCode ENDP\r
 \r
@@ -220,8 +227,8 @@ GDT_SIZE = $ - _NullSeg
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi\r
-    mov     r10d, ds\r
-    mov     r11d, es\r
+    mov     r10d, ds                    ; r9 ~ r11 are not accessible in 16-bit\r
+    mov     r11d, es                    ; so use them for saving seg registers\r
     mov     r9d, ss\r
     push    fs\r
     push    gs\r
@@ -238,8 +245,8 @@ InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi
     lea     ecx, [rdx + (SavedCr4 - m16Start)]\r
     mov     eax, edx                    ; eax <- transition code address\r
     and     edx, 0fh\r
-    shl     eax, 12\r
-    lea     ax, [rdx + (_BackFromUserCode - m16Start)]\r
+    shl     eax, 12                     ; segment address in high order 16 bits\r
+    lea     ax, [rdx + (_BackFromUserCode - m16Start)]  ; offset address\r
     stosd                               ; [edi] <- return address of user code\r
     sgdt    fword ptr [rcx + (SavedGdt - SavedCr4)]\r
     sidt    fword ptr [rsp + 38h]       ; save IDT stack in argument space\r
@@ -257,13 +264,12 @@ InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi
     pushfq\r
     lea     edx, [rdx + DATA16 - DATA32]\r
     lea     r8, @RetFromRealMode\r
-    mov     [rcx + (SavedRip - SavedCr4)], r8\r
+    push    r8\r
     mov     r8d, cs\r
     mov     [rcx + (SavedCs - SavedCr4)], r8w\r
     mov     r8, rsp\r
     jmp     fword ptr [rcx + (_EntryPoint - SavedCr4)]\r
 @RetFromRealMode:\r
-    mov     rsp, r8\r
     popfq\r
     lidt    fword ptr [rsp + 38h]       ; restore protected mode IDTR\r
     lea     eax, [rbp - sizeof (IA32_REGS)]\r
index ad618f1089aa7dda9f64cb8681b8f1078aeb6628..188a9d4ddcd5d80e13bcc57c6750762f287dd4d3 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteDr4 PROC\r
+    ;\r
+    ; There's no obvious reason to access this register, since it's aliased to\r
+    ; DR6 when DE=0 or an exception generated when DE=1\r
+    ;\r
     DB      0fh, 23h, 0e1h\r
     mov     rax, rcx\r
     ret\r
index 91eeef20e334dd83019b0ab1d1b8f035f044e5ba..86009c32871d343cb61e728b7065c3e769d1e8ec 100644 (file)
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteDr5 PROC\r
+    ;\r
+    ; There's no obvious reason to access this register, since it's aliased to\r
+    ; DR7 when DE=0 or an exception generated when DE=1\r
+    ;\r
     DB      0fh, 23h, 0e9h\r
     mov     rax, rcx\r
     ret\r
index 0ad65e366d4957c13c6c40cf2b53d24ad1bbd116..7496362d86fc18c985bbe0af1c9cdcf41b8f5a0a 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm0 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0c1h\r
     ret\r
 AsmWriteMm0 ENDP\r
index 66425a43c24635ae8ce2fdab332974970a94212b..a00f551f8a5ea72106b21af4de0a8da062ea3528 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm1 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0c9h\r
     ret\r
 AsmWriteMm1 ENDP\r
index bd0f0a0e1adcd3c94b5e7c0c25fe51f898ac09bd..01db79007d647c78286ac488b8b4dcd7aa3fa12b 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm2 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0d1h\r
     ret\r
 AsmWriteMm2 ENDP\r
index fa36ea64bb15cd66ef4f6313c82d9ded619ebc2d..59464641eb4b8fa41c09a5c6df44b16e82f9063a 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm3 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0d9h\r
     ret\r
 AsmWriteMm3 ENDP\r
index d9bc46e16c5ec21cef11a09735885ad5dcb8b8a5..6848dbf7ec085b7495bee1f7a30b6710d9ecdc6a 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm4 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0e1h\r
     ret\r
 AsmWriteMm4 ENDP\r
index f268bea8ec8966dcbbfb43e077ede93b3dad895d..fbe44ae078682e48de89028e2346d2650c06c62c 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm5 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0e9h\r
     ret\r
 AsmWriteMm5 ENDP\r
index cb32ea6dc384581cdefe7a59d5410a5aef6a4b4a..cd5938828d8150720d3a9f1b676c39eaafef6ecf 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm6 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0f1h\r
     ret\r
 AsmWriteMm6 ENDP\r
index f5713823bb91ce5d6f2a1781e4abed7d16863f74..9636a4cd8d14178891107f61dc9cfd4c44a1a422 100644 (file)
@@ -31,6 +31,9 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMm7 PROC\r
+    ;\r
+    ; 64-bit MASM doesn't support MMX instructions, so use opcode here\r
+    ;\r
     DB      48h, 0fh, 6eh, 0f9h\r
     ret\r
 AsmWriteMm7 ENDP\r
index 6b8066f922ccef426aa2d188216122bf92582be5..b7704b81853048f47c9340f71d0a3be058719618 100644 (file)
@@ -32,8 +32,8 @@
 ;   );\r
 ;------------------------------------------------------------------------------\r
 AsmWriteMsr64   PROC\r
-    mov     rax, rdx\r
-    shr     rdx, 20h\r
+    mov     rax, rdx                    ; meanwhile, rax <- return value\r
+    shr     rdx, 20h                    ; edx:eax contains the value to write\r
     wrmsr\r
     ret\r
 AsmWriteMsr64   ENDP\r