Skip to content

Commit aa04b4c

Browse files
paulusmackavikivity
authored andcommitted
KVM: PPC: Allocate RMAs (Real Mode Areas) at boot for use by guests
This adds infrastructure which will be needed to allow book3s_hv KVM to run on older POWER processors, including PPC970, which don't support the Virtual Real Mode Area (VRMA) facility, but only the Real Mode Offset (RMO) facility. These processors require a physically contiguous, aligned area of memory for each guest. When the guest does an access in real mode (MMU off), the address is compared against a limit value, and if it is lower, the address is ORed with an offset value (from the Real Mode Offset Register (RMOR)) and the result becomes the real address for the access. The size of the RMA has to be one of a set of supported values, which usually includes 64MB, 128MB, 256MB and some larger powers of 2. Since we are unlikely to be able to allocate 64MB or more of physically contiguous memory after the kernel has been running for a while, we allocate a pool of RMAs at boot time using the bootmem allocator. The size and number of the RMAs can be set using the kvm_rma_size=xx and kvm_rma_count=xx kernel command line options. KVM exports a new capability, KVM_CAP_PPC_RMA, to signal the availability of the pool of preallocated RMAs. The capability value is 1 if the processor can use an RMA but doesn't require one (because it supports the VRMA facility), or 2 if the processor requires an RMA for each guest. This adds a new ioctl, KVM_ALLOCATE_RMA, which allocates an RMA from the pool and returns a file descriptor which can be used to map the RMA. It also returns the size of the RMA in the argument structure. Having an RMA means we will get multiple KMV_SET_USER_MEMORY_REGION ioctl calls from userspace. To cope with this, we now preallocate the kvm->arch.ram_pginfo array when the VM is created with a size sufficient for up to 64GB of guest memory. Subsequently we will get rid of this array and use memory associated with each memslot instead. This moves most of the code that translates the user addresses into host pfns (page frame numbers) out of kvmppc_prepare_vrma up one level to kvmppc_core_prepare_memory_region. Also, instead of having to look up the VMA for each page in order to check the page size, we now check that the pages we get are compound pages of 16MB. However, if we are adding memory that is mapped to an RMA, we don't bother with calling get_user_pages_fast and instead just offset from the base pfn for the RMA. Typically the RMA gets added after vcpus are created, which makes it inconvenient to have the LPCR (logical partition control register) value in the vcpu->arch struct, since the LPCR controls whether the processor uses RMA or VRMA for the guest. This moves the LPCR value into the kvm->arch struct and arranges for the MER (mediated external request) bit, which is the only bit that varies between vcpus, to be set in assembly code when going into the guest if there is a pending external interrupt request. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent 371fefd commit aa04b4c

15 files changed

Lines changed: 505 additions & 119 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,38 @@ the entries written by kernel-handled H_PUT_TCE calls, and also lets
13981398
userspace update the TCE table directly which is useful in some
13991399
circumstances.
14001400

1401+
4.63 KVM_ALLOCATE_RMA
1402+
1403+
Capability: KVM_CAP_PPC_RMA
1404+
Architectures: powerpc
1405+
Type: vm ioctl
1406+
Parameters: struct kvm_allocate_rma (out)
1407+
Returns: file descriptor for mapping the allocated RMA
1408+
1409+
This allocates a Real Mode Area (RMA) from the pool allocated at boot
1410+
time by the kernel. An RMA is a physically-contiguous, aligned region
1411+
of memory used on older POWER processors to provide the memory which
1412+
will be accessed by real-mode (MMU off) accesses in a KVM guest.
1413+
POWER processors support a set of sizes for the RMA that usually
1414+
includes 64MB, 128MB, 256MB and some larger powers of two.
1415+
1416+
/* for KVM_ALLOCATE_RMA */
1417+
struct kvm_allocate_rma {
1418+
__u64 rma_size;
1419+
};
1420+
1421+
The return value is a file descriptor which can be passed to mmap(2)
1422+
to map the allocated RMA into userspace. The mapped area can then be
1423+
passed to the KVM_SET_USER_MEMORY_REGION ioctl to establish it as the
1424+
RMA for a virtual machine. The size of the RMA in bytes (which is
1425+
fixed at host kernel boot time) is returned in the rma_size field of
1426+
the argument structure.
1427+
1428+
The KVM_CAP_PPC_RMA capability is 1 or 2 if the KVM_ALLOCATE_RMA ioctl
1429+
is supported; 2 if the processor requires all virtual machines to have
1430+
an RMA, or 1 if the processor can use an RMA but doesn't require it,
1431+
because it supports the Virtual RMA (VRMA) facility.
1432+
14011433
5. The kvm_run structure
14021434

