guestdbg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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, u8 perc,
  336. unsigned long peraddr)
  337. {
  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. if (guestdbg_hw_bp_enabled(vcpu)) {
  343. if (per_write_wp_event(perc) &&
  344. vcpu->arch.guestdbg.nr_hw_wp > 0) {
  345. wp_info = any_wp_changed(vcpu);
  346. if (wp_info) {
  347. debug_exit->addr = wp_info->addr;
  348. debug_exit->type = KVM_HW_WP_WRITE;
  349. goto exit_required;
  350. }
  351. }
  352. if (per_bp_event(perc) &&
  353. vcpu->arch.guestdbg.nr_hw_bp > 0) {
  354. bp_info = find_hw_bp(vcpu, addr);
  355. /* remove duplicate events if PC==PER address */
  356. if (bp_info && (addr != peraddr)) {
  357. debug_exit->addr = addr;
  358. debug_exit->type = KVM_HW_BP;
  359. vcpu->arch.guestdbg.last_bp = addr;
  360. goto exit_required;
  361. }
  362. /* breakpoint missed */
  363. bp_info = find_hw_bp(vcpu, peraddr);
  364. if (bp_info && vcpu->arch.guestdbg.last_bp != peraddr) {
  365. debug_exit->addr = peraddr;
  366. debug_exit->type = KVM_HW_BP;
  367. goto exit_required;
  368. }
  369. }
  370. }
  371. if (guestdbg_sstep_enabled(vcpu) && per_bp_event(perc)) {
  372. debug_exit->addr = addr;
  373. debug_exit->type = KVM_SINGLESTEP;
  374. goto exit_required;
  375. }
  376. return 0;
  377. exit_required:
  378. return 1;
  379. }
  380. static int per_fetched_addr(struct kvm_vcpu *vcpu, unsigned long *addr)
  381. {
  382. u8 exec_ilen = 0;
  383. u16 opcode[3];
  384. int rc;
  385. if (vcpu->arch.sie_block->icptcode == ICPT_PROGI) {
  386. /* PER address references the fetched or the execute instr */
  387. *addr = vcpu->arch.sie_block->peraddr;
  388. /*
  389. * Manually detect if we have an EXECUTE instruction. As
  390. * instructions are always 2 byte aligned we can read the
  391. * first two bytes unconditionally
  392. */
  393. rc = read_guest_instr(vcpu, *addr, &opcode, 2);
  394. if (rc)
  395. return rc;
  396. if (opcode[0] >> 8 == 0x44)
  397. exec_ilen = 4;
  398. if ((opcode[0] & 0xff0f) == 0xc600)
  399. exec_ilen = 6;
  400. } else {
  401. /* instr was suppressed, calculate the responsible instr */
  402. *addr = __rewind_psw(vcpu->arch.sie_block->gpsw,
  403. kvm_s390_get_ilen(vcpu));
  404. if (vcpu->arch.sie_block->icptstatus & 0x01) {
  405. exec_ilen = (vcpu->arch.sie_block->icptstatus & 0x60) >> 4;
  406. if (!exec_ilen)
  407. exec_ilen = 4;
  408. }
  409. }
  410. if (exec_ilen) {
  411. /* read the complete EXECUTE instr to detect the fetched addr */
  412. rc = read_guest_instr(vcpu, *addr, &opcode, exec_ilen);
  413. if (rc)
  414. return rc;
  415. if (exec_ilen == 6) {
  416. /* EXECUTE RELATIVE LONG - RIL-b format */
  417. s32 rl = *((s32 *) (opcode + 1));
  418. /* rl is a _signed_ 32 bit value specifying halfwords */
  419. *addr += (u64)(s64) rl * 2;
  420. } else {
  421. /* EXECUTE - RX-a format */
  422. u32 base = (opcode[1] & 0xf000) >> 12;
  423. u32 disp = opcode[1] & 0x0fff;
  424. u32 index = opcode[0] & 0x000f;
  425. *addr = base ? vcpu->run->s.regs.gprs[base] : 0;
  426. *addr += index ? vcpu->run->s.regs.gprs[index] : 0;
  427. *addr += disp;
  428. }
  429. *addr = kvm_s390_logical_to_effective(vcpu, *addr);
  430. }
  431. return 0;
  432. }
  433. #define guest_per_enabled(vcpu) \
  434. (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER)
  435. int kvm_s390_handle_per_ifetch_icpt(struct kvm_vcpu *vcpu)
  436. {
  437. const u64 cr10 = vcpu->arch.sie_block->gcr[10];
  438. const u64 cr11 = vcpu->arch.sie_block->gcr[11];
  439. const u8 ilen = kvm_s390_get_ilen(vcpu);
  440. struct kvm_s390_pgm_info pgm_info = {
  441. .code = PGM_PER,
  442. .per_code = PER_CODE_IFETCH,
  443. .per_address = __rewind_psw(vcpu->arch.sie_block->gpsw, ilen),
  444. };
  445. unsigned long fetched_addr;
  446. int rc;
  447. /*
  448. * The PSW points to the next instruction, therefore the intercepted
  449. * instruction generated a PER i-fetch event. PER address therefore
  450. * points at the previous PSW address (could be an EXECUTE function).
  451. */
  452. if (!guestdbg_enabled(vcpu))
  453. return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
  454. if (debug_exit_required(vcpu, pgm_info.per_code, pgm_info.per_address))
  455. vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
  456. if (!guest_per_enabled(vcpu) ||
  457. !(vcpu->arch.sie_block->gcr[9] & PER_EVENT_IFETCH))
  458. return 0;
  459. rc = per_fetched_addr(vcpu, &fetched_addr);
  460. if (rc < 0)
  461. return rc;
  462. if (rc)
  463. /* instruction-fetching exceptions */
  464. return kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
  465. if (in_addr_range(fetched_addr, cr10, cr11))
  466. return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
  467. return 0;
  468. }
  469. static int filter_guest_per_event(struct kvm_vcpu *vcpu)
  470. {
  471. const u8 perc = vcpu->arch.sie_block->perc;
  472. u64 addr = vcpu->arch.sie_block->gpsw.addr;
  473. u64 cr9 = vcpu->arch.sie_block->gcr[9];
  474. u64 cr10 = vcpu->arch.sie_block->gcr[10];
  475. u64 cr11 = vcpu->arch.sie_block->gcr[11];
  476. /* filter all events, demanded by the guest */
  477. u8 guest_perc = perc & (cr9 >> 24) & PER_CODE_MASK;
  478. unsigned long fetched_addr;
  479. int rc;
  480. if (!guest_per_enabled(vcpu))
  481. guest_perc = 0;
  482. /* filter "successful-branching" events */
  483. if (guest_perc & PER_CODE_BRANCH &&
  484. cr9 & PER_CONTROL_BRANCH_ADDRESS &&
  485. !in_addr_range(addr, cr10, cr11))
  486. guest_perc &= ~PER_CODE_BRANCH;
  487. /* filter "instruction-fetching" events */
  488. if (guest_perc & PER_CODE_IFETCH) {
  489. rc = per_fetched_addr(vcpu, &fetched_addr);
  490. if (rc < 0)
  491. return rc;
  492. /*
  493. * Don't inject an irq on exceptions. This would make handling
  494. * on icpt code 8 very complex (as PSW was already rewound).
  495. */
  496. if (rc || !in_addr_range(fetched_addr, cr10, cr11))
  497. guest_perc &= ~PER_CODE_IFETCH;
  498. }
  499. /* All other PER events will be given to the guest */
  500. /* TODO: Check altered address/address space */
  501. vcpu->arch.sie_block->perc = guest_perc;
  502. if (!guest_perc)
  503. vcpu->arch.sie_block->iprcc &= ~PGM_PER;
  504. return 0;
  505. }
  506. #define pssec(vcpu) (vcpu->arch.sie_block->gcr[1] & _ASCE_SPACE_SWITCH)
  507. #define hssec(vcpu) (vcpu->arch.sie_block->gcr[13] & _ASCE_SPACE_SWITCH)
  508. #define old_ssec(vcpu) ((vcpu->arch.sie_block->tecmc >> 31) & 0x1)
  509. #define old_as_is_home(vcpu) !(vcpu->arch.sie_block->tecmc & 0xffff)
  510. int kvm_s390_handle_per_event(struct kvm_vcpu *vcpu)
  511. {
  512. int rc, new_as;
  513. if (debug_exit_required(vcpu, vcpu->arch.sie_block->perc,
  514. vcpu->arch.sie_block->peraddr))
  515. vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
  516. rc = filter_guest_per_event(vcpu);
  517. if (rc)
  518. return rc;
  519. /*
  520. * Only RP, SAC, SACF, PT, PTI, PR, PC instructions can trigger
  521. * a space-switch event. PER events enforce space-switch events
  522. * for these instructions. So if no PER event for the guest is left,
  523. * we might have to filter the space-switch element out, too.
  524. */
  525. if (vcpu->arch.sie_block->iprcc == PGM_SPACE_SWITCH) {
  526. vcpu->arch.sie_block->iprcc = 0;
  527. new_as = psw_bits(vcpu->arch.sie_block->gpsw).as;
  528. /*
  529. * If the AS changed from / to home, we had RP, SAC or SACF
  530. * instruction. Check primary and home space-switch-event
  531. * controls. (theoretically home -> home produced no event)
  532. */
  533. if (((new_as == PSW_BITS_AS_HOME) ^ old_as_is_home(vcpu)) &&
  534. (pssec(vcpu) || hssec(vcpu)))
  535. vcpu->arch.sie_block->iprcc = PGM_SPACE_SWITCH;
  536. /*
  537. * PT, PTI, PR, PC instruction operate on primary AS only. Check
  538. * if the primary-space-switch-event control was or got set.
  539. */
  540. if (new_as == PSW_BITS_AS_PRIMARY && !old_as_is_home(vcpu) &&
  541. (pssec(vcpu) || old_ssec(vcpu)))
  542. vcpu->arch.sie_block->iprcc = PGM_SPACE_SWITCH;
  543. }
  544. return 0;
  545. }