semaphore.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * i386 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/config.h>
  16. #include <asm/semaphore.h>
  17. /*
  18. * The semaphore operations have a special calling sequence that
  19. * allow us to do a simpler in-line version of them. These routines
  20. * need to convert that sequence back into the C sequence when
  21. * there is contention on the semaphore.
  22. *
  23. * %eax contains the semaphore pointer on entry. Save the C-clobbered
  24. * registers (%eax, %edx and %ecx) except %eax whish is either a return
  25. * value or just clobbered..
  26. */
  27. asm(
  28. ".section .sched.text\n"
  29. ".align 4\n"
  30. ".globl __down_failed\n"
  31. "__down_failed:\n\t"
  32. #if defined(CONFIG_FRAME_POINTER)
  33. "pushl %ebp\n\t"
  34. "movl %esp,%ebp\n\t"
  35. #endif
  36. "pushl %edx\n\t"
  37. "pushl %ecx\n\t"
  38. "call __down\n\t"
  39. "popl %ecx\n\t"
  40. "popl %edx\n\t"
  41. #if defined(CONFIG_FRAME_POINTER)
  42. "movl %ebp,%esp\n\t"
  43. "popl %ebp\n\t"
  44. #endif
  45. "ret"
  46. );
  47. asm(
  48. ".section .sched.text\n"
  49. ".align 4\n"
  50. ".globl __down_failed_interruptible\n"
  51. "__down_failed_interruptible:\n\t"
  52. #if defined(CONFIG_FRAME_POINTER)
  53. "pushl %ebp\n\t"
  54. "movl %esp,%ebp\n\t"
  55. #endif
  56. "pushl %edx\n\t"
  57. "pushl %ecx\n\t"
  58. "call __down_interruptible\n\t"
  59. "popl %ecx\n\t"
  60. "popl %edx\n\t"
  61. #if defined(CONFIG_FRAME_POINTER)
  62. "movl %ebp,%esp\n\t"
  63. "popl %ebp\n\t"
  64. #endif
  65. "ret"
  66. );
  67. asm(
  68. ".section .sched.text\n"
  69. ".align 4\n"
  70. ".globl __down_failed_trylock\n"
  71. "__down_failed_trylock:\n\t"
  72. #if defined(CONFIG_FRAME_POINTER)
  73. "pushl %ebp\n\t"
  74. "movl %esp,%ebp\n\t"
  75. #endif
  76. "pushl %edx\n\t"
  77. "pushl %ecx\n\t"
  78. "call __down_trylock\n\t"
  79. "popl %ecx\n\t"
  80. "popl %edx\n\t"
  81. #if defined(CONFIG_FRAME_POINTER)
  82. "movl %ebp,%esp\n\t"
  83. "popl %ebp\n\t"
  84. #endif
  85. "ret"
  86. );
  87. asm(
  88. ".section .sched.text\n"
  89. ".align 4\n"
  90. ".globl __up_wakeup\n"
  91. "__up_wakeup:\n\t"
  92. "pushl %edx\n\t"
  93. "pushl %ecx\n\t"
  94. "call __up\n\t"
  95. "popl %ecx\n\t"
  96. "popl %edx\n\t"
  97. "ret"
  98. );
  99. /*
  100. * rw spinlock fallbacks
  101. */
  102. #if defined(CONFIG_SMP)
  103. asm(
  104. ".section .sched.text\n"
  105. ".align 4\n"
  106. ".globl __write_lock_failed\n"
  107. "__write_lock_failed:\n\t"
  108. LOCK "addl $" RW_LOCK_BIAS_STR ",(%eax)\n"
  109. "1: rep; nop\n\t"
  110. "cmpl $" RW_LOCK_BIAS_STR ",(%eax)\n\t"
  111. "jne 1b\n\t"
  112. LOCK "subl $" RW_LOCK_BIAS_STR ",(%eax)\n\t"
  113. "jnz __write_lock_failed\n\t"
  114. "ret"
  115. );
  116. asm(
  117. ".section .sched.text\n"
  118. ".align 4\n"
  119. ".globl __read_lock_failed\n"
  120. "__read_lock_failed:\n\t"
  121. LOCK "incl (%eax)\n"
  122. "1: rep; nop\n\t"
  123. "cmpl $1,(%eax)\n\t"
  124. "js 1b\n\t"
  125. LOCK "decl (%eax)\n\t"
  126. "js __read_lock_failed\n\t"
  127. "ret"
  128. );
  129. #endif