سؤال

I'm a CS student writing in Intel x86-64 assembly, compiling with nasm, and running on an Core i7 processor with Ubuntu 12.04 as the guest OS. Does anyone have an example of how to use XSAVE and XRSTOR? I've read the section on XSAVE in Intel Architectures Software Developers manual several times. I tried to implement xsave in C++ and then disassemble the binary to get an understanding of what it's doing. And of course I've scoured the Internet for examples. Any suggestions would be much obliged.

هل كانت مفيدة؟

المحلول

Finally, an answer to this question. Thanks to user: harold who helped answer the question for me. A summary of what I've found:

Set up a memory space in .data and align it on a 64-byte boundary. Then you can use the commands with that memory space. If you want to use the stack, you should be able to do so similarly ensuring that the stack is 64-byte aligned, but this way seems easier to me for this purpose.

eax: edx is used to set the flags of which registers you WANT to save, restore. This combined is 64-bits and is ANDed with an internal control which knows which registers you CAN save/restore (this allows processors that don't have ymm for example to ignore those registers) I find it easiest to just set all bits on and save / restore everything:

segment .data

align   64
regsave times 1024 dq 0

segment .text

mov     rdx, 0xFFFFFFFFFFFFFFFF
mov     rax, 0xFFFFFFFFFFFFFFFF
xsave   [regsave]

vzeroall

mov     rdx, 0xFFFFFFFFFFFFFFFF
mov     rax, 0xFFFFFFFFFFFFFFFF
xrstor  [regsave]

نصائح أخرى

The xsave/xrstor/xsaveopt instructions are used to perform a full save/restore of the extended state in the processor to/from memory. Similar to fxsave/fxrstor, it saves/restores fpu state st[0..7], xmm[0..7], mxcsr, etc... in addition to supporting ymm[0..15] and future extensions (zmm[0..31]). The actual values saved, and the data layout are enumerated via the relevant cpuid leaves. The use is generally operating system context switching. The programmer reference describes how to use them correctly.

For general userspace register save/restore, the assembler usually has a facility for saving/restoring a set of registers.

For example...

masm

foo PROC USES eax,ebx,ecx
    xor    ebx, ebx

loop:
    mov    eax, [esi + ebx*4]
    mov    [edi + ebx*4], eax
    inc    ebx
    dec    ecx

    jnz    loop

    ret

foo ENDP

yasm

%macro  mpush 1-*

  %rep  %0
        push    %1
  %rotate 1
  %endrep

%endmacro

%macro  mpop 1-*

  %rep %0
  %rotate -1
        pop     %1
  %endrep

%endmacro


foo:
    mpush  rax,rbx,rcx
    xor    rbx, rbx

loop:
    mov    rax, [rsi + rbx*8]
    mov    [rdi + rbx*8], rax
    inc    rbx
    dec    rcx

    jnz    loop

    mpop   rax,rbx,rcx
    ret

In ia-32, there is a pushad to save all the general purpose registers, but with amd64 you need to have corresponding push/pop pairs for each of the registers you use.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top