guestdbg.c 16 KB

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