va_layout.c 8.3 KB

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