Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
0003  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License, version 2, as
0007  * published by the Free Software Foundation.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #include <linux/linkage.h>
0020 #include <asm/assembler.h>
0021 #include <asm/unified.h>
0022 #include <asm/asm-offsets.h>
0023 #include <asm/kvm_asm.h>
0024 #include <asm/kvm_arm.h>
0025 #include <asm/kvm_mmu.h>
0026 
0027 /********************************************************************
0028  * Hypervisor initialization
0029  *   - should be called with:
0030  *       r0 = top of Hyp stack (kernel VA)
0031  *       r1 = pointer to hyp vectors
0032  *       r2,r3 = Hypervisor pgd pointer
0033  *
0034  * The init scenario is:
0035  * - We jump in HYP with 3 parameters: runtime HYP pgd, runtime stack,
0036  *   runtime vectors
0037  * - Invalidate TLBs
0038  * - Set stack and vectors
0039  * - Setup the page tables
0040  * - Enable the MMU
0041  * - Profit! (or eret, if you only care about the code).
0042  */
0043 
0044     .text
0045     .pushsection    .hyp.idmap.text,"ax"
0046     .align 5
0047 __kvm_hyp_init:
0048     .globl __kvm_hyp_init
0049 
0050     @ Hyp-mode exception vector
0051     W(b)    .
0052     W(b)    .
0053     W(b)    .
0054     W(b)    .
0055     W(b)    .
0056     W(b)    __do_hyp_init
0057     W(b)    .
0058     W(b)    .
0059 
0060 __do_hyp_init:
0061     @ Set stack pointer
0062     mov sp, r0
0063 
0064     @ Set HVBAR to point to the HYP vectors
0065     mcr p15, 4, r1, c12, c0, 0  @ HVBAR
0066 
0067     @ Set the HTTBR to point to the hypervisor PGD pointer passed
0068     mcrr    p15, 4, rr_lo_hi(r2, r3), c2
0069 
0070     @ Set the HTCR and VTCR to the same shareability and cacheability
0071     @ settings as the non-secure TTBCR and with T0SZ == 0.
0072     mrc p15, 4, r0, c2, c0, 2   @ HTCR
0073     ldr r2, =HTCR_MASK
0074     bic r0, r0, r2
0075     mrc p15, 0, r1, c2, c0, 2   @ TTBCR
0076     and r1, r1, #(HTCR_MASK & ~TTBCR_T0SZ)
0077     orr r0, r0, r1
0078     mcr p15, 4, r0, c2, c0, 2   @ HTCR
0079 
0080     @ Use the same memory attributes for hyp. accesses as the kernel
0081     @ (copy MAIRx ro HMAIRx).
0082     mrc p15, 0, r0, c10, c2, 0
0083     mcr p15, 4, r0, c10, c2, 0
0084     mrc p15, 0, r0, c10, c2, 1
0085     mcr p15, 4, r0, c10, c2, 1
0086 
0087     @ Invalidate the stale TLBs from Bootloader
0088     mcr p15, 4, r0, c8, c7, 0   @ TLBIALLH
0089     dsb ish
0090 
0091     @ Set the HSCTLR to:
0092     @  - ARM/THUMB exceptions: Kernel config (Thumb-2 kernel)
0093     @  - Endianness: Kernel config
0094     @  - Fast Interrupt Features: Kernel config
0095     @  - Write permission implies XN: disabled
0096     @  - Instruction cache: enabled
0097     @  - Data/Unified cache: enabled
0098     @  - Memory alignment checks: enabled
0099     @  - MMU: enabled (this code must be run from an identity mapping)
0100     mrc p15, 4, r0, c1, c0, 0   @ HSCR
0101     ldr r2, =HSCTLR_MASK
0102     bic r0, r0, r2
0103     mrc p15, 0, r1, c1, c0, 0   @ SCTLR
0104     ldr r2, =(HSCTLR_EE | HSCTLR_FI | HSCTLR_I | HSCTLR_C)
0105     and r1, r1, r2
0106  ARM(   ldr r2, =(HSCTLR_M | HSCTLR_A)          )
0107  THUMB( ldr r2, =(HSCTLR_M | HSCTLR_A | HSCTLR_TE)      )
0108     orr r1, r1, r2
0109     orr r0, r0, r1
0110     mcr p15, 4, r0, c1, c0, 0   @ HSCR
0111     isb
0112 
0113     eret
0114 
0115     @ r0 : stub vectors address
0116 ENTRY(__kvm_hyp_reset)
0117     /* We're now in idmap, disable MMU */
0118     mrc p15, 4, r1, c1, c0, 0   @ HSCTLR
0119     ldr r2, =(HSCTLR_M | HSCTLR_A | HSCTLR_C | HSCTLR_I)
0120     bic r1, r1, r2
0121     mcr p15, 4, r1, c1, c0, 0   @ HSCTLR
0122 
0123     /* Install stub vectors */
0124     mcr p15, 4, r0, c12, c0, 0  @ HVBAR
0125     isb
0126 
0127     eret
0128 ENDPROC(__kvm_hyp_reset)
0129 
0130     .ltorg
0131 
0132     .globl __kvm_hyp_init_end
0133 __kvm_hyp_init_end:
0134 
0135     .popsection