KVM: PPC: Separate loadstore emulation from priv emulation
[deliverable/linux.git] / arch / powerpc / include / asm / kvm_ppc.h
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 IBM Corp. 2008
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 */
19
20 #ifndef __POWERPC_KVM_PPC_H__
21 #define __POWERPC_KVM_PPC_H__
22
23 /* This file exists just so we can dereference kvm_vcpu, avoiding nested header
24 * dependencies. */
25
26 #include <linux/mutex.h>
27 #include <linux/timer.h>
28 #include <linux/types.h>
29 #include <linux/kvm_types.h>
30 #include <linux/kvm_host.h>
31 #include <linux/bug.h>
32 #ifdef CONFIG_PPC_BOOK3S
33 #include <asm/kvm_book3s.h>
34 #else
35 #include <asm/kvm_booke.h>
36 #endif
37 #ifdef CONFIG_KVM_BOOK3S_64_HANDLER
38 #include <asm/paca.h>
39 #endif
40
41 enum emulation_result {
42 EMULATE_DONE, /* no further processing */
43 EMULATE_DO_MMIO, /* kvm_run filled with MMIO request */
44 EMULATE_DO_DCR, /* kvm_run filled with DCR request */
45 EMULATE_FAIL, /* can't emulate this instruction */
46 EMULATE_AGAIN, /* something went wrong. go again */
47 EMULATE_EXIT_USER, /* emulation requires exit to user-space */
48 };
49
50 enum instruction_type {
51 INST_GENERIC,
52 INST_SC, /* system call */
53 };
54
55 enum xlate_instdata {
56 XLATE_INST, /* translate instruction address */
57 XLATE_DATA /* translate data address */
58 };
59
60 enum xlate_readwrite {
61 XLATE_READ, /* check for read permissions */
62 XLATE_WRITE /* check for write permissions */
63 };
64
65 extern int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
66 extern int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
67 extern void kvmppc_handler_highmem(void);
68
69 extern void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu);
70 extern int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
71 unsigned int rt, unsigned int bytes,
72 int is_default_endian);
73 extern int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
74 unsigned int rt, unsigned int bytes,
75 int is_default_endian);
76 extern int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
77 u64 val, unsigned int bytes,
78 int is_default_endian);
79
80 extern int kvmppc_load_last_inst(struct kvm_vcpu *vcpu,
81 enum instruction_type type, u32 *inst);
82
83 extern int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
84 bool data);
85 extern int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
86 bool data);
87 extern int kvmppc_emulate_instruction(struct kvm_run *run,
88 struct kvm_vcpu *vcpu);
89 extern int kvmppc_emulate_loadstore(struct kvm_vcpu *vcpu);
90 extern int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu);
91 extern void kvmppc_emulate_dec(struct kvm_vcpu *vcpu);
92 extern u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb);
93 extern void kvmppc_decrementer_func(unsigned long data);
94 extern int kvmppc_sanity_check(struct kvm_vcpu *vcpu);
95 extern int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu);
96 extern void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu);
97
98 /* Core-specific hooks */
99
100 extern void kvmppc_mmu_map(struct kvm_vcpu *vcpu, u64 gvaddr, gpa_t gpaddr,
101 unsigned int gtlb_idx);
102 extern void kvmppc_mmu_priv_switch(struct kvm_vcpu *vcpu, int usermode);
103 extern void kvmppc_mmu_switch_pid(struct kvm_vcpu *vcpu, u32 pid);
104 extern void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu);
105 extern int kvmppc_mmu_init(struct kvm_vcpu *vcpu);
106 extern int kvmppc_mmu_dtlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
107 extern int kvmppc_mmu_itlb_index(struct kvm_vcpu *vcpu, gva_t eaddr);
108 extern gpa_t kvmppc_mmu_xlate(struct kvm_vcpu *vcpu, unsigned int gtlb_index,
109 gva_t eaddr);
110 extern void kvmppc_mmu_dtlb_miss(struct kvm_vcpu *vcpu);
111 extern void kvmppc_mmu_itlb_miss(struct kvm_vcpu *vcpu);
112 extern int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr,
113 enum xlate_instdata xlid, enum xlate_readwrite xlrw,
114 struct kvmppc_pte *pte);
115
116 extern struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm,
117 unsigned int id);
118 extern void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu);
119 extern int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu);
120 extern int kvmppc_core_check_processor_compat(void);
121 extern int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
122 struct kvm_translation *tr);
123
124 extern void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
125 extern void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu);
126
127 extern int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu);
128 extern int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu);
129 extern void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags);
130 extern void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu);
131 extern void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu);
132 extern void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
133 struct kvm_interrupt *irq);
134 extern void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu);
135 extern void kvmppc_core_flush_tlb(struct kvm_vcpu *vcpu);
136 extern int kvmppc_core_check_requests(struct kvm_vcpu *vcpu);
137
138 extern int kvmppc_booke_init(void);
139 extern void kvmppc_booke_exit(void);
140
141 extern void kvmppc_core_destroy_mmu(struct kvm_vcpu *vcpu);
142 extern int kvmppc_kvm_pv(struct kvm_vcpu *vcpu);
143 extern void kvmppc_map_magic(struct kvm_vcpu *vcpu);
144
145 extern long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp);
146 extern long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp);
147 extern void kvmppc_free_hpt(struct kvm *kvm);
148 extern long kvmppc_prepare_vrma(struct kvm *kvm,
149 struct kvm_userspace_memory_region *mem);
150 extern void kvmppc_map_vrma(struct kvm_vcpu *vcpu,
151 struct kvm_memory_slot *memslot, unsigned long porder);
152 extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
153
154 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
155 struct kvm_create_spapr_tce *args);
156 extern long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
157 unsigned long ioba, unsigned long tce);
158 extern long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
159 unsigned long ioba);
160 extern struct kvm_rma_info *kvm_alloc_rma(void);
161 extern void kvm_release_rma(struct kvm_rma_info *ri);
162 extern struct page *kvm_alloc_hpt(unsigned long nr_pages);
163 extern void kvm_release_hpt(struct page *page, unsigned long nr_pages);
164 extern int kvmppc_core_init_vm(struct kvm *kvm);
165 extern void kvmppc_core_destroy_vm(struct kvm *kvm);
166 extern void kvmppc_core_free_memslot(struct kvm *kvm,
167 struct kvm_memory_slot *free,
168 struct kvm_memory_slot *dont);
169 extern int kvmppc_core_create_memslot(struct kvm *kvm,
170 struct kvm_memory_slot *slot,
171 unsigned long npages);
172 extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
173 struct kvm_memory_slot *memslot,
174 struct kvm_userspace_memory_region *mem);
175 extern void kvmppc_core_commit_memory_region(struct kvm *kvm,
176 struct kvm_userspace_memory_region *mem,
177 const struct kvm_memory_slot *old);
178 extern int kvm_vm_ioctl_get_smmu_info(struct kvm *kvm,
179 struct kvm_ppc_smmu_info *info);
180 extern void kvmppc_core_flush_memslot(struct kvm *kvm,
181 struct kvm_memory_slot *memslot);
182
183 extern int kvmppc_bookehv_init(void);
184 extern void kvmppc_bookehv_exit(void);
185
186 extern int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu);
187
188 extern int kvm_vm_ioctl_get_htab_fd(struct kvm *kvm, struct kvm_get_htab_fd *);
189
190 int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
191
192 extern int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp);
193 extern int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu);
194 extern void kvmppc_rtas_tokens_free(struct kvm *kvm);
195 extern int kvmppc_xics_set_xive(struct kvm *kvm, u32 irq, u32 server,
196 u32 priority);
197 extern int kvmppc_xics_get_xive(struct kvm *kvm, u32 irq, u32 *server,
198 u32 *priority);
199 extern int kvmppc_xics_int_on(struct kvm *kvm, u32 irq);
200 extern int kvmppc_xics_int_off(struct kvm *kvm, u32 irq);
201
202 union kvmppc_one_reg {
203 u32 wval;
204 u64 dval;
205 vector128 vval;
206 u64 vsxval[2];
207 struct {
208 u64 addr;
209 u64 length;
210 } vpaval;
211 };
212
213 struct kvmppc_ops {
214 struct module *owner;
215 int (*get_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
216 int (*set_sregs)(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
217 int (*get_one_reg)(struct kvm_vcpu *vcpu, u64 id,
218 union kvmppc_one_reg *val);
219 int (*set_one_reg)(struct kvm_vcpu *vcpu, u64 id,
220 union kvmppc_one_reg *val);
221 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
222 void (*vcpu_put)(struct kvm_vcpu *vcpu);
223 void (*set_msr)(struct kvm_vcpu *vcpu, u64 msr);
224 int (*vcpu_run)(struct kvm_run *run, struct kvm_vcpu *vcpu);
225 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned int id);
226 void (*vcpu_free)(struct kvm_vcpu *vcpu);
227 int (*check_requests)(struct kvm_vcpu *vcpu);
228 int (*get_dirty_log)(struct kvm *kvm, struct kvm_dirty_log *log);
229 void (*flush_memslot)(struct kvm *kvm, struct kvm_memory_slot *memslot);
230 int (*prepare_memory_region)(struct kvm *kvm,
231 struct kvm_memory_slot *memslot,
232 struct kvm_userspace_memory_region *mem);
233 void (*commit_memory_region)(struct kvm *kvm,
234 struct kvm_userspace_memory_region *mem,
235 const struct kvm_memory_slot *old);
236 int (*unmap_hva)(struct kvm *kvm, unsigned long hva);
237 int (*unmap_hva_range)(struct kvm *kvm, unsigned long start,
238 unsigned long end);
239 int (*age_hva)(struct kvm *kvm, unsigned long hva);
240 int (*test_age_hva)(struct kvm *kvm, unsigned long hva);
241 void (*set_spte_hva)(struct kvm *kvm, unsigned long hva, pte_t pte);
242 void (*mmu_destroy)(struct kvm_vcpu *vcpu);
243 void (*free_memslot)(struct kvm_memory_slot *free,
244 struct kvm_memory_slot *dont);
245 int (*create_memslot)(struct kvm_memory_slot *slot,
246 unsigned long npages);
247 int (*init_vm)(struct kvm *kvm);
248 void (*destroy_vm)(struct kvm *kvm);
249 int (*get_smmu_info)(struct kvm *kvm, struct kvm_ppc_smmu_info *info);
250 int (*emulate_op)(struct kvm_run *run, struct kvm_vcpu *vcpu,
251 unsigned int inst, int *advance);
252 int (*emulate_mtspr)(struct kvm_vcpu *vcpu, int sprn, ulong spr_val);
253 int (*emulate_mfspr)(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val);
254 void (*fast_vcpu_kick)(struct kvm_vcpu *vcpu);
255 long (*arch_vm_ioctl)(struct file *filp, unsigned int ioctl,
256 unsigned long arg);
257 int (*hcall_implemented)(unsigned long hcall);
258 };
259
260 extern struct kvmppc_ops *kvmppc_hv_ops;
261 extern struct kvmppc_ops *kvmppc_pr_ops;
262
263 static inline int kvmppc_get_last_inst(struct kvm_vcpu *vcpu,
264 enum instruction_type type, u32 *inst)
265 {
266 int ret = EMULATE_DONE;
267 u32 fetched_inst;
268
269 /* Load the instruction manually if it failed to do so in the
270 * exit path */
271 if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED)
272 ret = kvmppc_load_last_inst(vcpu, type, &vcpu->arch.last_inst);
273
274 /* Write fetch_failed unswapped if the fetch failed */
275 if (ret == EMULATE_DONE)
276 fetched_inst = kvmppc_need_byteswap(vcpu) ?
277 swab32(vcpu->arch.last_inst) :
278 vcpu->arch.last_inst;
279 else
280 fetched_inst = vcpu->arch.last_inst;
281
282 *inst = fetched_inst;
283 return ret;
284 }
285
286 static inline bool is_kvmppc_hv_enabled(struct kvm *kvm)
287 {
288 return kvm->arch.kvm_ops == kvmppc_hv_ops;
289 }
290
291 /*
292 * Cuts out inst bits with ordering according to spec.
293 * That means the leftmost bit is zero. All given bits are included.
294 */
295 static inline u32 kvmppc_get_field(u64 inst, int msb, int lsb)
296 {
297 u32 r;
298 u32 mask;
299
300 BUG_ON(msb > lsb);
301
302 mask = (1 << (lsb - msb + 1)) - 1;
303 r = (inst >> (63 - lsb)) & mask;
304
305 return r;
306 }
307
308 /*
309 * Replaces inst bits with ordering according to spec.
310 */
311 static inline u32 kvmppc_set_field(u64 inst, int msb, int lsb, int value)
312 {
313 u32 r;
314 u32 mask;
315
316 BUG_ON(msb > lsb);
317
318 mask = ((1 << (lsb - msb + 1)) - 1) << (63 - lsb);
319 r = (inst & ~mask) | ((value << (63 - lsb)) & mask);
320
321 return r;
322 }
323
324 #define one_reg_size(id) \
325 (1ul << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
326
327 #define get_reg_val(id, reg) ({ \
328 union kvmppc_one_reg __u; \
329 switch (one_reg_size(id)) { \
330 case 4: __u.wval = (reg); break; \
331 case 8: __u.dval = (reg); break; \
332 default: BUG(); \
333 } \
334 __u; \
335 })
336
337
338 #define set_reg_val(id, val) ({ \
339 u64 __v; \
340 switch (one_reg_size(id)) { \
341 case 4: __v = (val).wval; break; \
342 case 8: __v = (val).dval; break; \
343 default: BUG(); \
344 } \
345 __v; \
346 })
347
348 int kvmppc_core_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
349 int kvmppc_core_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
350
351 int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
352 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs);
353
354 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
355 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg);
356 int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
357 int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, union kvmppc_one_reg *);
358
359 void kvmppc_set_pid(struct kvm_vcpu *vcpu, u32 pid);
360
361 struct openpic;
362
363 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
364 extern void kvm_cma_reserve(void) __init;
365 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
366 {
367 paca[cpu].kvm_hstate.xics_phys = addr;
368 }
369
370 static inline u32 kvmppc_get_xics_latch(void)
371 {
372 u32 xirr;
373
374 xirr = get_paca()->kvm_hstate.saved_xirr;
375 get_paca()->kvm_hstate.saved_xirr = 0;
376 return xirr;
377 }
378
379 static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
380 {
381 paca[cpu].kvm_hstate.host_ipi = host_ipi;
382 }
383
384 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
385 {
386 vcpu->kvm->arch.kvm_ops->fast_vcpu_kick(vcpu);
387 }
388
389 extern void kvm_hv_vm_activated(void);
390 extern void kvm_hv_vm_deactivated(void);
391 extern bool kvm_hv_mode_active(void);
392
393 #else
394 static inline void __init kvm_cma_reserve(void)
395 {}
396
397 static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
398 {}
399
400 static inline u32 kvmppc_get_xics_latch(void)
401 {
402 return 0;
403 }
404
405 static inline void kvmppc_set_host_ipi(int cpu, u8 host_ipi)
406 {}
407
408 static inline void kvmppc_fast_vcpu_kick(struct kvm_vcpu *vcpu)
409 {
410 kvm_vcpu_kick(vcpu);
411 }
412
413 static inline bool kvm_hv_mode_active(void) { return false; }
414
415 #endif
416
417 #ifdef CONFIG_KVM_XICS
418 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
419 {
420 return vcpu->arch.irq_type == KVMPPC_IRQ_XICS;
421 }
422 extern void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu);
423 extern int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu, unsigned long server);
424 extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args);
425 extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
426 extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
427 extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
428 extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
429 struct kvm_vcpu *vcpu, u32 cpu);
430 #else
431 static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
432 { return 0; }
433 static inline void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu) { }
434 static inline int kvmppc_xics_create_icp(struct kvm_vcpu *vcpu,
435 unsigned long server)
436 { return -EINVAL; }
437 static inline int kvm_vm_ioctl_xics_irq(struct kvm *kvm,
438 struct kvm_irq_level *args)
439 { return -ENOTTY; }
440 static inline int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd)
441 { return 0; }
442 #endif
443
444 static inline unsigned long kvmppc_get_epr(struct kvm_vcpu *vcpu)
445 {
446 #ifdef CONFIG_KVM_BOOKE_HV
447 return mfspr(SPRN_GEPR);
448 #elif defined(CONFIG_BOOKE)
449 return vcpu->arch.epr;
450 #else
451 return 0;
452 #endif
453 }
454
455 static inline void kvmppc_set_epr(struct kvm_vcpu *vcpu, u32 epr)
456 {
457 #ifdef CONFIG_KVM_BOOKE_HV
458 mtspr(SPRN_GEPR, epr);
459 #elif defined(CONFIG_BOOKE)
460 vcpu->arch.epr = epr;
461 #endif
462 }
463
464 #ifdef CONFIG_KVM_MPIC
465
466 void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu);
467 int kvmppc_mpic_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
468 u32 cpu);
469 void kvmppc_mpic_disconnect_vcpu(struct openpic *opp, struct kvm_vcpu *vcpu);
470
471 #else
472
473 static inline void kvmppc_mpic_set_epr(struct kvm_vcpu *vcpu)
474 {
475 }
476
477 static inline int kvmppc_mpic_connect_vcpu(struct kvm_device *dev,
478 struct kvm_vcpu *vcpu, u32 cpu)
479 {
480 return -EINVAL;
481 }
482
483 static inline void kvmppc_mpic_disconnect_vcpu(struct openpic *opp,
484 struct kvm_vcpu *vcpu)
485 {
486 }
487
488 #endif /* CONFIG_KVM_MPIC */
489
490 int kvm_vcpu_ioctl_config_tlb(struct kvm_vcpu *vcpu,
491 struct kvm_config_tlb *cfg);
492 int kvm_vcpu_ioctl_dirty_tlb(struct kvm_vcpu *vcpu,
493 struct kvm_dirty_tlb *cfg);
494
495 long kvmppc_alloc_lpid(void);
496 void kvmppc_claim_lpid(long lpid);
497 void kvmppc_free_lpid(long lpid);
498 void kvmppc_init_lpid(unsigned long nr_lpids);
499
500 static inline void kvmppc_mmu_flush_icache(pfn_t pfn)
501 {
502 struct page *page;
503 /*
504 * We can only access pages that the kernel maps
505 * as memory. Bail out for unmapped ones.
506 */
507 if (!pfn_valid(pfn))
508 return;
509
510 /* Clear i-cache for new pages */
511 page = pfn_to_page(pfn);
512 if (!test_bit(PG_arch_1, &page->flags)) {
513 flush_dcache_icache_page(page);
514 set_bit(PG_arch_1, &page->flags);
515 }
516 }
517
518 /*
519 * Shared struct helpers. The shared struct can be little or big endian,
520 * depending on the guest endianness. So expose helpers to all of them.
521 */
522 static inline bool kvmppc_shared_big_endian(struct kvm_vcpu *vcpu)
523 {
524 #if defined(CONFIG_PPC_BOOK3S_64) && defined(CONFIG_KVM_BOOK3S_PR_POSSIBLE)
525 /* Only Book3S_64 PR supports bi-endian for now */
526 return vcpu->arch.shared_big_endian;
527 #elif defined(CONFIG_PPC_BOOK3S_64) && defined(__LITTLE_ENDIAN__)
528 /* Book3s_64 HV on little endian is always little endian */
529 return false;
530 #else
531 return true;
532 #endif
533 }
534
535 #define SPRNG_WRAPPER_GET(reg, e500hv_spr) \
536 static inline ulong kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
537 { \
538 return mfspr(e500hv_spr); \
539 } \
540
541 #define SPRNG_WRAPPER_SET(reg, e500hv_spr) \
542 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, ulong val) \
543 { \
544 mtspr(e500hv_spr, val); \
545 } \
546
547 #define SHARED_WRAPPER_GET(reg, size) \
548 static inline u##size kvmppc_get_##reg(struct kvm_vcpu *vcpu) \
549 { \
550 if (kvmppc_shared_big_endian(vcpu)) \
551 return be##size##_to_cpu(vcpu->arch.shared->reg); \
552 else \
553 return le##size##_to_cpu(vcpu->arch.shared->reg); \
554 } \
555
556 #define SHARED_WRAPPER_SET(reg, size) \
557 static inline void kvmppc_set_##reg(struct kvm_vcpu *vcpu, u##size val) \
558 { \
559 if (kvmppc_shared_big_endian(vcpu)) \
560 vcpu->arch.shared->reg = cpu_to_be##size(val); \
561 else \
562 vcpu->arch.shared->reg = cpu_to_le##size(val); \
563 } \
564
565 #define SHARED_WRAPPER(reg, size) \
566 SHARED_WRAPPER_GET(reg, size) \
567 SHARED_WRAPPER_SET(reg, size) \
568
569 #define SPRNG_WRAPPER(reg, e500hv_spr) \
570 SPRNG_WRAPPER_GET(reg, e500hv_spr) \
571 SPRNG_WRAPPER_SET(reg, e500hv_spr) \
572
573 #ifdef CONFIG_KVM_BOOKE_HV
574
575 #define SHARED_SPRNG_WRAPPER(reg, size, e500hv_spr) \
576 SPRNG_WRAPPER(reg, e500hv_spr) \
577
578 #else
579
580 #define SHARED_SPRNG_WRAPPER(reg, size, e500hv_spr) \
581 SHARED_WRAPPER(reg, size) \
582
583 #endif
584
585 SHARED_WRAPPER(critical, 64)
586 SHARED_SPRNG_WRAPPER(sprg0, 64, SPRN_GSPRG0)
587 SHARED_SPRNG_WRAPPER(sprg1, 64, SPRN_GSPRG1)
588 SHARED_SPRNG_WRAPPER(sprg2, 64, SPRN_GSPRG2)
589 SHARED_SPRNG_WRAPPER(sprg3, 64, SPRN_GSPRG3)
590 SHARED_SPRNG_WRAPPER(srr0, 64, SPRN_GSRR0)
591 SHARED_SPRNG_WRAPPER(srr1, 64, SPRN_GSRR1)
592 SHARED_SPRNG_WRAPPER(dar, 64, SPRN_GDEAR)
593 SHARED_SPRNG_WRAPPER(esr, 64, SPRN_GESR)
594 SHARED_WRAPPER_GET(msr, 64)
595 static inline void kvmppc_set_msr_fast(struct kvm_vcpu *vcpu, u64 val)
596 {
597 if (kvmppc_shared_big_endian(vcpu))
598 vcpu->arch.shared->msr = cpu_to_be64(val);
599 else
600 vcpu->arch.shared->msr = cpu_to_le64(val);
601 }
602 SHARED_WRAPPER(dsisr, 32)
603 SHARED_WRAPPER(int_pending, 32)
604 SHARED_WRAPPER(sprg4, 64)
605 SHARED_WRAPPER(sprg5, 64)
606 SHARED_WRAPPER(sprg6, 64)
607 SHARED_WRAPPER(sprg7, 64)
608
609 static inline u32 kvmppc_get_sr(struct kvm_vcpu *vcpu, int nr)
610 {
611 if (kvmppc_shared_big_endian(vcpu))
612 return be32_to_cpu(vcpu->arch.shared->sr[nr]);
613 else
614 return le32_to_cpu(vcpu->arch.shared->sr[nr]);
615 }
616
617 static inline void kvmppc_set_sr(struct kvm_vcpu *vcpu, int nr, u32 val)
618 {
619 if (kvmppc_shared_big_endian(vcpu))
620 vcpu->arch.shared->sr[nr] = cpu_to_be32(val);
621 else
622 vcpu->arch.shared->sr[nr] = cpu_to_le32(val);
623 }
624
625 /*
626 * Please call after prepare_to_enter. This function puts the lazy ee and irq
627 * disabled tracking state back to normal mode, without actually enabling
628 * interrupts.
629 */
630 static inline void kvmppc_fix_ee_before_entry(void)
631 {
632 trace_hardirqs_on();
633
634 #ifdef CONFIG_PPC64
635 /*
636 * To avoid races, the caller must have gone directly from having
637 * interrupts fully-enabled to hard-disabled.
638 */
639 WARN_ON(local_paca->irq_happened != PACA_IRQ_HARD_DIS);
640
641 /* Only need to enable IRQs by hard enabling them after this */
642 local_paca->irq_happened = 0;
643 local_paca->soft_enabled = 1;
644 #endif
645 }
646
647 static inline ulong kvmppc_get_ea_indexed(struct kvm_vcpu *vcpu, int ra, int rb)
648 {
649 ulong ea;
650 ulong msr_64bit = 0;
651
652 ea = kvmppc_get_gpr(vcpu, rb);
653 if (ra)
654 ea += kvmppc_get_gpr(vcpu, ra);
655
656 #if defined(CONFIG_PPC_BOOK3E_64)
657 msr_64bit = MSR_CM;
658 #elif defined(CONFIG_PPC_BOOK3S_64)
659 msr_64bit = MSR_SF;
660 #endif
661
662 if (!(kvmppc_get_msr(vcpu) & msr_64bit))
663 ea = (uint32_t)ea;
664
665 return ea;
666 }
667
668 extern void xics_wake_cpu(int cpu);
669
670 #endif /* __POWERPC_KVM_PPC_H__ */
This page took 0.055163 seconds and 5 git commands to generate.