fsys.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. -*-Mode: outline-*-
  2. Light-weight System Calls for IA-64
  3. -----------------------------------
  4. Started: 13-Jan-2003
  5. Last update: 27-Sep-2003
  6. David Mosberger-Tang
  7. <davidm@hpl.hp.com>
  8. Using the "epc" instruction effectively introduces a new mode of
  9. execution to the ia64 linux kernel. We call this mode the
  10. "fsys-mode". To recap, the normal states of execution are:
  11. - kernel mode:
  12. Both the register stack and the memory stack have been
  13. switched over to kernel memory. The user-level state is saved
  14. in a pt-regs structure at the top of the kernel memory stack.
  15. - user mode:
  16. Both the register stack and the kernel stack are in
  17. user memory. The user-level state is contained in the
  18. CPU registers.
  19. - bank 0 interruption-handling mode:
  20. This is the non-interruptible state which all
  21. interruption-handlers start execution in. The user-level
  22. state remains in the CPU registers and some kernel state may
  23. be stored in bank 0 of registers r16-r31.
  24. In contrast, fsys-mode has the following special properties:
  25. - execution is at privilege level 0 (most-privileged)
  26. - CPU registers may contain a mixture of user-level and kernel-level
  27. state (it is the responsibility of the kernel to ensure that no
  28. security-sensitive kernel-level state is leaked back to
  29. user-level)
  30. - execution is interruptible and preemptible (an fsys-mode handler
  31. can disable interrupts and avoid all other interruption-sources
  32. to avoid preemption)
  33. - neither the memory-stack nor the register-stack can be trusted while
  34. in fsys-mode (they point to the user-level stacks, which may
  35. be invalid, or completely bogus addresses)
  36. In summary, fsys-mode is much more similar to running in user-mode
  37. than it is to running in kernel-mode. Of course, given that the
  38. privilege level is at level 0, this means that fsys-mode requires some
  39. care (see below).
  40. * How to tell fsys-mode
  41. Linux operates in fsys-mode when (a) the privilege level is 0 (most
  42. privileged) and (b) the stacks have NOT been switched to kernel memory
  43. yet. For convenience, the header file <asm-ia64/ptrace.h> provides
  44. three macros:
  45. user_mode(regs)
  46. user_stack(task,regs)
  47. fsys_mode(task,regs)
  48. The "regs" argument is a pointer to a pt_regs structure. The "task"
  49. argument is a pointer to the task structure to which the "regs"
  50. pointer belongs to. user_mode() returns TRUE if the CPU state pointed
  51. to by "regs" was executing in user mode (privilege level 3).
  52. user_stack() returns TRUE if the state pointed to by "regs" was
  53. executing on the user-level stack(s). Finally, fsys_mode() returns
  54. TRUE if the CPU state pointed to by "regs" was executing in fsys-mode.
  55. The fsys_mode() macro is equivalent to the expression:
  56. !user_mode(regs) && user_stack(task,regs)
  57. * How to write an fsyscall handler
  58. The file arch/ia64/kernel/fsys.S contains a table of fsyscall-handlers
  59. (fsyscall_table). This table contains one entry for each system call.
  60. By default, a system call is handled by fsys_fallback_syscall(). This
  61. routine takes care of entering (full) kernel mode and calling the
  62. normal Linux system call handler. For performance-critical system
  63. calls, it is possible to write a hand-tuned fsyscall_handler. For
  64. example, fsys.S contains fsys_getpid(), which is a hand-tuned version
  65. of the getpid() system call.
  66. The entry and exit-state of an fsyscall handler is as follows:
  67. ** Machine state on entry to fsyscall handler:
  68. - r10 = 0
  69. - r11 = saved ar.pfs (a user-level value)
  70. - r15 = system call number
  71. - r16 = "current" task pointer (in normal kernel-mode, this is in r13)
  72. - r32-r39 = system call arguments
  73. - b6 = return address (a user-level value)
  74. - ar.pfs = previous frame-state (a user-level value)
  75. - PSR.be = cleared to zero (i.e., little-endian byte order is in effect)
  76. - all other registers may contain values passed in from user-mode
  77. ** Required machine state on exit to fsyscall handler:
  78. - r11 = saved ar.pfs (as passed into the fsyscall handler)
  79. - r15 = system call number (as passed into the fsyscall handler)
  80. - r32-r39 = system call arguments (as passed into the fsyscall handler)
  81. - b6 = return address (as passed into the fsyscall handler)
  82. - ar.pfs = previous frame-state (as passed into the fsyscall handler)
  83. Fsyscall handlers can execute with very little overhead, but with that
  84. speed comes a set of restrictions:
  85. o Fsyscall-handlers MUST check for any pending work in the flags
  86. member of the thread-info structure and if any of the
  87. TIF_ALLWORK_MASK flags are set, the handler needs to fall back on
  88. doing a full system call (by calling fsys_fallback_syscall).
  89. o Fsyscall-handlers MUST preserve incoming arguments (r32-r39, r11,
  90. r15, b6, and ar.pfs) because they will be needed in case of a
  91. system call restart. Of course, all "preserved" registers also
  92. must be preserved, in accordance to the normal calling conventions.
  93. o Fsyscall-handlers MUST check argument registers for containing a
  94. NaT value before using them in any way that could trigger a
  95. NaT-consumption fault. If a system call argument is found to
  96. contain a NaT value, an fsyscall-handler may return immediately
  97. with r8=EINVAL, r10=-1.
  98. o Fsyscall-handlers MUST NOT use the "alloc" instruction or perform
  99. any other operation that would trigger mandatory RSE
  100. (register-stack engine) traffic.
  101. o Fsyscall-handlers MUST NOT write to any stacked registers because
  102. it is not safe to assume that user-level called a handler with the
  103. proper number of arguments.
  104. o Fsyscall-handlers need to be careful when accessing per-CPU variables:
  105. unless proper safe-guards are taken (e.g., interruptions are avoided),
  106. execution may be pre-empted and resumed on another CPU at any given
  107. time.
  108. o Fsyscall-handlers must be careful not to leak sensitive kernel'
  109. information back to user-level. In particular, before returning to
  110. user-level, care needs to be taken to clear any scratch registers
  111. that could contain sensitive information (note that the current
  112. task pointer is not considered sensitive: it's already exposed
  113. through ar.k6).
  114. o Fsyscall-handlers MUST NOT access user-memory without first
  115. validating access-permission (this can be done typically via
  116. probe.r.fault and/or probe.w.fault) and without guarding against
  117. memory access exceptions (this can be done with the EX() macros
  118. defined by asmmacro.h).
  119. The above restrictions may seem draconian, but remember that it's
  120. possible to trade off some of the restrictions by paying a slightly
  121. higher overhead. For example, if an fsyscall-handler could benefit
  122. from the shadow register bank, it could temporarily disable PSR.i and
  123. PSR.ic, switch to bank 0 (bsw.0) and then use the shadow registers as
  124. needed. In other words, following the above rules yields extremely
  125. fast system call execution (while fully preserving system call
  126. semantics), but there is also a lot of flexibility in handling more
  127. complicated cases.
  128. * Signal handling
  129. The delivery of (asynchronous) signals must be delayed until fsys-mode
  130. is exited. This is accomplished with the help of the lower-privilege
  131. transfer trap: arch/ia64/kernel/process.c:do_notify_resume_user()
  132. checks whether the interrupted task was in fsys-mode and, if so, sets
  133. PSR.lp and returns immediately. When fsys-mode is exited via the
  134. "br.ret" instruction that lowers the privilege level, a trap will
  135. occur. The trap handler clears PSR.lp again and returns immediately.
  136. The kernel exit path then checks for and delivers any pending signals.
  137. * PSR Handling
  138. The "epc" instruction doesn't change the contents of PSR at all. This
  139. is in contrast to a regular interruption, which clears almost all
  140. bits. Because of that, some care needs to be taken to ensure things
  141. work as expected. The following discussion describes how each PSR bit
  142. is handled.
  143. PSR.be Cleared when entering fsys-mode. A srlz.d instruction is used
  144. to ensure the CPU is in little-endian mode before the first
  145. load/store instruction is executed. PSR.be is normally NOT
  146. restored upon return from an fsys-mode handler. In other
  147. words, user-level code must not rely on PSR.be being preserved
  148. across a system call.
  149. PSR.up Unchanged.
  150. PSR.ac Unchanged.
  151. PSR.mfl Unchanged. Note: fsys-mode handlers must not write-registers!
  152. PSR.mfh Unchanged. Note: fsys-mode handlers must not write-registers!
  153. PSR.ic Unchanged. Note: fsys-mode handlers can clear the bit, if needed.
  154. PSR.i Unchanged. Note: fsys-mode handlers can clear the bit, if needed.
  155. PSR.pk Unchanged.
  156. PSR.dt Unchanged.
  157. PSR.dfl Unchanged. Note: fsys-mode handlers must not write-registers!
  158. PSR.dfh Unchanged. Note: fsys-mode handlers must not write-registers!
  159. PSR.sp Unchanged.
  160. PSR.pp Unchanged.
  161. PSR.di Unchanged.
  162. PSR.si Unchanged.
  163. PSR.db Unchanged. The kernel prevents user-level from setting a hardware
  164. breakpoint that triggers at any privilege level other than 3 (user-mode).
  165. PSR.lp Unchanged.
  166. PSR.tb Lazy redirect. If a taken-branch trap occurs while in
  167. fsys-mode, the trap-handler modifies the saved machine state
  168. such that execution resumes in the gate page at
  169. syscall_via_break(), with privilege level 3. Note: the
  170. taken branch would occur on the branch invoking the
  171. fsyscall-handler, at which point, by definition, a syscall
  172. restart is still safe. If the system call number is invalid,
  173. the fsys-mode handler will return directly to user-level. This
  174. return will trigger a taken-branch trap, but since the trap is
  175. taken _after_ restoring the privilege level, the CPU has already
  176. left fsys-mode, so no special treatment is needed.
  177. PSR.rt Unchanged.
  178. PSR.cpl Cleared to 0.
  179. PSR.is Unchanged (guaranteed to be 0 on entry to the gate page).
  180. PSR.mc Unchanged.
  181. PSR.it Unchanged (guaranteed to be 1).
  182. PSR.id Unchanged. Note: the ia64 linux kernel never sets this bit.
  183. PSR.da Unchanged. Note: the ia64 linux kernel never sets this bit.
  184. PSR.dd Unchanged. Note: the ia64 linux kernel never sets this bit.
  185. PSR.ss Lazy redirect. If set, "epc" will cause a Single Step Trap to
  186. be taken. The trap handler then modifies the saved machine
  187. state such that execution resumes in the gate page at
  188. syscall_via_break(), with privilege level 3.
  189. PSR.ri Unchanged.
  190. PSR.ed Unchanged. Note: This bit could only have an effect if an fsys-mode
  191. handler performed a speculative load that gets NaTted. If so, this
  192. would be the normal & expected behavior, so no special treatment is
  193. needed.
  194. PSR.bn Unchanged. Note: fsys-mode handlers may clear the bit, if needed.
  195. Doing so requires clearing PSR.i and PSR.ic as well.
  196. PSR.ia Unchanged. Note: the ia64 linux kernel never sets this bit.
  197. * Using fast system calls
  198. To use fast system calls, userspace applications need simply call
  199. __kernel_syscall_via_epc(). For example
  200. -- example fgettimeofday() call --
  201. -- fgettimeofday.S --
  202. #include <asm/asmmacro.h>
  203. GLOBAL_ENTRY(fgettimeofday)
  204. .prologue
  205. .save ar.pfs, r11
  206. mov r11 = ar.pfs
  207. .body
  208. mov r2 = 0xa000000000020660;; // gate address
  209. // found by inspection of System.map for the
  210. // __kernel_syscall_via_epc() function. See
  211. // below for how to do this for real.
  212. mov b7 = r2
  213. mov r15 = 1087 // gettimeofday syscall
  214. ;;
  215. br.call.sptk.many b6 = b7
  216. ;;
  217. .restore sp
  218. mov ar.pfs = r11
  219. br.ret.sptk.many rp;; // return to caller
  220. END(fgettimeofday)
  221. -- end fgettimeofday.S --
  222. In reality, getting the gate address is accomplished by two extra
  223. values passed via the ELF auxiliary vector (include/asm-ia64/elf.h)
  224. o AT_SYSINFO : is the address of __kernel_syscall_via_epc()
  225. o AT_SYSINFO_EHDR : is the address of the kernel gate ELF DSO
  226. The ELF DSO is a pre-linked library that is mapped in by the kernel at
  227. the gate page. It is a proper ELF shared object so, with a dynamic
  228. loader that recognises the library, you should be able to make calls to
  229. the exported functions within it as with any other shared library.
  230. AT_SYSINFO points into the kernel DSO at the
  231. __kernel_syscall_via_epc() function for historical reasons (it was
  232. used before the kernel DSO) and as a convenience.