| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Based on arch/arm/mm/proc.S |
| 3 | * |
| 4 | * Copyright (C) 2001 Deep Blue Solutions Ltd. |
| 5 | * Copyright (C) 2012 ARM Ltd. |
| 6 | * Author: Catalin Marinas <catalin.marinas@arm.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/linkage.h> |
| 23 | #include <asm/assembler.h> |
| 24 | #include <asm/asm-offsets.h> |
| 25 | #include <asm/hwcap.h> |
| 26 | #include <asm/pgtable.h> |
| 27 | #include <asm/pgtable-hwdef.h> |
| 28 | #include <asm/cpufeature.h> |
| 29 | #include <asm/alternative.h> |
| 30 | |
| 31 | #ifdef CONFIG_ARM64_64K_PAGES |
| 32 | #define TCR_TG_FLAGS TCR_TG0_64K | TCR_TG1_64K |
| 33 | #elif defined(CONFIG_ARM64_16K_PAGES) |
| 34 | #define TCR_TG_FLAGS TCR_TG0_16K | TCR_TG1_16K |
| 35 | #else /* CONFIG_ARM64_4K_PAGES */ |
| 36 | #define TCR_TG_FLAGS TCR_TG0_4K | TCR_TG1_4K |
| 37 | #endif |
| 38 | |
| 39 | #ifdef CONFIG_RANDOMIZE_BASE |
| 40 | #define TCR_KASLR_FLAGS TCR_NFD1 |
| 41 | #else |
| 42 | #define TCR_KASLR_FLAGS 0 |
| 43 | #endif |
| 44 | |
| 45 | #define TCR_SMP_FLAGS TCR_SHARED |
| 46 | |
| 47 | /* PTWs cacheable, inner/outer WBWA */ |
| 48 | #define TCR_CACHE_FLAGS TCR_IRGN_WBWA | TCR_ORGN_WBWA |
| 49 | |
| 50 | #ifdef CONFIG_KASAN_SW_TAGS |
| 51 | #define TCR_KASAN_FLAGS TCR_TBI1 |
| 52 | #else |
| 53 | #define TCR_KASAN_FLAGS 0 |
| 54 | #endif |
| 55 | |
| 56 | #define MAIR(attr, mt) ((attr) << ((mt) * 8)) |
| 57 | |
| 58 | /* |
| 59 | * cpu_do_idle() |
| 60 | * |
| 61 | * Idle the processor (wait for interrupt). |
| 62 | */ |
| 63 | ENTRY(cpu_do_idle) |
| 64 | dsb sy // WFI may enter a low-power mode |
| 65 | wfi |
| 66 | ret |
| 67 | ENDPROC(cpu_do_idle) |
| 68 | |
| 69 | #ifdef CONFIG_CPU_PM |
| 70 | /** |
| 71 | * cpu_do_suspend - save CPU registers context |
| 72 | * |
| 73 | * x0: virtual address of context pointer |
| 74 | * |
| 75 | * This must be kept in sync with struct cpu_suspend_ctx in <asm/suspend.h>. |
| 76 | */ |
| 77 | ENTRY(cpu_do_suspend) |
| 78 | mrs x2, tpidr_el0 |
| 79 | mrs x3, tpidrro_el0 |
| 80 | mrs x4, contextidr_el1 |
| 81 | mrs x5, osdlr_el1 |
| 82 | mrs x6, cpacr_el1 |
| 83 | mrs x7, tcr_el1 |
| 84 | mrs x8, vbar_el1 |
| 85 | mrs x9, mdscr_el1 |
| 86 | mrs x10, oslsr_el1 |
| 87 | mrs x11, sctlr_el1 |
| 88 | alternative_if_not ARM64_HAS_VIRT_HOST_EXTN |
| 89 | mrs x12, tpidr_el1 |
| 90 | alternative_else |
| 91 | mrs x12, tpidr_el2 |
| 92 | alternative_endif |
| 93 | mrs x13, sp_el0 |
| 94 | stp x2, x3, [x0] |
| 95 | stp x4, x5, [x0, #16] |
| 96 | stp x6, x7, [x0, #32] |
| 97 | stp x8, x9, [x0, #48] |
| 98 | stp x10, x11, [x0, #64] |
| 99 | stp x12, x13, [x0, #80] |
| 100 | /* |
| 101 | * Save x18 as it may be used as a platform register, e.g. by shadow |
| 102 | * call stack. |
| 103 | */ |
| 104 | str x18, [x0, #96] |
| 105 | ret |
| 106 | ENDPROC(cpu_do_suspend) |
| 107 | |
| 108 | /** |
| 109 | * cpu_do_resume - restore CPU register context |
| 110 | * |
| 111 | * x0: Address of context pointer |
| 112 | */ |
| 113 | .pushsection ".idmap.text", "awx" |
| 114 | ENTRY(cpu_do_resume) |
| 115 | ldp x2, x3, [x0] |
| 116 | ldp x4, x5, [x0, #16] |
| 117 | ldp x6, x8, [x0, #32] |
| 118 | ldp x9, x10, [x0, #48] |
| 119 | ldp x11, x12, [x0, #64] |
| 120 | ldp x13, x14, [x0, #80] |
| 121 | /* |
| 122 | * Restore x18, as it may be used as a platform register, and clear |
| 123 | * the buffer to minimize the risk of exposure when used for shadow |
| 124 | * call stack. |
| 125 | */ |
| 126 | ldr x18, [x0, #96] |
| 127 | str xzr, [x0, #96] |
| 128 | msr tpidr_el0, x2 |
| 129 | msr tpidrro_el0, x3 |
| 130 | msr contextidr_el1, x4 |
| 131 | msr cpacr_el1, x6 |
| 132 | |
| 133 | /* Don't change t0sz here, mask those bits when restoring */ |
| 134 | mrs x7, tcr_el1 |
| 135 | bfi x8, x7, TCR_T0SZ_OFFSET, TCR_TxSZ_WIDTH |
| 136 | |
| 137 | msr tcr_el1, x8 |
| 138 | msr vbar_el1, x9 |
| 139 | |
| 140 | /* |
| 141 | * __cpu_setup() cleared MDSCR_EL1.MDE and friends, before unmasking |
| 142 | * debug exceptions. By restoring MDSCR_EL1 here, we may take a debug |
| 143 | * exception. Mask them until local_daif_restore() in cpu_suspend() |
| 144 | * resets them. |
| 145 | */ |
| 146 | disable_daif |
| 147 | msr mdscr_el1, x10 |
| 148 | |
| 149 | msr sctlr_el1, x12 |
| 150 | alternative_if_not ARM64_HAS_VIRT_HOST_EXTN |
| 151 | msr tpidr_el1, x13 |
| 152 | alternative_else |
| 153 | msr tpidr_el2, x13 |
| 154 | alternative_endif |
| 155 | msr sp_el0, x14 |
| 156 | /* |
| 157 | * Restore oslsr_el1 by writing oslar_el1 |
| 158 | */ |
| 159 | msr osdlr_el1, x5 |
| 160 | ubfx x11, x11, #1, #1 |
| 161 | msr oslar_el1, x11 |
| 162 | reset_pmuserenr_el0 x0 // Disable PMU access from EL0 |
| 163 | |
| 164 | alternative_if ARM64_HAS_RAS_EXTN |
| 165 | msr_s SYS_DISR_EL1, xzr |
| 166 | alternative_else_nop_endif |
| 167 | |
| 168 | isb |
| 169 | ret |
| 170 | ENDPROC(cpu_do_resume) |
| 171 | .popsection |
| 172 | #endif |
| 173 | |
| 174 | /* |
| 175 | * cpu_do_switch_mm(pgd_phys, tsk) |
| 176 | * |
| 177 | * Set the translation table base pointer to be pgd_phys. |
| 178 | * |
| 179 | * - pgd_phys - physical address of new TTB |
| 180 | */ |
| 181 | ENTRY(cpu_do_switch_mm) |
| 182 | mrs x2, ttbr1_el1 |
| 183 | mmid x1, x1 // get mm->context.id |
| 184 | phys_to_ttbr x3, x0 |
| 185 | #ifdef CONFIG_ARM64_SW_TTBR0_PAN |
| 186 | bfi x3, x1, #48, #16 // set the ASID field in TTBR0 |
| 187 | #endif |
| 188 | bfi x2, x1, #48, #16 // set the ASID |
| 189 | msr ttbr1_el1, x2 // in TTBR1 (since TCR.A1 is set) |
| 190 | isb |
| 191 | msr ttbr0_el1, x3 // now update TTBR0 |
| 192 | isb |
| 193 | b post_ttbr_update_workaround // Back to C code... |
| 194 | ENDPROC(cpu_do_switch_mm) |
| 195 | |
| 196 | .pushsection ".idmap.text", "awx" |
| 197 | |
| 198 | .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2 |
| 199 | adrp \tmp1, empty_zero_page |
| 200 | phys_to_ttbr \tmp2, \tmp1 |
| 201 | msr ttbr1_el1, \tmp2 |
| 202 | isb |
| 203 | tlbi vmalle1 |
| 204 | dsb nsh |
| 205 | isb |
| 206 | .endm |
| 207 | |
| 208 | /* |
| 209 | * void idmap_cpu_replace_ttbr1(phys_addr_t new_pgd) |
| 210 | * |
| 211 | * This is the low-level counterpart to cpu_replace_ttbr1, and should not be |
| 212 | * called by anything else. It can only be executed from a TTBR0 mapping. |
| 213 | */ |
| 214 | ENTRY(idmap_cpu_replace_ttbr1) |
| 215 | save_and_disable_daif flags=x2 |
| 216 | |
| 217 | __idmap_cpu_set_reserved_ttbr1 x1, x3 |
| 218 | |
| 219 | phys_to_ttbr x3, x0 |
| 220 | msr ttbr1_el1, x3 |
| 221 | isb |
| 222 | |
| 223 | restore_daif x2 |
| 224 | |
| 225 | ret |
| 226 | ENDPROC(idmap_cpu_replace_ttbr1) |
| 227 | .popsection |
| 228 | |
| 229 | #ifdef CONFIG_UNMAP_KERNEL_AT_EL0 |
| 230 | .pushsection ".idmap.text", "awx" |
| 231 | |
| 232 | .macro __idmap_kpti_get_pgtable_ent, type |
| 233 | dc cvac, cur_\()\type\()p // Ensure any existing dirty |
| 234 | dmb sy // lines are written back before |
| 235 | ldr \type, [cur_\()\type\()p] // loading the entry |
| 236 | tbz \type, #0, skip_\()\type // Skip invalid and |
| 237 | tbnz \type, #11, skip_\()\type // non-global entries |
| 238 | .endm |
| 239 | |
| 240 | .macro __idmap_kpti_put_pgtable_ent_ng, type |
| 241 | orr \type, \type, #PTE_NG // Same bit for blocks and pages |
| 242 | str \type, [cur_\()\type\()p] // Update the entry and ensure |
| 243 | dmb sy // that it is visible to all |
| 244 | dc civac, cur_\()\type\()p // CPUs. |
| 245 | .endm |
| 246 | |
| 247 | /* |
| 248 | * void __kpti_install_ng_mappings(int cpu, int num_cpus, phys_addr_t swapper) |
| 249 | * |
| 250 | * Called exactly once from stop_machine context by each CPU found during boot. |
| 251 | */ |
| 252 | __idmap_kpti_flag: |
| 253 | .long 1 |
| 254 | ENTRY(idmap_kpti_install_ng_mappings) |
| 255 | cpu .req w0 |
| 256 | num_cpus .req w1 |
| 257 | swapper_pa .req x2 |
| 258 | swapper_ttb .req x3 |
| 259 | flag_ptr .req x4 |
| 260 | cur_pgdp .req x5 |
| 261 | end_pgdp .req x6 |
| 262 | pgd .req x7 |
| 263 | cur_pudp .req x8 |
| 264 | end_pudp .req x9 |
| 265 | pud .req x10 |
| 266 | cur_pmdp .req x11 |
| 267 | end_pmdp .req x12 |
| 268 | pmd .req x13 |
| 269 | cur_ptep .req x14 |
| 270 | end_ptep .req x15 |
| 271 | pte .req x16 |
| 272 | |
| 273 | mrs swapper_ttb, ttbr1_el1 |
| 274 | adr flag_ptr, __idmap_kpti_flag |
| 275 | |
| 276 | cbnz cpu, __idmap_kpti_secondary |
| 277 | |
| 278 | /* We're the boot CPU. Wait for the others to catch up */ |
| 279 | sevl |
| 280 | 1: wfe |
| 281 | ldaxr w17, [flag_ptr] |
| 282 | eor w17, w17, num_cpus |
| 283 | cbnz w17, 1b |
| 284 | |
| 285 | /* We need to walk swapper, so turn off the MMU. */ |
| 286 | pre_disable_mmu_workaround |
| 287 | mrs x17, sctlr_el1 |
| 288 | bic x17, x17, #SCTLR_ELx_M |
| 289 | msr sctlr_el1, x17 |
| 290 | isb |
| 291 | |
| 292 | /* Everybody is enjoying the idmap, so we can rewrite swapper. */ |
| 293 | /* PGD */ |
| 294 | mov cur_pgdp, swapper_pa |
| 295 | add end_pgdp, cur_pgdp, #(PTRS_PER_PGD * 8) |
| 296 | do_pgd: __idmap_kpti_get_pgtable_ent pgd |
| 297 | tbnz pgd, #1, walk_puds |
| 298 | next_pgd: |
| 299 | __idmap_kpti_put_pgtable_ent_ng pgd |
| 300 | skip_pgd: |
| 301 | add cur_pgdp, cur_pgdp, #8 |
| 302 | cmp cur_pgdp, end_pgdp |
| 303 | b.ne do_pgd |
| 304 | |
| 305 | /* Publish the updated tables and nuke all the TLBs */ |
| 306 | dsb sy |
| 307 | tlbi vmalle1is |
| 308 | dsb ish |
| 309 | isb |
| 310 | |
| 311 | /* We're done: fire up the MMU again */ |
| 312 | mrs x17, sctlr_el1 |
| 313 | orr x17, x17, #SCTLR_ELx_M |
| 314 | msr sctlr_el1, x17 |
| 315 | isb |
| 316 | |
| 317 | /* |
| 318 | * Invalidate the local I-cache so that any instructions fetched |
| 319 | * speculatively from the PoC are discarded, since they may have |
| 320 | * been dynamically patched at the PoU. |
| 321 | */ |
| 322 | ic iallu |
| 323 | dsb nsh |
| 324 | isb |
| 325 | |
| 326 | /* Set the flag to zero to indicate that we're all done */ |
| 327 | str wzr, [flag_ptr] |
| 328 | ret |
| 329 | |
| 330 | /* PUD */ |
| 331 | walk_puds: |
| 332 | .if CONFIG_PGTABLE_LEVELS > 3 |
| 333 | pte_to_phys cur_pudp, pgd |
| 334 | add end_pudp, cur_pudp, #(PTRS_PER_PUD * 8) |
| 335 | do_pud: __idmap_kpti_get_pgtable_ent pud |
| 336 | tbnz pud, #1, walk_pmds |
| 337 | next_pud: |
| 338 | __idmap_kpti_put_pgtable_ent_ng pud |
| 339 | skip_pud: |
| 340 | add cur_pudp, cur_pudp, 8 |
| 341 | cmp cur_pudp, end_pudp |
| 342 | b.ne do_pud |
| 343 | b next_pgd |
| 344 | .else /* CONFIG_PGTABLE_LEVELS <= 3 */ |
| 345 | mov pud, pgd |
| 346 | b walk_pmds |
| 347 | next_pud: |
| 348 | b next_pgd |
| 349 | .endif |
| 350 | |
| 351 | /* PMD */ |
| 352 | walk_pmds: |
| 353 | .if CONFIG_PGTABLE_LEVELS > 2 |
| 354 | pte_to_phys cur_pmdp, pud |
| 355 | add end_pmdp, cur_pmdp, #(PTRS_PER_PMD * 8) |
| 356 | do_pmd: __idmap_kpti_get_pgtable_ent pmd |
| 357 | tbnz pmd, #1, walk_ptes |
| 358 | next_pmd: |
| 359 | __idmap_kpti_put_pgtable_ent_ng pmd |
| 360 | skip_pmd: |
| 361 | add cur_pmdp, cur_pmdp, #8 |
| 362 | cmp cur_pmdp, end_pmdp |
| 363 | b.ne do_pmd |
| 364 | b next_pud |
| 365 | .else /* CONFIG_PGTABLE_LEVELS <= 2 */ |
| 366 | mov pmd, pud |
| 367 | b walk_ptes |
| 368 | next_pmd: |
| 369 | b next_pud |
| 370 | .endif |
| 371 | |
| 372 | /* PTE */ |
| 373 | walk_ptes: |
| 374 | pte_to_phys cur_ptep, pmd |
| 375 | add end_ptep, cur_ptep, #(PTRS_PER_PTE * 8) |
| 376 | do_pte: __idmap_kpti_get_pgtable_ent pte |
| 377 | __idmap_kpti_put_pgtable_ent_ng pte |
| 378 | skip_pte: |
| 379 | add cur_ptep, cur_ptep, #8 |
| 380 | cmp cur_ptep, end_ptep |
| 381 | b.ne do_pte |
| 382 | b next_pmd |
| 383 | |
| 384 | .unreq cpu |
| 385 | .unreq num_cpus |
| 386 | .unreq swapper_pa |
| 387 | .unreq cur_pgdp |
| 388 | .unreq end_pgdp |
| 389 | .unreq pgd |
| 390 | .unreq cur_pudp |
| 391 | .unreq end_pudp |
| 392 | .unreq pud |
| 393 | .unreq cur_pmdp |
| 394 | .unreq end_pmdp |
| 395 | .unreq pmd |
| 396 | .unreq cur_ptep |
| 397 | .unreq end_ptep |
| 398 | .unreq pte |
| 399 | |
| 400 | /* Secondary CPUs end up here */ |
| 401 | __idmap_kpti_secondary: |
| 402 | /* Uninstall swapper before surgery begins */ |
| 403 | __idmap_cpu_set_reserved_ttbr1 x16, x17 |
| 404 | |
| 405 | /* Increment the flag to let the boot CPU we're ready */ |
| 406 | 1: ldxr w16, [flag_ptr] |
| 407 | add w16, w16, #1 |
| 408 | stxr w17, w16, [flag_ptr] |
| 409 | cbnz w17, 1b |
| 410 | |
| 411 | /* Wait for the boot CPU to finish messing around with swapper */ |
| 412 | sevl |
| 413 | 1: wfe |
| 414 | ldxr w16, [flag_ptr] |
| 415 | cbnz w16, 1b |
| 416 | |
| 417 | /* All done, act like nothing happened */ |
| 418 | msr ttbr1_el1, swapper_ttb |
| 419 | isb |
| 420 | ret |
| 421 | |
| 422 | .unreq swapper_ttb |
| 423 | .unreq flag_ptr |
| 424 | ENDPROC(idmap_kpti_install_ng_mappings) |
| 425 | .popsection |
| 426 | #endif |
| 427 | |
| 428 | /* |
| 429 | * __cpu_setup |
| 430 | * |
| 431 | * Initialise the processor for turning the MMU on. Return in x0 the |
| 432 | * value of the SCTLR_EL1 register. |
| 433 | */ |
| 434 | .pushsection ".idmap.text", "awx" |
| 435 | ENTRY(__cpu_setup) |
| 436 | tlbi vmalle1 // Invalidate local TLB |
| 437 | dsb nsh |
| 438 | |
| 439 | mov x0, #3 << 20 |
| 440 | msr cpacr_el1, x0 // Enable FP/ASIMD |
| 441 | mov x0, #1 << 12 // Reset mdscr_el1 and disable |
| 442 | msr mdscr_el1, x0 // access to the DCC from EL0 |
| 443 | isb // Unmask debug exceptions now, |
| 444 | enable_dbg // since this is per-cpu |
| 445 | reset_pmuserenr_el0 x0 // Disable PMU access from EL0 |
| 446 | /* |
| 447 | * Memory region attributes for LPAE: |
| 448 | * |
| 449 | * n = AttrIndx[2:0] |
| 450 | * n MAIR |
| 451 | * DEVICE_nGnRnE 000 00000000 |
| 452 | * DEVICE_nGnRE 001 00000100 |
| 453 | * DEVICE_GRE 010 00001100 |
| 454 | * NORMAL_NC 011 01000100 |
| 455 | * NORMAL 100 11111111 |
| 456 | * NORMAL_WT 101 10111011 |
| 457 | */ |
| 458 | ldr x5, =MAIR(0x00, MT_DEVICE_nGnRnE) | \ |
| 459 | MAIR(0x04, MT_DEVICE_nGnRE) | \ |
| 460 | MAIR(0x0c, MT_DEVICE_GRE) | \ |
| 461 | MAIR(0x44, MT_NORMAL_NC) | \ |
| 462 | MAIR(0xff, MT_NORMAL) | \ |
| 463 | MAIR(0xbb, MT_NORMAL_WT) |
| 464 | msr mair_el1, x5 |
| 465 | /* |
| 466 | * Prepare SCTLR |
| 467 | */ |
| 468 | mov_q x0, SCTLR_EL1_SET |
| 469 | /* |
| 470 | * Set/prepare TCR and TTBR. We use 512GB (39-bit) address range for |
| 471 | * both user and kernel. |
| 472 | */ |
| 473 | ldr x10, =TCR_TxSZ(VA_BITS) | TCR_CACHE_FLAGS | TCR_SMP_FLAGS | \ |
| 474 | TCR_TG_FLAGS | TCR_KASLR_FLAGS | TCR_ASID16 | \ |
| 475 | TCR_TBI0 | TCR_A1 | TCR_KASAN_FLAGS |
| 476 | tcr_set_idmap_t0sz x10, x9 |
| 477 | |
| 478 | /* |
| 479 | * Set the IPS bits in TCR_EL1. |
| 480 | */ |
| 481 | tcr_compute_pa_size x10, #TCR_IPS_SHIFT, x5, x6 |
| 482 | #ifdef CONFIG_ARM64_HW_AFDBM |
| 483 | /* |
| 484 | * Enable hardware update of the Access Flags bit. |
| 485 | * Hardware dirty bit management is enabled later, |
| 486 | * via capabilities. |
| 487 | */ |
| 488 | mrs x9, ID_AA64MMFR1_EL1 |
| 489 | and x9, x9, #0xf |
| 490 | cbz x9, 1f |
| 491 | orr x10, x10, #TCR_HA // hardware Access flag update |
| 492 | 1: |
| 493 | #endif /* CONFIG_ARM64_HW_AFDBM */ |
| 494 | msr tcr_el1, x10 |
| 495 | ret // return to head.S |
| 496 | ENDPROC(__cpu_setup) |