syscallas.S 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * syscallas.S
  15. *
  16. * This file contains the entry point of system call table in library OS.
  17. */
  18. #include <shim_unistd_defs.h>
  19. #include "asm-offsets.h"
  20. .global syscalldb
  21. .type syscalldb, @function
  22. .extern shim_table, debug_unsupp
  23. .global syscall_wrapper
  24. .type syscall_wrapper, @function
  25. .global syscall_wrapper_after_syscalldb
  26. .type syscall_wrapper_after_syscalldb, @function
  27. syscalldb:
  28. .cfi_startproc
  29. # Create shim_regs struct on the stack.
  30. pushfq
  31. # Under GDB, single-stepping sets Trap Flag (TP) of EFLAGS,
  32. # thus TP=1 is stored on pushfq above. Upon consequent popfq,
  33. # TP is 1, resulting in spurious trap. Reset TP here.
  34. andq $~0x100, (%rsp)
  35. cld
  36. pushq %rbp
  37. pushq %rbx
  38. pushq %rdi
  39. pushq %rsi
  40. pushq %rdx
  41. pushq %rcx
  42. pushq %r8
  43. pushq %r9
  44. pushq %r10
  45. pushq %r11
  46. pushq %r12
  47. pushq %r13
  48. pushq %r14
  49. pushq %r15
  50. leaq SHIM_REGS_SIZE - SHIM_REGS_R15(%rsp), %rbx
  51. pushq %rbx
  52. pushq %rax
  53. # shim_regs struct ends here.
  54. movq %rsp, %rbp
  55. .cfi_def_cfa_offset SHIM_REGS_SIZE
  56. .cfi_offset %rbp, -3 * 8 # saved_rbp is at CFA-24 (saved_rflags + saved_rbp)
  57. .cfi_def_cfa_register %rbp # %rbp
  58. cmp $LIBOS_SYSCALL_BOUND, %rax
  59. jae isundef
  60. movq shim_table@GOTPCREL(%rip), %rbx
  61. movq (%rbx,%rax,8), %rbx
  62. cmp $0, %rbx
  63. je isundef
  64. movq %rbp, %fs:(SHIM_TCB_OFFSET + TCB_REGS)
  65. /* Translating x86_64 kernel calling convention to user-space
  66. * calling convention */
  67. movq %r10, %rcx
  68. andq $~0xF, %rsp # Required by System V AMD64 ABI.
  69. call *%rbx
  70. movq $0, %fs:(SHIM_TCB_OFFSET + TCB_REGS)
  71. ret:
  72. movq %rbp, %rsp
  73. addq $2 * 8, %rsp # skip orig_rax and rsp
  74. popq %r15
  75. popq %r14
  76. popq %r13
  77. popq %r12
  78. popq %r11
  79. popq %r10
  80. popq %r9
  81. popq %r8
  82. popq %rcx
  83. popq %rdx
  84. popq %rsi
  85. popq %rdi
  86. popq %rbx
  87. popq %rbp
  88. .cfi_def_cfa %rsp, 2 * 8 # +8 for ret_addr, +8 for saved_rflags
  89. popfq
  90. .cfi_def_cfa_offset 8 # +8 for ret_addr
  91. retq
  92. isundef:
  93. #ifdef DEBUG
  94. mov %rax, %rdi
  95. andq $~0xF, %rsp # Required by System V AMD64 ABI.
  96. call *debug_unsupp@GOTPCREL(%rip)
  97. #endif
  98. movq $-38, %rax # ENOSYS
  99. jmp ret
  100. .cfi_endproc
  101. .size syscalldb, .-syscalldb
  102. /*
  103. * syscall_wrapper: emulate syscall instruction
  104. * prohibited in e.g. Linux-SGX PAL which raises a SIGILL exception
  105. * See illegal_upcall() @ shim_signal.c and
  106. * fixup_child_context() @ shim_clone.c
  107. *
  108. * input:
  109. * %rcx: Instruction address to continue app execution after trapped
  110. * syscall instruction
  111. * %r11: rflags on entering syscall
  112. */
  113. syscall_wrapper:
  114. .cfi_startproc
  115. .cfi_def_cfa %rsp, 0
  116. # %rcx is used as input for returning %rip
  117. .cfi_register %rip, %rcx
  118. # %r11 is used as input to keep %rflags
  119. .cfi_register %rflags, %r11
  120. subq $RED_ZONE_SIZE, %rsp
  121. .cfi_adjust_cfa_offset RED_ZONE_SIZE
  122. callq *syscalldb@GOTPCREL(%rip)
  123. syscall_wrapper_after_syscalldb:
  124. addq $RED_ZONE_SIZE, %rsp
  125. .cfi_adjust_cfa_offset -RED_ZONE_SIZE
  126. # restore %rflags for syscall abi compatibility.
  127. # This must be done after "addq $RED_ZONE_SIZE, %rsp" above
  128. # which destroys %rflags
  129. xchg %r11, (%rsp)
  130. .cfi_offset %rflags, 0
  131. popfq
  132. .cfi_adjust_cfa_offset -8
  133. .cfi_same_value %rflags
  134. pushq %r11
  135. .cfi_adjust_cfa_offset 8
  136. jmp *%rcx
  137. .cfi_endproc
  138. .size syscall_wrapper, .-syscall_wrapper