rwsem.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/dwarf2.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 whish is either a return
  29. * value or just clobbered..
  30. */
  31. #define save_common_regs \
  32. pushl_cfi %ecx; CFI_REL_OFFSET ecx, 0
  33. #define restore_common_regs \
  34. popl_cfi %ecx; CFI_RESTORE ecx
  35. /* Avoid uglifying the argument copying x86-64 needs to do. */
  36. .macro movq src, dst
  37. .endm
  38. #else
  39. /*
  40. * x86-64 rwsem wrappers
  41. *
  42. * This interfaces the inline asm code to the slow-path
  43. * C routines. We need to save the call-clobbered regs
  44. * that the asm does not mark as clobbered, and move the
  45. * argument from %rax to %rdi.
  46. *
  47. * NOTE! We don't need to save %rax, because the functions
  48. * will always return the semaphore pointer in %rax (which
  49. * is also the input argument to these helpers)
  50. *
  51. * The following can clobber %rdx because the asm clobbers it:
  52. * call_rwsem_down_write_failed
  53. * call_rwsem_wake
  54. * but %rdi, %rsi, %rcx, %r8-r11 always need saving.
  55. */
  56. #define save_common_regs \
  57. pushq_cfi %rdi; CFI_REL_OFFSET rdi, 0; \
  58. pushq_cfi %rsi; CFI_REL_OFFSET rsi, 0; \
  59. pushq_cfi %rcx; CFI_REL_OFFSET rcx, 0; \
  60. pushq_cfi %r8; CFI_REL_OFFSET r8, 0; \
  61. pushq_cfi %r9; CFI_REL_OFFSET r9, 0; \
  62. pushq_cfi %r10; CFI_REL_OFFSET r10, 0; \
  63. pushq_cfi %r11; CFI_REL_OFFSET r11, 0
  64. #define restore_common_regs \
  65. popq_cfi %r11; CFI_RESTORE r11; \
  66. popq_cfi %r10; CFI_RESTORE r10; \
  67. popq_cfi %r9; CFI_RESTORE r9; \
  68. popq_cfi %r8; CFI_RESTORE r8; \
  69. popq_cfi %rcx; CFI_RESTORE rcx; \
  70. popq_cfi %rsi; CFI_RESTORE rsi; \
  71. popq_cfi %rdi; CFI_RESTORE rdi
  72. #endif
  73. /* Fix up special calling conventions */
  74. ENTRY(call_rwsem_down_read_failed)
  75. CFI_STARTPROC
  76. save_common_regs
  77. __ASM_SIZE(push,_cfi) %__ASM_REG(dx)
  78. CFI_REL_OFFSET __ASM_REG(dx), 0
  79. movq %rax,%rdi
  80. call rwsem_down_read_failed
  81. __ASM_SIZE(pop,_cfi) %__ASM_REG(dx)
  82. CFI_RESTORE __ASM_REG(dx)
  83. restore_common_regs
  84. ret
  85. CFI_ENDPROC
  86. ENDPROC(call_rwsem_down_read_failed)
  87. ENTRY(call_rwsem_down_write_failed)
  88. CFI_STARTPROC
  89. save_common_regs
  90. movq %rax,%rdi
  91. call rwsem_down_write_failed
  92. restore_common_regs
  93. ret
  94. CFI_ENDPROC
  95. ENDPROC(call_rwsem_down_write_failed)
  96. ENTRY(call_rwsem_wake)
  97. CFI_STARTPROC
  98. /* do nothing if still outstanding active readers */
  99. __ASM_HALF_SIZE(dec) %__ASM_HALF_REG(dx)
  100. jnz 1f
  101. save_common_regs
  102. movq %rax,%rdi
  103. call rwsem_wake
  104. restore_common_regs
  105. 1: ret
  106. CFI_ENDPROC
  107. ENDPROC(call_rwsem_wake)
  108. ENTRY(call_rwsem_downgrade_wake)
  109. CFI_STARTPROC
  110. save_common_regs
  111. __ASM_SIZE(push,_cfi) %__ASM_REG(dx)
  112. CFI_REL_OFFSET __ASM_REG(dx), 0
  113. movq %rax,%rdi
  114. call rwsem_downgrade_wake
  115. __ASM_SIZE(pop,_cfi) %__ASM_REG(dx)
  116. CFI_RESTORE __ASM_REG(dx)
  117. restore_common_regs
  118. ret
  119. CFI_ENDPROC
  120. ENDPROC(call_rwsem_downgrade_wake)