Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1 | #ifndef ASM_KVM_CACHE_REGS_H |
| 2 | #define ASM_KVM_CACHE_REGS_H |
| 3 | |
Avi Kivity | 8ae0991 | 2010-01-21 15:31:51 +0200 | [diff] [blame] | 4 | #define KVM_POSSIBLE_CR0_GUEST_BITS X86_CR0_TS |
| 5 | #define KVM_POSSIBLE_CR4_GUEST_BITS \ |
| 6 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 7 | | X86_CR4_OSXMMEXCPT | X86_CR4_PGE) |
| 8 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9 | static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, |
| 10 | enum kvm_reg reg) |
| 11 | { |
| 12 | if (!test_bit(reg, (unsigned long *)&vcpu->arch.regs_avail)) |
| 13 | kvm_x86_ops->cache_reg(vcpu, reg); |
| 14 | |
| 15 | return vcpu->arch.regs[reg]; |
| 16 | } |
| 17 | |
| 18 | static inline void kvm_register_write(struct kvm_vcpu *vcpu, |
| 19 | enum kvm_reg reg, |
| 20 | unsigned long val) |
| 21 | { |
| 22 | vcpu->arch.regs[reg] = val; |
| 23 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_dirty); |
| 24 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 25 | } |
| 26 | |
| 27 | static inline unsigned long kvm_rip_read(struct kvm_vcpu *vcpu) |
| 28 | { |
| 29 | return kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 30 | } |
| 31 | |
| 32 | static inline void kvm_rip_write(struct kvm_vcpu *vcpu, unsigned long val) |
| 33 | { |
| 34 | kvm_register_write(vcpu, VCPU_REGS_RIP, val); |
| 35 | } |
| 36 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 37 | static inline u64 kvm_pdptr_read(struct kvm_vcpu *vcpu, int index) |
| 38 | { |
Avi Kivity | 08acfa1 | 2010-05-04 13:00:55 +0300 | [diff] [blame] | 39 | might_sleep(); /* on svm */ |
| 40 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 41 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 42 | (unsigned long *)&vcpu->arch.regs_avail)) |
| 43 | kvm_x86_ops->cache_reg(vcpu, VCPU_EXREG_PDPTR); |
| 44 | |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 45 | return vcpu->arch.walk_mmu->pdptrs[index]; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 46 | } |
| 47 | |
Joerg Roedel | d41d189 | 2010-09-10 17:30:58 +0200 | [diff] [blame] | 48 | static inline u64 kvm_pdptr_read_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, int index) |
| 49 | { |
| 50 | load_pdptrs(vcpu, mmu, mmu->get_cr3(vcpu)); |
| 51 | |
| 52 | return mmu->pdptrs[index]; |
| 53 | } |
| 54 | |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 55 | static inline ulong kvm_read_cr0_bits(struct kvm_vcpu *vcpu, ulong mask) |
| 56 | { |
Avi Kivity | 8ae0991 | 2010-01-21 15:31:51 +0200 | [diff] [blame] | 57 | ulong tmask = mask & KVM_POSSIBLE_CR0_GUEST_BITS; |
| 58 | if (tmask & vcpu->arch.cr0_guest_owned_bits) |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 59 | kvm_x86_ops->decache_cr0_guest_bits(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 60 | return vcpu->arch.cr0 & mask; |
| 61 | } |
| 62 | |
| 63 | static inline ulong kvm_read_cr0(struct kvm_vcpu *vcpu) |
| 64 | { |
| 65 | return kvm_read_cr0_bits(vcpu, ~0UL); |
| 66 | } |
| 67 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 68 | static inline ulong kvm_read_cr4_bits(struct kvm_vcpu *vcpu, ulong mask) |
| 69 | { |
Avi Kivity | 8ae0991 | 2010-01-21 15:31:51 +0200 | [diff] [blame] | 70 | ulong tmask = mask & KVM_POSSIBLE_CR4_GUEST_BITS; |
| 71 | if (tmask & vcpu->arch.cr4_guest_owned_bits) |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 72 | kvm_x86_ops->decache_cr4_guest_bits(vcpu); |
| 73 | return vcpu->arch.cr4 & mask; |
| 74 | } |
| 75 | |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 76 | static inline ulong kvm_read_cr3(struct kvm_vcpu *vcpu) |
| 77 | { |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 78 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 79 | kvm_x86_ops->decache_cr3(vcpu); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 80 | return vcpu->arch.cr3; |
| 81 | } |
| 82 | |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 83 | static inline ulong kvm_read_cr4(struct kvm_vcpu *vcpu) |
| 84 | { |
| 85 | return kvm_read_cr4_bits(vcpu, ~0UL); |
| 86 | } |
| 87 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 88 | static inline u64 kvm_read_edx_eax(struct kvm_vcpu *vcpu) |
| 89 | { |
| 90 | return (kvm_register_read(vcpu, VCPU_REGS_RAX) & -1u) |
| 91 | | ((u64)(kvm_register_read(vcpu, VCPU_REGS_RDX) & -1u) << 32); |
| 92 | } |
| 93 | |
Joerg Roedel | ec9e60b | 2010-11-29 17:51:47 +0100 | [diff] [blame] | 94 | static inline void enter_guest_mode(struct kvm_vcpu *vcpu) |
| 95 | { |
| 96 | vcpu->arch.hflags |= HF_GUEST_MASK; |
| 97 | } |
| 98 | |
| 99 | static inline void leave_guest_mode(struct kvm_vcpu *vcpu) |
| 100 | { |
| 101 | vcpu->arch.hflags &= ~HF_GUEST_MASK; |
| 102 | } |
| 103 | |
| 104 | static inline bool is_guest_mode(struct kvm_vcpu *vcpu) |
| 105 | { |
| 106 | return vcpu->arch.hflags & HF_GUEST_MASK; |
| 107 | } |
| 108 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 109 | #endif |