system_call 의 swapgs 인스트럭션에 대하여

지현구 2007.09.16 04:59 조회 수 : 8438 추천:112

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




번호 제목 글쓴이 날짜 조회 수
공지 [공지] 커널 스터디 관련 Q&A 게시판 입니다. [5] woos 2016.04.09 2197
1585 Linux Device Model [2] file 김민호 2011.05.22 8459
» system_call 의 swapgs 인스트럭션에 대하여 [2] 지현구 2007.09.16 8438
1583 fork()와 vfork()의 차이점과 COW(Copy On Write) [1] 아폴로 2013.05.02 8340
1582 스핀락에 대한 나름대로의 정리와 3가지 질문.. [19] 이종인 2011.04.25 8335
1581 이번주 논의 사항 [2] 남현우 2008.05.10 8202
1580 죄송합니다. [1] 송형주 2008.05.13 8106
1579 스터디 장소 예약 했습니다. [13] 장병남 2010.04.05 8067
1578 [투표]이번주 모임 장소 투표해주세요. [15] 나영찬 2010.04.05 8014
1577 init_task의 thread info의 cpu값 설정. [8] 유경환 2011.10.21 8010
1576 ARM TCM Tightly Couple Memory a 2010.04.10 7954
1575 어제 sched_init()을 끝냈습니다. [1] 윤석훈 2010.08.22 7921
1574 [의미] Memory Barrier [10] 맥주 2007.12.17 7890
1573 ARMv8 아키텍쳐 관련 읽을거리 [3] K 2013.05.06 7829
1572 namespace file 유강희 2011.06.10 7824
1571 어제는 모두 잘 들어가셨나요'ㅅ' ㅎㅎㅎ [3] file 조은지 2010.04.11 7710
1570 2012.4.14 스터디 [6] 이한울 2012.04.14 7695
1569 smdk2440보드 회로도. [3] 김희근 2008.12.17 7690
1568 4/3 ARM C Study group 회의 내용 [10] file 강효민 2010.04.03 7670
1567 이제 start_kernel()에 함수가 38개밖에 안남았네요.. [4] 윤석훈 2010.11.30 7634
1566 리눅스 커널 심층분석 (4장 ~9장) 요약부분 [1] file 박재성 2011.06.03 7631
XE Login