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 2198
325 init_IRQ 정리 일부.. [1] 박경태 2007.11.11 5509
324 일단 D모사 회의실 2시 변경사항 리플달아 주세요. 유상민 2007.11.10 3286
323 토요일 못갈것 같습니다. [3] 유상민 2007.11.02 4322
322 주석이 깨져요 ㅜ.ㅜ [9] 송형주 2007.10.29 3968
321 3기 x86팀 이번 주 스터디는 쉬어야 할 것 같습니다 [2] 지현구 2007.10.27 4042
320 제가 요즘.. 맥주 2007.10.27 3307
319 4기 모집도 됐는데 언제 총단합회 한 번 해야 하지 않나요? [8] 지현구 2007.10.25 3789
318 리눅스 커널의 이해 [2] file 백창우 2007.10.24 8480
317 RCU 정리.. 박경태 2007.10.21 6823
316 10월 20일 스터디 장소 및 시각 (미리 써둡니다. 13일에 못오신 분들 참고하세요.) 유상민 2007.10.16 3505
315 x86 calling convention [3] 백창우 2007.10.16 5316
314 스터디 장소 시간 논의 [1] 유상민 2007.10.10 3837
313 스터디 관련하여 제안하나 할까 합니다. [1] 선준규 2007.10.08 3792
312 정리하면... [1] 지현구 2007.09.29 3753
311 [질문] 이번 주 SUV 강의 있나요? [7] 지현구 2007.09.27 3682
310 모두다 즐거운 추석 되세요. 유상민 2007.09.19 3670
309 이번 토요일(9/22)은 쉬어요~ 추석 잘 보내세요 :D (본문내용무) [6] 지현구 2007.09.19 3514
» system_call 의 swapgs 인스트럭션에 대하여 [2] 지현구 2007.09.16 8438
307 이번주도 못갈꺼 같습니다... 남용우 2007.09.15 3469
306 이번 주 스터디는 어디에서??? [3] 지현구 2007.09.15 3859
XE Login