diff mbox

[edk2,2/3] Ovmf: XenHypercallLib: add explicit cache maintenance

Message ID 1427224544-29955-2-git-send-email-ard.biesheuvel@linaro.org
State New
Headers show

Commit Message

Ard Biesheuvel March 24, 2015, 7:15 p.m. UTC
This library may be used with the caches and MMU off, while the
hypervisor has the caches enabled. So add explicit cache
maintenance to deal with the potential incoherency.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
---
 OvmfPkg/Library/XenHypercallLib/XenHypercall.c      | 15 +++++++++++++++
 OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf |  3 +++
 2 files changed, 18 insertions(+)

Comments

Ard Biesheuvel March 24, 2015, 9:51 p.m. UTC | #1
On 24 March 2015 at 22:47, Stefano Stabellini
<stefano.stabellini@eu.citrix.com> wrote:
> On Tue, 24 Mar 2015, Ard Biesheuvel wrote:
>> This library may be used with the caches and MMU off, while the
>> hypervisor has the caches enabled. So add explicit cache
>> maintenance to deal with the potential incoherency.
>>
>> Contributed-under: TianoCore Contribution Agreement 1.0
>> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
>> ---
>>  OvmfPkg/Library/XenHypercallLib/XenHypercall.c      | 15 +++++++++++++++
>>  OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf |  3 +++
>>  2 files changed, 18 insertions(+)
>>
>> diff --git a/OvmfPkg/Library/XenHypercallLib/XenHypercall.c b/OvmfPkg/Library/XenHypercallLib/XenHypercall.c
>> index ecc757cf707c..00ba9e64b406 100644
>> --- a/OvmfPkg/Library/XenHypercallLib/XenHypercall.c
>> +++ b/OvmfPkg/Library/XenHypercallLib/XenHypercall.c
>> @@ -20,6 +20,7 @@
>>
>>  #include <Library/DebugLib.h>
>>  #include <Library/XenHypercallLib.h>
>> +#include <Library/CacheMaintenanceLib.h>
>>
>>  UINT64
>>  XenHypercallHvmGetParam (
>> @@ -31,6 +32,13 @@ XenHypercallHvmGetParam (
>>
>>    Parameter.domid = DOMID_SELF;
>>    Parameter.index = Index;
>> +
>> +  //
>> +  // We may be running with the MMU and caches off, so invalidate any copies
>> +  // of Parameter in the cache before passing it to the hypervisor.
>> +  //
>> +  InvalidateDataCacheRange (&Parameter, sizeof(Parameter));
>> +
>>    Error = XenHypercall2 (__HYPERVISOR_hvm_op,
>>                           HVMOP_get_param, (INTN) &Parameter);
>>    if (Error != 0) {
>> @@ -39,6 +47,13 @@ XenHypercallHvmGetParam (
>>              Error, Index));
>>      return 0;
>>    }
>> +
>> +  //
>> +  // We may be running with the MMU and caches off, so force the updated
>> +  // Parameter to be visible in main memory.
>> +  //
>> +  WriteBackDataCacheRange (&Parameter, sizeof(Parameter));
>> +
>>    return Parameter.value;
>>  }
>
> What about the other hypercalls?
>

Yes, you are right. They did not look like by-ref params at first
glance, but they should be treated the same.

However, it seems that I can get away with deferring all of this until
the cache and MMU are up, which means I can at least avoid cache
maintenance in this library, and hopefully also in the
xenconsoleserialportlib driver.


>
>> diff --git a/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf b/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf
>> index f4503a4b01f4..8175ba6d3186 100644
>> --- a/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf
>> +++ b/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf
>> @@ -62,5 +62,8 @@
>>    HobLib
>>    DebugLib
>>
>> +[LibraryClasses]
>> +  CacheMaintenanceLib
>> +
>>  [Guids.IA32, Guids.X64]
>>    gEfiXenInfoGuid
>> --
>> 1.8.3.2
>>

------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
diff mbox

Patch

diff --git a/OvmfPkg/Library/XenHypercallLib/XenHypercall.c b/OvmfPkg/Library/XenHypercallLib/XenHypercall.c
index ecc757cf707c..00ba9e64b406 100644
--- a/OvmfPkg/Library/XenHypercallLib/XenHypercall.c
+++ b/OvmfPkg/Library/XenHypercallLib/XenHypercall.c
@@ -20,6 +20,7 @@ 
 
 #include <Library/DebugLib.h>
 #include <Library/XenHypercallLib.h>
+#include <Library/CacheMaintenanceLib.h>
 
 UINT64
 XenHypercallHvmGetParam (
@@ -31,6 +32,13 @@  XenHypercallHvmGetParam (
 
   Parameter.domid = DOMID_SELF;
   Parameter.index = Index;
+
+  //
+  // We may be running with the MMU and caches off, so invalidate any copies
+  // of Parameter in the cache before passing it to the hypervisor.
+  //
+  InvalidateDataCacheRange (&Parameter, sizeof(Parameter));
+
   Error = XenHypercall2 (__HYPERVISOR_hvm_op,
                          HVMOP_get_param, (INTN) &Parameter);
   if (Error != 0) {
@@ -39,6 +47,13 @@  XenHypercallHvmGetParam (
             Error, Index));
     return 0;
   }
+
+  //
+  // We may be running with the MMU and caches off, so force the updated
+  // Parameter to be visible in main memory.
+  //
+  WriteBackDataCacheRange (&Parameter, sizeof(Parameter));
+
   return Parameter.value;
 }
 
diff --git a/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf b/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf
index f4503a4b01f4..8175ba6d3186 100644
--- a/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf
+++ b/OvmfPkg/Library/XenHypercallLib/XenHypercallLib.inf
@@ -62,5 +62,8 @@ 
   HobLib
   DebugLib
 
+[LibraryClasses]
+  CacheMaintenanceLib
+
 [Guids.IA32, Guids.X64]
   gEfiXenInfoGuid