watch.c 5.3 KB

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