]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/XenBusDxe: Add support to make Xen Hypercalls.
authorAnthony PERARD <anthony.perard@citrix.com>
Wed, 29 Oct 2014 06:49:10 +0000 (06:49 +0000)
committerjljusten <jljusten@Edk2>
Wed, 29 Oct 2014 06:49:10 +0000 (06:49 +0000)
Change in V4:
- Replace the license by the commonly used file header text.
- add file header to XenHypercall.h (license, copyright, brief desc)

Change in V3:
- adding IA32 support. (not reviewed yet)
  both XenBusDxe/Ia32/hypercall.{S,asm} file are new

Change in V2:
- file header, copyright
- Add License
- Add push/pop instruction.
- fix types
- Comment of exported functions
- Improve coding style
- Add error handling in the main init function (of the drivers)
- Comment assembly

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16260 6f19259b-4bc3-4df7-8a09-765794883524

OvmfPkg/XenBusDxe/Ia32/hypercall.S [new file with mode: 0644]
OvmfPkg/XenBusDxe/Ia32/hypercall.asm [new file with mode: 0644]
OvmfPkg/XenBusDxe/X64/hypercall.S [new file with mode: 0644]
OvmfPkg/XenBusDxe/X64/hypercall.asm [new file with mode: 0644]
OvmfPkg/XenBusDxe/XenBusDxe.c
OvmfPkg/XenBusDxe/XenBusDxe.h
OvmfPkg/XenBusDxe/XenBusDxe.inf
OvmfPkg/XenBusDxe/XenHypercall.c [new file with mode: 0644]
OvmfPkg/XenBusDxe/XenHypercall.h [new file with mode: 0644]

diff --git a/OvmfPkg/XenBusDxe/Ia32/hypercall.S b/OvmfPkg/XenBusDxe/Ia32/hypercall.S
new file mode 100644 (file)
index 0000000..77d3478
--- /dev/null
@@ -0,0 +1,22 @@
+# INTN\r
+# EFIAPI\r
+# XenHypercall2 (\r
+#   IN     VOID *HypercallAddr,\r
+#   IN OUT INTN Arg1,\r
+#   IN OUT INTN Arg2\r
+#   );\r
+ASM_GLOBAL ASM_PFX(XenHypercall2)\r
+ASM_PFX(XenHypercall2):\r
+  # Save only ebx, ecx is supposed to be a scratch register and needs to be\r
+  # saved by the caller\r
+  push %ebx\r
+  # Copy HypercallAddr to eax\r
+  mov 8(%esp), %eax\r
+  # Copy Arg1 to the register expected by Xen\r
+  mov 12(%esp), %ebx\r
+  # Copy Arg2 to the register expected by Xen\r
+  mov 16(%esp), %ecx\r
+  # Call HypercallAddr\r
+  call *%eax\r
+  pop %ebx\r
+  ret\r
diff --git a/OvmfPkg/XenBusDxe/Ia32/hypercall.asm b/OvmfPkg/XenBusDxe/Ia32/hypercall.asm
new file mode 100644 (file)
index 0000000..9ead740
--- /dev/null
@@ -0,0 +1,26 @@
+.code\r
+\r
+; INTN\r
+; EFIAPI\r
+; XenHypercall2 (\r
+;   IN     VOID *HypercallAddr,\r
+;   IN OUT INTN Arg1,\r
+;   IN OUT INTN Arg2\r
+;   );\r
+XenHypercall2 PROC\r
+  ; Save only ebx, ecx is supposed to be a scratch register and needs to be\r
+  ; saved by the caller\r
+  push ebx\r
+  ; Copy HypercallAddr to eax\r
+  mov eax, [esp + 8]\r
+  ; Copy Arg1 to the register expected by Xen\r
+  mov ebx, [esp + 12]\r
+  ; Copy Arg2 to the register expected by Xen\r
+  mov ecx, [esp + 16]\r
+  ; Call HypercallAddr\r
+  call eax\r
+  pop ebx\r
+  ret\r
+XenHypercall2 ENDP\r
+\r
+END\r
diff --git a/OvmfPkg/XenBusDxe/X64/hypercall.S b/OvmfPkg/XenBusDxe/X64/hypercall.S
new file mode 100644 (file)
index 0000000..83cf466
--- /dev/null
@@ -0,0 +1,22 @@
+# INTN\r
+# EFIAPI\r
+# XenHypercall2 (\r
+#   IN     VOID *HypercallAddr,\r
+#   IN OUT INTN Arg1,\r
+#   IN OUT INTN Arg2\r
+#   );\r
+ASM_GLOBAL ASM_PFX(XenHypercall2)\r
+ASM_PFX(XenHypercall2):\r
+  push %rdi\r
+  push %rsi\r
+  # Copy HypercallAddr to rax\r
+  movq %rcx, %rax\r
+  # Copy Arg1 to the register expected by Xen\r
+  movq %rdx, %rdi\r
+  # Copy Arg2 to the register expected by Xen\r
+  movq %r8, %rsi\r
+  # Call HypercallAddr\r
+  call *%rax\r
+  pop %rsi\r
+  pop %rdi\r
+  ret\r
diff --git a/OvmfPkg/XenBusDxe/X64/hypercall.asm b/OvmfPkg/XenBusDxe/X64/hypercall.asm
new file mode 100644 (file)
index 0000000..5b34514
--- /dev/null
@@ -0,0 +1,26 @@
+.code\r
+\r
+; INTN\r
+; EFIAPI\r
+; XenHypercall2 (\r
+;   IN     VOID *HypercallAddr,\r
+;   IN OUT INTN Arg1,\r
+;   IN OUT INTN Arg2\r
+;   );\r
+XenHypercall2 PROC\r
+  push rdi\r
+  push rsi\r
+  ; Copy HypercallAddr to rax\r
+  mov rax, rcx\r
+  ; Copy Arg1 to the register expected by Xen\r
+  mov rdi, rdx\r
+  ; Copy Arg2 to the register expected by Xen\r
+  mov rsi, r8\r
+  ; Call HypercallAddr\r
+  call rax\r
+  pop rsi\r
+  pop rdi\r
+  ret\r
+XenHypercall2 ENDP\r
+\r
+END\r
index f3c74e1fbe00337d8654408a246c5bce98073935..4c638b85f1abbe14aab3121a19da15e85035a69a 100644 (file)
@@ -29,6 +29,8 @@
 \r
 #include "XenBusDxe.h"\r
 \r
