kernel-stacks 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Most of the text from Keith Owens, hacked by AK
  2. x86_64 page size (PAGE_SIZE) is 4K.
  3. Like all other architectures, x86_64 has a kernel stack for every
  4. active thread. These thread stacks are THREAD_SIZE (2*PAGE_SIZE) big.
  5. These stacks contain useful data as long as a thread is alive or a
  6. zombie. While the thread is in user space the kernel stack is empty
  7. except for the thread_info structure at the bottom.
  8. In addition to the per thread stacks, there are specialized stacks
  9. associated with each cpu. These stacks are only used while the kernel
  10. is in control on that cpu, when a cpu returns to user space the
  11. specialized stacks contain no useful data. The main cpu stacks is
  12. * Interrupt stack. IRQSTACKSIZE
  13. Used for external hardware interrupts. If this is the first external
  14. hardware interrupt (i.e. not a nested hardware interrupt) then the
  15. kernel switches from the current task to the interrupt stack. Like
  16. the split thread and interrupt stacks on i386 (with CONFIG_4KSTACKS),
  17. this gives more room for kernel interrupt processing without having
  18. to increase the size of every per thread stack.
  19. The interrupt stack is also used when processing a softirq.
  20. Switching to the kernel interrupt stack is done by software based on a
  21. per CPU interrupt nest counter. This is needed because x86-64 "IST"
  22. hardware stacks cannot nest without races.
  23. x86_64 also has a feature which is not available on i386, the ability
  24. to automatically switch to a new stack for designated events such as
  25. double fault or NMI, which makes it easier to handle these unusual
  26. events on x86_64. This feature is called the Interrupt Stack Table
  27. (IST). There can be up to 7 IST entries per cpu. The IST code is an
  28. index into the Task State Segment (TSS), the IST entries in the TSS
  29. point to dedicated stacks, each stack can be a different size.
  30. An IST is selected by an non-zero value in the IST field of an
  31. interrupt-gate descriptor. When an interrupt occurs and the hardware
  32. loads such a descriptor, the hardware automatically sets the new stack
  33. pointer based on the IST value, then invokes the interrupt handler. If
  34. software wants to allow nested IST interrupts then the handler must
  35. adjust the IST values on entry to and exit from the interrupt handler.
  36. (this is occasionally done, e.g. for debug exceptions)
  37. Events with different IST codes (i.e. with different stacks) can be
  38. nested. For example, a debug interrupt can safely be interrupted by an
  39. NMI. arch/x86_64/kernel/entry.S::paranoidentry adjusts the stack
  40. pointers on entry to and exit from all IST events, in theory allowing
  41. IST events with the same code to be nested. However in most cases, the
  42. stack size allocated to an IST assumes no nesting for the same code.
  43. If that assumption is ever broken then the stacks will become corrupt.
  44. The currently assigned IST stacks are :-
  45. * STACKFAULT_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
  46. Used for interrupt 12 - Stack Fault Exception (#SS).
  47. This allows to recover from invalid stack segments. Rarely
  48. happens.
  49. * DOUBLEFAULT_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
  50. Used for interrupt 8 - Double Fault Exception (#DF).
  51. Invoked when handling a exception causes another exception. Happens
  52. when the kernel is very confused (e.g. kernel stack pointer corrupt)
  53. Using a separate stack allows to recover from it well enough in many
  54. cases to still output an oops.
  55. * NMI_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
  56. Used for non-maskable interrupts (NMI).
  57. NMI can be delivered at any time, including when the kernel is in the
  58. middle of switching stacks. Using IST for NMI events avoids making
  59. assumptions about the previous state of the kernel stack.
  60. * DEBUG_STACK. DEBUG_STKSZ
  61. Used for hardware debug interrupts (interrupt 1) and for software
  62. debug interrupts (INT3).
  63. When debugging a kernel, debug interrupts (both hardware and
  64. software) can occur at any time. Using IST for these interrupts
  65. avoids making assumptions about the previous state of the kernel
  66. stack.
  67. * MCE_STACK. EXCEPTION_STKSZ (PAGE_SIZE).
  68. Used for interrupt 18 - Machine Check Exception (#MC).
  69. MCE can be delivered at any time, including when the kernel is in the
  70. middle of switching stacks. Using IST for MCE events avoids making
  71. assumptions about the previous state of the kernel stack.
  72. For more details see the Intel IA32 or AMD AMD64 architecture manuals.