syscallas.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_tls.h>
  19. #include <shim_unistd_defs.h>
  20. #include "asm-offsets.h"
  21. .global syscalldb
  22. .type syscalldb, @function
  23. .extern shim_table, debug_unsupp
  24. .global syscall_wrapper
  25. .type syscall_wrapper, @function
  26. syscalldb:
  27. .cfi_startproc
  28. # Create shim_regs struct on the stack.
  29. pushfq
  30. cld
  31. pushq %rbp
  32. pushq %rbx
  33. pushq %rdi
  34. pushq %rsi
  35. pushq %rdx
  36. pushq %rcx
  37. pushq %r8
  38. pushq %r9
  39. pushq %r10
  40. pushq %r11
  41. pushq %r12
  42. pushq %r13
  43. pushq %r14
  44. pushq %r15
  45. # shim_regs struct ends here.
  46. movq %rsp, %rbp
  47. .cfi_def_cfa_offset SHIM_REGS_SIZE+8 # +8 for ret_addr
  48. .cfi_offset %rbp, -3 * 8 # saved_rbp is at CFA-24 (ret + saved_rflags + saved_rbp)
  49. .cfi_def_cfa_register %rbp # %rbp
  50. cmp $LIBOS_SYSCALL_BOUND, %rax
  51. jae isundef
  52. movq shim_table@GOTPCREL(%rip), %rbx
  53. movq (%rbx,%rax,8), %rbx
  54. cmp $0, %rbx
  55. je isundef
  56. movq %rax, %fs:(SHIM_TCB_OFFSET + TCB_SYSCALL_NR)
  57. leaq SHIM_REGS_SIZE+8(%rbp), %rax
  58. movq %rax, %fs:(SHIM_TCB_OFFSET + TCB_SP)
  59. movq SHIM_REGS_SIZE(%rbp), %rax
  60. movq %rax, %fs:(SHIM_TCB_OFFSET + TCB_RET_IP)
  61. movq %rbp, %fs:(SHIM_TCB_OFFSET + TCB_REGS)
  62. /* Translating x86_64 kernel calling convention to user-space
  63. * calling convention */
  64. movq %r10, %rcx
  65. andq $~0xF, %rsp # Required by System V AMD64 ABI.
  66. call *%rbx
  67. movq $0, %fs:(SHIM_TCB_OFFSET + TCB_SYSCALL_NR)
  68. movq $0, %fs:(SHIM_TCB_OFFSET + TCB_SP)
  69. movq $0, %fs:(SHIM_TCB_OFFSET + TCB_RET_IP)
  70. movq $0, %fs:(SHIM_TCB_OFFSET + TCB_REGS)
  71. ret:
  72. movq %rbp, %rsp
  73. popq %r15
  74. popq %r14
  75. popq %r13
  76. popq %r12
  77. popq %r11
  78. popq %r10
  79. popq %r9
  80. popq %r8
  81. popq %rcx
  82. popq %rdx
  83. popq %rsi
  84. popq %rdi
  85. popq %rbx
  86. popq %rbp
  87. .cfi_def_cfa %rsp, 2 * 8 # +8 for ret_addr, +8 for saved_rflags
  88. popfq
  89. .cfi_def_cfa_offset 8 # +8 for ret_addr
  90. retq
  91. isundef:
  92. #ifdef DEBUG
  93. mov %rax, %rdi
  94. andq $~0xF, %rsp # Required by System V AMD64 ABI.
  95. call *debug_unsupp@GOTPCREL(%rip)
  96. #endif
  97. movq $-38, %rax # ENOSYS
  98. jmp ret
  99. .cfi_endproc
  100. .size syscalldb, .-syscalldb
  101. /*
  102. * syscall_wrapper: emulate syscall instruction
  103. * prohibited in e.g. Linux-SGX PAL which raises a SIGILL exception
  104. *
  105. * input:
  106. * %rcx: Instruction address to continue app execution after trapped
  107. * syscall instruction
  108. * %r11: rflags on entering syscall
  109. *
  110. * FIXME: preserve rflags.
  111. * remember that clone-child can't use parent stack.
  112. */
  113. syscall_wrapper:
  114. .cfi_startproc
  115. subq $RED_ZONE_SIZE, %rsp
  116. callq *syscalldb@GOTPCREL(%rip)
  117. addq $RED_ZONE_SIZE, %rsp
  118. #if 0
  119. # TODO: once clone emulation is fixed, remove this #if 0
  120. xchg %r11, (%rsp)
  121. popfq
  122. pushq %r11
  123. #endif
  124. jmp *%rcx
  125. .cfi_endproc
  126. .size syscall_wrapper, .-syscall_wrapper