+#include "XenHypercall.h"\r
+\r
 \r
 ///\r
 /// Driver Binding Protocol instance\r
@@ -264,6 +266,8 @@ NotifyExitBoot (
   @retval EFI_SUCCESS              The device was started.\r
   @retval EFI_DEVICE_ERROR         The device could not be started due to a device error.Currently not implemented.\r
   @retval EFI_OUT_OF_RESOURCES     The request could not be completed due to a lack of resources.\r
+  @retval EFI_UNSUPPORTED          Something is missing on the system that\r
+                                   prevent to start the edvice.\r
   @retval Others                   The driver failded to start the device.\r
 \r
 **/\r
@@ -295,6 +299,20 @@ XenBusDxeDriverBindingStart (
   mMyDevice = Dev;\r
   EfiReleaseLock (&mMyDeviceLock);\r
 \r
+  Status = XenHyperpageInit (Dev);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XenBus: Unable to retrieve the hyperpage.\n"));\r
+    Status = EFI_UNSUPPORTED;\r
+    goto ErrorAllocated;\r
+  }\r
+\r
+  Status = XenGetSharedInfoPage (Dev);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "XenBus: Unable to get the shared info page.\n"));\r
+    Status = EFI_UNSUPPORTED;\r
+    goto ErrorAllocated;\r
+  }\r
+\r
   Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,\r
                              NotifyExitBoot,\r
                              (VOID*) Dev,\r
index d7537f3fe5ea9f153adc85640797d794f7e37ae7..ccec0ce0e55dad56ce51f0a4ee60e35debf1a6c6 100644 (file)
@@ -70,6 +70,8 @@ extern EFI_COMPONENT_NAME_PROTOCOL  gXenBusDxeComponentName;
 //\r
 // Other stuff\r
 //\r
+#include <IndustryStandard/Xen/xen.h>\r
+\r
 #define PCI_VENDOR_ID_XEN                0x5853\r
 #define PCI_DEVICE_ID_XEN_PLATFORM       0x0001\r
 \r
@@ -83,6 +85,9 @@ struct _XENBUS_DEVICE {
   EFI_DRIVER_BINDING_PROTOCOL   *This;\r
   EFI_HANDLE                    ControllerHandle;\r
   EFI_EVENT                     ExitBootEvent;\r
+\r
+  VOID                          *Hyperpage;\r
+  shared_info_t                 *SharedInfo;\r
 };\r
 \r
 #endif\r