14031435
Application code obtains a pointer to the kvm_run structure by

‎arch/powerpc/include/asm/kvm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,9 @@ struct kvm_create_spapr_tce {
282282
__u32 window_size;
283283
};
284284

285+
/* for KVM_ALLOCATE_RMA */
286+
struct kvm_allocate_rma {
287+
__u64 rma_size;
288+
};
289+
285290
#endif /* __LINUX_KVM_POWERPC_H */

‎arch/powerpc/include/asm/kvm_book3s.h‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,6 @@ static inline unsigned long kvmppc_interrupt_offset(struct kvm_vcpu *vcpu)
298298
static inline void kvmppc_update_int_pending(struct kvm_vcpu *vcpu,
299299
unsigned long pending_now, unsigned long old_pending)
300300
{
301-
/* Recalculate LPCR:MER based on the presence of
302-
* a pending external interrupt
303-
*/
304-
if (test_bit(BOOK3S_IRQPRIO_EXTERNAL, &pending_now) ||
305-
test_bit(BOOK3S_IRQPRIO_EXTERNAL_LEVEL, &pending_now))
306-
vcpu->arch.lpcr |= LPCR_MER;
307-
else
308-
vcpu->arch.lpcr &= ~((u64)LPCR_MER);
309301
}
310302

311303
static inline void kvmppc_set_gpr(struct kvm_vcpu *vcpu, int num, ulong val)

‎arch/powerpc/include/asm/kvm_host.h‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <linux/threads.h>
2929
#include <linux/spinlock.h>
3030
#include <linux/kvm_para.h>
31+
#include <linux/list.h>
32+
#include <linux/atomic.h>
3133
#include <asm/kvm_asm.h>
3234
#include <asm/processor.h>
3335

@@ -156,6 +158,14 @@ struct kvmppc_spapr_tce_table {
156158
struct page *pages[0];
157159
};
158160

