va_layout.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 ARM Ltd.
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #include <linux/kvm_host.h>
  7. #include <linux/random.h>
  8. #include <linux/memblock.h>
  9. #include <asm/alternative.h>
  10. #include <asm/debug-monitors.h>
  11. #include <asm/insn.h>
  12. #include <asm/kvm_mmu.h>
  13. #include <asm/memory.h>
  14. #include <asm/patching.h>
  15. /*
  16. * The LSB of the HYP VA tag
  17. */
  18. static u8 tag_lsb;
  19. /*
  20. * The HYP VA tag value with the region bit
  21. */
  22. static u64 tag_val;
  23. static u64 va_mask;
  24. /*
  25. * Compute HYP VA by using the same computation as kern_hyp_va().
  26. */
  27. static u64 __early_kern_hyp_va(u64 addr)
  28. {
  29. addr &= va_mask;
  30. addr |= tag_val << tag_lsb;
  31. return addr;
  32. }
  33. /*
  34. * Store a hyp VA <-> PA offset into a EL2-owned variable.
  35. */
  36. static void init_hyp_physvirt_offset(void)
  37. {
  38. u64 kern_va, hyp_va;
  39. /* Compute the offset from the hyp VA and PA of a random symbol. */
  40. kern_va = (u64)lm_alias(__hyp_text_start);
  41. hyp_va = __early_kern_hyp_va(kern_va);
  42. hyp_physvirt_offset = (s64)__pa(kern_va) - (s64)hyp_va;
  43. }
  44. /*
  45. * We want to generate a hyp VA with the following format (with V ==
  46. * vabits_actual):
  47. *
  48. * 63 ... V | V-1 | V-2 .. tag_lsb | tag_lsb - 1 .. 0
  49. * ---------------------------------------------------------
  50. * | 0000000 | hyp_va_msb | random tag | kern linear VA |
  51. * |--------- tag_val -----------|----- va_mask ---|
  52. *
  53. * which does not conflict with the idmap regions.
  54. */
  55. __init void kvm_compute_layout(void)
  56. {
  57. phys_addr_t idmap_addr = __pa_symbol(__hyp_idmap_text_start);
  58. u64 hyp_va_msb;
  59. /* Where is my RAM region? */
  60. hyp_va_msb = idmap_addr & BIT(vabits_actual - 1);
  61. hyp_va_msb ^= BIT(vabits_actual - 1);
  62. tag_lsb = fls64((u64)phys_to_virt(memblock_start_of_DRAM()) ^
  63. (u64)(high_memory - 1));
  64. va_mask = GENMASK_ULL(tag_lsb - 1, 0);
  65. tag_val = hyp_va_msb;
  66. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && tag_lsb != (vabits_actual - 1)) {
  67. /* We have some free bits to insert a random tag. */
  68. tag_val |= get_random_long() & GENMASK_ULL(vabits_actual - 2, tag_lsb);
  69. }
  70. tag_val >>= tag_lsb;
  71. init_hyp_physvirt_offset();
  72. }
  73. /*
  74. * The .hyp.reloc ELF section contains a list of kimg positions that
  75. * contains kimg VAs but will be accessed only in hyp execution context.
  76. * Convert them to hyp VAs. See gen-hyprel.c for more details.
  77. */
  78. __init void kvm_apply_hyp_relocations(void)
  79. {
  80. int32_t *rel;
  81. int32_t *begin = (int32_t *)__hyp_reloc_begin;
  82. int32_t *end = (int32_t *)__hyp_reloc_end;
  83. for (rel = begin; rel < end; ++rel) {
  84. uintptr_t *ptr, kimg_va;
  85. /*
  86. * Each entry contains a 32-bit relative offset from itself
  87. * to a kimg VA position.
  88. */
  89. ptr = (uintptr_t *)lm_alias((char *)rel + *rel);
  90. /* Read the kimg VA value at the relocation address. */
  91. kimg_va = *ptr;
  92. /* Convert to hyp VA and store back to the relocation address. */
  93. *ptr = __early_kern_hyp_va((uintptr_t)lm_alias(kimg_va));
  94. }
  95. }
  96. void kvm_apply_hyp_module_relocations(void *mod_start, void *hyp_va,
  97. kvm_nvhe_reloc_t *begin,
  98. kvm_nvhe_reloc_t *end)
  99. {
  100. kvm_nvhe_reloc_t *rel;
  101. for (rel = begin; rel < end; ++rel) {
  102. u32 **ptr, *va;
  103. /*
  104. * Each entry contains a 32-bit relative offset from itself
  105. * to a VA position in the module area.
  106. */
  107. ptr = (u32 **)((char *)rel + *rel);
  108. /* Read the module VA value at the relocation address. */
  109. va = *ptr;
  110. /* Convert the module VA of the reloc to a hyp VA */
  111. WARN_ON(aarch64_addr_write(ptr, (u64)(((void *)va - mod_start) + hyp_va)));
  112. }
  113. }
  114. static u32 compute_instruction(int n, u32 rd, u32 rn)
  115. {
  116. u32 insn = AARCH64_BREAK_FAULT;
  117. switch (n) {
  118. case 0:
  119. insn = aarch64_insn_gen_logical_immediate(AARCH64_INSN_LOGIC_AND,
  120. AARCH64_INSN_VARIANT_64BIT,
  121. rn, rd, va_mask);
  122. break;
  123. case 1:
  124. /* ROR is a variant of EXTR with Rm = Rn */
  125. insn = aarch64_insn_gen_extr(AARCH64_INSN_VARIANT_64BIT,
  126. rn, rn, rd,
  127. tag_lsb);
  128. break;
  129. case 2:
  130. insn = aarch64_insn_gen_add_sub_imm(rd, rn,
  131. tag_val & GENMASK(11, 0),
  132. AARCH64_INSN_VARIANT_64BIT,
  133. AARCH64_INSN_ADSB_ADD);
  134. break;
  135. case 3:
  136. insn = aarch64_insn_gen_add_sub_imm(rd, rn,
  137. tag_val & GENMASK(23, 12),
  138. AARCH64_INSN_VARIANT_64BIT,
  139. AARCH64_INSN_ADSB_ADD);
  140. break;
  141. case 4:
  142. /* ROR is a variant of EXTR with Rm = Rn */
  143. insn = aarch64_insn_gen_extr(AARCH64_INSN_VARIANT_64BIT,
  144. rn, rn, rd, 64 - tag_lsb);
  145. break;
  146. }
  147. return insn;
  148. }
  149. void __init kvm_update_va_mask(struct alt_instr *alt,
  150. __le32 *origptr, __le32 *updptr, int nr_inst)
  151. {
  152. int i;
  153. BUG_ON(nr_inst != 5);
  154. for (i = 0; i < nr_inst; i++) {
  155. u32 rd, rn, insn, oinsn;
  156. /*
  157. * VHE doesn't need any address translation, let's NOP
  158. * everything.
  159. *
  160. * Alternatively, if the tag is zero (because the layout
  161. * dictates it and we don't have any spare bits in the
  162. * address), NOP everything after masking the kernel VA.
  163. */
  164. if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN) || (!tag_val && i > 0)) {
  165. updptr[i] = cpu_to_le32(aarch64_insn_gen_nop());
  166. continue;
  167. }
  168. oinsn = le32_to_cpu(origptr[i]);
  169. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, oinsn);
  170. rn = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RN, oinsn);
  171. insn = compute_instruction(i, rd, rn);
  172. BUG_ON(insn == AARCH64_BREAK_FAULT);
  173. updptr[i] = cpu_to_le32(insn);
  174. }
  175. }
  176. void kvm_patch_vector_branch(struct alt_instr *alt,
  177. __le32 *origptr, __le32 *updptr, int nr_inst)
  178. {
  179. u64 addr;
  180. u32 insn;
  181. BUG_ON(nr_inst != 4);
  182. if (!cpus_have_cap(ARM64_SPECTRE_V3A) ||
  183. WARN_ON_ONCE(cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN)))
  184. return;
  185. /*
  186. * Compute HYP VA by using the same computation as kern_hyp_va()
  187. */
  188. addr = __early_kern_hyp_va((u64)kvm_ksym_ref(__kvm_hyp_vector));
  189. /* Use PC[10:7] to branch to the same vector in KVM */
  190. addr |= ((u64)origptr & GENMASK_ULL(10, 7));
  191. /*
  192. * Branch over the preamble in order to avoid the initial store on
  193. * the stack (which we already perform in the hardening vectors).
  194. */
  195. addr += KVM_VECTOR_PREAMBLE;
  196. /* movz x0, #(addr & 0xffff) */
  197. insn = aarch64_insn_gen_movewide(AARCH64_INSN_REG_0,
  198. (u16)addr,
  199. 0,
  200. AARCH64_INSN_VARIANT_64BIT,
  201. AARCH64_INSN_MOVEWIDE_ZERO);
  202. *updptr++ = cpu_to_le32(insn);
  203. /* movk x0, #((addr >> 16) & 0xffff), lsl #16 */
  204. insn = aarch64_insn_gen_movewide(AARCH64_INSN_REG_0,
  205. (u16)(addr >> 16),
  206. 16,
  207. AARCH64_INSN_VARIANT_64BIT,
  208. AARCH64_INSN_MOVEWIDE_KEEP);
  209. *updptr++ = cpu_to_le32(insn);
  210. /* movk x0, #((addr >> 32) & 0xffff), lsl #32 */
  211. insn = aarch64_insn_gen_movewide(AARCH64_INSN_REG_0,
  212. (u16)(addr >> 32),
  213. 32,
  214. AARCH64_INSN_VARIANT_64BIT,
  215. AARCH64_INSN_MOVEWIDE_KEEP);
  216. *updptr++ = cpu_to_le32(insn);
  217. /* br x0 */
  218. insn = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_0,
  219. AARCH64_INSN_BRANCH_NOLINK);
  220. *updptr++ = cpu_to_le32(insn);
  221. }
  222. static void generate_mov_q(u64 val, __le32 *origptr, __le32 *updptr, int nr_inst)
  223. {
  224. u32 insn, oinsn, rd;
  225. BUG_ON(nr_inst != 4);
  226. /* Compute target register */
  227. oinsn = le32_to_cpu(*origptr);
  228. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, oinsn);
  229. /* movz rd, #(val & 0xffff) */
  230. insn = aarch64_insn_gen_movewide(rd,
  231. (u16)val,
  232. 0,
  233. AARCH64_INSN_VARIANT_64BIT,
  234. AARCH64_INSN_MOVEWIDE_ZERO);
  235. *updptr++ = cpu_to_le32(insn);
  236. /* movk rd, #((val >> 16) & 0xffff), lsl #16 */
  237. insn = aarch64_insn_gen_movewide(rd,
  238. (u16)(val >> 16),
  239. 16,
  240. AARCH64_INSN_VARIANT_64BIT,
  241. AARCH64_INSN_MOVEWIDE_KEEP);
  242. *updptr++ = cpu_to_le32(insn);
  243. /* movk rd, #((val >> 32) & 0xffff), lsl #32 */
  244. insn = aarch64_insn_gen_movewide(rd,
  245. (u16)(val >> 32),
  246. 32,
  247. AARCH64_INSN_VARIANT_64BIT,
  248. AARCH64_INSN_MOVEWIDE_KEEP);
  249. *updptr++ = cpu_to_le32(insn);
  250. /* movk rd, #((val >> 48) & 0xffff), lsl #48 */
  251. insn = aarch64_insn_gen_movewide(rd,
  252. (u16)(val >> 48),
  253. 48,
  254. AARCH64_INSN_VARIANT_64BIT,
  255. AARCH64_INSN_MOVEWIDE_KEEP);
  256. *updptr++ = cpu_to_le32(insn);
  257. }
  258. void kvm_get_kimage_voffset(struct alt_instr *alt,
  259. __le32 *origptr, __le32 *updptr, int nr_inst)
  260. {
  261. generate_mov_q(kimage_voffset, origptr, updptr, nr_inst);
  262. }
  263. void kvm_compute_final_ctr_el0(struct alt_instr *alt,
  264. __le32 *origptr, __le32 *updptr, int nr_inst)
  265. {
  266. generate_mov_q(read_sanitised_ftr_reg(SYS_CTR_EL0),
  267. origptr, updptr, nr_inst);
  268. }