entry_64.txt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. This file documents some of the kernel entries in
  2. arch/x86/entry/entry_64.S. A lot of this explanation is adapted from
  3. an email from Ingo Molnar:
  4. http://lkml.kernel.org/r/<20110529191055.GC9835%40elte.hu>
  5. The x86 architecture has quite a few different ways to jump into
  6. kernel code. Most of these entry points are registered in
  7. arch/x86/kernel/traps.c and implemented in arch/x86/entry/entry_64.S
  8. for 64-bit, arch/x86/entry/entry_32.S for 32-bit and finally
  9. arch/x86/entry/entry_64_compat.S which implements the 32-bit compatibility
  10. syscall entry points and thus provides for 32-bit processes the
  11. ability to execute syscalls when running on 64-bit kernels.
  12. The IDT vector assignments are listed in arch/x86/include/asm/irq_vectors.h.
  13. Some of these entries are:
  14. - system_call: syscall instruction from 64-bit code.
  15. - entry_INT80_compat: int 0x80 from 32-bit or 64-bit code; compat syscall
  16. either way.
  17. - entry_INT80_compat, ia32_sysenter: syscall and sysenter from 32-bit
  18. code
  19. - interrupt: An array of entries. Every IDT vector that doesn't
  20. explicitly point somewhere else gets set to the corresponding
  21. value in interrupts. These point to a whole array of
  22. magically-generated functions that make their way to do_IRQ with
  23. the interrupt number as a parameter.
  24. - APIC interrupts: Various special-purpose interrupts for things
  25. like TLB shootdown.
  26. - Architecturally-defined exceptions like divide_error.
  27. There are a few complexities here. The different x86-64 entries
  28. have different calling conventions. The syscall and sysenter
  29. instructions have their own peculiar calling conventions. Some of
  30. the IDT entries push an error code onto the stack; others don't.
  31. IDT entries using the IST alternative stack mechanism need their own
  32. magic to get the stack frames right. (You can find some
  33. documentation in the AMD APM, Volume 2, Chapter 8 and the Intel SDM,
  34. Volume 3, Chapter 6.)
  35. Dealing with the swapgs instruction is especially tricky. Swapgs
  36. toggles whether gs is the kernel gs or the user gs. The swapgs
  37. instruction is rather fragile: it must nest perfectly and only in
  38. single depth, it should only be used if entering from user mode to
  39. kernel mode and then when returning to user-space, and precisely
  40. so. If we mess that up even slightly, we crash.
  41. So when we have a secondary entry, already in kernel mode, we *must
  42. not* use SWAPGS blindly - nor must we forget doing a SWAPGS when it's
  43. not switched/swapped yet.
  44. Now, there's a secondary complication: there's a cheap way to test
  45. which mode the CPU is in and an expensive way.
  46. The cheap way is to pick this info off the entry frame on the kernel
  47. stack, from the CS of the ptregs area of the kernel stack:
  48. xorl %ebx,%ebx
  49. testl $3,CS+8(%rsp)
  50. je error_kernelspace
  51. SWAPGS
  52. The expensive (paranoid) way is to read back the MSR_GS_BASE value
  53. (which is what SWAPGS modifies):
  54. movl $1,%ebx
  55. movl $MSR_GS_BASE,%ecx
  56. rdmsr
  57. testl %edx,%edx
  58. js 1f /* negative -> in kernel */
  59. SWAPGS
  60. xorl %ebx,%ebx
  61. 1: ret
  62. If we are at an interrupt or user-trap/gate-alike boundary then we can
  63. use the faster check: the stack will be a reliable indicator of
  64. whether SWAPGS was already done: if we see that we are a secondary
  65. entry interrupting kernel mode execution, then we know that the GS
  66. base has already been switched. If it says that we interrupted
  67. user-space execution then we must do the SWAPGS.
  68. But if we are in an NMI/MCE/DEBUG/whatever super-atomic entry context,
  69. which might have triggered right after a normal entry wrote CS to the
  70. stack but before we executed SWAPGS, then the only safe way to check
  71. for GS is the slower method: the RDMSR.
  72. Therefore, super-atomic entries (except NMI, which is handled separately)
  73. must use idtentry with paranoid=1 to handle gsbase correctly. This
  74. triggers three main behavior changes:
  75. - Interrupt entry will use the slower gsbase check.
  76. - Interrupt entry from user mode will switch off the IST stack.
  77. - Interrupt exit to kernel mode will not attempt to reschedule.
  78. We try to only use IST entries and the paranoid entry code for vectors
  79. that absolutely need the more expensive check for the GS base - and we
  80. generate all 'normal' entry points with the regular (faster) paranoid=0
  81. variant.