161+
struct kvmppc_rma_info {
162+
void *base_virt;
163+
unsigned long base_pfn;
164+
unsigned long npages;
165+
struct list_head list;
166+
atomic_t use_count;
167+
};
168+
159169
struct kvm_arch {
160170
#ifdef CONFIG_KVM_BOOK3S_64_HV
161171
unsigned long hpt_virt;
@@ -169,6 +179,10 @@ struct kvm_arch {
169179
unsigned long sdr1;
170180
unsigned long host_sdr1;
171181
int tlbie_lock;
182+
int n_rma_pages;
183+
unsigned long lpcr;
184+
unsigned long rmor;
185+
struct kvmppc_rma_info *rma;
172186
struct list_head spapr_tce_tables;
173187
unsigned short last_vcpu[NR_CPUS];
174188
struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
@@ -295,7 +309,6 @@ struct kvm_vcpu_arch {
295309
ulong guest_owned_ext;
296310
ulong purr;
297311
ulong spurr;
298-
ulong lpcr;
299312
ulong dscr;
300313
ulong amr;
301314
ulong uamor;

‎arch/powerpc/include/asm/kvm_ppc.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ extern void kvmppc_map_vrma(struct kvm *kvm,
124124
extern int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu);
125125
extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
126126
struct kvm_create_spapr_tce *args);
127+
extern long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
128+
struct kvm_allocate_rma *rma);
129+
extern struct kvmppc_rma_info *kvm_alloc_rma(void);
130+
extern void kvm_release_rma(struct kvmppc_rma_info *ri);
127131
extern int kvmppc_core_init_vm(struct kvm *kvm);
128132
extern void kvmppc_core_destroy_vm(struct kvm *kvm);
129133
extern int kvmppc_core_prepare_memory_region(struct kvm *kvm,
@@ -177,9 +181,15 @@ static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
177181
{
178182
paca[cpu].kvm_hstate.xics_phys = addr;
179183
}
184+
185+
extern void kvm_rma_init(void);
186+
180187
#else
181188
static inline void kvmppc_set_xics_phys(int cpu, unsigned long addr)
182189
{}
190+
191+
static inline void kvm_rma_init(void)
192+
{}
183193
#endif
184194

185195
#endif /* __POWERPC_KVM_PPC_H__ */

‎arch/powerpc/include/asm/reg.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
#define LPCR_VRMA_LP1 (1ul << (63-16))
243243
#define LPCR_VRMASD_SH (63-16)
244244
#define LPCR_RMLS 0x1C000000 /* impl dependent rmo limit sel */
245+
#define LPCR_RMLS_SH (63-37)
245246
#define LPCR_ILE 0x02000000 /* !HV irqs set MSR:LE */
246247
#define LPCR_PECE 0x00007000 /* powersave exit cause enable */
247248
#define LPCR_PECE0 0x00004000 /* ext. exceptions can cause exit */

‎arch/powerpc/kernel/asm-offsets.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ int main(void)
437437
DEFINE(KVM_TLBIE_LOCK, offsetof(struct kvm, arch.tlbie_lock));
438438
DEFINE(KVM_ONLINE_CPUS, offsetof(struct kvm, online_vcpus.counter));
439439
DEFINE(KVM_LAST_VCPU, offsetof(struct kvm, arch.last_vcpu));
440+
DEFINE(KVM_LPCR, offsetof(struct kvm, arch.lpcr));
441+
DEFINE(KVM_RMOR, offsetof(struct kvm, arch.rmor));
440442
DEFINE(VCPU_DSISR, offsetof(struct kvm_vcpu, arch.shregs.dsisr));
441443
DEFINE(VCPU_DAR, offsetof(struct kvm_vcpu, arch.shregs.dar));
442444
#endif
@@ -459,7 +461,7 @@ int main(void)
459461
DEFINE(VCPU_HFLAGS, offsetof(struct kvm_vcpu, arch.hflags));
460462
DEFINE(VCPU_DEC, offsetof(struct kvm_vcpu, arch.dec));
461463
DEFINE(VCPU_DEC_EXPIRES, offsetof(struct kvm_vcpu, arch.dec_expires));
462-
DEFINE(VCPU_LPCR, offsetof(struct kvm_vcpu, arch.lpcr));
464+
DEFINE(VCPU_PENDING_EXC, offsetof(struct kvm_vcpu, arch.pending_exceptions));
463465
DEFINE(VCPU_VPA, offsetof(struct kvm_vcpu, arch.vpa));
464466
DEFINE(VCPU_MMCR, offsetof(struct kvm_vcpu, arch.mmcr));
465467
DEFINE(VCPU_PMC, offsetof(struct kvm_vcpu, arch.pmc));

‎arch/powerpc/kernel/setup_64.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include <asm/kexec.h>
6464
#include <asm/mmu_context.h>
6565
#include <asm/code-patching.h>
66+
#include <asm/kvm_ppc.h>
6667

6768
#include "setup.h"
6869

@@ -580,6 +581,8 @@ void __init setup_arch(char **cmdline_p)
580581
/* Initialize the MMU context management stuff */
581582
mmu_context_init();
582583

584+
kvm_rma_init();
585+
583586
ppc64_boot_msg(0x15, "Setup Done");
584587
}
585588

‎arch/powerpc/kvm/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ kvm-book3s_64-objs-$(CONFIG_KVM_BOOK3S_64_HV) := \
5656
book3s_64_mmu_hv.o
5757
kvm-book3s_64-builtin-objs-$(CONFIG_KVM_BOOK3S_64_HV) := \
5858
book3s_hv_rm_mmu.o \
59-
book3s_64_vio_hv.o
59+
book3s_64_vio_hv.o \
60+
book3s_hv_builtin.o
6061

6162
kvm-book3s_64-module-objs := \
6263
../../../virt/kvm/kvm_main.o \

‎arch/powerpc/kvm/book3s_64_mmu_hv.c‎

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -79,103 +79,8 @@ long kvmppc_alloc_hpt(struct kvm *kvm)
7979

8080
void kvmppc_free_hpt(struct kvm *kvm)
8181
{
82-
unsigned long i;
83-
struct kvmppc_pginfo *pginfo;
84-
8582
clear_bit(kvm->arch.lpid, lpid_inuse);
8683
free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
87-
88-
if (kvm->arch.ram_pginfo) {
89-
pginfo = kvm->arch.ram_pginfo;
90-
kvm->arch.ram_pginfo = NULL;
91-
for (i = 0; i < kvm->arch.ram_npages; ++i)
92-
put_page(pfn_to_page(pginfo[i].pfn));
93-
kfree(pginfo);
94-
}
95-
}
96-
97-
static unsigned long user_page_size(unsigned long addr)
98-
{
99-
struct vm_area_struct *vma;
100-
unsigned long size = PAGE_SIZE;
101-
102-
down_read(&current->mm->mmap_sem);
103-
vma = find_vma(current->mm, addr);
104-
if (vma)
105-
size = vma_kernel_pagesize(vma);
106-
up_read(&current->mm->mmap_sem);
107-
return size;
108-
}
109-
110-
static pfn_t hva_to_pfn(unsigned long addr)
111-
{
112-
struct page *page[1];
113-
int npages;
114-
115-
might_sleep();
116-
117-
npages = get_user_pages_fast(addr, 1, 1, page);
118-
119-
if (unlikely(npages != 1))
120-
return 0;
121-
122-
return page_to_pfn(page[0]);
123-
}
124-
125-
long kvmppc_prepare_vrma(struct kvm *kvm,
126-
struct kvm_userspace_memory_region *mem)
127-
{
128-
unsigned long psize, porder;
129-
unsigned long i, npages;
130-
struct kvmppc_pginfo *pginfo;
131-
pfn_t pfn;
132-
unsigned long hva;
133-
134-
/* First see what page size we have */
135-
psize = user_page_size(mem->userspace_addr);
136-
/* For now, only allow 16MB pages */
137-
if (psize != 1ul << VRMA_PAGE_ORDER || (mem->memory_size & (psize - 1))) {
138-
pr_err("bad psize=%lx memory_size=%llx @ %llx\n",
139-
psize, mem->memory_size, mem->userspace_addr);
140-
return -EINVAL;
141-
}
142-
porder = __ilog2(psize);
143-
144-
npages = mem->memory_size >> porder;
145-
pginfo = kzalloc(npages * sizeof(struct kvmppc_pginfo), GFP_KERNEL);
146-
if (!pginfo) {
147-
pr_err("kvmppc_prepare_vrma: couldn't alloc %lu bytes\n",
148-
npages * sizeof(struct kvmppc_pginfo));
149-
return -ENOMEM;
150-
}
151-
152-
for (i = 0; i < npages; ++i) {
153-
hva = mem->userspace_addr + (i << porder);
154-
if (user_page_size(hva) != psize)
155-
goto err;
156-
pfn = hva_to_pfn(hva);
157-
if (pfn == 0) {
158-
pr_err("oops, no pfn for hva %lx\n", hva);
159-
goto err;
160-
}
161-
if (pfn & ((1ul << (porder - PAGE_SHIFT)) - 1)) {
162-
pr_err("oops, unaligned pfn %llx\n", pfn);
163-
put_page(pfn_to_page(pfn));
164-
goto err;
165-
}
166-
pginfo[i].pfn = pfn;
167-
}
168-
169-
kvm->arch.ram_npages = npages;
170-
kvm->arch.ram_psize = psize;
171-
kvm->arch.ram_porder = porder;
172-
kvm->arch.ram_pginfo = pginfo;
173-
174-
return 0;
175-
176-
err:
177-
kfree(pginfo);
178-
return -EINVAL;
17984
}
18085

18186
void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
@@ -199,6 +104,8 @@ void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
199104

200105
for (i = 0; i < npages; ++i) {
201106
pfn = pginfo[i].pfn;
107+
if (!pfn)
108+
break;
202109
/* can't use hpt_hash since va > 64 bits */
203110
hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
204111
/*

0 commit comments

Comments
 (0)