rwsem.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * x86 semaphore implementation.
  3. *
  4. * (C) Copyright 1999 Linus Torvalds
  5. *
  6. * Portions Copyright 1999 Red Hat, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. *
  13. * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
  14. */
  15. #include <linux/linkage.h>
  16. #include <asm/alternative-asm.h>
  17. #include <asm/frame.h>
  18. #define __ASM_HALF_REG(reg) __ASM_SEL(reg, e##reg)
  19. #define __ASM_HALF_SIZE(inst) __ASM_SEL(inst##w, inst##l)
  20. #ifdef CONFIG_X86_32
  21. /*
  22. * The semaphore operations have a special calling sequence that
  23. * allow us to do a simpler in-line version of them. These routines
  24. * need to convert that sequence back into the C sequence when
  25. * there is contention on the semaphore.
  26. *
  27. * %eax contains the semaphore pointer on entry. Save the C-clobbered
  28. * registers (%eax, %edx and %ecx) except %eax which is either a return
  29. * value or just gets clobbered. Same is true for %edx so make sure GCC
  30. * reloads it after the slow path, by making it hold a temporary, for
  31. * example see ____down_write().
  32. */
  33. #define save_common_regs \
  34. pushl %ecx
  35. #define restore_common_regs \
  36. popl %ecx
  37. /* Avoid uglifying the argument copying x86-64 needs to do. */
  38. .macro movq src, dst
  39. .endm
  40. #else
  41. /*
  42. * x86-64 rwsem wrappers
  43. *
  44. * This interfaces the inline asm code to the slow-path
  45. * C routines. We need to save the call-clobbered regs
  46. * that the asm does not mark as clobbered, and move the
  47. * argument from %rax to %rdi.
  48. *
  49. * NOTE! We don't need to save %rax, because the functions
  50. * will always return the semaphore pointer in %rax (which
  51. * is also the input argument to these helpers)
  52. *
  53. * The following can clobber %rdx because the asm clobbers it:
  54. * call_rwsem_down_write_failed
  55. * call_rwsem_wake
  56. * but %rdi, %rsi, %rcx, %r8-r11 always need saving.
  57. */
  58. #define save_common_regs \
  59. pushq %rdi; \
  60. pushq %rsi; \
  61. pushq %rcx; \
  62. pushq %r8; \
  63. pushq %r9; \
  64. pushq %r10; \
  65. pushq %r11
  66. #define restore_common_regs \
  67. popq %r11; \
  68. popq %r10; \
  69. popq %r9; \
  70. popq %r8; \
  71. popq %rcx; \
  72. popq %rsi; \
  73. popq %rdi
  74. #endif
  75. /* Fix up special calling conventions */
  76. ENTRY(call_rwsem_down_read_failed)
  77. FRAME_BEGIN
  78. save_common_regs
  79. __ASM_SIZE(push,) %__ASM_REG(dx)
  80. movq %rax,%rdi
  81. call rwsem_down_read_failed
  82. __ASM_SIZE(pop,) %__ASM_REG(dx)
  83. restore_common_regs
  84. FRAME_END
  85. ret
  86. ENDPROC(call_rwsem_down_read_failed)
  87. ENTRY(call_rwsem_down_write_failed)
  88. FRAME_BEGIN
  89. save_common_regs
  90. movq %rax,%rdi
  91. call rwsem_down_write_failed
  92. restore_common_regs
  93. FRAME_END
  94. ret
  95. ENDPROC(call_rwsem_down_write_failed)
  96. ENTRY(call_rwsem_down_write_failed_killable)
  97. FRAME_BEGIN
  98. save_common_regs
  99. movq %rax,%rdi
  100. call rwsem_down_write_failed_killable
  101. restore_common_regs
  102. FRAME_END
  103. ret
  104. ENDPROC(call_rwsem_down_write_failed_killable)
  105. ENTRY(call_rwsem_wake)
  106. FRAME_BEGIN
  107. /* do nothing if still outstanding active readers */
  108. __ASM_HALF_SIZE(dec) %__ASM_HALF_REG(dx)
  109. jnz 1f
  110. save_common_regs
  111. movq %rax,%rdi
  112. call rwsem_wake
  113. restore_common_regs
  114. 1: FRAME_END
  115. ret
  116. ENDPROC(call_rwsem_wake)
  117. ENTRY(call_rwsem_downgrade_wake)
  118. FRAME_BEGIN
  119. save_common_regs
  120. __ASM_SIZE(push,) %__ASM_REG(dx)
  121. movq %rax,%rdi
  122. call rwsem_downgrade_wake
  123. __ASM_SIZE(pop,) %__ASM_REG(dx)
  124. restore_common_regs
  125. FRAME_END
  126. ret
  127. ENDPROC(call_rwsem_downgrade_wake)