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.
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.
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.
15 * Copyright SUSE Linux Products GmbH 2009
17 * Authors: Alexander Graf <agraf@suse.de>
20 #ifndef __ASM_KVM_BOOK3S_H__
21 #define __ASM_KVM_BOOK3S_H__
23 #include <linux/types.h>
24 #include <linux/kvm_host.h>
25 #include <asm/kvm_book3s_asm.h>
38 struct kvmppc_sid_map {
45 #define SID_MAP_BITS 9
46 #define SID_MAP_NUM (1 << SID_MAP_BITS)
47 #define SID_MAP_MASK (SID_MAP_NUM - 1)
49 #ifdef CONFIG_PPC_BOOK3S_64
50 #define SID_CONTEXTS 1
52 #define SID_CONTEXTS 128
53 #define VSID_POOL_SIZE (SID_CONTEXTS * 16)
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;
65 struct kvmppc_pte pte;
68 struct kvmppc_vcpu_book3s {
70 struct kvmppc_book3s_shadow_vcpu *shadow_vcpu;
71 struct kvmppc_sid_map sid_map[SID_MAP_NUM];
77 struct kvmppc_bat ibat[8];
78 struct kvmppc_bat dbat[8];
85 #ifdef CONFIG_PPC_BOOK3S_32
86 u32 vsid_pool[VSID_POOL_SIZE];
91 int context_id[SID_CONTEXTS];
93 bool hior_explicit; /* HIOR is set by ioctl, not PVR */
95 struct hlist_head hpte_hash_pte[HPTEG_HASH_NUM_PTE];
96 struct hlist_head hpte_hash_pte_long[HPTEG_HASH_NUM_PTE_LONG];
97 struct hlist_head hpte_hash_vpte[HPTEG_HASH_NUM_VPTE];
98 struct hlist_head hpte_hash_vpte_long[HPTEG_HASH_NUM_VPTE_LONG];
103 #define CONTEXT_HOST 0
104 #define CONTEXT_GUEST 1
105 #define CONTEXT_GUEST_END 2
107 #define VSID_REAL 0x1fffffffffc00000ULL
108 #define VSID_BAT 0x1fffffffffb00000ULL
109 #define VSID_REAL_DR 0x2000000000000000ULL
110 #define VSID_REAL_IR 0x4000000000000000ULL
111 #define VSID_PR 0x8000000000000000ULL
113 extern void kvmppc_mmu_pte_flush(struct kvm_vcpu *vcpu, ulong ea, ulong ea_mask);
114 extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu *vcpu, u64 vp, u64 vp_mask);
115 extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end);
116 extern void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 new_msr);
117 extern void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr);
118 extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu *vcpu);
119 extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu *vcpu);
120 extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu);
121 extern int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte);
122 extern int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr);
123 extern void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu);
124 extern int kvmppc_book3s_hv_page_fault(struct kvm_run *run,
125 struct kvm_vcpu *vcpu, unsigned long addr,
126 unsigned long status);
127 extern long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr,
128 unsigned long slb_v, unsigned long valid);
130 extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
131 extern struct hpte_cache *kvmppc_mmu_hpte_cache_next(struct kvm_vcpu *vcpu);
132 extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu *vcpu);
133 extern int kvmppc_mmu_hpte_init(struct kvm_vcpu *vcpu);
134 extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte);
135 extern int kvmppc_mmu_hpte_sysinit(void);
136 extern void kvmppc_mmu_hpte_sysexit(void);
137 extern int kvmppc_mmu_hv_init(void);
139 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
140 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, bool data);
141 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec);
142 extern void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags);
143 extern void kvmppc_set_bat(struct kvm_vcpu *vcpu, struct kvmppc_bat *bat,
144 bool upper, u32 val);
145 extern void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
146 extern int kvmppc_emulate_paired_single(struct kvm_run *run, struct kvm_vcpu *vcpu);
147 extern pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn);
148 extern void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
149 unsigned long *rmap, long pte_index, int realmode);
150 extern void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
151 unsigned long pte_index);
152 void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep,
153 unsigned long pte_index);
154 extern void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long addr,
155 unsigned long *nb_ret);
156 extern void kvmppc_unpin_guest_page(struct kvm *kvm, void *addr);
157 extern long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
158 long pte_index, unsigned long pteh, unsigned long ptel);
159 extern long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
160 long pte_index, unsigned long pteh, unsigned long ptel);
161 extern long kvmppc_hv_get_dirty_log(struct kvm *kvm,
162 struct kvm_memory_slot *memslot);
164 extern void kvmppc_entry_trampoline(void);
165 extern void kvmppc_hv_entry_trampoline(void);
166 extern void kvmppc_load_up_fpu(void);
167 extern void kvmppc_load_up_altivec(void);
168 extern void kvmppc_load_up_vsx(void);
169 extern u32 kvmppc_alignment_dsisr(struct kvm_vcpu *vcpu, unsigned int inst);
170 extern ulong kvmppc_alignment_dar(struct kvm_vcpu *vcpu, unsigned int inst);
171 extern int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd);
173 static inline struct kvmppc_vcpu_book3s *to_book3s(struct kvm_vcpu *vcpu)
175 return container_of(vcpu, struct kvmppc_vcpu_book3s, vcpu);
178 extern void kvm_return_point(void);
180 /* Also add subarch specific defines */
182 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
183 #include <asm/kvm_book3s_32.h>
185 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
186 #include <asm/kvm_book3s_64.h>
189 #ifdef CONFIG_KVM_BOOK3S_PR
191 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
193 return to_book3s(vcpu)->hior;
196 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
197 unsigned long pending_now, unsigned long old_pending)
200 vcpu->arch.shared->int_pending = 1;
201 else if (old_pending)
202 vcpu->arch.shared->int_pending = 0;
205 static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
208 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
209 svcpu->gpr[num] = val;
211 to_book3s(vcpu)->shadow_vcpu->gpr[num] = val;
213 vcpu->arch.gpr[num] = val;
216 static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
219 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
220 ulong r = svcpu->gpr[num];
224 return vcpu->arch.gpr[num];
227 static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
229 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
232 to_book3s(vcpu)->shadow_vcpu->cr = val;
235 static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
237 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
244 static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
246 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
248 to_book3s(vcpu)->shadow_vcpu->xer = val;
252 static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
254 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
261 static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
263 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
268 static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
270 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
277 static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
279 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
284 static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
286 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
293 static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
295 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
300 static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
302 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
309 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
311 ulong pc = kvmppc_get_pc(vcpu);
312 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
315 /* Load the instruction manually if it failed to do so in the
317 if (svcpu->last_inst == KVM_INST_FETCH_FAILED)
318 kvmppc_ld(vcpu, &pc, sizeof(u32), &svcpu->last_inst, false);
320 r = svcpu->last_inst;
325 static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
327 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
329 r = svcpu->fault_dar;
334 static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
336 ulong crit_raw = vcpu->arch.shared->critical;
337 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
340 /* Truncate crit indicators in 32 bit mode */
341 if (!(vcpu->arch.shared->msr & MSR_SF)) {
342 crit_raw &= 0xffffffff;
343 crit_r1 &= 0xffffffff;
346 /* Critical section when crit == r1 */
347 crit = (crit_raw == crit_r1);
348 /* ... and we're in supervisor mode */
349 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
353 #else /* CONFIG_KVM_BOOK3S_PR */
355 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
360 static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
361 unsigned long pending_now, unsigned long old_pending)
365 static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)
367 vcpu->arch.gpr[num] = val;
370 static inline ulong kvmppc_get_gpr(struct kvm_vcpu *vcpu, int num)
372 return vcpu->arch.gpr[num];
375 static inline void kvmppc_set_cr(struct kvm_vcpu *vcpu, u32 val)
380 static inline u32 kvmppc_get_cr(struct kvm_vcpu *vcpu)
382 return vcpu->arch.cr;
385 static inline void kvmppc_set_xer(struct kvm_vcpu *vcpu, u32 val)
387 vcpu->arch.xer = val;
390 static inline u32 kvmppc_get_xer(struct kvm_vcpu *vcpu)
392 return vcpu->arch.xer;
395 static inline void kvmppc_set_ctr(struct kvm_vcpu *vcpu, ulong val)
397 vcpu->arch.ctr = val;
400 static inline ulong kvmppc_get_ctr(struct kvm_vcpu *vcpu)
402 return vcpu->arch.ctr;
405 static inline void kvmppc_set_lr(struct kvm_vcpu *vcpu, ulong val)
410 static inline ulong kvmppc_get_lr(struct kvm_vcpu *vcpu)
412 return vcpu->arch.lr;
415 static inline void kvmppc_set_pc(struct kvm_vcpu *vcpu, ulong val)
420 static inline ulong kvmppc_get_pc(struct kvm_vcpu *vcpu)
422 return vcpu->arch.pc;
425 static inline u32 kvmppc_get_last_inst(struct kvm_vcpu *vcpu)
427 ulong pc = kvmppc_get_pc(vcpu);
429 /* Load the instruction manually if it failed to do so in the
431 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
432 kvmppc_ld(vcpu, &pc, sizeof(u32), &vcpu->arch.last_inst, false);
434 return vcpu->arch.last_inst;
437 static inline ulong kvmppc_get_fault_dar(struct kvm_vcpu *vcpu)
439 return vcpu->arch.fault_dar;
442 static inline bool kvmppc_critical_section(struct kvm_vcpu *vcpu)
448 /* Magic register values loaded into r3 and r4 before the 'sc' assembly
449 * instruction for the OSI hypercalls */
450 #define OSI_SC_MAGIC_R3 0x113724FA
451 #define OSI_SC_MAGIC_R4 0x77810F9B
453 #define INS_DCBZ 0x7c0007ec
455 #endif /* __ASM_KVM_BOOK3S_H__ */