kernel-ABI.txt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. =================================
  2. INTERNAL KERNEL ABI FOR FR-V ARCH
  3. =================================
  4. The internal FRV kernel ABI is not quite the same as the userspace ABI. A
  5. number of the registers are used for special purposed, and the ABI is not
  6. consistent between modules vs core, and MMU vs no-MMU.
  7. This partly stems from the fact that FRV CPUs do not have a separate
  8. supervisor stack pointer, and most of them do not have any scratch
  9. registers, thus requiring at least one general purpose register to be
  10. clobbered in such an event. Also, within the kernel core, it is possible to
  11. simply jump or call directly between functions using a relative offset.
  12. This cannot be extended to modules for the displacement is likely to be too
  13. far. Thus in modules the address of a function to call must be calculated
  14. in a register and then used, requiring two extra instructions.
  15. This document has the following sections:
  16. (*) System call register ABI
  17. (*) CPU operating modes
  18. (*) Internal kernel-mode register ABI
  19. (*) Internal debug-mode register ABI
  20. (*) Virtual interrupt handling
  21. ========================
  22. SYSTEM CALL REGISTER ABI
  23. ========================
  24. When a system call is made, the following registers are effective:
  25. REGISTERS CALL RETURN
  26. =============== ======================= =======================
  27. GR7 System call number Preserved
  28. GR8 Syscall arg #1 Return value
  29. GR9-GR13 Syscall arg #2-6 Preserved
  30. ===================
  31. CPU OPERATING MODES
  32. ===================
  33. The FR-V CPU has three basic operating modes. In order of increasing
  34. capability:
  35. (1) User mode.
  36. Basic userspace running mode.
  37. (2) Kernel mode.
  38. Normal kernel mode. There are many additional control registers
  39. available that may be accessed in this mode, in addition to all the
  40. stuff available to user mode. This has two submodes:
  41. (a) Exceptions enabled (PSR.T == 1).
  42. Exceptions will invoke the appropriate normal kernel mode
  43. handler. On entry to the handler, the PSR.T bit will be cleared.
  44. (b) Exceptions disabled (PSR.T == 0).
  45. No exceptions or interrupts may happen. Any mandatory exceptions
  46. will cause the CPU to halt unless the CPU is told to jump into
  47. debug mode instead.
  48. (3) Debug mode.
  49. No exceptions may happen in this mode. Memory protection and
  50. management exceptions will be flagged for later consideration, but
  51. the exception handler won't be invoked. Debugging traps such as
  52. hardware breakpoints and watchpoints will be ignored. This mode is
  53. entered only by debugging events obtained from the other two modes.
  54. All kernel mode registers may be accessed, plus a few extra debugging
  55. specific registers.
  56. =================================
  57. INTERNAL KERNEL-MODE REGISTER ABI
  58. =================================
  59. There are a number of permanent register assignments that are set up by
  60. entry.S in the exception prologue. Note that there is a complete set of
  61. exception prologues for each of user->kernel transition and kernel->kernel
  62. transition. There are also user->debug and kernel->debug mode transition
  63. prologues.
  64. REGISTER FLAVOUR USE
  65. =============== ======= ==============================================
  66. GR1 Supervisor stack pointer
  67. GR15 Current thread info pointer
  68. GR16 GP-Rel base register for small data
  69. GR28 Current exception frame pointer (__frame)
  70. GR29 Current task pointer (current)
  71. GR30 Destroyed by kernel mode entry
  72. GR31 NOMMU Destroyed by debug mode entry
  73. GR31 MMU Destroyed by TLB miss kernel mode entry
  74. CCR.ICC2 Virtual interrupt disablement tracking
  75. CCCR.CC3 Cleared by exception prologue
  76. (atomic op emulation)
  77. SCR0 MMU See mmu-layout.txt.
  78. SCR1 MMU See mmu-layout.txt.
  79. SCR2 MMU Save for EAR0 (destroyed by icache insns
  80. in debug mode)
  81. SCR3 MMU Save for GR31 during debug exceptions
  82. DAMR/IAMR NOMMU Fixed memory protection layout.
  83. DAMR/IAMR MMU See mmu-layout.txt.
  84. Certain registers are also used or modified across function calls:
  85. REGISTER CALL RETURN
  86. =============== =============================== ======================
  87. GR0 Fixed Zero -
  88. GR2 Function call frame pointer
  89. GR3 Special Preserved
  90. GR3-GR7 - Clobbered
  91. GR8 Function call arg #1 Return value
  92. (or clobbered)
  93. GR9 Function call arg #2 Return value MSW
  94. (or clobbered)
  95. GR10-GR13 Function call arg #3-#6 Clobbered
  96. GR14 - Clobbered
  97. GR15-GR16 Special Preserved
  98. GR17-GR27 - Preserved
  99. GR28-GR31 Special Only accessed
  100. explicitly
  101. LR Return address after CALL Clobbered
  102. CCR/CCCR - Mostly Clobbered
  103. ================================
  104. INTERNAL DEBUG-MODE REGISTER ABI
  105. ================================
  106. This is the same as the kernel-mode register ABI for functions calls. The
  107. difference is that in debug-mode there's a different stack and a different
  108. exception frame. Almost all the global registers from kernel-mode
  109. (including the stack pointer) may be changed.
  110. REGISTER FLAVOUR USE
  111. =============== ======= ==============================================
  112. GR1 Debug stack pointer
  113. GR16 GP-Rel base register for small data
  114. GR31 Current debug exception frame pointer
  115. (__debug_frame)
  116. SCR3 MMU Saved value of GR31
  117. Note that debug mode is able to interfere with the kernel's emulated atomic
  118. ops, so it must be exceedingly careful not to do any that would interact
  119. with the main kernel in this regard. Hence the debug mode code (gdbstub) is
  120. almost completely self-contained. The only external code used is the
  121. sprintf family of functions.
  122. Furthermore, break.S is so complicated because single-step mode does not
  123. switch off on entry to an exception. That means unless manually disabled,
  124. single-stepping will blithely go on stepping into things like interrupts.
  125. See gdbstub.txt for more information.
  126. ==========================
  127. VIRTUAL INTERRUPT HANDLING
  128. ==========================
  129. Because accesses to the PSR is so slow, and to disable interrupts we have
  130. to access it twice (once to read and once to write), we don't actually
  131. disable interrupts at all if we don't have to. What we do instead is use
  132. the ICC2 condition code flags to note virtual disablement, such that if we
  133. then do take an interrupt, we note the flag, really disable interrupts, set
  134. another flag and resume execution at the point the interrupt happened.
  135. Setting condition flags as a side effect of an arithmetic or logical
  136. instruction is really fast. This use of the ICC2 only occurs within the
  137. kernel - it does not affect userspace.
  138. The flags we use are:
  139. (*) CCR.ICC2.Z [Zero flag]
  140. Set to virtually disable interrupts, clear when interrupts are
  141. virtually enabled. Can be modified by logical instructions without
  142. affecting the Carry flag.
  143. (*) CCR.ICC2.C [Carry flag]
  144. Clear to indicate hardware interrupts are really disabled, set otherwise.
  145. What happens is this:
  146. (1) Normal kernel-mode operation.
  147. ICC2.Z is 0, ICC2.C is 1.
  148. (2) An interrupt occurs. The exception prologue examines ICC2.Z and
  149. determines that nothing needs doing. This is done simply with an
  150. unlikely BEQ instruction.
  151. (3) The interrupts are disabled (local_irq_disable)
  152. ICC2.Z is set to 1.
  153. (4) If interrupts were then re-enabled (local_irq_enable):
  154. ICC2.Z would be set to 0.
  155. A TIHI #2 instruction (trap #2 if condition HI - Z==0 && C==0) would
  156. be used to trap if interrupts were now virtually enabled, but
  157. physically disabled - which they're not, so the trap isn't taken. The
  158. kernel would then be back to state (1).
  159. (5) An interrupt occurs. The exception prologue examines ICC2.Z and
  160. determines that the interrupt shouldn't actually have happened. It
  161. jumps aside, and there disabled interrupts by setting PSR.PIL to 14
  162. and then it clears ICC2.C.
  163. (6) If interrupts were then saved and disabled again (local_irq_save):
  164. ICC2.Z would be shifted into the save variable and masked off
  165. (giving a 1).
  166. ICC2.Z would then be set to 1 (thus unchanged), and ICC2.C would be
  167. unaffected (ie: 0).
  168. (7) If interrupts were then restored from state (6) (local_irq_restore):
  169. ICC2.Z would be set to indicate the result of XOR'ing the saved
  170. value (ie: 1) with 1, which gives a result of 0 - thus leaving
  171. ICC2.Z set.
  172. ICC2.C would remain unaffected (ie: 0).
  173. A TIHI #2 instruction would be used to again assay the current state,
  174. but this would do nothing as Z==1.
  175. (8) If interrupts were then enabled (local_irq_enable):
  176. ICC2.Z would be cleared. ICC2.C would be left unaffected. Both
  177. flags would now be 0.
  178. A TIHI #2 instruction again issued to assay the current state would
  179. then trap as both Z==0 [interrupts virtually enabled] and C==0
  180. [interrupts really disabled] would then be true.
  181. (9) The trap #2 handler would simply enable hardware interrupts
  182. (set PSR.PIL to 0), set ICC2.C to 1 and return.
  183. (10) Immediately upon returning, the pending interrupt would be taken.
  184. (11) The interrupt handler would take the path of actually processing the
  185. interrupt (ICC2.Z is clear, BEQ fails as per step (2)).
  186. (12) The interrupt handler would then set ICC2.C to 1 since hardware
  187. interrupts are definitely enabled - or else the kernel wouldn't be here.
  188. (13) On return from the interrupt handler, things would be back to state (1).
  189. This trap (#2) is only available in kernel mode. In user mode it will
  190. result in SIGILL.