watch.c 4.7 KB

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