watch.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 David Daney
  7. */
  8. #include <linux/sched.h>
  9. #include <asm/processor.h>
  10. #include <asm/watch.h>
  11. /*
  12. * Install the watch registers for the current thread. A maximum of
  13. * four registers are installed although the machine may have more.
  14. */
  15. void mips_install_watch_registers(struct task_struct *t)
  16. {
  17. struct mips3264_watch_reg_state *watches = &t->thread.watch.mips3264;
  18. unsigned int watchhi = MIPS_WATCHHI_G | /* Trap all ASIDs */
  19. MIPS_WATCHHI_IRW; /* Clear result bits */
  20. switch (current_cpu_data.watch_reg_use_cnt) {
  21. default:
  22. BUG();
  23. case 4:
  24. write_c0_watchlo3(watches->watchlo[3]);
  25. write_c0_watchhi3(watchhi | watches->watchhi[3]);
  26. case 3:
  27. write_c0_watchlo2(watches->watchlo[2]);
  28. write_c0_watchhi2(watchhi | watches->watchhi[2]);
  29. case 2:
  30. write_c0_watchlo1(watches->watchlo[1]);
  31. write_c0_watchhi1(watchhi | watches->watchhi[1]);
  32. case 1:
  33. write_c0_watchlo0(watches->watchlo[0]);
  34. write_c0_watchhi0(watchhi | watches->watchhi[0]);
  35. }
  36. }
  37. /*
  38. * Read back the watchhi registers so the user space debugger has
  39. * access to the I, R, and W bits. A maximum of four registers are
  40. * read although the machine may have more.
  41. */
  42. void mips_read_watch_registers(void)
  43. {
  44. struct mips3264_watch_reg_state *watches =
  45. &current->thread.watch.mips3264;
  46. unsigned int watchhi_mask = MIPS_WATCHHI_MASK | MIPS_WATCHHI_IRW;
  47. switch (current_cpu_data.watch_reg_use_cnt) {
  48. default:
  49. BUG();
  50. case 4:
  51. watches->watchhi[3] = (read_c0_watchhi3() & watchhi_mask);
  52. case 3:
  53. watches->watchhi[2] = (read_c0_watchhi2() & watchhi_mask);
  54. case 2:
  55. watches->watchhi[1] = (read_c0_watchhi1() & watchhi_mask);
  56. case 1:
  57. watches->watchhi[0] = (read_c0_watchhi0() & watchhi_mask);
  58. }
  59. if (current_cpu_data.watch_reg_use_cnt == 1 &&
  60. (watches->watchhi[0] & MIPS_WATCHHI_IRW) == 0) {
  61. /* Pathological case of release 1 architecture that
  62. * doesn't set the condition bits. We assume that
  63. * since we got here, the watch condition was met and
  64. * signal that the conditions requested in watchlo
  65. * were met. */
  66. watches->watchhi[0] |= (watches->watchlo[0] & MIPS_WATCHHI_IRW);
  67. }
  68. }
  69. /*
  70. * Disable all watch registers. Although only four registers are
  71. * installed, all are cleared to eliminate the possibility of endless
  72. * looping in the watch handler.
  73. */
  74. void mips_clear_watch_registers(void)
  75. {
  76. switch (current_cpu_data.watch_reg_count) {
  77. default:
  78. BUG();
  79. case 8:
  80. write_c0_watchlo7(0);
  81. case 7:
  82. write_c0_watchlo6(0);
  83. case 6:
  84. write_c0_watchlo5(0);
  85. case 5:
  86. write_c0_watchlo4(0);
  87. case 4:
  88. write_c0_watchlo3(0);
  89. case 3:
  90. write_c0_watchlo2(0);
  91. case 2:
  92. write_c0_watchlo1(0);
  93. case 1:
  94. write_c0_watchlo0(0);
  95. }
  96. }
  97. void mips_probe_watch_registers(struct cpuinfo_mips *c)
  98. {
  99. unsigned int t;
  100. if ((c->options & MIPS_CPU_WATCH) == 0)
  101. return;
  102. /*
  103. * Check which of the I,R and W bits are supported, then
  104. * disable the register.
  105. */
  106. write_c0_watchlo0(MIPS_WATCHLO_IRW);
  107. back_to_back_c0_hazard();
  108. t = read_c0_watchlo0();
  109. write_c0_watchlo0(0);
  110. c->watch_reg_masks[0] = t & MIPS_WATCHLO_IRW;
  111. /* Write the mask bits and read them back to determine which
  112. * can be used. */
  113. c->watch_reg_count = 1;
  114. c->watch_reg_use_cnt = 1;
  115. t = read_c0_watchhi0();
  116. write_c0_watchhi0(t | MIPS_WATCHHI_MASK);
  117. back_to_back_c0_hazard();
  118. t = read_c0_watchhi0();
  119. c->watch_reg_masks[0] |= (t & MIPS_WATCHHI_MASK);
  120. if ((t & MIPS_WATCHHI_M) == 0)
  121. return;
  122. write_c0_watchlo1(MIPS_WATCHLO_IRW);
  123. back_to_back_c0_hazard();
  124. t = read_c0_watchlo1();
  125. write_c0_watchlo1(0);
  126. c->watch_reg_masks[1] = t & MIPS_WATCHLO_IRW;
  127. c->watch_reg_count = 2;
  128. c->watch_reg_use_cnt = 2;
  129. t = read_c0_watchhi1();
  130. write_c0_watchhi1(t | MIPS_WATCHHI_MASK);
  131. back_to_back_c0_hazard();
  132. t = read_c0_watchhi1();
  133. c->watch_reg_masks[1] |= (t & MIPS_WATCHHI_MASK);
  134. if ((t & MIPS_WATCHHI_M) == 0)
  135. return;
  136. write_c0_watchlo2(MIPS_WATCHLO_IRW);
  137. back_to_back_c0_hazard();
  138. t = read_c0_watchlo2();
  139. write_c0_watchlo2(0);
  140. c->watch_reg_masks[2] = t & MIPS_WATCHLO_IRW;
  141. c->watch_reg_count = 3;
  142. c->watch_reg_use_cnt = 3;
  143. t = read_c0_watchhi2();
  144. write_c0_watchhi2(t | MIPS_WATCHHI_MASK);
  145. back_to_back_c0_hazard();
  146. t = read_c0_watchhi2();
  147. c->watch_reg_masks[2] |= (t & MIPS_WATCHHI_MASK);
  148. if ((t & MIPS_WATCHHI_M) == 0)
  149. return;
  150. write_c0_watchlo3(MIPS_WATCHLO_IRW);
  151. back_to_back_c0_hazard();
  152. t = read_c0_watchlo3();
  153. write_c0_watchlo3(0);
  154. c->watch_reg_masks[3] = t & MIPS_WATCHLO_IRW;
  155. c->watch_reg_count = 4;
  156. c->watch_reg_use_cnt = 4;
  157. t = read_c0_watchhi3();
  158. write_c0_watchhi3(t | MIPS_WATCHHI_MASK);
  159. back_to_back_c0_hazard();
  160. t = read_c0_watchhi3();
  161. c->watch_reg_masks[3] |= (t & MIPS_WATCHHI_MASK);
  162. if ((t & MIPS_WATCHHI_M) == 0)
  163. return;
  164. /* We use at most 4, but probe and report up to 8. */
  165. c->watch_reg_count = 5;
  166. t = read_c0_watchhi4();
  167. if ((t & MIPS_WATCHHI_M) == 0)
  168. return;
  169. c->watch_reg_count = 6;
  170. t = read_c0_watchhi5();
  171. if ((t & MIPS_WATCHHI_M) == 0)
  172. return;
  173. c->watch_reg_count = 7;
  174. t = read_c0_watchhi6();
  175. if ((t & MIPS_WATCHHI_M) == 0)
  176. return;
  177. c->watch_reg_count = 8;
  178. }