tas.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "libc.h"
  11. /*
  12. * first argument (l) is in r3 at entry.
  13. * r3 contains return value upon return.
  14. */
  15. int
  16. tas(int32_t *x)
  17. {
  18. int v;
  19. /*
  20. * this __asm__ works with gcc 2.95.2 (mac os x 10.1).
  21. * this assembly language destroys r0 (0), some other register (v),
  22. * r4 (x) and r5 (temp).
  23. */
  24. __asm__("\n sync\n"
  25. " li r0,0\n"
  26. " mr r4,%1 /* &l->val */\n"
  27. " lis r5,0xdead /* assemble constant 0xdeaddead */\n"
  28. " ori r5,r5,0xdead /* \" */\n"
  29. "tas1:\n"
  30. " dcbf r4,r0 /* cache flush; \"fix for 603x bug\" */\n"
  31. " lwarx %0,r4,r0 /* v = l->val with reservation */\n"
  32. " cmp cr0,0,%0,r0 /* v == 0 */\n"
  33. " bne tas0\n"
  34. " stwcx. r5,r4,r0 /* if (l->val same) l->val = 0xdeaddead */\n"
  35. " bne tas1\n"
  36. "tas0:\n"
  37. " sync\n"
  38. " isync\n"
  39. : "=r" (v)
  40. : "r" (x)
  41. : "cc", "memory", "r0", "r4", "r5"
  42. );
  43. switch(v) {
  44. case 0: return 0;
  45. case 0xdeaddead: return 1;
  46. default: print("tas: corrupted 0x%lux\n", v);
  47. }
  48. return 0;
  49. }