xref: /freebsd/sys/arm64/vmm/hyp.h (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2017 Alexandru Elisei <alexandru.elisei@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _VMM_HYP_H_
29 #define	_VMM_HYP_H_
30 
31 /*
32  * The translation tables for the hypervisor mode will hold mappings for kernel
33  * virtual addresses and an identity mapping (VA == PA) necessary when
34  * enabling/disabling the MMU.
35  *
36  * When in EL2 exception level the translation table base register is TTBR0_EL2
37  * and the virtual addresses generated by the CPU must be at the bottom of the
38  * memory, with the first 16 bits all set to zero:
39  *
40  *                  0x0000ffffffffffff  End hyp address space
41  *                  0x0000000000000000  Start of hyp address space
42  *
43  * To run code in hyp mode we need to convert kernel virtual addresses to
44  * addreses that fit into this address space.
45  *
46  * The kernel virtual address range is:
47  *
48  *                  0xffff007fffffffff  End of KVA
49  *                  0xffff000000000000  Kernel base address & start of KVA
50  *
51  * (see /sys/arm64/include/vmparam.h).
52  *
53  * We could convert the kernel virtual addresses to valid EL2 addresses by
54  * setting the first 16 bits to zero and thus mapping the kernel addresses in
55  * the bottom half of the EL2 address space, but then they might clash with the
56  * identity mapping addresses. Instead we map the kernel addresses in the upper
57  * half of the EL2 address space.
58  *
59  * The hypervisor address space will look like this:
60  *
61  *                  0x0000807fffffffff  End of KVA mapping
62  *                  0x0000800000000000  Start of KVA mapping
63  *
64  *                  0x00007fffffffffff  End of identity mapping
65  *                  0x0000000000000000  Start of identity mapping
66  *
67  * With the scheme we have 47 bits at our disposable for the identity map and
68  * another 47 bits for the kernel virtual addresses. For a maximum physical
69  * memory size of 128TB we are guaranteed to not have any clashes between
70  * addresses.
71  */
72 #define	HYP_VM_MIN_ADDRESS	0x0000000000000000
73 #define	HYP_VM_MAX_ADDRESS	0x0001000000000000
74 
75 /*
76  * When the vmm code is installed the following handles can be used by
77  * the host to call into EL2.
78  */
79 #define	HYP_CLEANUP		0x00000001
80 #define	HYP_ENTER_GUEST		0x00000002
81 #define	HYP_READ_REGISTER	0x00000003
82 #define	 HYP_REG_ICH_VTR	0x1
83 #define	 HYP_REG_CNTHCTL	0x2
84 #define	HYP_CLEAN_S2_TLBI	0x00000004
85 #define	HYP_DC_CIVAC		0x00000005
86 #define	HYP_EL2_TLBI		0x00000006
87 #define	 HYP_EL2_TLBI_ALL	0x1
88 #define	 HYP_EL2_TLBI_VA	0x2
89 #define	HYP_S2_TLBI_RANGE	0x00000010
90 #define	HYP_S2_TLBI_ALL		0x00000011
91 
92 /*
93  * When taking asynchronous exceptions, or interrupts, with the exception of the
94  * SError interrupt, the exception syndrome register is not updated with the
95  * exception code. We need to differentiate between the different exception
96  * types taken to EL2.
97  */
98 #define EXCP_TYPE_EL1_SYNC	0
99 #define EXCP_TYPE_EL1_IRQ	1
100 #define EXCP_TYPE_EL1_FIQ	2
101 #define EXCP_TYPE_EL1_ERROR	3
102 
103 #define EXCP_TYPE_EL2_SYNC	4
104 #define EXCP_TYPE_EL2_IRQ	5
105 #define EXCP_TYPE_EL2_FIQ	6
106 #define EXCP_TYPE_EL2_ERROR	7
107 
108 #define	EXCP_TYPE_MAINT_IRQ	8
109 /* Used internally in vmm_hyp.c */
110 #define	EXCP_TYPE_REENTER	9
111 
112 #define	HYP_GET_VECTOR_TABLE	-1
113 
114 #endif /* !_VMM_HYP_H_ */
115