XenBusDxe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /** @file
  2. This driver produces XenBus Protocol instances for each Xen PV devices.
  3. This XenBus bus driver will first initialize different services in order to
  4. enumerate the ParaVirtualized devices available.
  5. Those services are:
  6. - HyperCall
  7. - Event Channel
  8. - Grant Table
  9. - XenStore
  10. - XenBus
  11. Copyright (C) 2014, Citrix Ltd.
  12. SPDX-License-Identifier: BSD-2-Clause-Patent
  13. **/
  14. #include <Library/DebugLib.h>
  15. #include <Library/XenHypercallLib.h>
  16. #include "XenBusDxe.h"
  17. #include "GrantTable.h"
  18. #include "XenStore.h"
  19. #include "XenBus.h"
  20. #include <IndustryStandard/Xen/hvm/params.h>
  21. #include <IndustryStandard/Xen/memory.h>
  22. ///
  23. /// Driver Binding Protocol instance
  24. ///
  25. EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding = {
  26. XenBusDxeDriverBindingSupported,
  27. XenBusDxeDriverBindingStart,
  28. XenBusDxeDriverBindingStop,
  29. XENBUS_DXE_VERSION,
  30. NULL,
  31. NULL
  32. };
  33. STATIC EFI_LOCK mMyDeviceLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK);
  34. STATIC XENBUS_DEVICE *mMyDevice = NULL;
  35. /**
  36. Map the shared_info_t page into memory.
  37. @param Dev A XENBUS_DEVICE instance.
  38. @retval EFI_SUCCESS Dev->SharedInfo whill contain a pointer to
  39. the shared info page
  40. @retval EFI_LOAD_ERROR The shared info page could not be mapped. The
  41. hypercall returned an error.
  42. **/
  43. STATIC
  44. EFI_STATUS
  45. XenGetSharedInfoPage (
  46. IN OUT XENBUS_DEVICE *Dev
  47. )
  48. {
  49. xen_add_to_physmap_t Parameter;
  50. ASSERT (Dev->SharedInfo == NULL);
  51. Parameter.domid = DOMID_SELF;
  52. Parameter.space = XENMAPSPACE_shared_info;
  53. Parameter.idx = 0;
  54. //
  55. // using reserved page because the page is not released when Linux is
  56. // starting because of the add_to_physmap. QEMU might try to access the
  57. // page, and fail because it have no right to do so (segv).
  58. //
  59. Dev->SharedInfo = AllocateReservedPages (1);
  60. Parameter.gpfn = (UINTN)Dev->SharedInfo >> EFI_PAGE_SHIFT;
  61. if (XenHypercallMemoryOp (XENMEM_add_to_physmap, &Parameter) != 0) {
  62. FreePages (Dev->SharedInfo, 1);
  63. Dev->SharedInfo = NULL;
  64. return EFI_LOAD_ERROR;
  65. }
  66. return EFI_SUCCESS;
  67. }
  68. /**
  69. Unloads an image.
  70. @param ImageHandle Handle that identifies the image to be unloaded.
  71. @retval EFI_SUCCESS The image has been unloaded.
  72. @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
  73. **/
  74. EFI_STATUS
  75. EFIAPI
  76. XenBusDxeUnload (
  77. IN EFI_HANDLE ImageHandle
  78. )
  79. {
  80. EFI_STATUS Status;
  81. EFI_HANDLE *HandleBuffer;
  82. UINTN HandleCount;
  83. UINTN Index;
  84. //
  85. // Retrieve array of all handles in the handle database
  86. //
  87. Status = gBS->LocateHandleBuffer (
  88. AllHandles,
  89. NULL,
  90. NULL,
  91. &HandleCount,
  92. &HandleBuffer
  93. );
  94. if (EFI_ERROR (Status)) {
  95. return Status;
  96. }
  97. //
  98. // Disconnect the current driver from handles in the handle database
  99. //
  100. for (Index = 0; Index < HandleCount; Index++) {
  101. gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);
  102. }
  103. //
  104. // Free the array of handles
  105. //
  106. FreePool (HandleBuffer);
  107. //
  108. // Uninstall protocols installed in the driver entry point
  109. //
  110. Status = gBS->UninstallMultipleProtocolInterfaces (
  111. ImageHandle,
  112. &gEfiDriverBindingProtocolGuid,
  113. &gXenBusDxeDriverBinding,
  114. &gEfiComponentNameProtocolGuid,
  115. &gXenBusDxeComponentName,
  116. &gEfiComponentName2ProtocolGuid,
  117. &gXenBusDxeComponentName2,
  118. NULL
  119. );
  120. if (EFI_ERROR (Status)) {
  121. return Status;
  122. }
  123. return EFI_SUCCESS;
  124. }
  125. /**
  126. This is the declaration of an EFI image entry point. This entry point is
  127. the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
  128. both device drivers and bus drivers.
  129. @param ImageHandle The firmware allocated handle for the UEFI image.
  130. @param SystemTable A pointer to the EFI System Table.
  131. @retval EFI_SUCCESS The operation completed successfully.
  132. @retval EFI_ABORTED Xen hypercalls are not available.
  133. @retval Others An unexpected error occurred.
  134. **/
  135. EFI_STATUS
  136. EFIAPI
  137. XenBusDxeDriverEntryPoint (
  138. IN EFI_HANDLE ImageHandle,
  139. IN EFI_SYSTEM_TABLE *SystemTable
  140. )
  141. {
  142. EFI_STATUS Status;
  143. if (!XenHypercallIsAvailable ()) {
  144. return EFI_ABORTED;
  145. }
  146. //
  147. // Install UEFI Driver Model protocol(s).
  148. //
  149. Status = EfiLibInstallDriverBindingComponentName2 (
  150. ImageHandle,
  151. SystemTable,
  152. &gXenBusDxeDriverBinding,
  153. ImageHandle,
  154. &gXenBusDxeComponentName,
  155. &gXenBusDxeComponentName2
  156. );
  157. ASSERT_EFI_ERROR (Status);
  158. return Status;
  159. }
  160. /**
  161. Tests to see if this driver supports a given controller. If a child device is provided,
  162. it further tests to see if this driver supports creating a handle for the specified child device.
  163. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  164. @param[in] ControllerHandle The handle of the controller to test. This handle
  165. must support a protocol interface that supplies
  166. an I/O abstraction to the driver.
  167. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  168. parameter is ignored by device drivers, and is optional for bus
  169. drivers. For bus drivers, if this parameter is not NULL, then
  170. the bus driver must determine if the bus controller specified
  171. by ControllerHandle and the child controller specified
  172. by RemainingDevicePath are both supported by this
  173. bus driver.
  174. @retval EFI_SUCCESS The device specified by ControllerHandle and
  175. RemainingDevicePath is supported by the driver specified by This.
  176. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  177. RemainingDevicePath is already being managed by the driver
  178. specified by This.
  179. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  180. RemainingDevicePath is already being managed by a different
  181. driver or an application that requires exclusive access.
  182. Currently not implemented.
  183. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  184. RemainingDevicePath is not supported by the driver specified by This.
  185. **/
  186. EFI_STATUS
  187. EFIAPI
  188. XenBusDxeDriverBindingSupported (
  189. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  190. IN EFI_HANDLE ControllerHandle,
  191. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  192. )
  193. {
  194. EFI_STATUS Status;
  195. XENIO_PROTOCOL *XenIo;
  196. Status = gBS->OpenProtocol (
  197. ControllerHandle,
  198. &gXenIoProtocolGuid,
  199. (VOID **)&XenIo,
  200. This->DriverBindingHandle,
  201. ControllerHandle,
  202. EFI_OPEN_PROTOCOL_BY_DRIVER
  203. );
  204. if (EFI_ERROR (Status)) {
  205. return Status;
  206. }
  207. gBS->CloseProtocol (
  208. ControllerHandle,
  209. &gXenIoProtocolGuid,
  210. This->DriverBindingHandle,
  211. ControllerHandle
  212. );
  213. return Status;
  214. }
  215. VOID
  216. EFIAPI
  217. NotifyExitBoot (
  218. IN EFI_EVENT Event,
  219. IN VOID *Context
  220. )
  221. {
  222. XENBUS_DEVICE *Dev = Context;
  223. gBS->DisconnectController (
  224. Dev->ControllerHandle,
  225. Dev->This->DriverBindingHandle,
  226. NULL
  227. );
  228. }
  229. /**
  230. Starts a bus controller.
  231. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  232. As a result, much of the error checking on the parameters to Start() has been moved into this
  233. common boot service. It is legal to call Start() from other locations,
  234. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  235. 1. ControllerHandle must be a valid EFI_HANDLE.
  236. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  237. EFI_DEVICE_PATH_PROTOCOL.
  238. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  239. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  240. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  241. @param[in] ControllerHandle The handle of the controller to start. This handle
  242. must support a protocol interface that supplies
  243. an I/O abstraction to the driver.
  244. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  245. parameter is ignored by device drivers, and is optional for bus
  246. drivers. For a bus driver, if this parameter is NULL, then handles
  247. for all the children of Controller are created by this driver.
  248. If this parameter is not NULL and the first Device Path Node is
  249. not the End of Device Path Node, then only the handle for the
  250. child device specified by the first Device Path Node of
  251. RemainingDevicePath is created by this driver.
  252. If the first Device Path Node of RemainingDevicePath is
  253. the End of Device Path Node, no child handle is created by this
  254. driver.
  255. @retval EFI_SUCCESS The device was started.
  256. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  257. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  258. @retval EFI_UNSUPPORTED Something is missing on the system that
  259. prevent to start the device.
  260. @retval Others The driver failed to start the device.
  261. **/
  262. EFI_STATUS
  263. EFIAPI
  264. XenBusDxeDriverBindingStart (
  265. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  266. IN EFI_HANDLE ControllerHandle,
  267. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
  268. )
  269. {
  270. EFI_STATUS Status;
  271. XENBUS_DEVICE *Dev;
  272. XENIO_PROTOCOL *XenIo;
  273. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  274. Status = gBS->OpenProtocol (
  275. ControllerHandle,
  276. &gXenIoProtocolGuid,
  277. (VOID **)&XenIo,
  278. This->DriverBindingHandle,
  279. ControllerHandle,
  280. EFI_OPEN_PROTOCOL_BY_DRIVER
  281. );
  282. if (EFI_ERROR (Status)) {
  283. return Status;
  284. }
  285. Status = gBS->OpenProtocol (
  286. ControllerHandle,
  287. &gEfiDevicePathProtocolGuid,
  288. (VOID **)&DevicePath,
  289. This->DriverBindingHandle,
  290. ControllerHandle,
  291. EFI_OPEN_PROTOCOL_BY_DRIVER
  292. );
  293. if (EFI_ERROR (Status)) {
  294. goto ErrorOpenningProtocol;
  295. }
  296. Dev = AllocateZeroPool (sizeof (*Dev));
  297. Dev->Signature = XENBUS_DEVICE_SIGNATURE;
  298. Dev->This = This;
  299. Dev->ControllerHandle = ControllerHandle;
  300. Dev->XenIo = XenIo;
  301. Dev->DevicePath = DevicePath;
  302. InitializeListHead (&Dev->ChildList);
  303. EfiAcquireLock (&mMyDeviceLock);
  304. if (mMyDevice != NULL) {
  305. EfiReleaseLock (&mMyDeviceLock);
  306. //
  307. // There is already a XenBus running, only one can be used at a time.
  308. //
  309. Status = EFI_ALREADY_STARTED;
  310. goto ErrorAllocated;
  311. }
  312. mMyDevice = Dev;
  313. EfiReleaseLock (&mMyDeviceLock);
  314. Status = XenGetSharedInfoPage (Dev);
  315. if (EFI_ERROR (Status)) {
  316. DEBUG ((DEBUG_ERROR, "XenBus: Unable to get the shared info page.\n"));
  317. Status = EFI_UNSUPPORTED;
  318. goto ErrorAllocated;
  319. }
  320. XenGrantTableInit (Dev);
  321. Status = XenStoreInit (Dev);
  322. ASSERT_EFI_ERROR (Status);
  323. XenBusEnumerateBus (Dev);
  324. Status = gBS->CreateEvent (
  325. EVT_SIGNAL_EXIT_BOOT_SERVICES,
  326. TPL_CALLBACK,
  327. NotifyExitBoot,
  328. (VOID *)Dev,
  329. &Dev->ExitBootEvent
  330. );
  331. ASSERT_EFI_ERROR (Status);
  332. return EFI_SUCCESS;
  333. ErrorAllocated:
  334. FreePool (Dev);
  335. gBS->CloseProtocol (
  336. ControllerHandle,
  337. &gEfiDevicePathProtocolGuid,
  338. This->DriverBindingHandle,
  339. ControllerHandle
  340. );
  341. ErrorOpenningProtocol:
  342. gBS->CloseProtocol (
  343. ControllerHandle,
  344. &gXenIoProtocolGuid,
  345. This->DriverBindingHandle,
  346. ControllerHandle
  347. );
  348. return Status;
  349. }
  350. /**
  351. Stops a bus controller.
  352. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  353. As a result, much of the error checking on the parameters to Stop() has been moved
  354. into this common boot service. It is legal to call Stop() from other locations,
  355. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  356. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  357. same driver's Start() function.
  358. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  359. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  360. Start() function, and the Start() function must have called OpenProtocol() on
  361. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  362. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  363. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  364. support a bus specific I/O protocol for the driver
  365. to use to stop the device.
  366. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  367. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  368. if NumberOfChildren is 0.
  369. @retval EFI_SUCCESS The device was stopped.
  370. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  371. **/
  372. EFI_STATUS
  373. EFIAPI
  374. XenBusDxeDriverBindingStop (
  375. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  376. IN EFI_HANDLE ControllerHandle,
  377. IN UINTN NumberOfChildren,
  378. IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
  379. )
  380. {
  381. UINTN Index;
  382. XENBUS_PROTOCOL *XenBusIo;
  383. XENBUS_PRIVATE_DATA *ChildData;
  384. EFI_STATUS Status;
  385. XENBUS_DEVICE *Dev = mMyDevice;
  386. for (Index = 0; Index < NumberOfChildren; Index++) {
  387. Status = gBS->OpenProtocol (
  388. ChildHandleBuffer[Index],
  389. &gXenBusProtocolGuid,
  390. (VOID **)&XenBusIo,
  391. This->DriverBindingHandle,
  392. ControllerHandle,
  393. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  394. );
  395. if (EFI_ERROR (Status)) {
  396. DEBUG ((DEBUG_ERROR, "XenBusDxe: get children protocol failed: %r\n", Status));
  397. continue;
  398. }
  399. ChildData = XENBUS_PRIVATE_DATA_FROM_THIS (XenBusIo);
  400. Status = gBS->CloseProtocol (
  401. Dev->ControllerHandle,
  402. &gXenIoProtocolGuid,
  403. Dev->This->DriverBindingHandle,
  404. ChildData->Handle
  405. );
  406. ASSERT_EFI_ERROR (Status);
  407. Status = gBS->UninstallMultipleProtocolInterfaces (
  408. ChildData->Handle,
  409. &gEfiDevicePathProtocolGuid,
  410. ChildData->DevicePath,
  411. &gXenBusProtocolGuid,
  412. &ChildData->XenBusIo,
  413. NULL
  414. );
  415. ASSERT_EFI_ERROR (Status);
  416. FreePool ((VOID *)ChildData->XenBusIo.Type);
  417. FreePool ((VOID *)ChildData->XenBusIo.Node);
  418. FreePool ((VOID *)ChildData->XenBusIo.Backend);
  419. FreePool (ChildData->DevicePath);
  420. RemoveEntryList (&ChildData->Link);
  421. FreePool (ChildData);
  422. }
  423. if (NumberOfChildren > 0) {
  424. return EFI_SUCCESS;
  425. }
  426. gBS->CloseEvent (Dev->ExitBootEvent);
  427. XenStoreDeinit (Dev);
  428. XenGrantTableDeinit (Dev);
  429. gBS->CloseProtocol (
  430. ControllerHandle,
  431. &gEfiDevicePathProtocolGuid,
  432. This->DriverBindingHandle,
  433. ControllerHandle
  434. );
  435. gBS->CloseProtocol (
  436. ControllerHandle,
  437. &gXenIoProtocolGuid,
  438. This->DriverBindingHandle,
  439. ControllerHandle
  440. );
  441. mMyDevice = NULL;
  442. FreePool (Dev);
  443. return EFI_SUCCESS;
  444. }