va_layout.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2017 ARM Ltd.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/kvm_host.h>
  18. #include <linux/random.h>
  19. #include <linux/memblock.h>
  20. #include <asm/alternative.h>
  21. #include <asm/debug-monitors.h>
  22. #include <asm/insn.h>
  23. #include <asm/kvm_mmu.h>
  24. /*
  25. * The LSB of the random hyp VA tag or 0 if no randomization is used.
  26. */
  27. static u8 tag_lsb;
  28. /*
  29. * The random hyp VA tag value with the region bit if hyp randomization is used
  30. */
  31. static u64 tag_val;
  32. static u64 va_mask;
  33. static void compute_layout(void)
  34. {
  35. phys_addr_t idmap_addr = __pa_symbol(__hyp_idmap_text_start);
  36. u64 hyp_va_msb;
  37. int kva_msb;
  38. /* Where is my RAM region? */
  39. hyp_va_msb = idmap_addr & BIT(VA_BITS - 1);
  40. hyp_va_msb ^= BIT(VA_BITS - 1);
  41. kva_msb = fls64((u64)phys_to_virt(memblock_start_of_DRAM()) ^
  42. (u64)(high_memory - 1));
  43. if (kva_msb == (VA_BITS - 1)) {
  44. /*
  45. * No space in the address, let's compute the mask so
  46. * that it covers (VA_BITS - 1) bits, and the region
  47. * bit. The tag stays set to zero.
  48. */
  49. va_mask = BIT(VA_BITS - 1) - 1;
  50. va_mask |= hyp_va_msb;
  51. } else {
  52. /*
  53. * We do have some free bits to insert a random tag.
  54. * Hyp VAs are now created from kernel linear map VAs
  55. * using the following formula (with V == VA_BITS):
  56. *
  57. * 63 ... V | V-1 | V-2 .. tag_lsb | tag_lsb - 1 .. 0
  58. * ---------------------------------------------------------
  59. * | 0000000 | hyp_va_msb | random tag | kern linear VA |
  60. */
  61. tag_lsb = kva_msb;
  62. va_mask = GENMASK_ULL(tag_lsb - 1, 0);
  63. tag_val = get_random_long() & GENMASK_ULL(VA_BITS - 2, tag_lsb);
  64. tag_val |= hyp_va_msb;
  65. tag_val >>= tag_lsb;
  66. }
  67. }
  68. static u32 compute_instruction(int n, u32 rd, u32 rn)
  69. {
  70. u32 insn = AARCH64_BREAK_FAULT;
  71. switch (n) {
  72. case 0:
  73. insn = aarch64_insn_gen_logical_immediate(AARCH64_INSN_LOGIC_AND,
  74. AARCH64_INSN_VARIANT_64BIT,
  75. rn, rd, va_mask);
  76. break;
  77. case 1:
  78. /* ROR is a variant of EXTR with Rm = Rn */
  79. insn = aarch64_insn_gen_extr(AARCH64_INSN_VARIANT_64BIT,
  80. rn, rn, rd,
  81. tag_lsb);
  82. break;
  83. case 2:
  84. insn = aarch64_insn_gen_add_sub_imm(rd, rn,
  85. tag_val & GENMASK(11, 0),
  86. AARCH64_INSN_VARIANT_64BIT,
  87. AARCH64_INSN_ADSB_ADD);
  88. break;
  89. case 3:
  90. insn = aarch64_insn_gen_add_sub_imm(rd, rn,
  91. tag_val & GENMASK(23, 12),
  92. AARCH64_INSN_VARIANT_64BIT,
  93. AARCH64_INSN_ADSB_ADD);
  94. break;
  95. case 4:
  96. /* ROR is a variant of EXTR with Rm = Rn */
  97. insn = aarch64_insn_gen_extr(AARCH64_INSN_VARIANT_64BIT,
  98. rn, rn, rd, 64 - tag_lsb);
  99. break;
  100. }
  101. return insn;
  102. }
  103. void __init kvm_update_va_mask(struct alt_instr *alt,
  104. __le32 *origptr, __le32 *updptr, int nr_inst)
  105. {
  106. int i;
  107. BUG_ON(nr_inst != 5);
  108. if (!has_vhe() && !va_mask)
  109. compute_layout();
  110. for (i = 0; i < nr_inst; i++) {
  111. u32 rd, rn, insn, oinsn;
  112. /*
  113. * VHE doesn't need any address translation, let's NOP
  114. * everything.
  115. *
  116. * Alternatively, if we don't have any spare bits in
  117. * the address, NOP everything after masking that
  118. * kernel VA.
  119. */
  120. if (has_vhe() || (!tag_lsb && i > 0)) {
  121. updptr[i] = cpu_to_le32(aarch64_insn_gen_nop());
  122. continue;
  123. }
  124. oinsn = le32_to_cpu(origptr[i]);
  125. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD, oinsn);
  126. rn = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RN, oinsn);
  127. insn = compute_instruction(i, rd, rn);
  128. BUG_ON(insn == AARCH64_BREAK_FAULT);
  129. updptr[i] = cpu_to_le32(insn);
  130. }
  131. }
  132. void *__kvm_bp_vect_base;
  133. int __kvm_harden_el2_vector_slot;
  134. void kvm_patch_vector_branch(struct alt_instr *alt,
  135. __le32 *origptr, __le32 *updptr, int nr_inst)
  136. {
  137. u64 addr;
  138. u32 insn;
  139. BUG_ON(nr_inst != 5);
  140. if (has_vhe() || !cpus_have_const_cap(ARM64_HARDEN_EL2_VECTORS)) {
  141. WARN_ON_ONCE(cpus_have_const_cap(ARM64_HARDEN_EL2_VECTORS));
  142. return;
  143. }
  144. if (!va_mask)
  145. compute_layout();
  146. /*
  147. * Compute HYP VA by using the same computation as kern_hyp_va()
  148. */
  149. addr = (uintptr_t)kvm_ksym_ref(__kvm_hyp_vector);
  150. addr &= va_mask;
  151. addr |= tag_val << tag_lsb;
  152. /* Use PC[10:7] to branch to the same vector in KVM */
  153. addr |= ((u64)origptr & GENMASK_ULL(10, 7));
  154. /*
  155. * Branch to the second instruction in the vectors in order to
  156. * avoid the initial store on the stack (which we already
  157. * perform in the hardening vectors).
  158. */
  159. addr += AARCH64_INSN_SIZE;
  160. /* stp x0, x1, [sp, #-16]! */
  161. insn = aarch64_insn_gen_load_store_pair(AARCH64_INSN_REG_0,
  162. AARCH64_INSN_REG_1,
  163. AARCH64_INSN_REG_SP,
  164. -16,
  165. AARCH64_INSN_VARIANT_64BIT,
  166. AARCH64_INSN_LDST_STORE_PAIR_PRE_INDEX);
  167. *updptr++ = cpu_to_le32(insn);
  168. /* movz x0, #(addr & 0xffff) */
  169. insn = aarch64_insn_gen_movewide(AARCH64_INSN_REG_0,
  170. (u16)addr,
  171. 0,
  172. AARCH64_INSN_VARIANT_64BIT,
  173. AARCH64_INSN_MOVEWIDE_ZERO);
  174. *updptr++ = cpu_to_le32(insn);
  175. /* movk x0, #((addr >> 16) & 0xffff), lsl #16 */
  176. insn = aarch64_insn_gen_movewide(AARCH64_INSN_REG_0,
  177. (u16)(addr >> 16),
  178. 16,
  179. AARCH64_INSN_VARIANT_64BIT,
  180. AARCH64_INSN_MOVEWIDE_KEEP);
  181. *updptr++ = cpu_to_le32(insn);
  182. /* movk x0, #((addr >> 32) & 0xffff), lsl #32 */
  183. insn = aarch64_insn_gen_movewide(AARCH64_INSN_REG_0,
  184. (u16)(addr >> 32),
  185. 32,
  186. AARCH64_INSN_VARIANT_64BIT,
  187. AARCH64_INSN_MOVEWIDE_KEEP);
  188. *updptr++ = cpu_to_le32(insn);
  189. /* br x0 */
  190. insn = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_0,
  191. AARCH64_INSN_BRANCH_NOLINK);
  192. *updptr++ = cpu_to_le32(insn);
  193. }