sys_regs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * Copyright (C) 2012,2013 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * Derived from arch/arm/kvm/coproc.c:
  6. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  7. * Authors: Rusty Russell <rusty@rustcorp.com.au>
  8. * Christoffer Dall <c.dall@virtualopensystems.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/mm.h>
  23. #include <linux/kvm_host.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/kvm_arm.h>
  26. #include <asm/kvm_host.h>
  27. #include <asm/kvm_emulate.h>
  28. #include <asm/kvm_coproc.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/cputype.h>
  31. #include <trace/events/kvm.h>
  32. #include "sys_regs.h"
  33. /*
  34. * All of this file is extremly similar to the ARM coproc.c, but the
  35. * types are different. My gut feeling is that it should be pretty
  36. * easy to merge, but that would be an ABI breakage -- again. VFP
  37. * would also need to be abstracted.
  38. */
  39. /* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
  40. static u32 cache_levels;
  41. /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
  42. #define CSSELR_MAX 12
  43. /* Which cache CCSIDR represents depends on CSSELR value. */
  44. static u32 get_ccsidr(u32 csselr)
  45. {
  46. u32 ccsidr;
  47. /* Make sure noone else changes CSSELR during this! */
  48. local_irq_disable();
  49. /* Put value into CSSELR */
  50. asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
  51. isb();
  52. /* Read result out of CCSIDR */
  53. asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
  54. local_irq_enable();
  55. return ccsidr;
  56. }
  57. static void do_dc_cisw(u32 val)
  58. {
  59. asm volatile("dc cisw, %x0" : : "r" (val));
  60. dsb();
  61. }
  62. static void do_dc_csw(u32 val)
  63. {
  64. asm volatile("dc csw, %x0" : : "r" (val));
  65. dsb();
  66. }
  67. /* See note at ARM ARM B1.14.4 */
  68. static bool access_dcsw(struct kvm_vcpu *vcpu,
  69. const struct sys_reg_params *p,
  70. const struct sys_reg_desc *r)
  71. {
  72. unsigned long val;
  73. int cpu;
  74. if (!p->is_write)
  75. return read_from_write_only(vcpu, p);
  76. cpu = get_cpu();
  77. cpumask_setall(&vcpu->arch.require_dcache_flush);
  78. cpumask_clear_cpu(cpu, &vcpu->arch.require_dcache_flush);
  79. /* If we were already preempted, take the long way around */
  80. if (cpu != vcpu->arch.last_pcpu) {
  81. flush_cache_all();
  82. goto done;
  83. }
  84. val = *vcpu_reg(vcpu, p->Rt);
  85. switch (p->CRm) {
  86. case 6: /* Upgrade DCISW to DCCISW, as per HCR.SWIO */
  87. case 14: /* DCCISW */
  88. do_dc_cisw(val);
  89. break;
  90. case 10: /* DCCSW */
  91. do_dc_csw(val);
  92. break;
  93. }
  94. done:
  95. put_cpu();
  96. return true;
  97. }
  98. /*
  99. * We could trap ID_DFR0 and tell the guest we don't support performance
  100. * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
  101. * NAKed, so it will read the PMCR anyway.
  102. *
  103. * Therefore we tell the guest we have 0 counters. Unfortunately, we
  104. * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
  105. * all PM registers, which doesn't crash the guest kernel at least.
  106. */
  107. static bool pm_fake(struct kvm_vcpu *vcpu,
  108. const struct sys_reg_params *p,
  109. const struct sys_reg_desc *r)
  110. {
  111. if (p->is_write)
  112. return ignore_write(vcpu, p);
  113. else
  114. return read_zero(vcpu, p);
  115. }
  116. static void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
  117. {
  118. u64 amair;
  119. asm volatile("mrs %0, amair_el1\n" : "=r" (amair));
  120. vcpu_sys_reg(vcpu, AMAIR_EL1) = amair;
  121. }
  122. static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
  123. {
  124. /*
  125. * Simply map the vcpu_id into the Aff0 field of the MPIDR.
  126. */
  127. vcpu_sys_reg(vcpu, MPIDR_EL1) = (1UL << 31) | (vcpu->vcpu_id & 0xff);
  128. }
  129. /*
  130. * Architected system registers.
  131. * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
  132. */
  133. static const struct sys_reg_desc sys_reg_descs[] = {
  134. /* DC ISW */
  135. { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b0110), Op2(0b010),
  136. access_dcsw },
  137. /* DC CSW */
  138. { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1010), Op2(0b010),
  139. access_dcsw },
  140. /* DC CISW */
  141. { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
  142. access_dcsw },
  143. /* MPIDR_EL1 */
  144. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b101),
  145. NULL, reset_mpidr, MPIDR_EL1 },
  146. /* SCTLR_EL1 */
  147. { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
  148. NULL, reset_val, SCTLR_EL1, 0x00C50078 },
  149. /* CPACR_EL1 */
  150. { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b010),
  151. NULL, reset_val, CPACR_EL1, 0 },
  152. /* TTBR0_EL1 */
  153. { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b000),
  154. NULL, reset_unknown, TTBR0_EL1 },
  155. /* TTBR1_EL1 */
  156. { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b001),
  157. NULL, reset_unknown, TTBR1_EL1 },
  158. /* TCR_EL1 */
  159. { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b010),
  160. NULL, reset_val, TCR_EL1, 0 },
  161. /* AFSR0_EL1 */
  162. { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b000),
  163. NULL, reset_unknown, AFSR0_EL1 },
  164. /* AFSR1_EL1 */
  165. { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b001),
  166. NULL, reset_unknown, AFSR1_EL1 },
  167. /* ESR_EL1 */
  168. { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0010), Op2(0b000),
  169. NULL, reset_unknown, ESR_EL1 },
  170. /* FAR_EL1 */
  171. { Op0(0b11), Op1(0b000), CRn(0b0110), CRm(0b0000), Op2(0b000),
  172. NULL, reset_unknown, FAR_EL1 },
  173. /* PMINTENSET_EL1 */
  174. { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b001),
  175. pm_fake },
  176. /* PMINTENCLR_EL1 */
  177. { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b010),
  178. pm_fake },
  179. /* MAIR_EL1 */
  180. { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000),
  181. NULL, reset_unknown, MAIR_EL1 },
  182. /* AMAIR_EL1 */
  183. { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000),
  184. NULL, reset_amair_el1, AMAIR_EL1 },
  185. /* VBAR_EL1 */
  186. { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b0000), Op2(0b000),
  187. NULL, reset_val, VBAR_EL1, 0 },
  188. /* CONTEXTIDR_EL1 */
  189. { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b001),
  190. NULL, reset_val, CONTEXTIDR_EL1, 0 },
  191. /* TPIDR_EL1 */
  192. { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b100),
  193. NULL, reset_unknown, TPIDR_EL1 },
  194. /* CNTKCTL_EL1 */
  195. { Op0(0b11), Op1(0b000), CRn(0b1110), CRm(0b0001), Op2(0b000),
  196. NULL, reset_val, CNTKCTL_EL1, 0},
  197. /* CSSELR_EL1 */
  198. { Op0(0b11), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
  199. NULL, reset_unknown, CSSELR_EL1 },
  200. /* PMCR_EL0 */
  201. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
  202. pm_fake },
  203. /* PMCNTENSET_EL0 */
  204. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
  205. pm_fake },
  206. /* PMCNTENCLR_EL0 */
  207. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
  208. pm_fake },
  209. /* PMOVSCLR_EL0 */
  210. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
  211. pm_fake },
  212. /* PMSWINC_EL0 */
  213. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b100),
  214. pm_fake },
  215. /* PMSELR_EL0 */
  216. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b101),
  217. pm_fake },
  218. /* PMCEID0_EL0 */
  219. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b110),
  220. pm_fake },
  221. /* PMCEID1_EL0 */
  222. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b111),
  223. pm_fake },
  224. /* PMCCNTR_EL0 */
  225. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
  226. pm_fake },
  227. /* PMXEVTYPER_EL0 */
  228. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
  229. pm_fake },
  230. /* PMXEVCNTR_EL0 */
  231. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
  232. pm_fake },
  233. /* PMUSERENR_EL0 */
  234. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
  235. pm_fake },
  236. /* PMOVSSET_EL0 */
  237. { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b011),
  238. pm_fake },
  239. /* TPIDR_EL0 */
  240. { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b010),
  241. NULL, reset_unknown, TPIDR_EL0 },
  242. /* TPIDRRO_EL0 */
  243. { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
  244. NULL, reset_unknown, TPIDRRO_EL0 },
  245. };
  246. /* Target specific emulation tables */
  247. static struct kvm_sys_reg_target_table *target_tables[KVM_ARM_NUM_TARGETS];
  248. void kvm_register_target_sys_reg_table(unsigned int target,
  249. struct kvm_sys_reg_target_table *table)
  250. {
  251. target_tables[target] = table;
  252. }
  253. /* Get specific register table for this target. */
  254. static const struct sys_reg_desc *get_target_table(unsigned target, size_t *num)
  255. {
  256. struct kvm_sys_reg_target_table *table;
  257. table = target_tables[target];
  258. *num = table->table64.num;
  259. return table->table64.table;
  260. }
  261. static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params,
  262. const struct sys_reg_desc table[],
  263. unsigned int num)
  264. {
  265. unsigned int i;
  266. for (i = 0; i < num; i++) {
  267. const struct sys_reg_desc *r = &table[i];
  268. if (params->Op0 != r->Op0)
  269. continue;
  270. if (params->Op1 != r->Op1)
  271. continue;
  272. if (params->CRn != r->CRn)
  273. continue;
  274. if (params->CRm != r->CRm)
  275. continue;
  276. if (params->Op2 != r->Op2)
  277. continue;
  278. return r;
  279. }
  280. return NULL;
  281. }
  282. static int emulate_sys_reg(struct kvm_vcpu *vcpu,
  283. const struct sys_reg_params *params)
  284. {
  285. size_t num;
  286. const struct sys_reg_desc *table, *r;
  287. table = get_target_table(vcpu->arch.target, &num);
  288. /* Search target-specific then generic table. */
  289. r = find_reg(params, table, num);
  290. if (!r)
  291. r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
  292. if (likely(r)) {
  293. /*
  294. * Not having an accessor means that we have
  295. * configured a trap that we don't know how to
  296. * handle. This certainly qualifies as a gross bug
  297. * that should be fixed right away.
  298. */
  299. BUG_ON(!r->access);
  300. if (likely(r->access(vcpu, params, r))) {
  301. /* Skip instruction, since it was emulated */
  302. kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
  303. return 1;
  304. }
  305. /* If access function fails, it should complain. */
  306. } else {
  307. kvm_err("Unsupported guest sys_reg access at: %lx\n",
  308. *vcpu_pc(vcpu));
  309. print_sys_reg_instr(params);
  310. }
  311. kvm_inject_undefined(vcpu);
  312. return 1;
  313. }
  314. static void reset_sys_reg_descs(struct kvm_vcpu *vcpu,
  315. const struct sys_reg_desc *table, size_t num)
  316. {
  317. unsigned long i;
  318. for (i = 0; i < num; i++)
  319. if (table[i].reset)
  320. table[i].reset(vcpu, &table[i]);
  321. }
  322. /**
  323. * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
  324. * @vcpu: The VCPU pointer
  325. * @run: The kvm_run struct
  326. */
  327. int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run)
  328. {
  329. struct sys_reg_params params;
  330. unsigned long esr = kvm_vcpu_get_hsr(vcpu);
  331. params.Op0 = (esr >> 20) & 3;
  332. params.Op1 = (esr >> 14) & 0x7;
  333. params.CRn = (esr >> 10) & 0xf;
  334. params.CRm = (esr >> 1) & 0xf;
  335. params.Op2 = (esr >> 17) & 0x7;
  336. params.Rt = (esr >> 5) & 0x1f;
  337. params.is_write = !(esr & 1);
  338. return emulate_sys_reg(vcpu, &params);
  339. }
  340. /******************************************************************************
  341. * Userspace API
  342. *****************************************************************************/
  343. static bool index_to_params(u64 id, struct sys_reg_params *params)
  344. {
  345. switch (id & KVM_REG_SIZE_MASK) {
  346. case KVM_REG_SIZE_U64:
  347. /* Any unused index bits means it's not valid. */
  348. if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
  349. | KVM_REG_ARM_COPROC_MASK
  350. | KVM_REG_ARM64_SYSREG_OP0_MASK
  351. | KVM_REG_ARM64_SYSREG_OP1_MASK
  352. | KVM_REG_ARM64_SYSREG_CRN_MASK
  353. | KVM_REG_ARM64_SYSREG_CRM_MASK
  354. | KVM_REG_ARM64_SYSREG_OP2_MASK))
  355. return false;
  356. params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK)
  357. >> KVM_REG_ARM64_SYSREG_OP0_SHIFT);
  358. params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK)
  359. >> KVM_REG_ARM64_SYSREG_OP1_SHIFT);
  360. params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK)
  361. >> KVM_REG_ARM64_SYSREG_CRN_SHIFT);
  362. params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK)
  363. >> KVM_REG_ARM64_SYSREG_CRM_SHIFT);
  364. params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK)
  365. >> KVM_REG_ARM64_SYSREG_OP2_SHIFT);
  366. return true;
  367. default:
  368. return false;
  369. }
  370. }
  371. /* Decode an index value, and find the sys_reg_desc entry. */
  372. static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
  373. u64 id)
  374. {
  375. size_t num;
  376. const struct sys_reg_desc *table, *r;
  377. struct sys_reg_params params;
  378. /* We only do sys_reg for now. */
  379. if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG)
  380. return NULL;
  381. if (!index_to_params(id, &params))
  382. return NULL;
  383. table = get_target_table(vcpu->arch.target, &num);
  384. r = find_reg(&params, table, num);
  385. if (!r)
  386. r = find_reg(&params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
  387. /* Not saved in the sys_reg array? */
  388. if (r && !r->reg)
  389. r = NULL;
  390. return r;
  391. }
  392. /*
  393. * These are the invariant sys_reg registers: we let the guest see the
  394. * host versions of these, so they're part of the guest state.
  395. *
  396. * A future CPU may provide a mechanism to present different values to
  397. * the guest, or a future kvm may trap them.
  398. */
  399. #define FUNCTION_INVARIANT(reg) \
  400. static void get_##reg(struct kvm_vcpu *v, \
  401. const struct sys_reg_desc *r) \
  402. { \
  403. u64 val; \
  404. \
  405. asm volatile("mrs %0, " __stringify(reg) "\n" \
  406. : "=r" (val)); \
  407. ((struct sys_reg_desc *)r)->val = val; \
  408. }
  409. FUNCTION_INVARIANT(midr_el1)
  410. FUNCTION_INVARIANT(ctr_el0)
  411. FUNCTION_INVARIANT(revidr_el1)
  412. FUNCTION_INVARIANT(id_pfr0_el1)
  413. FUNCTION_INVARIANT(id_pfr1_el1)
  414. FUNCTION_INVARIANT(id_dfr0_el1)
  415. FUNCTION_INVARIANT(id_afr0_el1)
  416. FUNCTION_INVARIANT(id_mmfr0_el1)
  417. FUNCTION_INVARIANT(id_mmfr1_el1)
  418. FUNCTION_INVARIANT(id_mmfr2_el1)
  419. FUNCTION_INVARIANT(id_mmfr3_el1)
  420. FUNCTION_INVARIANT(id_isar0_el1)
  421. FUNCTION_INVARIANT(id_isar1_el1)
  422. FUNCTION_INVARIANT(id_isar2_el1)
  423. FUNCTION_INVARIANT(id_isar3_el1)
  424. FUNCTION_INVARIANT(id_isar4_el1)
  425. FUNCTION_INVARIANT(id_isar5_el1)
  426. FUNCTION_INVARIANT(clidr_el1)
  427. FUNCTION_INVARIANT(aidr_el1)
  428. /* ->val is filled in by kvm_sys_reg_table_init() */
  429. static struct sys_reg_desc invariant_sys_regs[] = {
  430. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b000),
  431. NULL, get_midr_el1 },
  432. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b110),
  433. NULL, get_revidr_el1 },
  434. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b000),
  435. NULL, get_id_pfr0_el1 },
  436. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b001),
  437. NULL, get_id_pfr1_el1 },
  438. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b010),
  439. NULL, get_id_dfr0_el1 },
  440. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b011),
  441. NULL, get_id_afr0_el1 },
  442. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b100),
  443. NULL, get_id_mmfr0_el1 },
  444. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b101),
  445. NULL, get_id_mmfr1_el1 },
  446. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b110),
  447. NULL, get_id_mmfr2_el1 },
  448. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b111),
  449. NULL, get_id_mmfr3_el1 },
  450. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
  451. NULL, get_id_isar0_el1 },
  452. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b001),
  453. NULL, get_id_isar1_el1 },
  454. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
  455. NULL, get_id_isar2_el1 },
  456. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b011),
  457. NULL, get_id_isar3_el1 },
  458. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b100),
  459. NULL, get_id_isar4_el1 },
  460. { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b101),
  461. NULL, get_id_isar5_el1 },
  462. { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b001),
  463. NULL, get_clidr_el1 },
  464. { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b111),
  465. NULL, get_aidr_el1 },
  466. { Op0(0b11), Op1(0b011), CRn(0b0000), CRm(0b0000), Op2(0b001),
  467. NULL, get_ctr_el0 },
  468. };
  469. static int reg_from_user(void *val, const void __user *uaddr, u64 id)
  470. {
  471. /* This Just Works because we are little endian. */
  472. if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
  473. return -EFAULT;
  474. return 0;
  475. }
  476. static int reg_to_user(void __user *uaddr, const void *val, u64 id)
  477. {
  478. /* This Just Works because we are little endian. */
  479. if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
  480. return -EFAULT;
  481. return 0;
  482. }
  483. static int get_invariant_sys_reg(u64 id, void __user *uaddr)
  484. {
  485. struct sys_reg_params params;
  486. const struct sys_reg_desc *r;
  487. if (!index_to_params(id, &params))
  488. return -ENOENT;
  489. r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
  490. if (!r)
  491. return -ENOENT;
  492. return reg_to_user(uaddr, &r->val, id);
  493. }
  494. static int set_invariant_sys_reg(u64 id, void __user *uaddr)
  495. {
  496. struct sys_reg_params params;
  497. const struct sys_reg_desc *r;
  498. int err;
  499. u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
  500. if (!index_to_params(id, &params))
  501. return -ENOENT;
  502. r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
  503. if (!r)
  504. return -ENOENT;
  505. err = reg_from_user(&val, uaddr, id);
  506. if (err)
  507. return err;
  508. /* This is what we mean by invariant: you can't change it. */
  509. if (r->val != val)
  510. return -EINVAL;
  511. return 0;
  512. }
  513. static bool is_valid_cache(u32 val)
  514. {
  515. u32 level, ctype;
  516. if (val >= CSSELR_MAX)
  517. return -ENOENT;
  518. /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
  519. level = (val >> 1);
  520. ctype = (cache_levels >> (level * 3)) & 7;
  521. switch (ctype) {
  522. case 0: /* No cache */
  523. return false;
  524. case 1: /* Instruction cache only */
  525. return (val & 1);
  526. case 2: /* Data cache only */
  527. case 4: /* Unified cache */
  528. return !(val & 1);
  529. case 3: /* Separate instruction and data caches */
  530. return true;
  531. default: /* Reserved: we can't know instruction or data. */
  532. return false;
  533. }
  534. }
  535. static int demux_c15_get(u64 id, void __user *uaddr)
  536. {
  537. u32 val;
  538. u32 __user *uval = uaddr;
  539. /* Fail if we have unknown bits set. */
  540. if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
  541. | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
  542. return -ENOENT;
  543. switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
  544. case KVM_REG_ARM_DEMUX_ID_CCSIDR:
  545. if (KVM_REG_SIZE(id) != 4)
  546. return -ENOENT;
  547. val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
  548. >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
  549. if (!is_valid_cache(val))
  550. return -ENOENT;
  551. return put_user(get_ccsidr(val), uval);
  552. default:
  553. return -ENOENT;
  554. }
  555. }
  556. static int demux_c15_set(u64 id, void __user *uaddr)
  557. {
  558. u32 val, newval;
  559. u32 __user *uval = uaddr;
  560. /* Fail if we have unknown bits set. */
  561. if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
  562. | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
  563. return -ENOENT;
  564. switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
  565. case KVM_REG_ARM_DEMUX_ID_CCSIDR:
  566. if (KVM_REG_SIZE(id) != 4)
  567. return -ENOENT;
  568. val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
  569. >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
  570. if (!is_valid_cache(val))
  571. return -ENOENT;
  572. if (get_user(newval, uval))
  573. return -EFAULT;
  574. /* This is also invariant: you can't change it. */
  575. if (newval != get_ccsidr(val))
  576. return -EINVAL;
  577. return 0;
  578. default:
  579. return -ENOENT;
  580. }
  581. }
  582. int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  583. {
  584. const struct sys_reg_desc *r;
  585. void __user *uaddr = (void __user *)(unsigned long)reg->addr;
  586. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
  587. return demux_c15_get(reg->id, uaddr);
  588. if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
  589. return -ENOENT;
  590. r = index_to_sys_reg_desc(vcpu, reg->id);
  591. if (!r)
  592. return get_invariant_sys_reg(reg->id, uaddr);
  593. return reg_to_user(uaddr, &vcpu_sys_reg(vcpu, r->reg), reg->id);
  594. }
  595. int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  596. {
  597. const struct sys_reg_desc *r;
  598. void __user *uaddr = (void __user *)(unsigned long)reg->addr;
  599. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
  600. return demux_c15_set(reg->id, uaddr);
  601. if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
  602. return -ENOENT;
  603. r = index_to_sys_reg_desc(vcpu, reg->id);
  604. if (!r)
  605. return set_invariant_sys_reg(reg->id, uaddr);
  606. return reg_from_user(&vcpu_sys_reg(vcpu, r->reg), uaddr, reg->id);
  607. }
  608. static unsigned int num_demux_regs(void)
  609. {
  610. unsigned int i, count = 0;
  611. for (i = 0; i < CSSELR_MAX; i++)
  612. if (is_valid_cache(i))
  613. count++;
  614. return count;
  615. }
  616. static int write_demux_regids(u64 __user *uindices)
  617. {
  618. u64 val = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
  619. unsigned int i;
  620. val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
  621. for (i = 0; i < CSSELR_MAX; i++) {
  622. if (!is_valid_cache(i))
  623. continue;
  624. if (put_user(val | i, uindices))
  625. return -EFAULT;
  626. uindices++;
  627. }
  628. return 0;
  629. }
  630. static u64 sys_reg_to_index(const struct sys_reg_desc *reg)
  631. {
  632. return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 |
  633. KVM_REG_ARM64_SYSREG |
  634. (reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) |
  635. (reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) |
  636. (reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) |
  637. (reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) |
  638. (reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT));
  639. }
  640. static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
  641. {
  642. if (!*uind)
  643. return true;
  644. if (put_user(sys_reg_to_index(reg), *uind))
  645. return false;
  646. (*uind)++;
  647. return true;
  648. }
  649. /* Assumed ordered tables, see kvm_sys_reg_table_init. */
  650. static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
  651. {
  652. const struct sys_reg_desc *i1, *i2, *end1, *end2;
  653. unsigned int total = 0;
  654. size_t num;
  655. /* We check for duplicates here, to allow arch-specific overrides. */
  656. i1 = get_target_table(vcpu->arch.target, &num);
  657. end1 = i1 + num;
  658. i2 = sys_reg_descs;
  659. end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
  660. BUG_ON(i1 == end1 || i2 == end2);
  661. /* Walk carefully, as both tables may refer to the same register. */
  662. while (i1 || i2) {
  663. int cmp = cmp_sys_reg(i1, i2);
  664. /* target-specific overrides generic entry. */
  665. if (cmp <= 0) {
  666. /* Ignore registers we trap but don't save. */
  667. if (i1->reg) {
  668. if (!copy_reg_to_user(i1, &uind))
  669. return -EFAULT;
  670. total++;
  671. }
  672. } else {
  673. /* Ignore registers we trap but don't save. */
  674. if (i2->reg) {
  675. if (!copy_reg_to_user(i2, &uind))
  676. return -EFAULT;
  677. total++;
  678. }
  679. }
  680. if (cmp <= 0 && ++i1 == end1)
  681. i1 = NULL;
  682. if (cmp >= 0 && ++i2 == end2)
  683. i2 = NULL;
  684. }
  685. return total;
  686. }
  687. unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
  688. {
  689. return ARRAY_SIZE(invariant_sys_regs)
  690. + num_demux_regs()
  691. + walk_sys_regs(vcpu, (u64 __user *)NULL);
  692. }
  693. int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  694. {
  695. unsigned int i;
  696. int err;
  697. /* Then give them all the invariant registers' indices. */
  698. for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) {
  699. if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices))
  700. return -EFAULT;
  701. uindices++;
  702. }
  703. err = walk_sys_regs(vcpu, uindices);
  704. if (err < 0)
  705. return err;
  706. uindices += err;
  707. return write_demux_regids(uindices);
  708. }
  709. void kvm_sys_reg_table_init(void)
  710. {
  711. unsigned int i;
  712. struct sys_reg_desc clidr;
  713. /* Make sure tables are unique and in order. */
  714. for (i = 1; i < ARRAY_SIZE(sys_reg_descs); i++)
  715. BUG_ON(cmp_sys_reg(&sys_reg_descs[i-1], &sys_reg_descs[i]) >= 0);
  716. /* We abuse the reset function to overwrite the table itself. */
  717. for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
  718. invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
  719. /*
  720. * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
  721. *
  722. * If software reads the Cache Type fields from Ctype1
  723. * upwards, once it has seen a value of 0b000, no caches
  724. * exist at further-out levels of the hierarchy. So, for
  725. * example, if Ctype3 is the first Cache Type field with a
  726. * value of 0b000, the values of Ctype4 to Ctype7 must be
  727. * ignored.
  728. */
  729. get_clidr_el1(NULL, &clidr); /* Ugly... */
  730. cache_levels = clidr.val;
  731. for (i = 0; i < 7; i++)
  732. if (((cache_levels >> (i*3)) & 7) == 0)
  733. break;
  734. /* Clear all higher bits. */
  735. cache_levels &= (1 << (i*3))-1;
  736. }
  737. /**
  738. * kvm_reset_sys_regs - sets system registers to reset value
  739. * @vcpu: The VCPU pointer
  740. *
  741. * This function finds the right table above and sets the registers on the
  742. * virtual CPU struct to their architecturally defined reset values.
  743. */
  744. void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
  745. {
  746. size_t num;
  747. const struct sys_reg_desc *table;
  748. /* Catch someone adding a register without putting in reset entry. */
  749. memset(&vcpu->arch.ctxt.sys_regs, 0x42, sizeof(vcpu->arch.ctxt.sys_regs));
  750. /* Generic chip reset first (so target could override). */
  751. reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
  752. table = get_target_table(vcpu->arch.target, &num);
  753. reset_sys_reg_descs(vcpu, table, num);
  754. for (num = 1; num < NR_SYS_REGS; num++)
  755. if (vcpu_sys_reg(vcpu, num) == 0x4242424242424242)
  756. panic("Didn't reset vcpu_sys_reg(%zi)", num);
  757. }