Skip to content

Commit 4e3c052

Browse files
committed
[RTL/x64] Fix RtlCaptureContext/RtlpRestoreContextInternal in kernel mode
According to tests, legacy fp state is not saved in kernel mode. Also add an int 2c to the path that changes cs, as it should not be used and probably never will be.
1 parent 467efcb commit 4e3c052

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

sdk/lib/rtl/amd64/except_asm.S

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,20 @@ PUBLIC RtlCaptureContext
9494
movaps [rcx + CxXmm14], xmm14
9595
movaps [rcx + CxXmm15], xmm15
9696

97+
/* Store eflags */
98+
mov [rcx + CxEFlags], eax
99+
100+
/* Store mxcsr */
101+
stmxcsr [rcx + CxMxCsr]
102+
103+
/* Check if we are in user mode */
104+
test byte ptr [rcx + CxSegCs], 3
105+
jz RtlCaptureContextExit
106+
97107
/* Store legacy floating point registers */
98108
fxsave [rcx + CxFltSave]
99-
stmxcsr [rcx + CxMxCsr]
100109

101-
/* Store rflags */
102-
mov [rcx + CxEFlags], eax
110+
RtlCaptureContextExit:
103111

104112
/* Cleanup stack and return */
105113
add rsp, 8
@@ -119,10 +127,6 @@ PUBLIC RtlpRestoreContextInternal
119127
.ALLOCSTACK 8
120128
.ENDPROLOG
121129

122-
/* Restore legacy floating point registers (It is slow, so do it first) */
123-
ldmxcsr [rcx + CxMxCsr]
124-
fxrstor [rcx + CxFltSave]
125-
126130
/* Load the target stack pointer into rdx */
127131
mov rdx, [rcx + CxRsp]
128132

@@ -185,6 +189,18 @@ PUBLIC RtlpRestoreContextInternal
185189
mov r8, [rcx + CxR8]
186190
mov r9, [rcx + CxR9]
187191

192+
/* Restore MXCSR */
193+
ldmxcsr [rcx + CxMxCsr]
194+
195+
/* Check if we go to user mode */
196+
test byte ptr [rcx + CxSegCs], 3
197+
jz Exit
198+
199+
/* Restore legacy floating point registers */
200+
fxrstor [rcx + CxFltSave]
201+
202+
Exit:
203+
188204
/* Check if we go to a different cs */
189205
mov ax, cs
190206
cmp [rcx + CxSegCs], ax
@@ -208,6 +224,9 @@ PUBLIC RtlpRestoreContextInternal
208224

209225
ReturnFar:
210226

227+
// We should never need this path
228+
int HEX(2c)
229+
211230
/* Put cs on the stack for the far return */
212231
mov ax, [rcx + CxSegCs]
213232
mov [rdx - 1 * 8], ax

0 commit comments

Comments
 (0)