http://72.14.235.104/search?q=cache:YkaP7V6pxroJ:https://www.x86-64.org/pipermail/discuss/2000-October/001009.html+swapgs&hl=ko&ct=clnk&cd=1&gl=kr
x86-64 Spec addition - SwapGS instruction
Kevin McGrath kevin.mcgrath at amd.com
Wed Oct 25 02:19:45 CEST 2000
Previous message: [discuss] Encoding of 64bit moves from sign extended immediat e to register
Next message: [discuss] x86-64 Spec addition - SwapGS instruction
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--------------------------------------------------------------------------------
A new instruction (SwapGS) is described below. It is designed to be used by an
OS kernel. It provides a method for the kernel to obtain a pointer to kernel data
structures in 64-bit mode.
SwapGS exchanges the kernel data structure pointer from the KernelGSbase MSR with the
GS base register. The kernel can then use the GS prefix on normal memory references
to access the kernel data structures.
The need for SwapGS arises from the requirement that, upon entry to the OS kernel,
the kernel needs to get a 64-bit pointer to its key data structures.
When using SYSCALL to implement system calls, no kernel stack exists at the OS
entry point. Neither is there a straightforward method to obtain a pointer to kernel
structures, from which the kernel stack pointer could be read. Thus, the kernel
can't save GPRs or reference memory. By design, SwapGS does not require any GPR
or memory operands, therefore no registers need to be saved before using it.
Similarly, when the OS kernel is entered via an interrupt or exception (and the kernel
stack is already set up), SwapGS can be used to quickly get a pointer to the kernel
data structures.
This following description will be included in the next spec update.
Kevin McGrath
Senior Member Technical Staff
Advanced Micro Devices
Description
-----------
SwapGS - 0f 01 /7, MOD =11b, r/m =000b
Swaps the 64-bit 'KernelGSbase' MSR with the 64-bit GS segment base.
The SwapGS instruction is a privileged instruction intended for use by system software.
It is used upon entry to the kernel (via SYSCALL, interrupt or exceptions)
to obtain a pointer to kernel data structures.
SwapGS exchanges the kernel data structure pointer from the KernelGSbase MSR with the
GS base register. The kernel can then use the GS prefix on normal memory references
to access the kernel data structures.
SwapGS is a serializing instruction.
Operation
---------
if mode <> 64 then #UD;
if CPL <> 0 then #GP (0);
temp = GS base;
GS base = MSR_KernelGSbase;
MSR_KernelGSbase = temp;
Example usage
-------------
At a kernel entry point the OS can use SwapGS to obtain a pointer to kernel
data structures and simultaneously save the user's GS base. Upon exit it
can use SwapGS to restore the user's GS base:
SystemCallEntryPoint:
SwapGS ; set up kernel pointer, save user's GS base
mov gs:[SavedUserRSP], rsp ; save user's stack pointer
mov rsp, gs:[KernelStackPtr] ; set up kernel stack
push rax ; now that we have a stack, save user's GPRs
mov rax, gs:[CPUnumber] ; get CPU number < or whatever >
. ; perform system service
.
SwapGS ; restore user's GS, save kernel pointer
The KernelGSbase MSR itself is only accessible via the normal RDMSR/WRMSR
instructions. Those instructions are privileged so KernelGSbase MSR is only
readable/writable by the OS. WRMSR will cause a #GP(0) if the value written into
KernelGSbase is not canonical; the SwapGS instruction itself does not perform a
canonical check.
The KernelGSbase is MSR C000_0102h.
Exceptions
----------
Protected Mode:
#UD if mode <> 64-bit mode
#GP(0) if CPL <> 0
Real Mode:
#UD always, since mode <> 64-bit mode
Virtual Mode
#UD always, since mode <> 64-bit mode
Note on instruction encoding
----------------------------
SwapGS uses a previously unused (and illegal) MODRM encoding
of the 0f 01 /7 opcode. Previously, only the memory forms (mod <> 11)
of this opcode were legal and encoded the INVLPG instruction. The
register forms (mod=11) were illegal. In 64-bit mode, 1 of the 8 register
forms are used for SwapGS. The other encodings remain illegal and may be
used in the future.
OPCODE MODRM Instruction
bytes mod reg r/m legacy 64-bit mode
-------------------------------------------
OF 01 !11 111 xxx INVLPG INVLPG
OF 01 11 111 000 #UD SwapGS
OF 01 11 111 001 #UD #UD
OF 01 11 111 010 #UD #UD
OF 01 11 111 011 #UD #UD
OF 01 11 111 100 #UD #UD
OF 01 11 111 101 #UD #UD
OF 01 11 111 110 #UD #UD
OF 01 11 111 111 #UD #UD
x86-64 Spec addition - SwapGS instruction
Kevin McGrath kevin.mcgrath at amd.com
Wed Oct 25 02:19:45 CEST 2000
Previous message: [discuss] Encoding of 64bit moves from sign extended immediat e to register
Next message: [discuss] x86-64 Spec addition - SwapGS instruction
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
--------------------------------------------------------------------------------
A new instruction (SwapGS) is described below. It is designed to be used by an
OS kernel. It provides a method for the kernel to obtain a pointer to kernel data
structures in 64-bit mode.
SwapGS exchanges the kernel data structure pointer from the KernelGSbase MSR with the
GS base register. The kernel can then use the GS prefix on normal memory references
to access the kernel data structures.
The need for SwapGS arises from the requirement that, upon entry to the OS kernel,
the kernel needs to get a 64-bit pointer to its key data structures.
When using SYSCALL to implement system calls, no kernel stack exists at the OS
entry point. Neither is there a straightforward method to obtain a pointer to kernel
structures, from which the kernel stack pointer could be read. Thus, the kernel
can't save GPRs or reference memory. By design, SwapGS does not require any GPR
or memory operands, therefore no registers need to be saved before using it.
Similarly, when the OS kernel is entered via an interrupt or exception (and the kernel
stack is already set up), SwapGS can be used to quickly get a pointer to the kernel
data structures.
This following description will be included in the next spec update.
Kevin McGrath
Senior Member Technical Staff
Advanced Micro Devices
Description
-----------
SwapGS - 0f 01 /7, MOD =11b, r/m =000b
Swaps the 64-bit 'KernelGSbase' MSR with the 64-bit GS segment base.
The SwapGS instruction is a privileged instruction intended for use by system software.
It is used upon entry to the kernel (via SYSCALL, interrupt or exceptions)
to obtain a pointer to kernel data structures.
SwapGS exchanges the kernel data structure pointer from the KernelGSbase MSR with the
GS base register. The kernel can then use the GS prefix on normal memory references
to access the kernel data structures.
SwapGS is a serializing instruction.
Operation
---------
if mode <> 64 then #UD;
if CPL <> 0 then #GP (0);
temp = GS base;
GS base = MSR_KernelGSbase;
MSR_KernelGSbase = temp;
Example usage
-------------
At a kernel entry point the OS can use SwapGS to obtain a pointer to kernel
data structures and simultaneously save the user's GS base. Upon exit it
can use SwapGS to restore the user's GS base:
SystemCallEntryPoint:
SwapGS ; set up kernel pointer, save user's GS base
mov gs:[SavedUserRSP], rsp ; save user's stack pointer
mov rsp, gs:[KernelStackPtr] ; set up kernel stack
push rax ; now that we have a stack, save user's GPRs
mov rax, gs:[CPUnumber] ; get CPU number < or whatever >
. ; perform system service
.
SwapGS ; restore user's GS, save kernel pointer
The KernelGSbase MSR itself is only accessible via the normal RDMSR/WRMSR
instructions. Those instructions are privileged so KernelGSbase MSR is only
readable/writable by the OS. WRMSR will cause a #GP(0) if the value written into
KernelGSbase is not canonical; the SwapGS instruction itself does not perform a
canonical check.
The KernelGSbase is MSR C000_0102h.
Exceptions
----------
Protected Mode:
#UD if mode <> 64-bit mode
#GP(0) if CPL <> 0
Real Mode:
#UD always, since mode <> 64-bit mode
Virtual Mode
#UD always, since mode <> 64-bit mode
Note on instruction encoding
----------------------------
SwapGS uses a previously unused (and illegal) MODRM encoding
of the 0f 01 /7 opcode. Previously, only the memory forms (mod <> 11)
of this opcode were legal and encoded the INVLPG instruction. The
register forms (mod=11) were illegal. In 64-bit mode, 1 of the 8 register
forms are used for SwapGS. The other encodings remain illegal and may be
used in the future.
OPCODE MODRM Instruction
bytes mod reg r/m legacy 64-bit mode
-------------------------------------------
OF 01 !11 111 xxx INVLPG INVLPG
OF 01 11 111 000 #UD SwapGS
OF 01 11 111 001 #UD #UD
OF 01 11 111 010 #UD #UD
OF 01 11 111 011 #UD #UD
OF 01 11 111 100 #UD #UD
OF 01 11 111 101 #UD #UD
OF 01 11 111 110 #UD #UD
OF 01 11 111 111 #UD #UD
.
그래서 swapgs 인스트럭션이 필요하다.
swapgs 인스트럭션은 MSR_KERNEL_GS_BASE 레지스터와 GS 레지스터 값을 서로 바꾸어 준다.
이 인스트럭션은 어떤 레지스터나 메모리 주소도 피연산자로 취하지 않는다.