shll $12,%eax # segment address in high order 16 bits\r
lea (_BackFromUserCode - ASM_PFX(m16Start))(%rdx), %ax\r
stosl # [edi] <- return address of user code\r
- sgdt (SavedGdt - SavedCr4)(%rcx) \r
+ sgdt 0x60(%rsp) # save GDT stack in argument space\r
+ movzwq 0x60(%rsp), %r10 # r10 <- GDT limit \r
+ lea ((ASM_PFX(InternalAsmThunk16) - SavedCr4) + 0xf)(%rcx), %r11 \r
+ andl $0xfffffff0, r11 # r11 <- 16-byte aligned shadowed GDT table in real mode buffer \r
+ \r
+ movw %r10w, (SavedGdt - SavedCr4)(%rcx) # save the limit of shadowed GDT table\r
+ movq %r11, (SavedGdt - SavedCr4 + 0x2)(%rcx) # save the base address of shadowed GDT table\r
+ \r
+ movq 0x62(%rsp) ,%rsi # rsi <- the original GDT base address\r
+ xchg %r10, %rcx # save rcx to r10 and initialize rcx to be the limit of GDT table \r
+ incq %rcx # rcx <- the size of memory to copy\r
+ xchg %r11, %rdi # save rdi to r11 and initialize rdi to the base address of shadowed GDT table\r
+ rep\r
+ movsb # perform memory copy to shadow GDT table\r
+ movq %r10, %rcx # restore the orignal rcx before memory copy\r
+ movq %r11, %rdi # restore the original rdi before memory copy\r
+ \r
sidt 0x50(%rsp)\r
movq %cr0, %rax\r
movl %eax, (SavedCr0 - SavedCr4)(%rcx)\r
.byte _EntryPoint - SavedCr4\r
L_RetFromRealMode: \r
popfq\r
- lidt 0x50(%rsp)\r
+ lgdt 0x60(%rsp) # restore protected mode GDTR\r
+ lidt 0x50(%rsp) # restore protected mode IDTR\r
lea -IA32_REGS_SIZE(%rbp), %eax\r
.byte 0x0f, 0xa9 # pop gs\r
.byte 0x0f, 0xa1 # pop fs\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
+ \r
+ sgdt fword ptr [rsp + 60h] ; save GDT stack in argument space\r
+ movzx r10, word ptr [rsp + 60h] ; r10 <- GDT limit \r
+ lea r11, [rcx + (InternalAsmThunk16 - SavedCr4) + 0xf]\r
+ and r11, 0xfffffff0 ; r11 <- 16-byte aligned shadowed GDT table in real mode buffer\r
+ \r
+ mov word ptr [rcx + (SavedGdt - SavedCr4)], r10w ; save the limit of shadowed GDT table\r
+ mov qword ptr [rcx + (SavedGdt - SavedCr4) + 2], r11 ; save the base address of shadowed GDT table\r
+ \r
+ mov rsi, qword ptr [rsp + 62h] ; rsi <- the original GDT base address\r
+ xchg rcx, r10 ; save rcx to r10 and initialize rcx to be the limit of GDT table\r
+ inc rcx ; rcx <- the size of memory to copy\r
+ xchg rdi, r11 ; save rdi to r11 and initialize rdi to the base address of shadowed GDT table\r
+ rep movsb ; perform memory copy to shadow GDT table\r
+ mov rcx, r10 ; restore the orignal rcx before memory copy\r
+ mov rdi, r11 ; restore the original rdi before memory copy\r
+ \r
sidt fword ptr [rsp + 50h] ; save IDT stack in argument space\r
mov rax, cr0\r
mov [rcx + (SavedCr0 - SavedCr4)], eax\r
jmp fword ptr [rcx + (_EntryPoint - SavedCr4)]\r
@RetFromRealMode:\r
popfq\r
+ lgdt fword ptr [rsp + 60h] ; restore protected mode GDTR\r
lidt fword ptr [rsp + 50h] ; restore protected mode IDTR\r
lea eax, [rbp - sizeof (IA32_REGS)]\r
pop gs\r