index fb63ec251e48ed326df0458c4a8de456bd856fd7..6da3d2d670a95e54801abf66c565277b807b5afa 100644 (file)
   DriverBinding.h\r
   ComponentName.c\r
   ComponentName.h\r
+  XenHypercall.c\r
+  XenHypercall.h\r
+\r
+[Sources.IA32]\r
+  Ia32/hypercall.S\r
+  Ia32/hypercall.asm\r
+\r
+[Sources.X64]\r
+  X64/hypercall.S\r
+  X64/hypercall.asm\r
 \r
 [LibraryClasses]\r
   UefiDriverEntryPoint\r
diff --git a/OvmfPkg/XenBusDxe/XenHypercall.c b/OvmfPkg/XenBusDxe/XenHypercall.c
new file mode 100644 (file)
index 0000000..0f2ba5d
--- /dev/null
@@ -0,0 +1,118 @@
+/** @file\r
+  Functions to make Xen hypercalls.\r
+\r
+  Copyright (C) 2014, Citrix Ltd.\r
+\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <PiDxe.h>\r
+#include <Library/HobLib.h>\r
+#include <Guid/XenInfo.h>\r
+\r
+#include "XenBusDxe.h"\r
+#include "XenHypercall.h"\r
+\r
+#include <IndustryStandard/Xen/hvm/params.h>\r
+#include <IndustryStandard/Xen/memory.h>\r
+\r
+EFI_STATUS\r
+XenHyperpageInit (\r
+  IN OUT XENBUS_DEVICE *Dev\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE   *GuidHob;\r
+  EFI_XEN_INFO        *XenInfo;\r
+\r
+  GuidHob = GetFirstGuidHob (&gEfiXenInfoGuid);\r
+  if (GuidHob == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  XenInfo = (EFI_XEN_INFO *) GET_GUID_HOB_DATA (GuidHob);\r
+  Dev->Hyperpage = XenInfo->HyperPages;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+UINT64\r
+XenHypercallHvmGetParam (\r
+  IN XENBUS_DEVICE *Dev,\r
+  IN INTN          Index\r
+  )\r
+{\r
+  xen_hvm_param_t     Parameter;\r
+  INTN                Error;\r
+\r
+  ASSERT (Dev->Hyperpage != NULL);\r
+\r
+  Parameter.domid = DOMID_SELF;\r
+  Parameter.index = Index;\r
+  Error = XenHypercall2 (Dev->Hyperpage + __HYPERVISOR_hvm_op * 32,\r
+                         HVMOP_get_param, (INTN) &Parameter);\r
+  if (Error != 0) {\r
+    DEBUG ((EFI_D_ERROR,\r
+            "XenHypercall: Error %d trying to get HVM parameter %d\n",\r
+            Error, Index));\r
+    return 0;\r
+  }\r
+  return Parameter.value;\r
+}\r
+\r
+INTN\r
+XenHypercallMemoryOp (\r
+  IN     XENBUS_DEVICE *Dev,\r
+  IN     UINTN Operation,\r
+  IN OUT VOID *Arguments\r
+  )\r
+{\r
+  ASSERT (Dev->Hyperpage != NULL);\r
+  return XenHypercall2 (Dev->Hyperpage + __HYPERVISOR_memory_op * 32,\r
+                        Operation, (INTN) Arguments);\r
+}\r
+\r
+INTN\r
+XenHypercallEventChannelOp (\r
+  IN     XENBUS_DEVICE *Dev,\r
+  IN     INTN Operation,\r
+  IN OUT VOID *Arguments\r
+  )\r
+{\r
+  ASSERT (Dev->Hyperpage != NULL);\r
+  return XenHypercall2 (Dev->Hyperpage + __HYPERVISOR_event_channel_op * 32,\r
+                        Operation, (INTN) Arguments);\r
+}\r
+\r
+EFI_STATUS\r
+XenGetSharedInfoPage (\r
+  IN OUT XENBUS_DEVICE *Dev\r
+  )\r
+{\r
+  xen_add_to_physmap_t Parameter;\r
+\r
+  ASSERT (Dev->SharedInfo == NULL);\r
+\r
+  Parameter.domid = DOMID_SELF;\r
+  Parameter.space = XENMAPSPACE_shared_info;\r
+  Parameter.idx = 0;\r
+\r
+  //\r
+  // using reserved page because the page is not released when Linux is\r
+  // starting because of the add_to_physmap. QEMU might try to access the\r
+  // page, and fail because it have no right to do so (segv).\r
+  //\r
+  Dev->SharedInfo = AllocateReservedPages (1);\r
+  Parameter.gpfn = (UINTN) Dev->SharedInfo >> EFI_PAGE_SHIFT;\r
+  if (XenHypercallMemoryOp (Dev, XENMEM_add_to_physmap, &Parameter) != 0) {\r
+    FreePages (Dev->SharedInfo, 1);\r
+    Dev->SharedInfo = NULL;\r
+    return EFI_LOAD_ERROR;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
diff --git a/OvmfPkg/XenBusDxe/XenHypercall.h b/OvmfPkg/XenBusDxe/XenHypercall.h
new file mode 100644 (file)
index 0000000..3627b18
--- /dev/null
@@ -0,0 +1,115 @@
+/** @file\r
+  Functions declarations to make Xen hypercalls.\r
+\r
+  Copyright (C) 2014, Citrix Ltd.\r
+\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#ifndef __XENBUS_DXE_HYPERCALL_H__\r
+#define __XENBUS_DXE_HYPERCALL_H__\r
+\r
+typedef struct _XENBUS_DEVICE XENBUS_DEVICE;\r
+\r
+/**\r
+  This function will put the two arguments in the right place (registers) and\r
+  call HypercallAddr, which correspond to an entry in the hypercall pages.\r
+\r
+  @param HypercallAddr  A memory address where the hypercall to call is.\r
+  @param Arg1           First argument.\r
+  @param Arg2           Second argument.\r
+\r
+  @return   Return 0 if success otherwise it return an errno.\r
+**/\r
+INTN\r
+EFIAPI\r
+XenHypercall2 (\r
+  IN     VOID *HypercallAddr,\r
+  IN OUT INTN Arg1,\r
+  IN OUT INTN Arg2\r
+  );\r
+\r
+/**\r
+  Get the page where all hypercall are from the XenInfo hob.\r
+\r
+  @param Dev    A XENBUS_DEVICE instance.\r
+\r
+  @retval EFI_NOT_FOUND   hyperpage could not be found.\r
+  @retval EFI_SUCCESS     Successfully retrieve the hyperpage pointer.\r
+**/\r
+EFI_STATUS\r
+XenHyperpageInit (\r
+  XENBUS_DEVICE *Dev\r
+  );\r
+\r
+/**\r
+  Return the value of the HVM parameter Index.\r
+\r
+  @param Dev    A XENBUS_DEVICE instance.\r
+  @param Index  The parameter to get, e.g. HVM_PARAM_STORE_EVTCHN.\r
+\r
+  @return   The value of the asked parameter or 0 in case of error.\r
+**/\r
+UINT64\r
+XenHypercallHvmGetParam (\r
+  XENBUS_DEVICE *Dev,\r
+  INTN Index\r
+  );\r
+\r
+/**\r
+  Hypercall to do different operation on the memory.\r
+\r
+  @param Dev        A XENBUS_DEVICE instance.\r
+  @param Operation  The operation number, e.g. XENMEM_add_to_physmap.\r
+  @param Arguments  The arguments associated to the operation.\r
+\r
+  @return  Return the return value from the hypercall, 0 in case of success\r
+           otherwise, an error code.\r
+**/\r
+INTN\r
+XenHypercallMemoryOp (\r
+  IN     XENBUS_DEVICE *Dev,\r
+  IN     UINTN Operation,\r
+  IN OUT VOID *Arguments\r
+  );\r
+\r
+/**\r
+  Do an operation on the event channels.\r
+\r
+  @param Dev        A XENBUS_DEVICE instance.\r
+  @param Operation  The operation number, e.g. EVTCHNOP_send.\r
+  @param Arguments  The argument associated to the operation.\r
+\r
+  @return  Return the return value from the hypercall, 0 in case of success\r
+           otherwise, an error code.\r
+**/\r
+INTN\r
+XenHypercallEventChannelOp (\r
+  IN     XENBUS_DEVICE *Dev,\r
+  IN     INTN Operation,\r
+  IN OUT VOID *Arguments\r
+  );\r
+\r
+/**\r
+  Map the shared_info_t page into memory.\r
+\r
+  @param Dev    A XENBUS_DEVICE instance.\r
+\r
+  @retval EFI_SUCCESS     Dev->SharedInfo whill contain a pointer to\r
+                          the shared info page\r
+  @retval EFI_LOAD_ERROR  The shared info page could not be mapped. The\r
+                          hypercall returned an error.\r
+**/\r
+EFI_STATUS\r
+XenGetSharedInfoPage (\r
+  IN OUT XENBUS_DEVICE *Dev\r
+  );\r
+\r
+#endif\r