rwsem.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_reg ecx
  33. #define restore_common_regs \
  34. popl_cfi_reg 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_reg rdi; \
  58. pushq_cfi_reg rsi; \
  59. pushq_cfi_reg rcx; \
  60. pushq_cfi_reg r8; \
  61. pushq_cfi_reg r9; \
  62. pushq_cfi_reg r10; \
  63. pushq_cfi_reg r11
  64. #define restore_common_regs \
  65. popq_cfi_reg r11; \
  66. popq_cfi_reg r10; \
  67. popq_cfi_reg r9; \
  68. popq_cfi_reg r8; \
  69. popq_cfi_reg rcx; \
  70. popq_cfi_reg rsi; \
  71. popq_cfi_reg 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_reg) __ASM_REG(dx)
  78. movq %rax,%rdi
  79. call rwsem_down_read_failed
  80. __ASM_SIZE(pop,_cfi_reg) __ASM_REG(dx)
  81. restore_common_regs
  82. ret
  83. CFI_ENDPROC
  84. ENDPROC(call_rwsem_down_read_failed)
  85. ENTRY(call_rwsem_down_write_failed)
  86. CFI_STARTPROC
  87. save_common_regs
  88. movq %rax,%rdi
  89. call rwsem_down_write_failed
  90. restore_common_regs
  91. ret
  92. CFI_ENDPROC
  93. ENDPROC(call_rwsem_down_write_failed)
  94. ENTRY(call_rwsem_wake)
  95. CFI_STARTPROC
  96. /* do nothing if still outstanding active readers */
  97. __ASM_HALF_SIZE(dec) %__ASM_HALF_REG(dx)
  98. jnz 1f
  99. save_common_regs
  100. movq %rax,%rdi
  101. call rwsem_wake
  102. restore_common_regs
  103. 1: ret
  104. CFI_ENDPROC
  105. ENDPROC(call_rwsem_wake)
  106. ENTRY(call_rwsem_downgrade_wake)
  107. CFI_STARTPROC
  108. save_common_regs
  109. __ASM_SIZE(push,_cfi_reg) __ASM_REG(dx)
  110. movq %rax,%rdi
  111. call rwsem_downgrade_wake
  112. __ASM_SIZE(pop,_cfi_reg) __ASM_REG(dx)
  113. restore_common_regs
  114. ret
  115. CFI_ENDPROC
  116. ENDPROC(call_rwsem_downgrade_wake)