kernel-stacks.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============
  3. Kernel Stacks
  4. =============
  5. Kernel stacks on x86-64 bit
  6. ===========================
  7. Most of the text from Keith Owens, hacked by AK
  8. x86_64 page size (PAGE_SIZE) is 4K.
  9. Like all other architectures, x86_64 has a kernel stack for every
  10. active thread. These thread stacks are THREAD_SIZE (2*PAGE_SIZE) big.
  11. These stacks contain useful data as long as a thread is alive or a
  12. zombie. While the thread is in user space the kernel stack is empty
  13. except for the thread_info structure at the bottom.
  14. In addition to the per thread stacks, there are specialized stacks
  15. associated with each CPU. These stacks are only used while the kernel
  16. is in control on that CPU; when a CPU returns to user space the
  17. specialized stacks contain no useful data. The main CPU stacks are:
  18. * Interrupt stack. IRQ_STACK_SIZE
  19. Used for external hardware interrupts. If this is the first external
  20. hardware interrupt (i.e. not a nested hardware interrupt) then the
  21. kernel switches from the current task to the interrupt stack. Like
  22. the split thread and interrupt stacks on i386, this gives more room
  23. for kernel interrupt processing without having to increase the size
  24. of every per thread stack.
  25. The interrupt stack is also used when processing a softirq.
  26. Switching to the kernel interrupt stack is done by software based on a
  27. per CPU interrupt nest counter. This is needed because x86-64 "IST"
  28. hardware stacks cannot nest without races.
  29. x86_64 also has a feature which is not available on i386, the ability
  30. to automatically switch to a new stack for designated events such as
  31. double fault or NMI, which makes it easier to handle these unusual
  32. events on x86_64. This feature is called the Interrupt Stack Table
  33. (IST). There can be up to 7 IST entries per CPU. The IST code is an
  34. index into the Task State Segment (TSS). The IST entries in the TSS
  35. point to dedicated stacks; each stack can be a different size.
  36. An IST is selected by a non-zero value in the IST field of an
  37. interrupt-gate descriptor. When an interrupt occurs and the hardware
  38. loads such a descriptor, the hardware automatically sets the new stack
  39. pointer based on the IST value, then invokes the interrupt handler. If
  40. the interrupt came from user mode, then the interrupt handler prologue
  41. will switch back to the per-thread stack. If software wants to allow
  42. nested IST interrupts then the handler must adjust the IST values on
  43. entry to and exit from the interrupt handler. (This is occasionally
  44. done, e.g. for debug exceptions.)
  45. Events with different IST codes (i.e. with different stacks) can be
  46. nested. For example, a debug interrupt can safely be interrupted by an
  47. NMI. arch/x86_64/kernel/entry.S::paranoidentry adjusts the stack
  48. pointers on entry to and exit from all IST events, in theory allowing
  49. IST events with the same code to be nested. However in most cases, the
  50. stack size allocated to an IST assumes no nesting for the same code.
  51. If that assumption is ever broken then the stacks will become corrupt.
  52. The currently assigned IST stacks are:
  53. * ESTACK_DF. EXCEPTION_STKSZ (PAGE_SIZE).
  54. Used for interrupt 8 - Double Fault Exception (#DF).
  55. Invoked when handling one exception causes another exception. Happens
  56. when the kernel is very confused (e.g. kernel stack pointer corrupt).
  57. Using a separate stack allows the kernel to recover from it well enough
  58. in many cases to still output an oops.
  59. * ESTACK_NMI. EXCEPTION_STKSZ (PAGE_SIZE).
  60. Used for non-maskable interrupts (NMI).
  61. NMI can be delivered at any time, including when the kernel is in the
  62. middle of switching stacks. Using IST for NMI events avoids making
  63. assumptions about the previous state of the kernel stack.
  64. * ESTACK_DB. EXCEPTION_STKSZ (PAGE_SIZE).
  65. Used for hardware debug interrupts (interrupt 1) and for software
  66. debug interrupts (INT3).
  67. When debugging a kernel, debug interrupts (both hardware and
  68. software) can occur at any time. Using IST for these interrupts
  69. avoids making assumptions about the previous state of the kernel
  70. stack.
  71. To handle nested #DB correctly there exist two instances of DB stacks. On
  72. #DB entry the IST stackpointer for #DB is switched to the second instance
  73. so a nested #DB starts from a clean stack. The nested #DB switches
  74. the IST stackpointer to a guard hole to catch triple nesting.
  75. * ESTACK_MCE. EXCEPTION_STKSZ (PAGE_SIZE).
  76. Used for interrupt 18 - Machine Check Exception (#MC).
  77. MCE can be delivered at any time, including when the kernel is in the
  78. middle of switching stacks. Using IST for MCE events avoids making
  79. assumptions about the previous state of the kernel stack.
  80. For more details see the Intel IA32 or AMD AMD64 architecture manuals.
  81. Printing backtraces on x86
  82. ==========================
  83. The question about the '?' preceding function names in an x86 stacktrace
  84. keeps popping up, here's an indepth explanation. It helps if the reader
  85. stares at print_context_stack() and the whole machinery in and around
  86. arch/x86/kernel/dumpstack.c.
  87. Adapted from Ingo's mail, Message-ID: <20150521101614.GA10889@gmail.com>:
  88. We always scan the full kernel stack for return addresses stored on
  89. the kernel stack(s) [1]_, from stack top to stack bottom, and print out
  90. anything that 'looks like' a kernel text address.
  91. If it fits into the frame pointer chain, we print it without a question
  92. mark, knowing that it's part of the real backtrace.
  93. If the address does not fit into our expected frame pointer chain we
  94. still print it, but we print a '?'. It can mean two things:
  95. - either the address is not part of the call chain: it's just stale
  96. values on the kernel stack, from earlier function calls. This is
  97. the common case.
  98. - or it is part of the call chain, but the frame pointer was not set
  99. up properly within the function, so we don't recognize it.
  100. This way we will always print out the real call chain (plus a few more
  101. entries), regardless of whether the frame pointer was set up correctly
  102. or not - but in most cases we'll get the call chain right as well. The
  103. entries printed are strictly in stack order, so you can deduce more
  104. information from that as well.
  105. The most important property of this method is that we _never_ lose
  106. information: we always strive to print _all_ addresses on the stack(s)
  107. that look like kernel text addresses, so if debug information is wrong,
  108. we still print out the real call chain as well - just with more question
  109. marks than ideal.
  110. .. [1] For things like IRQ and IST stacks, we also scan those stacks, in
  111. the right order, and try to cross from one stack into another
  112. reconstructing the call chain. This works most of the time.