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];
86 #ifdef CONFIG_PPC_BOOK3S_32
87 u32 vsid_pool
[VSID_POOL_SIZE
];
94 int context_id
[SID_CONTEXTS
];
96 bool hior_explicit
; /* HIOR is set by ioctl, not PVR */
98 struct hlist_head hpte_hash_pte
[HPTEG_HASH_NUM_PTE
];
99 struct hlist_head hpte_hash_pte_long
[HPTEG_HASH_NUM_PTE_LONG
];
100 struct hlist_head hpte_hash_vpte
[HPTEG_HASH_NUM_VPTE
];
101 struct hlist_head hpte_hash_vpte_long
[HPTEG_HASH_NUM_VPTE_LONG
];
102 int hpte_cache_count
;
106 #define CONTEXT_HOST 0
107 #define CONTEXT_GUEST 1
108 #define CONTEXT_GUEST_END 2
110 #define VSID_REAL 0x0fffffffffc00000ULL
111 #define VSID_BAT 0x0fffffffffb00000ULL
112 #define VSID_1T 0x1000000000000000ULL
113 #define VSID_REAL_DR 0x2000000000000000ULL
114 #define VSID_REAL_IR 0x4000000000000000ULL
115 #define VSID_PR 0x8000000000000000ULL
117 extern void kvmppc_mmu_pte_flush(struct kvm_vcpu
*vcpu
, ulong ea
, ulong ea_mask
);
118 extern void kvmppc_mmu_pte_vflush(struct kvm_vcpu
*vcpu
, u64 vp
, u64 vp_mask
);
119 extern void kvmppc_mmu_pte_pflush(struct kvm_vcpu
*vcpu
, ulong pa_start
, ulong pa_end
);
120 extern void kvmppc_set_msr(struct kvm_vcpu
*vcpu
, u64 new_msr
);
121 extern void kvmppc_set_pvr(struct kvm_vcpu
*vcpu
, u32 pvr
);
122 extern void kvmppc_mmu_book3s_64_init(struct kvm_vcpu
*vcpu
);
123 extern void kvmppc_mmu_book3s_32_init(struct kvm_vcpu
*vcpu
);
124 extern void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu
*vcpu
);
125 extern int kvmppc_mmu_map_page(struct kvm_vcpu
*vcpu
, struct kvmppc_pte
*pte
);
126 extern int kvmppc_mmu_map_segment(struct kvm_vcpu
*vcpu
, ulong eaddr
);
127 extern void kvmppc_mmu_flush_segment(struct kvm_vcpu
*vcpu
, ulong eaddr
, ulong seg_size
);
128 extern void kvmppc_mmu_flush_segments(struct kvm_vcpu
*vcpu
);
129 extern int kvmppc_book3s_hv_page_fault(struct kvm_run
*run
,
130 struct kvm_vcpu
*vcpu
, unsigned long addr
,
131 unsigned long status
);
132 extern long kvmppc_hv_find_lock_hpte(struct kvm
*kvm
, gva_t eaddr
,
133 unsigned long slb_v
, unsigned long valid
);
135 extern void kvmppc_mmu_hpte_cache_map(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
);
136 extern struct hpte_cache
*kvmppc_mmu_hpte_cache_next(struct kvm_vcpu
*vcpu
);
137 extern void kvmppc_mmu_hpte_destroy(struct kvm_vcpu
*vcpu
);
138 extern int kvmppc_mmu_hpte_init(struct kvm_vcpu
*vcpu
);
139 extern void kvmppc_mmu_invalidate_pte(struct kvm_vcpu
*vcpu
, struct hpte_cache
*pte
);
140 extern int kvmppc_mmu_hpte_sysinit(void);
141 extern void kvmppc_mmu_hpte_sysexit(void);
142 extern int kvmppc_mmu_hv_init(void);
144 extern int kvmppc_ld(struct kvm_vcpu
*vcpu
, ulong
*eaddr
, int size
, void *ptr
, bool data
);
145 extern int kvmppc_st(struct kvm_vcpu
*vcpu
, ulong
*eaddr
, int size
, void *ptr
, bool data
);
146 extern void kvmppc_book3s_queue_irqprio(struct kvm_vcpu
*vcpu
, unsigned int vec
);
147 extern void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu
*vcpu
,
149 extern void kvmppc_inject_interrupt(struct kvm_vcpu
*vcpu
, int vec
, u64 flags
);
150 extern void kvmppc_set_bat(struct kvm_vcpu
*vcpu
, struct kvmppc_bat
*bat
,
151 bool upper
, u32 val
);
152 extern void kvmppc_giveup_ext(struct kvm_vcpu
*vcpu
, ulong msr
);
153 extern int kvmppc_emulate_paired_single(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
);
154 extern pfn_t
kvmppc_gfn_to_pfn(struct kvm_vcpu
*vcpu
, gfn_t gfn
);
155 extern void kvmppc_add_revmap_chain(struct kvm
*kvm
, struct revmap_entry
*rev
,
156 unsigned long *rmap
, long pte_index
, int realmode
);
157 extern void kvmppc_invalidate_hpte(struct kvm
*kvm
, unsigned long *hptep
,
158 unsigned long pte_index
);
159 void kvmppc_clear_ref_hpte(struct kvm
*kvm
, unsigned long *hptep
,
160 unsigned long pte_index
);
161 extern void *kvmppc_pin_guest_page(struct kvm
*kvm
, unsigned long addr
,
162 unsigned long *nb_ret
);
163 extern void kvmppc_unpin_guest_page(struct kvm
*kvm
, void *addr
,
164 unsigned long gpa
, bool dirty
);
165 extern long kvmppc_virtmode_h_enter(struct kvm_vcpu
*vcpu
, unsigned long flags
,
166 long pte_index
, unsigned long pteh
, unsigned long ptel
);
167 extern long kvmppc_do_h_enter(struct kvm
*kvm
, unsigned long flags
,
168 long pte_index
, unsigned long pteh
, unsigned long ptel
,
169 pgd_t
*pgdir
, bool realmode
, unsigned long *idx_ret
);
170 extern long kvmppc_do_h_remove(struct kvm
*kvm
, unsigned long flags
,
171 unsigned long pte_index
, unsigned long avpn
,
172 unsigned long *hpret
);
173 extern long kvmppc_hv_get_dirty_log(struct kvm
*kvm
,
174 struct kvm_memory_slot
*memslot
, unsigned long *map
);
176 extern void kvmppc_entry_trampoline(void);
177 extern void kvmppc_hv_entry_trampoline(void);
178 extern void kvmppc_load_up_fpu(void);
179 extern void kvmppc_load_up_altivec(void);
180 extern void kvmppc_load_up_vsx(void);
181 extern u32
kvmppc_alignment_dsisr(struct kvm_vcpu
*vcpu
, unsigned int inst
);
182 extern ulong
kvmppc_alignment_dar(struct kvm_vcpu
*vcpu
, unsigned int inst
);
183 extern int kvmppc_h_pr(struct kvm_vcpu
*vcpu
, unsigned long cmd
);
185 static inline struct kvmppc_vcpu_book3s
*to_book3s(struct kvm_vcpu
*vcpu
)
187 return container_of(vcpu
, struct kvmppc_vcpu_book3s
, vcpu
);
190 extern void kvm_return_point(void);
192 /* Also add subarch specific defines */
194 #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
195 #include <asm/kvm_book3s_32.h>
197 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
198 #include <asm/kvm_book3s_64.h>
201 #ifdef CONFIG_KVM_BOOK3S_PR
203 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu
*vcpu
)
205 return to_book3s(vcpu
)->hior
;
208 static inline void kvmppc_update_int_pending(struct kvm_vcpu
*vcpu
,
209 unsigned long pending_now
, unsigned long old_pending
)
212 vcpu
->arch
.shared
->int_pending
= 1;
213 else if (old_pending
)
214 vcpu
->arch
.shared
->int_pending
= 0;
217 static inline void kvmppc_set_gpr(struct kvm_vcpu
*vcpu
, int num
, ulong val
)
220 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
221 svcpu
->gpr
[num
] = val
;
223 to_book3s(vcpu
)->shadow_vcpu
->gpr
[num
] = val
;
225 vcpu
->arch
.gpr
[num
] = val
;
228 static inline ulong
kvmppc_get_gpr(struct kvm_vcpu
*vcpu
, int num
)
231 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
232 ulong r
= svcpu
->gpr
[num
];
236 return vcpu
->arch
.gpr
[num
];
239 static inline void kvmppc_set_cr(struct kvm_vcpu
*vcpu
, u32 val
)
241 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
244 to_book3s(vcpu
)->shadow_vcpu
->cr
= val
;
247 static inline u32
kvmppc_get_cr(struct kvm_vcpu
*vcpu
)
249 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
256 static inline void kvmppc_set_xer(struct kvm_vcpu
*vcpu
, u32 val
)
258 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
260 to_book3s(vcpu
)->shadow_vcpu
->xer
= val
;
264 static inline u32
kvmppc_get_xer(struct kvm_vcpu
*vcpu
)
266 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
273 static inline void kvmppc_set_ctr(struct kvm_vcpu
*vcpu
, ulong val
)
275 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
280 static inline ulong
kvmppc_get_ctr(struct kvm_vcpu
*vcpu
)
282 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
289 static inline void kvmppc_set_lr(struct kvm_vcpu
*vcpu
, ulong val
)
291 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
296 static inline ulong
kvmppc_get_lr(struct kvm_vcpu
*vcpu
)
298 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
305 static inline void kvmppc_set_pc(struct kvm_vcpu
*vcpu
, ulong val
)
307 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
312 static inline ulong
kvmppc_get_pc(struct kvm_vcpu
*vcpu
)
314 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
321 static inline u32
kvmppc_get_last_inst(struct kvm_vcpu
*vcpu
)
323 ulong pc
= kvmppc_get_pc(vcpu
);
324 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
327 /* Load the instruction manually if it failed to do so in the
329 if (svcpu
->last_inst
== KVM_INST_FETCH_FAILED
)
330 kvmppc_ld(vcpu
, &pc
, sizeof(u32
), &svcpu
->last_inst
, false);
332 r
= svcpu
->last_inst
;
338 * Like kvmppc_get_last_inst(), but for fetching a sc instruction.
339 * Because the sc instruction sets SRR0 to point to the following
340 * instruction, we have to fetch from pc - 4.
342 static inline u32
kvmppc_get_last_sc(struct kvm_vcpu
*vcpu
)
344 ulong pc
= kvmppc_get_pc(vcpu
) - 4;
345 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
348 /* Load the instruction manually if it failed to do so in the
350 if (svcpu
->last_inst
== KVM_INST_FETCH_FAILED
)
351 kvmppc_ld(vcpu
, &pc
, sizeof(u32
), &svcpu
->last_inst
, false);
353 r
= svcpu
->last_inst
;
358 static inline ulong
kvmppc_get_fault_dar(struct kvm_vcpu
*vcpu
)
360 struct kvmppc_book3s_shadow_vcpu
*svcpu
= svcpu_get(vcpu
);
362 r
= svcpu
->fault_dar
;
367 static inline bool kvmppc_critical_section(struct kvm_vcpu
*vcpu
)
369 ulong crit_raw
= vcpu
->arch
.shared
->critical
;
370 ulong crit_r1
= kvmppc_get_gpr(vcpu
, 1);
373 /* Truncate crit indicators in 32 bit mode */
374 if (!(vcpu
->arch
.shared
->msr
& MSR_SF
)) {
375 crit_raw
&= 0xffffffff;
376 crit_r1
&= 0xffffffff;
379 /* Critical section when crit == r1 */
380 crit
= (crit_raw
== crit_r1
);
381 /* ... and we're in supervisor mode */
382 crit
= crit
&& !(vcpu
->arch
.shared
->msr
& MSR_PR
);
386 #else /* CONFIG_KVM_BOOK3S_PR */
388 static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu
*vcpu
)
393 static inline void kvmppc_update_int_pending(struct kvm_vcpu
*vcpu
,
394 unsigned long pending_now
, unsigned long old_pending
)
398 static inline void kvmppc_set_gpr(struct kvm_vcpu
*vcpu
, int num
, ulong val
)
400 vcpu
->arch
.gpr
[num
] = val
;
403 static inline ulong
kvmppc_get_gpr(struct kvm_vcpu
*vcpu
, int num
)
405 return vcpu
->arch
.gpr
[num
];
408 static inline void kvmppc_set_cr(struct kvm_vcpu
*vcpu
, u32 val
)
413 static inline u32
kvmppc_get_cr(struct kvm_vcpu
*vcpu
)
415 return vcpu
->arch
.cr
;
418 static inline void kvmppc_set_xer(struct kvm_vcpu
*vcpu
, u32 val
)
420 vcpu
->arch
.xer
= val
;
423 static inline u32
kvmppc_get_xer(struct kvm_vcpu
*vcpu
)
425 return vcpu
->arch
.xer
;
428 static inline void kvmppc_set_ctr(struct kvm_vcpu
*vcpu
, ulong val
)
430 vcpu
->arch
.ctr
= val
;
433 static inline ulong
kvmppc_get_ctr(struct kvm_vcpu
*vcpu
)
435 return vcpu
->arch
.ctr
;
438 static inline void kvmppc_set_lr(struct kvm_vcpu
*vcpu
, ulong val
)
443 static inline ulong
kvmppc_get_lr(struct kvm_vcpu
*vcpu
)
445 return vcpu
->arch
.lr
;
448 static inline void kvmppc_set_pc(struct kvm_vcpu
*vcpu
, ulong val
)
453 static inline ulong
kvmppc_get_pc(struct kvm_vcpu
*vcpu
)
455 return vcpu
->arch
.pc
;
458 static inline u32
kvmppc_get_last_inst(struct kvm_vcpu
*vcpu
)
460 ulong pc
= kvmppc_get_pc(vcpu
);
462 /* Load the instruction manually if it failed to do so in the
464 if (vcpu
->arch
.last_inst
== KVM_INST_FETCH_FAILED
)
465 kvmppc_ld(vcpu
, &pc
, sizeof(u32
), &vcpu
->arch
.last_inst
, false);
467 return vcpu
->arch
.last_inst
;
471 * Like kvmppc_get_last_inst(), but for fetching a sc instruction.
472 * Because the sc instruction sets SRR0 to point to the following
473 * instruction, we have to fetch from pc - 4.
475 static inline u32
kvmppc_get_last_sc(struct kvm_vcpu
*vcpu
)
477 ulong pc
= kvmppc_get_pc(vcpu
) - 4;
479 /* Load the instruction manually if it failed to do so in the
481 if (vcpu
->arch
.last_inst
== KVM_INST_FETCH_FAILED
)
482 kvmppc_ld(vcpu
, &pc
, sizeof(u32
), &vcpu
->arch
.last_inst
, false);
484 return vcpu
->arch
.last_inst
;
487 static inline ulong
kvmppc_get_fault_dar(struct kvm_vcpu
*vcpu
)
489 return vcpu
->arch
.fault_dar
;
492 static inline bool kvmppc_critical_section(struct kvm_vcpu
*vcpu
)
498 /* Magic register values loaded into r3 and r4 before the 'sc' assembly
499 * instruction for the OSI hypercalls */
500 #define OSI_SC_MAGIC_R3 0x113724FA
501 #define OSI_SC_MAGIC_R4 0x77810F9B
503 #define INS_DCBZ 0x7c0007ec
504 /* TO = 31 for unconditional trap */
505 #define INS_TW 0x7fe00008
507 /* LPIDs we support with this build -- runtime limit may be lower */
508 #define KVMPPC_NR_LPIDS (LPID_RSVD + 1)
510 #endif /* __ASM_KVM_BOOK3S_H__ */