debug-exceptions.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <test_util.h>
  3. #include <kvm_util.h>
  4. #include <processor.h>
  5. #define MDSCR_KDE (1 << 13)
  6. #define MDSCR_MDE (1 << 15)
  7. #define MDSCR_SS (1 << 0)
  8. #define DBGBCR_LEN8 (0xff << 5)
  9. #define DBGBCR_EXEC (0x0 << 3)
  10. #define DBGBCR_EL1 (0x1 << 1)
  11. #define DBGBCR_E (0x1 << 0)
  12. #define DBGWCR_LEN8 (0xff << 5)
  13. #define DBGWCR_RD (0x1 << 3)
  14. #define DBGWCR_WR (0x2 << 3)
  15. #define DBGWCR_EL1 (0x1 << 1)
  16. #define DBGWCR_E (0x1 << 0)
  17. #define SPSR_D (1 << 9)
  18. #define SPSR_SS (1 << 21)
  19. extern unsigned char sw_bp, sw_bp2, hw_bp, hw_bp2, bp_svc, bp_brk, hw_wp, ss_start;
  20. extern unsigned char iter_ss_begin, iter_ss_end;
  21. static volatile uint64_t sw_bp_addr, hw_bp_addr;
  22. static volatile uint64_t wp_addr, wp_data_addr;
  23. static volatile uint64_t svc_addr;
  24. static volatile uint64_t ss_addr[4], ss_idx;
  25. #define PC(v) ((uint64_t)&(v))
  26. static void reset_debug_state(void)
  27. {
  28. asm volatile("msr daifset, #8");
  29. write_sysreg(0, osdlr_el1);
  30. write_sysreg(0, oslar_el1);
  31. isb();
  32. write_sysreg(0, mdscr_el1);
  33. /* This test only uses the first bp and wp slot. */
  34. write_sysreg(0, dbgbvr0_el1);
  35. write_sysreg(0, dbgbcr0_el1);
  36. write_sysreg(0, dbgwcr0_el1);
  37. write_sysreg(0, dbgwvr0_el1);
  38. isb();
  39. }
  40. static void enable_os_lock(void)
  41. {
  42. write_sysreg(1, oslar_el1);
  43. isb();
  44. GUEST_ASSERT(read_sysreg(oslsr_el1) & 2);
  45. }
  46. static void install_wp(uint64_t addr)
  47. {
  48. uint32_t wcr;
  49. uint32_t mdscr;
  50. wcr = DBGWCR_LEN8 | DBGWCR_RD | DBGWCR_WR | DBGWCR_EL1 | DBGWCR_E;
  51. write_sysreg(wcr, dbgwcr0_el1);
  52. write_sysreg(addr, dbgwvr0_el1);
  53. isb();
  54. asm volatile("msr daifclr, #8");
  55. mdscr = read_sysreg(mdscr_el1) | MDSCR_KDE | MDSCR_MDE;
  56. write_sysreg(mdscr, mdscr_el1);
  57. isb();
  58. }
  59. static void install_hw_bp(uint64_t addr)
  60. {
  61. uint32_t bcr;
  62. uint32_t mdscr;
  63. bcr = DBGBCR_LEN8 | DBGBCR_EXEC | DBGBCR_EL1 | DBGBCR_E;
  64. write_sysreg(bcr, dbgbcr0_el1);
  65. write_sysreg(addr, dbgbvr0_el1);
  66. isb();
  67. asm volatile("msr daifclr, #8");
  68. mdscr = read_sysreg(mdscr_el1) | MDSCR_KDE | MDSCR_MDE;
  69. write_sysreg(mdscr, mdscr_el1);
  70. isb();
  71. }
  72. static void install_ss(void)
  73. {
  74. uint32_t mdscr;
  75. asm volatile("msr daifclr, #8");
  76. mdscr = read_sysreg(mdscr_el1) | MDSCR_KDE | MDSCR_SS;
  77. write_sysreg(mdscr, mdscr_el1);
  78. isb();
  79. }
  80. static volatile char write_data;
  81. static void guest_code(void)
  82. {
  83. GUEST_SYNC(0);
  84. /* Software-breakpoint */
  85. reset_debug_state();
  86. asm volatile("sw_bp: brk #0");
  87. GUEST_ASSERT_EQ(sw_bp_addr, PC(sw_bp));
  88. GUEST_SYNC(1);
  89. /* Hardware-breakpoint */
  90. reset_debug_state();
  91. install_hw_bp(PC(hw_bp));
  92. asm volatile("hw_bp: nop");
  93. GUEST_ASSERT_EQ(hw_bp_addr, PC(hw_bp));
  94. GUEST_SYNC(2);
  95. /* Hardware-breakpoint + svc */
  96. reset_debug_state();
  97. install_hw_bp(PC(bp_svc));
  98. asm volatile("bp_svc: svc #0");
  99. GUEST_ASSERT_EQ(hw_bp_addr, PC(bp_svc));
  100. GUEST_ASSERT_EQ(svc_addr, PC(bp_svc) + 4);
  101. GUEST_SYNC(3);
  102. /* Hardware-breakpoint + software-breakpoint */
  103. reset_debug_state();
  104. install_hw_bp(PC(bp_brk));
  105. asm volatile("bp_brk: brk #0");
  106. GUEST_ASSERT_EQ(sw_bp_addr, PC(bp_brk));
  107. GUEST_ASSERT_EQ(hw_bp_addr, PC(bp_brk));
  108. GUEST_SYNC(4);
  109. /* Watchpoint */
  110. reset_debug_state();
  111. install_wp(PC(write_data));
  112. write_data = 'x';
  113. GUEST_ASSERT_EQ(write_data, 'x');
  114. GUEST_ASSERT_EQ(wp_data_addr, PC(write_data));
  115. GUEST_SYNC(5);
  116. /* Single-step */
  117. reset_debug_state();
  118. install_ss();
  119. ss_idx = 0;
  120. asm volatile("ss_start:\n"
  121. "mrs x0, esr_el1\n"
  122. "add x0, x0, #1\n"
  123. "msr daifset, #8\n"
  124. : : : "x0");
  125. GUEST_ASSERT_EQ(ss_addr[0], PC(ss_start));
  126. GUEST_ASSERT_EQ(ss_addr[1], PC(ss_start) + 4);
  127. GUEST_ASSERT_EQ(ss_addr[2], PC(ss_start) + 8);
  128. GUEST_SYNC(6);
  129. /* OS Lock does not block software-breakpoint */
  130. reset_debug_state();
  131. enable_os_lock();
  132. sw_bp_addr = 0;
  133. asm volatile("sw_bp2: brk #0");
  134. GUEST_ASSERT_EQ(sw_bp_addr, PC(sw_bp2));
  135. GUEST_SYNC(7);
  136. /* OS Lock blocking hardware-breakpoint */
  137. reset_debug_state();
  138. enable_os_lock();
  139. install_hw_bp(PC(hw_bp2));
  140. hw_bp_addr = 0;
  141. asm volatile("hw_bp2: nop");
  142. GUEST_ASSERT_EQ(hw_bp_addr, 0);
  143. GUEST_SYNC(8);
  144. /* OS Lock blocking watchpoint */
  145. reset_debug_state();
  146. enable_os_lock();
  147. write_data = '\0';
  148. wp_data_addr = 0;
  149. install_wp(PC(write_data));
  150. write_data = 'x';
  151. GUEST_ASSERT_EQ(write_data, 'x');
  152. GUEST_ASSERT_EQ(wp_data_addr, 0);
  153. GUEST_SYNC(9);
  154. /* OS Lock blocking single-step */
  155. reset_debug_state();
  156. enable_os_lock();
  157. ss_addr[0] = 0;
  158. install_ss();
  159. ss_idx = 0;
  160. asm volatile("mrs x0, esr_el1\n\t"
  161. "add x0, x0, #1\n\t"
  162. "msr daifset, #8\n\t"
  163. : : : "x0");
  164. GUEST_ASSERT_EQ(ss_addr[0], 0);
  165. GUEST_DONE();
  166. }
  167. static void guest_sw_bp_handler(struct ex_regs *regs)
  168. {
  169. sw_bp_addr = regs->pc;
  170. regs->pc += 4;
  171. }
  172. static void guest_hw_bp_handler(struct ex_regs *regs)
  173. {
  174. hw_bp_addr = regs->pc;
  175. regs->pstate |= SPSR_D;
  176. }
  177. static void guest_wp_handler(struct ex_regs *regs)
  178. {
  179. wp_data_addr = read_sysreg(far_el1);
  180. wp_addr = regs->pc;
  181. regs->pstate |= SPSR_D;
  182. }
  183. static void guest_ss_handler(struct ex_regs *regs)
  184. {
  185. GUEST_ASSERT_1(ss_idx < 4, ss_idx);
  186. ss_addr[ss_idx++] = regs->pc;
  187. regs->pstate |= SPSR_SS;
  188. }
  189. static void guest_svc_handler(struct ex_regs *regs)
  190. {
  191. svc_addr = regs->pc;
  192. }
  193. enum single_step_op {
  194. SINGLE_STEP_ENABLE = 0,
  195. SINGLE_STEP_DISABLE = 1,
  196. };
  197. static void guest_code_ss(int test_cnt)
  198. {
  199. uint64_t i;
  200. uint64_t bvr, wvr, w_bvr, w_wvr;
  201. for (i = 0; i < test_cnt; i++) {
  202. /* Bits [1:0] of dbg{b,w}vr are RES0 */
  203. w_bvr = i << 2;
  204. w_wvr = i << 2;
  205. /* Enable Single Step execution */
  206. GUEST_SYNC(SINGLE_STEP_ENABLE);
  207. /*
  208. * The userspace will veriry that the pc is as expected during
  209. * single step execution between iter_ss_begin and iter_ss_end.
  210. */
  211. asm volatile("iter_ss_begin:nop\n");
  212. write_sysreg(w_bvr, dbgbvr0_el1);
  213. write_sysreg(w_wvr, dbgwvr0_el1);
  214. bvr = read_sysreg(dbgbvr0_el1);
  215. wvr = read_sysreg(dbgwvr0_el1);
  216. asm volatile("iter_ss_end:\n");
  217. /* Disable Single Step execution */
  218. GUEST_SYNC(SINGLE_STEP_DISABLE);
  219. GUEST_ASSERT(bvr == w_bvr);
  220. GUEST_ASSERT(wvr == w_wvr);
  221. }
  222. GUEST_DONE();
  223. }
  224. static int debug_version(struct kvm_vcpu *vcpu)
  225. {
  226. uint64_t id_aa64dfr0;
  227. vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64DFR0_EL1), &id_aa64dfr0);
  228. return id_aa64dfr0 & 0xf;
  229. }
  230. static void test_guest_debug_exceptions(void)
  231. {
  232. struct kvm_vcpu *vcpu;
  233. struct kvm_vm *vm;
  234. struct ucall uc;
  235. int stage;
  236. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  237. ucall_init(vm, NULL);
  238. vm_init_descriptor_tables(vm);
  239. vcpu_init_descriptor_tables(vcpu);
  240. vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
  241. ESR_EC_BRK_INS, guest_sw_bp_handler);
  242. vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
  243. ESR_EC_HW_BP_CURRENT, guest_hw_bp_handler);
  244. vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
  245. ESR_EC_WP_CURRENT, guest_wp_handler);
  246. vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
  247. ESR_EC_SSTEP_CURRENT, guest_ss_handler);
  248. vm_install_sync_handler(vm, VECTOR_SYNC_CURRENT,
  249. ESR_EC_SVC64, guest_svc_handler);
  250. for (stage = 0; stage < 11; stage++) {
  251. vcpu_run(vcpu);
  252. switch (get_ucall(vcpu, &uc)) {
  253. case UCALL_SYNC:
  254. TEST_ASSERT(uc.args[1] == stage,
  255. "Stage %d: Unexpected sync ucall, got %lx",
  256. stage, (ulong)uc.args[1]);
  257. break;
  258. case UCALL_ABORT:
  259. REPORT_GUEST_ASSERT_2(uc, "values: %#lx, %#lx");
  260. break;
  261. case UCALL_DONE:
  262. goto done;
  263. default:
  264. TEST_FAIL("Unknown ucall %lu", uc.cmd);
  265. }
  266. }
  267. done:
  268. kvm_vm_free(vm);
  269. }
  270. void test_single_step_from_userspace(int test_cnt)
  271. {
  272. struct kvm_vcpu *vcpu;
  273. struct kvm_vm *vm;
  274. struct ucall uc;
  275. struct kvm_run *run;
  276. uint64_t pc, cmd;
  277. uint64_t test_pc = 0;
  278. bool ss_enable = false;
  279. struct kvm_guest_debug debug = {};
  280. vm = vm_create_with_one_vcpu(&vcpu, guest_code_ss);
  281. ucall_init(vm, NULL);
  282. run = vcpu->run;
  283. vcpu_args_set(vcpu, 1, test_cnt);
  284. while (1) {
  285. vcpu_run(vcpu);
  286. if (run->exit_reason != KVM_EXIT_DEBUG) {
  287. cmd = get_ucall(vcpu, &uc);
  288. if (cmd == UCALL_ABORT) {
  289. REPORT_GUEST_ASSERT(uc);
  290. /* NOT REACHED */
  291. } else if (cmd == UCALL_DONE) {
  292. break;
  293. }
  294. TEST_ASSERT(cmd == UCALL_SYNC,
  295. "Unexpected ucall cmd 0x%lx", cmd);
  296. if (uc.args[1] == SINGLE_STEP_ENABLE) {
  297. debug.control = KVM_GUESTDBG_ENABLE |
  298. KVM_GUESTDBG_SINGLESTEP;
  299. ss_enable = true;
  300. } else {
  301. debug.control = SINGLE_STEP_DISABLE;
  302. ss_enable = false;
  303. }
  304. vcpu_guest_debug_set(vcpu, &debug);
  305. continue;
  306. }
  307. TEST_ASSERT(ss_enable, "Unexpected KVM_EXIT_DEBUG");
  308. /* Check if the current pc is expected. */
  309. vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pc), &pc);
  310. TEST_ASSERT(!test_pc || pc == test_pc,
  311. "Unexpected pc 0x%lx (expected 0x%lx)",
  312. pc, test_pc);
  313. /*
  314. * If the current pc is between iter_ss_bgin and
  315. * iter_ss_end, the pc for the next KVM_EXIT_DEBUG should
  316. * be the current pc + 4.
  317. */
  318. if ((pc >= (uint64_t)&iter_ss_begin) &&
  319. (pc < (uint64_t)&iter_ss_end))
  320. test_pc = pc + 4;
  321. else
  322. test_pc = 0;
  323. }
  324. kvm_vm_free(vm);
  325. }
  326. static void help(char *name)
  327. {
  328. puts("");
  329. printf("Usage: %s [-h] [-i iterations of the single step test]\n", name);
  330. puts("");
  331. exit(0);
  332. }
  333. int main(int argc, char *argv[])
  334. {
  335. struct kvm_vcpu *vcpu;
  336. struct kvm_vm *vm;
  337. int opt;
  338. int ss_iteration = 10000;
  339. vm = vm_create_with_one_vcpu(&vcpu, guest_code);
  340. __TEST_REQUIRE(debug_version(vcpu) >= 6,
  341. "Armv8 debug architecture not supported.");
  342. kvm_vm_free(vm);
  343. while ((opt = getopt(argc, argv, "i:")) != -1) {
  344. switch (opt) {
  345. case 'i':
  346. ss_iteration = atoi(optarg);
  347. break;
  348. case 'h':
  349. default:
  350. help(argv[0]);
  351. break;
  352. }
  353. }
  354. test_guest_debug_exceptions();
  355. test_single_step_from_userspace(ss_iteration);
  356. return 0;
  357. }