flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > save xmm + r9 r10 r8 in kernel for timing

Author
Thread Post new topic Reply to topic
zkykernel



Joined: 29 May 2024
Posts: 3
zkykernel 29 May 2024, 18:59
hello im trying to fix reading around my timing in kernel the problem is xmm needing saving because context switches i read like this but its still cause overflow issues i only need save xmm and not destroy r8 r9 r10 i think to fix it i read like this extern "C" double GetTiming (unsigned __int64 frequency); where frequency load from mov r10, rcx

Code:
 INCLUDELIB ntoskrnl

CONST SEGMENT
    __real@bff0000000000000 DQ 0bff0000000000000r ; -1.0
CONST ENDS

_TEXT SEGMENT
PUBLIC GetTiming

GetTiming PROC
     sub rsp, 70h
     movups [rsp+30h], xmm0
     movups [rsp+40h], xmm1
     movups [rsp+50h], xmm2
     push r10
     push r9
     push r8

    ; Load frequency into r10
    mov r10, rcx

    ; Check if frequency is zero
    test rcx, rcx
    jne SHORT not_zero
    movsd xmm0, QWORD PTR __real@bff0000000000000
    ret

not_zero:
    ; Get high-precision time using RDTSC
    rdtsc
    shl rdx, 32
    or rax, rdx

    ; Compute time_s and time_fract
    xor edx, edx
    div r10
    mov r9, rdx             ; Store time_fract in r9
    mov r8, rax             ; Store time_s in r8

    ; Perform intermediate calculations
    mov rax, -8633038790181359431 
    imul r8
    add rdx, r8
    sar rdx, 24
    mov rcx, rdx
    shr rcx, 63
    add rdx, rcx
    imul rax, rdx, 31536000
    sub r8, rax



    ; Perform the floating-point operation
    xorps xmm0, xmm0
    cvtsi2sd xmm0, r8
    xorps xmm2, xmm2
    cvtsi2sd xmm2, r9
    xorps xmm1, xmm1
    cvtsi2sd xmm1, r10
    divsd xmm2, xmm1
    addsd xmm0, xmm2

    pop r8
    pop r9
    pop r10
    movups xmm0, [rsp+30h]
    movups xmm1, [rsp+40h]
    movups xmm2, [rsp+50h]
    add rsp, 70h


cleanup:
    ret
GetTiming ENDP

_TEXT ENDS

END    
any help will be greatful thanks !


Last edited by zkykernel on 31 May 2024, 04:42; edited 3 times in total
Post 29 May 2024, 18:59
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 921
Location: Russia
macomics 30 May 2024, 02:26
It is correct
Post 30 May 2024, 02:26
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20207
Location: In your JS exploiting you and your system
revolution 30 May 2024, 03:05
If the code is a call target then the pushfq will re-align the stack, so it will work fine.

But if the code is used inline then this will crash due to the stack being unaligned. You can fix that by changing movaps to movups.. Then it will work in all situations.


Last edited by revolution on 30 May 2024, 04:48; edited 2 times in total
Post 30 May 2024, 03:05
View user's profile Send private message Visit poster's website Reply with quote
zkykernel



Joined: 29 May 2024
Posts: 3
zkykernel 30 May 2024, 03:38
revolution wrote:
If the code is a call target then the pushfd will re-align the stack, so it will work fine.

But if the code is used inline then this will crash due to the stack being unaligned. You can fix that by changing movaps to movups.. Then it will work in all situations.


i just need to save all xmm not call target i will change to movups thanks !
Post 30 May 2024, 03:38
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 921
Location: Russia
macomics 30 May 2024, 03:55
revolution wrote:
If the code is a call target then the pushfd will re-align the stack, so it will work fine.
It's not pushfd, it's pushfq.
Post 30 May 2024, 03:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20207
Location: In your JS exploiting you and your system
revolution 30 May 2024, 04:48
macomics wrote:
revolution wrote:
If the code is a call target then the pushfd will re-align the stack, so it will work fine.
It's not pushfd, it's pushfq.
Thanks. I fixed it.
Post 30 May 2024, 04:48
View user's profile Send private message Visit poster's website Reply with quote
zkykernel



Joined: 29 May 2024
Posts: 3
zkykernel 31 May 2024, 04:35
seems change to movups not enough
Post 31 May 2024, 04:35
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.