guestdbg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * kvm guest debug support
  3. *
  4. * Copyright IBM Corp. 2014
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): David Hildenbrand <dahi@linux.vnet.ibm.com>
  11. */
  12. #include <linux/kvm_host.h>
  13. #include <linux/errno.h>
  14. #include "kvm-s390.h"
  15. #include "gaccess.h"
  16. /*
  17. * Extends the address range given by *start and *stop to include the address
  18. * range starting with estart and the length len. Takes care of overflowing
  19. * intervals and tries to minimize the overall interval size.
  20. */
  21. static void extend_address_range(u64 *start, u64 *stop, u64 estart, int len)
  22. {
  23. u64 estop;
  24. if (len > 0)
  25. len--;
  26. else
  27. len = 0;
  28. estop = estart + len;
  29. /* 0-0 range represents "not set" */
  30. if ((*start == 0) && (*stop == 0)) {
  31. *start = estart;
  32. *stop = estop;
  33. } else if (*start <= *stop) {
  34. /* increase the existing range */
  35. if (estart < *start)
  36. *start = estart;
  37. if (estop > *stop)
  38. *stop = estop;
  39. } else {
  40. /* "overflowing" interval, whereby *stop > *start */
  41. if (estart <= *stop) {
  42. if (estop > *stop)
  43. *stop = estop;
  44. } else if (estop > *start) {
  45. if (estart < *start)
  46. *start = estart;
  47. }
  48. /* minimize the range */
  49. else if ((estop - *stop) < (*start - estart))
  50. *stop = estop;
  51. else
  52. *start = estart;
  53. }
  54. }
  55. #define MAX_INST_SIZE 6
  56. static void enable_all_hw_bp(struct kvm_vcpu *vcpu)
  57. {
  58. unsigned long start, len;
  59. u64 *cr9 = &vcpu->arch.sie_block->gcr[9];
  60. u64 *cr10 = &vcpu->arch.sie_block->gcr[10];
  61. u64 *cr11 = &vcpu->arch.sie_block->gcr[11];
  62. int i;
  63. if (vcpu->arch.guestdbg.nr_hw_bp <= 0 ||
  64. vcpu->arch.guestdbg.hw_bp_info == NULL)
  65. return;
  66. /*
  67. * If the guest is not interested in branching events, we can safely
  68. * limit them to the PER address range.
  69. */
  70. if (!(*cr9 & PER_EVENT_BRANCH))
  71. *cr9 |= PER_CONTROL_BRANCH_ADDRESS;
  72. *cr9 |= PER_EVENT_IFETCH | PER_EVENT_BRANCH;
  73. for (i = 0; i < vcpu->arch.guestdbg.nr_hw_bp; i++) {
  74. start = vcpu->arch.guestdbg.hw_bp_info[i].addr;
  75. len = vcpu->arch.guestdbg.hw_bp_info[i].len;
  76. /*
  77. * The instruction in front of the desired bp has to
  78. * report instruction-fetching events
  79. */
  80. if (start < MAX_INST_SIZE) {
  81. len += start;
  82. start = 0;
  83. } else {
  84. start -= MAX_INST_SIZE;
  85. len += MAX_INST_SIZE;
  86. }
  87. extend_address_range(cr10, cr11, start, len);
  88. }
  89. }
  90. static void enable_all_hw_wp(struct kvm_vcpu *vcpu)
  91. {
  92. unsigned long start, len;
  93. u64 *cr9 = &vcpu->arch.sie_block->gcr[9];
  94. u64 *cr10 = &vcpu->arch.sie_block->gcr[10];
  95. u64 *cr11 = &vcpu->arch.sie_block->gcr[11];
  96. int i;
  97. if (vcpu->arch.guestdbg.nr_hw_wp <= 0 ||
  98. vcpu->arch.guestdbg.hw_wp_info == NULL)
  99. return;
  100. /* if host uses storage alternation for special address
  101. * spaces, enable all events and give all to the guest */
  102. if (*cr9 & PER_EVENT_STORE && *cr9 & PER_CONTROL_ALTERATION) {
  103. *cr9 &= ~PER_CONTROL_ALTERATION;
  104. *cr10 = 0;
  105. *cr11 = -1UL;
  106. } else {
  107. *cr9 &= ~PER_CONTROL_ALTERATION;
  108. *cr9 |= PER_EVENT_STORE;
  109. for (i = 0; i < vcpu->arch.guestdbg.nr_hw_wp; i++) {
  110. start = vcpu->arch.guestdbg.hw_wp_info[i].addr;
  111. len = vcpu->arch.guestdbg.hw_wp_info[i].len;
  112. extend_address_range(cr10, cr11, start, len);
  113. }
  114. }
  115. }
  116. void kvm_s390_backup_guest_per_regs(struct kvm_vcpu *vcpu)
  117. {
  118. vcpu->arch.guestdbg.cr0 = vcpu->arch.sie_block->gcr[0];
  119. vcpu->arch.guestdbg.cr9 = vcpu->arch.sie_block->gcr[9];
  120. vcpu->arch.guestdbg.cr10 = vcpu->arch.sie_block->gcr[10];
  121. vcpu->arch.guestdbg.cr11 = vcpu->arch.sie_block->gcr[11];
  122. }
  123. void kvm_s390_restore_guest_per_regs(struct kvm_vcpu *vcpu)
  124. {
  125. vcpu->arch.sie_block->gcr[0] = vcpu->arch.guestdbg.cr0;
  126. vcpu->arch.sie_block->gcr[9] = vcpu->arch.guestdbg.cr9;
  127. vcpu->arch.sie_block->gcr[10] = vcpu->arch.guestdbg.cr10;
  128. vcpu->arch.sie_block->gcr[11] = vcpu->arch.guestdbg.cr11;
  129. }
  130. void kvm_s390_patch_guest_per_regs(struct kvm_vcpu *vcpu)
  131. {
  132. /*
  133. * TODO: if guest psw has per enabled, otherwise 0s!
  134. * This reduces the amount of reported events.
  135. * Need to intercept all psw changes!
  136. */
  137. if (guestdbg_sstep_enabled(vcpu)) {
  138. /* disable timer (clock-comparator) interrupts */
  139. vcpu->arch.sie_block->gcr[0] &= ~0x800ul;
  140. vcpu->arch.sie_block->gcr[9] |= PER_EVENT_IFETCH;
  141. vcpu->arch.sie_block->gcr[10] = 0;
  142. vcpu->arch.sie_block->gcr[11] = -1UL;
  143. }
  144. if (guestdbg_hw_bp_enabled(vcpu)) {
  145. enable_all_hw_bp(vcpu);
  146. enable_all_hw_wp(vcpu);
  147. }
  148. /* TODO: Instruction-fetching-nullification not allowed for now */
  149. if (vcpu->arch.sie_block->gcr[9] & PER_EVENT_NULLIFICATION)
  150. vcpu->arch.sie_block->gcr[9] &= ~PER_EVENT_NULLIFICATION;
  151. }
  152. #define MAX_WP_SIZE 100
  153. static int __import_wp_info(struct kvm_vcpu *vcpu,
  154. struct kvm_hw_breakpoint *bp_data,
  155. struct kvm_hw_wp_info_arch *wp_info)
  156. {
  157. int ret = 0;
  158. wp_info->len = bp_data->len;
  159. wp_info->addr = bp_data->addr;
  160. wp_info->phys_addr = bp_data->phys_addr;
  161. wp_info->old_data = NULL;
  162. if (wp_info->len < 0 || wp_info->len > MAX_WP_SIZE)
  163. return -EINVAL;
  164. wp_info->old_data = kmalloc(bp_data->len, GFP_KERNEL);
  165. if (!wp_info->old_data)
  166. return -ENOMEM;
  167. /* try to backup the original value */
  168. ret = read_guest_abs(vcpu, wp_info->phys_addr, wp_info->old_data,
  169. wp_info->len);
  170. if (ret) {
  171. kfree(wp_info->old_data);
  172. wp_info->old_data = NULL;
  173. }
  174. return ret;
  175. }
  176. #define MAX_BP_COUNT 50
  177. int kvm_s390_import_bp_data(struct kvm_vcpu *vcpu,
  178. struct kvm_guest_debug *dbg)
  179. {
  180. int ret = 0, nr_wp = 0, nr_bp = 0, i;
  181. struct kvm_hw_breakpoint *bp_data = NULL;
  182. struct kvm_hw_wp_info_arch *wp_info = NULL;
  183. struct kvm_hw_bp_info_arch *bp_info = NULL;
  184. if (dbg->arch.nr_hw_bp <= 0 || !dbg->arch.hw_bp)
  185. return 0;
  186. else if (dbg->arch.nr_hw_bp > MAX_BP_COUNT)
  187. return -EINVAL;
  188. bp_data = memdup_user(dbg->arch.hw_bp,
  189. sizeof(*bp_data) * dbg->arch.nr_hw_bp);
  190. if (IS_ERR(bp_data))
  191. return PTR_ERR(bp_data);
  192. for (i = 0; i < dbg->arch.nr_hw_bp; i++) {
  193. switch (bp_data[i].type) {
  194. case KVM_HW_WP_WRITE:
  195. nr_wp++;
  196. break;
  197. case KVM_HW_BP:
  198. nr_bp++;
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. if (nr_wp > 0) {
  205. wp_info = kmalloc_array(nr_wp,
  206. sizeof(*wp_info),
  207. GFP_KERNEL);
  208. if (!wp_info) {
  209. ret = -ENOMEM;
  210. goto error;
  211. }
  212. }
  213. if (nr_bp > 0) {
  214. bp_info = kmalloc_array(nr_bp,
  215. sizeof(*bp_info),
  216. GFP_KERNEL);
  217. if (!bp_info) {
  218. ret = -ENOMEM;
  219. goto error;
  220. }
  221. }
  222. for (nr_wp = 0, nr_bp = 0, i = 0; i < dbg->arch.nr_hw_bp; i++) {
  223. switch (bp_data[i].type) {
  224. case KVM_HW_WP_WRITE:
  225. ret = __import_wp_info(vcpu, &bp_data[i],
  226. &wp_info[nr_wp]);
  227. if (ret)
  228. goto error;
  229. nr_wp++;
  230. break;
  231. case KVM_HW_BP:
  232. bp_info[nr_bp].len = bp_data[i].len;
  233. bp_info[nr_bp].addr = bp_data[i].addr;
  234. nr_bp++;
  235. break;
  236. }
  237. }
  238. vcpu->arch.guestdbg.nr_hw_bp = nr_bp;
  239. vcpu->arch.guestdbg.hw_bp_info = bp_info;
  240. vcpu->arch.guestdbg.nr_hw_wp = nr_wp;
  241. vcpu->arch.guestdbg.hw_wp_info = wp_info;
  242. return 0;
  243. error:
  244. kfree(bp_data);
  245. kfree(wp_info);
  246. kfree(bp_info);
  247. return ret;
  248. }
  249. void kvm_s390_clear_bp_data(struct kvm_vcpu *vcpu)
  250. {
  251. int i;
  252. struct kvm_hw_wp_info_arch *hw_wp_info = NULL;
  253. for (i = 0; i < vcpu->arch.guestdbg.nr_hw_wp; i++) {
  254. hw_wp_info = &vcpu->arch.guestdbg.hw_wp_info[i];
  255. kfree(hw_wp_info->old_data);
  256. hw_wp_info->old_data = NULL;
  257. }
  258. kfree(vcpu->arch.guestdbg.hw_wp_info);
  259. vcpu->arch.guestdbg.hw_wp_info = NULL;
  260. kfree(vcpu->arch.guestdbg.hw_bp_info);
  261. vcpu->arch.guestdbg.hw_bp_info = NULL;
  262. vcpu->arch.guestdbg.nr_hw_wp = 0;
  263. vcpu->arch.guestdbg.nr_hw_bp = 0;
  264. }
  265. static inline int in_addr_range(u64 addr, u64 a, u64 b)
  266. {
  267. if (a <= b)
  268. return (addr >= a) && (addr <= b);
  269. else
  270. /* "overflowing" interval */
  271. return (addr <= a) && (addr >= b);
  272. }
  273. #define end_of_range(bp_info) (bp_info->addr + bp_info->len - 1)
  274. static struct kvm_hw_bp_info_arch *find_hw_bp(struct kvm_vcpu *vcpu,
  275. unsigned long addr)
  276. {
  277. struct kvm_hw_bp_info_arch *bp_info = vcpu->arch.guestdbg.hw_bp_info;
  278. int i;
  279. if (vcpu->arch.guestdbg.nr_hw_bp == 0)
  280. return NULL;
  281. for (i = 0; i < vcpu->arch.guestdbg.nr_hw_bp; i++) {
  282. /* addr is directly the start or in the range of a bp */
  283. if (addr == bp_info->addr)
  284. goto found;
  285. if (bp_info->len > 0 &&
  286. in_addr_range(addr, bp_info->addr, end_of_range(bp_info)))
  287. goto found;
  288. bp_info++;
  289. }
  290. return NULL;
  291. found:
  292. return bp_info;
  293. }
  294. static struct kvm_hw_wp_info_arch *any_wp_changed(struct kvm_vcpu *vcpu)
  295. {
  296. int i;
  297. struct kvm_hw_wp_info_arch *wp_info = NULL;
  298. void *temp = NULL;
  299. if (vcpu->arch.guestdbg.nr_hw_wp == 0)
  300. return NULL;
  301. for (i = 0; i < vcpu->arch.guestdbg.nr_hw_wp; i++) {
  302. wp_info = &vcpu->arch.guestdbg.hw_wp_info[i];
  303. if (!wp_info || !wp_info->old_data || wp_info->len <= 0)
  304. continue;
  305. temp = kmalloc(wp_info->len, GFP_KERNEL);
  306. if (!temp)
  307. continue;
  308. /* refetch the wp data and compare it to the old value */
  309. if (!read_guest_abs(vcpu, wp_info->phys_addr, temp,
  310. wp_info->len)) {
  311. if (memcmp(temp, wp_info->old_data, wp_info->len)) {
  312. kfree(temp);
  313. return wp_info;
  314. }
  315. }
  316. kfree(temp);
  317. temp = NULL;
  318. }
  319. return NULL;
  320. }
  321. void kvm_s390_prepare_debug_exit(struct kvm_vcpu *vcpu)
  322. {
  323. vcpu->run->exit_reason = KVM_EXIT_DEBUG;
  324. vcpu->guest_debug &= ~KVM_GUESTDBG_EXIT_PENDING;
  325. }
  326. #define PER_CODE_MASK (PER_EVENT_MASK >> 24)
  327. #define PER_CODE_BRANCH (PER_EVENT_BRANCH >> 24)
  328. #define PER_CODE_IFETCH (PER_EVENT_IFETCH >> 24)
  329. #define PER_CODE_STORE (PER_EVENT_STORE >> 24)
  330. #define PER_CODE_STORE_REAL (PER_EVENT_STORE_REAL >> 24)
  331. #define per_bp_event(code) \
  332. (code & (PER_CODE_IFETCH | PER_CODE_BRANCH))
  333. #define per_write_wp_event(code) \
  334. (code & (PER_CODE_STORE | PER_CODE_STORE_REAL))
  335. static int debug_exit_required(struct kvm_vcpu *vcpu)
  336. {
  337. u8 perc = vcpu->arch.sie_block->perc;
  338. struct kvm_debug_exit_arch *debug_exit = &vcpu->run->debug.arch;
  339. struct kvm_hw_wp_info_arch *wp_info = NULL;
  340. struct kvm_hw_bp_info_arch *bp_info = NULL;
  341. unsigned long addr = vcpu->arch.sie_block->gpsw.addr;
  342. unsigned long peraddr = vcpu->arch.sie_block->peraddr;
  343. if (guestdbg_hw_bp_enabled(vcpu)) {
  344. if (per_write_wp_event(perc) &&
  345. vcpu->arch.guestdbg.nr_hw_wp > 0) {
  346. wp_info = any_wp_changed(vcpu);
  347. if (wp_info) {
  348. debug_exit->addr = wp_info->addr;
  349. debug_exit->type = KVM_HW_WP_WRITE;
  350. goto exit_required;
  351. }
  352. }
  353. if (per_bp_event(perc) &&
  354. vcpu->arch.guestdbg.nr_hw_bp > 0) {
  355. bp_info = find_hw_bp(vcpu, addr);
  356. /* remove duplicate events if PC==PER address */
  357. if (bp_info && (addr != peraddr)) {
  358. debug_exit->addr = addr;
  359. debug_exit->type = KVM_HW_BP;
  360. vcpu->arch.guestdbg.last_bp = addr;
  361. goto exit_required;
  362. }
  363. /* breakpoint missed */
  364. bp_info = find_hw_bp(vcpu, peraddr);
  365. if (bp_info && vcpu->arch.guestdbg.last_bp != peraddr) {
  366. debug_exit->addr = peraddr;
  367. debug_exit->type = KVM_HW_BP;
  368. goto exit_required;
  369. }
  370. }
  371. }
  372. if (guestdbg_sstep_enabled(vcpu) && per_bp_event(perc)) {
  373. debug_exit->addr = addr;
  374. debug_exit->type = KVM_SINGLESTEP;
  375. goto exit_required;
  376. }
  377. return 0;
  378. exit_required:
  379. return 1;
  380. }
  381. #define guest_per_enabled(vcpu) \
  382. (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER)
  383. int kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu *vcpu)
  384. {
  385. const u8 ilen = kvm_s390_get_ilen(vcpu);
  386. struct kvm_s390_pgm_info pgm_info = {
  387. .code = PGM_PER,
  388. .per_code = PER_CODE_IFETCH,
  389. .per_address = __rewind_psw(vcpu->arch.sie_block->gpsw, ilen),
  390. };
  391. /*
  392. * The PSW points to the next instruction, therefore the intercepted
  393. * instruction generated a PER i-fetch event. PER address therefore
  394. * points at the previous PSW address (could be an EXECUTE function).
  395. */
  396. return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
  397. }
  398. static void filter_guest_per_event(struct kvm_vcpu *vcpu)
  399. {
  400. const u8 perc = vcpu->arch.sie_block->perc;
  401. u64 peraddr = vcpu->arch.sie_block->peraddr;
  402. u64 addr = vcpu->arch.sie_block->gpsw.addr;
  403. u64 cr9 = vcpu->arch.sie_block->gcr[9];
  404. u64 cr10 = vcpu->arch.sie_block->gcr[10];
  405. u64 cr11 = vcpu->arch.sie_block->gcr[11];
  406. /* filter all events, demanded by the guest */
  407. u8 guest_perc = perc & (cr9 >> 24) & PER_CODE_MASK;
  408. if (!guest_per_enabled(vcpu))
  409. guest_perc = 0;
  410. /* filter "successful-branching" events */
  411. if (guest_perc & PER_CODE_BRANCH &&
  412. cr9 & PER_CONTROL_BRANCH_ADDRESS &&
  413. !in_addr_range(addr, cr10, cr11))
  414. guest_perc &= ~PER_CODE_BRANCH;
  415. /* filter "instruction-fetching" events */
  416. if (guest_perc & PER_CODE_IFETCH &&
  417. !in_addr_range(peraddr, cr10, cr11))
  418. guest_perc &= ~PER_CODE_IFETCH;
  419. /* All other PER events will be given to the guest */
  420. /* TODO: Check altered address/address space */
  421. vcpu->arch.sie_block->perc = guest_perc;
  422. if (!guest_perc)
  423. vcpu->arch.sie_block->iprcc &= ~PGM_PER;
  424. }
  425. #define pssec(vcpu) (vcpu->arch.sie_block->gcr[1] & _ASCE_SPACE_SWITCH)
  426. #define hssec(vcpu) (vcpu->arch.sie_block->gcr[13] & _ASCE_SPACE_SWITCH)
  427. #define old_ssec(vcpu) ((vcpu->arch.sie_block->tecmc >> 31) & 0x1)
  428. #define old_as_is_home(vcpu) !(vcpu->arch.sie_block->tecmc & 0xffff)
  429. void kvm_s390_handle_per_event(struct kvm_vcpu *vcpu)
  430. {
  431. int new_as;
  432. if (debug_exit_required(vcpu))
  433. vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
  434. filter_guest_per_event(vcpu);
  435. /*
  436. * Only RP, SAC, SACF, PT, PTI, PR, PC instructions can trigger
  437. * a space-switch event. PER events enforce space-switch events
  438. * for these instructions. So if no PER event for the guest is left,
  439. * we might have to filter the space-switch element out, too.
  440. */
  441. if (vcpu->arch.sie_block->iprcc == PGM_SPACE_SWITCH) {
  442. vcpu->arch.sie_block->iprcc = 0;
  443. new_as = psw_bits(vcpu->arch.sie_block->gpsw).as;
  444. /*
  445. * If the AS changed from / to home, we had RP, SAC or SACF
  446. * instruction. Check primary and home space-switch-event
  447. * controls. (theoretically home -> home produced no event)
  448. */
  449. if (((new_as == PSW_AS_HOME) ^ old_as_is_home(vcpu)) &&
  450. (pssec(vcpu) || hssec(vcpu)))
  451. vcpu->arch.sie_block->iprcc = PGM_SPACE_SWITCH;
  452. /*
  453. * PT, PTI, PR, PC instruction operate on primary AS only. Check
  454. * if the primary-space-switch-event control was or got set.
  455. */
  456. if (new_as == PSW_AS_PRIMARY && !old_as_is_home(vcpu) &&
  457. (pssec(vcpu) || old_ssec(vcpu)))
  458. vcpu->arch.sie_block->iprcc = PGM_SPACE_SWITCH;
  459. }
  460. }