coproc.c 29 KB

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