Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/arch/powerpc/kvm/book3s_64_mmu_host.c
10817 views
1
/*
2
* Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
3
*
4
* Authors:
5
* Alexander Graf <[email protected]>
6
* Kevin Wolf <[email protected]>
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, write to the Free Software
19
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
*/
21
22
#include <linux/kvm_host.h>
23
24
#include <asm/kvm_ppc.h>
25
#include <asm/kvm_book3s.h>
26
#include <asm/mmu-hash64.h>
27
#include <asm/machdep.h>
28
#include <asm/mmu_context.h>
29
#include <asm/hw_irq.h>
30
#include "trace.h"
31
32
#define PTE_SIZE 12
33
34
void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
35
{
36
ppc_md.hpte_invalidate(pte->slot, pte->host_va,
37
MMU_PAGE_4K, MMU_SEGSIZE_256M,
38
false);
39
}
40
41
/* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using
42
* a hash, so we don't waste cycles on looping */
43
static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)
44
{
45
return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^
46
((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^
47
((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^
48
((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^
49
((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^
50
((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^
51
((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^
52
((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));
53
}
54
55
56
static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
57
{
58
struct kvmppc_sid_map *map;
59
u16 sid_map_mask;
60
61
if (vcpu->arch.shared->msr & MSR_PR)
62
gvsid |= VSID_PR;
63
64
sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
65
map = &to_book3s(vcpu)->sid_map[sid_map_mask];
66
if (map->valid && (map->guest_vsid == gvsid)) {
67
trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
68
return map;
69
}
70
71
map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];
72
if (map->valid && (map->guest_vsid == gvsid)) {
73
trace_kvm_book3s_slb_found(gvsid, map->host_vsid);
74
return map;
75
}
76
77
trace_kvm_book3s_slb_fail(sid_map_mask, gvsid);
78
return NULL;
79
}
80
81
int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)
82
{
83
pfn_t hpaddr;
84
ulong hash, hpteg, va;
85
u64 vsid;
86
int ret;
87
int rflags = 0x192;
88
int vflags = 0;
89
int attempt = 0;
90
struct kvmppc_sid_map *map;
91
92
/* Get host physical address for gpa */
93
hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);
94
if (is_error_pfn(hpaddr)) {
95
printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr);
96
return -EINVAL;
97
}
98
hpaddr <<= PAGE_SHIFT;
99
hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
100
101
/* and write the mapping ea -> hpa into the pt */
102
vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
103
map = find_sid_vsid(vcpu, vsid);
104
if (!map) {
105
ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
106
WARN_ON(ret < 0);
107
map = find_sid_vsid(vcpu, vsid);
108
}
109
if (!map) {
110
printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
111
vsid, orig_pte->eaddr);
112
WARN_ON(true);
113
return -EINVAL;
114
}
115
116
vsid = map->host_vsid;
117
va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M);
118
119
if (!orig_pte->may_write)
120
rflags |= HPTE_R_PP;
121
else
122
mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);
123
124
if (!orig_pte->may_execute)
125
rflags |= HPTE_R_N;
126
127
hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M);
128
129
map_again:
130
hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
131
132
/* In case we tried normal mapping already, let's nuke old entries */
133
if (attempt > 1)
134
if (ppc_md.hpte_remove(hpteg) < 0)
135
return -1;
136
137
ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M);
138
139
if (ret < 0) {
140
/* If we couldn't map a primary PTE, try a secondary */
141
hash = ~hash;
142
vflags ^= HPTE_V_SECONDARY;
143
attempt++;
144
goto map_again;
145
} else {
146
struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu);
147
148
trace_kvm_book3s_64_mmu_map(rflags, hpteg, va, hpaddr, orig_pte);
149
150
/* The ppc_md code may give us a secondary entry even though we
151
asked for a primary. Fix up. */
152
if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
153
hash = ~hash;
154
hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
155
}
156
157
pte->slot = hpteg + (ret & 7);
158
pte->host_va = va;
159
pte->pte = *orig_pte;
160
pte->pfn = hpaddr >> PAGE_SHIFT;
161
162
kvmppc_mmu_hpte_cache_map(vcpu, pte);
163
}
164
165
return 0;
166
}
167
168
static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
169
{
170
struct kvmppc_sid_map *map;
171
struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
172
u16 sid_map_mask;
173
static int backwards_map = 0;
174
175
if (vcpu->arch.shared->msr & MSR_PR)
176
gvsid |= VSID_PR;
177
178
/* We might get collisions that trap in preceding order, so let's
179
map them differently */
180
181
sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
182
if (backwards_map)
183
sid_map_mask = SID_MAP_MASK - sid_map_mask;
184
185
map = &to_book3s(vcpu)->sid_map[sid_map_mask];
186
187
/* Make sure we're taking the other map next time */
188
backwards_map = !backwards_map;
189
190
/* Uh-oh ... out of mappings. Let's flush! */
191
if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) {
192
vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
193
memset(vcpu_book3s->sid_map, 0,
194
sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);
195
kvmppc_mmu_pte_flush(vcpu, 0, 0);
196
kvmppc_mmu_flush_segments(vcpu);
197
}
198
map->host_vsid = vcpu_book3s->vsid_next++;
199
200
map->guest_vsid = gvsid;
201
map->valid = true;
202
203
trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid);
204
205
return map;
206
}
207
208
static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
209
{
210
int i;
211
int max_slb_size = 64;
212
int found_inval = -1;
213
int r;
214
215
if (!to_svcpu(vcpu)->slb_max)
216
to_svcpu(vcpu)->slb_max = 1;
217
218
/* Are we overwriting? */
219
for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) {
220
if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V))
221
found_inval = i;
222
else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid)
223
return i;
224
}
225
226
/* Found a spare entry that was invalidated before */
227
if (found_inval > 0)
228
return found_inval;
229
230
/* No spare invalid entry, so create one */
231
232
if (mmu_slb_size < 64)
233
max_slb_size = mmu_slb_size;
234
235
/* Overflowing -> purge */
236
if ((to_svcpu(vcpu)->slb_max) == max_slb_size)
237
kvmppc_mmu_flush_segments(vcpu);
238
239
r = to_svcpu(vcpu)->slb_max;
240
to_svcpu(vcpu)->slb_max++;
241
242
return r;
243
}
244
245
int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
246
{
247
u64 esid = eaddr >> SID_SHIFT;
248
u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V;
249
u64 slb_vsid = SLB_VSID_USER;
250
u64 gvsid;
251
int slb_index;
252
struct kvmppc_sid_map *map;
253
254
slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
255
256
if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
257
/* Invalidate an entry */
258
to_svcpu(vcpu)->slb[slb_index].esid = 0;
259
return -ENOENT;
260
}
261
262
map = find_sid_vsid(vcpu, gvsid);
263
if (!map)
264
map = create_sid_map(vcpu, gvsid);
265
266
map->guest_esid = esid;
267
268
slb_vsid |= (map->host_vsid << 12);
269
slb_vsid &= ~SLB_VSID_KP;
270
slb_esid |= slb_index;
271
272
to_svcpu(vcpu)->slb[slb_index].esid = slb_esid;
273
to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid;
274
275
trace_kvm_book3s_slbmte(slb_vsid, slb_esid);
276
277
return 0;
278
}
279
280
void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
281
{
282
to_svcpu(vcpu)->slb_max = 1;
283
to_svcpu(vcpu)->slb[0].esid = 0;
284
}
285
286
void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
287
{
288
kvmppc_mmu_hpte_destroy(vcpu);
289
__destroy_context(to_book3s(vcpu)->context_id[0]);
290
}
291
292
int kvmppc_mmu_init(struct kvm_vcpu *vcpu)
293
{
294
struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
295
int err;
296
297
err = __init_new_context();
298
if (err < 0)
299
return -1;
300
vcpu3s->context_id[0] = err;
301
302
vcpu3s->vsid_max = ((vcpu3s->context_id[0] + 1) << USER_ESID_BITS) - 1;
303
vcpu3s->vsid_first = vcpu3s->context_id[0] << USER_ESID_BITS;
304
vcpu3s->vsid_next = vcpu3s->vsid_first;
305
306
kvmppc_mmu_hpte_init(vcpu);
307
308
return 0;
309
}
310
311