1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License, version 2, as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program; if not, write to the Free Software
13  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14  *
15  * Copyright SUSE Linux Products GmbH 2009
16  *
17  * Authors: Alexander Graf <agraf@suse.de>
18  */
19 
20 #ifndef __ASM_KVM_BOOK3S_H__
21 #define __ASM_KVM_BOOK3S_H__
22 
23 #include <linux/types.h>
24 #include <linux/kvm_host.h>
25 #include <asm/kvm_book3s_asm.h>
26 
27 struct kvmppc_bat {
28 	u64 raw;
29 	u32 bepi;
30 	u32 bepi_mask;
31 	u32 brpn;
32 	u8 wimg;
33 	u8 pp;
34 	bool vs		: 1;
35 	bool vp		: 1;
36 };
37 
38 struct kvmppc_sid_map {
39 	u64 guest_vsid;
40 	u64 guest_esid;
41 	u64 host_vsid;
42 	bool valid	: 1;
43 };
44 
45 #define SID_MAP_BITS    9
46 #define SID_MAP_NUM     (1 << SID_MAP_BITS)
47 #define SID_MAP_MASK    (SID_MAP_NUM - 1)
48 
49 #ifdef CONFIG_PPC_BOOK3S_64
50 #define SID_CONTEXTS	1
51 #else
52 #define SID_CONTEXTS	128
53 #define VSID_POOL_SIZE	(SID_CONTEXTS * 16)
54 #endif
55 
56 struct hpte_cache {
57 	struct hlist_node list_pte;
58 	struct hlist_node list_pte_long;
59 	struct hlist_node list_vpte;
60 	struct hlist_node list_vpte_long;
61 	struct rcu_head rcu_head;
62 	u64 host_va;
63 	u64 pfn;
64 	ulong slot;
65 	struct kvmppc_pte pte;
66 };
67 
68 struct kvmppc_vcpu_book3s {
69 	struct kvm_vcpu vcpu;
70 	struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
71 	struct kvmppc_sid_map sid_map[SID_MAP_NUM];
72 	struct {
73 		u64 esid;
74 		u64 vsid;
75 	} slb_shadow[64];
76 	u8 slb_shadow_max;
77 	struct kvmppc_bat ibat[8];
78 	struct kvmppc_bat dbat[8];
79 	u64 hid[6];
80 	u64 gqr[8];
81 	u64 sdr1;
82 	u64 hior;
83 	u64 msr_mask;
84 #ifdef CONFIG_PPC_BOOK3S_32
85 	u32 vsid_pool[VSID_POOL_SIZE];
86 	u32 vsid_next;
87 #else
88 	u64 proto_vsid_first;
89 	u64 proto_vsid_max;
90 	u64 proto_vsid_next;
91 #endif
92 	int context_id[SID_CONTEXTS];
93 
94 	bool hior_explicit;		/* HIOR is set by ioctl, not PVR */
95 
96 	struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
97 	struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
98 	struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
99 	struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
100 	int hpte_cache_count;
101 	spinlock_t mmu_lock;
102 };
103 
104 #define CONTEXT_HOST		0
105 #define CONTEXT_GUEST		1
106 #define CONTEXT_GUEST_END	2
107 
108 #define VSID_REAL	0x1fffffffffc00000ULL
109 #define VSID_BAT	0x1fffffffffb00000ULL
110 #define VSID_REAL_DR	0x2000000000000000ULL
111 #define VSID_REAL_IR	0x4000000000000000ULL
112 #define VSID_PR		0x8000000000000000ULL
113 
114 extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
115 extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
116 extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
117 extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
118 extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
119 extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
120 extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
121 extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
122 extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
123 extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
124 extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
125 extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
126 			struct kvm_vcpu *vcpu, unsigned long addr,
127 			unsigned long status);
128 extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
129 			unsigned long slb_v, unsigned long valid);
130 
131 extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
132 extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
133 extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
134 extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
135 extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
136 extern int kvmppc_mmu_hpte_sysinit(void);
137 extern void kvmppc_mmu_hpte_sysexit(void);
138 extern int kvmppc_mmu_hv_init(void);
139 
140 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
141 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
142 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
143 extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
144 extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
145 			   bool upper, u32 val);
146 extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
147 extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
148 extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
149 extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
150 			unsigned long *rmap, long pte_index, int realmode);
151 extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
152 			unsigned long pte_index);
153 void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep,
154 			unsigned long pte_index);
155 extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
156 			unsigned long *nb_ret);
157 extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr);
158 extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
159 			long pte_index, unsigned long pteh, unsigned long ptel);
160 extern long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
161 			long pte_index, unsigned long pteh, unsigned long ptel);
162 extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
163 			struct kvm_memory_slot *memslot);
164 
165 extern void kvmppc_entry_trampoline(void);
166 extern void kvmppc_hv_entry_trampoline(void);
167 extern void kvmppc_load_up_fpu(void);
168 extern void kvmppc_load_up_altivec(void);
169 extern void kvmppc_load_up_vsx(void);
170 extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
171 extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
172 extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
173 
174 static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
175 {
176 	return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
177 }
178 
179 extern void kvm_return_point(void);
180 
181 /* Also add subarch specific defines */
182 
183 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
184 #include <asm/kvm_book3s_32.h>
185 #endif
186 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
187 #include <asm/kvm_book3s_64.h>
188 #endif
189 
190 #ifdef CONFIG_KVM_BOOK3S_PR
191 
192 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
193 {
194 	return to_book3s(vcpu)->hior;
195 }
196 
197 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
198 			unsigned long pending_now, unsigned long old_pending)
199 {
200 	if (pending_now)
201 		vcpu->arch.shared->int_pending = 1;
202 	else if (old_pending)
203 		vcpu->arch.shared->int_pending = 0;
204 }
205 
206 static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
207 {
208 	if ( num < 14 ) {
209 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
210 		svcpu->gpr[num] = val;
211 		svcpu_put(svcpu);
212 		to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
213 	} else
214 		vcpu->arch.gpr[num] = val;
215 }
216 
217 static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
218 {
219 	if ( num < 14 ) {
220 		struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
221 		ulong r = svcpu->gpr[num];
222 		svcpu_put(svcpu);
223 		return r;
224 	} else
225 		return vcpu->arch.gpr[num];
226 }
227 
228 static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
229 {
230 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
231 	svcpu->cr = val;
232 	svcpu_put(svcpu);
233 	to_book3s(vcpu)->shadow_vcpu->cr = val;
234 }
235 
236 static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
237 {
238 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
239 	u32 r;
240 	r = svcpu->cr;
241 	svcpu_put(svcpu);
242 	return r;
243 }
244 
245 static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
246 {
247 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
248 	svcpu->xer = val;
249 	to_book3s(vcpu)->shadow_vcpu->xer = val;
250 	svcpu_put(svcpu);
251 }
252 
253 static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
254 {
255 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
256 	u32 r;
257 	r = svcpu->xer;
258 	svcpu_put(svcpu);
259 	return r;
260 }
261 
262 static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
263 {
264 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
265 	svcpu->ctr = val;
266 	svcpu_put(svcpu);
267 }
268 
269 static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
270 {
271 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
272 	ulong r;
273 	r = svcpu->ctr;
274 	svcpu_put(svcpu);
275 	return r;
276 }
277 
278 static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
279 {
280 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
281 	svcpu->lr = val;
282 	svcpu_put(svcpu);
283 }
284 
285 static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
286 {
287 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
288 	ulong r;
289 	r = svcpu->lr;
290 	svcpu_put(svcpu);
291 	return r;
292 }
293 
294 static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
295 {
296 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
297 	svcpu->pc = val;
298 	svcpu_put(svcpu);
299 }
300 
301 static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
302 {
303 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
304 	ulong r;
305 	r = svcpu->pc;
306 	svcpu_put(svcpu);
307 	return r;
308 }
309 
310 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
311 {
312 	ulong pc = kvmppc_get_pc(vcpu);
313 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
314 	u32 r;
315 
316 	/* Load the instruction manually if it failed to do so in the
317 	 * exit path */
318 	if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
319 		kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
320 
321 	r = svcpu->last_inst;
322 	svcpu_put(svcpu);
323 	return r;
324 }
325 
326 static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
327 {
328 	struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
329 	ulong r;
330 	r = svcpu->fault_dar;
331 	svcpu_put(svcpu);
332 	return r;
333 }
334 
335 static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
336 {
337 	ulong crit_raw = vcpu->arch.shared->critical;
338 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
339 	bool crit;
340 
341 	/* Truncate crit indicators in 32 bit mode */
342 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
343 		crit_raw &= 0xffffffff;
344 		crit_r1 &= 0xffffffff;
345 	}
346 
347 	/* Critical section when crit == r1 */
348 	crit = (crit_raw == crit_r1);
349 	/* ... and we're in supervisor mode */
350 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
351 
352 	return crit;
353 }
354 #else /* CONFIG_KVM_BOOK3S_PR */
355 
356 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
357 {
358 	return 0;
359 }
360 
361 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
362 			unsigned long pending_now, unsigned long old_pending)
363 {
364 }
365 
366 static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
367 {
368 	vcpu->arch.gpr[num] = val;
369 }
370 
371 static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
372 {
373 	return vcpu->arch.gpr[num];
374 }
375 
376 static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
377 {
378 	vcpu->arch.cr = val;
379 }
380 
381 static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
382 {
383 	return vcpu->arch.cr;
384 }
385 
386 static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
387 {
388 	vcpu->arch.xer = val;
389 }
390 
391 static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
392 {
393 	return vcpu->arch.xer;
394 }
395 
396 static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
397 {
398 	vcpu->arch.ctr = val;
399 }
400 
401 static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
402 {
403 	return vcpu->arch.ctr;
404 }
405 
406 static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
407 {
408 	vcpu->arch.lr = val;
409 }
410 
411 static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
412 {
413 	return vcpu->arch.lr;
414 }
415 
416 static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
417 {
418 	vcpu->arch.pc = val;
419 }
420 
421 static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
422 {
423 	return vcpu->arch.pc;
424 }
425 
426 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
427 {
428 	ulong pc = kvmppc_get_pc(vcpu);
429 
430 	/* Load the instruction manually if it failed to do so in the
431 	 * exit path */
432 	if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
433 		kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
434 
435 	return vcpu->arch.last_inst;
436 }
437 
438 static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
439 {
440 	return vcpu->arch.fault_dar;
441 }
442 
443 static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
444 {
445 	return false;
446 }
447 #endif
448 
449 /* Magic register values loaded into r3 and r4 before the 'sc' assembly
450  * instruction for the OSI hypercalls */
451 #define OSI_SC_MAGIC_R3			0x113724FA
452 #define OSI_SC_MAGIC_R4			0x77810F9B
453 
454 #define INS_DCBZ			0x7c0007ec
455 
456 /* LPIDs we support with this build -- runtime limit may be lower */
457 #define KVMPPC_NR_LPIDS			(LPID_RSVD + 1)
458 
459 #endif /* __ASM_KVM_BOOK3S_H__ */
460