coproc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  3. * Authors: Rusty Russell <rusty@rustcorp.com.au>
  4. * Christoffer Dall <c.dall@virtualopensystems.com>
  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, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include <linux/mm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/kvm_arm.h>
  23. #include <asm/kvm_host.h>
  24. #include <asm/kvm_emulate.h>
  25. #include <asm/kvm_coproc.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/cputype.h>
  28. #include <trace/events/kvm.h>
  29. #include "trace.h"
  30. #include "coproc.h"
  31. /******************************************************************************
  32. * Co-processor emulation
  33. *****************************************************************************/
  34. /* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
  35. static u32 cache_levels;
  36. /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
  37. #define CSSELR_MAX 12
  38. int kvm_handle_cp10_id(struct kvm_vcpu *vcpu, struct kvm_run *run)
  39. {
  40. kvm_inject_undefined(vcpu);
  41. return 1;
  42. }
  43. int kvm_handle_cp_0_13_access(struct kvm_vcpu *vcpu, struct kvm_run *run)
  44. {
  45. /*
  46. * We can get here, if the host has been built without VFPv3 support,
  47. * but the guest attempted a floating point operation.
  48. */
  49. kvm_inject_undefined(vcpu);
  50. return 1;
  51. }
  52. int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run)
  53. {
  54. kvm_inject_undefined(vcpu);
  55. return 1;
  56. }
  57. int kvm_handle_cp14_access(struct kvm_vcpu *vcpu, struct kvm_run *run)
  58. {
  59. kvm_inject_undefined(vcpu);
  60. return 1;
  61. }
  62. /* See note at ARM ARM B1.14.4 */
  63. static bool access_dcsw(struct kvm_vcpu *vcpu,
  64. const struct coproc_params *p,
  65. const struct coproc_reg *r)
  66. {
  67. u32 val;
  68. int cpu;
  69. cpu = get_cpu();
  70. if (!p->is_write)
  71. return read_from_write_only(vcpu, p);
  72. cpumask_setall(&vcpu->arch.require_dcache_flush);
  73. cpumask_clear_cpu(cpu, &vcpu->arch.require_dcache_flush);
  74. /* If we were already preempted, take the long way around */
  75. if (cpu != vcpu->arch.last_pcpu) {
  76. flush_cache_all();
  77. goto done;
  78. }
  79. val = *vcpu_reg(vcpu, p->Rt1);
  80. switch (p->CRm) {
  81. case 6: /* Upgrade DCISW to DCCISW, as per HCR.SWIO */
  82. case 14: /* DCCISW */
  83. asm volatile("mcr p15, 0, %0, c7, c14, 2" : : "r" (val));
  84. break;
  85. case 10: /* DCCSW */
  86. asm volatile("mcr p15, 0, %0, c7, c10, 2" : : "r" (val));
  87. break;
  88. }
  89. done:
  90. put_cpu();
  91. return true;
  92. }
  93. /*
  94. * We could trap ID_DFR0 and tell the guest we don't support performance
  95. * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
  96. * NAKed, so it will read the PMCR anyway.
  97. *
  98. * Therefore we tell the guest we have 0 counters. Unfortunately, we
  99. * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
  100. * all PM registers, which doesn't crash the guest kernel at least.
  101. */
  102. static bool pm_fake(struct kvm_vcpu *vcpu,
  103. const struct coproc_params *p,
  104. const struct coproc_reg *r)
  105. {
  106. if (p->is_write)
  107. return ignore_write(vcpu, p);
  108. else
  109. return read_zero(vcpu, p);
  110. }
  111. #define access_pmcr pm_fake
  112. #define access_pmcntenset pm_fake
  113. #define access_pmcntenclr pm_fake
  114. #define access_pmovsr pm_fake
  115. #define access_pmselr pm_fake
  116. #define access_pmceid0 pm_fake
  117. #define access_pmceid1 pm_fake
  118. #define access_pmccntr pm_fake
  119. #define access_pmxevtyper pm_fake
  120. #define access_pmxevcntr pm_fake
  121. #define access_pmuserenr pm_fake
  122. #define access_pmintenset pm_fake
  123. #define access_pmintenclr pm_fake
  124. /* Architected CP15 registers.
  125. * Important: Must be sorted ascending by CRn, CRM, Op1, Op2
  126. */
  127. static const struct coproc_reg cp15_regs[] = {
  128. /* CSSELR: swapped by interrupt.S. */
  129. { CRn( 0), CRm( 0), Op1( 2), Op2( 0), is32,
  130. NULL, reset_unknown, c0_CSSELR },
  131. /* TTBR0/TTBR1: swapped by interrupt.S. */
  132. { CRm( 2), Op1( 0), is64, NULL, reset_unknown64, c2_TTBR0 },
  133. { CRm( 2), Op1( 1), is64, NULL, reset_unknown64, c2_TTBR1 },
  134. /* TTBCR: swapped by interrupt.S. */
  135. { CRn( 2), CRm( 0), Op1( 0), Op2( 2), is32,
  136. NULL, reset_val, c2_TTBCR, 0x00000000 },
  137. /* DACR: swapped by interrupt.S. */
  138. { CRn( 3), CRm( 0), Op1( 0), Op2( 0), is32,
  139. NULL, reset_unknown, c3_DACR },
  140. /* DFSR/IFSR/ADFSR/AIFSR: swapped by interrupt.S. */
  141. { CRn( 5), CRm( 0), Op1( 0), Op2( 0), is32,
  142. NULL, reset_unknown, c5_DFSR },
  143. { CRn( 5), CRm( 0), Op1( 0), Op2( 1), is32,
  144. NULL, reset_unknown, c5_IFSR },
  145. { CRn( 5), CRm( 1), Op1( 0), Op2( 0), is32,
  146. NULL, reset_unknown, c5_ADFSR },
  147. { CRn( 5), CRm( 1), Op1( 0), Op2( 1), is32,
  148. NULL, reset_unknown, c5_AIFSR },
  149. /* DFAR/IFAR: swapped by interrupt.S. */
  150. { CRn( 6), CRm( 0), Op1( 0), Op2( 0), is32,
  151. NULL, reset_unknown, c6_DFAR },
  152. { CRn( 6), CRm( 0), Op1( 0), Op2( 2), is32,
  153. NULL, reset_unknown, c6_IFAR },
  154. /*
  155. * DC{C,I,CI}SW operations:
  156. */
  157. { CRn( 7), CRm( 6), Op1( 0), Op2( 2), is32, access_dcsw},
  158. { CRn( 7), CRm(10), Op1( 0), Op2( 2), is32, access_dcsw},
  159. { CRn( 7), CRm(14), Op1( 0), Op2( 2), is32, access_dcsw},
  160. /*
  161. * Dummy performance monitor implementation.
  162. */
  163. { CRn( 9), CRm(12), Op1( 0), Op2( 0), is32, access_pmcr},
  164. { CRn( 9), CRm(12), Op1( 0), Op2( 1), is32, access_pmcntenset},
  165. { CRn( 9), CRm(12), Op1( 0), Op2( 2), is32, access_pmcntenclr},
  166. { CRn( 9), CRm(12), Op1( 0), Op2( 3), is32, access_pmovsr},
  167. { CRn( 9), CRm(12), Op1( 0), Op2( 5), is32, access_pmselr},
  168. { CRn( 9), CRm(12), Op1( 0), Op2( 6), is32, access_pmceid0},
  169. { CRn( 9), CRm(12), Op1( 0), Op2( 7), is32, access_pmceid1},
  170. { CRn( 9), CRm(13), Op1( 0), Op2( 0), is32, access_pmccntr},
  171. { CRn( 9), CRm(13), Op1( 0), Op2( 1), is32, access_pmxevtyper},
  172. { CRn( 9), CRm(13), Op1( 0), Op2( 2), is32, access_pmxevcntr},
  173. { CRn( 9), CRm(14), Op1( 0), Op2( 0), is32, access_pmuserenr},
  174. { CRn( 9), CRm(14), Op1( 0), Op2( 1), is32, access_pmintenset},
  175. { CRn( 9), CRm(14), Op1( 0), Op2( 2), is32, access_pmintenclr},
  176. /* PRRR/NMRR (aka MAIR0/MAIR1): swapped by interrupt.S. */
  177. { CRn(10), CRm( 2), Op1( 0), Op2( 0), is32,
  178. NULL, reset_unknown, c10_PRRR},
  179. { CRn(10), CRm( 2), Op1( 0), Op2( 1), is32,
  180. NULL, reset_unknown, c10_NMRR},
  181. /* VBAR: swapped by interrupt.S. */
  182. { CRn(12), CRm( 0), Op1( 0), Op2( 0), is32,
  183. NULL, reset_val, c12_VBAR, 0x00000000 },
  184. /* CONTEXTIDR/TPIDRURW/TPIDRURO/TPIDRPRW: swapped by interrupt.S. */
  185. { CRn(13), CRm( 0), Op1( 0), Op2( 1), is32,
  186. NULL, reset_val, c13_CID, 0x00000000 },
  187. { CRn(13), CRm( 0), Op1( 0), Op2( 2), is32,
  188. NULL, reset_unknown, c13_TID_URW },
  189. { CRn(13), CRm( 0), Op1( 0), Op2( 3), is32,
  190. NULL, reset_unknown, c13_TID_URO },
  191. { CRn(13), CRm( 0), Op1( 0), Op2( 4), is32,
  192. NULL, reset_unknown, c13_TID_PRIV },
  193. };
  194. /* Target specific emulation tables */
  195. static struct kvm_coproc_target_table *target_tables[KVM_ARM_NUM_TARGETS];
  196. void kvm_register_target_coproc_table(struct kvm_coproc_target_table *table)
  197. {
  198. target_tables[table->target] = table;
  199. }
  200. /* Get specific register table for this target. */
  201. static const struct coproc_reg *get_target_table(unsigned target, size_t *num)
  202. {
  203. struct kvm_coproc_target_table *table;
  204. table = target_tables[target];
  205. *num = table->num;
  206. return table->table;
  207. }
  208. static const struct coproc_reg *find_reg(const struct coproc_params *params,
  209. const struct coproc_reg table[],
  210. unsigned int num)
  211. {
  212. unsigned int i;
  213. for (i = 0; i < num; i++) {
  214. const struct coproc_reg *r = &table[i];
  215. if (params->is_64bit != r->is_64)
  216. continue;
  217. if (params->CRn != r->CRn)
  218. continue;
  219. if (params->CRm != r->CRm)
  220. continue;
  221. if (params->Op1 != r->Op1)
  222. continue;
  223. if (params->Op2 != r->Op2)
  224. continue;
  225. return r;
  226. }
  227. return NULL;
  228. }
  229. static int emulate_cp15(struct kvm_vcpu *vcpu,
  230. const struct coproc_params *params)
  231. {
  232. size_t num;
  233. const struct coproc_reg *table, *r;
  234. trace_kvm_emulate_cp15_imp(params->Op1, params->Rt1, params->CRn,
  235. params->CRm, params->Op2, params->is_write);
  236. table = get_target_table(vcpu->arch.target, &num);
  237. /* Search target-specific then generic table. */
  238. r = find_reg(params, table, num);
  239. if (!r)
  240. r = find_reg(params, cp15_regs, ARRAY_SIZE(cp15_regs));
  241. if (likely(r)) {
  242. /* If we don't have an accessor, we should never get here! */
  243. BUG_ON(!r->access);
  244. if (likely(r->access(vcpu, params, r))) {
  245. /* Skip instruction, since it was emulated */
  246. kvm_skip_instr(vcpu, (vcpu->arch.hsr >> 25) & 1);
  247. return 1;
  248. }
  249. /* If access function fails, it should complain. */
  250. } else {
  251. kvm_err("Unsupported guest CP15 access at: %08x\n",
  252. *vcpu_pc(vcpu));
  253. print_cp_instr(params);
  254. }
  255. kvm_inject_undefined(vcpu);
  256. return 1;
  257. }
  258. /**
  259. * kvm_handle_cp15_64 -- handles a mrrc/mcrr trap on a guest CP15 access
  260. * @vcpu: The VCPU pointer
  261. * @run: The kvm_run struct
  262. */
  263. int kvm_handle_cp15_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
  264. {
  265. struct coproc_params params;
  266. params.CRm = (vcpu->arch.hsr >> 1) & 0xf;
  267. params.Rt1 = (vcpu->arch.hsr >> 5) & 0xf;
  268. params.is_write = ((vcpu->arch.hsr & 1) == 0);
  269. params.is_64bit = true;
  270. params.Op1 = (vcpu->arch.hsr >> 16) & 0xf;
  271. params.Op2 = 0;
  272. params.Rt2 = (vcpu->arch.hsr >> 10) & 0xf;
  273. params.CRn = 0;
  274. return emulate_cp15(vcpu, &params);
  275. }
  276. static void reset_coproc_regs(struct kvm_vcpu *vcpu,
  277. const struct coproc_reg *table, size_t num)
  278. {
  279. unsigned long i;
  280. for (i = 0; i < num; i++)
  281. if (table[i].reset)
  282. table[i].reset(vcpu, &table[i]);
  283. }
  284. /**
  285. * kvm_handle_cp15_32 -- handles a mrc/mcr trap on a guest CP15 access
  286. * @vcpu: The VCPU pointer
  287. * @run: The kvm_run struct
  288. */
  289. int kvm_handle_cp15_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
  290. {
  291. struct coproc_params params;
  292. params.CRm = (vcpu->arch.hsr >> 1) & 0xf;
  293. params.Rt1 = (vcpu->arch.hsr >> 5) & 0xf;
  294. params.is_write = ((vcpu->arch.hsr & 1) == 0);
  295. params.is_64bit = false;
  296. params.CRn = (vcpu->arch.hsr >> 10) & 0xf;
  297. params.Op1 = (vcpu->arch.hsr >> 14) & 0x7;
  298. params.Op2 = (vcpu->arch.hsr >> 17) & 0x7;
  299. params.Rt2 = 0;
  300. return emulate_cp15(vcpu, &params);
  301. }
  302. /******************************************************************************
  303. * Userspace API
  304. *****************************************************************************/
  305. static bool index_to_params(u64 id, struct coproc_params *params)
  306. {
  307. switch (id & KVM_REG_SIZE_MASK) {
  308. case KVM_REG_SIZE_U32:
  309. /* Any unused index bits means it's not valid. */
  310. if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
  311. | KVM_REG_ARM_COPROC_MASK
  312. | KVM_REG_ARM_32_CRN_MASK
  313. | KVM_REG_ARM_CRM_MASK
  314. | KVM_REG_ARM_OPC1_MASK
  315. | KVM_REG_ARM_32_OPC2_MASK))
  316. return false;
  317. params->is_64bit = false;
  318. params->CRn = ((id & KVM_REG_ARM_32_CRN_MASK)
  319. >> KVM_REG_ARM_32_CRN_SHIFT);
  320. params->CRm = ((id & KVM_REG_ARM_CRM_MASK)
  321. >> KVM_REG_ARM_CRM_SHIFT);
  322. params->Op1 = ((id & KVM_REG_ARM_OPC1_MASK)
  323. >> KVM_REG_ARM_OPC1_SHIFT);
  324. params->Op2 = ((id & KVM_REG_ARM_32_OPC2_MASK)
  325. >> KVM_REG_ARM_32_OPC2_SHIFT);
  326. return true;
  327. case KVM_REG_SIZE_U64:
  328. /* Any unused index bits means it's not valid. */
  329. if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
  330. | KVM_REG_ARM_COPROC_MASK
  331. | KVM_REG_ARM_CRM_MASK
  332. | KVM_REG_ARM_OPC1_MASK))
  333. return false;
  334. params->is_64bit = true;
  335. params->CRm = ((id & KVM_REG_ARM_CRM_MASK)
  336. >> KVM_REG_ARM_CRM_SHIFT);
  337. params->Op1 = ((id & KVM_REG_ARM_OPC1_MASK)
  338. >> KVM_REG_ARM_OPC1_SHIFT);
  339. params->Op2 = 0;
  340. params->CRn = 0;
  341. return true;
  342. default:
  343. return false;
  344. }
  345. }
  346. /* Decode an index value, and find the cp15 coproc_reg entry. */
  347. static const struct coproc_reg *index_to_coproc_reg(struct kvm_vcpu *vcpu,
  348. u64 id)
  349. {
  350. size_t num;
  351. const struct coproc_reg *table, *r;
  352. struct coproc_params params;
  353. /* We only do cp15 for now. */
  354. if ((id & KVM_REG_ARM_COPROC_MASK) >> KVM_REG_ARM_COPROC_SHIFT != 15)
  355. return NULL;
  356. if (!index_to_params(id, &params))
  357. return NULL;
  358. table = get_target_table(vcpu->arch.target, &num);
  359. r = find_reg(&params, table, num);
  360. if (!r)
  361. r = find_reg(&params, cp15_regs, ARRAY_SIZE(cp15_regs));
  362. /* Not saved in the cp15 array? */
  363. if (r && !r->reg)
  364. r = NULL;
  365. return r;
  366. }
  367. /*
  368. * These are the invariant cp15 registers: we let the guest see the host
  369. * versions of these, so they're part of the guest state.
  370. *
  371. * A future CPU may provide a mechanism to present different values to
  372. * the guest, or a future kvm may trap them.
  373. */
  374. /* Unfortunately, there's no register-argument for mrc, so generate. */
  375. #define FUNCTION_FOR32(crn, crm, op1, op2, name) \
  376. static void get_##name(struct kvm_vcpu *v, \
  377. const struct coproc_reg *r) \
  378. { \
  379. u32 val; \
  380. \
  381. asm volatile("mrc p15, " __stringify(op1) \
  382. ", %0, c" __stringify(crn) \
  383. ", c" __stringify(crm) \
  384. ", " __stringify(op2) "\n" : "=r" (val)); \
  385. ((struct coproc_reg *)r)->val = val; \
  386. }
  387. FUNCTION_FOR32(0, 0, 0, 0, MIDR)
  388. FUNCTION_FOR32(0, 0, 0, 1, CTR)
  389. FUNCTION_FOR32(0, 0, 0, 2, TCMTR)
  390. FUNCTION_FOR32(0, 0, 0, 3, TLBTR)
  391. FUNCTION_FOR32(0, 0, 0, 6, REVIDR)
  392. FUNCTION_FOR32(0, 1, 0, 0, ID_PFR0)
  393. FUNCTION_FOR32(0, 1, 0, 1, ID_PFR1)
  394. FUNCTION_FOR32(0, 1, 0, 2, ID_DFR0)
  395. FUNCTION_FOR32(0, 1, 0, 3, ID_AFR0)
  396. FUNCTION_FOR32(0, 1, 0, 4, ID_MMFR0)
  397. FUNCTION_FOR32(0, 1, 0, 5, ID_MMFR1)
  398. FUNCTION_FOR32(0, 1, 0, 6, ID_MMFR2)
  399. FUNCTION_FOR32(0, 1, 0, 7, ID_MMFR3)
  400. FUNCTION_FOR32(0, 2, 0, 0, ID_ISAR0)
  401. FUNCTION_FOR32(0, 2, 0, 1, ID_ISAR1)
  402. FUNCTION_FOR32(0, 2, 0, 2, ID_ISAR2)
  403. FUNCTION_FOR32(0, 2, 0, 3, ID_ISAR3)
  404. FUNCTION_FOR32(0, 2, 0, 4, ID_ISAR4)
  405. FUNCTION_FOR32(0, 2, 0, 5, ID_ISAR5)
  406. FUNCTION_FOR32(0, 0, 1, 1, CLIDR)
  407. FUNCTION_FOR32(0, 0, 1, 7, AIDR)
  408. /* ->val is filled in by kvm_invariant_coproc_table_init() */
  409. static struct coproc_reg invariant_cp15[] = {
  410. { CRn( 0), CRm( 0), Op1( 0), Op2( 0), is32, NULL, get_MIDR },
  411. { CRn( 0), CRm( 0), Op1( 0), Op2( 1), is32, NULL, get_CTR },
  412. { CRn( 0), CRm( 0), Op1( 0), Op2( 2), is32, NULL, get_TCMTR },
  413. { CRn( 0), CRm( 0), Op1( 0), Op2( 3), is32, NULL, get_TLBTR },
  414. { CRn( 0), CRm( 0), Op1( 0), Op2( 6), is32, NULL, get_REVIDR },
  415. { CRn( 0), CRm( 1), Op1( 0), Op2( 0), is32, NULL, get_ID_PFR0 },
  416. { CRn( 0), CRm( 1), Op1( 0), Op2( 1), is32, NULL, get_ID_PFR1 },
  417. { CRn( 0), CRm( 1), Op1( 0), Op2( 2), is32, NULL, get_ID_DFR0 },
  418. { CRn( 0), CRm( 1), Op1( 0), Op2( 3), is32, NULL, get_ID_AFR0 },
  419. { CRn( 0), CRm( 1), Op1( 0), Op2( 4), is32, NULL, get_ID_MMFR0 },
  420. { CRn( 0), CRm( 1), Op1( 0), Op2( 5), is32, NULL, get_ID_MMFR1 },
  421. { CRn( 0), CRm( 1), Op1( 0), Op2( 6), is32, NULL, get_ID_MMFR2 },
  422. { CRn( 0), CRm( 1), Op1( 0), Op2( 7), is32, NULL, get_ID_MMFR3 },
  423. { CRn( 0), CRm( 2), Op1( 0), Op2( 0), is32, NULL, get_ID_ISAR0 },
  424. { CRn( 0), CRm( 2), Op1( 0), Op2( 1), is32, NULL, get_ID_ISAR1 },
  425. { CRn( 0), CRm( 2), Op1( 0), Op2( 2), is32, NULL, get_ID_ISAR2 },
  426. { CRn( 0), CRm( 2), Op1( 0), Op2( 3), is32, NULL, get_ID_ISAR3 },
  427. { CRn( 0), CRm( 2), Op1( 0), Op2( 4), is32, NULL, get_ID_ISAR4 },
  428. { CRn( 0), CRm( 2), Op1( 0), Op2( 5), is32, NULL, get_ID_ISAR5 },
  429. { CRn( 0), CRm( 0), Op1( 1), Op2( 1), is32, NULL, get_CLIDR },
  430. { CRn( 0), CRm( 0), Op1( 1), Op2( 7), is32, NULL, get_AIDR },
  431. };
  432. static int reg_from_user(void *val, const void __user *uaddr, u64 id)
  433. {
  434. /* This Just Works because we are little endian. */
  435. if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
  436. return -EFAULT;
  437. return 0;
  438. }
  439. static int reg_to_user(void __user *uaddr, const void *val, u64 id)
  440. {
  441. /* This Just Works because we are little endian. */
  442. if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
  443. return -EFAULT;
  444. return 0;
  445. }
  446. static int get_invariant_cp15(u64 id, void __user *uaddr)
  447. {
  448. struct coproc_params params;
  449. const struct coproc_reg *r;
  450. if (!index_to_params(id, &params))
  451. return -ENOENT;
  452. r = find_reg(&params, invariant_cp15, ARRAY_SIZE(invariant_cp15));
  453. if (!r)
  454. return -ENOENT;
  455. return reg_to_user(uaddr, &r->val, id);
  456. }
  457. static int set_invariant_cp15(u64 id, void __user *uaddr)
  458. {
  459. struct coproc_params params;
  460. const struct coproc_reg *r;
  461. int err;
  462. u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
  463. if (!index_to_params(id, &params))
  464. return -ENOENT;
  465. r = find_reg(&params, invariant_cp15, ARRAY_SIZE(invariant_cp15));
  466. if (!r)
  467. return -ENOENT;
  468. err = reg_from_user(&val, uaddr, id);
  469. if (err)
  470. return err;
  471. /* This is what we mean by invariant: you can't change it. */
  472. if (r->val != val)
  473. return -EINVAL;
  474. return 0;
  475. }
  476. static bool is_valid_cache(u32 val)
  477. {
  478. u32 level, ctype;
  479. if (val >= CSSELR_MAX)
  480. return -ENOENT;
  481. /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
  482. level = (val >> 1);
  483. ctype = (cache_levels >> (level * 3)) & 7;
  484. switch (ctype) {
  485. case 0: /* No cache */
  486. return false;
  487. case 1: /* Instruction cache only */
  488. return (val & 1);
  489. case 2: /* Data cache only */
  490. case 4: /* Unified cache */
  491. return !(val & 1);
  492. case 3: /* Separate instruction and data caches */
  493. return true;
  494. default: /* Reserved: we can't know instruction or data. */
  495. return false;
  496. }
  497. }
  498. /* Which cache CCSIDR represents depends on CSSELR value. */
  499. static u32 get_ccsidr(u32 csselr)
  500. {
  501. u32 ccsidr;
  502. /* Make sure noone else changes CSSELR during this! */
  503. local_irq_disable();
  504. /* Put value into CSSELR */
  505. asm volatile("mcr p15, 2, %0, c0, c0, 0" : : "r" (csselr));
  506. isb();
  507. /* Read result out of CCSIDR */
  508. asm volatile("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
  509. local_irq_enable();
  510. return ccsidr;
  511. }
  512. static int demux_c15_get(u64 id, void __user *uaddr)
  513. {
  514. u32 val;
  515. u32 __user *uval = uaddr;
  516. /* Fail if we have unknown bits set. */
  517. if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
  518. | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
  519. return -ENOENT;
  520. switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
  521. case KVM_REG_ARM_DEMUX_ID_CCSIDR:
  522. if (KVM_REG_SIZE(id) != 4)
  523. return -ENOENT;
  524. val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
  525. >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
  526. if (!is_valid_cache(val))
  527. return -ENOENT;
  528. return put_user(get_ccsidr(val), uval);
  529. default:
  530. return -ENOENT;
  531. }
  532. }
  533. static int demux_c15_set(u64 id, void __user *uaddr)
  534. {
  535. u32 val, newval;
  536. u32 __user *uval = uaddr;
  537. /* Fail if we have unknown bits set. */
  538. if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
  539. | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
  540. return -ENOENT;
  541. switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
  542. case KVM_REG_ARM_DEMUX_ID_CCSIDR:
  543. if (KVM_REG_SIZE(id) != 4)
  544. return -ENOENT;
  545. val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
  546. >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
  547. if (!is_valid_cache(val))
  548. return -ENOENT;
  549. if (get_user(newval, uval))
  550. return -EFAULT;
  551. /* This is also invariant: you can't change it. */
  552. if (newval != get_ccsidr(val))
  553. return -EINVAL;
  554. return 0;
  555. default:
  556. return -ENOENT;
  557. }
  558. }
  559. int kvm_arm_coproc_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  560. {
  561. const struct coproc_reg *r;
  562. void __user *uaddr = (void __user *)(long)reg->addr;
  563. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
  564. return demux_c15_get(reg->id, uaddr);
  565. r = index_to_coproc_reg(vcpu, reg->id);
  566. if (!r)
  567. return get_invariant_cp15(reg->id, uaddr);
  568. /* Note: copies two regs if size is 64 bit. */
  569. return reg_to_user(uaddr, &vcpu->arch.cp15[r->reg], reg->id);
  570. }
  571. int kvm_arm_coproc_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
  572. {
  573. const struct coproc_reg *r;
  574. void __user *uaddr = (void __user *)(long)reg->addr;
  575. if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
  576. return demux_c15_set(reg->id, uaddr);
  577. r = index_to_coproc_reg(vcpu, reg->id);
  578. if (!r)
  579. return set_invariant_cp15(reg->id, uaddr);
  580. /* Note: copies two regs if size is 64 bit */
  581. return reg_from_user(&vcpu->arch.cp15[r->reg], uaddr, reg->id);
  582. }
  583. static unsigned int num_demux_regs(void)
  584. {
  585. unsigned int i, count = 0;
  586. for (i = 0; i < CSSELR_MAX; i++)
  587. if (is_valid_cache(i))
  588. count++;
  589. return count;
  590. }
  591. static int write_demux_regids(u64 __user *uindices)
  592. {
  593. u64 val = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
  594. unsigned int i;
  595. val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
  596. for (i = 0; i < CSSELR_MAX; i++) {
  597. if (!is_valid_cache(i))
  598. continue;
  599. if (put_user(val | i, uindices))
  600. return -EFAULT;
  601. uindices++;
  602. }
  603. return 0;
  604. }
  605. static u64 cp15_to_index(const struct coproc_reg *reg)
  606. {
  607. u64 val = KVM_REG_ARM | (15 << KVM_REG_ARM_COPROC_SHIFT);
  608. if (reg->is_64) {
  609. val |= KVM_REG_SIZE_U64;
  610. val |= (reg->Op1 << KVM_REG_ARM_OPC1_SHIFT);
  611. val |= (reg->CRm << KVM_REG_ARM_CRM_SHIFT);
  612. } else {
  613. val |= KVM_REG_SIZE_U32;
  614. val |= (reg->Op1 << KVM_REG_ARM_OPC1_SHIFT);
  615. val |= (reg->Op2 << KVM_REG_ARM_32_OPC2_SHIFT);
  616. val |= (reg->CRm << KVM_REG_ARM_CRM_SHIFT);
  617. val |= (reg->CRn << KVM_REG_ARM_32_CRN_SHIFT);
  618. }
  619. return val;
  620. }
  621. static bool copy_reg_to_user(const struct coproc_reg *reg, u64 __user **uind)
  622. {
  623. if (!*uind)
  624. return true;
  625. if (put_user(cp15_to_index(reg), *uind))
  626. return false;
  627. (*uind)++;
  628. return true;
  629. }
  630. /* Assumed ordered tables, see kvm_coproc_table_init. */
  631. static int walk_cp15(struct kvm_vcpu *vcpu, u64 __user *uind)
  632. {
  633. const struct coproc_reg *i1, *i2, *end1, *end2;
  634. unsigned int total = 0;
  635. size_t num;
  636. /* We check for duplicates here, to allow arch-specific overrides. */
  637. i1 = get_target_table(vcpu->arch.target, &num);
  638. end1 = i1 + num;
  639. i2 = cp15_regs;
  640. end2 = cp15_regs + ARRAY_SIZE(cp15_regs);
  641. BUG_ON(i1 == end1 || i2 == end2);
  642. /* Walk carefully, as both tables may refer to the same register. */
  643. while (i1 || i2) {
  644. int cmp = cmp_reg(i1, i2);
  645. /* target-specific overrides generic entry. */
  646. if (cmp <= 0) {
  647. /* Ignore registers we trap but don't save. */
  648. if (i1->reg) {
  649. if (!copy_reg_to_user(i1, &uind))
  650. return -EFAULT;
  651. total++;
  652. }
  653. } else {
  654. /* Ignore registers we trap but don't save. */
  655. if (i2->reg) {
  656. if (!copy_reg_to_user(i2, &uind))
  657. return -EFAULT;
  658. total++;
  659. }
  660. }
  661. if (cmp <= 0 && ++i1 == end1)
  662. i1 = NULL;
  663. if (cmp >= 0 && ++i2 == end2)
  664. i2 = NULL;
  665. }
  666. return total;
  667. }
  668. unsigned long kvm_arm_num_coproc_regs(struct kvm_vcpu *vcpu)
  669. {
  670. return ARRAY_SIZE(invariant_cp15)
  671. + num_demux_regs()
  672. + walk_cp15(vcpu, (u64 __user *)NULL);
  673. }
  674. int kvm_arm_copy_coproc_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
  675. {
  676. unsigned int i;
  677. int err;
  678. /* Then give them all the invariant registers' indices. */
  679. for (i = 0; i < ARRAY_SIZE(invariant_cp15); i++) {
  680. if (put_user(cp15_to_index(&invariant_cp15[i]), uindices))
  681. return -EFAULT;
  682. uindices++;
  683. }
  684. err = walk_cp15(vcpu, uindices);
  685. if (err < 0)
  686. return err;
  687. uindices += err;
  688. return write_demux_regids(uindices);
  689. }
  690. void kvm_coproc_table_init(void)
  691. {
  692. unsigned int i;
  693. /* Make sure tables are unique and in order. */
  694. for (i = 1; i < ARRAY_SIZE(cp15_regs); i++)
  695. BUG_ON(cmp_reg(&cp15_regs[i-1], &cp15_regs[i]) >= 0);
  696. /* We abuse the reset function to overwrite the table itself. */
  697. for (i = 0; i < ARRAY_SIZE(invariant_cp15); i++)
  698. invariant_cp15[i].reset(NULL, &invariant_cp15[i]);
  699. /*
  700. * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
  701. *
  702. * If software reads the Cache Type fields from Ctype1
  703. * upwards, once it has seen a value of 0b000, no caches
  704. * exist at further-out levels of the hierarchy. So, for
  705. * example, if Ctype3 is the first Cache Type field with a
  706. * value of 0b000, the values of Ctype4 to Ctype7 must be
  707. * ignored.
  708. */
  709. asm volatile("mrc p15, 1, %0, c0, c0, 1" : "=r" (cache_levels));
  710. for (i = 0; i < 7; i++)
  711. if (((cache_levels >> (i*3)) & 7) == 0)
  712. break;
  713. /* Clear all higher bits. */
  714. cache_levels &= (1 << (i*3))-1;
  715. }
  716. /**
  717. * kvm_reset_coprocs - sets cp15 registers to reset value
  718. * @vcpu: The VCPU pointer
  719. *
  720. * This function finds the right table above and sets the registers on the
  721. * virtual CPU struct to their architecturally defined reset values.
  722. */
  723. void kvm_reset_coprocs(struct kvm_vcpu *vcpu)
  724. {
  725. size_t num;
  726. const struct coproc_reg *table;
  727. /* Catch someone adding a register without putting in reset entry. */
  728. memset(vcpu->arch.cp15, 0x42, sizeof(vcpu->arch.cp15));
  729. /* Generic chip reset first (so target could override). */
  730. reset_coproc_regs(vcpu, cp15_regs, ARRAY_SIZE(cp15_regs));
  731. table = get_target_table(vcpu->arch.target, &num);
  732. reset_coproc_regs(vcpu, table, num);
  733. for (num = 1; num < NR_CP15_REGS; num++)
  734. if (vcpu->arch.cp15[num] == 0x42424242)
  735. panic("Didn't reset vcpu->arch.cp15[%zi]", num);
  736. }