3636#include <asm/kvm_host.h>
3737#include <asm/udbg.h>
3838#include <asm/iommu.h>
39+ #include <asm/tce.h>
3940
4041#define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64))
4142
@@ -64,7 +65,7 @@ static struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm_vcpu *vcpu,
6465 * WARNING: This will be called in real-mode on HV KVM and virtual
6566 * mode on PR KVM
6667 */
67- static long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table * stt ,
68+ long kvmppc_ioba_validate (struct kvmppc_spapr_tce_table * stt ,
6869 unsigned long ioba , unsigned long npages )
6970{
7071 unsigned long mask = (1ULL << IOMMU_PAGE_SHIFT_4K ) - 1 ;
@@ -76,6 +77,79 @@ static long kvmppc_ioba_validate(struct kvmppc_spapr_tce_table *stt,
7677
7778 return H_SUCCESS ;
7879}
80+ EXPORT_SYMBOL_GPL (kvmppc_ioba_validate );
81+
82+ /*
83+ * Validates TCE address.
84+ * At the moment flags and page mask are validated.
85+ * As the host kernel does not access those addresses (just puts them
86+ * to the table and user space is supposed to process them), we can skip
87+ * checking other things (such as TCE is a guest RAM address or the page
88+ * was actually allocated).
89+ *
90+ * WARNING: This will be called in real-mode on HV KVM and virtual
91+ * mode on PR KVM
92+ */
93+ long kvmppc_tce_validate (struct kvmppc_spapr_tce_table * stt , unsigned long tce )
94+ {
95+ unsigned long mask =
96+ ~(IOMMU_PAGE_MASK_4K | TCE_PCI_WRITE | TCE_PCI_READ );
97+
98+ if (tce & mask )
99+ return H_PARAMETER ;
100+
101+ return H_SUCCESS ;
102+ }
103+ EXPORT_SYMBOL_GPL (kvmppc_tce_validate );
104+
105+ /* Note on the use of page_address() in real mode,
106+ *
107+ * It is safe to use page_address() in real mode on ppc64 because
108+ * page_address() is always defined as lowmem_page_address()
109+ * which returns __va(PFN_PHYS(page_to_pfn(page))) which is arithmetic
110+ * operation and does not access page struct.
111+ *
112+ * Theoretically page_address() could be defined different
113+ * but either WANT_PAGE_VIRTUAL or HASHED_PAGE_VIRTUAL
114+ * would have to be enabled.
115+ * WANT_PAGE_VIRTUAL is never enabled on ppc32/ppc64,
116+ * HASHED_PAGE_VIRTUAL could be enabled for ppc32 only and only
117+ * if CONFIG_HIGHMEM is defined. As CONFIG_SPARSEMEM_VMEMMAP
118+ * is not expected to be enabled on ppc32, page_address()
119+ * is safe for ppc32 as well.
120+ *
121+ * WARNING: This will be called in real-mode on HV KVM and virtual
122+ * mode on PR KVM
123+ */
124+ static u64 * kvmppc_page_address (struct page * page )
125+ {
126+ #if defined(HASHED_PAGE_VIRTUAL ) || defined(WANT_PAGE_VIRTUAL )
127+ #error TODO: fix to avoid page_address() here
128+ #endif
129+ return (u64 * ) page_address (page );
130+ }
131+
132+ /*
133+ * Handles TCE requests for emulated devices.
134+ * Puts guest TCE values to the table and expects user space to convert them.
135+ * Called in both real and virtual modes.
136+ * Cannot fail so kvmppc_tce_validate must be called before it.
137+ *
138+ * WARNING: This will be called in real-mode on HV KVM and virtual
139+ * mode on PR KVM
140+ */
141+ void kvmppc_tce_put (struct kvmppc_spapr_tce_table * stt ,
142+ unsigned long idx , unsigned long tce )
143+ {
144+ struct page * page ;
145+ u64 * tbl ;
146+
147+ page = stt -> pages [idx / TCES_PER_PAGE ];
148+ tbl = kvmppc_page_address (page );
149+
150+ tbl [idx % TCES_PER_PAGE ] = tce ;
151+ }
152+ EXPORT_SYMBOL_GPL (kvmppc_tce_put );
79153
80154/* WARNING: This will be called in real-mode on HV KVM and virtual
81155 * mode on PR KVM
@@ -85,9 +159,6 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
85159{
86160 struct kvmppc_spapr_tce_table * stt = kvmppc_find_table (vcpu , liobn );
87161 long ret ;
88- unsigned long idx ;
89- struct page * page ;
90- u64 * tbl ;
91162
92163 /* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
93164 /* liobn, ioba, tce); */
@@ -99,13 +170,11 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
99170 if (ret != H_SUCCESS )
100171 return ret ;
101172
102- idx = ioba >> IOMMU_PAGE_SHIFT_4K ;
103- page = stt -> pages [ idx / TCES_PER_PAGE ];
104- tbl = ( u64 * ) page_address ( page ) ;
173+ ret = kvmppc_tce_validate ( stt , tce ) ;
174+ if ( ret != H_SUCCESS )
175+ return ret ;
105176
106- /* FIXME: Need to validate the TCE itself */
107- /* udbg_printf("tce @ %p\n", &tbl[idx % TCES_PER_PAGE]); */
108- tbl [idx % TCES_PER_PAGE ] = tce ;
177+ kvmppc_tce_put (stt , ioba >> IOMMU_PAGE_SHIFT_4K , tce );
109178
110179 return H_SUCCESS ;
111180}
0 commit comments