kernel-ABI.txt 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ==========================
  2. KERNEL ABIS FOR METAG ARCH
  3. ==========================
  4. This document describes the Linux ABIs for the metag architecture, and has the
  5. following sections:
  6. (*) Outline of registers
  7. (*) Userland registers
  8. (*) Kernel registers
  9. (*) System call ABI
  10. (*) Calling conventions
  11. ====================
  12. OUTLINE OF REGISTERS
  13. ====================
  14. The main Meta core registers are arranged in units:
  15. UNIT Type DESCRIPTION GP EXT PRIV GLOBAL
  16. ======= ======= =============== ======= ======= ======= =======
  17. CT Special Control unit
  18. D0 General Data unit 0 0-7 8-15 16-31 16-31
  19. D1 General Data unit 1 0-7 8-15 16-31 16-31
  20. A0 General Address unit 0 0-3 4-7 8-15 8-15
  21. A1 General Address unit 1 0-3 4-7 8-15 8-15
  22. PC Special PC unit 0 1
  23. PORT Special Ports
  24. TR Special Trigger unit 0-7
  25. TT Special Trace unit 0-5
  26. FX General FP unit 0-15
  27. GP registers form part of the main context.
  28. Extended context registers (EXT) may not be present on all hardware threads and
  29. can be context switched if support is enabled and the appropriate bits are set
  30. in e.g. the D0.8 register to indicate what extended state to preserve.
  31. Global registers are shared between threads and are privilege protected.
  32. See arch/metag/include/asm/metag_regs.h for definitions relating to core
  33. registers and the fields and bits they contain. See the TRMs for further details
  34. about special registers.
  35. Several special registers are preserved in the main context, these are the
  36. interesting ones:
  37. REG (ALIAS) PURPOSE
  38. ======================= ===============================================
  39. CT.1 (TXMODE) Processor mode bits (particularly for DSP)
  40. CT.2 (TXSTATUS) Condition flags and LSM_STEP (MGET/MSET step)
  41. CT.3 (TXRPT) Branch repeat counter
  42. PC.0 (PC) Program counter
  43. Some of the general registers have special purposes in the ABI and therefore
  44. have aliases:
  45. D0 REG (ALIAS) PURPOSE D1 REG (ALIAS) PURPOSE
  46. =============== =============== =============== =======================
  47. D0.0 (D0Re0) 32bit result D1.0 (D1Re0) Top half of 64bit result
  48. D0.1 (D0Ar6) Argument 6 D1.1 (D1Ar5) Argument 5
  49. D0.2 (D0Ar4) Argument 4 D1.2 (D1Ar3) Argument 3
  50. D0.3 (D0Ar2) Argument 2 D1.3 (D1Ar1) Argument 1
  51. D0.4 (D0FrT) Frame temp D1.4 (D1RtP) Return pointer
  52. D0.5 Call preserved D1.5 Call preserved
  53. D0.6 Call preserved D1.6 Call preserved
  54. D0.7 Call preserved D1.7 Call preserved
  55. A0 REG (ALIAS) PURPOSE A1 REG (ALIAS) PURPOSE
  56. =============== =============== =============== =======================
  57. A0.0 (A0StP) Stack pointer A1.0 (A1GbP) Global base pointer
  58. A0.1 (A0FrP) Frame pointer A1.1 (A1LbP) Local base pointer
  59. A0.2 A1.2
  60. A0.3 A1.3
  61. ==================
  62. USERLAND REGISTERS
  63. ==================
  64. All the general purpose D0, D1, A0, A1 registers are preserved when entering the
  65. kernel (including asynchronous events such as interrupts and timer ticks) except
  66. the following which have special purposes in the ABI:
  67. REGISTERS WHEN STATUS PURPOSE
  68. =============== ======= =============== ===============================
  69. D0.8 DSP Preserved ECH, determines what extended
  70. DSP state to preserve.
  71. A0.0 (A0StP) ALWAYS Preserved Stack >= A0StP may be clobbered
  72. at any time by the creation of a
  73. signal frame.
  74. A1.0 (A1GbP) SMP Clobbered Used as temporary for loading
  75. kernel stack pointer and saving
  76. core context.
  77. A0.15 !SMP Protected Stores kernel stack pointer.
  78. A1.15 ALWAYS Protected Stores kernel base pointer.
  79. On UP A0.15 is used to store the kernel stack pointer for storing the userland
  80. context. A0.15 is global between hardware threads though which means it cannot
  81. be used on SMP for this purpose. Since no protected local registers are
  82. available A1GbP is reserved for use as a temporary to allow a percpu stack
  83. pointer to be loaded for storing the rest of the context.
  84. ================
  85. KERNEL REGISTERS
  86. ================
  87. When in the kernel the following registers have special purposes in the ABI:
  88. REGISTERS WHEN STATUS PURPOSE
  89. =============== ======= =============== ===============================
  90. A0.0 (A0StP) ALWAYS Preserved Stack >= A0StP may be clobbered
  91. at any time by the creation of
  92. an irq signal frame.
  93. A1.0 (A1GbP) ALWAYS Preserved Reserved (kernel base pointer).
  94. ===============
  95. SYSTEM CALL ABI
  96. ===============
  97. When a system call is made, the following registers are effective:
  98. REGISTERS CALL RETURN
  99. =============== ======================= ===============================
  100. D0.0 (D0Re0) Return value (or -errno)
  101. D1.0 (D1Re0) System call number Clobbered
  102. D0.1 (D0Ar6) Syscall arg #6 Preserved
  103. D1.1 (D1Ar5) Syscall arg #5 Preserved
  104. D0.2 (D0Ar4) Syscall arg #4 Preserved
  105. D1.2 (D1Ar3) Syscall arg #3 Preserved
  106. D0.3 (D0Ar2) Syscall arg #2 Preserved
  107. D1.3 (D1Ar1) Syscall arg #1 Preserved
  108. Due to the limited number of argument registers and some system calls with badly
  109. aligned 64-bit arguments, 64-bit values are always packed in consecutive
  110. arguments, even if this is contrary to the normal calling conventions (where the
  111. two halves would go in a matching pair of data registers).
  112. For example fadvise64_64 usually has the signature:
  113. long sys_fadvise64_64(i32 fd, i64 offs, i64 len, i32 advice);
  114. But for metag fadvise64_64 is wrapped so that the 64-bit arguments are packed:
  115. long sys_fadvise64_64_metag(i32 fd, i32 offs_lo,
  116. i32 offs_hi, i32 len_lo,
  117. i32 len_hi, i32 advice)
  118. So the arguments are packed in the registers like this:
  119. D0 REG (ALIAS) VALUE D1 REG (ALIAS) VALUE
  120. =============== =============== =============== =======================
  121. D0.1 (D0Ar6) advice D1.1 (D1Ar5) hi(len)
  122. D0.2 (D0Ar4) lo(len) D1.2 (D1Ar3) hi(offs)
  123. D0.3 (D0Ar2) lo(offs) D1.3 (D1Ar1) fd
  124. ===================
  125. CALLING CONVENTIONS
  126. ===================
  127. These calling conventions apply to both user and kernel code. The stack grows
  128. from low addresses to high addresses in the metag ABI. The stack pointer (A0StP)
  129. should always point to the next free address on the stack and should at all
  130. times be 64-bit aligned. The following registers are effective at the point of a
  131. call:
  132. REGISTERS CALL RETURN
  133. =============== ======================= ===============================
  134. D0.0 (D0Re0) 32bit return value
  135. D1.0 (D1Re0) Upper half of 64bit return value
  136. D0.1 (D0Ar6) 32bit argument #6 Clobbered
  137. D1.1 (D1Ar5) 32bit argument #5 Clobbered
  138. D0.2 (D0Ar4) 32bit argument #4 Clobbered
  139. D1.2 (D1Ar3) 32bit argument #3 Clobbered
  140. D0.3 (D0Ar2) 32bit argument #2 Clobbered
  141. D1.3 (D1Ar1) 32bit argument #1 Clobbered
  142. D0.4 (D0FrT) Clobbered
  143. D1.4 (D1RtP) Return pointer Clobbered
  144. D{0-1}.{5-7} Preserved
  145. A0.0 (A0StP) Stack pointer Preserved
  146. A1.0 (A0GbP) Preserved
  147. A0.1 (A0FrP) Frame pointer Preserved
  148. A1.1 (A0LbP) Preserved
  149. A{0-1},{2-3} Clobbered
  150. 64-bit arguments are placed in matching pairs of registers (i.e. the same
  151. register number in both D0 and D1 units), with the least significant half in D0
  152. and the most significant half in D1, leaving a gap where necessary. Further
  153. arguments are stored on the stack in reverse order (earlier arguments at higher
  154. addresses):
  155. ADDRESS 0 1 2 3 4 5 6 7
  156. =============== ===== ===== ===== ===== ===== ===== ===== =====
  157. A0StP -->
  158. A0StP-0x08 32bit argument #8 32bit argument #7
  159. A0StP-0x10 32bit argument #10 32bit argument #9
  160. Function prologues tend to look a bit like this:
  161. /* If frame pointer in use, move it to frame temp register so it can be
  162. easily pushed onto stack */
  163. MOV D0FrT,A0FrP
  164. /* If frame pointer in use, set it to stack pointer */
  165. ADD A0FrP,A0StP,#0
  166. /* Preserve D0FrT, D1RtP, D{0-1}.{5-7} on stack, incrementing A0StP */
  167. MSETL [A0StP++],D0FrT,D0.5,D0.6,D0.7
  168. /* Allocate some stack space for local variables */
  169. ADD A0StP,A0StP,#0x10
  170. At this point the stack would look like this:
  171. ADDRESS 0 1 2 3 4 5 6 7
  172. =============== ===== ===== ===== ===== ===== ===== ===== =====
  173. A0StP -->
  174. A0StP-0x08
  175. A0StP-0x10
  176. A0StP-0x18 Old D0.7 Old D1.7
  177. A0StP-0x20 Old D0.6 Old D1.6
  178. A0StP-0x28 Old D0.5 Old D1.5
  179. A0FrP --> Old A0FrP (frame ptr) Old D1RtP (return ptr)
  180. A0FrP-0x08 32bit argument #8 32bit argument #7
  181. A0FrP-0x10 32bit argument #10 32bit argument #9
  182. Function epilogues tend to differ depending on the use of a frame pointer. An
  183. example of a frame pointer epilogue:
  184. /* Restore D0FrT, D1RtP, D{0-1}.{5-7} from stack, incrementing A0FrP */
  185. MGETL D0FrT,D0.5,D0.6,D0.7,[A0FrP++]
  186. /* Restore stack pointer to where frame pointer was before increment */
  187. SUB A0StP,A0FrP,#0x20
  188. /* Restore frame pointer from frame temp */
  189. MOV A0FrP,D0FrT
  190. /* Return to caller via restored return pointer */
  191. MOV PC,D1RtP
  192. If the function hasn't touched the frame pointer, MGETL cannot be safely used
  193. with A0StP as it always increments and that would expose the stack to clobbering
  194. by interrupts (kernel) or signals (user). Therefore it's common to see the MGETL
  195. split into separate GETL instructions:
  196. /* Restore D0FrT, D1RtP, D{0-1}.{5-7} from stack */
  197. GETL D0FrT,D1RtP,[A0StP+#-0x30]
  198. GETL D0.5,D1.5,[A0StP+#-0x28]
  199. GETL D0.6,D1.6,[A0StP+#-0x20]
  200. GETL D0.7,D1.7,[A0StP+#-0x18]
  201. /* Restore stack pointer */
  202. SUB A0StP,A0StP,#0x30
  203. /* Return to caller via restored return pointer */
  204. MOV PC,D1RtP